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>
16 #define RETPOLINE_THUNK_SIZE 32
19 * Fill the CPU return stack buffer.
21 * Each entry in the RSB, if used for a speculative 'ret', contains an
22 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
24 * This is required in various cases for retpoline and IBRS-based
25 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
26 * eliminate potentially bogus entries from the RSB, and sometimes
27 * purely to ensure that it doesn't get empty, which on some CPUs would
28 * allow predictions from other (unwanted!) sources to be used.
30 * We define a CPP macro such that it can be used from both .S files and
31 * inline assembly. It's possible to do a .macro and then include that
32 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
35 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
38 * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN.
40 #define __FILL_RETURN_SLOT \
41 ANNOTATE_INTRA_FUNCTION_CALL; \
47 * Stuff the entire RSB.
49 * Google experimented with loop-unrolling and this turned out to be
50 * the optimal version - two calls, each with their own speculation
51 * trap should their return address end up getting used, in a loop.
54 #define __FILL_RETURN_BUFFER(reg, nr) \
59 add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \
62 /* barrier for jnz misprediction */ \
66 * i386 doesn't unconditionally have LFENCE, as such it can't
69 #define __FILL_RETURN_BUFFER(reg, nr) \
73 add $(BITS_PER_LONG/8) * nr, %_ASM_SP;
77 * Stuff a single RSB slot.
79 * To mitigate Post-Barrier RSB speculation, one CALL instruction must be
80 * forced to retire before letting a RET instruction execute.
82 * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed
85 #define __FILL_ONE_RETURN \
87 add $(BITS_PER_LONG/8), %_ASM_SP; \
93 * This should be used immediately before an indirect jump/call. It tells
94 * objtool the subsequent indirect jump/call is vouched safe for retpoline
97 .macro ANNOTATE_RETPOLINE_SAFE
99 .pushsection .discard.retpoline_safe
100 _ASM_PTR .Lannotate_\@
105 * (ab)use RETPOLINE_SAFE on RET to annotate away 'bare' RET instructions
106 * vs RETBleed validation.
108 #define ANNOTATE_UNRET_SAFE ANNOTATE_RETPOLINE_SAFE
111 * Abuse ANNOTATE_RETPOLINE_SAFE on a NOP to indicate UNRET_END, should
112 * eventually turn into it's own annotation.
114 .macro ANNOTATE_UNRET_END
115 #ifdef CONFIG_DEBUG_ENTRY
116 ANNOTATE_RETPOLINE_SAFE
122 * Equivalent to -mindirect-branch-cs-prefix; emit the 5 byte jmp/call
123 * to the retpoline thunk with a CS prefix when the register requires
124 * a RAX prefix byte to encode. Also see apply_retpolines().
126 .macro __CS_PREFIX reg:req
127 .irp rs,r8,r9,r10,r11,r12,r13,r14,r15
135 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
136 * indirect jmp/call which may be susceptible to the Spectre variant 2
139 .macro JMP_NOSPEC reg:req
140 #ifdef CONFIG_RETPOLINE
142 jmp __x86_indirect_thunk_\reg
149 .macro CALL_NOSPEC reg:req
150 #ifdef CONFIG_RETPOLINE
152 call __x86_indirect_thunk_\reg
159 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
160 * monstrosity above, manually.
162 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS)
163 ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \
164 __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \
165 __stringify(__FILL_ONE_RETURN), \ftr2
170 #ifdef CONFIG_CPU_UNRET_ENTRY
171 #define CALL_ZEN_UNTRAIN_RET "call zen_untrain_ret"
173 #define CALL_ZEN_UNTRAIN_RET ""
177 * Mitigate RETBleed for AMD/Hygon Zen uarch. Requires KERNEL CR3 because the
178 * return thunk isn't mapped into the userspace tables (then again, AMD
179 * typically has NO_MELTDOWN).
181 * While zen_untrain_ret() doesn't clobber anything but requires stack,
182 * entry_ibpb() will clobber AX, CX, DX.
184 * As such, this must be placed after every *SWITCH_TO_KERNEL_CR3 at a point
185 * where we have a stack but before any RET instruction.
188 #if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_IBPB_ENTRY)
191 CALL_ZEN_UNTRAIN_RET, X86_FEATURE_UNRET, \
192 "call entry_ibpb", X86_FEATURE_ENTRY_IBPB
196 #else /* __ASSEMBLY__ */
198 #define ANNOTATE_RETPOLINE_SAFE \
200 ".pushsection .discard.retpoline_safe\n\t" \
201 _ASM_PTR " 999b\n\t" \
204 typedef u8 retpoline_thunk_t[RETPOLINE_THUNK_SIZE];
205 extern retpoline_thunk_t __x86_indirect_thunk_array[];
207 extern void __x86_return_thunk(void);
208 extern void zen_untrain_ret(void);
209 extern void entry_ibpb(void);
211 #ifdef CONFIG_RETPOLINE
214 extern retpoline_thunk_t __x86_indirect_thunk_ ## reg;
215 #include <asm/GEN-for-each-reg.h>
221 * Inline asm uses the %V modifier which is only in newer GCC
222 * which is ensured when CONFIG_RETPOLINE is defined.
224 # define CALL_NOSPEC \
226 ANNOTATE_RETPOLINE_SAFE \
227 "call *%[thunk_target]\n", \
228 "call __x86_indirect_thunk_%V[thunk_target]\n", \
229 X86_FEATURE_RETPOLINE, \
231 ANNOTATE_RETPOLINE_SAFE \
232 "call *%[thunk_target]\n", \
233 X86_FEATURE_RETPOLINE_LFENCE)
235 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
237 #else /* CONFIG_X86_32 */
239 * For i386 we use the original ret-equivalent retpoline, because
240 * otherwise we'll run out of registers. We don't care about CET
243 # define CALL_NOSPEC \
245 ANNOTATE_RETPOLINE_SAFE \
246 "call *%[thunk_target]\n", \
249 "901: call 903f;\n" \
254 "903: lea 4(%%esp), %%esp;\n" \
255 " pushl %[thunk_target];\n" \
258 "904: call 901b;\n", \
259 X86_FEATURE_RETPOLINE, \
261 ANNOTATE_RETPOLINE_SAFE \
262 "call *%[thunk_target]\n", \
263 X86_FEATURE_RETPOLINE_LFENCE)
265 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
267 #else /* No retpoline for C / inline asm */
268 # define CALL_NOSPEC "call *%[thunk_target]\n"
269 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
272 /* The Spectre V2 mitigation variants */
273 enum spectre_v2_mitigation {
275 SPECTRE_V2_RETPOLINE,
278 SPECTRE_V2_EIBRS_RETPOLINE,
279 SPECTRE_V2_EIBRS_LFENCE,
283 /* The indirect branch speculation control variants */
284 enum spectre_v2_user_mitigation {
285 SPECTRE_V2_USER_NONE,
286 SPECTRE_V2_USER_STRICT,
287 SPECTRE_V2_USER_STRICT_PREFERRED,
288 SPECTRE_V2_USER_PRCTL,
289 SPECTRE_V2_USER_SECCOMP,
292 /* The Speculative Store Bypass disable variants */
293 enum ssb_mitigation {
294 SPEC_STORE_BYPASS_NONE,
295 SPEC_STORE_BYPASS_DISABLE,
296 SPEC_STORE_BYPASS_PRCTL,
297 SPEC_STORE_BYPASS_SECCOMP,
300 extern char __indirect_thunk_start[];
301 extern char __indirect_thunk_end[];
303 static __always_inline
304 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
306 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
309 "d" ((u32)(val >> 32)),
310 [feature] "i" (feature)
314 static inline void indirect_branch_prediction_barrier(void)
316 u64 val = PRED_CMD_IBPB;
318 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
321 /* The Intel SPEC CTRL MSR base value cache */
322 extern u64 x86_spec_ctrl_base;
323 DECLARE_PER_CPU(u64, x86_spec_ctrl_current);
324 extern void write_spec_ctrl_current(u64 val, bool force);
325 extern u64 spec_ctrl_current(void);
328 * With retpoline, we must use IBRS to restrict branch prediction
329 * before calling into firmware.
331 * (Implemented as CPP macros due to header hell.)
333 #define firmware_restrict_branch_speculation_start() \
336 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
337 spec_ctrl_current() | SPEC_CTRL_IBRS, \
338 X86_FEATURE_USE_IBRS_FW); \
339 alternative_msr_write(MSR_IA32_PRED_CMD, PRED_CMD_IBPB, \
340 X86_FEATURE_USE_IBPB_FW); \
343 #define firmware_restrict_branch_speculation_end() \
345 alternative_msr_write(MSR_IA32_SPEC_CTRL, \
346 spec_ctrl_current(), \
347 X86_FEATURE_USE_IBRS_FW); \
351 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
352 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
353 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
355 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
356 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
358 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
360 DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
362 #include <asm/segment.h>
365 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
367 * This uses the otherwise unused and obsolete VERW instruction in
368 * combination with microcode which triggers a CPU buffer flush when the
369 * instruction is executed.
371 static __always_inline void mds_clear_cpu_buffers(void)
373 static const u16 ds = __KERNEL_DS;
376 * Has to be the memory-operand variant because only that
377 * guarantees the CPU buffer flush functionality according to
378 * documentation. The register-operand variant does not.
379 * Works with any segment selector, but a valid writable
380 * data segment is the fastest variant.
382 * "cc" clobber is required because VERW modifies ZF.
384 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
388 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
390 * Clear CPU buffers if the corresponding static key is enabled
392 static __always_inline void mds_user_clear_cpu_buffers(void)
394 if (static_branch_likely(&mds_user_clear))
395 mds_clear_cpu_buffers();
399 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
401 * Clear CPU buffers if the corresponding static key is enabled
403 static inline void mds_idle_clear_cpu_buffers(void)
405 if (static_branch_likely(&mds_idle_clear))
406 mds_clear_cpu_buffers();
409 #endif /* __ASSEMBLY__ */
411 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */