xref: /kvm-unit-tests/x86/msr.c (revision ca85dda2671e88d34acfbca6de48a9ab32b1810d)
17d36db35SAvi Kivity /* msr tests */
27d36db35SAvi Kivity 
37d36db35SAvi Kivity #include "libcflat.h"
4850479e3SJason Wang #include "processor.h"
5d1bdd07cSAvi Kivity #include "msr.h"
65b4855b3SDaniele Ahmed #include <stdlib.h>
75b4855b3SDaniele Ahmed 
85b4855b3SDaniele Ahmed /**
95b4855b3SDaniele Ahmed  * This test allows two modes:
105b4855b3SDaniele Ahmed  * 1. Default: the `msr_info' array contains the default test configurations
115b4855b3SDaniele Ahmed  * 2. Custom: by providing command line arguments it is possible to test any MSR and value
125b4855b3SDaniele Ahmed  *	Parameters order:
135b4855b3SDaniele Ahmed  *		1. msr index as a base 16 number
145b4855b3SDaniele Ahmed  *		2. value as a base 16 number
155b4855b3SDaniele Ahmed  */
167d36db35SAvi Kivity 
177d36db35SAvi Kivity struct msr_info {
187d36db35SAvi Kivity 	int index;
19142ff635SSean Christopherson 	bool is_64bit_only;
20797d79a2SThomas Huth 	const char *name;
217d36db35SAvi Kivity 	unsigned long long value;
22bab19cadSPaolo Bonzini 	unsigned long long keep;
237d36db35SAvi Kivity };
247d36db35SAvi Kivity 
257d36db35SAvi Kivity 
267d36db35SAvi Kivity #define addr_64 0x0000123456789abcULL
278feb8cfbSSean Christopherson #define addr_ul (unsigned long)addr_64
287d36db35SAvi Kivity 
29*ca85dda2SSean Christopherson #define MSR_TEST(msr, val, ro)	\
30*ca85dda2SSean Christopherson 	{ .index = msr, .name = #msr, .value = val, .is_64bit_only = false, .keep = ro }
31*ca85dda2SSean Christopherson #define MSR_TEST_ONLY64(msr, val, ro)	\
32*ca85dda2SSean Christopherson 	{ .index = msr, .name = #msr, .value = val, .is_64bit_only = true, .keep = ro }
3364662079SSean Christopherson 
347d36db35SAvi Kivity struct msr_info msr_info[] =
357d36db35SAvi Kivity {
36*ca85dda2SSean Christopherson 	MSR_TEST(MSR_IA32_SYSENTER_CS, 0x1234, 0),
37*ca85dda2SSean Christopherson 	MSR_TEST(MSR_IA32_SYSENTER_ESP, addr_ul, 0),
38*ca85dda2SSean Christopherson 	MSR_TEST(MSR_IA32_SYSENTER_EIP, addr_ul, 0),
397d36db35SAvi Kivity 	// reserved: 1:2, 4:6, 8:10, 13:15, 17, 19:21, 24:33, 35:63
40bab19cadSPaolo Bonzini 	// read-only: 7, 11, 12
41*ca85dda2SSean Christopherson 	MSR_TEST(MSR_IA32_MISC_ENABLE, 0x400c50809, 0x1880),
42*ca85dda2SSean Christopherson 	MSR_TEST(MSR_IA32_CR_PAT, 0x07070707, 0),
43*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_FS_BASE, addr_64, 0),
44*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_GS_BASE, addr_64, 0),
45*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_KERNEL_GS_BASE, addr_64, 0),
46*ca85dda2SSean Christopherson 	MSR_TEST(MSR_EFER, EFER_SCE, 0),
47*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_LSTAR, addr_64, 0),
48*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_CSTAR, addr_64, 0),
49*ca85dda2SSean Christopherson 	MSR_TEST_ONLY64(MSR_SYSCALL_MASK, 0xffffffff, 0),
507d36db35SAvi Kivity //	MSR_IA32_DEBUGCTLMSR needs svm feature LBRV
517d36db35SAvi Kivity //	MSR_VM_HSAVE_PA only AMD host
527d36db35SAvi Kivity };
537d36db35SAvi Kivity 
543e788d91SSean Christopherson static void test_msr_rw(struct msr_info *msr, unsigned long long val)
557d36db35SAvi Kivity {
56a73d6ae4SSean Christopherson 	unsigned long long r, orig;
5750273266SSean Christopherson 
583e788d91SSean Christopherson 	orig = rdmsr(msr->index);
59dcae8d5fSSean Christopherson 	/*
60dcae8d5fSSean Christopherson 	 * Special case EFER since clearing LME/LMA is not allowed in 64-bit mode,
61dcae8d5fSSean Christopherson 	 * and conversely setting those bits on 32-bit CPUs is not allowed.  Treat
62dcae8d5fSSean Christopherson 	 * the desired value as extra bits to set.
63dcae8d5fSSean Christopherson 	 */
64dcae8d5fSSean Christopherson 	if (msr->index == MSR_EFER)
65dcae8d5fSSean Christopherson 		val |= orig;
66bab19cadSPaolo Bonzini 	else
67bab19cadSPaolo Bonzini 		val = (val & ~msr->keep) | (orig & msr->keep);
683e788d91SSean Christopherson 	wrmsr(msr->index, val);
693e788d91SSean Christopherson 	r = rdmsr(msr->index);
703e788d91SSean Christopherson 	wrmsr(msr->index, orig);
719295327cSSean Christopherson 	if (r != val) {
72d26193a0SRoman Bolshakov 		printf("testing %s: output = %#" PRIx32 ":%#" PRIx32
733e788d91SSean Christopherson 		       " expected = %#" PRIx32 ":%#" PRIx32 "\n", msr->name,
749295327cSSean Christopherson 		       (u32)(r >> 32), (u32)r, (u32)(val >> 32), (u32)val);
757d36db35SAvi Kivity 	}
763e788d91SSean Christopherson 	report(val == r, "%s", msr->name);
777d36db35SAvi Kivity }
787d36db35SAvi Kivity 
79142ff635SSean Christopherson static void test_wrmsr_fault(struct msr_info *msr, unsigned long long val)
80142ff635SSean Christopherson {
81142ff635SSean Christopherson 	unsigned char vector = wrmsr_checking(msr->index, val);
82142ff635SSean Christopherson 
83142ff635SSean Christopherson 	report(vector == GP_VECTOR,
84142ff635SSean Christopherson 	       "Expected #GP on WRSMR(%s, 0x%llx), got vector %d",
85142ff635SSean Christopherson 	       msr->name, val, vector);
86142ff635SSean Christopherson }
87142ff635SSean Christopherson 
88142ff635SSean Christopherson static void test_rdmsr_fault(struct msr_info *msr)
89142ff635SSean Christopherson {
90142ff635SSean Christopherson 	unsigned char vector = rdmsr_checking(msr->index);
91142ff635SSean Christopherson 
92142ff635SSean Christopherson 	report(vector == GP_VECTOR,
93142ff635SSean Christopherson 	       "Expected #GP on RDSMR(%s), got vector %d", msr->name, vector);
94142ff635SSean Christopherson }
95142ff635SSean Christopherson 
969e8ecb28SDaniele Ahmed static void test_msr(struct msr_info *msr, bool is_64bit_host)
979e8ecb28SDaniele Ahmed {
989e8ecb28SDaniele Ahmed 	if (is_64bit_host || !msr->is_64bit_only) {
999e8ecb28SDaniele Ahmed 		test_msr_rw(msr, msr->value);
1009e8ecb28SDaniele Ahmed 
1019e8ecb28SDaniele Ahmed 		/*
1029e8ecb28SDaniele Ahmed 		 * The 64-bit only MSRs that take an address always perform
1039e8ecb28SDaniele Ahmed 		 * canonical checks on both Intel and AMD.
1049e8ecb28SDaniele Ahmed 		 */
1059e8ecb28SDaniele Ahmed 		if (msr->is_64bit_only &&
1069e8ecb28SDaniele Ahmed 		    msr->value == addr_64)
1079e8ecb28SDaniele Ahmed 			test_wrmsr_fault(msr, NONCANONICAL);
1089e8ecb28SDaniele Ahmed 	} else {
1099e8ecb28SDaniele Ahmed 		test_wrmsr_fault(msr, msr->value);
1109e8ecb28SDaniele Ahmed 		test_rdmsr_fault(msr);
1119e8ecb28SDaniele Ahmed 	}
1129e8ecb28SDaniele Ahmed }
1139e8ecb28SDaniele Ahmed 
1147d36db35SAvi Kivity int main(int ac, char **av)
1157d36db35SAvi Kivity {
116142ff635SSean Christopherson 	bool is_64bit_host = this_cpu_has(X86_FEATURE_LM);
11764662079SSean Christopherson 	int i;
11864662079SSean Christopherson 
1195b4855b3SDaniele Ahmed 	if (ac == 3) {
1205b4855b3SDaniele Ahmed 		char msr_name[16];
1215b4855b3SDaniele Ahmed 		int index = strtoul(av[1], NULL, 0x10);
1225b4855b3SDaniele Ahmed 		snprintf(msr_name, sizeof(msr_name), "MSR:0x%x", index);
1235b4855b3SDaniele Ahmed 
1245b4855b3SDaniele Ahmed 		struct msr_info msr = {
1255b4855b3SDaniele Ahmed 			.index = index,
1265b4855b3SDaniele Ahmed 			.name = msr_name,
1275b4855b3SDaniele Ahmed 			.value = strtoull(av[2], NULL, 0x10)
1285b4855b3SDaniele Ahmed 		};
1295b4855b3SDaniele Ahmed 		test_msr(&msr, is_64bit_host);
1305b4855b3SDaniele Ahmed 	} else {
131142ff635SSean Christopherson 		for (i = 0 ; i < ARRAY_SIZE(msr_info); i++) {
1329e8ecb28SDaniele Ahmed 			test_msr(&msr_info[i], is_64bit_host);
133142ff635SSean Christopherson 		}
1345b4855b3SDaniele Ahmed 	}
1357d36db35SAvi Kivity 
136f3cdd159SJan Kiszka 	return report_summary();
1377d36db35SAvi Kivity }
138