1 // SPDX-License-Identifier: GPL-2.0
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7
8 #include "kvm_util.h"
9 #include "processor.h"
10
11 #define CPUID_MWAIT (1u << 3)
12
13 enum monitor_mwait_testcases {
14 MWAIT_QUIRK_DISABLED = BIT(0),
15 MISC_ENABLES_QUIRK_DISABLED = BIT(1),
16 MWAIT_DISABLED = BIT(2),
17 };
18
guest_monitor_wait(int testcase)19 static void guest_monitor_wait(int testcase)
20 {
21 /*
22 * If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD,
23 * in all other scenarios KVM should emulate them as nops.
24 */
25 bool fault_wanted = (testcase & MWAIT_QUIRK_DISABLED) &&
26 (testcase & MWAIT_DISABLED);
27 u8 vector;
28
29 GUEST_SYNC(testcase);
30
31 /*
32 * Arbitrarily MONITOR this function, SVM performs fault checks before
33 * intercept checks, so the inputs for MONITOR and MWAIT must be valid.
34 */
35 vector = kvm_asm_safe("monitor", "a"(guest_monitor_wait), "c"(0), "d"(0));
36 if (fault_wanted)
37 GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
38 else
39 GUEST_ASSERT_2(!vector, testcase, vector);
40
41 vector = kvm_asm_safe("mwait", "a"(guest_monitor_wait), "c"(0), "d"(0));
42 if (fault_wanted)
43 GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
44 else
45 GUEST_ASSERT_2(!vector, testcase, vector);
46 }
47
guest_code(void)48 static void guest_code(void)
49 {
50 guest_monitor_wait(MWAIT_DISABLED);
51
52 guest_monitor_wait(MWAIT_QUIRK_DISABLED | MWAIT_DISABLED);
53
54 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_DISABLED);
55 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED);
56
57 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_QUIRK_DISABLED | MWAIT_DISABLED);
58 guest_monitor_wait(MISC_ENABLES_QUIRK_DISABLED | MWAIT_QUIRK_DISABLED);
59
60 GUEST_DONE();
61 }
62
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65 uint64_t disabled_quirks;
66 struct kvm_vcpu *vcpu;
67 struct kvm_run *run;
68 struct kvm_vm *vm;
69 struct ucall uc;
70 int testcase;
71
72 TEST_REQUIRE(kvm_has_cap(KVM_CAP_DISABLE_QUIRKS2));
73
74 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
75 vcpu_clear_cpuid_feature(vcpu, X86_FEATURE_MWAIT);
76
77 run = vcpu->run;
78
79 vm_init_descriptor_tables(vm);
80 vcpu_init_descriptor_tables(vcpu);
81
82 while (1) {
83 vcpu_run(vcpu);
84
85 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
86 "Unexpected exit reason: %u (%s),\n",
87 run->exit_reason,
88 exit_reason_str(run->exit_reason));
89
90 switch (get_ucall(vcpu, &uc)) {
91 case UCALL_SYNC:
92 testcase = uc.args[1];
93 break;
94 case UCALL_ABORT:
95 REPORT_GUEST_ASSERT_2(uc, "testcase = %lx, vector = %ld");
96 goto done;
97 case UCALL_DONE:
98 goto done;
99 default:
100 TEST_FAIL("Unknown ucall %lu", uc.cmd);
101 goto done;
102 }
103
104 disabled_quirks = 0;
105 if (testcase & MWAIT_QUIRK_DISABLED)
106 disabled_quirks |= KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS;
107 if (testcase & MISC_ENABLES_QUIRK_DISABLED)
108 disabled_quirks |= KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT;
109 vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, disabled_quirks);
110
111 /*
112 * If the MISC_ENABLES quirk (KVM neglects to update CPUID to
113 * enable/disable MWAIT) is disabled, toggle the ENABLE_MWAIT
114 * bit in MISC_ENABLES accordingly. If the quirk is enabled,
115 * the only valid configuration is MWAIT disabled, as CPUID
116 * can't be manually changed after running the vCPU.
117 */
118 if (!(testcase & MISC_ENABLES_QUIRK_DISABLED)) {
119 TEST_ASSERT(testcase & MWAIT_DISABLED,
120 "Can't toggle CPUID features after running vCPU");
121 continue;
122 }
123
124 vcpu_set_msr(vcpu, MSR_IA32_MISC_ENABLE,
125 (testcase & MWAIT_DISABLED) ? 0 : MSR_IA32_MISC_ENABLE_MWAIT);
126 }
127
128 done:
129 kvm_vm_free(vm);
130 return 0;
131 }
132