1 // SPDX-License-Identifier: GPL-2.0
11 #define CPUID_MWAIT (1u << 3)
13 enum monitor_mwait_testcases {
14 MWAIT_QUIRK_DISABLED = BIT(0),
15 MISC_ENABLES_QUIRK_DISABLED = BIT(1),
16 MWAIT_DISABLED = BIT(2),
20 * If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD, in all
21 * other scenarios KVM should emulate them as nops.
23 #define GUEST_ASSERT_MONITOR_MWAIT(insn, testcase, vector) \
25 bool fault_wanted = ((testcase) & MWAIT_QUIRK_DISABLED) && \
26 ((testcase) & MWAIT_DISABLED); \
29 __GUEST_ASSERT((vector) == UD_VECTOR, \
30 "Expected #UD on " insn " for testcase '0x%x', got '0x%x'", vector); \
32 __GUEST_ASSERT(!(vector), \
33 "Expected success on " insn " for testcase '0x%x', got '0x%x'", vector); \
36 static void guest_monitor_wait(int testcase)
43 * Arbitrarily MONITOR this function, SVM performs fault checks before
44 * intercept checks, so the inputs for MONITOR and MWAIT must be valid.
46 vector = kvm_asm_safe("monitor", "a"(guest_monitor_wait), "c"(0), "d"(0));
47 GUEST_ASSERT_MONITOR_MWAIT("MONITOR", testcase, vector);
49 vector = kvm_asm_safe("mwait", "a"(guest_monitor_wait), "c"(0), "d"(0));
50 GUEST_ASSERT_MONITOR_MWAIT("MWAIT", testcase, vector);
53 static void guest_code(void)
55 guest_monitor_wait(MWAIT_DISABLED);
57 guest_monitor_wait(MWAIT_QUIRK_DISABLED | MWAIT_DISABLED);
59 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_DISABLED);
60 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED);
62 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_QUIRK_DISABLED | MWAIT_DISABLED);
63 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_QUIRK_DISABLED);
68 int main(int argc, char *argv[])
70 uint64_t disabled_quirks;
71 struct kvm_vcpu *vcpu;
76 TEST_REQUIRE(kvm_has_cap(KVM_CAP_DISABLE_QUIRKS2));
78 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
79 vcpu_clear_cpuid_feature(vcpu, X86_FEATURE_MWAIT);
81 vm_init_descriptor_tables(vm);
82 vcpu_init_descriptor_tables(vcpu);
86 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
88 switch (get_ucall(vcpu, &uc)) {
90 testcase = uc.args[1];
93 REPORT_GUEST_ASSERT(uc);
98 TEST_FAIL("Unknown ucall %lu", uc.cmd);
103 if (testcase & MWAIT_QUIRK_DISABLED)
104 disabled_quirks |= KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS;
105 if (testcase & MISC_ENABLES_QUIRK_DISABLED)
106 disabled_quirks |= KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT;
107 vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, disabled_quirks);
110 * If the MISC_ENABLES quirk (KVM neglects to update CPUID to
111 * enable/disable MWAIT) is disabled, toggle the ENABLE_MWAIT
112 * bit in MISC_ENABLES accordingly. If the quirk is enabled,
113 * the only valid configuration is MWAIT disabled, as CPUID
114 * can't be manually changed after running the vCPU.
116 if (!(testcase & MISC_ENABLES_QUIRK_DISABLED)) {
117 TEST_ASSERT(testcase & MWAIT_DISABLED,
118 "Can't toggle CPUID features after running vCPU");
122 vcpu_set_msr(vcpu, MSR_IA32_MISC_ENABLE,
123 (testcase & MWAIT_DISABLED) ? 0 : MSR_IA32_MISC_ENABLE_MWAIT);