OpenOCD
cortex_a.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  * Copyright (C) 2009 by Dirk Behme *
14  * dirk.behme@gmail.com - copy from cortex_m3 *
15  * *
16  * Copyright (C) 2010 Øyvind Harboe *
17  * oyvind.harboe@zylin.com *
18  * *
19  * Copyright (C) ST-Ericsson SA 2011 *
20  * michel.jaouen@stericsson.com : smp minimum support *
21  * *
22  * Copyright (C) Broadcom 2012 *
23  * ehunter@broadcom.com : Cortex-R4 support *
24  * *
25  * Copyright (C) 2013 Kamal Dasu *
26  * kdasu.kdev@gmail.com *
27  * *
28  * Copyright (C) 2016 Chengyu Zheng *
29  * chengyu.zheng@polimi.it : watchpoint support *
30  * *
31  * Cortex-A8(tm) TRM, ARM DDI 0344H *
32  * Cortex-A9(tm) TRM, ARM DDI 0407F *
33  * Cortex-A4(tm) TRM, ARM DDI 0363E *
34  * Cortex-A15(tm)TRM, ARM DDI 0438C *
35  * *
36  ***************************************************************************/
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include "breakpoints.h"
43 #include "cortex_a.h"
44 #include "register.h"
45 #include "armv7a_mmu.h"
46 #include "target_request.h"
47 #include "target_type.h"
48 #include "arm_coresight.h"
49 #include "arm_opcodes.h"
50 #include "arm_semihosting.h"
51 #include "jtag/interface.h"
52 #include "transport/transport.h"
53 #include "smp.h"
54 #include <helper/bits.h>
55 #include <helper/nvp.h>
56 #include <helper/time_support.h>
57 #include <helper/align.h>
58 
59 static int cortex_a_poll(struct target *target);
60 static int cortex_a_debug_entry(struct target *target);
61 static int cortex_a_restore_context(struct target *target, bool bpwp);
62 static int cortex_a_set_breakpoint(struct target *target,
63  struct breakpoint *breakpoint, uint8_t matchmode);
65  struct breakpoint *breakpoint, uint8_t matchmode);
67  struct breakpoint *breakpoint);
68 static int cortex_a_unset_breakpoint(struct target *target,
69  struct breakpoint *breakpoint);
70 static int cortex_a_wait_dscr_bits(struct target *target, uint32_t mask,
71  uint32_t value, uint32_t *dscr);
72 static int cortex_a_mmu(struct target *target, bool *enabled);
73 static int cortex_a_mmu_modify(struct target *target, bool enable);
74 static int cortex_a_virt2phys(struct target *target,
75  target_addr_t virt, target_addr_t *phys);
76 static int cortex_a_read_cpu_memory(struct target *target,
77  uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
78 
79 static unsigned int ilog2(unsigned int x)
80 {
81  unsigned int y = 0;
82  x /= 2;
83  while (x) {
84  ++y;
85  x /= 2;
86  }
87  return y;
88 }
89 
90 /* restore cp15_control_reg at resume */
92 {
93  int retval = ERROR_OK;
94  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
95  struct armv7a_common *armv7a = target_to_armv7a(target);
96 
97  if (cortex_a->cp15_control_reg != cortex_a->cp15_control_reg_curr) {
98  cortex_a->cp15_control_reg_curr = cortex_a->cp15_control_reg;
99  /* LOG_INFO("cp15_control_reg: %8.8" PRIx32, cortex_a->cp15_control_reg); */
100  retval = armv7a->arm.mcr(target, 15,
101  0, 0, /* op1, op2 */
102  1, 0, /* CRn, CRm */
103  cortex_a->cp15_control_reg);
104  }
105  return retval;
106 }
107 
108 /*
109  * Set up ARM core for memory access.
110  * If !phys_access, switch to SVC mode and make sure MMU is on
111  * If phys_access, switch off mmu
112  */
113 static int cortex_a_prep_memaccess(struct target *target, bool phys_access)
114 {
115  struct armv7a_common *armv7a = target_to_armv7a(target);
116  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
117  bool mmu_enabled = false;
118 
119  if (!phys_access) {
121  cortex_a_mmu(target, &mmu_enabled);
122  if (mmu_enabled)
124  if (cortex_a->dacrfixup_mode == CORTEX_A_DACRFIXUP_ON) {
125  /* overwrite DACR to all-manager */
126  armv7a->arm.mcr(target, 15,
127  0, 0, 3, 0,
128  0xFFFFFFFF);
129  }
130  } else {
131  cortex_a_mmu(target, &mmu_enabled);
132  if (mmu_enabled)
133  cortex_a_mmu_modify(target, false);
134  }
135  return ERROR_OK;
136 }
137 
138 /*
139  * Restore ARM core after memory access.
140  * If !phys_access, switch to previous mode
141  * If phys_access, restore MMU setting
142  */
143 static int cortex_a_post_memaccess(struct target *target, bool phys_access)
144 {
145  struct armv7a_common *armv7a = target_to_armv7a(target);
146  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
147 
148  if (!phys_access) {
149  if (cortex_a->dacrfixup_mode == CORTEX_A_DACRFIXUP_ON) {
150  /* restore */
151  armv7a->arm.mcr(target, 15,
152  0, 0, 3, 0,
153  cortex_a->cp15_dacr_reg);
154  }
156  } else {
157  bool mmu_enabled = false;
158  cortex_a_mmu(target, &mmu_enabled);
159  if (mmu_enabled)
161  }
162  return ERROR_OK;
163 }
164 
165 
166 /* modify cp15_control_reg in order to enable or disable mmu for :
167  * - virt2phys address conversion
168  * - read or write memory in phys or virt address */
169 static int cortex_a_mmu_modify(struct target *target, bool enable)
170 {
171  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
172  struct armv7a_common *armv7a = target_to_armv7a(target);
173  int retval = ERROR_OK;
174  bool need_write = false;
175 
176  if (enable) {
177  /* if mmu enabled at target stop and mmu not enable */
178  if (!(cortex_a->cp15_control_reg & 0x1U)) {
179  LOG_ERROR("trying to enable mmu on target stopped with mmu disable");
180  return ERROR_FAIL;
181  }
182  if ((cortex_a->cp15_control_reg_curr & 0x1U) == 0) {
183  cortex_a->cp15_control_reg_curr |= 0x1U;
184  need_write = true;
185  }
186  } else {
187  if ((cortex_a->cp15_control_reg_curr & 0x1U) == 0x1U) {
188  cortex_a->cp15_control_reg_curr &= ~0x1U;
189  need_write = true;
190  }
191  }
192 
193  if (need_write) {
194  LOG_DEBUG("%s, writing cp15 ctrl: %" PRIx32,
195  enable ? "enable mmu" : "disable mmu",
196  cortex_a->cp15_control_reg_curr);
197 
198  retval = armv7a->arm.mcr(target, 15,
199  0, 0, /* op1, op2 */
200  1, 0, /* CRn, CRm */
201  cortex_a->cp15_control_reg_curr);
202  }
203  return retval;
204 }
205 
206 /*
207  * Cortex-A Basic debug access, very low level assumes state is saved
208  */
210 {
211  struct armv7a_common *armv7a = target_to_armv7a(target);
212  uint32_t dscr;
213  int retval;
214 
215  /* lock memory-mapped access to debug registers to prevent
216  * software interference */
217  retval = mem_ap_write_u32(armv7a->debug_ap,
218  armv7a->debug_base + CPUDBG_LOCKACCESS, 0);
219  if (retval != ERROR_OK)
220  return retval;
221 
222  /* Disable cacheline fills and force cache write-through in debug state */
223  retval = mem_ap_write_u32(armv7a->debug_ap,
224  armv7a->debug_base + CPUDBG_DSCCR, 0);
225  if (retval != ERROR_OK)
226  return retval;
227 
228  /* Disable TLB lookup and refill/eviction in debug state */
229  retval = mem_ap_write_u32(armv7a->debug_ap,
230  armv7a->debug_base + CPUDBG_DSMCR, 0);
231  if (retval != ERROR_OK)
232  return retval;
233 
234  retval = dap_run(armv7a->debug_ap->dap);
235  if (retval != ERROR_OK)
236  return retval;
237 
238  /* Enabling of instruction execution in debug mode is done in debug_entry code */
239 
240  /* Resync breakpoint registers */
241 
242  /* Enable halt for breakpoint, watchpoint and vector catch */
243  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
244  armv7a->debug_base + CPUDBG_DSCR, &dscr);
245  if (retval != ERROR_OK)
246  return retval;
247  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
248  armv7a->debug_base + CPUDBG_DSCR, dscr | DSCR_HALT_DBG_MODE);
249  if (retval != ERROR_OK)
250  return retval;
251 
252  /* Since this is likely called from init or reset, update target state information*/
253  return cortex_a_poll(target);
254 }
255 
256 static int cortex_a_wait_instrcmpl(struct target *target, uint32_t *dscr, bool force)
257 {
258  /* Waits until InstrCmpl_l becomes 1, indicating instruction is done.
259  * Writes final value of DSCR into *dscr. Pass force to force always
260  * reading DSCR at least once. */
261  struct armv7a_common *armv7a = target_to_armv7a(target);
262  int retval;
263 
264  if (force) {
265  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
266  armv7a->debug_base + CPUDBG_DSCR, dscr);
267  if (retval != ERROR_OK) {
268  LOG_ERROR("Could not read DSCR register");
269  return retval;
270  }
271  }
272 
274  if (retval != ERROR_OK)
275  LOG_ERROR("Error waiting for InstrCompl=1");
276  return retval;
277 }
278 
279 /* To reduce needless round-trips, pass in a pointer to the current
280  * DSCR value. Initialize it to zero if you just need to know the
281  * value on return from this function; or DSCR_INSTR_COMP if you
282  * happen to know that no instruction is pending.
283  */
284 static int cortex_a_exec_opcode(struct target *target,
285  uint32_t opcode, uint32_t *dscr_p)
286 {
287  uint32_t dscr;
288  int retval;
289  struct armv7a_common *armv7a = target_to_armv7a(target);
290 
291  dscr = dscr_p ? *dscr_p : 0;
292 
293  LOG_DEBUG("exec opcode 0x%08" PRIx32, opcode);
294 
295  /* Wait for InstrCompl bit to be set */
296  retval = cortex_a_wait_instrcmpl(target, dscr_p, false);
297  if (retval != ERROR_OK)
298  return retval;
299 
300  retval = mem_ap_write_u32(armv7a->debug_ap,
301  armv7a->debug_base + CPUDBG_ITR, opcode);
302  if (retval != ERROR_OK)
303  return retval;
304 
305  /* Wait for InstrCompl bit to be set */
306  retval = cortex_a_wait_instrcmpl(target, &dscr, true);
307  if (retval != ERROR_OK) {
308  LOG_ERROR("Error waiting for cortex_a_exec_opcode");
309  return retval;
310  }
311 
312  if (dscr_p)
313  *dscr_p = dscr;
314 
315  return retval;
316 }
317 
318 /*
319  * Cortex-A implementation of Debug Programmer's Model
320  *
321  * NOTE the invariant: these routines return with DSCR_INSTR_COMP set,
322  * so there's no need to poll for it before executing an instruction.
323  *
324  * NOTE that in several of these cases the "stall" mode might be useful.
325  * It'd let us queue a few operations together... prepare/finish might
326  * be the places to enable/disable that mode.
327  */
328 
329 static inline struct cortex_a_common *dpm_to_a(struct arm_dpm *dpm)
330 {
331  return container_of(dpm, struct cortex_a_common, armv7a_common.dpm);
332 }
333 
334 static int cortex_a_write_dcc(struct cortex_a_common *a, uint32_t data)
335 {
336  LOG_DEBUG("write DCC 0x%08" PRIx32, data);
339 }
340 
341 static int cortex_a_read_dcc(struct cortex_a_common *a, uint32_t *data,
342  uint32_t *dscr_p)
343 {
344  uint32_t dscr = DSCR_INSTR_COMP;
345  int retval;
346 
347  if (dscr_p)
348  dscr = *dscr_p;
349 
350  /* Wait for DTRRXfull */
353  if (retval != ERROR_OK) {
354  LOG_ERROR("Error waiting for read dcc");
355  return retval;
356  }
357 
360  if (retval != ERROR_OK)
361  return retval;
362  /* LOG_DEBUG("read DCC 0x%08" PRIx32, *data); */
363 
364  if (dscr_p)
365  *dscr_p = dscr;
366 
367  return retval;
368 }
369 
370 static int cortex_a_dpm_prepare(struct arm_dpm *dpm)
371 {
372  struct cortex_a_common *a = dpm_to_a(dpm);
373  uint32_t dscr;
374  int retval;
375 
376  /* set up invariant: INSTR_COMP is set after ever DPM operation */
377  retval = cortex_a_wait_instrcmpl(dpm->arm->target, &dscr, true);
378  if (retval != ERROR_OK) {
379  LOG_ERROR("Error waiting for dpm prepare");
380  return retval;
381  }
382 
383  /* this "should never happen" ... */
384  if (dscr & DSCR_DTR_RX_FULL) {
385  LOG_ERROR("DSCR_DTR_RX_FULL, dscr 0x%08" PRIx32, dscr);
386  /* Clear DCCRX */
387  retval = cortex_a_exec_opcode(
389  ARMV4_5_MRC(14, 0, 0, 0, 5, 0),
390  &dscr);
391  if (retval != ERROR_OK)
392  return retval;
393  }
394 
395  return retval;
396 }
397 
398 static int cortex_a_dpm_finish(struct arm_dpm *dpm)
399 {
400  /* REVISIT what could be done here? */
401  return ERROR_OK;
402 }
403 
404 static int cortex_a_instr_write_data_dcc(struct arm_dpm *dpm,
405  uint32_t opcode, uint32_t data)
406 {
407  struct cortex_a_common *a = dpm_to_a(dpm);
408  int retval;
409  uint32_t dscr = DSCR_INSTR_COMP;
410 
411  retval = cortex_a_write_dcc(a, data);
412  if (retval != ERROR_OK)
413  return retval;
414 
415  return cortex_a_exec_opcode(
417  opcode,
418  &dscr);
419 }
420 
422  uint8_t rt, uint32_t data)
423 {
424  struct cortex_a_common *a = dpm_to_a(dpm);
425  uint32_t dscr = DSCR_INSTR_COMP;
426  int retval;
427 
428  if (rt > 15)
429  return ERROR_TARGET_INVALID;
430 
431  retval = cortex_a_write_dcc(a, data);
432  if (retval != ERROR_OK)
433  return retval;
434 
435  /* DCCRX to Rt, "MCR p14, 0, R0, c0, c5, 0", 0xEE000E15 */
436  return cortex_a_exec_opcode(
438  ARMV4_5_MRC(14, 0, rt, 0, 5, 0),
439  &dscr);
440 }
441 
442 static int cortex_a_instr_write_data_r0(struct arm_dpm *dpm,
443  uint32_t opcode, uint32_t data)
444 {
445  struct cortex_a_common *a = dpm_to_a(dpm);
446  uint32_t dscr = DSCR_INSTR_COMP;
447  int retval;
448 
449  retval = cortex_a_instr_write_data_rt_dcc(dpm, 0, data);
450  if (retval != ERROR_OK)
451  return retval;
452 
453  /* then the opcode, taking data from R0 */
454  retval = cortex_a_exec_opcode(
456  opcode,
457  &dscr);
458 
459  return retval;
460 }
461 
463  uint32_t opcode, uint64_t data)
464 {
465  struct cortex_a_common *a = dpm_to_a(dpm);
466  uint32_t dscr = DSCR_INSTR_COMP;
467  int retval;
468 
469  retval = cortex_a_instr_write_data_rt_dcc(dpm, 0, data & 0xffffffffULL);
470  if (retval != ERROR_OK)
471  return retval;
472 
473  retval = cortex_a_instr_write_data_rt_dcc(dpm, 1, data >> 32);
474  if (retval != ERROR_OK)
475  return retval;
476 
477  /* then the opcode, taking data from R0, R1 */
479  opcode,
480  &dscr);
481  return retval;
482 }
483 
484 static int cortex_a_instr_cpsr_sync(struct arm_dpm *dpm)
485 {
486  struct target *target = dpm->arm->target;
487  uint32_t dscr = DSCR_INSTR_COMP;
488 
489  /* "Prefetch flush" after modifying execution status in CPSR */
491  ARMV4_5_MCR(15, 0, 0, 7, 5, 4),
492  &dscr);
493 }
494 
495 static int cortex_a_instr_read_data_dcc(struct arm_dpm *dpm,
496  uint32_t opcode, uint32_t *data)
497 {
498  struct cortex_a_common *a = dpm_to_a(dpm);
499  int retval;
500  uint32_t dscr = DSCR_INSTR_COMP;
501 
502  /* the opcode, writing data to DCC */
503  retval = cortex_a_exec_opcode(
505  opcode,
506  &dscr);
507  if (retval != ERROR_OK)
508  return retval;
509 
510  return cortex_a_read_dcc(a, data, &dscr);
511 }
512 
514  uint8_t rt, uint32_t *data)
515 {
516  struct cortex_a_common *a = dpm_to_a(dpm);
517  uint32_t dscr = DSCR_INSTR_COMP;
518  int retval;
519 
520  if (rt > 15)
521  return ERROR_TARGET_INVALID;
522 
523  retval = cortex_a_exec_opcode(
525  ARMV4_5_MCR(14, 0, rt, 0, 5, 0),
526  &dscr);
527  if (retval != ERROR_OK)
528  return retval;
529 
530  return cortex_a_read_dcc(a, data, &dscr);
531 }
532 
533 static int cortex_a_instr_read_data_r0(struct arm_dpm *dpm,
534  uint32_t opcode, uint32_t *data)
535 {
536  struct cortex_a_common *a = dpm_to_a(dpm);
537  uint32_t dscr = DSCR_INSTR_COMP;
538  int retval;
539 
540  /* the opcode, writing data to R0 */
541  retval = cortex_a_exec_opcode(
543  opcode,
544  &dscr);
545  if (retval != ERROR_OK)
546  return retval;
547 
548  /* write R0 to DCC */
549  return cortex_a_instr_read_data_rt_dcc(dpm, 0, data);
550 }
551 
553  uint32_t opcode, uint64_t *data)
554 {
555  uint32_t lo, hi;
556  int retval;
557 
558  /* the opcode, writing data to RO, R1 */
559  retval = cortex_a_instr_read_data_r0(dpm, opcode, &lo);
560  if (retval != ERROR_OK)
561  return retval;
562 
563  *data = lo;
564 
565  /* write R1 to DCC */
566  retval = cortex_a_instr_read_data_rt_dcc(dpm, 1, &hi);
567  if (retval != ERROR_OK)
568  return retval;
569 
570  *data |= (uint64_t)hi << 32;
571 
572  return retval;
573 }
574 
575 static int cortex_a_bpwp_enable(struct arm_dpm *dpm, unsigned int index_t,
576  uint32_t addr, uint32_t control)
577 {
578  struct cortex_a_common *a = dpm_to_a(dpm);
579  uint32_t vr = a->armv7a_common.debug_base;
580  uint32_t cr = a->armv7a_common.debug_base;
581  int retval;
582 
583  switch (index_t) {
584  case 0 ... 15: /* breakpoints */
585  vr += CPUDBG_BVR_BASE;
586  cr += CPUDBG_BCR_BASE;
587  break;
588  case 16 ... 31: /* watchpoints */
589  vr += CPUDBG_WVR_BASE;
590  cr += CPUDBG_WCR_BASE;
591  index_t -= 16;
592  break;
593  default:
594  return ERROR_FAIL;
595  }
596  vr += 4 * index_t;
597  cr += 4 * index_t;
598 
599  LOG_DEBUG("A: bpwp enable, vr %08" PRIx32 " cr %08" PRIx32, vr, cr);
600 
602  vr, addr);
603  if (retval != ERROR_OK)
604  return retval;
606  cr, control);
607  return retval;
608 }
609 
610 static int cortex_a_bpwp_disable(struct arm_dpm *dpm, unsigned int index_t)
611 {
612  struct cortex_a_common *a = dpm_to_a(dpm);
613  uint32_t cr;
614 
615  switch (index_t) {
616  case 0 ... 15:
618  break;
619  case 16 ... 31:
621  index_t -= 16;
622  break;
623  default:
624  return ERROR_FAIL;
625  }
626  cr += 4 * index_t;
627 
628  LOG_DEBUG("A: bpwp disable, cr %08" PRIx32, cr);
629 
630  /* clear control register */
632 }
633 
634 static int cortex_a_dpm_setup(struct cortex_a_common *a, uint32_t didr)
635 {
636  struct arm_dpm *dpm = &a->armv7a_common.dpm;
637  int retval;
638 
639  dpm->arm = &a->armv7a_common.arm;
640  dpm->didr = didr;
641 
644 
649 
653 
656 
657  retval = arm_dpm_setup(dpm);
658  if (retval == ERROR_OK)
659  retval = arm_dpm_initialize(dpm);
660 
661  return retval;
662 }
663 static struct target *get_cortex_a(struct target *target, int32_t coreid)
664 {
665  struct target_list *head;
666 
668  struct target *curr = head->target;
669  if ((curr->coreid == coreid) && (curr->state == TARGET_HALTED))
670  return curr;
671  }
672  return target;
673 }
674 static int cortex_a_halt(struct target *target);
675 
676 static int cortex_a_halt_smp(struct target *target)
677 {
678  int retval = 0;
679  struct target_list *head;
680 
682  struct target *curr = head->target;
683  if ((curr != target) && (curr->state != TARGET_HALTED)
684  && target_was_examined(curr))
685  retval += cortex_a_halt(curr);
686  }
687  return retval;
688 }
689 
690 static int update_halt_gdb(struct target *target)
691 {
692  struct target *gdb_target = NULL;
693  struct target_list *head;
694  struct target *curr;
695  int retval = 0;
696 
697  if (target->gdb_service && target->gdb_service->core[0] == -1) {
700  retval += cortex_a_halt_smp(target);
701  }
702 
703  if (target->gdb_service)
704  gdb_target = target->gdb_service->target;
705 
707  curr = head->target;
708  /* skip calling context */
709  if (curr == target)
710  continue;
711  if (!target_was_examined(curr))
712  continue;
713  /* skip targets that were already halted */
714  if (curr->state == TARGET_HALTED)
715  continue;
716  /* Skip gdb_target; it alerts GDB so has to be polled as last one */
717  if (curr == gdb_target)
718  continue;
719 
720  /* avoid recursion in cortex_a_poll() */
721  curr->smp = 0;
722  cortex_a_poll(curr);
723  curr->smp = 1;
724  }
725 
726  /* after all targets were updated, poll the gdb serving target */
727  if (gdb_target && gdb_target != target)
728  cortex_a_poll(gdb_target);
729  return retval;
730 }
731 
732 /*
733  * Cortex-A Run control
734  */
735 
736 static int cortex_a_poll(struct target *target)
737 {
738  int retval = ERROR_OK;
739  uint32_t dscr;
740  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
741  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
742  enum target_state prev_target_state = target->state;
743  /* toggle to another core is done by gdb as follow */
744  /* maint packet J core_id */
745  /* continue */
746  /* the next polling trigger an halt event sent to gdb */
747  if ((target->state == TARGET_HALTED) && (target->smp) &&
748  (target->gdb_service) &&
749  (!target->gdb_service->target)) {
753  return retval;
754  }
755  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
756  armv7a->debug_base + CPUDBG_DSCR, &dscr);
757  if (retval != ERROR_OK)
758  return retval;
759  cortex_a->cpudbg_dscr = dscr;
760 
762  if (prev_target_state != TARGET_HALTED) {
763  /* We have a halting debug event */
764  LOG_TARGET_DEBUG(target, "Target halted");
766 
767  retval = cortex_a_debug_entry(target);
768  if (retval != ERROR_OK)
769  return retval;
770 
771  if (target->smp) {
772  retval = update_halt_gdb(target);
773  if (retval != ERROR_OK)
774  return retval;
775  }
776 
777  if (prev_target_state == TARGET_DEBUG_RUNNING) {
779  } else { /* prev_target_state is RUNNING, UNKNOWN or RESET */
780  if (arm_semihosting(target, &retval) != 0)
781  return retval;
782 
785  }
786  }
787  } else
789 
790  return retval;
791 }
792 
793 static int cortex_a_halt(struct target *target)
794 {
795  int retval;
796  uint32_t dscr;
797  struct armv7a_common *armv7a = target_to_armv7a(target);
798 
799  /*
800  * Tell the core to be halted by writing DRCR with 0x1
801  * and then wait for the core to be halted.
802  */
803  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
804  armv7a->debug_base + CPUDBG_DRCR, DRCR_HALT);
805  if (retval != ERROR_OK)
806  return retval;
807 
808  dscr = 0; /* force read of dscr */
810  DSCR_CORE_HALTED, &dscr);
811  if (retval != ERROR_OK) {
812  LOG_TARGET_ERROR(target, "Error waiting for halt");
813  return retval;
814  }
815 
817 
818  return ERROR_OK;
819 }
820 
821 static int cortex_a_internal_restore(struct target *target, bool current,
822  target_addr_t *address, bool handle_breakpoints, bool debug_execution)
823 {
824  struct armv7a_common *armv7a = target_to_armv7a(target);
825  struct arm *arm = &armv7a->arm;
826  int retval;
827  uint32_t resume_pc;
828 
829  if (!debug_execution)
831 
832 #if 0
833  if (debug_execution) {
834  /* Disable interrupts */
835  /* We disable interrupts in the PRIMASK register instead of
836  * masking with C_MASKINTS,
837  * This is probably the same issue as Cortex-M3 Errata 377493:
838  * C_MASKINTS in parallel with disabled interrupts can cause
839  * local faults to not be taken. */
840  buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
841  armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = true;
842  armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = true;
843 
844  /* Make sure we are in Thumb mode */
845  buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_XPSR].value, 0, 32,
846  buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_XPSR].value, 0,
847  32) | (1 << 24));
848  armv7m->core_cache->reg_list[ARMV7M_XPSR].dirty = true;
849  armv7m->core_cache->reg_list[ARMV7M_XPSR].valid = true;
850  }
851 #endif
852 
853  /* current = true: continue on current pc, otherwise continue at <address> */
854  resume_pc = buf_get_u32(arm->pc->value, 0, 32);
855  if (!current)
856  resume_pc = *address;
857  else
858  *address = resume_pc;
859 
860  /* Make sure that the Armv7 gdb thumb fixups does not
861  * kill the return address
862  */
863  switch (arm->core_state) {
864  case ARM_STATE_ARM:
865  resume_pc &= 0xFFFFFFFC;
866  break;
867  case ARM_STATE_THUMB:
868  case ARM_STATE_THUMB_EE:
869  /* When the return address is loaded into PC
870  * bit 0 must be 1 to stay in Thumb state
871  */
872  resume_pc |= 0x1;
873  break;
874  case ARM_STATE_JAZELLE:
875  LOG_TARGET_ERROR(target, "How do I resume into Jazelle state??");
876  return ERROR_FAIL;
877  case ARM_STATE_AARCH64:
878  LOG_TARGET_ERROR(target, "Shouldn't be in AARCH64 state");
879  return ERROR_FAIL;
880  }
881  LOG_TARGET_DEBUG(target, "resume pc = 0x%08" PRIx32, resume_pc);
882  buf_set_u32(arm->pc->value, 0, 32, resume_pc);
883  arm->pc->dirty = true;
884  arm->pc->valid = true;
885 
886  /* restore dpm_mode at system halt */
888  /* called it now before restoring context because it uses cpu
889  * register r0 for restoring cp15 control register */
891  if (retval != ERROR_OK)
892  return retval;
893  retval = cortex_a_restore_context(target, handle_breakpoints);
894  if (retval != ERROR_OK)
895  return retval;
898 
899  /* registers are now invalid */
901 
902 #if 0
903  /* the front-end may request us not to handle breakpoints */
904  if (handle_breakpoints) {
905  /* Single step past breakpoint at current address */
906  breakpoint = breakpoint_find(target, resume_pc);
907  if (breakpoint) {
908  LOG_DEBUG("unset breakpoint at 0x%8.8x", breakpoint->address);
909  cortex_m3_unset_breakpoint(target, breakpoint);
910  cortex_m3_single_step_core(target);
911  cortex_m3_set_breakpoint(target, breakpoint);
912  }
913  }
914 
915 #endif
916  return retval;
917 }
918 
920 {
921  struct armv7a_common *armv7a = target_to_armv7a(target);
922  struct arm *arm = &armv7a->arm;
923  int retval;
924  uint32_t dscr;
925  /*
926  * * Restart core and wait for it to be started. Clear ITRen and sticky
927  * * exception flags: see ARMv7 ARM, C5.9.
928  *
929  * REVISIT: for single stepping, we probably want to
930  * disable IRQs by default, with optional override...
931  */
932 
933  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
934  armv7a->debug_base + CPUDBG_DSCR, &dscr);
935  if (retval != ERROR_OK)
936  return retval;
937 
938  if ((dscr & DSCR_INSTR_COMP) == 0)
939  LOG_TARGET_ERROR(target, "DSCR InstrCompl must be set before leaving debug!");
940 
941  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
942  armv7a->debug_base + CPUDBG_DSCR, dscr & ~DSCR_ITR_EN);
943  if (retval != ERROR_OK)
944  return retval;
945 
946  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
947  armv7a->debug_base + CPUDBG_DRCR, DRCR_RESTART |
949  if (retval != ERROR_OK)
950  return retval;
951 
952  dscr = 0; /* force read of dscr */
954  DSCR_CORE_RESTARTED, &dscr);
955  if (retval != ERROR_OK) {
956  LOG_TARGET_ERROR(target, "Error waiting for resume");
957  return retval;
958  }
959 
962 
963  /* registers are now invalid */
965 
966  return ERROR_OK;
967 }
968 
969 static int cortex_a_restore_smp(struct target *target, bool handle_breakpoints)
970 {
971  int retval = ERROR_OK;
972  struct target_list *head;
974 
976  struct target *curr = head->target;
977  if ((curr != target) && (curr->state != TARGET_RUNNING)
978  && target_was_examined(curr)) {
979  /* resume current address , not in step mode */
980  int retval2 = cortex_a_internal_restore(curr, true, &address,
981  handle_breakpoints, false);
982 
983  if (retval2 == ERROR_OK)
984  retval2 = cortex_a_internal_restart(curr);
985 
986  if (retval2 == ERROR_OK)
988 
989  if (retval == ERROR_OK)
990  retval = retval2; // save the first error
991  }
992  }
993  return retval;
994 }
995 
996 static int cortex_a_resume(struct target *target, bool current,
997  target_addr_t address, bool handle_breakpoints, bool debug_execution)
998 {
999  int retval = 0;
1000  /* dummy resume for smp toggle in order to reduce gdb impact */
1001  if ((target->smp) && (target->gdb_service->core[1] != -1)) {
1002  /* simulate a start and halt of target */
1005  /* fake resume at next poll we play the target core[1], see poll*/
1007  return 0;
1008  }
1009  cortex_a_internal_restore(target, current, &address, handle_breakpoints,
1010  debug_execution);
1011  if (target->smp) {
1012  target->gdb_service->core[0] = -1;
1013  retval = cortex_a_restore_smp(target, handle_breakpoints);
1014  if (retval != ERROR_OK)
1015  return retval;
1016  }
1018 
1019  if (!debug_execution) {
1022  LOG_TARGET_DEBUG(target, "target resumed at " TARGET_ADDR_FMT, address);
1023  } else {
1026  LOG_TARGET_DEBUG(target, "target debug resumed at " TARGET_ADDR_FMT, address);
1027  }
1028 
1029  return ERROR_OK;
1030 }
1031 
1033 {
1034  uint32_t dscr;
1035  int retval = ERROR_OK;
1036  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1037  struct armv7a_common *armv7a = target_to_armv7a(target);
1038  struct arm *arm = &armv7a->arm;
1039 
1040  LOG_TARGET_DEBUG(target, "dscr = 0x%08" PRIx32, cortex_a->cpudbg_dscr);
1041 
1042  /* REVISIT surely we should not re-read DSCR !! */
1043  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1044  armv7a->debug_base + CPUDBG_DSCR, &dscr);
1045  if (retval != ERROR_OK)
1046  return retval;
1047 
1048  /* REVISIT see A TRM 12.11.4 steps 2..3 -- make sure that any
1049  * imprecise data aborts get discarded by issuing a Data
1050  * Synchronization Barrier: ARMV4_5_MCR(15, 0, 0, 7, 10, 4).
1051  */
1052 
1053  /* Enable the ITR execution once we are in debug mode */
1054  dscr |= DSCR_ITR_EN;
1055  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1056  armv7a->debug_base + CPUDBG_DSCR, dscr);
1057  if (retval != ERROR_OK)
1058  return retval;
1059 
1060  /* Examine debug reason */
1061  arm_dpm_report_dscr(&armv7a->dpm, cortex_a->cpudbg_dscr);
1062 
1063  /* save address of instruction that triggered the watchpoint? */
1065  uint32_t wfar;
1066 
1067  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1068  armv7a->debug_base + CPUDBG_WFAR,
1069  &wfar);
1070  if (retval != ERROR_OK)
1071  return retval;
1072  arm_dpm_report_wfar(&armv7a->dpm, wfar);
1073  }
1074 
1075  /* First load register accessible through core debug port */
1076  retval = arm_dpm_read_current_registers(&armv7a->dpm);
1077  if (retval != ERROR_OK)
1078  return retval;
1079 
1080  if (arm->spsr) {
1081  /* read SPSR */
1082  retval = arm_dpm_read_reg(&armv7a->dpm, arm->spsr, 17);
1083  if (retval != ERROR_OK)
1084  return retval;
1085  }
1086 
1087 #if 0
1088 /* TODO, Move this */
1089  uint32_t cp15_control_register, cp15_cacr, cp15_nacr;
1090  cortex_a_read_cp(target, &cp15_control_register, 15, 0, 1, 0, 0);
1091  LOG_DEBUG("cp15_control_register = 0x%08x", cp15_control_register);
1092 
1093  cortex_a_read_cp(target, &cp15_cacr, 15, 0, 1, 0, 2);
1094  LOG_DEBUG("cp15 Coprocessor Access Control Register = 0x%08x", cp15_cacr);
1095 
1096  cortex_a_read_cp(target, &cp15_nacr, 15, 0, 1, 1, 2);
1097  LOG_DEBUG("cp15 Nonsecure Access Control Register = 0x%08x", cp15_nacr);
1098 #endif
1099 
1100  /* Are we in an exception handler */
1101 /* armv4_5->exception_number = 0; */
1102  if (armv7a->post_debug_entry) {
1103  retval = armv7a->post_debug_entry(target);
1104  if (retval != ERROR_OK)
1105  return retval;
1106  }
1107 
1108  return retval;
1109 }
1110 
1112 {
1113  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1114  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1115  int retval;
1116 
1117  /* MRC p15,0,<Rt>,c1,c0,0 ; Read CP15 System Control Register */
1118  retval = armv7a->arm.mrc(target, 15,
1119  0, 0, /* op1, op2 */
1120  1, 0, /* CRn, CRm */
1121  &cortex_a->cp15_control_reg);
1122  if (retval != ERROR_OK)
1123  return retval;
1124  LOG_TARGET_DEBUG(target, "cp15_control_reg: %8.8" PRIx32,
1125  cortex_a->cp15_control_reg);
1126  cortex_a->cp15_control_reg_curr = cortex_a->cp15_control_reg;
1127 
1128  if (!armv7a->is_armv7r)
1130 
1131  if (!armv7a->armv7a_mmu.armv7a_cache.info_valid)
1133 
1134  if (armv7a->is_armv7r) {
1135  armv7a->armv7a_mmu.mmu_enabled = false;
1136  } else {
1137  armv7a->armv7a_mmu.mmu_enabled = cortex_a->cp15_control_reg & 0x1U;
1138  }
1140  cortex_a->cp15_control_reg & 0x4U;
1142  cortex_a->cp15_control_reg & 0x1000U;
1143  cortex_a->curr_mode = armv7a->arm.core_mode;
1144 
1145  /* switch to SVC mode to read DACR */
1146  arm_dpm_modeswitch(&armv7a->dpm, ARM_MODE_SVC);
1147  armv7a->arm.mrc(target, 15,
1148  0, 0, 3, 0,
1149  &cortex_a->cp15_dacr_reg);
1150 
1151  LOG_DEBUG("cp15_dacr_reg: %8.8" PRIx32,
1152  cortex_a->cp15_dacr_reg);
1153 
1154  arm_dpm_modeswitch(&armv7a->dpm, ARM_MODE_ANY);
1155  return ERROR_OK;
1156 }
1157 
1159  unsigned long bit_mask, unsigned long value)
1160 {
1161  struct armv7a_common *armv7a = target_to_armv7a(target);
1162  uint32_t dscr;
1163 
1164  /* Read DSCR */
1165  int retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1166  armv7a->debug_base + CPUDBG_DSCR, &dscr);
1167  if (retval != ERROR_OK)
1168  return retval;
1169 
1170  /* clear bitfield */
1171  dscr &= ~bit_mask;
1172  /* put new value */
1173  dscr |= value & bit_mask;
1174 
1175  /* write new DSCR */
1176  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1177  armv7a->debug_base + CPUDBG_DSCR, dscr);
1178  return retval;
1179 }
1180 
1181 /*
1182  * Single-step on ARMv7a/r is implemented through a HW breakpoint that hits
1183  * every instruction at any address except the address of the current
1184  * instruction.
1185  * Such HW breakpoint is never hit in case of a single instruction that jumps
1186  * on itself (infinite loop), or a WFI or a WFE. In this case, halt the CPU
1187  * after a timeout.
1188  * The jump on itself would be executed several times before the timeout forces
1189  * the halt, but this is not an issue. In ARMv7a/r there are few "pathological"
1190  * instructions, listed below, that jumps on itself and that can have side
1191  * effects if executed more than once; but they are not considered as real use
1192  * cases generated by a compiler.
1193  * Some example:
1194  * - 'pop {pc}' or multi register 'pop' including PC, when the new PC value is
1195  * the same value of current PC. The single step will not stop at the first
1196  * 'pop' and will continue taking values from the stack, modifying SP at each
1197  * iteration.
1198  * - 'rfeda', 'rfedb', 'rfeia', 'rfeib', when the new PC value is the same
1199  * value of current PC. The register provided to the instruction (usually SP)
1200  * will be incremented or decremented at each iteration.
1201  *
1202  * TODO: fix exit in case of error, cleaning HW breakpoints.
1203  */
1204 static int cortex_a_step(struct target *target, bool current, target_addr_t address,
1205  bool handle_breakpoints)
1206 {
1207  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1208  struct armv7a_common *armv7a = target_to_armv7a(target);
1209  struct arm *arm = &armv7a->arm;
1210  struct breakpoint *breakpoint = NULL;
1211  struct breakpoint stepbreakpoint;
1212  struct reg *r;
1213  int retval;
1214 
1215  if (target->state != TARGET_HALTED) {
1216  LOG_TARGET_ERROR(target, "not halted");
1217  return ERROR_TARGET_NOT_HALTED;
1218  }
1219 
1220  /* current = true: continue on current pc, otherwise continue at <address> */
1221  r = arm->pc;
1222  if (!current)
1223  buf_set_u32(r->value, 0, 32, address);
1224  else
1225  address = buf_get_u32(r->value, 0, 32);
1226 
1227  /* The front-end may request us not to handle breakpoints.
1228  * But since Cortex-A uses breakpoint for single step,
1229  * we MUST handle breakpoints.
1230  */
1231  handle_breakpoints = true;
1232  if (handle_breakpoints) {
1234  if (breakpoint)
1236  }
1237 
1238  /* Setup single step breakpoint */
1239  stepbreakpoint.address = address;
1240  stepbreakpoint.asid = 0;
1241  stepbreakpoint.length = (arm->core_state == ARM_STATE_THUMB)
1242  ? 2 : 4;
1243  stepbreakpoint.type = BKPT_HARD;
1244  stepbreakpoint.is_set = false;
1245 
1246  /* Disable interrupts during single step if requested */
1247  if (cortex_a->isrmasking_mode == CORTEX_A_ISRMASK_ON) {
1249  if (retval != ERROR_OK)
1250  return retval;
1251  }
1252 
1253  /* Break on IVA mismatch */
1254  cortex_a_set_breakpoint(target, &stepbreakpoint, 0x04);
1255 
1257 
1258  retval = cortex_a_resume(target, true, address, false, false);
1259  if (retval != ERROR_OK)
1260  return retval;
1261 
1262  // poll at least once before starting the timeout
1263  retval = cortex_a_poll(target);
1264  if (retval != ERROR_OK)
1265  return retval;
1266 
1267  int64_t then = timeval_ms() + 100;
1268  while (target->state != TARGET_HALTED) {
1269  if (timeval_ms() > then)
1270  break;
1271 
1272  retval = cortex_a_poll(target);
1273  if (retval != ERROR_OK)
1274  return retval;
1275  }
1276 
1277  if (target->state != TARGET_HALTED) {
1278  LOG_TARGET_DEBUG(target, "timeout waiting for target halt, try halt");
1279 
1280  retval = cortex_a_halt(target);
1281  if (retval != ERROR_OK)
1282  return retval;
1283 
1284  retval = cortex_a_poll(target);
1285  if (retval != ERROR_OK)
1286  return retval;
1287 
1288  if (target->state != TARGET_HALTED) {
1289  LOG_TARGET_ERROR(target, "timeout waiting for target halt");
1290  return ERROR_FAIL;
1291  }
1292  }
1293 
1294  cortex_a_unset_breakpoint(target, &stepbreakpoint);
1295 
1296  /* Re-enable interrupts if they were disabled */
1297  if (cortex_a->isrmasking_mode == CORTEX_A_ISRMASK_ON) {
1299  if (retval != ERROR_OK)
1300  return retval;
1301  }
1302 
1303 
1305 
1306  if (breakpoint)
1308 
1309  return ERROR_OK;
1310 }
1311 
1312 static int cortex_a_restore_context(struct target *target, bool bpwp)
1313 {
1314  struct armv7a_common *armv7a = target_to_armv7a(target);
1315 
1316  LOG_TARGET_DEBUG(target, " ");
1317 
1318  if (armv7a->pre_restore_context)
1319  armv7a->pre_restore_context(target);
1320 
1321  return arm_dpm_write_dirty_registers(&armv7a->dpm, bpwp);
1322 }
1323 
1324 /*
1325  * Cortex-A Breakpoint and watchpoint functions
1326  */
1327 
1328 /* Setup hardware Breakpoint Register Pair */
1330  struct breakpoint *breakpoint, uint8_t matchmode)
1331 {
1332  int retval;
1333  int brp_i = 0;
1334  uint32_t control;
1335  uint8_t byte_addr_select = 0x0F;
1336  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1337  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1338  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1339 
1340  if (breakpoint->is_set) {
1341  LOG_WARNING("breakpoint already set");
1342  return ERROR_OK;
1343  }
1344 
1345  if (breakpoint->type == BKPT_HARD) {
1346  while (brp_list[brp_i].used && (brp_i < cortex_a->brp_num))
1347  brp_i++;
1348  if (brp_i >= cortex_a->brp_num) {
1349  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1351  }
1352  breakpoint_hw_set(breakpoint, brp_i);
1353  if (breakpoint->length == 3) {
1354  /* Thumb-2 breakpoint: fixup to length 4 if word aligned,
1355  * set byte mask for length 2 if unaligned */
1356  if (IS_ALIGNED(breakpoint->address, 4))
1357  breakpoint->length = 4;
1358  else
1359  breakpoint->length = 2;
1360  }
1361  if (breakpoint->length == 2)
1362  byte_addr_select = (3 << (breakpoint->address & 0x02));
1363  control = ((matchmode & 0x7) << 20)
1364  | (byte_addr_select << 5)
1365  | (3 << 1) | 1;
1366  brp_list[brp_i].used = true;
1367  brp_list[brp_i].value = (breakpoint->address & 0xFFFFFFFC);
1368  brp_list[brp_i].control = control;
1369  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1370  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1371  brp_list[brp_i].value);
1372  if (retval != ERROR_OK)
1373  return retval;
1374  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1375  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1376  brp_list[brp_i].control);
1377  if (retval != ERROR_OK)
1378  return retval;
1379  LOG_DEBUG("brp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1380  brp_list[brp_i].control,
1381  brp_list[brp_i].value);
1382  } else if (breakpoint->type == BKPT_SOFT) {
1383  uint8_t code[4];
1384  if (breakpoint->length == 2) {
1385  /* length == 2: Thumb breakpoint */
1386  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1387  } else if (breakpoint->length == 3) {
1388  /* length == 3: Thumb-2 breakpoint, actual encoding is
1389  * a regular Thumb BKPT instruction but we replace a
1390  * 32bit Thumb-2 instruction, so fix-up the breakpoint
1391  * length
1392  */
1393  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1394  breakpoint->length = 4;
1395  } else {
1396  /* length == 4, normal ARM breakpoint */
1397  buf_set_u32(code, 0, 32, ARMV5_BKPT(0x11));
1398  }
1399 
1400  /*
1401  * ARMv7-A/R fetches instructions in little-endian on both LE and BE CPUs.
1402  * But Cortex-R4 and Cortex-R5 big-endian require BE instructions.
1403  * https://developer.arm.com/documentation/den0042/a/Coding-for-Cortex-R-Processors/Endianness
1404  * https://developer.arm.com/documentation/den0013/d/Porting/Endianness
1405  */
1406  if ((((cortex_a->cpuid & CPUDBG_CPUID_MASK) == CPUDBG_CPUID_CORTEX_R4) ||
1407  ((cortex_a->cpuid & CPUDBG_CPUID_MASK) == CPUDBG_CPUID_CORTEX_R5)) &&
1409  // In place swapping is allowed
1410  buf_bswap32(code, code, 4);
1411  }
1412 
1413  retval = target_read_memory(target,
1414  breakpoint->address & 0xFFFFFFFE,
1415  breakpoint->length, 1,
1417  if (retval != ERROR_OK)
1418  return retval;
1419 
1420  /* make sure data cache is cleaned & invalidated down to PoC */
1422 
1423  retval = target_write_memory(target,
1424  breakpoint->address & 0xFFFFFFFE,
1425  breakpoint->length, 1, code);
1426  if (retval != ERROR_OK)
1427  return retval;
1428 
1429  /* update i-cache at breakpoint location */
1432 
1433  breakpoint->is_set = true;
1434  }
1435 
1436  return ERROR_OK;
1437 }
1438 
1440  struct breakpoint *breakpoint, uint8_t matchmode)
1441 {
1442  int retval = ERROR_FAIL;
1443  int brp_i = 0;
1444  uint32_t control;
1445  uint8_t byte_addr_select = 0x0F;
1446  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1447  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1448  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1449 
1450  if (breakpoint->is_set) {
1451  LOG_WARNING("breakpoint already set");
1452  return retval;
1453  }
1454  /*check available context BRPs*/
1455  while ((brp_list[brp_i].used ||
1456  (brp_list[brp_i].type != BRP_CONTEXT)) && (brp_i < cortex_a->brp_num))
1457  brp_i++;
1458 
1459  if (brp_i >= cortex_a->brp_num) {
1460  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1461  return ERROR_FAIL;
1462  }
1463 
1464  breakpoint_hw_set(breakpoint, brp_i);
1465  control = ((matchmode & 0x7) << 20)
1466  | (byte_addr_select << 5)
1467  | (3 << 1) | 1;
1468  brp_list[brp_i].used = true;
1469  brp_list[brp_i].value = (breakpoint->asid);
1470  brp_list[brp_i].control = control;
1471  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1472  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1473  brp_list[brp_i].value);
1474  if (retval != ERROR_OK)
1475  return retval;
1476  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1477  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1478  brp_list[brp_i].control);
1479  if (retval != ERROR_OK)
1480  return retval;
1481  LOG_DEBUG("brp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1482  brp_list[brp_i].control,
1483  brp_list[brp_i].value);
1484  return ERROR_OK;
1485 
1486 }
1487 
1489 {
1490  int retval = ERROR_FAIL;
1491  int brp_1 = 0; /* holds the contextID pair */
1492  int brp_2 = 0; /* holds the IVA pair */
1493  uint32_t control_ctx, control_iva;
1494  uint8_t ctx_byte_addr_select = 0x0F;
1495  uint8_t iva_byte_addr_select = 0x0F;
1496  uint8_t ctx_machmode = 0x03;
1497  uint8_t iva_machmode = 0x01;
1498  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1499  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1500  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1501 
1502  if (breakpoint->is_set) {
1503  LOG_WARNING("breakpoint already set");
1504  return retval;
1505  }
1506  /*check available context BRPs*/
1507  while ((brp_list[brp_1].used ||
1508  (brp_list[brp_1].type != BRP_CONTEXT)) && (brp_1 < cortex_a->brp_num))
1509  brp_1++;
1510 
1511  LOG_DEBUG("brp(CTX) found num: %d", brp_1);
1512  if (brp_1 >= cortex_a->brp_num) {
1513  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1514  return ERROR_FAIL;
1515  }
1516 
1517  while ((brp_list[brp_2].used ||
1518  (brp_list[brp_2].type != BRP_NORMAL)) && (brp_2 < cortex_a->brp_num))
1519  brp_2++;
1520 
1521  LOG_DEBUG("brp(IVA) found num: %d", brp_2);
1522  if (brp_2 >= cortex_a->brp_num) {
1523  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1524  return ERROR_FAIL;
1525  }
1526 
1527  breakpoint_hw_set(breakpoint, brp_1);
1528  breakpoint->linked_brp = brp_2;
1529  control_ctx = ((ctx_machmode & 0x7) << 20)
1530  | (brp_2 << 16)
1531  | (0 << 14)
1532  | (ctx_byte_addr_select << 5)
1533  | (3 << 1) | 1;
1534  brp_list[brp_1].used = true;
1535  brp_list[brp_1].value = (breakpoint->asid);
1536  brp_list[brp_1].control = control_ctx;
1537  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1538  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_1].brpn,
1539  brp_list[brp_1].value);
1540  if (retval != ERROR_OK)
1541  return retval;
1542  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1543  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_1].brpn,
1544  brp_list[brp_1].control);
1545  if (retval != ERROR_OK)
1546  return retval;
1547 
1548  control_iva = ((iva_machmode & 0x7) << 20)
1549  | (brp_1 << 16)
1550  | (iva_byte_addr_select << 5)
1551  | (3 << 1) | 1;
1552  brp_list[brp_2].used = true;
1553  brp_list[brp_2].value = (breakpoint->address & 0xFFFFFFFC);
1554  brp_list[brp_2].control = control_iva;
1555  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1556  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_2].brpn,
1557  brp_list[brp_2].value);
1558  if (retval != ERROR_OK)
1559  return retval;
1560  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1561  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_2].brpn,
1562  brp_list[brp_2].control);
1563  if (retval != ERROR_OK)
1564  return retval;
1565 
1566  return ERROR_OK;
1567 }
1568 
1570 {
1571  int retval;
1572  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1573  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1574  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1575 
1576  if (!breakpoint->is_set) {
1577  LOG_WARNING("breakpoint not set");
1578  return ERROR_OK;
1579  }
1580 
1581  if (breakpoint->type == BKPT_HARD) {
1582  if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
1583  int brp_i = breakpoint->number;
1584  int brp_j = breakpoint->linked_brp;
1585  if (brp_i >= cortex_a->brp_num) {
1586  LOG_DEBUG("Invalid BRP number in breakpoint");
1587  return ERROR_OK;
1588  }
1589  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1590  brp_list[brp_i].control, brp_list[brp_i].value);
1591  brp_list[brp_i].used = false;
1592  brp_list[brp_i].value = 0;
1593  brp_list[brp_i].control = 0;
1594  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1595  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1596  brp_list[brp_i].control);
1597  if (retval != ERROR_OK)
1598  return retval;
1599  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1600  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1601  brp_list[brp_i].value);
1602  if (retval != ERROR_OK)
1603  return retval;
1604  if ((brp_j < 0) || (brp_j >= cortex_a->brp_num)) {
1605  LOG_DEBUG("Invalid BRP number in breakpoint");
1606  return ERROR_OK;
1607  }
1608  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_j,
1609  brp_list[brp_j].control, brp_list[brp_j].value);
1610  brp_list[brp_j].used = false;
1611  brp_list[brp_j].value = 0;
1612  brp_list[brp_j].control = 0;
1613  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1614  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_j].brpn,
1615  brp_list[brp_j].control);
1616  if (retval != ERROR_OK)
1617  return retval;
1618  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1619  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_j].brpn,
1620  brp_list[brp_j].value);
1621  if (retval != ERROR_OK)
1622  return retval;
1623  breakpoint->linked_brp = 0;
1624  breakpoint->is_set = false;
1625  return ERROR_OK;
1626 
1627  } else {
1628  int brp_i = breakpoint->number;
1629  if (brp_i >= cortex_a->brp_num) {
1630  LOG_DEBUG("Invalid BRP number in breakpoint");
1631  return ERROR_OK;
1632  }
1633  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1634  brp_list[brp_i].control, brp_list[brp_i].value);
1635  brp_list[brp_i].used = false;
1636  brp_list[brp_i].value = 0;
1637  brp_list[brp_i].control = 0;
1638  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1639  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1640  brp_list[brp_i].control);
1641  if (retval != ERROR_OK)
1642  return retval;
1643  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1644  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1645  brp_list[brp_i].value);
1646  if (retval != ERROR_OK)
1647  return retval;
1648  breakpoint->is_set = false;
1649  return ERROR_OK;
1650  }
1651  } else {
1652 
1653  /* make sure data cache is cleaned & invalidated down to PoC */
1655  breakpoint->length);
1656 
1657  /* restore original instruction (kept in target endianness) */
1658  if (breakpoint->length == 4) {
1659  retval = target_write_memory(target,
1660  breakpoint->address & 0xFFFFFFFE,
1661  4, 1, breakpoint->orig_instr);
1662  if (retval != ERROR_OK)
1663  return retval;
1664  } else {
1665  retval = target_write_memory(target,
1666  breakpoint->address & 0xFFFFFFFE,
1667  2, 1, breakpoint->orig_instr);
1668  if (retval != ERROR_OK)
1669  return retval;
1670  }
1671 
1672  /* update i-cache at breakpoint location */
1674  breakpoint->length);
1676  breakpoint->length);
1677  }
1678  breakpoint->is_set = false;
1679 
1680  return ERROR_OK;
1681 }
1682 
1684  struct breakpoint *breakpoint)
1685 {
1686  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1687 
1688  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1689  LOG_INFO("no hardware breakpoint available");
1691  }
1692 
1693  if (breakpoint->type == BKPT_HARD)
1694  cortex_a->brp_num_available--;
1695 
1696  return cortex_a_set_breakpoint(target, breakpoint, 0x00); /* Exact match */
1697 }
1698 
1700  struct breakpoint *breakpoint)
1701 {
1702  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1703 
1704  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1705  LOG_INFO("no hardware breakpoint available");
1707  }
1708 
1709  if (breakpoint->type == BKPT_HARD)
1710  cortex_a->brp_num_available--;
1711 
1712  return cortex_a_set_context_breakpoint(target, breakpoint, 0x02); /* asid match */
1713 }
1714 
1716  struct breakpoint *breakpoint)
1717 {
1718  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1719 
1720  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1721  LOG_INFO("no hardware breakpoint available");
1723  }
1724 
1725  if (breakpoint->type == BKPT_HARD)
1726  cortex_a->brp_num_available--;
1727 
1729 }
1730 
1731 
1733 {
1734  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1735 
1736 #if 0
1737 /* It is perfectly possible to remove breakpoints while the target is running */
1738  if (target->state != TARGET_HALTED) {
1739  LOG_WARNING("target not halted");
1740  return ERROR_TARGET_NOT_HALTED;
1741  }
1742 #endif
1743 
1744  if (breakpoint->is_set) {
1746  if (breakpoint->type == BKPT_HARD)
1747  cortex_a->brp_num_available++;
1748  }
1749 
1750 
1751  return ERROR_OK;
1752 }
1753 
1765 {
1766  int retval = ERROR_OK;
1767  int wrp_i = 0;
1768  uint32_t control;
1769  uint32_t address;
1770  uint8_t address_mask;
1771  uint8_t byte_address_select;
1772  uint8_t load_store_access_control = 0x3;
1773  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1774  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1775  struct cortex_a_wrp *wrp_list = cortex_a->wrp_list;
1776 
1777  if (watchpoint->is_set) {
1778  LOG_WARNING("watchpoint already set");
1779  return retval;
1780  }
1781 
1782  /* check available context WRPs */
1783  while (wrp_list[wrp_i].used && (wrp_i < cortex_a->wrp_num))
1784  wrp_i++;
1785 
1786  if (wrp_i >= cortex_a->wrp_num) {
1787  LOG_ERROR("ERROR Can not find free Watchpoint Register Pair");
1788  return ERROR_FAIL;
1789  }
1790 
1791  if (watchpoint->length == 0 || watchpoint->length > 0x80000000U ||
1792  (watchpoint->length & (watchpoint->length - 1))) {
1793  LOG_WARNING("watchpoint length must be a power of 2");
1794  return ERROR_FAIL;
1795  }
1796 
1797  if (watchpoint->address & (watchpoint->length - 1)) {
1798  LOG_WARNING("watchpoint address must be aligned at length");
1799  return ERROR_FAIL;
1800  }
1801 
1802  /* FIXME: ARM DDI 0406C: address_mask is optional. What to do if it's missing? */
1803  /* handle wp length 1 and 2 through byte select */
1804  switch (watchpoint->length) {
1805  case 1:
1806  byte_address_select = BIT(watchpoint->address & 0x3);
1807  address = watchpoint->address & ~0x3;
1808  address_mask = 0;
1809  break;
1810 
1811  case 2:
1812  byte_address_select = 0x03 << (watchpoint->address & 0x2);
1813  address = watchpoint->address & ~0x3;
1814  address_mask = 0;
1815  break;
1816 
1817  case 4:
1818  byte_address_select = 0x0f;
1820  address_mask = 0;
1821  break;
1822 
1823  default:
1824  byte_address_select = 0xff;
1826  address_mask = ilog2(watchpoint->length);
1827  break;
1828  }
1829 
1830  watchpoint_set(watchpoint, wrp_i);
1831  control = (address_mask << 24) |
1832  (byte_address_select << 5) |
1833  (load_store_access_control << 3) |
1834  (0x3 << 1) | 1;
1835  wrp_list[wrp_i].used = true;
1836  wrp_list[wrp_i].value = address;
1837  wrp_list[wrp_i].control = control;
1838 
1839  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1840  armv7a->debug_base + CPUDBG_WVR_BASE + 4 * wrp_list[wrp_i].wrpn,
1841  wrp_list[wrp_i].value);
1842  if (retval != ERROR_OK)
1843  return retval;
1844 
1845  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1846  armv7a->debug_base + CPUDBG_WCR_BASE + 4 * wrp_list[wrp_i].wrpn,
1847  wrp_list[wrp_i].control);
1848  if (retval != ERROR_OK)
1849  return retval;
1850 
1851  LOG_DEBUG("wp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, wrp_i,
1852  wrp_list[wrp_i].control,
1853  wrp_list[wrp_i].value);
1854 
1855  return ERROR_OK;
1856 }
1857 
1867 {
1868  int retval;
1869  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1870  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1871  struct cortex_a_wrp *wrp_list = cortex_a->wrp_list;
1872 
1873  if (!watchpoint->is_set) {
1874  LOG_WARNING("watchpoint not set");
1875  return ERROR_OK;
1876  }
1877 
1878  int wrp_i = watchpoint->number;
1879  if (wrp_i >= cortex_a->wrp_num) {
1880  LOG_DEBUG("Invalid WRP number in watchpoint");
1881  return ERROR_OK;
1882  }
1883  LOG_DEBUG("wrp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, wrp_i,
1884  wrp_list[wrp_i].control, wrp_list[wrp_i].value);
1885  wrp_list[wrp_i].used = false;
1886  wrp_list[wrp_i].value = 0;
1887  wrp_list[wrp_i].control = 0;
1888  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1889  armv7a->debug_base + CPUDBG_WCR_BASE + 4 * wrp_list[wrp_i].wrpn,
1890  wrp_list[wrp_i].control);
1891  if (retval != ERROR_OK)
1892  return retval;
1893  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1894  armv7a->debug_base + CPUDBG_WVR_BASE + 4 * wrp_list[wrp_i].wrpn,
1895  wrp_list[wrp_i].value);
1896  if (retval != ERROR_OK)
1897  return retval;
1898  watchpoint->is_set = false;
1899 
1900  return ERROR_OK;
1901 }
1902 
1912 {
1913  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1914 
1915  if (cortex_a->wrp_num_available < 1) {
1916  LOG_INFO("no hardware watchpoint available");
1918  }
1919 
1920  int retval = cortex_a_set_watchpoint(target, watchpoint);
1921  if (retval != ERROR_OK)
1922  return retval;
1923 
1924  cortex_a->wrp_num_available--;
1925  return ERROR_OK;
1926 }
1927 
1937 {
1938  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1939 
1940  if (watchpoint->is_set) {
1941  cortex_a->wrp_num_available++;
1943  }
1944  return ERROR_OK;
1945 }
1946 
1947 
1948 /*
1949  * Cortex-A Reset functions
1950  */
1951 
1953 {
1954  struct armv7a_common *armv7a = target_to_armv7a(target);
1955 
1956  LOG_DEBUG(" ");
1957 
1958  /* FIXME when halt is requested, make it work somehow... */
1959 
1960  /* This function can be called in "target not examined" state */
1961 
1962  /* Issue some kind of warm reset. */
1965  else if (jtag_get_reset_config() & RESET_HAS_SRST) {
1966  /* REVISIT handle "pulls" cases, if there's
1967  * hardware that needs them to work.
1968  */
1969 
1970  /*
1971  * FIXME: fix reset when transport is not JTAG. This is a temporary
1972  * work-around for release v0.10 that is not intended to stay!
1973  */
1974  if (!transport_is_jtag() ||
1977 
1978  } else {
1979  LOG_ERROR("%s: how to reset?", target_name(target));
1980  return ERROR_FAIL;
1981  }
1982 
1983  /* registers are now invalid */
1984  if (armv7a->arm.core_cache)
1986 
1988 
1989  return ERROR_OK;
1990 }
1991 
1993 {
1994  struct armv7a_common *armv7a = target_to_armv7a(target);
1995  int retval;
1996 
1997  LOG_DEBUG(" ");
1998 
1999  /* be certain SRST is off */
2001 
2002  if (target_was_examined(target)) {
2003  retval = cortex_a_poll(target);
2004  if (retval != ERROR_OK)
2005  return retval;
2006  }
2007 
2008  if (target->reset_halt) {
2009  if (target->state != TARGET_HALTED) {
2010  LOG_WARNING("%s: ran after reset and before halt ...",
2011  target_name(target));
2012  if (target_was_examined(target)) {
2013  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2014  armv7a->debug_base + CPUDBG_DRCR, DRCR_HALT);
2015  if (retval != ERROR_OK)
2016  return retval;
2017  } else
2019  }
2020  }
2021 
2022  return ERROR_OK;
2023 }
2024 
2025 static int cortex_a_set_dcc_mode(struct target *target, uint32_t mode, uint32_t *dscr)
2026 {
2027  /* Changes the mode of the DCC between non-blocking, stall, and fast mode.
2028  * New desired mode must be in mode. Current value of DSCR must be in
2029  * *dscr, which is updated with new value.
2030  *
2031  * This function elides actually sending the mode-change over the debug
2032  * interface if the mode is already set as desired.
2033  */
2034  uint32_t new_dscr = (*dscr & ~DSCR_EXT_DCC_MASK) | mode;
2035  if (new_dscr != *dscr) {
2036  struct armv7a_common *armv7a = target_to_armv7a(target);
2037  int retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2038  armv7a->debug_base + CPUDBG_DSCR, new_dscr);
2039  if (retval == ERROR_OK)
2040  *dscr = new_dscr;
2041  return retval;
2042  } else {
2043  return ERROR_OK;
2044  }
2045 }
2046 
2047 static int cortex_a_wait_dscr_bits(struct target *target, uint32_t mask,
2048  uint32_t value, uint32_t *dscr)
2049 {
2050  /* Waits until the specified bit(s) of DSCR take on a specified value. */
2051  struct armv7a_common *armv7a = target_to_armv7a(target);
2052  int64_t then;
2053  int retval;
2054 
2055  if ((*dscr & mask) == value)
2056  return ERROR_OK;
2057 
2058  then = timeval_ms();
2059  while (1) {
2060  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2061  armv7a->debug_base + CPUDBG_DSCR, dscr);
2062  if (retval != ERROR_OK) {
2063  LOG_ERROR("Could not read DSCR register");
2064  return retval;
2065  }
2066  if ((*dscr & mask) == value)
2067  break;
2068  if (timeval_ms() > then + 1000) {
2069  LOG_ERROR("timeout waiting for DSCR bit change");
2070  return ERROR_FAIL;
2071  }
2072  }
2073  return ERROR_OK;
2074 }
2075 
2076 static int cortex_a_read_copro(struct target *target, uint32_t opcode,
2077  uint32_t *data, uint32_t *dscr)
2078 {
2079  int retval;
2080  struct armv7a_common *armv7a = target_to_armv7a(target);
2081 
2082  /* Move from coprocessor to R0. */
2083  retval = cortex_a_exec_opcode(target, opcode, dscr);
2084  if (retval != ERROR_OK)
2085  return retval;
2086 
2087  /* Move from R0 to DTRTX. */
2088  retval = cortex_a_exec_opcode(target, ARMV4_5_MCR(14, 0, 0, 0, 5, 0), dscr);
2089  if (retval != ERROR_OK)
2090  return retval;
2091 
2092  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture
2093  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2094  * must also check TXfull_l). Most of the time this will be free
2095  * because TXfull_l will be set immediately and cached in dscr. */
2097  DSCR_DTRTX_FULL_LATCHED, dscr);
2098  if (retval != ERROR_OK)
2099  return retval;
2100 
2101  /* Read the value transferred to DTRTX. */
2102  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2103  armv7a->debug_base + CPUDBG_DTRTX, data);
2104  if (retval != ERROR_OK)
2105  return retval;
2106 
2107  return ERROR_OK;
2108 }
2109 
2110 static int cortex_a_read_dfar_dfsr(struct target *target, uint32_t *dfar,
2111  uint32_t *dfsr, uint32_t *dscr)
2112 {
2113  int retval;
2114 
2115  if (dfar) {
2116  retval = cortex_a_read_copro(target, ARMV4_5_MRC(15, 0, 0, 6, 0, 0), dfar, dscr);
2117  if (retval != ERROR_OK)
2118  return retval;
2119  }
2120 
2121  if (dfsr) {
2122  retval = cortex_a_read_copro(target, ARMV4_5_MRC(15, 0, 0, 5, 0, 0), dfsr, dscr);
2123  if (retval != ERROR_OK)
2124  return retval;
2125  }
2126 
2127  return ERROR_OK;
2128 }
2129 
2130 static int cortex_a_write_copro(struct target *target, uint32_t opcode,
2131  uint32_t data, uint32_t *dscr)
2132 {
2133  int retval;
2134  struct armv7a_common *armv7a = target_to_armv7a(target);
2135 
2136  /* Write the value into DTRRX. */
2137  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2138  armv7a->debug_base + CPUDBG_DTRRX, data);
2139  if (retval != ERROR_OK)
2140  return retval;
2141 
2142  /* Move from DTRRX to R0. */
2143  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), dscr);
2144  if (retval != ERROR_OK)
2145  return retval;
2146 
2147  /* Move from R0 to coprocessor. */
2148  retval = cortex_a_exec_opcode(target, opcode, dscr);
2149  if (retval != ERROR_OK)
2150  return retval;
2151 
2152  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture manual
2153  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2154  * check RXfull_l). Most of the time this will be free because RXfull_l
2155  * will be cleared immediately and cached in dscr. */
2157  if (retval != ERROR_OK)
2158  return retval;
2159 
2160  return ERROR_OK;
2161 }
2162 
2163 static int cortex_a_write_dfar_dfsr(struct target *target, uint32_t dfar,
2164  uint32_t dfsr, uint32_t *dscr)
2165 {
2166  int retval;
2167 
2168  retval = cortex_a_write_copro(target, ARMV4_5_MCR(15, 0, 0, 6, 0, 0), dfar, dscr);
2169  if (retval != ERROR_OK)
2170  return retval;
2171 
2172  retval = cortex_a_write_copro(target, ARMV4_5_MCR(15, 0, 0, 5, 0, 0), dfsr, dscr);
2173  if (retval != ERROR_OK)
2174  return retval;
2175 
2176  return ERROR_OK;
2177 }
2178 
2179 static int cortex_a_dfsr_to_error_code(uint32_t dfsr)
2180 {
2181  uint32_t status, upper4;
2182 
2183  if (dfsr & (1 << 9)) {
2184  /* LPAE format. */
2185  status = dfsr & 0x3f;
2186  upper4 = status >> 2;
2187  if (upper4 == 1 || upper4 == 2 || upper4 == 3 || upper4 == 15)
2189  else if (status == 33)
2191  else
2192  return ERROR_TARGET_DATA_ABORT;
2193  } else {
2194  /* Normal format. */
2195  status = ((dfsr >> 6) & 0x10) | (dfsr & 0xf);
2196  if (status == 1)
2198  else if (status == 5 || status == 7 || status == 3 || status == 6 ||
2199  status == 9 || status == 11 || status == 13 || status == 15)
2201  else
2202  return ERROR_TARGET_DATA_ABORT;
2203  }
2204 }
2205 
2207  uint32_t size, uint32_t count, const uint8_t *buffer, uint32_t *dscr)
2208 {
2209  /* Writes count objects of size size from *buffer. Old value of DSCR must
2210  * be in *dscr; updated to new value. This is slow because it works for
2211  * non-word-sized objects. Avoid unaligned accesses as they do not work
2212  * on memory address space without "Normal" attribute. If size == 4 and
2213  * the address is aligned, cortex_a_write_cpu_memory_fast should be
2214  * preferred.
2215  * Preconditions:
2216  * - Address is in R0.
2217  * - R0 is marked dirty.
2218  */
2219  struct armv7a_common *armv7a = target_to_armv7a(target);
2220  struct arm *arm = &armv7a->arm;
2221  int retval;
2222 
2223  /* Mark register R1 as dirty, to use for transferring data. */
2224  arm_reg_current(arm, 1)->dirty = true;
2225 
2226  /* Switch to non-blocking mode if not already in that mode. */
2228  if (retval != ERROR_OK)
2229  return retval;
2230 
2231  /* Go through the objects. */
2232  while (count) {
2233  /* Write the value to store into DTRRX. */
2234  uint32_t data, opcode;
2235  if (size == 1)
2236  data = *buffer;
2237  else if (size == 2)
2239  else
2241  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2242  armv7a->debug_base + CPUDBG_DTRRX, data);
2243  if (retval != ERROR_OK)
2244  return retval;
2245 
2246  /* Transfer the value from DTRRX to R1. */
2247  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), dscr);
2248  if (retval != ERROR_OK)
2249  return retval;
2250 
2251  /* Write the value transferred to R1 into memory. */
2252  if (size == 1)
2253  opcode = ARMV4_5_STRB_IP(1, 0);
2254  else if (size == 2)
2255  opcode = ARMV4_5_STRH_IP(1, 0);
2256  else
2257  opcode = ARMV4_5_STRW_IP(1, 0);
2258  retval = cortex_a_exec_opcode(target, opcode, dscr);
2259  if (retval != ERROR_OK)
2260  return retval;
2261 
2262  /* Check for faults and return early. */
2264  return ERROR_OK; /* A data fault is not considered a system failure. */
2265 
2266  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture
2267  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2268  * must also check RXfull_l). Most of the time this will be free
2269  * because RXfull_l will be cleared immediately and cached in dscr. */
2271  if (retval != ERROR_OK)
2272  return retval;
2273 
2274  /* Advance. */
2275  buffer += size;
2276  --count;
2277  }
2278 
2279  return ERROR_OK;
2280 }
2281 
2283  uint32_t count, const uint8_t *buffer, uint32_t *dscr)
2284 {
2285  /* Writes count objects of size 4 from *buffer. Old value of DSCR must be
2286  * in *dscr; updated to new value. This is fast but only works for
2287  * word-sized objects at aligned addresses.
2288  * Preconditions:
2289  * - Address is in R0 and must be a multiple of 4.
2290  * - R0 is marked dirty.
2291  */
2292  struct armv7a_common *armv7a = target_to_armv7a(target);
2293  int retval;
2294 
2295  /* Switch to fast mode if not already in that mode. */
2297  if (retval != ERROR_OK)
2298  return retval;
2299 
2300  /* Latch STC instruction. */
2301  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2302  armv7a->debug_base + CPUDBG_ITR, ARMV4_5_STC(0, 1, 0, 1, 14, 5, 0, 4));
2303  if (retval != ERROR_OK)
2304  return retval;
2305 
2306  /* Transfer all the data and issue all the instructions. */
2307  return mem_ap_write_buf_noincr(armv7a->debug_ap, buffer,
2308  4, count, armv7a->debug_base + CPUDBG_DTRRX);
2309 }
2310 
2312  uint32_t address, uint32_t size,
2313  uint32_t count, const uint8_t *buffer)
2314 {
2315  /* Write memory through the CPU. */
2316  int retval, final_retval;
2317  struct armv7a_common *armv7a = target_to_armv7a(target);
2318  struct arm *arm = &armv7a->arm;
2319  uint32_t dscr, orig_dfar, orig_dfsr, fault_dscr, fault_dfar, fault_dfsr;
2320 
2321  LOG_DEBUG("Writing CPU memory address 0x%" PRIx32 " size %" PRIu32 " count %" PRIu32,
2322  address, size, count);
2323  if (target->state != TARGET_HALTED) {
2324  LOG_TARGET_ERROR(target, "not halted");
2325  return ERROR_TARGET_NOT_HALTED;
2326  }
2327 
2328  if (!count)
2329  return ERROR_OK;
2330 
2331  /* Clear any abort. */
2332  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2334  if (retval != ERROR_OK)
2335  return retval;
2336 
2337  /* Read DSCR. */
2338  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2339  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2340  if (retval != ERROR_OK)
2341  return retval;
2342 
2343  /* Switch to non-blocking mode if not already in that mode. */
2345  if (retval != ERROR_OK)
2346  return retval;
2347 
2348  /* Mark R0 as dirty. */
2349  arm_reg_current(arm, 0)->dirty = true;
2350 
2351  /* Read DFAR and DFSR, as they will be modified in the event of a fault. */
2352  retval = cortex_a_read_dfar_dfsr(target, &orig_dfar, &orig_dfsr, &dscr);
2353  if (retval != ERROR_OK)
2354  return retval;
2355 
2356  /* Get the memory address into R0. */
2357  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2358  armv7a->debug_base + CPUDBG_DTRRX, address);
2359  if (retval != ERROR_OK)
2360  return retval;
2361  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), &dscr);
2362  if (retval != ERROR_OK)
2363  return retval;
2364 
2365  if (size == 4 && (address % 4) == 0) {
2366  /* We are doing a word-aligned transfer, so use fast mode. */
2368  } else {
2369  /* Use slow path. Adjust size for aligned accesses */
2370  switch (address % 4) {
2371  case 1:
2372  case 3:
2373  count *= size;
2374  size = 1;
2375  break;
2376  case 2:
2377  if (size == 4) {
2378  count *= 2;
2379  size = 2;
2380  }
2381  break;
2382  case 0:
2383  default:
2384  break;
2385  }
2387  }
2388 
2389  final_retval = retval;
2390 
2391  /* Switch to non-blocking mode if not already in that mode. */
2393  if (final_retval == ERROR_OK)
2394  final_retval = retval;
2395 
2396  /* Wait for last issued instruction to complete. */
2397  retval = cortex_a_wait_instrcmpl(target, &dscr, true);
2398  if (final_retval == ERROR_OK)
2399  final_retval = retval;
2400 
2401  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture manual
2402  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2403  * check RXfull_l). Most of the time this will be free because RXfull_l
2404  * will be cleared immediately and cached in dscr. However, don't do this
2405  * if there is fault, because then the instruction might not have completed
2406  * successfully. */
2407  if (!(dscr & DSCR_STICKY_ABORT_PRECISE)) {
2409  if (retval != ERROR_OK)
2410  return retval;
2411  }
2412 
2413  /* If there were any sticky abort flags, clear them. */
2415  fault_dscr = dscr;
2419  } else {
2420  fault_dscr = 0;
2421  }
2422 
2423  /* Handle synchronous data faults. */
2424  if (fault_dscr & DSCR_STICKY_ABORT_PRECISE) {
2425  if (final_retval == ERROR_OK) {
2426  /* Final return value will reflect cause of fault. */
2427  retval = cortex_a_read_dfar_dfsr(target, &fault_dfar, &fault_dfsr, &dscr);
2428  if (retval == ERROR_OK) {
2429  LOG_ERROR("data abort at 0x%08" PRIx32 ", dfsr = 0x%08" PRIx32, fault_dfar, fault_dfsr);
2430  final_retval = cortex_a_dfsr_to_error_code(fault_dfsr);
2431  } else
2432  final_retval = retval;
2433  }
2434  /* Fault destroyed DFAR/DFSR; restore them. */
2435  retval = cortex_a_write_dfar_dfsr(target, orig_dfar, orig_dfsr, &dscr);
2436  if (retval != ERROR_OK)
2437  LOG_ERROR("error restoring dfar/dfsr - dscr = 0x%08" PRIx32, dscr);
2438  }
2439 
2440  /* Handle asynchronous data faults. */
2441  if (fault_dscr & DSCR_STICKY_ABORT_IMPRECISE) {
2442  if (final_retval == ERROR_OK)
2443  /* No other error has been recorded so far, so keep this one. */
2444  final_retval = ERROR_TARGET_DATA_ABORT;
2445  }
2446 
2447  /* If the DCC is nonempty, clear it. */
2448  if (dscr & DSCR_DTRTX_FULL_LATCHED) {
2449  uint32_t dummy;
2450  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2451  armv7a->debug_base + CPUDBG_DTRTX, &dummy);
2452  if (final_retval == ERROR_OK)
2453  final_retval = retval;
2454  }
2455  if (dscr & DSCR_DTRRX_FULL_LATCHED) {
2456  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), &dscr);
2457  if (final_retval == ERROR_OK)
2458  final_retval = retval;
2459  }
2460 
2461  /* Done. */
2462  return final_retval;
2463 }
2464 
2466  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t *dscr)
2467 {
2468  /* Reads count objects of size size into *buffer. Old value of DSCR must be
2469  * in *dscr; updated to new value. This is slow because it works for
2470  * non-word-sized objects. Avoid unaligned accesses as they do not work
2471  * on memory address space without "Normal" attribute. If size == 4 and
2472  * the address is aligned, cortex_a_read_cpu_memory_fast should be
2473  * preferred.
2474  * Preconditions:
2475  * - Address is in R0.
2476  * - R0 is marked dirty.
2477  */
2478  struct armv7a_common *armv7a = target_to_armv7a(target);
2479  struct arm *arm = &armv7a->arm;
2480  int retval;
2481 
2482  /* Mark register R1 as dirty, to use for transferring data. */
2483  arm_reg_current(arm, 1)->dirty = true;
2484 
2485  /* Switch to non-blocking mode if not already in that mode. */
2487  if (retval != ERROR_OK)
2488  return retval;
2489 
2490  /* Go through the objects. */
2491  while (count) {
2492  /* Issue a load of the appropriate size to R1. */
2493  uint32_t opcode, data;
2494  if (size == 1)
2495  opcode = ARMV4_5_LDRB_IP(1, 0);
2496  else if (size == 2)
2497  opcode = ARMV4_5_LDRH_IP(1, 0);
2498  else
2499  opcode = ARMV4_5_LDRW_IP(1, 0);
2500  retval = cortex_a_exec_opcode(target, opcode, dscr);
2501  if (retval != ERROR_OK)
2502  return retval;
2503 
2504  /* Issue a write of R1 to DTRTX. */
2505  retval = cortex_a_exec_opcode(target, ARMV4_5_MCR(14, 0, 1, 0, 5, 0), dscr);
2506  if (retval != ERROR_OK)
2507  return retval;
2508 
2509  /* Check for faults and return early. */
2511  return ERROR_OK; /* A data fault is not considered a system failure. */
2512 
2513  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture
2514  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2515  * must also check TXfull_l). Most of the time this will be free
2516  * because TXfull_l will be set immediately and cached in dscr. */
2518  DSCR_DTRTX_FULL_LATCHED, dscr);
2519  if (retval != ERROR_OK)
2520  return retval;
2521 
2522  /* Read the value transferred to DTRTX into the buffer. */
2523  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2524  armv7a->debug_base + CPUDBG_DTRTX, &data);
2525  if (retval != ERROR_OK)
2526  return retval;
2527  if (size == 1)
2528  *buffer = (uint8_t) data;
2529  else if (size == 2)
2530  target_buffer_set_u16(target, buffer, (uint16_t) data);
2531  else
2533 
2534  /* Advance. */
2535  buffer += size;
2536  --count;
2537  }
2538 
2539  return ERROR_OK;
2540 }
2541 
2543  uint32_t count, uint8_t *buffer, uint32_t *dscr)
2544 {
2545  /* Reads count objects of size 4 into *buffer. Old value of DSCR must be in
2546  * *dscr; updated to new value. This is fast but only works for word-sized
2547  * objects at aligned addresses.
2548  * Preconditions:
2549  * - Address is in R0 and must be a multiple of 4.
2550  * - R0 is marked dirty.
2551  */
2552  struct armv7a_common *armv7a = target_to_armv7a(target);
2553  uint32_t u32;
2554  int retval;
2555 
2556  /* Switch to non-blocking mode if not already in that mode. */
2558  if (retval != ERROR_OK)
2559  return retval;
2560 
2561  /* Issue the LDC instruction via a write to ITR. */
2562  retval = cortex_a_exec_opcode(target, ARMV4_5_LDC(0, 1, 0, 1, 14, 5, 0, 4), dscr);
2563  if (retval != ERROR_OK)
2564  return retval;
2565 
2566  count--;
2567 
2568  if (count > 0) {
2569  /* Switch to fast mode if not already in that mode. */
2571  if (retval != ERROR_OK)
2572  return retval;
2573 
2574  /* Latch LDC instruction. */
2575  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2576  armv7a->debug_base + CPUDBG_ITR, ARMV4_5_LDC(0, 1, 0, 1, 14, 5, 0, 4));
2577  if (retval != ERROR_OK)
2578  return retval;
2579 
2580  /* Read the value transferred to DTRTX into the buffer. Due to fast
2581  * mode rules, this blocks until the instruction finishes executing and
2582  * then reissues the read instruction to read the next word from
2583  * memory. The last read of DTRTX in this call reads the second-to-last
2584  * word from memory and issues the read instruction for the last word.
2585  */
2586  retval = mem_ap_read_buf_noincr(armv7a->debug_ap, buffer,
2587  4, count, armv7a->debug_base + CPUDBG_DTRTX);
2588  if (retval != ERROR_OK)
2589  return retval;
2590 
2591  /* Advance. */
2592  buffer += count * 4;
2593  }
2594 
2595  /* Wait for last issued instruction to complete. */
2596  retval = cortex_a_wait_instrcmpl(target, dscr, false);
2597  if (retval != ERROR_OK)
2598  return retval;
2599 
2600  /* Switch to non-blocking mode if not already in that mode. */
2602  if (retval != ERROR_OK)
2603  return retval;
2604 
2605  /* Check for faults and return early. */
2607  return ERROR_OK; /* A data fault is not considered a system failure. */
2608 
2609  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture manual
2610  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2611  * check TXfull_l). Most of the time this will be free because TXfull_l
2612  * will be set immediately and cached in dscr. */
2614  DSCR_DTRTX_FULL_LATCHED, dscr);
2615  if (retval != ERROR_OK)
2616  return retval;
2617 
2618  /* Read the value transferred to DTRTX into the buffer. This is the last
2619  * word. */
2620  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2621  armv7a->debug_base + CPUDBG_DTRTX, &u32);
2622  if (retval != ERROR_OK)
2623  return retval;
2625 
2626  return ERROR_OK;
2627 }
2628 
2630  uint32_t address, uint32_t size,
2631  uint32_t count, uint8_t *buffer)
2632 {
2633  /* Read memory through the CPU. */
2634  int retval, final_retval;
2635  struct armv7a_common *armv7a = target_to_armv7a(target);
2636  struct arm *arm = &armv7a->arm;
2637  uint32_t dscr, orig_dfar, orig_dfsr, fault_dscr, fault_dfar, fault_dfsr;
2638 
2639  LOG_DEBUG("Reading CPU memory address 0x%" PRIx32 " size %" PRIu32 " count %" PRIu32,
2640  address, size, count);
2641  if (target->state != TARGET_HALTED) {
2642  LOG_TARGET_ERROR(target, "not halted");
2643  return ERROR_TARGET_NOT_HALTED;
2644  }
2645 
2646  if (!count)
2647  return ERROR_OK;
2648 
2649  /* Clear any abort. */
2650  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2652  if (retval != ERROR_OK)
2653  return retval;
2654 
2655  /* Read DSCR */
2656  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2657  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2658  if (retval != ERROR_OK)
2659  return retval;
2660 
2661  /* Switch to non-blocking mode if not already in that mode. */
2663  if (retval != ERROR_OK)
2664  return retval;
2665 
2666  /* Mark R0 as dirty. */
2667  arm_reg_current(arm, 0)->dirty = true;
2668 
2669  /* Read DFAR and DFSR, as they will be modified in the event of a fault. */
2670  retval = cortex_a_read_dfar_dfsr(target, &orig_dfar, &orig_dfsr, &dscr);
2671  if (retval != ERROR_OK)
2672  return retval;
2673 
2674  /* Get the memory address into R0. */
2675  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2676  armv7a->debug_base + CPUDBG_DTRRX, address);
2677  if (retval != ERROR_OK)
2678  return retval;
2679  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), &dscr);
2680  if (retval != ERROR_OK)
2681  return retval;
2682 
2683  if (size == 4 && (address % 4) == 0) {
2684  /* We are doing a word-aligned transfer, so use fast mode. */
2685  retval = cortex_a_read_cpu_memory_fast(target, count, buffer, &dscr);
2686  } else {
2687  /* Use slow path. Adjust size for aligned accesses */
2688  switch (address % 4) {
2689  case 1:
2690  case 3:
2691  count *= size;
2692  size = 1;
2693  break;
2694  case 2:
2695  if (size == 4) {
2696  count *= 2;
2697  size = 2;
2698  }
2699  break;
2700  case 0:
2701  default:
2702  break;
2703  }
2705  }
2706 
2707  final_retval = retval;
2708 
2709  /* Switch to non-blocking mode if not already in that mode. */
2711  if (final_retval == ERROR_OK)
2712  final_retval = retval;
2713 
2714  /* Wait for last issued instruction to complete. */
2715  retval = cortex_a_wait_instrcmpl(target, &dscr, true);
2716  if (final_retval == ERROR_OK)
2717  final_retval = retval;
2718 
2719  /* If there were any sticky abort flags, clear them. */
2721  fault_dscr = dscr;
2725  } else {
2726  fault_dscr = 0;
2727  }
2728 
2729  /* Handle synchronous data faults. */
2730  if (fault_dscr & DSCR_STICKY_ABORT_PRECISE) {
2731  if (final_retval == ERROR_OK) {
2732  /* Final return value will reflect cause of fault. */
2733  retval = cortex_a_read_dfar_dfsr(target, &fault_dfar, &fault_dfsr, &dscr);
2734  if (retval == ERROR_OK) {
2735  LOG_ERROR("data abort at 0x%08" PRIx32 ", dfsr = 0x%08" PRIx32, fault_dfar, fault_dfsr);
2736  final_retval = cortex_a_dfsr_to_error_code(fault_dfsr);
2737  } else
2738  final_retval = retval;
2739  }
2740  /* Fault destroyed DFAR/DFSR; restore them. */
2741  retval = cortex_a_write_dfar_dfsr(target, orig_dfar, orig_dfsr, &dscr);
2742  if (retval != ERROR_OK)
2743  LOG_ERROR("error restoring dfar/dfsr - dscr = 0x%08" PRIx32, dscr);
2744  }
2745 
2746  /* Handle asynchronous data faults. */
2747  if (fault_dscr & DSCR_STICKY_ABORT_IMPRECISE) {
2748  if (final_retval == ERROR_OK)
2749  /* No other error has been recorded so far, so keep this one. */
2750  final_retval = ERROR_TARGET_DATA_ABORT;
2751  }
2752 
2753  /* If the DCC is nonempty, clear it. */
2754  if (dscr & DSCR_DTRTX_FULL_LATCHED) {
2755  uint32_t dummy;
2756  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2757  armv7a->debug_base + CPUDBG_DTRTX, &dummy);
2758  if (final_retval == ERROR_OK)
2759  final_retval = retval;
2760  }
2761  if (dscr & DSCR_DTRRX_FULL_LATCHED) {
2762  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), &dscr);
2763  if (final_retval == ERROR_OK)
2764  final_retval = retval;
2765  }
2766 
2767  /* Done. */
2768  return final_retval;
2769 }
2770 
2771 
2772 /*
2773  * Cortex-A Memory access
2774  *
2775  * This is same Cortex-M3 but we must also use the correct
2776  * ap number for every access.
2777  */
2778 
2780  target_addr_t address, uint32_t size,
2781  uint32_t count, uint8_t *buffer)
2782 {
2783  int retval;
2784 
2785  if (!count || !buffer)
2787 
2788  LOG_DEBUG("Reading memory at real address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2789  address, size, count);
2790 
2791  /* read memory through the CPU */
2795 
2796  return retval;
2797 }
2798 
2800  uint32_t size, uint32_t count, uint8_t *buffer)
2801 {
2802  int retval;
2803 
2804  /* cortex_a handles unaligned memory access */
2805  LOG_DEBUG("Reading memory at address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2806  address, size, count);
2807 
2811 
2812  return retval;
2813 }
2814 
2816  target_addr_t address, uint32_t size,
2817  uint32_t count, const uint8_t *buffer)
2818 {
2819  int retval;
2820 
2821  if (!count || !buffer)
2823 
2824  LOG_DEBUG("Writing memory to real address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2825  address, size, count);
2826 
2827  /* write memory through the CPU */
2831 
2832  return retval;
2833 }
2834 
2836  uint32_t size, uint32_t count, const uint8_t *buffer)
2837 {
2838  int retval;
2839 
2840  /* cortex_a handles unaligned memory access */
2841  LOG_DEBUG("Writing memory at address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2842  address, size, count);
2843 
2847  return retval;
2848 }
2849 
2851  uint32_t count, uint8_t *buffer)
2852 {
2853  uint32_t size;
2854 
2855  /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2856  * will have something to do with the size we leave to it. */
2857  for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2858  if (address & size) {
2859  int retval = target_read_memory(target, address, size, 1, buffer);
2860  if (retval != ERROR_OK)
2861  return retval;
2862  address += size;
2863  count -= size;
2864  buffer += size;
2865  }
2866  }
2867 
2868  /* Read the data with as large access size as possible. */
2869  for (; size > 0; size /= 2) {
2870  uint32_t aligned = count - count % size;
2871  if (aligned > 0) {
2872  int retval = target_read_memory(target, address, size, aligned / size, buffer);
2873  if (retval != ERROR_OK)
2874  return retval;
2875  address += aligned;
2876  count -= aligned;
2877  buffer += aligned;
2878  }
2879  }
2880 
2881  return ERROR_OK;
2882 }
2883 
2885  uint32_t count, const uint8_t *buffer)
2886 {
2887  uint32_t size;
2888 
2889  /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2890  * will have something to do with the size we leave to it. */
2891  for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2892  if (address & size) {
2893  int retval = target_write_memory(target, address, size, 1, buffer);
2894  if (retval != ERROR_OK)
2895  return retval;
2896  address += size;
2897  count -= size;
2898  buffer += size;
2899  }
2900  }
2901 
2902  /* Write the data with as large access size as possible. */
2903  for (; size > 0; size /= 2) {
2904  uint32_t aligned = count - count % size;
2905  if (aligned > 0) {
2906  int retval = target_write_memory(target, address, size, aligned / size, buffer);
2907  if (retval != ERROR_OK)
2908  return retval;
2909  address += aligned;
2910  count -= aligned;
2911  buffer += aligned;
2912  }
2913  }
2914 
2915  return ERROR_OK;
2916 }
2917 
2919 {
2920  struct target *target = priv;
2921  struct armv7a_common *armv7a = target_to_armv7a(target);
2922  int retval;
2923 
2925  return ERROR_OK;
2926  if (!target->dbg_msg_enabled)
2927  return ERROR_OK;
2928 
2929  if (target->state == TARGET_RUNNING) {
2930  uint32_t request;
2931  uint32_t dscr;
2932  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2933  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2934 
2935  /* check if we have data */
2936  int64_t then = timeval_ms();
2937  while ((dscr & DSCR_DTR_TX_FULL) && (retval == ERROR_OK)) {
2938  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2939  armv7a->debug_base + CPUDBG_DTRTX, &request);
2940  if (retval == ERROR_OK) {
2941  target_request(target, request);
2942  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2943  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2944  }
2945  if (timeval_ms() > then + 1000) {
2946  LOG_ERROR("Timeout waiting for dtr tx full");
2947  return ERROR_FAIL;
2948  }
2949  }
2950  }
2951 
2952  return ERROR_OK;
2953 }
2954 
2955 /*
2956  * Cortex-A target information and configuration
2957  */
2958 
2960 {
2961  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
2962  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
2963  struct adiv5_dap *swjdp = armv7a->arm.dap;
2965 
2966  int i;
2967  int retval = ERROR_OK;
2968  uint32_t didr, cpuid, dbg_osreg, dbg_idpfr1;
2969 
2970  if (!armv7a->debug_ap) {
2971  if (pc->ap_num == DP_APSEL_INVALID) {
2972  /* Search for the APB-AP - it is needed for access to debug registers */
2973  retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv7a->debug_ap);
2974  if (retval != ERROR_OK) {
2975  LOG_ERROR("Could not find APB-AP for debug access");
2976  return retval;
2977  }
2978  } else {
2979  armv7a->debug_ap = dap_get_ap(swjdp, pc->ap_num);
2980  if (!armv7a->debug_ap) {
2981  LOG_ERROR("Cannot get AP");
2982  return ERROR_FAIL;
2983  }
2984  }
2985  }
2986 
2987  retval = mem_ap_init(armv7a->debug_ap);
2988  if (retval != ERROR_OK) {
2989  LOG_ERROR("Could not initialize the APB-AP");
2990  return retval;
2991  }
2992 
2993  armv7a->debug_ap->memaccess_tck = 80;
2994 
2995  if (!target->dbgbase_set) {
2996  LOG_TARGET_DEBUG(target, "dbgbase is not set, trying to detect using the ROM table");
2997  /* Lookup Processor DAP */
2999  &armv7a->debug_base, target->coreid);
3000  if (retval != ERROR_OK) {
3001  LOG_TARGET_ERROR(target, "Can't detect dbgbase from the ROM table; you need to specify it explicitly");
3002  return retval;
3003  }
3004  LOG_DEBUG("Detected core %" PRId32 " dbgbase: " TARGET_ADDR_FMT,
3005  target->coreid, armv7a->debug_base);
3006  } else
3007  armv7a->debug_base = target->dbgbase;
3008 
3009  if ((armv7a->debug_base & (1UL<<31)) == 0)
3011  "Debug base address has bit 31 set to 0. Access to debug registers will likely fail!\n"
3012  "Please fix the target configuration");
3013 
3014  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3015  armv7a->debug_base + CPUDBG_DIDR, &didr);
3016  if (retval != ERROR_OK) {
3017  LOG_DEBUG("Examine %s failed", "DIDR");
3018  return retval;
3019  }
3020 
3021  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3022  armv7a->debug_base + CPUDBG_CPUID, &cpuid);
3023  if (retval != ERROR_OK) {
3024  LOG_DEBUG("Examine %s failed", "CPUID");
3025  return retval;
3026  }
3027 
3028  LOG_DEBUG("didr = 0x%08" PRIx32, didr);
3029  LOG_DEBUG("cpuid = 0x%08" PRIx32, cpuid);
3030 
3031  cortex_a->didr = didr;
3032  cortex_a->cpuid = cpuid;
3033 
3034  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3035  armv7a->debug_base + CPUDBG_PRSR, &dbg_osreg);
3036  if (retval != ERROR_OK)
3037  return retval;
3038  LOG_TARGET_DEBUG(target, "DBGPRSR 0x%" PRIx32, dbg_osreg);
3039 
3040  if ((dbg_osreg & PRSR_POWERUP_STATUS) == 0) {
3041  LOG_TARGET_ERROR(target, "powered down!");
3042  target->state = TARGET_UNKNOWN; /* TARGET_NO_POWER? */
3043  return ERROR_TARGET_INIT_FAILED;
3044  }
3045 
3046  if (dbg_osreg & PRSR_STICKY_RESET_STATUS)
3047  LOG_TARGET_DEBUG(target, "was reset!");
3048 
3049  /* Read DBGOSLSR and check if OSLK is implemented */
3050  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3051  armv7a->debug_base + CPUDBG_OSLSR, &dbg_osreg);
3052  if (retval != ERROR_OK)
3053  return retval;
3054  LOG_TARGET_DEBUG(target, "DBGOSLSR 0x%" PRIx32, dbg_osreg);
3055 
3056  /* check if OS Lock is implemented */
3057  if ((dbg_osreg & OSLSR_OSLM) == OSLSR_OSLM0 || (dbg_osreg & OSLSR_OSLM) == OSLSR_OSLM1) {
3058  /* check if OS Lock is set */
3059  if (dbg_osreg & OSLSR_OSLK) {
3060  LOG_TARGET_DEBUG(target, "OSLock set! Trying to unlock");
3061 
3062  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
3063  armv7a->debug_base + CPUDBG_OSLAR,
3064  0);
3065  if (retval == ERROR_OK)
3066  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3067  armv7a->debug_base + CPUDBG_OSLSR, &dbg_osreg);
3068 
3069  /* if we fail to access the register or cannot reset the OSLK bit, bail out */
3070  if (retval != ERROR_OK || (dbg_osreg & OSLSR_OSLK) != 0) {
3071  LOG_TARGET_ERROR(target, "OSLock sticky, core not powered?");
3072  target->state = TARGET_UNKNOWN; /* TARGET_NO_POWER? */
3073  return ERROR_TARGET_INIT_FAILED;
3074  }
3075  }
3076  }
3077 
3078  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3079  armv7a->debug_base + CPUDBG_ID_PFR1, &dbg_idpfr1);
3080  if (retval != ERROR_OK)
3081  return retval;
3082 
3083  if (dbg_idpfr1 & 0x000000f0) {
3084  LOG_TARGET_DEBUG(target, "has security extensions");
3086  }
3087  if (dbg_idpfr1 & 0x0000f000) {
3088  LOG_TARGET_DEBUG(target, "has virtualization extensions");
3089  /*
3090  * overwrite and simplify the checks.
3091  * virtualization extensions require implementation of security extension
3092  */
3094  }
3095 
3096  /* Avoid recreating the registers cache */
3097  if (!target_was_examined(target)) {
3098  retval = cortex_a_dpm_setup(cortex_a, didr);
3099  if (retval != ERROR_OK)
3100  return retval;
3101  }
3102 
3103  /* Setup Breakpoint Register Pairs */
3104  cortex_a->brp_num = ((didr >> 24) & 0x0F) + 1;
3105  cortex_a->brp_num_context = ((didr >> 20) & 0x0F) + 1;
3106  cortex_a->brp_num_available = cortex_a->brp_num;
3107  free(cortex_a->brp_list);
3108  cortex_a->brp_list = calloc(cortex_a->brp_num, sizeof(struct cortex_a_brp));
3109 /* cortex_a->brb_enabled = ????; */
3110  for (i = 0; i < cortex_a->brp_num; i++) {
3111  cortex_a->brp_list[i].used = false;
3112  if (i < (cortex_a->brp_num-cortex_a->brp_num_context))
3113  cortex_a->brp_list[i].type = BRP_NORMAL;
3114  else
3115  cortex_a->brp_list[i].type = BRP_CONTEXT;
3116  cortex_a->brp_list[i].value = 0;
3117  cortex_a->brp_list[i].control = 0;
3118  cortex_a->brp_list[i].brpn = i;
3119  }
3120 
3121  LOG_DEBUG("Configured %i hw breakpoints", cortex_a->brp_num);
3122 
3123  /* Setup Watchpoint Register Pairs */
3124  cortex_a->wrp_num = ((didr >> 28) & 0x0F) + 1;
3125  cortex_a->wrp_num_available = cortex_a->wrp_num;
3126  free(cortex_a->wrp_list);
3127  cortex_a->wrp_list = calloc(cortex_a->wrp_num, sizeof(struct cortex_a_wrp));
3128  for (i = 0; i < cortex_a->wrp_num; i++) {
3129  cortex_a->wrp_list[i].used = false;
3130  cortex_a->wrp_list[i].value = 0;
3131  cortex_a->wrp_list[i].control = 0;
3132  cortex_a->wrp_list[i].wrpn = i;
3133  }
3134 
3135  LOG_DEBUG("Configured %i hw watchpoints", cortex_a->wrp_num);
3136 
3137  /* select debug_ap as default */
3138  swjdp->apsel = armv7a->debug_ap->ap_num;
3139 
3141  return ERROR_OK;
3142 }
3143 
3144 static int cortex_a_examine(struct target *target)
3145 {
3146  int retval = ERROR_OK;
3147 
3148  /* Reestablish communication after target reset */
3149  retval = cortex_a_examine_first(target);
3150 
3151  /* Configure core debug access */
3152  if (retval == ERROR_OK)
3154 
3155  return retval;
3156 }
3157 
3158 /*
3159  * Cortex-A target creation and initialization
3160  */
3161 
3162 static int cortex_a_init_target(struct command_context *cmd_ctx,
3163  struct target *target)
3164 {
3165  /* examine_first() does a bunch of this */
3167  return ERROR_OK;
3168 }
3169 
3171  struct cortex_a_common *cortex_a, struct adiv5_dap *dap)
3172 {
3173  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
3174 
3175  /* Setup struct cortex_a_common */
3176  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3177  armv7a->arm.dap = dap;
3178 
3179  /* register arch-specific functions */
3180  armv7a->examine_debug_reason = NULL;
3181 
3183 
3184  armv7a->pre_restore_context = NULL;
3185 
3187 
3188 
3189 /* arm7_9->handle_target_request = cortex_a_handle_target_request; */
3190 
3191  /* REVISIT v7a setup should be in a v7a-specific routine */
3192  armv7a_init_arch_info(target, armv7a);
3195 
3196  return ERROR_OK;
3197 }
3198 
3200 {
3201  struct cortex_a_common *cortex_a;
3202  struct adiv5_private_config *pc;
3203 
3204  if (!target->private_config)
3205  return ERROR_FAIL;
3206 
3207  pc = (struct adiv5_private_config *)target->private_config;
3208 
3209  cortex_a = calloc(1, sizeof(struct cortex_a_common));
3210  if (!cortex_a) {
3211  LOG_ERROR("Out of memory");
3212  return ERROR_FAIL;
3213  }
3214  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3215  cortex_a->armv7a_common.is_armv7r = false;
3217 
3218  return cortex_a_init_arch_info(target, cortex_a, pc->dap);
3219 }
3220 
3222 {
3223  struct cortex_a_common *cortex_a;
3224  struct adiv5_private_config *pc;
3225 
3226  pc = (struct adiv5_private_config *)target->private_config;
3227  if (adiv5_verify_config(pc) != ERROR_OK)
3228  return ERROR_FAIL;
3229 
3230  cortex_a = calloc(1, sizeof(struct cortex_a_common));
3231  if (!cortex_a) {
3232  LOG_ERROR("Out of memory");
3233  return ERROR_FAIL;
3234  }
3235  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3236  cortex_a->armv7a_common.is_armv7r = true;
3237 
3238  return cortex_a_init_arch_info(target, cortex_a, pc->dap);
3239 }
3240 
3242 {
3243  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3244  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
3245  struct arm_dpm *dpm = &armv7a->dpm;
3246  uint32_t dscr;
3247  int retval;
3248 
3249  if (target_was_examined(target)) {
3250  /* Disable halt for breakpoint, watchpoint and vector catch */
3251  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3252  armv7a->debug_base + CPUDBG_DSCR, &dscr);
3253  if (retval == ERROR_OK)
3255  armv7a->debug_base + CPUDBG_DSCR,
3257  }
3258 
3259  if (armv7a->debug_ap)
3260  dap_put_ap(armv7a->debug_ap);
3261 
3262  free(cortex_a->wrp_list);
3263  free(cortex_a->brp_list);
3264  arm_free_reg_cache(dpm->arm);
3265  free(dpm->dbp);
3266  free(dpm->dwp);
3267  free(target->private_config);
3268  free(cortex_a);
3269 }
3270 
3271 static int cortex_a_mmu(struct target *target, bool *enabled)
3272 {
3273  struct armv7a_common *armv7a = target_to_armv7a(target);
3274 
3275  if (target->state != TARGET_HALTED) {
3276  LOG_TARGET_ERROR(target, "not halted");
3277  return ERROR_TARGET_NOT_HALTED;
3278  }
3279 
3280  if (armv7a->is_armv7r)
3281  *enabled = false;
3282  else
3284 
3285  return ERROR_OK;
3286 }
3287 
3288 static int cortex_a_virt2phys(struct target *target,
3289  target_addr_t virt, target_addr_t *phys)
3290 {
3291  int retval;
3292  bool mmu_enabled = false;
3293 
3294  /*
3295  * If the MMU was not enabled at debug entry, there is no
3296  * way of knowing if there was ever a valid configuration
3297  * for it and thus it's not safe to enable it. In this case,
3298  * just return the virtual address as physical.
3299  */
3300  cortex_a_mmu(target, &mmu_enabled);
3301  if (!mmu_enabled) {
3302  *phys = virt;
3303  return ERROR_OK;
3304  }
3305 
3306  /* mmu must be enable in order to get a correct translation */
3307  retval = cortex_a_mmu_modify(target, true);
3308  if (retval != ERROR_OK)
3309  return retval;
3310  return armv7a_mmu_translate_va_pa(target, (uint32_t)virt,
3311  phys, 1);
3312 }
3313 
3314 COMMAND_HANDLER(cortex_a_handle_cache_info_command)
3315 {
3317  struct armv7a_common *armv7a = target_to_armv7a(target);
3318 
3320  &armv7a->armv7a_mmu.armv7a_cache);
3321 }
3322 
3323 
3324 COMMAND_HANDLER(cortex_a_handle_dbginit_command)
3325 {
3327  if (!target_was_examined(target)) {
3328  LOG_ERROR("target not examined yet");
3329  return ERROR_FAIL;
3330  }
3331 
3333 }
3334 
3335 COMMAND_HANDLER(handle_cortex_a_mask_interrupts_command)
3336 {
3338  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3339 
3340  static const struct nvp nvp_maskisr_modes[] = {
3341  { .name = "off", .value = CORTEX_A_ISRMASK_OFF },
3342  { .name = "on", .value = CORTEX_A_ISRMASK_ON },
3343  { .name = NULL, .value = -1 },
3344  };
3345  const struct nvp *n;
3346 
3347  if (CMD_ARGC > 0) {
3348  n = nvp_name2value(nvp_maskisr_modes, CMD_ARGV[0]);
3349  if (!n->name) {
3350  LOG_ERROR("Unknown parameter: %s - should be off or on", CMD_ARGV[0]);
3352  }
3353 
3354  cortex_a->isrmasking_mode = n->value;
3355  }
3356 
3357  n = nvp_value2name(nvp_maskisr_modes, cortex_a->isrmasking_mode);
3358  command_print(CMD, "cortex_a interrupt mask %s", n->name);
3359 
3360  return ERROR_OK;
3361 }
3362 
3363 COMMAND_HANDLER(handle_cortex_a_dacrfixup_command)
3364 {
3366  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3367 
3368  static const struct nvp nvp_dacrfixup_modes[] = {
3369  { .name = "off", .value = CORTEX_A_DACRFIXUP_OFF },
3370  { .name = "on", .value = CORTEX_A_DACRFIXUP_ON },
3371  { .name = NULL, .value = -1 },
3372  };
3373  const struct nvp *n;
3374 
3375  if (CMD_ARGC > 0) {
3376  n = nvp_name2value(nvp_dacrfixup_modes, CMD_ARGV[0]);
3377  if (!n->name)
3379  cortex_a->dacrfixup_mode = n->value;
3380 
3381  }
3382 
3383  n = nvp_value2name(nvp_dacrfixup_modes, cortex_a->dacrfixup_mode);
3384  command_print(CMD, "cortex_a domain access control fixup %s", n->name);
3385 
3386  return ERROR_OK;
3387 }
3388 
3389 static const struct command_registration cortex_a_exec_command_handlers[] = {
3390  {
3391  .name = "cache_info",
3392  .handler = cortex_a_handle_cache_info_command,
3393  .mode = COMMAND_EXEC,
3394  .help = "display information about target caches",
3395  .usage = "",
3396  },
3397  {
3398  .name = "dbginit",
3399  .handler = cortex_a_handle_dbginit_command,
3400  .mode = COMMAND_EXEC,
3401  .help = "Initialize core debug",
3402  .usage = "",
3403  },
3404  {
3405  .name = "maskisr",
3406  .handler = handle_cortex_a_mask_interrupts_command,
3407  .mode = COMMAND_ANY,
3408  .help = "mask cortex_a interrupts",
3409  .usage = "['on'|'off']",
3410  },
3411  {
3412  .name = "dacrfixup",
3413  .handler = handle_cortex_a_dacrfixup_command,
3414  .mode = COMMAND_ANY,
3415  .help = "set domain access control (DACR) to all-manager "
3416  "on memory access",
3417  .usage = "['on'|'off']",
3418  },
3419  {
3420  .chain = armv7a_mmu_command_handlers,
3421  },
3422  {
3424  },
3425 
3427 };
3428 static const struct command_registration cortex_a_command_handlers[] = {
3429  {
3431  },
3432  {
3434  },
3435  {
3436  .name = "cortex_a",
3437  .mode = COMMAND_ANY,
3438  .help = "Cortex-A command group",
3439  .usage = "",
3441  },
3443 };
3444 
3445 struct target_type cortexa_target = {
3446  .name = "cortex_a",
3447 
3448  .poll = cortex_a_poll,
3449  .arch_state = armv7a_arch_state,
3450 
3451  .halt = cortex_a_halt,
3452  .resume = cortex_a_resume,
3453  .step = cortex_a_step,
3454 
3455  .assert_reset = cortex_a_assert_reset,
3456  .deassert_reset = cortex_a_deassert_reset,
3457 
3458  /* REVISIT allow exporting VFP3 registers ... */
3459  .get_gdb_arch = arm_get_gdb_arch,
3460  .get_gdb_reg_list = arm_get_gdb_reg_list,
3461 
3462  .read_memory = cortex_a_read_memory,
3463  .write_memory = cortex_a_write_memory,
3464 
3465  .read_buffer = cortex_a_read_buffer,
3466  .write_buffer = cortex_a_write_buffer,
3467 
3468  .checksum_memory = arm_checksum_memory,
3469  .blank_check_memory = arm_blank_check_memory,
3470 
3471  .run_algorithm = armv4_5_run_algorithm,
3472 
3473  .add_breakpoint = cortex_a_add_breakpoint,
3474  .add_context_breakpoint = cortex_a_add_context_breakpoint,
3475  .add_hybrid_breakpoint = cortex_a_add_hybrid_breakpoint,
3476  .remove_breakpoint = cortex_a_remove_breakpoint,
3477  .add_watchpoint = cortex_a_add_watchpoint,
3478  .remove_watchpoint = cortex_a_remove_watchpoint,
3479 
3480  .commands = cortex_a_command_handlers,
3481  .target_create = cortex_a_target_create,
3482  .target_jim_configure = adiv5_jim_configure,
3483  .init_target = cortex_a_init_target,
3484  .examine = cortex_a_examine,
3485  .deinit_target = cortex_a_deinit_target,
3486 
3487  .read_phys_memory = cortex_a_read_phys_memory,
3488  .write_phys_memory = cortex_a_write_phys_memory,
3489  .mmu = cortex_a_mmu,
3490  .virt2phys = cortex_a_virt2phys,
3491 };
3492 
3493 static const struct command_registration cortex_r4_exec_command_handlers[] = {
3494  {
3495  .name = "dbginit",
3496  .handler = cortex_a_handle_dbginit_command,
3497  .mode = COMMAND_EXEC,
3498  .help = "Initialize core debug",
3499  .usage = "",
3500  },
3501  {
3502  .name = "maskisr",
3503  .handler = handle_cortex_a_mask_interrupts_command,
3504  .mode = COMMAND_EXEC,
3505  .help = "mask cortex_r4 interrupts",
3506  .usage = "['on'|'off']",
3507  },
3508 
3510 };
3511 static const struct command_registration cortex_r4_command_handlers[] = {
3512  {
3514  },
3515  {
3516  .name = "cortex_r4",
3517  .mode = COMMAND_ANY,
3518  .help = "Cortex-R4 command group",
3519  .usage = "",
3521  },
3523 };
3524 
3525 struct target_type cortexr4_target = {
3526  .name = "cortex_r4",
3527 
3528  .poll = cortex_a_poll,
3529  .arch_state = armv7a_arch_state,
3530 
3531  .halt = cortex_a_halt,
3532  .resume = cortex_a_resume,
3533  .step = cortex_a_step,
3534 
3535  .assert_reset = cortex_a_assert_reset,
3536  .deassert_reset = cortex_a_deassert_reset,
3537 
3538  /* REVISIT allow exporting VFP3 registers ... */
3539  .get_gdb_arch = arm_get_gdb_arch,
3540  .get_gdb_reg_list = arm_get_gdb_reg_list,
3541 
3542  .read_memory = cortex_a_read_phys_memory,
3543  .write_memory = cortex_a_write_phys_memory,
3544 
3545  .checksum_memory = arm_checksum_memory,
3546  .blank_check_memory = arm_blank_check_memory,
3547 
3548  .run_algorithm = armv4_5_run_algorithm,
3549 
3550  .add_breakpoint = cortex_a_add_breakpoint,
3551  .add_context_breakpoint = cortex_a_add_context_breakpoint,
3552  .add_hybrid_breakpoint = cortex_a_add_hybrid_breakpoint,
3553  .remove_breakpoint = cortex_a_remove_breakpoint,
3554  .add_watchpoint = cortex_a_add_watchpoint,
3555  .remove_watchpoint = cortex_a_remove_watchpoint,
3556 
3557  .commands = cortex_r4_command_handlers,
3558  .target_create = cortex_r4_target_create,
3559  .target_jim_configure = adiv5_jim_configure,
3560  .init_target = cortex_a_init_target,
3561  .examine = cortex_a_examine,
3562  .deinit_target = cortex_a_deinit_target,
3563 };
#define BRP_CONTEXT
Definition: aarch64.h:23
#define CPUDBG_CPUID
Definition: aarch64.h:14
#define BRP_NORMAL
Definition: aarch64.h:22
#define CPUDBG_LOCKACCESS
Definition: aarch64.h:19
#define IS_ALIGNED(x, a)
Definition: align.h:22
int arm_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Runs ARM code in the target to check whether a memory block holds all ones.
Definition: armv4_5.c:1687
struct reg * arm_reg_current(struct arm *arm, unsigned int regnum)
Returns handle to the register currently mapped to a given number.
Definition: armv4_5.c:517
@ ARM_VFP_V3
Definition: arm.h:164
int arm_checksum_memory(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Runs ARM code in the target to calculate a CRC32 checksum.
Definition: armv4_5.c:1614
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1283
int arm_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Definition: armv4_5.c:1288
@ ARM_MODE_ANY
Definition: arm.h:106
@ ARM_MODE_SVC
Definition: arm.h:86
void arm_free_reg_cache(struct arm *arm)
Definition: armv4_5.c:777
@ ARM_STATE_JAZELLE
Definition: arm.h:154
@ ARM_STATE_THUMB
Definition: arm.h:153
@ ARM_STATE_ARM
Definition: arm.h:152
@ ARM_STATE_AARCH64
Definition: arm.h:156
@ ARM_STATE_THUMB_EE
Definition: arm.h:155
const struct command_registration arm_command_handlers[]
Definition: armv4_5.c:1263
int armv4_5_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)
Definition: armv4_5.c:1588
@ ARM_CORE_TYPE_SEC_EXT
Definition: arm.h:47
@ ARM_CORE_TYPE_VIRT_EXT
Definition: arm.h:48
int dap_lookup_cs_component(struct adiv5_ap *ap, uint8_t type, target_addr_t *addr, int32_t core_id)
Definition: arm_adi_v5.c:2295
int mem_ap_read_buf_noincr(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:742
int adiv5_verify_config(struct adiv5_private_config *pc)
Definition: arm_adi_v5.c:2494
int mem_ap_write_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Asynchronous (queued) write of a word to memory or a system register.
Definition: arm_adi_v5.c:297
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2489
int dap_find_get_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
Definition: arm_adi_v5.c:1115
int mem_ap_write_buf_noincr(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:748
int mem_ap_read_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t *value)
Synchronous read of a word from memory or a system register.
Definition: arm_adi_v5.c:274
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1197
int dap_put_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1217
int mem_ap_init(struct adiv5_ap *ap)
Initialize a DAP.
Definition: arm_adi_v5.c:896
int mem_ap_write_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Synchronous write of a word to memory or a system register.
Definition: arm_adi_v5.c:326
@ AP_TYPE_APB_AP
Definition: arm_adi_v5.h:491
#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
#define ARM_CS_C9_DEVTYPE_CORE_DEBUG
Definition: arm_coresight.h:88
void arm_dpm_report_dscr(struct arm_dpm *dpm, uint32_t dscr)
Definition: arm_dpm.c:1054
int arm_dpm_read_current_registers(struct arm_dpm *dpm)
Read basic registers of the current context: R0 to R15, and CPSR; sets the core mode (such as USR or ...
Definition: arm_dpm.c:377
int arm_dpm_modeswitch(struct arm_dpm *dpm, enum arm_mode mode)
Definition: arm_dpm.c:144
int arm_dpm_setup(struct arm_dpm *dpm)
Hooks up this DPM to its associated target; call only once.
Definition: arm_dpm.c:1093
int arm_dpm_read_reg(struct arm_dpm *dpm, struct reg *r, unsigned int regnum)
Definition: arm_dpm.c:206
int arm_dpm_write_dirty_registers(struct arm_dpm *dpm, bool bpwp)
Writes all modified core registers for all processor modes.
Definition: arm_dpm.c:484
void arm_dpm_report_wfar(struct arm_dpm *dpm, uint32_t addr)
Definition: arm_dpm.c:1030
int arm_dpm_initialize(struct arm_dpm *dpm)
Reinitializes DPM state at the beginning of a new debug session or after a reset which may have affec...
Definition: arm_dpm.c:1160
#define OSLSR_OSLM
Definition: arm_dpm.h:248
#define DRCR_HALT
Definition: arm_dpm.h:223
#define DSCR_INSTR_COMP
Definition: arm_dpm.h:190
#define DRCR_CLEAR_EXCEPTIONS
Definition: arm_dpm.h:225
#define DSCR_INT_DIS
Definition: arm_dpm.h:180
#define OSLSR_OSLM0
Definition: arm_dpm.h:244
#define DSCR_STICKY_ABORT_IMPRECISE
Definition: arm_dpm.h:176
#define DSCR_EXT_DCC_FAST_MODE
Definition: arm_dpm.h:216
#define OSLSR_OSLK
Definition: arm_dpm.h:245
#define DSCR_DTR_TX_FULL
Definition: arm_dpm.h:194
#define DSCR_DTRRX_FULL_LATCHED
Definition: arm_dpm.h:193
#define DRCR_RESTART
Definition: arm_dpm.h:224
#define DSCR_RUN_MODE(dscr)
Definition: arm_dpm.h:198
#define DSCR_STICKY_ABORT_PRECISE
Definition: arm_dpm.h:175
#define OSLSR_OSLM1
Definition: arm_dpm.h:247
#define DSCR_CORE_HALTED
Definition: arm_dpm.h:172
#define DSCR_ITR_EN
Definition: arm_dpm.h:182
#define DSCR_EXT_DCC_NON_BLOCKING
Definition: arm_dpm.h:214
#define PRSR_STICKY_RESET_STATUS
Definition: arm_dpm.h:238
#define PRSR_POWERUP_STATUS
Definition: arm_dpm.h:235
#define DSCR_EXT_DCC_MASK
Definition: arm_dpm.h:189
#define DSCR_DTR_RX_FULL
Definition: arm_dpm.h:195
#define DSCR_CORE_RESTARTED
Definition: arm_dpm.h:173
#define DSCR_HALT_DBG_MODE
Definition: arm_dpm.h:183
#define DSCR_DTRTX_FULL_LATCHED
Definition: arm_dpm.h:192
Macros used to generate various ARM or Thumb opcodes.
#define ARMV5_BKPT(im)
Definition: arm_opcodes.h:227
#define ARMV4_5_STC(p, u, d, w, cp, crd, rn, imm)
Definition: arm_opcodes.h:159
#define ARMV5_T_BKPT(im)
Definition: arm_opcodes.h:313
#define ARMV4_5_LDC(p, u, d, w, cp, crd, rn, imm)
Definition: arm_opcodes.h:174
#define ARMV4_5_MRC(cp, op1, rd, crn, crm, op2)
Definition: arm_opcodes.h:186
#define ARMV4_5_STRH_IP(rd, rn)
Definition: arm_opcodes.h:105
#define ARMV4_5_MCR(cp, op1, rd, crn, crm, op2)
Definition: arm_opcodes.h:209
#define ARMV4_5_LDRH_IP(rd, rn)
Definition: arm_opcodes.h:87
#define ARMV4_5_LDRB_IP(rd, rn)
Definition: arm_opcodes.h:93
#define ARMV4_5_LDRW_IP(rd, rn)
Definition: arm_opcodes.h:81
#define ARMV4_5_STRW_IP(rd, rn)
Definition: arm_opcodes.h:99
#define ARMV4_5_STRB_IP(rd, rn)
Definition: arm_opcodes.h:111
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.
enum arm_mode mode
Definition: armv4_5.c:281
int armv7a_handle_cache_info_command(struct command_invocation *cmd, struct armv7a_cache_common *armv7a_cache)
Definition: armv7a.c:183
int armv7a_read_ttbcr(struct target *target)
Definition: armv7a.c:119
int armv7a_arch_state(struct target *target)
Definition: armv7a.c:482
const struct command_registration armv7a_command_handlers[]
Definition: armv7a.c:511
int armv7a_init_arch_info(struct target *target, struct armv7a_common *armv7a)
Definition: armv7a.c:466
int armv7a_identify_cache(struct target *target)
Definition: armv7a.c:315
#define CPUDBG_DSMCR
Definition: armv7a.h:164
#define CPUDBG_DSCCR
Definition: armv7a.h:163
#define CPUDBG_OSLAR
Definition: armv7a.h:157
#define CPUDBG_BCR_BASE
Definition: armv7a.h:151
#define CPUDBG_OSLSR
Definition: armv7a.h:158
#define CPUDBG_DSCR
Definition: armv7a.h:139
#define CPUDBG_DRCR
Definition: armv7a.h:140
#define CPUDBG_DIDR
Definition: armv7a.h:134
#define CPUDBG_WCR_BASE
Definition: armv7a.h:153
#define CPUDBG_DTRTX
Definition: armv7a.h:147
static struct armv7a_common * target_to_armv7a(struct target *target)
Definition: armv7a.h:120
#define CPUDBG_WVR_BASE
Definition: armv7a.h:152
#define CPUDBG_WFAR
Definition: armv7a.h:137
#define CPUDBG_BVR_BASE
Definition: armv7a.h:150
#define CPUDBG_DTRRX
Definition: armv7a.h:145
#define CPUDBG_PRSR
Definition: armv7a.h:142
#define CPUDBG_ITR
Definition: armv7a.h:146
#define CPUDBG_ID_PFR1
Definition: armv7a.h:170
int armv7a_l1_i_cache_inval_virt(struct target *target, uint32_t virt, uint32_t size)
Definition: armv7a_cache.c:329
int armv7a_cache_flush_virt(struct target *target, uint32_t virt, uint32_t size)
Definition: armv7a_cache.c:376
int armv7a_l1_d_cache_inval_virt(struct target *target, uint32_t virt, uint32_t size)
Definition: armv7a_cache.c:146
const struct command_registration armv7a_mmu_command_handlers[]
Definition: armv7a_mmu.c:359
int armv7a_mmu_translate_va_pa(struct target *target, uint32_t va, target_addr_t *val, int meminfo)
Definition: armv7a_mmu.c:27
@ ARMV7M_PRIMASK
Definition: armv7m.h:148
@ ARMV7M_XPSR
Definition: armv7m.h:131
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
struct breakpoint * breakpoint_find(struct target *target, target_addr_t address)
Definition: breakpoints.c:472
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
static void watchpoint_set(struct watchpoint *watchpoint, unsigned int number)
Definition: breakpoints.h:81
static void breakpoint_hw_set(struct breakpoint *breakpoint, unsigned int hw_number)
Definition: breakpoints.h:65
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int cortex_a_dpm_finish(struct arm_dpm *dpm)
Definition: cortex_a.c:398
static int cortex_a_read_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: cortex_a.c:2779
static int cortex_a_dpm_prepare(struct arm_dpm *dpm)
Definition: cortex_a.c:370
static int cortex_a_exec_opcode(struct target *target, uint32_t opcode, uint32_t *dscr_p)
Definition: cortex_a.c:284
static const struct command_registration cortex_a_command_handlers[]
Definition: cortex_a.c:3428
static int cortex_a_write_dcc(struct cortex_a_common *a, uint32_t data)
Definition: cortex_a.c:334
static int cortex_a_write_dfar_dfsr(struct target *target, uint32_t dfar, uint32_t dfsr, uint32_t *dscr)
Definition: cortex_a.c:2163
static int cortex_a_dpm_setup(struct cortex_a_common *a, uint32_t didr)
Definition: cortex_a.c:634
static int cortex_a_write_buffer(struct target *target, target_addr_t address, uint32_t count, const uint8_t *buffer)
Definition: cortex_a.c:2884
static int cortex_a_restore_smp(struct target *target, bool handle_breakpoints)
Definition: cortex_a.c:969
static int cortex_a_read_buffer(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
Definition: cortex_a.c:2850
static int cortex_a_init_debug_access(struct target *target)
Definition: cortex_a.c:209
static int cortex_a_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Remove a watchpoint from an Cortex-A target.
Definition: cortex_a.c:1936
static int cortex_a_instr_cpsr_sync(struct arm_dpm *dpm)
Definition: cortex_a.c:484
static const struct command_registration cortex_r4_exec_command_handlers[]
Definition: cortex_a.c:3493
static const struct command_registration cortex_a_exec_command_handlers[]
Definition: cortex_a.c:3389
static int cortex_a_read_cpu_memory_slow(struct target *target, uint32_t size, uint32_t count, uint8_t *buffer, uint32_t *dscr)
Definition: cortex_a.c:2465
static int cortex_a_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: cortex_a.c:2799
static int cortex_a_read_copro(struct target *target, uint32_t opcode, uint32_t *data, uint32_t *dscr)
Definition: cortex_a.c:2076
static int cortex_a_instr_read_data_r0_r1(struct arm_dpm *dpm, uint32_t opcode, uint64_t *data)
Definition: cortex_a.c:552
static int cortex_a_instr_read_data_dcc(struct arm_dpm *dpm, uint32_t opcode, uint32_t *data)
Definition: cortex_a.c:495
static int cortex_a_restore_context(struct target *target, bool bpwp)
Definition: cortex_a.c:1312
static int cortex_a_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1732
static int cortex_a_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: cortex_a.c:1204
static int cortex_a_handle_target_request(void *priv)
Definition: cortex_a.c:2918
static int cortex_a_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Add a watchpoint to an Cortex-A target.
Definition: cortex_a.c:1911
static int cortex_a_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
Sets a watchpoint for an Cortex-A target in one of the watchpoint units.
Definition: cortex_a.c:1764
static int cortex_a_init_arch_info(struct target *target, struct cortex_a_common *cortex_a, struct adiv5_dap *dap)
Definition: cortex_a.c:3170
static int cortex_a_instr_write_data_r0(struct arm_dpm *dpm, uint32_t opcode, uint32_t data)
Definition: cortex_a.c:442
static int cortex_a_post_debug_entry(struct target *target)
Definition: cortex_a.c:1111
struct target_type cortexr4_target
Definition: cortex_a.c:3525
static int update_halt_gdb(struct target *target)
Definition: cortex_a.c:690
static int cortex_a_read_cpu_memory_fast(struct target *target, uint32_t count, uint8_t *buffer, uint32_t *dscr)
Definition: cortex_a.c:2542
static int cortex_a_set_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1488
static int cortex_r4_target_create(struct target *target)
Definition: cortex_a.c:3221
static int cortex_a_add_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1715
static int cortex_a_examine(struct target *target)
Definition: cortex_a.c:3144
static int cortex_a_write_cpu_memory_slow(struct target *target, uint32_t size, uint32_t count, const uint8_t *buffer, uint32_t *dscr)
Definition: cortex_a.c:2206
static int cortex_a_halt_smp(struct target *target)
Definition: cortex_a.c:676
static int cortex_a_mmu_modify(struct target *target, bool enable)
Definition: cortex_a.c:169
static int cortex_a_add_context_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1699
static int cortex_a_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1569
static int cortex_a_set_dscr_bits(struct target *target, unsigned long bit_mask, unsigned long value)
Definition: cortex_a.c:1158
static int cortex_a_deassert_reset(struct target *target)
Definition: cortex_a.c:1992
static int cortex_a_target_create(struct target *target)
Definition: cortex_a.c:3199
static int cortex_a_write_copro(struct target *target, uint32_t opcode, uint32_t data, uint32_t *dscr)
Definition: cortex_a.c:2130
static int cortex_a_read_dfar_dfsr(struct target *target, uint32_t *dfar, uint32_t *dfsr, uint32_t *dscr)
Definition: cortex_a.c:2110
static int cortex_a_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
Unset an existing watchpoint and clear the used watchpoint unit.
Definition: cortex_a.c:1866
static int cortex_a_set_dcc_mode(struct target *target, uint32_t mode, uint32_t *dscr)
Definition: cortex_a.c:2025
static int cortex_a_prep_memaccess(struct target *target, bool phys_access)
Definition: cortex_a.c:113
static int cortex_a_bpwp_enable(struct arm_dpm *dpm, unsigned int index_t, uint32_t addr, uint32_t control)
Definition: cortex_a.c:575
static int cortex_a_internal_restore(struct target *target, bool current, target_addr_t *address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_a.c:821
static int cortex_a_virt2phys(struct target *target, target_addr_t virt, target_addr_t *phys)
Definition: cortex_a.c:3288
static int cortex_a_examine_first(struct target *target)
Definition: cortex_a.c:2959
static int cortex_a_mmu(struct target *target, bool *enabled)
Definition: cortex_a.c:3271
static int cortex_a_instr_read_data_r0(struct arm_dpm *dpm, uint32_t opcode, uint32_t *data)
Definition: cortex_a.c:533
static int cortex_a_wait_instrcmpl(struct target *target, uint32_t *dscr, bool force)
Definition: cortex_a.c:256
static int cortex_a_init_target(struct command_context *cmd_ctx, struct target *target)
Definition: cortex_a.c:3162
static int cortex_a_poll(struct target *target)
Definition: cortex_a.c:736
static void cortex_a_deinit_target(struct target *target)
Definition: cortex_a.c:3241
static int cortex_a_bpwp_disable(struct arm_dpm *dpm, unsigned int index_t)
Definition: cortex_a.c:610
static int cortex_a_restore_cp15_control_reg(struct target *target)
Definition: cortex_a.c:91
static const struct command_registration cortex_r4_command_handlers[]
Definition: cortex_a.c:3511
static int cortex_a_write_cpu_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: cortex_a.c:2311
COMMAND_HANDLER(cortex_a_handle_cache_info_command)
Definition: cortex_a.c:3314
static int cortex_a_set_breakpoint(struct target *target, struct breakpoint *breakpoint, uint8_t matchmode)
Definition: cortex_a.c:1329
static int cortex_a_halt(struct target *target)
Definition: cortex_a.c:793
static int cortex_a_instr_write_data_dcc(struct arm_dpm *dpm, uint32_t opcode, uint32_t data)
Definition: cortex_a.c:404
static int cortex_a_read_dcc(struct cortex_a_common *a, uint32_t *data, uint32_t *dscr_p)
Definition: cortex_a.c:341
static int cortex_a_write_cpu_memory_fast(struct target *target, uint32_t count, const uint8_t *buffer, uint32_t *dscr)
Definition: cortex_a.c:2282
static int cortex_a_set_context_breakpoint(struct target *target, struct breakpoint *breakpoint, uint8_t matchmode)
Definition: cortex_a.c:1439
static int cortex_a_read_cpu_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: cortex_a.c:2629
static int cortex_a_post_memaccess(struct target *target, bool phys_access)
Definition: cortex_a.c:143
static int cortex_a_internal_restart(struct target *target)
Definition: cortex_a.c:919
static int cortex_a_dfsr_to_error_code(uint32_t dfsr)
Definition: cortex_a.c:2179
static int cortex_a_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1683
static int cortex_a_instr_write_data_r0_r1(struct arm_dpm *dpm, uint32_t opcode, uint64_t data)
Definition: cortex_a.c:462
static int cortex_a_instr_write_data_rt_dcc(struct arm_dpm *dpm, uint8_t rt, uint32_t data)
Definition: cortex_a.c:421
static int cortex_a_debug_entry(struct target *target)
Definition: cortex_a.c:1032
static int cortex_a_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: cortex_a.c:2835
static int cortex_a_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_a.c:996
static int cortex_a_instr_read_data_rt_dcc(struct arm_dpm *dpm, uint8_t rt, uint32_t *data)
Definition: cortex_a.c:513
static int cortex_a_wait_dscr_bits(struct target *target, uint32_t mask, uint32_t value, uint32_t *dscr)
Definition: cortex_a.c:2047
static struct cortex_a_common * dpm_to_a(struct arm_dpm *dpm)
Definition: cortex_a.c:329
static int cortex_a_write_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: cortex_a.c:2815
static int cortex_a_assert_reset(struct target *target)
Definition: cortex_a.c:1952
struct target_type cortexa_target
Definition: cortex_a.c:3445
static struct target * get_cortex_a(struct target *target, int32_t coreid)
Definition: cortex_a.c:663
static unsigned int ilog2(unsigned int x)
Definition: cortex_a.c:79
static struct cortex_a_common * target_to_cortex_a(struct target *target)
Definition: cortex_a.h:107
#define CPUDBG_CPUID_CORTEX_R5
Definition: cortex_a.h:35
@ CORTEX_A_ISRMASK_OFF
Definition: cortex_a.h:48
@ CORTEX_A_ISRMASK_ON
Definition: cortex_a.h:49
@ CORTEX_A_DACRFIXUP_ON
Definition: cortex_a.h:54
@ CORTEX_A_DACRFIXUP_OFF
Definition: cortex_a.h:53
#define CPUDBG_CPUID_MASK
Definition: cortex_a.h:33
#define CPUDBG_CPUID_CORTEX_R4
Definition: cortex_a.h:34
#define CORTEX_A_COMMON_MAGIC
Definition: cortex_a.h:22
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
bool transport_is_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
Definition: jtag/core.c:1840
int adapter_deassert_reset(void)
Definition: jtag/core.c:1912
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1747
int adapter_assert_reset(void)
Definition: jtag/core.c:1892
@ RESET_SRST_NO_GATING
Definition: jtag.h:224
@ RESET_HAS_SRST
Definition: jtag.h:218
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:160
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:163
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:151
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
uint8_t mask
Definition: parport.c:70
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
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
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
Definition: arm_adi_v5.h:261
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
uint64_t apsel
Definition: arm_adi_v5.h:367
struct adiv5_dap * dap
Definition: arm_adi_v5.h:787
This wraps an implementation of DPM primitives.
Definition: arm_dpm.h:47
int(* instr_read_data_dcc)(struct arm_dpm *dpm, uint32_t opcode, uint32_t *data)
Runs one instruction, reading data from dcc after execution.
Definition: arm_dpm.h:91
uint64_t didr
Cache of DIDR.
Definition: arm_dpm.h:51
int(* instr_write_data_r0)(struct arm_dpm *dpm, uint32_t opcode, uint32_t data)
Runs one instruction, writing data to R0 before execution.
Definition: arm_dpm.h:72
struct arm * arm
Definition: arm_dpm.h:48
int(* bpwp_enable)(struct arm_dpm *dpm, unsigned int index_value, uint32_t addr, uint32_t control)
Enables one breakpoint or watchpoint by writing to the hardware registers.
Definition: arm_dpm.h:122
int(* finish)(struct arm_dpm *dpm)
Invoke after a series of instruction operations.
Definition: arm_dpm.h:57
struct dpm_bp * dbp
Definition: arm_dpm.h:139
int(* instr_write_data_dcc)(struct arm_dpm *dpm, uint32_t opcode, uint32_t data)
Runs one instruction, writing data to DCC before execution.
Definition: arm_dpm.h:65
int(* prepare)(struct arm_dpm *dpm)
Invoke before a series of instruction operations.
Definition: arm_dpm.h:54
int(* instr_read_data_r0)(struct arm_dpm *dpm, uint32_t opcode, uint32_t *data)
Runs one instruction, reading data from r0 after execution.
Definition: arm_dpm.h:98
int(* instr_read_data_r0_r1)(struct arm_dpm *dpm, uint32_t opcode, uint64_t *data)
Runs two instructions, reading data from r0 and r1 after execution.
Definition: arm_dpm.h:105
struct dpm_wp * dwp
Definition: arm_dpm.h:140
int(* bpwp_disable)(struct arm_dpm *dpm, unsigned int index_value)
Disables one breakpoint or watchpoint by clearing its hardware control registers.
Definition: arm_dpm.h:130
int(* instr_cpsr_sync)(struct arm_dpm *dpm)
Optional core-specific operation invoked after CPSR writes.
Definition: arm_dpm.h:86
int(* instr_write_data_r0_r1)(struct arm_dpm *dpm, uint32_t opcode, uint64_t data)
Runs two instructions, writing data to R0 and R1 before execution.
Definition: arm_dpm.h:78
uint32_t dscr
Recent value of DSCR.
Definition: arm_dpm.h:150
Represents a generic ARM core, with standard application registers.
Definition: arm.h:176
enum arm_core_type core_type
Indicates what registers are in the ARM state core register set.
Definition: arm.h:194
int(* mrc)(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t *value)
Read coprocessor register.
Definition: arm.h:231
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:197
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
Definition: arm.h:258
struct reg * pc
Handle to the PC; valid in all core modes.
Definition: arm.h:182
struct reg_cache * core_cache
Definition: arm.h:179
int(* mcr)(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t crn, uint32_t crm, uint32_t value)
Write coprocessor register.
Definition: arm.h:242
struct reg * spsr
Handle to the SPSR; valid only in core modes with an SPSR.
Definition: arm.h:188
int arm_vfp_version
Floating point or VFP version, 0 if disabled.
Definition: arm.h:206
struct target * target
Backpointer to the target.
Definition: arm.h:211
enum arm_state core_state
Record the current core state: ARM, Thumb, or otherwise.
Definition: arm.h:200
bool i_cache_enabled
Definition: armv7a.h:66
bool d_u_cache_enabled
Definition: armv7a.h:67
bool is_armv7r
Definition: armv7a.h:103
int(* post_debug_entry)(struct target *target)
Definition: armv7a.h:114
int(* examine_debug_reason)(struct target *target)
Definition: armv7a.h:113
target_addr_t debug_base
Definition: armv7a.h:95
struct arm arm
Definition: armv7a.h:90
struct armv7a_mmu_common armv7a_mmu
Definition: armv7a.h:111
struct arm_dpm dpm
Definition: armv7a.h:94
struct adiv5_ap * debug_ap
Definition: armv7a.h:96
void(* pre_restore_context)(struct target *target)
Definition: armv7a.h:116
struct armv7a_cache_common armv7a_cache
Definition: armv7a.h:83
bool mmu_enabled
Definition: armv7a.h:84
int(* read_physical_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: armv7a.h:81
int linked_brp
Definition: breakpoints.h:36
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
bool is_set
Definition: breakpoints.h:31
unsigned int number
Definition: breakpoints.h:32
uint32_t asid
Definition: breakpoints.h:28
target_addr_t address
Definition: breakpoints.h:27
const char * name
Definition: command.h:234
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:247
uint32_t value
Definition: cortex_a.h:60
uint32_t control
Definition: cortex_a.h:61
bool used
Definition: cortex_a.h:58
uint8_t brpn
Definition: cortex_a.h:62
struct armv7a_common armv7a_common
Definition: cortex_a.h:75
struct cortex_a_wrp * wrp_list
Definition: cortex_a.h:97
uint32_t didr
Definition: cortex_a.h:100
int brp_num_context
Definition: cortex_a.h:91
struct cortex_a_brp * brp_list
Definition: cortex_a.h:94
uint32_t cp15_control_reg_curr
Definition: cortex_a.h:83
enum cortex_a_dacrfixup_mode dacrfixup_mode
Definition: cortex_a.h:103
int wrp_num_available
Definition: cortex_a.h:96
uint32_t cpudbg_dscr
Definition: cortex_a.h:78
uint32_t cp15_dacr_reg
Definition: cortex_a.h:87
unsigned int common_magic
Definition: cortex_a.h:73
enum cortex_a_isrmasking_mode isrmasking_mode
Definition: cortex_a.h:102
uint32_t cpuid
Definition: cortex_a.h:99
enum arm_mode curr_mode
Definition: cortex_a.h:88
uint32_t cp15_control_reg
Definition: cortex_a.h:81
int brp_num_available
Definition: cortex_a.h:93
uint8_t wrpn
Definition: cortex_a.h:69
bool used
Definition: cortex_a.h:66
uint32_t value
Definition: cortex_a.h:67
uint32_t control
Definition: cortex_a.h:68
int32_t core[2]
Definition: target.h:103
struct target * target
Definition: target.h:98
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
Definition: register.h:111
bool valid
Definition: register.h:126
uint8_t * value
Definition: register.h:122
bool dirty
Definition: register.h:124
struct target * target
Definition: target.h:217
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
const char * name
Name of this type of target.
Definition: target_type.h:31
Definition: target.h:119
int32_t coreid
Definition: target.h:123
struct gdb_service * gdb_service
Definition: target.h:202
bool dbgbase_set
Definition: target.h:177
bool dbg_msg_enabled
Definition: target.h:166
enum target_debug_reason debug_reason
Definition: target.h:157
enum target_state state
Definition: target.h:160
uint32_t dbgbase
Definition: target.h:178
void * private_config
Definition: target.h:168
enum target_endianness endianness
Definition: target.h:158
struct list_head * smp_targets
Definition: target.h:191
unsigned int smp
Definition: target.h:190
bool reset_halt
Definition: target.h:147
bool is_set
Definition: breakpoints.h:47
unsigned int length
Definition: breakpoints.h:43
unsigned int number
Definition: breakpoints.h:48
target_addr_t address
Definition: breakpoints.h:42
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1774
void target_free_all_working_areas(struct target *target)
Definition: target.c:2160
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:379
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:361
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1275
int target_register_timer_callback(int(*callback)(void *priv), unsigned int time_ms, enum target_timer_type type, void *priv)
The period is very approximate, the callback can happen much more often or much more rarely than spec...
Definition: target.c:1668
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:343
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1247
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4841
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4664
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:325
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
#define ERROR_TARGET_INIT_FAILED
Definition: target.h:784
static bool target_was_examined(const struct target *target)
Definition: target.h:432
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:788
#define ERROR_TARGET_INVALID
Definition: target.h:783
@ TARGET_TIMER_TYPE_PERIODIC
Definition: target.h:323
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:275
@ TARGET_EVENT_HALTED
Definition: target.h:255
@ TARGET_EVENT_RESUMED
Definition: target.h:256
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:274
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:267
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:236
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
@ TARGET_BIG_ENDIAN
Definition: target.h:85
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:790
static void target_set_examined(struct target *target)
Sets the examined flag for the given target.
Definition: target.h:439
#define ERROR_TARGET_DATA_ABORT
Definition: target.h:789
#define ERROR_TARGET_TRANSLATION_FAULT
Definition: target.h:791
int target_request(struct target *target, uint32_t request)
int64_t timeval_ms(void)
#define TARGET_ADDR_FMT
Definition: types.h:286
uint64_t target_addr_t
Definition: types.h:279
#define container_of(ptr, type, member)
Cast a member of a structure out to the containing structure.
Definition: types.h:68
static void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
Byte-swap buffer 32-bit.
Definition: types.h:249
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t dummy[96]
Definition: vdebug.c:23
uint8_t count[4]
Definition: vdebug.c:22