2 * Dynamic function tracing support.
4 * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
5 * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
7 * For licencing details, see COPYING.
9 * Defines low-level handling of mcount calls when the kernel
10 * is compiled with the -pg flag. When using dynamic ftrace, the
11 * mcount call-sites get patched with NOP till they are enabled.
12 * All code mutation routines here are called under stop_machine().
15 #include <linux/ftrace.h>
16 #include <linux/uaccess.h>
17 #include <linux/module.h>
18 #include <linux/stop_machine.h>
20 #include <asm/cacheflush.h>
21 #include <asm/opcodes.h>
22 #include <asm/ftrace.h>
26 #ifdef CONFIG_THUMB2_KERNEL
27 #define NOP 0xf85deb04 /* pop.w {lr} */
29 #define NOP 0xe8bd4000 /* pop {lr} */
32 #ifdef CONFIG_DYNAMIC_FTRACE
33 #ifdef CONFIG_OLD_MCOUNT
34 #define OLD_MCOUNT_ADDR ((unsigned long) mcount)
35 #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
37 #define OLD_NOP 0xe1a00000 /* mov r0, r0 */
39 static int __ftrace_modify_code(void *data)
44 ftrace_modify_all_code(*command);
50 void arch_ftrace_update_code(int command)
52 stop_machine(__ftrace_modify_code, &command, NULL);
55 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
57 return rec->arch.old_mcount ? OLD_NOP : NOP;
60 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
62 if (!rec->arch.old_mcount)
65 if (addr == MCOUNT_ADDR)
66 addr = OLD_MCOUNT_ADDR;
67 else if (addr == FTRACE_ADDR)
68 addr = OLD_FTRACE_ADDR;
73 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
78 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
84 int ftrace_arch_code_modify_prepare(void)
86 set_all_modules_text_rw();
90 int ftrace_arch_code_modify_post_process(void)
92 set_all_modules_text_ro();
93 /* Make sure any TLB misses during machine stop are cleared. */
98 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
100 return arm_gen_branch_link(pc, addr);
103 static int ftrace_modify_code(unsigned long pc, unsigned long old,
104 unsigned long new, bool validate)
106 unsigned long replaced;
108 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
109 old = __opcode_to_mem_thumb32(old);
110 new = __opcode_to_mem_thumb32(new);
112 old = __opcode_to_mem_arm(old);
113 new = __opcode_to_mem_arm(new);
117 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
124 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
127 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
132 int ftrace_update_ftrace_func(ftrace_func_t func)
138 pc = (unsigned long)&ftrace_call;
139 new = ftrace_call_replace(pc, (unsigned long)func);
141 ret = ftrace_modify_code(pc, 0, new, false);
143 #ifdef CONFIG_OLD_MCOUNT
145 pc = (unsigned long)&ftrace_call_old;
146 new = ftrace_call_replace(pc, (unsigned long)func);
148 ret = ftrace_modify_code(pc, 0, new, false);
155 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
157 unsigned long new, old;
158 unsigned long ip = rec->ip;
160 old = ftrace_nop_replace(rec);
161 new = ftrace_call_replace(ip, adjust_address(rec, addr));
163 return ftrace_modify_code(rec->ip, old, new, true);
166 int ftrace_make_nop(struct module *mod,
167 struct dyn_ftrace *rec, unsigned long addr)
169 unsigned long ip = rec->ip;
174 old = ftrace_call_replace(ip, adjust_address(rec, addr));
175 new = ftrace_nop_replace(rec);
176 ret = ftrace_modify_code(ip, old, new, true);
178 #ifdef CONFIG_OLD_MCOUNT
179 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
180 rec->arch.old_mcount = true;
182 old = ftrace_call_replace(ip, adjust_address(rec, addr));
183 new = ftrace_nop_replace(rec);
184 ret = ftrace_modify_code(ip, old, new, true);
191 int __init ftrace_dyn_arch_init(void)
195 #endif /* CONFIG_DYNAMIC_FTRACE */
197 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
198 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
199 unsigned long frame_pointer)
201 unsigned long return_hooker = (unsigned long) &return_to_handler;
202 struct ftrace_graph_ent trace;
206 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
210 *parent = return_hooker;
212 trace.func = self_addr;
213 trace.depth = current->curr_ret_stack + 1;
215 /* Only trace if the calling function expects to */
216 if (!ftrace_graph_entry(&trace)) {
221 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
229 #ifdef CONFIG_DYNAMIC_FTRACE
230 extern unsigned long ftrace_graph_call;
231 extern unsigned long ftrace_graph_call_old;
232 extern void ftrace_graph_caller_old(void);
234 static int __ftrace_modify_caller(unsigned long *callsite,
235 void (*func) (void), bool enable)
237 unsigned long caller_fn = (unsigned long) func;
238 unsigned long pc = (unsigned long) callsite;
239 unsigned long branch = arm_gen_branch(pc, caller_fn);
240 unsigned long nop = 0xe1a00000; /* mov r0, r0 */
241 unsigned long old = enable ? nop : branch;
242 unsigned long new = enable ? branch : nop;
244 return ftrace_modify_code(pc, old, new, true);
247 static int ftrace_modify_graph_caller(bool enable)
251 ret = __ftrace_modify_caller(&ftrace_graph_call,
255 #ifdef CONFIG_OLD_MCOUNT
257 ret = __ftrace_modify_caller(&ftrace_graph_call_old,
258 ftrace_graph_caller_old,
265 int ftrace_enable_ftrace_graph_caller(void)
267 return ftrace_modify_graph_caller(true);
270 int ftrace_disable_ftrace_graph_caller(void)
272 return ftrace_modify_graph_caller(false);
274 #endif /* CONFIG_DYNAMIC_FTRACE */
275 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */