public inbox for qemu-devel@nongnu.org
 help / color / mirror / Atom feed
From: Mohamed Mediouni <mohamed@unpredictable.fr>
To: qemu-devel@nongnu.org
Cc: Roman Bolshakov <rbolshakov@ddn.com>,
	Wei Liu <wei.liu@kernel.org>,
	Pedro Barbuda <pbarbuda@microsoft.com>,
	Mohamed Mediouni <mohamed@unpredictable.fr>,
	Phil Dennis-Jordan <phil@philjordan.eu>
Subject: [PATCH v2 11/13] whpx: i386: fetch segments on-demand
Date: Mon, 23 Mar 2026 23:34:32 +0100	[thread overview]
Message-ID: <20260323223434.81780-12-mohamed@unpredictable.fr> (raw)
In-Reply-To: <20260323223434.81780-1-mohamed@unpredictable.fr>

Instead of save/restore, fetch segments dynamically.

Rely on the fetched state instead of loading from memory.

Or, if available, on the VM exit context.

Signed-off-by: Mohamed Mediouni <mohamed@unpredictable.fr>
---
 target/i386/whpx/whpx-all.c | 55 +++++++++++++++++++++++++++++--------
 1 file changed, 44 insertions(+), 11 deletions(-)

diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c
index 71b33a632a..f278a52451 100644
--- a/target/i386/whpx/whpx-all.c
+++ b/target/i386/whpx/whpx-all.c
@@ -868,21 +868,54 @@ static int whpx_handle_portio(CPUState *cpu,
     return 0;
 }
 
+static void whpx_segment_to_x86_descriptor(CPUState *cpu, WHV_X64_SEGMENT_REGISTER* reg,
+                                   struct x86_segment_descriptor *desc)
+{
+    x86_set_segment_limit(desc, reg->Limit);
+    x86_set_segment_base(desc, reg->Base);
+
+    desc->type = reg->SegmentType;
+    desc->s = reg->NonSystemSegment;
+    desc->dpl = reg->DescriptorPrivilegeLevel;
+    desc->p = reg->Present;
+    desc->avl = reg->Available;
+    desc->l = reg->Long;
+    desc->db = reg->Default;
+    desc->g = reg->Granularity;
+}
+
+static void whpx_read_segment_descriptor(CPUState *cpu, WHV_X64_SEGMENT_REGISTER* reg,
+                                    X86Seg seg)
+{
+    AccelCPUState *vcpu = cpu->accel;
+    WHV_REGISTER_NAME reg_name = WHvX64RegisterEs + seg;
+    WHV_REGISTER_VALUE val;
+
+    if (seg == R_CS) {
+        *reg = vcpu->exit_ctx.VpContext.Cs;
+        return;
+    }
+    if (vcpu->exit_ctx.ExitReason == WHvRunVpExitReasonX64IoPortAccess) {
+        if (seg == R_DS) {
+            *reg = vcpu->exit_ctx.IoPortAccess.Ds;
+            return;
+        } else if (seg == R_ES) {
+            *reg = vcpu->exit_ctx.IoPortAccess.Es;
+            return;
+        }
+    }
+
+    whpx_get_reg(cpu, reg_name, &val);
+    *reg = val.Segment;
+}
+
 static void read_segment_descriptor(CPUState *cpu,
                                     struct x86_segment_descriptor *desc,
                                     enum X86Seg seg_idx)
 {
-    bool ret;
-    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 };
-
-    ret = x86_read_segment_descriptor(cpu, desc, sel);
-    if (ret == false) {
-        error_report("failed to read segment descriptor");
-        abort();
-    }
+    WHV_X64_SEGMENT_REGISTER reg;
+    whpx_read_segment_descriptor(cpu, &reg, seg_idx);
+    whpx_segment_to_x86_descriptor(cpu, &reg, desc);
 }
 
 static bool is_protected_mode(CPUState *cpu)
-- 
2.50.1 (Apple Git-155)



  parent reply	other threads:[~2026-03-23 22:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-23 22:34 [PATCH v2 00/13] whpx: i386: Windows 10 and performance fixes Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 01/13] whpx: i386: workaround for Windows 10 support Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 02/13] whpx: i386: enable exceptions VM exit only when needed Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 03/13] whpx: i386: skip TSC read for MMIO exits Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 04/13] whpx: i386: skip XCRs " Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 05/13] whpx: i386: don't restore segment registers after MMIO handling Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 06/13] target/i386: emulate: add new callbacks Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 07/13] whpx: i386: add implementation of new x86_emul_ops Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 08/13] target/i386: emulate: indirect access to CRs Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 09/13] whpx: i386: " Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 10/13] target/i386: emulate: segmentation rework Mohamed Mediouni
2026-03-23 22:34 ` Mohamed Mediouni [this message]
2026-03-23 22:34 ` [PATCH v2 12/13] whpx: i386: workaround for segment granularity reading as 0 Mohamed Mediouni
2026-03-23 22:34 ` [PATCH v2 13/13] whpx: i386: fast runtime state reads Mohamed Mediouni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260323223434.81780-12-mohamed@unpredictable.fr \
    --to=mohamed@unpredictable.fr \
    --cc=pbarbuda@microsoft.com \
    --cc=phil@philjordan.eu \
    --cc=qemu-devel@nongnu.org \
    --cc=rbolshakov@ddn.com \
    --cc=wei.liu@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox