stacktrace.h 6.0 KB

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