1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2008 Michael Ellerman, IBM Corporation.
6 #include <linux/kernel.h>
7 #include <linux/kprobes.h>
8 #include <linux/vmalloc.h>
9 #include <linux/init.h>
11 #include <linux/cpuhotplug.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
15 #include <asm/pgtable.h>
16 #include <asm/tlbflush.h>
18 #include <asm/code-patching.h>
19 #include <asm/setup.h>
22 static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
23 struct ppc_inst *patch_addr)
27 if (!ppc_inst_prefixed(instr)) {
28 __put_user_asm(ppc_inst_val(instr), patch_addr, err, "stw");
30 __put_user_asm(ppc_inst_as_u64(instr), patch_addr, err, "std");
36 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
42 int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
44 return __patch_instruction(addr, instr, addr);
47 #ifdef CONFIG_STRICT_KERNEL_RWX
48 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
50 static int text_area_cpu_up(unsigned int cpu)
52 struct vm_struct *area;
54 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
56 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
60 this_cpu_write(text_poke_area, area);
65 static int text_area_cpu_down(unsigned int cpu)
67 free_vm_area(this_cpu_read(text_poke_area));
72 * Run as a late init call. This allows all the boot time patching to be done
73 * simply by patching the code, and then we're called here prior to
74 * mark_rodata_ro(), which happens after all init calls are run. Although
75 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
76 * it as being preferable to a kernel that will crash later when someone tries
77 * to use patch_instruction().
79 static int __init setup_text_poke_area(void)
81 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
82 "powerpc/text_poke:online", text_area_cpu_up,
87 late_initcall(setup_text_poke_area);
90 * This can be called for kernel text or a module.
92 static int map_patch_area(void *addr, unsigned long text_poke_addr)
97 if (is_vmalloc_addr(addr))
98 pfn = vmalloc_to_pfn(addr);
100 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
102 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
104 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
111 static inline int unmap_patch_area(unsigned long addr)
119 pgdp = pgd_offset_k(addr);
123 p4dp = p4d_offset(pgdp, addr);
127 pudp = pud_offset(p4dp, addr);
131 pmdp = pmd_offset(pudp, addr);
135 ptep = pte_offset_kernel(pmdp, addr);
139 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
142 * In hash, pte_clear flushes the tlb, in radix, we have to
144 pte_clear(&init_mm, addr, ptep);
145 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
150 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
153 struct ppc_inst *patch_addr = NULL;
155 unsigned long text_poke_addr;
156 unsigned long kaddr = (unsigned long)addr;
159 * During early early boot patch_instruction is called
160 * when text_poke_area is not ready, but we still need
161 * to allow patching. We just do the plain old patching
163 if (!this_cpu_read(text_poke_area))
164 return raw_patch_instruction(addr, instr);
166 local_irq_save(flags);
168 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
169 if (map_patch_area(addr, text_poke_addr)) {
174 patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
176 __patch_instruction(addr, instr, patch_addr);
178 err = unmap_patch_area(text_poke_addr);
180 pr_warn("failed to unmap %lx\n", text_poke_addr);
183 local_irq_restore(flags);
187 #else /* !CONFIG_STRICT_KERNEL_RWX */
189 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
191 return raw_patch_instruction(addr, instr);
194 #endif /* CONFIG_STRICT_KERNEL_RWX */
196 int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
198 /* Make sure we aren't patching a freed init section */
199 if (init_mem_is_free && init_section_contains(addr, 4)) {
200 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
203 return do_patch_instruction(addr, instr);
205 NOKPROBE_SYMBOL(patch_instruction);
207 int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
209 struct ppc_inst instr;
211 create_branch(&instr, addr, target, flags);
212 return patch_instruction(addr, instr);
215 bool is_offset_in_branch_range(long offset)
218 * Powerpc branch instruction is :
221 * +---------+----------------+---+---+
222 * | opcode | LI |AA |LK |
223 * +---------+----------------+---+---+
224 * Where AA = 0 and LK = 0
226 * LI is a signed 24 bits integer. The real branch offset is computed
227 * by: imm32 = SignExtend(LI:'0b00', 32);
229 * So the maximum forward branch should be:
230 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
231 * The maximum backward branch should be:
232 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
234 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
238 * Helper to check if a given instruction is a conditional branch
239 * Derived from the conditional checks in analyse_instr()
241 bool is_conditional_branch(struct ppc_inst instr)
243 unsigned int opcode = ppc_inst_primary_opcode(instr);
245 if (opcode == 16) /* bc, bca, bcl, bcla */
248 switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
249 case 16: /* bclr, bclrl */
250 case 528: /* bcctr, bcctrl */
251 case 560: /* bctar, bctarl */
257 NOKPROBE_SYMBOL(is_conditional_branch);
259 int create_branch(struct ppc_inst *instr,
260 const struct ppc_inst *addr,
261 unsigned long target, int flags)
265 *instr = ppc_inst(0);
267 if (! (flags & BRANCH_ABSOLUTE))
268 offset = offset - (unsigned long)addr;
270 /* Check we can represent the target in the instruction format */
271 if (!is_offset_in_branch_range(offset))
274 /* Mask out the flags and target, so they don't step on each other. */
275 *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
280 int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
281 unsigned long target, int flags)
286 if (! (flags & BRANCH_ABSOLUTE))
287 offset = offset - (unsigned long)addr;
289 /* Check we can represent the target in the instruction format */
290 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
293 /* Mask out the flags and target, so they don't step on each other. */
294 *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
299 static unsigned int branch_opcode(struct ppc_inst instr)
301 return ppc_inst_primary_opcode(instr) & 0x3F;
304 static int instr_is_branch_iform(struct ppc_inst instr)
306 return branch_opcode(instr) == 18;
309 static int instr_is_branch_bform(struct ppc_inst instr)
311 return branch_opcode(instr) == 16;
314 int instr_is_relative_branch(struct ppc_inst instr)
316 if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
319 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
322 int instr_is_relative_link_branch(struct ppc_inst instr)
324 return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
327 static unsigned long branch_iform_target(const struct ppc_inst *instr)
331 imm = ppc_inst_val(*instr) & 0x3FFFFFC;
333 /* If the top bit of the immediate value is set this is negative */
337 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
338 imm += (unsigned long)instr;
340 return (unsigned long)imm;
343 static unsigned long branch_bform_target(const struct ppc_inst *instr)
347 imm = ppc_inst_val(*instr) & 0xFFFC;
349 /* If the top bit of the immediate value is set this is negative */
353 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
354 imm += (unsigned long)instr;
356 return (unsigned long)imm;
359 unsigned long branch_target(const struct ppc_inst *instr)
361 if (instr_is_branch_iform(ppc_inst_read(instr)))
362 return branch_iform_target(instr);
363 else if (instr_is_branch_bform(ppc_inst_read(instr)))
364 return branch_bform_target(instr);
369 int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
371 if (instr_is_branch_iform(ppc_inst_read(instr)) ||
372 instr_is_branch_bform(ppc_inst_read(instr)))
373 return branch_target(instr) == addr;
378 int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
379 const struct ppc_inst *src)
381 unsigned long target;
382 target = branch_target(src);
384 if (instr_is_branch_iform(ppc_inst_read(src)))
385 return create_branch(instr, dest, target,
386 ppc_inst_val(ppc_inst_read(src)));
387 else if (instr_is_branch_bform(ppc_inst_read(src)))
388 return create_cond_branch(instr, dest, target,
389 ppc_inst_val(ppc_inst_read(src)));
394 #ifdef CONFIG_PPC_BOOK3E_64
395 void __patch_exception(int exc, unsigned long addr)
397 extern unsigned int interrupt_base_book3e;
398 unsigned int *ibase = &interrupt_base_book3e;
400 /* Our exceptions vectors start with a NOP and -then- a branch
401 * to deal with single stepping from userspace which stops on
402 * the second instruction. Thus we need to patch the second
403 * instruction of the exception, not the first one
406 patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
410 #ifdef CONFIG_CODE_PATCHING_SELFTEST
412 static void __init test_trampoline(void)
418 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
420 static void __init test_branch_iform(void)
423 struct ppc_inst instr;
426 addr = (unsigned long)&instr;
428 /* The simplest case, branch to self, no flags */
429 check(instr_is_branch_iform(ppc_inst(0x48000000)));
430 /* All bits of target set, and flags */
431 check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
432 /* High bit of opcode set, which is wrong */
433 check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
434 /* Middle bits of opcode set, which is wrong */
435 check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
437 /* Simplest case, branch to self with link */
438 check(instr_is_branch_iform(ppc_inst(0x48000001)));
439 /* All bits of targets set */
440 check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
441 /* Some bits of targets set */
442 check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
443 /* Must be a valid branch to start with */
444 check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
446 /* Absolute branch to 0x100 */
447 instr = ppc_inst(0x48000103);
448 check(instr_is_branch_to_addr(&instr, 0x100));
449 /* Absolute branch to 0x420fc */
450 instr = ppc_inst(0x480420ff);
451 check(instr_is_branch_to_addr(&instr, 0x420fc));
452 /* Maximum positive relative branch, + 20MB - 4B */
453 instr = ppc_inst(0x49fffffc);
454 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
455 /* Smallest negative relative branch, - 4B */
456 instr = ppc_inst(0x4bfffffc);
457 check(instr_is_branch_to_addr(&instr, addr - 4));
458 /* Largest negative relative branch, - 32 MB */
459 instr = ppc_inst(0x4a000000);
460 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
462 /* Branch to self, with link */
463 err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
464 check(instr_is_branch_to_addr(&instr, addr));
466 /* Branch to self - 0x100, with link */
467 err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
468 check(instr_is_branch_to_addr(&instr, addr - 0x100));
470 /* Branch to self + 0x100, no link */
471 err = create_branch(&instr, &instr, addr + 0x100, 0);
472 check(instr_is_branch_to_addr(&instr, addr + 0x100));
474 /* Maximum relative negative offset, - 32 MB */
475 err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
476 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
478 /* Out of range relative negative offset, - 32 MB + 4*/
479 err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
482 /* Out of range relative positive offset, + 32 MB */
483 err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
486 /* Unaligned target */
487 err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
490 /* Check flags are masked correctly */
491 err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
492 check(instr_is_branch_to_addr(&instr, addr));
493 check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
496 static void __init test_create_function_call(void)
498 struct ppc_inst *iptr;
500 struct ppc_inst instr;
502 /* Check we can create a function call */
503 iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
504 dest = ppc_function_entry(test_create_function_call);
505 create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
506 patch_instruction(iptr, instr);
507 check(instr_is_branch_to_addr(iptr, dest));
510 static void __init test_branch_bform(void)
514 struct ppc_inst *iptr, instr;
518 addr = (unsigned long)iptr;
520 /* The simplest case, branch to self, no flags */
521 check(instr_is_branch_bform(ppc_inst(0x40000000)));
522 /* All bits of target set, and flags */
523 check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
524 /* High bit of opcode set, which is wrong */
525 check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
526 /* Middle bits of opcode set, which is wrong */
527 check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
529 /* Absolute conditional branch to 0x100 */
530 instr = ppc_inst(0x43ff0103);
531 check(instr_is_branch_to_addr(&instr, 0x100));
532 /* Absolute conditional branch to 0x20fc */
533 instr = ppc_inst(0x43ff20ff);
534 check(instr_is_branch_to_addr(&instr, 0x20fc));
535 /* Maximum positive relative conditional branch, + 32 KB - 4B */
536 instr = ppc_inst(0x43ff7ffc);
537 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
538 /* Smallest negative relative conditional branch, - 4B */
539 instr = ppc_inst(0x43fffffc);
540 check(instr_is_branch_to_addr(&instr, addr - 4));
541 /* Largest negative relative conditional branch, - 32 KB */
542 instr = ppc_inst(0x43ff8000);
543 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
545 /* All condition code bits set & link */
546 flags = 0x3ff000 | BRANCH_SET_LINK;
549 err = create_cond_branch(&instr, iptr, addr, flags);
550 check(instr_is_branch_to_addr(&instr, addr));
552 /* Branch to self - 0x100 */
553 err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
554 check(instr_is_branch_to_addr(&instr, addr - 0x100));
556 /* Branch to self + 0x100 */
557 err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
558 check(instr_is_branch_to_addr(&instr, addr + 0x100));
560 /* Maximum relative negative offset, - 32 KB */
561 err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
562 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
564 /* Out of range relative negative offset, - 32 KB + 4*/
565 err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
568 /* Out of range relative positive offset, + 32 KB */
569 err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
572 /* Unaligned target */
573 err = create_cond_branch(&instr, iptr, addr + 3, flags);
576 /* Check flags are masked correctly */
577 err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
578 check(instr_is_branch_to_addr(&instr, addr));
579 check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
582 static void __init test_translate_branch(void)
586 struct ppc_inst instr;
589 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
594 /* Simple case, branch to self moved a little */
596 addr = (unsigned long)p;
597 patch_branch(p, addr, 0);
598 check(instr_is_branch_to_addr(p, addr));
600 translate_branch(&instr, q, p);
601 patch_instruction(q, instr);
602 check(instr_is_branch_to_addr(q, addr));
604 /* Maximum negative case, move b . to addr + 32 MB */
606 addr = (unsigned long)p;
607 patch_branch(p, addr, 0);
609 translate_branch(&instr, q, p);
610 patch_instruction(q, instr);
611 check(instr_is_branch_to_addr(p, addr));
612 check(instr_is_branch_to_addr(q, addr));
613 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
615 /* Maximum positive case, move x to x - 32 MB + 4 */
617 addr = (unsigned long)p;
618 patch_branch(p, addr, 0);
620 translate_branch(&instr, q, p);
621 patch_instruction(q, instr);
622 check(instr_is_branch_to_addr(p, addr));
623 check(instr_is_branch_to_addr(q, addr));
624 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
626 /* Jump to x + 16 MB moved to x + 20 MB */
628 addr = 0x1000000 + (unsigned long)buf;
629 patch_branch(p, addr, BRANCH_SET_LINK);
631 translate_branch(&instr, q, p);
632 patch_instruction(q, instr);
633 check(instr_is_branch_to_addr(p, addr));
634 check(instr_is_branch_to_addr(q, addr));
636 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
638 addr = 0x2000000 + (unsigned long)buf;
639 patch_branch(p, addr, 0);
641 translate_branch(&instr, q, p);
642 patch_instruction(q, instr);
643 check(instr_is_branch_to_addr(p, addr));
644 check(instr_is_branch_to_addr(q, addr));
647 /* Conditional branch tests */
649 /* Simple case, branch to self moved a little */
651 addr = (unsigned long)p;
652 create_cond_branch(&instr, p, addr, 0);
653 patch_instruction(p, instr);
654 check(instr_is_branch_to_addr(p, addr));
656 translate_branch(&instr, q, p);
657 patch_instruction(q, instr);
658 check(instr_is_branch_to_addr(q, addr));
660 /* Maximum negative case, move b . to addr + 32 KB */
662 addr = (unsigned long)p;
663 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
664 patch_instruction(p, instr);
666 translate_branch(&instr, q, p);
667 patch_instruction(q, instr);
668 check(instr_is_branch_to_addr(p, addr));
669 check(instr_is_branch_to_addr(q, addr));
670 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
672 /* Maximum positive case, move x to x - 32 KB + 4 */
674 addr = (unsigned long)p;
675 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
676 patch_instruction(p, instr);
678 translate_branch(&instr, q, p);
679 patch_instruction(q, instr);
680 check(instr_is_branch_to_addr(p, addr));
681 check(instr_is_branch_to_addr(q, addr));
682 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
684 /* Jump to x + 12 KB moved to x + 20 KB */
686 addr = 0x3000 + (unsigned long)buf;
687 create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
688 patch_instruction(p, instr);
690 translate_branch(&instr, q, p);
691 patch_instruction(q, instr);
692 check(instr_is_branch_to_addr(p, addr));
693 check(instr_is_branch_to_addr(q, addr));
695 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
697 addr = 0x4000 + (unsigned long)buf;
698 create_cond_branch(&instr, p, addr, 0);
699 patch_instruction(p, instr);
701 translate_branch(&instr, q, p);
702 patch_instruction(q, instr);
703 check(instr_is_branch_to_addr(p, addr));
704 check(instr_is_branch_to_addr(q, addr));
706 /* Free the buffer we were using */
711 static void __init test_prefixed_patching(void)
713 extern unsigned int code_patching_test1[];
714 extern unsigned int code_patching_test1_expected[];
715 extern unsigned int end_code_patching_test1[];
717 __patch_instruction((struct ppc_inst *)code_patching_test1,
718 ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
719 (struct ppc_inst *)code_patching_test1);
721 check(!memcmp(code_patching_test1,
722 code_patching_test1_expected,
723 sizeof(unsigned int) *
724 (end_code_patching_test1 - code_patching_test1)));
727 static inline void test_prefixed_patching(void) {}
730 static int __init test_code_patching(void)
732 printk(KERN_DEBUG "Running code patching self-tests ...\n");
736 test_create_function_call();
737 test_translate_branch();
738 test_prefixed_patching();
742 late_initcall(test_code_patching);
744 #endif /* CONFIG_CODE_PATCHING_SELFTEST */