Path: blob/master/tools/testing/selftests/kvm/riscv/ebreak_test.c
38237 views
// SPDX-License-Identifier: GPL-2.01/*2* RISC-V KVM ebreak test.3*4* Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd.5*6*/7#include "kvm_util.h"8#include "ucall_common.h"910#define LABEL_ADDRESS(v) ((uint64_t)&(v))1112extern unsigned char sw_bp_1, sw_bp_2;13static uint64_t sw_bp_addr;1415static void guest_code(void)16{17asm volatile(18".option push\n"19".option norvc\n"20"sw_bp_1: ebreak\n"21"sw_bp_2: ebreak\n"22".option pop\n"23);24GUEST_ASSERT_EQ(READ_ONCE(sw_bp_addr), LABEL_ADDRESS(sw_bp_2));2526GUEST_DONE();27}2829static void guest_breakpoint_handler(struct pt_regs *regs)30{31WRITE_ONCE(sw_bp_addr, regs->epc);32regs->epc += 4;33}3435int main(void)36{37struct kvm_vm *vm;38struct kvm_vcpu *vcpu;39uint64_t pc;40struct kvm_guest_debug debug = {41.control = KVM_GUESTDBG_ENABLE,42};4344TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG));4546vm = vm_create_with_one_vcpu(&vcpu, guest_code);4748vm_init_vector_tables(vm);49vcpu_init_vector_tables(vcpu);50vm_install_exception_handler(vm, EXC_BREAKPOINT,51guest_breakpoint_handler);5253/*54* Enable the guest debug.55* ebreak should exit to the VMM with KVM_EXIT_DEBUG reason.56*/57vcpu_guest_debug_set(vcpu, &debug);58vcpu_run(vcpu);5960TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG);6162pc = vcpu_get_reg(vcpu, RISCV_CORE_REG(regs.pc));63TEST_ASSERT_EQ(pc, LABEL_ADDRESS(sw_bp_1));6465/* skip sw_bp_1 */66vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), pc + 4);6768/*69* Disable all debug controls.70* Guest should handle the ebreak without exiting to the VMM.71*/72memset(&debug, 0, sizeof(debug));73vcpu_guest_debug_set(vcpu, &debug);7475vcpu_run(vcpu);7677TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);7879kvm_vm_free(vm);8081return 0;82}838485