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(-)
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
Signed-off-by: Stephen Cheng <stephen.cheng@citrix.com>
---
I found this while fixing the INT imm8 bug, see the note in
https://lore.kernel.org/xen-devel/20261116012748.4872-1-stephen.cheng@citrix.com/
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(-)
diff --git a/tools/tests/x86_emulator/test_x86_emulator.c b/tools/tests/x86_emulator/test_x86_emulator.c
index 61b2840ee0..fa11401fee 100644
--- a/tools/tests/x86_emulator/test_x86_emulator.c
+++ b/tools/tests/x86_emulator/test_x86_emulator.c
@@ -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;
diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c b/xen/arch/x86/x86_emulate/x86_emulate.c
index f168f3ebcf..795eec878f 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
--
2.49.0
On 17.11.2026 05:58, Stephen Cheng wrote:
> 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
> Signed-off-by: Stephen Cheng <stephen.cheng@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
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
On 17.11.2026 11:11, Jan Beulich wrote: > Reviewed-by: Jan Beulich <jbeulich@suse.com> > 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
© 2016 - 2026 Red Hat, Inc.