|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * RISC-V KVM ebreak test. |
| 4 | + * |
| 5 | + * Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. |
| 6 | + * |
| 7 | + */ |
| 8 | +#include "kvm_util.h" |
| 9 | + |
| 10 | +#define LABEL_ADDRESS(v) ((uint64_t)&(v)) |
| 11 | + |
| 12 | +extern unsigned char sw_bp_1, sw_bp_2; |
| 13 | +static uint64_t sw_bp_addr; |
| 14 | + |
| 15 | +static void guest_code(void) |
| 16 | +{ |
| 17 | + asm 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 | + ); |
| 24 | + GUEST_ASSERT_EQ(READ_ONCE(sw_bp_addr), LABEL_ADDRESS(sw_bp_2)); |
| 25 | + |
| 26 | + GUEST_DONE(); |
| 27 | +} |
| 28 | + |
| 29 | +static void guest_breakpoint_handler(struct ex_regs *regs) |
| 30 | +{ |
| 31 | + WRITE_ONCE(sw_bp_addr, regs->epc); |
| 32 | + regs->epc += 4; |
| 33 | +} |
| 34 | + |
| 35 | +int main(void) |
| 36 | +{ |
| 37 | + struct kvm_vm *vm; |
| 38 | + struct kvm_vcpu *vcpu; |
| 39 | + uint64_t pc; |
| 40 | + struct kvm_guest_debug debug = { |
| 41 | + .control = KVM_GUESTDBG_ENABLE, |
| 42 | + }; |
| 43 | + |
| 44 | + TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG)); |
| 45 | + |
| 46 | + vm = vm_create_with_one_vcpu(&vcpu, guest_code); |
| 47 | + |
| 48 | + vm_init_vector_tables(vm); |
| 49 | + vcpu_init_vector_tables(vcpu); |
| 50 | + vm_install_exception_handler(vm, EXC_BREAKPOINT, |
| 51 | + guest_breakpoint_handler); |
| 52 | + |
| 53 | + /* |
| 54 | + * Enable the guest debug. |
| 55 | + * ebreak should exit to the VMM with KVM_EXIT_DEBUG reason. |
| 56 | + */ |
| 57 | + vcpu_guest_debug_set(vcpu, &debug); |
| 58 | + vcpu_run(vcpu); |
| 59 | + |
| 60 | + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG); |
| 61 | + |
| 62 | + vcpu_get_reg(vcpu, RISCV_CORE_REG(regs.pc), &pc); |
| 63 | + TEST_ASSERT_EQ(pc, LABEL_ADDRESS(sw_bp_1)); |
| 64 | + |
| 65 | + /* skip sw_bp_1 */ |
| 66 | + vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), pc + 4); |
| 67 | + |
| 68 | + /* |
| 69 | + * Disable all debug controls. |
| 70 | + * Guest should handle the ebreak without exiting to the VMM. |
| 71 | + */ |
| 72 | + memset(&debug, 0, sizeof(debug)); |
| 73 | + vcpu_guest_debug_set(vcpu, &debug); |
| 74 | + |
| 75 | + vcpu_run(vcpu); |
| 76 | + |
| 77 | + TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); |
| 78 | + |
| 79 | + kvm_vm_free(vm); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments