1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_STACKTRACE_H
3 #define _ASM_S390_STACKTRACE_H
5 #include <linux/uaccess.h>
6 #include <linux/ptrace.h>
7 #include <asm/switch_to.h>
19 unsigned long begin, end;
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);
26 static inline bool on_stack(struct stack_info *info,
27 unsigned long addr, size_t len)
29 if (info->type == STACK_TYPE_UNKNOWN)
31 if (addr + len < addr)
33 return addr >= info->begin && addr + len <= info->end;
36 static inline unsigned long get_stack_pointer(struct task_struct *task,
40 return (unsigned long) kernel_stack_pointer(regs);
42 return current_stack_pointer();
43 return (unsigned long) task->thread.ksp;
47 * Stack layout of a C stack frame.
51 unsigned long back_chain;
52 unsigned long empty1[5];
53 unsigned long gprs[10];
54 unsigned int empty2[8];
58 unsigned long empty1[5];
59 unsigned int empty2[8];
60 unsigned long gprs[10];
61 unsigned long back_chain;
65 #define CALL_ARGS_0() \
66 register unsigned long r2 asm("2")
67 #define CALL_ARGS_1(arg1) \
68 register unsigned long r2 asm("2") = (unsigned long)(arg1)
69 #define CALL_ARGS_2(arg1, arg2) \
71 register unsigned long r3 asm("3") = (unsigned long)(arg2)
72 #define CALL_ARGS_3(arg1, arg2, arg3) \
73 CALL_ARGS_2(arg1, arg2); \
74 register unsigned long r4 asm("4") = (unsigned long)(arg3)
75 #define CALL_ARGS_4(arg1, arg2, arg3, arg4) \
76 CALL_ARGS_3(arg1, arg2, arg3); \
77 register unsigned long r4 asm("5") = (unsigned long)(arg4)
78 #define CALL_ARGS_5(arg1, arg2, arg3, arg4, arg5) \
79 CALL_ARGS_4(arg1, arg2, arg3, arg4); \
80 register unsigned long r4 asm("6") = (unsigned long)(arg5)
83 * To keep this simple mark register 2-6 as being changed (volatile)
84 * by the called function, even though register 6 is saved/nonvolatile.
86 #define CALL_FMT_0 "=&d" (r2)
87 #define CALL_FMT_1 "+&d" (r2)
88 #define CALL_FMT_2 CALL_FMT_1, "+&d" (r3)
89 #define CALL_FMT_3 CALL_FMT_2, "+&d" (r4)
90 #define CALL_FMT_4 CALL_FMT_3, "+&d" (r5)
91 #define CALL_FMT_5 CALL_FMT_4, "+&d" (r6)
93 #define CALL_CLOBBER_5 "0", "1", "14", "cc", "memory"
94 #define CALL_CLOBBER_4 CALL_CLOBBER_5
95 #define CALL_CLOBBER_3 CALL_CLOBBER_4, "5"
96 #define CALL_CLOBBER_2 CALL_CLOBBER_3, "4"
97 #define CALL_CLOBBER_1 CALL_CLOBBER_2, "3"
98 #define CALL_CLOBBER_0 CALL_CLOBBER_1
100 #define CALL_ON_STACK(fn, stack, nr, args...) \
102 CALL_ARGS_##nr(args); \
103 unsigned long prev; \
106 " la %[_prev],0(15)\n" \
107 " la 15,0(%[_stack])\n" \
108 " stg %[_prev],%[_bc](15)\n" \
109 " brasl 14,%[_fn]\n" \
110 " la 15,0(%[_prev])\n" \
111 : [_prev] "=&a" (prev), CALL_FMT_##nr \
112 : [_stack] "a" (stack), \
113 [_bc] "i" (offsetof(struct stack_frame, back_chain)), \
114 [_fn] "X" (fn) : CALL_CLOBBER_##nr); \
118 #define CALL_LARGS_0(...) \
120 #define CALL_LARGS_1(t1, a1) \
121 long arg1 = (long)(t1)(a1)
122 #define CALL_LARGS_2(t1, a1, t2, a2) \
123 CALL_LARGS_1(t1, a1); \
124 long arg2 = (long)(t2)(a2)
125 #define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \
126 CALL_LARGS_2(t1, a1, t2, a2); \
127 long arg3 = (long)(t3)(a3)
128 #define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \
129 CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \
130 long arg4 = (long)(t4)(a4)
131 #define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
132 CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \
133 long arg5 = (long)(t5)(a5)
135 #define CALL_REGS_0 \
136 register long r2 asm("2") = dummy
137 #define CALL_REGS_1 \
138 register long r2 asm("2") = arg1
139 #define CALL_REGS_2 \
141 register long r3 asm("3") = arg2
142 #define CALL_REGS_3 \
144 register long r4 asm("4") = arg3
145 #define CALL_REGS_4 \
147 register long r5 asm("5") = arg4
148 #define CALL_REGS_5 \
150 register long r6 asm("6") = arg5
152 #define CALL_TYPECHECK_0(...)
153 #define CALL_TYPECHECK_1(t, a, ...) \
155 #define CALL_TYPECHECK_2(t, a, ...) \
156 CALL_TYPECHECK_1(__VA_ARGS__); \
158 #define CALL_TYPECHECK_3(t, a, ...) \
159 CALL_TYPECHECK_2(__VA_ARGS__); \
161 #define CALL_TYPECHECK_4(t, a, ...) \
162 CALL_TYPECHECK_3(__VA_ARGS__); \
164 #define CALL_TYPECHECK_5(t, a, ...) \
165 CALL_TYPECHECK_4(__VA_ARGS__); \
168 #define CALL_PARM_0(...) void
169 #define CALL_PARM_1(t, a, ...) t
170 #define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
171 #define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
172 #define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
173 #define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
174 #define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
177 * Use call_on_stack() to call a function switching to a specified
178 * stack. Proper sign and zero extension of function arguments is
181 * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
183 * - nr specifies the number of function arguments of fn.
184 * - stack specifies the stack to be used.
185 * - fn is the function to be called.
186 * - rettype is the return type of fn.
187 * - t1, a1, ... are pairs, where t1 must match the type of the first
188 * argument of fn, t2 the second, etc. a1 is the corresponding
189 * first function argument (not name), etc.
191 #define call_on_stack(nr, stack, rettype, fn, ...) \
193 rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \
194 unsigned long frame = current_frame_address(); \
195 unsigned long __stack = stack; \
196 unsigned long prev; \
197 CALL_LARGS_##nr(__VA_ARGS__); \
200 CALL_TYPECHECK_##nr(__VA_ARGS__); \
202 " lgr %[_prev],15\n" \
203 " lg 15,%[_stack]\n" \
204 " stg %[_frame],%[_bc](15)\n" \
205 " brasl 14,%[_fn]\n" \
206 " lgr 15,%[_prev]\n" \
207 : [_prev] "=&d" (prev), CALL_FMT_##nr \
208 : [_stack] "R" (__stack), \
209 [_bc] "i" (offsetof(struct stack_frame, back_chain)), \
210 [_frame] "d" (frame), \
211 [_fn] "X" (__fn) : CALL_CLOBBER_##nr); \
215 #define CALL_ON_STACK_NORETURN(fn, stack) \
218 " la 15,0(%[_stack])\n" \
219 " xc %[_bc](8,15),%[_bc](15)\n" \
220 " brasl 14,%[_fn]\n" \
221 ::[_bc] "i" (offsetof(struct stack_frame, back_chain)), \
222 [_stack] "a" (stack), [_fn] "X" (fn)); \
226 #endif /* _ASM_S390_STACKTRACE_H */