Skip to contentSkip to conversation

vmx: Allow software (user defined) interrupts to be injected in to the guest

PATCH In review Aravindh Puthiyaparambil 5 participants 13 messages Apr 20, 2012 – May 3, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vmx: Allow software (user defined) interrupts to be injected in to the guest

If xc_hvm_inject_trap() is called on a software (user defined) interrupt, it causes the guest to crash with a vmentry failure. The following patch fixes this issue.

xen/arch/x86/hvm/vmx/vmx.c +7 -0
diff -r 9036d6f974de -r f60377584f2d xen/arch/x86/hvm/vmx/vmx.c
@@ -1374,6 +1374,13 @@ void vmx_inject_hw_exception(int trap, i
 
         type = X86_EVENTTYPE_SW_EXCEPTION;
         __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 1); /* int3 */
+        break;
+    default:
+        if ( trap > TRAP_last_reserved )
+        {
+            type = X86_EVENTTYPE_SW_EXCEPTION;
+            __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 2); /* int imm8 */
+        }
     }
 
     if ( unlikely(intr_info & INTR_INFO_VALID_MASK) &&
xen/include/asm-x86/processor.h +1 -0
diff -r 9036d6f974de -r f60377584f2d xen/include/asm-x86/processor.h
@@ -111,6 +111,7 @@
 #define TRAP_alignment_check  17
 #define TRAP_machine_check    18
 #define TRAP_simd_error       19
+#define TRAP_last_reserved    31
 
 /* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. */
 /* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_ defn. */
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Aravindh Puthiyaparambil

If xc_hvm_inject_trap() is called on a software (user defined) interrupt, it causes the guest to crash with a vmentry failure. The following patch fixes this issue.

diff -r 9036d6f974de -r f60377584f2d xen/arch/x86/hvm/vmx/vmx.c

xen/arch/x86/hvm/vmx/vmx.c void vmx_inject_hw_exception(int trap, i · line 1382

Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c +6 -0
@@ -1374,6 +1374,13 @@ void vmx_inject_hw_exception(int trap, i
 
         type = X86_EVENTTYPE_SW_EXCEPTION;
         __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 1); /* int3 */
+        break;
+    default:
+        if ( trap > TRAP_last_reserved )
+        {
+            type = X86_EVENTTYPE_SW_EXCEPTION;
+            __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 2); /* int imm8 */

I doubt this is generally correct, in particular for the use you appear to desire: When the injection is not the result of an INT nn instruction (which I would guess to be the case when coming from libxc), you shouldn't set a non-zero instruction length. I believe this is also wrong for the INT3 code above.

Additionally the problem should not be limited to injection coming from libxc - injection originating from x86_emulate() should be affected as much.

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor with any (pointless) prefixes used on INT3 or INT nn.

Jan

Quoted 22 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -0
+        }
     }
 
     if ( unlikely(intr_info & INTR_INFO_VALID_MASK) &&

diff -r 9036d6f974de -r f60377584f2d xen/include/asm-x86/processor.h

xen/include/asm-x86/processor.h +1 -0
@@ -111,6 +111,7 @@
 #define TRAP_alignment_check  17
 #define TRAP_machine_check    18
 #define TRAP_simd_error       19
+#define TRAP_last_reserved    31
 
 /* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. 
 */

/* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_
defn. */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 18 lines · Jan Beulich

I doubt this is generally correct, in particular for the use you appear to desire: When the injection is not the result of an INT nn instruction (which I would guess to be the case when coming from libxc), you shouldn't set a non-zero instruction length. I believe this is also wrong for the INT3 code above.

Additionally the problem should not be limited to injection coming from libxc - injection originating from x86_emulate() should be affected as much.

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Quoted 3 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor with any (pointless) prefixes used on INT3 or INT nn.

Agreed, I applied the patch because at least it doesn't mess with any existing logic for vectors < 32, but really this function is now an overloaded mess. vmx_inject_hw_exception() should deal *only* with hw exceptions, and a more general function should be provided for the more general callers. Or something. It needs a bit of thought and is certainly not 4.2 material now.

-- Keir

#3 lore raw
In reply to Jan Beulich
Quoted 8 lines

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Mmm, SDM requires us to use X86_EVENTTYPE_SW_EXCEPTION for #OF & #BP, It seems we are slightly different here. Let me check w/ internal person.

Quoted 3 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor

The original code path works for the privilege violation introduced exceptions, It seems we probbaly need a new code for INT n emulation for both interrupt & exceptions.

Quoted 2 lines

with any (pointless) prefixes used on INT3 or INT nn.

What specific prefix do u mean here?

Thx, Eddie

#4 lore raw
Quoted 10 lines
Quoted 9 lines
Quoted 1 line · Eddie

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Quoted 2 lines

Mmm, SDM requires us to use X86_EVENTTYPE_SW_EXCEPTION for #OF & #BP, It seems we are slightly different here. Let me check w/ internal person.

Thanks.

Quoted 7 lines
Quoted 2 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor

The original code path works for the privilege violation introduced exceptions,
It seems we probbaly need a new code for INT n emulation for both interrupt & exceptions.

Indeed.

Quoted 3 lines
Quoted 2 lines

with any (pointless) prefixes used on INT3 or INT nn.

What specific prefix do u mean here?

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

Jan

#5 lore raw
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Quoted 5 lines
Quoted 1 line

Mmm, SDM requires us to use X86_EVENTTYPE_SW_EXCEPTION for #OF &

#BP,

Quoted 1 line

It seems we are slightly different here. Let me check w/ internal person.

Thanks.

The TRAP_debug should not use SW_EXCEPTION, it should use HW_EXCEPTION Per SDM and confirmation from our HW guys. We will send fixes soon.

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor

The original code path works for the privilege violation introduced exceptions,
It seems we probbaly need a new code for INT n emulation for both

interrupt &

Quoted 1 line

exceptions.

Indeed.

This API vmx_inject_hw_exception is never intended to be used for INT nn emulation,
Rather it is designed for the exceptions generated by processor-detected program-error exceptions and machine check exceptions.

If the purpose of Aravindh's patch is for INT nn emulation (CD nn), it is incorrect. We need a new API for that purpose, and use software interrupt. Of course, for INTO & INT 3 (CE & CC), we should use SW_EXCEPTION as SDM mentioned.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

with any (pointless) prefixes used on INT3 or INT nn.

What specific prefix do u mean here?

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

LOCK can never be used as prefix of INT nn instruction, nor can REPx prefix. Can you provide more details as for this concern?

Thx, Eddie

#6 lore raw
Quoted 29 lines · Eddie
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Quoted 5 lines
Quoted 1 line

Mmm, SDM requires us to use X86_EVENTTYPE_SW_EXCEPTION for #OF &

#BP,

Quoted 1 line

It seems we are slightly different here. Let me check w/ internal person.

Thanks.

The TRAP_debug should not use SW_EXCEPTION, it should use HW_EXCEPTION Per SDM and confirmation from our HW guys. We will send fixes soon.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor

The original code path works for the privilege violation introduced exceptions,
It seems we probbaly need a new code for INT n emulation for both

interrupt &

Quoted 1 line

exceptions.

Quoted 8 lines
Quoted 1 line

Indeed.

This API vmx_inject_hw_exception is never intended to be used for INT nn emulation,
Rather it is designed for the exceptions generated by processor-detected program-error exceptions and machine check exceptions.

If the purpose of Aravindh's patch is for INT nn emulation (CD nn), it is incorrect. We need a new API for that purpose, and use software interrupt. Of course, for INTO & INT 3 (CE & CC), we should use SW_EXCEPTION as SDM mentioned.

The reason I submitted the patch was, calling xc_hvm_inject_trap() on a software interrupt caused the guest to crash with a vmentry failure because the interrupt was injected as a hardware interrupt. The patch allowed me to inject a software interrupt successfully.

However I do agree that it is better if we have a separate API that does not overload vmx_inject_hw_exception().

Thanks,
Aravindh

#7 lore raw
Quoted 3 lines

The reason I submitted the patch was, calling xc_hvm_inject_trap() on a software interrupt caused the guest to crash with a vmentry failure

That should use SW_INTERRUPT, not SW_EXCEPTION.

Quoted 6 lines

because the interrupt was injected as a hardware interrupt. The patch allowed me to inject a software interrupt successfully.

However I do agree that it is better if we have a separate API that does not overload vmx_inject_hw_exception().

#8 lore raw
In reply to Dong, Eddie
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines · Eddie
Quoted 8 lines

Jun, Eddie - I further wonder why #OF is not being handled according to the documentation here either (should also result in X86_EVENTTYPE_SW_EXCEPTION). And the fall-through from TRAP_debug to TRAP_int3 is suspicious too (at the very minimum it should be annotated with a comment saying why fall-through is intended here). Nor does the documentation state that TRAP_debug should ever result in X86_EVENTTYPE_SW_EXCEPTION.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Mmm, SDM requires us to use X86_EVENTTYPE_SW_EXCEPTION for #OF &

#BP,

Quoted 1 line

It seems we are slightly different here. Let me check w/ internal person.

Thanks.

The TRAP_debug should not use SW_EXCEPTION, it should use HW_EXCEPTION Per SDM and confirmation from our HW guys. We will send fixes soon.

Please also have the opcode 0xF1 generated #DB addressed in whatever is the appropriate way.

Quoted 16 lines
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

Finally, the whole injection logic (including the patch here) doesn't appear to cope with INT nn being used by a guest with nn < 32, nor

The original code path works for the privilege violation introduced exceptions,
It seems we probbaly need a new code for INT n emulation for both

interrupt &

Quoted 1 line

exceptions.

Indeed.

This API vmx_inject_hw_exception is never intended to be used for INT nn emulation,
Rather it is designed for the exceptions generated by processor-detected program-error exceptions and machine check exceptions.

Quoted 4 lines

If the purpose of Aravindh's patch is for INT nn emulation (CD nn), it is incorrect. We need a new API for that purpose, and use software interrupt. Of course, for INTO & INT 3 (CE & CC), we should use SW_EXCEPTION as SDM mentioned.

I'm sure he took it to be the correct one because it previously handled #BP too.

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

with any (pointless) prefixes used on INT3 or INT nn.

What specific prefix do u mean here?

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

LOCK can never be used as prefix of INT nn instruction, nor can REPx prefix. Can you provide more details as for this concern?

The only prefix that is documented to cause #UD here is LOCK. All other prefixes should consequently be considered ignored, and so should the emulation do (and properly handle resulting instruction lengths).

Jan

#9 lore raw
Quoted 6 lines
Quoted 1 line

The TRAP_debug should not use SW_EXCEPTION, it should use

HW_EXCEPTION

Quoted 1 line

Per SDM and confirmation from our HW guys. We will send fixes soon.

Please also have the opcode 0xF1 generated #DB addressed in whatever is the appropriate way.

Opcode 0xf1 should use " privileged software exception".

What we can do probably include:
1: A patch to fix the mistake of #BP & #OF, plus additional comments to state the usage of the API. 2: Another patch to provide a new API for 0xf1 & CD nn? But we don't have real usage case to test so far.

We will provide #1 quickly, but for #2, can Aravindh provide test if we get the patch ready?

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

LOCK can never be used as prefix of INT nn instruction, nor can REPx

prefix.

Quoted 1 line

Can you provide more details as for this concern?

The only prefix that is documented to cause #UD here is LOCK. All

In #UD case (fault), the guest RIP is not advanced per SDM, and therefore guest will either spin in the previous LOCK instruction, or advance the IP to next instruction by guest #UD handler. I didn't see emulator could advance IP to the next instruction (INT nn) for LOCK prefix. Do I miss something?

Quoted 4 lines

other prefixes should consequently be considered ignored, and so should the emulation do (and properly handle resulting instruction lengths).

The behavior is un-defined per SDM in this case, so either solution should be fine :)

