Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[linux/fpc-iii.git] / arch / powerpc / lib / code-patching.c
blob5ffee298745fe4e98a66d2410d86e6579d2b2560
1 /*
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.
8 */
10 #include <linux/kernel.h>
11 #include <linux/kprobes.h>
12 #include <linux/vmalloc.h>
13 #include <linux/init.h>
14 #include <linux/mm.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>
22 #include <asm/page.h>
23 #include <asm/code-patching.h>
24 #include <asm/setup.h>
26 static int __patch_instruction(unsigned int *exec_addr, unsigned int instr,
27 unsigned int *patch_addr)
29 int err;
31 __put_user_size(instr, patch_addr, 4, err);
32 if (err)
33 return err;
35 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
36 "r" (exec_addr));
38 return 0;
41 int raw_patch_instruction(unsigned int *addr, unsigned int 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);
54 if (!area) {
55 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
56 cpu);
57 return -1;
59 this_cpu_write(text_poke_area, area);
61 return 0;
64 static int text_area_cpu_down(unsigned int cpu)
66 free_vm_area(this_cpu_read(text_poke_area));
67 return 0;
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,
82 text_area_cpu_down));
84 return 0;
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)
93 unsigned long pfn;
94 int err;
96 if (is_vmalloc_addr(addr))
97 pfn = vmalloc_to_pfn(addr);
98 else
99 pfn = __pa_symbol(addr) >> PAGE_SHIFT;
101 err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT),
102 pgprot_val(PAGE_KERNEL));
104 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
105 if (err)
106 return -1;
108 return 0;
111 static inline int unmap_patch_area(unsigned long addr)
113 pte_t *ptep;
114 pmd_t *pmdp;
115 pud_t *pudp;
116 pgd_t *pgdp;
118 pgdp = pgd_offset_k(addr);
119 if (unlikely(!pgdp))
120 return -EINVAL;
122 pudp = pud_offset(pgdp, addr);
123 if (unlikely(!pudp))
124 return -EINVAL;
126 pmdp = pmd_offset(pudp, addr);
127 if (unlikely(!pmdp))
128 return -EINVAL;
130 ptep = pte_offset_kernel(pmdp, addr);
131 if (unlikely(!ptep))
132 return -EINVAL;
134 pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
137 * In hash, pte_clear flushes the tlb, in radix, we have to
139 pte_clear(&init_mm, addr, ptep);
140 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
142 return 0;
145 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
147 int err;
148 unsigned int *patch_addr = NULL;
149 unsigned long flags;
150 unsigned long text_poke_addr;
151 unsigned long kaddr = (unsigned long)addr;
154 * During early early boot patch_instruction is called
155 * when text_poke_area is not ready, but we still need
156 * to allow patching. We just do the plain old patching
158 if (!this_cpu_read(text_poke_area))
159 return raw_patch_instruction(addr, instr);
161 local_irq_save(flags);
163 text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
164 if (map_patch_area(addr, text_poke_addr)) {
165 err = -1;
166 goto out;
169 patch_addr = (unsigned int *)(text_poke_addr) +
170 ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
172 __patch_instruction(addr, instr, patch_addr);
174 err = unmap_patch_area(text_poke_addr);
175 if (err)
176 pr_warn("failed to unmap %lx\n", text_poke_addr);
178 out:
179 local_irq_restore(flags);
181 return err;
183 #else /* !CONFIG_STRICT_KERNEL_RWX */
185 static int do_patch_instruction(unsigned int *addr, unsigned int instr)
187 return raw_patch_instruction(addr, instr);
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
192 int patch_instruction(unsigned int *addr, unsigned int instr)
194 /* Make sure we aren't patching a freed init section */
195 if (init_mem_is_free && init_section_contains(addr, 4)) {
196 pr_debug("Skipping init section patching addr: 0x%px\n", addr);
197 return 0;
199 return do_patch_instruction(addr, instr);
201 NOKPROBE_SYMBOL(patch_instruction);
203 int patch_branch(unsigned int *addr, unsigned long target, int flags)
205 return patch_instruction(addr, create_branch(addr, target, flags));
208 int patch_branch_site(s32 *site, unsigned long target, int flags)
210 unsigned int *addr;
212 addr = (unsigned int *)((unsigned long)site + *site);
213 return patch_instruction(addr, create_branch(addr, target, flags));
216 int patch_instruction_site(s32 *site, unsigned int instr)
218 unsigned int *addr;
220 addr = (unsigned int *)((unsigned long)site + *site);
221 return patch_instruction(addr, instr);
224 bool is_offset_in_branch_range(long offset)
227 * Powerpc branch instruction is :
229 * 0 6 30 31
230 * +---------+----------------+---+---+
231 * | opcode | LI |AA |LK |
232 * +---------+----------------+---+---+
233 * Where AA = 0 and LK = 0
235 * LI is a signed 24 bits integer. The real branch offset is computed
236 * by: imm32 = SignExtend(LI:'0b00', 32);
238 * So the maximum forward branch should be:
239 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
240 * The maximum backward branch should be:
241 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
243 return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
247 * Helper to check if a given instruction is a conditional branch
248 * Derived from the conditional checks in analyse_instr()
250 bool is_conditional_branch(unsigned int instr)
252 unsigned int opcode = instr >> 26;
254 if (opcode == 16) /* bc, bca, bcl, bcla */
255 return true;
256 if (opcode == 19) {
257 switch ((instr >> 1) & 0x3ff) {
258 case 16: /* bclr, bclrl */
259 case 528: /* bcctr, bcctrl */
260 case 560: /* bctar, bctarl */
261 return true;
264 return false;
266 NOKPROBE_SYMBOL(is_conditional_branch);
268 unsigned int create_branch(const unsigned int *addr,
269 unsigned long target, int flags)
271 unsigned int instruction;
272 long offset;
274 offset = target;
275 if (! (flags & BRANCH_ABSOLUTE))
276 offset = offset - (unsigned long)addr;
278 /* Check we can represent the target in the instruction format */
279 if (!is_offset_in_branch_range(offset))
280 return 0;
282 /* Mask out the flags and target, so they don't step on each other. */
283 instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
285 return instruction;
288 unsigned int create_cond_branch(const unsigned int *addr,
289 unsigned long target, int flags)
291 unsigned int instruction;
292 long offset;
294 offset = target;
295 if (! (flags & BRANCH_ABSOLUTE))
296 offset = offset - (unsigned long)addr;
298 /* Check we can represent the target in the instruction format */
299 if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
300 return 0;
302 /* Mask out the flags and target, so they don't step on each other. */
303 instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
305 return instruction;
308 static unsigned int branch_opcode(unsigned int instr)
310 return (instr >> 26) & 0x3F;
313 static int instr_is_branch_iform(unsigned int instr)
315 return branch_opcode(instr) == 18;
318 static int instr_is_branch_bform(unsigned int instr)
320 return branch_opcode(instr) == 16;
323 int instr_is_relative_branch(unsigned int instr)
325 if (instr & BRANCH_ABSOLUTE)
326 return 0;
328 return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
331 int instr_is_relative_link_branch(unsigned int instr)
333 return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
336 static unsigned long branch_iform_target(const unsigned int *instr)
338 signed long imm;
340 imm = *instr & 0x3FFFFFC;
342 /* If the top bit of the immediate value is set this is negative */
343 if (imm & 0x2000000)
344 imm -= 0x4000000;
346 if ((*instr & BRANCH_ABSOLUTE) == 0)
347 imm += (unsigned long)instr;
349 return (unsigned long)imm;
352 static unsigned long branch_bform_target(const unsigned int *instr)
354 signed long imm;
356 imm = *instr & 0xFFFC;
358 /* If the top bit of the immediate value is set this is negative */
359 if (imm & 0x8000)
360 imm -= 0x10000;
362 if ((*instr & BRANCH_ABSOLUTE) == 0)
363 imm += (unsigned long)instr;
365 return (unsigned long)imm;
368 unsigned long branch_target(const unsigned int *instr)
370 if (instr_is_branch_iform(*instr))
371 return branch_iform_target(instr);
372 else if (instr_is_branch_bform(*instr))
373 return branch_bform_target(instr);
375 return 0;
378 int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
380 if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
381 return branch_target(instr) == addr;
383 return 0;
386 unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
388 unsigned long target;
390 target = branch_target(src);
392 if (instr_is_branch_iform(*src))
393 return create_branch(dest, target, *src);
394 else if (instr_is_branch_bform(*src))
395 return create_cond_branch(dest, target, *src);
397 return 0;
400 #ifdef CONFIG_PPC_BOOK3E_64
401 void __patch_exception(int exc, unsigned long addr)
403 extern unsigned int interrupt_base_book3e;
404 unsigned int *ibase = &interrupt_base_book3e;
406 /* Our exceptions vectors start with a NOP and -then- a branch
407 * to deal with single stepping from userspace which stops on
408 * the second instruction. Thus we need to patch the second
409 * instruction of the exception, not the first one
412 patch_branch(ibase + (exc / 4) + 1, addr, 0);
414 #endif
416 #ifdef CONFIG_CODE_PATCHING_SELFTEST
418 static void __init test_trampoline(void)
420 asm ("nop;\n");
423 #define check(x) \
424 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
426 static void __init test_branch_iform(void)
428 unsigned int instr;
429 unsigned long addr;
431 addr = (unsigned long)&instr;
433 /* The simplest case, branch to self, no flags */
434 check(instr_is_branch_iform(0x48000000));
435 /* All bits of target set, and flags */
436 check(instr_is_branch_iform(0x4bffffff));
437 /* High bit of opcode set, which is wrong */
438 check(!instr_is_branch_iform(0xcbffffff));
439 /* Middle bits of opcode set, which is wrong */
440 check(!instr_is_branch_iform(0x7bffffff));
442 /* Simplest case, branch to self with link */
443 check(instr_is_branch_iform(0x48000001));
444 /* All bits of targets set */
445 check(instr_is_branch_iform(0x4bfffffd));
446 /* Some bits of targets set */
447 check(instr_is_branch_iform(0x4bff00fd));
448 /* Must be a valid branch to start with */
449 check(!instr_is_branch_iform(0x7bfffffd));
451 /* Absolute branch to 0x100 */
452 instr = 0x48000103;
453 check(instr_is_branch_to_addr(&instr, 0x100));
454 /* Absolute branch to 0x420fc */
455 instr = 0x480420ff;
456 check(instr_is_branch_to_addr(&instr, 0x420fc));
457 /* Maximum positive relative branch, + 20MB - 4B */
458 instr = 0x49fffffc;
459 check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
460 /* Smallest negative relative branch, - 4B */
461 instr = 0x4bfffffc;
462 check(instr_is_branch_to_addr(&instr, addr - 4));
463 /* Largest negative relative branch, - 32 MB */
464 instr = 0x4a000000;
465 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
467 /* Branch to self, with link */
468 instr = create_branch(&instr, addr, BRANCH_SET_LINK);
469 check(instr_is_branch_to_addr(&instr, addr));
471 /* Branch to self - 0x100, with link */
472 instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
473 check(instr_is_branch_to_addr(&instr, addr - 0x100));
475 /* Branch to self + 0x100, no link */
476 instr = create_branch(&instr, addr + 0x100, 0);
477 check(instr_is_branch_to_addr(&instr, addr + 0x100));
479 /* Maximum relative negative offset, - 32 MB */
480 instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
481 check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
483 /* Out of range relative negative offset, - 32 MB + 4*/
484 instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
485 check(instr == 0);
487 /* Out of range relative positive offset, + 32 MB */
488 instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
489 check(instr == 0);
491 /* Unaligned target */
492 instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
493 check(instr == 0);
495 /* Check flags are masked correctly */
496 instr = create_branch(&instr, addr, 0xFFFFFFFC);
497 check(instr_is_branch_to_addr(&instr, addr));
498 check(instr == 0x48000000);
501 static void __init test_create_function_call(void)
503 unsigned int *iptr;
504 unsigned long dest;
506 /* Check we can create a function call */
507 iptr = (unsigned int *)ppc_function_entry(test_trampoline);
508 dest = ppc_function_entry(test_create_function_call);
509 patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
510 check(instr_is_branch_to_addr(iptr, dest));
513 static void __init test_branch_bform(void)
515 unsigned long addr;
516 unsigned int *iptr, instr, flags;
518 iptr = &instr;
519 addr = (unsigned long)iptr;
521 /* The simplest case, branch to self, no flags */
522 check(instr_is_branch_bform(0x40000000));
523 /* All bits of target set, and flags */
524 check(instr_is_branch_bform(0x43ffffff));
525 /* High bit of opcode set, which is wrong */
526 check(!instr_is_branch_bform(0xc3ffffff));
527 /* Middle bits of opcode set, which is wrong */
528 check(!instr_is_branch_bform(0x7bffffff));
530 /* Absolute conditional branch to 0x100 */
531 instr = 0x43ff0103;
532 check(instr_is_branch_to_addr(&instr, 0x100));
533 /* Absolute conditional branch to 0x20fc */
534 instr = 0x43ff20ff;
535 check(instr_is_branch_to_addr(&instr, 0x20fc));
536 /* Maximum positive relative conditional branch, + 32 KB - 4B */
537 instr = 0x43ff7ffc;
538 check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
539 /* Smallest negative relative conditional branch, - 4B */
540 instr = 0x43fffffc;
541 check(instr_is_branch_to_addr(&instr, addr - 4));
542 /* Largest negative relative conditional branch, - 32 KB */
543 instr = 0x43ff8000;
544 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
546 /* All condition code bits set & link */
547 flags = 0x3ff000 | BRANCH_SET_LINK;
549 /* Branch to self */
550 instr = create_cond_branch(iptr, addr, flags);
551 check(instr_is_branch_to_addr(&instr, addr));
553 /* Branch to self - 0x100 */
554 instr = create_cond_branch(iptr, addr - 0x100, flags);
555 check(instr_is_branch_to_addr(&instr, addr - 0x100));
557 /* Branch to self + 0x100 */
558 instr = create_cond_branch(iptr, addr + 0x100, flags);
559 check(instr_is_branch_to_addr(&instr, addr + 0x100));
561 /* Maximum relative negative offset, - 32 KB */
562 instr = create_cond_branch(iptr, addr - 0x8000, flags);
563 check(instr_is_branch_to_addr(&instr, addr - 0x8000));
565 /* Out of range relative negative offset, - 32 KB + 4*/
566 instr = create_cond_branch(iptr, addr - 0x8004, flags);
567 check(instr == 0);
569 /* Out of range relative positive offset, + 32 KB */
570 instr = create_cond_branch(iptr, addr + 0x8000, flags);
571 check(instr == 0);
573 /* Unaligned target */
574 instr = create_cond_branch(iptr, addr + 3, flags);
575 check(instr == 0);
577 /* Check flags are masked correctly */
578 instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
579 check(instr_is_branch_to_addr(&instr, addr));
580 check(instr == 0x43FF0000);
583 static void __init test_translate_branch(void)
585 unsigned long addr;
586 unsigned int *p, *q;
587 void *buf;
589 buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
590 check(buf);
591 if (!buf)
592 return;
594 /* Simple case, branch to self moved a little */
595 p = buf;
596 addr = (unsigned long)p;
597 patch_branch(p, addr, 0);
598 check(instr_is_branch_to_addr(p, addr));
599 q = p + 1;
600 patch_instruction(q, translate_branch(q, p));
601 check(instr_is_branch_to_addr(q, addr));
603 /* Maximum negative case, move b . to addr + 32 MB */
604 p = buf;
605 addr = (unsigned long)p;
606 patch_branch(p, addr, 0);
607 q = buf + 0x2000000;
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));
611 check(*q == 0x4a000000);
613 /* Maximum positive case, move x to x - 32 MB + 4 */
614 p = buf + 0x2000000;
615 addr = (unsigned long)p;
616 patch_branch(p, addr, 0);
617 q = buf + 4;
618 patch_instruction(q, translate_branch(q, p));
619 check(instr_is_branch_to_addr(p, addr));
620 check(instr_is_branch_to_addr(q, addr));
621 check(*q == 0x49fffffc);
623 /* Jump to x + 16 MB moved to x + 20 MB */
624 p = buf;
625 addr = 0x1000000 + (unsigned long)buf;
626 patch_branch(p, addr, BRANCH_SET_LINK);
627 q = buf + 0x1400000;
628 patch_instruction(q, translate_branch(q, p));
629 check(instr_is_branch_to_addr(p, addr));
630 check(instr_is_branch_to_addr(q, addr));
632 /* Jump to x + 16 MB moved to x - 16 MB + 4 */
633 p = buf + 0x1000000;
634 addr = 0x2000000 + (unsigned long)buf;
635 patch_branch(p, addr, 0);
636 q = buf + 4;
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));
642 /* Conditional branch tests */
644 /* Simple case, branch to self moved a little */
645 p = buf;
646 addr = (unsigned long)p;
647 patch_instruction(p, create_cond_branch(p, addr, 0));
648 check(instr_is_branch_to_addr(p, addr));
649 q = p + 1;
650 patch_instruction(q, translate_branch(q, p));
651 check(instr_is_branch_to_addr(q, addr));
653 /* Maximum negative case, move b . to addr + 32 KB */
654 p = buf;
655 addr = (unsigned long)p;
656 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
657 q = buf + 0x8000;
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));
661 check(*q == 0x43ff8000);
663 /* Maximum positive case, move x to x - 32 KB + 4 */
664 p = buf + 0x8000;
665 addr = (unsigned long)p;
666 patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
667 q = buf + 4;
668 patch_instruction(q, translate_branch(q, p));
669 check(instr_is_branch_to_addr(p, addr));
670 check(instr_is_branch_to_addr(q, addr));
671 check(*q == 0x43ff7ffc);
673 /* Jump to x + 12 KB moved to x + 20 KB */
674 p = buf;
675 addr = 0x3000 + (unsigned long)buf;
676 patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
677 q = buf + 0x5000;
678 patch_instruction(q, translate_branch(q, p));
679 check(instr_is_branch_to_addr(p, addr));
680 check(instr_is_branch_to_addr(q, addr));
682 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
683 p = buf + 0x2000;
684 addr = 0x4000 + (unsigned long)buf;
685 patch_instruction(p, create_cond_branch(p, addr, 0));
686 q = buf + 4;
687 patch_instruction(q, translate_branch(q, p));
688 check(instr_is_branch_to_addr(p, addr));
689 check(instr_is_branch_to_addr(q, addr));
691 /* Free the buffer we were using */
692 vfree(buf);
695 static int __init test_code_patching(void)
697 printk(KERN_DEBUG "Running code patching self-tests ...\n");
699 test_branch_iform();
700 test_branch_bform();
701 test_create_function_call();
702 test_translate_branch();
704 return 0;
706 late_initcall(test_code_patching);
708 #endif /* CONFIG_CODE_PATCHING_SELFTEST */