1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm64/kernel/entry-ftrace.S
5 * Copyright (C) 2013 Linaro Limited
6 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
9 #include <linux/linkage.h>
10 #include <asm/assembler.h>
11 #include <asm/ftrace.h>
15 * Gcc with -pg will put the following code in the beginning of each function:
18 * [function's body ...]
19 * "bl _mcount" may be replaced to "bl ftrace_caller" or NOP if dynamic
22 * Please note that x0 as an argument will not be used here because we can
23 * get lr(x30) of instrumented function at any time by winding up call stack
24 * as long as the kernel is compiled without -fomit-frame-pointer.
25 * (or CONFIG_FRAME_POINTER, this is forced on arm64)
27 * stack layout after mcount_enter in _mcount():
29 * current sp/fp => 0:+-----+
30 * in _mcount() | x29 | -> instrumented function's fp
32 * | x30 | -> _mcount()'s lr (= instrumented function's pc)
33 * old sp => +16:+-----+
34 * when instrumented | |
35 * function calls | ... |
38 * instrumented => +xx:+-----+
39 * function's fp | x29 | -> parent's fp
41 * | x30 | -> instrumented function's lr (= parent's pc)
47 stp x29, x30, [sp, #-16]!
52 ldp x29, x30, [sp], #16
56 .macro mcount_adjust_addr rd, rn
57 sub \rd, \rn, #AARCH64_INSN_SIZE
60 /* for instrumented function's parent */
61 .macro mcount_get_parent_fp reg
66 /* for instrumented function */
67 .macro mcount_get_pc0 reg
68 mcount_adjust_addr \reg, x30
71 .macro mcount_get_pc reg
73 mcount_adjust_addr \reg, \reg
76 .macro mcount_get_lr reg
81 .macro mcount_get_lr_addr reg
86 #ifndef CONFIG_DYNAMIC_FTRACE
88 * void _mcount(unsigned long return_address)
89 * @return_address: return address to instrumented function
91 * This function makes calls, if enabled, to:
92 * - tracer function to probe instrumented function's entry,
93 * - ftrace_graph_caller to set up an exit hook
98 ldr_l x2, ftrace_trace_function
100 cmp x0, x2 // if (ftrace_trace_function
101 b.eq skip_ftrace_call // != ftrace_stub) {
103 mcount_get_pc x0 // function's pc
104 mcount_get_lr x1 // function's lr (= parent's pc)
105 blr x2 // (*ftrace_trace_function)(pc, lr);
107 skip_ftrace_call: // }
108 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
109 ldr_l x2, ftrace_graph_return
110 cmp x0, x2 // if ((ftrace_graph_return
111 b.ne ftrace_graph_caller // != ftrace_stub)
113 ldr_l x2, ftrace_graph_entry // || (ftrace_graph_entry
114 adr_l x0, ftrace_graph_entry_stub // != ftrace_graph_entry_stub))
116 b.ne ftrace_graph_caller // ftrace_graph_caller();
117 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
120 EXPORT_SYMBOL(_mcount)
123 #else /* CONFIG_DYNAMIC_FTRACE */
125 * _mcount() is used to build the kernel with -pg option, but all the branch
126 * instructions to _mcount() are replaced to NOP initially at kernel start up,
127 * and later on, NOP to branch to ftrace_caller() when enabled or branch to
128 * NOP when disabled per-function base.
133 EXPORT_SYMBOL(_mcount)
137 * void ftrace_caller(unsigned long return_address)
138 * @return_address: return address to instrumented function
140 * This function is a counterpart of _mcount() in 'static' ftrace, and
142 * - tracer function to probe instrumented function's entry,
143 * - ftrace_graph_caller to set up an exit hook
148 mcount_get_pc0 x0 // function's pc
149 mcount_get_lr x1 // function's lr
151 GLOBAL(ftrace_call) // tracer(pc, lr);
152 nop // This will be replaced with "bl xxx"
153 // where xxx can be any kind of tracer.
155 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
156 GLOBAL(ftrace_graph_call) // ftrace_graph_caller();
157 nop // If enabled, this will be replaced
158 // "b ftrace_graph_caller"
162 ENDPROC(ftrace_caller)
163 #endif /* CONFIG_DYNAMIC_FTRACE */
169 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
171 * void ftrace_graph_caller(void)
173 * Called from _mcount() or ftrace_caller() when function_graph tracer is
175 * This function w/ prepare_ftrace_return() fakes link register's value on
176 * the call stack in order to intercept instrumented function's return path
177 * and run return_to_handler() later on its exit.
179 ENTRY(ftrace_graph_caller)
180 mcount_get_pc x0 // function's pc
181 mcount_get_lr_addr x1 // pointer to function's saved lr
182 mcount_get_parent_fp x2 // parent's fp
183 bl prepare_ftrace_return // prepare_ftrace_return(pc, &lr, fp)
186 ENDPROC(ftrace_graph_caller)
189 * void return_to_handler(void)
191 * Run ftrace_return_to_handler() before going back to parent.
192 * @fp is checked against the value passed by ftrace_graph_caller().
194 ENTRY(return_to_handler)
195 /* save return value regs */
198 stp x2, x3, [sp, #16]
199 stp x4, x5, [sp, #32]
200 stp x6, x7, [sp, #48]
202 mov x0, x29 // parent's fp
203 bl ftrace_return_to_handler// addr = ftrace_return_to_hander(fp);
204 mov x30, x0 // restore the original return address
206 /* restore return value regs */
208 ldp x2, x3, [sp, #16]
209 ldp x4, x5, [sp, #32]
210 ldp x6, x7, [sp, #48]
214 END(return_to_handler)
215 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */