| // SPDX-License-Identifier: GPL-2.0-only | |
| /* | |
| * KVM_SET_SREGS tests | |
| * | |
| * Copyright (C) 2018, Google LLC. | |
| * | |
| * This is a regression test for the bug fixed by the following commit: | |
| * d3802286fa0f ("kvm: x86: Disallow illegal IA32_APIC_BASE MSR values") | |
| * | |
| * That bug allowed a user-mode program that called the KVM_SET_SREGS | |
| * ioctl to put a VCPU's local APIC into an invalid state. | |
| */ | |
| #include <fcntl.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <sys/ioctl.h> | |
| #include "test_util.h" | |
| #include "kvm_util.h" | |
| #include "processor.h" | |
| #define TEST_INVALID_SREG_BIT(vcpu, reg, orig, bit) \ | |
| do { \ | |
| struct kvm_sregs new; \ | |
| int rc; \ | |
| \ | |
| /* Skip the sub-test, the feature/bit is supported. */ \ | |
| if (orig.reg & bit) \ | |
| break; \ | |
| \ | |
| memcpy(&new, &orig, sizeof(new)); \ | |
| new.reg |= bit; \ | |
| \ | |
| rc = _vcpu_sregs_set(vcpu, &new); \ | |
| TEST_ASSERT(rc, "KVM allowed invalid " #reg " bit (0x%lx)", (u64)bit); \ | |
| \ | |
| /* Sanity check that KVM didn't change anything. */ \ | |
| vcpu_sregs_get(vcpu, &new); \ | |
| TEST_ASSERT(!memcmp(&new, &orig, sizeof(new)), "KVM modified sregs"); \ | |
| } while (0) | |
| #define KVM_ALWAYS_ALLOWED_CR4 (X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | \ | |
| X86_CR4_DE | X86_CR4_PSE | X86_CR4_PAE | \ | |
| X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE | \ | |
| X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT) | |
| #define KVM_ALWAYS_ALLOWED_EFER EFER_SCE | |
| static u64 calc_supported_cr4_feature_bits(void) | |
| { | |
| u64 cr4 = KVM_ALWAYS_ALLOWED_CR4; | |
| if (kvm_cpu_has(X86_FEATURE_UMIP)) | |
| cr4 |= X86_CR4_UMIP; | |
| if (kvm_cpu_has(X86_FEATURE_LA57)) | |
| cr4 |= X86_CR4_LA57; | |
| if (kvm_cpu_has(X86_FEATURE_VMX)) | |
| cr4 |= X86_CR4_VMXE; | |
| if (kvm_cpu_has(X86_FEATURE_SMX)) | |
| cr4 |= X86_CR4_SMXE; | |
| if (kvm_cpu_has(X86_FEATURE_FSGSBASE)) | |
| cr4 |= X86_CR4_FSGSBASE; | |
| if (kvm_cpu_has(X86_FEATURE_PCID)) | |
| cr4 |= X86_CR4_PCIDE; | |
| if (kvm_cpu_has(X86_FEATURE_XSAVE)) | |
| cr4 |= X86_CR4_OSXSAVE; | |
| if (kvm_cpu_has(X86_FEATURE_SMEP)) | |
| cr4 |= X86_CR4_SMEP; | |
| if (kvm_cpu_has(X86_FEATURE_SMAP)) | |
| cr4 |= X86_CR4_SMAP; | |
| if (kvm_cpu_has(X86_FEATURE_PKU)) | |
| cr4 |= X86_CR4_PKE; | |
| return cr4; | |
| } | |
| static u64 calc_supported_efer_feature_bits(void) | |
| { | |
| u64 efer = KVM_ALWAYS_ALLOWED_EFER; | |
| if (kvm_cpu_has(X86_FEATURE_LM)) | |
| efer |= (EFER_LME | EFER_LMA); | |
| if (kvm_cpu_has(X86_FEATURE_NX)) | |
| efer |= EFER_NX; | |
| if (kvm_cpu_has(X86_FEATURE_SVM)) | |
| efer |= EFER_SVME; | |
| if (kvm_cpu_has(X86_FEATURE_FXSR_OPT)) | |
| efer |= EFER_FFXSR; | |
| if (kvm_cpu_has(X86_FEATURE_AUTOIBRS)) | |
| efer |= EFER_AUTOIBRS; | |
| return efer; | |
| } | |
| static void test_cr_bits(struct kvm_vcpu *vcpu, u64 cr4) | |
| { | |
| struct kvm_sregs sregs; | |
| int rc, i; | |
| vcpu_sregs_get(vcpu, &sregs); | |
| sregs.cr0 &= ~(X86_CR0_CD | X86_CR0_NW); | |
| sregs.cr4 |= cr4; | |
| rc = _vcpu_sregs_set(vcpu, &sregs); | |
| TEST_ASSERT(!rc, "Failed to set supported CR4 bits (0x%lx)", cr4); | |
| TEST_ASSERT(!!(sregs.cr4 & X86_CR4_OSXSAVE) == | |
| (vcpu->cpuid && vcpu_cpuid_has(vcpu, X86_FEATURE_OSXSAVE)), | |
| "KVM didn't %s OSXSAVE in CPUID as expected", | |
| (sregs.cr4 & X86_CR4_OSXSAVE) ? "set" : "clear"); | |
| TEST_ASSERT(!!(sregs.cr4 & X86_CR4_PKE) == | |
| (vcpu->cpuid && vcpu_cpuid_has(vcpu, X86_FEATURE_OSPKE)), | |
| "KVM didn't %s OSPKE in CPUID as expected", | |
| (sregs.cr4 & X86_CR4_PKE) ? "set" : "clear"); | |
| vcpu_sregs_get(vcpu, &sregs); | |
| TEST_ASSERT_EQ(sregs.cr4, cr4); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_UMIP); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_LA57); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_VMXE); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_SMXE); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_FSGSBASE); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_PCIDE); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_OSXSAVE); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_SMEP); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_SMAP); | |
| TEST_INVALID_SREG_BIT(vcpu, cr4, sregs, X86_CR4_PKE); | |
| for (i = 32; i < 64; i++) | |
| TEST_INVALID_SREG_BIT(vcpu, cr0, sregs, BIT(i)); | |
| /* NW without CD is illegal, as is PG without PE. */ | |
| TEST_INVALID_SREG_BIT(vcpu, cr0, sregs, X86_CR0_NW); | |
| TEST_INVALID_SREG_BIT(vcpu, cr0, sregs, X86_CR0_PG); | |
| /* CR8 bits 3:0 are writable; bits 63:4 are reserved. */ | |
| vcpu_sregs_get(vcpu, &sregs); | |
| sregs.cr8 = 0xf; | |
| vcpu_sregs_set(vcpu, &sregs); | |
| vcpu_sregs_get(vcpu, &sregs); | |
| TEST_ASSERT_EQ(sregs.cr8, 0xf); | |
| for (i = 4; i < 64; i++) | |
| TEST_INVALID_SREG_BIT(vcpu, cr8, sregs, BIT_ULL(i)); | |
| } | |
| static void test_efer_bits(struct kvm_vcpu *vcpu, u64 efer) | |
| { | |
| struct kvm_sregs sregs; | |
| int rc; | |
| vcpu_sregs_get(vcpu, &sregs); | |
| sregs.efer |= efer; | |
| rc = _vcpu_sregs_set(vcpu, &sregs); | |
| TEST_ASSERT(!rc, "Failed to set supported EFER bits (0x%llx)", sregs.efer); | |
| vcpu_sregs_get(vcpu, &sregs); | |
| TEST_ASSERT_EQ(sregs.efer, efer); | |
| TEST_INVALID_SREG_BIT(vcpu, efer, sregs, EFER_LME); | |
| TEST_INVALID_SREG_BIT(vcpu, efer, sregs, EFER_NX); | |
| TEST_INVALID_SREG_BIT(vcpu, efer, sregs, EFER_SVME); | |
| TEST_INVALID_SREG_BIT(vcpu, efer, sregs, EFER_FFXSR); | |
| TEST_INVALID_SREG_BIT(vcpu, efer, sregs, EFER_AUTOIBRS); | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| struct kvm_sregs sregs; | |
| struct kvm_vcpu *vcpu; | |
| struct kvm_vm *vm; | |
| int rc; | |
| /* | |
| * Create a dummy VM, specifically to avoid doing KVM_SET_CPUID2, and | |
| * use it to verify KVM enforces guest CPUID even if *userspace* never | |
| * sets CPUID. | |
| */ | |
| vm = vm_create_barebones(); | |
| vcpu = __vm_vcpu_add(vm, 0); | |
| test_efer_bits(vcpu, KVM_ALWAYS_ALLOWED_EFER); | |
| test_cr_bits(vcpu, KVM_ALWAYS_ALLOWED_CR4); | |
| kvm_vm_free(vm); | |
| /* Create a "real" VM with a fully populated guest CPUID and verify | |
| * APIC_BASE and all supported CR4 can be set. | |
| */ | |
| vm = vm_create_with_one_vcpu(&vcpu, NULL); | |
| vcpu_sregs_get(vcpu, &sregs); | |
| sregs.apic_base = 1 << 10; | |
| rc = _vcpu_sregs_set(vcpu, &sregs); | |
| TEST_ASSERT(rc, "Set IA32_APIC_BASE to %llx (invalid)", | |
| sregs.apic_base); | |
| sregs.apic_base = 1 << 11; | |
| rc = _vcpu_sregs_set(vcpu, &sregs); | |
| TEST_ASSERT(!rc, "Couldn't set IA32_APIC_BASE to %llx (valid)", | |
| sregs.apic_base); | |
| test_cr_bits(vcpu, calc_supported_cr4_feature_bits()); | |
| test_efer_bits(vcpu, calc_supported_efer_feature_bits()); | |
| kvm_vm_free(vm); | |
| return 0; | |
| } |