stacktrace.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 ARM Limited
  4. * Copyright (C) 2014 Regents of the University of California
  5. */
  6. #include <linux/export.h>
  7. #include <linux/kallsyms.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/stacktrace.h>
  12. #include <linux/ftrace.h>
  13. #include <asm/stacktrace.h>
  14. #ifdef CONFIG_FRAME_POINTER
  15. extern asmlinkage void handle_exception(void);
  16. extern unsigned long ret_from_exception_end;
  17. static inline int fp_is_valid(unsigned long fp, unsigned long sp)
  18. {
  19. unsigned long low, high;
  20. low = sp + sizeof(struct stackframe);
  21. high = ALIGN(sp, THREAD_SIZE);
  22. return !(fp < low || fp > high || fp & 0x07);
  23. }
  24. void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
  25. bool (*fn)(void *, unsigned long), void *arg)
  26. {
  27. unsigned long fp, sp, pc;
  28. int graph_idx = 0;
  29. int level = 0;
  30. if (regs) {
  31. fp = frame_pointer(regs);
  32. sp = user_stack_pointer(regs);
  33. pc = instruction_pointer(regs);
  34. } else if (task == NULL || task == current) {
  35. fp = (unsigned long)__builtin_frame_address(0);
  36. sp = current_stack_pointer;
  37. pc = (unsigned long)walk_stackframe;
  38. level = -1;
  39. } else {
  40. /* task blocked in __switch_to */
  41. fp = task->thread.s[0];
  42. sp = task->thread.sp;
  43. pc = task->thread.ra;
  44. }
  45. for (;;) {
  46. struct stackframe *frame;
  47. if (unlikely(!__kernel_text_address(pc) || (level++ >= 0 && !fn(arg, pc))))
  48. break;
  49. if (unlikely(!fp_is_valid(fp, sp)))
  50. break;
  51. /* Unwind stack frame */
  52. frame = (struct stackframe *)fp - 1;
  53. sp = fp;
  54. if (regs && (regs->epc == pc) && fp_is_valid(frame->ra, sp)) {
  55. /* We hit function where ra is not saved on the stack */
  56. fp = frame->ra;
  57. pc = regs->ra;
  58. } else {
  59. fp = frame->fp;
  60. pc = ftrace_graph_ret_addr(current, &graph_idx, frame->ra,
  61. &frame->ra);
  62. if (pc >= (unsigned long)handle_exception &&
  63. pc < (unsigned long)&ret_from_exception_end) {
  64. if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
  65. break;
  66. pc = ((struct pt_regs *)sp)->epc;
  67. fp = ((struct pt_regs *)sp)->s0;
  68. }
  69. }
  70. }
  71. }
  72. #else /* !CONFIG_FRAME_POINTER */
  73. void notrace walk_stackframe(struct task_struct *task,
  74. struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
  75. {
  76. unsigned long sp, pc;
  77. unsigned long *ksp;
  78. if (regs) {
  79. sp = user_stack_pointer(regs);
  80. pc = instruction_pointer(regs);
  81. } else if (task == NULL || task == current) {
  82. sp = current_stack_pointer;
  83. pc = (unsigned long)walk_stackframe;
  84. } else {
  85. /* task blocked in __switch_to */
  86. sp = task->thread.sp;
  87. pc = task->thread.ra;
  88. }
  89. if (unlikely(sp & 0x7))
  90. return;
  91. ksp = (unsigned long *)sp;
  92. while (!kstack_end(ksp)) {
  93. if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
  94. break;
  95. pc = READ_ONCE_NOCHECK(*ksp++) - 0x4;
  96. }
  97. }
  98. #endif /* CONFIG_FRAME_POINTER */
  99. static bool print_trace_address(void *arg, unsigned long pc)
  100. {
  101. const char *loglvl = arg;
  102. print_ip_sym(loglvl, pc);
  103. return true;
  104. }
  105. noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
  106. const char *loglvl)
  107. {
  108. walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
  109. }
  110. void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
  111. {
  112. pr_cont("%sCall Trace:\n", loglvl);
  113. dump_backtrace(NULL, task, loglvl);
  114. }
  115. static bool save_wchan(void *arg, unsigned long pc)
  116. {
  117. if (!in_sched_functions(pc)) {
  118. unsigned long *p = arg;
  119. *p = pc;
  120. return false;
  121. }
  122. return true;
  123. }
  124. unsigned long __get_wchan(struct task_struct *task)
  125. {
  126. unsigned long pc = 0;
  127. if (!try_get_task_stack(task))
  128. return 0;
  129. walk_stackframe(task, NULL, save_wchan, &pc);
  130. put_task_stack(task);
  131. return pc;
  132. }
  133. noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  134. struct task_struct *task, struct pt_regs *regs)
  135. {
  136. walk_stackframe(task, regs, consume_entry, cookie);
  137. }
  138. /*
  139. * Get the return address for a single stackframe and return a pointer to the
  140. * next frame tail.
  141. */
  142. static unsigned long unwind_user_frame(stack_trace_consume_fn consume_entry,
  143. void *cookie, unsigned long fp,
  144. unsigned long reg_ra)
  145. {
  146. struct stackframe buftail;
  147. unsigned long ra = 0;
  148. unsigned long __user *user_frame_tail =
  149. (unsigned long __user *)(fp - sizeof(struct stackframe));
  150. /* Check accessibility of one struct frame_tail beyond */
  151. if (!access_ok(user_frame_tail, sizeof(buftail)))
  152. return 0;
  153. if (__copy_from_user_inatomic(&buftail, user_frame_tail,
  154. sizeof(buftail)))
  155. return 0;
  156. ra = reg_ra ? : buftail.ra;
  157. fp = buftail.fp;
  158. if (!ra || !consume_entry(cookie, ra))
  159. return 0;
  160. return fp;
  161. }
  162. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  163. const struct pt_regs *regs)
  164. {
  165. unsigned long fp = 0;
  166. fp = regs->s0;
  167. if (!consume_entry(cookie, regs->epc))
  168. return;
  169. fp = unwind_user_frame(consume_entry, cookie, fp, regs->ra);
  170. while (fp && !(fp & 0x7))
  171. fp = unwind_user_frame(consume_entry, cookie, fp, 0);
  172. }