stacktrace.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Stack tracing support
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/export.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/sched.h>
  22. #include <linux/sched/debug.h>
  23. #include <linux/sched/task_stack.h>
  24. #include <linux/stacktrace.h>
  25. #include <asm/irq.h>
  26. #include <asm/stack_pointer.h>
  27. #include <asm/stacktrace.h>
  28. /*
  29. * AArch64 PCS assigns the frame pointer to x29.
  30. *
  31. * A simple function prologue looks like this:
  32. * sub sp, sp, #0x10
  33. * stp x29, x30, [sp]
  34. * mov x29, sp
  35. *
  36. * A simple function epilogue looks like this:
  37. * mov sp, x29
  38. * ldp x29, x30, [sp]
  39. * add sp, sp, #0x10
  40. */
  41. int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
  42. {
  43. unsigned long fp = frame->fp;
  44. if (fp & 0xf)
  45. return -EINVAL;
  46. if (!tsk)
  47. tsk = current;
  48. if (!on_accessible_stack(tsk, fp, NULL))
  49. return -EINVAL;
  50. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
  51. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
  52. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  53. if (tsk->ret_stack &&
  54. (frame->pc == (unsigned long)return_to_handler)) {
  55. if (WARN_ON_ONCE(frame->graph == -1))
  56. return -EINVAL;
  57. if (frame->graph < -1)
  58. frame->graph += FTRACE_NOTRACE_DEPTH;
  59. /*
  60. * This is a case where function graph tracer has
  61. * modified a return address (LR) in a stack frame
  62. * to hook a function return.
  63. * So replace it to an original value.
  64. */
  65. frame->pc = tsk->ret_stack[frame->graph--].ret;
  66. }
  67. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  68. /*
  69. * Frames created upon entry from EL0 have NULL FP and PC values, so
  70. * don't bother reporting these. Frames created by __noreturn functions
  71. * might have a valid FP even if PC is bogus, so only terminate where
  72. * both are NULL.
  73. */
  74. if (!frame->fp && !frame->pc)
  75. return -EINVAL;
  76. return 0;
  77. }
  78. void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
  79. int (*fn)(struct stackframe *, void *), void *data)
  80. {
  81. while (1) {
  82. int ret;
  83. if (fn(frame, data))
  84. break;
  85. ret = unwind_frame(tsk, frame);
  86. if (ret < 0)
  87. break;
  88. }
  89. }
  90. #ifdef CONFIG_STACKTRACE
  91. struct stack_trace_data {
  92. struct stack_trace *trace;
  93. unsigned int no_sched_functions;
  94. unsigned int skip;
  95. };
  96. static int save_trace(struct stackframe *frame, void *d)
  97. {
  98. struct stack_trace_data *data = d;
  99. struct stack_trace *trace = data->trace;
  100. unsigned long addr = frame->pc;
  101. if (data->no_sched_functions && in_sched_functions(addr))
  102. return 0;
  103. if (data->skip) {
  104. data->skip--;
  105. return 0;
  106. }
  107. trace->entries[trace->nr_entries++] = addr;
  108. return trace->nr_entries >= trace->max_entries;
  109. }
  110. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  111. {
  112. struct stack_trace_data data;
  113. struct stackframe frame;
  114. data.trace = trace;
  115. data.skip = trace->skip;
  116. data.no_sched_functions = 0;
  117. frame.fp = regs->regs[29];
  118. frame.pc = regs->pc;
  119. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  120. frame.graph = current->curr_ret_stack;
  121. #endif
  122. walk_stackframe(current, &frame, save_trace, &data);
  123. if (trace->nr_entries < trace->max_entries)
  124. trace->entries[trace->nr_entries++] = ULONG_MAX;
  125. }
  126. static noinline void __save_stack_trace(struct task_struct *tsk,
  127. struct stack_trace *trace, unsigned int nosched)
  128. {
  129. struct stack_trace_data data;
  130. struct stackframe frame;
  131. if (!try_get_task_stack(tsk))
  132. return;
  133. data.trace = trace;
  134. data.skip = trace->skip;
  135. data.no_sched_functions = nosched;
  136. if (tsk != current) {
  137. frame.fp = thread_saved_fp(tsk);
  138. frame.pc = thread_saved_pc(tsk);
  139. } else {
  140. /* We don't want this function nor the caller */
  141. data.skip += 2;
  142. frame.fp = (unsigned long)__builtin_frame_address(0);
  143. frame.pc = (unsigned long)__save_stack_trace;
  144. }
  145. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  146. frame.graph = tsk->curr_ret_stack;
  147. #endif
  148. walk_stackframe(tsk, &frame, save_trace, &data);
  149. if (trace->nr_entries < trace->max_entries)
  150. trace->entries[trace->nr_entries++] = ULONG_MAX;
  151. put_task_stack(tsk);
  152. }
  153. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  154. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  155. {
  156. __save_stack_trace(tsk, trace, 1);
  157. }
  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