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>
11 #include "entry-header.S"
14 * When compiling with -pg, gcc inserts a call to the mcount routine at the
15 * start of every function. In mcount, apart from the function's address (in
16 * lr), we need to get hold of the function's caller's address.
18 * Older GCCs (pre-4.4) inserted a call to a routine called mcount like this:
22 * These versions have the limitation that in order for the mcount routine to
23 * be able to determine the function's caller's address, an APCS-style frame
24 * pointer (which is set up with something like the code below) is required.
27 * push {fp, ip, lr, pc}
30 * With EABI, these frame pointers are not available unless -mapcs-frame is
31 * specified, and if building as Thumb-2, not even then.
33 * Newer GCCs (4.4+) solve this problem by introducing a new version of mcount,
34 * with call sites like:
39 * With these compilers, frame pointers are not necessary.
41 * mcount can be thought of as a function called in the middle of a subroutine
42 * call. As such, it needs to be transparent for both the caller and the
43 * callee: the original lr needs to be restored when leaving mcount, and no
44 * registers should be clobbered. (In the __gnu_mcount_nc implementation, we
45 * clobber the ip register. This is OK because the ARM calling convention
46 * allows it to be clobbered in subroutines and doesn't use it to hold
49 * When using dynamic ftrace, we patch out the mcount call by a "mov r0, r0"
50 * for the mcount case, and a "pop {lr}" for the __gnu_mcount_nc case (see
51 * arch/arm/kernel/ftrace.c).
54 #ifndef CONFIG_OLD_MCOUNT
55 #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 4))
56 #error Ftrace requires CONFIG_FRAME_POINTER=y with GCC older than 4.4.0.
60 .macro mcount_adjust_addr rd, rn
61 bic \rd, \rn, #1 @ clear the Thumb bit if present
62 sub \rd, \rd, #MCOUNT_INSN_SIZE
65 .macro __mcount suffix
67 ldr r0, =ftrace_trace_function
73 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
74 ldr r1, =ftrace_graph_return
77 bne ftrace_graph_caller\suffix
79 ldr r1, =ftrace_graph_entry
81 ldr r0, =ftrace_graph_entry_stub
83 bne ftrace_graph_caller\suffix
88 1: mcount_get_lr r1 @ lr of instrumented func
89 mcount_adjust_addr r0, lr @ instrumented function
95 .macro __ftrace_caller suffix
98 mcount_get_lr r1 @ lr of instrumented func
99 mcount_adjust_addr r0, lr @ instrumented function
101 .globl ftrace_call\suffix
105 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
106 .globl ftrace_graph_call\suffix
107 ftrace_graph_call\suffix:
114 .macro __ftrace_graph_caller
115 sub r0, fp, #4 @ &lr of instrumented routine (&parent)
116 #ifdef CONFIG_DYNAMIC_FTRACE
117 @ called from __ftrace_caller, saved in mcount_enter
118 ldr r1, [sp, #16] @ instrumented routine (func)
119 mcount_adjust_addr r1, r1
121 @ called from __mcount, untouched in lr
122 mcount_adjust_addr r1, lr @ instrumented routine (func)
124 mov r2, fp @ frame pointer
125 bl prepare_ftrace_return
129 #ifdef CONFIG_OLD_MCOUNT
135 stmdb sp!, {r0-r3, lr}
138 .macro mcount_get_lr reg
144 ldmia sp!, {r0-r3, pc}
148 #ifdef CONFIG_DYNAMIC_FTRACE
157 #ifdef CONFIG_DYNAMIC_FTRACE
158 ENTRY(ftrace_caller_old)
160 ENDPROC(ftrace_caller_old)
163 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
164 ENTRY(ftrace_graph_caller_old)
165 __ftrace_graph_caller
166 ENDPROC(ftrace_graph_caller_old)
170 .purgem mcount_get_lr
180 * This pad compensates for the push {lr} at the call site. Note that we are
181 * unable to unwind through a function which does not otherwise save its lr.
184 stmdb sp!, {r0-r3, lr}
185 UNWIND(.save {r0-r3, lr})
188 .macro mcount_get_lr reg
193 ldmia sp!, {r0-r3, ip, lr}
197 ENTRY(__gnu_mcount_nc)
199 #ifdef CONFIG_DYNAMIC_FTRACE
207 ENDPROC(__gnu_mcount_nc)
209 #ifdef CONFIG_DYNAMIC_FTRACE
214 ENDPROC(ftrace_caller)
217 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
218 ENTRY(ftrace_graph_caller)
220 __ftrace_graph_caller
222 ENDPROC(ftrace_graph_caller)
226 .purgem mcount_get_lr
229 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
230 .globl return_to_handler
233 mov r0, fp @ frame pointer
234 bl ftrace_return_to_handler
235 mov lr, r0 @ r0 has real ret addr