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/module.h>
17 #include <linux/uaccess.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)
70 set_all_modules_text_rw();
74 int ftrace_arch_code_modify_post_process(void)
76 set_all_modules_text_ro();
81 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
83 return arm_gen_branch_link(pc, addr);
86 static int ftrace_modify_code(unsigned long pc, unsigned long old,
87 unsigned long new, bool validate)
89 unsigned long replaced;
91 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
92 old = __opcode_to_mem_thumb32(old);
93 new = __opcode_to_mem_thumb32(new);
95 old = __opcode_to_mem_arm(old);
96 new = __opcode_to_mem_arm(new);
100 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
107 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
110 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
115 int ftrace_update_ftrace_func(ftrace_func_t func)
121 pc = (unsigned long)&ftrace_call;
122 new = ftrace_call_replace(pc, (unsigned long)func);
124 ret = ftrace_modify_code(pc, 0, new, false);
126 #ifdef CONFIG_OLD_MCOUNT
128 pc = (unsigned long)&ftrace_call_old;
129 new = ftrace_call_replace(pc, (unsigned long)func);
131 ret = ftrace_modify_code(pc, 0, new, false);
138 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
140 unsigned long new, old;
141 unsigned long ip = rec->ip;
143 old = ftrace_nop_replace(rec);
144 new = ftrace_call_replace(ip, adjust_address(rec, addr));
146 return ftrace_modify_code(rec->ip, old, new, true);
149 int ftrace_make_nop(struct module *mod,
150 struct dyn_ftrace *rec, unsigned long addr)
152 unsigned long ip = rec->ip;
157 old = ftrace_call_replace(ip, adjust_address(rec, addr));
158 new = ftrace_nop_replace(rec);
159 ret = ftrace_modify_code(ip, old, new, true);
161 #ifdef CONFIG_OLD_MCOUNT
162 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
163 rec->arch.old_mcount = true;
165 old = ftrace_call_replace(ip, adjust_address(rec, addr));
166 new = ftrace_nop_replace(rec);
167 ret = ftrace_modify_code(ip, old, new, true);
174 int __init ftrace_dyn_arch_init(void *data)
176 *(unsigned long *)data = 0;
180 #endif /* CONFIG_DYNAMIC_FTRACE */
182 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
183 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
184 unsigned long frame_pointer)
186 unsigned long return_hooker = (unsigned long) &return_to_handler;
187 struct ftrace_graph_ent trace;
191 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
195 *parent = return_hooker;
197 trace.func = self_addr;
198 trace.depth = current->curr_ret_stack + 1;
200 /* Only trace if the calling function expects to */
201 if (!ftrace_graph_entry(&trace)) {
206 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
214 #ifdef CONFIG_DYNAMIC_FTRACE
215 extern unsigned long ftrace_graph_call;
216 extern unsigned long ftrace_graph_call_old;
217 extern void ftrace_graph_caller_old(void);
219 static int __ftrace_modify_caller(unsigned long *callsite,
220 void (*func) (void), bool enable)
222 unsigned long caller_fn = (unsigned long) func;
223 unsigned long pc = (unsigned long) callsite;
224 unsigned long branch = arm_gen_branch(pc, caller_fn);
225 unsigned long nop = 0xe1a00000; /* mov r0, r0 */
226 unsigned long old = enable ? nop : branch;
227 unsigned long new = enable ? branch : nop;
229 return ftrace_modify_code(pc, old, new, true);
232 static int ftrace_modify_graph_caller(bool enable)
236 ret = __ftrace_modify_caller(&ftrace_graph_call,
240 #ifdef CONFIG_OLD_MCOUNT
242 ret = __ftrace_modify_caller(&ftrace_graph_call_old,
243 ftrace_graph_caller_old,
250 int ftrace_enable_ftrace_graph_caller(void)
252 return ftrace_modify_graph_caller(true);
255 int ftrace_disable_ftrace_graph_caller(void)
257 return ftrace_modify_graph_caller(false);
259 #endif /* CONFIG_DYNAMIC_FTRACE */
260 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */