]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/mips/kernel/ftrace.c
MIPS: Tracing: Fix 32-bit support with -mmcount-ra-address
[net-next-2.6.git] / arch / mips / kernel / ftrace.c
1 /*
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
6  * Author: Wu Zhangjin <wuzhangjin@gmail.com>
7  *
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>
16 #include <asm/asm.h>
17 #include <asm/asm-offsets.h>
18
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
26 static unsigned int ftrace_nop = 0x00000000;
27
28 static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
29 {
30         int faulted;
31
32         /* *(unsigned int *)ip = new_code; */
33         safe_store_code(new_code, ip, faulted);
34
35         if (unlikely(faulted))
36                 return -EFAULT;
37
38         flush_icache_range(ip, ip + 8);
39
40         return 0;
41 }
42
43 static int lui_v1;
44 static int jal_mcount;
45
46 int ftrace_make_nop(struct module *mod,
47                     struct dyn_ftrace *rec, unsigned long addr)
48 {
49         unsigned int new;
50         int faulted;
51         unsigned long ip = rec->ip;
52
53         /* We have compiled module with -mlong-calls, but compiled the kernel
54          * without it, we need to cope with them respectively. */
55         if (ip & 0x40000000) {
56                 /* record it for ftrace_make_call */
57                 if (lui_v1 == 0) {
58                         /* lui_v1 = *(unsigned int *)ip; */
59                         safe_load_code(lui_v1, ip, faulted);
60
61                         if (unlikely(faulted))
62                                 return -EFAULT;
63                 }
64
65 #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
66                 /* lui v1, hi_16bit_of_mcount        --> b 1f (0x10000005)
67                  * addiu v1, v1, low_16bit_of_mcount
68                  * move at, ra
69                  * move $12, ra_address
70                  * jalr v1
71                  *  sub sp, sp, 8
72                  *                                  1: offset = 5 instructions
73                  */
74                 new = 0x10000005;
75 #else
76                 /* lui v1, hi_16bit_of_mcount        --> b 1f (0x10000004)
77                  * addiu v1, v1, low_16bit_of_mcount
78                  * move at, ra
79                  * jalr v1
80                  *  nop | move $12, ra_address | sub sp, sp, 8
81                  *                                  1: offset = 4 instructions
82                  */
83                 new = 0x10000004;
84 #endif
85         } else {
86                 /* record/calculate it for ftrace_make_call */
87                 if (jal_mcount == 0) {
88                         /* We can record it directly like this:
89                          *     jal_mcount = *(unsigned int *)ip;
90                          * Herein, jump over the first two nop instructions */
91                         jal_mcount = jump_insn_encode(JAL, (MCOUNT_ADDR + 8));
92                 }
93
94                 /* move at, ra
95                  * jalr v1              --> nop
96                  */
97                 new = ftrace_nop;
98         }
99         return ftrace_modify_code(ip, new);
100 }
101
102 static int modified;    /* initialized as 0 by default */
103
104 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
105 {
106         unsigned int new;
107         unsigned long ip = rec->ip;
108
109         /* We just need to remove the "b ftrace_stub" at the fist time! */
110         if (modified == 0) {
111                 modified = 1;
112                 ftrace_modify_code(addr, ftrace_nop);
113         }
114         /* ip, module: 0xc0000000, kernel: 0x80000000 */
115         new = (ip & 0x40000000) ? lui_v1 : jal_mcount;
116
117         return ftrace_modify_code(ip, new);
118 }
119
120 #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
121
122 int ftrace_update_ftrace_func(ftrace_func_t func)
123 {
124         unsigned int new;
125
126         new = jump_insn_encode(JAL, (unsigned long)func);
127
128         return ftrace_modify_code(FTRACE_CALL_IP, new);
129 }
130
131 int __init ftrace_dyn_arch_init(void *data)
132 {
133         /* The return code is retured via data */
134         *(unsigned long *)data = 0;
135
136         return 0;
137 }
138 #endif                          /* CONFIG_DYNAMIC_FTRACE */
139
140 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
141
142 #ifdef CONFIG_DYNAMIC_FTRACE
143
144 extern void ftrace_graph_call(void);
145 #define JMP     0x08000000      /* jump to target directly */
146 #define CALL_FTRACE_GRAPH_CALLER \
147         jump_insn_encode(JMP, (unsigned long)(&ftrace_graph_caller))
148 #define FTRACE_GRAPH_CALL_IP    ((unsigned long)(&ftrace_graph_call))
149
150 int ftrace_enable_ftrace_graph_caller(void)
151 {
152         return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
153                                   CALL_FTRACE_GRAPH_CALLER);
154 }
155
156 int ftrace_disable_ftrace_graph_caller(void)
157 {
158         return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, ftrace_nop);
159 }
160
161 #endif                          /* !CONFIG_DYNAMIC_FTRACE */
162
163 #ifndef KBUILD_MCOUNT_RA_ADDRESS
164 #define S_RA_SP (0xafbf << 16)  /* s{d,w} ra, offset(sp) */
165 #define S_R_SP  (0xafb0 << 16)  /* s{d,w} R, offset(sp) */
166 #define OFFSET_MASK     0xffff  /* stack offset range: 0 ~ PT_SIZE */
167
168 unsigned long ftrace_get_parent_addr(unsigned long self_addr,
169                                      unsigned long parent,
170                                      unsigned long parent_addr,
171                                      unsigned long fp)
172 {
173         unsigned long sp, ip, ra;
174         unsigned int code;
175         int faulted;
176
177         /* in module or kernel? */
178         if (self_addr & 0x40000000) {
179                 /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
180                 ip = self_addr - 20;
181         } else {
182                 /* kernel: move to the instruction "move ra, at" */
183                 ip = self_addr - 12;
184         }
185
186         /* search the text until finding the non-store instruction or "s{d,w}
187          * ra, offset(sp)" instruction */
188         do {
189                 ip -= 4;
190
191                 /* get the code at "ip": code = *(unsigned int *)ip; */
192                 safe_load_code(code, ip, faulted);
193
194                 if (unlikely(faulted))
195                         return 0;
196
197                 /* If we hit the non-store instruction before finding where the
198                  * ra is stored, then this is a leaf function and it does not
199                  * store the ra on the stack. */
200                 if ((code & S_R_SP) != S_R_SP)
201                         return parent_addr;
202
203         } while (((code & S_RA_SP) != S_RA_SP));
204
205         sp = fp + (code & OFFSET_MASK);
206
207         /* ra = *(unsigned long *)sp; */
208         safe_load_stack(ra, sp, faulted);
209         if (unlikely(faulted))
210                 return 0;
211
212         if (ra == parent)
213                 return sp;
214         return 0;
215 }
216
217 #endif
218
219 /*
220  * Hook the return address and push it in the stack of return addrs
221  * in current thread info.
222  */
223 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
224                            unsigned long fp)
225 {
226         unsigned long old;
227         struct ftrace_graph_ent trace;
228         unsigned long return_hooker = (unsigned long)
229             &return_to_handler;
230         int faulted;
231
232         if (unlikely(atomic_read(&current->tracing_graph_pause)))
233                 return;
234
235         /* "parent" is the stack address saved the return address of the caller
236          * of _mcount.
237          *
238          * if the gcc < 4.5, a leaf function does not save the return address
239          * in the stack address, so, we "emulate" one in _mcount's stack space,
240          * and hijack it directly, but for a non-leaf function, it save the
241          * return address to the its own stack space, we can not hijack it
242          * directly, but need to find the real stack address,
243          * ftrace_get_parent_addr() does it!
244          *
245          * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
246          * non-leaf function, the location of the return address will be saved
247          * to $12 for us, and for a leaf function, only put a zero into $12. we
248          * do it in ftrace_graph_caller of mcount.S.
249          */
250
251         /* old = *parent; */
252         safe_load_stack(old, parent, faulted);
253         if (unlikely(faulted))
254                 goto out;
255 #ifndef KBUILD_MCOUNT_RA_ADDRESS
256         parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
257                                                          (unsigned long)parent,
258                                                          fp);
259         /* If fails when getting the stack address of the non-leaf function's
260          * ra, stop function graph tracer and return */
261         if (parent == 0)
262                 goto out;
263 #endif
264         /* *parent = return_hooker; */
265         safe_store_stack(return_hooker, parent, faulted);
266         if (unlikely(faulted))
267                 goto out;
268
269         if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
270             -EBUSY) {
271                 *parent = old;
272                 return;
273         }
274
275         trace.func = self_addr;
276
277         /* Only trace if the calling function expects to */
278         if (!ftrace_graph_entry(&trace)) {
279                 current->curr_ret_stack--;
280                 *parent = old;
281         }
282         return;
283 out:
284         ftrace_graph_stop();
285         WARN_ON(1);
286 }
287 #endif                          /* CONFIG_FUNCTION_GRAPH_TRACER */