stacktrace.c 5.4 KB

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