GUEST_MEASURE_EVENT() hardcodes symbols specific to pmu_counters_test, preventing it from being safely shared with other PMU tests. Flush and reload a local dummy stack variable instead of kvm_pmu_version. Because the dummy variable resides on the stack, pass its address in %rsi ("S") to remain immune to %rsp and %rbp changes caused by "enter $0, $0". Also mark %rax/%rdx/%rcx as read-write outputs (+a/+d/+c) and add "cc" to clobbers since the asm modifies them, pass loop count via an immediate operand ("i"), and fix the "CLFUSH" typo in the comment. No change in what the test measures. Signed-off-by: Zide Chen --- v10: new patch. --- .../selftests/kvm/x86/pmu_counters_test.c | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c index c3e784e16348..5ae7f2d15d04 100644 --- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c @@ -221,51 +221,60 @@ static void guest_assert_event_count(u8 idx, u32 pmc, u32 pmc_msr) /* * Enable and disable the PMC in a monolithic asm blob to ensure that the - * compiler can't insert _any_ code into the measured sequence. Note, ECX - * doesn't need to be clobbered as the input value, @pmc_msr, is restored - * before the end of the sequence. + * compiler can't insert _any_ code into the measured sequence. * - * If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the - * CLFUSH{,OPT} instruction on each loop iteration to force LLC references and - * misses, i.e. to allow testing that those events actually count. + * If CLFLUSH{,OPT} is supported, flush and reload a dummy cacheline on each + * loop iteration to force LLC references and misses, i.e. to allow testing + * that those events actually count. * * If forced emulation is enabled (and specified), force emulation on a subset * of the measured code to verify that KVM correctly emulates instructions and * branches retired events in conjunction with hardware also counting said * events. */ -#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP) \ -do { \ - __asm__ __volatile__("wrmsr\n\t" \ - " mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \ - "1:\n\t" \ - FEP "enter $0, $0\n\t" \ - clflush "\n\t" \ - "mfence\n\t" \ - "mov %[m], %%eax\n\t" \ - FEP "leave\n\t" \ - FEP "loop 1b\n\t" \ - FEP "mov %%edi, %%ecx\n\t" \ - FEP "xor %%eax, %%eax\n\t" \ - FEP "xor %%edx, %%edx\n\t" \ - "wrmsr\n\t" \ - :: "a"((u32)_value), "d"(_value >> 32), \ - "c"(_msr), "D"(_msr), [m]"m"(kvm_pmu_version) \ - ); \ +#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP, _loops) \ +do { \ + u32 _a = (u32)(_value), _d = (u32)((_value) >> 32); \ + u32 _c = (_msr); \ + u32 _dummy = 0; \ + \ + __asm__ __volatile__("wrmsr\n\t" \ + "mov %[loops], %%ecx\n\t" \ + "1:\n\t" \ + FEP "enter $0, $0\n\t" \ + clflush "\n\t" \ + "mfence\n\t" \ + "mov (%[ptr]), %%eax\n\t" \ + FEP "leave\n\t" \ + FEP "loop 1b\n\t" \ + FEP "mov %%edi, %%ecx\n\t" \ + FEP "xor %%eax, %%eax\n\t" \ + FEP "xor %%edx, %%edx\n\t" \ + "wrmsr\n\t" \ + : "+a"(_a), "+d"(_d), "+c"(_c) \ + : "D"(_msr), [ptr]"S"(&_dummy), \ + [loops]"i"(_loops) \ + : "memory", "cc" \ + ); \ } while (0) -#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ -do { \ - wrmsr(_pmc_msr, 0); \ - \ - if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt %[m]", FEP); \ - else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush %[m]", FEP); \ - else \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ - \ - guest_assert_event_count(_idx, _pmc, _pmc_msr); \ +#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \ +do { \ + wrmsr(_pmc_msr, 0); \ + \ + if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflushopt (%[ptr])", \ + FEP, NUM_LOOPS); \ + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflush (%[ptr])", \ + FEP, NUM_LOOPS); \ + else \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", \ + FEP, NUM_LOOPS); \ + \ + guest_assert_event_count(_idx, _pmc, _pmc_msr); \ } while (0) static void __guest_test_arch_event(u8 idx, u32 pmc, u32 pmc_msr, -- 2.56.1