2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
7 #include <asm/assembler.h>
8 #include <asm/ftrace.h>
9 #include <asm/unwind.h>
10 #include <asm/export.h>
12 #include "entry-header.S"
15 * When compiling with -pg, gcc inserts a call to the mcount routine at the
16 * start of every function. In mcount, apart from the function's address (in
17 * lr), we need to get hold of the function's caller's address.
19 * Older GCCs (pre-4.4) inserted a call to a routine called mcount like this:
23 * These versions have the limitation that in order for the mcount routine to
24 * be able to determine the function's caller's address, an APCS-style frame
25 * pointer (which is set up with something like the code below) is required.
28 * push {fp, ip, lr, pc}
31 * With EABI, these frame pointers are not available unless -mapcs-frame is
32 * specified, and if building as Thumb-2, not even then.
34 * Newer GCCs (4.4+) solve this problem by introducing a new version of mcount,
35 * with call sites like:
40 * With these compilers, frame pointers are not necessary.
42 * mcount can be thought of as a function called in the middle of a subroutine
43 * call. As such, it needs to be transparent for both the caller and the
44 * callee: the original lr needs to be restored when leaving mcount, and no
45 * registers should be clobbered. (In the __gnu_mcount_nc implementation, we
46 * clobber the ip register. This is OK because the ARM calling convention
47 * allows it to be clobbered in subroutines and doesn't use it to hold
50 * When using dynamic ftrace, we patch out the mcount call by a "mov r0, r0"
51 * for the mcount case, and a "pop {lr}" for the __gnu_mcount_nc case (see
52 * arch/arm/kernel/ftrace.c).
55 #ifndef CONFIG_OLD_MCOUNT
56 #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 4))
57 #error Ftrace requires CONFIG_FRAME_POINTER=y with GCC older than 4.4.0.
61 .macro mcount_adjust_addr rd, rn
62 bic \rd, \rn, #1 @ clear the Thumb bit if present
63 sub \rd, \rd, #MCOUNT_INSN_SIZE
66 .macro __mcount suffix
68 ldr r0, =ftrace_trace_function
74 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
75 ldr r1, =ftrace_graph_return
78 bne ftrace_graph_caller\suffix
80 ldr r1, =ftrace_graph_entry
82 ldr r0, =ftrace_graph_entry_stub
84 bne ftrace_graph_caller\suffix
89 1: mcount_get_lr r1 @ lr of instrumented func
90 mcount_adjust_addr r0, lr @ instrumented function
96 .macro __ftrace_caller suffix
99 mcount_get_lr r1 @ lr of instrumented func
100 mcount_adjust_addr r0, lr @ instrumented function
102 .globl ftrace_call\suffix
106 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
107 .globl ftrace_graph_call\suffix
108 ftrace_graph_call\suffix:
115 .macro __ftrace_graph_caller
116 sub r0, fp, #4 @ &lr of instrumented routine (&parent)
117 #ifdef CONFIG_DYNAMIC_FTRACE
118 @ called from __ftrace_caller, saved in mcount_enter
119 ldr r1, [sp, #16] @ instrumented routine (func)
120 mcount_adjust_addr r1, r1
122 @ called from __mcount, untouched in lr
123 mcount_adjust_addr r1, lr @ instrumented routine (func)
125 mov r2, fp @ frame pointer
126 bl prepare_ftrace_return
130 #ifdef CONFIG_OLD_MCOUNT
136 stmdb sp!, {r0-r3, lr}
139 .macro mcount_get_lr reg
145 ldmia sp!, {r0-r3, pc}
149 #ifdef CONFIG_DYNAMIC_FTRACE
157 EXPORT_SYMBOL(mcount)
159 #ifdef CONFIG_DYNAMIC_FTRACE
160 ENTRY(ftrace_caller_old)
162 ENDPROC(ftrace_caller_old)
165 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
166 ENTRY(ftrace_graph_caller_old)
167 __ftrace_graph_caller
168 ENDPROC(ftrace_graph_caller_old)
172 .purgem mcount_get_lr
182 * This pad compensates for the push {lr} at the call site. Note that we are
183 * unable to unwind through a function which does not otherwise save its lr.
186 stmdb sp!, {r0-r3, lr}
187 UNWIND(.save {r0-r3, lr})
190 .macro mcount_get_lr reg
195 ldmia sp!, {r0-r3, ip, lr}
199 ENTRY(__gnu_mcount_nc)
201 #ifdef CONFIG_DYNAMIC_FTRACE
209 ENDPROC(__gnu_mcount_nc)
210 EXPORT_SYMBOL(__gnu_mcount_nc)
212 #ifdef CONFIG_DYNAMIC_FTRACE
217 ENDPROC(ftrace_caller)
220 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
221 ENTRY(ftrace_graph_caller)
223 __ftrace_graph_caller
225 ENDPROC(ftrace_graph_caller)
229 .purgem mcount_get_lr
232 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
233 .globl return_to_handler
236 mov r0, fp @ frame pointer
237 bl ftrace_return_to_handler
238 mov lr, r0 @ r0 has real ret addr