You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2087 lines
57 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2006 by Magnus Lundin *
  6. * lundin@mlu.mine.nu *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. * *
  26. * *
  27. * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
  28. * *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "breakpoints.h"
  34. #include "cortex_m3.h"
  35. #include "target_request.h"
  36. #include "target_type.h"
  37. #include "arm_disassembler.h"
  38. #include "register.h"
  39. #include "arm_opcodes.h"
  40. /* NOTE: most of this should work fine for the Cortex-M1 and
  41. * Cortex-M0 cores too, although they're ARMv6-M not ARMv7-M.
  42. * Some differences: M0/M1 doesn't have FBP remapping or the
  43. * DWT tracing/profiling support. (So the cycle counter will
  44. * not be usable; the other stuff isn't currently used here.)
  45. *
  46. * Although there are some workarounds for errata seen only in r0p0
  47. * silicon, such old parts are hard to find and thus not much tested
  48. * any longer.
  49. */
  50. /* forward declarations */
  51. static int cortex_m3_set_breakpoint(struct target *target, struct breakpoint *breakpoint);
  52. static int cortex_m3_unset_breakpoint(struct target *target, struct breakpoint *breakpoint);
  53. static void cortex_m3_enable_watchpoints(struct target *target);
  54. static int cortex_m3_store_core_reg_u32(struct target *target,
  55. enum armv7m_regtype type, uint32_t num, uint32_t value);
  56. static int cortexm3_dap_read_coreregister_u32(struct swjdp_common *swjdp,
  57. uint32_t *value, int regnum)
  58. {
  59. int retval;
  60. uint32_t dcrdr;
  61. /* because the DCB_DCRDR is used for the emulated dcc channel
  62. * we have to save/restore the DCB_DCRDR when used */
  63. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  64. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  65. /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
  66. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  67. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum);
  68. /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
  69. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  70. dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
  71. retval = swjdp_transaction_endcheck(swjdp);
  72. /* restore DCB_DCRDR - this needs to be in a seperate
  73. * transaction otherwise the emulated DCC channel breaks */
  74. if (retval == ERROR_OK)
  75. retval = mem_ap_write_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
  76. return retval;
  77. }
  78. static int cortexm3_dap_write_coreregister_u32(struct swjdp_common *swjdp,
  79. uint32_t value, int regnum)
  80. {
  81. int retval;
  82. uint32_t dcrdr;
  83. /* because the DCB_DCRDR is used for the emulated dcc channel
  84. * we have to save/restore the DCB_DCRDR when used */
  85. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  86. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  87. /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
  88. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  89. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
  90. /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR); */
  91. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  92. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR);
  93. retval = swjdp_transaction_endcheck(swjdp);
  94. /* restore DCB_DCRDR - this needs to be in a seperate
  95. * transaction otherwise the emulated DCC channel breaks */
  96. if (retval == ERROR_OK)
  97. retval = mem_ap_write_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
  98. return retval;
  99. }
  100. static int cortex_m3_write_debug_halt_mask(struct target *target,
  101. uint32_t mask_on, uint32_t mask_off)
  102. {
  103. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  104. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  105. /* mask off status bits */
  106. cortex_m3->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
  107. /* create new register mask */
  108. cortex_m3->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
  109. return mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, cortex_m3->dcb_dhcsr);
  110. }
  111. static int cortex_m3_clear_halt(struct target *target)
  112. {
  113. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  114. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  115. /* clear step if any */
  116. cortex_m3_write_debug_halt_mask(target, C_HALT, C_STEP);
  117. /* Read Debug Fault Status Register */
  118. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  119. /* Clear Debug Fault Status */
  120. mem_ap_write_atomic_u32(swjdp, NVIC_DFSR, cortex_m3->nvic_dfsr);
  121. LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32 "", cortex_m3->nvic_dfsr);
  122. return ERROR_OK;
  123. }
  124. static int cortex_m3_single_step_core(struct target *target)
  125. {
  126. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  127. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  128. uint32_t dhcsr_save;
  129. /* backup dhcsr reg */
  130. dhcsr_save = cortex_m3->dcb_dhcsr;
  131. /* Mask interrupts before clearing halt, if done already. This avoids
  132. * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
  133. * HALT can put the core into an unknown state.
  134. */
  135. if (!(cortex_m3->dcb_dhcsr & C_MASKINTS))
  136. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR,
  137. DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
  138. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR,
  139. DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
  140. LOG_DEBUG(" ");
  141. /* restore dhcsr reg */
  142. cortex_m3->dcb_dhcsr = dhcsr_save;
  143. cortex_m3_clear_halt(target);
  144. return ERROR_OK;
  145. }
  146. static int cortex_m3_endreset_event(struct target *target)
  147. {
  148. int i;
  149. uint32_t dcb_demcr;
  150. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  151. struct armv7m_common *armv7m = &cortex_m3->armv7m;
  152. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  153. struct cortex_m3_fp_comparator *fp_list = cortex_m3->fp_comparator_list;
  154. struct cortex_m3_dwt_comparator *dwt_list = cortex_m3->dwt_comparator_list;
  155. /* REVISIT The four debug monitor bits are currently ignored... */
  156. mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &dcb_demcr);
  157. LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32 "",dcb_demcr);
  158. /* this register is used for emulated dcc channel */
  159. mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
  160. /* Enable debug requests */
  161. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  162. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  163. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  164. /* clear any interrupt masking */
  165. cortex_m3_write_debug_halt_mask(target, 0, C_MASKINTS);
  166. /* Enable features controlled by ITM and DWT blocks, and catch only
  167. * the vectors we were told to pay attention to.
  168. *
  169. * Target firmware is responsible for all fault handling policy
  170. * choices *EXCEPT* explicitly scripted overrides like "vector_catch"
  171. * or manual updates to the NVIC SHCSR and CCR registers.
  172. */
  173. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | armv7m->demcr);
  174. /* Paranoia: evidently some (early?) chips don't preserve all the
  175. * debug state (including FBP, DWT, etc) across reset...
  176. */
  177. /* Enable FPB */
  178. target_write_u32(target, FP_CTRL, 3);
  179. cortex_m3->fpb_enabled = 1;
  180. /* Restore FPB registers */
  181. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  182. {
  183. target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
  184. }
  185. /* Restore DWT registers */
  186. for (i = 0; i < cortex_m3->dwt_num_comp; i++)
  187. {
  188. target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
  189. dwt_list[i].comp);
  190. target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
  191. dwt_list[i].mask);
  192. target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
  193. dwt_list[i].function);
  194. }
  195. swjdp_transaction_endcheck(swjdp);
  196. register_cache_invalidate(cortex_m3->armv7m.core_cache);
  197. /* make sure we have latest dhcsr flags */
  198. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  199. return ERROR_OK;
  200. }
  201. static int cortex_m3_examine_debug_reason(struct target *target)
  202. {
  203. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  204. /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
  205. /* only check the debug reason if we don't know it already */
  206. if ((target->debug_reason != DBG_REASON_DBGRQ)
  207. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  208. {
  209. if (cortex_m3->nvic_dfsr & DFSR_BKPT)
  210. {
  211. target->debug_reason = DBG_REASON_BREAKPOINT;
  212. if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  213. target->debug_reason = DBG_REASON_WPTANDBKPT;
  214. }
  215. else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  216. target->debug_reason = DBG_REASON_WATCHPOINT;
  217. else if (cortex_m3->nvic_dfsr & DFSR_VCATCH)
  218. target->debug_reason = DBG_REASON_BREAKPOINT;
  219. else /* EXTERNAL, HALTED */
  220. target->debug_reason = DBG_REASON_UNDEFINED;
  221. }
  222. return ERROR_OK;
  223. }
  224. static int cortex_m3_examine_exception_reason(struct target *target)
  225. {
  226. uint32_t shcsr, except_sr, cfsr = -1, except_ar = -1;
  227. struct armv7m_common *armv7m = target_to_armv7m(target);
  228. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  229. mem_ap_read_u32(swjdp, NVIC_SHCSR, &shcsr);
  230. switch (armv7m->exception_number)
  231. {
  232. case 2: /* NMI */
  233. break;
  234. case 3: /* Hard Fault */
  235. mem_ap_read_atomic_u32(swjdp, NVIC_HFSR, &except_sr);
  236. if (except_sr & 0x40000000)
  237. {
  238. mem_ap_read_u32(swjdp, NVIC_CFSR, &cfsr);
  239. }
  240. break;
  241. case 4: /* Memory Management */
  242. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  243. mem_ap_read_u32(swjdp, NVIC_MMFAR, &except_ar);
  244. break;
  245. case 5: /* Bus Fault */
  246. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  247. mem_ap_read_u32(swjdp, NVIC_BFAR, &except_ar);
  248. break;
  249. case 6: /* Usage Fault */
  250. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  251. break;
  252. case 11: /* SVCall */
  253. break;
  254. case 12: /* Debug Monitor */
  255. mem_ap_read_u32(swjdp, NVIC_DFSR, &except_sr);
  256. break;
  257. case 14: /* PendSV */
  258. break;
  259. case 15: /* SysTick */
  260. break;
  261. default:
  262. except_sr = 0;
  263. break;
  264. }
  265. swjdp_transaction_endcheck(swjdp);
  266. LOG_DEBUG("%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32 ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32 "", armv7m_exception_string(armv7m->exception_number), \
  267. shcsr, except_sr, cfsr, except_ar);
  268. return ERROR_OK;
  269. }
  270. static int cortex_m3_debug_entry(struct target *target)
  271. {
  272. int i;
  273. uint32_t xPSR;
  274. int retval;
  275. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  276. struct armv7m_common *armv7m = &cortex_m3->armv7m;
  277. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  278. struct reg *r;
  279. LOG_DEBUG(" ");
  280. cortex_m3_clear_halt(target);
  281. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  282. if ((retval = armv7m->examine_debug_reason(target)) != ERROR_OK)
  283. return retval;
  284. /* Examine target state and mode */
  285. /* First load register acessible through core debug port*/
  286. int num_regs = armv7m->core_cache->num_regs;
  287. for (i = 0; i < num_regs; i++)
  288. {
  289. if (!armv7m->core_cache->reg_list[i].valid)
  290. armv7m->read_core_reg(target, i);
  291. }
  292. r = armv7m->core_cache->reg_list + ARMV7M_xPSR;
  293. xPSR = buf_get_u32(r->value, 0, 32);
  294. #ifdef ARMV7_GDB_HACKS
  295. /* FIXME this breaks on scan chains with more than one Cortex-M3.
  296. * Instead, each CM3 should have its own dummy value...
  297. */
  298. /* copy real xpsr reg for gdb, setting thumb bit */
  299. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 0, 32, xPSR);
  300. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 5, 1, 1);
  301. armv7m_gdb_dummy_cpsr_reg.valid = r->valid;
  302. armv7m_gdb_dummy_cpsr_reg.dirty = r->dirty;
  303. #endif
  304. /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
  305. if (xPSR & 0xf00)
  306. {
  307. r->dirty = r->valid;
  308. cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR &~ 0xff);
  309. }
  310. /* Are we in an exception handler */
  311. if (xPSR & 0x1FF)
  312. {
  313. armv7m->core_mode = ARMV7M_MODE_HANDLER;
  314. armv7m->exception_number = (xPSR & 0x1FF);
  315. }
  316. else
  317. {
  318. armv7m->core_mode = buf_get_u32(armv7m->core_cache
  319. ->reg_list[ARMV7M_CONTROL].value, 0, 1);
  320. armv7m->exception_number = 0;
  321. }
  322. if (armv7m->exception_number)
  323. {
  324. cortex_m3_examine_exception_reason(target);
  325. }
  326. LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
  327. armv7m_mode_strings[armv7m->core_mode],
  328. *(uint32_t*)(armv7m->core_cache->reg_list[15].value),
  329. target_state_name(target));
  330. if (armv7m->post_debug_entry)
  331. armv7m->post_debug_entry(target);
  332. return ERROR_OK;
  333. }
  334. static int cortex_m3_poll(struct target *target)
  335. {
  336. int retval;
  337. enum target_state prev_target_state = target->state;
  338. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  339. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  340. /* Read from Debug Halting Control and Status Register */
  341. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  342. if (retval != ERROR_OK)
  343. {
  344. target->state = TARGET_UNKNOWN;
  345. return retval;
  346. }
  347. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  348. {
  349. /* check if still in reset */
  350. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  351. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  352. {
  353. target->state = TARGET_RESET;
  354. return ERROR_OK;
  355. }
  356. }
  357. if (target->state == TARGET_RESET)
  358. {
  359. /* Cannot switch context while running so endreset is
  360. * called with target->state == TARGET_RESET
  361. */
  362. LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32,
  363. cortex_m3->dcb_dhcsr);
  364. cortex_m3_endreset_event(target);
  365. target->state = TARGET_RUNNING;
  366. prev_target_state = TARGET_RUNNING;
  367. }
  368. if (cortex_m3->dcb_dhcsr & S_HALT)
  369. {
  370. target->state = TARGET_HALTED;
  371. if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET))
  372. {
  373. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  374. return retval;
  375. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  376. }
  377. if (prev_target_state == TARGET_DEBUG_RUNNING)
  378. {
  379. LOG_DEBUG(" ");
  380. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  381. return retval;
  382. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  383. }
  384. }
  385. /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
  386. * How best to model low power modes?
  387. */
  388. if (target->state == TARGET_UNKNOWN)
  389. {
  390. /* check if processor is retiring instructions */
  391. if (cortex_m3->dcb_dhcsr & S_RETIRE_ST)
  392. {
  393. target->state = TARGET_RUNNING;
  394. return ERROR_OK;
  395. }
  396. }
  397. return ERROR_OK;
  398. }
  399. static int cortex_m3_halt(struct target *target)
  400. {
  401. LOG_DEBUG("target->state: %s",
  402. target_state_name(target));
  403. if (target->state == TARGET_HALTED)
  404. {
  405. LOG_DEBUG("target was already halted");
  406. return ERROR_OK;
  407. }
  408. if (target->state == TARGET_UNKNOWN)
  409. {
  410. LOG_WARNING("target was in unknown state when halt was requested");
  411. }
  412. if (target->state == TARGET_RESET)
  413. {
  414. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
  415. {
  416. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  417. return ERROR_TARGET_FAILURE;
  418. }
  419. else
  420. {
  421. /* we came here in a reset_halt or reset_init sequence
  422. * debug entry was already prepared in cortex_m3_prepare_reset_halt()
  423. */
  424. target->debug_reason = DBG_REASON_DBGRQ;
  425. return ERROR_OK;
  426. }
  427. }
  428. /* Write to Debug Halting Control and Status Register */
  429. cortex_m3_write_debug_halt_mask(target, C_HALT, 0);
  430. target->debug_reason = DBG_REASON_DBGRQ;
  431. return ERROR_OK;
  432. }
  433. static int cortex_m3_soft_reset_halt(struct target *target)
  434. {
  435. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  436. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  437. uint32_t dcb_dhcsr = 0;
  438. int retval, timeout = 0;
  439. /* Enter debug state on reset; restore DEMCR in endreset_event() */
  440. mem_ap_write_u32(swjdp, DCB_DEMCR,
  441. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  442. /* Request a core-only reset */
  443. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR,
  444. AIRCR_VECTKEY | AIRCR_VECTRESET);
  445. target->state = TARGET_RESET;
  446. /* registers are now invalid */
  447. register_cache_invalidate(cortex_m3->armv7m.core_cache);
  448. while (timeout < 100)
  449. {
  450. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
  451. if (retval == ERROR_OK)
  452. {
  453. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR,
  454. &cortex_m3->nvic_dfsr);
  455. if ((dcb_dhcsr & S_HALT)
  456. && (cortex_m3->nvic_dfsr & DFSR_VCATCH))
  457. {
  458. LOG_DEBUG("system reset-halted, DHCSR 0x%08x, "
  459. "DFSR 0x%08x",
  460. (unsigned) dcb_dhcsr,
  461. (unsigned) cortex_m3->nvic_dfsr);
  462. cortex_m3_poll(target);
  463. /* FIXME restore user's vector catch config */
  464. return ERROR_OK;
  465. }
  466. else
  467. LOG_DEBUG("waiting for system reset-halt, "
  468. "DHCSR 0x%08x, %d ms",
  469. (unsigned) dcb_dhcsr, timeout);
  470. }
  471. timeout++;
  472. alive_sleep(1);
  473. }
  474. return ERROR_OK;
  475. }
  476. static void cortex_m3_enable_breakpoints(struct target *target)
  477. {
  478. struct breakpoint *breakpoint = target->breakpoints;
  479. /* set any pending breakpoints */
  480. while (breakpoint)
  481. {
  482. if (!breakpoint->set)
  483. cortex_m3_set_breakpoint(target, breakpoint);
  484. breakpoint = breakpoint->next;
  485. }
  486. }
  487. static int cortex_m3_resume(struct target *target, int current,
  488. uint32_t address, int handle_breakpoints, int debug_execution)
  489. {
  490. struct armv7m_common *armv7m = target_to_armv7m(target);
  491. struct breakpoint *breakpoint = NULL;
  492. uint32_t resume_pc;
  493. struct reg *r;
  494. if (target->state != TARGET_HALTED)
  495. {
  496. LOG_WARNING("target not halted");
  497. return ERROR_TARGET_NOT_HALTED;
  498. }
  499. if (!debug_execution)
  500. {
  501. target_free_all_working_areas(target);
  502. cortex_m3_enable_breakpoints(target);
  503. cortex_m3_enable_watchpoints(target);
  504. }
  505. if (debug_execution)
  506. {
  507. r = armv7m->core_cache->reg_list + ARMV7M_PRIMASK;
  508. /* Disable interrupts */
  509. /* We disable interrupts in the PRIMASK register instead of
  510. * masking with C_MASKINTS. This is probably the same issue
  511. * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
  512. * in parallel with disabled interrupts can cause local faults
  513. * to not be taken.
  514. *
  515. * REVISIT this clearly breaks non-debug execution, since the
  516. * PRIMASK register state isn't saved/restored... workaround
  517. * by never resuming app code after debug execution.
  518. */
  519. buf_set_u32(r->value, 0, 1, 1);
  520. r->dirty = true;
  521. r->valid = true;
  522. /* Make sure we are in Thumb mode */
  523. r = armv7m->core_cache->reg_list + ARMV7M_xPSR;
  524. buf_set_u32(r->value, 24, 1, 1);
  525. r->dirty = true;
  526. r->valid = true;
  527. }
  528. /* current = 1: continue on current pc, otherwise continue at <address> */
  529. r = armv7m->core_cache->reg_list + 15;
  530. if (!current)
  531. {
  532. buf_set_u32(r->value, 0, 32, address);
  533. r->dirty = true;
  534. r->valid = true;
  535. }
  536. /* if we halted last time due to a bkpt instruction
  537. * then we have to manually step over it, otherwise
  538. * the core will break again */
  539. if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
  540. && !debug_execution)
  541. {
  542. armv7m_maybe_skip_bkpt_inst(target, NULL);
  543. }
  544. resume_pc = buf_get_u32(r->value, 0, 32);
  545. armv7m_restore_context(target);
  546. /* the front-end may request us not to handle breakpoints */
  547. if (handle_breakpoints)
  548. {
  549. /* Single step past breakpoint at current address */
  550. if ((breakpoint = breakpoint_find(target, resume_pc)))
  551. {
  552. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (ID: %d)",
  553. breakpoint->address,
  554. breakpoint->unique_id);
  555. cortex_m3_unset_breakpoint(target, breakpoint);
  556. cortex_m3_single_step_core(target);
  557. cortex_m3_set_breakpoint(target, breakpoint);
  558. }
  559. }
  560. /* Restart core */
  561. cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
  562. target->debug_reason = DBG_REASON_NOTHALTED;
  563. /* registers are now invalid */
  564. register_cache_invalidate(armv7m->core_cache);
  565. if (!debug_execution)
  566. {
  567. target->state = TARGET_RUNNING;
  568. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  569. LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
  570. }
  571. else
  572. {
  573. target->state = TARGET_DEBUG_RUNNING;
  574. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  575. LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
  576. }
  577. return ERROR_OK;
  578. }
  579. /* int irqstepcount = 0; */
  580. static int cortex_m3_step(struct target *target, int current,
  581. uint32_t address, int handle_breakpoints)
  582. {
  583. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  584. struct armv7m_common *armv7m = &cortex_m3->armv7m;
  585. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  586. struct breakpoint *breakpoint = NULL;
  587. struct reg *pc = armv7m->core_cache->reg_list + 15;
  588. bool bkpt_inst_found = false;
  589. if (target->state != TARGET_HALTED)
  590. {
  591. LOG_WARNING("target not halted");
  592. return ERROR_TARGET_NOT_HALTED;
  593. }
  594. /* current = 1: continue on current pc, otherwise continue at <address> */
  595. if (!current)
  596. buf_set_u32(pc->value, 0, 32, address);
  597. /* the front-end may request us not to handle breakpoints */
  598. if (handle_breakpoints) {
  599. breakpoint = breakpoint_find(target,
  600. buf_get_u32(pc->value, 0, 32));
  601. if (breakpoint)
  602. cortex_m3_unset_breakpoint(target, breakpoint);
  603. }
  604. armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
  605. target->debug_reason = DBG_REASON_SINGLESTEP;
  606. armv7m_restore_context(target);
  607. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  608. /* if no bkpt instruction is found at pc then we can perform
  609. * a normal step, otherwise we have to manually step over the bkpt
  610. * instruction - as such simulate a step */
  611. if (bkpt_inst_found == false)
  612. {
  613. /* set step and clear halt */
  614. cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
  615. }
  616. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  617. /* registers are now invalid */
  618. register_cache_invalidate(cortex_m3->armv7m.core_cache);
  619. if (breakpoint)
  620. cortex_m3_set_breakpoint(target, breakpoint);
  621. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
  622. " nvic_icsr = 0x%" PRIx32,
  623. cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  624. cortex_m3_debug_entry(target);
  625. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  626. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
  627. " nvic_icsr = 0x%" PRIx32,
  628. cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  629. return ERROR_OK;
  630. }
  631. static int cortex_m3_assert_reset(struct target *target)
  632. {
  633. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  634. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  635. int assert_srst = 1;
  636. LOG_DEBUG("target->state: %s",
  637. target_state_name(target));
  638. enum reset_types jtag_reset_config = jtag_get_reset_config();
  639. /*
  640. * We can reset Cortex-M3 targets using just the NVIC without
  641. * requiring SRST, getting a SoC reset (or a core-only reset)
  642. * instead of a system reset.
  643. */
  644. if (!(jtag_reset_config & RESET_HAS_SRST))
  645. assert_srst = 0;
  646. /* Enable debug requests */
  647. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  648. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  649. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  650. mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
  651. if (!target->reset_halt)
  652. {
  653. /* Set/Clear C_MASKINTS in a separate operation */
  654. if (cortex_m3->dcb_dhcsr & C_MASKINTS)
  655. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR,
  656. DBGKEY | C_DEBUGEN | C_HALT);
  657. /* clear any debug flags before resuming */
  658. cortex_m3_clear_halt(target);
  659. /* clear C_HALT in dhcsr reg */
  660. cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
  661. }
  662. else
  663. {
  664. /* Halt in debug on reset; endreset_event() restores DEMCR.
  665. *
  666. * REVISIT catching BUSERR presumably helps to defend against
  667. * bad vector table entries. Should this include MMERR or
  668. * other flags too?
  669. */
  670. mem_ap_write_atomic_u32(swjdp, DCB_DEMCR,
  671. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  672. }
  673. /*
  674. * When nRST is asserted on most Stellaris devices, it clears some of
  675. * the debug state. The ARMv7M and Cortex-M3 TRMs say that's wrong;
  676. * and OpenOCD depends on those TRMs. So we won't use SRST on those
  677. * chips. (Only power-on reset should affect debug state, beyond a
  678. * few specified bits; not the chip's nRST input, wired to SRST.)
  679. *
  680. * REVISIT current errata specs don't seem to cover this issue.
  681. * Do we have more details than this email?
  682. * https://lists.berlios.de/pipermail
  683. * /openocd-development/2008-August/003065.html
  684. */
  685. if (strcmp(target->variant, "lm3s") == 0)
  686. {
  687. /* Check for silicon revisions with the issue. */
  688. uint32_t did0;
  689. if (target_read_u32(target, 0x400fe000, &did0) == ERROR_OK)
  690. {
  691. switch ((did0 >> 16) & 0xff)
  692. {
  693. case 0:
  694. /* all Sandstorm suffer issue */
  695. assert_srst = 0;
  696. break;
  697. case 1:
  698. case 3:
  699. /* Fury and DustDevil rev A have
  700. * this nRST problem. It should
  701. * be fixed in rev B silicon.
  702. */
  703. if (((did0 >> 8) & 0xff) == 0)
  704. assert_srst = 0;
  705. break;
  706. case 4:
  707. /* Tempest should be fine. */
  708. break;
  709. }
  710. }
  711. }
  712. if (assert_srst)
  713. {
  714. /* default to asserting srst */
  715. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  716. {
  717. jtag_add_reset(1, 1);
  718. }
  719. else
  720. {
  721. jtag_add_reset(0, 1);
  722. }
  723. }
  724. else
  725. {
  726. /* Use a standard Cortex-M3 software reset mechanism.
  727. * SYSRESETREQ will reset SoC peripherals outside the
  728. * core, like watchdog timers, if the SoC wires it up
  729. * correctly. Else VECRESET can reset just the core.
  730. */
  731. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR,
  732. AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  733. LOG_DEBUG("Using Cortex-M3 SYSRESETREQ");
  734. {
  735. /* I do not know why this is necessary, but it
  736. * fixes strange effects (step/resume cause NMI
  737. * after reset) on LM3S6918 -- Michael Schwingen
  738. */
  739. uint32_t tmp;
  740. mem_ap_read_atomic_u32(swjdp, NVIC_AIRCR, &tmp);
  741. }
  742. }
  743. target->state = TARGET_RESET;
  744. jtag_add_sleep(50000);
  745. register_cache_invalidate(cortex_m3->armv7m.core_cache);
  746. if (target->reset_halt)
  747. {
  748. int retval;
  749. if ((retval = target_halt(target)) != ERROR_OK)
  750. return retval;
  751. }
  752. return ERROR_OK;
  753. }
  754. static int cortex_m3_deassert_reset(struct target *target)
  755. {
  756. LOG_DEBUG("target->state: %s",
  757. target_state_name(target));
  758. /* deassert reset lines */
  759. jtag_add_reset(0, 0);
  760. return ERROR_OK;
  761. }
  762. static int
  763. cortex_m3_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
  764. {
  765. int retval;
  766. int fp_num = 0;
  767. uint32_t hilo;
  768. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  769. struct cortex_m3_fp_comparator *comparator_list = cortex_m3->fp_comparator_list;
  770. if (breakpoint->set)
  771. {
  772. LOG_WARNING("breakpoint (BPID: %d) already set", breakpoint->unique_id);
  773. return ERROR_OK;
  774. }
  775. if (cortex_m3->auto_bp_type)
  776. {
  777. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  778. }
  779. if (breakpoint->type == BKPT_HARD)
  780. {
  781. while (comparator_list[fp_num].used && (fp_num < cortex_m3->fp_num_code))
  782. fp_num++;
  783. if (fp_num >= cortex_m3->fp_num_code)
  784. {
  785. LOG_ERROR("Can not find free FPB Comparator!");
  786. return ERROR_FAIL;
  787. }
  788. breakpoint->set = fp_num + 1;
  789. hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
  790. comparator_list[fp_num].used = 1;
  791. comparator_list[fp_num].fpcr_value = (breakpoint->address & 0x1FFFFFFC) | hilo | 1;
  792. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  793. LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32 "", fp_num, comparator_list[fp_num].fpcr_value);
  794. if (!cortex_m3->fpb_enabled)
  795. {
  796. LOG_DEBUG("FPB wasn't enabled, do it now");
  797. target_write_u32(target, FP_CTRL, 3);
  798. }
  799. }
  800. else if (breakpoint->type == BKPT_SOFT)
  801. {
  802. uint8_t code[4];
  803. /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
  804. * semihosting; don't use that. Otherwise the BKPT
  805. * parameter is arbitrary.
  806. */
  807. buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
  808. retval = target_read_memory(target,
  809. breakpoint->address & 0xFFFFFFFE,
  810. breakpoint->length, 1,
  811. breakpoint->orig_instr);
  812. if (retval != ERROR_OK)
  813. return retval;
  814. retval = target_write_memory(target,
  815. breakpoint->address & 0xFFFFFFFE,
  816. breakpoint->length, 1,
  817. code);
  818. if (retval != ERROR_OK)
  819. return retval;
  820. breakpoint->set = true;
  821. }
  822. LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  823. breakpoint->unique_id,
  824. (int)(breakpoint->type),
  825. breakpoint->address,
  826. breakpoint->length,
  827. breakpoint->set);
  828. return ERROR_OK;
  829. }
  830. static int
  831. cortex_m3_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
  832. {
  833. int retval;
  834. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  835. struct cortex_m3_fp_comparator * comparator_list = cortex_m3->fp_comparator_list;
  836. if (!breakpoint->set)
  837. {
  838. LOG_WARNING("breakpoint not set");
  839. return ERROR_OK;
  840. }
  841. LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  842. breakpoint->unique_id,
  843. (int)(breakpoint->type),
  844. breakpoint->address,
  845. breakpoint->length,
  846. breakpoint->set);
  847. if (breakpoint->type == BKPT_HARD)
  848. {
  849. int fp_num = breakpoint->set - 1;
  850. if ((fp_num < 0) || (fp_num >= cortex_m3->fp_num_code))
  851. {
  852. LOG_DEBUG("Invalid FP Comparator number in breakpoint");
  853. return ERROR_OK;
  854. }
  855. comparator_list[fp_num].used = 0;
  856. comparator_list[fp_num].fpcr_value = 0;
  857. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  858. }
  859. else
  860. {
  861. /* restore original instruction (kept in target endianness) */
  862. if (breakpoint->length == 4)
  863. {
  864. if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  865. {
  866. return retval;
  867. }
  868. }
  869. else
  870. {
  871. if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  872. {
  873. return retval;
  874. }
  875. }
  876. }
  877. breakpoint->set = false;
  878. return ERROR_OK;
  879. }
  880. static int
  881. cortex_m3_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  882. {
  883. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  884. if (cortex_m3->auto_bp_type)
  885. {
  886. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  887. #ifdef ARMV7_GDB_HACKS
  888. if (breakpoint->length != 2) {
  889. /* XXX Hack: Replace all breakpoints with length != 2 with
  890. * a hardware breakpoint. */
  891. breakpoint->type = BKPT_HARD;
  892. breakpoint->length = 2;
  893. }
  894. #endif
  895. }
  896. if ((breakpoint->type == BKPT_HARD) && (breakpoint->address >= 0x20000000))
  897. {
  898. LOG_INFO("flash patch comparator requested outside code memory region");
  899. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  900. }
  901. if ((breakpoint->type == BKPT_SOFT) && (breakpoint->address < 0x20000000))
  902. {
  903. LOG_INFO("soft breakpoint requested in code (flash) memory region");
  904. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  905. }
  906. if ((breakpoint->type == BKPT_HARD) && (cortex_m3->fp_code_available < 1))
  907. {
  908. LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
  909. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  910. }
  911. if ((breakpoint->length != 2))
  912. {
  913. LOG_INFO("only breakpoints of two bytes length supported");
  914. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  915. }
  916. if (breakpoint->type == BKPT_HARD)
  917. cortex_m3->fp_code_available--;
  918. cortex_m3_set_breakpoint(target, breakpoint);
  919. return ERROR_OK;
  920. }
  921. static int
  922. cortex_m3_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
  923. {
  924. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  925. /* REVISIT why check? FBP can be updated with core running ... */
  926. if (target->state != TARGET_HALTED)
  927. {
  928. LOG_WARNING("target not halted");
  929. return ERROR_TARGET_NOT_HALTED;
  930. }
  931. if (cortex_m3->auto_bp_type)
  932. {
  933. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  934. }
  935. if (breakpoint->set)
  936. {
  937. cortex_m3_unset_breakpoint(target, breakpoint);
  938. }
  939. if (breakpoint->type == BKPT_HARD)
  940. cortex_m3->fp_code_available++;
  941. return ERROR_OK;
  942. }
  943. static int
  944. cortex_m3_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
  945. {
  946. int dwt_num = 0;
  947. uint32_t mask, temp;
  948. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  949. /* watchpoint params were validated earlier */
  950. mask = 0;
  951. temp = watchpoint->length;
  952. while (temp) {
  953. temp >>= 1;
  954. mask++;
  955. }
  956. mask--;
  957. /* REVISIT Don't fully trust these "not used" records ... users
  958. * may set up breakpoints by hand, e.g. dual-address data value
  959. * watchpoint using comparator #1; comparator #0 matching cycle
  960. * count; send data trace info through ITM and TPIU; etc
  961. */
  962. struct cortex_m3_dwt_comparator *comparator;
  963. for (comparator = cortex_m3->dwt_comparator_list;
  964. comparator->used && dwt_num < cortex_m3->dwt_num_comp;
  965. comparator++, dwt_num++)
  966. continue;
  967. if (dwt_num >= cortex_m3->dwt_num_comp)
  968. {
  969. LOG_ERROR("Can not find free DWT Comparator");
  970. return ERROR_FAIL;
  971. }
  972. comparator->used = 1;
  973. watchpoint->set = dwt_num + 1;
  974. comparator->comp = watchpoint->address;
  975. target_write_u32(target, comparator->dwt_comparator_address + 0,
  976. comparator->comp);
  977. comparator->mask = mask;
  978. target_write_u32(target, comparator->dwt_comparator_address + 4,
  979. comparator->mask);
  980. switch (watchpoint->rw) {
  981. case WPT_READ:
  982. comparator->function = 5;
  983. break;
  984. case WPT_WRITE:
  985. comparator->function = 6;
  986. break;
  987. case WPT_ACCESS:
  988. comparator->function = 7;
  989. break;
  990. }
  991. target_write_u32(target, comparator->dwt_comparator_address + 8,
  992. comparator->function);
  993. LOG_DEBUG("Watchpoint (ID %d) DWT%d 0x%08x 0x%x 0x%05x",
  994. watchpoint->unique_id, dwt_num,
  995. (unsigned) comparator->comp,
  996. (unsigned) comparator->mask,
  997. (unsigned) comparator->function);
  998. return ERROR_OK;
  999. }
  1000. static int
  1001. cortex_m3_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1002. {
  1003. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1004. struct cortex_m3_dwt_comparator *comparator;
  1005. int dwt_num;
  1006. if (!watchpoint->set)
  1007. {
  1008. LOG_WARNING("watchpoint (wpid: %d) not set",
  1009. watchpoint->unique_id);
  1010. return ERROR_OK;
  1011. }
  1012. dwt_num = watchpoint->set - 1;
  1013. LOG_DEBUG("Watchpoint (ID %d) DWT%d address: 0x%08x clear",
  1014. watchpoint->unique_id, dwt_num,
  1015. (unsigned) watchpoint->address);
  1016. if ((dwt_num < 0) || (dwt_num >= cortex_m3->dwt_num_comp))
  1017. {
  1018. LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
  1019. return ERROR_OK;
  1020. }
  1021. comparator = cortex_m3->dwt_comparator_list + dwt_num;
  1022. comparator->used = 0;
  1023. comparator->function = 0;
  1024. target_write_u32(target, comparator->dwt_comparator_address + 8,
  1025. comparator->function);
  1026. watchpoint->set = false;
  1027. return ERROR_OK;
  1028. }
  1029. static int
  1030. cortex_m3_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1031. {
  1032. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1033. if (cortex_m3->dwt_comp_available < 1)
  1034. {
  1035. LOG_DEBUG("no comparators?");
  1036. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1037. }
  1038. /* hardware doesn't support data value masking */
  1039. if (watchpoint->mask != ~(uint32_t)0) {
  1040. LOG_DEBUG("watchpoint value masks not supported");
  1041. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1042. }
  1043. /* hardware allows address masks of up to 32K */
  1044. unsigned mask;
  1045. for (mask = 0; mask < 16; mask++) {
  1046. if ((1u << mask) == watchpoint->length)
  1047. break;
  1048. }
  1049. if (mask == 16) {
  1050. LOG_DEBUG("unsupported watchpoint length");
  1051. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1052. }
  1053. if (watchpoint->address & ((1 << mask) - 1)) {
  1054. LOG_DEBUG("watchpoint address is unaligned");
  1055. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1056. }
  1057. /* Caller doesn't seem to be able to describe watching for data
  1058. * values of zero; that flags "no value".
  1059. *
  1060. * REVISIT This DWT may well be able to watch for specific data
  1061. * values. Requires comparator #1 to set DATAVMATCH and match
  1062. * the data, and another comparator (DATAVADDR0) matching addr.
  1063. */
  1064. if (watchpoint->value) {
  1065. LOG_DEBUG("data value watchpoint not YET supported");
  1066. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1067. }
  1068. cortex_m3->dwt_comp_available--;
  1069. LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
  1070. return ERROR_OK;
  1071. }
  1072. static int
  1073. cortex_m3_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1074. {
  1075. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1076. /* REVISIT why check? DWT can be updated with core running ... */
  1077. if (target->state != TARGET_HALTED)
  1078. {
  1079. LOG_WARNING("target not halted");
  1080. return ERROR_TARGET_NOT_HALTED;
  1081. }
  1082. if (watchpoint->set)
  1083. {
  1084. cortex_m3_unset_watchpoint(target, watchpoint);
  1085. }
  1086. cortex_m3->dwt_comp_available++;
  1087. LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
  1088. return ERROR_OK;
  1089. }
  1090. static void cortex_m3_enable_watchpoints(struct target *target)
  1091. {
  1092. struct watchpoint *watchpoint = target->watchpoints;
  1093. /* set any pending watchpoints */
  1094. while (watchpoint)
  1095. {
  1096. if (!watchpoint->set)
  1097. cortex_m3_set_watchpoint(target, watchpoint);
  1098. watchpoint = watchpoint->next;
  1099. }
  1100. }
  1101. static int cortex_m3_load_core_reg_u32(struct target *target,
  1102. enum armv7m_regtype type, uint32_t num, uint32_t * value)
  1103. {
  1104. int retval;
  1105. struct armv7m_common *armv7m = target_to_armv7m(target);
  1106. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1107. /* NOTE: we "know" here that the register identifiers used
  1108. * in the v7m header match the Cortex-M3 Debug Core Register
  1109. * Selector values for R0..R15, xPSR, MSP, and PSP.
  1110. */
  1111. switch (num) {
  1112. case 0 ... 18:
  1113. /* read a normal core register */
  1114. retval = cortexm3_dap_read_coreregister_u32(swjdp, value, num);
  1115. if (retval != ERROR_OK)
  1116. {
  1117. LOG_ERROR("JTAG failure %i",retval);
  1118. return ERROR_JTAG_DEVICE_ERROR;
  1119. }
  1120. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "",(int)num,*value);
  1121. break;
  1122. case ARMV7M_PRIMASK:
  1123. case ARMV7M_BASEPRI:
  1124. case ARMV7M_FAULTMASK:
  1125. case ARMV7M_CONTROL:
  1126. /* Cortex-M3 packages these four registers as bitfields
  1127. * in one Debug Core register. So say r0 and r2 docs;
  1128. * it was removed from r1 docs, but still works.
  1129. */
  1130. cortexm3_dap_read_coreregister_u32(swjdp, value, 20);
  1131. switch (num)
  1132. {
  1133. case ARMV7M_PRIMASK:
  1134. *value = buf_get_u32((uint8_t*)value, 0, 1);
  1135. break;
  1136. case ARMV7M_BASEPRI:
  1137. *value = buf_get_u32((uint8_t*)value, 8, 8);
  1138. break;
  1139. case ARMV7M_FAULTMASK:
  1140. *value = buf_get_u32((uint8_t*)value, 16, 1);
  1141. break;
  1142. case ARMV7M_CONTROL:
  1143. *value = buf_get_u32((uint8_t*)value, 24, 2);
  1144. break;
  1145. }
  1146. LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
  1147. break;
  1148. default:
  1149. return ERROR_INVALID_ARGUMENTS;
  1150. }
  1151. return ERROR_OK;
  1152. }
  1153. static int cortex_m3_store_core_reg_u32(struct target *target,
  1154. enum armv7m_regtype type, uint32_t num, uint32_t value)
  1155. {
  1156. int retval;
  1157. uint32_t reg;
  1158. struct armv7m_common *armv7m = target_to_armv7m(target);
  1159. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1160. #ifdef ARMV7_GDB_HACKS
  1161. /* If the LR register is being modified, make sure it will put us
  1162. * in "thumb" mode, or an INVSTATE exception will occur. This is a
  1163. * hack to deal with the fact that gdb will sometimes "forge"
  1164. * return addresses, and doesn't set the LSB correctly (i.e., when
  1165. * printing expressions containing function calls, it sets LR = 0.)
  1166. * Valid exception return codes have bit 0 set too.
  1167. */
  1168. if (num == ARMV7M_R14)
  1169. value |= 0x01;
  1170. #endif
  1171. /* NOTE: we "know" here that the register identifiers used
  1172. * in the v7m header match the Cortex-M3 Debug Core Register
  1173. * Selector values for R0..R15, xPSR, MSP, and PSP.
  1174. */
  1175. switch (num) {
  1176. case 0 ... 18:
  1177. retval = cortexm3_dap_write_coreregister_u32(swjdp, value, num);
  1178. if (retval != ERROR_OK)
  1179. {
  1180. struct reg *r;
  1181. LOG_ERROR("JTAG failure %i", retval);
  1182. r = armv7m->core_cache->reg_list + num;
  1183. r->dirty = r->valid;
  1184. return ERROR_JTAG_DEVICE_ERROR;
  1185. }
  1186. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  1187. break;
  1188. case ARMV7M_PRIMASK:
  1189. case ARMV7M_BASEPRI:
  1190. case ARMV7M_FAULTMASK:
  1191. case ARMV7M_CONTROL:
  1192. /* Cortex-M3 packages these four registers as bitfields
  1193. * in one Debug Core register. So say r0 and r2 docs;
  1194. * it was removed from r1 docs, but still works.
  1195. */
  1196. cortexm3_dap_read_coreregister_u32(swjdp, &reg, 20);
  1197. switch (num)
  1198. {
  1199. case ARMV7M_PRIMASK:
  1200. buf_set_u32((uint8_t*)&reg, 0, 1, value);
  1201. break;
  1202. case ARMV7M_BASEPRI:
  1203. buf_set_u32((uint8_t*)&reg, 8, 8, value);
  1204. break;
  1205. case ARMV7M_FAULTMASK:
  1206. buf_set_u32((uint8_t*)&reg, 16, 1, value);
  1207. break;
  1208. case ARMV7M_CONTROL:
  1209. buf_set_u32((uint8_t*)&reg, 24, 2, value);
  1210. break;
  1211. }
  1212. cortexm3_dap_write_coreregister_u32(swjdp, reg, 20);
  1213. LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
  1214. break;
  1215. default:
  1216. return ERROR_INVALID_ARGUMENTS;
  1217. }
  1218. return ERROR_OK;
  1219. }
  1220. static int cortex_m3_read_memory(struct target *target, uint32_t address,
  1221. uint32_t size, uint32_t count, uint8_t *buffer)
  1222. {
  1223. struct armv7m_common *armv7m = target_to_armv7m(target);
  1224. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1225. int retval = ERROR_INVALID_ARGUMENTS;
  1226. /* cortex_m3 handles unaligned memory access */
  1227. if (count && buffer) {
  1228. switch (size) {
  1229. case 4:
  1230. retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
  1231. break;
  1232. case 2:
  1233. retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
  1234. break;
  1235. case 1:
  1236. retval = mem_ap_read_buf_u8(swjdp, buffer, count, address);
  1237. break;
  1238. }
  1239. }
  1240. return retval;
  1241. }
  1242. static int cortex_m3_write_memory(struct target *target, uint32_t address,
  1243. uint32_t size, uint32_t count, uint8_t *buffer)
  1244. {
  1245. struct armv7m_common *armv7m = target_to_armv7m(target);
  1246. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1247. int retval = ERROR_INVALID_ARGUMENTS;
  1248. if (count && buffer) {
  1249. switch (size) {
  1250. case 4:
  1251. retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
  1252. break;
  1253. case 2:
  1254. retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
  1255. break;
  1256. case 1:
  1257. retval = mem_ap_write_buf_u8(swjdp, buffer, count, address);
  1258. break;
  1259. }
  1260. }
  1261. return retval;
  1262. }
  1263. static int cortex_m3_bulk_write_memory(struct target *target, uint32_t address,
  1264. uint32_t count, uint8_t *buffer)
  1265. {
  1266. return cortex_m3_write_memory(target, address, 4, count, buffer);
  1267. }
  1268. static int cortex_m3_init_target(struct command_context *cmd_ctx,
  1269. struct target *target)
  1270. {
  1271. armv7m_build_reg_cache(target);
  1272. return ERROR_OK;
  1273. }
  1274. /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
  1275. * on r/w if the core is not running, and clear on resume or reset ... or
  1276. * at least, in a post_restore_context() method.
  1277. */
  1278. struct dwt_reg_state {
  1279. struct target *target;
  1280. uint32_t addr;
  1281. uint32_t value; /* scratch/cache */
  1282. };
  1283. static int cortex_m3_dwt_get_reg(struct reg *reg)
  1284. {
  1285. struct dwt_reg_state *state = reg->arch_info;
  1286. return target_read_u32(state->target, state->addr, &state->value);
  1287. }
  1288. static int cortex_m3_dwt_set_reg(struct reg *reg, uint8_t *buf)
  1289. {
  1290. struct dwt_reg_state *state = reg->arch_info;
  1291. return target_write_u32(state->target, state->addr,
  1292. buf_get_u32(buf, 0, reg->size));
  1293. }
  1294. struct dwt_reg {
  1295. uint32_t addr;
  1296. char *name;
  1297. unsigned size;
  1298. };
  1299. static struct dwt_reg dwt_base_regs[] = {
  1300. { DWT_CTRL, "dwt_ctrl", 32, },
  1301. /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
  1302. * increments while the core is asleep.
  1303. */
  1304. { DWT_CYCCNT, "dwt_cyccnt", 32, },
  1305. /* plus some 8 bit counters, useful for profiling with TPIU */
  1306. };
  1307. static struct dwt_reg dwt_comp[] = {
  1308. #define DWT_COMPARATOR(i) \
  1309. { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
  1310. { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
  1311. { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
  1312. DWT_COMPARATOR(0),
  1313. DWT_COMPARATOR(1),
  1314. DWT_COMPARATOR(2),
  1315. DWT_COMPARATOR(3),
  1316. #undef DWT_COMPARATOR
  1317. };
  1318. static const struct reg_arch_type dwt_reg_type = {
  1319. .get = cortex_m3_dwt_get_reg,
  1320. .set = cortex_m3_dwt_set_reg,
  1321. };
  1322. static void
  1323. cortex_m3_dwt_addreg(struct target *t, struct reg *r, struct dwt_reg *d)
  1324. {
  1325. struct dwt_reg_state *state;
  1326. state = calloc(1, sizeof *state);
  1327. if (!state)
  1328. return;
  1329. state->addr = d->addr;
  1330. state->target = t;
  1331. r->name = d->name;
  1332. r->size = d->size;
  1333. r->value = &state->value;
  1334. r->arch_info = state;
  1335. r->type = &dwt_reg_type;
  1336. }
  1337. static void
  1338. cortex_m3_dwt_setup(struct cortex_m3_common *cm3, struct target *target)
  1339. {
  1340. uint32_t dwtcr;
  1341. struct reg_cache *cache;
  1342. struct cortex_m3_dwt_comparator *comparator;
  1343. int reg, i;
  1344. target_read_u32(target, DWT_CTRL, &dwtcr);
  1345. if (!dwtcr) {
  1346. LOG_DEBUG("no DWT");
  1347. return;
  1348. }
  1349. cm3->dwt_num_comp = (dwtcr >> 28) & 0xF;
  1350. cm3->dwt_comp_available = cm3->dwt_num_comp;
  1351. cm3->dwt_comparator_list = calloc(cm3->dwt_num_comp,
  1352. sizeof(struct cortex_m3_dwt_comparator));
  1353. if (!cm3->dwt_comparator_list) {
  1354. fail0:
  1355. cm3->dwt_num_comp = 0;
  1356. LOG_ERROR("out of mem");
  1357. return;
  1358. }
  1359. cache = calloc(1, sizeof *cache);
  1360. if (!cache) {
  1361. fail1:
  1362. free(cm3->dwt_comparator_list);
  1363. goto fail0;
  1364. }
  1365. cache->name = "cortex-m3 dwt registers";
  1366. cache->num_regs = 2 + cm3->dwt_num_comp * 3;
  1367. cache->reg_list = calloc(cache->num_regs, sizeof *cache->reg_list);
  1368. if (!cache->reg_list) {
  1369. free(cache);
  1370. goto fail1;
  1371. }
  1372. for (reg = 0; reg < 2; reg++)
  1373. cortex_m3_dwt_addreg(target, cache->reg_list + reg,
  1374. dwt_base_regs + reg);
  1375. comparator = cm3->dwt_comparator_list;
  1376. for (i = 0; i < cm3->dwt_num_comp; i++, comparator++) {
  1377. int j;
  1378. comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
  1379. for (j = 0; j < 3; j++, reg++)
  1380. cortex_m3_dwt_addreg(target, cache->reg_list + reg,
  1381. dwt_comp + 3 * i + j);
  1382. }
  1383. *register_get_last_cache_p(&target->reg_cache) = cache;
  1384. cm3->dwt_cache = cache;
  1385. LOG_DEBUG("DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
  1386. dwtcr, cm3->dwt_num_comp,
  1387. (dwtcr & (0xf << 24)) ? " only" : "/trigger");
  1388. /* REVISIT: if num_comp > 1, check whether comparator #1 can
  1389. * implement single-address data value watchpoints ... so we
  1390. * won't need to check it later, when asked to set one up.
  1391. */
  1392. }
  1393. static int cortex_m3_examine(struct target *target)
  1394. {
  1395. int retval;
  1396. uint32_t cpuid, fpcr;
  1397. int i;
  1398. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1399. struct swjdp_common *swjdp = &cortex_m3->armv7m.swjdp_info;
  1400. if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
  1401. return retval;
  1402. if (!target_was_examined(target))
  1403. {
  1404. target_set_examined(target);
  1405. /* Read from Device Identification Registers */
  1406. retval = target_read_u32(target, CPUID, &cpuid);
  1407. if (retval != ERROR_OK)
  1408. return retval;
  1409. if (((cpuid >> 4) & 0xc3f) == 0xc23)
  1410. LOG_DEBUG("Cortex-M3 r%dp%d processor detected",
  1411. (cpuid >> 20) & 0xf, (cpuid >> 0) & 0xf);
  1412. LOG_DEBUG("cpuid: 0x%8.8" PRIx32 "", cpuid);
  1413. /* NOTE: FPB and DWT are both optional. */
  1414. /* Setup FPB */
  1415. target_read_u32(target, FP_CTRL, &fpcr);
  1416. cortex_m3->auto_bp_type = 1;
  1417. cortex_m3->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF); /* bits [14:12] and [7:4] */
  1418. cortex_m3->fp_num_lit = (fpcr >> 8) & 0xF;
  1419. cortex_m3->fp_code_available = cortex_m3->fp_num_code;
  1420. cortex_m3->fp_comparator_list = calloc(cortex_m3->fp_num_code + cortex_m3->fp_num_lit, sizeof(struct cortex_m3_fp_comparator));
  1421. cortex_m3->fpb_enabled = fpcr & 1;
  1422. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  1423. {
  1424. cortex_m3->fp_comparator_list[i].type = (i < cortex_m3->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
  1425. cortex_m3->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
  1426. }
  1427. LOG_DEBUG("FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i", fpcr, cortex_m3->fp_num_code, cortex_m3->fp_num_lit);
  1428. /* Setup DWT */
  1429. cortex_m3_dwt_setup(cortex_m3, target);
  1430. /* These hardware breakpoints only work for code in flash! */
  1431. LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints",
  1432. target_name(target),
  1433. cortex_m3->fp_num_code,
  1434. cortex_m3->dwt_num_comp);
  1435. }
  1436. return ERROR_OK;
  1437. }
  1438. static int cortex_m3_dcc_read(struct swjdp_common *swjdp, uint8_t *value, uint8_t *ctrl)
  1439. {
  1440. uint16_t dcrdr;
  1441. mem_ap_read_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
  1442. *ctrl = (uint8_t)dcrdr;
  1443. *value = (uint8_t)(dcrdr >> 8);
  1444. LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
  1445. /* write ack back to software dcc register
  1446. * signify we have read data */
  1447. if (dcrdr & (1 << 0))
  1448. {
  1449. dcrdr = 0;
  1450. mem_ap_write_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
  1451. }
  1452. return ERROR_OK;
  1453. }
  1454. static int cortex_m3_target_request_data(struct target *target,
  1455. uint32_t size, uint8_t *buffer)
  1456. {
  1457. struct armv7m_common *armv7m = target_to_armv7m(target);
  1458. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1459. uint8_t data;
  1460. uint8_t ctrl;
  1461. uint32_t i;
  1462. for (i = 0; i < (size * 4); i++)
  1463. {
  1464. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1465. buffer[i] = data;
  1466. }
  1467. return ERROR_OK;
  1468. }
  1469. static int cortex_m3_handle_target_request(void *priv)
  1470. {
  1471. struct target *target = priv;
  1472. if (!target_was_examined(target))
  1473. return ERROR_OK;
  1474. struct armv7m_common *armv7m = target_to_armv7m(target);
  1475. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1476. if (!target->dbg_msg_enabled)
  1477. return ERROR_OK;
  1478. if (target->state == TARGET_RUNNING)
  1479. {
  1480. uint8_t data;
  1481. uint8_t ctrl;
  1482. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1483. /* check if we have data */
  1484. if (ctrl & (1 << 0))
  1485. {
  1486. uint32_t request;
  1487. /* we assume target is quick enough */
  1488. request = data;
  1489. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1490. request |= (data << 8);
  1491. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1492. request |= (data << 16);
  1493. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1494. request |= (data << 24);
  1495. target_request(target, request);
  1496. }
  1497. }
  1498. return ERROR_OK;
  1499. }
  1500. static int cortex_m3_init_arch_info(struct target *target,
  1501. struct cortex_m3_common *cortex_m3, struct jtag_tap *tap)
  1502. {
  1503. int retval;
  1504. struct armv7m_common *armv7m = &cortex_m3->armv7m;
  1505. armv7m_init_arch_info(target, armv7m);
  1506. /* prepare JTAG information for the new target */
  1507. cortex_m3->jtag_info.tap = tap;
  1508. cortex_m3->jtag_info.scann_size = 4;
  1509. armv7m->swjdp_info.dp_select_value = -1;
  1510. armv7m->swjdp_info.ap_csw_value = -1;
  1511. armv7m->swjdp_info.ap_tar_value = -1;
  1512. armv7m->swjdp_info.jtag_info = &cortex_m3->jtag_info;
  1513. armv7m->swjdp_info.memaccess_tck = 8;
  1514. armv7m->swjdp_info.tar_autoincr_block = (1 << 12); /* Cortex-M3 has 4096 bytes autoincrement range */
  1515. /* register arch-specific functions */
  1516. armv7m->examine_debug_reason = cortex_m3_examine_debug_reason;
  1517. armv7m->post_debug_entry = NULL;
  1518. armv7m->pre_restore_context = NULL;
  1519. armv7m->post_restore_context = NULL;
  1520. armv7m->load_core_reg_u32 = cortex_m3_load_core_reg_u32;
  1521. armv7m->store_core_reg_u32 = cortex_m3_store_core_reg_u32;
  1522. target_register_timer_callback(cortex_m3_handle_target_request, 1, 1, target);
  1523. if ((retval = arm_jtag_setup_connection(&cortex_m3->jtag_info)) != ERROR_OK)
  1524. {
  1525. return retval;
  1526. }
  1527. return ERROR_OK;
  1528. }
  1529. static int cortex_m3_target_create(struct target *target, Jim_Interp *interp)
  1530. {
  1531. struct cortex_m3_common *cortex_m3 = calloc(1,sizeof(struct cortex_m3_common));
  1532. cortex_m3->common_magic = CORTEX_M3_COMMON_MAGIC;
  1533. cortex_m3_init_arch_info(target, cortex_m3, target->tap);
  1534. return ERROR_OK;
  1535. }
  1536. /*--------------------------------------------------------------------------*/
  1537. static int cortex_m3_verify_pointer(struct command_context *cmd_ctx,
  1538. struct cortex_m3_common *cm3)
  1539. {
  1540. if (cm3->common_magic != CORTEX_M3_COMMON_MAGIC) {
  1541. command_print(cmd_ctx, "target is not a Cortex-M3");
  1542. return ERROR_TARGET_INVALID;
  1543. }
  1544. return ERROR_OK;
  1545. }
  1546. /*
  1547. * Only stuff below this line should need to verify that its target
  1548. * is a Cortex-M3. Everything else should have indirected through the
  1549. * cortexm3_target structure, which is only used with CM3 targets.
  1550. */
  1551. /*
  1552. * REVISIT Thumb2 disassembly should work for all ARMv7 cores, as well
  1553. * as at least ARM-1156T2. The interesting thing about Cortex-M is
  1554. * that *only* Thumb2 disassembly matters. There are also some small
  1555. * additions to Thumb2 that are specific to ARMv7-M.
  1556. */
  1557. COMMAND_HANDLER(handle_cortex_m3_disassemble_command)
  1558. {
  1559. int retval;
  1560. struct target *target = get_current_target(CMD_CTX);
  1561. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1562. uint32_t address;
  1563. unsigned long count = 1;
  1564. struct arm_instruction cur_instruction;
  1565. retval = cortex_m3_verify_pointer(CMD_CTX, cortex_m3);
  1566. if (retval != ERROR_OK)
  1567. return retval;
  1568. errno = 0;
  1569. switch (CMD_ARGC) {
  1570. case 2:
  1571. COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], count);
  1572. /* FALL THROUGH */
  1573. case 1:
  1574. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  1575. break;
  1576. default:
  1577. command_print(CMD_CTX,
  1578. "usage: cortex_m3 disassemble <address> [<count>]");
  1579. return ERROR_OK;
  1580. }
  1581. while (count--) {
  1582. retval = thumb2_opcode(target, address, &cur_instruction);
  1583. if (retval != ERROR_OK)
  1584. return retval;
  1585. command_print(CMD_CTX, "%s", cur_instruction.text);
  1586. address += cur_instruction.instruction_size;
  1587. }
  1588. return ERROR_OK;
  1589. }
  1590. static const struct {
  1591. char name[10];
  1592. unsigned mask;
  1593. } vec_ids[] = {
  1594. { "hard_err", VC_HARDERR, },
  1595. { "int_err", VC_INTERR, },
  1596. { "bus_err", VC_BUSERR, },
  1597. { "state_err", VC_STATERR, },
  1598. { "chk_err", VC_CHKERR, },
  1599. { "nocp_err", VC_NOCPERR, },
  1600. { "mm_err", VC_MMERR, },
  1601. { "reset", VC_CORERESET, },
  1602. };
  1603. COMMAND_HANDLER(handle_cortex_m3_vector_catch_command)
  1604. {
  1605. struct target *target = get_current_target(CMD_CTX);
  1606. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1607. struct armv7m_common *armv7m = &cortex_m3->armv7m;
  1608. struct swjdp_common *swjdp = &armv7m->swjdp_info;
  1609. uint32_t demcr = 0;
  1610. int retval;
  1611. retval = cortex_m3_verify_pointer(CMD_CTX, cortex_m3);
  1612. if (retval != ERROR_OK)
  1613. return retval;
  1614. mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &demcr);
  1615. if (CMD_ARGC > 0) {
  1616. unsigned catch = 0;
  1617. if (CMD_ARGC == 1) {
  1618. if (strcmp(CMD_ARGV[0], "all") == 0) {
  1619. catch = VC_HARDERR | VC_INTERR | VC_BUSERR
  1620. | VC_STATERR | VC_CHKERR | VC_NOCPERR
  1621. | VC_MMERR | VC_CORERESET;
  1622. goto write;
  1623. } else if (strcmp(CMD_ARGV[0], "none") == 0) {
  1624. goto write;
  1625. }
  1626. }
  1627. while (CMD_ARGC-- > 0) {
  1628. unsigned i;
  1629. for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
  1630. if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
  1631. continue;
  1632. catch |= vec_ids[i].mask;
  1633. break;
  1634. }
  1635. if (i == ARRAY_SIZE(vec_ids)) {
  1636. LOG_ERROR("No CM3 vector '%s'", CMD_ARGV[CMD_ARGC]);
  1637. return ERROR_INVALID_ARGUMENTS;
  1638. }
  1639. }
  1640. write:
  1641. /* For now, armv7m->demcr only stores vector catch flags. */
  1642. armv7m->demcr = catch;
  1643. demcr &= ~0xffff;
  1644. demcr |= catch;
  1645. /* write, but don't assume it stuck (why not??) */
  1646. mem_ap_write_u32(swjdp, DCB_DEMCR, demcr);
  1647. mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &demcr);
  1648. /* FIXME be sure to clear DEMCR on clean server shutdown.
  1649. * Otherwise the vector catch hardware could fire when there's
  1650. * no debugger hooked up, causing much confusion...
  1651. */
  1652. }
  1653. for (unsigned i = 0; i < ARRAY_SIZE(vec_ids); i++)
  1654. {
  1655. command_print(CMD_CTX, "%9s: %s", vec_ids[i].name,
  1656. (demcr & vec_ids[i].mask) ? "catch" : "ignore");
  1657. }
  1658. return ERROR_OK;
  1659. }
  1660. COMMAND_HANDLER(handle_cortex_m3_mask_interrupts_command)
  1661. {
  1662. struct target *target = get_current_target(CMD_CTX);
  1663. struct cortex_m3_common *cortex_m3 = target_to_cm3(target);
  1664. int retval;
  1665. retval = cortex_m3_verify_pointer(CMD_CTX, cortex_m3);
  1666. if (retval != ERROR_OK)
  1667. return retval;
  1668. if (target->state != TARGET_HALTED)
  1669. {
  1670. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  1671. return ERROR_OK;
  1672. }
  1673. if (CMD_ARGC > 0)
  1674. {
  1675. bool enable;
  1676. COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
  1677. uint32_t mask_on = C_HALT | (enable ? C_MASKINTS : 0);
  1678. uint32_t mask_off = enable ? 0 : C_MASKINTS;
  1679. cortex_m3_write_debug_halt_mask(target, mask_on, mask_off);
  1680. }
  1681. command_print(CMD_CTX, "cortex_m3 interrupt mask %s",
  1682. (cortex_m3->dcb_dhcsr & C_MASKINTS) ? "on" : "off");
  1683. return ERROR_OK;
  1684. }
  1685. static const struct command_registration cortex_m3_exec_command_handlers[] = {
  1686. {
  1687. .name = "disassemble",
  1688. .handler = handle_cortex_m3_disassemble_command,
  1689. .mode = COMMAND_EXEC,
  1690. .help = "disassemble Thumb2 instructions",
  1691. .usage = "address [count]",
  1692. },
  1693. {
  1694. .name = "maskisr",
  1695. .handler = handle_cortex_m3_mask_interrupts_command,
  1696. .mode = COMMAND_EXEC,
  1697. .help = "mask cortex_m3 interrupts",
  1698. .usage = "['on'|'off']",
  1699. },
  1700. {
  1701. .name = "vector_catch",
  1702. .handler = handle_cortex_m3_vector_catch_command,
  1703. .mode = COMMAND_EXEC,
  1704. .help = "configure hardware vectors to trigger debug entry",
  1705. .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
  1706. },
  1707. COMMAND_REGISTRATION_DONE
  1708. };
  1709. static const struct command_registration cortex_m3_command_handlers[] = {
  1710. {
  1711. .chain = armv7m_command_handlers,
  1712. },
  1713. {
  1714. .name = "cortex_m3",
  1715. .mode = COMMAND_EXEC,
  1716. .help = "Cortex-M3 command group",
  1717. .chain = cortex_m3_exec_command_handlers,
  1718. },
  1719. COMMAND_REGISTRATION_DONE
  1720. };
  1721. struct target_type cortexm3_target =
  1722. {
  1723. .name = "cortex_m3",
  1724. .poll = cortex_m3_poll,
  1725. .arch_state = armv7m_arch_state,
  1726. .target_request_data = cortex_m3_target_request_data,
  1727. .halt = cortex_m3_halt,
  1728. .resume = cortex_m3_resume,
  1729. .step = cortex_m3_step,
  1730. .assert_reset = cortex_m3_assert_reset,
  1731. .deassert_reset = cortex_m3_deassert_reset,
  1732. .soft_reset_halt = cortex_m3_soft_reset_halt,
  1733. .get_gdb_reg_list = armv7m_get_gdb_reg_list,
  1734. .read_memory = cortex_m3_read_memory,
  1735. .write_memory = cortex_m3_write_memory,
  1736. .bulk_write_memory = cortex_m3_bulk_write_memory,
  1737. .checksum_memory = armv7m_checksum_memory,
  1738. .blank_check_memory = armv7m_blank_check_memory,
  1739. .run_algorithm = armv7m_run_algorithm,
  1740. .add_breakpoint = cortex_m3_add_breakpoint,
  1741. .remove_breakpoint = cortex_m3_remove_breakpoint,
  1742. .add_watchpoint = cortex_m3_add_watchpoint,
  1743. .remove_watchpoint = cortex_m3_remove_watchpoint,
  1744. .commands = cortex_m3_command_handlers,
  1745. .target_create = cortex_m3_target_create,
  1746. .init_target = cortex_m3_init_target,
  1747. .examine = cortex_m3_examine,
  1748. };