@@ -96,7 +96,6 @@ SYM_CODE_START(entry_SYSCALL_64)
9696
9797SYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL)
9898 ANNOTATE_NOENDBR
99- UNTRAIN_RET
10099
101100 /* Construct struct pt_regs on stack */
102101 pushq $__USER_DS /* pt_regs->ss */
@@ -113,6 +112,11 @@ SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
113112 movq %rsp , %rdi
114113 /* Sign extend the lower 32bit as syscall numbers are treated as int */
115114 movslq %eax , %rsi
115+
116+ /* clobbers %rax, make sure it is after saving the syscall nr */
117+ IBRS_ENTER
118+ UNTRAIN_RET
119+
116120 call do_syscall_64 /* returns with IRQs disabled */
117121
118122 /*
@@ -192,6 +196,7 @@ SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
192196 * perf profiles. Nothing jumps here.
193197 */
194198syscall_return_via_sysret:
199+ IBRS_EXIT
195200 POP_REGS pop_rdi =0
196201
197202 /*
@@ -605,6 +610,7 @@ __irqentry_text_end:
605610
606611SYM_CODE_START_LOCAL (common_interrupt_return)
607612SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
613+ IBRS_EXIT
608614#ifdef CONFIG_DEBUG_ENTRY
609615 /* Assert that pt_regs indicates user mode. */
610616 testb $3 , CS (%rsp )
@@ -891,6 +897,9 @@ SYM_CODE_END(xen_failsafe_callback)
891897 * 1 -> no SWAPGS on exit
892898 *
893899 * Y GSBASE value at entry, must be restored in paranoid_exit
900+ *
901+ * R14 - old CR3
902+ * R15 - old SPEC_CTRL
894903 */
895904SYM_CODE_START_LOCAL (paranoid_entry)
896905 UNWIND_HINT_FUNC
@@ -913,7 +922,6 @@ SYM_CODE_START_LOCAL(paranoid_entry)
913922 * be retrieved from a kernel internal table.
914923 */
915924 SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg =%rax save_reg =%r14
916- UNTRAIN_RET
917925
918926 /*
919927 * Handling GSBASE depends on the availability of FSGSBASE.
@@ -935,7 +943,7 @@ SYM_CODE_START_LOCAL(paranoid_entry)
935943 * is needed here.
936944 */
937945 SAVE_AND_SET_GSBASE scratch_reg =%rax save_reg =%rbx
938- RET
946+ jmp .Lparanoid_gsbase_done
939947
940948.Lparanoid_entry_checkgs:
941949 /* EBX = 1 -> kernel GSBASE active, no restore required */
@@ -954,8 +962,16 @@ SYM_CODE_START_LOCAL(paranoid_entry)
954962 xorl %ebx , %ebx
955963 swapgs
956964.Lparanoid_kernel_gsbase:
957-
958965 FENCE_SWAPGS_KERNEL_ENTRY
966+ .Lparanoid_gsbase_done:
967+
968+ /*
969+ * Once we have CR3 and %GS setup save and set SPEC_CTRL. Just like
970+ * CR3 above, keep the old value in a callee saved register.
971+ */
972+ IBRS_ENTER save_reg =%r15
973+ UNTRAIN_RET
974+
959975 RET
960976SYM_CODE_END(paranoid_entry)
961977
@@ -977,9 +993,19 @@ SYM_CODE_END(paranoid_entry)
977993 * 1 -> no SWAPGS on exit
978994 *
979995 * Y User space GSBASE, must be restored unconditionally
996+ *
997+ * R14 - old CR3
998+ * R15 - old SPEC_CTRL
980999 */
9811000SYM_CODE_START_LOCAL (paranoid_exit)
9821001 UNWIND_HINT_REGS
1002+
1003+ /*
1004+ * Must restore IBRS state before both CR3 and %GS since we need access
1005+ * to the per-CPU x86_spec_ctrl_shadow variable.
1006+ */
1007+ IBRS_EXIT save_reg =%r15
1008+
9831009 /*
9841010 * The order of operations is important. RESTORE_CR3 requires
9851011 * kernel GSBASE.
@@ -1023,10 +1049,12 @@ SYM_CODE_START_LOCAL(error_entry)
10231049 FENCE_SWAPGS_USER_ENTRY
10241050 /* We have user CR3. Change to kernel CR3. */
10251051 SWITCH_TO_KERNEL_CR3 scratch_reg =%rax
1052+ IBRS_ENTER
10261053 UNTRAIN_RET
10271054
10281055 leaq 8 (%rsp ), %rdi /* arg0 = pt_regs pointer */
10291056.Lerror_entry_from_usermode_after_swapgs:
1057+
10301058 /* Put us onto the real thread stack. */
10311059 call sync_regs
10321060 RET
@@ -1076,6 +1104,7 @@ SYM_CODE_START_LOCAL(error_entry)
10761104 swapgs
10771105 FENCE_SWAPGS_USER_ENTRY
10781106 SWITCH_TO_KERNEL_CR3 scratch_reg =%rax
1107+ IBRS_ENTER
10791108 UNTRAIN_RET
10801109
10811110 /*
@@ -1172,7 +1201,6 @@ SYM_CODE_START(asm_exc_nmi)
11721201 movq %rsp , %rdx
11731202 movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
11741203 UNWIND_HINT_IRET_REGS base =%rdx offset =8
1175- UNTRAIN_RET
11761204 pushq 5*8 (%rdx ) /* pt_regs->ss */
11771205 pushq 4*8 (%rdx ) /* pt_regs->rsp */
11781206 pushq 3*8 (%rdx ) /* pt_regs->flags */
@@ -1183,6 +1211,9 @@ SYM_CODE_START(asm_exc_nmi)
11831211 PUSH_AND_CLEAR_REGS rdx = (%rdx )
11841212 ENCODE_FRAME_POINTER
11851213
1214+ IBRS_ENTER
1215+ UNTRAIN_RET
1216+
11861217 /*
11871218 * At this point we no longer need to worry about stack damage
11881219 * due to nesting -- we're on the normal thread stack and we're
@@ -1407,6 +1438,9 @@ end_repeat_nmi:
14071438 movq $-1 , %rsi
14081439 call exc_nmi
14091440
1441+ /* Always restore stashed SPEC_CTRL value (see paranoid_entry) */
1442+ IBRS_EXIT save_reg =%r15
1443+
14101444 /* Always restore stashed CR3 value (see paranoid_entry) */
14111445 RESTORE_CR3 scratch_reg =%r15 save_reg =%r14
14121446
0 commit comments