1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/stringify.h>
4 #include <linux/linkage.h>
5 #include <asm/dwarf2.h>
6 #include <asm/cpufeatures.h>
7 #include <asm/alternative.h>
8 #include <asm/export.h>
9 #include <asm/nospec-branch.h>
10 #include <asm/unwind_hints.h>
11 #include <asm/frame.h>
14 .section .text..__x86.indirect_thunk
17 ANNOTATE_INTRA_FUNCTION_CALL
32 .align RETPOLINE_THUNK_SIZE
33 SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
36 ALTERNATIVE_2 __stringify(RETPOLINE \reg), \
37 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \
38 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE)
43 * Despite being an assembler file we can't just use .irp here
44 * because __KSYM_DEPS__ only uses the C preprocessor and would
45 * only see one instance of "__x86_indirect_thunk_\reg" rather
46 * than one per register with the correct names. So we do it
47 * the simple and nasty way...
49 * Worse, you can only have a single EXPORT_SYMBOL per line,
50 * and CPP can't insert newlines, so we have to repeat everything
54 #define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
55 #define EXPORT_THUNK(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
57 .align RETPOLINE_THUNK_SIZE
58 SYM_CODE_START(__x86_indirect_thunk_array)
60 #define GEN(reg) THUNK reg
61 #include <asm/GEN-for-each-reg.h>
64 .align RETPOLINE_THUNK_SIZE
65 SYM_CODE_END(__x86_indirect_thunk_array)
67 #define GEN(reg) EXPORT_THUNK(reg)
68 #include <asm/GEN-for-each-reg.h>
72 * This function name is magical and is used by -mfunction-return=thunk-extern
73 * for the compiler to generate JMPs to it.
78 * srso_alias_untrain_ret() and srso_alias_safe_ret() are placed at
81 * - srso_alias_untrain_ret() is 2M aligned
82 * - srso_alias_safe_ret() is also in the same 2M page but bits 2, 8, 14
83 * and 20 in its virtual address are set (while those bits in the
84 * srso_alias_untrain_ret() function are cleared).
86 * This guarantees that those two addresses will alias in the branch
87 * target buffer of Zen3/4 generations, leading to any potential
88 * poisoned entries at that BTB slot to get evicted.
90 * As a result, srso_alias_safe_ret() becomes a safe return.
92 #ifdef CONFIG_CPU_SRSO
93 .section .text..__x86.rethunk_untrain
95 SYM_START(srso_alias_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
99 jmp srso_alias_return_thunk
100 SYM_FUNC_END(srso_alias_untrain_ret)
101 __EXPORT_THUNK(srso_alias_untrain_ret)
103 .section .text..__x86.rethunk_safe
105 /* dummy definition for alternatives */
106 SYM_START(srso_alias_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
110 SYM_FUNC_END(srso_alias_untrain_ret)
111 __EXPORT_THUNK(srso_alias_untrain_ret)
114 SYM_START(srso_alias_safe_ret, SYM_L_GLOBAL, SYM_A_NONE)
115 lea 8(%_ASM_SP), %_ASM_SP
120 SYM_FUNC_END(srso_alias_safe_ret)
122 .section .text..__x86.return_thunk
124 SYM_CODE_START(srso_alias_return_thunk)
127 call srso_alias_safe_ret
129 SYM_CODE_END(srso_alias_return_thunk)
132 * Some generic notes on the untraining sequences:
134 * They are interchangeable when it comes to flushing potentially wrong
135 * RET predictions from the BTB.
137 * The SRSO Zen1/2 (MOVABS) untraining sequence is longer than the
138 * Retbleed sequence because the return sequence done there
139 * (srso_safe_ret()) is longer and the return sequence must fully nest
140 * (end before) the untraining sequence. Therefore, the untraining
141 * sequence must fully overlap the return sequence.
143 * Regarding alignment - the instructions which need to be untrained,
144 * must all start at a cacheline boundary for Zen1/2 generations. That
145 * is, instruction sequences starting at srso_safe_ret() and
146 * the respective instruction sequences at retbleed_return_thunk()
147 * must start at a cacheline boundary.
151 * Safety details here pertain to the AMD Zen{1,2} microarchitecture:
152 * 1) The RET at retbleed_return_thunk must be on a 64 byte boundary, for
153 * alignment within the BTB.
154 * 2) The instruction at retbleed_untrain_ret must contain, and not
155 * end with, the 0xc3 byte of the RET.
156 * 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread
157 * from re-poisioning the BTB prediction.
160 .skip 64 - (retbleed_return_thunk - retbleed_untrain_ret), 0xcc
161 SYM_FUNC_START_NOALIGN(retbleed_untrain_ret);
164 * As executed from retbleed_untrain_ret, this is:
168 * JMP retbleed_return_thunk
170 * Executing the TEST instruction has a side effect of evicting any BTB
171 * prediction (potentially attacker controlled) attached to the RET, as
172 * retbleed_return_thunk + 1 isn't an instruction boundary at the moment.
177 * As executed from retbleed_return_thunk, this is a plain RET.
179 * As part of the TEST above, RET is the ModRM byte, and INT3 the imm8.
181 * We subsequently jump backwards and architecturally execute the RET.
182 * This creates a correct BTB prediction (type=ret), but in the
183 * meantime we suffer Straight Line Speculation (because the type was
184 * no branch) which is halted by the INT3.
186 * With SMT enabled and STIBP active, a sibling thread cannot poison
187 * RET's prediction to a type of its choice, but can evict the
188 * prediction due to competitive sharing. If the prediction is
189 * evicted, retbleed_return_thunk will suffer Straight Line Speculation
190 * which will be contained safely by the INT3.
192 SYM_INNER_LABEL(retbleed_return_thunk, SYM_L_GLOBAL)
195 SYM_CODE_END(retbleed_return_thunk)
198 * Ensure the TEST decoding / BTB invalidation is complete.
203 * Jump back and execute the RET in the middle of the TEST instruction.
204 * INT3 is for SLS protection.
206 jmp retbleed_return_thunk
208 SYM_FUNC_END(retbleed_untrain_ret)
209 __EXPORT_THUNK(retbleed_untrain_ret)
212 * SRSO untraining sequence for Zen1/2, similar to retbleed_untrain_ret()
213 * above. On kernel entry, srso_untrain_ret() is executed which is a
215 * movabs $0xccccc30824648d48,%rax
217 * and when the return thunk executes the inner label srso_safe_ret()
218 * later, it is a stack manipulation and a RET which is mispredicted and
219 * thus a "safe" one to use.
222 .skip 64 - (srso_safe_ret - srso_untrain_ret), 0xcc
223 SYM_START(srso_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
227 * This forces the function return instruction to speculate into a trap
228 * (UD2 in srso_return_thunk() below). This RET will then mispredict
229 * and execution will continue at the return site read from the top of
232 SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL)
233 lea 8(%_ASM_SP), %_ASM_SP
241 SYM_CODE_END(srso_safe_ret)
242 SYM_FUNC_END(srso_untrain_ret)
243 __EXPORT_THUNK(srso_untrain_ret)
245 SYM_CODE_START(srso_return_thunk)
250 SYM_CODE_END(srso_return_thunk)
252 SYM_FUNC_START(entry_untrain_ret)
253 ALTERNATIVE "jmp retbleed_untrain_ret", "jmp srso_untrain_ret", X86_FEATURE_SRSO
254 SYM_FUNC_END(entry_untrain_ret)
255 __EXPORT_THUNK(entry_untrain_ret)
257 SYM_CODE_START(__x86_return_thunk)
264 SYM_CODE_END(__x86_return_thunk)
265 EXPORT_SYMBOL(__x86_return_thunk)
267 #endif /* CONFIG_RETHUNK */