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

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


  • To: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Wed, 18 Nov 2026 10:11:51 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Ross Lagerwall <ross.lagerwall@xxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Wed, 18 Nov 2026 09:11:59 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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@xxxxxxxx>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Stephen Cheng <stephen.cheng@xxxxxxxxxx>

Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>

> 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



 


Rackspace

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