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
13_GLOBAL_TOC(ftrace_caller)
14	lbz	r3, PACA_FTRACE_ENABLED(r13)
15	cmpdi	r3, 0
16	beqlr
17
18	/* Taken from output of objdump from lib64/glibc */
19	mflr	r3
20	ld	r11, 0(r1)
21	stdu	r1, -112(r1)
22	std	r3, 128(r1)
23	ld	r4, 16(r11)
24	subi	r3, r3, MCOUNT_INSN_SIZE
25.globl ftrace_call
26ftrace_call:
27	bl	ftrace_stub
28	nop
29#ifdef CONFIG_FUNCTION_GRAPH_TRACER
30.globl ftrace_graph_call
31ftrace_graph_call:
32	b	ftrace_graph_stub
33_GLOBAL(ftrace_graph_stub)
34#endif
35	ld	r0, 128(r1)
36	mtlr	r0
37	addi	r1, r1, 112
38
39_GLOBAL(ftrace_stub)
40	blr
41
42#ifdef CONFIG_FUNCTION_GRAPH_TRACER
43_GLOBAL(ftrace_graph_caller)
44	addi	r5, r1, 112
45	/* load r4 with local address */
46	ld	r4, 128(r1)
47	subi	r4, r4, MCOUNT_INSN_SIZE
48
49	/* Grab the LR out of the caller stack frame */
50	ld	r11, 112(r1)
51	ld	r3, 16(r11)
52
53	bl	prepare_ftrace_return
54	nop
55
56	/*
57	 * prepare_ftrace_return gives us the address we divert to.
58	 * Change the LR in the callers stack frame to this.
59	 */
60	ld	r11, 112(r1)
61	std	r3, 16(r11)
62
63	ld	r0, 128(r1)
64	mtlr	r0
65	addi	r1, r1, 112
66	blr
67#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
68
69.pushsection ".tramp.ftrace.text","aw",@progbits;
70.globl ftrace_tramp_text
71ftrace_tramp_text:
72	.space 32
73.popsection
74
75.pushsection ".tramp.ftrace.init","aw",@progbits;
76.globl ftrace_tramp_init
77ftrace_tramp_init:
78	.space 32
79.popsection
80
81_GLOBAL(mcount)
82_GLOBAL(_mcount)
83EXPORT_SYMBOL(_mcount)
84	mflr	r12
85	mtctr	r12
86	mtlr	r0
87	bctr
88
89#ifdef CONFIG_FUNCTION_GRAPH_TRACER
90_GLOBAL(return_to_handler)
91	/* need to save return values */
92#ifdef CONFIG_PPC64
93	std	r4,  -32(r1)
94	std	r3,  -24(r1)
95	/* save TOC */
96	std	r2,  -16(r1)
97	std	r31, -8(r1)
98	mr	r31, r1
99	stdu	r1, -112(r1)
100
101	/*
102	 * We might be called from a module.
103	 * Switch to our TOC to run inside the core kernel.
104	 */
105	LOAD_PACA_TOC()
106#else
107	stwu	r1, -16(r1)
108	stw	r3, 8(r1)
109	stw	r4, 12(r1)
110#endif
111
112	bl	ftrace_return_to_handler
113	nop
114
115	/* return value has real return address */
116	mtlr	r3
117
118#ifdef CONFIG_PPC64
119	ld	r1, 0(r1)
120	ld	r4,  -32(r1)
121	ld	r3,  -24(r1)
122	ld	r2,  -16(r1)
123	ld	r31, -8(r1)
124#else
125	lwz	r3, 8(r1)
126	lwz	r4, 12(r1)
127	addi	r1, r1, 16
128#endif
129
130	/* Jump back to real return address */
131	blr
132#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
133