2 * Copyright (C) 2008 Matt Fleming <matt@console-pimps.org>
3 * Copyright (C) 2008 Paul Mundt <lethal@linux-sh.org>
5 * Code for replacing ftrace calls with jumps.
7 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
9 * Thanks goes to Ingo Molnar, for suggesting the idea.
10 * Mathieu Desnoyers, for suggesting postponing the modifications.
11 * Arjan van de Ven, for keeping me straight, and explaining to me
12 * the dangers of modifying code on the run.
14 #include <linux/uaccess.h>
15 #include <linux/ftrace.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <asm/ftrace.h>
21 #include <asm/cacheflush.h>
22 #include <asm/unistd.h>
23 #include <trace/syscall.h>
25 #ifdef CONFIG_DYNAMIC_FTRACE
26 static unsigned char ftrace_replaced_code
[MCOUNT_INSN_SIZE
];
28 static unsigned char ftrace_nop
[4];
30 * If we're trying to nop out a call to a function, we instead
31 * place a call to the address after the memory table.
34 * 8c011060: 02 d1 mov.l 8c01106c <a+0xc>,r1
35 * 8c011062: 22 4f sts.l pr,@-r15
36 * 8c011064: 02 c7 mova 8c011070 <a+0x10>,r0
37 * 8c011066: 2b 41 jmp @r1
38 * 8c011068: 2a 40 lds r0,pr
40 * 8c01106c: 68 24 .word 0x2468 <--- ip
41 * 8c01106e: 1d 8c .word 0x8c1d
42 * 8c011070: 26 4f lds.l @r15+,pr <--- ip + MCOUNT_INSN_SIZE
44 * We write 0x8c011070 to 0x8c01106c so that on entry to a() we branch
45 * past the _mcount call and continue executing code like normal.
47 static unsigned char *ftrace_nop_replace(unsigned long ip
)
49 __raw_writel(ip
+ MCOUNT_INSN_SIZE
, ftrace_nop
);
53 static unsigned char *ftrace_call_replace(unsigned long ip
, unsigned long addr
)
55 /* Place the address in the memory table. */
56 __raw_writel(addr
, ftrace_replaced_code
);
59 * No locking needed, this must be called via kstop_machine
60 * which in essence is like running on a uniprocessor machine.
62 return ftrace_replaced_code
;
66 * Modifying code must take extra care. On an SMP machine, if
67 * the code being modified is also being executed on another CPU
68 * that CPU will have undefined results and possibly take a GPF.
69 * We use kstop_machine to stop other CPUS from exectuing code.
70 * But this does not stop NMIs from happening. We still need
71 * to protect against that. We separate out the modification of
72 * the code to take care of this.
74 * Two buffers are added: An IP buffer and a "code" buffer.
76 * 1) Put the instruction pointer into the IP buffer
77 * and the new code into the "code" buffer.
78 * 2) Wait for any running NMIs to finish and set a flag that says
79 * we are modifying code, it is done in an atomic operation.
82 * 5) Wait for any running NMIs to finish.
84 * If an NMI is executed, the first thing it does is to call
85 * "ftrace_nmi_enter". This will check if the flag is set to write
86 * and if it is, it will write what is in the IP and "code" buffers.
88 * The trick is, it does not matter if everyone is writing the same
89 * content to the code location. Also, if a CPU is executing code
90 * it is OK to write to that code location if the contents being written
91 * are the same as what exists.
93 #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
94 static atomic_t nmi_running
= ATOMIC_INIT(0);
95 static int mod_code_status
; /* holds return value of text write */
96 static void *mod_code_ip
; /* holds the IP to write to */
97 static void *mod_code_newcode
; /* holds the text to write to the IP */
99 static unsigned nmi_wait_count
;
100 static atomic_t nmi_update_count
= ATOMIC_INIT(0);
102 int ftrace_arch_read_dyn_info(char *buf
, int size
)
106 r
= snprintf(buf
, size
, "%u %u",
108 atomic_read(&nmi_update_count
));
112 static void clear_mod_flag(void)
114 int old
= atomic_read(&nmi_running
);
117 int new = old
& ~MOD_CODE_WRITE_FLAG
;
122 old
= atomic_cmpxchg(&nmi_running
, old
, new);
126 static void ftrace_mod_code(void)
129 * Yes, more than one CPU process can be writing to mod_code_status.
130 * (and the code itself)
131 * But if one were to fail, then they all should, and if one were
132 * to succeed, then they all should.
134 mod_code_status
= probe_kernel_write(mod_code_ip
, mod_code_newcode
,
137 /* if we fail, then kill any new writers */
142 void ftrace_nmi_enter(void)
144 if (atomic_inc_return(&nmi_running
) & MOD_CODE_WRITE_FLAG
) {
147 atomic_inc(&nmi_update_count
);
149 /* Must have previous changes seen before executions */
153 void ftrace_nmi_exit(void)
155 /* Finish all executions before clearing nmi_running */
157 atomic_dec(&nmi_running
);
160 static void wait_for_nmi_and_set_mod_flag(void)
162 if (!atomic_cmpxchg(&nmi_running
, 0, MOD_CODE_WRITE_FLAG
))
167 } while (atomic_cmpxchg(&nmi_running
, 0, MOD_CODE_WRITE_FLAG
));
172 static void wait_for_nmi(void)
174 if (!atomic_read(&nmi_running
))
179 } while (atomic_read(&nmi_running
));
185 do_ftrace_mod_code(unsigned long ip
, void *new_code
)
187 mod_code_ip
= (void *)ip
;
188 mod_code_newcode
= new_code
;
190 /* The buffers need to be visible before we let NMIs write them */
193 wait_for_nmi_and_set_mod_flag();
195 /* Make sure all running NMIs have finished before we write the code */
200 /* Make sure the write happens before clearing the bit */
206 return mod_code_status
;
209 static int ftrace_modify_code(unsigned long ip
, unsigned char *old_code
,
210 unsigned char *new_code
)
212 unsigned char replaced
[MCOUNT_INSN_SIZE
];
215 * Note: Due to modules and __init, code can
216 * disappear and change, we need to protect against faulting
217 * as well as code changing. We do this by using the
218 * probe_kernel_* functions.
220 * No real locking needed, this code is run through
221 * kstop_machine, or before SMP starts.
224 /* read the text we want to modify */
225 if (probe_kernel_read(replaced
, (void *)ip
, MCOUNT_INSN_SIZE
))
228 /* Make sure it is what we expect it to be */
229 if (memcmp(replaced
, old_code
, MCOUNT_INSN_SIZE
) != 0)
232 /* replace the text with the new text */
233 if (do_ftrace_mod_code(ip
, new_code
))
236 flush_icache_range(ip
, ip
+ MCOUNT_INSN_SIZE
);
241 int ftrace_update_ftrace_func(ftrace_func_t func
)
243 unsigned long ip
= (unsigned long)(&ftrace_call
) + MCOUNT_INSN_OFFSET
;
244 unsigned char old
[MCOUNT_INSN_SIZE
], *new;
246 memcpy(old
, (unsigned char *)ip
, MCOUNT_INSN_SIZE
);
247 new = ftrace_call_replace(ip
, (unsigned long)func
);
249 return ftrace_modify_code(ip
, old
, new);
252 int ftrace_make_nop(struct module
*mod
,
253 struct dyn_ftrace
*rec
, unsigned long addr
)
255 unsigned char *new, *old
;
256 unsigned long ip
= rec
->ip
;
258 old
= ftrace_call_replace(ip
, addr
);
259 new = ftrace_nop_replace(ip
);
261 return ftrace_modify_code(rec
->ip
, old
, new);
264 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
266 unsigned char *new, *old
;
267 unsigned long ip
= rec
->ip
;
269 old
= ftrace_nop_replace(ip
);
270 new = ftrace_call_replace(ip
, addr
);
272 return ftrace_modify_code(rec
->ip
, old
, new);
275 int __init
ftrace_dyn_arch_init(void *data
)
277 /* The return code is retured via data */
278 __raw_writel(0, (unsigned long)data
);
282 #endif /* CONFIG_DYNAMIC_FTRACE */
284 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
285 #ifdef CONFIG_DYNAMIC_FTRACE
286 extern void ftrace_graph_call(void);
288 static int ftrace_mod(unsigned long ip
, unsigned long old_addr
,
289 unsigned long new_addr
)
291 unsigned char code
[MCOUNT_INSN_SIZE
];
293 if (probe_kernel_read(code
, (void *)ip
, MCOUNT_INSN_SIZE
))
296 if (old_addr
!= __raw_readl((unsigned long *)code
))
299 __raw_writel(new_addr
, ip
);
303 int ftrace_enable_ftrace_graph_caller(void)
305 unsigned long ip
, old_addr
, new_addr
;
307 ip
= (unsigned long)(&ftrace_graph_call
) + GRAPH_INSN_OFFSET
;
308 old_addr
= (unsigned long)(&skip_trace
);
309 new_addr
= (unsigned long)(&ftrace_graph_caller
);
311 return ftrace_mod(ip
, old_addr
, new_addr
);
314 int ftrace_disable_ftrace_graph_caller(void)
316 unsigned long ip
, old_addr
, new_addr
;
318 ip
= (unsigned long)(&ftrace_graph_call
) + GRAPH_INSN_OFFSET
;
319 old_addr
= (unsigned long)(&ftrace_graph_caller
);
320 new_addr
= (unsigned long)(&skip_trace
);
322 return ftrace_mod(ip
, old_addr
, new_addr
);
324 #endif /* CONFIG_DYNAMIC_FTRACE */
327 * Hook the return address and push it in the stack of return addrs
328 * in the current thread info.
330 * This is the main routine for the function graph tracer. The function
331 * graph tracer essentially works like this:
333 * parent is the stack address containing self_addr's return address.
334 * We pull the real return address out of parent and store it in
335 * current's ret_stack. Then, we replace the return address on the stack
336 * with the address of return_to_handler. self_addr is the function that
339 * When self_addr returns, it will jump to return_to_handler which calls
340 * ftrace_return_to_handler. ftrace_return_to_handler will pull the real
341 * return address off of current's ret_stack and jump to it.
343 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
)
347 struct ftrace_graph_ent trace
;
348 unsigned long return_hooker
= (unsigned long)&return_to_handler
;
350 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
354 * Protect against fault, even if it shouldn't
355 * happen. This tool is too much intrusive to
356 * ignore such a protection.
358 __asm__
__volatile__(
365 ".section .fixup, \"ax\" \n\t"
373 ".section __ex_table,\"a\" \n\t"
377 : "=&r" (old
), "=r" (faulted
)
378 : "r" (parent
), "r" (return_hooker
)
381 if (unlikely(faulted
)) {
387 err
= ftrace_push_return_trace(old
, self_addr
, &trace
.depth
, 0);
389 __raw_writel(old
, parent
);
393 trace
.func
= self_addr
;
395 /* Only trace if the calling function expects to */
396 if (!ftrace_graph_entry(&trace
)) {
397 current
->curr_ret_stack
--;
398 __raw_writel(old
, parent
);
401 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */