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>
9 #include <asm/alternative.h>
10 #include <asm/cpufeatures.h>
11 #include <asm/msr-index.h>
12 #include <asm/unwind_hints.h>
15 * Fill the CPU return stack buffer.
17 * Each entry in the RSB, if used for a speculative 'ret', contains an
18 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
20 * This is required in various cases for retpoline and IBRS-based
21 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
22 * eliminate potentially bogus entries from the RSB, and sometimes
23 * purely to ensure that it doesn't get empty, which on some CPUs would
24 * allow predictions from other (unwanted!) sources to be used.
26 * We define a CPP macro such that it can be used from both .S files and
27 * inline assembly. It's possible to do a .macro and then include that
28 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
31 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
34 * Google experimented with loop-unrolling and this turned out to be
35 * the optimal version - two calls, each with their own speculation
36 * trap should their return address end up getting used, in a loop.
38 #define __FILL_RETURN_BUFFER(reg, nr, sp) \
41 ANNOTATE_INTRA_FUNCTION_CALL; \
43 773: /* speculation trap */ \
49 ANNOTATE_INTRA_FUNCTION_CALL; \
51 775: /* speculation trap */ \
57 add $(BITS_PER_LONG/8) * 2, sp; \
64 * This should be used immediately before an indirect jump/call. It tells
65 * objtool the subsequent indirect jump/call is vouched safe for retpoline
68 .macro ANNOTATE_RETPOLINE_SAFE
70 .pushsection .discard.retpoline_safe
71 _ASM_PTR .Lannotate_\@
76 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
77 * indirect jmp/call which may be susceptible to the Spectre variant 2
80 .macro JMP_NOSPEC reg:req
81 #ifdef CONFIG_RETPOLINE
82 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), \
83 __stringify(jmp __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
84 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), X86_FEATURE_RETPOLINE_AMD
90 .macro CALL_NOSPEC reg:req
91 #ifdef CONFIG_RETPOLINE
92 ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *%\reg), \
93 __stringify(call __x86_indirect_thunk_\reg), X86_FEATURE_RETPOLINE, \
94 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *%\reg), X86_FEATURE_RETPOLINE_AMD
101 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
102 * monstrosity above, manually.
104 .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req
105 #ifdef CONFIG_RETPOLINE
106 ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr
107 __FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)
112 #else /* __ASSEMBLY__ */
114 #define ANNOTATE_RETPOLINE_SAFE \
116 ".pushsection .discard.retpoline_safe\n\t" \
117 _ASM_PTR " 999b\n\t" \
120 #ifdef CONFIG_RETPOLINE
124 * Inline asm uses the %V modifier which is only in newer GCC
125 * which is ensured when CONFIG_RETPOLINE is defined.
127 # define CALL_NOSPEC \
129 ANNOTATE_RETPOLINE_SAFE \
130 "call *%[thunk_target]\n", \
131 "call __x86_indirect_thunk_%V[thunk_target]\n", \
132 X86_FEATURE_RETPOLINE, \
134 ANNOTATE_RETPOLINE_SAFE \
135 "call *%[thunk_target]\n", \
136 X86_FEATURE_RETPOLINE_AMD)
138 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
140 #else /* CONFIG_X86_32 */
142 * For i386 we use the original ret-equivalent retpoline, because
143 * otherwise we'll run out of registers. We don't care about CET
146 # define CALL_NOSPEC \
148 ANNOTATE_RETPOLINE_SAFE \
149 "call *%[thunk_target]\n", \
152 "901: call 903f;\n" \
157 "903: lea 4(%%esp), %%esp;\n" \
158 " pushl %[thunk_target];\n" \
161 "904: call 901b;\n", \
162 X86_FEATURE_RETPOLINE, \
164 ANNOTATE_RETPOLINE_SAFE \
165 "call *%[thunk_target]\n", \
166 X86_FEATURE_RETPOLINE_AMD)
168 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
170 #else /* No retpoline for C / inline asm */
171 # define CALL_NOSPEC "call *%[thunk_target]\n"
172 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
175 /* The Spectre V2 mitigation variants */
176 enum spectre_v2_mitigation {
178 SPECTRE_V2_RETPOLINE_GENERIC,
179 SPECTRE_V2_RETPOLINE_AMD,
180 SPECTRE_V2_IBRS_ENHANCED,
183 /* The indirect branch speculation control variants */
184 enum spectre_v2_user_mitigation {
185 SPECTRE_V2_USER_NONE,
186 SPECTRE_V2_USER_STRICT,
187 SPECTRE_V2_USER_STRICT_PREFERRED,
188 SPECTRE_V2_USER_PRCTL,
189 SPECTRE_V2_USER_SECCOMP,
192 /* The Speculative Store Bypass disable variants */
193 enum ssb_mitigation {
194 SPEC_STORE_BYPASS_NONE,
195 SPEC_STORE_BYPASS_DISABLE,
196 SPEC_STORE_BYPASS_PRCTL,
197 SPEC_STORE_BYPASS_SECCOMP,
200 extern char __indirect_thunk_start[];
201 extern char __indirect_thunk_end[];
203 static __always_inline
204 void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
206 asm volatile(ALTERNATIVE("", "wrmsr", %c[feature])
209 "d" ((u32)(val >> 32)),
210 [feature] "i" (feature)
214 static inline void indirect_branch_prediction_barrier(void)
216 u64 val = PRED_CMD_IBPB;
218 alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
221 /* The Intel SPEC CTRL MSR base value cache */
222 extern u64 x86_spec_ctrl_base;
225 * With retpoline, we must use IBRS to restrict branch prediction
226 * before calling into firmware.
228 * (Implemented as CPP macros due to header hell.)
230 #define firmware_restrict_branch_speculation_start() \
232 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
235 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
236 X86_FEATURE_USE_IBRS_FW); \
239 #define firmware_restrict_branch_speculation_end() \
241 u64 val = x86_spec_ctrl_base; \
243 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
244 X86_FEATURE_USE_IBRS_FW); \
248 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
249 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
250 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
252 DECLARE_STATIC_KEY_FALSE(mds_user_clear);
253 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
255 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
257 #include <asm/segment.h>
260 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
262 * This uses the otherwise unused and obsolete VERW instruction in
263 * combination with microcode which triggers a CPU buffer flush when the
264 * instruction is executed.
266 static __always_inline void mds_clear_cpu_buffers(void)
268 static const u16 ds = __KERNEL_DS;
271 * Has to be the memory-operand variant because only that
272 * guarantees the CPU buffer flush functionality according to
273 * documentation. The register-operand variant does not.
274 * Works with any segment selector, but a valid writable
275 * data segment is the fastest variant.
277 * "cc" clobber is required because VERW modifies ZF.
279 asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc");
283 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
285 * Clear CPU buffers if the corresponding static key is enabled
287 static __always_inline void mds_user_clear_cpu_buffers(void)
289 if (static_branch_likely(&mds_user_clear))
290 mds_clear_cpu_buffers();
294 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
296 * Clear CPU buffers if the corresponding static key is enabled
298 static inline void mds_idle_clear_cpu_buffers(void)
300 if (static_branch_likely(&mds_idle_clear))
301 mds_clear_cpu_buffers();
304 #endif /* __ASSEMBLY__ */
307 * Below is used in the eBPF JIT compiler and emits the byte sequence
308 * for the following assembly:
310 * With retpolines configured:
318 * mov %rcx,(%rsp) for x86_64
319 * mov %edx,(%esp) for x86_32
322 * Without retpolines configured:
324 * jmp *%rcx for x86_64
325 * jmp *%edx for x86_32
327 #ifdef CONFIG_RETPOLINE
328 # ifdef CONFIG_X86_64
329 # define RETPOLINE_RCX_BPF_JIT_SIZE 17
330 # define RETPOLINE_RCX_BPF_JIT() \
332 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
334 EMIT2(0xF3, 0x90); /* pause */ \
335 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
336 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
338 EMIT4(0x48, 0x89, 0x0C, 0x24); /* mov %rcx,(%rsp) */ \
339 EMIT1(0xC3); /* retq */ \
341 # else /* !CONFIG_X86_64 */
342 # define RETPOLINE_EDX_BPF_JIT() \
344 EMIT1_off32(0xE8, 7); /* call do_rop */ \
346 EMIT2(0xF3, 0x90); /* pause */ \
347 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
348 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
350 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
351 EMIT1(0xC3); /* ret */ \
354 #else /* !CONFIG_RETPOLINE */
355 # ifdef CONFIG_X86_64
356 # define RETPOLINE_RCX_BPF_JIT_SIZE 2
357 # define RETPOLINE_RCX_BPF_JIT() \
358 EMIT2(0xFF, 0xE1); /* jmp *%rcx */
359 # else /* !CONFIG_X86_64 */
360 # define RETPOLINE_EDX_BPF_JIT() \
361 EMIT2(0xFF, 0xE2) /* jmp *%edx */
365 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */