Skip to content

Commit

Permalink
KVM: selftests: Add and use KVM helpers for x86 Family and Model
Browse files Browse the repository at this point in the history
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 <seanjc@google.com>
Link: https://lore.kernel.org/r/20221006005125.680782-12-seanjc@google.com
  • Loading branch information
Sean Christopherson committed Nov 17, 2022
1 parent 24f3f98 commit 074e9d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
19 changes: 17 additions & 2 deletions tools/testing/selftests/kvm/include/x86_64/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,27 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)
vcpu_ioctl(vcpu, KVM_SET_XCRS, xcrs);
}

const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
uint32_t function, uint32_t index);
const struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);

static inline uint32_t kvm_cpu_fms(void)
{
return get_cpuid_entry(kvm_get_supported_cpuid(), 0x1, 0)->eax;
}

static inline uint32_t kvm_cpu_family(void)
{
return x86_family(kvm_cpu_fms());
}

static inline uint32_t kvm_cpu_model(void)
{
return x86_model(kvm_cpu_fms());
}

bool kvm_cpuid_has(const struct kvm_cpuid2 *cpuid,
struct kvm_x86_cpu_feature feature);

Expand Down Expand Up @@ -825,8 +842,6 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
return cpuid;
}

const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
uint32_t function, uint32_t index);
void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const struct kvm_cpuid2 *cpuid);
void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);

Expand Down
23 changes: 11 additions & 12 deletions tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,19 @@ static bool use_intel_pmu(void)
kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED);
}

static bool is_zen1(uint32_t eax)
static bool is_zen1(uint32_t family, uint32_t model)
{
return x86_family(eax) == 0x17 && x86_model(eax) <= 0x0f;
return family == 0x17 && model <= 0x0f;
}

static bool is_zen2(uint32_t eax)
static bool is_zen2(uint32_t family, uint32_t model)
{
return x86_family(eax) == 0x17 &&
x86_model(eax) >= 0x30 && x86_model(eax) <= 0x3f;
return family == 0x17 && model >= 0x30 && model <= 0x3f;
}

static bool is_zen3(uint32_t eax)
static bool is_zen3(uint32_t family, uint32_t model)
{
return x86_family(eax) == 0x19 && x86_model(eax) <= 0x0f;
return family == 0x19 && model <= 0x0f;
}

/*
Expand All @@ -395,13 +394,13 @@ static bool is_zen3(uint32_t eax)
*/
static bool use_amd_pmu(void)
{
const struct kvm_cpuid_entry2 *entry;
uint32_t family = kvm_cpu_family();
uint32_t model = kvm_cpu_model();

entry = kvm_get_supported_cpuid_entry(1);
return is_amd_cpu() &&
(is_zen1(entry->eax) ||
is_zen2(entry->eax) ||
is_zen3(entry->eax));
(is_zen1(family, model) ||
is_zen2(family, model) ||
is_zen3(family, model));
}

int main(int argc, char *argv[])
Expand Down

0 comments on commit 074e9d4

Please sign in to comment.