Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c767a54b JP |
2 | #define pr_fmt(fmt) "SMP alternatives: " fmt |
3 | ||
9a0b5817 | 4 | #include <linux/module.h> |
f6a57033 | 5 | #include <linux/sched.h> |
d769811c | 6 | #include <linux/perf_event.h> |
2f1dafe5 | 7 | #include <linux/mutex.h> |
9a0b5817 | 8 | #include <linux/list.h> |
8b5a10fc | 9 | #include <linux/stringify.h> |
ca15ca40 | 10 | #include <linux/highmem.h> |
19d36ccd AK |
11 | #include <linux/mm.h> |
12 | #include <linux/vmalloc.h> | |
3945dab4 | 13 | #include <linux/memory.h> |
3d55cc8a | 14 | #include <linux/stop_machine.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
fd4363ff | 16 | #include <linux/kdebug.h> |
c13324a5 | 17 | #include <linux/kprobes.h> |
b3fd8e83 | 18 | #include <linux/mmu_context.h> |
c0213b0a | 19 | #include <linux/bsearch.h> |
9998a983 | 20 | #include <linux/sync_core.h> |
35de5b06 | 21 | #include <asm/text-patching.h> |
9a0b5817 GH |
22 | #include <asm/alternative.h> |
23 | #include <asm/sections.h> | |
8f4e956b AK |
24 | #include <asm/mce.h> |
25 | #include <asm/nmi.h> | |
e587cadd | 26 | #include <asm/cacheflush.h> |
78ff7fae | 27 | #include <asm/tlbflush.h> |
3a125539 | 28 | #include <asm/insn.h> |
e587cadd | 29 | #include <asm/io.h> |
78ff7fae | 30 | #include <asm/fixmap.h> |
4e629211 | 31 | #include <asm/paravirt.h> |
75085009 | 32 | #include <asm/asm-prototypes.h> |
9a0b5817 | 33 | |
5e907bb0 IM |
34 | int __read_mostly alternatives_patched; |
35 | ||
36 | EXPORT_SYMBOL_GPL(alternatives_patched); | |
37 | ||
ab144f5e AK |
38 | #define MAX_PATCH_LEN (255-1) |
39 | ||
6becb502 PZ |
40 | #define DA_ALL (~0) |
41 | #define DA_ALT 0x01 | |
42 | #define DA_RET 0x02 | |
43 | #define DA_RETPOLINE 0x04 | |
44 | #define DA_ENDBR 0x08 | |
45 | #define DA_SMP 0x10 | |
46 | ||
47 | static unsigned int __initdata_or_module debug_alternative; | |
b7fb4af0 | 48 | |
d167a518 GH |
49 | static int __init debug_alt(char *str) |
50 | { | |
6becb502 PZ |
51 | if (str && *str == '=') |
52 | str++; | |
53 | ||
54 | if (!str || kstrtouint(str, 0, &debug_alternative)) | |
55 | debug_alternative = DA_ALL; | |
56 | ||
d167a518 GH |
57 | return 1; |
58 | } | |
d167a518 GH |
59 | __setup("debug-alternative", debug_alt); |
60 | ||
09488165 JB |
61 | static int noreplace_smp; |
62 | ||
b7fb4af0 JF |
63 | static int __init setup_noreplace_smp(char *str) |
64 | { | |
65 | noreplace_smp = 1; | |
66 | return 1; | |
67 | } | |
68 | __setup("noreplace-smp", setup_noreplace_smp); | |
69 | ||
6becb502 | 70 | #define DPRINTK(type, fmt, args...) \ |
db477a33 | 71 | do { \ |
6becb502 | 72 | if (debug_alternative & DA_##type) \ |
1b2e335e | 73 | printk(KERN_DEBUG pr_fmt(fmt) "\n", ##args); \ |
c767a54b | 74 | } while (0) |
d167a518 | 75 | |
6becb502 | 76 | #define DUMP_BYTES(type, buf, len, fmt, args...) \ |
48c7a250 | 77 | do { \ |
6becb502 | 78 | if (unlikely(debug_alternative & DA_##type)) { \ |
48c7a250 BP |
79 | int j; \ |
80 | \ | |
81 | if (!(len)) \ | |
82 | break; \ | |
83 | \ | |
1b2e335e | 84 | printk(KERN_DEBUG pr_fmt(fmt), ##args); \ |
48c7a250 BP |
85 | for (j = 0; j < (len) - 1; j++) \ |
86 | printk(KERN_CONT "%02hhx ", buf[j]); \ | |
87 | printk(KERN_CONT "%02hhx\n", buf[j]); \ | |
88 | } \ | |
89 | } while (0) | |
90 | ||
64e1f587 | 91 | static const unsigned char x86nops[] = |
dc326fca | 92 | { |
a89dfde3 PZ |
93 | BYTES_NOP1, |
94 | BYTES_NOP2, | |
95 | BYTES_NOP3, | |
96 | BYTES_NOP4, | |
97 | BYTES_NOP5, | |
98 | BYTES_NOP6, | |
99 | BYTES_NOP7, | |
100 | BYTES_NOP8, | |
df25edba PZ |
101 | #ifdef CONFIG_64BIT |
102 | BYTES_NOP9, | |
103 | BYTES_NOP10, | |
104 | BYTES_NOP11, | |
105 | #endif | |
9a0b5817 | 106 | }; |
d167a518 | 107 | |
a89dfde3 | 108 | const unsigned char * const x86_nops[ASM_NOP_MAX+1] = |
dc326fca | 109 | { |
32c464f5 | 110 | NULL, |
a89dfde3 PZ |
111 | x86nops, |
112 | x86nops + 1, | |
113 | x86nops + 1 + 2, | |
114 | x86nops + 1 + 2 + 3, | |
115 | x86nops + 1 + 2 + 3 + 4, | |
116 | x86nops + 1 + 2 + 3 + 4 + 5, | |
117 | x86nops + 1 + 2 + 3 + 4 + 5 + 6, | |
118 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7, | |
df25edba PZ |
119 | #ifdef CONFIG_64BIT |
120 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, | |
121 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, | |
122 | x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, | |
123 | #endif | |
32c464f5 | 124 | }; |
9a0b5817 | 125 | |
6c480f22 | 126 | /* |
b6c881b2 PZ |
127 | * Fill the buffer with a single effective instruction of size @len. |
128 | * | |
6c480f22 PZ |
129 | * In order not to issue an ORC stack depth tracking CFI entry (Call Frame Info) |
130 | * for every single-byte NOP, try to generate the maximally available NOP of | |
131 | * size <= ASM_NOP_MAX such that only a single CFI entry is generated (vs one for | |
132 | * each single-byte NOPs). If @len to fill out is > ASM_NOP_MAX, pad with INT3 and | |
133 | * *jump* over instead of executing long and daft NOPs. | |
134 | */ | |
135 | static void __init_or_module add_nop(u8 *instr, unsigned int len) | |
139ec7c4 | 136 | { |
6c480f22 PZ |
137 | u8 *target = instr + len; |
138 | ||
139 | if (!len) | |
140 | return; | |
141 | ||
142 | if (len <= ASM_NOP_MAX) { | |
143 | memcpy(instr, x86_nops[len], len); | |
144 | return; | |
139ec7c4 | 145 | } |
6c480f22 PZ |
146 | |
147 | if (len < 128) { | |
148 | __text_gen_insn(instr, JMP8_INSN_OPCODE, instr, target, JMP8_INSN_SIZE); | |
149 | instr += JMP8_INSN_SIZE; | |
150 | } else { | |
151 | __text_gen_insn(instr, JMP32_INSN_OPCODE, instr, target, JMP32_INSN_SIZE); | |
152 | instr += JMP32_INSN_SIZE; | |
153 | } | |
154 | ||
155 | for (;instr < target; instr++) | |
156 | *instr = INT3_INSN_OPCODE; | |
139ec7c4 RR |
157 | } |
158 | ||
75085009 | 159 | extern s32 __retpoline_sites[], __retpoline_sites_end[]; |
15e67227 | 160 | extern s32 __return_sites[], __return_sites_end[]; |
931ab636 | 161 | extern s32 __cfi_sites[], __cfi_sites_end[]; |
ed53a0d9 | 162 | extern s32 __ibt_endbr_seal[], __ibt_endbr_seal_end[]; |
d167a518 | 163 | extern struct alt_instr __alt_instructions[], __alt_instructions_end[]; |
5967ed87 | 164 | extern s32 __smp_locks[], __smp_locks_end[]; |
0a203df5 | 165 | void text_poke_early(void *addr, const void *opcode, size_t len); |
d167a518 | 166 | |
b6c881b2 PZ |
167 | /* |
168 | * Matches NOP and NOPL, not any of the other possible NOPs. | |
169 | */ | |
6c480f22 | 170 | static bool insn_is_nop(struct insn *insn) |
2b31e8ed | 171 | { |
2bd4aa93 PZ |
172 | /* Anything NOP, but no REP NOP */ |
173 | if (insn->opcode.bytes[0] == 0x90 && | |
174 | (!insn->prefixes.nbytes || insn->prefixes.bytes[0] != 0xF3)) | |
6c480f22 | 175 | return true; |
2b31e8ed | 176 | |
2bd4aa93 | 177 | /* NOPL */ |
6c480f22 PZ |
178 | if (insn->opcode.bytes[0] == 0x0F && insn->opcode.bytes[1] == 0x1F) |
179 | return true; | |
2b31e8ed | 180 | |
6c480f22 | 181 | /* TODO: more nops */ |
2b31e8ed | 182 | |
6c480f22 PZ |
183 | return false; |
184 | } | |
2b31e8ed | 185 | |
b6c881b2 PZ |
186 | /* |
187 | * Find the offset of the first non-NOP instruction starting at @offset | |
188 | * but no further than @len. | |
189 | */ | |
6c480f22 PZ |
190 | static int skip_nops(u8 *instr, int offset, int len) |
191 | { | |
192 | struct insn insn; | |
2b31e8ed | 193 | |
6c480f22 PZ |
194 | for (; offset < len; offset += insn.length) { |
195 | if (insn_decode_kernel(&insn, &instr[offset])) | |
196 | break; | |
2b31e8ed | 197 | |
6c480f22 PZ |
198 | if (!insn_is_nop(&insn)) |
199 | break; | |
200 | } | |
2b31e8ed | 201 | |
6c480f22 | 202 | return offset; |
2b31e8ed BP |
203 | } |
204 | ||
b6c881b2 PZ |
205 | /* |
206 | * Optimize a sequence of NOPs, possibly preceded by an unconditional jump | |
207 | * to the end of the NOP sequence into a single NOP. | |
208 | */ | |
d42a2a89 BPA |
209 | static bool __init_or_module |
210 | __optimize_nops(u8 *instr, size_t len, struct insn *insn, int *next, int *prev, int *target) | |
b6c881b2 PZ |
211 | { |
212 | int i = *next - insn->length; | |
213 | ||
214 | switch (insn->opcode.bytes[0]) { | |
215 | case JMP8_INSN_OPCODE: | |
216 | case JMP32_INSN_OPCODE: | |
217 | *prev = i; | |
218 | *target = *next + insn->immediate.value; | |
219 | return false; | |
220 | } | |
221 | ||
222 | if (insn_is_nop(insn)) { | |
223 | int nop = i; | |
224 | ||
225 | *next = skip_nops(instr, *next, len); | |
226 | if (*target && *next == *target) | |
227 | nop = *prev; | |
228 | ||
229 | add_nop(instr + nop, *next - nop); | |
230 | DUMP_BYTES(ALT, instr, len, "%px: [%d:%d) optimized NOPs: ", instr, nop, *next); | |
231 | return true; | |
232 | } | |
233 | ||
234 | *target = 0; | |
235 | return false; | |
236 | } | |
237 | ||
34bfab0e BP |
238 | /* |
239 | * "noinline" to cause control flow change and thus invalidate I$ and | |
240 | * cause refetch after modification. | |
241 | */ | |
75085009 | 242 | static void __init_or_module noinline optimize_nops(u8 *instr, size_t len) |
4fd4b6e5 | 243 | { |
b6c881b2 PZ |
244 | int prev, target = 0; |
245 | ||
6c480f22 PZ |
246 | for (int next, i = 0; i < len; i = next) { |
247 | struct insn insn; | |
66c117d7 | 248 | |
23c1ad53 PZ |
249 | if (insn_decode_kernel(&insn, &instr[i])) |
250 | return; | |
251 | ||
6c480f22 | 252 | next = i + insn.length; |
23c1ad53 | 253 | |
b6c881b2 | 254 | __optimize_nops(instr, len, &insn, &next, &prev, &target); |
612e8e93 | 255 | } |
4fd4b6e5 BP |
256 | } |
257 | ||
270a69c4 PZ |
258 | /* |
259 | * In this context, "source" is where the instructions are placed in the | |
260 | * section .altinstr_replacement, for example during kernel build by the | |
261 | * toolchain. | |
262 | * "Destination" is where the instructions are being patched in by this | |
263 | * machinery. | |
264 | * | |
265 | * The source offset is: | |
266 | * | |
267 | * src_imm = target - src_next_ip (1) | |
268 | * | |
269 | * and the target offset is: | |
270 | * | |
271 | * dst_imm = target - dst_next_ip (2) | |
272 | * | |
273 | * so rework (1) as an expression for target like: | |
274 | * | |
275 | * target = src_imm + src_next_ip (1a) | |
276 | * | |
277 | * and substitute in (2) to get: | |
278 | * | |
279 | * dst_imm = (src_imm + src_next_ip) - dst_next_ip (3) | |
280 | * | |
281 | * Now, since the instruction stream is 'identical' at src and dst (it | |
282 | * is being copied after all) it can be stated that: | |
283 | * | |
284 | * src_next_ip = src + ip_offset | |
285 | * dst_next_ip = dst + ip_offset (4) | |
286 | * | |
287 | * Substitute (4) in (3) and observe ip_offset being cancelled out to | |
288 | * obtain: | |
289 | * | |
290 | * dst_imm = src_imm + (src + ip_offset) - (dst + ip_offset) | |
291 | * = src_imm + src - dst + ip_offset - ip_offset | |
292 | * = src_imm + src - dst (5) | |
293 | * | |
294 | * IOW, only the relative displacement of the code block matters. | |
295 | */ | |
296 | ||
297 | #define apply_reloc_n(n_, p_, d_) \ | |
298 | do { \ | |
299 | s32 v = *(s##n_ *)(p_); \ | |
300 | v += (d_); \ | |
301 | BUG_ON((v >> 31) != (v >> (n_-1))); \ | |
302 | *(s##n_ *)(p_) = (s##n_)v; \ | |
303 | } while (0) | |
304 | ||
305 | ||
306 | static __always_inline | |
307 | void apply_reloc(int n, void *ptr, uintptr_t diff) | |
308 | { | |
309 | switch (n) { | |
310 | case 1: apply_reloc_n(8, ptr, diff); break; | |
311 | case 2: apply_reloc_n(16, ptr, diff); break; | |
312 | case 4: apply_reloc_n(32, ptr, diff); break; | |
313 | default: BUG(); | |
314 | } | |
315 | } | |
316 | ||
317 | static __always_inline | |
318 | bool need_reloc(unsigned long offset, u8 *src, size_t src_len) | |
319 | { | |
320 | u8 *target = src + offset; | |
321 | /* | |
322 | * If the target is inside the patched block, it's relative to the | |
323 | * block itself and does not need relocation. | |
324 | */ | |
325 | return (target < src || target > src + src_len); | |
326 | } | |
327 | ||
328 | static void __init_or_module noinline | |
329 | apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len) | |
330 | { | |
b6c881b2 PZ |
331 | int prev, target = 0; |
332 | ||
270a69c4 PZ |
333 | for (int next, i = 0; i < len; i = next) { |
334 | struct insn insn; | |
335 | ||
336 | if (WARN_ON_ONCE(insn_decode_kernel(&insn, &buf[i]))) | |
337 | return; | |
338 | ||
339 | next = i + insn.length; | |
340 | ||
b6c881b2 PZ |
341 | if (__optimize_nops(buf, len, &insn, &next, &prev, &target)) |
342 | continue; | |
343 | ||
270a69c4 PZ |
344 | switch (insn.opcode.bytes[0]) { |
345 | case 0x0f: | |
346 | if (insn.opcode.bytes[1] < 0x80 || | |
347 | insn.opcode.bytes[1] > 0x8f) | |
348 | break; | |
349 | ||
350 | fallthrough; /* Jcc.d32 */ | |
351 | case 0x70 ... 0x7f: /* Jcc.d8 */ | |
352 | case JMP8_INSN_OPCODE: | |
353 | case JMP32_INSN_OPCODE: | |
354 | case CALL_INSN_OPCODE: | |
355 | if (need_reloc(next + insn.immediate.value, src, src_len)) { | |
356 | apply_reloc(insn.immediate.nbytes, | |
357 | buf + i + insn_offset_immediate(&insn), | |
358 | src - dest); | |
359 | } | |
360 | ||
361 | /* | |
362 | * Where possible, convert JMP.d32 into JMP.d8. | |
363 | */ | |
364 | if (insn.opcode.bytes[0] == JMP32_INSN_OPCODE) { | |
365 | s32 imm = insn.immediate.value; | |
366 | imm += src - dest; | |
367 | imm += JMP32_INSN_SIZE - JMP8_INSN_SIZE; | |
368 | if ((imm >> 31) == (imm >> 7)) { | |
369 | buf[i+0] = JMP8_INSN_OPCODE; | |
370 | buf[i+1] = (s8)imm; | |
371 | ||
372 | memset(&buf[i+2], INT3_INSN_OPCODE, insn.length - 2); | |
373 | } | |
374 | } | |
375 | break; | |
376 | } | |
377 | ||
378 | if (insn_rip_relative(&insn)) { | |
379 | if (need_reloc(next + insn.displacement.value, src, src_len)) { | |
380 | apply_reloc(insn.displacement.nbytes, | |
381 | buf + i + insn_offset_displacement(&insn), | |
382 | src - dest); | |
383 | } | |
384 | } | |
270a69c4 PZ |
385 | } |
386 | } | |
387 | ||
db477a33 BP |
388 | /* |
389 | * Replace instructions with better alternatives for this CPU type. This runs | |
390 | * before SMP is initialized to avoid SMP problems with self modifying code. | |
391 | * This implies that asymmetric systems where APs have less capabilities than | |
392 | * the boot processor are not handled. Tough. Make sure you disable such | |
393 | * features by hand. | |
34bfab0e BP |
394 | * |
395 | * Marked "noinline" to cause control flow change and thus insn cache | |
396 | * to refetch changed I$ lines. | |
db477a33 | 397 | */ |
34bfab0e BP |
398 | void __init_or_module noinline apply_alternatives(struct alt_instr *start, |
399 | struct alt_instr *end) | |
9a0b5817 | 400 | { |
9a0b5817 | 401 | struct alt_instr *a; |
59e97e4d | 402 | u8 *instr, *replacement; |
1fc654cf | 403 | u8 insn_buff[MAX_PATCH_LEN]; |
9a0b5817 | 404 | |
6becb502 | 405 | DPRINTK(ALT, "alt table %px, -> %px", start, end); |
50973133 FY |
406 | /* |
407 | * The scan order should be from start to end. A later scanned | |
db477a33 | 408 | * alternative code can overwrite previously scanned alternative code. |
50973133 FY |
409 | * Some kernel functions (e.g. memcpy, memset, etc) use this order to |
410 | * patch code. | |
411 | * | |
412 | * So be careful if you want to change the scan order to any other | |
413 | * order. | |
414 | */ | |
9a0b5817 | 415 | for (a = start; a < end; a++) { |
1fc654cf | 416 | int insn_buff_sz = 0; |
48c7a250 | 417 | |
59e97e4d AL |
418 | instr = (u8 *)&a->instr_offset + a->instr_offset; |
419 | replacement = (u8 *)&a->repl_offset + a->repl_offset; | |
1fc654cf | 420 | BUG_ON(a->instrlen > sizeof(insn_buff)); |
5d1dd961 | 421 | BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32); |
dda7bb76 JG |
422 | |
423 | /* | |
424 | * Patch if either: | |
425 | * - feature is present | |
5d1dd961 | 426 | * - feature not present but ALT_FLAG_NOT is set to mean, |
dda7bb76 JG |
427 | * patch if feature is *NOT* present. |
428 | */ | |
270a69c4 PZ |
429 | if (!boot_cpu_has(a->cpuid) == !(a->flags & ALT_FLAG_NOT)) { |
430 | optimize_nops(instr, a->instrlen); | |
431 | continue; | |
432 | } | |
59e97e4d | 433 | |
6becb502 | 434 | DPRINTK(ALT, "feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)", |
5d1dd961 BPA |
435 | (a->flags & ALT_FLAG_NOT) ? "!" : "", |
436 | a->cpuid >> 5, | |
437 | a->cpuid & 0x1f, | |
c1d4e419 | 438 | instr, instr, a->instrlen, |
23c1ad53 | 439 | replacement, a->replacementlen); |
db477a33 | 440 | |
1fc654cf IM |
441 | memcpy(insn_buff, replacement, a->replacementlen); |
442 | insn_buff_sz = a->replacementlen; | |
59e97e4d | 443 | |
23c1ad53 PZ |
444 | for (; insn_buff_sz < a->instrlen; insn_buff_sz++) |
445 | insn_buff[insn_buff_sz] = 0x90; | |
446 | ||
270a69c4 PZ |
447 | apply_relocation(insn_buff, a->instrlen, instr, replacement, a->replacementlen); |
448 | ||
449 | DUMP_BYTES(ALT, instr, a->instrlen, "%px: old_insn: ", instr); | |
450 | DUMP_BYTES(ALT, replacement, a->replacementlen, "%px: rpl_insn: ", replacement); | |
6becb502 | 451 | DUMP_BYTES(ALT, insn_buff, insn_buff_sz, "%px: final_insn: ", instr); |
59e97e4d | 452 | |
1fc654cf | 453 | text_poke_early(instr, insn_buff, insn_buff_sz); |
9a0b5817 GH |
454 | } |
455 | } | |
456 | ||
ac0ee0a9 PZ |
457 | static inline bool is_jcc32(struct insn *insn) |
458 | { | |
459 | /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ | |
460 | return insn->opcode.bytes[0] == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80; | |
461 | } | |
462 | ||
03f16cd0 | 463 | #if defined(CONFIG_RETPOLINE) && defined(CONFIG_OBJTOOL) |
75085009 PZ |
464 | |
465 | /* | |
466 | * CALL/JMP *%\reg | |
467 | */ | |
468 | static int emit_indirect(int op, int reg, u8 *bytes) | |
469 | { | |
470 | int i = 0; | |
471 | u8 modrm; | |
472 | ||
473 | switch (op) { | |
474 | case CALL_INSN_OPCODE: | |
475 | modrm = 0x10; /* Reg = 2; CALL r/m */ | |
476 | break; | |
477 | ||
478 | case JMP32_INSN_OPCODE: | |
479 | modrm = 0x20; /* Reg = 4; JMP r/m */ | |
480 | break; | |
481 | ||
482 | default: | |
483 | WARN_ON_ONCE(1); | |
484 | return -1; | |
485 | } | |
486 | ||
487 | if (reg >= 8) { | |
488 | bytes[i++] = 0x41; /* REX.B prefix */ | |
489 | reg -= 8; | |
490 | } | |
491 | ||
492 | modrm |= 0xc0; /* Mod = 3 */ | |
493 | modrm += reg; | |
494 | ||
495 | bytes[i++] = 0xff; /* opcode */ | |
496 | bytes[i++] = modrm; | |
497 | ||
498 | return i; | |
499 | } | |
500 | ||
3b6c1747 PZ |
501 | static int emit_call_track_retpoline(void *addr, struct insn *insn, int reg, u8 *bytes) |
502 | { | |
503 | u8 op = insn->opcode.bytes[0]; | |
504 | int i = 0; | |
505 | ||
506 | /* | |
507 | * Clang does 'weird' Jcc __x86_indirect_thunk_r11 conditional | |
508 | * tail-calls. Deal with them. | |
509 | */ | |
510 | if (is_jcc32(insn)) { | |
511 | bytes[i++] = op; | |
512 | op = insn->opcode.bytes[1]; | |
513 | goto clang_jcc; | |
514 | } | |
515 | ||
516 | if (insn->length == 6) | |
517 | bytes[i++] = 0x2e; /* CS-prefix */ | |
518 | ||
519 | switch (op) { | |
520 | case CALL_INSN_OPCODE: | |
521 | __text_gen_insn(bytes+i, op, addr+i, | |
522 | __x86_indirect_call_thunk_array[reg], | |
523 | CALL_INSN_SIZE); | |
524 | i += CALL_INSN_SIZE; | |
525 | break; | |
526 | ||
527 | case JMP32_INSN_OPCODE: | |
528 | clang_jcc: | |
529 | __text_gen_insn(bytes+i, op, addr+i, | |
530 | __x86_indirect_jump_thunk_array[reg], | |
531 | JMP32_INSN_SIZE); | |
532 | i += JMP32_INSN_SIZE; | |
533 | break; | |
534 | ||
535 | default: | |
ae25e00b | 536 | WARN(1, "%pS %px %*ph\n", addr, addr, 6, addr); |
3b6c1747 PZ |
537 | return -1; |
538 | } | |
539 | ||
540 | WARN_ON_ONCE(i != insn->length); | |
541 | ||
542 | return i; | |
543 | } | |
544 | ||
75085009 PZ |
545 | /* |
546 | * Rewrite the compiler generated retpoline thunk calls. | |
547 | * | |
548 | * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate | |
549 | * indirect instructions, avoiding the extra indirection. | |
550 | * | |
551 | * For example, convert: | |
552 | * | |
553 | * CALL __x86_indirect_thunk_\reg | |
554 | * | |
555 | * into: | |
556 | * | |
557 | * CALL *%\reg | |
558 | * | |
d45476d9 | 559 | * It also tries to inline spectre_v2=retpoline,lfence when size permits. |
75085009 PZ |
560 | */ |
561 | static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) | |
562 | { | |
563 | retpoline_thunk_t *target; | |
2f0cbb2a PZ |
564 | int reg, ret, i = 0; |
565 | u8 op, cc; | |
75085009 PZ |
566 | |
567 | target = addr + insn->length + insn->immediate.value; | |
568 | reg = target - __x86_indirect_thunk_array; | |
569 | ||
570 | if (WARN_ON_ONCE(reg & ~0xf)) | |
571 | return -1; | |
572 | ||
573 | /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */ | |
574 | BUG_ON(reg == 4); | |
575 | ||
bbe2df3f | 576 | if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) && |
3b6c1747 PZ |
577 | !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { |
578 | if (cpu_feature_enabled(X86_FEATURE_CALL_DEPTH)) | |
579 | return emit_call_track_retpoline(addr, insn, reg, bytes); | |
580 | ||
75085009 | 581 | return -1; |
3b6c1747 | 582 | } |
75085009 | 583 | |
2f0cbb2a PZ |
584 | op = insn->opcode.bytes[0]; |
585 | ||
586 | /* | |
587 | * Convert: | |
588 | * | |
589 | * Jcc.d32 __x86_indirect_thunk_\reg | |
590 | * | |
591 | * into: | |
592 | * | |
593 | * Jncc.d8 1f | |
bbe2df3f | 594 | * [ LFENCE ] |
2f0cbb2a | 595 | * JMP *%\reg |
bbe2df3f | 596 | * [ NOP ] |
2f0cbb2a PZ |
597 | * 1: |
598 | */ | |
3b6c1747 | 599 | if (is_jcc32(insn)) { |
2f0cbb2a PZ |
600 | cc = insn->opcode.bytes[1] & 0xf; |
601 | cc ^= 1; /* invert condition */ | |
602 | ||
603 | bytes[i++] = 0x70 + cc; /* Jcc.d8 */ | |
604 | bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */ | |
605 | ||
606 | /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */ | |
607 | op = JMP32_INSN_OPCODE; | |
608 | } | |
609 | ||
bbe2df3f | 610 | /* |
d45476d9 | 611 | * For RETPOLINE_LFENCE: prepend the indirect CALL/JMP with an LFENCE. |
bbe2df3f | 612 | */ |
d45476d9 | 613 | if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) { |
bbe2df3f PZ |
614 | bytes[i++] = 0x0f; |
615 | bytes[i++] = 0xae; | |
616 | bytes[i++] = 0xe8; /* LFENCE */ | |
617 | } | |
618 | ||
2f0cbb2a PZ |
619 | ret = emit_indirect(op, reg, bytes + i); |
620 | if (ret < 0) | |
621 | return ret; | |
622 | i += ret; | |
75085009 | 623 | |
8c03af3e PZ |
624 | /* |
625 | * The compiler is supposed to EMIT an INT3 after every unconditional | |
626 | * JMP instruction due to AMD BTC. However, if the compiler is too old | |
627 | * or SLS isn't enabled, we still need an INT3 after indirect JMPs | |
628 | * even on Intel. | |
629 | */ | |
630 | if (op == JMP32_INSN_OPCODE && i < insn->length) | |
631 | bytes[i++] = INT3_INSN_OPCODE; | |
632 | ||
75085009 PZ |
633 | for (; i < insn->length;) |
634 | bytes[i++] = BYTES_NOP1; | |
635 | ||
636 | return i; | |
637 | } | |
638 | ||
639 | /* | |
640 | * Generated by 'objtool --retpoline'. | |
641 | */ | |
642 | void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) | |
643 | { | |
644 | s32 *s; | |
645 | ||
646 | for (s = start; s < end; s++) { | |
647 | void *addr = (void *)s + *s; | |
648 | struct insn insn; | |
649 | int len, ret; | |
650 | u8 bytes[16]; | |
651 | u8 op1, op2; | |
652 | ||
653 | ret = insn_decode_kernel(&insn, addr); | |
654 | if (WARN_ON_ONCE(ret < 0)) | |
655 | continue; | |
656 | ||
657 | op1 = insn.opcode.bytes[0]; | |
658 | op2 = insn.opcode.bytes[1]; | |
659 | ||
660 | switch (op1) { | |
661 | case CALL_INSN_OPCODE: | |
662 | case JMP32_INSN_OPCODE: | |
663 | break; | |
664 | ||
2f0cbb2a PZ |
665 | case 0x0f: /* escape */ |
666 | if (op2 >= 0x80 && op2 <= 0x8f) | |
667 | break; | |
668 | fallthrough; | |
75085009 PZ |
669 | default: |
670 | WARN_ON_ONCE(1); | |
671 | continue; | |
672 | } | |
673 | ||
6becb502 | 674 | DPRINTK(RETPOLINE, "retpoline at: %pS (%px) len: %d to: %pS", |
d4b5a5c9 PZ |
675 | addr, addr, insn.length, |
676 | addr + insn.length + insn.immediate.value); | |
677 | ||
75085009 PZ |
678 | len = patch_retpoline(addr, &insn, bytes); |
679 | if (len == insn.length) { | |
680 | optimize_nops(bytes, len); | |
6becb502 PZ |
681 | DUMP_BYTES(RETPOLINE, ((u8*)addr), len, "%px: orig: ", addr); |
682 | DUMP_BYTES(RETPOLINE, ((u8*)bytes), len, "%px: repl: ", addr); | |
75085009 PZ |
683 | text_poke_early(addr, bytes, len); |
684 | } | |
685 | } | |
686 | } | |
687 | ||
f43b9876 | 688 | #ifdef CONFIG_RETHUNK |
770ae1b7 | 689 | |
15e67227 PZ |
690 | /* |
691 | * Rewrite the compiler generated return thunk tail-calls. | |
692 | * | |
693 | * For example, convert: | |
694 | * | |
695 | * JMP __x86_return_thunk | |
696 | * | |
697 | * into: | |
698 | * | |
699 | * RET | |
700 | */ | |
701 | static int patch_return(void *addr, struct insn *insn, u8 *bytes) | |
702 | { | |
703 | int i = 0; | |
704 | ||
d2408e04 | 705 | /* Patch the custom return thunks... */ |
770ae1b7 | 706 | if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) { |
770ae1b7 PZ |
707 | i = JMP32_INSN_SIZE; |
708 | __text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i); | |
709 | } else { | |
d2408e04 | 710 | /* ... or patch them out if not needed. */ |
770ae1b7 PZ |
711 | bytes[i++] = RET_INSN_OPCODE; |
712 | } | |
15e67227 PZ |
713 | |
714 | for (; i < insn->length;) | |
715 | bytes[i++] = INT3_INSN_OPCODE; | |
15e67227 PZ |
716 | return i; |
717 | } | |
718 | ||
719 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) | |
720 | { | |
721 | s32 *s; | |
722 | ||
723 | for (s = start; s < end; s++) { | |
ee88d363 | 724 | void *dest = NULL, *addr = (void *)s + *s; |
15e67227 PZ |
725 | struct insn insn; |
726 | int len, ret; | |
727 | u8 bytes[16]; | |
ee88d363 | 728 | u8 op; |
15e67227 PZ |
729 | |
730 | ret = insn_decode_kernel(&insn, addr); | |
731 | if (WARN_ON_ONCE(ret < 0)) | |
732 | continue; | |
733 | ||
ee88d363 PZ |
734 | op = insn.opcode.bytes[0]; |
735 | if (op == JMP32_INSN_OPCODE) | |
736 | dest = addr + insn.length + insn.immediate.value; | |
737 | ||
738 | if (__static_call_fixup(addr, op, dest) || | |
65cdf0d6 KC |
739 | WARN_ONCE(dest != &__x86_return_thunk, |
740 | "missing return thunk: %pS-%pS: %*ph", | |
741 | addr, dest, 5, addr)) | |
15e67227 PZ |
742 | continue; |
743 | ||
6becb502 | 744 | DPRINTK(RET, "return thunk at: %pS (%px) len: %d to: %pS", |
15e67227 PZ |
745 | addr, addr, insn.length, |
746 | addr + insn.length + insn.immediate.value); | |
747 | ||
748 | len = patch_return(addr, &insn, bytes); | |
749 | if (len == insn.length) { | |
6becb502 PZ |
750 | DUMP_BYTES(RET, ((u8*)addr), len, "%px: orig: ", addr); |
751 | DUMP_BYTES(RET, ((u8*)bytes), len, "%px: repl: ", addr); | |
15e67227 PZ |
752 | text_poke_early(addr, bytes, len); |
753 | } | |
754 | } | |
755 | } | |
f43b9876 PZ |
756 | #else |
757 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } | |
758 | #endif /* CONFIG_RETHUNK */ | |
759 | ||
03f16cd0 | 760 | #else /* !CONFIG_RETPOLINE || !CONFIG_OBJTOOL */ |
75085009 PZ |
761 | |
762 | void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { } | |
15e67227 | 763 | void __init_or_module noinline apply_returns(s32 *start, s32 *end) { } |
75085009 | 764 | |
03f16cd0 | 765 | #endif /* CONFIG_RETPOLINE && CONFIG_OBJTOOL */ |
75085009 | 766 | |
ed53a0d9 PZ |
767 | #ifdef CONFIG_X86_KERNEL_IBT |
768 | ||
9831c625 PZ |
769 | static void poison_cfi(void *addr); |
770 | ||
d42a2a89 | 771 | static void __init_or_module poison_endbr(void *addr, bool warn) |
931ab636 PZ |
772 | { |
773 | u32 endbr, poison = gen_endbr_poison(); | |
774 | ||
775 | if (WARN_ON_ONCE(get_kernel_nofault(endbr, addr))) | |
776 | return; | |
777 | ||
778 | if (!is_endbr(endbr)) { | |
779 | WARN_ON_ONCE(warn); | |
780 | return; | |
781 | } | |
782 | ||
6becb502 | 783 | DPRINTK(ENDBR, "ENDBR at: %pS (%px)", addr, addr); |
931ab636 PZ |
784 | |
785 | /* | |
786 | * When we have IBT, the lack of ENDBR will trigger #CP | |
787 | */ | |
6becb502 PZ |
788 | DUMP_BYTES(ENDBR, ((u8*)addr), 4, "%px: orig: ", addr); |
789 | DUMP_BYTES(ENDBR, ((u8*)&poison), 4, "%px: repl: ", addr); | |
931ab636 PZ |
790 | text_poke_early(addr, &poison, 4); |
791 | } | |
792 | ||
ed53a0d9 PZ |
793 | /* |
794 | * Generated by: objtool --ibt | |
9831c625 PZ |
795 | * |
796 | * Seal the functions for indirect calls by clobbering the ENDBR instructions | |
797 | * and the kCFI hash value. | |
ed53a0d9 | 798 | */ |
be0fffa5 | 799 | void __init_or_module noinline apply_seal_endbr(s32 *start, s32 *end) |
ed53a0d9 PZ |
800 | { |
801 | s32 *s; | |
802 | ||
803 | for (s = start; s < end; s++) { | |
ed53a0d9 PZ |
804 | void *addr = (void *)s + *s; |
805 | ||
931ab636 PZ |
806 | poison_endbr(addr, true); |
807 | if (IS_ENABLED(CONFIG_FINEIBT)) | |
9831c625 | 808 | poison_cfi(addr - 16); |
931ab636 PZ |
809 | } |
810 | } | |
811 | ||
812 | #else | |
813 | ||
be0fffa5 | 814 | void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { } |
931ab636 PZ |
815 | |
816 | #endif /* CONFIG_X86_KERNEL_IBT */ | |
817 | ||
818 | #ifdef CONFIG_FINEIBT | |
082c4c81 PZ |
819 | |
820 | enum cfi_mode { | |
821 | CFI_DEFAULT, | |
822 | CFI_OFF, | |
823 | CFI_KCFI, | |
824 | CFI_FINEIBT, | |
825 | }; | |
826 | ||
827 | static enum cfi_mode cfi_mode __ro_after_init = CFI_DEFAULT; | |
0c3e806e PZ |
828 | static bool cfi_rand __ro_after_init = true; |
829 | static u32 cfi_seed __ro_after_init; | |
830 | ||
831 | /* | |
832 | * Re-hash the CFI hash with a boot-time seed while making sure the result is | |
833 | * not a valid ENDBR instruction. | |
834 | */ | |
835 | static u32 cfi_rehash(u32 hash) | |
836 | { | |
837 | hash ^= cfi_seed; | |
838 | while (unlikely(is_endbr(hash) || is_endbr(-hash))) { | |
839 | bool lsb = hash & 1; | |
840 | hash >>= 1; | |
841 | if (lsb) | |
842 | hash ^= 0x80200003; | |
843 | } | |
844 | return hash; | |
845 | } | |
082c4c81 PZ |
846 | |
847 | static __init int cfi_parse_cmdline(char *str) | |
848 | { | |
849 | if (!str) | |
850 | return -EINVAL; | |
851 | ||
852 | while (str) { | |
853 | char *next = strchr(str, ','); | |
854 | if (next) { | |
855 | *next = 0; | |
856 | next++; | |
857 | } | |
858 | ||
859 | if (!strcmp(str, "auto")) { | |
860 | cfi_mode = CFI_DEFAULT; | |
861 | } else if (!strcmp(str, "off")) { | |
862 | cfi_mode = CFI_OFF; | |
0c3e806e | 863 | cfi_rand = false; |
082c4c81 PZ |
864 | } else if (!strcmp(str, "kcfi")) { |
865 | cfi_mode = CFI_KCFI; | |
866 | } else if (!strcmp(str, "fineibt")) { | |
867 | cfi_mode = CFI_FINEIBT; | |
0c3e806e PZ |
868 | } else if (!strcmp(str, "norand")) { |
869 | cfi_rand = false; | |
082c4c81 PZ |
870 | } else { |
871 | pr_err("Ignoring unknown cfi option (%s).", str); | |
872 | } | |
873 | ||
874 | str = next; | |
875 | } | |
876 | ||
877 | return 0; | |
878 | } | |
879 | early_param("cfi", cfi_parse_cmdline); | |
880 | ||
931ab636 PZ |
881 | /* |
882 | * kCFI FineIBT | |
883 | * | |
884 | * __cfi_\func: __cfi_\func: | |
885 | * movl $0x12345678,%eax // 5 endbr64 // 4 | |
886 | * nop subl $0x12345678,%r10d // 7 | |
887 | * nop jz 1f // 2 | |
888 | * nop ud2 // 2 | |
889 | * nop 1: nop // 1 | |
890 | * nop | |
891 | * nop | |
892 | * nop | |
893 | * nop | |
894 | * nop | |
895 | * nop | |
896 | * nop | |
897 | * | |
898 | * | |
899 | * caller: caller: | |
900 | * movl $(-0x12345678),%r10d // 6 movl $0x12345678,%r10d // 6 | |
901 | * addl $-15(%r11),%r10d // 4 sub $16,%r11 // 4 | |
902 | * je 1f // 2 nop4 // 4 | |
903 | * ud2 // 2 | |
904 | * 1: call __x86_indirect_thunk_r11 // 5 call *%r11; nop2; // 5 | |
905 | * | |
906 | */ | |
907 | ||
908 | asm( ".pushsection .rodata \n" | |
909 | "fineibt_preamble_start: \n" | |
910 | " endbr64 \n" | |
911 | " subl $0x12345678, %r10d \n" | |
912 | " je fineibt_preamble_end \n" | |
913 | " ud2 \n" | |
914 | " nop \n" | |
915 | "fineibt_preamble_end: \n" | |
916 | ".popsection\n" | |
917 | ); | |
918 | ||
919 | extern u8 fineibt_preamble_start[]; | |
920 | extern u8 fineibt_preamble_end[]; | |
921 | ||
922 | #define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start) | |
923 | #define fineibt_preamble_hash 7 | |
924 | ||
925 | asm( ".pushsection .rodata \n" | |
926 | "fineibt_caller_start: \n" | |
927 | " movl $0x12345678, %r10d \n" | |
928 | " sub $16, %r11 \n" | |
929 | ASM_NOP4 | |
930 | "fineibt_caller_end: \n" | |
931 | ".popsection \n" | |
932 | ); | |
933 | ||
934 | extern u8 fineibt_caller_start[]; | |
935 | extern u8 fineibt_caller_end[]; | |
936 | ||
937 | #define fineibt_caller_size (fineibt_caller_end - fineibt_caller_start) | |
938 | #define fineibt_caller_hash 2 | |
939 | ||
940 | #define fineibt_caller_jmp (fineibt_caller_size - 2) | |
941 | ||
942 | static u32 decode_preamble_hash(void *addr) | |
943 | { | |
944 | u8 *p = addr; | |
945 | ||
946 | /* b8 78 56 34 12 mov $0x12345678,%eax */ | |
947 | if (p[0] == 0xb8) | |
948 | return *(u32 *)(addr + 1); | |
949 | ||
950 | return 0; /* invalid hash value */ | |
951 | } | |
952 | ||
953 | static u32 decode_caller_hash(void *addr) | |
954 | { | |
955 | u8 *p = addr; | |
956 | ||
957 | /* 41 ba 78 56 34 12 mov $0x12345678,%r10d */ | |
958 | if (p[0] == 0x41 && p[1] == 0xba) | |
959 | return -*(u32 *)(addr + 2); | |
960 | ||
961 | /* e8 0c 78 56 34 12 jmp.d8 +12 */ | |
962 | if (p[0] == JMP8_INSN_OPCODE && p[1] == fineibt_caller_jmp) | |
963 | return -*(u32 *)(addr + 2); | |
964 | ||
965 | return 0; /* invalid hash value */ | |
966 | } | |
967 | ||
968 | /* .retpoline_sites */ | |
969 | static int cfi_disable_callers(s32 *start, s32 *end) | |
970 | { | |
971 | /* | |
972 | * Disable kCFI by patching in a JMP.d8, this leaves the hash immediate | |
973 | * in tact for later usage. Also see decode_caller_hash() and | |
974 | * cfi_rewrite_callers(). | |
975 | */ | |
976 | const u8 jmp[] = { JMP8_INSN_OPCODE, fineibt_caller_jmp }; | |
977 | s32 *s; | |
ed53a0d9 | 978 | |
931ab636 PZ |
979 | for (s = start; s < end; s++) { |
980 | void *addr = (void *)s + *s; | |
981 | u32 hash; | |
982 | ||
983 | addr -= fineibt_caller_size; | |
984 | hash = decode_caller_hash(addr); | |
985 | if (!hash) /* nocfi callers */ | |
ed53a0d9 PZ |
986 | continue; |
987 | ||
931ab636 PZ |
988 | text_poke_early(addr, jmp, 2); |
989 | } | |
ed53a0d9 | 990 | |
931ab636 PZ |
991 | return 0; |
992 | } | |
993 | ||
0c3e806e PZ |
994 | static int cfi_enable_callers(s32 *start, s32 *end) |
995 | { | |
996 | /* | |
997 | * Re-enable kCFI, undo what cfi_disable_callers() did. | |
998 | */ | |
999 | const u8 mov[] = { 0x41, 0xba }; | |
1000 | s32 *s; | |
1001 | ||
1002 | for (s = start; s < end; s++) { | |
1003 | void *addr = (void *)s + *s; | |
1004 | u32 hash; | |
1005 | ||
1006 | addr -= fineibt_caller_size; | |
1007 | hash = decode_caller_hash(addr); | |
1008 | if (!hash) /* nocfi callers */ | |
ed53a0d9 PZ |
1009 | continue; |
1010 | ||
0c3e806e PZ |
1011 | text_poke_early(addr, mov, 2); |
1012 | } | |
ed53a0d9 | 1013 | |
0c3e806e PZ |
1014 | return 0; |
1015 | } | |
1016 | ||
931ab636 | 1017 | /* .cfi_sites */ |
0c3e806e PZ |
1018 | static int cfi_rand_preamble(s32 *start, s32 *end) |
1019 | { | |
1020 | s32 *s; | |
1021 | ||
1022 | for (s = start; s < end; s++) { | |
1023 | void *addr = (void *)s + *s; | |
1024 | u32 hash; | |
1025 | ||
1026 | hash = decode_preamble_hash(addr); | |
1027 | if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", | |
1028 | addr, addr, 5, addr)) | |
1029 | return -EINVAL; | |
1030 | ||
1031 | hash = cfi_rehash(hash); | |
1032 | text_poke_early(addr + 1, &hash, 4); | |
1033 | } | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
931ab636 PZ |
1038 | static int cfi_rewrite_preamble(s32 *start, s32 *end) |
1039 | { | |
1040 | s32 *s; | |
1041 | ||
1042 | for (s = start; s < end; s++) { | |
1043 | void *addr = (void *)s + *s; | |
1044 | u32 hash; | |
1045 | ||
1046 | hash = decode_preamble_hash(addr); | |
1047 | if (WARN(!hash, "no CFI hash found at: %pS %px %*ph\n", | |
1048 | addr, addr, 5, addr)) | |
1049 | return -EINVAL; | |
1050 | ||
1051 | text_poke_early(addr, fineibt_preamble_start, fineibt_preamble_size); | |
1052 | WARN_ON(*(u32 *)(addr + fineibt_preamble_hash) != 0x12345678); | |
1053 | text_poke_early(addr + fineibt_preamble_hash, &hash, 4); | |
ed53a0d9 | 1054 | } |
931ab636 PZ |
1055 | |
1056 | return 0; | |
1057 | } | |
1058 | ||
04505bbb PZ |
1059 | static void cfi_rewrite_endbr(s32 *start, s32 *end) |
1060 | { | |
1061 | s32 *s; | |
1062 | ||
1063 | for (s = start; s < end; s++) { | |
1064 | void *addr = (void *)s + *s; | |
1065 | ||
1066 | poison_endbr(addr+16, false); | |
1067 | } | |
1068 | } | |
1069 | ||
931ab636 | 1070 | /* .retpoline_sites */ |
0c3e806e PZ |
1071 | static int cfi_rand_callers(s32 *start, s32 *end) |
1072 | { | |
1073 | s32 *s; | |
1074 | ||
1075 | for (s = start; s < end; s++) { | |
1076 | void *addr = (void *)s + *s; | |
1077 | u32 hash; | |
1078 | ||
1079 | addr -= fineibt_caller_size; | |
1080 | hash = decode_caller_hash(addr); | |
1081 | if (hash) { | |
1082 | hash = -cfi_rehash(hash); | |
1083 | text_poke_early(addr + 2, &hash, 4); | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | return 0; | |
1088 | } | |
1089 | ||
931ab636 PZ |
1090 | static int cfi_rewrite_callers(s32 *start, s32 *end) |
1091 | { | |
1092 | s32 *s; | |
1093 | ||
1094 | for (s = start; s < end; s++) { | |
1095 | void *addr = (void *)s + *s; | |
1096 | u32 hash; | |
1097 | ||
1098 | addr -= fineibt_caller_size; | |
1099 | hash = decode_caller_hash(addr); | |
1100 | if (hash) { | |
1101 | text_poke_early(addr, fineibt_caller_start, fineibt_caller_size); | |
1102 | WARN_ON(*(u32 *)(addr + fineibt_caller_hash) != 0x12345678); | |
1103 | text_poke_early(addr + fineibt_caller_hash, &hash, 4); | |
1104 | } | |
1105 | /* rely on apply_retpolines() */ | |
1106 | } | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
1111 | static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, | |
1112 | s32 *start_cfi, s32 *end_cfi, bool builtin) | |
1113 | { | |
1114 | int ret; | |
1115 | ||
1116 | if (WARN_ONCE(fineibt_preamble_size != 16, | |
1117 | "FineIBT preamble wrong size: %ld", fineibt_preamble_size)) | |
1118 | return; | |
1119 | ||
082c4c81 PZ |
1120 | if (cfi_mode == CFI_DEFAULT) { |
1121 | cfi_mode = CFI_KCFI; | |
1122 | if (HAS_KERNEL_IBT && cpu_feature_enabled(X86_FEATURE_IBT)) | |
1123 | cfi_mode = CFI_FINEIBT; | |
1124 | } | |
1125 | ||
0c3e806e PZ |
1126 | /* |
1127 | * Rewrite the callers to not use the __cfi_ stubs, such that we might | |
1128 | * rewrite them. This disables all CFI. If this succeeds but any of the | |
1129 | * later stages fails, we're without CFI. | |
1130 | */ | |
1131 | ret = cfi_disable_callers(start_retpoline, end_retpoline); | |
1132 | if (ret) | |
1133 | goto err; | |
1134 | ||
1135 | if (cfi_rand) { | |
1136 | if (builtin) | |
1137 | cfi_seed = get_random_u32(); | |
1138 | ||
1139 | ret = cfi_rand_preamble(start_cfi, end_cfi); | |
082c4c81 PZ |
1140 | if (ret) |
1141 | goto err; | |
1142 | ||
0c3e806e PZ |
1143 | ret = cfi_rand_callers(start_retpoline, end_retpoline); |
1144 | if (ret) | |
1145 | goto err; | |
ed53a0d9 | 1146 | } |
0c3e806e PZ |
1147 | |
1148 | switch (cfi_mode) { | |
1149 | case CFI_OFF: | |
082c4c81 PZ |
1150 | if (builtin) |
1151 | pr_info("Disabling CFI\n"); | |
931ab636 PZ |
1152 | return; |
1153 | ||
082c4c81 | 1154 | case CFI_KCFI: |
0c3e806e PZ |
1155 | ret = cfi_enable_callers(start_retpoline, end_retpoline); |
1156 | if (ret) | |
1157 | goto err; | |
1158 | ||
082c4c81 PZ |
1159 | if (builtin) |
1160 | pr_info("Using kCFI\n"); | |
1161 | return; | |
931ab636 | 1162 | |
082c4c81 | 1163 | case CFI_FINEIBT: |
04505bbb | 1164 | /* place the FineIBT preamble at func()-16 */ |
082c4c81 PZ |
1165 | ret = cfi_rewrite_preamble(start_cfi, end_cfi); |
1166 | if (ret) | |
1167 | goto err; | |
931ab636 | 1168 | |
04505bbb | 1169 | /* rewrite the callers to target func()-16 */ |
082c4c81 PZ |
1170 | ret = cfi_rewrite_callers(start_retpoline, end_retpoline); |
1171 | if (ret) | |
1172 | goto err; | |
931ab636 | 1173 | |
04505bbb PZ |
1174 | /* now that nobody targets func()+0, remove ENDBR there */ |
1175 | cfi_rewrite_endbr(start_cfi, end_cfi); | |
1176 | ||
082c4c81 PZ |
1177 | if (builtin) |
1178 | pr_info("Using FineIBT CFI\n"); | |
1179 | return; | |
931ab636 | 1180 | |
082c4c81 PZ |
1181 | default: |
1182 | break; | |
1183 | } | |
931ab636 PZ |
1184 | |
1185 | err: | |
1186 | pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n"); | |
ed53a0d9 PZ |
1187 | } |
1188 | ||
9831c625 PZ |
1189 | static inline void poison_hash(void *addr) |
1190 | { | |
1191 | *(u32 *)addr = 0; | |
1192 | } | |
1193 | ||
1194 | static void poison_cfi(void *addr) | |
1195 | { | |
1196 | switch (cfi_mode) { | |
1197 | case CFI_FINEIBT: | |
1198 | /* | |
1199 | * __cfi_\func: | |
1200 | * osp nopl (%rax) | |
1201 | * subl $0, %r10d | |
1202 | * jz 1f | |
1203 | * ud2 | |
1204 | * 1: nop | |
1205 | */ | |
1206 | poison_endbr(addr, false); | |
1207 | poison_hash(addr + fineibt_preamble_hash); | |
1208 | break; | |
1209 | ||
1210 | case CFI_KCFI: | |
1211 | /* | |
1212 | * __cfi_\func: | |
1213 | * movl $0, %eax | |
1214 | * .skip 11, 0x90 | |
1215 | */ | |
1216 | poison_hash(addr + 1); | |
1217 | break; | |
1218 | ||
1219 | default: | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | ||
ed53a0d9 PZ |
1224 | #else |
1225 | ||
931ab636 PZ |
1226 | static void __apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, |
1227 | s32 *start_cfi, s32 *end_cfi, bool builtin) | |
1228 | { | |
1229 | } | |
ed53a0d9 | 1230 | |
535d0ae3 | 1231 | #ifdef CONFIG_X86_KERNEL_IBT |
9831c625 | 1232 | static void poison_cfi(void *addr) { } |
535d0ae3 | 1233 | #endif |
9831c625 | 1234 | |
931ab636 PZ |
1235 | #endif |
1236 | ||
1237 | void apply_fineibt(s32 *start_retpoline, s32 *end_retpoline, | |
1238 | s32 *start_cfi, s32 *end_cfi) | |
1239 | { | |
1240 | return __apply_fineibt(start_retpoline, end_retpoline, | |
1241 | start_cfi, end_cfi, | |
1242 | /* .builtin = */ false); | |
1243 | } | |
ed53a0d9 | 1244 | |
8ec4d41f | 1245 | #ifdef CONFIG_SMP |
5967ed87 JB |
1246 | static void alternatives_smp_lock(const s32 *start, const s32 *end, |
1247 | u8 *text, u8 *text_end) | |
9a0b5817 | 1248 | { |
5967ed87 | 1249 | const s32 *poff; |
9a0b5817 | 1250 | |
5967ed87 JB |
1251 | for (poff = start; poff < end; poff++) { |
1252 | u8 *ptr = (u8 *)poff + *poff; | |
1253 | ||
1254 | if (!*poff || ptr < text || ptr >= text_end) | |
9a0b5817 | 1255 | continue; |
f88f07e0 | 1256 | /* turn DS segment override prefix into lock prefix */ |
d9c5841e PA |
1257 | if (*ptr == 0x3e) |
1258 | text_poke(ptr, ((unsigned char []){0xf0}), 1); | |
4b8073e4 | 1259 | } |
9a0b5817 GH |
1260 | } |
1261 | ||
5967ed87 JB |
1262 | static void alternatives_smp_unlock(const s32 *start, const s32 *end, |
1263 | u8 *text, u8 *text_end) | |
9a0b5817 | 1264 | { |
5967ed87 | 1265 | const s32 *poff; |
9a0b5817 | 1266 | |
5967ed87 JB |
1267 | for (poff = start; poff < end; poff++) { |
1268 | u8 *ptr = (u8 *)poff + *poff; | |
1269 | ||
1270 | if (!*poff || ptr < text || ptr >= text_end) | |
9a0b5817 | 1271 | continue; |
f88f07e0 | 1272 | /* turn lock prefix into DS segment override prefix */ |
d9c5841e PA |
1273 | if (*ptr == 0xf0) |
1274 | text_poke(ptr, ((unsigned char []){0x3E}), 1); | |
4b8073e4 | 1275 | } |
9a0b5817 GH |
1276 | } |
1277 | ||
1278 | struct smp_alt_module { | |
1279 | /* what is this ??? */ | |
1280 | struct module *mod; | |
1281 | char *name; | |
1282 | ||
1283 | /* ptrs to lock prefixes */ | |
5967ed87 JB |
1284 | const s32 *locks; |
1285 | const s32 *locks_end; | |
9a0b5817 GH |
1286 | |
1287 | /* .text segment, needed to avoid patching init code ;) */ | |
1288 | u8 *text; | |
1289 | u8 *text_end; | |
1290 | ||
1291 | struct list_head next; | |
1292 | }; | |
1293 | static LIST_HEAD(smp_alt_modules); | |
e846d139 | 1294 | static bool uniproc_patched = false; /* protected by text_mutex */ |
9a0b5817 | 1295 | |
8b5a10fc JB |
1296 | void __init_or_module alternatives_smp_module_add(struct module *mod, |
1297 | char *name, | |
1298 | void *locks, void *locks_end, | |
1299 | void *text, void *text_end) | |
9a0b5817 GH |
1300 | { |
1301 | struct smp_alt_module *smp; | |
9a0b5817 | 1302 | |
e846d139 | 1303 | mutex_lock(&text_mutex); |
816afe4f RR |
1304 | if (!uniproc_patched) |
1305 | goto unlock; | |
b7fb4af0 | 1306 | |
816afe4f RR |
1307 | if (num_possible_cpus() == 1) |
1308 | /* Don't bother remembering, we'll never have to undo it. */ | |
1309 | goto smp_unlock; | |
9a0b5817 GH |
1310 | |
1311 | smp = kzalloc(sizeof(*smp), GFP_KERNEL); | |
1312 | if (NULL == smp) | |
816afe4f RR |
1313 | /* we'll run the (safe but slow) SMP code then ... */ |
1314 | goto unlock; | |
9a0b5817 GH |
1315 | |
1316 | smp->mod = mod; | |
1317 | smp->name = name; | |
1318 | smp->locks = locks; | |
1319 | smp->locks_end = locks_end; | |
1320 | smp->text = text; | |
1321 | smp->text_end = text_end; | |
6becb502 | 1322 | DPRINTK(SMP, "locks %p -> %p, text %p -> %p, name %s\n", |
db477a33 | 1323 | smp->locks, smp->locks_end, |
9a0b5817 GH |
1324 | smp->text, smp->text_end, smp->name); |
1325 | ||
9a0b5817 | 1326 | list_add_tail(&smp->next, &smp_alt_modules); |
816afe4f RR |
1327 | smp_unlock: |
1328 | alternatives_smp_unlock(locks, locks_end, text, text_end); | |
1329 | unlock: | |
e846d139 | 1330 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
1331 | } |
1332 | ||
8b5a10fc | 1333 | void __init_or_module alternatives_smp_module_del(struct module *mod) |
9a0b5817 GH |
1334 | { |
1335 | struct smp_alt_module *item; | |
9a0b5817 | 1336 | |
e846d139 | 1337 | mutex_lock(&text_mutex); |
9a0b5817 GH |
1338 | list_for_each_entry(item, &smp_alt_modules, next) { |
1339 | if (mod != item->mod) | |
1340 | continue; | |
1341 | list_del(&item->next); | |
9a0b5817 | 1342 | kfree(item); |
816afe4f | 1343 | break; |
9a0b5817 | 1344 | } |
e846d139 | 1345 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
1346 | } |
1347 | ||
816afe4f | 1348 | void alternatives_enable_smp(void) |
9a0b5817 GH |
1349 | { |
1350 | struct smp_alt_module *mod; | |
9a0b5817 | 1351 | |
816afe4f RR |
1352 | /* Why bother if there are no other CPUs? */ |
1353 | BUG_ON(num_possible_cpus() == 1); | |
9a0b5817 | 1354 | |
e846d139 | 1355 | mutex_lock(&text_mutex); |
ca74a6f8 | 1356 | |
816afe4f | 1357 | if (uniproc_patched) { |
c767a54b | 1358 | pr_info("switching to SMP code\n"); |
816afe4f | 1359 | BUG_ON(num_online_cpus() != 1); |
53756d37 JF |
1360 | clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP); |
1361 | clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP); | |
9a0b5817 GH |
1362 | list_for_each_entry(mod, &smp_alt_modules, next) |
1363 | alternatives_smp_lock(mod->locks, mod->locks_end, | |
1364 | mod->text, mod->text_end); | |
816afe4f | 1365 | uniproc_patched = false; |
9a0b5817 | 1366 | } |
e846d139 | 1367 | mutex_unlock(&text_mutex); |
9a0b5817 GH |
1368 | } |
1369 | ||
e846d139 ZC |
1370 | /* |
1371 | * Return 1 if the address range is reserved for SMP-alternatives. | |
1372 | * Must hold text_mutex. | |
1373 | */ | |
2cfa1978 MH |
1374 | int alternatives_text_reserved(void *start, void *end) |
1375 | { | |
1376 | struct smp_alt_module *mod; | |
5967ed87 | 1377 | const s32 *poff; |
076dc4a6 MH |
1378 | u8 *text_start = start; |
1379 | u8 *text_end = end; | |
2cfa1978 | 1380 | |
e846d139 ZC |
1381 | lockdep_assert_held(&text_mutex); |
1382 | ||
2cfa1978 | 1383 | list_for_each_entry(mod, &smp_alt_modules, next) { |
076dc4a6 | 1384 | if (mod->text > text_end || mod->text_end < text_start) |
2cfa1978 | 1385 | continue; |
5967ed87 JB |
1386 | for (poff = mod->locks; poff < mod->locks_end; poff++) { |
1387 | const u8 *ptr = (const u8 *)poff + *poff; | |
1388 | ||
1389 | if (text_start <= ptr && text_end > ptr) | |
2cfa1978 | 1390 | return 1; |
5967ed87 | 1391 | } |
2cfa1978 MH |
1392 | } |
1393 | ||
1394 | return 0; | |
1395 | } | |
48c7a250 | 1396 | #endif /* CONFIG_SMP */ |
8ec4d41f | 1397 | |
139ec7c4 | 1398 | #ifdef CONFIG_PARAVIRT |
6c480f22 PZ |
1399 | |
1400 | /* Use this to add nops to a buffer, then text_poke the whole buffer. */ | |
1401 | static void __init_or_module add_nops(void *insns, unsigned int len) | |
1402 | { | |
1403 | while (len > 0) { | |
1404 | unsigned int noplen = len; | |
1405 | if (noplen > ASM_NOP_MAX) | |
1406 | noplen = ASM_NOP_MAX; | |
1407 | memcpy(insns, x86_nops[noplen], noplen); | |
1408 | insns += noplen; | |
1409 | len -= noplen; | |
1410 | } | |
1411 | } | |
1412 | ||
8b5a10fc JB |
1413 | void __init_or_module apply_paravirt(struct paravirt_patch_site *start, |
1414 | struct paravirt_patch_site *end) | |
139ec7c4 | 1415 | { |
98de032b | 1416 | struct paravirt_patch_site *p; |
1fc654cf | 1417 | char insn_buff[MAX_PATCH_LEN]; |
139ec7c4 RR |
1418 | |
1419 | for (p = start; p < end; p++) { | |
1420 | unsigned int used; | |
1421 | ||
ab144f5e | 1422 | BUG_ON(p->len > MAX_PATCH_LEN); |
d34fda4a | 1423 | /* prep the buffer with the original instructions */ |
1fc654cf | 1424 | memcpy(insn_buff, p->instr, p->len); |
054ac8ad | 1425 | used = paravirt_patch(p->type, insn_buff, (unsigned long)p->instr, p->len); |
7f63c41c | 1426 | |
63f70270 JF |
1427 | BUG_ON(used > p->len); |
1428 | ||
139ec7c4 | 1429 | /* Pad the rest with nops */ |
1fc654cf IM |
1430 | add_nops(insn_buff + used, p->len - used); |
1431 | text_poke_early(p->instr, insn_buff, p->len); | |
139ec7c4 | 1432 | } |
139ec7c4 | 1433 | } |
98de032b | 1434 | extern struct paravirt_patch_site __start_parainstructions[], |
139ec7c4 RR |
1435 | __stop_parainstructions[]; |
1436 | #endif /* CONFIG_PARAVIRT */ | |
1437 | ||
7457c0da PZ |
1438 | /* |
1439 | * Self-test for the INT3 based CALL emulation code. | |
1440 | * | |
1441 | * This exercises int3_emulate_call() to make sure INT3 pt_regs are set up | |
1442 | * properly and that there is a stack gap between the INT3 frame and the | |
1443 | * previous context. Without this gap doing a virtual PUSH on the interrupted | |
1444 | * stack would corrupt the INT3 IRET frame. | |
1445 | * | |
1446 | * See entry_{32,64}.S for more details. | |
1447 | */ | |
ecc60610 PZ |
1448 | |
1449 | /* | |
1450 | * We define the int3_magic() function in assembly to control the calling | |
1451 | * convention such that we can 'call' it from assembly. | |
1452 | */ | |
1453 | ||
1454 | extern void int3_magic(unsigned int *ptr); /* defined in asm */ | |
1455 | ||
1456 | asm ( | |
1457 | " .pushsection .init.text, \"ax\", @progbits\n" | |
1458 | " .type int3_magic, @function\n" | |
1459 | "int3_magic:\n" | |
3e3f0695 | 1460 | ANNOTATE_NOENDBR |
ecc60610 | 1461 | " movl $1, (%" _ASM_ARG1 ")\n" |
b17c2baa | 1462 | ASM_RET |
ecc60610 PZ |
1463 | " .size int3_magic, .-int3_magic\n" |
1464 | " .popsection\n" | |
1465 | ); | |
7457c0da | 1466 | |
99c95c5d | 1467 | extern void int3_selftest_ip(void); /* defined in asm below */ |
7457c0da PZ |
1468 | |
1469 | static int __init | |
1470 | int3_exception_notify(struct notifier_block *self, unsigned long val, void *data) | |
1471 | { | |
3e3f0695 | 1472 | unsigned long selftest = (unsigned long)&int3_selftest_ip; |
7457c0da PZ |
1473 | struct die_args *args = data; |
1474 | struct pt_regs *regs = args->regs; | |
1475 | ||
3e3f0695 PZ |
1476 | OPTIMIZER_HIDE_VAR(selftest); |
1477 | ||
7457c0da PZ |
1478 | if (!regs || user_mode(regs)) |
1479 | return NOTIFY_DONE; | |
1480 | ||
1481 | if (val != DIE_INT3) | |
1482 | return NOTIFY_DONE; | |
1483 | ||
3e3f0695 | 1484 | if (regs->ip - INT3_INSN_SIZE != selftest) |
7457c0da PZ |
1485 | return NOTIFY_DONE; |
1486 | ||
1487 | int3_emulate_call(regs, (unsigned long)&int3_magic); | |
1488 | return NOTIFY_STOP; | |
1489 | } | |
1490 | ||
99c95c5d PZ |
1491 | /* Must be noinline to ensure uniqueness of int3_selftest_ip. */ |
1492 | static noinline void __init int3_selftest(void) | |
7457c0da PZ |
1493 | { |
1494 | static __initdata struct notifier_block int3_exception_nb = { | |
1495 | .notifier_call = int3_exception_notify, | |
1496 | .priority = INT_MAX-1, /* last */ | |
1497 | }; | |
1498 | unsigned int val = 0; | |
1499 | ||
1500 | BUG_ON(register_die_notifier(&int3_exception_nb)); | |
1501 | ||
1502 | /* | |
1503 | * Basically: int3_magic(&val); but really complicated :-) | |
1504 | * | |
99c95c5d PZ |
1505 | * INT3 padded with NOP to CALL_INSN_SIZE. The int3_exception_nb |
1506 | * notifier above will emulate CALL for us. | |
7457c0da | 1507 | */ |
3e3f0695 PZ |
1508 | asm volatile ("int3_selftest_ip:\n\t" |
1509 | ANNOTATE_NOENDBR | |
1510 | " int3; nop; nop; nop; nop\n\t" | |
ecc60610 PZ |
1511 | : ASM_CALL_CONSTRAINT |
1512 | : __ASM_SEL_RAW(a, D) (&val) | |
1513 | : "memory"); | |
7457c0da PZ |
1514 | |
1515 | BUG_ON(val != 1); | |
1516 | ||
1517 | unregister_die_notifier(&int3_exception_nb); | |
1518 | } | |
1519 | ||
270a69c4 PZ |
1520 | static __initdata int __alt_reloc_selftest_addr; |
1521 | ||
1a3e4b4d | 1522 | extern void __init __alt_reloc_selftest(void *arg); |
270a69c4 PZ |
1523 | __visible noinline void __init __alt_reloc_selftest(void *arg) |
1524 | { | |
1525 | WARN_ON(arg != &__alt_reloc_selftest_addr); | |
1526 | } | |
1527 | ||
1528 | static noinline void __init alt_reloc_selftest(void) | |
1529 | { | |
1530 | /* | |
1531 | * Tests apply_relocation(). | |
1532 | * | |
1533 | * This has a relative immediate (CALL) in a place other than the first | |
1534 | * instruction and additionally on x86_64 we get a RIP-relative LEA: | |
1535 | * | |
1536 | * lea 0x0(%rip),%rdi # 5d0: R_X86_64_PC32 .init.data+0x5566c | |
1537 | * call +0 # 5d5: R_X86_64_PLT32 __alt_reloc_selftest-0x4 | |
1538 | * | |
1539 | * Getting this wrong will either crash and burn or tickle the WARN | |
1540 | * above. | |
1541 | */ | |
1542 | asm_inline volatile ( | |
1543 | ALTERNATIVE("", "lea %[mem], %%" _ASM_ARG1 "; call __alt_reloc_selftest;", X86_FEATURE_ALWAYS) | |
1544 | : /* output */ | |
1545 | : [mem] "m" (__alt_reloc_selftest_addr) | |
1546 | : _ASM_ARG1 | |
1547 | ); | |
1548 | } | |
1549 | ||
9a0b5817 GH |
1550 | void __init alternative_instructions(void) |
1551 | { | |
7457c0da PZ |
1552 | int3_selftest(); |
1553 | ||
1554 | /* | |
1555 | * The patching is not fully atomic, so try to avoid local | |
1556 | * interruptions that might execute the to be patched code. | |
1557 | * Other CPUs are not running. | |
1558 | */ | |
8f4e956b | 1559 | stop_nmi(); |
123aa76e AK |
1560 | |
1561 | /* | |
1562 | * Don't stop machine check exceptions while patching. | |
1563 | * MCEs only happen when something got corrupted and in this | |
1564 | * case we must do something about the corruption. | |
32b1cbe3 | 1565 | * Ignoring it is worse than an unlikely patching race. |
123aa76e AK |
1566 | * Also machine checks tend to be broadcast and if one CPU |
1567 | * goes into machine check the others follow quickly, so we don't | |
1568 | * expect a machine check to cause undue problems during to code | |
1569 | * patching. | |
1570 | */ | |
8f4e956b | 1571 | |
4e629211 JG |
1572 | /* |
1573 | * Paravirt patching and alternative patching can be combined to | |
1574 | * replace a function call with a short direct code sequence (e.g. | |
1575 | * by setting a constant return value instead of doing that in an | |
1576 | * external function). | |
1577 | * In order to make this work the following sequence is required: | |
1578 | * 1. set (artificial) features depending on used paravirt | |
1579 | * functions which can later influence alternative patching | |
1580 | * 2. apply paravirt patching (generally replacing an indirect | |
1581 | * function call with a direct one) | |
1582 | * 3. apply alternative patching (e.g. replacing a direct function | |
1583 | * call with a custom code sequence) | |
1584 | * Doing paravirt patching after alternative patching would clobber | |
1585 | * the optimization of the custom code with a function call again. | |
1586 | */ | |
1587 | paravirt_set_cap(); | |
1588 | ||
1589 | /* | |
1590 | * First patch paravirt functions, such that we overwrite the indirect | |
1591 | * call with the direct call. | |
1592 | */ | |
1593 | apply_paravirt(__parainstructions, __parainstructions_end); | |
1594 | ||
931ab636 PZ |
1595 | __apply_fineibt(__retpoline_sites, __retpoline_sites_end, |
1596 | __cfi_sites, __cfi_sites_end, true); | |
1597 | ||
75085009 PZ |
1598 | /* |
1599 | * Rewrite the retpolines, must be done before alternatives since | |
1600 | * those can rewrite the retpoline thunks. | |
1601 | */ | |
1602 | apply_retpolines(__retpoline_sites, __retpoline_sites_end); | |
15e67227 | 1603 | apply_returns(__return_sites, __return_sites_end); |
75085009 | 1604 | |
4e629211 JG |
1605 | /* |
1606 | * Then patch alternatives, such that those paravirt calls that are in | |
1607 | * alternatives can be overwritten by their immediate fragments. | |
1608 | */ | |
9a0b5817 GH |
1609 | apply_alternatives(__alt_instructions, __alt_instructions_end); |
1610 | ||
e81dc127 TG |
1611 | /* |
1612 | * Now all calls are established. Apply the call thunks if | |
1613 | * required. | |
1614 | */ | |
1615 | callthunks_patch_builtin_calls(); | |
1616 | ||
be0fffa5 PZ |
1617 | /* |
1618 | * Seal all functions that do not have their address taken. | |
1619 | */ | |
1620 | apply_seal_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end); | |
ed53a0d9 | 1621 | |
8ec4d41f | 1622 | #ifdef CONFIG_SMP |
816afe4f RR |
1623 | /* Patch to UP if other cpus not imminent. */ |
1624 | if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) { | |
1625 | uniproc_patched = true; | |
9a0b5817 GH |
1626 | alternatives_smp_module_add(NULL, "core kernel", |
1627 | __smp_locks, __smp_locks_end, | |
1628 | _text, _etext); | |
9a0b5817 | 1629 | } |
8f4e956b | 1630 | |
7457c0da | 1631 | if (!uniproc_patched || num_possible_cpus() == 1) { |
f68fd5f4 FW |
1632 | free_init_pages("SMP alternatives", |
1633 | (unsigned long)__smp_locks, | |
1634 | (unsigned long)__smp_locks_end); | |
7457c0da | 1635 | } |
816afe4f RR |
1636 | #endif |
1637 | ||
8f4e956b | 1638 | restart_nmi(); |
5e907bb0 | 1639 | alternatives_patched = 1; |
270a69c4 PZ |
1640 | |
1641 | alt_reloc_selftest(); | |
9a0b5817 | 1642 | } |
19d36ccd | 1643 | |
e587cadd MD |
1644 | /** |
1645 | * text_poke_early - Update instructions on a live kernel at boot time | |
1646 | * @addr: address to modify | |
1647 | * @opcode: source of the copy | |
1648 | * @len: length to copy | |
1649 | * | |
19d36ccd AK |
1650 | * When you use this code to patch more than one byte of an instruction |
1651 | * you need to make sure that other CPUs cannot execute this code in parallel. | |
e587cadd | 1652 | * Also no thread must be currently preempted in the middle of these |
32b1cbe3 MA |
1653 | * instructions. And on the local CPU you need to be protected against NMI or |
1654 | * MCE handlers seeing an inconsistent instruction while you patch. | |
19d36ccd | 1655 | */ |
0a203df5 NA |
1656 | void __init_or_module text_poke_early(void *addr, const void *opcode, |
1657 | size_t len) | |
19d36ccd | 1658 | { |
e587cadd | 1659 | unsigned long flags; |
f2c65fb3 NA |
1660 | |
1661 | if (boot_cpu_has(X86_FEATURE_NX) && | |
1662 | is_module_text_address((unsigned long)addr)) { | |
1663 | /* | |
1664 | * Modules text is marked initially as non-executable, so the | |
1665 | * code cannot be running and speculative code-fetches are | |
1666 | * prevented. Just change the code. | |
1667 | */ | |
1668 | memcpy(addr, opcode, len); | |
1669 | } else { | |
1670 | local_irq_save(flags); | |
1671 | memcpy(addr, opcode, len); | |
1672 | local_irq_restore(flags); | |
1673 | sync_core(); | |
1674 | ||
1675 | /* | |
1676 | * Could also do a CLFLUSH here to speed up CPU recovery; but | |
1677 | * that causes hangs on some VIA CPUs. | |
1678 | */ | |
1679 | } | |
e587cadd MD |
1680 | } |
1681 | ||
9020d395 TG |
1682 | typedef struct { |
1683 | struct mm_struct *mm; | |
1684 | } temp_mm_state_t; | |
1685 | ||
1686 | /* | |
1687 | * Using a temporary mm allows to set temporary mappings that are not accessible | |
1688 | * by other CPUs. Such mappings are needed to perform sensitive memory writes | |
1689 | * that override the kernel memory protections (e.g., W^X), without exposing the | |
1690 | * temporary page-table mappings that are required for these write operations to | |
1691 | * other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the | |
1692 | * mapping is torn down. | |
1693 | * | |
1694 | * Context: The temporary mm needs to be used exclusively by a single core. To | |
1695 | * harden security IRQs must be disabled while the temporary mm is | |
1696 | * loaded, thereby preventing interrupt handler bugs from overriding | |
1697 | * the kernel memory protection. | |
1698 | */ | |
1699 | static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm) | |
1700 | { | |
1701 | temp_mm_state_t temp_state; | |
1702 | ||
1703 | lockdep_assert_irqs_disabled(); | |
abee7c49 JG |
1704 | |
1705 | /* | |
1706 | * Make sure not to be in TLB lazy mode, as otherwise we'll end up | |
1707 | * with a stale address space WITHOUT being in lazy mode after | |
1708 | * restoring the previous mm. | |
1709 | */ | |
2f4305b1 | 1710 | if (this_cpu_read(cpu_tlbstate_shared.is_lazy)) |
abee7c49 JG |
1711 | leave_mm(smp_processor_id()); |
1712 | ||
9020d395 TG |
1713 | temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm); |
1714 | switch_mm_irqs_off(NULL, mm, current); | |
1715 | ||
1716 | /* | |
1717 | * If breakpoints are enabled, disable them while the temporary mm is | |
1718 | * used. Userspace might set up watchpoints on addresses that are used | |
1719 | * in the temporary mm, which would lead to wrong signals being sent or | |
1720 | * crashes. | |
1721 | * | |
1722 | * Note that breakpoints are not disabled selectively, which also causes | |
1723 | * kernel breakpoints (e.g., perf's) to be disabled. This might be | |
1724 | * undesirable, but still seems reasonable as the code that runs in the | |
1725 | * temporary mm should be short. | |
1726 | */ | |
1727 | if (hw_breakpoint_active()) | |
1728 | hw_breakpoint_disable(); | |
1729 | ||
1730 | return temp_state; | |
1731 | } | |
1732 | ||
1733 | static inline void unuse_temporary_mm(temp_mm_state_t prev_state) | |
1734 | { | |
1735 | lockdep_assert_irqs_disabled(); | |
1736 | switch_mm_irqs_off(NULL, prev_state.mm, current); | |
1737 | ||
1738 | /* | |
1739 | * Restore the breakpoints if they were disabled before the temporary mm | |
1740 | * was loaded. | |
1741 | */ | |
1742 | if (hw_breakpoint_active()) | |
1743 | hw_breakpoint_restore(); | |
1744 | } | |
1745 | ||
4fc19708 NA |
1746 | __ro_after_init struct mm_struct *poking_mm; |
1747 | __ro_after_init unsigned long poking_addr; | |
1748 | ||
aadd1b67 SL |
1749 | static void text_poke_memcpy(void *dst, const void *src, size_t len) |
1750 | { | |
1751 | memcpy(dst, src, len); | |
1752 | } | |
1753 | ||
1754 | static void text_poke_memset(void *dst, const void *src, size_t len) | |
1755 | { | |
1756 | int c = *(const int *)src; | |
1757 | ||
1758 | memset(dst, c, len); | |
1759 | } | |
1760 | ||
1761 | typedef void text_poke_f(void *dst, const void *src, size_t len); | |
1762 | ||
1763 | static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t len) | |
e587cadd | 1764 | { |
b3fd8e83 NA |
1765 | bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE; |
1766 | struct page *pages[2] = {NULL}; | |
1767 | temp_mm_state_t prev; | |
78ff7fae | 1768 | unsigned long flags; |
b3fd8e83 NA |
1769 | pte_t pte, *ptep; |
1770 | spinlock_t *ptl; | |
1771 | pgprot_t pgprot; | |
e587cadd | 1772 | |
6fffacb3 | 1773 | /* |
b3fd8e83 NA |
1774 | * While boot memory allocator is running we cannot use struct pages as |
1775 | * they are not yet initialized. There is no way to recover. | |
6fffacb3 PT |
1776 | */ |
1777 | BUG_ON(!after_bootmem); | |
1778 | ||
b7b66baa MD |
1779 | if (!core_kernel_text((unsigned long)addr)) { |
1780 | pages[0] = vmalloc_to_page(addr); | |
b3fd8e83 NA |
1781 | if (cross_page_boundary) |
1782 | pages[1] = vmalloc_to_page(addr + PAGE_SIZE); | |
15a601eb | 1783 | } else { |
b7b66baa | 1784 | pages[0] = virt_to_page(addr); |
00c6b2d5 | 1785 | WARN_ON(!PageReserved(pages[0])); |
b3fd8e83 NA |
1786 | if (cross_page_boundary) |
1787 | pages[1] = virt_to_page(addr + PAGE_SIZE); | |
e587cadd | 1788 | } |
b3fd8e83 NA |
1789 | /* |
1790 | * If something went wrong, crash and burn since recovery paths are not | |
1791 | * implemented. | |
1792 | */ | |
1793 | BUG_ON(!pages[0] || (cross_page_boundary && !pages[1])); | |
1794 | ||
b3fd8e83 NA |
1795 | /* |
1796 | * Map the page without the global bit, as TLB flushing is done with | |
1797 | * flush_tlb_mm_range(), which is intended for non-global PTEs. | |
1798 | */ | |
1799 | pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL); | |
1800 | ||
1801 | /* | |
1802 | * The lock is not really needed, but this allows to avoid open-coding. | |
1803 | */ | |
1804 | ptep = get_locked_pte(poking_mm, poking_addr, &ptl); | |
1805 | ||
1806 | /* | |
1807 | * This must not fail; preallocated in poking_init(). | |
1808 | */ | |
1809 | VM_BUG_ON(!ptep); | |
1810 | ||
a6d996cb SAS |
1811 | local_irq_save(flags); |
1812 | ||
b3fd8e83 NA |
1813 | pte = mk_pte(pages[0], pgprot); |
1814 | set_pte_at(poking_mm, poking_addr, ptep, pte); | |
1815 | ||
1816 | if (cross_page_boundary) { | |
1817 | pte = mk_pte(pages[1], pgprot); | |
1818 | set_pte_at(poking_mm, poking_addr + PAGE_SIZE, ptep + 1, pte); | |
1819 | } | |
1820 | ||
1821 | /* | |
1822 | * Loading the temporary mm behaves as a compiler barrier, which | |
1823 | * guarantees that the PTE will be set at the time memcpy() is done. | |
1824 | */ | |
1825 | prev = use_temporary_mm(poking_mm); | |
1826 | ||
1827 | kasan_disable_current(); | |
aadd1b67 | 1828 | func((u8 *)poking_addr + offset_in_page(addr), src, len); |
b3fd8e83 NA |
1829 | kasan_enable_current(); |
1830 | ||
1831 | /* | |
1832 | * Ensure that the PTE is only cleared after the instructions of memcpy | |
1833 | * were issued by using a compiler barrier. | |
1834 | */ | |
1835 | barrier(); | |
1836 | ||
1837 | pte_clear(poking_mm, poking_addr, ptep); | |
1838 | if (cross_page_boundary) | |
1839 | pte_clear(poking_mm, poking_addr + PAGE_SIZE, ptep + 1); | |
1840 | ||
1841 | /* | |
1842 | * Loading the previous page-table hierarchy requires a serializing | |
1843 | * instruction that already allows the core to see the updated version. | |
1844 | * Xen-PV is assumed to serialize execution in a similar manner. | |
1845 | */ | |
1846 | unuse_temporary_mm(prev); | |
1847 | ||
1848 | /* | |
1849 | * Flushing the TLB might involve IPIs, which would require enabled | |
1850 | * IRQs, but not if the mm is not used, as it is in this point. | |
1851 | */ | |
1852 | flush_tlb_mm_range(poking_mm, poking_addr, poking_addr + | |
1853 | (cross_page_boundary ? 2 : 1) * PAGE_SIZE, | |
1854 | PAGE_SHIFT, false); | |
1855 | ||
aadd1b67 SL |
1856 | if (func == text_poke_memcpy) { |
1857 | /* | |
1858 | * If the text does not match what we just wrote then something is | |
1859 | * fundamentally screwy; there's nothing we can really do about that. | |
1860 | */ | |
1861 | BUG_ON(memcmp(addr, src, len)); | |
1862 | } | |
b3fd8e83 | 1863 | |
7cf49427 | 1864 | local_irq_restore(flags); |
a6d996cb | 1865 | pte_unmap_unlock(ptep, ptl); |
e587cadd | 1866 | return addr; |
19d36ccd | 1867 | } |
3d55cc8a | 1868 | |
e836673c NA |
1869 | /** |
1870 | * text_poke - Update instructions on a live kernel | |
1871 | * @addr: address to modify | |
1872 | * @opcode: source of the copy | |
1873 | * @len: length to copy | |
1874 | * | |
1875 | * Only atomic text poke/set should be allowed when not doing early patching. | |
1876 | * It means the size must be writable atomically and the address must be aligned | |
1877 | * in a way that permits an atomic write. It also makes sure we fit on a single | |
1878 | * page. | |
3950746d NA |
1879 | * |
1880 | * Note that the caller must ensure that if the modified code is part of a | |
1881 | * module, the module would not be removed during poking. This can be achieved | |
1882 | * by registering a module notifier, and ordering module removal and patching | |
1883 | * trough a mutex. | |
e836673c NA |
1884 | */ |
1885 | void *text_poke(void *addr, const void *opcode, size_t len) | |
1886 | { | |
1887 | lockdep_assert_held(&text_mutex); | |
1888 | ||
aadd1b67 | 1889 | return __text_poke(text_poke_memcpy, addr, opcode, len); |
e836673c NA |
1890 | } |
1891 | ||
1892 | /** | |
1893 | * text_poke_kgdb - Update instructions on a live kernel by kgdb | |
1894 | * @addr: address to modify | |
1895 | * @opcode: source of the copy | |
1896 | * @len: length to copy | |
1897 | * | |
1898 | * Only atomic text poke/set should be allowed when not doing early patching. | |
1899 | * It means the size must be writable atomically and the address must be aligned | |
1900 | * in a way that permits an atomic write. It also makes sure we fit on a single | |
1901 | * page. | |
1902 | * | |
1903 | * Context: should only be used by kgdb, which ensures no other core is running, | |
1904 | * despite the fact it does not hold the text_mutex. | |
1905 | */ | |
1906 | void *text_poke_kgdb(void *addr, const void *opcode, size_t len) | |
1907 | { | |
aadd1b67 | 1908 | return __text_poke(text_poke_memcpy, addr, opcode, len); |
e836673c NA |
1909 | } |
1910 | ||
fe54d079 TG |
1911 | void *text_poke_copy_locked(void *addr, const void *opcode, size_t len, |
1912 | bool core_ok) | |
0e06b403 SL |
1913 | { |
1914 | unsigned long start = (unsigned long)addr; | |
1915 | size_t patched = 0; | |
1916 | ||
fe54d079 | 1917 | if (WARN_ON_ONCE(!core_ok && core_kernel_text(start))) |
0e06b403 SL |
1918 | return NULL; |
1919 | ||
0e06b403 SL |
1920 | while (patched < len) { |
1921 | unsigned long ptr = start + patched; | |
1922 | size_t s; | |
1923 | ||
1924 | s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); | |
1925 | ||
aadd1b67 SL |
1926 | __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s); |
1927 | patched += s; | |
1928 | } | |
fe54d079 TG |
1929 | return addr; |
1930 | } | |
1931 | ||
1932 | /** | |
1933 | * text_poke_copy - Copy instructions into (an unused part of) RX memory | |
1934 | * @addr: address to modify | |
1935 | * @opcode: source of the copy | |
1936 | * @len: length to copy, could be more than 2x PAGE_SIZE | |
1937 | * | |
1938 | * Not safe against concurrent execution; useful for JITs to dump | |
1939 | * new code blocks into unused regions of RX memory. Can be used in | |
1940 | * conjunction with synchronize_rcu_tasks() to wait for existing | |
1941 | * execution to quiesce after having made sure no existing functions | |
1942 | * pointers are live. | |
1943 | */ | |
1944 | void *text_poke_copy(void *addr, const void *opcode, size_t len) | |
1945 | { | |
1946 | mutex_lock(&text_mutex); | |
1947 | addr = text_poke_copy_locked(addr, opcode, len, false); | |
aadd1b67 SL |
1948 | mutex_unlock(&text_mutex); |
1949 | return addr; | |
1950 | } | |
1951 | ||
1952 | /** | |
1953 | * text_poke_set - memset into (an unused part of) RX memory | |
1954 | * @addr: address to modify | |
1955 | * @c: the byte to fill the area with | |
1956 | * @len: length to copy, could be more than 2x PAGE_SIZE | |
1957 | * | |
1958 | * This is useful to overwrite unused regions of RX memory with illegal | |
1959 | * instructions. | |
1960 | */ | |
1961 | void *text_poke_set(void *addr, int c, size_t len) | |
1962 | { | |
1963 | unsigned long start = (unsigned long)addr; | |
1964 | size_t patched = 0; | |
1965 | ||
1966 | if (WARN_ON_ONCE(core_kernel_text(start))) | |
1967 | return NULL; | |
1968 | ||
1969 | mutex_lock(&text_mutex); | |
1970 | while (patched < len) { | |
1971 | unsigned long ptr = start + patched; | |
1972 | size_t s; | |
1973 | ||
1974 | s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); | |
1975 | ||
1976 | __text_poke(text_poke_memset, (void *)ptr, (void *)&c, s); | |
0e06b403 SL |
1977 | patched += s; |
1978 | } | |
1979 | mutex_unlock(&text_mutex); | |
1980 | return addr; | |
1981 | } | |
1982 | ||
fd4363ff JK |
1983 | static void do_sync_core(void *info) |
1984 | { | |
1985 | sync_core(); | |
1986 | } | |
1987 | ||
5c02ece8 PZ |
1988 | void text_poke_sync(void) |
1989 | { | |
1990 | on_each_cpu(do_sync_core, NULL, 1); | |
1991 | } | |
1992 | ||
ac0ee0a9 PZ |
1993 | /* |
1994 | * NOTE: crazy scheme to allow patching Jcc.d32 but not increase the size of | |
1995 | * this thing. When len == 6 everything is prefixed with 0x0f and we map | |
1996 | * opcode to Jcc.d8, using len to distinguish. | |
1997 | */ | |
18cbc8be | 1998 | struct text_poke_loc { |
26c44b77 PZ |
1999 | /* addr := _stext + rel_addr */ |
2000 | s32 rel_addr; | |
2001 | s32 disp; | |
2002 | u8 len; | |
18cbc8be PZ |
2003 | u8 opcode; |
2004 | const u8 text[POKE_MAX_OPCODE_SIZE]; | |
26c44b77 | 2005 | /* see text_poke_bp_batch() */ |
d769811c | 2006 | u8 old; |
18cbc8be PZ |
2007 | }; |
2008 | ||
1f676247 | 2009 | struct bp_patching_desc { |
c0213b0a DBO |
2010 | struct text_poke_loc *vec; |
2011 | int nr_entries; | |
1f676247 PZ |
2012 | atomic_t refs; |
2013 | }; | |
2014 | ||
efd608fa | 2015 | static struct bp_patching_desc bp_desc; |
1f676247 | 2016 | |
4979fb53 | 2017 | static __always_inline |
efd608fa | 2018 | struct bp_patching_desc *try_get_desc(void) |
1f676247 | 2019 | { |
efd608fa | 2020 | struct bp_patching_desc *desc = &bp_desc; |
1f676247 | 2021 | |
0f613bfa | 2022 | if (!raw_atomic_inc_not_zero(&desc->refs)) |
1f676247 PZ |
2023 | return NULL; |
2024 | ||
2025 | return desc; | |
2026 | } | |
2027 | ||
efd608fa | 2028 | static __always_inline void put_desc(void) |
1f676247 | 2029 | { |
efd608fa NA |
2030 | struct bp_patching_desc *desc = &bp_desc; |
2031 | ||
1f676247 | 2032 | smp_mb__before_atomic(); |
0f613bfa | 2033 | raw_atomic_dec(&desc->refs); |
1f676247 | 2034 | } |
c0213b0a | 2035 | |
4979fb53 | 2036 | static __always_inline void *text_poke_addr(struct text_poke_loc *tp) |
4531ef6a PZ |
2037 | { |
2038 | return _stext + tp->rel_addr; | |
2039 | } | |
2040 | ||
f64366ef | 2041 | static __always_inline int patch_cmp(const void *key, const void *elt) |
c0213b0a DBO |
2042 | { |
2043 | struct text_poke_loc *tp = (struct text_poke_loc *) elt; | |
2044 | ||
4531ef6a | 2045 | if (key < text_poke_addr(tp)) |
c0213b0a | 2046 | return -1; |
4531ef6a | 2047 | if (key > text_poke_addr(tp)) |
c0213b0a DBO |
2048 | return 1; |
2049 | return 0; | |
2050 | } | |
fd4363ff | 2051 | |
7f6fa101 | 2052 | noinstr int poke_int3_handler(struct pt_regs *regs) |
fd4363ff | 2053 | { |
1f676247 | 2054 | struct bp_patching_desc *desc; |
c0213b0a | 2055 | struct text_poke_loc *tp; |
26c44b77 | 2056 | int ret = 0; |
c0213b0a | 2057 | void *ip; |
1f676247 PZ |
2058 | |
2059 | if (user_mode(regs)) | |
2060 | return 0; | |
c0213b0a | 2061 | |
01651324 PZ |
2062 | /* |
2063 | * Having observed our INT3 instruction, we now must observe | |
efd608fa | 2064 | * bp_desc with non-zero refcount: |
01651324 | 2065 | * |
efd608fa | 2066 | * bp_desc.refs = 1 INT3 |
c3d6324f | 2067 | * WMB RMB |
efd608fa | 2068 | * write INT3 if (bp_desc.refs != 0) |
01651324 | 2069 | */ |
fd4363ff JK |
2070 | smp_rmb(); |
2071 | ||
efd608fa | 2072 | desc = try_get_desc(); |
1f676247 | 2073 | if (!desc) |
17f41571 | 2074 | return 0; |
fd4363ff | 2075 | |
c0213b0a | 2076 | /* |
c3d6324f | 2077 | * Discount the INT3. See text_poke_bp_batch(). |
c0213b0a | 2078 | */ |
c3d6324f | 2079 | ip = (void *) regs->ip - INT3_INSN_SIZE; |
c0213b0a DBO |
2080 | |
2081 | /* | |
2082 | * Skip the binary search if there is a single member in the vector. | |
2083 | */ | |
1f676247 | 2084 | if (unlikely(desc->nr_entries > 1)) { |
f64366ef PZ |
2085 | tp = __inline_bsearch(ip, desc->vec, desc->nr_entries, |
2086 | sizeof(struct text_poke_loc), | |
2087 | patch_cmp); | |
c0213b0a | 2088 | if (!tp) |
1f676247 | 2089 | goto out_put; |
c0213b0a | 2090 | } else { |
1f676247 | 2091 | tp = desc->vec; |
4531ef6a | 2092 | if (text_poke_addr(tp) != ip) |
1f676247 | 2093 | goto out_put; |
c0213b0a DBO |
2094 | } |
2095 | ||
26c44b77 | 2096 | ip += tp->len; |
c3d6324f PZ |
2097 | |
2098 | switch (tp->opcode) { | |
2099 | case INT3_INSN_OPCODE: | |
2100 | /* | |
2101 | * Someone poked an explicit INT3, they'll want to handle it, | |
2102 | * do not consume. | |
2103 | */ | |
1f676247 | 2104 | goto out_put; |
c3d6324f | 2105 | |
c43a43e4 PZ |
2106 | case RET_INSN_OPCODE: |
2107 | int3_emulate_ret(regs); | |
2108 | break; | |
2109 | ||
c3d6324f | 2110 | case CALL_INSN_OPCODE: |
26c44b77 | 2111 | int3_emulate_call(regs, (long)ip + tp->disp); |
c3d6324f PZ |
2112 | break; |
2113 | ||
2114 | case JMP32_INSN_OPCODE: | |
2115 | case JMP8_INSN_OPCODE: | |
26c44b77 | 2116 | int3_emulate_jmp(regs, (long)ip + tp->disp); |
c3d6324f PZ |
2117 | break; |
2118 | ||
ac0ee0a9 PZ |
2119 | case 0x70 ... 0x7f: /* Jcc */ |
2120 | int3_emulate_jcc(regs, tp->opcode & 0xf, (long)ip, tp->disp); | |
2121 | break; | |
2122 | ||
c3d6324f PZ |
2123 | default: |
2124 | BUG(); | |
2125 | } | |
17f41571 | 2126 | |
1f676247 PZ |
2127 | ret = 1; |
2128 | ||
2129 | out_put: | |
efd608fa | 2130 | put_desc(); |
1f676247 | 2131 | return ret; |
fd4363ff | 2132 | } |
17f41571 | 2133 | |
18cbc8be PZ |
2134 | #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc)) |
2135 | static struct text_poke_loc tp_vec[TP_VEC_MAX]; | |
2136 | static int tp_vec_nr; | |
2137 | ||
fd4363ff | 2138 | /** |
c0213b0a DBO |
2139 | * text_poke_bp_batch() -- update instructions on live kernel on SMP |
2140 | * @tp: vector of instructions to patch | |
2141 | * @nr_entries: number of entries in the vector | |
fd4363ff JK |
2142 | * |
2143 | * Modify multi-byte instruction by using int3 breakpoint on SMP. | |
ea8596bb MH |
2144 | * We completely avoid stop_machine() here, and achieve the |
2145 | * synchronization using int3 breakpoint. | |
fd4363ff JK |
2146 | * |
2147 | * The way it is done: | |
c3d6324f | 2148 | * - For each entry in the vector: |
c0213b0a | 2149 | * - add a int3 trap to the address that will be patched |
fd4363ff | 2150 | * - sync cores |
c0213b0a DBO |
2151 | * - For each entry in the vector: |
2152 | * - update all but the first byte of the patched range | |
fd4363ff | 2153 | * - sync cores |
c0213b0a DBO |
2154 | * - For each entry in the vector: |
2155 | * - replace the first byte (int3) by the first byte of | |
2156 | * replacing opcode | |
fd4363ff | 2157 | * - sync cores |
fd4363ff | 2158 | */ |
18cbc8be | 2159 | static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries) |
fd4363ff | 2160 | { |
c3d6324f | 2161 | unsigned char int3 = INT3_INSN_OPCODE; |
c0213b0a | 2162 | unsigned int i; |
c3d6324f | 2163 | int do_sync; |
9222f606 JK |
2164 | |
2165 | lockdep_assert_held(&text_mutex); | |
2166 | ||
efd608fa NA |
2167 | bp_desc.vec = tp; |
2168 | bp_desc.nr_entries = nr_entries; | |
2169 | ||
2170 | /* | |
2171 | * Corresponds to the implicit memory barrier in try_get_desc() to | |
2172 | * ensure reading a non-zero refcount provides up to date bp_desc data. | |
2173 | */ | |
2174 | atomic_set_release(&bp_desc.refs, 1); | |
c0213b0a | 2175 | |
9350a629 SRG |
2176 | /* |
2177 | * Function tracing can enable thousands of places that need to be | |
2178 | * updated. This can take quite some time, and with full kernel debugging | |
2179 | * enabled, this could cause the softlockup watchdog to trigger. | |
2180 | * This function gets called every 256 entries added to be patched. | |
2181 | * Call cond_resched() here to make sure that other tasks can get scheduled | |
2182 | * while processing all the functions being patched. | |
2183 | */ | |
2184 | cond_resched(); | |
2185 | ||
fd4363ff | 2186 | /* |
01651324 | 2187 | * Corresponding read barrier in int3 notifier for making sure the |
c0213b0a | 2188 | * nr_entries and handler are correctly ordered wrt. patching. |
fd4363ff JK |
2189 | */ |
2190 | smp_wmb(); | |
2191 | ||
c0213b0a DBO |
2192 | /* |
2193 | * First step: add a int3 trap to the address that will be patched. | |
2194 | */ | |
d769811c AH |
2195 | for (i = 0; i < nr_entries; i++) { |
2196 | tp[i].old = *(u8 *)text_poke_addr(&tp[i]); | |
76ffa720 | 2197 | text_poke(text_poke_addr(&tp[i]), &int3, INT3_INSN_SIZE); |
d769811c | 2198 | } |
fd4363ff | 2199 | |
5c02ece8 | 2200 | text_poke_sync(); |
fd4363ff | 2201 | |
c0213b0a DBO |
2202 | /* |
2203 | * Second step: update all but the first byte of the patched range. | |
2204 | */ | |
c3d6324f | 2205 | for (do_sync = 0, i = 0; i < nr_entries; i++) { |
ac0ee0a9 PZ |
2206 | u8 old[POKE_MAX_OPCODE_SIZE+1] = { tp[i].old, }; |
2207 | u8 _new[POKE_MAX_OPCODE_SIZE+1]; | |
2208 | const u8 *new = tp[i].text; | |
26c44b77 | 2209 | int len = tp[i].len; |
97e6c977 | 2210 | |
76ffa720 | 2211 | if (len - INT3_INSN_SIZE > 0) { |
d769811c AH |
2212 | memcpy(old + INT3_INSN_SIZE, |
2213 | text_poke_addr(&tp[i]) + INT3_INSN_SIZE, | |
2214 | len - INT3_INSN_SIZE); | |
ac0ee0a9 PZ |
2215 | |
2216 | if (len == 6) { | |
2217 | _new[0] = 0x0f; | |
2218 | memcpy(_new + 1, new, 5); | |
2219 | new = _new; | |
2220 | } | |
2221 | ||
76ffa720 | 2222 | text_poke(text_poke_addr(&tp[i]) + INT3_INSN_SIZE, |
ac0ee0a9 | 2223 | new + INT3_INSN_SIZE, |
76ffa720 | 2224 | len - INT3_INSN_SIZE); |
ac0ee0a9 | 2225 | |
c3d6324f | 2226 | do_sync++; |
c0213b0a | 2227 | } |
d769811c AH |
2228 | |
2229 | /* | |
2230 | * Emit a perf event to record the text poke, primarily to | |
2231 | * support Intel PT decoding which must walk the executable code | |
2232 | * to reconstruct the trace. The flow up to here is: | |
2233 | * - write INT3 byte | |
2234 | * - IPI-SYNC | |
2235 | * - write instruction tail | |
2236 | * At this point the actual control flow will be through the | |
2237 | * INT3 and handler and not hit the old or new instruction. | |
2238 | * Intel PT outputs FUP/TIP packets for the INT3, so the flow | |
2239 | * can still be decoded. Subsequently: | |
2240 | * - emit RECORD_TEXT_POKE with the new instruction | |
2241 | * - IPI-SYNC | |
2242 | * - write first byte | |
2243 | * - IPI-SYNC | |
2244 | * So before the text poke event timestamp, the decoder will see | |
2245 | * either the old instruction flow or FUP/TIP of INT3. After the | |
2246 | * text poke event timestamp, the decoder will see either the | |
2247 | * new instruction flow or FUP/TIP of INT3. Thus decoders can | |
2248 | * use the timestamp as the point at which to modify the | |
2249 | * executable code. | |
2250 | * The old instruction is recorded so that the event can be | |
2251 | * processed forwards or backwards. | |
2252 | */ | |
ac0ee0a9 | 2253 | perf_event_text_poke(text_poke_addr(&tp[i]), old, len, new, len); |
c0213b0a DBO |
2254 | } |
2255 | ||
c3d6324f | 2256 | if (do_sync) { |
fd4363ff JK |
2257 | /* |
2258 | * According to Intel, this core syncing is very likely | |
2259 | * not necessary and we'd be safe even without it. But | |
2260 | * better safe than sorry (plus there's not only Intel). | |
2261 | */ | |
5c02ece8 | 2262 | text_poke_sync(); |
fd4363ff JK |
2263 | } |
2264 | ||
c0213b0a DBO |
2265 | /* |
2266 | * Third step: replace the first byte (int3) by the first byte of | |
2267 | * replacing opcode. | |
2268 | */ | |
c3d6324f | 2269 | for (do_sync = 0, i = 0; i < nr_entries; i++) { |
ac0ee0a9 PZ |
2270 | u8 byte = tp[i].text[0]; |
2271 | ||
2272 | if (tp[i].len == 6) | |
2273 | byte = 0x0f; | |
2274 | ||
2275 | if (byte == INT3_INSN_OPCODE) | |
c3d6324f PZ |
2276 | continue; |
2277 | ||
ac0ee0a9 | 2278 | text_poke(text_poke_addr(&tp[i]), &byte, INT3_INSN_SIZE); |
c3d6324f PZ |
2279 | do_sync++; |
2280 | } | |
2281 | ||
2282 | if (do_sync) | |
5c02ece8 | 2283 | text_poke_sync(); |
fd4363ff | 2284 | |
01651324 | 2285 | /* |
efd608fa | 2286 | * Remove and wait for refs to be zero. |
01651324 | 2287 | */ |
efd608fa NA |
2288 | if (!atomic_dec_and_test(&bp_desc.refs)) |
2289 | atomic_cond_read_acquire(&bp_desc.refs, !VAL); | |
fd4363ff JK |
2290 | } |
2291 | ||
244febbe QH |
2292 | static void text_poke_loc_init(struct text_poke_loc *tp, void *addr, |
2293 | const void *opcode, size_t len, const void *emulate) | |
c3d6324f PZ |
2294 | { |
2295 | struct insn insn; | |
ac0ee0a9 | 2296 | int ret, i = 0; |
c3d6324f | 2297 | |
ac0ee0a9 PZ |
2298 | if (len == 6) |
2299 | i = 1; | |
2300 | memcpy((void *)tp->text, opcode+i, len-i); | |
c3d6324f PZ |
2301 | if (!emulate) |
2302 | emulate = opcode; | |
2303 | ||
52fa82c2 | 2304 | ret = insn_decode_kernel(&insn, emulate); |
63c66cde | 2305 | BUG_ON(ret < 0); |
c3d6324f | 2306 | |
4531ef6a | 2307 | tp->rel_addr = addr - (void *)_stext; |
26c44b77 | 2308 | tp->len = len; |
c3d6324f PZ |
2309 | tp->opcode = insn.opcode.bytes[0]; |
2310 | ||
ac0ee0a9 PZ |
2311 | if (is_jcc32(&insn)) { |
2312 | /* | |
2313 | * Map Jcc.d32 onto Jcc.d8 and use len to distinguish. | |
2314 | */ | |
2315 | tp->opcode = insn.opcode.bytes[1] - 0x10; | |
2316 | } | |
2317 | ||
26c44b77 PZ |
2318 | switch (tp->opcode) { |
2319 | case RET_INSN_OPCODE: | |
2320 | case JMP32_INSN_OPCODE: | |
2321 | case JMP8_INSN_OPCODE: | |
2322 | /* | |
2323 | * Control flow instructions without implied execution of the | |
2324 | * next instruction can be padded with INT3. | |
2325 | */ | |
2326 | for (i = insn.length; i < len; i++) | |
2327 | BUG_ON(tp->text[i] != INT3_INSN_OPCODE); | |
2328 | break; | |
2329 | ||
2330 | default: | |
2331 | BUG_ON(len != insn.length); | |
64267734 | 2332 | } |
26c44b77 | 2333 | |
c3d6324f PZ |
2334 | switch (tp->opcode) { |
2335 | case INT3_INSN_OPCODE: | |
c43a43e4 | 2336 | case RET_INSN_OPCODE: |
c3d6324f PZ |
2337 | break; |
2338 | ||
2339 | case CALL_INSN_OPCODE: | |
2340 | case JMP32_INSN_OPCODE: | |
2341 | case JMP8_INSN_OPCODE: | |
ac0ee0a9 | 2342 | case 0x70 ... 0x7f: /* Jcc */ |
26c44b77 | 2343 | tp->disp = insn.immediate.value; |
c3d6324f PZ |
2344 | break; |
2345 | ||
2346 | default: /* assume NOP */ | |
2347 | switch (len) { | |
2348 | case 2: /* NOP2 -- emulate as JMP8+0 */ | |
a89dfde3 | 2349 | BUG_ON(memcmp(emulate, x86_nops[len], len)); |
c3d6324f | 2350 | tp->opcode = JMP8_INSN_OPCODE; |
26c44b77 | 2351 | tp->disp = 0; |
c3d6324f PZ |
2352 | break; |
2353 | ||
2354 | case 5: /* NOP5 -- emulate as JMP32+0 */ | |
a89dfde3 | 2355 | BUG_ON(memcmp(emulate, x86_nops[len], len)); |
c3d6324f | 2356 | tp->opcode = JMP32_INSN_OPCODE; |
26c44b77 | 2357 | tp->disp = 0; |
c3d6324f PZ |
2358 | break; |
2359 | ||
2360 | default: /* unknown instruction */ | |
2361 | BUG(); | |
2362 | } | |
2363 | break; | |
2364 | } | |
2365 | } | |
2366 | ||
18cbc8be PZ |
2367 | /* |
2368 | * We hard rely on the tp_vec being ordered; ensure this is so by flushing | |
2369 | * early if needed. | |
2370 | */ | |
2371 | static bool tp_order_fail(void *addr) | |
2372 | { | |
2373 | struct text_poke_loc *tp; | |
2374 | ||
2375 | if (!tp_vec_nr) | |
2376 | return false; | |
2377 | ||
2378 | if (!addr) /* force */ | |
2379 | return true; | |
2380 | ||
2381 | tp = &tp_vec[tp_vec_nr - 1]; | |
4531ef6a | 2382 | if ((unsigned long)text_poke_addr(tp) > (unsigned long)addr) |
18cbc8be PZ |
2383 | return true; |
2384 | ||
2385 | return false; | |
2386 | } | |
2387 | ||
2388 | static void text_poke_flush(void *addr) | |
2389 | { | |
2390 | if (tp_vec_nr == TP_VEC_MAX || tp_order_fail(addr)) { | |
2391 | text_poke_bp_batch(tp_vec, tp_vec_nr); | |
2392 | tp_vec_nr = 0; | |
2393 | } | |
2394 | } | |
2395 | ||
2396 | void text_poke_finish(void) | |
2397 | { | |
2398 | text_poke_flush(NULL); | |
2399 | } | |
2400 | ||
768ae440 | 2401 | void __ref text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate) |
18cbc8be PZ |
2402 | { |
2403 | struct text_poke_loc *tp; | |
2404 | ||
2405 | text_poke_flush(addr); | |
2406 | ||
2407 | tp = &tp_vec[tp_vec_nr++]; | |
2408 | text_poke_loc_init(tp, addr, opcode, len, emulate); | |
2409 | } | |
2410 | ||
c0213b0a DBO |
2411 | /** |
2412 | * text_poke_bp() -- update instructions on live kernel on SMP | |
2413 | * @addr: address to patch | |
2414 | * @opcode: opcode of new instruction | |
2415 | * @len: length to copy | |
72ebb5ff | 2416 | * @emulate: instruction to be emulated |
c0213b0a DBO |
2417 | * |
2418 | * Update a single instruction with the vector in the stack, avoiding | |
2419 | * dynamically allocated memory. This function should be used when it is | |
2420 | * not possible to allocate memory. | |
2421 | */ | |
768ae440 | 2422 | void __ref text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate) |
c0213b0a | 2423 | { |
c3d6324f | 2424 | struct text_poke_loc tp; |
c0213b0a | 2425 | |
c3d6324f | 2426 | text_poke_loc_init(&tp, addr, opcode, len, emulate); |
c0213b0a DBO |
2427 | text_poke_bp_batch(&tp, 1); |
2428 | } |