|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH v3] x86emul: Zero-extend unsigned 8-bit immediates when decoding
On 17.11.2026 03:50, 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. At the IN/OUT and AAM/AAD
> use sites, replace the (uint8_t) casts, which are not needed any more,
> with ASSERT()s.
>
> 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>
Reviewed-by: Jan Beulich <jbeulich@xxxxxxxx>
> @@ -1804,6 +1816,8 @@ int x86emul_decode(struct x86_emulate_state *s,
> break;
>
> case ext_0f3a:
> + /* All immediates in this encoding space are unsigned ones. */
> + s->imm1 &= 0xff;
> d = ext0f3a_table[b].to_mem ? DstMem | SrcReg : DstReg | SrcMem;
> if ( ext0f3a_table[b].two_op )
> d |= TwoOp;
RORX in this space is interesting. Both AMD and Intel implicitly say the
immediate there is unsigned (by clarifying that the upper few bits are
ignored), but (largely like for VPROTx below, just with directions
reversed) one can view negative values are rotates to the left.
> @@ -1814,6 +1828,8 @@ int x86emul_decode(struct x86_emulate_state *s,
> break;
>
> case ext_8f08:
> + /* All immediates in this encoding space are unsigned ones. */
> + s->imm1 &= 0xff;
> d = DstReg | SrcMem;
> if ( ext8f08_table[b].two_op )
> d |= TwoOp;
I think I'll adjust this comment slightly: For VPROTx one can look at the
immediate as both signed and unsigned: "When the count value is negative,
bits are rotated to the right". I.e. I'll do s/are/can be viewed as/.
Would you further be up to following up with another, smaller patch
dealing with a similar issue? XOP0A has a 32-bit immediate, which we
(apparently wrongly) sign-extend, as all three insns there use only the
low 16 bits anyway. (This only affects x86_insn_immediate() afaics.)
This may extend elsewhere, so the scope of this isn't entirely clear just
yet: For example, MOV (opcode B8) in 64-bit mode without REX.W zero-
extends its immediate. The zero-extension we do in decode_onebyte() for
the REX.W case wants pulling out. The 16-bit forms may also better zero-
extend their immediates.
While benign there (for now), I'll adjust the JMPABS patch in the APX
series accordingly as well. Similarly Map7 handling will want to have
the immediate zero-extended (for now uniformly), even if again only for
x86_insn_immediate()'s sake.
Jan
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |