1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef __NOSPEC_BRANCH_H__
4 #define __NOSPEC_BRANCH_H__
6 #include <asm/alternative.h>
7 #include <asm/alternative-asm.h>
8 #include <asm/cpufeature.h>
13 * These are the bare retpoline primitives for indirect jmp and call.
14 * Do not use these directly; they only exist to make the ALTERNATIVE
15 * invocation below less ugly.
17 .macro RETPOLINE_JMP reg:req
28 * This is a wrapper around RETPOLINE_JMP so the called function in reg
29 * returns to the instruction after the macro.
31 .macro RETPOLINE_CALL reg:req
33 .Ldo_retpoline_jmp_\@:
36 call .Ldo_retpoline_jmp_\@
40 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
41 * indirect jmp/call which may be susceptible to the Spectre variant 2
44 .macro JMP_NOSPEC reg:req
45 #ifdef CONFIG_RETPOLINE
46 ALTERNATIVE_2 __stringify(jmp *\reg), \
47 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
48 __stringify(lfence; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
54 .macro CALL_NOSPEC reg:req
55 #ifdef CONFIG_RETPOLINE
56 ALTERNATIVE_2 __stringify(call *\reg), \
57 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
58 __stringify(lfence; call *\reg), X86_FEATURE_RETPOLINE_AMD
64 #else /* __ASSEMBLY__ */
66 #if defined(CONFIG_X86_64) && defined(RETPOLINE)
69 * Since the inline asm uses the %V modifier which is only in newer GCC,
70 * the 64-bit one is dependent on RETPOLINE not CONFIG_RETPOLINE.
72 # define CALL_NOSPEC \
74 "call *%[thunk_target]\n", \
75 "call __x86_indirect_thunk_%V[thunk_target]\n", \
76 X86_FEATURE_RETPOLINE)
77 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
79 #elif defined(CONFIG_X86_32) && defined(CONFIG_RETPOLINE)
81 * For i386 we use the original ret-equivalent retpoline, because
82 * otherwise we'll run out of registers. We don't care about CET
85 # define CALL_NOSPEC ALTERNATIVE("call *%[thunk_target]\n", \
92 "903: addl $4, %%esp;\n" \
93 " pushl %[thunk_target];\n" \
96 "904: call 901b;\n", \
97 X86_FEATURE_RETPOLINE)
99 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
100 #else /* No retpoline */
101 # define CALL_NOSPEC "call *%[thunk_target]\n"
102 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
105 #endif /* __ASSEMBLY__ */
106 #endif /* __NOSPEC_BRANCH_H__ */