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>
8 #include <asm/alternative.h>
9 #include <asm/alternative-asm.h>
10 #include <asm/cpufeatures.h>
11 #include <asm/msr-index.h>
14 * This should be used immediately before a retpoline alternative. It tells
15 * objtool where the retpolines are so that it can make sense of the control
16 * flow by just reading the original instruction(s) and ignoring the
19 #define ANNOTATE_NOSPEC_ALTERNATIVE \
20 ANNOTATE_IGNORE_ALTERNATIVE
23 * Fill the CPU return stack buffer.
25 * Each entry in the RSB, if used for a speculative 'ret', contains an
26 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
28 * This is required in various cases for retpoline and IBRS-based
29 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
30 * eliminate potentially bogus entries from the RSB, and sometimes
31 * purely to ensure that it doesn't get empty, which on some CPUs would
32 * allow predictions from other (unwanted!) sources to be used.
34 * We define a CPP macro such that it can be used from both .S files and
35 * inline assembly. It's possible to do a .macro and then include that
36 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
39 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
42 * Google experimented with loop-unrolling and this turned out to be
43 * the optimal version — two calls, each with their own speculation
44 * trap should their return address end up getting used, in a loop.
46 #define __FILL_RETURN_BUFFER(reg, nr, sp) \
50 773: /* speculation trap */ \
56 775: /* speculation trap */ \
63 add $(BITS_PER_LONG/8) * nr, sp;
68 * This should be used immediately before an indirect jump/call. It tells
69 * objtool the subsequent indirect jump/call is vouched safe for retpoline
72 .macro ANNOTATE_RETPOLINE_SAFE
74 .pushsection
.discard
.retpoline_safe
75 _ASM_PTR
.Lannotate_\@
80 * These are the bare retpoline primitives for indirect jmp and call.
81 * Do not use these directly; they only exist to make the ALTERNATIVE
82 * invocation below less ugly.
84 .macro RETPOLINE_JMP reg
:req
96 * This is a wrapper around RETPOLINE_JMP so the called function in reg
97 * returns to the instruction after the macro.
99 .macro RETPOLINE_CALL reg
:req
101 .Ldo_retpoline_jmp_\@
:
104 call
.Ldo_retpoline_jmp_\@
108 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
109 * indirect jmp/call which may be susceptible to the Spectre variant 2
112 .macro JMP_NOSPEC reg
:req
113 #ifdef CONFIG_RETPOLINE
114 ANNOTATE_NOSPEC_ALTERNATIVE
115 ALTERNATIVE_2
__stringify(ANNOTATE_RETPOLINE_SAFE
; jmp
*\reg
), \
116 __stringify(RETPOLINE_JMP
\reg
), X86_FEATURE_RETPOLINE
, \
117 __stringify(lfence
; ANNOTATE_RETPOLINE_SAFE
; jmp
*\reg
), X86_FEATURE_RETPOLINE_AMD
123 .macro CALL_NOSPEC reg
:req
124 #ifdef CONFIG_RETPOLINE
125 ANNOTATE_NOSPEC_ALTERNATIVE
126 ALTERNATIVE_2
__stringify(ANNOTATE_RETPOLINE_SAFE
; call
*\reg
), \
127 __stringify(RETPOLINE_CALL
\reg
), X86_FEATURE_RETPOLINE
,\
128 __stringify(lfence
; ANNOTATE_RETPOLINE_SAFE
; call
*\reg
), X86_FEATURE_RETPOLINE_AMD
135 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
136 * monstrosity above, manually.
138 .macro FILL_RETURN_BUFFER reg
:req nr
:req ftr
:req
139 #ifdef CONFIG_RETPOLINE
140 ANNOTATE_NOSPEC_ALTERNATIVE
141 ALTERNATIVE
"jmp .Lskip_rsb_\@", \
142 __stringify(__FILL_RETURN_BUFFER(\reg
,\nr
,%_ASM_SP
)) \
148 #else /* __ASSEMBLY__ */
150 #define ANNOTATE_RETPOLINE_SAFE \
152 ".pushsection .discard.retpoline_safe\n\t" \
153 _ASM_PTR " 999b\n\t" \
156 #ifdef CONFIG_RETPOLINE
160 * Inline asm uses the %V modifier which is only in newer GCC
161 * which is ensured when CONFIG_RETPOLINE is defined.
163 # define CALL_NOSPEC \
164 ANNOTATE_NOSPEC_ALTERNATIVE \
166 ANNOTATE_RETPOLINE_SAFE \
167 "call *%[thunk_target]\n", \
168 "call __x86_indirect_thunk_%V[thunk_target]\n", \
169 X86_FEATURE_RETPOLINE, \
171 ANNOTATE_RETPOLINE_SAFE \
172 "call *%[thunk_target]\n", \
173 X86_FEATURE_RETPOLINE_AMD)
174 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
176 #else /* CONFIG_X86_32 */
178 * For i386 we use the original ret-equivalent retpoline, because
179 * otherwise we'll run out of registers. We don't care about CET
182 # define CALL_NOSPEC \
183 ANNOTATE_NOSPEC_ALTERNATIVE \
185 ANNOTATE_RETPOLINE_SAFE \
186 "call *%[thunk_target]\n", \
189 "901: call 903f;\n" \
194 "903: lea 4(%%esp), %%esp;\n" \
195 " pushl %[thunk_target];\n" \
198 "904: call 901b;\n", \
199 X86_FEATURE_RETPOLINE, \
201 ANNOTATE_RETPOLINE_SAFE \
202 "call *%[thunk_target]\n", \
203 X86_FEATURE_RETPOLINE_AMD)
205 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
207 #else /* No retpoline for C / inline asm */
208 # define CALL_NOSPEC "call *%[thunk_target]\n"
209 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
212 /* The Spectre V2 mitigation variants */
213 enum spectre_v2_mitigation
{
215 SPECTRE_V2_RETPOLINE_GENERIC
,
216 SPECTRE_V2_RETPOLINE_AMD
,
217 SPECTRE_V2_IBRS_ENHANCED
,
220 /* The indirect branch speculation control variants */
221 enum spectre_v2_user_mitigation
{
222 SPECTRE_V2_USER_NONE
,
223 SPECTRE_V2_USER_STRICT
,
224 SPECTRE_V2_USER_STRICT_PREFERRED
,
225 SPECTRE_V2_USER_PRCTL
,
226 SPECTRE_V2_USER_SECCOMP
,
229 /* The Speculative Store Bypass disable variants */
230 enum ssb_mitigation
{
231 SPEC_STORE_BYPASS_NONE
,
232 SPEC_STORE_BYPASS_DISABLE
,
233 SPEC_STORE_BYPASS_PRCTL
,
234 SPEC_STORE_BYPASS_SECCOMP
,
237 extern char __indirect_thunk_start
[];
238 extern char __indirect_thunk_end
[];
241 * On VMEXIT we must ensure that no RSB predictions learned in the guest
242 * can be followed in the host, by overwriting the RSB completely. Both
243 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
244 * CPUs with IBRS_ALL *might* it be avoided.
246 static inline void vmexit_fill_RSB(void)
248 #ifdef CONFIG_RETPOLINE
251 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
252 ALTERNATIVE("jmp 910f",
253 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS
, %1)),
254 X86_FEATURE_RETPOLINE
)
256 : "=r" (loops
), ASM_CALL_CONSTRAINT
261 static __always_inline
262 void alternative_msr_write(unsigned int msr
, u64 val
, unsigned int feature
)
264 asm volatile(ALTERNATIVE("", "wrmsr", %c
[feature
])
267 "d" ((u32
)(val
>> 32)),
268 [feature
] "i" (feature
)
272 static inline void indirect_branch_prediction_barrier(void)
274 u64 val
= PRED_CMD_IBPB
;
276 alternative_msr_write(MSR_IA32_PRED_CMD
, val
, X86_FEATURE_USE_IBPB
);
279 /* The Intel SPEC CTRL MSR base value cache */
280 extern u64 x86_spec_ctrl_base
;
283 * With retpoline, we must use IBRS to restrict branch prediction
284 * before calling into firmware.
286 * (Implemented as CPP macros due to header hell.)
288 #define firmware_restrict_branch_speculation_start() \
290 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
293 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
294 X86_FEATURE_USE_IBRS_FW); \
297 #define firmware_restrict_branch_speculation_end() \
299 u64 val = x86_spec_ctrl_base; \
301 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
302 X86_FEATURE_USE_IBRS_FW); \
306 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp
);
307 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb
);
308 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb
);
310 DECLARE_STATIC_KEY_FALSE(mds_user_clear
);
311 DECLARE_STATIC_KEY_FALSE(mds_idle_clear
);
313 #include <asm/segment.h>
316 * mds_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
318 * This uses the otherwise unused and obsolete VERW instruction in
319 * combination with microcode which triggers a CPU buffer flush when the
320 * instruction is executed.
322 static inline void mds_clear_cpu_buffers(void)
324 static const u16 ds
= __KERNEL_DS
;
327 * Has to be the memory-operand variant because only that
328 * guarantees the CPU buffer flush functionality according to
329 * documentation. The register-operand variant does not.
330 * Works with any segment selector, but a valid writable
331 * data segment is the fastest variant.
333 * "cc" clobber is required because VERW modifies ZF.
335 asm volatile("verw %[ds]" : : [ds
] "m" (ds
) : "cc");
339 * mds_user_clear_cpu_buffers - Mitigation for MDS and TAA vulnerability
341 * Clear CPU buffers if the corresponding static key is enabled
343 static inline void mds_user_clear_cpu_buffers(void)
345 if (static_branch_likely(&mds_user_clear
))
346 mds_clear_cpu_buffers();
350 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
352 * Clear CPU buffers if the corresponding static key is enabled
354 static inline void mds_idle_clear_cpu_buffers(void)
356 if (static_branch_likely(&mds_idle_clear
))
357 mds_clear_cpu_buffers();
360 #endif /* __ASSEMBLY__ */
363 * Below is used in the eBPF JIT compiler and emits the byte sequence
364 * for the following assembly:
366 * With retpolines configured:
374 * mov %rax,(%rsp) for x86_64
375 * mov %edx,(%esp) for x86_32
378 * Without retpolines configured:
380 * jmp *%rax for x86_64
381 * jmp *%edx for x86_32
383 #ifdef CONFIG_RETPOLINE
384 # ifdef CONFIG_X86_64
385 # define RETPOLINE_RAX_BPF_JIT_SIZE 17
386 # define RETPOLINE_RAX_BPF_JIT() \
388 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
390 EMIT2(0xF3, 0x90); /* pause */ \
391 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
392 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
394 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
395 EMIT1(0xC3); /* retq */ \
397 # else /* !CONFIG_X86_64 */
398 # define RETPOLINE_EDX_BPF_JIT() \
400 EMIT1_off32(0xE8, 7); /* call do_rop */ \
402 EMIT2(0xF3, 0x90); /* pause */ \
403 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
404 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
406 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
407 EMIT1(0xC3); /* ret */ \
410 #else /* !CONFIG_RETPOLINE */
411 # ifdef CONFIG_X86_64
412 # define RETPOLINE_RAX_BPF_JIT_SIZE 2
413 # define RETPOLINE_RAX_BPF_JIT() \
414 EMIT2(0xFF, 0xE0); /* jmp *%rax */
415 # else /* !CONFIG_X86_64 */
416 # define RETPOLINE_EDX_BPF_JIT() \
417 EMIT2(0xFF, 0xE2) /* jmp *%edx */
421 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */