1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _ASM_X86_NOSPEC_BRANCH_H_
4 #define _ASM_X86_NOSPEC_BRANCH_H_
6 #include <linux/static_key.h>
7 #include <linux/frame.h>
9 #include <asm/alternative.h>
10 #include <asm/alternative-asm.h>
11 #include <asm/cpufeatures.h>
12 #include <asm/msr-index.h>
13 #include <asm/unwind_hints.h>
14 #include <asm/percpu.h>
16 #include <linux/frame.h>
17 #include <asm/unwind_hints.h>
19 * This should be used immediately before a retpoline alternative. It tells
20 * objtool where the retpolines are so that it can make sense of the control
21 * flow by just reading the original instruction(s) and ignoring the
24 #define ANNOTATE_NOSPEC_ALTERNATIVE \
25 ANNOTATE_IGNORE_ALTERNATIVE
28 * Fill the CPU return stack buffer.
30 * Each entry in the RSB, if used for a speculative 'ret', contains an
31 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
33 * This is required in various cases for retpoline and IBRS-based
34 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
35 * eliminate potentially bogus entries from the RSB, and sometimes
36 * purely to ensure that it doesn't get empty, which on some CPUs would
37 * allow predictions from other (unwanted!) sources to be used.
39 * We define a CPP macro such that it can be used from both .S files and
40 * inline assembly. It's possible to do a .macro and then include that
41 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
44 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
45 #define RSB_FILL_LOOPS 16 /* To avoid underflow */
48 * Google experimented with loop-unrolling and this turned out to be
49 * the optimal version — two calls, each with their own speculation
50 * trap should their return address end up getting used, in a loop.
53 #define __FILL_RETURN_BUFFER(reg, nr, sp) \
56 ANNOTATE_INTRA_FUNCTION_CALL; \
58 773: /* speculation trap */ \
64 ANNOTATE_INTRA_FUNCTION_CALL; \
66 775: /* speculation trap */ \
72 add $(BITS_PER_LONG/8) * 2, sp; \
75 /* barrier for jnz misprediction */ \
79 * i386 doesn't unconditionally have LFENCE, as such it can't
82 #define __FILL_RETURN_BUFFER(reg, nr, sp) \
88 add $(BITS_PER_LONG/8) * nr, sp;
94 * This should be used immediately before an indirect jump/call. It tells
95 * objtool the subsequent indirect jump/call is vouched safe for retpoline
98 .macro ANNOTATE_RETPOLINE_SAFE
100 .pushsection .discard.retpoline_safe
101 _ASM_PTR .Lannotate_\@
106 * These are the bare retpoline primitives for indirect jmp and call.
107 * Do not use these directly; they only exist to make the ALTERNATIVE
108 * invocation below less ugly.
110 .macro RETPOLINE_JMP reg:req
122 * This is a wrapper around RETPOLINE_JMP so the called function in reg
123 * returns to the instruction after the macro.
125 .macro RETPOLINE_CALL reg:req
127 .Ldo_retpoline_jmp_\@:
130 call .Ldo_retpoline_jmp_\@
134 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
135 * indirect jmp/call which may be susceptible to the Spectre variant 2
138 .macro JMP_NOSPEC reg:req
139 #ifdef CONFIG_RETPOLINE
140 ANNOTATE_NOSPEC_ALTERNATIVE
141 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *\reg), \
142 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
143 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *\reg), X86_FEATURE_RETPOLINE_LFENCE
149 .macro CALL_NOSPEC reg:req
150 #ifdef CONFIG_RETPOLINE
151 ANNOTATE_NOSPEC_ALTERNATIVE
152 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *\reg), \
153 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
154 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *\reg), X86_FEATURE_RETPOLINE_LFENCE
160 .macro ISSUE_UNBALANCED_RET_GUARD
161 ANNOTATE_INTRA_FUNCTION_CALL;
162 call .Lunbalanced_ret_guard_\@
164 .Lunbalanced_ret_guard_\@:
165 add $(BITS_PER_LONG/8), %_ASM_SP
170 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
171 * monstrosity above, manually.
173 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2
175 ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
177 ALTERNATIVE_2 "jmp .Lskip_rsb_\@", "", \ftr, "jmp .Lunbalanced_\@", \ftr2
179 __FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
181 ISSUE_UNBALANCED_RET_GUARD
185 #else /* __ASSEMBLY__ */
187 #define ANNOTATE_RETPOLINE_SAFE \
189 ".pushsection .discard.retpoline_safe\n\t" \
190 _ASM_PTR " 999b\n\t" \
193 #ifdef CONFIG_RETPOLINE
197 * Inline asm uses the %V modifier which is only in newer GCC
198 * which is ensured when CONFIG_RETPOLINE is defined.
200 # define CALL_NOSPEC \
201 ANNOTATE_NOSPEC_ALTERNATIVE \
203 ANNOTATE_RETPOLINE_SAFE \
204 "call *%[thunk_target]\n", \
205 "call __x86_indirect_thunk_%V[thunk_target]\n", \
206 X86_FEATURE_RETPOLINE, \
208 ANNOTATE_RETPOLINE_SAFE \
209 "call *%[thunk_target]\n", \
210 X86_FEATURE_RETPOLINE_LFENCE)
211 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
213 #else /* CONFIG_X86_32 */
215 * For i386 we use the original ret-equivalent retpoline, because
216 * otherwise we'll run out of registers. We don't care about CET
219 # define CALL_NOSPEC \
220 ANNOTATE_NOSPEC_ALTERNATIVE \
222 ANNOTATE_RETPOLINE_SAFE \
223 "call *%[thunk_target]\n", \
226 "901: call 903f;\n" \
231 "903: lea 4(%%esp), %%esp;\n" \
232 " pushl %[thunk_target];\n" \
235 "904: call 901b;\n", \
236 X86_FEATURE_RETPOLINE, \
238 ANNOTATE_RETPOLINE_SAFE \
239 "call *%[thunk_target]\n", \
240 X86_FEATURE_RETPOLINE_LFENCE)
242 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
244 #else /* No retpoline for C / inline asm */
245 # define CALL_NOSPEC "call *%[thunk_target]\n"
246 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
249 /* The Spectre V2 mitigation variants */
250 enum spectre_v2_mitigation {
252 SPECTRE_V2_RETPOLINE,
255 SPECTRE_V2_EIBRS_RETPOLINE,
256 SPECTRE_V2_EIBRS_LFENCE,
260 /* The indirect branch speculation control variants */
261 enum spectre_v2_user_mitigation {
262 SPECTRE_V2_USER_NONE,
263 SPECTRE_V2_USER_STRICT,
264 SPECTRE_V2_USER_STRICT_PREFERRED,
265 SPECTRE_V2_USER_PRCTL,
266 SPECTRE_V2_USER_SECCOMP,
269 /* The Speculative Store Bypass disable variants */
270 enum ssb_mitigation {
271 SPEC_STORE_BYPASS_NONE,
272 SPEC_STORE_BYPASS_DISABLE,
273 SPEC_STORE_BYPASS_PRCTL,
274 SPEC_STORE_BYPASS_SECCOMP,
277 extern char __indirect_thunk_start[];
278 extern char __indirect_thunk_end[];
281 * On VMEXIT we must ensure that no RSB predictions learned in the guest
282 * can be followed in the host, by overwriting the RSB completely. Both
283 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
284 * CPUs with IBRS_ALL *might* it be avoided.
286 static inline void vmexit_fill_RSB(void)
288 #ifdef CONFIG_RETPOLINE
291 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
292 ALTERNATIVE("jmp 910f",
293 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
294 X86_FEATURE_RETPOLINE)
296 : "=r" (loops), ASM_CALL_CONSTRAINT
301 static __always_inline
302 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
304 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
307 "d" ((u32)(val >> 32)),
308 [feature] "i" (feature)
312 static inline void indirect_branch_prediction_barrier(void)
314 u64 val = PRED_CMD_IBPB;
316 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
319 /* The Intel SPEC CTRL MSR base value cache */
320 extern u64 x86_spec_ctrl_base;
321 DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
322 extern void update_spec_ctrl_cond(u64 val);
323 extern u64 spec_ctrl_current(void);
326 * With retpoline, we must use IBRS to restrict branch prediction
327 * before calling into firmware.
329 * (Implemented as CPP macros due to header hell.)
331 #define firmware_restrict_branch_speculation_start() \
334 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
335 spec_ctrl_current() | SPEC_CTRL_IBRS, \
336 X86_FEATURE_USE_IBRS_FW); \
339 #define firmware_restrict_branch_speculation_end() \
341 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
342 spec_ctrl_current(), \
343 X86_FEATURE_USE_IBRS_FW); \
347 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
348 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
349 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
351 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
352 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
354 DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
356 #include <asm/segment.h>
359 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
361 * This uses the otherwise unused and obsolete VERW instruction in
362 * combination with microcode which triggers a CPU buffer flush when the
363 * instruction is executed.
365 static __always_inline void mds_clear_cpu_buffers(void)
367 static const u16 ds = __KERNEL_DS;
370 * Has to be the memory-operand variant because only that
371 * guarantees the CPU buffer flush functionality according to
372 * documentation. The register-operand variant does not.
373 * Works with any segment selector, but a valid writable
374 * data segment is the fastest variant.
376 * "cc" clobber is required because VERW modifies ZF.
378 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
382 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
384 * Clear CPU buffers if the corresponding static key is enabled
386 static __always_inline void mds_user_clear_cpu_buffers(void)
388 if (static_branch_likely(&mds_user_clear))
389 mds_clear_cpu_buffers();
393 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
395 * Clear CPU buffers if the corresponding static key is enabled
397 static inline void mds_idle_clear_cpu_buffers(void)
399 if (static_branch_likely(&mds_idle_clear))
400 mds_clear_cpu_buffers();
403 #endif /* __ASSEMBLY__ */
406 * Below is used in the eBPF JIT compiler and emits the byte sequence
407 * for the following assembly:
409 * With retpolines configured:
417 * mov %rax,(%rsp) for x86_64
418 * mov %edx,(%esp) for x86_32
421 * Without retpolines configured:
423 * jmp *%rax for x86_64
424 * jmp *%edx for x86_32
426 #ifdef CONFIG_RETPOLINE
427 # ifdef CONFIG_X86_64
428 # define RETPOLINE_RAX_BPF_JIT_SIZE 17
429 # define RETPOLINE_RAX_BPF_JIT() \
431 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
433 EMIT2(0xF3, 0x90); /* pause */ \
434 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
435 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
437 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
438 EMIT1(0xC3); /* retq */ \
440 # else /* !CONFIG_X86_64 */
441 # define RETPOLINE_EDX_BPF_JIT() \
443 EMIT1_off32(0xE8, 7); /* call do_rop */ \
445 EMIT2(0xF3, 0x90); /* pause */ \
446 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
447 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
449 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
450 EMIT1(0xC3); /* ret */ \
453 #else /* !CONFIG_RETPOLINE */
454 # ifdef CONFIG_X86_64
455 # define RETPOLINE_RAX_BPF_JIT_SIZE 2
456 # define RETPOLINE_RAX_BPF_JIT() \
457 EMIT2(0xFF, 0xE0); /* jmp *%rax */
458 # else /* !CONFIG_X86_64 */
459 # define RETPOLINE_EDX_BPF_JIT() \
460 EMIT2(0xFF, 0xE2) /* jmp *%edx */
464 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */