Skip to content

Commit 074e9d4

Browse files
committed
KVM: selftests: Add and use KVM helpers for x86 Family and Model
Add KVM variants of the x86 Family and Model helpers, and use them in the PMU event filter test. Open code the retrieval of KVM's supported CPUID entry 0x1.0 in anticipation of dropping 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 24f3f98 commit 074e9d4

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,27 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)
754754
vcpu_ioctl(vcpu, KVM_SET_XCRS, xcrs);
755755
}
756756

757+
const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
758+
uint32_t function, uint32_t index);
757759
const struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
758760
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
759761
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
760762

763+
static inline uint32_t kvm_cpu_fms(void)
764+
{
765+
return get_cpuid_entry(kvm_get_supported_cpuid(), 0x1, 0)->eax;
766+
}
767+
768+
static inline uint32_t kvm_cpu_family(void)
769+
{
770+
return x86_family(kvm_cpu_fms());
771+
}
772+
773+
static inline uint32_t kvm_cpu_model(void)
774+
{
775+
return x86_model(kvm_cpu_fms());
776+
}
777+
761778
bool kvm_cpuid_has(const struct kvm_cpuid2 *cpuid,
762779
struct kvm_x86_cpu_feature feature);
763780

@@ -825,8 +842,6 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
825842
return cpuid;
826843
}
827844

828-
const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
829-
uint32_t function, uint32_t index);
830845
void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid);
831846
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
832847

tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,19 @@ static bool use_intel_pmu(void)
369369
kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED);
370370
}
371371

372-
static bool is_zen1(uint32_t eax)
372+
static bool is_zen1(uint32_t family, uint32_t model)
373373
{
374-
return x86_family(eax) == 0x17 && x86_model(eax) <= 0x0f;
374+
return family == 0x17 && model <= 0x0f;
375375
}
376376

377-
static bool is_zen2(uint32_t eax)
377+
static bool is_zen2(uint32_t family, uint32_t model)
378378
{
379-
return x86_family(eax) == 0x17 &&
380-
x86_model(eax) >= 0x30 && x86_model(eax) <= 0x3f;
379+
return family == 0x17 && model >= 0x30 && model <= 0x3f;
381380
}
382381

383-
static bool is_zen3(uint32_t eax)
382+
static bool is_zen3(uint32_t family, uint32_t model)
384383
{
385-
return x86_family(eax) == 0x19 && x86_model(eax) <= 0x0f;
384+
return family == 0x19 && model <= 0x0f;
386385
}
387386

388387
/*
@@ -395,13 +394,13 @@ static bool is_zen3(uint32_t eax)
395394
*/
396395
static bool use_amd_pmu(void)
397396
{
398-
const struct kvm_cpuid_entry2 *entry;
397+
uint32_t family = kvm_cpu_family();
398+
uint32_t model = kvm_cpu_model();
399399

400-
entry = kvm_get_supported_cpuid_entry(1);
401400
return is_amd_cpu() &&
402-
(is_zen1(entry->eax) ||
403-
is_zen2(entry->eax) ||
404-
is_zen3(entry->eax));
401+
(is_zen1(family, model) ||
402+
is_zen2(family, model) ||
403+
is_zen3(family, model));
405404
}
406405

407406
int main(int argc, char *argv[])

0 commit comments

Comments
 (0)