unwind.c 8.3 KB

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