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