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
879  uint32_t ccr = 0;
880  if (armv7m->armv7m_cache.info_valid) {
881  retval = mem_ap_read_u32(armv7m->debug_ap, CCR, &ccr);
882  if (retval != ERROR_OK)
883  return retval;
884  }
885 
886  /* Load all registers to arm.core_cache */
887  if (!cortex_m->slow_register_read) {
889  if (retval == ERROR_TIMEOUT_REACHED) {
890  cortex_m->slow_register_read = true;
891  LOG_TARGET_DEBUG(target, "Switched to slow register read");
892  }
893  }
894 
895  if (cortex_m->slow_register_read)
897 
898  if (retval != ERROR_OK)
899  return retval;
900 
901  r = arm->cpsr;
902  xpsr = buf_get_u32(r->value, 0, 32);
903 
904  /* Are we in an exception handler */
905  if (xpsr & 0x1FF) {
906  armv7m->exception_number = (xpsr & 0x1FF);
907 
910  } else {
911  unsigned int control = buf_get_u32(arm->core_cache
912  ->reg_list[ARMV7M_CONTROL].value, 0, 3);
913 
914  /* is this thread privileged? */
915  arm->core_mode = control & 1
917  : ARM_MODE_THREAD;
918 
919  /* which stack is it using? */
920  if (control & 2)
922  else
924 
925  armv7m->exception_number = 0;
926  }
927 
928  if (armv7m->exception_number)
930 
931  bool secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
932  LOG_TARGET_DEBUG(target, "entered debug state in core mode: %s at PC 0x%" PRIx32
933  ", cpu in %s state, target->state: %s",
935  buf_get_u32(arm->pc->value, 0, 32),
936  secure_state ? "Secure" : "Non-Secure",
938 
939  if (armv7m->armv7m_cache.info_valid)
940  LOG_TARGET_DEBUG(target, "D-Cache %s, I-Cache %s",
943 
944  /* Errata 3092511 workaround
945  * Cortex-M7 can halt in an incorrect address when breakpoint
946  * and exception occurs simultaneously */
947  if (cortex_m->incorrect_halt_erratum
948  && armv7m->exception_number
949  && cortex_m->nvic_dfsr == (DFSR_BKPT | DFSR_HALTED)) {
951  if (retval != ERROR_OK)
952  return retval;
953  }
954 
955  if (armv7m->post_debug_entry) {
956  retval = armv7m->post_debug_entry(target);
957  if (retval != ERROR_OK)
958  return retval;
959  }
960 
961  return ERROR_OK;
962 }
963 
964 static int cortex_m_poll_one(struct target *target)
965 {
966  int detected_failure = ERROR_OK;
967  int retval = ERROR_OK;
968  enum target_state prev_target_state = target->state;
969  struct cortex_m_common *cortex_m = target_to_cm(target);
970  struct armv7m_common *armv7m = &cortex_m->armv7m;
971 
972  /* Read from Debug Halting Control and Status Register */
974  if (retval != ERROR_OK) {
976  return retval;
977  }
978 
979  /* Recover from lockup. See ARMv7-M architecture spec,
980  * section B1.5.15 "Unrecoverable exception cases".
981  */
982  if (cortex_m->dcb_dhcsr & S_LOCKUP) {
983  LOG_TARGET_ERROR(target, "clearing lockup after double fault");
986 
987  /* We have to execute the rest (the "finally" equivalent, but
988  * still throw this exception again).
989  */
990  detected_failure = ERROR_FAIL;
991 
992  /* refresh status bits */
994  if (retval != ERROR_OK)
995  return retval;
996  }
997 
998  if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
1000  if (target->state != TARGET_RESET) {
1002  LOG_TARGET_INFO(target, "external reset detected");
1003  /* In case of an unexpected S_RESET_ST set TARGET_RESET state
1004  * and keep it until the next poll to allow its detection */
1005  return ERROR_OK;
1006  }
1007 
1008  /* refresh status bits */
1010  if (retval != ERROR_OK)
1011  return retval;
1012 
1013  /* If still under reset, quit and re-check at next poll */
1014  if (cortex_m->dcb_dhcsr_cumulated_sticky & S_RESET_ST) {
1015  cortex_m->dcb_dhcsr_cumulated_sticky &= ~S_RESET_ST;
1016  return ERROR_OK;
1017  }
1018 
1019  /* S_RESET_ST was expected (in a reset command). Continue processing
1020  * to quickly get out of TARGET_RESET state */
1021  }
1022 
1023  if (target->state == TARGET_RESET) {
1024  /* Cannot switch context while running so endreset is
1025  * called with target->state == TARGET_RESET
1026  */
1027  LOG_TARGET_DEBUG(target, "Exit from reset with dcb_dhcsr 0x%" PRIx32,
1028  cortex_m->dcb_dhcsr);
1029  retval = cortex_m_endreset_event(target);
1030  if (retval != ERROR_OK) {
1032  return retval;
1033  }
1035  prev_target_state = TARGET_RUNNING;
1036  }
1037 
1038  if (cortex_m->dcb_dhcsr & S_HALT) {
1040 
1041  if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET)) {
1042  retval = cortex_m_debug_entry(target);
1043 
1044  /* Errata 3092511 workaround
1045  * Cortex-M7 can halt in an incorrect address when breakpoint
1046  * and exception occurs simultaneously */
1047  if (retval == ERROR_TARGET_HALTED_DO_RESUME) {
1048  struct arm *arm = &armv7m->arm;
1049  LOG_TARGET_INFO(target, "Resuming after incorrect halt @ PC 0x%08" PRIx32
1050  ", ARM Cortex-M7 erratum 3092511",
1051  buf_get_u32(arm->pc->value, 0, 32));
1052  /* We don't need to restore registers, just restart the core */
1055  if (retval != ERROR_OK)
1056  return retval;
1057 
1059  /* registers are now invalid */
1061 
1063  return ERROR_OK;
1064  }
1065 
1066  /* arm_semihosting needs to know registers, don't run if debug entry returned error */
1067  if (retval == ERROR_OK && arm_semihosting(target, &retval) != 0)
1068  return retval;
1069 
1070  if (target->smp) {
1071  LOG_TARGET_DEBUG(target, "postpone target event 'halted'");
1073  } else {
1074  /* regardless of errors returned in previous code update state */
1076  }
1077  }
1078  if (prev_target_state == TARGET_DEBUG_RUNNING) {
1079  retval = cortex_m_debug_entry(target);
1080 
1082  }
1083  if (retval != ERROR_OK)
1084  return retval;
1085  }
1086 
1087  if (target->state == TARGET_UNKNOWN) {
1088  /* Check if processor is retiring instructions or sleeping.
1089  * Unlike S_RESET_ST here we test if the target *is* running now,
1090  * not if it has been running (possibly in the past). Instructions are
1091  * typically processed much faster than OpenOCD polls DHCSR so S_RETIRE_ST
1092  * is read always 1. That's the reason not to use dcb_dhcsr_cumulated_sticky.
1093  */
1094  if (cortex_m->dcb_dhcsr & S_RETIRE_ST || cortex_m->dcb_dhcsr & S_SLEEP) {
1096  retval = ERROR_OK;
1097  }
1098  }
1099 
1100  /* Check that target is truly halted, since the target could be resumed externally */
1101  if ((prev_target_state == TARGET_HALTED) && !(cortex_m->dcb_dhcsr & S_HALT)) {
1102  /* registers are now invalid */
1104 
1106  LOG_TARGET_WARNING(target, "external resume detected");
1108  retval = ERROR_OK;
1109  }
1110 
1111  /* Did we detect a failure condition that we cleared? */
1112  if (detected_failure != ERROR_OK)
1113  retval = detected_failure;
1114  return retval;
1115 }
1116 
1117 static int cortex_m_halt_one(struct target *target);
1118 
1119 static int cortex_m_smp_halt_all(struct list_head *smp_targets)
1120 {
1121  int retval = ERROR_OK;
1122  struct target_list *head;
1123 
1124  foreach_smp_target(head, smp_targets) {
1125  struct target *curr = head->target;
1126  if (!target_was_examined(curr))
1127  continue;
1128  if (curr->state == TARGET_HALTED)
1129  continue;
1130 
1131  int ret2 = cortex_m_halt_one(curr);
1132  if (retval == ERROR_OK)
1133  retval = ret2; /* store the first error code ignore others */
1134  }
1135  return retval;
1136 }
1137 
1139 {
1140  int retval = ERROR_OK;
1141  struct target_list *head;
1142 
1143  foreach_smp_target(head, smp_targets) {
1144  struct target *curr = head->target;
1145  if (!target_was_examined(curr))
1146  continue;
1147  /* skip targets that were already halted */
1148  if (curr->state == TARGET_HALTED)
1149  continue;
1150 
1151  int ret2 = cortex_m_poll_one(curr);
1152  if (retval == ERROR_OK)
1153  retval = ret2; /* store the first error code ignore others */
1154  }
1155  return retval;
1156 }
1157 
1159 {
1160  int retval = ERROR_OK;
1161  struct target_list *head;
1162  bool halted = false;
1163 
1164  foreach_smp_target(head, smp_targets) {
1165  struct target *curr = head->target;
1166  if (curr->smp_halt_event_postponed) {
1167  halted = true;
1168  break;
1169  }
1170  }
1171 
1172  if (halted) {
1174 
1176  if (retval == ERROR_OK)
1177  retval = ret2; /* store the first error code ignore others */
1178 
1180  struct target *curr = head->target;
1181  if (!curr->smp_halt_event_postponed)
1182  continue;
1183 
1184  curr->smp_halt_event_postponed = false;
1185  if (curr->state == TARGET_HALTED) {
1186  LOG_TARGET_DEBUG(curr, "sending postponed target event 'halted'");
1188  }
1189  }
1190  /* There is no need to set gdb_service->target
1191  * as hwthread_update_threads() selects an interesting thread
1192  * by its own
1193  */
1194  }
1195  return retval;
1196 }
1197 
1198 static int cortex_m_poll(struct target *target)
1199 {
1200  int retval = cortex_m_poll_one(target);
1201 
1202  if (target->smp) {
1203  struct target_list *last;
1204  last = list_last_entry(target->smp_targets, struct target_list, lh);
1205  if (target == last->target)
1206  /* After the last target in SMP group has been polled
1207  * check for postponed halted events and eventually halt and re-poll
1208  * other targets */
1210  }
1211  return retval;
1212 }
1213 
1214 static int cortex_m_halt_one(struct target *target)
1215 {
1216  int retval;
1217  LOG_TARGET_DEBUG(target, "target->state: %s", target_state_name(target));
1218 
1219  if (!target_was_examined(target)) {
1220  LOG_TARGET_ERROR(target, "target non examined yet");
1222  }
1223 
1224  if (target->state == TARGET_HALTED) {
1225  LOG_TARGET_DEBUG(target, "target was already halted");
1226  return ERROR_OK;
1227  }
1228 
1229  if (target->state == TARGET_UNKNOWN)
1230  LOG_TARGET_WARNING(target, "target was in unknown state when halt was requested");
1231 
1232  /* Write to Debug Halting Control and Status Register */
1234 
1235  /* Do this really early to minimize the window where the MASKINTS erratum
1236  * can pile up pending interrupts. */
1238 
1240 
1241  return retval;
1242 }
1243 
1244 static int cortex_m_halt(struct target *target)
1245 {
1246  if (target->smp)
1248  else
1249  return cortex_m_halt_one(target);
1250 }
1251 
1253 {
1254  struct cortex_m_common *cortex_m = target_to_cm(target);
1255  struct armv7m_common *armv7m = &cortex_m->armv7m;
1256  int retval, timeout = 0;
1257 
1258  /* on single cortex_m MCU soft_reset_halt should be avoided as same functionality
1259  * can be obtained by using 'reset halt' and 'cortex_m reset_config vectreset'.
1260  * As this reset only uses VC_CORERESET it would only ever reset the cortex_m
1261  * core, not the peripherals */
1262  LOG_TARGET_DEBUG(target, "soft_reset_halt is discouraged, please use 'reset halt' instead.");
1263 
1264  if (!cortex_m->vectreset_supported) {
1265  LOG_TARGET_ERROR(target, "VECTRESET is not supported on this Cortex-M core");
1266  return ERROR_FAIL;
1267  }
1268 
1269  /* Set C_DEBUGEN */
1271  if (retval != ERROR_OK)
1272  return retval;
1273 
1274  /* Enter debug state on reset; restore DEMCR in endreset_event() */
1275  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
1277  if (retval != ERROR_OK)
1278  return retval;
1279 
1280  /* Request a core-only reset */
1281  retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1283  if (retval != ERROR_OK)
1284  return retval;
1286 
1287  /* registers are now invalid */
1289 
1290  while (timeout < 100) {
1292  if (retval == ERROR_OK) {
1293  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR,
1294  &cortex_m->nvic_dfsr);
1295  if (retval != ERROR_OK)
1296  return retval;
1297  if ((cortex_m->dcb_dhcsr & S_HALT)
1298  && (cortex_m->nvic_dfsr & DFSR_VCATCH)) {
1299  LOG_TARGET_DEBUG(target, "system reset-halted, DHCSR 0x%08" PRIx32 ", DFSR 0x%08" PRIx32,
1300  cortex_m->dcb_dhcsr, cortex_m->nvic_dfsr);
1302  /* FIXME restore user's vector catch config */
1303  return ERROR_OK;
1304  } else {
1305  LOG_TARGET_DEBUG(target, "waiting for system reset-halt, "
1306  "DHCSR 0x%08" PRIx32 ", %d ms",
1307  cortex_m->dcb_dhcsr, timeout);
1308  }
1309  }
1310  timeout++;
1311  alive_sleep(1);
1312  }
1313 
1314  return ERROR_OK;
1315 }
1316 
1318 {
1320 
1321  /* set any pending breakpoints */
1322  while (breakpoint) {
1323  if (!breakpoint->is_set)
1326  }
1327 }
1328 
1329 static int cortex_m_restore_one(struct target *target, bool current,
1330  target_addr_t *address, bool handle_breakpoints, bool debug_execution)
1331 {
1332  struct armv7m_common *armv7m = target_to_armv7m(target);
1333  struct breakpoint *breakpoint = NULL;
1334  uint32_t resume_pc;
1335  struct reg *r;
1336 
1337  if (target->state != TARGET_HALTED) {
1338  LOG_TARGET_ERROR(target, "not halted");
1339  return ERROR_TARGET_NOT_HALTED;
1340  }
1341 
1342  if (!debug_execution) {
1346  }
1347 
1348  if (debug_execution) {
1349  r = armv7m->arm.core_cache->reg_list + ARMV7M_PRIMASK;
1350 
1351  /* Disable interrupts */
1352  /* We disable interrupts in the PRIMASK register instead of
1353  * masking with C_MASKINTS. This is probably the same issue
1354  * as Cortex-M3 Erratum 377493 (fixed in r1p0): C_MASKINTS
1355  * in parallel with disabled interrupts can cause local faults
1356  * to not be taken.
1357  *
1358  * This breaks non-debug (application) execution if not
1359  * called from armv7m_start_algorithm() which saves registers.
1360  */
1361  buf_set_u32(r->value, 0, 1, 1);
1362  r->dirty = true;
1363  r->valid = true;
1364 
1365  /* Make sure we are in Thumb mode, set xPSR.T bit */
1366  /* armv7m_start_algorithm() initializes entire xPSR register.
1367  * This duplicity handles the case when cortex_m_resume()
1368  * is used with the debug_execution flag directly,
1369  * not called through armv7m_start_algorithm().
1370  */
1371  r = armv7m->arm.cpsr;
1372  buf_set_u32(r->value, 24, 1, 1);
1373  r->dirty = true;
1374  r->valid = true;
1375  }
1376 
1377  /* current = true: continue on current pc, otherwise continue at <address> */
1378  r = armv7m->arm.pc;
1379  if (!current) {
1380  buf_set_u32(r->value, 0, 32, *address);
1381  r->dirty = true;
1382  r->valid = true;
1383  }
1384 
1385  /* if we halted last time due to a bkpt instruction
1386  * then we have to manually step over it, otherwise
1387  * the core will break again */
1388 
1389  if (!breakpoint_find(target, buf_get_u32(r->value, 0, 32))
1390  && !debug_execution)
1392 
1393  resume_pc = buf_get_u32(r->value, 0, 32);
1394  if (current)
1395  *address = resume_pc;
1396 
1397  int retval = armv7m_restore_context(target);
1398  if (retval != ERROR_OK)
1399  return retval;
1400 
1401  /* the front-end may request us not to handle breakpoints */
1402  if (handle_breakpoints) {
1403  /* Single step past breakpoint at current address */
1404  breakpoint = breakpoint_find(target, resume_pc);
1405  if (breakpoint) {
1406  LOG_TARGET_DEBUG(target, "unset breakpoint at " TARGET_ADDR_FMT " (ID: %" PRIu32 ")",
1410  if (retval == ERROR_OK)
1413  if (retval != ERROR_OK)
1414  return retval;
1415  if (ret2 != ERROR_OK)
1416  return ret2;
1417  }
1418  }
1419 
1420  return ERROR_OK;
1421 }
1422 
1423 static int cortex_m_restart_one(struct target *target, bool debug_execution)
1424 {
1425  struct armv7m_common *armv7m = target_to_armv7m(target);
1426 
1427  /* Restart core */
1430 
1432  /* registers are now invalid */
1434 
1435  if (!debug_execution) {
1438  } else {
1441  }
1442 
1443  return ERROR_OK;
1444 }
1445 
1446 static int cortex_m_restore_smp(struct target *target, bool handle_breakpoints)
1447 {
1448  struct target_list *head;
1451  struct target *curr = head->target;
1452  /* skip calling target */
1453  if (curr == target)
1454  continue;
1455  if (!target_was_examined(curr))
1456  continue;
1457  /* skip running targets */
1458  if (curr->state == TARGET_RUNNING)
1459  continue;
1460 
1461  int retval = cortex_m_restore_one(curr, true, &address,
1462  handle_breakpoints, false);
1463  if (retval != ERROR_OK)
1464  return retval;
1465 
1466  retval = cortex_m_restart_one(curr, false);
1467  if (retval != ERROR_OK)
1468  return retval;
1469 
1470  LOG_TARGET_DEBUG(curr, "SMP resumed at " TARGET_ADDR_FMT, address);
1471  }
1472  return ERROR_OK;
1473 }
1474 
1475 static int cortex_m_resume(struct target *target, bool current,
1476  target_addr_t address, bool handle_breakpoints, bool debug_execution)
1477 {
1478  int retval = cortex_m_restore_one(target, current, &address,
1479  handle_breakpoints, debug_execution);
1480  if (retval != ERROR_OK) {
1481  LOG_TARGET_ERROR(target, "context restore failed, aborting resume");
1482  return retval;
1483  }
1484 
1485  if (target->smp && !debug_execution) {
1486  retval = cortex_m_restore_smp(target, handle_breakpoints);
1487  if (retval != ERROR_OK)
1488  LOG_TARGET_WARNING(target, "resume of a SMP target failed, trying to resume current one");
1489  }
1490 
1491  cortex_m_restart_one(target, debug_execution);
1492  if (retval != ERROR_OK) {
1493  LOG_TARGET_ERROR(target, "resume failed");
1494  return retval;
1495  }
1496 
1497  LOG_TARGET_DEBUG(target, "%sresumed at " TARGET_ADDR_FMT,
1498  debug_execution ? "debug " : "", address);
1499 
1500  return ERROR_OK;
1501 }
1502 
1503 /* int irqstepcount = 0; */
1504 static int cortex_m_step(struct target *target, bool current,
1505  target_addr_t address, bool handle_breakpoints)
1506 {
1507  struct cortex_m_common *cortex_m = target_to_cm(target);
1508  struct armv7m_common *armv7m = &cortex_m->armv7m;
1509  struct breakpoint *breakpoint = NULL;
1510  struct reg *pc = armv7m->arm.pc;
1511  bool bkpt_inst_found = false;
1512  int retval;
1513  bool isr_timed_out = false;
1514 
1515  if (target->state != TARGET_HALTED) {
1516  LOG_TARGET_ERROR(target, "not halted");
1517  return ERROR_TARGET_NOT_HALTED;
1518  }
1519 
1520  /* Just one of SMP cores will step. Set the gdb control
1521  * target to current one or gdb miss gdb-end event */
1522  if (target->smp && target->gdb_service)
1524 
1525  /* current = true: continue on current pc, otherwise continue at <address> */
1526  if (!current) {
1527  buf_set_u32(pc->value, 0, 32, address);
1528  pc->dirty = true;
1529  pc->valid = true;
1530  }
1531 
1532  uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
1533 
1534  /* the front-end may request us not to handle breakpoints */
1535  if (handle_breakpoints) {
1536  breakpoint = breakpoint_find(target, pc_value);
1537  if (breakpoint)
1539  }
1540 
1541  armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
1542 
1544 
1546 
1548 
1549  /* if no bkpt instruction is found at pc then we can perform
1550  * a normal step, otherwise we have to manually step over the bkpt
1551  * instruction - as such simulate a step */
1552  if (!bkpt_inst_found) {
1553  if (cortex_m->isrmasking_mode != CORTEX_M_ISRMASK_AUTO) {
1554  /* Automatic ISR masking mode off: Just step over the next
1555  * instruction, with interrupts on or off as appropriate. */
1558  } else {
1559  /* Process interrupts during stepping in a way they don't interfere
1560  * debugging.
1561  *
1562  * Principle:
1563  *
1564  * Set a temporary break point at the current pc and let the core run
1565  * with interrupts enabled. Pending interrupts get served and we run
1566  * into the breakpoint again afterwards. Then we step over the next
1567  * instruction with interrupts disabled.
1568  *
1569  * If the pending interrupts don't complete within time, we leave the
1570  * core running. This may happen if the interrupts trigger faster
1571  * than the core can process them or the handler doesn't return.
1572  *
1573  * If no more breakpoints are available we simply do a step with
1574  * interrupts enabled.
1575  *
1576  */
1577 
1578  /* 2012-09-29 ph
1579  *
1580  * If a break point is already set on the lower half word then a break point on
1581  * the upper half word will not break again when the core is restarted. So we
1582  * just step over the instruction with interrupts disabled.
1583  *
1584  * The documentation has no information about this, it was found by observation
1585  * on STM32F1 and STM32F2. Proper explanation welcome. STM32F0 doesn't seem to
1586  * suffer from this problem.
1587  *
1588  * To add some confusion: pc_value has bit 0 always set, while the breakpoint
1589  * address has it always cleared. The former is done to indicate thumb mode
1590  * to gdb.
1591  *
1592  */
1593  if ((pc_value & 0x02) && breakpoint_find(target, pc_value & ~0x03)) {
1594  LOG_TARGET_DEBUG(target, "Stepping over next instruction with interrupts disabled");
1597  /* Re-enable interrupts if appropriate */
1600  } else {
1601 
1602  /* Set a temporary break point */
1603  if (breakpoint) {
1605  } else {
1607  if (cortex_m->fp_rev == 0 && pc_value > 0x1FFFFFFF) {
1608  /* FPB rev.1 cannot handle such addr, try BKPT instr */
1609  type = BKPT_SOFT;
1610  }
1611  retval = breakpoint_add(target, pc_value, 2, type);
1612  }
1613 
1614  bool tmp_bp_set = (retval == ERROR_OK);
1615 
1616  /* No more breakpoints left, just do a step */
1617  if (!tmp_bp_set) {
1620  /* Re-enable interrupts if appropriate */
1623  } else {
1624  /* Start the core */
1625  LOG_TARGET_DEBUG(target, "Starting core to serve pending interrupts");
1626  int64_t t_start = timeval_ms();
1629 
1630  /* Wait for pending handlers to complete or timeout */
1631  do {
1633  if (retval != ERROR_OK) {
1635  return retval;
1636  }
1637  isr_timed_out = ((timeval_ms() - t_start) > 500);
1638  } while (!((cortex_m->dcb_dhcsr & S_HALT) || isr_timed_out));
1639 
1640  /* only remove breakpoint if we created it */
1641  if (breakpoint)
1643  else {
1644  /* Remove the temporary breakpoint */
1645  breakpoint_remove(target, pc_value);
1646  }
1647 
1648  if (isr_timed_out) {
1649  LOG_TARGET_DEBUG(target, "Interrupt handlers didn't complete within time, "
1650  "leaving target running");
1651  } else {
1652  /* Step over next instruction with interrupts disabled */
1655  C_HALT | C_MASKINTS,
1656  0);
1658  /* Re-enable interrupts if appropriate */
1661  }
1662  }
1663  }
1664  }
1665  }
1666 
1668  if (retval != ERROR_OK)
1669  return retval;
1670 
1671  /* registers are now invalid */
1673 
1674  if (breakpoint)
1676 
1677  if (isr_timed_out) {
1678  /* Leave the core running. The user has to stop execution manually. */
1681  return ERROR_OK;
1682  }
1683 
1684  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1685  " nvic_icsr = 0x%" PRIx32,
1686  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1687 
1688  retval = cortex_m_debug_entry(target);
1689  if (retval != ERROR_OK && retval != ERROR_TARGET_HALTED_DO_RESUME)
1690  return retval;
1692 
1693  LOG_TARGET_DEBUG(target, "target stepped dcb_dhcsr = 0x%" PRIx32
1694  " nvic_icsr = 0x%" PRIx32,
1695  cortex_m->dcb_dhcsr, cortex_m->nvic_icsr);
1696 
1697  return ERROR_OK;
1698 }
1699 
1701 {
1702  struct cortex_m_common *cortex_m = target_to_cm(target);
1703  struct armv7m_common *armv7m = &cortex_m->armv7m;
1704  enum cortex_m_soft_reset_config reset_config = cortex_m->soft_reset_config;
1705 
1706  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1708  target_was_examined(target) ? "" : " not");
1709 
1711 
1713  /* allow scripts to override the reset event */
1714 
1718 
1719  return ERROR_OK;
1720  }
1721 
1722  /* some cores support connecting while srst is asserted
1723  * use that mode if it has been configured */
1724 
1725  bool srst_asserted = false;
1726 
1729  || (!armv7m->debug_ap && !target->defer_examine))) {
1730  /* If we have no debug_ap, asserting SRST is the only thing
1731  * we can do now */
1733  srst_asserted = true;
1734  }
1735 
1736  /* TODO: replace the hack calling target_examine_one()
1737  * as soon as a better reset framework is available */
1740  LOG_TARGET_DEBUG(target, "Trying to re-examine under reset");
1742  }
1743 
1744  /* We need at least debug_ap to go further.
1745  * Inform user and bail out if we don't have one. */
1746  if (!armv7m->debug_ap) {
1747  if (srst_asserted) {
1748  if (target->reset_halt)
1749  LOG_TARGET_ERROR(target, "Debug AP not available, will not halt after reset!");
1750 
1751  /* Do not propagate error: reset was asserted, proceed to deassert! */
1754  return ERROR_OK;
1755 
1756  } else {
1757  LOG_TARGET_ERROR(target, "Debug AP not available, reset NOT asserted!");
1758  return ERROR_FAIL;
1759  }
1760  }
1761 
1762  /* Enable debug requests */
1764 
1765  /* Store important errors instead of failing and proceed to reset assert */
1766 
1767  if (retval != ERROR_OK || !(cortex_m->dcb_dhcsr & C_DEBUGEN))
1769 
1770  /* If the processor is sleeping in a WFI or WFE instruction, the
1771  * C_HALT bit must be asserted to regain control */
1772  if (retval == ERROR_OK && (cortex_m->dcb_dhcsr & S_SLEEP))
1774 
1775  mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
1776  /* Ignore less important errors */
1777 
1778  if (!target->reset_halt) {
1779  /* Set/Clear C_MASKINTS in a separate operation */
1781 
1782  /* clear any debug flags before resuming */
1784 
1785  /* clear C_HALT in dhcsr reg */
1787  } else {
1788  /* Halt in debug on reset; endreset_event() restores DEMCR.
1789  *
1790  * REVISIT catching BUSERR presumably helps to defend against
1791  * bad vector table entries. Should this include MMERR or
1792  * other flags too?
1793  */
1794  int retval2;
1795  retval2 = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DEMCR,
1798  if (retval != ERROR_OK || retval2 != ERROR_OK)
1799  LOG_TARGET_INFO(target, "AP write error, reset will not halt");
1800  }
1801 
1803  /* default to asserting srst */
1804  if (!srst_asserted)
1806 
1807  /* srst is asserted, ignore AP access errors */
1808  retval = ERROR_OK;
1809  } else {
1810  /* Use a standard Cortex-M software reset mechanism.
1811  * We default to using VECTRESET.
1812  * This has the disadvantage of not resetting the peripherals, so a
1813  * reset-init event handler is needed to perform any peripheral resets.
1814  */
1815  if (!cortex_m->vectreset_supported
1816  && reset_config == CORTEX_M_RESET_VECTRESET) {
1817  reset_config = CORTEX_M_RESET_SYSRESETREQ;
1818  LOG_TARGET_WARNING(target, "VECTRESET is not supported on this Cortex-M core, using SYSRESETREQ instead.");
1819  LOG_TARGET_WARNING(target, "Set 'cortex_m reset_config sysresetreq'.");
1820  }
1821 
1822  LOG_TARGET_DEBUG(target, "Using Cortex-M %s", (reset_config == CORTEX_M_RESET_SYSRESETREQ)
1823  ? "SYSRESETREQ" : "VECTRESET");
1824 
1825  if (reset_config == CORTEX_M_RESET_VECTRESET) {
1826  LOG_TARGET_WARNING(target, "Only resetting the Cortex-M core, use a reset-init event "
1827  "handler to reset any peripherals or configure hardware srst support.");
1828  }
1829 
1830  int retval3;
1831  retval3 = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_AIRCR,
1832  AIRCR_VECTKEY | ((reset_config == CORTEX_M_RESET_SYSRESETREQ)
1834  if (retval3 != ERROR_OK)
1835  LOG_TARGET_DEBUG(target, "Ignoring AP write error right after reset");
1836 
1837  retval3 = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1838  if (retval3 != ERROR_OK) {
1839  LOG_TARGET_ERROR(target, "DP initialisation failed");
1840  /* The error return value must not be propagated in this case.
1841  * SYSRESETREQ or VECTRESET have been possibly triggered
1842  * so reset processing should continue */
1843  } else {
1844  /* I do not know why this is necessary, but it
1845  * fixes strange effects (step/resume cause NMI
1846  * after reset) on LM3S6918 -- Michael Schwingen
1847  */
1848  uint32_t tmp;
1849  mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_AIRCR, &tmp);
1850  }
1851  }
1852 
1854  jtag_sleep(50000);
1855 
1857 
1858  return retval;
1859 }
1860 
1862 {
1863  struct armv7m_common *armv7m = &target_to_cm(target)->armv7m;
1864 
1865  LOG_TARGET_DEBUG(target, "target->state: %s,%s examined",
1867  target_was_examined(target) ? "" : " not");
1868 
1870 
1871  /* deassert reset lines */
1874 
1875 
1878  armv7m->debug_ap) {
1879 
1880  int retval = dap_dp_init_or_reconnect(armv7m->debug_ap->dap);
1881  if (retval != ERROR_OK) {
1882  LOG_TARGET_ERROR(target, "DP initialisation failed");
1883  return retval;
1884  }
1885  }
1886 
1887  return ERROR_OK;
1888 }
1889 
1891 {
1892  int retval;
1893  unsigned int fp_num = 0;
1894  struct cortex_m_common *cortex_m = target_to_cm(target);
1895  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1896 
1897  if (breakpoint->is_set) {
1898  LOG_TARGET_WARNING(target, "breakpoint (BPID: %" PRIu32 ") already set", breakpoint->unique_id);
1899  return ERROR_OK;
1900  }
1901 
1902  if (breakpoint->type == BKPT_HARD) {
1903  uint32_t fpcr_value;
1904  while (comparator_list[fp_num].used && (fp_num < cortex_m->fp_num_code))
1905  fp_num++;
1906  if (fp_num >= cortex_m->fp_num_code) {
1907  LOG_TARGET_ERROR(target, "Can not find free FPB Comparator!");
1909  }
1910  breakpoint_hw_set(breakpoint, fp_num);
1911  fpcr_value = breakpoint->address | 1;
1912  if (cortex_m->fp_rev == 0) {
1913  if (breakpoint->address > 0x1FFFFFFF) {
1914  LOG_TARGET_ERROR(target, "Cortex-M Flash Patch Breakpoint rev.1 "
1915  "cannot handle HW breakpoint above address 0x1FFFFFFE");
1916  return ERROR_FAIL;
1917  }
1918  uint32_t hilo;
1920  fpcr_value = (fpcr_value & 0x1FFFFFFC) | hilo | 1;
1921  } else if (cortex_m->fp_rev > 1) {
1922  LOG_TARGET_ERROR(target, "Unhandled Cortex-M Flash Patch Breakpoint architecture revision");
1923  return ERROR_FAIL;
1924  }
1925  comparator_list[fp_num].used = true;
1926  comparator_list[fp_num].fpcr_value = fpcr_value;
1927  target_write_u32(target, comparator_list[fp_num].fpcr_address,
1928  comparator_list[fp_num].fpcr_value);
1929  LOG_TARGET_DEBUG(target, "fpc_num %i fpcr_value 0x%" PRIx32,
1930  fp_num,
1931  comparator_list[fp_num].fpcr_value);
1932  if (!cortex_m->fpb_enabled) {
1933  LOG_TARGET_DEBUG(target, "FPB wasn't enabled, do it now");
1934  retval = cortex_m_enable_fpb(target);
1935  if (retval != ERROR_OK) {
1936  LOG_TARGET_ERROR(target, "Failed to enable the FPB");
1937  return retval;
1938  }
1939 
1940  cortex_m->fpb_enabled = true;
1941  }
1942  } else if (breakpoint->type == BKPT_SOFT) {
1943  uint8_t code[4];
1944 
1945  /* NOTE: on ARMv6-M and ARMv7-M, BKPT(0xab) is used for
1946  * semihosting; don't use that. Otherwise the BKPT
1947  * parameter is arbitrary.
1948  */
1949  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1950  retval = target_read_memory(target,
1951  breakpoint->address & 0xFFFFFFFE,
1952  breakpoint->length, 1,
1954  if (retval != ERROR_OK)
1955  return retval;
1956  // make sure data cache is cleaned & invalidated down to PoC
1958  if (retval != ERROR_OK)
1959  return retval;
1960 
1961  retval = target_write_memory(target,
1962  breakpoint->address & 0xFFFFFFFE,
1963  breakpoint->length, 1,
1964  code);
1965  if (retval != ERROR_OK)
1966  return retval;
1967  // update i-cache at breakpoint location
1969  if (retval != ERROR_OK)
1970  return retval;
1972  if (retval != ERROR_OK)
1973  return retval;
1974 
1975  breakpoint->is_set = true;
1976  }
1977 
1978  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
1980  (int)(breakpoint->type),
1982  breakpoint->length,
1983  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
1984 
1985  return ERROR_OK;
1986 }
1987 
1989 {
1990  int retval;
1991  struct cortex_m_common *cortex_m = target_to_cm(target);
1992  struct cortex_m_fp_comparator *comparator_list = cortex_m->fp_comparator_list;
1993 
1994  if (!breakpoint->is_set) {
1995  LOG_TARGET_WARNING(target, "breakpoint not set");
1996  return ERROR_OK;
1997  }
1998 
1999  LOG_TARGET_DEBUG(target, "BPID: %" PRIu32 ", Type: %d, Address: " TARGET_ADDR_FMT " Length: %d (n=%u)",
2001  (int)(breakpoint->type),
2003  breakpoint->length,
2004  (breakpoint->type == BKPT_SOFT) ? 0 : breakpoint->number);
2005 
2006  if (breakpoint->type == BKPT_HARD) {
2007  unsigned int fp_num = breakpoint->number;
2008  if (fp_num >= cortex_m->fp_num_code) {
2009  LOG_TARGET_DEBUG(target, "Invalid FP Comparator number in breakpoint");
2010  return ERROR_OK;
2011  }
2012  comparator_list[fp_num].used = false;
2013  comparator_list[fp_num].fpcr_value = 0;
2014  target_write_u32(target, comparator_list[fp_num].fpcr_address,
2015  comparator_list[fp_num].fpcr_value);
2016  } else {
2017  // make sure data cache is cleaned & invalidated down to PoC
2019  if (retval != ERROR_OK)
2020  return retval;
2021 
2022  /* restore original instruction (kept in target endianness) */
2023  retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE,
2024  breakpoint->length, 1,
2026  if (retval != ERROR_OK)
2027  return retval;
2028 
2029  // update i-cache at breakpoint location
2031  if (retval != ERROR_OK)
2032  return retval;
2034  if (retval != ERROR_OK)
2035  return retval;
2036  }
2037  breakpoint->is_set = false;
2038 
2039  return ERROR_OK;
2040 }
2041 
2043 {
2044  /*
2045  * GDB packets Z0 and z0 provide the 'kind' parameter that is target-specific
2046  * and typically indicates the size in bytes of the breakpoint.
2047  * But for 32-bit Thumb mode (Thumb-2) breakpoint, GDB provides 'kind = 3' to
2048  * be used to derive the length information. See:
2049  * https://sourceware.org/gdb/current/onlinedocs/gdb.html/ARM-Breakpoint-Kinds.html
2050  * Since there isn't a four byte Thumb-2 breakpoint instruction, always use
2051  * the two bytes breakpoint instruction.
2052  */
2053  if (breakpoint->length == 3) {
2054  LOG_TARGET_DEBUG(target, "Using a two byte breakpoint for 32bit Thumb-2 request");
2055  breakpoint->length = 2;
2056  }
2057 
2058  if ((breakpoint->length != 2)) {
2059  LOG_TARGET_INFO(target, "only breakpoints of two bytes length supported");
2061  }
2062 
2064 }
2065 
2067 {
2068  if (!breakpoint->is_set)
2069  return ERROR_OK;
2070 
2072 }
2073 
2075 {
2076  unsigned int dwt_num = 0;
2077  struct cortex_m_common *cortex_m = target_to_cm(target);
2078 
2079  /* REVISIT Don't fully trust these "not used" records ... users
2080  * may set up breakpoints by hand, e.g. dual-address data value
2081  * watchpoint using comparator #1; comparator #0 matching cycle
2082  * count; send data trace info through ITM and TPIU; etc
2083  */
2084  struct cortex_m_dwt_comparator *comparator;
2085 
2086  for (comparator = cortex_m->dwt_comparator_list;
2087  comparator->used && dwt_num < cortex_m->dwt_num_comp;
2088  comparator++, dwt_num++)
2089  continue;
2090  if (dwt_num >= cortex_m->dwt_num_comp) {
2091  LOG_TARGET_ERROR(target, "Can not find free DWT Comparator");
2092  return ERROR_FAIL;
2093  }
2094  comparator->used = true;
2095  watchpoint_set(watchpoint, dwt_num);
2096 
2097  comparator->comp = watchpoint->address;
2099  comparator->comp);
2100 
2101  if ((cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M_V2_0
2102  && (cortex_m->dwt_devarch & 0x1FFFFF) != DWT_DEVARCH_ARMV8M_V2_1) {
2103  uint32_t mask = 0, temp;
2104 
2105  /* watchpoint params were validated earlier */
2106  temp = watchpoint->length;
2107  while (temp) {
2108  temp >>= 1;
2109  mask++;
2110  }
2111  mask--;
2112 
2113  comparator->mask = mask;
2115  comparator->mask);
2116 
2117  switch (watchpoint->rw) {
2118  case WPT_READ:
2119  comparator->function = 5;
2120  break;
2121  case WPT_WRITE:
2122  comparator->function = 6;
2123  break;
2124  case WPT_ACCESS:
2125  comparator->function = 7;
2126  break;
2127  }
2128  } else {
2129  uint32_t data_size = watchpoint->length >> 1;
2130  comparator->mask = (watchpoint->length >> 1) | 1;
2131 
2132  switch (watchpoint->rw) {
2133  case WPT_ACCESS:
2134  comparator->function = 4;
2135  break;
2136  case WPT_WRITE:
2137  comparator->function = 5;
2138  break;
2139  case WPT_READ:
2140  comparator->function = 6;
2141  break;
2142  }
2143  comparator->function = comparator->function | (1 << 4) |
2144  (data_size << 10);
2145  }
2146 
2148  comparator->function);
2149 
2150  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%d 0x%08" PRIx32 " 0x%" PRIx32 " 0x%05" PRIx32,
2151  watchpoint->unique_id, dwt_num,
2152  comparator->comp, comparator->mask, comparator->function);
2153  return ERROR_OK;
2154 }
2155 
2157 {
2158  struct cortex_m_common *cortex_m = target_to_cm(target);
2159  struct cortex_m_dwt_comparator *comparator;
2160 
2161  if (!watchpoint->is_set) {
2162  LOG_TARGET_WARNING(target, "watchpoint (wpid: %d) not set",
2164  return ERROR_OK;
2165  }
2166 
2167  unsigned int dwt_num = watchpoint->number;
2168 
2169  LOG_TARGET_DEBUG(target, "Watchpoint (ID %d) DWT%u address: " TARGET_ADDR_FMT " clear",
2170  watchpoint->unique_id, dwt_num,
2171  watchpoint->address);
2172 
2173  if (dwt_num >= cortex_m->dwt_num_comp) {
2174  LOG_TARGET_DEBUG(target, "Invalid DWT Comparator number in watchpoint");
2175  return ERROR_OK;
2176  }
2177 
2178  comparator = cortex_m->dwt_comparator_list + dwt_num;
2179  comparator->used = false;
2180  comparator->function = 0;
2182  comparator->function);
2183 
2184  watchpoint->is_set = false;
2185 
2186  return ERROR_OK;
2187 }
2188 
2190 {
2191  struct cortex_m_common *cortex_m = target_to_cm(target);
2192 
2193  if (cortex_m->dwt_comp_available < 1) {
2194  LOG_TARGET_DEBUG(target, "no comparators?");
2196  }
2197 
2198  /* REVISIT This DWT may well be able to watch for specific data
2199  * values. Requires comparator #1 to set DATAVMATCH and match
2200  * the data, and another comparator (DATAVADDR0) matching addr.
2201  *
2202  * NOTE: hardware doesn't support data value masking, so we'll need
2203  * to check that mask is zero
2204  */
2206  LOG_TARGET_DEBUG(target, "watchpoint value masks not supported");
2208  }
2209 
2210  /* hardware allows address masks of up to 32K */
2211  unsigned int mask;
2212 
2213  for (mask = 0; mask < 16; mask++) {
2214  if ((1u << mask) == watchpoint->length)
2215  break;
2216  }
2217  if (mask == 16) {
2218  LOG_TARGET_DEBUG(target, "unsupported watchpoint length");
2220  }
2221  if (watchpoint->address & ((1 << mask) - 1)) {
2222  LOG_TARGET_DEBUG(target, "watchpoint address is unaligned");
2224  }
2225 
2226  cortex_m->dwt_comp_available--;
2227  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
2228 
2229  return ERROR_OK;
2230 }
2231 
2233 {
2234  struct cortex_m_common *cortex_m = target_to_cm(target);
2235 
2236  /* REVISIT why check? DWT can be updated with core running ... */
2237  if (target->state != TARGET_HALTED) {
2238  LOG_TARGET_ERROR(target, "not halted");
2239  return ERROR_TARGET_NOT_HALTED;
2240  }
2241 
2242  if (watchpoint->is_set)
2244 
2245  cortex_m->dwt_comp_available++;
2246  LOG_TARGET_DEBUG(target, "dwt_comp_available: %d", cortex_m->dwt_comp_available);
2247 
2248  return ERROR_OK;
2249 }
2250 
2251 static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
2252 {
2254  return ERROR_FAIL;
2255 
2256  struct cortex_m_common *cortex_m = target_to_cm(target);
2257 
2258  for (struct watchpoint *wp = target->watchpoints; wp; wp = wp->next) {
2259  if (!wp->is_set)
2260  continue;
2261 
2262  unsigned int dwt_num = wp->number;
2263  struct cortex_m_dwt_comparator *comparator = cortex_m->dwt_comparator_list + dwt_num;
2264 
2265  uint32_t dwt_function;
2266  int retval = target_read_u32(target, comparator->dwt_comparator_address + 8, &dwt_function);
2267  if (retval != ERROR_OK)
2268  return ERROR_FAIL;
2269 
2270  /* check the MATCHED bit */
2271  if (dwt_function & BIT(24)) {
2272  *hit_watchpoint = wp;
2273  return ERROR_OK;
2274  }
2275  }
2276 
2277  return ERROR_FAIL;
2278 }
2279 
2281 {
2283 
2284  /* set any pending watchpoints */
2285  while (watchpoint) {
2286  if (!watchpoint->is_set)
2289  }
2290 }
2291 
2293  uint32_t size, uint32_t count, uint8_t *buffer)
2294 {
2295  struct armv7m_common *armv7m = target_to_armv7m(target);
2296 
2297  if (armv7m->arm.arch == ARM_ARCH_V6M) {
2298  /* armv6m does not handle unaligned memory access */
2299  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2301  }
2302 
2303  return mem_ap_read_buf(armv7m->debug_ap, buffer, size, count, address);
2304 }
2305 
2307  uint32_t size, uint32_t count, const uint8_t *buffer)
2308 {
2309  struct armv7m_common *armv7m = target_to_armv7m(target);
2310 
2311  if (armv7m->arm.arch == ARM_ARCH_V6M) {
2312  /* armv6m does not handle unaligned memory access */
2313  if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2315  }
2316 
2317  return mem_ap_write_buf(armv7m->debug_ap, buffer, size, count, address);
2318 }
2319 
2320 static int cortex_m_init_target(struct command_context *cmd_ctx,
2321  struct target *target)
2322 {
2325  return ERROR_OK;
2326 }
2327 
2329 {
2330  struct cortex_m_common *cortex_m = target_to_cm(target);
2331  struct armv7m_common *armv7m = target_to_armv7m(target);
2332 
2333  if (!armv7m->is_hla_target && armv7m->debug_ap)
2334  dap_put_ap(armv7m->debug_ap);
2335 
2336  free(cortex_m->fp_comparator_list);
2337 
2340 
2341  free(target->private_config);
2342  free(cortex_m);
2343 }
2344 
2345 int cortex_m_profiling(struct target *target, uint32_t *samples,
2346  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2347 {
2348  struct timeval timeout, now;
2349  struct armv7m_common *armv7m = target_to_armv7m(target);
2350  uint32_t reg_value;
2351  int retval;
2352 
2353  retval = target_read_u32(target, DWT_PCSR, &reg_value);
2354  if (retval != ERROR_OK) {
2355  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2356  return retval;
2357  }
2358  if (reg_value == 0) {
2359  LOG_TARGET_INFO(target, "PCSR sampling not supported on this processor.");
2360  return target_profiling_default(target, samples, max_num_samples, num_samples, seconds);
2361  }
2362 
2364  timeval_add_time(&timeout, seconds, 0);
2365 
2366  LOG_TARGET_INFO(target, "Starting Cortex-M profiling. Sampling DWT_PCSR as fast as we can...");
2367 
2368  /* Make sure the target is running */
2370  if (target->state == TARGET_HALTED)
2371  retval = target_resume(target, true, 0, false, false);
2372 
2373  if (retval != ERROR_OK) {
2374  LOG_TARGET_ERROR(target, "Error while resuming target");
2375  return retval;
2376  }
2377 
2378  uint32_t sample_count = 0;
2379 
2380  for (;;) {
2381  if (armv7m && armv7m->debug_ap) {
2382  uint32_t read_count = max_num_samples - sample_count;
2383  if (read_count > 1024)
2384  read_count = 1024;
2385 
2386  retval = mem_ap_read_buf_noincr(armv7m->debug_ap,
2387  (void *)&samples[sample_count],
2388  4, read_count, DWT_PCSR);
2389  sample_count += read_count;
2390  } else {
2391  target_read_u32(target, DWT_PCSR, &samples[sample_count++]);
2392  }
2393 
2394  if (retval != ERROR_OK) {
2395  LOG_TARGET_ERROR(target, "Error while reading PCSR");
2396  return retval;
2397  }
2398 
2399 
2400  gettimeofday(&now, NULL);
2401  if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) {
2402  LOG_TARGET_INFO(target, "Profiling completed. %" PRIu32 " samples.", sample_count);
2403  break;
2404  }
2405  }
2406 
2407  *num_samples = sample_count;
2408  return retval;
2409 }
2410 
2411 
2412 /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
2413  * on r/w if the core is not running, and clear on resume or reset ... or
2414  * at least, in a post_restore_context() method.
2415  */
2416 
2418  struct target *target;
2419  uint32_t addr;
2420  uint8_t value[4]; /* scratch/cache */
2421 };
2422 
2423 static int cortex_m_dwt_get_reg(struct reg *reg)
2424 {
2425  struct dwt_reg_state *state = reg->arch_info;
2426 
2427  uint32_t tmp;
2428  int retval = target_read_u32(state->target, state->addr, &tmp);
2429  if (retval != ERROR_OK)
2430  return retval;
2431 
2432  buf_set_u32(state->value, 0, 32, tmp);
2433  return ERROR_OK;
2434 }
2435 
2436 static int cortex_m_dwt_set_reg(struct reg *reg, uint8_t *buf)
2437 {
2438  struct dwt_reg_state *state = reg->arch_info;
2439 
2440  return target_write_u32(state->target, state->addr,
2441  buf_get_u32(buf, 0, reg->size));
2442 }
2443 
2444 struct dwt_reg {
2445  uint32_t addr;
2446  const char *name;
2447  unsigned int size;
2448 };
2449 
2450 static const struct dwt_reg dwt_base_regs[] = {
2451  { DWT_CTRL, "dwt_ctrl", 32, },
2452  /* NOTE that Erratum 532314 (fixed r2p0) affects CYCCNT: it wrongly
2453  * increments while the core is asleep.
2454  */
2455  { DWT_CYCCNT, "dwt_cyccnt", 32, },
2456  /* plus some 8 bit counters, useful for profiling with TPIU */
2457 };
2458 
2459 static const struct dwt_reg dwt_comp[] = {
2460 #define DWT_COMPARATOR(i) \
2461  { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
2462  { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
2463  { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
2464  DWT_COMPARATOR(0),
2465  DWT_COMPARATOR(1),
2466  DWT_COMPARATOR(2),
2467  DWT_COMPARATOR(3),
2468  DWT_COMPARATOR(4),
2469  DWT_COMPARATOR(5),
2470  DWT_COMPARATOR(6),
2471  DWT_COMPARATOR(7),
2472  DWT_COMPARATOR(8),
2473  DWT_COMPARATOR(9),
2474  DWT_COMPARATOR(10),
2475  DWT_COMPARATOR(11),
2476  DWT_COMPARATOR(12),
2477  DWT_COMPARATOR(13),
2478  DWT_COMPARATOR(14),
2479  DWT_COMPARATOR(15),
2480 #undef DWT_COMPARATOR
2481 };
2482 
2483 static const struct reg_arch_type dwt_reg_type = {
2485  .set = cortex_m_dwt_set_reg,
2486 };
2487 
2488 static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
2489 {
2490  struct dwt_reg_state *state;
2491 
2492  state = calloc(1, sizeof(*state));
2493  if (!state)
2494  return;
2495  state->addr = d->addr;
2496  state->target = t;
2497 
2498  r->name = d->name;
2499  r->size = d->size;
2500  r->value = state->value;
2501  r->arch_info = state;
2502  r->type = &dwt_reg_type;
2503  r->exist = true;
2504 }
2505 
2506 static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
2507 {
2508  uint32_t dwtcr;
2509  struct reg_cache *cache;
2510  struct cortex_m_dwt_comparator *comparator;
2511  int reg;
2512 
2513  target_read_u32(target, DWT_CTRL, &dwtcr);
2514  LOG_TARGET_DEBUG(target, "DWT_CTRL: 0x%" PRIx32, dwtcr);
2515  if (!dwtcr) {
2516  LOG_TARGET_DEBUG(target, "no DWT");
2517  return;
2518  }
2519 
2521  LOG_TARGET_DEBUG(target, "DWT_DEVARCH: 0x%" PRIx32, cm->dwt_devarch);
2522 
2523  cm->dwt_num_comp = (dwtcr >> 28) & 0xF;
2524  cm->dwt_comp_available = cm->dwt_num_comp;
2525  cm->dwt_comparator_list = calloc(cm->dwt_num_comp,
2526  sizeof(struct cortex_m_dwt_comparator));
2527  if (!cm->dwt_comparator_list) {
2528 fail0:
2529  cm->dwt_num_comp = 0;
2530  LOG_TARGET_ERROR(target, "out of mem");
2531  return;
2532  }
2533 
2534  cache = calloc(1, sizeof(*cache));
2535  if (!cache) {
2536 fail1:
2537  free(cm->dwt_comparator_list);
2538  goto fail0;
2539  }
2540  cache->name = "Cortex-M DWT registers";
2541  cache->num_regs = 2 + cm->dwt_num_comp * 3;
2542  cache->reg_list = calloc(cache->num_regs, sizeof(*cache->reg_list));
2543  if (!cache->reg_list) {
2544  free(cache);
2545  goto fail1;
2546  }
2547 
2548  for (reg = 0; reg < 2; reg++)
2550  dwt_base_regs + reg);
2551 
2552  comparator = cm->dwt_comparator_list;
2553  for (unsigned int i = 0; i < cm->dwt_num_comp; i++, comparator++) {
2554  int j;
2555 
2556  comparator->dwt_comparator_address = DWT_COMP0 + 0x10 * i;
2557  for (j = 0; j < 3; j++, reg++)
2559  dwt_comp + 3 * i + j);
2560 
2561  /* make sure we clear any watchpoints enabled on the target */
2562  target_write_u32(target, comparator->dwt_comparator_address + 8, 0);
2563  }
2564 
2566  cm->dwt_cache = cache;
2567 
2568  LOG_TARGET_DEBUG(target, "DWT dwtcr 0x%" PRIx32 ", comp %d, watch%s",
2569  dwtcr, cm->dwt_num_comp,
2570  (dwtcr & (0xf << 24)) ? " only" : "/trigger");
2571 
2572  /* REVISIT: if num_comp > 1, check whether comparator #1 can
2573  * implement single-address data value watchpoints ... so we
2574  * won't need to check it later, when asked to set one up.
2575  */
2576 }
2577 
2578 static void cortex_m_dwt_free(struct target *target)
2579 {
2580  struct cortex_m_common *cm = target_to_cm(target);
2581  struct reg_cache *cache = cm->dwt_cache;
2582 
2583  free(cm->dwt_comparator_list);
2584  cm->dwt_comparator_list = NULL;
2585  cm->dwt_num_comp = 0;
2586 
2587  if (cache) {
2589 
2590  if (cache->reg_list) {
2591  for (size_t i = 0; i < cache->num_regs; i++)
2592  free(cache->reg_list[i].arch_info);
2593  free(cache->reg_list);
2594  }
2595  free(cache);
2596  }
2597  cm->dwt_cache = NULL;
2598 }
2599 
2600 static bool cortex_m_has_tz(struct target *target)
2601 {
2602  struct armv7m_common *armv7m = target_to_armv7m(target);
2603  uint32_t dauthstatus;
2604 
2605  if (armv7m->arm.arch != ARM_ARCH_V8M)
2606  return false;
2607 
2608  int retval = target_read_u32(target, DAUTHSTATUS, &dauthstatus);
2609  if (retval != ERROR_OK) {
2610  LOG_TARGET_WARNING(target, "Error reading DAUTHSTATUS register");
2611  return false;
2612  }
2613  return (dauthstatus & DAUTHSTATUS_SID_MASK) != 0;
2614 }
2615 
2616 static bool cortex_m_main_extension(struct target *target, uint32_t cpuid)
2617 {
2618  /* Inspect architecture to differentiate main extension/baseline */
2619  unsigned int extension = (cpuid & ARM_CPUID_ARCHITECTURE_MASK) >> ARM_CPUID_ARCHITECTURE_POS;
2620 
2621  if (extension == ARM_CPUID_MAIN_EXTENSION)
2622  return true;
2623  else if (extension == ARM_CPUID_NO_MAIN_EXTENSION)
2624  return false;
2625 
2626  LOG_TARGET_WARNING(target, "Fail to detect target extension");
2627 
2628  return false;
2629 }
2630 
2632 {
2633  if (ssec) {
2634  ssec->dscsr_dirty = false;
2635  ssec->sau_ctrl_dirty = false;
2636  ssec->mpu_ctrl_dirty = false;
2637  }
2638 
2639  if (!cortex_m_has_tz(target))
2640  return ERROR_OK;
2641 
2642  uint32_t dscsr;
2643  int retval = target_read_u32(target, DCB_DSCSR, &dscsr);
2644  if (retval != ERROR_OK) {
2645  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR read failed");
2646  return retval;
2647  }
2648  if (!(dscsr & DSCSR_CDS)) {
2649  if (ssec) {
2650  ssec->dscsr_dirty = true;
2651  ssec->dscsr = dscsr;
2652  }
2653  LOG_TARGET_DEBUG(target, "Setting Current Domain Secure in DSCSR");
2655  if (retval != ERROR_OK) {
2656  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR write failed");
2657  return retval;
2658  }
2659  }
2660 
2661  uint32_t sau_ctrl;
2662  retval = target_read_u32(target, SAU_CTRL, &sau_ctrl);
2663  if (retval != ERROR_OK) {
2664  LOG_TARGET_ERROR(target, "ARMv8M set secure: SAU_CTRL read failed");
2665  return retval;
2666  }
2667  if (sau_ctrl & SAU_CTRL_ENABLE) {
2668  if (ssec) {
2669  ssec->sau_ctrl_dirty = true;
2670  ssec->sau_ctrl = sau_ctrl;
2671  }
2672  retval = target_write_u32(target, SAU_CTRL, sau_ctrl & ~SAU_CTRL_ENABLE);
2673  if (retval != ERROR_OK) {
2674  LOG_TARGET_ERROR(target, "ARMv8M set secure: SAU_CTRL write failed");
2675  return retval;
2676  }
2677  }
2678 
2679  uint32_t mpu_ctrl;
2680  retval = target_read_u32(target, MPU_CTRL, &mpu_ctrl);
2681  if (retval != ERROR_OK) {
2682  LOG_TARGET_ERROR(target, "ARMv8M set secure: MPU_CTRL read failed");
2683  return retval;
2684  }
2685  if (mpu_ctrl & MPU_CTRL_ENABLE) {
2686  if (ssec) {
2687  ssec->mpu_ctrl_dirty = true;
2688  ssec->mpu_ctrl = mpu_ctrl;
2689  }
2690  retval = target_write_u32(target, MPU_CTRL, mpu_ctrl & ~MPU_CTRL_ENABLE);
2691  if (retval != ERROR_OK) {
2692  LOG_TARGET_ERROR(target, "ARMv8M set secure: MPU_CTRL write failed");
2693  return retval;
2694  }
2695  }
2696  return ERROR_OK;
2697 }
2698 
2700 {
2701  int retval;
2702  if (!cortex_m_has_tz(target))
2703  return ERROR_OK;
2704 
2705  if (!ssec)
2706  return ERROR_OK;
2707 
2708  if (ssec->mpu_ctrl_dirty) {
2709  retval = target_write_u32(target, MPU_CTRL, ssec->mpu_ctrl);
2710  if (retval != ERROR_OK) {
2711  LOG_TARGET_ERROR(target, "ARMv8M security restore: MPU_CTRL write failed");
2712  return retval;
2713  }
2714  ssec->mpu_ctrl_dirty = false;
2715  }
2716 
2717  if (ssec->sau_ctrl_dirty) {
2718  retval = target_write_u32(target, SAU_CTRL, ssec->sau_ctrl);
2719  if (retval != ERROR_OK) {
2720  LOG_TARGET_ERROR(target, "ARMv8M security restore: SAU_CTRL write failed");
2721  return retval;
2722  }
2723  ssec->sau_ctrl_dirty = false;
2724  }
2725 
2726  if (ssec->dscsr_dirty) {
2727  LOG_TARGET_DEBUG(target, "Restoring Current Domain Security in DSCSR");
2728  retval = target_write_u32(target, DCB_DSCSR, ssec->dscsr & ~DSCSR_CDSKEY);
2729  if (retval != ERROR_OK) {
2730  LOG_TARGET_ERROR(target, "ARMv8M set secure: DSCSR write failed");
2731  return retval;
2732  }
2733  ssec->dscsr_dirty = false;
2734  }
2735  return ERROR_OK;
2736 }
2737 
2738 #define MVFR0 0xE000EF40
2739 #define MVFR0_SP_MASK 0x000000F0
2740 #define MVFR0_SP 0x00000020
2741 #define MVFR0_DP_MASK 0x00000F00
2742 #define MVFR0_DP 0x00000200
2743 
2744 #define MVFR1 0xE000EF44
2745 #define MVFR1_MVE_MASK 0x00000F00
2746 #define MVFR1_MVE_I 0x00000100
2747 #define MVFR1_MVE_F 0x00000200
2748 
2749 static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp,
2750  struct adiv5_ap **debug_ap)
2751 {
2753  return ERROR_OK;
2754 
2755  return dap_find_get_ap(swjdp, AP_TYPE_AHB5_AP, debug_ap);
2756 }
2757 
2759 {
2760  int retval;
2761  uint32_t cpuid, fpcr;
2762  struct cortex_m_common *cortex_m = target_to_cm(target);
2763  struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
2764  struct armv7m_common *armv7m = target_to_armv7m(target);
2765 
2766  /* hla_target shares the examine handler but does not support
2767  * all its calls */
2768  if (!armv7m->is_hla_target) {
2769  if (!armv7m->debug_ap) {
2770  if (cortex_m->apsel == DP_APSEL_INVALID) {
2771  /* Search for the MEM-AP */
2772  retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
2773  if (retval != ERROR_OK) {
2774  LOG_TARGET_ERROR(target, "Could not find MEM-AP to control the core");
2775  return retval;
2776  }
2777  } else {
2778  armv7m->debug_ap = dap_get_ap(swjdp, cortex_m->apsel);
2779  if (!armv7m->debug_ap) {
2780  LOG_TARGET_ERROR(target, "Cannot get AP");
2781  return ERROR_FAIL;
2782  }
2783  }
2784  }
2785 
2786  armv7m->debug_ap->memaccess_tck = 8;
2787 
2788  retval = mem_ap_init(armv7m->debug_ap);
2789  if (retval != ERROR_OK)
2790  return retval;
2791  }
2792 
2793  if (!target_was_examined(target)) {
2795 
2796  /* Read from Device Identification Registers */
2797  retval = target_read_u32(target, CPUID, &cpuid);
2798  if (retval != ERROR_OK)
2799  return retval;
2800 
2801  /* Inspect implementer/part to look for recognized cores */
2802  unsigned int impl_part = cpuid & (ARM_CPUID_IMPLEMENTER_MASK | ARM_CPUID_PARTNO_MASK);
2803 
2804  for (unsigned int n = 0; n < ARRAY_SIZE(cortex_m_parts); n++) {
2805  if (impl_part == cortex_m_parts[n].impl_part) {
2806  cortex_m->core_info = &cortex_m_parts[n];
2807  break;
2808  }
2809  }
2810 
2811  if (!cortex_m->core_info) {
2812  LOG_TARGET_ERROR(target, "Cortex-M CPUID: 0x%x is unrecognized", cpuid);
2813  return ERROR_FAIL;
2814  }
2815 
2816  armv7m->arm.arch = cortex_m->core_info->arch;
2817 
2818  LOG_TARGET_INFO(target, "%s r%" PRId8 "p%" PRId8 " processor detected",
2819  cortex_m->core_info->name,
2820  (uint8_t)((cpuid >> 20) & 0xf),
2821  (uint8_t)((cpuid >> 0) & 0xf));
2822 
2823  cortex_m->maskints_erratum = false;
2824  cortex_m->incorrect_halt_erratum = false;
2825  if (impl_part == CORTEX_M7_PARTNO) {
2826  uint8_t rev, patch;
2827  rev = (cpuid >> 20) & 0xf;
2828  patch = (cpuid >> 0) & 0xf;
2829  if ((rev == 0) && (patch < 2)) {
2830  LOG_TARGET_WARNING(target, "Erratum 702596: single stepping may enter pending exception handler!");
2831  cortex_m->maskints_erratum = true;
2832  }
2833  /* TODO: add revision check when a Cortex-M7 revision with fixed 3092511 is out */
2834  LOG_TARGET_WARNING(target, "Erratum 3092511: Cortex-M7 can halt in an incorrect address when breakpoint and exception occurs simultaneously");
2835  cortex_m->incorrect_halt_erratum = true;
2836  if (armv7m->is_hla_target)
2837  LOG_TARGET_WARNING(target, "No erratum 3092511 workaround on hla adapter");
2838  else
2839  LOG_TARGET_INFO(target, "The erratum 3092511 workaround will resume after an incorrect halt");
2840  }
2841  LOG_TARGET_DEBUG(target, "cpuid: 0x%8.8" PRIx32, cpuid);
2842 
2843  if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV4) {
2844  uint32_t mvfr0;
2845  target_read_u32(target, MVFR0, &mvfr0);
2846 
2847  if ((mvfr0 & MVFR0_SP_MASK) == MVFR0_SP) {
2848  LOG_TARGET_DEBUG(target, "%s floating point feature FPv4_SP found",
2849  cortex_m->core_info->name);
2850  armv7m->fp_feature = FPV4_SP;
2851  }
2852  } else if (cortex_m->core_info->flags & CORTEX_M_F_HAS_FPV5) {
2853  uint32_t mvfr0, mvfr1;
2854  target_read_u32(target, MVFR0, &mvfr0);
2855  target_read_u32(target, MVFR1, &mvfr1);
2856 
2857  if ((mvfr0 & MVFR0_DP_MASK) == MVFR0_DP) {
2858  if ((mvfr1 & MVFR1_MVE_MASK) == MVFR1_MVE_F) {
2859  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP + MVE-F found",
2860  cortex_m->core_info->name);
2861  armv7m->fp_feature = FPV5_MVE_F;
2862  } else {
2863  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_DP found",
2864  cortex_m->core_info->name);
2865  armv7m->fp_feature = FPV5_DP;
2866  }
2867  } else if ((mvfr0 & MVFR0_SP_MASK) == MVFR0_SP) {
2868  LOG_TARGET_DEBUG(target, "%s floating point feature FPv5_SP found",
2869  cortex_m->core_info->name);
2870  armv7m->fp_feature = FPV5_SP;
2871  } else if ((mvfr1 & MVFR1_MVE_MASK) == MVFR1_MVE_I) {
2872  LOG_TARGET_DEBUG(target, "%s floating point feature MVE-I found",
2873  cortex_m->core_info->name);
2874  armv7m->fp_feature = FPV5_MVE_I;
2875  }
2876  }
2877 
2878  /* VECTRESET is supported only on ARMv7-M cores */
2879  cortex_m->vectreset_supported = armv7m->arm.arch == ARM_ARCH_V7M;
2880 
2881  /* Check for FPU, otherwise mark FPU register as non-existent */
2882  if (armv7m->fp_feature == FP_NONE)
2883  for (size_t idx = ARMV7M_FPU_FIRST_REG; idx <= ARMV7M_FPU_LAST_REG; idx++)
2884  armv7m->arm.core_cache->reg_list[idx].exist = false;
2885 
2886  /* TODO: MVE can be present without floating points. Revisit this test */
2887  if (armv7m->fp_feature != FPV5_MVE_F && armv7m->fp_feature != FPV5_MVE_I)
2888  armv7m->arm.core_cache->reg_list[ARMV8M_VPR].exist = false;
2889 
2890  if (cortex_m->core_info->arch == ARM_ARCH_V8M) {
2891  bool cm_has_tz = cortex_m_has_tz(target);
2892  bool main_ext = cortex_m_main_extension(target, cpuid);
2893  bool baseline = !main_ext;
2894 
2895  if (!cm_has_tz) {
2896  for (size_t idx = ARMV8M_TZ_FIRST_REG; idx <= ARMV8M_TZ_LAST_REG; idx++)
2897  armv7m->arm.core_cache->reg_list[idx].exist = false;
2898 
2899  if (baseline) {
2900  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2901  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2902  }
2903  } else {
2904  if (baseline) {
2905  /* ARMV8M without main extension but with the security extension has
2906  only two stack limit registers in Secure state */
2907  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM_NS].exist = false;
2908  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM_NS].exist = false;
2909  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2910  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2911  } else {
2912  /* There is no separate regsel for msplim/psplim of ARMV8M mainline
2913  with the security extension that would point to correct alias
2914  depending on security state of the processor, thus register marked
2915  as non-existing letting to choose between S/NS alias manually */
2916  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2917  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2918  }
2919  }
2920  } else {
2921  /* Security extension and stack limit checking introduced in ARMV8M */
2922  for (size_t idx = ARMV8M_TZ_FIRST_REG; idx <= ARMV8M_TZ_LAST_REG; idx++)
2923  armv7m->arm.core_cache->reg_list[idx].exist = false;
2924 
2925  armv7m->arm.core_cache->reg_list[ARMV8M_MSPLIM].exist = false;
2926  armv7m->arm.core_cache->reg_list[ARMV8M_PSPLIM].exist = false;
2927  }
2928 
2929  if (!armv7m->is_hla_target) {
2931  /* Cortex-M3/M4 have 4096 bytes autoincrement range,
2932  * s. ARM IHI 0031C: MEM-AP 7.2.2 */
2933  armv7m->debug_ap->tar_autoincr_block = (1 << 12);
2934  }
2935 
2936  retval = target_read_u32(target, DCB_DHCSR, &cortex_m->dcb_dhcsr);
2937  if (retval != ERROR_OK)
2938  return retval;
2939 
2940  /* Don't cumulate sticky S_RESET_ST at the very first read of DHCSR
2941  * as S_RESET_ST may indicate a reset that happened long time ago
2942  * (most probably the power-on reset before OpenOCD was started).
2943  * As we are just initializing the debug system we do not need
2944  * to call cortex_m_endreset_event() in the following poll.
2945  */
2946  if (!cortex_m->dcb_dhcsr_sticky_is_recent) {
2947  cortex_m->dcb_dhcsr_sticky_is_recent = true;
2948  if (cortex_m->dcb_dhcsr & S_RESET_ST) {
2949  LOG_TARGET_DEBUG(target, "reset happened some time ago, ignore");
2950  cortex_m->dcb_dhcsr &= ~S_RESET_ST;
2951  }
2952  }
2953  cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
2954 
2955  if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
2956  /* Enable debug requests */
2957  uint32_t dhcsr = (cortex_m->dcb_dhcsr | C_DEBUGEN) & ~(C_HALT | C_STEP | C_MASKINTS);
2958 
2959  retval = target_write_u32(target, DCB_DHCSR, DBGKEY | (dhcsr & 0x0000FFFFUL));
2960  if (retval != ERROR_OK)
2961  return retval;
2962  cortex_m->dcb_dhcsr = dhcsr;
2963  }
2964 
2965  /* Configure trace modules */
2966  retval = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
2967  if (retval != ERROR_OK)
2968  return retval;
2969 
2970  /* Configure ITM */
2972 
2973  /* NOTE: FPB and DWT are both optional. */
2974 
2975  /* Setup FPB */
2976  target_read_u32(target, FP_CTRL, &fpcr);
2977  /* bits [14:12] and [7:4] */
2978  cortex_m->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF);
2979  cortex_m->fp_num_lit = (fpcr >> 8) & 0xF;
2980  /* Detect flash patch revision, see RM DDI 0403E.b page C1-817.
2981  Revision is zero base, fp_rev == 1 means Rev.2 ! */
2982  cortex_m->fp_rev = (fpcr >> 28) & 0xf;
2983  free(cortex_m->fp_comparator_list);
2984  cortex_m->fp_comparator_list = calloc(
2985  cortex_m->fp_num_code + cortex_m->fp_num_lit,
2986  sizeof(struct cortex_m_fp_comparator));
2987  cortex_m->fpb_enabled = fpcr & 1;
2988  for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
2989  cortex_m->fp_comparator_list[i].type =
2990  (i < cortex_m->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
2991  cortex_m->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
2992 
2993  /* make sure we clear any breakpoints enabled on the target */
2995  }
2996  LOG_TARGET_DEBUG(target, "FPB fpcr 0x%" PRIx32 ", numcode %i, numlit %i",
2997  fpcr,
2998  cortex_m->fp_num_code,
2999  cortex_m->fp_num_lit);
3000 
3001  /* Setup DWT */
3003  cortex_m_dwt_setup(cortex_m, target);
3004 
3005  /* These hardware breakpoints only work for code in flash! */
3006  LOG_TARGET_INFO(target, "target has %d breakpoints, %d watchpoints",
3007  cortex_m->fp_num_code,
3008  cortex_m->dwt_num_comp);
3009 
3010  retval = armv7m_identify_cache(target);
3011  if (retval != ERROR_OK) {
3012  LOG_ERROR("Cannot detect cache");
3013  return retval;
3014  }
3015  }
3016 
3017  return ERROR_OK;
3018 }
3019 
3020 static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
3021 {
3022  struct armv7m_common *armv7m = target_to_armv7m(target);
3023  uint16_t dcrdr;
3024  uint8_t buf[2];
3025  int retval;
3026 
3027  retval = mem_ap_read_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
3028  if (retval != ERROR_OK)
3029  return retval;
3030 
3031  dcrdr = target_buffer_get_u16(target, buf);
3032  *ctrl = (uint8_t)dcrdr;
3033  *value = (uint8_t)(dcrdr >> 8);
3034 
3035  LOG_TARGET_DEBUG(target, "data 0x%x ctrl 0x%x", *value, *ctrl);
3036 
3037  /* write ack back to software dcc register
3038  * signify we have read data */
3039  if (dcrdr & (1 << 0)) {
3040  target_buffer_set_u16(target, buf, 0);
3041  retval = mem_ap_write_buf_noincr(armv7m->debug_ap, buf, 2, 1, DCB_DCRDR);
3042  if (retval != ERROR_OK)
3043  return retval;
3044  }
3045 
3046  return ERROR_OK;
3047 }
3048 
3050  uint32_t size, uint8_t *buffer)
3051 {
3052  uint8_t data;
3053  uint8_t ctrl;
3054  uint32_t i;
3055 
3056  for (i = 0; i < (size * 4); i++) {
3057  int retval = cortex_m_dcc_read(target, &data, &ctrl);
3058  if (retval != ERROR_OK)
3059  return retval;
3060  buffer[i] = data;
3061  }
3062 
3063  return ERROR_OK;
3064 }
3065 
3067 {
3068  struct target *target = priv;
3070  return ERROR_OK;
3071 
3072  if (!target->dbg_msg_enabled)
3073  return ERROR_OK;
3074 
3075  if (target->state == TARGET_RUNNING) {
3076  uint8_t data;
3077  uint8_t ctrl;
3078  int retval;
3079 
3080  retval = cortex_m_dcc_read(target, &data, &ctrl);
3081  if (retval != ERROR_OK)
3082  return retval;
3083 
3084  /* check if we have data */
3085  if (ctrl & (1 << 0)) {
3086  uint32_t request;
3087 
3088  /* we assume target is quick enough */
3089  request = data;
3090  for (int i = 1; i <= 3; i++) {
3091  retval = cortex_m_dcc_read(target, &data, &ctrl);
3092  if (retval != ERROR_OK)
3093  return retval;
3094  request |= ((uint32_t)data << (i * 8));
3095  }
3096  target_request(target, request);
3097  }
3098  }
3099 
3100  return ERROR_OK;
3101 }
3102 
3104  struct cortex_m_common *cortex_m, struct adiv5_dap *dap)
3105 {
3106  struct armv7m_common *armv7m = &cortex_m->armv7m;
3107 
3108  armv7m_init_arch_info(target, armv7m);
3109 
3110  /* default reset mode is to use srst if fitted
3111  * if not it will use CORTEX_M_RESET_VECTRESET */
3113 
3114  armv7m->arm.dap = dap;
3115 
3116  /* register arch-specific functions */
3118 
3119  armv7m->post_debug_entry = NULL;
3120 
3121  armv7m->pre_restore_context = NULL;
3122 
3125 
3128 
3129  return ERROR_OK;
3130 }
3131 
3133 {
3134  struct adiv5_private_config *pc;
3135 
3136  pc = (struct adiv5_private_config *)target->private_config;
3137  if (adiv5_verify_config(pc) != ERROR_OK)
3138  return ERROR_FAIL;
3139 
3140  struct cortex_m_common *cortex_m = calloc(1, sizeof(struct cortex_m_common));
3141  if (!cortex_m) {
3142  LOG_TARGET_ERROR(target, "No memory creating target");
3143  return ERROR_FAIL;
3144  }
3145 
3146  cortex_m->common_magic = CORTEX_M_COMMON_MAGIC;
3147  cortex_m->apsel = pc->ap_num;
3148 
3149  cortex_m_init_arch_info(target, cortex_m, pc->dap);
3150 
3151  return ERROR_OK;
3152 }
3153 
3154 /*--------------------------------------------------------------------------*/
3155 
3157  struct cortex_m_common *cm)
3158 {
3159  if (!is_cortex_m_with_dap_access(cm)) {
3160  command_print(cmd, "target is not a Cortex-M");
3161  return ERROR_TARGET_INVALID;
3162  }
3163  return ERROR_OK;
3164 }
3165 
3166 /*
3167  * Only stuff below this line should need to verify that its target
3168  * is a Cortex-M with available DAP access (not a HLA adapter).
3169  */
3170 
3171 COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
3172 {
3174  struct cortex_m_common *cortex_m = target_to_cm(target);
3175  struct armv7m_common *armv7m = &cortex_m->armv7m;
3176  uint32_t demcr = 0;
3177  int retval;
3178 
3179  static const struct {
3180  char name[10];
3181  unsigned int mask;
3182  } vec_ids[] = {
3183  { "hard_err", VC_HARDERR, },
3184  { "int_err", VC_INTERR, },
3185  { "bus_err", VC_BUSERR, },
3186  { "state_err", VC_STATERR, },
3187  { "chk_err", VC_CHKERR, },
3188  { "nocp_err", VC_NOCPERR, },
3189  { "mm_err", VC_MMERR, },
3190  { "reset", VC_CORERESET, },
3191  };
3192 
3193  retval = cortex_m_verify_pointer(CMD, cortex_m);
3194  if (retval != ERROR_OK)
3195  return retval;
3196 
3197  if (!target_was_examined(target)) {
3198  LOG_TARGET_ERROR(target, "Target not examined yet");
3199  return ERROR_FAIL;
3200  }
3201 
3202  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
3203  if (retval != ERROR_OK)
3204  return retval;
3205 
3206  if (CMD_ARGC > 0) {
3207  unsigned int catch = 0;
3208 
3209  if (CMD_ARGC == 1) {
3210  if (strcmp(CMD_ARGV[0], "all") == 0) {
3211  catch = VC_HARDERR | VC_INTERR | VC_BUSERR
3213  | VC_MMERR | VC_CORERESET;
3214  goto write;
3215  } else if (strcmp(CMD_ARGV[0], "none") == 0)
3216  goto write;
3217  }
3218  while (CMD_ARGC-- > 0) {
3219  unsigned int i;
3220  for (i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3221  if (strcmp(CMD_ARGV[CMD_ARGC], vec_ids[i].name) != 0)
3222  continue;
3223  catch |= vec_ids[i].mask;
3224  break;
3225  }
3226  if (i == ARRAY_SIZE(vec_ids)) {
3227  LOG_TARGET_ERROR(target, "No Cortex-M vector '%s'", CMD_ARGV[CMD_ARGC]);
3229  }
3230  }
3231 write:
3232  /* For now, armv7m->demcr only stores vector catch flags. */
3233  armv7m->demcr = catch;
3234 
3235  demcr &= ~0xffff;
3236  demcr |= catch;
3237 
3238  /* write, but don't assume it stuck (why not??) */
3239  retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, demcr);
3240  if (retval != ERROR_OK)
3241  return retval;
3242  retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &demcr);
3243  if (retval != ERROR_OK)
3244  return retval;
3245 
3246  /* FIXME be sure to clear DEMCR on clean server shutdown.
3247  * Otherwise the vector catch hardware could fire when there's
3248  * no debugger hooked up, causing much confusion...
3249  */
3250  }
3251 
3252  for (unsigned int i = 0; i < ARRAY_SIZE(vec_ids); i++) {
3253  command_print(CMD, "%9s: %s", vec_ids[i].name,
3254  (demcr & vec_ids[i].mask) ? "catch" : "ignore");
3255  }
3256 
3257  return ERROR_OK;
3258 }
3259 
3260 COMMAND_HANDLER(handle_cortex_m_mask_interrupts_command)
3261 {
3263  struct cortex_m_common *cortex_m = target_to_cm(target);
3264  int retval;
3265 
3266  static const struct nvp nvp_maskisr_modes[] = {
3267  { .name = "auto", .value = CORTEX_M_ISRMASK_AUTO },
3268  { .name = "off", .value = CORTEX_M_ISRMASK_OFF },
3269  { .name = "on", .value = CORTEX_M_ISRMASK_ON },
3270  { .name = "steponly", .value = CORTEX_M_ISRMASK_STEPONLY },
3271  { .name = NULL, .value = -1 },
3272  };
3273  const struct nvp *n;
3274 
3275 
3276  retval = cortex_m_verify_pointer(CMD, cortex_m);
3277  if (retval != ERROR_OK)
3278  return retval;
3279 
3280  if (target->state != TARGET_HALTED) {
3281  command_print(CMD, "Error: target must be stopped for \"%s\" command", CMD_NAME);
3282  return ERROR_TARGET_NOT_HALTED;
3283  }
3284 
3285  if (CMD_ARGC > 0) {
3286  n = nvp_name2value(nvp_maskisr_modes, CMD_ARGV[0]);
3287  if (!n->name)
3289  cortex_m->isrmasking_mode = n->value;
3291  }
3292 
3293  n = nvp_value2name(nvp_maskisr_modes, cortex_m->isrmasking_mode);
3294  command_print(CMD, "cortex_m interrupt mask %s", n->name);
3295 
3296  return ERROR_OK;
3297 }
3298 
3299 COMMAND_HANDLER(handle_cortex_m_reset_config_command)
3300 {
3302  struct cortex_m_common *cortex_m = target_to_cm(target);
3303 
3304  int retval = cortex_m_verify_pointer(CMD, cortex_m);
3305  if (retval != ERROR_OK)
3306  return retval;
3307 
3308  if (!CMD_ARGC) {
3309  char *reset_config;
3310 
3311  switch (cortex_m->soft_reset_config) {
3313  reset_config = "sysresetreq";
3314  break;
3315 
3317  reset_config = "vectreset";
3318  break;
3319 
3320  default:
3321  reset_config = "unknown";
3322  break;
3323  }
3324 
3325  command_print(CMD, "%s", reset_config);
3326  return ERROR_OK;
3327  } else if (CMD_ARGC != 1) {
3329  }
3330 
3331  if (!strcmp(CMD_ARGV[0], "sysresetreq")) {
3333  } else if (!strcmp(CMD_ARGV[0], "vectreset")) {
3335  && !cortex_m->vectreset_supported)
3336  LOG_TARGET_WARNING(target, "VECTRESET is not supported on your Cortex-M core");
3337  else
3339  } else {
3340  command_print(CMD, "invalid reset config '%s'", CMD_ARGV[0]);
3342  }
3343 
3344  return ERROR_OK;
3345 }
3346 
3347 COMMAND_HANDLER(handle_cortex_m_cache_info_command)
3348 {
3349  if (CMD_ARGC)
3351 
3353 
3355 }
3356 
3357 static const struct command_registration cortex_m_exec_command_handlers[] = {
3358  {
3359  .name = "maskisr",
3360  .handler = handle_cortex_m_mask_interrupts_command,
3361  .mode = COMMAND_EXEC,
3362  .help = "mask cortex_m interrupts",
3363  .usage = "['auto'|'on'|'off'|'steponly']",
3364  },
3365  {
3366  .name = "vector_catch",
3367  .handler = handle_cortex_m_vector_catch_command,
3368  .mode = COMMAND_EXEC,
3369  .help = "configure hardware vectors to trigger debug entry",
3370  .usage = "['all'|'none'|('bus_err'|'chk_err'|...)*]",
3371  },
3372  {
3373  .name = "reset_config",
3374  .handler = handle_cortex_m_reset_config_command,
3375  .mode = COMMAND_ANY,
3376  .help = "configure software reset handling",
3377  .usage = "['sysresetreq'|'vectreset']",
3378  },
3379  {
3380  .name = "cache_info",
3381  .handler = handle_cortex_m_cache_info_command,
3382  .mode = COMMAND_EXEC,
3383  .help = "display information about target caches",
3384  .usage = "",
3385  },
3386  {
3387  .chain = smp_command_handlers,
3388  },
3390 };
3391 static const struct command_registration cortex_m_command_handlers[] = {
3392  {
3394  },
3395  {
3397  },
3398  /* START_DEPRECATED_TPIU */
3399  {
3401  },
3402  /* END_DEPRECATED_TPIU */
3403  {
3404  .name = "cortex_m",
3405  .mode = COMMAND_EXEC,
3406  .help = "Cortex-M command group",
3407  .usage = "",
3409  },
3410  {
3412  },
3414 };
3415 
3416 struct target_type cortexm_target = {
3417  .name = "cortex_m",
3418 
3419  .poll = cortex_m_poll,
3420  .arch_state = armv7m_arch_state,
3421 
3422  .target_request_data = cortex_m_target_request_data,
3423 
3424  .halt = cortex_m_halt,
3425  .resume = cortex_m_resume,
3426  .step = cortex_m_step,
3427 
3428  .assert_reset = cortex_m_assert_reset,
3429  .deassert_reset = cortex_m_deassert_reset,
3430  .soft_reset_halt = cortex_m_soft_reset_halt,
3431 
3432  .get_gdb_arch = arm_get_gdb_arch,
3433  .get_gdb_reg_list = armv7m_get_gdb_reg_list,
3434 
3435  .read_memory = cortex_m_read_memory,
3436  .write_memory = cortex_m_write_memory,
3437  .checksum_memory = armv7m_checksum_memory,
3438  .blank_check_memory = armv7m_blank_check_memory,
3439 
3440  .run_algorithm = armv7m_run_algorithm,
3441  .start_algorithm = armv7m_start_algorithm,
3442  .wait_algorithm = armv7m_wait_algorithm,
3443 
3444  .add_breakpoint = cortex_m_add_breakpoint,
3445  .remove_breakpoint = cortex_m_remove_breakpoint,
3446  .add_watchpoint = cortex_m_add_watchpoint,
3447  .remove_watchpoint = cortex_m_remove_watchpoint,
3448  .hit_watchpoint = cortex_m_hit_watchpoint,
3449 
3450  .commands = cortex_m_command_handlers,
3451  .target_create = cortex_m_target_create,
3452  .target_jim_configure = adiv5_jim_configure,
3453  .init_target = cortex_m_init_target,
3454  .examine = cortex_m_examine,
3455  .deinit_target = cortex_m_deinit_target,
3456 
3457  .profiling = cortex_m_profiling,
3458 };
@ 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:1279
@ 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
@ 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
static struct armv7m_common * target_to_armv7m(struct target *target)
Definition: armv7m.h:273
@ ARMV7M_PRIMASK
Definition: armv7m.h:148
@ ARMV7M_FPU_LAST_REG
Definition: armv7m.h:213
@ ARMV7M_CORE_FIRST_REG
Definition: armv7m.h:210
@ 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_PSPLIM
Definition: armv7m.h:156
@ ARMV8M_MSPLIM
Definition: armv7m.h:155
@ ARMV8M_PSPLIM_NS
Definition: armv7m.h:164
@ ARMV7M_LAST_REG
Definition: armv7m.h:209
@ ARMV8M_VPR
Definition: armv7m.h:206
int armv7m_handle_cache_info_command(struct command_invocation *cmd, struct target *target)
Definition: armv7m_cache.c:242
int armv7m_i_cache_inval(struct target *target, uint32_t address, unsigned int length)
Definition: armv7m_cache.c:218
int armv7m_d_cache_flush(struct target *target, uint32_t address, unsigned int length)
Definition: armv7m_cache.c:194
int armv7m_identify_cache(struct target *target)
Definition: armv7m_cache.c:71
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:344
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:208
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:470
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:2739
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:2578
static int cortex_m_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2156
COMMAND_HANDLER(handle_cortex_m_vector_catch_command)
Definition: cortex_m.c:3171
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:2699
static int cortex_m_find_mem_ap(struct adiv5_dap *swjdp, struct adiv5_ap **debug_ap)
Definition: cortex_m.c:2749
static int cortex_m_dwt_get_reg(struct reg *reg)
Definition: cortex_m.c:2423
#define MVFR1_MVE_F
Definition: cortex_m.c:2747
static int cortex_m_poll_smp(struct list_head *smp_targets)
Definition: cortex_m.c:1158
static int cortex_m_restore_smp(struct target *target, bool handle_breakpoints)
Definition: cortex_m.c:1446
static int cortex_m_restart_one(struct target *target, bool debug_execution)
Definition: cortex_m.c:1423
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:3416
static int cortex_m_halt_one(struct target *target)
Definition: cortex_m.c:1214
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:3391
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:1198
void cortex_m_enable_watchpoints(struct target *target)
Definition: cortex_m.c:2280
static int cortex_m_target_request_data(struct target *target, uint32_t size, uint8_t *buffer)
Definition: cortex_m.c:3049
static int cortex_m_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: cortex_m.c:2320
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:2631
static int cortex_m_poll_one(struct target *target)
Definition: cortex_m.c:964
int cortex_m_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2189
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:3103
#define MVFR0_DP
Definition: cortex_m.c:2742
static int cortex_m_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2074
void cortex_m_enable_breakpoints(struct target *target)
Definition: cortex_m.c:1317
int cortex_m_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:2066
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:1138
#define MVFR0_DP_MASK
Definition: cortex_m.c:2741
static int cortex_m_smp_halt_all(struct list_head *smp_targets)
Definition: cortex_m.c:1119
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:3066
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:1861
static int cortex_m_target_create(struct target *target)
Definition: cortex_m.c:3132
#define MVFR1
Definition: cortex_m.c:2744
static const struct reg_arch_type dwt_reg_type
Definition: cortex_m.c:2483
static int cortex_m_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_m.c:1475
static void cortex_m_dwt_addreg(struct target *t, struct reg *r, const struct dwt_reg *d)
Definition: cortex_m.c:2488
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:2758
#define MVFR1_MVE_I
Definition: cortex_m.c:2746
#define MVFR1_MVE_MASK
Definition: cortex_m.c:2745
int cortex_m_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Definition: cortex_m.c:2232
static int cortex_m_assert_reset(struct target *target)
Definition: cortex_m.c:1700
int cortex_m_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:2042
static int cortex_m_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Definition: cortex_m.c:2251
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:3156
static int cortex_m_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: cortex_m.c:1504
static const struct command_registration cortex_m_exec_command_handlers[]
Definition: cortex_m.c:3357
static bool cortex_m_main_extension(struct target *target, uint32_t cpuid)
Definition: cortex_m.c:2616
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:2306
static int cortex_m_dcc_read(struct target *target, uint8_t *value, uint8_t *ctrl)
Definition: cortex_m.c:3020
int cortex_m_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1890
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:2600
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:2292
static void cortex_m_dwt_setup(struct cortex_m_common *cm, struct target *target)
Definition: cortex_m.c:2506
void cortex_m_deinit_target(struct target *target)
Definition: cortex_m.c:2328
int cortex_m_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_m.c:1988
#define MVFR0
Definition: cortex_m.c:2738
static int cortex_m_soft_reset_halt(struct target *target)
Definition: cortex_m.c:1252
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:2345
static const struct dwt_reg dwt_comp[]
Definition: cortex_m.c:2459
#define MVFR0_SP
Definition: cortex_m.c:2740
static int cortex_m_halt(struct target *target)
Definition: cortex_m.c:1244
#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:1329
static const struct dwt_reg dwt_base_regs[]
Definition: cortex_m.c:2450
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:2436
#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, uint16_t offset) __attribute__((unused))
Definition: opcodes.h:117
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:175
enum arm_arch arch
ARM architecture version.
Definition: arm.h:202
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:196
struct reg * cpsr
Handle to the CPSR/xPSR; valid in all core modes.
Definition: arm.h:184
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
Definition: arm.h:257
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:181
const int * map
Support for arm_reg_current()
Definition: arm.h:190
int(* read_core_reg)(struct target *target, struct reg *reg, int num, enum arm_mode mode)
Retrieve a single core register.
Definition: arm.h:224
struct reg_cache * core_cache
Definition: arm.h:178
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:2420
struct target * target
Definition: cortex_m.c:2418
uint32_t addr
Definition: cortex_m.c:2419
const char * name
Definition: cortex_m.c:2446
uint32_t addr
Definition: cortex_m.c:2445
unsigned int size
Definition: cortex_m.c:2447
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 @128 vec_ids[]