Skip to content

Commit f1c48c1

Browse files
Du-Chaoavpatel
authored andcommitted
RISC-V: KVM: selftests: Add ebreak test support
Initial support for RISC-V KVM ebreak test. Check the exit reason and the PC when guest debug is enabled. Also to make sure the guest could handle the ebreak exception without exiting to the VMM when guest debug is not enabled. Signed-off-by: Chao Du <duchao@eswincomputing.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Link: https://lore.kernel.org/r/20240402062628.5425-4-duchao@eswincomputing.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 1df1fb5 commit f1c48c1

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

tools/testing/selftests/kvm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ TEST_GEN_PROGS_s390x += rseq_test
189189
TEST_GEN_PROGS_s390x += set_memory_region_test
190190
TEST_GEN_PROGS_s390x += kvm_binary_stats_test
191191

192+
TEST_GEN_PROGS_riscv += riscv/ebreak_test
192193
TEST_GEN_PROGS_riscv += arch_timer
193194
TEST_GEN_PROGS_riscv += demand_paging_test
194195
TEST_GEN_PROGS_riscv += dirty_log_test
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)