Thx, Eddie

#10 lore raw
Quoted 30 lines
Quoted 7 lines
Quoted 2 lines · Eddie

The TRAP_debug should not use SW_EXCEPTION, it should use

HW_EXCEPTION

Quoted 1 line

Per SDM and confirmation from our HW guys. We will send fixes soon.

Please also have the opcode 0xF1 generated #DB addressed in whatever is the appropriate way.

Opcode 0xf1 should use " privileged software exception".

What we can do probably include:
1: A patch to fix the mistake of #BP & #OF, plus additional comments to state the usage of the API.
2: Another patch to provide a new API for 0xf1 & CD nn? But we don't have real usage case to test so far.

We will provide #1 quickly, but for #2, can Aravindh provide test if we get the patch ready?

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

LOCK can never be used as prefix of INT nn instruction, nor can REPx

prefix.

Quoted 1 line

Can you provide more details as for this concern?

The only prefix that is documented to cause #UD here is LOCK. All

Quoted 7 lines

In #UD case (fault), the guest RIP is not advanced per SDM, and therefore guest will either
spin in the previous LOCK instruction, or advance the IP to next instruction by guest #UD handler.
I didn't see emulator could advance IP to the next instruction (INT nn) for LOCK prefix.
Do I miss something?

I'm sure you misunderstand me. I was saying that LOCK is the only prefix we can validly assume was not present on the original instruction.

