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/objtool.h>
8 #include <linux/linkage.h>
10 #include <asm/alternative.h>
11 #include <asm/cpufeatures.h>
12 #include <asm/msr-index.h>
13 #include <asm/unwind_hints.h>
14 #include <asm/percpu.h>
15 #include <asm/current.h>
18 * Call depth tracking for Intel SKL CPUs to address the RSB underflow
21 * The tracking does not use a counter. It uses uses arithmetic shift
22 * right on call entry and logical shift left on return.
24 * The depth tracking variable is initialized to 0x8000.... when the call
25 * depth is zero. The arithmetic shift right sign extends the MSB and
26 * saturates after the 12th call. The shift count is 5 for both directions
27 * so the tracking covers 12 nested calls.
30 * 0: 0x8000000000000000 0x0000000000000000
31 * 1: 0xfc00000000000000 0xf000000000000000
33 * 11: 0xfffffffffffffff8 0xfffffffffffffc00
34 * 12: 0xffffffffffffffff 0xffffffffffffffe0
36 * After a return buffer fill the depth is credited 12 calls before the
37 * next stuffing has to take place.
39 * There is a inaccuracy for situations like this:
48 * The shift count might cause this to be off by one in either direction,
49 * but there is still a cushion vs. the RSB depth. The algorithm does not
50 * claim to be perfect and it can be speculated around by the CPU, but it
51 * is considered that it obfuscates the problem enough to make exploitation
52 * extremely difficult.
54 #define RET_DEPTH_SHIFT 5
55 #define RSB_RET_STUFF_LOOPS 16
56 #define RET_DEPTH_INIT 0x8000000000000000ULL
57 #define RET_DEPTH_INIT_FROM_CALL 0xfc00000000000000ULL
58 #define RET_DEPTH_CREDIT 0xffffffffffffffffULL
60 #ifdef CONFIG_CALL_THUNKS_DEBUG
61 # define CALL_THUNKS_DEBUG_INC_CALLS \
62 incq PER_CPU_VAR(__x86_call_count);
63 # define CALL_THUNKS_DEBUG_INC_RETS \
64 incq PER_CPU_VAR(__x86_ret_count);
65 # define CALL_THUNKS_DEBUG_INC_STUFFS \
66 incq PER_CPU_VAR(__x86_stuffs_count);
67 # define CALL_THUNKS_DEBUG_INC_CTXSW \
68 incq PER_CPU_VAR(__x86_ctxsw_count);
70 # define CALL_THUNKS_DEBUG_INC_CALLS
71 # define CALL_THUNKS_DEBUG_INC_RETS
72 # define CALL_THUNKS_DEBUG_INC_STUFFS
73 # define CALL_THUNKS_DEBUG_INC_CTXSW
76 #if defined(CONFIG_CALL_DEPTH_TRACKING) && !defined(COMPILE_OFFSETS)
78 #include <asm/asm-offsets.h>
80 #define CREDIT_CALL_DEPTH \
81 movq $-1, PER_CPU_VAR(pcpu_hot + X86_call_depth);
83 #define RESET_CALL_DEPTH \
86 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth);
88 #define RESET_CALL_DEPTH_FROM_CALL \
91 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
92 CALL_THUNKS_DEBUG_INC_CALLS
94 #define INCREMENT_CALL_DEPTH \
95 sarq $5, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
96 CALL_THUNKS_DEBUG_INC_CALLS
99 #define CREDIT_CALL_DEPTH
100 #define RESET_CALL_DEPTH
101 #define RESET_CALL_DEPTH_FROM_CALL
102 #define INCREMENT_CALL_DEPTH
106 * Fill the CPU return stack buffer.
108 * Each entry in the RSB, if used for a speculative 'ret', contains an
109 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
111 * This is required in various cases for retpoline and IBRS-based
112 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
113 * eliminate potentially bogus entries from the RSB, and sometimes
114 * purely to ensure that it doesn't get empty, which on some CPUs would
115 * allow predictions from other (unwanted!) sources to be used.
117 * We define a CPP macro such that it can be used from both .S files and
118 * inline assembly. It's possible to do a .macro and then include that
119 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
122 #define RETPOLINE_THUNK_SIZE 32
123 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
126 * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
128 #define __FILL_RETURN_SLOT \
129 ANNOTATE_INTRA_FUNCTION_CALL; \
135 * Stuff the entire RSB.
137 * Google experimented with loop-unrolling and this turned out to be
138 * the optimal version - two calls, each with their own speculation
139 * trap should their return address end up getting used, in a loop.
142 #define __FILL_RETURN_BUFFER(reg, nr) \
147 add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
150 /* barrier for jnz misprediction */ \
153 CALL_THUNKS_DEBUG_INC_CTXSW
156 * i386 doesn't unconditionally have LFENCE, as such it can't
159 #define __FILL_RETURN_BUFFER(reg, nr) \
161 __FILL_RETURN_SLOT; \
163 add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
167 * Stuff a single RSB slot.
169 * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
170 * forced to retire before letting a RET instruction execute.
172 * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
175 #define __FILL_ONE_RETURN \
177 add $(BITS_PER_LONG/8), %_ASM_SP; \
183 * This should be used immediately before an indirect jump/call. It tells
184 * objtool the subsequent indirect jump/call is vouched safe for retpoline
187 .macro ANNOTATE_RETPOLINE_SAFE
189 .pushsection .discard.retpoline_safe
195 * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
196 * vs RETBleed validation.
198 #define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
201 * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
202 * eventually turn into its own annotation.
204 .macro VALIDATE_UNRET_END
205 #if defined(CONFIG_NOINSTR_VALIDATION) && \
206 (defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO))
207 ANNOTATE_RETPOLINE_SAFE
213 * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
214 * to the retpoline thunk with a CS prefix when the register requires
215 * a RAX prefix byte to encode. Also see apply_retpolines().
217 .macro __CS_PREFIX reg:req
218 .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
226 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
227 * indirect jmp/call which may be susceptible to the Spectre variant 2
230 * NOTE: these do not take kCFI into account and are thus not comparable to C
231 * indirect calls, take care when using. The target of these should be an ENDBR
232 * instruction irrespective of kCFI.
234 .macro JMP_NOSPEC reg:req
235 #ifdef CONFIG_RETPOLINE
237 jmp __x86_indirect_thunk_\reg
244 .macro CALL_NOSPEC reg:req
245 #ifdef CONFIG_RETPOLINE
247 call __x86_indirect_thunk_\reg
254 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
255 * monstrosity above, manually.
257 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
258 ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
259 __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
260 __stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
265 #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO)
266 #define CALL_UNTRAIN_RET "call entry_untrain_ret"
268 #define CALL_UNTRAIN_RET ""
272 * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
273 * return thunk isn't mapped into the userspace tables (then again, AMD
274 * typically has NO_MELTDOWN).
276 * While retbleed_untrain_ret() doesn't clobber anything but requires stack,
277 * entry_ibpb() will clobber AX, CX, DX.
279 * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
280 * where we have a stack but before any RET instruction.
282 .macro __UNTRAIN_RET ibpb_feature, call_depth_insns
283 #if defined(CONFIG_RETHUNK) || defined(CONFIG_CPU_IBPB_ENTRY)
286 CALL_UNTRAIN_RET, X86_FEATURE_UNRET, \
287 "call entry_ibpb", \ibpb_feature, \
288 __stringify(\call_depth_insns), X86_FEATURE_CALL_DEPTH
292 #define UNTRAIN_RET \
293 __UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH)
295 #define UNTRAIN_RET_VM \
296 __UNTRAIN_RET X86_FEATURE_IBPB_ON_VMEXIT, __stringify(RESET_CALL_DEPTH)
298 #define UNTRAIN_RET_FROM_CALL \
299 __UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
302 .macro CALL_DEPTH_ACCOUNT
303 #ifdef CONFIG_CALL_DEPTH_TRACKING
305 __stringify(INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
309 #else /* __ASSEMBLY__ */
311 #define ANNOTATE_RETPOLINE_SAFE \
313 ".pushsection .discard.retpoline_safe\n\t" \
317 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
318 extern retpoline_thunk_t __x86_indirect_thunk_array[];
319 extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
320 extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
322 #ifdef CONFIG_RETHUNK
323 extern void __x86_return_thunk(void);
325 static inline void __x86_return_thunk(void) {}
328 #ifdef CONFIG_CPU_UNRET_ENTRY
329 extern void retbleed_return_thunk(void);
331 static inline void retbleed_return_thunk(void) {}
334 #ifdef CONFIG_CPU_SRSO
335 extern void srso_return_thunk(void);
336 extern void srso_alias_return_thunk(void);
338 static inline void srso_return_thunk(void) {}
339 static inline void srso_alias_return_thunk(void) {}
342 extern void retbleed_return_thunk(void);
343 extern void srso_return_thunk(void);
344 extern void srso_alias_return_thunk(void);
346 extern void entry_untrain_ret(void);
347 extern void entry_ibpb(void);
349 extern void (*x86_return_thunk)(void);
351 #ifdef CONFIG_CALL_DEPTH_TRACKING
352 extern void call_depth_return_thunk(void);
354 #define CALL_DEPTH_ACCOUNT \
356 __stringify(INCREMENT_CALL_DEPTH), \
357 X86_FEATURE_CALL_DEPTH)
359 #ifdef CONFIG_CALL_THUNKS_DEBUG
360 DECLARE_PER_CPU(u64, __x86_call_count);
361 DECLARE_PER_CPU(u64, __x86_ret_count);
362 DECLARE_PER_CPU(u64, __x86_stuffs_count);
363 DECLARE_PER_CPU(u64, __x86_ctxsw_count);
365 #else /* !CONFIG_CALL_DEPTH_TRACKING */
367 static inline void call_depth_return_thunk(void) {}
368 #define CALL_DEPTH_ACCOUNT ""
370 #endif /* CONFIG_CALL_DEPTH_TRACKING */
372 #ifdef CONFIG_RETPOLINE
375 extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
376 #include <asm/GEN-for-each-reg.h>
380 extern retpoline_thunk_t __x86_indirect_call_thunk_ ## reg;
381 #include <asm/GEN-for-each-reg.h>
385 extern retpoline_thunk_t __x86_indirect_jump_thunk_ ## reg;
386 #include <asm/GEN-for-each-reg.h>
392 * Inline asm uses the %V modifier which is only in newer GCC
393 * which is ensured when CONFIG_RETPOLINE is defined.
395 # define CALL_NOSPEC \
397 ANNOTATE_RETPOLINE_SAFE \
398 "call *%[thunk_target]\n", \
399 "call __x86_indirect_thunk_%V[thunk_target]\n", \
400 X86_FEATURE_RETPOLINE, \
402 ANNOTATE_RETPOLINE_SAFE \
403 "call *%[thunk_target]\n", \
404 X86_FEATURE_RETPOLINE_LFENCE)
406 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
408 #else /* CONFIG_X86_32 */
410 * For i386 we use the original ret-equivalent retpoline, because
411 * otherwise we'll run out of registers. We don't care about CET
414 # define CALL_NOSPEC \
416 ANNOTATE_RETPOLINE_SAFE \
417 "call *%[thunk_target]\n", \
420 "901: call 903f;\n" \
425 "903: lea 4(%%esp), %%esp;\n" \
426 " pushl %[thunk_target];\n" \
429 "904: call 901b;\n", \
430 X86_FEATURE_RETPOLINE, \
432 ANNOTATE_RETPOLINE_SAFE \
433 "call *%[thunk_target]\n", \
434 X86_FEATURE_RETPOLINE_LFENCE)
436 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
438 #else /* No retpoline for C / inline asm */
439 # define CALL_NOSPEC "call *%[thunk_target]\n"
440 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
443 /* The Spectre V2 mitigation variants */
444 enum spectre_v2_mitigation {
446 SPECTRE_V2_RETPOLINE,
449 SPECTRE_V2_EIBRS_RETPOLINE,
450 SPECTRE_V2_EIBRS_LFENCE,
454 /* The indirect branch speculation control variants */
455 enum spectre_v2_user_mitigation {
456 SPECTRE_V2_USER_NONE,
457 SPECTRE_V2_USER_STRICT,
458 SPECTRE_V2_USER_STRICT_PREFERRED,
459 SPECTRE_V2_USER_PRCTL,
460 SPECTRE_V2_USER_SECCOMP,
463 /* The Speculative Store Bypass disable variants */
464 enum ssb_mitigation {
465 SPEC_STORE_BYPASS_NONE,
466 SPEC_STORE_BYPASS_DISABLE,
467 SPEC_STORE_BYPASS_PRCTL,
468 SPEC_STORE_BYPASS_SECCOMP,
471 static __always_inline
472 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
474 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
477 "d" ((u32)(val >> 32)),
478 [feature] "i" (feature)
482 extern u64 x86_pred_cmd;
484 static inline void indirect_branch_prediction_barrier(void)
486 alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
489 /* The Intel SPEC CTRL MSR base value cache */
490 extern u64 x86_spec_ctrl_base;
491 DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
492 extern void update_spec_ctrl_cond(u64 val);
493 extern u64 spec_ctrl_current(void);
496 * With retpoline, we must use IBRS to restrict branch prediction
497 * before calling into firmware.
499 * (Implemented as CPP macros due to header hell.)
501 #define firmware_restrict_branch_speculation_start() \
504 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
505 spec_ctrl_current() | SPEC_CTRL_IBRS, \
506 X86_FEATURE_USE_IBRS_FW); \
507 alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
508 X86_FEATURE_USE_IBPB_FW); \
511 #define firmware_restrict_branch_speculation_end() \
513 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
514 spec_ctrl_current(), \
515 X86_FEATURE_USE_IBRS_FW); \
519 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
520 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
521 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
523 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
524 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
526 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
528 DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
530 #include <asm/segment.h>
533 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
535 * This uses the otherwise unused and obsolete VERW instruction in
536 * combination with microcode which triggers a CPU buffer flush when the
537 * instruction is executed.
539 static __always_inline void mds_clear_cpu_buffers(void)
541 static const u16 ds = __KERNEL_DS;
544 * Has to be the memory-operand variant because only that
545 * guarantees the CPU buffer flush functionality according to
546 * documentation. The register-operand variant does not.
547 * Works with any segment selector, but a valid writable
548 * data segment is the fastest variant.
550 * "cc" clobber is required because VERW modifies ZF.
552 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
556 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
558 * Clear CPU buffers if the corresponding static key is enabled
560 static __always_inline void mds_user_clear_cpu_buffers(void)
562 if (static_branch_likely(&mds_user_clear))
563 mds_clear_cpu_buffers();
567 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
569 * Clear CPU buffers if the corresponding static key is enabled
571 static __always_inline void mds_idle_clear_cpu_buffers(void)
573 if (static_branch_likely(&mds_idle_clear))
574 mds_clear_cpu_buffers();
577 #endif /* __ASSEMBLY__ */
579 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */