blob: 5c9cecfaeb21f529c566b0d085b57987bf0c1807 [file] [log] [blame]
Abhishek Sagar014c2572008-05-31 14:23:50 +05301/*
2 * Dynamic function tracing support.
3 *
4 * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com>
Rabin Vincent3b6c2232010-08-10 19:43:28 +01005 * Copyright (C) 2010 Rabin Vincent <rabin@rab.in>
Abhishek Sagar014c2572008-05-31 14:23:50 +05306 *
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
Rabin Vincent3b6c2232010-08-10 19:43:28 +010011 * mcount call-sites get patched with NOP till they are enabled.
12 * All code mutation routines here are called under stop_machine().
Abhishek Sagar014c2572008-05-31 14:23:50 +053013 */
14
15#include <linux/ftrace.h>
Rabin Vincent3b6c2232010-08-10 19:43:28 +010016#include <linux/uaccess.h>
Abhishek Sagar014c2572008-05-31 14:23:50 +053017
Abhishek Sagar395a59d2008-06-21 23:47:27 +053018#include <asm/cacheflush.h>
Rabin Vincent4394e282012-02-18 17:47:03 +010019#include <asm/opcodes.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053020#include <asm/ftrace.h>
21
Rabin Vincent72dc43a2010-08-10 19:52:35 +010022#ifdef CONFIG_THUMB2_KERNEL
Rabin Vincent4394e282012-02-18 17:47:03 +010023#define NOP 0xf85deb04 /* pop.w {lr} */
Rabin Vincent72dc43a2010-08-10 19:52:35 +010024#else
Rabin Vincent3b6c2232010-08-10 19:43:28 +010025#define NOP 0xe8bd4000 /* pop {lr} */
Rabin Vincent72dc43a2010-08-10 19:52:35 +010026#endif
Abhishek Sagar014c2572008-05-31 14:23:50 +053027
Tim Bird376cfa82010-10-09 22:24:38 +053028#ifdef CONFIG_DYNAMIC_FTRACE
Rabin Vincent3b6c2232010-08-10 19:43:28 +010029#ifdef CONFIG_OLD_MCOUNT
30#define OLD_MCOUNT_ADDR ((unsigned long) mcount)
31#define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old)
Abhishek Sagar014c2572008-05-31 14:23:50 +053032
Rabin Vincent3b6c2232010-08-10 19:43:28 +010033#define OLD_NOP 0xe1a00000 /* mov r0, r0 */
34
35static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
Abhishek Sagar014c2572008-05-31 14:23:50 +053036{
Rabin Vincent3b6c2232010-08-10 19:43:28 +010037 return rec->arch.old_mcount ? OLD_NOP : NOP;
Abhishek Sagar014c2572008-05-31 14:23:50 +053038}
39
Rabin Vincent3b6c2232010-08-10 19:43:28 +010040static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
41{
42 if (!rec->arch.old_mcount)
43 return addr;
44
45 if (addr == MCOUNT_ADDR)
46 addr = OLD_MCOUNT_ADDR;
47 else if (addr == FTRACE_ADDR)
48 addr = OLD_FTRACE_ADDR;
49
50 return addr;
51}
52#else
53static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
54{
55 return NOP;
56}
57
58static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
59{
60 return addr;
61}
62#endif
63
Rabin Vincent72dc43a2010-08-10 19:52:35 +010064#ifdef CONFIG_THUMB2_KERNEL
Rabin Vincentdd686eb2010-11-06 23:03:21 +053065static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
66 bool link)
Rabin Vincent72dc43a2010-08-10 19:52:35 +010067{
68 unsigned long s, j1, j2, i1, i2, imm10, imm11;
69 unsigned long first, second;
70 long offset;
71
72 offset = (long)addr - (long)(pc + 4);
73 if (offset < -16777216 || offset > 16777214) {
74 WARN_ON_ONCE(1);
75 return 0;
76 }
77
78 s = (offset >> 24) & 0x1;
79 i1 = (offset >> 23) & 0x1;
80 i2 = (offset >> 22) & 0x1;
81 imm10 = (offset >> 12) & 0x3ff;
82 imm11 = (offset >> 1) & 0x7ff;
83
84 j1 = (!i1) ^ s;
85 j2 = (!i2) ^ s;
86
87 first = 0xf000 | (s << 10) | imm10;
Rabin Vincentdd686eb2010-11-06 23:03:21 +053088 second = 0x9000 | (j1 << 13) | (j2 << 11) | imm11;
89 if (link)
90 second |= 1 << 14;
Rabin Vincent72dc43a2010-08-10 19:52:35 +010091
Rabin Vincent4394e282012-02-18 17:47:03 +010092 return __opcode_thumb32_compose(first, second);
Rabin Vincent72dc43a2010-08-10 19:52:35 +010093}
94#else
Rabin Vincentdd686eb2010-11-06 23:03:21 +053095static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
96 bool link)
Abhishek Sagar014c2572008-05-31 14:23:50 +053097{
Rabin Vincentdd686eb2010-11-06 23:03:21 +053098 unsigned long opcode = 0xea000000;
Abhishek Sagar014c2572008-05-31 14:23:50 +053099 long offset;
100
Rabin Vincentdd686eb2010-11-06 23:03:21 +0530101 if (link)
102 opcode |= 1 << 24;
103
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100104 offset = (long)addr - (long)(pc + 8);
Abhishek Sagar014c2572008-05-31 14:23:50 +0530105 if (unlikely(offset < -33554432 || offset > 33554428)) {
106 /* Can't generate branches that far (from ARM ARM). Ftrace
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530107 * doesn't generate branches outside of kernel text.
Abhishek Sagar014c2572008-05-31 14:23:50 +0530108 */
109 WARN_ON_ONCE(1);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100110 return 0;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530111 }
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100112
113 offset = (offset >> 2) & 0x00ffffff;
114
Rabin Vincentdd686eb2010-11-06 23:03:21 +0530115 return opcode | offset;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530116}
Rabin Vincent72dc43a2010-08-10 19:52:35 +0100117#endif
Abhishek Sagar014c2572008-05-31 14:23:50 +0530118
Rabin Vincentdd686eb2010-11-06 23:03:21 +0530119static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
120{
121 return ftrace_gen_branch(pc, addr, true);
122}
123
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100124static int ftrace_modify_code(unsigned long pc, unsigned long old,
Rabin Vincentdc283d702012-02-29 15:59:07 +0100125 unsigned long new, bool validate)
Abhishek Sagar014c2572008-05-31 14:23:50 +0530126{
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100127 unsigned long replaced;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530128
Rabin Vincent4394e282012-02-18 17:47:03 +0100129 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
130 old = __opcode_to_mem_thumb32(old);
131 new = __opcode_to_mem_thumb32(new);
132 } else {
133 old = __opcode_to_mem_arm(old);
134 new = __opcode_to_mem_arm(new);
135 }
136
Rabin Vincentdc283d702012-02-29 15:59:07 +0100137 if (validate) {
138 if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE))
139 return -EFAULT;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530140
Rabin Vincentdc283d702012-02-29 15:59:07 +0100141 if (replaced != old)
142 return -EINVAL;
143 }
Abhishek Sagar014c2572008-05-31 14:23:50 +0530144
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100145 if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE))
146 return -EPERM;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530147
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100148 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
Abhishek Sagar014c2572008-05-31 14:23:50 +0530149
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100150 return 0;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530151}
152
153int ftrace_update_ftrace_func(ftrace_func_t func)
154{
Rabin Vincentdc283d702012-02-29 15:59:07 +0100155 unsigned long pc;
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100156 unsigned long new;
157 int ret;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530158
159 pc = (unsigned long)&ftrace_call;
Abhishek Sagar014c2572008-05-31 14:23:50 +0530160 new = ftrace_call_replace(pc, (unsigned long)func);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100161
Rabin Vincentdc283d702012-02-29 15:59:07 +0100162 ret = ftrace_modify_code(pc, 0, new, false);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100163
164#ifdef CONFIG_OLD_MCOUNT
165 if (!ret) {
166 pc = (unsigned long)&ftrace_call_old;
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100167 new = ftrace_call_replace(pc, (unsigned long)func);
168
Rabin Vincentdc283d702012-02-29 15:59:07 +0100169 ret = ftrace_modify_code(pc, 0, new, false);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100170 }
171#endif
172
Abhishek Sagar014c2572008-05-31 14:23:50 +0530173 return ret;
174}
175
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100176int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
177{
178 unsigned long new, old;
179 unsigned long ip = rec->ip;
180
181 old = ftrace_nop_replace(rec);
182 new = ftrace_call_replace(ip, adjust_address(rec, addr));
183
Rabin Vincentdc283d702012-02-29 15:59:07 +0100184 return ftrace_modify_code(rec->ip, old, new, true);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100185}
186
187int ftrace_make_nop(struct module *mod,
188 struct dyn_ftrace *rec, unsigned long addr)
189{
190 unsigned long ip = rec->ip;
191 unsigned long old;
192 unsigned long new;
193 int ret;
194
195 old = ftrace_call_replace(ip, adjust_address(rec, addr));
196 new = ftrace_nop_replace(rec);
Rabin Vincentdc283d702012-02-29 15:59:07 +0100197 ret = ftrace_modify_code(ip, old, new, true);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100198
199#ifdef CONFIG_OLD_MCOUNT
200 if (ret == -EINVAL && addr == MCOUNT_ADDR) {
201 rec->arch.old_mcount = true;
202
203 old = ftrace_call_replace(ip, adjust_address(rec, addr));
204 new = ftrace_nop_replace(rec);
Rabin Vincentdc283d702012-02-29 15:59:07 +0100205 ret = ftrace_modify_code(ip, old, new, true);
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100206 }
207#endif
208
209 return ret;
210}
211
Abhishek Sagar014c2572008-05-31 14:23:50 +0530212int __init ftrace_dyn_arch_init(void *data)
213{
Rabin Vincent3b6c2232010-08-10 19:43:28 +0100214 *(unsigned long *)data = 0;
215
Abhishek Sagar014c2572008-05-31 14:23:50 +0530216 return 0;
217}
Tim Bird376cfa82010-10-09 22:24:38 +0530218#endif /* CONFIG_DYNAMIC_FTRACE */
219
220#ifdef CONFIG_FUNCTION_GRAPH_TRACER
221void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
222 unsigned long frame_pointer)
223{
224 unsigned long return_hooker = (unsigned long) &return_to_handler;
225 struct ftrace_graph_ent trace;
226 unsigned long old;
227 int err;
228
229 if (unlikely(atomic_read(&current->tracing_graph_pause)))
230 return;
231
232 old = *parent;
233 *parent = return_hooker;
234
235 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
236 frame_pointer);
237 if (err == -EBUSY) {
238 *parent = old;
239 return;
240 }
241
242 trace.func = self_addr;
243
244 /* Only trace if the calling function expects to */
245 if (!ftrace_graph_entry(&trace)) {
246 current->curr_ret_stack--;
247 *parent = old;
248 }
249}
Rabin Vincentdd686eb2010-11-06 23:03:21 +0530250
251#ifdef CONFIG_DYNAMIC_FTRACE
252extern unsigned long ftrace_graph_call;
253extern unsigned long ftrace_graph_call_old;
254extern void ftrace_graph_caller_old(void);
255
256static int __ftrace_modify_caller(unsigned long *callsite,
257 void (*func) (void), bool enable)
258{
259 unsigned long caller_fn = (unsigned long) func;
260 unsigned long pc = (unsigned long) callsite;
261 unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
262 unsigned long nop = 0xe1a00000; /* mov r0, r0 */
263 unsigned long old = enable ? nop : branch;
264 unsigned long new = enable ? branch : nop;
265
Rabin Vincentdc283d702012-02-29 15:59:07 +0100266 return ftrace_modify_code(pc, old, new, true);
Rabin Vincentdd686eb2010-11-06 23:03:21 +0530267}
268
269static int ftrace_modify_graph_caller(bool enable)
270{
271 int ret;
272
273 ret = __ftrace_modify_caller(&ftrace_graph_call,
274 ftrace_graph_caller,
275 enable);
276
277#ifdef CONFIG_OLD_MCOUNT
278 if (!ret)
279 ret = __ftrace_modify_caller(&ftrace_graph_call_old,
280 ftrace_graph_caller_old,
281 enable);
282#endif
283
284 return ret;
285}
286
287int ftrace_enable_ftrace_graph_caller(void)
288{
289 return ftrace_modify_graph_caller(true);
290}
291
292int ftrace_disable_ftrace_graph_caller(void)
293{
294 return ftrace_modify_graph_caller(false);
295}
296#endif /* CONFIG_DYNAMIC_FTRACE */
Tim Bird376cfa82010-10-09 22:24:38 +0530297#endif /* CONFIG_FUNCTION_GRAPH_TRACER */