xref: /linux/tools/testing/selftests/kvm/x86_64/xss_msr_test.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019, Google LLC.
4  *
5  * Tests for the IA32_XSS MSR.
6  */
7 
8 #define _GNU_SOURCE /* for program_invocation_short_name */
9 #include <sys/ioctl.h>
10 
11 #include "test_util.h"
12 #include "kvm_util.h"
13 #include "vmx.h"
14 
15 #define MSR_BITS      64
16 
17 int main(int argc, char *argv[])
18 {
19 	bool xss_in_msr_list;
20 	struct kvm_vm *vm;
21 	struct kvm_vcpu *vcpu;
22 	uint64_t xss_val;
23 	int i, r;
24 
25 	/* Create VM */
26 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
27 
28 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVES));
29 
30 	xss_val = vcpu_get_msr(vcpu, MSR_IA32_XSS);
31 	TEST_ASSERT(xss_val == 0,
32 		    "MSR_IA32_XSS should be initialized to zero\n");
33 
34 	vcpu_set_msr(vcpu, MSR_IA32_XSS, xss_val);
35 
36 	/*
37 	 * At present, KVM only supports a guest IA32_XSS value of 0. Verify
38 	 * that trying to set the guest IA32_XSS to an unsupported value fails.
39 	 * Also, in the future when a non-zero value succeeds check that
40 	 * IA32_XSS is in the list of MSRs to save/restore.
41 	 */
42 	xss_in_msr_list = kvm_msr_is_in_save_restore_list(MSR_IA32_XSS);
43 	for (i = 0; i < MSR_BITS; ++i) {
44 		r = _vcpu_set_msr(vcpu, MSR_IA32_XSS, 1ull << i);
45 
46 		/*
47 		 * Setting a list of MSRs returns the entry that "faulted", or
48 		 * the last entry +1 if all MSRs were successfully written.
49 		 */
50 		TEST_ASSERT(!r || r == 1, KVM_IOCTL_ERROR(KVM_SET_MSRS, r));
51 		TEST_ASSERT(r != 1 || xss_in_msr_list,
52 			    "IA32_XSS was able to be set, but was not in save/restore list");
53 	}
54 
55 	kvm_vm_free(vm);
56 }
57