2 * Dynamic function tracing support.
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>
24 #include <asm/set_memory.h>
25 #include <asm/patch.h>
27 #ifdef CONFIG_THUMB2_KERNEL
28 #define NOP 0xf85deb04 /* pop.w {lr} */
30 #define NOP 0xe8bd4000 /* pop {lr} */
33 #ifdef CONFIG_DYNAMIC_FTRACE
35 static int __ftrace_modify_code(void *data)
39 ftrace_modify_all_code(*command);
44 void arch_ftrace_update_code(int command)
46 stop_machine(__ftrace_modify_code, &command, NULL);
49 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
54 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
59 int ftrace_arch_code_modify_prepare(void)
64 int ftrace_arch_code_modify_post_process(void)
66 /* Make sure any TLB misses during machine stop are cleared. */
71 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr,
74 return arm_gen_branch_link(pc, addr, warn);
77 static int ftrace_modify_code(unsigned long pc, unsigned long old,
78 unsigned long new, bool validate)
80 unsigned long replaced;
82 if (IS_ENABLED(CONFIG_THUMB2_KERNEL))
83 old = __opcode_to_mem_thumb32(old);
85 old = __opcode_to_mem_arm(old);
88 if (copy_from_kernel_nofault(&replaced, (void *)pc,
96 __patch_text((void *)pc, new);
101 int ftrace_update_ftrace_func(ftrace_func_t func)
107 pc = (unsigned long)&ftrace_call;
108 new = ftrace_call_replace(pc, (unsigned long)func, true);
110 ret = ftrace_modify_code(pc, 0, new, false);
112 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
114 pc = (unsigned long)&ftrace_regs_call;
115 new = ftrace_call_replace(pc, (unsigned long)func, true);
117 ret = ftrace_modify_code(pc, 0, new, false);
124 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
126 unsigned long new, old;
127 unsigned long ip = rec->ip;
128 unsigned long aaddr = adjust_address(rec, addr);
129 struct module *mod = NULL;
131 #ifdef CONFIG_ARM_MODULE_PLTS
135 old = ftrace_nop_replace(rec);
137 new = ftrace_call_replace(ip, aaddr, !mod);
138 #ifdef CONFIG_ARM_MODULE_PLTS
140 aaddr = get_module_plt(mod, ip, aaddr);
141 new = ftrace_call_replace(ip, aaddr, true);
145 return ftrace_modify_code(rec->ip, old, new, true);
148 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
150 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
153 unsigned long new, old;
154 unsigned long ip = rec->ip;
156 old = ftrace_call_replace(ip, adjust_address(rec, old_addr), true);
158 new = ftrace_call_replace(ip, adjust_address(rec, addr), true);
160 return ftrace_modify_code(rec->ip, old, new, true);
165 int ftrace_make_nop(struct module *mod,
166 struct dyn_ftrace *rec, unsigned long addr)
168 unsigned long aaddr = adjust_address(rec, addr);
169 unsigned long ip = rec->ip;
174 #ifdef CONFIG_ARM_MODULE_PLTS
175 /* mod is only supplied during module loading */
182 old = ftrace_call_replace(ip, aaddr,
183 !IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || !mod);
184 #ifdef CONFIG_ARM_MODULE_PLTS
186 aaddr = get_module_plt(mod, ip, aaddr);
187 old = ftrace_call_replace(ip, aaddr, true);
191 new = ftrace_nop_replace(rec);
192 ret = ftrace_modify_code(ip, old, new, true);
196 #endif /* CONFIG_DYNAMIC_FTRACE */
198 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
199 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
200 unsigned long frame_pointer)
202 unsigned long return_hooker = (unsigned long) &return_to_handler;
205 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
209 *parent = return_hooker;
211 if (function_graph_enter(old, self_addr, frame_pointer, NULL))
215 #ifdef CONFIG_DYNAMIC_FTRACE
216 extern unsigned long ftrace_graph_call;
217 extern unsigned long ftrace_graph_call_old;
218 extern void ftrace_graph_caller_old(void);
219 extern unsigned long ftrace_graph_regs_call;
220 extern void ftrace_graph_regs_caller(void);
222 static int __ftrace_modify_caller(unsigned long *callsite,
223 void (*func) (void), bool enable)
225 unsigned long caller_fn = (unsigned long) func;
226 unsigned long pc = (unsigned long) callsite;
227 unsigned long branch = arm_gen_branch(pc, caller_fn);
228 unsigned long nop = 0xe1a00000; /* mov r0, r0 */
229 unsigned long old = enable ? nop : branch;
230 unsigned long new = enable ? branch : nop;
232 return ftrace_modify_code(pc, old, new, true);
235 static int ftrace_modify_graph_caller(bool enable)
239 ret = __ftrace_modify_caller(&ftrace_graph_call,
243 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
245 ret = __ftrace_modify_caller(&ftrace_graph_regs_call,
246 ftrace_graph_regs_caller,
254 int ftrace_enable_ftrace_graph_caller(void)
256 return ftrace_modify_graph_caller(true);
259 int ftrace_disable_ftrace_graph_caller(void)
261 return ftrace_modify_graph_caller(false);
263 #endif /* CONFIG_DYNAMIC_FTRACE */
264 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */