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