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