KVM: x86: Fix INIT signal handling in various CPU states
authorLiran Alon <liran.alon@oracle.com>
Mon, 26 Aug 2019 10:24:49 +0000 (13:24 +0300)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 11 Sep 2019 16:11:45 +0000 (18:11 +0200)
Commit cd7764fe9f73 ("KVM: x86: latch INITs while in system management mode")
changed code to latch INIT while vCPU is in SMM and process latched INIT
when leaving SMM. It left a subtle remark in commit message that similar
treatment should also be done while vCPU is in VMX non-root-mode.

However, INIT signals should actually be latched in various vCPU states:
(*) For both Intel and AMD, INIT signals should be latched while vCPU
is in SMM.
(*) For Intel, INIT should also be latched while vCPU is in VMX
operation and later processed when vCPU leaves VMX operation by
executing VMXOFF.
(*) For AMD, INIT should also be latched while vCPU runs with GIF=0
or in guest-mode with intercept defined on INIT signal.

To fix this:
1) Add kvm_x86_ops->apic_init_signal_blocked() such that each CPU vendor
can define the various CPU states in which INIT signals should be
blocked and modify kvm_apic_accept_events() to use it.
2) Modify vmx_check_nested_events() to check for pending INIT signal
while vCPU in guest-mode. If so, emualte vmexit on
EXIT_REASON_INIT_SIGNAL. Note that nSVM should have similar behaviour
but is currently left as a TODO comment to implement in the future
because nSVM don't yet implement svm_check_nested_events().

Note: Currently KVM nVMX implementation don't support VMX wait-for-SIPI
activity state as specified in MSR_IA32_VMX_MISC bits 6:8 exposed to
guest (See nested_vmx_setup_ctls_msrs()).
If and when support for this activity state will be implemented,
kvm_check_nested_events() would need to avoid emulating vmexit on
INIT signal in case activity-state is wait-for-SIPI. In addition,
kvm_apic_accept_events() would need to be modified to avoid discarding
SIPI in case VMX activity-state is wait-for-SIPI but instead delay
SIPI processing to vmx_check_nested_events() that would clear
pending APIC events and emulate vmexit on SIPI.

Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Co-developed-by: Nikita Leshenko <nikita.leshchenko@oracle.com>
Signed-off-by: Nikita Leshenko <nikita.leshchenko@oracle.com>
Signed-off-by: Liran Alon <liran.alon@oracle.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/lapic.c
arch/x86/kvm/svm.c
arch/x86/kvm/vmx/nested.c
arch/x86/kvm/vmx/vmx.c

index c4f271a..b523949 100644 (file)
@@ -1209,6 +1209,8 @@ struct kvm_x86_ops {
        uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);
 
        bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu);
+
+       bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_arch_async_pf {
index 559e1c4..dbbe478 100644 (file)
@@ -2706,11 +2706,14 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
                return;
 
        /*
-        * INITs are latched while in SMM.  Because an SMM CPU cannot
-        * be in KVM_MP_STATE_INIT_RECEIVED state, just eat SIPIs
-        * and delay processing of INIT until the next RSM.
+        * INITs are latched while CPU is in specific states
+        * (SMM, VMX non-root mode, SVM with GIF=0).
+        * Because a CPU cannot be in these states immediately
+        * after it has processed an INIT signal (and thus in
+        * KVM_MP_STATE_INIT_RECEIVED state), just eat SIPIs
+        * and leave the INIT pending.
         */
-       if (is_smm(vcpu)) {
+       if (is_smm(vcpu) || kvm_x86_ops->apic_init_signal_blocked(vcpu)) {
                WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
                if (test_bit(KVM_APIC_SIPI, &apic->pending_events))
                        clear_bit(KVM_APIC_SIPI, &apic->pending_events);
index 2854aaf..d24050b 100644 (file)
@@ -7170,6 +7170,21 @@ static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
        return false;
 }
 
+static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
+{
+       struct vcpu_svm *svm = to_svm(vcpu);
+
+       /*
+        * TODO: Last condition latch INIT signals on vCPU when
+        * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
+        * To properly emulate the INIT intercept, SVM should implement
+        * kvm_x86_ops->check_nested_events() and call nested_svm_vmexit()
+        * there if an INIT signal is pending.
+        */
+       return !gif_set(svm) ||
+                  (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
+}
+
 static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
        .cpu_has_kvm_support = has_svm,
        .disabled_by_bios = is_disabled,
@@ -7306,6 +7321,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
        .nested_get_evmcs_version = nested_get_evmcs_version,
 
        .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
+
+       .apic_init_signal_blocked = svm_apic_init_signal_blocked,
 };
 
 static int __init svm_init(void)
index ad24533..6ce83c6 100644 (file)
@@ -3409,6 +3409,15 @@ static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
        unsigned long exit_qual;
        bool block_nested_events =
            vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
+       struct kvm_lapic *apic = vcpu->arch.apic;
+
+       if (lapic_in_kernel(vcpu) &&
+               test_bit(KVM_APIC_INIT, &apic->pending_events)) {
+               if (block_nested_events)
+                       return -EBUSY;
+               nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
+               return 0;
+       }
 
        if (vcpu->arch.exception.pending &&
                nested_vmx_check_exception(vcpu, &exit_qual)) {
@@ -4470,7 +4479,12 @@ static int handle_vmoff(struct kvm_vcpu *vcpu)
 {
        if (!nested_vmx_check_permission(vcpu))
                return 1;
+
        free_nested(vcpu);
+
+       /* Process a latched INIT during time CPU was in VMX operation */
+       kvm_make_request(KVM_REQ_EVENT, vcpu);
+
        return nested_vmx_succeed(vcpu);
 }
 
index 99f52f8..73bf9a2 100644 (file)
@@ -7470,6 +7470,11 @@ static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
        return false;
 }
 
+static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
+{
+       return to_vmx(vcpu)->nested.vmxon;
+}
+
 static __init int hardware_setup(void)
 {
        unsigned long host_bndcfgs;
@@ -7794,6 +7799,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
        .get_vmcs12_pages = NULL,
        .nested_enable_evmcs = NULL,
        .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
+       .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
 };
 
 static void vmx_cleanup_l1d_flush(void)