Skip to contentSkip to conversation

whpx: i386: interrupt-related updates

PATCH Applied Mohamed Mediouni 3 participants 6 messages Feb 26

lore
Share & tools
lore
#0 lore raw

cover whpx: i386: interrupt-related updates

Kick the CPU out of HLT manually after an interrupt has been raised through the non-MSI path.

Make legacy Windows guests (Windows XP) not run very slowly when kernel-irqchip=off. Windows XP doesn't currently run with kernel-irqchip=on on WHP. Previously sent this as a standalone patch, removed some dangling space characters.

Mohamed Mediouni (3):
whpx: x86: remove inaccurate comment
whpx: x86: kick out of HLT manually when using the kernel-irqchip
hw: i386: vapic: enable on WHPX with user-mode irqchip

 hw/i386/vapic.c             | 25 +++++++++++++++++++------
 target/i386/whpx/whpx-all.c | 32 ++++++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 8 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/3 whpx: x86: remove inaccurate comment

WHvRunVpExitReasonX64Halt _is_ triggered on halt with kernel-irqchip=off as of Windows 11 version 25H2.

---
 target/i386/whpx/whpx-all.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
target/i386/whpx/whpx-all.c +1 -2
index 8210250dc3..bd3a1c49c6 100644
@@ -1648,8 +1648,7 @@ int whpx_vcpu_run(CPUState *cpu)
 
         case WHvRunVpExitReasonX64Halt:
             /*
-             * WARNING: as of build 19043.1526 (21H1), this exit reason is no
-             * longer used.
+             * Used for kernel-irqchip=off
              */
             ret = whpx_handle_halt(cpu);
             break;
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH 2/3 whpx: x86: kick out of HLT manually when using the kernel-irqchip

In reply to Mohamed Mediouni

Otherwise, interrupts processed through the cancel vCPU and inject path will not cause the vCPU to go out of its halt state.

---
 target/i386/whpx/whpx-all.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
target/i386/whpx/whpx-all.c +29 -0
index bd3a1c49c6..650d65c61e 100644
@@ -1475,6 +1475,16 @@ static void whpx_vcpu_post_run(CPUState *cpu)
         !vcpu->exit_ctx.VpContext.ExecutionState.InterruptShadow;
 }
 
+static void whpx_vcpu_kick_out_of_hlt(CPUState *cpu) 
+{
+    WHV_REGISTER_VALUE reg;
+    whpx_get_reg(cpu, WHvRegisterInternalActivityState, &reg);
+    if (reg.InternalActivity.HaltSuspend) {
+        reg.InternalActivity.HaltSuspend = 0;
+        whpx_set_reg(cpu, WHvRegisterInternalActivityState, reg);
+    }
+}
+
 static void whpx_vcpu_process_async_events(CPUState *cpu)
 {
     X86CPU *x86_cpu = X86_CPU(cpu);
@@ -1760,6 +1770,25 @@ int whpx_vcpu_run(CPUState *cpu)
                 cpu->exception_index = EXCP_INTERRUPT;
                 ret = 1;
             }
+            /* 
+             * When the Hyper-V APIC is enabled, to get out of HLT we
+             * either have to request an interrupt or manually get it away
+             * from HLT.
+             *
+             * We also manually do inject some interrupts via WHvRegisterPendingEvent
+             * instead of WHVRequestInterrupt, which does not reset the HLT state.
+             *
+             * However, even with this done, if the guest does an HLT without
+             * interrupts enabled (which the test_sti_inhibit KVM unit test does)
+             * then the guest will stay in HLT forever.
+             *
+             * Keep it this way for now, with perhaps adding a heartbeat later
+             * so that we get the CPU time savings from having Hyper-V handle HLT
+             * instead of going away from it as soon as possible.
+             */
+            if (whpx_irqchip_in_kernel()) {
+                whpx_vcpu_kick_out_of_hlt(cpu);
+            }
             break;
         case WHvRunVpExitReasonX64MsrAccess: {
             WHV_REGISTER_VALUE reg_values[3] = {0};
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

Re: [PATCH 2/3] whpx: x86: kick out of HLT manually when using the kernel-irqchip

Am 26. Februar 2026 18:19:29 UTC schrieb Mohamed Mediouni <[email protected]>:

Quoted 1 line

Otherwise, interrupts processed through the cancel vCPU and inject path will not cause the vCPU to go out of its halt state.

Yay, thanks for tackling this!

Quoted 2 lines

It probably resolves https://gitlab.com/qemu-project/qemu/-/issues/346 , though that would need to be confirmed by testing.

Best regards,
Bernhard

Quoted 51 lines

---
target/i386/whpx/whpx-all.c | 29 +++++++++++++++++++++++++++++

1 file changed, 29 insertions(+)
target/i386/whpx/whpx-all.c +29 -0
index bd3a1c49c6..650d65c61e 100644
@@ -1475,6 +1475,16 @@ static void whpx_vcpu_post_run(CPUState *cpu)
        !vcpu->exit_ctx.VpContext.ExecutionState.InterruptShadow;
 }
 
