2 * arch/arm64/kernel/entry-ftrace.S
4 * Copyright (C) 2013 Linaro Limited
5 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/linkage.h>
13 #include <asm/assembler.h>
14 #include <asm/ftrace.h>
18 * Gcc with -pg will put the following code in the beginning of each function:
21 * [function's body ...]
22 * "bl _mcount" may be replaced to "bl ftrace_caller" or NOP if dynamic
25 * Please note that x0 as an argument will not be used here because we can
26 * get lr(x30) of instrumented function at any time by winding up call stack
27 * as long as the kernel is compiled without -fomit-frame-pointer.
28 * (or CONFIG_FRAME_POINTER, this is forced on arm64)
30 * stack layout after mcount_enter in _mcount():
32 * current sp/fp => 0:+-----+
33 * in _mcount() | x29 | -> instrumented function's fp
35 * | x30 | -> _mcount()'s lr (= instrumented function's pc)
36 * old sp => +16:+-----+
37 * when instrumented | |
38 * function calls | ... |
41 * instrumented => +xx:+-----+
42 * function's fp | x29 | -> parent's fp
44 * | x30 | -> instrumented function's lr (= parent's pc)
50 stp x29, x30, [sp, #-16]!
55 ldp x29, x30, [sp], #16
59 .macro mcount_adjust_addr rd, rn
60 sub \rd, \rn, #AARCH64_INSN_SIZE
63 /* for instrumented function's parent */
64 .macro mcount_get_parent_fp reg
69 /* for instrumented function */
70 .macro mcount_get_pc0 reg
71 mcount_adjust_addr \reg, x30
74 .macro mcount_get_pc reg
76 mcount_adjust_addr \reg, \reg
79 .macro mcount_get_lr reg
84 .macro mcount_get_lr_addr reg
89 #ifndef CONFIG_DYNAMIC_FTRACE
91 * void _mcount(unsigned long return_address)
92 * @return_address: return address to instrumented function
94 * This function makes calls, if enabled, to:
95 * - tracer function to probe instrumented function's entry,
96 * - ftrace_graph_caller to set up an exit hook
101 ldr_l x2, ftrace_trace_function
103 cmp x0, x2 // if (ftrace_trace_function
104 b.eq skip_ftrace_call // != ftrace_stub) {
106 mcount_get_pc x0 // function's pc
107 mcount_get_lr x1 // function's lr (= parent's pc)
108 blr x2 // (*ftrace_trace_function)(pc, lr);
110 skip_ftrace_call: // }
111 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
112 ldr_l x2, ftrace_graph_return
113 cmp x0, x2 // if ((ftrace_graph_return
114 b.ne ftrace_graph_caller // != ftrace_stub)
116 ldr_l x2, ftrace_graph_entry // || (ftrace_graph_entry
117 adr_l x0, ftrace_graph_entry_stub // != ftrace_graph_entry_stub))
119 b.ne ftrace_graph_caller // ftrace_graph_caller();
120 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
123 EXPORT_SYMBOL(_mcount)
126 #else /* CONFIG_DYNAMIC_FTRACE */
128 * _mcount() is used to build the kernel with -pg option, but all the branch
129 * instructions to _mcount() are replaced to NOP initially at kernel start up,
130 * and later on, NOP to branch to ftrace_caller() when enabled or branch to
131 * NOP when disabled per-function base.
136 EXPORT_SYMBOL(_mcount)
140 * void ftrace_caller(unsigned long return_address)
141 * @return_address: return address to instrumented function
143 * This function is a counterpart of _mcount() in 'static' ftrace, and
145 * - tracer function to probe instrumented function's entry,
146 * - ftrace_graph_caller to set up an exit hook
151 mcount_get_pc0 x0 // function's pc
152 mcount_get_lr x1 // function's lr
154 GLOBAL(ftrace_call) // tracer(pc, lr);
155 nop // This will be replaced with "bl xxx"
156 // where xxx can be any kind of tracer.
158 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
159 GLOBAL(ftrace_graph_call) // ftrace_graph_caller();
160 nop // If enabled, this will be replaced
161 // "b ftrace_graph_caller"
165 ENDPROC(ftrace_caller)
166 #endif /* CONFIG_DYNAMIC_FTRACE */
172 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
174 * void ftrace_graph_caller(void)
176 * Called from _mcount() or ftrace_caller() when function_graph tracer is
178 * This function w/ prepare_ftrace_return() fakes link register's value on
179 * the call stack in order to intercept instrumented function's return path
180 * and run return_to_handler() later on its exit.
182 ENTRY(ftrace_graph_caller)
183 mcount_get_pc x0 // function's pc
184 mcount_get_lr_addr x1 // pointer to function's saved lr
185 mcount_get_parent_fp x2 // parent's fp
186 bl prepare_ftrace_return // prepare_ftrace_return(pc, &lr, fp)
189 ENDPROC(ftrace_graph_caller)
192 * void return_to_handler(void)
194 * Run ftrace_return_to_handler() before going back to parent.
195 * @fp is checked against the value passed by ftrace_graph_caller().
197 ENTRY(return_to_handler)
198 /* save return value regs */
201 stp x2, x3, [sp, #16]
202 stp x4, x5, [sp, #32]
203 stp x6, x7, [sp, #48]
205 mov x0, x29 // parent's fp
206 bl ftrace_return_to_handler// addr = ftrace_return_to_hander(fp);
207 mov x30, x0 // restore the original return address
209 /* restore return value regs */
211 ldp x2, x3, [sp, #16]
212 ldp x4, x5, [sp, #32]
213 ldp x6, x7, [sp, #48]
217 END(return_to_handler)
218 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */