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 the possibly modified NIP */
110 #ifdef CONFIG_LIVEPATCH
111 cmpd r14, r15 /* has NIP been altered? */
114 #if defined(CONFIG_LIVEPATCH) && defined(CONFIG_KPROBES_ON_FTRACE)
115 /* NIP has not been altered, skip over further checks */
118 /* Check if there is an active jprobe on us */
120 bl __is_active_jprobe
124 * If r3 == 1, then this is a kprobe/jprobe.
125 * else, this is livepatched function.
127 * The conditional branch for livepatch_handler below will use the
128 * result of this comparison. For kprobe/jprobe, we just need to branch to
129 * the new NIP, not call livepatch_handler. The branch below is bne, so we
130 * want CR0[EQ] to be true if this is a kprobe/jprobe. Which means we want
131 * CR0[EQ] = (r3 == 1).
137 /* Load CTR with the possibly modified NIP */
146 /* Restore possibly modified LR */
150 /* Restore callee's TOC */
153 /* Pop our stack frame */
154 addi r1, r1, SWITCH_FRAME_SIZE
156 #ifdef CONFIG_LIVEPATCH
158 * Based on the cmpd or cmpdi above, if the NIP was altered and we're
159 * not on a kprobe/jprobe, then handle livepatch.
161 bne- livepatch_handler
164 ftrace_caller_common:
165 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
166 .globl ftrace_graph_call
169 _GLOBAL(ftrace_graph_stub)
172 bctr /* jump after _mcount site */
181 addi r1, r1, SWITCH_FRAME_SIZE
185 _GLOBAL(ftrace_caller)
186 /* Save the original return address in A's stack frame */
189 /* Create our stack frame + pt_regs */
190 stdu r1, -SWITCH_FRAME_SIZE(r1)
192 /* Save all gprs to pt_regs */
195 lbz r3, PACA_FTRACE_ENABLED(r13)
199 /* Get the _mcount() call site out of LR */
203 /* Save callee's TOC in the ABI compliant location */
205 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
207 addis r3, r2, function_trace_op@toc@ha
208 addi r3, r3, function_trace_op@toc@l
211 /* Calculate ip from nip-4 into r3 for call below */
212 subi r3, r7, MCOUNT_INSN_SIZE
214 /* Put the original return address in r4 as parent_ip */
217 /* Set pt_regs to NULL */
220 /* ftrace_call(r3, r4, r5, r6) */
232 /* Restore callee's TOC */
235 /* Pop our stack frame */
236 addi r1, r1, SWITCH_FRAME_SIZE
238 /* Reload original LR */
242 /* Handle function_graph or go back */
243 b ftrace_caller_common
245 #ifdef CONFIG_LIVEPATCH
247 * This function runs in the mcount context, between two functions. As
248 * such it can only clobber registers which are volatile and used in
251 * We get here when a function A, calls another function B, but B has
252 * been live patched with a new function C.
255 * - we have no stack frame and can not allocate one
256 * - LR points back to the original caller (in A)
257 * - CTR holds the new NIP in C
258 * - r0, r11 & r12 are free
261 CURRENT_THREAD_INFO(r12, r1)
263 /* Allocate 3 x 8 bytes */
264 ld r11, TI_livepatch_sp(r12)
266 std r11, TI_livepatch_sp(r12)
268 /* Save toc & real LR on livepatch stack */
273 /* Store stack end marker */
274 lis r12, STACK_END_MAGIC@h
275 ori r12, r12, STACK_END_MAGIC@l
278 /* Put ctr in r12 for global entry and branch there */
283 * Now we are returning from the patched function to the original
284 * caller A. We are free to use r11, r12 and we can use r2 until we
288 CURRENT_THREAD_INFO(r12, r1)
290 ld r11, TI_livepatch_sp(r12)
292 /* Check stack marker hasn't been trashed */
293 lis r2, STACK_END_MAGIC@h
294 ori r2, r2, STACK_END_MAGIC@l
297 EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
299 /* Restore LR & toc from livepatch stack */
304 /* Pop livepatch stack frame */
305 CURRENT_THREAD_INFO(r12, r1)
307 std r11, TI_livepatch_sp(r12)
309 /* Return to original caller of live patched function */
311 #endif /* CONFIG_LIVEPATCH */
313 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
314 _GLOBAL(ftrace_graph_caller)
316 /* with -mprofile-kernel, parameter regs are still alive at _mcount */
326 /* Save callee's TOC in the ABI compliant location */
328 ld r2, PACATOC(r13) /* get kernel TOC in r2 */
330 mfctr r4 /* ftrace_caller has moved local addr here */
332 mflr r3 /* ftrace_caller has restored LR from stack */
333 subi r4, r4, MCOUNT_INSN_SIZE
335 bl prepare_ftrace_return
339 * prepare_ftrace_return gives us the address we divert to.
340 * Change the LR to this.
355 /* Restore callee's TOC */
362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */