2 * Split from ftrace_64.S
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/magic.h>
11 #include <asm/ppc_asm.h>
12 #include <asm/asm-offsets.h>
13 #include <asm/ftrace.h>
14 #include <asm/ppc-opcode.h>
15 #include <asm/export.h>
16 #include <asm/thread_info.h>
18 #include <asm/ptrace.h>
22 * ftrace_caller()/ftrace_regs_caller() is the function that replaces _mcount()
23 * when ftrace is active.
25 * We arrive here after a function A calls function B, and we are the trace
26 * function for B. When we enter r1 points to A's stack frame, B has not yet
27 * had a chance to allocate one yet.
29 * Additionally r2 may point either to the TOC for A, or B, depending on
30 * whether B did a TOC setup sequence before calling us.
32 * On entry the LR points back to the _mcount() call site, and r0 holds the
33 * saved LR as it was on entry to B, ie. the original return address at the
36 * Our job is to save the register state into a struct pt_regs (on the stack)
37 * and then arrange for the ftrace function to be called.
39 _GLOBAL(ftrace_regs_caller)
40 /* Save the original return address in A's stack frame */
43 /* Create our stack frame + pt_regs */
44 stdu r1,-SWITCH_FRAME_SIZE(r1)
46 /* Save all gprs to pt_regs */
51 lbz r3, PACA_FTRACE_ENABLED(r13)
58 /* Save previous stack pointer (r1) */
59 addi r8, r1, SWITCH_FRAME_SIZE
62 /* Load special regs for save below */
68 /* Get the _mcount() call site out of LR */
70 /* Save it as pt_regs->nip */
72 /* Save the read LR in pt_regs->link */
75 /* Save callee's TOC in the ABI compliant location */
77 ld r2,PACATOC(r13) /* get kernel TOC in r2 */
79 addis r3,r2,function_trace_op@toc@ha
80 addi r3,r3,function_trace_op@toc@l
83 #ifdef CONFIG_LIVEPATCH
84 mr r14,r7 /* remember old NIP */
86 /* Calculate ip from nip-4 into r3 for call below */
87 subi r3, r7, MCOUNT_INSN_SIZE
89 /* Put the original return address in r4 as parent_ip */
92 /* Save special regs */
98 /* Load &pt_regs in r6 for call below */
99 addi r6, r1 ,STACK_FRAME_OVERHEAD
101 /* ftrace_call(r3, r4, r5, r6) */
102 .globl ftrace_regs_call
107 /* Load ctr with the possibly modified NIP */
110 #ifdef CONFIG_LIVEPATCH
111 cmpd r14, r3 /* has NIP been altered? */
120 /* Restore possibly modified LR */
124 /* Restore callee's TOC */
127 /* Pop our stack frame */
128 addi r1, r1, SWITCH_FRAME_SIZE
130 #ifdef CONFIG_LIVEPATCH
131 /* Based on the cmpd above, if the NIP was altered handle livepatch */
132 bne- livepatch_handler
135 ftrace_caller_common:
136 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
137 .globl ftrace_graph_call
140 _GLOBAL(ftrace_graph_stub)
143 bctr /* jump after _mcount site */
152 addi r1, r1, SWITCH_FRAME_SIZE
156 _GLOBAL(ftrace_caller)
157 /* Save the original return address in A's stack frame */
160 /* Create our stack frame + pt_regs */
161 stdu r1, -SWITCH_FRAME_SIZE(r1)
163 /* Save all gprs to pt_regs */
166 lbz r3, PACA_FTRACE_ENABLED(r13)
170 /* Get the _mcount() call site out of LR */
174 /* Save callee's TOC in the ABI compliant location */
176 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
178 addis r3, r2, function_trace_op@toc@ha
179 addi r3, r3, function_trace_op@toc@l
182 /* Calculate ip from nip-4 into r3 for call below */
183 subi r3, r7, MCOUNT_INSN_SIZE
185 /* Put the original return address in r4 as parent_ip */
188 /* Set pt_regs to NULL */
191 /* ftrace_call(r3, r4, r5, r6) */
203 /* Restore callee's TOC */
206 /* Pop our stack frame */
207 addi r1, r1, SWITCH_FRAME_SIZE
209 /* Reload original LR */
213 /* Handle function_graph or go back */
214 b ftrace_caller_common
216 #ifdef CONFIG_LIVEPATCH
218 * This function runs in the mcount context, between two functions. As
219 * such it can only clobber registers which are volatile and used in
222 * We get here when a function A, calls another function B, but B has
223 * been live patched with a new function C.
226 * - we have no stack frame and can not allocate one
227 * - LR points back to the original caller (in A)
228 * - CTR holds the new NIP in C
229 * - r0, r11 & r12 are free
232 CURRENT_THREAD_INFO(r12, r1)
234 /* Allocate 3 x 8 bytes */
235 ld r11, TI_livepatch_sp(r12)
237 std r11, TI_livepatch_sp(r12)
239 /* Save toc & real LR on livepatch stack */
244 /* Store stack end marker */
245 lis r12, STACK_END_MAGIC@h
246 ori r12, r12, STACK_END_MAGIC@l
249 /* Put ctr in r12 for global entry and branch there */
254 * Now we are returning from the patched function to the original
255 * caller A. We are free to use r11, r12 and we can use r2 until we
259 CURRENT_THREAD_INFO(r12, r1)
261 ld r11, TI_livepatch_sp(r12)
263 /* Check stack marker hasn't been trashed */
264 lis r2, STACK_END_MAGIC@h
265 ori r2, r2, STACK_END_MAGIC@l
268 EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
270 /* Restore LR & toc from livepatch stack */
275 /* Pop livepatch stack frame */
276 CURRENT_THREAD_INFO(r12, r1)
278 std r11, TI_livepatch_sp(r12)
280 /* Return to original caller of live patched function */
282 #endif /* CONFIG_LIVEPATCH */
284 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
285 _GLOBAL(ftrace_graph_caller)
287 /* with -mprofile-kernel, parameter regs are still alive at _mcount */
297 /* Save callee's TOC in the ABI compliant location */
299 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
301 mfctr r4 /* ftrace_caller has moved local addr here */
303 mflr r3 /* ftrace_caller has restored LR from stack */
304 subi r4, r4, MCOUNT_INSN_SIZE
306 bl prepare_ftrace_return
310 * prepare_ftrace_return gives us the address we divert to.
311 * Change the LR to this.
326 /* Restore callee's TOC */
333 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */