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