stacktrace.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/export.h>
  3. #include <linux/sched.h>
  4. #include <linux/sched/debug.h>
  5. #include <linux/stacktrace.h>
  6. #include <asm/sections.h>
  7. #include <asm/stacktrace.h>
  8. #include <asm/traps.h>
  9. #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
  10. /*
  11. * Unwind the current stack frame and store the new register values in the
  12. * structure passed as argument. Unwinding is equivalent to a function return,
  13. * hence the new PC value rather than LR should be used for backtrace.
  14. *
  15. * With framepointer enabled, a simple function prologue looks like this:
  16. * mov ip, sp
  17. * stmdb sp!, {fp, ip, lr, pc}
  18. * sub fp, ip, #4
  19. *
  20. * A simple function epilogue looks like this:
  21. * ldm sp, {fp, sp, pc}
  22. *
  23. * When compiled with clang, pc and sp are not pushed. A simple function
  24. * prologue looks like this when built with clang:
  25. *
  26. * stmdb {..., fp, lr}
  27. * add fp, sp, #x
  28. * sub sp, sp, #y
  29. *
  30. * A simple function epilogue looks like this when built with clang:
  31. *
  32. * sub sp, fp, #x
  33. * ldm {..., fp, pc}
  34. *
  35. *
  36. * Note that with framepointer enabled, even the leaf functions have the same
  37. * prologue and epilogue, therefore we can ignore the LR value in this case.
  38. */
  39. int notrace unwind_frame(struct stackframe *frame)
  40. {
  41. unsigned long high, low;
  42. unsigned long fp = frame->fp;
  43. /* only go to a higher address on the stack */
  44. low = frame->sp;
  45. high = ALIGN(low, THREAD_SIZE);
  46. #ifdef CONFIG_CC_IS_CLANG
  47. /* check current frame pointer is within bounds */
  48. if (fp < low + 4 || fp > high - 4)
  49. return -EINVAL;
  50. frame->sp = frame->fp;
  51. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
  52. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
  53. #else
  54. /* check current frame pointer is within bounds */
  55. if (fp < low + 12 || fp > high - 4)
  56. return -EINVAL;
  57. /* restore the registers from the stack frame */
  58. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
  59. frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
  60. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
  61. #endif
  62. return 0;
  63. }
  64. #endif
  65. void notrace walk_stackframe(struct stackframe *frame,
  66. int (*fn)(struct stackframe *, void *), void *data)
  67. {
  68. while (1) {
  69. int ret;
  70. if (fn(frame, data))
  71. break;
  72. ret = unwind_frame(frame);
  73. if (ret < 0)
  74. break;
  75. }
  76. }
  77. EXPORT_SYMBOL(walk_stackframe);
  78. #ifdef CONFIG_STACKTRACE
  79. struct stack_trace_data {
  80. struct stack_trace *trace;
  81. unsigned int no_sched_functions;
  82. unsigned int skip;
  83. };
  84. static int save_trace(struct stackframe *frame, void *d)
  85. {
  86. struct stack_trace_data *data = d;
  87. struct stack_trace *trace = data->trace;
  88. struct pt_regs *regs;
  89. unsigned long addr = frame->pc;
  90. if (data->no_sched_functions && in_sched_functions(addr))
  91. return 0;
  92. if (data->skip) {
  93. data->skip--;
  94. return 0;
  95. }
  96. trace->entries[trace->nr_entries++] = addr;
  97. if (trace->nr_entries >= trace->max_entries)
  98. return 1;
  99. if (!in_entry_text(frame->pc))
  100. return 0;
  101. regs = (struct pt_regs *)frame->sp;
  102. if ((unsigned long)&regs[1] > ALIGN(frame->sp, THREAD_SIZE))
  103. return 0;
  104. trace->entries[trace->nr_entries++] = regs->ARM_pc;
  105. return trace->nr_entries >= trace->max_entries;
  106. }
  107. /* This must be noinline to so that our skip calculation works correctly */
  108. static noinline void __save_stack_trace(struct task_struct *tsk,
  109. struct stack_trace *trace, unsigned int nosched)
  110. {
  111. struct stack_trace_data data;
  112. struct stackframe frame;
  113. data.trace = trace;
  114. data.skip = trace->skip;
  115. data.no_sched_functions = nosched;
  116. if (tsk != current) {
  117. #ifdef CONFIG_SMP
  118. /*
  119. * What guarantees do we have here that 'tsk' is not
  120. * running on another CPU? For now, ignore it as we
  121. * can't guarantee we won't explode.
  122. */
  123. return;
  124. #else
  125. frame.fp = thread_saved_fp(tsk);
  126. frame.sp = thread_saved_sp(tsk);
  127. frame.lr = 0; /* recovered from the stack */
  128. frame.pc = thread_saved_pc(tsk);
  129. #endif
  130. } else {
  131. /* We don't want this function nor the caller */
  132. data.skip += 2;
  133. frame.fp = (unsigned long)__builtin_frame_address(0);
  134. frame.sp = current_stack_pointer;
  135. frame.lr = (unsigned long)__builtin_return_address(0);
  136. frame.pc = (unsigned long)__save_stack_trace;
  137. }
  138. walk_stackframe(&frame, save_trace, &data);
  139. }
  140. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  141. {
  142. struct stack_trace_data data;
  143. struct stackframe frame;
  144. data.trace = trace;
  145. data.skip = trace->skip;
  146. data.no_sched_functions = 0;
  147. frame.fp = regs->ARM_fp;
  148. frame.sp = regs->ARM_sp;
  149. frame.lr = regs->ARM_lr;
  150. frame.pc = regs->ARM_pc;
  151. walk_stackframe(&frame, save_trace, &data);
  152. }
  153. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  154. {
  155. __save_stack_trace(tsk, trace, 1);
  156. }
  157. EXPORT_SYMBOL(save_stack_trace_tsk);
  158. void save_stack_trace(struct stack_trace *trace)
  159. {
  160. __save_stack_trace(current, trace, 0);
  161. }
  162. EXPORT_SYMBOL_GPL(save_stack_trace);
  163. #endif