kgdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* kgdb support for MN10300
  2. *
  3. * Copyright (C) 2010 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/kgdb.h>
  14. #include <linux/uaccess.h>
  15. #include <unit/leds.h>
  16. #include <unit/serial.h>
  17. #include <asm/debugger.h>
  18. #include <asm/serial-regs.h>
  19. #include "internal.h"
  20. /*
  21. * Software single-stepping breakpoint save (used by __switch_to())
  22. */
  23. static struct thread_info *kgdb_sstep_thread;
  24. u8 *kgdb_sstep_bp_addr[2];
  25. u8 kgdb_sstep_bp[2];
  26. /*
  27. * Copy kernel exception frame registers to the GDB register file
  28. */
  29. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  30. {
  31. unsigned long ssp = (unsigned long) (regs + 1);
  32. gdb_regs[GDB_FR_D0] = regs->d0;
  33. gdb_regs[GDB_FR_D1] = regs->d1;
  34. gdb_regs[GDB_FR_D2] = regs->d2;
  35. gdb_regs[GDB_FR_D3] = regs->d3;
  36. gdb_regs[GDB_FR_A0] = regs->a0;
  37. gdb_regs[GDB_FR_A1] = regs->a1;
  38. gdb_regs[GDB_FR_A2] = regs->a2;
  39. gdb_regs[GDB_FR_A3] = regs->a3;
  40. gdb_regs[GDB_FR_SP] = (regs->epsw & EPSW_nSL) ? regs->sp : ssp;
  41. gdb_regs[GDB_FR_PC] = regs->pc;
  42. gdb_regs[GDB_FR_MDR] = regs->mdr;
  43. gdb_regs[GDB_FR_EPSW] = regs->epsw;
  44. gdb_regs[GDB_FR_LIR] = regs->lir;
  45. gdb_regs[GDB_FR_LAR] = regs->lar;
  46. gdb_regs[GDB_FR_MDRQ] = regs->mdrq;
  47. gdb_regs[GDB_FR_E0] = regs->e0;
  48. gdb_regs[GDB_FR_E1] = regs->e1;
  49. gdb_regs[GDB_FR_E2] = regs->e2;
  50. gdb_regs[GDB_FR_E3] = regs->e3;
  51. gdb_regs[GDB_FR_E4] = regs->e4;
  52. gdb_regs[GDB_FR_E5] = regs->e5;
  53. gdb_regs[GDB_FR_E6] = regs->e6;
  54. gdb_regs[GDB_FR_E7] = regs->e7;
  55. gdb_regs[GDB_FR_SSP] = ssp;
  56. gdb_regs[GDB_FR_MSP] = 0;
  57. gdb_regs[GDB_FR_USP] = regs->sp;
  58. gdb_regs[GDB_FR_MCRH] = regs->mcrh;
  59. gdb_regs[GDB_FR_MCRL] = regs->mcrl;
  60. gdb_regs[GDB_FR_MCVF] = regs->mcvf;
  61. gdb_regs[GDB_FR_DUMMY0] = 0;
  62. gdb_regs[GDB_FR_DUMMY1] = 0;
  63. gdb_regs[GDB_FR_FS0] = 0;
  64. }
  65. /*
  66. * Extracts kernel SP/PC values understandable by gdb from the values
  67. * saved by switch_to().
  68. */
  69. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  70. {
  71. gdb_regs[GDB_FR_SSP] = p->thread.sp;
  72. gdb_regs[GDB_FR_PC] = p->thread.pc;
  73. gdb_regs[GDB_FR_A3] = p->thread.a3;
  74. gdb_regs[GDB_FR_USP] = p->thread.usp;
  75. gdb_regs[GDB_FR_FPCR] = p->thread.fpu_state.fpcr;
  76. }
  77. /*
  78. * Fill kernel exception frame registers from the GDB register file
  79. */
  80. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  81. {
  82. regs->d0 = gdb_regs[GDB_FR_D0];
  83. regs->d1 = gdb_regs[GDB_FR_D1];
  84. regs->d2 = gdb_regs[GDB_FR_D2];
  85. regs->d3 = gdb_regs[GDB_FR_D3];
  86. regs->a0 = gdb_regs[GDB_FR_A0];
  87. regs->a1 = gdb_regs[GDB_FR_A1];
  88. regs->a2 = gdb_regs[GDB_FR_A2];
  89. regs->a3 = gdb_regs[GDB_FR_A3];
  90. regs->sp = gdb_regs[GDB_FR_SP];
  91. regs->pc = gdb_regs[GDB_FR_PC];
  92. regs->mdr = gdb_regs[GDB_FR_MDR];
  93. regs->epsw = gdb_regs[GDB_FR_EPSW];
  94. regs->lir = gdb_regs[GDB_FR_LIR];
  95. regs->lar = gdb_regs[GDB_FR_LAR];
  96. regs->mdrq = gdb_regs[GDB_FR_MDRQ];
  97. regs->e0 = gdb_regs[GDB_FR_E0];
  98. regs->e1 = gdb_regs[GDB_FR_E1];
  99. regs->e2 = gdb_regs[GDB_FR_E2];
  100. regs->e3 = gdb_regs[GDB_FR_E3];
  101. regs->e4 = gdb_regs[GDB_FR_E4];
  102. regs->e5 = gdb_regs[GDB_FR_E5];
  103. regs->e6 = gdb_regs[GDB_FR_E6];
  104. regs->e7 = gdb_regs[GDB_FR_E7];
  105. regs->sp = gdb_regs[GDB_FR_SSP];
  106. /* gdb_regs[GDB_FR_MSP]; */
  107. // regs->usp = gdb_regs[GDB_FR_USP];
  108. regs->mcrh = gdb_regs[GDB_FR_MCRH];
  109. regs->mcrl = gdb_regs[GDB_FR_MCRL];
  110. regs->mcvf = gdb_regs[GDB_FR_MCVF];
  111. /* gdb_regs[GDB_FR_DUMMY0]; */
  112. /* gdb_regs[GDB_FR_DUMMY1]; */
  113. // regs->fpcr = gdb_regs[GDB_FR_FPCR];
  114. // regs->fs0 = gdb_regs[GDB_FR_FS0];
  115. }
  116. struct kgdb_arch arch_kgdb_ops = {
  117. .gdb_bpt_instr = { 0xff },
  118. .flags = KGDB_HW_BREAKPOINT,
  119. };
  120. static const unsigned char mn10300_kgdb_insn_sizes[256] =
  121. {
  122. /* 1 2 3 4 5 6 7 8 9 a b c d e f */
  123. 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, /* 0 */
  124. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
  125. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
  126. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
  127. 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
  128. 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
  129. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
  130. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
  131. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
  132. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
  133. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
  134. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
  135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
  136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
  137. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
  138. 0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1 /* f */
  139. };
  140. /*
  141. * Attempt to emulate single stepping by means of breakpoint instructions.
  142. * Although there is a single-step trace flag in EPSW, its use is not
  143. * sufficiently documented and is only intended for use with the JTAG debugger.
  144. */
  145. static int kgdb_arch_do_singlestep(struct pt_regs *regs)
  146. {
  147. unsigned long arg;
  148. unsigned size;
  149. u8 *pc = (u8 *)regs->pc, *sp = (u8 *)(regs + 1), cur;
  150. u8 *x = NULL, *y = NULL;
  151. int ret;
  152. ret = probe_kernel_read(&cur, pc, 1);
  153. if (ret < 0)
  154. return ret;
  155. size = mn10300_kgdb_insn_sizes[cur];
  156. if (size > 0) {
  157. x = pc + size;
  158. goto set_x;
  159. }
  160. switch (cur) {
  161. /* Bxx (d8,PC) */
  162. case 0xc0 ... 0xca:
  163. ret = probe_kernel_read(&arg, pc + 1, 1);
  164. if (ret < 0)
  165. return ret;
  166. x = pc + 2;
  167. if (arg >= 0 && arg <= 2)
  168. goto set_x;
  169. y = pc + (s8)arg;
  170. goto set_x_and_y;
  171. /* LXX (d8,PC) */
  172. case 0xd0 ... 0xda:
  173. x = pc + 1;
  174. if (regs->pc == regs->lar)
  175. goto set_x;
  176. y = (u8 *)regs->lar;
  177. goto set_x_and_y;
  178. /* SETLB - loads the next four bytes into the LIR register
  179. * (which mustn't include a breakpoint instruction) */
  180. case 0xdb:
  181. x = pc + 5;
  182. goto set_x;
  183. /* JMP (d16,PC) or CALL (d16,PC) */
  184. case 0xcc:
  185. case 0xcd:
  186. ret = probe_kernel_read(&arg, pc + 1, 2);
  187. if (ret < 0)
  188. return ret;
  189. x = pc + (s16)arg;
  190. goto set_x;
  191. /* JMP (d32,PC) or CALL (d32,PC) */
  192. case 0xdc:
  193. case 0xdd:
  194. ret = probe_kernel_read(&arg, pc + 1, 4);
  195. if (ret < 0)
  196. return ret;
  197. x = pc + (s32)arg;
  198. goto set_x;
  199. /* RETF */
  200. case 0xde:
  201. x = (u8 *)regs->mdr;
  202. goto set_x;
  203. /* RET */
  204. case 0xdf:
  205. ret = probe_kernel_read(&arg, pc + 2, 1);
  206. if (ret < 0)
  207. return ret;
  208. ret = probe_kernel_read(&x, sp + (s8)arg, 4);
  209. if (ret < 0)
  210. return ret;
  211. goto set_x;
  212. case 0xf0:
  213. ret = probe_kernel_read(&cur, pc + 1, 1);
  214. if (ret < 0)
  215. return ret;
  216. if (cur >= 0xf0 && cur <= 0xf7) {
  217. /* JMP (An) / CALLS (An) */
  218. switch (cur & 3) {
  219. case 0: x = (u8 *)regs->a0; break;
  220. case 1: x = (u8 *)regs->a1; break;
  221. case 2: x = (u8 *)regs->a2; break;
  222. case 3: x = (u8 *)regs->a3; break;
  223. }
  224. goto set_x;
  225. } else if (cur == 0xfc) {
  226. /* RETS */
  227. ret = probe_kernel_read(&x, sp, 4);
  228. if (ret < 0)
  229. return ret;
  230. goto set_x;
  231. } else if (cur == 0xfd) {
  232. /* RTI */
  233. ret = probe_kernel_read(&x, sp + 4, 4);
  234. if (ret < 0)
  235. return ret;
  236. goto set_x;
  237. } else {
  238. x = pc + 2;
  239. goto set_x;
  240. }
  241. break;
  242. /* potential 3-byte conditional branches */
  243. case 0xf8:
  244. ret = probe_kernel_read(&cur, pc + 1, 1);
  245. if (ret < 0)
  246. return ret;
  247. x = pc + 3;
  248. if (cur >= 0xe8 && cur <= 0xeb) {
  249. ret = probe_kernel_read(&arg, pc + 2, 1);
  250. if (ret < 0)
  251. return ret;
  252. if (arg >= 0 && arg <= 3)
  253. goto set_x;
  254. y = pc + (s8)arg;
  255. goto set_x_and_y;
  256. }
  257. goto set_x;
  258. case 0xfa:
  259. ret = probe_kernel_read(&cur, pc + 1, 1);
  260. if (ret < 0)
  261. return ret;
  262. if (cur == 0xff) {
  263. /* CALLS (d16,PC) */
  264. ret = probe_kernel_read(&arg, pc + 2, 2);
  265. if (ret < 0)
  266. return ret;
  267. x = pc + (s16)arg;
  268. goto set_x;
  269. }
  270. x = pc + 4;
  271. goto set_x;
  272. case 0xfc:
  273. ret = probe_kernel_read(&cur, pc + 1, 1);
  274. if (ret < 0)
  275. return ret;
  276. if (cur == 0xff) {
  277. /* CALLS (d32,PC) */
  278. ret = probe_kernel_read(&arg, pc + 2, 4);
  279. if (ret < 0)
  280. return ret;
  281. x = pc + (s32)arg;
  282. goto set_x;
  283. }
  284. x = pc + 6;
  285. goto set_x;
  286. }
  287. return 0;
  288. set_x:
  289. kgdb_sstep_bp_addr[0] = x;
  290. kgdb_sstep_bp_addr[1] = NULL;
  291. ret = probe_kernel_read(&kgdb_sstep_bp[0], x, 1);
  292. if (ret < 0)
  293. return ret;
  294. ret = probe_kernel_write(x, &arch_kgdb_ops.gdb_bpt_instr, 1);
  295. if (ret < 0)
  296. return ret;
  297. kgdb_sstep_thread = current_thread_info();
  298. debugger_local_cache_flushinv_one(x);
  299. return ret;
  300. set_x_and_y:
  301. kgdb_sstep_bp_addr[0] = x;
  302. kgdb_sstep_bp_addr[1] = y;
  303. ret = probe_kernel_read(&kgdb_sstep_bp[0], x, 1);
  304. if (ret < 0)
  305. return ret;
  306. ret = probe_kernel_read(&kgdb_sstep_bp[1], y, 1);
  307. if (ret < 0)
  308. return ret;
  309. ret = probe_kernel_write(x, &arch_kgdb_ops.gdb_bpt_instr, 1);
  310. if (ret < 0)
  311. return ret;
  312. ret = probe_kernel_write(y, &arch_kgdb_ops.gdb_bpt_instr, 1);
  313. if (ret < 0) {
  314. probe_kernel_write(kgdb_sstep_bp_addr[0],
  315. &kgdb_sstep_bp[0], 1);
  316. } else {
  317. kgdb_sstep_thread = current_thread_info();
  318. }
  319. debugger_local_cache_flushinv_one(x);
  320. debugger_local_cache_flushinv_one(y);
  321. return ret;
  322. }
  323. /*
  324. * Remove emplaced single-step breakpoints, returning true if we hit one of
  325. * them.
  326. */
  327. static bool kgdb_arch_undo_singlestep(struct pt_regs *regs)
  328. {
  329. bool hit = false;
  330. u8 *x = kgdb_sstep_bp_addr[0], *y = kgdb_sstep_bp_addr[1];
  331. u8 opcode;
  332. if (kgdb_sstep_thread == current_thread_info()) {
  333. if (x) {
  334. if (x == (u8 *)regs->pc)
  335. hit = true;
  336. if (probe_kernel_read(&opcode, x,
  337. 1) < 0 ||
  338. opcode != 0xff)
  339. BUG();
  340. probe_kernel_write(x, &kgdb_sstep_bp[0], 1);
  341. debugger_local_cache_flushinv_one(x);
  342. }
  343. if (y) {
  344. if (y == (u8 *)regs->pc)
  345. hit = true;
  346. if (probe_kernel_read(&opcode, y,
  347. 1) < 0 ||
  348. opcode != 0xff)
  349. BUG();
  350. probe_kernel_write(y, &kgdb_sstep_bp[1], 1);
  351. debugger_local_cache_flushinv_one(y);
  352. }
  353. }
  354. kgdb_sstep_bp_addr[0] = NULL;
  355. kgdb_sstep_bp_addr[1] = NULL;
  356. kgdb_sstep_thread = NULL;
  357. return hit;
  358. }
  359. /*
  360. * Catch a single-step-pending thread being deleted and make sure the global
  361. * single-step state is cleared. At this point the breakpoints should have
  362. * been removed by __switch_to().
  363. */
  364. void arch_release_thread_info(struct thread_info *ti)
  365. {
  366. if (kgdb_sstep_thread == ti) {
  367. kgdb_sstep_thread = NULL;
  368. /* However, we may now be running in degraded mode, with most
  369. * of the CPUs disabled until such a time as KGDB is reentered,
  370. * so force immediate reentry */
  371. kgdb_breakpoint();
  372. }
  373. }
  374. /*
  375. * Handle unknown packets and [CcsDk] packets
  376. * - at this point breakpoints have been installed
  377. */
  378. int kgdb_arch_handle_exception(int vector, int signo, int err_code,
  379. char *remcom_in_buffer, char *remcom_out_buffer,
  380. struct pt_regs *regs)
  381. {
  382. long addr;
  383. char *ptr;
  384. switch (remcom_in_buffer[0]) {
  385. case 'c':
  386. case 's':
  387. /* try to read optional parameter, pc unchanged if no parm */
  388. ptr = &remcom_in_buffer[1];
  389. if (kgdb_hex2long(&ptr, &addr))
  390. regs->pc = addr;
  391. case 'D':
  392. case 'k':
  393. atomic_set(&kgdb_cpu_doing_single_step, -1);
  394. if (remcom_in_buffer[0] == 's') {
  395. kgdb_arch_do_singlestep(regs);
  396. kgdb_single_step = 1;
  397. atomic_set(&kgdb_cpu_doing_single_step,
  398. raw_smp_processor_id());
  399. }
  400. return 0;
  401. }
  402. return -1; /* this means that we do not want to exit from the handler */
  403. }
  404. /*
  405. * Handle event interception
  406. * - returns 0 if the exception should be skipped, -ERROR otherwise.
  407. */
  408. int debugger_intercept(enum exception_code excep, int signo, int si_code,
  409. struct pt_regs *regs)
  410. {
  411. int ret;
  412. if (kgdb_arch_undo_singlestep(regs)) {
  413. excep = EXCEP_TRAP;
  414. signo = SIGTRAP;
  415. si_code = TRAP_TRACE;
  416. }
  417. ret = kgdb_handle_exception(excep, signo, si_code, regs);
  418. debugger_local_cache_flushinv();
  419. return ret;
  420. }
  421. /*
  422. * Determine if we've hit a debugger special breakpoint
  423. */
  424. int at_debugger_breakpoint(struct pt_regs *regs)
  425. {
  426. return regs->pc == (unsigned long)&__arch_kgdb_breakpoint;
  427. }
  428. /*
  429. * Initialise kgdb
  430. */
  431. int kgdb_arch_init(void)
  432. {
  433. return 0;
  434. }
  435. /*
  436. * Do something, perhaps, but don't know what.
  437. */
  438. void kgdb_arch_exit(void)
  439. {
  440. }
  441. #ifdef CONFIG_SMP
  442. void debugger_nmi_interrupt(struct pt_regs *regs, enum exception_code code)
  443. {
  444. kgdb_nmicallback(arch_smp_processor_id(), regs);
  445. debugger_local_cache_flushinv();
  446. }
  447. void kgdb_roundup_cpus(unsigned long flags)
  448. {
  449. smp_jump_to_debugger();
  450. }
  451. #endif