File Index
Files and directories
Sign in
FILE

ebreak_test.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/kvm/riscv/ebreak_test.c C
 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#include "ucall_common.h"
10
11#define LABEL_ADDRESS(v) ((uint64_t)&(v))
12
13extern unsigned char sw_bp_1, sw_bp_2;
14static uint64_t sw_bp_addr;
15
16static void guest_code(void)
17{
18	asm volatile(
19		".option push\n"
20		".option norvc\n"
21		"sw_bp_1: ebreak\n"
22		"sw_bp_2: ebreak\n"
23		".option pop\n"
24	);
25	GUEST_ASSERT_EQ(READ_ONCE(sw_bp_addr), LABEL_ADDRESS(sw_bp_2));
26
27	GUEST_DONE();
28}
29
30static void guest_breakpoint_handler(struct pt_regs *regs)
31{
32	WRITE_ONCE(sw_bp_addr, regs->epc);
33	regs->epc += 4;
34}
35
36int main(void)
37{
38	struct kvm_vm *vm;
39	struct kvm_vcpu *vcpu;
40	uint64_t pc;
41	struct kvm_guest_debug debug = {
42		.control = KVM_GUESTDBG_ENABLE,
43	};
44
45	TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG));
46
47	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
48
49	vm_init_vector_tables(vm);
50	vcpu_init_vector_tables(vcpu);
51	vm_install_exception_handler(vm, EXC_BREAKPOINT,
52					guest_breakpoint_handler);
53
54	/*
55	 * Enable the guest debug.
56	 * ebreak should exit to the VMM with KVM_EXIT_DEBUG reason.
57	 */
58	vcpu_guest_debug_set(vcpu, &debug);
59	vcpu_run(vcpu);
60
61	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG);
62
63	pc = vcpu_get_reg(vcpu, RISCV_CORE_REG(regs.pc));
64	TEST_ASSERT_EQ(pc, LABEL_ADDRESS(sw_bp_1));
65
66	/* skip sw_bp_1 */
67	vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), pc + 4);
68
69	/*
70	 * Disable all debug controls.
71	 * Guest should handle the ebreak without exiting to the VMM.
72	 */
73	memset(&debug, 0, sizeof(debug));
74	vcpu_guest_debug_set(vcpu, &debug);
75
76	vcpu_run(vcpu);
77
78	TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
79
80	kvm_vm_free(vm);
81
82	return 0;
83}
~/ Read-only file preview.