]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: x86: synthesize CPUID leaf 0x80000021h if useful
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 21 Oct 2021 21:19:27 +0000 (17:19 -0400)
committerPaolo Bonzini <pbonzini@redhat.com>
Mon, 21 Mar 2022 13:28:40 +0000 (09:28 -0400)
Guests X86_BUG_NULL_SEG if and only if the host has them.  Use the info
from static_cpu_has_bug to form the 0x80000021 CPUID leaf that was
defined for Zen3.  Userspace can then set the bit even on older CPUs
that do not have the bug, such as Zen2.

Do the same for X86_FEATURE_LFENCE_RDTSC as well, since various processors
have had very different ways of detecting it and not all of them are
available to userspace.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/cpuid.c

index f0d5ac7a12c9228dc92e7e1fcb320eba36784a7f..32007f902bba7c45c23a130097532c540a4322cf 100644 (file)
@@ -723,6 +723,19 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
                /* Hypervisor leaves are always synthesized by __do_cpuid_func.  */
                return entry;
 
+       case 0x80000000:
+               /*
+                * 0x80000021 is sometimes synthesized by __do_cpuid_func, which
+                * would result in out-of-bounds calls to do_host_cpuid.
+                */
+               {
+                       static int max_cpuid_80000000;
+                       if (!READ_ONCE(max_cpuid_80000000))
+                               WRITE_ONCE(max_cpuid_80000000, cpuid_eax(0x80000000));
+                       if (function > READ_ONCE(max_cpuid_80000000))
+                               return entry;
+               }
+
        default:
                break;
        }
@@ -1069,6 +1082,14 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
                break;
        case 0x80000000:
                entry->eax = min(entry->eax, 0x80000021);
+               /*
+                * Serializing LFENCE is reported in a multitude of ways,
+                * and NullSegClearsBase is not reported in CPUID on Zen2;
+                * help userspace by providing the CPUID leaf ourselves.
+                */
+               if (static_cpu_has(X86_FEATURE_LFENCE_RDTSC)
+                   || !static_cpu_has_bug(X86_BUG_NULL_SEG))
+                       entry->eax = max(entry->eax, 0x80000021);
                break;
        case 0x80000001:
                cpuid_entry_override(entry, CPUID_8000_0001_EDX);
@@ -1155,6 +1176,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
                 *   EAX      13     PCMSR, Prefetch control MSR
                 */
                entry->eax &= BIT(0) | BIT(2) | BIT(6);
+               if (static_cpu_has(X86_FEATURE_LFENCE_RDTSC))
+                       entry->eax |= BIT(2);
+               if (!static_cpu_has_bug(X86_BUG_NULL_SEG))
+                       entry->eax |= BIT(6);
                break;
        /*Add support for Centaur's CPUID instruction*/
        case 0xC0000000: