kgdb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  26. {
  27. gdb_regs[BFIN_R0] = regs->r0;
  28. gdb_regs[BFIN_R1] = regs->r1;
  29. gdb_regs[BFIN_R2] = regs->r2;
  30. gdb_regs[BFIN_R3] = regs->r3;
  31. gdb_regs[BFIN_R4] = regs->r4;
  32. gdb_regs[BFIN_R5] = regs->r5;
  33. gdb_regs[BFIN_R6] = regs->r6;
  34. gdb_regs[BFIN_R7] = regs->r7;
  35. gdb_regs[BFIN_P0] = regs->p0;
  36. gdb_regs[BFIN_P1] = regs->p1;
  37. gdb_regs[BFIN_P2] = regs->p2;
  38. gdb_regs[BFIN_P3] = regs->p3;
  39. gdb_regs[BFIN_P4] = regs->p4;
  40. gdb_regs[BFIN_P5] = regs->p5;
  41. gdb_regs[BFIN_SP] = regs->reserved;
  42. gdb_regs[BFIN_FP] = regs->fp;
  43. gdb_regs[BFIN_I0] = regs->i0;
  44. gdb_regs[BFIN_I1] = regs->i1;
  45. gdb_regs[BFIN_I2] = regs->i2;
  46. gdb_regs[BFIN_I3] = regs->i3;
  47. gdb_regs[BFIN_M0] = regs->m0;
  48. gdb_regs[BFIN_M1] = regs->m1;
  49. gdb_regs[BFIN_M2] = regs->m2;
  50. gdb_regs[BFIN_M3] = regs->m3;
  51. gdb_regs[BFIN_B0] = regs->b0;
  52. gdb_regs[BFIN_B1] = regs->b1;
  53. gdb_regs[BFIN_B2] = regs->b2;
  54. gdb_regs[BFIN_B3] = regs->b3;
  55. gdb_regs[BFIN_L0] = regs->l0;
  56. gdb_regs[BFIN_L1] = regs->l1;
  57. gdb_regs[BFIN_L2] = regs->l2;
  58. gdb_regs[BFIN_L3] = regs->l3;
  59. gdb_regs[BFIN_A0_DOT_X] = regs->a0x;
  60. gdb_regs[BFIN_A0_DOT_W] = regs->a0w;
  61. gdb_regs[BFIN_A1_DOT_X] = regs->a1x;
  62. gdb_regs[BFIN_A1_DOT_W] = regs->a1w;
  63. gdb_regs[BFIN_ASTAT] = regs->astat;
  64. gdb_regs[BFIN_RETS] = regs->rets;
  65. gdb_regs[BFIN_LC0] = regs->lc0;
  66. gdb_regs[BFIN_LT0] = regs->lt0;
  67. gdb_regs[BFIN_LB0] = regs->lb0;
  68. gdb_regs[BFIN_LC1] = regs->lc1;
  69. gdb_regs[BFIN_LT1] = regs->lt1;
  70. gdb_regs[BFIN_LB1] = regs->lb1;
  71. gdb_regs[BFIN_CYCLES] = 0;
  72. gdb_regs[BFIN_CYCLES2] = 0;
  73. gdb_regs[BFIN_USP] = regs->usp;
  74. gdb_regs[BFIN_SEQSTAT] = regs->seqstat;
  75. gdb_regs[BFIN_SYSCFG] = regs->syscfg;
  76. gdb_regs[BFIN_RETI] = regs->pc;
  77. gdb_regs[BFIN_RETX] = regs->retx;
  78. gdb_regs[BFIN_RETN] = regs->retn;
  79. gdb_regs[BFIN_RETE] = regs->rete;
  80. gdb_regs[BFIN_PC] = regs->pc;
  81. gdb_regs[BFIN_CC] = 0;
  82. gdb_regs[BFIN_EXTRA1] = 0;
  83. gdb_regs[BFIN_EXTRA2] = 0;
  84. gdb_regs[BFIN_EXTRA3] = 0;
  85. gdb_regs[BFIN_IPEND] = regs->ipend;
  86. }
  87. /*
  88. * Extracts ebp, esp and eip values understandable by gdb from the values
  89. * saved by switch_to.
  90. * thread.esp points to ebp. flags and ebp are pushed in switch_to hence esp
  91. * prior to entering switch_to is 8 greater than the value that is saved.
  92. * If switch_to changes, change following code appropriately.
  93. */
  94. void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  95. {
  96. gdb_regs[BFIN_SP] = p->thread.ksp;
  97. gdb_regs[BFIN_PC] = p->thread.pc;
  98. gdb_regs[BFIN_SEQSTAT] = p->thread.seqstat;
  99. }
  100. void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  101. {
  102. regs->r0 = gdb_regs[BFIN_R0];
  103. regs->r1 = gdb_regs[BFIN_R1];
  104. regs->r2 = gdb_regs[BFIN_R2];
  105. regs->r3 = gdb_regs[BFIN_R3];
  106. regs->r4 = gdb_regs[BFIN_R4];
  107. regs->r5 = gdb_regs[BFIN_R5];
  108. regs->r6 = gdb_regs[BFIN_R6];
  109. regs->r7 = gdb_regs[BFIN_R7];
  110. regs->p0 = gdb_regs[BFIN_P0];
  111. regs->p1 = gdb_regs[BFIN_P1];
  112. regs->p2 = gdb_regs[BFIN_P2];
  113. regs->p3 = gdb_regs[BFIN_P3];
  114. regs->p4 = gdb_regs[BFIN_P4];
  115. regs->p5 = gdb_regs[BFIN_P5];
  116. regs->fp = gdb_regs[BFIN_FP];
  117. regs->i0 = gdb_regs[BFIN_I0];
  118. regs->i1 = gdb_regs[BFIN_I1];
  119. regs->i2 = gdb_regs[BFIN_I2];
  120. regs->i3 = gdb_regs[BFIN_I3];
  121. regs->m0 = gdb_regs[BFIN_M0];
  122. regs->m1 = gdb_regs[BFIN_M1];
  123. regs->m2 = gdb_regs[BFIN_M2];
  124. regs->m3 = gdb_regs[BFIN_M3];
  125. regs->b0 = gdb_regs[BFIN_B0];
  126. regs->b1 = gdb_regs[BFIN_B1];
  127. regs->b2 = gdb_regs[BFIN_B2];
  128. regs->b3 = gdb_regs[BFIN_B3];
  129. regs->l0 = gdb_regs[BFIN_L0];
  130. regs->l1 = gdb_regs[BFIN_L1];
  131. regs->l2 = gdb_regs[BFIN_L2];
  132. regs->l3 = gdb_regs[BFIN_L3];
  133. regs->a0x = gdb_regs[BFIN_A0_DOT_X];
  134. regs->a0w = gdb_regs[BFIN_A0_DOT_W];
  135. regs->a1x = gdb_regs[BFIN_A1_DOT_X];
  136. regs->a1w = gdb_regs[BFIN_A1_DOT_W];
  137. regs->rets = gdb_regs[BFIN_RETS];
  138. regs->lc0 = gdb_regs[BFIN_LC0];
  139. regs->lt0 = gdb_regs[BFIN_LT0];
  140. regs->lb0 = gdb_regs[BFIN_LB0];
  141. regs->lc1 = gdb_regs[BFIN_LC1];
  142. regs->lt1 = gdb_regs[BFIN_LT1];
  143. regs->lb1 = gdb_regs[BFIN_LB1];
  144. regs->usp = gdb_regs[BFIN_USP];
  145. regs->syscfg = gdb_regs[BFIN_SYSCFG];
  146. regs->retx = gdb_regs[BFIN_PC];
  147. regs->retn = gdb_regs[BFIN_RETN];
  148. regs->rete = gdb_regs[BFIN_RETE];
  149. regs->pc = gdb_regs[BFIN_PC];
  150. #if 0 /* can't change these */
  151. regs->astat = gdb_regs[BFIN_ASTAT];
  152. regs->seqstat = gdb_regs[BFIN_SEQSTAT];
  153. regs->ipend = gdb_regs[BFIN_IPEND];
  154. #endif
  155. }
  156. struct hw_breakpoint {
  157. unsigned int occupied:1;
  158. unsigned int skip:1;
  159. unsigned int enabled:1;
  160. unsigned int type:1;
  161. unsigned int dataacc:2;
  162. unsigned short count;
  163. unsigned int addr;
  164. } breakinfo[HW_WATCHPOINT_NUM];
  165. int bfin_set_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  166. {
  167. int breakno;
  168. int bfin_type;
  169. int dataacc = 0;
  170. switch (type) {
  171. case BP_HARDWARE_BREAKPOINT:
  172. bfin_type = TYPE_INST_WATCHPOINT;
  173. break;
  174. case BP_WRITE_WATCHPOINT:
  175. dataacc = 1;
  176. bfin_type = TYPE_DATA_WATCHPOINT;
  177. break;
  178. case BP_READ_WATCHPOINT:
  179. dataacc = 2;
  180. bfin_type = TYPE_DATA_WATCHPOINT;
  181. break;
  182. case BP_ACCESS_WATCHPOINT:
  183. dataacc = 3;
  184. bfin_type = TYPE_DATA_WATCHPOINT;
  185. break;
  186. default:
  187. return -ENOSPC;
  188. }
  189. /* Becasue hardware data watchpoint impelemented in current
  190. * Blackfin can not trigger an exception event as the hardware
  191. * instrction watchpoint does, we ignaore all data watch point here.
  192. * They can be turned on easily after future blackfin design
  193. * supports this feature.
  194. */
  195. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  196. if (bfin_type == breakinfo[breakno].type
  197. && !breakinfo[breakno].occupied) {
  198. breakinfo[breakno].occupied = 1;
  199. breakinfo[breakno].skip = 0;
  200. breakinfo[breakno].enabled = 1;
  201. breakinfo[breakno].addr = addr;
  202. breakinfo[breakno].dataacc = dataacc;
  203. breakinfo[breakno].count = 0;
  204. return 0;
  205. }
  206. return -ENOSPC;
  207. }
  208. int bfin_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype type)
  209. {
  210. int breakno;
  211. int bfin_type;
  212. switch (type) {
  213. case BP_HARDWARE_BREAKPOINT:
  214. bfin_type = TYPE_INST_WATCHPOINT;
  215. break;
  216. case BP_WRITE_WATCHPOINT:
  217. case BP_READ_WATCHPOINT:
  218. case BP_ACCESS_WATCHPOINT:
  219. bfin_type = TYPE_DATA_WATCHPOINT;
  220. break;
  221. default:
  222. return 0;
  223. }
  224. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  225. if (bfin_type == breakinfo[breakno].type
  226. && breakinfo[breakno].occupied
  227. && breakinfo[breakno].addr == addr) {
  228. breakinfo[breakno].occupied = 0;
  229. breakinfo[breakno].enabled = 0;
  230. }
  231. return 0;
  232. }
  233. void bfin_remove_all_hw_break(void)
  234. {
  235. int breakno;
  236. memset(breakinfo, 0, sizeof(struct hw_breakpoint)*HW_WATCHPOINT_NUM);
  237. for (breakno = 0; breakno < HW_INST_WATCHPOINT_NUM; breakno++)
  238. breakinfo[breakno].type = TYPE_INST_WATCHPOINT;
  239. for (; breakno < HW_WATCHPOINT_NUM; breakno++)
  240. breakinfo[breakno].type = TYPE_DATA_WATCHPOINT;
  241. }
  242. void bfin_correct_hw_break(void)
  243. {
  244. int breakno;
  245. unsigned int wpiactl = 0;
  246. unsigned int wpdactl = 0;
  247. int enable_wp = 0;
  248. for (breakno = 0; breakno < HW_WATCHPOINT_NUM; breakno++)
  249. if (breakinfo[breakno].enabled) {
  250. enable_wp = 1;
  251. switch (breakno) {
  252. case 0:
  253. wpiactl |= WPIAEN0|WPICNTEN0;
  254. bfin_write_WPIA0(breakinfo[breakno].addr);
  255. bfin_write_WPIACNT0(breakinfo[breakno].count
  256. + breakinfo->skip);
  257. break;
  258. case 1:
  259. wpiactl |= WPIAEN1|WPICNTEN1;
  260. bfin_write_WPIA1(breakinfo[breakno].addr);
  261. bfin_write_WPIACNT1(breakinfo[breakno].count
  262. + breakinfo->skip);
  263. break;
  264. case 2:
  265. wpiactl |= WPIAEN2|WPICNTEN2;
  266. bfin_write_WPIA2(breakinfo[breakno].addr);
  267. bfin_write_WPIACNT2(breakinfo[breakno].count
  268. + breakinfo->skip);
  269. break;
  270. case 3:
  271. wpiactl |= WPIAEN3|WPICNTEN3;
  272. bfin_write_WPIA3(breakinfo[breakno].addr);
  273. bfin_write_WPIACNT3(breakinfo[breakno].count
  274. + breakinfo->skip);
  275. break;
  276. case 4:
  277. wpiactl |= WPIAEN4|WPICNTEN4;
  278. bfin_write_WPIA4(breakinfo[breakno].addr);
  279. bfin_write_WPIACNT4(breakinfo[breakno].count
  280. + breakinfo->skip);
  281. break;
  282. case 5:
  283. wpiactl |= WPIAEN5|WPICNTEN5;
  284. bfin_write_WPIA5(breakinfo[breakno].addr);
  285. bfin_write_WPIACNT5(breakinfo[breakno].count
  286. + breakinfo->skip);
  287. break;
  288. case 6:
  289. wpdactl |= WPDAEN0|WPDCNTEN0|WPDSRC0;
  290. wpdactl |= breakinfo[breakno].dataacc
  291. << WPDACC0_OFFSET;
  292. bfin_write_WPDA0(breakinfo[breakno].addr);
  293. bfin_write_WPDACNT0(breakinfo[breakno].count
  294. + breakinfo->skip);
  295. break;
  296. case 7:
  297. wpdactl |= WPDAEN1|WPDCNTEN1|WPDSRC1;
  298. wpdactl |= breakinfo[breakno].dataacc
  299. << WPDACC1_OFFSET;
  300. bfin_write_WPDA1(breakinfo[breakno].addr);
  301. bfin_write_WPDACNT1(breakinfo[breakno].count
  302. + breakinfo->skip);
  303. break;
  304. }
  305. }
  306. /* Should enable WPPWR bit first before set any other
  307. * WPIACTL and WPDACTL bits */
  308. if (enable_wp) {
  309. bfin_write_WPIACTL(WPPWR);
  310. CSYNC();
  311. bfin_write_WPIACTL(wpiactl|WPPWR);
  312. bfin_write_WPDACTL(wpdactl);
  313. CSYNC();
  314. }
  315. }
  316. void kgdb_disable_hw_debug(struct pt_regs *regs)
  317. {
  318. /* Disable hardware debugging while we are in kgdb */
  319. bfin_write_WPIACTL(0);
  320. bfin_write_WPDACTL(0);
  321. CSYNC();
  322. }
  323. #ifdef CONFIG_SMP
  324. void kgdb_passive_cpu_callback(void *info)
  325. {
  326. kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
  327. }
  328. void kgdb_roundup_cpus(unsigned long flags)
  329. {
  330. smp_call_function(kgdb_passive_cpu_callback, NULL, 0);
  331. }
  332. void kgdb_roundup_cpu(int cpu, unsigned long flags)
  333. {
  334. smp_call_function_single(cpu, kgdb_passive_cpu_callback, NULL, 0);
  335. }
  336. #endif
  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. }
  376. bfin_correct_hw_break();
  377. return 0;
  378. } /* switch */
  379. return -1; /* this means that we do not want to exit from the handler */
  380. }
  381. struct kgdb_arch arch_kgdb_ops = {
  382. .gdb_bpt_instr = {0xa1},
  383. #ifdef CONFIG_SMP
  384. .flags = KGDB_HW_BREAKPOINT|KGDB_THR_PROC_SWAP,
  385. #else
  386. .flags = KGDB_HW_BREAKPOINT,
  387. #endif
  388. .set_hw_breakpoint = bfin_set_hw_break,
  389. .remove_hw_breakpoint = bfin_remove_hw_break,
  390. .remove_all_hw_break = bfin_remove_all_hw_break,
  391. .correct_hw_break = bfin_correct_hw_break,
  392. };
  393. static int hex(char ch)
  394. {
  395. if ((ch >= 'a') && (ch <= 'f'))
  396. return ch - 'a' + 10;
  397. if ((ch >= '0') && (ch <= '9'))
  398. return ch - '0';
  399. if ((ch >= 'A') && (ch <= 'F'))
  400. return ch - 'A' + 10;
  401. return -1;
  402. }
  403. static int validate_memory_access_address(unsigned long addr, int size)
  404. {
  405. if (size < 0 || addr == 0)
  406. return -EFAULT;
  407. return bfin_mem_access_type(addr, size);
  408. }
  409. static int bfin_probe_kernel_read(char *dst, char *src, int size)
  410. {
  411. unsigned long lsrc = (unsigned long)src;
  412. int mem_type;
  413. mem_type = validate_memory_access_address(lsrc, size);
  414. if (mem_type < 0)
  415. return mem_type;
  416. if (lsrc >= SYSMMR_BASE) {
  417. if (size == 2 && lsrc % 2 == 0) {
  418. u16 mmr = bfin_read16(src);
  419. memcpy(dst, &mmr, sizeof(mmr));
  420. return 0;
  421. } else if (size == 4 && lsrc % 4 == 0) {
  422. u32 mmr = bfin_read32(src);
  423. memcpy(dst, &mmr, sizeof(mmr));
  424. return 0;
  425. }
  426. } else {
  427. switch (mem_type) {
  428. case BFIN_MEM_ACCESS_CORE:
  429. case BFIN_MEM_ACCESS_CORE_ONLY:
  430. return probe_kernel_read(dst, src, size);
  431. /* XXX: should support IDMA here with SMP */
  432. case BFIN_MEM_ACCESS_DMA:
  433. if (dma_memcpy(dst, src, size))
  434. return 0;
  435. break;
  436. case BFIN_MEM_ACCESS_ITEST:
  437. if (isram_memcpy(dst, src, size))
  438. return 0;
  439. break;
  440. }
  441. }
  442. return -EFAULT;
  443. }
  444. static int bfin_probe_kernel_write(char *dst, char *src, int size)
  445. {
  446. unsigned long ldst = (unsigned long)dst;
  447. int mem_type;
  448. mem_type = validate_memory_access_address(ldst, size);
  449. if (mem_type < 0)
  450. return mem_type;
  451. if (ldst >= SYSMMR_BASE) {
  452. if (size == 2 && ldst % 2 == 0) {
  453. u16 mmr;
  454. memcpy(&mmr, src, sizeof(mmr));
  455. bfin_write16(dst, mmr);
  456. return 0;
  457. } else if (size == 4 && ldst % 4 == 0) {
  458. u32 mmr;
  459. memcpy(&mmr, src, sizeof(mmr));
  460. bfin_write32(dst, mmr);
  461. return 0;
  462. }
  463. } else {
  464. switch (mem_type) {
  465. case BFIN_MEM_ACCESS_CORE:
  466. case BFIN_MEM_ACCESS_CORE_ONLY:
  467. return probe_kernel_write(dst, src, size);
  468. /* XXX: should support IDMA here with SMP */
  469. case BFIN_MEM_ACCESS_DMA:
  470. if (dma_memcpy(dst, src, size))
  471. return 0;
  472. break;
  473. case BFIN_MEM_ACCESS_ITEST:
  474. if (isram_memcpy(dst, src, size))
  475. return 0;
  476. break;
  477. }
  478. }
  479. return -EFAULT;
  480. }
  481. /*
  482. * Convert the memory pointed to by mem into hex, placing result in buf.
  483. * Return a pointer to the last char put in buf (null). May return an error.
  484. */
  485. int kgdb_mem2hex(char *mem, char *buf, int count)
  486. {
  487. char *tmp;
  488. int err;
  489. /*
  490. * We use the upper half of buf as an intermediate buffer for the
  491. * raw memory copy. Hex conversion will work against this one.
  492. */
  493. tmp = buf + count;
  494. err = bfin_probe_kernel_read(tmp, mem, count);
  495. if (!err) {
  496. while (count > 0) {
  497. buf = pack_hex_byte(buf, *tmp);
  498. tmp++;
  499. count--;
  500. }
  501. *buf = 0;
  502. }
  503. return err;
  504. }
  505. /*
  506. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  507. * 0x7d escaped with 0x7d. Return a pointer to the character after
  508. * the last byte written.
  509. */
  510. int kgdb_ebin2mem(char *buf, char *mem, int count)
  511. {
  512. char *tmp_old, *tmp_new;
  513. int size;
  514. tmp_old = tmp_new = buf;
  515. for (size = 0; size < count; ++size) {
  516. if (*tmp_old == 0x7d)
  517. *tmp_new = *(++tmp_old) ^ 0x20;
  518. else
  519. *tmp_new = *tmp_old;
  520. tmp_new++;
  521. tmp_old++;
  522. }
  523. return bfin_probe_kernel_write(mem, buf, count);
  524. }
  525. /*
  526. * Convert the hex array pointed to by buf into binary to be placed in mem.
  527. * Return a pointer to the character AFTER the last byte written.
  528. * May return an error.
  529. */
  530. int kgdb_hex2mem(char *buf, char *mem, int count)
  531. {
  532. char *tmp_raw, *tmp_hex;
  533. /*
  534. * We use the upper half of buf as an intermediate buffer for the
  535. * raw memory that is converted from hex.
  536. */
  537. tmp_raw = buf + count * 2;
  538. tmp_hex = tmp_raw - 1;
  539. while (tmp_hex >= buf) {
  540. tmp_raw--;
  541. *tmp_raw = hex(*tmp_hex--);
  542. *tmp_raw |= hex(*tmp_hex--) << 4;
  543. }
  544. return bfin_probe_kernel_write(mem, tmp_raw, count);
  545. }
  546. #define IN_MEM(addr, size, l1_addr, l1_size) \
  547. ({ \
  548. unsigned long __addr = (unsigned long)(addr); \
  549. (l1_size && __addr >= l1_addr && __addr + (size) <= l1_addr + l1_size); \
  550. })
  551. #define ASYNC_BANK_SIZE \
  552. (ASYNC_BANK0_SIZE + ASYNC_BANK1_SIZE + \
  553. ASYNC_BANK2_SIZE + ASYNC_BANK3_SIZE)
  554. int kgdb_validate_break_address(unsigned long addr)
  555. {
  556. int cpu = raw_smp_processor_id();
  557. if (addr >= 0x1000 && (addr + BREAK_INSTR_SIZE) <= physical_mem_end)
  558. return 0;
  559. if (IN_MEM(addr, BREAK_INSTR_SIZE, ASYNC_BANK0_BASE, ASYNC_BANK_SIZE))
  560. return 0;
  561. if (cpu == 0 && IN_MEM(addr, BREAK_INSTR_SIZE, L1_CODE_START, L1_CODE_LENGTH))
  562. return 0;
  563. #ifdef CONFIG_SMP
  564. else if (cpu == 1 && IN_MEM(addr, BREAK_INSTR_SIZE, COREB_L1_CODE_START, L1_CODE_LENGTH))
  565. return 0;
  566. #endif
  567. if (IN_MEM(addr, BREAK_INSTR_SIZE, L2_START, L2_LENGTH))
  568. return 0;
  569. return -EFAULT;
  570. }
  571. int kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
  572. {
  573. int err = bfin_probe_kernel_read(saved_instr, (char *)addr,
  574. BREAK_INSTR_SIZE);
  575. if (err)
  576. return err;
  577. return bfin_probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
  578. BREAK_INSTR_SIZE);
  579. }
  580. int kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
  581. {
  582. return bfin_probe_kernel_write((char *)addr, bundle, BREAK_INSTR_SIZE);
  583. }
  584. int kgdb_arch_init(void)
  585. {
  586. kgdb_single_step = 0;
  587. bfin_remove_all_hw_break();
  588. return 0;
  589. }
  590. void kgdb_arch_exit(void)
  591. {
  592. }