2 * Code for replacing ftrace calls with jumps.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
8 * Added function graph tracer code, taken from x86 that was written
9 * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
13 #define pr_fmt(fmt) "ftrace-powerpc: " fmt
15 #include <linux/spinlock.h>
16 #include <linux/hardirq.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ftrace.h>
20 #include <linux/percpu.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
24 #include <asm/cacheflush.h>
25 #include <asm/code-patching.h>
26 #include <asm/ftrace.h>
27 #include <asm/syscall.h>
30 #ifdef CONFIG_DYNAMIC_FTRACE
32 ftrace_call_replace(unsigned long ip
, unsigned long addr
, int link
)
36 addr
= ppc_function_entry((void *)addr
);
38 /* if (link) set op to 'bl' else 'b' */
39 op
= create_branch((unsigned int *)ip
, addr
, link
? 1 : 0);
45 ftrace_modify_code(unsigned long ip
, unsigned int old
, unsigned int new)
47 unsigned int replaced
;
50 * Note: Due to modules and __init, code can
51 * disappear and change, we need to protect against faulting
52 * as well as code changing. We do this by using the
53 * probe_kernel_* functions.
55 * No real locking needed, this code is run through
56 * kstop_machine, or before SMP starts.
59 /* read the text we want to modify */
60 if (probe_kernel_read(&replaced
, (void *)ip
, MCOUNT_INSN_SIZE
))
63 /* Make sure it is what we expect it to be */
67 /* replace the text with the new text */
68 if (patch_instruction((unsigned int *)ip
, new))
75 * Helper functions that are the same for both PPC64 and PPC32.
77 static int test_24bit_addr(unsigned long ip
, unsigned long addr
)
79 addr
= ppc_function_entry((void *)addr
);
81 /* use the create_branch to verify that this offset can be branched */
82 return create_branch((unsigned int *)ip
, addr
, 0);
87 static int is_bl_op(unsigned int op
)
89 return (op
& 0xfc000003) == 0x48000001;
92 static unsigned long find_bl_target(unsigned long ip
, unsigned int op
)
96 offset
= (op
& 0x03fffffc);
98 if (offset
& 0x02000000)
101 return ip
+ (long)offset
;
106 __ftrace_make_nop(struct module
*mod
,
107 struct dyn_ftrace
*rec
, unsigned long addr
)
110 unsigned long entry
, ptr
;
111 unsigned long ip
= rec
->ip
;
114 /* read where this goes */
115 if (probe_kernel_read(&op
, (void *)ip
, sizeof(int)))
118 /* Make sure that that this is still a 24bit jump */
120 pr_err("Not expected bl: opcode is %x\n", op
);
124 /* lets find where the pointer goes */
125 tramp
= (void *)find_bl_target(ip
, op
);
127 pr_devel("ip:%lx jumps to %p", ip
, tramp
);
129 if (!is_module_trampoline(tramp
)) {
130 pr_err("Not a trampoline\n");
134 if (module_trampoline_target(mod
, tramp
, &ptr
)) {
135 pr_err("Failed to get trampoline target\n");
139 pr_devel("trampoline target %lx", ptr
);
141 entry
= ppc_global_function_entry((void *)addr
);
142 /* This should match what was called */
144 pr_err("addr %lx does not match expected %lx\n", ptr
, entry
);
149 * Our original call site looks like:
154 * Milton Miller pointed out that we can not simply nop the branch.
155 * If a task was preempted when calling a trace function, the nops
156 * will remove the way to restore the TOC in r2 and the r2 TOC will
159 * Use a b +8 to jump over the load.
161 op
= 0x48000008; /* b +8 */
163 if (patch_instruction((unsigned int *)ip
, op
))
171 __ftrace_make_nop(struct module
*mod
,
172 struct dyn_ftrace
*rec
, unsigned long addr
)
176 unsigned long ip
= rec
->ip
;
179 if (probe_kernel_read(&op
, (void *)ip
, MCOUNT_INSN_SIZE
))
182 /* Make sure that that this is still a 24bit jump */
184 pr_err("Not expected bl: opcode is %x\n", op
);
188 /* lets find where the pointer goes */
189 tramp
= find_bl_target(ip
, op
);
192 * On PPC32 the trampoline looks like:
193 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
194 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
195 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
196 * 0x4e, 0x80, 0x04, 0x20 bctr
199 pr_devel("ip:%lx jumps to %lx", ip
, tramp
);
201 /* Find where the trampoline jumps to */
202 if (probe_kernel_read(jmp
, (void *)tramp
, sizeof(jmp
))) {
203 pr_err("Failed to read %lx\n", tramp
);
207 pr_devel(" %08x %08x ", jmp
[0], jmp
[1]);
209 /* verify that this is what we expect it to be */
210 if (((jmp
[0] & 0xffff0000) != 0x3d800000) ||
211 ((jmp
[1] & 0xffff0000) != 0x398c0000) ||
212 (jmp
[2] != 0x7d8903a6) ||
213 (jmp
[3] != 0x4e800420)) {
214 pr_err("Not a trampoline\n");
218 tramp
= (jmp
[1] & 0xffff) |
219 ((jmp
[0] & 0xffff) << 16);
223 pr_devel(" %lx ", tramp
);
226 pr_err("Trampoline location %08lx does not match addr\n",
233 if (patch_instruction((unsigned int *)ip
, op
))
239 #endif /* CONFIG_MODULES */
241 int ftrace_make_nop(struct module
*mod
,
242 struct dyn_ftrace
*rec
, unsigned long addr
)
244 unsigned long ip
= rec
->ip
;
245 unsigned int old
, new;
248 * If the calling address is more that 24 bits away,
249 * then we had to use a trampoline to make the call.
250 * Otherwise just update the call site.
252 if (test_24bit_addr(ip
, addr
)) {
254 old
= ftrace_call_replace(ip
, addr
, 1);
256 return ftrace_modify_code(ip
, old
, new);
259 #ifdef CONFIG_MODULES
261 * Out of range jumps are called from modules.
262 * We should either already have a pointer to the module
263 * or it has been passed in.
265 if (!rec
->arch
.mod
) {
267 pr_err("No module loaded addr=%lx\n", addr
);
272 if (mod
!= rec
->arch
.mod
) {
273 pr_err("Record mod %p not equal to passed in mod %p\n",
277 /* nothing to do if mod == rec->arch.mod */
281 return __ftrace_make_nop(mod
, rec
, addr
);
283 /* We should not get here without modules */
285 #endif /* CONFIG_MODULES */
288 #ifdef CONFIG_MODULES
291 __ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
294 void *ip
= (void *)rec
->ip
;
296 /* read where this goes */
297 if (probe_kernel_read(op
, ip
, sizeof(op
)))
306 * The load offset is different depending on the ABI. For simplicity
307 * just mask it out when doing the compare.
309 if ((op
[0] != 0x48000008) || ((op
[1] & 0xffff0000) != 0xe8410000)) {
310 pr_err("Unexpected call sequence: %x %x\n", op
[0], op
[1]);
314 /* If we never set up a trampoline to ftrace_caller, then bail */
315 if (!rec
->arch
.mod
->arch
.tramp
) {
316 pr_err("No ftrace trampoline\n");
320 /* Ensure branch is within 24 bits */
321 if (!create_branch(ip
, rec
->arch
.mod
->arch
.tramp
, BRANCH_SET_LINK
)) {
322 pr_err("Branch out of range\n");
326 if (patch_branch(ip
, rec
->arch
.mod
->arch
.tramp
, BRANCH_SET_LINK
)) {
327 pr_err("REL24 out of range!\n");
335 __ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
338 unsigned long ip
= rec
->ip
;
340 /* read where this goes */
341 if (probe_kernel_read(&op
, (void *)ip
, MCOUNT_INSN_SIZE
))
344 /* It should be pointing to a nop */
345 if (op
!= PPC_INST_NOP
) {
346 pr_err("Expected NOP but have %x\n", op
);
350 /* If we never set up a trampoline to ftrace_caller, then bail */
351 if (!rec
->arch
.mod
->arch
.tramp
) {
352 pr_err("No ftrace trampoline\n");
356 /* create the branch to the trampoline */
357 op
= create_branch((unsigned int *)ip
,
358 rec
->arch
.mod
->arch
.tramp
, BRANCH_SET_LINK
);
360 pr_err("REL24 out of range!\n");
364 pr_devel("write to %lx\n", rec
->ip
);
366 if (patch_instruction((unsigned int *)ip
, op
))
371 #endif /* CONFIG_PPC64 */
372 #endif /* CONFIG_MODULES */
374 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
376 unsigned long ip
= rec
->ip
;
377 unsigned int old
, new;
380 * If the calling address is more that 24 bits away,
381 * then we had to use a trampoline to make the call.
382 * Otherwise just update the call site.
384 if (test_24bit_addr(ip
, addr
)) {
387 new = ftrace_call_replace(ip
, addr
, 1);
388 return ftrace_modify_code(ip
, old
, new);
391 #ifdef CONFIG_MODULES
393 * Out of range jumps are called from modules.
394 * Being that we are converting from nop, it had better
395 * already have a module defined.
397 if (!rec
->arch
.mod
) {
398 pr_err("No module loaded\n");
402 return __ftrace_make_call(rec
, addr
);
404 /* We should not get here without modules */
406 #endif /* CONFIG_MODULES */
409 int ftrace_update_ftrace_func(ftrace_func_t func
)
411 unsigned long ip
= (unsigned long)(&ftrace_call
);
412 unsigned int old
, new;
415 old
= *(unsigned int *)&ftrace_call
;
416 new = ftrace_call_replace(ip
, (unsigned long)func
, 1);
417 ret
= ftrace_modify_code(ip
, old
, new);
422 static int __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
424 unsigned long ftrace_addr
= (unsigned long)FTRACE_ADDR
;
427 ret
= ftrace_update_record(rec
, enable
);
430 case FTRACE_UPDATE_IGNORE
:
432 case FTRACE_UPDATE_MAKE_CALL
:
433 return ftrace_make_call(rec
, ftrace_addr
);
434 case FTRACE_UPDATE_MAKE_NOP
:
435 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
441 void ftrace_replace_code(int enable
)
443 struct ftrace_rec_iter
*iter
;
444 struct dyn_ftrace
*rec
;
447 for (iter
= ftrace_rec_iter_start(); iter
;
448 iter
= ftrace_rec_iter_next(iter
)) {
449 rec
= ftrace_rec_iter_record(iter
);
450 ret
= __ftrace_replace_code(rec
, enable
);
452 ftrace_bug(ret
, rec
->ip
);
458 void arch_ftrace_update_code(int command
)
460 if (command
& FTRACE_UPDATE_CALLS
)
461 ftrace_replace_code(1);
462 else if (command
& FTRACE_DISABLE_CALLS
)
463 ftrace_replace_code(0);
465 if (command
& FTRACE_UPDATE_TRACE_FUNC
)
466 ftrace_update_ftrace_func(ftrace_trace_function
);
468 if (command
& FTRACE_START_FUNC_RET
)
469 ftrace_enable_ftrace_graph_caller();
470 else if (command
& FTRACE_STOP_FUNC_RET
)
471 ftrace_disable_ftrace_graph_caller();
474 int __init
ftrace_dyn_arch_init(void)
478 #endif /* CONFIG_DYNAMIC_FTRACE */
480 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
482 #ifdef CONFIG_DYNAMIC_FTRACE
483 extern void ftrace_graph_call(void);
484 extern void ftrace_graph_stub(void);
486 int ftrace_enable_ftrace_graph_caller(void)
488 unsigned long ip
= (unsigned long)(&ftrace_graph_call
);
489 unsigned long addr
= (unsigned long)(&ftrace_graph_caller
);
490 unsigned long stub
= (unsigned long)(&ftrace_graph_stub
);
491 unsigned int old
, new;
493 old
= ftrace_call_replace(ip
, stub
, 0);
494 new = ftrace_call_replace(ip
, addr
, 0);
496 return ftrace_modify_code(ip
, old
, new);
499 int ftrace_disable_ftrace_graph_caller(void)
501 unsigned long ip
= (unsigned long)(&ftrace_graph_call
);
502 unsigned long addr
= (unsigned long)(&ftrace_graph_caller
);
503 unsigned long stub
= (unsigned long)(&ftrace_graph_stub
);
504 unsigned int old
, new;
506 old
= ftrace_call_replace(ip
, addr
, 0);
507 new = ftrace_call_replace(ip
, stub
, 0);
509 return ftrace_modify_code(ip
, old
, new);
511 #endif /* CONFIG_DYNAMIC_FTRACE */
514 extern void mod_return_to_handler(void);
518 * Hook the return address and push it in the stack of return addrs
519 * in current thread info.
521 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
)
525 struct ftrace_graph_ent trace
;
526 unsigned long return_hooker
= (unsigned long)&return_to_handler
;
528 if (unlikely(ftrace_graph_is_dead()))
531 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
535 /* non core kernel code needs to save and restore the TOC */
536 if (REGION_ID(self_addr
) != KERNEL_REGION_ID
)
537 return_hooker
= (unsigned long)&mod_return_to_handler
;
540 return_hooker
= ppc_function_entry((void *)return_hooker
);
543 * Protect against fault, even if it shouldn't
544 * happen. This tool is too much intrusive to
545 * ignore such a protection.
548 "1: " PPC_LL
"%[old], 0(%[parent])\n"
549 "2: " PPC_STL
"%[return_hooker], 0(%[parent])\n"
550 " li %[faulted], 0\n"
553 ".section .fixup, \"ax\"\n"
554 "4: li %[faulted], 1\n"
558 ".section __ex_table,\"a\"\n"
564 : [old
] "=&r" (old
), [faulted
] "=r" (faulted
)
565 : [parent
] "r" (parent
), [return_hooker
] "r" (return_hooker
)
569 if (unlikely(faulted
)) {
575 trace
.func
= self_addr
;
576 trace
.depth
= current
->curr_ret_stack
+ 1;
578 /* Only trace if the calling function expects to */
579 if (!ftrace_graph_entry(&trace
)) {
584 if (ftrace_push_return_trace(old
, self_addr
, &trace
.depth
, 0) == -EBUSY
)
587 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
589 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
590 unsigned long __init
arch_syscall_addr(int nr
)
592 return sys_call_table
[nr
*2];
594 #endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */