MIPS: Tracing: Add function graph tracer support for MIPS
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / mips / kernel / ftrace.c
CommitLineData
538f1952
WZ
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>
29c5d346
WZ
16#include <asm/asm.h>
17#include <asm/asm-offsets.h>
538f1952
WZ
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
26static unsigned int ftrace_nop = 0x00000000;
27
28static 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
37static int lui_v1;
38static int jal_mcount;
39
40int 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
78static int modified; /* initialized as 0 by default */
79
80int 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
98int 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
107int __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 */
29c5d346
WZ
115
116#ifdef CONFIG_FUNCTION_GRAPH_TRACER
117
118#define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
119#define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
120#define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
121
122unsigned long ftrace_get_parent_addr(unsigned long self_addr,
123 unsigned long parent,
124 unsigned long parent_addr,
125 unsigned long fp)
126{
127 unsigned long sp, ip, ra;
128 unsigned int code;
129
130 /* in module or kernel? */
131 if (self_addr & 0x40000000) {
132 /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
133 ip = self_addr - 20;
134 } else {
135 /* kernel: move to the instruction "move ra, at" */
136 ip = self_addr - 12;
137 }
138
139 /* search the text until finding the non-store instruction or "s{d,w}
140 * ra, offset(sp)" instruction */
141 do {
142 ip -= 4;
143
144 /* get the code at "ip" */
145 code = *(unsigned int *)ip;
146
147 /* If we hit the non-store instruction before finding where the
148 * ra is stored, then this is a leaf function and it does not
149 * store the ra on the stack. */
150 if ((code & S_R_SP) != S_R_SP)
151 return parent_addr;
152
153 } while (((code & S_RA_SP) != S_RA_SP));
154
155 sp = fp + (code & OFFSET_MASK);
156 ra = *(unsigned long *)sp;
157
158 if (ra == parent)
159 return sp;
160
161 return 0;
162}
163
164/*
165 * Hook the return address and push it in the stack of return addrs
166 * in current thread info.
167 */
168void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
169 unsigned long fp)
170{
171 unsigned long old;
172 struct ftrace_graph_ent trace;
173 unsigned long return_hooker = (unsigned long)
174 &return_to_handler;
175
176 if (unlikely(atomic_read(&current->tracing_graph_pause)))
177 return;
178
179 /* "parent" is the stack address saved the return address of the caller
180 * of _mcount, for a leaf function not save the return address in the
181 * stack address, so, we "emulate" one in _mcount's stack space, and
182 * hijack it directly, but for a non-leaf function, it will save the
183 * return address to the its stack space, so, we can not hijack the
184 * "parent" directly, but need to find the real stack address,
185 * ftrace_get_parent_addr() does it!
186 */
187
188 old = *parent;
189
190 parent = (unsigned long *)ftrace_get_parent_addr(self_addr, old,
191 (unsigned long)parent,
192 fp);
193
194 /* If fails when getting the stack address of the non-leaf function's
195 * ra, stop function graph tracer and return */
196 if (parent == 0) {
197 ftrace_graph_stop();
198 WARN_ON(1);
199 return;
200 }
201
202 *parent = return_hooker;
203
204 if (ftrace_push_return_trace(old, self_addr, &trace.depth, fp) ==
205 -EBUSY) {
206 *parent = old;
207 return;
208 }
209
210 trace.func = self_addr;
211
212 /* Only trace if the calling function expects to */
213 if (!ftrace_graph_entry(&trace)) {
214 current->curr_ret_stack--;
215 *parent = old;
216 }
217}
218#endif /* CONFIG_FUNCTION_GRAPH_TRACER */