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/cacheflush.h>
22 #include <asm/ftrace.h>
27 #ifdef CONFIG_DYNAMIC_FTRACE
29 int ftrace_arch_code_modify_prepare(void)
35 int ftrace_arch_code_modify_post_process(void)
41 union ftrace_code_union {
42 char code[MCOUNT_INSN_SIZE];
46 } __attribute__((packed));
49 static int ftrace_calc_offset(long ip, long addr)
51 return (int)(addr - ip);
54 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
56 static union ftrace_code_union calc;
59 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
62 * No locking needed, this must be called via kstop_machine
63 * which in essence is like running on a uniprocessor machine.
69 * Modifying code must take extra care. On an SMP machine, if
70 * the code being modified is also being executed on another CPU
71 * that CPU will have undefined results and possibly take a GPF.
72 * We use kstop_machine to stop other CPUS from exectuing code.
73 * But this does not stop NMIs from happening. We still need
74 * to protect against that. We separate out the modification of
75 * the code to take care of this.
77 * Two buffers are added: An IP buffer and a "code" buffer.
79 * 1) Put the instruction pointer into the IP buffer
80 * and the new code into the "code" buffer.
81 * 2) Wait for any running NMIs to finish and set a flag that says
82 * we are modifying code, it is done in an atomic operation.
85 * 5) Wait for any running NMIs to finish.
87 * If an NMI is executed, the first thing it does is to call
88 * "ftrace_nmi_enter". This will check if the flag is set to write
89 * and if it is, it will write what is in the IP and "code" buffers.
91 * The trick is, it does not matter if everyone is writing the same
92 * content to the code location. Also, if a CPU is executing code
93 * it is OK to write to that code location if the contents being written
94 * are the same as what exists.
97 #define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
98 static atomic_t nmi_running = ATOMIC_INIT(0);
99 static int mod_code_status; /* holds return value of text write */
100 static void *mod_code_ip; /* holds the IP to write to */
101 static void *mod_code_newcode; /* holds the text to write to the IP */
103 static unsigned nmi_wait_count;
104 static atomic_t nmi_update_count = ATOMIC_INIT(0);
106 int ftrace_arch_read_dyn_info(char *buf, int size)
110 r = snprintf(buf, size, "%u %u",
112 atomic_read(&nmi_update_count));
116 static void clear_mod_flag(void)
118 int old = atomic_read(&nmi_running);
121 int new = old & ~MOD_CODE_WRITE_FLAG;
126 old = atomic_cmpxchg(&nmi_running, old, new);
130 static void ftrace_mod_code(void)
133 * Yes, more than one CPU process can be writing to mod_code_status.
134 * (and the code itself)
135 * But if one were to fail, then they all should, and if one were
136 * to succeed, then they all should.
138 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
141 /* if we fail, then kill any new writers */
146 void ftrace_nmi_enter(void)
148 if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
151 atomic_inc(&nmi_update_count);
153 /* Must have previous changes seen before executions */
157 void ftrace_nmi_exit(void)
159 /* Finish all executions before clearing nmi_running */
161 atomic_dec(&nmi_running);
164 static void wait_for_nmi_and_set_mod_flag(void)
166 if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
171 } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
176 static void wait_for_nmi(void)
178 if (!atomic_read(&nmi_running))
183 } while (atomic_read(&nmi_running));
189 do_ftrace_mod_code(unsigned long ip, void *new_code)
191 mod_code_ip = (void *)ip;
192 mod_code_newcode = new_code;
194 /* The buffers need to be visible before we let NMIs write them */
197 wait_for_nmi_and_set_mod_flag();
199 /* Make sure all running NMIs have finished before we write the code */
204 /* Make sure the write happens before clearing the bit */
210 return mod_code_status;
216 static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
218 static unsigned char *ftrace_nop_replace(void)
224 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
225 unsigned char *new_code)
227 unsigned char replaced[MCOUNT_INSN_SIZE];
230 * Note: Due to modules and __init, code can
231 * disappear and change, we need to protect against faulting
232 * as well as code changing. We do this by using the
233 * probe_kernel_* functions.
235 * No real locking needed, this code is run through
236 * kstop_machine, or before SMP starts.
239 /* read the text we want to modify */
240 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
243 /* Make sure it is what we expect it to be */
244 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
247 /* replace the text with the new text */
248 if (do_ftrace_mod_code(ip, new_code))
256 int ftrace_make_nop(struct module *mod,
257 struct dyn_ftrace *rec, unsigned long addr)
259 unsigned char *new, *old;
260 unsigned long ip = rec->ip;
262 old = ftrace_call_replace(ip, addr);
263 new = ftrace_nop_replace();
265 return ftrace_modify_code(rec->ip, old, new);
268 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
270 unsigned char *new, *old;
271 unsigned long ip = rec->ip;
273 old = ftrace_nop_replace();
274 new = ftrace_call_replace(ip, addr);
276 return ftrace_modify_code(rec->ip, old, new);
279 int ftrace_update_ftrace_func(ftrace_func_t func)
281 unsigned long ip = (unsigned long)(&ftrace_call);
282 unsigned char old[MCOUNT_INSN_SIZE], *new;
285 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
286 new = ftrace_call_replace(ip, (unsigned long)func);
287 ret = ftrace_modify_code(ip, old, new);
292 int __init ftrace_dyn_arch_init(void *data)
294 extern const unsigned char ftrace_test_p6nop[];
295 extern const unsigned char ftrace_test_nop5[];
296 extern const unsigned char ftrace_test_jmp[];
300 * There is no good nop for all x86 archs.
301 * We will default to using the P6_NOP5, but first we
302 * will test to make sure that the nop will actually
303 * work on this CPU. If it faults, we will then
304 * go to a lesser efficient 5 byte nop. If that fails
305 * we then just use a jmp as our nop. This isn't the most
306 * efficient nop, but we can not use a multi part nop
307 * since we would then risk being preempted in the middle
308 * of that nop, and if we enabled tracing then, it might
309 * cause a system crash.
311 * TODO: check the cpuid to determine the best nop.
315 "jmp ftrace_test_p6nop\n"
318 "nop\n" /* 2 byte jmp + 3 bytes */
323 ".byte 0x66,0x66,0x66,0x66,0x90\n"
325 ".section .fixup, \"ax\"\n"
327 " jmp ftrace_test_nop5\n"
331 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
332 _ASM_EXTABLE(ftrace_test_nop5, 3b)
333 : "=r"(faulted) : "0" (faulted));
337 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
338 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
341 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
342 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
345 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
346 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
350 /* The return code is retured via data */
351 *(unsigned long *)data = 0;
357 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
359 #ifdef CONFIG_DYNAMIC_FTRACE
360 extern void ftrace_graph_call(void);
362 static int ftrace_mod_jmp(unsigned long ip,
363 int old_offset, int new_offset)
365 unsigned char code[MCOUNT_INSN_SIZE];
367 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
370 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
373 *(int *)(&code[1]) = new_offset;
375 if (do_ftrace_mod_code(ip, &code))
381 int ftrace_enable_ftrace_graph_caller(void)
383 unsigned long ip = (unsigned long)(&ftrace_graph_call);
384 int old_offset, new_offset;
386 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
387 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
389 return ftrace_mod_jmp(ip, old_offset, new_offset);
392 int ftrace_disable_ftrace_graph_caller(void)
394 unsigned long ip = (unsigned long)(&ftrace_graph_call);
395 int old_offset, new_offset;
397 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
398 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
400 return ftrace_mod_jmp(ip, old_offset, new_offset);
403 #endif /* !CONFIG_DYNAMIC_FTRACE */
406 * Hook the return address and push it in the stack of return addrs
407 * in current thread info.
409 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
413 struct ftrace_graph_ent trace;
414 unsigned long return_hooker = (unsigned long)
417 /* Nmi's are currently unsupported */
418 if (unlikely(in_nmi()))
421 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
425 * Protect against fault, even if it shouldn't
426 * happen. This tool is too much intrusive to
427 * ignore such a protection.
430 "1: " _ASM_MOV " (%[parent]), %[old]\n"
431 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
432 " movl $0, %[faulted]\n"
435 ".section .fixup, \"ax\"\n"
436 "4: movl $1, %[faulted]\n"
443 : [old] "=r" (old), [faulted] "=r" (faulted)
444 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
448 if (unlikely(faulted)) {
454 if (ftrace_push_return_trace(old, self_addr, &trace.depth) == -EBUSY) {
459 trace.func = self_addr;
461 /* Only trace if the calling function expects to */
462 if (!ftrace_graph_entry(&trace)) {
463 current->curr_ret_stack--;
467 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
469 #ifdef CONFIG_FTRACE_SYSCALLS
471 extern unsigned long __start_syscalls_metadata[];
472 extern unsigned long __stop_syscalls_metadata[];
473 extern unsigned long *sys_call_table;
475 static struct syscall_metadata **syscalls_metadata;
477 static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
479 struct syscall_metadata *start;
480 struct syscall_metadata *stop;
481 char str[KSYM_SYMBOL_LEN];
484 start = (struct syscall_metadata *)__start_syscalls_metadata;
485 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
486 kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
488 for ( ; start < stop; start++) {
489 if (start->name && !strcmp(start->name, str))
495 struct syscall_metadata *syscall_nr_to_meta(int nr)
497 if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
500 return syscalls_metadata[nr];
503 void arch_init_ftrace_syscalls(void)
506 struct syscall_metadata *meta;
507 unsigned long **psys_syscall_table = &sys_call_table;
508 static atomic_t refs;
510 if (atomic_inc_return(&refs) != 1)
513 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
514 FTRACE_SYSCALL_MAX, GFP_KERNEL);
515 if (!syscalls_metadata) {
520 for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
521 meta = find_syscall_meta(psys_syscall_table[i]);
522 syscalls_metadata[i] = meta;
526 /* Paranoid: avoid overflow */