unwind.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * arch/sh/kernel/cpu/sh5/unwind.c
  3. *
  4. * Copyright (C) 2004 Paul Mundt
  5. * Copyright (C) 2004 Richard Curnow
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/kallsyms.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <asm/page.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/processor.h>
  18. #include <asm/io.h>
  19. #include <asm/unwinder.h>
  20. #include <asm/stacktrace.h>
  21. static u8 regcache[63];
  22. /*
  23. * Finding the previous stack frame isn't horribly straightforward as it is
  24. * on some other platforms. In the sh64 case, we don't have "linked" stack
  25. * frames, so we need to do a bit of work to determine the previous frame,
  26. * and in turn, the previous r14/r18 pair.
  27. *
  28. * There are generally a few cases which determine where we can find out
  29. * the r14/r18 values. In the general case, this can be determined by poking
  30. * around the prologue of the symbol PC is in (note that we absolutely must
  31. * have frame pointer support as well as the kernel symbol table mapped,
  32. * otherwise we can't even get this far).
  33. *
  34. * In other cases, such as the interrupt/exception path, we can poke around
  35. * the sp/fp.
  36. *
  37. * Notably, this entire approach is somewhat error prone, and in the event
  38. * that the previous frame cannot be determined, that's all we can do.
  39. * Either way, this still leaves us with a more correct backtrace then what
  40. * we would be able to come up with by walking the stack (which is garbage
  41. * for anything beyond the first frame).
  42. * -- PFM.
  43. */
  44. static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
  45. unsigned long *pprev_fp, unsigned long *pprev_pc,
  46. struct pt_regs *regs)
  47. {
  48. const char *sym;
  49. char namebuf[128];
  50. unsigned long offset;
  51. unsigned long prologue = 0;
  52. unsigned long fp_displacement = 0;
  53. unsigned long fp_prev = 0;
  54. unsigned long offset_r14 = 0, offset_r18 = 0;
  55. int i, found_prologue_end = 0;
  56. sym = kallsyms_lookup(pc, NULL, &offset, NULL, namebuf);
  57. if (!sym)
  58. return -EINVAL;
  59. prologue = pc - offset;
  60. if (!prologue)
  61. return -EINVAL;
  62. /* Validate fp, to avoid risk of dereferencing a bad pointer later.
  63. Assume 128Mb since that's the amount of RAM on a Cayman. Modify
  64. when there is an SH-5 board with more. */
  65. if ((fp < (unsigned long) phys_to_virt(__MEMORY_START)) ||
  66. (fp >= (unsigned long)(phys_to_virt(__MEMORY_START)) + 128*1024*1024) ||
  67. ((fp & 7) != 0)) {
  68. return -EINVAL;
  69. }
  70. /*
  71. * Depth to walk, depth is completely arbitrary.
  72. */
  73. for (i = 0; i < 100; i++, prologue += sizeof(unsigned long)) {
  74. unsigned long op;
  75. u8 major, minor;
  76. u8 src, dest, disp;
  77. op = *(unsigned long *)prologue;
  78. major = (op >> 26) & 0x3f;
  79. src = (op >> 20) & 0x3f;
  80. minor = (op >> 16) & 0xf;
  81. disp = (op >> 10) & 0x3f;
  82. dest = (op >> 4) & 0x3f;
  83. /*
  84. * Stack frame creation happens in a number of ways.. in the
  85. * general case when the stack frame is less than 511 bytes,
  86. * it's generally created by an addi or addi.l:
  87. *
  88. * addi/addi.l r15, -FRAME_SIZE, r15
  89. *
  90. * in the event that the frame size is bigger than this, it's
  91. * typically created using a movi/sub pair as follows:
  92. *
  93. * movi FRAME_SIZE, rX
  94. * sub r15, rX, r15
  95. */
  96. switch (major) {
  97. case (0x00 >> 2):
  98. switch (minor) {
  99. case 0x8: /* add.l */
  100. case 0x9: /* add */
  101. /* Look for r15, r63, r14 */
  102. if (src == 15 && disp == 63 && dest == 14)
  103. found_prologue_end = 1;
  104. break;
  105. case 0xa: /* sub.l */
  106. case 0xb: /* sub */
  107. if (src != 15 || dest != 15)
  108. continue;
  109. fp_displacement -= regcache[disp];
  110. fp_prev = fp - fp_displacement;
  111. break;
  112. }
  113. break;
  114. case (0xa8 >> 2): /* st.l */
  115. if (src != 15)
  116. continue;
  117. switch (dest) {
  118. case 14:
  119. if (offset_r14 || fp_displacement == 0)
  120. continue;
  121. offset_r14 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
  122. offset_r14 *= sizeof(unsigned long);
  123. offset_r14 += fp_displacement;
  124. break;
  125. case 18:
  126. if (offset_r18 || fp_displacement == 0)
  127. continue;
  128. offset_r18 = (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
  129. offset_r18 *= sizeof(unsigned long);
  130. offset_r18 += fp_displacement;
  131. break;
  132. }
  133. break;
  134. case (0xcc >> 2): /* movi */
  135. if (dest >= 63) {
  136. printk(KERN_NOTICE "%s: Invalid dest reg %d "
  137. "specified in movi handler. Failed "
  138. "opcode was 0x%lx: ", __func__,
  139. dest, op);
  140. continue;
  141. }
  142. /* Sign extend */
  143. regcache[dest] =
  144. sign_extend64((((u64)op >> 10) & 0xffff), 9);
  145. break;
  146. case (0xd0 >> 2): /* addi */
  147. case (0xd4 >> 2): /* addi.l */
  148. /* Look for r15, -FRAME_SIZE, r15 */
  149. if (src != 15 || dest != 15)
  150. continue;
  151. /* Sign extended frame size.. */
  152. fp_displacement +=
  153. (u64)(((((s64)op >> 10) & 0x3ff) << 54) >> 54);
  154. fp_prev = fp - fp_displacement;
  155. break;
  156. }
  157. if (found_prologue_end && offset_r14 && (offset_r18 || *pprev_pc) && fp_prev)
  158. break;
  159. }
  160. if (offset_r14 == 0 || fp_prev == 0) {
  161. if (!offset_r14)
  162. pr_debug("Unable to find r14 offset\n");
  163. if (!fp_prev)
  164. pr_debug("Unable to find previous fp\n");
  165. return -EINVAL;
  166. }
  167. /* For innermost leaf function, there might not be a offset_r18 */
  168. if (!*pprev_pc && (offset_r18 == 0))
  169. return -EINVAL;
  170. *pprev_fp = *(unsigned long *)(fp_prev + offset_r14);
  171. if (offset_r18)
  172. *pprev_pc = *(unsigned long *)(fp_prev + offset_r18);
  173. *pprev_pc &= ~1;
  174. return 0;
  175. }
  176. /*
  177. * Don't put this on the stack since we'll want to call in to
  178. * sh64_unwinder_dump() when we're close to underflowing the stack
  179. * anyway.
  180. */
  181. static struct pt_regs here_regs;
  182. extern const char syscall_ret;
  183. extern const char ret_from_syscall;
  184. extern const char ret_from_exception;
  185. extern const char ret_from_irq;
  186. static void sh64_unwind_inner(const struct stacktrace_ops *ops,
  187. void *data, struct pt_regs *regs);
  188. static inline void unwind_nested(const struct stacktrace_ops *ops, void *data,
  189. unsigned long pc, unsigned long fp)
  190. {
  191. if ((fp >= __MEMORY_START) &&
  192. ((fp & 7) == 0))
  193. sh64_unwind_inner(ops, data, (struct pt_regs *)fp);
  194. }
  195. static void sh64_unwind_inner(const struct stacktrace_ops *ops,
  196. void *data, struct pt_regs *regs)
  197. {
  198. unsigned long pc, fp;
  199. int ofs = 0;
  200. int first_pass;
  201. pc = regs->pc & ~1;
  202. fp = regs->regs[14];
  203. first_pass = 1;
  204. for (;;) {
  205. int cond;
  206. unsigned long next_fp, next_pc;
  207. if (pc == ((unsigned long)&syscall_ret & ~1)) {
  208. printk("SYSCALL\n");
  209. unwind_nested(ops, data, pc, fp);
  210. return;
  211. }
  212. if (pc == ((unsigned long)&ret_from_syscall & ~1)) {
  213. printk("SYSCALL (PREEMPTED)\n");
  214. unwind_nested(ops, data, pc, fp);
  215. return;
  216. }
  217. /* In this case, the PC is discovered by lookup_prev_stack_frame but
  218. it has 4 taken off it to look like the 'caller' */
  219. if (pc == ((unsigned long)&ret_from_exception & ~1)) {
  220. printk("EXCEPTION\n");
  221. unwind_nested(ops, data, pc, fp);
  222. return;
  223. }
  224. if (pc == ((unsigned long)&ret_from_irq & ~1)) {
  225. printk("IRQ\n");
  226. unwind_nested(ops, data, pc, fp);
  227. return;
  228. }
  229. cond = ((pc >= __MEMORY_START) && (fp >= __MEMORY_START) &&
  230. ((pc & 3) == 0) && ((fp & 7) == 0));
  231. pc -= ofs;
  232. ops->address(data, pc, 1);
  233. if (first_pass) {
  234. /* If the innermost frame is a leaf function, it's
  235. * possible that r18 is never saved out to the stack.
  236. */
  237. next_pc = regs->regs[18];
  238. } else {
  239. next_pc = 0;
  240. }
  241. if (lookup_prev_stack_frame(fp, pc, &next_fp, &next_pc, regs) == 0) {
  242. ofs = sizeof(unsigned long);
  243. pc = next_pc & ~1;
  244. fp = next_fp;
  245. } else {
  246. printk("Unable to lookup previous stack frame\n");
  247. break;
  248. }
  249. first_pass = 0;
  250. }
  251. printk("\n");
  252. }
  253. static void sh64_unwinder_dump(struct task_struct *task,
  254. struct pt_regs *regs,
  255. unsigned long *sp,
  256. const struct stacktrace_ops *ops,
  257. void *data)
  258. {
  259. if (!regs) {
  260. /*
  261. * Fetch current regs if we have no other saved state to back
  262. * trace from.
  263. */
  264. regs = &here_regs;
  265. __asm__ __volatile__ ("ori r14, 0, %0" : "=r" (regs->regs[14]));
  266. __asm__ __volatile__ ("ori r15, 0, %0" : "=r" (regs->regs[15]));
  267. __asm__ __volatile__ ("ori r18, 0, %0" : "=r" (regs->regs[18]));
  268. __asm__ __volatile__ ("gettr tr0, %0" : "=r" (regs->tregs[0]));
  269. __asm__ __volatile__ ("gettr tr1, %0" : "=r" (regs->tregs[1]));
  270. __asm__ __volatile__ ("gettr tr2, %0" : "=r" (regs->tregs[2]));
  271. __asm__ __volatile__ ("gettr tr3, %0" : "=r" (regs->tregs[3]));
  272. __asm__ __volatile__ ("gettr tr4, %0" : "=r" (regs->tregs[4]));
  273. __asm__ __volatile__ ("gettr tr5, %0" : "=r" (regs->tregs[5]));
  274. __asm__ __volatile__ ("gettr tr6, %0" : "=r" (regs->tregs[6]));
  275. __asm__ __volatile__ ("gettr tr7, %0" : "=r" (regs->tregs[7]));
  276. __asm__ __volatile__ (
  277. "pta 0f, tr0\n\t"
  278. "blink tr0, %0\n\t"
  279. "0: nop"
  280. : "=r" (regs->pc)
  281. );
  282. }
  283. sh64_unwind_inner(ops, data, regs);
  284. }
  285. static struct unwinder sh64_unwinder = {
  286. .name = "sh64-unwinder",
  287. .dump = sh64_unwinder_dump,
  288. .rating = 150,
  289. };
  290. static int __init sh64_unwinder_init(void)
  291. {
  292. return unwinder_register(&sh64_unwinder);
  293. }
  294. early_initcall(sh64_unwinder_init);