Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs

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>

+20 -1
+20 -1
tools/testing/selftests/kvm/include/x86_64/apic.h
··· 11 11 #include <stdint.h> 12 12 13 13 #include "processor.h" 14 + #include "ucall_common.h" 14 15 15 16 #define APIC_DEFAULT_GPA 0xfee00000ULL 16 17 ··· 94 93 return rdmsr(APIC_BASE_MSR + (reg >> 4)); 95 94 } 96 95 96 + static inline uint8_t x2apic_write_reg_safe(unsigned int reg, uint64_t value) 97 + { 98 + return wrmsr_safe(APIC_BASE_MSR + (reg >> 4), value); 99 + } 100 + 97 101 static inline void x2apic_write_reg(unsigned int reg, uint64_t value) 98 102 { 99 - wrmsr(APIC_BASE_MSR + (reg >> 4), value); 103 + uint8_t fault = x2apic_write_reg_safe(reg, value); 104 + 105 + __GUEST_ASSERT(!fault, "Unexpected fault 0x%x on WRMSR(%x) = %lx\n", 106 + fault, APIC_BASE_MSR + (reg >> 4), value); 100 107 } 108 + 109 + static inline void x2apic_write_reg_fault(unsigned int reg, uint64_t value) 110 + { 111 + uint8_t fault = x2apic_write_reg_safe(reg, value); 112 + 113 + __GUEST_ASSERT(fault == GP_VECTOR, 114 + "Wanted #GP on WRMSR(%x) = %lx, got 0x%x\n", 115 + APIC_BASE_MSR + (reg >> 4), value, fault); 116 + } 117 + 101 118 102 119 #endif /* SELFTEST_KVM_APIC_H */