kgdb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PowerPC backend to the KGDB stub.
  4. *
  5. * 1998 (c) Michael AK Tesch ([email protected])
  6. * Copyright (C) 2003 Timesys Corporation.
  7. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  8. * PPC64 Mods (C) 2005 Frank Rowand ([email protected])
  9. * PPC32 support restored by Vitaly Wool <[email protected]> and
  10. * Sergei Shtylyov <[email protected]>
  11. * Copyright (C) 2007-2008 Wind River Systems, Inc.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/kgdb.h>
  15. #include <linux/smp.h>
  16. #include <linux/signal.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/kdebug.h>
  19. #include <asm/current.h>
  20. #include <asm/processor.h>
  21. #include <asm/machdep.h>
  22. #include <asm/debug.h>
  23. #include <asm/code-patching.h>
  24. #include <linux/slab.h>
  25. #include <asm/inst.h>
  26. /*
  27. * This table contains the mapping between PowerPC hardware trap types, and
  28. * signals, which are primarily what GDB understands. GDB and the kernel
  29. * don't always agree on values, so we use constants taken from gdb-6.2.
  30. */
  31. static struct hard_trap_info
  32. {
  33. unsigned int tt; /* Trap type code for powerpc */
  34. unsigned char signo; /* Signal that we map this trap into */
  35. } hard_trap_info[] = {
  36. { 0x0100, 0x02 /* SIGINT */ }, /* system reset */
  37. { 0x0200, 0x0b /* SIGSEGV */ }, /* machine check */
  38. { 0x0300, 0x0b /* SIGSEGV */ }, /* data access */
  39. { 0x0400, 0x0b /* SIGSEGV */ }, /* instruction access */
  40. { 0x0500, 0x02 /* SIGINT */ }, /* external interrupt */
  41. { 0x0600, 0x0a /* SIGBUS */ }, /* alignment */
  42. { 0x0700, 0x05 /* SIGTRAP */ }, /* program check */
  43. { 0x0800, 0x08 /* SIGFPE */ }, /* fp unavailable */
  44. { 0x0900, 0x0e /* SIGALRM */ }, /* decrementer */
  45. { 0x0c00, 0x14 /* SIGCHLD */ }, /* system call */
  46. #ifdef CONFIG_BOOKE_OR_40x
  47. { 0x2002, 0x05 /* SIGTRAP */ }, /* debug */
  48. #if defined(CONFIG_PPC_85xx)
  49. { 0x2010, 0x08 /* SIGFPE */ }, /* spe unavailable */
  50. { 0x2020, 0x08 /* SIGFPE */ }, /* spe unavailable */
  51. { 0x2030, 0x08 /* SIGFPE */ }, /* spe fp data */
  52. { 0x2040, 0x08 /* SIGFPE */ }, /* spe fp data */
  53. { 0x2050, 0x08 /* SIGFPE */ }, /* spe fp round */
  54. { 0x2060, 0x0e /* SIGILL */ }, /* performance monitor */
  55. { 0x2900, 0x08 /* SIGFPE */ }, /* apu unavailable */
  56. { 0x3100, 0x0e /* SIGALRM */ }, /* fixed interval timer */
  57. { 0x3200, 0x02 /* SIGINT */ }, /* watchdog */
  58. #else /* ! CONFIG_PPC_85xx */
  59. { 0x1000, 0x0e /* SIGALRM */ }, /* prog interval timer */
  60. { 0x1010, 0x0e /* SIGALRM */ }, /* fixed interval timer */
  61. { 0x1020, 0x02 /* SIGINT */ }, /* watchdog */
  62. { 0x2010, 0x08 /* SIGFPE */ }, /* fp unavailable */
  63. { 0x2020, 0x08 /* SIGFPE */ }, /* ap unavailable */
  64. #endif
  65. #else /* !CONFIG_BOOKE_OR_40x */
  66. { 0x0d00, 0x05 /* SIGTRAP */ }, /* single-step */
  67. #if defined(CONFIG_PPC_8xx)
  68. { 0x1000, 0x04 /* SIGILL */ }, /* software emulation */
  69. #else /* ! CONFIG_PPC_8xx */
  70. { 0x0f00, 0x04 /* SIGILL */ }, /* performance monitor */
  71. { 0x0f20, 0x08 /* SIGFPE */ }, /* altivec unavailable */
  72. { 0x1300, 0x05 /* SIGTRAP */ }, /* instruction address break */
  73. #if defined(CONFIG_PPC64)
  74. { 0x1200, 0x05 /* SIGILL */ }, /* system error */
  75. { 0x1500, 0x04 /* SIGILL */ }, /* soft patch */
  76. { 0x1600, 0x04 /* SIGILL */ }, /* maintenance */
  77. { 0x1700, 0x08 /* SIGFPE */ }, /* altivec assist */
  78. { 0x1800, 0x04 /* SIGILL */ }, /* thermal */
  79. #else /* ! CONFIG_PPC64 */
  80. { 0x1400, 0x02 /* SIGINT */ }, /* SMI */
  81. { 0x1600, 0x08 /* SIGFPE */ }, /* altivec assist */
  82. { 0x1700, 0x04 /* SIGILL */ }, /* TAU */
  83. { 0x2000, 0x05 /* SIGTRAP */ }, /* run mode */
  84. #endif
  85. #endif
  86. #endif
  87. { 0x0000, 0x00 } /* Must be last */
  88. };
  89. static int computeSignal(unsigned int tt)
  90. {
  91. struct hard_trap_info *ht;
  92. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  93. if (ht->tt == tt)
  94. return ht->signo;
  95. return SIGHUP; /* default for things we don't know about */
  96. }
  97. /**
  98. *
  99. * kgdb_skipexception - Bail out of KGDB when we've been triggered.
  100. * @exception: Exception vector number
  101. * @regs: Current &struct pt_regs.
  102. *
  103. * On some architectures we need to skip a breakpoint exception when
  104. * it occurs after a breakpoint has been removed.
  105. *
  106. */
  107. int kgdb_skipexception(int exception, struct pt_regs *regs)
  108. {
  109. return kgdb_isremovedbreak(regs->nip);
  110. }
  111. static int kgdb_debugger_ipi(struct pt_regs *regs)
  112. {
  113. kgdb_nmicallback(raw_smp_processor_id(), regs);
  114. return 0;
  115. }
  116. #ifdef CONFIG_SMP
  117. void kgdb_roundup_cpus(void)
  118. {
  119. smp_send_debugger_break();
  120. }
  121. #endif
  122. /* KGDB functions to use existing PowerPC64 hooks. */
  123. static int kgdb_debugger(struct pt_regs *regs)
  124. {
  125. return !kgdb_handle_exception(1, computeSignal(TRAP(regs)),
  126. DIE_OOPS, regs);
  127. }
  128. static int kgdb_handle_breakpoint(struct pt_regs *regs)
  129. {
  130. if (user_mode(regs))
  131. return 0;
  132. if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0)
  133. return 0;
  134. if (*(u32 *)regs->nip == BREAK_INSTR)
  135. regs_add_return_ip(regs, BREAK_INSTR_SIZE);
  136. return 1;
  137. }
  138. static int kgdb_singlestep(struct pt_regs *regs)
  139. {
  140. if (user_mode(regs))
  141. return 0;
  142. kgdb_handle_exception(0, SIGTRAP, 0, regs);
  143. return 1;
  144. }
  145. static int kgdb_iabr_match(struct pt_regs *regs)
  146. {
  147. if (user_mode(regs))
  148. return 0;
  149. if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
  150. return 0;
  151. return 1;
  152. }
  153. static int kgdb_break_match(struct pt_regs *regs)
  154. {
  155. if (user_mode(regs))
  156. return 0;
  157. if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
  158. return 0;
  159. return 1;
  160. }
  161. #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
  162. #define PACK32(ptr, src) do { \
  163. u32 *ptr32; \
  164. ptr32 = (u32 *)ptr; \
  165. *(ptr32++) = (src); \
  166. ptr = (unsigned long *)ptr32; \
  167. } while (0)
  168. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  169. {
  170. struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
  171. STACK_FRAME_OVERHEAD);
  172. unsigned long *ptr = gdb_regs;
  173. int reg;
  174. memset(gdb_regs, 0, NUMREGBYTES);
  175. /* Regs GPR0-2 */
  176. for (reg = 0; reg < 3; reg++)
  177. PACK64(ptr, regs->gpr[reg]);
  178. /* Regs GPR3-13 are caller saved, not in regs->gpr[] */
  179. ptr += 11;
  180. /* Regs GPR14-31 */
  181. for (reg = 14; reg < 32; reg++)
  182. PACK64(ptr, regs->gpr[reg]);
  183. #ifdef CONFIG_PPC_85xx
  184. #ifdef CONFIG_SPE
  185. for (reg = 0; reg < 32; reg++)
  186. PACK64(ptr, p->thread.evr[reg]);
  187. #else
  188. ptr += 32;
  189. #endif
  190. #else
  191. /* fp registers not used by kernel, leave zero */
  192. ptr += 32 * 8 / sizeof(long);
  193. #endif
  194. PACK64(ptr, regs->nip);
  195. PACK64(ptr, regs->msr);
  196. PACK32(ptr, regs->ccr);
  197. PACK64(ptr, regs->link);
  198. PACK64(ptr, regs->ctr);
  199. PACK32(ptr, regs->xer);
  200. BUG_ON((unsigned long)ptr >
  201. (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
  202. }
  203. #define GDB_SIZEOF_REG sizeof(unsigned long)
  204. #define GDB_SIZEOF_REG_U32 sizeof(u32)
  205. #ifdef CONFIG_PPC_85xx
  206. #define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long)
  207. #else
  208. #define GDB_SIZEOF_FLOAT_REG sizeof(u64)
  209. #endif
  210. struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
  211. {
  212. { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) },
  213. { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) },
  214. { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) },
  215. { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) },
  216. { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) },
  217. { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) },
  218. { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) },
  219. { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) },
  220. { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) },
  221. { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) },
  222. { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) },
  223. { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) },
  224. { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) },
  225. { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) },
  226. { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) },
  227. { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) },
  228. { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) },
  229. { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) },
  230. { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) },
  231. { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) },
  232. { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) },
  233. { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) },
  234. { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) },
  235. { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) },
  236. { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) },
  237. { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) },
  238. { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) },
  239. { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) },
  240. { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) },
  241. { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) },
  242. { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) },
  243. { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) },
  244. { "f0", GDB_SIZEOF_FLOAT_REG, 0 },
  245. { "f1", GDB_SIZEOF_FLOAT_REG, 1 },
  246. { "f2", GDB_SIZEOF_FLOAT_REG, 2 },
  247. { "f3", GDB_SIZEOF_FLOAT_REG, 3 },
  248. { "f4", GDB_SIZEOF_FLOAT_REG, 4 },
  249. { "f5", GDB_SIZEOF_FLOAT_REG, 5 },
  250. { "f6", GDB_SIZEOF_FLOAT_REG, 6 },
  251. { "f7", GDB_SIZEOF_FLOAT_REG, 7 },
  252. { "f8", GDB_SIZEOF_FLOAT_REG, 8 },
  253. { "f9", GDB_SIZEOF_FLOAT_REG, 9 },
  254. { "f10", GDB_SIZEOF_FLOAT_REG, 10 },
  255. { "f11", GDB_SIZEOF_FLOAT_REG, 11 },
  256. { "f12", GDB_SIZEOF_FLOAT_REG, 12 },
  257. { "f13", GDB_SIZEOF_FLOAT_REG, 13 },
  258. { "f14", GDB_SIZEOF_FLOAT_REG, 14 },
  259. { "f15", GDB_SIZEOF_FLOAT_REG, 15 },
  260. { "f16", GDB_SIZEOF_FLOAT_REG, 16 },
  261. { "f17", GDB_SIZEOF_FLOAT_REG, 17 },
  262. { "f18", GDB_SIZEOF_FLOAT_REG, 18 },
  263. { "f19", GDB_SIZEOF_FLOAT_REG, 19 },
  264. { "f20", GDB_SIZEOF_FLOAT_REG, 20 },
  265. { "f21", GDB_SIZEOF_FLOAT_REG, 21 },
  266. { "f22", GDB_SIZEOF_FLOAT_REG, 22 },
  267. { "f23", GDB_SIZEOF_FLOAT_REG, 23 },
  268. { "f24", GDB_SIZEOF_FLOAT_REG, 24 },
  269. { "f25", GDB_SIZEOF_FLOAT_REG, 25 },
  270. { "f26", GDB_SIZEOF_FLOAT_REG, 26 },
  271. { "f27", GDB_SIZEOF_FLOAT_REG, 27 },
  272. { "f28", GDB_SIZEOF_FLOAT_REG, 28 },
  273. { "f29", GDB_SIZEOF_FLOAT_REG, 29 },
  274. { "f30", GDB_SIZEOF_FLOAT_REG, 30 },
  275. { "f31", GDB_SIZEOF_FLOAT_REG, 31 },
  276. { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) },
  277. { "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) },
  278. { "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) },
  279. { "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) },
  280. { "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) },
  281. { "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) },
  282. };
  283. char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
  284. {
  285. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  286. return NULL;
  287. if (regno < 32 || regno >= 64)
  288. /* First 0 -> 31 gpr registers*/
  289. /* pc, msr, ls... registers 64 -> 69 */
  290. memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
  291. dbg_reg_def[regno].size);
  292. if (regno >= 32 && regno < 64) {
  293. /* FP registers 32 -> 63 */
  294. #if defined(CONFIG_PPC_85xx) && defined(CONFIG_SPE)
  295. if (current)
  296. memcpy(mem, &current->thread.evr[regno-32],
  297. dbg_reg_def[regno].size);
  298. #else
  299. /* fp registers not used by kernel, leave zero */
  300. memset(mem, 0, dbg_reg_def[regno].size);
  301. #endif
  302. }
  303. return dbg_reg_def[regno].name;
  304. }
  305. int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
  306. {
  307. if (regno >= DBG_MAX_REG_NUM || regno < 0)
  308. return -EINVAL;
  309. if (regno < 32 || regno >= 64)
  310. /* First 0 -> 31 gpr registers*/
  311. /* pc, msr, ls... registers 64 -> 69 */
  312. memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
  313. dbg_reg_def[regno].size);
  314. if (regno >= 32 && regno < 64) {
  315. /* FP registers 32 -> 63 */
  316. #if defined(CONFIG_PPC_85xx) && defined(CONFIG_SPE)
  317. memcpy(&current->thread.evr[regno-32], mem,
  318. dbg_reg_def[regno].size);
  319. #else
  320. /* fp registers not used by kernel, leave zero */
  321. return 0;
  322. #endif
  323. }
  324. return 0;
  325. }
  326. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
  327. {
  328. regs_set_return_ip(regs, pc);
  329. }
  330. /*
  331. * This function does PowerPC specific processing for interfacing to gdb.
  332. */
  333. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  334. char *remcom_in_buffer, char *remcom_out_buffer,
  335. struct pt_regs *linux_regs)
  336. {
  337. char *ptr = &remcom_in_buffer[1];
  338. unsigned long addr;
  339. switch (remcom_in_buffer[0]) {
  340. /*
  341. * sAA..AA Step one instruction from AA..AA
  342. * This will return an error to gdb ..
  343. */
  344. case 's':
  345. case 'c':
  346. /* handle the optional parameter */
  347. if (kgdb_hex2long(&ptr, &addr))
  348. regs_set_return_ip(linux_regs, addr);
  349. atomic_set(&kgdb_cpu_doing_single_step, -1);
  350. /* set the trace bit if we're stepping */
  351. if (remcom_in_buffer[0] == 's') {
  352. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  353. mtspr(SPRN_DBCR0,
  354. mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
  355. regs_set_return_msr(linux_regs, linux_regs->msr | MSR_DE);
  356. #else
  357. regs_set_return_msr(linux_regs, linux_regs->msr | MSR_SE);
  358. #endif
  359. atomic_set(&kgdb_cpu_doing_single_step,
  360. raw_smp_processor_id());
  361. }
  362. return 0;
  363. }
  364. return -1;
  365. }
  366. int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
  367. {
  368. u32 instr, *addr = (u32 *)bpt->bpt_addr;
  369. int err;
  370. err = get_kernel_nofault(instr, addr);
  371. if (err)
  372. return err;
  373. err = patch_instruction(addr, ppc_inst(BREAK_INSTR));
  374. if (err)
  375. return -EFAULT;
  376. *(u32 *)bpt->saved_instr = instr;
  377. return 0;
  378. }
  379. int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
  380. {
  381. int err;
  382. unsigned int instr = *(unsigned int *)bpt->saved_instr;
  383. u32 *addr = (u32 *)bpt->bpt_addr;
  384. err = patch_instruction(addr, ppc_inst(instr));
  385. if (err)
  386. return -EFAULT;
  387. return 0;
  388. }
  389. /*
  390. * Global data
  391. */
  392. const struct kgdb_arch arch_kgdb_ops;
  393. static int kgdb_not_implemented(struct pt_regs *regs)
  394. {
  395. return 0;
  396. }
  397. static void *old__debugger_ipi;
  398. static void *old__debugger;
  399. static void *old__debugger_bpt;
  400. static void *old__debugger_sstep;
  401. static void *old__debugger_iabr_match;
  402. static void *old__debugger_break_match;
  403. static void *old__debugger_fault_handler;
  404. int kgdb_arch_init(void)
  405. {
  406. old__debugger_ipi = __debugger_ipi;
  407. old__debugger = __debugger;
  408. old__debugger_bpt = __debugger_bpt;
  409. old__debugger_sstep = __debugger_sstep;
  410. old__debugger_iabr_match = __debugger_iabr_match;
  411. old__debugger_break_match = __debugger_break_match;
  412. old__debugger_fault_handler = __debugger_fault_handler;
  413. __debugger_ipi = kgdb_debugger_ipi;
  414. __debugger = kgdb_debugger;
  415. __debugger_bpt = kgdb_handle_breakpoint;
  416. __debugger_sstep = kgdb_singlestep;
  417. __debugger_iabr_match = kgdb_iabr_match;
  418. __debugger_break_match = kgdb_break_match;
  419. __debugger_fault_handler = kgdb_not_implemented;
  420. return 0;
  421. }
  422. void kgdb_arch_exit(void)
  423. {
  424. __debugger_ipi = old__debugger_ipi;
  425. __debugger = old__debugger;
  426. __debugger_bpt = old__debugger_bpt;
  427. __debugger_sstep = old__debugger_sstep;
  428. __debugger_iabr_match = old__debugger_iabr_match;
  429. __debugger_break_match = old__debugger_break_match;
  430. __debugger_fault_handler = old__debugger_fault_handler;
  431. }