stacktrace.c 6.0 KB

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