Skip to contentSkip to conversation

whpx: i386: fix accel init/hang on legacy Windows (build 17763)

PATCH New Akshey Taneja 1 participant 3 messages Oct 15

lore
Share & tools
lore
#0 lore raw

cover whpx: i386: fix accel init/hang on legacy Windows (build 17763)

Two WHPX capability/property queries were added without a legacy-OS guard and break "-accel whpx" on Windows 10 (build 17763):

1. WHvCapabilityCodeProcessorFeaturesBanks -> WHV_E_UNKNOWN_CAPABILITY (hr=0x80370300). whpx_accel_init() aborts with a misleading -ENOSPC ("No space left on device") instead of starting.
2. WHvPartitionPropertyCodeX64MsrExitBitmap -> WHV_E_UNKNOWN_PROPERTY (hr=0x80370302). Even made non-fatal, leaving x2APIC enabled with no MSR exits hangs the guest black-screen right after firmware hand-off, since x2APIC is MSR-based.

Both capabilities are Server 2022 (build 20348)+ only. No shipped release checked (8.1 through 11.1.1, including git master as of 2026-10-15) has a fix for both at once.

Patch 1 gates the banked capability/property query on whpx_is_legacy_os(), falling back to the original single-bank WHvCapabilityCodeProcessorFeatures query -- this restores the pre-8b87283ce4 behaviour on legacy hosts, same pattern already used for the perfmon capability in 68438cb3cb.

Patch 2 records whether the MSR exit bitmap property was accepted and, when it is not, applies x2apic=off so the guest falls back to MMIO based xAPIC, which is still intercepted without MSR exits.

Verified end-to-end on real hardware (Windows 10 Enterprise LTSC 17763, Intel i7-6700, non-admin build via MSYS2 UCRT64): WHPX accelerated boot to desktop in ~65s vs. ~600s under TCG, combined with "-machine q35,pic=off" (a runtime flag, not part of this series) to route around a separate PIC->APIC handover issue on this OS build.

Akshey Taneja (2):
whpx: i386: do not require ProcessorFeaturesBanks on legacy Windows
whpx: i386: fall back to xAPIC when the MSR exit bitmap is unavailable

 target/i386/whpx/whpx-all.c | 94 ++++++++++++++++++++++++++++---------
 1 file changed, 73 insertions(+), 21 deletions(-)
Signature · 1 line
2.50.1.windows.1
#1 lore raw

PATCH 1/2 whpx: i386: do not require ProcessorFeaturesBanks on legacy Windows

WHvCapabilityCodeProcessorFeaturesBanks and the matching partition property are only implemented from Windows Server 2022 (build 20348), the same cutoff already handled for the perfmon capability in commit 68438cb3cb ("whpx: i386: workaround for Windows 10 support").

On Windows 10 the capability query returns WHV_E_UNKNOWN_CAPABILITY (0x80370300) and whpx_accel_init() aborts with -ENOSPC, reported to the user as the misleading "failed to initialize whpx: No space left on device". This regressed Windows 10 hosts in 8b87283ce4 ("whpx: i386:
enable all supported host features").

Fall back to the original single-bank WHvCapabilityCodeProcessorFeatures on legacy hosts and skip the corresponding SetPartitionProperty, which restores the pre-8b87283ce4 behaviour there.

Reported-on: Windows 10 Enterprise LTSC 17763, Intel i7-6700

---
 target/i386/whpx/whpx-all.c | 61 +++++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 16 deletions(-)
target/i386/whpx/whpx-all.c +45 -16
index 113f79afb9..9a9fc7d974 100644
@@ -3193,13 +3193,35 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
     memset(&processor_features, 0, sizeof(WHV_PROCESSOR_FEATURES_BANKS));
     processor_features.BanksCount = 2;
 
-    hr = whp_dispatch.WHvGetCapability(
-        WHvCapabilityCodeProcessorFeaturesBanks, &processor_features,
-        sizeof(WHV_PROCESSOR_FEATURES_BANKS), &whpx_cap_size);
-    if (FAILED(hr)) {
-        error_report("WHPX: Failed to get processor features, hr=%08lx", hr);
-        ret = -ENOSPC;
-        goto error;
+    if (whpx_is_legacy_os()) {
+        /*
+         * WHvCapabilityCodeProcessorFeaturesBanks, like the perfmon capability
+         * probed above, is only implemented from Windows Server 2022 (build
+         * 20348) onwards. Fall back to the original single-bank capability,
+         * which Windows 10 does implement, and treat failure as non-fatal:
+         * the banked feature set is an optimisation, not a requirement.
+         */
+        WHV_PROCESSOR_FEATURES legacy_features = {0};
+
+        processor_features.BanksCount = 1;
+        hr = whp_dispatch.WHvGetCapability(
+            WHvCapabilityCodeProcessorFeatures, &legacy_features,
+            sizeof(legacy_features), &whpx_cap_size);
+        if (FAILED(hr)) {
+            warn_report("WHPX: Failed to get legacy processor features, "
+                        "hr=%08lx", hr);
+        } else {
+            processor_features.Bank0 = legacy_features;
+        }
+    } else {
+        hr = whp_dispatch.WHvGetCapability(
+            WHvCapabilityCodeProcessorFeaturesBanks, &processor_features,
+            sizeof(WHV_PROCESSOR_FEATURES_BANKS), &whpx_cap_size);
+        if (FAILED(hr)) {
+            error_report("WHPX: Failed to get processor features, hr=%08lx", hr);
+            ret = -ENOSPC;
+            goto error;
+        }
     }
 
     whpx_rdtsc_cap = processor_features.Bank0.RdtscpSupport;
@@ -3246,15 +3268,22 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
         }
     }
 
