stacktrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * stacktrace.c : stacktracing APIs needed by rest of kernel
  4. * (wrappers over ARC dwarf based unwinder)
  5. *
  6. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  7. *
  8. * vineetg: aug 2009
  9. * -Implemented CONFIG_STACKTRACE APIs, primarily save_stack_trace_tsk( )
  10. * for displaying task's kernel mode call stack in /proc/<pid>/stack
  11. * -Iterator based approach to have single copy of unwinding core and APIs
  12. * needing unwinding, implement the logic in iterator regarding:
  13. * = which frame onwards to start capture
  14. * = which frame to stop capturing (wchan)
  15. * = specifics of data structs where trace is saved(CONFIG_STACKTRACE etc)
  16. *
  17. * vineetg: March 2009
  18. * -Implemented correct versions of thread_saved_pc() and get_wchan()
  19. *
  20. * rajeshwarr: 2008
  21. * -Initial implementation
  22. */
  23. #include <linux/ptrace.h>
  24. #include <linux/export.h>
  25. #include <linux/stacktrace.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/sched/debug.h>
  28. #include <asm/arcregs.h>
  29. #include <asm/unwind.h>
  30. #include <asm/switch_to.h>
  31. /*-------------------------------------------------------------------------
  32. * Unwinder Iterator
  33. *-------------------------------------------------------------------------
  34. */
  35. #ifdef CONFIG_ARC_DW2_UNWIND
  36. static int
  37. seed_unwind_frame_info(struct task_struct *tsk, struct pt_regs *regs,
  38. struct unwind_frame_info *frame_info)
  39. {
  40. if (regs) {
  41. /*
  42. * Asynchronous unwinding of intr/exception
  43. * - Just uses the pt_regs passed
  44. */
  45. frame_info->task = tsk;
  46. frame_info->regs.r27 = regs->fp;
  47. frame_info->regs.r28 = regs->sp;
  48. frame_info->regs.r31 = regs->blink;
  49. frame_info->regs.r63 = regs->ret;
  50. frame_info->call_frame = 0;
  51. } else if (tsk == NULL || tsk == current) {
  52. /*
  53. * synchronous unwinding (e.g. dump_stack)
  54. * - uses current values of SP and friends
  55. */
  56. unsigned long fp, sp, blink, ret;
  57. frame_info->task = current;
  58. __asm__ __volatile__(
  59. "mov %0,r27\n\t"
  60. "mov %1,r28\n\t"
  61. "mov %2,r31\n\t"
  62. "mov %3,r63\n\t"
  63. : "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)
  64. );
  65. frame_info->regs.r27 = fp;
  66. frame_info->regs.r28 = sp;
  67. frame_info->regs.r31 = blink;
  68. frame_info->regs.r63 = ret;
  69. frame_info->call_frame = 0;
  70. } else {
  71. /*
  72. * Asynchronous unwinding of a likely sleeping task
  73. * - first ensure it is actually sleeping
  74. * - if so, it will be in __switch_to, kernel mode SP of task
  75. * is safe-kept and BLINK at a well known location in there
  76. */
  77. if (tsk->state == TASK_RUNNING)
  78. return -1;
  79. frame_info->task = tsk;
  80. frame_info->regs.r27 = TSK_K_FP(tsk);
  81. frame_info->regs.r28 = TSK_K_ESP(tsk);
  82. frame_info->regs.r31 = TSK_K_BLINK(tsk);
  83. frame_info->regs.r63 = (unsigned int)__switch_to;
  84. /* In the prologue of __switch_to, first FP is saved on stack
  85. * and then SP is copied to FP. Dwarf assumes cfa as FP based
  86. * but we didn't save FP. The value retrieved above is FP's
  87. * state in previous frame.
  88. * As a work around for this, we unwind from __switch_to start
  89. * and adjust SP accordingly. The other limitation is that
  90. * __switch_to macro is dwarf rules are not generated for inline
  91. * assembly code
  92. */
  93. frame_info->regs.r27 = 0;
  94. frame_info->regs.r28 += 60;
  95. frame_info->call_frame = 0;
  96. }
  97. return 0;
  98. }
  99. #endif
  100. notrace noinline unsigned int
  101. arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,
  102. int (*consumer_fn) (unsigned int, void *), void *arg)
  103. {
  104. #ifdef CONFIG_ARC_DW2_UNWIND
  105. int ret = 0, cnt = 0;
  106. unsigned int address;
  107. struct unwind_frame_info frame_info;
  108. if (seed_unwind_frame_info(tsk, regs, &frame_info))
  109. return 0;
  110. while (1) {
  111. address = UNW_PC(&frame_info);
  112. if (!address || !__kernel_text_address(address))
  113. break;
  114. if (consumer_fn(address, arg) == -1)
  115. break;
  116. ret = arc_unwind(&frame_info);
  117. if (ret)
  118. break;
  119. frame_info.regs.r63 = frame_info.regs.r31;
  120. if (cnt++ > 128) {
  121. printk("unwinder looping too long, aborting !\n");
  122. return 0;
  123. }
  124. }
  125. return address; /* return the last address it saw */
  126. #else
  127. /* On ARC, only Dward based unwinder works. fp based backtracing is
  128. * not possible (-fno-omit-frame-pointer) because of the way function
  129. * prelogue is setup (callee regs saved and then fp set and not other
  130. * way around
  131. */
  132. pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
  133. return 0;
  134. #endif
  135. }
  136. /*-------------------------------------------------------------------------
  137. * callbacks called by unwinder iterator to implement kernel APIs
  138. *
  139. * The callback can return -1 to force the iterator to stop, which by default
  140. * keeps going till the bottom-most frame.
  141. *-------------------------------------------------------------------------
  142. */
  143. /* Call-back which plugs into unwinding core to dump the stack in
  144. * case of panic/OOPs/BUG etc
  145. */
  146. static int __print_sym(unsigned int address, void *arg)
  147. {
  148. const char *loglvl = arg;
  149. printk("%s %pS\n", loglvl, (void *)address);
  150. return 0;
  151. }
  152. #ifdef CONFIG_STACKTRACE
  153. /* Call-back which plugs into unwinding core to capture the
  154. * traces needed by kernel on /proc/<pid>/stack
  155. */
  156. static int __collect_all(unsigned int address, void *arg)
  157. {
  158. struct stack_trace *trace = arg;
  159. if (trace->skip > 0)
  160. trace->skip--;
  161. else
  162. trace->entries[trace->nr_entries++] = address;
  163. if (trace->nr_entries >= trace->max_entries)
  164. return -1;
  165. return 0;
  166. }
  167. static int __collect_all_but_sched(unsigned int address, void *arg)
  168. {
  169. struct stack_trace *trace = arg;
  170. if (in_sched_functions(address))
  171. return 0;
  172. if (trace->skip > 0)
  173. trace->skip--;
  174. else
  175. trace->entries[trace->nr_entries++] = address;
  176. if (trace->nr_entries >= trace->max_entries)
  177. return -1;
  178. return 0;
  179. }
  180. #endif
  181. static int __get_first_nonsched(unsigned int address, void *unused)
  182. {
  183. if (in_sched_functions(address))
  184. return 0;
  185. return -1;
  186. }
  187. /*-------------------------------------------------------------------------
  188. * APIs expected by various kernel sub-systems
  189. *-------------------------------------------------------------------------
  190. */
  191. noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs,
  192. const char *loglvl)
  193. {
  194. printk("%s\nStack Trace:\n", loglvl);
  195. arc_unwind_core(tsk, regs, __print_sym, (void *)loglvl);
  196. }
  197. EXPORT_SYMBOL(show_stacktrace);
  198. /* Expected by sched Code */
  199. void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
  200. {
  201. show_stacktrace(tsk, NULL, loglvl);
  202. }
  203. /* Another API expected by schedular, shows up in "ps" as Wait Channel
  204. * Of course just returning schedule( ) would be pointless so unwind until
  205. * the function is not in schedular code
  206. */
  207. unsigned int get_wchan(struct task_struct *tsk)
  208. {
  209. return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);
  210. }
  211. #ifdef CONFIG_STACKTRACE
  212. /*
  213. * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
  214. * A typical use is when /proc/<pid>/stack is queried by userland
  215. */
  216. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  217. {
  218. /* Assumes @tsk is sleeping so unwinds from __switch_to */
  219. arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);
  220. }
  221. void save_stack_trace(struct stack_trace *trace)
  222. {
  223. /* Pass NULL for task so it unwinds the current call frame */
  224. arc_unwind_core(NULL, NULL, __collect_all, trace);
  225. }
  226. EXPORT_SYMBOL_GPL(save_stack_trace);
  227. #endif