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/tlbflush.h>
17 #include <asm/code-patching.h>
18 #include <asm/setup.h>
21 static int __patch_instruction(struct ppc_inst
*exec_addr
, struct ppc_inst instr
,
22 struct ppc_inst
*patch_addr
)
24 if (!ppc_inst_prefixed(instr
))
25 __put_user_asm_goto(ppc_inst_val(instr
), patch_addr
, failed
, "stw");
27 __put_user_asm_goto(ppc_inst_as_u64(instr
), patch_addr
, failed
, "std");
29 asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr
),
38 int raw_patch_instruction(struct ppc_inst
*addr
, struct ppc_inst instr
)
40 return __patch_instruction(addr
, instr
, addr
);
43 #ifdef CONFIG_STRICT_KERNEL_RWX
44 static DEFINE_PER_CPU(struct vm_struct
*, text_poke_area
);
46 static int text_area_cpu_up(unsigned int cpu
)
48 struct vm_struct
*area
;
50 area
= get_vm_area(PAGE_SIZE
, VM_ALLOC
);
52 WARN_ONCE(1, "Failed to create text area for cpu %d\n",
56 this_cpu_write(text_poke_area
, area
);
61 static int text_area_cpu_down(unsigned int cpu
)
63 free_vm_area(this_cpu_read(text_poke_area
));
68 * Run as a late init call. This allows all the boot time patching to be done
69 * simply by patching the code, and then we're called here prior to
70 * mark_rodata_ro(), which happens after all init calls are run. Although
71 * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
72 * it as being preferable to a kernel that will crash later when someone tries
73 * to use patch_instruction().
75 static int __init
setup_text_poke_area(void)
77 BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
,
78 "powerpc/text_poke:online", text_area_cpu_up
,
83 late_initcall(setup_text_poke_area
);
86 * This can be called for kernel text or a module.
88 static int map_patch_area(void *addr
, unsigned long text_poke_addr
)
93 if (is_vmalloc_or_module_addr(addr
))
94 pfn
= vmalloc_to_pfn(addr
);
96 pfn
= __pa_symbol(addr
) >> PAGE_SHIFT
;
98 err
= map_kernel_page(text_poke_addr
, (pfn
<< PAGE_SHIFT
), PAGE_KERNEL
);
100 pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr
, pfn
, err
);
107 static inline int unmap_patch_area(unsigned long addr
)
115 pgdp
= pgd_offset_k(addr
);
119 p4dp
= p4d_offset(pgdp
, addr
);
123 pudp
= pud_offset(p4dp
, 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(struct ppc_inst
*addr
, struct ppc_inst instr
)
149 struct ppc_inst
*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
= (struct ppc_inst
*)(text_poke_addr
+ (kaddr
& ~PAGE_MASK
));
172 __patch_instruction(addr
, instr
, patch_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 static int do_patch_instruction(struct ppc_inst
*addr
, struct ppc_inst instr
)
187 return raw_patch_instruction(addr
, instr
);
190 #endif /* CONFIG_STRICT_KERNEL_RWX */
192 int patch_instruction(struct ppc_inst
*addr
, struct ppc_inst 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
);
199 return do_patch_instruction(addr
, instr
);
201 NOKPROBE_SYMBOL(patch_instruction
);
203 int patch_branch(struct ppc_inst
*addr
, unsigned long target
, int flags
)
205 struct ppc_inst instr
;
207 create_branch(&instr
, addr
, target
, flags
);
208 return patch_instruction(addr
, instr
);
211 bool is_offset_in_branch_range(long offset
)
214 * Powerpc branch instruction is :
217 * +---------+----------------+---+---+
218 * | opcode | LI |AA |LK |
219 * +---------+----------------+---+---+
220 * Where AA = 0 and LK = 0
222 * LI is a signed 24 bits integer. The real branch offset is computed
223 * by: imm32 = SignExtend(LI:'0b00', 32);
225 * So the maximum forward branch should be:
226 * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
227 * The maximum backward branch should be:
228 * (0xff800000 << 2) = 0xfe000000 = -0x2000000
230 return (offset
>= -0x2000000 && offset
<= 0x1fffffc && !(offset
& 0x3));
234 * Helper to check if a given instruction is a conditional branch
235 * Derived from the conditional checks in analyse_instr()
237 bool is_conditional_branch(struct ppc_inst instr
)
239 unsigned int opcode
= ppc_inst_primary_opcode(instr
);
241 if (opcode
== 16) /* bc, bca, bcl, bcla */
244 switch ((ppc_inst_val(instr
) >> 1) & 0x3ff) {
245 case 16: /* bclr, bclrl */
246 case 528: /* bcctr, bcctrl */
247 case 560: /* bctar, bctarl */
253 NOKPROBE_SYMBOL(is_conditional_branch
);
255 int create_branch(struct ppc_inst
*instr
,
256 const struct ppc_inst
*addr
,
257 unsigned long target
, int flags
)
261 *instr
= ppc_inst(0);
263 if (! (flags
& BRANCH_ABSOLUTE
))
264 offset
= offset
- (unsigned long)addr
;
266 /* Check we can represent the target in the instruction format */
267 if (!is_offset_in_branch_range(offset
))
270 /* Mask out the flags and target, so they don't step on each other. */
271 *instr
= ppc_inst(0x48000000 | (flags
& 0x3) | (offset
& 0x03FFFFFC));
276 int create_cond_branch(struct ppc_inst
*instr
, const struct ppc_inst
*addr
,
277 unsigned long target
, int flags
)
282 if (! (flags
& BRANCH_ABSOLUTE
))
283 offset
= offset
- (unsigned long)addr
;
285 /* Check we can represent the target in the instruction format */
286 if (offset
< -0x8000 || offset
> 0x7FFF || offset
& 0x3)
289 /* Mask out the flags and target, so they don't step on each other. */
290 *instr
= ppc_inst(0x40000000 | (flags
& 0x3FF0003) | (offset
& 0xFFFC));
295 static unsigned int branch_opcode(struct ppc_inst instr
)
297 return ppc_inst_primary_opcode(instr
) & 0x3F;
300 static int instr_is_branch_iform(struct ppc_inst instr
)
302 return branch_opcode(instr
) == 18;
305 static int instr_is_branch_bform(struct ppc_inst instr
)
307 return branch_opcode(instr
) == 16;
310 int instr_is_relative_branch(struct ppc_inst instr
)
312 if (ppc_inst_val(instr
) & BRANCH_ABSOLUTE
)
315 return instr_is_branch_iform(instr
) || instr_is_branch_bform(instr
);
318 int instr_is_relative_link_branch(struct ppc_inst instr
)
320 return instr_is_relative_branch(instr
) && (ppc_inst_val(instr
) & BRANCH_SET_LINK
);
323 static unsigned long branch_iform_target(const struct ppc_inst
*instr
)
327 imm
= ppc_inst_val(*instr
) & 0x3FFFFFC;
329 /* If the top bit of the immediate value is set this is negative */
333 if ((ppc_inst_val(*instr
) & BRANCH_ABSOLUTE
) == 0)
334 imm
+= (unsigned long)instr
;
336 return (unsigned long)imm
;
339 static unsigned long branch_bform_target(const struct ppc_inst
*instr
)
343 imm
= ppc_inst_val(*instr
) & 0xFFFC;
345 /* If the top bit of the immediate value is set this is negative */
349 if ((ppc_inst_val(*instr
) & BRANCH_ABSOLUTE
) == 0)
350 imm
+= (unsigned long)instr
;
352 return (unsigned long)imm
;
355 unsigned long branch_target(const struct ppc_inst
*instr
)
357 if (instr_is_branch_iform(ppc_inst_read(instr
)))
358 return branch_iform_target(instr
);
359 else if (instr_is_branch_bform(ppc_inst_read(instr
)))
360 return branch_bform_target(instr
);
365 int instr_is_branch_to_addr(const struct ppc_inst
*instr
, unsigned long addr
)
367 if (instr_is_branch_iform(ppc_inst_read(instr
)) ||
368 instr_is_branch_bform(ppc_inst_read(instr
)))
369 return branch_target(instr
) == addr
;
374 int translate_branch(struct ppc_inst
*instr
, const struct ppc_inst
*dest
,
375 const struct ppc_inst
*src
)
377 unsigned long target
;
378 target
= branch_target(src
);
380 if (instr_is_branch_iform(ppc_inst_read(src
)))
381 return create_branch(instr
, dest
, target
,
382 ppc_inst_val(ppc_inst_read(src
)));
383 else if (instr_is_branch_bform(ppc_inst_read(src
)))
384 return create_cond_branch(instr
, dest
, target
,
385 ppc_inst_val(ppc_inst_read(src
)));
390 #ifdef CONFIG_PPC_BOOK3E_64
391 void __patch_exception(int exc
, unsigned long addr
)
393 extern unsigned int interrupt_base_book3e
;
394 unsigned int *ibase
= &interrupt_base_book3e
;
396 /* Our exceptions vectors start with a NOP and -then- a branch
397 * to deal with single stepping from userspace which stops on
398 * the second instruction. Thus we need to patch the second
399 * instruction of the exception, not the first one
402 patch_branch((struct ppc_inst
*)(ibase
+ (exc
/ 4) + 1), addr
, 0);
406 #ifdef CONFIG_CODE_PATCHING_SELFTEST
408 static void __init
test_trampoline(void)
414 if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
416 static void __init
test_branch_iform(void)
419 struct ppc_inst instr
;
422 addr
= (unsigned long)&instr
;
424 /* The simplest case, branch to self, no flags */
425 check(instr_is_branch_iform(ppc_inst(0x48000000)));
426 /* All bits of target set, and flags */
427 check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
428 /* High bit of opcode set, which is wrong */
429 check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
430 /* Middle bits of opcode set, which is wrong */
431 check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
433 /* Simplest case, branch to self with link */
434 check(instr_is_branch_iform(ppc_inst(0x48000001)));
435 /* All bits of targets set */
436 check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
437 /* Some bits of targets set */
438 check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
439 /* Must be a valid branch to start with */
440 check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
442 /* Absolute branch to 0x100 */
443 instr
= ppc_inst(0x48000103);
444 check(instr_is_branch_to_addr(&instr
, 0x100));
445 /* Absolute branch to 0x420fc */
446 instr
= ppc_inst(0x480420ff);
447 check(instr_is_branch_to_addr(&instr
, 0x420fc));
448 /* Maximum positive relative branch, + 20MB - 4B */
449 instr
= ppc_inst(0x49fffffc);
450 check(instr_is_branch_to_addr(&instr
, addr
+ 0x1FFFFFC));
451 /* Smallest negative relative branch, - 4B */
452 instr
= ppc_inst(0x4bfffffc);
453 check(instr_is_branch_to_addr(&instr
, addr
- 4));
454 /* Largest negative relative branch, - 32 MB */
455 instr
= ppc_inst(0x4a000000);
456 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
458 /* Branch to self, with link */
459 err
= create_branch(&instr
, &instr
, addr
, BRANCH_SET_LINK
);
460 check(instr_is_branch_to_addr(&instr
, addr
));
462 /* Branch to self - 0x100, with link */
463 err
= create_branch(&instr
, &instr
, addr
- 0x100, BRANCH_SET_LINK
);
464 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
466 /* Branch to self + 0x100, no link */
467 err
= create_branch(&instr
, &instr
, addr
+ 0x100, 0);
468 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
470 /* Maximum relative negative offset, - 32 MB */
471 err
= create_branch(&instr
, &instr
, addr
- 0x2000000, BRANCH_SET_LINK
);
472 check(instr_is_branch_to_addr(&instr
, addr
- 0x2000000));
474 /* Out of range relative negative offset, - 32 MB + 4*/
475 err
= create_branch(&instr
, &instr
, addr
- 0x2000004, BRANCH_SET_LINK
);
478 /* Out of range relative positive offset, + 32 MB */
479 err
= create_branch(&instr
, &instr
, addr
+ 0x2000000, BRANCH_SET_LINK
);
482 /* Unaligned target */
483 err
= create_branch(&instr
, &instr
, addr
+ 3, BRANCH_SET_LINK
);
486 /* Check flags are masked correctly */
487 err
= create_branch(&instr
, &instr
, addr
, 0xFFFFFFFC);
488 check(instr_is_branch_to_addr(&instr
, addr
));
489 check(ppc_inst_equal(instr
, ppc_inst(0x48000000)));
492 static void __init
test_create_function_call(void)
494 struct ppc_inst
*iptr
;
496 struct ppc_inst instr
;
498 /* Check we can create a function call */
499 iptr
= (struct ppc_inst
*)ppc_function_entry(test_trampoline
);
500 dest
= ppc_function_entry(test_create_function_call
);
501 create_branch(&instr
, iptr
, dest
, BRANCH_SET_LINK
);
502 patch_instruction(iptr
, instr
);
503 check(instr_is_branch_to_addr(iptr
, dest
));
506 static void __init
test_branch_bform(void)
510 struct ppc_inst
*iptr
, instr
;
514 addr
= (unsigned long)iptr
;
516 /* The simplest case, branch to self, no flags */
517 check(instr_is_branch_bform(ppc_inst(0x40000000)));
518 /* All bits of target set, and flags */
519 check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
520 /* High bit of opcode set, which is wrong */
521 check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
522 /* Middle bits of opcode set, which is wrong */
523 check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
525 /* Absolute conditional branch to 0x100 */
526 instr
= ppc_inst(0x43ff0103);
527 check(instr_is_branch_to_addr(&instr
, 0x100));
528 /* Absolute conditional branch to 0x20fc */
529 instr
= ppc_inst(0x43ff20ff);
530 check(instr_is_branch_to_addr(&instr
, 0x20fc));
531 /* Maximum positive relative conditional branch, + 32 KB - 4B */
532 instr
= ppc_inst(0x43ff7ffc);
533 check(instr_is_branch_to_addr(&instr
, addr
+ 0x7FFC));
534 /* Smallest negative relative conditional branch, - 4B */
535 instr
= ppc_inst(0x43fffffc);
536 check(instr_is_branch_to_addr(&instr
, addr
- 4));
537 /* Largest negative relative conditional branch, - 32 KB */
538 instr
= ppc_inst(0x43ff8000);
539 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
541 /* All condition code bits set & link */
542 flags
= 0x3ff000 | BRANCH_SET_LINK
;
545 err
= create_cond_branch(&instr
, iptr
, addr
, flags
);
546 check(instr_is_branch_to_addr(&instr
, addr
));
548 /* Branch to self - 0x100 */
549 err
= create_cond_branch(&instr
, iptr
, addr
- 0x100, flags
);
550 check(instr_is_branch_to_addr(&instr
, addr
- 0x100));
552 /* Branch to self + 0x100 */
553 err
= create_cond_branch(&instr
, iptr
, addr
+ 0x100, flags
);
554 check(instr_is_branch_to_addr(&instr
, addr
+ 0x100));
556 /* Maximum relative negative offset, - 32 KB */
557 err
= create_cond_branch(&instr
, iptr
, addr
- 0x8000, flags
);
558 check(instr_is_branch_to_addr(&instr
, addr
- 0x8000));
560 /* Out of range relative negative offset, - 32 KB + 4*/
561 err
= create_cond_branch(&instr
, iptr
, addr
- 0x8004, flags
);
564 /* Out of range relative positive offset, + 32 KB */
565 err
= create_cond_branch(&instr
, iptr
, addr
+ 0x8000, flags
);
568 /* Unaligned target */
569 err
= create_cond_branch(&instr
, iptr
, addr
+ 3, flags
);
572 /* Check flags are masked correctly */
573 err
= create_cond_branch(&instr
, iptr
, addr
, 0xFFFFFFFC);
574 check(instr_is_branch_to_addr(&instr
, addr
));
575 check(ppc_inst_equal(instr
, ppc_inst(0x43FF0000)));
578 static void __init
test_translate_branch(void)
582 struct ppc_inst instr
;
585 buf
= vmalloc(PAGE_ALIGN(0x2000000 + 1));
590 /* Simple case, branch to self moved a little */
592 addr
= (unsigned long)p
;
593 patch_branch(p
, addr
, 0);
594 check(instr_is_branch_to_addr(p
, addr
));
596 translate_branch(&instr
, q
, p
);
597 patch_instruction(q
, instr
);
598 check(instr_is_branch_to_addr(q
, addr
));
600 /* Maximum negative case, move b . to addr + 32 MB */
602 addr
= (unsigned long)p
;
603 patch_branch(p
, addr
, 0);
605 translate_branch(&instr
, q
, p
);
606 patch_instruction(q
, instr
);
607 check(instr_is_branch_to_addr(p
, addr
));
608 check(instr_is_branch_to_addr(q
, addr
));
609 check(ppc_inst_equal(ppc_inst_read(q
), ppc_inst(0x4a000000)));
611 /* Maximum positive case, move x to x - 32 MB + 4 */
613 addr
= (unsigned long)p
;
614 patch_branch(p
, addr
, 0);
616 translate_branch(&instr
, q
, p
);
617 patch_instruction(q
, instr
);
618 check(instr_is_branch_to_addr(p
, addr
));
619 check(instr_is_branch_to_addr(q
, addr
));
620 check(ppc_inst_equal(ppc_inst_read(q
), ppc_inst(0x49fffffc)));
622 /* Jump to x + 16 MB moved to x + 20 MB */
624 addr
= 0x1000000 + (unsigned long)buf
;
625 patch_branch(p
, addr
, BRANCH_SET_LINK
);
627 translate_branch(&instr
, q
, p
);
628 patch_instruction(q
, instr
);
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 */
634 addr
= 0x2000000 + (unsigned long)buf
;
635 patch_branch(p
, addr
, 0);
637 translate_branch(&instr
, q
, p
);
638 patch_instruction(q
, instr
);
639 check(instr_is_branch_to_addr(p
, addr
));
640 check(instr_is_branch_to_addr(q
, addr
));
643 /* Conditional branch tests */
645 /* Simple case, branch to self moved a little */
647 addr
= (unsigned long)p
;
648 create_cond_branch(&instr
, p
, addr
, 0);
649 patch_instruction(p
, instr
);
650 check(instr_is_branch_to_addr(p
, addr
));
652 translate_branch(&instr
, q
, p
);
653 patch_instruction(q
, instr
);
654 check(instr_is_branch_to_addr(q
, addr
));
656 /* Maximum negative case, move b . to addr + 32 KB */
658 addr
= (unsigned long)p
;
659 create_cond_branch(&instr
, p
, addr
, 0xFFFFFFFC);
660 patch_instruction(p
, instr
);
662 translate_branch(&instr
, q
, p
);
663 patch_instruction(q
, instr
);
664 check(instr_is_branch_to_addr(p
, addr
));
665 check(instr_is_branch_to_addr(q
, addr
));
666 check(ppc_inst_equal(ppc_inst_read(q
), ppc_inst(0x43ff8000)));
668 /* Maximum positive case, move x to x - 32 KB + 4 */
670 addr
= (unsigned long)p
;
671 create_cond_branch(&instr
, p
, addr
, 0xFFFFFFFC);
672 patch_instruction(p
, instr
);
674 translate_branch(&instr
, q
, p
);
675 patch_instruction(q
, instr
);
676 check(instr_is_branch_to_addr(p
, addr
));
677 check(instr_is_branch_to_addr(q
, addr
));
678 check(ppc_inst_equal(ppc_inst_read(q
), ppc_inst(0x43ff7ffc)));
680 /* Jump to x + 12 KB moved to x + 20 KB */
682 addr
= 0x3000 + (unsigned long)buf
;
683 create_cond_branch(&instr
, p
, addr
, BRANCH_SET_LINK
);
684 patch_instruction(p
, instr
);
686 translate_branch(&instr
, q
, p
);
687 patch_instruction(q
, instr
);
688 check(instr_is_branch_to_addr(p
, addr
));
689 check(instr_is_branch_to_addr(q
, addr
));
691 /* Jump to x + 8 KB moved to x - 8 KB + 4 */
693 addr
= 0x4000 + (unsigned long)buf
;
694 create_cond_branch(&instr
, p
, addr
, 0);
695 patch_instruction(p
, instr
);
697 translate_branch(&instr
, q
, p
);
698 patch_instruction(q
, instr
);
699 check(instr_is_branch_to_addr(p
, addr
));
700 check(instr_is_branch_to_addr(q
, addr
));
702 /* Free the buffer we were using */
707 static void __init
test_prefixed_patching(void)
709 extern unsigned int code_patching_test1
[];
710 extern unsigned int code_patching_test1_expected
[];
711 extern unsigned int end_code_patching_test1
[];
713 __patch_instruction((struct ppc_inst
*)code_patching_test1
,
714 ppc_inst_prefix(OP_PREFIX
<< 26, 0x00000000),
715 (struct ppc_inst
*)code_patching_test1
);
717 check(!memcmp(code_patching_test1
,
718 code_patching_test1_expected
,
719 sizeof(unsigned int) *
720 (end_code_patching_test1
- code_patching_test1
)));
723 static inline void test_prefixed_patching(void) {}
726 static int __init
test_code_patching(void)
728 printk(KERN_DEBUG
"Running code patching self-tests ...\n");
732 test_create_function_call();
733 test_translate_branch();
734 test_prefixed_patching();
738 late_initcall(test_code_patching
);
740 #endif /* CONFIG_CODE_PATCHING_SELFTEST */