Skip to content

Commit 08a8282

Browse files
committed
KVM: selftests: Verify post-RESET value of PERF_GLOBAL_CTRL in PMCs test
Add a guest assert in the PMU counters test to verify that KVM stuffs the vCPU's post-RESET value to globally enable all general purpose counters. Per Intel's SDM, IA32_PERF_GLOBAL_CTRL: Sets bits n-1:0 and clears the upper bits. and Where "n" is the number of general-purpose counters available in the processor. For the edge case where there are zero GP counters, follow the spirit of the architecture, not the SDM's literal wording, which doesn't account for this possibility and would require the CPU to set _all_ bits in PERF_GLOBAL_CTRL. 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 de120e1 commit 08a8282

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,30 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters
416416

417417
static void guest_test_gp_counters(void)
418418
{
419+
uint8_t pmu_version = guest_get_pmu_version();
419420
uint8_t nr_gp_counters = 0;
420421
uint32_t base_msr;
421422

422-
if (guest_get_pmu_version())
423+
if (pmu_version)
423424
nr_gp_counters = this_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
424425

426+
/*
427+
* For v2+ PMUs, PERF_GLOBAL_CTRL's architectural post-RESET value is
428+
* "Sets bits n-1:0 and clears the upper bits", where 'n' is the number
429+
* of GP counters. If there are no GP counters, require KVM to leave
430+
* PERF_GLOBAL_CTRL '0'. This edge case isn't covered by the SDM, but
431+
* follow the spirit of the architecture and only globally enable GP
432+
* counters, of which there are none.
433+
*/
434+
if (pmu_version > 1) {
435+
uint64_t global_ctrl = rdmsr(MSR_CORE_PERF_GLOBAL_CTRL);
436+
437+
if (nr_gp_counters)
438+
GUEST_ASSERT_EQ(global_ctrl, GENMASK_ULL(nr_gp_counters - 1, 0));
439+
else
440+
GUEST_ASSERT_EQ(global_ctrl, 0);
441+
}
442+
425443
if (this_cpu_has(X86_FEATURE_PDCM) &&
426444
rdmsr(MSR_IA32_PERF_CAPABILITIES) & PMU_CAP_FW_WRITES)
427445
base_msr = MSR_IA32_PMC0;

0 commit comments

Comments
 (0)