Mention branches and keyring.
[releases.git] / x86 / kernel / alternative.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "SMP alternatives: " fmt
3
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/perf_event.h>
7 #include <linux/mutex.h>
8 #include <linux/list.h>
9 #include <linux/stringify.h>
10 #include <linux/highmem.h>
11 #include <linux/mm.h>
12 #include <linux/vmalloc.h>
13 #include <linux/memory.h>
14 #include <linux/stop_machine.h>
15 #include <linux/slab.h>
16 #include <linux/kdebug.h>
17 #include <linux/kprobes.h>
18 #include <linux/mmu_context.h>
19 #include <linux/bsearch.h>
20 #include <linux/sync_core.h>
21 #include <asm/text-patching.h>
22 #include <asm/alternative.h>
23 #include <asm/sections.h>
24 #include <asm/mce.h>
25 #include <asm/nmi.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28 #include <asm/insn.h>
29 #include <asm/io.h>
30 #include <asm/fixmap.h>
31 #include <asm/paravirt.h>
32 #include <asm/asm-prototypes.h>
33
34 int __read_mostly alternatives_patched;
35
36 EXPORT_SYMBOL_GPL(alternatives_patched);
37
38 #define MAX_PATCH_LEN (255-1)
39
40 static int __initdata_or_module debug_alternative;
41
42 static int __init debug_alt(char *str)
43 {
44         debug_alternative = 1;
45         return 1;
46 }
47 __setup("debug-alternative", debug_alt);
48
49 static int noreplace_smp;
50
51 static int __init setup_noreplace_smp(char *str)
52 {
53         noreplace_smp = 1;
54         return 1;
55 }
56 __setup("noreplace-smp", setup_noreplace_smp);
57
58 #define DPRINTK(fmt, args...)                                           \
59 do {                                                                    \
60         if (debug_alternative)                                          \
61                 printk(KERN_DEBUG pr_fmt(fmt) "\n", ##args);            \
62 } while (0)
63
64 #define DUMP_BYTES(buf, len, fmt, args...)                              \
65 do {                                                                    \
66         if (unlikely(debug_alternative)) {                              \
67                 int j;                                                  \
68                                                                         \
69                 if (!(len))                                             \
70                         break;                                          \
71                                                                         \
72                 printk(KERN_DEBUG pr_fmt(fmt), ##args);                 \
73                 for (j = 0; j < (len) - 1; j++)                         \
74                         printk(KERN_CONT "%02hhx ", buf[j]);            \
75                 printk(KERN_CONT "%02hhx\n", buf[j]);                   \
76         }                                                               \
77 } while (0)
78
79 static const unsigned char x86nops[] =
80 {
81         BYTES_NOP1,
82         BYTES_NOP2,
83         BYTES_NOP3,
84         BYTES_NOP4,
85         BYTES_NOP5,
86         BYTES_NOP6,
87         BYTES_NOP7,
88         BYTES_NOP8,
89 };
90
91 const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
92 {
93         NULL,
94         x86nops,
95         x86nops + 1,
96         x86nops + 1 + 2,
97         x86nops + 1 + 2 + 3,
98         x86nops + 1 + 2 + 3 + 4,
99         x86nops + 1 + 2 + 3 + 4 + 5,
100         x86nops + 1 + 2 + 3 + 4 + 5 + 6,
101         x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
102 };
103
104 /* Use this to add nops to a buffer, then text_poke the whole buffer. */
105 static void __init_or_module add_nops(void *insns, unsigned int len)
106 {
107         while (len > 0) {
108                 unsigned int noplen = len;
109                 if (noplen > ASM_NOP_MAX)
110                         noplen = ASM_NOP_MAX;
111                 memcpy(insns, x86_nops[noplen], noplen);
112                 insns += noplen;
113                 len -= noplen;
114         }
115 }
116
117 extern s32 __retpoline_sites[], __retpoline_sites_end[];
118 extern s32 __return_sites[], __return_sites_end[];
119 extern s32 __ibt_endbr_seal[], __ibt_endbr_seal_end[];
120 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
121 extern s32 __smp_locks[], __smp_locks_end[];
122 void text_poke_early(void *addr, const void *opcode, size_t len);
123
124 /*
125  * Are we looking at a near JMP with a 1 or 4-byte displacement.
126  */
127 static inline bool is_jmp(const u8 opcode)
128 {
129         return opcode == 0xeb || opcode == 0xe9;
130 }
131
132 static void __init_or_module
133 recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insn_buff)
134 {
135         u8 *next_rip, *tgt_rip;
136         s32 n_dspl, o_dspl;
137         int repl_len;
138
139         if (a->replacementlen != 5)
140                 return;
141
142         o_dspl = *(s32 *)(insn_buff + 1);
143
144         /* next_rip of the replacement JMP */
145         next_rip = repl_insn + a->replacementlen;
146         /* target rip of the replacement JMP */
147         tgt_rip  = next_rip + o_dspl;
148         n_dspl = tgt_rip - orig_insn;
149
150         DPRINTK("target RIP: %px, new_displ: 0x%x", tgt_rip, n_dspl);
151
152         if (tgt_rip - orig_insn >= 0) {
153                 if (n_dspl - 2 <= 127)
154                         goto two_byte_jmp;
155                 else
156                         goto five_byte_jmp;
157         /* negative offset */
158         } else {
159                 if (((n_dspl - 2) & 0xff) == (n_dspl - 2))
160                         goto two_byte_jmp;
161                 else
162                         goto five_byte_jmp;
163         }
164
165 two_byte_jmp:
166         n_dspl -= 2;
167
168         insn_buff[0] = 0xeb;
169         insn_buff[1] = (s8)n_dspl;
170         add_nops(insn_buff + 2, 3);
171
172         repl_len = 2;
173         goto done;
174
175 five_byte_jmp:
176         n_dspl -= 5;
177
178         insn_buff[0] = 0xe9;
179         *(s32 *)&insn_buff[1] = n_dspl;
180
181         repl_len = 5;
182
183 done:
184
185         DPRINTK("final displ: 0x%08x, JMP 0x%lx",
186                 n_dspl, (unsigned long)orig_insn + n_dspl + repl_len);
187 }
188
189 /*
190  * optimize_nops_range() - Optimize a sequence of single byte NOPs (0x90)
191  *
192  * @instr: instruction byte stream
193  * @instrlen: length of the above
194  * @off: offset within @instr where the first NOP has been detected
195  *
196  * Return: number of NOPs found (and replaced).
197  */
198 static __always_inline int optimize_nops_range(u8 *instr, u8 instrlen, int off)
199 {
200         unsigned long flags;
201         int i = off, nnops;
202
203         while (i < instrlen) {
204                 if (instr[i] != 0x90)
205                         break;
206
207                 i++;
208         }
209
210         nnops = i - off;
211
212         if (nnops <= 1)
213                 return nnops;
214
215         local_irq_save(flags);
216         add_nops(instr + off, nnops);
217         local_irq_restore(flags);
218
219         DUMP_BYTES(instr, instrlen, "%px: [%d:%d) optimized NOPs: ", instr, off, i);
220
221         return nnops;
222 }
223
224 /*
225  * "noinline" to cause control flow change and thus invalidate I$ and
226  * cause refetch after modification.
227  */
228 static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
229 {
230         struct insn insn;
231         int i = 0;
232
233         /*
234          * Jump over the non-NOP insns and optimize single-byte NOPs into bigger
235          * ones.
236          */
237         for (;;) {
238                 if (insn_decode_kernel(&insn, &instr[i]))
239                         return;
240
241                 /*
242                  * See if this and any potentially following NOPs can be
243                  * optimized.
244                  */
245                 if (insn.length == 1 && insn.opcode.bytes[0] == 0x90)
246                         i += optimize_nops_range(instr, len, i);
247                 else
248                         i += insn.length;
249
250                 if (i >= len)
251                         return;
252         }
253 }
254
255 /*
256  * Replace instructions with better alternatives for this CPU type. This runs
257  * before SMP is initialized to avoid SMP problems with self modifying code.
258  * This implies that asymmetric systems where APs have less capabilities than
259  * the boot processor are not handled. Tough. Make sure you disable such
260  * features by hand.
261  *
262  * Marked "noinline" to cause control flow change and thus insn cache
263  * to refetch changed I$ lines.
264  */
265 void __init_or_module noinline apply_alternatives(struct alt_instr *start,
266                                                   struct alt_instr *end)
267 {
268         struct alt_instr *a;
269         u8 *instr, *replacement;
270         u8 insn_buff[MAX_PATCH_LEN];
271
272         DPRINTK("alt table %px, -> %px", start, end);
273
274         /*
275          * In the case CONFIG_X86_5LEVEL=y, KASAN_SHADOW_START is defined using
276          * cpu_feature_enabled(X86_FEATURE_LA57) and is therefore patched here.
277          * During the process, KASAN becomes confused seeing partial LA57
278          * conversion and triggers a false-positive out-of-bound report.
279          *
280          * Disable KASAN until the patching is complete.
281          */
282         kasan_disable_current();
283
284         /*
285          * The scan order should be from start to end. A later scanned
286          * alternative code can overwrite previously scanned alternative code.
287          * Some kernel functions (e.g. memcpy, memset, etc) use this order to
288          * patch code.
289          *
290          * So be careful if you want to change the scan order to any other
291          * order.
292          */
293         for (a = start; a < end; a++) {
294                 int insn_buff_sz = 0;
295                 /* Mask away "NOT" flag bit for feature to test. */
296                 u16 feature = a->cpuid & ~ALTINSTR_FLAG_INV;
297
298                 instr = (u8 *)&a->instr_offset + a->instr_offset;
299                 replacement = (u8 *)&a->repl_offset + a->repl_offset;
300                 BUG_ON(a->instrlen > sizeof(insn_buff));
301                 BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);
302
303                 /*
304                  * Patch if either:
305                  * - feature is present
306                  * - feature not present but ALTINSTR_FLAG_INV is set to mean,
307                  *   patch if feature is *NOT* present.
308                  */
309                 if (!boot_cpu_has(feature) == !(a->cpuid & ALTINSTR_FLAG_INV))
310                         goto next;
311
312                 DPRINTK("feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
313                         (a->cpuid & ALTINSTR_FLAG_INV) ? "!" : "",
314                         feature >> 5,
315                         feature & 0x1f,
316                         instr, instr, a->instrlen,
317                         replacement, a->replacementlen);
318
319                 DUMP_BYTES(instr, a->instrlen, "%px:   old_insn: ", instr);
320                 DUMP_BYTES(replacement, a->replacementlen, "%px:   rpl_insn: ", replacement);
321
322                 memcpy(insn_buff, replacement, a->replacementlen);
323                 insn_buff_sz = a->replacementlen;
324
325                 /*
326                  * 0xe8 is a relative jump; fix the offset.
327                  *
328                  * Instruction length is checked before the opcode to avoid
329                  * accessing uninitialized bytes for zero-length replacements.
330                  */
331                 if (a->replacementlen == 5 && *insn_buff == 0xe8) {
332                         *(s32 *)(insn_buff + 1) += replacement - instr;
333                         DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
334                                 *(s32 *)(insn_buff + 1),
335                                 (unsigned long)instr + *(s32 *)(insn_buff + 1) + 5);
336                 }
337
338                 if (a->replacementlen && is_jmp(replacement[0]))
339                         recompute_jump(a, instr, replacement, insn_buff);
340
341                 for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
342                         insn_buff[insn_buff_sz] = 0x90;
343
344                 DUMP_BYTES(insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
345
346                 text_poke_early(instr, insn_buff, insn_buff_sz);
347
348 next:
349                 optimize_nops(instr, a->instrlen);
350         }
351
352         kasan_enable_current();
353 }
354
355 static inline bool is_jcc32(struct insn *insn)
356 {
357         /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */
358         return insn->opcode.bytes[0] == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80;
359 }
360
361 #if defined(CONFIG_RETPOLINE) && defined(CONFIG_OBJTOOL)
362
363 /*
364  * CALL/JMP *%\reg
365  */
366 static int emit_indirect(int op, int reg, u8 *bytes)
367 {
368         int i = 0;
369         u8 modrm;
370
371         switch (op) {
372         case CALL_INSN_OPCODE:
373                 modrm = 0x10; /* Reg = 2; CALL r/m */
374                 break;
375
376         case JMP32_INSN_OPCODE:
377                 modrm = 0x20; /* Reg = 4; JMP r/m */
378                 break;
379
380         default:
381                 WARN_ON_ONCE(1);
382                 return -1;
383         }
384
385         if (reg >= 8) {
386                 bytes[i++] = 0x41; /* REX.B prefix */
387                 reg -= 8;
388         }
389
390         modrm |= 0xc0; /* Mod = 3 */
391         modrm += reg;
392
393         bytes[i++] = 0xff; /* opcode */
394         bytes[i++] = modrm;
395
396         return i;
397 }
398
399 /*
400  * Rewrite the compiler generated retpoline thunk calls.
401  *
402  * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate
403  * indirect instructions, avoiding the extra indirection.
404  *
405  * For example, convert:
406  *
407  *   CALL __x86_indirect_thunk_\reg
408  *
409  * into:
410  *
411  *   CALL *%\reg
412  *
413  * It also tries to inline spectre_v2=retpoline,lfence when size permits.
414  */
415 static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes)
416 {
417         retpoline_thunk_t *target;
418         int reg, ret, i = 0;
419         u8 op, cc;
420
421         target = addr + insn->length + insn->immediate.value;
422         reg = target - __x86_indirect_thunk_array;
423
424         if (WARN_ON_ONCE(reg & ~0xf))
425                 return -1;
426
427         /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */
428         BUG_ON(reg == 4);
429
430         if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) &&
431             !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE))
432                 return -1;
433
434         op = insn->opcode.bytes[0];
435
436         /*
437          * Convert:
438          *
439          *   Jcc.d32 __x86_indirect_thunk_\reg
440          *
441          * into:
442          *
443          *   Jncc.d8 1f
444          *   [ LFENCE ]
445          *   JMP *%\reg
446          *   [ NOP ]
447          * 1:
448          */
449         if (is_jcc32(insn)) {
450                 cc = insn->opcode.bytes[1] & 0xf;
451                 cc ^= 1; /* invert condition */
452
453                 bytes[i++] = 0x70 + cc;        /* Jcc.d8 */
454                 bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */
455
456                 /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */
457                 op = JMP32_INSN_OPCODE;
458         }
459
460         /*
461          * For RETPOLINE_LFENCE: prepend the indirect CALL/JMP with an LFENCE.
462          */
463         if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) {
464                 bytes[i++] = 0x0f;
465                 bytes[i++] = 0xae;
466                 bytes[i++] = 0xe8; /* LFENCE */
467         }
468
469         ret = emit_indirect(op, reg, bytes + i);
470         if (ret < 0)
471                 return ret;
472         i += ret;
473
474         /*
475          * The compiler is supposed to EMIT an INT3 after every unconditional
476          * JMP instruction due to AMD BTC. However, if the compiler is too old
477          * or SLS isn't enabled, we still need an INT3 after indirect JMPs
478          * even on Intel.
479          */
480         if (op == JMP32_INSN_OPCODE && i < insn->length)
481                 bytes[i++] = INT3_INSN_OPCODE;
482
483         for (; i < insn->length;)
484                 bytes[i++] = BYTES_NOP1;
485
486         return i;
487 }
488
489 /*
490  * Generated by 'objtool --retpoline'.
491  */
492 void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
493 {
494         s32 *s;
495
496         for (s = start; s < end; s++) {
497                 void *addr = (void *)s + *s;
498                 struct insn insn;
499                 int len, ret;
500                 u8 bytes[16];
501                 u8 op1, op2;
502
503                 ret = insn_decode_kernel(&insn, addr);
504                 if (WARN_ON_ONCE(ret < 0))
505                         continue;
506
507                 op1 = insn.opcode.bytes[0];
508                 op2 = insn.opcode.bytes[1];
509
510                 switch (op1) {
511                 case CALL_INSN_OPCODE:
512                 case JMP32_INSN_OPCODE:
513                         break;
514
515                 case 0x0f: /* escape */
516                         if (op2 >= 0x80 && op2 <= 0x8f)
517                                 break;
518                         fallthrough;
519                 default:
520                         WARN_ON_ONCE(1);
521                         continue;
522                 }
523
524                 DPRINTK("retpoline at: %pS (%px) len: %d to: %pS",
525                         addr, addr, insn.length,
526                         addr + insn.length + insn.immediate.value);
527
528                 len = patch_retpoline(addr, &insn, bytes);
529                 if (len == insn.length) {
530                         optimize_nops(bytes, len);
531                         DUMP_BYTES(((u8*)addr),  len, "%px: orig: ", addr);
532                         DUMP_BYTES(((u8*)bytes), len, "%px: repl: ", addr);
533                         text_poke_early(addr, bytes, len);
534                 }
535         }
536 }
537
538 #ifdef CONFIG_RETHUNK
539
540 /*
541  * Rewrite the compiler generated return thunk tail-calls.
542  *
543  * For example, convert:
544  *
545  *   JMP __x86_return_thunk
546  *
547  * into:
548  *
549  *   RET
550  */
551 static int patch_return(void *addr, struct insn *insn, u8 *bytes)
552 {
553         int i = 0;
554
555         if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
556                 if (x86_return_thunk == __x86_return_thunk)
557                         return -1;
558
559                 i = JMP32_INSN_SIZE;
560                 __text_gen_insn(bytes, JMP32_INSN_OPCODE, addr, x86_return_thunk, i);
561         } else {
562                 bytes[i++] = RET_INSN_OPCODE;
563         }
564
565         for (; i < insn->length;)
566                 bytes[i++] = INT3_INSN_OPCODE;
567         return i;
568 }
569
570 void __init_or_module noinline apply_returns(s32 *start, s32 *end)
571 {
572         s32 *s;
573
574         for (s = start; s < end; s++) {
575                 void *dest = NULL, *addr = (void *)s + *s;
576                 struct insn insn;
577                 int len, ret;
578                 u8 bytes[16];
579                 u8 op;
580
581                 ret = insn_decode_kernel(&insn, addr);
582                 if (WARN_ON_ONCE(ret < 0))
583                         continue;
584
585                 op = insn.opcode.bytes[0];
586                 if (op == JMP32_INSN_OPCODE)
587                         dest = addr + insn.length + insn.immediate.value;
588
589                 if (__static_call_fixup(addr, op, dest) ||
590                     WARN_ONCE(dest != &__x86_return_thunk,
591                               "missing return thunk: %pS-%pS: %*ph",
592                               addr, dest, 5, addr))
593                         continue;
594
595                 DPRINTK("return thunk at: %pS (%px) len: %d to: %pS",
596                         addr, addr, insn.length,
597                         addr + insn.length + insn.immediate.value);
598
599                 len = patch_return(addr, &insn, bytes);
600                 if (len == insn.length) {
601                         DUMP_BYTES(((u8*)addr),  len, "%px: orig: ", addr);
602                         DUMP_BYTES(((u8*)bytes), len, "%px: repl: ", addr);
603                         text_poke_early(addr, bytes, len);
604                 }
605         }
606 }
607 #else
608 void __init_or_module noinline apply_returns(s32 *start, s32 *end) { }
609 #endif /* CONFIG_RETHUNK */
610
611 #else /* !CONFIG_RETPOLINE || !CONFIG_OBJTOOL */
612
613 void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { }
614 void __init_or_module noinline apply_returns(s32 *start, s32 *end) { }
615
616 #endif /* CONFIG_RETPOLINE && CONFIG_OBJTOOL */
617
618 #ifdef CONFIG_X86_KERNEL_IBT
619
620 /*
621  * Generated by: objtool --ibt
622  */
623 void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end)
624 {
625         s32 *s;
626
627         for (s = start; s < end; s++) {
628                 u32 endbr, poison = gen_endbr_poison();
629                 void *addr = (void *)s + *s;
630
631                 if (WARN_ON_ONCE(get_kernel_nofault(endbr, addr)))
632                         continue;
633
634                 if (WARN_ON_ONCE(!is_endbr(endbr)))
635                         continue;
636
637                 DPRINTK("ENDBR at: %pS (%px)", addr, addr);
638
639                 /*
640                  * When we have IBT, the lack of ENDBR will trigger #CP
641                  */
642                 DUMP_BYTES(((u8*)addr), 4, "%px: orig: ", addr);
643                 DUMP_BYTES(((u8*)&poison), 4, "%px: repl: ", addr);
644                 text_poke_early(addr, &poison, 4);
645         }
646 }
647
648 #else
649
650 void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end) { }
651
652 #endif /* CONFIG_X86_KERNEL_IBT */
653
654 #ifdef CONFIG_SMP
655 static void alternatives_smp_lock(const s32 *start, const s32 *end,
656                                   u8 *text, u8 *text_end)
657 {
658         const s32 *poff;
659
660         for (poff = start; poff < end; poff++) {
661                 u8 *ptr = (u8 *)poff + *poff;
662
663                 if (!*poff || ptr < text || ptr >= text_end)
664                         continue;
665                 /* turn DS segment override prefix into lock prefix */
666                 if (*ptr == 0x3e)
667                         text_poke(ptr, ((unsigned char []){0xf0}), 1);
668         }
669 }
670
671 static void alternatives_smp_unlock(const s32 *start, const s32 *end,
672                                     u8 *text, u8 *text_end)
673 {
674         const s32 *poff;
675
676         for (poff = start; poff < end; poff++) {
677                 u8 *ptr = (u8 *)poff + *poff;
678
679                 if (!*poff || ptr < text || ptr >= text_end)
680                         continue;
681                 /* turn lock prefix into DS segment override prefix */
682                 if (*ptr == 0xf0)
683                         text_poke(ptr, ((unsigned char []){0x3E}), 1);
684         }
685 }
686
687 struct smp_alt_module {
688         /* what is this ??? */
689         struct module   *mod;
690         char            *name;
691
692         /* ptrs to lock prefixes */
693         const s32       *locks;
694         const s32       *locks_end;
695
696         /* .text segment, needed to avoid patching init code ;) */
697         u8              *text;
698         u8              *text_end;
699
700         struct list_head next;
701 };
702 static LIST_HEAD(smp_alt_modules);
703 static bool uniproc_patched = false;    /* protected by text_mutex */
704
705 void __init_or_module alternatives_smp_module_add(struct module *mod,
706                                                   char *name,
707                                                   void *locks, void *locks_end,
708                                                   void *text,  void *text_end)
709 {
710         struct smp_alt_module *smp;
711
712         mutex_lock(&text_mutex);
713         if (!uniproc_patched)
714                 goto unlock;
715
716         if (num_possible_cpus() == 1)
717                 /* Don't bother remembering, we'll never have to undo it. */
718                 goto smp_unlock;
719
720         smp = kzalloc(sizeof(*smp), GFP_KERNEL);
721         if (NULL == smp)
722                 /* we'll run the (safe but slow) SMP code then ... */
723                 goto unlock;
724
725         smp->mod        = mod;
726         smp->name       = name;
727         smp->locks      = locks;
728         smp->locks_end  = locks_end;
729         smp->text       = text;
730         smp->text_end   = text_end;
731         DPRINTK("locks %p -> %p, text %p -> %p, name %s\n",
732                 smp->locks, smp->locks_end,
733                 smp->text, smp->text_end, smp->name);
734
735         list_add_tail(&smp->next, &smp_alt_modules);
736 smp_unlock:
737         alternatives_smp_unlock(locks, locks_end, text, text_end);
738 unlock:
739         mutex_unlock(&text_mutex);
740 }
741
742 void __init_or_module alternatives_smp_module_del(struct module *mod)
743 {
744         struct smp_alt_module *item;
745
746         mutex_lock(&text_mutex);
747         list_for_each_entry(item, &smp_alt_modules, next) {
748                 if (mod != item->mod)
749                         continue;
750                 list_del(&item->next);
751                 kfree(item);
752                 break;
753         }
754         mutex_unlock(&text_mutex);
755 }
756
757 void alternatives_enable_smp(void)
758 {
759         struct smp_alt_module *mod;
760
761         /* Why bother if there are no other CPUs? */
762         BUG_ON(num_possible_cpus() == 1);
763
764         mutex_lock(&text_mutex);
765
766         if (uniproc_patched) {
767                 pr_info("switching to SMP code\n");
768                 BUG_ON(num_online_cpus() != 1);
769                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
770                 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
771                 list_for_each_entry(mod, &smp_alt_modules, next)
772                         alternatives_smp_lock(mod->locks, mod->locks_end,
773                                               mod->text, mod->text_end);
774                 uniproc_patched = false;
775         }
776         mutex_unlock(&text_mutex);
777 }
778
779 /*
780  * Return 1 if the address range is reserved for SMP-alternatives.
781  * Must hold text_mutex.
782  */
783 int alternatives_text_reserved(void *start, void *end)
784 {
785         struct smp_alt_module *mod;
786         const s32 *poff;
787         u8 *text_start = start;
788         u8 *text_end = end;
789
790         lockdep_assert_held(&text_mutex);
791
792         list_for_each_entry(mod, &smp_alt_modules, next) {
793                 if (mod->text > text_end || mod->text_end < text_start)
794                         continue;
795                 for (poff = mod->locks; poff < mod->locks_end; poff++) {
796                         const u8 *ptr = (const u8 *)poff + *poff;
797
798                         if (text_start <= ptr && text_end > ptr)
799                                 return 1;
800                 }
801         }
802
803         return 0;
804 }
805 #endif /* CONFIG_SMP */
806
807 #ifdef CONFIG_PARAVIRT
808 void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
809                                      struct paravirt_patch_site *end)
810 {
811         struct paravirt_patch_site *p;
812         char insn_buff[MAX_PATCH_LEN];
813
814         for (p = start; p < end; p++) {
815                 unsigned int used;
816
817                 BUG_ON(p->len > MAX_PATCH_LEN);
818                 /* prep the buffer with the original instructions */
819                 memcpy(insn_buff, p->instr, p->len);
820                 used = paravirt_patch(p->type, insn_buff, (unsigned long)p->instr, p->len);
821
822                 BUG_ON(used > p->len);
823
824                 /* Pad the rest with nops */
825                 add_nops(insn_buff + used, p->len - used);
826                 text_poke_early(p->instr, insn_buff, p->len);
827         }
828 }
829 extern struct paravirt_patch_site __start_parainstructions[],
830         __stop_parainstructions[];
831 #endif  /* CONFIG_PARAVIRT */
832
833 /*
834  * Self-test for the INT3 based CALL emulation code.
835  *
836  * This exercises int3_emulate_call() to make sure INT3 pt_regs are set up
837  * properly and that there is a stack gap between the INT3 frame and the
838  * previous context. Without this gap doing a virtual PUSH on the interrupted
839  * stack would corrupt the INT3 IRET frame.
840  *
841  * See entry_{32,64}.S for more details.
842  */
843
844 /*
845  * We define the int3_magic() function in assembly to control the calling
846  * convention such that we can 'call' it from assembly.
847  */
848
849 extern void int3_magic(unsigned int *ptr); /* defined in asm */
850
851 asm (
852 "       .pushsection    .init.text, \"ax\", @progbits\n"
853 "       .type           int3_magic, @function\n"
854 "int3_magic:\n"
855         ANNOTATE_NOENDBR
856 "       movl    $1, (%" _ASM_ARG1 ")\n"
857         ASM_RET
858 "       .size           int3_magic, .-int3_magic\n"
859 "       .popsection\n"
860 );
861
862 extern void int3_selftest_ip(void); /* defined in asm below */
863
864 static int __init
865 int3_exception_notify(struct notifier_block *self, unsigned long val, void *data)
866 {
867         unsigned long selftest = (unsigned long)&int3_selftest_ip;
868         struct die_args *args = data;
869         struct pt_regs *regs = args->regs;
870
871         OPTIMIZER_HIDE_VAR(selftest);
872
873         if (!regs || user_mode(regs))
874                 return NOTIFY_DONE;
875
876         if (val != DIE_INT3)
877                 return NOTIFY_DONE;
878
879         if (regs->ip - INT3_INSN_SIZE != selftest)
880                 return NOTIFY_DONE;
881
882         int3_emulate_call(regs, (unsigned long)&int3_magic);
883         return NOTIFY_STOP;
884 }
885
886 /* Must be noinline to ensure uniqueness of int3_selftest_ip. */
887 static noinline void __init int3_selftest(void)
888 {
889         static __initdata struct notifier_block int3_exception_nb = {
890                 .notifier_call  = int3_exception_notify,
891                 .priority       = INT_MAX-1, /* last */
892         };
893         unsigned int val = 0;
894
895         BUG_ON(register_die_notifier(&int3_exception_nb));
896
897         /*
898          * Basically: int3_magic(&val); but really complicated :-)
899          *
900          * INT3 padded with NOP to CALL_INSN_SIZE. The int3_exception_nb
901          * notifier above will emulate CALL for us.
902          */
903         asm volatile ("int3_selftest_ip:\n\t"
904                       ANNOTATE_NOENDBR
905                       "    int3; nop; nop; nop; nop\n\t"
906                       : ASM_CALL_CONSTRAINT
907                       : __ASM_SEL_RAW(a, D) (&val)
908                       : "memory");
909
910         BUG_ON(val != 1);
911
912         unregister_die_notifier(&int3_exception_nb);
913 }
914
915 void __init alternative_instructions(void)
916 {
917         int3_selftest();
918
919         /*
920          * The patching is not fully atomic, so try to avoid local
921          * interruptions that might execute the to be patched code.
922          * Other CPUs are not running.
923          */
924         stop_nmi();
925
926         /*
927          * Don't stop machine check exceptions while patching.
928          * MCEs only happen when something got corrupted and in this
929          * case we must do something about the corruption.
930          * Ignoring it is worse than an unlikely patching race.
931          * Also machine checks tend to be broadcast and if one CPU
932          * goes into machine check the others follow quickly, so we don't
933          * expect a machine check to cause undue problems during to code
934          * patching.
935          */
936
937         /*
938          * Paravirt patching and alternative patching can be combined to
939          * replace a function call with a short direct code sequence (e.g.
940          * by setting a constant return value instead of doing that in an
941          * external function).
942          * In order to make this work the following sequence is required:
943          * 1. set (artificial) features depending on used paravirt
944          *    functions which can later influence alternative patching
945          * 2. apply paravirt patching (generally replacing an indirect
946          *    function call with a direct one)
947          * 3. apply alternative patching (e.g. replacing a direct function
948          *    call with a custom code sequence)
949          * Doing paravirt patching after alternative patching would clobber
950          * the optimization of the custom code with a function call again.
951          */
952         paravirt_set_cap();
953
954         /*
955          * First patch paravirt functions, such that we overwrite the indirect
956          * call with the direct call.
957          */
958         apply_paravirt(__parainstructions, __parainstructions_end);
959
960         /*
961          * Rewrite the retpolines, must be done before alternatives since
962          * those can rewrite the retpoline thunks.
963          */
964         apply_retpolines(__retpoline_sites, __retpoline_sites_end);
965         apply_returns(__return_sites, __return_sites_end);
966
967         /*
968          * Then patch alternatives, such that those paravirt calls that are in
969          * alternatives can be overwritten by their immediate fragments.
970          */
971         apply_alternatives(__alt_instructions, __alt_instructions_end);
972
973         apply_ibt_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end);
974
975 #ifdef CONFIG_SMP
976         /* Patch to UP if other cpus not imminent. */
977         if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
978                 uniproc_patched = true;
979                 alternatives_smp_module_add(NULL, "core kernel",
980                                             __smp_locks, __smp_locks_end,
981                                             _text, _etext);
982         }
983
984         if (!uniproc_patched || num_possible_cpus() == 1) {
985                 free_init_pages("SMP alternatives",
986                                 (unsigned long)__smp_locks,
987                                 (unsigned long)__smp_locks_end);
988         }
989 #endif
990
991         restart_nmi();
992         alternatives_patched = 1;
993 }
994
995 /**
996  * text_poke_early - Update instructions on a live kernel at boot time
997  * @addr: address to modify
998  * @opcode: source of the copy
999  * @len: length to copy
1000  *
1001  * When you use this code to patch more than one byte of an instruction
1002  * you need to make sure that other CPUs cannot execute this code in parallel.
1003  * Also no thread must be currently preempted in the middle of these
1004  * instructions. And on the local CPU you need to be protected against NMI or
1005  * MCE handlers seeing an inconsistent instruction while you patch.
1006  */
1007 void __init_or_module text_poke_early(void *addr, const void *opcode,
1008                                       size_t len)
1009 {
1010         unsigned long flags;
1011
1012         if (boot_cpu_has(X86_FEATURE_NX) &&
1013             is_module_text_address((unsigned long)addr)) {
1014                 /*
1015                  * Modules text is marked initially as non-executable, so the
1016                  * code cannot be running and speculative code-fetches are
1017                  * prevented. Just change the code.
1018                  */
1019                 memcpy(addr, opcode, len);
1020         } else {
1021                 local_irq_save(flags);
1022                 memcpy(addr, opcode, len);
1023                 sync_core();
1024                 local_irq_restore(flags);
1025
1026                 /*
1027                  * Could also do a CLFLUSH here to speed up CPU recovery; but
1028                  * that causes hangs on some VIA CPUs.
1029                  */
1030         }
1031 }
1032
1033 typedef struct {
1034         struct mm_struct *mm;
1035 } temp_mm_state_t;
1036
1037 /*
1038  * Using a temporary mm allows to set temporary mappings that are not accessible
1039  * by other CPUs. Such mappings are needed to perform sensitive memory writes
1040  * that override the kernel memory protections (e.g., W^X), without exposing the
1041  * temporary page-table mappings that are required for these write operations to
1042  * other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the
1043  * mapping is torn down.
1044  *
1045  * Context: The temporary mm needs to be used exclusively by a single core. To
1046  *          harden security IRQs must be disabled while the temporary mm is
1047  *          loaded, thereby preventing interrupt handler bugs from overriding
1048  *          the kernel memory protection.
1049  */
1050 static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm)
1051 {
1052         temp_mm_state_t temp_state;
1053
1054         lockdep_assert_irqs_disabled();
1055
1056         /*
1057          * Make sure not to be in TLB lazy mode, as otherwise we'll end up
1058          * with a stale address space WITHOUT being in lazy mode after
1059          * restoring the previous mm.
1060          */
1061         if (this_cpu_read(cpu_tlbstate_shared.is_lazy))
1062                 leave_mm(smp_processor_id());
1063
1064         temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm);
1065         switch_mm_irqs_off(NULL, mm, current);
1066
1067         /*
1068          * If breakpoints are enabled, disable them while the temporary mm is
1069          * used. Userspace might set up watchpoints on addresses that are used
1070          * in the temporary mm, which would lead to wrong signals being sent or
1071          * crashes.
1072          *
1073          * Note that breakpoints are not disabled selectively, which also causes
1074          * kernel breakpoints (e.g., perf's) to be disabled. This might be
1075          * undesirable, but still seems reasonable as the code that runs in the
1076          * temporary mm should be short.
1077          */
1078         if (hw_breakpoint_active())
1079                 hw_breakpoint_disable();
1080
1081         return temp_state;
1082 }
1083
1084 static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
1085 {
1086         lockdep_assert_irqs_disabled();
1087         switch_mm_irqs_off(NULL, prev_state.mm, current);
1088
1089         /*
1090          * Restore the breakpoints if they were disabled before the temporary mm
1091          * was loaded.
1092          */
1093         if (hw_breakpoint_active())
1094                 hw_breakpoint_restore();
1095 }
1096
1097 __ro_after_init struct mm_struct *poking_mm;
1098 __ro_after_init unsigned long poking_addr;
1099
1100 static void text_poke_memcpy(void *dst, const void *src, size_t len)
1101 {
1102         memcpy(dst, src, len);
1103 }
1104
1105 static void text_poke_memset(void *dst, const void *src, size_t len)
1106 {
1107         int c = *(const int *)src;
1108
1109         memset(dst, c, len);
1110 }
1111
1112 typedef void text_poke_f(void *dst, const void *src, size_t len);
1113
1114 static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t len)
1115 {
1116         bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE;
1117         struct page *pages[2] = {NULL};
1118         temp_mm_state_t prev;
1119         unsigned long flags;
1120         pte_t pte, *ptep;
1121         spinlock_t *ptl;
1122         pgprot_t pgprot;
1123
1124         /*
1125          * While boot memory allocator is running we cannot use struct pages as
1126          * they are not yet initialized. There is no way to recover.
1127          */
1128         BUG_ON(!after_bootmem);
1129
1130         if (!core_kernel_text((unsigned long)addr)) {
1131                 pages[0] = vmalloc_to_page(addr);
1132                 if (cross_page_boundary)
1133                         pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
1134         } else {
1135                 pages[0] = virt_to_page(addr);
1136                 WARN_ON(!PageReserved(pages[0]));
1137                 if (cross_page_boundary)
1138                         pages[1] = virt_to_page(addr + PAGE_SIZE);
1139         }
1140         /*
1141          * If something went wrong, crash and burn since recovery paths are not
1142          * implemented.
1143          */
1144         BUG_ON(!pages[0] || (cross_page_boundary && !pages[1]));
1145
1146         /*
1147          * Map the page without the global bit, as TLB flushing is done with
1148          * flush_tlb_mm_range(), which is intended for non-global PTEs.
1149          */
1150         pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL);
1151
1152         /*
1153          * The lock is not really needed, but this allows to avoid open-coding.
1154          */
1155         ptep = get_locked_pte(poking_mm, poking_addr, &ptl);
1156
1157         /*
1158          * This must not fail; preallocated in poking_init().
1159          */
1160         VM_BUG_ON(!ptep);
1161
1162         local_irq_save(flags);
1163
1164         pte = mk_pte(pages[0], pgprot);
1165         set_pte_at(poking_mm, poking_addr, ptep, pte);
1166
1167         if (cross_page_boundary) {
1168                 pte = mk_pte(pages[1], pgprot);
1169                 set_pte_at(poking_mm, poking_addr + PAGE_SIZE, ptep + 1, pte);
1170         }
1171
1172         /*
1173          * Loading the temporary mm behaves as a compiler barrier, which
1174          * guarantees that the PTE will be set at the time memcpy() is done.
1175          */
1176         prev = use_temporary_mm(poking_mm);
1177
1178         kasan_disable_current();
1179         func((u8 *)poking_addr + offset_in_page(addr), src, len);
1180         kasan_enable_current();
1181
1182         /*
1183          * Ensure that the PTE is only cleared after the instructions of memcpy
1184          * were issued by using a compiler barrier.
1185          */
1186         barrier();
1187
1188         pte_clear(poking_mm, poking_addr, ptep);
1189         if (cross_page_boundary)
1190                 pte_clear(poking_mm, poking_addr + PAGE_SIZE, ptep + 1);
1191
1192         /*
1193          * Loading the previous page-table hierarchy requires a serializing
1194          * instruction that already allows the core to see the updated version.
1195          * Xen-PV is assumed to serialize execution in a similar manner.
1196          */
1197         unuse_temporary_mm(prev);
1198
1199         /*
1200          * Flushing the TLB might involve IPIs, which would require enabled
1201          * IRQs, but not if the mm is not used, as it is in this point.
1202          */
1203         flush_tlb_mm_range(poking_mm, poking_addr, poking_addr +
1204                            (cross_page_boundary ? 2 : 1) * PAGE_SIZE,
1205                            PAGE_SHIFT, false);
1206
1207         if (func == text_poke_memcpy) {
1208                 /*
1209                  * If the text does not match what we just wrote then something is
1210                  * fundamentally screwy; there's nothing we can really do about that.
1211                  */
1212                 BUG_ON(memcmp(addr, src, len));
1213         }
1214
1215         local_irq_restore(flags);
1216         pte_unmap_unlock(ptep, ptl);
1217         return addr;
1218 }
1219
1220 /**
1221  * text_poke - Update instructions on a live kernel
1222  * @addr: address to modify
1223  * @opcode: source of the copy
1224  * @len: length to copy
1225  *
1226  * Only atomic text poke/set should be allowed when not doing early patching.
1227  * It means the size must be writable atomically and the address must be aligned
1228  * in a way that permits an atomic write. It also makes sure we fit on a single
1229  * page.
1230  *
1231  * Note that the caller must ensure that if the modified code is part of a
1232  * module, the module would not be removed during poking. This can be achieved
1233  * by registering a module notifier, and ordering module removal and patching
1234  * trough a mutex.
1235  */
1236 void *text_poke(void *addr, const void *opcode, size_t len)
1237 {
1238         lockdep_assert_held(&text_mutex);
1239
1240         return __text_poke(text_poke_memcpy, addr, opcode, len);
1241 }
1242
1243 /**
1244  * text_poke_kgdb - Update instructions on a live kernel by kgdb
1245  * @addr: address to modify
1246  * @opcode: source of the copy
1247  * @len: length to copy
1248  *
1249  * Only atomic text poke/set should be allowed when not doing early patching.
1250  * It means the size must be writable atomically and the address must be aligned
1251  * in a way that permits an atomic write. It also makes sure we fit on a single
1252  * page.
1253  *
1254  * Context: should only be used by kgdb, which ensures no other core is running,
1255  *          despite the fact it does not hold the text_mutex.
1256  */
1257 void *text_poke_kgdb(void *addr, const void *opcode, size_t len)
1258 {
1259         return __text_poke(text_poke_memcpy, addr, opcode, len);
1260 }
1261
1262 /**
1263  * text_poke_copy - Copy instructions into (an unused part of) RX memory
1264  * @addr: address to modify
1265  * @opcode: source of the copy
1266  * @len: length to copy, could be more than 2x PAGE_SIZE
1267  *
1268  * Not safe against concurrent execution; useful for JITs to dump
1269  * new code blocks into unused regions of RX memory. Can be used in
1270  * conjunction with synchronize_rcu_tasks() to wait for existing
1271  * execution to quiesce after having made sure no existing functions
1272  * pointers are live.
1273  */
1274 void *text_poke_copy(void *addr, const void *opcode, size_t len)
1275 {
1276         unsigned long start = (unsigned long)addr;
1277         size_t patched = 0;
1278
1279         if (WARN_ON_ONCE(core_kernel_text(start)))
1280                 return NULL;
1281
1282         mutex_lock(&text_mutex);
1283         while (patched < len) {
1284                 unsigned long ptr = start + patched;
1285                 size_t s;
1286
1287                 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
1288
1289                 __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s);
1290                 patched += s;
1291         }
1292         mutex_unlock(&text_mutex);
1293         return addr;
1294 }
1295
1296 /**
1297  * text_poke_set - memset into (an unused part of) RX memory
1298  * @addr: address to modify
1299  * @c: the byte to fill the area with
1300  * @len: length to copy, could be more than 2x PAGE_SIZE
1301  *
1302  * This is useful to overwrite unused regions of RX memory with illegal
1303  * instructions.
1304  */
1305 void *text_poke_set(void *addr, int c, size_t len)
1306 {
1307         unsigned long start = (unsigned long)addr;
1308         size_t patched = 0;
1309
1310         if (WARN_ON_ONCE(core_kernel_text(start)))
1311                 return NULL;
1312
1313         mutex_lock(&text_mutex);
1314         while (patched < len) {
1315                 unsigned long ptr = start + patched;
1316                 size_t s;
1317
1318                 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
1319
1320                 __text_poke(text_poke_memset, (void *)ptr, (void *)&c, s);
1321                 patched += s;
1322         }
1323         mutex_unlock(&text_mutex);
1324         return addr;
1325 }
1326
1327 static void do_sync_core(void *info)
1328 {
1329         sync_core();
1330 }
1331
1332 void text_poke_sync(void)
1333 {
1334         on_each_cpu(do_sync_core, NULL, 1);
1335 }
1336
1337 /*
1338  * NOTE: crazy scheme to allow patching Jcc.d32 but not increase the size of
1339  * this thing. When len == 6 everything is prefixed with 0x0f and we map
1340  * opcode to Jcc.d8, using len to distinguish.
1341  */
1342 struct text_poke_loc {
1343         /* addr := _stext + rel_addr */
1344         s32 rel_addr;
1345         s32 disp;
1346         u8 len;
1347         u8 opcode;
1348         const u8 text[POKE_MAX_OPCODE_SIZE];
1349         /* see text_poke_bp_batch() */
1350         u8 old;
1351 };
1352
1353 struct bp_patching_desc {
1354         struct text_poke_loc *vec;
1355         int nr_entries;
1356         atomic_t refs;
1357 };
1358
1359 static struct bp_patching_desc bp_desc;
1360
1361 static __always_inline
1362 struct bp_patching_desc *try_get_desc(void)
1363 {
1364         struct bp_patching_desc *desc = &bp_desc;
1365
1366         if (!arch_atomic_inc_not_zero(&desc->refs))
1367                 return NULL;
1368
1369         return desc;
1370 }
1371
1372 static __always_inline void put_desc(void)
1373 {
1374         struct bp_patching_desc *desc = &bp_desc;
1375
1376         smp_mb__before_atomic();
1377         arch_atomic_dec(&desc->refs);
1378 }
1379
1380 static __always_inline void *text_poke_addr(struct text_poke_loc *tp)
1381 {
1382         return _stext + tp->rel_addr;
1383 }
1384
1385 static __always_inline int patch_cmp(const void *key, const void *elt)
1386 {
1387         struct text_poke_loc *tp = (struct text_poke_loc *) elt;
1388
1389         if (key < text_poke_addr(tp))
1390                 return -1;
1391         if (key > text_poke_addr(tp))
1392                 return 1;
1393         return 0;
1394 }
1395
1396 noinstr int poke_int3_handler(struct pt_regs *regs)
1397 {
1398         struct bp_patching_desc *desc;
1399         struct text_poke_loc *tp;
1400         int ret = 0;
1401         void *ip;
1402
1403         if (user_mode(regs))
1404                 return 0;
1405
1406         /*
1407          * Having observed our INT3 instruction, we now must observe
1408          * bp_desc with non-zero refcount:
1409          *
1410          *      bp_desc.refs = 1                INT3
1411          *      WMB                             RMB
1412          *      write INT3                      if (bp_desc.refs != 0)
1413          */
1414         smp_rmb();
1415
1416         desc = try_get_desc();
1417         if (!desc)
1418                 return 0;
1419
1420         /*
1421          * Discount the INT3. See text_poke_bp_batch().
1422          */
1423         ip = (void *) regs->ip - INT3_INSN_SIZE;
1424
1425         /*
1426          * Skip the binary search if there is a single member in the vector.
1427          */
1428         if (unlikely(desc->nr_entries > 1)) {
1429                 tp = __inline_bsearch(ip, desc->vec, desc->nr_entries,
1430                                       sizeof(struct text_poke_loc),
1431                                       patch_cmp);
1432                 if (!tp)
1433                         goto out_put;
1434         } else {
1435                 tp = desc->vec;
1436                 if (text_poke_addr(tp) != ip)
1437                         goto out_put;
1438         }
1439
1440         ip += tp->len;
1441
1442         switch (tp->opcode) {
1443         case INT3_INSN_OPCODE:
1444                 /*
1445                  * Someone poked an explicit INT3, they'll want to handle it,
1446                  * do not consume.
1447                  */
1448                 goto out_put;
1449
1450         case RET_INSN_OPCODE:
1451                 int3_emulate_ret(regs);
1452                 break;
1453
1454         case CALL_INSN_OPCODE:
1455                 int3_emulate_call(regs, (long)ip + tp->disp);
1456                 break;
1457
1458         case JMP32_INSN_OPCODE:
1459         case JMP8_INSN_OPCODE:
1460                 int3_emulate_jmp(regs, (long)ip + tp->disp);
1461                 break;
1462
1463         case 0x70 ... 0x7f: /* Jcc */
1464                 int3_emulate_jcc(regs, tp->opcode & 0xf, (long)ip, tp->disp);
1465                 break;
1466
1467         default:
1468                 BUG();
1469         }
1470
1471         ret = 1;
1472
1473 out_put:
1474         put_desc();
1475         return ret;
1476 }
1477
1478 #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc))
1479 static struct text_poke_loc tp_vec[TP_VEC_MAX];
1480 static int tp_vec_nr;
1481
1482 /**
1483  * text_poke_bp_batch() -- update instructions on live kernel on SMP
1484  * @tp:                 vector of instructions to patch
1485  * @nr_entries:         number of entries in the vector
1486  *
1487  * Modify multi-byte instruction by using int3 breakpoint on SMP.
1488  * We completely avoid stop_machine() here, and achieve the
1489  * synchronization using int3 breakpoint.
1490  *
1491  * The way it is done:
1492  *      - For each entry in the vector:
1493  *              - add a int3 trap to the address that will be patched
1494  *      - sync cores
1495  *      - For each entry in the vector:
1496  *              - update all but the first byte of the patched range
1497  *      - sync cores
1498  *      - For each entry in the vector:
1499  *              - replace the first byte (int3) by the first byte of
1500  *                replacing opcode
1501  *      - sync cores
1502  */
1503 static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries)
1504 {
1505         unsigned char int3 = INT3_INSN_OPCODE;
1506         unsigned int i;
1507         int do_sync;
1508
1509         lockdep_assert_held(&text_mutex);
1510
1511         bp_desc.vec = tp;
1512         bp_desc.nr_entries = nr_entries;
1513
1514         /*
1515          * Corresponds to the implicit memory barrier in try_get_desc() to
1516          * ensure reading a non-zero refcount provides up to date bp_desc data.
1517          */
1518         atomic_set_release(&bp_desc.refs, 1);
1519
1520         /*
1521          * Corresponding read barrier in int3 notifier for making sure the
1522          * nr_entries and handler are correctly ordered wrt. patching.
1523          */
1524         smp_wmb();
1525
1526         /*
1527          * First step: add a int3 trap to the address that will be patched.
1528          */
1529         for (i = 0; i < nr_entries; i++) {
1530                 tp[i].old = *(u8 *)text_poke_addr(&tp[i]);
1531                 text_poke(text_poke_addr(&tp[i]), &int3, INT3_INSN_SIZE);
1532         }
1533
1534         text_poke_sync();
1535
1536         /*
1537          * Second step: update all but the first byte of the patched range.
1538          */
1539         for (do_sync = 0, i = 0; i < nr_entries; i++) {
1540                 u8 old[POKE_MAX_OPCODE_SIZE+1] = { tp[i].old, };
1541                 u8 _new[POKE_MAX_OPCODE_SIZE+1];
1542                 const u8 *new = tp[i].text;
1543                 int len = tp[i].len;
1544
1545                 if (len - INT3_INSN_SIZE > 0) {
1546                         memcpy(old + INT3_INSN_SIZE,
1547                                text_poke_addr(&tp[i]) + INT3_INSN_SIZE,
1548                                len - INT3_INSN_SIZE);
1549
1550                         if (len == 6) {
1551                                 _new[0] = 0x0f;
1552                                 memcpy(_new + 1, new, 5);
1553                                 new = _new;
1554                         }
1555
1556                         text_poke(text_poke_addr(&tp[i]) + INT3_INSN_SIZE,
1557                                   new + INT3_INSN_SIZE,
1558                                   len - INT3_INSN_SIZE);
1559
1560                         do_sync++;
1561                 }
1562
1563                 /*
1564                  * Emit a perf event to record the text poke, primarily to
1565                  * support Intel PT decoding which must walk the executable code
1566                  * to reconstruct the trace. The flow up to here is:
1567                  *   - write INT3 byte
1568                  *   - IPI-SYNC
1569                  *   - write instruction tail
1570                  * At this point the actual control flow will be through the
1571                  * INT3 and handler and not hit the old or new instruction.
1572                  * Intel PT outputs FUP/TIP packets for the INT3, so the flow
1573                  * can still be decoded. Subsequently:
1574                  *   - emit RECORD_TEXT_POKE with the new instruction
1575                  *   - IPI-SYNC
1576                  *   - write first byte
1577                  *   - IPI-SYNC
1578                  * So before the text poke event timestamp, the decoder will see
1579                  * either the old instruction flow or FUP/TIP of INT3. After the
1580                  * text poke event timestamp, the decoder will see either the
1581                  * new instruction flow or FUP/TIP of INT3. Thus decoders can
1582                  * use the timestamp as the point at which to modify the
1583                  * executable code.
1584                  * The old instruction is recorded so that the event can be
1585                  * processed forwards or backwards.
1586                  */
1587                 perf_event_text_poke(text_poke_addr(&tp[i]), old, len, new, len);
1588         }
1589
1590         if (do_sync) {
1591                 /*
1592                  * According to Intel, this core syncing is very likely
1593                  * not necessary and we'd be safe even without it. But
1594                  * better safe than sorry (plus there's not only Intel).
1595                  */
1596                 text_poke_sync();
1597         }
1598
1599         /*
1600          * Third step: replace the first byte (int3) by the first byte of
1601          * replacing opcode.
1602          */
1603         for (do_sync = 0, i = 0; i < nr_entries; i++) {
1604                 u8 byte = tp[i].text[0];
1605
1606                 if (tp[i].len == 6)
1607                         byte = 0x0f;
1608
1609                 if (byte == INT3_INSN_OPCODE)
1610                         continue;
1611
1612                 text_poke(text_poke_addr(&tp[i]), &byte, INT3_INSN_SIZE);
1613                 do_sync++;
1614         }
1615
1616         if (do_sync)
1617                 text_poke_sync();
1618
1619         /*
1620          * Remove and wait for refs to be zero.
1621          */
1622         if (!atomic_dec_and_test(&bp_desc.refs))
1623                 atomic_cond_read_acquire(&bp_desc.refs, !VAL);
1624 }
1625
1626 static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
1627                                const void *opcode, size_t len, const void *emulate)
1628 {
1629         struct insn insn;
1630         int ret, i = 0;
1631
1632         if (len == 6)
1633                 i = 1;
1634         memcpy((void *)tp->text, opcode+i, len-i);
1635         if (!emulate)
1636                 emulate = opcode;
1637
1638         ret = insn_decode_kernel(&insn, emulate);
1639         BUG_ON(ret < 0);
1640
1641         tp->rel_addr = addr - (void *)_stext;
1642         tp->len = len;
1643         tp->opcode = insn.opcode.bytes[0];
1644
1645         if (is_jcc32(&insn)) {
1646                 /*
1647                  * Map Jcc.d32 onto Jcc.d8 and use len to distinguish.
1648                  */
1649                 tp->opcode = insn.opcode.bytes[1] - 0x10;
1650         }
1651
1652         switch (tp->opcode) {
1653         case RET_INSN_OPCODE:
1654         case JMP32_INSN_OPCODE:
1655         case JMP8_INSN_OPCODE:
1656                 /*
1657                  * Control flow instructions without implied execution of the
1658                  * next instruction can be padded with INT3.
1659                  */
1660                 for (i = insn.length; i < len; i++)
1661                         BUG_ON(tp->text[i] != INT3_INSN_OPCODE);
1662                 break;
1663
1664         default:
1665                 BUG_ON(len != insn.length);
1666         };
1667
1668         switch (tp->opcode) {
1669         case INT3_INSN_OPCODE:
1670         case RET_INSN_OPCODE:
1671                 break;
1672
1673         case CALL_INSN_OPCODE:
1674         case JMP32_INSN_OPCODE:
1675         case JMP8_INSN_OPCODE:
1676         case 0x70 ... 0x7f: /* Jcc */
1677                 tp->disp = insn.immediate.value;
1678                 break;
1679
1680         default: /* assume NOP */
1681                 switch (len) {
1682                 case 2: /* NOP2 -- emulate as JMP8+0 */
1683                         BUG_ON(memcmp(emulate, x86_nops[len], len));
1684                         tp->opcode = JMP8_INSN_OPCODE;
1685                         tp->disp = 0;
1686                         break;
1687
1688                 case 5: /* NOP5 -- emulate as JMP32+0 */
1689                         BUG_ON(memcmp(emulate, x86_nops[len], len));
1690                         tp->opcode = JMP32_INSN_OPCODE;
1691                         tp->disp = 0;
1692                         break;
1693
1694                 default: /* unknown instruction */
1695                         BUG();
1696                 }
1697                 break;
1698         }
1699 }
1700
1701 /*
1702  * We hard rely on the tp_vec being ordered; ensure this is so by flushing
1703  * early if needed.
1704  */
1705 static bool tp_order_fail(void *addr)
1706 {
1707         struct text_poke_loc *tp;
1708
1709         if (!tp_vec_nr)
1710                 return false;
1711
1712         if (!addr) /* force */
1713                 return true;
1714
1715         tp = &tp_vec[tp_vec_nr - 1];
1716         if ((unsigned long)text_poke_addr(tp) > (unsigned long)addr)
1717                 return true;
1718
1719         return false;
1720 }
1721
1722 static void text_poke_flush(void *addr)
1723 {
1724         if (tp_vec_nr == TP_VEC_MAX || tp_order_fail(addr)) {
1725                 text_poke_bp_batch(tp_vec, tp_vec_nr);
1726                 tp_vec_nr = 0;
1727         }
1728 }
1729
1730 void text_poke_finish(void)
1731 {
1732         text_poke_flush(NULL);
1733 }
1734
1735 void __ref text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate)
1736 {
1737         struct text_poke_loc *tp;
1738
1739         if (unlikely(system_state == SYSTEM_BOOTING)) {
1740                 text_poke_early(addr, opcode, len);
1741                 return;
1742         }
1743
1744         text_poke_flush(addr);
1745
1746         tp = &tp_vec[tp_vec_nr++];
1747         text_poke_loc_init(tp, addr, opcode, len, emulate);
1748 }
1749
1750 /**
1751  * text_poke_bp() -- update instructions on live kernel on SMP
1752  * @addr:       address to patch
1753  * @opcode:     opcode of new instruction
1754  * @len:        length to copy
1755  * @emulate:    instruction to be emulated
1756  *
1757  * Update a single instruction with the vector in the stack, avoiding
1758  * dynamically allocated memory. This function should be used when it is
1759  * not possible to allocate memory.
1760  */
1761 void __ref text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate)
1762 {
1763         struct text_poke_loc tp;
1764
1765         if (unlikely(system_state == SYSTEM_BOOTING)) {
1766                 text_poke_early(addr, opcode, len);
1767                 return;
1768         }
1769
1770         text_poke_loc_init(&tp, addr, opcode, len, emulate);
1771         text_poke_bp_batch(&tp, 1);
1772 }