stacktrace.c 4.8 KB

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