Skip to contentSkip to conversation

x86/spec_ctrl: Fix several bugs in SPEC_CTRL_ENTRY_FROM_INTR_IST

PATCH In review Andrew Cooper 4 participants 5 messages Feb 14, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/spec_ctrl: Fix several bugs in SPEC_CTRL_ENTRY_FROM_INTR_IST

DO_OVERWRITE_RSB clobbers %rax, meaning in practice that that the bti_ist_info field gets zeroed. Older versions of this code had the DO_OVERWRITE_RSB register selectable, so reintroduce this ability and use it to cause the INTR_IST path to use %rdx instead.

The use of %dl for the %cs.rpl check means that when an IST interrupt hits Xen, we try to load 1 into the high 32 bits of MSR_SPEC_CTRL, suffering a #GP fault instead.

Also, drop an unused label which was a copy/paste mistake.

---
CC: Jan Beulich <[email protected]>
CC: Zhenzhong Duan <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/include/asm-x86/spec_ctrl_asm.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
xen/include/asm-x86/spec_ctrl_asm.h +6 -6
index 814f53d..1f78599 100644
@@ -79,10 +79,10 @@
  *  - SPEC_CTRL_EXIT_TO_GUEST
  */
 
-.macro DO_OVERWRITE_RSB
+.macro DO_OVERWRITE_RSB tmp=%rax
 /*
  * Requires nothing
- * Clobbers %rax, %rcx
+ * Clobbers \tmp (%rax by default), %rcx
  *
  * Requires 256 bytes of stack space, but %rsp has no net change. Based on
  * Google's performance numbers, the loop is unrolled to 16 iterations and two
@@ -97,7 +97,7 @@
  * optimised with mov-elimination in modern cores.
  */
     mov $16, %ecx                   /* 16 iterations, two calls per loop */
-    mov %rsp, %rax                  /* Store the current %rsp */
+    mov %rsp, \tmp                  /* Store the current %rsp */
 
 .L\@_fill_rsb_loop:
 
@@ -114,7 +114,7 @@
 
     sub $1, %ecx
     jnz .L\@_fill_rsb_loop
-    mov %rax, %rsp                  /* Restore old %rsp */
+    mov \tmp, %rsp                  /* Restore old %rsp */
 .endm
 
 .macro DO_SPEC_CTRL_ENTRY_FROM_VMEXIT ibrs_val:req
@@ -274,7 +274,7 @@
     testb $BTI_IST_RSB, %al
     jz .L\@_skip_rsb
 
-    DO_OVERWRITE_RSB
+    DO_OVERWRITE_RSB tmp=%rdx /* Clobbers %ecx/%rdx */
 
 .L\@_skip_rsb:
 
@@ -286,13 +286,13 @@
     setz %dl
     and %dl, STACK_CPUINFO_FIELD(use_shadow_spec_ctrl)(%r14)
 
-.L\@_entry_from_xen:
     /*
      * Load Xen's intended value.  SPEC_CTRL_IBRS vs 0 is encoded in the
      * bottom bit of bti_ist_info, via a deliberate alias with BTI_IST_IBRS.
      */
     mov $MSR_SPEC_CTRL, %ecx
     and $BTI_IST_IBRS, %eax
+    xor %edx, %edx
     wrmsr
 
     /* Opencoded UNLIKELY_START() with no condition. */
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.02.18 at 12:10, <[email protected]> wrote:

DO_OVERWRITE_RSB clobbers %rax, meaning in practice that that the bti_ist_info field gets zeroed. Older versions of this code had the DO_OVERWRITE_RSB register selectable, so reintroduce this ability and use it to cause the INTR_IST path to use %rdx instead.

The use of %dl for the %cs.rpl check means that when an IST interrupt hits Xen, we try to load 1 into the high 32 bits of MSR_SPEC_CTRL, suffering a #GP fault instead.

Quoted 5 lines

Also, drop an unused label which was a copy/paste mistake.

with one remark/suggestion:

xen/include/asm-x86/spec_ctrl_asm.h line 82

Quoted 8 lines
xen/include/asm-x86/spec_ctrl_asm.h +1 -1
@@ -79,10 +79,10 @@
  *  - SPEC_CTRL_EXIT_TO_GUEST
  */
 
-.macro DO_OVERWRITE_RSB
+.macro DO_OVERWRITE_RSB tmp=%rax

