Commit f01645dab1 for qemu.org

commit f01645dab1e891e4e3cbf2fc30be442ae2099ad4
Author: NickSampanis <nicksampanis@gmail.com>
Date:   Mon Oct 26 01:18:01 2026 +0200

    target/i386/tcg: Fix 64-bit RSP truncation on retf in 64-bit mode

    When executing retf in 64-bit mode with a 16- or 32-bit operand size,
    `helper_ret_protected()` is invoked with a shift of 0 or 1. However,
    in 64-bit mode, operand size should only govern the number of bytes
    that are popped, not how wide the stack pointer is because in 64-bit
    mode the SS attributes are ignored.

    By falling back to `get_sp_mask(SS.flags)`, QEMU applied a 16-bit mask
    (`0xffff`) with `SS.B` clear, and a 32-bit mask (`0xffffffff`) with
    `SS.B` set.  As a result, a 64-bit RSP pointing above 4 GiB was
    truncated. As observed in testing, the truncated stack pop read the wrong
    address, loading corrupted values for the new CS and RIP which resulted
    in a `#GP` fault and then a triple fault.

    Fix this by ensuring that `helper_ret_protected()` uses the full 64-bit
    stack pointer with no mask applied whenever the active code segment (CS)
    is running in 64-bit mode.

    Signed-off-by: NickSampanis <nicksampanis@gmail.com>
    Link: https://lore.kernel.org/r/20261025231803.870-2-nicksampanis@gmail.com
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c
index 45bffcd738..af18aec694 100644
--- a/target/i386/tcg/seg_helper.c
+++ b/target/i386/tcg/seg_helper.c
@@ -2056,7 +2056,7 @@ static inline void helper_ret_protected(CPUX86State *env, int shift,
     sa.mmu_index = x86_mmu_index_pl(env, cpl);

 #ifdef TARGET_X86_64
-    if (shift == 2) {
+    if (shift == 2 || (env->hflags & HF_CS64_MASK)) {
         sa.sp_mask = -1;
     } else
 #endif