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/ftrace.h>
15 #include <linux/percpu.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
19 #include <asm/alternative.h>
23 /* Long is fine, even if it is only 4 bytes ;-) */
24 static long *ftrace_nop;
26 union ftrace_code_union {
31 } __attribute__((packed));
34 static int notrace ftrace_calc_offset(long ip, long addr)
36 return (int)(addr - ip);
39 notrace unsigned char *ftrace_nop_replace(void)
41 return (char *)ftrace_nop;
44 notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
46 static union ftrace_code_union calc;
49 calc.offset = ftrace_calc_offset(ip, addr);
52 * No locking needed, this must be called via kstop_machine
53 * which in essence is like running on a uniprocessor machine.
59 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
60 unsigned char *new_code)
63 unsigned old = *(unsigned *)old_code; /* 4 bytes */
64 unsigned new = *(unsigned *)new_code; /* 4 bytes */
65 unsigned char newch = new_code[4];
68 /* move the IP back to the start of the call */
72 * Note: Due to modules and __init, code can
73 * disappear and change, we need to protect against faulting
74 * as well as code changing.
76 * No real locking needed, this code is run through
85 ".section .fixup, \"ax\"\n"
90 : "=r"(faulted), "=a"(replaced)
91 : "r"(ip), "r"(new), "r"(newch),
92 "0"(faulted), "a"(old)
96 if (replaced != old && replaced != new)
102 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
104 unsigned long ip = (unsigned long)(&ftrace_call);
105 unsigned char old[5], *new;
110 memcpy(old, &ftrace_call, 5);
111 new = ftrace_call_replace(ip, (unsigned long)func);
112 ret = ftrace_modify_code(ip, old, new);
117 notrace int ftrace_mcount_set(unsigned long *data)
119 unsigned long ip = (long)(&mcount_call);
120 unsigned long *addr = data;
121 unsigned char old[5], *new;
123 /* ip is at the location, but modify code will subtact this */
127 * Replace the mcount stub with a pointer to the
128 * ip recorder function.
130 memcpy(old, &mcount_call, 5);
131 new = ftrace_call_replace(ip, *addr);
132 *addr = ftrace_modify_code(ip, old, new);
137 int __init ftrace_dyn_arch_init(void *data)
139 const unsigned char *const *noptable = find_nop_table();
141 /* This is running in kstop_machine */
143 ftrace_mcount_set(data);
145 ftrace_nop = (unsigned long *)noptable[CALL_BACK];