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