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

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



commit 11a91e7a3da22700da407f6365bae4f3d2e86b6d
Author:     Stephen Cheng <stephen.cheng@xxxxxxxxxx>
AuthorDate: Wed Nov 18 09:58:04 2026 +0100
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Wed Nov 18 09:58:04 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>
---
 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 61b2840ee0..dfadc02624 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -1201,6 +1201,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 4ad3e556a0..09d43d204b 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -1505,7 +1505,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#staging



 


Rackspace

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