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 #define _GNU_SOURCE /* for program_invocation_short_name */
14cc68765dSAndrew Jones #include <fcntl.h>
15cc68765dSAndrew Jones #include <stdio.h>
16cc68765dSAndrew Jones #include <stdlib.h>
17cc68765dSAndrew Jones #include <string.h>
18cc68765dSAndrew Jones #include <sys/ioctl.h>
19cc68765dSAndrew Jones 
20cc68765dSAndrew Jones #include "test_util.h"
21cc68765dSAndrew Jones 
22cc68765dSAndrew Jones #include "kvm_util.h"
23cc68765dSAndrew Jones #include "processor.h"
24cc68765dSAndrew Jones 
25*5a759117SSean Christopherson #define TEST_INVALID_CR_BIT(vcpu, cr, orig, bit)				\
26*5a759117SSean Christopherson do {										\
27*5a759117SSean Christopherson 	struct kvm_sregs new;							\
28*5a759117SSean Christopherson 	int rc;									\
29*5a759117SSean Christopherson 										\
30*5a759117SSean Christopherson 	/* Skip the sub-test, the feature/bit is supported. */			\
31*5a759117SSean Christopherson 	if (orig.cr & bit)							\
32*5a759117SSean Christopherson 		break;								\
33*5a759117SSean Christopherson 										\
34*5a759117SSean Christopherson 	memcpy(&new, &orig, sizeof(sregs));					\
35*5a759117SSean Christopherson 	new.cr |= bit;								\
36*5a759117SSean Christopherson 										\
37*5a759117SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &new);					\
38*5a759117SSean Christopherson 	TEST_ASSERT(rc, "KVM allowed invalid " #cr " bit (0x%lx)", bit);	\
39*5a759117SSean Christopherson 										\
40*5a759117SSean Christopherson 	/* Sanity check that KVM didn't change anything. */			\
41*5a759117SSean Christopherson 	vcpu_sregs_get(vcpu, &new);						\
42*5a759117SSean Christopherson 	TEST_ASSERT(!memcmp(&new, &orig, sizeof(new)), "KVM modified sregs");	\
43*5a759117SSean Christopherson } while (0)
447a873e45SSean Christopherson 
calc_supported_cr4_feature_bits(void)4561d76b8aSSean Christopherson static uint64_t calc_supported_cr4_feature_bits(void)
467a873e45SSean Christopherson {
477a873e45SSean Christopherson 	uint64_t cr4;
487a873e45SSean Christopherson 
497a873e45SSean Christopherson 	cr4 = X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE |
507a873e45SSean Christopherson 	      X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE | X86_CR4_PGE |
517a873e45SSean Christopherson 	      X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT;
5261d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_UMIP))
537a873e45SSean Christopherson 		cr4 |= X86_CR4_UMIP;
5461d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_LA57))
557a873e45SSean Christopherson 		cr4 |= X86_CR4_LA57;
5661d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_VMX))
577a873e45SSean Christopherson 		cr4 |= X86_CR4_VMXE;
5861d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMX))
597a873e45SSean Christopherson 		cr4 |= X86_CR4_SMXE;
6061d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_FSGSBASE))
617a873e45SSean Christopherson 		cr4 |= X86_CR4_FSGSBASE;
6261d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PCID))
637a873e45SSean Christopherson 		cr4 |= X86_CR4_PCIDE;
6461d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_XSAVE))
657a873e45SSean Christopherson 		cr4 |= X86_CR4_OSXSAVE;
6661d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMEP))
677a873e45SSean Christopherson 		cr4 |= X86_CR4_SMEP;
6861d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMAP))
697a873e45SSean Christopherson 		cr4 |= X86_CR4_SMAP;
7061d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PKU))
717a873e45SSean Christopherson 		cr4 |= X86_CR4_PKE;
727a873e45SSean Christopherson 
737a873e45SSean Christopherson 	return cr4;
747a873e45SSean Christopherson }
757a873e45SSean Christopherson 
main(int argc,char * argv[])76cc68765dSAndrew Jones int main(int argc, char *argv[])
77cc68765dSAndrew Jones {
78cc68765dSAndrew Jones 	struct kvm_sregs sregs;
79d31e1500SSean Christopherson 	struct kvm_vcpu *vcpu;
80cc68765dSAndrew Jones 	struct kvm_vm *vm;
817a873e45SSean Christopherson 	uint64_t cr4;
82*5a759117SSean Christopherson 	int rc, i;
83cc68765dSAndrew Jones 
847a873e45SSean Christopherson 	/*
857a873e45SSean Christopherson 	 * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and
867a873e45SSean Christopherson 	 * use it to verify all supported CR4 bits can be set prior to defining
877a873e45SSean Christopherson 	 * the vCPU model, i.e. without doing KVM_SET_CPUID2.
887a873e45SSean Christopherson 	 */
8995fb0460SSean Christopherson 	vm = vm_create_barebones();
90f742d94fSSean Christopherson 	vcpu = __vm_vcpu_add(vm, 0);
917a873e45SSean Christopherson 
92768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
937a873e45SSean Christopherson 
94*5a759117SSean Christopherson 	sregs.cr0 = 0;
9561d76b8aSSean Christopherson 	sregs.cr4 |= calc_supported_cr4_feature_bits();
967a873e45SSean Christopherson 	cr4 = sregs.cr4;
977a873e45SSean Christopherson 
98768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
997a873e45SSean Christopherson 	TEST_ASSERT(!rc, "Failed to set supported CR4 bits (0x%lx)", cr4);
1007a873e45SSean Christopherson 
101768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
1027a873e45SSean Christopherson 	TEST_ASSERT(sregs.cr4 == cr4, "sregs.CR4 (0x%llx) != CR4 (0x%lx)",
1037a873e45SSean Christopherson 		    sregs.cr4, cr4);
1047a873e45SSean Christopherson 
1057a873e45SSean Christopherson 	/* Verify all unsupported features are rejected by KVM. */
106*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_UMIP);
107*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_LA57);
108*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_VMXE);
109*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMXE);
110*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_FSGSBASE);
111*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_PCIDE);
112*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_OSXSAVE);
113*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMEP);
114*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_SMAP);
115*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr4, sregs, X86_CR4_PKE);
116*5a759117SSean Christopherson 
117*5a759117SSean Christopherson 	for (i = 32; i < 64; i++)
118*5a759117SSean Christopherson 		TEST_INVALID_CR_BIT(vcpu, cr0, sregs, BIT(i));
119*5a759117SSean Christopherson 
120*5a759117SSean Christopherson 	/* NW without CD is illegal, as is PG without PE. */
121*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr0, sregs, X86_CR0_NW);
122*5a759117SSean Christopherson 	TEST_INVALID_CR_BIT(vcpu, cr0, sregs, X86_CR0_PG);
123*5a759117SSean Christopherson 
1247a873e45SSean Christopherson 	kvm_vm_free(vm);
1257a873e45SSean Christopherson 
1267a873e45SSean Christopherson 	/* Create a "real" VM and verify APIC_BASE can be set. */
127d31e1500SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
128cc68765dSAndrew Jones 
129768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
130cc68765dSAndrew Jones 	sregs.apic_base = 1 << 10;
131768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
132cc68765dSAndrew Jones 	TEST_ASSERT(rc, "Set IA32_APIC_BASE to %llx (invalid)",
133cc68765dSAndrew Jones 		    sregs.apic_base);
134cc68765dSAndrew Jones 	sregs.apic_base = 1 << 11;
135768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
136cc68765dSAndrew Jones 	TEST_ASSERT(!rc, "Couldn't set IA32_APIC_BASE to %llx (valid)",
137cc68765dSAndrew Jones 		    sregs.apic_base);
138cc68765dSAndrew Jones 
139cc68765dSAndrew Jones 	kvm_vm_free(vm);
140cc68765dSAndrew Jones 
141cc68765dSAndrew Jones 	return 0;
142cc68765dSAndrew Jones }
143