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