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/uaccess.h>
13 #include <linux/ftrace.h>
14 #include <linux/percpu.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
18 #include <asm/cacheflush.h>
19 #include <asm/ftrace.h>
22 static unsigned int ftrace_nop = 0x60000000;
25 # define GET_ADDR(addr) addr
27 /* PowerPC64's functions are data that points to the functions */
28 # define GET_ADDR(addr) *(unsigned long *)addr
32 static unsigned int ftrace_calc_offset(long ip, long addr)
34 return (int)(addr - ip);
37 static unsigned char *ftrace_nop_replace(void)
39 return (char *)&ftrace_nop;
42 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
44 static unsigned int op;
47 * It would be nice to just use create_function_call, but that will
48 * update the code itself. Here we need to just return the
49 * instruction that is going to be modified, without modifying the
52 addr = GET_ADDR(addr);
54 /* Set to "bl addr" */
55 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
58 * No locking needed, this must be called via kstop_machine
59 * which in essence is like running on a uniprocessor machine.
61 return (unsigned char *)&op;
65 # define _ASM_ALIGN " .align 3 "
66 # define _ASM_PTR " .llong "
68 # define _ASM_ALIGN " .align 2 "
69 # define _ASM_PTR " .long "
73 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
74 unsigned char *new_code)
76 unsigned char replaced[MCOUNT_INSN_SIZE];
79 * Note: Due to modules and __init, code can
80 * disappear and change, we need to protect against faulting
81 * as well as code changing. We do this by using the
82 * probe_kernel_* functions.
84 * No real locking needed, this code is run through
85 * kstop_machine, or before SMP starts.
88 /* read the text we want to modify */
89 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
92 /* Make sure it is what we expect it to be */
93 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
96 /* replace the text with the new text */
97 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
100 flush_icache_range(ip, ip + 8);
105 static int test_24bit_addr(unsigned long ip, unsigned long addr)
110 * Can we get to addr from ip in 24 bits?
111 * (26 really, since we mulitply by 4 for 4 byte alignment)
116 * Return true if diff is less than 1 << 25
117 * and greater than -1 << 26.
119 return (diff < (1 << 25)) && (diff > (-1 << 26));
122 int ftrace_make_nop(struct module *mod,
123 struct dyn_ftrace *rec, unsigned long addr)
125 unsigned char *old, *new;
128 * If the calling address is more that 24 bits away,
129 * then we had to use a trampoline to make the call.
130 * Otherwise just update the call site.
132 if (test_24bit_addr(rec->ip, addr)) {
134 old = ftrace_call_replace(rec->ip, addr);
135 new = ftrace_nop_replace();
136 return ftrace_modify_code(rec->ip, old, new);
142 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
144 unsigned char *old, *new;
147 * If the calling address is more that 24 bits away,
148 * then we had to use a trampoline to make the call.
149 * Otherwise just update the call site.
151 if (test_24bit_addr(rec->ip, addr)) {
153 old = ftrace_nop_replace();
154 new = ftrace_call_replace(rec->ip, addr);
155 return ftrace_modify_code(rec->ip, old, new);
161 int ftrace_update_ftrace_func(ftrace_func_t func)
163 unsigned long ip = (unsigned long)(&ftrace_call);
164 unsigned char old[MCOUNT_INSN_SIZE], *new;
167 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
168 new = ftrace_call_replace(ip, (unsigned long)func);
169 ret = ftrace_modify_code(ip, old, new);
174 int __init ftrace_dyn_arch_init(void *data)
176 /* caller expects data to be zero */
177 unsigned long *p = data;