kgdb.c 12 KB

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