Skip to content

Commit 5228c02

Browse files
committed
KVM: selftests: Add PMU feature framework, use in PMU event filter test
Add an X86_PMU_FEATURE_* framework to simplify probing architectural events on Intel PMUs, which require checking the length of a bit vector and the _absence_ of a "feature" bit. Add helpers for both KVM and "this CPU", and use the newfangled magic (along with X86_PROPERTY_*) to clean up pmu_event_filter_test. No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4feb9d2 commit 5228c02

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ struct kvm_x86_cpu_property {
201201

202202
#define X86_PROPERTY_MAX_BASIC_LEAF KVM_X86_CPU_PROPERTY(0, 0, EAX, 0, 31)
203203
#define X86_PROPERTY_PMU_VERSION KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 0, 7)
204+
#define X86_PROPERTY_PMU_NR_GP_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 8, 15)
205+
#define X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 24, 31)
204206

205207
#define X86_PROPERTY_XSTATE_MAX_SIZE_XCR0 KVM_X86_CPU_PROPERTY(0xd, 0, EBX, 0, 31)
206208
#define X86_PROPERTY_XSTATE_MAX_SIZE KVM_X86_CPU_PROPERTY(0xd, 0, ECX, 0, 31)
@@ -221,6 +223,29 @@ struct kvm_x86_cpu_property {
221223

222224
#define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31)
223225

226+
/*
227+
* Intel's architectural PMU events are bizarre. They have a "feature" bit
228+
* that indicates the feature is _not_ supported, and a property that states
229+
* the length of the bit mask of unsupported features. A feature is supported
230+
* if the size of the bit mask is larger than the "unavailable" bit, and said
231+
* bit is not set.
232+
*
233+
* Wrap the "unavailable" feature to simplify checking whether or not a given
234+
* architectural event is supported.
235+
*/
236+
struct kvm_x86_pmu_feature {
237+
struct kvm_x86_cpu_feature anti_feature;
238+
};
239+
#define KVM_X86_PMU_FEATURE(name, __bit) \
240+
({ \
241+
struct kvm_x86_pmu_feature feature = { \
242+
.anti_feature = KVM_X86_CPU_FEATURE(0xa, 0, EBX, __bit), \
243+
}; \
244+
\
245+
feature; \
246+
})
247+
248+
#define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(BRANCH_INSNS_RETIRED, 5)
224249

225250
/* Page table bitfield declarations */
226251
#define PTE_PRESENT_MASK BIT_ULL(0)
@@ -535,6 +560,14 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property)
535560
return max_leaf >= property.function;
536561
}
537562

563+
static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
564+
{
565+
uint32_t nr_bits = this_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH);
566+
567+
return nr_bits > feature.anti_feature.bit &&
568+
!this_cpu_has(feature.anti_feature);
569+
}
570+
538571
#define SET_XMM(__var, __xmm) \
539572
asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
540573

@@ -743,6 +776,14 @@ static __always_inline bool kvm_cpu_has_p(struct kvm_x86_cpu_property property)
743776
return max_leaf >= property.function;
744777
}
745778

779+
static inline bool kvm_pmu_has(struct kvm_x86_pmu_feature feature)
780+
{
781+
uint32_t nr_bits = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH);
782+
783+
return nr_bits > feature.anti_feature.bit &&
784+
!kvm_cpu_has(feature.anti_feature);
785+
}
786+
746787
static inline size_t kvm_cpuid2_size(int nr_entries)
747788
{
748789
return sizeof(struct kvm_cpuid2) +

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

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,6 @@
2121
#define ARCH_PERFMON_EVENTSEL_OS (1ULL << 17)
2222
#define ARCH_PERFMON_EVENTSEL_ENABLE (1ULL << 22)
2323

24-
union cpuid10_eax {
25-
struct {
26-
unsigned int version_id:8;
27-
unsigned int num_counters:8;
28-
unsigned int bit_width:8;
29-
unsigned int mask_length:8;
30-
} split;
31-
unsigned int full;
32-
};
33-
34-
union cpuid10_ebx {
35-
struct {
36-
unsigned int no_unhalted_core_cycles:1;
37-
unsigned int no_instructions_retired:1;
38-
unsigned int no_unhalted_reference_cycles:1;
39-
unsigned int no_llc_reference:1;
40-
unsigned int no_llc_misses:1;
41-
unsigned int no_branch_instruction_retired:1;
42-
unsigned int no_branch_misses_retired:1;
43-
} split;
44-
unsigned int full;
45-
};
46-
4724
/* End of stuff taken from perf_event.h. */
4825

4926
/* Oddly, this isn't in perf_event.h. */
@@ -380,30 +357,16 @@ static void test_pmu_config_disable(void (*guest_code)(void))
380357
}
381358

382359
/*
383-
* Check for a non-zero PMU version, at least one general-purpose
384-
* counter per logical processor, an EBX bit vector of length greater
385-
* than 5, and EBX[5] clear.
386-
*/
387-
static bool check_intel_pmu_leaf(const struct kvm_cpuid_entry2 *entry)
388-
{
389-
union cpuid10_eax eax = { .full = entry->eax };
390-
union cpuid10_ebx ebx = { .full = entry->ebx };
391-
392-
return eax.split.version_id && eax.split.num_counters > 0 &&
393-
eax.split.mask_length > ARCH_PERFMON_BRANCHES_RETIRED &&
394-
!ebx.split.no_branch_instruction_retired;
395-
}
396-
397-
/*
398-
* Note that CPUID leaf 0xa is Intel-specific. This leaf should be
399-
* clear on AMD hardware.
360+
* On Intel, check for a non-zero PMU version, at least one general-purpose
361+
* counter per logical processor, and support for counting the number of branch
362+
* instructions retired.
400363
*/
401364
static bool use_intel_pmu(void)
402365
{
403-
const struct kvm_cpuid_entry2 *entry;
404-
405-
entry = kvm_get_supported_cpuid_entry(0xa);
406-
return is_intel_cpu() && check_intel_pmu_leaf(entry);
366+
return is_intel_cpu() &&
367+
kvm_cpu_property(X86_PROPERTY_PMU_VERSION) &&
368+
kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS) &&
369+
kvm_pmu_has(X86_PMU_FEATURE_BRANCH_INSNS_RETIRED);
407370
}
408371

409372
static bool is_zen1(uint32_t eax)

0 commit comments

Comments
 (0)