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