1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test for x86 KVM_CAP_HYPERV_CPUID
4 *
5 * Copyright (C) 2018, Red Hat, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2.
8 *
9 */
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/ioctl.h>
15
16 #include "test_util.h"
17 #include "kvm_util.h"
18 #include "processor.h"
19 #include "vmx.h"
20
guest_code(void)21 static void guest_code(void)
22 {
23 }
24
smt_possible(void)25 static bool smt_possible(void)
26 {
27 char buf[16];
28 FILE *f;
29 bool res = true;
30
31 f = fopen("/sys/devices/system/cpu/smt/control", "r");
32 if (f) {
33 if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
34 if (!strncmp(buf, "forceoff", 8) ||
35 !strncmp(buf, "notsupported", 12))
36 res = false;
37 }
38 fclose(f);
39 }
40
41 return res;
42 }
43
test_hv_cpuid(struct kvm_vcpu * vcpu,bool evmcs_expected)44 static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
45 {
46 const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
47 const struct kvm_cpuid2 *hv_cpuid_entries;
48 int i;
49 int nent_expected = 10;
50 u32 test_val;
51
52 if (vcpu)
53 hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
54 else
55 hv_cpuid_entries = kvm_get_supported_hv_cpuid();
56
57 TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
58 "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
59 " (returned %d)",
60 nent_expected, hv_cpuid_entries->nent);
61
62 for (i = 0; i < hv_cpuid_entries->nent; i++) {
63 const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
64
65 TEST_ASSERT((entry->function >= 0x40000000) &&
66 (entry->function <= 0x40000082),
67 "function %x is our of supported range",
68 entry->function);
69
70 TEST_ASSERT(entry->index == 0,
71 ".index field should be zero");
72
73 TEST_ASSERT(entry->flags == 0,
74 ".flags field should be zero");
75
76 TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
77 !entry->padding[2], "padding should be zero");
78
79 switch (entry->function) {
80 case 0x40000000:
81 test_val = 0x40000082;
82
83 TEST_ASSERT(entry->eax == test_val,
84 "Wrong max leaf report in 0x40000000.EAX: %x"
85 " (evmcs=%d)",
86 entry->eax, evmcs_expected
87 );
88 break;
89 case 0x40000003:
90 TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),
91 "\"Direct\" Synthetic Timers should require in-kernel APIC");
92 break;
93 case 0x40000004:
94 test_val = entry->eax & (1UL << 18);
95
96 TEST_ASSERT(!!test_val == !smt_possible(),
97 "NoNonArchitecturalCoreSharing bit"
98 " doesn't reflect SMT setting");
99
100 TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),
101 "Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");
102 break;
103 case 0x4000000A:
104 TEST_ASSERT(entry->eax & (1UL << 19),
105 "Enlightened MSR-Bitmap should always be supported"
106 " 0x40000000.EAX: %x", entry->eax);
107 if (evmcs_expected)
108 TEST_ASSERT((entry->eax & 0xffff) == 0x101,
109 "Supported Enlightened VMCS version range is supposed to be 1:1"
110 " 0x40000000.EAX: %x", entry->eax);
111
112 break;
113 default:
114 break;
115
116 }
117 /*
118 * If needed for debug:
119 * fprintf(stdout,
120 * "CPUID%lx EAX=0x%lx EBX=0x%lx ECX=0x%lx EDX=0x%lx\n",
121 * entry->function, entry->eax, entry->ebx, entry->ecx,
122 * entry->edx);
123 */
124 }
125
126 /*
127 * Note, the CPUID array returned by the system-scoped helper is a one-
128 * time allocation, i.e. must not be freed.
129 */
130 if (vcpu)
131 free((void *)hv_cpuid_entries);
132 }
133
test_hv_cpuid_e2big(struct kvm_vm * vm,struct kvm_vcpu * vcpu)134 static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
135 {
136 static struct kvm_cpuid2 cpuid = {.nent = 0};
137 int ret;
138
139 if (vcpu)
140 ret = __vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
141 else
142 ret = __kvm_ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, &cpuid);
143
144 TEST_ASSERT(ret == -1 && errno == E2BIG,
145 "%s KVM_GET_SUPPORTED_HV_CPUID didn't fail with -E2BIG when"
146 " it should have: %d %d", !vcpu ? "KVM" : "vCPU", ret, errno);
147 }
148
main(int argc,char * argv[])149 int main(int argc, char *argv[])
150 {
151 struct kvm_vm *vm;
152 struct kvm_vcpu *vcpu;
153
154 TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
155
156 /* Test the vCPU ioctl without an in-kernel local APIC. */
157 vm = vm_create_barebones();
158 vcpu = __vm_vcpu_add(vm, 0);
159 test_hv_cpuid(vcpu, false);
160 kvm_vm_free(vm);
161
162 /* Test vCPU ioctl version */
163 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
164 test_hv_cpuid_e2big(vm, vcpu);
165 test_hv_cpuid(vcpu, false);
166
167 if (!kvm_cpu_has(X86_FEATURE_VMX) ||
168 !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
169 print_skip("Enlightened VMCS is unsupported");
170 goto do_sys;
171 }
172 vcpu_enable_evmcs(vcpu);
173 test_hv_cpuid(vcpu, true);
174
175 do_sys:
176 /* Test system ioctl version */
177 if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
178 print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
179 goto out;
180 }
181
182 test_hv_cpuid_e2big(vm, NULL);
183 test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
184
185 out:
186 kvm_vm_free(vm);
187
188 return 0;
189 }
190