i386: hvf: Implement CPU kick
PATCH v4 1/1 i386: hvf: Implement CPU kick
HVF doesn't have a CPU kick and without it it's not possible to perform an action on CPU thread until a VMEXIT happens. The kick is also needed for timely interrupt delivery.
Existing implementation of CPU kick sends SIG_IPI (aka SIGUSR1) to vCPU thread, but it's different from what hv_vcpu_interrupt does. The latter one results in invocation of mp_cpus_kick() in XNU kernel [1].
mp_cpus_kick() sends an IPI through the host LAPIC to the HVF vCPU. And the kick interrupt leads to VM exit because "external-interrupt exiting” VM-execution control is enabled for HVF.
hv_vcpu_interrupt() has no effect if it's delivered when vCPU is outside of a guest, therefore to avoid kick loss it's complemented with a SIG_IPI handler and zero VMX-preemption timer. If the kick happens outside of hv_vcpu_run(), the signal handler will re-queue the kick by setting exit_request. exit_request is cleared when the request is satisifed, i.e. when vCPU thread returns with EXCP_INTERRUPT.
So we get the following scenarios time/location-wise for the kick:
1) vCPU thread is far away before hv_vcpu_run(), then exit_request is scheduled. As soon as vCPU thread approaches hv_vcpu_run(), the exit request is satasified.
2) vCPU thread is about to enter the guest, then VMX-preemption timer is enabled to expedite immediate VM-exit. The VMX-preemption timer is then cleared in VM-exit handler, exit from vCPU thread is performed.
3) The guest is running, then hv_vcpu_run() is interrupted by hv_vcpu_interrupt() and vCPU thread quits.
4) vCPU thread has just made VM-exit, then exit_request is recorded and VMX-preemption timer is enabled but the exit request won't be satisfied until the next iteration of vCPU thread, no kick loss happens.
5) vCPU thread is far after hv_vcpu_run(), then exit_request is recorded and VMX-preemption timer is not enabled. The exit request will be satasfied on the next iteration of vCPU thread, like in 4). The kick is not lost.
6) If some external interupt happens we can satisify exit request and can clear VMX-preemption timer, i.e. kicks are coalesced with interrupts.
While at it, correct type of hvf_fd to the type of hv_vcpuid_t to avoid compilation warnings.
1. https://opensource.apple.com/source/xnu/xnu-6153.81.5/osfmk/i386/mp.c
---
Hi,
The version is just for review. There's an issue where interrupts (x86_gsi_interrupt) don't reach HVF (i.e. no interrupts are loaded into VMCS interruption info field). I've been triaging the issue for a while and dtrace patches are byproduct of the triage efforts. Once I figure out where interrupts are lost/suppressed (which won't happen until the end of the next week due to vacation), I'll send v5 with kick patch and the interrupt delivery fix.
Thanks,
Roman
Changes since v3:
- Replaced TLS pthread_key_t with current_cpu (Paolo)
- Eliminated race condition by signalling through atomic variables (Paolo)
Changes since v2:
- Reworked workaround to minimize kick loss race. Use signals to interrupt vCPU thread outside of hv_vcpu_run() and turn-on/turn-off VMX-preemeption timer, while timer value is always zero. v3 also assumes that VMX-preemption timer control is always available on the hardware that supports HVF.
Changes since v1:
- Reduced kick loss race (Paolo) and removed SIG_IPI blocking
include/hw/core/cpu.h | 2 +- include/sysemu/hvf.h | 1 + softmmu/cpus.c | 13 ++++++--- target/i386/cpu.h | 1 + target/i386/hvf/hvf.c | 60 ++++++++++++++++++++++++++++++++++++++++-- target/i386/hvf/vmcs.h | 1 + 6 files changed, 71 insertions(+), 7 deletions(-)
include/hw/core/cpu.h +1 -1
@@ -454,7 +454,7 @@ struct CPUState {struct hax_vcpu_state *hax_vcpu; -int hvf_fd; +unsigned hvf_fd;/* track IOMMUs whose translations we've cached in the TCG TLB */ GArray *iommu_notifiers;
include/sysemu/hvf.h +1 -0
@@ -25,6 +25,7 @@ extern bool hvf_allowed;int hvf_init_vcpu (CPUState *);int hvf_vcpu_exec (CPUState *); +void hvf_vcpu_kick (CPUState *);void hvf_cpu_synchronize_state (CPUState *);void hvf_cpu_synchronize_post_reset (CPUState *);void hvf_cpu_synchronize_post_init (CPUState *);
softmmu/cpus.c +9 -4
@@ -1708,10 +1708,15 @@ static void qemu_cpu_kick_thread(CPUState *cpu) return; } cpu->thread_kicked = true; - err =pthread_kill (cpu->thread->thread,SIG_IPI ); -if (err && err !=ESRCH ) { -fprintf (stderr,"qemu:%s: %s" , __func__,strerror (err)); -exit (1 ); + +if (hvf_enabled ()) { +hvf_vcpu_kick (cpu); + }else { + err =pthread_kill (cpu->thread->thread,SIG_IPI ); +if (err && err !=ESRCH ) { +fprintf (stderr,"qemu:%s: %s" , __func__,strerror (err)); +exit (1 ); + } }#else /* _WIN32 */ if (!qemu_cpu_is_self (cpu)) {
target/i386/cpu.h +1 -0
@@ -1608,6 +1608,7 @@ typedef struct CPUX86State {struct kvm_nested_state *nested_state;#endif #if defined(CONFIG_HVF ) +bool hvf_in_guest;HVFX86LazyFlags hvf_lflags;void *hvf_mmio_buf;#endif
target/i386/hvf/hvf.c +58 -2
108 lines not on this page · raw message
target/i386/hvf/vmcs.h +1 -0
@@ -349,6 +349,7 @@#define VMCS_PIN_BASED_CTLS_EXTINT (1 << 0)#define VMCS_PIN_BASED_CTLS_NMI (1 << 3)#define VMCS_PIN_BASED_CTLS_VNMI (1 << 5) +#define VMCS_PIN_BASED_CTLS_VMX_PREEMPT_TIMER (1 << 6)#define VMCS_PRI_PROC_BASED_CTLS_INT_WINDOW_EXITING (1 << 2)#define VMCS_PRI_PROC_BASED_CTLS_TSC_OFFSET (1 << 3)
Signature · 1 line
2.26.1