stacktrace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/stacktrace.h>
  23. #include <asm/irq.h>
  24. #include <asm/stacktrace.h>
  25. /*
  26. * AArch64 PCS assigns the frame pointer to x29.
  27. *
  28. * A simple function prologue looks like this:
  29. * sub sp, sp, #0x10
  30. * stp x29, x30, [sp]
  31. * mov x29, sp
  32. *
  33. * A simple function epilogue looks like this:
  34. * mov sp, x29
  35. * ldp x29, x30, [sp]
  36. * add sp, sp, #0x10
  37. */
  38. int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
  39. {
  40. unsigned long high, low;
  41. unsigned long fp = frame->fp;
  42. unsigned long irq_stack_ptr;
  43. if (!tsk)
  44. tsk = current;
  45. /*
  46. * Switching between stacks is valid when tracing current and in
  47. * non-preemptible context.
  48. */
  49. if (tsk == current && !preemptible())
  50. irq_stack_ptr = IRQ_STACK_PTR(smp_processor_id());
  51. else
  52. irq_stack_ptr = 0;
  53. low = frame->sp;
  54. /* irq stacks are not THREAD_SIZE aligned */
  55. if (on_irq_stack(frame->sp, raw_smp_processor_id()))
  56. high = irq_stack_ptr;
  57. else
  58. high = ALIGN(low, THREAD_SIZE) - 0x20;
  59. if (fp < low || fp > high || fp & 0xf)
  60. return -EINVAL;
  61. frame->sp = fp + 0x10;
  62. frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
  63. frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
  64. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  65. if (tsk->ret_stack &&
  66. (frame->pc == (unsigned long)return_to_handler)) {
  67. if (WARN_ON_ONCE(frame->graph == -1))
  68. return -EINVAL;
  69. if (frame->graph < -1)
  70. frame->graph += FTRACE_NOTRACE_DEPTH;
  71. /*
  72. * This is a case where function graph tracer has
  73. * modified a return address (LR) in a stack frame
  74. * to hook a function return.
  75. * So replace it to an original value.
  76. */
  77. frame->pc = tsk->ret_stack[frame->graph--].ret;
  78. }
  79. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  80. /*
  81. * Check whether we are going to walk through from interrupt stack
  82. * to task stack.
  83. * If we reach the end of the stack - and its an interrupt stack,
  84. * unpack the dummy frame to find the original elr.
  85. *
  86. * Check the frame->fp we read from the bottom of the irq_stack,
  87. * and the original task stack pointer are both in current->stack.
  88. */
  89. if (frame->sp == irq_stack_ptr) {
  90. struct pt_regs *irq_args;
  91. unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
  92. if (object_is_on_stack((void *)orig_sp) &&
  93. object_is_on_stack((void *)frame->fp)) {
  94. frame->sp = orig_sp;
  95. /* orig_sp is the saved pt_regs, find the elr */
  96. irq_args = (struct pt_regs *)orig_sp;
  97. frame->pc = irq_args->pc;
  98. } else {
  99. /*
  100. * This frame has a non-standard format, and we
  101. * didn't fix it, because the data looked wrong.
  102. * Refuse to output this frame.
  103. */
  104. return -EINVAL;
  105. }
  106. }
  107. return 0;
  108. }
  109. void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
  110. int (*fn)(struct stackframe *, void *), void *data)
  111. {
  112. while (1) {
  113. int ret;
  114. if (fn(frame, data))
  115. break;
  116. ret = unwind_frame(tsk, frame);
  117. if (ret < 0)
  118. break;
  119. }
  120. }
  121. EXPORT_SYMBOL(walk_stackframe);
  122. #ifdef CONFIG_STACKTRACE
  123. struct stack_trace_data {
  124. struct stack_trace *trace;
  125. unsigned int no_sched_functions;
  126. unsigned int skip;
  127. };
  128. static int save_trace(struct stackframe *frame, void *d)
  129. {
  130. struct stack_trace_data *data = d;
  131. struct stack_trace *trace = data->trace;
  132. unsigned long addr = frame->pc;
  133. if (data->no_sched_functions && in_sched_functions(addr))
  134. return 0;
  135. if (data->skip) {
  136. data->skip--;
  137. return 0;
  138. }
  139. trace->entries[trace->nr_entries++] = addr;
  140. return trace->nr_entries >= trace->max_entries;
  141. }
  142. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  143. {
  144. struct stack_trace_data data;
  145. struct stackframe frame;
  146. data.trace = trace;
  147. data.skip = trace->skip;
  148. data.no_sched_functions = 0;
  149. frame.fp = regs->regs[29];
  150. frame.sp = regs->sp;
  151. frame.pc = regs->pc;
  152. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  153. frame.graph = current->curr_ret_stack;
  154. #endif
  155. walk_stackframe(current, &frame, save_trace, &data);
  156. if (trace->nr_entries < trace->max_entries)
  157. trace->entries[trace->nr_entries++] = ULONG_MAX;
  158. }
  159. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  160. {
  161. struct stack_trace_data data;
  162. struct stackframe frame;
  163. data.trace = trace;
  164. data.skip = trace->skip;
  165. if (tsk != current) {
  166. data.no_sched_functions = 1;
  167. frame.fp = thread_saved_fp(tsk);
  168. frame.sp = thread_saved_sp(tsk);
  169. frame.pc = thread_saved_pc(tsk);
  170. } else {
  171. data.no_sched_functions = 0;
  172. frame.fp = (unsigned long)__builtin_frame_address(0);
  173. frame.sp = current_stack_pointer;
  174. frame.pc = (unsigned long)save_stack_trace_tsk;
  175. }
  176. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  177. frame.graph = tsk->curr_ret_stack;
  178. #endif
  179. walk_stackframe(tsk, &frame, save_trace, &data);
  180. if (trace->nr_entries < trace->max_entries)
  181. trace->entries[trace->nr_entries++] = ULONG_MAX;
  182. }
  183. void save_stack_trace(struct stack_trace *trace)
  184. {
  185. save_stack_trace_tsk(current, trace);
  186. }
  187. EXPORT_SYMBOL_GPL(save_stack_trace);
  188. #endif