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