unwind.c 8.8 KB

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