stacktrace.c 7.2 KB

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