If only registers are supposed to be passed as macro arguments, I generally consider it better to leave specifying at least the % to the macro body - that way nothing else (like a memory operand, perhaps referencing %rsp in a dangerous way) can be passed, as the assembler will choke on invalid names following %. In order to have the flexibility to also use the 32-bit register name inside the macro, I would also generally omit the r (but this is less of a concern here, as it's quite unlikely for the 32-bit name to be needed).

Jan

Jan

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

DO_OVERWRITE_RSB clobbers %rax, meaning in practice that that the bti_ist_info field gets zeroed. Older versions of this code had the DO_OVERWRITE_RSB register selectable, so reintroduce this ability and use it to cause the INTR_IST path to use %rdx instead.

The use of %dl for the %cs.rpl check means that when an IST interrupt hits Xen, we try to load 1 into the high 32 bits of MSR_SPEC_CTRL, suffering a #GP fault instead.

Quoted 5 lines

Also, drop an unused label which was a copy/paste mistake.

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

Reviewed-by on v1 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 1 line · Andrew Cooper

DO_OVERWRITE_RSB clobbers %rax, meaning in practice that that the bti_ist_info

^dup

Quoted 8 lines

field gets zeroed. Older versions of this code had the DO_OVERWRITE_RSB register selectable, so reintroduce this ability and use it to cause the INTR_IST path to use %rdx instead.

The use of %dl for the %cs.rpl check means that when an IST interrupt hits Xen, we try to load 1 into the high 32 bits of MSR_SPEC_CTRL, suffering a #GP fault instead.

Quoted 5 lines

Also, drop an unused label which was a copy/paste mistake.

Quoted 15 lines

---
CC: Jan Beulich <[email protected]>
CC: Zhenzhong Duan <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/include/asm-x86/spec_ctrl_asm.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
xen/include/asm-x86/spec_ctrl_asm.h +0 -0
index 814f53d..1f78599 100644

xen/include/asm-x86/spec_ctrl_asm.h line 85

Quoted 9 lines
xen/include/asm-x86/spec_ctrl_asm.h +2 -2
@@ -79,10 +79,10 @@
  *  - SPEC_CTRL_EXIT_TO_GUEST
  */
 
-.macro DO_OVERWRITE_RSB
+.macro DO_OVERWRITE_RSB tmp=%rax
 /*
  * Requires nothing
- * Clobbers %rax, %rcx
+ * Clobbers \tmp (%rax by default), %rcx

Is it worth changing %rcx to %ecx here...

Quoted 22 lines
xen/include/asm-x86/spec_ctrl_asm.h +2 -2
  *
  * Requires 256 bytes of stack space, but %rsp has no net change. Based on
  * Google's performance numbers, the loop is unrolled to 16 iterations and two
@@ -97,7 +97,7 @@
  * optimised with mov-elimination in modern cores.
  */
     mov $16, %ecx                   /* 16 iterations, two calls per loop */
-    mov %rsp, %rax                  /* Store the current %rsp */
+    mov %rsp, \tmp                  /* Store the current %rsp */
 
 .L\@_fill_rsb_loop:
 
@@ -114,7 +114,7 @@
 
     sub $1, %ecx
     jnz .L\@_fill_rsb_loop
-    mov %rax, %rsp                  /* Restore old %rsp */
+    mov \tmp, %rsp                  /* Restore old %rsp */
 .endm
 
 .macro DO_SPEC_CTRL_ENTRY_FROM_VMEXIT ibrs_val:req

xen/include/asm-x86/spec_ctrl_asm.h line 277

Quoted 5 lines
xen/include/asm-x86/spec_ctrl_asm.h +1 -1
@@ -274,7 +274,7 @@
     testb $BTI_IST_RSB, %al
     jz .L\@_skip_rsb
 
-    DO_OVERWRITE_RSB
+    DO_OVERWRITE_RSB tmp=%rdx /* Clobbers %ecx/%rdx */

... to match the comment here?

Thanks, Roger.

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

#4 lore raw
Quoted 9 lines · Roger Pau Monné
Quoted 9 lines
xen/include/asm-x86/spec_ctrl_asm.h +0 -0
index 814f53d..1f78599 100644
@@ -79,10 +79,10 @@
  *  - SPEC_CTRL_EXIT_TO_GUEST
  */
 
Quoted 7 lines

xen/include/asm-x86/spec_ctrl_asm.h line 85

Quoted 6 lines
xen/include/asm-x86/spec_ctrl_asm.h +2 -2
-.macro DO_OVERWRITE_RSB
+.macro DO_OVERWRITE_RSB tmp=%rax
 /*
  * Requires nothing
- * Clobbers %rax, %rcx
+ * Clobbers \tmp (%rax by default), %rcx

Is it worth changing %rcx to %ecx here...

No, because a mov to %ecx clobbers %rcx.  I'll adjust the comment.

~Andrew

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