Skip to contentSkip to conversation

x86/pv: Fix guest crashes following f75b1a5247b "x86/pv: Drop int80_bounce from struct pv_vcpu"

PATCH In review Andrew Cooper 2 participants 3 messages Mar 14, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pv: Fix guest crashes following f75b1a5247b "x86/pv: Drop int80_bounce from struct pv_vcpu"

The original init_int80_direct_trap() was in fact buggy; int $0x80 is not an exception. This went unnoticed for years because int80_bounce and trap_bounce were separate structures, but were combined by this change.

Exception handling is different to interrupt handling for PV guests. By reusing trap_bounce, the following corner case can occur:

* Handle a guest int $0x80 instruction. Latches TBF_EXCEPTION into trap_bounce.
* Handle an exception, which emulates to success (such as ptwr support), which leaves trap_bounce unmodified.
* The exception exit path sees TBF_EXCEPTION set and re-injects the int $0x80 a second time.

Drop the TBF_EXCEPTION from the int80 invocation, which matches the equivalent logic from the syscall/sysenter paths.

---
CC: Jan Beulich <[email protected]>
CC: Sander Eikelenboom <[email protected]>

---
 xen/arch/x86/x86_64/entry.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/x86_64/entry.S +2 -2
index e011c90..f4e1b80 100644
@@ -373,10 +373,10 @@ UNLIKELY_END(msi_check)
         mov   %cx, TRAPBOUNCE_cs(%rdx)
         mov   %rdi, TRAPBOUNCE_eip(%rdx)
 
-        /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
+        /* TB_flags = (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
         testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi)
         setnz %cl
-        lea   TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx
+        lea   (, %rcx, TBF_INTERRUPT), %ecx
         mov   %cl, TRAPBOUNCE_flags(%rdx)
 
         cmpb  $0, DOMAIN_is_32bit_pv(%rax)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 14.03.18 at 12:51, <[email protected]> wrote:

The original init_int80_direct_trap() was in fact buggy; int $0x80 is not an
exception. This went unnoticed for years because int80_bounce and trap_bounce
were separate structures, but were combined by this change.

Exception handling is different to interrupt handling for PV guests. By reusing trap_bounce, the following corner case can occur:

Quoted 6 lines

* Handle a guest int $0x80 instruction. Latches TBF_EXCEPTION into trap_bounce.
* Handle an exception, which emulates to success (such as ptwr support), which leaves trap_bounce unmodified.
* The exception exit path sees TBF_EXCEPTION set and re-injects the int $0x80 a second time.

Oh, and then it was the clearing of trap_bounce after consuming it in your conversion to C which masked the problem?

xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 379

Quoted 12 lines
xen/arch/x86/x86_64/entry.S +2 -2
@@ -373,10 +373,10 @@ UNLIKELY_END(msi_check)
         mov   %cx, TRAPBOUNCE_cs(%rdx)
         mov   %rdi, TRAPBOUNCE_eip(%rdx)
 
-        /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
+        /* TB_flags = (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
         testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi)
         setnz %cl
-        lea   TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx
+        lea   (, %rcx, TBF_INTERRUPT), %ecx

With the immediate gone I think

    shl   $3, %ecx

would be more readable and perhaps no worse code wise (the use of LEA was introduced in cases like this only to combine the shift with the ORing in of other flags). I won't insist on that change though (the more that there's no symbolic constant available for that literal 3 right now), so with or without it

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 14.03.18 at 12:51, <[email protected]> wrote:

The original init_int80_direct_trap() was in fact buggy; int $0x80 is not an
exception. This went unnoticed for years because int80_bounce and trap_bounce
were separate structures, but were combined by this change.

Exception handling is different to interrupt handling for PV guests. By reusing trap_bounce, the following corner case can occur:

Quoted 8 lines
Quoted 6 lines

* Handle a guest int $0x80 instruction. Latches TBF_EXCEPTION into trap_bounce.
* Handle an exception, which emulates to success (such as ptwr support), which leaves trap_bounce unmodified.
* The exception exit path sees TBF_EXCEPTION set and re-injects the int $0x80 a second time.

Oh, and then it was the clearing of trap_bounce after consuming it in your conversion to C which masked the problem?

Yes.

Quoted 17 lines

xen/arch/x86/x86_64/entry.S UNLIKELY_END(msi_check) · line 379

Quoted 13 lines
xen/arch/x86/x86_64/entry.S +2 -2
@@ -373,10 +373,10 @@ UNLIKELY_END(msi_check)
         mov   %cx, TRAPBOUNCE_cs(%rdx)
         mov   %rdi, TRAPBOUNCE_eip(%rdx)
 
-        /* TB_flags = TBF_EXCEPTION | (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
+        /* TB_flags = (TI_GET_IF(ti) ? TBF_INTERRUPT : 0); */
         testb $4, 0x80 * TRAPINFO_sizeof + TRAPINFO_flags(%rsi)
         setnz %cl
-        lea   TBF_EXCEPTION(, %rcx, TBF_INTERRUPT), %ecx
+        lea   (, %rcx, TBF_INTERRUPT), %ecx

With the immediate gone I think

    shl   $3, %ecx
Quoted 7 lines

would be more readable and perhaps no worse code wise (the use of LEA was introduced in cases like this only to combine the shift with the ORing in of other flags). I won't insist on that change though (the more that there's no symbolic constant available for that literal 3 right now), so with or without it

I'll do a followup patch.  This particular pattern exists elsewhere, so might as well fix them all.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel