unwind.c 8.6 KB

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