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>
19 #include <asm/cacheflush.h>
20 #include <asm/opcodes.h>
21 #include <asm/ftrace.h>
25 #ifdef CONFIG_THUMB2_KERNEL
26 #define NOP 0xf85deb04 /* pop.w {lr} */
28 #define NOP 0xe8bd4000 /* pop {lr} */
31 #ifdef CONFIG_DYNAMIC_FTRACE
32 #ifdef CONFIG_OLD_MCOUNT
33 #define OLD_MCOUNT_ADDR ((unsigned long) mcount)
34 #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
36 #define OLD_NOP 0xe1a00000 /* mov r0, r0 */
38 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
40 return rec->arch.old_mcount ? OLD_NOP : NOP;
43 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
45 if (!rec->arch.old_mcount)
48 if (addr == MCOUNT_ADDR)
49 addr = OLD_MCOUNT_ADDR;
50 else if (addr == FTRACE_ADDR)
51 addr = OLD_FTRACE_ADDR;
56 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
61 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
67 int ftrace_arch_code_modify_prepare(void)
69 set_all_modules_text_rw();
73 int ftrace_arch_code_modify_post_process(void)
75 set_all_modules_text_ro();
79 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
81 return arm_gen_branch_link(pc, addr);
84 static int ftrace_modify_code(unsigned long pc, unsigned long old,
85 unsigned long new, bool validate)
87 unsigned long replaced;
89 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
90 old = __opcode_to_mem_thumb32(old);
91 new = __opcode_to_mem_thumb32(new);
93 old = __opcode_to_mem_arm(old);
94 new = __opcode_to_mem_arm(new);
98 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
105 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
108 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
113 int ftrace_update_ftrace_func(ftrace_func_t func)
119 pc = (unsigned long)&ftrace_call;
120 new = ftrace_call_replace(pc, (unsigned long)func);
122 ret = ftrace_modify_code(pc, 0, new, false);
124 #ifdef CONFIG_OLD_MCOUNT
126 pc = (unsigned long)&ftrace_call_old;
127 new = ftrace_call_replace(pc, (unsigned long)func);
129 ret = ftrace_modify_code(pc, 0, new, false);
136 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
138 unsigned long new, old;
139 unsigned long ip = rec->ip;
141 old = ftrace_nop_replace(rec);
142 new = ftrace_call_replace(ip, adjust_address(rec, addr));
144 return ftrace_modify_code(rec->ip, old, new, true);
147 int ftrace_make_nop(struct module *mod,
148 struct dyn_ftrace *rec, unsigned long addr)
150 unsigned long ip = rec->ip;
155 old = ftrace_call_replace(ip, adjust_address(rec, addr));
156 new = ftrace_nop_replace(rec);
157 ret = ftrace_modify_code(ip, old, new, true);
159 #ifdef CONFIG_OLD_MCOUNT
160 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
161 rec->arch.old_mcount = true;
163 old = ftrace_call_replace(ip, adjust_address(rec, addr));
164 new = ftrace_nop_replace(rec);
165 ret = ftrace_modify_code(ip, old, new, true);
172 int __init ftrace_dyn_arch_init(void)
176 #endif /* CONFIG_DYNAMIC_FTRACE */
178 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
179 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
180 unsigned long frame_pointer)
182 unsigned long return_hooker = (unsigned long) &return_to_handler;
183 struct ftrace_graph_ent trace;
187 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
191 *parent = return_hooker;
193 trace.func = self_addr;
194 trace.depth = current->curr_ret_stack + 1;
196 /* Only trace if the calling function expects to */
197 if (!ftrace_graph_entry(&trace)) {
202 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
210 #ifdef CONFIG_DYNAMIC_FTRACE
211 extern unsigned long ftrace_graph_call;
212 extern unsigned long ftrace_graph_call_old;
213 extern void ftrace_graph_caller_old(void);
215 static int __ftrace_modify_caller(unsigned long *callsite,
216 void (*func) (void), bool enable)
218 unsigned long caller_fn = (unsigned long) func;
219 unsigned long pc = (unsigned long) callsite;
220 unsigned long branch = arm_gen_branch(pc, caller_fn);
221 unsigned long nop = 0xe1a00000; /* mov r0, r0 */
222 unsigned long old = enable ? nop : branch;
223 unsigned long new = enable ? branch : nop;
225 return ftrace_modify_code(pc, old, new, true);
228 static int ftrace_modify_graph_caller(bool enable)
232 ret = __ftrace_modify_caller(&ftrace_graph_call,
236 #ifdef CONFIG_OLD_MCOUNT
238 ret = __ftrace_modify_caller(&ftrace_graph_call_old,
239 ftrace_graph_caller_old,
246 int ftrace_enable_ftrace_graph_caller(void)
248 return ftrace_modify_graph_caller(true);
251 int ftrace_disable_ftrace_graph_caller(void)
253 return ftrace_modify_graph_caller(false);
255 #endif /* CONFIG_DYNAMIC_FTRACE */
256 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */