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 * Fill the CPU return stack buffer.
16 * Each entry in the RSB, if used for a speculative 'ret', contains an
17 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
19 * This is required in various cases for retpoline and IBRS-based
20 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
21 * eliminate potentially bogus entries from the RSB, and sometimes
22 * purely to ensure that it doesn't get empty, which on some CPUs would
23 * allow predictions from other (unwanted!) sources to be used.
25 * We define a CPP macro such that it can be used from both .S files and
26 * inline assembly. It's possible to do a .macro and then include that
27 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
30 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
31 #define RSB_FILL_LOOPS 16 /* To avoid underflow */
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) \
42 773: /* speculation trap */ \
48 775: /* speculation trap */ \
55 add $(BITS_PER_LONG/8) * nr, sp;
60 * This should be used immediately before a retpoline alternative. It tells
61 * objtool where the retpolines are so that it can make sense of the control
62 * flow by just reading the original instruction(s) and ignoring the
65 .macro ANNOTATE_NOSPEC_ALTERNATIVE
67 .pushsection
.discard
.nospec
68 .long .Lannotate_\@
- .
73 * This should be used immediately before an indirect jump/call. It tells
74 * objtool the subsequent indirect jump/call is vouched safe for retpoline
77 .macro ANNOTATE_RETPOLINE_SAFE
79 .pushsection
.discard
.retpoline_safe
80 _ASM_PTR
.Lannotate_\@
85 * These are the bare retpoline primitives for indirect jmp and call.
86 * Do not use these directly; they only exist to make the ALTERNATIVE
87 * invocation below less ugly.
89 .macro RETPOLINE_JMP reg
:req
101 * This is a wrapper around RETPOLINE_JMP so the called function in reg
102 * returns to the instruction after the macro.
104 .macro RETPOLINE_CALL reg
:req
106 .Ldo_retpoline_jmp_\@
:
109 call
.Ldo_retpoline_jmp_\@
113 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
114 * indirect jmp/call which may be susceptible to the Spectre variant 2
117 .macro JMP_NOSPEC reg
:req
118 #ifdef CONFIG_RETPOLINE
119 ANNOTATE_NOSPEC_ALTERNATIVE
120 ALTERNATIVE_2
__stringify(ANNOTATE_RETPOLINE_SAFE
; jmp
*\reg
), \
121 __stringify(RETPOLINE_JMP
\reg
), X86_FEATURE_RETPOLINE
, \
122 __stringify(lfence
; ANNOTATE_RETPOLINE_SAFE
; jmp
*\reg
), X86_FEATURE_RETPOLINE_AMD
128 .macro CALL_NOSPEC reg
:req
129 #ifdef CONFIG_RETPOLINE
130 ANNOTATE_NOSPEC_ALTERNATIVE
131 ALTERNATIVE_2
__stringify(ANNOTATE_RETPOLINE_SAFE
; call
*\reg
), \
132 __stringify(RETPOLINE_CALL
\reg
), X86_FEATURE_RETPOLINE
,\
133 __stringify(lfence
; ANNOTATE_RETPOLINE_SAFE
; call
*\reg
), X86_FEATURE_RETPOLINE_AMD
140 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
141 * monstrosity above, manually.
143 .macro FILL_RETURN_BUFFER reg
:req nr
:req ftr
:req
144 #ifdef CONFIG_RETPOLINE
145 ANNOTATE_NOSPEC_ALTERNATIVE
146 ALTERNATIVE
"jmp .Lskip_rsb_\@", \
147 __stringify(__FILL_RETURN_BUFFER(\reg
,\nr
,%_ASM_SP
)) \
153 #else /* __ASSEMBLY__ */
155 #define ANNOTATE_NOSPEC_ALTERNATIVE \
157 ".pushsection .discard.nospec\n\t" \
158 ".long 999b - .\n\t" \
161 #define ANNOTATE_RETPOLINE_SAFE \
163 ".pushsection .discard.retpoline_safe\n\t" \
164 _ASM_PTR " 999b\n\t" \
167 #ifdef CONFIG_RETPOLINE
171 * Inline asm uses the %V modifier which is only in newer GCC
172 * which is ensured when CONFIG_RETPOLINE is defined.
174 # define CALL_NOSPEC \
175 ANNOTATE_NOSPEC_ALTERNATIVE \
177 ANNOTATE_RETPOLINE_SAFE \
178 "call *%[thunk_target]\n", \
179 "call __x86_indirect_thunk_%V[thunk_target]\n", \
180 X86_FEATURE_RETPOLINE, \
182 ANNOTATE_RETPOLINE_SAFE \
183 "call *%[thunk_target]\n", \
184 X86_FEATURE_RETPOLINE_AMD)
185 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
187 #else /* CONFIG_X86_32 */
189 * For i386 we use the original ret-equivalent retpoline, because
190 * otherwise we'll run out of registers. We don't care about CET
193 # define CALL_NOSPEC \
194 ANNOTATE_NOSPEC_ALTERNATIVE \
196 ANNOTATE_RETPOLINE_SAFE \
197 "call *%[thunk_target]\n", \
200 "901: call 903f;\n" \
205 "903: addl $4, %%esp;\n" \
206 " pushl %[thunk_target];\n" \
209 "904: call 901b;\n", \
210 X86_FEATURE_RETPOLINE, \
212 ANNOTATE_RETPOLINE_SAFE \
213 "call *%[thunk_target]\n", \
214 X86_FEATURE_RETPOLINE_AMD)
216 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
218 #else /* No retpoline for C / inline asm */
219 # define CALL_NOSPEC "call *%[thunk_target]\n"
220 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
223 /* The Spectre V2 mitigation variants */
224 enum spectre_v2_mitigation
{
226 SPECTRE_V2_RETPOLINE_GENERIC
,
227 SPECTRE_V2_RETPOLINE_AMD
,
228 SPECTRE_V2_IBRS_ENHANCED
,
231 /* The indirect branch speculation control variants */
232 enum spectre_v2_user_mitigation
{
233 SPECTRE_V2_USER_NONE
,
234 SPECTRE_V2_USER_STRICT
,
235 SPECTRE_V2_USER_PRCTL
,
236 SPECTRE_V2_USER_SECCOMP
,
239 /* The Speculative Store Bypass disable variants */
240 enum ssb_mitigation
{
241 SPEC_STORE_BYPASS_NONE
,
242 SPEC_STORE_BYPASS_DISABLE
,
243 SPEC_STORE_BYPASS_PRCTL
,
244 SPEC_STORE_BYPASS_SECCOMP
,
247 extern char __indirect_thunk_start
[];
248 extern char __indirect_thunk_end
[];
251 * On VMEXIT we must ensure that no RSB predictions learned in the guest
252 * can be followed in the host, by overwriting the RSB completely. Both
253 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
254 * CPUs with IBRS_ALL *might* it be avoided.
256 static inline void vmexit_fill_RSB(void)
258 #ifdef CONFIG_RETPOLINE
261 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
262 ALTERNATIVE("jmp 910f",
263 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS
, %1)),
264 X86_FEATURE_RETPOLINE
)
266 : "=r" (loops
), ASM_CALL_CONSTRAINT
271 static __always_inline
272 void alternative_msr_write(unsigned int msr
, u64 val
, unsigned int feature
)
274 asm volatile(ALTERNATIVE("", "wrmsr", %c
[feature
])
277 "d" ((u32
)(val
>> 32)),
278 [feature
] "i" (feature
)
282 static inline void indirect_branch_prediction_barrier(void)
284 u64 val
= PRED_CMD_IBPB
;
286 alternative_msr_write(MSR_IA32_PRED_CMD
, val
, X86_FEATURE_USE_IBPB
);
289 /* The Intel SPEC CTRL MSR base value cache */
290 extern u64 x86_spec_ctrl_base
;
293 * With retpoline, we must use IBRS to restrict branch prediction
294 * before calling into firmware.
296 * (Implemented as CPP macros due to header hell.)
298 #define firmware_restrict_branch_speculation_start() \
300 u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \
303 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
304 X86_FEATURE_USE_IBRS_FW); \
307 #define firmware_restrict_branch_speculation_end() \
309 u64 val = x86_spec_ctrl_base; \
311 alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \
312 X86_FEATURE_USE_IBRS_FW); \
316 DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp
);
317 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb
);
318 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb
);
320 DECLARE_STATIC_KEY_FALSE(mds_user_clear
);
321 DECLARE_STATIC_KEY_FALSE(mds_idle_clear
);
323 #include <asm/segment.h>
326 * mds_clear_cpu_buffers - Mitigation for MDS vulnerability
328 * This uses the otherwise unused and obsolete VERW instruction in
329 * combination with microcode which triggers a CPU buffer flush when the
330 * instruction is executed.
332 static inline void mds_clear_cpu_buffers(void)
334 static const u16 ds
= __KERNEL_DS
;
337 * Has to be the memory-operand variant because only that
338 * guarantees the CPU buffer flush functionality according to
339 * documentation. The register-operand variant does not.
340 * Works with any segment selector, but a valid writable
341 * data segment is the fastest variant.
343 * "cc" clobber is required because VERW modifies ZF.
345 asm volatile("verw %[ds]" : : [ds
] "m" (ds
) : "cc");
349 * mds_user_clear_cpu_buffers - Mitigation for MDS vulnerability
351 * Clear CPU buffers if the corresponding static key is enabled
353 static inline void mds_user_clear_cpu_buffers(void)
355 if (static_branch_likely(&mds_user_clear
))
356 mds_clear_cpu_buffers();
360 * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability
362 * Clear CPU buffers if the corresponding static key is enabled
364 static inline void mds_idle_clear_cpu_buffers(void)
366 if (static_branch_likely(&mds_idle_clear
))
367 mds_clear_cpu_buffers();
370 #endif /* __ASSEMBLY__ */
373 * Below is used in the eBPF JIT compiler and emits the byte sequence
374 * for the following assembly:
376 * With retpolines configured:
384 * mov %rax,(%rsp) for x86_64
385 * mov %edx,(%esp) for x86_32
388 * Without retpolines configured:
390 * jmp *%rax for x86_64
391 * jmp *%edx for x86_32
393 #ifdef CONFIG_RETPOLINE
394 # ifdef CONFIG_X86_64
395 # define RETPOLINE_RAX_BPF_JIT_SIZE 17
396 # define RETPOLINE_RAX_BPF_JIT() \
398 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
400 EMIT2(0xF3, 0x90); /* pause */ \
401 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
402 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
404 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
405 EMIT1(0xC3); /* retq */ \
407 # else /* !CONFIG_X86_64 */
408 # define RETPOLINE_EDX_BPF_JIT() \
410 EMIT1_off32(0xE8, 7); /* call do_rop */ \
412 EMIT2(0xF3, 0x90); /* pause */ \
413 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
414 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
416 EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \
417 EMIT1(0xC3); /* ret */ \
420 #else /* !CONFIG_RETPOLINE */
421 # ifdef CONFIG_X86_64
422 # define RETPOLINE_RAX_BPF_JIT_SIZE 2
423 # define RETPOLINE_RAX_BPF_JIT() \
424 EMIT2(0xFF, 0xE0); /* jmp *%rax */
425 # else /* !CONFIG_X86_64 */
426 # define RETPOLINE_EDX_BPF_JIT() \
427 EMIT2(0xFF, 0xE2) /* jmp *%edx */
431 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */