[PATCH v2 12/13] whpx: i386: workaround for segment granularity reading as 0

Mohamed Mediouni posted 13 patches 1 week, 5 days ago
Maintainers: Roman Bolshakov <rbolshakov@ddn.com>, Phil Dennis-Jordan <phil@philjordan.eu>, Wei Liu <wei.liu@kernel.org>, Pedro Barbuda <pbarbuda@microsoft.com>, Mohamed Mediouni <mohamed@unpredictable.fr>
There is a newer version of this series
[PATCH v2 12/13] whpx: i386: workaround for segment granularity reading as 0
Posted by Mohamed Mediouni 1 week, 5 days ago
OS boot triggers a Cs segment limit assertion, while
Hyper-V reports that Granularity = 0 despite the
limit being 0xffffffff.

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

diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c
index f278a52451..d87afceb08 100644
--- a/target/i386/whpx/whpx-all.c
+++ b/target/i386/whpx/whpx-all.c
@@ -916,6 +916,25 @@ static void read_segment_descriptor(CPUState *cpu,
     WHV_X64_SEGMENT_REGISTER reg;
     whpx_read_segment_descriptor(cpu, &reg, seg_idx);
     whpx_segment_to_x86_descriptor(cpu, &reg, desc);
+
+    /*
+     * Workaround: vcpu->exit_ctx.VpContext.Cs.Granularity is 0...
+     *
+     * OS boot triggers a Cs segment limit assertion, while
+     * Hyper-V reports that Granularity = 0 despite the
+     * limit being 0xffffffff.
+     *
+     * This particular issue is much easier to trigger with
+     * the instruction_stream logic disabled. With that enabled,
+     * plenty of guests boot just fine as they don't trigger the
+     * immediate in CS read from a trapped instruction case.
+     *
+     * However, 32-bit Linux on AMD triggers specifically in
+     * https://lore.kernel.org/20250422234830.2840784-6-superm1@kernel.org
+     */
+    if (seg_idx == R_CS) {
+        desc->g = 1;
+    }
 }
 
 static bool is_protected_mode(CPUState *cpu)
-- 
2.50.1 (Apple Git-155)