kgdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * arch/blackfin/kernel/kgdb.c - Blackfin kgdb pieces
  3. *
  4. * Copyright 2005-2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/ptrace.h> /* for linux pt_regs struct */
  9. #include <linux/kgdb.h>
  10. #include <linux/uaccess.h>
  11. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  12. {
  13. gdb_regs[BFIN_R0] = regs->r0;
  14. gdb_regs[BFIN_R1] = regs->r1;
  15. gdb_regs[BFIN_R2] = regs->r2;
  16. gdb_regs[BFIN_R3] = regs->r3;
  17. gdb_regs[BFIN_R4] = regs->r4;
  18. gdb_regs[BFIN_R5] = regs->r5;
  19. gdb_regs[BFIN_R6] = regs->r6;
  20. gdb_regs[BFIN_R7] = regs->r7;
  21. gdb_regs[BFIN_P0] = regs->p0;
  22. gdb_regs[BFIN_P1] = regs->p1;
  23. gdb_regs[BFIN_P2] = regs->p2;
  24. gdb_regs[BFIN_P3] = regs->p3;
  25. gdb_regs[BFIN_P4] = regs->p4;
  26. gdb_regs[BFIN_P5] = regs->p5;
  27. gdb_regs[BFIN_SP] = regs->reserved;
  28. gdb_regs[BFIN_FP] = regs->fp;
  29. gdb_regs[BFIN_I0] = regs->i0;
  30. gdb_regs[BFIN_I1] = regs->i1;
  31. gdb_regs[BFIN_I2] = regs->i2;
  32. gdb_regs[BFIN_I3] = regs->i3;
  33. gdb_regs[BFIN_M0] = regs->m0;
  34. gdb_regs[BFIN_M1] = regs->m1;
  35. gdb_regs[BFIN_M2] = regs->m2;
  36. gdb_regs[BFIN_M3] = regs->m3;
  37. gdb_regs[BFIN_B0] = regs->b0;
  38. gdb_regs[BFIN_B1] = regs->b1;
  39. gdb_regs[BFIN_B2] = regs->b2;
  40. gdb_regs[BFIN_B3] = regs->b3;
  41. gdb_regs[BFIN_L0] = regs->l0;
  42. gdb_regs[BFIN_L1] = regs->l1;
  43. gdb_regs[BFIN_L2] = regs->l2;
  44. gdb_regs[BFIN_L3] = regs->l3;
  45. gdb_regs[BFIN_A0_DOT_X] = regs->a0x;
  46. gdb_regs[BFIN_A0_DOT_W] = regs->a0w;
  47. gdb_regs[BFIN_A1_DOT_X] = regs->a1x;
  48. gdb_regs[BFIN_A1_DOT_W] = regs->a1w;
  49. gdb_regs[BFIN_ASTAT] = regs->astat;
  50. gdb_regs[BFIN_RETS] = regs->rets;
  51. gdb_regs[BFIN_LC0] = regs->lc0;
  52. gdb_regs[BFIN_LT0] = regs->lt0;
  53. gdb_regs[BFIN_LB0] = regs->lb0;
  54. gdb_regs[BFIN_LC1] = regs->lc1;
  55. gdb_regs[BFIN_LT1] = regs->lt1;
  56. gdb_regs[BFIN_LB1] = regs->lb1;
  57. gdb_regs[BFIN_CYCLES] = 0;
  58. gdb_regs[BFIN_CYCLES2] = 0;
  59. gdb_regs[BFIN_USP] = regs->usp;
  60. gdb_regs[BFIN_SEQSTAT] = regs->seqstat;
  61. gdb_regs[BFIN_SYSCFG] = regs->syscfg;
  62. gdb_regs[BFIN_RETI] = regs->pc;
  63. gdb_regs[BFIN_RETX] = regs->retx;
  64. gdb_regs[BFIN_RETN] = regs->retn;
  65. gdb_regs[BFIN_RETE] = regs->rete;
  66. gdb_regs[BFIN_PC] = regs->pc;
  67. gdb_regs[BFIN_CC] = (regs->astat >> 5) & 1;
  68. gdb_regs[BFIN_EXTRA1] = 0;
  69. gdb_regs[BFIN_EXTRA2] = 0;
  70. gdb_regs[BFIN_EXTRA3] = 0;
  71. gdb_regs[BFIN_IPEND] = regs->ipend;
  72. }
  73. /*
  74. * Extracts ebp, esp and eip values understandable by gdb from the values
  75. * saved by switch_to.
  76. * thread.esp points to ebp. flags and ebp are pushed in switch_to hence esp
  77. * prior to entering switch_to is 8 greater than the value that is saved.
  78. * If switch_to changes, change following code appropriately.
  79. */
  80. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  81. {
  82. gdb_regs[BFIN_SP] = p->thread.ksp;
  83. gdb_regs[BFIN_PC] = p->thread.pc;
  84. gdb_regs[BFIN_SEQSTAT] = p->thread.seqstat;
  85. }
  86. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  87. {
  88. regs->r0 = gdb_regs[BFIN_R0];
  89. regs->r1 = gdb_regs[BFIN_R1];
  90. regs->r2 = gdb_regs[BFIN_R2];
  91. regs->r3 = gdb_regs[BFIN_R3];
  92. regs->r4 = gdb_regs[BFIN_R4];
  93. regs->r5 = gdb_regs[BFIN_R5];
  94. regs->r6 = gdb_regs[BFIN_R6];
  95. regs->r7 = gdb_regs[BFIN_R7];
  96. regs->p0 = gdb_regs[BFIN_P0];
  97. regs->p1 = gdb_regs[BFIN_P1];
  98. regs->p2 = gdb_regs[BFIN_P2];
  99. regs->p3 = gdb_regs[BFIN_P3];
  100. regs->p4 = gdb_regs[BFIN_P4];
  101. regs->p5 = gdb_regs[BFIN_P5];
  102. regs->fp = gdb_regs[BFIN_FP];
  103. regs->i0 = gdb_regs[BFIN_I0];
  104. regs->i1 = gdb_regs[BFIN_I1];
  105. regs->i2 = gdb_regs[BFIN_I2];
  106. regs->i3 = gdb_regs[BFIN_I3];
  107. regs->m0 = gdb_regs[BFIN_M0];
  108. regs->m1 = gdb_regs[BFIN_M1];
  109. regs->m2 = gdb_regs[BFIN_M2];
  110. regs->m3 = gdb_regs[BFIN_M3];
  111. regs->b0 = gdb_regs[BFIN_B0];
  112. regs->b1 = gdb_regs[BFIN_B1];
  113. regs->b2 = gdb_regs[BFIN_B2];
  114. regs->b3 = gdb_regs[BFIN_B3];
  115. regs->l0 = gdb_regs[BFIN_L0];
  116. regs->l1 = gdb_regs[BFIN_L1];
  117. regs->l2 = gdb_regs[BFIN_L2];
  118. regs->l3 = gdb_regs[BFIN_L3];
  119. regs->a0x = gdb_regs[BFIN_A0_DOT_X];
  120. regs->a0w = gdb_regs[BFIN_A0_DOT_W];
  121. regs->a1x = gdb_regs[BFIN_A1_DOT_X];
  122. regs->a1w = gdb_regs[BFIN_A1_DOT_W];
  123. regs->rets = gdb_regs[BFIN_RETS];
  124. regs->lc0 = gdb_regs[BFIN_LC0];
  125. regs->lt0 = gdb_regs[BFIN_LT0];
  126. regs->lb0 = gdb_regs[BFIN_LB0];
  127. regs->lc1 = gdb_regs[BFIN_LC1];
  128. regs->lt1 = gdb_regs[BFIN_LT1];
  129. regs->lb1 = gdb_regs[BFIN_LB1];
  130. regs->usp = gdb_regs[BFIN_USP];
  131. regs->syscfg = gdb_regs[BFIN_SYSCFG];
  132. regs->retx = gdb_regs[BFIN_RETX];
  133. regs->retn = gdb_regs[BFIN_RETN];
  134. regs->rete = gdb_regs[BFIN_RETE];
  135. regs->pc = gdb_regs[BFIN_PC];
  136. #if 0 /* can't change these */
  137. regs->astat = gdb_regs[BFIN_ASTAT];
  138. regs->seqstat = gdb_regs[BFIN_SEQSTAT];
  139. regs->ipend = gdb_regs[BFIN_IPEND];
  140. #endif
  141. }
  142. static struct hw_breakpoint {
  143. unsigned int occupied:1;
  144. unsigned int skip:1;
  145. unsigned int enabled:1;
  146. unsigned int type:1;
  147. unsigned int dataacc:2;
  148. unsigned short count;
  149. unsigned int addr;
  150. } breakinfo[HW_WATCHPOINT_NUM];
  151. static int bfin_set_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  152. {
  153. int breakno;
  154. int bfin_type;
  155. int dataacc = 0;
  156. switch (type) {
  157. case BP_HARDWARE_BREAKPOINT:
  158. bfin_type = TYPE_INST_WATCHPOINT;
  159. break;
  160. case BP_WRITE_WATCHPOINT:
  161. dataacc = 1;
  162. bfin_type = TYPE_DATA_WATCHPOINT;
  163. break;
  164. case BP_READ_WATCHPOINT:
  165. dataacc = 2;
  166. bfin_type = TYPE_DATA_WATCHPOINT;
  167. break;
  168. case BP_ACCESS_WATCHPOINT:
  169. dataacc = 3;
  170. bfin_type = TYPE_DATA_WATCHPOINT;
  171. break;
  172. default:
  173. return -ENOSPC;
  174. }
  175. /* Because hardware data watchpoint impelemented in current
  176. * Blackfin can not trigger an exception event as the hardware
  177. * instrction watchpoint does, we ignaore all data watch point here.
  178. * They can be turned on easily after future blackfin design
  179. * supports this feature.
  180. */
  181. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  182. if (bfin_type == breakinfo[breakno].type
  183. && !breakinfo[breakno].occupied) {
  184. breakinfo[breakno].occupied = 1;
  185. breakinfo[breakno].skip = 0;
  186. breakinfo[breakno].enabled = 1;
  187. breakinfo[breakno].addr = addr;
  188. breakinfo[breakno].dataacc = dataacc;
  189. breakinfo[breakno].count = 0;
  190. return 0;
  191. }
  192. return -ENOSPC;
  193. }
  194. static int bfin_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  195. {
  196. int breakno;
  197. int bfin_type;
  198. switch (type) {
  199. case BP_HARDWARE_BREAKPOINT:
  200. bfin_type = TYPE_INST_WATCHPOINT;
  201. break;
  202. case BP_WRITE_WATCHPOINT:
  203. case BP_READ_WATCHPOINT:
  204. case BP_ACCESS_WATCHPOINT:
  205. bfin_type = TYPE_DATA_WATCHPOINT;
  206. break;
  207. default:
  208. return 0;
  209. }
  210. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  211. if (bfin_type == breakinfo[breakno].type
  212. && breakinfo[breakno].occupied
  213. && breakinfo[breakno].addr == addr) {
  214. breakinfo[breakno].occupied = 0;
  215. breakinfo[breakno].enabled = 0;
  216. }
  217. return 0;
  218. }
  219. static void bfin_remove_all_hw_break(void)
  220. {
  221. int breakno;
  222. memset(breakinfo, 0, sizeof(struct hw_breakpoint)*HW_WATCHPOINT_NUM);
  223. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  224. breakinfo[breakno].type = TYPE_INST_WATCHPOINT;
  225. for (; breakno < HW_WATCHPOINT_NUM; breakno++)
  226. breakinfo[breakno].type = TYPE_DATA_WATCHPOINT;
  227. }
  228. static void bfin_correct_hw_break(void)
  229. {
  230. int breakno;
  231. unsigned int wpiactl = 0;
  232. unsigned int wpdactl = 0;
  233. int enable_wp = 0;
  234. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  235. if (breakinfo[breakno].enabled) {
  236. enable_wp = 1;
  237. switch (breakno) {
  238. case 0:
  239. wpiactl |= WPIAEN0|WPICNTEN0;
  240. bfin_write_WPIA0(breakinfo[breakno].addr);
  241. bfin_write_WPIACNT0(breakinfo[breakno].count
  242. + breakinfo->skip);
  243. break;
  244. case 1:
  245. wpiactl |= WPIAEN1|WPICNTEN1;
  246. bfin_write_WPIA1(breakinfo[breakno].addr);
  247. bfin_write_WPIACNT1(breakinfo[breakno].count
  248. + breakinfo->skip);
  249. break;
  250. case 2:
  251. wpiactl |= WPIAEN2|WPICNTEN2;
  252. bfin_write_WPIA2(breakinfo[breakno].addr);
  253. bfin_write_WPIACNT2(breakinfo[breakno].count
  254. + breakinfo->skip);
  255. break;
  256. case 3:
  257. wpiactl |= WPIAEN3|WPICNTEN3;
  258. bfin_write_WPIA3(breakinfo[breakno].addr);
  259. bfin_write_WPIACNT3(breakinfo[breakno].count
  260. + breakinfo->skip);
  261. break;
  262. case 4:
  263. wpiactl |= WPIAEN4|WPICNTEN4;
  264. bfin_write_WPIA4(breakinfo[breakno].addr);
  265. bfin_write_WPIACNT4(breakinfo[breakno].count
  266. + breakinfo->skip);
  267. break;
  268. case 5:
  269. wpiactl |= WPIAEN5|WPICNTEN5;
  270. bfin_write_WPIA5(breakinfo[breakno].addr);
  271. bfin_write_WPIACNT5(breakinfo[breakno].count
  272. + breakinfo->skip);
  273. break;
  274. case 6:
  275. wpdactl |= WPDAEN0|WPDCNTEN0|WPDSRC0;
  276. wpdactl |= breakinfo[breakno].dataacc
  277. << WPDACC0_OFFSET;
  278. bfin_write_WPDA0(breakinfo[breakno].addr);
  279. bfin_write_WPDACNT0(breakinfo[breakno].count
  280. + breakinfo->skip);
  281. break;
  282. case 7:
  283. wpdactl |= WPDAEN1|WPDCNTEN1|WPDSRC1;
  284. wpdactl |= breakinfo[breakno].dataacc
  285. << WPDACC1_OFFSET;
  286. bfin_write_WPDA1(breakinfo[breakno].addr);
  287. bfin_write_WPDACNT1(breakinfo[breakno].count
  288. + breakinfo->skip);
  289. break;
  290. }
  291. }
  292. /* Should enable WPPWR bit first before set any other
  293. * WPIACTL and WPDACTL bits */
  294. if (enable_wp) {
  295. bfin_write_WPIACTL(WPPWR);
  296. CSYNC();
  297. bfin_write_WPIACTL(wpiactl|WPPWR);
  298. bfin_write_WPDACTL(wpdactl);
  299. CSYNC();
  300. }
  301. }
  302. static void bfin_disable_hw_debug(struct pt_regs *regs)
  303. {
  304. /* Disable hardware debugging while we are in kgdb */
  305. bfin_write_WPIACTL(0);
  306. bfin_write_WPDACTL(0);
  307. CSYNC();
  308. }
  309. #ifdef CONFIG_SMP
  310. void kgdb_passive_cpu_callback(void *info)
  311. {
  312. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  313. }
  314. void kgdb_roundup_cpus(unsigned long flags)
  315. {
  316. smp_call_function(kgdb_passive_cpu_callback, NULL, 0);
  317. }
  318. void kgdb_roundup_cpu(int cpu, unsigned long flags)
  319. {
  320. smp_call_function_single(cpu, kgdb_passive_cpu_callback, NULL, 0);
  321. }
  322. #endif
  323. #ifdef CONFIG_IPIPE
  324. static unsigned long kgdb_arch_imask;
  325. #endif
  326. void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
  327. {
  328. if (kgdb_single_step)
  329. preempt_enable();
  330. #ifdef CONFIG_IPIPE
  331. if (kgdb_arch_imask) {
  332. cpu_pda[raw_smp_processor_id()].ex_imask = kgdb_arch_imask;
  333. kgdb_arch_imask = 0;
  334. }
  335. #endif
  336. }
  337. int kgdb_arch_handle_exception(int vector, int signo,
  338. int err_code, char *remcom_in_buffer,
  339. char *remcom_out_buffer,
  340. struct pt_regs *regs)
  341. {
  342. long addr;
  343. char *ptr;
  344. int newPC;
  345. int i;
  346. switch (remcom_in_buffer[0]) {
  347. case 'c':
  348. case 's':
  349. if (kgdb_contthread && kgdb_contthread != current) {
  350. strcpy(remcom_out_buffer, "E00");
  351. break;
  352. }
  353. kgdb_contthread = NULL;
  354. /* try to read optional parameter, pc unchanged if no parm */
  355. ptr = &remcom_in_buffer[1];
  356. if (kgdb_hex2long(&ptr, &addr)) {
  357. regs->retx = addr;
  358. }
  359. newPC = regs->retx;
  360. /* clear the trace bit */
  361. regs->syscfg &= 0xfffffffe;
  362. /* set the trace bit if we're stepping */
  363. if (remcom_in_buffer[0] == 's') {
  364. regs->syscfg |= 0x1;
  365. kgdb_single_step = regs->ipend;
  366. kgdb_single_step >>= 6;
  367. for (i = 10; i > 0; i--, kgdb_single_step >>= 1)
  368. if (kgdb_single_step & 1)
  369. break;
  370. /* i indicate event priority of current stopped instruction
  371. * user space instruction is 0, IVG15 is 1, IVTMR is 10.
  372. * kgdb_single_step > 0 means in single step mode
  373. */
  374. kgdb_single_step = i + 1;
  375. preempt_disable();
  376. #ifdef CONFIG_IPIPE
  377. kgdb_arch_imask = cpu_pda[raw_smp_processor_id()].ex_imask;
  378. cpu_pda[raw_smp_processor_id()].ex_imask = 0;
  379. #endif
  380. }
  381. bfin_correct_hw_break();
  382. return 0;
  383. } /* switch */
  384. return -1; /* this means that we do not want to exit from the handler */
  385. }
  386. struct kgdb_arch arch_kgdb_ops = {
  387. .gdb_bpt_instr = {0xa1},
  388. .flags = KGDB_HW_BREAKPOINT,
  389. .set_hw_breakpoint = bfin_set_hw_break,
  390. .remove_hw_breakpoint = bfin_remove_hw_break,
  391. .disable_hw_break = bfin_disable_hw_debug,
  392. .remove_all_hw_break = bfin_remove_all_hw_break,
  393. .correct_hw_break = bfin_correct_hw_break,
  394. };
  395. #define IN_MEM(addr, size, l1_addr, l1_size) \
  396. ({ \
  397. unsigned long __addr = (unsigned long)(addr); \
  398. (l1_size && __addr >= l1_addr && __addr + (size) <= l1_addr + l1_size); \
  399. })
  400. #define ASYNC_BANK_SIZE \
  401. (ASYNC_BANK0_SIZE + ASYNC_BANK1_SIZE + \
  402. ASYNC_BANK2_SIZE + ASYNC_BANK3_SIZE)
  403. int kgdb_validate_break_address(unsigned long addr)
  404. {
  405. int cpu = raw_smp_processor_id();
  406. if (addr >= 0x1000 && (addr + BREAK_INSTR_SIZE) <= physical_mem_end)
  407. return 0;
  408. if (IN_MEM(addr, BREAK_INSTR_SIZE, ASYNC_BANK0_BASE, ASYNC_BANK_SIZE))
  409. return 0;
  410. if (cpu == 0 && IN_MEM(addr, BREAK_INSTR_SIZE, L1_CODE_START, L1_CODE_LENGTH))
  411. return 0;
  412. #ifdef CONFIG_SMP
  413. else if (cpu == 1 && IN_MEM(addr, BREAK_INSTR_SIZE, COREB_L1_CODE_START, L1_CODE_LENGTH))
  414. return 0;
  415. #endif
  416. if (IN_MEM(addr, BREAK_INSTR_SIZE, L2_START, L2_LENGTH))
  417. return 0;
  418. return -EFAULT;
  419. }
  420. void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
  421. {
  422. regs->retx = ip;
  423. }
  424. int kgdb_arch_init(void)
  425. {
  426. kgdb_single_step = 0;
  427. #ifdef CONFIG_IPIPE
  428. kgdb_arch_imask = 0;
  429. #endif
  430. bfin_remove_all_hw_break();
  431. return 0;
  432. }
  433. void kgdb_arch_exit(void)
  434. {
  435. }