[kvm-unit-tests PATCH 1/2] x86/pmu: Update rdpmc testcase to cover #GP and emulation path

From: Like Xu
Date: Mon Sep 05 2022 - 08:44:14 EST


From: Like Xu <likexu@xxxxxxxxxxx>

Specifying an unsupported PMC encoding will cause a #GP(0).
All testcases should be passed when the KVM_FEP prefix is added.

Signed-off-by: Like Xu <likexu@xxxxxxxxxxx>
---
lib/x86/processor.h | 5 ++++-
x86/pmu.c | 13 +++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index 10bca27..9c490d9 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -441,7 +441,10 @@ static inline int wrmsr_safe(u32 index, u64 val)
static inline uint64_t rdpmc(uint32_t index)
{
uint32_t a, d;
- asm volatile ("rdpmc" : "=a"(a), "=d"(d) : "c"(index));
+ if (is_fep_available())
+ asm volatile (KVM_FEP "rdpmc" : "=a"(a), "=d"(d) : "c"(index));
+ else
+ asm volatile ("rdpmc" : "=a"(a), "=d"(d) : "c"(index));
return a | ((uint64_t)d << 32);
}

diff --git a/x86/pmu.c b/x86/pmu.c
index 203a9d4..11607c0 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -758,12 +758,25 @@ static bool pmu_is_detected(void)
return detect_intel_pmu();
}

+static void rdpmc_unsupported_counter(void *data)
+{
+ rdpmc(64);
+}
+
+static void check_rdpmc_cause_gp(void)
+{
+ report(test_for_exception(GP_VECTOR, rdpmc_unsupported_counter, NULL),
+ "rdpmc with invalid PMC index raises #GP");
+}
+
int main(int ac, char **av)
{
setup_vm();
handle_irq(PC_VECTOR, cnt_overflow);
buf = malloc(N*64);

+ check_rdpmc_cause_gp();
+
if (!pmu_is_detected())
return report_summary();

--
2.37.3