Skip to contentSkip to conversation

whpx: i386: some fixes

PATCH In review Mohamed Mediouni 3 participants 9 messages Aug 12 – Aug 24

lore
Share & tools
lore
#0 lore raw

cover whpx: i386: some fixes

Some (backportable) fixes for x86_64 whpx.

-cpu host on Intel CPUs works again without needing a -cpu host,vmx=off workaround.

A floating point state sync fix and re-injecting exceptions on debug outside of the int1 case.

Mohamed Mediouni (6):
whpx: i386: fix xsaves enablement in legacy probing path hw/i386: fw_cfg: do not set VMX feature control on WHPX
whpx: i386: enable fast hypercall output
whpx: i386: synchronise PAT too
whpx: i386: work around Hyper-V FP state oddities
whpx: i386: inject back db

 hw/i386/fw_cfg.c                   | 13 ++++++--
 target/i386/whpx/whpx-all.c        | 51 ++++++++++++++++++++----------
 target/i386/whpx/whpx-cpu-legacy.c |  2 +-
 3 files changed, 47 insertions(+), 19 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-156)
#1 lore raw

PATCH 1/6 whpx: i386: fix xsaves enablement in legacy probing path

Without this, Linux will crash in a configuration where xsaves is expected.

---
 target/i386/whpx/whpx-cpu-legacy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/whpx/whpx-cpu-legacy.c +1 -1
