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 #include <linux/spinlock.h>
14 #include <linux/hardirq.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/ftrace.h>
18 #include <linux/percpu.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
22 #include <asm/cacheflush.h>
23 #include <asm/code-patching.h>
24 #include <asm/ftrace.h>
25 #include <asm/syscall.h>
28 #ifdef CONFIG_DYNAMIC_FTRACE
30 ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
34 addr = ppc_function_entry((void *)addr);
36 /* if (link) set op to 'bl' else 'b' */
37 op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
43 ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
45 unsigned int replaced;
48 * Note: Due to modules and __init, code can
49 * disappear and change, we need to protect against faulting
50 * as well as code changing. We do this by using the
51 * probe_kernel_* functions.
53 * No real locking needed, this code is run through
54 * kstop_machine, or before SMP starts.
57 /* read the text we want to modify */
58 if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
61 /* Make sure it is what we expect it to be */
65 /* replace the text with the new text */
66 if (patch_instruction((unsigned int *)ip, new))
73 * Helper functions that are the same for both PPC64 and PPC32.
75 static int test_24bit_addr(unsigned long ip, unsigned long addr)
77 addr = ppc_function_entry((void *)addr);
79 /* use the create_branch to verify that this offset can be branched */
80 return create_branch((unsigned int *)ip, addr, 0);
85 static int is_bl_op(unsigned int op)
87 return (op & 0xfc000003) == 0x48000001;
90 static unsigned long find_bl_target(unsigned long ip, unsigned int op)
94 offset = (op & 0x03fffffc);
96 if (offset & 0x02000000)
99 return ip + (long)offset;
104 __ftrace_make_nop(struct module *mod,
105 struct dyn_ftrace *rec, unsigned long addr)
109 unsigned long ip = rec->ip;
112 /* read where this goes */
113 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
116 /* Make sure that that this is still a 24bit jump */
118 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
122 /* lets find where the pointer goes */
123 tramp = (void *)find_bl_target(ip, op);
125 pr_devel("ip:%lx jumps to %p", ip, tramp);
127 if (!is_module_trampoline(tramp)) {
128 printk(KERN_ERR "Not a trampoline\n");
132 if (module_trampoline_target(mod, tramp, &ptr)) {
133 printk(KERN_ERR "Failed to get trampoline target\n");
137 pr_devel("trampoline target %lx", ptr);
139 /* This should match what was called */
140 if (ptr != ppc_function_entry((void *)addr)) {
141 printk(KERN_ERR "addr %lx does not match expected %lx\n",
142 ptr, ppc_function_entry((void *)addr));
147 * Our original call site looks like:
152 * Milton Miller pointed out that we can not simply nop the branch.
153 * If a task was preempted when calling a trace function, the nops
154 * will remove the way to restore the TOC in r2 and the r2 TOC will
157 * Use a b +8 to jump over the load.
159 op = 0x48000008; /* b +8 */
161 if (patch_instruction((unsigned int *)ip, op))
169 __ftrace_make_nop(struct module *mod,
170 struct dyn_ftrace *rec, unsigned long addr)
174 unsigned long ip = rec->ip;
177 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
180 /* Make sure that that this is still a 24bit jump */
182 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
186 /* lets find where the pointer goes */
187 tramp = find_bl_target(ip, op);
190 * On PPC32 the trampoline looks like:
191 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
192 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
193 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
194 * 0x4e, 0x80, 0x04, 0x20 bctr
197 pr_devel("ip:%lx jumps to %lx", ip, tramp);
199 /* Find where the trampoline jumps to */
200 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
201 printk(KERN_ERR "Failed to read %lx\n", tramp);
205 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
207 /* verify that this is what we expect it to be */
208 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
209 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
210 (jmp[2] != 0x7d8903a6) ||
211 (jmp[3] != 0x4e800420)) {
212 printk(KERN_ERR "Not a trampoline\n");
216 tramp = (jmp[1] & 0xffff) |
217 ((jmp[0] & 0xffff) << 16);
221 pr_devel(" %lx ", tramp);
225 "Trampoline location %08lx does not match addr\n",
232 if (patch_instruction((unsigned int *)ip, op))
238 #endif /* CONFIG_MODULES */
240 int ftrace_make_nop(struct module *mod,
241 struct dyn_ftrace *rec, unsigned long addr)
243 unsigned long ip = rec->ip;
244 unsigned int old, new;
247 * If the calling address is more that 24 bits away,
248 * then we had to use a trampoline to make the call.
249 * Otherwise just update the call site.
251 if (test_24bit_addr(ip, addr)) {
253 old = ftrace_call_replace(ip, addr, 1);
255 return ftrace_modify_code(ip, old, new);
258 #ifdef CONFIG_MODULES
260 * Out of range jumps are called from modules.
261 * We should either already have a pointer to the module
262 * or it has been passed in.
264 if (!rec->arch.mod) {
266 printk(KERN_ERR "No module loaded addr=%lx\n",
272 if (mod != rec->arch.mod) {
274 "Record mod %p not equal to passed in mod %p\n",
278 /* nothing to do if mod == rec->arch.mod */
282 return __ftrace_make_nop(mod, rec, addr);
284 /* We should not get here without modules */
286 #endif /* CONFIG_MODULES */
289 #ifdef CONFIG_MODULES
292 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
295 unsigned long ip = rec->ip;
297 /* read where this goes */
298 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
302 * It should be pointing to two nops or
305 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
306 ((op[0] != PPC_INST_NOP) || (op[1] != PPC_INST_NOP))) {
307 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
311 /* If we never set up a trampoline to ftrace_caller, then bail */
312 if (!rec->arch.mod->arch.tramp) {
313 printk(KERN_ERR "No ftrace trampoline\n");
317 /* create the branch to the trampoline */
318 op[0] = create_branch((unsigned int *)ip,
319 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
321 printk(KERN_ERR "REL24 out of range!\n");
328 pr_devel("write to %lx\n", rec->ip);
330 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
333 flush_icache_range(ip, ip + 8);
339 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
342 unsigned long ip = rec->ip;
344 /* read where this goes */
345 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
348 /* It should be pointing to a nop */
349 if (op != PPC_INST_NOP) {
350 printk(KERN_ERR "Expected NOP but have %x\n", op);
354 /* If we never set up a trampoline to ftrace_caller, then bail */
355 if (!rec->arch.mod->arch.tramp) {
356 printk(KERN_ERR "No ftrace trampoline\n");
360 /* create the branch to the trampoline */
361 op = create_branch((unsigned int *)ip,
362 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
364 printk(KERN_ERR "REL24 out of range!\n");
368 pr_devel("write to %lx\n", rec->ip);
370 if (patch_instruction((unsigned int *)ip, op))
375 #endif /* CONFIG_PPC64 */
376 #endif /* CONFIG_MODULES */
378 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
380 unsigned long ip = rec->ip;
381 unsigned int old, new;
384 * If the calling address is more that 24 bits away,
385 * then we had to use a trampoline to make the call.
386 * Otherwise just update the call site.
388 if (test_24bit_addr(ip, addr)) {
391 new = ftrace_call_replace(ip, addr, 1);
392 return ftrace_modify_code(ip, old, new);
395 #ifdef CONFIG_MODULES
397 * Out of range jumps are called from modules.
398 * Being that we are converting from nop, it had better
399 * already have a module defined.
401 if (!rec->arch.mod) {
402 printk(KERN_ERR "No module loaded\n");
406 return __ftrace_make_call(rec, addr);
408 /* We should not get here without modules */
410 #endif /* CONFIG_MODULES */
413 int ftrace_update_ftrace_func(ftrace_func_t func)
415 unsigned long ip = (unsigned long)(&ftrace_call);
416 unsigned int old, new;
419 old = *(unsigned int *)&ftrace_call;
420 new = ftrace_call_replace(ip, (unsigned long)func, 1);
421 ret = ftrace_modify_code(ip, old, new);
426 static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
428 unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
431 ret = ftrace_update_record(rec, enable);
434 case FTRACE_UPDATE_IGNORE:
436 case FTRACE_UPDATE_MAKE_CALL:
437 return ftrace_make_call(rec, ftrace_addr);
438 case FTRACE_UPDATE_MAKE_NOP:
439 return ftrace_make_nop(NULL, rec, ftrace_addr);
445 void ftrace_replace_code(int enable)
447 struct ftrace_rec_iter *iter;
448 struct dyn_ftrace *rec;
451 for (iter = ftrace_rec_iter_start(); iter;
452 iter = ftrace_rec_iter_next(iter)) {
453 rec = ftrace_rec_iter_record(iter);
454 ret = __ftrace_replace_code(rec, enable);
456 ftrace_bug(ret, rec->ip);
462 void arch_ftrace_update_code(int command)
464 if (command & FTRACE_UPDATE_CALLS)
465 ftrace_replace_code(1);
466 else if (command & FTRACE_DISABLE_CALLS)
467 ftrace_replace_code(0);
469 if (command & FTRACE_UPDATE_TRACE_FUNC)
470 ftrace_update_ftrace_func(ftrace_trace_function);
472 if (command & FTRACE_START_FUNC_RET)
473 ftrace_enable_ftrace_graph_caller();
474 else if (command & FTRACE_STOP_FUNC_RET)
475 ftrace_disable_ftrace_graph_caller();
478 int __init ftrace_dyn_arch_init(void)
482 #endif /* CONFIG_DYNAMIC_FTRACE */
484 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
486 #ifdef CONFIG_DYNAMIC_FTRACE
487 extern void ftrace_graph_call(void);
488 extern void ftrace_graph_stub(void);
490 int ftrace_enable_ftrace_graph_caller(void)
492 unsigned long ip = (unsigned long)(&ftrace_graph_call);
493 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
494 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
495 unsigned int old, new;
497 old = ftrace_call_replace(ip, stub, 0);
498 new = ftrace_call_replace(ip, addr, 0);
500 return ftrace_modify_code(ip, old, new);
503 int ftrace_disable_ftrace_graph_caller(void)
505 unsigned long ip = (unsigned long)(&ftrace_graph_call);
506 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
507 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
508 unsigned int old, new;
510 old = ftrace_call_replace(ip, addr, 0);
511 new = ftrace_call_replace(ip, stub, 0);
513 return ftrace_modify_code(ip, old, new);
515 #endif /* CONFIG_DYNAMIC_FTRACE */
518 extern void mod_return_to_handler(void);
522 * Hook the return address and push it in the stack of return addrs
523 * in current thread info.
525 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
529 struct ftrace_graph_ent trace;
530 unsigned long return_hooker = (unsigned long)&return_to_handler;
532 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
536 /* non core kernel code needs to save and restore the TOC */
537 if (REGION_ID(self_addr) != KERNEL_REGION_ID)
538 return_hooker = (unsigned long)&mod_return_to_handler;
541 return_hooker = ppc_function_entry((void *)return_hooker);
544 * Protect against fault, even if it shouldn't
545 * happen. This tool is too much intrusive to
546 * ignore such a protection.
549 "1: " PPC_LL "%[old], 0(%[parent])\n"
550 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
551 " li %[faulted], 0\n"
554 ".section .fixup, \"ax\"\n"
555 "4: li %[faulted], 1\n"
559 ".section __ex_table,\"a\"\n"
565 : [old] "=&r" (old), [faulted] "=r" (faulted)
566 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
570 if (unlikely(faulted)) {
576 trace.func = self_addr;
577 trace.depth = current->curr_ret_stack + 1;
579 /* Only trace if the calling function expects to */
580 if (!ftrace_graph_entry(&trace)) {
585 if (ftrace_push_return_trace(old, self_addr, &trace.depth, 0) == -EBUSY)
588 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
590 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
591 unsigned long __init arch_syscall_addr(int nr)
593 return sys_call_table[nr*2];
595 #endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */