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.
 
 
 
 
 
 

2461 lines
70 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, see <http://www.gnu.org/licenses/>. *
  23. * *
  24. * *
  25. * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
  26. * *
  27. ***************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "jtag/interface.h"
  32. #include "breakpoints.h"
  33. #include "cortex_m.h"
  34. #include "target_request.h"
  35. #include "target_type.h"
  36. #include "arm_disassembler.h"
  37. #include "register.h"
  38. #include "arm_opcodes.h"
  39. #include "arm_semihosting.h"
  40. #include <helper/time_support.h>
  41. /* NOTE: most of this should work fine for the Cortex-M1 and
  42. * Cortex-M0 cores too, although they're ARMv6-M not ARMv7-M.
  43. * Some differences: M0/M1 doesn't have FBP remapping or the
  44. * DWT tracing/profiling support. (So the cycle counter will
  45. * not be usable; the other stuff isn't currently used here.)
  46. *
  47. * Although there are some workarounds for errata seen only in r0p0
  48. * silicon, such old parts are hard to find and thus not much tested
  49. * any longer.
  50. */
  51. /**
  52. * Returns the type of a break point required by address location
  53. */
  54. #define BKPT_TYPE_BY_ADDR(addr) ((addr) < 0x20000000 ? BKPT_HARD : BKPT_SOFT)
  55. /* forward declarations */
  56. static int cortex_m_store_core_reg_u32(struct target *target,
  57. uint32_t num, uint32_t value);
  58. static void cortex_m_dwt_free(struct target *target);
  59. static int cortexm_dap_read_coreregister_u32(struct target *target,
  60. uint32_t *value, int regnum)
  61. {
  62. struct armv7m_common *armv7m = target_to_armv7m(target);
  63. int retval;
  64. uint32_t dcrdr;
  65. /* because the DCB_DCRDR is used for the emulated dcc channel
  66. * we have to save/restore the DCB_DCRDR when used */
  67. if (target->dbg_msg_enabled) {
  68. retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
  69. if (retval != ERROR_OK)
  70. return retval;
  71. }
  72. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regnum);
  73. if (retval != ERROR_OK)
  74. return retval;
  75. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR, value);
  76. if (retval != ERROR_OK)
  77. return retval;
  78. if (target->dbg_msg_enabled) {
  79. /* restore DCB_DCRDR - this needs to be in a separate
  80. * transaction otherwise the emulated DCC channel breaks */
  81. if (retval == ERROR_OK)
  82. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
  83. }
  84. return retval;
  85. }
  86. static int cortexm_dap_write_coreregister_u32(struct target *target,
  87. uint32_t value, int regnum)
  88. {
  89. struct armv7m_common *armv7m = target_to_armv7m(target);
  90. int retval;
  91. uint32_t dcrdr;
  92. /* because the DCB_DCRDR is used for the emulated dcc channel
  93. * we have to save/restore the DCB_DCRDR when used */
  94. if (target->dbg_msg_enabled) {
  95. retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
  96. if (retval != ERROR_OK)
  97. return retval;
  98. }
  99. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, value);
  100. if (retval != ERROR_OK)
  101. return retval;
  102. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRSR, regnum | DCRSR_WnR);
  103. if (retval != ERROR_OK)
  104. return retval;
  105. if (target->dbg_msg_enabled) {
  106. /* restore DCB_DCRDR - this needs to be in a seperate
  107. * transaction otherwise the emulated DCC channel breaks */
  108. if (retval == ERROR_OK)
  109. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
  110. }
  111. return retval;
  112. }
  113. static int cortex_m_write_debug_halt_mask(struct target *target,
  114. uint32_t mask_on, uint32_t mask_off)
  115. {
  116. struct cortex_m_common *cortex_m = target_to_cm(target);
  117. struct armv7m_common *armv7m = &cortex_m->armv7m;
  118. /* mask off status bits */
  119. cortex_m->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
  120. /* create new register mask */
  121. cortex_m->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
  122. return mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR, cortex_m->dcb_dhcsr);
  123. }
  124. static int cortex_m_clear_halt(struct target *target)
  125. {
  126. struct cortex_m_common *cortex_m = target_to_cm(target);
  127. struct armv7m_common *armv7m = &cortex_m->armv7m;
  128. int retval;
  129. /* clear step if any */
  130. cortex_m_write_debug_halt_mask(target, C_HALT, C_STEP);
  131. /* Read Debug Fault Status Register */
  132. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR, &cortex_m->nvic_dfsr);
  133. if (retval != ERROR_OK)
  134. return retval;
  135. /* Clear Debug Fault Status */
  136. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_DFSR, cortex_m->nvic_dfsr);
  137. if (retval != ERROR_OK)
  138. return retval;
  139. LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32 "", cortex_m->nvic_dfsr);
  140. return ERROR_OK;
  141. }
  142. static int cortex_m_single_step_core(struct target *target)
  143. {
  144. struct cortex_m_common *cortex_m = target_to_cm(target);
  145. struct armv7m_common *armv7m = &cortex_m->armv7m;
  146. uint32_t dhcsr_save;
  147. int retval;
  148. /* backup dhcsr reg */
  149. dhcsr_save = cortex_m->dcb_dhcsr;
  150. /* Mask interrupts before clearing halt, if done already. This avoids
  151. * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
  152. * HALT can put the core into an unknown state.
  153. */
  154. if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) {
  155. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
  156. DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
  157. if (retval != ERROR_OK)
  158. return retval;
  159. }
  160. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
  161. DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
  162. if (retval != ERROR_OK)
  163. return retval;
  164. LOG_DEBUG(" ");
  165. /* restore dhcsr reg */
  166. cortex_m->dcb_dhcsr = dhcsr_save;
  167. cortex_m_clear_halt(target);
  168. return ERROR_OK;
  169. }
  170. static int cortex_m_enable_fpb(struct target *target)
  171. {
  172. int retval = target_write_u32(target, FP_CTRL, 3);
  173. if (retval != ERROR_OK)
  174. return retval;
  175. /* check the fpb is actually enabled */
  176. uint32_t fpctrl;
  177. retval = target_read_u32(target, FP_CTRL, &fpctrl);
  178. if (retval != ERROR_OK)
  179. return retval;
  180. if (fpctrl & 1)
  181. return ERROR_OK;
  182. return ERROR_FAIL;
  183. }
  184. static int cortex_m_endreset_event(struct target *target)
  185. {
  186. int i;
  187. int retval;
  188. uint32_t dcb_demcr;
  189. struct cortex_m_common *cortex_m = target_to_cm(target);
  190. struct armv7m_common *armv7m = &cortex_m->armv7m;
  191. struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
  192. struct cortex_m_fp_comparator *fp_list = cortex_m->fp_comparator_list;
  193. struct cortex_m_dwt_comparator *dwt_list = cortex_m->dwt_comparator_list;
  194. /* REVISIT The four debug monitor bits are currently ignored... */
  195. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &dcb_demcr);
  196. if (retval != ERROR_OK)
  197. return retval;
  198. LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32 "", dcb_demcr);
  199. /* this register is used for emulated dcc channel */
  200. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
  201. if (retval != ERROR_OK)
  202. return retval;
  203. /* Enable debug requests */
  204. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  205. if (retval != ERROR_OK)
  206. return retval;
  207. if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
  208. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  209. if (retval != ERROR_OK)
  210. return retval;
  211. }
  212. /* clear any interrupt masking */
  213. cortex_m_write_debug_halt_mask(target, 0, C_MASKINTS);
  214. /* Enable features controlled by ITM and DWT blocks, and catch only
  215. * the vectors we were told to pay attention to.
  216. *
  217. * Target firmware is responsible for all fault handling policy
  218. * choices *EXCEPT* explicitly scripted overrides like "vector_catch"
  219. * or manual updates to the NVIC SHCSR and CCR registers.
  220. */
  221. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, TRCENA | armv7m->demcr);
  222. if (retval != ERROR_OK)
  223. return retval;
  224. /* Paranoia: evidently some (early?) chips don't preserve all the
  225. * debug state (including FBP, DWT, etc) across reset...
  226. */
  227. /* Enable FPB */
  228. retval = cortex_m_enable_fpb(target);
  229. if (retval != ERROR_OK) {
  230. LOG_ERROR("Failed to enable the FPB");
  231. return retval;
  232. }
  233. cortex_m->fpb_enabled = 1;
  234. /* Restore FPB registers */
  235. for (i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
  236. retval = target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
  237. if (retval != ERROR_OK)
  238. return retval;
  239. }
  240. /* Restore DWT registers */
  241. for (i = 0; i < cortex_m->dwt_num_comp; i++) {
  242. retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
  243. dwt_list[i].comp);
  244. if (retval != ERROR_OK)
  245. return retval;
  246. retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
  247. dwt_list[i].mask);
  248. if (retval != ERROR_OK)
  249. return retval;
  250. retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
  251. dwt_list[i].function);
  252. if (retval != ERROR_OK)
  253. return retval;
  254. }
  255. retval = dap_run(swjdp);
  256. if (retval != ERROR_OK)
  257. return retval;
  258. register_cache_invalidate(armv7m->arm.core_cache);
  259. /* make sure we have latest dhcsr flags */
  260. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  261. return retval;
  262. }
  263. static int cortex_m_examine_debug_reason(struct target *target)
  264. {
  265. struct cortex_m_common *cortex_m = target_to_cm(target);
  266. /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason
  267. * only check the debug reason if we don't know it already */
  268. if ((target->debug_reason != DBG_REASON_DBGRQ)
  269. && (target->debug_reason != DBG_REASON_SINGLESTEP)) {
  270. if (cortex_m->nvic_dfsr & DFSR_BKPT) {
  271. target->debug_reason = DBG_REASON_BREAKPOINT;
  272. if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
  273. target->debug_reason = DBG_REASON_WPTANDBKPT;
  274. } else if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
  275. target->debug_reason = DBG_REASON_WATCHPOINT;
  276. else if (cortex_m->nvic_dfsr & DFSR_VCATCH)
  277. target->debug_reason = DBG_REASON_BREAKPOINT;
  278. else /* EXTERNAL, HALTED */
  279. target->debug_reason = DBG_REASON_UNDEFINED;
  280. }
  281. return ERROR_OK;
  282. }
  283. static int cortex_m_examine_exception_reason(struct target *target)
  284. {
  285. uint32_t shcsr = 0, except_sr = 0, cfsr = -1, except_ar = -1;
  286. struct armv7m_common *armv7m = target_to_armv7m(target);
  287. struct adiv5_dap *swjdp = armv7m->arm.dap;
  288. int retval;
  289. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SHCSR, &shcsr);
  290. if (retval != ERROR_OK)
  291. return retval;
  292. switch (armv7m->exception_number) {
  293. case 2: /* NMI */
  294. break;
  295. case 3: /* Hard Fault */
  296. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_HFSR, &except_sr);
  297. if (retval != ERROR_OK)
  298. return retval;
  299. if (except_sr & 0x40000000) {
  300. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &cfsr);
  301. if (retval != ERROR_OK)
  302. return retval;
  303. }
  304. break;
  305. case 4: /* Memory Management */
  306. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
  307. if (retval != ERROR_OK)
  308. return retval;
  309. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_MMFAR, &except_ar);
  310. if (retval != ERROR_OK)
  311. return retval;
  312. break;
  313. case 5: /* Bus Fault */
  314. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
  315. if (retval != ERROR_OK)
  316. return retval;
  317. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_BFAR, &except_ar);
  318. if (retval != ERROR_OK)
  319. return retval;
  320. break;
  321. case 6: /* Usage Fault */
  322. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
  323. if (retval != ERROR_OK)
  324. return retval;
  325. break;
  326. case 11: /* SVCall */
  327. break;
  328. case 12: /* Debug Monitor */
  329. retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_DFSR, &except_sr);
  330. if (retval != ERROR_OK)
  331. return retval;
  332. break;
  333. case 14: /* PendSV */
  334. break;
  335. case 15: /* SysTick */
  336. break;
  337. default:
  338. except_sr = 0;
  339. break;
  340. }
  341. retval = dap_run(swjdp);
  342. if (retval == ERROR_OK)
  343. LOG_DEBUG("%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32
  344. ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32,
  345. armv7m_exception_string(armv7m->exception_number),
  346. shcsr, except_sr, cfsr, except_ar);
  347. return retval;
  348. }
  349. static int cortex_m_debug_entry(struct target *target)
  350. {
  351. int i;
  352. uint32_t xPSR;
  353. int retval;
  354. struct cortex_m_common *cortex_m = target_to_cm(target);
  355. struct armv7m_common *armv7m = &cortex_m->armv7m;
  356. struct arm *arm = &armv7m->arm;
  357. struct reg *r;
  358. LOG_DEBUG(" ");
  359. cortex_m_clear_halt(target);
  360. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  361. if (retval != ERROR_OK)
  362. return retval;
  363. retval = armv7m->examine_debug_reason(target);
  364. if (retval != ERROR_OK)
  365. return retval;
  366. /* Examine target state and mode
  367. * First load register accessible through core debug port */
  368. int num_regs = arm->core_cache->num_regs;
  369. for (i = 0; i < num_regs; i++) {
  370. r = &armv7m->arm.core_cache->reg_list[i];
  371. if (!r->valid)
  372. arm->read_core_reg(target, r, i, ARM_MODE_ANY);
  373. }
  374. r = arm->cpsr;
  375. xPSR = buf_get_u32(r->value, 0, 32);
  376. /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
  377. if (xPSR & 0xf00) {
  378. r->dirty = r->valid;
  379. cortex_m_store_core_reg_u32(target, 16, xPSR & ~0xff);
  380. }
  381. /* Are we in an exception handler */
  382. if (xPSR & 0x1FF) {
  383. armv7m->exception_number = (xPSR & 0x1FF);
  384. arm->core_mode = ARM_MODE_HANDLER;
  385. arm->map = armv7m_msp_reg_map;
  386. } else {
  387. unsigned control = buf_get_u32(arm->core_cache
  388. ->reg_list[ARMV7M_CONTROL].value, 0, 2);
  389. /* is this thread privileged? */
  390. arm->core_mode = control & 1
  391. ? ARM_MODE_USER_THREAD
  392. : ARM_MODE_THREAD;
  393. /* which stack is it using? */
  394. if (control & 2)
  395. arm->map = armv7m_psp_reg_map;
  396. else
  397. arm->map = armv7m_msp_reg_map;
  398. armv7m->exception_number = 0;
  399. }
  400. if (armv7m->exception_number)
  401. cortex_m_examine_exception_reason(target);
  402. LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32 ", target->state: %s",
  403. arm_mode_name(arm->core_mode),
  404. buf_get_u32(arm->pc->value, 0, 32),
  405. target_state_name(target));
  406. if (armv7m->post_debug_entry) {
  407. retval = armv7m->post_debug_entry(target);
  408. if (retval != ERROR_OK)
  409. return retval;
  410. }
  411. return ERROR_OK;
  412. }
  413. static int cortex_m_poll(struct target *target)
  414. {
  415. int detected_failure = ERROR_OK;
  416. int retval = ERROR_OK;
  417. enum target_state prev_target_state = target->state;
  418. struct cortex_m_common *cortex_m = target_to_cm(target);
  419. struct armv7m_common *armv7m = &cortex_m->armv7m;
  420. /* Read from Debug Halting Control and Status Register */
  421. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  422. if (retval != ERROR_OK) {
  423. target->state = TARGET_UNKNOWN;
  424. return retval;
  425. }
  426. /* Recover from lockup. See ARMv7-M architecture spec,
  427. * section B1.5.15 "Unrecoverable exception cases".
  428. */
  429. if (cortex_m->dcb_dhcsr & S_LOCKUP) {
  430. LOG_ERROR("%s -- clearing lockup after double fault",
  431. target_name(target));
  432. cortex_m_write_debug_halt_mask(target, C_HALT, 0);
  433. target->debug_reason = DBG_REASON_DBGRQ;
  434. /* We have to execute the rest (the "finally" equivalent, but
  435. * still throw this exception again).
  436. */
  437. detected_failure = ERROR_FAIL;
  438. /* refresh status bits */
  439. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  440. if (retval != ERROR_OK)
  441. return retval;
  442. }
  443. if (cortex_m->dcb_dhcsr & S_RESET_ST) {
  444. target->state = TARGET_RESET;
  445. return ERROR_OK;
  446. }
  447. if (target->state == TARGET_RESET) {
  448. /* Cannot switch context while running so endreset is
  449. * called with target->state == TARGET_RESET
  450. */
  451. LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32,
  452. cortex_m->dcb_dhcsr);
  453. retval = cortex_m_endreset_event(target);
  454. if (retval != ERROR_OK) {
  455. target->state = TARGET_UNKNOWN;
  456. return retval;
  457. }
  458. target->state = TARGET_RUNNING;
  459. prev_target_state = TARGET_RUNNING;
  460. }
  461. if (cortex_m->dcb_dhcsr & S_HALT) {
  462. target->state = TARGET_HALTED;
  463. if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET)) {
  464. retval = cortex_m_debug_entry(target);
  465. if (retval != ERROR_OK)
  466. return retval;
  467. if (arm_semihosting(target, &retval) != 0)
  468. return retval;
  469. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  470. }
  471. if (prev_target_state == TARGET_DEBUG_RUNNING) {
  472. LOG_DEBUG(" ");
  473. retval = cortex_m_debug_entry(target);
  474. if (retval != ERROR_OK)
  475. return retval;
  476. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  477. }
  478. }
  479. /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
  480. * How best to model low power modes?
  481. */
  482. if (target->state == TARGET_UNKNOWN) {
  483. /* check if processor is retiring instructions */
  484. if (cortex_m->dcb_dhcsr & S_RETIRE_ST) {
  485. target->state = TARGET_RUNNING;
  486. retval = ERROR_OK;
  487. }
  488. }
  489. /* Did we detect a failure condition that we cleared? */
  490. if (detected_failure != ERROR_OK)
  491. retval = detected_failure;
  492. return retval;
  493. }
  494. static int cortex_m_halt(struct target *target)
  495. {
  496. LOG_DEBUG("target->state: %s",
  497. target_state_name(target));
  498. if (target->state == TARGET_HALTED) {
  499. LOG_DEBUG("target was already halted");
  500. return ERROR_OK;
  501. }
  502. if (target->state == TARGET_UNKNOWN)
  503. LOG_WARNING("target was in unknown state when halt was requested");
  504. if (target->state == TARGET_RESET) {
  505. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst()) {
  506. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  507. return ERROR_TARGET_FAILURE;
  508. } else {
  509. /* we came here in a reset_halt or reset_init sequence
  510. * debug entry was already prepared in cortex_m3_assert_reset()
  511. */
  512. target->debug_reason = DBG_REASON_DBGRQ;
  513. return ERROR_OK;
  514. }
  515. }
  516. /* Write to Debug Halting Control and Status Register */
  517. cortex_m_write_debug_halt_mask(target, C_HALT, 0);
  518. target->debug_reason = DBG_REASON_DBGRQ;
  519. return ERROR_OK;
  520. }
  521. static int cortex_m_soft_reset_halt(struct target *target)
  522. {
  523. struct cortex_m_common *cortex_m = target_to_cm(target);
  524. struct armv7m_common *armv7m = &cortex_m->armv7m;
  525. uint32_t dcb_dhcsr = 0;
  526. int retval, timeout = 0;
  527. /* soft_reset_halt is deprecated on cortex_m as the same functionality
  528. * can be obtained by using 'reset halt' and 'cortex_m reset_config vectreset'
  529. * As this reset only used VC_CORERESET it would only ever reset the cortex_m
  530. * core, not the peripherals */
  531. LOG_WARNING("soft_reset_halt is deprecated, please use 'reset halt' instead.");
  532. /* Enter debug state on reset; restore DEMCR in endreset_event() */
  533. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
  534. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  535. if (retval != ERROR_OK)
  536. return retval;
  537. /* Request a core-only reset */
  538. retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
  539. AIRCR_VECTKEY | AIRCR_VECTRESET);
  540. if (retval != ERROR_OK)
  541. return retval;
  542. target->state = TARGET_RESET;
  543. /* registers are now invalid */
  544. register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
  545. while (timeout < 100) {
  546. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &dcb_dhcsr);
  547. if (retval == ERROR_OK) {
  548. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR,
  549. &cortex_m->nvic_dfsr);
  550. if (retval != ERROR_OK)
  551. return retval;
  552. if ((dcb_dhcsr & S_HALT)
  553. && (cortex_m->nvic_dfsr & DFSR_VCATCH)) {
  554. LOG_DEBUG("system reset-halted, DHCSR 0x%08x, "
  555. "DFSR 0x%08x",
  556. (unsigned) dcb_dhcsr,
  557. (unsigned) cortex_m->nvic_dfsr);
  558. cortex_m_poll(target);
  559. /* FIXME restore user's vector catch config */
  560. return ERROR_OK;
  561. } else
  562. LOG_DEBUG("waiting for system reset-halt, "
  563. "DHCSR 0x%08x, %d ms",
  564. (unsigned) dcb_dhcsr, timeout);
  565. }
  566. timeout++;
  567. alive_sleep(1);
  568. }
  569. return ERROR_OK;
  570. }
  571. void cortex_m_enable_breakpoints(struct target *target)
  572. {
  573. struct breakpoint *breakpoint = target->breakpoints;
  574. /* set any pending breakpoints */
  575. while (breakpoint) {
  576. if (!breakpoint->set)
  577. cortex_m_set_breakpoint(target, breakpoint);
  578. breakpoint = breakpoint->next;
  579. }
  580. }
  581. static int cortex_m_resume(struct target *target, int current,
  582. uint32_t address, int handle_breakpoints, int debug_execution)
  583. {
  584. struct armv7m_common *armv7m = target_to_armv7m(target);
  585. struct breakpoint *breakpoint = NULL;
  586. uint32_t resume_pc;
  587. struct reg *r;
  588. if (target->state != TARGET_HALTED) {
  589. LOG_WARNING("target not halted");
  590. return ERROR_TARGET_NOT_HALTED;
  591. }
  592. if (!debug_execution) {
  593. target_free_all_working_areas(target);
  594. cortex_m_enable_breakpoints(target);
  595. cortex_m_enable_watchpoints(target);
  596. }
  597. if (debug_execution) {
  598. r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
  599. /* Disable interrupts */
  600. /* We disable interrupts in the PRIMASK register instead of
  601. * masking with C_MASKINTS. This is probably the same issue
  602. * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
  603. * in parallel with disabled interrupts can cause local faults
  604. * to not be taken.
  605. *
  606. * REVISIT this clearly breaks non-debug execution, since the
  607. * PRIMASK register state isn't saved/restored... workaround
  608. * by never resuming app code after debug execution.
  609. */
  610. buf_set_u32(r->value, 0, 1, 1);
  611. r->dirty = true;
  612. r->valid = true;
  613. /* Make sure we are in Thumb mode */
  614. r = armv7m->arm.cpsr;
  615. buf_set_u32(r->value, 24, 1, 1);
  616. r->dirty = true;
  617. r->valid = true;
  618. }
  619. /* current = 1: continue on current pc, otherwise continue at <address> */
  620. r = armv7m->arm.pc;
  621. if (!current) {
  622. buf_set_u32(r->value, 0, 32, address);
  623. r->dirty = true;
  624. r->valid = true;
  625. }
  626. /* if we halted last time due to a bkpt instruction
  627. * then we have to manually step over it, otherwise
  628. * the core will break again */
  629. if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
  630. && !debug_execution)
  631. armv7m_maybe_skip_bkpt_inst(target, NULL);
  632. resume_pc = buf_get_u32(r->value, 0, 32);
  633. armv7m_restore_context(target);
  634. /* the front-end may request us not to handle breakpoints */
  635. if (handle_breakpoints) {
  636. /* Single step past breakpoint at current address */
  637. breakpoint = breakpoint_find(target, resume_pc);
  638. if (breakpoint) {
  639. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (ID: %" PRIu32 ")",
  640. breakpoint->address,
  641. breakpoint->unique_id);
  642. cortex_m_unset_breakpoint(target, breakpoint);
  643. cortex_m_single_step_core(target);
  644. cortex_m_set_breakpoint(target, breakpoint);
  645. }
  646. }
  647. /* Restart core */
  648. cortex_m_write_debug_halt_mask(target, 0, C_HALT);
  649. target->debug_reason = DBG_REASON_NOTHALTED;
  650. /* registers are now invalid */
  651. register_cache_invalidate(armv7m->arm.core_cache);
  652. if (!debug_execution) {
  653. target->state = TARGET_RUNNING;
  654. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  655. LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
  656. } else {
  657. target->state = TARGET_DEBUG_RUNNING;
  658. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  659. LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
  660. }
  661. return ERROR_OK;
  662. }
  663. /* int irqstepcount = 0; */
  664. static int cortex_m_step(struct target *target, int current,
  665. uint32_t address, int handle_breakpoints)
  666. {
  667. struct cortex_m_common *cortex_m = target_to_cm(target);
  668. struct armv7m_common *armv7m = &cortex_m->armv7m;
  669. struct breakpoint *breakpoint = NULL;
  670. struct reg *pc = armv7m->arm.pc;
  671. bool bkpt_inst_found = false;
  672. int retval;
  673. bool isr_timed_out = false;
  674. if (target->state != TARGET_HALTED) {
  675. LOG_WARNING("target not halted");
  676. return ERROR_TARGET_NOT_HALTED;
  677. }
  678. /* current = 1: continue on current pc, otherwise continue at <address> */
  679. if (!current)
  680. buf_set_u32(pc->value, 0, 32, address);
  681. uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
  682. /* the front-end may request us not to handle breakpoints */
  683. if (handle_breakpoints) {
  684. breakpoint = breakpoint_find(target, pc_value);
  685. if (breakpoint)
  686. cortex_m_unset_breakpoint(target, breakpoint);
  687. }
  688. armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
  689. target->debug_reason = DBG_REASON_SINGLESTEP;
  690. armv7m_restore_context(target);
  691. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  692. /* if no bkpt instruction is found at pc then we can perform
  693. * a normal step, otherwise we have to manually step over the bkpt
  694. * instruction - as such simulate a step */
  695. if (bkpt_inst_found == false) {
  696. /* Automatic ISR masking mode off: Just step over the next instruction */
  697. if ((cortex_m->isrmasking_mode != CORTEX_M_ISRMASK_AUTO))
  698. cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
  699. else {
  700. /* Process interrupts during stepping in a way they don't interfere
  701. * debugging.
  702. *
  703. * Principle:
  704. *
  705. * Set a temporary break point at the current pc and let the core run
  706. * with interrupts enabled. Pending interrupts get served and we run
  707. * into the breakpoint again afterwards. Then we step over the next
  708. * instruction with interrupts disabled.
  709. *
  710. * If the pending interrupts don't complete within time, we leave the
  711. * core running. This may happen if the interrupts trigger faster
  712. * than the core can process them or the handler doesn't return.
  713. *
  714. * If no more breakpoints are available we simply do a step with
  715. * interrupts enabled.
  716. *
  717. */
  718. /* 2012-09-29 ph
  719. *
  720. * If a break point is already set on the lower half word then a break point on
  721. * the upper half word will not break again when the core is restarted. So we
  722. * just step over the instruction with interrupts disabled.
  723. *
  724. * The documentation has no information about this, it was found by observation
  725. * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 dosen't seem to
  726. * suffer from this problem.
  727. *
  728. * To add some confusion: pc_value has bit 0 always set, while the breakpoint
  729. * address has it always cleared. The former is done to indicate thumb mode
  730. * to gdb.
  731. *
  732. */
  733. if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
  734. LOG_DEBUG("Stepping over next instruction with interrupts disabled");
  735. cortex_m_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
  736. cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
  737. /* Re-enable interrupts */
  738. cortex_m_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
  739. }
  740. else {
  741. /* Set a temporary break point */
  742. if (breakpoint)
  743. retval = cortex_m_set_breakpoint(target, breakpoint);
  744. else
  745. retval = breakpoint_add(target, pc_value, 2, BKPT_TYPE_BY_ADDR(pc_value));
  746. bool tmp_bp_set = (retval == ERROR_OK);
  747. /* No more breakpoints left, just do a step */
  748. if (!tmp_bp_set)
  749. cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
  750. else {
  751. /* Start the core */
  752. LOG_DEBUG("Starting core to serve pending interrupts");
  753. int64_t t_start = timeval_ms();
  754. cortex_m_write_debug_halt_mask(target, 0, C_HALT | C_STEP);
  755. /* Wait for pending handlers to complete or timeout */
  756. do {
  757. retval = mem_ap_read_atomic_u32(armv7m->debug_ap,
  758. DCB_DHCSR,
  759. &cortex_m->dcb_dhcsr);
  760. if (retval != ERROR_OK) {
  761. target->state = TARGET_UNKNOWN;
  762. return retval;
  763. }
  764. isr_timed_out = ((timeval_ms() - t_start) > 500);
  765. } while (!((cortex_m->dcb_dhcsr & S_HALT) || isr_timed_out));
  766. /* only remove breakpoint if we created it */
  767. if (breakpoint)
  768. cortex_m_unset_breakpoint(target, breakpoint);
  769. else {
  770. /* Remove the temporary breakpoint */
  771. breakpoint_remove(target, pc_value);
  772. }
  773. if (isr_timed_out) {
  774. LOG_DEBUG("Interrupt handlers didn't complete within time, "
  775. "leaving target running");
  776. } else {
  777. /* Step over next instruction with interrupts disabled */
  778. cortex_m_write_debug_halt_mask(target,
  779. C_HALT | C_MASKINTS,
  780. 0);
  781. cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
  782. /* Re-enable interrupts */
  783. cortex_m_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
  784. }
  785. }
  786. }
  787. }
  788. }
  789. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  790. if (retval != ERROR_OK)
  791. return retval;
  792. /* registers are now invalid */
  793. register_cache_invalidate(armv7m->arm.core_cache);
  794. if (breakpoint)
  795. cortex_m_set_breakpoint(target, breakpoint);
  796. if (isr_timed_out) {
  797. /* Leave the core running. The user has to stop execution manually. */
  798. target->debug_reason = DBG_REASON_NOTHALTED;
  799. target->state = TARGET_RUNNING;
  800. return ERROR_OK;
  801. }
  802. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
  803. " nvic_icsr = 0x%" PRIx32,
  804. cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
  805. retval = cortex_m_debug_entry(target);
  806. if (retval != ERROR_OK)
  807. return retval;
  808. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  809. LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
  810. " nvic_icsr = 0x%" PRIx32,
  811. cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
  812. return ERROR_OK;
  813. }
  814. static int cortex_m_assert_reset(struct target *target)
  815. {
  816. struct cortex_m_common *cortex_m = target_to_cm(target);
  817. struct armv7m_common *armv7m = &cortex_m->armv7m;
  818. enum cortex_m_soft_reset_config reset_config = cortex_m->soft_reset_config;
  819. LOG_DEBUG("target->state: %s",
  820. target_state_name(target));
  821. enum reset_types jtag_reset_config = jtag_get_reset_config();
  822. if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT)) {
  823. /* allow scripts to override the reset event */
  824. target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
  825. register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
  826. target->state = TARGET_RESET;
  827. return ERROR_OK;
  828. }
  829. /* some cores support connecting while srst is asserted
  830. * use that mode is it has been configured */
  831. bool srst_asserted = false;
  832. if (!target_was_examined(target)) {
  833. if (jtag_reset_config & RESET_HAS_SRST) {
  834. adapter_assert_reset();
  835. if (target->reset_halt)
  836. LOG_ERROR("Target not examined, will not halt after reset!");
  837. return ERROR_OK;
  838. } else {
  839. LOG_ERROR("Target not examined, reset NOT asserted!");
  840. return ERROR_FAIL;
  841. }
  842. }
  843. if ((jtag_reset_config & RESET_HAS_SRST) &&
  844. (jtag_reset_config & RESET_SRST_NO_GATING)) {
  845. adapter_assert_reset();
  846. srst_asserted = true;
  847. }
  848. /* Enable debug requests */
  849. int retval;
  850. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR, &cortex_m->dcb_dhcsr);
  851. /* Store important errors instead of failing and proceed to reset assert */
  852. if (retval != ERROR_OK || !(cortex_m->dcb_dhcsr & C_DEBUGEN))
  853. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  854. /* If the processor is sleeping in a WFI or WFE instruction, the
  855. * C_HALT bit must be asserted to regain control */
  856. if (retval == ERROR_OK && (cortex_m->dcb_dhcsr & S_SLEEP))
  857. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DHCSR, DBGKEY | C_HALT | C_DEBUGEN);
  858. mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
  859. /* Ignore less important errors */
  860. if (!target->reset_halt) {
  861. /* Set/Clear C_MASKINTS in a separate operation */
  862. if (cortex_m->dcb_dhcsr & C_MASKINTS)
  863. mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
  864. DBGKEY | C_DEBUGEN | C_HALT);
  865. /* clear any debug flags before resuming */
  866. cortex_m_clear_halt(target);
  867. /* clear C_HALT in dhcsr reg */
  868. cortex_m_write_debug_halt_mask(target, 0, C_HALT);
  869. } else {
  870. /* Halt in debug on reset; endreset_event() restores DEMCR.
  871. *
  872. * REVISIT catching BUSERR presumably helps to defend against
  873. * bad vector table entries. Should this include MMERR or
  874. * other flags too?
  875. */
  876. int retval2;
  877. retval2 = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DEMCR,
  878. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  879. if (retval != ERROR_OK || retval2 != ERROR_OK)
  880. LOG_INFO("AP write error, reset will not halt");
  881. }
  882. if (jtag_reset_config & RESET_HAS_SRST) {
  883. /* default to asserting srst */
  884. if (!srst_asserted)
  885. adapter_assert_reset();
  886. /* srst is asserted, ignore AP access errors */
  887. retval = ERROR_OK;
  888. } else {
  889. /* Use a standard Cortex-M3 software reset mechanism.
  890. * We default to using VECRESET as it is supported on all current cores.
  891. * This has the disadvantage of not resetting the peripherals, so a
  892. * reset-init event handler is needed to perform any peripheral resets.
  893. */
  894. LOG_DEBUG("Using Cortex-M %s", (reset_config == CORTEX_M_RESET_SYSRESETREQ)
  895. ? "SYSRESETREQ" : "VECTRESET");
  896. if (reset_config == CORTEX_M_RESET_VECTRESET) {
  897. LOG_WARNING("Only resetting the Cortex-M core, use a reset-init event "
  898. "handler to reset any peripherals or configure hardware srst support.");
  899. }
  900. int retval3;
  901. retval3 = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
  902. AIRCR_VECTKEY | ((reset_config == CORTEX_M_RESET_SYSRESETREQ)
  903. ? AIRCR_SYSRESETREQ : AIRCR_VECTRESET));
  904. if (retval3 != ERROR_OK)
  905. LOG_DEBUG("Ignoring AP write error right after reset");
  906. retval3 = dap_dp_init(armv7m->debug_ap->dap);
  907. if (retval3 != ERROR_OK)
  908. LOG_ERROR("DP initialisation failed");
  909. else {
  910. /* I do not know why this is necessary, but it
  911. * fixes strange effects (step/resume cause NMI
  912. * after reset) on LM3S6918 -- Michael Schwingen
  913. */
  914. uint32_t tmp;
  915. mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_AIRCR, &tmp);
  916. }
  917. }
  918. target->state = TARGET_RESET;
  919. jtag_add_sleep(50000);
  920. register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
  921. /* now return stored error code if any */
  922. if (retval != ERROR_OK)
  923. return retval;
  924. if (target->reset_halt) {
  925. retval = target_halt(target);
  926. if (retval != ERROR_OK)
  927. return retval;
  928. }
  929. return ERROR_OK;
  930. }
  931. static int cortex_m_deassert_reset(struct target *target)
  932. {
  933. struct armv7m_common *armv7m = &target_to_cm(target)->armv7m;
  934. LOG_DEBUG("target->state: %s",
  935. target_state_name(target));
  936. /* deassert reset lines */
  937. adapter_deassert_reset();
  938. enum reset_types jtag_reset_config = jtag_get_reset_config();
  939. if ((jtag_reset_config & RESET_HAS_SRST) &&
  940. !(jtag_reset_config & RESET_SRST_NO_GATING) &&
  941. target_was_examined(target)) {
  942. int retval = dap_dp_init(armv7m->debug_ap->dap);
  943. if (retval != ERROR_OK) {
  944. LOG_ERROR("DP initialisation failed");
  945. return retval;
  946. }
  947. }
  948. return ERROR_OK;
  949. }
  950. int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
  951. {
  952. int retval;
  953. int fp_num = 0;
  954. struct cortex_m_common *cortex_m = target_to_cm(target);
  955. struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
  956. if (breakpoint->set) {
  957. LOG_WARNING("breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id);
  958. return ERROR_OK;
  959. }
  960. if (cortex_m->auto_bp_type)
  961. breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
  962. if (breakpoint->type == BKPT_HARD) {
  963. uint32_t fpcr_value;
  964. while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
  965. fp_num++;
  966. if (fp_num >= cortex_m->fp_num_code) {
  967. LOG_ERROR("Can not find free FPB Comparator!");
  968. return ERROR_FAIL;
  969. }
  970. breakpoint->set = fp_num + 1;
  971. fpcr_value = breakpoint->address | 1;
  972. if (cortex_m->fp_rev == 0) {
  973. uint32_t hilo;
  974. hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
  975. fpcr_value = (fpcr_value & 0x1FFFFFFC) | hilo | 1;
  976. } else if (cortex_m->fp_rev > 1) {
  977. LOG_ERROR("Unhandled Cortex-M Flash Patch Breakpoint architecture revision");
  978. return ERROR_FAIL;
  979. }
  980. comparator_list[fp_num].used = 1;
  981. comparator_list[fp_num].fpcr_value = fpcr_value;
  982. target_write_u32(target, comparator_list[fp_num].fpcr_address,
  983. comparator_list[fp_num].fpcr_value);
  984. LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32 "",
  985. fp_num,
  986. comparator_list[fp_num].fpcr_value);
  987. if (!cortex_m->fpb_enabled) {
  988. LOG_DEBUG("FPB wasn't enabled, do it now");
  989. retval = cortex_m_enable_fpb(target);
  990. if (retval != ERROR_OK) {
  991. LOG_ERROR("Failed to enable the FPB");
  992. return retval;
  993. }
  994. cortex_m->fpb_enabled = 1;
  995. }
  996. } else if (breakpoint->type == BKPT_SOFT) {
  997. uint8_t code[4];
  998. /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
  999. * semihosting; don't use that. Otherwise the BKPT
  1000. * parameter is arbitrary.
  1001. */
  1002. buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
  1003. retval = target_read_memory(target,
  1004. breakpoint->address & 0xFFFFFFFE,
  1005. breakpoint->length, 1,
  1006. breakpoint->orig_instr);
  1007. if (retval != ERROR_OK)
  1008. return retval;
  1009. retval = target_write_memory(target,
  1010. breakpoint->address & 0xFFFFFFFE,
  1011. breakpoint->length, 1,
  1012. code);
  1013. if (retval != ERROR_OK)
  1014. return retval;
  1015. breakpoint->set = true;
  1016. }
  1017. LOG_DEBUG("BPID: %" PRIu32 ", Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  1018. breakpoint->unique_id,
  1019. (int)(breakpoint->type),
  1020. breakpoint->address,
  1021. breakpoint->length,
  1022. breakpoint->set);
  1023. return ERROR_OK;
  1024. }
  1025. int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
  1026. {
  1027. int retval;
  1028. struct cortex_m_common *cortex_m = target_to_cm(target);
  1029. struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
  1030. if (!breakpoint->set) {
  1031. LOG_WARNING("breakpoint not set");
  1032. return ERROR_OK;
  1033. }
  1034. LOG_DEBUG("BPID: %" PRIu32 ", Type: %d, Address: 0x%08" PRIx32 " Length: %d (set=%d)",
  1035. breakpoint->unique_id,
  1036. (int)(breakpoint->type),
  1037. breakpoint->address,
  1038. breakpoint->length,
  1039. breakpoint->set);
  1040. if (breakpoint->type == BKPT_HARD) {
  1041. int fp_num = breakpoint->set - 1;
  1042. if ((fp_num < 0) || (fp_num >= cortex_m->fp_num_code)) {
  1043. LOG_DEBUG("Invalid FP Comparator number in breakpoint");
  1044. return ERROR_OK;
  1045. }
  1046. comparator_list[fp_num].used = 0;
  1047. comparator_list[fp_num].fpcr_value = 0;
  1048. target_write_u32(target, comparator_list[fp_num].fpcr_address,
  1049. comparator_list[fp_num].fpcr_value);
  1050. } else {
  1051. /* restore original instruction (kept in target endianness) */
  1052. if (breakpoint->length == 4) {
  1053. retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1,
  1054. breakpoint->orig_instr);
  1055. if (retval != ERROR_OK)
  1056. return retval;
  1057. } else {
  1058. retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1,
  1059. breakpoint->orig_instr);
  1060. if (retval != ERROR_OK)
  1061. return retval;
  1062. }
  1063. }
  1064. breakpoint->set = false;
  1065. return ERROR_OK;
  1066. }
  1067. int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  1068. {
  1069. struct cortex_m_common *cortex_m = target_to_cm(target);
  1070. if (cortex_m->auto_bp_type)
  1071. breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
  1072. if (breakpoint->type != BKPT_TYPE_BY_ADDR(breakpoint->address)) {
  1073. if (breakpoint->type == BKPT_HARD) {
  1074. LOG_INFO("flash patch comparator requested outside code memory region");
  1075. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1076. }
  1077. if (breakpoint->type == BKPT_SOFT) {
  1078. LOG_INFO("soft breakpoint requested in code (flash) memory region");
  1079. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1080. }
  1081. }
  1082. if ((breakpoint->type == BKPT_HARD) && (cortex_m->fp_code_available < 1)) {
  1083. LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
  1084. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1085. }
  1086. if (breakpoint->length == 3) {
  1087. LOG_DEBUG("Using a two byte breakpoint for 32bit Thumb-2 request");
  1088. breakpoint->length = 2;
  1089. }
  1090. if ((breakpoint->length != 2)) {
  1091. LOG_INFO("only breakpoints of two bytes length supported");
  1092. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1093. }
  1094. if (breakpoint->type == BKPT_HARD)
  1095. cortex_m->fp_code_available--;
  1096. return cortex_m_set_breakpoint(target, breakpoint);
  1097. }
  1098. int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
  1099. {
  1100. struct cortex_m_common *cortex_m = target_to_cm(target);
  1101. /* REVISIT why check? FBP can be updated with core running ... */
  1102. if (target->state != TARGET_HALTED) {
  1103. LOG_WARNING("target not halted");
  1104. return ERROR_TARGET_NOT_HALTED;
  1105. }
  1106. if (cortex_m->auto_bp_type)
  1107. breakpoint->type = BKPT_TYPE_BY_ADDR(breakpoint->address);
  1108. if (breakpoint->set)
  1109. cortex_m_unset_breakpoint(target, breakpoint);
  1110. if (breakpoint->type == BKPT_HARD)
  1111. cortex_m->fp_code_available++;
  1112. return ERROR_OK;
  1113. }
  1114. int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1115. {
  1116. int dwt_num = 0;
  1117. uint32_t mask, temp;
  1118. struct cortex_m_common *cortex_m = target_to_cm(target);
  1119. /* watchpoint params were validated earlier */
  1120. mask = 0;
  1121. temp = watchpoint->length;
  1122. while (temp) {
  1123. temp >>= 1;
  1124. mask++;
  1125. }
  1126. mask--;
  1127. /* REVISIT Don't fully trust these "not used" records ... users
  1128. * may set up breakpoints by hand, e.g. dual-address data value
  1129. * watchpoint using comparator #1; comparator #0 matching cycle
  1130. * count; send data trace info through ITM and TPIU; etc
  1131. */
  1132. struct cortex_m_dwt_comparator *comparator;
  1133. for (comparator = cortex_m->dwt_comparator_list;
  1134. comparator->used && dwt_num < cortex_m->dwt_num_comp;
  1135. comparator++, dwt_num++)
  1136. continue;
  1137. if (dwt_num >= cortex_m->dwt_num_comp) {
  1138. LOG_ERROR("Can not find free DWT Comparator");
  1139. return ERROR_FAIL;
  1140. }
  1141. comparator->used = 1;
  1142. watchpoint->set = dwt_num + 1;
  1143. comparator->comp = watchpoint->address;
  1144. target_write_u32(target, comparator->dwt_comparator_address + 0,
  1145. comparator->comp);
  1146. comparator->mask = mask;
  1147. target_write_u32(target, comparator->dwt_comparator_address + 4,
  1148. comparator->mask);
  1149. switch (watchpoint->rw) {
  1150. case WPT_READ:
  1151. comparator->function = 5;
  1152. break;
  1153. case WPT_WRITE:
  1154. comparator->function = 6;
  1155. break;
  1156. case WPT_ACCESS:
  1157. comparator->function = 7;
  1158. break;
  1159. }
  1160. target_write_u32(target, comparator->dwt_comparator_address + 8,
  1161. comparator->function);
  1162. LOG_DEBUG("Watchpoint (ID %d) DWT%d 0x%08x 0x%x 0x%05x",
  1163. watchpoint->unique_id, dwt_num,
  1164. (unsigned) comparator->comp,
  1165. (unsigned) comparator->mask,
  1166. (unsigned) comparator->function);
  1167. return ERROR_OK;
  1168. }
  1169. int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1170. {
  1171. struct cortex_m_common *cortex_m = target_to_cm(target);
  1172. struct cortex_m_dwt_comparator *comparator;
  1173. int dwt_num;
  1174. if (!watchpoint->set) {
  1175. LOG_WARNING("watchpoint (wpid: %d) not set",
  1176. watchpoint->unique_id);
  1177. return ERROR_OK;
  1178. }
  1179. dwt_num = watchpoint->set - 1;
  1180. LOG_DEBUG("Watchpoint (ID %d) DWT%d address: 0x%08x clear",
  1181. watchpoint->unique_id, dwt_num,
  1182. (unsigned) watchpoint->address);
  1183. if ((dwt_num < 0) || (dwt_num >= cortex_m->dwt_num_comp)) {
  1184. LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
  1185. return ERROR_OK;
  1186. }
  1187. comparator = cortex_m->dwt_comparator_list + dwt_num;
  1188. comparator->used = 0;
  1189. comparator->function = 0;
  1190. target_write_u32(target, comparator->dwt_comparator_address + 8,
  1191. comparator->function);
  1192. watchpoint->set = false;
  1193. return ERROR_OK;
  1194. }
  1195. int cortex_m_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1196. {
  1197. struct cortex_m_common *cortex_m = target_to_cm(target);
  1198. if (cortex_m->dwt_comp_available < 1) {
  1199. LOG_DEBUG("no comparators?");
  1200. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1201. }
  1202. /* hardware doesn't support data value masking */
  1203. if (watchpoint->mask != ~(uint32_t)0) {
  1204. LOG_DEBUG("watchpoint value masks not supported");
  1205. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1206. }
  1207. /* hardware allows address masks of up to 32K */
  1208. unsigned mask;
  1209. for (mask = 0; mask < 16; mask++) {
  1210. if ((1u << mask) == watchpoint->length)
  1211. break;
  1212. }
  1213. if (mask == 16) {
  1214. LOG_DEBUG("unsupported watchpoint length");
  1215. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1216. }
  1217. if (watchpoint->address & ((1 << mask) - 1)) {
  1218. LOG_DEBUG("watchpoint address is unaligned");
  1219. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1220. }
  1221. /* Caller doesn't seem to be able to describe watching for data
  1222. * values of zero; that flags "no value".
  1223. *
  1224. * REVISIT This DWT may well be able to watch for specific data
  1225. * values. Requires comparator #1 to set DATAVMATCH and match
  1226. * the data, and another comparator (DATAVADDR0) matching addr.
  1227. */
  1228. if (watchpoint->value) {
  1229. LOG_DEBUG("data value watchpoint not YET supported");
  1230. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1231. }
  1232. cortex_m->dwt_comp_available--;
  1233. LOG_DEBUG("dwt_comp_available: %d", cortex_m->dwt_comp_available);
  1234. return ERROR_OK;
  1235. }
  1236. int cortex_m_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1237. {
  1238. struct cortex_m_common *cortex_m = target_to_cm(target);
  1239. /* REVISIT why check? DWT can be updated with core running ... */
  1240. if (target->state != TARGET_HALTED) {
  1241. LOG_WARNING("target not halted");
  1242. return ERROR_TARGET_NOT_HALTED;
  1243. }
  1244. if (watchpoint->set)
  1245. cortex_m_unset_watchpoint(target, watchpoint);
  1246. cortex_m->dwt_comp_available++;
  1247. LOG_DEBUG("dwt_comp_available: %d", cortex_m->dwt_comp_available);
  1248. return ERROR_OK;
  1249. }
  1250. void cortex_m_enable_watchpoints(struct target *target)
  1251. {
  1252. struct watchpoint *watchpoint = target->watchpoints;
  1253. /* set any pending watchpoints */
  1254. while (watchpoint) {
  1255. if (!watchpoint->set)
  1256. cortex_m_set_watchpoint(target, watchpoint);
  1257. watchpoint = watchpoint->next;
  1258. }
  1259. }
  1260. static int cortex_m_load_core_reg_u32(struct target *target,
  1261. uint32_t num, uint32_t *value)
  1262. {
  1263. int retval;
  1264. /* NOTE: we "know" here that the register identifiers used
  1265. * in the v7m header match the Cortex-M3 Debug Core Register
  1266. * Selector values for R0..R15, xPSR, MSP, and PSP.
  1267. */
  1268. switch (num) {
  1269. case 0 ... 18:
  1270. /* read a normal core register */
  1271. retval = cortexm_dap_read_coreregister_u32(target, value, num);
  1272. if (retval != ERROR_OK) {
  1273. LOG_ERROR("JTAG failure %i", retval);
  1274. return ERROR_JTAG_DEVICE_ERROR;
  1275. }
  1276. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "", (int)num, *value);
  1277. break;
  1278. case ARMV7M_FPSCR:
  1279. /* Floating-point Status and Registers */
  1280. retval = target_write_u32(target, DCB_DCRSR, 0x21);
  1281. if (retval != ERROR_OK)
  1282. return retval;
  1283. retval = target_read_u32(target, DCB_DCRDR, value);
  1284. if (retval != ERROR_OK)
  1285. return retval;
  1286. LOG_DEBUG("load from FPSCR value 0x%" PRIx32, *value);
  1287. break;
  1288. case ARMV7M_S0 ... ARMV7M_S31:
  1289. /* Floating-point Status and Registers */
  1290. retval = target_write_u32(target, DCB_DCRSR, num - ARMV7M_S0 + 0x40);
  1291. if (retval != ERROR_OK)
  1292. return retval;
  1293. retval = target_read_u32(target, DCB_DCRDR, value);
  1294. if (retval != ERROR_OK)
  1295. return retval;
  1296. LOG_DEBUG("load from FPU reg S%d value 0x%" PRIx32,
  1297. (int)(num - ARMV7M_S0), *value);
  1298. break;
  1299. case ARMV7M_PRIMASK:
  1300. case ARMV7M_BASEPRI:
  1301. case ARMV7M_FAULTMASK:
  1302. case ARMV7M_CONTROL:
  1303. /* Cortex-M3 packages these four registers as bitfields
  1304. * in one Debug Core register. So say r0 and r2 docs;
  1305. * it was removed from r1 docs, but still works.
  1306. */
  1307. cortexm_dap_read_coreregister_u32(target, value, 20);
  1308. switch (num) {
  1309. case ARMV7M_PRIMASK:
  1310. *value = buf_get_u32((uint8_t *)value, 0, 1);
  1311. break;
  1312. case ARMV7M_BASEPRI:
  1313. *value = buf_get_u32((uint8_t *)value, 8, 8);
  1314. break;
  1315. case ARMV7M_FAULTMASK:
  1316. *value = buf_get_u32((uint8_t *)value, 16, 1);
  1317. break;
  1318. case ARMV7M_CONTROL:
  1319. *value = buf_get_u32((uint8_t *)value, 24, 2);
  1320. break;
  1321. }
  1322. LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "", (int)num, *value);
  1323. break;
  1324. default:
  1325. return ERROR_COMMAND_SYNTAX_ERROR;
  1326. }
  1327. return ERROR_OK;
  1328. }
  1329. static int cortex_m_store_core_reg_u32(struct target *target,
  1330. uint32_t num, uint32_t value)
  1331. {
  1332. int retval;
  1333. uint32_t reg;
  1334. struct armv7m_common *armv7m = target_to_armv7m(target);
  1335. /* NOTE: we "know" here that the register identifiers used
  1336. * in the v7m header match the Cortex-M3 Debug Core Register
  1337. * Selector values for R0..R15, xPSR, MSP, and PSP.
  1338. */
  1339. switch (num) {
  1340. case 0 ... 18:
  1341. retval = cortexm_dap_write_coreregister_u32(target, value, num);
  1342. if (retval != ERROR_OK) {
  1343. struct reg *r;
  1344. LOG_ERROR("JTAG failure");
  1345. r = armv7m->arm.core_cache->reg_list + num;
  1346. r->dirty = r->valid;
  1347. return ERROR_JTAG_DEVICE_ERROR;
  1348. }
  1349. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  1350. break;
  1351. case ARMV7M_FPSCR:
  1352. /* Floating-point Status and Registers */
  1353. retval = target_write_u32(target, DCB_DCRDR, value);
  1354. if (retval != ERROR_OK)
  1355. return retval;
  1356. retval = target_write_u32(target, DCB_DCRSR, 0x21 | (1<<16));
  1357. if (retval != ERROR_OK)
  1358. return retval;
  1359. LOG_DEBUG("write FPSCR value 0x%" PRIx32, value);
  1360. break;
  1361. case ARMV7M_S0 ... ARMV7M_S31:
  1362. /* Floating-point Status and Registers */
  1363. retval = target_write_u32(target, DCB_DCRDR, value);
  1364. if (retval != ERROR_OK)
  1365. return retval;
  1366. retval = target_write_u32(target, DCB_DCRSR, (num - ARMV7M_S0 + 0x40) | (1<<16));
  1367. if (retval != ERROR_OK)
  1368. return retval;
  1369. LOG_DEBUG("write FPU reg S%d value 0x%" PRIx32,
  1370. (int)(num - ARMV7M_S0), value);
  1371. break;
  1372. case ARMV7M_PRIMASK:
  1373. case ARMV7M_BASEPRI:
  1374. case ARMV7M_FAULTMASK:
  1375. case ARMV7M_CONTROL:
  1376. /* Cortex-M3 packages these four registers as bitfields
  1377. * in one Debug Core register. So say r0 and r2 docs;
  1378. * it was removed from r1 docs, but still works.
  1379. */
  1380. cortexm_dap_read_coreregister_u32(target, &reg, 20);
  1381. switch (num) {
  1382. case ARMV7M_PRIMASK:
  1383. buf_set_u32((uint8_t *)&reg, 0, 1, value);
  1384. break;
  1385. case ARMV7M_BASEPRI:
  1386. buf_set_u32((uint8_t *)&reg, 8, 8, value);
  1387. break;
  1388. case ARMV7M_FAULTMASK:
  1389. buf_set_u32((uint8_t *)&reg, 16, 1, value);
  1390. break;
  1391. case ARMV7M_CONTROL:
  1392. buf_set_u32((uint8_t *)&reg, 24, 2, value);
  1393. break;
  1394. }
  1395. cortexm_dap_write_coreregister_u32(target, reg, 20);
  1396. LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
  1397. break;
  1398. default:
  1399. return ERROR_COMMAND_SYNTAX_ERROR;
  1400. }
  1401. return ERROR_OK;
  1402. }
  1403. static int cortex_m_read_memory(struct target *target, uint32_t address,
  1404. uint32_t size, uint32_t count, uint8_t *buffer)
  1405. {
  1406. struct armv7m_common *armv7m = target_to_armv7m(target);
  1407. if (armv7m->arm.is_armv6m) {
  1408. /* armv6m does not handle unaligned memory access */
  1409. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  1410. return ERROR_TARGET_UNALIGNED_ACCESS;
  1411. }
  1412. return mem_ap_read_buf(armv7m->debug_ap, buffer, size, count, address);
  1413. }
  1414. static int cortex_m_write_memory(struct target *target, uint32_t address,
  1415. uint32_t size, uint32_t count, const uint8_t *buffer)
  1416. {
  1417. struct armv7m_common *armv7m = target_to_armv7m(target);
  1418. if (armv7m->arm.is_armv6m) {
  1419. /* armv6m does not handle unaligned memory access */
  1420. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  1421. return ERROR_TARGET_UNALIGNED_ACCESS;
  1422. }
  1423. return mem_ap_write_buf(armv7m->debug_ap, buffer, size, count, address);
  1424. }
  1425. static int cortex_m_init_target(struct command_context *cmd_ctx,
  1426. struct target *target)
  1427. {
  1428. armv7m_build_reg_cache(target);
  1429. arm_semihosting_init(target);
  1430. return ERROR_OK;
  1431. }
  1432. void cortex_m_deinit_target(struct target *target)
  1433. {
  1434. struct cortex_m_common *cortex_m = target_to_cm(target);
  1435. free(cortex_m->fp_comparator_list);
  1436. cortex_m_dwt_free(target);
  1437. armv7m_free_reg_cache(target);
  1438. free(target->private_config);
  1439. free(cortex_m);
  1440. }
  1441. /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
  1442. * on r/w if the core is not running, and clear on resume or reset ... or
  1443. * at least, in a post_restore_context() method.
  1444. */
  1445. struct dwt_reg_state {
  1446. struct target *target;
  1447. uint32_t addr;
  1448. uint8_t value[4]; /* scratch/cache */
  1449. };
  1450. static int cortex_m_dwt_get_reg(struct reg *reg)
  1451. {
  1452. struct dwt_reg_state *state = reg->arch_info;
  1453. uint32_t tmp;
  1454. int retval = target_read_u32(state->target, state->addr, &tmp);
  1455. if (retval != ERROR_OK)
  1456. return retval;
  1457. buf_set_u32(state->value, 0, 32, tmp);
  1458. return ERROR_OK;
  1459. }
  1460. static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
  1461. {
  1462. struct dwt_reg_state *state = reg->arch_info;
  1463. return target_write_u32(state->target, state->addr,
  1464. buf_get_u32(buf, 0, reg->size));
  1465. }
  1466. struct dwt_reg {
  1467. uint32_t addr;
  1468. char *name;
  1469. unsigned size;
  1470. };
  1471. static struct dwt_reg dwt_base_regs[] = {
  1472. { DWT_CTRL, "dwt_ctrl", 32, },
  1473. /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
  1474. * increments while the core is asleep.
  1475. */
  1476. { DWT_CYCCNT, "dwt_cyccnt", 32, },
  1477. /* plus some 8 bit counters, useful for profiling with TPIU */
  1478. };
  1479. static struct dwt_reg dwt_comp[] = {
  1480. #define DWT_COMPARATOR(i) \
  1481. { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
  1482. { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
  1483. { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
  1484. DWT_COMPARATOR(0),
  1485. DWT_COMPARATOR(1),
  1486. DWT_COMPARATOR(2),
  1487. DWT_COMPARATOR(3),
  1488. #undef DWT_COMPARATOR
  1489. };
  1490. static const struct reg_arch_type dwt_reg_type = {
  1491. .get = cortex_m_dwt_get_reg,
  1492. .set = cortex_m_dwt_set_reg,
  1493. };
  1494. static void cortex_m_dwt_addreg(struct target *t, struct reg *r, struct dwt_reg *d)
  1495. {
  1496. struct dwt_reg_state *state;
  1497. state = calloc(1, sizeof *state);
  1498. if (!state)
  1499. return;
  1500. state->addr = d->addr;
  1501. state->target = t;
  1502. r->name = d->name;
  1503. r->size = d->size;
  1504. r->value = state->value;
  1505. r->arch_info = state;
  1506. r->type = &dwt_reg_type;
  1507. }
  1508. void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
  1509. {
  1510. uint32_t dwtcr;
  1511. struct reg_cache *cache;
  1512. struct cortex_m_dwt_comparator *comparator;
  1513. int reg, i;
  1514. target_read_u32(target, DWT_CTRL, &dwtcr);
  1515. if (!dwtcr) {
  1516. LOG_DEBUG("no DWT");
  1517. return;
  1518. }
  1519. cm->dwt_num_comp = (dwtcr >> 28) & 0xF;
  1520. cm->dwt_comp_available = cm->dwt_num_comp;
  1521. cm->dwt_comparator_list = calloc(cm->dwt_num_comp,
  1522. sizeof(struct cortex_m_dwt_comparator));
  1523. if (!cm->dwt_comparator_list) {
  1524. fail0:
  1525. cm->dwt_num_comp = 0;
  1526. LOG_ERROR("out of mem");
  1527. return;
  1528. }
  1529. cache = calloc(1, sizeof *cache);
  1530. if (!cache) {
  1531. fail1:
  1532. free(cm->dwt_comparator_list);
  1533. goto fail0;
  1534. }
  1535. cache->name = "Cortex-M DWT registers";
  1536. cache->num_regs = 2 + cm->dwt_num_comp * 3;
  1537. cache->reg_list = calloc(cache->num_regs, sizeof *cache->reg_list);
  1538. if (!cache->reg_list) {
  1539. free(cache);
  1540. goto fail1;
  1541. }
  1542. for (reg = 0; reg < 2; reg++)
  1543. cortex_m_dwt_addreg(target, cache->reg_list + reg,
  1544. dwt_base_regs + reg);
  1545. comparator = cm->dwt_comparator_list;
  1546. for (i = 0; i < cm->dwt_num_comp; i++, comparator++) {
  1547. int j;
  1548. comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
  1549. for (j = 0; j < 3; j++, reg++)
  1550. cortex_m_dwt_addreg(target, cache->reg_list + reg,
  1551. dwt_comp + 3 * i + j);
  1552. /* make sure we clear any watchpoints enabled on the target */
  1553. target_write_u32(target, comparator->dwt_comparator_address + 8, 0);
  1554. }
  1555. *register_get_last_cache_p(&target->reg_cache) = cache;
  1556. cm->dwt_cache = cache;
  1557. LOG_DEBUG("DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
  1558. dwtcr, cm->dwt_num_comp,
  1559. (dwtcr & (0xf << 24)) ? " only" : "/trigger");
  1560. /* REVISIT: if num_comp > 1, check whether comparator #1 can
  1561. * implement single-address data value watchpoints ... so we
  1562. * won't need to check it later, when asked to set one up.
  1563. */
  1564. }
  1565. static void cortex_m_dwt_free(struct target *target)
  1566. {
  1567. struct cortex_m_common *cm = target_to_cm(target);
  1568. struct reg_cache *cache = cm->dwt_cache;
  1569. free(cm->dwt_comparator_list);
  1570. cm->dwt_comparator_list = NULL;
  1571. cm->dwt_num_comp = 0;
  1572. if (cache) {
  1573. register_unlink_cache(&target->reg_cache, cache);
  1574. if (cache->reg_list) {
  1575. for (size_t i = 0; i < cache->num_regs; i++)
  1576. free(cache->reg_list[i].arch_info);
  1577. free(cache->reg_list);
  1578. }
  1579. free(cache);
  1580. }
  1581. cm->dwt_cache = NULL;
  1582. }
  1583. #define MVFR0 0xe000ef40
  1584. #define MVFR1 0xe000ef44
  1585. #define MVFR0_DEFAULT_M4 0x10110021
  1586. #define MVFR1_DEFAULT_M4 0x11000011
  1587. #define MVFR0_DEFAULT_M7_SP 0x10110021
  1588. #define MVFR0_DEFAULT_M7_DP 0x10110221
  1589. #define MVFR1_DEFAULT_M7_SP 0x11000011
  1590. #define MVFR1_DEFAULT_M7_DP 0x12000011
  1591. int cortex_m_examine(struct target *target)
  1592. {
  1593. int retval;
  1594. uint32_t cpuid, fpcr, mvfr0, mvfr1;
  1595. int i;
  1596. struct cortex_m_common *cortex_m = target_to_cm(target);
  1597. struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
  1598. struct armv7m_common *armv7m = target_to_armv7m(target);
  1599. /* stlink shares the examine handler but does not support
  1600. * all its calls */
  1601. if (!armv7m->stlink) {
  1602. retval = dap_dp_init(swjdp);
  1603. if (retval != ERROR_OK) {
  1604. LOG_ERROR("Could not initialize the debug port");
  1605. return retval;
  1606. }
  1607. if (cortex_m->apsel < 0) {
  1608. /* Search for the MEM-AP */
  1609. retval = dap_find_ap(swjdp, AP_TYPE_AHB_AP, &armv7m->debug_ap);
  1610. if (retval != ERROR_OK) {
  1611. LOG_ERROR("Could not find MEM-AP to control the core");
  1612. return retval;
  1613. }
  1614. } else {
  1615. armv7m->debug_ap = dap_ap(swjdp, cortex_m->apsel);
  1616. }
  1617. /* Leave (only) generic DAP stuff for debugport_init(); */
  1618. armv7m->debug_ap->memaccess_tck = 8;
  1619. retval = mem_ap_init(armv7m->debug_ap);
  1620. if (retval != ERROR_OK)
  1621. return retval;
  1622. }
  1623. if (!target_was_examined(target)) {
  1624. target_set_examined(target);
  1625. /* Read from Device Identification Registers */
  1626. retval = target_read_u32(target, CPUID, &cpuid);
  1627. if (retval != ERROR_OK)
  1628. return retval;
  1629. /* Get CPU Type */
  1630. i = (cpuid >> 4) & 0xf;
  1631. LOG_DEBUG("Cortex-M%d r%" PRId8 "p%" PRId8 " processor detected",
  1632. i, (uint8_t)((cpuid >> 20) & 0xf), (uint8_t)((cpuid >> 0) & 0xf));
  1633. if (i == 7) {
  1634. uint8_t rev, patch;
  1635. rev = (cpuid >> 20) & 0xf;
  1636. patch = (cpuid >> 0) & 0xf;
  1637. if ((rev == 0) && (patch < 2))
  1638. LOG_WARNING("Silicon bug: single stepping will enter pending exception handler!");
  1639. }
  1640. LOG_DEBUG("cpuid: 0x%8.8" PRIx32 "", cpuid);
  1641. if (i == 4) {
  1642. target_read_u32(target, MVFR0, &mvfr0);
  1643. target_read_u32(target, MVFR1, &mvfr1);
  1644. /* test for floating point feature on Cortex-M4 */
  1645. if ((mvfr0 == MVFR0_DEFAULT_M4) && (mvfr1 == MVFR1_DEFAULT_M4)) {
  1646. LOG_DEBUG("Cortex-M%d floating point feature FPv4_SP found", i);
  1647. armv7m->fp_feature = FPv4_SP;
  1648. }
  1649. } else if (i == 7) {
  1650. target_read_u32(target, MVFR0, &mvfr0);
  1651. target_read_u32(target, MVFR1, &mvfr1);
  1652. /* test for floating point features on Cortex-M7 */
  1653. if ((mvfr0 == MVFR0_DEFAULT_M7_SP) && (mvfr1 == MVFR1_DEFAULT_M7_SP)) {
  1654. LOG_DEBUG("Cortex-M%d floating point feature FPv5_SP found", i);
  1655. armv7m->fp_feature = FPv5_SP;
  1656. } else if ((mvfr0 == MVFR0_DEFAULT_M7_DP) && (mvfr1 == MVFR1_DEFAULT_M7_DP)) {
  1657. LOG_DEBUG("Cortex-M%d floating point feature FPv5_DP found", i);
  1658. armv7m->fp_feature = FPv5_DP;
  1659. }
  1660. } else if (i == 0) {
  1661. /* Cortex-M0 does not support unaligned memory access */
  1662. armv7m->arm.is_armv6m = true;
  1663. }
  1664. if (armv7m->fp_feature == FP_NONE &&
  1665. armv7m->arm.core_cache->num_regs > ARMV7M_NUM_CORE_REGS_NOFP) {
  1666. /* free unavailable FPU registers */
  1667. size_t idx;
  1668. for (idx = ARMV7M_NUM_CORE_REGS_NOFP;
  1669. idx < armv7m->arm.core_cache->num_regs;
  1670. idx++) {
  1671. free(armv7m->arm.core_cache->reg_list[idx].value);
  1672. free(armv7m->arm.core_cache->reg_list[idx].feature);
  1673. free(armv7m->arm.core_cache->reg_list[idx].reg_data_type);
  1674. }
  1675. armv7m->arm.core_cache->num_regs = ARMV7M_NUM_CORE_REGS_NOFP;
  1676. }
  1677. if (!armv7m->stlink) {
  1678. if (i == 3 || i == 4)
  1679. /* Cortex-M3/M4 have 4096 bytes autoincrement range,
  1680. * s. ARM IHI 0031C: MEM-AP 7.2.2 */
  1681. armv7m->debug_ap->tar_autoincr_block = (1 << 12);
  1682. else if (i == 7)
  1683. /* Cortex-M7 has only 1024 bytes autoincrement range */
  1684. armv7m->debug_ap->tar_autoincr_block = (1 << 10);
  1685. }
  1686. /* Configure trace modules */
  1687. retval = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
  1688. if (retval != ERROR_OK)
  1689. return retval;
  1690. if (armv7m->trace_config.config_type != DISABLED) {
  1691. armv7m_trace_tpiu_config(target);
  1692. armv7m_trace_itm_config(target);
  1693. }
  1694. /* NOTE: FPB and DWT are both optional. */
  1695. /* Setup FPB */
  1696. target_read_u32(target, FP_CTRL, &fpcr);
  1697. cortex_m->auto_bp_type = 1;
  1698. /* bits [14:12] and [7:4] */
  1699. cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
  1700. cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
  1701. cortex_m->fp_code_available = cortex_m->fp_num_code;
  1702. /* Detect flash patch revision, see RM DDI 0403E.b page C1-817.
  1703. Revision is zero base, fp_rev == 1 means Rev.2 ! */
  1704. cortex_m->fp_rev = (fpcr >> 28) & 0xf;
  1705. free(cortex_m->fp_comparator_list);
  1706. cortex_m->fp_comparator_list = calloc(
  1707. cortex_m->fp_num_code + cortex_m->fp_num_lit,
  1708. sizeof(struct cortex_m_fp_comparator));
  1709. cortex_m->fpb_enabled = fpcr & 1;
  1710. for (i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
  1711. cortex_m->fp_comparator_list[i].type =
  1712. (i < cortex_m->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
  1713. cortex_m->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
  1714. /* make sure we clear any breakpoints enabled on the target */
  1715. target_write_u32(target, cortex_m->fp_comparator_list[i].fpcr_address, 0);
  1716. }
  1717. LOG_DEBUG("FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i",
  1718. fpcr,
  1719. cortex_m->fp_num_code,
  1720. cortex_m->fp_num_lit);
  1721. /* Setup DWT */
  1722. cortex_m_dwt_free(target);
  1723. cortex_m_dwt_setup(cortex_m, target);
  1724. /* These hardware breakpoints only work for code in flash! */
  1725. LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints",
  1726. target_name(target),
  1727. cortex_m->fp_num_code,
  1728. cortex_m->dwt_num_comp);
  1729. }
  1730. return ERROR_OK;
  1731. }
  1732. static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
  1733. {
  1734. struct armv7m_common *armv7m = target_to_armv7m(target);
  1735. uint16_t dcrdr;
  1736. uint8_t buf[2];
  1737. int retval;
  1738. retval = mem_ap_read_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
  1739. if (retval != ERROR_OK)
  1740. return retval;
  1741. dcrdr = target_buffer_get_u16(target, buf);
  1742. *ctrl = (uint8_t)dcrdr;
  1743. *value = (uint8_t)(dcrdr >> 8);
  1744. LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
  1745. /* write ack back to software dcc register
  1746. * signify we have read data */
  1747. if (dcrdr & (1 << 0)) {
  1748. target_buffer_set_u16(target, buf, 0);
  1749. retval = mem_ap_write_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
  1750. if (retval != ERROR_OK)
  1751. return retval;
  1752. }
  1753. return ERROR_OK;
  1754. }
  1755. static int cortex_m_target_request_data(struct target *target,
  1756. uint32_t size, uint8_t *buffer)
  1757. {
  1758. uint8_t data;
  1759. uint8_t ctrl;
  1760. uint32_t i;
  1761. for (i = 0; i < (size * 4); i++) {
  1762. int retval = cortex_m_dcc_read(target, &data, &ctrl);
  1763. if (retval != ERROR_OK)
  1764. return retval;
  1765. buffer[i] = data;
  1766. }
  1767. return ERROR_OK;
  1768. }
  1769. static int cortex_m_handle_target_request(void *priv)
  1770. {
  1771. struct target *target = priv;
  1772. if (!target_was_examined(target))
  1773. return ERROR_OK;
  1774. if (!target->dbg_msg_enabled)
  1775. return ERROR_OK;
  1776. if (target->state == TARGET_RUNNING) {
  1777. uint8_t data;
  1778. uint8_t ctrl;
  1779. int retval;
  1780. retval = cortex_m_dcc_read(target, &data, &ctrl);
  1781. if (retval != ERROR_OK)
  1782. return retval;
  1783. /* check if we have data */
  1784. if (ctrl & (1 << 0)) {
  1785. uint32_t request;
  1786. /* we assume target is quick enough */
  1787. request = data;
  1788. for (int i = 1; i <= 3; i++) {
  1789. retval = cortex_m_dcc_read(target, &data, &ctrl);
  1790. if (retval != ERROR_OK)
  1791. return retval;
  1792. request |= ((uint32_t)data << (i * 8));
  1793. }
  1794. target_request(target, request);
  1795. }
  1796. }
  1797. return ERROR_OK;
  1798. }
  1799. static int cortex_m_init_arch_info(struct target *target,
  1800. struct cortex_m_common *cortex_m, struct jtag_tap *tap)
  1801. {
  1802. struct armv7m_common *armv7m = &cortex_m->armv7m;
  1803. armv7m_init_arch_info(target, armv7m);
  1804. /* tap has no dap initialized */
  1805. if (!tap->dap) {
  1806. tap->dap = dap_init();
  1807. /* Leave (only) generic DAP stuff for debugport_init() */
  1808. tap->dap->tap = tap;
  1809. }
  1810. /* default reset mode is to use srst if fitted
  1811. * if not it will use CORTEX_M3_RESET_VECTRESET */
  1812. cortex_m->soft_reset_config = CORTEX_M_RESET_VECTRESET;
  1813. armv7m->arm.dap = tap->dap;
  1814. /* register arch-specific functions */
  1815. armv7m->examine_debug_reason = cortex_m_examine_debug_reason;
  1816. armv7m->post_debug_entry = NULL;
  1817. armv7m->pre_restore_context = NULL;
  1818. armv7m->load_core_reg_u32 = cortex_m_load_core_reg_u32;
  1819. armv7m->store_core_reg_u32 = cortex_m_store_core_reg_u32;
  1820. target_register_timer_callback(cortex_m_handle_target_request, 1, 1, target);
  1821. return ERROR_OK;
  1822. }
  1823. static int cortex_m_target_create(struct target *target, Jim_Interp *interp)
  1824. {
  1825. struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
  1826. cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
  1827. cortex_m_init_arch_info(target, cortex_m, target->tap);
  1828. if (target->private_config != NULL) {
  1829. struct adiv5_private_config *pc =
  1830. (struct adiv5_private_config *)target->private_config;
  1831. cortex_m->apsel = pc->ap_num;
  1832. } else
  1833. cortex_m->apsel = -1;
  1834. return ERROR_OK;
  1835. }
  1836. /*--------------------------------------------------------------------------*/
  1837. static int cortex_m_verify_pointer(struct command_context *cmd_ctx,
  1838. struct cortex_m_common *cm)
  1839. {
  1840. if (cm->common_magic != CORTEX_M_COMMON_MAGIC) {
  1841. command_print(cmd_ctx, "target is not a Cortex-M");
  1842. return ERROR_TARGET_INVALID;
  1843. }
  1844. return ERROR_OK;
  1845. }
  1846. /*
  1847. * Only stuff below this line should need to verify that its target
  1848. * is a Cortex-M3. Everything else should have indirected through the
  1849. * cortexm3_target structure, which is only used with CM3 targets.
  1850. */
  1851. static const struct {
  1852. char name[10];
  1853. unsigned mask;
  1854. } vec_ids[] = {
  1855. { "hard_err", VC_HARDERR, },
  1856. { "int_err", VC_INTERR, },
  1857. { "bus_err", VC_BUSERR, },
  1858. { "state_err", VC_STATERR, },
  1859. { "chk_err", VC_CHKERR, },
  1860. { "nocp_err", VC_NOCPERR, },
  1861. { "mm_err", VC_MMERR, },
  1862. { "reset", VC_CORERESET, },
  1863. };
  1864. COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
  1865. {
  1866. struct target *target = get_current_target(CMD_CTX);
  1867. struct cortex_m_common *cortex_m = target_to_cm(target);
  1868. struct armv7m_common *armv7m = &cortex_m->armv7m;
  1869. uint32_t demcr = 0;
  1870. int retval;
  1871. retval = cortex_m_verify_pointer(CMD_CTX, cortex_m);
  1872. if (retval != ERROR_OK)
  1873. return retval;
  1874. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
  1875. if (retval != ERROR_OK)
  1876. return retval;
  1877. if (CMD_ARGC > 0) {
  1878. unsigned catch = 0;
  1879. if (CMD_ARGC == 1) {
  1880. if (strcmp(CMD_ARGV[0], "all") == 0) {
  1881. catch = VC_HARDERR | VC_INTERR | VC_BUSERR
  1882. | VC_STATERR | VC_CHKERR | VC_NOCPERR
  1883. | VC_MMERR | VC_CORERESET;
  1884. goto write;
  1885. } else if (strcmp(CMD_ARGV[0], "none") == 0)
  1886. goto write;
  1887. }
  1888. while (CMD_ARGC-- > 0) {
  1889. unsigned i;
  1890. for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
  1891. if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
  1892. continue;
  1893. catch |= vec_ids[i].mask;
  1894. break;
  1895. }
  1896. if (i == ARRAY_SIZE(vec_ids)) {
  1897. LOG_ERROR("No CM3 vector '%s'", CMD_ARGV[CMD_ARGC]);
  1898. return ERROR_COMMAND_SYNTAX_ERROR;
  1899. }
  1900. }
  1901. write:
  1902. /* For now, armv7m->demcr only stores vector catch flags. */
  1903. armv7m->demcr = catch;
  1904. demcr &= ~0xffff;
  1905. demcr |= catch;
  1906. /* write, but don't assume it stuck (why not??) */
  1907. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, demcr);
  1908. if (retval != ERROR_OK)
  1909. return retval;
  1910. retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
  1911. if (retval != ERROR_OK)
  1912. return retval;
  1913. /* FIXME be sure to clear DEMCR on clean server shutdown.
  1914. * Otherwise the vector catch hardware could fire when there's
  1915. * no debugger hooked up, causing much confusion...
  1916. */
  1917. }
  1918. for (unsigned i = 0; i < ARRAY_SIZE(vec_ids); i++) {
  1919. command_print(CMD_CTX, "%9s: %s", vec_ids[i].name,
  1920. (demcr & vec_ids[i].mask) ? "catch" : "ignore");
  1921. }
  1922. return ERROR_OK;
  1923. }
  1924. COMMAND_HANDLER(handle_cortex_m_mask_interrupts_command)
  1925. {
  1926. struct target *target = get_current_target(CMD_CTX);
  1927. struct cortex_m_common *cortex_m = target_to_cm(target);
  1928. int retval;
  1929. static const Jim_Nvp nvp_maskisr_modes[] = {
  1930. { .name = "auto", .value = CORTEX_M_ISRMASK_AUTO },
  1931. { .name = "off", .value = CORTEX_M_ISRMASK_OFF },
  1932. { .name = "on", .value = CORTEX_M_ISRMASK_ON },
  1933. { .name = NULL, .value = -1 },
  1934. };
  1935. const Jim_Nvp *n;
  1936. retval = cortex_m_verify_pointer(CMD_CTX, cortex_m);
  1937. if (retval != ERROR_OK)
  1938. return retval;
  1939. if (target->state != TARGET_HALTED) {
  1940. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  1941. return ERROR_OK;
  1942. }
  1943. if (CMD_ARGC > 0) {
  1944. n = Jim_Nvp_name2value_simple(nvp_maskisr_modes, CMD_ARGV[0]);
  1945. if (n->name == NULL)
  1946. return ERROR_COMMAND_SYNTAX_ERROR;
  1947. cortex_m->isrmasking_mode = n->value;
  1948. if (cortex_m->isrmasking_mode == CORTEX_M_ISRMASK_ON)
  1949. cortex_m_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
  1950. else
  1951. cortex_m_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
  1952. }
  1953. n = Jim_Nvp_value2name_simple(nvp_maskisr_modes, cortex_m->isrmasking_mode);
  1954. command_print(CMD_CTX, "cortex_m interrupt mask %s", n->name);
  1955. return ERROR_OK;
  1956. }
  1957. COMMAND_HANDLER(handle_cortex_m_reset_config_command)
  1958. {
  1959. struct target *target = get_current_target(CMD_CTX);
  1960. struct cortex_m_common *cortex_m = target_to_cm(target);
  1961. int retval;
  1962. char *reset_config;
  1963. retval = cortex_m_verify_pointer(CMD_CTX, cortex_m);
  1964. if (retval != ERROR_OK)
  1965. return retval;
  1966. if (CMD_ARGC > 0) {
  1967. if (strcmp(*CMD_ARGV, "sysresetreq") == 0)
  1968. cortex_m->soft_reset_config = CORTEX_M_RESET_SYSRESETREQ;
  1969. else if (strcmp(*CMD_ARGV, "vectreset") == 0)
  1970. cortex_m->soft_reset_config = CORTEX_M_RESET_VECTRESET;
  1971. }
  1972. switch (cortex_m->soft_reset_config) {
  1973. case CORTEX_M_RESET_SYSRESETREQ:
  1974. reset_config = "sysresetreq";
  1975. break;
  1976. case CORTEX_M_RESET_VECTRESET:
  1977. reset_config = "vectreset";
  1978. break;
  1979. default:
  1980. reset_config = "unknown";
  1981. break;
  1982. }
  1983. command_print(CMD_CTX, "cortex_m reset_config %s", reset_config);
  1984. return ERROR_OK;
  1985. }
  1986. static const struct command_registration cortex_m_exec_command_handlers[] = {
  1987. {
  1988. .name = "maskisr",
  1989. .handler = handle_cortex_m_mask_interrupts_command,
  1990. .mode = COMMAND_EXEC,
  1991. .help = "mask cortex_m interrupts",
  1992. .usage = "['auto'|'on'|'off']",
  1993. },
  1994. {
  1995. .name = "vector_catch",
  1996. .handler = handle_cortex_m_vector_catch_command,
  1997. .mode = COMMAND_EXEC,
  1998. .help = "configure hardware vectors to trigger debug entry",
  1999. .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
  2000. },
  2001. {
  2002. .name = "reset_config",
  2003. .handler = handle_cortex_m_reset_config_command,
  2004. .mode = COMMAND_ANY,
  2005. .help = "configure software reset handling",
  2006. .usage = "['srst'|'sysresetreq'|'vectreset']",
  2007. },
  2008. COMMAND_REGISTRATION_DONE
  2009. };
  2010. static const struct command_registration cortex_m_command_handlers[] = {
  2011. {
  2012. .chain = armv7m_command_handlers,
  2013. },
  2014. {
  2015. .chain = armv7m_trace_command_handlers,
  2016. },
  2017. {
  2018. .name = "cortex_m",
  2019. .mode = COMMAND_EXEC,
  2020. .help = "Cortex-M command group",
  2021. .usage = "",
  2022. .chain = cortex_m_exec_command_handlers,
  2023. },
  2024. COMMAND_REGISTRATION_DONE
  2025. };
  2026. struct target_type cortexm_target = {
  2027. .name = "cortex_m",
  2028. .deprecated_name = "cortex_m3",
  2029. .poll = cortex_m_poll,
  2030. .arch_state = armv7m_arch_state,
  2031. .target_request_data = cortex_m_target_request_data,
  2032. .halt = cortex_m_halt,
  2033. .resume = cortex_m_resume,
  2034. .step = cortex_m_step,
  2035. .assert_reset = cortex_m_assert_reset,
  2036. .deassert_reset = cortex_m_deassert_reset,
  2037. .soft_reset_halt = cortex_m_soft_reset_halt,
  2038. .get_gdb_reg_list = armv7m_get_gdb_reg_list,
  2039. .read_memory = cortex_m_read_memory,
  2040. .write_memory = cortex_m_write_memory,
  2041. .checksum_memory = armv7m_checksum_memory,
  2042. .blank_check_memory = armv7m_blank_check_memory,
  2043. .run_algorithm = armv7m_run_algorithm,
  2044. .start_algorithm = armv7m_start_algorithm,
  2045. .wait_algorithm = armv7m_wait_algorithm,
  2046. .add_breakpoint = cortex_m_add_breakpoint,
  2047. .remove_breakpoint = cortex_m_remove_breakpoint,
  2048. .add_watchpoint = cortex_m_add_watchpoint,
  2049. .remove_watchpoint = cortex_m_remove_watchpoint,
  2050. .commands = cortex_m_command_handlers,
  2051. .target_create = cortex_m_target_create,
  2052. .target_jim_configure = adiv5_jim_configure,
  2053. .init_target = cortex_m_init_target,
  2054. .examine = cortex_m_examine,
  2055. .deinit_target = cortex_m_deinit_target,
  2056. };