[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen stable-4.21] x86emul: Don't adjust the memory operand of BT* with an immediate offset



commit 466c383c49f3465c1655044098d33a749d3255e1
Author:     Stephen Cheng <stephen.cheng@xxxxxxxxxx>
AuthorDate: Wed Nov 25 14:38:36 2026 +0100
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Wed Nov 25 14:38:36 2026 +0100

    x86emul: Don't adjust the memory operand of BT* with an immediate offset
    
    With a register bit offset, BT{,S,R,C} can address any bit from their
    memory operand, so the emulator moves the memory address to the operand
    holding the bit. With an immediate bit offset, the CPU only uses the
    immediate modulo the operand size, and doesn't move the address (Intel
    SDM 325462-092US, Vol 2A, section 3.1.1.9 "Operation Section", Table
    3-2 "Range of Bit Positions Specified by Bit Offset Operands").
    
    The check that skipped the adjustment for the immediate forms was
    mistakenly dropped by the commit below. Add it back, and add a test.
    
    Fixes: 47591e012f08 ("x86/emul: Further simplify DstBitBase handling")
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
    Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>
    master commit: 11a91e7a3da22700da407f6365bae4f3d2e86b6d
    master date: 2026-11-18 09:58:04 +0100
---
 tools/tests/x86_emulator/test_x86_emulator.c | 19 +++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c       |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/tools/tests/x86_emulator/test_x86_emulator.c 
b/tools/tests/x86_emulator/test_x86_emulator.c
index 39e8056d77..2123b50053 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -1139,6 +1139,25 @@ int main(int argc, char **argv)
         goto fail;
     printf("okay\n");
 
+    printf("%-40s", "Testing btrl $0x21,(%edi)...");
+    instr[0] = 0x0f; instr[1] = 0xba; instr[2] = 0x37; instr[3] = 0x21;
+    res[0]      = 0x2233445F;
+    res[1]      = 0x2233445F;
+    regs.eflags = EFLAGS_ALWAYS_SET;
+    regs.eip    = (unsigned long)&instr[0];
+    regs.edi    = (unsigned long)res;
+    rc = x86_emulate(&ctxt, &emulops);
+    if ( (rc != X86EMUL_OKAY) ||
+         /* Bit 5 should have no effect. */
+         (res[0] != 0x2233445D) ||
+         (res[1] != 0x2233445F) ||
+         ((regs.eflags & (EFLAGS_ALWAYS_SET | X86_EFLAGS_ZF |
+                          X86_EFLAGS_CF)) !=
+          (EFLAGS_ALWAYS_SET | X86_EFLAGS_CF)) ||
+         (regs.eip != (unsigned long)&instr[4]) )
+        goto fail;
+    printf("okay\n");
+
     printf("%-40s", "Testing btrl %eax,(%edi)...");
     instr[0] = 0x0f; instr[1] = 0xb3; instr[2] = 0x07;
     *res        = 0x2233445F;
diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c 
b/xen/arch/x86/x86_emulate/x86_emulate.c
index 118996ae60..6712e39fef 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -1455,7 +1455,7 @@ x86_emulate(
         }
         break;
     case DstBitBase:
-        if ( ea.type == OP_MEM )
+        if ( ea.type == OP_MEM && (d & SrcMask) != SrcImmByte )
         {
             /*
              * Instructions such as bt can reference an arbitrary offset from
--
generated by git-patchbot for /home/xen/git/xen.git#stable-4.21



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.