Any other prefix could be present, and should count towards the instruction length. Note the

        __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 1); /* int3 */

and (after the recent change for INT nn)

            __vmwrite(VM_ENTRY_INSTRUCTION_LEN, 2); /* int imm8 */

which both use hard coded values.

Furthermore, for Aravindh's use case where there might not even be an "original instruction" (i.e. injecting an interrupt/exception for reasons other than emulating a respective instruction), advancing IP seems bogus to me altogether.

Quoted 6 lines
Quoted 4 lines

other prefixes should consequently be considered ignored, and so should the emulation do (and properly handle resulting instruction lengths).

The behavior is un-defined per SDM in this case, so either solution should be fine :)

Can you please point me to where this is being stated? I particularly doubt that for operand and address size prefixes as well as on 64-bit
- since they are documented to be ignored there - CS, DS, ES, and SS segment prefixes...

Jan

#11 lore raw
In reply to Dong, Eddie
Quoted 7 lines · Eddie
Quoted 6 lines
Quoted 1 line

The TRAP_debug should not use SW_EXCEPTION, it should use

HW_EXCEPTION

Quoted 1 line

Per SDM and confirmation from our HW guys. We will send fixes soon.

Please also have the opcode 0xF1 generated #DB addressed in whatever is the appropriate way.

