xref: /openbmc/linux/arch/arm/kernel/ftrace.c (revision 63705da3)
1 /*
2  * Dynamic function tracing support.
3  *
4  * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
5  * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
6  *
7  * For licencing details, see COPYING.
8  *
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().
13  */
14 
15 #include <linux/ftrace.h>
16 #include <linux/uaccess.h>
17 #include <linux/module.h>
18 #include <linux/stop_machine.h>
19 
20 #include <asm/cacheflush.h>
21 #include <asm/opcodes.h>
22 #include <asm/ftrace.h>
23 #include <asm/insn.h>
24 #include <asm/set_memory.h>
25 #include <asm/patch.h>
26 
27 #ifdef CONFIG_THUMB2_KERNEL
28 #define	NOP		0xf85deb04	/* pop.w {lr} */
29 #else
30 #define	NOP		0xe8bd4000	/* pop {lr} */
31 #endif
32 
33 #ifdef CONFIG_DYNAMIC_FTRACE
34 
35 static int __ftrace_modify_code(void *data)
36 {
37 	int *command = data;
38 
39 	ftrace_modify_all_code(*command);
40 
41 	return 0;
42 }
43 
44 void arch_ftrace_update_code(int command)
45 {
46 	stop_machine(__ftrace_modify_code, &command, NULL);
47 }
48 
49 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
50 {
51 	return NOP;
52 }
53 
54 static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
55 {
56 	return addr;
57 }
58 
59 int ftrace_arch_code_modify_prepare(void)
60 {
61 	return 0;
62 }
63 
64 int ftrace_arch_code_modify_post_process(void)
65 {
66 	/* Make sure any TLB misses during machine stop are cleared. */
67 	flush_tlb_all();
68 	return 0;
69 }
70 
71 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr,
72 					 bool warn)
73 {
74 	return arm_gen_branch_link(pc, addr, warn);
75 }
76 
77 static int ftrace_modify_code(unsigned long pc, unsigned long old,
78 			      unsigned long new, bool validate)
79 {
80 	unsigned long replaced;
81 
82 	if (IS_ENABLED(CONFIG_THUMB2_KERNEL))
83 		old = __opcode_to_mem_thumb32(old);
84 	else
85 		old = __opcode_to_mem_arm(old);
86 
87 	if (validate) {
88 		if (copy_from_kernel_nofault(&replaced, (void *)pc,
89 				MCOUNT_INSN_SIZE))
90 			return -EFAULT;
91 
92 		if (replaced != old)
93 			return -EINVAL;
94 	}
95 
96 	__patch_text((void *)pc, new);
97 
98 	return 0;
99 }
100 
101 int ftrace_update_ftrace_func(ftrace_func_t func)
102 {
103 	unsigned long pc;
104 	unsigned long new;
105 	int ret;
106 
107 	pc = (unsigned long)&ftrace_call;
108 	new = ftrace_call_replace(pc, (unsigned long)func, true);
109 
110 	ret = ftrace_modify_code(pc, 0, new, false);
111 
112 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
113 	if (!ret) {
114 		pc = (unsigned long)&ftrace_regs_call;
115 		new = ftrace_call_replace(pc, (unsigned long)func, true);
116 
117 		ret = ftrace_modify_code(pc, 0, new, false);
118 	}
119 #endif
120 
121 	return ret;
122 }
123 
124 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
125 {
126 	unsigned long new, old;
127 	unsigned long ip = rec->ip;
128 	unsigned long aaddr = adjust_address(rec, addr);
129 	struct module *mod = NULL;
130 
131 #ifdef CONFIG_ARM_MODULE_PLTS
132 	mod = rec->arch.mod;
133 #endif
134 
135 	old = ftrace_nop_replace(rec);
136 
137 	new = ftrace_call_replace(ip, aaddr, !mod);
138 #ifdef CONFIG_ARM_MODULE_PLTS
139 	if (!new && mod) {
140 		aaddr = get_module_plt(mod, ip, aaddr);
141 		new = ftrace_call_replace(ip, aaddr, true);
142 	}
143 #endif
144 
145 	return ftrace_modify_code(rec->ip, old, new, true);
146 }
147 
148 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
149 
150 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
151 				unsigned long addr)
152 {
153 	unsigned long new, old;
154 	unsigned long ip = rec->ip;
155 
156 	old = ftrace_call_replace(ip, adjust_address(rec, old_addr), true);
157 
158 	new = ftrace_call_replace(ip, adjust_address(rec, addr), true);
159 
160 	return ftrace_modify_code(rec->ip, old, new, true);
161 }
162 
163 #endif
164 
165 int ftrace_make_nop(struct module *mod,
166 		    struct dyn_ftrace *rec, unsigned long addr)
167 {
168 	unsigned long aaddr = adjust_address(rec, addr);
169 	unsigned long ip = rec->ip;
170 	unsigned long old;
171 	unsigned long new;
172 	int ret;
173 
174 #ifdef CONFIG_ARM_MODULE_PLTS
175 	/* mod is only supplied during module loading */
176 	if (!mod)
177 		mod = rec->arch.mod;
178 	else
179 		rec->arch.mod = mod;
180 #endif
181 
182 	old = ftrace_call_replace(ip, aaddr,
183 				  !IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || !mod);
184 #ifdef CONFIG_ARM_MODULE_PLTS
185 	if (!old && mod) {
186 		aaddr = get_module_plt(mod, ip, aaddr);
187 		old = ftrace_call_replace(ip, aaddr, true);
188 	}
189 #endif
190 
191 	new = ftrace_nop_replace(rec);
192 	ret = ftrace_modify_code(ip, old, new, true);
193 
194 	return ret;
195 }
196 #endif /* CONFIG_DYNAMIC_FTRACE */
197 
198 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
199 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
200 			   unsigned long frame_pointer)
201 {
202 	unsigned long return_hooker = (unsigned long) &return_to_handler;
203 	unsigned long old;
204 
205 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
206 		return;
207 
208 	old = *parent;
209 	*parent = return_hooker;
210 
211 	if (function_graph_enter(old, self_addr, frame_pointer, NULL))
212 		*parent = old;
213 }
214 
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);
221 
222 static int __ftrace_modify_caller(unsigned long *callsite,
223 				  void (*func) (void), bool enable)
224 {
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;
231 
232 	return ftrace_modify_code(pc, old, new, true);
233 }
234 
235 static int ftrace_modify_graph_caller(bool enable)
236 {
237 	int ret;
238 
239 	ret = __ftrace_modify_caller(&ftrace_graph_call,
240 				     ftrace_graph_caller,
241 				     enable);
242 
243 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
244 	if (!ret)
245 		ret = __ftrace_modify_caller(&ftrace_graph_regs_call,
246 				     ftrace_graph_regs_caller,
247 				     enable);
248 #endif
249 
250 
251 	return ret;
252 }
253 
254 int ftrace_enable_ftrace_graph_caller(void)
255 {
256 	return ftrace_modify_graph_caller(true);
257 }
258 
259 int ftrace_disable_ftrace_graph_caller(void)
260 {
261 	return ftrace_modify_graph_caller(false);
262 }
263 #endif /* CONFIG_DYNAMIC_FTRACE */
264 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
265