1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright IBM Corp. 2008, 2009
4 *
5 */
6
7#include <linux/linkage.h>
8#include <asm/asm-offsets.h>
9#include <asm/ftrace.h>
10#include <asm/nospec-insn.h>
11#include <asm/ptrace.h>
12#include <asm/export.h>
13
14
15#define STACK_FRAME_SIZE	(STACK_FRAME_OVERHEAD + __PT_SIZE)
16#define STACK_PTREGS		(STACK_FRAME_OVERHEAD)
17#define STACK_PTREGS_GPRS	(STACK_PTREGS + __PT_GPRS)
18#define STACK_PTREGS_PSW	(STACK_PTREGS + __PT_PSW)
19#define STACK_PTREGS_ORIG_GPR2	(STACK_PTREGS + __PT_ORIG_GPR2)
20#define STACK_PTREGS_FLAGS	(STACK_PTREGS + __PT_FLAGS)
21/* packed stack: allocate just enough for r14, r15 and backchain */
22#define TRACED_FUNC_FRAME_SIZE	24
23
24#ifdef CONFIG_FUNCTION_TRACER
25
26	GEN_BR_THUNK %r1
27	GEN_BR_THUNK %r14
28
29	.section .kprobes.text, "ax"
30
31ENTRY(ftrace_stub)
32	BR_EX	%r14
33ENDPROC(ftrace_stub)
34
35	.macro	ftrace_regs_entry, allregs=0
36	stg	%r14,(__SF_GPRS+8*8)(%r15)	# save traced function caller
37
38	.if \allregs == 1
39	# save psw mask
40	# don't put any instructions clobbering CC before this point
41	epsw	%r1,%r14
42	risbg	%r14,%r1,0,31,32
43	.endif
44
45	lgr	%r1,%r15
46	# allocate stack frame for ftrace_caller to contain traced function
47	aghi	%r15,-TRACED_FUNC_FRAME_SIZE
48	stg	%r1,__SF_BACKCHAIN(%r15)
49	stg	%r0,(__SF_GPRS+8*8)(%r15)
50	stg	%r15,(__SF_GPRS+9*8)(%r15)
51	# allocate pt_regs and stack frame for ftrace_trace_function
52	aghi	%r15,-STACK_FRAME_SIZE
53	stg	%r1,(STACK_PTREGS_GPRS+15*8)(%r15)
54	xc	STACK_PTREGS_ORIG_GPR2(8,%r15),STACK_PTREGS_ORIG_GPR2(%r15)
55
56	.if \allregs == 1
57	stg	%r14,(STACK_PTREGS_PSW)(%r15)
58	mvghi	STACK_PTREGS_FLAGS(%r15),_PIF_FTRACE_FULL_REGS
59	.else
60	xc	STACK_PTREGS_FLAGS(8,%r15),STACK_PTREGS_FLAGS(%r15)
61	.endif
62
63	lg	%r14,(__SF_GPRS+8*8)(%r1)	# restore original return address
64	aghi	%r1,-TRACED_FUNC_FRAME_SIZE
65	stg	%r1,__SF_BACKCHAIN(%r15)
66	stg	%r0,(STACK_PTREGS_PSW+8)(%r15)
67	stmg	%r2,%r14,(STACK_PTREGS_GPRS+2*8)(%r15)
68	.endm
69
70SYM_CODE_START(ftrace_regs_caller)
71	ftrace_regs_entry	1
72	j	ftrace_common
73SYM_CODE_END(ftrace_regs_caller)
74
75SYM_CODE_START(ftrace_caller)
76	ftrace_regs_entry	0
77	j	ftrace_common
78SYM_CODE_END(ftrace_caller)
79
80SYM_CODE_START(ftrace_common)
81#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
82	aghik	%r2,%r0,-MCOUNT_INSN_SIZE
83	lgrl	%r4,function_trace_op
84	lgrl	%r1,ftrace_func
85#else
86	lgr	%r2,%r0
87	aghi	%r2,-MCOUNT_INSN_SIZE
88	larl	%r4,function_trace_op
89	lg	%r4,0(%r4)
90	larl	%r1,ftrace_func
91	lg	%r1,0(%r1)
92#endif
93	lgr	%r3,%r14
94	la	%r5,STACK_PTREGS(%r15)
95	BASR_EX	%r14,%r1
96#ifdef CONFIG_FUNCTION_GRAPH_TRACER
97# The j instruction gets runtime patched to a nop instruction.
98# See ftrace_enable_ftrace_graph_caller.
99SYM_INNER_LABEL(ftrace_graph_caller, SYM_L_GLOBAL)
100	j	.Lftrace_graph_caller_end
101	lmg	%r2,%r3,(STACK_PTREGS_GPRS+14*8)(%r15)
102	lg	%r4,(STACK_PTREGS_PSW+8)(%r15)
103	brasl	%r14,prepare_ftrace_return
104	stg	%r2,(STACK_PTREGS_GPRS+14*8)(%r15)
105.Lftrace_graph_caller_end:
106#endif
107	lg	%r0,(STACK_PTREGS_PSW+8)(%r15)
108#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
109	ltg	%r1,STACK_PTREGS_ORIG_GPR2(%r15)
110	locgrz	%r1,%r0
111#else
112	lg	%r1,STACK_PTREGS_ORIG_GPR2(%r15)
113	ltgr	%r1,%r1
114	jnz	0f
115	lgr	%r1,%r0
116#endif
1170:	lmg	%r2,%r15,(STACK_PTREGS_GPRS+2*8)(%r15)
118	BR_EX	%r1
119SYM_CODE_END(ftrace_common)
120
121#ifdef CONFIG_FUNCTION_GRAPH_TRACER
122
123SYM_FUNC_START(return_to_handler)
124	stmg	%r2,%r5,32(%r15)
125	lgr	%r1,%r15
126	aghi	%r15,-STACK_FRAME_OVERHEAD
127	stg	%r1,__SF_BACKCHAIN(%r15)
128	brasl	%r14,ftrace_return_to_handler
129	aghi	%r15,STACK_FRAME_OVERHEAD
130	lgr	%r14,%r2
131	lmg	%r2,%r5,32(%r15)
132	BR_EX	%r14
133SYM_FUNC_END(return_to_handler)
134
135#endif
136#endif /* CONFIG_FUNCTION_TRACER */
137
138#ifdef CONFIG_RETHOOK
139
140SYM_FUNC_START(arch_rethook_trampoline)
141
142	stg	%r14,(__SF_GPRS+8*8)(%r15)
143	lay	%r15,-STACK_FRAME_SIZE(%r15)
144	stmg	%r0,%r14,STACK_PTREGS_GPRS(%r15)
145
146	# store original stack pointer in backchain and pt_regs
147	lay	%r7,STACK_FRAME_SIZE(%r15)
148	stg	%r7,__SF_BACKCHAIN(%r15)
149	stg	%r7,STACK_PTREGS_GPRS+(15*8)(%r15)
150
151	# store full psw
152	epsw	%r2,%r3
153	risbg	%r3,%r2,0,31,32
154	stg	%r3,STACK_PTREGS_PSW(%r15)
155	larl	%r1,arch_rethook_trampoline
156	stg	%r1,STACK_PTREGS_PSW+8(%r15)
157
158	lay	%r2,STACK_PTREGS(%r15)
159	brasl	%r14,arch_rethook_trampoline_callback
160
161	mvc	__SF_EMPTY(16,%r7),STACK_PTREGS_PSW(%r15)
162	lmg	%r0,%r15,STACK_PTREGS_GPRS(%r15)
163	lpswe	__SF_EMPTY(%r15)
164
165SYM_FUNC_END(arch_rethook_trampoline)
166
167#endif /* CONFIG_RETHOOK */
168