Skip to contentSkip to conversation

accel/mshv: add support for PIC interrupt injection

PATCH In review Magnus Kulke 2 participants 8 messages Oct 28 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover accel/mshv: add support for PIC interrupt injection

This change adds support for injecting legacy PIC interrupts for mshv-accelerated x86 guests. This is relevant e.g. for guest that rely on a PIT in early boot (e.g. ISOLINUX).

The series carries two provisional patches for
feature to work:

- target/i386/mshv: fix vCPU run result contract

this has been posted earlier as a standalone patch, but it is required here, so we might be able to drop it in an upcoming revision if the standalone fix is merged earlier.

- accel/mshv: do not coalesce vCPU kicks

this is a workaround for a race condition that currently exists in the upstream mshv driver, which can lead to interruption requests being lost. A new CANCEL_VP ioctl is being tested. It will address this issue, so we can drop this workaround once it the new ioctl is available upstream.

Magnus Kulke (4):
target/i386/mshv: fix vCPU run result contract
accel/mshv: stop VM on vCPU execution errors
target/i386/mshv: deliver legacy PIC interrupts
accel/mshv: do not coalesce vCPU kicks

 accel/mshv/mshv-all.c          |  26 ++++-
 hw/i386/x86-cpu.c              |   9 +-
 include/hw/hyperv/hvgdk_mini.h |  42 +++++++-
 include/system/mshv_int.h      |   1 +
 meson.build                    |   1 +
 target/i386/mshv/mshv-cpu.c    | 183 +++++++++++++++++++++++++++++----
 target/i386/mshv/trace-events  |   7 ++
 target/i386/mshv/trace.h       |   2 +
 8 files changed, 240 insertions(+), 31 deletions(-)
 create mode 100644 target/i386/mshv/trace-events
 create mode 100644 target/i386/mshv/trace.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 target/i386/mshv: fix vCPU run result contract

Currently the return semantics of mshv_run_vcpu() are broken. They return a mix of int and exit_reason, ignoring the out param *exit.

This change propagates errno's to the call site and sets the *exit value accordingly. We also consider the case when an ioctl has return due to a qemu-requested interruption of the call, this is a legitimate condition that should not produce an error.

Fixes 6dec60528c4 (target/i386/mshv: Implement mshv_vcpu_run())
---
 target/i386/mshv/mshv-cpu.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
target/i386/mshv/mshv-cpu.c +17 -11
index 4df73bb497..e3c155da2e 100644
@@ -2124,7 +2124,6 @@ static int arch_set_single_step(CPUState *cpu, bool enable)
 int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 {
     int ret;
-    enum MshvVmExit exit_reason;
     int cpu_fd = mshv_vcpufd(cpu);
     bool single_step;
 
@@ -2142,7 +2141,12 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
     ret = ioctl(cpu_fd, MSHV_RUN_VP, msg);
     if (ret < 0) {
-        return MshvVmExitShutdown;
+        if (errno == EINTR) {
+            *exit = MshvVmExitSpecial;
+            return 0;
+        }
+        error_report("failed to run vCPU: %s", strerror(errno));
+        return -errno;
     }
 
     /* disable single stepping by flipping RFLAGS.TF */
@@ -2154,31 +2158,33 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
     switch (msg->header.message_type) {
     case HVMSG_UNRECOVERABLE_EXCEPTION:
-        return MshvVmExitShutdown;
+        *exit = MshvVmExitShutdown;
+        return 0;
     case HVMSG_UNMAPPED_GPA:
     case HVMSG_GPA_INTERCEPT:
-        ret = handle_mmio(cpu, msg, &exit_reason);
+        ret = handle_mmio(cpu, msg, exit);
         if (ret < 0) {
             error_report("failed to handle mmio");
-            return -1;
+            return ret;
         }
-        return exit_reason;
+        return 0;
     case HVMSG_X64_IO_PORT_INTERCEPT:
         ret = handle_pio(cpu, msg);
         if (ret < 0) {
-            return MshvVmExitSpecial;
+            error_report("failed to handle PIO");
+            return ret;
         }
-        return MshvVmExitIgnore;
+        *exit = MshvVmExitIgnore;
+        return 0;
     case HVMSG_X64_EXCEPTION_INTERCEPT:
         bql_lock();
-        ret = handle_exception_interrupt(cpu, msg, &exit_reason);
+        ret = handle_exception_interrupt(cpu, msg, exit);
         bql_unlock();
         if (ret < 0) {
             error_report("failed to handle exception intercept");
             return -1;
         }
-        *exit = exit_reason;
-        return exit_reason;
+        return 0;
     default:
         break;
     }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] target/i386/mshv: fix vCPU run result contract

