1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Split from ftrace_64.S
6 #include <linux/magic.h>
7 #include <asm/ppc_asm.h>
8 #include <asm/asm-offsets.h>
9 #include <asm/ftrace.h>
10 #include <asm/ppc-opcode.h>
11 #include <asm/export.h>
12 #include <asm/thread_info.h>
14 #include <asm/ptrace.h>
18 * ftrace_caller()/ftrace_regs_caller() is the function that replaces _mcount()
19 * when ftrace is active.
21 * We arrive here after a function A calls function B, and we are the trace
22 * function for B. When we enter r1 points to A's stack frame, B has not yet
23 * had a chance to allocate one yet.
25 * Additionally r2 may point either to the TOC for A, or B, depending on
26 * whether B did a TOC setup sequence before calling us.
28 * On entry the LR points back to the _mcount() call site, and r0 holds the
29 * saved LR as it was on entry to B, ie. the original return address at the
32 * Our job is to save the register state into a struct pt_regs (on the stack)
33 * and then arrange for the ftrace function to be called.
35 _GLOBAL(ftrace_regs_caller)
36 /* Save the original return address in A's stack frame */
39 /* Create our stack frame + pt_regs */
40 stdu r1,-SWITCH_FRAME_SIZE(r1)
42 /* Save all gprs to pt_regs */
47 lbz r3, PACA_FTRACE_ENABLED(r13)
54 /* Save previous stack pointer (r1) */
55 addi r8, r1, SWITCH_FRAME_SIZE
58 /* Load special regs for save below */
64 /* Get the _mcount() call site out of LR */
66 /* Save it as pt_regs->nip */
68 /* Save the read LR in pt_regs->link */
71 /* Save callee's TOC in the ABI compliant location */
73 ld r2,PACATOC(r13) /* get kernel TOC in r2 */
75 addis r3,r2,function_trace_op@toc@ha
76 addi r3,r3,function_trace_op@toc@l
79 #ifdef CONFIG_LIVEPATCH
80 mr r14,r7 /* remember old NIP */
82 /* Calculate ip from nip-4 into r3 for call below */
83 subi r3, r7, MCOUNT_INSN_SIZE
85 /* Put the original return address in r4 as parent_ip */
88 /* Save special regs */
94 /* Load &pt_regs in r6 for call below */
95 addi r6, r1 ,STACK_FRAME_OVERHEAD
97 /* ftrace_call(r3, r4, r5, r6) */
98 .globl ftrace_regs_call
103 /* Load ctr with the possibly modified NIP */
106 #ifdef CONFIG_LIVEPATCH
107 cmpd r14, r3 /* has NIP been altered? */
116 /* Restore possibly modified LR */
120 /* Restore callee's TOC */
123 /* Pop our stack frame */
124 addi r1, r1, SWITCH_FRAME_SIZE
126 #ifdef CONFIG_LIVEPATCH
127 /* Based on the cmpd above, if the NIP was altered handle livepatch */
128 bne- livepatch_handler
131 ftrace_caller_common:
132 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
133 .globl ftrace_graph_call
136 _GLOBAL(ftrace_graph_stub)
139 bctr /* jump after _mcount site */
148 addi r1, r1, SWITCH_FRAME_SIZE
152 _GLOBAL(ftrace_caller)
153 /* Save the original return address in A's stack frame */
156 /* Create our stack frame + pt_regs */
157 stdu r1, -SWITCH_FRAME_SIZE(r1)
159 /* Save all gprs to pt_regs */
162 lbz r3, PACA_FTRACE_ENABLED(r13)
166 /* Get the _mcount() call site out of LR */
170 /* Save callee's TOC in the ABI compliant location */
172 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
174 addis r3, r2, function_trace_op@toc@ha
175 addi r3, r3, function_trace_op@toc@l
178 /* Calculate ip from nip-4 into r3 for call below */
179 subi r3, r7, MCOUNT_INSN_SIZE
181 /* Put the original return address in r4 as parent_ip */
184 /* Set pt_regs to NULL */
187 /* ftrace_call(r3, r4, r5, r6) */
199 /* Restore callee's TOC */
202 /* Pop our stack frame */
203 addi r1, r1, SWITCH_FRAME_SIZE
205 /* Reload original LR */
209 /* Handle function_graph or go back */
210 b ftrace_caller_common
212 #ifdef CONFIG_LIVEPATCH
214 * This function runs in the mcount context, between two functions. As
215 * such it can only clobber registers which are volatile and used in
218 * We get here when a function A, calls another function B, but B has
219 * been live patched with a new function C.
222 * - we have no stack frame and can not allocate one
223 * - LR points back to the original caller (in A)
224 * - CTR holds the new NIP in C
225 * - r0, r11 & r12 are free
228 ld r12, PACA_THREAD_INFO(r13)
230 /* Allocate 3 x 8 bytes */
231 ld r11, TI_livepatch_sp(r12)
233 std r11, TI_livepatch_sp(r12)
235 /* Save toc & real LR on livepatch stack */
240 /* Store stack end marker */
241 lis r12, STACK_END_MAGIC@h
242 ori r12, r12, STACK_END_MAGIC@l
245 /* Put ctr in r12 for global entry and branch there */
250 * Now we are returning from the patched function to the original
251 * caller A. We are free to use r11, r12 and we can use r2 until we
255 ld r12, PACA_THREAD_INFO(r13)
257 ld r11, TI_livepatch_sp(r12)
259 /* Check stack marker hasn't been trashed */
260 lis r2, STACK_END_MAGIC@h
261 ori r2, r2, STACK_END_MAGIC@l
264 EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
266 /* Restore LR & toc from livepatch stack */
271 /* Pop livepatch stack frame */
272 ld r12, PACA_THREAD_INFO(r13)
274 std r11, TI_livepatch_sp(r12)
276 /* Return to original caller of live patched function */
278 #endif /* CONFIG_LIVEPATCH */
280 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
281 _GLOBAL(ftrace_graph_caller)
283 /* with -mprofile-kernel, parameter regs are still alive at _mcount */
293 /* Save callee's TOC in the ABI compliant location */
295 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
298 mfctr r4 /* ftrace_caller has moved local addr here */
300 mflr r3 /* ftrace_caller has restored LR from stack */
301 subi r4, r4, MCOUNT_INSN_SIZE
303 bl prepare_ftrace_return
307 * prepare_ftrace_return gives us the address we divert to.
308 * Change the LR to this.
323 /* Restore callee's TOC */
330 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */