Skip to content

Commit c7d7c76

Browse files
Jinrong Liangsean-jc
authored andcommitted
KVM: selftests: Test consistency of CPUID with num of fixed counters
Extend the PMU counters test to verify KVM emulation of fixed counters in addition to general purpose counters. Fixed counters add an extra wrinkle in the form of an extra supported bitmask. Thus quoth the SDM: fixed-function performance counter 'i' is supported if ECX[i] || (EDX[4:0] > i) Test that KVM handles a counter being available through either method. Reviewed-by: Dapeng Mi <[email protected]> Co-developed-by: Like Xu <[email protected]> Signed-off-by: Like Xu <[email protected]> Signed-off-by: Jinrong Liang <[email protected]> Co-developed-by: Sean Christopherson <[email protected]> Tested-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 7137cf7 commit c7d7c76

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \
290290
msr, expected_val, val);
291291

292292
static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters,
293-
uint8_t nr_counters)
293+
uint8_t nr_counters, uint32_t or_mask)
294294
{
295295
uint8_t i;
296296

@@ -301,7 +301,13 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters
301301
*/
302302
const uint64_t test_val = 0xffff;
303303
const uint32_t msr = base_msr + i;
304-
const bool expect_success = i < nr_counters;
304+
305+
/*
306+
* Fixed counters are supported if the counter is less than the
307+
* number of enumerated contiguous counters *or* the counter is
308+
* explicitly enumerated in the supported counters mask.
309+
*/
310+
const bool expect_success = i < nr_counters || (or_mask & BIT(i));
305311

306312
/*
307313
* KVM drops writes to MSR_P6_PERFCTR[0|1] if the counters are
@@ -343,7 +349,7 @@ static void guest_test_gp_counters(void)
343349
else
344350
base_msr = MSR_IA32_PERFCTR0;
345351

346-
guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters);
352+
guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters, 0);
347353
}
348354

349355
static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities,
@@ -363,9 +369,50 @@ static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities,
363369
kvm_vm_free(vm);
364370
}
365371

372+
static void guest_test_fixed_counters(void)
373+
{
374+
uint64_t supported_bitmask = 0;
375+
uint8_t nr_fixed_counters = 0;
376+
377+
/* Fixed counters require Architectural vPMU Version 2+. */
378+
if (guest_get_pmu_version() >= 2)
379+
nr_fixed_counters = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
380+
381+
/*
382+
* The supported bitmask for fixed counters was introduced in PMU
383+
* version 5.
384+
*/
385+
if (guest_get_pmu_version() >= 5)
386+
supported_bitmask = this_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);
387+
388+
guest_rd_wr_counters(MSR_CORE_PERF_FIXED_CTR0, MAX_NR_FIXED_COUNTERS,
389+
nr_fixed_counters, supported_bitmask);
390+
}
391+
392+
static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities,
393+
uint8_t nr_fixed_counters,
394+
uint32_t supported_bitmask)
395+
{
396+
struct kvm_vcpu *vcpu;
397+
struct kvm_vm *vm;
398+
399+
vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_fixed_counters,
400+
pmu_version, perf_capabilities);
401+
402+
vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK,
403+
supported_bitmask);
404+
vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS,
405+
nr_fixed_counters);
406+
407+
run_vcpu(vcpu);
408+
409+
kvm_vm_free(vm);
410+
}
411+
366412
static void test_intel_counters(void)
367413
{
368414
uint8_t nr_arch_events = kvm_cpu_property(X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH);
415+
uint8_t nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
369416
uint8_t nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
370417
uint8_t pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
371418
unsigned int i;
@@ -435,6 +482,13 @@ static void test_intel_counters(void)
435482
v, perf_caps[i]);
436483
for (j = 0; j <= nr_gp_counters; j++)
437484
test_gp_counters(v, perf_caps[i], j);
485+
486+
pr_info("Testing fixed counters, PMU version %u, perf_caps = %lx\n",
487+
v, perf_caps[i]);
488+
for (j = 0; j <= nr_fixed_counters; j++) {
489+
for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
490+
test_fixed_counters(v, perf_caps[i], j, k);
491+
}
438492
}
439493
}
440494
}

0 commit comments

Comments
 (0)