Skip to contentSkip to conversation

whpx: i386: even more updates

PATCH Superseded Mohamed Mediouni 2 participants 10 messages Mar 26 – Mar 27 Arm/Amlogic Meson SoC support

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover whpx: i386: even more updates

Demoting some warnings that appeared too often
and adding some documentation.

Also add emulation of SLDT (and STR done in the same vein) because of a bug report.

Mohamed Mediouni (6):
docs: add WHPX section with initial info
whpx: i386: trace unsupported MSR accesses target/i386: emulate: remove redundant logging for unmapped MMIO
access
target/i386/emulate, whpx, hvf, mshv: add read_segment_selector target/i386: emulate: include name of unhandled instruction target/i386: emulate: emulate SLDT and STR

 MAINTAINERS                   |   1 +
 docs/system/index.rst         |   1 +
 docs/system/whpx.rst          | 144 ++++++++++++++++++++++++++++++++++
 meson.build                   |   1 +
 target/i386/emulate/x86_emu.c |  22 +++++-
 target/i386/emulate/x86_emu.h |   1 +
 target/i386/emulate/x86_mmu.c |   3 -
 target/i386/hvf/hvf.c         |   1 +
 target/i386/mshv/mshv-cpu.c   |  11 +++
 target/i386/whpx/trace-events |   1 +
 target/i386/whpx/trace.h      |   2 +
 target/i386/whpx/whpx-all.c   |  15 +++-
 12 files changed, 196 insertions(+), 7 deletions(-)
 create mode 100644 docs/system/whpx.rst
 create mode 100644 target/i386/whpx/trace-events
 create mode 100644 target/i386/whpx/trace.h
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH v5 1/6 docs: add WHPX section with initial info

---
 MAINTAINERS           |   1 +
 docs/system/index.rst |   1 +
 docs/system/whpx.rst  | 144 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+)
 create mode 100644 docs/system/whpx.rst
MAINTAINERS +1 -0
index cd8ba14450..eb7132e39d 100644
@@ -582,6 +582,7 @@ F: include/system/whpx.h
 F: include/system/whpx-accel-ops.h
 F: include/system/whpx-common.h
 F: include/system/whpx-internal.h
+F: docs/system/whpx.rst
 
 MSHV
 M: Magnus Kulke <[email protected]>
docs/system/index.rst +1 -0
index bb948e2993..4509630fa4 100644
@@ -40,6 +40,7 @@ or Hypervisor.Framework.
    confidential-guest-support
    igvm
    nitro
+   whpx
    vm-templating
    sriov
    qemu-colo
docs/system/whpx.rst new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH v5 2/6 whpx: i386: trace unsupported MSR accesses

In reply to Mohamed Mediouni

Not actionable information for users, so stop
having it displayed unconditionally.

---
 meson.build                   | 1 +
 target/i386/whpx/trace-events | 1 +
 target/i386/whpx/trace.h      | 2 ++
 target/i386/whpx/whpx-all.c   | 5 +++--
 4 files changed, 7 insertions(+), 2 deletions(-)
 create mode 100644 target/i386/whpx/trace-events
 create mode 100644 target/i386/whpx/trace.h
meson.build +1 -0
index e6f17dd5f7..c19c5afa46 100644
@@ -3692,6 +3692,7 @@ if have_system or have_user
     'target/hppa',
     'target/i386',
     'target/i386/kvm',
+    'target/i386/whpx',
     'target/loongarch',
     'target/mips/tcg',
     'target/ppc',
target/i386/whpx/trace-events new file +1 -0
new file mode 100644
index 0000000000..ebdfa34b28
@@ -0,0 +1 @@
+whpx_unsupported_msr_access(uint32_t msr, int is_write) "WHPX: Unsupported MSR access (0x%x), IsWrite=%i"
target/i386/whpx/trace.h new file +2 -0
new file mode 100644
index 0000000000..b7c090deff
@@ -0,0 +1,2 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "trace/trace-target_i386_whpx.h"
target/i386/whpx/whpx-all.c +3 -2
index acae61e089..e56ae2b343 100644
@@ -41,6 +41,7 @@
 #include "emulate/x86_emu.h"
 #include "emulate/x86_flags.h"
 #include "emulate/x86_mmu.h"
+#include "trace.h"
 
 #include <winhvplatform.h>
 
