kgdb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include <common.h>
  2. #include <command.h>
  3. #include <kgdb.h>
  4. #include <asm/signal.h>
  5. #include <asm/processor.h>
  6. #define PC_REGNUM 64
  7. #define SP_REGNUM 1
  8. void breakinst(void);
  9. int
  10. kgdb_setjmp(long *buf)
  11. {
  12. asm ("mflr 0; stw 0,0(%0);"
  13. "stw 1,4(%0); stw 2,8(%0);"
  14. "mfcr 0; stw 0,12(%0);"
  15. "stmw 13,16(%0)"
  16. : : "r" (buf));
  17. /* XXX should save fp regs as well */
  18. return 0;
  19. }
  20. void
  21. kgdb_longjmp(long *buf, int val)
  22. {
  23. if (val == 0)
  24. val = 1;
  25. asm ("lmw 13,16(%0);"
  26. "lwz 0,12(%0); mtcrf 0x38,0;"
  27. "lwz 0,0(%0); lwz 1,4(%0); lwz 2,8(%0);"
  28. "mtlr 0; mr 3,%1"
  29. : : "r" (buf), "r" (val));
  30. }
  31. static inline unsigned long
  32. get_msr(void)
  33. {
  34. unsigned long msr;
  35. asm volatile("mfmsr %0" : "=r" (msr):);
  36. return msr;
  37. }
  38. static inline void
  39. set_msr(unsigned long msr)
  40. {
  41. asm volatile("mtmsr %0" : : "r" (msr));
  42. }
  43. /* Convert the SPARC hardware trap type code to a unix signal number. */
  44. /*
  45. * This table contains the mapping between PowerPC hardware trap types, and
  46. * signals, which are primarily what GDB understands.
  47. */
  48. static struct hard_trap_info
  49. {
  50. unsigned int tt; /* Trap type code for powerpc */
  51. unsigned char signo; /* Signal that we map this trap into */
  52. } hard_trap_info[] = {
  53. { 0x200, SIGSEGV }, /* machine check */
  54. { 0x300, SIGSEGV }, /* address error (store) */
  55. { 0x400, SIGBUS }, /* instruction bus error */
  56. { 0x500, SIGINT }, /* interrupt */
  57. { 0x600, SIGBUS }, /* alingment */
  58. { 0x700, SIGTRAP }, /* breakpoint trap */
  59. { 0x800, SIGFPE }, /* fpu unavail */
  60. { 0x900, SIGALRM }, /* decrementer */
  61. { 0xa00, SIGILL }, /* reserved */
  62. { 0xb00, SIGILL }, /* reserved */
  63. { 0xc00, SIGCHLD }, /* syscall */
  64. { 0xd00, SIGTRAP }, /* single-step/watch */
  65. { 0xe00, SIGFPE }, /* fp assist */
  66. { 0, 0} /* Must be last */
  67. };
  68. static int
  69. computeSignal(unsigned int tt)
  70. {
  71. struct hard_trap_info *ht;
  72. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  73. if (ht->tt == tt)
  74. return ht->signo;
  75. return SIGHUP; /* default for things we don't know about */
  76. }
  77. void
  78. kgdb_enter(struct pt_regs *regs, kgdb_data *kdp)
  79. {
  80. unsigned long msr;
  81. kdp->private[0] = msr = get_msr();
  82. set_msr(msr & ~MSR_EE); /* disable interrupts */
  83. if (regs->nip == (unsigned long)breakinst) {
  84. /* Skip over breakpoint trap insn */
  85. regs->nip += 4;
  86. }
  87. regs->msr &= ~MSR_SE;
  88. /* reply to host that an exception has occurred */
  89. kdp->sigval = computeSignal(regs->trap);
  90. kdp->nregs = 2;
  91. kdp->regs[0].num = PC_REGNUM;
  92. kdp->regs[0].val = regs->nip;
  93. kdp->regs[1].num = SP_REGNUM;
  94. kdp->regs[1].val = regs->gpr[SP_REGNUM];
  95. }
  96. void
  97. kgdb_exit(struct pt_regs *regs, kgdb_data *kdp)
  98. {
  99. unsigned long msr = kdp->private[0];
  100. if (kdp->extype & KGDBEXIT_WITHADDR)
  101. regs->nip = kdp->exaddr;
  102. switch (kdp->extype & KGDBEXIT_TYPEMASK) {
  103. case KGDBEXIT_KILL:
  104. case KGDBEXIT_CONTINUE:
  105. set_msr(msr);
  106. break;
  107. case KGDBEXIT_SINGLE:
  108. regs->msr |= MSR_SE;
  109. #if 0
  110. set_msr(msr | MSR_SE);
  111. #endif
  112. break;
  113. }
  114. }
  115. int
  116. kgdb_trap(struct pt_regs *regs)
  117. {
  118. return (regs->trap);
  119. }
  120. /* return the value of the CPU registers.
  121. * some of them are non-PowerPC names :(
  122. * they are stored in gdb like:
  123. * struct {
  124. * u32 gpr[32];
  125. * f64 fpr[32];
  126. * u32 pc, ps, cnd, lr; (ps=msr)
  127. * u32 cnt, xer, mq;
  128. * }
  129. */
  130. #define SPACE_REQUIRED ((32*4)+(32*8)+(6*4))
  131. #ifdef CONFIG_8260
  132. /* store floating double indexed */
  133. #define STFDI(n,p) __asm__ __volatile__ ("stfd " #n ",%0" : "=o"(p[2*n]))
  134. /* store floating double multiple */
  135. #define STFDM(p) { STFDI( 0,p); STFDI( 1,p); STFDI( 2,p); STFDI( 3,p); \
  136. STFDI( 4,p); STFDI( 5,p); STFDI( 6,p); STFDI( 7,p); \
  137. STFDI( 8,p); STFDI( 9,p); STFDI(10,p); STFDI(11,p); \
  138. STFDI(12,p); STFDI(13,p); STFDI(14,p); STFDI(15,p); \
  139. STFDI(16,p); STFDI(17,p); STFDI(18,p); STFDI(19,p); \
  140. STFDI(20,p); STFDI(21,p); STFDI(22,p); STFDI(23,p); \
  141. STFDI(24,p); STFDI(25,p); STFDI(26,p); STFDI(27,p); \
  142. STFDI(28,p); STFDI(29,p); STFDI(30,p); STFDI(31,p); }
  143. #endif
  144. int
  145. kgdb_getregs(struct pt_regs *regs, char *buf, int max)
  146. {
  147. int i;
  148. unsigned long *ptr = (unsigned long *)buf;
  149. if (max < SPACE_REQUIRED)
  150. kgdb_error(KGDBERR_NOSPACE);
  151. if ((unsigned long)ptr & 3)
  152. kgdb_error(KGDBERR_ALIGNFAULT);
  153. /* General Purpose Regs */
  154. for (i = 0; i < 32; i++)
  155. *ptr++ = regs->gpr[i];
  156. /* Floating Point Regs */
  157. #ifdef CONFIG_8260
  158. STFDM(ptr);
  159. ptr += 32*2;
  160. #else
  161. for (i = 0; i < 32; i++) {
  162. *ptr++ = 0;
  163. *ptr++ = 0;
  164. }
  165. #endif
  166. /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
  167. *ptr++ = regs->nip;
  168. *ptr++ = regs->msr;
  169. *ptr++ = regs->ccr;
  170. *ptr++ = regs->link;
  171. *ptr++ = regs->ctr;
  172. *ptr++ = regs->xer;
  173. return (SPACE_REQUIRED);
  174. }
  175. /* set the value of the CPU registers */
  176. #ifdef CONFIG_8260
  177. /* load floating double */
  178. #define LFD(n,v) __asm__ __volatile__ ("lfd " #n ",%0" :: "o"(v))
  179. /* load floating double indexed */
  180. #define LFDI(n,p) __asm__ __volatile__ ("lfd " #n ",%0" :: "o"((p)[2*n]))
  181. /* load floating double multiple */
  182. #define LFDM(p) { LFDI( 0,p); LFDI( 1,p); LFDI( 2,p); LFDI( 3,p); \
  183. LFDI( 4,p); LFDI( 5,p); LFDI( 6,p); LFDI( 7,p); \
  184. LFDI( 8,p); LFDI( 9,p); LFDI(10,p); LFDI(11,p); \
  185. LFDI(12,p); LFDI(13,p); LFDI(14,p); LFDI(15,p); \
  186. LFDI(16,p); LFDI(17,p); LFDI(18,p); LFDI(19,p); \
  187. LFDI(20,p); LFDI(21,p); LFDI(22,p); LFDI(23,p); \
  188. LFDI(24,p); LFDI(25,p); LFDI(26,p); LFDI(27,p); \
  189. LFDI(28,p); LFDI(29,p); LFDI(30,p); LFDI(31,p); }
  190. #endif
  191. void
  192. kgdb_putreg(struct pt_regs *regs, int regno, char *buf, int length)
  193. {
  194. unsigned long *ptr = (unsigned long *)buf;
  195. if (regno < 0 || regno >= 70)
  196. kgdb_error(KGDBERR_BADPARAMS);
  197. else if (regno >= 32 && regno < 64) {
  198. if (length < 8)
  199. kgdb_error(KGDBERR_NOSPACE);
  200. }
  201. else {
  202. if (length < 4)
  203. kgdb_error(KGDBERR_NOSPACE);
  204. }
  205. if ((unsigned long)ptr & 3)
  206. kgdb_error(KGDBERR_ALIGNFAULT);
  207. if (regno >= 0 && regno < 32)
  208. regs->gpr[regno] = *ptr;
  209. else switch (regno) {
  210. #ifdef CONFIG_8260
  211. #define caseF(n) \
  212. case (n) + 32: LFD(n, *ptr); break;
  213. caseF( 0) caseF( 1) caseF( 2) caseF( 3) caseF( 4) caseF( 5) caseF( 6) caseF( 7)
  214. caseF( 8) caseF( 9) caseF(10) caseF(11) caseF(12) caseF(13) caseF(14) caseF(15)
  215. caseF(16) caseF(17) caseF(18) caseF(19) caseF(20) caseF(21) caseF(22) caseF(23)
  216. caseF(24) caseF(25) caseF(26) caseF(27) caseF(28) caseF(29) caseF(30) caseF(31)
  217. #undef caseF
  218. #endif
  219. case 64: regs->nip = *ptr; break;
  220. case 65: regs->msr = *ptr; break;
  221. case 66: regs->ccr = *ptr; break;
  222. case 67: regs->link = *ptr; break;
  223. case 68: regs->ctr = *ptr; break;
  224. case 69: regs->ctr = *ptr; break;
  225. default:
  226. kgdb_error(KGDBERR_BADPARAMS);
  227. }
  228. }
  229. void
  230. kgdb_putregs(struct pt_regs *regs, char *buf, int length)
  231. {
  232. int i;
  233. unsigned long *ptr = (unsigned long *)buf;
  234. if (length < SPACE_REQUIRED)
  235. kgdb_error(KGDBERR_NOSPACE);
  236. if ((unsigned long)ptr & 3)
  237. kgdb_error(KGDBERR_ALIGNFAULT);
  238. /*
  239. * If the stack pointer has moved, you should pray.
  240. * (cause only god can help you).
  241. */
  242. /* General Purpose Regs */
  243. for (i = 0; i < 32; i++)
  244. regs->gpr[i] = *ptr++;
  245. /* Floating Point Regs */
  246. #ifdef CONFIG_8260
  247. LFDM(ptr);
  248. #endif
  249. ptr += 32*2;
  250. /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
  251. regs->nip = *ptr++;
  252. regs->msr = *ptr++;
  253. regs->ccr = *ptr++;
  254. regs->link = *ptr++;
  255. regs->ctr = *ptr++;
  256. regs->xer = *ptr++;
  257. }
  258. /* This function will generate a breakpoint exception. It is used at the
  259. beginning of a program to sync up with a debugger and can be used
  260. otherwise as a quick means to stop program execution and "break" into
  261. the debugger. */
  262. void
  263. kgdb_breakpoint(int argc, char * const argv[])
  264. {
  265. asm(" .globl breakinst\n\
  266. breakinst: .long 0x7d821008\n\
  267. ");
  268. }