+static void whpx_vcpu_kick_out_of_hlt(CPUState *cpu) 
+{
+    WHV_REGISTER_VALUE reg;
+    whpx_get_reg(cpu, WHvRegisterInternalActivityState, &reg);
+    if (reg.InternalActivity.HaltSuspend) {
+        reg.InternalActivity.HaltSuspend = 0;
+        whpx_set_reg(cpu, WHvRegisterInternalActivityState, reg);
+    }
+}
+
 static void whpx_vcpu_process_async_events(CPUState *cpu)
 {
    X86CPU *x86_cpu = X86_CPU(cpu);
@@ -1760,6 +1770,25 @@ int whpx_vcpu_run(CPUState *cpu)
                cpu->exception_index = EXCP_INTERRUPT;
                ret = 1;
            }
+            /* 
+             * When the Hyper-V APIC is enabled, to get out of HLT we
+             * either have to request an interrupt or manually get it away
+             * from HLT.
+             *
+             * We also manually do inject some interrupts via WHvRegisterPendingEvent
+             * instead of WHVRequestInterrupt, which does not reset the HLT state.
+             *
+             * However, even with this done, if the guest does an HLT without
+             * interrupts enabled (which the test_sti_inhibit KVM unit test does)
+             * then the guest will stay in HLT forever.
+             *
+             * Keep it this way for now, with perhaps adding a heartbeat later
+             * so that we get the CPU time savings from having Hyper-V handle HLT
+             * instead of going away from it as soon as possible.
+             */
+            if (whpx_irqchip_in_kernel()) {
+                whpx_vcpu_kick_out_of_hlt(cpu);
+            }
            break;
        case WHvRunVpExitReasonX64MsrAccess: {
            WHV_REGISTER_VALUE reg_values[3] = {0};
#4 lore raw

PATCH 3/3 hw: i386: vapic: enable on WHPX with user-mode irqchip

In reply to Mohamed Mediouni

Alleviate a performance bottleneck on legacy Windows guests.

In my test setup, this makes Windows XP boot times be 20x faster than they're otherwise.

---
 hw/i386/vapic.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
hw/i386/vapic.c +19 -6
index 670a50524d..35e28428ff 100644
@@ -16,6 +16,7 @@
 #include "system/cpus.h"
 #include "system/hw_accel.h"
 #include "system/kvm.h"
+#include "system/whpx.h"
 #include "system/runstate.h"
 #include "system/address-spaces.h"
 #include "hw/i386/apic_internal.h"
@@ -229,7 +230,8 @@ static int evaluate_tpr_instruction(VAPICROMState *s, X86CPU *cpu,
         return -1;
     }
 
-    if (kvm_enabled() && !kvm_irqchip_in_kernel()) {
+    if ((kvm_enabled() && !kvm_irqchip_in_kernel())
+        || (whpx_enabled() && !whpx_irqchip_in_kernel())) {
         /*
          * KVM without kernel-based TPR access reporting will pass an IP that
          * points after the accessing instruction. So we need to look backward
@@ -549,7 +551,7 @@ static int patch_hypercalls(VAPICROMState *s)
     cpu_physical_memory_read(rom_paddr, rom, s->rom_size);
 
     for (pos = 0; pos < s->rom_size - sizeof(vmcall_pattern); pos++) {
-        if (kvm_irqchip_in_kernel()) {
+        if (kvm_enabled() && kvm_irqchip_in_kernel()) {
             pattern = outl_pattern;
             alternates[0] = outl_pattern[7];
             alternates[1] = outl_pattern[7];
@@ -679,16 +681,26 @@ static void vapic_write(void *opaque, hwaddr addr, uint64_t data,
         }
         break;
     case 1:
-        if (kvm_enabled()) {
+        if (kvm_enabled() || (whpx_enabled() && !whpx_irqchip_in_kernel())) {
             /*
              * Disable triggering instruction in ROM by writing a NOP.
              *
              * We cannot do this in TCG mode as the reported IP is not
              * accurate.
+             *
+             * Oddly enough, KVM increments EIP _before_ the execution
+             * of the instruction is finished.
              */
             pause_all_vcpus();
-            patch_byte(cpu, env->eip - 2, 0x66);
-            patch_byte(cpu, env->eip - 1, 0x90);
+            if (!kvm_enabled()) {
+                patch_byte(cpu, env->eip, 0x66);
+                patch_byte(cpu, env->eip + 1, 0x90);
+            }
+            else {
+                patch_byte(cpu, env->eip - 2, 0x66);
+                patch_byte(cpu, env->eip - 1, 0x90);
+
+            }
             resume_all_vcpus();
         }
 
@@ -705,7 +717,8 @@ static void vapic_write(void *opaque, hwaddr addr, uint64_t data,
         break;
     default:
     case 4:
-        if (!kvm_irqchip_in_kernel()) {
+        if ((kvm_enabled() && !kvm_irqchip_in_kernel())
+          || (whpx_enabled() && !whpx_irqchip_in_kernel())) {
             apic_poll_irq(cpu->apic_state);
         }
         break;
Signature · 1 line
2.50.1 (Apple Git-155)