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