Skip to content

Commit 787071f

Browse files
Jinrong Liangsean-jc
authored andcommitted
KVM: selftests: Add functional test for Intel's fixed PMU counters
Extend the fixed counters test to verify that supported counters can actually be enabled in the control MSRs, that unsupported counters cannot, and that enabled counters actually count. Co-developed-by: Like Xu <[email protected]> Signed-off-by: Like Xu <[email protected]> Signed-off-by: Jinrong Liang <[email protected]> [sean: fold into the rd/wr access test, massage changelog] Reviewed-by: Dapeng Mi <[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 c7d7c76 commit 787071f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters
332332
vector = wrmsr_safe(msr, 0);
333333
GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector);
334334
}
335-
GUEST_DONE();
336335
}
337336

338337
static void guest_test_gp_counters(void)
@@ -350,6 +349,7 @@ static void guest_test_gp_counters(void)
350349
base_msr = MSR_IA32_PERFCTR0;
351350

352351
guest_rd_wr_counters(base_msr, MAX_NR_GP_COUNTERS, nr_gp_counters, 0);
352+
GUEST_DONE();
353353
}
354354

355355
static void test_gp_counters(uint8_t pmu_version, uint64_t perf_capabilities,
@@ -373,6 +373,7 @@ static void guest_test_fixed_counters(void)
373373
{
374374
uint64_t supported_bitmask = 0;
375375
uint8_t nr_fixed_counters = 0;
376+
uint8_t i;
376377

377378
/* Fixed counters require Architectural vPMU Version 2+. */
378379
if (guest_get_pmu_version() >= 2)
@@ -387,6 +388,34 @@ static void guest_test_fixed_counters(void)
387388

388389
guest_rd_wr_counters(MSR_CORE_PERF_FIXED_CTR0, MAX_NR_FIXED_COUNTERS,
389390
nr_fixed_counters, supported_bitmask);
391+
392+
for (i = 0; i < MAX_NR_FIXED_COUNTERS; i++) {
393+
uint8_t vector;
394+
uint64_t val;
395+
396+
if (i >= nr_fixed_counters && !(supported_bitmask & BIT_ULL(i))) {
397+
vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL,
398+
FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
399+
__GUEST_ASSERT(vector == GP_VECTOR,
400+
"Expected #GP for counter %u in FIXED_CTR_CTRL", i);
401+
402+
vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL,
403+
FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
404+
__GUEST_ASSERT(vector == GP_VECTOR,
405+
"Expected #GP for counter %u in PERF_GLOBAL_CTRL", i);
406+
continue;
407+
}
408+
409+
wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, 0);
410+
wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
411+
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
412+
__asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
413+
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
414+
val = rdmsr(MSR_CORE_PERF_FIXED_CTR0 + i);
415+
416+
GUEST_ASSERT_NE(val, 0);
417+
}
418+
GUEST_DONE();
390419
}
391420

392421
static void test_fixed_counters(uint8_t pmu_version, uint64_t perf_capabilities,

0 commit comments

Comments
 (0)