xref: /linux/arch/powerpc/kernel/trace/ftrace_entry.S (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Split from ftrace_64.S
4 */
5
6#include <linux/export.h>
7#include <linux/magic.h>
8#include <asm/ppc_asm.h>
9#include <asm/asm-offsets.h>
10#include <asm/ftrace.h>
11#include <asm/ppc-opcode.h>
12#include <asm/thread_info.h>
13#include <asm/bug.h>
14#include <asm/ptrace.h>
15
16/*
17 *
18 * ftrace_caller()/ftrace_regs_caller() is the function that replaces _mcount()
19 * when ftrace is active.
20 *
21 * We arrive here after a function A calls function B, and we are the trace
22 * function for B. When we enter r1 points to A's stack frame, B has not yet
23 * had a chance to allocate one yet.
24 *
25 * Additionally r2 may point either to the TOC for A, or B, depending on
26 * whether B did a TOC setup sequence before calling us.
27 *
28 * On entry the LR points back to the _mcount() call site, and r0 holds the
29 * saved LR as it was on entry to B, ie. the original return address at the
30 * call site in A.
31 *
32 * Our job is to save the register state into a struct pt_regs (on the stack)
33 * and then arrange for the ftrace function to be called.
34 */
35.macro	ftrace_regs_entry allregs
36	/* Create a minimal stack frame for representing B */
37	PPC_STLU	r1, -STACK_FRAME_MIN_SIZE(r1)
38
39	/* Create our stack frame + pt_regs */
40	PPC_STLU	r1,-SWITCH_FRAME_SIZE(r1)
41
42	.if \allregs == 1
43	SAVE_GPRS(11, 12, r1)
44	.endif
45
46	/* Get the _mcount() call site out of LR */
47	mflr	r11
48
49#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
50	/* Load the ftrace_op */
51	PPC_LL	r12, -(MCOUNT_INSN_SIZE*2 + SZL)(r11)
52
53	/* Load direct_call from the ftrace_op */
54	PPC_LL	r12, FTRACE_OPS_DIRECT_CALL(r12)
55	PPC_LCMPI r12, 0
56	.if \allregs == 1
57	bne	.Lftrace_direct_call_regs
58	.else
59	bne	.Lftrace_direct_call
60	.endif
61#endif
62
63	/* Save the previous LR in pt_regs->link */
64	PPC_STL	r0, _LINK(r1)
65	/* Also save it in A's stack frame */
66	PPC_STL	r0, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE+LRSAVE(r1)
67
68	/* Save all gprs to pt_regs */
69	SAVE_GPR(0, r1)
70	SAVE_GPRS(3, 10, r1)
71
72#ifdef CONFIG_PPC64
73	/* Ok to continue? */
74	lbz	r3, PACA_FTRACE_ENABLED(r13)
75	cmpdi	r3, 0
76	beq	ftrace_no_trace
77#endif
78
79	.if \allregs == 1
80	SAVE_GPR(2, r1)
81	SAVE_GPRS(13, 31, r1)
82	.else
83#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
84	SAVE_GPR(14, r1)
85#endif
86	.endif
87
88	/* Save previous stack pointer (r1) */
89	addi	r8, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
90	PPC_STL	r8, GPR1(r1)
91
92	.if \allregs == 1
93	/* Load special regs for save below */
94	mfcr	r7
95	mfmsr   r8
96	mfctr   r9
97	mfxer   r10
98	.else
99	/* Clear MSR to flag as ftrace_caller versus frace_regs_caller */
100	li	r8, 0
101	.endif
102
103#ifdef CONFIG_PPC64
104	/* Save callee's TOC in the ABI compliant location */
105	std	r2, STK_GOT(r1)
106	LOAD_PACA_TOC()		/* get kernel TOC in r2 */
107#endif
108
109#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
110	/* r11 points to the instruction following the call to ftrace */
111	PPC_LL	r5, -(MCOUNT_INSN_SIZE*2 + SZL)(r11)
112	PPC_LL	r12, FTRACE_OPS_FUNC(r5)
113	mtctr	r12
114#else /* !CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS */
115#ifdef CONFIG_PPC64
116	LOAD_REG_ADDR(r3, function_trace_op)
117	ld	r5,0(r3)
118#else
119	lis	r3,function_trace_op@ha
120	lwz	r5,function_trace_op@l(r3)
121#endif
122#endif
123
124	/* Save special regs */
125	PPC_STL	r8, _MSR(r1)
126	.if \allregs == 1
127	PPC_STL	r7, _CCR(r1)
128	PPC_STL	r9, _CTR(r1)
129	PPC_STL	r10, _XER(r1)
130	.endif
131
132#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
133	/* Clear orig_gpr3 to later detect ftrace_direct call */
134	li	r7, 0
135	PPC_STL	r7, ORIG_GPR3(r1)
136#endif
137
138#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
139	/* Save our real return address in nvr for return */
140	.if \allregs == 0
141	SAVE_GPR(15, r1)
142	.endif
143	mr	r15, r11
144	/*
145	 * We want the ftrace location in the function, but our lr (in r11)
146	 * points at the 'mtlr r0' instruction in the out of line stub.  To
147	 * recover the ftrace location, we read the branch instruction in the
148	 * stub, and adjust our lr by the branch offset.
149	 *
150	 * See ftrace_init_ool_stub() for the profile sequence.
151	 */
152	lwz	r8, MCOUNT_INSN_SIZE(r11)
153	slwi	r8, r8, 6
154	srawi	r8, r8, 6
155	add	r3, r11, r8
156	/*
157	 * Override our nip to point past the branch in the original function.
158	 * This allows reliable stack trace and the ftrace stack tracer to work as-is.
159	 */
160	addi	r11, r3, MCOUNT_INSN_SIZE
161#else
162	/* Calculate ip from nip-4 into r3 for call below */
163	subi    r3, r11, MCOUNT_INSN_SIZE
164#endif
165
166	/* Save NIP as pt_regs->nip */
167	PPC_STL	r11, _NIP(r1)
168	/* Also save it in B's stackframe header for proper unwind */
169	PPC_STL	r11, LRSAVE+SWITCH_FRAME_SIZE(r1)
170#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
171	mr	r14, r11	/* remember old NIP */
172#endif
173
174	/* Put the original return address in r4 as parent_ip */
175	mr	r4, r0
176
177	/* Load &pt_regs in r6 for call below */
178	addi    r6, r1, STACK_INT_FRAME_REGS
179.endm
180
181.macro	ftrace_regs_exit allregs
182#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
183	/* Check orig_gpr3 to detect ftrace_direct call */
184	PPC_LL	r3, ORIG_GPR3(r1)
185	PPC_LCMPI cr1, r3, 0
186	mtctr	r3
187#endif
188
189	/* Restore possibly modified LR */
190	PPC_LL	r0, _LINK(r1)
191
192#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE
193	/* Load ctr with the possibly modified NIP */
194	PPC_LL	r3, _NIP(r1)
195#ifdef CONFIG_LIVEPATCH_64
196	cmpd	r14, r3		/* has NIP been altered? */
197#endif
198
199#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
200	beq	cr1,2f
201	mtlr	r3
202	b	3f
203#endif
2042:	mtctr	r3
205	mtlr	r0
2063:
207
208#else /* !CONFIG_PPC_FTRACE_OUT_OF_LINE */
209	/* Load LR with the possibly modified NIP */
210	PPC_LL	r3, _NIP(r1)
211	cmpd	r14, r3		/* has NIP been altered? */
212	bne-	1f
213
214	mr	r3, r15
2151:	mtlr	r3
216	.if \allregs == 0
217	REST_GPR(15, r1)
218	.endif
219#endif
220
221	/* Restore gprs */
222	.if \allregs == 1
223#ifdef CONFIG_PPC64
224	REST_GPRS(2, 12, r1)
225	/* Do not restore a stale PACA pointer if the task migrated */
226	REST_GPRS(14, 31, r1)
227#else
228	REST_GPRS(2, 31, r1)
229#endif
230	.else
231	REST_GPRS(3, 10, r1)
232#if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
233	REST_GPR(14, r1)
234#endif
235	.endif
236
237#ifdef CONFIG_PPC64
238	/* Restore callee's TOC */
239	ld	r2, STK_GOT(r1)
240#endif
241
242	/* Pop our stack frame */
243	addi r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
244
245#ifdef CONFIG_LIVEPATCH_64
246        /* Based on the cmpd above, if the NIP was altered handle livepatch */
247	bne-	livepatch_handler
248#endif
249
250	/* jump after _mcount site */
251#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
252#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
253	bnectr	cr1
254#endif
255	/*
256	 * Return with blr to keep the link stack balanced. The function profiling sequence
257	 * uses 'mtlr r0' to restore LR.
258	 */
259	blr
260#else
261	bctr
262#endif
263.endm
264
265.macro ftrace_regs_func allregs
266#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
267	bctrl
268#else
269	.if \allregs == 1
270.globl ftrace_regs_call
271ftrace_regs_call:
272	.else
273.globl ftrace_call
274ftrace_call:
275	.endif
276	/* ftrace_call(r3, r4, r5, r6) */
277	bl	ftrace_stub
278#endif
279.endm
280
281_GLOBAL(ftrace_regs_caller)
282	ftrace_regs_entry 1
283	ftrace_regs_func 1
284	ftrace_regs_exit 1
285
286_GLOBAL(ftrace_caller)
287	ftrace_regs_entry 0
288	ftrace_regs_func 0
289	ftrace_regs_exit 0
290
291_GLOBAL(ftrace_stub)
292	blr
293
294#ifdef CONFIG_PPC64
295ftrace_no_trace:
296#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
297	REST_GPR(3, r1)
298	addi	r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
299	blr
300#else
301	mflr	r3
302	mtctr	r3
303	REST_GPR(3, r1)
304	addi	r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
305	mtlr	r0
306	bctr
307#endif
308#endif
309
310#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
311.Lftrace_direct_call_regs:
312	mtctr	r12
313	REST_GPRS(11, 12, r1)
314	addi	r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
315	bctr
316.Lftrace_direct_call:
317	mtctr	r12
318	addi	r1, r1, SWITCH_FRAME_SIZE+STACK_FRAME_MIN_SIZE
319	bctr
320SYM_FUNC_START(ftrace_stub_direct_tramp)
321	blr
322SYM_FUNC_END(ftrace_stub_direct_tramp)
323#endif
324
325#ifdef CONFIG_LIVEPATCH_64
326	/*
327	 * This function runs in the mcount context, between two functions. As
328	 * such it can only clobber registers which are volatile and used in
329	 * function linkage.
330	 *
331	 * We get here when a function A, calls another function B, but B has
332	 * been live patched with a new function C.
333	 *
334	 * On entry, we have no stack frame and can not allocate one.
335	 *
336	 * With PPC_FTRACE_OUT_OF_LINE=n, on entry:
337	 *  - LR points back to the original caller (in A)
338	 *  - CTR holds the new NIP in C
339	 *  - r0, r11 & r12 are free
340	 *
341	 * With PPC_FTRACE_OUT_OF_LINE=y, on entry:
342	 *  - r0 points back to the original caller (in A)
343	 *  - LR holds the new NIP in C
344	 *  - r11 & r12 are free
345	 */
346livepatch_handler:
347	ld	r12, PACA_THREAD_INFO(r13)
348
349	/* Allocate 3 x 8 bytes */
350	ld	r11, TI_livepatch_sp(r12)
351	addi	r11, r11, 24
352	std	r11, TI_livepatch_sp(r12)
353
354	/* Store stack end marker */
355	lis     r12, STACK_END_MAGIC@h
356	ori     r12, r12, STACK_END_MAGIC@l
357	std	r12, -8(r11)
358
359	/* Save toc & real LR on livepatch stack */
360	std	r2,  -24(r11)
361#ifndef CONFIG_PPC_FTRACE_OUT_OF_LINE
362	mflr	r12
363	std	r12, -16(r11)
364	mfctr	r12
365#else
366	std	r0, -16(r11)
367	mflr	r12
368	/* Put ctr in r12 for global entry and branch there */
369	mtctr	r12
370#endif
371	bctrl
372
373	/*
374	 * Now we are returning from the patched function to the original
375	 * caller A. We are free to use r11, r12 and we can use r2 until we
376	 * restore it.
377	 */
378
379	ld	r12, PACA_THREAD_INFO(r13)
380
381	ld	r11, TI_livepatch_sp(r12)
382
383	/* Check stack marker hasn't been trashed */
384	lis     r2,  STACK_END_MAGIC@h
385	ori     r2,  r2, STACK_END_MAGIC@l
386	ld	r12, -8(r11)
3871:	tdne	r12, r2
388	EMIT_BUG_ENTRY 1b, __FILE__, __LINE__ - 1, 0
389
390	/* Restore LR & toc from livepatch stack */
391	ld	r12, -16(r11)
392	mtlr	r12
393	ld	r2,  -24(r11)
394
395	/* Pop livepatch stack frame */
396	ld	r12, PACA_THREAD_INFO(r13)
397	subi	r11, r11, 24
398	std	r11, TI_livepatch_sp(r12)
399
400	/* Return to original caller of live patched function */
401	blr
402#endif /* CONFIG_LIVEPATCH */
403
404#ifndef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY
405_GLOBAL(mcount)
406_GLOBAL(_mcount)
407EXPORT_SYMBOL(_mcount)
408	mflr	r12
409	mtctr	r12
410	mtlr	r0
411	bctr
412#endif
413
414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
415_GLOBAL(return_to_handler)
416	/* need to save return values */
417#ifdef CONFIG_PPC64
418	stdu	r1, -SWITCH_FRAME_SIZE(r1)
419	std	r4, GPR4(r1)
420	std	r3, GPR3(r1)
421	/* Save previous stack pointer (r1) */
422	addi	r3, r1, SWITCH_FRAME_SIZE
423	std	r3, GPR1(r1)
424	/* save TOC */
425	std	r2,  24(r1)
426	std	r31, 32(r1)
427	mr	r31, r1
428	/* pass ftrace_regs/pt_regs to ftrace_return_to_handler */
429	addi	r3, r1, STACK_INT_FRAME_REGS
430	/*
431	 * We might be called from a module.
432	 * Switch to our TOC to run inside the core kernel.
433	 */
434	LOAD_PACA_TOC()
435#else
436	stwu	r1, -SWITCH_FRAME_SIZE(r1)
437	stw	r4, GPR4(r1)
438	stw	r3, GPR3(r1)
439	addi	r3, r1, SWITCH_FRAME_SIZE
440	stw	r3, GPR1(r1)
441	/* pass ftrace_regs/pt_regs to ftrace_return_to_handler */
442	addi	r3, r1, STACK_INT_FRAME_REGS
443#endif
444
445	bl	ftrace_return_to_handler
446	nop
447
448	/* return value has real return address */
449	mtlr	r3
450
451#ifdef CONFIG_PPC64
452	ld	r4,  GPR4(r1)
453	ld	r3,  GPR3(r1)
454	ld	r2,  24(r1)
455	ld	r31, 32(r1)
456	ld	r1,  0(r1)
457#else
458	lwz	r3, GPR3(r1)
459	lwz	r4, GPR4(r1)
460	addi	r1, r1, SWITCH_FRAME_SIZE
461#endif
462
463	/* Jump back to real return address */
464	blr
465#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
466
467#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
468SYM_DATA(ftrace_ool_stub_text_count, .long CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE)
469
470SYM_START(ftrace_ool_stub_text, SYM_L_GLOBAL, .balign SZL)
471	.space CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE * FTRACE_OOL_STUB_SIZE
472SYM_CODE_END(ftrace_ool_stub_text)
473#endif
474
475.pushsection ".tramp.ftrace.text","aw",@progbits;
476.globl ftrace_tramp_text
477ftrace_tramp_text:
478	.space 32
479.popsection
480
481.pushsection ".tramp.ftrace.init","aw",@progbits;
482.globl ftrace_tramp_init
483ftrace_tramp_init:
484	.space 32
485.popsection
486