1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Split from ftrace_64.S
6 #include <linux/export.h>
7 #include <linux/magic.h>
8 #include <asm/ppc_asm.h>
9 #include <asm/asm-offsets.h>
10 #include <asm/ftrace.h>
11 #include <asm/ppc-opcode.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 .macro ftrace_regs_entry allregs
36 /* Create our stack frame + pt_regs */
37 PPC_STLU r1,-SWITCH_FRAME_SIZE(r1)
39 /* Save all gprs to pt_regs */
44 /* Save the original return address in A's stack frame */
45 std r0, LRSAVE+SWITCH_FRAME_SIZE(r1)
47 lbz r3, PACA_FTRACE_ENABLED(r13)
56 #ifdef CONFIG_LIVEPATCH_64
61 /* Save previous stack pointer (r1) */
62 addi r8, r1, SWITCH_FRAME_SIZE
66 /* Load special regs for save below */
72 /* Clear MSR to flag as ftrace_caller versus frace_regs_caller */
76 /* Get the _mcount() call site out of LR */
78 /* Save it as pt_regs->nip */
80 /* Save the read LR in pt_regs->link */
84 /* Save callee's TOC in the ABI compliant location */
86 LOAD_PACA_TOC() /* get kernel TOC in r2 */
87 LOAD_REG_ADDR(r3, function_trace_op)
90 lis r3,function_trace_op@ha
91 lwz r5,function_trace_op@l(r3)
94 #ifdef CONFIG_LIVEPATCH_64
95 mr r14, r7 /* remember old NIP */
98 /* Calculate ip from nip-4 into r3 for call below */
99 subi r3, r7, MCOUNT_INSN_SIZE
101 /* Put the original return address in r4 as parent_ip */
104 /* Save special regs */
108 PPC_STL r10, _XER(r1)
109 PPC_STL r11, _CCR(r1)
112 /* Load &pt_regs in r6 for call below */
113 addi r6, r1, STACK_INT_FRAME_REGS
116 .macro ftrace_regs_exit allregs
117 /* Load ctr with the possibly modified NIP */
121 #ifdef CONFIG_LIVEPATCH_64
122 cmpd r14, r3 /* has NIP been altered? */
130 #ifdef CONFIG_LIVEPATCH_64
135 /* Restore possibly modified LR */
140 /* Restore callee's TOC */
144 /* Pop our stack frame */
145 addi r1, r1, SWITCH_FRAME_SIZE
147 #ifdef CONFIG_LIVEPATCH_64
148 /* Based on the cmpd above, if the NIP was altered handle livepatch */
149 bne- livepatch_handler
151 bctr /* jump after _mcount site */
154 _GLOBAL(ftrace_regs_caller)
156 /* ftrace_call(r3, r4, r5, r6) */
157 .globl ftrace_regs_call
163 _GLOBAL(ftrace_caller)
165 /* ftrace_call(r3, r4, r5, r6) */
180 addi r1, r1, SWITCH_FRAME_SIZE
185 #ifdef CONFIG_LIVEPATCH_64
187 * This function runs in the mcount context, between two functions. As
188 * such it can only clobber registers which are volatile and used in
191 * We get here when a function A, calls another function B, but B has
192 * been live patched with a new function C.
195 * - we have no stack frame and can not allocate one
196 * - LR points back to the original caller (in A)
197 * - CTR holds the new NIP in C
198 * - r0, r11 & r12 are free
201 ld r12, PACA_THREAD_INFO(r13)
203 /* Allocate 3 x 8 bytes */
204 ld r11, TI_livepatch_sp(r12)
206 std r11, TI_livepatch_sp(r12)
208 /* Save toc & real LR on livepatch stack */
213 /* Store stack end marker */
214 lis r12, STACK_END_MAGIC@h
215 ori r12, r12, STACK_END_MAGIC@l
218 /* Put ctr in r12 for global entry and branch there */
223 * Now we are returning from the patched function to the original
224 * caller A. We are free to use r11, r12 and we can use r2 until we
228 ld r12, PACA_THREAD_INFO(r13)
230 ld r11, TI_livepatch_sp(r12)
232 /* Check stack marker hasn't been trashed */
233 lis r2, STACK_END_MAGIC@h
234 ori r2, r2, STACK_END_MAGIC@l
237 EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
239 /* Restore LR & toc from livepatch stack */
244 /* Pop livepatch stack frame */
245 ld r12, PACA_THREAD_INFO(r13)
247 std r11, TI_livepatch_sp(r12)
249 /* Return to original caller of live patched function */
251 #endif /* CONFIG_LIVEPATCH */
253 #ifndef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY
256 EXPORT_SYMBOL(_mcount)
263 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
264 _GLOBAL(return_to_handler)
265 /* need to save return values */
276 * We might be called from a module.
277 * Switch to our TOC to run inside the core kernel.
286 bl ftrace_return_to_handler
289 /* return value has real return address */
304 /* Jump back to real return address */
306 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
308 .pushsection ".tramp.ftrace.text","aw",@progbits;
309 .globl ftrace_tramp_text
314 .pushsection ".tramp.ftrace.init","aw",@progbits;
315 .globl ftrace_tramp_init