xref: /linux/tools/testing/selftests/kvm/x86_64/set_sregs_test.c (revision f4b0c4b508364fde023e4f7b9f23f7e38c663dfe)
17a338472SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cc68765dSAndrew Jones /*
3cc68765dSAndrew Jones  * KVM_SET_SREGS tests
4cc68765dSAndrew Jones  *
5cc68765dSAndrew Jones  * Copyright (C) 2018, Google LLC.
6cc68765dSAndrew Jones  *
7cc68765dSAndrew Jones  * This is a regression test for the bug fixed by the following commit:
8cc68765dSAndrew Jones  * d3802286fa0f ("kvm: x86: Disallow illegal IA32_APIC_BASE MSR values")
9cc68765dSAndrew Jones  *
10cc68765dSAndrew Jones  * That bug allowed a user-mode program that called the KVM_SET_SREGS
11cc68765dSAndrew Jones  * ioctl to put a VCPU's local APIC into an invalid state.
12cc68765dSAndrew Jones  */
13cc68765dSAndrew Jones #include <fcntl.h>
14cc68765dSAndrew Jones #include <stdio.h>
15cc68765dSAndrew Jones #include <stdlib.h>
16cc68765dSAndrew Jones #include <string.h>
17cc68765dSAndrew Jones #include <sys/ioctl.h>
18cc68765dSAndrew Jones 
19cc68765dSAndrew Jones #include "test_util.h"
20cc68765dSAndrew Jones 
21cc68765dSAndrew Jones #include "kvm_util.h"
22cc68765dSAndrew Jones #include "processor.h"
23cc68765dSAndrew Jones 
24*5a759117SSean Christopherson #define TEST_INVALID_CR_BIT(vcpu, cr, orig, bit)				\
25*5a759117SSean Christopherson do {										\
26*5a759117SSean Christopherson 	struct kvm_sregs new;							\
27*5a759117SSean Christopherson 	int rc;									\
28*5a759117SSean Christopherson 										\
29*5a759117SSean Christopherson 	/* Skip the sub-test, the feature/bit is supported. */			\
30*5a759117SSean Christopherson 	if (orig.cr & bit)							\
31*5a759117SSean Christopherson 		break;								\
32*5a759117SSean Christopherson 										\
33*5a759117SSean Christopherson 	memcpy(&new, &orig, sizeof(sregs));					\
34*5a759117SSean Christopherson 	new.cr |= bit;								\
35*5a759117SSean Christopherson 										\
36*5a759117SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &new);					\
37*5a759117SSean Christopherson 	TEST_ASSERT(rc, "KVM allowed invalid " #cr " bit (0x%lx)", bit);	\
38*5a759117SSean Christopherson 										\
39*5a759117SSean Christopherson 	/* Sanity check that KVM didn't change anything. */			\
40*5a759117SSean Christopherson 	vcpu_sregs_get(vcpu, &new);						\
41*5a759117SSean Christopherson 	TEST_ASSERT(!memcmp(&new, &orig, sizeof(new)), "KVM modified sregs");	\
42*5a759117SSean Christopherson } while (0)
437a873e45SSean Christopherson 
calc_supported_cr4_feature_bits(void)4461d76b8aSSean Christopherson static uint64_t calc_supported_cr4_feature_bits(void)
457a873e45SSean Christopherson {
467a873e45SSean Christopherson 	uint64_t cr4;
477a873e45SSean Christopherson 
487a873e45SSean Christopherson 	cr4 = X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE |
497a873e45SSean Christopherson 	      X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE | X86_CR4_PGE |
507a873e45SSean Christopherson 	      X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT;
5161d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_UMIP))
527a873e45SSean Christopherson 		cr4 |= X86_CR4_UMIP;
5361d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_LA57))
547a873e45SSean Christopherson 		cr4 |= X86_CR4_LA57;
5561d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_VMX))
567a873e45SSean Christopherson 		cr4 |= X86_CR4_VMXE;
5761d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMX))
587a873e45SSean Christopherson 		cr4 |= X86_CR4_SMXE;
5961d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_FSGSBASE))
607a873e45SSean Christopherson 		cr4 |= X86_CR4_FSGSBASE;
6161d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PCID))
627a873e45SSean Christopherson 		cr4 |= X86_CR4_PCIDE;
6361d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_XSAVE))
647a873e45SSean Christopherson 		cr4 |= X86_CR4_OSXSAVE;
6561d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMEP))
667a873e45SSean Christopherson 		cr4 |= X86_CR4_SMEP;
6761d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMAP))
687a873e45SSean Christopherson 		cr4 |= X86_CR4_SMAP;
6961d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PKU))
707a873e45SSean Christopherson 		cr4 |= X86_CR4_PKE;
717a873e45SSean Christopherson 
727a873e45SSean Christopherson 	return cr4;
737a873e45SSean Christopherson }
747a873e45SSean Christopherson 
main(int argc,char * argv[])75cc68765dSAndrew Jones int main(int argc, char *argv[])
76cc68765dSAndrew Jones {
77cc68765dSAndrew Jones 	struct kvm_sregs sregs;
78d31e1500SSean Christopherson 	struct kvm_vcpu *vcpu;
79cc68765dSAndrew Jones 	struct kvm_vm *vm;
807a873e45SSean Christopherson 	uint64_t cr4;
81*5a759117SSean Christopherson 	int rc, i;
82cc68765dSAndrew Jones 
837a873e45SSean Christopherson 	/*
847a873e45SSean Christopherson 	 * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and
857a873e45SSean Christopherson 	 * use it to verify all supported CR4 bits can be set prior to defining
867a873e45SSean Christopherson 	 * the vCPU model, i.e. without doing KVM_SET_CPUID2.
877a873e45SSean Christopherson 	 */
8895fb0460SSean Christopherson 	vm = vm_create_barebones();
89f742d94fSSean Christopherson 	vcpu = __vm_vcpu_add(vm, 0);
907a873e45SSean Christopherson 
91768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
927a873e45SSean Christopherson 
93*5a759117SSean Christopherson 	sregs.cr0 = 0;
9461d76b8aSSean Christopherson 	sregs.cr4 |= calc_supported_cr4_feature_bits();
957a873e45SSean Christopherson 	cr4 = sregs.cr4;
967a873e45SSean Christopherson 
97768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
987a873e45SSean Christopherson 	TEST_ASSERT(!rc, "Failed to set supported CR4 bits (0x%lx)", cr4);
997a873e45SSean Christopherson 
100768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
1017a873e45SSean Christopherson 	TEST_ASSERT(sregs.cr4 == cr4, "sregs.CR4 (0x%llx) != CR4 (0x%lx)",
1027a873e45SSean Christopherson 		    sregs.cr4, cr4);
1037a873e45SSean Christopherson 
1047a873e45SSean Christopherson 	/* Verify all unsupported features are rejected by KVM. */
105*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_UMIP);
106*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_LA57);
107*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_VMXE);
108*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMXE);
109*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_FSGSBASE);
110*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_PCIDE);
111*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_OSXSAVE);
112*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMEP);
113*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMAP);
114*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_PKE);
115*5a759117SSean Christopherson 
116*5a759117SSean Christopherson 	for (i = 32; i < 64; i++)
117*5a759117SSean Christopherson 		TEST_INVALID_CR_BIT(vcpu, cr0, sregs, BIT(i));
118*5a759117SSean Christopherson 
119*5a759117SSean Christopherson 	/* NW without CD is illegal, as is PG without PE. */
120*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr0, sregs, X86_CR0_NW);
121*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr0, sregs, X86_CR0_PG);
122*5a759117SSean Christopherson 
1237a873e45SSean Christopherson 	kvm_vm_free(vm);
1247a873e45SSean Christopherson 
1257a873e45SSean Christopherson 	/* Create a "real" VM and verify APIC_BASE can be set. */
126d31e1500SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
127cc68765dSAndrew Jones 
128768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
129cc68765dSAndrew Jones 	sregs.apic_base = 1 << 10;
130768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
131cc68765dSAndrew Jones 	TEST_ASSERT(rc, "Set IA32_APIC_BASE to %llx (invalid)",
132cc68765dSAndrew Jones 		    sregs.apic_base);
133cc68765dSAndrew Jones 	sregs.apic_base = 1 << 11;
134768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
135cc68765dSAndrew Jones 	TEST_ASSERT(!rc, "Couldn't set IA32_APIC_BASE to %llx (valid)",
136cc68765dSAndrew Jones 		    sregs.apic_base);
137cc68765dSAndrew Jones 
138cc68765dSAndrew Jones 	kvm_vm_free(vm);
139cc68765dSAndrew Jones 
140cc68765dSAndrew Jones 	return 0;
141cc68765dSAndrew Jones }
142