1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * VMX control MSR test 4 * 5 * Copyright (C) 2022 Google LLC. 6 * 7 * Tests for KVM ownership of bits in the VMX entry/exit control MSRs. Checks 8 * that KVM will set owned bits where appropriate, and will not if 9 * KVM_X86_QUIRK_TWEAK_VMX_CTRL_MSRS is disabled. 10 */ 11 #include <linux/bitmap.h> 12 #include "kvm_util.h" 13 #include "vmx.h" 14 15 static void vmx_fixed1_msr_test(struct kvm_vcpu *vcpu, uint32_t msr_index, 16 uint64_t mask) 17 { 18 uint64_t val = vcpu_get_msr(vcpu, msr_index); 19 uint64_t bit; 20 21 mask &= val; 22 23 for_each_set_bit(bit, &mask, 64) { 24 vcpu_set_msr(vcpu, msr_index, val & ~BIT_ULL(bit)); 25 vcpu_set_msr(vcpu, msr_index, val); 26 } 27 } 28 29 static void vmx_fixed0_msr_test(struct kvm_vcpu *vcpu, uint32_t msr_index, 30 uint64_t mask) 31 { 32 uint64_t val = vcpu_get_msr(vcpu, msr_index); 33 uint64_t bit; 34 35 mask = ~mask | val; 36 37 for_each_clear_bit(bit, &mask, 64) { 38 vcpu_set_msr(vcpu, msr_index, val | BIT_ULL(bit)); 39 vcpu_set_msr(vcpu, msr_index, val); 40 } 41 } 42 43 static void vmx_fixed0and1_msr_test(struct kvm_vcpu *vcpu, uint32_t msr_index) 44 { 45 vmx_fixed0_msr_test(vcpu, msr_index, GENMASK_ULL(31, 0)); 46 vmx_fixed1_msr_test(vcpu, msr_index, GENMASK_ULL(63, 32)); 47 } 48 49 static void vmx_save_restore_msrs_test(struct kvm_vcpu *vcpu) 50 { 51 vcpu_set_msr(vcpu, MSR_IA32_VMX_VMCS_ENUM, 0); 52 vcpu_set_msr(vcpu, MSR_IA32_VMX_VMCS_ENUM, -1ull); 53 54 vmx_fixed1_msr_test(vcpu, MSR_IA32_VMX_BASIC, 55 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55)); 56 57 vmx_fixed1_msr_test(vcpu, MSR_IA32_VMX_MISC, 58 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | 59 BIT_ULL(15) | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30)); 60 61 vmx_fixed0and1_msr_test(vcpu, MSR_IA32_VMX_PROCBASED_CTLS2); 62 vmx_fixed1_msr_test(vcpu, MSR_IA32_VMX_EPT_VPID_CAP, -1ull); 63 vmx_fixed0and1_msr_test(vcpu, MSR_IA32_VMX_TRUE_PINBASED_CTLS); 64 vmx_fixed0and1_msr_test(vcpu, MSR_IA32_VMX_TRUE_PROCBASED_CTLS); 65 vmx_fixed0and1_msr_test(vcpu, MSR_IA32_VMX_TRUE_EXIT_CTLS); 66 vmx_fixed0and1_msr_test(vcpu, MSR_IA32_VMX_TRUE_ENTRY_CTLS); 67 vmx_fixed1_msr_test(vcpu, MSR_IA32_VMX_VMFUNC, -1ull); 68 } 69 70 int main(void) 71 { 72 struct kvm_vcpu *vcpu; 73 struct kvm_vm *vm; 74 75 TEST_REQUIRE(kvm_has_cap(KVM_CAP_DISABLE_QUIRKS2)); 76 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); 77 78 /* No need to actually do KVM_RUN, thus no guest code. */ 79 vm = vm_create_with_one_vcpu(&vcpu, NULL); 80 81 vmx_save_restore_msrs_test(vcpu); 82 83 kvm_vm_free(vm); 84 } 85