kgdb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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/string.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/smp.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/delay.h>
  14. #include <linux/ptrace.h> /* for linux pt_regs struct */
  15. #include <linux/kgdb.h>
  16. #include <linux/console.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/irq.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/system.h>
  22. #include <asm/traps.h>
  23. #include <asm/blackfin.h>
  24. #include <asm/dma.h>
  25. /* Put the error code here just in case the user cares. */
  26. int gdb_bfin_errcode;
  27. /* Likewise, the vector number here (since GDB only gets the signal
  28. number through the usual means, and that's not very specific). */
  29. int gdb_bfin_vector = -1;
  30. #if KGDB_MAX_NO_CPUS != 8
  31. #error change the definition of slavecpulocks
  32. #endif
  33. #ifdef CONFIG_BFIN_WDT
  34. # error "Please unselect blackfin watchdog driver before build KGDB."
  35. #endif
  36. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  37. {
  38. gdb_regs[BFIN_R0] = regs->r0;
  39. gdb_regs[BFIN_R1] = regs->r1;
  40. gdb_regs[BFIN_R2] = regs->r2;
  41. gdb_regs[BFIN_R3] = regs->r3;
  42. gdb_regs[BFIN_R4] = regs->r4;
  43. gdb_regs[BFIN_R5] = regs->r5;
  44. gdb_regs[BFIN_R6] = regs->r6;
  45. gdb_regs[BFIN_R7] = regs->r7;
  46. gdb_regs[BFIN_P0] = regs->p0;
  47. gdb_regs[BFIN_P1] = regs->p1;
  48. gdb_regs[BFIN_P2] = regs->p2;
  49. gdb_regs[BFIN_P3] = regs->p3;
  50. gdb_regs[BFIN_P4] = regs->p4;
  51. gdb_regs[BFIN_P5] = regs->p5;
  52. gdb_regs[BFIN_SP] = regs->reserved;
  53. gdb_regs[BFIN_FP] = regs->fp;
  54. gdb_regs[BFIN_I0] = regs->i0;
  55. gdb_regs[BFIN_I1] = regs->i1;
  56. gdb_regs[BFIN_I2] = regs->i2;
  57. gdb_regs[BFIN_I3] = regs->i3;
  58. gdb_regs[BFIN_M0] = regs->m0;
  59. gdb_regs[BFIN_M1] = regs->m1;
  60. gdb_regs[BFIN_M2] = regs->m2;
  61. gdb_regs[BFIN_M3] = regs->m3;
  62. gdb_regs[BFIN_B0] = regs->b0;
  63. gdb_regs[BFIN_B1] = regs->b1;
  64. gdb_regs[BFIN_B2] = regs->b2;
  65. gdb_regs[BFIN_B3] = regs->b3;
  66. gdb_regs[BFIN_L0] = regs->l0;
  67. gdb_regs[BFIN_L1] = regs->l1;
  68. gdb_regs[BFIN_L2] = regs->l2;
  69. gdb_regs[BFIN_L3] = regs->l3;
  70. gdb_regs[BFIN_A0_DOT_X] = regs->a0x;
  71. gdb_regs[BFIN_A0_DOT_W] = regs->a0w;
  72. gdb_regs[BFIN_A1_DOT_X] = regs->a1x;
  73. gdb_regs[BFIN_A1_DOT_W] = regs->a1w;
  74. gdb_regs[BFIN_ASTAT] = regs->astat;
  75. gdb_regs[BFIN_RETS] = regs->rets;
  76. gdb_regs[BFIN_LC0] = regs->lc0;
  77. gdb_regs[BFIN_LT0] = regs->lt0;
  78. gdb_regs[BFIN_LB0] = regs->lb0;
  79. gdb_regs[BFIN_LC1] = regs->lc1;
  80. gdb_regs[BFIN_LT1] = regs->lt1;
  81. gdb_regs[BFIN_LB1] = regs->lb1;
  82. gdb_regs[BFIN_CYCLES] = 0;
  83. gdb_regs[BFIN_CYCLES2] = 0;
  84. gdb_regs[BFIN_USP] = regs->usp;
  85. gdb_regs[BFIN_SEQSTAT] = regs->seqstat;
  86. gdb_regs[BFIN_SYSCFG] = regs->syscfg;
  87. gdb_regs[BFIN_RETI] = regs->pc;
  88. gdb_regs[BFIN_RETX] = regs->retx;
  89. gdb_regs[BFIN_RETN] = regs->retn;
  90. gdb_regs[BFIN_RETE] = regs->rete;
  91. gdb_regs[BFIN_PC] = regs->pc;
  92. gdb_regs[BFIN_CC] = 0;
  93. gdb_regs[BFIN_EXTRA1] = 0;
  94. gdb_regs[BFIN_EXTRA2] = 0;
  95. gdb_regs[BFIN_EXTRA3] = 0;
  96. gdb_regs[BFIN_IPEND] = regs->ipend;
  97. }
  98. /*
  99. * Extracts ebp, esp and eip values understandable by gdb from the values
  100. * saved by switch_to.
  101. * thread.esp points to ebp. flags and ebp are pushed in switch_to hence esp
  102. * prior to entering switch_to is 8 greater then the value that is saved.
  103. * If switch_to changes, change following code appropriately.
  104. */
  105. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  106. {
  107. gdb_regs[BFIN_SP] = p->thread.ksp;
  108. gdb_regs[BFIN_PC] = p->thread.pc;
  109. gdb_regs[BFIN_SEQSTAT] = p->thread.seqstat;
  110. }
  111. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  112. {
  113. regs->r0 = gdb_regs[BFIN_R0];
  114. regs->r1 = gdb_regs[BFIN_R1];
  115. regs->r2 = gdb_regs[BFIN_R2];
  116. regs->r3 = gdb_regs[BFIN_R3];
  117. regs->r4 = gdb_regs[BFIN_R4];
  118. regs->r5 = gdb_regs[BFIN_R5];
  119. regs->r6 = gdb_regs[BFIN_R6];
  120. regs->r7 = gdb_regs[BFIN_R7];
  121. regs->p0 = gdb_regs[BFIN_P0];
  122. regs->p1 = gdb_regs[BFIN_P1];
  123. regs->p2 = gdb_regs[BFIN_P2];
  124. regs->p3 = gdb_regs[BFIN_P3];
  125. regs->p4 = gdb_regs[BFIN_P4];
  126. regs->p5 = gdb_regs[BFIN_P5];
  127. regs->fp = gdb_regs[BFIN_FP];
  128. regs->i0 = gdb_regs[BFIN_I0];
  129. regs->i1 = gdb_regs[BFIN_I1];
  130. regs->i2 = gdb_regs[BFIN_I2];
  131. regs->i3 = gdb_regs[BFIN_I3];
  132. regs->m0 = gdb_regs[BFIN_M0];
  133. regs->m1 = gdb_regs[BFIN_M1];
  134. regs->m2 = gdb_regs[BFIN_M2];
  135. regs->m3 = gdb_regs[BFIN_M3];
  136. regs->b0 = gdb_regs[BFIN_B0];
  137. regs->b1 = gdb_regs[BFIN_B1];
  138. regs->b2 = gdb_regs[BFIN_B2];
  139. regs->b3 = gdb_regs[BFIN_B3];
  140. regs->l0 = gdb_regs[BFIN_L0];
  141. regs->l1 = gdb_regs[BFIN_L1];
  142. regs->l2 = gdb_regs[BFIN_L2];
  143. regs->l3 = gdb_regs[BFIN_L3];
  144. regs->a0x = gdb_regs[BFIN_A0_DOT_X];
  145. regs->a0w = gdb_regs[BFIN_A0_DOT_W];
  146. regs->a1x = gdb_regs[BFIN_A1_DOT_X];
  147. regs->a1w = gdb_regs[BFIN_A1_DOT_W];
  148. regs->rets = gdb_regs[BFIN_RETS];
  149. regs->lc0 = gdb_regs[BFIN_LC0];
  150. regs->lt0 = gdb_regs[BFIN_LT0];
  151. regs->lb0 = gdb_regs[BFIN_LB0];
  152. regs->lc1 = gdb_regs[BFIN_LC1];
  153. regs->lt1 = gdb_regs[BFIN_LT1];
  154. regs->lb1 = gdb_regs[BFIN_LB1];
  155. regs->usp = gdb_regs[BFIN_USP];
  156. regs->syscfg = gdb_regs[BFIN_SYSCFG];
  157. regs->retx = gdb_regs[BFIN_PC];
  158. regs->retn = gdb_regs[BFIN_RETN];
  159. regs->rete = gdb_regs[BFIN_RETE];
  160. regs->pc = gdb_regs[BFIN_PC];
  161. #if 0 /* can't change these */
  162. regs->astat = gdb_regs[BFIN_ASTAT];
  163. regs->seqstat = gdb_regs[BFIN_SEQSTAT];
  164. regs->ipend = gdb_regs[BFIN_IPEND];
  165. #endif
  166. }
  167. struct hw_breakpoint {
  168. unsigned int occupied:1;
  169. unsigned int skip:1;
  170. unsigned int enabled:1;
  171. unsigned int type:1;
  172. unsigned int dataacc:2;
  173. unsigned short count;
  174. unsigned int addr;
  175. } breakinfo[HW_WATCHPOINT_NUM];
  176. int bfin_set_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  177. {
  178. int breakno;
  179. int bfin_type;
  180. int dataacc = 0;
  181. switch (type) {
  182. case BP_HARDWARE_BREAKPOINT:
  183. bfin_type = TYPE_INST_WATCHPOINT;
  184. break;
  185. case BP_WRITE_WATCHPOINT:
  186. dataacc = 1;
  187. bfin_type = TYPE_DATA_WATCHPOINT;
  188. break;
  189. case BP_READ_WATCHPOINT:
  190. dataacc = 2;
  191. bfin_type = TYPE_DATA_WATCHPOINT;
  192. break;
  193. case BP_ACCESS_WATCHPOINT:
  194. dataacc = 3;
  195. bfin_type = TYPE_DATA_WATCHPOINT;
  196. break;
  197. default:
  198. return -ENOSPC;
  199. }
  200. /* Becasue hardware data watchpoint impelemented in current
  201. * Blackfin can not trigger an exception event as the hardware
  202. * instrction watchpoint does, we ignaore all data watch point here.
  203. * They can be turned on easily after future blackfin design
  204. * supports this feature.
  205. */
  206. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  207. if (bfin_type == breakinfo[breakno].type
  208. && !breakinfo[breakno].occupied) {
  209. breakinfo[breakno].occupied = 1;
  210. breakinfo[breakno].enabled = 1;
  211. breakinfo[breakno].addr = addr;
  212. breakinfo[breakno].dataacc = dataacc;
  213. breakinfo[breakno].count = 0;
  214. return 0;
  215. }
  216. return -ENOSPC;
  217. }
  218. int bfin_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  219. {
  220. int breakno;
  221. int bfin_type;
  222. switch (type) {
  223. case BP_HARDWARE_BREAKPOINT:
  224. bfin_type = TYPE_INST_WATCHPOINT;
  225. break;
  226. case BP_WRITE_WATCHPOINT:
  227. case BP_READ_WATCHPOINT:
  228. case BP_ACCESS_WATCHPOINT:
  229. bfin_type = TYPE_DATA_WATCHPOINT;
  230. break;
  231. default:
  232. return 0;
  233. }
  234. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  235. if (bfin_type == breakinfo[breakno].type
  236. && breakinfo[breakno].occupied
  237. && breakinfo[breakno].addr == addr) {
  238. breakinfo[breakno].occupied = 0;
  239. breakinfo[breakno].enabled = 0;
  240. }
  241. return 0;
  242. }
  243. void bfin_remove_all_hw_break(void)
  244. {
  245. int breakno;
  246. memset(breakinfo, 0, sizeof(struct hw_breakpoint)*HW_WATCHPOINT_NUM);
  247. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  248. breakinfo[breakno].type = TYPE_INST_WATCHPOINT;
  249. for (; breakno < HW_WATCHPOINT_NUM; breakno++)
  250. breakinfo[breakno].type = TYPE_DATA_WATCHPOINT;
  251. }
  252. void bfin_correct_hw_break(void)
  253. {
  254. int breakno;
  255. unsigned int wpiactl = 0;
  256. unsigned int wpdactl = 0;
  257. int enable_wp = 0;
  258. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  259. if (breakinfo[breakno].enabled) {
  260. enable_wp = 1;
  261. switch (breakno) {
  262. case 0:
  263. wpiactl |= WPIAEN0|WPICNTEN0;
  264. bfin_write_WPIA0(breakinfo[breakno].addr);
  265. bfin_write_WPIACNT0(breakinfo[breakno].count
  266. + breakinfo->skip);
  267. break;
  268. case 1:
  269. wpiactl |= WPIAEN1|WPICNTEN1;
  270. bfin_write_WPIA1(breakinfo[breakno].addr);
  271. bfin_write_WPIACNT1(breakinfo[breakno].count
  272. + breakinfo->skip);
  273. break;
  274. case 2:
  275. wpiactl |= WPIAEN2|WPICNTEN2;
  276. bfin_write_WPIA2(breakinfo[breakno].addr);
  277. bfin_write_WPIACNT2(breakinfo[breakno].count
  278. + breakinfo->skip);
  279. break;
  280. case 3:
  281. wpiactl |= WPIAEN3|WPICNTEN3;
  282. bfin_write_WPIA3(breakinfo[breakno].addr);
  283. bfin_write_WPIACNT3(breakinfo[breakno].count
  284. + breakinfo->skip);
  285. break;
  286. case 4:
  287. wpiactl |= WPIAEN4|WPICNTEN4;
  288. bfin_write_WPIA4(breakinfo[breakno].addr);
  289. bfin_write_WPIACNT4(breakinfo[breakno].count
  290. + breakinfo->skip);
  291. break;
  292. case 5:
  293. wpiactl |= WPIAEN5|WPICNTEN5;
  294. bfin_write_WPIA5(breakinfo[breakno].addr);
  295. bfin_write_WPIACNT5(breakinfo[breakno].count
  296. + breakinfo->skip);
  297. break;
  298. case 6:
  299. wpdactl |= WPDAEN0|WPDCNTEN0|WPDSRC0;
  300. wpdactl |= breakinfo[breakno].dataacc
  301. << WPDACC0_OFFSET;
  302. bfin_write_WPDA0(breakinfo[breakno].addr);
  303. bfin_write_WPDACNT0(breakinfo[breakno].count
  304. + breakinfo->skip);
  305. break;
  306. case 7:
  307. wpdactl |= WPDAEN1|WPDCNTEN1|WPDSRC1;
  308. wpdactl |= breakinfo[breakno].dataacc
  309. << WPDACC1_OFFSET;
  310. bfin_write_WPDA1(breakinfo[breakno].addr);
  311. bfin_write_WPDACNT1(breakinfo[breakno].count
  312. + breakinfo->skip);
  313. break;
  314. }
  315. }
  316. /* Should enable WPPWR bit first before set any other
  317. * WPIACTL and WPDACTL bits */
  318. if (enable_wp) {
  319. bfin_write_WPIACTL(WPPWR);
  320. CSYNC();
  321. bfin_write_WPIACTL(wpiactl|WPPWR);
  322. bfin_write_WPDACTL(wpdactl);
  323. CSYNC();
  324. }
  325. }
  326. void kgdb_disable_hw_debug(struct pt_regs *regs)
  327. {
  328. /* Disable hardware debugging while we are in kgdb */
  329. bfin_write_WPIACTL(0);
  330. bfin_write_WPDACTL(0);
  331. CSYNC();
  332. }
  333. #ifdef CONFIG_SMP
  334. void kgdb_passive_cpu_callback(void *info)
  335. {
  336. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  337. }
  338. void kgdb_roundup_cpus(unsigned long flags)
  339. {
  340. smp_call_function(kgdb_passive_cpu_callback, NULL, 0, 0);
  341. }
  342. void kgdb_roundup_cpu(int cpu, unsigned long flags)
  343. {
  344. smp_call_function_single(cpu, kgdb_passive_cpu_callback, NULL, 0, 0);
  345. }
  346. #endif
  347. void kgdb_post_primary_code(struct pt_regs *regs, int eVector, int err_code)
  348. {
  349. /* Master processor is completely in the debugger */
  350. gdb_bfin_vector = eVector;
  351. gdb_bfin_errcode = err_code;
  352. }
  353. int kgdb_arch_handle_exception(int vector, int signo,
  354. int err_code, char *remcom_in_buffer,
  355. char *remcom_out_buffer,
  356. struct pt_regs *regs)
  357. {
  358. long addr;
  359. long breakno;
  360. char *ptr;
  361. int newPC;
  362. int wp_status;
  363. int i;
  364. switch (remcom_in_buffer[0]) {
  365. case 'c':
  366. case 's':
  367. if (kgdb_contthread && kgdb_contthread != current) {
  368. strcpy(remcom_out_buffer, "E00");
  369. break;
  370. }
  371. kgdb_contthread = NULL;
  372. /* try to read optional parameter, pc unchanged if no parm */
  373. ptr = &remcom_in_buffer[1];
  374. if (kgdb_hex2long(&ptr, &addr)) {
  375. regs->retx = addr;
  376. }
  377. newPC = regs->retx;
  378. /* clear the trace bit */
  379. regs->syscfg &= 0xfffffffe;
  380. /* set the trace bit if we're stepping */
  381. if (remcom_in_buffer[0] == 's') {
  382. regs->syscfg |= 0x1;
  383. kgdb_single_step = regs->ipend;
  384. kgdb_single_step >>= 6;
  385. for (i = 10; i > 0; i--, kgdb_single_step >>= 1)
  386. if (kgdb_single_step & 1)
  387. break;
  388. /* i indicate event priority of current stopped instruction
  389. * user space instruction is 0, IVG15 is 1, IVTMR is 10.
  390. * kgdb_single_step > 0 means in single step mode
  391. */
  392. kgdb_single_step = i + 1;
  393. }
  394. if (vector == VEC_WATCH) {
  395. wp_status = bfin_read_WPSTAT();
  396. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++) {
  397. if (wp_status & (1 << breakno)) {
  398. breakinfo->skip = 1;
  399. break;
  400. }
  401. }
  402. bfin_write_WPSTAT(0);
  403. }
  404. bfin_correct_hw_break();
  405. return 0;
  406. } /* switch */
  407. return -1; /* this means that we do not want to exit from the handler */
  408. }
  409. struct kgdb_arch arch_kgdb_ops = {
  410. .gdb_bpt_instr = {0xa1},
  411. #ifdef CONFIG_SMP
  412. .flags = KGDB_HW_BREAKPOINT|KGDB_THR_PROC_SWAP,
  413. #else
  414. .flags = KGDB_HW_BREAKPOINT,
  415. #endif
  416. .set_hw_breakpoint = bfin_set_hw_break,
  417. .remove_hw_breakpoint = bfin_remove_hw_break,
  418. .remove_all_hw_break = bfin_remove_all_hw_break,
  419. .correct_hw_break = bfin_correct_hw_break,
  420. };
  421. static int hex(char ch)
  422. {
  423. if ((ch >= 'a') && (ch <= 'f'))
  424. return ch - 'a' + 10;
  425. if ((ch >= '0') && (ch <= '9'))
  426. return ch - '0';
  427. if ((ch >= 'A') && (ch <= 'F'))
  428. return ch - 'A' + 10;
  429. return -1;
  430. }
  431. static int validate_memory_access_address(unsigned long addr, int size)
  432. {
  433. int cpu = raw_smp_processor_id();
  434. if (size < 0)
  435. return EFAULT;
  436. if (addr >= 0x1000 && (addr + size) <= physical_mem_end)
  437. return 0;
  438. if (addr >= SYSMMR_BASE)
  439. return 0;
  440. if (addr >= ASYNC_BANK0_BASE
  441. && addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
  442. return 0;
  443. if (cpu == 0) {
  444. if (addr >= L1_SCRATCH_START
  445. && (addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH))
  446. return 0;
  447. #if L1_CODE_LENGTH != 0
  448. if (addr >= L1_CODE_START
  449. && (addr + size <= L1_CODE_START + L1_CODE_LENGTH))
  450. return 0;
  451. #endif
  452. #if L1_DATA_A_LENGTH != 0
  453. if (addr >= L1_DATA_A_START
  454. && (addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH))
  455. return 0;
  456. #endif
  457. #if L1_DATA_B_LENGTH != 0
  458. if (addr >= L1_DATA_B_START
  459. && (addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH))
  460. return 0;
  461. #endif
  462. #ifdef CONFIG_SMP
  463. } else if (cpu == 1) {
  464. if (addr >= COREB_L1_SCRATCH_START
  465. && (addr + size <= COREB_L1_SCRATCH_START
  466. + L1_SCRATCH_LENGTH))
  467. return 0;
  468. # if L1_CODE_LENGTH != 0
  469. if (addr >= COREB_L1_CODE_START
  470. && (addr + size <= COREB_L1_CODE_START + L1_CODE_LENGTH))
  471. return 0;
  472. # endif
  473. # if L1_DATA_A_LENGTH != 0
  474. if (addr >= COREB_L1_DATA_A_START
  475. && (addr + size <= COREB_L1_DATA_A_START + L1_DATA_A_LENGTH))
  476. return 0;
  477. # endif
  478. # if L1_DATA_B_LENGTH != 0
  479. if (addr >= COREB_L1_DATA_B_START
  480. && (addr + size <= COREB_L1_DATA_B_START + L1_DATA_B_LENGTH))
  481. return 0;
  482. # endif
  483. #endif
  484. }
  485. #if L2_LENGTH != 0
  486. if (addr >= L2_START
  487. && addr + size <= L2_START + L2_LENGTH)
  488. return 0;
  489. #endif
  490. return EFAULT;
  491. }
  492. /*
  493. * Convert the memory pointed to by mem into hex, placing result in buf.
  494. * Return a pointer to the last char put in buf (null). May return an error.
  495. */
  496. int kgdb_mem2hex(char *mem, char *buf, int count)
  497. {
  498. char *tmp;
  499. int err = 0;
  500. unsigned char *pch;
  501. unsigned short mmr16;
  502. unsigned long mmr32;
  503. int cpu = raw_smp_processor_id();
  504. if (validate_memory_access_address((unsigned long)mem, count))
  505. return EFAULT;
  506. /*
  507. * We use the upper half of buf as an intermediate buffer for the
  508. * raw memory copy. Hex conversion will work against this one.
  509. */
  510. tmp = buf + count;
  511. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  512. switch (count) {
  513. case 2:
  514. if ((unsigned int)mem % 2 == 0) {
  515. mmr16 = *(unsigned short *)mem;
  516. pch = (unsigned char *)&mmr16;
  517. *tmp++ = *pch++;
  518. *tmp++ = *pch++;
  519. tmp -= 2;
  520. } else
  521. err = EFAULT;
  522. break;
  523. case 4:
  524. if ((unsigned int)mem % 4 == 0) {
  525. mmr32 = *(unsigned long *)mem;
  526. pch = (unsigned char *)&mmr32;
  527. *tmp++ = *pch++;
  528. *tmp++ = *pch++;
  529. *tmp++ = *pch++;
  530. *tmp++ = *pch++;
  531. tmp -= 4;
  532. } else
  533. err = EFAULT;
  534. break;
  535. default:
  536. err = EFAULT;
  537. }
  538. } else if (cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  539. (unsigned int)(mem + count) <= L1_CODE_START + L1_CODE_LENGTH
  540. #ifdef CONFIG_SMP
  541. || cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  542. (unsigned int)(mem + count) <=
  543. COREB_L1_CODE_START + L1_CODE_LENGTH
  544. #endif
  545. ) {
  546. /* access L1 instruction SRAM*/
  547. if (dma_memcpy(tmp, mem, count) == NULL)
  548. err = EFAULT;
  549. } else
  550. err = probe_kernel_read(tmp, mem, count);
  551. if (!err) {
  552. while (count > 0) {
  553. buf = pack_hex_byte(buf, *tmp);
  554. tmp++;
  555. count--;
  556. }
  557. *buf = 0;
  558. }
  559. return err;
  560. }
  561. /*
  562. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  563. * 0x7d escaped with 0x7d. Return a pointer to the character after
  564. * the last byte written.
  565. */
  566. int kgdb_ebin2mem(char *buf, char *mem, int count)
  567. {
  568. char *tmp_old;
  569. char *tmp_new;
  570. unsigned short *mmr16;
  571. unsigned long *mmr32;
  572. int err = 0;
  573. int size = 0;
  574. int cpu = raw_smp_processor_id();
  575. tmp_old = tmp_new = buf;
  576. while (count-- > 0) {
  577. if (*tmp_old == 0x7d)
  578. *tmp_new = *(++tmp_old) ^ 0x20;
  579. else
  580. *tmp_new = *tmp_old;
  581. tmp_new++;
  582. tmp_old++;
  583. size++;
  584. }
  585. if (validate_memory_access_address((unsigned long)mem, size))
  586. return EFAULT;
  587. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  588. switch (size) {
  589. case 2:
  590. if ((unsigned int)mem % 2 == 0) {
  591. mmr16 = (unsigned short *)buf;
  592. *(unsigned short *)mem = *mmr16;
  593. } else
  594. return EFAULT;
  595. break;
  596. case 4:
  597. if ((unsigned int)mem % 4 == 0) {
  598. mmr32 = (unsigned long *)buf;
  599. *(unsigned long *)mem = *mmr32;
  600. } else
  601. return EFAULT;
  602. break;
  603. default:
  604. return EFAULT;
  605. }
  606. } else if (cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  607. (unsigned int)(mem + count) < L1_CODE_START + L1_CODE_LENGTH
  608. #ifdef CONFIG_SMP
  609. || cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  610. (unsigned int)(mem + count) <=
  611. COREB_L1_CODE_START + L1_CODE_LENGTH
  612. #endif
  613. ) {
  614. /* access L1 instruction SRAM */
  615. if (dma_memcpy(mem, buf, size) == NULL)
  616. err = EFAULT;
  617. } else
  618. err = probe_kernel_write(mem, buf, size);
  619. return err;
  620. }
  621. /*
  622. * Convert the hex array pointed to by buf into binary to be placed in mem.
  623. * Return a pointer to the character AFTER the last byte written.
  624. * May return an error.
  625. */
  626. int kgdb_hex2mem(char *buf, char *mem, int count)
  627. {
  628. char *tmp_raw;
  629. char *tmp_hex;
  630. unsigned short *mmr16;
  631. unsigned long *mmr32;
  632. int cpu = raw_smp_processor_id();
  633. if (validate_memory_access_address((unsigned long)mem, count))
  634. return EFAULT;
  635. /*
  636. * We use the upper half of buf as an intermediate buffer for the
  637. * raw memory that is converted from hex.
  638. */
  639. tmp_raw = buf + count * 2;
  640. tmp_hex = tmp_raw - 1;
  641. while (tmp_hex >= buf) {
  642. tmp_raw--;
  643. *tmp_raw = hex(*tmp_hex--);
  644. *tmp_raw |= hex(*tmp_hex--) << 4;
  645. }
  646. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  647. switch (count) {
  648. case 2:
  649. if ((unsigned int)mem % 2 == 0) {
  650. mmr16 = (unsigned short *)tmp_raw;
  651. *(unsigned short *)mem = *mmr16;
  652. } else
  653. return EFAULT;
  654. break;
  655. case 4:
  656. if ((unsigned int)mem % 4 == 0) {
  657. mmr32 = (unsigned long *)tmp_raw;
  658. *(unsigned long *)mem = *mmr32;
  659. } else
  660. return EFAULT;
  661. break;
  662. default:
  663. return EFAULT;
  664. }
  665. } else if (cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  666. (unsigned int)(mem + count) <= L1_CODE_START + L1_CODE_LENGTH
  667. #ifdef CONFIG_SMP
  668. || cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  669. (unsigned int)(mem + count) <=
  670. COREB_L1_CODE_START + L1_CODE_LENGTH
  671. #endif
  672. ) {
  673. /* access L1 instruction SRAM */
  674. if (dma_memcpy(mem, tmp_raw, count) == NULL)
  675. return EFAULT;
  676. } else
  677. return probe_kernel_write(mem, tmp_raw, count);
  678. return 0;
  679. }
  680. int kgdb_validate_break_address(unsigned long addr)
  681. {
  682. int cpu = raw_smp_processor_id();
  683. if (addr >= 0x1000 && (addr + BREAK_INSTR_SIZE) <= physical_mem_end)
  684. return 0;
  685. if (addr >= ASYNC_BANK0_BASE
  686. && addr + BREAK_INSTR_SIZE <= ASYNC_BANK3_BASE + ASYNC_BANK3_BASE)
  687. return 0;
  688. #if L1_CODE_LENGTH != 0
  689. if (cpu == 0 && addr >= L1_CODE_START
  690. && addr + BREAK_INSTR_SIZE <= L1_CODE_START + L1_CODE_LENGTH)
  691. return 0;
  692. # ifdef CONFIG_SMP
  693. else if (cpu == 1 && addr >= COREB_L1_CODE_START
  694. && addr + BREAK_INSTR_SIZE <= COREB_L1_CODE_START + L1_CODE_LENGTH)
  695. return 0;
  696. # endif
  697. #endif
  698. #if L2_LENGTH != 0
  699. if (addr >= L2_START
  700. && addr + BREAK_INSTR_SIZE <= L2_START + L2_LENGTH)
  701. return 0;
  702. #endif
  703. return EFAULT;
  704. }
  705. int kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
  706. {
  707. int err;
  708. int cpu = raw_smp_processor_id();
  709. if ((cpu == 0 && (unsigned int)addr >= L1_CODE_START
  710. && (unsigned int)(addr + BREAK_INSTR_SIZE)
  711. < L1_CODE_START + L1_CODE_LENGTH)
  712. #ifdef CONFIG_SMP
  713. || (cpu == 1 && (unsigned int)addr >= COREB_L1_CODE_START
  714. && (unsigned int)(addr + BREAK_INSTR_SIZE)
  715. < COREB_L1_CODE_START + L1_CODE_LENGTH)
  716. #endif
  717. ) {
  718. /* access L1 instruction SRAM */
  719. if (dma_memcpy(saved_instr, (void *)addr, BREAK_INSTR_SIZE)
  720. == NULL)
  721. return -EFAULT;
  722. if (dma_memcpy((void *)addr, arch_kgdb_ops.gdb_bpt_instr,
  723. BREAK_INSTR_SIZE) == NULL)
  724. return -EFAULT;
  725. return 0;
  726. } else {
  727. err = probe_kernel_read(saved_instr, (char *)addr,
  728. BREAK_INSTR_SIZE);
  729. if (err)
  730. return err;
  731. return probe_kernel_write((char *)addr,
  732. arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
  733. }
  734. }
  735. int kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
  736. {
  737. if ((unsigned int)addr >= L1_CODE_START &&
  738. (unsigned int)(addr + BREAK_INSTR_SIZE) <
  739. L1_CODE_START + L1_CODE_LENGTH) {
  740. /* access L1 instruction SRAM */
  741. if (dma_memcpy((void *)addr, bundle, BREAK_INSTR_SIZE) == NULL)
  742. return -EFAULT;
  743. return 0;
  744. } else
  745. return probe_kernel_write((char *)addr,
  746. (char *)bundle, BREAK_INSTR_SIZE);
  747. }
  748. int kgdb_arch_init(void)
  749. {
  750. kgdb_single_step = 0;
  751. bfin_remove_all_hw_break();
  752. return 0;
  753. }
  754. void kgdb_arch_exit(void)
  755. {
  756. }