2 * Code for replacing ftrace calls with jumps.
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>
8 * Thanks goes to Steven Rostedt for writing the original x86 version.
11 #include <linux/uaccess.h>
12 #include <linux/init.h>
13 #include <linux/ftrace.h>
15 #include <asm/cacheflush.h>
17 #include <asm/asm-offsets.h>
19 #ifdef CONFIG_DYNAMIC_FTRACE
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)))
26 static unsigned int ftrace_nop
= 0x00000000;
28 static int ftrace_modify_code(unsigned long ip
, unsigned int new_code
)
32 /* *(unsigned int *)ip = new_code; */
33 safe_store_code(new_code
, ip
, faulted
);
35 if (unlikely(faulted
))
38 flush_icache_range(ip
, ip
+ 8);
44 static int jal_mcount
;
46 int ftrace_make_nop(struct module
*mod
,
47 struct dyn_ftrace
*rec
, unsigned long addr
)
51 unsigned long ip
= rec
->ip
;
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 */
58 /* lui_v1 = *(unsigned int *)ip; */
59 safe_load_code(lui_v1
, ip
, faulted
);
61 if (unlikely(faulted
))
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
69 * move $12, ra_address
72 * 1: offset = 5 instructions
76 /* lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
77 * addiu v1, v1, low_16bit_of_mcount
80 * nop | move $12, ra_address | sub sp, sp, 8
81 * 1: offset = 4 instructions
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));
99 return ftrace_modify_code(ip
, new);
102 static int modified
; /* initialized as 0 by default */
104 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
107 unsigned long ip
= rec
->ip
;
109 /* We just need to remove the "b ftrace_stub" at the fist time! */
112 ftrace_modify_code(addr
, ftrace_nop
);
114 /* ip, module: 0xc0000000, kernel: 0x80000000 */
115 new = (ip
& 0x40000000) ? lui_v1
: jal_mcount
;
117 return ftrace_modify_code(ip
, new);
120 #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
122 int ftrace_update_ftrace_func(ftrace_func_t func
)
126 new = jump_insn_encode(JAL
, (unsigned long)func
);
128 return ftrace_modify_code(FTRACE_CALL_IP
, new);
131 int __init
ftrace_dyn_arch_init(void *data
)
133 /* The return code is retured via data */
134 *(unsigned long *)data
= 0;
138 #endif /* CONFIG_DYNAMIC_FTRACE */
140 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
142 #ifdef CONFIG_DYNAMIC_FTRACE
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))
150 int ftrace_enable_ftrace_graph_caller(void)
152 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP
,
153 CALL_FTRACE_GRAPH_CALLER
);
156 int ftrace_disable_ftrace_graph_caller(void)
158 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP
, ftrace_nop
);
161 #endif /* !CONFIG_DYNAMIC_FTRACE */
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 */
168 unsigned long ftrace_get_parent_addr(unsigned long self_addr
,
169 unsigned long parent
,
170 unsigned long parent_addr
,
173 unsigned long sp
, ip
, ra
;
177 /* in module or kernel? */
178 if (self_addr
& 0x40000000) {
179 /* module: move to the instruction "lui v1, HI_16BIT_OF_MCOUNT" */
182 /* kernel: move to the instruction "move ra, at" */
186 /* search the text until finding the non-store instruction or "s{d,w}
187 * ra, offset(sp)" instruction */
191 /* get the code at "ip": code = *(unsigned int *)ip; */
192 safe_load_code(code
, ip
, faulted
);
194 if (unlikely(faulted
))
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
)
203 } while (((code
& S_RA_SP
) != S_RA_SP
));
205 sp
= fp
+ (code
& OFFSET_MASK
);
207 /* ra = *(unsigned long *)sp; */
208 safe_load_stack(ra
, sp
, faulted
);
209 if (unlikely(faulted
))
220 * Hook the return address and push it in the stack of return addrs
221 * in current thread info.
223 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
,
227 struct ftrace_graph_ent trace
;
228 unsigned long return_hooker
= (unsigned long)
232 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
235 /* "parent" is the stack address saved the return address of the caller
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!
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.
252 safe_load_stack(old
, parent
, faulted
);
253 if (unlikely(faulted
))
255 #ifndef KBUILD_MCOUNT_RA_ADDRESS
256 parent
= (unsigned long *)ftrace_get_parent_addr(self_addr
, old
,
257 (unsigned long)parent
,
259 /* If fails when getting the stack address of the non-leaf function's
260 * ra, stop function graph tracer and return */
264 /* *parent = return_hooker; */
265 safe_store_stack(return_hooker
, parent
, faulted
);
266 if (unlikely(faulted
))
269 if (ftrace_push_return_trace(old
, self_addr
, &trace
.depth
, fp
) ==
275 trace
.func
= self_addr
;
277 /* Only trace if the calling function expects to */
278 if (!ftrace_graph_entry(&trace
)) {
279 current
->curr_ret_stack
--;
287 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
This page took 0.045823 seconds and 5 git commands to generate.