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 notrace int ftrace_ip_converted(unsigned long ip)
35 save = *(unsigned int *)ip;
37 return save == ftrace_nop;
40 static unsigned int notrace ftrace_calc_offset(long ip, long addr)
42 return (int)((addr + CALL_BACK) - ip);
45 notrace unsigned char *ftrace_nop_replace(void)
47 return (char *)&ftrace_nop;
50 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
52 static unsigned int op;
55 * It would be nice to just use create_function_call, but that will
56 * update the code itself. Here we need to just return the
57 * instruction that is going to be modified, without modifying the
60 addr = GET_ADDR(addr);
62 /* Set to "bl addr" */
63 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
66 * No locking needed, this must be called via kstop_machine
67 * which in essence is like running on a uniprocessor machine.
69 return (unsigned char *)&op;
73 # define _ASM_ALIGN " .align 3 "
74 # define _ASM_PTR " .llong "
76 # define _ASM_ALIGN " .align 2 "
77 # define _ASM_PTR " .long "
81 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
82 unsigned char *new_code)
85 unsigned old = *(unsigned *)old_code;
86 unsigned new = *(unsigned *)new_code;
89 /* move the IP back to the start of the call */
93 * Note: Due to modules and __init, code can
94 * disappear and change, we need to protect against faulting
95 * as well as code changing.
97 * No real locking needed, this code is run through
106 ".section .fixup, \"ax\"\n"
110 ".section __ex_table,\"a\"\n"
114 : "=r"(faulted), "=r"(replaced)
116 "0"(faulted), "r"(old)
119 if (replaced != old && replaced != new)
123 flush_icache_range(ip, ip + 8);
128 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
130 unsigned long ip = (unsigned long)(&ftrace_call);
131 unsigned char old[4], *new;
136 memcpy(old, &ftrace_call, 4);
137 new = ftrace_call_replace(ip, (unsigned long)func);
138 ret = ftrace_modify_code(ip, old, new);
143 notrace int ftrace_mcount_set(unsigned long *data)
145 unsigned long ip = (long)(&mcount_call);
146 unsigned long *addr = data;
147 unsigned char old[4], *new;
149 /* ip is at the location, but modify code will subtact this */
153 * Replace the mcount stub with a pointer to the
154 * ip recorder function.
156 memcpy(old, &mcount_call, 4);
157 new = ftrace_call_replace(ip, *addr);
158 *addr = ftrace_modify_code(ip, old, new);
163 int __init ftrace_dyn_arch_init(void *data)
165 /* This is running in kstop_machine */
167 ftrace_mcount_set(data);