stacktrace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Kernel and userspace stack tracing.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2013 Tensilica Inc.
  9. * Copyright (C) 2015 Cadence Design Systems Inc.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/sched.h>
  13. #include <linux/stacktrace.h>
  14. #include <asm/stacktrace.h>
  15. #include <asm/traps.h>
  16. #include <asm/uaccess.h>
  17. #if IS_ENABLED(CONFIG_OPROFILE) || IS_ENABLED(CONFIG_PERF_EVENTS)
  18. /* Address of common_exception_return, used to check the
  19. * transition from kernel to user space.
  20. */
  21. extern int common_exception_return;
  22. /* A struct that maps to the part of the frame containing the a0 and
  23. * a1 registers.
  24. */
  25. struct frame_start {
  26. unsigned long a0;
  27. unsigned long a1;
  28. };
  29. void xtensa_backtrace_user(struct pt_regs *regs, unsigned int depth,
  30. int (*ufn)(struct stackframe *frame, void *data),
  31. void *data)
  32. {
  33. unsigned long windowstart = regs->windowstart;
  34. unsigned long windowbase = regs->windowbase;
  35. unsigned long a0 = regs->areg[0];
  36. unsigned long a1 = regs->areg[1];
  37. unsigned long pc = regs->pc;
  38. struct stackframe frame;
  39. int index;
  40. if (!depth--)
  41. return;
  42. frame.pc = pc;
  43. frame.sp = a1;
  44. if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
  45. return;
  46. /* Two steps:
  47. *
  48. * 1. Look through the register window for the
  49. * previous PCs in the call trace.
  50. *
  51. * 2. Look on the stack.
  52. */
  53. /* Step 1. */
  54. /* Rotate WINDOWSTART to move the bit corresponding to
  55. * the current window to the bit #0.
  56. */
  57. windowstart = (windowstart << WSBITS | windowstart) >> windowbase;
  58. /* Look for bits that are set, they correspond to
  59. * valid windows.
  60. */
  61. for (index = WSBITS - 1; (index > 0) && depth; depth--, index--)
  62. if (windowstart & (1 << index)) {
  63. /* Get the PC from a0 and a1. */
  64. pc = MAKE_PC_FROM_RA(a0, pc);
  65. /* Read a0 and a1 from the
  66. * corresponding position in AREGs.
  67. */
  68. a0 = regs->areg[index * 4];
  69. a1 = regs->areg[index * 4 + 1];
  70. frame.pc = pc;
  71. frame.sp = a1;
  72. if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
  73. return;
  74. }
  75. /* Step 2. */
  76. /* We are done with the register window, we need to
  77. * look through the stack.
  78. */
  79. if (!depth)
  80. return;
  81. /* Start from the a1 register. */
  82. /* a1 = regs->areg[1]; */
  83. while (a0 != 0 && depth--) {
  84. struct frame_start frame_start;
  85. /* Get the location for a1, a0 for the
  86. * previous frame from the current a1.
  87. */
  88. unsigned long *psp = (unsigned long *)a1;
  89. psp -= 4;
  90. /* Check if the region is OK to access. */
  91. if (!access_ok(VERIFY_READ, psp, sizeof(frame_start)))
  92. return;
  93. /* Copy a1, a0 from user space stack frame. */
  94. if (__copy_from_user_inatomic(&frame_start, psp,
  95. sizeof(frame_start)))
  96. return;
  97. pc = MAKE_PC_FROM_RA(a0, pc);
  98. a0 = frame_start.a0;
  99. a1 = frame_start.a1;
  100. frame.pc = pc;
  101. frame.sp = a1;
  102. if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
  103. return;
  104. }
  105. }
  106. EXPORT_SYMBOL(xtensa_backtrace_user);
  107. void xtensa_backtrace_kernel(struct pt_regs *regs, unsigned int depth,
  108. int (*kfn)(struct stackframe *frame, void *data),
  109. int (*ufn)(struct stackframe *frame, void *data),
  110. void *data)
  111. {
  112. unsigned long pc = regs->depc > VALID_DOUBLE_EXCEPTION_ADDRESS ?
  113. regs->depc : regs->pc;
  114. unsigned long sp_start, sp_end;
  115. unsigned long a0 = regs->areg[0];
  116. unsigned long a1 = regs->areg[1];
  117. sp_start = a1 & ~(THREAD_SIZE - 1);
  118. sp_end = sp_start + THREAD_SIZE;
  119. /* Spill the register window to the stack first. */
  120. spill_registers();
  121. /* Read the stack frames one by one and create the PC
  122. * from the a0 and a1 registers saved there.
  123. */
  124. while (a1 > sp_start && a1 < sp_end && depth--) {
  125. struct stackframe frame;
  126. unsigned long *psp = (unsigned long *)a1;
  127. frame.pc = pc;
  128. frame.sp = a1;
  129. if (kernel_text_address(pc) && kfn(&frame, data))
  130. return;
  131. if (pc == (unsigned long)&common_exception_return) {
  132. regs = (struct pt_regs *)a1;
  133. if (user_mode(regs)) {
  134. if (ufn == NULL)
  135. return;
  136. xtensa_backtrace_user(regs, depth, ufn, data);
  137. return;
  138. }
  139. a0 = regs->areg[0];
  140. a1 = regs->areg[1];
  141. continue;
  142. }
  143. sp_start = a1;
  144. pc = MAKE_PC_FROM_RA(a0, pc);
  145. a0 = *(psp - 4);
  146. a1 = *(psp - 3);
  147. }
  148. }
  149. EXPORT_SYMBOL(xtensa_backtrace_kernel);
  150. #endif
  151. void walk_stackframe(unsigned long *sp,
  152. int (*fn)(struct stackframe *frame, void *data),
  153. void *data)
  154. {
  155. unsigned long a0, a1;
  156. unsigned long sp_end;
  157. a1 = (unsigned long)sp;
  158. sp_end = ALIGN(a1, THREAD_SIZE);
  159. spill_registers();
  160. while (a1 < sp_end) {
  161. struct stackframe frame;
  162. sp = (unsigned long *)a1;
  163. a0 = *(sp - 4);
  164. a1 = *(sp - 3);
  165. if (a1 <= (unsigned long)sp)
  166. break;
  167. frame.pc = MAKE_PC_FROM_RA(a0, a1);
  168. frame.sp = a1;
  169. if (fn(&frame, data))
  170. return;
  171. }
  172. }
  173. #ifdef CONFIG_STACKTRACE
  174. struct stack_trace_data {
  175. struct stack_trace *trace;
  176. unsigned skip;
  177. };
  178. static int stack_trace_cb(struct stackframe *frame, void *data)
  179. {
  180. struct stack_trace_data *trace_data = data;
  181. struct stack_trace *trace = trace_data->trace;
  182. if (trace_data->skip) {
  183. --trace_data->skip;
  184. return 0;
  185. }
  186. if (!kernel_text_address(frame->pc))
  187. return 0;
  188. trace->entries[trace->nr_entries++] = frame->pc;
  189. return trace->nr_entries >= trace->max_entries;
  190. }
  191. void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
  192. {
  193. struct stack_trace_data trace_data = {
  194. .trace = trace,
  195. .skip = trace->skip,
  196. };
  197. walk_stackframe(stack_pointer(task), stack_trace_cb, &trace_data);
  198. }
  199. EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
  200. void save_stack_trace(struct stack_trace *trace)
  201. {
  202. save_stack_trace_tsk(current, trace);
  203. }
  204. EXPORT_SYMBOL_GPL(save_stack_trace);
  205. #endif
  206. #ifdef CONFIG_FRAME_POINTER
  207. struct return_addr_data {
  208. unsigned long addr;
  209. unsigned skip;
  210. };
  211. static int return_address_cb(struct stackframe *frame, void *data)
  212. {
  213. struct return_addr_data *r = data;
  214. if (r->skip) {
  215. --r->skip;
  216. return 0;
  217. }
  218. if (!kernel_text_address(frame->pc))
  219. return 0;
  220. r->addr = frame->pc;
  221. return 1;
  222. }
  223. unsigned long return_address(unsigned level)
  224. {
  225. struct return_addr_data r = {
  226. .skip = level + 1,
  227. };
  228. walk_stackframe(stack_pointer(NULL), return_address_cb, &r);
  229. return r.addr;
  230. }
  231. EXPORT_SYMBOL(return_address);
  232. #endif