Add tests to verify that DEBUGCTLMSR_BUS_LOCK_DETECT state is correctly handled during VMX transitions. Wrap the VM-entry/VM-exit DEBUGCTL transition flow into a shared helper so follow-up DEBUGCTL feature tests can reuse the same checks. The following behaviors are verified in the helper: 1. Guest states are loaded from VMCS iff "Load debug controls" VM-Entry control is set. 2. Guest states are saved to VMCS iff "Save debug controls" VM-Exit control is set. 3. DEBUGCTL MSR is cleared after VM exits. Signed-off-by: Chenyi Qiang --- lib/x86/msr.h | 1 + x86/unittests.cfg | 8 ++++ x86/vmx_tests.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/lib/x86/msr.h b/lib/x86/msr.h index 7397809c..da75740d 100644 --- a/lib/x86/msr.h +++ b/lib/x86/msr.h @@ -103,6 +103,7 @@ /* DEBUGCTLMSR bits (others vary by model): */ #define DEBUGCTLMSR_LBR (1UL << 0) /* last branch recording */ #define DEBUGCTLMSR_BTF (1UL << 1) /* single-step on branches */ +#define DEBUGCTLMSR_BUS_LOCK_DETECT (1UL << 2) #define DEBUGCTLMSR_TR (1UL << 6) #define DEBUGCTLMSR_BTS (1UL << 7) #define DEBUGCTLMSR_BTINT (1UL << 8) diff --git a/x86/unittests.cfg b/x86/unittests.cfg index 48835eba..c8426770 100644 --- a/x86/unittests.cfg +++ b/x86/unittests.cfg @@ -471,6 +471,14 @@ arch = x86_64 groups = vmx timeout = 240 +[vmx_bus_lock_detect_test] +file = vmx.flat +test_args = "vmx_bus_lock_detect_test" +qemu_params = -cpu max,+vmx +arch = x86_64 +groups = vmx +timeout = 240 + [debug] file = debug.flat arch = x86_64 diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 91f417c6..a3e52d3b 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -11737,6 +11737,100 @@ static void vmx_cet_test(void) test_set_guest_finished(); } +static struct vmx_debugctl_test_data { + u64 mask; + u64 guest_debugctl; +} vmx_debugctl_test_data; + +static void vmx_debugctl_test_guest(void) +{ + vmx_debugctl_test_data.guest_debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); + vmcall(); + + wrmsr(MSR_IA32_DEBUGCTLMSR, vmx_debugctl_test_data.mask); + vmcall(); + + vmx_debugctl_test_data.guest_debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); + vmcall(); + + wrmsr(MSR_IA32_DEBUGCTLMSR, vmx_debugctl_test_data.mask); + vmcall(); +} + +static void vmx_debugctl_transition_test(const char *label, u64 mask) +{ + u64 debugctl; + + vmx_debugctl_test_data.mask = mask; + + msr_bmp_init(); + wrmsr(MSR_IA32_DEBUGCTLMSR, mask); + vmcs_write(GUEST_DEBUGCTL, 0x0); + + /* Validate when save/load debug controls are set */ + vmcs_set_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS); + vmcs_set_bits(EXI_CONTROLS, EXI_SAVE_DBGCTLS); + + test_set_guest(vmx_debugctl_test_guest); + + enter_guest(); + skip_exit_vmcall(); + /* Validate VM-entry loads the guest DEBUGCTL value from GUEST_DEBUGCTL. */ + report(vmx_debugctl_test_data.guest_debugctl == 0x0, + "%s: Load debug controls", label); + + enter_guest(); + skip_exit_vmcall(); + debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); + /* Validate VM-exit clears host DEBUGCTL and saves guest DEBUGCTL into the VMCS. */ + report(debugctl == 0x0 && vmcs_read(GUEST_DEBUGCTL) == mask, + "%s: Save debug controls", label); + + if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS || + ctrl_exit_rev.set & EXI_SAVE_DBGCTLS) { + printf("\tDebug controls are always loaded/saved\n"); + test_set_guest_finished(); + return; + } + + wrmsr(MSR_IA32_DEBUGCTLMSR, mask); + vmcs_write(GUEST_DEBUGCTL, 0x0); + /* Validate when save/load debug controls are clear */ + vmcs_clear_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS); + vmcs_clear_bits(EXI_CONTROLS, EXI_SAVE_DBGCTLS); + + enter_guest(); + skip_exit_vmcall(); + /* Validate guest observes the host's DEBUGCTL value. */ + report(vmx_debugctl_test_data.guest_debugctl == mask, + "%s: Guest=host debug controls", label); + + wrmsr(MSR_IA32_DEBUGCTLMSR, 0x0); + vmcs_write(GUEST_DEBUGCTL, 0x0); + + enter_guest(); + skip_exit_vmcall(); + debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); + /* Validate VM-exit clears host DEBUGCTL and does not save guest DEBUGCTL into the VMCS. */ + report(debugctl == 0x0 && vmcs_read(GUEST_DEBUGCTL) == 0x0, + "%s: Don't save debug controls", label); + + enter_guest(); +} + +static void vmx_bus_lock_detect_test(void) +{ + + if (!this_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) { + report_skip("%s : \"Bus Lock Detect\" not supported", __func__); + return; + } + + vmx_debugctl_transition_test("DEBUGCTLMSR.BUS_LOCK_DETECT", + DEBUGCTLMSR_BUS_LOCK_DETECT); + report_pass(__func__); +} + #define TEST(name) { #name, .v2 = name } /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ @@ -11858,5 +11952,7 @@ struct vmx_test vmx_tests[] = { TEST(vmx_canonical_test), /* "Load CET" VM-entry/exit controls tests. */ TEST(vmx_cet_test), + /* "debug controls VM-entry/exit controls tests" for DEBUGCTLMSR_BUS_LOCK_DETECT */ + TEST(vmx_bus_lock_detect_test), { NULL, NULL, NULL, NULL, NULL, {0} }, }; -- 2.43.5