stacktrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/kprobes.h>
  22. #include <linux/sched.h>
  23. #include <linux/sched/debug.h>
  24. #include <linux/sched/task_stack.h>
  25. #include <linux/stacktrace.h>
  26. #include <asm/irq.h>
  27. #include <asm/stack_pointer.h>
  28. #include <asm/stacktrace.h>
  29. /*
  30. * AArch64 PCS assigns the frame pointer to x29.
  31. *
  32. * A simple function prologue looks like this:
  33. * sub sp, sp, #0x10
  34. * stp x29, x30, [sp]
  35. * mov x29, sp
  36. *
  37. * A simple function epilogue looks like this:
  38. * mov sp, x29
  39. * ldp x29, x30, [sp]
  40. * add sp, sp, #0x10
  41. */
  42. int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
  43. {
  44. unsigned long fp = frame->fp;
  45. if (fp & 0xf)
  46. return -EINVAL;
  47. if (!tsk)
  48. tsk = current;
  49. if (!on_accessible_stack(tsk, fp, NULL))
  50. return -EINVAL;
  51. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
  52. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
  53. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  54. if (tsk->ret_stack &&
  55. (frame->pc == (unsigned long)return_to_handler)) {
  56. if (WARN_ON_ONCE(frame->graph == -1))
  57. return -EINVAL;
  58. if (frame->graph < -1)
  59. frame->graph += FTRACE_NOTRACE_DEPTH;
  60. /*
  61. * This is a case where function graph tracer has
  62. * modified a return address (LR) in a stack frame
  63. * to hook a function return.
  64. * So replace it to an original value.
  65. */
  66. frame->pc = tsk->ret_stack[frame->graph--].ret;
  67. }
  68. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  69. /*
  70. * Frames created upon entry from EL0 have NULL FP and PC values, so
  71. * don't bother reporting these. Frames created by __noreturn functions
  72. * might have a valid FP even if PC is bogus, so only terminate where
  73. * both are NULL.
  74. */
  75. if (!frame->fp && !frame->pc)
  76. return -EINVAL;
  77. return 0;
  78. }
  79. NOKPROBE_SYMBOL(unwind_frame);
  80. void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
  81. int (*fn)(struct stackframe *, void *), void *data)
  82. {
  83. while (1) {
  84. int ret;
  85. if (fn(frame, data))
  86. break;
  87. ret = unwind_frame(tsk, frame);
  88. if (ret < 0)
  89. break;
  90. }
  91. }
  92. NOKPROBE_SYMBOL(walk_stackframe);
  93. #ifdef CONFIG_STACKTRACE
  94. struct stack_trace_data {
  95. struct stack_trace *trace;
  96. unsigned int no_sched_functions;
  97. unsigned int skip;
  98. };
  99. static int save_trace(struct stackframe *frame, void *d)
  100. {
  101. struct stack_trace_data *data = d;
  102. struct stack_trace *trace = data->trace;
  103. unsigned long addr = frame->pc;
  104. if (data->no_sched_functions && in_sched_functions(addr))
  105. return 0;
  106. if (data->skip) {
  107. data->skip--;
  108. return 0;
  109. }
  110. trace->entries[trace->nr_entries++] = addr;
  111. return trace->nr_entries >= trace->max_entries;
  112. }
  113. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  114. {
  115. struct stack_trace_data data;
  116. struct stackframe frame;
  117. data.trace = trace;
  118. data.skip = trace->skip;
  119. data.no_sched_functions = 0;
  120. frame.fp = regs->regs[29];
  121. frame.pc = regs->pc;
  122. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  123. frame.graph = current->curr_ret_stack;
  124. #endif
  125. walk_stackframe(current, &frame, save_trace, &data);
  126. if (trace->nr_entries < trace->max_entries)
  127. trace->entries[trace->nr_entries++] = ULONG_MAX;
  128. }
  129. static noinline void __save_stack_trace(struct task_struct *tsk,
  130. struct stack_trace *trace, unsigned int nosched)
  131. {
  132. struct stack_trace_data data;
  133. struct stackframe frame;
  134. if (!try_get_task_stack(tsk))
  135. return;
  136. data.trace = trace;
  137. data.skip = trace->skip;
  138. data.no_sched_functions = nosched;
  139. if (tsk != current) {
  140. frame.fp = thread_saved_fp(tsk);
  141. frame.pc = thread_saved_pc(tsk);
  142. } else {
  143. /* We don't want this function nor the caller */
  144. data.skip += 2;
  145. frame.fp = (unsigned long)__builtin_frame_address(0);
  146. frame.pc = (unsigned long)__save_stack_trace;
  147. }
  148. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  149. frame.graph = tsk->curr_ret_stack;
  150. #endif
  151. walk_stackframe(tsk, &frame, save_trace, &data);
  152. if (trace->nr_entries < trace->max_entries)
  153. trace->entries[trace->nr_entries++] = ULONG_MAX;
  154. put_task_stack(tsk);
  155. }
  156. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  157. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  158. {
  159. __save_stack_trace(tsk, trace, 1);
  160. }
  161. void save_stack_trace(struct stack_trace *trace)
  162. {
  163. __save_stack_trace(current, trace, 0);
  164. }
  165. EXPORT_SYMBOL_GPL(save_stack_trace);
  166. #endif