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

Re: [PATCH v2] x86emul: Zero-extend unsigned 8-bit immediates when decoding


  • To: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 16 Nov 2026 09:58:38 +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: Mon, 16 Nov 2026 08:58:50 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 16.11.2026 02:27, Stephen Cheng wrote:
> The decoder sign-extends all 8-bit immediates. This is right for e.g.
> Jcc rel8 and Grp1 (83), but some 8-bit immediates are unsigned: the
> vector of INT, the port of IN/OUT, the base of AAM/AAD, the count of
> the Grp2 shifts (C0, C1), and the 8-bit immediates in the 0F, 0F3A and
> XOP08 maps. For these, s->imm1 holds a wrong value for 0x80 and above.
> x86_insn_immediate() can return this value too.
> 
> The worst case is INT $0x80 to INT $0xff, which puts a negative vector
> into the event (e.g. -128 for 0x80). hvm_inject_event() then fails its
> "vector == event->vector" assertion, and a debug Xen panics. An HVM
> guest can trigger this with the forced emulation prefix (hvm_fep).
> 
> Zero-extend these immediates in the decoder, and drop the (uint8_t)
> casts at the IN/OUT and AAM/AAD use sites, which are not needed any
> more.
> 
> Fixes: 653cae725cea ("x86/32on64: use generic instruction decoding for call 
> gate emulation")
> Fixes: 52053d5fc2f9 ("x86/emul: Drop swint_emulate infrastructure")
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Stephen Cheng <stephen.cheng@xxxxxxxxxx>

Looks largely good to me now, just a few minor things:

> --- a/xen/arch/x86/x86_emulate/decode.c
> +++ b/xen/arch/x86/x86_emulate/decode.c
> @@ -650,10 +650,19 @@ decode_onebyte(struct x86_emulate_state *s,
>      case 0xc4: /* les */
>      case 0xc5: /* lds */
>      case 0xce: /* into */
> +    case 0xd6: /* salc / udb */
> +        s->not_64bit = true;
> +        break;
> +
>      case 0xd4: /* aam */
>      case 0xd5: /* aad */
> -    case 0xd6: /* salc / udb */
>          s->not_64bit = true;
> +        fallthrough;
> +    case 0xc0: case 0xc1: /* Grp2 */
> +    case 0xcd: /* int imm8 */
> +    case 0xe4 ... 0xe7: /* in/out imm8 */
> +        /* The 8-bit immediate is unsigned. */
> +        s->imm1 = (uint8_t)s->imm1;
>          break;

This now separate case block needs to move down. All such switch() statements
aim at having their case blocks sorted by lowest-numbered case each. There
may be anomalies to that rule, but we want to make sure to reduce their set,
not increase it.

> @@ -1796,6 +1805,8 @@ int x86emul_decode(struct x86_emulate_state *s,
>          break;
>  
>      case ext_0f:
> +        if ( (d & SrcMask) == SrcImmByte )
> +            s->imm1 = (uint8_t)s->imm1;
>          rc = decode_twobyte(s, ctxt, ops);
>          break;
>  
> @@ -1804,6 +1815,7 @@ int x86emul_decode(struct x86_emulate_state *s,
>          break;
>  
>      case ext_0f3a:
> +        s->imm1 = (uint8_t)s->imm1;
>          d = ext0f3a_table[b].to_mem ? DstMem | SrcReg : DstReg | SrcMem;
>          if ( ext0f3a_table[b].two_op )
>              d |= TwoOp;
> @@ -1814,6 +1826,7 @@ int x86emul_decode(struct x86_emulate_state *s,
>          break;
>  
>      case ext_8f08:
> +        s->imm1 = (uint8_t)s->imm1;
>          d = DstReg | SrcMem;
>          if ( ext8f08_table[b].two_op )
>              d |= TwoOp;

All of these three may benefit from having a brief comment added, e.g.
"All immediates in this encoding space are unsigned ones."

Also, just to mention - in the APX series, where I've done the respective
adjustments already, I've deliberately avoided the use of casts. "&= 0xff"
is shorter, and avoiding casts is generally a worthwhile goal imo.

> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
> @@ -2505,7 +2505,7 @@ x86_emulate(
>  
>      case 0xd4: /* aam */
>      case 0xd5: /* aad */
> -        n = (uint8_t)src.val;
> +        n = src.val;
>          if ( b & 0x01 )
>              _regs.ax = (uint8_t)(_regs.al + (_regs.ah * n));
>          else
> @@ -2561,7 +2561,7 @@ x86_emulate(
>      case 0xed: /* in %dx,%eax */
>      case 0xee: /* out %al,%dx */
>      case 0xef: /* out %eax,%dx */ {
> -        unsigned int port = ((b < 0xe8) ? (uint8_t)src.val : _regs.dx);
> +        unsigned int port = ((b < 0xe8) ? src.val : _regs.dx);
>  
>          op_bytes = !(b & 1) ? 1 : (op_bytes == 8) ? 4 : op_bytes;
>          if ( (rc = ioport_access_check(port, op_bytes, ctxt, ops)) != 0 )

How about an ASSERT() each, to make sure the handling doesn't break again?

Jan



 


Rackspace

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