kgdb.c 19 KB

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