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.
10 #include <linux/spinlock.h>
11 #include <linux/hardirq.h>
12 #include <linux/ftrace.h>
13 #include <linux/percpu.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
17 #include <asm/cacheflush.h>
21 static unsigned int ftrace_nop = 0x60000000;
24 # define GET_ADDR(addr) addr
26 /* PowerPC64's functions are data that points to the functions */
27 # define GET_ADDR(addr) *(unsigned long *)addr
30 static unsigned int notrace ftrace_calc_offset(long ip, long addr)
32 return (int)((addr + CALL_BACK) - ip);
35 notrace unsigned char *ftrace_nop_replace(void)
37 return (char *)&ftrace_nop;
40 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
42 static unsigned int op;
45 * It would be nice to just use create_function_call, but that will
46 * update the code itself. Here we need to just return the
47 * instruction that is going to be modified, without modifying the
50 addr = GET_ADDR(addr);
52 /* Set to "bl addr" */
53 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
56 * No locking needed, this must be called via kstop_machine
57 * which in essence is like running on a uniprocessor machine.
59 return (unsigned char *)&op;
63 # define _ASM_ALIGN " .align 3 "
64 # define _ASM_PTR " .llong "
66 # define _ASM_ALIGN " .align 2 "
67 # define _ASM_PTR " .long "
71 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
72 unsigned char *new_code)
75 unsigned old = *(unsigned *)old_code;
76 unsigned new = *(unsigned *)new_code;
79 /* move the IP back to the start of the call */
83 * Note: Due to modules and __init, code can
84 * disappear and change, we need to protect against faulting
85 * as well as code changing.
87 * No real locking needed, this code is run through
96 ".section .fixup, \"ax\"\n"
100 ".section __ex_table,\"a\"\n"
104 : "=r"(faulted), "=r"(replaced)
106 "0"(faulted), "r"(old)
109 if (replaced != old && replaced != new)
113 flush_icache_range(ip, ip + 8);
118 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
120 unsigned long ip = (unsigned long)(&ftrace_call);
121 unsigned char old[4], *new;
126 memcpy(old, &ftrace_call, 4);
127 new = ftrace_call_replace(ip, (unsigned long)func);
128 ret = ftrace_modify_code(ip, old, new);
133 notrace int ftrace_mcount_set(unsigned long *data)
135 unsigned long ip = (long)(&mcount_call);
136 unsigned long *addr = data;
137 unsigned char old[4], *new;
139 /* ip is at the location, but modify code will subtact this */
143 * Replace the mcount stub with a pointer to the
144 * ip recorder function.
146 memcpy(old, &mcount_call, 4);
147 new = ftrace_call_replace(ip, *addr);
148 *addr = ftrace_modify_code(ip, old, new);
153 int __init ftrace_dyn_arch_init(void *data)
155 /* This is running in kstop_machine */
157 ftrace_mcount_set(data);