Skip to content

Commit 24f3f98

Browse files
committed
KVM: selftests: Add dedicated helpers for getting x86 Family and Model
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5228c02 commit 24f3f98

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ struct kvm_x86_pmu_feature {
247247

248248
#define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(BRANCH_INSNS_RETIRED, 5)
249249

250+
static inline unsigned int x86_family(unsigned int eax)
251+
{
252+
unsigned int x86;
253+
254+
x86 = (eax >> 8) & 0xf;
255+
256+
if (x86 == 0xf)
257+
x86 += (eax >> 20) & 0xff;
258+
259+
return x86;
260+
}
261+
262+
static inline unsigned int x86_model(unsigned int eax)
263+
{
264+
return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
265+
}
266+
250267
/* Page table bitfield declarations */
251268
#define PTE_PRESENT_MASK BIT_ULL(0)
252269
#define PTE_WRITABLE_MASK BIT_ULL(1)
@@ -516,6 +533,24 @@ static inline void cpuid(uint32_t function,
516533
return __cpuid(function, 0, eax, ebx, ecx, edx);
517534
}
518535

536+
static inline uint32_t this_cpu_fms(void)
537+
{
538+
uint32_t eax, ebx, ecx, edx;
539+
540+
cpuid(1, &eax, &ebx, &ecx, &edx);
541+
return eax;
542+
}
543+
544+
static inline uint32_t this_cpu_family(void)
545+
{
546+
return x86_family(this_cpu_fms());
547+
}
548+
549+
static inline uint32_t this_cpu_model(void)
550+
{
551+
return x86_model(this_cpu_fms());
552+
}
553+
519554
static inline uint32_t __this_cpu_has(uint32_t function, uint32_t index,
520555
uint8_t reg, uint8_t lo, uint8_t hi)
521556
{
@@ -658,23 +693,6 @@ static inline void cpu_relax(void)
658693
bool is_intel_cpu(void);
659694
bool is_amd_cpu(void);
660695

661-
static inline unsigned int x86_family(unsigned int eax)
662-
{
663-
unsigned int x86;
664-
665-
x86 = (eax >> 8) & 0xf;
666-
667-
if (x86 == 0xf)
668-
x86 += (eax >> 20) & 0xff;
669-
670-
return x86;
671-
}
672-
673-
static inline unsigned int x86_model(unsigned int eax)
674-
{
675-
return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
676-
}
677-
678696
struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu);
679697
void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state);
680698
void kvm_x86_state_cleanup(struct kvm_x86_state *state);

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,6 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12391239
{
12401240
const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */
12411241
unsigned long ht_gfn, max_gfn, max_pfn;
1242-
uint32_t eax, ebx, ecx, edx;
12431242
uint8_t maxphyaddr;
12441243

12451244
max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
@@ -1254,8 +1253,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
12541253

12551254
/* Before family 17h, the HyperTransport area is just below 1T. */
12561255
ht_gfn = (1 << 28) - num_ht_pages;
1257-
cpuid(1, &eax, &ebx, &ecx, &edx);
1258-
if (x86_family(eax) < 0x17)
1256+
if (this_cpu_family() < 0x17)
12591257
goto done;
12601258

12611259
/*

0 commit comments

Comments
 (0)