xref: /linux/arch/s390/include/asm/stacktrace.h (revision 84f1f7fb3eec0de12a3cb30dce10e667a5fcfad2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_STACKTRACE_H
3 #define _ASM_S390_STACKTRACE_H
4 
5 #include <linux/stacktrace.h>
6 #include <linux/uaccess.h>
7 #include <linux/ptrace.h>
8 
9 struct stack_frame_user {
10 	unsigned long back_chain;
11 	unsigned long empty1[5];
12 	unsigned long gprs[10];
13 	unsigned long empty2[4];
14 };
15 
16 struct stack_frame_vdso_wrapper {
17 	struct stack_frame_user sf;
18 	unsigned long return_address;
19 };
20 
21 struct perf_callchain_entry_ctx;
22 
23 void arch_stack_walk_user_common(stack_trace_consume_fn consume_entry, void *cookie,
24 				 struct perf_callchain_entry_ctx *entry,
25 				 const struct pt_regs *regs, bool perf);
26 
27 enum stack_type {
28 	STACK_TYPE_UNKNOWN,
29 	STACK_TYPE_TASK,
30 	STACK_TYPE_IRQ,
31 	STACK_TYPE_NODAT,
32 	STACK_TYPE_RESTART,
33 	STACK_TYPE_MCCK,
34 };
35 
36 struct stack_info {
37 	enum stack_type type;
38 	unsigned long begin, end;
39 };
40 
41 const char *stack_type_name(enum stack_type type);
42 int get_stack_info(unsigned long sp, struct task_struct *task,
43 		   struct stack_info *info, unsigned long *visit_mask);
44 
45 static inline bool on_stack(struct stack_info *info,
46 			    unsigned long addr, size_t len)
47 {
48 	if (info->type == STACK_TYPE_UNKNOWN)
49 		return false;
50 	if (addr + len < addr)
51 		return false;
52 	return addr >= info->begin && addr + len <= info->end;
53 }
54 
55 /*
56  * Stack layout of a C stack frame.
57  * Kernel uses the packed stack layout (-mpacked-stack).
58  */
59 struct stack_frame {
60 	union {
61 		unsigned long empty[9];
62 		/* SIE stack frame */
63 		struct {
64 			unsigned long sie_control_block;
65 			unsigned long sie_savearea;
66 			unsigned long sie_return;
67 			unsigned long sie_flags;
68 			unsigned long sie_control_block_phys;
69 			unsigned long sie_guest_asce;
70 			unsigned long sie_irq;
71 		};
72 		/* SAE stack frame */
73 		struct {
74 			unsigned long sae_bear;
75 		};
76 	};
77 	unsigned long gprs[10];
78 	unsigned long back_chain;
79 };
80 
81 /*
82  * Unlike current_stack_pointer which simply contains the current value of %r15
83  * current_frame_address() returns function stack frame address, which matches
84  * %r15 upon function invocation. It may differ from %r15 later if function
85  * allocates stack for local variables or new stack frame to call other
86  * functions.
87  */
88 #define current_frame_address()						\
89 	((unsigned long)__builtin_frame_address(0) -			\
90 	 offsetof(struct stack_frame, back_chain))
91 
92 static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
93 						       struct pt_regs *regs)
94 {
95 	if (regs)
96 		return (unsigned long)kernel_stack_pointer(regs);
97 	if (task == current)
98 		return current_frame_address();
99 	return (unsigned long)task->thread.ksp;
100 }
101 
102 /*
103  * To keep this simple mark register 2-6 as being changed (volatile)
104  * by the called function, even though register 6 is saved/nonvolatile.
105  */
106 #define CALL_FMT_0 "=&d" (r2)
107 #define CALL_FMT_1 "+&d" (r2)
108 #define CALL_FMT_2 CALL_FMT_1, "+&d" (r3)
109 #define CALL_FMT_3 CALL_FMT_2, "+&d" (r4)
110 #define CALL_FMT_4 CALL_FMT_3, "+&d" (r5)
111 #define CALL_FMT_5 CALL_FMT_4, "+&d" (r6)
112 
113 #define CALL_CLOBBER_5 "0", "1", "14", "cc", "memory"
114 #define CALL_CLOBBER_4 CALL_CLOBBER_5
115 #define CALL_CLOBBER_3 CALL_CLOBBER_4, "5"
116 #define CALL_CLOBBER_2 CALL_CLOBBER_3, "4"
117 #define CALL_CLOBBER_1 CALL_CLOBBER_2, "3"
118 #define CALL_CLOBBER_0 CALL_CLOBBER_1
119 
120 #define CALL_LARGS_0(...)						\
121 	long dummy = 0
122 #define CALL_LARGS_1(t1, a1)						\
123 	long arg1  = (long)(t1)(a1)
124 #define CALL_LARGS_2(t1, a1, t2, a2)					\
125 	CALL_LARGS_1(t1, a1);						\
126 	long arg2 = (long)(t2)(a2)
127 #define CALL_LARGS_3(t1, a1, t2, a2, t3, a3)				\
128 	CALL_LARGS_2(t1, a1, t2, a2);					\
129 	long arg3 = (long)(t3)(a3)
130 #define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4)			\
131 	CALL_LARGS_3(t1, a1, t2, a2, t3, a3);				\
132 	long arg4  = (long)(t4)(a4)
133 #define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)		\
134 	CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4);			\
135 	long arg5 = (long)(t5)(a5)
136 
137 #define CALL_REGS_0							\
138 	register long r2 asm("2") = dummy
139 #define CALL_REGS_1							\
140 	register long r2 asm("2") = arg1
141 #define CALL_REGS_2							\
142 	CALL_REGS_1;							\
143 	register long r3 asm("3") = arg2
144 #define CALL_REGS_3							\
145 	CALL_REGS_2;							\
146 	register long r4 asm("4") = arg3
147 #define CALL_REGS_4							\
148 	CALL_REGS_3;							\
149 	register long r5 asm("5") = arg4
150 #define CALL_REGS_5							\
151 	CALL_REGS_4;							\
152 	register long r6 asm("6") = arg5
153 
154 #define CALL_TYPECHECK_0(...)
155 #define CALL_TYPECHECK_1(t, a, ...)					\
156 	typecheck(t, a)
157 #define CALL_TYPECHECK_2(t, a, ...)					\
158 	CALL_TYPECHECK_1(__VA_ARGS__);					\
159 	typecheck(t, a)
160 #define CALL_TYPECHECK_3(t, a, ...)					\
161 	CALL_TYPECHECK_2(__VA_ARGS__);					\
162 	typecheck(t, a)
163 #define CALL_TYPECHECK_4(t, a, ...)					\
164 	CALL_TYPECHECK_3(__VA_ARGS__);					\
165 	typecheck(t, a)
166 #define CALL_TYPECHECK_5(t, a, ...)					\
167 	CALL_TYPECHECK_4(__VA_ARGS__);					\
168 	typecheck(t, a)
169 
170 #define CALL_PARM_0(...) void
171 #define CALL_PARM_1(t, a, ...) t
172 #define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
173 #define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
174 #define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
175 #define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
176 #define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
177 
178 /*
179  * Use call_on_stack() to call a function switching to a specified
180  * stack. Proper sign and zero extension of function arguments is
181  * done. Usage:
182  *
183  * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
184  *
185  * - nr specifies the number of function arguments of fn.
186  * - stack specifies the stack to be used.
187  * - fn is the function to be called.
188  * - rettype is the return type of fn.
189  * - t1, a1, ... are pairs, where t1 must match the type of the first
190  *   argument of fn, t2 the second, etc. a1 is the corresponding
191  *   first function argument (not name), etc.
192  */
193 #define call_on_stack(nr, stack, rettype, fn, ...)			\
194 ({									\
195 	rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn;		\
196 	unsigned long frame = current_frame_address();			\
197 	unsigned long __stack = stack;					\
198 	unsigned long prev;						\
199 	CALL_LARGS_##nr(__VA_ARGS__);					\
200 	CALL_REGS_##nr;							\
201 									\
202 	CALL_TYPECHECK_##nr(__VA_ARGS__);				\
203 	asm volatile(							\
204 		"	lgr	%[_prev],15\n"				\
205 		"	lg	15,%[_stack]\n"				\
206 		"	stg	%[_frame],%[_bc](15)\n"			\
207 		"	brasl	14,%[_fn]\n"				\
208 		"	lgr	15,%[_prev]"				\
209 		: [_prev] "=&d" (prev), CALL_FMT_##nr			\
210 		: [_stack] "R" (__stack),				\
211 		  [_bc] "i" (offsetof(struct stack_frame, back_chain)),	\
212 		  [_frame] "d" (frame),					\
213 		  [_fn] "X" (__fn) : CALL_CLOBBER_##nr);		\
214 	(rettype)r2;							\
215 })
216 
217 /*
218  * Use call_nodat() to call a function with DAT disabled.
219  * Proper sign and zero extension of function arguments is done.
220  * Usage:
221  *
222  * rc = call_nodat(nr, rettype, fn, t1, a1, t2, a2, ...)
223  *
224  * - nr specifies the number of function arguments of fn.
225  * - fn is the function to be called, where fn is a physical address.
226  * - rettype is the return type of fn.
227  * - t1, a1, ... are pairs, where t1 must match the type of the first
228  *   argument of fn, t2 the second, etc. a1 is the corresponding
229  *   first function argument (not name), etc.
230  *
231  * fn() is called with standard C function call ABI, with the exception
232  * that no useful stackframe or stackpointer is passed via register 15.
233  * Therefore the called function must not use r15 to access the stack.
234  */
235 #define call_nodat(nr, rettype, fn, ...)				\
236 ({									\
237 	rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = (fn);		\
238 	/* aligned since psw_leave must not cross page boundary */	\
239 	psw_t __aligned(16) psw_leave;					\
240 	psw_t psw_enter;						\
241 	CALL_LARGS_##nr(__VA_ARGS__);					\
242 	CALL_REGS_##nr;							\
243 									\
244 	CALL_TYPECHECK_##nr(__VA_ARGS__);				\
245 	psw_enter.mask = PSW_KERNEL_BITS & ~PSW_MASK_DAT;		\
246 	psw_enter.addr = (unsigned long)__fn;				\
247 	asm volatile(							\
248 		"	epsw	0,1\n"					\
249 		"	risbg	1,0,0,31,32\n"				\
250 		"	larl	7,1f\n"					\
251 		"	stg	1,%[psw_leave]\n"			\
252 		"	stg	7,8+%[psw_leave]\n"			\
253 		"	la	7,%[psw_leave]\n"			\
254 		"	lra	7,0(7)\n"				\
255 		"	larl	1,0f\n"					\
256 		"	lra	14,0(1)\n"				\
257 		"	lpswe	%[psw_enter]\n"				\
258 		"0:	lpswe	0(7)\n"					\
259 		"1:"							\
260 		: CALL_FMT_##nr, [psw_leave] "=Q" (psw_leave)		\
261 		: [psw_enter] "Q" (psw_enter)				\
262 		: "7", CALL_CLOBBER_##nr);				\
263 	(rettype)r2;							\
264 })
265 
266 #endif /* _ASM_S390_STACKTRACE_H */
267