Skip to contentSkip to conversation

x86emul: Zero-extend unsigned 8-bit immediates when decoding

PATCH Superseded Stephen Cheng 2 participants 2 messages Nov 16

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86emul: Zero-extend unsigned 8-bit immediates when decoding

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

---
v2:
- Fix this in the decoder instead of at the INT use site, and also cover the IN/OUT, AAM/AAD and C0/C1 immediates, plus the 0F, 0F3A and XOP08 maps (Jan).
- Drop the (uint8_t) casts at the IN/OUT and AAM/AAD use sites (Jan).
- Add a Fixes: tag for the commit that added x86_insn_immediate() (Jan).
- Drop Ross's R-b (patch changed a lot).

v1: https://lore.kernel.org/xen-devel/[email protected]/

Note: While fixing this bug, I found a separate, older one. BT{,S,R,C} with an imm8 bit offset and a memory operand move the memory address by the immediate. The CPU doesn't do this: it uses the immediate modulo the operand size, at the same address. See Intel SDM (325462-092US, June 2026) Vol 2A, section 3.1.1.9 "Operation Section", Table 3-2 "Range of Bit Positions Specified by Bit Offset Operands", and "BT-Bit Test". So any immediate >= 32 (for 32-bit operands) accesses the wrong memory. E.g. with FEP, btsl $0xff,(%rdi) sets a bit at rdi-4 before this patch and at rdi+28 after it, but the CPU uses rdi. I'll send a separate fix for this.

 xen/arch/x86/x86_emulate/decode.c      | 15 ++++++++++++++-
 xen/arch/x86/x86_emulate/private.h     |  2 +-
 xen/arch/x86/x86_emulate/x86_emulate.c |  4 ++--
 3 files changed, 17 insertions(+), 4 deletions(-)
xen/arch/x86/x86_emulate/decode.c +14 -1
index c4b18718f8..a8129de672 100644
@@ -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;
 
     case 0x82: /* Grp1 (x86/32 only) */
@@ -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;
xen/arch/x86/x86_emulate/private.h +1 -1
index 7892b5603b..b2997b20cd 100644
@@ -62,7 +62,7 @@ void BUG(void);
 #define SrcMem      (2<<3) /* Memory operand. */
 #define SrcMem16    (3<<3) /* Memory operand (16-bit). */
 #define SrcImm      (4<<3) /* Immediate operand. */
-#define SrcImmByte  (5<<3) /* 8-bit sign-extended immediate operand. */
+#define SrcImmByte  (5<<3) /* 8-bit immediate operand. */
 #define SrcImm16    (6<<3) /* 16-bit zero-extended immediate operand. */
 #define SrcMask     (7<<3)
 /* Generic ModRM decode. */
xen/arch/x86/x86_emulate/x86_emulate.c +2 -2
index f168f3ebcf..da7e9753ce 100644
@@ -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 )
Signature · 1 line
2.49.0
#1 lore raw
Quoted 12 lines · Stephen Cheng

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).

Quoted 8 lines

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

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

Quoted 11 lines
xen/arch/x86/x86_emulate/decode.c +4 -0
@@ -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 */

xen/arch/x86/x86_emulate/decode.c decode_onebyte(struct x86_emulate_state *s, · line 666

Quoted 10 lines
xen/arch/x86/x86_emulate/decode.c +6 -1
     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.

Quoted 18 lines
xen/arch/x86/x86_emulate/decode.c +3 -0
@@ -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;

xen/arch/x86/x86_emulate/decode.c int x86emul_decode(struct x86_emulate_state *s, · line 1832

Quoted 7 lines
xen/arch/x86/x86_emulate/decode.c +1 -0
@@ -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.

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -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

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 2567

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -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