845cfb0d748fc98855910f2ce94d0a4b7326d3f7
[linux-block.git] / arch / x86 / lib / retpoline.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2
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/asm-offsets.h>
9 #include <asm/export.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/unwind_hints.h>
12 #include <asm/percpu.h>
13 #include <asm/frame.h>
14 #include <asm/nops.h>
15
16         .section .text.__x86.indirect_thunk
17
18
19 .macro POLINE reg
20         ANNOTATE_INTRA_FUNCTION_CALL
21         call    .Ldo_rop_\@
22         int3
23 .Ldo_rop_\@:
24         mov     %\reg, (%_ASM_SP)
25         UNWIND_HINT_FUNC
26 .endm
27
28 .macro RETPOLINE reg
29         POLINE \reg
30         RET
31 .endm
32
33 .macro THUNK reg
34
35         .align RETPOLINE_THUNK_SIZE
36 SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
37         UNWIND_HINT_UNDEFINED
38         ANNOTATE_NOENDBR
39
40         ALTERNATIVE_2 __stringify(RETPOLINE \reg), \
41                       __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \
42                       __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE)
43
44 .endm
45
46 /*
47  * Despite being an assembler file we can't just use .irp here
48  * because __KSYM_DEPS__ only uses the C preprocessor and would
49  * only see one instance of "__x86_indirect_thunk_\reg" rather
50  * than one per register with the correct names. So we do it
51  * the simple and nasty way...
52  *
53  * Worse, you can only have a single EXPORT_SYMBOL per line,
54  * and CPP can't insert newlines, so we have to repeat everything
55  * at least twice.
56  */
57
58 #define __EXPORT_THUNK(sym)     _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
59
60         .align RETPOLINE_THUNK_SIZE
61 SYM_CODE_START(__x86_indirect_thunk_array)
62
63 #define GEN(reg) THUNK reg
64 #include <asm/GEN-for-each-reg.h>
65 #undef GEN
66
67         .align RETPOLINE_THUNK_SIZE
68 SYM_CODE_END(__x86_indirect_thunk_array)
69
70 #define GEN(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
71 #include <asm/GEN-for-each-reg.h>
72 #undef GEN
73
74 #ifdef CONFIG_CALL_DEPTH_TRACKING
75 .macro CALL_THUNK reg
76         .align RETPOLINE_THUNK_SIZE
77
78 SYM_INNER_LABEL(__x86_indirect_call_thunk_\reg, SYM_L_GLOBAL)
79         UNWIND_HINT_UNDEFINED
80         ANNOTATE_NOENDBR
81
82         CALL_DEPTH_ACCOUNT
83         POLINE \reg
84         ANNOTATE_UNRET_SAFE
85         ret
86         int3
87 .endm
88
89         .align RETPOLINE_THUNK_SIZE
90 SYM_CODE_START(__x86_indirect_call_thunk_array)
91
92 #define GEN(reg) CALL_THUNK reg
93 #include <asm/GEN-for-each-reg.h>
94 #undef GEN
95
96         .align RETPOLINE_THUNK_SIZE
97 SYM_CODE_END(__x86_indirect_call_thunk_array)
98
99 #define GEN(reg) __EXPORT_THUNK(__x86_indirect_call_thunk_ ## reg)
100 #include <asm/GEN-for-each-reg.h>
101 #undef GEN
102
103 .macro JUMP_THUNK reg
104         .align RETPOLINE_THUNK_SIZE
105
106 SYM_INNER_LABEL(__x86_indirect_jump_thunk_\reg, SYM_L_GLOBAL)
107         UNWIND_HINT_UNDEFINED
108         ANNOTATE_NOENDBR
109         POLINE \reg
110         ANNOTATE_UNRET_SAFE
111         ret
112         int3
113 .endm
114
115         .align RETPOLINE_THUNK_SIZE
116 SYM_CODE_START(__x86_indirect_jump_thunk_array)
117
118 #define GEN(reg) JUMP_THUNK reg
119 #include <asm/GEN-for-each-reg.h>
120 #undef GEN
121
122         .align RETPOLINE_THUNK_SIZE
123 SYM_CODE_END(__x86_indirect_jump_thunk_array)
124
125 #define GEN(reg) __EXPORT_THUNK(__x86_indirect_jump_thunk_ ## reg)
126 #include <asm/GEN-for-each-reg.h>
127 #undef GEN
128 #endif
129 /*
130  * This function name is magical and is used by -mfunction-return=thunk-extern
131  * for the compiler to generate JMPs to it.
132  */
133 #ifdef CONFIG_RETHUNK
134
135 /*
136  * srso_untrain_ret_alias() and srso_safe_ret_alias() are placed at
137  * special addresses:
138  *
139  * - srso_untrain_ret_alias() is 2M aligned
140  * - srso_safe_ret_alias() is also in the same 2M page but bits 2, 8, 14
141  * and 20 in its virtual address are set (while those bits in the
142  * srso_untrain_ret_alias() function are cleared).
143  *
144  * This guarantees that those two addresses will alias in the branch
145  * target buffer of Zen3/4 generations, leading to any potential
146  * poisoned entries at that BTB slot to get evicted.
147  *
148  * As a result, srso_safe_ret_alias() becomes a safe return.
149  */
150 #ifdef CONFIG_CPU_SRSO
151         .section .text.__x86.rethunk_untrain
152
153 SYM_START(srso_untrain_ret_alias, SYM_L_GLOBAL, SYM_A_NONE)
154         ASM_NOP2
155         lfence
156         jmp __x86_return_thunk
157 SYM_FUNC_END(srso_untrain_ret_alias)
158 __EXPORT_THUNK(srso_untrain_ret_alias)
159
160         .section .text.__x86.rethunk_safe
161 #endif
162
163 /* Needs a definition for the __x86_return_thunk alternative below. */
164 SYM_START(srso_safe_ret_alias, SYM_L_GLOBAL, SYM_A_NONE)
165 #ifdef CONFIG_CPU_SRSO
166         add $8, %_ASM_SP
167         UNWIND_HINT_FUNC
168 #endif
169         ANNOTATE_UNRET_SAFE
170         ret
171         int3
172 SYM_FUNC_END(srso_safe_ret_alias)
173
174         .section .text.__x86.return_thunk
175
176 /*
177  * Safety details here pertain to the AMD Zen{1,2} microarchitecture:
178  * 1) The RET at __x86_return_thunk must be on a 64 byte boundary, for
179  *    alignment within the BTB.
180  * 2) The instruction at zen_untrain_ret must contain, and not
181  *    end with, the 0xc3 byte of the RET.
182  * 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread
183  *    from re-poisioning the BTB prediction.
184  */
185         .align 64
186         .skip 64 - (__ret - zen_untrain_ret), 0xcc
187 SYM_START(zen_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
188         ANNOTATE_NOENDBR
189         /*
190          * As executed from zen_untrain_ret, this is:
191          *
192          *   TEST $0xcc, %bl
193          *   LFENCE
194          *   JMP __x86_return_thunk
195          *
196          * Executing the TEST instruction has a side effect of evicting any BTB
197          * prediction (potentially attacker controlled) attached to the RET, as
198          * __x86_return_thunk + 1 isn't an instruction boundary at the moment.
199          */
200         .byte   0xf6
201
202         /*
203          * As executed from __x86_return_thunk, this is a plain RET.
204          *
205          * As part of the TEST above, RET is the ModRM byte, and INT3 the imm8.
206          *
207          * We subsequently jump backwards and architecturally execute the RET.
208          * This creates a correct BTB prediction (type=ret), but in the
209          * meantime we suffer Straight Line Speculation (because the type was
210          * no branch) which is halted by the INT3.
211          *
212          * With SMT enabled and STIBP active, a sibling thread cannot poison
213          * RET's prediction to a type of its choice, but can evict the
214          * prediction due to competitive sharing. If the prediction is
215          * evicted, __x86_return_thunk will suffer Straight Line Speculation
216          * which will be contained safely by the INT3.
217          */
218 SYM_INNER_LABEL(__ret, SYM_L_GLOBAL)
219         ret
220         int3
221 SYM_CODE_END(__ret)
222
223         /*
224          * Ensure the TEST decoding / BTB invalidation is complete.
225          */
226         lfence
227
228         /*
229          * Jump back and execute the RET in the middle of the TEST instruction.
230          * INT3 is for SLS protection.
231          */
232         jmp __ret
233         int3
234 SYM_FUNC_END(zen_untrain_ret)
235 __EXPORT_THUNK(zen_untrain_ret)
236
237 /*
238  * SRSO untraining sequence for Zen1/2, similar to zen_untrain_ret()
239  * above. On kernel entry, srso_untrain_ret() is executed which is a
240  *
241  * movabs $0xccccccc308c48348,%rax
242  *
243  * and when the return thunk executes the inner label srso_safe_ret()
244  * later, it is a stack manipulation and a RET which is mispredicted and
245  * thus a "safe" one to use.
246  */
247         .align 64
248         .skip 64 - (srso_safe_ret - srso_untrain_ret), 0xcc
249 SYM_START(srso_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
250         ANNOTATE_NOENDBR
251         .byte 0x48, 0xb8
252
253 SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL)
254         add $8, %_ASM_SP
255         ret
256         int3
257         int3
258         int3
259         lfence
260         call srso_safe_ret
261         int3
262 SYM_CODE_END(srso_safe_ret)
263 SYM_FUNC_END(srso_untrain_ret)
264 __EXPORT_THUNK(srso_untrain_ret)
265
266 SYM_FUNC_START(__x86_return_thunk)
267         ALTERNATIVE_2 "jmp __ret", "call srso_safe_ret", X86_FEATURE_SRSO, \
268                         "call srso_safe_ret_alias", X86_FEATURE_SRSO_ALIAS
269         int3
270 SYM_CODE_END(__x86_return_thunk)
271 EXPORT_SYMBOL(__x86_return_thunk)
272
273 #endif /* CONFIG_RETHUNK */
274
275 #ifdef CONFIG_CALL_DEPTH_TRACKING
276
277         .align 64
278 SYM_FUNC_START(__x86_return_skl)
279         ANNOTATE_NOENDBR
280         /*
281          * Keep the hotpath in a 16byte I-fetch for the non-debug
282          * case.
283          */
284         CALL_THUNKS_DEBUG_INC_RETS
285         shlq    $5, PER_CPU_VAR(pcpu_hot + X86_call_depth)
286         jz      1f
287         ANNOTATE_UNRET_SAFE
288         ret
289         int3
290 1:
291         CALL_THUNKS_DEBUG_INC_STUFFS
292         .rept   16
293         ANNOTATE_INTRA_FUNCTION_CALL
294         call    2f
295         int3
296 2:
297         .endr
298         add     $(8*16), %rsp
299
300         CREDIT_CALL_DEPTH
301
302         ANNOTATE_UNRET_SAFE
303         ret
304         int3
305 SYM_FUNC_END(__x86_return_skl)
306
307 #endif /* CONFIG_CALL_DEPTH_TRACKING */