diff mbox series

[kvm-unit-tests,RESEND,2/2] x86/pmu: Wrap the written counter value with gp_counter_width

Message ID 20221226075412.61167-3-likexu@tencent.com (mailing list archive)
State New, archived
Headers show
Series x86/pmu: Add TSX testcase and fix force_emulation_prefix | expand

Commit Message

Like Xu Dec. 26, 2022, 7:54 a.m. UTC
From: Like Xu <likexu@tencent.com>

The check_emulated_instr() testcase fails when the KVM module parameter
"force_emulation_prefix" is 1. The root cause is that the value written by
the counter exceeds the maximum bit width of the GP counter.

Signed-off-by: Like Xu <likexu@tencent.com>
---
 x86/pmu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/x86/pmu.c b/x86/pmu.c
index 356d589..4dbbe71 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -477,6 +477,7 @@  static void check_running_counter_wrmsr(void)
 static void check_emulated_instr(void)
 {
 	uint64_t status, instr_start, brnch_start;
+	uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1;
 	unsigned int branch_idx = pmu.is_intel ? 5 : 2;
 	pmu_counter_t brnch_cnt = {
 		.ctr = MSR_GP_COUNTERx(0),
@@ -498,8 +499,8 @@  static void check_emulated_instr(void)
 
 	brnch_start = -EXPECTED_BRNCH;
 	instr_start = -EXPECTED_INSTR;
-	wrmsr(MSR_GP_COUNTERx(0), brnch_start);
-	wrmsr(MSR_GP_COUNTERx(1), instr_start);
+	wrmsr(MSR_GP_COUNTERx(0), brnch_start & gp_counter_width);
+	wrmsr(MSR_GP_COUNTERx(1), instr_start & gp_counter_width);
 	// KVM_FEP is a magic prefix that forces emulation so
 	// 'KVM_FEP "jne label\n"' just counts as a single instruction.
 	asm volatile(