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>
26 static int __patch_instruction(unsigned int *addr, unsigned int instr)
30 __put_user_size(instr, addr, 4, err);
34 asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" :: "r" (addr));
39 #ifdef CONFIG_STRICT_KERNEL_RWX
40 static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
42 static int text_area_cpu_up(unsigned int cpu)
44 struct vm_struct *area;
46 area = get_vm_area(PAGE_SIZE, VM_ALLOC);
48 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
52 this_cpu_write(text_poke_area, area);
57 static int text_area_cpu_down(unsigned int cpu)
59 free_vm_area(this_cpu_read(text_poke_area));
64 * Run as a late init call. This allows all the boot time patching to be done
65 * simply by patching the code, and then we're called here prior to
66 * mark_rodata_ro(), which happens after all init calls are run. Although
67 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
68 * it as being preferable to a kernel that will crash later when someone tries
69 * to use patch_instruction().
71 static int __init setup_text_poke_area(void)
73 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
74 "powerpc/text_poke:online", text_area_cpu_up,
79 late_initcall(setup_text_poke_area);
82 * This can be called for kernel text or a module.
84 static int map_patch_area(void *addr, unsigned long text_poke_addr)
89 if (is_vmalloc_addr(addr))
90 pfn = vmalloc_to_pfn(addr);
92 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
94 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT),
95 pgprot_val(PAGE_KERNEL));
97 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
104 static inline int unmap_patch_area(unsigned long addr)
111 pgdp = pgd_offset_k(addr);
115 pudp = pud_offset(pgdp, addr);
119 pmdp = pmd_offset(pudp, addr);
123 ptep = pte_offset_kernel(pmdp, addr);
127 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
130 * In hash, pte_clear flushes the tlb, in radix, we have to
132 pte_clear(&init_mm, addr, ptep);
133 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
138 int patch_instruction(unsigned int *addr, unsigned int instr)
141 unsigned int *dest = NULL;
143 unsigned long text_poke_addr;
144 unsigned long kaddr = (unsigned long)addr;
147 * During early early boot patch_instruction is called
148 * when text_poke_area is not ready, but we still need
149 * to allow patching. We just do the plain old patching
151 if (!this_cpu_read(*PTRRELOC(&text_poke_area)))
152 return __patch_instruction(addr, instr);
154 local_irq_save(flags);
156 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
157 if (map_patch_area(addr, text_poke_addr)) {
162 dest = (unsigned int *)(text_poke_addr) +
163 ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
166 * We use __put_user_size so that we can handle faults while
167 * writing to dest and return err to handle faults gracefully
169 __put_user_size(instr, dest, 4, err);
171 asm ("dcbst 0, %0; sync; icbi 0,%0; icbi 0,%1; sync; isync"
172 ::"r" (dest), "r"(addr));
174 err = unmap_patch_area(text_poke_addr);
176 pr_warn("failed to unmap %lx\n", text_poke_addr);
179 local_irq_restore(flags);
183 #else /* !CONFIG_STRICT_KERNEL_RWX */
185 int patch_instruction(unsigned int *addr, unsigned int instr)
187 return __patch_instruction(addr, instr);
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
191 NOKPROBE_SYMBOL(patch_instruction);
193 int patch_branch(unsigned int *addr, unsigned long target, int flags)
195 return patch_instruction(addr, create_branch(addr, target, flags));
198 bool is_offset_in_branch_range(long offset)
201 * Powerpc branch instruction is :
204 * +---------+----------------+---+---+
205 * | opcode | LI |AA |LK |
206 * +---------+----------------+---+---+
207 * Where AA = 0 and LK = 0
209 * LI is a signed 24 bits integer. The real branch offset is computed
210 * by: imm32 = SignExtend(LI:'0b00', 32);
212 * So the maximum forward branch should be:
213 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
214 * The maximum backward branch should be:
215 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
217 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
221 * Helper to check if a given instruction is a conditional branch
222 * Derived from the conditional checks in analyse_instr()
224 bool is_conditional_branch(unsigned int instr)
226 unsigned int opcode = instr >> 26;
228 if (opcode == 16) /* bc, bca, bcl, bcla */
231 switch ((instr >> 1) & 0x3ff) {
232 case 16: /* bclr, bclrl */
233 case 528: /* bcctr, bcctrl */
234 case 560: /* bctar, bctarl */
240 NOKPROBE_SYMBOL(is_conditional_branch);
242 unsigned int create_branch(const unsigned int *addr,
243 unsigned long target, int flags)
245 unsigned int instruction;
249 if (! (flags & BRANCH_ABSOLUTE))
250 offset = offset - (unsigned long)addr;
252 /* Check we can represent the target in the instruction format */
253 if (!is_offset_in_branch_range(offset))
256 /* Mask out the flags and target, so they don't step on each other. */
257 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
262 unsigned int create_cond_branch(const unsigned int *addr,
263 unsigned long target, int flags)
265 unsigned int instruction;
269 if (! (flags & BRANCH_ABSOLUTE))
270 offset = offset - (unsigned long)addr;
272 /* Check we can represent the target in the instruction format */
273 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
276 /* Mask out the flags and target, so they don't step on each other. */
277 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
282 static unsigned int branch_opcode(unsigned int instr)
284 return (instr >> 26) & 0x3F;
287 static int instr_is_branch_iform(unsigned int instr)
289 return branch_opcode(instr) == 18;
292 static int instr_is_branch_bform(unsigned int instr)
294 return branch_opcode(instr) == 16;
297 int instr_is_relative_branch(unsigned int instr)
299 if (instr & BRANCH_ABSOLUTE)
302 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
305 static unsigned long branch_iform_target(const unsigned int *instr)
309 imm = *instr & 0x3FFFFFC;
311 /* If the top bit of the immediate value is set this is negative */
315 if ((*instr & BRANCH_ABSOLUTE) == 0)
316 imm += (unsigned long)instr;
318 return (unsigned long)imm;
321 static unsigned long branch_bform_target(const unsigned int *instr)
325 imm = *instr & 0xFFFC;
327 /* If the top bit of the immediate value is set this is negative */
331 if ((*instr & BRANCH_ABSOLUTE) == 0)
332 imm += (unsigned long)instr;
334 return (unsigned long)imm;
337 unsigned long branch_target(const unsigned int *instr)
339 if (instr_is_branch_iform(*instr))
340 return branch_iform_target(instr);
341 else if (instr_is_branch_bform(*instr))
342 return branch_bform_target(instr);
347 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
349 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
350 return branch_target(instr) == addr;
355 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
357 unsigned long target;
359 target = branch_target(src);
361 if (instr_is_branch_iform(*src))
362 return create_branch(dest, target, *src);
363 else if (instr_is_branch_bform(*src))
364 return create_cond_branch(dest, target, *src);
369 #ifdef CONFIG_PPC_BOOK3E_64
370 void __patch_exception(int exc, unsigned long addr)
372 extern unsigned int interrupt_base_book3e;
373 unsigned int *ibase = &interrupt_base_book3e;
375 /* Our exceptions vectors start with a NOP and -then- a branch
376 * to deal with single stepping from userspace which stops on
377 * the second instruction. Thus we need to patch the second
378 * instruction of the exception, not the first one
381 patch_branch(ibase + (exc / 4) + 1, addr, 0);
385 #ifdef CONFIG_CODE_PATCHING_SELFTEST
387 static void __init test_trampoline(void)
393 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
395 static void __init test_branch_iform(void)
400 addr = (unsigned long)&instr;
402 /* The simplest case, branch to self, no flags */
403 check(instr_is_branch_iform(0x48000000));
404 /* All bits of target set, and flags */
405 check(instr_is_branch_iform(0x4bffffff));
406 /* High bit of opcode set, which is wrong */
407 check(!instr_is_branch_iform(0xcbffffff));
408 /* Middle bits of opcode set, which is wrong */
409 check(!instr_is_branch_iform(0x7bffffff));
411 /* Simplest case, branch to self with link */
412 check(instr_is_branch_iform(0x48000001));
413 /* All bits of targets set */
414 check(instr_is_branch_iform(0x4bfffffd));
415 /* Some bits of targets set */
416 check(instr_is_branch_iform(0x4bff00fd));
417 /* Must be a valid branch to start with */
418 check(!instr_is_branch_iform(0x7bfffffd));
420 /* Absolute branch to 0x100 */
422 check(instr_is_branch_to_addr(&instr, 0x100));
423 /* Absolute branch to 0x420fc */
425 check(instr_is_branch_to_addr(&instr, 0x420fc));
426 /* Maximum positive relative branch, + 20MB - 4B */
428 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
429 /* Smallest negative relative branch, - 4B */
431 check(instr_is_branch_to_addr(&instr, addr - 4));
432 /* Largest negative relative branch, - 32 MB */
434 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
436 /* Branch to self, with link */
437 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
438 check(instr_is_branch_to_addr(&instr, addr));
440 /* Branch to self - 0x100, with link */
441 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
442 check(instr_is_branch_to_addr(&instr, addr - 0x100));
444 /* Branch to self + 0x100, no link */
445 instr = create_branch(&instr, addr + 0x100, 0);
446 check(instr_is_branch_to_addr(&instr, addr + 0x100));
448 /* Maximum relative negative offset, - 32 MB */
449 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
450 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
452 /* Out of range relative negative offset, - 32 MB + 4*/
453 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
456 /* Out of range relative positive offset, + 32 MB */
457 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
460 /* Unaligned target */
461 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
464 /* Check flags are masked correctly */
465 instr = create_branch(&instr, addr, 0xFFFFFFFC);
466 check(instr_is_branch_to_addr(&instr, addr));
467 check(instr == 0x48000000);
470 static void __init test_create_function_call(void)
475 /* Check we can create a function call */
476 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
477 dest = ppc_function_entry(test_create_function_call);
478 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
479 check(instr_is_branch_to_addr(iptr, dest));
482 static void __init test_branch_bform(void)
485 unsigned int *iptr, instr, flags;
488 addr = (unsigned long)iptr;
490 /* The simplest case, branch to self, no flags */
491 check(instr_is_branch_bform(0x40000000));
492 /* All bits of target set, and flags */
493 check(instr_is_branch_bform(0x43ffffff));
494 /* High bit of opcode set, which is wrong */
495 check(!instr_is_branch_bform(0xc3ffffff));
496 /* Middle bits of opcode set, which is wrong */
497 check(!instr_is_branch_bform(0x7bffffff));
499 /* Absolute conditional branch to 0x100 */
501 check(instr_is_branch_to_addr(&instr, 0x100));
502 /* Absolute conditional branch to 0x20fc */
504 check(instr_is_branch_to_addr(&instr, 0x20fc));
505 /* Maximum positive relative conditional branch, + 32 KB - 4B */
507 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
508 /* Smallest negative relative conditional branch, - 4B */
510 check(instr_is_branch_to_addr(&instr, addr - 4));
511 /* Largest negative relative conditional branch, - 32 KB */
513 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
515 /* All condition code bits set & link */
516 flags = 0x3ff000 | BRANCH_SET_LINK;
519 instr = create_cond_branch(iptr, addr, flags);
520 check(instr_is_branch_to_addr(&instr, addr));
522 /* Branch to self - 0x100 */
523 instr = create_cond_branch(iptr, addr - 0x100, flags);
524 check(instr_is_branch_to_addr(&instr, addr - 0x100));
526 /* Branch to self + 0x100 */
527 instr = create_cond_branch(iptr, addr + 0x100, flags);
528 check(instr_is_branch_to_addr(&instr, addr + 0x100));
530 /* Maximum relative negative offset, - 32 KB */
531 instr = create_cond_branch(iptr, addr - 0x8000, flags);
532 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
534 /* Out of range relative negative offset, - 32 KB + 4*/
535 instr = create_cond_branch(iptr, addr - 0x8004, flags);
538 /* Out of range relative positive offset, + 32 KB */
539 instr = create_cond_branch(iptr, addr + 0x8000, flags);
542 /* Unaligned target */
543 instr = create_cond_branch(iptr, addr + 3, flags);
546 /* Check flags are masked correctly */
547 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
548 check(instr_is_branch_to_addr(&instr, addr));
549 check(instr == 0x43FF0000);
552 static void __init test_translate_branch(void)
558 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
563 /* Simple case, branch to self moved a little */
565 addr = (unsigned long)p;
566 patch_branch(p, addr, 0);
567 check(instr_is_branch_to_addr(p, addr));
569 patch_instruction(q, translate_branch(q, p));
570 check(instr_is_branch_to_addr(q, addr));
572 /* Maximum negative case, move b . to addr + 32 MB */
574 addr = (unsigned long)p;
575 patch_branch(p, addr, 0);
577 patch_instruction(q, translate_branch(q, p));
578 check(instr_is_branch_to_addr(p, addr));
579 check(instr_is_branch_to_addr(q, addr));
580 check(*q == 0x4a000000);
582 /* Maximum positive case, move x to x - 32 MB + 4 */
584 addr = (unsigned long)p;
585 patch_branch(p, addr, 0);
587 patch_instruction(q, translate_branch(q, p));
588 check(instr_is_branch_to_addr(p, addr));
589 check(instr_is_branch_to_addr(q, addr));
590 check(*q == 0x49fffffc);
592 /* Jump to x + 16 MB moved to x + 20 MB */
594 addr = 0x1000000 + (unsigned long)buf;
595 patch_branch(p, addr, BRANCH_SET_LINK);
597 patch_instruction(q, translate_branch(q, p));
598 check(instr_is_branch_to_addr(p, addr));
599 check(instr_is_branch_to_addr(q, addr));
601 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
603 addr = 0x2000000 + (unsigned long)buf;
604 patch_branch(p, addr, 0);
606 patch_instruction(q, translate_branch(q, p));
607 check(instr_is_branch_to_addr(p, addr));
608 check(instr_is_branch_to_addr(q, addr));
611 /* Conditional branch tests */
613 /* Simple case, branch to self moved a little */
615 addr = (unsigned long)p;
616 patch_instruction(p, create_cond_branch(p, addr, 0));
617 check(instr_is_branch_to_addr(p, addr));
619 patch_instruction(q, translate_branch(q, p));
620 check(instr_is_branch_to_addr(q, addr));
622 /* Maximum negative case, move b . to addr + 32 KB */
624 addr = (unsigned long)p;
625 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
627 patch_instruction(q, translate_branch(q, p));
628 check(instr_is_branch_to_addr(p, addr));
629 check(instr_is_branch_to_addr(q, addr));
630 check(*q == 0x43ff8000);
632 /* Maximum positive case, move x to x - 32 KB + 4 */
634 addr = (unsigned long)p;
635 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
637 patch_instruction(q, translate_branch(q, p));
638 check(instr_is_branch_to_addr(p, addr));
639 check(instr_is_branch_to_addr(q, addr));
640 check(*q == 0x43ff7ffc);
642 /* Jump to x + 12 KB moved to x + 20 KB */
644 addr = 0x3000 + (unsigned long)buf;
645 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
647 patch_instruction(q, translate_branch(q, p));
648 check(instr_is_branch_to_addr(p, addr));
649 check(instr_is_branch_to_addr(q, addr));
651 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
653 addr = 0x4000 + (unsigned long)buf;
654 patch_instruction(p, create_cond_branch(p, addr, 0));
656 patch_instruction(q, translate_branch(q, p));
657 check(instr_is_branch_to_addr(p, addr));
658 check(instr_is_branch_to_addr(q, addr));
660 /* Free the buffer we were using */
664 static int __init test_code_patching(void)
666 printk(KERN_DEBUG "Running code patching self-tests ...\n");
670 test_create_function_call();
671 test_translate_branch();
675 late_initcall(test_code_patching);
677 #endif /* CONFIG_CODE_PATCHING_SELFTEST */