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

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


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Date: Tue, 17 Nov 2026 12:58:12 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pdOGsWJlYCHlMXlKBcvsF0CcUJUNB/qihTJ/D+U0tA4=; b=vydF237eMug5lz7SLoZBuSpjF+5wn5eusrL9txRoTrDvo3McTD+7L7vgazKKLrtNubgnoKOtVwD5q/EUvPOWnSsggzwbZJCk9E/yONwwY5xpgT7bRjF2jL0w6Y+OE8thYiSsfrr6xFAPOJekheDQfgEKNmEBb/voUAWxt2oJPqq1y9u+e0Pykt1wamBm+ZmpGGVyS7Q6WcqFU6V0fWVNBWJiYbYZR/VYTHt0EEH40X84tFKZJ/gmh1HZwZYUXM55cr9iG5vMVoDSuSzsGPSccEtVQRA3V9QBfy0uwkm8LY+g0oG7sktJUldhAvIqdz9Y9b0kK5Se682egxv4YuwLCw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gseRqZ5K5KNEU+fRku69R4m6Blw3iTGz1nnx9pURFaGpiGASOK2z2WixgHPO+ihp7R0ISf0/IGnLtX7+a4RIl4BIltRcoi7Q1asn8sbHzJ/g7YquUJdpcNCL7kZtvWUiwmtscSa2Nx8Ya54RBSMtvFSQQVsclxP5PMOACnF5kV4m5N0tD0uP6HtxDKOZnwkmTOSrsqkGQN3yA1ULMtvc6ZjH6Pd4kzb3LzH3BXS2pw/Da0vJOt4A8sCg6yWP1XGUstK3G97nvPe/OM8HIxyGmSaAu1ttL9dNYGWxYt9xrzTFg1q3mD5BXfQsaP9wXi3/UYLq1CGKyuCggjjlREAiXQ==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Ross Lagerwall <ross.lagerwall@xxxxxxxxxx>, Stephen Cheng <stephen.cheng@xxxxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>
  • Delivery-date: Tue, 17 Nov 2026 04:58:32 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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@xxxxxxxxxx>
---
I found this while fixing the INT imm8 bug, see the note in
https://lore.kernel.org/xen-devel/20261116012748.4872-1-stephen.cheng@xxxxxxxxxx/

 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




 


Rackspace

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