2 * Code for replacing ftrace calls with jumps.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Thanks goes to Ingo Molnar, for suggesting the idea.
7 * Mathieu Desnoyers, for suggesting postponing the modifications.
8 * Arjan van de Ven, for keeping me straight, and explaining to me
9 * the dangers of modifying code on the run.
12 #include <linux/spinlock.h>
13 #include <linux/hardirq.h>
14 #include <linux/uaccess.h>
15 #include <linux/ftrace.h>
16 #include <linux/percpu.h>
17 #include <linux/sched.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
21 #include <asm/ftrace.h>
22 #include <linux/ftrace.h>
27 #ifdef CONFIG_DYNAMIC_FTRACE
29 union ftrace_code_union {
30 char code[MCOUNT_INSN_SIZE];
34 } __attribute__((packed));
37 static int ftrace_calc_offset(long ip, long addr)
39 return (int)(addr - ip);
42 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
44 static union ftrace_code_union calc;
47 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
50 * No locking needed, this must be called via kstop_machine
51 * which in essence is like running on a uniprocessor machine.
57 * Modifying code must take extra care. On an SMP machine, if
58 * the code being modified is also being executed on another CPU
59 * that CPU will have undefined results and possibly take a GPF.
60 * We use kstop_machine to stop other CPUS from exectuing code.
61 * But this does not stop NMIs from happening. We still need
62 * to protect against that. We separate out the modification of
63 * the code to take care of this.
65 * Two buffers are added: An IP buffer and a "code" buffer.
67 * 1) Put the instruction pointer into the IP buffer
68 * and the new code into the "code" buffer.
69 * 2) Set a flag that says we are modifying code
70 * 3) Wait for any running NMIs to finish.
73 * 6) Wait for any running NMIs to finish.
75 * If an NMI is executed, the first thing it does is to call
76 * "ftrace_nmi_enter". This will check if the flag is set to write
77 * and if it is, it will write what is in the IP and "code" buffers.
79 * The trick is, it does not matter if everyone is writing the same
80 * content to the code location. Also, if a CPU is executing code
81 * it is OK to write to that code location if the contents being written
82 * are the same as what exists.
85 static atomic_t nmi_running = ATOMIC_INIT(0);
86 static int mod_code_status; /* holds return value of text write */
87 static int mod_code_write; /* set when NMI should do the write */
88 static void *mod_code_ip; /* holds the IP to write to */
89 static void *mod_code_newcode; /* holds the text to write to the IP */
91 static unsigned nmi_wait_count;
92 static atomic_t nmi_update_count = ATOMIC_INIT(0);
94 int ftrace_arch_read_dyn_info(char *buf, int size)
98 r = snprintf(buf, size, "%u %u",
100 atomic_read(&nmi_update_count));
104 static void ftrace_mod_code(void)
107 * Yes, more than one CPU process can be writing to mod_code_status.
108 * (and the code itself)
109 * But if one were to fail, then they all should, and if one were
110 * to succeed, then they all should.
112 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
116 void ftrace_nmi_enter(void)
118 atomic_inc(&nmi_running);
119 /* Must have nmi_running seen before reading write flag */
121 if (mod_code_write) {
123 atomic_inc(&nmi_update_count);
127 void ftrace_nmi_exit(void)
129 /* Finish all executions before clearing nmi_running */
131 atomic_dec(&nmi_running);
134 static void wait_for_nmi(void)
136 if (!atomic_read(&nmi_running))
141 } while (atomic_read(&nmi_running));
147 do_ftrace_mod_code(unsigned long ip, void *new_code)
149 mod_code_ip = (void *)ip;
150 mod_code_newcode = new_code;
152 /* The buffers need to be visible before we let NMIs write them */
157 /* Make sure write bit is visible before we wait on NMIs */
162 /* Make sure all running NMIs have finished before we write the code */
167 /* Make sure the write happens before clearing the bit */
172 /* make sure NMIs see the cleared bit */
177 return mod_code_status;
183 static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
185 static unsigned char *ftrace_nop_replace(void)
191 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
192 unsigned char *new_code)
194 unsigned char replaced[MCOUNT_INSN_SIZE];
197 * Note: Due to modules and __init, code can
198 * disappear and change, we need to protect against faulting
199 * as well as code changing. We do this by using the
200 * probe_kernel_* functions.
202 * No real locking needed, this code is run through
203 * kstop_machine, or before SMP starts.
206 /* read the text we want to modify */
207 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
210 /* Make sure it is what we expect it to be */
211 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
214 /* replace the text with the new text */
215 if (do_ftrace_mod_code(ip, new_code))
223 int ftrace_make_nop(struct module *mod,
224 struct dyn_ftrace *rec, unsigned long addr)
226 unsigned char *new, *old;
227 unsigned long ip = rec->ip;
229 old = ftrace_call_replace(ip, addr);
230 new = ftrace_nop_replace();
232 return ftrace_modify_code(rec->ip, old, new);
235 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
237 unsigned char *new, *old;
238 unsigned long ip = rec->ip;
240 old = ftrace_nop_replace();
241 new = ftrace_call_replace(ip, addr);
243 return ftrace_modify_code(rec->ip, old, new);
246 int ftrace_update_ftrace_func(ftrace_func_t func)
248 unsigned long ip = (unsigned long)(&ftrace_call);
249 unsigned char old[MCOUNT_INSN_SIZE], *new;
252 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
253 new = ftrace_call_replace(ip, (unsigned long)func);
254 ret = ftrace_modify_code(ip, old, new);
259 int __init ftrace_dyn_arch_init(void *data)
261 extern const unsigned char ftrace_test_p6nop[];
262 extern const unsigned char ftrace_test_nop5[];
263 extern const unsigned char ftrace_test_jmp[];
267 * There is no good nop for all x86 archs.
268 * We will default to using the P6_NOP5, but first we
269 * will test to make sure that the nop will actually
270 * work on this CPU. If it faults, we will then
271 * go to a lesser efficient 5 byte nop. If that fails
272 * we then just use a jmp as our nop. This isn't the most
273 * efficient nop, but we can not use a multi part nop
274 * since we would then risk being preempted in the middle
275 * of that nop, and if we enabled tracing then, it might
276 * cause a system crash.
278 * TODO: check the cpuid to determine the best nop.
282 "jmp ftrace_test_p6nop\n"
285 "nop\n" /* 2 byte jmp + 3 bytes */
290 ".byte 0x66,0x66,0x66,0x66,0x90\n"
292 ".section .fixup, \"ax\"\n"
294 " jmp ftrace_test_nop5\n"
298 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
299 _ASM_EXTABLE(ftrace_test_nop5, 3b)
300 : "=r"(faulted) : "0" (faulted));
304 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
305 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
308 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
309 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
312 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
313 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
317 /* The return code is retured via data */
318 *(unsigned long *)data = 0;
324 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
326 #ifdef CONFIG_DYNAMIC_FTRACE
327 extern void ftrace_graph_call(void);
329 static int ftrace_mod_jmp(unsigned long ip,
330 int old_offset, int new_offset)
332 unsigned char code[MCOUNT_INSN_SIZE];
334 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
337 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
340 *(int *)(&code[1]) = new_offset;
342 if (do_ftrace_mod_code(ip, &code))
348 int ftrace_enable_ftrace_graph_caller(void)
350 unsigned long ip = (unsigned long)(&ftrace_graph_call);
351 int old_offset, new_offset;
353 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
354 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
356 return ftrace_mod_jmp(ip, old_offset, new_offset);
359 int ftrace_disable_ftrace_graph_caller(void)
361 unsigned long ip = (unsigned long)(&ftrace_graph_call);
362 int old_offset, new_offset;
364 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
365 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
367 return ftrace_mod_jmp(ip, old_offset, new_offset);
370 #endif /* !CONFIG_DYNAMIC_FTRACE */
373 * Hook the return address and push it in the stack of return addrs
374 * in current thread info.
376 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
379 unsigned long long calltime;
381 struct ftrace_graph_ent trace;
382 unsigned long return_hooker = (unsigned long)
385 /* Nmi's are currently unsupported */
386 if (unlikely(in_nmi()))
389 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
393 * Protect against fault, even if it shouldn't
394 * happen. This tool is too much intrusive to
395 * ignore such a protection.
398 "1: " _ASM_MOV " (%[parent]), %[old]\n"
399 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
400 " movl $0, %[faulted]\n"
403 ".section .fixup, \"ax\"\n"
404 "4: movl $1, %[faulted]\n"
411 : [old] "=r" (old), [faulted] "=r" (faulted)
412 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
416 if (unlikely(faulted)) {
422 calltime = cpu_clock(raw_smp_processor_id());
424 if (ftrace_push_return_trace(old, calltime,
425 self_addr, &trace.depth) == -EBUSY) {
430 trace.func = self_addr;
432 /* Only trace if the calling function expects to */
433 if (!ftrace_graph_entry(&trace)) {
434 current->curr_ret_stack--;
438 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */