blob: cf5509f13dd57bc832e1db50d65f88ccda6e81c7 [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>
Wu Zhangjine4240542010-05-14 19:08:32 +08005 * Copyright (C) 2009, 2010 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
Wu Zhangjin29c5d342009-11-20 20:34:34 +080015#include <asm/asm.h>
16#include <asm/asm-offsets.h>
Wu Zhangjine4240542010-05-14 19:08:32 +080017#include <asm/cacheflush.h>
18#include <asm/uasm.h>
Wu Zhangjin538f1952009-11-20 20:34:32 +080019
Wu Zhangjind9cdb2f2011-01-20 03:28:29 +080020#include <asm-generic/sections.h>
Wu Zhangjinc9f84872010-05-14 19:08:34 +080021
Thomas Gleixner49de8302011-07-23 12:41:23 +000022#if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
23#define MCOUNT_OFFSET_INSNS 5
24#else
25#define MCOUNT_OFFSET_INSNS 4
26#endif
27
Al Cooper58b69402013-01-16 22:43:28 +000028/* Arch override because MIPS doesn't need to run this from stop_machine() */
29void arch_ftrace_update_code(int command)
30{
31 ftrace_modify_all_code(command);
32}
33
Thomas Gleixner49de8302011-07-23 12:41:23 +000034/*
35 * Check if the address is in kernel space
36 *
37 * Clone core_kernel_text() from kernel/extable.c, but doesn't call
38 * init_kernel_text() for Ftrace doesn't trace functions in init sections.
39 */
40static inline int in_kernel_space(unsigned long ip)
41{
42 if (ip >= (unsigned long)_stext &&
43 ip <= (unsigned long)_etext)
44 return 1;
45 return 0;
46}
47
Wu Zhangjin538f1952009-11-20 20:34:32 +080048#ifdef CONFIG_DYNAMIC_FTRACE
49
50#define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */
51#define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */
David Daneyc54794d2010-12-28 13:21:37 -080052#define JUMP_RANGE_MASK ((1UL << 28) - 1)
Wu Zhangjin538f1952009-11-20 20:34:32 +080053
Wu Zhangjin4d6829f2010-05-14 19:08:31 +080054#define INSN_NOP 0x00000000 /* nop */
Wu Zhangjine4240542010-05-14 19:08:32 +080055#define INSN_JAL(addr) \
56 ((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
57
58static unsigned int insn_jal_ftrace_caller __read_mostly;
59static unsigned int insn_lui_v1_hi16_mcount __read_mostly;
60static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;
61
62static inline void ftrace_dyn_arch_init_insns(void)
63{
64 u32 *buf;
65 unsigned int v1;
66
67 /* lui v1, hi16_mcount */
68 v1 = 3;
69 buf = (u32 *)&insn_lui_v1_hi16_mcount;
70 UASM_i_LA_mostly(&buf, v1, MCOUNT_ADDR);
71
72 /* jal (ftrace_caller + 8), jump over the first two instruction */
73 buf = (u32 *)&insn_jal_ftrace_caller;
David Daneyc54794d2010-12-28 13:21:37 -080074 uasm_i_jal(&buf, (FTRACE_ADDR + 8) & JUMP_RANGE_MASK);
Wu Zhangjine4240542010-05-14 19:08:32 +080075
76#ifdef CONFIG_FUNCTION_GRAPH_TRACER
77 /* j ftrace_graph_caller */
78 buf = (u32 *)&insn_j_ftrace_graph_caller;
David Daneyc54794d2010-12-28 13:21:37 -080079 uasm_i_j(&buf, (unsigned long)ftrace_graph_caller & JUMP_RANGE_MASK);
Wu Zhangjine4240542010-05-14 19:08:32 +080080#endif
81}
Wu Zhangjin538f1952009-11-20 20:34:32 +080082
83static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
84{
Wu Zhangjin046199c2009-11-20 20:34:36 +080085 int faulted;
86
87 /* *(unsigned int *)ip = new_code; */
88 safe_store_code(new_code, ip, faulted);
89
90 if (unlikely(faulted))
91 return -EFAULT;
Wu Zhangjin538f1952009-11-20 20:34:32 +080092
93 flush_icache_range(ip, ip + 8);
94
95 return 0;
96}
97
Al Cooper58b69402013-01-16 22:43:28 +000098#ifndef CONFIG_64BIT
99static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
100 unsigned int new_code2)
101{
102 int faulted;
103
104 safe_store_code(new_code1, ip, faulted);
105 if (unlikely(faulted))
106 return -EFAULT;
107 ip += 4;
108 safe_store_code(new_code2, ip, faulted);
109 if (unlikely(faulted))
110 return -EFAULT;
111 flush_icache_range(ip, ip + 8); /* original ip + 12 */
112 return 0;
113}
114#endif
115
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800116/*
117 * The details about the calling site of mcount on MIPS
118 *
119 * 1. For kernel:
120 *
121 * move at, ra
122 * jal _mcount --> nop
123 *
124 * 2. For modules:
125 *
126 * 2.1 For KBUILD_MCOUNT_RA_ADDRESS and CONFIG_32BIT
127 *
Ralf Baechle70342282013-01-22 12:59:30 +0100128 * lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800129 * addiu v1, v1, low_16bit_of_mcount
130 * move at, ra
131 * move $12, ra_address
132 * jalr v1
133 * sub sp, sp, 8
Ralf Baechle70342282013-01-22 12:59:30 +0100134 * 1: offset = 5 instructions
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800135 * 2.2 For the Other situations
136 *
Ralf Baechle70342282013-01-22 12:59:30 +0100137 * lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800138 * addiu v1, v1, low_16bit_of_mcount
139 * move at, ra
140 * jalr v1
141 * nop | move $12, ra_address | sub sp, sp, 8
Ralf Baechle70342282013-01-22 12:59:30 +0100142 * 1: offset = 4 instructions
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800143 */
144
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800145#define INSN_B_1F (0x10000000 | MCOUNT_OFFSET_INSNS)
146
Wu Zhangjin538f1952009-11-20 20:34:32 +0800147int ftrace_make_nop(struct module *mod,
148 struct dyn_ftrace *rec, unsigned long addr)
149{
150 unsigned int new;
151 unsigned long ip = rec->ip;
152
Wu Zhangjine4240542010-05-14 19:08:32 +0800153 /*
Wu Zhangjind9cdb2f2011-01-20 03:28:29 +0800154 * If ip is in kernel space, no long call, otherwise, long call is
155 * needed.
Wu Zhangjine4240542010-05-14 19:08:32 +0800156 */
Wu Zhangjin7f21a602011-01-20 03:28:31 +0800157 new = in_kernel_space(ip) ? INSN_NOP : INSN_B_1F;
Al Cooper58b69402013-01-16 22:43:28 +0000158#ifdef CONFIG_64BIT
Wu Zhangjin538f1952009-11-20 20:34:32 +0800159 return ftrace_modify_code(ip, new);
Al Cooper58b69402013-01-16 22:43:28 +0000160#else
161 /*
162 * On 32 bit MIPS platforms, gcc adds a stack adjust
163 * instruction in the delay slot after the branch to
164 * mcount and expects mcount to restore the sp on return.
165 * This is based on a legacy API and does nothing but
166 * waste instructions so it's being removed at runtime.
167 */
168 return ftrace_modify_code_2(ip, new, INSN_NOP);
169#endif
Wu Zhangjin538f1952009-11-20 20:34:32 +0800170}
171
Wu Zhangjin538f1952009-11-20 20:34:32 +0800172int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
173{
174 unsigned int new;
175 unsigned long ip = rec->ip;
176
Wu Zhangjind9cdb2f2011-01-20 03:28:29 +0800177 new = in_kernel_space(ip) ? insn_jal_ftrace_caller :
178 insn_lui_v1_hi16_mcount;
Wu Zhangjin538f1952009-11-20 20:34:32 +0800179
180 return ftrace_modify_code(ip, new);
181}
182
183#define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
184
185int ftrace_update_ftrace_func(ftrace_func_t func)
186{
187 unsigned int new;
188
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800189 new = INSN_JAL((unsigned long)func);
Wu Zhangjin538f1952009-11-20 20:34:32 +0800190
191 return ftrace_modify_code(FTRACE_CALL_IP, new);
192}
193
194int __init ftrace_dyn_arch_init(void *data)
195{
Wu Zhangjine4240542010-05-14 19:08:32 +0800196 /* Encode the instructions when booting */
197 ftrace_dyn_arch_init_insns();
198
199 /* Remove "b ftrace_stub" to ensure ftrace_caller() is executed */
200 ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);
201
Wu Zhangjin538f1952009-11-20 20:34:32 +0800202 /* The return code is retured via data */
203 *(unsigned long *)data = 0;
204
205 return 0;
206}
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800207#endif /* CONFIG_DYNAMIC_FTRACE */
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800208
209#ifdef CONFIG_FUNCTION_GRAPH_TRACER
210
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800211#ifdef CONFIG_DYNAMIC_FTRACE
212
213extern void ftrace_graph_call(void);
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800214#define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
215
216int ftrace_enable_ftrace_graph_caller(void)
217{
218 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
Wu Zhangjine4240542010-05-14 19:08:32 +0800219 insn_j_ftrace_graph_caller);
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800220}
221
222int ftrace_disable_ftrace_graph_caller(void)
223{
Wu Zhangjin4d6829f2010-05-14 19:08:31 +0800224 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800225}
226
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800227#endif /* CONFIG_DYNAMIC_FTRACE */
Wu Zhangjine17ff5f2009-11-20 20:34:35 +0800228
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800229#ifndef KBUILD_MCOUNT_RA_ADDRESS
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800230
Ralf Baechle70342282013-01-22 12:59:30 +0100231#define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */
232#define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800233#define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */
234
Wu Zhangjin2816e322011-01-20 03:28:30 +0800235unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long
236 old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800237{
Wu Zhangjin2816e322011-01-20 03:28:30 +0800238 unsigned long sp, ip, tmp;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800239 unsigned int code;
Wu Zhangjin046199c2009-11-20 20:34:36 +0800240 int faulted;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800241
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800242 /*
Wu Zhangjin2816e322011-01-20 03:28:30 +0800243 * For module, move the ip from the return address after the
Wu Zhangjin9a620a52011-01-20 03:28:27 +0800244 * instruction "lui v1, hi_16bit_of_mcount"(offset is 24), but for
245 * kernel, move after the instruction "move ra, at"(offset is 16)
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800246 */
Wu Zhangjin2816e322011-01-20 03:28:30 +0800247 ip = self_ra - (in_kernel_space(self_ra) ? 16 : 24);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800248
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800249 /*
250 * search the text until finding the non-store instruction or "s{d,w}
251 * ra, offset(sp)" instruction
252 */
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800253 do {
Wu Zhangjin046199c2009-11-20 20:34:36 +0800254 /* get the code at "ip": code = *(unsigned int *)ip; */
255 safe_load_code(code, ip, faulted);
256
257 if (unlikely(faulted))
258 return 0;
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800259 /*
260 * If we hit the non-store instruction before finding where the
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800261 * ra is stored, then this is a leaf function and it does not
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800262 * store the ra on the stack
263 */
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800264 if ((code & S_R_SP) != S_R_SP)
Wu Zhangjin2816e322011-01-20 03:28:30 +0800265 return parent_ra_addr;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800266
Wu Zhangjin9a620a52011-01-20 03:28:27 +0800267 /* Move to the next instruction */
268 ip -= 4;
269 } while ((code & S_RA_SP) != S_RA_SP);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800270
271 sp = fp + (code & OFFSET_MASK);
Wu Zhangjin046199c2009-11-20 20:34:36 +0800272
Wu Zhangjin2816e322011-01-20 03:28:30 +0800273 /* tmp = *(unsigned long *)sp; */
274 safe_load_stack(tmp, sp, faulted);
Wu Zhangjin046199c2009-11-20 20:34:36 +0800275 if (unlikely(faulted))
276 return 0;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800277
Wu Zhangjin2816e322011-01-20 03:28:30 +0800278 if (tmp == old_parent_ra)
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800279 return sp;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800280 return 0;
281}
282
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800283#endif /* !KBUILD_MCOUNT_RA_ADDRESS */
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800284
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800285/*
286 * Hook the return address and push it in the stack of return addrs
287 * in current thread info.
288 */
Wu Zhangjin2816e322011-01-20 03:28:30 +0800289void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800290 unsigned long fp)
291{
Wu Zhangjin2816e322011-01-20 03:28:30 +0800292 unsigned long old_parent_ra;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800293 struct ftrace_graph_ent trace;
294 unsigned long return_hooker = (unsigned long)
295 &return_to_handler;
Wu Zhangjinb9f07eb2011-01-22 02:01:53 +0800296 int faulted, insns;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800297
298 if (unlikely(atomic_read(&current->tracing_graph_pause)))
299 return;
300
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800301 /*
Wu Zhangjin2816e322011-01-20 03:28:30 +0800302 * "parent_ra_addr" is the stack address saved the return address of
303 * the caller of _mcount.
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800304 *
305 * if the gcc < 4.5, a leaf function does not save the return address
306 * in the stack address, so, we "emulate" one in _mcount's stack space,
307 * and hijack it directly, but for a non-leaf function, it save the
308 * return address to the its own stack space, we can not hijack it
309 * directly, but need to find the real stack address,
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800310 * ftrace_get_parent_addr() does it!
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800311 *
312 * if gcc>= 4.5, with the new -mmcount-ra-address option, for a
313 * non-leaf function, the location of the return address will be saved
314 * to $12 for us, and for a leaf function, only put a zero into $12. we
315 * do it in ftrace_graph_caller of mcount.S.
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800316 */
317
Wu Zhangjin2816e322011-01-20 03:28:30 +0800318 /* old_parent_ra = *parent_ra_addr; */
319 safe_load_stack(old_parent_ra, parent_ra_addr, faulted);
Wu Zhangjin046199c2009-11-20 20:34:36 +0800320 if (unlikely(faulted))
321 goto out;
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800322#ifndef KBUILD_MCOUNT_RA_ADDRESS
Wu Zhangjin2816e322011-01-20 03:28:30 +0800323 parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,
324 old_parent_ra, (unsigned long)parent_ra_addr, fp);
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800325 /*
326 * If fails when getting the stack address of the non-leaf function's
327 * ra, stop function graph tracer and return
328 */
Wu Zhangjin2816e322011-01-20 03:28:30 +0800329 if (parent_ra_addr == 0)
Wu Zhangjin046199c2009-11-20 20:34:36 +0800330 goto out;
Wu Zhangjin7326c4e2009-11-20 20:34:38 +0800331#endif
Wu Zhangjin2816e322011-01-20 03:28:30 +0800332 /* *parent_ra_addr = return_hooker; */
333 safe_store_stack(return_hooker, parent_ra_addr, faulted);
Wu Zhangjin046199c2009-11-20 20:34:36 +0800334 if (unlikely(faulted))
335 goto out;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800336
Wu Zhangjin2816e322011-01-20 03:28:30 +0800337 if (ftrace_push_return_trace(old_parent_ra, self_ra, &trace.depth, fp)
338 == -EBUSY) {
339 *parent_ra_addr = old_parent_ra;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800340 return;
341 }
342
Wu Zhangjinb9f07eb2011-01-22 02:01:53 +0800343 /*
344 * Get the recorded ip of the current mcount calling site in the
345 * __mcount_loc section, which will be used to filter the function
346 * entries configured through the tracing/set_graph_function interface.
347 */
348
349 insns = in_kernel_space(self_ra) ? 2 : MCOUNT_OFFSET_INSNS + 1;
350 trace.func = self_ra - (MCOUNT_INSN_SIZE * insns);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800351
352 /* Only trace if the calling function expects to */
353 if (!ftrace_graph_entry(&trace)) {
354 current->curr_ret_stack--;
Wu Zhangjin2816e322011-01-20 03:28:30 +0800355 *parent_ra_addr = old_parent_ra;
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800356 }
Wu Zhangjin046199c2009-11-20 20:34:36 +0800357 return;
358out:
359 ftrace_graph_stop();
360 WARN_ON(1);
Wu Zhangjin29c5d342009-11-20 20:34:34 +0800361}
Wu Zhangjin68ccf752010-05-14 19:08:33 +0800362#endif /* CONFIG_FUNCTION_GRAPH_TRACER */