Merge tag 'for-4.18-rc5-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[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 /*
21  *
22  * ftrace_caller()/ftrace_regs_caller() is the function that replaces _mcount()
23  * when ftrace is active.
24  *
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.
28  *
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.
31  *
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
34  * call site in A.
35  *
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.
38  */
39 _GLOBAL(ftrace_regs_caller)
40         /* Save the original return address in A's stack frame */
41         std     r0,LRSAVE(r1)
42
43         /* Create our stack frame + pt_regs */
44         stdu    r1,-SWITCH_FRAME_SIZE(r1)
45
46         /* Save all gprs to pt_regs */
47         SAVE_GPR(0, r1)
48         SAVE_10GPRS(2, r1)
49
50         /* Ok to continue? */
51         lbz     r3, PACA_FTRACE_ENABLED(r13)
52         cmpdi   r3, 0
53         beq     ftrace_no_trace
54
55         SAVE_10GPRS(12, r1)
56         SAVE_10GPRS(22, r1)
57
58         /* Save previous stack pointer (r1) */
59         addi    r8, r1, SWITCH_FRAME_SIZE
60         std     r8, GPR1(r1)
61
62         /* Load special regs for save below */
63         mfmsr   r8
64         mfctr   r9
65         mfxer   r10
66         mfcr    r11
67
68         /* Get the _mcount() call site out of LR */
69         mflr    r7
70         /* Save it as pt_regs->nip */
71         std     r7, _NIP(r1)
72         /* Save the read LR in pt_regs->link */
73         std     r0, _LINK(r1)
74
75         /* Save callee's TOC in the ABI compliant location */
76         std     r2, 24(r1)
77         ld      r2,PACATOC(r13) /* get kernel TOC in r2 */
78
79         addis   r3,r2,function_trace_op@toc@ha
80         addi    r3,r3,function_trace_op@toc@l
81         ld      r5,0(r3)
82
83 #ifdef CONFIG_LIVEPATCH
84         mr      r14,r7          /* remember old NIP */
85 #endif
86         /* Calculate ip from nip-4 into r3 for call below */
87         subi    r3, r7, MCOUNT_INSN_SIZE
88
89         /* Put the original return address in r4 as parent_ip */
90         mr      r4, r0
91
92         /* Save special regs */
93         std     r8, _MSR(r1)
94         std     r9, _CTR(r1)
95         std     r10, _XER(r1)
96         std     r11, _CCR(r1)
97
98         /* Load &pt_regs in r6 for call below */
99         addi    r6, r1 ,STACK_FRAME_OVERHEAD
100
101         /* ftrace_call(r3, r4, r5, r6) */
102 .globl ftrace_regs_call
103 ftrace_regs_call:
104         bl      ftrace_stub
105         nop
106
107         /* Load the possibly modified NIP */
108         ld      r15, _NIP(r1)
109
110 #ifdef CONFIG_LIVEPATCH
111         cmpd    r14, r15        /* has NIP been altered? */
112 #endif
113
114 #if defined(CONFIG_LIVEPATCH) && defined(CONFIG_KPROBES_ON_FTRACE)
115         /* NIP has not been altered, skip over further checks */
116         beq     1f
117
118         /* Check if there is an active jprobe on us */
119         subi    r3, r14, 4
120         bl      __is_active_jprobe
121         nop
122
123         /*
124          * If r3 == 1, then this is a kprobe/jprobe.
125          * else, this is livepatched function.
126          *
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).
132          */
133         cmpdi   r3, 1
134 1:
135 #endif
136
137         /* Load CTR with the possibly modified NIP */
138         mtctr   r15
139
140         /* Restore gprs */
141         REST_GPR(0,r1)
142         REST_10GPRS(2,r1)
143         REST_10GPRS(12,r1)
144         REST_10GPRS(22,r1)
145
146         /* Restore possibly modified LR */
147         ld      r0, _LINK(r1)
148         mtlr    r0
149
150         /* Restore callee's TOC */
151         ld      r2, 24(r1)
152
153         /* Pop our stack frame */
154         addi r1, r1, SWITCH_FRAME_SIZE
155
156 #ifdef CONFIG_LIVEPATCH
157         /*
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.
160          */
161         bne-    livepatch_handler
162 #endif
163
164 ftrace_caller_common:
165 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
166 .globl ftrace_graph_call
167 ftrace_graph_call:
168         b       ftrace_graph_stub
169 _GLOBAL(ftrace_graph_stub)
170 #endif
171
172         bctr                    /* jump after _mcount site */
173
174 _GLOBAL(ftrace_stub)
175         blr
176
177 ftrace_no_trace:
178         mflr    r3
179         mtctr   r3
180         REST_GPR(3, r1)
181         addi    r1, r1, SWITCH_FRAME_SIZE
182         mtlr    r0
183         bctr
184
185 _GLOBAL(ftrace_caller)
186         /* Save the original return address in A's stack frame */
187         std     r0, LRSAVE(r1)
188
189         /* Create our stack frame + pt_regs */
190         stdu    r1, -SWITCH_FRAME_SIZE(r1)
191
192         /* Save all gprs to pt_regs */
193         SAVE_8GPRS(3, r1)
194
195         lbz     r3, PACA_FTRACE_ENABLED(r13)
196         cmpdi   r3, 0
197         beq     ftrace_no_trace
198
199         /* Get the _mcount() call site out of LR */
200         mflr    r7
201         std     r7, _NIP(r1)
202
203         /* Save callee's TOC in the ABI compliant location */
204         std     r2, 24(r1)
205         ld      r2, PACATOC(r13)        /* get kernel TOC in r2 */
206
207         addis   r3, r2, function_trace_op@toc@ha
208         addi    r3, r3, function_trace_op@toc@l
209         ld      r5, 0(r3)
210
211         /* Calculate ip from nip-4 into r3 for call below */
212         subi    r3, r7, MCOUNT_INSN_SIZE
213
214         /* Put the original return address in r4 as parent_ip */
215         mr      r4, r0
216
217         /* Set pt_regs to NULL */
218         li      r6, 0
219
220         /* ftrace_call(r3, r4, r5, r6) */
221 .globl ftrace_call
222 ftrace_call:
223         bl      ftrace_stub
224         nop
225
226         ld      r3, _NIP(r1)
227         mtctr   r3
228
229         /* Restore gprs */
230         REST_8GPRS(3,r1)
231
232         /* Restore callee's TOC */
233         ld      r2, 24(r1)
234
235         /* Pop our stack frame */
236         addi    r1, r1, SWITCH_FRAME_SIZE
237
238         /* Reload original LR */
239         ld      r0, LRSAVE(r1)
240         mtlr    r0
241
242         /* Handle function_graph or go back */
243         b       ftrace_caller_common
244
245 #ifdef CONFIG_LIVEPATCH
246         /*
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
249          * function linkage.
250          *
251          * We get here when a function A, calls another function B, but B has
252          * been live patched with a new function C.
253          *
254          * On entry:
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
259          */
260 livepatch_handler:
261         CURRENT_THREAD_INFO(r12, r1)
262
263         /* Allocate 3 x 8 bytes */
264         ld      r11, TI_livepatch_sp(r12)
265         addi    r11, r11, 24
266         std     r11, TI_livepatch_sp(r12)
267
268         /* Save toc & real LR on livepatch stack */
269         std     r2,  -24(r11)
270         mflr    r12
271         std     r12, -16(r11)
272
273         /* Store stack end marker */
274         lis     r12, STACK_END_MAGIC@h
275         ori     r12, r12, STACK_END_MAGIC@l
276         std     r12, -8(r11)
277
278         /* Put ctr in r12 for global entry and branch there */
279         mfctr   r12
280         bctrl
281
282         /*
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
285          * restore it.
286          */
287
288         CURRENT_THREAD_INFO(r12, r1)
289
290         ld      r11, TI_livepatch_sp(r12)
291
292         /* Check stack marker hasn't been trashed */
293         lis     r2,  STACK_END_MAGIC@h
294         ori     r2,  r2, STACK_END_MAGIC@l
295         ld      r12, -8(r11)
296 1:      tdne    r12, r2
297         EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
298
299         /* Restore LR & toc from livepatch stack */
300         ld      r12, -16(r11)
301         mtlr    r12
302         ld      r2,  -24(r11)
303
304         /* Pop livepatch stack frame */
305         CURRENT_THREAD_INFO(r12, r1)
306         subi    r11, r11, 24
307         std     r11, TI_livepatch_sp(r12)
308
309         /* Return to original caller of live patched function */
310         blr
311 #endif /* CONFIG_LIVEPATCH */
312
313 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
314 _GLOBAL(ftrace_graph_caller)
315         stdu    r1, -112(r1)
316         /* with -mprofile-kernel, parameter regs are still alive at _mcount */
317         std     r10, 104(r1)
318         std     r9, 96(r1)
319         std     r8, 88(r1)
320         std     r7, 80(r1)
321         std     r6, 72(r1)
322         std     r5, 64(r1)
323         std     r4, 56(r1)
324         std     r3, 48(r1)
325
326         /* Save callee's TOC in the ABI compliant location */
327         std     r2, 24(r1)
328         ld      r2, PACATOC(r13)        /* get kernel TOC in r2 */
329
330         mfctr   r4              /* ftrace_caller has moved local addr here */
331         std     r4, 40(r1)
332         mflr    r3              /* ftrace_caller has restored LR from stack */
333         subi    r4, r4, MCOUNT_INSN_SIZE
334
335         bl      prepare_ftrace_return
336         nop
337
338         /*
339          * prepare_ftrace_return gives us the address we divert to.
340          * Change the LR to this.
341          */
342         mtlr    r3
343
344         ld      r0, 40(r1)
345         mtctr   r0
346         ld      r10, 104(r1)
347         ld      r9, 96(r1)
348         ld      r8, 88(r1)
349         ld      r7, 80(r1)
350         ld      r6, 72(r1)
351         ld      r5, 64(r1)
352         ld      r4, 56(r1)
353         ld      r3, 48(r1)
354
355         /* Restore callee's TOC */
356         ld      r2, 24(r1)
357
358         addi    r1, r1, 112
359         mflr    r0
360         std     r0, LRSAVE(r1)
361         bctr
362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */