# Commit 2378d16a931de0e62c03669169989e9437306abe
# Date 2024-04-09 16:37:30 +0100
# Author Andrew Cooper <andrew.cooper3@citrix.com>
# Committer Andrew Cooper <andrew.cooper3@citrix.com>
x86/spec-ctrl: Simplify DO_COND_IBPB

With the prior refactoring, SPEC_CTRL_ENTRY_{PV,INTR} both load SCF into %ebx,
and handle the conditional safety including skipping if interrupting Xen.

Therefore, we can drop the maybexen parameter and the conditional safety.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>

--- a/xen/include/asm-x86/nops.h
+++ b/xen/include/asm-x86/nops.h
@@ -63,12 +63,11 @@
 
 #ifdef __ASSEMBLY__
 #define ASM_NOP14 ASM_NOP8; ASM_NOP6
+#define ASM_NOP17 ASM_NOP8; ASM_NOP7; ASM_NOP2
 #define ASM_NOP22 ASM_NOP8; ASM_NOP8; ASM_NOP6
 #define ASM_NOP24 ASM_NOP8; ASM_NOP8; ASM_NOP8
 #define ASM_NOP25 ASM_NOP8; ASM_NOP8; ASM_NOP7; ASM_NOP2
-#define ASM_NOP27 ASM_NOP8; ASM_NOP8; ASM_NOP7; ASM_NOP4
 #define ASM_NOP33 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP7; ASM_NOP2
-#define ASM_NOP34 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP7; ASM_NOP3
 #define ASM_NOP36 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP4
 #define ASM_NOP40 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8
 #else
--- a/xen/include/asm-x86/spec_ctrl_asm.h
+++ b/xen/include/asm-x86/spec_ctrl_asm.h
@@ -114,33 +114,21 @@
  *  - SPEC_CTRL_EXIT_TO_{SVM,VMX}
  */
 
-.macro DO_SPEC_CTRL_COND_IBPB maybexen:req
+.macro DO_COND_IBPB
 /*
- * Requires %rsp=regs (also cpuinfo if !maybexen)
- * Requires %r14=stack_end (if maybexen), %rdx=0
- * Clobbers %rax, %rcx, %rdx
+ * Requires %rbx=SCF, %rdx=0
+ * Clobbers %rax, %rcx
  *
- * Conditionally issue IBPB if SCF_entry_ibpb is active.  In the maybexen
- * case, we can safely look at UREGS_cs to skip taking the hit when
- * interrupting Xen.
+ * Conditionally issue IBPB if SCF_entry_ibpb is active.
  */
-    .if \maybexen
-        testb  $SCF_entry_ibpb, STACK_CPUINFO_FIELD(scf)(%r14)
-        jz     .L\@_skip
-        testb  $3, UREGS_cs(%rsp)
-    .else
-        testb  $SCF_entry_ibpb, CPUINFO_scf(%rsp)
-    .endif
+    testb  $SCF_entry_ibpb, %bl
     jz     .L\@_skip
 
     mov     $MSR_PRED_CMD, %ecx
     mov     $PRED_CMD_IBPB, %eax
     wrmsr
-    jmp     .L\@_done
 
 .L\@_skip:
-    lfence
-.L\@_done:
 .endm
 
 #define LBL(name) .L\@_##name
@@ -327,9 +315,7 @@ LBLn(insert_rsb_entry):;
  */
     movzbl STACK_CPUINFO_FIELD(scf)(%r14), %ebx
 
-    ALTERNATIVE __stringify(ASM_NOP27),                                 \
-        __stringify(DO_SPEC_CTRL_COND_IBPB maybexen=0),                 \
-        X86_FEATURE_IBPB_ENTRY_PV
+    ALTERNATIVE __stringify(ASM_NOP17), DO_COND_IBPB, X86_FEATURE_IBPB_ENTRY_PV
 
     ALTERNATIVE __stringify(ASM_NOP40),                                 \
         DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV
@@ -359,9 +345,7 @@ LBLn(insert_rsb_entry):;
     testb $3, UREGS_cs(%rsp)
     jz .L\@_skip
 
-    ALTERNATIVE __stringify(ASM_NOP34),                                 \
-        __stringify(DO_SPEC_CTRL_COND_IBPB maybexen=1),                 \
-        X86_FEATURE_IBPB_ENTRY_PV
+    ALTERNATIVE __stringify(ASM_NOP17), DO_COND_IBPB, X86_FEATURE_IBPB_ENTRY_PV
 
     ALTERNATIVE __stringify(ASM_NOP40),                                 \
         DO_OVERWRITE_RSB, X86_FEATURE_SC_RSB_PV
@@ -403,7 +387,7 @@ LBLn(insert_rsb_entry):;
  * Clobbers %rax, %rbx, %rcx, %rdx
  *
  * This is logical merge of:
- *    DO_SPEC_CTRL_COND_IBPB maybexen=0
+ *    DO_COND_IBPB
  *    DO_OVERWRITE_RSB
  *    DO_SPEC_CTRL_ENTRY maybexen=1
  * but with conditionals rather than alternatives.
