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 
25d31e1500SSean Christopherson static void test_cr4_feature_bit(struct kvm_vcpu *vcpu, struct kvm_sregs *orig,
267a873e45SSean Christopherson 				 uint64_t feature_bit)
277a873e45SSean Christopherson {
287a873e45SSean Christopherson 	struct kvm_sregs sregs;
297a873e45SSean Christopherson 	int rc;
307a873e45SSean Christopherson 
317a873e45SSean Christopherson 	/* Skip the sub-test, the feature is supported. */
327a873e45SSean Christopherson 	if (orig->cr4 & feature_bit)
337a873e45SSean Christopherson 		return;
347a873e45SSean Christopherson 
357a873e45SSean Christopherson 	memcpy(&sregs, orig, sizeof(sregs));
367a873e45SSean Christopherson 	sregs.cr4 |= feature_bit;
377a873e45SSean Christopherson 
38768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
397a873e45SSean Christopherson 	TEST_ASSERT(rc, "KVM allowed unsupported CR4 bit (0x%lx)", feature_bit);
407a873e45SSean Christopherson 
417a873e45SSean Christopherson 	/* Sanity check that KVM didn't change anything. */
42768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
437a873e45SSean Christopherson 	TEST_ASSERT(!memcmp(&sregs, orig, sizeof(sregs)), "KVM modified sregs");
447a873e45SSean Christopherson }
457a873e45SSean Christopherson 
46*61d76b8aSSean Christopherson static uint64_t calc_supported_cr4_feature_bits(void)
477a873e45SSean Christopherson {
487a873e45SSean Christopherson 	uint64_t cr4;
497a873e45SSean Christopherson 
507a873e45SSean Christopherson 	cr4 = X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE |
517a873e45SSean Christopherson 	      X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE | X86_CR4_PGE |
527a873e45SSean Christopherson 	      X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT;
53*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_UMIP))
547a873e45SSean Christopherson 		cr4 |= X86_CR4_UMIP;
55*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_LA57))
567a873e45SSean Christopherson 		cr4 |= X86_CR4_LA57;
57*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_VMX))
587a873e45SSean Christopherson 		cr4 |= X86_CR4_VMXE;
59*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMX))
607a873e45SSean Christopherson 		cr4 |= X86_CR4_SMXE;
61*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_FSGSBASE))
627a873e45SSean Christopherson 		cr4 |= X86_CR4_FSGSBASE;
63*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PCID))
647a873e45SSean Christopherson 		cr4 |= X86_CR4_PCIDE;
65*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_XSAVE))
667a873e45SSean Christopherson 		cr4 |= X86_CR4_OSXSAVE;
67*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMEP))
687a873e45SSean Christopherson 		cr4 |= X86_CR4_SMEP;
69*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_SMAP))
707a873e45SSean Christopherson 		cr4 |= X86_CR4_SMAP;
71*61d76b8aSSean Christopherson 	if (kvm_cpu_has(X86_FEATURE_PKU))
727a873e45SSean Christopherson 		cr4 |= X86_CR4_PKE;
737a873e45SSean Christopherson 
747a873e45SSean Christopherson 	return cr4;
757a873e45SSean Christopherson }
767a873e45SSean Christopherson 
77cc68765dSAndrew Jones int main(int argc, char *argv[])
78cc68765dSAndrew Jones {
79cc68765dSAndrew Jones 	struct kvm_sregs sregs;
80d31e1500SSean Christopherson 	struct kvm_vcpu *vcpu;
81cc68765dSAndrew Jones 	struct kvm_vm *vm;
827a873e45SSean Christopherson 	uint64_t cr4;
83cc68765dSAndrew Jones 	int rc;
84cc68765dSAndrew Jones 
85cc68765dSAndrew Jones 	/* Tell stdout not to buffer its content */
86cc68765dSAndrew Jones 	setbuf(stdout, NULL);
87cc68765dSAndrew Jones 
887a873e45SSean Christopherson 	/*
897a873e45SSean Christopherson 	 * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and
907a873e45SSean Christopherson 	 * use it to verify all supported CR4 bits can be set prior to defining
917a873e45SSean Christopherson 	 * the vCPU model, i.e. without doing KVM_SET_CPUID2.
927a873e45SSean Christopherson 	 */
9395fb0460SSean Christopherson 	vm = vm_create_barebones();
94f742d94fSSean Christopherson 	vcpu = __vm_vcpu_add(vm, 0);
957a873e45SSean Christopherson 
96768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
977a873e45SSean Christopherson 
98*61d76b8aSSean Christopherson 	sregs.cr4 |= calc_supported_cr4_feature_bits();
997a873e45SSean Christopherson 	cr4 = sregs.cr4;
1007a873e45SSean Christopherson 
101768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
1027a873e45SSean Christopherson 	TEST_ASSERT(!rc, "Failed to set supported CR4 bits (0x%lx)", cr4);
1037a873e45SSean Christopherson 
104768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
1057a873e45SSean Christopherson 	TEST_ASSERT(sregs.cr4 == cr4, "sregs.CR4 (0x%llx) != CR4 (0x%lx)",
1067a873e45SSean Christopherson 		    sregs.cr4, cr4);
1077a873e45SSean Christopherson 
1087a873e45SSean Christopherson 	/* Verify all unsupported features are rejected by KVM. */
109d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_UMIP);
110d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_LA57);
111d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_VMXE);
112d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_SMXE);
113d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_FSGSBASE);
114d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_PCIDE);
115d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_OSXSAVE);
116d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_SMEP);
117d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_SMAP);
118d31e1500SSean Christopherson 	test_cr4_feature_bit(vcpu, &sregs, X86_CR4_PKE);
1197a873e45SSean Christopherson 	kvm_vm_free(vm);
1207a873e45SSean Christopherson 
1217a873e45SSean Christopherson 	/* Create a "real" VM and verify APIC_BASE can be set. */
122d31e1500SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
123cc68765dSAndrew Jones 
124768e9a61SSean Christopherson 	vcpu_sregs_get(vcpu, &sregs);
125cc68765dSAndrew Jones 	sregs.apic_base = 1 << 10;
126768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
127cc68765dSAndrew Jones 	TEST_ASSERT(rc, "Set IA32_APIC_BASE to %llx (invalid)",
128cc68765dSAndrew Jones 		    sregs.apic_base);
129cc68765dSAndrew Jones 	sregs.apic_base = 1 << 11;
130768e9a61SSean Christopherson 	rc = _vcpu_sregs_set(vcpu, &sregs);
131cc68765dSAndrew Jones 	TEST_ASSERT(!rc, "Couldn't set IA32_APIC_BASE to %llx (valid)",
132cc68765dSAndrew Jones 		    sregs.apic_base);
133cc68765dSAndrew Jones 
134cc68765dSAndrew Jones 	kvm_vm_free(vm);
135cc68765dSAndrew Jones 
136cc68765dSAndrew Jones 	return 0;
137cc68765dSAndrew Jones }
138