2 * Copyright 2008 Michael Ellerman, IBM Corporation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/kernel.h>
11 #include <linux/kprobes.h>
12 #include <linux/vmalloc.h>
13 #include <linux/init.h>
15 #include <linux/cpuhotplug.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/kprobes.h>
20 #include <asm/pgtable.h>
21 #include <asm/tlbflush.h>
23 #include <asm/code-patching.h>
24 #include <asm/setup.h>
25 #include <asm/sections.h>
27 static int __patch_instruction(unsigned int *exec_addr, unsigned int instr,
28 unsigned int *patch_addr)
32 __put_user_size(instr, patch_addr, 4, err);
36 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
42 int raw_patch_instruction(unsigned int *addr, unsigned int 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),
103 pgprot_val(PAGE_KERNEL));
105 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
112 static inline int unmap_patch_area(unsigned long addr)
119 pgdp = pgd_offset_k(addr);
123 pudp = pud_offset(pgdp, addr);
127 pmdp = pmd_offset(pudp, addr);
131 ptep = pte_offset_kernel(pmdp, addr);
135 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
138 * In hash, pte_clear flushes the tlb, in radix, we have to
140 pte_clear(&init_mm, addr, ptep);
141 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
146 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
149 unsigned int *patch_addr = NULL;
151 unsigned long text_poke_addr;
152 unsigned long kaddr = (unsigned long)addr;
155 * During early early boot patch_instruction is called
156 * when text_poke_area is not ready, but we still need
157 * to allow patching. We just do the plain old patching
159 if (!this_cpu_read(text_poke_area))
160 return raw_patch_instruction(addr, instr);
162 local_irq_save(flags);
164 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
165 if (map_patch_area(addr, text_poke_addr)) {
170 patch_addr = (unsigned int *)(text_poke_addr) +
171 ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
173 __patch_instruction(addr, instr, patch_addr);
175 err = unmap_patch_area(text_poke_addr);
177 pr_warn("failed to unmap %lx\n", text_poke_addr);
180 local_irq_restore(flags);
184 #else /* !CONFIG_STRICT_KERNEL_RWX */
186 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
188 return raw_patch_instruction(addr, instr);
191 #endif /* CONFIG_STRICT_KERNEL_RWX */
193 int patch_instruction(unsigned int *addr, unsigned int instr)
195 /* Make sure we aren't patching a freed init section */
196 if (init_mem_is_free && init_section_contains(addr, 4)) {
197 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
200 return do_patch_instruction(addr, instr);
202 NOKPROBE_SYMBOL(patch_instruction);
204 int patch_branch(unsigned int *addr, unsigned long target, int flags)
206 return patch_instruction(addr, create_branch(addr, target, flags));
209 int patch_branch_site(s32 *site, unsigned long target, int flags)
213 addr = (unsigned int *)((unsigned long)site + *site);
214 return patch_instruction(addr, create_branch(addr, target, flags));
217 int patch_instruction_site(s32 *site, unsigned int instr)
221 addr = (unsigned int *)((unsigned long)site + *site);
222 return patch_instruction(addr, instr);
225 bool is_offset_in_branch_range(long offset)
228 * Powerpc branch instruction is :
231 * +---------+----------------+---+---+
232 * | opcode | LI |AA |LK |
233 * +---------+----------------+---+---+
234 * Where AA = 0 and LK = 0
236 * LI is a signed 24 bits integer. The real branch offset is computed
237 * by: imm32 = SignExtend(LI:'0b00', 32);
239 * So the maximum forward branch should be:
240 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
241 * The maximum backward branch should be:
242 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
244 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
247 bool is_offset_in_cond_branch_range(long offset)
249 return offset >= -0x8000 && offset <= 0x7fff && !(offset & 0x3);
253 * Helper to check if a given instruction is a conditional branch
254 * Derived from the conditional checks in analyse_instr()
256 bool is_conditional_branch(unsigned int instr)
258 unsigned int opcode = instr >> 26;
260 if (opcode == 16) /* bc, bca, bcl, bcla */
263 switch ((instr >> 1) & 0x3ff) {
264 case 16: /* bclr, bclrl */
265 case 528: /* bcctr, bcctrl */
266 case 560: /* bctar, bctarl */
272 NOKPROBE_SYMBOL(is_conditional_branch);
274 unsigned int create_branch(const unsigned int *addr,
275 unsigned long target, int flags)
277 unsigned int instruction;
281 if (! (flags & BRANCH_ABSOLUTE))
282 offset = offset - (unsigned long)addr;
284 /* Check we can represent the target in the instruction format */
285 if (!is_offset_in_branch_range(offset))
288 /* Mask out the flags and target, so they don't step on each other. */
289 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
294 unsigned int create_cond_branch(const unsigned int *addr,
295 unsigned long target, int flags)
297 unsigned int instruction;
301 if (! (flags & BRANCH_ABSOLUTE))
302 offset = offset - (unsigned long)addr;
304 /* Check we can represent the target in the instruction format */
305 if (!is_offset_in_cond_branch_range(offset))
308 /* Mask out the flags and target, so they don't step on each other. */
309 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
314 static unsigned int branch_opcode(unsigned int instr)
316 return (instr >> 26) & 0x3F;
319 static int instr_is_branch_iform(unsigned int instr)
321 return branch_opcode(instr) == 18;
324 static int instr_is_branch_bform(unsigned int instr)
326 return branch_opcode(instr) == 16;
329 int instr_is_relative_branch(unsigned int instr)
331 if (instr & BRANCH_ABSOLUTE)
334 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
337 int instr_is_relative_link_branch(unsigned int instr)
339 return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
342 static unsigned long branch_iform_target(const unsigned int *instr)
346 imm = *instr & 0x3FFFFFC;
348 /* If the top bit of the immediate value is set this is negative */
352 if ((*instr & BRANCH_ABSOLUTE) == 0)
353 imm += (unsigned long)instr;
355 return (unsigned long)imm;
358 static unsigned long branch_bform_target(const unsigned int *instr)
362 imm = *instr & 0xFFFC;
364 /* If the top bit of the immediate value is set this is negative */
368 if ((*instr & BRANCH_ABSOLUTE) == 0)
369 imm += (unsigned long)instr;
371 return (unsigned long)imm;
374 unsigned long branch_target(const unsigned int *instr)
376 if (instr_is_branch_iform(*instr))
377 return branch_iform_target(instr);
378 else if (instr_is_branch_bform(*instr))
379 return branch_bform_target(instr);
384 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
386 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
387 return branch_target(instr) == addr;
392 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
394 unsigned long target;
396 target = branch_target(src);
398 if (instr_is_branch_iform(*src))
399 return create_branch(dest, target, *src);
400 else if (instr_is_branch_bform(*src))
401 return create_cond_branch(dest, target, *src);
406 #ifdef CONFIG_PPC_BOOK3E_64
407 void __patch_exception(int exc, unsigned long addr)
409 extern unsigned int interrupt_base_book3e;
410 unsigned int *ibase = &interrupt_base_book3e;
412 /* Our exceptions vectors start with a NOP and -then- a branch
413 * to deal with single stepping from userspace which stops on
414 * the second instruction. Thus we need to patch the second
415 * instruction of the exception, not the first one
418 patch_branch(ibase + (exc / 4) + 1, addr, 0);
422 #ifdef CONFIG_CODE_PATCHING_SELFTEST
424 static void __init test_trampoline(void)
430 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
432 static void __init test_branch_iform(void)
437 addr = (unsigned long)&instr;
439 /* The simplest case, branch to self, no flags */
440 check(instr_is_branch_iform(0x48000000));
441 /* All bits of target set, and flags */
442 check(instr_is_branch_iform(0x4bffffff));
443 /* High bit of opcode set, which is wrong */
444 check(!instr_is_branch_iform(0xcbffffff));
445 /* Middle bits of opcode set, which is wrong */
446 check(!instr_is_branch_iform(0x7bffffff));
448 /* Simplest case, branch to self with link */
449 check(instr_is_branch_iform(0x48000001));
450 /* All bits of targets set */
451 check(instr_is_branch_iform(0x4bfffffd));
452 /* Some bits of targets set */
453 check(instr_is_branch_iform(0x4bff00fd));
454 /* Must be a valid branch to start with */
455 check(!instr_is_branch_iform(0x7bfffffd));
457 /* Absolute branch to 0x100 */
459 check(instr_is_branch_to_addr(&instr, 0x100));
460 /* Absolute branch to 0x420fc */
462 check(instr_is_branch_to_addr(&instr, 0x420fc));
463 /* Maximum positive relative branch, + 20MB - 4B */
465 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
466 /* Smallest negative relative branch, - 4B */
468 check(instr_is_branch_to_addr(&instr, addr - 4));
469 /* Largest negative relative branch, - 32 MB */
471 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
473 /* Branch to self, with link */
474 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
475 check(instr_is_branch_to_addr(&instr, addr));
477 /* Branch to self - 0x100, with link */
478 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
479 check(instr_is_branch_to_addr(&instr, addr - 0x100));
481 /* Branch to self + 0x100, no link */
482 instr = create_branch(&instr, addr + 0x100, 0);
483 check(instr_is_branch_to_addr(&instr, addr + 0x100));
485 /* Maximum relative negative offset, - 32 MB */
486 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
487 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
489 /* Out of range relative negative offset, - 32 MB + 4*/
490 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
493 /* Out of range relative positive offset, + 32 MB */
494 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
497 /* Unaligned target */
498 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
501 /* Check flags are masked correctly */
502 instr = create_branch(&instr, addr, 0xFFFFFFFC);
503 check(instr_is_branch_to_addr(&instr, addr));
504 check(instr == 0x48000000);
507 static void __init test_create_function_call(void)
512 /* Check we can create a function call */
513 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
514 dest = ppc_function_entry(test_create_function_call);
515 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
516 check(instr_is_branch_to_addr(iptr, dest));
519 static void __init test_branch_bform(void)
522 unsigned int *iptr, instr, flags;
525 addr = (unsigned long)iptr;
527 /* The simplest case, branch to self, no flags */
528 check(instr_is_branch_bform(0x40000000));
529 /* All bits of target set, and flags */
530 check(instr_is_branch_bform(0x43ffffff));
531 /* High bit of opcode set, which is wrong */
532 check(!instr_is_branch_bform(0xc3ffffff));
533 /* Middle bits of opcode set, which is wrong */
534 check(!instr_is_branch_bform(0x7bffffff));
536 /* Absolute conditional branch to 0x100 */
538 check(instr_is_branch_to_addr(&instr, 0x100));
539 /* Absolute conditional branch to 0x20fc */
541 check(instr_is_branch_to_addr(&instr, 0x20fc));
542 /* Maximum positive relative conditional branch, + 32 KB - 4B */
544 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
545 /* Smallest negative relative conditional branch, - 4B */
547 check(instr_is_branch_to_addr(&instr, addr - 4));
548 /* Largest negative relative conditional branch, - 32 KB */
550 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
552 /* All condition code bits set & link */
553 flags = 0x3ff000 | BRANCH_SET_LINK;
556 instr = create_cond_branch(iptr, addr, flags);
557 check(instr_is_branch_to_addr(&instr, addr));
559 /* Branch to self - 0x100 */
560 instr = create_cond_branch(iptr, addr - 0x100, flags);
561 check(instr_is_branch_to_addr(&instr, addr - 0x100));
563 /* Branch to self + 0x100 */
564 instr = create_cond_branch(iptr, addr + 0x100, flags);
565 check(instr_is_branch_to_addr(&instr, addr + 0x100));
567 /* Maximum relative negative offset, - 32 KB */
568 instr = create_cond_branch(iptr, addr - 0x8000, flags);
569 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
571 /* Out of range relative negative offset, - 32 KB + 4*/
572 instr = create_cond_branch(iptr, addr - 0x8004, flags);
575 /* Out of range relative positive offset, + 32 KB */
576 instr = create_cond_branch(iptr, addr + 0x8000, flags);
579 /* Unaligned target */
580 instr = create_cond_branch(iptr, addr + 3, flags);
583 /* Check flags are masked correctly */
584 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
585 check(instr_is_branch_to_addr(&instr, addr));
586 check(instr == 0x43FF0000);
589 static void __init test_translate_branch(void)
595 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
600 /* Simple case, branch to self moved a little */
602 addr = (unsigned long)p;
603 patch_branch(p, addr, 0);
604 check(instr_is_branch_to_addr(p, addr));
606 patch_instruction(q, translate_branch(q, p));
607 check(instr_is_branch_to_addr(q, addr));
609 /* Maximum negative case, move b . to addr + 32 MB */
611 addr = (unsigned long)p;
612 patch_branch(p, addr, 0);
614 patch_instruction(q, translate_branch(q, p));
615 check(instr_is_branch_to_addr(p, addr));
616 check(instr_is_branch_to_addr(q, addr));
617 check(*q == 0x4a000000);
619 /* Maximum positive case, move x to x - 32 MB + 4 */
621 addr = (unsigned long)p;
622 patch_branch(p, addr, 0);
624 patch_instruction(q, translate_branch(q, p));
625 check(instr_is_branch_to_addr(p, addr));
626 check(instr_is_branch_to_addr(q, addr));
627 check(*q == 0x49fffffc);
629 /* Jump to x + 16 MB moved to x + 20 MB */
631 addr = 0x1000000 + (unsigned long)buf;
632 patch_branch(p, addr, BRANCH_SET_LINK);
634 patch_instruction(q, translate_branch(q, p));
635 check(instr_is_branch_to_addr(p, addr));
636 check(instr_is_branch_to_addr(q, addr));
638 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
640 addr = 0x2000000 + (unsigned long)buf;
641 patch_branch(p, addr, 0);
643 patch_instruction(q, translate_branch(q, p));
644 check(instr_is_branch_to_addr(p, addr));
645 check(instr_is_branch_to_addr(q, addr));
648 /* Conditional branch tests */
650 /* Simple case, branch to self moved a little */
652 addr = (unsigned long)p;
653 patch_instruction(p, create_cond_branch(p, addr, 0));
654 check(instr_is_branch_to_addr(p, addr));
656 patch_instruction(q, translate_branch(q, p));
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 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
664 patch_instruction(q, translate_branch(q, p));
665 check(instr_is_branch_to_addr(p, addr));
666 check(instr_is_branch_to_addr(q, addr));
667 check(*q == 0x43ff8000);
669 /* Maximum positive case, move x to x - 32 KB + 4 */
671 addr = (unsigned long)p;
672 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
674 patch_instruction(q, translate_branch(q, p));
675 check(instr_is_branch_to_addr(p, addr));
676 check(instr_is_branch_to_addr(q, addr));
677 check(*q == 0x43ff7ffc);
679 /* Jump to x + 12 KB moved to x + 20 KB */
681 addr = 0x3000 + (unsigned long)buf;
682 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
684 patch_instruction(q, translate_branch(q, p));
685 check(instr_is_branch_to_addr(p, addr));
686 check(instr_is_branch_to_addr(q, addr));
688 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
690 addr = 0x4000 + (unsigned long)buf;
691 patch_instruction(p, create_cond_branch(p, addr, 0));
693 patch_instruction(q, translate_branch(q, p));
694 check(instr_is_branch_to_addr(p, addr));
695 check(instr_is_branch_to_addr(q, addr));
697 /* Free the buffer we were using */
701 static int __init test_code_patching(void)
703 printk(KERN_DEBUG "Running code patching self-tests ...\n");
707 test_create_function_call();
708 test_translate_branch();
712 late_initcall(test_code_patching);
714 #endif /* CONFIG_CODE_PATCHING_SELFTEST */