]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/x86/include/asm/nospec-branch.h
x86/speculation: Add command line control for indirect branch speculation
[thirdparty/kernel/linux.git] / arch / x86 / include / asm / nospec-branch.h
CommitLineData
76b04384
DW
1/* SPDX-License-Identifier: GPL-2.0 */
2
7a32fc51
BP
3#ifndef _ASM_X86_NOSPEC_BRANCH_H_
4#define _ASM_X86_NOSPEC_BRANCH_H_
76b04384 5
fa1202ef
TG
6#include <linux/static_key.h>
7
76b04384
DW
8#include <asm/alternative.h>
9#include <asm/alternative-asm.h>
10#include <asm/cpufeatures.h>
ea00f301 11#include <asm/msr-index.h>
76b04384 12
d1c99108
DW
13/*
14 * Fill the CPU return stack buffer.
15 *
16 * Each entry in the RSB, if used for a speculative 'ret', contains an
17 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
18 *
19 * This is required in various cases for retpoline and IBRS-based
20 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
21 * eliminate potentially bogus entries from the RSB, and sometimes
22 * purely to ensure that it doesn't get empty, which on some CPUs would
23 * allow predictions from other (unwanted!) sources to be used.
24 *
25 * We define a CPP macro such that it can be used from both .S files and
26 * inline assembly. It's possible to do a .macro and then include that
27 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
28 */
29
30#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
31#define RSB_FILL_LOOPS 16 /* To avoid underflow */
32
33/*
34 * Google experimented with loop-unrolling and this turned out to be
35 * the optimal version — two calls, each with their own speculation
36 * trap should their return address end up getting used, in a loop.
37 */
38#define __FILL_RETURN_BUFFER(reg, nr, sp) \
39 mov $(nr/2), reg; \
40771: \
41 call 772f; \
42773: /* speculation trap */ \
43 pause; \
44 lfence; \
45 jmp 773b; \
46772: \
47 call 774f; \
48775: /* speculation trap */ \
49 pause; \
50 lfence; \
51 jmp 775b; \
52774: \
53 dec reg; \
54 jnz 771b; \
55 add $(BITS_PER_LONG/8) * nr, sp;
56
76b04384
DW
57#ifdef __ASSEMBLY__
58
59/*
60 * This should be used immediately before a retpoline alternative. It tells
61 * objtool where the retpolines are so that it can make sense of the control
62 * flow by just reading the original instruction(s) and ignoring the
63 * alternatives.
64 */
65.macro ANNOTATE_NOSPEC_ALTERNATIVE
66 .Lannotate_\@:
67 .pushsection .discard.nospec
68 .long .Lannotate_\@ - .
69 .popsection
70.endm
71
9e0e3c51
PZ
72/*
73 * This should be used immediately before an indirect jump/call. It tells
74 * objtool the subsequent indirect jump/call is vouched safe for retpoline
75 * builds.
76 */
77.macro ANNOTATE_RETPOLINE_SAFE
78 .Lannotate_\@:
79 .pushsection .discard.retpoline_safe
80 _ASM_PTR .Lannotate_\@
81 .popsection
82.endm
83
76b04384
DW
84/*
85 * These are the bare retpoline primitives for indirect jmp and call.
86 * Do not use these directly; they only exist to make the ALTERNATIVE
87 * invocation below less ugly.
88 */
89.macro RETPOLINE_JMP reg:req
90 call .Ldo_rop_\@
91.Lspec_trap_\@:
92 pause
28d437d5 93 lfence
76b04384
DW
94 jmp .Lspec_trap_\@
95.Ldo_rop_\@:
96 mov \reg, (%_ASM_SP)
97 ret
98.endm
99
100/*
101 * This is a wrapper around RETPOLINE_JMP so the called function in reg
102 * returns to the instruction after the macro.
103 */
104.macro RETPOLINE_CALL reg:req
105 jmp .Ldo_call_\@
106.Ldo_retpoline_jmp_\@:
107 RETPOLINE_JMP \reg
108.Ldo_call_\@:
109 call .Ldo_retpoline_jmp_\@
110.endm
111
112/*
113 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
114 * indirect jmp/call which may be susceptible to the Spectre variant 2
115 * attack.
116 */
117.macro JMP_NOSPEC reg:req
118#ifdef CONFIG_RETPOLINE
119 ANNOTATE_NOSPEC_ALTERNATIVE
9e0e3c51 120 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *\reg), \
76b04384 121 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
9e0e3c51 122 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
76b04384
DW
123#else
124 jmp *\reg
125#endif
126.endm
127
128.macro CALL_NOSPEC reg:req
129#ifdef CONFIG_RETPOLINE
130 ANNOTATE_NOSPEC_ALTERNATIVE
9e0e3c51 131 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *\reg), \
76b04384 132 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
9e0e3c51 133 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *\reg), X86_FEATURE_RETPOLINE_AMD
76b04384
DW
134#else
135 call *\reg
136#endif
117cc7a9
DW
137.endm
138
d1c99108
DW
139 /*
140 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
141 * monstrosity above, manually.
142 */
143.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
117cc7a9 144#ifdef CONFIG_RETPOLINE
d1c99108
DW
145 ANNOTATE_NOSPEC_ALTERNATIVE
146 ALTERNATIVE "jmp .Lskip_rsb_\@", \
147 __stringify(__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)) \
148 \ftr
149.Lskip_rsb_\@:
117cc7a9 150#endif
76b04384
DW
151.endm
152
153#else /* __ASSEMBLY__ */
154
155#define ANNOTATE_NOSPEC_ALTERNATIVE \
156 "999:\n\t" \
157 ".pushsection .discard.nospec\n\t" \
158 ".long 999b - .\n\t" \
159 ".popsection\n\t"
160
9e0e3c51
PZ
161#define ANNOTATE_RETPOLINE_SAFE \
162 "999:\n\t" \
163 ".pushsection .discard.retpoline_safe\n\t" \
164 _ASM_PTR " 999b\n\t" \
165 ".popsection\n\t"
166
4cd24de3
ZD
167#ifdef CONFIG_RETPOLINE
168#ifdef CONFIG_X86_64
76b04384
DW
169
170/*
4cd24de3
ZD
171 * Inline asm uses the %V modifier which is only in newer GCC
172 * which is ensured when CONFIG_RETPOLINE is defined.
76b04384
DW
173 */
174# define CALL_NOSPEC \
175 ANNOTATE_NOSPEC_ALTERNATIVE \
0cbb76d6 176 ALTERNATIVE_2( \
9e0e3c51 177 ANNOTATE_RETPOLINE_SAFE \
76b04384
DW
178 "call *%[thunk_target]\n", \
179 "call __x86_indirect_thunk_%V[thunk_target]\n", \
0cbb76d6
ZD
180 X86_FEATURE_RETPOLINE, \
181 "lfence;\n" \
182 ANNOTATE_RETPOLINE_SAFE \
183 "call *%[thunk_target]\n", \
184 X86_FEATURE_RETPOLINE_AMD)
76b04384
DW
185# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
186
4cd24de3 187#else /* CONFIG_X86_32 */
76b04384
DW
188/*
189 * For i386 we use the original ret-equivalent retpoline, because
190 * otherwise we'll run out of registers. We don't care about CET
191 * here, anyway.
192 */
a14bff13 193# define CALL_NOSPEC \
0cbb76d6
ZD
194 ANNOTATE_NOSPEC_ALTERNATIVE \
195 ALTERNATIVE_2( \
a14bff13
AW
196 ANNOTATE_RETPOLINE_SAFE \
197 "call *%[thunk_target]\n", \
76b04384
DW
198 " jmp 904f;\n" \
199 " .align 16\n" \
200 "901: call 903f;\n" \
201 "902: pause;\n" \
28d437d5 202 " lfence;\n" \
76b04384
DW
203 " jmp 902b;\n" \
204 " .align 16\n" \
205 "903: addl $4, %%esp;\n" \
206 " pushl %[thunk_target];\n" \
207 " ret;\n" \
208 " .align 16\n" \
209 "904: call 901b;\n", \
0cbb76d6
ZD
210 X86_FEATURE_RETPOLINE, \
211 "lfence;\n" \
212 ANNOTATE_RETPOLINE_SAFE \
213 "call *%[thunk_target]\n", \
214 X86_FEATURE_RETPOLINE_AMD)
76b04384
DW
215
216# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
4cd24de3 217#endif
117cc7a9 218#else /* No retpoline for C / inline asm */
76b04384
DW
219# define CALL_NOSPEC "call *%[thunk_target]\n"
220# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
221#endif
222
da285121
DW
223/* The Spectre V2 mitigation variants */
224enum spectre_v2_mitigation {
225 SPECTRE_V2_NONE,
da285121
DW
226 SPECTRE_V2_RETPOLINE_GENERIC,
227 SPECTRE_V2_RETPOLINE_AMD,
706d5168 228 SPECTRE_V2_IBRS_ENHANCED,
da285121
DW
229};
230
fa1202ef
TG
231/* The indirect branch speculation control variants */
232enum spectre_v2_user_mitigation {
233 SPECTRE_V2_USER_NONE,
234 SPECTRE_V2_USER_STRICT,
235};
236
24f7fc83
KRW
237/* The Speculative Store Bypass disable variants */
238enum ssb_mitigation {
239 SPEC_STORE_BYPASS_NONE,
240 SPEC_STORE_BYPASS_DISABLE,
a73ec77e 241 SPEC_STORE_BYPASS_PRCTL,
f21b53b2 242 SPEC_STORE_BYPASS_SECCOMP,
24f7fc83
KRW
243};
244
736e80a4
MH
245extern char __indirect_thunk_start[];
246extern char __indirect_thunk_end[];
247
117cc7a9
DW
248/*
249 * On VMEXIT we must ensure that no RSB predictions learned in the guest
250 * can be followed in the host, by overwriting the RSB completely. Both
251 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
af189c95 252 * CPUs with IBRS_ALL *might* it be avoided.
117cc7a9
DW
253 */
254static inline void vmexit_fill_RSB(void)
255{
256#ifdef CONFIG_RETPOLINE
d1c99108
DW
257 unsigned long loops;
258
259 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
260 ALTERNATIVE("jmp 910f",
261 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
262 X86_FEATURE_RETPOLINE)
263 "910:"
264 : "=r" (loops), ASM_CALL_CONSTRAINT
265 : : "memory" );
117cc7a9
DW
266#endif
267}
3f7d8755 268
1aa7a573
LT
269static __always_inline
270void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
271{
272 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
273 : : "c" (msr),
5f2b745f
JM
274 "a" ((u32)val),
275 "d" ((u32)(val >> 32)),
1aa7a573
LT
276 [feature] "i" (feature)
277 : "memory");
278}
dd84441a 279
20ffa1ca
DW
280static inline void indirect_branch_prediction_barrier(void)
281{
1b86883c
KRW
282 u64 val = PRED_CMD_IBPB;
283
284 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
20ffa1ca
DW
285}
286
fa8ac498
TG
287/* The Intel SPEC CTRL MSR base value cache */
288extern u64 x86_spec_ctrl_base;
289
dd84441a
DW
290/*
291 * With retpoline, we must use IBRS to restrict branch prediction
292 * before calling into firmware.
d72f4e29
IM
293 *
294 * (Implemented as CPP macros due to header hell.)
dd84441a 295 */
d72f4e29
IM
296#define firmware_restrict_branch_speculation_start() \
297do { \
fa8ac498 298 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
1b86883c 299 \
d72f4e29 300 preempt_disable(); \
1b86883c 301 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
d72f4e29
IM
302 X86_FEATURE_USE_IBRS_FW); \
303} while (0)
dd84441a 304
d72f4e29
IM
305#define firmware_restrict_branch_speculation_end() \
306do { \
fa8ac498 307 u64 val = x86_spec_ctrl_base; \
1b86883c
KRW
308 \
309 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
d72f4e29
IM
310 X86_FEATURE_USE_IBRS_FW); \
311 preempt_enable(); \
312} while (0)
20ffa1ca 313
fa1202ef
TG
314DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
315
76b04384 316#endif /* __ASSEMBLY__ */
a493a87f
DB
317
318/*
319 * Below is used in the eBPF JIT compiler and emits the byte sequence
320 * for the following assembly:
321 *
322 * With retpolines configured:
323 *
324 * callq do_rop
325 * spec_trap:
326 * pause
327 * lfence
328 * jmp spec_trap
329 * do_rop:
03f5781b
WY
330 * mov %rax,(%rsp) for x86_64
331 * mov %edx,(%esp) for x86_32
a493a87f
DB
332 * retq
333 *
334 * Without retpolines configured:
335 *
03f5781b
WY
336 * jmp *%rax for x86_64
337 * jmp *%edx for x86_32
a493a87f
DB
338 */
339#ifdef CONFIG_RETPOLINE
36256009
DB
340# ifdef CONFIG_X86_64
341# define RETPOLINE_RAX_BPF_JIT_SIZE 17
342# define RETPOLINE_RAX_BPF_JIT() \
03f5781b 343do { \
a493a87f
DB
344 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
345 /* spec_trap: */ \
346 EMIT2(0xF3, 0x90); /* pause */ \
347 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
348 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
349 /* do_rop: */ \
350 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
03f5781b
WY
351 EMIT1(0xC3); /* retq */ \
352} while (0)
36256009
DB
353# else /* !CONFIG_X86_64 */
354# define RETPOLINE_EDX_BPF_JIT() \
03f5781b
WY
355do { \
356 EMIT1_off32(0xE8, 7); /* call do_rop */ \
357 /* spec_trap: */ \
358 EMIT2(0xF3, 0x90); /* pause */ \
359 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
360 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
361 /* do_rop: */ \
362 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
363 EMIT1(0xC3); /* ret */ \
364} while (0)
36256009 365# endif
03f5781b 366#else /* !CONFIG_RETPOLINE */
36256009
DB
367# ifdef CONFIG_X86_64
368# define RETPOLINE_RAX_BPF_JIT_SIZE 2
369# define RETPOLINE_RAX_BPF_JIT() \
370 EMIT2(0xFF, 0xE0); /* jmp *%rax */
371# else /* !CONFIG_X86_64 */
372# define RETPOLINE_EDX_BPF_JIT() \
373 EMIT2(0xFF, 0xE2) /* jmp *%edx */
374# endif
a493a87f
DB
375#endif
376
7a32fc51 377#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */