Mention branches and keyring.
[releases.git] / x86 / kernel / static_call.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/static_call.h>
3 #include <linux/memory.h>
4 #include <linux/bug.h>
5 #include <asm/text-patching.h>
6
7 enum insn_type {
8         CALL = 0, /* site call */
9         NOP = 1,  /* site cond-call */
10         JMP = 2,  /* tramp / site tail-call */
11         RET = 3,  /* tramp / site cond-tail-call */
12         JCC = 4,
13 };
14
15 /*
16  * ud1 %esp, %ecx - a 3 byte #UD that is unique to trampolines, chosen such
17  * that there is no false-positive trampoline identification while also being a
18  * speculation stop.
19  */
20 static const u8 tramp_ud[] = { 0x0f, 0xb9, 0xcc };
21
22 /*
23  * cs cs cs xorl %eax, %eax - a single 5 byte instruction that clears %[er]ax
24  */
25 static const u8 xor5rax[] = { 0x2e, 0x2e, 0x2e, 0x31, 0xc0 };
26
27 static const u8 retinsn[] = { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc };
28
29 static u8 __is_Jcc(u8 *insn) /* Jcc.d32 */
30 {
31         u8 ret = 0;
32
33         if (insn[0] == 0x0f) {
34                 u8 tmp = insn[1];
35                 if ((tmp & 0xf0) == 0x80)
36                         ret = tmp;
37         }
38
39         return ret;
40 }
41
42 extern void __static_call_return(void);
43
44 asm (".global __static_call_return\n\t"
45      ".type __static_call_return, @function\n\t"
46      "__static_call_return:\n\t"
47      ANNOTATE_NOENDBR
48      ANNOTATE_RETPOLINE_SAFE
49      "ret; int3\n\t"
50      ".size __static_call_return, . - __static_call_return \n\t");
51
52 static void __ref __static_call_transform(void *insn, enum insn_type type,
53                                           void *func, bool modinit)
54 {
55         const void *emulate = NULL;
56         int size = CALL_INSN_SIZE;
57         const void *code;
58         u8 op, buf[6];
59
60         if ((type == JMP || type == RET) && (op = __is_Jcc(insn)))
61                 type = JCC;
62
63         switch (type) {
64         case CALL:
65                 code = text_gen_insn(CALL_INSN_OPCODE, insn, func);
66                 if (func == &__static_call_return0) {
67                         emulate = code;
68                         code = &xor5rax;
69                 }
70
71                 break;
72
73         case NOP:
74                 code = x86_nops[5];
75                 break;
76
77         case JMP:
78                 code = text_gen_insn(JMP32_INSN_OPCODE, insn, func);
79                 break;
80
81         case RET:
82                 if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
83                         code = text_gen_insn(JMP32_INSN_OPCODE, insn, x86_return_thunk);
84                 else
85                         code = &retinsn;
86                 break;
87
88         case JCC:
89                 if (!func) {
90                         func = __static_call_return;
91                         if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
92                                 func = __x86_return_thunk;
93                 }
94
95                 buf[0] = 0x0f;
96                 __text_gen_insn(buf+1, op, insn+1, func, 5);
97                 code = buf;
98                 size = 6;
99
100                 break;
101         }
102
103         if (memcmp(insn, code, size) == 0)
104                 return;
105
106         if (system_state == SYSTEM_BOOTING || modinit)
107                 return text_poke_early(insn, code, size);
108
109         text_poke_bp(insn, code, size, emulate);
110 }
111
112 static void __static_call_validate(u8 *insn, bool tail, bool tramp)
113 {
114         u8 opcode = insn[0];
115
116         if (tramp && memcmp(insn+5, tramp_ud, 3)) {
117                 pr_err("trampoline signature fail");
118                 BUG();
119         }
120
121         if (tail) {
122                 if (opcode == JMP32_INSN_OPCODE ||
123                     opcode == RET_INSN_OPCODE ||
124                     __is_Jcc(insn))
125                         return;
126         } else {
127                 if (opcode == CALL_INSN_OPCODE ||
128                     !memcmp(insn, x86_nops[5], 5) ||
129                     !memcmp(insn, xor5rax, 5))
130                         return;
131         }
132
133         /*
134          * If we ever trigger this, our text is corrupt, we'll probably not live long.
135          */
136         pr_err("unexpected static_call insn opcode 0x%x at %pS\n", opcode, insn);
137         BUG();
138 }
139
140 static inline enum insn_type __sc_insn(bool null, bool tail)
141 {
142         /*
143          * Encode the following table without branches:
144          *
145          *      tail    null    insn
146          *      -----+-------+------
147          *        0  |   0   |  CALL
148          *        0  |   1   |  NOP
149          *        1  |   0   |  JMP
150          *        1  |   1   |  RET
151          */
152         return 2*tail + null;
153 }
154
155 void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
156 {
157         mutex_lock(&text_mutex);
158
159         if (tramp) {
160                 __static_call_validate(tramp, true, true);
161                 __static_call_transform(tramp, __sc_insn(!func, true), func, false);
162         }
163
164         if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site) {
165                 __static_call_validate(site, tail, false);
166                 __static_call_transform(site, __sc_insn(!func, tail), func, false);
167         }
168
169         mutex_unlock(&text_mutex);
170 }
171 EXPORT_SYMBOL_GPL(arch_static_call_transform);
172
173 #ifdef CONFIG_RETHUNK
174 /*
175  * This is called by apply_returns() to fix up static call trampolines,
176  * specifically ARCH_DEFINE_STATIC_CALL_NULL_TRAMP which is recorded as
177  * having a return trampoline.
178  *
179  * The problem is that static_call() is available before determining
180  * X86_FEATURE_RETHUNK and, by implication, running alternatives.
181  *
182  * This means that __static_call_transform() above can have overwritten the
183  * return trampoline and we now need to fix things up to be consistent.
184  */
185 bool __static_call_fixup(void *tramp, u8 op, void *dest)
186 {
187         unsigned long addr = (unsigned long)tramp;
188         /*
189          * Not all .return_sites are a static_call trampoline (most are not).
190          * Check if the 3 bytes after the return are still kernel text, if not,
191          * then this definitely is not a trampoline and we need not worry
192          * further.
193          *
194          * This avoids the memcmp() below tripping over pagefaults etc..
195          */
196         if (((addr >> PAGE_SHIFT) != ((addr + 7) >> PAGE_SHIFT)) &&
197             !kernel_text_address(addr + 7))
198                 return false;
199
200         if (memcmp(tramp+5, tramp_ud, 3)) {
201                 /* Not a trampoline site, not our problem. */
202                 return false;
203         }
204
205         mutex_lock(&text_mutex);
206         if (op == RET_INSN_OPCODE || dest == &__x86_return_thunk)
207                 __static_call_transform(tramp, RET, NULL, true);
208         mutex_unlock(&text_mutex);
209
210         return true;
211 }
212 #endif