stacktrace.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. bool (*fn)(void *, unsigned long), void *data)
  113. {
  114. while (1) {
  115. int ret;
  116. if (!fn(data, frame->pc))
  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. static void start_stack_trace(struct stackframe *frame, struct task_struct *task,
  126. unsigned long fp, unsigned long sp,
  127. unsigned long lr, unsigned long pc)
  128. {
  129. frame->fp = fp;
  130. frame->sp = sp;
  131. frame->lr = lr;
  132. frame->pc = pc;
  133. #ifdef CONFIG_KRETPROBES
  134. frame->kr_cur = NULL;
  135. frame->tsk = task;
  136. #endif
  137. #ifdef CONFIG_UNWINDER_FRAME_POINTER
  138. frame->ex_frame = in_entry_text(frame->pc);
  139. #endif
  140. }
  141. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  142. struct task_struct *task, struct pt_regs *regs)
  143. {
  144. struct stackframe frame;
  145. if (regs) {
  146. start_stack_trace(&frame, NULL, regs->ARM_fp, regs->ARM_sp,
  147. regs->ARM_lr, regs->ARM_pc);
  148. } else if (task != current) {
  149. #ifdef CONFIG_SMP
  150. /*
  151. * What guarantees do we have here that 'tsk' is not
  152. * running on another CPU? For now, ignore it as we
  153. * can't guarantee we won't explode.
  154. */
  155. return;
  156. #else
  157. start_stack_trace(&frame, task, thread_saved_fp(task),
  158. thread_saved_sp(task), 0,
  159. thread_saved_pc(task));
  160. #endif
  161. } else {
  162. here:
  163. start_stack_trace(&frame, task,
  164. (unsigned long)__builtin_frame_address(0),
  165. current_stack_pointer,
  166. (unsigned long)__builtin_return_address(0),
  167. (unsigned long)&&here);
  168. /* skip this function */
  169. if (unwind_frame(&frame))
  170. return;
  171. }
  172. walk_stackframe(&frame, consume_entry, cookie);
  173. }
  174. #endif