index d341e6f4fd..3fb5ed79e3 100644
@@ -113,7 +113,7 @@ uint32_t whpx_get_supported_cpuid_legacy(uint32_t func, uint32_t idx,
         if (idx == 0) {
             eax = supported_xcr0;
         } else if (idx == 1) {
-            eax &= CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1;
+            eax &= CPUID_XSAVE_XSAVEOPT | CPUID_XSAVE_XGETBV1 | CPUID_XSAVE_XSAVES;
             if (!whpx_has_xsaves()) {
                 eax &= ~CPUID_XSAVE_XSAVES;
             }
Signature · 1 line
2.54.0 (Apple Git-156)
#2 lore raw

PATCH 2/6 hw/i386: fw_cfg: do not set VMX feature control on WHPX

In reply to Mohamed Mediouni

Hyper-V doesn't allow setting this bit on build 26100.

---
 hw/i386/fw_cfg.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
hw/i386/fw_cfg.c +11 -2
index d422302c1c..5903994a7c 100644
@@ -13,6 +13,8 @@
  */
 
 #include "qemu/osdep.h"
+#include "system/mshv.h"
+#include "system/whpx.h"
 #include "system/numa.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/aml-build.h"
@@ -182,8 +184,15 @@ void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg)
     uint64_t *val;
 
     cpu_x86_cpuid(env, 1, 0, &unused, &unused, &ecx, &edx);
-    if (ecx & CPUID_EXT_VMX) {
-        feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
+
+    /*
+     * Hyper-V in 26100 disallows this bit to be set.
+     * Otherwise a #GP gets raised.
+     */
+    if (!(whpx_enabled())) {
+        if (ecx & CPUID_EXT_VMX) {
+            feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
+        }
     }
 
     if ((edx & (CPUID_EXT2_MCE | CPUID_EXT2_MCA)) ==
Signature · 1 line
2.54.0 (Apple Git-156)
#3 lore raw

PATCH 3/6 whpx: i386: enable fast hypercall output

In reply to Mohamed Mediouni

Already enabled on Arm so extending it to x86.

---
 target/i386/whpx/whpx-all.c | 1 +
 1 file changed, 1 insertion(+)
target/i386/whpx/whpx-all.c +1 -0
index 634d542821..1800ba8eb1 100644
@@ -3247,6 +3247,7 @@ int whpx_accel_init(AccelState *as, MachineState *ms)
 
     synthetic_features.Bank0.HypervisorPresent = 1;
     synthetic_features.Bank0.Hv1 = 1;
+    synthetic_features.Bank0.FastHypercallOutput = 1;
     synthetic_features.Bank0.AccessVpRunTimeReg = 1;
     synthetic_features.Bank0.AccessPartitionReferenceCounter = 1;
     synthetic_features.Bank0.AccessPartitionReferenceTsc = 1;
Signature · 1 line
2.54.0 (Apple Git-156)
#4 lore raw

PATCH 4/6 whpx: i386: synchronise PAT too

In reply to Mohamed Mediouni

https://github.com/cmspam/winq-emu hints that
this might be wanted for some use-cases and
it's a step towards full state sync.

---
 target/i386/whpx/whpx-all.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
target/i386/whpx/whpx-all.c +5 -7
index 1800ba8eb1..2739a80da3 100644
@@ -115,7 +115,7 @@ static const WHV_REGISTER_NAME whpx_register_names[] = {
 #ifdef TARGET_X86_64
     WHvX64RegisterKernelGsBase,
 #endif
-    /* WHvX64RegisterPat, */
+    WHvX64RegisterPat,
     WHvX64RegisterSysenterCs,
     WHvX64RegisterSysenterEip,
     WHvX64RegisterSysenterEsp,
@@ -624,9 +624,8 @@ void whpx_set_registers(CPUState *cpu, WHPXStateLevel level)
         assert(whpx_register_names[idx] == WHvX64RegisterKernelGsBase);
         vcxt.values[idx++].Reg64 = env->kernelgsbase;
 #endif
-
-        /* WHvX64RegisterPat - Skipped */
-
+        assert(whpx_register_names[idx] == WHvX64RegisterPat);
+        vcxt.values[idx++].Reg64 = env->pat;
         assert(whpx_register_names[idx] == WHvX64RegisterSysenterCs);
         vcxt.values[idx++].Reg64 = env->sysenter_cs;
         assert(whpx_register_names[idx] == WHvX64RegisterSysenterEip);
@@ -962,9 +961,8 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel level)
     assert(whpx_register_names[idx] == WHvX64RegisterKernelGsBase);
     env->kernelgsbase = vcxt.values[idx++].Reg64;
 #endif
-
-    /* WHvX64RegisterPat - Skipped */
-
+    assert(whpx_register_names[idx] == WHvX64RegisterPat);
+    env->pat = vcxt.values[idx++].Reg64;
     assert(whpx_register_names[idx] == WHvX64RegisterSysenterCs);
     env->sysenter_cs = vcxt.values[idx++].Reg64;
     assert(whpx_register_names[idx] == WHvX64RegisterSysenterEip);
Signature · 1 line
2.54.0 (Apple Git-156)
Reviewed-by on v1 4/6
#5 lore raw

Re: [PATCH 4/6] whpx: i386: synchronise PAT too

Quoted 3 lines · Mohamed Mediouni

https://github.com/cmspam/winq-emu hints that
this might be wanted for some use-cases and
it's a step towards full state sync.

The link might disappear or be updated, maybe include the relevant justification here?

Quoted 10 lines

Linux uses PAT to mark virtio-gpu / Venus shared memory as Write-Combining. Previously the partition's PAT was not synchronised with the guest, so the guest's MTRR/PAT cache-type computation could fall back to UC for memory that should be WC.

---
  target/i386/whpx/whpx-all.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
#6 lore raw

PATCH 5/6 whpx: i386: work around Hyper-V FP state oddities

In reply to Mohamed Mediouni

On Hyper-V looks like we need to fetch both the legacy and new state instead of being able to rely on xsave.

---
 target/i386/whpx/whpx-all.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/i386/whpx/whpx-all.c +3 -4
index 2739a80da3..56a8b16749 100644
@@ -614,9 +614,9 @@ void whpx_set_registers(CPUState *cpu, WHPXStateLevel level)
 
         if (whpx_is_xsave_enabled(cpu)) {
             whpx_set_xsave_state(cpu);
-        } else {
-            whpx_set_legacy_fp_registers(cpu, level);
         }
+        whpx_set_legacy_fp_registers(cpu, level);
+
         /* MSRs */
         assert(whpx_register_names[idx] == WHvX64RegisterEfer);
         vcxt.values[idx++].Reg64 = env->efer;
@@ -950,9 +950,8 @@ void whpx_get_registers(CPUState *cpu, WHPXStateLevel level)
 
     if (whpx_is_xsave_enabled(cpu)) {
         whpx_get_xsave_state(cpu);
-    } else {
-        whpx_get_legacy_fp_registers(cpu, level);
     }
+    whpx_get_legacy_fp_registers(cpu, level);
 
     /* MSRs */
     assert(whpx_register_names[idx] == WHvX64RegisterEfer);
Signature · 1 line
2.54.0 (Apple Git-156)
#7 lore raw

PATCH 6/6 whpx: i386: inject back db

In reply to Mohamed Mediouni

When we get a debug exception other than int1,
inject it back to the guest.

---
 target/i386/whpx/whpx-all.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
target/i386/whpx/whpx-all.c +26 -5
index 56a8b16749..113f79afb9 100644
@@ -1079,6 +1079,31 @@ static void whpx_inject_back_gpf(CPUState *cpu)
     whpx_set_reg(cpu, WHvRegisterPendingEvent, reg);
 }
 
+static void whpx_inject_back_db(CPUState *cpu)
+{
+    WHV_VP_EXCEPTION_CONTEXT *ctx = &cpu->accel->exit_ctx.VpException;
+    WHV_REGISTER_VALUE reg = {};
+
+    if (ctx->ExceptionInfo.SoftwareException) {
+        /* TODO */
+        warn_report("Was asked to inject software exception.");
+        return;
+    }
+
+    if (ctx->ExceptionType != EXCP01_DB) {
+        warn_report("Was asked to inject exception other than debug.");
+        return;
+    }
+
+    reg.ExceptionEvent.EventPending = 1;
+    reg.ExceptionEvent.EventType = WHvX64PendingEventException;
+    reg.ExceptionEvent.DeliverErrorCode = ctx->ExceptionInfo.ErrorCodeValid;
+    reg.ExceptionEvent.Vector = ctx->ExceptionType;
+    reg.ExceptionEvent.ErrorCode = ctx->ErrorCode;
+    reg.ExceptionEvent.ExceptionParameter = ctx->ExceptionParameter;
+    whpx_set_reg(cpu, WHvRegisterPendingEvent, reg);
+}
+
 static void handle_io(CPUState *env, uint16_t port, void *buffer,
                   int direction, int size, int count)
 {
@@ -2646,11 +2671,7 @@ int whpx_vcpu_run(CPUState *cpu)
             } else if ((vcpu->exit_ctx.VpException.ExceptionType ==
                         WHvX64ExceptionTypeDebugTrapOrFault) &&
                        !cpu_single_stepping(cpu)) {
-                /*
-                 * Just finished stepping over a breakpoint, but the
-                 * gdb does not expect us to do single-stepping.
-                 * Don't do anything special.
-                 */
+                whpx_inject_back_db(cpu);
                 cpu->exception_index = EXCP_INTERRUPT;
             } else {
                 /* Another exception or debug event. Report it to GDB. */
Signature · 1 line
2.54.0 (Apple Git-156)
#8 lore raw
In reply to Mohamed Mediouni
Quoted 8 lines · Mohamed Mediouni

Some (backportable) fixes for x86_64 whpx.

-cpu host on Intel CPUs works again without needing a -cpu host,vmx=off workaround.

A floating point state sync fix and re-injecting exceptions on debug outside of the int1 case.

Quoted 7 lines

Mohamed Mediouni (6):
whpx: i386: fix xsaves enablement in legacy probing path hw/i386: fw_cfg: do not set VMX feature control on WHPX
whpx: i386: enable fast hypercall output
whpx: i386: synchronise PAT too
whpx: i386: work around Hyper-V FP state oddities
whpx: i386: inject back db

I'm picking this patch set for qemu 11.1.x stable series.

Thanks,

/mjt