5763548fb30b9f2f8cf76607a9d97e699ca2f488
[pandora-kernel.git] / arch / x86 / include / asm / nospec-branch.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef __NOSPEC_BRANCH_H__
4 #define __NOSPEC_BRANCH_H__
5
6 #include <asm/alternative.h>
7 #include <asm/alternative-asm.h>
8 #include <asm/cpufeature.h>
9
10 #ifdef __ASSEMBLY__
11
12 /*
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.
16  */
17 .macro RETPOLINE_JMP reg:req
18         call    .Ldo_rop_\@
19 .Lspec_trap_\@:
20         pause
21         jmp     .Lspec_trap_\@
22 .Ldo_rop_\@:
23         mov     \reg, (%_ASM_SP)
24         ret
25 .endm
26
27 /*
28  * This is a wrapper around RETPOLINE_JMP so the called function in reg
29  * returns to the instruction after the macro.
30  */
31 .macro RETPOLINE_CALL reg:req
32         jmp     .Ldo_call_\@
33 .Ldo_retpoline_jmp_\@:
34         RETPOLINE_JMP \reg
35 .Ldo_call_\@:
36         call    .Ldo_retpoline_jmp_\@
37 .endm
38
39 /*
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
42  * attack.
43  */
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
49 #else
50         jmp     *\reg
51 #endif
52 .endm
53
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
59 #else
60         call    *\reg
61 #endif
62 .endm
63
64 #else /* __ASSEMBLY__ */
65
66 #if defined(CONFIG_X86_64) && defined(RETPOLINE)
67
68 /*
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.
71  */
72 # define CALL_NOSPEC                                            \
73         ALTERNATIVE(                                            \
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)
78
79 #elif defined(CONFIG_X86_32) && defined(CONFIG_RETPOLINE)
80 /*
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
83  * here, anyway.
84  */
85 # define CALL_NOSPEC ALTERNATIVE("call *%[thunk_target]\n",     \
86         "       jmp    904f;\n"                                 \
87         "       .align 16\n"                                    \
88         "901:   call   903f;\n"                                 \
89         "902:   pause;\n"                                       \
90         "       jmp    902b;\n"                                 \
91         "       .align 16\n"                                    \
92         "903:   addl   $4, %%esp;\n"                            \
93         "       pushl  %[thunk_target];\n"                      \
94         "       ret;\n"                                         \
95         "       .align 16\n"                                    \
96         "904:   call   901b;\n",                                \
97         X86_FEATURE_RETPOLINE)
98
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)
103 #endif
104
105 #endif /* __ASSEMBLY__ */
106 #endif /* __NOSPEC_BRANCH_H__ */