Mention branches and keyring.
[releases.git] / 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/export.h>
9 #include <asm/nospec-branch.h>
10 #include <asm/unwind_hints.h>
11 #include <asm/frame.h>
12 #include <asm/nops.h>
13
14         .section .text..__x86.indirect_thunk
15
16 .macro RETPOLINE reg
17         ANNOTATE_INTRA_FUNCTION_CALL
18         call    .Ldo_rop_\@
19 .Lspec_trap_\@:
20         UNWIND_HINT_EMPTY
21         pause
22         lfence
23         jmp .Lspec_trap_\@
24 .Ldo_rop_\@:
25         mov     %\reg, (%_ASM_SP)
26         UNWIND_HINT_FUNC
27         RET
28 .endm
29
30 .macro THUNK reg
31
32         .align RETPOLINE_THUNK_SIZE
33 SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL)
34         UNWIND_HINT_EMPTY
35         ANNOTATE_NOENDBR
36
37         ALTERNATIVE_2 __stringify(RETPOLINE \reg), \
38                       __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \
39                       __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE)
40
41 .endm
42
43 /*
44  * Despite being an assembler file we can't just use .irp here
45  * because __KSYM_DEPS__ only uses the C preprocessor and would
46  * only see one instance of "__x86_indirect_thunk_\reg" rather
47  * than one per register with the correct names. So we do it
48  * the simple and nasty way...
49  *
50  * Worse, you can only have a single EXPORT_SYMBOL per line,
51  * and CPP can't insert newlines, so we have to repeat everything
52  * at least twice.
53  */
54
55 #define __EXPORT_THUNK(sym)     _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym)
56 #define EXPORT_THUNK(reg)       __EXPORT_THUNK(__x86_indirect_thunk_ ## reg)
57
58         .align RETPOLINE_THUNK_SIZE
59 SYM_CODE_START(__x86_indirect_thunk_array)
60
61 #define GEN(reg) THUNK reg
62 #include <asm/GEN-for-each-reg.h>
63 #undef GEN
64
65         .align RETPOLINE_THUNK_SIZE
66 SYM_CODE_END(__x86_indirect_thunk_array)
67
68 #define GEN(reg) EXPORT_THUNK(reg)
69 #include <asm/GEN-for-each-reg.h>
70 #undef GEN
71
72 /*
73  * This function name is magical and is used by -mfunction-return=thunk-extern
74  * for the compiler to generate JMPs to it.
75  */
76 #ifdef CONFIG_RETHUNK
77
78 /*
79  * srso_alias_untrain_ret() and srso_alias_safe_ret() are placed at
80  * special addresses:
81  *
82  * - srso_alias_untrain_ret() is 2M aligned
83  * - srso_alias_safe_ret() is also in the same 2M page but bits 2, 8, 14
84  * and 20 in its virtual address are set (while those bits in the
85  * srso_alias_untrain_ret() function are cleared).
86  *
87  * This guarantees that those two addresses will alias in the branch
88  * target buffer of Zen3/4 generations, leading to any potential
89  * poisoned entries at that BTB slot to get evicted.
90  *
91  * As a result, srso_alias_safe_ret() becomes a safe return.
92  */
93 #ifdef CONFIG_CPU_SRSO
94         .section .text..__x86.rethunk_untrain
95
96 SYM_START(srso_alias_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
97         UNWIND_HINT_FUNC
98         ANNOTATE_NOENDBR
99         ASM_NOP2
100         lfence
101         jmp srso_alias_return_thunk
102 SYM_FUNC_END(srso_alias_untrain_ret)
103 __EXPORT_THUNK(srso_alias_untrain_ret)
104
105         .section .text..__x86.rethunk_safe
106 #else
107 /* dummy definition for alternatives */
108 SYM_START(srso_alias_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
109         ANNOTATE_UNRET_SAFE
110         ret
111         int3
112 SYM_FUNC_END(srso_alias_untrain_ret)
113 __EXPORT_THUNK(srso_alias_untrain_ret)
114 #endif
115
116 SYM_START(srso_alias_safe_ret, SYM_L_GLOBAL, SYM_A_NONE)
117         lea 8(%_ASM_SP), %_ASM_SP
118         UNWIND_HINT_FUNC
119         ANNOTATE_UNRET_SAFE
120         ret
121         int3
122 SYM_FUNC_END(srso_alias_safe_ret)
123
124         .section .text..__x86.return_thunk
125
126 SYM_CODE_START(srso_alias_return_thunk)
127         UNWIND_HINT_FUNC
128         ANNOTATE_NOENDBR
129         call srso_alias_safe_ret
130         ud2
131 SYM_CODE_END(srso_alias_return_thunk)
132
133 /*
134  * Some generic notes on the untraining sequences:
135  *
136  * They are interchangeable when it comes to flushing potentially wrong
137  * RET predictions from the BTB.
138  *
139  * The SRSO Zen1/2 (MOVABS) untraining sequence is longer than the
140  * Retbleed sequence because the return sequence done there
141  * (srso_safe_ret()) is longer and the return sequence must fully nest
142  * (end before) the untraining sequence. Therefore, the untraining
143  * sequence must fully overlap the return sequence.
144  *
145  * Regarding alignment - the instructions which need to be untrained,
146  * must all start at a cacheline boundary for Zen1/2 generations. That
147  * is, instruction sequences starting at srso_safe_ret() and
148  * the respective instruction sequences at retbleed_return_thunk()
149  * must start at a cacheline boundary.
150  */
151
152 /*
153  * Safety details here pertain to the AMD Zen{1,2} microarchitecture:
154  * 1) The RET at retbleed_return_thunk must be on a 64 byte boundary, for
155  *    alignment within the BTB.
156  * 2) The instruction at retbleed_untrain_ret must contain, and not
157  *    end with, the 0xc3 byte of the RET.
158  * 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread
159  *    from re-poisioning the BTB prediction.
160  */
161         .align 64
162         .skip 64 - (retbleed_return_thunk - retbleed_untrain_ret), 0xcc
163 SYM_START(retbleed_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
164         ANNOTATE_NOENDBR
165         /*
166          * As executed from retbleed_untrain_ret, this is:
167          *
168          *   TEST $0xcc, %bl
169          *   LFENCE
170          *   JMP retbleed_return_thunk
171          *
172          * Executing the TEST instruction has a side effect of evicting any BTB
173          * prediction (potentially attacker controlled) attached to the RET, as
174          * retbleed_return_thunk + 1 isn't an instruction boundary at the moment.
175          */
176         .byte   0xf6
177
178         /*
179          * As executed from retbleed_return_thunk, this is a plain RET.
180          *
181          * As part of the TEST above, RET is the ModRM byte, and INT3 the imm8.
182          *
183          * We subsequently jump backwards and architecturally execute the RET.
184          * This creates a correct BTB prediction (type=ret), but in the
185          * meantime we suffer Straight Line Speculation (because the type was
186          * no branch) which is halted by the INT3.
187          *
188          * With SMT enabled and STIBP active, a sibling thread cannot poison
189          * RET's prediction to a type of its choice, but can evict the
190          * prediction due to competitive sharing. If the prediction is
191          * evicted, retbleed_return_thunk will suffer Straight Line Speculation
192          * which will be contained safely by the INT3.
193          */
194 SYM_INNER_LABEL(retbleed_return_thunk, SYM_L_GLOBAL)
195         ret
196         int3
197 SYM_CODE_END(retbleed_return_thunk)
198
199         /*
200          * Ensure the TEST decoding / BTB invalidation is complete.
201          */
202         lfence
203
204         /*
205          * Jump back and execute the RET in the middle of the TEST instruction.
206          * INT3 is for SLS protection.
207          */
208         jmp retbleed_return_thunk
209         int3
210 SYM_FUNC_END(retbleed_untrain_ret)
211 __EXPORT_THUNK(retbleed_untrain_ret)
212
213 /*
214  * SRSO untraining sequence for Zen1/2, similar to retbleed_untrain_ret()
215  * above. On kernel entry, srso_untrain_ret() is executed which is a
216  *
217  * movabs $0xccccc30824648d48,%rax
218  *
219  * and when the return thunk executes the inner label srso_safe_ret()
220  * later, it is a stack manipulation and a RET which is mispredicted and
221  * thus a "safe" one to use.
222  */
223         .align 64
224         .skip 64 - (srso_safe_ret - srso_untrain_ret), 0xcc
225 SYM_START(srso_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE)
226         ANNOTATE_NOENDBR
227         .byte 0x48, 0xb8
228
229 /*
230  * This forces the function return instruction to speculate into a trap
231  * (UD2 in srso_return_thunk() below).  This RET will then mispredict
232  * and execution will continue at the return site read from the top of
233  * the stack.
234  */
235 SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL)
236         lea 8(%_ASM_SP), %_ASM_SP
237         ret
238         int3
239         int3
240         /* end of movabs */
241         lfence
242         call srso_safe_ret
243         ud2
244 SYM_CODE_END(srso_safe_ret)
245 SYM_FUNC_END(srso_untrain_ret)
246 __EXPORT_THUNK(srso_untrain_ret)
247
248 SYM_CODE_START(srso_return_thunk)
249         UNWIND_HINT_FUNC
250         ANNOTATE_NOENDBR
251         call srso_safe_ret
252         ud2
253 SYM_CODE_END(srso_return_thunk)
254
255 SYM_FUNC_START(entry_untrain_ret)
256         ALTERNATIVE "jmp retbleed_untrain_ret", "jmp srso_untrain_ret", X86_FEATURE_SRSO
257 SYM_FUNC_END(entry_untrain_ret)
258 __EXPORT_THUNK(entry_untrain_ret)
259
260 SYM_CODE_START(__x86_return_thunk)
261         UNWIND_HINT_FUNC
262         ANNOTATE_NOENDBR
263         ANNOTATE_UNRET_SAFE
264         ANNOTATE_NOENDBR
265         ret
266         int3
267 SYM_CODE_END(__x86_return_thunk)
268 EXPORT_SYMBOL(__x86_return_thunk)
269
270 #endif /* CONFIG_RETHUNK */