1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2006 by Magnus Lundin *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
14 * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
16 ***************************************************************************/
21 #include "jtag/interface.h"
22 #include "breakpoints.h"
24 #include "target_request.h"
25 #include "target_type.h"
26 #include "arm_adi_v5.h"
27 #include "arm_disassembler.h"
29 #include "arm_opcodes.h"
30 #include "arm_semihosting.h"
31 #include <helper/time_support.h>
34 /* NOTE: most of this should work fine for the Cortex-M1 and
35 * Cortex-M0 cores too, although they're ARMv6-M not ARMv7-M.
36 * Some differences: M0/M1 doesn't have FPB remapping or the
37 * DWT tracing/profiling support. (So the cycle counter will
38 * not be usable; the other stuff isn't currently used here.)
40 * Although there are some workarounds for errata seen only in r0p0
41 * silicon, such old parts are hard to find and thus not much tested
45 /* Timeout for register r/w */
46 #define DHCSR_S_REGRDY_TIMEOUT (500)
48 /* Supported Cortex-M Cores */
49 static const struct cortex_m_part_info cortex_m_parts[] = {
51 .partno = CORTEX_M0_PARTNO,
56 .partno = CORTEX_M0P_PARTNO,
61 .partno = CORTEX_M1_PARTNO,
66 .partno = CORTEX_M3_PARTNO,
69 .flags = CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K,
72 .partno = CORTEX_M4_PARTNO,
75 .flags = CORTEX_M_F_HAS_FPV4 | CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K,
78 .partno = CORTEX_M7_PARTNO,
81 .flags = CORTEX_M_F_HAS_FPV5,
84 .partno = CORTEX_M23_PARTNO,
89 .partno = CORTEX_M33_PARTNO,
92 .flags = CORTEX_M_F_HAS_FPV5,
95 .partno = CORTEX_M35P_PARTNO,
96 .name = "Cortex-M35P",
98 .flags = CORTEX_M_F_HAS_FPV5,
101 .partno = CORTEX_M55_PARTNO,
102 .name = "Cortex-M55",
103 .arch = ARM_ARCH_V8M,
104 .flags = CORTEX_M_F_HAS_FPV5,
108 /* forward declarations */
109 static int cortex_m_store_core_reg_u32(struct target *target,
110 uint32_t num, uint32_t value);
111 static void cortex_m_dwt_free(struct target *target);
113 /** DCB DHCSR register contains S_RETIRE_ST and S_RESET_ST bits cleared
114 * on a read. Call this helper function each time DHCSR is read
115 * to preserve S_RESET_ST state in case of a reset event was detected.
117 static inline void cortex_m_cumulate_dhcsr_sticky(struct cortex_m_common *cortex_m,
120 cortex_m->dcb_dhcsr_cumulated_sticky |= dhcsr;
123 /** Read DCB DHCSR register to cortex_m->dcb_dhcsr and cumulate
124 * sticky bits in cortex_m->dcb_dhcsr_cumulated_sticky
126 static int cortex_m_read_dhcsr_atomic_sticky(struct target *target)
128 struct cortex_m_common *cortex_m = target_to_cm(target);
129 struct armv7m_common *armv7m = target_to_armv7m(target);
131 int retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
132 &cortex_m->dcb_dhcsr);
133 if (retval != ERROR_OK)
136 cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
140 static int cortex_m_load_core_reg_u32(struct target *target,
141 uint32_t regsel, uint32_t *value)
143 struct cortex_m_common *cortex_m = target_to_cm(target);
144 struct armv7m_common *armv7m = target_to_armv7m(target);
146 uint32_t dcrdr, tmp_value;
149 /* because the DCB_DCRDR is used for the emulated dcc channel
150 * we have to save/restore the DCB_DCRDR when used */
151 if (target->dbg_msg_enabled) {
152 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
153 if (retval != ERROR_OK)
157 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
158 if (retval != ERROR_OK)
161 /* check if value from register is ready and pre-read it */
164 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR,
165 &cortex_m->dcb_dhcsr);
166 if (retval != ERROR_OK)
168 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR,
170 if (retval != ERROR_OK)
172 cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
173 if (cortex_m->dcb_dhcsr & S_REGRDY)
175 cortex_m->slow_register_read = true; /* Polling (still) needed. */
176 if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
177 LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
178 return ERROR_TIMEOUT_REACHED;
185 if (target->dbg_msg_enabled) {
186 /* restore DCB_DCRDR - this needs to be in a separate
187 * transaction otherwise the emulated DCC channel breaks */
188 if (retval == ERROR_OK)
189 retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
195 static int cortex_m_slow_read_all_regs(struct target *target)
197 struct cortex_m_common *cortex_m = target_to_cm(target);
198 struct armv7m_common *armv7m = target_to_armv7m(target);
199 const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
201 /* Opportunistically restore fast read, it'll revert to slow
202 * if any register needed polling in cortex_m_load_core_reg_u32(). */
203 cortex_m->slow_register_read = false;
205 for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) {
206 struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
208 int retval = armv7m->arm.read_core_reg(target, r, reg_id, ARM_MODE_ANY);
209 if (retval != ERROR_OK)
214 if (!cortex_m->slow_register_read)
215 LOG_TARGET_DEBUG(target, "Switching back to fast register reads");
220 static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel,
221 uint32_t *reg_value, uint32_t *dhcsr)
223 struct armv7m_common *armv7m = target_to_armv7m(target);
226 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
227 if (retval != ERROR_OK)
230 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR, dhcsr);
231 if (retval != ERROR_OK)
234 return mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, reg_value);
237 static int cortex_m_fast_read_all_regs(struct target *target)
239 struct cortex_m_common *cortex_m = target_to_cm(target);
240 struct armv7m_common *armv7m = target_to_armv7m(target);
244 /* because the DCB_DCRDR is used for the emulated dcc channel
245 * we have to save/restore the DCB_DCRDR when used */
246 if (target->dbg_msg_enabled) {
247 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
248 if (retval != ERROR_OK)
252 const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
253 const unsigned int n_r32 = ARMV7M_LAST_REG - ARMV7M_CORE_FIRST_REG + 1
254 + ARMV7M_FPU_LAST_REG - ARMV7M_FPU_FIRST_REG + 1;
255 /* we need one 32-bit word for each register except FP D0..D15, which
257 uint32_t r_vals[n_r32];
258 uint32_t dhcsr[n_r32];
260 unsigned int wi = 0; /* write index to r_vals and dhcsr arrays */
261 unsigned int reg_id; /* register index in the reg_list, ARMV7M_R0... */
262 for (reg_id = 0; reg_id < num_regs; reg_id++) {
263 struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
265 continue; /* skip non existent registers */
268 /* Any 8-bit or shorter register is unpacked from a 32-bit
269 * container register. Skip it now. */
273 uint32_t regsel = armv7m_map_id_to_regsel(reg_id);
274 retval = cortex_m_queue_reg_read(target, regsel, &r_vals[wi],
276 if (retval != ERROR_OK)
280 assert(r->size == 32 || r->size == 64);
282 continue; /* done with 32-bit register */
284 assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
285 /* the odd part of FP register (S1, S3...) */
286 retval = cortex_m_queue_reg_read(target, regsel + 1, &r_vals[wi],
288 if (retval != ERROR_OK)
295 retval = dap_run(armv7m->debug_ap->dap);
296 if (retval != ERROR_OK)
299 if (target->dbg_msg_enabled) {
300 /* restore DCB_DCRDR - this needs to be in a separate
301 * transaction otherwise the emulated DCC channel breaks */
302 retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
303 if (retval != ERROR_OK)
307 bool not_ready = false;
308 for (unsigned int i = 0; i < wi; i++) {
309 if ((dhcsr[i] & S_REGRDY) == 0) {
311 LOG_TARGET_DEBUG(target, "Register %u was not ready during fast read", i);
313 cortex_m_cumulate_dhcsr_sticky(cortex_m, dhcsr[i]);
317 /* Any register was not ready,
318 * fall back to slow read with S_REGRDY polling */
319 return ERROR_TIMEOUT_REACHED;
322 LOG_TARGET_DEBUG(target, "read %u 32-bit registers", wi);
324 unsigned int ri = 0; /* read index from r_vals array */
325 for (reg_id = 0; reg_id < num_regs; reg_id++) {
326 struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
328 continue; /* skip non existent registers */
332 unsigned int reg32_id;
334 if (armv7m_map_reg_packing(reg_id, ®32_id, &offset)) {
335 /* Unpack a partial register from 32-bit container register */
336 struct reg *r32 = &armv7m->arm.core_cache->reg_list[reg32_id];
338 /* The container register ought to precede all regs unpacked
339 * from it in the reg_list. So the value should be ready
342 buf_cpy(r32->value + offset, r->value, r->size);
345 assert(r->size == 32 || r->size == 64);
346 buf_set_u32(r->value, 0, 32, r_vals[ri++]);
349 assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
350 /* the odd part of FP register (S1, S3...) */
351 buf_set_u32(r->value + 4, 0, 32, r_vals[ri++]);
361 static int cortex_m_store_core_reg_u32(struct target *target,
362 uint32_t regsel, uint32_t value)
364 struct cortex_m_common *cortex_m = target_to_cm(target);
365 struct armv7m_common *armv7m = target_to_armv7m(target);
370 /* because the DCB_DCRDR is used for the emulated dcc channel
371 * we have to save/restore the DCB_DCRDR when used */
372 if (target->dbg_msg_enabled) {
373 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
374 if (retval != ERROR_OK)
378 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, value);
379 if (retval != ERROR_OK)
382 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
383 if (retval != ERROR_OK)
386 /* check if value is written into register */
389 retval = cortex_m_read_dhcsr_atomic_sticky(target);
390 if (retval != ERROR_OK)
392 if (cortex_m->dcb_dhcsr & S_REGRDY)
394 if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
395 LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
396 return ERROR_TIMEOUT_REACHED;
401 if (target->dbg_msg_enabled) {
402 /* restore DCB_DCRDR - this needs to be in a separate
403 * transaction otherwise the emulated DCC channel breaks */
404 if (retval == ERROR_OK)
405 retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
411 static int cortex_m_write_debug_halt_mask(struct target *target,
412 uint32_t mask_on, uint32_t mask_off)
414 struct cortex_m_common *cortex_m = target_to_cm(target);
415 struct armv7m_common *armv7m = &cortex_m->armv7m;
417 /* mask off status bits */
418 cortex_m->dcb_dhcsr &= ~((0xFFFFul << 16) | mask_off);
419 /* create new register mask */
420 cortex_m->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
422 return mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR, cortex_m->dcb_dhcsr);
425 static int cortex_m_set_maskints(struct target *target, bool mask)
427 struct cortex_m_common *cortex_m = target_to_cm(target);
428 if (!!(cortex_m->dcb_dhcsr & C_MASKINTS) != mask)
429 return cortex_m_write_debug_halt_mask(target, mask ? C_MASKINTS : 0, mask ? 0 : C_MASKINTS);
434 static int cortex_m_set_maskints_for_halt(struct target *target)
436 struct cortex_m_common *cortex_m = target_to_cm(target);
437 switch (cortex_m->isrmasking_mode) {
438 case CORTEX_M_ISRMASK_AUTO:
439 /* interrupts taken at resume, whether for step or run -> no mask */
440 return cortex_m_set_maskints(target, false);
442 case CORTEX_M_ISRMASK_OFF:
443 /* interrupts never masked */
444 return cortex_m_set_maskints(target, false);
446 case CORTEX_M_ISRMASK_ON:
447 /* interrupts always masked */
448 return cortex_m_set_maskints(target, true);
450 case CORTEX_M_ISRMASK_STEPONLY:
451 /* interrupts masked for single step only -> mask now if MASKINTS
452 * erratum, otherwise only mask before stepping */
453 return cortex_m_set_maskints(target, cortex_m->maskints_erratum);
458 static int cortex_m_set_maskints_for_run(struct target *target)
460 switch (target_to_cm(target)->isrmasking_mode) {
461 case CORTEX_M_ISRMASK_AUTO:
462 /* interrupts taken at resume, whether for step or run -> no mask */
463 return cortex_m_set_maskints(target, false);
465 case CORTEX_M_ISRMASK_OFF:
466 /* interrupts never masked */
467 return cortex_m_set_maskints(target, false);
469 case CORTEX_M_ISRMASK_ON:
470 /* interrupts always masked */
471 return cortex_m_set_maskints(target, true);
473 case CORTEX_M_ISRMASK_STEPONLY:
474 /* interrupts masked for single step only -> no mask */
475 return cortex_m_set_maskints(target, false);
480 static int cortex_m_set_maskints_for_step(struct target *target)
482 switch (target_to_cm(target)->isrmasking_mode) {
483 case CORTEX_M_ISRMASK_AUTO:
484 /* the auto-interrupt should already be done -> mask */
485 return cortex_m_set_maskints(target, true);
487 case CORTEX_M_ISRMASK_OFF:
488 /* interrupts never masked */
489 return cortex_m_set_maskints(target, false);
491 case CORTEX_M_ISRMASK_ON:
492 /* interrupts always masked */
493 return cortex_m_set_maskints(target, true);
495 case CORTEX_M_ISRMASK_STEPONLY:
496 /* interrupts masked for single step only -> mask */
497 return cortex_m_set_maskints(target, true);
502 static int cortex_m_clear_halt(struct target *target)
504 struct cortex_m_common *cortex_m = target_to_cm(target);
505 struct armv7m_common *armv7m = &cortex_m->armv7m;
508 /* clear step if any */
509 cortex_m_write_debug_halt_mask(target, C_HALT, C_STEP);
511 /* Read Debug Fault Status Register */
512 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR, &cortex_m->nvic_dfsr);
513 if (retval != ERROR_OK)
516 /* Clear Debug Fault Status */
517 retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_DFSR, cortex_m->nvic_dfsr);
518 if (retval != ERROR_OK)
520 LOG_TARGET_DEBUG(target, "NVIC_DFSR 0x%" PRIx32 "", cortex_m->nvic_dfsr);
525 static int cortex_m_single_step_core(struct target *target)
527 struct cortex_m_common *cortex_m = target_to_cm(target);
530 /* Mask interrupts before clearing halt, if not done already. This avoids
531 * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
532 * HALT can put the core into an unknown state.
534 if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) {
535 retval = cortex_m_write_debug_halt_mask(target, C_MASKINTS, 0);
536 if (retval != ERROR_OK)
539 retval = cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
540 if (retval != ERROR_OK)
542 LOG_TARGET_DEBUG(target, "single step");
544 /* restore dhcsr reg */
545 cortex_m_clear_halt(target);
550 static int cortex_m_enable_fpb(struct target *target)
552 int retval = target_write_u32(target, FP_CTRL, 3);
553 if (retval != ERROR_OK)
556 /* check the fpb is actually enabled */
558 retval = target_read_u32(target, FP_CTRL, &fpctrl);
559 if (retval != ERROR_OK)
568 static int cortex_m_endreset_event(struct target *target)
572 struct cortex_m_common *cortex_m = target_to_cm(target);
573 struct armv7m_common *armv7m = &cortex_m->armv7m;
574 struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
575 struct cortex_m_fp_comparator *fp_list = cortex_m->fp_comparator_list;
576 struct cortex_m_dwt_comparator *dwt_list = cortex_m->dwt_comparator_list;
578 /* REVISIT The four debug monitor bits are currently ignored... */
579 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &dcb_demcr);
580 if (retval != ERROR_OK)
582 LOG_TARGET_DEBUG(target, "DCB_DEMCR = 0x%8.8" PRIx32 "", dcb_demcr);
584 /* this register is used for emulated dcc channel */
585 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
586 if (retval != ERROR_OK)
589 retval = cortex_m_read_dhcsr_atomic_sticky(target);
590 if (retval != ERROR_OK)
593 if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
594 /* Enable debug requests */
595 retval = cortex_m_write_debug_halt_mask(target, 0, C_HALT | C_STEP | C_MASKINTS);
596 if (retval != ERROR_OK)
600 /* Restore proper interrupt masking setting for running CPU. */
601 cortex_m_set_maskints_for_run(target);
603 /* Enable features controlled by ITM and DWT blocks, and catch only
604 * the vectors we were told to pay attention to.
606 * Target firmware is responsible for all fault handling policy
607 * choices *EXCEPT* explicitly scripted overrides like "vector_catch"
608 * or manual updates to the NVIC SHCSR and CCR registers.
610 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, TRCENA | armv7m->demcr);
611 if (retval != ERROR_OK)
614 /* Paranoia: evidently some (early?) chips don't preserve all the
615 * debug state (including FPB, DWT, etc) across reset...
619 retval = cortex_m_enable_fpb(target);
620 if (retval != ERROR_OK) {
621 LOG_TARGET_ERROR(target, "Failed to enable the FPB");
625 cortex_m->fpb_enabled = true;
627 /* Restore FPB registers */
628 for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
629 retval = target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
630 if (retval != ERROR_OK)
634 /* Restore DWT registers */
635 for (unsigned int i = 0; i < cortex_m->dwt_num_comp; i++) {
636 retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
638 if (retval != ERROR_OK)
640 retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
642 if (retval != ERROR_OK)
644 retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
645 dwt_list[i].function);
646 if (retval != ERROR_OK)
649 retval = dap_run(swjdp);
650 if (retval != ERROR_OK)
653 register_cache_invalidate(armv7m->arm.core_cache);
655 /* make sure we have latest dhcsr flags */
656 retval = cortex_m_read_dhcsr_atomic_sticky(target);
657 if (retval != ERROR_OK)
663 static int cortex_m_examine_debug_reason(struct target *target)
665 struct cortex_m_common *cortex_m = target_to_cm(target);
667 /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason
668 * only check the debug reason if we don't know it already */
670 if ((target->debug_reason != DBG_REASON_DBGRQ)
671 && (target->debug_reason != DBG_REASON_SINGLESTEP)) {
672 if (cortex_m->nvic_dfsr & DFSR_BKPT) {
673 target->debug_reason = DBG_REASON_BREAKPOINT;
674 if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
675 target->debug_reason = DBG_REASON_WPTANDBKPT;
676 } else if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
677 target->debug_reason = DBG_REASON_WATCHPOINT;
678 else if (cortex_m->nvic_dfsr & DFSR_VCATCH)
679 target->debug_reason = DBG_REASON_BREAKPOINT;
680 else if (cortex_m->nvic_dfsr & DFSR_EXTERNAL)
681 target->debug_reason = DBG_REASON_DBGRQ;
683 target->debug_reason = DBG_REASON_UNDEFINED;
689 static int cortex_m_examine_exception_reason(struct target *target)
691 uint32_t shcsr = 0, except_sr = 0, cfsr = -1, except_ar = -1;
692 struct armv7m_common *armv7m = target_to_armv7m(target);
693 struct adiv5_dap *swjdp = armv7m->arm.dap;
696 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SHCSR, &shcsr);
697 if (retval != ERROR_OK)
699 switch (armv7m->exception_number) {
702 case 3: /* Hard Fault */
703 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_HFSR, &except_sr);
704 if (retval != ERROR_OK)
706 if (except_sr & 0x40000000) {
707 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &cfsr);
708 if (retval != ERROR_OK)
712 case 4: /* Memory Management */
713 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
714 if (retval != ERROR_OK)
716 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_MMFAR, &except_ar);
717 if (retval != ERROR_OK)
720 case 5: /* Bus Fault */
721 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
722 if (retval != ERROR_OK)
724 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_BFAR, &except_ar);
725 if (retval != ERROR_OK)
728 case 6: /* Usage Fault */
729 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
730 if (retval != ERROR_OK)
733 case 7: /* Secure Fault */
734 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFSR, &except_sr);
735 if (retval != ERROR_OK)
737 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFAR, &except_ar);
738 if (retval != ERROR_OK)
741 case 11: /* SVCall */
743 case 12: /* Debug Monitor */
744 retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_DFSR, &except_sr);
745 if (retval != ERROR_OK)
748 case 14: /* PendSV */
750 case 15: /* SysTick */
756 retval = dap_run(swjdp);
757 if (retval == ERROR_OK)
758 LOG_TARGET_DEBUG(target, "%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32
759 ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32,
760 armv7m_exception_string(armv7m->exception_number),
761 shcsr, except_sr, cfsr, except_ar);
765 static int cortex_m_debug_entry(struct target *target)
769 struct cortex_m_common *cortex_m = target_to_cm(target);
770 struct armv7m_common *armv7m = &cortex_m->armv7m;
771 struct arm *arm = &armv7m->arm;
774 LOG_TARGET_DEBUG(target, " ");
776 /* Do this really early to minimize the window where the MASKINTS erratum
777 * can pile up pending interrupts. */
778 cortex_m_set_maskints_for_halt(target);
780 cortex_m_clear_halt(target);
782 retval = cortex_m_read_dhcsr_atomic_sticky(target);
783 if (retval != ERROR_OK)
786 retval = armv7m->examine_debug_reason(target);
787 if (retval != ERROR_OK)
790 /* examine PE security state */
791 bool secure_state = false;
792 if (armv7m->arm.arch == ARM_ARCH_V8M) {
795 retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DSCSR, &dscsr);
796 if (retval != ERROR_OK)
799 secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
802 /* Load all registers to arm.core_cache */
803 if (!cortex_m->slow_register_read) {
804 retval = cortex_m_fast_read_all_regs(target);
805 if (retval == ERROR_TIMEOUT_REACHED) {
806 cortex_m->slow_register_read = true;
807 LOG_TARGET_DEBUG(target, "Switched to slow register read");
811 if (cortex_m->slow_register_read)
812 retval = cortex_m_slow_read_all_regs(target);
814 if (retval != ERROR_OK)
818 xPSR = buf_get_u32(r->value, 0, 32);
820 /* Are we in an exception handler */
822 armv7m->exception_number = (xPSR & 0x1FF);
824 arm->core_mode = ARM_MODE_HANDLER;
825 arm->map = armv7m_msp_reg_map;
827 unsigned control = buf_get_u32(arm->core_cache
828 ->reg_list[ARMV7M_CONTROL].value, 0, 3);
830 /* is this thread privileged? */
831 arm->core_mode = control & 1
832 ? ARM_MODE_USER_THREAD
835 /* which stack is it using? */
837 arm->map = armv7m_psp_reg_map;
839 arm->map = armv7m_msp_reg_map;
841 armv7m->exception_number = 0;
844 if (armv7m->exception_number)
845 cortex_m_examine_exception_reason(target);
847 LOG_TARGET_DEBUG(target, "entered debug state in core mode: %s at PC 0x%" PRIx32
848 ", cpu in %s state, target->state: %s",
849 arm_mode_name(arm->core_mode),
850 buf_get_u32(arm->pc->value, 0, 32),
851 secure_state ? "Secure" : "Non-Secure",
852 target_state_name(target));
854 if (armv7m->post_debug_entry) {
855 retval = armv7m->post_debug_entry(target);
856 if (retval != ERROR_OK)
863 static int cortex_m_poll(struct target *target)
865 int detected_failure = ERROR_OK;
866 int retval = ERROR_OK;
867 enum target_state prev_target_state = target->state;
868 struct cortex_m_common *cortex_m = target_to_cm(target);
869 struct armv7m_common *armv7m = &cortex_m->armv7m;
871 /* Read from Debug Halting Control and Status Register */
872 retval = cortex_m_read_dhcsr_atomic_sticky(target);
873 if (retval != ERROR_OK) {
874 target->state = TARGET_UNKNOWN;
878 /* Recover from lockup. See ARMv7-M architecture spec,
879 * section B1.5.15 "Unrecoverable exception cases".
881 if (cortex_m->dcb_dhcsr & S_LOCKUP) {
882 LOG_TARGET_ERROR(target, "clearing lockup after double fault");
883 cortex_m_write_debug_halt_mask(target, C_HALT, 0);
884 target->debug_reason = DBG_REASON_DBGRQ;
886 /* We have to execute the rest (the "finally" equivalent, but
887 * still throw this exception again).
889 detected_failure = ERROR_FAIL;
891 /* refresh status bits */
892 retval = cortex_m_read_dhcsr_atomic_sticky(target);
893 if (retval != ERROR_OK)
897 if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
898 cortex_m->dcb_dhcsr_cumulated_sticky &= ~S_RESET_ST;
899 if (target->state != TARGET_RESET) {
900 target->state = TARGET_RESET;
901 LOG_TARGET_INFO(target, "external reset detected");
906 if (target->state == TARGET_RESET) {
907 /* Cannot switch context while running so endreset is
908 * called with target->state == TARGET_RESET
910 LOG_TARGET_DEBUG(target, "Exit from reset with dcb_dhcsr 0x%" PRIx32,
911 cortex_m->dcb_dhcsr);
912 retval = cortex_m_endreset_event(target);
913 if (retval != ERROR_OK) {
914 target->state = TARGET_UNKNOWN;
917 target->state = TARGET_RUNNING;
918 prev_target_state = TARGET_RUNNING;
921 if (cortex_m->dcb_dhcsr & S_HALT) {
922 target->state = TARGET_HALTED;
924 if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET)) {
925 retval = cortex_m_debug_entry(target);
926 if (retval != ERROR_OK)
929 if (arm_semihosting(target, &retval) != 0)
932 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
934 if (prev_target_state == TARGET_DEBUG_RUNNING) {
935 retval = cortex_m_debug_entry(target);
936 if (retval != ERROR_OK)
939 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
943 if (target->state == TARGET_UNKNOWN) {
944 /* Check if processor is retiring instructions or sleeping.
945 * Unlike S_RESET_ST here we test if the target *is* running now,
946 * not if it has been running (possibly in the past). Instructions are
947 * typically processed much faster than OpenOCD polls DHCSR so S_RETIRE_ST
948 * is read always 1. That's the reason not to use dcb_dhcsr_cumulated_sticky.
950 if (cortex_m->dcb_dhcsr & S_RETIRE_ST || cortex_m->dcb_dhcsr & S_SLEEP) {
951 target->state = TARGET_RUNNING;
956 /* Check that target is truly halted, since the target could be resumed externally */
957 if ((prev_target_state == TARGET_HALTED) && !(cortex_m->dcb_dhcsr & S_HALT)) {
958 /* registers are now invalid */
959 register_cache_invalidate(armv7m->arm.core_cache);
961 target->state = TARGET_RUNNING;
962 LOG_TARGET_WARNING(target, "external resume detected");
963 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
967 /* Did we detect a failure condition that we cleared? */
968 if (detected_failure != ERROR_OK)
969 retval = detected_failure;
973 static int cortex_m_halt(struct target *target)
975 LOG_TARGET_DEBUG(target, "target->state: %s", target_state_name(target));
977 if (target->state == TARGET_HALTED) {
978 LOG_TARGET_DEBUG(target, "target was already halted");
982 if (target->state == TARGET_UNKNOWN)
983 LOG_TARGET_WARNING(target, "target was in unknown state when halt was requested");
985 if (target->state == TARGET_RESET) {
986 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst()) {
987 LOG_TARGET_ERROR(target, "can't request a halt while in reset if nSRST pulls nTRST");
988 return ERROR_TARGET_FAILURE;
990 /* we came here in a reset_halt or reset_init sequence
991 * debug entry was already prepared in cortex_m3_assert_reset()
993 target->debug_reason = DBG_REASON_DBGRQ;
999 /* Write to Debug Halting Control and Status Register */
1000 cortex_m_write_debug_halt_mask(target, C_HALT, 0);
1002 /* Do this really early to minimize the window where the MASKINTS erratum
1003 * can pile up pending interrupts. */
1004 cortex_m_set_maskints_for_halt(target);
1006 target->debug_reason = DBG_REASON_DBGRQ;
1011 static int cortex_m_soft_reset_halt(struct target *target)
1013 struct cortex_m_common *cortex_m = target_to_cm(target);
1014 struct armv7m_common *armv7m = &cortex_m->armv7m;
1015 int retval, timeout = 0;
1017 /* on single cortex_m MCU soft_reset_halt should be avoided as same functionality
1018 * can be obtained by using 'reset halt' and 'cortex_m reset_config vectreset'.
1019 * As this reset only uses VC_CORERESET it would only ever reset the cortex_m
1020 * core, not the peripherals */
1021 LOG_TARGET_DEBUG(target, "soft_reset_halt is discouraged, please use 'reset halt' instead.");
1023 if (!cortex_m->vectreset_supported) {
1024 LOG_TARGET_ERROR(target, "VECTRESET is not supported on this Cortex-M core");
1029 retval = cortex_m_write_debug_halt_mask(target, 0, C_STEP | C_MASKINTS);
1030 if (retval != ERROR_OK)
1033 /* Enter debug state on reset; restore DEMCR in endreset_event() */
1034 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
1035 TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
1036 if (retval != ERROR_OK)
1039 /* Request a core-only reset */
1040 retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1041 AIRCR_VECTKEY | AIRCR_VECTRESET);
1042 if (retval != ERROR_OK)
1044 target->state = TARGET_RESET;
1046 /* registers are now invalid */
1047 register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
1049 while (timeout < 100) {
1050 retval = cortex_m_read_dhcsr_atomic_sticky(target);
1051 if (retval == ERROR_OK) {
1052 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR,
1053 &cortex_m->nvic_dfsr);
1054 if (retval != ERROR_OK)
1056 if ((cortex_m->dcb_dhcsr & S_HALT)
1057 && (cortex_m->nvic_dfsr & DFSR_VCATCH)) {
1058 LOG_TARGET_DEBUG(target, "system reset-halted, DHCSR 0x%08" PRIx32 ", DFSR 0x%08" PRIx32,
1059 cortex_m->dcb_dhcsr, cortex_m->nvic_dfsr);
1060 cortex_m_poll(target);
1061 /* FIXME restore user's vector catch config */
1064 LOG_TARGET_DEBUG(target, "waiting for system reset-halt, "
1065 "DHCSR 0x%08" PRIx32 ", %d ms",
1066 cortex_m->dcb_dhcsr, timeout);
1076 void cortex_m_enable_breakpoints(struct target *target)
1078 struct breakpoint *breakpoint = target->breakpoints;
1080 /* set any pending breakpoints */
1081 while (breakpoint) {
1082 if (!breakpoint->is_set)
1083 cortex_m_set_breakpoint(target, breakpoint);
1084 breakpoint = breakpoint->next;
1088 static int cortex_m_resume(struct target *target, int current,
1089 target_addr_t address, int handle_breakpoints, int debug_execution)
1091 struct armv7m_common *armv7m = target_to_armv7m(target);
1092 struct breakpoint *breakpoint = NULL;
1096 if (target->state != TARGET_HALTED) {
1097 LOG_TARGET_WARNING(target, "target not halted");
1098 return ERROR_TARGET_NOT_HALTED;
1101 if (!debug_execution) {
1102 target_free_all_working_areas(target);
1103 cortex_m_enable_breakpoints(target);
1104 cortex_m_enable_watchpoints(target);
1107 if (debug_execution) {
1108 r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
1110 /* Disable interrupts */
1111 /* We disable interrupts in the PRIMASK register instead of
1112 * masking with C_MASKINTS. This is probably the same issue
1113 * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
1114 * in parallel with disabled interrupts can cause local faults
1117 * This breaks non-debug (application) execution if not
1118 * called from armv7m_start_algorithm() which saves registers.
1120 buf_set_u32(r->value, 0, 1, 1);
1124 /* Make sure we are in Thumb mode, set xPSR.T bit */
1125 /* armv7m_start_algorithm() initializes entire xPSR register.
1126 * This duplicity handles the case when cortex_m_resume()
1127 * is used with the debug_execution flag directly,
1128 * not called through armv7m_start_algorithm().
1130 r = armv7m->arm.cpsr;
1131 buf_set_u32(r->value, 24, 1, 1);
1136 /* current = 1: continue on current pc, otherwise continue at <address> */
1139 buf_set_u32(r->value, 0, 32, address);
1144 /* if we halted last time due to a bkpt instruction
1145 * then we have to manually step over it, otherwise
1146 * the core will break again */
1148 if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
1149 && !debug_execution)
1150 armv7m_maybe_skip_bkpt_inst(target, NULL);
1152 resume_pc = buf_get_u32(r->value, 0, 32);
1154 armv7m_restore_context(target);
1156 /* the front-end may request us not to handle breakpoints */
1157 if (handle_breakpoints) {
1158 /* Single step past breakpoint at current address */
1159 breakpoint = breakpoint_find(target, resume_pc);
1161 LOG_TARGET_DEBUG(target, "unset breakpoint at " TARGET_ADDR_FMT " (ID: %" PRIu32 ")",
1162 breakpoint->address,
1163 breakpoint->unique_id);
1164 cortex_m_unset_breakpoint(target, breakpoint);
1165 cortex_m_single_step_core(target);
1166 cortex_m_set_breakpoint(target, breakpoint);
1171 cortex_m_set_maskints_for_run(target);
1172 cortex_m_write_debug_halt_mask(target, 0, C_HALT);
1174 target->debug_reason = DBG_REASON_NOTHALTED;
1176 /* registers are now invalid */
1177 register_cache_invalidate(armv7m->arm.core_cache);
1179 if (!debug_execution) {
1180 target->state = TARGET_RUNNING;
1181 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1182 LOG_TARGET_DEBUG(target, "target resumed at 0x%" PRIx32 "", resume_pc);
1184 target->state = TARGET_DEBUG_RUNNING;
1185 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
1186 LOG_TARGET_DEBUG(target, "target debug resumed at 0x%" PRIx32 "", resume_pc);
1192 /* int irqstepcount = 0; */
1193 static int cortex_m_step(struct target *target, int current,
1194 target_addr_t address, int handle_breakpoints)
1196 struct cortex_m_common *cortex_m = target_to_cm(target);
1197 struct armv7m_common *armv7m = &cortex_m->armv7m;
1198 struct breakpoint *breakpoint = NULL;
1199 struct reg *pc = armv7m->arm.pc;
1200 bool bkpt_inst_found = false;
1202 bool isr_timed_out = false;
1204 if (target->state != TARGET_HALTED) {
1205 LOG_TARGET_WARNING(target, "target not halted");
1206 return ERROR_TARGET_NOT_HALTED;
1209 /* current = 1: continue on current pc, otherwise continue at <address> */
1211 buf_set_u32(pc->value, 0, 32, address);
1216 uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
1218 /* the front-end may request us not to handle breakpoints */
1219 if (handle_breakpoints) {
1220 breakpoint = breakpoint_find(target, pc_value);
1222 cortex_m_unset_breakpoint(target, breakpoint);
1225 armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
1227 target->debug_reason = DBG_REASON_SINGLESTEP;
1229 armv7m_restore_context(target);
1231 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1233 /* if no bkpt instruction is found at pc then we can perform
1234 * a normal step, otherwise we have to manually step over the bkpt
1235 * instruction - as such simulate a step */
1236 if (bkpt_inst_found == false) {
1237 if (cortex_m->isrmasking_mode != CORTEX_M_ISRMASK_AUTO) {
1238 /* Automatic ISR masking mode off: Just step over the next
1239 * instruction, with interrupts on or off as appropriate. */
1240 cortex_m_set_maskints_for_step(target);
1241 cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
1243 /* Process interrupts during stepping in a way they don't interfere
1248 * Set a temporary break point at the current pc and let the core run
1249 * with interrupts enabled. Pending interrupts get served and we run
1250 * into the breakpoint again afterwards. Then we step over the next
1251 * instruction with interrupts disabled.
1253 * If the pending interrupts don't complete within time, we leave the
1254 * core running. This may happen if the interrupts trigger faster
1255 * than the core can process them or the handler doesn't return.
1257 * If no more breakpoints are available we simply do a step with
1258 * interrupts enabled.
1264 * If a break point is already set on the lower half word then a break point on
1265 * the upper half word will not break again when the core is restarted. So we
1266 * just step over the instruction with interrupts disabled.
1268 * The documentation has no information about this, it was found by observation
1269 * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 doesn't seem to
1270 * suffer from this problem.
1272 * To add some confusion: pc_value has bit 0 always set, while the breakpoint
1273 * address has it always cleared. The former is done to indicate thumb mode
1277 if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
1278 LOG_TARGET_DEBUG(target, "Stepping over next instruction with interrupts disabled");
1279 cortex_m_write_debug_halt_mask(target, C_HALT | C_MASKINTS, 0);
1280 cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
1281 /* Re-enable interrupts if appropriate */
1282 cortex_m_write_debug_halt_mask(target, C_HALT, 0);
1283 cortex_m_set_maskints_for_halt(target);
1286 /* Set a temporary break point */
1288 retval = cortex_m_set_breakpoint(target, breakpoint);
1290 enum breakpoint_type type = BKPT_HARD;
1291 if (cortex_m->fp_rev == 0 && pc_value > 0x1FFFFFFF) {
1292 /* FPB rev.1 cannot handle such addr, try BKPT instr */
1295 retval = breakpoint_add(target, pc_value, 2, type);
1298 bool tmp_bp_set = (retval == ERROR_OK);
1300 /* No more breakpoints left, just do a step */
1302 cortex_m_set_maskints_for_step(target);
1303 cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
1304 /* Re-enable interrupts if appropriate */
1305 cortex_m_write_debug_halt_mask(target, C_HALT, 0);
1306 cortex_m_set_maskints_for_halt(target);
1308 /* Start the core */
1309 LOG_TARGET_DEBUG(target, "Starting core to serve pending interrupts");
1310 int64_t t_start = timeval_ms();
1311 cortex_m_set_maskints_for_run(target);
1312 cortex_m_write_debug_halt_mask(target, 0, C_HALT | C_STEP);
1314 /* Wait for pending handlers to complete or timeout */
1316 retval = cortex_m_read_dhcsr_atomic_sticky(target);
1317 if (retval != ERROR_OK) {
1318 target->state = TARGET_UNKNOWN;
1321 isr_timed_out = ((timeval_ms() - t_start) > 500);
1322 } while (!((cortex_m->dcb_dhcsr & S_HALT) || isr_timed_out));
1324 /* only remove breakpoint if we created it */
1326 cortex_m_unset_breakpoint(target, breakpoint);
1328 /* Remove the temporary breakpoint */
1329 breakpoint_remove(target, pc_value);
1332 if (isr_timed_out) {
1333 LOG_TARGET_DEBUG(target, "Interrupt handlers didn't complete within time, "
1334 "leaving target running");
1336 /* Step over next instruction with interrupts disabled */
1337 cortex_m_set_maskints_for_step(target);
1338 cortex_m_write_debug_halt_mask(target,
1339 C_HALT | C_MASKINTS,
1341 cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
1342 /* Re-enable interrupts if appropriate */
1343 cortex_m_write_debug_halt_mask(target, C_HALT, 0);
1344 cortex_m_set_maskints_for_halt(target);
1351 retval = cortex_m_read_dhcsr_atomic_sticky(target);
1352 if (retval != ERROR_OK)
1355 /* registers are now invalid */
1356 register_cache_invalidate(armv7m->arm.core_cache);
1359 cortex_m_set_breakpoint(target, breakpoint);
1361 if (isr_timed_out) {
1362 /* Leave the core running. The user has to stop execution manually. */
1363 target->debug_reason = DBG_REASON_NOTHALTED;
1364 target->state = TARGET_RUNNING;
1368 LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1369 " nvic_icsr = 0x%" PRIx32,
1370 cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1372 retval = cortex_m_debug_entry(target);
1373 if (retval != ERROR_OK)
1375 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1377 LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1378 " nvic_icsr = 0x%" PRIx32,
1379 cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1384 static int cortex_m_assert_reset(struct target *target)
1386 struct cortex_m_common *cortex_m = target_to_cm(target);
1387 struct armv7m_common *armv7m = &cortex_m->armv7m;
1388 enum cortex_m_soft_reset_config reset_config = cortex_m->soft_reset_config;
1390 LOG_TARGET_DEBUG(target, "target->state: %s",
1391 target_state_name(target));
1393 enum reset_types jtag_reset_config = jtag_get_reset_config();
1395 if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT)) {
1396 /* allow scripts to override the reset event */
1398 target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
1399 register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
1400 target->state = TARGET_RESET;
1405 /* some cores support connecting while srst is asserted
1406 * use that mode is it has been configured */
1408 bool srst_asserted = false;
1410 if (!target_was_examined(target)) {
1411 if (jtag_reset_config & RESET_HAS_SRST) {
1412 adapter_assert_reset();
1413 if (target->reset_halt)
1414 LOG_TARGET_ERROR(target, "Target not examined, will not halt after reset!");
1417 LOG_TARGET_ERROR(target, "Target not examined, reset NOT asserted!");
1422 if ((jtag_reset_config & RESET_HAS_SRST) &&
1423 (jtag_reset_config & RESET_SRST_NO_GATING)) {
1424 adapter_assert_reset();
1425 srst_asserted = true;
1428 /* Enable debug requests */
1429 int retval = cortex_m_read_dhcsr_atomic_sticky(target);
1431 /* Store important errors instead of failing and proceed to reset assert */
1433 if (retval != ERROR_OK || !(cortex_m->dcb_dhcsr & C_DEBUGEN))
1434 retval = cortex_m_write_debug_halt_mask(target, 0, C_HALT | C_STEP | C_MASKINTS);
1436 /* If the processor is sleeping in a WFI or WFE instruction, the
1437 * C_HALT bit must be asserted to regain control */
1438 if (retval == ERROR_OK && (cortex_m->dcb_dhcsr & S_SLEEP))
1439 retval = cortex_m_write_debug_halt_mask(target, C_HALT, 0);
1441 mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
1442 /* Ignore less important errors */
1444 if (!target->reset_halt) {
1445 /* Set/Clear C_MASKINTS in a separate operation */
1446 cortex_m_set_maskints_for_run(target);
1448 /* clear any debug flags before resuming */
1449 cortex_m_clear_halt(target);
1451 /* clear C_HALT in dhcsr reg */
1452 cortex_m_write_debug_halt_mask(target, 0, C_HALT);
1454 /* Halt in debug on reset; endreset_event() restores DEMCR.
1456 * REVISIT catching BUSERR presumably helps to defend against
1457 * bad vector table entries. Should this include MMERR or
1461 retval2 = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DEMCR,
1462 TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
1463 if (retval != ERROR_OK || retval2 != ERROR_OK)
1464 LOG_TARGET_INFO(target, "AP write error, reset will not halt");
1467 if (jtag_reset_config & RESET_HAS_SRST) {
1468 /* default to asserting srst */
1470 adapter_assert_reset();
1472 /* srst is asserted, ignore AP access errors */
1475 /* Use a standard Cortex-M3 software reset mechanism.
1476 * We default to using VECTRESET as it is supported on all current cores
1477 * (except Cortex-M0, M0+ and M1 which support SYSRESETREQ only!)
1478 * This has the disadvantage of not resetting the peripherals, so a
1479 * reset-init event handler is needed to perform any peripheral resets.
1481 if (!cortex_m->vectreset_supported
1482 && reset_config == CORTEX_M_RESET_VECTRESET) {
1483 reset_config = CORTEX_M_RESET_SYSRESETREQ;
1484 LOG_TARGET_WARNING(target, "VECTRESET is not supported on this Cortex-M core, using SYSRESETREQ instead.");
1485 LOG_TARGET_WARNING(target, "Set 'cortex_m reset_config sysresetreq'.");
1488 LOG_TARGET_DEBUG(target, "Using Cortex-M %s", (reset_config == CORTEX_M_RESET_SYSRESETREQ)
1489 ? "SYSRESETREQ" : "VECTRESET");
1491 if (reset_config == CORTEX_M_RESET_VECTRESET) {
1492 LOG_TARGET_WARNING(target, "Only resetting the Cortex-M core, use a reset-init event "
1493 "handler to reset any peripherals or configure hardware srst support.");
1497 retval3 = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1498 AIRCR_VECTKEY | ((reset_config == CORTEX_M_RESET_SYSRESETREQ)
1499 ? AIRCR_SYSRESETREQ : AIRCR_VECTRESET));
1500 if (retval3 != ERROR_OK)
1501 LOG_TARGET_DEBUG(target, "Ignoring AP write error right after reset");
1503 retval3 = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1504 if (retval3 != ERROR_OK) {
1505 LOG_TARGET_ERROR(target, "DP initialisation failed");
1506 /* The error return value must not be propagated in this case.
1507 * SYSRESETREQ or VECTRESET have been possibly triggered
1508 * so reset processing should continue */
1510 /* I do not know why this is necessary, but it
1511 * fixes strange effects (step/resume cause NMI
1512 * after reset) on LM3S6918 -- Michael Schwingen
1515 mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_AIRCR, &tmp);
1519 target->state = TARGET_RESET;
1522 register_cache_invalidate(cortex_m->armv7m.arm.core_cache);
1524 /* now return stored error code if any */
1525 if (retval != ERROR_OK)
1528 if (target->reset_halt) {
1529 retval = target_halt(target);
1530 if (retval != ERROR_OK)
1537 static int cortex_m_deassert_reset(struct target *target)
1539 struct armv7m_common *armv7m = &target_to_cm(target)->armv7m;
1541 LOG_TARGET_DEBUG(target, "target->state: %s",
1542 target_state_name(target));
1544 /* deassert reset lines */
1545 adapter_deassert_reset();
1547 enum reset_types jtag_reset_config = jtag_get_reset_config();
1549 if ((jtag_reset_config & RESET_HAS_SRST) &&
1550 !(jtag_reset_config & RESET_SRST_NO_GATING) &&
1551 target_was_examined(target)) {
1553 int retval = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1554 if (retval != ERROR_OK) {
1555 LOG_TARGET_ERROR(target, "DP initialisation failed");
1563 int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
1566 unsigned int fp_num = 0;
1567 struct cortex_m_common *cortex_m = target_to_cm(target);
1568 struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1570 if (breakpoint->is_set) {
1571 LOG_TARGET_WARNING(target, "breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id);
1575 if (breakpoint->type == BKPT_HARD) {
1576 uint32_t fpcr_value;
1577 while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
1579 if (fp_num >= cortex_m->fp_num_code) {
1580 LOG_TARGET_ERROR(target, "Can not find free FPB Comparator!");
1581 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1583 breakpoint_hw_set(breakpoint, fp_num);
1584 fpcr_value = breakpoint->address | 1;
1585 if (cortex_m->fp_rev == 0) {
1586 if (breakpoint->address > 0x1FFFFFFF) {
1587 LOG_TARGET_ERROR(target, "Cortex-M Flash Patch Breakpoint rev.1 "
1588 "cannot handle HW breakpoint above address 0x1FFFFFFE");
1592 hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
1593 fpcr_value = (fpcr_value & 0x1FFFFFFC) | hilo | 1;
1594 } else if (cortex_m->fp_rev > 1) {
1595 LOG_TARGET_ERROR(target, "Unhandled Cortex-M Flash Patch Breakpoint architecture revision");
1598 comparator_list[fp_num].used = true;
1599 comparator_list[fp_num].fpcr_value = fpcr_value;
1600 target_write_u32(target, comparator_list[fp_num].fpcr_address,
1601 comparator_list[fp_num].fpcr_value);
1602 LOG_TARGET_DEBUG(target, "fpc_num %i fpcr_value 0x%" PRIx32 "",
1604 comparator_list[fp_num].fpcr_value);
1605 if (!cortex_m->fpb_enabled) {
1606 LOG_TARGET_DEBUG(target, "FPB wasn't enabled, do it now");
1607 retval = cortex_m_enable_fpb(target);
1608 if (retval != ERROR_OK) {
1609 LOG_TARGET_ERROR(target, "Failed to enable the FPB");
1613 cortex_m->fpb_enabled = true;
1615 } else if (breakpoint->type == BKPT_SOFT) {
1618 /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
1619 * semihosting; don't use that. Otherwise the BKPT
1620 * parameter is arbitrary.
1622 buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1623 retval = target_read_memory(target,
1624 breakpoint->address & 0xFFFFFFFE,
1625 breakpoint->length, 1,
1626 breakpoint->orig_instr);
1627 if (retval != ERROR_OK)
1629 retval = target_write_memory(target,
1630 breakpoint->address & 0xFFFFFFFE,
1631 breakpoint->length, 1,
1633 if (retval != ERROR_OK)
1635 breakpoint->is_set = true;
1638 LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1639 breakpoint->unique_id,
1640 (int)(breakpoint->type),
1641 breakpoint->address,
1643 (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1648 int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
1651 struct cortex_m_common *cortex_m = target_to_cm(target);
1652 struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1654 if (!breakpoint->is_set) {
1655 LOG_TARGET_WARNING(target, "breakpoint not set");
1659 LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1660 breakpoint->unique_id,
1661 (int)(breakpoint->type),
1662 breakpoint->address,
1664 (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1666 if (breakpoint->type == BKPT_HARD) {
1667 unsigned int fp_num = breakpoint->number;
1668 if (fp_num >= cortex_m->fp_num_code) {
1669 LOG_TARGET_DEBUG(target, "Invalid FP Comparator number in breakpoint");
1672 comparator_list[fp_num].used = false;
1673 comparator_list[fp_num].fpcr_value = 0;
1674 target_write_u32(target, comparator_list[fp_num].fpcr_address,
1675 comparator_list[fp_num].fpcr_value);
1677 /* restore original instruction (kept in target endianness) */
1678 retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE,
1679 breakpoint->length, 1,
1680 breakpoint->orig_instr);
1681 if (retval != ERROR_OK)
1684 breakpoint->is_set = false;
1689 int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
1691 if (breakpoint->length == 3) {
1692 LOG_TARGET_DEBUG(target, "Using a two byte breakpoint for 32bit Thumb-2 request");
1693 breakpoint->length = 2;
1696 if ((breakpoint->length != 2)) {
1697 LOG_TARGET_INFO(target, "only breakpoints of two bytes length supported");
1698 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1701 return cortex_m_set_breakpoint(target, breakpoint);
1704 int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
1706 if (!breakpoint->is_set)
1709 return cortex_m_unset_breakpoint(target, breakpoint);
1712 static int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
1714 unsigned int dwt_num = 0;
1715 struct cortex_m_common *cortex_m = target_to_cm(target);
1717 /* REVISIT Don't fully trust these "not used" records ... users
1718 * may set up breakpoints by hand, e.g. dual-address data value
1719 * watchpoint using comparator #1; comparator #0 matching cycle
1720 * count; send data trace info through ITM and TPIU; etc
1722 struct cortex_m_dwt_comparator *comparator;
1724 for (comparator = cortex_m->dwt_comparator_list;
1725 comparator->used && dwt_num < cortex_m->dwt_num_comp;
1726 comparator++, dwt_num++)
1728 if (dwt_num >= cortex_m->dwt_num_comp) {
1729 LOG_TARGET_ERROR(target, "Can not find free DWT Comparator");
1732 comparator->used = true;
1733 watchpoint_set(watchpoint, dwt_num);
1735 comparator->comp = watchpoint->address;
1736 target_write_u32(target, comparator->dwt_comparator_address + 0,
1739 if ((cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M) {
1740 uint32_t mask = 0, temp;
1742 /* watchpoint params were validated earlier */
1743 temp = watchpoint->length;
1750 comparator->mask = mask;
1751 target_write_u32(target, comparator->dwt_comparator_address + 4,
1754 switch (watchpoint->rw) {
1756 comparator->function = 5;
1759 comparator->function = 6;
1762 comparator->function = 7;
1766 uint32_t data_size = watchpoint->length >> 1;
1767 comparator->mask = (watchpoint->length >> 1) | 1;
1769 switch (watchpoint->rw) {
1771 comparator->function = 4;
1774 comparator->function = 5;
1777 comparator->function = 6;
1780 comparator->function = comparator->function | (1 << 4) |
1784 target_write_u32(target, comparator->dwt_comparator_address + 8,
1785 comparator->function);
1787 LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%d 0x%08x 0x%x 0x%05x",
1788 watchpoint->unique_id, dwt_num,
1789 (unsigned) comparator->comp,
1790 (unsigned) comparator->mask,
1791 (unsigned) comparator->function);
1795 static int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
1797 struct cortex_m_common *cortex_m = target_to_cm(target);
1798 struct cortex_m_dwt_comparator *comparator;
1800 if (!watchpoint->is_set) {
1801 LOG_TARGET_WARNING(target, "watchpoint (wpid: %d) not set",
1802 watchpoint->unique_id);
1806 unsigned int dwt_num = watchpoint->number;
1808 LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%u address: 0x%08x clear",
1809 watchpoint->unique_id, dwt_num,
1810 (unsigned) watchpoint->address);
1812 if (dwt_num >= cortex_m->dwt_num_comp) {
1813 LOG_TARGET_DEBUG(target, "Invalid DWT Comparator number in watchpoint");
1817 comparator = cortex_m->dwt_comparator_list + dwt_num;
1818 comparator->used = false;
1819 comparator->function = 0;
1820 target_write_u32(target, comparator->dwt_comparator_address + 8,
1821 comparator->function);
1823 watchpoint->is_set = false;
1828 int cortex_m_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
1830 struct cortex_m_common *cortex_m = target_to_cm(target);
1832 if (cortex_m->dwt_comp_available < 1) {
1833 LOG_TARGET_DEBUG(target, "no comparators?");
1834 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1837 /* hardware doesn't support data value masking */
1838 if (watchpoint->mask != ~(uint32_t)0) {
1839 LOG_TARGET_DEBUG(target, "watchpoint value masks not supported");
1840 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1843 /* hardware allows address masks of up to 32K */
1846 for (mask = 0; mask < 16; mask++) {
1847 if ((1u << mask) == watchpoint->length)
1851 LOG_TARGET_DEBUG(target, "unsupported watchpoint length");
1852 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1854 if (watchpoint->address & ((1 << mask) - 1)) {
1855 LOG_TARGET_DEBUG(target, "watchpoint address is unaligned");
1856 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1859 /* Caller doesn't seem to be able to describe watching for data
1860 * values of zero; that flags "no value".
1862 * REVISIT This DWT may well be able to watch for specific data
1863 * values. Requires comparator #1 to set DATAVMATCH and match
1864 * the data, and another comparator (DATAVADDR0) matching addr.
1866 if (watchpoint->value) {
1867 LOG_TARGET_DEBUG(target, "data value watchpoint not YET supported");
1868 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1871 cortex_m->dwt_comp_available--;
1872 LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
1877 int cortex_m_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
1879 struct cortex_m_common *cortex_m = target_to_cm(target);
1881 /* REVISIT why check? DWT can be updated with core running ... */
1882 if (target->state != TARGET_HALTED) {
1883 LOG_TARGET_WARNING(target, "target not halted");
1884 return ERROR_TARGET_NOT_HALTED;
1887 if (watchpoint->is_set)
1888 cortex_m_unset_watchpoint(target, watchpoint);
1890 cortex_m->dwt_comp_available++;
1891 LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
1896 int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
1898 if (target->debug_reason != DBG_REASON_WATCHPOINT)
1901 struct cortex_m_common *cortex_m = target_to_cm(target);
1903 for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
1907 unsigned int dwt_num = wp->number;
1908 struct cortex_m_dwt_comparator *comparator = cortex_m->dwt_comparator_list + dwt_num;
1910 uint32_t dwt_function;
1911 int retval = target_read_u32(target, comparator->dwt_comparator_address + 8, &dwt_function);
1912 if (retval != ERROR_OK)
1915 /* check the MATCHED bit */
1916 if (dwt_function & BIT(24)) {
1917 *hit_watchpoint = wp;
1925 void cortex_m_enable_watchpoints(struct target *target)
1927 struct watchpoint *watchpoint = target->watchpoints;
1929 /* set any pending watchpoints */
1930 while (watchpoint) {
1931 if (!watchpoint->is_set)
1932 cortex_m_set_watchpoint(target, watchpoint);
1933 watchpoint = watchpoint->next;
1937 static int cortex_m_read_memory(struct target *target, target_addr_t address,
1938 uint32_t size, uint32_t count, uint8_t *buffer)
1940 struct armv7m_common *armv7m = target_to_armv7m(target);
1942 if (armv7m->arm.arch == ARM_ARCH_V6M) {
1943 /* armv6m does not handle unaligned memory access */
1944 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1945 return ERROR_TARGET_UNALIGNED_ACCESS;
1948 return mem_ap_read_buf(armv7m->debug_ap, buffer, size, count, address);
1951 static int cortex_m_write_memory(struct target *target, target_addr_t address,
1952 uint32_t size, uint32_t count, const uint8_t *buffer)
1954 struct armv7m_common *armv7m = target_to_armv7m(target);
1956 if (armv7m->arm.arch == ARM_ARCH_V6M) {
1957 /* armv6m does not handle unaligned memory access */
1958 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1959 return ERROR_TARGET_UNALIGNED_ACCESS;
1962 return mem_ap_write_buf(armv7m->debug_ap, buffer, size, count, address);
1965 static int cortex_m_init_target(struct command_context *cmd_ctx,
1966 struct target *target)
1968 armv7m_build_reg_cache(target);
1969 arm_semihosting_init(target);
1973 void cortex_m_deinit_target(struct target *target)
1975 struct cortex_m_common *cortex_m = target_to_cm(target);
1976 struct armv7m_common *armv7m = target_to_armv7m(target);
1978 if (!armv7m->is_hla_target && armv7m->debug_ap)
1979 dap_put_ap(armv7m->debug_ap);
1981 free(cortex_m->fp_comparator_list);
1983 cortex_m_dwt_free(target);
1984 armv7m_free_reg_cache(target);
1986 free(target->private_config);
1990 int cortex_m_profiling(struct target *target, uint32_t *samples,
1991 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1993 struct timeval timeout, now;
1994 struct armv7m_common *armv7m = target_to_armv7m(target);
1998 retval = target_read_u32(target, DWT_PCSR, ®_value);
1999 if (retval != ERROR_OK) {
2000 LOG_TARGET_ERROR(target, "Error while reading PCSR");
2003 if (reg_value == 0) {
2004 LOG_TARGET_INFO(target, "PCSR sampling not supported on this processor.");
2005 return target_profiling_default(target, samples, max_num_samples, num_samples, seconds);
2008 gettimeofday(&timeout, NULL);
2009 timeval_add_time(&timeout, seconds, 0);
2011 LOG_TARGET_INFO(target, "Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
2013 /* Make sure the target is running */
2014 target_poll(target);
2015 if (target->state == TARGET_HALTED)
2016 retval = target_resume(target, 1, 0, 0, 0);
2018 if (retval != ERROR_OK) {
2019 LOG_TARGET_ERROR(target, "Error while resuming target");
2023 uint32_t sample_count = 0;
2026 if (armv7m && armv7m->debug_ap) {
2027 uint32_t read_count = max_num_samples - sample_count;
2028 if (read_count > 1024)
2031 retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
2032 (void *)&samples[sample_count],
2033 4, read_count, DWT_PCSR);
2034 sample_count += read_count;
2036 target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
2039 if (retval != ERROR_OK) {
2040 LOG_TARGET_ERROR(target, "Error while reading PCSR");
2045 gettimeofday(&now, NULL);
2046 if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
2047 LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 " samples.", sample_count);
2052 *num_samples = sample_count;
2057 /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
2058 * on r/w if the core is not running, and clear on resume or reset ... or
2059 * at least, in a post_restore_context() method.
2062 struct dwt_reg_state {
2063 struct target *target;
2065 uint8_t value[4]; /* scratch/cache */
2068 static int cortex_m_dwt_get_reg(struct reg *reg)
2070 struct dwt_reg_state *state = reg->arch_info;
2073 int retval = target_read_u32(state->target, state->addr, &tmp);
2074 if (retval != ERROR_OK)
2077 buf_set_u32(state->value, 0, 32, tmp);
2081 static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
2083 struct dwt_reg_state *state = reg->arch_info;
2085 return target_write_u32(state->target, state->addr,
2086 buf_get_u32(buf, 0, reg->size));
2095 static const struct dwt_reg dwt_base_regs[] = {
2096 { DWT_CTRL, "dwt_ctrl", 32, },
2097 /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
2098 * increments while the core is asleep.
2100 { DWT_CYCCNT, "dwt_cyccnt", 32, },
2101 /* plus some 8 bit counters, useful for profiling with TPIU */
2104 static const struct dwt_reg dwt_comp[] = {
2105 #define DWT_COMPARATOR(i) \
2106 { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
2107 { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
2108 { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
2125 #undef DWT_COMPARATOR
2128 static const struct reg_arch_type dwt_reg_type = {
2129 .get = cortex_m_dwt_get_reg,
2130 .set = cortex_m_dwt_set_reg,
2133 static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
2135 struct dwt_reg_state *state;
2137 state = calloc(1, sizeof(*state));
2140 state->addr = d->addr;
2145 r->value = state->value;
2146 r->arch_info = state;
2147 r->type = &dwt_reg_type;
2150 static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
2153 struct reg_cache *cache;
2154 struct cortex_m_dwt_comparator *comparator;
2157 target_read_u32(target, DWT_CTRL, &dwtcr);
2158 LOG_TARGET_DEBUG(target, "DWT_CTRL: 0x%" PRIx32, dwtcr);
2160 LOG_TARGET_DEBUG(target, "no DWT");
2164 target_read_u32(target, DWT_DEVARCH, &cm->dwt_devarch);
2165 LOG_TARGET_DEBUG(target, "DWT_DEVARCH: 0x%" PRIx32, cm->dwt_devarch);
2167 cm->dwt_num_comp = (dwtcr >> 28) & 0xF;
2168 cm->dwt_comp_available = cm->dwt_num_comp;
2169 cm->dwt_comparator_list = calloc(cm->dwt_num_comp,
2170 sizeof(struct cortex_m_dwt_comparator));
2171 if (!cm->dwt_comparator_list) {
2173 cm->dwt_num_comp = 0;
2174 LOG_TARGET_ERROR(target, "out of mem");
2178 cache = calloc(1, sizeof(*cache));
2181 free(cm->dwt_comparator_list);
2184 cache->name = "Cortex-M DWT registers";
2185 cache->num_regs = 2 + cm->dwt_num_comp * 3;
2186 cache->reg_list = calloc(cache->num_regs, sizeof(*cache->reg_list));
2187 if (!cache->reg_list) {
2192 for (reg = 0; reg < 2; reg++)
2193 cortex_m_dwt_addreg(target, cache->reg_list + reg,
2194 dwt_base_regs + reg);
2196 comparator = cm->dwt_comparator_list;
2197 for (unsigned int i = 0; i < cm->dwt_num_comp; i++, comparator++) {
2200 comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
2201 for (j = 0; j < 3; j++, reg++)
2202 cortex_m_dwt_addreg(target, cache->reg_list + reg,
2203 dwt_comp + 3 * i + j);
2205 /* make sure we clear any watchpoints enabled on the target */
2206 target_write_u32(target, comparator->dwt_comparator_address + 8, 0);
2209 *register_get_last_cache_p(&target->reg_cache) = cache;
2210 cm->dwt_cache = cache;
2212 LOG_TARGET_DEBUG(target, "DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
2213 dwtcr, cm->dwt_num_comp,
2214 (dwtcr & (0xf << 24)) ? " only" : "/trigger");
2216 /* REVISIT: if num_comp > 1, check whether comparator #1 can
2217 * implement single-address data value watchpoints ... so we
2218 * won't need to check it later, when asked to set one up.
2222 static void cortex_m_dwt_free(struct target *target)
2224 struct cortex_m_common *cm = target_to_cm(target);
2225 struct reg_cache *cache = cm->dwt_cache;
2227 free(cm->dwt_comparator_list);
2228 cm->dwt_comparator_list = NULL;
2229 cm->dwt_num_comp = 0;
2232 register_unlink_cache(&target->reg_cache, cache);
2234 if (cache->reg_list) {
2235 for (size_t i = 0; i < cache->num_regs; i++)
2236 free(cache->reg_list[i].arch_info);
2237 free(cache->reg_list);
2241 cm->dwt_cache = NULL;
2244 #define MVFR0 0xe000ef40
2245 #define MVFR1 0xe000ef44
2247 #define MVFR0_DEFAULT_M4 0x10110021
2248 #define MVFR1_DEFAULT_M4 0x11000011
2250 #define MVFR0_DEFAULT_M7_SP 0x10110021
2251 #define MVFR0_DEFAULT_M7_DP 0x10110221
2252 #define MVFR1_DEFAULT_M7_SP 0x11000011
2253 #define MVFR1_DEFAULT_M7_DP 0x12000011
2255 static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp,
2256 struct adiv5_ap **debug_ap)
2258 if (dap_find_get_ap(swjdp, AP_TYPE_AHB3_AP, debug_ap) == ERROR_OK)
2261 return dap_find_get_ap(swjdp, AP_TYPE_AHB5_AP, debug_ap);
2264 int cortex_m_examine(struct target *target)
2267 uint32_t cpuid, fpcr, mvfr0, mvfr1;
2268 struct cortex_m_common *cortex_m = target_to_cm(target);
2269 struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
2270 struct armv7m_common *armv7m = target_to_armv7m(target);
2272 /* hla_target shares the examine handler but does not support
2274 if (!armv7m->is_hla_target) {
2275 if (armv7m->debug_ap) {
2276 dap_put_ap(armv7m->debug_ap);
2277 armv7m->debug_ap = NULL;
2280 if (cortex_m->apsel == DP_APSEL_INVALID) {
2281 /* Search for the MEM-AP */
2282 retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
2283 if (retval != ERROR_OK) {
2284 LOG_TARGET_ERROR(target, "Could not find MEM-AP to control the core");
2288 armv7m->debug_ap = dap_get_ap(swjdp, cortex_m->apsel);
2289 if (!armv7m->debug_ap) {
2290 LOG_ERROR("Cannot get AP");
2295 armv7m->debug_ap->memaccess_tck = 8;
2297 retval = mem_ap_init(armv7m->debug_ap);
2298 if (retval != ERROR_OK)
2302 if (!target_was_examined(target)) {
2303 target_set_examined(target);
2305 /* Read from Device Identification Registers */
2306 retval = target_read_u32(target, CPUID, &cpuid);
2307 if (retval != ERROR_OK)
2310 /* Get ARCH and CPU types */
2311 const enum cortex_m_partno core_partno = (cpuid & ARM_CPUID_PARTNO_MASK) >> ARM_CPUID_PARTNO_POS;
2313 for (unsigned int n = 0; n < ARRAY_SIZE(cortex_m_parts); n++) {
2314 if (core_partno == cortex_m_parts[n].partno) {
2315 cortex_m->core_info = &cortex_m_parts[n];
2320 if (!cortex_m->core_info) {
2321 LOG_TARGET_ERROR(target, "Cortex-M PARTNO 0x%x is unrecognized", core_partno);
2325 armv7m->arm.arch = cortex_m->core_info->arch;
2327 LOG_TARGET_INFO(target, "%s r%" PRId8 "p%" PRId8 " processor detected",
2328 cortex_m->core_info->name,
2329 (uint8_t)((cpuid >> 20) & 0xf),
2330 (uint8_t)((cpuid >> 0) & 0xf));
2332 cortex_m->maskints_erratum = false;
2333 if (core_partno == CORTEX_M7_PARTNO) {
2335 rev = (cpuid >> 20) & 0xf;
2336 patch = (cpuid >> 0) & 0xf;
2337 if ((rev == 0) && (patch < 2)) {
2338 LOG_TARGET_WARNING(target, "Silicon bug: single stepping may enter pending exception handler!");
2339 cortex_m->maskints_erratum = true;
2342 LOG_TARGET_DEBUG(target, "cpuid: 0x%8.8" PRIx32 "", cpuid);
2344 if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV4) {
2345 target_read_u32(target, MVFR0, &mvfr0);
2346 target_read_u32(target, MVFR1, &mvfr1);
2348 /* test for floating point feature on Cortex-M4 */
2349 if ((mvfr0 == MVFR0_DEFAULT_M4) && (mvfr1 == MVFR1_DEFAULT_M4)) {
2350 LOG_TARGET_DEBUG(target, "%s floating point feature FPv4_SP found", cortex_m->core_info->name);
2351 armv7m->fp_feature = FPV4_SP;
2353 } else if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV5) {
2354 target_read_u32(target, MVFR0, &mvfr0);
2355 target_read_u32(target, MVFR1, &mvfr1);
2357 /* test for floating point features on Cortex-M7 */
2358 if ((mvfr0 == MVFR0_DEFAULT_M7_SP) && (mvfr1 == MVFR1_DEFAULT_M7_SP)) {
2359 LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_SP found", cortex_m->core_info->name);
2360 armv7m->fp_feature = FPV5_SP;
2361 } else if ((mvfr0 == MVFR0_DEFAULT_M7_DP) && (mvfr1 == MVFR1_DEFAULT_M7_DP)) {
2362 LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP found", cortex_m->core_info->name);
2363 armv7m->fp_feature = FPV5_DP;
2367 /* VECTRESET is supported only on ARMv7-M cores */
2368 cortex_m->vectreset_supported = armv7m->arm.arch == ARM_ARCH_V7M;
2370 /* Check for FPU, otherwise mark FPU register as non-existent */
2371 if (armv7m->fp_feature == FP_NONE)
2372 for (size_t idx = ARMV7M_FPU_FIRST_REG; idx <= ARMV7M_FPU_LAST_REG; idx++)
2373 armv7m->arm.core_cache->reg_list[idx].exist = false;
2375 if (armv7m->arm.arch != ARM_ARCH_V8M)
2376 for (size_t idx = ARMV8M_FIRST_REG; idx <= ARMV8M_LAST_REG; idx++)
2377 armv7m->arm.core_cache->reg_list[idx].exist = false;
2379 if (!armv7m->is_hla_target) {
2380 if (cortex_m->core_info->flags & CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K)
2381 /* Cortex-M3/M4 have 4096 bytes autoincrement range,
2382 * s. ARM IHI 0031C: MEM-AP 7.2.2 */
2383 armv7m->debug_ap->tar_autoincr_block = (1 << 12);
2386 retval = target_read_u32(target, DCB_DHCSR, &cortex_m->dcb_dhcsr);
2387 if (retval != ERROR_OK)
2389 cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
2391 if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
2392 /* Enable debug requests */
2393 uint32_t dhcsr = (cortex_m->dcb_dhcsr | C_DEBUGEN) & ~(C_HALT | C_STEP | C_MASKINTS);
2395 retval = target_write_u32(target, DCB_DHCSR, DBGKEY | (dhcsr & 0x0000FFFFUL));
2396 if (retval != ERROR_OK)
2398 cortex_m->dcb_dhcsr = dhcsr;
2401 /* Configure trace modules */
2402 retval = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
2403 if (retval != ERROR_OK)
2406 if (armv7m->trace_config.itm_deferred_config)
2407 armv7m_trace_itm_config(target);
2409 /* NOTE: FPB and DWT are both optional. */
2412 target_read_u32(target, FP_CTRL, &fpcr);
2413 /* bits [14:12] and [7:4] */
2414 cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
2415 cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
2416 /* Detect flash patch revision, see RM DDI 0403E.b page C1-817.
2417 Revision is zero base, fp_rev == 1 means Rev.2 ! */
2418 cortex_m->fp_rev = (fpcr >> 28) & 0xf;
2419 free(cortex_m->fp_comparator_list);
2420 cortex_m->fp_comparator_list = calloc(
2421 cortex_m->fp_num_code + cortex_m->fp_num_lit,
2422 sizeof(struct cortex_m_fp_comparator));
2423 cortex_m->fpb_enabled = fpcr & 1;
2424 for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
2425 cortex_m->fp_comparator_list[i].type =
2426 (i < cortex_m->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
2427 cortex_m->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
2429 /* make sure we clear any breakpoints enabled on the target */
2430 target_write_u32(target, cortex_m->fp_comparator_list[i].fpcr_address, 0);
2432 LOG_TARGET_DEBUG(target, "FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i",
2434 cortex_m->fp_num_code,
2435 cortex_m->fp_num_lit);
2438 cortex_m_dwt_free(target);
2439 cortex_m_dwt_setup(cortex_m, target);
2441 /* These hardware breakpoints only work for code in flash! */
2442 LOG_TARGET_INFO(target, "target has %d breakpoints, %d watchpoints",
2443 cortex_m->fp_num_code,
2444 cortex_m->dwt_num_comp);
2450 static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
2452 struct armv7m_common *armv7m = target_to_armv7m(target);
2457 retval = mem_ap_read_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
2458 if (retval != ERROR_OK)
2461 dcrdr = target_buffer_get_u16(target, buf);
2462 *ctrl = (uint8_t)dcrdr;
2463 *value = (uint8_t)(dcrdr >> 8);
2465 LOG_TARGET_DEBUG(target, "data 0x%x ctrl 0x%x", *value, *ctrl);
2467 /* write ack back to software dcc register
2468 * signify we have read data */
2469 if (dcrdr & (1 << 0)) {
2470 target_buffer_set_u16(target, buf, 0);
2471 retval = mem_ap_write_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
2472 if (retval != ERROR_OK)
2479 static int cortex_m_target_request_data(struct target *target,
2480 uint32_t size, uint8_t *buffer)
2486 for (i = 0; i < (size * 4); i++) {
2487 int retval = cortex_m_dcc_read(target, &data, &ctrl);
2488 if (retval != ERROR_OK)
2496 static int cortex_m_handle_target_request(void *priv)
2498 struct target *target = priv;
2499 if (!target_was_examined(target))
2502 if (!target->dbg_msg_enabled)
2505 if (target->state == TARGET_RUNNING) {
2510 retval = cortex_m_dcc_read(target, &data, &ctrl);
2511 if (retval != ERROR_OK)
2514 /* check if we have data */
2515 if (ctrl & (1 << 0)) {
2518 /* we assume target is quick enough */
2520 for (int i = 1; i <= 3; i++) {
2521 retval = cortex_m_dcc_read(target, &data, &ctrl);
2522 if (retval != ERROR_OK)
2524 request |= ((uint32_t)data << (i * 8));
2526 target_request(target, request);
2533 static int cortex_m_init_arch_info(struct target *target,
2534 struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
2536 struct armv7m_common *armv7m = &cortex_m->armv7m;
2538 armv7m_init_arch_info(target, armv7m);
2540 /* default reset mode is to use srst if fitted
2541 * if not it will use CORTEX_M3_RESET_VECTRESET */
2542 cortex_m->soft_reset_config = CORTEX_M_RESET_VECTRESET;
2544 armv7m->arm.dap = dap;
2546 /* register arch-specific functions */
2547 armv7m->examine_debug_reason = cortex_m_examine_debug_reason;
2549 armv7m->post_debug_entry = NULL;
2551 armv7m->pre_restore_context = NULL;
2553 armv7m->load_core_reg_u32 = cortex_m_load_core_reg_u32;
2554 armv7m->store_core_reg_u32 = cortex_m_store_core_reg_u32;
2556 target_register_timer_callback(cortex_m_handle_target_request, 1,
2557 TARGET_TIMER_TYPE_PERIODIC, target);
2562 static int cortex_m_target_create(struct target *target, Jim_Interp *interp)
2564 struct adiv5_private_config *pc;
2566 pc = (struct adiv5_private_config *)target->private_config;
2567 if (adiv5_verify_config(pc) != ERROR_OK)
2570 struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
2572 LOG_TARGET_ERROR(target, "No memory creating target");
2576 cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
2577 cortex_m->apsel = pc->ap_num;
2579 cortex_m_init_arch_info(target, cortex_m, pc->dap);
2584 /*--------------------------------------------------------------------------*/
2586 static int cortex_m_verify_pointer(struct command_invocation *cmd,
2587 struct cortex_m_common *cm)
2589 if (!is_cortex_m_with_dap_access(cm)) {
2590 command_print(cmd, "target is not a Cortex-M");
2591 return ERROR_TARGET_INVALID;
2597 * Only stuff below this line should need to verify that its target
2598 * is a Cortex-M3. Everything else should have indirected through the
2599 * cortexm3_target structure, which is only used with CM3 targets.
2602 COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
2604 struct target *target = get_current_target(CMD_CTX);
2605 struct cortex_m_common *cortex_m = target_to_cm(target);
2606 struct armv7m_common *armv7m = &cortex_m->armv7m;
2610 static const struct {
2614 { "hard_err", VC_HARDERR, },
2615 { "int_err", VC_INTERR, },
2616 { "bus_err", VC_BUSERR, },
2617 { "state_err", VC_STATERR, },
2618 { "chk_err", VC_CHKERR, },
2619 { "nocp_err", VC_NOCPERR, },
2620 { "mm_err", VC_MMERR, },
2621 { "reset", VC_CORERESET, },
2624 retval = cortex_m_verify_pointer(CMD, cortex_m);
2625 if (retval != ERROR_OK)
2628 if (!target_was_examined(target)) {
2629 LOG_TARGET_ERROR(target, "Target not examined yet");
2633 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
2634 if (retval != ERROR_OK)
2640 if (CMD_ARGC == 1) {
2641 if (strcmp(CMD_ARGV[0], "all") == 0) {
2642 catch = VC_HARDERR | VC_INTERR | VC_BUSERR
2643 | VC_STATERR | VC_CHKERR | VC_NOCPERR
2644 | VC_MMERR | VC_CORERESET;
2646 } else if (strcmp(CMD_ARGV[0], "none") == 0)
2649 while (CMD_ARGC-- > 0) {
2651 for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
2652 if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
2654 catch |= vec_ids[i].mask;
2657 if (i == ARRAY_SIZE(vec_ids)) {
2658 LOG_TARGET_ERROR(target, "No CM3 vector '%s'", CMD_ARGV[CMD_ARGC]);
2659 return ERROR_COMMAND_SYNTAX_ERROR;
2663 /* For now, armv7m->demcr only stores vector catch flags. */
2664 armv7m->demcr = catch;
2669 /* write, but don't assume it stuck (why not??) */
2670 retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, demcr);
2671 if (retval != ERROR_OK)
2673 retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
2674 if (retval != ERROR_OK)
2677 /* FIXME be sure to clear DEMCR on clean server shutdown.
2678 * Otherwise the vector catch hardware could fire when there's
2679 * no debugger hooked up, causing much confusion...
2683 for (unsigned i = 0; i < ARRAY_SIZE(vec_ids); i++) {
2684 command_print(CMD, "%9s: %s", vec_ids[i].name,
2685 (demcr & vec_ids[i].mask) ? "catch" : "ignore");
2691 COMMAND_HANDLER(handle_cortex_m_mask_interrupts_command)
2693 struct target *target = get_current_target(CMD_CTX);
2694 struct cortex_m_common *cortex_m = target_to_cm(target);
2697 static const struct jim_nvp nvp_maskisr_modes[] = {
2698 { .name = "auto", .value = CORTEX_M_ISRMASK_AUTO },
2699 { .name = "off", .value = CORTEX_M_ISRMASK_OFF },
2700 { .name = "on", .value = CORTEX_M_ISRMASK_ON },
2701 { .name = "steponly", .value = CORTEX_M_ISRMASK_STEPONLY },
2702 { .name = NULL, .value = -1 },
2704 const struct jim_nvp *n;
2707 retval = cortex_m_verify_pointer(CMD, cortex_m);
2708 if (retval != ERROR_OK)
2711 if (target->state != TARGET_HALTED) {
2712 command_print(CMD, "target must be stopped for \"%s\" command", CMD_NAME);
2717 n = jim_nvp_name2value_simple(nvp_maskisr_modes, CMD_ARGV[0]);
2719 return ERROR_COMMAND_SYNTAX_ERROR;
2720 cortex_m->isrmasking_mode = n->value;
2721 cortex_m_set_maskints_for_halt(target);
2724 n = jim_nvp_value2name_simple(nvp_maskisr_modes, cortex_m->isrmasking_mode);
2725 command_print(CMD, "cortex_m interrupt mask %s", n->name);
2730 COMMAND_HANDLER(handle_cortex_m_reset_config_command)
2732 struct target *target = get_current_target(CMD_CTX);
2733 struct cortex_m_common *cortex_m = target_to_cm(target);
2737 retval = cortex_m_verify_pointer(CMD, cortex_m);
2738 if (retval != ERROR_OK)
2742 if (strcmp(*CMD_ARGV, "sysresetreq") == 0)
2743 cortex_m->soft_reset_config = CORTEX_M_RESET_SYSRESETREQ;
2745 else if (strcmp(*CMD_ARGV, "vectreset") == 0) {
2746 if (target_was_examined(target)
2747 && !cortex_m->vectreset_supported)
2748 LOG_TARGET_WARNING(target, "VECTRESET is not supported on your Cortex-M core!");
2750 cortex_m->soft_reset_config = CORTEX_M_RESET_VECTRESET;
2753 return ERROR_COMMAND_SYNTAX_ERROR;
2756 switch (cortex_m->soft_reset_config) {
2757 case CORTEX_M_RESET_SYSRESETREQ:
2758 reset_config = "sysresetreq";
2761 case CORTEX_M_RESET_VECTRESET:
2762 reset_config = "vectreset";
2766 reset_config = "unknown";
2770 command_print(CMD, "cortex_m reset_config %s", reset_config);
2775 static const struct command_registration cortex_m_exec_command_handlers[] = {
2778 .handler = handle_cortex_m_mask_interrupts_command,
2779 .mode = COMMAND_EXEC,
2780 .help = "mask cortex_m interrupts",
2781 .usage = "['auto'|'on'|'off'|'steponly']",
2784 .name = "vector_catch",
2785 .handler = handle_cortex_m_vector_catch_command,
2786 .mode = COMMAND_EXEC,
2787 .help = "configure hardware vectors to trigger debug entry",
2788 .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
2791 .name = "reset_config",
2792 .handler = handle_cortex_m_reset_config_command,
2793 .mode = COMMAND_ANY,
2794 .help = "configure software reset handling",
2795 .usage = "['sysresetreq'|'vectreset']",
2797 COMMAND_REGISTRATION_DONE
2799 static const struct command_registration cortex_m_command_handlers[] = {
2801 .chain = armv7m_command_handlers,
2804 .chain = armv7m_trace_command_handlers,
2806 /* START_DEPRECATED_TPIU */
2808 .chain = arm_tpiu_deprecated_command_handlers,
2810 /* END_DEPRECATED_TPIU */
2813 .mode = COMMAND_EXEC,
2814 .help = "Cortex-M command group",
2816 .chain = cortex_m_exec_command_handlers,
2819 .chain = rtt_target_command_handlers,
2821 COMMAND_REGISTRATION_DONE
2824 struct target_type cortexm_target = {
2827 .poll = cortex_m_poll,
2828 .arch_state = armv7m_arch_state,
2830 .target_request_data = cortex_m_target_request_data,
2832 .halt = cortex_m_halt,
2833 .resume = cortex_m_resume,
2834 .step = cortex_m_step,
2836 .assert_reset = cortex_m_assert_reset,
2837 .deassert_reset = cortex_m_deassert_reset,
2838 .soft_reset_halt = cortex_m_soft_reset_halt,
2840 .get_gdb_arch = arm_get_gdb_arch,
2841 .get_gdb_reg_list = armv7m_get_gdb_reg_list,
2843 .read_memory = cortex_m_read_memory,
2844 .write_memory = cortex_m_write_memory,
2845 .checksum_memory = armv7m_checksum_memory,
2846 .blank_check_memory = armv7m_blank_check_memory,
2848 .run_algorithm = armv7m_run_algorithm,
2849 .start_algorithm = armv7m_start_algorithm,
2850 .wait_algorithm = armv7m_wait_algorithm,
2852 .add_breakpoint = cortex_m_add_breakpoint,
2853 .remove_breakpoint = cortex_m_remove_breakpoint,
2854 .add_watchpoint = cortex_m_add_watchpoint,
2855 .remove_watchpoint = cortex_m_remove_watchpoint,
2856 .hit_watchpoint = cortex_m_hit_watchpoint,
2858 .commands = cortex_m_command_handlers,
2859 .target_create = cortex_m_target_create,
2860 .target_jim_configure = adiv5_jim_configure,
2861 .init_target = cortex_m_init_target,
2862 .examine = cortex_m_examine,
2863 .deinit_target = cortex_m_deinit_target,
2865 .profiling = cortex_m_profiling,