Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c767a54b JP |
2 | #define pr_fmt(fmt) "SMP alternatives: " fmt |
3 | ||
db5c68c8 | 4 | #include <linux/mmu_context.h> |
d769811c | 5 | #include <linux/perf_event.h> |
19d36ccd | 6 | #include <linux/vmalloc.h> |
3945dab4 | 7 | #include <linux/memory.h> |
872df34d | 8 | #include <linux/execmem.h> |
db5c68c8 | 9 | |
35de5b06 | 10 | #include <asm/text-patching.h> |
3a125539 | 11 | #include <asm/insn.h> |
ebebe307 | 12 | #include <asm/ibt.h> |
872df34d | 13 | #include <asm/set_memory.h> |
db5c68c8 | 14 | #include <asm/nmi.h> |
9a0b5817 | 15 | |
5e907bb0 IM |
16 | int __read_mostly alternatives_patched; |
17 | ||
18 | EXPORT_SYMBOL_GPL(alternatives_patched); | |
19 | ||
ab144f5e AK |
20 | #define MAX_PATCH_LEN (255-1) |
21 | ||
6becb502 PZ |
22 | #define DA_ALL (~0) |
23 | #define DA_ALT 0x01 | |
24 | #define DA_RET 0x02 | |
25 | #define DA_RETPOLINE 0x04 | |
26 | #define DA_ENDBR 0x08 | |
27 | #define DA_SMP 0x10 | |
28 | ||
86ed430c | 29 | static unsigned int debug_alternative; |
b7fb4af0 | 30 | |
d167a518 GH |
31 | static int __init debug_alt(char *str) |
32 | { | |
6becb502 PZ |
33 | if (str && *str == '=') |
34 | str++; | |
35 | ||
36 | if (!str || kstrtouint(str, 0, &debug_alternative)) | |
37 | debug_alternative = DA_ALL; | |
38 | ||
d167a518 GH |
39 | return 1; |
40 | } | |
d167a518 GH |
41 | __setup("debug-alternative", debug_alt); |
42 | ||
09488165 JB |
43 | static int noreplace_smp; |
44 | ||
b7fb4af0 JF |
45 | static int __init setup_noreplace_smp(char *str) |
46 | { | |
47 | noreplace_smp = 1; | |
48 | return 1; | |
49 | } | |
50 | __setup("noreplace-smp", setup_noreplace_smp); | |
51 | ||
6becb502 | 52 | #define DPRINTK(type, fmt, args...) \ |
db477a33 | 53 | do { \ |
6becb502 | 54 | if (debug_alternative & DA_##type) \ |
1b2e335e | 55 | printk(KERN_DEBUG pr_fmt(fmt) "\n", ##args); \ |
c767a54b | 56 | } while (0) |
d167a518 | 57 | |
6becb502 | 58 | #define DUMP_BYTES(type, buf, len, fmt, args...) \ |
48c7a250 | 59 | do { \ |
6becb502 | 60 | if (unlikely(debug_alternative & DA_##type)) { \ |
48c7a250 BP |
61 | int j; \ |
62 | \ | |
63 | if (!(len)) \ | |
64 | break; \ | |
65 | \ | |
1b2e335e | 66 | printk(KERN_DEBUG pr_fmt(fmt), ##args); \ |
48c7a250 BP |
67 | for (j = 0; j < (len) - 1; j++) \ |
68 | printk(KERN_CONT "%02hhx ", buf[j]); \ | |
69 | printk(KERN_CONT "%02hhx\n", buf[j]); \ | |
70 | } \ | |
71 | } while (0) | |
72 | ||
64e1f587 | 73 | static const unsigned char x86nops[] = |
dc326fca | 74 | { |
a89dfde3 PZ |
75 | BYTES_NOP1, |
76 | BYTES_NOP2, | |
77 | BYTES_NOP3, | |
78 | BYTES_NOP4, | |
79 | BYTES_NOP5, | |
80 | BYTES_NOP6, | |
81 | BYTES_NOP7, | |
82 | BYTES_NOP8, | |
df25edba PZ |
83 | #ifdef CONFIG_64BIT |
84 | BYTES_NOP9, | |
85 | BYTES_NOP10, | |
86 | BYTES_NOP11, | |
87 | #endif | |
9a0b5817 | 88 | }; |
d167a518 | 89 | |
a89dfde3 | 90 | const unsigned char * const x86_nops[ASM_NOP_MAX+1] = |
dc326fca | 91 | { |
32c464f5 | 92 | NULL, |
a89dfde3 PZ |
93 | x86nops, |
94 | x86nops + 1, | |
95 | x86nops + 1 + 2, | |
96 | x86nops + 1 + 2 + 3, | |
97 | x86nops + 1 + 2 + 3 + 4, | |
98 | x86nops + 1 + 2 + 3 + 4 + 5, | |
99 | x86nops + 1 + 2 + 3 + 4 + 5 + 6, | |
100 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7, | |
df25edba PZ |
101 | #ifdef CONFIG_64BIT |
102 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, | |
103 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, | |
104 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, | |
105 | #endif | |
32c464f5 | 106 | }; |
9a0b5817 | 107 | |
e52c1dc7 PZ |
108 | #ifdef CONFIG_FINEIBT |
109 | static bool cfi_paranoid __ro_after_init; | |
110 | #endif | |
111 | ||
872df34d PZ |
112 | #ifdef CONFIG_MITIGATION_ITS |
113 | ||
9f35e331 | 114 | #ifdef CONFIG_MODULES |
872df34d | 115 | static struct module *its_mod; |
9f35e331 | 116 | #endif |
872df34d PZ |
117 | static void *its_page; |
118 | static unsigned int its_offset; | |
a82b2645 PZI |
119 | struct its_array its_pages; |
120 | ||
121 | static void *__its_alloc(struct its_array *pages) | |
122 | { | |
838955f6 | 123 | void *page __free(execmem) = execmem_alloc_rw(EXECMEM_MODULE_TEXT, PAGE_SIZE); |
a82b2645 PZI |
124 | if (!page) |
125 | return NULL; | |
126 | ||
127 | void *tmp = krealloc(pages->pages, (pages->num+1) * sizeof(void *), | |
128 | GFP_KERNEL); | |
129 | if (!tmp) | |
130 | return NULL; | |
131 | ||
132 | pages->pages = tmp; | |
133 | pages->pages[pages->num++] = page; | |
134 | ||
135 | return no_free_ptr(page); | |
136 | } | |
872df34d PZ |
137 | |
138 | /* Initialize a thunk with the "jmp *reg; int3" instructions. */ | |
139 | static void *its_init_thunk(void *thunk, int reg) | |
140 | { | |
141 | u8 *bytes = thunk; | |
e52c1dc7 | 142 | int offset = 0; |
872df34d PZ |
143 | int i = 0; |
144 | ||
e52c1dc7 PZ |
145 | #ifdef CONFIG_FINEIBT |
146 | if (cfi_paranoid) { | |
147 | /* | |
148 | * When ITS uses indirect branch thunk the fineibt_paranoid | |
149 | * caller sequence doesn't fit in the caller site. So put the | |
150 | * remaining part of the sequence (<ea> + JNE) into the ITS | |
151 | * thunk. | |
152 | */ | |
153 | bytes[i++] = 0xea; /* invalid instruction */ | |
154 | bytes[i++] = 0x75; /* JNE */ | |
155 | bytes[i++] = 0xfd; | |
156 | ||
157 | offset = 1; | |
158 | } | |
159 | #endif | |
160 | ||
872df34d PZ |
161 | if (reg >= 8) { |
162 | bytes[i++] = 0x41; /* REX.B prefix */ | |
163 | reg -= 8; | |
164 | } | |
165 | bytes[i++] = 0xff; | |
166 | bytes[i++] = 0xe0 + reg; /* jmp *reg */ | |
167 | bytes[i++] = 0xcc; | |
168 | ||
e52c1dc7 | 169 | return thunk + offset; |
872df34d PZ |
170 | } |
171 | ||
a82b2645 PZI |
172 | static void its_pages_protect(struct its_array *pages) |
173 | { | |
174 | for (int i = 0; i < pages->num; i++) { | |
175 | void *page = pages->pages[i]; | |
176 | execmem_restore_rox(page, PAGE_SIZE); | |
177 | } | |
178 | } | |
179 | ||
180 | static void its_fini_core(void) | |
181 | { | |
182 | if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) | |
183 | its_pages_protect(&its_pages); | |
184 | kfree(its_pages.pages); | |
185 | } | |
186 | ||
9f35e331 | 187 | #ifdef CONFIG_MODULES |
872df34d PZ |
188 | void its_init_mod(struct module *mod) |
189 | { | |
190 | if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) | |
191 | return; | |
192 | ||
193 | mutex_lock(&text_mutex); | |
194 | its_mod = mod; | |
195 | its_page = NULL; | |
196 | } | |
197 | ||
198 | void its_fini_mod(struct module *mod) | |
199 | { | |
200 | if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) | |
201 | return; | |
202 | ||
203 | WARN_ON_ONCE(its_mod != mod); | |
204 | ||
205 | its_mod = NULL; | |
206 | its_page = NULL; | |
207 | mutex_unlock(&text_mutex); | |
208 | ||
a82b2645 PZI |
209 | if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) |
210 | its_pages_protect(&mod->arch.its_pages); | |
872df34d PZ |
211 | } |
212 | ||
213 | void its_free_mod(struct module *mod) | |
214 | { | |
215 | if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) | |
216 | return; | |
217 | ||
0b0cae71 MRM |
218 | for (int i = 0; i < mod->arch.its_pages.num; i++) { |
219 | void *page = mod->arch.its_pages.pages[i]; | |
872df34d PZ |
220 | execmem_free(page); |
221 | } | |
0b0cae71 | 222 | kfree(mod->arch.its_pages.pages); |
872df34d | 223 | } |
9f35e331 | 224 | #endif /* CONFIG_MODULES */ |
872df34d PZ |
225 | |
226 | static void *its_alloc(void) | |
227 | { | |
a82b2645 PZI |
228 | struct its_array *pages = &its_pages; |
229 | void *page; | |
872df34d | 230 | |
3c902383 | 231 | #ifdef CONFIG_MODULES |
a82b2645 PZI |
232 | if (its_mod) |
233 | pages = &its_mod->arch.its_pages; | |
234 | #endif | |
235 | ||
236 | page = __its_alloc(pages); | |
872df34d PZ |
237 | if (!page) |
238 | return NULL; | |
239 | ||
a82b2645 PZI |
240 | if (pages == &its_pages) |
241 | set_memory_x((unsigned long)page, 1); | |
872df34d | 242 | |
a82b2645 | 243 | return page; |
872df34d PZ |
244 | } |
245 | ||
246 | static void *its_allocate_thunk(int reg) | |
247 | { | |
248 | int size = 3 + (reg / 8); | |
249 | void *thunk; | |
250 | ||
e52c1dc7 PZ |
251 | #ifdef CONFIG_FINEIBT |
252 | /* | |
253 | * The ITS thunk contains an indirect jump and an int3 instruction so | |
254 | * its size is 3 or 4 bytes depending on the register used. If CFI | |
255 | * paranoid is used then 3 extra bytes are added in the ITS thunk to | |
256 | * complete the fineibt_paranoid caller sequence. | |
257 | */ | |
258 | if (cfi_paranoid) | |
259 | size += 3; | |
260 | #endif | |
261 | ||
872df34d PZ |
262 | if (!its_page || (its_offset + size - 1) >= PAGE_SIZE) { |
263 | its_page = its_alloc(); | |
264 | if (!its_page) { | |
265 | pr_err("ITS page allocation failed\n"); | |
266 | return NULL; | |
267 | } | |
268 | memset(its_page, INT3_INSN_OPCODE, PAGE_SIZE); | |
269 | its_offset = 32; | |
270 | } | |
271 | ||
272 | /* | |
273 | * If the indirect branch instruction will be in the lower half | |
274 | * of a cacheline, then update the offset to reach the upper half. | |
275 | */ | |
276 | if ((its_offset + size - 1) % 64 < 32) | |
277 | its_offset = ((its_offset - 1) | 0x3F) + 33; | |
278 | ||
279 | thunk = its_page + its_offset; | |
280 | its_offset += size; | |
281 | ||
282 | return its_init_thunk(thunk, reg); | |
283 | } | |
284 | ||
e52c1dc7 PZ |
285 | u8 *its_static_thunk(int reg) |
286 | { | |
287 | u8 *thunk = __x86_indirect_its_thunk_array[reg]; | |
288 | ||
289 | #ifdef CONFIG_FINEIBT | |
290 | /* Paranoid thunk starts 2 bytes before */ | |
291 | if (cfi_paranoid) | |
292 | return thunk - 2; | |
293 | #endif | |
294 | return thunk; | |
295 | } | |
296 | ||
a82b2645 PZI |
297 | #else |
298 | static inline void its_fini_core(void) {} | |
299 | #endif /* CONFIG_MITIGATION_ITS */ | |
872df34d | 300 | |
f796c758 BPA |
301 | /* |
302 | * Nomenclature for variable names to simplify and clarify this code and ease | |
303 | * any potential staring at it: | |
304 | * | |
305 | * @instr: source address of the original instructions in the kernel text as | |
306 | * generated by the compiler. | |
307 | * | |
308 | * @buf: temporary buffer on which the patching operates. This buffer is | |
309 | * eventually text-poked into the kernel image. | |
310 | * | |
311 | * @replacement/@repl: pointer to the opcodes which are replacing @instr, located | |
312 | * in the .altinstr_replacement section. | |
313 | */ | |
314 | ||
6c480f22 | 315 | /* |
b6c881b2 PZ |
316 | * Fill the buffer with a single effective instruction of size @len. |
317 | * | |
6c480f22 PZ |
318 | * In order not to issue an ORC stack depth tracking CFI entry (Call Frame Info) |
319 | * for every single-byte NOP, try to generate the maximally available NOP of | |
320 | * size <= ASM_NOP_MAX such that only a single CFI entry is generated (vs one for | |
321 | * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and | |
322 | * *jump* over instead of executing long and daft NOPs. | |
323 | */ | |
f796c758 | 324 | static void add_nop(u8 *buf, unsigned int len) |
139ec7c4 | 325 | { |
f796c758 | 326 | u8 *target = buf + len; |
6c480f22 PZ |
327 | |
328 | if (!len) | |
329 | return; | |
330 | ||
331 | if (len <= ASM_NOP_MAX) { | |
f796c758 | 332 | memcpy(buf, x86_nops[len], len); |
6c480f22 | 333 | return; |
139ec7c4 | 334 | } |
6c480f22 PZ |
335 | |
336 | if (len < 128) { | |
f796c758 BPA |
337 | __text_gen_insn(buf, JMP8_INSN_OPCODE, buf, target, JMP8_INSN_SIZE); |
338 | buf += JMP8_INSN_SIZE; | |
6c480f22 | 339 | } else { |
f796c758 BPA |
340 | __text_gen_insn(buf, JMP32_INSN_OPCODE, buf, target, JMP32_INSN_SIZE); |
341 | buf += JMP32_INSN_SIZE; | |
6c480f22 PZ |
342 | } |
343 | ||
f796c758 BPA |
344 | for (;buf < target; buf++) |
345 | *buf = INT3_INSN_OPCODE; | |
139ec7c4 RR |
346 | } |
347 | ||
b6c881b2 PZ |
348 | /* |
349 | * Matches NOP and NOPL, not any of the other possible NOPs. | |
350 | */ | |
6c480f22 | 351 | static bool insn_is_nop(struct insn *insn) |
2b31e8ed | 352 | { |
2bd4aa93 PZ |
353 | /* Anything NOP, but no REP NOP */ |
354 | if (insn->opcode.bytes[0] == 0x90 && | |
355 | (!insn->prefixes.nbytes || insn->prefixes.bytes[0] != 0xF3)) | |
6c480f22 | 356 | return true; |
2b31e8ed | 357 | |
2bd4aa93 | 358 | /* NOPL */ |
6c480f22 PZ |
359 | if (insn->opcode.bytes[0] == 0x0F && insn->opcode.bytes[1] == 0x1F) |
360 | return true; | |
2b31e8ed | 361 | |
6c480f22 | 362 | /* TODO: more nops */ |
2b31e8ed | 363 | |
6c480f22 PZ |
364 | return false; |
365 | } | |
2b31e8ed | 366 | |
b6c881b2 PZ |
367 | /* |
368 | * Find the offset of the first non-NOP instruction starting at @offset | |
369 | * but no further than @len. | |
370 | */ | |
f796c758 | 371 | static int skip_nops(u8 *buf, int offset, int len) |
6c480f22 PZ |
372 | { |
373 | struct insn insn; | |
2b31e8ed | 374 | |
6c480f22 | 375 | for (; offset < len; offset += insn.length) { |
f796c758 | 376 | if (insn_decode_kernel(&insn, &buf[offset])) |
6c480f22 | 377 | break; |
2b31e8ed | 378 | |
6c480f22 PZ |
379 | if (!insn_is_nop(&insn)) |
380 | break; | |
381 | } | |
2b31e8ed | 382 | |
6c480f22 | 383 | return offset; |
2b31e8ed BP |
384 | } |
385 | ||
34bfab0e BP |
386 | /* |
387 | * "noinline" to cause control flow change and thus invalidate I$ and | |
388 | * cause refetch after modification. | |
389 | */ | |
da8f9cf7 | 390 | static void noinline optimize_nops(const u8 * const instr, u8 *buf, size_t len) |
4fd4b6e5 | 391 | { |
6c480f22 PZ |
392 | for (int next, i = 0; i < len; i = next) { |
393 | struct insn insn; | |
66c117d7 | 394 | |
f796c758 | 395 | if (insn_decode_kernel(&insn, &buf[i])) |
23c1ad53 PZ |
396 | return; |
397 | ||
6c480f22 | 398 | next = i + insn.length; |
23c1ad53 | 399 | |
da8f9cf7 BPA |
400 | if (insn_is_nop(&insn)) { |
401 | int nop = i; | |
402 | ||
c3a3cb5c BPA |
403 | /* Has the NOP already been optimized? */ |
404 | if (i + insn.length == len) | |
405 | return; | |
406 | ||
da8f9cf7 BPA |
407 | next = skip_nops(buf, next, len); |
408 | ||
409 | add_nop(buf + nop, next - nop); | |
410 | DUMP_BYTES(ALT, buf, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, next); | |
411 | } | |
612e8e93 | 412 | } |
4fd4b6e5 BP |
413 | } |
414 | ||
270a69c4 PZ |
415 | /* |
416 | * In this context, "source" is where the instructions are placed in the | |
417 | * section .altinstr_replacement, for example during kernel build by the | |
418 | * toolchain. | |
419 | * "Destination" is where the instructions are being patched in by this | |
420 | * machinery. | |
421 | * | |
422 | * The source offset is: | |
423 | * | |
424 | * src_imm = target - src_next_ip (1) | |
425 | * | |
426 | * and the target offset is: | |
427 | * | |
428 | * dst_imm = target - dst_next_ip (2) | |
429 | * | |
430 | * so rework (1) as an expression for target like: | |
431 | * | |
432 | * target = src_imm + src_next_ip (1a) | |
433 | * | |
434 | * and substitute in (2) to get: | |
435 | * | |
436 | * dst_imm = (src_imm + src_next_ip) - dst_next_ip (3) | |
437 | * | |
438 | * Now, since the instruction stream is 'identical' at src and dst (it | |
439 | * is being copied after all) it can be stated that: | |
440 | * | |
441 | * src_next_ip = src + ip_offset | |
442 | * dst_next_ip = dst + ip_offset (4) | |
443 | * | |
444 | * Substitute (4) in (3) and observe ip_offset being cancelled out to | |
445 | * obtain: | |
446 | * | |
447 | * dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset) | |
448 | * = src_imm + src - dst + ip_offset - ip_offset | |
449 | * = src_imm + src - dst (5) | |
450 | * | |
451 | * IOW, only the relative displacement of the code block matters. | |
452 | */ | |
453 | ||
454 | #define apply_reloc_n(n_, p_, d_) \ | |
455 | do { \ | |
456 | s32 v = *(s##n_ *)(p_); \ | |
457 | v += (d_); \ | |
458 | BUG_ON((v >> 31) != (v >> (n_-1))); \ | |
459 | *(s##n_ *)(p_) = (s##n_)v; \ | |
460 | } while (0) | |
461 | ||
462 | ||
463 | static __always_inline | |
464 | void apply_reloc(int n, void *ptr, uintptr_t diff) | |
465 | { | |
466 | switch (n) { | |
467 | case 1: apply_reloc_n(8, ptr, diff); break; | |
468 | case 2: apply_reloc_n(16, ptr, diff); break; | |
469 | case 4: apply_reloc_n(32, ptr, diff); break; | |
470 | default: BUG(); | |
471 | } | |
472 | } | |
473 | ||
474 | static __always_inline | |
475 | bool need_reloc(unsigned long offset, u8 *src, size_t src_len) | |
476 | { | |
477 | u8 *target = src + offset; | |
478 | /* | |
479 | * If the target is inside the patched block, it's relative to the | |
480 | * block itself and does not need relocation. | |
481 | */ | |
482 | return (target < src || target > src + src_len); | |
483 | } | |
484 | ||
da8f9cf7 | 485 | static void __apply_relocation(u8 *buf, const u8 * const instr, size_t instrlen, u8 *repl, size_t repl_len) |
270a69c4 | 486 | { |
f796c758 | 487 | for (int next, i = 0; i < instrlen; i = next) { |
270a69c4 PZ |
488 | struct insn insn; |
489 | ||
490 | if (WARN_ON_ONCE(insn_decode_kernel(&insn, &buf[i]))) | |
491 | return; | |
492 | ||
493 | next = i + insn.length; | |
494 | ||
495 | switch (insn.opcode.bytes[0]) { | |
496 | case 0x0f: | |
497 | if (insn.opcode.bytes[1] < 0x80 || | |
498 | insn.opcode.bytes[1] > 0x8f) | |
499 | break; | |
500 | ||
501 | fallthrough; /* Jcc.d32 */ | |
502 | case 0x70 ... 0x7f: /* Jcc.d8 */ | |
503 | case JMP8_INSN_OPCODE: | |
504 | case JMP32_INSN_OPCODE: | |
505 | case CALL_INSN_OPCODE: | |
f796c758 | 506 | if (need_reloc(next + insn.immediate.value, repl, repl_len)) { |
270a69c4 PZ |
507 | apply_reloc(insn.immediate.nbytes, |
508 | buf + i + insn_offset_immediate(&insn), | |
f796c758 | 509 | repl - instr); |
270a69c4 PZ |
510 | } |
511 | ||
512 | /* | |
513 | * Where possible, convert JMP.d32 into JMP.d8. | |
514 | */ | |
515 | if (insn.opcode.bytes[0] == JMP32_INSN_OPCODE) { | |
516 | s32 imm = insn.immediate.value; | |
f796c758 | 517 | imm += repl - instr; |
270a69c4 PZ |
518 | imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE; |
519 | if ((imm >> 31) == (imm >> 7)) { | |
520 | buf[i+0] = JMP8_INSN_OPCODE; | |
521 | buf[i+1] = (s8)imm; | |
522 | ||
523 | memset(&buf[i+2], INT3_INSN_OPCODE, insn.length - 2); | |
524 | } | |
525 | } | |
526 | break; | |
527 | } | |
528 | ||
529 | if (insn_rip_relative(&insn)) { | |
f796c758 | 530 | if (need_reloc(next + insn.displacement.value, repl, repl_len)) { |
270a69c4 PZ |
531 | apply_reloc(insn.displacement.nbytes, |
532 | buf + i + insn_offset_displacement(&insn), | |
f796c758 | 533 | repl - instr); |
270a69c4 PZ |
534 | } |
535 | } | |
270a69c4 PZ |
536 | } |
537 | } | |
538 | ||
023f42dd | 539 | void text_poke_apply_relocation(u8 *buf, const u8 * const instr, size_t instrlen, u8 *repl, size_t repl_len) |
da8f9cf7 BPA |
540 | { |
541 | __apply_relocation(buf, instr, instrlen, repl, repl_len); | |
9dba9c67 | 542 | optimize_nops(instr, buf, instrlen); |
da8f9cf7 BPA |
543 | } |
544 | ||
9824b00c JG |
545 | /* Low-level backend functions usable from alternative code replacements. */ |
546 | DEFINE_ASM_FUNC(nop_func, "", .entry.text); | |
547 | EXPORT_SYMBOL_GPL(nop_func); | |
548 | ||
549 | noinstr void BUG_func(void) | |
550 | { | |
551 | BUG(); | |
552 | } | |
f7cfe701 | 553 | EXPORT_SYMBOL(BUG_func); |
9824b00c | 554 | |
da0fe6e6 JG |
555 | #define CALL_RIP_REL_OPCODE 0xff |
556 | #define CALL_RIP_REL_MODRM 0x15 | |
557 | ||
558 | /* | |
559 | * Rewrite the "call BUG_func" replacement to point to the target of the | |
560 | * indirect pv_ops call "call *disp(%ip)". | |
561 | */ | |
1d7e707a | 562 | static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a) |
da0fe6e6 JG |
563 | { |
564 | void *target, *bug = &BUG_func; | |
565 | s32 disp; | |
566 | ||
567 | if (a->replacementlen != 5 || insn_buff[0] != CALL_INSN_OPCODE) { | |
568 | pr_err("ALT_FLAG_DIRECT_CALL set for a non-call replacement instruction\n"); | |
569 | BUG(); | |
570 | } | |
571 | ||
572 | if (a->instrlen != 6 || | |
1d7e707a MRM |
573 | instr[0] != CALL_RIP_REL_OPCODE || |
574 | instr[1] != CALL_RIP_REL_MODRM) { | |
da0fe6e6 JG |
575 | pr_err("ALT_FLAG_DIRECT_CALL set for unrecognized indirect call\n"); |
576 | BUG(); | |
577 | } | |
578 | ||
579 | /* Skip CALL_RIP_REL_OPCODE and CALL_RIP_REL_MODRM */ | |
1d7e707a | 580 | disp = *(s32 *)(instr + 2); |
da0fe6e6 JG |
581 | #ifdef CONFIG_X86_64 |
582 | /* ff 15 00 00 00 00 call *0x0(%rip) */ | |
583 | /* target address is stored at "next instruction + disp". */ | |
584 | target = *(void **)(instr + a->instrlen + disp); | |
585 | #else | |
586 | /* ff 15 00 00 00 00 call *0x0 */ | |
587 | /* target address is stored at disp. */ | |
588 | target = *(void **)disp; | |
589 | #endif | |
590 | if (!target) | |
591 | target = bug; | |
592 | ||
593 | /* (BUG_func - .) + (target - BUG_func) := target - . */ | |
594 | *(s32 *)(insn_buff + 1) += target - bug; | |
595 | ||
596 | if (target == &nop_func) | |
597 | return 0; | |
598 | ||
599 | return 5; | |
600 | } | |
601 | ||
d2a793da PZ |
602 | static inline u8 * instr_va(struct alt_instr *i) |
603 | { | |
604 | return (u8 *)&i->instr_offset + i->instr_offset; | |
605 | } | |
606 | ||
db477a33 BP |
607 | /* |
608 | * Replace instructions with better alternatives for this CPU type. This runs | |
609 | * before SMP is initialized to avoid SMP problems with self modifying code. | |
610 | * This implies that asymmetric systems where APs have less capabilities than | |
611 | * the boot processor are not handled. Tough. Make sure you disable such | |
612 | * features by hand. | |
34bfab0e BP |
613 | * |
614 | * Marked "noinline" to cause control flow change and thus insn cache | |
615 | * to refetch changed I$ lines. | |
db477a33 | 616 | */ |
34bfab0e | 617 | void __init_or_module noinline apply_alternatives(struct alt_instr *start, |
1d7e707a | 618 | struct alt_instr *end) |
9a0b5817 | 619 | { |
1fc654cf | 620 | u8 insn_buff[MAX_PATCH_LEN]; |
05d277c9 | 621 | u8 *instr, *replacement; |
d2a793da | 622 | struct alt_instr *a, *b; |
9a0b5817 | 623 | |
6becb502 | 624 | DPRINTK(ALT, "alt table %px, -> %px", start, end); |
d35652a5 KS |
625 | |
626 | /* | |
7212b58d | 627 | * KASAN_SHADOW_START is defined using |
d35652a5 KS |
628 | * cpu_feature_enabled(X86_FEATURE_LA57) and is therefore patched here. |
629 | * During the process, KASAN becomes confused seeing partial LA57 | |
630 | * conversion and triggers a false-positive out-of-bound report. | |
631 | * | |
632 | * Disable KASAN until the patching is complete. | |
633 | */ | |
634 | kasan_disable_current(); | |
635 | ||
50973133 FY |
636 | /* |
637 | * The scan order should be from start to end. A later scanned | |
db477a33 | 638 | * alternative code can overwrite previously scanned alternative code. |
50973133 FY |
639 | * Some kernel functions (e.g. memcpy, memset, etc) use this order to |
640 | * patch code. | |
641 | * | |
642 | * So be careful if you want to change the scan order to any other | |
643 | * order. | |
644 | */ | |
9a0b5817 | 645 | for (a = start; a < end; a++) { |
1fc654cf | 646 | int insn_buff_sz = 0; |
48c7a250 | 647 | |
d2a793da PZ |
648 | /* |
649 | * In case of nested ALTERNATIVE()s the outer alternative might | |
650 | * add more padding. To ensure consistent patching find the max | |
651 | * padding for all alt_instr entries for this site (nested | |
652 | * alternatives result in consecutive entries). | |
653 | */ | |
654 | for (b = a+1; b < end && instr_va(b) == instr_va(a); b++) { | |
655 | u8 len = max(a->instrlen, b->instrlen); | |
656 | a->instrlen = b->instrlen = len; | |
657 | } | |
658 | ||
659 | instr = instr_va(a); | |
59e97e4d | 660 | replacement = (u8 *)&a->repl_offset + a->repl_offset; |
1fc654cf | 661 | BUG_ON(a->instrlen > sizeof(insn_buff)); |
5d1dd961 | 662 | BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); |
dda7bb76 JG |
663 | |
664 | /* | |
665 | * Patch if either: | |
666 | * - feature is present | |
5d1dd961 | 667 | * - feature not present but ALT_FLAG_NOT is set to mean, |
dda7bb76 JG |
668 | * patch if feature is *NOT* present. |
669 | */ | |
270a69c4 | 670 | if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { |
1d7e707a | 671 | memcpy(insn_buff, instr, a->instrlen); |
f796c758 | 672 | optimize_nops(instr, insn_buff, a->instrlen); |
1d7e707a | 673 | text_poke_early(instr, insn_buff, a->instrlen); |
270a69c4 PZ |
674 | continue; |
675 | } | |
59e97e4d | 676 | |
7991ed43 | 677 | DPRINTK(ALT, "feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x", |
5d1dd961 BPA |
678 | a->cpuid >> 5, |
679 | a->cpuid & 0x1f, | |
c1d4e419 | 680 | instr, instr, a->instrlen, |
da0fe6e6 | 681 | replacement, a->replacementlen, a->flags); |
db477a33 | 682 | |
1d7e707a | 683 | memcpy(insn_buff, replacement, a->replacementlen); |
1fc654cf | 684 | insn_buff_sz = a->replacementlen; |
59e97e4d | 685 | |
da0fe6e6 | 686 | if (a->flags & ALT_FLAG_DIRECT_CALL) { |
1d7e707a | 687 | insn_buff_sz = alt_replace_call(instr, insn_buff, a); |
da0fe6e6 JG |
688 | if (insn_buff_sz < 0) |
689 | continue; | |
690 | } | |
691 | ||
23c1ad53 PZ |
692 | for (; insn_buff_sz < a->instrlen; insn_buff_sz++) |
693 | insn_buff[insn_buff_sz] = 0x90; | |
694 | ||
023f42dd | 695 | text_poke_apply_relocation(insn_buff, instr, a->instrlen, replacement, a->replacementlen); |
270a69c4 | 696 | |
1d7e707a | 697 | DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); |
270a69c4 | 698 | DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); |
6becb502 | 699 | DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr); |
59e97e4d | 700 | |
1d7e707a | 701 | text_poke_early(instr, insn_buff, insn_buff_sz); |
9a0b5817 | 702 | } |
d35652a5 KS |
703 | |
704 | kasan_enable_current(); | |
9a0b5817 GH |
705 | } |
706 | ||
ac0ee0a9 PZ |
707 | static inline bool is_jcc32(struct insn *insn) |
708 | { | |
709 | /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ | |
710 | return insn->opcode.bytes[0] == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80; | |
711 | } | |
712 | ||
aefb2f2e | 713 | #if defined(CONFIG_MITIGATION_RETPOLINE) && defined(CONFIG_OBJTOOL) |
75085009 PZ |
714 | |
715 | /* | |
716 | * CALL/JMP *%\reg | |
717 | */ | |
718 | static int emit_indirect(int op, int reg, u8 *bytes) | |
719 | { | |
720 | int i = 0; | |
721 | u8 modrm; | |
722 | ||
723 | switch (op) { | |
724 | case CALL_INSN_OPCODE: | |
725 | modrm = 0x10; /* Reg = 2; CALL r/m */ | |
726 | break; | |
727 | ||
728 | case JMP32_INSN_OPCODE: | |
729 | modrm = 0x20; /* Reg = 4; JMP r/m */ | |
730 | break; | |
731 | ||
732 | default: | |
733 | WARN_ON_ONCE(1); | |
734 | return -1; | |
735 | } | |
736 | ||
737 | if (reg >= 8) { | |
738 | bytes[i++] = 0x41; /* REX.B prefix */ | |
739 | reg -= 8; | |
740 | } | |
741 | ||
742 | modrm |= 0xc0; /* Mod = 3 */ | |
743 | modrm += reg; | |
744 | ||
745 | bytes[i++] = 0xff; /* opcode */ | |
746 | bytes[i++] = modrm; | |
747 | ||
748 | return i; | |
749 | } | |
750 | ||
8754e67a PG |
751 | static int __emit_trampoline(void *addr, struct insn *insn, u8 *bytes, |
752 | void *call_dest, void *jmp_dest) | |
3b6c1747 PZ |
753 | { |
754 | u8 op = insn->opcode.bytes[0]; | |
755 | int i = 0; | |
756 | ||
757 | /* | |
758 | * Clang does 'weird' Jcc __x86_indirect_thunk_r11 conditional | |
759 | * tail-calls. Deal with them. | |
760 | */ | |
761 | if (is_jcc32(insn)) { | |
762 | bytes[i++] = op; | |
763 | op = insn->opcode.bytes[1]; | |
764 | goto clang_jcc; | |
765 | } | |
766 | ||
767 | if (insn->length == 6) | |
768 | bytes[i++] = 0x2e; /* CS-prefix */ | |
769 | ||
770 | switch (op) { | |
771 | case CALL_INSN_OPCODE: | |
772 | __text_gen_insn(bytes+i, op, addr+i, | |
8754e67a | 773 | call_dest, |
3b6c1747 PZ |
774 | CALL_INSN_SIZE); |
775 | i += CALL_INSN_SIZE; | |
776 | break; | |
777 | ||
778 | case JMP32_INSN_OPCODE: | |
779 | clang_jcc: | |
780 | __text_gen_insn(bytes+i, op, addr+i, | |
8754e67a | 781 | jmp_dest, |
3b6c1747 PZ |
782 | JMP32_INSN_SIZE); |
783 | i += JMP32_INSN_SIZE; | |
784 | break; | |
785 | ||
786 | default: | |
ae25e00b | 787 | WARN(1, "%pS %px %*ph\n", addr, addr, 6, addr); |
3b6c1747 PZ |
788 | return -1; |
789 | } | |
790 | ||
791 | WARN_ON_ONCE(i != insn->length); | |
792 | ||
793 | return i; | |
794 | } | |
795 | ||
8754e67a PG |
796 | static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8 *bytes) |
797 | { | |
798 | return __emit_trampoline(addr, insn, bytes, | |
799 | __x86_indirect_call_thunk_array[reg], | |
800 | __x86_indirect_jump_thunk_array[reg]); | |
801 | } | |
802 | ||
803 | #ifdef CONFIG_MITIGATION_ITS | |
804 | static int emit_its_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes) | |
805 | { | |
872df34d PZ |
806 | u8 *thunk = __x86_indirect_its_thunk_array[reg]; |
807 | u8 *tmp = its_allocate_thunk(reg); | |
808 | ||
809 | if (tmp) | |
810 | thunk = tmp; | |
811 | ||
812 | return __emit_trampoline(addr, insn, bytes, thunk, thunk); | |
8754e67a PG |
813 | } |
814 | ||
815 | /* Check if an indirect branch is at ITS-unsafe address */ | |
816 | static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg) | |
817 | { | |
818 | if (!cpu_feature_enabled(X86_FEATURE_INDIRECT_THUNK_ITS)) | |
819 | return false; | |
820 | ||
821 | /* Indirect branch opcode is 2 or 3 bytes depending on reg */ | |
822 | addr += 1 + reg / 8; | |
823 | ||
824 | /* Lower-half of the cacheline? */ | |
825 | return !(addr & 0x20); | |
826 | } | |
e52c1dc7 PZ |
827 | #else /* CONFIG_MITIGATION_ITS */ |
828 | ||
829 | #ifdef CONFIG_FINEIBT | |
830 | static bool cpu_wants_indirect_its_thunk_at(unsigned long addr, int reg) | |
831 | { | |
832 | return false; | |
833 | } | |
8754e67a PG |
834 | #endif |
835 | ||
e52c1dc7 PZ |
836 | #endif /* CONFIG_MITIGATION_ITS */ |
837 | ||
75085009 PZ |
838 | /* |
839 | * Rewrite the compiler generated retpoline thunk calls. | |
840 | * | |
841 | * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate | |
842 | * indirect instructions, avoiding the extra indirection. | |
843 | * | |
844 | * For example, convert: | |
845 | * | |
846 | * CALL __x86_indirect_thunk_\reg | |
847 | * | |
848 | * into: | |
849 | * | |
850 | * CALL *%\reg | |
851 | * | |
d45476d9 | 852 | * It also tries to inline spectre_v2=retpoline,lfence when size permits. |
75085009 PZ |
853 | */ |
854 | static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) | |
855 | { | |
856 | retpoline_thunk_t *target; | |
2f0cbb2a PZ |
857 | int reg, ret, i = 0; |
858 | u8 op, cc; | |
75085009 PZ |
859 | |
860 | target = addr + insn->length + insn->immediate.value; | |
861 | reg = target - __x86_indirect_thunk_array; | |
862 | ||
863 | if (WARN_ON_ONCE(reg & ~0xf)) | |
864 | return -1; | |
865 | ||
866 | /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */ | |
867 | BUG_ON(reg == 4); | |
868 | ||
bbe2df3f | 869 | if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) && |
3b6c1747 PZ |
870 | !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { |
871 | if (cpu_feature_enabled(X86_FEATURE_CALL_DEPTH)) | |
872 | return emit_call_track_retpoline(addr, insn, reg, bytes); | |
873 | ||
75085009 | 874 | return -1; |
3b6c1747 | 875 | } |
75085009 | 876 | |
2f0cbb2a PZ |
877 | op = insn->opcode.bytes[0]; |
878 | ||
879 | /* | |
880 | * Convert: | |
881 | * | |
882 | * Jcc.d32 __x86_indirect_thunk_\reg | |
883 | * | |
884 | * into: | |
885 | * | |
886 | * Jncc.d8 1f | |
bbe2df3f | 887 | * [ LFENCE ] |
2f0cbb2a | 888 | * JMP *%\reg |
bbe2df3f | 889 | * [ NOP ] |
2f0cbb2a PZ |
890 | * 1: |
891 | */ | |
3b6c1747 | 892 | if (is_jcc32(insn)) { |
2f0cbb2a PZ |
893 | cc = insn->opcode.bytes[1] & 0xf; |
894 | cc ^= 1; /* invert condition */ | |
895 | ||
896 | bytes[i++] = 0x70 + cc; /* Jcc.d8 */ | |
897 | bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */ | |
898 | ||
899 | /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */ | |
900 | op = JMP32_INSN_OPCODE; | |
901 | } | |
902 | ||
bbe2df3f | 903 | /* |
d45476d9 | 904 | * For RETPOLINE_LFENCE: prepend the indirect CALL/JMP with an LFENCE. |
bbe2df3f | 905 | */ |
d45476d9 | 906 | if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { |
bbe2df3f PZ |
907 | bytes[i++] = 0x0f; |
908 | bytes[i++] = 0xae; | |
909 | bytes[i++] = 0xe8; /* LFENCE */ | |
910 | } | |
911 | ||
8754e67a PG |
912 | #ifdef CONFIG_MITIGATION_ITS |
913 | /* | |
914 | * Check if the address of last byte of emitted-indirect is in | |
915 | * lower-half of the cacheline. Such branches need ITS mitigation. | |
916 | */ | |
917 | if (cpu_wants_indirect_its_thunk_at((unsigned long)addr + i, reg)) | |
918 | return emit_its_trampoline(addr, insn, reg, bytes); | |
919 | #endif | |
920 | ||
2f0cbb2a PZ |
921 | ret = emit_indirect(op, reg, bytes + i); |
922 | if (ret < 0) | |
923 | return ret; | |
924 | i += ret; | |
75085009 | 925 | |
8c03af3e PZ |
926 | /* |
927 | * The compiler is supposed to EMIT an INT3 after every unconditional | |
928 | * JMP instruction due to AMD BTC. However, if the compiler is too old | |
7b75782f BL |
929 | * or MITIGATION_SLS isn't enabled, we still need an INT3 after |
930 | * indirect JMPs even on Intel. | |
8c03af3e PZ |
931 | */ |
932 | if (op == JMP32_INSN_OPCODE && i < insn->length) | |
933 | bytes[i++] = INT3_INSN_OPCODE; | |
934 | ||
75085009 PZ |
935 | for (; i < insn->length;) |
936 | bytes[i++] = BYTES_NOP1; | |
937 | ||
938 | return i; | |
939 | } | |
940 | ||
941 | /* | |
942 | * Generated by 'objtool --retpoline'. | |
943 | */ | |
1d7e707a | 944 | void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) |
75085009 PZ |
945 | { |
946 | s32 *s; | |
947 | ||
948 | for (s = start; s < end; s++) { | |
949 | void *addr = (void *)s + *s; | |
950 | struct insn insn; | |
951 | int len, ret; | |
952 | u8 bytes[16]; | |
953 | u8 op1, op2; | |
e52c1dc7 | 954 | u8 *dest; |
75085009 | 955 | |
1d7e707a | 956 | ret = insn_decode_kernel(&insn, addr); |
75085009 PZ |
957 | if (WARN_ON_ONCE(ret < 0)) |
958 | continue; | |
959 | ||
960 | op1 = insn.opcode.bytes[0]; | |
961 | op2 = insn.opcode.bytes[1]; | |
962 | ||
963 | switch (op1) { | |
97e59672 PZ |
964 | case 0x70 ... 0x7f: /* Jcc.d8 */ |
965 | /* See cfi_paranoid. */ | |
966 | WARN_ON_ONCE(cfi_mode != CFI_FINEIBT); | |
967 | continue; | |
968 | ||
75085009 PZ |
969 | case CALL_INSN_OPCODE: |
970 | case JMP32_INSN_OPCODE: | |
e52c1dc7 PZ |
971 | /* Check for cfi_paranoid + ITS */ |
972 | dest = addr + insn.length + insn.immediate.value; | |
973 | if (dest[-1] == 0xea && (dest[0] & 0xf0) == 0x70) { | |
974 | WARN_ON_ONCE(cfi_mode != CFI_FINEIBT); | |
975 | continue; | |
976 | } | |
75085009 PZ |
977 | break; |
978 | ||
2f0cbb2a PZ |
979 | case 0x0f: /* escape */ |
980 | if (op2 >= 0x80 && op2 <= 0x8f) | |
981 | break; | |
982 | fallthrough; | |
75085009 PZ |
983 | default: |
984 | WARN_ON_ONCE(1); | |
985 | continue; | |
986 | } | |
987 | ||
6becb502 | 988 | DPRINTK(RETPOLINE, "retpoline at: %pS (%px) len: %d to: %pS", |
d4b5a5c9 PZ |
989 | addr, addr, insn.length, |
990 | addr + insn.length + insn.immediate.value); | |
991 | ||
75085009 PZ |
992 | len = patch_retpoline(addr, &insn, bytes); |
993 | if (len == insn.length) { | |
f796c758 | 994 | optimize_nops(addr, bytes, len); |
1d7e707a | 995 | DUMP_BYTES(RETPOLINE, ((u8*)addr), len, "%px: orig: ", addr); |
6becb502 | 996 | DUMP_BYTES(RETPOLINE, ((u8*)bytes), len, "%px: repl: ", addr); |
1d7e707a | 997 | text_poke_early(addr, bytes, len); |
75085009 PZ |
998 | } |
999 | } | |
1000 | } | |
1001 | ||
0911b8c5 | 1002 | #ifdef CONFIG_MITIGATION_RETHUNK |
770ae1b7 | 1003 | |
a75bf27f PG |
1004 | bool cpu_wants_rethunk(void) |
1005 | { | |
1006 | return cpu_feature_enabled(X86_FEATURE_RETHUNK); | |
1007 | } | |
1008 | ||
1009 | bool cpu_wants_rethunk_at(void *addr) | |
1010 | { | |
1011 | if (!cpu_feature_enabled(X86_FEATURE_RETHUNK)) | |
1012 | return false; | |
1013 | if (x86_return_thunk != its_return_thunk) | |
1014 | return true; | |
1015 | ||
1016 | return !((unsigned long)addr & 0x20); | |
1017 | } | |
1018 | ||
15e67227 PZ |
1019 | /* |
1020 | * Rewrite the compiler generated return thunk tail-calls. | |
1021 | * | |
1022 | * For example, convert: | |
1023 | * | |
1024 | * JMP __x86_return_thunk | |
1025 | * | |
1026 | * into: | |
1027 | * | |
1028 | * RET | |
1029 | */ | |
1030 | static int patch_return(void *addr, struct insn *insn, u8 *bytes) | |
1031 | { | |
1032 | int i = 0; | |
1033 | ||
d2408e04 | 1034 | /* Patch the custom return thunks... */ |
a75bf27f | 1035 | if (cpu_wants_rethunk_at(addr)) { |
770ae1b7 PZ |
1036 | i = JMP32_INSN_SIZE; |
1037 | __text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i); | |
1038 | } else { | |
d2408e04 | 1039 | /* ... or patch them out if not needed. */ |
770ae1b7 PZ |
1040 | bytes[i++] = RET_INSN_OPCODE; |
1041 | } | |
15e67227 PZ |
1042 | |
1043 | for (; i < insn->length;) | |
1044 | bytes[i++] = INT3_INSN_OPCODE; | |
15e67227 PZ |
1045 | return i; |
1046 | } | |
1047 | ||
1d7e707a | 1048 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) |
15e67227 PZ |
1049 | { |
1050 | s32 *s; | |
1051 | ||
a75bf27f | 1052 | if (cpu_wants_rethunk()) |
aee9d30b | 1053 | static_call_force_reinit(); |
d2408e04 | 1054 | |
15e67227 | 1055 | for (s = start; s < end; s++) { |
ee88d363 | 1056 | void *dest = NULL, *addr = (void *)s + *s; |
15e67227 PZ |
1057 | struct insn insn; |
1058 | int len, ret; | |
1059 | u8 bytes[16]; | |
ee88d363 | 1060 | u8 op; |
15e67227 | 1061 | |
1d7e707a | 1062 | ret = insn_decode_kernel(&insn, addr); |
15e67227 PZ |
1063 | if (WARN_ON_ONCE(ret < 0)) |
1064 | continue; | |
1065 | ||
ee88d363 PZ |
1066 | op = insn.opcode.bytes[0]; |
1067 | if (op == JMP32_INSN_OPCODE) | |
1068 | dest = addr + insn.length + insn.immediate.value; | |
1069 | ||
1070 | if (__static_call_fixup(addr, op, dest) || | |
65cdf0d6 KC |
1071 | WARN_ONCE(dest != &__x86_return_thunk, |
1072 | "missing return thunk: %pS-%pS: %*ph", | |
1073 | addr, dest, 5, addr)) | |
15e67227 PZ |
1074 | continue; |
1075 | ||
6becb502 | 1076 | DPRINTK(RET, "return thunk at: %pS (%px) len: %d to: %pS", |
15e67227 PZ |
1077 | addr, addr, insn.length, |
1078 | addr + insn.length + insn.immediate.value); | |
1079 | ||
1080 | len = patch_return(addr, &insn, bytes); | |
1081 | if (len == insn.length) { | |
1d7e707a | 1082 | DUMP_BYTES(RET, ((u8*)addr), len, "%px: orig: ", addr); |
6becb502 | 1083 | DUMP_BYTES(RET, ((u8*)bytes), len, "%px: repl: ", addr); |
1d7e707a | 1084 | text_poke_early(addr, bytes, len); |
15e67227 PZ |
1085 | } |
1086 | } | |
1087 | } | |
5d703825 | 1088 | #else /* !CONFIG_MITIGATION_RETHUNK: */ |
1d7e707a | 1089 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } |
5d703825 | 1090 | #endif /* !CONFIG_MITIGATION_RETHUNK */ |
f43b9876 | 1091 | |
aefb2f2e | 1092 | #else /* !CONFIG_MITIGATION_RETPOLINE || !CONFIG_OBJTOOL */ |
75085009 | 1093 | |
1d7e707a MRM |
1094 | void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { } |
1095 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } | |
75085009 | 1096 | |
5d703825 | 1097 | #endif /* !CONFIG_MITIGATION_RETPOLINE || !CONFIG_OBJTOOL */ |
75085009 | 1098 | |
ed53a0d9 PZ |
1099 | #ifdef CONFIG_X86_KERNEL_IBT |
1100 | ||
72e213a7 PZ |
1101 | __noendbr bool is_endbr(u32 *val) |
1102 | { | |
1103 | u32 endbr; | |
1104 | ||
1105 | __get_kernel_nofault(&endbr, val, u32, Efault); | |
1106 | return __is_endbr(endbr); | |
1107 | ||
1108 | Efault: | |
1109 | return false; | |
1110 | } | |
1111 | ||
500a41ac PZ |
1112 | #ifdef CONFIG_FINEIBT |
1113 | ||
1114 | static __noendbr bool exact_endbr(u32 *val) | |
1115 | { | |
1116 | u32 endbr; | |
1117 | ||
1118 | __get_kernel_nofault(&endbr, val, u32, Efault); | |
1119 | return endbr == gen_endbr(); | |
1120 | ||
1121 | Efault: | |
1122 | return false; | |
1123 | } | |
1124 | ||
1125 | #endif | |
1126 | ||
1d7e707a | 1127 | static void poison_cfi(void *addr); |
9831c625 | 1128 | |
c4239a72 | 1129 | static void __init_or_module poison_endbr(void *addr) |
931ab636 | 1130 | { |
72e213a7 | 1131 | u32 poison = gen_endbr_poison(); |
931ab636 | 1132 | |
c4239a72 | 1133 | if (WARN_ON_ONCE(!is_endbr(addr))) |
931ab636 | 1134 | return; |
931ab636 | 1135 | |
6becb502 | 1136 | DPRINTK(ENDBR, "ENDBR at: %pS (%px)", addr, addr); |
931ab636 PZ |
1137 | |
1138 | /* | |
1139 | * When we have IBT, the lack of ENDBR will trigger #CP | |
1140 | */ | |
6becb502 PZ |
1141 | DUMP_BYTES(ENDBR, ((u8*)addr), 4, "%px: orig: ", addr); |
1142 | DUMP_BYTES(ENDBR, ((u8*)&poison), 4, "%px: repl: ", addr); | |
1d7e707a | 1143 | text_poke_early(addr, &poison, 4); |
931ab636 PZ |
1144 | } |
1145 | ||
ed53a0d9 PZ |
1146 | /* |
1147 | * Generated by: objtool --ibt | |
9831c625 PZ |
1148 | * |
1149 | * Seal the functions for indirect calls by clobbering the ENDBR instructions | |
1150 | * and the kCFI hash value. | |
ed53a0d9 | 1151 | */ |
1d7e707a | 1152 | void __init_or_module noinline apply_seal_endbr(s32 *start, s32 *end) |
ed53a0d9 PZ |
1153 | { |
1154 | s32 *s; | |
1155 | ||
1156 | for (s = start; s < end; s++) { | |
ed53a0d9 PZ |
1157 | void *addr = (void *)s + *s; |
1158 | ||
c4239a72 | 1159 | poison_endbr(addr); |
931ab636 | 1160 | if (IS_ENABLED(CONFIG_FINEIBT)) |
1d7e707a | 1161 | poison_cfi(addr - 16); |
931ab636 PZ |
1162 | } |
1163 | } | |
1164 | ||
5d703825 | 1165 | #else /* !CONFIG_X86_KERNEL_IBT: */ |
931ab636 | 1166 | |
1d7e707a | 1167 | void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { } |
931ab636 | 1168 | |
5d703825 | 1169 | #endif /* !CONFIG_X86_KERNEL_IBT */ |
931ab636 | 1170 | |
d6f635bc | 1171 | #ifdef CONFIG_CFI_AUTO_DEFAULT |
5d703825 | 1172 | # define __CFI_DEFAULT CFI_AUTO |
4f9087f1 | 1173 | #elif defined(CONFIG_CFI_CLANG) |
5d703825 | 1174 | # define __CFI_DEFAULT CFI_KCFI |
4f9087f1 | 1175 | #else |
5d703825 | 1176 | # define __CFI_DEFAULT CFI_OFF |
4f9087f1 | 1177 | #endif |
082c4c81 | 1178 | |
4f9087f1 | 1179 | enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT; |
73e8079b PZ |
1180 | |
1181 | #ifdef CONFIG_FINEIBT_BHI | |
0c92385d | 1182 | bool cfi_bhi __ro_after_init = false; |
73e8079b | 1183 | #endif |
4f9087f1 PZ |
1184 | |
1185 | #ifdef CONFIG_CFI_CLANG | |
2cd3e377 PZ |
1186 | u32 cfi_get_func_hash(void *func) |
1187 | { | |
1188 | u32 hash; | |
1189 | ||
1190 | func -= cfi_get_offset(); | |
1191 | switch (cfi_mode) { | |
1192 | case CFI_FINEIBT: | |
1193 | func += 7; | |
1194 | break; | |
1195 | case CFI_KCFI: | |
1196 | func += 1; | |
1197 | break; | |
1198 | default: | |
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | if (get_kernel_nofault(hash, func)) | |
1203 | return 0; | |
1204 | ||
1205 | return hash; | |
1206 | } | |
0c92385d PZ |
1207 | |
1208 | int cfi_get_func_arity(void *func) | |
1209 | { | |
1210 | bhi_thunk *target; | |
1211 | s32 disp; | |
1212 | ||
1213 | if (cfi_mode != CFI_FINEIBT && !cfi_bhi) | |
1214 | return 0; | |
1215 | ||
1216 | if (get_kernel_nofault(disp, func - 4)) | |
1217 | return 0; | |
1218 | ||
1219 | target = func + disp; | |
1220 | return target - __bhi_args; | |
1221 | } | |
4f9087f1 PZ |
1222 | #endif |
1223 | ||
1224 | #ifdef CONFIG_FINEIBT | |
082c4c81 | 1225 | |
0c3e806e PZ |
1226 | static bool cfi_rand __ro_after_init = true; |
1227 | static u32 cfi_seed __ro_after_init; | |
1228 | ||
1229 | /* | |
1230 | * Re-hash the CFI hash with a boot-time seed while making sure the result is | |
1231 | * not a valid ENDBR instruction. | |
1232 | */ | |
1233 | static u32 cfi_rehash(u32 hash) | |
1234 | { | |
1235 | hash ^= cfi_seed; | |
72e213a7 | 1236 | while (unlikely(__is_endbr(hash) || __is_endbr(-hash))) { |
0c3e806e PZ |
1237 | bool lsb = hash & 1; |
1238 | hash >>= 1; | |
1239 | if (lsb) | |
1240 | hash ^= 0x80200003; | |
1241 | } | |
1242 | return hash; | |
1243 | } | |
082c4c81 PZ |
1244 | |
1245 | static __init int cfi_parse_cmdline(char *str) | |
1246 | { | |
1247 | if (!str) | |
1248 | return -EINVAL; | |
1249 | ||
1250 | while (str) { | |
1251 | char *next = strchr(str, ','); | |
1252 | if (next) { | |
1253 | *next = 0; | |
1254 | next++; | |
1255 | } | |
1256 | ||
1257 | if (!strcmp(str, "auto")) { | |
d6f635bc | 1258 | cfi_mode = CFI_AUTO; |
082c4c81 PZ |
1259 | } else if (!strcmp(str, "off")) { |
1260 | cfi_mode = CFI_OFF; | |
0c3e806e | 1261 | cfi_rand = false; |
082c4c81 PZ |
1262 | } else if (!strcmp(str, "kcfi")) { |
1263 | cfi_mode = CFI_KCFI; | |
1264 | } else if (!strcmp(str, "fineibt")) { | |
1265 | cfi_mode = CFI_FINEIBT; | |
0c3e806e PZ |
1266 | } else if (!strcmp(str, "norand")) { |
1267 | cfi_rand = false; | |
9a54fb31 PZ |
1268 | } else if (!strcmp(str, "warn")) { |
1269 | pr_alert("CFI mismatch non-fatal!\n"); | |
1270 | cfi_warn = true; | |
97e59672 PZ |
1271 | } else if (!strcmp(str, "paranoid")) { |
1272 | if (cfi_mode == CFI_FINEIBT) { | |
1273 | cfi_paranoid = true; | |
1274 | } else { | |
1275 | pr_err("Ignoring paranoid; depends on fineibt.\n"); | |
1276 | } | |
0c92385d | 1277 | } else if (!strcmp(str, "bhi")) { |
73e8079b | 1278 | #ifdef CONFIG_FINEIBT_BHI |
0c92385d PZ |
1279 | if (cfi_mode == CFI_FINEIBT) { |
1280 | cfi_bhi = true; | |
1281 | } else { | |
1282 | pr_err("Ignoring bhi; depends on fineibt.\n"); | |
1283 | } | |
73e8079b PZ |
1284 | #else |
1285 | pr_err("Ignoring bhi; depends on FINEIBT_BHI=y.\n"); | |
1286 | #endif | |
082c4c81 PZ |
1287 | } else { |
1288 | pr_err("Ignoring unknown cfi option (%s).", str); | |
1289 | } | |
1290 | ||
1291 | str = next; | |
1292 | } | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | early_param("cfi", cfi_parse_cmdline); | |
1297 | ||
931ab636 PZ |
1298 | /* |
1299 | * kCFI FineIBT | |
1300 | * | |
1301 | * __cfi_\func: __cfi_\func: | |
1302 | * movl $0x12345678,%eax // 5 endbr64 // 4 | |
1303 | * nop subl $0x12345678,%r10d // 7 | |
06926c6c PZ |
1304 | * nop jne __cfi_\func+6 // 2 |
1305 | * nop nop3 // 3 | |
1306 | * nop | |
931ab636 PZ |
1307 | * nop |
1308 | * nop | |
1309 | * nop | |
1310 | * nop | |
1311 | * nop | |
1312 | * nop | |
1313 | * nop | |
1314 | * | |
1315 | * | |
1316 | * caller: caller: | |
1317 | * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6 | |
06926c6c | 1318 | * addl $-15(%r11),%r10d // 4 lea -0x10(%r11),%r11 // 4 |
931ab636 PZ |
1319 | * je 1f // 2 nop4 // 4 |
1320 | * ud2 // 2 | |
06926c6c | 1321 | * 1: cs call __x86_indirect_thunk_r11 // 6 call *%r11; nop3; // 6 |
931ab636 PZ |
1322 | * |
1323 | */ | |
1324 | ||
06926c6c PZ |
1325 | /* |
1326 | * <fineibt_preamble_start>: | |
1327 | * 0: f3 0f 1e fa endbr64 | |
1328 | * 4: 41 81 <ea> 78 56 34 12 sub $0x12345678, %r10d | |
1329 | * b: 75 f9 jne 6 <fineibt_preamble_start+0x6> | |
1330 | * d: 0f 1f 00 nopl (%rax) | |
1331 | * | |
1332 | * Note that the JNE target is the 0xEA byte inside the SUB, this decodes as | |
1333 | * (bad) on x86_64 and raises #UD. | |
1334 | */ | |
1335 | asm( ".pushsection .rodata \n" | |
1336 | "fineibt_preamble_start: \n" | |
1337 | " endbr64 \n" | |
1338 | " subl $0x12345678, %r10d \n" | |
0c92385d | 1339 | "fineibt_preamble_bhi: \n" |
06926c6c PZ |
1340 | " jne fineibt_preamble_start+6 \n" |
1341 | ASM_NOP3 | |
1342 | "fineibt_preamble_end: \n" | |
931ab636 PZ |
1343 | ".popsection\n" |
1344 | ); | |
1345 | ||
1346 | extern u8 fineibt_preamble_start[]; | |
0c92385d | 1347 | extern u8 fineibt_preamble_bhi[]; |
931ab636 PZ |
1348 | extern u8 fineibt_preamble_end[]; |
1349 | ||
1350 | #define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start) | |
0c92385d | 1351 | #define fineibt_preamble_bhi (fineibt_preamble_bhi - fineibt_preamble_start) |
06926c6c | 1352 | #define fineibt_preamble_ud 6 |
931ab636 PZ |
1353 | #define fineibt_preamble_hash 7 |
1354 | ||
06926c6c PZ |
1355 | /* |
1356 | * <fineibt_caller_start>: | |
1357 | * 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d | |
1358 | * 6: 4d 8d 5b f0 lea -0x10(%r11), %r11 | |
1359 | * a: 0f 1f 40 00 nopl 0x0(%rax) | |
1360 | */ | |
931ab636 PZ |
1361 | asm( ".pushsection .rodata \n" |
1362 | "fineibt_caller_start: \n" | |
1363 | " movl $0x12345678, %r10d \n" | |
06926c6c | 1364 | " lea -0x10(%r11), %r11 \n" |
931ab636 PZ |
1365 | ASM_NOP4 |
1366 | "fineibt_caller_end: \n" | |
1367 | ".popsection \n" | |
1368 | ); | |
1369 | ||
1370 | extern u8 fineibt_caller_start[]; | |
1371 | extern u8 fineibt_caller_end[]; | |
1372 | ||
1373 | #define fineibt_caller_size (fineibt_caller_end - fineibt_caller_start) | |
1374 | #define fineibt_caller_hash 2 | |
1375 | ||
1376 | #define fineibt_caller_jmp (fineibt_caller_size - 2) | |
1377 | ||
97e59672 PZ |
1378 | /* |
1379 | * Since FineIBT does hash validation on the callee side it is prone to | |
1380 | * circumvention attacks where a 'naked' ENDBR instruction exists that | |
1381 | * is not part of the fineibt_preamble sequence. | |
1382 | * | |
1383 | * Notably the x86 entry points must be ENDBR and equally cannot be | |
1384 | * fineibt_preamble. | |
1385 | * | |
1386 | * The fineibt_paranoid caller sequence adds additional caller side | |
1387 | * hash validation. This stops such circumvention attacks dead, but at the cost | |
1388 | * of adding a load. | |
1389 | * | |
1390 | * <fineibt_paranoid_start>: | |
1391 | * 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d | |
1392 | * 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d | |
1393 | * a: 4d 8d 5b <f0> lea -0x10(%r11), %r11 | |
1394 | * e: 75 fd jne d <fineibt_paranoid_start+0xd> | |
1395 | * 10: 41 ff d3 call *%r11 | |
1396 | * 13: 90 nop | |
1397 | * | |
1398 | * Notably LEA does not modify flags and can be reordered with the CMP, | |
1399 | * avoiding a dependency. Again, using a non-taken (backwards) branch | |
1400 | * for the failure case, abusing LEA's immediate 0xf0 as LOCK prefix for the | |
1401 | * Jcc.d8, causing #UD. | |
1402 | */ | |
1403 | asm( ".pushsection .rodata \n" | |
1404 | "fineibt_paranoid_start: \n" | |
1405 | " movl $0x12345678, %r10d \n" | |
1406 | " cmpl -9(%r11), %r10d \n" | |
1407 | " lea -0x10(%r11), %r11 \n" | |
1408 | " jne fineibt_paranoid_start+0xd \n" | |
1409 | "fineibt_paranoid_ind: \n" | |
1410 | " call *%r11 \n" | |
1411 | " nop \n" | |
1412 | "fineibt_paranoid_end: \n" | |
1413 | ".popsection \n" | |
1414 | ); | |
1415 | ||
1416 | extern u8 fineibt_paranoid_start[]; | |
1417 | extern u8 fineibt_paranoid_ind[]; | |
1418 | extern u8 fineibt_paranoid_end[]; | |
1419 | ||
1420 | #define fineibt_paranoid_size (fineibt_paranoid_end - fineibt_paranoid_start) | |
1421 | #define fineibt_paranoid_ind (fineibt_paranoid_ind - fineibt_paranoid_start) | |
1422 | #define fineibt_paranoid_ud 0xd | |
1423 | ||
0c92385d | 1424 | static u32 decode_preamble_hash(void *addr, int *reg) |
931ab636 PZ |
1425 | { |
1426 | u8 *p = addr; | |
1427 | ||
0c92385d PZ |
1428 | /* b8+reg 78 56 34 12 movl $0x12345678,\reg */ |
1429 | if (p[0] >= 0xb8 && p[0] < 0xc0) { | |
1430 | if (reg) | |
1431 | *reg = p[0] - 0xb8; | |
931ab636 | 1432 | return *(u32 *)(addr + 1); |
0c92385d | 1433 | } |
931ab636 PZ |
1434 | |
1435 | return 0; /* invalid hash value */ | |
1436 | } | |
1437 | ||
1438 | static u32 decode_caller_hash(void *addr) | |
1439 | { | |
1440 | u8 *p = addr; | |
1441 | ||
0c92385d | 1442 | /* 41 ba 88 a9 cb ed mov $(-0x12345678),%r10d */ |
931ab636 PZ |
1443 | if (p[0] == 0x41 && p[1] == 0xba) |
1444 | return -*(u32 *)(addr + 2); | |
1445 | ||
0c92385d | 1446 | /* e8 0c 88 a9 cb ed jmp.d8 +12 */ |
931ab636 PZ |
1447 | if (p[0] == JMP8_INSN_OPCODE && p[1] == fineibt_caller_jmp) |
1448 | return -*(u32 *)(addr + 2); | |
1449 | ||
1450 | return 0; /* invalid hash value */ | |
1451 | } | |
1452 | ||
1453 | /* .retpoline_sites */ | |
1d7e707a | 1454 | static int cfi_disable_callers(s32 *start, s32 *end) |
931ab636 PZ |
1455 | { |
1456 | /* | |
1457 | * Disable kCFI by patching in a JMP.d8, this leaves the hash immediate | |
1458 | * in tact for later usage. Also see decode_caller_hash() and | |
1459 | * cfi_rewrite_callers(). | |
1460 | */ | |
1461 | const u8 jmp[] = { JMP8_INSN_OPCODE, fineibt_caller_jmp }; | |
1462 | s32 *s; | |
ed53a0d9 | 1463 | |
931ab636 PZ |
1464 | for (s = start; s < end; s++) { |
1465 | void *addr = (void *)s + *s; | |
1466 | u32 hash; | |
1467 | ||
1468 | addr -= fineibt_caller_size; | |
1d7e707a | 1469 | hash = decode_caller_hash(addr); |
931ab636 | 1470 | if (!hash) /* nocfi callers */ |
ed53a0d9 PZ |
1471 | continue; |
1472 | ||
1d7e707a | 1473 | text_poke_early(addr, jmp, 2); |
931ab636 | 1474 | } |
ed53a0d9 | 1475 | |
931ab636 PZ |
1476 | return 0; |
1477 | } | |
1478 | ||
1d7e707a | 1479 | static int cfi_enable_callers(s32 *start, s32 *end) |
0c3e806e PZ |
1480 | { |
1481 | /* | |
1482 | * Re-enable kCFI, undo what cfi_disable_callers() did. | |
1483 | */ | |
1484 | const u8 mov[] = { 0x41, 0xba }; | |
1485 | s32 *s; | |
1486 | ||
1487 | for (s = start; s < end; s++) { | |
1488 | void *addr = (void *)s + *s; | |
1489 | u32 hash; | |
1490 | ||
1491 | addr -= fineibt_caller_size; | |
1d7e707a | 1492 | hash = decode_caller_hash(addr); |
0c3e806e | 1493 | if (!hash) /* nocfi callers */ |
ed53a0d9 PZ |
1494 | continue; |
1495 | ||
1d7e707a | 1496 | text_poke_early(addr, mov, 2); |
0c3e806e | 1497 | } |
ed53a0d9 | 1498 | |
0c3e806e PZ |
1499 | return 0; |
1500 | } | |
1501 | ||
931ab636 | 1502 | /* .cfi_sites */ |
1d7e707a | 1503 | static int cfi_rand_preamble(s32 *start, s32 *end) |
0c3e806e PZ |
1504 | { |
1505 | s32 *s; | |
1506 | ||
1507 | for (s = start; s < end; s++) { | |
1508 | void *addr = (void *)s + *s; | |
1509 | u32 hash; | |
1510 | ||
0c92385d | 1511 | hash = decode_preamble_hash(addr, NULL); |
0c3e806e PZ |
1512 | if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", |
1513 | addr, addr, 5, addr)) | |
1514 | return -EINVAL; | |
1515 | ||
1516 | hash = cfi_rehash(hash); | |
1d7e707a | 1517 | text_poke_early(addr + 1, &hash, 4); |
0c3e806e PZ |
1518 | } |
1519 | ||
1520 | return 0; | |
1521 | } | |
1522 | ||
dfebe736 PZ |
1523 | static void cfi_fineibt_bhi_preamble(void *addr, int arity) |
1524 | { | |
1525 | if (!arity) | |
1526 | return; | |
1527 | ||
1528 | if (!cfi_warn && arity == 1) { | |
1529 | /* | |
1530 | * Crazy scheme to allow arity-1 inline: | |
1531 | * | |
1532 | * __cfi_foo: | |
1533 | * 0: f3 0f 1e fa endbr64 | |
1534 | * 4: 41 81 <ea> 78 56 34 12 sub 0x12345678, %r10d | |
1535 | * b: 49 0f 45 fa cmovne %r10, %rdi | |
1536 | * f: 75 f5 jne __cfi_foo+6 | |
1537 | * 11: 0f 1f 00 nopl (%rax) | |
1538 | * | |
1539 | * Code that direct calls to foo()+0, decodes the tail end as: | |
1540 | * | |
1541 | * foo: | |
1542 | * 0: f5 cmc | |
1543 | * 1: 0f 1f 00 nopl (%rax) | |
1544 | * | |
1545 | * which clobbers CF, but does not affect anything ABI | |
1546 | * wise. | |
1547 | * | |
1548 | * Notably, this scheme is incompatible with permissive CFI | |
1549 | * because the CMOVcc is unconditional and RDI will have been | |
1550 | * clobbered. | |
1551 | */ | |
1552 | const u8 magic[9] = { | |
1553 | 0x49, 0x0f, 0x45, 0xfa, | |
1554 | 0x75, 0xf5, | |
1555 | BYTES_NOP3, | |
1556 | }; | |
1557 | ||
1558 | text_poke_early(addr + fineibt_preamble_bhi, magic, 9); | |
1559 | ||
1560 | return; | |
1561 | } | |
1562 | ||
1563 | text_poke_early(addr + fineibt_preamble_bhi, | |
1564 | text_gen_insn(CALL_INSN_OPCODE, | |
1565 | addr + fineibt_preamble_bhi, | |
1566 | __bhi_args[arity]), | |
1567 | CALL_INSN_SIZE); | |
1568 | } | |
1569 | ||
1d7e707a | 1570 | static int cfi_rewrite_preamble(s32 *start, s32 *end) |
931ab636 PZ |
1571 | { |
1572 | s32 *s; | |
1573 | ||
1574 | for (s = start; s < end; s++) { | |
1575 | void *addr = (void *)s + *s; | |
0c92385d | 1576 | int arity; |
931ab636 PZ |
1577 | u32 hash; |
1578 | ||
c4239a72 PZ |
1579 | /* |
1580 | * When the function doesn't start with ENDBR the compiler will | |
1581 | * have determined there are no indirect calls to it and we | |
1582 | * don't need no CFI either. | |
1583 | */ | |
1584 | if (!is_endbr(addr + 16)) | |
1585 | continue; | |
1586 | ||
0c92385d | 1587 | hash = decode_preamble_hash(addr, &arity); |
931ab636 PZ |
1588 | if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", |
1589 | addr, addr, 5, addr)) | |
1590 | return -EINVAL; | |
1591 | ||
1d7e707a MRM |
1592 | text_poke_early(addr, fineibt_preamble_start, fineibt_preamble_size); |
1593 | WARN_ON(*(u32 *)(addr + fineibt_preamble_hash) != 0x12345678); | |
1594 | text_poke_early(addr + fineibt_preamble_hash, &hash, 4); | |
0c92385d PZ |
1595 | |
1596 | WARN_ONCE(!IS_ENABLED(CONFIG_FINEIBT_BHI) && arity, | |
1597 | "kCFI preamble has wrong register at: %pS %*ph\n", | |
1598 | addr, 5, addr); | |
1599 | ||
dfebe736 PZ |
1600 | if (cfi_bhi) |
1601 | cfi_fineibt_bhi_preamble(addr, arity); | |
ed53a0d9 | 1602 | } |
931ab636 PZ |
1603 | |
1604 | return 0; | |
1605 | } | |
1606 | ||
1d7e707a | 1607 | static void cfi_rewrite_endbr(s32 *start, s32 *end) |
04505bbb PZ |
1608 | { |
1609 | s32 *s; | |
1610 | ||
1611 | for (s = start; s < end; s++) { | |
1612 | void *addr = (void *)s + *s; | |
1613 | ||
dfebe736 | 1614 | if (!exact_endbr(addr + 16)) |
c4239a72 PZ |
1615 | continue; |
1616 | ||
1617 | poison_endbr(addr + 16); | |
04505bbb PZ |
1618 | } |
1619 | } | |
1620 | ||
931ab636 | 1621 | /* .retpoline_sites */ |
1d7e707a | 1622 | static int cfi_rand_callers(s32 *start, s32 *end) |
0c3e806e PZ |
1623 | { |
1624 | s32 *s; | |
1625 | ||
1626 | for (s = start; s < end; s++) { | |
1627 | void *addr = (void *)s + *s; | |
1628 | u32 hash; | |
1629 | ||
1630 | addr -= fineibt_caller_size; | |
1d7e707a | 1631 | hash = decode_caller_hash(addr); |
0c3e806e PZ |
1632 | if (hash) { |
1633 | hash = -cfi_rehash(hash); | |
1d7e707a | 1634 | text_poke_early(addr + 2, &hash, 4); |
0c3e806e PZ |
1635 | } |
1636 | } | |
1637 | ||
1638 | return 0; | |
1639 | } | |
1640 | ||
e52c1dc7 PZ |
1641 | static int emit_paranoid_trampoline(void *addr, struct insn *insn, int reg, u8 *bytes) |
1642 | { | |
1643 | u8 *thunk = (void *)__x86_indirect_its_thunk_array[reg] - 2; | |
1644 | ||
1645 | #ifdef CONFIG_MITIGATION_ITS | |
1646 | u8 *tmp = its_allocate_thunk(reg); | |
1647 | if (tmp) | |
1648 | thunk = tmp; | |
1649 | #endif | |
1650 | ||
1651 | return __emit_trampoline(addr, insn, bytes, thunk, thunk); | |
1652 | } | |
1653 | ||
1d7e707a | 1654 | static int cfi_rewrite_callers(s32 *start, s32 *end) |
931ab636 PZ |
1655 | { |
1656 | s32 *s; | |
1657 | ||
97e59672 PZ |
1658 | BUG_ON(fineibt_paranoid_size != 20); |
1659 | ||
931ab636 PZ |
1660 | for (s = start; s < end; s++) { |
1661 | void *addr = (void *)s + *s; | |
97e59672 PZ |
1662 | struct insn insn; |
1663 | u8 bytes[20]; | |
931ab636 | 1664 | u32 hash; |
97e59672 PZ |
1665 | int ret; |
1666 | u8 op; | |
931ab636 PZ |
1667 | |
1668 | addr -= fineibt_caller_size; | |
1d7e707a | 1669 | hash = decode_caller_hash(addr); |
97e59672 PZ |
1670 | if (!hash) |
1671 | continue; | |
1672 | ||
1673 | if (!cfi_paranoid) { | |
1d7e707a MRM |
1674 | text_poke_early(addr, fineibt_caller_start, fineibt_caller_size); |
1675 | WARN_ON(*(u32 *)(addr + fineibt_caller_hash) != 0x12345678); | |
1676 | text_poke_early(addr + fineibt_caller_hash, &hash, 4); | |
97e59672 PZ |
1677 | /* rely on apply_retpolines() */ |
1678 | continue; | |
1679 | } | |
1680 | ||
1681 | /* cfi_paranoid */ | |
1682 | ret = insn_decode_kernel(&insn, addr + fineibt_caller_size); | |
1683 | if (WARN_ON_ONCE(ret < 0)) | |
1684 | continue; | |
1685 | ||
1686 | op = insn.opcode.bytes[0]; | |
1687 | if (op != CALL_INSN_OPCODE && op != JMP32_INSN_OPCODE) { | |
1688 | WARN_ON_ONCE(1); | |
1689 | continue; | |
931ab636 | 1690 | } |
97e59672 PZ |
1691 | |
1692 | memcpy(bytes, fineibt_paranoid_start, fineibt_paranoid_size); | |
1693 | memcpy(bytes + fineibt_caller_hash, &hash, 4); | |
1694 | ||
e52c1dc7 PZ |
1695 | if (cpu_wants_indirect_its_thunk_at((unsigned long)addr + fineibt_paranoid_ind, 11)) { |
1696 | emit_paranoid_trampoline(addr + fineibt_caller_size, | |
1697 | &insn, 11, bytes + fineibt_caller_size); | |
1698 | } else { | |
1699 | ret = emit_indirect(op, 11, bytes + fineibt_paranoid_ind); | |
1700 | if (WARN_ON_ONCE(ret != 3)) | |
1701 | continue; | |
1702 | } | |
97e59672 PZ |
1703 | |
1704 | text_poke_early(addr, bytes, fineibt_paranoid_size); | |
931ab636 PZ |
1705 | } |
1706 | ||
1707 | return 0; | |
1708 | } | |
1709 | ||
1710 | static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, | |
1d7e707a | 1711 | s32 *start_cfi, s32 *end_cfi, bool builtin) |
931ab636 PZ |
1712 | { |
1713 | int ret; | |
1714 | ||
1715 | if (WARN_ONCE(fineibt_preamble_size != 16, | |
1716 | "FineIBT preamble wrong size: %ld", fineibt_preamble_size)) | |
1717 | return; | |
1718 | ||
d6f635bc | 1719 | if (cfi_mode == CFI_AUTO) { |
082c4c81 | 1720 | cfi_mode = CFI_KCFI; |
97e59672 PZ |
1721 | if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT)) { |
1722 | /* | |
1723 | * FRED has much saner context on exception entry and | |
1724 | * is less easy to take advantage of. | |
1725 | */ | |
1726 | if (!cpu_feature_enabled(X86_FEATURE_FRED)) | |
1727 | cfi_paranoid = true; | |
082c4c81 | 1728 | cfi_mode = CFI_FINEIBT; |
97e59672 | 1729 | } |
082c4c81 PZ |
1730 | } |
1731 | ||
0c3e806e PZ |
1732 | /* |
1733 | * Rewrite the callers to not use the __cfi_ stubs, such that we might | |
1734 | * rewrite them. This disables all CFI. If this succeeds but any of the | |
1735 | * later stages fails, we're without CFI. | |
1736 | */ | |
1d7e707a | 1737 | ret = cfi_disable_callers(start_retpoline, end_retpoline); |
0c3e806e PZ |
1738 | if (ret) |
1739 | goto err; | |
1740 | ||
1741 | if (cfi_rand) { | |
4f9087f1 | 1742 | if (builtin) { |
0c3e806e | 1743 | cfi_seed = get_random_u32(); |
4f9087f1 | 1744 | cfi_bpf_hash = cfi_rehash(cfi_bpf_hash); |
e72d88d1 | 1745 | cfi_bpf_subprog_hash = cfi_rehash(cfi_bpf_subprog_hash); |
4f9087f1 | 1746 | } |
0c3e806e | 1747 | |
1d7e707a | 1748 | ret = cfi_rand_preamble(start_cfi, end_cfi); |
082c4c81 PZ |
1749 | if (ret) |
1750 | goto err; | |
1751 | ||
1d7e707a | 1752 | ret = cfi_rand_callers(start_retpoline, end_retpoline); |
0c3e806e PZ |
1753 | if (ret) |
1754 | goto err; | |
ed53a0d9 | 1755 | } |
0c3e806e PZ |
1756 | |
1757 | switch (cfi_mode) { | |
1758 | case CFI_OFF: | |
082c4c81 PZ |
1759 | if (builtin) |
1760 | pr_info("Disabling CFI\n"); | |
931ab636 PZ |
1761 | return; |
1762 | ||
082c4c81 | 1763 | case CFI_KCFI: |
1d7e707a | 1764 | ret = cfi_enable_callers(start_retpoline, end_retpoline); |
0c3e806e PZ |
1765 | if (ret) |
1766 | goto err; | |
1767 | ||
082c4c81 PZ |
1768 | if (builtin) |
1769 | pr_info("Using kCFI\n"); | |
1770 | return; | |
931ab636 | 1771 | |
082c4c81 | 1772 | case CFI_FINEIBT: |
04505bbb | 1773 | /* place the FineIBT preamble at func()-16 */ |
1d7e707a | 1774 | ret = cfi_rewrite_preamble(start_cfi, end_cfi); |
082c4c81 PZ |
1775 | if (ret) |
1776 | goto err; | |
931ab636 | 1777 | |
04505bbb | 1778 | /* rewrite the callers to target func()-16 */ |
1d7e707a | 1779 | ret = cfi_rewrite_callers(start_retpoline, end_retpoline); |
082c4c81 PZ |
1780 | if (ret) |
1781 | goto err; | |
931ab636 | 1782 | |
04505bbb | 1783 | /* now that nobody targets func()+0, remove ENDBR there */ |
1d7e707a | 1784 | cfi_rewrite_endbr(start_cfi, end_cfi); |
04505bbb | 1785 | |
97e59672 | 1786 | if (builtin) { |
0c92385d PZ |
1787 | pr_info("Using %sFineIBT%s CFI\n", |
1788 | cfi_paranoid ? "paranoid " : "", | |
1789 | cfi_bhi ? "+BHI" : ""); | |
97e59672 | 1790 | } |
082c4c81 | 1791 | return; |
931ab636 | 1792 | |
082c4c81 PZ |
1793 | default: |
1794 | break; | |
1795 | } | |
931ab636 PZ |
1796 | |
1797 | err: | |
1798 | pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n"); | |
ed53a0d9 PZ |
1799 | } |
1800 | ||
9831c625 PZ |
1801 | static inline void poison_hash(void *addr) |
1802 | { | |
1803 | *(u32 *)addr = 0; | |
1804 | } | |
1805 | ||
1d7e707a | 1806 | static void poison_cfi(void *addr) |
9831c625 | 1807 | { |
c4239a72 PZ |
1808 | /* |
1809 | * Compilers manage to be inconsistent with ENDBR vs __cfi prefixes, | |
1810 | * some (static) functions for which they can determine the address | |
1811 | * is never taken do not get a __cfi prefix, but *DO* get an ENDBR. | |
1812 | * | |
1813 | * As such, these functions will get sealed, but we need to be careful | |
1814 | * to not unconditionally scribble the previous function. | |
1815 | */ | |
9831c625 PZ |
1816 | switch (cfi_mode) { |
1817 | case CFI_FINEIBT: | |
c4239a72 PZ |
1818 | /* |
1819 | * FineIBT prefix should start with an ENDBR. | |
1820 | */ | |
1821 | if (!is_endbr(addr)) | |
1822 | break; | |
1823 | ||
9831c625 PZ |
1824 | /* |
1825 | * __cfi_\func: | |
1826 | * osp nopl (%rax) | |
1827 | * subl $0, %r10d | |
1828 | * jz 1f | |
1829 | * ud2 | |
1830 | * 1: nop | |
1831 | */ | |
c4239a72 | 1832 | poison_endbr(addr); |
1d7e707a | 1833 | poison_hash(addr + fineibt_preamble_hash); |
9831c625 PZ |
1834 | break; |
1835 | ||
1836 | case CFI_KCFI: | |
c4239a72 PZ |
1837 | /* |
1838 | * kCFI prefix should start with a valid hash. | |
1839 | */ | |
0c92385d | 1840 | if (!decode_preamble_hash(addr, NULL)) |
c4239a72 PZ |
1841 | break; |
1842 | ||
9831c625 PZ |
1843 | /* |
1844 | * __cfi_\func: | |
1845 | * movl $0, %eax | |
1846 | * .skip 11, 0x90 | |
1847 | */ | |
1d7e707a | 1848 | poison_hash(addr + 1); |
9831c625 PZ |
1849 | break; |
1850 | ||
1851 | default: | |
1852 | break; | |
1853 | } | |
1854 | } | |
1855 | ||
882b86fd | 1856 | /* |
06926c6c PZ |
1857 | * When regs->ip points to a 0xEA byte in the FineIBT preamble, |
1858 | * return true and fill out target and type. | |
882b86fd PZ |
1859 | * |
1860 | * We check the preamble by checking for the ENDBR instruction relative to the | |
06926c6c | 1861 | * 0xEA instruction. |
882b86fd | 1862 | */ |
97e59672 | 1863 | static bool decode_fineibt_preamble(struct pt_regs *regs, unsigned long *target, u32 *type) |
882b86fd | 1864 | { |
06926c6c | 1865 | unsigned long addr = regs->ip - fineibt_preamble_ud; |
500a41ac | 1866 | u32 hash; |
882b86fd | 1867 | |
500a41ac | 1868 | if (!exact_endbr((void *)addr)) |
882b86fd PZ |
1869 | return false; |
1870 | ||
1871 | *target = addr + fineibt_preamble_size; | |
1872 | ||
1873 | __get_kernel_nofault(&hash, addr + fineibt_preamble_hash, u32, Efault); | |
1874 | *type = (u32)regs->r10 + hash; | |
1875 | ||
06926c6c PZ |
1876 | /* |
1877 | * Since regs->ip points to the middle of an instruction; it cannot | |
1878 | * continue with the normal fixup. | |
1879 | */ | |
1880 | regs->ip = *target; | |
1881 | ||
882b86fd PZ |
1882 | return true; |
1883 | ||
1884 | Efault: | |
1885 | return false; | |
1886 | } | |
1887 | ||
0c92385d PZ |
1888 | /* |
1889 | * regs->ip points to one of the UD2 in __bhi_args[]. | |
1890 | */ | |
1891 | static bool decode_fineibt_bhi(struct pt_regs *regs, unsigned long *target, u32 *type) | |
1892 | { | |
1893 | unsigned long addr; | |
1894 | u32 hash; | |
1895 | ||
1896 | if (!cfi_bhi) | |
1897 | return false; | |
1898 | ||
1899 | if (regs->ip < (unsigned long)__bhi_args || | |
1900 | regs->ip >= (unsigned long)__bhi_args_end) | |
1901 | return false; | |
1902 | ||
1903 | /* | |
1904 | * Fetch the return address from the stack, this points to the | |
1905 | * FineIBT preamble. Since the CALL instruction is in the 5 last | |
1906 | * bytes of the preamble, the return address is in fact the target | |
1907 | * address. | |
1908 | */ | |
1909 | __get_kernel_nofault(&addr, regs->sp, unsigned long, Efault); | |
1910 | *target = addr; | |
1911 | ||
1912 | addr -= fineibt_preamble_size; | |
1913 | if (!exact_endbr((void *)addr)) | |
1914 | return false; | |
1915 | ||
1916 | __get_kernel_nofault(&hash, addr + fineibt_preamble_hash, u32, Efault); | |
1917 | *type = (u32)regs->r10 + hash; | |
1918 | ||
1919 | /* | |
1920 | * The UD2 sites are constructed with a RET immediately following, | |
1921 | * as such the non-fatal case can use the regular fixup. | |
1922 | */ | |
1923 | return true; | |
1924 | ||
1925 | Efault: | |
1926 | return false; | |
1927 | } | |
1928 | ||
e52c1dc7 PZ |
1929 | static bool is_paranoid_thunk(unsigned long addr) |
1930 | { | |
1931 | u32 thunk; | |
1932 | ||
1933 | __get_kernel_nofault(&thunk, (u32 *)addr, u32, Efault); | |
1934 | return (thunk & 0x00FFFFFF) == 0xfd75ea; | |
1935 | ||
1936 | Efault: | |
1937 | return false; | |
1938 | } | |
1939 | ||
97e59672 PZ |
1940 | /* |
1941 | * regs->ip points to a LOCK Jcc.d8 instruction from the fineibt_paranoid_start[] | |
e52c1dc7 PZ |
1942 | * sequence, or to an invalid instruction (0xea) + Jcc.d8 for cfi_paranoid + ITS |
1943 | * thunk. | |
97e59672 PZ |
1944 | */ |
1945 | static bool decode_fineibt_paranoid(struct pt_regs *regs, unsigned long *target, u32 *type) | |
1946 | { | |
1947 | unsigned long addr = regs->ip - fineibt_paranoid_ud; | |
97e59672 | 1948 | |
e52c1dc7 | 1949 | if (!cfi_paranoid) |
97e59672 PZ |
1950 | return false; |
1951 | ||
e52c1dc7 PZ |
1952 | if (is_cfi_trap(addr + fineibt_caller_size - LEN_UD2)) { |
1953 | *target = regs->r11 + fineibt_preamble_size; | |
1954 | *type = regs->r10; | |
1955 | ||
1956 | /* | |
1957 | * Since the trapping instruction is the exact, but LOCK prefixed, | |
1958 | * Jcc.d8 that got us here, the normal fixup will work. | |
1959 | */ | |
1960 | return true; | |
1961 | } | |
97e59672 PZ |
1962 | |
1963 | /* | |
e52c1dc7 PZ |
1964 | * The cfi_paranoid + ITS thunk combination results in: |
1965 | * | |
1966 | * 0: 41 ba 78 56 34 12 mov $0x12345678, %r10d | |
1967 | * 6: 45 3b 53 f7 cmp -0x9(%r11), %r10d | |
1968 | * a: 4d 8d 5b f0 lea -0x10(%r11), %r11 | |
1969 | * e: 2e e8 XX XX XX XX cs call __x86_indirect_paranoid_thunk_r11 | |
1970 | * | |
1971 | * Where the paranoid_thunk looks like: | |
1972 | * | |
1973 | * 1d: <ea> (bad) | |
1974 | * __x86_indirect_paranoid_thunk_r11: | |
1975 | * 1e: 75 fd jne 1d | |
1976 | * __x86_indirect_its_thunk_r11: | |
1977 | * 20: 41 ff eb jmp *%r11 | |
1978 | * 23: cc int3 | |
1979 | * | |
97e59672 | 1980 | */ |
e52c1dc7 PZ |
1981 | if (is_paranoid_thunk(regs->ip)) { |
1982 | *target = regs->r11 + fineibt_preamble_size; | |
1983 | *type = regs->r10; | |
1984 | ||
1985 | regs->ip = *target; | |
1986 | return true; | |
1987 | } | |
97e59672 | 1988 | |
97e59672 PZ |
1989 | return false; |
1990 | } | |
1991 | ||
1992 | bool decode_fineibt_insn(struct pt_regs *regs, unsigned long *target, u32 *type) | |
1993 | { | |
1994 | if (decode_fineibt_paranoid(regs, target, type)) | |
1995 | return true; | |
1996 | ||
0c92385d PZ |
1997 | if (decode_fineibt_bhi(regs, target, type)) |
1998 | return true; | |
1999 | ||
97e59672 PZ |
2000 | return decode_fineibt_preamble(regs, target, type); |
2001 | } | |
2002 | ||
5d703825 | 2003 | #else /* !CONFIG_FINEIBT: */ |
ed53a0d9 | 2004 | |
931ab636 | 2005 | static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, |
1d7e707a | 2006 | s32 *start_cfi, s32 *end_cfi, bool builtin) |
931ab636 PZ |
2007 | { |
2008 | } | |
ed53a0d9 | 2009 | |
535d0ae3 | 2010 | #ifdef CONFIG_X86_KERNEL_IBT |
1d7e707a | 2011 | static void poison_cfi(void *addr) { } |
535d0ae3 | 2012 | #endif |
9831c625 | 2013 | |
5d703825 | 2014 | #endif /* !CONFIG_FINEIBT */ |
931ab636 PZ |
2015 | |
2016 | void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, | |
1d7e707a | 2017 | s32 *start_cfi, s32 *end_cfi) |
931ab636 PZ |
2018 | { |
2019 | return __apply_fineibt(start_retpoline, end_retpoline, | |
1d7e707a MRM |
2020 | start_cfi, end_cfi, |
2021 | /* .builtin = */ false); | |
931ab636 | 2022 | } |
ed53a0d9 | 2023 | |
8ec4d41f | 2024 | #ifdef CONFIG_SMP |
5967ed87 JB |
2025 | static void alternatives_smp_lock(const s32 *start, const s32 *end, |
2026 | u8 *text, u8 *text_end) | |
9a0b5817 | 2027 | { |
5967ed87 | 2028 | const s32 *poff; |
9a0b5817 | 2029 | |
5967ed87 JB |
2030 | for (poff = start; poff < end; poff++) { |
2031 | u8 *ptr = (u8 *)poff + *poff; | |
2032 | ||
2033 | if (!*poff || ptr < text || ptr >= text_end) | |
9a0b5817 | 2034 | continue; |
f88f07e0 | 2035 | /* turn DS segment override prefix into lock prefix */ |
d9c5841e PA |
2036 | if (*ptr == 0x3e) |
2037 | text_poke(ptr, ((unsigned char []){0xf0}), 1); | |
4b8073e4 | 2038 | } |
9a0b5817 GH |
2039 | } |
2040 | ||
5967ed87 JB |
2041 | static void alternatives_smp_unlock(const s32 *start, const s32 *end, |
2042 | u8 *text, u8 *text_end) | |
9a0b5817 | 2043 | { |
5967ed87 | 2044 | const s32 *poff; |
9a0b5817 | 2045 | |
5967ed87 JB |
2046 | for (poff = start; poff < end; poff++) { |
2047 | u8 *ptr = (u8 *)poff + *poff; | |
2048 | ||
2049 | if (!*poff || ptr < text || ptr >= text_end) | |
9a0b5817 | 2050 | continue; |
f88f07e0 | 2051 | /* turn lock prefix into DS segment override prefix */ |
d9c5841e PA |
2052 | if (*ptr == 0xf0) |
2053 | text_poke(ptr, ((unsigned char []){0x3E}), 1); | |
4b8073e4 | 2054 | } |
9a0b5817 GH |
2055 | } |
2056 | ||
2057 | struct smp_alt_module { | |
2058 | /* what is this ??? */ | |
2059 | struct module *mod; | |
2060 | char *name; | |
2061 | ||
2062 | /* ptrs to lock prefixes */ | |
5967ed87 JB |
2063 | const s32 *locks; |
2064 | const s32 *locks_end; | |
9a0b5817 GH |
2065 | |
2066 | /* .text segment, needed to avoid patching init code ;) */ | |
2067 | u8 *text; | |
2068 | u8 *text_end; | |
2069 | ||
2070 | struct list_head next; | |
2071 | }; | |
2072 | static LIST_HEAD(smp_alt_modules); | |
e846d139 | 2073 | static bool uniproc_patched = false; /* protected by text_mutex */ |
9a0b5817 | 2074 | |
8b5a10fc JB |
2075 | void __init_or_module alternatives_smp_module_add(struct module *mod, |
2076 | char *name, | |
2077 | void *locks, void *locks_end, | |
2078 | void *text, void *text_end) | |
9a0b5817 GH |
2079 | { |
2080 | struct smp_alt_module *smp; | |
9a0b5817 | 2081 | |
e846d139 | 2082 | mutex_lock(&text_mutex); |
816afe4f RR |
2083 | if (!uniproc_patched) |
2084 | goto unlock; | |
b7fb4af0 | 2085 | |
816afe4f RR |
2086 | if (num_possible_cpus() == 1) |
2087 | /* Don't bother remembering, we'll never have to undo it. */ | |
2088 | goto smp_unlock; | |
9a0b5817 GH |
2089 | |
2090 | smp = kzalloc(sizeof(*smp), GFP_KERNEL); | |
2091 | if (NULL == smp) | |
816afe4f RR |
2092 | /* we'll run the (safe but slow) SMP code then ... */ |
2093 | goto unlock; | |
9a0b5817 GH |
2094 | |
2095 | smp->mod = mod; | |
2096 | smp->name = name; | |
2097 | smp->locks = locks; | |
2098 | smp->locks_end = locks_end; | |
2099 | smp->text = text; | |
2100 | smp->text_end = text_end; | |
6becb502 | 2101 | DPRINTK(SMP, "locks %p -> %p, text %p -> %p, name %s\n", |
db477a33 | 2102 | smp->locks, smp->locks_end, |
9a0b5817 GH |
2103 | smp->text, smp->text_end, smp->name); |
2104 | ||
9a0b5817 | 2105 | list_add_tail(&smp->next, &smp_alt_modules); |
816afe4f RR |
2106 | smp_unlock: |
2107 | alternatives_smp_unlock(locks, locks_end, text, text_end); | |
2108 | unlock: | |
e846d139 | 2109 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
2110 | } |
2111 | ||
8b5a10fc | 2112 | void __init_or_module alternatives_smp_module_del(struct module *mod) |
9a0b5817 GH |
2113 | { |
2114 | struct smp_alt_module *item; | |
9a0b5817 | 2115 | |
e846d139 | 2116 | mutex_lock(&text_mutex); |
9a0b5817 GH |
2117 | list_for_each_entry(item, &smp_alt_modules, next) { |
2118 | if (mod != item->mod) | |
2119 | continue; | |
2120 | list_del(&item->next); | |
9a0b5817 | 2121 | kfree(item); |
816afe4f | 2122 | break; |
9a0b5817 | 2123 | } |
e846d139 | 2124 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
2125 | } |
2126 | ||
816afe4f | 2127 | void alternatives_enable_smp(void) |
9a0b5817 GH |
2128 | { |
2129 | struct smp_alt_module *mod; | |
9a0b5817 | 2130 | |
816afe4f RR |
2131 | /* Why bother if there are no other CPUs? */ |
2132 | BUG_ON(num_possible_cpus() == 1); | |
9a0b5817 | 2133 | |
e846d139 | 2134 | mutex_lock(&text_mutex); |
ca74a6f8 | 2135 | |
816afe4f | 2136 | if (uniproc_patched) { |
c767a54b | 2137 | pr_info("switching to SMP code\n"); |
816afe4f | 2138 | BUG_ON(num_online_cpus() != 1); |
53756d37 JF |
2139 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP); |
2140 | clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP); | |
9a0b5817 GH |
2141 | list_for_each_entry(mod, &smp_alt_modules, next) |
2142 | alternatives_smp_lock(mod->locks, mod->locks_end, | |
2143 | mod->text, mod->text_end); | |
816afe4f | 2144 | uniproc_patched = false; |
9a0b5817 | 2145 | } |
e846d139 | 2146 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
2147 | } |
2148 | ||
e846d139 ZC |
2149 | /* |
2150 | * Return 1 if the address range is reserved for SMP-alternatives. | |
2151 | * Must hold text_mutex. | |
2152 | */ | |
2cfa1978 MH |
2153 | int alternatives_text_reserved(void *start, void *end) |
2154 | { | |
2155 | struct smp_alt_module *mod; | |
5967ed87 | 2156 | const s32 *poff; |
076dc4a6 MH |
2157 | u8 *text_start = start; |
2158 | u8 *text_end = end; | |
2cfa1978 | 2159 | |
e846d139 ZC |
2160 | lockdep_assert_held(&text_mutex); |
2161 | ||
2cfa1978 | 2162 | list_for_each_entry(mod, &smp_alt_modules, next) { |
076dc4a6 | 2163 | if (mod->text > text_end || mod->text_end < text_start) |
2cfa1978 | 2164 | continue; |
5967ed87 JB |
2165 | for (poff = mod->locks; poff < mod->locks_end; poff++) { |
2166 | const u8 *ptr = (const u8 *)poff + *poff; | |
2167 | ||
2168 | if (text_start <= ptr && text_end > ptr) | |
2cfa1978 | 2169 | return 1; |
5967ed87 | 2170 | } |
2cfa1978 MH |
2171 | } |
2172 | ||
2173 | return 0; | |
2174 | } | |
48c7a250 | 2175 | #endif /* CONFIG_SMP */ |
8ec4d41f | 2176 | |
7457c0da PZ |
2177 | /* |
2178 | * Self-test for the INT3 based CALL emulation code. | |
2179 | * | |
2180 | * This exercises int3_emulate_call() to make sure INT3 pt_regs are set up | |
2181 | * properly and that there is a stack gap between the INT3 frame and the | |
2182 | * previous context. Without this gap doing a virtual PUSH on the interrupted | |
2183 | * stack would corrupt the INT3 IRET frame. | |
2184 | * | |
2185 | * See entry_{32,64}.S for more details. | |
2186 | */ | |
ecc60610 PZ |
2187 | |
2188 | /* | |
2189 | * We define the int3_magic() function in assembly to control the calling | |
2190 | * convention such that we can 'call' it from assembly. | |
2191 | */ | |
2192 | ||
2193 | extern void int3_magic(unsigned int *ptr); /* defined in asm */ | |
2194 | ||
2195 | asm ( | |
2196 | " .pushsection .init.text, \"ax\", @progbits\n" | |
2197 | " .type int3_magic, @function\n" | |
2198 | "int3_magic:\n" | |
3e3f0695 | 2199 | ANNOTATE_NOENDBR |
ecc60610 | 2200 | " movl $1, (%" _ASM_ARG1 ")\n" |
b17c2baa | 2201 | ASM_RET |
ecc60610 PZ |
2202 | " .size int3_magic, .-int3_magic\n" |
2203 | " .popsection\n" | |
2204 | ); | |
7457c0da | 2205 | |
99c95c5d | 2206 | extern void int3_selftest_ip(void); /* defined in asm below */ |
7457c0da PZ |
2207 | |
2208 | static int __init | |
2209 | int3_exception_notify(struct notifier_block *self, unsigned long val, void *data) | |
2210 | { | |
3e3f0695 | 2211 | unsigned long selftest = (unsigned long)&int3_selftest_ip; |
7457c0da PZ |
2212 | struct die_args *args = data; |
2213 | struct pt_regs *regs = args->regs; | |
2214 | ||
3e3f0695 PZ |
2215 | OPTIMIZER_HIDE_VAR(selftest); |
2216 | ||
7457c0da PZ |
2217 | if (!regs || user_mode(regs)) |
2218 | return NOTIFY_DONE; | |
2219 | ||
2220 | if (val != DIE_INT3) | |
2221 | return NOTIFY_DONE; | |
2222 | ||
3e3f0695 | 2223 | if (regs->ip - INT3_INSN_SIZE != selftest) |
7457c0da PZ |
2224 | return NOTIFY_DONE; |
2225 | ||
2226 | int3_emulate_call(regs, (unsigned long)&int3_magic); | |
2227 | return NOTIFY_STOP; | |
2228 | } | |
2229 | ||
99c95c5d PZ |
2230 | /* Must be noinline to ensure uniqueness of int3_selftest_ip. */ |
2231 | static noinline void __init int3_selftest(void) | |
7457c0da PZ |
2232 | { |
2233 | static __initdata struct notifier_block int3_exception_nb = { | |
2234 | .notifier_call = int3_exception_notify, | |
2235 | .priority = INT_MAX-1, /* last */ | |
2236 | }; | |
2237 | unsigned int val = 0; | |
2238 | ||
2239 | BUG_ON(register_die_notifier(&int3_exception_nb)); | |
2240 | ||
2241 | /* | |
2242 | * Basically: int3_magic(&val); but really complicated :-) | |
2243 | * | |
99c95c5d PZ |
2244 | * INT3 padded with NOP to CALL_INSN_SIZE. The int3_exception_nb |
2245 | * notifier above will emulate CALL for us. | |
7457c0da | 2246 | */ |
3e3f0695 PZ |
2247 | asm volatile ("int3_selftest_ip:\n\t" |
2248 | ANNOTATE_NOENDBR | |
2249 | " int3; nop; nop; nop; nop\n\t" | |
ecc60610 PZ |
2250 | : ASM_CALL_CONSTRAINT |
2251 | : __ASM_SEL_RAW(a, D) (&val) | |
2252 | : "memory"); | |
7457c0da PZ |
2253 | |
2254 | BUG_ON(val != 1); | |
2255 | ||
2256 | unregister_die_notifier(&int3_exception_nb); | |
2257 | } | |
2258 | ||
270a69c4 PZ |
2259 | static __initdata int __alt_reloc_selftest_addr; |
2260 | ||
1a3e4b4d | 2261 | extern void __init __alt_reloc_selftest(void *arg); |
270a69c4 PZ |
2262 | __visible noinline void __init __alt_reloc_selftest(void *arg) |
2263 | { | |
2264 | WARN_ON(arg != &__alt_reloc_selftest_addr); | |
2265 | } | |
2266 | ||
2267 | static noinline void __init alt_reloc_selftest(void) | |
2268 | { | |
2269 | /* | |
023f42dd | 2270 | * Tests text_poke_apply_relocation(). |
270a69c4 PZ |
2271 | * |
2272 | * This has a relative immediate (CALL) in a place other than the first | |
2273 | * instruction and additionally on x86_64 we get a RIP-relative LEA: | |
2274 | * | |
2275 | * lea 0x0(%rip),%rdi # 5d0: R_X86_64_PC32 .init.data+0x5566c | |
2276 | * call +0 # 5d5: R_X86_64_PLT32 __alt_reloc_selftest-0x4 | |
2277 | * | |
2278 | * Getting this wrong will either crash and burn or tickle the WARN | |
2279 | * above. | |
2280 | */ | |
2281 | asm_inline volatile ( | |
2282 | ALTERNATIVE("", "lea %[mem], %%" _ASM_ARG1 "; call __alt_reloc_selftest;", X86_FEATURE_ALWAYS) | |
0d3db1f1 | 2283 | : ASM_CALL_CONSTRAINT |
270a69c4 PZ |
2284 | : [mem] "m" (__alt_reloc_selftest_addr) |
2285 | : _ASM_ARG1 | |
2286 | ); | |
2287 | } | |
2288 | ||
9a0b5817 GH |
2289 | void __init alternative_instructions(void) |
2290 | { | |
ebebe307 PG |
2291 | u64 ibt; |
2292 | ||
7457c0da PZ |
2293 | int3_selftest(); |
2294 | ||
2295 | /* | |
2296 | * The patching is not fully atomic, so try to avoid local | |
2297 | * interruptions that might execute the to be patched code. | |
2298 | * Other CPUs are not running. | |
2299 | */ | |
8f4e956b | 2300 | stop_nmi(); |
123aa76e AK |
2301 | |
2302 | /* | |
2303 | * Don't stop machine check exceptions while patching. | |
2304 | * MCEs only happen when something got corrupted and in this | |
2305 | * case we must do something about the corruption. | |
32b1cbe3 | 2306 | * Ignoring it is worse than an unlikely patching race. |
123aa76e AK |
2307 | * Also machine checks tend to be broadcast and if one CPU |
2308 | * goes into machine check the others follow quickly, so we don't | |
2309 | * expect a machine check to cause undue problems during to code | |
2310 | * patching. | |
2311 | */ | |
8f4e956b | 2312 | |
4e629211 | 2313 | /* |
f7af6977 JG |
2314 | * Make sure to set (artificial) features depending on used paravirt |
2315 | * functions which can later influence alternative patching. | |
4e629211 JG |
2316 | */ |
2317 | paravirt_set_cap(); | |
2318 | ||
ebebe307 PG |
2319 | /* Keep CET-IBT disabled until caller/callee are patched */ |
2320 | ibt = ibt_save(/*disable*/ true); | |
2321 | ||
931ab636 | 2322 | __apply_fineibt(__retpoline_sites, __retpoline_sites_end, |
1d7e707a | 2323 | __cfi_sites, __cfi_sites_end, true); |
931ab636 | 2324 | |
75085009 PZ |
2325 | /* |
2326 | * Rewrite the retpolines, must be done before alternatives since | |
2327 | * those can rewrite the retpoline thunks. | |
2328 | */ | |
1d7e707a MRM |
2329 | apply_retpolines(__retpoline_sites, __retpoline_sites_end); |
2330 | apply_returns(__return_sites, __return_sites_end); | |
75085009 | 2331 | |
a82b2645 PZI |
2332 | its_fini_core(); |
2333 | ||
e81dc127 | 2334 | /* |
ab9fea59 PZ |
2335 | * Adjust all CALL instructions to point to func()-10, including |
2336 | * those in .altinstr_replacement. | |
e81dc127 TG |
2337 | */ |
2338 | callthunks_patch_builtin_calls(); | |
2339 | ||
ab9fea59 PZ |
2340 | apply_alternatives(__alt_instructions, __alt_instructions_end); |
2341 | ||
be0fffa5 PZ |
2342 | /* |
2343 | * Seal all functions that do not have their address taken. | |
2344 | */ | |
1d7e707a | 2345 | apply_seal_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end); |
ed53a0d9 | 2346 | |
ebebe307 PG |
2347 | ibt_restore(ibt); |
2348 | ||
8ec4d41f | 2349 | #ifdef CONFIG_SMP |
816afe4f RR |
2350 | /* Patch to UP if other cpus not imminent. */ |
2351 | if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) { | |
2352 | uniproc_patched = true; | |
9a0b5817 GH |
2353 | alternatives_smp_module_add(NULL, "core kernel", |
2354 | __smp_locks, __smp_locks_end, | |
2355 | _text, _etext); | |
9a0b5817 | 2356 | } |
8f4e956b | 2357 | |
7457c0da | 2358 | if (!uniproc_patched || num_possible_cpus() == 1) { |
f68fd5f4 FW |
2359 | free_init_pages("SMP alternatives", |
2360 | (unsigned long)__smp_locks, | |
2361 | (unsigned long)__smp_locks_end); | |
7457c0da | 2362 | } |
816afe4f RR |
2363 | #endif |
2364 | ||
8f4e956b | 2365 | restart_nmi(); |
5e907bb0 | 2366 | alternatives_patched = 1; |
270a69c4 PZ |
2367 | |
2368 | alt_reloc_selftest(); | |
9a0b5817 | 2369 | } |
19d36ccd | 2370 | |
e587cadd MD |
2371 | /** |
2372 | * text_poke_early - Update instructions on a live kernel at boot time | |
2373 | * @addr: address to modify | |
2374 | * @opcode: source of the copy | |
2375 | * @len: length to copy | |
2376 | * | |
19d36ccd AK |
2377 | * When you use this code to patch more than one byte of an instruction |
2378 | * you need to make sure that other CPUs cannot execute this code in parallel. | |
e587cadd | 2379 | * Also no thread must be currently preempted in the middle of these |
32b1cbe3 MA |
2380 | * instructions. And on the local CPU you need to be protected against NMI or |
2381 | * MCE handlers seeing an inconsistent instruction while you patch. | |
19d36ccd | 2382 | */ |
0a203df5 NA |
2383 | void __init_or_module text_poke_early(void *addr, const void *opcode, |
2384 | size_t len) | |
19d36ccd | 2385 | { |
e587cadd | 2386 | unsigned long flags; |
f2c65fb3 NA |
2387 | |
2388 | if (boot_cpu_has(X86_FEATURE_NX) && | |
2389 | is_module_text_address((unsigned long)addr)) { | |
2390 | /* | |
2391 | * Modules text is marked initially as non-executable, so the | |
2392 | * code cannot be running and speculative code-fetches are | |
2393 | * prevented. Just change the code. | |
2394 | */ | |
2395 | memcpy(addr, opcode, len); | |
2396 | } else { | |
2397 | local_irq_save(flags); | |
2398 | memcpy(addr, opcode, len); | |
f2c65fb3 | 2399 | sync_core(); |
3ea1704a | 2400 | local_irq_restore(flags); |
f2c65fb3 NA |
2401 | |
2402 | /* | |
2403 | * Could also do a CLFLUSH here to speed up CPU recovery; but | |
2404 | * that causes hangs on some VIA CPUs. | |
2405 | */ | |
2406 | } | |
e587cadd MD |
2407 | } |
2408 | ||
a5c832e0 | 2409 | __ro_after_init struct mm_struct *text_poke_mm; |
da364fc5 | 2410 | __ro_after_init unsigned long text_poke_mm_addr; |
209954cb | 2411 | |
aadd1b67 SL |
2412 | static void text_poke_memcpy(void *dst, const void *src, size_t len) |
2413 | { | |
2414 | memcpy(dst, src, len); | |
2415 | } | |
2416 | ||
2417 | static void text_poke_memset(void *dst, const void *src, size_t len) | |
2418 | { | |
2419 | int c = *(const int *)src; | |
2420 | ||
2421 | memset(dst, c, len); | |
2422 | } | |
2423 | ||
2424 | typedef void text_poke_f(void *dst, const void *src, size_t len); | |
2425 | ||
2426 | static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t len) | |
e587cadd | 2427 | { |
b3fd8e83 NA |
2428 | bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE; |
2429 | struct page *pages[2] = {NULL}; | |
f5afa2e8 | 2430 | struct mm_struct *prev_mm; |
78ff7fae | 2431 | unsigned long flags; |
b3fd8e83 NA |
2432 | pte_t pte, *ptep; |
2433 | spinlock_t *ptl; | |
2434 | pgprot_t pgprot; | |
e587cadd | 2435 | |
6fffacb3 | 2436 | /* |
b3fd8e83 NA |
2437 | * While boot memory allocator is running we cannot use struct pages as |
2438 | * they are not yet initialized. There is no way to recover. | |
6fffacb3 PT |
2439 | */ |
2440 | BUG_ON(!after_bootmem); | |
2441 | ||
b7b66baa MD |
2442 | if (!core_kernel_text((unsigned long)addr)) { |
2443 | pages[0] = vmalloc_to_page(addr); | |
b3fd8e83 NA |
2444 | if (cross_page_boundary) |
2445 | pages[1] = vmalloc_to_page(addr + PAGE_SIZE); | |
15a601eb | 2446 | } else { |
b7b66baa | 2447 | pages[0] = virt_to_page(addr); |
00c6b2d5 | 2448 | WARN_ON(!PageReserved(pages[0])); |
b3fd8e83 NA |
2449 | if (cross_page_boundary) |
2450 | pages[1] = virt_to_page(addr + PAGE_SIZE); | |
e587cadd | 2451 | } |
b3fd8e83 NA |
2452 | /* |
2453 | * If something went wrong, crash and burn since recovery paths are not | |
2454 | * implemented. | |
2455 | */ | |
2456 | BUG_ON(!pages[0] || (cross_page_boundary && !pages[1])); | |
2457 | ||
b3fd8e83 NA |
2458 | /* |
2459 | * Map the page without the global bit, as TLB flushing is done with | |
2460 | * flush_tlb_mm_range(), which is intended for non-global PTEs. | |
2461 | */ | |
2462 | pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL); | |
2463 | ||
2464 | /* | |
2465 | * The lock is not really needed, but this allows to avoid open-coding. | |
2466 | */ | |
da364fc5 | 2467 | ptep = get_locked_pte(text_poke_mm, text_poke_mm_addr, &ptl); |
b3fd8e83 NA |
2468 | |
2469 | /* | |
2470 | * This must not fail; preallocated in poking_init(). | |
2471 | */ | |
2472 | VM_BUG_ON(!ptep); | |
2473 | ||
a6d996cb SAS |
2474 | local_irq_save(flags); |
2475 | ||
b3fd8e83 | 2476 | pte = mk_pte(pages[0], pgprot); |
da364fc5 | 2477 | set_pte_at(text_poke_mm, text_poke_mm_addr, ptep, pte); |
b3fd8e83 NA |
2478 | |
2479 | if (cross_page_boundary) { | |
2480 | pte = mk_pte(pages[1], pgprot); | |
da364fc5 | 2481 | set_pte_at(text_poke_mm, text_poke_mm_addr + PAGE_SIZE, ptep + 1, pte); |
b3fd8e83 NA |
2482 | } |
2483 | ||
2484 | /* | |
2485 | * Loading the temporary mm behaves as a compiler barrier, which | |
2486 | * guarantees that the PTE will be set at the time memcpy() is done. | |
2487 | */ | |
f5afa2e8 | 2488 | prev_mm = use_temporary_mm(text_poke_mm); |
b3fd8e83 NA |
2489 | |
2490 | kasan_disable_current(); | |
da364fc5 | 2491 | func((u8 *)text_poke_mm_addr + offset_in_page(addr), src, len); |
b3fd8e83 NA |
2492 | kasan_enable_current(); |
2493 | ||
2494 | /* | |
2495 | * Ensure that the PTE is only cleared after the instructions of memcpy | |
2496 | * were issued by using a compiler barrier. | |
2497 | */ | |
2498 | barrier(); | |
2499 | ||
da364fc5 | 2500 | pte_clear(text_poke_mm, text_poke_mm_addr, ptep); |
b3fd8e83 | 2501 | if (cross_page_boundary) |
da364fc5 | 2502 | pte_clear(text_poke_mm, text_poke_mm_addr + PAGE_SIZE, ptep + 1); |
b3fd8e83 NA |
2503 | |
2504 | /* | |
2505 | * Loading the previous page-table hierarchy requires a serializing | |
2506 | * instruction that already allows the core to see the updated version. | |
2507 | * Xen-PV is assumed to serialize execution in a similar manner. | |
2508 | */ | |
4873f494 | 2509 | unuse_temporary_mm(prev_mm); |
b3fd8e83 NA |
2510 | |
2511 | /* | |
2512 | * Flushing the TLB might involve IPIs, which would require enabled | |
2513 | * IRQs, but not if the mm is not used, as it is in this point. | |
2514 | */ | |
da364fc5 | 2515 | flush_tlb_mm_range(text_poke_mm, text_poke_mm_addr, text_poke_mm_addr + |
b3fd8e83 NA |
2516 | (cross_page_boundary ? 2 : 1) * PAGE_SIZE, |
2517 | PAGE_SHIFT, false); | |
2518 | ||
aadd1b67 SL |
2519 | if (func == text_poke_memcpy) { |
2520 | /* | |
2521 | * If the text does not match what we just wrote then something is | |
2522 | * fundamentally screwy; there's nothing we can really do about that. | |
2523 | */ | |
2524 | BUG_ON(memcmp(addr, src, len)); | |
2525 | } | |
b3fd8e83 | 2526 | |
7cf49427 | 2527 | local_irq_restore(flags); |
a6d996cb | 2528 | pte_unmap_unlock(ptep, ptl); |
e587cadd | 2529 | return addr; |
19d36ccd | 2530 | } |
3d55cc8a | 2531 | |
e836673c NA |
2532 | /** |
2533 | * text_poke - Update instructions on a live kernel | |
2534 | * @addr: address to modify | |
2535 | * @opcode: source of the copy | |
2536 | * @len: length to copy | |
2537 | * | |
2538 | * Only atomic text poke/set should be allowed when not doing early patching. | |
2539 | * It means the size must be writable atomically and the address must be aligned | |
2540 | * in a way that permits an atomic write. It also makes sure we fit on a single | |
2541 | * page. | |
3950746d NA |
2542 | * |
2543 | * Note that the caller must ensure that if the modified code is part of a | |
2544 | * module, the module would not be removed during poking. This can be achieved | |
2545 | * by registering a module notifier, and ordering module removal and patching | |
54aa699e | 2546 | * through a mutex. |
e836673c NA |
2547 | */ |
2548 | void *text_poke(void *addr, const void *opcode, size_t len) | |
2549 | { | |
2550 | lockdep_assert_held(&text_mutex); | |
2551 | ||
aadd1b67 | 2552 | return __text_poke(text_poke_memcpy, addr, opcode, len); |
e836673c NA |
2553 | } |
2554 | ||
2555 | /** | |
2556 | * text_poke_kgdb - Update instructions on a live kernel by kgdb | |
2557 | * @addr: address to modify | |
2558 | * @opcode: source of the copy | |
2559 | * @len: length to copy | |
2560 | * | |
2561 | * Only atomic text poke/set should be allowed when not doing early patching. | |
2562 | * It means the size must be writable atomically and the address must be aligned | |
2563 | * in a way that permits an atomic write. It also makes sure we fit on a single | |
2564 | * page. | |
2565 | * | |
2566 | * Context: should only be used by kgdb, which ensures no other core is running, | |
2567 | * despite the fact it does not hold the text_mutex. | |
2568 | */ | |
2569 | void *text_poke_kgdb(void *addr, const void *opcode, size_t len) | |
2570 | { | |
aadd1b67 | 2571 | return __text_poke(text_poke_memcpy, addr, opcode, len); |
e836673c NA |
2572 | } |
2573 | ||
fe54d079 TG |
2574 | void *text_poke_copy_locked(void *addr, const void *opcode, size_t len, |
2575 | bool core_ok) | |
0e06b403 SL |
2576 | { |
2577 | unsigned long start = (unsigned long)addr; | |
2578 | size_t patched = 0; | |
2579 | ||
fe54d079 | 2580 | if (WARN_ON_ONCE(!core_ok && core_kernel_text(start))) |
0e06b403 SL |
2581 | return NULL; |
2582 | ||
0e06b403 SL |
2583 | while (patched < len) { |
2584 | unsigned long ptr = start + patched; | |
2585 | size_t s; | |
2586 | ||
2587 | s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); | |
2588 | ||
aadd1b67 SL |
2589 | __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s); |
2590 | patched += s; | |
2591 | } | |
fe54d079 TG |
2592 | return addr; |
2593 | } | |
2594 | ||
2595 | /** | |
2596 | * text_poke_copy - Copy instructions into (an unused part of) RX memory | |
2597 | * @addr: address to modify | |
2598 | * @opcode: source of the copy | |
2599 | * @len: length to copy, could be more than 2x PAGE_SIZE | |
2600 | * | |
2601 | * Not safe against concurrent execution; useful for JITs to dump | |
2602 | * new code blocks into unused regions of RX memory. Can be used in | |
2603 | * conjunction with synchronize_rcu_tasks() to wait for existing | |
2604 | * execution to quiesce after having made sure no existing functions | |
2605 | * pointers are live. | |
2606 | */ | |
2607 | void *text_poke_copy(void *addr, const void *opcode, size_t len) | |
2608 | { | |
2609 | mutex_lock(&text_mutex); | |
2610 | addr = text_poke_copy_locked(addr, opcode, len, false); | |
aadd1b67 SL |
2611 | mutex_unlock(&text_mutex); |
2612 | return addr; | |
2613 | } | |
2614 | ||
2615 | /** | |
2616 | * text_poke_set - memset into (an unused part of) RX memory | |
2617 | * @addr: address to modify | |
2618 | * @c: the byte to fill the area with | |
2619 | * @len: length to copy, could be more than 2x PAGE_SIZE | |
2620 | * | |
2621 | * This is useful to overwrite unused regions of RX memory with illegal | |
2622 | * instructions. | |
2623 | */ | |
2624 | void *text_poke_set(void *addr, int c, size_t len) | |
2625 | { | |
2626 | unsigned long start = (unsigned long)addr; | |
2627 | size_t patched = 0; | |
2628 | ||
2629 | if (WARN_ON_ONCE(core_kernel_text(start))) | |
2630 | return NULL; | |
2631 | ||
2632 | mutex_lock(&text_mutex); | |
2633 | while (patched < len) { | |
2634 | unsigned long ptr = start + patched; | |
2635 | size_t s; | |
2636 | ||
2637 | s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); | |
2638 | ||
2639 | __text_poke(text_poke_memset, (void *)ptr, (void *)&c, s); | |
0e06b403 SL |
2640 | patched += s; |
2641 | } | |
2642 | mutex_unlock(&text_mutex); | |
2643 | return addr; | |
2644 | } | |
2645 | ||
fd4363ff JK |
2646 | static void do_sync_core(void *info) |
2647 | { | |
2648 | sync_core(); | |
2649 | } | |
2650 | ||
6e4955a9 | 2651 | void smp_text_poke_sync_each_cpu(void) |
5c02ece8 PZ |
2652 | { |
2653 | on_each_cpu(do_sync_core, NULL, 1); | |
2654 | } | |
2655 | ||
ac0ee0a9 PZ |
2656 | /* |
2657 | * NOTE: crazy scheme to allow patching Jcc.d32 but not increase the size of | |
2658 | * this thing. When len == 6 everything is prefixed with 0x0f and we map | |
2659 | * opcode to Jcc.d8, using len to distinguish. | |
2660 | */ | |
a81d43c4 | 2661 | struct smp_text_poke_loc { |
26c44b77 PZ |
2662 | /* addr := _stext + rel_addr */ |
2663 | s32 rel_addr; | |
2664 | s32 disp; | |
2665 | u8 len; | |
18cbc8be | 2666 | u8 opcode; |
3c8454df | 2667 | const u8 text[TEXT_POKE_MAX_OPCODE_SIZE]; |
23a76739 | 2668 | /* see smp_text_poke_batch_finish() */ |
d769811c | 2669 | u8 old; |
18cbc8be PZ |
2670 | }; |
2671 | ||
8036fbe5 | 2672 | #define TEXT_POKE_ARRAY_MAX (PAGE_SIZE / sizeof(struct smp_text_poke_loc)) |
6e7dc03a IM |
2673 | |
2674 | static struct smp_text_poke_array { | |
8036fbe5 | 2675 | struct smp_text_poke_loc vec[TEXT_POKE_ARRAY_MAX]; |
6e4955a9 | 2676 | int nr_entries; |
6e7dc03a IM |
2677 | } text_poke_array; |
2678 | ||
0494b16b IM |
2679 | static DEFINE_PER_CPU(atomic_t, text_poke_array_refs); |
2680 | ||
4f953471 IM |
2681 | /* |
2682 | * These four __always_inline annotations imply noinstr, necessary | |
2683 | * due to smp_text_poke_int3_handler() being noinstr: | |
2684 | */ | |
2685 | ||
b6a25841 | 2686 | static __always_inline bool try_get_text_poke_array(void) |
1f676247 | 2687 | { |
28fb7909 | 2688 | atomic_t *refs = this_cpu_ptr(&text_poke_array_refs); |
1f676247 | 2689 | |
4334336e | 2690 | if (!raw_atomic_inc_not_zero(refs)) |
b6a25841 | 2691 | return false; |
1f676247 | 2692 | |
b6a25841 | 2693 | return true; |
1f676247 PZ |
2694 | } |
2695 | ||
3916eec5 | 2696 | static __always_inline void put_text_poke_array(void) |
1f676247 | 2697 | { |
28fb7909 | 2698 | atomic_t *refs = this_cpu_ptr(&text_poke_array_refs); |
efd608fa | 2699 | |
1f676247 | 2700 | smp_mb__before_atomic(); |
4334336e | 2701 | raw_atomic_dec(refs); |
1f676247 | 2702 | } |
c0213b0a | 2703 | |
22b96623 | 2704 | static __always_inline void *text_poke_addr(const struct smp_text_poke_loc *tpl) |
4531ef6a | 2705 | { |
22b96623 | 2706 | return _stext + tpl->rel_addr; |
4531ef6a PZ |
2707 | } |
2708 | ||
3e6f4757 | 2709 | static __always_inline int patch_cmp(const void *tpl_a, const void *tpl_b) |
c0213b0a | 2710 | { |
3e6f4757 | 2711 | if (tpl_a < text_poke_addr(tpl_b)) |
c0213b0a | 2712 | return -1; |
3e6f4757 | 2713 | if (tpl_a > text_poke_addr(tpl_b)) |
c0213b0a DBO |
2714 | return 1; |
2715 | return 0; | |
2716 | } | |
fd4363ff | 2717 | |
5236b6a0 | 2718 | noinstr int smp_text_poke_int3_handler(struct pt_regs *regs) |
fd4363ff | 2719 | { |
22b96623 | 2720 | struct smp_text_poke_loc *tpl; |
26c44b77 | 2721 | int ret = 0; |
c0213b0a | 2722 | void *ip; |
1f676247 PZ |
2723 | |
2724 | if (user_mode(regs)) | |
2725 | return 0; | |
c0213b0a | 2726 | |
01651324 PZ |
2727 | /* |
2728 | * Having observed our INT3 instruction, we now must observe | |
0494b16b | 2729 | * text_poke_array with non-zero refcount: |
01651324 | 2730 | * |
28fb7909 | 2731 | * text_poke_array_refs = 1 INT3 |
4334336e | 2732 | * WMB RMB |
28fb7909 | 2733 | * write INT3 if (text_poke_array_refs != 0) |
01651324 | 2734 | */ |
fd4363ff JK |
2735 | smp_rmb(); |
2736 | ||
b6a25841 | 2737 | if (!try_get_text_poke_array()) |
17f41571 | 2738 | return 0; |
476ad071 | 2739 | |
c0213b0a | 2740 | /* |
23a76739 | 2741 | * Discount the INT3. See smp_text_poke_batch_finish(). |
c0213b0a | 2742 | */ |
c3d6324f | 2743 | ip = (void *) regs->ip - INT3_INSN_SIZE; |
c0213b0a DBO |
2744 | |
2745 | /* | |
2746 | * Skip the binary search if there is a single member in the vector. | |
2747 | */ | |
8e35752f | 2748 | if (unlikely(text_poke_array.nr_entries > 1)) { |
22b96623 | 2749 | tpl = __inline_bsearch(ip, text_poke_array.vec, text_poke_array.nr_entries, |
a81d43c4 | 2750 | sizeof(struct smp_text_poke_loc), |
f64366ef | 2751 | patch_cmp); |
22b96623 | 2752 | if (!tpl) |
1f676247 | 2753 | goto out_put; |
c0213b0a | 2754 | } else { |
22b96623 IM |
2755 | tpl = text_poke_array.vec; |
2756 | if (text_poke_addr(tpl) != ip) | |
1f676247 | 2757 | goto out_put; |
c0213b0a DBO |
2758 | } |
2759 | ||
22b96623 | 2760 | ip += tpl->len; |
c3d6324f | 2761 | |
22b96623 | 2762 | switch (tpl->opcode) { |
c3d6324f PZ |
2763 | case INT3_INSN_OPCODE: |
2764 | /* | |
2765 | * Someone poked an explicit INT3, they'll want to handle it, | |
2766 | * do not consume. | |
2767 | */ | |
1f676247 | 2768 | goto out_put; |
c3d6324f | 2769 | |
c43a43e4 PZ |
2770 | case RET_INSN_OPCODE: |
2771 | int3_emulate_ret(regs); | |
2772 | break; | |
2773 | ||
c3d6324f | 2774 | case CALL_INSN_OPCODE: |
22b96623 | 2775 | int3_emulate_call(regs, (long)ip + tpl->disp); |
c3d6324f PZ |
2776 | break; |
2777 | ||
2778 | case JMP32_INSN_OPCODE: | |
2779 | case JMP8_INSN_OPCODE: | |
22b96623 | 2780 | int3_emulate_jmp(regs, (long)ip + tpl->disp); |
c3d6324f PZ |
2781 | break; |
2782 | ||
ac0ee0a9 | 2783 | case 0x70 ... 0x7f: /* Jcc */ |
22b96623 | 2784 | int3_emulate_jcc(regs, tpl->opcode & 0xf, (long)ip, tpl->disp); |
ac0ee0a9 PZ |
2785 | break; |
2786 | ||
c3d6324f PZ |
2787 | default: |
2788 | BUG(); | |
2789 | } | |
17f41571 | 2790 | |
1f676247 PZ |
2791 | ret = 1; |
2792 | ||
2793 | out_put: | |
3916eec5 | 2794 | put_text_poke_array(); |
1f676247 | 2795 | return ret; |
fd4363ff | 2796 | } |
17f41571 | 2797 | |
fd4363ff | 2798 | /** |
23a76739 | 2799 | * smp_text_poke_batch_finish() -- update instructions on live kernel on SMP |
fd4363ff | 2800 | * |
dac0d754 IM |
2801 | * Input state: |
2802 | * text_poke_array.vec: vector of instructions to patch | |
2803 | * text_poke_array.nr_entries: number of entries in the vector | |
2804 | * | |
2805 | * Modify multi-byte instructions by using INT3 breakpoints on SMP. | |
2806 | * We completely avoid using stop_machine() here, and achieve the | |
2807 | * synchronization using INT3 breakpoints and SMP cross-calls. | |
fd4363ff JK |
2808 | * |
2809 | * The way it is done: | |
c3d6324f | 2810 | * - For each entry in the vector: |
dac0d754 IM |
2811 | * - add an INT3 trap to the address that will be patched |
2812 | * - SMP sync all CPUs | |
c0213b0a DBO |
2813 | * - For each entry in the vector: |
2814 | * - update all but the first byte of the patched range | |
dac0d754 | 2815 | * - SMP sync all CPUs |
c0213b0a | 2816 | * - For each entry in the vector: |
dac0d754 | 2817 | * - replace the first byte (INT3) by the first byte of the |
c0213b0a | 2818 | * replacing opcode |
dac0d754 | 2819 | * - SMP sync all CPUs |
fd4363ff | 2820 | */ |
23a76739 | 2821 | void smp_text_poke_batch_finish(void) |
fd4363ff | 2822 | { |
c3d6324f | 2823 | unsigned char int3 = INT3_INSN_OPCODE; |
c0213b0a | 2824 | unsigned int i; |
c3d6324f | 2825 | int do_sync; |
9222f606 | 2826 | |
23a76739 NB |
2827 | if (!text_poke_array.nr_entries) |
2828 | return; | |
2829 | ||
9222f606 JK |
2830 | lockdep_assert_held(&text_mutex); |
2831 | ||
efd608fa | 2832 | /* |
46f3d9d3 | 2833 | * Corresponds to the implicit memory barrier in try_get_text_poke_array() to |
0494b16b | 2834 | * ensure reading a non-zero refcount provides up to date text_poke_array data. |
efd608fa | 2835 | */ |
4334336e | 2836 | for_each_possible_cpu(i) |
28fb7909 | 2837 | atomic_set_release(per_cpu_ptr(&text_poke_array_refs, i), 1); |
c0213b0a | 2838 | |
9350a629 SRG |
2839 | /* |
2840 | * Function tracing can enable thousands of places that need to be | |
2841 | * updated. This can take quite some time, and with full kernel debugging | |
2842 | * enabled, this could cause the softlockup watchdog to trigger. | |
2843 | * This function gets called every 256 entries added to be patched. | |
2844 | * Call cond_resched() here to make sure that other tasks can get scheduled | |
2845 | * while processing all the functions being patched. | |
2846 | */ | |
2847 | cond_resched(); | |
2848 | ||
fd4363ff | 2849 | /* |
dac0d754 | 2850 | * Corresponding read barrier in INT3 notifier for making sure the |
74e8e2bf | 2851 | * text_poke_array.nr_entries and handler are correctly ordered wrt. patching. |
fd4363ff JK |
2852 | */ |
2853 | smp_wmb(); | |
2854 | ||
c0213b0a | 2855 | /* |
dac0d754 | 2856 | * First step: add a INT3 trap to the address that will be patched. |
c0213b0a | 2857 | */ |
74e8e2bf IM |
2858 | for (i = 0; i < text_poke_array.nr_entries; i++) { |
2859 | text_poke_array.vec[i].old = *(u8 *)text_poke_addr(&text_poke_array.vec[i]); | |
2860 | text_poke(text_poke_addr(&text_poke_array.vec[i]), &int3, INT3_INSN_SIZE); | |
d769811c | 2861 | } |
fd4363ff | 2862 | |
6e4955a9 | 2863 | smp_text_poke_sync_each_cpu(); |
fd4363ff | 2864 | |
c0213b0a DBO |
2865 | /* |
2866 | * Second step: update all but the first byte of the patched range. | |
2867 | */ | |
74e8e2bf | 2868 | for (do_sync = 0, i = 0; i < text_poke_array.nr_entries; i++) { |
3c8454df IM |
2869 | u8 old[TEXT_POKE_MAX_OPCODE_SIZE+1] = { text_poke_array.vec[i].old, }; |
2870 | u8 _new[TEXT_POKE_MAX_OPCODE_SIZE+1]; | |
74e8e2bf IM |
2871 | const u8 *new = text_poke_array.vec[i].text; |
2872 | int len = text_poke_array.vec[i].len; | |
97e6c977 | 2873 | |
76ffa720 | 2874 | if (len - INT3_INSN_SIZE > 0) { |
d769811c | 2875 | memcpy(old + INT3_INSN_SIZE, |
74e8e2bf | 2876 | text_poke_addr(&text_poke_array.vec[i]) + INT3_INSN_SIZE, |
d769811c | 2877 | len - INT3_INSN_SIZE); |
ac0ee0a9 PZ |
2878 | |
2879 | if (len == 6) { | |
2880 | _new[0] = 0x0f; | |
2881 | memcpy(_new + 1, new, 5); | |
2882 | new = _new; | |
2883 | } | |
2884 | ||
74e8e2bf | 2885 | text_poke(text_poke_addr(&text_poke_array.vec[i]) + INT3_INSN_SIZE, |
ac0ee0a9 | 2886 | new + INT3_INSN_SIZE, |
76ffa720 | 2887 | len - INT3_INSN_SIZE); |
ac0ee0a9 | 2888 | |
c3d6324f | 2889 | do_sync++; |
c0213b0a | 2890 | } |
d769811c AH |
2891 | |
2892 | /* | |
2893 | * Emit a perf event to record the text poke, primarily to | |
2894 | * support Intel PT decoding which must walk the executable code | |
2895 | * to reconstruct the trace. The flow up to here is: | |
2896 | * - write INT3 byte | |
2897 | * - IPI-SYNC | |
2898 | * - write instruction tail | |
2899 | * At this point the actual control flow will be through the | |
2900 | * INT3 and handler and not hit the old or new instruction. | |
2901 | * Intel PT outputs FUP/TIP packets for the INT3, so the flow | |
2902 | * can still be decoded. Subsequently: | |
2903 | * - emit RECORD_TEXT_POKE with the new instruction | |
2904 | * - IPI-SYNC | |
2905 | * - write first byte | |
2906 | * - IPI-SYNC | |
2907 | * So before the text poke event timestamp, the decoder will see | |
2908 | * either the old instruction flow or FUP/TIP of INT3. After the | |
2909 | * text poke event timestamp, the decoder will see either the | |
2910 | * new instruction flow or FUP/TIP of INT3. Thus decoders can | |
2911 | * use the timestamp as the point at which to modify the | |
2912 | * executable code. | |
2913 | * The old instruction is recorded so that the event can be | |
2914 | * processed forwards or backwards. | |
2915 | */ | |
74e8e2bf | 2916 | perf_event_text_poke(text_poke_addr(&text_poke_array.vec[i]), old, len, new, len); |
c0213b0a DBO |
2917 | } |
2918 | ||
c3d6324f | 2919 | if (do_sync) { |
fd4363ff JK |
2920 | /* |
2921 | * According to Intel, this core syncing is very likely | |
2922 | * not necessary and we'd be safe even without it. But | |
2923 | * better safe than sorry (plus there's not only Intel). | |
2924 | */ | |
6e4955a9 | 2925 | smp_text_poke_sync_each_cpu(); |
fd4363ff JK |
2926 | } |
2927 | ||
c0213b0a | 2928 | /* |
dac0d754 | 2929 | * Third step: replace the first byte (INT3) by the first byte of the |
c0213b0a DBO |
2930 | * replacing opcode. |
2931 | */ | |
74e8e2bf IM |
2932 | for (do_sync = 0, i = 0; i < text_poke_array.nr_entries; i++) { |
2933 | u8 byte = text_poke_array.vec[i].text[0]; | |
ac0ee0a9 | 2934 | |
74e8e2bf | 2935 | if (text_poke_array.vec[i].len == 6) |
ac0ee0a9 PZ |
2936 | byte = 0x0f; |
2937 | ||
2938 | if (byte == INT3_INSN_OPCODE) | |
c3d6324f PZ |
2939 | continue; |
2940 | ||
74e8e2bf | 2941 | text_poke(text_poke_addr(&text_poke_array.vec[i]), &byte, INT3_INSN_SIZE); |
c3d6324f PZ |
2942 | do_sync++; |
2943 | } | |
2944 | ||
2945 | if (do_sync) | |
6e4955a9 | 2946 | smp_text_poke_sync_each_cpu(); |
fd4363ff | 2947 | |
01651324 | 2948 | /* |
efd608fa | 2949 | * Remove and wait for refs to be zero. |
d60e4b24 PZ |
2950 | * |
2951 | * Notably, if after step-3 above the INT3 got removed, then the | |
6e4955a9 | 2952 | * smp_text_poke_sync_each_cpu() will have serialized against any running INT3 |
d60e4b24 PZ |
2953 | * handlers and the below spin-wait will not happen. |
2954 | * | |
2955 | * IOW. unless the replacement instruction is INT3, this case goes | |
2956 | * unused. | |
01651324 | 2957 | */ |
4334336e | 2958 | for_each_possible_cpu(i) { |
28fb7909 | 2959 | atomic_t *refs = per_cpu_ptr(&text_poke_array_refs, i); |
4334336e ED |
2960 | |
2961 | if (unlikely(!atomic_dec_and_test(refs))) | |
2962 | atomic_cond_read_acquire(refs, !VAL); | |
2963 | } | |
7fbadb50 IM |
2964 | |
2965 | /* They are all completed: */ | |
2966 | text_poke_array.nr_entries = 0; | |
fd4363ff JK |
2967 | } |
2968 | ||
0e351aec | 2969 | static void __smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate) |
c3d6324f | 2970 | { |
22b96623 | 2971 | struct smp_text_poke_loc *tpl; |
c3d6324f | 2972 | struct insn insn; |
ac0ee0a9 | 2973 | int ret, i = 0; |
c3d6324f | 2974 | |
22b96623 | 2975 | tpl = &text_poke_array.vec[text_poke_array.nr_entries++]; |
0e351aec | 2976 | |
ac0ee0a9 PZ |
2977 | if (len == 6) |
2978 | i = 1; | |
22b96623 | 2979 | memcpy((void *)tpl->text, opcode+i, len-i); |
c3d6324f PZ |
2980 | if (!emulate) |
2981 | emulate = opcode; | |
2982 | ||
52fa82c2 | 2983 | ret = insn_decode_kernel(&insn, emulate); |
63c66cde | 2984 | BUG_ON(ret < 0); |
c3d6324f | 2985 | |
22b96623 IM |
2986 | tpl->rel_addr = addr - (void *)_stext; |
2987 | tpl->len = len; | |
2988 | tpl->opcode = insn.opcode.bytes[0]; | |
c3d6324f | 2989 | |
ac0ee0a9 PZ |
2990 | if (is_jcc32(&insn)) { |
2991 | /* | |
2992 | * Map Jcc.d32 onto Jcc.d8 and use len to distinguish. | |
2993 | */ | |
22b96623 | 2994 | tpl->opcode = insn.opcode.bytes[1] - 0x10; |
ac0ee0a9 PZ |
2995 | } |
2996 | ||
22b96623 | 2997 | switch (tpl->opcode) { |
26c44b77 PZ |
2998 | case RET_INSN_OPCODE: |
2999 | case JMP32_INSN_OPCODE: | |
3000 | case JMP8_INSN_OPCODE: | |
3001 | /* | |
3002 | * Control flow instructions without implied execution of the | |
3003 | * next instruction can be padded with INT3. | |
3004 | */ | |
3005 | for (i = insn.length; i < len; i++) | |
22b96623 | 3006 | BUG_ON(tpl->text[i] != INT3_INSN_OPCODE); |
26c44b77 PZ |
3007 | break; |
3008 | ||
3009 | default: | |
3010 | BUG_ON(len != insn.length); | |
64267734 | 3011 | } |
26c44b77 | 3012 | |
22b96623 | 3013 | switch (tpl->opcode) { |
c3d6324f | 3014 | case INT3_INSN_OPCODE: |
c43a43e4 | 3015 | case RET_INSN_OPCODE: |
c3d6324f PZ |
3016 | break; |
3017 | ||
3018 | case CALL_INSN_OPCODE: | |
3019 | case JMP32_INSN_OPCODE: | |
3020 | case JMP8_INSN_OPCODE: | |
ac0ee0a9 | 3021 | case 0x70 ... 0x7f: /* Jcc */ |
22b96623 | 3022 | tpl->disp = insn.immediate.value; |
c3d6324f PZ |
3023 | break; |
3024 | ||
3025 | default: /* assume NOP */ | |
3026 | switch (len) { | |
3027 | case 2: /* NOP2 -- emulate as JMP8+0 */ | |
a89dfde3 | 3028 | BUG_ON(memcmp(emulate, x86_nops[len], len)); |
22b96623 IM |
3029 | tpl->opcode = JMP8_INSN_OPCODE; |
3030 | tpl->disp = 0; | |
c3d6324f PZ |
3031 | break; |
3032 | ||
3033 | case 5: /* NOP5 -- emulate as JMP32+0 */ | |
a89dfde3 | 3034 | BUG_ON(memcmp(emulate, x86_nops[len], len)); |
22b96623 IM |
3035 | tpl->opcode = JMP32_INSN_OPCODE; |
3036 | tpl->disp = 0; | |
c3d6324f PZ |
3037 | break; |
3038 | ||
3039 | default: /* unknown instruction */ | |
3040 | BUG(); | |
3041 | } | |
3042 | break; | |
3043 | } | |
3044 | } | |
3045 | ||
18cbc8be | 3046 | /* |
6e7dc03a | 3047 | * We hard rely on the text_poke_array.vec being ordered; ensure this is so by flushing |
18cbc8be PZ |
3048 | * early if needed. |
3049 | */ | |
2d0cf10a | 3050 | static bool text_poke_addr_ordered(void *addr) |
18cbc8be | 3051 | { |
eaa24c91 IM |
3052 | WARN_ON_ONCE(!addr); |
3053 | ||
6e7dc03a | 3054 | if (!text_poke_array.nr_entries) |
2d0cf10a | 3055 | return true; |
18cbc8be | 3056 | |
2d0cf10a IM |
3057 | /* |
3058 | * If the last current entry's address is higher than the | |
3059 | * new entry's address we'd like to add, then ordering | |
3060 | * is violated and we must first flush all pending patching | |
3061 | * requests: | |
3062 | */ | |
0e67e587 | 3063 | if (text_poke_addr(text_poke_array.vec + text_poke_array.nr_entries-1) > addr) |
2d0cf10a | 3064 | return false; |
18cbc8be | 3065 | |
2d0cf10a | 3066 | return true; |
18cbc8be PZ |
3067 | } |
3068 | ||
cca34739 IM |
3069 | /** |
3070 | * smp_text_poke_batch_add() -- update instruction on live kernel on SMP, batched | |
3071 | * @addr: address to patch | |
3072 | * @opcode: opcode of new instruction | |
3073 | * @len: length to copy | |
3074 | * @emulate: instruction to be emulated | |
3075 | * | |
3076 | * Add a new instruction to the current queue of to-be-patched instructions | |
3077 | * the kernel maintains. The patching request will not be executed immediately, | |
3078 | * but becomes part of an array of patching requests, optimized for batched | |
3079 | * execution. All pending patching requests will be executed on the next | |
3080 | * smp_text_poke_batch_finish() call. | |
3081 | */ | |
732c7c33 | 3082 | void __ref smp_text_poke_batch_add(void *addr, const void *opcode, size_t len, const void *emulate) |
18cbc8be | 3083 | { |
2c373ca0 | 3084 | if (text_poke_array.nr_entries == TEXT_POKE_ARRAY_MAX || !text_poke_addr_ordered(addr)) |
23a76739 | 3085 | smp_text_poke_batch_finish(); |
0e351aec | 3086 | __smp_text_poke_batch_add(addr, opcode, len, emulate); |
18cbc8be PZ |
3087 | } |
3088 | ||
c0213b0a | 3089 | /** |
9647ce46 | 3090 | * smp_text_poke_single() -- update instruction on live kernel on SMP immediately |
c0213b0a DBO |
3091 | * @addr: address to patch |
3092 | * @opcode: opcode of new instruction | |
3093 | * @len: length to copy | |
72ebb5ff | 3094 | * @emulate: instruction to be emulated |
c0213b0a DBO |
3095 | * |
3096 | * Update a single instruction with the vector in the stack, avoiding | |
3097 | * dynamically allocated memory. This function should be used when it is | |
9647ce46 IM |
3098 | * not possible to allocate memory for a vector. The single instruction |
3099 | * is patched in immediately. | |
c0213b0a | 3100 | */ |
9586ae48 | 3101 | void __ref smp_text_poke_single(void *addr, const void *opcode, size_t len, const void *emulate) |
c0213b0a | 3102 | { |
2aebf5ee | 3103 | smp_text_poke_batch_add(addr, opcode, len, emulate); |
c8976ade | 3104 | smp_text_poke_batch_finish(); |
c0213b0a | 3105 | } |