kgdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * PowerPC backend to the KGDB stub.
  3. *
  4. * 1998 (c) Michael AK Tesch (tesch@cs.wisc.edu)
  5. * Copyright (C) 2003 Timesys Corporation.
  6. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  7. * PPC64 Mods (C) 2005 Frank Rowand (frowand@mvista.com)
  8. * PPC32 support restored by Vitaly Wool <vwool@ru.mvista.com> and
  9. * Sergei Shtylyov <sshtylyov@ru.mvista.com>
  10. * Copyright (C) 2007-2008 Wind River Systems, Inc.
  11. *
  12. * This file is licensed under the terms of the GNU General Public License
  13. * version 2. This program as licensed "as is" without any warranty of any
  14. * kind, whether express or implied.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/kgdb.h>
  19. #include <linux/smp.h>
  20. #include <linux/signal.h>
  21. #include <linux/ptrace.h>
  22. #include <asm/current.h>
  23. #include <asm/processor.h>
  24. #include <asm/machdep.h>
  25. /*
  26. * This table contains the mapping between PowerPC hardware trap types, and
  27. * signals, which are primarily what GDB understands. GDB and the kernel
  28. * don't always agree on values, so we use constants taken from gdb-6.2.
  29. */
  30. static struct hard_trap_info
  31. {
  32. unsigned int tt; /* Trap type code for powerpc */
  33. unsigned char signo; /* Signal that we map this trap into */
  34. } hard_trap_info[] = {
  35. { 0x0100, 0x02 /* SIGINT */ }, /* system reset */
  36. { 0x0200, 0x0b /* SIGSEGV */ }, /* machine check */
  37. { 0x0300, 0x0b /* SIGSEGV */ }, /* data access */
  38. { 0x0400, 0x0b /* SIGSEGV */ }, /* instruction access */
  39. { 0x0500, 0x02 /* SIGINT */ }, /* external interrupt */
  40. { 0x0600, 0x0a /* SIGBUS */ }, /* alignment */
  41. { 0x0700, 0x05 /* SIGTRAP */ }, /* program check */
  42. { 0x0800, 0x08 /* SIGFPE */ }, /* fp unavailable */
  43. { 0x0900, 0x0e /* SIGALRM */ }, /* decrementer */
  44. { 0x0c00, 0x14 /* SIGCHLD */ }, /* system call */
  45. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  46. { 0x2002, 0x05 /* SIGTRAP */ }, /* debug */
  47. #if defined(CONFIG_FSL_BOOKE)
  48. { 0x2010, 0x08 /* SIGFPE */ }, /* spe unavailable */
  49. { 0x2020, 0x08 /* SIGFPE */ }, /* spe unavailable */
  50. { 0x2030, 0x08 /* SIGFPE */ }, /* spe fp data */
  51. { 0x2040, 0x08 /* SIGFPE */ }, /* spe fp data */
  52. { 0x2050, 0x08 /* SIGFPE */ }, /* spe fp round */
  53. { 0x2060, 0x0e /* SIGILL */ }, /* performace monitor */
  54. { 0x2900, 0x08 /* SIGFPE */ }, /* apu unavailable */
  55. { 0x3100, 0x0e /* SIGALRM */ }, /* fixed interval timer */
  56. { 0x3200, 0x02 /* SIGINT */ }, /* watchdog */
  57. #else /* ! CONFIG_FSL_BOOKE */
  58. { 0x1000, 0x0e /* SIGALRM */ }, /* prog interval timer */
  59. { 0x1010, 0x0e /* SIGALRM */ }, /* fixed interval timer */
  60. { 0x1020, 0x02 /* SIGINT */ }, /* watchdog */
  61. { 0x2010, 0x08 /* SIGFPE */ }, /* fp unavailable */
  62. { 0x2020, 0x08 /* SIGFPE */ }, /* ap unavailable */
  63. #endif
  64. #else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */
  65. { 0x0d00, 0x05 /* SIGTRAP */ }, /* single-step */
  66. #if defined(CONFIG_8xx)
  67. { 0x1000, 0x04 /* SIGILL */ }, /* software emulation */
  68. #else /* ! CONFIG_8xx */
  69. { 0x0f00, 0x04 /* SIGILL */ }, /* performance monitor */
  70. { 0x0f20, 0x08 /* SIGFPE */ }, /* altivec unavailable */
  71. { 0x1300, 0x05 /* SIGTRAP */ }, /* instruction address break */
  72. #if defined(CONFIG_PPC64)
  73. { 0x1200, 0x05 /* SIGILL */ }, /* system error */
  74. { 0x1500, 0x04 /* SIGILL */ }, /* soft patch */
  75. { 0x1600, 0x04 /* SIGILL */ }, /* maintenance */
  76. { 0x1700, 0x08 /* SIGFPE */ }, /* altivec assist */
  77. { 0x1800, 0x04 /* SIGILL */ }, /* thermal */
  78. #else /* ! CONFIG_PPC64 */
  79. { 0x1400, 0x02 /* SIGINT */ }, /* SMI */
  80. { 0x1600, 0x08 /* SIGFPE */ }, /* altivec assist */
  81. { 0x1700, 0x04 /* SIGILL */ }, /* TAU */
  82. { 0x2000, 0x05 /* SIGTRAP */ }, /* run mode */
  83. #endif
  84. #endif
  85. #endif
  86. { 0x0000, 0x00 } /* Must be last */
  87. };
  88. static int computeSignal(unsigned int tt)
  89. {
  90. struct hard_trap_info *ht;
  91. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  92. if (ht->tt == tt)
  93. return ht->signo;
  94. return SIGHUP; /* default for things we don't know about */
  95. }
  96. static int kgdb_call_nmi_hook(struct pt_regs *regs)
  97. {
  98. kgdb_nmicallback(raw_smp_processor_id(), regs);
  99. return 0;
  100. }
  101. #ifdef CONFIG_SMP
  102. void kgdb_roundup_cpus(unsigned long flags)
  103. {
  104. smp_send_debugger_break(MSG_ALL_BUT_SELF);
  105. }
  106. #endif
  107. /* KGDB functions to use existing PowerPC64 hooks. */
  108. static int kgdb_debugger(struct pt_regs *regs)
  109. {
  110. return kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs);
  111. }
  112. static int kgdb_handle_breakpoint(struct pt_regs *regs)
  113. {
  114. if (user_mode(regs))
  115. return 0;
  116. if (kgdb_handle_exception(0, SIGTRAP, 0, regs) != 0)
  117. return 0;
  118. if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr))
  119. regs->nip += 4;
  120. return 1;
  121. }
  122. static int kgdb_singlestep(struct pt_regs *regs)
  123. {
  124. struct thread_info *thread_info, *exception_thread_info;
  125. if (user_mode(regs))
  126. return 0;
  127. /*
  128. * On Book E and perhaps other processsors, singlestep is handled on
  129. * the critical exception stack. This causes current_thread_info()
  130. * to fail, since it it locates the thread_info by masking off
  131. * the low bits of the current stack pointer. We work around
  132. * this issue by copying the thread_info from the kernel stack
  133. * before calling kgdb_handle_exception, and copying it back
  134. * afterwards. On most processors the copy is avoided since
  135. * exception_thread_info == thread_info.
  136. */
  137. thread_info = (struct thread_info *)(regs->gpr[1] & ~(THREAD_SIZE-1));
  138. exception_thread_info = current_thread_info();
  139. if (thread_info != exception_thread_info)
  140. memcpy(exception_thread_info, thread_info, sizeof *thread_info);
  141. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  142. if (thread_info != exception_thread_info)
  143. memcpy(thread_info, exception_thread_info, sizeof *thread_info);
  144. return 1;
  145. }
  146. static int kgdb_iabr_match(struct pt_regs *regs)
  147. {
  148. if (user_mode(regs))
  149. return 0;
  150. if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
  151. return 0;
  152. return 1;
  153. }
  154. static int kgdb_dabr_match(struct pt_regs *regs)
  155. {
  156. if (user_mode(regs))
  157. return 0;
  158. if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
  159. return 0;
  160. return 1;
  161. }
  162. #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
  163. #define PACK32(ptr, src) do { \
  164. u32 *ptr32; \
  165. ptr32 = (u32 *)ptr; \
  166. *(ptr32++) = (src); \
  167. ptr = (unsigned long *)ptr32; \
  168. } while (0)
  169. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  170. {
  171. unsigned long *ptr = gdb_regs;
  172. int reg;
  173. memset(gdb_regs, 0, NUMREGBYTES);
  174. for (reg = 0; reg < 32; reg++)
  175. PACK64(ptr, regs->gpr[reg]);
  176. #ifdef CONFIG_FSL_BOOKE
  177. #ifdef CONFIG_SPE
  178. for (reg = 0; reg < 32; reg++)
  179. PACK64(ptr, current->thread.evr[reg]);
  180. #else
  181. ptr += 32;
  182. #endif
  183. #else
  184. /* fp registers not used by kernel, leave zero */
  185. ptr += 32 * 8 / sizeof(long);
  186. #endif
  187. PACK64(ptr, regs->nip);
  188. PACK64(ptr, regs->msr);
  189. PACK32(ptr, regs->ccr);
  190. PACK64(ptr, regs->link);
  191. PACK64(ptr, regs->ctr);
  192. PACK32(ptr, regs->xer);
  193. BUG_ON((unsigned long)ptr >
  194. (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
  195. }
  196. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  197. {
  198. struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
  199. STACK_FRAME_OVERHEAD);
  200. unsigned long *ptr = gdb_regs;
  201. int reg;
  202. memset(gdb_regs, 0, NUMREGBYTES);
  203. /* Regs GPR0-2 */
  204. for (reg = 0; reg < 3; reg++)
  205. PACK64(ptr, regs->gpr[reg]);
  206. /* Regs GPR3-13 are caller saved, not in regs->gpr[] */
  207. ptr += 11;
  208. /* Regs GPR14-31 */
  209. for (reg = 14; reg < 32; reg++)
  210. PACK64(ptr, regs->gpr[reg]);
  211. #ifdef CONFIG_FSL_BOOKE
  212. #ifdef CONFIG_SPE
  213. for (reg = 0; reg < 32; reg++)
  214. PACK64(ptr, p->thread.evr[reg]);
  215. #else
  216. ptr += 32;
  217. #endif
  218. #else
  219. /* fp registers not used by kernel, leave zero */
  220. ptr += 32 * 8 / sizeof(long);
  221. #endif
  222. PACK64(ptr, regs->nip);
  223. PACK64(ptr, regs->msr);
  224. PACK32(ptr, regs->ccr);
  225. PACK64(ptr, regs->link);
  226. PACK64(ptr, regs->ctr);
  227. PACK32(ptr, regs->xer);
  228. BUG_ON((unsigned long)ptr >
  229. (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
  230. }
  231. #define UNPACK64(dest, ptr) do { dest = *(ptr++); } while (0)
  232. #define UNPACK32(dest, ptr) do { \
  233. u32 *ptr32; \
  234. ptr32 = (u32 *)ptr; \
  235. dest = *(ptr32++); \
  236. ptr = (unsigned long *)ptr32; \
  237. } while (0)
  238. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  239. {
  240. unsigned long *ptr = gdb_regs;
  241. int reg;
  242. #ifdef CONFIG_SPE
  243. union {
  244. u32 v32[2];
  245. u64 v64;
  246. } acc;
  247. #endif
  248. for (reg = 0; reg < 32; reg++)
  249. UNPACK64(regs->gpr[reg], ptr);
  250. #ifdef CONFIG_FSL_BOOKE
  251. #ifdef CONFIG_SPE
  252. for (reg = 0; reg < 32; reg++)
  253. UNPACK64(current->thread.evr[reg], ptr);
  254. #else
  255. ptr += 32;
  256. #endif
  257. #else
  258. /* fp registers not used by kernel, leave zero */
  259. ptr += 32 * 8 / sizeof(int);
  260. #endif
  261. UNPACK64(regs->nip, ptr);
  262. UNPACK64(regs->msr, ptr);
  263. UNPACK32(regs->ccr, ptr);
  264. UNPACK64(regs->link, ptr);
  265. UNPACK64(regs->ctr, ptr);
  266. UNPACK32(regs->xer, ptr);
  267. BUG_ON((unsigned long)ptr >
  268. (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
  269. }
  270. /*
  271. * This function does PowerPC specific procesing for interfacing to gdb.
  272. */
  273. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  274. char *remcom_in_buffer, char *remcom_out_buffer,
  275. struct pt_regs *linux_regs)
  276. {
  277. char *ptr = &remcom_in_buffer[1];
  278. unsigned long addr;
  279. switch (remcom_in_buffer[0]) {
  280. /*
  281. * sAA..AA Step one instruction from AA..AA
  282. * This will return an error to gdb ..
  283. */
  284. case 's':
  285. case 'c':
  286. /* handle the optional parameter */
  287. if (kgdb_hex2long(&ptr, &addr))
  288. linux_regs->nip = addr;
  289. atomic_set(&kgdb_cpu_doing_single_step, -1);
  290. /* set the trace bit if we're stepping */
  291. if (remcom_in_buffer[0] == 's') {
  292. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  293. mtspr(SPRN_DBCR0,
  294. mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
  295. linux_regs->msr |= MSR_DE;
  296. #else
  297. linux_regs->msr |= MSR_SE;
  298. #endif
  299. kgdb_single_step = 1;
  300. atomic_set(&kgdb_cpu_doing_single_step,
  301. raw_smp_processor_id());
  302. }
  303. return 0;
  304. }
  305. return -1;
  306. }
  307. /*
  308. * Global data
  309. */
  310. struct kgdb_arch arch_kgdb_ops = {
  311. .gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08},
  312. };
  313. static int kgdb_not_implemented(struct pt_regs *regs)
  314. {
  315. return 0;
  316. }
  317. static void *old__debugger_ipi;
  318. static void *old__debugger;
  319. static void *old__debugger_bpt;
  320. static void *old__debugger_sstep;
  321. static void *old__debugger_iabr_match;
  322. static void *old__debugger_dabr_match;
  323. static void *old__debugger_fault_handler;
  324. int kgdb_arch_init(void)
  325. {
  326. old__debugger_ipi = __debugger_ipi;
  327. old__debugger = __debugger;
  328. old__debugger_bpt = __debugger_bpt;
  329. old__debugger_sstep = __debugger_sstep;
  330. old__debugger_iabr_match = __debugger_iabr_match;
  331. old__debugger_dabr_match = __debugger_dabr_match;
  332. old__debugger_fault_handler = __debugger_fault_handler;
  333. __debugger_ipi = kgdb_call_nmi_hook;
  334. __debugger = kgdb_debugger;
  335. __debugger_bpt = kgdb_handle_breakpoint;
  336. __debugger_sstep = kgdb_singlestep;
  337. __debugger_iabr_match = kgdb_iabr_match;
  338. __debugger_dabr_match = kgdb_dabr_match;
  339. __debugger_fault_handler = kgdb_not_implemented;
  340. return 0;
  341. }
  342. void kgdb_arch_exit(void)
  343. {
  344. __debugger_ipi = old__debugger_ipi;
  345. __debugger = old__debugger;
  346. __debugger_bpt = old__debugger_bpt;
  347. __debugger_sstep = old__debugger_sstep;
  348. __debugger_iabr_match = old__debugger_iabr_match;
  349. __debugger_dabr_match = old__debugger_dabr_match;
  350. __debugger_fault_handler = old__debugger_fault_handler;
  351. }