stacktrace.c 6.0 KB

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