@@ -1921,8 +1922,8 @@ int whpx_vcpu_run(CPUState *cpu)
                         1 : 3;
 
             if (!is_known_msr) {
-                warn_report("WHPX: Unsupported MSR access (0x%x), IsWrite=%i", 
-                vcpu->exit_ctx.MsrAccess.MsrNumber, vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite);
+                trace_whpx_unsupported_msr_access(vcpu->exit_ctx.MsrAccess.MsrNumber,
+                    vcpu->exit_ctx.MsrAccess.AccessInfo.IsWrite);
             }
 
             hr = whp_dispatch.WHvSetVirtualProcessorRegisters(
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

PATCH v5 3/6 target/i386: emulate: remove redundant logging for unmapped MMIO access

In reply to Mohamed Mediouni

ReactOS's install ISO does a bunch of 4-byte accesses to 0xffdff124. This doesn't happen for the boot ISO.

It looks to be an access relative to the Windows KPCR which is at 0xffdff000 but mistakenly done prior to paging being on...

As this logging is redundant with -d invalid_mem,
remove it.

https://geoffchappell.com/studies/windows/km/ntoskrnl/inc/ntos/i386_x/kpcr.htm

---
 target/i386/emulate/x86_mmu.c | 3 ---
 1 file changed, 3 deletions(-)
target/i386/emulate/x86_mmu.c +0 -3
index c69ae96acb..007de582de 100644
@@ -21,7 +21,6 @@
 #include "cpu.h"
 #include "system/address-spaces.h"
 #include "system/memory.h"
-#include "qemu/error-report.h"
 #include "emulate/x86.h"
 #include "emulate/x86_emu.h"
 #include "emulate/x86_mmu.h"
@@ -287,7 +286,6 @@ static MMUTranslateResult x86_write_mem_ex(CPUState *cpu, void *data, target_ulo
                             MEMTXATTRS_UNSPECIFIED, data, copy);
 
         if (mem_tx_res == MEMTX_DECODE_ERROR) {
-            warn_report("write to unmapped mmio region gpa=0x%" PRIx64 " size=%i", gpa, bytes);
             return MMU_TRANSLATE_GPA_UNMAPPED;
         } else if (mem_tx_res == MEMTX_ACCESS_ERROR) {
             return MMU_TRANSLATE_GPA_NO_WRITE_ACCESS;
@@ -339,7 +337,6 @@ static MMUTranslateResult x86_read_mem_ex(CPUState *cpu, void *data, target_ulon
                            data, copy);
 
         if (mem_tx_res == MEMTX_DECODE_ERROR) {
-            warn_report("read from unmapped mmio region gpa=0x%" PRIx64 " size=%i", gpa, bytes);
             return MMU_TRANSLATE_GPA_UNMAPPED;
         } else if (mem_tx_res == MEMTX_ACCESS_ERROR) {
             return MMU_TRANSLATE_GPA_NO_READ_ACCESS;
Signature · 1 line
2.50.1 (Apple Git-155)
#4 lore raw

Re: [PATCH v5 3/6] target/i386: emulate: remove redundant logging for unmapped MMIO access

Quoted 10 lines · Mohamed Mediouni

ReactOS's install ISO does a bunch of 4-byte accesses to 0xffdff124. This doesn't happen for the boot ISO.

It looks to be an access relative to the Windows KPCR which is at 0xffdff000 but mistakenly done prior to paging being on...

As this logging is redundant with -d invalid_mem,
remove it.

It's not obvious why it's redundant, I don't see another mmio write path from exec_movs_single.

Quoted 7 lines

https://geoffchappell.com/studies/windows/km/ntoskrnl/inc/ntos/i386_x/kpcr.htm

---
  target/i386/emulate/x86_mmu.c | 3 ---
  1 file changed, 3 deletions(-)
#5 lore raw

Re: [PATCH v5 3/6] target/i386: emulate: remove redundant logging for unmapped MMIO access

Quoted 14 lines · Pierrick Bouvier
Quoted 8 lines · Mohamed Mediouni

ReactOS's install ISO does a bunch of 4-byte accesses to 0xffdff124. This doesn't happen for the boot ISO. It looks to be an access relative to the Windows KPCR which is at 0xffdff000 but mistakenly done prior to paging being on...
As this logging is redundant with -d invalid_mem, remove it.

Quoted 1 line

It's not obvious why it's redundant, I don't see another mmio write path from exec_movs_single.

Hello,

It’s a bit long but it gets there :)

For example on the read path:

exec_movs_single -> read_val_from_mem -> read_mmio -> x86_read_mem

x86_read_mem_ex -> address_space_read -> flatview_read_continue or address_space_read_full

address_space_read_full -> flatview_read -> flatview_read_continue -> flatview_read_continue_step -> memory_region_dispatch_read -> memory_region_access_valid

and memory_region_access_valid raises that error message with -d invalid_mem

And on the write side it’s fairly similar, with it ending on memory_region_access_valid at the end.

It’s a bit of a labyrinth though.

Quoted 6 lines
Quoted 6 lines

https://geoffchappell.com/studies/windows/km/ntoskrnl/inc/ntos/i386_x/kpcr.htm

---
 target/i386/emulate/x86_mmu.c | 3 ---
 1 file changed, 3 deletions(-)
#6 lore raw

Re: [PATCH v5 3/6] target/i386: emulate: remove redundant logging for unmapped MMIO access

Quoted 22 lines · Mohamed Mediouni
Quoted 14 lines · Pierrick Bouvier
Quoted 8 lines · Mohamed Mediouni

ReactOS's install ISO does a bunch of 4-byte accesses to 0xffdff124. This doesn't happen for the boot ISO. It looks to be an access relative to the Windows KPCR which is at 0xffdff000 but mistakenly done prior to paging being on...
As this logging is redundant with -d invalid_mem, remove it.

Quoted 1 line

It's not obvious why it's redundant, I don't see another mmio write path from exec_movs_single.

Hello,

It’s a bit long but it gets there :)

For example on the read path:

Quoted 8 lines

exec_movs_single -> read_val_from_mem -> read_mmio -> x86_read_mem

x86_read_mem_ex -> address_space_read -> flatview_read_continue or address_space_read_full

address_space_read_full -> flatview_read -> flatview_read_continue -> flatview_read_continue_step -> memory_region_dispatch_read -> memory_region_access_valid

and memory_region_access_valid raises that error message with -d invalid_mem

I didn't try -d invalid_mem on x86_64-softmmu, but on aarch64-softmmu, it can be more noisy than guest_errors.
In this case, your analysis proved the message was a proper guest_error in ReactOS, so I think it's worth keeping the warning but in a different category (guest_errors).

Quoted 12 lines

And on the write side it’s fairly similar, with it ending on memory_region_access_valid at the end.

It’s a bit of a labyrinth though.

Quoted 6 lines
Quoted 6 lines

https://geoffchappell.com/studies/windows/km/ntoskrnl/inc/ntos/i386_x/kpcr.htm

---
  target/i386/emulate/x86_mmu.c | 3 ---
  1 file changed, 3 deletions(-)

Regards,
Pierrick

#7 lore raw

PATCH v5 4/6 target/i386/emulate, whpx, hvf, mshv: add read_segment_selector

In reply to Mohamed Mediouni

Opens the door to implement SLDT in the instruction emulator.

---
 target/i386/emulate/x86_emu.h |  1 +
 target/i386/hvf/hvf.c         |  1 +
 target/i386/mshv/mshv-cpu.c   | 11 +++++++++++
 target/i386/whpx/whpx-all.c   | 10 ++++++++++
 4 files changed, 23 insertions(+)
target/i386/emulate/x86_emu.h +1 -0
index a8d4c93098..2397c24200 100644
@@ -28,6 +28,7 @@ struct x86_emul_ops {
     MMUTranslateResult (*mmu_gva_to_gpa) (CPUState *cpu, target_ulong gva, uint64_t *gpa, MMUTranslateFlags flags);
     void (*read_segment_descriptor)(CPUState *cpu, struct x86_segment_descriptor *desc,
                                     enum X86Seg seg);
+    x86_segment_selector (*read_segment_selector)(CPUState *cpu, enum X86Seg seg);
     target_ulong (*read_cr) (CPUState *cpu, int cr);
     void (*handle_io)(CPUState *cpu, uint16_t port, void *data, int direction,
                       int size, int count);
target/i386/hvf/hvf.c +1 -0
index c0d028b147..8fda4429b3 100644
@@ -254,6 +254,7 @@ static void hvf_read_segment_descriptor(CPUState *s, struct x86_segment_descript
 
 static const struct x86_emul_ops hvf_x86_emul_ops = {
     .read_segment_descriptor = hvf_read_segment_descriptor,
+    .read_segment_selector = vmx_read_segment_selector,
     .handle_io = hvf_handle_io,
     .simulate_rdmsr = hvf_simulate_rdmsr,
     .simulate_wrmsr = hvf_simulate_wrmsr,
target/i386/mshv/mshv-cpu.c +11 -0
index 2bc978deb2..25e0de2eab 100644
@@ -1565,8 +1565,19 @@ static void read_segment_descriptor(CPUState *cpu,
     }
 }
 
+static x86_segment_selector read_segment_selector(CPUState *cpu, enum X86Seg seg_idx)
+{
+    X86CPU *x86_cpu = X86_CPU(cpu);
+    CPUX86State *env = &x86_cpu->env;
+    SegmentCache *seg = &env->segs[seg_idx];
+    x86_segment_selector sel = { .sel = seg->selector & 0xFFFF };
+
+    return sel;
+}
+
 static const struct x86_emul_ops mshv_x86_emul_ops = {
     .read_segment_descriptor = read_segment_descriptor,
+    .read_segment_selector = read_segment_selector
 };
 
 void mshv_init_mmio_emu(void)
target/i386/whpx/whpx-all.c +10 -0
index e56ae2b343..9a405118c5 100644
@@ -998,6 +998,15 @@ static void read_segment_descriptor(CPUState *cpu,
     whpx_segment_to_x86_descriptor(cpu, &reg, desc);
 }
 
+static x86_segment_selector read_segment_selector(CPUState *cpu, enum X86Seg seg_idx)
+{
+    WHV_X64_SEGMENT_REGISTER reg;
+    x86_segment_selector sel;
+    whpx_read_segment_descriptor(cpu, &reg, seg_idx);
+    sel.sel = reg.Selector & 0xFFFF;
+    return sel;
+}
+
 static bool is_protected_mode(CPUState *cpu)
 {
     AccelCPUState *vcpu = cpu->accel;
@@ -1049,6 +1058,7 @@ static target_ulong read_cr(CPUState *cpu, int cr)
 
 static const struct x86_emul_ops whpx_x86_emul_ops = {
     .read_segment_descriptor = read_segment_descriptor,
+    .read_segment_selector = read_segment_selector,
     .handle_io = handle_io,
     .is_protected_mode = is_protected_mode,
     .is_long_mode = is_long_mode,
Signature · 1 line
2.50.1 (Apple Git-155)
#8 lore raw

PATCH v5 5/6 target/i386: emulate: include name of unhandled instruction

In reply to Mohamed Mediouni

Instead of just the command number, include the instruction name to make debugging easier.

---
 target/i386/emulate/x86_emu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/i386/emulate/x86_emu.c +2 -2
index 55b1a68eb6..c2da1a133f 100644
@@ -1399,8 +1399,8 @@ static void init_cmd_handler(void)
 bool exec_instruction(CPUX86State *env, struct x86_decode *ins)
 {
     if (!_cmd_handler[ins->cmd].handler) {
-        printf("Unimplemented handler (" TARGET_FMT_lx ") for %d (%x %x)\n",
-                env->eip,
+        printf("Unimplemented handler (" TARGET_FMT_lx ") for %s - %d (%x %x)\n",
+                env->eip, decode_cmd_to_string(ins->cmd),
                 ins->cmd, ins->opcode[0],
                 ins->opcode_len > 1 ? ins->opcode[1] : 0);
         env->eip += ins->len;
Signature · 1 line
2.50.1 (Apple Git-155)
#9 lore raw

PATCH v5 6/6 target/i386: emulate: emulate SLDT and STR

In reply to Mohamed Mediouni

Never saw that one being trapped and could not reproduce on AMD. But from the QEMU bug tracker, that looks like another story on Intel processors.

Present in "whpx_failures.txt" on
https://gitlab.com/qemu-project/qemu/-/work_items/3349#note_3195249254

---
 target/i386/emulate/x86_emu.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
target/i386/emulate/x86_emu.c +18 -0
index c2da1a133f..e6e6500ea5 100644
@@ -1338,6 +1338,22 @@ static bool exec_xadd(CPUX86State *env, struct x86_decode *decode)
     return 0;
 }
 
+static bool exec_sldt(CPUX86State *env, struct x86_decode *decode)
+{
+    x86_segment_selector seg = emul_ops->read_segment_selector(env_cpu(env), R_LDTR);
+    write_val_ext(env, &decode->op[0], seg.sel, 2);
+    env->eip += decode->len;
+    return 0;
+}
+
+static bool exec_str(CPUX86State *env, struct x86_decode *decode)
+{
+    x86_segment_selector seg = emul_ops->read_segment_selector(env_cpu(env), R_TR);
+    write_val_ext(env, &decode->op[0], seg.sel, 2);
+    env->eip += decode->len;
+    return 0;
+}
+
 static struct cmd_handler {
     enum x86_decode_cmd cmd;
     bool (*handler)(CPUX86State *env, struct x86_decode *ins);
@@ -1382,6 +1398,8 @@ static struct cmd_handler {
     {X86_DECODE_CMD_MOVSX, exec_movsx},
     {X86_DECODE_CMD_XCHG, exec_xchg},
     {X86_DECODE_CMD_XADD, exec_xadd},
+    {X86_DECODE_CMD_SLDT, exec_sldt},
+    {X86_DECODE_CMD_STR, exec_str}
 };
 
 static struct cmd_handler _cmd_handler[X86_DECODE_CMD_LAST];
Signature · 1 line
2.50.1 (Apple Git-155)