1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "SMP alternatives: " fmt
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>
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>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
30 #include <asm/fixmap.h>
31 #include <asm/paravirt.h>
32 #include <asm/asm-prototypes.h>
34 int __read_mostly alternatives_patched;
36 EXPORT_SYMBOL_GPL(alternatives_patched);
38 #define MAX_PATCH_LEN (255-1)
40 static int __initdata_or_module debug_alternative;
42 static int __init debug_alt(char *str)
44 debug_alternative = 1;
47 __setup("debug-alternative", debug_alt);
49 static int noreplace_smp;
51 static int __init setup_noreplace_smp(char *str)
56 __setup("noreplace-smp", setup_noreplace_smp);
58 #define DPRINTK(fmt, args...) \
60 if (debug_alternative) \
61 printk(KERN_DEBUG pr_fmt(fmt) "\n", ##args); \
64 #define DUMP_BYTES(buf, len, fmt, args...) \
66 if (unlikely(debug_alternative)) { \
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]); \
79 static const unsigned char x86nops[] =
91 const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
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,
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)
108 unsigned int noplen = len;
109 if (noplen > ASM_NOP_MAX)
110 noplen = ASM_NOP_MAX;
111 memcpy(insns, x86_nops[noplen], noplen);
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);
125 * Are we looking at a near JMP with a 1 or 4-byte displacement.
127 static inline bool is_jmp(const u8 opcode)
129 return opcode == 0xeb || opcode == 0xe9;
132 static void __init_or_module
133 recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insn_buff)
135 u8 *next_rip, *tgt_rip;
139 if (a->replacementlen != 5)
142 o_dspl = *(s32 *)(insn_buff + 1);
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;
150 DPRINTK("target RIP: %px, new_displ: 0x%x", tgt_rip, n_dspl);
152 if (tgt_rip - orig_insn >= 0) {
153 if (n_dspl - 2 <= 127)
157 /* negative offset */
159 if (((n_dspl - 2) & 0xff) == (n_dspl - 2))
169 insn_buff[1] = (s8)n_dspl;
170 add_nops(insn_buff + 2, 3);
179 *(s32 *)&insn_buff[1] = n_dspl;
185 DPRINTK("final displ: 0x%08x, JMP 0x%lx",
186 n_dspl, (unsigned long)orig_insn + n_dspl + repl_len);
190 * optimize_nops_range() - Optimize a sequence of single byte NOPs (0x90)
192 * @instr: instruction byte stream
193 * @instrlen: length of the above
194 * @off: offset within @instr where the first NOP has been detected
196 * Return: number of NOPs found (and replaced).
198 static __always_inline int optimize_nops_range(u8 *instr, u8 instrlen, int off)
203 while (i < instrlen) {
204 if (instr[i] != 0x90)
215 local_irq_save(flags);
216 add_nops(instr + off, nnops);
217 local_irq_restore(flags);
219 DUMP_BYTES(instr, instrlen, "%px: [%d:%d) optimized NOPs: ", instr, off, i);
225 * "noinline" to cause control flow change and thus invalidate I$ and
226 * cause refetch after modification.
228 static void __init_or_module noinline optimize_nops(u8 *instr, size_t len)
234 * Jump over the non-NOP insns and optimize single-byte NOPs into bigger
238 if (insn_decode_kernel(&insn, &instr[i]))
242 * See if this and any potentially following NOPs can be
245 if (insn.length == 1 && insn.opcode.bytes[0] == 0x90)
246 i += optimize_nops_range(instr, len, i);
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
262 * Marked "noinline" to cause control flow change and thus insn cache
263 * to refetch changed I$ lines.
265 void __init_or_module noinline apply_alternatives(struct alt_instr *start,
266 struct alt_instr *end)
269 u8 *instr, *replacement;
270 u8 insn_buff[MAX_PATCH_LEN];
272 DPRINTK("alt table %px, -> %px", start, end);
274 * The scan order should be from start to end. A later scanned
275 * alternative code can overwrite previously scanned alternative code.
276 * Some kernel functions (e.g. memcpy, memset, etc) use this order to
279 * So be careful if you want to change the scan order to any other
282 for (a = start; a < end; a++) {
283 int insn_buff_sz = 0;
284 /* Mask away "NOT" flag bit for feature to test. */
285 u16 feature = a->cpuid & ~ALTINSTR_FLAG_INV;
287 instr = (u8 *)&a->instr_offset + a->instr_offset;
288 replacement = (u8 *)&a->repl_offset + a->repl_offset;
289 BUG_ON(a->instrlen > sizeof(insn_buff));
290 BUG_ON(feature >= (NCAPINTS + NBUGINTS) * 32);
294 * - feature is present
295 * - feature not present but ALTINSTR_FLAG_INV is set to mean,
296 * patch if feature is *NOT* present.
298 if (!boot_cpu_has(feature) == !(a->cpuid & ALTINSTR_FLAG_INV))
301 DPRINTK("feat: %s%d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d)",
302 (a->cpuid & ALTINSTR_FLAG_INV) ? "!" : "",
305 instr, instr, a->instrlen,
306 replacement, a->replacementlen);
308 DUMP_BYTES(instr, a->instrlen, "%px: old_insn: ", instr);
309 DUMP_BYTES(replacement, a->replacementlen, "%px: rpl_insn: ", replacement);
311 memcpy(insn_buff, replacement, a->replacementlen);
312 insn_buff_sz = a->replacementlen;
315 * 0xe8 is a relative jump; fix the offset.
317 * Instruction length is checked before the opcode to avoid
318 * accessing uninitialized bytes for zero-length replacements.
320 if (a->replacementlen == 5 && *insn_buff == 0xe8) {
321 *(s32 *)(insn_buff + 1) += replacement - instr;
322 DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
323 *(s32 *)(insn_buff + 1),
324 (unsigned long)instr + *(s32 *)(insn_buff + 1) + 5);
327 if (a->replacementlen && is_jmp(replacement[0]))
328 recompute_jump(a, instr, replacement, insn_buff);
330 for (; insn_buff_sz < a->instrlen; insn_buff_sz++)
331 insn_buff[insn_buff_sz] = 0x90;
333 DUMP_BYTES(insn_buff, insn_buff_sz, "%px: final_insn: ", instr);
335 text_poke_early(instr, insn_buff, insn_buff_sz);
338 optimize_nops(instr, a->instrlen);
342 #if defined(CONFIG_RETPOLINE) && defined(CONFIG_OBJTOOL)
347 static int emit_indirect(int op, int reg, u8 *bytes)
353 case CALL_INSN_OPCODE:
354 modrm = 0x10; /* Reg = 2; CALL r/m */
357 case JMP32_INSN_OPCODE:
358 modrm = 0x20; /* Reg = 4; JMP r/m */
367 bytes[i++] = 0x41; /* REX.B prefix */
371 modrm |= 0xc0; /* Mod = 3 */
374 bytes[i++] = 0xff; /* opcode */
381 * Rewrite the compiler generated retpoline thunk calls.
383 * For spectre_v2=off (!X86_FEATURE_RETPOLINE), rewrite them into immediate
384 * indirect instructions, avoiding the extra indirection.
386 * For example, convert:
388 * CALL __x86_indirect_thunk_\reg
394 * It also tries to inline spectre_v2=retpoline,lfence when size permits.
396 static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes)
398 retpoline_thunk_t *target;
402 target = addr + insn->length + insn->immediate.value;
403 reg = target - __x86_indirect_thunk_array;
405 if (WARN_ON_ONCE(reg & ~0xf))
408 /* If anyone ever does: CALL/JMP *%rsp, we're in deep trouble. */
411 if (cpu_feature_enabled(X86_FEATURE_RETPOLINE) &&
412 !cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE))
415 op = insn->opcode.bytes[0];
420 * Jcc.d32 __x86_indirect_thunk_\reg
430 /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */
431 if (op == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80) {
432 cc = insn->opcode.bytes[1] & 0xf;
433 cc ^= 1; /* invert condition */
435 bytes[i++] = 0x70 + cc; /* Jcc.d8 */
436 bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */
438 /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */
439 op = JMP32_INSN_OPCODE;
443 * For RETPOLINE_LFENCE: prepend the indirect CALL/JMP with an LFENCE.
445 if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) {
448 bytes[i++] = 0xe8; /* LFENCE */
451 ret = emit_indirect(op, reg, bytes + i);
456 for (; i < insn->length;)
457 bytes[i++] = BYTES_NOP1;
463 * Generated by 'objtool --retpoline'.
465 void __init_or_module noinline apply_retpolines(s32 *start, s32 *end)
469 for (s = start; s < end; s++) {
470 void *addr = (void *)s + *s;
476 ret = insn_decode_kernel(&insn, addr);
477 if (WARN_ON_ONCE(ret < 0))
480 op1 = insn.opcode.bytes[0];
481 op2 = insn.opcode.bytes[1];
484 case CALL_INSN_OPCODE:
485 case JMP32_INSN_OPCODE:
488 case 0x0f: /* escape */
489 if (op2 >= 0x80 && op2 <= 0x8f)
497 DPRINTK("retpoline at: %pS (%px) len: %d to: %pS",
498 addr, addr, insn.length,
499 addr + insn.length + insn.immediate.value);
501 len = patch_retpoline(addr, &insn, bytes);
502 if (len == insn.length) {
503 optimize_nops(bytes, len);
504 DUMP_BYTES(((u8*)addr), len, "%px: orig: ", addr);
505 DUMP_BYTES(((u8*)bytes), len, "%px: repl: ", addr);
506 text_poke_early(addr, bytes, len);
511 #ifdef CONFIG_RETHUNK
513 * Rewrite the compiler generated return thunk tail-calls.
515 * For example, convert:
517 * JMP __x86_return_thunk
523 static int patch_return(void *addr, struct insn *insn, u8 *bytes)
527 if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
530 bytes[i++] = RET_INSN_OPCODE;
532 for (; i < insn->length;)
533 bytes[i++] = INT3_INSN_OPCODE;
538 void __init_or_module noinline apply_returns(s32 *start, s32 *end)
542 for (s = start; s < end; s++) {
543 void *dest = NULL, *addr = (void *)s + *s;
549 ret = insn_decode_kernel(&insn, addr);
550 if (WARN_ON_ONCE(ret < 0))
553 op = insn.opcode.bytes[0];
554 if (op == JMP32_INSN_OPCODE)
555 dest = addr + insn.length + insn.immediate.value;
557 if (__static_call_fixup(addr, op, dest) ||
558 WARN_ONCE(dest != &__x86_return_thunk,
559 "missing return thunk: %pS-%pS: %*ph",
560 addr, dest, 5, addr))
563 DPRINTK("return thunk at: %pS (%px) len: %d to: %pS",
564 addr, addr, insn.length,
565 addr + insn.length + insn.immediate.value);
567 len = patch_return(addr, &insn, bytes);
568 if (len == insn.length) {
569 DUMP_BYTES(((u8*)addr), len, "%px: orig: ", addr);
570 DUMP_BYTES(((u8*)bytes), len, "%px: repl: ", addr);
571 text_poke_early(addr, bytes, len);
576 void __init_or_module noinline apply_returns(s32 *start, s32 *end) { }
577 #endif /* CONFIG_RETHUNK */
579 #else /* !CONFIG_RETPOLINE || !CONFIG_OBJTOOL */
581 void __init_or_module noinline apply_retpolines(s32 *start, s32 *end) { }
582 void __init_or_module noinline apply_returns(s32 *start, s32 *end) { }
584 #endif /* CONFIG_RETPOLINE && CONFIG_OBJTOOL */
586 #ifdef CONFIG_X86_KERNEL_IBT
589 * Generated by: objtool --ibt
591 void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end)
595 for (s = start; s < end; s++) {
596 u32 endbr, poison = gen_endbr_poison();
597 void *addr = (void *)s + *s;
599 if (WARN_ON_ONCE(get_kernel_nofault(endbr, addr)))
602 if (WARN_ON_ONCE(!is_endbr(endbr)))
605 DPRINTK("ENDBR at: %pS (%px)", addr, addr);
608 * When we have IBT, the lack of ENDBR will trigger #CP
610 DUMP_BYTES(((u8*)addr), 4, "%px: orig: ", addr);
611 DUMP_BYTES(((u8*)&poison), 4, "%px: repl: ", addr);
612 text_poke_early(addr, &poison, 4);
618 void __init_or_module noinline apply_ibt_endbr(s32 *start, s32 *end) { }
620 #endif /* CONFIG_X86_KERNEL_IBT */
623 static void alternatives_smp_lock(const s32 *start, const s32 *end,
624 u8 *text, u8 *text_end)
628 for (poff = start; poff < end; poff++) {
629 u8 *ptr = (u8 *)poff + *poff;
631 if (!*poff || ptr < text || ptr >= text_end)
633 /* turn DS segment override prefix into lock prefix */
635 text_poke(ptr, ((unsigned char []){0xf0}), 1);
639 static void alternatives_smp_unlock(const s32 *start, const s32 *end,
640 u8 *text, u8 *text_end)
644 for (poff = start; poff < end; poff++) {
645 u8 *ptr = (u8 *)poff + *poff;
647 if (!*poff || ptr < text || ptr >= text_end)
649 /* turn lock prefix into DS segment override prefix */
651 text_poke(ptr, ((unsigned char []){0x3E}), 1);
655 struct smp_alt_module {
656 /* what is this ??? */
660 /* ptrs to lock prefixes */
662 const s32 *locks_end;
664 /* .text segment, needed to avoid patching init code ;) */
668 struct list_head next;
670 static LIST_HEAD(smp_alt_modules);
671 static bool uniproc_patched = false; /* protected by text_mutex */
673 void __init_or_module alternatives_smp_module_add(struct module *mod,
675 void *locks, void *locks_end,
676 void *text, void *text_end)
678 struct smp_alt_module *smp;
680 mutex_lock(&text_mutex);
681 if (!uniproc_patched)
684 if (num_possible_cpus() == 1)
685 /* Don't bother remembering, we'll never have to undo it. */
688 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
690 /* we'll run the (safe but slow) SMP code then ... */
696 smp->locks_end = locks_end;
698 smp->text_end = text_end;
699 DPRINTK("locks %p -> %p, text %p -> %p, name %s\n",
700 smp->locks, smp->locks_end,
701 smp->text, smp->text_end, smp->name);
703 list_add_tail(&smp->next, &smp_alt_modules);
705 alternatives_smp_unlock(locks, locks_end, text, text_end);
707 mutex_unlock(&text_mutex);
710 void __init_or_module alternatives_smp_module_del(struct module *mod)
712 struct smp_alt_module *item;
714 mutex_lock(&text_mutex);
715 list_for_each_entry(item, &smp_alt_modules, next) {
716 if (mod != item->mod)
718 list_del(&item->next);
722 mutex_unlock(&text_mutex);
725 void alternatives_enable_smp(void)
727 struct smp_alt_module *mod;
729 /* Why bother if there are no other CPUs? */
730 BUG_ON(num_possible_cpus() == 1);
732 mutex_lock(&text_mutex);
734 if (uniproc_patched) {
735 pr_info("switching to SMP code\n");
736 BUG_ON(num_online_cpus() != 1);
737 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_UP);
738 clear_cpu_cap(&cpu_data(0), X86_FEATURE_UP);
739 list_for_each_entry(mod, &smp_alt_modules, next)
740 alternatives_smp_lock(mod->locks, mod->locks_end,
741 mod->text, mod->text_end);
742 uniproc_patched = false;
744 mutex_unlock(&text_mutex);
748 * Return 1 if the address range is reserved for SMP-alternatives.
749 * Must hold text_mutex.
751 int alternatives_text_reserved(void *start, void *end)
753 struct smp_alt_module *mod;
755 u8 *text_start = start;
758 lockdep_assert_held(&text_mutex);
760 list_for_each_entry(mod, &smp_alt_modules, next) {
761 if (mod->text > text_end || mod->text_end < text_start)
763 for (poff = mod->locks; poff < mod->locks_end; poff++) {
764 const u8 *ptr = (const u8 *)poff + *poff;
766 if (text_start <= ptr && text_end > ptr)
773 #endif /* CONFIG_SMP */
775 #ifdef CONFIG_PARAVIRT
776 void __init_or_module apply_paravirt(struct paravirt_patch_site *start,
777 struct paravirt_patch_site *end)
779 struct paravirt_patch_site *p;
780 char insn_buff[MAX_PATCH_LEN];
782 for (p = start; p < end; p++) {
785 BUG_ON(p->len > MAX_PATCH_LEN);
786 /* prep the buffer with the original instructions */
787 memcpy(insn_buff, p->instr, p->len);
788 used = paravirt_patch(p->type, insn_buff, (unsigned long)p->instr, p->len);
790 BUG_ON(used > p->len);
792 /* Pad the rest with nops */
793 add_nops(insn_buff + used, p->len - used);
794 text_poke_early(p->instr, insn_buff, p->len);
797 extern struct paravirt_patch_site __start_parainstructions[],
798 __stop_parainstructions[];
799 #endif /* CONFIG_PARAVIRT */
802 * Self-test for the INT3 based CALL emulation code.
804 * This exercises int3_emulate_call() to make sure INT3 pt_regs are set up
805 * properly and that there is a stack gap between the INT3 frame and the
806 * previous context. Without this gap doing a virtual PUSH on the interrupted
807 * stack would corrupt the INT3 IRET frame.
809 * See entry_{32,64}.S for more details.
813 * We define the int3_magic() function in assembly to control the calling
814 * convention such that we can 'call' it from assembly.
817 extern void int3_magic(unsigned int *ptr); /* defined in asm */
820 " .pushsection .init.text, \"ax\", @progbits\n"
821 " .type int3_magic, @function\n"
824 " movl $1, (%" _ASM_ARG1 ")\n"
826 " .size int3_magic, .-int3_magic\n"
830 extern void int3_selftest_ip(void); /* defined in asm below */
833 int3_exception_notify(struct notifier_block *self, unsigned long val, void *data)
835 unsigned long selftest = (unsigned long)&int3_selftest_ip;
836 struct die_args *args = data;
837 struct pt_regs *regs = args->regs;
839 OPTIMIZER_HIDE_VAR(selftest);
841 if (!regs || user_mode(regs))
847 if (regs->ip - INT3_INSN_SIZE != selftest)
850 int3_emulate_call(regs, (unsigned long)&int3_magic);
854 /* Must be noinline to ensure uniqueness of int3_selftest_ip. */
855 static noinline void __init int3_selftest(void)
857 static __initdata struct notifier_block int3_exception_nb = {
858 .notifier_call = int3_exception_notify,
859 .priority = INT_MAX-1, /* last */
861 unsigned int val = 0;
863 BUG_ON(register_die_notifier(&int3_exception_nb));
866 * Basically: int3_magic(&val); but really complicated :-)
868 * INT3 padded with NOP to CALL_INSN_SIZE. The int3_exception_nb
869 * notifier above will emulate CALL for us.
871 asm volatile ("int3_selftest_ip:\n\t"
873 " int3; nop; nop; nop; nop\n\t"
874 : ASM_CALL_CONSTRAINT
875 : __ASM_SEL_RAW(a, D) (&val)
880 unregister_die_notifier(&int3_exception_nb);
883 void __init alternative_instructions(void)
888 * The patching is not fully atomic, so try to avoid local
889 * interruptions that might execute the to be patched code.
890 * Other CPUs are not running.
895 * Don't stop machine check exceptions while patching.
896 * MCEs only happen when something got corrupted and in this
897 * case we must do something about the corruption.
898 * Ignoring it is worse than an unlikely patching race.
899 * Also machine checks tend to be broadcast and if one CPU
900 * goes into machine check the others follow quickly, so we don't
901 * expect a machine check to cause undue problems during to code
906 * Paravirt patching and alternative patching can be combined to
907 * replace a function call with a short direct code sequence (e.g.
908 * by setting a constant return value instead of doing that in an
909 * external function).
910 * In order to make this work the following sequence is required:
911 * 1. set (artificial) features depending on used paravirt
912 * functions which can later influence alternative patching
913 * 2. apply paravirt patching (generally replacing an indirect
914 * function call with a direct one)
915 * 3. apply alternative patching (e.g. replacing a direct function
916 * call with a custom code sequence)
917 * Doing paravirt patching after alternative patching would clobber
918 * the optimization of the custom code with a function call again.
923 * First patch paravirt functions, such that we overwrite the indirect
924 * call with the direct call.
926 apply_paravirt(__parainstructions, __parainstructions_end);
929 * Rewrite the retpolines, must be done before alternatives since
930 * those can rewrite the retpoline thunks.
932 apply_retpolines(__retpoline_sites, __retpoline_sites_end);
933 apply_returns(__return_sites, __return_sites_end);
936 * Then patch alternatives, such that those paravirt calls that are in
937 * alternatives can be overwritten by their immediate fragments.
939 apply_alternatives(__alt_instructions, __alt_instructions_end);
941 apply_ibt_endbr(__ibt_endbr_seal, __ibt_endbr_seal_end);
944 /* Patch to UP if other cpus not imminent. */
945 if (!noreplace_smp && (num_present_cpus() == 1 || setup_max_cpus <= 1)) {
946 uniproc_patched = true;
947 alternatives_smp_module_add(NULL, "core kernel",
948 __smp_locks, __smp_locks_end,
952 if (!uniproc_patched || num_possible_cpus() == 1) {
953 free_init_pages("SMP alternatives",
954 (unsigned long)__smp_locks,
955 (unsigned long)__smp_locks_end);
960 alternatives_patched = 1;
964 * text_poke_early - Update instructions on a live kernel at boot time
965 * @addr: address to modify
966 * @opcode: source of the copy
967 * @len: length to copy
969 * When you use this code to patch more than one byte of an instruction
970 * you need to make sure that other CPUs cannot execute this code in parallel.
971 * Also no thread must be currently preempted in the middle of these
972 * instructions. And on the local CPU you need to be protected against NMI or
973 * MCE handlers seeing an inconsistent instruction while you patch.
975 void __init_or_module text_poke_early(void *addr, const void *opcode,
980 if (boot_cpu_has(X86_FEATURE_NX) &&
981 is_module_text_address((unsigned long)addr)) {
983 * Modules text is marked initially as non-executable, so the
984 * code cannot be running and speculative code-fetches are
985 * prevented. Just change the code.
987 memcpy(addr, opcode, len);
989 local_irq_save(flags);
990 memcpy(addr, opcode, len);
991 local_irq_restore(flags);
995 * Could also do a CLFLUSH here to speed up CPU recovery; but
996 * that causes hangs on some VIA CPUs.
1002 struct mm_struct *mm;
1006 * Using a temporary mm allows to set temporary mappings that are not accessible
1007 * by other CPUs. Such mappings are needed to perform sensitive memory writes
1008 * that override the kernel memory protections (e.g., W^X), without exposing the
1009 * temporary page-table mappings that are required for these write operations to
1010 * other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the
1011 * mapping is torn down.
1013 * Context: The temporary mm needs to be used exclusively by a single core. To
1014 * harden security IRQs must be disabled while the temporary mm is
1015 * loaded, thereby preventing interrupt handler bugs from overriding
1016 * the kernel memory protection.
1018 static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm)
1020 temp_mm_state_t temp_state;
1022 lockdep_assert_irqs_disabled();
1025 * Make sure not to be in TLB lazy mode, as otherwise we'll end up
1026 * with a stale address space WITHOUT being in lazy mode after
1027 * restoring the previous mm.
1029 if (this_cpu_read(cpu_tlbstate_shared.is_lazy))
1030 leave_mm(smp_processor_id());
1032 temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm);
1033 switch_mm_irqs_off(NULL, mm, current);
1036 * If breakpoints are enabled, disable them while the temporary mm is
1037 * used. Userspace might set up watchpoints on addresses that are used
1038 * in the temporary mm, which would lead to wrong signals being sent or
1041 * Note that breakpoints are not disabled selectively, which also causes
1042 * kernel breakpoints (e.g., perf's) to be disabled. This might be
1043 * undesirable, but still seems reasonable as the code that runs in the
1044 * temporary mm should be short.
1046 if (hw_breakpoint_active())
1047 hw_breakpoint_disable();
1052 static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
1054 lockdep_assert_irqs_disabled();
1055 switch_mm_irqs_off(NULL, prev_state.mm, current);
1058 * Restore the breakpoints if they were disabled before the temporary mm
1061 if (hw_breakpoint_active())
1062 hw_breakpoint_restore();
1065 __ro_after_init struct mm_struct *poking_mm;
1066 __ro_after_init unsigned long poking_addr;
1068 static void text_poke_memcpy(void *dst, const void *src, size_t len)
1070 memcpy(dst, src, len);
1073 static void text_poke_memset(void *dst, const void *src, size_t len)
1075 int c = *(const int *)src;
1077 memset(dst, c, len);
1080 typedef void text_poke_f(void *dst, const void *src, size_t len);
1082 static void *__text_poke(text_poke_f func, void *addr, const void *src, size_t len)
1084 bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE;
1085 struct page *pages[2] = {NULL};
1086 temp_mm_state_t prev;
1087 unsigned long flags;
1093 * While boot memory allocator is running we cannot use struct pages as
1094 * they are not yet initialized. There is no way to recover.
1096 BUG_ON(!after_bootmem);
1098 if (!core_kernel_text((unsigned long)addr)) {
1099 pages[0] = vmalloc_to_page(addr);
1100 if (cross_page_boundary)
1101 pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
1103 pages[0] = virt_to_page(addr);
1104 WARN_ON(!PageReserved(pages[0]));
1105 if (cross_page_boundary)
1106 pages[1] = virt_to_page(addr + PAGE_SIZE);
1109 * If something went wrong, crash and burn since recovery paths are not
1112 BUG_ON(!pages[0] || (cross_page_boundary && !pages[1]));
1115 * Map the page without the global bit, as TLB flushing is done with
1116 * flush_tlb_mm_range(), which is intended for non-global PTEs.
1118 pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL);
1121 * The lock is not really needed, but this allows to avoid open-coding.
1123 ptep = get_locked_pte(poking_mm, poking_addr, &ptl);
1126 * This must not fail; preallocated in poking_init().
1130 local_irq_save(flags);
1132 pte = mk_pte(pages[0], pgprot);
1133 set_pte_at(poking_mm, poking_addr, ptep, pte);
1135 if (cross_page_boundary) {
1136 pte = mk_pte(pages[1], pgprot);
1137 set_pte_at(poking_mm, poking_addr + PAGE_SIZE, ptep + 1, pte);
1141 * Loading the temporary mm behaves as a compiler barrier, which
1142 * guarantees that the PTE will be set at the time memcpy() is done.
1144 prev = use_temporary_mm(poking_mm);
1146 kasan_disable_current();
1147 func((u8 *)poking_addr + offset_in_page(addr), src, len);
1148 kasan_enable_current();
1151 * Ensure that the PTE is only cleared after the instructions of memcpy
1152 * were issued by using a compiler barrier.
1156 pte_clear(poking_mm, poking_addr, ptep);
1157 if (cross_page_boundary)
1158 pte_clear(poking_mm, poking_addr + PAGE_SIZE, ptep + 1);
1161 * Loading the previous page-table hierarchy requires a serializing
1162 * instruction that already allows the core to see the updated version.
1163 * Xen-PV is assumed to serialize execution in a similar manner.
1165 unuse_temporary_mm(prev);
1168 * Flushing the TLB might involve IPIs, which would require enabled
1169 * IRQs, but not if the mm is not used, as it is in this point.
1171 flush_tlb_mm_range(poking_mm, poking_addr, poking_addr +
1172 (cross_page_boundary ? 2 : 1) * PAGE_SIZE,
1175 if (func == text_poke_memcpy) {
1177 * If the text does not match what we just wrote then something is
1178 * fundamentally screwy; there's nothing we can really do about that.
1180 BUG_ON(memcmp(addr, src, len));
1183 local_irq_restore(flags);
1184 pte_unmap_unlock(ptep, ptl);
1189 * text_poke - Update instructions on a live kernel
1190 * @addr: address to modify
1191 * @opcode: source of the copy
1192 * @len: length to copy
1194 * Only atomic text poke/set should be allowed when not doing early patching.
1195 * It means the size must be writable atomically and the address must be aligned
1196 * in a way that permits an atomic write. It also makes sure we fit on a single
1199 * Note that the caller must ensure that if the modified code is part of a
1200 * module, the module would not be removed during poking. This can be achieved
1201 * by registering a module notifier, and ordering module removal and patching
1204 void *text_poke(void *addr, const void *opcode, size_t len)
1206 lockdep_assert_held(&text_mutex);
1208 return __text_poke(text_poke_memcpy, addr, opcode, len);
1212 * text_poke_kgdb - Update instructions on a live kernel by kgdb
1213 * @addr: address to modify
1214 * @opcode: source of the copy
1215 * @len: length to copy
1217 * Only atomic text poke/set should be allowed when not doing early patching.
1218 * It means the size must be writable atomically and the address must be aligned
1219 * in a way that permits an atomic write. It also makes sure we fit on a single
1222 * Context: should only be used by kgdb, which ensures no other core is running,
1223 * despite the fact it does not hold the text_mutex.
1225 void *text_poke_kgdb(void *addr, const void *opcode, size_t len)
1227 return __text_poke(text_poke_memcpy, addr, opcode, len);
1231 * text_poke_copy - Copy instructions into (an unused part of) RX memory
1232 * @addr: address to modify
1233 * @opcode: source of the copy
1234 * @len: length to copy, could be more than 2x PAGE_SIZE
1236 * Not safe against concurrent execution; useful for JITs to dump
1237 * new code blocks into unused regions of RX memory. Can be used in
1238 * conjunction with synchronize_rcu_tasks() to wait for existing
1239 * execution to quiesce after having made sure no existing functions
1240 * pointers are live.
1242 void *text_poke_copy(void *addr, const void *opcode, size_t len)
1244 unsigned long start = (unsigned long)addr;
1247 if (WARN_ON_ONCE(core_kernel_text(start)))
1250 mutex_lock(&text_mutex);
1251 while (patched < len) {
1252 unsigned long ptr = start + patched;
1255 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
1257 __text_poke(text_poke_memcpy, (void *)ptr, opcode + patched, s);
1260 mutex_unlock(&text_mutex);
1265 * text_poke_set - memset into (an unused part of) RX memory
1266 * @addr: address to modify
1267 * @c: the byte to fill the area with
1268 * @len: length to copy, could be more than 2x PAGE_SIZE
1270 * This is useful to overwrite unused regions of RX memory with illegal
1273 void *text_poke_set(void *addr, int c, size_t len)
1275 unsigned long start = (unsigned long)addr;
1278 if (WARN_ON_ONCE(core_kernel_text(start)))
1281 mutex_lock(&text_mutex);
1282 while (patched < len) {
1283 unsigned long ptr = start + patched;
1286 s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched);
1288 __text_poke(text_poke_memset, (void *)ptr, (void *)&c, s);
1291 mutex_unlock(&text_mutex);
1295 static void do_sync_core(void *info)
1300 void text_poke_sync(void)
1302 on_each_cpu(do_sync_core, NULL, 1);
1305 struct text_poke_loc {
1306 /* addr := _stext + rel_addr */
1311 const u8 text[POKE_MAX_OPCODE_SIZE];
1312 /* see text_poke_bp_batch() */
1316 struct bp_patching_desc {
1317 struct text_poke_loc *vec;
1322 static struct bp_patching_desc *bp_desc;
1324 static __always_inline
1325 struct bp_patching_desc *try_get_desc(struct bp_patching_desc **descp)
1327 /* rcu_dereference */
1328 struct bp_patching_desc *desc = __READ_ONCE(*descp);
1330 if (!desc || !arch_atomic_inc_not_zero(&desc->refs))
1336 static __always_inline void put_desc(struct bp_patching_desc *desc)
1338 smp_mb__before_atomic();
1339 arch_atomic_dec(&desc->refs);
1342 static __always_inline void *text_poke_addr(struct text_poke_loc *tp)
1344 return _stext + tp->rel_addr;
1347 static __always_inline int patch_cmp(const void *key, const void *elt)
1349 struct text_poke_loc *tp = (struct text_poke_loc *) elt;
1351 if (key < text_poke_addr(tp))
1353 if (key > text_poke_addr(tp))
1358 noinstr int poke_int3_handler(struct pt_regs *regs)
1360 struct bp_patching_desc *desc;
1361 struct text_poke_loc *tp;
1365 if (user_mode(regs))
1369 * Having observed our INT3 instruction, we now must observe
1372 * bp_desc = desc INT3
1374 * write INT3 if (desc)
1378 desc = try_get_desc(&bp_desc);
1383 * Discount the INT3. See text_poke_bp_batch().
1385 ip = (void *) regs->ip - INT3_INSN_SIZE;
1388 * Skip the binary search if there is a single member in the vector.
1390 if (unlikely(desc->nr_entries > 1)) {
1391 tp = __inline_bsearch(ip, desc->vec, desc->nr_entries,
1392 sizeof(struct text_poke_loc),
1398 if (text_poke_addr(tp) != ip)
1404 switch (tp->opcode) {
1405 case INT3_INSN_OPCODE:
1407 * Someone poked an explicit INT3, they'll want to handle it,
1412 case RET_INSN_OPCODE:
1413 int3_emulate_ret(regs);
1416 case CALL_INSN_OPCODE:
1417 int3_emulate_call(regs, (long)ip + tp->disp);
1420 case JMP32_INSN_OPCODE:
1421 case JMP8_INSN_OPCODE:
1422 int3_emulate_jmp(regs, (long)ip + tp->disp);
1436 #define TP_VEC_MAX (PAGE_SIZE / sizeof(struct text_poke_loc))
1437 static struct text_poke_loc tp_vec[TP_VEC_MAX];
1438 static int tp_vec_nr;
1441 * text_poke_bp_batch() -- update instructions on live kernel on SMP
1442 * @tp: vector of instructions to patch
1443 * @nr_entries: number of entries in the vector
1445 * Modify multi-byte instruction by using int3 breakpoint on SMP.
1446 * We completely avoid stop_machine() here, and achieve the
1447 * synchronization using int3 breakpoint.
1449 * The way it is done:
1450 * - For each entry in the vector:
1451 * - add a int3 trap to the address that will be patched
1453 * - For each entry in the vector:
1454 * - update all but the first byte of the patched range
1456 * - For each entry in the vector:
1457 * - replace the first byte (int3) by the first byte of
1461 static void text_poke_bp_batch(struct text_poke_loc *tp, unsigned int nr_entries)
1463 struct bp_patching_desc desc = {
1465 .nr_entries = nr_entries,
1466 .refs = ATOMIC_INIT(1),
1468 unsigned char int3 = INT3_INSN_OPCODE;
1472 lockdep_assert_held(&text_mutex);
1474 smp_store_release(&bp_desc, &desc); /* rcu_assign_pointer */
1477 * Corresponding read barrier in int3 notifier for making sure the
1478 * nr_entries and handler are correctly ordered wrt. patching.
1483 * First step: add a int3 trap to the address that will be patched.
1485 for (i = 0; i < nr_entries; i++) {
1486 tp[i].old = *(u8 *)text_poke_addr(&tp[i]);
1487 text_poke(text_poke_addr(&tp[i]), &int3, INT3_INSN_SIZE);
1493 * Second step: update all but the first byte of the patched range.
1495 for (do_sync = 0, i = 0; i < nr_entries; i++) {
1496 u8 old[POKE_MAX_OPCODE_SIZE] = { tp[i].old, };
1497 int len = tp[i].len;
1499 if (len - INT3_INSN_SIZE > 0) {
1500 memcpy(old + INT3_INSN_SIZE,
1501 text_poke_addr(&tp[i]) + INT3_INSN_SIZE,
1502 len - INT3_INSN_SIZE);
1503 text_poke(text_poke_addr(&tp[i]) + INT3_INSN_SIZE,
1504 (const char *)tp[i].text + INT3_INSN_SIZE,
1505 len - INT3_INSN_SIZE);
1510 * Emit a perf event to record the text poke, primarily to
1511 * support Intel PT decoding which must walk the executable code
1512 * to reconstruct the trace. The flow up to here is:
1515 * - write instruction tail
1516 * At this point the actual control flow will be through the
1517 * INT3 and handler and not hit the old or new instruction.
1518 * Intel PT outputs FUP/TIP packets for the INT3, so the flow
1519 * can still be decoded. Subsequently:
1520 * - emit RECORD_TEXT_POKE with the new instruction
1522 * - write first byte
1524 * So before the text poke event timestamp, the decoder will see
1525 * either the old instruction flow or FUP/TIP of INT3. After the
1526 * text poke event timestamp, the decoder will see either the
1527 * new instruction flow or FUP/TIP of INT3. Thus decoders can
1528 * use the timestamp as the point at which to modify the
1530 * The old instruction is recorded so that the event can be
1531 * processed forwards or backwards.
1533 perf_event_text_poke(text_poke_addr(&tp[i]), old, len,
1539 * According to Intel, this core syncing is very likely
1540 * not necessary and we'd be safe even without it. But
1541 * better safe than sorry (plus there's not only Intel).
1547 * Third step: replace the first byte (int3) by the first byte of
1550 for (do_sync = 0, i = 0; i < nr_entries; i++) {
1551 if (tp[i].text[0] == INT3_INSN_OPCODE)
1554 text_poke(text_poke_addr(&tp[i]), tp[i].text, INT3_INSN_SIZE);
1562 * Remove and synchronize_rcu(), except we have a very primitive
1563 * refcount based completion.
1565 WRITE_ONCE(bp_desc, NULL); /* RCU_INIT_POINTER */
1566 if (!atomic_dec_and_test(&desc.refs))
1567 atomic_cond_read_acquire(&desc.refs, !VAL);
1570 static void text_poke_loc_init(struct text_poke_loc *tp, void *addr,
1571 const void *opcode, size_t len, const void *emulate)
1576 memcpy((void *)tp->text, opcode, len);
1580 ret = insn_decode_kernel(&insn, emulate);
1583 tp->rel_addr = addr - (void *)_stext;
1585 tp->opcode = insn.opcode.bytes[0];
1587 switch (tp->opcode) {
1588 case RET_INSN_OPCODE:
1589 case JMP32_INSN_OPCODE:
1590 case JMP8_INSN_OPCODE:
1592 * Control flow instructions without implied execution of the
1593 * next instruction can be padded with INT3.
1595 for (i = insn.length; i < len; i++)
1596 BUG_ON(tp->text[i] != INT3_INSN_OPCODE);
1600 BUG_ON(len != insn.length);
1604 switch (tp->opcode) {
1605 case INT3_INSN_OPCODE:
1606 case RET_INSN_OPCODE:
1609 case CALL_INSN_OPCODE:
1610 case JMP32_INSN_OPCODE:
1611 case JMP8_INSN_OPCODE:
1612 tp->disp = insn.immediate.value;
1615 default: /* assume NOP */
1617 case 2: /* NOP2 -- emulate as JMP8+0 */
1618 BUG_ON(memcmp(emulate, x86_nops[len], len));
1619 tp->opcode = JMP8_INSN_OPCODE;
1623 case 5: /* NOP5 -- emulate as JMP32+0 */
1624 BUG_ON(memcmp(emulate, x86_nops[len], len));
1625 tp->opcode = JMP32_INSN_OPCODE;
1629 default: /* unknown instruction */
1637 * We hard rely on the tp_vec being ordered; ensure this is so by flushing
1640 static bool tp_order_fail(void *addr)
1642 struct text_poke_loc *tp;
1647 if (!addr) /* force */
1650 tp = &tp_vec[tp_vec_nr - 1];
1651 if ((unsigned long)text_poke_addr(tp) > (unsigned long)addr)
1657 static void text_poke_flush(void *addr)
1659 if (tp_vec_nr == TP_VEC_MAX || tp_order_fail(addr)) {
1660 text_poke_bp_batch(tp_vec, tp_vec_nr);
1665 void text_poke_finish(void)
1667 text_poke_flush(NULL);
1670 void __ref text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate)
1672 struct text_poke_loc *tp;
1674 if (unlikely(system_state == SYSTEM_BOOTING)) {
1675 text_poke_early(addr, opcode, len);
1679 text_poke_flush(addr);
1681 tp = &tp_vec[tp_vec_nr++];
1682 text_poke_loc_init(tp, addr, opcode, len, emulate);
1686 * text_poke_bp() -- update instructions on live kernel on SMP
1687 * @addr: address to patch
1688 * @opcode: opcode of new instruction
1689 * @len: length to copy
1690 * @emulate: instruction to be emulated
1692 * Update a single instruction with the vector in the stack, avoiding
1693 * dynamically allocated memory. This function should be used when it is
1694 * not possible to allocate memory.
1696 void __ref text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate)
1698 struct text_poke_loc tp;
1700 if (unlikely(system_state == SYSTEM_BOOTING)) {
1701 text_poke_early(addr, opcode, len);
1705 text_poke_loc_init(&tp, addr, opcode, len, emulate);
1706 text_poke_bp_batch(&tp, 1);