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(unsigned int *exec_addr, unsigned int instr,
23 unsigned int *patch_addr)
27 __put_user_asm(instr, patch_addr, err, "stw");
31 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
37 int raw_patch_instruction(unsigned int *addr, unsigned int instr)
39 return __patch_instruction(addr, instr, addr);
42 #ifdef CONFIG_STRICT_KERNEL_RWX
43 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
45 static int text_area_cpu_up(unsigned int cpu)
47 struct vm_struct *area;
49 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
51 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
55 this_cpu_write(text_poke_area, area);
60 static int text_area_cpu_down(unsigned int cpu)
62 free_vm_area(this_cpu_read(text_poke_area));
67 * Run as a late init call. This allows all the boot time patching to be done
68 * simply by patching the code, and then we're called here prior to
69 * mark_rodata_ro(), which happens after all init calls are run. Although
70 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
71 * it as being preferable to a kernel that will crash later when someone tries
72 * to use patch_instruction().
74 static int __init setup_text_poke_area(void)
76 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
77 "powerpc/text_poke:online", text_area_cpu_up,
82 late_initcall(setup_text_poke_area);
85 * This can be called for kernel text or a module.
87 static int map_patch_area(void *addr, unsigned long text_poke_addr)
92 if (is_vmalloc_addr(addr))
93 pfn = vmalloc_to_pfn(addr);
95 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
97 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT), PAGE_KERNEL);
99 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
106 static inline int unmap_patch_area(unsigned long addr)
113 pgdp = pgd_offset_k(addr);
117 pudp = pud_offset(pgdp, addr);
121 pmdp = pmd_offset(pudp, addr);
125 ptep = pte_offset_kernel(pmdp, addr);
129 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
132 * In hash, pte_clear flushes the tlb, in radix, we have to
134 pte_clear(&init_mm, addr, ptep);
135 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
140 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
143 unsigned int *patch_addr = NULL;
145 unsigned long text_poke_addr;
146 unsigned long kaddr = (unsigned long)addr;
149 * During early early boot patch_instruction is called
150 * when text_poke_area is not ready, but we still need
151 * to allow patching. We just do the plain old patching
153 if (!this_cpu_read(text_poke_area))
154 return raw_patch_instruction(addr, instr);
156 local_irq_save(flags);
158 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
159 if (map_patch_area(addr, text_poke_addr)) {
164 patch_addr = (unsigned int *)(text_poke_addr) +
165 ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
167 __patch_instruction(addr, instr, patch_addr);
169 err = unmap_patch_area(text_poke_addr);
171 pr_warn("failed to unmap %lx\n", text_poke_addr);
174 local_irq_restore(flags);
178 #else /* !CONFIG_STRICT_KERNEL_RWX */
180 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
182 return raw_patch_instruction(addr, instr);
185 #endif /* CONFIG_STRICT_KERNEL_RWX */
187 int patch_instruction(unsigned int *addr, unsigned int instr)
189 /* Make sure we aren't patching a freed init section */
190 if (init_mem_is_free && init_section_contains(addr, 4)) {
191 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
194 return do_patch_instruction(addr, instr);
196 NOKPROBE_SYMBOL(patch_instruction);
198 int patch_branch(unsigned int *addr, unsigned long target, int flags)
202 create_branch(&instr, addr, target, flags);
203 return patch_instruction(addr, instr);
206 bool is_offset_in_branch_range(long offset)
209 * Powerpc branch instruction is :
212 * +---------+----------------+---+---+
213 * | opcode | LI |AA |LK |
214 * +---------+----------------+---+---+
215 * Where AA = 0 and LK = 0
217 * LI is a signed 24 bits integer. The real branch offset is computed
218 * by: imm32 = SignExtend(LI:'0b00', 32);
220 * So the maximum forward branch should be:
221 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
222 * The maximum backward branch should be:
223 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
225 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
229 * Helper to check if a given instruction is a conditional branch
230 * Derived from the conditional checks in analyse_instr()
232 bool is_conditional_branch(unsigned int instr)
234 unsigned int opcode = ppc_inst_primary_opcode(instr);
236 if (opcode == 16) /* bc, bca, bcl, bcla */
239 switch ((ppc_inst_val(instr) >> 1) & 0x3ff) {
240 case 16: /* bclr, bclrl */
241 case 528: /* bcctr, bcctrl */
242 case 560: /* bctar, bctarl */
248 NOKPROBE_SYMBOL(is_conditional_branch);
250 int create_branch(unsigned int *instr,
251 const unsigned int *addr,
252 unsigned long target, int flags)
258 if (! (flags & BRANCH_ABSOLUTE))
259 offset = offset - (unsigned long)addr;
261 /* Check we can represent the target in the instruction format */
262 if (!is_offset_in_branch_range(offset))
265 /* Mask out the flags and target, so they don't step on each other. */
266 *instr = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
271 int create_cond_branch(unsigned int *instr, const unsigned int *addr,
272 unsigned long target, int flags)
277 if (! (flags & BRANCH_ABSOLUTE))
278 offset = offset - (unsigned long)addr;
280 /* Check we can represent the target in the instruction format */
281 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
284 /* Mask out the flags and target, so they don't step on each other. */
285 *instr = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
290 static unsigned int branch_opcode(unsigned int instr)
292 return ppc_inst_primary_opcode(instr) & 0x3F;
295 static int instr_is_branch_iform(unsigned int instr)
297 return branch_opcode(instr) == 18;
300 static int instr_is_branch_bform(unsigned int instr)
302 return branch_opcode(instr) == 16;
305 int instr_is_relative_branch(unsigned int instr)
307 if (ppc_inst_val(instr) & BRANCH_ABSOLUTE)
310 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
313 int instr_is_relative_link_branch(unsigned int instr)
315 return instr_is_relative_branch(instr) && (ppc_inst_val(instr) & BRANCH_SET_LINK);
318 static unsigned long branch_iform_target(const unsigned int *instr)
322 imm = ppc_inst_val(*instr) & 0x3FFFFFC;
324 /* If the top bit of the immediate value is set this is negative */
328 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
329 imm += (unsigned long)instr;
331 return (unsigned long)imm;
334 static unsigned long branch_bform_target(const unsigned int *instr)
338 imm = ppc_inst_val(*instr) & 0xFFFC;
340 /* If the top bit of the immediate value is set this is negative */
344 if ((ppc_inst_val(*instr) & BRANCH_ABSOLUTE) == 0)
345 imm += (unsigned long)instr;
347 return (unsigned long)imm;
350 unsigned long branch_target(const unsigned int *instr)
352 if (instr_is_branch_iform(*instr))
353 return branch_iform_target(instr);
354 else if (instr_is_branch_bform(*instr))
355 return branch_bform_target(instr);
360 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
362 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
363 return branch_target(instr) == addr;
368 int translate_branch(unsigned int *instr, const unsigned int *dest,
369 const unsigned int *src)
371 unsigned long target;
373 target = branch_target(src);
375 if (instr_is_branch_iform(*src))
376 return create_branch(instr, dest, target, ppc_inst_val(*src));
377 else if (instr_is_branch_bform(*src))
378 return create_cond_branch(instr, dest, target, ppc_inst_val(*src));
383 #ifdef CONFIG_PPC_BOOK3E_64
384 void __patch_exception(int exc, unsigned long addr)
386 extern unsigned int interrupt_base_book3e;
387 unsigned int *ibase = &interrupt_base_book3e;
389 /* Our exceptions vectors start with a NOP and -then- a branch
390 * to deal with single stepping from userspace which stops on
391 * the second instruction. Thus we need to patch the second
392 * instruction of the exception, not the first one
395 patch_branch(ibase + (exc / 4) + 1, addr, 0);
399 #ifdef CONFIG_CODE_PATCHING_SELFTEST
401 static void __init test_trampoline(void)
407 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
409 static void __init test_branch_iform(void)
415 addr = (unsigned long)&instr;
417 /* The simplest case, branch to self, no flags */
418 check(instr_is_branch_iform(ppc_inst(0x48000000)));
419 /* All bits of target set, and flags */
420 check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
421 /* High bit of opcode set, which is wrong */
422 check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
423 /* Middle bits of opcode set, which is wrong */
424 check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
426 /* Simplest case, branch to self with link */
427 check(instr_is_branch_iform(ppc_inst(0x48000001)));
428 /* All bits of targets set */
429 check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
430 /* Some bits of targets set */
431 check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
432 /* Must be a valid branch to start with */
433 check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
435 /* Absolute branch to 0x100 */
436 instr = ppc_inst(0x48000103);
437 check(instr_is_branch_to_addr(&instr, 0x100));
438 /* Absolute branch to 0x420fc */
439 instr = ppc_inst(0x480420ff);
440 check(instr_is_branch_to_addr(&instr, 0x420fc));
441 /* Maximum positive relative branch, + 20MB - 4B */
442 instr = ppc_inst(0x49fffffc);
443 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
444 /* Smallest negative relative branch, - 4B */
445 instr = ppc_inst(0x4bfffffc);
446 check(instr_is_branch_to_addr(&instr, addr - 4));
447 /* Largest negative relative branch, - 32 MB */
448 instr = ppc_inst(0x4a000000);
449 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
451 /* Branch to self, with link */
452 err = create_branch(&instr, &instr, addr, BRANCH_SET_LINK);
453 check(instr_is_branch_to_addr(&instr, addr));
455 /* Branch to self - 0x100, with link */
456 err = create_branch(&instr, &instr, addr - 0x100, BRANCH_SET_LINK);
457 check(instr_is_branch_to_addr(&instr, addr - 0x100));
459 /* Branch to self + 0x100, no link */
460 err = create_branch(&instr, &instr, addr + 0x100, 0);
461 check(instr_is_branch_to_addr(&instr, addr + 0x100));
463 /* Maximum relative negative offset, - 32 MB */
464 err = create_branch(&instr, &instr, addr - 0x2000000, BRANCH_SET_LINK);
465 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
467 /* Out of range relative negative offset, - 32 MB + 4*/
468 err = create_branch(&instr, &instr, addr - 0x2000004, BRANCH_SET_LINK);
471 /* Out of range relative positive offset, + 32 MB */
472 err = create_branch(&instr, &instr, addr + 0x2000000, BRANCH_SET_LINK);
475 /* Unaligned target */
476 err = create_branch(&instr, &instr, addr + 3, BRANCH_SET_LINK);
479 /* Check flags are masked correctly */
480 err = create_branch(&instr, &instr, addr, 0xFFFFFFFC);
481 check(instr_is_branch_to_addr(&instr, addr));
482 check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
485 static void __init test_create_function_call(void)
491 /* Check we can create a function call */
492 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
493 dest = ppc_function_entry(test_create_function_call);
494 create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
495 patch_instruction(iptr, instr);
496 check(instr_is_branch_to_addr(iptr, dest));
499 static void __init test_branch_bform(void)
503 unsigned int *iptr, instr, flags;
506 addr = (unsigned long)iptr;
508 /* The simplest case, branch to self, no flags */
509 check(instr_is_branch_bform(ppc_inst(0x40000000)));
510 /* All bits of target set, and flags */
511 check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
512 /* High bit of opcode set, which is wrong */
513 check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
514 /* Middle bits of opcode set, which is wrong */
515 check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
517 /* Absolute conditional branch to 0x100 */
518 instr = ppc_inst(0x43ff0103);
519 check(instr_is_branch_to_addr(&instr, 0x100));
520 /* Absolute conditional branch to 0x20fc */
521 instr = ppc_inst(0x43ff20ff);
522 check(instr_is_branch_to_addr(&instr, 0x20fc));
523 /* Maximum positive relative conditional branch, + 32 KB - 4B */
524 instr = ppc_inst(0x43ff7ffc);
525 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
526 /* Smallest negative relative conditional branch, - 4B */
527 instr = ppc_inst(0x43fffffc);
528 check(instr_is_branch_to_addr(&instr, addr - 4));
529 /* Largest negative relative conditional branch, - 32 KB */
530 instr = ppc_inst(0x43ff8000);
531 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
533 /* All condition code bits set & link */
534 flags = 0x3ff000 | BRANCH_SET_LINK;
537 err = create_cond_branch(&instr, iptr, addr, flags);
538 check(instr_is_branch_to_addr(&instr, addr));
540 /* Branch to self - 0x100 */
541 err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
542 check(instr_is_branch_to_addr(&instr, addr - 0x100));
544 /* Branch to self + 0x100 */
545 err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
546 check(instr_is_branch_to_addr(&instr, addr + 0x100));
548 /* Maximum relative negative offset, - 32 KB */
549 err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
550 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
552 /* Out of range relative negative offset, - 32 KB + 4*/
553 err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
556 /* Out of range relative positive offset, + 32 KB */
557 err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
560 /* Unaligned target */
561 err = create_cond_branch(&instr, iptr, addr + 3, flags);
564 /* Check flags are masked correctly */
565 err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
566 check(instr_is_branch_to_addr(&instr, addr));
567 check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
570 static void __init test_translate_branch(void)
577 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
582 /* Simple case, branch to self moved a little */
584 addr = (unsigned long)p;
585 patch_branch(p, addr, 0);
586 check(instr_is_branch_to_addr(p, addr));
588 translate_branch(&instr, q, p);
589 patch_instruction(q, instr);
590 check(instr_is_branch_to_addr(q, addr));
592 /* Maximum negative case, move b . to addr + 32 MB */
594 addr = (unsigned long)p;
595 patch_branch(p, addr, 0);
597 translate_branch(&instr, q, p);
598 patch_instruction(q, instr);
599 check(instr_is_branch_to_addr(p, addr));
600 check(instr_is_branch_to_addr(q, addr));
601 check(ppc_inst_equal(*q, ppc_inst(0x4a000000)));
603 /* Maximum positive case, move x to x - 32 MB + 4 */
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(*q, ppc_inst(0x49fffffc)));
614 /* Jump to x + 16 MB moved to x + 20 MB */
616 addr = 0x1000000 + (unsigned long)buf;
617 patch_branch(p, addr, BRANCH_SET_LINK);
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));
624 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
626 addr = 0x2000000 + (unsigned long)buf;
627 patch_branch(p, addr, 0);
629 translate_branch(&instr, q, p);
630 patch_instruction(q, instr);
631 check(instr_is_branch_to_addr(p, addr));
632 check(instr_is_branch_to_addr(q, addr));
635 /* Conditional branch tests */
637 /* Simple case, branch to self moved a little */
639 addr = (unsigned long)p;
640 create_cond_branch(&instr, p, addr, 0);
641 patch_instruction(p, instr);
642 check(instr_is_branch_to_addr(p, addr));
644 translate_branch(&instr, q, p);
645 patch_instruction(q, instr);
646 check(instr_is_branch_to_addr(q, addr));
648 /* Maximum negative case, move b . to addr + 32 KB */
650 addr = (unsigned long)p;
651 create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
652 patch_instruction(p, instr);
654 translate_branch(&instr, q, p);
655 patch_instruction(q, instr);
656 check(instr_is_branch_to_addr(p, addr));
657 check(instr_is_branch_to_addr(q, addr));
658 check(ppc_inst_equal(*q, ppc_inst(0x43ff8000)));
660 /* Maximum positive case, move x to x - 32 KB + 4 */
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(*q, ppc_inst(0x43ff7ffc)));
672 /* Jump to x + 12 KB moved to x + 20 KB */
674 addr = 0x3000 + (unsigned long)buf;
675 create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
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));
683 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
685 addr = 0x4000 + (unsigned long)buf;
686 create_cond_branch(&instr, p, addr, 0);
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 /* Free the buffer we were using */
698 static int __init test_code_patching(void)
700 printk(KERN_DEBUG "Running code patching self-tests ...\n");
704 test_create_function_call();
705 test_translate_branch();
709 late_initcall(test_code_patching);
711 #endif /* CONFIG_CODE_PATCHING_SELFTEST */