1 // SPDX-License-Identifier: GPL-2.0
3 * Code for replacing ftrace calls with jumps.
7 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
9 * Added function graph tracer code, taken from x86 that was written
10 * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
14 #define pr_fmt(fmt) "ftrace-powerpc: " fmt
16 #include <linux/spinlock.h>
17 #include <linux/hardirq.h>
18 #include <linux/uaccess.h>
19 #include <linux/module.h>
20 #include <linux/ftrace.h>
21 #include <linux/percpu.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
25 #include <asm/cacheflush.h>
26 #include <asm/text-patching.h>
27 #include <asm/ftrace.h>
28 #include <asm/syscall.h>
32 * We generally only have a single long_branch tramp and at most 2 or 3 plt
33 * tramps generated. But, we don't use the plt tramps currently. We also allot
34 * 2 tramps after .text and .init.text. So, we only end up with around 3 usable
35 * tramps in total. Set aside 8 just to be sure.
37 #define NUM_FTRACE_TRAMPS 8
38 static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS];
40 unsigned long ftrace_call_adjust(unsigned long addr)
46 ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
50 addr = ppc_function_entry((void *)addr);
52 /* if (link) set op to 'bl' else 'b' */
53 create_branch(&op, (u32 *)ip, addr, link ? BRANCH_SET_LINK : 0);
59 ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
65 * We are paranoid about modifying text, as if a bug was to happen, it
66 * could cause us to read or write to someplace that could cause harm.
67 * Carefully read and modify the code with probe_kernel_*(), and make
68 * sure what we read is what we expected it to be before modifying it.
71 /* read the text we want to modify */
72 if (copy_inst_from_kernel_nofault(&replaced, (void *)ip))
75 /* Make sure it is what we expect it to be */
76 if (!ppc_inst_equal(replaced, old)) {
77 pr_err("%p: replaced (%08lx) != old (%08lx)", (void *)ip,
78 ppc_inst_as_ulong(replaced), ppc_inst_as_ulong(old));
82 /* replace the text with the new text */
83 return patch_instruction((u32 *)ip, new);
87 * Helper functions that are the same for both PPC64 and PPC32.
89 static int test_24bit_addr(unsigned long ip, unsigned long addr)
91 addr = ppc_function_entry((void *)addr);
93 return is_offset_in_branch_range(addr - ip);
96 static int is_bl_op(ppc_inst_t op)
98 return (ppc_inst_val(op) & ~PPC_LI_MASK) == PPC_RAW_BL(0);
101 static int is_b_op(ppc_inst_t op)
103 return (ppc_inst_val(op) & ~PPC_LI_MASK) == PPC_RAW_BRANCH(0);
106 static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
110 offset = PPC_LI(ppc_inst_val(op));
112 if (offset & 0x02000000)
113 offset |= 0xfe000000;
115 return ip + (long)offset;
118 #ifdef CONFIG_MODULES
119 static struct module *ftrace_lookup_module(struct dyn_ftrace *rec)
124 mod = __module_text_address(rec->ip);
128 pr_err("No module loaded at addr=%lx\n", rec->ip);
134 __ftrace_make_nop(struct module *mod,
135 struct dyn_ftrace *rec, unsigned long addr)
137 unsigned long entry, ptr, tramp;
138 unsigned long ip = rec->ip;
142 mod = ftrace_lookup_module(rec);
147 /* read where this goes */
148 if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
149 pr_err("Fetching opcode failed.\n");
153 /* Make sure that this is still a 24bit jump */
155 pr_err("Not expected bl: opcode is %08lx\n", ppc_inst_as_ulong(op));
159 /* lets find where the pointer goes */
160 tramp = find_bl_target(ip, op);
162 pr_devel("ip:%lx jumps to %lx", ip, tramp);
164 if (module_trampoline_target(mod, tramp, &ptr)) {
165 pr_err("Failed to get trampoline target\n");
169 pr_devel("trampoline target %lx", ptr);
171 entry = ppc_global_function_entry((void *)addr);
172 /* This should match what was called */
174 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
178 if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
179 if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) {
180 pr_err("Fetching instruction at %lx failed.\n", ip - 4);
184 /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
185 if (!ppc_inst_equal(op, ppc_inst(PPC_RAW_MFLR(_R0))) &&
186 !ppc_inst_equal(op, ppc_inst(PPC_INST_STD_LR))) {
187 pr_err("Unexpected instruction %08lx around bl _mcount\n",
188 ppc_inst_as_ulong(op));
191 } else if (IS_ENABLED(CONFIG_PPC64)) {
193 * Check what is in the next instruction. We can see ld r2,40(r1), but
194 * on first pass after boot we will see mflr r0.
196 if (copy_inst_from_kernel_nofault(&op, (void *)(ip + 4))) {
197 pr_err("Fetching op failed.\n");
201 if (!ppc_inst_equal(op, ppc_inst(PPC_INST_LD_TOC))) {
202 pr_err("Expected %08lx found %08lx\n", PPC_INST_LD_TOC,
203 ppc_inst_as_ulong(op));
209 * When using -mprofile-kernel or PPC32 there is no load to jump over.
211 * Otherwise our original call site looks like:
216 * Milton Miller pointed out that we can not simply nop the branch.
217 * If a task was preempted when calling a trace function, the nops
218 * will remove the way to restore the TOC in r2 and the r2 TOC will
221 * Use a b +8 to jump over the load.
223 if (IS_ENABLED(CONFIG_MPROFILE_KERNEL) || IS_ENABLED(CONFIG_PPC32))
224 pop = ppc_inst(PPC_RAW_NOP());
226 pop = ppc_inst(PPC_RAW_BRANCH(8)); /* b +8 */
228 if (patch_instruction((u32 *)ip, pop)) {
229 pr_err("Patching NOP failed.\n");
236 static int __ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
240 #endif /* CONFIG_MODULES */
242 static unsigned long find_ftrace_tramp(unsigned long ip)
247 * We have the compiler generated long_branch tramps at the end
248 * and we prefer those
250 for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--)
251 if (!ftrace_tramps[i])
253 else if (is_offset_in_branch_range(ftrace_tramps[i] - ip))
254 return ftrace_tramps[i];
259 static int add_ftrace_tramp(unsigned long tramp)
263 for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
264 if (!ftrace_tramps[i]) {
265 ftrace_tramps[i] = tramp;
273 * If this is a compiler generated long_branch trampoline (essentially, a
274 * trampoline that has a branch to _mcount()), we re-write the branch to
275 * instead go to ftrace_[regs_]caller() and note down the location of this
278 static int setup_mcount_compiler_tramp(unsigned long tramp)
284 /* Is this a known long jump tramp? */
285 for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
286 if (ftrace_tramps[i] == tramp)
289 /* New trampoline -- read where this goes */
290 if (copy_inst_from_kernel_nofault(&op, (void *)tramp)) {
291 pr_debug("Fetching opcode failed.\n");
295 /* Is this a 24 bit branch? */
297 pr_debug("Trampoline is not a long branch tramp.\n");
301 /* lets find where the pointer goes */
302 ptr = find_bl_target(tramp, op);
304 if (ptr != ppc_global_function_entry((void *)_mcount)) {
305 pr_debug("Trampoline target %p is not _mcount\n", (void *)ptr);
309 /* Let's re-write the tramp to go to ftrace_[regs_]caller */
310 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
311 ptr = ppc_global_function_entry((void *)ftrace_regs_caller);
313 ptr = ppc_global_function_entry((void *)ftrace_caller);
315 if (patch_branch((u32 *)tramp, ptr, 0)) {
316 pr_debug("REL24 out of range!\n");
320 if (add_ftrace_tramp(tramp)) {
321 pr_debug("No tramp locations left\n");
328 static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr)
330 unsigned long tramp, ip = rec->ip;
333 /* Read where this goes */
334 if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
335 pr_err("Fetching opcode failed.\n");
339 /* Make sure that this is still a 24bit jump */
341 pr_err("Not expected bl: opcode is %08lx\n", ppc_inst_as_ulong(op));
345 /* Let's find where the pointer goes */
346 tramp = find_bl_target(ip, op);
348 pr_devel("ip:%lx jumps to %lx", ip, tramp);
350 if (setup_mcount_compiler_tramp(tramp)) {
351 /* Are other trampolines reachable? */
352 if (!find_ftrace_tramp(ip)) {
353 pr_err("No ftrace trampolines reachable from %ps\n",
359 if (patch_instruction((u32 *)ip, ppc_inst(PPC_RAW_NOP()))) {
360 pr_err("Patching NOP failed.\n");
367 int ftrace_make_nop(struct module *mod,
368 struct dyn_ftrace *rec, unsigned long addr)
370 unsigned long ip = rec->ip;
374 * If the calling address is more that 24 bits away,
375 * then we had to use a trampoline to make the call.
376 * Otherwise just update the call site.
378 if (test_24bit_addr(ip, addr)) {
380 old = ftrace_call_replace(ip, addr, 1);
381 new = ppc_inst(PPC_RAW_NOP());
382 return ftrace_modify_code(ip, old, new);
383 } else if (core_kernel_text(ip)) {
384 return __ftrace_make_nop_kernel(rec, addr);
385 } else if (!IS_ENABLED(CONFIG_MODULES)) {
389 return __ftrace_make_nop(mod, rec, addr);
392 #ifdef CONFIG_MODULES
394 * Examine the existing instructions for __ftrace_make_call.
395 * They should effectively be a NOP, and follow formal constraints,
396 * depending on the ABI. Return false if they don't.
398 static bool expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
400 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
401 return ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()));
403 return ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) &&
404 ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC));
408 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
411 void *ip = (void *)rec->ip;
412 unsigned long entry, ptr, tramp;
413 struct module *mod = ftrace_lookup_module(rec);
418 /* read where this goes */
419 if (copy_inst_from_kernel_nofault(op, ip))
422 if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) &&
423 copy_inst_from_kernel_nofault(op + 1, ip + 4))
426 if (!expected_nop_sequence(ip, op[0], op[1])) {
427 pr_err("Unexpected call sequence at %p: %08lx %08lx\n", ip,
428 ppc_inst_as_ulong(op[0]), ppc_inst_as_ulong(op[1]));
432 /* If we never set up ftrace trampoline(s), then bail */
433 if (!mod->arch.tramp ||
434 (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !mod->arch.tramp_regs)) {
435 pr_err("No ftrace trampoline\n");
439 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && rec->flags & FTRACE_FL_REGS)
440 tramp = mod->arch.tramp_regs;
442 tramp = mod->arch.tramp;
444 if (module_trampoline_target(mod, tramp, &ptr)) {
445 pr_err("Failed to get trampoline target\n");
449 pr_devel("trampoline target %lx", ptr);
451 entry = ppc_global_function_entry((void *)addr);
452 /* This should match what was called */
454 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
458 if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
459 pr_err("REL24 out of range!\n");
466 static int __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
470 #endif /* CONFIG_MODULES */
472 static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
475 void *ip = (void *)rec->ip;
476 unsigned long tramp, entry, ptr;
478 /* Make sure we're being asked to patch branch to a known ftrace addr */
479 entry = ppc_global_function_entry((void *)ftrace_caller);
480 ptr = ppc_global_function_entry((void *)addr);
482 if (ptr != entry && IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
483 entry = ppc_global_function_entry((void *)ftrace_regs_caller);
486 pr_err("Unknown ftrace addr to patch: %ps\n", (void *)ptr);
490 /* Make sure we have a nop */
491 if (copy_inst_from_kernel_nofault(&op, ip)) {
492 pr_err("Unable to read ftrace location %p\n", ip);
496 if (!ppc_inst_equal(op, ppc_inst(PPC_RAW_NOP()))) {
497 pr_err("Unexpected call sequence at %p: %08lx\n",
498 ip, ppc_inst_as_ulong(op));
502 tramp = find_ftrace_tramp((unsigned long)ip);
504 pr_err("No ftrace trampolines reachable from %ps\n", ip);
508 if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
509 pr_err("Error patching branch to ftrace tramp!\n");
516 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
518 unsigned long ip = rec->ip;
522 * If the calling address is more that 24 bits away,
523 * then we had to use a trampoline to make the call.
524 * Otherwise just update the call site.
526 if (test_24bit_addr(ip, addr)) {
528 old = ppc_inst(PPC_RAW_NOP());
529 new = ftrace_call_replace(ip, addr, 1);
530 return ftrace_modify_code(ip, old, new);
531 } else if (core_kernel_text(ip)) {
532 return __ftrace_make_call_kernel(rec, addr);
533 } else if (!IS_ENABLED(CONFIG_MODULES)) {
534 /* We should not get here without modules */
538 return __ftrace_make_call(rec, addr);
541 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
542 #ifdef CONFIG_MODULES
544 __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
548 unsigned long ip = rec->ip;
549 unsigned long entry, ptr, tramp;
550 struct module *mod = ftrace_lookup_module(rec);
555 /* If we never set up ftrace trampolines, then bail */
556 if (!mod->arch.tramp || !mod->arch.tramp_regs) {
557 pr_err("No ftrace trampoline\n");
561 /* read where this goes */
562 if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
563 pr_err("Fetching opcode failed.\n");
567 /* Make sure that this is still a 24bit jump */
569 pr_err("Not expected bl: opcode is %08lx\n", ppc_inst_as_ulong(op));
573 /* lets find where the pointer goes */
574 tramp = find_bl_target(ip, op);
575 entry = ppc_global_function_entry((void *)old_addr);
577 pr_devel("ip:%lx jumps to %lx", ip, tramp);
579 if (tramp != entry) {
580 /* old_addr is not within range, so we must have used a trampoline */
581 if (module_trampoline_target(mod, tramp, &ptr)) {
582 pr_err("Failed to get trampoline target\n");
586 pr_devel("trampoline target %lx", ptr);
588 /* This should match what was called */
590 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
595 /* The new target may be within range */
596 if (test_24bit_addr(ip, addr)) {
598 if (patch_branch((u32 *)ip, addr, BRANCH_SET_LINK)) {
599 pr_err("REL24 out of range!\n");
606 if (rec->flags & FTRACE_FL_REGS)
607 tramp = mod->arch.tramp_regs;
609 tramp = mod->arch.tramp;
611 if (module_trampoline_target(mod, tramp, &ptr)) {
612 pr_err("Failed to get trampoline target\n");
616 pr_devel("trampoline target %lx", ptr);
618 entry = ppc_global_function_entry((void *)addr);
619 /* This should match what was called */
621 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
625 if (patch_branch((u32 *)ip, tramp, BRANCH_SET_LINK)) {
626 pr_err("REL24 out of range!\n");
633 static int __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsigned long addr)
639 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
642 unsigned long ip = rec->ip;
646 * If the calling address is more that 24 bits away,
647 * then we had to use a trampoline to make the call.
648 * Otherwise just update the call site.
650 if (test_24bit_addr(ip, addr) && test_24bit_addr(ip, old_addr)) {
652 old = ftrace_call_replace(ip, old_addr, 1);
653 new = ftrace_call_replace(ip, addr, 1);
654 return ftrace_modify_code(ip, old, new);
655 } else if (core_kernel_text(ip)) {
657 * We always patch out of range locations to go to the regs
658 * variant, so there is nothing to do here
661 } else if (!IS_ENABLED(CONFIG_MODULES)) {
662 /* We should not get here without modules */
666 return __ftrace_modify_call(rec, old_addr, addr);
670 int ftrace_update_ftrace_func(ftrace_func_t func)
672 unsigned long ip = (unsigned long)(&ftrace_call);
676 old = ppc_inst_read((u32 *)&ftrace_call);
677 new = ftrace_call_replace(ip, (unsigned long)func, 1);
678 ret = ftrace_modify_code(ip, old, new);
680 /* Also update the regs callback function */
681 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !ret) {
682 ip = (unsigned long)(&ftrace_regs_call);
683 old = ppc_inst_read((u32 *)&ftrace_regs_call);
684 new = ftrace_call_replace(ip, (unsigned long)func, 1);
685 ret = ftrace_modify_code(ip, old, new);
692 * Use the default ftrace_modify_all_code, but without
695 void arch_ftrace_update_code(int command)
697 ftrace_modify_all_code(command);
701 #define PACATOC offsetof(struct paca_struct, kernel_toc)
703 extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];
705 void ftrace_free_init_tramp(void)
709 for (i = 0; i < NUM_FTRACE_TRAMPS && ftrace_tramps[i]; i++)
710 if (ftrace_tramps[i] == (unsigned long)ftrace_tramp_init) {
711 ftrace_tramps[i] = 0;
716 int __init ftrace_dyn_arch_init(void)
719 unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
721 PPC_RAW_LD(_R12, _R13, PACATOC),
722 PPC_RAW_ADDIS(_R12, _R12, 0),
723 PPC_RAW_ADDI(_R12, _R12, 0),
730 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
731 addr = ppc_global_function_entry((void *)ftrace_regs_caller);
733 addr = ppc_global_function_entry((void *)ftrace_caller);
735 reladdr = addr - kernel_toc_addr();
737 if (reladdr >= SZ_2G || reladdr < -(long)SZ_2G) {
738 pr_err("Address of %ps out of range of kernel_toc.\n",
743 for (i = 0; i < 2; i++) {
744 memcpy(tramp[i], stub_insns, sizeof(stub_insns));
745 tramp[i][1] |= PPC_HA(reladdr);
746 tramp[i][2] |= PPC_LO(reladdr);
747 add_ftrace_tramp((unsigned long)tramp[i]);
754 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
756 extern void ftrace_graph_call(void);
757 extern void ftrace_graph_stub(void);
759 static int ftrace_modify_ftrace_graph_caller(bool enable)
761 unsigned long ip = (unsigned long)(&ftrace_graph_call);
762 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
763 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
766 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_ARGS))
769 old = ftrace_call_replace(ip, enable ? stub : addr, 0);
770 new = ftrace_call_replace(ip, enable ? addr : stub, 0);
772 return ftrace_modify_code(ip, old, new);
775 int ftrace_enable_ftrace_graph_caller(void)
777 return ftrace_modify_ftrace_graph_caller(true);
780 int ftrace_disable_ftrace_graph_caller(void)
782 return ftrace_modify_ftrace_graph_caller(false);
786 * Hook the return address and push it in the stack of return addrs
787 * in current thread info. Return the address we want to divert to.
790 __prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned long sp)
792 unsigned long return_hooker;
795 if (unlikely(ftrace_graph_is_dead()))
798 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
801 bit = ftrace_test_recursion_trylock(ip, parent);
805 return_hooker = ppc_function_entry(return_to_handler);
807 if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp))
808 parent = return_hooker;
810 ftrace_test_recursion_unlock(bit);
815 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
816 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
817 struct ftrace_ops *op, struct ftrace_regs *fregs)
819 arch_ftrace_regs(fregs)->regs.link = __prepare_ftrace_return(parent_ip, ip, arch_ftrace_regs(fregs)->regs.gpr[1]);
822 unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
825 return __prepare_ftrace_return(parent, ip, sp);
828 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
830 #ifdef CONFIG_PPC64_ELF_ABI_V1
831 char *arch_ftrace_match_adjust(char *str, const char *search)
833 if (str[0] == '.' && search[0] != '.')
838 #endif /* CONFIG_PPC64_ELF_ABI_V1 */