Quoted 7 lines · Magnus Kulke

Currently the return semantics of mshv_run_vcpu() are broken. They return a mix of int and exit_reason, ignoring the out param *exit.

Quoted 8 lines

This change propagates errno's to the call site and sets the *exit value accordingly. We also consider the case when an ioctl has return due to a qemu-requested interruption of the call, this is a legitimate condition that should not produce an error.

Fixes 6dec60528c4 (target/i386/mshv: Implement mshv_vcpu_run())

---

Quoted 76 lines
 target/i386/mshv/mshv-cpu.c | 28 +++++++++++++++++-----------
1 file changed, 17 insertions(+), 11 deletions(-)
target/i386/mshv/mshv-cpu.c +17 -11
index 4df73bb497..e3c155da2e 100644
@@ -2124,7 +2124,6 @@ static int arch_set_single_step(CPUState *cpu, bool enable)
 int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 {
    int ret;
-    enum MshvVmExit exit_reason;
    int cpu_fd = mshv_vcpufd(cpu);
    bool single_step;
 
@@ -2142,7 +2141,12 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
    ret = ioctl(cpu_fd, MSHV_RUN_VP, msg);
    if (ret < 0) {
-        return MshvVmExitShutdown;
+        if (errno == EINTR) {
+            *exit = MshvVmExitSpecial;
+            return 0;
+        }
+        error_report("failed to run vCPU: %s", strerror(errno));
+        return -errno;
    }
 
    /* disable single stepping by flipping RFLAGS.TF */
@@ -2154,31 +2158,33 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
 
    switch (msg->header.message_type) {
    case HVMSG_UNRECOVERABLE_EXCEPTION:
-        return MshvVmExitShutdown;
+        *exit = MshvVmExitShutdown;
+        return 0;
    case HVMSG_UNMAPPED_GPA:
    case HVMSG_GPA_INTERCEPT:
-        ret = handle_mmio(cpu, msg, &exit_reason);
+        ret = handle_mmio(cpu, msg, exit);
        if (ret < 0) {
            error_report("failed to handle mmio");
-            return -1;
+            return ret;
        }
-        return exit_reason;
+        return 0;
    case HVMSG_X64_IO_PORT_INTERCEPT:
        ret = handle_pio(cpu, msg);
        if (ret < 0) {
-            return MshvVmExitSpecial;
+            error_report("failed to handle PIO");
+            return ret;
        }
-        return MshvVmExitIgnore;
+        *exit = MshvVmExitIgnore;
+        return 0;
    case HVMSG_X64_EXCEPTION_INTERCEPT:
        bql_lock();
-        ret = handle_exception_interrupt(cpu, msg, &exit_reason);
+        ret = handle_exception_interrupt(cpu, msg, exit);
        bql_unlock();
        if (ret < 0) {
            error_report("failed to handle exception intercept");
            return -1;
        }
-        *exit = exit_reason;
-        return exit_reason;
+        return 0;
    default:
        break;
    }
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/4 accel/mshv: stop VM on vCPU execution errors

In reply to Magnus Kulke

Currently we abort the process immediately if the ioctl returns an error, so the error handling routine outside the loop cannot be reached. We also enumerate the enum variants explicitly to make it obvious how every exit reason is being handled.

---
 accel/mshv/mshv-all.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
accel/mshv/mshv-all.c +4 -3
index d820a67b95..7289c8d1c2 100644
@@ -778,8 +778,8 @@ static int mshv_cpu_exec(CPUState *cpu)
 
         ret = mshv_run_vcpu(mshv_state->vm, cpu, &mshv_msg, &exit_reason);
         if (ret < 0) {
-            error_report("Failed to run on vcpu %d", cpu->cpu_index);
-            abort();
+            error_report("Failed to run on vCPU %d", cpu->cpu_index);
+            break;
         }
 
         switch (exit_reason) {
@@ -788,7 +788,8 @@ static int mshv_cpu_exec(CPUState *cpu)
         case MshvVmExitDebug:
             ret = EXCP_DEBUG;
             break;
-        default:
+        case MshvVmExitSpecial:
+        case MshvVmExitShutdown:
             ret = EXCP_INTERRUPT;
             break;
         }
Signature · 1 line
2.34.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] accel/mshv: stop VM on vCPU execution errors

