1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_STACKTRACE_H
3 #define _ASM_S390_STACKTRACE_H
5 #include <linux/stacktrace.h>
6 #include <linux/uaccess.h>
7 #include <linux/ptrace.h>
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];
16 struct stack_frame_vdso_wrapper {
17 struct stack_frame_user sf;
18 unsigned long return_address;
21 struct perf_callchain_entry_ctx;
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);
38 unsigned long begin, end;
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);
45 static inline bool on_stack(struct stack_info *info,
46 unsigned long addr, size_t len)
48 if (info->type == STACK_TYPE_UNKNOWN)
50 if (addr + len < addr)
52 return addr >= info->begin && addr + len <= info->end;
56 * Stack layout of a C stack frame.
57 * Kernel uses the packed stack layout (-mpacked-stack).
61 unsigned long empty[9];
63 unsigned long sie_control_block;
64 unsigned long sie_savearea;
65 unsigned long sie_reason;
66 unsigned long sie_flags;
67 unsigned long sie_control_block_phys;
70 unsigned long gprs[10];
71 unsigned long back_chain;
75 * Unlike current_stack_pointer which simply contains the current value of %r15
76 * current_frame_address() returns function stack frame address, which matches
77 * %r15 upon function invocation. It may differ from %r15 later if function
78 * allocates stack for local variables or new stack frame to call other
81 #define current_frame_address() \
82 ((unsigned long)__builtin_frame_address(0) - \
83 offsetof(struct stack_frame, back_chain))
85 static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
89 return (unsigned long)kernel_stack_pointer(regs);
91 return current_frame_address();
92 return (unsigned long)task->thread.ksp;
96 * To keep this simple mark register 2-6 as being changed (volatile)
97 * by the called function, even though register 6 is saved/nonvolatile.
99 #define CALL_FMT_0 "=&d" (r2)
100 #define CALL_FMT_1 "+&d" (r2)
101 #define CALL_FMT_2 CALL_FMT_1, "+&d" (r3)
102 #define CALL_FMT_3 CALL_FMT_2, "+&d" (r4)
103 #define CALL_FMT_4 CALL_FMT_3, "+&d" (r5)
104 #define CALL_FMT_5 CALL_FMT_4, "+&d" (r6)
106 #define CALL_CLOBBER_5 "0", "1", "14", "cc", "memory"
107 #define CALL_CLOBBER_4 CALL_CLOBBER_5
108 #define CALL_CLOBBER_3 CALL_CLOBBER_4, "5"
109 #define CALL_CLOBBER_2 CALL_CLOBBER_3, "4"
110 #define CALL_CLOBBER_1 CALL_CLOBBER_2, "3"
111 #define CALL_CLOBBER_0 CALL_CLOBBER_1
113 #define CALL_LARGS_0(...) \
115 #define CALL_LARGS_1(t1, a1) \
116 long arg1 = (long)(t1)(a1)
117 #define CALL_LARGS_2(t1, a1, t2, a2) \
118 CALL_LARGS_1(t1, a1); \
119 long arg2 = (long)(t2)(a2)
120 #define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \
121 CALL_LARGS_2(t1, a1, t2, a2); \
122 long arg3 = (long)(t3)(a3)
123 #define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \
124 CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \
125 long arg4 = (long)(t4)(a4)
126 #define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
127 CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \
128 long arg5 = (long)(t5)(a5)
130 #define CALL_REGS_0 \
131 register long r2 asm("2") = dummy
132 #define CALL_REGS_1 \
133 register long r2 asm("2") = arg1
134 #define CALL_REGS_2 \
136 register long r3 asm("3") = arg2
137 #define CALL_REGS_3 \
139 register long r4 asm("4") = arg3
140 #define CALL_REGS_4 \
142 register long r5 asm("5") = arg4
143 #define CALL_REGS_5 \
145 register long r6 asm("6") = arg5
147 #define CALL_TYPECHECK_0(...)
148 #define CALL_TYPECHECK_1(t, a, ...) \
150 #define CALL_TYPECHECK_2(t, a, ...) \
151 CALL_TYPECHECK_1(__VA_ARGS__); \
153 #define CALL_TYPECHECK_3(t, a, ...) \
154 CALL_TYPECHECK_2(__VA_ARGS__); \
156 #define CALL_TYPECHECK_4(t, a, ...) \
157 CALL_TYPECHECK_3(__VA_ARGS__); \
159 #define CALL_TYPECHECK_5(t, a, ...) \
160 CALL_TYPECHECK_4(__VA_ARGS__); \
163 #define CALL_PARM_0(...) void
164 #define CALL_PARM_1(t, a, ...) t
165 #define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
166 #define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
167 #define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
168 #define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
169 #define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
172 * Use call_on_stack() to call a function switching to a specified
173 * stack. Proper sign and zero extension of function arguments is
176 * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
178 * - nr specifies the number of function arguments of fn.
179 * - stack specifies the stack to be used.
180 * - fn is the function to be called.
181 * - rettype is the return type of fn.
182 * - t1, a1, ... are pairs, where t1 must match the type of the first
183 * argument of fn, t2 the second, etc. a1 is the corresponding
184 * first function argument (not name), etc.
186 #define call_on_stack(nr, stack, rettype, fn, ...) \
188 rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \
189 unsigned long frame = current_frame_address(); \
190 unsigned long __stack = stack; \
191 unsigned long prev; \
192 CALL_LARGS_##nr(__VA_ARGS__); \
195 CALL_TYPECHECK_##nr(__VA_ARGS__); \
197 " lgr %[_prev],15\n" \
198 " lg 15,%[_stack]\n" \
199 " stg %[_frame],%[_bc](15)\n" \
200 " brasl 14,%[_fn]\n" \
201 " lgr 15,%[_prev]\n" \
202 : [_prev] "=&d" (prev), CALL_FMT_##nr \
203 : [_stack] "R" (__stack), \
204 [_bc] "i" (offsetof(struct stack_frame, back_chain)), \
205 [_frame] "d" (frame), \
206 [_fn] "X" (__fn) : CALL_CLOBBER_##nr); \
211 * Use call_nodat() to call a function with DAT disabled.
212 * Proper sign and zero extension of function arguments is done.
215 * rc = call_nodat(nr, rettype, fn, t1, a1, t2, a2, ...)
217 * - nr specifies the number of function arguments of fn.
218 * - fn is the function to be called, where fn is a physical address.
219 * - rettype is the return type of fn.
220 * - t1, a1, ... are pairs, where t1 must match the type of the first
221 * argument of fn, t2 the second, etc. a1 is the corresponding
222 * first function argument (not name), etc.
224 * fn() is called with standard C function call ABI, with the exception
225 * that no useful stackframe or stackpointer is passed via register 15.
226 * Therefore the called function must not use r15 to access the stack.
228 #define call_nodat(nr, rettype, fn, ...) \
230 rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = (fn); \
231 /* aligned since psw_leave must not cross page boundary */ \
232 psw_t __aligned(16) psw_leave; \
234 CALL_LARGS_##nr(__VA_ARGS__); \
237 CALL_TYPECHECK_##nr(__VA_ARGS__); \
238 psw_enter.mask = PSW_KERNEL_BITS & ~PSW_MASK_DAT; \
239 psw_enter.addr = (unsigned long)__fn; \
242 " risbg 1,0,0,31,32\n" \
244 " stg 1,%[psw_leave]\n" \
245 " stg 7,8+%[psw_leave]\n" \
246 " la 7,%[psw_leave]\n" \
250 " lpswe %[psw_enter]\n" \
253 : CALL_FMT_##nr, [psw_leave] "=Q" (psw_leave) \
254 : [psw_enter] "Q" (psw_enter) \
255 : "7", CALL_CLOBBER_##nr); \
259 #endif /* _ASM_S390_STACKTRACE_H */