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