Quoted 10 lines · Magnus Kulke

Currently we abort the process immediately if the ioctl returns an error, so the error handling routine outside the loop cannot be reached. We also enumerate the enum variants explicitly to make it obvious how every exit reason is being handled.

Quoted 33 lines

---
accel/mshv/mshv-all.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
accel/mshv/mshv-all.c +4 -3
index d820a67b95..7289c8d1c2 100644
@@ -778,8 +778,8 @@ static int mshv_cpu_exec(CPUState *cpu)
 
        ret = mshv_run_vcpu(mshv_state->vm, cpu, &mshv_msg, &exit_reason);
        if (ret < 0) {
-            error_report("Failed to run on vcpu %d", cpu->cpu_index);
-            abort();
+            error_report("Failed to run on vCPU %d", cpu->cpu_index);
+            break;
        }
 
        switch (exit_reason) {
@@ -788,7 +788,8 @@ static int mshv_cpu_exec(CPUState *cpu)
        case MshvVmExitDebug:
            ret = EXCP_DEBUG;
            break;
-        default:
+        case MshvVmExitSpecial:
+        case MshvVmExitShutdown:
            ret = EXCP_INTERRUPT;
            break;
        }
Signature · 1 line
2.34.1
#5 lore raw

PATCH 3/4 target/i386/mshv: deliver legacy PIC interrupts

In reply to Magnus Kulke

To inject legacy interrupts in the MSHV accelerator, we have to request a "deliverability notification" once the PIC asserts an interrupt. Once we receive a INTERRUPTION_DELIVERABLE exit from the hypervisor we can proceed to acknowledge the interrupt and write to the PENDING_EVENT0 register with EXT_INT event type and the respective vector.

Since an interrupt is supposed to wake a vCPU stopped by HLT, we do clear the halt_suspend field of the INTERNAL_ACTIVITY_STATE register.

We also need to consider pending EXT_INT events in migration, splitting the mapping of pending_exc into two discrete EXCEPTION and EXT_INT event types.

---
 hw/i386/x86-cpu.c              |   9 +-
 include/hw/hyperv/hvgdk_mini.h |  42 ++++++++-
 include/system/mshv_int.h      |   1 +
 meson.build                    |   1 +
 target/i386/mshv/mshv-cpu.c    | 155 +++++++++++++++++++++++++++++++--
 target/i386/mshv/trace-events  |   7 ++
 target/i386/mshv/trace.h       |   2 +
 7 files changed, 200 insertions(+), 17 deletions(-)
 create mode 100644 target/i386/mshv/trace-events
 create mode 100644 target/i386/mshv/trace.h
hw/i386/x86-cpu.c +3 -6
index fba313376c..7b9299272a 100644
@@ -46,12 +46,8 @@ static void pic_irq_request(void *opaque, int irq, int level)
 
     trace_x86_pic_interrupt(irq, level);
 
