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 "armv7m_cache.h"
25 #include "target_request.h"
26 #include "target_type.h"
27 #include "arm_adi_v5.h"
28 #include "arm_disassembler.h"
29 #include "register.h"
30 #include "arm_opcodes.h"
31 #include "arm_semihosting.h"
32 #include "smp.h"
33 #include <helper/nvp.h>
34 #include <helper/string_choices.h>
35 #include <helper/time_support.h>
36 #include <rtt/rtt.h>
37 
38 /* NOTE: most of this should work fine for the Cortex-M1 and
39  * Cortex-M0 cores too, although they're ARMv6-M not ARMv7-M.
40  * Some differences: M0/M1 doesn't have FPB remapping or the
41  * DWT tracing/profiling support. (So the cycle counter will
42  * not be usable; the other stuff isn't currently used here.)
43  *
44  * Although there are some workarounds for errata seen only in r0p0
45  * silicon, such old parts are hard to find and thus not much tested
46  * any longer.
47  */
48 
49 /* Timeout for register r/w */
50 #define DHCSR_S_REGRDY_TIMEOUT (500)
51 
52 /* Supported Cortex-M Cores */
53 static const struct cortex_m_part_info cortex_m_parts[] = {
54  {
56  .name = "Cortex-M0",
57  .arch = ARM_ARCH_V6M,
58  },
59  {
60  .impl_part = CORTEX_M0P_PARTNO,
61  .name = "Cortex-M0+",
62  .arch = ARM_ARCH_V6M,
63  },
64  {
65  .impl_part = CORTEX_M1_PARTNO,
66  .name = "Cortex-M1",
67  .arch = ARM_ARCH_V6M,
68  },
69  {
70  .impl_part = CORTEX_M3_PARTNO,
71  .name = "Cortex-M3",
72  .arch = ARM_ARCH_V7M,
74  },
75  {
76  .impl_part = CORTEX_M4_PARTNO,
77  .name = "Cortex-M4",
78  .arch = ARM_ARCH_V7M,
80  },
81  {
82  .impl_part = CORTEX_M7_PARTNO,
83  .name = "Cortex-M7",
84  .arch = ARM_ARCH_V7M,
85  .flags = CORTEX_M_F_HAS_FPV5,
86  },
87  {
88  .impl_part = CORTEX_M23_PARTNO,
89  .name = "Cortex-M23",
90  .arch = ARM_ARCH_V8M,
91  },
92  {
93  .impl_part = CORTEX_M33_PARTNO,
94  .name = "Cortex-M33",
95  .arch = ARM_ARCH_V8M,
96  .flags = CORTEX_M_F_HAS_FPV5,
97  },
98  {
99  .impl_part = CORTEX_M35P_PARTNO,
100  .name = "Cortex-M35P",
101  .arch = ARM_ARCH_V8M,
102  .flags = CORTEX_M_F_HAS_FPV5,
103  },
104  {
105  .impl_part = CORTEX_M52_PARTNO,
106  .name = "Cortex-M52",
107  .arch = ARM_ARCH_V8M,
108  .flags = CORTEX_M_F_HAS_FPV5,
109  },
110  {
111  .impl_part = CORTEX_M55_PARTNO,
112  .name = "Cortex-M55",
113  .arch = ARM_ARCH_V8M,
114  .flags = CORTEX_M_F_HAS_FPV5,
115  },
116  {
117  .impl_part = CORTEX_M85_PARTNO,
118  .name = "Cortex-M85",
119  .arch = ARM_ARCH_V8M,
120  .flags = CORTEX_M_F_HAS_FPV5,
121  },
122  {
123  .impl_part = STAR_MC1_PARTNO,
124  .name = "STAR-MC1",
125  .arch = ARM_ARCH_V8M,
126  .flags = CORTEX_M_F_HAS_FPV5,
127  },
128  {
129  .impl_part = INFINEON_SLX2_PARTNO,
130  .name = "Infineon-SLx2",
131  .arch = ARM_ARCH_V8M,
132  },
133  {
134  .impl_part = REALTEK_M200_PARTNO,
135  .name = "Real-M200 (KM0)",
136  .arch = ARM_ARCH_V8M,
137  },
138  {
139  .impl_part = REALTEK_M300_PARTNO,
140  .name = "Real-M300 (KM4)",
141  .arch = ARM_ARCH_V8M,
142  .flags = CORTEX_M_F_HAS_FPV5,
143  },
144 };
145 
146 /* forward declarations */
147 static int cortex_m_store_core_reg_u32(struct target *target,
148  uint32_t num, uint32_t value);
149 static void cortex_m_dwt_free(struct target *target);
150 
155 static inline void cortex_m_cumulate_dhcsr_sticky(struct cortex_m_common *cortex_m,
156  uint32_t dhcsr)
157 {
158  cortex_m->dcb_dhcsr_cumulated_sticky |= dhcsr;
159 }
160 
165 {
166  struct cortex_m_common *cortex_m = target_to_cm(target);
167  struct armv7m_common *armv7m = target_to_armv7m(target);
168 
169  int retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
170  &cortex_m->dcb_dhcsr);
171  if (retval != ERROR_OK)
172  return retval;
173 
174  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
175  return ERROR_OK;
176 }
177 
179  uint32_t regsel, uint32_t *value)
180 {
181  struct cortex_m_common *cortex_m = target_to_cm(target);
182  struct armv7m_common *armv7m = target_to_armv7m(target);
183  int retval;
184  uint32_t dcrdr, tmp_value;
185  int64_t then;
186 
187  /* because the DCB_DCRDR is used for the emulated dcc channel
188  * we have to save/restore the DCB_DCRDR when used */
189  if (target->dbg_msg_enabled) {
190  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
191  if (retval != ERROR_OK)
192  return retval;
193  }
194 
195  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
196  if (retval != ERROR_OK)
197  return retval;
198 
199  /* check if value from register is ready and pre-read it */
200  then = timeval_ms();
201  while (1) {
202  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR,
203  &cortex_m->dcb_dhcsr);
204  if (retval != ERROR_OK)
205  return retval;
206  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR,
207  &tmp_value);
208  if (retval != ERROR_OK)
209  return retval;
210  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
211  if (cortex_m->dcb_dhcsr & S_REGRDY)
212  break;
213  cortex_m->slow_register_read = true; /* Polling (still) needed. */
214  if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
215  LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
216  return ERROR_TIMEOUT_REACHED;
217  }
218  keep_alive();
219  }
220 
221  *value = tmp_value;
222 
223  if (target->dbg_msg_enabled) {
224  /* restore DCB_DCRDR - this needs to be in a separate
225  * transaction otherwise the emulated DCC channel breaks */
226  if (retval == ERROR_OK)
227  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
228  }
229 
230  return retval;
231 }
232 
234 {
235  struct cortex_m_common *cortex_m = target_to_cm(target);
236  struct armv7m_common *armv7m = target_to_armv7m(target);
237  const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
238 
239  /* Opportunistically restore fast read, it'll revert to slow
240  * if any register needed polling in cortex_m_load_core_reg_u32(). */
241  cortex_m->slow_register_read = false;
242 
243  for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) {
244  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
245  if (r->exist) {
246  int retval = armv7m->arm.read_core_reg(target, r, reg_id, ARM_MODE_ANY);
247  if (retval != ERROR_OK)
248  return retval;
249  }
250  }
251 
252  if (!cortex_m->slow_register_read)
253  LOG_TARGET_DEBUG(target, "Switching back to fast register reads");
254 
255  return ERROR_OK;
256 }
257 
258 static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel,
259  uint32_t *reg_value, uint32_t *dhcsr)
260 {
261  struct armv7m_common *armv7m = target_to_armv7m(target);
262  int retval;
263 
264  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
265  if (retval != ERROR_OK)
266  return retval;
267 
268  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR, dhcsr);
269  if (retval != ERROR_OK)
270  return retval;
271 
272  return mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, reg_value);
273 }
274 
276 {
277  struct cortex_m_common *cortex_m = target_to_cm(target);
278  struct armv7m_common *armv7m = target_to_armv7m(target);
279  int retval;
280  uint32_t dcrdr;
281 
282  /* because the DCB_DCRDR is used for the emulated dcc channel
283  * we have to save/restore the DCB_DCRDR when used */
284  bool dbg_msg_enabled = target->dbg_msg_enabled;
285  if (dbg_msg_enabled) {
286  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
287  if (retval != ERROR_OK)
288  return retval;
289  }
290 
291  const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
292  const unsigned int n_r32 = ARMV7M_LAST_REG - ARMV7M_CORE_FIRST_REG + 1
294  /* we need one 32-bit word for each register except FP D0..D15, which
295  * need two words */
296  uint32_t r_vals[n_r32];
297  uint32_t dhcsr[n_r32];
298 
299  unsigned int wi = 0; /* write index to r_vals and dhcsr arrays */
300  unsigned int reg_id; /* register index in the reg_list, ARMV7M_R0... */
301  for (reg_id = 0; reg_id < num_regs; reg_id++) {
302  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
303  if (!r->exist)
304  continue; /* skip non existent registers */
305 
306  if (r->size <= 8) {
307  /* Any 8-bit or shorter register is unpacked from a 32-bit
308  * container register. Skip it now. */
309  continue;
310  }
311 
312  uint32_t regsel = armv7m_map_id_to_regsel(reg_id);
313  retval = cortex_m_queue_reg_read(target, regsel, &r_vals[wi],
314  &dhcsr[wi]);
315  if (retval != ERROR_OK)
316  return retval;
317  wi++;
318 
319  assert(r->size == 32 || r->size == 64);
320  if (r->size == 32)
321  continue; /* done with 32-bit register */
322 
323  assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
324  /* the odd part of FP register (S1, S3...) */
325  retval = cortex_m_queue_reg_read(target, regsel + 1, &r_vals[wi],
326  &dhcsr[wi]);
327  if (retval != ERROR_OK)
328  return retval;
329  wi++;
330  }
331 
332  assert(wi <= n_r32);
333 
334  retval = dap_run(armv7m->debug_ap->dap);
335  if (retval != ERROR_OK)
336  return retval;
337 
338  if (dbg_msg_enabled) {
339  /* restore DCB_DCRDR - this needs to be in a separate
340  * transaction otherwise the emulated DCC channel breaks */
341  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
342  if (retval != ERROR_OK)
343  return retval;
344  }
345 
346  bool not_ready = false;
347  for (unsigned int i = 0; i < wi; i++) {
348  if ((dhcsr[i] & S_REGRDY) == 0) {
349  not_ready = true;
350  LOG_TARGET_DEBUG(target, "Register %u was not ready during fast read", i);
351  }
352  cortex_m_cumulate_dhcsr_sticky(cortex_m, dhcsr[i]);
353  }
354 
355  if (not_ready) {
356  /* Any register was not ready,
357  * fall back to slow read with S_REGRDY polling */
358  return ERROR_TIMEOUT_REACHED;
359  }
360 
361  LOG_TARGET_DEBUG(target, "read %u 32-bit registers", wi);
362 
363  unsigned int ri = 0; /* read index from r_vals array */
364  for (reg_id = 0; reg_id < num_regs; reg_id++) {
365  struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
366  if (!r->exist)
367  continue; /* skip non existent registers */
368 
369  r->dirty = false;
370 
371  unsigned int reg32_id;
372  uint32_t offset;
373  if (armv7m_map_reg_packing(reg_id, &reg32_id, &offset)) {
374  /* Unpack a partial register from 32-bit container register */
375  struct reg *r32 = &armv7m->arm.core_cache->reg_list[reg32_id];
376 
377  /* The container register ought to precede all regs unpacked
378  * from it in the reg_list. So the value should be ready
379  * to unpack */
380  assert(r32->valid);
381  buf_cpy(r32->value + offset, r->value, r->size);
382 
383  } else {
384  assert(r->size == 32 || r->size == 64);
385  buf_set_u32(r->value, 0, 32, r_vals[ri++]);
386 
387  if (r->size == 64) {
388  assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
389  /* the odd part of FP register (S1, S3...) */
390  buf_set_u32(r->value + 4, 0, 32, r_vals[ri++]);
391  }
392  }
393  r->valid = true;
394  }
395  assert(ri == wi);
396 
397  return retval;
398 }
399 
401  uint32_t regsel, uint32_t value)
402 {
403  struct cortex_m_common *cortex_m = target_to_cm(target);
404  struct armv7m_common *armv7m = target_to_armv7m(target);
405  int retval;
406  uint32_t dcrdr;
407  int64_t then;
408 
409  /* because the DCB_DCRDR is used for the emulated dcc channel
410  * we have to save/restore the DCB_DCRDR when used */
411  if (target->dbg_msg_enabled) {
412  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
413  if (retval != ERROR_OK)
414  return retval;
415  }
416 
417  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, value);
418  if (retval != ERROR_OK)
419  return retval;
420 
421  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
422  if (retval != ERROR_OK)
423  return retval;
424 
425  /* check if value is written into register */
426  then = timeval_ms();
427  while (1) {
429  if (retval != ERROR_OK)
430  return retval;
431  if (cortex_m->dcb_dhcsr & S_REGRDY)
432  break;
433  if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
434  LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
435  return ERROR_TIMEOUT_REACHED;
436  }
437  keep_alive();
438  }
439 
440  if (target->dbg_msg_enabled) {
441  /* restore DCB_DCRDR - this needs to be in a separate
442  * transaction otherwise the emulated DCC channel breaks */
443  if (retval == ERROR_OK)
444  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
445  }
446 
447  return retval;
448 }
449 
451  uint32_t mask_on, uint32_t mask_off)
452 {
453  struct cortex_m_common *cortex_m = target_to_cm(target);
454  struct armv7m_common *armv7m = &cortex_m->armv7m;
455 
456  /* mask off status bits */
457  cortex_m->dcb_dhcsr &= ~((0xFFFFul << 16) | mask_off);
458  /* create new register mask */
459  cortex_m->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
460 
461  return mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR, cortex_m->dcb_dhcsr);
462 }
463 
464 static int cortex_m_set_maskints(struct target *target, bool mask)
465 {
466  struct cortex_m_common *cortex_m = target_to_cm(target);
467  if (!!(cortex_m->dcb_dhcsr & C_MASKINTS) != mask)
469  else
470  return ERROR_OK;
471 }
472 
474 {
475  struct cortex_m_common *cortex_m = target_to_cm(target);
476  switch (cortex_m->isrmasking_mode) {
478  /* interrupts taken at resume, whether for step or run -> no mask */
479  return cortex_m_set_maskints(target, false);
480 
482  /* interrupts never masked */
483  return cortex_m_set_maskints(target, false);
484 
485  case CORTEX_M_ISRMASK_ON:
486  /* interrupts always masked */
487  return cortex_m_set_maskints(target, true);
488 
490  /* interrupts masked for single step only -> mask now if MASKINTS
491  * erratum, otherwise only mask before stepping */
492  return cortex_m_set_maskints(target, cortex_m->maskints_erratum);
493  }
494  return ERROR_OK;
495 }
496 
498 {
499  switch (target_to_cm(target)->isrmasking_mode) {
501  /* interrupts taken at resume, whether for step or run -> no mask */
502  return cortex_m_set_maskints(target, false);
503 
505  /* interrupts never masked */
506  return cortex_m_set_maskints(target, false);
507 
508  case CORTEX_M_ISRMASK_ON:
509  /* interrupts always masked */
510  return cortex_m_set_maskints(target, true);
511 
513  /* interrupts masked for single step only -> no mask */
514  return cortex_m_set_maskints(target, false);
515  }
516  return ERROR_OK;
517 }
518 
520 {
521  switch (target_to_cm(target)->isrmasking_mode) {
523  /* the auto-interrupt should already be done -> mask */
524  return cortex_m_set_maskints(target, true);
525 
527  /* interrupts never masked */
528  return cortex_m_set_maskints(target, false);
529 
530  case CORTEX_M_ISRMASK_ON:
531  /* interrupts always masked */
532  return cortex_m_set_maskints(target, true);
533 
535  /* interrupts masked for single step only -> mask */
536  return cortex_m_set_maskints(target, true);
537  }
538  return ERROR_OK;
539 }
540 
541 static int cortex_m_clear_halt(struct target *target)
542 {
543  struct cortex_m_common *cortex_m = target_to_cm(target);
544  struct armv7m_common *armv7m = &cortex_m->armv7m;
545  int retval;
546 
547  /* clear step if any */
549 
550  /* Read Debug Fault Status Register */
551  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR, &cortex_m->nvic_dfsr);
552  if (retval != ERROR_OK)
553  return retval;
554 
555  /* Clear Debug Fault Status */
556  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_DFSR, cortex_m->nvic_dfsr);
557  if (retval != ERROR_OK)
558  return retval;
559  LOG_TARGET_DEBUG(target, "NVIC_DFSR 0x%" PRIx32, cortex_m->nvic_dfsr);
560 
561  return ERROR_OK;
562 }
563 
565 {
566  struct cortex_m_common *cortex_m = target_to_cm(target);
567  int retval;
568 
569  /* Mask interrupts before clearing halt, if not done already. This avoids
570  * Erratum 377497 (fixed in r1p0) where setting MASKINTS while clearing
571  * HALT can put the core into an unknown state.
572  */
573  if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) {
575  if (retval != ERROR_OK)
576  return retval;
577  }
579  if (retval != ERROR_OK)
580  return retval;
581  LOG_TARGET_DEBUG(target, "single step");
582 
583  /* restore dhcsr reg */
585 
586  return ERROR_OK;
587 }
588 
589 static int cortex_m_enable_fpb(struct target *target)
590 {
591  int retval = target_write_u32(target, FP_CTRL, 3);
592  if (retval != ERROR_OK)
593  return retval;
594 
595  /* check the fpb is actually enabled */
596  uint32_t fpctrl;
597  retval = target_read_u32(target, FP_CTRL, &fpctrl);
598  if (retval != ERROR_OK)
599  return retval;
600 
601  if (fpctrl & 1)
602  return ERROR_OK;
603 
604  return ERROR_FAIL;
605 }
606 
608 {
609  int retval;
610  uint32_t dcb_demcr;
611  struct cortex_m_common *cortex_m = target_to_cm(target);
612  struct armv7m_common *armv7m = &cortex_m->armv7m;
613  struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
614  struct cortex_m_fp_comparator *fp_list = cortex_m->fp_comparator_list;
615  struct cortex_m_dwt_comparator *dwt_list = cortex_m->dwt_comparator_list;
616 
617  /* REVISIT The four debug monitor bits are currently ignored... */
618  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &dcb_demcr);
619  if (retval != ERROR_OK)
620  return retval;
621  LOG_TARGET_DEBUG(target, "DCB_DEMCR = 0x%8.8" PRIx32, dcb_demcr);
622 
623  /* this register is used for emulated dcc channel */
624  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
625  if (retval != ERROR_OK)
626  return retval;
627 
629  if (retval != ERROR_OK)
630  return retval;
631 
632  if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
633  /* Enable debug requests */
635  if (retval != ERROR_OK)
636  return retval;
637  }
638 
639  /* Restore proper interrupt masking setting for running CPU. */
641 
642  /* Enable features controlled by ITM and DWT blocks, and catch only
643  * the vectors we were told to pay attention to.
644  *
645  * Target firmware is responsible for all fault handling policy
646  * choices *EXCEPT* explicitly scripted overrides like "vector_catch"
647  * or manual updates to the NVIC SHCSR and CCR registers.
648  */
649  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, TRCENA | armv7m->demcr);
650  if (retval != ERROR_OK)
651  return retval;
652 
653  /* Paranoia: evidently some (early?) chips don't preserve all the
654  * debug state (including FPB, DWT, etc) across reset...
655  */
656 
657  /* Enable FPB */
658  retval = cortex_m_enable_fpb(target);
659  if (retval != ERROR_OK) {
660  LOG_TARGET_ERROR(target, "Failed to enable the FPB");
661  return retval;
662  }
663 
664  cortex_m->fpb_enabled = true;
665 
666  /* Restore FPB registers */
667  for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
668  retval = target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
669  if (retval != ERROR_OK)
670  return retval;
671  }
672 
673  /* Restore DWT registers */
674  for (unsigned int i = 0; i < cortex_m->dwt_num_comp; i++) {
675  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
676  dwt_list[i].comp);
677  if (retval != ERROR_OK)
678  return retval;
679  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
680  dwt_list[i].mask);
681  if (retval != ERROR_OK)
682  return retval;
683  retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
684  dwt_list[i].function);
685  if (retval != ERROR_OK)
686  return retval;
687  }
688  retval = dap_run(swjdp);
689  if (retval != ERROR_OK)
690  return retval;
691 
693 
694  /* TODO: invalidate also working areas (needed in the case of detected reset).
695  * Doing so will require flash drivers to test if working area
696  * is still valid in all target algo calling loops.
697  */
698 
699  /* make sure we have latest dhcsr flags */
701  if (retval != ERROR_OK)
702  return retval;
703 
704  return retval;
705 }
706 
708 {
709  struct cortex_m_common *cortex_m = target_to_cm(target);
710 
711  /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason
712  * only check the debug reason if we don't know it already */
713 
716  if (cortex_m->nvic_dfsr & DFSR_BKPT) {
718  if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
720  } else if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
722  else if (cortex_m->nvic_dfsr & DFSR_VCATCH)
724  else if (cortex_m->nvic_dfsr & DFSR_EXTERNAL)
726  else /* HALTED */
728  }
729 
730  return ERROR_OK;
731 }
732 
734 {
735  uint32_t shcsr = 0, except_sr = 0, cfsr = -1, except_ar = -1;
736  struct armv7m_common *armv7m = target_to_armv7m(target);
737  struct adiv5_dap *swjdp = armv7m->arm.dap;
738  int retval;
739 
740  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SHCSR, &shcsr);
741  if (retval != ERROR_OK)
742  return retval;
743  switch (armv7m->exception_number) {
744  case 2: /* NMI */
745  break;
746  case 3: /* Hard Fault */
747  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_HFSR, &except_sr);
748  if (retval != ERROR_OK)
749  return retval;
750  if (except_sr & 0x40000000) {
751  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &cfsr);
752  if (retval != ERROR_OK)
753  return retval;
754  }
755  break;
756  case 4: /* Memory Management */
757  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
758  if (retval != ERROR_OK)
759  return retval;
760  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_MMFAR, &except_ar);
761  if (retval != ERROR_OK)
762  return retval;
763  break;
764  case 5: /* Bus Fault */
765  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
766  if (retval != ERROR_OK)
767  return retval;
768  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_BFAR, &except_ar);
769  if (retval != ERROR_OK)
770  return retval;
771  break;
772  case 6: /* Usage Fault */
773  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
774  if (retval != ERROR_OK)
775  return retval;
776  break;
777  case 7: /* Secure Fault */
778  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFSR, &except_sr);
779  if (retval != ERROR_OK)
780  return retval;
781  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFAR, &except_ar);
782  if (retval != ERROR_OK)
783  return retval;
784  break;
785  case 11: /* SVCall */
786  break;
787  case 12: /* Debug Monitor */
788  retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_DFSR, &except_sr);
789  if (retval != ERROR_OK)
790  return retval;
791  break;
792  case 14: /* PendSV */
793  break;
794  case 15: /* SysTick */
795  break;
796  default:
797  except_sr = 0;
798  break;
799  }
800  retval = dap_run(swjdp);
801  if (retval == ERROR_OK)
802  LOG_TARGET_DEBUG(target, "%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32
803  ", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32,
805  shcsr, except_sr, cfsr, except_ar);
806  return retval;
807 }
808 
809 /* Errata 3092511 workaround
810  * Cortex-M7 can halt in an incorrect address when breakpoint
811  * and exception occurs simultaneously */
813 {
814  struct cortex_m_common *cortex_m = target_to_cm(target);
815  struct armv7m_common *armv7m = &cortex_m->armv7m;
816  struct arm *arm = &armv7m->arm;
817 
818  uint32_t pc = buf_get_u32(arm->pc->value, 0, 32);
819 
820  /* To reduce the workaround processing cost we assume FPB is in sync
821  * with OpenOCD breakpoints. If the target app writes to FPB
822  * OpenOCD will resume after the break set by app */
823  struct breakpoint *bkpt = breakpoint_find(target, pc);
824  if (bkpt) {
825  LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint confirmed");
826  return ERROR_OK;
827  }
828  if (pc >= 0xe0000000u)
829  /* not executable area, do not read instruction @ pc */
830  return ERROR_OK;
831 
832  uint16_t insn;
833  int retval = target_read_u16(target, pc, &insn);
834  if (retval != ERROR_OK)
835  return ERROR_OK; /* do not propagate the error, just avoid workaround */
836 
837  if ((insn & 0xff00) == (ARMV5_T_BKPT(0) & 0xff00)) {
838  LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint embedded in code confirmed");
839  return ERROR_OK;
840  }
841  LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint not found, proceed with resume");
843 }
844 
845 static int cortex_m_debug_entry(struct target *target)
846 {
847  uint32_t xpsr;
848  int retval;
849  struct cortex_m_common *cortex_m = target_to_cm(target);
850  struct armv7m_common *armv7m = &cortex_m->armv7m;
851  struct arm *arm = &armv7m->arm;
852  struct reg *r;
853 
854  LOG_TARGET_DEBUG(target, " ");
855 
856  /* Do this really early to minimize the window where the MASKINTS erratum
857  * can pile up pending interrupts. */
859 
861 
863  if (retval != ERROR_OK)
864  return retval;
865 
866  retval = armv7m->examine_debug_reason(target);
867  if (retval != ERROR_OK)
868  return retval;
869 
870  /* examine PE security state */
871  uint32_t dscsr = 0;
872  if (armv7m->arm.arch == ARM_ARCH_V8M) {
873  retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DSCSR, &dscsr);
874  if (retval != ERROR_OK)
875  return retval;
876  }
877 
878  // read caches state
880  if (retval != ERROR_OK)
881  return retval;
882 
883  uint32_t ccr = 0;
884  if (armv7m->armv7m_cache.info_valid) {
885  retval = mem_ap_read_u32(armv7m->debug_ap, CCR, &ccr);
886  if (retval != ERROR_OK)
887  return retval;
888  }
889 
890  /* Load all registers to arm.core_cache */
891  if (!cortex_m->slow_register_read) {
893  if (retval == ERROR_TIMEOUT_REACHED) {
894  cortex_m->slow_register_read = true;
895  LOG_TARGET_DEBUG(target, "Switched to slow register read");
896  }
897  }
898 
899  if (cortex_m->slow_register_read)
901 
902  if (retval != ERROR_OK)
903  return retval;
904 
905  r = arm->cpsr;
906  xpsr = buf_get_u32(r->value, 0, 32);
907 
908  /* Are we in an exception handler */
909  if (xpsr & 0x1FF) {
910  armv7m->exception_number = (xpsr & 0x1FF);
911 
914  } else {
915  unsigned int control = buf_get_u32(arm->core_cache
916  ->reg_list[ARMV7M_CONTROL].value, 0, 3);
917 
918  /* is this thread privileged? */
919  arm->core_mode = control & 1
921  : ARM_MODE_THREAD;
922 
923  /* which stack is it using? */
924  if (control & 2)
926  else
928 
929  armv7m->exception_number = 0;
930  }
931 
932  if (armv7m->exception_number)
934 
935  bool secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
936  LOG_TARGET_DEBUG(target, "entered debug state in core mode: %s at PC 0x%" PRIx32
937  ", cpu in %s state, target->state: %s",
939  buf_get_u32(arm->pc->value, 0, 32),
940  secure_state ? "Secure" : "Non-Secure",
942 
943  if (armv7m->armv7m_cache.info_valid)
944  LOG_TARGET_DEBUG(target, "D-Cache %s, I-Cache %s",
947 
948  /* Errata 3092511 workaround
949  * Cortex-M7 can halt in an incorrect address when breakpoint
950  * and exception occurs simultaneously */
951  if (cortex_m->incorrect_halt_erratum
952  && armv7m->exception_number
953  && cortex_m->nvic_dfsr == (DFSR_BKPT | DFSR_HALTED)) {
955  if (retval != ERROR_OK)
956  return retval;
957  }
958 
959  if (armv7m->post_debug_entry) {
960  retval = armv7m->post_debug_entry(target);
961  if (retval != ERROR_OK)
962  return retval;
963  }
964 
965  return ERROR_OK;
966 }
967 
968 static int cortex_m_poll_one(struct target *target)
969 {
970  int detected_failure = ERROR_OK;
971  int retval = ERROR_OK;
972  enum target_state prev_target_state = target->state;
973  struct cortex_m_common *cortex_m = target_to_cm(target);
974  struct armv7m_common *armv7m = &cortex_m->armv7m;
975 
976  /* Read from Debug Halting Control and Status Register */
978  if (retval != ERROR_OK) {
980  return retval;
981  }
982 
983  /* Recover from lockup. See ARMv7-M architecture spec,
984  * section B1.5.15 "Unrecoverable exception cases".
985  */
986  if (cortex_m->dcb_dhcsr & S_LOCKUP) {
987  LOG_TARGET_ERROR(target, "clearing lockup after double fault");
990 
991  /* We have to execute the rest (the "finally" equivalent, but
992  * still throw this exception again).
993  */
994  detected_failure = ERROR_FAIL;
995 
996  /* refresh status bits */
998  if (retval != ERROR_OK)
999  return retval;
1000  }
1001 
1002  if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
1003  cortex_m->dcb_dhcsr_cumulated_sticky &= ~S_RESET_ST;
1004  if (target->state != TARGET_RESET) {
1006  LOG_TARGET_INFO(target, "external reset detected");
1007  /* In case of an unexpected S_RESET_ST set TARGET_RESET state
1008  * and keep it until the next poll to allow its detection */
1009  return ERROR_OK;
1010  }
1011 
1012  /* refresh status bits */
1014  if (retval != ERROR_OK)
1015  return retval;
1016 
1017  /* If still under reset, quit and re-check at next poll */
1018  if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
1019  cortex_m->dcb_dhcsr_cumulated_sticky &= ~S_RESET_ST;
1020  return ERROR_OK;
1021  }
1022 
1023  /* S_RESET_ST was expected (in a reset command). Continue processing
1024  * to quickly get out of TARGET_RESET state */
1025  } else {
1027  if (retval != ERROR_OK)
1028  return retval;
1029  }
1030 
1031  if (target->state == TARGET_RESET) {
1032  /* Cannot switch context while running so endreset is
1033  * called with target->state == TARGET_RESET
1034  */
1035  LOG_TARGET_DEBUG(target, "Exit from reset with dcb_dhcsr 0x%" PRIx32,
1036  cortex_m->dcb_dhcsr);
1037  retval = cortex_m_endreset_event(target);
1038  if (retval != ERROR_OK) {
1040  return retval;
1041  }
1043  prev_target_state = TARGET_RUNNING;
1044  }
1045 
1046  if (cortex_m->dcb_dhcsr & S_HALT) {
1048 
1049  if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET)) {
1050  retval = cortex_m_debug_entry(target);
1051 
1052  /* Errata 3092511 workaround
1053  * Cortex-M7 can halt in an incorrect address when breakpoint
1054  * and exception occurs simultaneously */
1055  if (retval == ERROR_TARGET_HALTED_DO_RESUME) {
1056  struct arm *arm = &armv7m->arm;
1057  LOG_TARGET_INFO(target, "Resuming after incorrect halt @ PC 0x%08" PRIx32
1058  ", ARM Cortex-M7 erratum 3092511",
1059  buf_get_u32(arm->pc->value, 0, 32));
1060  /* We don't need to restore registers, just restart the core */
1063  if (retval != ERROR_OK)
1064  return retval;
1065 
1067  /* registers are now invalid */
1069 
1071  return ERROR_OK;
1072  }
1073 
1074  /* arm_semihosting needs to know registers, don't run if debug entry returned error */
1075  if (retval == ERROR_OK && arm_semihosting(target, &retval) != 0)
1076  return retval;
1077 
1078  if (target->smp) {
1079  LOG_TARGET_DEBUG(target, "postpone target event 'halted'");
1081  } else {
1082  /* regardless of errors returned in previous code update state */
1084  }
1085  }
1086  if (prev_target_state == TARGET_DEBUG_RUNNING) {
1087  retval = cortex_m_debug_entry(target);
1088 
1090  }
1091  if (retval != ERROR_OK)
1092  return retval;
1093  }
1094 
1095  if (target->state == TARGET_UNKNOWN) {
1096  /* Check if processor is retiring instructions or sleeping.
1097  * Unlike S_RESET_ST here we test if the target *is* running now,
1098  * not if it has been running (possibly in the past). Instructions are
1099  * typically processed much faster than OpenOCD polls DHCSR so S_RETIRE_ST
1100  * is read always 1. That's the reason not to use dcb_dhcsr_cumulated_sticky.
1101  */
1102  if (cortex_m->dcb_dhcsr & S_RETIRE_ST || cortex_m->dcb_dhcsr & S_SLEEP) {
1104  retval = ERROR_OK;
1105  }
1106  }
1107 
1108  /* Check that target is truly halted, since the target could be resumed externally */
1109  if ((prev_target_state == TARGET_HALTED) && !(cortex_m->dcb_dhcsr & S_HALT)) {
1110  /* registers are now invalid */
1112 
1114  LOG_TARGET_WARNING(target, "external resume detected");
1116  retval = ERROR_OK;
1117  }
1118 
1119  /* Did we detect a failure condition that we cleared? */
1120  if (detected_failure != ERROR_OK)
1121  retval = detected_failure;
1122  return retval;
1123 }
1124 
1125 static int cortex_m_halt_one(struct target *target);
1126 
1127 static int cortex_m_smp_halt_all(struct list_head *smp_targets)
1128 {
1129  int retval = ERROR_OK;
1130  struct target_list *head;
1131 
1132  foreach_smp_target(head, smp_targets) {
1133  struct target *curr = head->target;
1134  if (!target_was_examined(curr))
1135  continue;
1136  if (curr->state == TARGET_HALTED)
1137  continue;
1138 
1139  int ret2 = cortex_m_halt_one(curr);
1140  if (retval == ERROR_OK)
1141  retval = ret2; /* store the first error code ignore others */
1142  }
1143  return retval;
1144 }
1145 
1147 {
1148  int retval = ERROR_OK;
1149  struct target_list *head;
1150 
1151  foreach_smp_target(head, smp_targets) {
1152  struct target *curr = head->target;
1153  if (!target_was_examined(curr))
1154  continue;
1155  /* skip targets that were already halted */
1156  if (curr->state == TARGET_HALTED)
1157  continue;
1158 
1159  int ret2 = cortex_m_poll_one(curr);
1160  if (retval == ERROR_OK)
1161  retval = ret2; /* store the first error code ignore others */
1162  }
1163  return retval;
1164 }
1165 
1167 {
1168  int retval = ERROR_OK;
1169  struct target_list *head;
1170  bool halted = false;
1171 
1172  foreach_smp_target(head, smp_targets) {
1173  struct target *curr = head->target;
1174  if (curr->smp_halt_event_postponed) {
1175  halted = true;
1176  break;
1177  }
1178  }
1179 
1180  if (halted) {
1182 
1184  if (retval == ERROR_OK)
1185  retval = ret2; /* store the first error code ignore others */
1186 
1188  struct target *curr = head->target;
1189  if (!curr->smp_halt_event_postponed)
1190  continue;
1191 
1192  curr->smp_halt_event_postponed = false;
1193  if (curr->state == TARGET_HALTED) {
1194  LOG_TARGET_DEBUG(curr, "sending postponed target event 'halted'");
1196  }
1197  }
1198  /* There is no need to set gdb_service->target
1199  * as hwthread_update_threads() selects an interesting thread
1200  * by its own
1201  */
1202  }
1203  return retval;
1204 }
1205 
1206 static int cortex_m_poll(struct target *target)
1207 {
1208  int retval = cortex_m_poll_one(target);
1209 
1210  if (target->smp) {
1211  struct target_list *last;
1212  last = list_last_entry(target->smp_targets, struct target_list, lh);
1213  if (target == last->target)
1214  /* After the last target in SMP group has been polled
1215  * check for postponed halted events and eventually halt and re-poll
1216  * other targets */
1218  }
1219  return retval;
1220 }
1221 
1222 static int cortex_m_halt_one(struct target *target)
1223 {
1224  int retval;
1225  LOG_TARGET_DEBUG(target, "target->state: %s", target_state_name(target));
1226 
1227  if (!target_was_examined(target)) {
1228  LOG_TARGET_ERROR(target, "target non examined yet");
1230  }
1231 
1232  if (target->state == TARGET_HALTED) {
1233  LOG_TARGET_DEBUG(target, "target was already halted");
1234  return ERROR_OK;
1235  }
1236 
1237  if (target->state == TARGET_UNKNOWN)
1238  LOG_TARGET_WARNING(target, "target was in unknown state when halt was requested");
1239 
1240  /* Write to Debug Halting Control and Status Register */
1242 
1243  /* Do this really early to minimize the window where the MASKINTS erratum
1244  * can pile up pending interrupts. */
1246 
1248 
1249  return retval;
1250 }
1251 
1252 static int cortex_m_halt(struct target *target)
1253 {
1254  if (target->smp)
1256  else
1257  return cortex_m_halt_one(target);
1258 }
1259 
1261 {
1262  struct cortex_m_common *cortex_m = target_to_cm(target);
1263  struct armv7m_common *armv7m = &cortex_m->armv7m;
1264  int retval, timeout = 0;
1265 
1266  /* on single cortex_m MCU soft_reset_halt should be avoided as same functionality
1267  * can be obtained by using 'reset halt' and 'cortex_m reset_config vectreset'.
1268  * As this reset only uses VC_CORERESET it would only ever reset the cortex_m
1269  * core, not the peripherals */
1270  LOG_TARGET_DEBUG(target, "soft_reset_halt is discouraged, please use 'reset halt' instead.");
1271 
1272  if (!cortex_m->vectreset_supported) {
1273  LOG_TARGET_ERROR(target, "VECTRESET is not supported on this Cortex-M core");
1274  return ERROR_FAIL;
1275  }
1276 
1277  /* Set C_DEBUGEN */
1279  if (retval != ERROR_OK)
1280  return retval;
1281 
1282  /* Enter debug state on reset; restore DEMCR in endreset_event() */
1283  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
1285  if (retval != ERROR_OK)
1286  return retval;
1287 
1288  /* Request a core-only reset */
1289  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1291  if (retval != ERROR_OK)
1292  return retval;
1294 
1295  /* registers are now invalid */
1297 
1298  while (timeout < 100) {
1300  if (retval == ERROR_OK) {
1301  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR,
1302  &cortex_m->nvic_dfsr);
1303  if (retval != ERROR_OK)
1304  return retval;
1305  if ((cortex_m->dcb_dhcsr & S_HALT)
1306  && (cortex_m->nvic_dfsr & DFSR_VCATCH)) {
1307  LOG_TARGET_DEBUG(target, "system reset-halted, DHCSR 0x%08" PRIx32 ", DFSR 0x%08" PRIx32,
1308  cortex_m->dcb_dhcsr, cortex_m->nvic_dfsr);
1310  /* FIXME restore user's vector catch config */
1311  return ERROR_OK;
1312  } else {
1313  LOG_TARGET_DEBUG(target, "waiting for system reset-halt, "
1314  "DHCSR 0x%08" PRIx32 ", %d ms",
1315  cortex_m->dcb_dhcsr, timeout);
1316  }
1317  }
1318  timeout++;
1319  alive_sleep(1);
1320  }
1321 
1322  return ERROR_OK;
1323 }
1324 
1326 {
1328 
1329  /* set any pending breakpoints */
1330  while (breakpoint) {
1331  if (!breakpoint->is_set)
1334  }
1335 }
1336 
1337 static int cortex_m_restore_one(struct target *target, bool current,
1338  target_addr_t *address, bool handle_breakpoints, bool debug_execution)
1339 {
1340  struct armv7m_common *armv7m = target_to_armv7m(target);
1341  struct breakpoint *breakpoint = NULL;
1342  uint32_t resume_pc;
1343  struct reg *r;
1344 
1345  if (target->state != TARGET_HALTED) {
1346  LOG_TARGET_ERROR(target, "not halted");
1347  return ERROR_TARGET_NOT_HALTED;
1348  }
1349 
1350  if (!debug_execution) {
1354  }
1355 
1356  if (debug_execution) {
1357  r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
1358 
1359  /* Disable interrupts */
1360  /* We disable interrupts in the PRIMASK register instead of
1361  * masking with C_MASKINTS. This is probably the same issue
1362  * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
1363  * in parallel with disabled interrupts can cause local faults
1364  * to not be taken.
1365  *
1366  * This breaks non-debug (application) execution if not
1367  * called from armv7m_start_algorithm() which saves registers.
1368  */
1369  buf_set_u32(r->value, 0, 1, 1);
1370  r->dirty = true;
1371  r->valid = true;
1372 
1373  /* Make sure we are in Thumb mode, set xPSR.T bit */
1374  /* armv7m_start_algorithm() initializes entire xPSR register.
1375  * This duplicity handles the case when cortex_m_resume()
1376  * is used with the debug_execution flag directly,
1377  * not called through armv7m_start_algorithm().
1378  */
1379  r = armv7m->arm.cpsr;
1380  buf_set_u32(r->value, 24, 1, 1);
1381  r->dirty = true;
1382  r->valid = true;
1383  }
1384 
1385  /* current = true: continue on current pc, otherwise continue at <address> */
1386  r = armv7m->arm.pc;
1387  if (!current) {
1388  buf_set_u32(r->value, 0, 32, *address);
1389  r->dirty = true;
1390  r->valid = true;
1391  }
1392 
1393  /* if we halted last time due to a bkpt instruction
1394  * then we have to manually step over it, otherwise
1395  * the core will break again */
1396 
1397  if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
1398  && !debug_execution)
1400 
1401  resume_pc = buf_get_u32(r->value, 0, 32);
1402  if (current)
1403  *address = resume_pc;
1404 
1405  int retval = armv7m_restore_context(target);
1406  if (retval != ERROR_OK)
1407  return retval;
1408 
1409  /* the front-end may request us not to handle breakpoints */
1410  if (handle_breakpoints) {
1411  /* Single step past breakpoint at current address */
1412  breakpoint = breakpoint_find(target, resume_pc);
1413  if (breakpoint) {
1414  LOG_TARGET_DEBUG(target, "unset breakpoint at " TARGET_ADDR_FMT " (ID: %" PRIu32 ")",
1418  if (retval == ERROR_OK)
1421  if (retval != ERROR_OK)
1422  return retval;
1423  if (ret2 != ERROR_OK)
1424  return ret2;
1425  }
1426  }
1427 
1428  return ERROR_OK;
1429 }
1430 
1431 static int cortex_m_restart_one(struct target *target, bool debug_execution)
1432 {
1433  struct armv7m_common *armv7m = target_to_armv7m(target);
1434 
1435  /* Restart core */
1438 
1440  /* registers are now invalid */
1442 
1443  if (!debug_execution) {
1446  } else {
1449  }
1450 
1451  return ERROR_OK;
1452 }
1453 
1454 static int cortex_m_restore_smp(struct target *target, bool handle_breakpoints)
1455 {
1456  struct target_list *head;
1459  struct target *curr = head->target;
1460  /* skip calling target */
1461  if (curr == target)
1462  continue;
1463  if (!target_was_examined(curr))
1464  continue;
1465  /* skip running targets */
1466  if (curr->state == TARGET_RUNNING)
1467  continue;
1468 
1469  int retval = cortex_m_restore_one(curr, true, &address,
1470  handle_breakpoints, false);
1471  if (retval != ERROR_OK)
1472  return retval;
1473 
1474  retval = cortex_m_restart_one(curr, false);
1475  if (retval != ERROR_OK)
1476  return retval;
1477 
1478  LOG_TARGET_DEBUG(curr, "SMP resumed at " TARGET_ADDR_FMT, address);
1479  }
1480  return ERROR_OK;
1481 }
1482 
1483 static int cortex_m_resume(struct target *target, bool current,
1484  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1485 {
1486  int retval = cortex_m_restore_one(target, current, &address,
1487  handle_breakpoints, debug_execution);
1488  if (retval != ERROR_OK) {
1489  LOG_TARGET_ERROR(target, "context restore failed, aborting resume");
1490  return retval;
1491  }
1492 
1493  if (target->smp && !debug_execution) {
1494  retval = cortex_m_restore_smp(target, handle_breakpoints);
1495  if (retval != ERROR_OK)
1496  LOG_TARGET_WARNING(target, "resume of a SMP target failed, trying to resume current one");
1497  }
1498 
1499  cortex_m_restart_one(target, debug_execution);
1500  if (retval != ERROR_OK) {
1501  LOG_TARGET_ERROR(target, "resume failed");
1502  return retval;
1503  }
1504 
1505  LOG_TARGET_DEBUG(target, "%sresumed at " TARGET_ADDR_FMT,
1506  debug_execution ? "debug " : "", address);
1507 
1508  return ERROR_OK;
1509 }
1510 
1511 /* int irqstepcount = 0; */
1512 static int cortex_m_step(struct target *target, bool current,
1513  target_addr_t address, bool handle_breakpoints)
1514 {
1515  struct cortex_m_common *cortex_m = target_to_cm(target);
1516  struct armv7m_common *armv7m = &cortex_m->armv7m;
1517  struct breakpoint *breakpoint = NULL;
1518  struct reg *pc = armv7m->arm.pc;
1519  bool bkpt_inst_found = false;
1520  int retval;
1521  bool isr_timed_out = false;
1522 
1523  if (target->state != TARGET_HALTED) {
1524  LOG_TARGET_ERROR(target, "not halted");
1525  return ERROR_TARGET_NOT_HALTED;
1526  }
1527 
1528  /* Just one of SMP cores will step. Set the gdb control
1529  * target to current one or gdb miss gdb-end event */
1530  if (target->smp && target->gdb_service)
1532 
1533  /* current = true: continue on current pc, otherwise continue at <address> */
1534  if (!current) {
1535  buf_set_u32(pc->value, 0, 32, address);
1536  pc->dirty = true;
1537  pc->valid = true;
1538  }
1539 
1540  uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
1541 
1542  /* the front-end may request us not to handle breakpoints */
1543  if (handle_breakpoints) {
1544  breakpoint = breakpoint_find(target, pc_value);
1545  if (breakpoint)
1547  }
1548 
1549  armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
1550 
1552 
1554 
1556 
1557  /* if no bkpt instruction is found at pc then we can perform
1558  * a normal step, otherwise we have to manually step over the bkpt
1559  * instruction - as such simulate a step */
1560  if (!bkpt_inst_found) {
1561  if (cortex_m->isrmasking_mode != CORTEX_M_ISRMASK_AUTO) {
1562  /* Automatic ISR masking mode off: Just step over the next
1563  * instruction, with interrupts on or off as appropriate. */
1566  } else {
1567  /* Process interrupts during stepping in a way they don't interfere
1568  * debugging.
1569  *
1570  * Principle:
1571  *
1572  * Set a temporary break point at the current pc and let the core run
1573  * with interrupts enabled. Pending interrupts get served and we run
1574  * into the breakpoint again afterwards. Then we step over the next
1575  * instruction with interrupts disabled.
1576  *
1577  * If the pending interrupts don't complete within time, we leave the
1578  * core running. This may happen if the interrupts trigger faster
1579  * than the core can process them or the handler doesn't return.
1580  *
1581  * If no more breakpoints are available we simply do a step with
1582  * interrupts enabled.
1583  *
1584  */
1585 
1586  /* 2012-09-29 ph
1587  *
1588  * If a break point is already set on the lower half word then a break point on
1589  * the upper half word will not break again when the core is restarted. So we
1590  * just step over the instruction with interrupts disabled.
1591  *
1592  * The documentation has no information about this, it was found by observation
1593  * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 doesn't seem to
1594  * suffer from this problem.
1595  *
1596  * To add some confusion: pc_value has bit 0 always set, while the breakpoint
1597  * address has it always cleared. The former is done to indicate thumb mode
1598  * to gdb.
1599  *
1600  */
1601  if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
1602  LOG_TARGET_DEBUG(target, "Stepping over next instruction with interrupts disabled");
1605  /* Re-enable interrupts if appropriate */
1608  } else {
1609 
1610  /* Set a temporary break point */
1611  if (breakpoint) {
1613  } else {
1615  if (cortex_m->fp_rev == 0 && pc_value > 0x1FFFFFFF) {
1616  /* FPB rev.1 cannot handle such addr, try BKPT instr */
1617  type = BKPT_SOFT;
1618  }
1619  retval = breakpoint_add(target, pc_value, 2, type);
1620  }
1621 
1622  bool tmp_bp_set = (retval == ERROR_OK);
1623 
1624  /* No more breakpoints left, just do a step */
1625  if (!tmp_bp_set) {
1628  /* Re-enable interrupts if appropriate */
1631  } else {
1632  /* Start the core */
1633  LOG_TARGET_DEBUG(target, "Starting core to serve pending interrupts");
1634  int64_t t_start = timeval_ms();
1637 
1638  /* Wait for pending handlers to complete or timeout */
1639  do {
1641  if (retval != ERROR_OK) {
1643  return retval;
1644  }
1645  isr_timed_out = ((timeval_ms() - t_start) > 500);
1646  } while (!((cortex_m->dcb_dhcsr & S_HALT) || isr_timed_out));
1647 
1648  /* only remove breakpoint if we created it */
1649  if (breakpoint)
1651  else {
1652  /* Remove the temporary breakpoint */
1653  breakpoint_remove(target, pc_value);
1654  }
1655 
1656  if (isr_timed_out) {
1657  LOG_TARGET_DEBUG(target, "Interrupt handlers didn't complete within time, "
1658  "leaving target running");
1659  } else {
1660  /* Step over next instruction with interrupts disabled */
1663  C_HALT | C_MASKINTS,
1664  0);
1666  /* Re-enable interrupts if appropriate */
1669  }
1670  }
1671  }
1672  }
1673  }
1674 
1676  if (retval != ERROR_OK)
1677  return retval;
1678 
1679  /* registers are now invalid */
1681 
1682  if (breakpoint)
1684 
1685  if (isr_timed_out) {
1686  /* Leave the core running. The user has to stop execution manually. */
1689  return ERROR_OK;
1690  }
1691 
1692  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1693  " nvic_icsr = 0x%" PRIx32,
1694  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1695 
1696  retval = cortex_m_debug_entry(target);
1697  if (retval != ERROR_OK && retval != ERROR_TARGET_HALTED_DO_RESUME)
1698  return retval;
1700 
1701  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1702  " nvic_icsr = 0x%" PRIx32,
1703  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1704 
1705  return ERROR_OK;
1706 }
1707 
1709 {
1710  struct cortex_m_common *cortex_m = target_to_cm(target);
1711  struct armv7m_common *armv7m = &cortex_m->armv7m;
1712  enum cortex_m_soft_reset_config reset_config = cortex_m->soft_reset_config;
1713 
1714  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1716  target_was_examined(target) ? "" : " not");
1717 
1719 
1721  /* allow scripts to override the reset event */
1722 
1726 
1727  return ERROR_OK;
1728  }
1729 
1730  /* some cores support connecting while srst is asserted
1731  * use that mode if it has been configured */
1732 
1733  bool srst_asserted = false;
1734 
1737  || (!armv7m->debug_ap && !target->defer_examine))) {
1738  /* If we have no debug_ap, asserting SRST is the only thing
1739  * we can do now */
1741  srst_asserted = true;
1742  }
1743 
1744  /* TODO: replace the hack calling target_examine_one()
1745  * as soon as a better reset framework is available */
1748  LOG_TARGET_DEBUG(target, "Trying to re-examine under reset");
1750  }
1751 
1752  /* We need at least debug_ap to go further.
1753  * Inform user and bail out if we don't have one. */
1754  if (!armv7m->debug_ap) {
1755  if (srst_asserted) {
1756  if (target->reset_halt)
1757  LOG_TARGET_ERROR(target, "Debug AP not available, will not halt after reset!");
1758 
1759  /* Do not propagate error: reset was asserted, proceed to deassert! */
1762  return ERROR_OK;
1763 
1764  } else {
1765  LOG_TARGET_ERROR(target, "Debug AP not available, reset NOT asserted!");
1766  return ERROR_FAIL;
1767  }
1768  }
1769 
1770  /* Enable debug requests */
1772 
1773  /* Store important errors instead of failing and proceed to reset assert */
1774 
1775  if (retval != ERROR_OK || !(cortex_m->dcb_dhcsr & C_DEBUGEN))
1777 
1778  /* If the processor is sleeping in a WFI or WFE instruction, the
1779  * C_HALT bit must be asserted to regain control */
1780  if (retval == ERROR_OK && (cortex_m->dcb_dhcsr & S_SLEEP))
1782 
1783  mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
1784  /* Ignore less important errors */
1785 
1786  if (!target->reset_halt) {
1787  /* Set/Clear C_MASKINTS in a separate operation */
1789 
1790  /* clear any debug flags before resuming */
1792 
1793  /* clear C_HALT in dhcsr reg */
1795  } else {
1796  /* Halt in debug on reset; endreset_event() restores DEMCR.
1797  *
1798  * REVISIT catching BUSERR presumably helps to defend against
1799  * bad vector table entries. Should this include MMERR or
1800  * other flags too?
1801  */
1802  int retval2;
1803  retval2 = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DEMCR,
1806  if (retval != ERROR_OK || retval2 != ERROR_OK)
1807  LOG_TARGET_INFO(target, "AP write error, reset will not halt");
1808  }
1809 
1811  /* default to asserting srst */
1812  if (!srst_asserted)
1814 
1815  /* srst is asserted, ignore AP access errors */
1816  retval = ERROR_OK;
1817  } else {
1818  /* Use a standard Cortex-M software reset mechanism.
1819  * We default to using VECTRESET.
1820  * This has the disadvantage of not resetting the peripherals, so a
1821  * reset-init event handler is needed to perform any peripheral resets.
1822  */
1823  if (!cortex_m->vectreset_supported
1824  && reset_config == CORTEX_M_RESET_VECTRESET) {
1825  reset_config = CORTEX_M_RESET_SYSRESETREQ;
1826  LOG_TARGET_WARNING(target, "VECTRESET is not supported on this Cortex-M core, using SYSRESETREQ instead.");
1827  LOG_TARGET_WARNING(target, "Set 'cortex_m reset_config sysresetreq'.");
1828  }
1829 
1830  LOG_TARGET_DEBUG(target, "Using Cortex-M %s", (reset_config == CORTEX_M_RESET_SYSRESETREQ)
1831  ? "SYSRESETREQ" : "VECTRESET");
1832 
1833  if (reset_config == CORTEX_M_RESET_VECTRESET) {
1834  LOG_TARGET_WARNING(target, "Only resetting the Cortex-M core, use a reset-init event "
1835  "handler to reset any peripherals or configure hardware srst support.");
1836  }
1837 
1838  int retval3;
1839  retval3 = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1840  AIRCR_VECTKEY | ((reset_config == CORTEX_M_RESET_SYSRESETREQ)
1842  if (retval3 != ERROR_OK)
1843  LOG_TARGET_DEBUG(target, "Ignoring AP write error right after reset");
1844 
1845  retval3 = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1846  if (retval3 != ERROR_OK) {
1847  LOG_TARGET_ERROR(target, "DP initialisation failed");
1848  /* The error return value must not be propagated in this case.
1849  * SYSRESETREQ or VECTRESET have been possibly triggered
1850  * so reset processing should continue */
1851  } else {
1852  /* I do not know why this is necessary, but it
1853  * fixes strange effects (step/resume cause NMI
1854  * after reset) on LM3S6918 -- Michael Schwingen
1855  */
1856  uint32_t tmp;
1857  mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_AIRCR, &tmp);
1858  }
1859  }
1860 
1862  jtag_sleep(50000);
1863 
1865 
1866  return retval;
1867 }
1868 
1870 {
1871  struct armv7m_common *armv7m = &target_to_cm(target)->armv7m;
1872 
1873  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1875  target_was_examined(target) ? "" : " not");
1876 
1878 
1879  /* deassert reset lines */
1882 
1883 
1886  armv7m->debug_ap) {
1887 
1888  int retval = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1889  if (retval != ERROR_OK) {
1890  LOG_TARGET_ERROR(target, "DP initialisation failed");
1891  return retval;
1892  }
1893  }
1894 
1895  return ERROR_OK;
1896 }
1897 
1899 {
1900  int retval;
1901  unsigned int fp_num = 0;
1902  struct cortex_m_common *cortex_m = target_to_cm(target);
1903  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1904 
1905  if (breakpoint->is_set) {
1906  LOG_TARGET_WARNING(target, "breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id);
1907  return ERROR_OK;
1908  }
1909 
1910  if (breakpoint->type == BKPT_HARD) {
1911  uint32_t fpcr_value;
1912  while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
1913  fp_num++;
1914  if (fp_num >= cortex_m->fp_num_code) {
1915  LOG_TARGET_ERROR(target, "Can not find free FPB Comparator!");
1917  }
1918  breakpoint_hw_set(breakpoint, fp_num);
1919  fpcr_value = breakpoint->address | 1;
1920  if (cortex_m->fp_rev == 0) {
1921  if (breakpoint->address > 0x1FFFFFFF) {
1922  LOG_TARGET_ERROR(target, "Cortex-M Flash Patch Breakpoint rev.1 "
1923  "cannot handle HW breakpoint above address 0x1FFFFFFE");
1924  return ERROR_FAIL;
1925  }
1926  uint32_t hilo;
1928  fpcr_value = (fpcr_value & 0x1FFFFFFC) | hilo | 1;
1929  } else if (cortex_m->fp_rev > 1) {
1930  LOG_TARGET_ERROR(target, "Unhandled Cortex-M Flash Patch Breakpoint architecture revision");
1931  return ERROR_FAIL;
1932  }
1933  comparator_list[fp_num].used = true;
1934  comparator_list[fp_num].fpcr_value = fpcr_value;
1935  target_write_u32(target, comparator_list[fp_num].fpcr_address,
1936  comparator_list[fp_num].fpcr_value);
1937  LOG_TARGET_DEBUG(target, "fpc_num %i fpcr_value 0x%" PRIx32,
1938  fp_num,
1939  comparator_list[fp_num].fpcr_value);
1940  if (!cortex_m->fpb_enabled) {
1941  LOG_TARGET_DEBUG(target, "FPB wasn't enabled, do it now");
1942  retval = cortex_m_enable_fpb(target);
1943  if (retval != ERROR_OK) {
1944  LOG_TARGET_ERROR(target, "Failed to enable the FPB");
1945  return retval;
1946  }
1947 
1948  cortex_m->fpb_enabled = true;
1949  }
1950  } else if (breakpoint->type == BKPT_SOFT) {
1951  uint8_t code[4];
1952 
1953  /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
1954  * semihosting; don't use that. Otherwise the BKPT
1955  * parameter is arbitrary.
1956  */
1957  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1958  retval = target_read_memory(target,
1959  breakpoint->address & 0xFFFFFFFE,
1960  breakpoint->length, 1,
1962  if (retval != ERROR_OK)
1963  return retval;
1964  // make sure data cache is cleaned & invalidated down to PoC
1966  if (retval != ERROR_OK)
1967  return retval;
1968 
1969  retval = target_write_memory(target,
1970  breakpoint->address & 0xFFFFFFFE,
1971  breakpoint->length, 1,
1972  code);
1973  if (retval != ERROR_OK)
1974  return retval;
1975  // update i-cache at breakpoint location
1977  if (retval != ERROR_OK)
1978  return retval;
1980  if (retval != ERROR_OK)
1981  return retval;
1982 
1983  breakpoint->is_set = true;
1984  }
1985 
1986  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1988  (int)(breakpoint->type),
1990  breakpoint->length,
1991  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1992 
1993  return ERROR_OK;
1994 }
1995 
1997 {
1998  int retval;
1999  struct cortex_m_common *cortex_m = target_to_cm(target);
2000  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
2001 
2002  if (!breakpoint->is_set) {
2003  LOG_TARGET_WARNING(target, "breakpoint not set");
2004  return ERROR_OK;
2005  }
2006 
2007  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
2009  (int)(breakpoint->type),
2011  breakpoint->length,
2012  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
2013 
2014  if (breakpoint->type == BKPT_HARD) {
2015  unsigned int fp_num = breakpoint->number;
2016  if (fp_num >= cortex_m->fp_num_code) {
2017  LOG_TARGET_DEBUG(target, "Invalid FP Comparator number in breakpoint");
2018  return ERROR_OK;
2019  }
2020  comparator_list[fp_num].used = false;
2021  comparator_list[fp_num].fpcr_value = 0;
2022  target_write_u32(target, comparator_list[fp_num].fpcr_address,
2023  comparator_list[fp_num].fpcr_value);
2024  } else {
2025  // make sure data cache is cleaned & invalidated down to PoC
2027  if (retval != ERROR_OK)
2028  return retval;
2029 
2030  /* restore original instruction (kept in target endianness) */
2031  retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE,
2032  breakpoint->length, 1,
2034  if (retval != ERROR_OK)
2035  return retval;
2036 
2037  // update i-cache at breakpoint location
2039  if (retval != ERROR_OK)
2040  return retval;
2042  if (retval != ERROR_OK)
2043  return retval;
2044  }
2045  breakpoint->is_set = false;
2046 
2047  return ERROR_OK;
2048 }
2049 
2051 {
2052  /*
2053  * GDB packets Z0 and z0 provide the 'kind' parameter that is target-specific
2054  * and typically indicates the size in bytes of the breakpoint.
2055  * But for 32-bit Thumb mode (Thumb-2) breakpoint, GDB provides 'kind = 3' to
2056  * be used to derive the length information. See:
2057  * https://sourceware.org/gdb/current/onlinedocs/gdb.html/ARM-Breakpoint-Kinds.html
2058  * Since there isn't a four byte Thumb-2 breakpoint instruction, always use
2059  * the two bytes breakpoint instruction.
2060  */
2061  if (breakpoint->length == 3) {
2062  LOG_TARGET_DEBUG(target, "Using a two byte breakpoint for 32bit Thumb-2 request");
2063  breakpoint->length = 2;
2064  }
2065 
2066  if ((breakpoint->length != 2)) {
2067  LOG_TARGET_INFO(target, "only breakpoints of two bytes length supported");
2069  }
2070 
2072 }
2073 
2075 {
2076  if (!breakpoint->is_set)
2077  return ERROR_OK;
2078 
2080 }
2081 
2083 {
2084  unsigned int dwt_num = 0;
2085  struct cortex_m_common *cortex_m = target_to_cm(target);
2086 
2087  /* REVISIT Don't fully trust these "not used" records ... users
2088  * may set up breakpoints by hand, e.g. dual-address data value
2089  * watchpoint using comparator #1; comparator #0 matching cycle
2090  * count; send data trace info through ITM and TPIU; etc
2091  */
2092  struct cortex_m_dwt_comparator *comparator;
2093 
2094  for (comparator = cortex_m->dwt_comparator_list;
2095  comparator->used && dwt_num < cortex_m->dwt_num_comp;
2096  comparator++, dwt_num++)
2097  continue;
2098  if (dwt_num >= cortex_m->dwt_num_comp) {
2099  LOG_TARGET_ERROR(target, "Can not find free DWT Comparator");
2100  return ERROR_FAIL;
2101  }
2102  comparator->used = true;
2103  watchpoint_set(watchpoint, dwt_num);
2104 
2105  comparator->comp = watchpoint->address;
2107  comparator->comp);
2108 
2109  if ((cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M_V2_0
2110  && (cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M_V2_1) {
2111  uint32_t mask = 0, temp;
2112 
2113  /* watchpoint params were validated earlier */
2114  temp = watchpoint->length;
2115  while (temp) {
2116  temp >>= 1;
2117  mask++;
2118  }
2119  mask--;
2120 
2121  comparator->mask = mask;
2123  comparator->mask);
2124 
2125  switch (watchpoint->rw) {
2126  case WPT_READ:
2127  comparator->function = 5;
2128  break;
2129  case WPT_WRITE:
2130  comparator->function = 6;
2131  break;
2132  case WPT_ACCESS:
2133  comparator->function = 7;
2134  break;
2135  }
2136  } else {
2137  uint32_t data_size = watchpoint->length >> 1;
2138  comparator->mask = (watchpoint->length >> 1) | 1;
2139 
2140  switch (watchpoint->rw) {
2141  case WPT_ACCESS:
2142  comparator->function = 4;
2143  break;
2144  case WPT_WRITE:
2145  comparator->function = 5;
2146  break;
2147  case WPT_READ:
2148  comparator->function = 6;
2149  break;
2150  }
2151  comparator->function = comparator->function | (1 << 4) |
2152  (data_size << 10);
2153  }
2154 
2156  comparator->function);
2157 
2158  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%d 0x%08" PRIx32 " 0x%" PRIx32 " 0x%05" PRIx32,
2159  watchpoint->unique_id, dwt_num,
2160  comparator->comp, comparator->mask, comparator->function);
2161  return ERROR_OK;
2162 }
2163 
2165 {
2166  struct cortex_m_common *cortex_m = target_to_cm(target);
2167  struct cortex_m_dwt_comparator *comparator;
2168 
2169  if (!watchpoint->is_set) {
2170  LOG_TARGET_WARNING(target, "watchpoint (wpid: %d) not set",
2172  return ERROR_OK;
2173  }
2174 
2175  unsigned int dwt_num = watchpoint->number;
2176 
2177  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%u address: " TARGET_ADDR_FMT " clear",
2178  watchpoint->unique_id, dwt_num,
2179  watchpoint->address);
2180 
2181  if (dwt_num >= cortex_m->dwt_num_comp) {
2182  LOG_TARGET_DEBUG(target, "Invalid DWT Comparator number in watchpoint");
2183  return ERROR_OK;
2184  }
2185 
2186  comparator = cortex_m->dwt_comparator_list + dwt_num;
2187  comparator->used = false;
2188  comparator->function = 0;
2190  comparator->function);
2191 
2192  watchpoint->is_set = false;
2193 
2194  return ERROR_OK;
2195 }
2196 
2198 {
2199  struct cortex_m_common *cortex_m = target_to_cm(target);
2200 
2201  if (cortex_m->dwt_comp_available < 1) {
2202  LOG_TARGET_DEBUG(target, "no comparators?");
2204  }
2205 
2206  /* REVISIT This DWT may well be able to watch for specific data
2207  * values. Requires comparator #1 to set DATAVMATCH and match
2208  * the data, and another comparator (DATAVADDR0) matching addr.
2209  *
2210  * NOTE: hardware doesn't support data value masking, so we'll need
2211  * to check that mask is zero
2212  */
2214  LOG_TARGET_DEBUG(target, "watchpoint value masks not supported");
2216  }
2217 
2218  /* hardware allows address masks of up to 32K */
2219  unsigned int mask;
2220 
2221  for (mask = 0; mask < 16; mask++) {
2222  if ((1u << mask) == watchpoint->length)
2223  break;
2224  }
2225  if (mask == 16) {
2226  LOG_TARGET_DEBUG(target, "unsupported watchpoint length");
2228  }
2229  if (watchpoint->address & ((1 << mask) - 1)) {
2230  LOG_TARGET_DEBUG(target, "watchpoint address is unaligned");
2232  }
2233 
2234  cortex_m->dwt_comp_available--;
2235  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
2236 
2237  return ERROR_OK;
2238 }
2239 
2241 {
2242  struct cortex_m_common *cortex_m = target_to_cm(target);
2243 
2244  /* REVISIT why check? DWT can be updated with core running ... */
2245  if (target->state != TARGET_HALTED) {
2246  LOG_TARGET_ERROR(target, "not halted");
2247  return ERROR_TARGET_NOT_HALTED;
2248  }
2249 
2250  if (watchpoint->is_set)
2252 
2253  cortex_m->dwt_comp_available++;
2254  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
2255 
2256  return ERROR_OK;
2257 }
2258 
2259 static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
2260 {
2262  return ERROR_FAIL;
2263 
2264  struct cortex_m_common *cortex_m = target_to_cm(target);
2265 
2266  for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
2267  if (!wp->is_set)
2268  continue;
2269 
2270  unsigned int dwt_num = wp->number;
2271  struct cortex_m_dwt_comparator *comparator = cortex_m->dwt_comparator_list + dwt_num;
2272 
2273  uint32_t dwt_function;
2274  int retval = target_read_u32(target, comparator->dwt_comparator_address + 8, &dwt_function);
2275  if (retval != ERROR_OK)
2276  return ERROR_FAIL;
2277 
2278  /* check the MATCHED bit */
2279  if (dwt_function & BIT(24)) {
2280  *hit_watchpoint = wp;
2281  return ERROR_OK;
2282  }
2283  }
2284 
2285  return ERROR_FAIL;
2286 }
2287 
2289 {
2291 
2292  /* set any pending watchpoints */
2293  while (watchpoint) {
2294  if (!watchpoint->is_set)
2297  }
2298 }
2299 
2301  uint32_t size, uint32_t count, uint8_t *buffer)
2302 {
2303  struct armv7m_common *armv7m = target_to_armv7m(target);
2304 
2305  if (armv7m->arm.arch == ARM_ARCH_V6M) {
2306  /* armv6m does not handle unaligned memory access */
2307  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2309  }
2310 
2311  return mem_ap_read_buf(armv7m->debug_ap, buffer, size, count, address);
2312 }
2313 
2315  uint32_t size, uint32_t count, const uint8_t *buffer)
2316 {
2317  struct armv7m_common *armv7m = target_to_armv7m(target);
2318 
2319  if (armv7m->arm.arch == ARM_ARCH_V6M) {
2320  /* armv6m does not handle unaligned memory access */
2321  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2323  }
2324 
2325  return mem_ap_write_buf(armv7m->debug_ap, buffer, size, count, address);
2326 }
2327 
2328 static int cortex_m_init_target(struct command_context *cmd_ctx,
2329  struct target *target)
2330 {
2333  return ERROR_OK;
2334 }
2335 
2337 {
2338  struct cortex_m_common *cortex_m = target_to_cm(target);
2339  struct armv7m_common *armv7m = target_to_armv7m(target);
2340 
2341  if (!armv7m->is_hla_target && armv7m->debug_ap)
2342  dap_put_ap(armv7m->debug_ap);
2343 
2344  free(cortex_m->fp_comparator_list);
2345 
2348 
2349  free(target->private_config);
2350  free(cortex_m);
2351 }
2352 
2353 int cortex_m_profiling(struct target *target, uint32_t *samples,
2354  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2355 {
2356  struct timeval timeout, now;
2357  struct armv7m_common *armv7m = target_to_armv7m(target);
2358  uint32_t reg_value;
2359  int retval;
2360 
2361  retval = target_read_u32(target, DWT_PCSR, &reg_value);
2362  if (retval != ERROR_OK) {
2363  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2364  return retval;
2365  }
2366  if (reg_value == 0) {
2367  LOG_TARGET_INFO(target, "PCSR sampling not supported on this processor.");
2368  return target_profiling_default(target, samples, max_num_samples, num_samples, seconds);
2369  }
2370 
2372  timeval_add_time(&timeout, seconds, 0);
2373 
2374  LOG_TARGET_INFO(target, "Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
2375 
2376  /* Make sure the target is running */
2378  if (target->state == TARGET_HALTED)
2379  retval = target_resume(target, true, 0, false, false);
2380 
2381  if (retval != ERROR_OK) {
2382  LOG_TARGET_ERROR(target, "Error while resuming target");
2383  return retval;
2384  }
2385 
2386  uint32_t sample_count = 0;
2387 
2388  for (;;) {
2389  if (armv7m && armv7m->debug_ap) {
2390  uint32_t read_count = max_num_samples - sample_count;
2391  if (read_count > 1024)
2392  read_count = 1024;
2393 
2394  retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
2395  (void *)&samples[sample_count],
2396  4, read_count, DWT_PCSR);
2397  sample_count += read_count;
2398  } else {
2399  target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
2400  }
2401 
2402  if (retval != ERROR_OK) {
2403  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2404  return retval;
2405  }
2406 
2407 
2408  gettimeofday(&now, NULL);
2409  if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
2410  LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 " samples.", sample_count);
2411  break;
2412  }
2413  }
2414 
2415  *num_samples = sample_count;
2416  return retval;
2417 }
2418 
2419 
2420 /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
2421  * on r/w if the core is not running, and clear on resume or reset ... or
2422  * at least, in a post_restore_context() method.
2423  */
2424 
2426  struct target *target;
2427  uint32_t addr;
2428  uint8_t value[4]; /* scratch/cache */
2429 };
2430 
2431 static int cortex_m_dwt_get_reg(struct reg *reg)
2432 {
2433  struct dwt_reg_state *state = reg->arch_info;
2434 
2435  uint32_t tmp;
2436  int retval = target_read_u32(state->target, state->addr, &tmp);
2437  if (retval != ERROR_OK)
2438  return retval;
2439 
2440  buf_set_u32(state->value, 0, 32, tmp);
2441  return ERROR_OK;
2442 }
2443 
2444 static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
2445 {
2446  struct dwt_reg_state *state = reg->arch_info;
2447 
2448  return target_write_u32(state->target, state->addr,
2449  buf_get_u32(buf, 0, reg->size));
2450 }
2451 
2452 struct dwt_reg {
2453  uint32_t addr;
2454  const char *name;
2455  unsigned int size;
2456 };
2457 
2458 static const struct dwt_reg dwt_base_regs[] = {
2459  { DWT_CTRL, "dwt_ctrl", 32, },
2460  /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
2461  * increments while the core is asleep.
2462  */
2463  { DWT_CYCCNT, "dwt_cyccnt", 32, },
2464  /* plus some 8 bit counters, useful for profiling with TPIU */
2465 };
2466 
2467 static const struct dwt_reg dwt_comp[] = {
2468 #define DWT_COMPARATOR(i) \
2469  { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
2470  { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
2471  { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
2472  DWT_COMPARATOR(0),
2473  DWT_COMPARATOR(1),
2474  DWT_COMPARATOR(2),
2475  DWT_COMPARATOR(3),
2476  DWT_COMPARATOR(4),
2477  DWT_COMPARATOR(5),
2478  DWT_COMPARATOR(6),
2479  DWT_COMPARATOR(7),
2480  DWT_COMPARATOR(8),
2481  DWT_COMPARATOR(9),
2482  DWT_COMPARATOR(10),
2483  DWT_COMPARATOR(11),
2484  DWT_COMPARATOR(12),
2485  DWT_COMPARATOR(13),
2486  DWT_COMPARATOR(14),
2487  DWT_COMPARATOR(15),
2488 #undef DWT_COMPARATOR
2489 };
2490 
2491 static const struct reg_arch_type dwt_reg_type = {
2493  .set = cortex_m_dwt_set_reg,
2494 };
2495 
2496 static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
2497 {
2498  struct dwt_reg_state *state;
2499 
2500  state = calloc(1, sizeof(*state));
2501  if (!state)
2502  return;
2503  state->addr = d->addr;
2504  state->target = t;
2505 
2506  r->name = d->name;
2507  r->size = d->size;
2508  r->value = state->value;
2509  r->arch_info = state;
2510  r->type = &dwt_reg_type;
2511  r->exist = true;
2512 }
2513 
2514 static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
2515 {
2516  uint32_t dwtcr;
2517  struct reg_cache *cache;
2518  struct cortex_m_dwt_comparator *comparator;
2519  int reg;
2520 
2521  target_read_u32(target, DWT_CTRL, &dwtcr);
2522  LOG_TARGET_DEBUG(target, "DWT_CTRL: 0x%" PRIx32, dwtcr);
2523  if (!dwtcr) {
2524  LOG_TARGET_DEBUG(target, "no DWT");
2525  return;
2526  }
2527 
2529  LOG_TARGET_DEBUG(target, "DWT_DEVARCH: 0x%" PRIx32, cm->dwt_devarch);
2530 
2531  cm->dwt_num_comp = (dwtcr >> 28) & 0xF;
2532  cm->dwt_comp_available = cm->dwt_num_comp;
2533  cm->dwt_comparator_list = calloc(cm->dwt_num_comp,
2534  sizeof(struct cortex_m_dwt_comparator));
2535  if (!cm->dwt_comparator_list) {
2536 fail0:
2537  cm->dwt_num_comp = 0;
2538  LOG_TARGET_ERROR(target, "out of mem");
2539  return;
2540  }
2541 
2542  cache = calloc(1, sizeof(*cache));
2543  if (!cache) {
2544 fail1:
2545  free(cm->dwt_comparator_list);
2546  goto fail0;
2547  }
2548  cache->name = "Cortex-M DWT registers";
2549  cache->num_regs = 2 + cm->dwt_num_comp * 3;
2550  cache->reg_list = calloc(cache->num_regs, sizeof(*cache->reg_list));
2551  if (!cache->reg_list) {
2552  free(cache);
2553  goto fail1;
2554  }
2555 
2556  for (reg = 0; reg < 2; reg++)
2558  dwt_base_regs + reg);
2559 
2560  comparator = cm->dwt_comparator_list;
2561  for (unsigned int i = 0; i < cm->dwt_num_comp; i++, comparator++) {
2562  int j;
2563 
2564  comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
2565  for (j = 0; j < 3; j++, reg++)
2567  dwt_comp + 3 * i + j);
2568 
2569  /* make sure we clear any watchpoints enabled on the target */
2570  target_write_u32(target, comparator->dwt_comparator_address + 8, 0);
2571  }
2572 
2574  cm->dwt_cache = cache;
2575 
2576  LOG_TARGET_DEBUG(target, "DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
2577  dwtcr, cm->dwt_num_comp,
2578  (dwtcr & (0xf << 24)) ? " only" : "/trigger");
2579 
2580  /* REVISIT: if num_comp > 1, check whether comparator #1 can
2581  * implement single-address data value watchpoints ... so we
2582  * won't need to check it later, when asked to set one up.
2583  */
2584 }
2585 
2586 static void cortex_m_dwt_free(struct target *target)
2587 {
2588  struct cortex_m_common *cm = target_to_cm(target);
2589  struct reg_cache *cache = cm->dwt_cache;
2590 
2591  free(cm->dwt_comparator_list);
2592  cm->dwt_comparator_list = NULL;
2593  cm->dwt_num_comp = 0;
2594 
2595  if (cache) {
2597 
2598  if (cache->reg_list) {
2599  for (size_t i = 0; i < cache->num_regs; i++)
2600  free(cache->reg_list[i].arch_info);
2601  free(cache->reg_list);
2602  }
2603  free(cache);
2604  }
2605  cm->dwt_cache = NULL;
2606 }
2607 
2608 static bool cortex_m_has_tz(struct target *target)
2609 {
2610  struct armv7m_common *armv7m = target_to_armv7m(target);
2611  uint32_t dauthstatus;
2612 
2613  if (armv7m->arm.arch != ARM_ARCH_V8M)
2614  return false;
2615 
2616  int retval = target_read_u32(target, DAUTHSTATUS, &dauthstatus);
2617  if (retval != ERROR_OK) {
2618  LOG_TARGET_WARNING(target, "Error reading DAUTHSTATUS register");
2619  return false;
2620  }
2621  return (dauthstatus & DAUTHSTATUS_SID_MASK) != 0;
2622 }
2623 
2624 static bool cortex_m_main_extension(struct target *target, uint32_t cpuid)
2625 {
2626  /* Inspect architecture to differentiate main extension/baseline */
2627  unsigned int extension = (cpuid & ARM_CPUID_ARCHITECTURE_MASK) >> ARM_CPUID_ARCHITECTURE_POS;
2628 
2629  if (extension == ARM_CPUID_MAIN_EXTENSION)
2630  return true;
2631  else if (extension == ARM_CPUID_NO_MAIN_EXTENSION)
2632  return false;
2633 
2634  LOG_TARGET_WARNING(target, "Fail to detect target extension");
2635 
2636  return false;
2637 }
2638 
2640 {
2641  if (ssec) {
2642  ssec->dscsr_dirty = false;
2643  ssec->sau_ctrl_dirty = false;
2644  ssec->mpu_ctrl_dirty = false;
2645  }
2646 
2647  if (!cortex_m_has_tz(target))
2648  return ERROR_OK;
2649 
2650  uint32_t dscsr;
2651  int retval = target_read_u32(target, DCB_DSCSR, &dscsr);
2652  if (retval != ERROR_OK) {
2653  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR read failed");
2654  return retval;
2655  }
2656  if (!(dscsr & DSCSR_CDS)) {
2657  if (ssec) {
2658  ssec->dscsr_dirty = true;
2659  ssec->dscsr = dscsr;
2660  }
2661  LOG_TARGET_DEBUG(target, "Setting Current Domain Secure in DSCSR");
2663  if (retval != ERROR_OK) {
2664  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR write failed");
2665  return retval;
2666  }
2667  }
2668 
2669  uint32_t sau_ctrl;
2670  retval = target_read_u32(target, SAU_CTRL, &sau_ctrl);
2671  if (retval != ERROR_OK) {
2672  LOG_TARGET_ERROR(target, "ARMv8M set secure: SAU_CTRL read failed");
2673  return retval;
2674  }
2675  if (sau_ctrl & SAU_CTRL_ENABLE) {
2676  if (ssec) {
2677  ssec->sau_ctrl_dirty = true;
2678  ssec->sau_ctrl = sau_ctrl;
2679  }
2680  retval = target_write_u32(target, SAU_CTRL, sau_ctrl & ~SAU_CTRL_ENABLE);
2681  if (retval != ERROR_OK) {
2682  LOG_TARGET_ERROR(target, "ARMv8M set secure: SAU_CTRL write failed");
2683  return retval;
2684  }
2685  }
2686 
2687  uint32_t mpu_ctrl;
2688  retval = target_read_u32(target, MPU_CTRL, &mpu_ctrl);
2689  if (retval != ERROR_OK) {
2690  LOG_TARGET_ERROR(target, "ARMv8M set secure: MPU_CTRL read failed");
2691  return retval;
2692  }
2693  if (mpu_ctrl & MPU_CTRL_ENABLE) {
2694  if (ssec) {
2695  ssec->mpu_ctrl_dirty = true;
2696  ssec->mpu_ctrl = mpu_ctrl;
2697  }
2698  retval = target_write_u32(target, MPU_CTRL, mpu_ctrl & ~MPU_CTRL_ENABLE);
2699  if (retval != ERROR_OK) {
2700  LOG_TARGET_ERROR(target, "ARMv8M set secure: MPU_CTRL write failed");
2701  return retval;
2702  }
2703  }
2704  return ERROR_OK;
2705 }
2706 
2708 {
2709  int retval;
2710  if (!cortex_m_has_tz(target))
2711  return ERROR_OK;
2712 
2713  if (!ssec)
2714  return ERROR_OK;
2715 
2716  if (ssec->mpu_ctrl_dirty) {
2717  retval = target_write_u32(target, MPU_CTRL, ssec->mpu_ctrl);
2718  if (retval != ERROR_OK) {
2719  LOG_TARGET_ERROR(target, "ARMv8M security restore: MPU_CTRL write failed");
2720  return retval;
2721  }
2722  ssec->mpu_ctrl_dirty = false;
2723  }
2724 
2725  if (ssec->sau_ctrl_dirty) {
2726  retval = target_write_u32(target, SAU_CTRL, ssec->sau_ctrl);
2727  if (retval != ERROR_OK) {
2728  LOG_TARGET_ERROR(target, "ARMv8M security restore: SAU_CTRL write failed");
2729  return retval;
2730  }
2731  ssec->sau_ctrl_dirty = false;
2732  }
2733 
2734  if (ssec->dscsr_dirty) {
2735  LOG_TARGET_DEBUG(target, "Restoring Current Domain Security in DSCSR");
2736  retval = target_write_u32(target, DCB_DSCSR, ssec->dscsr & ~DSCSR_CDSKEY);
2737  if (retval != ERROR_OK) {
2738  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR write failed");
2739  return retval;
2740  }
2741  ssec->dscsr_dirty = false;
2742  }
2743  return ERROR_OK;
2744 }
2745 
2746 #define MVFR0 0xE000EF40
2747 #define MVFR0_SP_MASK 0x000000F0
2748 #define MVFR0_SP 0x00000020
2749 #define MVFR0_DP_MASK 0x00000F00
2750 #define MVFR0_DP 0x00000200
2751 
2752 #define MVFR1 0xE000EF44
2753 #define MVFR1_MVE_MASK 0x00000F00
2754 #define MVFR1_MVE_I 0x00000100
2755 #define MVFR1_MVE_F 0x00000200
2756 
2757 static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp,
2758  struct adiv5_ap **debug_ap)
2759 {
2761  return ERROR_OK;
2762 
2763  return dap_find_get_ap(swjdp, AP_TYPE_AHB5_AP, debug_ap);
2764 }
2765 
2767 {
2768  int retval;
2769  uint32_t cpuid, fpcr;
2770  struct cortex_m_common *cortex_m = target_to_cm(target);
2771  struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
2772  struct armv7m_common *armv7m = target_to_armv7m(target);
2773 
2774  /* hla_target shares the examine handler but does not support
2775  * all its calls */
2776  if (!armv7m->is_hla_target) {
2777  if (!armv7m->debug_ap) {
2778  if (cortex_m->apsel == DP_APSEL_INVALID) {
2779  /* Search for the MEM-AP */
2780  retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
2781  if (retval != ERROR_OK) {
2782  LOG_TARGET_ERROR(target, "Could not find MEM-AP to control the core");
2783  return retval;
2784  }
2785  } else {
2786  armv7m->debug_ap = dap_get_ap(swjdp, cortex_m->apsel);
2787  if (!armv7m->debug_ap) {
2788  LOG_TARGET_ERROR(target, "Cannot get AP");
2789  return ERROR_FAIL;
2790  }
2791  }
2792  }
2793 
2794  armv7m->debug_ap->memaccess_tck = 8;
2795 
2796  retval = mem_ap_init(armv7m->debug_ap);
2797  if (retval != ERROR_OK)
2798  return retval;
2799  }
2800 
2801  if (!target_was_examined(target)) {
2803 
2804  /* Read from Device Identification Registers */
2805  retval = target_read_u32(target, CPUID, &cpuid);
2806  if (retval != ERROR_OK)
2807  return retval;
2808 
2809  /* Inspect implementer/part to look for recognized cores */
2810  unsigned int impl_part = cpuid & (ARM_CPUID_IMPLEMENTER_MASK | ARM_CPUID_PARTNO_MASK);
2811 
2812  for (unsigned int n = 0; n < ARRAY_SIZE(cortex_m_parts); n++) {
2813  if (impl_part == cortex_m_parts[n].impl_part) {
2814  cortex_m->core_info = &cortex_m_parts[n];
2815  break;
2816  }
2817  }
2818 
2819  if (!cortex_m->core_info) {
2820  LOG_TARGET_ERROR(target, "Cortex-M CPUID: 0x%x is unrecognized", cpuid);
2821  return ERROR_FAIL;
2822  }
2823 
2824  armv7m->arm.arch = cortex_m->core_info->arch;
2825 
2826  LOG_TARGET_INFO(target, "%s r%" PRId8 "p%" PRId8 " processor detected",
2827  cortex_m->core_info->name,
2828  (uint8_t)((cpuid >> 20) & 0xf),
2829  (uint8_t)((cpuid >> 0) & 0xf));
2830 
2831  cortex_m->maskints_erratum = false;
2832  cortex_m->incorrect_halt_erratum = false;
2833  if (impl_part == CORTEX_M7_PARTNO) {
2834  uint8_t rev, patch;
2835  rev = (cpuid >> 20) & 0xf;
2836  patch = (cpuid >> 0) & 0xf;
2837  if ((rev == 0) && (patch < 2)) {
2838  LOG_TARGET_WARNING(target, "Erratum 702596: single stepping may enter pending exception handler!");
2839  cortex_m->maskints_erratum = true;
2840  }
2841  /* TODO: add revision check when a Cortex-M7 revision with fixed 3092511 is out */
2842  LOG_TARGET_WARNING(target, "Erratum 3092511: Cortex-M7 can halt in an incorrect address when breakpoint and exception occurs simultaneously");
2843  cortex_m->incorrect_halt_erratum = true;
2844  if (armv7m->is_hla_target)
2845  LOG_TARGET_WARNING(target, "No erratum 3092511 workaround on hla adapter");
2846  else
2847  LOG_TARGET_INFO(target, "The erratum 3092511 workaround will resume after an incorrect halt");
2848  }
2849  LOG_TARGET_DEBUG(target, "cpuid: 0x%8.8" PRIx32, cpuid);
2850 
2851  if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV4) {
2852  uint32_t mvfr0;
2853  target_read_u32(target, MVFR0, &mvfr0);
2854 
2855  if ((mvfr0 & MVFR0_SP_MASK) == MVFR0_SP) {
2856  LOG_TARGET_DEBUG(target, "%s floating point feature FPv4_SP found",
2857  cortex_m->core_info->name);
2858  armv7m->fp_feature = FPV4_SP;
2859  }
2860  } else if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV5) {
2861  uint32_t mvfr0, mvfr1;
2862  target_read_u32(target, MVFR0, &mvfr0);
2863  target_read_u32(target, MVFR1, &mvfr1);
2864 
2865  if ((mvfr0 & MVFR0_DP_MASK) == MVFR0_DP) {
2866  if ((mvfr1 & MVFR1_MVE_MASK) == MVFR1_MVE_F) {
2867  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP + MVE-F found",
2868  cortex_m->core_info->name);
2869  armv7m->fp_feature = FPV5_MVE_F;
2870  } else {
2871  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP found",
2872  cortex_m->core_info->name);
2873  armv7m->fp_feature = FPV5_DP;
2874  }
2875  } else if ((mvfr0 & MVFR0_SP_MASK) == MVFR0_SP) {
2876  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_SP found",
2877  cortex_m->core_info->name);
2878  armv7m->fp_feature = FPV5_SP;
2879  } else if ((mvfr1 & MVFR1_MVE_MASK) == MVFR1_MVE_I) {
2880  LOG_TARGET_DEBUG(target, "%s floating point feature MVE-I found",
2881  cortex_m->core_info->name);
2882  armv7m->fp_feature = FPV5_MVE_I;
2883  }
2884  }
2885 
2886  /* VECTRESET is supported only on ARMv7-M cores */
2887  cortex_m->vectreset_supported = armv7m->arm.arch == ARM_ARCH_V7M;
2888 
2889  /* Check for FPU, otherwise mark FPU register as non-existent */
2890  if (armv7m->fp_feature == FP_NONE)
2891  for (size_t idx = ARMV7M_FPU_FIRST_REG; idx <= ARMV7M_FPU_LAST_REG; idx++)
2892  armv7m->arm.core_cache->reg_list[idx].exist = false;
2893 
2894  /* TODO: MVE can be present without floating points. Revisit this test */
2895  if (armv7m->fp_feature != FPV5_MVE_F && armv7m->fp_feature != FPV5_MVE_I)
2896  armv7m->arm.core_cache->reg_list[ARMV8M_VPR].exist = false;
2897 
2898  if (armv7m->arm.arch == ARM_ARCH_V8M) {
2899  bool cm_has_tz = cortex_m_has_tz(target);
2900  bool main_ext = cortex_m_main_extension(target, cpuid);
2901  bool baseline = !main_ext;
2902 
2903  if (!cm_has_tz) {
2904  for (size_t idx = ARMV8M_TZ_FIRST_REG; idx <= ARMV8M_TZ_LAST_REG; idx++)
2905  armv7m->arm.core_cache->reg_list[idx].exist = false;
2906 
2907  if (baseline) {
2908  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2909  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2910  }
2911  } else {
2912  if (baseline) {
2913  /* ARMV8M without main extension but with the security extension has
2914  only two stack limit registers in Secure state */
2915  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM_NS].exist = false;
2916  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM_NS].exist = false;
2917  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2918  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2919 
2920  armv7m->arm.core_cache->reg_list[ARMV8M_BASEPRI_S].exist = false;
2921  armv7m->arm.core_cache->reg_list[ARMV8M_FAULTMASK_S].exist = false;
2922  armv7m->arm.core_cache->reg_list[ARMV8M_BASEPRI_NS].exist = false;
2923  armv7m->arm.core_cache->reg_list[ARMV8M_FAULTMASK_NS].exist = false;
2924  } else {
2925  /* There is no separate regsel for msplim/psplim of ARMV8M mainline
2926  with the security extension that would point to correct alias
2927  depending on security state of the processor, thus register marked
2928  as non-existing letting to choose between S/NS alias manually */
2929  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2930  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2931  }
2932  }
2933 
2934  if (baseline) {
2935  armv7m->arm.core_cache->reg_list[ARMV7M_BASEPRI].exist = false;
2936  armv7m->arm.core_cache->reg_list[ARMV7M_FAULTMASK].exist = false;
2937  }
2938  } else {
2939  /* Security extension and stack limit checking introduced in ARMV8M */
2940  for (size_t idx = ARMV8M_TZ_FIRST_REG; idx <= ARMV8M_TZ_LAST_REG; idx++)
2941  armv7m->arm.core_cache->reg_list[idx].exist = false;
2942 
2943  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2944  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2945 
2946  if (armv7m->arm.arch == ARM_ARCH_V6M) {
2947  armv7m->arm.core_cache->reg_list[ARMV7M_BASEPRI].exist = false;
2948  armv7m->arm.core_cache->reg_list[ARMV7M_FAULTMASK].exist = false;
2949  }
2950  }
2951 
2952  if (!armv7m->is_hla_target) {
2954  /* Cortex-M3/M4 have 4096 bytes autoincrement range,
2955  * s. ARM IHI 0031C: MEM-AP 7.2.2 */
2956  armv7m->debug_ap->tar_autoincr_block = (1 << 12);
2957  }
2958 
2959  retval = target_read_u32(target, DCB_DHCSR, &cortex_m->dcb_dhcsr);
2960  if (retval != ERROR_OK)
2961  return retval;
2962 
2963  /*
2964  * Use a safe value of sticky S_RESET_ST for cache detection, before
2965  * clearing it below.
2966  */
2967  if (!armv7m->is_hla_target) {
2968  retval = armv7m_identify_cache(target);
2969  if (retval != ERROR_OK) {
2970  LOG_ERROR("Cannot detect cache");
2971  return retval;
2972  }
2973  }
2974 
2975  /* Don't cumulate sticky S_RESET_ST at the very first read of DHCSR
2976  * as S_RESET_ST may indicate a reset that happened long time ago
2977  * (most probably the power-on reset before OpenOCD was started).
2978  * As we are just initializing the debug system we do not need
2979  * to call cortex_m_endreset_event() in the following poll.
2980  */
2981  if (!cortex_m->dcb_dhcsr_sticky_is_recent) {
2982  cortex_m->dcb_dhcsr_sticky_is_recent = true;
2983  if (cortex_m->dcb_dhcsr & S_RESET_ST) {
2984  LOG_TARGET_DEBUG(target, "reset happened some time ago, ignore");
2985  cortex_m->dcb_dhcsr &= ~S_RESET_ST;
2986  }
2987  }
2988  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
2989 
2990  if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
2991  /* Enable debug requests */
2992  uint32_t dhcsr = (cortex_m->dcb_dhcsr | C_DEBUGEN) & ~(C_HALT | C_STEP | C_MASKINTS);
2993 
2994  retval = target_write_u32(target, DCB_DHCSR, DBGKEY | (dhcsr & 0x0000FFFFUL));
2995  if (retval != ERROR_OK)
2996  return retval;
2997  cortex_m->dcb_dhcsr = dhcsr;
2998  }
2999 
3000  /* Configure trace modules */
3001  retval = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
3002  if (retval != ERROR_OK)
3003  return retval;
3004 
3005  /* Configure ITM */
3007 
3008  /* NOTE: FPB and DWT are both optional. */
3009 
3010  /* Setup FPB */
3011  target_read_u32(target, FP_CTRL, &fpcr);
3012  /* bits [14:12] and [7:4] */
3013  cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
3014  cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
3015  /* Detect flash patch revision, see RM DDI 0403E.b page C1-817.
3016  Revision is zero base, fp_rev == 1 means Rev.2 ! */
3017  cortex_m->fp_rev = (fpcr >> 28) & 0xf;
3018  free(cortex_m->fp_comparator_list);
3019  cortex_m->fp_comparator_list = calloc(
3020  cortex_m->fp_num_code + cortex_m->fp_num_lit,
3021  sizeof(struct cortex_m_fp_comparator));
3022  cortex_m->fpb_enabled = fpcr & 1;
3023  for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
3024  cortex_m->fp_comparator_list[i].type =
3025  (i < cortex_m->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
3026  cortex_m->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
3027 
3028  /* make sure we clear any breakpoints enabled on the target */
3030  }
3031  LOG_TARGET_DEBUG(target, "FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i",
3032  fpcr,
3033  cortex_m->fp_num_code,
3034  cortex_m->fp_num_lit);
3035 
3036  /* Setup DWT */
3038  cortex_m_dwt_setup(cortex_m, target);
3039 
3040  /* These hardware breakpoints only work for code in flash! */
3041  LOG_TARGET_INFO(target, "target has %d breakpoints, %d watchpoints",
3042  cortex_m->fp_num_code,
3043  cortex_m->dwt_num_comp);
3044  }
3045 
3046  return ERROR_OK;
3047 }
3048 
3049 static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
3050 {
3051  struct armv7m_common *armv7m = target_to_armv7m(target);
3052  uint16_t dcrdr;
3053  uint8_t buf[2];
3054  int retval;
3055 
3056  retval = mem_ap_read_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
3057  if (retval != ERROR_OK)
3058  return retval;
3059 
3060  dcrdr = target_buffer_get_u16(target, buf);
3061  *ctrl = (uint8_t)dcrdr;
3062  *value = (uint8_t)(dcrdr >> 8);
3063 
3064  LOG_TARGET_DEBUG(target, "data 0x%x ctrl 0x%x", *value, *ctrl);
3065 
3066  /* write ack back to software dcc register
3067  * signify we have read data */
3068  if (dcrdr & (1 << 0)) {
3069  target_buffer_set_u16(target, buf, 0);
3070  retval = mem_ap_write_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
3071  if (retval != ERROR_OK)
3072  return retval;
3073  }
3074 
3075  return ERROR_OK;
3076 }
3077 
3079  uint32_t size, uint8_t *buffer)
3080 {
3081  uint8_t data;
3082  uint8_t ctrl;
3083  uint32_t i;
3084 
3085  for (i = 0; i < (size * 4); i++) {
3086  int retval = cortex_m_dcc_read(target, &data, &ctrl);
3087  if (retval != ERROR_OK)
3088  return retval;
3089  buffer[i] = data;
3090  }
3091 
3092  return ERROR_OK;
3093 }
3094 
3096 {
3097  struct target *target = priv;
3099  return ERROR_OK;
3100 
3101  if (!target->dbg_msg_enabled)
3102  return ERROR_OK;
3103 
3104  if (target->state == TARGET_RUNNING) {
3105  uint8_t data;
3106  uint8_t ctrl;
3107  int retval;
3108 
3109  retval = cortex_m_dcc_read(target, &data, &ctrl);
3110  if (retval != ERROR_OK)
3111  return retval;
3112 
3113  /* check if we have data */
3114  if (ctrl & (1 << 0)) {
3115  uint32_t request;
3116 
3117  /* we assume target is quick enough */
3118  request = data;
3119  for (int i = 1; i <= 3; i++) {
3120  retval = cortex_m_dcc_read(target, &data, &ctrl);
3121  if (retval != ERROR_OK)
3122  return retval;
3123  request |= ((uint32_t)data << (i * 8));
3124  }
3125  target_request(target, request);
3126  }
3127  }
3128 
3129  return ERROR_OK;
3130 }
3131 
3133  struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
3134 {
3135  struct armv7m_common *armv7m = &cortex_m->armv7m;
3136 
3137  armv7m_init_arch_info(target, armv7m);
3138 
3139  /* default reset mode is to use srst if fitted
3140  * if not it will use CORTEX_M_RESET_VECTRESET */
3142 
3143  armv7m->arm.dap = dap;
3144 
3145  /* register arch-specific functions */
3147 
3148  armv7m->post_debug_entry = NULL;
3149 
3150  armv7m->pre_restore_context = NULL;
3151 
3154 
3157 
3158  return ERROR_OK;
3159 }
3160 
3162 {
3163  struct adiv5_private_config *pc;
3164 
3165  pc = (struct adiv5_private_config *)target->private_config;
3166  if (adiv5_verify_config(pc) != ERROR_OK)
3167  return ERROR_FAIL;
3168 
3169  struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
3170  if (!cortex_m) {
3171  LOG_TARGET_ERROR(target, "No memory creating target");
3172  return ERROR_FAIL;
3173  }
3174 
3175  cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
3176  cortex_m->apsel = pc->ap_num;
3177 
3178  cortex_m_init_arch_info(target, cortex_m, pc->dap);
3179 
3180  return ERROR_OK;
3181 }
3182 
3183 /*--------------------------------------------------------------------------*/
3184 
3186  struct cortex_m_common *cm)
3187 {
3188  if (!is_cortex_m_with_dap_access(cm)) {
3189  command_print(cmd, "target is not a Cortex-M");
3190  return ERROR_TARGET_INVALID;
3191  }
3192  return ERROR_OK;
3193 }
3194 
3195 /*
3196  * Only stuff below this line should need to verify that its target
3197  * is a Cortex-M with available DAP access (not a HLA adapter).
3198  */
3199 
3200 COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
3201 {
3203  struct cortex_m_common *cortex_m = target_to_cm(target);
3204  struct armv7m_common *armv7m = &cortex_m->armv7m;
3205  uint32_t demcr = 0;
3206  int retval;
3207 
3208  static const struct {
3209  char name[10];
3210  unsigned int mask;
3211  } vec_ids[] = {
3212  { "hard_err", VC_HARDERR, },
3213  { "int_err", VC_INTERR, },
3214  { "bus_err", VC_BUSERR, },
3215  { "state_err", VC_STATERR, },
3216  { "chk_err", VC_CHKERR, },
3217  { "nocp_err", VC_NOCPERR, },
3218  { "mm_err", VC_MMERR, },
3219  { "reset", VC_CORERESET, },
3220  };
3221 
3222  retval = cortex_m_verify_pointer(CMD, cortex_m);
3223  if (retval != ERROR_OK)
3224  return retval;
3225 
3226  if (!target_was_examined(target)) {
3227  LOG_TARGET_ERROR(target, "Target not examined yet");
3228  return ERROR_FAIL;
3229  }
3230 
3231  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
3232  if (retval != ERROR_OK)
3233  return retval;
3234 
3235  if (CMD_ARGC > 0) {
3236  unsigned int catch = 0;
3237 
3238  if (CMD_ARGC == 1) {
3239  if (strcmp(CMD_ARGV[0], "all") == 0) {
3240  catch = VC_HARDERR | VC_INTERR | VC_BUSERR
3242  | VC_MMERR | VC_CORERESET;
3243  goto write;
3244  } else if (strcmp(CMD_ARGV[0], "none") == 0)
3245  goto write;
3246  }
3247  while (CMD_ARGC-- > 0) {
3248  unsigned int i;
3249  for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3250  if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
3251  continue;
3252  catch |= vec_ids[i].mask;
3253  break;
3254  }
3255  if (i == ARRAY_SIZE(vec_ids)) {
3256  LOG_TARGET_ERROR(target, "No Cortex-M vector '%s'", CMD_ARGV[CMD_ARGC]);
3258  }
3259  }
3260 write:
3261  /* For now, armv7m->demcr only stores vector catch flags. */
3262  armv7m->demcr = catch;
3263 
3264  demcr &= ~0xffff;
3265  demcr |= catch;
3266 
3267  /* write, but don't assume it stuck (why not??) */
3268  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, demcr);
3269  if (retval != ERROR_OK)
3270  return retval;
3271  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
3272  if (retval != ERROR_OK)
3273  return retval;
3274 
3275  /* FIXME be sure to clear DEMCR on clean server shutdown.
3276  * Otherwise the vector catch hardware could fire when there's
3277  * no debugger hooked up, causing much confusion...
3278  */
3279  }
3280 
3281  for (unsigned int i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3282  command_print(CMD, "%9s: %s", vec_ids[i].name,
3283  (demcr & vec_ids[i].mask) ? "catch" : "ignore");
3284  }
3285 
3286  return ERROR_OK;
3287 }
3288 
3289 COMMAND_HANDLER(handle_cortex_m_mask_interrupts_command)
3290 {
3292  struct cortex_m_common *cortex_m = target_to_cm(target);
3293  int retval;
3294 
3295  static const struct nvp nvp_maskisr_modes[] = {
3296  { .name = "auto", .value = CORTEX_M_ISRMASK_AUTO },
3297  { .name = "off", .value = CORTEX_M_ISRMASK_OFF },
3298  { .name = "on", .value = CORTEX_M_ISRMASK_ON },
3299  { .name = "steponly", .value = CORTEX_M_ISRMASK_STEPONLY },
3300  { .name = NULL, .value = -1 },
3301  };
3302  const struct nvp *n;
3303 
3304 
3305  retval = cortex_m_verify_pointer(CMD, cortex_m);
3306  if (retval != ERROR_OK)
3307  return retval;
3308 
3309  if (target->state != TARGET_HALTED) {
3310  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3311  return ERROR_TARGET_NOT_HALTED;
3312  }
3313 
3314  if (CMD_ARGC > 0) {
3315  n = nvp_name2value(nvp_maskisr_modes, CMD_ARGV[0]);
3316  if (!n->name)
3318  cortex_m->isrmasking_mode = n->value;
3320  }
3321 
3322  n = nvp_value2name(nvp_maskisr_modes, cortex_m->isrmasking_mode);
3323  command_print(CMD, "cortex_m interrupt mask %s", n->name);
3324 
3325  return ERROR_OK;
3326 }
3327 
3328 COMMAND_HANDLER(handle_cortex_m_reset_config_command)
3329 {
3331  struct cortex_m_common *cortex_m = target_to_cm(target);
3332 
3333  int retval = cortex_m_verify_pointer(CMD, cortex_m);
3334  if (retval != ERROR_OK)
3335  return retval;
3336 
3337  if (!CMD_ARGC) {
3338  char *reset_config;
3339 
3340  switch (cortex_m->soft_reset_config) {
3342  reset_config = "sysresetreq";
3343  break;
3344 
3346  reset_config = "vectreset";
3347  break;
3348 
3349  default:
3350  reset_config = "unknown";
3351  break;
3352  }
3353 
3354  command_print(CMD, "%s", reset_config);
3355  return ERROR_OK;
3356  } else if (CMD_ARGC != 1) {
3358  }
3359 
3360  if (!strcmp(CMD_ARGV[0], "sysresetreq")) {
3362  } else if (!strcmp(CMD_ARGV[0], "vectreset")) {
3364  && !cortex_m->vectreset_supported)
3365  LOG_TARGET_WARNING(target, "VECTRESET is not supported on your Cortex-M core");
3366  else
3368  } else {
3369  command_print(CMD, "invalid reset config '%s'", CMD_ARGV[0]);
3371  }
3372 
3373  return ERROR_OK;
3374 }
3375 
3376 COMMAND_HANDLER(handle_cortex_m_cache_info_command)
3377 {
3378  if (CMD_ARGC)
3380 
3382 
3384 }
3385 
3386 static const struct command_registration cortex_m_exec_command_handlers[] = {
3387  {
3388  .name = "maskisr",
3389  .handler = handle_cortex_m_mask_interrupts_command,
3390  .mode = COMMAND_EXEC,
3391  .help = "mask cortex_m interrupts",
3392  .usage = "['auto'|'on'|'off'|'steponly']",
3393  },
3394  {
3395  .name = "vector_catch",
3396  .handler = handle_cortex_m_vector_catch_command,
3397  .mode = COMMAND_EXEC,
3398  .help = "configure hardware vectors to trigger debug entry",
3399  .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
3400  },
3401  {
3402  .name = "reset_config",
3403  .handler = handle_cortex_m_reset_config_command,
3404  .mode = COMMAND_ANY,
3405  .help = "configure software reset handling",
3406  .usage = "['sysresetreq'|'vectreset']",
3407  },
3408  {
3409  .name = "cache_info",
3410  .handler = handle_cortex_m_cache_info_command,
3411  .mode = COMMAND_EXEC,
3412  .help = "display information about target caches",
3413  .usage = "",
3414  },
3415  {
3416  .chain = smp_command_handlers,
3417  },
3419 };
3420 static const struct command_registration cortex_m_command_handlers[] = {
3421  {
3423  },
3424  {
3426  },
3427  /* START_DEPRECATED_TPIU */
3428  {
3430  },
3431  /* END_DEPRECATED_TPIU */
3432  {
3433  .name = "cortex_m",
3434  .mode = COMMAND_EXEC,
3435  .help = "Cortex-M command group",
3436  .usage = "",
3438  },
3439  {
3441  },
3443 };
3444 
3445 struct target_type cortexm_target = {
3446  .name = "cortex_m",
3447 
3448  .poll = cortex_m_poll,
3449  .arch_state = armv7m_arch_state,
3450 
3451  .target_request_data = cortex_m_target_request_data,
3452 
3453  .halt = cortex_m_halt,
3454  .resume = cortex_m_resume,
3455  .step = cortex_m_step,
3456 
3457  .assert_reset = cortex_m_assert_reset,
3458  .deassert_reset = cortex_m_deassert_reset,
3459  .soft_reset_halt = cortex_m_soft_reset_halt,
3460 
3461  .get_gdb_arch = arm_get_gdb_arch,
3462  .get_gdb_reg_list = armv7m_get_gdb_reg_list,
3463 
3464  .read_memory = cortex_m_read_memory,
3465  .write_memory = cortex_m_write_memory,
3466  .checksum_memory = armv7m_checksum_memory,
3467  .blank_check_memory = armv7m_blank_check_memory,
3468 
3469  .run_algorithm = armv7m_run_algorithm,
3470  .start_algorithm = armv7m_start_algorithm,
3471  .wait_algorithm = armv7m_wait_algorithm,
3472 
3473  .add_breakpoint = cortex_m_add_breakpoint,
3474  .remove_breakpoint = cortex_m_remove_breakpoint,
3475  .add_watchpoint = cortex_m_add_watchpoint,
3476  .remove_watchpoint = cortex_m_remove_watchpoint,
3477  .hit_watchpoint = cortex_m_hit_watchpoint,
3478 
3479  .commands = cortex_m_command_handlers,
3480  .target_create = cortex_m_target_create,
3481  .target_jim_configure = adiv5_jim_configure,
3482  .init_target = cortex_m_init_target,
3483  .examine = cortex_m_examine,
3484  .deinit_target = cortex_m_deinit_target,
3485 
3486  .profiling = cortex_m_profiling,
3487 };
@ ARM_ARCH_V6M
Definition: arm.h:56
@ ARM_ARCH_V8M
Definition: arm.h:58
@ ARM_ARCH_V7M
Definition: arm.h:57
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1283
@ ARM_MODE_HANDLER
Definition: arm.h:96
@ ARM_MODE_ANY
Definition: arm.h:106
@ ARM_MODE_USER_THREAD
Definition: arm.h:95
@ ARM_MODE_THREAD
Definition: arm.h:94
const char * arm_mode_name(unsigned int 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:730
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:742
int adiv5_verify_config(struct adiv5_private_config *pc)
Definition: arm_adi_v5.c:2494
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:245
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:297
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2489
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:1115
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:748
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:865
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:274
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1197
int dap_put_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1217
int mem_ap_init(struct adiv5_ap *ap)
Initialize a DAP.
Definition: arm_adi_v5.c:896
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:736
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:326
This defines formats and data structures used to talk to ADIv5 entities.
@ AP_TYPE_AHB3_AP
Definition: arm_adi_v5.h:490
@ AP_TYPE_AHB5_AP
Definition: arm_adi_v5.h:493
#define DP_APSEL_INVALID
Definition: arm_adi_v5.h:110
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:648
Macros used to generate various ARM or Thumb opcodes.
#define ARMV5_T_BKPT(im)
Definition: arm_opcodes.h:313
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:496
int armv7m_maybe_skip_bkpt_inst(struct target *target, bool *inst_found)
Definition: armv7m.c:1108
void armv7m_free_reg_cache(struct target *target)
Definition: armv7m.c:868
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:274
struct reg_cache * armv7m_build_reg_cache(struct target *target)
Builds cache of architecturally defined registers.
Definition: armv7m.c:801
const int armv7m_msp_reg_map[ARMV7M_NUM_CORE_REGS]
Definition: armv7m.c:60
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, unsigned int timeout_ms, void *arch_info)
Runs a Thumb algorithm in the target.
Definition: armv7m.c:520
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:924
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, unsigned int timeout_ms, void *arch_info)
Waits for an algorithm in the target.
Definition: armv7m.c:660
bool armv7m_map_reg_packing(unsigned int arm_reg_id, unsigned int *reg32_id, uint32_t *offset)
Definition: armv7m.c:321
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:975
int armv7m_arch_state(struct target *target)
Logs summary of ARMv7-M state for a halted target.
Definition: armv7m.c:767
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:196
const char * armv7m_exception_string(int number)
Maps ISR number (from xPSR) to name.
Definition: armv7m.c:232
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:545
const struct command_registration armv7m_command_handlers[]
Definition: armv7m.c:1142
int armv7m_init_arch_info(struct target *target, struct armv7m_common *armv7m)
Sets up target as a generic ARMv7-M core.
Definition: armv7m.c:902
@ ARMV7M_PRIMASK
Definition: armv7m.h:148
@ ARMV7M_FAULTMASK
Definition: armv7m.h:150
@ ARMV8M_BASEPRI_NS
Definition: armv7m.h:179
@ ARMV7M_FPU_LAST_REG
Definition: armv7m.h:213
@ ARMV7M_CORE_FIRST_REG
Definition: armv7m.h:210
@ ARMV7M_BASEPRI
Definition: armv7m.h:149
@ ARMV8M_MSPLIM_NS
Definition: armv7m.h:163
@ ARMV7M_CONTROL
Definition: armv7m.h:151
@ ARMV8M_TZ_FIRST_REG
Definition: armv7m.h:214
@ ARMV8M_TZ_LAST_REG
Definition: armv7m.h:215
@ ARMV7M_FPU_FIRST_REG
Definition: armv7m.h:212
@ ARMV8M_BASEPRI_S
Definition: armv7m.h:170
@ ARMV8M_FAULTMASK_S
Definition: armv7m.h:171
@ ARMV8M_PSPLIM
Definition: armv7m.h:156
@ ARMV8M_MSPLIM
Definition: armv7m.h:155
@ ARMV8M_PSPLIM_NS
Definition: armv7m.h:164
@ ARMV8M_FAULTMASK_NS
Definition: armv7m.h:180
@ ARMV7M_LAST_REG
Definition: armv7m.h:209
@ ARMV8M_VPR
Definition: armv7m.h:206
static struct armv7m_common * target_to_armv7m(struct target *target)
Definition: armv7m.h:273
@ FPV4_SP
Definition: armv7m.h:220
@ FPV5_MVE_F
Definition: armv7m.h:224
@ FPV5_DP
Definition: armv7m.h:222
@ FPV5_SP
Definition: armv7m.h:221
@ FPV5_MVE_I
Definition: armv7m.h:223
@ FP_NONE
Definition: armv7m.h:219
int armv7m_handle_cache_info_command(struct command_invocation *cmd, struct target *target)
Definition: armv7m_cache.c:290
int armv7m_i_cache_inval(struct target *target, uint32_t address, unsigned int length)
Definition: armv7m_cache.c:266
int armv7m_d_cache_flush(struct target *target, uint32_t address, unsigned int length)
Definition: armv7m_cache.c:242
int armv7m_deferred_identify_cache(struct target *target)
Definition: armv7m_cache.c:225
int armv7m_identify_cache(struct target *target)
Definition: armv7m_cache.c:207
const struct command_registration armv7m_trace_command_handlers[]
Definition: armv7m_trace.c:146
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 int 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 int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:472
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:81
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:65
@ 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:371
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:166
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define MVFR0_SP_MASK
Definition: cortex_m.c:2747
static int cortex_m_debug_entry(struct target *target)
Definition: cortex_m.c:845
static void cortex_m_dwt_free(struct target *target)
Definition: cortex_m.c:2586
static int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2164
COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
Definition: cortex_m.c:3200
static int cortex_m_set_maskints_for_run(struct target *target)
Definition: cortex_m.c:497
int cortex_m_security_restore(struct target *target, struct cortex_m_saved_security *ssec)
Restores saved security context to MPU_CTRL, SAU_CTRL and DSCSR.
Definition: cortex_m.c:2707
static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp, struct adiv5_ap **debug_ap)
Definition: cortex_m.c:2757
static int cortex_m_dwt_get_reg(struct reg *reg)
Definition: cortex_m.c:2431
#define MVFR1_MVE_F
Definition: cortex_m.c:2755
static int cortex_m_poll_smp(struct list_head *smp_targets)
Definition: cortex_m.c:1166
static int cortex_m_restore_smp(struct target *target, bool handle_breakpoints)
Definition: cortex_m.c:1454
static int cortex_m_restart_one(struct target *target, bool debug_execution)
Definition: cortex_m.c:1431
static int cortex_m_enable_fpb(struct target *target)
Definition: cortex_m.c:589
static int cortex_m_erratum_check_breakpoint(struct target *target)
Definition: cortex_m.c:812
static int cortex_m_single_step_core(struct target *target)
Definition: cortex_m.c:564
static int cortex_m_write_debug_halt_mask(struct target *target, uint32_t mask_on, uint32_t mask_off)
Definition: cortex_m.c:450
static int cortex_m_set_maskints_for_halt(struct target *target)
Definition: cortex_m.c:473
struct target_type cortexm_target
Definition: cortex_m.c:3445
static int cortex_m_halt_one(struct target *target)
Definition: cortex_m.c:1222
static int cortex_m_clear_halt(struct target *target)
Definition: cortex_m.c:541
#define DHCSR_S_REGRDY_TIMEOUT
Definition: cortex_m.c:50
static const struct command_registration cortex_m_command_handlers[]
Definition: cortex_m.c:3420
static int cortex_m_set_maskints_for_step(struct target *target)
Definition: cortex_m.c:519
static int cortex_m_poll(struct target *target)
Definition: cortex_m.c:1206
void cortex_m_enable_watchpoints(struct target *target)
Definition: cortex_m.c:2288
static int cortex_m_target_request_data(struct target *target, uint32_t size, uint8_t *buffer)
Definition: cortex_m.c:3078
static int cortex_m_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: cortex_m.c:2328
static int cortex_m_examine_exception_reason(struct target *target)
Definition: cortex_m.c:733
int cortex_m_set_secure(struct target *target, struct cortex_m_saved_security *ssec)
Forces Cortex-M core to the basic secure context with SAU and MPU off.
Definition: cortex_m.c:2639
static int cortex_m_poll_one(struct target *target)
Definition: cortex_m.c:968
int cortex_m_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2197
static int cortex_m_store_core_reg_u32(struct target *target, uint32_t num, uint32_t value)
Definition: cortex_m.c:400
static int cortex_m_slow_read_all_regs(struct target *target)
Definition: cortex_m.c:233
static int cortex_m_init_arch_info(struct target *target, struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
Definition: cortex_m.c:3132
#define MVFR0_DP
Definition: cortex_m.c:2750
static int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2082
void cortex_m_enable_breakpoints(struct target *target)
Definition: cortex_m.c:1325
int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:2074
static int cortex_m_endreset_event(struct target *target)
Definition: cortex_m.c:607
static int cortex_m_smp_post_halt_poll(struct list_head *smp_targets)
Definition: cortex_m.c:1146
#define MVFR0_DP_MASK
Definition: cortex_m.c:2749
static int cortex_m_smp_halt_all(struct list_head *smp_targets)
Definition: cortex_m.c:1127
static int cortex_m_examine_debug_reason(struct target *target)
Definition: cortex_m.c:707
static int cortex_m_handle_target_request(void *priv)
Definition: cortex_m.c:3095
static int cortex_m_fast_read_all_regs(struct target *target)
Definition: cortex_m.c:275
static int cortex_m_deassert_reset(struct target *target)
Definition: cortex_m.c:1869
static int cortex_m_target_create(struct target *target)
Definition: cortex_m.c:3161
#define MVFR1
Definition: cortex_m.c:2752
static const struct reg_arch_type dwt_reg_type
Definition: cortex_m.c:2491
static int cortex_m_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_m.c:1483
static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
Definition: cortex_m.c:2496
static int cortex_m_load_core_reg_u32(struct target *target, uint32_t regsel, uint32_t *value)
Definition: cortex_m.c:178
int cortex_m_examine(struct target *target)
Definition: cortex_m.c:2766
#define MVFR1_MVE_I
Definition: cortex_m.c:2754
#define MVFR1_MVE_MASK
Definition: cortex_m.c:2753
int cortex_m_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2240
static int cortex_m_assert_reset(struct target *target)
Definition: cortex_m.c:1708
int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:2050
static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Definition: cortex_m.c:2259
static const struct cortex_m_part_info cortex_m_parts[]
Definition: cortex_m.c:53
static int cortex_m_verify_pointer(struct command_invocation *cmd, struct cortex_m_common *cm)
Definition: cortex_m.c:3185
static int cortex_m_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: cortex_m.c:1512
static const struct command_registration cortex_m_exec_command_handlers[]
Definition: cortex_m.c:3386
static bool cortex_m_main_extension(struct target *target, uint32_t cpuid)
Definition: cortex_m.c:2624
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:2314
static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
Definition: cortex_m.c:3049
int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1898
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:164
static int cortex_m_set_maskints(struct target *target, bool mask)
Definition: cortex_m.c:464
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:155
static bool cortex_m_has_tz(struct target *target)
Definition: cortex_m.c:2608
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:2300
static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
Definition: cortex_m.c:2514
void cortex_m_deinit_target(struct target *target)
Definition: cortex_m.c:2336
int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1996
#define MVFR0
Definition: cortex_m.c:2746
static int cortex_m_soft_reset_halt(struct target *target)
Definition: cortex_m.c:1260
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:2353
static const struct dwt_reg dwt_comp[]
Definition: cortex_m.c:2467
#define MVFR0_SP
Definition: cortex_m.c:2748
static int cortex_m_halt(struct target *target)
Definition: cortex_m.c:1252
#define DWT_COMPARATOR(i)
static int cortex_m_restore_one(struct target *target, bool current, target_addr_t *address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_m.c:1337
static const struct dwt_reg dwt_base_regs[]
Definition: cortex_m.c:2458
static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel, uint32_t *reg_value, uint32_t *dhcsr)
Definition: cortex_m.c:258
static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
Definition: cortex_m.c:2444
#define DWT_CYCCNT
Definition: cortex_m.h:97
#define FPCR_REPLACE_BKPT_HIGH
Definition: cortex_m.h:241
#define DSCSR_CDS
Definition: cortex_m.h:199
#define NVIC_HFSR
Definition: cortex_m.h:211
#define VC_CHKERR
Definition: cortex_m.h:192
#define DWT_PCSR
Definition: cortex_m.h:98
#define S_REGRDY
Definition: cortex_m.h:179
#define DFSR_EXTERNAL
Definition: cortex_m.h:232
#define SAU_CTRL
Definition: cortex_m.h:216
#define DWT_DEVARCH_ARMV8M_V2_1
Definition: cortex_m.h:105
#define DBGKEY
Definition: cortex_m.h:174
#define S_LOCKUP
Definition: cortex_m.h:182
#define FP_CTRL
Definition: cortex_m.h:107
#define CCR_DC_MASK
Definition: cortex_m.h:133
#define ARM_CPUID_IMPLEMENTER_MASK
Definition: cortex_m.h:36
#define FPCR_REPLACE_BKPT_LOW
Definition: cortex_m.h:240
#define NVIC_SFAR
Definition: cortex_m.h:218
#define ARM_CPUID_NO_MAIN_EXTENSION
Definition: cortex_m.h:43
#define DFSR_BKPT
Definition: cortex_m.h:229
#define CCR_IC_MASK
Definition: cortex_m.h:132
#define CORTEX_M_F_HAS_FPV4
Definition: cortex_m.h:73
#define SAU_CTRL_ENABLE
Definition: cortex_m.h:235
#define AIRCR_VECTKEY
Definition: cortex_m.h:221
#define ARM_CPUID_MAIN_EXTENSION
Definition: cortex_m.h:42
#define S_RETIRE_ST
Definition: cortex_m.h:183
#define ARM_CPUID_ARCHITECTURE_MASK
Definition: cortex_m.h:41
#define DCB_DSCSR
Definition: cortex_m.h:89
#define CORTEX_M_F_HAS_FPV5
Definition: cortex_m.h:74
#define NVIC_SFSR
Definition: cortex_m.h:217
#define CORTEX_M_COMMON_MAGIC
Definition: cortex_m.h:21
#define AIRCR_SYSRESETREQ
Definition: cortex_m.h:222
#define S_RESET_ST
Definition: cortex_m.h:184
#define C_MASKINTS
Definition: cortex_m.h:178
#define FPCR_LITERAL
Definition: cortex_m.h:238
#define ARM_CPUID_ARCHITECTURE_POS
Definition: cortex_m.h:40
#define NVIC_CFSR
Definition: cortex_m.h:207
#define CPUID
Definition: cortex_m.h:33
#define DFSR_DWTTRAP
Definition: cortex_m.h:230
#define C_HALT
Definition: cortex_m.h:176
#define VC_NOCPERR
Definition: cortex_m.h:193
#define DCB_DCRSR
Definition: cortex_m.h:86
#define VC_INTERR
Definition: cortex_m.h:189
static bool is_cortex_m_with_dap_access(const struct cortex_m_common *cortex_m)
Definition: cortex_m.h:331
#define DWT_CTRL
Definition: cortex_m.h:96
#define VC_BUSERR
Definition: cortex_m.h:190
#define VC_CORERESET
Definition: cortex_m.h:195
#define DWT_COMP0
Definition: cortex_m.h:99
#define DCRSR_WNR
Definition: cortex_m.h:94
#define NVIC_MMFAR
Definition: cortex_m.h:213
#define DCB_DEMCR
Definition: cortex_m.h:88
#define C_DEBUGEN
Definition: cortex_m.h:175
#define DAUTHSTATUS_SID_MASK
Definition: cortex_m.h:92
#define DCB_DCRDR
Definition: cortex_m.h:87
#define NVIC_BFAR
Definition: cortex_m.h:214
#define ARM_CPUID_PARTNO_MASK
Definition: cortex_m.h:38
#define MPU_CTRL_ENABLE
Definition: cortex_m.h:234
#define C_STEP
Definition: cortex_m.h:177
#define DSCSR_CDSKEY
Definition: cortex_m.h:198
#define FPCR_CODE
Definition: cortex_m.h:237
#define DWT_DEVARCH
Definition: cortex_m.h:102
#define MPU_CTRL
Definition: cortex_m.h:215
static struct cortex_m_common * target_to_cm(struct target *target)
Definition: cortex_m.h:346
#define S_HALT
Definition: cortex_m.h:180
#define S_SLEEP
Definition: cortex_m.h:181
#define AIRCR_VECTRESET
Definition: cortex_m.h:224
#define NVIC_DFSR
Definition: cortex_m.h:212
@ CORTEX_M_ISRMASK_OFF
Definition: cortex_m.h:266
@ CORTEX_M_ISRMASK_ON
Definition: cortex_m.h:267
@ CORTEX_M_ISRMASK_STEPONLY
Definition: cortex_m.h:268
@ CORTEX_M_ISRMASK_AUTO
Definition: cortex_m.h:265
#define VC_STATERR
Definition: cortex_m.h:191
#define NVIC_AIRCR
Definition: cortex_m.h:205
#define CCR
Definition: cortex_m.h:124
#define DFSR_VCATCH
Definition: cortex_m.h:231
#define FP_COMP0
Definition: cortex_m.h:109
#define DCB_DHCSR
Definition: cortex_m.h:85
#define DWT_DEVARCH_ARMV8M_V2_0
Definition: cortex_m.h:104
#define TRCENA
Definition: cortex_m.h:187
@ CORTEX_M52_PARTNO
Definition: cortex_m.h:64
@ CORTEX_M85_PARTNO
Definition: cortex_m.h:66
@ CORTEX_M7_PARTNO
Definition: cortex_m.h:59
@ INFINEON_SLX2_PARTNO
Definition: cortex_m.h:67
@ CORTEX_M35P_PARTNO
Definition: cortex_m.h:63
@ CORTEX_M4_PARTNO
Definition: cortex_m.h:58
@ STAR_MC1_PARTNO
Definition: cortex_m.h:54
@ CORTEX_M33_PARTNO
Definition: cortex_m.h:62
@ CORTEX_M1_PARTNO
Definition: cortex_m.h:56
@ CORTEX_M0_PARTNO
Definition: cortex_m.h:55
@ CORTEX_M0P_PARTNO
Definition: cortex_m.h:60
@ REALTEK_M200_PARTNO
Definition: cortex_m.h:68
@ CORTEX_M55_PARTNO
Definition: cortex_m.h:65
@ REALTEK_M300_PARTNO
Definition: cortex_m.h:69
@ CORTEX_M23_PARTNO
Definition: cortex_m.h:61
@ CORTEX_M3_PARTNO
Definition: cortex_m.h:57
#define CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K
Definition: cortex_m.h:75
#define VC_HARDERR
Definition: cortex_m.h:188
cortex_m_soft_reset_config
Definition: cortex_m.h:259
@ CORTEX_M_RESET_VECTRESET
Definition: cortex_m.h:261
@ CORTEX_M_RESET_SYSRESETREQ
Definition: cortex_m.h:260
#define DFSR_HALTED
Definition: cortex_m.h:228
#define DAUTHSTATUS
Definition: cortex_m.h:91
#define NVIC_SHCSR
Definition: cortex_m.h:206
#define VC_MMERR
Definition: cortex_m.h:194
static int halted(struct target *target, const char *label)
Definition: davinci.c:58
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
static enum reset_types jtag_reset_config
Definition: jtag/core.c:89
int adapter_deassert_reset(void)
Definition: jtag/core.c:1912
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1075
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1747
int adapter_assert_reset(void)
Definition: jtag/core.c:1892
reset_types
Definition: jtag.h:215
@ RESET_SRST_NO_GATING
Definition: jtag.h:224
@ RESET_HAS_SRST
Definition: jtag.h:218
#define list_last_entry(ptr, type, member)
Definition: list.h:134
void alive_sleep(uint64_t ms)
Definition: log.c:470
void keep_alive(void)
Definition: log.c:429
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:154
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:160
#define ERROR_FAIL
Definition: log.h:175
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:163
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:151
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define ERROR_TIMEOUT_REACHED
Definition: log.h:178
#define ERROR_OK
Definition: log.h:169
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
uint8_t mask
Definition: parport.c:70
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 target * target
Definition: rtt/rtt.c:26
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
const struct command_registration rtt_target_command_handlers[]
Definition: rtt/tcl.c:267
const struct command_registration smp_command_handlers[]
Definition: smp.c:153
#define foreach_smp_target(pos, head)
Definition: smp.h:15
#define BIT(nr)
Definition: stm32l4x.h:18
static const char * str_enabled_disabled(bool value)
This represents an ARM Debug Interface (v5) Access Port (AP).
Definition: arm_adi_v5.h:250
uint32_t tar_autoincr_block
Definition: arm_adi_v5.h:309
struct adiv5_dap * dap
DAP this AP belongs to.
Definition: arm_adi_v5.h:254
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:306
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
Definition: arm_adi_v5.h:348
struct adiv5_dap * dap
Definition: arm_adi_v5.h:787
Represents a generic ARM core, with standard application registers.
Definition: arm.h:176
enum arm_arch arch
ARM architecture version.
Definition: arm.h:203
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:197
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:185
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
Definition: arm.h:258
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:182
const int * map
Support for arm_reg_current()
Definition: arm.h:191
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:225
struct reg_cache * core_cache
Definition: arm.h:179
bool is_hla_target
Definition: armv7m.h:245
int exception_number
Definition: armv7m.h:236
int fp_feature
Definition: armv7m.h:241
void(* pre_restore_context)(struct target *target)
Definition: armv7m.h:258
struct arm arm
Definition: armv7m.h:234
int(* store_core_reg_u32)(struct target *target, uint32_t regsel, uint32_t value)
Definition: armv7m.h:253
int(* load_core_reg_u32)(struct target *target, uint32_t regsel, uint32_t *value)
Definition: armv7m.h:252
uint32_t demcr
Definition: armv7m.h:242
struct adiv5_ap * debug_ap
Definition: armv7m.h:239
int(* examine_debug_reason)(struct target *target)
Definition: armv7m.h:255
struct armv7m_cache_common armv7m_cache
Definition: armv7m.h:247
int(* post_debug_entry)(struct target *target)
Definition: armv7m.h:256
struct breakpoint * next
Definition: breakpoints.h:34
unsigned int length
Definition: breakpoints.h:29
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:234
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:247
const struct cortex_m_part_info * core_info
Definition: cortex_m.h:302
enum cortex_m_soft_reset_config soft_reset_config
Definition: cortex_m.h:298
struct armv7m_common armv7m
Definition: cortex_m.h:274
uint64_t apsel
Definition: cortex_m.h:306
unsigned int dwt_comp_available
Definition: cortex_m.h:293
unsigned int dwt_num_comp
Definition: cortex_m.h:292
uint32_t dcb_dhcsr
Definition: cortex_m.h:277
bool fpb_enabled
Definition: cortex_m.h:288
struct cortex_m_dwt_comparator * dwt_comparator_list
Definition: cortex_m.h:295
bool incorrect_halt_erratum
Definition: cortex_m.h:314
bool slow_register_read
Definition: cortex_m.h:304
bool dcb_dhcsr_sticky_is_recent
Definition: cortex_m.h:280
struct cortex_m_fp_comparator * fp_comparator_list
Definition: cortex_m.h:289
struct reg_cache * dwt_cache
Definition: cortex_m.h:296
unsigned int fp_num_lit
Definition: cortex_m.h:285
bool vectreset_supported
Definition: cortex_m.h:299
uint32_t dwt_devarch
Definition: cortex_m.h:294
uint32_t nvic_dfsr
Definition: cortex_m.h:281
unsigned int fp_num_code
Definition: cortex_m.h:286
bool maskints_erratum
Definition: cortex_m.h:310
enum cortex_m_isrmasking_mode isrmasking_mode
Definition: cortex_m.h:300
uint32_t nvic_icsr
Definition: cortex_m.h:282
unsigned int common_magic
Definition: cortex_m.h:272
uint32_t dcb_dhcsr_cumulated_sticky
Definition: cortex_m.h:278
uint32_t dwt_comparator_address
Definition: cortex_m.h:256
enum arm_arch arch
Definition: cortex_m.h:80
const char * name
Definition: cortex_m.h:79
enum cortex_m_impl_part impl_part
Definition: cortex_m.h:78
uint32_t flags
Definition: cortex_m.h:81
uint8_t value[4]
Definition: cortex_m.c:2428
struct target * target
Definition: cortex_m.c:2426
uint32_t addr
Definition: cortex_m.c:2427
const char * name
Definition: cortex_m.c:2454
uint32_t addr
Definition: cortex_m.c:2453
unsigned int size
Definition: cortex_m.c:2455
struct target * target
Definition: target.h:98
Definition: list.h:41
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
int(* get)(struct reg *reg)
Definition: register.h:152
const char * name
Definition: register.h:145
unsigned int 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
struct target * target
Definition: target.h:217
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:119
struct gdb_service * gdb_service
Definition: target.h:202
bool dbg_msg_enabled
Definition: target.h:166
enum target_debug_reason debug_reason
Definition: target.h:157
enum target_state state
Definition: target.h:160
void * private_config
Definition: target.h:168
struct reg_cache * reg_cache
Definition: target.h:161
struct list_head * smp_targets
Definition: target.h:191
struct breakpoint * breakpoints
Definition: target.h:162
unsigned int smp
Definition: target.h:190
struct watchpoint * watchpoints
Definition: target.h:163
bool smp_halt_event_postponed
Definition: target.h:194
bool reset_halt
Definition: target.h:147
bool defer_examine
Should we defer examine to later.
Definition: target.h:126
Definition: psoc6.c:83
uint64_t mask
Definition: breakpoints.h:44
enum watchpoint_rw rw
Definition: breakpoints.h:46
bool is_set
Definition: breakpoints.h:47
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
int unique_id
Definition: breakpoints.h:50
unsigned int number
Definition: breakpoints.h:48
target_addr_t address
Definition: breakpoints.h:42
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1774
void target_free_all_working_areas(struct target *target)
Definition: target.c:2160
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:379
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:2299
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:1275
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:682
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:269
int target_poll(struct target *target)
Definition: target.c:486
static int srst_asserted
Definition: target.c:2857
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:1668
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2583
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:565
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:343
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:1247
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4841
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4664
@ DBG_REASON_WPTANDBKPT
Definition: target.h:75
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
static bool target_was_examined(const struct target *target)
Definition: target.h:432
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:788
#define ERROR_TARGET_INVALID
Definition: target.h:783
@ TARGET_TIMER_TYPE_PERIODIC
Definition: target.h:323
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:275
@ TARGET_EVENT_HALTED
Definition: target.h:255
@ TARGET_EVENT_RESUMED
Definition: target.h:256
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:274
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:267
#define ERROR_TARGET_HALTED_DO_RESUME
Definition: target.h:798
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:793
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:790
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:439
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:286
#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:279
#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 @135 vec_ids[]