KVM: selftests: Add dedicated helpers for getting x86 Family and Model
authorSean Christopherson <seanjc@google.com>
Thu, 6 Oct 2022 00:51:23 +0000 (00:51 +0000)
committerSean Christopherson <seanjc@google.com>
Thu, 17 Nov 2022 00:59:06 +0000 (16:59 -0800)
Add dedicated helpers for getting x86's Family and Model, which are the
last holdouts that "need" raw access to CPUID information.  FMS info is
a mess and requires not only splicing together multiple values, but
requires doing so conditional in the Family case.

Provide wrappers to reduce the odds of copy+paste errors, but mostly to
allow for the eventual removal of kvm_get_supported_cpuid_entry().

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221006005125.680782-11-seanjc@google.com
tools/testing/selftests/kvm/include/x86_64/processor.h
tools/testing/selftests/kvm/lib/x86_64/processor.c

index 3433933..5d0acf8 100644 (file)
@@ -247,6 +247,23 @@ struct kvm_x86_pmu_feature {
 
 #define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED   KVM_X86_PMU_FEATURE(BRANCH_INSNS_RETIRED, 5)
 
+static inline unsigned int x86_family(unsigned int eax)
+{
+       unsigned int x86;
+
+       x86 = (eax >> 8) & 0xf;
+
+       if (x86 == 0xf)
+               x86 += (eax >> 20) & 0xff;
+
+       return x86;
+}
+
+static inline unsigned int x86_model(unsigned int eax)
+{
+       return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
+}
+
 /* Page table bitfield declarations */
 #define PTE_PRESENT_MASK        BIT_ULL(0)
 #define PTE_WRITABLE_MASK       BIT_ULL(1)
@@ -516,6 +533,24 @@ static inline void cpuid(uint32_t function,
        return __cpuid(function, 0, eax, ebx, ecx, edx);
 }
 
+static inline uint32_t this_cpu_fms(void)
+{
+       uint32_t eax, ebx, ecx, edx;
+
+       cpuid(1, &eax, &ebx, &ecx, &edx);
+       return eax;
+}
+
+static inline uint32_t this_cpu_family(void)
+{
+       return x86_family(this_cpu_fms());
+}
+
+static inline uint32_t this_cpu_model(void)
+{
+       return x86_model(this_cpu_fms());
+}
+
 static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index,
                                      uint8_t reg, uint8_t lo, uint8_t hi)
 {
@@ -658,23 +693,6 @@ static inline void cpu_relax(void)
 bool is_intel_cpu(void);
 bool is_amd_cpu(void);
 
-static inline unsigned int x86_family(unsigned int eax)
-{
-       unsigned int x86;
-
-       x86 = (eax >> 8) & 0xf;
-
-       if (x86 == 0xf)
-               x86 += (eax >> 20) & 0xff;
-
-       return x86;
-}
-
-static inline unsigned int x86_model(unsigned int eax)
-{
-       return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
-}
-
 struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu);
 void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state);
 void kvm_x86_state_cleanup(struct kvm_x86_state *state);
index af23c70..65e87b5 100644 (file)
@@ -1239,7 +1239,6 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 {
        const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */
        unsigned long ht_gfn, max_gfn, max_pfn;
-       uint32_t eax, ebx, ecx, edx;
        uint8_t maxphyaddr;
 
        max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
@@ -1254,8 +1253,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 
        /* Before family 17h, the HyperTransport area is just below 1T.  */
        ht_gfn = (1 << 28) - num_ht_pages;
-       cpuid(1, &eax, &ebx, &ecx, &edx);
-       if (x86_family(eax) < 0x17)
+       if (this_cpu_family() < 0x17)
                goto done;
 
        /*