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/cpufeatures.h>
13 * This should be used immediately before a retpoline alternative. It tells
14 * objtool where the retpolines are so that it can make sense of the control
15 * flow by just reading the original instruction(s) and ignoring the
18 .macro ANNOTATE_NOSPEC_ALTERNATIVE
20 .pushsection .discard.nospec
21 .long .Lannotate_\@ - .
26 * These are the bare retpoline primitives for indirect jmp and call.
27 * Do not use these directly; they only exist to make the ALTERNATIVE
28 * invocation below less ugly.
30 .macro RETPOLINE_JMP reg:req
41 * This is a wrapper around RETPOLINE_JMP so the called function in reg
42 * returns to the instruction after the macro.
44 .macro RETPOLINE_CALL reg:req
46 .Ldo_retpoline_jmp_\@:
49 call .Ldo_retpoline_jmp_\@
53 * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple
54 * indirect jmp/call which may be susceptible to the Spectre variant 2
57 .macro JMP_NOSPEC reg:req
58 #ifdef CONFIG_RETPOLINE
59 ANNOTATE_NOSPEC_ALTERNATIVE
60 ALTERNATIVE_2 __stringify(jmp *\reg), \
61 __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \
62 __stringify(lfence; jmp *\reg), X86_FEATURE_RETPOLINE_AMD
68 .macro CALL_NOSPEC reg:req
69 #ifdef CONFIG_RETPOLINE
70 ANNOTATE_NOSPEC_ALTERNATIVE
71 ALTERNATIVE_2 __stringify(call *\reg), \
72 __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\
73 __stringify(lfence; call *\reg), X86_FEATURE_RETPOLINE_AMD
79 #else /* __ASSEMBLY__ */
81 #define ANNOTATE_NOSPEC_ALTERNATIVE \
83 ".pushsection .discard.nospec\n\t" \
84 ".long 999b - .\n\t" \
87 #if defined(CONFIG_X86_64) && defined(RETPOLINE)
90 * Since the inline asm uses the %V modifier which is only in newer GCC,
91 * the 64-bit one is dependent on RETPOLINE not CONFIG_RETPOLINE.
93 # define CALL_NOSPEC \
94 ANNOTATE_NOSPEC_ALTERNATIVE \
96 "call *%[thunk_target]\n", \
97 "call __x86_indirect_thunk_%V[thunk_target]\n", \
98 X86_FEATURE_RETPOLINE)
99 # define THUNK_TARGET(addr) [thunk_target] "r" (addr)
101 #elif defined(CONFIG_X86_32) && defined(CONFIG_RETPOLINE)
103 * For i386 we use the original ret-equivalent retpoline, because
104 * otherwise we'll run out of registers. We don't care about CET
107 # define CALL_NOSPEC ALTERNATIVE("call *%[thunk_target]\n", \
110 "901: call 903f;\n" \
114 "903: addl $4, %%esp;\n" \
115 " pushl %[thunk_target];\n" \
118 "904: call 901b;\n", \
119 X86_FEATURE_RETPOLINE)
121 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
122 #else /* No retpoline */
123 # define CALL_NOSPEC "call *%[thunk_target]\n"
124 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
127 #endif /* __ASSEMBLY__ */
128 #endif /* __NOSPEC_BRANCH_H__ */