stacktrace.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /*
  44. * Use raw_smp_processor_id() to avoid false-positives from
  45. * CONFIG_DEBUG_PREEMPT. get_wchan() calls unwind_frame() on sleeping
  46. * task stacks, we can be pre-empted in this case, so
  47. * {raw_,}smp_processor_id() may give us the wrong value. Sleeping
  48. * tasks can't ever be on an interrupt stack, so regardless of cpu,
  49. * the checks will always fail.
  50. */
  51. irq_stack_ptr = IRQ_STACK_PTR(raw_smp_processor_id());
  52. low = frame->sp;
  53. /* irq stacks are not THREAD_SIZE aligned */
  54. if (on_irq_stack(frame->sp, raw_smp_processor_id()))
  55. high = irq_stack_ptr;
  56. else
  57. high = ALIGN(low, THREAD_SIZE) - 0x20;
  58. if (fp < low || fp > high || fp & 0xf)
  59. return -EINVAL;
  60. frame->sp = fp + 0x10;
  61. frame->fp = *(unsigned long *)(fp);
  62. frame->pc = *(unsigned long *)(fp + 8);
  63. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  64. if (tsk && tsk->ret_stack &&
  65. (frame->pc == (unsigned long)return_to_handler)) {
  66. /*
  67. * This is a case where function graph tracer has
  68. * modified a return address (LR) in a stack frame
  69. * to hook a function return.
  70. * So replace it to an original value.
  71. */
  72. frame->pc = tsk->ret_stack[frame->graph--].ret;
  73. }
  74. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  75. /*
  76. * Check whether we are going to walk through from interrupt stack
  77. * to task stack.
  78. * If we reach the end of the stack - and its an interrupt stack,
  79. * unpack the dummy frame to find the original elr.
  80. *
  81. * Check the frame->fp we read from the bottom of the irq_stack,
  82. * and the original task stack pointer are both in current->stack.
  83. */
  84. if (frame->sp == irq_stack_ptr) {
  85. struct pt_regs *irq_args;
  86. unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
  87. if (object_is_on_stack((void *)orig_sp) &&
  88. object_is_on_stack((void *)frame->fp)) {
  89. frame->sp = orig_sp;
  90. /* orig_sp is the saved pt_regs, find the elr */
  91. irq_args = (struct pt_regs *)orig_sp;
  92. frame->pc = irq_args->pc;
  93. } else {
  94. /*
  95. * This frame has a non-standard format, and we
  96. * didn't fix it, because the data looked wrong.
  97. * Refuse to output this frame.
  98. */
  99. return -EINVAL;
  100. }
  101. }
  102. return 0;
  103. }
  104. void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
  105. int (*fn)(struct stackframe *, void *), void *data)
  106. {
  107. while (1) {
  108. int ret;
  109. if (fn(frame, data))
  110. break;
  111. ret = unwind_frame(tsk, frame);
  112. if (ret < 0)
  113. break;
  114. }
  115. }
  116. EXPORT_SYMBOL(walk_stackframe);
  117. #ifdef CONFIG_STACKTRACE
  118. struct stack_trace_data {
  119. struct stack_trace *trace;
  120. unsigned int no_sched_functions;
  121. unsigned int skip;
  122. };
  123. static int save_trace(struct stackframe *frame, void *d)
  124. {
  125. struct stack_trace_data *data = d;
  126. struct stack_trace *trace = data->trace;
  127. unsigned long addr = frame->pc;
  128. if (data->no_sched_functions && in_sched_functions(addr))
  129. return 0;
  130. if (data->skip) {
  131. data->skip--;
  132. return 0;
  133. }
  134. trace->entries[trace->nr_entries++] = addr;
  135. return trace->nr_entries >= trace->max_entries;
  136. }
  137. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  138. {
  139. struct stack_trace_data data;
  140. struct stackframe frame;
  141. data.trace = trace;
  142. data.skip = trace->skip;
  143. if (tsk != current) {
  144. data.no_sched_functions = 1;
  145. frame.fp = thread_saved_fp(tsk);
  146. frame.sp = thread_saved_sp(tsk);
  147. frame.pc = thread_saved_pc(tsk);
  148. } else {
  149. data.no_sched_functions = 0;
  150. frame.fp = (unsigned long)__builtin_frame_address(0);
  151. frame.sp = current_stack_pointer;
  152. frame.pc = (unsigned long)save_stack_trace_tsk;
  153. }
  154. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  155. frame.graph = tsk->curr_ret_stack;
  156. #endif
  157. walk_stackframe(tsk, &frame, save_trace, &data);
  158. if (trace->nr_entries < trace->max_entries)
  159. trace->entries[trace->nr_entries++] = ULONG_MAX;
  160. }
  161. void save_stack_trace(struct stack_trace *trace)
  162. {
  163. save_stack_trace_tsk(current, trace);
  164. }
  165. EXPORT_SYMBOL_GPL(save_stack_trace);
  166. #endif