| /* msr tests */ |
| |
| #include "libcflat.h" |
| #include "processor.h" |
| #include "msr.h" |
| #include <stdlib.h> |
| |
| /** |
| * This test allows two modes: |
| * 1. Default: the `msr_info' array contains the default test configurations |
| * 2. Custom: by providing command line arguments it is possible to test any MSR and value |
| * Parameters order: |
| * 1. msr index as a base 16 number |
| * 2. value as a base 16 number |
| */ |
| |
| struct msr_info { |
| int index; |
| bool is_64bit_only; |
| const char *name; |
| unsigned long long value; |
| }; |
| |
| |
| #define addr_64 0x0000123456789abcULL |
| #define addr_ul (unsigned long)addr_64 |
| |
| #define MSR_TEST(msr, val, only64) \ |
| { .index = msr, .name = #msr, .value = val, .is_64bit_only = only64 } |
| |
| struct msr_info msr_info[] = |
| { |
| MSR_TEST(MSR_IA32_SYSENTER_CS, 0x1234, false), |
| MSR_TEST(MSR_IA32_SYSENTER_ESP, addr_ul, false), |
| MSR_TEST(MSR_IA32_SYSENTER_EIP, addr_ul, false), |
| // reserved: 1:2, 4:6, 8:10, 13:15, 17, 19:21, 24:33, 35:63 |
| MSR_TEST(MSR_IA32_MISC_ENABLE, 0x400c51889, false), |
| MSR_TEST(MSR_IA32_CR_PAT, 0x07070707, false), |
| MSR_TEST(MSR_FS_BASE, addr_64, true), |
| MSR_TEST(MSR_GS_BASE, addr_64, true), |
| MSR_TEST(MSR_KERNEL_GS_BASE, addr_64, true), |
| MSR_TEST(MSR_EFER, EFER_SCE, false), |
| MSR_TEST(MSR_LSTAR, addr_64, true), |
| MSR_TEST(MSR_CSTAR, addr_64, true), |
| MSR_TEST(MSR_SYSCALL_MASK, 0xffffffff, true), |
| // MSR_IA32_DEBUGCTLMSR needs svm feature LBRV |
| // MSR_VM_HSAVE_PA only AMD host |
| }; |
| |
| static void test_msr_rw(struct msr_info *msr, unsigned long long val) |
| { |
| unsigned long long r, orig; |
| |
| orig = rdmsr(msr->index); |
| /* |
| * Special case EFER since clearing LME/LMA is not allowed in 64-bit mode, |
| * and conversely setting those bits on 32-bit CPUs is not allowed. Treat |
| * the desired value as extra bits to set. |
| */ |
| if (msr->index == MSR_EFER) |
| val |= orig; |
| wrmsr(msr->index, val); |
| r = rdmsr(msr->index); |
| wrmsr(msr->index, orig); |
| if (r != val) { |
| printf("testing %s: output = %#" PRIx32 ":%#" PRIx32 |
| " expected = %#" PRIx32 ":%#" PRIx32 "\n", msr->name, |
| (u32)(r >> 32), (u32)r, (u32)(val >> 32), (u32)val); |
| } |
| report(val == r, "%s", msr->name); |
| } |
| |
| static void test_wrmsr_fault(struct msr_info *msr, unsigned long long val) |
| { |
| unsigned char vector = wrmsr_checking(msr->index, val); |
| |
| report(vector == GP_VECTOR, |
| "Expected #GP on WRSMR(%s, 0x%llx), got vector %d", |
| msr->name, val, vector); |
| } |
| |
| static void test_rdmsr_fault(struct msr_info *msr) |
| { |
| unsigned char vector = rdmsr_checking(msr->index); |
| |
| report(vector == GP_VECTOR, |
| "Expected #GP on RDSMR(%s), got vector %d", msr->name, vector); |
| } |
| |
| static void test_msr(struct msr_info *msr, bool is_64bit_host) |
| { |
| if (is_64bit_host || !msr->is_64bit_only) { |
| test_msr_rw(msr, msr->value); |
| |
| /* |
| * The 64-bit only MSRs that take an address always perform |
| * canonical checks on both Intel and AMD. |
| */ |
| if (msr->is_64bit_only && |
| msr->value == addr_64) |
| test_wrmsr_fault(msr, NONCANONICAL); |
| } else { |
| test_wrmsr_fault(msr, msr->value); |
| test_rdmsr_fault(msr); |
| } |
| } |
| |
| int main(int ac, char **av) |
| { |
| bool is_64bit_host = this_cpu_has(X86_FEATURE_LM); |
| int i; |
| |
| if (ac == 3) { |
| char msr_name[16]; |
| int index = strtoul(av[1], NULL, 0x10); |
| snprintf(msr_name, sizeof(msr_name), "MSR:0x%x", index); |
| |
| struct msr_info msr = { |
| .index = index, |
| .name = msr_name, |
| .value = strtoull(av[2], NULL, 0x10) |
| }; |
| test_msr(&msr, is_64bit_host); |
| } else { |
| for (i = 0 ; i < ARRAY_SIZE(msr_info); i++) { |
| test_msr(&msr_info[i], is_64bit_host); |
| } |
| } |
| |
| return report_summary(); |
| } |