KVM: VMX: Do not trap VMFUNC instructions for L1 guests.
authorYu Zhang <yu.c.zhang@linux.intel.com>
Wed, 9 Nov 2022 07:54:12 +0000 (15:54 +0800)
committerSean Christopherson <seanjc@google.com>
Tue, 7 Feb 2023 01:51:17 +0000 (01:51 +0000)
Explicitly disable VMFUNC in vmcs01 to document that KVM doesn't support
any VM-Functions for L1.  WARN in the dedicated VMFUNC handler if an exit
occurs while L1 is active, but keep the existing handlers as fallbacks to
avoid killing the VM as an unexpected VMFUNC VM-Exit isn't fatal

Signed-off-by: Yu Zhang <yu.c.zhang@linux.intel.com>
Link: https://lore.kernel.org/r/20221109075413.1405803-2-yu.c.zhang@linux.intel.com
[sean: don't kill the VM on an unexpected VMFUNC from L1, reword changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/vmx/nested.c
arch/x86/kvm/vmx/vmx.c

index 557b9c4687346b70d6abecc79b74e2927199d399..3c226de4b562bed99050767dd4a4e5a6fff13738 100644 (file)
@@ -5864,11 +5864,10 @@ static int handle_vmfunc(struct kvm_vcpu *vcpu)
        u32 function = kvm_rax_read(vcpu);
 
        /*
-        * VMFUNC is only supported for nested guests, but we always enable the
-        * secondary control for simplicity; for non-nested mode, fake that we
-        * didn't by injecting #UD.
+        * VMFUNC should never execute cleanly while L1 is active; KVM supports
+        * VMFUNC for nested VMs, but not for L1.
         */
-       if (!is_guest_mode(vcpu)) {
+       if (WARN_ON_ONCE(!is_guest_mode(vcpu))) {
                kvm_queue_exception(vcpu, UD_VECTOR);
                return 1;
        }
index 664994e3e9096840a3ce78c8ba0d63a15f3d0be5..8a9911ae1240f9a6ad124f1bca68640adcafc576 100644 (file)
@@ -4590,6 +4590,12 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
                                  SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
        exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
 
+       /*
+        * KVM doesn't support VMFUNC for L1, but the control is set in KVM's
+        * base configuration as KVM emulates VMFUNC[EPTP_SWITCHING] for L2.
+        */
+       exec_control &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
+
        /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
         * in vmx_set_cr4.  */
        exec_control &= ~SECONDARY_EXEC_DESC;