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
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 %gs:__x86_call_count;
63 # define CALL_THUNKS_DEBUG_INC_RETS \
64 incq %gs:__x86_ret_count;
65 # define CALL_THUNKS_DEBUG_INC_STUFFS \
66 incq %gs:__x86_stuffs_count;
67 # define CALL_THUNKS_DEBUG_INC_CTXSW \
68 incq %gs:__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 ASM_CREDIT_CALL_DEPTH \
84 movq $-1, PER_CPU_VAR(pcpu_hot + X86_call_depth);
86 #define RESET_CALL_DEPTH \
89 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth);
91 #define RESET_CALL_DEPTH_FROM_CALL \
94 movq %rax, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
95 CALL_THUNKS_DEBUG_INC_CALLS
97 #define INCREMENT_CALL_DEPTH \
98 sarq $5, %gs:pcpu_hot + X86_call_depth; \
99 CALL_THUNKS_DEBUG_INC_CALLS
101 #define ASM_INCREMENT_CALL_DEPTH \
102 sarq $5, PER_CPU_VAR(pcpu_hot + X86_call_depth); \
103 CALL_THUNKS_DEBUG_INC_CALLS
106 #define CREDIT_CALL_DEPTH
107 #define ASM_CREDIT_CALL_DEPTH
108 #define RESET_CALL_DEPTH
109 #define INCREMENT_CALL_DEPTH
110 #define ASM_INCREMENT_CALL_DEPTH
111 #define RESET_CALL_DEPTH_FROM_CALL
115 * Fill the CPU return stack buffer.
117 * Each entry in the RSB, if used for a speculative 'ret', contains an
118 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
120 * This is required in various cases for retpoline and IBRS-based
121 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
122 * eliminate potentially bogus entries from the RSB, and sometimes
123 * purely to ensure that it doesn't get empty, which on some CPUs would
124 * allow predictions from other (unwanted!) sources to be used.
126 * We define a CPP macro such that it can be used from both .S files and
127 * inline assembly. It's possible to do a .macro and then include that
128 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
131 #define RETPOLINE_THUNK_SIZE 32
132 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
135 * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
137 #define __FILL_RETURN_SLOT \
138 ANNOTATE_INTRA_FUNCTION_CALL; \
144 * Stuff the entire RSB.
146 * Google experimented with loop-unrolling and this turned out to be
147 * the optimal version - two calls, each with their own speculation
148 * trap should their return address end up getting used, in a loop.
151 #define __FILL_RETURN_BUFFER(reg, nr) \
156 add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
159 /* barrier for jnz misprediction */ \
161 ASM_CREDIT_CALL_DEPTH \
162 CALL_THUNKS_DEBUG_INC_CTXSW
165 * i386 doesn't unconditionally have LFENCE, as such it can't
168 #define __FILL_RETURN_BUFFER(reg, nr) \
170 __FILL_RETURN_SLOT; \
172 add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
176 * Stuff a single RSB slot.
178 * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
179 * forced to retire before letting a RET instruction execute.
181 * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
184 #define __FILL_ONE_RETURN \
186 add $(BITS_PER_LONG/8), %_ASM_SP; \
192 * This should be used immediately before an indirect jump/call. It tells
193 * objtool the subsequent indirect jump/call is vouched safe for retpoline
196 .macro ANNOTATE_RETPOLINE_SAFE
198 .pushsection .discard.retpoline_safe
204 * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
205 * vs RETBleed validation.
207 #define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
210 * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
211 * eventually turn into it's own annotation.
213 .macro VALIDATE_UNRET_END
214 #if defined(CONFIG_NOINSTR_VALIDATION) && defined(CONFIG_CPU_UNRET_ENTRY)
215 ANNOTATE_RETPOLINE_SAFE
221 * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
222 * to the retpoline thunk with a CS prefix when the register requires
223 * a RAX prefix byte to encode. Also see apply_retpolines().
225 .macro __CS_PREFIX reg:req
226 .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
234 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
235 * indirect jmp/call which may be susceptible to the Spectre variant 2
238 .macro JMP_NOSPEC reg:req
239 #ifdef CONFIG_RETPOLINE
241 jmp __x86_indirect_thunk_\reg
248 .macro CALL_NOSPEC reg:req
249 #ifdef CONFIG_RETPOLINE
251 call __x86_indirect_thunk_\reg
258 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
259 * monstrosity above, manually.
261 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
262 ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
263 __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
264 __stringify(nop;nop;__FILL_ONE_RETURN), \ftr2
269 #ifdef CONFIG_CPU_UNRET_ENTRY
270 #define CALL_ZEN_UNTRAIN_RET "call zen_untrain_ret"
272 #define CALL_ZEN_UNTRAIN_RET ""
276 * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
277 * return thunk isn't mapped into the userspace tables (then again, AMD
278 * typically has NO_MELTDOWN).
280 * While zen_untrain_ret() doesn't clobber anything but requires stack,
281 * entry_ibpb() will clobber AX, CX, DX.
283 * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
284 * where we have a stack but before any RET instruction.
287 #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY) || \
288 defined(CONFIG_CALL_DEPTH_TRACKING)
291 CALL_ZEN_UNTRAIN_RET, X86_FEATURE_UNRET, \
292 "call entry_ibpb", X86_FEATURE_ENTRY_IBPB, \
293 __stringify(RESET_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
297 .macro UNTRAIN_RET_FROM_CALL
298 #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY) || \
299 defined(CONFIG_CALL_DEPTH_TRACKING)
302 CALL_ZEN_UNTRAIN_RET, X86_FEATURE_UNRET, \
303 "call entry_ibpb", X86_FEATURE_ENTRY_IBPB, \
304 __stringify(RESET_CALL_DEPTH_FROM_CALL), X86_FEATURE_CALL_DEPTH
309 .macro CALL_DEPTH_ACCOUNT
310 #ifdef CONFIG_CALL_DEPTH_TRACKING
312 __stringify(ASM_INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH
316 #else /* __ASSEMBLY__ */
318 #define ANNOTATE_RETPOLINE_SAFE \
320 ".pushsection .discard.retpoline_safe\n\t" \
321 ".long 999b - .\n\t" \
324 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
325 extern retpoline_thunk_t __x86_indirect_thunk_array[];
326 extern retpoline_thunk_t __x86_indirect_call_thunk_array[];
327 extern retpoline_thunk_t __x86_indirect_jump_thunk_array[];
329 extern void __x86_return_thunk(void);
330 extern void zen_untrain_ret(void);
331 extern void entry_ibpb(void);
333 #ifdef CONFIG_CALL_THUNKS
334 extern void (*x86_return_thunk)(void);
336 #define x86_return_thunk (&__x86_return_thunk)
339 #ifdef CONFIG_CALL_DEPTH_TRACKING
340 extern void __x86_return_skl(void);
342 static inline void x86_set_skl_return_thunk(void)
344 x86_return_thunk = &__x86_return_skl;
347 #define CALL_DEPTH_ACCOUNT \
349 __stringify(INCREMENT_CALL_DEPTH), \
350 X86_FEATURE_CALL_DEPTH)
352 #ifdef CONFIG_CALL_THUNKS_DEBUG
353 DECLARE_PER_CPU(u64, __x86_call_count);
354 DECLARE_PER_CPU(u64, __x86_ret_count);
355 DECLARE_PER_CPU(u64, __x86_stuffs_count);
356 DECLARE_PER_CPU(u64, __x86_ctxsw_count);
359 static inline void x86_set_skl_return_thunk(void) {}
361 #define CALL_DEPTH_ACCOUNT ""
365 #ifdef CONFIG_RETPOLINE
368 extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
369 #include <asm/GEN-for-each-reg.h>
373 extern retpoline_thunk_t __x86_indirect_call_thunk_ ## reg;
374 #include <asm/GEN-for-each-reg.h>
378 extern retpoline_thunk_t __x86_indirect_jump_thunk_ ## reg;
379 #include <asm/GEN-for-each-reg.h>
385 * Inline asm uses the %V modifier which is only in newer GCC
386 * which is ensured when CONFIG_RETPOLINE is defined.
388 # define CALL_NOSPEC \
390 ANNOTATE_RETPOLINE_SAFE \
391 "call *%[thunk_target]\n", \
392 "call __x86_indirect_thunk_%V[thunk_target]\n", \
393 X86_FEATURE_RETPOLINE, \
395 ANNOTATE_RETPOLINE_SAFE \
396 "call *%[thunk_target]\n", \
397 X86_FEATURE_RETPOLINE_LFENCE)
399 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
401 #else /* CONFIG_X86_32 */
403 * For i386 we use the original ret-equivalent retpoline, because
404 * otherwise we'll run out of registers. We don't care about CET
407 # define CALL_NOSPEC \
409 ANNOTATE_RETPOLINE_SAFE \
410 "call *%[thunk_target]\n", \
413 "901: call 903f;\n" \
418 "903: lea 4(%%esp), %%esp;\n" \
419 " pushl %[thunk_target];\n" \
422 "904: call 901b;\n", \
423 X86_FEATURE_RETPOLINE, \
425 ANNOTATE_RETPOLINE_SAFE \
426 "call *%[thunk_target]\n", \
427 X86_FEATURE_RETPOLINE_LFENCE)
429 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
431 #else /* No retpoline for C / inline asm */
432 # define CALL_NOSPEC "call *%[thunk_target]\n"
433 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
436 /* The Spectre V2 mitigation variants */
437 enum spectre_v2_mitigation {
439 SPECTRE_V2_RETPOLINE,
442 SPECTRE_V2_EIBRS_RETPOLINE,
443 SPECTRE_V2_EIBRS_LFENCE,
447 /* The indirect branch speculation control variants */
448 enum spectre_v2_user_mitigation {
449 SPECTRE_V2_USER_NONE,
450 SPECTRE_V2_USER_STRICT,
451 SPECTRE_V2_USER_STRICT_PREFERRED,
452 SPECTRE_V2_USER_PRCTL,
453 SPECTRE_V2_USER_SECCOMP,
456 /* The Speculative Store Bypass disable variants */
457 enum ssb_mitigation {
458 SPEC_STORE_BYPASS_NONE,
459 SPEC_STORE_BYPASS_DISABLE,
460 SPEC_STORE_BYPASS_PRCTL,
461 SPEC_STORE_BYPASS_SECCOMP,
464 extern char __indirect_thunk_start[];
465 extern char __indirect_thunk_end[];
467 static __always_inline
468 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
470 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
473 "d" ((u32)(val >> 32)),
474 [feature] "i" (feature)
478 static inline void indirect_branch_prediction_barrier(void)
480 u64 val = PRED_CMD_IBPB;
482 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
485 /* The Intel SPEC CTRL MSR base value cache */
486 extern u64 x86_spec_ctrl_base;
487 DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
488 extern void update_spec_ctrl_cond(u64 val);
489 extern u64 spec_ctrl_current(void);
492 * With retpoline, we must use IBRS to restrict branch prediction
493 * before calling into firmware.
495 * (Implemented as CPP macros due to header hell.)
497 #define firmware_restrict_branch_speculation_start() \
500 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
501 spec_ctrl_current() | SPEC_CTRL_IBRS, \
502 X86_FEATURE_USE_IBRS_FW); \
503 alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
504 X86_FEATURE_USE_IBPB_FW); \
507 #define firmware_restrict_branch_speculation_end() \
509 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
510 spec_ctrl_current(), \
511 X86_FEATURE_USE_IBRS_FW); \
515 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
516 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
517 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
519 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
520 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
522 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
524 DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
526 #include <asm/segment.h>
529 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
531 * This uses the otherwise unused and obsolete VERW instruction in
532 * combination with microcode which triggers a CPU buffer flush when the
533 * instruction is executed.
535 static __always_inline void mds_clear_cpu_buffers(void)
537 static const u16 ds = __KERNEL_DS;
540 * Has to be the memory-operand variant because only that
541 * guarantees the CPU buffer flush functionality according to
542 * documentation. The register-operand variant does not.
543 * Works with any segment selector, but a valid writable
544 * data segment is the fastest variant.
546 * "cc" clobber is required because VERW modifies ZF.
548 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
552 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
554 * Clear CPU buffers if the corresponding static key is enabled
556 static __always_inline void mds_user_clear_cpu_buffers(void)
558 if (static_branch_likely(&mds_user_clear))
559 mds_clear_cpu_buffers();
563 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
565 * Clear CPU buffers if the corresponding static key is enabled
567 static __always_inline void mds_idle_clear_cpu_buffers(void)
569 if (static_branch_likely(&mds_idle_clear))
570 mds_clear_cpu_buffers();
573 #endif /* __ASSEMBLY__ */
575 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */