Merge remote-tracking branch 'asoc/topic/dapm' into asoc-next
[sfrench/cifs-2.6.git] / arch / powerpc / kernel / trace / ftrace_64_mprofile.S
1 /*
2  * Split from ftrace_64.S
3  *
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.
8  */
9
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>
17 #include <asm/bug.h>
18 #include <asm/ptrace.h>
19
20 #ifdef CONFIG_DYNAMIC_FTRACE
21 /*
22  *
23  * ftrace_caller() is the function that replaces _mcount() when ftrace is
24  * active.
25  *
26  * We arrive here after a function A calls function B, and we are the trace
27  * function for B. When we enter r1 points to A's stack frame, B has not yet
28  * had a chance to allocate one yet.
29  *
30  * Additionally r2 may point either to the TOC for A, or B, depending on
31  * whether B did a TOC setup sequence before calling us.
32  *
33  * On entry the LR points back to the _mcount() call site, and r0 holds the
34  * saved LR as it was on entry to B, ie. the original return address at the
35  * call site in A.
36  *
37  * Our job is to save the register state into a struct pt_regs (on the stack)
38  * and then arrange for the ftrace function to be called.
39  */
40 _GLOBAL(ftrace_caller)
41         /* Save the original return address in A's stack frame */
42         std     r0,LRSAVE(r1)
43
44         /* Create our stack frame + pt_regs */
45         stdu    r1,-SWITCH_FRAME_SIZE(r1)
46
47         /* Save all gprs to pt_regs */
48         SAVE_GPR(0, r1)
49         SAVE_10GPRS(2, r1)
50         SAVE_10GPRS(12, r1)
51         SAVE_10GPRS(22, r1)
52
53         /* Save previous stack pointer (r1) */
54         addi    r8, r1, SWITCH_FRAME_SIZE
55         std     r8, GPR1(r1)
56
57         /* Load special regs for save below */
58         mfmsr   r8
59         mfctr   r9
60         mfxer   r10
61         mfcr    r11
62
63         /* Get the _mcount() call site out of LR */
64         mflr    r7
65         /* Save it as pt_regs->nip */
66         std     r7, _NIP(r1)
67         /* Save the read LR in pt_regs->link */
68         std     r0, _LINK(r1)
69
70         /* Save callee's TOC in the ABI compliant location */
71         std     r2, 24(r1)
72         ld      r2,PACATOC(r13) /* get kernel TOC in r2 */
73
74         addis   r3,r2,function_trace_op@toc@ha
75         addi    r3,r3,function_trace_op@toc@l
76         ld      r5,0(r3)
77
78 #ifdef CONFIG_LIVEPATCH
79         mr      r14,r7          /* remember old NIP */
80 #endif
81         /* Calculate ip from nip-4 into r3 for call below */
82         subi    r3, r7, MCOUNT_INSN_SIZE
83
84         /* Put the original return address in r4 as parent_ip */
85         mr      r4, r0
86
87         /* Save special regs */
88         std     r8, _MSR(r1)
89         std     r9, _CTR(r1)
90         std     r10, _XER(r1)
91         std     r11, _CCR(r1)
92
93         /* Load &pt_regs in r6 for call below */
94         addi    r6, r1 ,STACK_FRAME_OVERHEAD
95
96         /* ftrace_call(r3, r4, r5, r6) */
97 .globl ftrace_call
98 ftrace_call:
99         bl      ftrace_stub
100         nop
101
102         /* Load the possibly modified NIP */
103         ld      r15, _NIP(r1)
104
105 #ifdef CONFIG_LIVEPATCH
106         cmpd    r14, r15        /* has NIP been altered? */
107 #endif
108
109 #if defined(CONFIG_LIVEPATCH) && defined(CONFIG_KPROBES_ON_FTRACE)
110         /* NIP has not been altered, skip over further checks */
111         beq     1f
112
113         /* Check if there is an active kprobe on us */
114         subi    r3, r14, 4
115         bl      is_current_kprobe_addr
116         nop
117
118         /*
119          * If r3 == 1, then this is a kprobe/jprobe.
120          * else, this is livepatched function.
121          *
122          * The conditional branch for livepatch_handler below will use the
123          * result of this comparison. For kprobe/jprobe, we just need to branch to
124          * the new NIP, not call livepatch_handler. The branch below is bne, so we
125          * want CR0[EQ] to be true if this is a kprobe/jprobe. Which means we want
126          * CR0[EQ] = (r3 == 1).
127          */
128         cmpdi   r3, 1
129 1:
130 #endif
131
132         /* Load CTR with the possibly modified NIP */
133         mtctr   r15
134
135         /* Restore gprs */
136         REST_GPR(0,r1)
137         REST_10GPRS(2,r1)
138         REST_10GPRS(12,r1)
139         REST_10GPRS(22,r1)
140
141         /* Restore possibly modified LR */
142         ld      r0, _LINK(r1)
143         mtlr    r0
144
145         /* Restore callee's TOC */
146         ld      r2, 24(r1)
147
148         /* Pop our stack frame */
149         addi r1, r1, SWITCH_FRAME_SIZE
150
151 #ifdef CONFIG_LIVEPATCH
152         /*
153          * Based on the cmpd or cmpdi above, if the NIP was altered and we're
154          * not on a kprobe/jprobe, then handle livepatch.
155          */
156         bne-    livepatch_handler
157 #endif
158
159 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
160 .globl ftrace_graph_call
161 ftrace_graph_call:
162         b       ftrace_graph_stub
163 _GLOBAL(ftrace_graph_stub)
164 #endif
165
166         bctr                    /* jump after _mcount site */
167
168 _GLOBAL(ftrace_stub)
169         blr
170
171 #ifdef CONFIG_LIVEPATCH
172         /*
173          * This function runs in the mcount context, between two functions. As
174          * such it can only clobber registers which are volatile and used in
175          * function linkage.
176          *
177          * We get here when a function A, calls another function B, but B has
178          * been live patched with a new function C.
179          *
180          * On entry:
181          *  - we have no stack frame and can not allocate one
182          *  - LR points back to the original caller (in A)
183          *  - CTR holds the new NIP in C
184          *  - r0 & r12 are free
185          *
186          * r0 can't be used as the base register for a DS-form load or store, so
187          * we temporarily shuffle r1 (stack pointer) into r0 and then put it back.
188          */
189 livepatch_handler:
190         CURRENT_THREAD_INFO(r12, r1)
191
192         /* Save stack pointer into r0 */
193         mr      r0, r1
194
195         /* Allocate 3 x 8 bytes */
196         ld      r1, TI_livepatch_sp(r12)
197         addi    r1, r1, 24
198         std     r1, TI_livepatch_sp(r12)
199
200         /* Save toc & real LR on livepatch stack */
201         std     r2,  -24(r1)
202         mflr    r12
203         std     r12, -16(r1)
204
205         /* Store stack end marker */
206         lis     r12, STACK_END_MAGIC@h
207         ori     r12, r12, STACK_END_MAGIC@l
208         std     r12, -8(r1)
209
210         /* Restore real stack pointer */
211         mr      r1, r0
212
213         /* Put ctr in r12 for global entry and branch there */
214         mfctr   r12
215         bctrl
216
217         /*
218          * Now we are returning from the patched function to the original
219          * caller A. We are free to use r0 and r12, and we can use r2 until we
220          * restore it.
221          */
222
223         CURRENT_THREAD_INFO(r12, r1)
224
225         /* Save stack pointer into r0 */
226         mr      r0, r1
227
228         ld      r1, TI_livepatch_sp(r12)
229
230         /* Check stack marker hasn't been trashed */
231         lis     r2,  STACK_END_MAGIC@h
232         ori     r2,  r2, STACK_END_MAGIC@l
233         ld      r12, -8(r1)
234 1:      tdne    r12, r2
235         EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
236
237         /* Restore LR & toc from livepatch stack */
238         ld      r12, -16(r1)
239         mtlr    r12
240         ld      r2,  -24(r1)
241
242         /* Pop livepatch stack frame */
243         CURRENT_THREAD_INFO(r12, r0)
244         subi    r1, r1, 24
245         std     r1, TI_livepatch_sp(r12)
246
247         /* Restore real stack pointer */
248         mr      r1, r0
249
250         /* Return to original caller of live patched function */
251         blr
252 #endif /* CONFIG_LIVEPATCH */
253
254 #endif /* CONFIG_DYNAMIC_FTRACE */
255
256 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
257 _GLOBAL(ftrace_graph_caller)
258         stdu    r1, -112(r1)
259         /* with -mprofile-kernel, parameter regs are still alive at _mcount */
260         std     r10, 104(r1)
261         std     r9, 96(r1)
262         std     r8, 88(r1)
263         std     r7, 80(r1)
264         std     r6, 72(r1)
265         std     r5, 64(r1)
266         std     r4, 56(r1)
267         std     r3, 48(r1)
268
269         /* Save callee's TOC in the ABI compliant location */
270         std     r2, 24(r1)
271         ld      r2, PACATOC(r13)        /* get kernel TOC in r2 */
272
273         mfctr   r4              /* ftrace_caller has moved local addr here */
274         std     r4, 40(r1)
275         mflr    r3              /* ftrace_caller has restored LR from stack */
276         subi    r4, r4, MCOUNT_INSN_SIZE
277
278         bl      prepare_ftrace_return
279         nop
280
281         /*
282          * prepare_ftrace_return gives us the address we divert to.
283          * Change the LR to this.
284          */
285         mtlr    r3
286
287         ld      r0, 40(r1)
288         mtctr   r0
289         ld      r10, 104(r1)
290         ld      r9, 96(r1)
291         ld      r8, 88(r1)
292         ld      r7, 80(r1)
293         ld      r6, 72(r1)
294         ld      r5, 64(r1)
295         ld      r4, 56(r1)
296         ld      r3, 48(r1)
297
298         /* Restore callee's TOC */
299         ld      r2, 24(r1)
300
301         addi    r1, r1, 112
302         mflr    r0
303         std     r0, LRSAVE(r1)
304         bctr
305 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */