x86/kvm/hyper-v: move VMX controls sanitization out of nested_enable_evmcs()
authorVitaly Kuznetsov <vkuznets@redhat.com>
Wed, 5 Feb 2020 12:30:33 +0000 (13:30 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 5 Feb 2020 14:55:06 +0000 (15:55 +0100)
With fine grained VMX feature enablement QEMU>=4.2 tries to do KVM_SET_MSRS
with default (matching CPU model) values and in case eVMCS is also enabled,
fails.

It would be possible to drop VMX feature filtering completely and make
this a guest's responsibility: if it decides to use eVMCS it should know
which fields are available and which are not. Hyper-V mostly complies to
this, however, there are some problematic controls:
SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES
VM_{ENTRY,EXIT}_LOAD_IA32_PERF_GLOBAL_CTRL

which Hyper-V enables. As there are no corresponding fields in eVMCS, we
can't handle this properly in KVM. This is a Hyper-V issue.

Move VMX controls sanitization from nested_enable_evmcs() to vmx_get_msr(),
and do the bare minimum (only clear controls which are known to cause issues).
This allows userspace to keep setting controls it wants and at the same
time hides them from the guest.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/evmcs.c
arch/x86/kvm/vmx/evmcs.h
arch/x86/kvm/vmx/vmx.c

index 89c3e0caf39f8b45113c48938d0a54ceb0bc288c..ba886fb7bc392d832725a24d132b82a6d9e666b3 100644 (file)
@@ -346,6 +346,32 @@ uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu)
        return 0;
 }
 
+void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata)
+{
+       u32 ctl_low = (u32)*pdata;
+       u32 ctl_high = (u32)(*pdata >> 32);
+
+       /*
+        * Hyper-V 2016 and 2019 try using these features even when eVMCS
+        * is enabled but there are no corresponding fields.
+        */
+       switch (msr_index) {
+       case MSR_IA32_VMX_EXIT_CTLS:
+       case MSR_IA32_VMX_TRUE_EXIT_CTLS:
+               ctl_high &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
+               break;
+       case MSR_IA32_VMX_ENTRY_CTLS:
+       case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
+               ctl_high &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
+               break;
+       case MSR_IA32_VMX_PROCBASED_CTLS2:
+               ctl_high &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
+               break;
+       }
+
+       *pdata = ctl_low | ((u64)ctl_high << 32);
+}
+
 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
                        uint16_t *vmcs_version)
 {
@@ -356,11 +382,5 @@ int nested_enable_evmcs(struct kvm_vcpu *vcpu,
        if (vmcs_version)
                *vmcs_version = nested_get_evmcs_version(vcpu);
 
-       vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
-       vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
-       vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
-       vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
-       vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
-
        return 0;
 }
index 07ebf6882a458ab7f85c03d0dd22aa759e6085eb..b88d9807a79675685de84573ec49b8074ce4c6ad 100644 (file)
@@ -201,5 +201,6 @@ bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
                        uint16_t *vmcs_version);
+void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata);
 
 #endif /* __KVM_X86_VMX_EVMCS_H */
index 678edbd6e278b16259e2746b525d66fb6f405fc0..ba334acaa37e7389e4fb6bab214a8bb588283eac 100644 (file)
@@ -1853,8 +1853,20 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
                if (!nested_vmx_allowed(vcpu))
                        return 1;
-               return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
-                                      &msr_info->data);
+               if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
+                                   &msr_info->data))
+                       return 1;
+               /*
+                * Enlightened VMCS v1 doesn't have certain fields, but buggy
+                * Hyper-V versions are still trying to use corresponding
+                * features when they are exposed. Filter out the essential
+                * minimum.
+                */
+               if (!msr_info->host_initiated &&
+                   vmx->nested.enlightened_vmcs_enabled)
+                       nested_evmcs_filter_control_msr(msr_info->index,
+                                                       &msr_info->data);
+               break;
        case MSR_IA32_RTIT_CTL:
                if (pt_mode != PT_MODE_HOST_GUEST)
                        return 1;