-    hr = whp_dispatch.WHvSetPartitionProperty(
-            whpx->partition,
-            WHvPartitionPropertyCodeProcessorFeaturesBanks,
-            &processor_features,
-            sizeof(WHV_PROCESSOR_FEATURES_BANKS));
-    if (FAILED(hr)) {
-        error_report("WHPX: Failed to set processor features, hr=%08lx", hr);
-        ret = -EINVAL;
-        goto error;
+    /*
+     * The matching partition property is also Windows Server 2022 and later.
+     * Legacy hosts simply keep the features the hypervisor gave them, which
+     * is how WHPX behaved before the banked feature set was introduced.
+     */
+    if (!whpx_is_legacy_os()) {
+        hr = whp_dispatch.WHvSetPartitionProperty(
+                whpx->partition,
+                WHvPartitionPropertyCodeProcessorFeaturesBanks,
+                &processor_features,
+                sizeof(WHV_PROCESSOR_FEATURES_BANKS));
+        if (FAILED(hr)) {
+            error_report("WHPX: Failed to set processor features, hr=%08lx", hr);
+            ret = -EINVAL;
+            goto error;
+        }
     }
 
 
Signature · 1 line
2.50.1.windows.1
#2 lore raw

PATCH 2/2 whpx: i386: fall back to xAPIC when the MSR exit bitmap is unavailable

In reply to Akshey Taneja

WHvPartitionPropertyCodeX64MsrExitBitmap is not implemented on Windows 10, where WHvSetPartitionProperty() returns WHV_E_UNKNOWN_PROPERTY (0x80370302) and whpx_accel_init() aborts with -EINVAL. QEMU started requesting it in ba1bda25b7 ("whpx: i386: x2apic emulation").

Treating the failure as non-fatal is not sufficient on its own:
whpx_default_props unconditionally enables x2apic, and x2APIC is reached through MSRs, so without MSR exits the userspace LAPIC never observes the guest's APIC accesses and the guest hangs immediately after the firmware hands over. Observed as a black screen with an Ubuntu 24.04 guest that otherwise boots in about 40 seconds.

Record whether the MSR exit bitmap was accepted and, when it was not, apply x2apic=off so the guest uses the MMIO based xAPIC, which is still intercepted normally.

Reported-on: Windows 10 Enterprise LTSC 17763, Intel i7-6700

---
 target/i386/whpx/whpx-all.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
target/i386/whpx/whpx-all.c +28 -5
index 9a9fc7d974..3e59aacf31 100644
@@ -57,7 +57,7 @@
 #define HV_X64_MSR_GUEST_IDLE           0x400000f0
 
 static bool is_modern_os = true;
-
+static bool whpx_msr_exits_available = true;
 static const WHV_REGISTER_NAME whpx_register_names[] = {
 
     /* X64 General purpose registers */
@@ -2889,6 +2889,16 @@ static PropValue whpx_default_props[] = {
     { NULL, NULL },
 };
 
+/*
+ * x2APIC is reached through MSRs, so emulating it in userspace requires the
+ * MSR exit bitmap. Hosts without that property must fall back to xAPIC,
+ * which is MMIO based and therefore still intercepted.
+ */
+static PropValue whpx_no_msr_exit_props[] = {
+    { "x2apic", "off" },
+    { NULL, NULL },
+};
+
 
 void whpx_cpu_instance_init(CPUState *cs)
 {
@@ -2896,7 +2906,8 @@ void whpx_cpu_instance_init(CPUState *cs)
     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
 
     host_cpu_instance_init(cpu);
-    x86_cpu_apply_props(cpu, whpx_default_props);
+    x86_cpu_apply_props(cpu, whpx_msr_exits_available ? whpx_default_props
+                                                      : whpx_no_msr_exit_props);
 
     if (xcc->max_features) {
         whpx_cpu_max_instance_init(cpu);
@@ -3344,10 +3355,22 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
             WHvPartitionPropertyCodeX64MsrExitBitmap,
             &prop,
             sizeof(WHV_PARTITION_PROPERTY));
+    /*
+     * Windows 10 does not implement this property. Without the MSR exits the
+     * guest simply gets the behaviour QEMU had before commit ba1bda25b7
+     * ("whpx: i386: x2apic emulation") started asking for them, so treat an
+     * unknown property as non-fatal rather than refusing to start.
+     */
     if (FAILED(hr)) {
-        error_report("WHPX: Failed to set MSR exit bitmap, hr=%08lx", hr);
-        ret = -EINVAL;
-        goto error;
+        if (hr == WHV_E_UNKNOWN_PROPERTY) {
+            whpx_msr_exits_available = false;
+            warn_report("WHPX: MSR exit bitmap unsupported on this Windows "
+                        "version, falling back to xAPIC");
+        } else {
+            error_report("WHPX: Failed to set MSR exit bitmap, hr=%08lx", hr);
+            ret = -EINVAL;
+            goto error;
+        }
     }
 
     hr = whp_dispatch.WHvSetPartitionProperty(
Signature · 1 line
2.50.1.windows.1