Merge tag 'locks-v4.19-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton...
[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 ctr with the possibly modified NIP */
108         ld      r3, _NIP(r1)
109         mtctr   r3
110 #ifdef CONFIG_LIVEPATCH
111         cmpd    r14, r3         /* has NIP been altered? */
112 #endif
113
114         /* Restore gprs */
115         REST_GPR(0,r1)
116         REST_10GPRS(2,r1)
117         REST_10GPRS(12,r1)
118         REST_10GPRS(22,r1)
119
120         /* Restore possibly modified LR */
121         ld      r0, _LINK(r1)
122         mtlr    r0
123
124         /* Restore callee's TOC */
125         ld      r2, 24(r1)
126
127         /* Pop our stack frame */
128         addi r1, r1, SWITCH_FRAME_SIZE
129
130 #ifdef CONFIG_LIVEPATCH
131         /* Based on the cmpd above, if the NIP was altered handle livepatch */
132         bne-    livepatch_handler
133 #endif
134
135 ftrace_caller_common:
136 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
137 .globl ftrace_graph_call
138 ftrace_graph_call:
139         b       ftrace_graph_stub
140 _GLOBAL(ftrace_graph_stub)
141 #endif
142
143         bctr                    /* jump after _mcount site */
144
145 _GLOBAL(ftrace_stub)
146         blr
147
148 ftrace_no_trace:
149         mflr    r3
150         mtctr   r3
151         REST_GPR(3, r1)
152         addi    r1, r1, SWITCH_FRAME_SIZE
153         mtlr    r0
154         bctr
155
156 _GLOBAL(ftrace_caller)
157         /* Save the original return address in A's stack frame */
158         std     r0, LRSAVE(r1)
159
160         /* Create our stack frame + pt_regs */
161         stdu    r1, -SWITCH_FRAME_SIZE(r1)
162
163         /* Save all gprs to pt_regs */
164         SAVE_8GPRS(3, r1)
165
166         lbz     r3, PACA_FTRACE_ENABLED(r13)
167         cmpdi   r3, 0
168         beq     ftrace_no_trace
169
170         /* Get the _mcount() call site out of LR */
171         mflr    r7
172         std     r7, _NIP(r1)
173
174         /* Save callee's TOC in the ABI compliant location */
175         std     r2, 24(r1)
176         ld      r2, PACATOC(r13)        /* get kernel TOC in r2 */
177
178         addis   r3, r2, function_trace_op@toc@ha
179         addi    r3, r3, function_trace_op@toc@l
180         ld      r5, 0(r3)
181
182         /* Calculate ip from nip-4 into r3 for call below */
183         subi    r3, r7, MCOUNT_INSN_SIZE
184
185         /* Put the original return address in r4 as parent_ip */
186         mr      r4, r0
187
188         /* Set pt_regs to NULL */
189         li      r6, 0
190
191         /* ftrace_call(r3, r4, r5, r6) */
192 .globl ftrace_call
193 ftrace_call:
194         bl      ftrace_stub
195         nop
196
197         ld      r3, _NIP(r1)
198         mtctr   r3
199
200         /* Restore gprs */
201         REST_8GPRS(3,r1)
202
203         /* Restore callee's TOC */
204         ld      r2, 24(r1)
205
206         /* Pop our stack frame */
207         addi    r1, r1, SWITCH_FRAME_SIZE
208
209         /* Reload original LR */
210         ld      r0, LRSAVE(r1)
211         mtlr    r0
212
213         /* Handle function_graph or go back */
214         b       ftrace_caller_common
215
216 #ifdef CONFIG_LIVEPATCH
217         /*
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
220          * function linkage.
221          *
222          * We get here when a function A, calls another function B, but B has
223          * been live patched with a new function C.
224          *
225          * On entry:
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
230          */
231 livepatch_handler:
232         CURRENT_THREAD_INFO(r12, r1)
233
234         /* Allocate 3 x 8 bytes */
235         ld      r11, TI_livepatch_sp(r12)
236         addi    r11, r11, 24
237         std     r11, TI_livepatch_sp(r12)
238
239         /* Save toc & real LR on livepatch stack */
240         std     r2,  -24(r11)
241         mflr    r12
242         std     r12, -16(r11)
243
244         /* Store stack end marker */
245         lis     r12, STACK_END_MAGIC@h
246         ori     r12, r12, STACK_END_MAGIC@l
247         std     r12, -8(r11)
248
249         /* Put ctr in r12 for global entry and branch there */
250         mfctr   r12
251         bctrl
252
253         /*
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
256          * restore it.
257          */
258
259         CURRENT_THREAD_INFO(r12, r1)
260
261         ld      r11, TI_livepatch_sp(r12)
262
263         /* Check stack marker hasn't been trashed */
264         lis     r2,  STACK_END_MAGIC@h
265         ori     r2,  r2, STACK_END_MAGIC@l
266         ld      r12, -8(r11)
267 1:      tdne    r12, r2
268         EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
269
270         /* Restore LR & toc from livepatch stack */
271         ld      r12, -16(r11)
272         mtlr    r12
273         ld      r2,  -24(r11)
274
275         /* Pop livepatch stack frame */
276         CURRENT_THREAD_INFO(r12, r1)
277         subi    r11, r11, 24
278         std     r11, TI_livepatch_sp(r12)
279
280         /* Return to original caller of live patched function */
281         blr
282 #endif /* CONFIG_LIVEPATCH */
283
284 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
285 _GLOBAL(ftrace_graph_caller)
286         stdu    r1, -112(r1)
287         /* with -mprofile-kernel, parameter regs are still alive at _mcount */
288         std     r10, 104(r1)
289         std     r9, 96(r1)
290         std     r8, 88(r1)
291         std     r7, 80(r1)
292         std     r6, 72(r1)
293         std     r5, 64(r1)
294         std     r4, 56(r1)
295         std     r3, 48(r1)
296
297         /* Save callee's TOC in the ABI compliant location */
298         std     r2, 24(r1)
299         ld      r2, PACATOC(r13)        /* get kernel TOC in r2 */
300
301         mfctr   r4              /* ftrace_caller has moved local addr here */
302         std     r4, 40(r1)
303         mflr    r3              /* ftrace_caller has restored LR from stack */
304         subi    r4, r4, MCOUNT_INSN_SIZE
305
306         bl      prepare_ftrace_return
307         nop
308
309         /*
310          * prepare_ftrace_return gives us the address we divert to.
311          * Change the LR to this.
312          */
313         mtlr    r3
314
315         ld      r0, 40(r1)
316         mtctr   r0
317         ld      r10, 104(r1)
318         ld      r9, 96(r1)
319         ld      r8, 88(r1)
320         ld      r7, 80(r1)
321         ld      r6, 72(r1)
322         ld      r5, 64(r1)
323         ld      r4, 56(r1)
324         ld      r3, 48(r1)
325
326         /* Restore callee's TOC */
327         ld      r2, 24(r1)
328
329         addi    r1, r1, 112
330         mflr    r0
331         std     r0, LRSAVE(r1)
332         bctr
333 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */