Commit | Line | Data |
---|---|---|
014c257c AS |
1 | /* |
2 | * Dynamic function tracing support. | |
3 | * | |
4 | * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com> | |
3b6c223b | 5 | * Copyright (C) 2010 Rabin Vincent <rabin@rab.in> |
014c257c AS |
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 | |
3b6c223b RV |
11 | * mcount call-sites get patched with NOP till they are enabled. |
12 | * All code mutation routines here are called under stop_machine(). | |
014c257c AS |
13 | */ |
14 | ||
15 | #include <linux/ftrace.h> | |
3b6c223b | 16 | #include <linux/uaccess.h> |
a672917a | 17 | #include <linux/module.h> |
395a59d0 | 18 | |
014c257c | 19 | #include <asm/cacheflush.h> |
4394e282 | 20 | #include <asm/opcodes.h> |
395a59d0 | 21 | #include <asm/ftrace.h> |
014c257c | 22 | |
d82227cf RV |
23 | #include "insn.h" |
24 | ||
72dc43a9 | 25 | #ifdef CONFIG_THUMB2_KERNEL |
4394e282 | 26 | #define NOP 0xf85deb04 /* pop.w {lr} */ |
72dc43a9 | 27 | #else |
3b6c223b | 28 | #define NOP 0xe8bd4000 /* pop {lr} */ |
72dc43a9 | 29 | #endif |
014c257c | 30 | |
376cfa87 | 31 | #ifdef CONFIG_DYNAMIC_FTRACE |
3b6c223b RV |
32 | #ifdef CONFIG_OLD_MCOUNT |
33 | #define OLD_MCOUNT_ADDR ((unsigned long) mcount) | |
34 | #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old) | |
014c257c | 35 | |
3b6c223b RV |
36 | #define OLD_NOP 0xe1a00000 /* mov r0, r0 */ |
37 | ||
38 | static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec) | |
39 | { | |
40 | return rec->arch.old_mcount ? OLD_NOP : NOP; | |
41 | } | |
42 | ||
43 | static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr) | |
44 | { | |
45 | if (!rec->arch.old_mcount) | |
46 | return addr; | |
47 | ||
48 | if (addr == MCOUNT_ADDR) | |
49 | addr = OLD_MCOUNT_ADDR; | |
50 | else if (addr == FTRACE_ADDR) | |
51 | addr = OLD_FTRACE_ADDR; | |
52 | ||
53 | return addr; | |
54 | } | |
55 | #else | |
56 | static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec) | |
57 | { | |
58 | return NOP; | |
59 | } | |
60 | ||
61 | static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr) | |
014c257c | 62 | { |
3b6c223b | 63 | return addr; |
014c257c | 64 | } |
3b6c223b | 65 | #endif |
014c257c | 66 | |
a672917a RV |
67 | int ftrace_arch_code_modify_prepare(void) |
68 | { | |
69 | set_all_modules_text_rw(); | |
70 | return 0; | |
71 | } | |
72 | ||
73 | int ftrace_arch_code_modify_post_process(void) | |
74 | { | |
75 | set_all_modules_text_ro(); | |
76 | return 0; | |
77 | } | |
78 | ||
dd686eb1 RV |
79 | static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) |
80 | { | |
d82227cf | 81 | return arm_gen_branch_link(pc, addr); |
dd686eb1 RV |
82 | } |
83 | ||
3b6c223b | 84 | static int ftrace_modify_code(unsigned long pc, unsigned long old, |
dc283d70 | 85 | unsigned long new, bool validate) |
3b6c223b RV |
86 | { |
87 | unsigned long replaced; | |
014c257c | 88 | |
4394e282 RV |
89 | if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { |
90 | old = __opcode_to_mem_thumb32(old); | |
91 | new = __opcode_to_mem_thumb32(new); | |
92 | } else { | |
93 | old = __opcode_to_mem_arm(old); | |
94 | new = __opcode_to_mem_arm(new); | |
95 | } | |
96 | ||
dc283d70 RV |
97 | if (validate) { |
98 | if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE)) | |
99 | return -EFAULT; | |
014c257c | 100 | |
dc283d70 RV |
101 | if (replaced != old) |
102 | return -EINVAL; | |
103 | } | |
014c257c | 104 | |
3b6c223b RV |
105 | if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE)) |
106 | return -EPERM; | |
014c257c | 107 | |
3b6c223b | 108 | flush_icache_range(pc, pc + MCOUNT_INSN_SIZE); |
014c257c | 109 | |
3b6c223b | 110 | return 0; |
014c257c AS |
111 | } |
112 | ||
113 | int ftrace_update_ftrace_func(ftrace_func_t func) | |
114 | { | |
dc283d70 | 115 | unsigned long pc; |
3b6c223b RV |
116 | unsigned long new; |
117 | int ret; | |
014c257c AS |
118 | |
119 | pc = (unsigned long)&ftrace_call; | |
014c257c | 120 | new = ftrace_call_replace(pc, (unsigned long)func); |
3b6c223b | 121 | |
dc283d70 | 122 | ret = ftrace_modify_code(pc, 0, new, false); |
3b6c223b RV |
123 | |
124 | #ifdef CONFIG_OLD_MCOUNT | |
125 | if (!ret) { | |
126 | pc = (unsigned long)&ftrace_call_old; | |
3b6c223b RV |
127 | new = ftrace_call_replace(pc, (unsigned long)func); |
128 | ||
dc283d70 | 129 | ret = ftrace_modify_code(pc, 0, new, false); |
3b6c223b RV |
130 | } |
131 | #endif | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) | |
137 | { | |
138 | unsigned long new, old; | |
139 | unsigned long ip = rec->ip; | |
140 | ||
141 | old = ftrace_nop_replace(rec); | |
142 | new = ftrace_call_replace(ip, adjust_address(rec, addr)); | |
143 | ||
dc283d70 | 144 | return ftrace_modify_code(rec->ip, old, new, true); |
3b6c223b RV |
145 | } |
146 | ||
147 | int ftrace_make_nop(struct module *mod, | |
148 | struct dyn_ftrace *rec, unsigned long addr) | |
149 | { | |
150 | unsigned long ip = rec->ip; | |
151 | unsigned long old; | |
152 | unsigned long new; | |
153 | int ret; | |
154 | ||
155 | old = ftrace_call_replace(ip, adjust_address(rec, addr)); | |
156 | new = ftrace_nop_replace(rec); | |
dc283d70 | 157 | ret = ftrace_modify_code(ip, old, new, true); |
3b6c223b RV |
158 | |
159 | #ifdef CONFIG_OLD_MCOUNT | |
160 | if (ret == -EINVAL && addr == MCOUNT_ADDR) { | |
161 | rec->arch.old_mcount = true; | |
162 | ||
163 | old = ftrace_call_replace(ip, adjust_address(rec, addr)); | |
164 | new = ftrace_nop_replace(rec); | |
dc283d70 | 165 | ret = ftrace_modify_code(ip, old, new, true); |
3b6c223b RV |
166 | } |
167 | #endif | |
168 | ||
014c257c AS |
169 | return ret; |
170 | } | |
171 | ||
3a36cb11 | 172 | int __init ftrace_dyn_arch_init(void) |
014c257c | 173 | { |
014c257c AS |
174 | return 0; |
175 | } | |
376cfa87 TB |
176 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
177 | ||
178 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
179 | void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, | |
180 | unsigned long frame_pointer) | |
181 | { | |
182 | unsigned long return_hooker = (unsigned long) &return_to_handler; | |
183 | struct ftrace_graph_ent trace; | |
184 | unsigned long old; | |
185 | int err; | |
186 | ||
187 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) | |
188 | return; | |
189 | ||
190 | old = *parent; | |
191 | *parent = return_hooker; | |
192 | ||
376cfa87 | 193 | trace.func = self_addr; |
4c36595e | 194 | trace.depth = current->curr_ret_stack + 1; |
376cfa87 TB |
195 | |
196 | /* Only trace if the calling function expects to */ | |
197 | if (!ftrace_graph_entry(&trace)) { | |
376cfa87 | 198 | *parent = old; |
4c36595e CC |
199 | return; |
200 | } | |
201 | ||
202 | err = ftrace_push_return_trace(old, self_addr, &trace.depth, | |
203 | frame_pointer); | |
204 | if (err == -EBUSY) { | |
205 | *parent = old; | |
206 | return; | |
376cfa87 TB |
207 | } |
208 | } | |
dd686eb1 RV |
209 | |
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); | |
214 | ||
215 | static int __ftrace_modify_caller(unsigned long *callsite, | |
216 | void (*func) (void), bool enable) | |
217 | { | |
218 | unsigned long caller_fn = (unsigned long) func; | |
219 | unsigned long pc = (unsigned long) callsite; | |
d82227cf | 220 | unsigned long branch = arm_gen_branch(pc, caller_fn); |
dd686eb1 RV |
221 | unsigned long nop = 0xe1a00000; /* mov r0, r0 */ |
222 | unsigned long old = enable ? nop : branch; | |
223 | unsigned long new = enable ? branch : nop; | |
224 | ||
dc283d70 | 225 | return ftrace_modify_code(pc, old, new, true); |
dd686eb1 RV |
226 | } |
227 | ||
228 | static int ftrace_modify_graph_caller(bool enable) | |
229 | { | |
230 | int ret; | |
231 | ||
232 | ret = __ftrace_modify_caller(&ftrace_graph_call, | |
233 | ftrace_graph_caller, | |
234 | enable); | |
235 | ||
236 | #ifdef CONFIG_OLD_MCOUNT | |
237 | if (!ret) | |
238 | ret = __ftrace_modify_caller(&ftrace_graph_call_old, | |
239 | ftrace_graph_caller_old, | |
240 | enable); | |
241 | #endif | |
242 | ||
243 | return ret; | |
244 | } | |
245 | ||
246 | int ftrace_enable_ftrace_graph_caller(void) | |
247 | { | |
248 | return ftrace_modify_graph_caller(true); | |
249 | } | |
250 | ||
251 | int ftrace_disable_ftrace_graph_caller(void) | |
252 | { | |
253 | return ftrace_modify_graph_caller(false); | |
254 | } | |
255 | #endif /* CONFIG_DYNAMIC_FTRACE */ | |
376cfa87 | 256 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |