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>
20 unsigned long begin, end;
23 const char *stack_type_name(enum stack_type type);
24 int get_stack_info(unsigned long sp, struct task_struct *task,
25 struct stack_info *info, unsigned long *visit_mask);
27 static inline bool on_stack(struct stack_info *info,
28 unsigned long addr, size_t len)
30 if (info->type == STACK_TYPE_UNKNOWN)
32 if (addr + len < addr)
34 return addr >= info->begin && addr + len <= info->end;
38 * Stack layout of a C stack frame.
42 unsigned long back_chain;
43 unsigned long empty1[5];
44 unsigned long gprs[10];
45 unsigned int empty2[8];
49 unsigned long empty1[5];
50 unsigned int empty2[8];
51 unsigned long gprs[10];
52 unsigned long back_chain;
57 * Unlike current_stack_pointer() which simply returns current value of %r15
58 * current_frame_address() returns function stack frame address, which matches
59 * %r15 upon function invocation. It may differ from %r15 later if function
60 * allocates stack for local variables or new stack frame to call other
63 #define current_frame_address() \
64 ((unsigned long)__builtin_frame_address(0) - \
65 offsetof(struct stack_frame, back_chain))
67 static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
71 return (unsigned long)kernel_stack_pointer(regs);
73 return current_frame_address();
74 return (unsigned long)task->thread.ksp;
78 * To keep this simple mark register 2-6 as being changed (volatile)
79 * by the called function, even though register 6 is saved/nonvolatile.
81 #define CALL_FMT_0 "=&d" (r2)
82 #define CALL_FMT_1 "+&d" (r2)
83 #define CALL_FMT_2 CALL_FMT_1, "+&d" (r3)
84 #define CALL_FMT_3 CALL_FMT_2, "+&d" (r4)
85 #define CALL_FMT_4 CALL_FMT_3, "+&d" (r5)
86 #define CALL_FMT_5 CALL_FMT_4, "+&d" (r6)
88 #define CALL_CLOBBER_5 "0", "1", "14", "cc", "memory"
89 #define CALL_CLOBBER_4 CALL_CLOBBER_5
90 #define CALL_CLOBBER_3 CALL_CLOBBER_4, "5"
91 #define CALL_CLOBBER_2 CALL_CLOBBER_3, "4"
92 #define CALL_CLOBBER_1 CALL_CLOBBER_2, "3"
93 #define CALL_CLOBBER_0 CALL_CLOBBER_1
95 #define CALL_LARGS_0(...) \
97 #define CALL_LARGS_1(t1, a1) \
98 long arg1 = (long)(t1)(a1)
99 #define CALL_LARGS_2(t1, a1, t2, a2) \
100 CALL_LARGS_1(t1, a1); \
101 long arg2 = (long)(t2)(a2)
102 #define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \
103 CALL_LARGS_2(t1, a1, t2, a2); \
104 long arg3 = (long)(t3)(a3)
105 #define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \
106 CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \
107 long arg4 = (long)(t4)(a4)
108 #define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
109 CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \
110 long arg5 = (long)(t5)(a5)
112 #define CALL_REGS_0 \
113 register long r2 asm("2") = dummy
114 #define CALL_REGS_1 \
115 register long r2 asm("2") = arg1
116 #define CALL_REGS_2 \
118 register long r3 asm("3") = arg2
119 #define CALL_REGS_3 \
121 register long r4 asm("4") = arg3
122 #define CALL_REGS_4 \
124 register long r5 asm("5") = arg4
125 #define CALL_REGS_5 \
127 register long r6 asm("6") = arg5
129 #define CALL_TYPECHECK_0(...)
130 #define CALL_TYPECHECK_1(t, a, ...) \
132 #define CALL_TYPECHECK_2(t, a, ...) \
133 CALL_TYPECHECK_1(__VA_ARGS__); \
135 #define CALL_TYPECHECK_3(t, a, ...) \
136 CALL_TYPECHECK_2(__VA_ARGS__); \
138 #define CALL_TYPECHECK_4(t, a, ...) \
139 CALL_TYPECHECK_3(__VA_ARGS__); \
141 #define CALL_TYPECHECK_5(t, a, ...) \
142 CALL_TYPECHECK_4(__VA_ARGS__); \
145 #define CALL_PARM_0(...) void
146 #define CALL_PARM_1(t, a, ...) t
147 #define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
148 #define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
149 #define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
150 #define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
151 #define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
154 * Use call_on_stack() to call a function switching to a specified
155 * stack. Proper sign and zero extension of function arguments is
158 * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
160 * - nr specifies the number of function arguments of fn.
161 * - stack specifies the stack to be used.
162 * - fn is the function to be called.
163 * - rettype is the return type of fn.
164 * - t1, a1, ... are pairs, where t1 must match the type of the first
165 * argument of fn, t2 the second, etc. a1 is the corresponding
166 * first function argument (not name), etc.
168 #define call_on_stack(nr, stack, rettype, fn, ...) \
170 rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \
171 unsigned long frame = current_frame_address(); \
172 unsigned long __stack = stack; \
173 unsigned long prev; \
174 CALL_LARGS_##nr(__VA_ARGS__); \
177 CALL_TYPECHECK_##nr(__VA_ARGS__); \
179 " lgr %[_prev],15\n" \
180 " lg 15,%[_stack]\n" \
181 " stg %[_frame],%[_bc](15)\n" \
182 " brasl 14,%[_fn]\n" \
183 " lgr 15,%[_prev]\n" \
184 : [_prev] "=&d" (prev), CALL_FMT_##nr \
185 : [_stack] "R" (__stack), \
186 [_bc] "i" (offsetof(struct stack_frame, back_chain)), \
187 [_frame] "d" (frame), \
188 [_fn] "X" (__fn) : CALL_CLOBBER_##nr); \
192 #define call_on_stack_noreturn(fn, stack) \
194 void (*__fn)(void) = fn; \
197 " la 15,0(%[_stack])\n" \
198 " xc %[_bc](8,15),%[_bc](15)\n" \
199 " brasl 14,%[_fn]\n" \
200 ::[_bc] "i" (offsetof(struct stack_frame, back_chain)), \
201 [_stack] "a" (stack), [_fn] "X" (__fn)); \
205 #endif /* _ASM_S390_STACKTRACE_H */