blob: b1b8fec2432f8666405e064064f042c3788ea964 [file] [log] [blame]
Wu Zhangjin538f1952009-11-20 20:34:32 +08001/*
2 * Code for replacing ftrace calls with jumps.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2009 DSLab, Lanzhou University, China
Wu Zhangjinf7a904d2010-01-04 17:16:51 +08006 * Author: Wu Zhangjin <wuzhangjin@gmail.com>
Wu Zhangjin538f1952009-11-20 20:34:32 +08007 *
8 * Thanks goes to Steven Rostedt for writing the original x86 version.
9 */
10
11#include <linux/uaccess.h>
12#include <linux/init.h>
13#include <linux/ftrace.h>
14
15#include <asm/cacheflush.h>
Wu Zhangjin29c5d342009-11-20 20:34:34 +080016#include <asm/asm.h>
17#include <asm/asm-offsets.h>
Wu Zhangjin538f1952009-11-20 20:34:32 +080018
19#ifdef CONFIG_DYNAMIC_FTRACE
20
21#define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
22#define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
23#define jump_insn_encode(op_code, addr) \
24 ((unsigned int)((op_code) | (((addr) >> 2) & ADDR_MASK)))
25
Wu Zhangjin4d6829f2010-05-14 19:08:31 +080026#define INSN_B_1F_4 0x10000004 /* b 1f; offset = 4 */
27#define INSN_B_1F_5 0x10000005 /* b 1f; offset = 5 */
28#define INSN_NOP 0x00000000 /* nop */
29#define INSN_JAL(addr) jump_insn_encode(JAL, addr)
Wu Zhangjin538f1952009-11-20 20:34:32 +080030
31static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
32{
Wu Zhangjin046199c2009-11-20 20:34:36 +080033 int faulted;
34
35 /* *(unsigned int *)ip = new_code; */
36 safe_store_code(new_code, ip, faulted);
37
38 if (unlikely(faulted))
39 return -EFAULT;
Wu Zhangjin538f1952009-11-20 20:34:32 +080040
41 flush_icache_range(ip, ip + 8);
42
43 return 0;
44}
45
46static int lui_v1;
47static int jal_mcount;
48
49int ftrace_make_nop(struct module *mod,
50 struct dyn_ftrace *rec, unsigned long addr)
51{
52 unsigned int new;
Wu Zhangjin046199c2009-11-20 20:34:36 +080053 int faulted;
Wu Zhangjin538f1952009-11-20 20:34:32 +080054 unsigned long ip = rec->ip;
55
56 /* We have compiled module with -mlong-calls, but compiled the kernel
57 * without it, we need to cope with them respectively. */
58 if (ip & 0x40000000) {
59 /* record it for ftrace_make_call */
Wu Zhangjin046199c2009-11-20 20:34:36 +080060 if (lui_v1 == 0) {
61 /* lui_v1 = *(unsigned int *)ip; */
62 safe_load_code(lui_v1, ip, faulted);
63
64 if (unlikely(faulted))
65 return -EFAULT;
66 }
Wu Zhangjin538f1952009-11-20 20:34:32 +080067
Wu Zhangjin3a2af2d2010-05-14 19:08:30 +080068#if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
69 /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
70 * addiu v1, v1, low_16bit_of_mcount
71 * move at, ra
72 * move $12, ra_address
73 * jalr v1
74 * sub sp, sp, 8
75 * 1: offset = 5 instructions
76 */
Wu Zhangjin4d6829f2010-05-14 19:08:31 +080077 new = INSN_B_1F_5;
Wu Zhangjin3a2af2d2010-05-14 19:08:30 +080078#else
Wu Zhangjin538f1952009-11-20 20:34:32 +080079 /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
80 * addiu v1, v1, low_16bit_of_mcount
81 * move at, ra
82 * jalr v1
Wu Zhangjin3a2af2d2010-05-14 19:08:30 +080083 * nop | move $12, ra_address | sub sp, sp, 8
84 * 1: offset = 4 instructions
Wu Zhangjin538f1952009-11-20 20:34:32 +080085 */
Wu Zhangjin4d6829f2010-05-14 19:08:31 +080086 new = INSN_B_1F_4;
Wu Zhangjin3a2af2d2010-05-14 19:08:30 +080087#endif
Wu Zhangjin538f1952009-11-20 20:34:32 +080088 } else {
89 /* record/calculate it for ftrace_make_call */
90 if (jal_mcount == 0) {
91 /* We can record it directly like this:
92 * jal_mcount = *(unsigned int *)ip;
93 * Herein, jump over the first two nop instructions */
Wu Zhangjin4d6829f2010-05-14 19:08:31 +080094 jal_mcount = INSN_JAL(MCOUNT_ADDR + 8);
Wu Zhangjin538f1952009-11-20 20:34:32 +080095 }
96
97 /* move at, ra
98 * jalr v1 --> nop
99 */
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800100 new = INSN_NOP;
Wu Zhangjin538f1952009-11-20 20:34:32 +0800101 }
102 return ftrace_modify_code(ip, new);
103}
104
105static int modified; /* initialized as 0 by default */
106
107int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
108{
109 unsigned int new;
110 unsigned long ip = rec->ip;
111
112 /* We just need to remove the "b ftrace_stub" at the fist time! */
113 if (modified == 0) {
114 modified = 1;
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800115 ftrace_modify_code(addr, INSN_NOP);
Wu Zhangjin538f1952009-11-20 20:34:32 +0800116 }
117 /* ip, module: 0xc0000000, kernel: 0x80000000 */
118 new = (ip & 0x40000000) ? lui_v1 : jal_mcount;
119
120 return ftrace_modify_code(ip, new);
121}
122
123#define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
124
125int ftrace_update_ftrace_func(ftrace_func_t func)
126{
127 unsigned int new;
128
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800129 new = INSN_JAL((unsigned long)func);
Wu Zhangjin538f1952009-11-20 20:34:32 +0800130
131 return ftrace_modify_code(FTRACE_CALL_IP, new);
132}
133
134int __init ftrace_dyn_arch_init(void *data)
135{
136 /* The return code is retured via data */
137 *(unsigned long *)data = 0;
138
139 return 0;
140}
141#endif /* CONFIG_DYNAMIC_FTRACE */
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800142
143#ifdef CONFIG_FUNCTION_GRAPH_TRACER
144
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800145#ifdef CONFIG_DYNAMIC_FTRACE
146
147extern void ftrace_graph_call(void);
148#define JMP 0x08000000 /* jump to target directly */
149#define CALL_FTRACE_GRAPH_CALLER \
150 jump_insn_encode(JMP, (unsigned long)(&ftrace_graph_caller))
151#define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
152
153int ftrace_enable_ftrace_graph_caller(void)
154{
155 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
156 CALL_FTRACE_GRAPH_CALLER);
157}
158
159int ftrace_disable_ftrace_graph_caller(void)
160{
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800161 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800162}
163
164#endif /* !CONFIG_DYNAMIC_FTRACE */
165
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800166#ifndef KBUILD_MCOUNT_RA_ADDRESS
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800167#define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
168#define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
169#define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
170
171unsigned long ftrace_get_parent_addr(unsigned long self_addr,
172 unsigned long parent,
173 unsigned long parent_addr,
174 unsigned long fp)
175{
176 unsigned long sp, ip, ra;
177 unsigned int code;
Wu Zhangjin046199c2009-11-20 20:34:36 +0800178 int faulted;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800179
180 /* in module or kernel? */
181 if (self_addr & 0x40000000) {
182 /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
183 ip = self_addr - 20;
184 } else {
185 /* kernel: move to the instruction "move ra, at" */
186 ip = self_addr - 12;
187 }
188
189 /* search the text until finding the non-store instruction or "s{d,w}
190 * ra, offset(sp)" instruction */
191 do {
192 ip -= 4;
193
Wu Zhangjin046199c2009-11-20 20:34:36 +0800194 /* get the code at "ip": code = *(unsigned int *)ip; */
195 safe_load_code(code, ip, faulted);
196
197 if (unlikely(faulted))
198 return 0;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800199
200 /* If we hit the non-store instruction before finding where the
201 * ra is stored, then this is a leaf function and it does not
202 * store the ra on the stack. */
203 if ((code & S_R_SP) != S_R_SP)
204 return parent_addr;
205
206 } while (((code & S_RA_SP) != S_RA_SP));
207
208 sp = fp + (code & OFFSET_MASK);
Wu Zhangjin046199c2009-11-20 20:34:36 +0800209
210 /* ra = *(unsigned long *)sp; */
211 safe_load_stack(ra, sp, faulted);
212 if (unlikely(faulted))
213 return 0;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800214
215 if (ra == parent)
216 return sp;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800217 return 0;
218}
219
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800220#endif
221
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800222/*
223 * Hook the return address and push it in the stack of return addrs
224 * in current thread info.
225 */
226void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
227 unsigned long fp)
228{
229 unsigned long old;
230 struct ftrace_graph_ent trace;
231 unsigned long return_hooker = (unsigned long)
232 &return_to_handler;
Wu Zhangjin046199c2009-11-20 20:34:36 +0800233 int faulted;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800234
235 if (unlikely(atomic_read(&current->tracing_graph_pause)))
236 return;
237
238 /* "parent" is the stack address saved the return address of the caller
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800239 * of _mcount.
240 *
241 * if the gcc < 4.5, a leaf function does not save the return address
242 * in the stack address, so, we "emulate" one in _mcount's stack space,
243 * and hijack it directly, but for a non-leaf function, it save the
244 * return address to the its own stack space, we can not hijack it
245 * directly, but need to find the real stack address,
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800246 * ftrace_get_parent_addr() does it!
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800247 *
248 * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
249 * non-leaf function, the location of the return address will be saved
250 * to $12 for us, and for a leaf function, only put a zero into $12. we
251 * do it in ftrace_graph_caller of mcount.S.
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800252 */
253
Wu Zhangjin046199c2009-11-20 20:34:36 +0800254 /* old = *parent; */
255 safe_load_stack(old, parent, faulted);
256 if (unlikely(faulted))
257 goto out;
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800258#ifndef KBUILD_MCOUNT_RA_ADDRESS
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800259 parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
260 (unsigned long)parent,
261 fp);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800262 /* If fails when getting the stack address of the non-leaf function's
263 * ra, stop function graph tracer and return */
Wu Zhangjin046199c2009-11-20 20:34:36 +0800264 if (parent == 0)
265 goto out;
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800266#endif
Wu Zhangjin046199c2009-11-20 20:34:36 +0800267 /* *parent = return_hooker; */
268 safe_store_stack(return_hooker, parent, faulted);
269 if (unlikely(faulted))
270 goto out;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800271
272 if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
273 -EBUSY) {
274 *parent = old;
275 return;
276 }
277
278 trace.func = self_addr;
279
280 /* Only trace if the calling function expects to */
281 if (!ftrace_graph_entry(&trace)) {
282 current->curr_ret_stack--;
283 *parent = old;
284 }
Wu Zhangjin046199c2009-11-20 20:34:36 +0800285 return;
286out:
287 ftrace_graph_stop();
288 WARN_ON(1);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800289}
290#endif /* CONFIG_FUNCTION_GRAPH_TRACER */