On 02/11/2022 23.50, Sean Christopherson wrote:
From: Like Xu <likexu@xxxxxxxxxxx>
Specifying an unsupported PMC encoding will cause a #GP(0).
There are multiple reasons RDPMC can #GP, the one that is being relied
on to guarantee #GP is specifically that the PMC is invalid. The most
extensible solution is to provide a safe variant.
Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Signed-off-by: Like Xu <likexu@xxxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
lib/x86/processor.h | 21 ++++++++++++++++++---
x86/pmu.c | 10 ++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index f85abe36..ba14c7a0 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -438,11 +438,26 @@ static inline int wrmsr_safe(u32 index, u64 val)
return exception_vector();
}
+static inline int rdpmc_safe(u32 index, uint64_t *val)
+{
+ uint32_t a, d;
+
+ asm volatile (ASM_TRY("1f")
+ "rdpmc\n\t"
+ "1:"
+ : "=a"(a), "=d"(d) : "c"(index) : "memory");
+ *val = (uint64_t)a | ((uint64_t)d << 32);
+ return exception_vector();
+}
+
static inline uint64_t rdpmc(uint32_t index)
{
- uint32_t a, d;
- asm volatile ("rdpmc" : "=a"(a), "=d"(d) : "c"(index));
- return a | ((uint64_t)d << 32);
+ uint64_t val;
+ int vector = rdpmc_safe(index, &val);
+
+ assert_msg(!vector, "Unexpected %s on RDPMC(%d)",
+ exception_mnemonic(vector), index);
+ return val;
}
Seems like this is causing the CI to fail:
https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/jobs/3339274319#L1260
I guess you have to use PRId32 here? Could you please send a patch?
Thanks,
Thomas