]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/mips/kernel/ftrace.c
MIPS: Tracing: Add dynamic function graph tracer for MIPS
[mv-sheeva.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 <wuzj@lemote.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         *(unsigned int *)ip = new_code;
31
32         flush_icache_range(ip, ip + 8);
33
34         return 0;
35 }
36
37 static int lui_v1;
38 static int jal_mcount;
39
40 int ftrace_make_nop(struct module *mod,
41                     struct dyn_ftrace *rec, unsigned long addr)
42 {
43         unsigned int new;
44         unsigned long ip = rec->ip;
45
46         /* We have compiled module with -mlong-calls, but compiled the kernel
47          * without it, we need to cope with them respectively. */
48         if (ip & 0x40000000) {
49                 /* record it for ftrace_make_call */
50                 if (lui_v1 == 0)
51                         lui_v1 = *(unsigned int *)ip;
52
53                 /* lui v1, hi_16bit_of_mcount        --> b 1f (0x10000004)
54                  * addiu v1, v1, low_16bit_of_mcount
55                  * move at, ra
56                  * jalr v1
57                  * nop
58                  *                                   1f: (ip + 12)
59                  */
60                 new = 0x10000004;
61         } else {
62                 /* record/calculate it for ftrace_make_call */
63                 if (jal_mcount == 0) {
64                         /* We can record it directly like this:
65                          *     jal_mcount = *(unsigned int *)ip;
66                          * Herein, jump over the first two nop instructions */
67                         jal_mcount = jump_insn_encode(JAL, (MCOUNT_ADDR + 8));
68                 }
69
70                 /* move at, ra
71                  * jalr v1              --> nop
72                  */
73                 new = ftrace_nop;
74         }
75         return ftrace_modify_code(ip, new);
76 }
77
78 static int modified;    /* initialized as 0 by default */
79
80 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
81 {
82         unsigned int new;
83         unsigned long ip = rec->ip;
84
85         /* We just need to remove the "b ftrace_stub" at the fist time! */
86         if (modified == 0) {
87                 modified = 1;
88                 ftrace_modify_code(addr, ftrace_nop);
89         }
90         /* ip, module: 0xc0000000, kernel: 0x80000000 */
91         new = (ip & 0x40000000) ? lui_v1 : jal_mcount;
92
93         return ftrace_modify_code(ip, new);
94 }
95
96 #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
97
98 int ftrace_update_ftrace_func(ftrace_func_t func)
99 {
100         unsigned int new;
101
102         new = jump_insn_encode(JAL, (unsigned long)func);
103
104         return ftrace_modify_code(FTRACE_CALL_IP, new);
105 }
106
107 int __init ftrace_dyn_arch_init(void *data)
108 {
109         /* The return code is retured via data */
110         *(unsigned long *)data = 0;
111
112         return 0;
113 }
114 #endif                          /* CONFIG_DYNAMIC_FTRACE */
115
116 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
117
118 #ifdef CONFIG_DYNAMIC_FTRACE
119
120 extern void ftrace_graph_call(void);
121 #define JMP     0x08000000      /* jump to target directly */
122 #define CALL_FTRACE_GRAPH_CALLER \
123         jump_insn_encode(JMP, (unsigned long)(&ftrace_graph_caller))
124 #define FTRACE_GRAPH_CALL_IP    ((unsigned long)(&ftrace_graph_call))
125
126 int ftrace_enable_ftrace_graph_caller(void)
127 {
128         return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
129                                   CALL_FTRACE_GRAPH_CALLER);
130 }
131
132 int ftrace_disable_ftrace_graph_caller(void)
133 {
134         return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, ftrace_nop);
135 }
136
137 #endif                          /* !CONFIG_DYNAMIC_FTRACE */
138
139 #define S_RA_SP (0xafbf << 16)  /* s{d,w} ra, offset(sp) */
140 #define S_R_SP  (0xafb0 << 16)  /* s{d,w} R, offset(sp) */
141 #define OFFSET_MASK     0xffff  /* stack offset range: 0 ~ PT_SIZE */
142
143 unsigned long ftrace_get_parent_addr(unsigned long self_addr,
144                                      unsigned long parent,
145                                      unsigned long parent_addr,
146                                      unsigned long fp)
147 {
148         unsigned long sp, ip, ra;
149         unsigned int code;
150
151         /* in module or kernel? */
152         if (self_addr & 0x40000000) {
153                 /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
154                 ip = self_addr - 20;
155         } else {
156                 /* kernel: move to the instruction "move ra, at" */
157                 ip = self_addr - 12;
158         }
159
160         /* search the text until finding the non-store instruction or "s{d,w}
161          * ra, offset(sp)" instruction */
162         do {
163                 ip -= 4;
164
165                 /* get the code at "ip" */
166                 code = *(unsigned int *)ip;
167
168                 /* If we hit the non-store instruction before finding where the
169                  * ra is stored, then this is a leaf function and it does not
170                  * store the ra on the stack. */
171                 if ((code & S_R_SP) != S_R_SP)
172                         return parent_addr;
173
174         } while (((code & S_RA_SP) != S_RA_SP));
175
176         sp = fp + (code & OFFSET_MASK);
177         ra = *(unsigned long *)sp;
178
179         if (ra == parent)
180                 return sp;
181
182         return 0;
183 }
184
185 /*
186  * Hook the return address and push it in the stack of return addrs
187  * in current thread info.
188  */
189 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
190                            unsigned long fp)
191 {
192         unsigned long old;
193         struct ftrace_graph_ent trace;
194         unsigned long return_hooker = (unsigned long)
195             &return_to_handler;
196
197         if (unlikely(atomic_read(&current->tracing_graph_pause)))
198                 return;
199
200         /* "parent" is the stack address saved the return address of the caller
201          * of _mcount, for a leaf function not save the return address in the
202          * stack address, so, we "emulate" one in _mcount's stack space, and
203          * hijack it directly, but for a non-leaf function, it will save the
204          * return address to the its stack space, so, we can not hijack the
205          * "parent" directly, but need to find the real stack address,
206          * ftrace_get_parent_addr() does it!
207          */
208
209         old = *parent;
210
211         parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
212                                                          (unsigned long)parent,
213                                                          fp);
214
215         /* If fails when getting the stack address of the non-leaf function's
216          * ra, stop function graph tracer and return */
217         if (parent == 0) {
218                 ftrace_graph_stop();
219                 WARN_ON(1);
220                 return;
221         }
222
223         *parent = return_hooker;
224
225         if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
226             -EBUSY) {
227                 *parent = old;
228                 return;
229         }
230
231         trace.func = self_addr;
232
233         /* Only trace if the calling function expects to */
234         if (!ftrace_graph_entry(&trace)) {
235                 current->curr_ret_stack--;
236                 *parent = old;
237         }
238 }
239 #endif                          /* CONFIG_FUNCTION_GRAPH_TRACER */