stacktrace.c 5.5 KB

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