OpenOCD
cortex_m.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2006 by Magnus Lundin *
8  * lundin@mlu.mine.nu *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  * *
13  * *
14  * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
15  * *
16  ***************************************************************************/
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "jtag/interface.h"
22 #include "breakpoints.h"
23 #include "cortex_m.h"
24 #include "target_request.h"
25 #include "target_type.h"
26 #include "arm_adi_v5.h"
27 #include "arm_disassembler.h"
28 #include "register.h"
29 #include "arm_opcodes.h"
30 #include "arm_semihosting.h"
31 #include <helper/time_support.h>
32 #include <rtt/rtt.h>
33 
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.)
39  *
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
42  * any longer.
43  */
44 
45 /* Timeout for register r/w */
46 #define DHCSR_S_REGRDY_TIMEOUT (500)
47 
48 /* Supported Cortex-M Cores */
49 static const struct cortex_m_part_info cortex_m_parts[] = {
50  {
52  .name = "Cortex-M0",
53  .arch = ARM_ARCH_V6M,
54  },
55  {
56  .partno = CORTEX_M0P_PARTNO,
57  .name = "Cortex-M0+",
58  .arch = ARM_ARCH_V6M,
59  },
60  {
61  .partno = CORTEX_M1_PARTNO,
62  .name = "Cortex-M1",
63  .arch = ARM_ARCH_V6M,
64  },
65  {
66  .partno = CORTEX_M3_PARTNO,
67  .name = "Cortex-M3",
68  .arch = ARM_ARCH_V7M,
70  },
71  {
72  .partno = CORTEX_M4_PARTNO,
73  .name = "Cortex-M4",
74  .arch = ARM_ARCH_V7M,
76  },
77  {
78  .partno = CORTEX_M7_PARTNO,
79  .name = "Cortex-M7",
80  .arch = ARM_ARCH_V7M,
81  .flags = CORTEX_M_F_HAS_FPV5,
82  },
83  {
84  .partno = CORTEX_M23_PARTNO,
85  .name = "Cortex-M23",
86  .arch = ARM_ARCH_V8M,
87  },
88  {
89  .partno = CORTEX_M33_PARTNO,
90  .name = "Cortex-M33",
91  .arch = ARM_ARCH_V8M,
92  .flags = CORTEX_M_F_HAS_FPV5,
93  },
94  {
95  .partno = CORTEX_M35P_PARTNO,
96  .name = "Cortex-M35P",
97  .arch = ARM_ARCH_V8M,
98  .flags = CORTEX_M_F_HAS_FPV5,
99  },
100  {
101  .partno = CORTEX_M55_PARTNO,
102  .name = "Cortex-M55",
103  .arch = ARM_ARCH_V8M,
104  .flags = CORTEX_M_F_HAS_FPV5,
105  },
106  {
107  .partno = STAR_MC1_PARTNO,
108  .name = "STAR-MC1",
109  .arch = ARM_ARCH_V8M,
110  .flags = CORTEX_M_F_HAS_FPV5,
111  },
112 };
113 
114 /* forward declarations */
115 static int cortex_m_store_core_reg_u32(struct target *target,
116  uint32_t num, uint32_t value);
117 static void cortex_m_dwt_free(struct target *target);
118 
123 static inline void cortex_m_cumulate_dhcsr_sticky(struct cortex_m_common *cortex_m,
124  uint32_t dhcsr)
125 {
126  cortex_m->dcb_dhcsr_cumulated_sticky |= dhcsr;
127 }
128 
133 {
134  struct cortex_m_common *cortex_m = target_to_cm(target);
135  struct armv7m_common *armv7m = target_to_armv7m(target);
136 
137  int retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
138  &cortex_m->dcb_dhcsr);
139  if (retval != ERROR_OK)
140  return retval;
141 
142  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
143  return ERROR_OK;
144 }
145 
147  uint32_t regsel, uint32_t *value)
148 {
149  struct cortex_m_common *cortex_m = target_to_cm(target);
150  struct armv7m_common *armv7m = target_to_armv7m(target);
151  int retval;
152  uint32_t dcrdr, tmp_value;
153  int64_t then;
154 
155  /* because the DCB_DCRDR is used for the emulated dcc channel
156  * we have to save/restore the DCB_DCRDR when used */
157  if (target->dbg_msg_enabled) {
158  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
159  if (retval != ERROR_OK)
160  return retval;
161  }
162 
163  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
164  if (retval != ERROR_OK)
165  return retval;
166 
167  /* check if value from register is ready and pre-read it */
168  then = timeval_ms();
169  while (1) {
170  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR,
171  &cortex_m->dcb_dhcsr);
172  if (retval != ERROR_OK)
173  return retval;
174  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR,
175  &tmp_value);
176  if (retval != ERROR_OK)
177  return retval;
178  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
179  if (cortex_m->dcb_dhcsr & S_REGRDY)
180  break;
181  cortex_m->slow_register_read = true; /* Polling (still) needed. */
182  if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
183  LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
184  return ERROR_TIMEOUT_REACHED;
185  }
186  keep_alive();
187  }
188 
189  *value = tmp_value;
190 
191  if (target->dbg_msg_enabled) {
192  /* restore DCB_DCRDR - this needs to be in a separate
193  * transaction otherwise the emulated DCC channel breaks */
194  if (retval == ERROR_OK)
195  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
196  }
197 
198  return retval;
199 }
200 
202 {
203  struct cortex_m_common *cortex_m = target_to_cm(target);
204  struct armv7m_common *armv7m = target_to_armv7m(target);
205  const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
206 
207  /* Opportunistically restore fast read, it'll revert to slow
208  * if any register needed polling in cortex_m_load_core_reg_u32(). */
209  cortex_m->slow_register_read = false;
210 
211  for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) {
212  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
213  if (r->exist) {
214  int retval = armv7m->arm.read_core_reg(target, r, reg_id, ARM_MODE_ANY);
215  if (retval != ERROR_OK)
216  return retval;
217  }
218  }
219 
220  if (!cortex_m->slow_register_read)
221  LOG_TARGET_DEBUG(target, "Switching back to fast register reads");
222 
223  return ERROR_OK;
224 }
225 
226 static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel,
227  uint32_t *reg_value, uint32_t *dhcsr)
228 {
229  struct armv7m_common *armv7m = target_to_armv7m(target);
230  int retval;
231 
232  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
233  if (retval != ERROR_OK)
234  return retval;
235 
236  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR, dhcsr);
237  if (retval != ERROR_OK)
238  return retval;
239 
240  return mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, reg_value);
241 }
242 
244 {
245  struct cortex_m_common *cortex_m = target_to_cm(target);
246  struct armv7m_common *armv7m = target_to_armv7m(target);
247  int retval;
248  uint32_t dcrdr;
249 
250  /* because the DCB_DCRDR is used for the emulated dcc channel
251  * we have to save/restore the DCB_DCRDR when used */
252  if (target->dbg_msg_enabled) {
253  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
254  if (retval != ERROR_OK)
255  return retval;
256  }
257 
258  const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
259  const unsigned int n_r32 = ARMV7M_LAST_REG - ARMV7M_CORE_FIRST_REG + 1
261  /* we need one 32-bit word for each register except FP D0..D15, which
262  * need two words */
263  uint32_t r_vals[n_r32];
264  uint32_t dhcsr[n_r32];
265 
266  unsigned int wi = 0; /* write index to r_vals and dhcsr arrays */
267  unsigned int reg_id; /* register index in the reg_list, ARMV7M_R0... */
268  for (reg_id = 0; reg_id < num_regs; reg_id++) {
269  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
270  if (!r->exist)
271  continue; /* skip non existent registers */
272 
273  if (r->size <= 8) {
274  /* Any 8-bit or shorter register is unpacked from a 32-bit
275  * container register. Skip it now. */
276  continue;
277  }
278 
279  uint32_t regsel = armv7m_map_id_to_regsel(reg_id);
280  retval = cortex_m_queue_reg_read(target, regsel, &r_vals[wi],
281  &dhcsr[wi]);
282  if (retval != ERROR_OK)
283  return retval;
284  wi++;
285 
286  assert(r->size == 32 || r->size == 64);
287  if (r->size == 32)
288  continue; /* done with 32-bit register */
289 
290  assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
291  /* the odd part of FP register (S1, S3...) */
292  retval = cortex_m_queue_reg_read(target, regsel + 1, &r_vals[wi],
293  &dhcsr[wi]);
294  if (retval != ERROR_OK)
295  return retval;
296  wi++;
297  }
298 
299  assert(wi <= n_r32);
300 
301  retval = dap_run(armv7m->debug_ap->dap);
302  if (retval != ERROR_OK)
303  return retval;
304 
305  if (target->dbg_msg_enabled) {
306  /* restore DCB_DCRDR - this needs to be in a separate
307  * transaction otherwise the emulated DCC channel breaks */
308  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
309  if (retval != ERROR_OK)
310  return retval;
311  }
312 
313  bool not_ready = false;
314  for (unsigned int i = 0; i < wi; i++) {
315  if ((dhcsr[i] & S_REGRDY) == 0) {
316  not_ready = true;
317  LOG_TARGET_DEBUG(target, "Register %u was not ready during fast read", i);
318  }
319  cortex_m_cumulate_dhcsr_sticky(cortex_m, dhcsr[i]);
320  }
321 
322  if (not_ready) {
323  /* Any register was not ready,
324  * fall back to slow read with S_REGRDY polling */
325  return ERROR_TIMEOUT_REACHED;
326  }
327 
328  LOG_TARGET_DEBUG(target, "read %u 32-bit registers", wi);
329 
330  unsigned int ri = 0; /* read index from r_vals array */
331  for (reg_id = 0; reg_id < num_regs; reg_id++) {
332  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
333  if (!r->exist)
334  continue; /* skip non existent registers */
335 
336  r->dirty = false;
337 
338  unsigned int reg32_id;
339  uint32_t offset;
340  if (armv7m_map_reg_packing(reg_id, &reg32_id, &offset)) {
341  /* Unpack a partial register from 32-bit container register */
342  struct reg *r32 = &armv7m->arm.core_cache->reg_list[reg32_id];
343 
344  /* The container register ought to precede all regs unpacked
345  * from it in the reg_list. So the value should be ready
346  * to unpack */
347  assert(r32->valid);
348  buf_cpy(r32->value + offset, r->value, r->size);
349 
350  } else {
351  assert(r->size == 32 || r->size == 64);
352  buf_set_u32(r->value, 0, 32, r_vals[ri++]);
353 
354  if (r->size == 64) {
355  assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
356  /* the odd part of FP register (S1, S3...) */
357  buf_set_u32(r->value + 4, 0, 32, r_vals[ri++]);
358  }
359  }
360  r->valid = true;
361  }
362  assert(ri == wi);
363 
364  return retval;
365 }
366 
368  uint32_t regsel, uint32_t value)
369 {
370  struct cortex_m_common *cortex_m = target_to_cm(target);
371  struct armv7m_common *armv7m = target_to_armv7m(target);
372  int retval;
373  uint32_t dcrdr;
374  int64_t then;
375 
376  /* because the DCB_DCRDR is used for the emulated dcc channel
377  * we have to save/restore the DCB_DCRDR when used */
378  if (target->dbg_msg_enabled) {
379  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
380  if (retval != ERROR_OK)
381  return retval;
382  }
383 
384  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, value);
385  if (retval != ERROR_OK)
386  return retval;
387 
388  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
389  if (retval != ERROR_OK)
390  return retval;
391 
392  /* check if value is written into register */
393  then = timeval_ms();
394  while (1) {
396  if (retval != ERROR_OK)
397  return retval;
398  if (cortex_m->dcb_dhcsr & S_REGRDY)
399  break;
400  if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
401  LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
402  return ERROR_TIMEOUT_REACHED;
403  }
404  keep_alive();
405  }
406 
407  if (target->dbg_msg_enabled) {
408  /* restore DCB_DCRDR - this needs to be in a separate
409  * transaction otherwise the emulated DCC channel breaks */
410  if (retval == ERROR_OK)
411  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
412  }
413 
414  return retval;
415 }
416 
418  uint32_t mask_on, uint32_t mask_off)
419 {
420  struct cortex_m_common *cortex_m = target_to_cm(target);
421  struct armv7m_common *armv7m = &cortex_m->armv7m;
422 
423  /* mask off status bits */
424  cortex_m->dcb_dhcsr &= ~((0xFFFFul << 16) | mask_off);
425  /* create new register mask */
426  cortex_m->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
427 
428  return mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR, cortex_m->dcb_dhcsr);
429 }
430 
431 static int cortex_m_set_maskints(struct target *target, bool mask)
432 {
433  struct cortex_m_common *cortex_m = target_to_cm(target);
434  if (!!(cortex_m->dcb_dhcsr & C_MASKINTS) != mask)
436  else
437  return ERROR_OK;
438 }
439 
441 {
442  struct cortex_m_common *cortex_m = target_to_cm(target);
443  switch (cortex_m->isrmasking_mode) {
445  /* interrupts taken at resume, whether for step or run -> no mask */
446  return cortex_m_set_maskints(target, false);
447 
449  /* interrupts never masked */
450  return cortex_m_set_maskints(target, false);
451 
452  case CORTEX_M_ISRMASK_ON:
453  /* interrupts always masked */
454  return cortex_m_set_maskints(target, true);
455 
457  /* interrupts masked for single step only -> mask now if MASKINTS
458  * erratum, otherwise only mask before stepping */
459  return cortex_m_set_maskints(target, cortex_m->maskints_erratum);
460  }
461  return ERROR_OK;
462 }
463 
465 {
466  switch (target_to_cm(target)->isrmasking_mode) {
468  /* interrupts taken at resume, whether for step or run -> no mask */
469  return cortex_m_set_maskints(target, false);
470 
472  /* interrupts never masked */
473  return cortex_m_set_maskints(target, false);
474 
475  case CORTEX_M_ISRMASK_ON:
476  /* interrupts always masked */
477  return cortex_m_set_maskints(target, true);
478 
480  /* interrupts masked for single step only -> no mask */
481  return cortex_m_set_maskints(target, false);
482  }
483  return ERROR_OK;
484 }
485 
487 {
488  switch (target_to_cm(target)->isrmasking_mode) {
490  /* the auto-interrupt should already be done -> mask */
491  return cortex_m_set_maskints(target, true);
492 
494  /* interrupts never masked */
495  return cortex_m_set_maskints(target, false);
496 
497  case CORTEX_M_ISRMASK_ON:
498  /* interrupts always masked */
499  return cortex_m_set_maskints(target, true);
500 
502  /* interrupts masked for single step only -> mask */
503  return cortex_m_set_maskints(target, true);
504  }
505  return ERROR_OK;
506 }
507 
508 static int cortex_m_clear_halt(struct target *target)
509 {
510  struct cortex_m_common *cortex_m = target_to_cm(target);
511  struct armv7m_common *armv7m = &cortex_m->armv7m;
512  int retval;
513 
514  /* clear step if any */
516 
517  /* Read Debug Fault Status Register */
518  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR, &cortex_m->nvic_dfsr);
519  if (retval != ERROR_OK)
520  return retval;
521 
522  /* Clear Debug Fault Status */
523  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_DFSR, cortex_m->nvic_dfsr);
524  if (retval != ERROR_OK)
525  return retval;
526  LOG_TARGET_DEBUG(target, "NVIC_DFSR 0x%" PRIx32 "", cortex_m->nvic_dfsr);
527 
528  return ERROR_OK;
529 }
530 
532 {
533  struct cortex_m_common *cortex_m = target_to_cm(target);
534  int retval;
535 
536  /* Mask interrupts before clearing halt, if not done already. This avoids
537  * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
538  * HALT can put the core into an unknown state.
539  */
540  if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) {
542  if (retval != ERROR_OK)
543  return retval;
544  }
546  if (retval != ERROR_OK)
547  return retval;
548  LOG_TARGET_DEBUG(target, "single step");
549 
550  /* restore dhcsr reg */
552 
553  return ERROR_OK;
554 }
555 
556 static int cortex_m_enable_fpb(struct target *target)
557 {
558  int retval = target_write_u32(target, FP_CTRL, 3);
559  if (retval != ERROR_OK)
560  return retval;
561 
562  /* check the fpb is actually enabled */
563  uint32_t fpctrl;
564  retval = target_read_u32(target, FP_CTRL, &fpctrl);
565  if (retval != ERROR_OK)
566  return retval;
567 
568  if (fpctrl & 1)
569  return ERROR_OK;
570 
571  return ERROR_FAIL;
572 }
573 
575 {
576  int retval;
577  uint32_t dcb_demcr;
578  struct cortex_m_common *cortex_m = target_to_cm(target);
579  struct armv7m_common *armv7m = &cortex_m->armv7m;
580  struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
581  struct cortex_m_fp_comparator *fp_list = cortex_m->fp_comparator_list;
582  struct cortex_m_dwt_comparator *dwt_list = cortex_m->dwt_comparator_list;
583 
584  /* REVISIT The four debug monitor bits are currently ignored... */
585  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &dcb_demcr);
586  if (retval != ERROR_OK)
587  return retval;
588  LOG_TARGET_DEBUG(target, "DCB_DEMCR = 0x%8.8" PRIx32 "", dcb_demcr);
589 
590  /* this register is used for emulated dcc channel */
591  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
592  if (retval != ERROR_OK)
593  return retval;
594 
596  if (retval != ERROR_OK)
597  return retval;
598 
599  if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
600  /* Enable debug requests */
602  if (retval != ERROR_OK)
603  return retval;
604  }
605 
606  /* Restore proper interrupt masking setting for running CPU. */
608 
609  /* Enable features controlled by ITM and DWT blocks, and catch only
610  * the vectors we were told to pay attention to.
611  *
612  * Target firmware is responsible for all fault handling policy
613  * choices *EXCEPT* explicitly scripted overrides like "vector_catch"
614  * or manual updates to the NVIC SHCSR and CCR registers.
615  */
616  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, TRCENA | armv7m->demcr);
617  if (retval != ERROR_OK)
618  return retval;
619 
620  /* Paranoia: evidently some (early?) chips don't preserve all the
621  * debug state (including FPB, DWT, etc) across reset...
622  */
623 
624  /* Enable FPB */
625  retval = cortex_m_enable_fpb(target);
626  if (retval != ERROR_OK) {
627  LOG_TARGET_ERROR(target, "Failed to enable the FPB");
628  return retval;
629  }
630 
631  cortex_m->fpb_enabled = true;
632 
633  /* Restore FPB registers */
634  for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
635  retval = target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
636  if (retval != ERROR_OK)
637  return retval;
638  }
639 
640  /* Restore DWT registers */
641  for (unsigned int i = 0; i < cortex_m->dwt_num_comp; i++) {
642  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
643  dwt_list[i].comp);
644  if (retval != ERROR_OK)
645  return retval;
646  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
647  dwt_list[i].mask);
648  if (retval != ERROR_OK)
649  return retval;
650  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
651  dwt_list[i].function);
652  if (retval != ERROR_OK)
653  return retval;
654  }
655  retval = dap_run(swjdp);
656  if (retval != ERROR_OK)
657  return retval;
658 
660 
661  /* TODO: invalidate also working areas (needed in the case of detected reset).
662  * Doing so will require flash drivers to test if working area
663  * is still valid in all target algo calling loops.
664  */
665 
666  /* make sure we have latest dhcsr flags */
668  if (retval != ERROR_OK)
669  return retval;
670 
671  return retval;
672 }
673 
675 {
676  struct cortex_m_common *cortex_m = target_to_cm(target);
677 
678  /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason
679  * only check the debug reason if we don't know it already */
680 
683  if (cortex_m->nvic_dfsr & DFSR_BKPT) {
685  if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
687  } else if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
689  else if (cortex_m->nvic_dfsr & DFSR_VCATCH)
691  else if (cortex_m->nvic_dfsr & DFSR_EXTERNAL)
693  else /* HALTED */
695  }
696 
697  return ERROR_OK;
698 }
699 
701 {
702  uint32_t shcsr = 0, except_sr = 0, cfsr = -1, except_ar = -1;
703  struct armv7m_common *armv7m = target_to_armv7m(target);
704  struct adiv5_dap *swjdp = armv7m->arm.dap;
705  int retval;
706 
707  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SHCSR, &shcsr);
708  if (retval != ERROR_OK)
709  return retval;
710  switch (armv7m->exception_number) {
711  case 2: /* NMI */
712  break;
713  case 3: /* Hard Fault */
714  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_HFSR, &except_sr);
715  if (retval != ERROR_OK)
716  return retval;
717  if (except_sr & 0x40000000) {
718  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &cfsr);
719  if (retval != ERROR_OK)
720  return retval;
721  }
722  break;
723  case 4: /* Memory Management */
724  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
725  if (retval != ERROR_OK)
726  return retval;
727  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_MMFAR, &except_ar);
728  if (retval != ERROR_OK)
729  return retval;
730  break;
731  case 5: /* Bus Fault */
732  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
733  if (retval != ERROR_OK)
734  return retval;
735  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_BFAR, &except_ar);
736  if (retval != ERROR_OK)
737  return retval;
738  break;
739  case 6: /* Usage Fault */
740  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
741  if (retval != ERROR_OK)
742  return retval;
743  break;
744  case 7: /* Secure Fault */
745  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFSR, &except_sr);
746  if (retval != ERROR_OK)
747  return retval;
748  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFAR, &except_ar);
749  if (retval != ERROR_OK)
750  return retval;
751  break;
752  case 11: /* SVCall */
753  break;
754  case 12: /* Debug Monitor */
755  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_DFSR, &except_sr);
756  if (retval != ERROR_OK)
757  return retval;
758  break;
759  case 14: /* PendSV */
760  break;
761  case 15: /* SysTick */
762  break;
763  default:
764  except_sr = 0;
765  break;
766  }
767  retval = dap_run(swjdp);
768  if (retval == ERROR_OK)
769  LOG_TARGET_DEBUG(target, "%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32
770  ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32,
772  shcsr, except_sr, cfsr, except_ar);
773  return retval;
774 }
775 
776 static int cortex_m_debug_entry(struct target *target)
777 {
778  uint32_t xpsr;
779  int retval;
780  struct cortex_m_common *cortex_m = target_to_cm(target);
781  struct armv7m_common *armv7m = &cortex_m->armv7m;
782  struct arm *arm = &armv7m->arm;
783  struct reg *r;
784 
785  LOG_TARGET_DEBUG(target, " ");
786 
787  /* Do this really early to minimize the window where the MASKINTS erratum
788  * can pile up pending interrupts. */
790 
792 
794  if (retval != ERROR_OK)
795  return retval;
796 
797  retval = armv7m->examine_debug_reason(target);
798  if (retval != ERROR_OK)
799  return retval;
800 
801  /* examine PE security state */
802  bool secure_state = false;
803  if (armv7m->arm.arch == ARM_ARCH_V8M) {
804  uint32_t dscsr;
805 
806  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DSCSR, &dscsr);
807  if (retval != ERROR_OK)
808  return retval;
809 
810  secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
811  }
812 
813  /* Load all registers to arm.core_cache */
814  if (!cortex_m->slow_register_read) {
816  if (retval == ERROR_TIMEOUT_REACHED) {
817  cortex_m->slow_register_read = true;
818  LOG_TARGET_DEBUG(target, "Switched to slow register read");
819  }
820  }
821 
822  if (cortex_m->slow_register_read)
824 
825  if (retval != ERROR_OK)
826  return retval;
827 
828  r = arm->cpsr;
829  xpsr = buf_get_u32(r->value, 0, 32);
830 
831  /* Are we in an exception handler */
832  if (xpsr & 0x1FF) {
833  armv7m->exception_number = (xpsr & 0x1FF);
834 
837  } else {
838  unsigned control = buf_get_u32(arm->core_cache
839  ->reg_list[ARMV7M_CONTROL].value, 0, 3);
840 
841  /* is this thread privileged? */
842  arm->core_mode = control & 1
844  : ARM_MODE_THREAD;
845 
846  /* which stack is it using? */
847  if (control & 2)
849  else
851 
852  armv7m->exception_number = 0;
853  }
854 
855  if (armv7m->exception_number)
857 
858  LOG_TARGET_DEBUG(target, "entered debug state in core mode: %s at PC 0x%" PRIx32
859  ", cpu in %s state, target->state: %s",
861  buf_get_u32(arm->pc->value, 0, 32),
862  secure_state ? "Secure" : "Non-Secure",
864 
865  if (armv7m->post_debug_entry) {
866  retval = armv7m->post_debug_entry(target);
867  if (retval != ERROR_OK)
868  return retval;
869  }
870 
871  return ERROR_OK;
872 }
873 
874 static int cortex_m_poll(struct target *target)
875 {
876  int detected_failure = ERROR_OK;
877  int retval = ERROR_OK;
878  enum target_state prev_target_state = target->state;
879  struct cortex_m_common *cortex_m = target_to_cm(target);
880  struct armv7m_common *armv7m = &cortex_m->armv7m;
881 
882  /* Read from Debug Halting Control and Status Register */
884  if (retval != ERROR_OK) {
886  return retval;
887  }
888 
889  /* Recover from lockup. See ARMv7-M architecture spec,
890  * section B1.5.15 "Unrecoverable exception cases".
891  */
892  if (cortex_m->dcb_dhcsr & S_LOCKUP) {
893  LOG_TARGET_ERROR(target, "clearing lockup after double fault");
896 
897  /* We have to execute the rest (the "finally" equivalent, but
898  * still throw this exception again).
899  */
900  detected_failure = ERROR_FAIL;
901 
902  /* refresh status bits */
904  if (retval != ERROR_OK)
905  return retval;
906  }
907 
908  if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
910  if (target->state != TARGET_RESET) {
912  LOG_TARGET_INFO(target, "external reset detected");
913  }
914  return ERROR_OK;
915  }
916 
917  if (target->state == TARGET_RESET) {
918  /* Cannot switch context while running so endreset is
919  * called with target->state == TARGET_RESET
920  */
921  LOG_TARGET_DEBUG(target, "Exit from reset with dcb_dhcsr 0x%" PRIx32,
922  cortex_m->dcb_dhcsr);
924  if (retval != ERROR_OK) {
926  return retval;
927  }
929  prev_target_state = TARGET_RUNNING;
930  }
931 
932  if (cortex_m->dcb_dhcsr & S_HALT) {
934 
935  if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET)) {
936  retval = cortex_m_debug_entry(target);
937  if (retval != ERROR_OK)
938  return retval;
939 
940  if (arm_semihosting(target, &retval) != 0)
941  return retval;
942 
944  }
945  if (prev_target_state == TARGET_DEBUG_RUNNING) {
946  retval = cortex_m_debug_entry(target);
947  if (retval != ERROR_OK)
948  return retval;
949 
951  }
952  }
953 
954  if (target->state == TARGET_UNKNOWN) {
955  /* Check if processor is retiring instructions or sleeping.
956  * Unlike S_RESET_ST here we test if the target *is* running now,
957  * not if it has been running (possibly in the past). Instructions are
958  * typically processed much faster than OpenOCD polls DHCSR so S_RETIRE_ST
959  * is read always 1. That's the reason not to use dcb_dhcsr_cumulated_sticky.
960  */
961  if (cortex_m->dcb_dhcsr & S_RETIRE_ST || cortex_m->dcb_dhcsr & S_SLEEP) {
963  retval = ERROR_OK;
964  }
965  }
966 
967  /* Check that target is truly halted, since the target could be resumed externally */
968  if ((prev_target_state == TARGET_HALTED) && !(cortex_m->dcb_dhcsr & S_HALT)) {
969  /* registers are now invalid */
971 
973  LOG_TARGET_WARNING(target, "external resume detected");
975  retval = ERROR_OK;
976  }
977 
978  /* Did we detect a failure condition that we cleared? */
979  if (detected_failure != ERROR_OK)
980  retval = detected_failure;
981  return retval;
982 }
983 
984 static int cortex_m_halt(struct target *target)
985 {
986  LOG_TARGET_DEBUG(target, "target->state: %s", target_state_name(target));
987 
988  if (target->state == TARGET_HALTED) {
989  LOG_TARGET_DEBUG(target, "target was already halted");
990  return ERROR_OK;
991  }
992 
993  if (target->state == TARGET_UNKNOWN)
994  LOG_TARGET_WARNING(target, "target was in unknown state when halt was requested");
995 
996  if (target->state == TARGET_RESET) {
998  LOG_TARGET_ERROR(target, "can't request a halt while in reset if nSRST pulls nTRST");
999  return ERROR_TARGET_FAILURE;
1000  } else {
1001  /* we came here in a reset_halt or reset_init sequence
1002  * debug entry was already prepared in cortex_m3_assert_reset()
1003  */
1005 
1006  return ERROR_OK;
1007  }
1008  }
1009 
1010  /* Write to Debug Halting Control and Status Register */
1012 
1013  /* Do this really early to minimize the window where the MASKINTS erratum
1014  * can pile up pending interrupts. */
1016 
1018 
1019  return ERROR_OK;
1020 }
1021 
1023 {
1024  struct cortex_m_common *cortex_m = target_to_cm(target);
1025  struct armv7m_common *armv7m = &cortex_m->armv7m;
1026  int retval, timeout = 0;
1027 
1028  /* on single cortex_m MCU soft_reset_halt should be avoided as same functionality
1029  * can be obtained by using 'reset halt' and 'cortex_m reset_config vectreset'.
1030  * As this reset only uses VC_CORERESET it would only ever reset the cortex_m
1031  * core, not the peripherals */
1032  LOG_TARGET_DEBUG(target, "soft_reset_halt is discouraged, please use 'reset halt' instead.");
1033 
1034  if (!cortex_m->vectreset_supported) {
1035  LOG_TARGET_ERROR(target, "VECTRESET is not supported on this Cortex-M core");
1036  return ERROR_FAIL;
1037  }
1038 
1039  /* Set C_DEBUGEN */
1041  if (retval != ERROR_OK)
1042  return retval;
1043 
1044  /* Enter debug state on reset; restore DEMCR in endreset_event() */
1045  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
1047  if (retval != ERROR_OK)
1048  return retval;
1049 
1050  /* Request a core-only reset */
1051  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1053  if (retval != ERROR_OK)
1054  return retval;
1056 
1057  /* registers are now invalid */
1059 
1060  while (timeout < 100) {
1062  if (retval == ERROR_OK) {
1063  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR,
1064  &cortex_m->nvic_dfsr);
1065  if (retval != ERROR_OK)
1066  return retval;
1067  if ((cortex_m->dcb_dhcsr & S_HALT)
1068  && (cortex_m->nvic_dfsr & DFSR_VCATCH)) {
1069  LOG_TARGET_DEBUG(target, "system reset-halted, DHCSR 0x%08" PRIx32 ", DFSR 0x%08" PRIx32,
1070  cortex_m->dcb_dhcsr, cortex_m->nvic_dfsr);
1072  /* FIXME restore user's vector catch config */
1073  return ERROR_OK;
1074  } else {
1075  LOG_TARGET_DEBUG(target, "waiting for system reset-halt, "
1076  "DHCSR 0x%08" PRIx32 ", %d ms",
1077  cortex_m->dcb_dhcsr, timeout);
1078  }
1079  }
1080  timeout++;
1081  alive_sleep(1);
1082  }
1083 
1084  return ERROR_OK;
1085 }
1086 
1088 {
1090 
1091  /* set any pending breakpoints */
1092  while (breakpoint) {
1093  if (!breakpoint->is_set)
1096  }
1097 }
1098 
1099 static int cortex_m_resume(struct target *target, int current,
1100  target_addr_t address, int handle_breakpoints, int debug_execution)
1101 {
1102  struct armv7m_common *armv7m = target_to_armv7m(target);
1103  struct breakpoint *breakpoint = NULL;
1104  uint32_t resume_pc;
1105  struct reg *r;
1106 
1107  if (target->state != TARGET_HALTED) {
1108  LOG_TARGET_WARNING(target, "target not halted");
1109  return ERROR_TARGET_NOT_HALTED;
1110  }
1111 
1112  if (!debug_execution) {
1116  }
1117 
1118  if (debug_execution) {
1119  r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
1120 
1121  /* Disable interrupts */
1122  /* We disable interrupts in the PRIMASK register instead of
1123  * masking with C_MASKINTS. This is probably the same issue
1124  * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
1125  * in parallel with disabled interrupts can cause local faults
1126  * to not be taken.
1127  *
1128  * This breaks non-debug (application) execution if not
1129  * called from armv7m_start_algorithm() which saves registers.
1130  */
1131  buf_set_u32(r->value, 0, 1, 1);
1132  r->dirty = true;
1133  r->valid = true;
1134 
1135  /* Make sure we are in Thumb mode, set xPSR.T bit */
1136  /* armv7m_start_algorithm() initializes entire xPSR register.
1137  * This duplicity handles the case when cortex_m_resume()
1138  * is used with the debug_execution flag directly,
1139  * not called through armv7m_start_algorithm().
1140  */
1141  r = armv7m->arm.cpsr;
1142  buf_set_u32(r->value, 24, 1, 1);
1143  r->dirty = true;
1144  r->valid = true;
1145  }
1146 
1147  /* current = 1: continue on current pc, otherwise continue at <address> */
1148  r = armv7m->arm.pc;
1149  if (!current) {
1150  buf_set_u32(r->value, 0, 32, address);
1151  r->dirty = true;
1152  r->valid = true;
1153  }
1154 
1155  /* if we halted last time due to a bkpt instruction
1156  * then we have to manually step over it, otherwise
1157  * the core will break again */
1158 
1159  if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
1160  && !debug_execution)
1162 
1163  resume_pc = buf_get_u32(r->value, 0, 32);
1164 
1166 
1167  /* the front-end may request us not to handle breakpoints */
1168  if (handle_breakpoints) {
1169  /* Single step past breakpoint at current address */
1170  breakpoint = breakpoint_find(target, resume_pc);
1171  if (breakpoint) {
1172  LOG_TARGET_DEBUG(target, "unset breakpoint at " TARGET_ADDR_FMT " (ID: %" PRIu32 ")",
1178  }
1179  }
1180 
1181  /* Restart core */
1184 
1186 
1187  /* registers are now invalid */
1189 
1190  if (!debug_execution) {
1193  LOG_TARGET_DEBUG(target, "target resumed at 0x%" PRIx32 "", resume_pc);
1194  } else {
1197  LOG_TARGET_DEBUG(target, "target debug resumed at 0x%" PRIx32 "", resume_pc);
1198  }
1199 
1200  return ERROR_OK;
1201 }
1202 
1203 /* int irqstepcount = 0; */
1204 static int cortex_m_step(struct target *target, int current,
1205  target_addr_t address, int handle_breakpoints)
1206 {
1207  struct cortex_m_common *cortex_m = target_to_cm(target);
1208  struct armv7m_common *armv7m = &cortex_m->armv7m;
1209  struct breakpoint *breakpoint = NULL;
1210  struct reg *pc = armv7m->arm.pc;
1211  bool bkpt_inst_found = false;
1212  int retval;
1213  bool isr_timed_out = false;
1214 
1215  if (target->state != TARGET_HALTED) {
1216  LOG_TARGET_WARNING(target, "target not halted");
1217  return ERROR_TARGET_NOT_HALTED;
1218  }
1219 
1220  /* current = 1: continue on current pc, otherwise continue at <address> */
1221  if (!current) {
1222  buf_set_u32(pc->value, 0, 32, address);
1223  pc->dirty = true;
1224  pc->valid = true;
1225  }
1226 
1227  uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
1228 
1229  /* the front-end may request us not to handle breakpoints */
1230  if (handle_breakpoints) {
1231  breakpoint = breakpoint_find(target, pc_value);
1232  if (breakpoint)
1234  }
1235 
1236  armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
1237 
1239 
1241 
1243 
1244  /* if no bkpt instruction is found at pc then we can perform
1245  * a normal step, otherwise we have to manually step over the bkpt
1246  * instruction - as such simulate a step */
1247  if (bkpt_inst_found == false) {
1248  if (cortex_m->isrmasking_mode != CORTEX_M_ISRMASK_AUTO) {
1249  /* Automatic ISR masking mode off: Just step over the next
1250  * instruction, with interrupts on or off as appropriate. */
1253  } else {
1254  /* Process interrupts during stepping in a way they don't interfere
1255  * debugging.
1256  *
1257  * Principle:
1258  *
1259  * Set a temporary break point at the current pc and let the core run
1260  * with interrupts enabled. Pending interrupts get served and we run
1261  * into the breakpoint again afterwards. Then we step over the next
1262  * instruction with interrupts disabled.
1263  *
1264  * If the pending interrupts don't complete within time, we leave the
1265  * core running. This may happen if the interrupts trigger faster
1266  * than the core can process them or the handler doesn't return.
1267  *
1268  * If no more breakpoints are available we simply do a step with
1269  * interrupts enabled.
1270  *
1271  */
1272 
1273  /* 2012-09-29 ph
1274  *
1275  * If a break point is already set on the lower half word then a break point on
1276  * the upper half word will not break again when the core is restarted. So we
1277  * just step over the instruction with interrupts disabled.
1278  *
1279  * The documentation has no information about this, it was found by observation
1280  * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 doesn't seem to
1281  * suffer from this problem.
1282  *
1283  * To add some confusion: pc_value has bit 0 always set, while the breakpoint
1284  * address has it always cleared. The former is done to indicate thumb mode
1285  * to gdb.
1286  *
1287  */
1288  if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
1289  LOG_TARGET_DEBUG(target, "Stepping over next instruction with interrupts disabled");
1292  /* Re-enable interrupts if appropriate */
1295  } else {
1296 
1297  /* Set a temporary break point */
1298  if (breakpoint) {
1300  } else {
1302  if (cortex_m->fp_rev == 0 && pc_value > 0x1FFFFFFF) {
1303  /* FPB rev.1 cannot handle such addr, try BKPT instr */
1304  type = BKPT_SOFT;
1305  }
1306  retval = breakpoint_add(target, pc_value, 2, type);
1307  }
1308 
1309  bool tmp_bp_set = (retval == ERROR_OK);
1310 
1311  /* No more breakpoints left, just do a step */
1312  if (!tmp_bp_set) {
1315  /* Re-enable interrupts if appropriate */
1318  } else {
1319  /* Start the core */
1320  LOG_TARGET_DEBUG(target, "Starting core to serve pending interrupts");
1321  int64_t t_start = timeval_ms();
1324 
1325  /* Wait for pending handlers to complete or timeout */
1326  do {
1328  if (retval != ERROR_OK) {
1330  return retval;
1331  }
1332  isr_timed_out = ((timeval_ms() - t_start) > 500);
1333  } while (!((cortex_m->dcb_dhcsr & S_HALT) || isr_timed_out));
1334 
1335  /* only remove breakpoint if we created it */
1336  if (breakpoint)
1338  else {
1339  /* Remove the temporary breakpoint */
1340  breakpoint_remove(target, pc_value);
1341  }
1342 
1343  if (isr_timed_out) {
1344  LOG_TARGET_DEBUG(target, "Interrupt handlers didn't complete within time, "
1345  "leaving target running");
1346  } else {
1347  /* Step over next instruction with interrupts disabled */
1350  C_HALT | C_MASKINTS,
1351  0);
1353  /* Re-enable interrupts if appropriate */
1356  }
1357  }
1358  }
1359  }
1360  }
1361 
1363  if (retval != ERROR_OK)
1364  return retval;
1365 
1366  /* registers are now invalid */
1368 
1369  if (breakpoint)
1371 
1372  if (isr_timed_out) {
1373  /* Leave the core running. The user has to stop execution manually. */
1376  return ERROR_OK;
1377  }
1378 
1379  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1380  " nvic_icsr = 0x%" PRIx32,
1381  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1382 
1383  retval = cortex_m_debug_entry(target);
1384  if (retval != ERROR_OK)
1385  return retval;
1387 
1388  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1389  " nvic_icsr = 0x%" PRIx32,
1390  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1391 
1392  return ERROR_OK;
1393 }
1394 
1396 {
1397  struct cortex_m_common *cortex_m = target_to_cm(target);
1398  struct armv7m_common *armv7m = &cortex_m->armv7m;
1399  enum cortex_m_soft_reset_config reset_config = cortex_m->soft_reset_config;
1400 
1401  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1403  target_was_examined(target) ? "" : " not");
1404 
1406 
1408  /* allow scripts to override the reset event */
1409 
1413 
1414  return ERROR_OK;
1415  }
1416 
1417  /* some cores support connecting while srst is asserted
1418  * use that mode is it has been configured */
1419 
1420  bool srst_asserted = false;
1421 
1423  ((jtag_reset_config & RESET_SRST_NO_GATING) || !armv7m->debug_ap)) {
1424  /* If we have no debug_ap, asserting SRST is the only thing
1425  * we can do now */
1427  srst_asserted = true;
1428  }
1429 
1430  /* TODO: replace the hack calling target_examine_one()
1431  * as soon as a better reset framework is available */
1434  LOG_TARGET_DEBUG(target, "Trying to re-examine under reset");
1436  }
1437 
1438  /* We need at least debug_ap to go further.
1439  * Inform user and bail out if we don't have one. */
1440  if (!armv7m->debug_ap) {
1441  if (srst_asserted) {
1442  if (target->reset_halt)
1443  LOG_TARGET_ERROR(target, "Debug AP not available, will not halt after reset!");
1444 
1445  /* Do not propagate error: reset was asserted, proceed to deassert! */
1448  return ERROR_OK;
1449 
1450  } else {
1451  LOG_TARGET_ERROR(target, "Debug AP not available, reset NOT asserted!");
1452  return ERROR_FAIL;
1453  }
1454  }
1455 
1456  /* Enable debug requests */
1458 
1459  /* Store important errors instead of failing and proceed to reset assert */
1460 
1461  if (retval != ERROR_OK || !(cortex_m->dcb_dhcsr & C_DEBUGEN))
1463 
1464  /* If the processor is sleeping in a WFI or WFE instruction, the
1465  * C_HALT bit must be asserted to regain control */
1466  if (retval == ERROR_OK && (cortex_m->dcb_dhcsr & S_SLEEP))
1468 
1469  mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
1470  /* Ignore less important errors */
1471 
1472  if (!target->reset_halt) {
1473  /* Set/Clear C_MASKINTS in a separate operation */
1475 
1476  /* clear any debug flags before resuming */
1478 
1479  /* clear C_HALT in dhcsr reg */
1481  } else {
1482  /* Halt in debug on reset; endreset_event() restores DEMCR.
1483  *
1484  * REVISIT catching BUSERR presumably helps to defend against
1485  * bad vector table entries. Should this include MMERR or
1486  * other flags too?
1487  */
1488  int retval2;
1489  retval2 = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DEMCR,
1491  if (retval != ERROR_OK || retval2 != ERROR_OK)
1492  LOG_TARGET_INFO(target, "AP write error, reset will not halt");
1493  }
1494 
1496  /* default to asserting srst */
1497  if (!srst_asserted)
1499 
1500  /* srst is asserted, ignore AP access errors */
1501  retval = ERROR_OK;
1502  } else {
1503  /* Use a standard Cortex-M3 software reset mechanism.
1504  * We default to using VECTRESET as it is supported on all current cores
1505  * (except Cortex-M0, M0+ and M1 which support SYSRESETREQ only!)
1506  * This has the disadvantage of not resetting the peripherals, so a
1507  * reset-init event handler is needed to perform any peripheral resets.
1508  */
1509  if (!cortex_m->vectreset_supported
1510  && reset_config == CORTEX_M_RESET_VECTRESET) {
1511  reset_config = CORTEX_M_RESET_SYSRESETREQ;
1512  LOG_TARGET_WARNING(target, "VECTRESET is not supported on this Cortex-M core, using SYSRESETREQ instead.");
1513  LOG_TARGET_WARNING(target, "Set 'cortex_m reset_config sysresetreq'.");
1514  }
1515 
1516  LOG_TARGET_DEBUG(target, "Using Cortex-M %s", (reset_config == CORTEX_M_RESET_SYSRESETREQ)
1517  ? "SYSRESETREQ" : "VECTRESET");
1518 
1519  if (reset_config == CORTEX_M_RESET_VECTRESET) {
1520  LOG_TARGET_WARNING(target, "Only resetting the Cortex-M core, use a reset-init event "
1521  "handler to reset any peripherals or configure hardware srst support.");
1522  }
1523 
1524  int retval3;
1525  retval3 = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1526  AIRCR_VECTKEY | ((reset_config == CORTEX_M_RESET_SYSRESETREQ)
1528  if (retval3 != ERROR_OK)
1529  LOG_TARGET_DEBUG(target, "Ignoring AP write error right after reset");
1530 
1531  retval3 = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1532  if (retval3 != ERROR_OK) {
1533  LOG_TARGET_ERROR(target, "DP initialisation failed");
1534  /* The error return value must not be propagated in this case.
1535  * SYSRESETREQ or VECTRESET have been possibly triggered
1536  * so reset processing should continue */
1537  } else {
1538  /* I do not know why this is necessary, but it
1539  * fixes strange effects (step/resume cause NMI
1540  * after reset) on LM3S6918 -- Michael Schwingen
1541  */
1542  uint32_t tmp;
1543  mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_AIRCR, &tmp);
1544  }
1545  }
1546 
1548  jtag_sleep(50000);
1549 
1551 
1552  /* now return stored error code if any */
1553  if (retval != ERROR_OK)
1554  return retval;
1555 
1557  retval = target_halt(target);
1558  if (retval != ERROR_OK)
1559  return retval;
1560  }
1561 
1562  return ERROR_OK;
1563 }
1564 
1566 {
1567  struct armv7m_common *armv7m = &target_to_cm(target)->armv7m;
1568 
1569  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1571  target_was_examined(target) ? "" : " not");
1572 
1573  /* deassert reset lines */
1575 
1577 
1580  armv7m->debug_ap) {
1581 
1582  int retval = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1583  if (retval != ERROR_OK) {
1584  LOG_TARGET_ERROR(target, "DP initialisation failed");
1585  return retval;
1586  }
1587  }
1588 
1589  return ERROR_OK;
1590 }
1591 
1593 {
1594  int retval;
1595  unsigned int fp_num = 0;
1596  struct cortex_m_common *cortex_m = target_to_cm(target);
1597  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1598 
1599  if (breakpoint->is_set) {
1600  LOG_TARGET_WARNING(target, "breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id);
1601  return ERROR_OK;
1602  }
1603 
1604  if (breakpoint->type == BKPT_HARD) {
1605  uint32_t fpcr_value;
1606  while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
1607  fp_num++;
1608  if (fp_num >= cortex_m->fp_num_code) {
1609  LOG_TARGET_ERROR(target, "Can not find free FPB Comparator!");
1611  }
1612  breakpoint_hw_set(breakpoint, fp_num);
1613  fpcr_value = breakpoint->address | 1;
1614  if (cortex_m->fp_rev == 0) {
1615  if (breakpoint->address > 0x1FFFFFFF) {
1616  LOG_TARGET_ERROR(target, "Cortex-M Flash Patch Breakpoint rev.1 "
1617  "cannot handle HW breakpoint above address 0x1FFFFFFE");
1618  return ERROR_FAIL;
1619  }
1620  uint32_t hilo;
1622  fpcr_value = (fpcr_value & 0x1FFFFFFC) | hilo | 1;
1623  } else if (cortex_m->fp_rev > 1) {
1624  LOG_TARGET_ERROR(target, "Unhandled Cortex-M Flash Patch Breakpoint architecture revision");
1625  return ERROR_FAIL;
1626  }
1627  comparator_list[fp_num].used = true;
1628  comparator_list[fp_num].fpcr_value = fpcr_value;
1629  target_write_u32(target, comparator_list[fp_num].fpcr_address,
1630  comparator_list[fp_num].fpcr_value);
1631  LOG_TARGET_DEBUG(target, "fpc_num %i fpcr_value 0x%" PRIx32 "",
1632  fp_num,
1633  comparator_list[fp_num].fpcr_value);
1634  if (!cortex_m->fpb_enabled) {
1635  LOG_TARGET_DEBUG(target, "FPB wasn't enabled, do it now");
1636  retval = cortex_m_enable_fpb(target);
1637  if (retval != ERROR_OK) {
1638  LOG_TARGET_ERROR(target, "Failed to enable the FPB");
1639  return retval;
1640  }
1641 
1642  cortex_m->fpb_enabled = true;
1643  }
1644  } else if (breakpoint->type == BKPT_SOFT) {
1645  uint8_t code[4];
1646 
1647  /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
1648  * semihosting; don't use that. Otherwise the BKPT
1649  * parameter is arbitrary.
1650  */
1651  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1652  retval = target_read_memory(target,
1653  breakpoint->address & 0xFFFFFFFE,
1654  breakpoint->length, 1,
1656  if (retval != ERROR_OK)
1657  return retval;
1658  retval = target_write_memory(target,
1659  breakpoint->address & 0xFFFFFFFE,
1660  breakpoint->length, 1,
1661  code);
1662  if (retval != ERROR_OK)
1663  return retval;
1664  breakpoint->is_set = true;
1665  }
1666 
1667  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1669  (int)(breakpoint->type),
1671  breakpoint->length,
1672  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1673 
1674  return ERROR_OK;
1675 }
1676 
1678 {
1679  int retval;
1680  struct cortex_m_common *cortex_m = target_to_cm(target);
1681  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1682 
1683  if (!breakpoint->is_set) {
1684  LOG_TARGET_WARNING(target, "breakpoint not set");
1685  return ERROR_OK;
1686  }
1687 
1688  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1690  (int)(breakpoint->type),
1692  breakpoint->length,
1693  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1694 
1695  if (breakpoint->type == BKPT_HARD) {
1696  unsigned int fp_num = breakpoint->number;
1697  if (fp_num >= cortex_m->fp_num_code) {
1698  LOG_TARGET_DEBUG(target, "Invalid FP Comparator number in breakpoint");
1699  return ERROR_OK;
1700  }
1701  comparator_list[fp_num].used = false;
1702  comparator_list[fp_num].fpcr_value = 0;
1703  target_write_u32(target, comparator_list[fp_num].fpcr_address,
1704  comparator_list[fp_num].fpcr_value);
1705  } else {
1706  /* restore original instruction (kept in target endianness) */
1707  retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE,
1708  breakpoint->length, 1,
1710  if (retval != ERROR_OK)
1711  return retval;
1712  }
1713  breakpoint->is_set = false;
1714 
1715  return ERROR_OK;
1716 }
1717 
1719 {
1720  if (breakpoint->length == 3) {
1721  LOG_TARGET_DEBUG(target, "Using a two byte breakpoint for 32bit Thumb-2 request");
1722  breakpoint->length = 2;
1723  }
1724 
1725  if ((breakpoint->length != 2)) {
1726  LOG_TARGET_INFO(target, "only breakpoints of two bytes length supported");
1728  }
1729 
1731 }
1732 
1734 {
1735  if (!breakpoint->is_set)
1736  return ERROR_OK;
1737 
1739 }
1740 
1742 {
1743  unsigned int dwt_num = 0;
1744  struct cortex_m_common *cortex_m = target_to_cm(target);
1745 
1746  /* REVISIT Don't fully trust these "not used" records ... users
1747  * may set up breakpoints by hand, e.g. dual-address data value
1748  * watchpoint using comparator #1; comparator #0 matching cycle
1749  * count; send data trace info through ITM and TPIU; etc
1750  */
1751  struct cortex_m_dwt_comparator *comparator;
1752 
1753  for (comparator = cortex_m->dwt_comparator_list;
1754  comparator->used && dwt_num < cortex_m->dwt_num_comp;
1755  comparator++, dwt_num++)
1756  continue;
1757  if (dwt_num >= cortex_m->dwt_num_comp) {
1758  LOG_TARGET_ERROR(target, "Can not find free DWT Comparator");
1759  return ERROR_FAIL;
1760  }
1761  comparator->used = true;
1762  watchpoint_set(watchpoint, dwt_num);
1763 
1764  comparator->comp = watchpoint->address;
1766  comparator->comp);
1767 
1768  if ((cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M) {
1769  uint32_t mask = 0, temp;
1770 
1771  /* watchpoint params were validated earlier */
1772  temp = watchpoint->length;
1773  while (temp) {
1774  temp >>= 1;
1775  mask++;
1776  }
1777  mask--;
1778 
1779  comparator->mask = mask;
1781  comparator->mask);
1782 
1783  switch (watchpoint->rw) {
1784  case WPT_READ:
1785  comparator->function = 5;
1786  break;
1787  case WPT_WRITE:
1788  comparator->function = 6;
1789  break;
1790  case WPT_ACCESS:
1791  comparator->function = 7;
1792  break;
1793  }
1794  } else {
1795  uint32_t data_size = watchpoint->length >> 1;
1796  comparator->mask = (watchpoint->length >> 1) | 1;
1797 
1798  switch (watchpoint->rw) {
1799  case WPT_ACCESS:
1800  comparator->function = 4;
1801  break;
1802  case WPT_WRITE:
1803  comparator->function = 5;
1804  break;
1805  case WPT_READ:
1806  comparator->function = 6;
1807  break;
1808  }
1809  comparator->function = comparator->function | (1 << 4) |
1810  (data_size << 10);
1811  }
1812 
1814  comparator->function);
1815 
1816  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%d 0x%08x 0x%x 0x%05x",
1817  watchpoint->unique_id, dwt_num,
1818  (unsigned) comparator->comp,
1819  (unsigned) comparator->mask,
1820  (unsigned) comparator->function);
1821  return ERROR_OK;
1822 }
1823 
1825 {
1826  struct cortex_m_common *cortex_m = target_to_cm(target);
1827  struct cortex_m_dwt_comparator *comparator;
1828 
1829  if (!watchpoint->is_set) {
1830  LOG_TARGET_WARNING(target, "watchpoint (wpid: %d) not set",
1832  return ERROR_OK;
1833  }
1834 
1835  unsigned int dwt_num = watchpoint->number;
1836 
1837  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%u address: 0x%08x clear",
1838  watchpoint->unique_id, dwt_num,
1839  (unsigned) watchpoint->address);
1840 
1841  if (dwt_num >= cortex_m->dwt_num_comp) {
1842  LOG_TARGET_DEBUG(target, "Invalid DWT Comparator number in watchpoint");
1843  return ERROR_OK;
1844  }
1845 
1846  comparator = cortex_m->dwt_comparator_list + dwt_num;
1847  comparator->used = false;
1848  comparator->function = 0;
1850  comparator->function);
1851 
1852  watchpoint->is_set = false;
1853 
1854  return ERROR_OK;
1855 }
1856 
1858 {
1859  struct cortex_m_common *cortex_m = target_to_cm(target);
1860 
1861  if (cortex_m->dwt_comp_available < 1) {
1862  LOG_TARGET_DEBUG(target, "no comparators?");
1864  }
1865 
1866  /* hardware doesn't support data value masking */
1867  if (watchpoint->mask != ~(uint32_t)0) {
1868  LOG_TARGET_DEBUG(target, "watchpoint value masks not supported");
1870  }
1871 
1872  /* hardware allows address masks of up to 32K */
1873  unsigned mask;
1874 
1875  for (mask = 0; mask < 16; mask++) {
1876  if ((1u << mask) == watchpoint->length)
1877  break;
1878  }
1879  if (mask == 16) {
1880  LOG_TARGET_DEBUG(target, "unsupported watchpoint length");
1882  }
1883  if (watchpoint->address & ((1 << mask) - 1)) {
1884  LOG_TARGET_DEBUG(target, "watchpoint address is unaligned");
1886  }
1887 
1888  /* Caller doesn't seem to be able to describe watching for data
1889  * values of zero; that flags "no value".
1890  *
1891  * REVISIT This DWT may well be able to watch for specific data
1892  * values. Requires comparator #1 to set DATAVMATCH and match
1893  * the data, and another comparator (DATAVADDR0) matching addr.
1894  */
1895  if (watchpoint->value) {
1896  LOG_TARGET_DEBUG(target, "data value watchpoint not YET supported");
1898  }
1899 
1900  cortex_m->dwt_comp_available--;
1901  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
1902 
1903  return ERROR_OK;
1904 }
1905 
1907 {
1908  struct cortex_m_common *cortex_m = target_to_cm(target);
1909 
1910  /* REVISIT why check? DWT can be updated with core running ... */
1911  if (target->state != TARGET_HALTED) {
1912  LOG_TARGET_WARNING(target, "target not halted");
1913  return ERROR_TARGET_NOT_HALTED;
1914  }
1915 
1916  if (watchpoint->is_set)
1918 
1919  cortex_m->dwt_comp_available++;
1920  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
1921 
1922  return ERROR_OK;
1923 }
1924 
1925 static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
1926 {
1928  return ERROR_FAIL;
1929 
1930  struct cortex_m_common *cortex_m = target_to_cm(target);
1931 
1932  for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
1933  if (!wp->is_set)
1934  continue;
1935 
1936  unsigned int dwt_num = wp->number;
1937  struct cortex_m_dwt_comparator *comparator = cortex_m->dwt_comparator_list + dwt_num;
1938 
1939  uint32_t dwt_function;
1940  int retval = target_read_u32(target, comparator->dwt_comparator_address + 8, &dwt_function);
1941  if (retval != ERROR_OK)
1942  return ERROR_FAIL;
1943 
1944  /* check the MATCHED bit */
1945  if (dwt_function & BIT(24)) {
1946  *hit_watchpoint = wp;
1947  return ERROR_OK;
1948  }
1949  }
1950 
1951  return ERROR_FAIL;
1952 }
1953 
1955 {
1957 
1958  /* set any pending watchpoints */
1959  while (watchpoint) {
1960  if (!watchpoint->is_set)
1963  }
1964 }
1965 
1967  uint32_t size, uint32_t count, uint8_t *buffer)
1968 {
1969  struct armv7m_common *armv7m = target_to_armv7m(target);
1970 
1971  if (armv7m->arm.arch == ARM_ARCH_V6M) {
1972  /* armv6m does not handle unaligned memory access */
1973  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1975  }
1976 
1977  return mem_ap_read_buf(armv7m->debug_ap, buffer, size, count, address);
1978 }
1979 
1980 static int cortex_m_write_memory(struct target *target, target_addr_t address,
1981  uint32_t size, uint32_t count, const uint8_t *buffer)
1982 {
1983  struct armv7m_common *armv7m = target_to_armv7m(target);
1984 
1985  if (armv7m->arm.arch == ARM_ARCH_V6M) {
1986  /* armv6m does not handle unaligned memory access */
1987  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1989  }
1990 
1991  return mem_ap_write_buf(armv7m->debug_ap, buffer, size, count, address);
1992 }
1993 
1994 static int cortex_m_init_target(struct command_context *cmd_ctx,
1995  struct target *target)
1996 {
1999  return ERROR_OK;
2000 }
2001 
2003 {
2004  struct cortex_m_common *cortex_m = target_to_cm(target);
2005  struct armv7m_common *armv7m = target_to_armv7m(target);
2006 
2007  if (!armv7m->is_hla_target && armv7m->debug_ap)
2008  dap_put_ap(armv7m->debug_ap);
2009 
2010  free(cortex_m->fp_comparator_list);
2011 
2014 
2015  free(target->private_config);
2016  free(cortex_m);
2017 }
2018 
2019 int cortex_m_profiling(struct target *target, uint32_t *samples,
2020  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2021 {
2022  struct timeval timeout, now;
2023  struct armv7m_common *armv7m = target_to_armv7m(target);
2024  uint32_t reg_value;
2025  int retval;
2026 
2027  retval = target_read_u32(target, DWT_PCSR, &reg_value);
2028  if (retval != ERROR_OK) {
2029  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2030  return retval;
2031  }
2032  if (reg_value == 0) {
2033  LOG_TARGET_INFO(target, "PCSR sampling not supported on this processor.");
2034  return target_profiling_default(target, samples, max_num_samples, num_samples, seconds);
2035  }
2036 
2038  timeval_add_time(&timeout, seconds, 0);
2039 
2040  LOG_TARGET_INFO(target, "Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
2041 
2042  /* Make sure the target is running */
2044  if (target->state == TARGET_HALTED)
2045  retval = target_resume(target, 1, 0, 0, 0);
2046 
2047  if (retval != ERROR_OK) {
2048  LOG_TARGET_ERROR(target, "Error while resuming target");
2049  return retval;
2050  }
2051 
2052  uint32_t sample_count = 0;
2053 
2054  for (;;) {
2055  if (armv7m && armv7m->debug_ap) {
2056  uint32_t read_count = max_num_samples - sample_count;
2057  if (read_count > 1024)
2058  read_count = 1024;
2059 
2060  retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
2061  (void *)&samples[sample_count],
2062  4, read_count, DWT_PCSR);
2063  sample_count += read_count;
2064  } else {
2065  target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
2066  }
2067 
2068  if (retval != ERROR_OK) {
2069  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2070  return retval;
2071  }
2072 
2073 
2074  gettimeofday(&now, NULL);
2075  if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
2076  LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 " samples.", sample_count);
2077  break;
2078  }
2079  }
2080 
2081  *num_samples = sample_count;
2082  return retval;
2083 }
2084 
2085 
2086 /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
2087  * on r/w if the core is not running, and clear on resume or reset ... or
2088  * at least, in a post_restore_context() method.
2089  */
2090 
2092  struct target *target;
2093  uint32_t addr;
2094  uint8_t value[4]; /* scratch/cache */
2095 };
2096 
2097 static int cortex_m_dwt_get_reg(struct reg *reg)
2098 {
2099  struct dwt_reg_state *state = reg->arch_info;
2100 
2101  uint32_t tmp;
2102  int retval = target_read_u32(state->target, state->addr, &tmp);
2103  if (retval != ERROR_OK)
2104  return retval;
2105 
2106  buf_set_u32(state->value, 0, 32, tmp);
2107  return ERROR_OK;
2108 }
2109 
2110 static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
2111 {
2112  struct dwt_reg_state *state = reg->arch_info;
2113 
2114  return target_write_u32(state->target, state->addr,
2115  buf_get_u32(buf, 0, reg->size));
2116 }
2117 
2118 struct dwt_reg {
2119  uint32_t addr;
2120  const char *name;
2121  unsigned size;
2122 };
2123 
2124 static const struct dwt_reg dwt_base_regs[] = {
2125  { DWT_CTRL, "dwt_ctrl", 32, },
2126  /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
2127  * increments while the core is asleep.
2128  */
2129  { DWT_CYCCNT, "dwt_cyccnt", 32, },
2130  /* plus some 8 bit counters, useful for profiling with TPIU */
2131 };
2132 
2133 static const struct dwt_reg dwt_comp[] = {
2134 #define DWT_COMPARATOR(i) \
2135  { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
2136  { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
2137  { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
2138  DWT_COMPARATOR(0),
2139  DWT_COMPARATOR(1),
2140  DWT_COMPARATOR(2),
2141  DWT_COMPARATOR(3),
2142  DWT_COMPARATOR(4),
2143  DWT_COMPARATOR(5),
2144  DWT_COMPARATOR(6),
2145  DWT_COMPARATOR(7),
2146  DWT_COMPARATOR(8),
2147  DWT_COMPARATOR(9),
2148  DWT_COMPARATOR(10),
2149  DWT_COMPARATOR(11),
2150  DWT_COMPARATOR(12),
2151  DWT_COMPARATOR(13),
2152  DWT_COMPARATOR(14),
2153  DWT_COMPARATOR(15),
2154 #undef DWT_COMPARATOR
2155 };
2156 
2157 static const struct reg_arch_type dwt_reg_type = {
2159  .set = cortex_m_dwt_set_reg,
2160 };
2161 
2162 static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
2163 {
2164  struct dwt_reg_state *state;
2165 
2166  state = calloc(1, sizeof(*state));
2167  if (!state)
2168  return;
2169  state->addr = d->addr;
2170  state->target = t;
2171 
2172  r->name = d->name;
2173  r->size = d->size;
2174  r->value = state->value;
2175  r->arch_info = state;
2176  r->type = &dwt_reg_type;
2177 }
2178 
2179 static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
2180 {
2181  uint32_t dwtcr;
2182  struct reg_cache *cache;
2183  struct cortex_m_dwt_comparator *comparator;
2184  int reg;
2185 
2186  target_read_u32(target, DWT_CTRL, &dwtcr);
2187  LOG_TARGET_DEBUG(target, "DWT_CTRL: 0x%" PRIx32, dwtcr);
2188  if (!dwtcr) {
2189  LOG_TARGET_DEBUG(target, "no DWT");
2190  return;
2191  }
2192 
2194  LOG_TARGET_DEBUG(target, "DWT_DEVARCH: 0x%" PRIx32, cm->dwt_devarch);
2195 
2196  cm->dwt_num_comp = (dwtcr >> 28) & 0xF;
2197  cm->dwt_comp_available = cm->dwt_num_comp;
2198  cm->dwt_comparator_list = calloc(cm->dwt_num_comp,
2199  sizeof(struct cortex_m_dwt_comparator));
2200  if (!cm->dwt_comparator_list) {
2201 fail0:
2202  cm->dwt_num_comp = 0;
2203  LOG_TARGET_ERROR(target, "out of mem");
2204  return;
2205  }
2206 
2207  cache = calloc(1, sizeof(*cache));
2208  if (!cache) {
2209 fail1:
2210  free(cm->dwt_comparator_list);
2211  goto fail0;
2212  }
2213  cache->name = "Cortex-M DWT registers";
2214  cache->num_regs = 2 + cm->dwt_num_comp * 3;
2215  cache->reg_list = calloc(cache->num_regs, sizeof(*cache->reg_list));
2216  if (!cache->reg_list) {
2217  free(cache);
2218  goto fail1;
2219  }
2220 
2221  for (reg = 0; reg < 2; reg++)
2223  dwt_base_regs + reg);
2224 
2225  comparator = cm->dwt_comparator_list;
2226  for (unsigned int i = 0; i < cm->dwt_num_comp; i++, comparator++) {
2227  int j;
2228 
2229  comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
2230  for (j = 0; j < 3; j++, reg++)
2232  dwt_comp + 3 * i + j);
2233 
2234  /* make sure we clear any watchpoints enabled on the target */
2235  target_write_u32(target, comparator->dwt_comparator_address + 8, 0);
2236  }
2237 
2239  cm->dwt_cache = cache;
2240 
2241  LOG_TARGET_DEBUG(target, "DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
2242  dwtcr, cm->dwt_num_comp,
2243  (dwtcr & (0xf << 24)) ? " only" : "/trigger");
2244 
2245  /* REVISIT: if num_comp > 1, check whether comparator #1 can
2246  * implement single-address data value watchpoints ... so we
2247  * won't need to check it later, when asked to set one up.
2248  */
2249 }
2250 
2251 static void cortex_m_dwt_free(struct target *target)
2252 {
2253  struct cortex_m_common *cm = target_to_cm(target);
2254  struct reg_cache *cache = cm->dwt_cache;
2255 
2256  free(cm->dwt_comparator_list);
2257  cm->dwt_comparator_list = NULL;
2258  cm->dwt_num_comp = 0;
2259 
2260  if (cache) {
2262 
2263  if (cache->reg_list) {
2264  for (size_t i = 0; i < cache->num_regs; i++)
2265  free(cache->reg_list[i].arch_info);
2266  free(cache->reg_list);
2267  }
2268  free(cache);
2269  }
2270  cm->dwt_cache = NULL;
2271 }
2272 
2273 static bool cortex_m_has_tz(struct target *target)
2274 {
2275  struct armv7m_common *armv7m = target_to_armv7m(target);
2276  uint32_t dauthstatus;
2277 
2278  if (armv7m->arm.arch != ARM_ARCH_V8M)
2279  return false;
2280 
2281  int retval = target_read_u32(target, DAUTHSTATUS, &dauthstatus);
2282  if (retval != ERROR_OK) {
2283  LOG_WARNING("Error reading DAUTHSTATUS register");
2284  return false;
2285  }
2286  return (dauthstatus & DAUTHSTATUS_SID_MASK) != 0;
2287 }
2288 
2289 #define MVFR0 0xe000ef40
2290 #define MVFR1 0xe000ef44
2291 
2292 #define MVFR0_DEFAULT_M4 0x10110021
2293 #define MVFR1_DEFAULT_M4 0x11000011
2294 
2295 #define MVFR0_DEFAULT_M7_SP 0x10110021
2296 #define MVFR0_DEFAULT_M7_DP 0x10110221
2297 #define MVFR1_DEFAULT_M7_SP 0x11000011
2298 #define MVFR1_DEFAULT_M7_DP 0x12000011
2299 
2300 static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp,
2301  struct adiv5_ap **debug_ap)
2302 {
2304  return ERROR_OK;
2305 
2306  return dap_find_get_ap(swjdp, AP_TYPE_AHB5_AP, debug_ap);
2307 }
2308 
2310 {
2311  int retval;
2312  uint32_t cpuid, fpcr, mvfr0, mvfr1;
2313  struct cortex_m_common *cortex_m = target_to_cm(target);
2314  struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
2315  struct armv7m_common *armv7m = target_to_armv7m(target);
2316 
2317  /* hla_target shares the examine handler but does not support
2318  * all its calls */
2319  if (!armv7m->is_hla_target) {
2320  if (!armv7m->debug_ap) {
2321  if (cortex_m->apsel == DP_APSEL_INVALID) {
2322  /* Search for the MEM-AP */
2323  retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
2324  if (retval != ERROR_OK) {
2325  LOG_TARGET_ERROR(target, "Could not find MEM-AP to control the core");
2326  return retval;
2327  }
2328  } else {
2329  armv7m->debug_ap = dap_get_ap(swjdp, cortex_m->apsel);
2330  if (!armv7m->debug_ap) {
2331  LOG_ERROR("Cannot get AP");
2332  return ERROR_FAIL;
2333  }
2334  }
2335  }
2336 
2337  armv7m->debug_ap->memaccess_tck = 8;
2338 
2339  retval = mem_ap_init(armv7m->debug_ap);
2340  if (retval != ERROR_OK)
2341  return retval;
2342  }
2343 
2344  if (!target_was_examined(target)) {
2346 
2347  /* Read from Device Identification Registers */
2348  retval = target_read_u32(target, CPUID, &cpuid);
2349  if (retval != ERROR_OK)
2350  return retval;
2351 
2352  /* Get ARCH and CPU types */
2353  const enum cortex_m_partno core_partno = (cpuid & ARM_CPUID_PARTNO_MASK) >> ARM_CPUID_PARTNO_POS;
2354 
2355  for (unsigned int n = 0; n < ARRAY_SIZE(cortex_m_parts); n++) {
2356  if (core_partno == cortex_m_parts[n].partno) {
2357  cortex_m->core_info = &cortex_m_parts[n];
2358  break;
2359  }
2360  }
2361 
2362  if (!cortex_m->core_info) {
2363  LOG_TARGET_ERROR(target, "Cortex-M PARTNO 0x%x is unrecognized", core_partno);
2364  return ERROR_FAIL;
2365  }
2366 
2367  armv7m->arm.arch = cortex_m->core_info->arch;
2368 
2369  LOG_TARGET_INFO(target, "%s r%" PRId8 "p%" PRId8 " processor detected",
2370  cortex_m->core_info->name,
2371  (uint8_t)((cpuid >> 20) & 0xf),
2372  (uint8_t)((cpuid >> 0) & 0xf));
2373 
2374  cortex_m->maskints_erratum = false;
2375  if (core_partno == CORTEX_M7_PARTNO) {
2376  uint8_t rev, patch;
2377  rev = (cpuid >> 20) & 0xf;
2378  patch = (cpuid >> 0) & 0xf;
2379  if ((rev == 0) && (patch < 2)) {
2380  LOG_TARGET_WARNING(target, "Silicon bug: single stepping may enter pending exception handler!");
2381  cortex_m->maskints_erratum = true;
2382  }
2383  }
2384  LOG_TARGET_DEBUG(target, "cpuid: 0x%8.8" PRIx32 "", cpuid);
2385 
2386  if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV4) {
2387  target_read_u32(target, MVFR0, &mvfr0);
2388  target_read_u32(target, MVFR1, &mvfr1);
2389 
2390  /* test for floating point feature on Cortex-M4 */
2391  if ((mvfr0 == MVFR0_DEFAULT_M4) && (mvfr1 == MVFR1_DEFAULT_M4)) {
2392  LOG_TARGET_DEBUG(target, "%s floating point feature FPv4_SP found", cortex_m->core_info->name);
2393  armv7m->fp_feature = FPV4_SP;
2394  }
2395  } else if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV5) {
2396  target_read_u32(target, MVFR0, &mvfr0);
2397  target_read_u32(target, MVFR1, &mvfr1);
2398 
2399  /* test for floating point features on Cortex-M7 */
2400  if ((mvfr0 == MVFR0_DEFAULT_M7_SP) && (mvfr1 == MVFR1_DEFAULT_M7_SP)) {
2401  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_SP found", cortex_m->core_info->name);
2402  armv7m->fp_feature = FPV5_SP;
2403  } else if ((mvfr0 == MVFR0_DEFAULT_M7_DP) && (mvfr1 == MVFR1_DEFAULT_M7_DP)) {
2404  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP found", cortex_m->core_info->name);
2405  armv7m->fp_feature = FPV5_DP;
2406  }
2407  }
2408 
2409  /* VECTRESET is supported only on ARMv7-M cores */
2410  cortex_m->vectreset_supported = armv7m->arm.arch == ARM_ARCH_V7M;
2411 
2412  /* Check for FPU, otherwise mark FPU register as non-existent */
2413  if (armv7m->fp_feature == FP_NONE)
2414  for (size_t idx = ARMV7M_FPU_FIRST_REG; idx <= ARMV7M_FPU_LAST_REG; idx++)
2415  armv7m->arm.core_cache->reg_list[idx].exist = false;
2416 
2417  if (!cortex_m_has_tz(target))
2418  for (size_t idx = ARMV8M_FIRST_REG; idx <= ARMV8M_LAST_REG; idx++)
2419  armv7m->arm.core_cache->reg_list[idx].exist = false;
2420 
2421  if (!armv7m->is_hla_target) {
2423  /* Cortex-M3/M4 have 4096 bytes autoincrement range,
2424  * s. ARM IHI 0031C: MEM-AP 7.2.2 */
2425  armv7m->debug_ap->tar_autoincr_block = (1 << 12);
2426  }
2427 
2428  retval = target_read_u32(target, DCB_DHCSR, &cortex_m->dcb_dhcsr);
2429  if (retval != ERROR_OK)
2430  return retval;
2431 
2432  /* Don't cumulate sticky S_RESET_ST at the very first read of DHCSR
2433  * as S_RESET_ST may indicate a reset that happened long time ago
2434  * (most probably the power-on reset before OpenOCD was started).
2435  * As we are just initializing the debug system we do not need
2436  * to call cortex_m_endreset_event() in the following poll.
2437  */
2438  if (!cortex_m->dcb_dhcsr_sticky_is_recent) {
2439  cortex_m->dcb_dhcsr_sticky_is_recent = true;
2440  if (cortex_m->dcb_dhcsr & S_RESET_ST) {
2441  LOG_TARGET_DEBUG(target, "reset happened some time ago, ignore");
2442  cortex_m->dcb_dhcsr &= ~S_RESET_ST;
2443  }
2444  }
2445  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
2446 
2447  if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
2448  /* Enable debug requests */
2449  uint32_t dhcsr = (cortex_m->dcb_dhcsr | C_DEBUGEN) & ~(C_HALT | C_STEP | C_MASKINTS);
2450 
2451  retval = target_write_u32(target, DCB_DHCSR, DBGKEY | (dhcsr & 0x0000FFFFUL));
2452  if (retval != ERROR_OK)
2453  return retval;
2454  cortex_m->dcb_dhcsr = dhcsr;
2455  }
2456 
2457  /* Configure trace modules */
2458  retval = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
2459  if (retval != ERROR_OK)
2460  return retval;
2461 
2462  if (armv7m->trace_config.itm_deferred_config)
2464 
2465  /* NOTE: FPB and DWT are both optional. */
2466 
2467  /* Setup FPB */
2468  target_read_u32(target, FP_CTRL, &fpcr);
2469  /* bits [14:12] and [7:4] */
2470  cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
2471  cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
2472  /* Detect flash patch revision, see RM DDI 0403E.b page C1-817.
2473  Revision is zero base, fp_rev == 1 means Rev.2 ! */
2474  cortex_m->fp_rev = (fpcr >> 28) & 0xf;
2475  free(cortex_m->fp_comparator_list);
2476  cortex_m->fp_comparator_list = calloc(
2477  cortex_m->fp_num_code + cortex_m->fp_num_lit,
2478  sizeof(struct cortex_m_fp_comparator));
2479  cortex_m->fpb_enabled = fpcr & 1;
2480  for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
2481  cortex_m->fp_comparator_list[i].type =
2482  (i < cortex_m->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
2483  cortex_m->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
2484 
2485  /* make sure we clear any breakpoints enabled on the target */
2487  }
2488  LOG_TARGET_DEBUG(target, "FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i",
2489  fpcr,
2490  cortex_m->fp_num_code,
2491  cortex_m->fp_num_lit);
2492 
2493  /* Setup DWT */
2495  cortex_m_dwt_setup(cortex_m, target);
2496 
2497  /* These hardware breakpoints only work for code in flash! */
2498  LOG_TARGET_INFO(target, "target has %d breakpoints, %d watchpoints",
2499  cortex_m->fp_num_code,
2500  cortex_m->dwt_num_comp);
2501  }
2502 
2503  return ERROR_OK;
2504 }
2505 
2506 static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
2507 {
2508  struct armv7m_common *armv7m = target_to_armv7m(target);
2509  uint16_t dcrdr;
2510  uint8_t buf[2];
2511  int retval;
2512 
2513  retval = mem_ap_read_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
2514  if (retval != ERROR_OK)
2515  return retval;
2516 
2517  dcrdr = target_buffer_get_u16(target, buf);
2518  *ctrl = (uint8_t)dcrdr;
2519  *value = (uint8_t)(dcrdr >> 8);
2520 
2521  LOG_TARGET_DEBUG(target, "data 0x%x ctrl 0x%x", *value, *ctrl);
2522 
2523  /* write ack back to software dcc register
2524  * signify we have read data */
2525  if (dcrdr & (1 << 0)) {
2526  target_buffer_set_u16(target, buf, 0);
2527  retval = mem_ap_write_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
2528  if (retval != ERROR_OK)
2529  return retval;
2530  }
2531 
2532  return ERROR_OK;
2533 }
2534 
2536  uint32_t size, uint8_t *buffer)
2537 {
2538  uint8_t data;
2539  uint8_t ctrl;
2540  uint32_t i;
2541 
2542  for (i = 0; i < (size * 4); i++) {
2543  int retval = cortex_m_dcc_read(target, &data, &ctrl);
2544  if (retval != ERROR_OK)
2545  return retval;
2546  buffer[i] = data;
2547  }
2548 
2549  return ERROR_OK;
2550 }
2551 
2553 {
2554  struct target *target = priv;
2556  return ERROR_OK;
2557 
2558  if (!target->dbg_msg_enabled)
2559  return ERROR_OK;
2560 
2561  if (target->state == TARGET_RUNNING) {
2562  uint8_t data;
2563  uint8_t ctrl;
2564  int retval;
2565 
2566  retval = cortex_m_dcc_read(target, &data, &ctrl);
2567  if (retval != ERROR_OK)
2568  return retval;
2569 
2570  /* check if we have data */
2571  if (ctrl & (1 << 0)) {
2572  uint32_t request;
2573 
2574  /* we assume target is quick enough */
2575  request = data;
2576  for (int i = 1; i <= 3; i++) {
2577  retval = cortex_m_dcc_read(target, &data, &ctrl);
2578  if (retval != ERROR_OK)
2579  return retval;
2580  request |= ((uint32_t)data << (i * 8));
2581  }
2582  target_request(target, request);
2583  }
2584  }
2585 
2586  return ERROR_OK;
2587 }
2588 
2590  struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
2591 {
2592  struct armv7m_common *armv7m = &cortex_m->armv7m;
2593 
2594  armv7m_init_arch_info(target, armv7m);
2595 
2596  /* default reset mode is to use srst if fitted
2597  * if not it will use CORTEX_M3_RESET_VECTRESET */
2599 
2600  armv7m->arm.dap = dap;
2601 
2602  /* register arch-specific functions */
2604 
2605  armv7m->post_debug_entry = NULL;
2606 
2607  armv7m->pre_restore_context = NULL;
2608 
2611 
2614 
2615  return ERROR_OK;
2616 }
2617 
2618 static int cortex_m_target_create(struct target *target, Jim_Interp *interp)
2619 {
2620  struct adiv5_private_config *pc;
2621 
2622  pc = (struct adiv5_private_config *)target->private_config;
2623  if (adiv5_verify_config(pc) != ERROR_OK)
2624  return ERROR_FAIL;
2625 
2626  struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
2627  if (!cortex_m) {
2628  LOG_TARGET_ERROR(target, "No memory creating target");
2629  return ERROR_FAIL;
2630  }
2631 
2632  cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
2633  cortex_m->apsel = pc->ap_num;
2634 
2635  cortex_m_init_arch_info(target, cortex_m, pc->dap);
2636 
2637  return ERROR_OK;
2638 }
2639 
2640 /*--------------------------------------------------------------------------*/
2641 
2643  struct cortex_m_common *cm)
2644 {
2645  if (!is_cortex_m_with_dap_access(cm)) {
2646  command_print(cmd, "target is not a Cortex-M");
2647  return ERROR_TARGET_INVALID;
2648  }
2649  return ERROR_OK;
2650 }
2651 
2652 /*
2653  * Only stuff below this line should need to verify that its target
2654  * is a Cortex-M3. Everything else should have indirected through the
2655  * cortexm3_target structure, which is only used with CM3 targets.
2656  */
2657 
2658 COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
2659 {
2661  struct cortex_m_common *cortex_m = target_to_cm(target);
2662  struct armv7m_common *armv7m = &cortex_m->armv7m;
2663  uint32_t demcr = 0;
2664  int retval;
2665 
2666  static const struct {
2667  char name[10];
2668  unsigned mask;
2669  } vec_ids[] = {
2670  { "hard_err", VC_HARDERR, },
2671  { "int_err", VC_INTERR, },
2672  { "bus_err", VC_BUSERR, },
2673  { "state_err", VC_STATERR, },
2674  { "chk_err", VC_CHKERR, },
2675  { "nocp_err", VC_NOCPERR, },
2676  { "mm_err", VC_MMERR, },
2677  { "reset", VC_CORERESET, },
2678  };
2679 
2680  retval = cortex_m_verify_pointer(CMD, cortex_m);
2681  if (retval != ERROR_OK)
2682  return retval;
2683 
2684  if (!target_was_examined(target)) {
2685  LOG_TARGET_ERROR(target, "Target not examined yet");
2686  return ERROR_FAIL;
2687  }
2688 
2689  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
2690  if (retval != ERROR_OK)
2691  return retval;
2692 
2693  if (CMD_ARGC > 0) {
2694  unsigned catch = 0;
2695 
2696  if (CMD_ARGC == 1) {
2697  if (strcmp(CMD_ARGV[0], "all") == 0) {
2698  catch = VC_HARDERR | VC_INTERR | VC_BUSERR
2700  | VC_MMERR | VC_CORERESET;
2701  goto write;
2702  } else if (strcmp(CMD_ARGV[0], "none") == 0)
2703  goto write;
2704  }
2705  while (CMD_ARGC-- > 0) {
2706  unsigned i;
2707  for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
2708  if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
2709  continue;
2710  catch |= vec_ids[i].mask;
2711  break;
2712  }
2713  if (i == ARRAY_SIZE(vec_ids)) {
2714  LOG_TARGET_ERROR(target, "No CM3 vector '%s'", CMD_ARGV[CMD_ARGC]);
2716  }
2717  }
2718 write:
2719  /* For now, armv7m->demcr only stores vector catch flags. */
2720  armv7m->demcr = catch;
2721 
2722  demcr &= ~0xffff;
2723  demcr |= catch;
2724 
2725  /* write, but don't assume it stuck (why not??) */
2726  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, demcr);
2727  if (retval != ERROR_OK)
2728  return retval;
2729  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
2730  if (retval != ERROR_OK)
2731  return retval;
2732 
2733  /* FIXME be sure to clear DEMCR on clean server shutdown.
2734  * Otherwise the vector catch hardware could fire when there's
2735  * no debugger hooked up, causing much confusion...
2736  */
2737  }
2738 
2739  for (unsigned i = 0; i < ARRAY_SIZE(vec_ids); i++) {
2740  command_print(CMD, "%9s: %s", vec_ids[i].name,
2741  (demcr & vec_ids[i].mask) ? "catch" : "ignore");
2742  }
2743 
2744  return ERROR_OK;
2745 }
2746 
2747 COMMAND_HANDLER(handle_cortex_m_mask_interrupts_command)
2748 {
2750  struct cortex_m_common *cortex_m = target_to_cm(target);
2751  int retval;
2752 
2753  static const struct jim_nvp nvp_maskisr_modes[] = {
2754  { .name = "auto", .value = CORTEX_M_ISRMASK_AUTO },
2755  { .name = "off", .value = CORTEX_M_ISRMASK_OFF },
2756  { .name = "on", .value = CORTEX_M_ISRMASK_ON },
2757  { .name = "steponly", .value = CORTEX_M_ISRMASK_STEPONLY },
2758  { .name = NULL, .value = -1 },
2759  };
2760  const struct jim_nvp *n;
2761 
2762 
2763  retval = cortex_m_verify_pointer(CMD, cortex_m);
2764  if (retval != ERROR_OK)
2765  return retval;
2766 
2767  if (target->state != TARGET_HALTED) {
2768  command_print(CMD, "target must be stopped for \"%s\" command", CMD_NAME);
2769  return ERROR_OK;
2770  }
2771 
2772  if (CMD_ARGC > 0) {
2773  n = jim_nvp_name2value_simple(nvp_maskisr_modes, CMD_ARGV[0]);
2774  if (!n->name)
2776  cortex_m->isrmasking_mode = n->value;
2778  }
2779 
2780  n = jim_nvp_value2name_simple(nvp_maskisr_modes, cortex_m->isrmasking_mode);
2781  command_print(CMD, "cortex_m interrupt mask %s", n->name);
2782 
2783  return ERROR_OK;
2784 }
2785 
2786 COMMAND_HANDLER(handle_cortex_m_reset_config_command)
2787 {
2789  struct cortex_m_common *cortex_m = target_to_cm(target);
2790  int retval;
2791  char *reset_config;
2792 
2793  retval = cortex_m_verify_pointer(CMD, cortex_m);
2794  if (retval != ERROR_OK)
2795  return retval;
2796 
2797  if (CMD_ARGC > 0) {
2798  if (strcmp(*CMD_ARGV, "sysresetreq") == 0)
2800 
2801  else if (strcmp(*CMD_ARGV, "vectreset") == 0) {
2803  && !cortex_m->vectreset_supported)
2804  LOG_TARGET_WARNING(target, "VECTRESET is not supported on your Cortex-M core!");
2805  else
2807 
2808  } else
2810  }
2811 
2812  switch (cortex_m->soft_reset_config) {
2814  reset_config = "sysresetreq";
2815  break;
2816 
2818  reset_config = "vectreset";
2819  break;
2820 
2821  default:
2822  reset_config = "unknown";
2823  break;
2824  }
2825 
2826  command_print(CMD, "cortex_m reset_config %s", reset_config);
2827 
2828  return ERROR_OK;
2829 }
2830 
2831 static const struct command_registration cortex_m_exec_command_handlers[] = {
2832  {
2833  .name = "maskisr",
2834  .handler = handle_cortex_m_mask_interrupts_command,
2835  .mode = COMMAND_EXEC,
2836  .help = "mask cortex_m interrupts",
2837  .usage = "['auto'|'on'|'off'|'steponly']",
2838  },
2839  {
2840  .name = "vector_catch",
2841  .handler = handle_cortex_m_vector_catch_command,
2842  .mode = COMMAND_EXEC,
2843  .help = "configure hardware vectors to trigger debug entry",
2844  .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
2845  },
2846  {
2847  .name = "reset_config",
2848  .handler = handle_cortex_m_reset_config_command,
2849  .mode = COMMAND_ANY,
2850  .help = "configure software reset handling",
2851  .usage = "['sysresetreq'|'vectreset']",
2852  },
2854 };
2855 static const struct command_registration cortex_m_command_handlers[] = {
2856  {
2858  },
2859  {
2861  },
2862  /* START_DEPRECATED_TPIU */
2863  {
2865  },
2866  /* END_DEPRECATED_TPIU */
2867  {
2868  .name = "cortex_m",
2869  .mode = COMMAND_EXEC,
2870  .help = "Cortex-M command group",
2871  .usage = "",
2873  },
2874  {
2876  },
2878 };
2879 
2880 struct target_type cortexm_target = {
2881  .name = "cortex_m",
2882 
2883  .poll = cortex_m_poll,
2884  .arch_state = armv7m_arch_state,
2885 
2886  .target_request_data = cortex_m_target_request_data,
2887 
2888  .halt = cortex_m_halt,
2889  .resume = cortex_m_resume,
2890  .step = cortex_m_step,
2891 
2892  .assert_reset = cortex_m_assert_reset,
2893  .deassert_reset = cortex_m_deassert_reset,
2894  .soft_reset_halt = cortex_m_soft_reset_halt,
2895 
2896  .get_gdb_arch = arm_get_gdb_arch,
2897  .get_gdb_reg_list = armv7m_get_gdb_reg_list,
2898 
2899  .read_memory = cortex_m_read_memory,
2900  .write_memory = cortex_m_write_memory,
2901  .checksum_memory = armv7m_checksum_memory,
2902  .blank_check_memory = armv7m_blank_check_memory,
2903 
2904  .run_algorithm = armv7m_run_algorithm,
2905  .start_algorithm = armv7m_start_algorithm,
2906  .wait_algorithm = armv7m_wait_algorithm,
2907 
2908  .add_breakpoint = cortex_m_add_breakpoint,
2909  .remove_breakpoint = cortex_m_remove_breakpoint,
2910  .add_watchpoint = cortex_m_add_watchpoint,
2911  .remove_watchpoint = cortex_m_remove_watchpoint,
2912  .hit_watchpoint = cortex_m_hit_watchpoint,
2913 
2914  .commands = cortex_m_command_handlers,
2915  .target_create = cortex_m_target_create,
2916  .target_jim_configure = adiv5_jim_configure,
2917  .init_target = cortex_m_init_target,
2918  .examine = cortex_m_examine,
2919  .deinit_target = cortex_m_deinit_target,
2920 
2921  .profiling = cortex_m_profiling,
2922 };
uint8_t partno
Definition: ambiqmicro.c:99
@ ARM_ARCH_V6M
Definition: arm.h:56
@ ARM_ARCH_V8M
Definition: arm.h:58
@ ARM_ARCH_V7M
Definition: arm.h:57
@ ARM_MODE_HANDLER
Definition: arm.h:88
@ ARM_MODE_ANY
Definition: arm.h:98
@ ARM_MODE_USER_THREAD
Definition: arm.h:87
@ ARM_MODE_THREAD
Definition: arm.h:86
const char * arm_get_gdb_arch(struct target *target)
Definition: armv4_5.c:1189
const char * arm_mode_name(unsigned psr_mode)
Map PSR mode bits to the name of an ARM processor operating mode.
Definition: armv4_5.c:171
int mem_ap_read_buf(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:622
int mem_ap_read_buf_noincr(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:634
int adiv5_verify_config(struct adiv5_private_config *pc)
Definition: arm_adi_v5.c:2366
int mem_ap_read_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t *value)
Asynchronous (queued) read of a word from memory or a system register.
Definition: arm_adi_v5.c:230
int mem_ap_write_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Asynchronous (queued) write of a word to memory or a system register.
Definition: arm_adi_v5.c:282
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2330
int dap_find_get_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
Definition: arm_adi_v5.c:1009
int mem_ap_write_buf_noincr(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:640
int dap_dp_init_or_reconnect(struct adiv5_dap *dap)
Initialize a DAP or do reconnect if DAP is not accessible.
Definition: arm_adi_v5.c:752
int mem_ap_read_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t *value)
Synchronous read of a word from memory or a system register.
Definition: arm_adi_v5.c:259
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1091
int dap_put_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1111
int mem_ap_init(struct adiv5_ap *ap)
Initialize a DAP.
Definition: arm_adi_v5.c:783
int mem_ap_write_buf(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:628
int mem_ap_write_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Synchronous write of a word to memory or a system register.
Definition: arm_adi_v5.c:311
This defines formats and data structures used to talk to ADIv5 entities.
@ AP_TYPE_AHB3_AP
Definition: arm_adi_v5.h:449
@ AP_TYPE_AHB5_AP
Definition: arm_adi_v5.h:452
#define DP_APSEL_INVALID
Definition: arm_adi_v5.h:106
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
Definition: arm_adi_v5.h:604
Macros used to generate various ARM or Thumb opcodes.
#define ARMV5_T_BKPT(im)
Definition: arm_opcodes.h:291
int arm_semihosting(struct target *target, int *retval)
Checks for and processes an ARM semihosting request.
int arm_semihosting_init(struct target *target)
Initialize ARM semihosting support.
const struct command_registration arm_tpiu_deprecated_command_handlers[]
const char * name
Definition: armv4_5.c:76
int armv7m_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Returns generic ARM userspace registers to GDB.
Definition: armv7m.c:456
int armv7m_maybe_skip_bkpt_inst(struct target *target, bool *inst_found)
Definition: armv7m.c:1062
void armv7m_free_reg_cache(struct target *target)
Definition: armv7m.c:823
const int armv7m_psp_reg_map[ARMV7M_NUM_CORE_REGS]
Definition: armv7m.c:51
uint32_t armv7m_map_id_to_regsel(unsigned int arm_reg_id)
Definition: armv7m.c:243
struct reg_cache * armv7m_build_reg_cache(struct target *target)
Builds cache of architecturally defined registers.
Definition: armv7m.c:760
const int armv7m_msp_reg_map[ARMV7M_NUM_CORE_REGS]
Definition: armv7m.c:60
int armv7m_wait_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t exit_point, int timeout_ms, void *arch_info)
Waits for an algorithm in the target.
Definition: armv7m.c:619
int armv7m_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Generates a CRC32 checksum of a memory region.
Definition: armv7m.c:879
bool armv7m_map_reg_packing(unsigned int arm_reg_id, unsigned int *reg32_id, uint32_t *offset)
Definition: armv7m.c:281
int armv7m_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Checks an array of memory regions whether they are erased.
Definition: armv7m.c:930
int armv7m_arch_state(struct target *target)
Logs summary of ARMv7-M state for a halted target.
Definition: armv7m.c:725
int armv7m_restore_context(struct target *target)
Restores target context using the cache of core registers set up by armv7m_build_reg_cache(),...
Definition: armv7m.c:168
const char * armv7m_exception_string(int number)
Maps ISR number (from xPSR) to name.
Definition: armv7m.c:201
int armv7m_start_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, void *arch_info)
Starts a Thumb algorithm in the target.
Definition: armv7m.c:505
int armv7m_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, int timeout_ms, void *arch_info)
Runs a Thumb algorithm in the target.
Definition: armv7m.c:480
const struct command_registration armv7m_command_handlers[]
Definition: armv7m.c:1096
int armv7m_init_arch_info(struct target *target, struct armv7m_common *armv7m)
Sets up target as a generic ARMv7-M core.
Definition: armv7m.c:857
@ ARMV7M_PRIMASK
Definition: armv7m.h:144
@ ARMV7M_FPU_LAST_REG
Definition: armv7m.h:204
@ ARMV7M_CORE_FIRST_REG
Definition: armv7m.h:201
@ ARMV7M_CONTROL
Definition: armv7m.h:147
@ ARMV8M_LAST_REG
Definition: armv7m.h:206
@ ARMV7M_FPU_FIRST_REG
Definition: armv7m.h:203
@ ARMV8M_FIRST_REG
Definition: armv7m.h:205
@ ARMV7M_LAST_REG
Definition: armv7m.h:200
static struct armv7m_common * target_to_armv7m(struct target *target)
Definition: armv7m.h:260
@ FPV4_SP
Definition: armv7m.h:211
@ FPV5_DP
Definition: armv7m.h:213
@ FPV5_SP
Definition: armv7m.h:212
@ FP_NONE
Definition: armv7m.h:210
const struct command_registration armv7m_trace_command_handlers[]
Definition: armv7m_trace.c:140
int armv7m_trace_itm_config(struct target *target)
Configure hardware accordingly to the current ITM target settings.
Definition: armv7m_trace.c:18
void * buf_cpy(const void *from, void *_to, unsigned size)
Copies size bits out of from and into to.
Definition: binarybuffer.c:43
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:98
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:30
void breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:329
int breakpoint_add(struct target *target, target_addr_t address, uint32_t length, enum breakpoint_type type)
Definition: breakpoints.c:204
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:382
breakpoint_type
Definition: breakpoints.h:17
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:79
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:63
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:160
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int cortex_m_debug_entry(struct target *target)
Definition: cortex_m.c:776
static void cortex_m_dwt_free(struct target *target)
Definition: cortex_m.c:2251
static int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:1824
COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
Definition: cortex_m.c:2658
static int cortex_m_set_maskints_for_run(struct target *target)
Definition: cortex_m.c:464
#define MVFR1_DEFAULT_M7_DP
Definition: cortex_m.c:2298
#define MVFR0_DEFAULT_M7_SP
Definition: cortex_m.c:2295
static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp, struct adiv5_ap **debug_ap)
Definition: cortex_m.c:2300
#define MVFR0_DEFAULT_M4
Definition: cortex_m.c:2292
static int cortex_m_dwt_get_reg(struct reg *reg)
Definition: cortex_m.c:2097
static int cortex_m_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Definition: cortex_m.c:1099
static int cortex_m_target_create(struct target *target, Jim_Interp *interp)
Definition: cortex_m.c:2618
static int cortex_m_enable_fpb(struct target *target)
Definition: cortex_m.c:556
static int cortex_m_single_step_core(struct target *target)
Definition: cortex_m.c:531
static int cortex_m_write_debug_halt_mask(struct target *target, uint32_t mask_on, uint32_t mask_off)
Definition: cortex_m.c:417
#define MVFR1_DEFAULT_M7_SP
Definition: cortex_m.c:2297
static int cortex_m_set_maskints_for_halt(struct target *target)
Definition: cortex_m.c:440
#define MVFR1_DEFAULT_M4
Definition: cortex_m.c:2293
struct target_type cortexm_target
Definition: cortex_m.c:2880
static int cortex_m_clear_halt(struct target *target)
Definition: cortex_m.c:508
#define DHCSR_S_REGRDY_TIMEOUT
Definition: cortex_m.c:46
static const struct command_registration cortex_m_command_handlers[]
Definition: cortex_m.c:2855
static int cortex_m_set_maskints_for_step(struct target *target)
Definition: cortex_m.c:486
static int cortex_m_poll(struct target *target)
Definition: cortex_m.c:874
void cortex_m_enable_watchpoints(struct target *target)
Definition: cortex_m.c:1954
static int cortex_m_target_request_data(struct target *target, uint32_t size, uint8_t *buffer)
Definition: cortex_m.c:2535
static int cortex_m_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: cortex_m.c:1994
static int cortex_m_examine_exception_reason(struct target *target)
Definition: cortex_m.c:700
static int cortex_m_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
Definition: cortex_m.c:1204
int cortex_m_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:1857
static int cortex_m_store_core_reg_u32(struct target *target, uint32_t num, uint32_t value)
Definition: cortex_m.c:367
static int cortex_m_slow_read_all_regs(struct target *target)
Definition: cortex_m.c:201
static int cortex_m_init_arch_info(struct target *target, struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
Definition: cortex_m.c:2589
static int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:1741
void cortex_m_enable_breakpoints(struct target *target)
Definition: cortex_m.c:1087
int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1733
static int cortex_m_endreset_event(struct target *target)
Definition: cortex_m.c:574
static int cortex_m_examine_debug_reason(struct target *target)
Definition: cortex_m.c:674
static int cortex_m_handle_target_request(void *priv)
Definition: cortex_m.c:2552
static int cortex_m_fast_read_all_regs(struct target *target)
Definition: cortex_m.c:243
static int cortex_m_deassert_reset(struct target *target)
Definition: cortex_m.c:1565
#define MVFR1
Definition: cortex_m.c:2290
static const struct reg_arch_type dwt_reg_type
Definition: cortex_m.c:2157
static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
Definition: cortex_m.c:2162
static int cortex_m_load_core_reg_u32(struct target *target, uint32_t regsel, uint32_t *value)
Definition: cortex_m.c:146
int cortex_m_examine(struct target *target)
Definition: cortex_m.c:2309
int cortex_m_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:1906
static int cortex_m_assert_reset(struct target *target)
Definition: cortex_m.c:1395
int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1718
static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Definition: cortex_m.c:1925
static const struct cortex_m_part_info cortex_m_parts[]
Definition: cortex_m.c:49
static int cortex_m_verify_pointer(struct command_invocation *cmd, struct cortex_m_common *cm)
Definition: cortex_m.c:2642
static const struct command_registration cortex_m_exec_command_handlers[]
Definition: cortex_m.c:2831
static int cortex_m_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: cortex_m.c:1980
static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
Definition: cortex_m.c:2506
int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1592
static int cortex_m_read_dhcsr_atomic_sticky(struct target *target)
Read DCB DHCSR register to cortex_m->dcb_dhcsr and cumulate sticky bits in cortex_m->dcb_dhcsr_cumula...
Definition: cortex_m.c:132
static int cortex_m_set_maskints(struct target *target, bool mask)
Definition: cortex_m.c:431
static void cortex_m_cumulate_dhcsr_sticky(struct cortex_m_common *cortex_m, uint32_t dhcsr)
DCB DHCSR register contains S_RETIRE_ST and S_RESET_ST bits cleared on a read.
Definition: cortex_m.c:123
static bool cortex_m_has_tz(struct target *target)
Definition: cortex_m.c:2273
static int cortex_m_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: cortex_m.c:1966
static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
Definition: cortex_m.c:2179
void cortex_m_deinit_target(struct target *target)
Definition: cortex_m.c:2002
int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1677
#define MVFR0_DEFAULT_M7_DP
Definition: cortex_m.c:2296
#define MVFR0
Definition: cortex_m.c:2289
static int cortex_m_soft_reset_halt(struct target *target)
Definition: cortex_m.c:1022
int cortex_m_profiling(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: cortex_m.c:2019
static const struct dwt_reg dwt_comp[]
Definition: cortex_m.c:2133
static int cortex_m_halt(struct target *target)
Definition: cortex_m.c:984
#define DWT_COMPARATOR(i)
static const struct dwt_reg dwt_base_regs[]
Definition: cortex_m.c:2124
static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel, uint32_t *reg_value, uint32_t *dhcsr)
Definition: cortex_m.c:226
static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
Definition: cortex_m.c:2110
#define DWT_CYCCNT
Definition: cortex_m.h:77
#define FPCR_REPLACE_BKPT_HIGH
Definition: cortex_m.h:175
#define DSCSR_CDS
Definition: cortex_m.h:138
#define NVIC_HFSR
Definition: cortex_m.h:150
#define VC_CHKERR
Definition: cortex_m.h:132
#define DWT_PCSR
Definition: cortex_m.h:78
#define S_REGRDY
Definition: cortex_m.h:119
#define ARM_CPUID_PARTNO_POS
Definition: cortex_m.h:34
#define DFSR_EXTERNAL
Definition: cortex_m.h:169
#define DBGKEY
Definition: cortex_m.h:114
#define S_LOCKUP
Definition: cortex_m.h:122
#define FP_CTRL
Definition: cortex_m.h:86
#define FPCR_REPLACE_BKPT_LOW
Definition: cortex_m.h:174
#define NVIC_SFAR
Definition: cortex_m.h:155
#define DFSR_BKPT
Definition: cortex_m.h:166
#define CORTEX_M_F_HAS_FPV4
Definition: cortex_m.h:53
#define AIRCR_VECTKEY
Definition: cortex_m.h:158
#define S_RETIRE_ST
Definition: cortex_m.h:123
#define DCB_DSCSR
Definition: cortex_m.h:69
#define CORTEX_M_F_HAS_FPV5
Definition: cortex_m.h:54
#define NVIC_SFSR
Definition: cortex_m.h:154
#define CORTEX_M_COMMON_MAGIC
Definition: cortex_m.h:20
#define AIRCR_SYSRESETREQ
Definition: cortex_m.h:159
#define S_RESET_ST
Definition: cortex_m.h:124
#define C_MASKINTS
Definition: cortex_m.h:118
#define FPCR_LITERAL
Definition: cortex_m.h:172
#define NVIC_CFSR
Definition: cortex_m.h:146
cortex_m_partno
Definition: cortex_m.h:37
@ CORTEX_M7_PARTNO
Definition: cortex_m.h:44
@ CORTEX_M35P_PARTNO
Definition: cortex_m.h:48
@ CORTEX_M4_PARTNO
Definition: cortex_m.h:43
@ STAR_MC1_PARTNO
Definition: cortex_m.h:39
@ CORTEX_M33_PARTNO
Definition: cortex_m.h:47
@ CORTEX_M1_PARTNO
Definition: cortex_m.h:41
@ CORTEX_M0_PARTNO
Definition: cortex_m.h:40
@ CORTEX_M0P_PARTNO
Definition: cortex_m.h:45
@ CORTEX_M55_PARTNO
Definition: cortex_m.h:49
@ CORTEX_M23_PARTNO
Definition: cortex_m.h:46
@ CORTEX_M3_PARTNO
Definition: cortex_m.h:42
#define CPUID
Definition: cortex_m.h:32
#define DFSR_DWTTRAP
Definition: cortex_m.h:167
#define C_HALT
Definition: cortex_m.h:116
#define VC_NOCPERR
Definition: cortex_m.h:133
#define DCB_DCRSR
Definition: cortex_m.h:66
#define VC_INTERR
Definition: cortex_m.h:129
static bool is_cortex_m_with_dap_access(const struct cortex_m_common *cortex_m)
Definition: cortex_m.h:252
#define DWT_CTRL
Definition: cortex_m.h:76
#define VC_BUSERR
Definition: cortex_m.h:130
#define VC_CORERESET
Definition: cortex_m.h:135
#define DWT_COMP0
Definition: cortex_m.h:79
#define DCRSR_WNR
Definition: cortex_m.h:74
#define NVIC_MMFAR
Definition: cortex_m.h:152
#define DCB_DEMCR
Definition: cortex_m.h:68
#define C_DEBUGEN
Definition: cortex_m.h:115
#define DAUTHSTATUS_SID_MASK
Definition: cortex_m.h:72
#define DCB_DCRDR
Definition: cortex_m.h:67
#define NVIC_BFAR
Definition: cortex_m.h:153
#define ARM_CPUID_PARTNO_MASK
Definition: cortex_m.h:35
#define C_STEP
Definition: cortex_m.h:117
#define FPCR_CODE
Definition: cortex_m.h:171
#define DWT_DEVARCH
Definition: cortex_m.h:82
static struct cortex_m_common * target_to_cm(struct target *target)
Definition: cortex_m.h:267
#define S_HALT
Definition: cortex_m.h:120
#define S_SLEEP
Definition: cortex_m.h:121
#define AIRCR_VECTRESET
Definition: cortex_m.h:161
#define NVIC_DFSR
Definition: cortex_m.h:151
@ CORTEX_M_ISRMASK_OFF
Definition: cortex_m.h:200
@ CORTEX_M_ISRMASK_ON
Definition: cortex_m.h:201
@ CORTEX_M_ISRMASK_STEPONLY
Definition: cortex_m.h:202
@ CORTEX_M_ISRMASK_AUTO
Definition: cortex_m.h:199
#define VC_STATERR
Definition: cortex_m.h:131
#define NVIC_AIRCR
Definition: cortex_m.h:144
#define DFSR_VCATCH
Definition: cortex_m.h:168
#define FP_COMP0
Definition: cortex_m.h:88
#define DCB_DHCSR
Definition: cortex_m.h:65
#define TRCENA
Definition: cortex_m.h:127
#define CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K
Definition: cortex_m.h:55
#define DWT_DEVARCH_ARMV8M
Definition: cortex_m.h:84
#define VC_HARDERR
Definition: cortex_m.h:128
cortex_m_soft_reset_config
Definition: cortex_m.h:193
@ CORTEX_M_RESET_VECTRESET
Definition: cortex_m.h:195
@ CORTEX_M_RESET_SYSRESETREQ
Definition: cortex_m.h:194
#define DAUTHSTATUS
Definition: cortex_m.h:71
#define NVIC_SHCSR
Definition: cortex_m.h:145
#define VC_MMERR
Definition: cortex_m.h:134
int mask
Definition: esirisc.c:1698
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
struct jim_nvp * jim_nvp_name2value_simple(const struct jim_nvp *p, const char *name)
Definition: jim-nvp.c:45
struct jim_nvp * jim_nvp_value2name_simple(const struct jim_nvp *p, int value)
Definition: jim-nvp.c:123
static enum reset_types jtag_reset_config
Definition: jtag/core.c:87
int jtag_get_srst(void)
Definition: jtag/core.c:1747
int adapter_deassert_reset(void)
Definition: jtag/core.c:1900
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1062
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1734
int adapter_assert_reset(void)
Definition: jtag/core.c:1880
reset_types
Definition: jtag.h:212
@ RESET_SRST_NO_GATING
Definition: jtag.h:221
@ RESET_HAS_SRST
Definition: jtag.h:215
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:217
void alive_sleep(uint64_t ms)
Definition: log.c:460
void keep_alive(void)
Definition: log.c:419
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:143
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:146
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define ERROR_FAIL
Definition: log.h:161
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:149
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:140
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define ERROR_TIMEOUT_REACHED
Definition: log.h:164
#define ERROR_OK
Definition: log.h:155
struct reg_cache ** register_get_last_cache_p(struct reg_cache **first)
Definition: register.c:72
void register_unlink_cache(struct reg_cache **cache_p, const struct reg_cache *cache)
Definition: register.c:85
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
int gettimeofday(struct timeval *tv, struct timezone *tz)
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
const struct command_registration rtt_target_command_handlers[]
Definition: rtt/tcl.c:295
#define BIT(nr)
Definition: stm32l4x.h:18
This represents an ARM Debug Interface (v5) Access Port (AP).
Definition: arm_adi_v5.h:243
uint32_t tar_autoincr_block
Definition: arm_adi_v5.h:282
struct adiv5_dap * dap
DAP this AP belongs to.
Definition: arm_adi_v5.h:247
uint32_t memaccess_tck
Configures how many extra tck clocks are added after starting a MEM-AP access before we try to read i...
Definition: arm_adi_v5.h:279
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
Definition: arm_adi_v5.h:320
struct adiv5_dap * dap
Definition: arm_adi_v5.h:743
Represents a generic ARM core, with standard application registers.
Definition: arm.h:167
enum arm_arch arch
ARM architecture version.
Definition: arm.h:194
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:188
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:176
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
Definition: arm.h:239
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:173
const int * map
Support for arm_reg_current()
Definition: arm.h:182
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:216
struct reg_cache * core_cache
Definition: arm.h:170
struct armv7m_trace_config trace_config
Definition: armv7m.h:236
bool is_hla_target
Definition: armv7m.h:234
int exception_number
Definition: armv7m.h:225
int fp_feature
Definition: armv7m.h:230
void(* pre_restore_context)(struct target *target)
Definition: armv7m.h:245
struct arm arm
Definition: armv7m.h:223
int(* store_core_reg_u32)(struct target *target, uint32_t regsel, uint32_t value)
Definition: armv7m.h:240
int(* load_core_reg_u32)(struct target *target, uint32_t regsel, uint32_t *value)
Definition: armv7m.h:239
uint32_t demcr
Definition: armv7m.h:231
struct adiv5_ap * debug_ap
Definition: armv7m.h:228
int(* examine_debug_reason)(struct target *target)
Definition: armv7m.h:242
int(* post_debug_entry)(struct target *target)
Definition: armv7m.h:243
bool itm_deferred_config
Config ITM after target examine.
Definition: armv7m_trace.h:39
struct breakpoint * next
Definition: breakpoints.h:34
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
uint32_t unique_id
Definition: breakpoints.h:35
bool is_set
Definition: breakpoints.h:31
unsigned int number
Definition: breakpoints.h:32
target_addr_t address
Definition: breakpoints.h:27
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:229
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:243
const struct cortex_m_part_info * core_info
Definition: cortex_m.h:236
enum cortex_m_soft_reset_config soft_reset_config
Definition: cortex_m.h:232
struct armv7m_common armv7m
Definition: cortex_m.h:208
uint64_t apsel
Definition: cortex_m.h:240
unsigned int dwt_comp_available
Definition: cortex_m.h:227
unsigned int dwt_num_comp
Definition: cortex_m.h:226
uint32_t dcb_dhcsr
Definition: cortex_m.h:211
bool fpb_enabled
Definition: cortex_m.h:222
struct cortex_m_dwt_comparator * dwt_comparator_list
Definition: cortex_m.h:229
bool slow_register_read
Definition: cortex_m.h:238
bool dcb_dhcsr_sticky_is_recent
Definition: cortex_m.h:214
struct cortex_m_fp_comparator * fp_comparator_list
Definition: cortex_m.h:223
struct reg_cache * dwt_cache
Definition: cortex_m.h:230
unsigned int fp_num_lit
Definition: cortex_m.h:219
bool vectreset_supported
Definition: cortex_m.h:233
uint32_t dwt_devarch
Definition: cortex_m.h:228
uint32_t nvic_dfsr
Definition: cortex_m.h:215
unsigned int fp_num_code
Definition: cortex_m.h:220
bool maskints_erratum
Definition: cortex_m.h:244
enum cortex_m_isrmasking_mode isrmasking_mode
Definition: cortex_m.h:234
uint32_t nvic_icsr
Definition: cortex_m.h:216
unsigned int common_magic
Definition: cortex_m.h:206
uint32_t dcb_dhcsr_cumulated_sticky
Definition: cortex_m.h:212
uint32_t dwt_comparator_address
Definition: cortex_m.h:190
enum arm_arch arch
Definition: cortex_m.h:60
const char * name
Definition: cortex_m.h:59
enum cortex_m_partno partno
Definition: cortex_m.h:58
uint32_t flags
Definition: cortex_m.h:61
uint8_t value[4]
Definition: cortex_m.c:2094
struct target * target
Definition: cortex_m.c:2092
uint32_t addr
Definition: cortex_m.c:2093
const char * name
Definition: cortex_m.c:2120
unsigned size
Definition: cortex_m.c:2121
uint32_t addr
Definition: cortex_m.c:2119
Name Value Pairs, aka: NVP.
Definition: jim-nvp.h:59
const char * name
Definition: jim-nvp.h:60
int value
Definition: jim-nvp.h:61
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
Definition: register.h:111
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:120
enum target_debug_reason debug_reason
Definition: target.h:159
enum target_state state
Definition: target.h:162
void * private_config
Definition: target.h:170
struct reg_cache * reg_cache
Definition: target.h:163
struct breakpoint * breakpoints
Definition: target.h:164
struct watchpoint * watchpoints
Definition: target.h:165
uint32_t dbg_msg_enabled
Definition: target.h:168
bool reset_halt
Definition: target.h:149
bool defer_examine
Should we defer examine to later.
Definition: target.h:128
Definition: psoc6.c:84
enum watchpoint_rw rw
Definition: breakpoints.h:44
uint32_t mask
Definition: breakpoints.h:42
bool is_set
Definition: breakpoints.h:45
struct watchpoint * next
Definition: breakpoints.h:47
int unique_id
Definition: breakpoints.h:48
uint32_t value
Definition: breakpoints.h:43
unsigned int number
Definition: breakpoints.h:46
uint32_t length
Definition: breakpoints.h:41
target_addr_t address
Definition: breakpoints.h:40
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1833
void target_free_all_working_areas(struct target *target)
Definition: target.c:2219
const char * target_state_name(struct target *t)
Return the name of this targets current state.
Definition: target.c:302
int target_halt(struct target *target)
Definition: target.c:585
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:429
bool target_has_event_action(struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:5287
int target_profiling_default(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target.c:2356
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1334
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2707
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:750
int target_poll(struct target *target)
Definition: target.c:555
static int srst_asserted
Definition: target.c:2916
int target_register_timer_callback(int(*callback)(void *priv), unsigned int time_ms, enum target_timer_type type, void *priv)
The period is very approximate, the callback can happen much more often or much more rarely than spec...
Definition: target.c:1727
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2616
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:393
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1306
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:536
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:5092
int target_resume(struct target *target, int current, target_addr_t address, int handle_breakpoints, int debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:634
@ DBG_REASON_WPTANDBKPT
Definition: target.h:76
@ DBG_REASON_UNDEFINED
Definition: target.h:81
@ DBG_REASON_NOTHALTED
Definition: target.h:78
@ DBG_REASON_DBGRQ
Definition: target.h:73
@ DBG_REASON_SINGLESTEP
Definition: target.h:77
@ DBG_REASON_WATCHPOINT
Definition: target.h:75
@ DBG_REASON_BREAKPOINT
Definition: target.h:74
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:792
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:794
#define ERROR_TARGET_INVALID
Definition: target.h:789
@ TARGET_TIMER_TYPE_PERIODIC
Definition: target.h:328
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:273
@ TARGET_EVENT_HALTED
Definition: target.h:253
@ TARGET_EVENT_RESUMED
Definition: target.h:254
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:272
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:265
target_state
Definition: target.h:52
@ TARGET_RESET
Definition: target.h:56
@ TARGET_DEBUG_RUNNING
Definition: target.h:57
@ TARGET_UNKNOWN
Definition: target.h:53
@ TARGET_HALTED
Definition: target.h:55
@ TARGET_RUNNING
Definition: target.h:54
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:796
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:445
static bool target_was_examined(struct target *target)
Definition: target.h:438
#define ERROR_TARGET_FAILURE
Definition: target.h:793
int target_request(struct target *target, uint32_t request)
int timeval_compare(const struct timeval *x, const struct timeval *y)
Definition: time_support.c:55
int timeval_add_time(struct timeval *result, long sec, long usec)
Definition: time_support.c:41
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:342
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
uint64_t target_addr_t
Definition: types.h:335
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22
static const struct @118 vec_ids[]