xen/arch/x86/x86_emulate/decode.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
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@suse.com>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Cheng <stephen.cheng@citrix.com>
---
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@suse.com/
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
On 18.11.2026 09:57, Stephen Cheng wrote:
> 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@suse.com>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Stephen Cheng <stephen.cheng@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
> I left the 16-bit immediates of other insns as they are, because
> changing the shared fetch would affect many insns.
Sure. For ALU insns with 16-bit operand size it is ambiguous anyway whether
to treat the unused bits as sign- or zero-extended.
> --- 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;
You covering the 16-bit case here as well makes me wonder about the 8-bit
one. I think we want to be consistent: Either we leave the 16-bit form
alone here (then a v2 would be needed), and then we don't need to do
anything else for the 8-bit form. Or we make the 8-bit form match the 16-
bit one (then another small patch amending the earlier one would be
needed, while this patch could go in as-is). Personally I favor the
former option, as that's less change in behavior (and less churn) overall.
Jan
© 2016 - 2026 Red Hat, Inc.