Skip to contentSkip to conversation

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

PATCH In review Stephen Cheng 2 participants 3 messages Nov 17 – Nov 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 accidentally 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

---
I found this while fixing the INT imm8 bug, see the note in
https://lore.kernel.org/xen-devel/[email protected]/

 tools/tests/x86_emulator/test_x86_emulator.c | 18 ++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.c       |  2 +-
 2 files changed, 19 insertions(+), 1 deletion(-)
tools/tests/x86_emulator/test_x86_emulator.c +18 -0
index 61b2840ee0..fa11401fee 100644
@@ -1201,6 +1201,24 @@ 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) ||
+         (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;
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
index f168f3ebcf..795eec878f 100644
@@ -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
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Stephen Cheng

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").

Quoted 6 lines

The check that skipped the adjustment for the immediate forms was accidentally 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

with a remark towards the description: I question "accidentally", when the description of the offending patch says "... and simplify the entry condition for adjusting the memory operand." I'm inclined to change the word to "mistakenly" when committing.

Jan

#2 lore raw
Quoted 5 lines · Jan Beulich

with a remark towards the description: I question "accidentally", when the description of the offending patch says "... and simplify the entry condition for adjusting the memory operand." I'm inclined to change the word to "mistakenly" when committing.

Thanks for the review. You are right, the change was intended, so "mistakenly" is better. Please change it when committing.

Stephen