1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _ASM_X86_NOSPEC_BRANCH_H_
4 #define _ASM_X86_NOSPEC_BRANCH_H_
6 #include <asm/alternative.h>
7 #include <asm/alternative-asm.h>
8 #include <asm/cpufeatures.h>
9 #include <asm/msr-index.h>
12 * Fill the CPU return stack buffer.
14 * Each entry in the RSB, if used for a speculative 'ret', contains an
15 * infinite 'pause; lfence; jmp' loop to capture speculative execution.
17 * This is required in various cases for retpoline and IBRS-based
18 * mitigations for the Spectre variant 2 vulnerability. Sometimes to
19 * eliminate potentially bogus entries from the RSB, and sometimes
20 * purely to ensure that it doesn't get empty, which on some CPUs would
21 * allow predictions from other (unwanted!) sources to be used.
23 * We define a CPP macro such that it can be used from both .S files and
24 * inline assembly. It's possible to do a .macro and then include that
25 * from C via asm(".include <asm/nospec-branch.h>") but let's not go there.
28 #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */
29 #define RSB_FILL_LOOPS 16 /* To avoid underflow */
32 * Google experimented with loop-unrolling and this turned out to be
33 * the optimal version — two calls, each with their own speculation
34 * trap should their return address end up getting used, in a loop.
36 #define __FILL_RETURN_BUFFER(reg, nr, sp) \
40 773: /* speculation trap */ \
46 775: /* speculation trap */ \
53 add $(BITS_PER_LONG/8) * nr, sp;
58 * This should be used immediately before a retpoline alternative. It tells
59 * objtool where the retpolines are so that it can make sense of the control
60 * flow by just reading the original instruction(s) and ignoring the
63 .macro ANNOTATE_NOSPEC_ALTERNATIVE
65 .pushsection
.discard
.nospec
66 .long .Lannotate_\@
- .
71 * These are the bare retpoline primitives for indirect jmp and call.
72 * Do not use these directly; they only exist to make the ALTERNATIVE
73 * invocation below less ugly.
75 .macro RETPOLINE_JMP reg
:req
87 * This is a wrapper around RETPOLINE_JMP so the called function in reg
88 * returns to the instruction after the macro.
90 .macro RETPOLINE_CALL reg
:req
92 .Ldo_retpoline_jmp_\@
:
95 call
.Ldo_retpoline_jmp_\@
99 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
100 * indirect jmp/call which may be susceptible to the Spectre variant 2
103 .macro JMP_NOSPEC reg
:req
104 #ifdef CONFIG_RETPOLINE
105 ANNOTATE_NOSPEC_ALTERNATIVE
106 ALTERNATIVE_2
__stringify(jmp
*\reg
), \
107 __stringify(RETPOLINE_JMP
\reg
), X86_FEATURE_RETPOLINE
, \
108 __stringify(lfence
; jmp
*\reg
), X86_FEATURE_RETPOLINE_AMD
114 .macro CALL_NOSPEC reg
:req
115 #ifdef CONFIG_RETPOLINE
116 ANNOTATE_NOSPEC_ALTERNATIVE
117 ALTERNATIVE_2
__stringify(call
*\reg
), \
118 __stringify(RETPOLINE_CALL
\reg
), X86_FEATURE_RETPOLINE
,\
119 __stringify(lfence
; call
*\reg
), X86_FEATURE_RETPOLINE_AMD
126 * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP
127 * monstrosity above, manually.
129 .macro FILL_RETURN_BUFFER reg
:req nr
:req ftr
:req
130 #ifdef CONFIG_RETPOLINE
131 ANNOTATE_NOSPEC_ALTERNATIVE
132 ALTERNATIVE
"jmp .Lskip_rsb_\@", \
133 __stringify(__FILL_RETURN_BUFFER(\reg
,\nr
,%_ASM_SP
)) \
139 #else /* __ASSEMBLY__ */
141 #define ANNOTATE_NOSPEC_ALTERNATIVE \
143 ".pushsection .discard.nospec\n\t" \
144 ".long 999b - .\n\t" \
147 #if defined(CONFIG_X86_64) && defined(RETPOLINE)
150 * Since the inline asm uses the %V modifier which is only in newer GCC,
151 * the 64-bit one is dependent on RETPOLINE not CONFIG_RETPOLINE.
153 # define CALL_NOSPEC \
154 ANNOTATE_NOSPEC_ALTERNATIVE \
156 "call *%[thunk_target]\n", \
157 "call __x86_indirect_thunk_%V[thunk_target]\n", \
158 X86_FEATURE_RETPOLINE)
159 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
161 #elif defined(CONFIG_X86_32) && defined(CONFIG_RETPOLINE)
163 * For i386 we use the original ret-equivalent retpoline, because
164 * otherwise we'll run out of registers. We don't care about CET
167 # define CALL_NOSPEC ALTERNATIVE("call *%[thunk_target]\n", \
170 "901: call 903f;\n" \
175 "903: addl $4, %%esp;\n" \
176 " pushl %[thunk_target];\n" \
179 "904: call 901b;\n", \
180 X86_FEATURE_RETPOLINE)
182 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
183 #else /* No retpoline for C / inline asm */
184 # define CALL_NOSPEC "call *%[thunk_target]\n"
185 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
188 /* The Spectre V2 mitigation variants */
189 enum spectre_v2_mitigation
{
191 SPECTRE_V2_RETPOLINE_MINIMAL
,
192 SPECTRE_V2_RETPOLINE_MINIMAL_AMD
,
193 SPECTRE_V2_RETPOLINE_GENERIC
,
194 SPECTRE_V2_RETPOLINE_AMD
,
198 extern char __indirect_thunk_start
[];
199 extern char __indirect_thunk_end
[];
202 * On VMEXIT we must ensure that no RSB predictions learned in the guest
203 * can be followed in the host, by overwriting the RSB completely. Both
204 * retpoline and IBRS mitigations for Spectre v2 need this; only on future
205 * CPUs with IBRS_ALL *might* it be avoided.
207 static inline void vmexit_fill_RSB(void)
209 #ifdef CONFIG_RETPOLINE
212 asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
213 ALTERNATIVE("jmp 910f",
214 __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS
, %1)),
215 X86_FEATURE_RETPOLINE
)
217 : "=r" (loops
), ASM_CALL_CONSTRAINT
222 #define alternative_msr_write(_msr, _val, _feature) \
223 asm volatile(ALTERNATIVE("", \
224 "movl %[msr], %%ecx\n\t" \
225 "movl %[val], %%eax\n\t" \
226 "movl $0, %%edx\n\t" \
229 : : [msr] "i" (_msr), [val] "i" (_val) \
230 : "eax", "ecx", "edx", "memory")
232 static inline void indirect_branch_prediction_barrier(void)
234 alternative_msr_write(MSR_IA32_PRED_CMD
, PRED_CMD_IBPB
,
235 X86_FEATURE_USE_IBPB
);
239 * With retpoline, we must use IBRS to restrict branch prediction
240 * before calling into firmware.
242 static inline void firmware_restrict_branch_speculation_start(void)
245 alternative_msr_write(MSR_IA32_SPEC_CTRL
, SPEC_CTRL_IBRS
,
246 X86_FEATURE_USE_IBRS_FW
);
249 static inline void firmware_restrict_branch_speculation_end(void)
251 alternative_msr_write(MSR_IA32_SPEC_CTRL
, 0,
252 X86_FEATURE_USE_IBRS_FW
);
256 #endif /* __ASSEMBLY__ */
259 * Below is used in the eBPF JIT compiler and emits the byte sequence
260 * for the following assembly:
262 * With retpolines configured:
273 * Without retpolines configured:
277 #ifdef CONFIG_RETPOLINE
278 # define RETPOLINE_RAX_BPF_JIT_SIZE 17
279 # define RETPOLINE_RAX_BPF_JIT() \
280 EMIT1_off32(0xE8, 7); /* callq do_rop */ \
282 EMIT2(0xF3, 0x90); /* pause */ \
283 EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \
284 EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \
286 EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \
287 EMIT1(0xC3); /* retq */
289 # define RETPOLINE_RAX_BPF_JIT_SIZE 2
290 # define RETPOLINE_RAX_BPF_JIT() \
291 EMIT2(0xFF, 0xE0); /* jmp *%rax */
294 #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */