drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / arch / x86 / kvm / cpuid.c
blob097bdc022d0f476cb9e1f4d149f9a5a082f90898
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel-based Virtual Machine driver for Linux
4 * cpuid support routines
6 * derived from arch/x86/kvm/x86.c
8 * Copyright 2011 Red Hat, Inc. and/or its affiliates.
9 * Copyright IBM Corporation, 2008
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kvm_host.h>
14 #include "linux/lockdep.h"
15 #include <linux/export.h>
16 #include <linux/vmalloc.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/stat.h>
20 #include <asm/processor.h>
21 #include <asm/user.h>
22 #include <asm/fpu/xstate.h>
23 #include <asm/sgx.h>
24 #include <asm/cpuid.h>
25 #include "cpuid.h"
26 #include "lapic.h"
27 #include "mmu.h"
28 #include "trace.h"
29 #include "pmu.h"
30 #include "xen.h"
33 * Unlike "struct cpuinfo_x86.x86_capability", kvm_cpu_caps doesn't need to be
34 * aligned to sizeof(unsigned long) because it's not accessed via bitops.
36 u32 kvm_cpu_caps[NR_KVM_CPU_CAPS] __read_mostly;
37 EXPORT_SYMBOL_GPL(kvm_cpu_caps);
39 u32 xstate_required_size(u64 xstate_bv, bool compacted)
41 int feature_bit = 0;
42 u32 ret = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
44 xstate_bv &= XFEATURE_MASK_EXTEND;
45 while (xstate_bv) {
46 if (xstate_bv & 0x1) {
47 u32 eax, ebx, ecx, edx, offset;
48 cpuid_count(0xD, feature_bit, &eax, &ebx, &ecx, &edx);
49 /* ECX[1]: 64B alignment in compacted form */
50 if (compacted)
51 offset = (ecx & 0x2) ? ALIGN(ret, 64) : ret;
52 else
53 offset = ebx;
54 ret = max(ret, offset + eax);
57 xstate_bv >>= 1;
58 feature_bit++;
61 return ret;
64 #define F feature_bit
66 /* Scattered Flag - For features that are scattered by cpufeatures.h. */
67 #define SF(name) \
68 ({ \
69 BUILD_BUG_ON(X86_FEATURE_##name >= MAX_CPU_FEATURES); \
70 (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0); \
74 * Magic value used by KVM when querying userspace-provided CPUID entries and
75 * doesn't care about the CPIUD index because the index of the function in
76 * question is not significant. Note, this magic value must have at least one
77 * bit set in bits[63:32] and must be consumed as a u64 by cpuid_entry2_find()
78 * to avoid false positives when processing guest CPUID input.
80 #define KVM_CPUID_INDEX_NOT_SIGNIFICANT -1ull
82 static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
83 struct kvm_cpuid_entry2 *entries, int nent, u32 function, u64 index)
85 struct kvm_cpuid_entry2 *e;
86 int i;
89 * KVM has a semi-arbitrary rule that querying the guest's CPUID model
90 * with IRQs disabled is disallowed. The CPUID model can legitimately
91 * have over one hundred entries, i.e. the lookup is slow, and IRQs are
92 * typically disabled in KVM only when KVM is in a performance critical
93 * path, e.g. the core VM-Enter/VM-Exit run loop. Nothing will break
94 * if this rule is violated, this assertion is purely to flag potential
95 * performance issues. If this fires, consider moving the lookup out
96 * of the hotpath, e.g. by caching information during CPUID updates.
98 lockdep_assert_irqs_enabled();
100 for (i = 0; i < nent; i++) {
101 e = &entries[i];
103 if (e->function != function)
104 continue;
107 * If the index isn't significant, use the first entry with a
108 * matching function. It's userspace's responsibility to not
109 * provide "duplicate" entries in all cases.
111 if (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) || e->index == index)
112 return e;
116 * Similarly, use the first matching entry if KVM is doing a
117 * lookup (as opposed to emulating CPUID) for a function that's
118 * architecturally defined as not having a significant index.
120 if (index == KVM_CPUID_INDEX_NOT_SIGNIFICANT) {
122 * Direct lookups from KVM should not diverge from what
123 * KVM defines internally (the architectural behavior).
125 WARN_ON_ONCE(cpuid_function_is_indexed(function));
126 return e;
130 return NULL;
133 static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
134 struct kvm_cpuid_entry2 *entries,
135 int nent)
137 struct kvm_cpuid_entry2 *best;
138 u64 xfeatures;
141 * The existing code assumes virtual address is 48-bit or 57-bit in the
142 * canonical address checks; exit if it is ever changed.
144 best = cpuid_entry2_find(entries, nent, 0x80000008,
145 KVM_CPUID_INDEX_NOT_SIGNIFICANT);
146 if (best) {
147 int vaddr_bits = (best->eax & 0xff00) >> 8;
149 if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0)
150 return -EINVAL;
154 * Exposing dynamic xfeatures to the guest requires additional
155 * enabling in the FPU, e.g. to expand the guest XSAVE state size.
157 best = cpuid_entry2_find(entries, nent, 0xd, 0);
158 if (!best)
159 return 0;
161 xfeatures = best->eax | ((u64)best->edx << 32);
162 xfeatures &= XFEATURE_MASK_USER_DYNAMIC;
163 if (!xfeatures)
164 return 0;
166 return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures);
169 /* Check whether the supplied CPUID data is equal to what is already set for the vCPU. */
170 static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
171 int nent)
173 struct kvm_cpuid_entry2 *orig;
174 int i;
176 if (nent != vcpu->arch.cpuid_nent)
177 return -EINVAL;
179 for (i = 0; i < nent; i++) {
180 orig = &vcpu->arch.cpuid_entries[i];
181 if (e2[i].function != orig->function ||
182 e2[i].index != orig->index ||
183 e2[i].flags != orig->flags ||
184 e2[i].eax != orig->eax || e2[i].ebx != orig->ebx ||
185 e2[i].ecx != orig->ecx || e2[i].edx != orig->edx)
186 return -EINVAL;
189 return 0;
192 static struct kvm_hypervisor_cpuid __kvm_get_hypervisor_cpuid(struct kvm_cpuid_entry2 *entries,
193 int nent, const char *sig)
195 struct kvm_hypervisor_cpuid cpuid = {};
196 struct kvm_cpuid_entry2 *entry;
197 u32 base;
199 for_each_possible_hypervisor_cpuid_base(base) {
200 entry = cpuid_entry2_find(entries, nent, base, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
202 if (entry) {
203 u32 signature[3];
205 signature[0] = entry->ebx;
206 signature[1] = entry->ecx;
207 signature[2] = entry->edx;
209 if (!memcmp(signature, sig, sizeof(signature))) {
210 cpuid.base = base;
211 cpuid.limit = entry->eax;
212 break;
217 return cpuid;
220 static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcpu,
221 const char *sig)
223 return __kvm_get_hypervisor_cpuid(vcpu->arch.cpuid_entries,
224 vcpu->arch.cpuid_nent, sig);
227 static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_cpuid_entry2 *entries,
228 int nent, u32 kvm_cpuid_base)
230 return cpuid_entry2_find(entries, nent, kvm_cpuid_base | KVM_CPUID_FEATURES,
231 KVM_CPUID_INDEX_NOT_SIGNIFICANT);
234 static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu)
236 u32 base = vcpu->arch.kvm_cpuid.base;
238 if (!base)
239 return NULL;
241 return __kvm_find_kvm_cpuid_features(vcpu->arch.cpuid_entries,
242 vcpu->arch.cpuid_nent, base);
245 void kvm_update_pv_runtime(struct kvm_vcpu *vcpu)
247 struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu);
250 * save the feature bitmap to avoid cpuid lookup for every PV
251 * operation
253 if (best)
254 vcpu->arch.pv_cpuid.features = best->eax;
258 * Calculate guest's supported XCR0 taking into account guest CPUID data and
259 * KVM's supported XCR0 (comprised of host's XCR0 and KVM_SUPPORTED_XCR0).
261 static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent)
263 struct kvm_cpuid_entry2 *best;
265 best = cpuid_entry2_find(entries, nent, 0xd, 0);
266 if (!best)
267 return 0;
269 return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0;
272 static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries,
273 int nent)
275 struct kvm_cpuid_entry2 *best;
276 struct kvm_hypervisor_cpuid kvm_cpuid;
278 best = cpuid_entry2_find(entries, nent, 1, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
279 if (best) {
280 /* Update OSXSAVE bit */
281 if (boot_cpu_has(X86_FEATURE_XSAVE))
282 cpuid_entry_change(best, X86_FEATURE_OSXSAVE,
283 kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE));
285 cpuid_entry_change(best, X86_FEATURE_APIC,
286 vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE);
289 best = cpuid_entry2_find(entries, nent, 7, 0);
290 if (best && boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7)
291 cpuid_entry_change(best, X86_FEATURE_OSPKE,
292 kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE));
294 best = cpuid_entry2_find(entries, nent, 0xD, 0);
295 if (best)
296 best->ebx = xstate_required_size(vcpu->arch.xcr0, false);
298 best = cpuid_entry2_find(entries, nent, 0xD, 1);
299 if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) ||
300 cpuid_entry_has(best, X86_FEATURE_XSAVEC)))
301 best->ebx = xstate_required_size(vcpu->arch.xcr0, true);
303 kvm_cpuid = __kvm_get_hypervisor_cpuid(entries, nent, KVM_SIGNATURE);
304 if (kvm_cpuid.base) {
305 best = __kvm_find_kvm_cpuid_features(entries, nent, kvm_cpuid.base);
306 if (kvm_hlt_in_guest(vcpu->kvm) && best)
307 best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT);
310 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) {
311 best = cpuid_entry2_find(entries, nent, 0x1, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
312 if (best)
313 cpuid_entry_change(best, X86_FEATURE_MWAIT,
314 vcpu->arch.ia32_misc_enable_msr &
315 MSR_IA32_MISC_ENABLE_MWAIT);
319 void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
321 __kvm_update_cpuid_runtime(vcpu, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
323 EXPORT_SYMBOL_GPL(kvm_update_cpuid_runtime);
325 static bool kvm_cpuid_has_hyperv(struct kvm_cpuid_entry2 *entries, int nent)
327 #ifdef CONFIG_KVM_HYPERV
328 struct kvm_cpuid_entry2 *entry;
330 entry = cpuid_entry2_find(entries, nent, HYPERV_CPUID_INTERFACE,
331 KVM_CPUID_INDEX_NOT_SIGNIFICANT);
332 return entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX;
333 #else
334 return false;
335 #endif
338 static bool guest_cpuid_is_amd_or_hygon(struct kvm_vcpu *vcpu)
340 struct kvm_cpuid_entry2 *entry;
342 entry = kvm_find_cpuid_entry(vcpu, 0);
343 if (!entry)
344 return false;
346 return is_guest_vendor_amd(entry->ebx, entry->ecx, entry->edx) ||
347 is_guest_vendor_hygon(entry->ebx, entry->ecx, entry->edx);
350 static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
352 struct kvm_lapic *apic = vcpu->arch.apic;
353 struct kvm_cpuid_entry2 *best;
354 bool allow_gbpages;
356 BUILD_BUG_ON(KVM_NR_GOVERNED_FEATURES > KVM_MAX_NR_GOVERNED_FEATURES);
357 bitmap_zero(vcpu->arch.governed_features.enabled,
358 KVM_MAX_NR_GOVERNED_FEATURES);
361 * If TDP is enabled, let the guest use GBPAGES if they're supported in
362 * hardware. The hardware page walker doesn't let KVM disable GBPAGES,
363 * i.e. won't treat them as reserved, and KVM doesn't redo the GVA->GPA
364 * walk for performance and complexity reasons. Not to mention KVM
365 * _can't_ solve the problem because GVA->GPA walks aren't visible to
366 * KVM once a TDP translation is installed. Mimic hardware behavior so
367 * that KVM's is at least consistent, i.e. doesn't randomly inject #PF.
368 * If TDP is disabled, honor *only* guest CPUID as KVM has full control
369 * and can install smaller shadow pages if the host lacks 1GiB support.
371 allow_gbpages = tdp_enabled ? boot_cpu_has(X86_FEATURE_GBPAGES) :
372 guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES);
373 if (allow_gbpages)
374 kvm_governed_feature_set(vcpu, X86_FEATURE_GBPAGES);
376 best = kvm_find_cpuid_entry(vcpu, 1);
377 if (best && apic) {
378 if (cpuid_entry_has(best, X86_FEATURE_TSC_DEADLINE_TIMER))
379 apic->lapic_timer.timer_mode_mask = 3 << 17;
380 else
381 apic->lapic_timer.timer_mode_mask = 1 << 17;
383 kvm_apic_set_version(vcpu);
386 vcpu->arch.guest_supported_xcr0 =
387 cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent);
389 kvm_update_pv_runtime(vcpu);
391 vcpu->arch.is_amd_compatible = guest_cpuid_is_amd_or_hygon(vcpu);
392 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
393 vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu);
395 kvm_pmu_refresh(vcpu);
396 vcpu->arch.cr4_guest_rsvd_bits =
397 __cr4_reserved_bits(guest_cpuid_has, vcpu);
399 kvm_hv_set_cpuid(vcpu, kvm_cpuid_has_hyperv(vcpu->arch.cpuid_entries,
400 vcpu->arch.cpuid_nent));
402 /* Invoke the vendor callback only after the above state is updated. */
403 kvm_x86_call(vcpu_after_set_cpuid)(vcpu);
406 * Except for the MMU, which needs to do its thing any vendor specific
407 * adjustments to the reserved GPA bits.
409 kvm_mmu_after_set_cpuid(vcpu);
412 int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu)
414 struct kvm_cpuid_entry2 *best;
416 best = kvm_find_cpuid_entry(vcpu, 0x80000000);
417 if (!best || best->eax < 0x80000008)
418 goto not_found;
419 best = kvm_find_cpuid_entry(vcpu, 0x80000008);
420 if (best)
421 return best->eax & 0xff;
422 not_found:
423 return 36;
427 * This "raw" version returns the reserved GPA bits without any adjustments for
428 * encryption technologies that usurp bits. The raw mask should be used if and
429 * only if hardware does _not_ strip the usurped bits, e.g. in virtual MTRRs.
431 u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu)
433 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63);
436 static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
437 int nent)
439 int r;
441 __kvm_update_cpuid_runtime(vcpu, e2, nent);
444 * KVM does not correctly handle changing guest CPUID after KVM_RUN, as
445 * MAXPHYADDR, GBPAGES support, AMD reserved bit behavior, etc.. aren't
446 * tracked in kvm_mmu_page_role. As a result, KVM may miss guest page
447 * faults due to reusing SPs/SPTEs. In practice no sane VMM mucks with
448 * the core vCPU model on the fly. It would've been better to forbid any
449 * KVM_SET_CPUID{,2} calls after KVM_RUN altogether but unfortunately
450 * some VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do
451 * KVM_SET_CPUID{,2} again. To support this legacy behavior, check
452 * whether the supplied CPUID data is equal to what's already set.
454 if (kvm_vcpu_has_run(vcpu)) {
455 r = kvm_cpuid_check_equal(vcpu, e2, nent);
456 if (r)
457 return r;
459 kvfree(e2);
460 return 0;
463 #ifdef CONFIG_KVM_HYPERV
464 if (kvm_cpuid_has_hyperv(e2, nent)) {
465 r = kvm_hv_vcpu_init(vcpu);
466 if (r)
467 return r;
469 #endif
471 r = kvm_check_cpuid(vcpu, e2, nent);
472 if (r)
473 return r;
475 kvfree(vcpu->arch.cpuid_entries);
476 vcpu->arch.cpuid_entries = e2;
477 vcpu->arch.cpuid_nent = nent;
479 vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE);
480 #ifdef CONFIG_KVM_XEN
481 vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
482 #endif
483 kvm_vcpu_after_set_cpuid(vcpu);
485 return 0;
488 /* when an old userspace process fills a new kernel module */
489 int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
490 struct kvm_cpuid *cpuid,
491 struct kvm_cpuid_entry __user *entries)
493 int r, i;
494 struct kvm_cpuid_entry *e = NULL;
495 struct kvm_cpuid_entry2 *e2 = NULL;
497 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
498 return -E2BIG;
500 if (cpuid->nent) {
501 e = vmemdup_array_user(entries, cpuid->nent, sizeof(*e));
502 if (IS_ERR(e))
503 return PTR_ERR(e);
505 e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);
506 if (!e2) {
507 r = -ENOMEM;
508 goto out_free_cpuid;
511 for (i = 0; i < cpuid->nent; i++) {
512 e2[i].function = e[i].function;
513 e2[i].eax = e[i].eax;
514 e2[i].ebx = e[i].ebx;
515 e2[i].ecx = e[i].ecx;
516 e2[i].edx = e[i].edx;
517 e2[i].index = 0;
518 e2[i].flags = 0;
519 e2[i].padding[0] = 0;
520 e2[i].padding[1] = 0;
521 e2[i].padding[2] = 0;
524 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
525 if (r)
526 kvfree(e2);
528 out_free_cpuid:
529 kvfree(e);
531 return r;
534 int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
535 struct kvm_cpuid2 *cpuid,
536 struct kvm_cpuid_entry2 __user *entries)
538 struct kvm_cpuid_entry2 *e2 = NULL;
539 int r;
541 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
542 return -E2BIG;
544 if (cpuid->nent) {
545 e2 = vmemdup_array_user(entries, cpuid->nent, sizeof(*e2));
546 if (IS_ERR(e2))
547 return PTR_ERR(e2);
550 r = kvm_set_cpuid(vcpu, e2, cpuid->nent);
551 if (r)
552 kvfree(e2);
554 return r;
557 int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
558 struct kvm_cpuid2 *cpuid,
559 struct kvm_cpuid_entry2 __user *entries)
561 if (cpuid->nent < vcpu->arch.cpuid_nent)
562 return -E2BIG;
564 if (copy_to_user(entries, vcpu->arch.cpuid_entries,
565 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
566 return -EFAULT;
568 cpuid->nent = vcpu->arch.cpuid_nent;
569 return 0;
572 /* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */
573 static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf)
575 const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32);
576 struct kvm_cpuid_entry2 entry;
578 reverse_cpuid_check(leaf);
580 cpuid_count(cpuid.function, cpuid.index,
581 &entry.eax, &entry.ebx, &entry.ecx, &entry.edx);
583 kvm_cpu_caps[leaf] &= *__cpuid_entry_get_reg(&entry, cpuid.reg);
586 static __always_inline
587 void kvm_cpu_cap_init_kvm_defined(enum kvm_only_cpuid_leafs leaf, u32 mask)
589 /* Use kvm_cpu_cap_mask for leafs that aren't KVM-only. */
590 BUILD_BUG_ON(leaf < NCAPINTS);
592 kvm_cpu_caps[leaf] = mask;
594 __kvm_cpu_cap_mask(leaf);
597 static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask)
599 /* Use kvm_cpu_cap_init_kvm_defined for KVM-only leafs. */
600 BUILD_BUG_ON(leaf >= NCAPINTS);
602 kvm_cpu_caps[leaf] &= mask;
604 __kvm_cpu_cap_mask(leaf);
607 void kvm_set_cpu_caps(void)
609 #ifdef CONFIG_X86_64
610 unsigned int f_gbpages = F(GBPAGES);
611 unsigned int f_lm = F(LM);
612 unsigned int f_xfd = F(XFD);
613 #else
614 unsigned int f_gbpages = 0;
615 unsigned int f_lm = 0;
616 unsigned int f_xfd = 0;
617 #endif
618 memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps));
620 BUILD_BUG_ON(sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)) >
621 sizeof(boot_cpu_data.x86_capability));
623 memcpy(&kvm_cpu_caps, &boot_cpu_data.x86_capability,
624 sizeof(kvm_cpu_caps) - (NKVMCAPINTS * sizeof(*kvm_cpu_caps)));
626 kvm_cpu_cap_mask(CPUID_1_ECX,
628 * NOTE: MONITOR (and MWAIT) are emulated as NOP, but *not*
629 * advertised to guests via CPUID!
631 F(XMM3) | F(PCLMULQDQ) | 0 /* DTES64, MONITOR */ |
632 0 /* DS-CPL, VMX, SMX, EST */ |
633 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
634 F(FMA) | F(CX16) | 0 /* xTPR Update */ | F(PDCM) |
635 F(PCID) | 0 /* Reserved, DCA */ | F(XMM4_1) |
636 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
637 0 /* Reserved*/ | F(AES) | F(XSAVE) | 0 /* OSXSAVE */ | F(AVX) |
638 F(F16C) | F(RDRAND)
640 /* KVM emulates x2apic in software irrespective of host support. */
641 kvm_cpu_cap_set(X86_FEATURE_X2APIC);
643 kvm_cpu_cap_mask(CPUID_1_EDX,
644 F(FPU) | F(VME) | F(DE) | F(PSE) |
645 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
646 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
647 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
648 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLUSH) |
649 0 /* Reserved, DS, ACPI */ | F(MMX) |
650 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
651 0 /* HTT, TM, Reserved, PBE */
654 kvm_cpu_cap_mask(CPUID_7_0_EBX,
655 F(FSGSBASE) | F(SGX) | F(BMI1) | F(HLE) | F(AVX2) |
656 F(FDP_EXCPTN_ONLY) | F(SMEP) | F(BMI2) | F(ERMS) | F(INVPCID) |
657 F(RTM) | F(ZERO_FCS_FDS) | 0 /*MPX*/ | F(AVX512F) |
658 F(AVX512DQ) | F(RDSEED) | F(ADX) | F(SMAP) | F(AVX512IFMA) |
659 F(CLFLUSHOPT) | F(CLWB) | 0 /*INTEL_PT*/ | F(AVX512PF) |
660 F(AVX512ER) | F(AVX512CD) | F(SHA_NI) | F(AVX512BW) |
661 F(AVX512VL));
663 kvm_cpu_cap_mask(CPUID_7_ECX,
664 F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
665 F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
666 F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
667 F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
668 F(SGX_LC) | F(BUS_LOCK_DETECT)
670 /* Set LA57 based on hardware capability. */
671 if (cpuid_ecx(7) & F(LA57))
672 kvm_cpu_cap_set(X86_FEATURE_LA57);
675 * PKU not yet implemented for shadow paging and requires OSPKE
676 * to be set on the host. Clear it if that is not the case
678 if (!tdp_enabled || !boot_cpu_has(X86_FEATURE_OSPKE))
679 kvm_cpu_cap_clear(X86_FEATURE_PKU);
681 kvm_cpu_cap_mask(CPUID_7_EDX,
682 F(AVX512_4VNNIW) | F(AVX512_4FMAPS) | F(SPEC_CTRL) |
683 F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
684 F(MD_CLEAR) | F(AVX512_VP2INTERSECT) | F(FSRM) |
685 F(SERIALIZE) | F(TSXLDTRK) | F(AVX512_FP16) |
686 F(AMX_TILE) | F(AMX_INT8) | F(AMX_BF16) | F(FLUSH_L1D)
689 /* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
690 kvm_cpu_cap_set(X86_FEATURE_TSC_ADJUST);
691 kvm_cpu_cap_set(X86_FEATURE_ARCH_CAPABILITIES);
693 if (boot_cpu_has(X86_FEATURE_AMD_IBPB_RET) &&
694 boot_cpu_has(X86_FEATURE_AMD_IBPB) &&
695 boot_cpu_has(X86_FEATURE_AMD_IBRS))
696 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL);
697 if (boot_cpu_has(X86_FEATURE_STIBP))
698 kvm_cpu_cap_set(X86_FEATURE_INTEL_STIBP);
699 if (boot_cpu_has(X86_FEATURE_AMD_SSBD))
700 kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
702 kvm_cpu_cap_mask(CPUID_7_1_EAX,
703 F(SHA512) | F(SM3) | F(SM4) | F(AVX_VNNI) | F(AVX512_BF16) |
704 F(CMPCCXADD) | F(FZRM) | F(FSRS) | F(FSRC) | F(AMX_FP16) |
705 F(AVX_IFMA) | F(LAM)
708 kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX,
709 F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(AMX_COMPLEX) |
710 F(AVX_VNNI_INT16) | F(PREFETCHITI) | F(AVX10)
713 kvm_cpu_cap_init_kvm_defined(CPUID_7_2_EDX,
714 F(INTEL_PSFD) | F(IPRED_CTRL) | F(RRSBA_CTRL) | F(DDPD_U) |
715 F(BHI_CTRL) | F(MCDT_NO)
718 kvm_cpu_cap_mask(CPUID_D_1_EAX,
719 F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
722 kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX,
723 SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA)
726 kvm_cpu_cap_init_kvm_defined(CPUID_24_0_EBX,
727 F(AVX10_128) | F(AVX10_256) | F(AVX10_512)
730 kvm_cpu_cap_mask(CPUID_8000_0001_ECX,
731 F(LAHF_LM) | F(CMP_LEGACY) | 0 /*SVM*/ | 0 /* ExtApicSpace */ |
732 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
733 F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
734 0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
735 F(TOPOEXT) | 0 /* PERFCTR_CORE */
738 kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
739 F(FPU) | F(VME) | F(DE) | F(PSE) |
740 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
741 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
742 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
743 F(PAT) | F(PSE36) | 0 /* Reserved */ |
744 F(NX) | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
745 F(FXSR) | F(FXSR_OPT) | f_gbpages | F(RDTSCP) |
746 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW)
749 if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64))
750 kvm_cpu_cap_set(X86_FEATURE_GBPAGES);
752 kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX,
753 SF(CONSTANT_TSC)
756 kvm_cpu_cap_mask(CPUID_8000_0008_EBX,
757 F(CLZERO) | F(XSAVEERPTR) |
758 F(WBNOINVD) | F(AMD_IBPB) | F(AMD_IBRS) | F(AMD_SSBD) | F(VIRT_SSBD) |
759 F(AMD_SSB_NO) | F(AMD_STIBP) | F(AMD_STIBP_ALWAYS_ON) |
760 F(AMD_PSFD) | F(AMD_IBPB_RET)
764 * AMD has separate bits for each SPEC_CTRL bit.
765 * arch/x86/kernel/cpu/bugs.c is kind enough to
766 * record that in cpufeatures so use them.
768 if (boot_cpu_has(X86_FEATURE_IBPB)) {
769 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB);
770 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) &&
771 !boot_cpu_has_bug(X86_BUG_EIBRS_PBRSB))
772 kvm_cpu_cap_set(X86_FEATURE_AMD_IBPB_RET);
774 if (boot_cpu_has(X86_FEATURE_IBRS))
775 kvm_cpu_cap_set(X86_FEATURE_AMD_IBRS);
776 if (boot_cpu_has(X86_FEATURE_STIBP))
777 kvm_cpu_cap_set(X86_FEATURE_AMD_STIBP);
778 if (boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD))
779 kvm_cpu_cap_set(X86_FEATURE_AMD_SSBD);
780 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
781 kvm_cpu_cap_set(X86_FEATURE_AMD_SSB_NO);
783 * The preference is to use SPEC CTRL MSR instead of the
784 * VIRT_SPEC MSR.
786 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) &&
787 !boot_cpu_has(X86_FEATURE_AMD_SSBD))
788 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
791 * Hide all SVM features by default, SVM will set the cap bits for
792 * features it emulates and/or exposes for L1.
794 kvm_cpu_cap_mask(CPUID_8000_000A_EDX, 0);
796 kvm_cpu_cap_mask(CPUID_8000_001F_EAX,
797 0 /* SME */ | 0 /* SEV */ | 0 /* VM_PAGE_FLUSH */ | 0 /* SEV_ES */ |
798 F(SME_COHERENT));
800 kvm_cpu_cap_mask(CPUID_8000_0021_EAX,
801 F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ |
802 F(NULL_SEL_CLR_BASE) | F(AUTOIBRS) | 0 /* PrefetchCtlMsr */ |
803 F(WRMSR_XX_BASE_NS)
806 kvm_cpu_cap_check_and_set(X86_FEATURE_SBPB);
807 kvm_cpu_cap_check_and_set(X86_FEATURE_IBPB_BRTYPE);
808 kvm_cpu_cap_check_and_set(X86_FEATURE_SRSO_NO);
810 kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX,
811 F(PERFMON_V2)
815 * Synthesize "LFENCE is serializing" into the AMD-defined entry in
816 * KVM's supported CPUID if the feature is reported as supported by the
817 * kernel. LFENCE_RDTSC was a Linux-defined synthetic feature long
818 * before AMD joined the bandwagon, e.g. LFENCE is serializing on most
819 * CPUs that support SSE2. On CPUs that don't support AMD's leaf,
820 * kvm_cpu_cap_mask() will unfortunately drop the flag due to ANDing
821 * the mask with the raw host CPUID, and reporting support in AMD's
822 * leaf can make it easier for userspace to detect the feature.
824 if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC))
825 kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC);
826 if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
827 kvm_cpu_cap_set(X86_FEATURE_NULL_SEL_CLR_BASE);
828 kvm_cpu_cap_set(X86_FEATURE_NO_SMM_CTL_MSR);
830 kvm_cpu_cap_mask(CPUID_C000_0001_EDX,
831 F(XSTORE) | F(XSTORE_EN) | F(XCRYPT) | F(XCRYPT_EN) |
832 F(ACE2) | F(ACE2_EN) | F(PHE) | F(PHE_EN) |
833 F(PMM) | F(PMM_EN)
837 * Hide RDTSCP and RDPID if either feature is reported as supported but
838 * probing MSR_TSC_AUX failed. This is purely a sanity check and
839 * should never happen, but the guest will likely crash if RDTSCP or
840 * RDPID is misreported, and KVM has botched MSR_TSC_AUX emulation in
841 * the past. For example, the sanity check may fire if this instance of
842 * KVM is running as L1 on top of an older, broken KVM.
844 if (WARN_ON((kvm_cpu_cap_has(X86_FEATURE_RDTSCP) ||
845 kvm_cpu_cap_has(X86_FEATURE_RDPID)) &&
846 !kvm_is_supported_user_return_msr(MSR_TSC_AUX))) {
847 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
848 kvm_cpu_cap_clear(X86_FEATURE_RDPID);
851 EXPORT_SYMBOL_GPL(kvm_set_cpu_caps);
853 struct kvm_cpuid_array {
854 struct kvm_cpuid_entry2 *entries;
855 int maxnent;
856 int nent;
859 static struct kvm_cpuid_entry2 *get_next_cpuid(struct kvm_cpuid_array *array)
861 if (array->nent >= array->maxnent)
862 return NULL;
864 return &array->entries[array->nent++];
867 static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
868 u32 function, u32 index)
870 struct kvm_cpuid_entry2 *entry = get_next_cpuid(array);
872 if (!entry)
873 return NULL;
875 memset(entry, 0, sizeof(*entry));
876 entry->function = function;
877 entry->index = index;
878 switch (function & 0xC0000000) {
879 case 0x40000000:
880 /* Hypervisor leaves are always synthesized by __do_cpuid_func. */
881 return entry;
883 case 0x80000000:
885 * 0x80000021 is sometimes synthesized by __do_cpuid_func, which
886 * would result in out-of-bounds calls to do_host_cpuid.
889 static int max_cpuid_80000000;
890 if (!READ_ONCE(max_cpuid_80000000))
891 WRITE_ONCE(max_cpuid_80000000, cpuid_eax(0x80000000));
892 if (function > READ_ONCE(max_cpuid_80000000))
893 return entry;
895 break;
897 default:
898 break;
901 cpuid_count(entry->function, entry->index,
902 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
904 if (cpuid_function_is_indexed(function))
905 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
907 return entry;
910 static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
912 struct kvm_cpuid_entry2 *entry;
914 if (array->nent >= array->maxnent)
915 return -E2BIG;
917 entry = &array->entries[array->nent];
918 entry->function = func;
919 entry->index = 0;
920 entry->flags = 0;
922 switch (func) {
923 case 0:
924 entry->eax = 7;
925 ++array->nent;
926 break;
927 case 1:
928 entry->ecx = F(MOVBE);
929 ++array->nent;
930 break;
931 case 7:
932 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
933 entry->eax = 0;
934 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP))
935 entry->ecx = F(RDPID);
936 ++array->nent;
937 break;
938 default:
939 break;
942 return 0;
945 static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
947 struct kvm_cpuid_entry2 *entry;
948 int r, i, max_idx;
950 /* all calls to cpuid_count() should be made on the same cpu */
951 get_cpu();
953 r = -E2BIG;
955 entry = do_host_cpuid(array, function, 0);
956 if (!entry)
957 goto out;
959 switch (function) {
960 case 0:
961 /* Limited to the highest leaf implemented in KVM. */
962 entry->eax = min(entry->eax, 0x24U);
963 break;
964 case 1:
965 cpuid_entry_override(entry, CPUID_1_EDX);
966 cpuid_entry_override(entry, CPUID_1_ECX);
967 break;
968 case 2:
970 * On ancient CPUs, function 2 entries are STATEFUL. That is,
971 * CPUID(function=2, index=0) may return different results each
972 * time, with the least-significant byte in EAX enumerating the
973 * number of times software should do CPUID(2, 0).
975 * Modern CPUs, i.e. every CPU KVM has *ever* run on are less
976 * idiotic. Intel's SDM states that EAX & 0xff "will always
977 * return 01H. Software should ignore this value and not
978 * interpret it as an informational descriptor", while AMD's
979 * APM states that CPUID(2) is reserved.
981 * WARN if a frankenstein CPU that supports virtualization and
982 * a stateful CPUID.0x2 is encountered.
984 WARN_ON_ONCE((entry->eax & 0xff) > 1);
985 break;
986 /* functions 4 and 0x8000001d have additional index. */
987 case 4:
988 case 0x8000001d:
990 * Read entries until the cache type in the previous entry is
991 * zero, i.e. indicates an invalid entry.
993 for (i = 1; entry->eax & 0x1f; ++i) {
994 entry = do_host_cpuid(array, function, i);
995 if (!entry)
996 goto out;
998 break;
999 case 6: /* Thermal management */
1000 entry->eax = 0x4; /* allow ARAT */
1001 entry->ebx = 0;
1002 entry->ecx = 0;
1003 entry->edx = 0;
1004 break;
1005 /* function 7 has additional index. */
1006 case 7:
1007 max_idx = entry->eax = min(entry->eax, 2u);
1008 cpuid_entry_override(entry, CPUID_7_0_EBX);
1009 cpuid_entry_override(entry, CPUID_7_ECX);
1010 cpuid_entry_override(entry, CPUID_7_EDX);
1012 /* KVM only supports up to 0x7.2, capped above via min(). */
1013 if (max_idx >= 1) {
1014 entry = do_host_cpuid(array, function, 1);
1015 if (!entry)
1016 goto out;
1018 cpuid_entry_override(entry, CPUID_7_1_EAX);
1019 cpuid_entry_override(entry, CPUID_7_1_EDX);
1020 entry->ebx = 0;
1021 entry->ecx = 0;
1023 if (max_idx >= 2) {
1024 entry = do_host_cpuid(array, function, 2);
1025 if (!entry)
1026 goto out;
1028 cpuid_entry_override(entry, CPUID_7_2_EDX);
1029 entry->ecx = 0;
1030 entry->ebx = 0;
1031 entry->eax = 0;
1033 break;
1034 case 0xa: { /* Architectural Performance Monitoring */
1035 union cpuid10_eax eax;
1036 union cpuid10_edx edx;
1038 if (!enable_pmu || !static_cpu_has(X86_FEATURE_ARCH_PERFMON)) {
1039 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1040 break;
1043 eax.split.version_id = kvm_pmu_cap.version;
1044 eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
1045 eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
1046 eax.split.mask_length = kvm_pmu_cap.events_mask_len;
1047 edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
1048 edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
1050 if (kvm_pmu_cap.version)
1051 edx.split.anythread_deprecated = 1;
1052 edx.split.reserved1 = 0;
1053 edx.split.reserved2 = 0;
1055 entry->eax = eax.full;
1056 entry->ebx = kvm_pmu_cap.events_mask;
1057 entry->ecx = 0;
1058 entry->edx = edx.full;
1059 break;
1061 case 0x1f:
1062 case 0xb:
1064 * No topology; a valid topology is indicated by the presence
1065 * of subleaf 1.
1067 entry->eax = entry->ebx = entry->ecx = 0;
1068 break;
1069 case 0xd: {
1070 u64 permitted_xcr0 = kvm_get_filtered_xcr0();
1071 u64 permitted_xss = kvm_caps.supported_xss;
1073 entry->eax &= permitted_xcr0;
1074 entry->ebx = xstate_required_size(permitted_xcr0, false);
1075 entry->ecx = entry->ebx;
1076 entry->edx &= permitted_xcr0 >> 32;
1077 if (!permitted_xcr0)
1078 break;
1080 entry = do_host_cpuid(array, function, 1);
1081 if (!entry)
1082 goto out;
1084 cpuid_entry_override(entry, CPUID_D_1_EAX);
1085 if (entry->eax & (F(XSAVES)|F(XSAVEC)))
1086 entry->ebx = xstate_required_size(permitted_xcr0 | permitted_xss,
1087 true);
1088 else {
1089 WARN_ON_ONCE(permitted_xss != 0);
1090 entry->ebx = 0;
1092 entry->ecx &= permitted_xss;
1093 entry->edx &= permitted_xss >> 32;
1095 for (i = 2; i < 64; ++i) {
1096 bool s_state;
1097 if (permitted_xcr0 & BIT_ULL(i))
1098 s_state = false;
1099 else if (permitted_xss & BIT_ULL(i))
1100 s_state = true;
1101 else
1102 continue;
1104 entry = do_host_cpuid(array, function, i);
1105 if (!entry)
1106 goto out;
1109 * The supported check above should have filtered out
1110 * invalid sub-leafs. Only valid sub-leafs should
1111 * reach this point, and they should have a non-zero
1112 * save state size. Furthermore, check whether the
1113 * processor agrees with permitted_xcr0/permitted_xss
1114 * on whether this is an XCR0- or IA32_XSS-managed area.
1116 if (WARN_ON_ONCE(!entry->eax || (entry->ecx & 0x1) != s_state)) {
1117 --array->nent;
1118 continue;
1121 if (!kvm_cpu_cap_has(X86_FEATURE_XFD))
1122 entry->ecx &= ~BIT_ULL(2);
1123 entry->edx = 0;
1125 break;
1127 case 0x12:
1128 /* Intel SGX */
1129 if (!kvm_cpu_cap_has(X86_FEATURE_SGX)) {
1130 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1131 break;
1135 * Index 0: Sub-features, MISCSELECT (a.k.a extended features)
1136 * and max enclave sizes. The SGX sub-features and MISCSELECT
1137 * are restricted by kernel and KVM capabilities (like most
1138 * feature flags), while enclave size is unrestricted.
1140 cpuid_entry_override(entry, CPUID_12_EAX);
1141 entry->ebx &= SGX_MISC_EXINFO;
1143 entry = do_host_cpuid(array, function, 1);
1144 if (!entry)
1145 goto out;
1148 * Index 1: SECS.ATTRIBUTES. ATTRIBUTES are restricted a la
1149 * feature flags. Advertise all supported flags, including
1150 * privileged attributes that require explicit opt-in from
1151 * userspace. ATTRIBUTES.XFRM is not adjusted as userspace is
1152 * expected to derive it from supported XCR0.
1154 entry->eax &= SGX_ATTR_PRIV_MASK | SGX_ATTR_UNPRIV_MASK;
1155 entry->ebx &= 0;
1156 break;
1157 /* Intel PT */
1158 case 0x14:
1159 if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) {
1160 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1161 break;
1164 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
1165 if (!do_host_cpuid(array, function, i))
1166 goto out;
1168 break;
1169 /* Intel AMX TILE */
1170 case 0x1d:
1171 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
1172 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1173 break;
1176 for (i = 1, max_idx = entry->eax; i <= max_idx; ++i) {
1177 if (!do_host_cpuid(array, function, i))
1178 goto out;
1180 break;
1181 case 0x1e: /* TMUL information */
1182 if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
1183 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1184 break;
1186 break;
1187 case 0x24: {
1188 u8 avx10_version;
1190 if (!kvm_cpu_cap_has(X86_FEATURE_AVX10)) {
1191 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1192 break;
1196 * The AVX10 version is encoded in EBX[7:0]. Note, the version
1197 * is guaranteed to be >=1 if AVX10 is supported. Note #2, the
1198 * version needs to be captured before overriding EBX features!
1200 avx10_version = min_t(u8, entry->ebx & 0xff, 1);
1201 cpuid_entry_override(entry, CPUID_24_0_EBX);
1202 entry->ebx |= avx10_version;
1204 entry->eax = 0;
1205 entry->ecx = 0;
1206 entry->edx = 0;
1207 break;
1209 case KVM_CPUID_SIGNATURE: {
1210 const u32 *sigptr = (const u32 *)KVM_SIGNATURE;
1211 entry->eax = KVM_CPUID_FEATURES;
1212 entry->ebx = sigptr[0];
1213 entry->ecx = sigptr[1];
1214 entry->edx = sigptr[2];
1215 break;
1217 case KVM_CPUID_FEATURES:
1218 entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
1219 (1 << KVM_FEATURE_NOP_IO_DELAY) |
1220 (1 << KVM_FEATURE_CLOCKSOURCE2) |
1221 (1 << KVM_FEATURE_ASYNC_PF) |
1222 (1 << KVM_FEATURE_PV_EOI) |
1223 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
1224 (1 << KVM_FEATURE_PV_UNHALT) |
1225 (1 << KVM_FEATURE_PV_TLB_FLUSH) |
1226 (1 << KVM_FEATURE_ASYNC_PF_VMEXIT) |
1227 (1 << KVM_FEATURE_PV_SEND_IPI) |
1228 (1 << KVM_FEATURE_POLL_CONTROL) |
1229 (1 << KVM_FEATURE_PV_SCHED_YIELD) |
1230 (1 << KVM_FEATURE_ASYNC_PF_INT);
1232 if (sched_info_on())
1233 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
1235 entry->ebx = 0;
1236 entry->ecx = 0;
1237 entry->edx = 0;
1238 break;
1239 case 0x80000000:
1240 entry->eax = min(entry->eax, 0x80000022);
1242 * Serializing LFENCE is reported in a multitude of ways, and
1243 * NullSegClearsBase is not reported in CPUID on Zen2; help
1244 * userspace by providing the CPUID leaf ourselves.
1246 * However, only do it if the host has CPUID leaf 0x8000001d.
1247 * QEMU thinks that it can query the host blindly for that
1248 * CPUID leaf if KVM reports that it supports 0x8000001d or
1249 * above. The processor merrily returns values from the
1250 * highest Intel leaf which QEMU tries to use as the guest's
1251 * 0x8000001d. Even worse, this can result in an infinite
1252 * loop if said highest leaf has no subleaves indexed by ECX.
1254 if (entry->eax >= 0x8000001d &&
1255 (static_cpu_has(X86_FEATURE_LFENCE_RDTSC)
1256 || !static_cpu_has_bug(X86_BUG_NULL_SEG)))
1257 entry->eax = max(entry->eax, 0x80000021);
1258 break;
1259 case 0x80000001:
1260 entry->ebx &= ~GENMASK(27, 16);
1261 cpuid_entry_override(entry, CPUID_8000_0001_EDX);
1262 cpuid_entry_override(entry, CPUID_8000_0001_ECX);
1263 break;
1264 case 0x80000005:
1265 /* Pass host L1 cache and TLB info. */
1266 break;
1267 case 0x80000006:
1268 /* Drop reserved bits, pass host L2 cache and TLB info. */
1269 entry->edx &= ~GENMASK(17, 16);
1270 break;
1271 case 0x80000007: /* Advanced power management */
1272 cpuid_entry_override(entry, CPUID_8000_0007_EDX);
1274 /* mask against host */
1275 entry->edx &= boot_cpu_data.x86_power;
1276 entry->eax = entry->ebx = entry->ecx = 0;
1277 break;
1278 case 0x80000008: {
1280 * GuestPhysAddrSize (EAX[23:16]) is intended for software
1281 * use.
1283 * KVM's ABI is to report the effective MAXPHYADDR for the
1284 * guest in PhysAddrSize (phys_as), and the maximum
1285 * *addressable* GPA in GuestPhysAddrSize (g_phys_as).
1287 * GuestPhysAddrSize is valid if and only if TDP is enabled,
1288 * in which case the max GPA that can be addressed by KVM may
1289 * be less than the max GPA that can be legally generated by
1290 * the guest, e.g. if MAXPHYADDR>48 but the CPU doesn't
1291 * support 5-level TDP.
1293 unsigned int virt_as = max((entry->eax >> 8) & 0xff, 48U);
1294 unsigned int phys_as, g_phys_as;
1297 * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as
1298 * the guest operates in the same PA space as the host, i.e.
1299 * reductions in MAXPHYADDR for memory encryption affect shadow
1300 * paging, too.
1302 * If TDP is enabled, use the raw bare metal MAXPHYADDR as
1303 * reductions to the HPAs do not affect GPAs. The max
1304 * addressable GPA is the same as the max effective GPA, except
1305 * that it's capped at 48 bits if 5-level TDP isn't supported
1306 * (hardware processes bits 51:48 only when walking the fifth
1307 * level page table).
1309 if (!tdp_enabled) {
1310 phys_as = boot_cpu_data.x86_phys_bits;
1311 g_phys_as = 0;
1312 } else {
1313 phys_as = entry->eax & 0xff;
1314 g_phys_as = phys_as;
1315 if (kvm_mmu_get_max_tdp_level() < 5)
1316 g_phys_as = min(g_phys_as, 48);
1319 entry->eax = phys_as | (virt_as << 8) | (g_phys_as << 16);
1320 entry->ecx &= ~(GENMASK(31, 16) | GENMASK(11, 8));
1321 entry->edx = 0;
1322 cpuid_entry_override(entry, CPUID_8000_0008_EBX);
1323 break;
1325 case 0x8000000A:
1326 if (!kvm_cpu_cap_has(X86_FEATURE_SVM)) {
1327 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1328 break;
1330 entry->eax = 1; /* SVM revision 1 */
1331 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
1332 ASID emulation to nested SVM */
1333 entry->ecx = 0; /* Reserved */
1334 cpuid_entry_override(entry, CPUID_8000_000A_EDX);
1335 break;
1336 case 0x80000019:
1337 entry->ecx = entry->edx = 0;
1338 break;
1339 case 0x8000001a:
1340 entry->eax &= GENMASK(2, 0);
1341 entry->ebx = entry->ecx = entry->edx = 0;
1342 break;
1343 case 0x8000001e:
1344 /* Do not return host topology information. */
1345 entry->eax = entry->ebx = entry->ecx = 0;
1346 entry->edx = 0; /* reserved */
1347 break;
1348 case 0x8000001F:
1349 if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) {
1350 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1351 } else {
1352 cpuid_entry_override(entry, CPUID_8000_001F_EAX);
1353 /* Clear NumVMPL since KVM does not support VMPL. */
1354 entry->ebx &= ~GENMASK(31, 12);
1356 * Enumerate '0' for "PA bits reduction", the adjusted
1357 * MAXPHYADDR is enumerated directly (see 0x80000008).
1359 entry->ebx &= ~GENMASK(11, 6);
1361 break;
1362 case 0x80000020:
1363 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1364 break;
1365 case 0x80000021:
1366 entry->ebx = entry->ecx = entry->edx = 0;
1367 cpuid_entry_override(entry, CPUID_8000_0021_EAX);
1368 break;
1369 /* AMD Extended Performance Monitoring and Debug */
1370 case 0x80000022: {
1371 union cpuid_0x80000022_ebx ebx;
1373 entry->ecx = entry->edx = 0;
1374 if (!enable_pmu || !kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2)) {
1375 entry->eax = entry->ebx;
1376 break;
1379 cpuid_entry_override(entry, CPUID_8000_0022_EAX);
1381 if (kvm_cpu_cap_has(X86_FEATURE_PERFMON_V2))
1382 ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
1383 else if (kvm_cpu_cap_has(X86_FEATURE_PERFCTR_CORE))
1384 ebx.split.num_core_pmc = AMD64_NUM_COUNTERS_CORE;
1385 else
1386 ebx.split.num_core_pmc = AMD64_NUM_COUNTERS;
1388 entry->ebx = ebx.full;
1389 break;
1391 /*Add support for Centaur's CPUID instruction*/
1392 case 0xC0000000:
1393 /*Just support up to 0xC0000004 now*/
1394 entry->eax = min(entry->eax, 0xC0000004);
1395 break;
1396 case 0xC0000001:
1397 cpuid_entry_override(entry, CPUID_C000_0001_EDX);
1398 break;
1399 case 3: /* Processor serial number */
1400 case 5: /* MONITOR/MWAIT */
1401 case 0xC0000002:
1402 case 0xC0000003:
1403 case 0xC0000004:
1404 default:
1405 entry->eax = entry->ebx = entry->ecx = entry->edx = 0;
1406 break;
1409 r = 0;
1411 out:
1412 put_cpu();
1414 return r;
1417 static int do_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1418 unsigned int type)
1420 if (type == KVM_GET_EMULATED_CPUID)
1421 return __do_cpuid_func_emulated(array, func);
1423 return __do_cpuid_func(array, func);
1426 #define CENTAUR_CPUID_SIGNATURE 0xC0000000
1428 static int get_cpuid_func(struct kvm_cpuid_array *array, u32 func,
1429 unsigned int type)
1431 u32 limit;
1432 int r;
1434 if (func == CENTAUR_CPUID_SIGNATURE &&
1435 boot_cpu_data.x86_vendor != X86_VENDOR_CENTAUR)
1436 return 0;
1438 r = do_cpuid_func(array, func, type);
1439 if (r)
1440 return r;
1442 limit = array->entries[array->nent - 1].eax;
1443 for (func = func + 1; func <= limit; ++func) {
1444 r = do_cpuid_func(array, func, type);
1445 if (r)
1446 break;
1449 return r;
1452 static bool sanity_check_entries(struct kvm_cpuid_entry2 __user *entries,
1453 __u32 num_entries, unsigned int ioctl_type)
1455 int i;
1456 __u32 pad[3];
1458 if (ioctl_type != KVM_GET_EMULATED_CPUID)
1459 return false;
1462 * We want to make sure that ->padding is being passed clean from
1463 * userspace in case we want to use it for something in the future.
1465 * Sadly, this wasn't enforced for KVM_GET_SUPPORTED_CPUID and so we
1466 * have to give ourselves satisfied only with the emulated side. /me
1467 * sheds a tear.
1469 for (i = 0; i < num_entries; i++) {
1470 if (copy_from_user(pad, entries[i].padding, sizeof(pad)))
1471 return true;
1473 if (pad[0] || pad[1] || pad[2])
1474 return true;
1476 return false;
1479 int kvm_dev_ioctl_get_cpuid(struct kvm_cpuid2 *cpuid,
1480 struct kvm_cpuid_entry2 __user *entries,
1481 unsigned int type)
1483 static const u32 funcs[] = {
1484 0, 0x80000000, CENTAUR_CPUID_SIGNATURE, KVM_CPUID_SIGNATURE,
1487 struct kvm_cpuid_array array = {
1488 .nent = 0,
1490 int r, i;
1492 if (cpuid->nent < 1)
1493 return -E2BIG;
1494 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1495 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
1497 if (sanity_check_entries(entries, cpuid->nent, type))
1498 return -EINVAL;
1500 array.entries = kvcalloc(cpuid->nent, sizeof(struct kvm_cpuid_entry2), GFP_KERNEL);
1501 if (!array.entries)
1502 return -ENOMEM;
1504 array.maxnent = cpuid->nent;
1506 for (i = 0; i < ARRAY_SIZE(funcs); i++) {
1507 r = get_cpuid_func(&array, funcs[i], type);
1508 if (r)
1509 goto out_free;
1511 cpuid->nent = array.nent;
1513 if (copy_to_user(entries, array.entries,
1514 array.nent * sizeof(struct kvm_cpuid_entry2)))
1515 r = -EFAULT;
1517 out_free:
1518 kvfree(array.entries);
1519 return r;
1522 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry_index(struct kvm_vcpu *vcpu,
1523 u32 function, u32 index)
1525 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1526 function, index);
1528 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry_index);
1530 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
1531 u32 function)
1533 return cpuid_entry2_find(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent,
1534 function, KVM_CPUID_INDEX_NOT_SIGNIFICANT);
1536 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
1539 * Intel CPUID semantics treats any query for an out-of-range leaf as if the
1540 * highest basic leaf (i.e. CPUID.0H:EAX) were requested. AMD CPUID semantics
1541 * returns all zeroes for any undefined leaf, whether or not the leaf is in
1542 * range. Centaur/VIA follows Intel semantics.
1544 * A leaf is considered out-of-range if its function is higher than the maximum
1545 * supported leaf of its associated class or if its associated class does not
1546 * exist.
1548 * There are three primary classes to be considered, with their respective
1549 * ranges described as "<base> - <top>[,<base2> - <top2>] inclusive. A primary
1550 * class exists if a guest CPUID entry for its <base> leaf exists. For a given
1551 * class, CPUID.<base>.EAX contains the max supported leaf for the class.
1553 * - Basic: 0x00000000 - 0x3fffffff, 0x50000000 - 0x7fffffff
1554 * - Hypervisor: 0x40000000 - 0x4fffffff
1555 * - Extended: 0x80000000 - 0xbfffffff
1556 * - Centaur: 0xc0000000 - 0xcfffffff
1558 * The Hypervisor class is further subdivided into sub-classes that each act as
1559 * their own independent class associated with a 0x100 byte range. E.g. if Qemu
1560 * is advertising support for both HyperV and KVM, the resulting Hypervisor
1561 * CPUID sub-classes are:
1563 * - HyperV: 0x40000000 - 0x400000ff
1564 * - KVM: 0x40000100 - 0x400001ff
1566 static struct kvm_cpuid_entry2 *
1567 get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
1569 struct kvm_cpuid_entry2 *basic, *class;
1570 u32 function = *fn_ptr;
1572 basic = kvm_find_cpuid_entry(vcpu, 0);
1573 if (!basic)
1574 return NULL;
1576 if (is_guest_vendor_amd(basic->ebx, basic->ecx, basic->edx) ||
1577 is_guest_vendor_hygon(basic->ebx, basic->ecx, basic->edx))
1578 return NULL;
1580 if (function >= 0x40000000 && function <= 0x4fffffff)
1581 class = kvm_find_cpuid_entry(vcpu, function & 0xffffff00);
1582 else if (function >= 0xc0000000)
1583 class = kvm_find_cpuid_entry(vcpu, 0xc0000000);
1584 else
1585 class = kvm_find_cpuid_entry(vcpu, function & 0x80000000);
1587 if (class && function <= class->eax)
1588 return NULL;
1591 * Leaf specific adjustments are also applied when redirecting to the
1592 * max basic entry, e.g. if the max basic leaf is 0xb but there is no
1593 * entry for CPUID.0xb.index (see below), then the output value for EDX
1594 * needs to be pulled from CPUID.0xb.1.
1596 *fn_ptr = basic->eax;
1599 * The class does not exist or the requested function is out of range;
1600 * the effective CPUID entry is the max basic leaf. Note, the index of
1601 * the original requested leaf is observed!
1603 return kvm_find_cpuid_entry_index(vcpu, basic->eax, index);
1606 bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
1607 u32 *ecx, u32 *edx, bool exact_only)
1609 u32 orig_function = *eax, function = *eax, index = *ecx;
1610 struct kvm_cpuid_entry2 *entry;
1611 bool exact, used_max_basic = false;
1613 entry = kvm_find_cpuid_entry_index(vcpu, function, index);
1614 exact = !!entry;
1616 if (!entry && !exact_only) {
1617 entry = get_out_of_range_cpuid_entry(vcpu, &function, index);
1618 used_max_basic = !!entry;
1621 if (entry) {
1622 *eax = entry->eax;
1623 *ebx = entry->ebx;
1624 *ecx = entry->ecx;
1625 *edx = entry->edx;
1626 if (function == 7 && index == 0) {
1627 u64 data;
1628 if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) &&
1629 (data & TSX_CTRL_CPUID_CLEAR))
1630 *ebx &= ~(F(RTM) | F(HLE));
1631 } else if (function == 0x80000007) {
1632 if (kvm_hv_invtsc_suppressed(vcpu))
1633 *edx &= ~SF(CONSTANT_TSC);
1635 } else {
1636 *eax = *ebx = *ecx = *edx = 0;
1638 * When leaf 0BH or 1FH is defined, CL is pass-through
1639 * and EDX is always the x2APIC ID, even for undefined
1640 * subleaves. Index 1 will exist iff the leaf is
1641 * implemented, so we pass through CL iff leaf 1
1642 * exists. EDX can be copied from any existing index.
1644 if (function == 0xb || function == 0x1f) {
1645 entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
1646 if (entry) {
1647 *ecx = index & 0xff;
1648 *edx = entry->edx;
1652 trace_kvm_cpuid(orig_function, index, *eax, *ebx, *ecx, *edx, exact,
1653 used_max_basic);
1654 return exact;
1656 EXPORT_SYMBOL_GPL(kvm_cpuid);
1658 int kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
1660 u32 eax, ebx, ecx, edx;
1662 if (cpuid_fault_enabled(vcpu) && !kvm_require_cpl(vcpu, 0))
1663 return 1;
1665 eax = kvm_rax_read(vcpu);
1666 ecx = kvm_rcx_read(vcpu);
1667 kvm_cpuid(vcpu, &eax, &ebx, &ecx, &edx, false);
1668 kvm_rax_write(vcpu, eax);
1669 kvm_rbx_write(vcpu, ebx);
1670 kvm_rcx_write(vcpu, ecx);
1671 kvm_rdx_write(vcpu, edx);
1672 return kvm_skip_emulated_instruction(vcpu);
1674 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);