unwind.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Backtrace support for Microblaze
  3. *
  4. * Copyright (C) 2010 Digital Design Corporation
  5. *
  6. * Based on arch/sh/kernel/cpu/sh5/unwind.c code which is:
  7. * Copyright (C) 2004 Paul Mundt
  8. * Copyright (C) 2004 Richard Curnow
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. /* #define DEBUG 1 */
  15. #include <linux/export.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/stacktrace.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/io.h>
  23. #include <asm/sections.h>
  24. #include <asm/exceptions.h>
  25. #include <asm/unwind.h>
  26. #include <asm/switch_to.h>
  27. struct stack_trace;
  28. /*
  29. * On Microblaze, finding the previous stack frame is a little tricky.
  30. * At this writing (3/2010), Microblaze does not support CONFIG_FRAME_POINTERS,
  31. * and even if it did, gcc (4.1.2) does not store the frame pointer at
  32. * a consistent offset within each frame. To determine frame size, it is
  33. * necessary to search for the assembly instruction that creates or reclaims
  34. * the frame and extract the size from it.
  35. *
  36. * Microblaze stores the stack pointer in r1, and creates a frame via
  37. *
  38. * addik r1, r1, -FRAME_SIZE
  39. *
  40. * The frame is reclaimed via
  41. *
  42. * addik r1, r1, FRAME_SIZE
  43. *
  44. * Frame creation occurs at or near the top of a function.
  45. * Depending on the compiler, reclaim may occur at the end, or before
  46. * a mid-function return.
  47. *
  48. * A stack frame is usually not created in a leaf function.
  49. *
  50. */
  51. /**
  52. * get_frame_size - Extract the stack adjustment from an
  53. * "addik r1, r1, adjust" instruction
  54. * @instr : Microblaze instruction
  55. *
  56. * Return - Number of stack bytes the instruction reserves or reclaims
  57. */
  58. static inline long get_frame_size(unsigned long instr)
  59. {
  60. return abs((s16)(instr & 0xFFFF));
  61. }
  62. /**
  63. * find_frame_creation - Search backward to find the instruction that creates
  64. * the stack frame (hopefully, for the same function the
  65. * initial PC is in).
  66. * @pc : Program counter at which to begin the search
  67. *
  68. * Return - PC at which stack frame creation occurs
  69. * NULL if this cannot be found, i.e. a leaf function
  70. */
  71. static unsigned long *find_frame_creation(unsigned long *pc)
  72. {
  73. int i;
  74. /* NOTE: Distance to search is arbitrary
  75. * 250 works well for most things,
  76. * 750 picks up things like tcp_recvmsg(),
  77. * 1000 needed for fat_fill_super()
  78. */
  79. for (i = 0; i < 1000; i++, pc--) {
  80. unsigned long instr;
  81. s16 frame_size;
  82. if (!kernel_text_address((unsigned long) pc))
  83. return NULL;
  84. instr = *pc;
  85. /* addik r1, r1, foo ? */
  86. if ((instr & 0xFFFF0000) != 0x30210000)
  87. continue; /* No */
  88. frame_size = get_frame_size(instr);
  89. if ((frame_size < 8) || (frame_size & 3)) {
  90. pr_debug(" Invalid frame size %d at 0x%p\n",
  91. frame_size, pc);
  92. return NULL;
  93. }
  94. pr_debug(" Found frame creation at 0x%p, size %d\n", pc,
  95. frame_size);
  96. return pc;
  97. }
  98. return NULL;
  99. }
  100. /**
  101. * lookup_prev_stack_frame - Find the stack frame of the previous function.
  102. * @fp : Frame (stack) pointer for current function
  103. * @pc : Program counter within current function
  104. * @leaf_return : r15 value within current function. If the current function
  105. * is a leaf, this is the caller's return address.
  106. * @pprev_fp : On exit, set to frame (stack) pointer for previous function
  107. * @pprev_pc : On exit, set to current function caller's return address
  108. *
  109. * Return - 0 on success, -EINVAL if the previous frame cannot be found
  110. */
  111. static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
  112. unsigned long leaf_return,
  113. unsigned long *pprev_fp,
  114. unsigned long *pprev_pc)
  115. {
  116. unsigned long *prologue = NULL;
  117. /* _switch_to is a special leaf function */
  118. if (pc != (unsigned long) &_switch_to)
  119. prologue = find_frame_creation((unsigned long *)pc);
  120. if (prologue) {
  121. long frame_size = get_frame_size(*prologue);
  122. *pprev_fp = fp + frame_size;
  123. *pprev_pc = *(unsigned long *)fp;
  124. } else {
  125. if (!leaf_return)
  126. return -EINVAL;
  127. *pprev_pc = leaf_return;
  128. *pprev_fp = fp;
  129. }
  130. /* NOTE: don't check kernel_text_address here, to allow display
  131. * of userland return address
  132. */
  133. return (!*pprev_pc || (*pprev_pc & 3)) ? -EINVAL : 0;
  134. }
  135. static void microblaze_unwind_inner(struct task_struct *task,
  136. unsigned long pc, unsigned long fp,
  137. unsigned long leaf_return,
  138. struct stack_trace *trace);
  139. /**
  140. * unwind_trap - Unwind through a system trap, that stored previous state
  141. * on the stack.
  142. */
  143. #ifdef CONFIG_MMU
  144. static inline void unwind_trap(struct task_struct *task, unsigned long pc,
  145. unsigned long fp, struct stack_trace *trace)
  146. {
  147. /* To be implemented */
  148. }
  149. #else
  150. static inline void unwind_trap(struct task_struct *task, unsigned long pc,
  151. unsigned long fp, struct stack_trace *trace)
  152. {
  153. const struct pt_regs *regs = (const struct pt_regs *) fp;
  154. microblaze_unwind_inner(task, regs->pc, regs->r1, regs->r15, trace);
  155. }
  156. #endif
  157. /**
  158. * microblaze_unwind_inner - Unwind the stack from the specified point
  159. * @task : Task whose stack we are to unwind (may be NULL)
  160. * @pc : Program counter from which we start unwinding
  161. * @fp : Frame (stack) pointer from which we start unwinding
  162. * @leaf_return : Value of r15 at pc. If the function is a leaf, this is
  163. * the caller's return address.
  164. * @trace : Where to store stack backtrace (PC values).
  165. * NULL == print backtrace to kernel log
  166. */
  167. static void microblaze_unwind_inner(struct task_struct *task,
  168. unsigned long pc, unsigned long fp,
  169. unsigned long leaf_return,
  170. struct stack_trace *trace)
  171. {
  172. int ofs = 0;
  173. pr_debug(" Unwinding with PC=%p, FP=%p\n", (void *)pc, (void *)fp);
  174. if (!pc || !fp || (pc & 3) || (fp & 3)) {
  175. pr_debug(" Invalid state for unwind, aborting\n");
  176. return;
  177. }
  178. for (; pc != 0;) {
  179. unsigned long next_fp, next_pc = 0;
  180. unsigned long return_to = pc + 2 * sizeof(unsigned long);
  181. const struct trap_handler_info *handler =
  182. &microblaze_trap_handlers;
  183. /* Is previous function the HW exception handler? */
  184. if ((return_to >= (unsigned long)&_hw_exception_handler)
  185. &&(return_to < (unsigned long)&ex_handler_unhandled)) {
  186. /*
  187. * HW exception handler doesn't save all registers,
  188. * so we open-code a special case of unwind_trap()
  189. */
  190. #ifndef CONFIG_MMU
  191. const struct pt_regs *regs =
  192. (const struct pt_regs *) fp;
  193. #endif
  194. pr_info("HW EXCEPTION\n");
  195. #ifndef CONFIG_MMU
  196. microblaze_unwind_inner(task, regs->r17 - 4,
  197. fp + EX_HANDLER_STACK_SIZ,
  198. regs->r15, trace);
  199. #endif
  200. return;
  201. }
  202. /* Is previous function a trap handler? */
  203. for (; handler->start_addr; ++handler) {
  204. if ((return_to >= handler->start_addr)
  205. && (return_to <= handler->end_addr)) {
  206. if (!trace)
  207. pr_info("%s\n", handler->trap_name);
  208. unwind_trap(task, pc, fp, trace);
  209. return;
  210. }
  211. }
  212. pc -= ofs;
  213. if (trace) {
  214. #ifdef CONFIG_STACKTRACE
  215. if (trace->skip > 0)
  216. trace->skip--;
  217. else
  218. trace->entries[trace->nr_entries++] = pc;
  219. if (trace->nr_entries >= trace->max_entries)
  220. break;
  221. #endif
  222. } else {
  223. /* Have we reached userland? */
  224. if (unlikely(pc == task_pt_regs(task)->pc)) {
  225. pr_info("[<%p>] PID %lu [%s]\n",
  226. (void *) pc,
  227. (unsigned long) task->pid,
  228. task->comm);
  229. break;
  230. } else
  231. print_ip_sym(pc);
  232. }
  233. /* Stop when we reach anything not part of the kernel */
  234. if (!kernel_text_address(pc))
  235. break;
  236. if (lookup_prev_stack_frame(fp, pc, leaf_return, &next_fp,
  237. &next_pc) == 0) {
  238. ofs = sizeof(unsigned long);
  239. pc = next_pc & ~3;
  240. fp = next_fp;
  241. leaf_return = 0;
  242. } else {
  243. pr_debug(" Failed to find previous stack frame\n");
  244. break;
  245. }
  246. pr_debug(" Next PC=%p, next FP=%p\n",
  247. (void *)next_pc, (void *)next_fp);
  248. }
  249. }
  250. /**
  251. * microblaze_unwind - Stack unwinder for Microblaze (external entry point)
  252. * @task : Task whose stack we are to unwind (NULL == current)
  253. * @trace : Where to store stack backtrace (PC values).
  254. * NULL == print backtrace to kernel log
  255. */
  256. void microblaze_unwind(struct task_struct *task, struct stack_trace *trace)
  257. {
  258. if (task) {
  259. if (task == current) {
  260. const struct pt_regs *regs = task_pt_regs(task);
  261. microblaze_unwind_inner(task, regs->pc, regs->r1,
  262. regs->r15, trace);
  263. } else {
  264. struct thread_info *thread_info =
  265. (struct thread_info *)(task->stack);
  266. const struct cpu_context *cpu_context =
  267. &thread_info->cpu_context;
  268. microblaze_unwind_inner(task,
  269. (unsigned long) &_switch_to,
  270. cpu_context->r1,
  271. cpu_context->r15, trace);
  272. }
  273. } else {
  274. unsigned long pc, fp;
  275. __asm__ __volatile__ ("or %0, r1, r0" : "=r" (fp));
  276. __asm__ __volatile__ (
  277. "brlid %0, 0f;"
  278. "nop;"
  279. "0:"
  280. : "=r" (pc)
  281. );
  282. /* Since we are not a leaf function, use leaf_return = 0 */
  283. microblaze_unwind_inner(current, pc, fp, 0, trace);
  284. }
  285. }