kgdb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 then 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. int cpu = raw_smp_processor_id();
  420. if (size < 0)
  421. return EFAULT;
  422. if (addr >= 0x1000 && (addr + size) <= physical_mem_end)
  423. return 0;
  424. if (addr >= SYSMMR_BASE)
  425. return 0;
  426. if (addr >= ASYNC_BANK0_BASE
  427. && addr + size <= ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)
  428. return 0;
  429. if (cpu == 0) {
  430. if (addr >= L1_SCRATCH_START
  431. && (addr + size <= L1_SCRATCH_START + L1_SCRATCH_LENGTH))
  432. return 0;
  433. #if L1_CODE_LENGTH != 0
  434. if (addr >= L1_CODE_START
  435. && (addr + size <= L1_CODE_START + L1_CODE_LENGTH))
  436. return 0;
  437. #endif
  438. #if L1_DATA_A_LENGTH != 0
  439. if (addr >= L1_DATA_A_START
  440. && (addr + size <= L1_DATA_A_START + L1_DATA_A_LENGTH))
  441. return 0;
  442. #endif
  443. #if L1_DATA_B_LENGTH != 0
  444. if (addr >= L1_DATA_B_START
  445. && (addr + size <= L1_DATA_B_START + L1_DATA_B_LENGTH))
  446. return 0;
  447. #endif
  448. #ifdef CONFIG_SMP
  449. } else if (cpu == 1) {
  450. if (addr >= COREB_L1_SCRATCH_START
  451. && (addr + size <= COREB_L1_SCRATCH_START
  452. + L1_SCRATCH_LENGTH))
  453. return 0;
  454. # if L1_CODE_LENGTH != 0
  455. if (addr >= COREB_L1_CODE_START
  456. && (addr + size <= COREB_L1_CODE_START + L1_CODE_LENGTH))
  457. return 0;
  458. # endif
  459. # if L1_DATA_A_LENGTH != 0
  460. if (addr >= COREB_L1_DATA_A_START
  461. && (addr + size <= COREB_L1_DATA_A_START + L1_DATA_A_LENGTH))
  462. return 0;
  463. # endif
  464. # if L1_DATA_B_LENGTH != 0
  465. if (addr >= COREB_L1_DATA_B_START
  466. && (addr + size <= COREB_L1_DATA_B_START + L1_DATA_B_LENGTH))
  467. return 0;
  468. # endif
  469. #endif
  470. }
  471. #if L2_LENGTH != 0
  472. if (addr >= L2_START
  473. && addr + size <= L2_START + L2_LENGTH)
  474. return 0;
  475. #endif
  476. return EFAULT;
  477. }
  478. /*
  479. * Convert the memory pointed to by mem into hex, placing result in buf.
  480. * Return a pointer to the last char put in buf (null). May return an error.
  481. */
  482. int kgdb_mem2hex(char *mem, char *buf, int count)
  483. {
  484. char *tmp;
  485. int err = 0;
  486. unsigned char *pch;
  487. unsigned short mmr16;
  488. unsigned long mmr32;
  489. int cpu = raw_smp_processor_id();
  490. if (validate_memory_access_address((unsigned long)mem, count))
  491. return EFAULT;
  492. /*
  493. * We use the upper half of buf as an intermediate buffer for the
  494. * raw memory copy. Hex conversion will work against this one.
  495. */
  496. tmp = buf + count;
  497. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  498. switch (count) {
  499. case 2:
  500. if ((unsigned int)mem % 2 == 0) {
  501. mmr16 = *(unsigned short *)mem;
  502. pch = (unsigned char *)&mmr16;
  503. *tmp++ = *pch++;
  504. *tmp++ = *pch++;
  505. tmp -= 2;
  506. } else
  507. err = EFAULT;
  508. break;
  509. case 4:
  510. if ((unsigned int)mem % 4 == 0) {
  511. mmr32 = *(unsigned long *)mem;
  512. pch = (unsigned char *)&mmr32;
  513. *tmp++ = *pch++;
  514. *tmp++ = *pch++;
  515. *tmp++ = *pch++;
  516. *tmp++ = *pch++;
  517. tmp -= 4;
  518. } else
  519. err = EFAULT;
  520. break;
  521. default:
  522. err = EFAULT;
  523. }
  524. } else if ((cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  525. (unsigned int)(mem + count) <= L1_CODE_START + L1_CODE_LENGTH)
  526. #ifdef CONFIG_SMP
  527. || (cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  528. (unsigned int)(mem + count) <=
  529. COREB_L1_CODE_START + L1_CODE_LENGTH)
  530. #endif
  531. ) {
  532. /* access L1 instruction SRAM*/
  533. if (dma_memcpy(tmp, mem, count) == NULL)
  534. err = EFAULT;
  535. } else
  536. err = probe_kernel_read(tmp, mem, count);
  537. if (!err) {
  538. while (count > 0) {
  539. buf = pack_hex_byte(buf, *tmp);
  540. tmp++;
  541. count--;
  542. }
  543. *buf = 0;
  544. }
  545. return err;
  546. }
  547. /*
  548. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  549. * 0x7d escaped with 0x7d. Return a pointer to the character after
  550. * the last byte written.
  551. */
  552. int kgdb_ebin2mem(char *buf, char *mem, int count)
  553. {
  554. char *tmp_old;
  555. char *tmp_new;
  556. unsigned short *mmr16;
  557. unsigned long *mmr32;
  558. int err = 0;
  559. int size = 0;
  560. int cpu = raw_smp_processor_id();
  561. tmp_old = tmp_new = buf;
  562. while (count-- > 0) {
  563. if (*tmp_old == 0x7d)
  564. *tmp_new = *(++tmp_old) ^ 0x20;
  565. else
  566. *tmp_new = *tmp_old;
  567. tmp_new++;
  568. tmp_old++;
  569. size++;
  570. }
  571. if (validate_memory_access_address((unsigned long)mem, size))
  572. return EFAULT;
  573. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  574. switch (size) {
  575. case 2:
  576. if ((unsigned int)mem % 2 == 0) {
  577. mmr16 = (unsigned short *)buf;
  578. *(unsigned short *)mem = *mmr16;
  579. } else
  580. return EFAULT;
  581. break;
  582. case 4:
  583. if ((unsigned int)mem % 4 == 0) {
  584. mmr32 = (unsigned long *)buf;
  585. *(unsigned long *)mem = *mmr32;
  586. } else
  587. return EFAULT;
  588. break;
  589. default:
  590. return EFAULT;
  591. }
  592. } else if ((cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  593. (unsigned int)(mem + count) < L1_CODE_START + L1_CODE_LENGTH)
  594. #ifdef CONFIG_SMP
  595. || (cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  596. (unsigned int)(mem + count) <=
  597. COREB_L1_CODE_START + L1_CODE_LENGTH)
  598. #endif
  599. ) {
  600. /* access L1 instruction SRAM */
  601. if (dma_memcpy(mem, buf, size) == NULL)
  602. err = EFAULT;
  603. } else
  604. err = probe_kernel_write(mem, buf, size);
  605. return err;
  606. }
  607. /*
  608. * Convert the hex array pointed to by buf into binary to be placed in mem.
  609. * Return a pointer to the character AFTER the last byte written.
  610. * May return an error.
  611. */
  612. int kgdb_hex2mem(char *buf, char *mem, int count)
  613. {
  614. char *tmp_raw;
  615. char *tmp_hex;
  616. unsigned short *mmr16;
  617. unsigned long *mmr32;
  618. int cpu = raw_smp_processor_id();
  619. if (validate_memory_access_address((unsigned long)mem, count))
  620. return EFAULT;
  621. /*
  622. * We use the upper half of buf as an intermediate buffer for the
  623. * raw memory that is converted from hex.
  624. */
  625. tmp_raw = buf + count * 2;
  626. tmp_hex = tmp_raw - 1;
  627. while (tmp_hex >= buf) {
  628. tmp_raw--;
  629. *tmp_raw = hex(*tmp_hex--);
  630. *tmp_raw |= hex(*tmp_hex--) << 4;
  631. }
  632. if ((unsigned int)mem >= SYSMMR_BASE) { /*access MMR registers*/
  633. switch (count) {
  634. case 2:
  635. if ((unsigned int)mem % 2 == 0) {
  636. mmr16 = (unsigned short *)tmp_raw;
  637. *(unsigned short *)mem = *mmr16;
  638. } else
  639. return EFAULT;
  640. break;
  641. case 4:
  642. if ((unsigned int)mem % 4 == 0) {
  643. mmr32 = (unsigned long *)tmp_raw;
  644. *(unsigned long *)mem = *mmr32;
  645. } else
  646. return EFAULT;
  647. break;
  648. default:
  649. return EFAULT;
  650. }
  651. } else if ((cpu == 0 && (unsigned int)mem >= L1_CODE_START &&
  652. (unsigned int)(mem + count) <= L1_CODE_START + L1_CODE_LENGTH)
  653. #ifdef CONFIG_SMP
  654. || (cpu == 1 && (unsigned int)mem >= COREB_L1_CODE_START &&
  655. (unsigned int)(mem + count) <=
  656. COREB_L1_CODE_START + L1_CODE_LENGTH)
  657. #endif
  658. ) {
  659. /* access L1 instruction SRAM */
  660. if (dma_memcpy(mem, tmp_raw, count) == NULL)
  661. return EFAULT;
  662. } else
  663. return probe_kernel_write(mem, tmp_raw, count);
  664. return 0;
  665. }
  666. int kgdb_validate_break_address(unsigned long addr)
  667. {
  668. int cpu = raw_smp_processor_id();
  669. if (addr >= 0x1000 && (addr + BREAK_INSTR_SIZE) <= physical_mem_end)
  670. return 0;
  671. if (addr >= ASYNC_BANK0_BASE
  672. && addr + BREAK_INSTR_SIZE <= ASYNC_BANK3_BASE + ASYNC_BANK3_BASE)
  673. return 0;
  674. #if L1_CODE_LENGTH != 0
  675. if (cpu == 0 && addr >= L1_CODE_START
  676. && addr + BREAK_INSTR_SIZE <= L1_CODE_START + L1_CODE_LENGTH)
  677. return 0;
  678. # ifdef CONFIG_SMP
  679. else if (cpu == 1 && addr >= COREB_L1_CODE_START
  680. && addr + BREAK_INSTR_SIZE <= COREB_L1_CODE_START + L1_CODE_LENGTH)
  681. return 0;
  682. # endif
  683. #endif
  684. #if L2_LENGTH != 0
  685. if (addr >= L2_START
  686. && addr + BREAK_INSTR_SIZE <= L2_START + L2_LENGTH)
  687. return 0;
  688. #endif
  689. return EFAULT;
  690. }
  691. int kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
  692. {
  693. int err;
  694. int cpu = raw_smp_processor_id();
  695. if ((cpu == 0 && (unsigned int)addr >= L1_CODE_START
  696. && (unsigned int)(addr + BREAK_INSTR_SIZE)
  697. < L1_CODE_START + L1_CODE_LENGTH)
  698. #ifdef CONFIG_SMP
  699. || (cpu == 1 && (unsigned int)addr >= COREB_L1_CODE_START
  700. && (unsigned int)(addr + BREAK_INSTR_SIZE)
  701. < COREB_L1_CODE_START + L1_CODE_LENGTH)
  702. #endif
  703. ) {
  704. /* access L1 instruction SRAM */
  705. if (dma_memcpy(saved_instr, (void *)addr, BREAK_INSTR_SIZE)
  706. == NULL)
  707. return -EFAULT;
  708. if (dma_memcpy((void *)addr, arch_kgdb_ops.gdb_bpt_instr,
  709. BREAK_INSTR_SIZE) == NULL)
  710. return -EFAULT;
  711. return 0;
  712. } else {
  713. err = probe_kernel_read(saved_instr, (char *)addr,
  714. BREAK_INSTR_SIZE);
  715. if (err)
  716. return err;
  717. return probe_kernel_write((char *)addr,
  718. arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
  719. }
  720. }
  721. int kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
  722. {
  723. if ((unsigned int)addr >= L1_CODE_START &&
  724. (unsigned int)(addr + BREAK_INSTR_SIZE) <
  725. L1_CODE_START + L1_CODE_LENGTH) {
  726. /* access L1 instruction SRAM */
  727. if (dma_memcpy((void *)addr, bundle, BREAK_INSTR_SIZE) == NULL)
  728. return -EFAULT;
  729. return 0;
  730. } else
  731. return probe_kernel_write((char *)addr,
  732. (char *)bundle, BREAK_INSTR_SIZE);
  733. }
  734. int kgdb_arch_init(void)
  735. {
  736. kgdb_single_step = 0;
  737. bfin_remove_all_hw_break();
  738. return 0;
  739. }
  740. void kgdb_arch_exit(void)
  741. {
  742. }