Quoted 7 lines

Opcode 0xf1 should use " privileged software exception".

What we can do probably include:
1: A patch to fix the mistake of #BP & #OF, plus additional comments to state the usage of the API.
2: Another patch to provide a new API for 0xf1 & CD nn? But we don't have real usage case to test so far.

Yes, this sounds great.

-- Keir

Quoted 35 lines

We will provide #1 quickly, but for #2, can Aravindh provide test if we get the patch ready?

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines

Anyone except perhaps LOCK - none of them should have any effect other than making the instruction longer.

LOCK can never be used as prefix of INT nn instruction, nor can REPx

prefix.

Quoted 1 line

Can you provide more details as for this concern?

The only prefix that is documented to cause #UD here is LOCK. All

In #UD case (fault), the guest RIP is not advanced per SDM, and therefore guest will either
spin in the previous LOCK instruction, or advance the IP to next instruction by guest #UD handler.
I didn't see emulator could advance IP to the next instruction (INT nn) for LOCK prefix.
Do I miss something?

Quoted 4 lines

other prefixes should consequently be considered ignored, and so should the emulation do (and properly handle resulting instruction lengths).

The behavior is un-defined per SDM in this case, so either solution should be fine :)

Thx, Eddie

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#12 lore raw
In reply to Dong, Eddie
Quoted 7 lines · Eddie
Quoted 6 lines
Quoted 1 line

The TRAP_debug should not use SW_EXCEPTION, it should use

HW_EXCEPTION

Quoted 1 line

Per SDM and confirmation from our HW guys. We will send fixes soon.

Please also have the opcode 0xF1 generated #DB addressed in whatever is the appropriate way.

Quoted 7 lines

Opcode 0xf1 should use " privileged software exception".

What we can do probably include:
1: A patch to fix the mistake of #BP & #OF, plus additional comments to state the usage of the API. 2: Another patch to provide a new API for 0xf1 & CD nn? But we don't have real usage case to test so far.

We will provide #1 quickly, but for #2, can Aravindh provide test if we get the patch ready?

I will gladly debug and test #2 for you.

Thanks,
Aravindh