xref: /linux/arch/s390/kernel/mcount.S (revision f96a974170b749e3a56844e25b31d46a7233b6f6)
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/march.h>
13
14#define STACK_FRAME_SIZE_PTREGS		(STACK_FRAME_OVERHEAD + __PT_SIZE)
15#define STACK_PTREGS			(STACK_FRAME_OVERHEAD)
16#define STACK_PTREGS_GPRS		(STACK_PTREGS + __PT_GPRS)
17#define STACK_PTREGS_PSW		(STACK_PTREGS + __PT_PSW)
18
19#define STACK_FRAME_SIZE_FREGS		(STACK_FRAME_OVERHEAD + __FTRACE_REGS_SIZE)
20#define STACK_FREGS			(STACK_FRAME_OVERHEAD)
21#define STACK_FREGS_PTREGS		(STACK_FRAME_OVERHEAD + __FTRACE_REGS_PT_REGS)
22#define STACK_FREGS_PTREGS_GPRS		(STACK_FREGS_PTREGS + __PT_GPRS)
23#define STACK_FREGS_PTREGS_PSW		(STACK_FREGS_PTREGS + __PT_PSW)
24#define STACK_FREGS_PTREGS_ORIG_GPR2	(STACK_FREGS_PTREGS + __PT_ORIG_GPR2)
25#define STACK_FREGS_PTREGS_FLAGS	(STACK_FREGS_PTREGS + __PT_FLAGS)
26
27/* packed stack: allocate just enough for r14, r15 and backchain */
28#define TRACED_FUNC_FRAME_SIZE	24
29
30#ifdef CONFIG_FUNCTION_TRACER
31
32	GEN_BR_THUNK %r1
33	GEN_BR_THUNK %r14
34
35	.section .kprobes.text, "ax"
36
37SYM_FUNC_START(ftrace_stub)
38	BR_EX	%r14
39SYM_FUNC_END(ftrace_stub)
40
41SYM_CODE_START(ftrace_stub_direct_tramp)
42	lgr	%r1, %r0
43	BR_EX	%r1
44SYM_CODE_END(ftrace_stub_direct_tramp)
45
46	.macro	ftrace_regs_entry, allregs=0
47	stg	%r14,(__SF_GPRS+8*8)(%r15)	# save traced function caller
48
49	.if \allregs == 1
50	# save psw mask
51	# don't put any instructions clobbering CC before this point
52	epsw	%r1,%r14
53	risbg	%r14,%r1,0,31,32
54	.endif
55
56	lgr	%r1,%r15
57	# allocate stack frame for ftrace_caller to contain traced function
58	aghi	%r15,-TRACED_FUNC_FRAME_SIZE
59	stg	%r1,__SF_BACKCHAIN(%r15)
60	stg	%r0,(__SF_GPRS+8*8)(%r15)
61	stg	%r15,(__SF_GPRS+9*8)(%r15)
62	# allocate ftrace_regs and stack frame for ftrace_trace_function
63	aghi	%r15,-STACK_FRAME_SIZE_FREGS
64	stg	%r1,(STACK_FREGS_PTREGS_GPRS+15*8)(%r15)
65	xc	STACK_FREGS_PTREGS_ORIG_GPR2(8,%r15),STACK_FREGS_PTREGS_ORIG_GPR2(%r15)
66
67	.if \allregs == 1
68	stg	%r14,(STACK_FREGS_PTREGS_PSW)(%r15)
69	mvghi	STACK_FREGS_PTREGS_FLAGS(%r15),_PIF_FTRACE_FULL_REGS
70	.else
71	xc	STACK_FREGS_PTREGS_FLAGS(8,%r15),STACK_FREGS_PTREGS_FLAGS(%r15)
72	.endif
73
74	lg	%r14,(__SF_GPRS+8*8)(%r1)	# restore original return address
75	aghi	%r1,-TRACED_FUNC_FRAME_SIZE
76	stg	%r1,__SF_BACKCHAIN(%r15)
77	stg	%r0,(STACK_FREGS_PTREGS_PSW+8)(%r15)
78	stmg	%r2,%r14,(STACK_FREGS_PTREGS_GPRS+2*8)(%r15)
79	.endm
80
81SYM_CODE_START(ftrace_regs_caller)
82	ftrace_regs_entry	1
83	j	ftrace_common
84SYM_CODE_END(ftrace_regs_caller)
85
86SYM_CODE_START(ftrace_caller)
87	ftrace_regs_entry	0
88	j	ftrace_common
89SYM_CODE_END(ftrace_caller)
90
91SYM_CODE_START(ftrace_common)
92#ifdef MARCH_HAS_Z196_FEATURES
93	aghik	%r2,%r0,-MCOUNT_INSN_SIZE
94	lgrl	%r4,function_trace_op
95	lgrl	%r1,ftrace_func
96#else
97	lgr	%r2,%r0
98	aghi	%r2,-MCOUNT_INSN_SIZE
99	larl	%r4,function_trace_op
100	lg	%r4,0(%r4)
101	larl	%r1,ftrace_func
102	lg	%r1,0(%r1)
103#endif
104	lgr	%r3,%r14
105	la	%r5,STACK_FREGS(%r15)
106	BASR_EX	%r14,%r1
107	lg	%r0,(STACK_FREGS_PTREGS_PSW+8)(%r15)
108#ifdef MARCH_HAS_Z196_FEATURES
109	ltg	%r1,STACK_FREGS_PTREGS_ORIG_GPR2(%r15)
110	locgrz	%r1,%r0
111#else
112	lg	%r1,STACK_FREGS_PTREGS_ORIG_GPR2(%r15)
113	ltgr	%r1,%r1
114	jnz	0f
115	lgr	%r1,%r0
116#endif
1170:	lmg	%r2,%r15,(STACK_FREGS_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	# allocate ftrace_regs and stack frame for ftrace_return_to_handler
127	aghi	%r15,-STACK_FRAME_SIZE_FREGS
128	stg	%r1,__SF_BACKCHAIN(%r15)
129	stg	%r2,(STACK_FREGS_PTREGS_GPRS+2*8)(%r15)
130	stg	%r1,(STACK_FREGS_PTREGS_GPRS+15*8)(%r15)
131	la	%r2,STACK_FRAME_OVERHEAD(%r15)
132	brasl	%r14,ftrace_return_to_handler
133	aghi	%r15,STACK_FRAME_SIZE_FREGS
134	lgr	%r14,%r2
135	lmg	%r2,%r5,32(%r15)
136	BR_EX	%r14
137SYM_FUNC_END(return_to_handler)
138
139#endif
140#endif /* CONFIG_FUNCTION_TRACER */
141
142SYM_CODE_START(ftrace_shared_hotpatch_trampoline_br)
143	lmg	%r0,%r1,2(%r1)
144	br	%r1
145SYM_INNER_LABEL(ftrace_shared_hotpatch_trampoline_br_end, SYM_L_GLOBAL)
146SYM_CODE_END(ftrace_shared_hotpatch_trampoline_br)
147
148#ifdef CONFIG_EXPOLINE
149SYM_CODE_START(ftrace_shared_hotpatch_trampoline_exrl)
150	lmg	%r0,%r1,2(%r1)
151	exrl	%r0,0f
152	j	.
1530:	br	%r1
154SYM_INNER_LABEL(ftrace_shared_hotpatch_trampoline_exrl_end, SYM_L_GLOBAL)
155SYM_CODE_END(ftrace_shared_hotpatch_trampoline_exrl)
156#endif /* CONFIG_EXPOLINE */
157
158#ifdef CONFIG_RETHOOK
159
160SYM_CODE_START(arch_rethook_trampoline)
161	stg	%r14,(__SF_GPRS+8*8)(%r15)
162	lay	%r15,-STACK_FRAME_SIZE_PTREGS(%r15)
163	stmg	%r0,%r14,STACK_PTREGS_GPRS(%r15)
164
165	# store original stack pointer in backchain and pt_regs
166	lay	%r7,STACK_FRAME_SIZE_PTREGS(%r15)
167	stg	%r7,__SF_BACKCHAIN(%r15)
168	stg	%r7,STACK_PTREGS_GPRS+(15*8)(%r15)
169
170	# store full psw
171	epsw	%r2,%r3
172	risbg	%r3,%r2,0,31,32
173	stg	%r3,STACK_PTREGS_PSW(%r15)
174	larl	%r1,arch_rethook_trampoline
175	stg	%r1,STACK_PTREGS_PSW+8(%r15)
176
177	lay	%r2,STACK_PTREGS(%r15)
178	brasl	%r14,arch_rethook_trampoline_callback
179
180	mvc	__SF_EMPTY(16,%r7),STACK_PTREGS_PSW(%r15)
181	lmg	%r0,%r15,STACK_PTREGS_GPRS(%r15)
182	lpswe	__SF_EMPTY(%r15)
183SYM_CODE_END(arch_rethook_trampoline)
184
185#endif /* CONFIG_RETHOOK */
186