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.
 
 
 
 
 
 

1744 lines
50 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 "cortex_m3.h"
  34. #include "target_request.h"
  35. #include "target_type.h"
  36. #include "arm_disassembler.h"
  37. /* cli handling */
  38. int cortex_m3_register_commands(struct command_context_s *cmd_ctx);
  39. int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. /* forward declarations */
  41. void cortex_m3_enable_breakpoints(struct target_s *target);
  42. void cortex_m3_enable_watchpoints(struct target_s *target);
  43. int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp);
  44. int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  45. int cortex_m3_quit(void);
  46. int cortex_m3_load_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t *value);
  47. int cortex_m3_store_core_reg_u32(target_t *target, enum armv7m_regtype type, uint32_t num, uint32_t value);
  48. int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer);
  49. int cortex_m3_examine(struct target_s *target);
  50. #ifdef ARMV7_GDB_HACKS
  51. extern uint8_t armv7m_gdb_dummy_cpsr_value[];
  52. extern reg_t armv7m_gdb_dummy_cpsr_reg;
  53. #endif
  54. target_type_t cortexm3_target =
  55. {
  56. .name = "cortex_m3",
  57. .poll = cortex_m3_poll,
  58. .arch_state = armv7m_arch_state,
  59. .target_request_data = cortex_m3_target_request_data,
  60. .halt = cortex_m3_halt,
  61. .resume = cortex_m3_resume,
  62. .step = cortex_m3_step,
  63. .assert_reset = cortex_m3_assert_reset,
  64. .deassert_reset = cortex_m3_deassert_reset,
  65. .soft_reset_halt = cortex_m3_soft_reset_halt,
  66. .get_gdb_reg_list = armv7m_get_gdb_reg_list,
  67. .read_memory = cortex_m3_read_memory,
  68. .write_memory = cortex_m3_write_memory,
  69. .bulk_write_memory = cortex_m3_bulk_write_memory,
  70. .checksum_memory = armv7m_checksum_memory,
  71. .blank_check_memory = armv7m_blank_check_memory,
  72. .run_algorithm = armv7m_run_algorithm,
  73. .add_breakpoint = cortex_m3_add_breakpoint,
  74. .remove_breakpoint = cortex_m3_remove_breakpoint,
  75. .add_watchpoint = cortex_m3_add_watchpoint,
  76. .remove_watchpoint = cortex_m3_remove_watchpoint,
  77. .register_commands = cortex_m3_register_commands,
  78. .target_create = cortex_m3_target_create,
  79. .init_target = cortex_m3_init_target,
  80. .examine = cortex_m3_examine,
  81. .quit = cortex_m3_quit
  82. };
  83. int cortexm3_dap_read_coreregister_u32(swjdp_common_t *swjdp, uint32_t *value, int regnum)
  84. {
  85. int retval;
  86. uint32_t dcrdr;
  87. /* because the DCB_DCRDR is used for the emulated dcc channel
  88. * we gave to save/restore the DCB_DCRDR when used */
  89. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  90. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  91. /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
  92. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  93. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum);
  94. /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
  95. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  96. dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
  97. mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
  98. retval = swjdp_transaction_endcheck(swjdp);
  99. return retval;
  100. }
  101. int cortexm3_dap_write_coreregister_u32(swjdp_common_t *swjdp, uint32_t value, int regnum)
  102. {
  103. int retval;
  104. uint32_t dcrdr;
  105. /* because the DCB_DCRDR is used for the emulated dcc channel
  106. * we gave to save/restore the DCB_DCRDR when used */
  107. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  108. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  109. /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
  110. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  111. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value);
  112. /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR); */
  113. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  114. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR);
  115. mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
  116. retval = swjdp_transaction_endcheck(swjdp);
  117. return retval;
  118. }
  119. int cortex_m3_write_debug_halt_mask(target_t *target, uint32_t mask_on, uint32_t mask_off)
  120. {
  121. /* get pointers to arch-specific information */
  122. armv7m_common_t *armv7m = target->arch_info;
  123. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  124. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  125. /* mask off status bits */
  126. cortex_m3->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
  127. /* create new register mask */
  128. cortex_m3->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
  129. return mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, cortex_m3->dcb_dhcsr);
  130. }
  131. int cortex_m3_clear_halt(target_t *target)
  132. {
  133. /* get pointers to arch-specific information */
  134. armv7m_common_t *armv7m = target->arch_info;
  135. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  136. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  137. /* clear step if any */
  138. cortex_m3_write_debug_halt_mask(target, C_HALT, C_STEP);
  139. /* Read Debug Fault Status Register */
  140. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  141. /* Write Debug Fault Status Register to enable processing to resume ?? Try with and without this !! */
  142. mem_ap_write_atomic_u32(swjdp, NVIC_DFSR, cortex_m3->nvic_dfsr);
  143. LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32 "", cortex_m3->nvic_dfsr);
  144. return ERROR_OK;
  145. }
  146. int cortex_m3_single_step_core(target_t *target)
  147. {
  148. /* get pointers to arch-specific information */
  149. armv7m_common_t *armv7m = target->arch_info;
  150. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  151. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  152. uint32_t dhcsr_save;
  153. /* backup dhcsr reg */
  154. dhcsr_save = cortex_m3->dcb_dhcsr;
  155. /* mask interrupts if not done already */
  156. if (!(cortex_m3->dcb_dhcsr & C_MASKINTS))
  157. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
  158. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
  159. LOG_DEBUG(" ");
  160. /* restore dhcsr reg */
  161. cortex_m3->dcb_dhcsr = dhcsr_save;
  162. cortex_m3_clear_halt(target);
  163. return ERROR_OK;
  164. }
  165. int cortex_m3_exec_opcode(target_t *target,uint32_t opcode, int len /* MODE, r0_invalue, &r0_outvalue */)
  166. {
  167. /* get pointers to arch-specific information */
  168. armv7m_common_t *armv7m = target->arch_info;
  169. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  170. uint32_t savedram;
  171. int retvalue;
  172. mem_ap_read_u32(swjdp, 0x20000000, &savedram);
  173. mem_ap_write_u32(swjdp, 0x20000000, opcode);
  174. cortexm3_dap_write_coreregister_u32(swjdp, 0x20000000, 15);
  175. cortex_m3_single_step_core(target);
  176. armv7m->core_cache->reg_list[15].dirty = armv7m->core_cache->reg_list[15].valid;
  177. retvalue = mem_ap_write_atomic_u32(swjdp, 0x20000000, savedram);
  178. return retvalue;
  179. }
  180. #if 0
  181. /* Enable interrupts */
  182. int cortex_m3_cpsie(target_t *target, uint32_t IF)
  183. {
  184. return cortex_m3_exec_opcode(target, ARMV7M_T_CPSIE(IF), 2);
  185. }
  186. /* Disable interrupts */
  187. int cortex_m3_cpsid(target_t *target, uint32_t IF)
  188. {
  189. return cortex_m3_exec_opcode(target, ARMV7M_T_CPSID(IF), 2);
  190. }
  191. #endif
  192. int cortex_m3_endreset_event(target_t *target)
  193. {
  194. int i;
  195. uint32_t dcb_demcr;
  196. /* get pointers to arch-specific information */
  197. armv7m_common_t *armv7m = target->arch_info;
  198. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  199. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  200. cortex_m3_fp_comparator_t *fp_list = cortex_m3->fp_comparator_list;
  201. cortex_m3_dwt_comparator_t *dwt_list = cortex_m3->dwt_comparator_list;
  202. mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &dcb_demcr);
  203. LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32 "",dcb_demcr);
  204. /* this regsiter is used for emulated dcc channel */
  205. mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
  206. /* Enable debug requests */
  207. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  208. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  209. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  210. /* clear any interrupt masking */
  211. cortex_m3_write_debug_halt_mask(target, 0, C_MASKINTS);
  212. /* Enable trace and dwt */
  213. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
  214. /* Monitor bus faults */
  215. mem_ap_write_u32(swjdp, NVIC_SHCSR, SHCSR_BUSFAULTENA);
  216. /* Enable FPB */
  217. target_write_u32(target, FP_CTRL, 3);
  218. cortex_m3->fpb_enabled = 1;
  219. /* Restore FPB registers */
  220. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  221. {
  222. target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
  223. }
  224. /* Restore DWT registers */
  225. for (i = 0; i < cortex_m3->dwt_num_comp; i++)
  226. {
  227. target_write_u32(target, dwt_list[i].dwt_comparator_address, dwt_list[i].comp);
  228. target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x4, dwt_list[i].mask);
  229. target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x8, dwt_list[i].function);
  230. }
  231. swjdp_transaction_endcheck(swjdp);
  232. armv7m_invalidate_core_regs(target);
  233. /* make sure we have latest dhcsr flags */
  234. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  235. return ERROR_OK;
  236. }
  237. int cortex_m3_examine_debug_reason(target_t *target)
  238. {
  239. /* get pointers to arch-specific information */
  240. armv7m_common_t *armv7m = target->arch_info;
  241. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  242. /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
  243. /* only check the debug reason if we don't know it already */
  244. if ((target->debug_reason != DBG_REASON_DBGRQ)
  245. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  246. {
  247. /* INCOMPLETE */
  248. if (cortex_m3->nvic_dfsr & DFSR_BKPT)
  249. {
  250. target->debug_reason = DBG_REASON_BREAKPOINT;
  251. if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  252. target->debug_reason = DBG_REASON_WPTANDBKPT;
  253. }
  254. else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  255. target->debug_reason = DBG_REASON_WATCHPOINT;
  256. }
  257. return ERROR_OK;
  258. }
  259. int cortex_m3_examine_exception_reason(target_t *target)
  260. {
  261. uint32_t shcsr, except_sr, cfsr = -1, except_ar = -1;
  262. /* get pointers to arch-specific information */
  263. armv7m_common_t *armv7m = target->arch_info;
  264. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  265. mem_ap_read_u32(swjdp, NVIC_SHCSR, &shcsr);
  266. switch (armv7m->exception_number)
  267. {
  268. case 2: /* NMI */
  269. break;
  270. case 3: /* Hard Fault */
  271. mem_ap_read_atomic_u32(swjdp, NVIC_HFSR, &except_sr);
  272. if (except_sr & 0x40000000)
  273. {
  274. mem_ap_read_u32(swjdp, NVIC_CFSR, &cfsr);
  275. }
  276. break;
  277. case 4: /* Memory Management */
  278. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  279. mem_ap_read_u32(swjdp, NVIC_MMFAR, &except_ar);
  280. break;
  281. case 5: /* Bus Fault */
  282. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  283. mem_ap_read_u32(swjdp, NVIC_BFAR, &except_ar);
  284. break;
  285. case 6: /* Usage Fault */
  286. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  287. break;
  288. case 11: /* SVCall */
  289. break;
  290. case 12: /* Debug Monitor */
  291. mem_ap_read_u32(swjdp, NVIC_DFSR, &except_sr);
  292. break;
  293. case 14: /* PendSV */
  294. break;
  295. case 15: /* SysTick */
  296. break;
  297. default:
  298. except_sr = 0;
  299. break;
  300. }
  301. swjdp_transaction_endcheck(swjdp);
  302. LOG_DEBUG("%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32 ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32 "", armv7m_exception_string(armv7m->exception_number), \
  303. shcsr, except_sr, cfsr, except_ar);
  304. return ERROR_OK;
  305. }
  306. int cortex_m3_debug_entry(target_t *target)
  307. {
  308. int i;
  309. uint32_t xPSR;
  310. int retval;
  311. /* get pointers to arch-specific information */
  312. armv7m_common_t *armv7m = target->arch_info;
  313. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  314. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  315. LOG_DEBUG(" ");
  316. if (armv7m->pre_debug_entry)
  317. armv7m->pre_debug_entry(target);
  318. cortex_m3_clear_halt(target);
  319. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  320. if ((retval = armv7m->examine_debug_reason(target)) != ERROR_OK)
  321. return retval;
  322. /* Examine target state and mode */
  323. /* First load register acessible through core debug port*/
  324. for (i = 0; i < ARMV7M_PRIMASK; i++)
  325. {
  326. if (!armv7m->core_cache->reg_list[i].valid)
  327. armv7m->read_core_reg(target, i);
  328. }
  329. xPSR = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32);
  330. #ifdef ARMV7_GDB_HACKS
  331. /* copy real xpsr reg for gdb, setting thumb bit */
  332. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 0, 32, xPSR);
  333. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 5, 1, 1);
  334. armv7m_gdb_dummy_cpsr_reg.valid = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
  335. armv7m_gdb_dummy_cpsr_reg.dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty;
  336. #endif
  337. /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
  338. if (xPSR & 0xf00)
  339. {
  340. armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
  341. cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR &~ 0xff);
  342. }
  343. /* Now we can load SP core registers */
  344. for (i = ARMV7M_PRIMASK; i < ARMV7NUMCOREREGS; i++)
  345. {
  346. if (!armv7m->core_cache->reg_list[i].valid)
  347. armv7m->read_core_reg(target, i);
  348. }
  349. /* Are we in an exception handler */
  350. if (xPSR & 0x1FF)
  351. {
  352. armv7m->core_mode = ARMV7M_MODE_HANDLER;
  353. armv7m->exception_number = (xPSR & 0x1FF);
  354. }
  355. else
  356. {
  357. armv7m->core_mode = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_CONTROL].value, 0, 1);
  358. armv7m->exception_number = 0;
  359. }
  360. if (armv7m->exception_number)
  361. {
  362. cortex_m3_examine_exception_reason(target);
  363. }
  364. LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
  365. armv7m_mode_strings[armv7m->core_mode],
  366. *(uint32_t*)(armv7m->core_cache->reg_list[15].value),
  367. target_state_name(target));
  368. if (armv7m->post_debug_entry)
  369. armv7m->post_debug_entry(target);
  370. return ERROR_OK;
  371. }
  372. int cortex_m3_poll(target_t *target)
  373. {
  374. int retval;
  375. enum target_state prev_target_state = target->state;
  376. /* get pointers to arch-specific information */
  377. armv7m_common_t *armv7m = target->arch_info;
  378. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  379. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  380. /* Read from Debug Halting Control and Status Register */
  381. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  382. if (retval != ERROR_OK)
  383. {
  384. target->state = TARGET_UNKNOWN;
  385. return retval;
  386. }
  387. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  388. {
  389. /* check if still in reset */
  390. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  391. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  392. {
  393. target->state = TARGET_RESET;
  394. return ERROR_OK;
  395. }
  396. }
  397. if (target->state == TARGET_RESET)
  398. {
  399. /* Cannot switch context while running so endreset is called with target->state == TARGET_RESET */
  400. LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32 "", cortex_m3->dcb_dhcsr);
  401. cortex_m3_endreset_event(target);
  402. target->state = TARGET_RUNNING;
  403. prev_target_state = TARGET_RUNNING;
  404. }
  405. if (cortex_m3->dcb_dhcsr & S_HALT)
  406. {
  407. target->state = TARGET_HALTED;
  408. if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET))
  409. {
  410. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  411. return retval;
  412. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  413. }
  414. if (prev_target_state == TARGET_DEBUG_RUNNING)
  415. {
  416. LOG_DEBUG(" ");
  417. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  418. return retval;
  419. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  420. }
  421. }
  422. /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
  423. * How best to model low power modes?
  424. */
  425. if (target->state == TARGET_UNKNOWN)
  426. {
  427. /* check if processor is retiring instructions */
  428. if (cortex_m3->dcb_dhcsr & S_RETIRE_ST)
  429. {
  430. target->state = TARGET_RUNNING;
  431. return ERROR_OK;
  432. }
  433. }
  434. return ERROR_OK;
  435. }
  436. int cortex_m3_halt(target_t *target)
  437. {
  438. LOG_DEBUG("target->state: %s",
  439. target_state_name(target));
  440. if (target->state == TARGET_HALTED)
  441. {
  442. LOG_DEBUG("target was already halted");
  443. return ERROR_OK;
  444. }
  445. if (target->state == TARGET_UNKNOWN)
  446. {
  447. LOG_WARNING("target was in unknown state when halt was requested");
  448. }
  449. if (target->state == TARGET_RESET)
  450. {
  451. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
  452. {
  453. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  454. return ERROR_TARGET_FAILURE;
  455. }
  456. else
  457. {
  458. /* we came here in a reset_halt or reset_init sequence
  459. * debug entry was already prepared in cortex_m3_prepare_reset_halt()
  460. */
  461. target->debug_reason = DBG_REASON_DBGRQ;
  462. return ERROR_OK;
  463. }
  464. }
  465. /* Write to Debug Halting Control and Status Register */
  466. cortex_m3_write_debug_halt_mask(target, C_HALT, 0);
  467. target->debug_reason = DBG_REASON_DBGRQ;
  468. return ERROR_OK;
  469. }
  470. int cortex_m3_soft_reset_halt(struct target_s *target)
  471. {
  472. /* get pointers to arch-specific information */
  473. armv7m_common_t *armv7m = target->arch_info;
  474. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  475. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  476. uint32_t dcb_dhcsr = 0;
  477. int retval, timeout = 0;
  478. /* Enter debug state on reset, cf. end_reset_event() */
  479. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  480. /* Request a reset */
  481. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_VECTRESET);
  482. target->state = TARGET_RESET;
  483. /* registers are now invalid */
  484. armv7m_invalidate_core_regs(target);
  485. while (timeout < 100)
  486. {
  487. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
  488. if (retval == ERROR_OK)
  489. {
  490. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  491. if ((dcb_dhcsr & S_HALT) && (cortex_m3->nvic_dfsr & DFSR_VCATCH))
  492. {
  493. LOG_DEBUG("system reset-halted, dcb_dhcsr 0x%" PRIx32 ", nvic_dfsr 0x%" PRIx32 "", dcb_dhcsr, cortex_m3->nvic_dfsr);
  494. cortex_m3_poll(target);
  495. return ERROR_OK;
  496. }
  497. else
  498. LOG_DEBUG("waiting for system reset-halt, dcb_dhcsr 0x%" PRIx32 ", %i ms", dcb_dhcsr, timeout);
  499. }
  500. timeout++;
  501. alive_sleep(1);
  502. }
  503. return ERROR_OK;
  504. }
  505. int cortex_m3_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
  506. {
  507. /* get pointers to arch-specific information */
  508. armv7m_common_t *armv7m = target->arch_info;
  509. breakpoint_t *breakpoint = NULL;
  510. uint32_t resume_pc;
  511. if (target->state != TARGET_HALTED)
  512. {
  513. LOG_WARNING("target not halted");
  514. return ERROR_TARGET_NOT_HALTED;
  515. }
  516. if (!debug_execution)
  517. {
  518. target_free_all_working_areas(target);
  519. cortex_m3_enable_breakpoints(target);
  520. cortex_m3_enable_watchpoints(target);
  521. }
  522. if (debug_execution)
  523. {
  524. /* Disable interrupts */
  525. /* We disable interrupts in the PRIMASK register instead of masking with C_MASKINTS,
  526. * This is probably the same issue as Cortex-M3 Errata 377493:
  527. * C_MASKINTS in parallel with disabled interrupts can cause local faults to not be taken. */
  528. buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
  529. armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = 1;
  530. armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = 1;
  531. /* Make sure we are in Thumb mode */
  532. buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32,
  533. buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32) | (1 << 24));
  534. armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = 1;
  535. armv7m->core_cache->reg_list[ARMV7M_xPSR].valid = 1;
  536. }
  537. /* current = 1: continue on current pc, otherwise continue at <address> */
  538. if (!current)
  539. {
  540. buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
  541. armv7m->core_cache->reg_list[15].dirty = 1;
  542. armv7m->core_cache->reg_list[15].valid = 1;
  543. }
  544. resume_pc = buf_get_u32(armv7m->core_cache->reg_list[15].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. armv7m_invalidate_core_regs(target);
  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. int cortex_m3_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
  581. {
  582. /* get pointers to arch-specific information */
  583. armv7m_common_t *armv7m = target->arch_info;
  584. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  585. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  586. breakpoint_t *breakpoint = NULL;
  587. if (target->state != TARGET_HALTED)
  588. {
  589. LOG_WARNING("target not halted");
  590. return ERROR_TARGET_NOT_HALTED;
  591. }
  592. /* current = 1: continue on current pc, otherwise continue at <address> */
  593. if (!current)
  594. buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
  595. /* the front-end may request us not to handle breakpoints */
  596. if (handle_breakpoints)
  597. if ((breakpoint = breakpoint_find(target, buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32))))
  598. cortex_m3_unset_breakpoint(target, breakpoint);
  599. target->debug_reason = DBG_REASON_SINGLESTEP;
  600. armv7m_restore_context(target);
  601. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  602. /* set step and clear halt */
  603. cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
  604. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  605. /* registers are now invalid */
  606. armv7m_invalidate_core_regs(target);
  607. if (breakpoint)
  608. cortex_m3_set_breakpoint(target, breakpoint);
  609. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  610. cortex_m3_debug_entry(target);
  611. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  612. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32 " nvic_icsr = 0x%" PRIx32 "", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  613. return ERROR_OK;
  614. }
  615. int cortex_m3_assert_reset(target_t *target)
  616. {
  617. armv7m_common_t *armv7m = target->arch_info;
  618. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  619. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  620. int assert_srst = 1;
  621. LOG_DEBUG("target->state: %s",
  622. target_state_name(target));
  623. enum reset_types jtag_reset_config = jtag_get_reset_config();
  624. if (!(jtag_reset_config & RESET_HAS_SRST))
  625. {
  626. LOG_ERROR("Can't assert SRST");
  627. return ERROR_FAIL;
  628. }
  629. /* Enable debug requests */
  630. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  631. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  632. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  633. mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
  634. if (!target->reset_halt)
  635. {
  636. /* Set/Clear C_MASKINTS in a separate operation */
  637. if (cortex_m3->dcb_dhcsr & C_MASKINTS)
  638. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN | C_HALT);
  639. /* clear any debug flags before resuming */
  640. cortex_m3_clear_halt(target);
  641. /* clear C_HALT in dhcsr reg */
  642. cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
  643. /* Enter debug state on reset, cf. end_reset_event() */
  644. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
  645. }
  646. else
  647. {
  648. /* Enter debug state on reset, cf. end_reset_event() */
  649. mem_ap_write_atomic_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  650. }
  651. /* following hack is to handle luminary reset
  652. * when srst is asserted the luminary device seesm to also clear the debug registers
  653. * which does not match the armv7 debug TRM */
  654. if (strcmp(target->variant, "lm3s") == 0)
  655. {
  656. /* get revision of lm3s target, only early silicon has this issue
  657. * Fury Rev B, DustDevil Rev B, Tempest all ok */
  658. uint32_t did0;
  659. if (target_read_u32(target, 0x400fe000, &did0) == ERROR_OK)
  660. {
  661. switch ((did0 >> 16) & 0xff)
  662. {
  663. case 0:
  664. /* all Sandstorm suffer issue */
  665. assert_srst = 0;
  666. break;
  667. case 1:
  668. case 3:
  669. /* only Fury/DustDevil rev A suffer reset problems */
  670. if (((did0 >> 8) & 0xff) == 0)
  671. assert_srst = 0;
  672. break;
  673. }
  674. }
  675. }
  676. if (assert_srst)
  677. {
  678. /* default to asserting srst */
  679. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  680. {
  681. jtag_add_reset(1, 1);
  682. }
  683. else
  684. {
  685. jtag_add_reset(0, 1);
  686. }
  687. }
  688. else
  689. {
  690. /* this causes the luminary device to reset using the watchdog */
  691. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  692. LOG_DEBUG("Using Luminary Reset: SYSRESETREQ");
  693. {
  694. /* I do not know why this is necessary, but it fixes strange effects
  695. * (step/resume cause a NMI after reset) on LM3S6918 -- Michael Schwingen */
  696. uint32_t tmp;
  697. mem_ap_read_atomic_u32(swjdp, NVIC_AIRCR, &tmp);
  698. }
  699. }
  700. target->state = TARGET_RESET;
  701. jtag_add_sleep(50000);
  702. armv7m_invalidate_core_regs(target);
  703. if (target->reset_halt)
  704. {
  705. int retval;
  706. if ((retval = target_halt(target)) != ERROR_OK)
  707. return retval;
  708. }
  709. return ERROR_OK;
  710. }
  711. int cortex_m3_deassert_reset(target_t *target)
  712. {
  713. LOG_DEBUG("target->state: %s",
  714. target_state_name(target));
  715. /* deassert reset lines */
  716. jtag_add_reset(0, 0);
  717. return ERROR_OK;
  718. }
  719. void cortex_m3_enable_breakpoints(struct target_s *target)
  720. {
  721. breakpoint_t *breakpoint = target->breakpoints;
  722. /* set any pending breakpoints */
  723. while (breakpoint)
  724. {
  725. if (breakpoint->set == 0)
  726. cortex_m3_set_breakpoint(target, breakpoint);
  727. breakpoint = breakpoint->next;
  728. }
  729. }
  730. int cortex_m3_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  731. {
  732. int retval;
  733. int fp_num = 0;
  734. uint32_t hilo;
  735. /* get pointers to arch-specific information */
  736. armv7m_common_t *armv7m = target->arch_info;
  737. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  738. cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
  739. if (breakpoint->set)
  740. {
  741. LOG_WARNING("breakpoint (BPID: %d) already set", breakpoint->unique_id);
  742. return ERROR_OK;
  743. }
  744. if (cortex_m3->auto_bp_type)
  745. {
  746. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  747. }
  748. if (breakpoint->type == BKPT_HARD)
  749. {
  750. while (comparator_list[fp_num].used && (fp_num < cortex_m3->fp_num_code))
  751. fp_num++;
  752. if (fp_num >= cortex_m3->fp_num_code)
  753. {
  754. LOG_DEBUG("ERROR Can not find free FP Comparator");
  755. LOG_WARNING("ERROR Can not find free FP Comparator");
  756. exit(-1);
  757. }
  758. breakpoint->set = fp_num + 1;
  759. hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
  760. comparator_list[fp_num].used = 1;
  761. comparator_list[fp_num].fpcr_value = (breakpoint->address & 0x1FFFFFFC) | hilo | 1;
  762. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  763. LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32 "", fp_num, comparator_list[fp_num].fpcr_value);
  764. if (!cortex_m3->fpb_enabled)
  765. {
  766. LOG_DEBUG("FPB wasn't enabled, do it now");
  767. target_write_u32(target, FP_CTRL, 3);
  768. }
  769. }
  770. else if (breakpoint->type == BKPT_SOFT)
  771. {
  772. uint8_t code[4];
  773. buf_set_u32(code, 0, 32, ARMV7M_T_BKPT(0x11));
  774. if ((retval = target_read_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
  775. {
  776. return retval;
  777. }
  778. if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, code)) != ERROR_OK)
  779. {
  780. return retval;
  781. }
  782. breakpoint->set = 0x11; /* Any nice value but 0 */
  783. }
  784. LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  785. breakpoint->unique_id,
  786. (int)(breakpoint->type),
  787. breakpoint->address,
  788. breakpoint->length,
  789. breakpoint->set);
  790. return ERROR_OK;
  791. }
  792. int cortex_m3_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  793. {
  794. int retval;
  795. /* get pointers to arch-specific information */
  796. armv7m_common_t *armv7m = target->arch_info;
  797. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  798. cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
  799. if (!breakpoint->set)
  800. {
  801. LOG_WARNING("breakpoint not set");
  802. return ERROR_OK;
  803. }
  804. LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  805. breakpoint->unique_id,
  806. (int)(breakpoint->type),
  807. breakpoint->address,
  808. breakpoint->length,
  809. breakpoint->set);
  810. if (breakpoint->type == BKPT_HARD)
  811. {
  812. int fp_num = breakpoint->set - 1;
  813. if ((fp_num < 0) || (fp_num >= cortex_m3->fp_num_code))
  814. {
  815. LOG_DEBUG("Invalid FP Comparator number in breakpoint");
  816. return ERROR_OK;
  817. }
  818. comparator_list[fp_num].used = 0;
  819. comparator_list[fp_num].fpcr_value = 0;
  820. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  821. }
  822. else
  823. {
  824. /* restore original instruction (kept in target endianness) */
  825. if (breakpoint->length == 4)
  826. {
  827. if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  828. {
  829. return retval;
  830. }
  831. }
  832. else
  833. {
  834. if ((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  835. {
  836. return retval;
  837. }
  838. }
  839. }
  840. breakpoint->set = 0;
  841. return ERROR_OK;
  842. }
  843. int cortex_m3_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  844. {
  845. /* get pointers to arch-specific information */
  846. armv7m_common_t *armv7m = target->arch_info;
  847. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  848. if (cortex_m3->auto_bp_type)
  849. {
  850. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  851. #ifdef ARMV7_GDB_HACKS
  852. if (breakpoint->length != 2) {
  853. /* XXX Hack: Replace all breakpoints with length != 2 with
  854. * a hardware breakpoint. */
  855. breakpoint->type = BKPT_HARD;
  856. breakpoint->length = 2;
  857. }
  858. #endif
  859. }
  860. if ((breakpoint->type == BKPT_HARD) && (breakpoint->address >= 0x20000000))
  861. {
  862. LOG_INFO("flash patch comparator requested outside code memory region");
  863. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  864. }
  865. if ((breakpoint->type == BKPT_SOFT) && (breakpoint->address < 0x20000000))
  866. {
  867. LOG_INFO("soft breakpoint requested in code (flash) memory region");
  868. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  869. }
  870. if ((breakpoint->type == BKPT_HARD) && (cortex_m3->fp_code_available < 1))
  871. {
  872. LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
  873. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  874. }
  875. if ((breakpoint->length != 2))
  876. {
  877. LOG_INFO("only breakpoints of two bytes length supported");
  878. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  879. }
  880. if (breakpoint->type == BKPT_HARD)
  881. cortex_m3->fp_code_available--;
  882. cortex_m3_set_breakpoint(target, breakpoint);
  883. return ERROR_OK;
  884. }
  885. int cortex_m3_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  886. {
  887. /* get pointers to arch-specific information */
  888. armv7m_common_t *armv7m = target->arch_info;
  889. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  890. if (target->state != TARGET_HALTED)
  891. {
  892. LOG_WARNING("target not halted");
  893. return ERROR_TARGET_NOT_HALTED;
  894. }
  895. if (cortex_m3->auto_bp_type)
  896. {
  897. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  898. }
  899. if (breakpoint->set)
  900. {
  901. cortex_m3_unset_breakpoint(target, breakpoint);
  902. }
  903. if (breakpoint->type == BKPT_HARD)
  904. cortex_m3->fp_code_available++;
  905. return ERROR_OK;
  906. }
  907. int cortex_m3_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  908. {
  909. int dwt_num = 0;
  910. uint32_t mask, temp;
  911. /* get pointers to arch-specific information */
  912. armv7m_common_t *armv7m = target->arch_info;
  913. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  914. cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
  915. if (watchpoint->set)
  916. {
  917. LOG_WARNING("watchpoint (%d) already set", watchpoint->unique_id );
  918. return ERROR_OK;
  919. }
  920. if (watchpoint->mask == 0xffffffffu)
  921. {
  922. while (comparator_list[dwt_num].used && (dwt_num < cortex_m3->dwt_num_comp))
  923. dwt_num++;
  924. if (dwt_num >= cortex_m3->dwt_num_comp)
  925. {
  926. LOG_DEBUG("ERROR Can not find free DWT Comparator");
  927. LOG_WARNING("ERROR Can not find free DWT Comparator");
  928. return -1;
  929. }
  930. watchpoint->set = dwt_num + 1;
  931. mask = 0;
  932. temp = watchpoint->length;
  933. while (temp > 1)
  934. {
  935. temp = temp / 2;
  936. mask++;
  937. }
  938. comparator_list[dwt_num].used = 1;
  939. comparator_list[dwt_num].comp = watchpoint->address;
  940. comparator_list[dwt_num].mask = mask;
  941. comparator_list[dwt_num].function = watchpoint->rw + 5;
  942. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address, comparator_list[dwt_num].comp);
  943. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x4, comparator_list[dwt_num].mask);
  944. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x8, comparator_list[dwt_num].function);
  945. LOG_DEBUG("dwt_num %i 0x%" PRIx32 " 0x%" PRIx32 " 0x%" PRIx32 "", dwt_num, comparator_list[dwt_num].comp, comparator_list[dwt_num].mask, comparator_list[dwt_num].function);
  946. }
  947. else
  948. {
  949. /* Move this test to add_watchpoint */
  950. LOG_WARNING("Cannot watch data values (id: %d)",
  951. watchpoint->unique_id );
  952. return ERROR_OK;
  953. }
  954. LOG_DEBUG("Watchpoint (ID: %d) address: 0x%08" PRIx32 " set=%d ",
  955. watchpoint->unique_id, watchpoint->address, watchpoint->set );
  956. return ERROR_OK;
  957. }
  958. int cortex_m3_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  959. {
  960. /* get pointers to arch-specific information */
  961. armv7m_common_t *armv7m = target->arch_info;
  962. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  963. cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
  964. int dwt_num;
  965. if (!watchpoint->set)
  966. {
  967. LOG_WARNING("watchpoint (wpid: %d) not set", watchpoint->unique_id );
  968. return ERROR_OK;
  969. }
  970. LOG_DEBUG("Watchpoint (ID: %d) address: 0x%08" PRIx32 " set=%d ",
  971. watchpoint->unique_id, watchpoint->address,watchpoint->set );
  972. dwt_num = watchpoint->set - 1;
  973. if ((dwt_num < 0) || (dwt_num >= cortex_m3->dwt_num_comp))
  974. {
  975. LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
  976. return ERROR_OK;
  977. }
  978. comparator_list[dwt_num].used = 0;
  979. comparator_list[dwt_num].function = 0;
  980. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address | 0x8, comparator_list[dwt_num].function);
  981. watchpoint->set = 0;
  982. return ERROR_OK;
  983. }
  984. int cortex_m3_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  985. {
  986. /* get pointers to arch-specific information */
  987. armv7m_common_t *armv7m = target->arch_info;
  988. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  989. if (target->state != TARGET_HALTED)
  990. {
  991. LOG_WARNING("target not halted");
  992. return ERROR_TARGET_NOT_HALTED;
  993. }
  994. if (cortex_m3->dwt_comp_available < 1)
  995. {
  996. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  997. }
  998. if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
  999. {
  1000. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1001. }
  1002. cortex_m3->dwt_comp_available--;
  1003. LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
  1004. return ERROR_OK;
  1005. }
  1006. int cortex_m3_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  1007. {
  1008. /* get pointers to arch-specific information */
  1009. armv7m_common_t *armv7m = target->arch_info;
  1010. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  1011. if (target->state != TARGET_HALTED)
  1012. {
  1013. LOG_WARNING("target not halted");
  1014. return ERROR_TARGET_NOT_HALTED;
  1015. }
  1016. if (watchpoint->set)
  1017. {
  1018. cortex_m3_unset_watchpoint(target, watchpoint);
  1019. }
  1020. cortex_m3->dwt_comp_available++;
  1021. LOG_DEBUG("dwt_comp_available: %d", cortex_m3->dwt_comp_available);
  1022. return ERROR_OK;
  1023. }
  1024. void cortex_m3_enable_watchpoints(struct target_s *target)
  1025. {
  1026. watchpoint_t *watchpoint = target->watchpoints;
  1027. /* set any pending watchpoints */
  1028. while (watchpoint)
  1029. {
  1030. if (watchpoint->set == 0)
  1031. cortex_m3_set_watchpoint(target, watchpoint);
  1032. watchpoint = watchpoint->next;
  1033. }
  1034. }
  1035. int cortex_m3_load_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t * value)
  1036. {
  1037. int retval;
  1038. /* get pointers to arch-specific information */
  1039. armv7m_common_t *armv7m = target->arch_info;
  1040. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1041. if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
  1042. {
  1043. /* read a normal core register */
  1044. retval = cortexm3_dap_read_coreregister_u32(swjdp, value, num);
  1045. if (retval != ERROR_OK)
  1046. {
  1047. LOG_ERROR("JTAG failure %i",retval);
  1048. return ERROR_JTAG_DEVICE_ERROR;
  1049. }
  1050. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "",(int)num,*value);
  1051. }
  1052. else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
  1053. {
  1054. /* read other registers */
  1055. cortexm3_dap_read_coreregister_u32(swjdp, value, 20);
  1056. switch (num)
  1057. {
  1058. case 19:
  1059. *value = buf_get_u32((uint8_t*)value, 0, 8);
  1060. break;
  1061. case 20:
  1062. *value = buf_get_u32((uint8_t*)value, 8, 8);
  1063. break;
  1064. case 21:
  1065. *value = buf_get_u32((uint8_t*)value, 16, 8);
  1066. break;
  1067. case 22:
  1068. *value = buf_get_u32((uint8_t*)value, 24, 8);
  1069. break;
  1070. }
  1071. LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
  1072. }
  1073. else
  1074. {
  1075. return ERROR_INVALID_ARGUMENTS;
  1076. }
  1077. return ERROR_OK;
  1078. }
  1079. int cortex_m3_store_core_reg_u32(struct target_s *target, enum armv7m_regtype type, uint32_t num, uint32_t value)
  1080. {
  1081. int retval;
  1082. uint32_t reg;
  1083. /* get pointers to arch-specific information */
  1084. armv7m_common_t *armv7m = target->arch_info;
  1085. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1086. #ifdef ARMV7_GDB_HACKS
  1087. /* If the LR register is being modified, make sure it will put us
  1088. * in "thumb" mode, or an INVSTATE exception will occur. This is a
  1089. * hack to deal with the fact that gdb will sometimes "forge"
  1090. * return addresses, and doesn't set the LSB correctly (i.e., when
  1091. * printing expressions containing function calls, it sets LR = 0.) */
  1092. if (num == 14)
  1093. value |= 0x01;
  1094. #endif
  1095. if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
  1096. {
  1097. retval = cortexm3_dap_write_coreregister_u32(swjdp, value, num);
  1098. if (retval != ERROR_OK)
  1099. {
  1100. LOG_ERROR("JTAG failure %i", retval);
  1101. armv7m->core_cache->reg_list[num].dirty = armv7m->core_cache->reg_list[num].valid;
  1102. return ERROR_JTAG_DEVICE_ERROR;
  1103. }
  1104. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  1105. }
  1106. else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
  1107. {
  1108. /* write other registers */
  1109. cortexm3_dap_read_coreregister_u32(swjdp, &reg, 20);
  1110. switch (num)
  1111. {
  1112. case 19:
  1113. buf_set_u32((uint8_t*)&reg, 0, 8, value);
  1114. break;
  1115. case 20:
  1116. buf_set_u32((uint8_t*)&reg, 8, 8, value);
  1117. break;
  1118. case 21:
  1119. buf_set_u32((uint8_t*)&reg, 16, 8, value);
  1120. break;
  1121. case 22:
  1122. buf_set_u32((uint8_t*)&reg, 24, 8, value);
  1123. break;
  1124. }
  1125. cortexm3_dap_write_coreregister_u32(swjdp, reg, 20);
  1126. LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
  1127. }
  1128. else
  1129. {
  1130. return ERROR_INVALID_ARGUMENTS;
  1131. }
  1132. return ERROR_OK;
  1133. }
  1134. int cortex_m3_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  1135. {
  1136. /* get pointers to arch-specific information */
  1137. armv7m_common_t *armv7m = target->arch_info;
  1138. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1139. int retval;
  1140. /* sanitize arguments */
  1141. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1142. return ERROR_INVALID_ARGUMENTS;
  1143. /* cortex_m3 handles unaligned memory access */
  1144. switch (size)
  1145. {
  1146. case 4:
  1147. retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
  1148. break;
  1149. case 2:
  1150. retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
  1151. break;
  1152. case 1:
  1153. retval = mem_ap_read_buf_u8(swjdp, buffer, count, address);
  1154. break;
  1155. default:
  1156. LOG_ERROR("BUG: we shouldn't get here");
  1157. exit(-1);
  1158. }
  1159. return retval;
  1160. }
  1161. int cortex_m3_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  1162. {
  1163. /* get pointers to arch-specific information */
  1164. armv7m_common_t *armv7m = target->arch_info;
  1165. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1166. int retval;
  1167. /* sanitize arguments */
  1168. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1169. return ERROR_INVALID_ARGUMENTS;
  1170. switch (size)
  1171. {
  1172. case 4:
  1173. retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
  1174. break;
  1175. case 2:
  1176. retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
  1177. break;
  1178. case 1:
  1179. retval = mem_ap_write_buf_u8(swjdp, buffer, count, address);
  1180. break;
  1181. default:
  1182. LOG_ERROR("BUG: we shouldn't get here");
  1183. exit(-1);
  1184. }
  1185. return retval;
  1186. }
  1187. int cortex_m3_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
  1188. {
  1189. return cortex_m3_write_memory(target, address, 4, count, buffer);
  1190. }
  1191. void cortex_m3_build_reg_cache(target_t *target)
  1192. {
  1193. armv7m_build_reg_cache(target);
  1194. }
  1195. int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  1196. {
  1197. cortex_m3_build_reg_cache(target);
  1198. return ERROR_OK;
  1199. }
  1200. int cortex_m3_examine(struct target_s *target)
  1201. {
  1202. int retval;
  1203. uint32_t cpuid, fpcr, dwtcr, ictr;
  1204. int i;
  1205. /* get pointers to arch-specific information */
  1206. armv7m_common_t *armv7m = target->arch_info;
  1207. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  1208. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1209. if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
  1210. return retval;
  1211. if (!target_was_examined(target))
  1212. {
  1213. target_set_examined(target);
  1214. /* Read from Device Identification Registers */
  1215. if ((retval = target_read_u32(target, CPUID, &cpuid)) != ERROR_OK)
  1216. return retval;
  1217. if (((cpuid >> 4) & 0xc3f) == 0xc23)
  1218. LOG_DEBUG("CORTEX-M3 processor detected");
  1219. LOG_DEBUG("cpuid: 0x%8.8" PRIx32 "", cpuid);
  1220. target_read_u32(target, NVIC_ICTR, &ictr);
  1221. cortex_m3->intlinesnum = (ictr & 0x1F) + 1;
  1222. cortex_m3->intsetenable = calloc(cortex_m3->intlinesnum, 4);
  1223. for (i = 0; i < cortex_m3->intlinesnum; i++)
  1224. {
  1225. target_read_u32(target, NVIC_ISE0 + 4 * i, cortex_m3->intsetenable + i);
  1226. LOG_DEBUG("interrupt enable[%i] = 0x%8.8" PRIx32 "", i, cortex_m3->intsetenable[i]);
  1227. }
  1228. /* Setup FPB */
  1229. target_read_u32(target, FP_CTRL, &fpcr);
  1230. cortex_m3->auto_bp_type = 1;
  1231. cortex_m3->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF); /* bits [14:12] and [7:4] */
  1232. cortex_m3->fp_num_lit = (fpcr >> 8) & 0xF;
  1233. cortex_m3->fp_code_available = cortex_m3->fp_num_code;
  1234. cortex_m3->fp_comparator_list = calloc(cortex_m3->fp_num_code + cortex_m3->fp_num_lit, sizeof(cortex_m3_fp_comparator_t));
  1235. cortex_m3->fpb_enabled = fpcr & 1;
  1236. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  1237. {
  1238. cortex_m3->fp_comparator_list[i].type = (i < cortex_m3->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
  1239. cortex_m3->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
  1240. }
  1241. LOG_DEBUG("FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i", fpcr, cortex_m3->fp_num_code, cortex_m3->fp_num_lit);
  1242. /* Setup DWT */
  1243. target_read_u32(target, DWT_CTRL, &dwtcr);
  1244. cortex_m3->dwt_num_comp = (dwtcr >> 28) & 0xF;
  1245. cortex_m3->dwt_comp_available = cortex_m3->dwt_num_comp;
  1246. cortex_m3->dwt_comparator_list = calloc(cortex_m3->dwt_num_comp, sizeof(cortex_m3_dwt_comparator_t));
  1247. for (i = 0; i < cortex_m3->dwt_num_comp; i++)
  1248. {
  1249. cortex_m3->dwt_comparator_list[i].dwt_comparator_address = DWT_COMP0 + 0x10 * i;
  1250. }
  1251. }
  1252. return ERROR_OK;
  1253. }
  1254. int cortex_m3_quit(void)
  1255. {
  1256. return ERROR_OK;
  1257. }
  1258. int cortex_m3_dcc_read(swjdp_common_t *swjdp, uint8_t *value, uint8_t *ctrl)
  1259. {
  1260. uint16_t dcrdr;
  1261. mem_ap_read_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
  1262. *ctrl = (uint8_t)dcrdr;
  1263. *value = (uint8_t)(dcrdr >> 8);
  1264. LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
  1265. /* write ack back to software dcc register
  1266. * signify we have read data */
  1267. if (dcrdr & (1 << 0))
  1268. {
  1269. dcrdr = 0;
  1270. mem_ap_write_buf_u16(swjdp, (uint8_t*)&dcrdr, 1, DCB_DCRDR);
  1271. }
  1272. return ERROR_OK;
  1273. }
  1274. int cortex_m3_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
  1275. {
  1276. armv7m_common_t *armv7m = target->arch_info;
  1277. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1278. uint8_t data;
  1279. uint8_t ctrl;
  1280. uint32_t i;
  1281. for (i = 0; i < (size * 4); i++)
  1282. {
  1283. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1284. buffer[i] = data;
  1285. }
  1286. return ERROR_OK;
  1287. }
  1288. int cortex_m3_handle_target_request(void *priv)
  1289. {
  1290. target_t *target = priv;
  1291. if (!target_was_examined(target))
  1292. return ERROR_OK;
  1293. armv7m_common_t *armv7m = target->arch_info;
  1294. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1295. if (!target->dbg_msg_enabled)
  1296. return ERROR_OK;
  1297. if (target->state == TARGET_RUNNING)
  1298. {
  1299. uint8_t data;
  1300. uint8_t ctrl;
  1301. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1302. /* check if we have data */
  1303. if (ctrl & (1 << 0))
  1304. {
  1305. uint32_t request;
  1306. /* we assume target is quick enough */
  1307. request = data;
  1308. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1309. request |= (data << 8);
  1310. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1311. request |= (data << 16);
  1312. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1313. request |= (data << 24);
  1314. target_request(target, request);
  1315. }
  1316. }
  1317. return ERROR_OK;
  1318. }
  1319. int cortex_m3_init_arch_info(target_t *target, cortex_m3_common_t *cortex_m3, jtag_tap_t *tap)
  1320. {
  1321. int retval;
  1322. armv7m_common_t *armv7m;
  1323. armv7m = &cortex_m3->armv7m;
  1324. armv7m_init_arch_info(target, armv7m);
  1325. /* prepare JTAG information for the new target */
  1326. cortex_m3->jtag_info.tap = tap;
  1327. cortex_m3->jtag_info.scann_size = 4;
  1328. armv7m->swjdp_info.dp_select_value = -1;
  1329. armv7m->swjdp_info.ap_csw_value = -1;
  1330. armv7m->swjdp_info.ap_tar_value = -1;
  1331. armv7m->swjdp_info.jtag_info = &cortex_m3->jtag_info;
  1332. armv7m->swjdp_info.memaccess_tck = 8;
  1333. armv7m->swjdp_info.tar_autoincr_block = (1 << 12); /* Cortex-M3 has 4096 bytes autoincrement range */
  1334. /* initialize arch-specific breakpoint handling */
  1335. cortex_m3->common_magic = CORTEX_M3_COMMON_MAGIC;
  1336. cortex_m3->arch_info = NULL;
  1337. /* register arch-specific functions */
  1338. armv7m->examine_debug_reason = cortex_m3_examine_debug_reason;
  1339. armv7m->pre_debug_entry = NULL;
  1340. armv7m->post_debug_entry = NULL;
  1341. armv7m->pre_restore_context = NULL;
  1342. armv7m->post_restore_context = NULL;
  1343. armv7m->arch_info = cortex_m3;
  1344. armv7m->load_core_reg_u32 = cortex_m3_load_core_reg_u32;
  1345. armv7m->store_core_reg_u32 = cortex_m3_store_core_reg_u32;
  1346. target_register_timer_callback(cortex_m3_handle_target_request, 1, 1, target);
  1347. if ((retval = arm_jtag_setup_connection(&cortex_m3->jtag_info)) != ERROR_OK)
  1348. {
  1349. return retval;
  1350. }
  1351. return ERROR_OK;
  1352. }
  1353. int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp)
  1354. {
  1355. cortex_m3_common_t *cortex_m3 = calloc(1,sizeof(cortex_m3_common_t));
  1356. cortex_m3_init_arch_info(target, cortex_m3, target->tap);
  1357. return ERROR_OK;
  1358. }
  1359. /*
  1360. * REVISIT Thumb2 disassembly should work for all ARMv7 cores, as well
  1361. * as at least ARM-1156T2. The interesting thing about Cortex-M is
  1362. * that *only* Thumb2 disassembly matters. There are also some small
  1363. * additions to Thumb2 that are specific to ARMv7-M.
  1364. */
  1365. static int
  1366. handle_cortex_m3_disassemble_command(struct command_context_s *cmd_ctx,
  1367. char *cmd, char **args, int argc)
  1368. {
  1369. int retval = ERROR_OK;
  1370. target_t *target = get_current_target(cmd_ctx);
  1371. uint32_t address;
  1372. unsigned long count;
  1373. arm_instruction_t cur_instruction;
  1374. if (argc != 2) {
  1375. command_print(cmd_ctx,
  1376. "usage: cortex_m3 disassemble <address> <count>");
  1377. return ERROR_OK;
  1378. }
  1379. errno = 0;
  1380. address = strtoul(args[0], NULL, 0);
  1381. if (errno)
  1382. return ERROR_FAIL;
  1383. count = strtoul(args[1], NULL, 0);
  1384. if (errno)
  1385. return ERROR_FAIL;
  1386. while (count--) {
  1387. retval = thumb2_opcode(target, address, &cur_instruction);
  1388. if (retval != ERROR_OK)
  1389. return retval;
  1390. command_print(cmd_ctx, "%s", cur_instruction.text);
  1391. address += cur_instruction.instruction_size;
  1392. }
  1393. return ERROR_OK;
  1394. }
  1395. int cortex_m3_register_commands(struct command_context_s *cmd_ctx)
  1396. {
  1397. int retval;
  1398. command_t *cortex_m3_cmd;
  1399. retval = armv7m_register_commands(cmd_ctx);
  1400. cortex_m3_cmd = register_command(cmd_ctx, NULL, "cortex_m3",
  1401. NULL, COMMAND_ANY, "cortex_m3 specific commands");
  1402. register_command(cmd_ctx, cortex_m3_cmd, "disassemble",
  1403. handle_cortex_m3_disassemble_command, COMMAND_EXEC,
  1404. "disassemble Thumb2 instructions <address> <count>");
  1405. register_command(cmd_ctx, cortex_m3_cmd, "maskisr",
  1406. handle_cortex_m3_mask_interrupts_command, COMMAND_EXEC,
  1407. "mask cortex_m3 interrupts ['on'|'off']");
  1408. return retval;
  1409. }
  1410. int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1411. {
  1412. target_t *target = get_current_target(cmd_ctx);
  1413. armv7m_common_t *armv7m = target->arch_info;
  1414. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  1415. if (target->state != TARGET_HALTED)
  1416. {
  1417. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  1418. return ERROR_OK;
  1419. }
  1420. if (argc > 0)
  1421. {
  1422. if (!strcmp(args[0], "on"))
  1423. {
  1424. cortex_m3_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
  1425. }
  1426. else if (!strcmp(args[0], "off"))
  1427. {
  1428. cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
  1429. }
  1430. else
  1431. {
  1432. command_print(cmd_ctx, "usage: cortex_m3 maskisr ['on'|'off']");
  1433. }
  1434. }
  1435. command_print(cmd_ctx, "cortex_m3 interrupt mask %s",
  1436. (cortex_m3->dcb_dhcsr & C_MASKINTS) ? "on" : "off");
  1437. return ERROR_OK;
  1438. }