kgdb.c 17 KB

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