]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs
authorSean Christopherson <seanjc@google.com>
Fri, 19 Jul 2024 23:51:03 +0000 (16:51 -0700)
committerSean Christopherson <seanjc@google.com>
Thu, 29 Aug 2024 23:25:06 +0000 (16:25 -0700)
Add helpers to allow and expect #GP on x2APIC MSRs, and opportunistically
have the existing helper spit out a more useful error message if an
unexpected exception occurs.

Link: https://lore.kernel.org/r/20240719235107.3023592-7-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86_64/apic.h

index 0f268b55fa0670c19e7646a76899f03ec9b562ab..51990094effda991967480eb3130464be5f9aa4c 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdint.h>
 
 #include "processor.h"
+#include "ucall_common.h"
 
 #define APIC_DEFAULT_GPA               0xfee00000ULL
 
@@ -93,9 +94,27 @@ static inline uint64_t x2apic_read_reg(unsigned int reg)
        return rdmsr(APIC_BASE_MSR + (reg >> 4));
 }
 
+static inline uint8_t x2apic_write_reg_safe(unsigned int reg, uint64_t value)
+{
+       return wrmsr_safe(APIC_BASE_MSR + (reg >> 4), value);
+}
+
 static inline void x2apic_write_reg(unsigned int reg, uint64_t value)
 {
-       wrmsr(APIC_BASE_MSR + (reg >> 4), value);
+       uint8_t fault = x2apic_write_reg_safe(reg, value);
+
+       __GUEST_ASSERT(!fault, "Unexpected fault 0x%x on WRMSR(%x) = %lx\n",
+                      fault, APIC_BASE_MSR + (reg >> 4), value);
 }
 
+static inline void x2apic_write_reg_fault(unsigned int reg, uint64_t value)
+{
+       uint8_t fault = x2apic_write_reg_safe(reg, value);
+
+       __GUEST_ASSERT(fault == GP_VECTOR,
+                      "Wanted #GP on WRMSR(%x) = %lx, got 0x%x\n",
+                      APIC_BASE_MSR + (reg >> 4), value, fault);
+}
+
+
 #endif /* SELFTEST_KVM_APIC_H */