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;
54 addr = GET_ADDR(addr);
56 /* Set to "bl addr" */
57 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffe);
60 * No locking needed, this must be called via kstop_machine
61 * which in essence is like running on a uniprocessor machine.
63 return (unsigned char *)&op;
67 # define _ASM_ALIGN " .align 3 "
68 # define _ASM_PTR " .llong "
70 # define _ASM_ALIGN " .align 2 "
71 # define _ASM_PTR " .long "
75 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
76 unsigned char *new_code)
79 unsigned old = *(unsigned *)old_code;
80 unsigned new = *(unsigned *)new_code;
83 /* move the IP back to the start of the call */
87 * Note: Due to modules and __init, code can
88 * disappear and change, we need to protect against faulting
89 * as well as code changing.
91 * No real locking needed, this code is run through
100 ".section .fixup, \"ax\"\n"
104 ".section __ex_table,\"a\"\n"
108 : "=r"(faulted), "=r"(replaced)
110 "0"(faulted), "r"(old)
113 if (replaced != old && replaced != new)
117 flush_icache_range(ip, ip + 8);
122 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
124 unsigned long ip = (unsigned long)(&ftrace_call);
125 unsigned char old[4], *new;
130 memcpy(old, &ftrace_call, 4);
131 new = ftrace_call_replace(ip, (unsigned long)func);
132 ret = ftrace_modify_code(ip, old, new);
137 notrace int ftrace_mcount_set(unsigned long *data)
139 unsigned long ip = (long)(&mcount_call);
140 unsigned long *addr = data;
141 unsigned char old[4], *new;
143 /* ip is at the location, but modify code will subtact this */
147 * Replace the mcount stub with a pointer to the
148 * ip recorder function.
150 memcpy(old, &mcount_call, 4);
151 new = ftrace_call_replace(ip, *addr);
152 *addr = ftrace_modify_code(ip, old, new);
157 int __init ftrace_dyn_arch_init(void *data)
159 /* This is running in kstop_machine */
161 ftrace_mcount_set(data);