KVM: x86: emulate wait-for-SIPI and SIPI-VMExit
Paolo Bonzini
maintainercover KVM: x86: emulate wait-for-SIPI and SIPI-VMExit
The first patch is new, the second is a rebase of Yadong's patch
Paolo Bonzini (1):
KVM: x86: fix apic_accept_events vs check_nested_events
Yadong Qi (1):
KVM: x86: emulate wait-for-SIPI and SIPI-VMExit
arch/x86/include/asm/vmx.h | 1 + arch/x86/include/uapi/asm/vmx.h | 2 ++ arch/x86/kvm/lapic.c | 30 ++++++++++++++++--- arch/x86/kvm/vmx/nested.c | 53 ++++++++++++++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 2 +- 5 files changed, 69 insertions(+), 19 deletions(-)
Signature · 1 line
2.26.2
Paolo Bonzini
maintainerPATCH 1/2 KVM: x86: fix apic_accept_events vs check_nested_events
vmx_apic_init_signal_blocked is buggy in that it returns true even in VMX non-root mode. In non-root mode, however, INITs are not latched, they just cause a vmexit. Previously, KVM was waiting for them to be processed when kvm_apic_accept_events and in the meanwhile it ate the SIPIs that the processor received.
However, in order to implement the wait-for-SIPI activity state, KVM will have to process KVM_APIC_SIPI in vmx_check_nested_events, and it will not be possible anymore to disregard SIPIs in non-root mode as the code is currently doing.
By calling kvm_x86_ops.nested_ops->check_events, we can force a vmexit (with the side-effect of latching INITs) before incorrectly injecting an INIT or SIPI in a guest, and therefore vmx_apic_init_signal_blocked can do the right thing.
--- arch/x86/kvm/lapic.c | 30 ++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-)
arch/x86/kvm/lapic.c +26 -4
@@ -2843,14 +2843,35 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) {struct kvm_lapic *apic = vcpu->arch.apic;u8 sipi_vector; +int r;unsigned long pe; -if (!lapic_in_kernel (vcpu) || !apic->pending_events) +if (!lapic_in_kernel (vcpu)) +return ; + +/* +* Read pending events before calling the check_events +* callback. +*/ + pe =smp_load_acquire (&apic->pending_events); +if (!pe)return ; +if (is_guest_mode (vcpu)) { + r = kvm_x86_ops.nested_ops->check_events (vcpu); +if (r <0 ) +return ; +/* +* If an event has happened and caused a vmexit, +* we know INITs are latched and therefore +* we will not incorrectly deliver an APIC +* event instead of a vmexit. +*/ + } +/* * INITs are latched while CPU is in specific states -* (SMM, VMX non-root mode, SVM with GIF=0). +* (SMM, VMX 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
@@ -2858,13 +2879,13 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu)*/ if (kvm_vcpu_latch_init (vcpu)) {WARN_ON_ONCE (vcpu->arch.mp_state ==KVM_MP_STATE_INIT_RECEIVED ); -if (test_bit (KVM_APIC_SIPI , &apic->pending_events)) +if (test_bit (KVM_APIC_SIPI , &pe))clear_bit (KVM_APIC_SIPI , &apic->pending_events);return ; } - pe =xchg (&apic->pending_events,0 );if (test_bit (KVM_APIC_INIT , &pe)) { +clear_bit (KVM_APIC_INIT , &apic->pending_events);kvm_vcpu_reset (vcpu, true);if (kvm_vcpu_is_bsp (apic->vcpu)) vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ;
@@ -2873,6 +2894,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu) }if (test_bit (KVM_APIC_SIPI , &pe) && vcpu->arch.mp_state ==KVM_MP_STATE_INIT_RECEIVED ) { +clear_bit (KVM_APIC_SIPI , &apic->pending_events);/* evaluate pending_events before reading the vector */ smp_rmb (); sipi_vector = apic->sipi_vector;
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -7558,7 +7558,7 @@ static void enable_smi_window(struct kvm_vcpu *vcpu)static bool vmx_apic_init_signal_blocked (struct kvm_vcpu *vcpu) { -return to_vmx (vcpu)->nested.vmxon; +return to_vmx (vcpu)->nested.vmxon && !is_guest_mode (vcpu); }static void vmx_migrate_timers (struct kvm_vcpu *vcpu)
Signature · 1 line
2.26.2
Paolo Bonzini
maintainerPATCH 2/2 KVM: x86: emulate wait-for-SIPI and SIPI-VMExit
In reply to Paolo BonziniFrom: Yadong Qi <[email protected]>
Background: We have a lightweight HV, it needs INIT-VMExit and SIPI-VMExit to wake-up APs for guests since it do not monitor the Local APIC. But currently virtual wait-for-SIPI(WFS) state is not supported in nVMX, so when running on top of KVM, the L1 HV cannot receive the INIT-VMExit and SIPI-VMExit which cause the L2 guest cannot wake up the APs.
According to Intel SDM Chapter 25.2 Other Causes of VM Exits, SIPIs cause VM exits when a logical processor is in wait-for-SIPI state.
In this patch:
1. introduce SIPI exit reason,
2. introduce wait-for-SIPI state for nVMX,
3. advertise wait-for-SIPI support to guest.
When L1 hypervisor is not monitoring Local APIC, L0 need to emulate INIT-VMExit and SIPI-VMExit to L1 to emulate INIT-SIPI-SIPI for L2. L2 LAPIC write would be traped by L0 Hypervisor(KVM), L0 should emulate the INIT/SIPI vmexit to L1 hypervisor to set proper state for L2's vcpu state.
Handle procdure:
Source vCPU:
L2 write LAPIC.ICR(INIT).
L0 trap LAPIC.ICR write(INIT): inject a latched INIT event to target
vCPU.
Target vCPU:
L0 emulate an INIT VMExit to L1 if is guest mode.
L1 set guest VMCS, guest_activity_state=WAIT_SIPI, vmresume. L0 set vcpu.mp_state to INIT_RECEIVED if (vmcs12.guest_activity_state
== WAIT_SIPI).
Source vCPU:
L2 write LAPIC.ICR(SIPI).
L0 trap LAPIC.ICR write(INIT): inject a latched SIPI event to traget
vCPU.
Target vCPU:
L0 emulate an SIPI VMExit to L1 if (vcpu.mp_state == INIT_RECEIVED). L1 set CS:IP, guest_activity_state=ACTIVE, vmresume.
L0 resume to L2.
L2 start-up.
Message-Id: <[email protected]>
--- arch/x86/include/asm/vmx.h | 1 + arch/x86/include/uapi/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/nested.c | 53 ++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 14 deletions(-)
arch/x86/include/asm/vmx.h +1 -0
@@ -113,6 +113,7 @@#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f#define VMX_MISC_SAVE_EFER_LMA 0x00000020#define VMX_MISC_ACTIVITY_HLT 0x00000040 +#define VMX_MISC_ACTIVITY_WAIT_SIPI 0x00000100#define VMX_MISC_ZERO_LEN_INS 0x40000000#define VMX_MISC_MSR_LIST_MULTIPLIER 512
arch/x86/include/uapi/asm/vmx.h +2 -0
@@ -32,6 +32,7 @@#define EXIT_REASON_EXTERNAL_INTERRUPT 1#define EXIT_REASON_TRIPLE_FAULT 2#define EXIT_REASON_INIT_SIGNAL 3 +#define EXIT_REASON_SIPI_SIGNAL 4#define EXIT_REASON_INTERRUPT_WINDOW 7#define EXIT_REASON_NMI_WINDOW 8
@@ -94,6 +95,7 @@ { EXIT_REASON_EXTERNAL_INTERRUPT, "EXTERNAL_INTERRUPT" }, \ { EXIT_REASON_TRIPLE_FAULT, "TRIPLE_FAULT" }, \ { EXIT_REASON_INIT_SIGNAL, "INIT_SIGNAL" }, \ + { EXIT_REASON_SIPI_SIGNAL, "SIPI_SIGNAL" }, \ { EXIT_REASON_INTERRUPT_WINDOW, "INTERRUPT_WINDOW" }, \ { EXIT_REASON_NMI_WINDOW, "NMI_WINDOW" }, \ { EXIT_REASON_TASK_SWITCH, "TASK_SWITCH" }, \
arch/x86/kvm/vmx/nested.c +39 -14
79 lines not on this page · raw message
Signature · 1 line
2.26.2