Skip to contentSkip to conversation

x86emul: Treat the vector of INT imm8 as unsigned

PATCH In review Stephen Cheng 3 participants 5 messages Nov 12

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: Treat the vector of INT imm8 as unsigned

The decoder sign-extends all 8-bit immediates (SrcImmByte). For INT imm8 the immediate is the vector, which is unsigned. So INT $0x80 to INT $0xff put 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).

Cast the vector to uint8_t, as is already done for the port of IN/OUT and the operand of AAM/AAD.

Fixes 52053d5fc2f9 ("x86/emul: Drop swint_emulate infrastructure")
Assisted-by Claude:claude-opus-5-5

---
I found this with an XTF test which runs "int $0x80" with the forced emulation prefix. Tested on AMD with a debug build: without the patch the host panics, with it the guest gets vector 0x80 (also 0x21, 0x7f and 0xff).

 xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
index f168f3ebcf..8d53873c67 100644
@@ -2453,7 +2453,7 @@ x86_emulate(
             ctxt->event.type = X86_ET_SW_EXC;
             break;
         case 0xcd: /* int imm8 */
-            ctxt->event.vector = imm1;
+            ctxt->event.vector = (uint8_t)imm1;
             ctxt->event.type = X86_ET_SW_INT;
             break;
         case 0xce: /* into */
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Stephen Cheng

The decoder sign-extends all 8-bit immediates (SrcImmByte). For INT imm8 the immediate is the vector, which is unsigned. So INT $0x80 to INT $0xff put 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).

Cast the vector to uint8_t, as is already done for the port of IN/OUT and the operand of AAM/AAD.

Fixes 52053d5fc2f9 ("x86/emul: Drop swint_emulate infrastructure")
Assisted-by Claude:claude-opus-5-5

---
I found this with an XTF test which runs "int $0x80" with the forced emulation prefix. Tested on AMD with a debug build: without the patch the host panics, with it the guest gets vector 0x80 (also 0x21, 0x7f and 0xff).

  xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
index f168f3ebcf..8d53873c67 100644

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

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -2453,7 +2453,7 @@ x86_emulate(
              ctxt->event.type = X86_ET_SW_EXC;
              break;
          case 0xcd: /* int imm8 */
-            ctxt->event.vector = imm1;
+            ctxt->event.vector = (uint8_t)imm1;
              ctxt->event.type = X86_ET_SW_INT;
              break;
          case 0xce: /* into */

Thanks,
Ross

#2 lore raw
In reply to Stephen Cheng
Quoted 10 lines · Stephen Cheng

The decoder sign-extends all 8-bit immediates (SrcImmByte). For INT imm8 the immediate is the vector, which is unsigned. So INT $0x80 to INT $0xff put 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).

Cast the vector to uint8_t, as is already done for the port of IN/OUT and the operand of AAM/AAD.

I'm afraid this isn't sufficient: At least x86_insn_immediate() is also affected, and then also for the other insns you mention above plus quite a few more (in particular uniformly for 0F, 0F3A and XOP08). I think this needs sorting in x86_decode_onebyte() for all of the former (some of which I then need to mirror into the APX series for Map4), and in the switch() below setting ctxt->opcode and s->desc (where decode_*() are called) for the latter three.

There'll then want to be a 2nd Fixes: tag for the commit which introduced x86_insn_immediate().

Jan

#3 lore raw
Quoted 20 lines · Jan Beulich
Quoted 10 lines · Stephen Cheng

The decoder sign-extends all 8-bit immediates (SrcImmByte). For INT imm8 the immediate is the vector, which is unsigned. So INT $0x80 to INT $0xff put 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).

Cast the vector to uint8_t, as is already done for the port of IN/OUT and the operand of AAM/AAD.

I'm afraid this isn't sufficient: At least x86_insn_immediate() is also affected, and then also for the other insns you mention above plus quite a few more (in particular uniformly for 0F, 0F3A and XOP08). I think this needs sorting in x86_decode_onebyte() for all of the former (some of which I then need to mirror into the APX series for Map4), and in the switch() below setting ctxt->opcode and s->desc (where decode_*() are called) for the latter three.

Quoted 2 lines

There'll then want to be a 2nd Fixes: tag for the commit which introduced x86_insn_immediate().

Thanks Jan, I see. I'll move the fix into the decoder and add the 2nd
Fixes: tag in v2.

Two questions:

1. Which other one-byte insns do you mean by "quite a few more"? I can see the shift/rotate counts (C0, C1). Are there others?

2. Should I keep the existing (uint8_t) casts at the IN/OUT and AAM/AAD use sites, or drop them?

Stephen

#4 lore raw
Quoted 24 lines · Stephen Cheng
Quoted 20 lines · Jan Beulich
Quoted 10 lines · Stephen Cheng

The decoder sign-extends all 8-bit immediates (SrcImmByte). For INT imm8 the immediate is the vector, which is unsigned. So INT $0x80 to INT $0xff put 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).

Cast the vector to uint8_t, as is already done for the port of IN/OUT and the operand of AAM/AAD.

I'm afraid this isn't sufficient: At least x86_insn_immediate() is also affected, and then also for the other insns you mention above plus quite a few more (in particular uniformly for 0F, 0F3A and XOP08). I think this needs sorting in x86_decode_onebyte() for all of the former (some of which I then need to mirror into the APX series for Map4), and in the switch() below setting ctxt->opcode and s->desc (where decode_*() are called) for the latter three.

Quoted 2 lines

There'll then want to be a 2nd Fixes: tag for the commit which introduced x86_insn_immediate().

Quoted 7 lines

Thanks Jan, I see. I'll move the fix into the decoder and add the 2nd
Fixes: tag in v2.

Two questions:

1. Which other one-byte insns do you mean by "quite a few more"? I can see the shift/rotate counts (C0, C1). Are there others?

Having done the APX side change, it may indeed be just those in the base opcode map. "quite a few" covered the other opcode maps which I did mention.

Quoted 2 lines

2. Should I keep the existing (uint8_t) casts at the IN/OUT and AAM/AAD use sites, or drop them?

I'd prefer if they were dropped in exchange.

Jan