1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel-based Virtual Machine driver for Linux
4 * cpuid support routines
5 *
6 * derived from arch/x86/kvm/x86.c
7 *
8 * Copyright 2011 Red Hat, Inc. and/or its affiliates.
9 * Copyright IBM Corporation, 2008
10 */
11
12 #include <linux/kvm_host.h>
13 #include <linux/export.h>
14 #include <linux/vmalloc.h>
15 #include <linux/uaccess.h>
16 #include <linux/sched/stat.h>
17
18 #include <asm/processor.h>
19 #include <asm/user.h>
20 #include <asm/fpu/xstate.h>
21 #include <asm/sgx.h>
22 #include "cpuid.h"
23 #include "lapic.h"
24 #include "mmu.h"
25 #include "trace.h"
26 #include "pmu.h"
27
28 /*
29 * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
30 * aligned to sizeof(unsigned long) because it's not accessed via bitops.
31 */
32 u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
33 EXPORT_SYMBOL_GPL(kvm_cpu_caps);
34
xstate_required_size(u64 xstate_bv,bool compacted)35 static u32 xstate_required_size(u64 xstate_bv, bool compacted)
36 {
37 int feature_bit = 0;
38 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
39
40 xstate_bv &= XFEATURE_MASK_EXTEND;
41 while (xstate_bv) {
42 if (xstate_bv & 0x1) {
43 u32 eax, ebx, ecx, edx, offset;
44 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
45 offset = compacted ? ret : ebx;
46 ret = max(ret, offset + eax);
47 }
48
49 xstate_bv >>= 1;
50 feature_bit++;
51 }
52
53 return ret;
54 }
55
56 #define F feature_bit
57 #define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)
58
cpuid_entry2_find(struct kvm_cpuid_entry2 * entries,int nent,u32 function,u32 index)59 static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
60 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)
61 {
62 struct kvm_cpuid_entry2 *e;
63 int i;
64
65 for (i = 0; i < nent; i++) {
66 e = &entries[i];
67
68 if (e->function == function &&
69 (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index))
70 return e;
71 }
72
73 return NULL;
74 }
75
kvm_check_cpuid(struct kvm_cpuid_entry2 * entries,int nent)76 static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent)
77 {
78 struct kvm_cpuid_entry2 *best;
79
80 /*
81 * The existing code assumes virtual address is 48-bit or 57-bit in the
82 * canonical address checks; exit if it is ever changed.
83 */
84 best = cpuid_entry2_find(entries, nent, 0x80000008, 0);
85 if (best) {
86 int vaddr_bits = (best->eax & 0xff00) >> 8;
87
88 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
89 return -EINVAL;
90 }
91
92 return 0;
93 }
94
kvm_update_pv_runtime(struct kvm_vcpu * vcpu)95 void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
96 {
97 struct kvm_cpuid_entry2 *best;
98
99 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
100
101 /*
102 * save the feature bitmap to avoid cpuid lookup for every PV
103 * operation
104 */
105 if (best)
106 vcpu->arch.pv_cpuid.features = best->eax;
107 }
108
kvm_update_cpuid_runtime(struct kvm_vcpu * vcpu)109 void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
110 {
111 struct kvm_cpuid_entry2 *best;
112
113 best = kvm_find_cpuid_entry(vcpu, 1, 0);
114 if (best) {
115 /* Update OSXSAVE bit */
116 if (boot_cpu_has(X86_FEATURE_XSAVE))
117 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
118 kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE));
119
120 cpuid_entry_change(best, X86_FEATURE_APIC,
121 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
122 }
123
124 best = kvm_find_cpuid_entry(vcpu, 7, 0);
125 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
126 cpuid_entry_change(best, X86_FEATURE_OSPKE,
127 kvm_read_cr4_bits(vcpu, X86_CR4_PKE));
128
129 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
130 if (best)
131 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
132
133 best = kvm_find_cpuid_entry(vcpu, 0xD, 1);
134 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
135 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
136 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
137
138 best = kvm_find_cpuid_entry(vcpu, KVM_CPUID_FEATURES, 0);
139 if (kvm_hlt_in_guest(vcpu->kvm) && best &&
140 (best->eax & (1 << KVM_FEATURE_PV_UNHALT)))
141 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
142
143 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
144 best = kvm_find_cpuid_entry(vcpu, 0x1, 0);
145 if (best)
146 cpuid_entry_change(best, X86_FEATURE_MWAIT,
147 vcpu->arch.ia32_misc_enable_msr &
148 MSR_IA32_MISC_ENABLE_MWAIT);
149 }
150 }
151 EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
152
kvm_vcpu_after_set_cpuid(struct kvm_vcpu * vcpu)153 static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
154 {
155 struct kvm_lapic *apic = vcpu->arch.apic;
156 struct kvm_cpuid_entry2 *best;
157
158 best = kvm_find_cpuid_entry(vcpu, 1, 0);
159 if (best && apic) {
160 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
161 apic->lapic_timer.timer_mode_mask = 3 << 17;
162 else
163 apic->lapic_timer.timer_mode_mask = 1 << 17;
164
165 kvm_apic_set_version(vcpu);
166 }
167
168 best = kvm_find_cpuid_entry(vcpu, 0xD, 0);
169 if (!best)
170 vcpu->arch.guest_supported_xcr0 = 0;
171 else
172 vcpu->arch.guest_supported_xcr0 =
173 (best->eax | ((u64)best->edx << 32)) & supported_xcr0;
174
175 /*
176 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
177 * the supported XSAVE Feature Request Mask (XFRM), i.e. the enclave's
178 * requested XCR0 value. The enclave's XFRM must be a subset of XCRO
179 * at the time of EENTER, thus adjust the allowed XFRM by the guest's
180 * supported XCR0. Similar to XCR0 handling, FP and SSE are forced to
181 * '1' even on CPUs that don't support XSAVE.
182 */
183 best = kvm_find_cpuid_entry(vcpu, 0x12, 0x1);
184 if (best) {
185 best->ecx &= vcpu->arch.guest_supported_xcr0 & 0xffffffff;
186 best->edx &= vcpu->arch.guest_supported_xcr0 >> 32;
187 best->ecx |= XFEATURE_MASK_FPSSE;
188 }
189
190 kvm_update_pv_runtime(vcpu);
191
192 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
193 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
194
195 kvm_pmu_refresh(vcpu);
196 vcpu->arch.cr4_guest_rsvd_bits =
197 __cr4_reserved_bits(guest_cpuid_has, vcpu);
198
199 kvm_hv_set_cpuid(vcpu);
200
201 /* Invoke the vendor callback only after the above state is updated. */
202 static_call(kvm_x86_vcpu_after_set_cpuid)(vcpu);
203
204 /*
205 * Except for the MMU, which needs to do its thing any vendor specific
206 * adjustments to the reserved GPA bits.
207 */
208 kvm_mmu_after_set_cpuid(vcpu);
209 }
210
cpuid_query_maxphyaddr(struct kvm_vcpu * vcpu)211 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
212 {
213 struct kvm_cpuid_entry2 *best;
214
215 best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
216 if (!best || best->eax < 0x80000008)
217 goto not_found;
218 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
219 if (best)
220 return best->eax & 0xff;
221 not_found:
222 return 36;
223 }
224
225 /*
226 * This "raw" version returns the reserved GPA bits without any adjustments for
227 * encryption technologies that usurp bits. The raw mask should be used if and
228 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
229 */
kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu * vcpu)230 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
231 {
232 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
233 }
234
kvm_set_cpuid(struct kvm_vcpu * vcpu,struct kvm_cpuid_entry2 * e2,int nent)235 static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
236 int nent)
237 {
238 int r;
239
240 r = kvm_check_cpuid(e2, nent);
241 if (r)
242 return r;
243
244 kvfree(vcpu->arch.cpuid_entries);
245 vcpu->arch.cpuid_entries = e2;
246 vcpu->arch.cpuid_nent = nent;
247
248 kvm_update_cpuid_runtime(vcpu);
249 kvm_vcpu_after_set_cpuid(vcpu);
250
251 return 0;
252 }
253
254 /* when an old userspace process fills a new kernel module */
kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu * vcpu,struct kvm_cpuid * cpuid,struct kvm_cpuid_entry __user * entries)255 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
256 struct kvm_cpuid *cpuid,
257 struct kvm_cpuid_entry __user *entries)
258 {
259 int r, i;
260 struct kvm_cpuid_entry *e = NULL;
261 struct kvm_cpuid_entry2 *e2 = NULL;
262
263 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
264 return -E2BIG;
265
266 if (cpuid->nent) {
267 e = vmemdup_user(entries, array_size(sizeof(*e), cpuid->nent));
268 if (IS_ERR(e))
269 return PTR_ERR(e);
270
271 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
272 if (!e2) {
273 r = -ENOMEM;
274 goto out_free_cpuid;
275 }
276 }
277 for (i = 0; i < cpuid->nent; i++) {
278 e2[i].function = e[i].function;
279 e2[i].eax = e[i].eax;
280 e2[i].ebx = e[i].ebx;
281 e2[i].ecx = e[i].ecx;
282 e2[i].edx = e[i].edx;
283 e2[i].index = 0;
284 e2[i].flags = 0;
285 e2[i].padding[0] = 0;
286 e2[i].padding[1] = 0;
287 e2[i].padding[2] = 0;
288 }
289
290 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
291 if (r)
292 kvfree(e2);
293
294 out_free_cpuid:
295 kvfree(e);
296
297 return r;
298 }
299
kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu * vcpu,struct kvm_cpuid2 * cpuid,struct kvm_cpuid_entry2 __user * entries)300 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
301 struct kvm_cpuid2 *cpuid,
302 struct kvm_cpuid_entry2 __user *entries)
303 {
304 struct kvm_cpuid_entry2 *e2 = NULL;
305 int r;
306
307 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
308 return -E2BIG;
309
310 if (cpuid->nent) {
311 e2 = vmemdup_user(entries, array_size(sizeof(*e2), cpuid->nent));
312 if (IS_ERR(e2))
313 return PTR_ERR(e2);
314 }
315
316 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
317 if (r)
318 kvfree(e2);
319
320 return r;
321 }
322
kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu * vcpu,struct kvm_cpuid2 * cpuid,struct kvm_cpuid_entry2 __user * entries)323 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
324 struct kvm_cpuid2 *cpuid,
325 struct kvm_cpuid_entry2 __user *entries)
326 {
327 int r;
328
329 r = -E2BIG;
330 if (cpuid->nent < vcpu->arch.cpuid_nent)
331 goto out;
332 r = -EFAULT;
333 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
334 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
335 goto out;
336 return 0;
337
338 out:
339 cpuid->nent = vcpu->arch.cpuid_nent;
340 return r;
341 }
342
343 /* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
__kvm_cpu_cap_mask(unsigned int leaf)344 static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
345 {
346 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
347 struct kvm_cpuid_entry2 entry;
348
349 reverse_cpuid_check(leaf);
350
351 cpuid_count(cpuid.function, cpuid.index,
352 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
353
354 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
355 }
356
357 static __always_inline
kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf,u32 mask)358 void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask)
359 {
360 /* Use kvm_cpu_cap_mask for non-scattered leafs. */
361 BUILD_BUG_ON(leaf < NCAPINTS);
362
363 kvm_cpu_caps[leaf] = mask;
364
365 __kvm_cpu_cap_mask(leaf);
366 }
367
kvm_cpu_cap_mask(enum cpuid_leafs leaf,u32 mask)368 static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
369 {
370 /* Use kvm_cpu_cap_init_scattered for scattered leafs. */
371 BUILD_BUG_ON(leaf >= NCAPINTS);
372
373 kvm_cpu_caps[leaf] &= mask;
374
375 __kvm_cpu_cap_mask(leaf);
376 }
377
kvm_set_cpu_caps(void)378 void kvm_set_cpu_caps(void)
379 {
380 #ifdef CONFIG_X86_64
381 unsigned int f_gbpages = F(GBPAGES);
382 unsigned int f_lm = F(LM);
383 #else
384 unsigned int f_gbpages = 0;
385 unsigned int f_lm = 0;
386 #endif
387 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
388
389 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
390 sizeof(boot_cpu_data.x86_capability));
391
392 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
393 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
394
395 kvm_cpu_cap_mask(CPUID_1_ECX,
396 /*
397 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
398 * advertised to guests via CPUID!
399 */
400 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
401 0 /* DS-CPL, VMX, SMX, EST */ |
402 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
403 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
404 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
405 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
406 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
407 F(F16C) | F(RDRAND)
408 );
409 /* KVM emulates x2apic in software irrespective of host support. */
410 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
411
412 kvm_cpu_cap_mask(CPUID_1_EDX,
413 F(FPU) | F(VME) | F(DE) | F(PSE) |
414 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
415 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
416 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
417 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
418 0 /* Reserved, DS, ACPI */ | F(MMX) |
419 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
420 0 /* HTT, TM, Reserved, PBE */
421 );
422
423 kvm_cpu_cap_mask(CPUID_7_0_EBX,
424 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) |
425 F(FDP_EXCPTN_ONLY) | F(SMEP) | F(BMI2) | F(ERMS) | F(INVPCID) |
426 F(RTM) | F(ZERO_FCS_FDS) | 0 /*MPX*/ | F(AVX512F) |
427 F(AVX512DQ) | F(RDSEED) | F(ADX) | F(SMAP) | F(AVX512IFMA) |
428 F(CLFLUSHOPT) | F(CLWB) | 0 /*INTEL_PT*/ | F(AVX512PF) |
429 F(AVX512ER) | F(AVX512CD) | F(SHA_NI) | F(AVX512BW) |
430 F(AVX512VL));
431
432 kvm_cpu_cap_mask(CPUID_7_ECX,
433 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
434 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
435 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
436 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
437 F(SGX_LC) | F(BUS_LOCK_DETECT)
438 );
439 /* Set LA57 based on hardware capability. */
440 if (cpuid_ecx(7) & F(LA57))
441 kvm_cpu_cap_set(X86_FEATURE_LA57);
442
443 /*
444 * PKU not yet implemented for shadow paging and requires OSPKE
445 * to be set on the host. Clear it if that is not the case
446 */
447 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
448 kvm_cpu_cap_clear(X86_FEATURE_PKU);
449
450 kvm_cpu_cap_mask(CPUID_7_EDX,
451 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
452 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
453 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
454 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16)
455 );
456
457 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
458 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
459 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
460
461 if (boot_cpu_has(X86_FEATURE_IBPB) && boot_cpu_has(X86_FEATURE_IBRS))
462 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
463 if (boot_cpu_has(X86_FEATURE_STIBP))
464 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
465 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
466 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
467
468 kvm_cpu_cap_mask(CPUID_7_1_EAX,
469 F(AVX_VNNI) | F(AVX512_BF16)
470 );
471
472 kvm_cpu_cap_mask(CPUID_D_1_EAX,
473 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES)
474 );
475
476 kvm_cpu_cap_init_scattered(CPUID_12_EAX,
477 SF(SGX1) | SF(SGX2)
478 );
479
480 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
481 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
482 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
483 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
484 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
485 F(TOPOEXT) | F(PERFCTR_CORE)
486 );
487
488 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
489 F(FPU) | F(VME) | F(DE) | F(PSE) |
490 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
491 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
492 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
493 F(PAT) | F(PSE36) | 0 /* Reserved */ |
494 F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
495 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
496 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
497 );
498
499 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
500 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
501
502 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
503 F(CLZERO) | F(XSAVEERPTR) |
504 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
505 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON)
506 );
507
508 /*
509 * AMD has separate bits for each SPEC_CTRL bit.
510 * arch/x86/kernel/cpu/bugs.c is kind enough to
511 * record that in cpufeatures so use them.
512 */
513 if (boot_cpu_has(X86_FEATURE_IBPB))
514 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
515 if (boot_cpu_has(X86_FEATURE_IBRS))
516 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
517 if (boot_cpu_has(X86_FEATURE_STIBP))
518 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
519 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
520 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
521 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
522 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
523 /*
524 * The preference is to use SPEC CTRL MSR instead of the
525 * VIRT_SPEC MSR.
526 */
527 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
528 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
529 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
530
531 /*
532 * Hide all SVM features by default, SVM will set the cap bits for
533 * features it emulates and/or exposes for L1.
534 */
535 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
536
537 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
538 0 /* SME */ | F(SEV) | 0 /* VM_PAGE_FLUSH */ | F(SEV_ES) |
539 F(SME_COHERENT));
540
541 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
542 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
543 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
544 F(PMM) | F(PMM_EN)
545 );
546
547 if (cpu_feature_enabled(X86_FEATURE_SRSO_NO))
548 kvm_cpu_cap_set(X86_FEATURE_SRSO_NO);
549
550 /*
551 * Hide RDTSCP and RDPID if either feature is reported as supported but
552 * probing MSR_TSC_AUX failed. This is purely a sanity check and
553 * should never happen, but the guest will likely crash if RDTSCP or
554 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
555 * the past. For example, the sanity check may fire if this instance of
556 * KVM is running as L1 on top of an older, broken KVM.
557 */
558 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
559 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
560 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
561 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
562 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
563 }
564 }
565 EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
566
567 struct kvm_cpuid_array {
568 struct kvm_cpuid_entry2 *entries;
569 int maxnent;
570 int nent;
571 };
572
get_next_cpuid(struct kvm_cpuid_array * array)573 static struct kvm_cpuid_entry2 *get_next_cpuid(struct kvm_cpuid_array *array)
574 {
575 if (array->nent >= array->maxnent)
576 return NULL;
577
578 return &array->entries[array->nent++];
579 }
580
do_host_cpuid(struct kvm_cpuid_array * array,u32 function,u32 index)581 static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
582 u32 function, u32 index)
583 {
584 struct kvm_cpuid_entry2 *entry = get_next_cpuid(array);
585
586 if (!entry)
587 return NULL;
588
589 entry->function = function;
590 entry->index = index;
591 entry->flags = 0;
592
593 cpuid_count(entry->function, entry->index,
594 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
595
596 switch (function) {
597 case 4:
598 case 7:
599 case 0xb:
600 case 0xd:
601 case 0xf:
602 case 0x10:
603 case 0x12:
604 case 0x14:
605 case 0x17:
606 case 0x18:
607 case 0x1f:
608 case 0x8000001d:
609 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
610 break;
611 }
612
613 return entry;
614 }
615
__do_cpuid_func_emulated(struct kvm_cpuid_array * array,u32 func)616 static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
617 {
618 struct kvm_cpuid_entry2 *entry;
619
620 if (array->nent >= array->maxnent)
621 return -E2BIG;
622
623 entry = &array->entries[array->nent];
624 entry->function = func;
625 entry->index = 0;
626 entry->flags = 0;
627
628 switch (func) {
629 case 0:
630 entry->eax = 7;
631 ++array->nent;
632 break;
633 case 1:
634 entry->ecx = F(MOVBE);
635 ++array->nent;
636 break;
637 case 7:
638 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
639 entry->eax = 0;
640 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
641 entry->ecx = F(RDPID);
642 ++array->nent;
643 break;
644 default:
645 break;
646 }
647
648 return 0;
649 }
650
__do_cpuid_func(struct kvm_cpuid_array * array,u32 function)651 static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
652 {
653 struct kvm_cpuid_entry2 *entry;
654 int r, i, max_idx;
655
656 /* all calls to cpuid_count() should be made on the same cpu */
657 get_cpu();
658
659 r = -E2BIG;
660
661 entry = do_host_cpuid(array, function, 0);
662 if (!entry)
663 goto out;
664
665 switch (function) {
666 case 0:
667 /* Limited to the highest leaf implemented in KVM. */
668 entry->eax = min(entry->eax, 0x1fU);
669 break;
670 case 1:
671 cpuid_entry_override(entry, CPUID_1_EDX);
672 cpuid_entry_override(entry, CPUID_1_ECX);
673 break;
674 case 2:
675 /*
676 * On ancient CPUs, function 2 entries are STATEFUL. That is,
677 * CPUID(function=2, index=0) may return different results each
678 * time, with the least-significant byte in EAX enumerating the
679 * number of times software should do CPUID(2, 0).
680 *
681 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
682 * idiotic. Intel's SDM states that EAX & 0xff "will always
683 * return 01H. Software should ignore this value and not
684 * interpret it as an informational descriptor", while AMD's
685 * APM states that CPUID(2) is reserved.
686 *
687 * WARN if a frankenstein CPU that supports virtualization and
688 * a stateful CPUID.0x2 is encountered.
689 */
690 WARN_ON_ONCE((entry->eax & 0xff) > 1);
691 break;
692 /* functions 4 and 0x8000001d have additional index. */
693 case 4:
694 case 0x8000001d:
695 /*
696 * Read entries until the cache type in the previous entry is
697 * zero, i.e. indicates an invalid entry.
698 */
699 for (i = 1; entry->eax & 0x1f; ++i) {
700 entry = do_host_cpuid(array, function, i);
701 if (!entry)
702 goto out;
703 }
704 break;
705 case 6: /* Thermal management */
706 entry->eax = 0x4; /* allow ARAT */
707 entry->ebx = 0;
708 entry->ecx = 0;
709 entry->edx = 0;
710 break;
711 /* function 7 has additional index. */
712 case 7:
713 entry->eax = min(entry->eax, 1u);
714 cpuid_entry_override(entry, CPUID_7_0_EBX);
715 cpuid_entry_override(entry, CPUID_7_ECX);
716 cpuid_entry_override(entry, CPUID_7_EDX);
717
718 /* KVM only supports 0x7.0 and 0x7.1, capped above via min(). */
719 if (entry->eax == 1) {
720 entry = do_host_cpuid(array, function, 1);
721 if (!entry)
722 goto out;
723
724 cpuid_entry_override(entry, CPUID_7_1_EAX);
725 entry->ebx = 0;
726 entry->ecx = 0;
727 entry->edx = 0;
728 }
729 break;
730 case 0xa: { /* Architectural Performance Monitoring */
731 struct x86_pmu_capability cap;
732 union cpuid10_eax eax;
733 union cpuid10_edx edx;
734
735 if (!static_cpu_has(X86_FEATURE_ARCH_PERFMON)) {
736 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
737 break;
738 }
739
740 perf_get_x86_pmu_capability(&cap);
741
742 /*
743 * Only support guest architectural pmu on a host
744 * with architectural pmu.
745 */
746 if (!cap.version)
747 memset(&cap, 0, sizeof(cap));
748
749 eax.split.version_id = min(cap.version, 2);
750 eax.split.num_counters = cap.num_counters_gp;
751 eax.split.bit_width = cap.bit_width_gp;
752 eax.split.mask_length = cap.events_mask_len;
753
754 edx.split.num_counters_fixed = min(cap.num_counters_fixed, MAX_FIXED_COUNTERS);
755 edx.split.bit_width_fixed = cap.bit_width_fixed;
756 if (cap.version)
757 edx.split.anythread_deprecated = 1;
758 edx.split.reserved1 = 0;
759 edx.split.reserved2 = 0;
760
761 entry->eax = eax.full;
762 entry->ebx = cap.events_mask;
763 entry->ecx = 0;
764 entry->edx = edx.full;
765 break;
766 }
767 case 0x1f:
768 case 0xb:
769 /*
770 * No topology; a valid topology is indicated by the presence
771 * of subleaf 1.
772 */
773 entry->eax = entry->ebx = entry->ecx = 0;
774 break;
775 case 0xd:
776 entry->eax &= supported_xcr0;
777 entry->ebx = xstate_required_size(supported_xcr0, false);
778 entry->ecx = entry->ebx;
779 entry->edx &= supported_xcr0 >> 32;
780 if (!supported_xcr0)
781 break;
782
783 entry = do_host_cpuid(array, function, 1);
784 if (!entry)
785 goto out;
786
787 cpuid_entry_override(entry, CPUID_D_1_EAX);
788 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
789 entry->ebx = xstate_required_size(supported_xcr0 | supported_xss,
790 true);
791 else {
792 WARN_ON_ONCE(supported_xss != 0);
793 entry->ebx = 0;
794 }
795 entry->ecx &= supported_xss;
796 entry->edx &= supported_xss >> 32;
797
798 for (i = 2; i < 64; ++i) {
799 bool s_state;
800 if (supported_xcr0 & BIT_ULL(i))
801 s_state = false;
802 else if (supported_xss & BIT_ULL(i))
803 s_state = true;
804 else
805 continue;
806
807 entry = do_host_cpuid(array, function, i);
808 if (!entry)
809 goto out;
810
811 /*
812 * The supported check above should have filtered out
813 * invalid sub-leafs. Only valid sub-leafs should
814 * reach this point, and they should have a non-zero
815 * save state size. Furthermore, check whether the
816 * processor agrees with supported_xcr0/supported_xss
817 * on whether this is an XCR0- or IA32_XSS-managed area.
818 */
819 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
820 --array->nent;
821 continue;
822 }
823 entry->edx = 0;
824 }
825 break;
826 case 0x12:
827 /* Intel SGX */
828 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
829 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
830 break;
831 }
832
833 /*
834 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
835 * and max enclave sizes. The SGX sub-features and MISCSELECT
836 * are restricted by kernel and KVM capabilities (like most
837 * feature flags), while enclave size is unrestricted.
838 */
839 cpuid_entry_override(entry, CPUID_12_EAX);
840 entry->ebx &= SGX_MISC_EXINFO;
841
842 entry = do_host_cpuid(array, function, 1);
843 if (!entry)
844 goto out;
845
846 /*
847 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
848 * feature flags. Advertise all supported flags, including
849 * privileged attributes that require explicit opt-in from
850 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
851 * expected to derive it from supported XCR0.
852 */
853 entry->eax &= SGX_ATTR_DEBUG | SGX_ATTR_MODE64BIT |
854 SGX_ATTR_PROVISIONKEY | SGX_ATTR_EINITTOKENKEY |
855 SGX_ATTR_KSS;
856 entry->ebx &= 0;
857 break;
858 /* Intel PT */
859 case 0x14:
860 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
861 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
862 break;
863 }
864
865 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
866 if (!do_host_cpuid(array, function, i))
867 goto out;
868 }
869 break;
870 case KVM_CPUID_SIGNATURE: {
871 static const char signature[12] = "KVMKVMKVM\0\0";
872 const u32 *sigptr = (const u32 *)signature;
873 entry->eax = KVM_CPUID_FEATURES;
874 entry->ebx = sigptr[0];
875 entry->ecx = sigptr[1];
876 entry->edx = sigptr[2];
877 break;
878 }
879 case KVM_CPUID_FEATURES:
880 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
881 (1 << KVM_FEATURE_NOP_IO_DELAY) |
882 (1 << KVM_FEATURE_CLOCKSOURCE2) |
883 (1 << KVM_FEATURE_ASYNC_PF) |
884 (1 << KVM_FEATURE_PV_EOI) |
885 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
886 (1 << KVM_FEATURE_PV_UNHALT) |
887 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
888 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
889 (1 << KVM_FEATURE_PV_SEND_IPI) |
890 (1 << KVM_FEATURE_POLL_CONTROL) |
891 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
892 (1 << KVM_FEATURE_ASYNC_PF_INT);
893
894 if (sched_info_on())
895 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
896
897 entry->ebx = 0;
898 entry->ecx = 0;
899 entry->edx = 0;
900 break;
901 case 0x80000000:
902 entry->eax = min(entry->eax, 0x8000001f);
903 break;
904 case 0x80000001:
905 entry->ebx &= ~GENMASK(27, 16);
906 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
907 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
908 break;
909 case 0x80000006:
910 /* Drop reserved bits, pass host L2 cache and TLB info. */
911 entry->edx &= ~GENMASK(17, 16);
912 break;
913 case 0x80000007: /* Advanced power management */
914 /* invariant TSC is CPUID.80000007H:EDX[8] */
915 entry->edx &= (1 << 8);
916 /* mask against host */
917 entry->edx &= boot_cpu_data.x86_power;
918 entry->eax = entry->ebx = entry->ecx = 0;
919 break;
920 case 0x80000008: {
921 unsigned g_phys_as = (entry->eax >> 16) & 0xff;
922 unsigned virt_as = max((entry->eax >> 8) & 0xff, 48U);
923 unsigned phys_as = entry->eax & 0xff;
924
925 /*
926 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
927 * the guest operates in the same PA space as the host, i.e.
928 * reductions in MAXPHYADDR for memory encryption affect shadow
929 * paging, too.
930 *
931 * If TDP is enabled but an explicit guest MAXPHYADDR is not
932 * provided, use the raw bare metal MAXPHYADDR as reductions to
933 * the HPAs do not affect GPAs.
934 */
935 if (!tdp_enabled)
936 g_phys_as = boot_cpu_data.x86_phys_bits;
937 else if (!g_phys_as)
938 g_phys_as = phys_as;
939
940 entry->eax = g_phys_as | (virt_as << 8);
941 entry->ecx &= ~(GENMASK(31, 16) | GENMASK(11, 8));
942 entry->edx = 0;
943 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
944 break;
945 }
946 case 0x8000000A:
947 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
948 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
949 break;
950 }
951 entry->eax = 1; /* SVM revision 1 */
952 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
953 ASID emulation to nested SVM */
954 entry->ecx = 0; /* Reserved */
955 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
956 break;
957 case 0x80000019:
958 entry->ecx = entry->edx = 0;
959 break;
960 case 0x8000001a:
961 entry->eax &= GENMASK(2, 0);
962 entry->ebx = entry->ecx = entry->edx = 0;
963 break;
964 case 0x8000001e:
965 /* Do not return host topology information. */
966 entry->eax = entry->ebx = entry->ecx = 0;
967 entry->edx = 0; /* reserved */
968 break;
969 case 0x8000001F:
970 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
971 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
972 } else {
973 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
974 /* Clear NumVMPL since KVM does not support VMPL. */
975 entry->ebx &= ~GENMASK(31, 12);
976 /*
977 * Enumerate '0' for "PA bits reduction", the adjusted
978 * MAXPHYADDR is enumerated directly (see 0x80000008).
979 */
980 entry->ebx &= ~GENMASK(11, 6);
981 }
982 break;
983 /*Add support for Centaur's CPUID instruction*/
984 case 0xC0000000:
985 /*Just support up to 0xC0000004 now*/
986 entry->eax = min(entry->eax, 0xC0000004);
987 break;
988 case 0xC0000001:
989 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
990 break;
991 case 3: /* Processor serial number */
992 case 5: /* MONITOR/MWAIT */
993 case 0xC0000002:
994 case 0xC0000003:
995 case 0xC0000004:
996 default:
997 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
998 break;
999 }
1000
1001 r = 0;
1002
1003 out:
1004 put_cpu();
1005
1006 return r;
1007 }
1008
do_cpuid_func(struct kvm_cpuid_array * array,u32 func,unsigned int type)1009 static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1010 unsigned int type)
1011 {
1012 if (type == KVM_GET_EMULATED_CPUID)
1013 return __do_cpuid_func_emulated(array, func);
1014
1015 return __do_cpuid_func(array, func);
1016 }
1017
1018 #define CENTAUR_CPUID_SIGNATURE 0xC0000000
1019
get_cpuid_func(struct kvm_cpuid_array * array,u32 func,unsigned int type)1020 static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1021 unsigned int type)
1022 {
1023 u32 limit;
1024 int r;
1025
1026 if (func == CENTAUR_CPUID_SIGNATURE &&
1027 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1028 return 0;
1029
1030 r = do_cpuid_func(array, func, type);
1031 if (r)
1032 return r;
1033
1034 limit = array->entries[array->nent - 1].eax;
1035 for (func = func + 1; func <= limit; ++func) {
1036 r = do_cpuid_func(array, func, type);
1037 if (r)
1038 break;
1039 }
1040
1041 return r;
1042 }
1043
sanity_check_entries(struct kvm_cpuid_entry2 __user * entries,__u32 num_entries,unsigned int ioctl_type)1044 static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1045 __u32 num_entries, unsigned int ioctl_type)
1046 {
1047 int i;
1048 __u32 pad[3];
1049
1050 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1051 return false;
1052
1053 /*
1054 * We want to make sure that ->padding is being passed clean from
1055 * userspace in case we want to use it for something in the future.
1056 *
1057 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1058 * have to give ourselves satisfied only with the emulated side. /me
1059 * sheds a tear.
1060 */
1061 for (i = 0; i < num_entries; i++) {
1062 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1063 return true;
1064
1065 if (pad[0] || pad[1] || pad[2])
1066 return true;
1067 }
1068 return false;
1069 }
1070
kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 * cpuid,struct kvm_cpuid_entry2 __user * entries,unsigned int type)1071 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1072 struct kvm_cpuid_entry2 __user *entries,
1073 unsigned int type)
1074 {
1075 static const u32 funcs[] = {
1076 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
1077 };
1078
1079 struct kvm_cpuid_array array = {
1080 .nent = 0,
1081 };
1082 int r, i;
1083
1084 if (cpuid->nent < 1)
1085 return -E2BIG;
1086 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1087 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
1088
1089 if (sanity_check_entries(entries, cpuid->nent, type))
1090 return -EINVAL;
1091
1092 array.entries = vzalloc(array_size(sizeof(struct kvm_cpuid_entry2),
1093 cpuid->nent));
1094 if (!array.entries)
1095 return -ENOMEM;
1096
1097 array.maxnent = cpuid->nent;
1098
1099 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
1100 r = get_cpuid_func(&array, funcs[i], type);
1101 if (r)
1102 goto out_free;
1103 }
1104 cpuid->nent = array.nent;
1105
1106 if (copy_to_user(entries, array.entries,
1107 array.nent * sizeof(struct kvm_cpuid_entry2)))
1108 r = -EFAULT;
1109
1110 out_free:
1111 vfree(array.entries);
1112 return r;
1113 }
1114
kvm_find_cpuid_entry(struct kvm_vcpu * vcpu,u32 function,u32 index)1115 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1116 u32 function, u32 index)
1117 {
1118 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1119 function, index);
1120 }
1121 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1122
1123 /*
1124 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1125 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1126 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1127 * range. Centaur/VIA follows Intel semantics.
1128 *
1129 * A leaf is considered out-of-range if its function is higher than the maximum
1130 * supported leaf of its associated class or if its associated class does not
1131 * exist.
1132 *
1133 * There are three primary classes to be considered, with their respective
1134 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1135 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1136 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1137 *
1138 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1139 * - Hypervisor: 0x40000000 - 0x4fffffff
1140 * - Extended: 0x80000000 - 0xbfffffff
1141 * - Centaur: 0xc0000000 - 0xcfffffff
1142 *
1143 * The Hypervisor class is further subdivided into sub-classes that each act as
1144 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
1145 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1146 * CPUID sub-classes are:
1147 *
1148 * - HyperV: 0x40000000 - 0x400000ff
1149 * - KVM: 0x40000100 - 0x400001ff
1150 */
1151 static struct kvm_cpuid_entry2 *
get_out_of_range_cpuid_entry(struct kvm_vcpu * vcpu,u32 * fn_ptr,u32 index)1152 get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
1153 {
1154 struct kvm_cpuid_entry2 *basic, *class;
1155 u32 function = *fn_ptr;
1156
1157 basic = kvm_find_cpuid_entry(vcpu, 0, 0);
1158 if (!basic)
1159 return NULL;
1160
1161 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1162 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1163 return NULL;
1164
1165 if (function >= 0x40000000 && function <= 0x4fffffff)
1166 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00, 0);
1167 else if (function >= 0xc0000000)
1168 class = kvm_find_cpuid_entry(vcpu, 0xc0000000, 0);
1169 else
1170 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000, 0);
1171
1172 if (class && function <= class->eax)
1173 return NULL;
1174
1175 /*
1176 * Leaf specific adjustments are also applied when redirecting to the
1177 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1178 * entry for CPUID.0xb.index (see below), then the output value for EDX
1179 * needs to be pulled from CPUID.0xb.1.
1180 */
1181 *fn_ptr = basic->eax;
1182
1183 /*
1184 * The class does not exist or the requested function is out of range;
1185 * the effective CPUID entry is the max basic leaf. Note, the index of
1186 * the original requested leaf is observed!
1187 */
1188 return kvm_find_cpuid_entry(vcpu, basic->eax, index);
1189 }
1190
kvm_cpuid(struct kvm_vcpu * vcpu,u32 * eax,u32 * ebx,u32 * ecx,u32 * edx,bool exact_only)1191 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
1192 u32 *ecx, u32 *edx, bool exact_only)
1193 {
1194 u32 orig_function = *eax, function = *eax, index = *ecx;
1195 struct kvm_cpuid_entry2 *entry;
1196 bool exact, used_max_basic = false;
1197
1198 entry = kvm_find_cpuid_entry(vcpu, function, index);
1199 exact = !!entry;
1200
1201 if (!entry && !exact_only) {
1202 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
1203 used_max_basic = !!entry;
1204 }
1205
1206 if (entry) {
1207 *eax = entry->eax;
1208 *ebx = entry->ebx;
1209 *ecx = entry->ecx;
1210 *edx = entry->edx;
1211 if (function == 7 && index == 0) {
1212 u64 data;
1213 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1214 (data & TSX_CTRL_CPUID_CLEAR))
1215 *ebx &= ~(F(RTM) | F(HLE));
1216 }
1217 } else {
1218 *eax = *ebx = *ecx = *edx = 0;
1219 /*
1220 * When leaf 0BH or 1FH is defined, CL is pass-through
1221 * and EDX is always the x2APIC ID, even for undefined
1222 * subleaves. Index 1 will exist iff the leaf is
1223 * implemented, so we pass through CL iff leaf 1
1224 * exists. EDX can be copied from any existing index.
1225 */
1226 if (function == 0xb || function == 0x1f) {
1227 entry = kvm_find_cpuid_entry(vcpu, function, 1);
1228 if (entry) {
1229 *ecx = index & 0xff;
1230 *edx = entry->edx;
1231 }
1232 }
1233 }
1234 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1235 used_max_basic);
1236 return exact;
1237 }
1238 EXPORT_SYMBOL_GPL(kvm_cpuid);
1239
kvm_emulate_cpuid(struct kvm_vcpu * vcpu)1240 int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1241 {
1242 u32 eax, ebx, ecx, edx;
1243
1244 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1245 return 1;
1246
1247 eax = kvm_rax_read(vcpu);
1248 ecx = kvm_rcx_read(vcpu);
1249 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
1250 kvm_rax_write(vcpu, eax);
1251 kvm_rbx_write(vcpu, ebx);
1252 kvm_rcx_write(vcpu, ecx);
1253 kvm_rdx_write(vcpu, edx);
1254 return kvm_skip_emulated_instruction(vcpu);
1255 }
1256 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
1257