kgdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * File: arch/blackfin/kernel/kgdb.c
  3. * Based on:
  4. * Author: Sonic Zhang
  5. *
  6. * Created:
  7. * Description:
  8. *
  9. * Rev: $Id: kgdb_bfin_linux-2.6.x.patch 4934 2007-02-13 09:32:11Z sonicz $
  10. *
  11. * Modified:
  12. * Copyright 2005-2006 Analog Devices Inc.
  13. *
  14. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see the file COPYING, or write
  28. * to the Free Software Foundation, Inc.,
  29. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #include <linux/string.h>
  32. #include <linux/kernel.h>
  33. #include <linux/sched.h>
  34. #include <linux/smp.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/delay.h>
  37. #include <linux/ptrace.h> /* for linux pt_regs struct */
  38. #include <linux/kgdb.h>
  39. #include <linux/console.h>
  40. #include <linux/init.h>
  41. #include <linux/debugger.h>
  42. #include <linux/errno.h>
  43. #include <linux/irq.h>
  44. #include <asm/system.h>
  45. #include <asm/traps.h>
  46. #include <asm/blackfin.h>
  47. /* Put the error code here just in case the user cares. */
  48. int gdb_bf533errcode;
  49. /* Likewise, the vector number here (since GDB only gets the signal
  50. number through the usual means, and that's not very specific). */
  51. int gdb_bf533vector = -1;
  52. #if KGDB_MAX_NO_CPUS != 8
  53. #error change the definition of slavecpulocks
  54. #endif
  55. void regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  56. {
  57. gdb_regs[BFIN_R0] = regs->r0;
  58. gdb_regs[BFIN_R1] = regs->r1;
  59. gdb_regs[BFIN_R2] = regs->r2;
  60. gdb_regs[BFIN_R3] = regs->r3;
  61. gdb_regs[BFIN_R4] = regs->r4;
  62. gdb_regs[BFIN_R5] = regs->r5;
  63. gdb_regs[BFIN_R6] = regs->r6;
  64. gdb_regs[BFIN_R7] = regs->r7;
  65. gdb_regs[BFIN_P0] = regs->p0;
  66. gdb_regs[BFIN_P1] = regs->p1;
  67. gdb_regs[BFIN_P2] = regs->p2;
  68. gdb_regs[BFIN_P3] = regs->p3;
  69. gdb_regs[BFIN_P4] = regs->p4;
  70. gdb_regs[BFIN_P5] = regs->p5;
  71. gdb_regs[BFIN_SP] = regs->reserved;
  72. gdb_regs[BFIN_FP] = regs->fp;
  73. gdb_regs[BFIN_I0] = regs->i0;
  74. gdb_regs[BFIN_I1] = regs->i1;
  75. gdb_regs[BFIN_I2] = regs->i2;
  76. gdb_regs[BFIN_I3] = regs->i3;
  77. gdb_regs[BFIN_M0] = regs->m0;
  78. gdb_regs[BFIN_M1] = regs->m1;
  79. gdb_regs[BFIN_M2] = regs->m2;
  80. gdb_regs[BFIN_M3] = regs->m3;
  81. gdb_regs[BFIN_B0] = regs->b0;
  82. gdb_regs[BFIN_B1] = regs->b1;
  83. gdb_regs[BFIN_B2] = regs->b2;
  84. gdb_regs[BFIN_B3] = regs->b3;
  85. gdb_regs[BFIN_L0] = regs->l0;
  86. gdb_regs[BFIN_L1] = regs->l1;
  87. gdb_regs[BFIN_L2] = regs->l2;
  88. gdb_regs[BFIN_L3] = regs->l3;
  89. gdb_regs[BFIN_A0_DOT_X] = regs->a0x;
  90. gdb_regs[BFIN_A0_DOT_W] = regs->a0w;
  91. gdb_regs[BFIN_A1_DOT_X] = regs->a1x;
  92. gdb_regs[BFIN_A1_DOT_W] = regs->a1w;
  93. gdb_regs[BFIN_ASTAT] = regs->astat;
  94. gdb_regs[BFIN_RETS] = regs->rets;
  95. gdb_regs[BFIN_LC0] = regs->lc0;
  96. gdb_regs[BFIN_LT0] = regs->lt0;
  97. gdb_regs[BFIN_LB0] = regs->lb0;
  98. gdb_regs[BFIN_LC1] = regs->lc1;
  99. gdb_regs[BFIN_LT1] = regs->lt1;
  100. gdb_regs[BFIN_LB1] = regs->lb1;
  101. gdb_regs[BFIN_CYCLES] = 0;
  102. gdb_regs[BFIN_CYCLES2] = 0;
  103. gdb_regs[BFIN_USP] = regs->usp;
  104. gdb_regs[BFIN_SEQSTAT] = regs->seqstat;
  105. gdb_regs[BFIN_SYSCFG] = regs->syscfg;
  106. gdb_regs[BFIN_RETI] = regs->pc;
  107. gdb_regs[BFIN_RETX] = regs->retx;
  108. gdb_regs[BFIN_RETN] = regs->retn;
  109. gdb_regs[BFIN_RETE] = regs->rete;
  110. gdb_regs[BFIN_PC] = regs->pc;
  111. gdb_regs[BFIN_CC] = 0;
  112. gdb_regs[BFIN_EXTRA1] = 0;
  113. gdb_regs[BFIN_EXTRA2] = 0;
  114. gdb_regs[BFIN_EXTRA3] = 0;
  115. gdb_regs[BFIN_IPEND] = regs->ipend;
  116. }
  117. /*
  118. * Extracts ebp, esp and eip values understandable by gdb from the values
  119. * saved by switch_to.
  120. * thread.esp points to ebp. flags and ebp are pushed in switch_to hence esp
  121. * prior to entering switch_to is 8 greater then the value that is saved.
  122. * If switch_to changes, change following code appropriately.
  123. */
  124. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  125. {
  126. gdb_regs[BFIN_SP] = p->thread.ksp;
  127. gdb_regs[BFIN_PC] = p->thread.pc;
  128. gdb_regs[BFIN_SEQSTAT] = p->thread.seqstat;
  129. }
  130. void gdb_regs_to_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  131. {
  132. regs->r0 = gdb_regs[BFIN_R0];
  133. regs->r1 = gdb_regs[BFIN_R1];
  134. regs->r2 = gdb_regs[BFIN_R2];
  135. regs->r3 = gdb_regs[BFIN_R3];
  136. regs->r4 = gdb_regs[BFIN_R4];
  137. regs->r5 = gdb_regs[BFIN_R5];
  138. regs->r6 = gdb_regs[BFIN_R6];
  139. regs->r7 = gdb_regs[BFIN_R7];
  140. regs->p0 = gdb_regs[BFIN_P0];
  141. regs->p1 = gdb_regs[BFIN_P1];
  142. regs->p2 = gdb_regs[BFIN_P2];
  143. regs->p3 = gdb_regs[BFIN_P3];
  144. regs->p4 = gdb_regs[BFIN_P4];
  145. regs->p5 = gdb_regs[BFIN_P5];
  146. regs->fp = gdb_regs[BFIN_FP];
  147. regs->i0 = gdb_regs[BFIN_I0];
  148. regs->i1 = gdb_regs[BFIN_I1];
  149. regs->i2 = gdb_regs[BFIN_I2];
  150. regs->i3 = gdb_regs[BFIN_I3];
  151. regs->m0 = gdb_regs[BFIN_M0];
  152. regs->m1 = gdb_regs[BFIN_M1];
  153. regs->m2 = gdb_regs[BFIN_M2];
  154. regs->m3 = gdb_regs[BFIN_M3];
  155. regs->b0 = gdb_regs[BFIN_B0];
  156. regs->b1 = gdb_regs[BFIN_B1];
  157. regs->b2 = gdb_regs[BFIN_B2];
  158. regs->b3 = gdb_regs[BFIN_B3];
  159. regs->l0 = gdb_regs[BFIN_L0];
  160. regs->l1 = gdb_regs[BFIN_L1];
  161. regs->l2 = gdb_regs[BFIN_L2];
  162. regs->l3 = gdb_regs[BFIN_L3];
  163. regs->a0x = gdb_regs[BFIN_A0_DOT_X];
  164. regs->a0w = gdb_regs[BFIN_A0_DOT_W];
  165. regs->a1x = gdb_regs[BFIN_A1_DOT_X];
  166. regs->a1w = gdb_regs[BFIN_A1_DOT_W];
  167. regs->rets = gdb_regs[BFIN_RETS];
  168. regs->lc0 = gdb_regs[BFIN_LC0];
  169. regs->lt0 = gdb_regs[BFIN_LT0];
  170. regs->lb0 = gdb_regs[BFIN_LB0];
  171. regs->lc1 = gdb_regs[BFIN_LC1];
  172. regs->lt1 = gdb_regs[BFIN_LT1];
  173. regs->lb1 = gdb_regs[BFIN_LB1];
  174. regs->usp = gdb_regs[BFIN_USP];
  175. regs->syscfg = gdb_regs[BFIN_SYSCFG];
  176. regs->retx = gdb_regs[BFIN_PC];
  177. regs->retn = gdb_regs[BFIN_RETN];
  178. regs->rete = gdb_regs[BFIN_RETE];
  179. regs->pc = gdb_regs[BFIN_PC];
  180. #if 0 /* can't change these */
  181. regs->astat = gdb_regs[BFIN_ASTAT];
  182. regs->seqstat = gdb_regs[BFIN_SEQSTAT];
  183. regs->ipend = gdb_regs[BFIN_IPEND];
  184. #endif
  185. }
  186. struct hw_breakpoint {
  187. unsigned int occupied:1;
  188. unsigned int skip:1;
  189. unsigned int enabled:1;
  190. unsigned int type:1;
  191. unsigned int dataacc:2;
  192. unsigned short count;
  193. unsigned int addr;
  194. } breakinfo[HW_BREAKPOINT_NUM];
  195. int kgdb_arch_init(void)
  196. {
  197. debugger_step = 0;
  198. kgdb_remove_all_hw_break();
  199. return 0;
  200. }
  201. int kgdb_set_hw_break(unsigned long addr)
  202. {
  203. int breakno;
  204. for (breakno = 0; breakno < HW_BREAKPOINT_NUM; breakno++)
  205. if (!breakinfo[breakno].occupied) {
  206. breakinfo[breakno].occupied = 1;
  207. breakinfo[breakno].enabled = 1;
  208. breakinfo[breakno].type = 1;
  209. breakinfo[breakno].addr = addr;
  210. return 0;
  211. }
  212. return -ENOSPC;
  213. }
  214. int kgdb_remove_hw_break(unsigned long addr)
  215. {
  216. int breakno;
  217. for (breakno = 0; breakno < HW_BREAKPOINT_NUM; breakno++)
  218. if (breakinfo[breakno].addr == addr)
  219. memset(&(breakinfo[breakno]), 0, sizeof(struct hw_breakpoint));
  220. return 0;
  221. }
  222. void kgdb_remove_all_hw_break(void)
  223. {
  224. memset(breakinfo, 0, sizeof(struct hw_breakpoint)*8);
  225. }
  226. /*
  227. void kgdb_show_info(void)
  228. {
  229. printk(KERN_DEBUG "hwd: wpia0=0x%x, wpiacnt0=%d, wpiactl=0x%x, wpstat=0x%x\n",
  230. bfin_read_WPIA0(), bfin_read_WPIACNT0(),
  231. bfin_read_WPIACTL(), bfin_read_WPSTAT());
  232. }
  233. */
  234. void kgdb_correct_hw_break(void)
  235. {
  236. int breakno;
  237. int correctit;
  238. uint32_t wpdactl = bfin_read_WPDACTL();
  239. correctit = 0;
  240. for (breakno = 0; breakno < HW_BREAKPOINT_NUM; breakno++) {
  241. if (breakinfo[breakno].type == 1) {
  242. switch (breakno) {
  243. case 0:
  244. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN0)) {
  245. correctit = 1;
  246. wpdactl &= ~(WPIREN01|EMUSW0);
  247. wpdactl |= WPIAEN0|WPICNTEN0;
  248. bfin_write_WPIA0(breakinfo[breakno].addr);
  249. bfin_write_WPIACNT0(breakinfo[breakno].skip);
  250. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN0)) {
  251. correctit = 1;
  252. wpdactl &= ~WPIAEN0;
  253. }
  254. break;
  255. case 1:
  256. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN1)) {
  257. correctit = 1;
  258. wpdactl &= ~(WPIREN01|EMUSW1);
  259. wpdactl |= WPIAEN1|WPICNTEN1;
  260. bfin_write_WPIA1(breakinfo[breakno].addr);
  261. bfin_write_WPIACNT1(breakinfo[breakno].skip);
  262. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN1)) {
  263. correctit = 1;
  264. wpdactl &= ~WPIAEN1;
  265. }
  266. break;
  267. case 2:
  268. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN2)) {
  269. correctit = 1;
  270. wpdactl &= ~(WPIREN23|EMUSW2);
  271. wpdactl |= WPIAEN2|WPICNTEN2;
  272. bfin_write_WPIA2(breakinfo[breakno].addr);
  273. bfin_write_WPIACNT2(breakinfo[breakno].skip);
  274. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN2)) {
  275. correctit = 1;
  276. wpdactl &= ~WPIAEN2;
  277. }
  278. break;
  279. case 3:
  280. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN3)) {
  281. correctit = 1;
  282. wpdactl &= ~(WPIREN23|EMUSW3);
  283. wpdactl |= WPIAEN3|WPICNTEN3;
  284. bfin_write_WPIA3(breakinfo[breakno].addr);
  285. bfin_write_WPIACNT3(breakinfo[breakno].skip);
  286. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN3)) {
  287. correctit = 1;
  288. wpdactl &= ~WPIAEN3;
  289. }
  290. break;
  291. case 4:
  292. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN4)) {
  293. correctit = 1;
  294. wpdactl &= ~(WPIREN45|EMUSW4);
  295. wpdactl |= WPIAEN4|WPICNTEN4;
  296. bfin_write_WPIA4(breakinfo[breakno].addr);
  297. bfin_write_WPIACNT4(breakinfo[breakno].skip);
  298. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN4)) {
  299. correctit = 1;
  300. wpdactl &= ~WPIAEN4;
  301. }
  302. break;
  303. case 5:
  304. if (breakinfo[breakno].enabled && !(wpdactl & WPIAEN5)) {
  305. correctit = 1;
  306. wpdactl &= ~(WPIREN45|EMUSW5);
  307. wpdactl |= WPIAEN5|WPICNTEN5;
  308. bfin_write_WPIA5(breakinfo[breakno].addr);
  309. bfin_write_WPIACNT5(breakinfo[breakno].skip);
  310. } else if (!breakinfo[breakno].enabled && (wpdactl & WPIAEN5)) {
  311. correctit = 1;
  312. wpdactl &= ~WPIAEN5;
  313. }
  314. break;
  315. }
  316. }
  317. }
  318. if (correctit) {
  319. wpdactl &= ~WPAND;
  320. wpdactl |= WPPWR;
  321. /*printk("correct_hw_break: wpdactl=0x%x\n", wpdactl);*/
  322. bfin_write_WPDACTL(wpdactl);
  323. CSYNC();
  324. /*kgdb_show_info();*/
  325. }
  326. }
  327. void kgdb_disable_hw_debug(struct pt_regs *regs)
  328. {
  329. /* Disable hardware debugging while we are in kgdb */
  330. bfin_write_WPIACTL(bfin_read_WPIACTL() & ~0x1);
  331. CSYNC();
  332. }
  333. void kgdb_post_master_code(struct pt_regs *regs, int eVector, int err_code)
  334. {
  335. /* Master processor is completely in the debugger */
  336. gdb_bf533vector = eVector;
  337. gdb_bf533errcode = err_code;
  338. }
  339. int kgdb_arch_handle_exception(int exceptionVector, int signo,
  340. int err_code, char *remcom_in_buffer,
  341. char *remcom_out_buffer,
  342. struct pt_regs *linux_regs)
  343. {
  344. long addr;
  345. long breakno;
  346. char *ptr;
  347. int newPC;
  348. int wp_status;
  349. int i;
  350. switch (remcom_in_buffer[0]) {
  351. case 'c':
  352. case 's':
  353. if (kgdb_contthread && kgdb_contthread != current) {
  354. strcpy(remcom_out_buffer, "E00");
  355. break;
  356. }
  357. kgdb_contthread = NULL;
  358. /* try to read optional parameter, pc unchanged if no parm */
  359. ptr = &remcom_in_buffer[1];
  360. if (kgdb_hex2long(&ptr, &addr)) {
  361. linux_regs->retx = addr;
  362. }
  363. newPC = linux_regs->retx;
  364. /* clear the trace bit */
  365. linux_regs->syscfg &= 0xfffffffe;
  366. /* set the trace bit if we're stepping */
  367. if (remcom_in_buffer[0] == 's') {
  368. linux_regs->syscfg |= 0x1;
  369. debugger_step = linux_regs->ipend;
  370. debugger_step >>= 6;
  371. for (i = 10; i > 0; i--, debugger_step >>= 1)
  372. if (debugger_step & 1)
  373. break;
  374. /* i indicate event priority of current stopped instruction
  375. * user space instruction is 0, IVG15 is 1, IVTMR is 10.
  376. * debugger_step > 0 means in single step mode
  377. */
  378. debugger_step = i + 1;
  379. } else {
  380. debugger_step = 0;
  381. }
  382. wp_status = bfin_read_WPSTAT();
  383. CSYNC();
  384. if (exceptionVector == VEC_WATCH) {
  385. for (breakno = 0; breakno < 6; ++breakno) {
  386. if (wp_status & (1 << breakno)) {
  387. breakinfo->skip = 1;
  388. break;
  389. }
  390. }
  391. }
  392. kgdb_correct_hw_break();
  393. bfin_write_WPSTAT(0);
  394. return 0;
  395. } /* switch */
  396. return -1; /* this means that we do not want to exit from the handler */
  397. }
  398. struct kgdb_arch arch_kgdb_ops = {
  399. .gdb_bpt_instr = {0xa1},
  400. .flags = KGDB_HW_BREAKPOINT,
  401. };