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/tlbflush.h>
17 #include <asm/code-patching.h>
18 #include <asm/setup.h>
21 static int __patch_instruction(struct ppc_inst *exec_addr, struct ppc_inst instr,
22 struct ppc_inst *patch_addr)
26 if (!ppc_inst_prefixed(instr)) {
27 __put_user_asm(ppc_inst_val(instr), patch_addr, err, "stw");
29 __put_user_asm(ppc_inst_as_u64(instr), patch_addr, err, "std");
35 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
41 int raw_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
43 return __patch_instruction(addr, instr, addr);
46 #ifdef CONFIG_STRICT_KERNEL_RWX
47 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
49 static int text_area_cpu_up(unsigned int cpu)
51 struct vm_struct *area;
53 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
55 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
59 this_cpu_write(text_poke_area, area);
64 static int text_area_cpu_down(unsigned int cpu)
66 free_vm_area(this_cpu_read(text_poke_area));
71 * Run as a late init call. This allows all the boot time patching to be done
72 * simply by patching the code, and then we're called here prior to
73 * mark_rodata_ro(), which happens after all init calls are run. Although
74 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
75 * it as being preferable to a kernel that will crash later when someone tries
76 * to use patch_instruction().
78 static int __init setup_text_poke_area(void)
80 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
81 "powerpc/text_poke:online", text_area_cpu_up,
86 late_initcall(setup_text_poke_area);
89 * This can be called for kernel text or a module.
91 static int map_patch_area(void *addr, unsigned long text_poke_addr)
96 if (is_vmalloc_addr(addr))
97 pfn = vmalloc_to_pfn(addr);
99 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
101 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
103 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
110 static inline int unmap_patch_area(unsigned long addr)
118 pgdp = pgd_offset_k(addr);
122 p4dp = p4d_offset(pgdp, addr);
126 pudp = pud_offset(p4dp, addr);
130 pmdp = pmd_offset(pudp, addr);
134 ptep = pte_offset_kernel(pmdp, addr);
138 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
141 * In hash, pte_clear flushes the tlb, in radix, we have to
143 pte_clear(&init_mm, addr, ptep);
144 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
149 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
152 struct ppc_inst *patch_addr = NULL;
154 unsigned long text_poke_addr;
155 unsigned long kaddr = (unsigned long)addr;
158 * During early early boot patch_instruction is called
159 * when text_poke_area is not ready, but we still need
160 * to allow patching. We just do the plain old patching
162 if (!this_cpu_read(text_poke_area))
163 return raw_patch_instruction(addr, instr);
165 local_irq_save(flags);
167 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
168 if (map_patch_area(addr, text_poke_addr)) {
173 patch_addr = (struct ppc_inst *)(text_poke_addr + (kaddr & ~PAGE_MASK));
175 __patch_instruction(addr, instr, patch_addr);
177 err = unmap_patch_area(text_poke_addr);
179 pr_warn("failed to unmap %lx\n", text_poke_addr);
182 local_irq_restore(flags);
186 #else /* !CONFIG_STRICT_KERNEL_RWX */
188 static int do_patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
190 return raw_patch_instruction(addr, instr);
193 #endif /* CONFIG_STRICT_KERNEL_RWX */
195 int patch_instruction(struct ppc_inst *addr, struct ppc_inst instr)
197 /* Make sure we aren't patching a freed init section */
198 if (init_mem_is_free && init_section_contains(addr, 4)) {
199 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
202 return do_patch_instruction(addr, instr);
204 NOKPROBE_SYMBOL(patch_instruction);
206 int patch_branch(struct ppc_inst *addr, unsigned long target, int flags)
208 struct ppc_inst instr;
210 create_branch(&instr, addr, target, flags);
211 return patch_instruction(addr, instr);
214 bool is_offset_in_branch_range(long offset)
217 * Powerpc branch instruction is :
220 * +---------+----------------+---+---+
221 * | opcode | LI |AA |LK |
222 * +---------+----------------+---+---+
223 * Where AA = 0 and LK = 0
225 * LI is a signed 24 bits integer. The real branch offset is computed
226 * by: imm32 = SignExtend(LI:'0b00', 32);
228 * So the maximum forward branch should be:
229 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
230 * The maximum backward branch should be:
231 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
233 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
237 * Helper to check if a given instruction is a conditional branch
238 * Derived from the conditional checks in analyse_instr()
240 bool is_conditional_branch(struct ppc_inst instr)
242 unsigned int opcode = ppc_inst_primary_opcode(instr);
244 if (opcode == 16) /* bc, bca, bcl, bcla */
247 switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
248 case 16: /* bclr, bclrl */
249 case 528: /* bcctr, bcctrl */
250 case 560: /* bctar, bctarl */
256 NOKPROBE_SYMBOL(is_conditional_branch);
258 int create_branch(struct ppc_inst *instr,
259 const struct ppc_inst *addr,
260 unsigned long target, int flags)
264 *instr = ppc_inst(0);
266 if (! (flags & BRANCH_ABSOLUTE))
267 offset = offset - (unsigned long)addr;
269 /* Check we can represent the target in the instruction format */
270 if (!is_offset_in_branch_range(offset))
273 /* Mask out the flags and target, so they don't step on each other. */
274 *instr = ppc_inst(0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC));
279 int create_cond_branch(struct ppc_inst *instr, const struct ppc_inst *addr,
280 unsigned long target, int flags)
285 if (! (flags & BRANCH_ABSOLUTE))
286 offset = offset - (unsigned long)addr;
288 /* Check we can represent the target in the instruction format */
289 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
292 /* Mask out the flags and target, so they don't step on each other. */
293 *instr = ppc_inst(0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC));
298 static unsigned int branch_opcode(struct ppc_inst instr)
300 return ppc_inst_primary_opcode(instr) & 0x3F;
303 static int instr_is_branch_iform(struct ppc_inst instr)
305 return branch_opcode(instr) == 18;
308 static int instr_is_branch_bform(struct ppc_inst instr)
310 return branch_opcode(instr) == 16;
313 int instr_is_relative_branch(struct ppc_inst instr)
315 if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
318 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
321 int instr_is_relative_link_branch(struct ppc_inst instr)
323 return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
326 static unsigned long branch_iform_target(const struct ppc_inst *instr)
330 imm = ppc_inst_val(*instr) & 0x3FFFFFC;
332 /* If the top bit of the immediate value is set this is negative */
336 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
337 imm += (unsigned long)instr;
339 return (unsigned long)imm;
342 static unsigned long branch_bform_target(const struct ppc_inst *instr)
346 imm = ppc_inst_val(*instr) & 0xFFFC;
348 /* If the top bit of the immediate value is set this is negative */
352 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
353 imm += (unsigned long)instr;
355 return (unsigned long)imm;
358 unsigned long branch_target(const struct ppc_inst *instr)
360 if (instr_is_branch_iform(ppc_inst_read(instr)))
361 return branch_iform_target(instr);
362 else if (instr_is_branch_bform(ppc_inst_read(instr)))
363 return branch_bform_target(instr);
368 int instr_is_branch_to_addr(const struct ppc_inst *instr, unsigned long addr)
370 if (instr_is_branch_iform(ppc_inst_read(instr)) ||
371 instr_is_branch_bform(ppc_inst_read(instr)))
372 return branch_target(instr) == addr;
377 int translate_branch(struct ppc_inst *instr, const struct ppc_inst *dest,
378 const struct ppc_inst *src)
380 unsigned long target;
381 target = branch_target(src);
383 if (instr_is_branch_iform(ppc_inst_read(src)))
384 return create_branch(instr, dest, target,
385 ppc_inst_val(ppc_inst_read(src)));
386 else if (instr_is_branch_bform(ppc_inst_read(src)))
387 return create_cond_branch(instr, dest, target,
388 ppc_inst_val(ppc_inst_read(src)));
393 #ifdef CONFIG_PPC_BOOK3E_64
394 void __patch_exception(int exc, unsigned long addr)
396 extern unsigned int interrupt_base_book3e;
397 unsigned int *ibase = &interrupt_base_book3e;
399 /* Our exceptions vectors start with a NOP and -then- a branch
400 * to deal with single stepping from userspace which stops on
401 * the second instruction. Thus we need to patch the second
402 * instruction of the exception, not the first one
405 patch_branch((struct ppc_inst *)(ibase + (exc / 4) + 1), addr, 0);
409 #ifdef CONFIG_CODE_PATCHING_SELFTEST
411 static void __init test_trampoline(void)
417 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
419 static void __init test_branch_iform(void)
422 struct ppc_inst instr;
425 addr = (unsigned long)&instr;
427 /* The simplest case, branch to self, no flags */
428 check(instr_is_branch_iform(ppc_inst(0x48000000)));
429 /* All bits of target set, and flags */
430 check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
431 /* High bit of opcode set, which is wrong */
432 check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
433 /* Middle bits of opcode set, which is wrong */
434 check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
436 /* Simplest case, branch to self with link */
437 check(instr_is_branch_iform(ppc_inst(0x48000001)));
438 /* All bits of targets set */
439 check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
440 /* Some bits of targets set */
441 check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
442 /* Must be a valid branch to start with */
443 check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
445 /* Absolute branch to 0x100 */
446 instr = ppc_inst(0x48000103);
447 check(instr_is_branch_to_addr(&instr, 0x100));
448 /* Absolute branch to 0x420fc */
449 instr = ppc_inst(0x480420ff);
450 check(instr_is_branch_to_addr(&instr, 0x420fc));
451 /* Maximum positive relative branch, + 20MB - 4B */
452 instr = ppc_inst(0x49fffffc);
453 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
454 /* Smallest negative relative branch, - 4B */
455 instr = ppc_inst(0x4bfffffc);
456 check(instr_is_branch_to_addr(&instr, addr - 4));
457 /* Largest negative relative branch, - 32 MB */
458 instr = ppc_inst(0x4a000000);
459 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
461 /* Branch to self, with link */
462 err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
463 check(instr_is_branch_to_addr(&instr, addr));
465 /* Branch to self - 0x100, with link */
466 err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
467 check(instr_is_branch_to_addr(&instr, addr - 0x100));
469 /* Branch to self + 0x100, no link */
470 err = create_branch(&instr, &instr, addr + 0x100, 0);
471 check(instr_is_branch_to_addr(&instr, addr + 0x100));
473 /* Maximum relative negative offset, - 32 MB */
474 err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
475 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
477 /* Out of range relative negative offset, - 32 MB + 4*/
478 err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
481 /* Out of range relative positive offset, + 32 MB */
482 err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
485 /* Unaligned target */
486 err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
489 /* Check flags are masked correctly */
490 err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
491 check(instr_is_branch_to_addr(&instr, addr));
492 check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
495 static void __init test_create_function_call(void)
497 struct ppc_inst *iptr;
499 struct ppc_inst instr;
501 /* Check we can create a function call */
502 iptr = (struct ppc_inst *)ppc_function_entry(test_trampoline);
503 dest = ppc_function_entry(test_create_function_call);
504 create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
505 patch_instruction(iptr, instr);
506 check(instr_is_branch_to_addr(iptr, dest));
509 static void __init test_branch_bform(void)
513 struct ppc_inst *iptr, instr;
517 addr = (unsigned long)iptr;
519 /* The simplest case, branch to self, no flags */
520 check(instr_is_branch_bform(ppc_inst(0x40000000)));
521 /* All bits of target set, and flags */
522 check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
523 /* High bit of opcode set, which is wrong */
524 check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
525 /* Middle bits of opcode set, which is wrong */
526 check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
528 /* Absolute conditional branch to 0x100 */
529 instr = ppc_inst(0x43ff0103);
530 check(instr_is_branch_to_addr(&instr, 0x100));
531 /* Absolute conditional branch to 0x20fc */
532 instr = ppc_inst(0x43ff20ff);
533 check(instr_is_branch_to_addr(&instr, 0x20fc));
534 /* Maximum positive relative conditional branch, + 32 KB - 4B */
535 instr = ppc_inst(0x43ff7ffc);
536 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
537 /* Smallest negative relative conditional branch, - 4B */
538 instr = ppc_inst(0x43fffffc);
539 check(instr_is_branch_to_addr(&instr, addr - 4));
540 /* Largest negative relative conditional branch, - 32 KB */
541 instr = ppc_inst(0x43ff8000);
542 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
544 /* All condition code bits set & link */
545 flags = 0x3ff000 | BRANCH_SET_LINK;
548 err = create_cond_branch(&instr, iptr, addr, flags);
549 check(instr_is_branch_to_addr(&instr, addr));
551 /* Branch to self - 0x100 */
552 err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
553 check(instr_is_branch_to_addr(&instr, addr - 0x100));
555 /* Branch to self + 0x100 */
556 err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
557 check(instr_is_branch_to_addr(&instr, addr + 0x100));
559 /* Maximum relative negative offset, - 32 KB */
560 err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
561 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
563 /* Out of range relative negative offset, - 32 KB + 4*/
564 err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
567 /* Out of range relative positive offset, + 32 KB */
568 err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
571 /* Unaligned target */
572 err = create_cond_branch(&instr, iptr, addr + 3, flags);
575 /* Check flags are masked correctly */
576 err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
577 check(instr_is_branch_to_addr(&instr, addr));
578 check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
581 static void __init test_translate_branch(void)
585 struct ppc_inst instr;
588 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
593 /* Simple case, branch to self moved a little */
595 addr = (unsigned long)p;
596 patch_branch(p, addr, 0);
597 check(instr_is_branch_to_addr(p, addr));
599 translate_branch(&instr, q, p);
600 patch_instruction(q, instr);
601 check(instr_is_branch_to_addr(q, addr));
603 /* Maximum negative case, move b . to addr + 32 MB */
605 addr = (unsigned long)p;
606 patch_branch(p, addr, 0);
608 translate_branch(&instr, q, p);
609 patch_instruction(q, instr);
610 check(instr_is_branch_to_addr(p, addr));
611 check(instr_is_branch_to_addr(q, addr));
612 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
614 /* Maximum positive case, move x to x - 32 MB + 4 */
616 addr = (unsigned long)p;
617 patch_branch(p, addr, 0);
619 translate_branch(&instr, q, p);
620 patch_instruction(q, instr);
621 check(instr_is_branch_to_addr(p, addr));
622 check(instr_is_branch_to_addr(q, addr));
623 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
625 /* Jump to x + 16 MB moved to x + 20 MB */
627 addr = 0x1000000 + (unsigned long)buf;
628 patch_branch(p, addr, BRANCH_SET_LINK);
630 translate_branch(&instr, q, p);
631 patch_instruction(q, instr);
632 check(instr_is_branch_to_addr(p, addr));
633 check(instr_is_branch_to_addr(q, addr));
635 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
637 addr = 0x2000000 + (unsigned long)buf;
638 patch_branch(p, addr, 0);
640 translate_branch(&instr, q, p);
641 patch_instruction(q, instr);
642 check(instr_is_branch_to_addr(p, addr));
643 check(instr_is_branch_to_addr(q, addr));
646 /* Conditional branch tests */
648 /* Simple case, branch to self moved a little */
650 addr = (unsigned long)p;
651 create_cond_branch(&instr, p, addr, 0);
652 patch_instruction(p, instr);
653 check(instr_is_branch_to_addr(p, addr));
655 translate_branch(&instr, q, p);
656 patch_instruction(q, instr);
657 check(instr_is_branch_to_addr(q, addr));
659 /* Maximum negative case, move b . to addr + 32 KB */
661 addr = (unsigned long)p;
662 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
663 patch_instruction(p, instr);
665 translate_branch(&instr, q, p);
666 patch_instruction(q, instr);
667 check(instr_is_branch_to_addr(p, addr));
668 check(instr_is_branch_to_addr(q, addr));
669 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
671 /* Maximum positive case, move x to x - 32 KB + 4 */
673 addr = (unsigned long)p;
674 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
675 patch_instruction(p, instr);
677 translate_branch(&instr, q, p);
678 patch_instruction(q, instr);
679 check(instr_is_branch_to_addr(p, addr));
680 check(instr_is_branch_to_addr(q, addr));
681 check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
683 /* Jump to x + 12 KB moved to x + 20 KB */
685 addr = 0x3000 + (unsigned long)buf;
686 create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
687 patch_instruction(p, instr);
689 translate_branch(&instr, q, p);
690 patch_instruction(q, instr);
691 check(instr_is_branch_to_addr(p, addr));
692 check(instr_is_branch_to_addr(q, addr));
694 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
696 addr = 0x4000 + (unsigned long)buf;
697 create_cond_branch(&instr, p, addr, 0);
698 patch_instruction(p, instr);
700 translate_branch(&instr, q, p);
701 patch_instruction(q, instr);
702 check(instr_is_branch_to_addr(p, addr));
703 check(instr_is_branch_to_addr(q, addr));
705 /* Free the buffer we were using */
710 static void __init test_prefixed_patching(void)
712 extern unsigned int code_patching_test1[];
713 extern unsigned int code_patching_test1_expected[];
714 extern unsigned int end_code_patching_test1[];
716 __patch_instruction((struct ppc_inst *)code_patching_test1,
717 ppc_inst_prefix(OP_PREFIX << 26, 0x00000000),
718 (struct ppc_inst *)code_patching_test1);
720 check(!memcmp(code_patching_test1,
721 code_patching_test1_expected,
722 sizeof(unsigned int) *
723 (end_code_patching_test1 - code_patching_test1)));
726 static inline void test_prefixed_patching(void) {}
729 static int __init test_code_patching(void)
731 printk(KERN_DEBUG "Running code patching self-tests ...\n");
735 test_create_function_call();
736 test_translate_branch();
737 test_prefixed_patching();
741 late_initcall(test_code_patching);
743 #endif /* CONFIG_CODE_PATCHING_SELFTEST */