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>
25 static int __patch_instruction(unsigned int *addr, unsigned int instr)
29 __put_user_size(instr, addr, 4, err);
33 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" :: "r" (addr));
38 #ifdef CONFIG_STRICT_KERNEL_RWX
39 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
41 static int text_area_cpu_up(unsigned int cpu)
43 struct vm_struct *area;
45 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
47 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
51 this_cpu_write(text_poke_area, area);
56 static int text_area_cpu_down(unsigned int cpu)
58 free_vm_area(this_cpu_read(text_poke_area));
63 * Run as a late init call. This allows all the boot time patching to be done
64 * simply by patching the code, and then we're called here prior to
65 * mark_rodata_ro(), which happens after all init calls are run. Although
66 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
67 * it as being preferable to a kernel that will crash later when someone tries
68 * to use patch_instruction().
70 static int __init setup_text_poke_area(void)
72 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
73 "powerpc/text_poke:online", text_area_cpu_up,
78 late_initcall(setup_text_poke_area);
81 * This can be called for kernel text or a module.
83 static int map_patch_area(void *addr, unsigned long text_poke_addr)
88 if (is_vmalloc_addr(addr))
89 pfn = vmalloc_to_pfn(addr);
91 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
93 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT),
94 pgprot_val(PAGE_KERNEL));
96 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
103 static inline int unmap_patch_area(unsigned long addr)
110 pgdp = pgd_offset_k(addr);
114 pudp = pud_offset(pgdp, addr);
118 pmdp = pmd_offset(pudp, addr);
122 ptep = pte_offset_kernel(pmdp, addr);
126 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
129 * In hash, pte_clear flushes the tlb, in radix, we have to
131 pte_clear(&init_mm, addr, ptep);
132 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
137 int patch_instruction(unsigned int *addr, unsigned int instr)
140 unsigned int *dest = NULL;
142 unsigned long text_poke_addr;
143 unsigned long kaddr = (unsigned long)addr;
146 * During early early boot patch_instruction is called
147 * when text_poke_area is not ready, but we still need
148 * to allow patching. We just do the plain old patching
149 * We use slab_is_available and per cpu read * via this_cpu_read
150 * of text_poke_area. Per-CPU areas might not be up early
151 * this can create problems with just using this_cpu_read()
153 if (!slab_is_available() || !this_cpu_read(text_poke_area))
154 return __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 dest = (unsigned int *)(text_poke_addr) +
165 ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
168 * We use __put_user_size so that we can handle faults while
169 * writing to dest and return err to handle faults gracefully
171 __put_user_size(instr, dest, 4, err);
173 asm ("dcbst 0, %0; sync; icbi 0,%0; icbi 0,%1; sync; isync"
174 ::"r" (dest), "r"(addr));
176 err = unmap_patch_area(text_poke_addr);
178 pr_warn("failed to unmap %lx\n", text_poke_addr);
181 local_irq_restore(flags);
185 #else /* !CONFIG_STRICT_KERNEL_RWX */
187 int patch_instruction(unsigned int *addr, unsigned int instr)
189 return __patch_instruction(addr, instr);
192 #endif /* CONFIG_STRICT_KERNEL_RWX */
193 NOKPROBE_SYMBOL(patch_instruction);
195 int patch_branch(unsigned int *addr, unsigned long target, int flags)
197 return patch_instruction(addr, create_branch(addr, target, flags));
200 bool is_offset_in_branch_range(long offset)
203 * Powerpc branch instruction is :
206 * +---------+----------------+---+---+
207 * | opcode | LI |AA |LK |
208 * +---------+----------------+---+---+
209 * Where AA = 0 and LK = 0
211 * LI is a signed 24 bits integer. The real branch offset is computed
212 * by: imm32 = SignExtend(LI:'0b00', 32);
214 * So the maximum forward branch should be:
215 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
216 * The maximum backward branch should be:
217 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
219 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
223 * Helper to check if a given instruction is a conditional branch
224 * Derived from the conditional checks in analyse_instr()
226 bool is_conditional_branch(unsigned int instr)
228 unsigned int opcode = instr >> 26;
230 if (opcode == 16) /* bc, bca, bcl, bcla */
233 switch ((instr >> 1) & 0x3ff) {
234 case 16: /* bclr, bclrl */
235 case 528: /* bcctr, bcctrl */
236 case 560: /* bctar, bctarl */
242 NOKPROBE_SYMBOL(is_conditional_branch);
244 unsigned int create_branch(const unsigned int *addr,
245 unsigned long target, int flags)
247 unsigned int instruction;
251 if (! (flags & BRANCH_ABSOLUTE))
252 offset = offset - (unsigned long)addr;
254 /* Check we can represent the target in the instruction format */
255 if (!is_offset_in_branch_range(offset))
258 /* Mask out the flags and target, so they don't step on each other. */
259 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
264 unsigned int create_cond_branch(const unsigned int *addr,
265 unsigned long target, int flags)
267 unsigned int instruction;
271 if (! (flags & BRANCH_ABSOLUTE))
272 offset = offset - (unsigned long)addr;
274 /* Check we can represent the target in the instruction format */
275 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
278 /* Mask out the flags and target, so they don't step on each other. */
279 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
284 static unsigned int branch_opcode(unsigned int instr)
286 return (instr >> 26) & 0x3F;
289 static int instr_is_branch_iform(unsigned int instr)
291 return branch_opcode(instr) == 18;
294 static int instr_is_branch_bform(unsigned int instr)
296 return branch_opcode(instr) == 16;
299 int instr_is_relative_branch(unsigned int instr)
301 if (instr & BRANCH_ABSOLUTE)
304 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
307 static unsigned long branch_iform_target(const unsigned int *instr)
311 imm = *instr & 0x3FFFFFC;
313 /* If the top bit of the immediate value is set this is negative */
317 if ((*instr & BRANCH_ABSOLUTE) == 0)
318 imm += (unsigned long)instr;
320 return (unsigned long)imm;
323 static unsigned long branch_bform_target(const unsigned int *instr)
327 imm = *instr & 0xFFFC;
329 /* If the top bit of the immediate value is set this is negative */
333 if ((*instr & BRANCH_ABSOLUTE) == 0)
334 imm += (unsigned long)instr;
336 return (unsigned long)imm;
339 unsigned long branch_target(const unsigned int *instr)
341 if (instr_is_branch_iform(*instr))
342 return branch_iform_target(instr);
343 else if (instr_is_branch_bform(*instr))
344 return branch_bform_target(instr);
349 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
351 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
352 return branch_target(instr) == addr;
357 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
359 unsigned long target;
361 target = branch_target(src);
363 if (instr_is_branch_iform(*src))
364 return create_branch(dest, target, *src);
365 else if (instr_is_branch_bform(*src))
366 return create_cond_branch(dest, target, *src);
371 #ifdef CONFIG_PPC_BOOK3E_64
372 void __patch_exception(int exc, unsigned long addr)
374 extern unsigned int interrupt_base_book3e;
375 unsigned int *ibase = &interrupt_base_book3e;
377 /* Our exceptions vectors start with a NOP and -then- a branch
378 * to deal with single stepping from userspace which stops on
379 * the second instruction. Thus we need to patch the second
380 * instruction of the exception, not the first one
383 patch_branch(ibase + (exc / 4) + 1, addr, 0);
387 #ifdef CONFIG_CODE_PATCHING_SELFTEST
389 static void __init test_trampoline(void)
395 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
397 static void __init test_branch_iform(void)
402 addr = (unsigned long)&instr;
404 /* The simplest case, branch to self, no flags */
405 check(instr_is_branch_iform(0x48000000));
406 /* All bits of target set, and flags */
407 check(instr_is_branch_iform(0x4bffffff));
408 /* High bit of opcode set, which is wrong */
409 check(!instr_is_branch_iform(0xcbffffff));
410 /* Middle bits of opcode set, which is wrong */
411 check(!instr_is_branch_iform(0x7bffffff));
413 /* Simplest case, branch to self with link */
414 check(instr_is_branch_iform(0x48000001));
415 /* All bits of targets set */
416 check(instr_is_branch_iform(0x4bfffffd));
417 /* Some bits of targets set */
418 check(instr_is_branch_iform(0x4bff00fd));
419 /* Must be a valid branch to start with */
420 check(!instr_is_branch_iform(0x7bfffffd));
422 /* Absolute branch to 0x100 */
424 check(instr_is_branch_to_addr(&instr, 0x100));
425 /* Absolute branch to 0x420fc */
427 check(instr_is_branch_to_addr(&instr, 0x420fc));
428 /* Maximum positive relative branch, + 20MB - 4B */
430 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
431 /* Smallest negative relative branch, - 4B */
433 check(instr_is_branch_to_addr(&instr, addr - 4));
434 /* Largest negative relative branch, - 32 MB */
436 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
438 /* Branch to self, with link */
439 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
440 check(instr_is_branch_to_addr(&instr, addr));
442 /* Branch to self - 0x100, with link */
443 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
444 check(instr_is_branch_to_addr(&instr, addr - 0x100));
446 /* Branch to self + 0x100, no link */
447 instr = create_branch(&instr, addr + 0x100, 0);
448 check(instr_is_branch_to_addr(&instr, addr + 0x100));
450 /* Maximum relative negative offset, - 32 MB */
451 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
452 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
454 /* Out of range relative negative offset, - 32 MB + 4*/
455 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
458 /* Out of range relative positive offset, + 32 MB */
459 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
462 /* Unaligned target */
463 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
466 /* Check flags are masked correctly */
467 instr = create_branch(&instr, addr, 0xFFFFFFFC);
468 check(instr_is_branch_to_addr(&instr, addr));
469 check(instr == 0x48000000);
472 static void __init test_create_function_call(void)
477 /* Check we can create a function call */
478 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
479 dest = ppc_function_entry(test_create_function_call);
480 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
481 check(instr_is_branch_to_addr(iptr, dest));
484 static void __init test_branch_bform(void)
487 unsigned int *iptr, instr, flags;
490 addr = (unsigned long)iptr;
492 /* The simplest case, branch to self, no flags */
493 check(instr_is_branch_bform(0x40000000));
494 /* All bits of target set, and flags */
495 check(instr_is_branch_bform(0x43ffffff));
496 /* High bit of opcode set, which is wrong */
497 check(!instr_is_branch_bform(0xc3ffffff));
498 /* Middle bits of opcode set, which is wrong */
499 check(!instr_is_branch_bform(0x7bffffff));
501 /* Absolute conditional branch to 0x100 */
503 check(instr_is_branch_to_addr(&instr, 0x100));
504 /* Absolute conditional branch to 0x20fc */
506 check(instr_is_branch_to_addr(&instr, 0x20fc));
507 /* Maximum positive relative conditional branch, + 32 KB - 4B */
509 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
510 /* Smallest negative relative conditional branch, - 4B */
512 check(instr_is_branch_to_addr(&instr, addr - 4));
513 /* Largest negative relative conditional branch, - 32 KB */
515 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
517 /* All condition code bits set & link */
518 flags = 0x3ff000 | BRANCH_SET_LINK;
521 instr = create_cond_branch(iptr, addr, flags);
522 check(instr_is_branch_to_addr(&instr, addr));
524 /* Branch to self - 0x100 */
525 instr = create_cond_branch(iptr, addr - 0x100, flags);
526 check(instr_is_branch_to_addr(&instr, addr - 0x100));
528 /* Branch to self + 0x100 */
529 instr = create_cond_branch(iptr, addr + 0x100, flags);
530 check(instr_is_branch_to_addr(&instr, addr + 0x100));
532 /* Maximum relative negative offset, - 32 KB */
533 instr = create_cond_branch(iptr, addr - 0x8000, flags);
534 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
536 /* Out of range relative negative offset, - 32 KB + 4*/
537 instr = create_cond_branch(iptr, addr - 0x8004, flags);
540 /* Out of range relative positive offset, + 32 KB */
541 instr = create_cond_branch(iptr, addr + 0x8000, flags);
544 /* Unaligned target */
545 instr = create_cond_branch(iptr, addr + 3, flags);
548 /* Check flags are masked correctly */
549 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
550 check(instr_is_branch_to_addr(&instr, addr));
551 check(instr == 0x43FF0000);
554 static void __init test_translate_branch(void)
560 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
565 /* Simple case, branch to self moved a little */
567 addr = (unsigned long)p;
568 patch_branch(p, addr, 0);
569 check(instr_is_branch_to_addr(p, addr));
571 patch_instruction(q, translate_branch(q, p));
572 check(instr_is_branch_to_addr(q, addr));
574 /* Maximum negative case, move b . to addr + 32 MB */
576 addr = (unsigned long)p;
577 patch_branch(p, addr, 0);
579 patch_instruction(q, translate_branch(q, p));
580 check(instr_is_branch_to_addr(p, addr));
581 check(instr_is_branch_to_addr(q, addr));
582 check(*q == 0x4a000000);
584 /* Maximum positive case, move x to x - 32 MB + 4 */
586 addr = (unsigned long)p;
587 patch_branch(p, addr, 0);
589 patch_instruction(q, translate_branch(q, p));
590 check(instr_is_branch_to_addr(p, addr));
591 check(instr_is_branch_to_addr(q, addr));
592 check(*q == 0x49fffffc);
594 /* Jump to x + 16 MB moved to x + 20 MB */
596 addr = 0x1000000 + (unsigned long)buf;
597 patch_branch(p, addr, BRANCH_SET_LINK);
599 patch_instruction(q, translate_branch(q, p));
600 check(instr_is_branch_to_addr(p, addr));
601 check(instr_is_branch_to_addr(q, addr));
603 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
605 addr = 0x2000000 + (unsigned long)buf;
606 patch_branch(p, addr, 0);
608 patch_instruction(q, translate_branch(q, p));
609 check(instr_is_branch_to_addr(p, addr));
610 check(instr_is_branch_to_addr(q, addr));
613 /* Conditional branch tests */
615 /* Simple case, branch to self moved a little */
617 addr = (unsigned long)p;
618 patch_instruction(p, create_cond_branch(p, addr, 0));
619 check(instr_is_branch_to_addr(p, addr));
621 patch_instruction(q, translate_branch(q, p));
622 check(instr_is_branch_to_addr(q, addr));
624 /* Maximum negative case, move b . to addr + 32 KB */
626 addr = (unsigned long)p;
627 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
629 patch_instruction(q, translate_branch(q, p));
630 check(instr_is_branch_to_addr(p, addr));
631 check(instr_is_branch_to_addr(q, addr));
632 check(*q == 0x43ff8000);
634 /* Maximum positive case, move x to x - 32 KB + 4 */
636 addr = (unsigned long)p;
637 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
639 patch_instruction(q, translate_branch(q, p));
640 check(instr_is_branch_to_addr(p, addr));
641 check(instr_is_branch_to_addr(q, addr));
642 check(*q == 0x43ff7ffc);
644 /* Jump to x + 12 KB moved to x + 20 KB */
646 addr = 0x3000 + (unsigned long)buf;
647 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
649 patch_instruction(q, translate_branch(q, p));
650 check(instr_is_branch_to_addr(p, addr));
651 check(instr_is_branch_to_addr(q, addr));
653 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
655 addr = 0x4000 + (unsigned long)buf;
656 patch_instruction(p, create_cond_branch(p, addr, 0));
658 patch_instruction(q, translate_branch(q, p));
659 check(instr_is_branch_to_addr(p, addr));
660 check(instr_is_branch_to_addr(q, addr));
662 /* Free the buffer we were using */
666 static int __init test_code_patching(void)
668 printk(KERN_DEBUG "Running code patching self-tests ...\n");
672 test_create_function_call();
673 test_translate_branch();
677 late_initcall(test_code_patching);
679 #endif /* CONFIG_CODE_PATCHING_SELFTEST */