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

[PATCH] x86emul: Zero-extend the XOP0A and MOV B8+r immediates when decoding


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Date: Wed, 18 Nov 2026 16:57:11 +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=6DF72q7IOHo6VgxIWuEuOXyOP6aOzp77z+QPw8IQGoo=; b=hDbRxbjn0xz/3ZayMzHVGutyah9gzrt8VPt1qBFaCQBsq7WG3jA9sF+lKPCoXfdqt+3vV8E/7a8+90nIRj+E5Noq3b38DGrWcCc/FgopKn0IKhwncWpJgFul79bmXd30mJYQXW9pxTs37y7eRI9Fbkyljg2MK50QyCj2Y40+EYJkesx1azmSpy9V7WcU5g7zSJ006vr7kLkmotecYMRo1/Y7n6NykJxUFVYe8ifmiOPwjbpl5q8hFciY12596tG/i7cn8fbVMdGSDmXbzEj2yKjgB/nKtcKnpSCxpYeSwVwbdIwF946LxVxFKWQbKjoj8Mdylu1+bF9zJVB+NymLiw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=zRwG8r3JOBacU3WIC4ORU/q4pUChJxMDA2aVZrR/F/EFfWp5FXDwdHZfsr725rMYLRhFad54yQ70XJE7putSoMkXvXxrx14yKkRHxGZ0x5Ly6encseA1vlINeixF4Zbd3m8CQPUAh/7eNf+moMvUyoCTwkVN1NUTXhEKSkSKND5VEmquzZiRaj4vrr56y9O/qfW8Yusnulda3AJYtS0ju/aGV6/L/IaR7hygaeWgPwB1O13yXoisFRBTWJN007tJGux+EjX97d2ugWzaN9Litx904LeIm4qnBAr+Mps1r/Cl/oaPvOVfDbkzN4gGKo8TDv8DfEXl36zL9v1wnU4NnQ==
  • 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>
  • Delivery-date: Wed, 18 Nov 2026 08:57:33 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

The decoder sign-extends the 32-bit immediate of the XOP0A insns
(BEXTR, LWPINS, LWPVAL), but this immediate is unsigned. It also
sign-extends the immediate of MOV B8+r without REX.W, while with REX.W
the low half is already zero-extended to build the 64-bit immediate.
In 64-bit mode MOV r32,imm32 zero-extends into the full register, and
no form of B8+r sign-extends its immediate. Emulation uses only the low
bits, so only x86_insn_immediate() returns a wrong value. No current
caller reads it for these insns.

Zero-extend the XOP0A immediate, as commit aa6070d2c840 ("x86emul:
Zero-extend unsigned 8-bit immediates when decoding") did for the 0F,
0F3A and XOP08 maps. For MOV B8+r, zero-extend the immediate for all
operand sizes, and keep only the fetch of the high half in the REX.W
path. Other insns' 16-bit immediates are left as they are for now.

Fixes: 653cae725cea ("x86/32on64: use generic instruction decoding for call 
gate emulation")
Requested-by: Jan Beulich <jbeulich@xxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
---
This is the follow-up Jan asked for in his review of v3 of
"x86emul: Zero-extend unsigned 8-bit immediates when decoding":
https://lore.kernel.org/xen-devel/521ed475-45b0-4dfa-8b66-11d5f76a2a96@xxxxxxxx/

I left the 16-bit immediates of other insns as they are, because
changing the shared fetch would affect many insns.

 xen/arch/x86/x86_emulate/decode.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/xen/arch/x86/x86_emulate/decode.c 
b/xen/arch/x86/x86_emulate/decode.c
index 2bd2494272..96b3f3f6e2 100644
--- a/xen/arch/x86/x86_emulate/decode.c
+++ b/xen/arch/x86/x86_emulate/decode.c
@@ -683,9 +683,10 @@ decode_onebyte(struct x86_emulate_state *s,
         break;
 
     case 0xb8 ... 0xbf: /* mov imm{16,32,64},r{16,32,64} */
+        /* The immediate is zero-extended, not sign-extended. */
+        s->imm1 &= s->op_bytes == 2 ? 0xffff : 0xffffffffU;
         if ( s->op_bytes == 8 ) /* Fetch more bytes to obtain imm64. */
-            s->imm1 = ((uint32_t)s->imm1 |
-                       ((uint64_t)insn_fetch_type(uint32_t) << 32));
+            s->imm1 |= (uint64_t)insn_fetch_type(uint32_t) << 32;
         break;
 
     case 0xd4: /* aam */
@@ -1839,10 +1840,14 @@ int x86emul_decode(struct x86_emulate_state *s,
         s->simd_size = ext8f08_table[b].simd_size;
         break;
 
+    case ext_8f0a:
+        /* All immediates in this encoding space are unsigned ones. */
+        s->imm1 &= 0xffffffffU;
+        break;
+
     case ext_map5:
     case ext_map6:
     case ext_8f09:
-    case ext_8f0a:
         break;
 
     default:
-- 
2.49.0




 


Rackspace

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