17d36db35SAvi Kivity /* msr tests */ 27d36db35SAvi Kivity 37d36db35SAvi Kivity #include "libcflat.h" 4850479e3SJason Wang #include "processor.h" 5d1bdd07cSAvi Kivity #include "msr.h" 67d36db35SAvi Kivity 77d36db35SAvi Kivity struct msr_info { 87d36db35SAvi Kivity int index; 9142ff635SSean Christopherson bool is_64bit_only; 10797d79a2SThomas Huth const char *name; 117d36db35SAvi Kivity unsigned long long value; 127d36db35SAvi Kivity }; 137d36db35SAvi Kivity 147d36db35SAvi Kivity 157d36db35SAvi Kivity #define addr_64 0x0000123456789abcULL 168feb8cfbSSean Christopherson #define addr_ul (unsigned long)addr_64 177d36db35SAvi Kivity 18142ff635SSean Christopherson #define MSR_TEST(msr, val, only64) \ 19142ff635SSean Christopherson { .index = msr, .name = #msr, .value = val, .is_64bit_only = only64 } 2064662079SSean Christopherson 217d36db35SAvi Kivity struct msr_info msr_info[] = 227d36db35SAvi Kivity { 23142ff635SSean Christopherson MSR_TEST(MSR_IA32_SYSENTER_CS, 0x1234, false), 24142ff635SSean Christopherson MSR_TEST(MSR_IA32_SYSENTER_ESP, addr_ul, false), 25142ff635SSean Christopherson MSR_TEST(MSR_IA32_SYSENTER_EIP, addr_ul, false), 267d36db35SAvi Kivity // reserved: 1:2, 4:6, 8:10, 13:15, 17, 19:21, 24:33, 35:63 27142ff635SSean Christopherson MSR_TEST(MSR_IA32_MISC_ENABLE, 0x400c51889, false), 28142ff635SSean Christopherson MSR_TEST(MSR_IA32_CR_PAT, 0x07070707, false), 29142ff635SSean Christopherson MSR_TEST(MSR_FS_BASE, addr_64, true), 30142ff635SSean Christopherson MSR_TEST(MSR_GS_BASE, addr_64, true), 31142ff635SSean Christopherson MSR_TEST(MSR_KERNEL_GS_BASE, addr_64, true), 322ac205f8SSean Christopherson #ifdef __x86_64__ 33142ff635SSean Christopherson MSR_TEST(MSR_EFER, 0xD00, false), 34ff2525d7SSean Christopherson #endif 35142ff635SSean Christopherson MSR_TEST(MSR_LSTAR, addr_64, true), 36142ff635SSean Christopherson MSR_TEST(MSR_CSTAR, addr_64, true), 37142ff635SSean Christopherson MSR_TEST(MSR_SYSCALL_MASK, 0xffffffff, true), 387d36db35SAvi Kivity // MSR_IA32_DEBUGCTLMSR needs svm feature LBRV 397d36db35SAvi Kivity // MSR_VM_HSAVE_PA only AMD host 407d36db35SAvi Kivity }; 417d36db35SAvi Kivity 423e788d91SSean Christopherson static void test_msr_rw(struct msr_info *msr, unsigned long long val) 437d36db35SAvi Kivity { 44a73d6ae4SSean Christopherson unsigned long long r, orig; 4550273266SSean Christopherson 463e788d91SSean Christopherson orig = rdmsr(msr->index); 473e788d91SSean Christopherson wrmsr(msr->index, val); 483e788d91SSean Christopherson r = rdmsr(msr->index); 493e788d91SSean Christopherson wrmsr(msr->index, orig); 509295327cSSean Christopherson if (r != val) { 51d26193a0SRoman Bolshakov printf("testing %s: output = %#" PRIx32 ":%#" PRIx32 523e788d91SSean Christopherson " expected = %#" PRIx32 ":%#" PRIx32 "\n", msr->name, 539295327cSSean Christopherson (u32)(r >> 32), (u32)r, (u32)(val >> 32), (u32)val); 547d36db35SAvi Kivity } 553e788d91SSean Christopherson report(val == r, "%s", msr->name); 567d36db35SAvi Kivity } 577d36db35SAvi Kivity 58142ff635SSean Christopherson static void test_wrmsr_fault(struct msr_info *msr, unsigned long long val) 59142ff635SSean Christopherson { 60142ff635SSean Christopherson unsigned char vector = wrmsr_checking(msr->index, val); 61142ff635SSean Christopherson 62142ff635SSean Christopherson report(vector == GP_VECTOR, 63142ff635SSean Christopherson "Expected #GP on WRSMR(%s, 0x%llx), got vector %d", 64142ff635SSean Christopherson msr->name, val, vector); 65142ff635SSean Christopherson } 66142ff635SSean Christopherson 67142ff635SSean Christopherson static void test_rdmsr_fault(struct msr_info *msr) 68142ff635SSean Christopherson { 69142ff635SSean Christopherson unsigned char vector = rdmsr_checking(msr->index); 70142ff635SSean Christopherson 71142ff635SSean Christopherson report(vector == GP_VECTOR, 72142ff635SSean Christopherson "Expected #GP on RDSMR(%s), got vector %d", msr->name, vector); 73142ff635SSean Christopherson } 74142ff635SSean Christopherson 757d36db35SAvi Kivity int main(int ac, char **av) 767d36db35SAvi Kivity { 77142ff635SSean Christopherson bool is_64bit_host = this_cpu_has(X86_FEATURE_LM); 7864662079SSean Christopherson int i; 7964662079SSean Christopherson 80142ff635SSean Christopherson for (i = 0 ; i < ARRAY_SIZE(msr_info); i++) { 81142ff635SSean Christopherson if (is_64bit_host || !msr_info[i].is_64bit_only) { 823e788d91SSean Christopherson test_msr_rw(&msr_info[i], msr_info[i].value); 83*88f0bb17SSean Christopherson 84*88f0bb17SSean Christopherson /* 85*88f0bb17SSean Christopherson * The 64-bit only MSRs that take an address always perform 86*88f0bb17SSean Christopherson * canonical checks on both Intel and AMD. 87*88f0bb17SSean Christopherson */ 88*88f0bb17SSean Christopherson if (msr_info[i].is_64bit_only && 89*88f0bb17SSean Christopherson msr_info[i].value == addr_64) 90*88f0bb17SSean Christopherson test_wrmsr_fault(&msr_info[i], NONCANONICAL); 91142ff635SSean Christopherson } else { 92142ff635SSean Christopherson test_wrmsr_fault(&msr_info[i], msr_info[i].value); 93142ff635SSean Christopherson test_rdmsr_fault(&msr_info[i]); 94142ff635SSean Christopherson } 95142ff635SSean Christopherson } 967d36db35SAvi Kivity 97f3cdd159SJan Kiszka return report_summary(); 987d36db35SAvi Kivity } 99