blob: 4d0f5386e637ba09361f36abddc7fb8ac0fe3bde [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef _ASM_X86_NOSPEC_BRANCH_H_
4#define _ASM_X86_NOSPEC_BRANCH_H_
5
6#include <linux/static_key.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02007#include <linux/objtool.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008
9#include <asm/alternative.h>
10#include <asm/alternative-asm.h>
11#include <asm/cpufeatures.h>
12#include <asm/msr-index.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <asm/unwind_hints.h>
David Brazdil0f672f62019-12-10 10:32:29 +000014
15/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016 * Fill the CPU return stack buffer.
17 *
18 * Each entry in the RSB, if used for a speculative 'ret', contains an
19 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
20 *
21 * This is required in various cases for retpoline and IBRS-based
22 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
23 * eliminate potentially bogus entries from the RSB, and sometimes
24 * purely to ensure that it doesn't get empty, which on some CPUs would
25 * allow predictions from other (unwanted!) sources to be used.
26 *
27 * We define a CPP macro such that it can be used from both .S files and
28 * inline assembly. It's possible to do a .macro and then include that
29 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
30 */
31
32#define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033
34/*
35 * Google experimented with loop-unrolling and this turned out to be
36 * the optimal version — two calls, each with their own speculation
37 * trap should their return address end up getting used, in a loop.
38 */
39#define __FILL_RETURN_BUFFER(reg, nr, sp) \
40 mov $(nr/2), reg; \
41771: \
Olivier Deprez157378f2022-04-04 15:47:50 +020042 ANNOTATE_INTRA_FUNCTION_CALL; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 call 772f; \
44773: /* speculation trap */ \
Olivier Deprez157378f2022-04-04 15:47:50 +020045 UNWIND_HINT_EMPTY; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 pause; \
47 lfence; \
48 jmp 773b; \
49772: \
Olivier Deprez157378f2022-04-04 15:47:50 +020050 ANNOTATE_INTRA_FUNCTION_CALL; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 call 774f; \
52775: /* speculation trap */ \
Olivier Deprez157378f2022-04-04 15:47:50 +020053 UNWIND_HINT_EMPTY; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 pause; \
55 lfence; \
56 jmp 775b; \
57774: \
Olivier Deprez157378f2022-04-04 15:47:50 +020058 add $(BITS_PER_LONG/8) * 2, sp; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 dec reg; \
Olivier Deprez157378f2022-04-04 15:47:50 +020060 jnz 771b;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061
62#ifdef __ASSEMBLY__
63
64/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065 * This should be used immediately before an indirect jump/call. It tells
66 * objtool the subsequent indirect jump/call is vouched safe for retpoline
67 * builds.
68 */
69.macro ANNOTATE_RETPOLINE_SAFE
70 .Lannotate_\@:
71 .pushsection .discard.retpoline_safe
72 _ASM_PTR .Lannotate_\@
73 .popsection
74.endm
75
76/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
78 * indirect jmp/call which may be susceptible to the Spectre variant 2
79 * attack.
80 */
81.macro JMP_NOSPEC reg:req
82#ifdef CONFIG_RETPOLINE
Olivier Deprez157378f2022-04-04 15:47:50 +020083 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
84 __stringify(jmp __x86_retpoline_\reg), X86_FEATURE_RETPOLINE, \
85 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_LFENCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086#else
Olivier Deprez157378f2022-04-04 15:47:50 +020087 jmp *%\reg
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088#endif
89.endm
90
91.macro CALL_NOSPEC reg:req
92#ifdef CONFIG_RETPOLINE
Olivier Deprez157378f2022-04-04 15:47:50 +020093 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \
94 __stringify(call __x86_retpoline_\reg), X86_FEATURE_RETPOLINE, \
95 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_LFENCE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096#else
Olivier Deprez157378f2022-04-04 15:47:50 +020097 call *%\reg
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098#endif
99.endm
100
101 /*
102 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
103 * monstrosity above, manually.
104 */
105.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
106#ifdef CONFIG_RETPOLINE
Olivier Deprez157378f2022-04-04 15:47:50 +0200107 ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
108 __FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109.Lskip_rsb_\@:
110#endif
111.endm
112
113#else /* __ASSEMBLY__ */
114
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115#define ANNOTATE_RETPOLINE_SAFE \
116 "999:\n\t" \
117 ".pushsection .discard.retpoline_safe\n\t" \
118 _ASM_PTR " 999b\n\t" \
119 ".popsection\n\t"
120
121#ifdef CONFIG_RETPOLINE
122#ifdef CONFIG_X86_64
123
124/*
125 * Inline asm uses the %V modifier which is only in newer GCC
126 * which is ensured when CONFIG_RETPOLINE is defined.
127 */
128# define CALL_NOSPEC \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129 ALTERNATIVE_2( \
130 ANNOTATE_RETPOLINE_SAFE \
131 "call *%[thunk_target]\n", \
Olivier Deprez157378f2022-04-04 15:47:50 +0200132 "call __x86_retpoline_%V[thunk_target]\n", \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 X86_FEATURE_RETPOLINE, \
134 "lfence;\n" \
135 ANNOTATE_RETPOLINE_SAFE \
136 "call *%[thunk_target]\n", \
Olivier Deprez157378f2022-04-04 15:47:50 +0200137 X86_FEATURE_RETPOLINE_LFENCE)
138
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139# define THUNK_TARGET(addr) [thunk_target] "r" (addr)
140
141#else /* CONFIG_X86_32 */
142/*
143 * For i386 we use the original ret-equivalent retpoline, because
144 * otherwise we'll run out of registers. We don't care about CET
145 * here, anyway.
146 */
147# define CALL_NOSPEC \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 ALTERNATIVE_2( \
149 ANNOTATE_RETPOLINE_SAFE \
150 "call *%[thunk_target]\n", \
151 " jmp 904f;\n" \
152 " .align 16\n" \
153 "901: call 903f;\n" \
154 "902: pause;\n" \
155 " lfence;\n" \
156 " jmp 902b;\n" \
157 " .align 16\n" \
David Brazdil0f672f62019-12-10 10:32:29 +0000158 "903: lea 4(%%esp), %%esp;\n" \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159 " pushl %[thunk_target];\n" \
160 " ret;\n" \
161 " .align 16\n" \
162 "904: call 901b;\n", \
163 X86_FEATURE_RETPOLINE, \
164 "lfence;\n" \
165 ANNOTATE_RETPOLINE_SAFE \
166 "call *%[thunk_target]\n", \
Olivier Deprez157378f2022-04-04 15:47:50 +0200167 X86_FEATURE_RETPOLINE_LFENCE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168
169# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
170#endif
171#else /* No retpoline for C / inline asm */
172# define CALL_NOSPEC "call *%[thunk_target]\n"
173# define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
174#endif
175
176/* The Spectre V2 mitigation variants */
177enum spectre_v2_mitigation {
178 SPECTRE_V2_NONE,
Olivier Deprez157378f2022-04-04 15:47:50 +0200179 SPECTRE_V2_RETPOLINE,
180 SPECTRE_V2_LFENCE,
181 SPECTRE_V2_EIBRS,
182 SPECTRE_V2_EIBRS_RETPOLINE,
183 SPECTRE_V2_EIBRS_LFENCE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000184};
185
186/* The indirect branch speculation control variants */
187enum spectre_v2_user_mitigation {
188 SPECTRE_V2_USER_NONE,
189 SPECTRE_V2_USER_STRICT,
David Brazdil0f672f62019-12-10 10:32:29 +0000190 SPECTRE_V2_USER_STRICT_PREFERRED,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000191 SPECTRE_V2_USER_PRCTL,
192 SPECTRE_V2_USER_SECCOMP,
193};
194
195/* The Speculative Store Bypass disable variants */
196enum ssb_mitigation {
197 SPEC_STORE_BYPASS_NONE,
198 SPEC_STORE_BYPASS_DISABLE,
199 SPEC_STORE_BYPASS_PRCTL,
200 SPEC_STORE_BYPASS_SECCOMP,
201};
202
203extern char __indirect_thunk_start[];
204extern char __indirect_thunk_end[];
205
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206static __always_inline
207void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
208{
209 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
210 : : "c" (msr),
211 "a" ((u32)val),
212 "d" ((u32)(val >> 32)),
213 [feature] "i" (feature)
214 : "memory");
215}
216
217static inline void indirect_branch_prediction_barrier(void)
218{
219 u64 val = PRED_CMD_IBPB;
220
221 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
222}
223
224/* The Intel SPEC CTRL MSR base value cache */
225extern u64 x86_spec_ctrl_base;
226
227/*
228 * With retpoline, we must use IBRS to restrict branch prediction
229 * before calling into firmware.
230 *
231 * (Implemented as CPP macros due to header hell.)
232 */
233#define firmware_restrict_branch_speculation_start() \
234do { \
235 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
236 \
237 preempt_disable(); \
238 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
239 X86_FEATURE_USE_IBRS_FW); \
240} while (0)
241
242#define firmware_restrict_branch_speculation_end() \
243do { \
244 u64 val = x86_spec_ctrl_base; \
245 \
246 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
247 X86_FEATURE_USE_IBRS_FW); \
248 preempt_enable(); \
249} while (0)
250
251DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
252DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
253DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
254
David Brazdil0f672f62019-12-10 10:32:29 +0000255DECLARE_STATIC_KEY_FALSE(mds_user_clear);
256DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
257
258#include <asm/segment.h>
259
260/**
261 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
262 *
263 * This uses the otherwise unused and obsolete VERW instruction in
264 * combination with microcode which triggers a CPU buffer flush when the
265 * instruction is executed.
266 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200267static __always_inline void mds_clear_cpu_buffers(void)
David Brazdil0f672f62019-12-10 10:32:29 +0000268{
269 static const u16 ds = __KERNEL_DS;
270
271 /*
272 * Has to be the memory-operand variant because only that
273 * guarantees the CPU buffer flush functionality according to
274 * documentation. The register-operand variant does not.
275 * Works with any segment selector, but a valid writable
276 * data segment is the fastest variant.
277 *
278 * "cc" clobber is required because VERW modifies ZF.
279 */
280 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
281}
282
283/**
284 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
285 *
286 * Clear CPU buffers if the corresponding static key is enabled
287 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200288static __always_inline void mds_user_clear_cpu_buffers(void)
David Brazdil0f672f62019-12-10 10:32:29 +0000289{
290 if (static_branch_likely(&mds_user_clear))
291 mds_clear_cpu_buffers();
292}
293
294/**
295 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
296 *
297 * Clear CPU buffers if the corresponding static key is enabled
298 */
299static inline void mds_idle_clear_cpu_buffers(void)
300{
301 if (static_branch_likely(&mds_idle_clear))
302 mds_clear_cpu_buffers();
303}
304
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305#endif /* __ASSEMBLY__ */
306
307/*
308 * Below is used in the eBPF JIT compiler and emits the byte sequence
309 * for the following assembly:
310 *
311 * With retpolines configured:
312 *
313 * callq do_rop
314 * spec_trap:
315 * pause
316 * lfence
317 * jmp spec_trap
318 * do_rop:
Olivier Deprez157378f2022-04-04 15:47:50 +0200319 * mov %rcx,(%rsp) for x86_64
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000320 * mov %edx,(%esp) for x86_32
321 * retq
322 *
323 * Without retpolines configured:
324 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200325 * jmp *%rcx for x86_64
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 * jmp *%edx for x86_32
327 */
328#ifdef CONFIG_RETPOLINE
329# ifdef CONFIG_X86_64
Olivier Deprez157378f2022-04-04 15:47:50 +0200330# define RETPOLINE_RCX_BPF_JIT_SIZE 17
331# define RETPOLINE_RCX_BPF_JIT() \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332do { \
333 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
334 /* spec_trap: */ \
335 EMIT2(0xF3, 0x90); /* pause */ \
336 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
337 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
338 /* do_rop: */ \
Olivier Deprez157378f2022-04-04 15:47:50 +0200339 EMIT4(0x48, 0x89, 0x0C, 0x24); /* mov %rcx,(%rsp) */ \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 EMIT1(0xC3); /* retq */ \
341} while (0)
342# else /* !CONFIG_X86_64 */
343# define RETPOLINE_EDX_BPF_JIT() \
344do { \
345 EMIT1_off32(0xE8, 7); /* call do_rop */ \
346 /* spec_trap: */ \
347 EMIT2(0xF3, 0x90); /* pause */ \
348 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
349 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
350 /* do_rop: */ \
351 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
352 EMIT1(0xC3); /* ret */ \
353} while (0)
354# endif
355#else /* !CONFIG_RETPOLINE */
356# ifdef CONFIG_X86_64
Olivier Deprez157378f2022-04-04 15:47:50 +0200357# define RETPOLINE_RCX_BPF_JIT_SIZE 2
358# define RETPOLINE_RCX_BPF_JIT() \
359 EMIT2(0xFF, 0xE1); /* jmp *%rcx */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000360# else /* !CONFIG_X86_64 */
361# define RETPOLINE_EDX_BPF_JIT() \
362 EMIT2(0xFF, 0xE2) /* jmp *%edx */
363# endif
364#endif
365
366#endif /* _ASM_X86_NOSPEC_BRANCH_H_ */