stacktrace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/kprobes.h>
  4. #include <linux/sched.h>
  5. #include <linux/sched/debug.h>
  6. #include <linux/stacktrace.h>
  7. #include <asm/sections.h>
  8. #include <asm/stacktrace.h>
  9. #include <asm/traps.h>
  10. #include "reboot.h"
  11. #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
  12. /*
  13. * Unwind the current stack frame and store the new register values in the
  14. * structure passed as argument. Unwinding is equivalent to a function return,
  15. * hence the new PC value rather than LR should be used for backtrace.
  16. *
  17. * With framepointer enabled, a simple function prologue looks like this:
  18. * mov ip, sp
  19. * stmdb sp!, {fp, ip, lr, pc}
  20. * sub fp, ip, #4
  21. *
  22. * A simple function epilogue looks like this:
  23. * ldm sp, {fp, sp, pc}
  24. *
  25. * When compiled with clang, pc and sp are not pushed. A simple function
  26. * prologue looks like this when built with clang:
  27. *
  28. * stmdb {..., fp, lr}
  29. * add fp, sp, #x
  30. * sub sp, sp, #y
  31. *
  32. * A simple function epilogue looks like this when built with clang:
  33. *
  34. * sub sp, fp, #x
  35. * ldm {..., fp, pc}
  36. *
  37. *
  38. * Note that with framepointer enabled, even the leaf functions have the same
  39. * prologue and epilogue, therefore we can ignore the LR value in this case.
  40. */
  41. extern unsigned long call_with_stack_end;
  42. static int frame_pointer_check(struct stackframe *frame)
  43. {
  44. unsigned long high, low;
  45. unsigned long fp = frame->fp;
  46. unsigned long pc = frame->pc;
  47. /*
  48. * call_with_stack() is the only place we allow SP to jump from one
  49. * stack to another, with FP and SP pointing to different stacks,
  50. * skipping the FP boundary check at this point.
  51. */
  52. if (pc >= (unsigned long)&call_with_stack &&
  53. pc < (unsigned long)&call_with_stack_end)
  54. return 0;
  55. /* only go to a higher address on the stack */
  56. low = frame->sp;
  57. high = ALIGN(low, THREAD_SIZE);
  58. /* check current frame pointer is within bounds */
  59. #ifdef CONFIG_CC_IS_CLANG
  60. if (fp < low + 4 || fp > high - 4)
  61. return -EINVAL;
  62. #else
  63. if (fp < low + 12 || fp > high - 4)
  64. return -EINVAL;
  65. #endif
  66. return 0;
  67. }
  68. int notrace unwind_frame(struct stackframe *frame)
  69. {
  70. unsigned long fp = frame->fp;
  71. if (frame_pointer_check(frame))
  72. return -EINVAL;
  73. /*
  74. * When we unwind through an exception stack, include the saved PC
  75. * value into the stack trace.
  76. */
  77. if (frame->ex_frame) {
  78. struct pt_regs *regs = (struct pt_regs *)frame->sp;
  79. /*
  80. * We check that 'regs + sizeof(struct pt_regs)' (that is,
  81. * &regs[1]) does not exceed the bottom of the stack to avoid
  82. * accessing data outside the task's stack. This may happen
  83. * when frame->ex_frame is a false positive.
  84. */
  85. if ((unsigned long)&regs[1] > ALIGN(frame->sp, THREAD_SIZE))
  86. return -EINVAL;
  87. frame->pc = regs->ARM_pc;
  88. frame->ex_frame = false;
  89. return 0;
  90. }
  91. /* restore the registers from the stack frame */
  92. #ifdef CONFIG_CC_IS_CLANG
  93. frame->sp = frame->fp;
  94. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
  95. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
  96. #else
  97. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
  98. frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
  99. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
  100. #endif
  101. #ifdef CONFIG_KRETPROBES
  102. if (is_kretprobe_trampoline(frame->pc))
  103. frame->pc = kretprobe_find_ret_addr(frame->tsk,
  104. (void *)frame->fp, &frame->kr_cur);
  105. #endif
  106. if (in_entry_text(frame->pc))
  107. frame->ex_frame = true;
  108. return 0;
  109. }
  110. #endif
  111. void notrace walk_stackframe(struct stackframe *frame,
  112. int (*fn)(struct stackframe *, void *), void *data)
  113. {
  114. while (1) {
  115. int ret;
  116. if (fn(frame, data))
  117. break;
  118. ret = unwind_frame(frame);
  119. if (ret < 0)
  120. break;
  121. }
  122. }
  123. EXPORT_SYMBOL(walk_stackframe);
  124. #ifdef CONFIG_STACKTRACE
  125. struct stack_trace_data {
  126. struct stack_trace *trace;
  127. unsigned int no_sched_functions;
  128. unsigned int skip;
  129. };
  130. static int save_trace(struct stackframe *frame, void *d)
  131. {
  132. struct stack_trace_data *data = d;
  133. struct stack_trace *trace = data->trace;
  134. unsigned long addr = frame->pc;
  135. if (data->no_sched_functions && in_sched_functions(addr))
  136. return 0;
  137. if (data->skip) {
  138. data->skip--;
  139. return 0;
  140. }
  141. trace->entries[trace->nr_entries++] = addr;
  142. return trace->nr_entries >= trace->max_entries;
  143. }
  144. /* This must be noinline to so that our skip calculation works correctly */
  145. static noinline void __save_stack_trace(struct task_struct *tsk,
  146. struct stack_trace *trace, unsigned int nosched)
  147. {
  148. struct stack_trace_data data;
  149. struct stackframe frame;
  150. data.trace = trace;
  151. data.skip = trace->skip;
  152. data.no_sched_functions = nosched;
  153. if (tsk != current) {
  154. #ifdef CONFIG_SMP
  155. /*
  156. * What guarantees do we have here that 'tsk' is not
  157. * running on another CPU? For now, ignore it as we
  158. * can't guarantee we won't explode.
  159. */
  160. return;
  161. #else
  162. frame.fp = thread_saved_fp(tsk);
  163. frame.sp = thread_saved_sp(tsk);
  164. frame.lr = 0; /* recovered from the stack */
  165. frame.pc = thread_saved_pc(tsk);
  166. #endif
  167. } else {
  168. /* We don't want this function nor the caller */
  169. data.skip += 2;
  170. frame.fp = (unsigned long)__builtin_frame_address(0);
  171. frame.sp = current_stack_pointer;
  172. frame.lr = (unsigned long)__builtin_return_address(0);
  173. here:
  174. frame.pc = (unsigned long)&&here;
  175. }
  176. #ifdef CONFIG_KRETPROBES
  177. frame.kr_cur = NULL;
  178. frame.tsk = tsk;
  179. #endif
  180. #ifdef CONFIG_UNWINDER_FRAME_POINTER
  181. frame.ex_frame = false;
  182. #endif
  183. walk_stackframe(&frame, save_trace, &data);
  184. }
  185. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  186. {
  187. struct stack_trace_data data;
  188. struct stackframe frame;
  189. data.trace = trace;
  190. data.skip = trace->skip;
  191. data.no_sched_functions = 0;
  192. frame.fp = regs->ARM_fp;
  193. frame.sp = regs->ARM_sp;
  194. frame.lr = regs->ARM_lr;
  195. frame.pc = regs->ARM_pc;
  196. #ifdef CONFIG_KRETPROBES
  197. frame.kr_cur = NULL;
  198. frame.tsk = current;
  199. #endif
  200. #ifdef CONFIG_UNWINDER_FRAME_POINTER
  201. frame.ex_frame = in_entry_text(frame.pc);
  202. #endif
  203. walk_stackframe(&frame, save_trace, &data);
  204. }
  205. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  206. {
  207. __save_stack_trace(tsk, trace, 1);
  208. }
  209. EXPORT_SYMBOL(save_stack_trace_tsk);
  210. void save_stack_trace(struct stack_trace *trace)
  211. {
  212. __save_stack_trace(current, trace, 0);
  213. }
  214. EXPORT_SYMBOL_GPL(save_stack_trace);
  215. #endif