-    if (mshv_irqchip_in_kernel()) {
-        return;
-    }
-
     if (cpu_is_apic_enabled(cpu->apic_state) && !kvm_irqchip_in_kernel() &&
-        !whpx_irqchip_in_kernel()) {
+        !mshv_irqchip_in_kernel() && !whpx_irqchip_in_kernel()) {
         CPU_FOREACH(cs) {
             cpu = X86_CPU(cs);
             if (apic_accept_pic_intr(cpu->apic_state)) {
@@ -77,7 +73,8 @@ int cpu_get_pic_interrupt(CPUX86State *env)
     X86CPU *cpu = env_archcpu(env);
     int intno;
 
-    if (!kvm_irqchip_in_kernel() && !whpx_irqchip_in_kernel()) {
+    if (!kvm_irqchip_in_kernel() && !mshv_irqchip_in_kernel() &&
+        !whpx_irqchip_in_kernel()) {
         intno = apic_get_interrupt(cpu->apic_state);
         if (intno >= 0) {
             return intno;
include/hw/hyperv/hvgdk_mini.h +38 -4

72 lines not on this page · raw message

include/system/mshv_int.h +1 -0
index 1f77ffd152..d527cd8d77 100644
@@ -45,6 +45,7 @@ typedef struct MshvHvCallArgs {
 struct AccelCPUState {
     int cpufd;
     MshvHvCallArgs hvcall_args;
+    bool pic_interrupt_window;
 };
 
 typedef struct MshvMemoryListener {
meson.build +1 -0
index bbef9d9c29..24caec97a9 100644
@@ -3686,6 +3686,7 @@ if have_system or have_user
     'target/hppa',
     'target/i386',
     'target/i386/kvm',
+    'target/i386/mshv',
     'target/i386/whpx',
     'target/loongarch',
     'target/mips/tcg',
target/i386/mshv/mshv-cpu.c +148 -7

192 lines not on this page · raw message

target/i386/mshv/trace-events new file +7 -0
new file mode 100644
index 0000000000..347ce040ab
@@ -0,0 +1,7 @@
+# Authors: Magnus Kulke <[email protected]>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+mshv_run_vcpu_eintr(uint32_t cpu) "cpu_index=%d"
+mshv_set_pic_interrupt_window(uint32_t cpu, bool enabled) "cpu_index=%d enabled=%d"
+mshv_pic_interrupt(uint32_t cpu, uint8_t vector) "cpu_index=%d vector=0x%x"
target/i386/mshv/trace.h new file +2 -0
new file mode 100644
index 0000000000..81b6b655c7
@@ -0,0 +1,2 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "trace/trace-target_i386_mshv.h"
Signature · 1 line
2.34.1
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] target/i386/mshv: deliver legacy PIC interrupts

Quoted 12 lines · Magnus Kulke

To inject legacy interrupts in the MSHV accelerator, we have to request a "deliverability notification" once the PIC asserts an interrupt. Once we receive a INTERRUPTION_DELIVERABLE exit from the hypervisor we can proceed to acknowledge the interrupt and write to the PENDING_EVENT0 register with EXT_INT event type and the respective vector.

Since an interrupt is supposed to wake a vCPU stopped by HLT, we do clear the halt_suspend field of the INTERNAL_ACTIVITY_STATE register.

Keeping this bug on the Hyper-V side is a bit unfortunate. But oh well...

Quoted 6 lines

We also need to consider pending EXT_INT events in migration, splitting the mapping of pending_exc into two discrete EXCEPTION and EXT_INT event types.

Quoted 372 lines

---

hw/i386/x86-cpu.c              |   9 +-
include/hw/hyperv/hvgdk_mini.h |  42 ++++++++-
include/system/mshv_int.h      |   1 +
meson.build                    |   1 +
target/i386/mshv/mshv-cpu.c    | 155 +++++++++++++++++++++++++++++++--
target/i386/mshv/trace-events  |   7 ++
target/i386/mshv/trace.h       |   2 +
7 files changed, 200 insertions(+), 17 deletions(-)

create mode 100644 target/i386/mshv/trace-events
create mode 100644 target/i386/mshv/trace.h

hw/i386/x86-cpu.c +3 -6
index fba313376c..7b9299272a 100644
@@ -46,12 +46,8 @@ static void pic_irq_request(void *opaque, int irq, int level)
 
    trace_x86_pic_interrupt(irq, level);
 
-    if (mshv_irqchip_in_kernel()) {
-        return;
-    }
-
    if (cpu_is_apic_enabled(cpu->apic_state) && !kvm_irqchip_in_kernel() &&
-        !whpx_irqchip_in_kernel()) {
+        !mshv_irqchip_in_kernel() && !whpx_irqchip_in_kernel()) {
        CPU_FOREACH(cs) {
            cpu = X86_CPU(cs);
            if (apic_accept_pic_intr(cpu->apic_state)) {
@@ -77,7 +73,8 @@ int cpu_get_pic_interrupt(CPUX86State *env)
    X86CPU *cpu = env_archcpu(env);
    int intno;
 
-    if (!kvm_irqchip_in_kernel() && !whpx_irqchip_in_kernel()) {
+    if (!kvm_irqchip_in_kernel() && !mshv_irqchip_in_kernel() &&
+        !whpx_irqchip_in_kernel()) {
        intno = apic_get_interrupt(cpu->apic_state);
        if (intno >= 0) {
            return intno;
include/hw/hyperv/hvgdk_mini.h +38 -4
index 0983e3a58d..e585661b2c 100644
@@ -30,10 +30,11 @@ typedef enum hv_register_name {
    HV_REGISTER_INTERNAL_ACTIVITY_STATE = 0x00000004,
 
    /* Pending Interruption Register */
-    HV_REGISTER_PENDING_INTERRUPTION = 0x00010002,
-    HV_REGISTER_INTERRUPT_STATE      = 0x00010003,
-    HV_REGISTER_PENDING_EVENT0       = 0x00010004,
-    HV_REGISTER_PENDING_EVENT1       = 0x00010005,
+    HV_REGISTER_PENDING_INTERRUPTION         = 0x00010002,
+    HV_REGISTER_INTERRUPT_STATE              = 0x00010003,
+    HV_REGISTER_PENDING_EVENT0               = 0x00010004,
+    HV_REGISTER_PENDING_EVENT1               = 0x00010005,
+    HV_REGISTER_DELIVERABILITY_NOTIFICATIONS = 0x00010006,
 
    /* X64 User-Mode Registers */
    HV_X64_REGISTER_RAX     = 0x00020000,
@@ -313,6 +314,21 @@ union hv_x64_pending_exception_event {
    };
 };
 
+#define HV_X64_PENDING_EVENT_EXCEPTION 0
+#define HV_X64_PENDING_EVENT_EXT_INT   5
+
+union hv_x64_pending_ext_int_event {
+    uint64_t as_uint64[2];
+    struct {
+        uint64_t event_pending:1;
+        uint64_t event_type:3;
+        uint64_t reserved0:4;
+        uint64_t vector:8;
+        uint64_t reserved1:48;
+        uint64_t reserved2;
+    };
+};
+
 union hv_x64_pending_virtualization_fault_event {
    uint64_t as_uint64[2];
    struct {
@@ -443,6 +459,17 @@ union hv_x64_interrupt_state_register {
    };
 };
 
+union hv_x64_deliverability_notifications_register {
+    uint64_t as_uint64;
+    struct {
+        uint64_t nmi_notification:1;
+        uint64_t interrupt_notification:1;
+        uint64_t interrupt_priority:4;
+        uint64_t reserved:42;
+        uint64_t sints:16;
+    };
+};
+
 union hv_intercept_suspend_register {
    uint64_t as_uint64;
    struct {
@@ -469,6 +496,7 @@ typedef union hv_register_value {
    union hv_x64_pending_interruption_register pending_interruption;
    union hv_x64_msr_npiep_config_contents npiep_config;
    union hv_x64_pending_exception_event pending_exception_event;
+    union hv_x64_pending_ext_int_event pending_ext_int_event;
    union hv_x64_pending_virtualization_fault_event
        pending_virtualization_fault_event;
    union hv_x64_register_sev_control sev_control;
@@ -715,6 +743,12 @@ struct hv_x64_intercept_message_header {
    uint64_t rflags;
 } QEMU_PACKED;
 
+struct hv_x64_interruption_deliverable_message {
+    struct hv_x64_intercept_message_header header;
+    uint32_t deliverable_type;
+    uint32_t reserved;
+} QEMU_PACKED;
+
 union hv_x64_io_port_access_info {
    uint8_t as_uint8;
    struct {
include/system/mshv_int.h +1 -0
index 1f77ffd152..d527cd8d77 100644
@@ -45,6 +45,7 @@ typedef struct MshvHvCallArgs {
 struct AccelCPUState {
    int cpufd;
    MshvHvCallArgs hvcall_args;
+    bool pic_interrupt_window;
 };
 
 typedef struct MshvMemoryListener {
meson.build +1 -0
index bbef9d9c29..24caec97a9 100644
@@ -3686,6 +3686,7 @@ if have_system or have_user
    'target/hppa',
    'target/i386',
    'target/i386/kvm',
+    'target/i386/mshv',
    'target/i386/whpx',
    'target/loongarch',
    'target/mips/tcg',
target/i386/mshv/mshv-cpu.c +148 -7
index e3c155da2e..7032e0f6ac 100644
@@ -771,6 +771,7 @@ static int get_vcpu_events(CPUState *cpu)
    union hv_x64_pending_interruption_register pending_int;
    union hv_x64_interrupt_state_register int_state;
    union hv_x64_pending_exception_event pending_exc;
+    union hv_x64_pending_ext_int_event pending_ext_int;
    int ret;
 
    ret = mshv_get_generic_regs(cpu, assocs, ARRAY_SIZE(assocs));
@@ -782,6 +783,7 @@ static int get_vcpu_events(CPUState *cpu)
    pending_int.as_uint64 = assocs[0].value.reg64;
    int_state.as_uint64 = assocs[1].value.reg64;
    pending_exc = assocs[2].value.pending_exception_event;
+    pending_ext_int = assocs[2].value.pending_ext_int_event;
 
    /* Clear previous state. injected ints/excs are blanked w/ -1 */
    env->interrupt_injected    = -1;
@@ -841,14 +843,25 @@ static int get_vcpu_events(CPUState *cpu)
        env->hflags2 &= ~HF2_NMI_MASK;
    }
 
-    /* HV_REGISTER_PENDING_EVENT0: pending exception not yet injected */
+    /* HV_REGISTER_PENDING_EVENT0: event not yet injected */
    if (pending_exc.event_pending) {
-        env->exception_pending     = 1;
-        env->exception_nr          = pending_exc.vector;
-        env->has_error_code        = pending_exc.deliver_error_code;
-        env->error_code            = pending_exc.error_code;
-        env->exception_has_payload = (pending_exc.exception_parameter != 0);
-        env->exception_payload     = pending_exc.exception_parameter;
+        switch (pending_exc.event_type) {
+        case HV_X64_PENDING_EVENT_EXCEPTION:
+            env->exception_pending     = 1;
+            env->exception_nr          = pending_exc.vector;
+            env->has_error_code        = pending_exc.deliver_error_code;
+            env->error_code            = pending_exc.error_code;
+            env->exception_has_payload = pending_exc.exception_parameter != 0;
+            env->exception_payload     = pending_exc.exception_parameter;
+            break;
+        case HV_X64_PENDING_EVENT_EXT_INT:
+            env->interrupt_injected = pending_ext_int.vector;
+            break;
+        default:
+            error_report("unknown pending event type %u",
+                         pending_exc.event_type);
+            return -EINVAL;
+        }
    }
 
    /*
@@ -2091,6 +2104,119 @@ static int handle_exception_interrupt(CPUState *cpu,
    return 0;
 }
 
+static int set_pic_interrupt_window(CPUState *cpu, bool enabled)
+{
+    union hv_x64_deliverability_notifications_register notifications = { 0 };
+    struct hv_register_assoc assoc = { 0 };
+    AccelCPUState *cpu_state = cpu->accel;
+    int ret;
+
+    notifications.interrupt_notification = enabled;
+
+    assoc.name        = HV_REGISTER_DELIVERABILITY_NOTIFICATIONS;
+    assoc.value.reg64 = notifications.as_uint64;
+
+    /* we are already waiting for a PIC interrupt window */
+    if (cpu_state->pic_interrupt_window == enabled) {
+        return 0;
+    }
+
+    trace_mshv_set_pic_interrupt_window(cpu->cpu_index, enabled);
+
+    ret = mshv_set_generic_regs(cpu, &assoc, 1);
+    if (ret < 0) {
+        error_report("failed to set deliverability notifications register");
+        return ret;
+    }
+
+    cpu_state->pic_interrupt_window = enabled;
+    return 0;
+}
+
+static inline int request_pic_interrupt_window(CPUState *cpu)
+{
+    return set_pic_interrupt_window(cpu, true);
+}
+
+static inline int clear_pic_interrupt_window(CPUState *cpu)
+{
+    return set_pic_interrupt_window(cpu, false);
+}
+
+static int handle_interrupt_deliverable(CPUState *cpu,
+                                        const struct hyperv_message *msg)
+{
+    CPUX86State *env = &X86_CPU(cpu)->env;
+    struct hv_x64_interruption_deliverable_message info;
+    union hv_x64_pending_ext_int_event event = { 0 };
+    struct hv_register_assoc assocs[2] = { 0 };
+    struct hv_register_assoc *event_assoc = &assocs[0],
+                             *activity_assoc = &assocs[1];
+    union hv_internal_activity_register activity;
+    bool is_suspended;
+    int ret, vector;
+
+    memcpy(&info, msg->payload, sizeof(info));
+    if (info.deliverable_type != MSHV_HV_INTERRUPTION_TYPE_EXT_INT) {
+        error_report("unexpected deliverable interruption type %u",
+                     info.deliverable_type);
+        return -1;
+    }
+
+    ret = clear_pic_interrupt_window(cpu);
+    if (ret < 0) {
+        error_report("failed to clear PIC interrupt window");
+        return -1;
+    }
+
+    bql_lock();
+    cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
+    vector = cpu_get_pic_interrupt(env);
+    bql_unlock();
+
+    /* vector == -1 means the PIC has no longer a pending interrupt */
+    if (vector < 0) {
+        return 0;
+    }
+
+    trace_mshv_pic_interrupt(cpu->cpu_index, vector);
+
+    /* we check whether the vcpu is halted first */
+    activity_assoc->name = HV_REGISTER_INTERNAL_ACTIVITY_STATE;
+    ret = mshv_get_generic_regs(cpu, activity_assoc, 1);
+    if (ret < 0) {
+        error_report("failed to read activity state before PIC delivery");
+        return -1;
+    }
+
+    event.event_pending = 1;
+    event.event_type    = HV_X64_PENDING_EVENT_EXT_INT;
+    event.vector        = vector;
+
+    event_assoc->name                        = HV_REGISTER_PENDING_EVENT0;
+    event_assoc->value.pending_ext_int_event = event;
+
+    /*
+     * If the vcpu was hlt suspended we clear the suspend bit, otherwise we
+     * only set the pending event.
+     */
+    activity = activity_assoc->value.internal_activity;
+    is_suspended = activity.halt_suspend;
+    if (is_suspended) {
+        activity.halt_suspend = 0;
+        activity_assoc->value.internal_activity = activity;
+    }
+
+    ret = mshv_set_generic_regs(cpu, assocs, is_suspended ? 2 : 1);
+    if (ret < 0) {
+        error_report("failed to set pending event%s",
+                     is_suspended ? " and activity state" : "");
+        return ret;
+    }
+
+    return 0;
+}
+
 /*
 * Flip RFLAGS.TF like WHPX. Set it only on the live register, not env->eflags,
 * so a later store won't put it back.
@@ -2139,6 +2265,14 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
        }
    }
 
+    if (cpu_test_interrupt(cpu, CPU_INTERRUPT_HARD)) {
+        ret = request_pic_interrupt_window(cpu);
+        if (ret < 0) {
+            error_report("failed to request PIC interrupt window");
+            return -1;
+        }
+    }
+
    ret = ioctl(cpu_fd, MSHV_RUN_VP, msg);
    if (ret < 0) {
        if (errno == EINTR) {
@@ -2185,6 +2319,13 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
            return -1;
        }
        return 0;
+    case HVMSG_X64_INTERRUPTION_DELIVERABLE:
+        ret = handle_interrupt_deliverable(cpu, msg);
+        if (ret < 0) {
+            return ret;
+        }
+        *exit = MshvVmExitIgnore;
+        return 0;
    default:
        break;
    }
target/i386/mshv/trace-events new file +7 -0
new file mode 100644
index 0000000000..347ce040ab
@@ -0,0 +1,7 @@
+# Authors: Magnus Kulke <[email protected]>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+mshv_run_vcpu_eintr(uint32_t cpu) "cpu_index=%d"
+mshv_set_pic_interrupt_window(uint32_t cpu, bool enabled) "cpu_index=%d enabled=%d"
+mshv_pic_interrupt(uint32_t cpu, uint8_t vector) "cpu_index=%d vector=0x%x"
target/i386/mshv/trace.h new file +2 -0
new file mode 100644
index 0000000000..81b6b655c7
@@ -0,0 +1,2 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "trace/trace-target_i386_mshv.h"
Signature · 1 line
2.34.1
#7 lore raw

PATCH 4/4 accel/mshv: do not coalesce vCPU kicks

In reply to Magnus Kulke

MSHV today lacks an immediate-exit API. QEMU tehre kicks a vCPU by sending SIG_IPI to interrupt an active MSHV_VP_RUN ioctl invocation.

The generic kick path suppresses repeat signals using CPUState::thread_kicked, which is cleared when we return to the the outer vCPU loop (in mshv_vcpu_thread()). MSHV cannot guarantee that a signal will cause sucha return: it may be handled between MSHV_VP_RUN calls or MSHV_RUN_VP may return an unrelated intercept instead of EINTR.

Hence we install an MSHV-specific kick impl that sends SIG_IPI for every requested vCPU kick.

---
 accel/mshv/mshv-all.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
accel/mshv/mshv-all.c +19 -0
index 7289c8d1c2..0f733cac9d 100644
@@ -888,6 +888,24 @@ static void mshv_start_vcpu_thread(CPUState *cpu)
                        QEMU_THREAD_JOINABLE);
 }
 
+static void mshv_kick_vcpu_thread(CPUState *cpu)
+{
+    int err;
+
+    /* don't kick ourselves, we are already running */
+    if (qemu_cpu_is_self(cpu)) {
+        return;
+    }
+
+    err = pthread_kill(cpu->thread->thread, SIG_IPI);
+    /* in case the vcpu thread has been torn down we don't want to abort */
+    if (err && err != ESRCH) {
+        error_report("failed to kick MSHV vCPU %d: %s",
+                     cpu->cpu_index, strerror(err));
+        abort();
+    }
+}
+
 static void do_mshv_cpu_synchronize_post_init(CPUState *cpu,
                                               run_on_cpu_data arg)
 {
@@ -996,6 +1014,7 @@ static void mshv_accel_ops_class_init(ObjectClass *oc, const void *data)
     AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
 
     ops->create_vcpu_thread = mshv_start_vcpu_thread;
+    ops->kick_vcpu_thread = mshv_kick_vcpu_thread;
     ops->cpu_thread_is_idle = mshv_vcpu_thread_is_idle;
     ops->synchronize_post_init = mshv_cpu_synchronize_post_init;
     ops->synchronize_post_reset = mshv_cpu_synchronize_post_reset;
Signature · 1 line
2.34.1