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 
668 static int cortex_a_halt(struct target *target);
669 
670 static int cortex_a_halt_smp(struct target *target)
671 {
672  int retval = ERROR_OK;
673  struct target_list *head;
674 
676  struct target *curr = head->target;
677  if ((curr != target) && (curr->state != TARGET_HALTED)
678  && target_was_examined(curr)) {
679  int retval1 = cortex_a_halt(curr);
680  if (retval == ERROR_OK)
681  retval = retval1; // save the first error and continue loop
682  }
683  }
684  return retval;
685 }
686 
687 static int update_halt_gdb(struct target *target)
688 {
689  struct target *gdb_target = NULL;
690  struct target_list *head;
691  struct target *curr;
692  int retval = ERROR_OK;
693 
694  if (target->gdb_service && target->gdb_service->core[0] == -1) {
697  retval = cortex_a_halt_smp(target);
698  }
699 
700  if (target->gdb_service)
701  gdb_target = target->gdb_service->target;
702 
704  curr = head->target;
705  /* skip calling context */
706  if (curr == target)
707  continue;
708  if (!target_was_examined(curr))
709  continue;
710  /* skip targets that were already halted */
711  if (curr->state == TARGET_HALTED)
712  continue;
713  /* Skip gdb_target; it alerts GDB so has to be polled as last one */
714  if (curr == gdb_target)
715  continue;
716 
717  /* avoid recursion in cortex_a_poll() */
718  curr->smp = 0;
719  cortex_a_poll(curr);
720  curr->smp = 1;
721  }
722 
723  /* after all targets were updated, poll the gdb serving target */
724  if (gdb_target && gdb_target != target)
725  cortex_a_poll(gdb_target);
726  return retval;
727 }
728 
729 /*
730  * Cortex-A Run control
731  */
732 
733 static int cortex_a_poll(struct target *target)
734 {
735  int retval = ERROR_OK;
736  uint32_t dscr;
737  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
738  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
739  enum target_state prev_target_state = target->state;
740  /* toggle to another core is done by gdb as follow */
741  /* maint packet J core_id */
742  /* continue */
743  /* the next polling trigger an halt event sent to gdb */
744  if ((target->state == TARGET_HALTED) && (target->smp) &&
745  (target->gdb_service) &&
746  (!target->gdb_service->target)) {
750  return retval;
751  }
752  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
753  armv7a->debug_base + CPUDBG_DSCR, &dscr);
754  if (retval != ERROR_OK)
755  return retval;
756  cortex_a->cpudbg_dscr = dscr;
757 
759  if (prev_target_state != TARGET_HALTED) {
760  /* We have a halting debug event */
761  LOG_TARGET_DEBUG(target, "Target halted");
763 
764  retval = cortex_a_debug_entry(target);
765  if (retval != ERROR_OK)
766  return retval;
767 
768  if (target->smp) {
769  retval = update_halt_gdb(target);
770  if (retval != ERROR_OK)
771  return retval;
772  }
773 
774  if (prev_target_state == TARGET_DEBUG_RUNNING) {
776  } else { /* prev_target_state is RUNNING, UNKNOWN or RESET */
777  if (arm_semihosting(target, &retval) != 0)
778  return retval;
779 
782  }
783  }
784  } else
786 
787  return retval;
788 }
789 
790 static int cortex_a_halt(struct target *target)
791 {
792  int retval;
793  uint32_t dscr;
794  struct armv7a_common *armv7a = target_to_armv7a(target);
795 
796  /*
797  * Tell the core to be halted by writing DRCR with 0x1
798  * and then wait for the core to be halted.
799  */
800  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
801  armv7a->debug_base + CPUDBG_DRCR, DRCR_HALT);
802  if (retval != ERROR_OK)
803  return retval;
804 
805  dscr = 0; /* force read of dscr */
807  DSCR_CORE_HALTED, &dscr);
808  if (retval != ERROR_OK) {
809  LOG_TARGET_ERROR(target, "Error waiting for halt");
810  return retval;
811  }
812 
814 
815  return ERROR_OK;
816 }
817 
818 static int cortex_a_internal_restore(struct target *target, bool current,
819  target_addr_t *address, bool handle_breakpoints, bool debug_execution)
820 {
821  struct armv7a_common *armv7a = target_to_armv7a(target);
822  struct arm *arm = &armv7a->arm;
823  int retval;
824  uint32_t resume_pc;
825 
826  if (!debug_execution)
828 
829 #if 0
830  if (debug_execution) {
831  /* Disable interrupts */
832  /* We disable interrupts in the PRIMASK register instead of
833  * masking with C_MASKINTS,
834  * This is probably the same issue as Cortex-M3 Errata 377493:
835  * C_MASKINTS in parallel with disabled interrupts can cause
836  * local faults to not be taken. */
837  buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
838  armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = true;
839  armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = true;
840 
841  /* Make sure we are in Thumb mode */
842  buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_XPSR].value, 0, 32,
843  buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_XPSR].value, 0,
844  32) | (1 << 24));
845  armv7m->core_cache->reg_list[ARMV7M_XPSR].dirty = true;
846  armv7m->core_cache->reg_list[ARMV7M_XPSR].valid = true;
847  }
848 #endif
849 
850  /* current = true: continue on current pc, otherwise continue at <address> */
851  resume_pc = buf_get_u32(arm->pc->value, 0, 32);
852  if (!current)
853  resume_pc = *address;
854  else
855  *address = resume_pc;
856 
857  /* Make sure that the Armv7 gdb thumb fixups does not
858  * kill the return address
859  */
860  switch (arm->core_state) {
861  case ARM_STATE_ARM:
862  resume_pc &= 0xFFFFFFFC;
863  break;
864  case ARM_STATE_THUMB:
865  case ARM_STATE_THUMB_EE:
866  /* When the return address is loaded into PC
867  * bit 0 must be 1 to stay in Thumb state
868  */
869  resume_pc |= 0x1;
870  break;
871  case ARM_STATE_JAZELLE:
872  LOG_TARGET_ERROR(target, "How do I resume into Jazelle state??");
873  return ERROR_FAIL;
874  case ARM_STATE_AARCH64:
875  LOG_TARGET_ERROR(target, "Shouldn't be in AARCH64 state");
876  return ERROR_FAIL;
877  }
878  LOG_TARGET_DEBUG(target, "resume pc = 0x%08" PRIx32, resume_pc);
879  buf_set_u32(arm->pc->value, 0, 32, resume_pc);
880  arm->pc->dirty = true;
881  arm->pc->valid = true;
882 
883  /* restore dpm_mode at system halt */
885  /* called it now before restoring context because it uses cpu
886  * register r0 for restoring cp15 control register */
888  if (retval != ERROR_OK)
889  return retval;
890  retval = cortex_a_restore_context(target, handle_breakpoints);
891  if (retval != ERROR_OK)
892  return retval;
895 
896  /* registers are now invalid */
898 
899 #if 0
900  /* the front-end may request us not to handle breakpoints */
901  if (handle_breakpoints) {
902  /* Single step past breakpoint at current address */
903  breakpoint = breakpoint_find(target, resume_pc);
904  if (breakpoint) {
905  LOG_DEBUG("unset breakpoint at 0x%8.8x", breakpoint->address);
906  cortex_m3_unset_breakpoint(target, breakpoint);
907  cortex_m3_single_step_core(target);
908  cortex_m3_set_breakpoint(target, breakpoint);
909  }
910  }
911 
912 #endif
913  return retval;
914 }
915 
917 {
918  struct armv7a_common *armv7a = target_to_armv7a(target);
919  struct arm *arm = &armv7a->arm;
920  int retval;
921  uint32_t dscr;
922  /*
923  * * Restart core and wait for it to be started. Clear ITRen and sticky
924  * * exception flags: see ARMv7 ARM, C5.9.
925  *
926  * REVISIT: for single stepping, we probably want to
927  * disable IRQs by default, with optional override...
928  */
929 
930  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
931  armv7a->debug_base + CPUDBG_DSCR, &dscr);
932  if (retval != ERROR_OK)
933  return retval;
934 
935  if ((dscr & DSCR_INSTR_COMP) == 0)
936  LOG_TARGET_ERROR(target, "DSCR InstrCompl must be set before leaving debug!");
937 
938  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
939  armv7a->debug_base + CPUDBG_DSCR, dscr & ~DSCR_ITR_EN);
940  if (retval != ERROR_OK)
941  return retval;
942 
943  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
944  armv7a->debug_base + CPUDBG_DRCR, DRCR_RESTART |
946  if (retval != ERROR_OK)
947  return retval;
948 
949  dscr = 0; /* force read of dscr */
951  DSCR_CORE_RESTARTED, &dscr);
952  if (retval != ERROR_OK) {
953  LOG_TARGET_ERROR(target, "Error waiting for resume");
954  return retval;
955  }
956 
959 
960  /* registers are now invalid */
962 
963  return ERROR_OK;
964 }
965 
966 static int cortex_a_restore_smp(struct target *target, bool handle_breakpoints)
967 {
968  int retval = ERROR_OK;
969  struct target_list *head;
971 
973  struct target *curr = head->target;
974  if ((curr != target) && (curr->state != TARGET_RUNNING)
975  && target_was_examined(curr)) {
976  /* resume current address , not in step mode */
977  int retval2 = cortex_a_internal_restore(curr, true, &address,
978  handle_breakpoints, false);
979 
980  if (retval2 == ERROR_OK)
981  retval2 = cortex_a_internal_restart(curr);
982 
983  if (retval2 == ERROR_OK)
985 
986  if (retval == ERROR_OK)
987  retval = retval2; // save the first error
988  }
989  }
990  return retval;
991 }
992 
993 static int cortex_a_resume(struct target *target, bool current,
994  target_addr_t address, bool handle_breakpoints, bool debug_execution)
995 {
996  int retval = 0;
997  /* dummy resume for smp toggle in order to reduce gdb impact */
998  if ((target->smp) && (target->gdb_service->core[1] != -1)) {
999  /* simulate a start and halt of target */
1002  /* fake resume at next poll we play the target core[1], see poll*/
1004  return 0;
1005  }
1006  cortex_a_internal_restore(target, current, &address, handle_breakpoints,
1007  debug_execution);
1008  if (target->smp) {
1009  target->gdb_service->core[0] = -1;
1010  retval = cortex_a_restore_smp(target, handle_breakpoints);
1011  if (retval != ERROR_OK)
1012  return retval;
1013  }
1015 
1016  if (!debug_execution) {
1019  LOG_TARGET_DEBUG(target, "target resumed at " TARGET_ADDR_FMT, address);
1020  } else {
1023  LOG_TARGET_DEBUG(target, "target debug resumed at " TARGET_ADDR_FMT, address);
1024  }
1025 
1026  return ERROR_OK;
1027 }
1028 
1030 {
1031  uint32_t dscr;
1032  int retval = ERROR_OK;
1033  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1034  struct armv7a_common *armv7a = target_to_armv7a(target);
1035  struct arm *arm = &armv7a->arm;
1036 
1037  LOG_TARGET_DEBUG(target, "dscr = 0x%08" PRIx32, cortex_a->cpudbg_dscr);
1038 
1039  /* REVISIT surely we should not re-read DSCR !! */
1040  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1041  armv7a->debug_base + CPUDBG_DSCR, &dscr);
1042  if (retval != ERROR_OK)
1043  return retval;
1044 
1045  /* REVISIT see A TRM 12.11.4 steps 2..3 -- make sure that any
1046  * imprecise data aborts get discarded by issuing a Data
1047  * Synchronization Barrier: ARMV4_5_MCR(15, 0, 0, 7, 10, 4).
1048  */
1049 
1050  /* Enable the ITR execution once we are in debug mode */
1051  dscr |= DSCR_ITR_EN;
1052  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1053  armv7a->debug_base + CPUDBG_DSCR, dscr);
1054  if (retval != ERROR_OK)
1055  return retval;
1056 
1057  /* Examine debug reason */
1058  arm_dpm_report_dscr(&armv7a->dpm, cortex_a->cpudbg_dscr);
1059 
1060  /* First load register accessible through core debug port */
1061  retval = arm_dpm_read_current_registers(&armv7a->dpm);
1062  if (retval != ERROR_OK)
1063  return retval;
1064 
1065  if (arm->spsr) {
1066  /* read SPSR */
1067  retval = arm_dpm_read_reg(&armv7a->dpm, arm->spsr, 17);
1068  if (retval != ERROR_OK)
1069  return retval;
1070  }
1071 
1072  /* save address of instruction that triggered the watchpoint? */
1074  /*
1075  * On v7.1 Debug architecture or on synchronous (precise) watchpoints,
1076  * WFAR is not used. Take the instruction address from halted PC.
1077  * See ARM DDI 0406C.d chapter C5.2.2 "Effect of entering Debug state
1078  * on CP15 registers and the DBGWFAR".
1079  */
1080  if (((armv7a->dpm.didr >> 16) & 0xf) > 4 ||
1082  armv7a->dpm.wp_addr = buf_get_u32(arm->pc->value, 0, 32);
1083  } else {
1084  uint32_t wfar;
1085 
1086  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1087  armv7a->debug_base + CPUDBG_WFAR,
1088  &wfar);
1089  if (retval != ERROR_OK)
1090  return retval;
1091  arm_dpm_report_wfar(&armv7a->dpm, wfar);
1092  }
1093  }
1094 
1095 #if 0
1096 /* TODO, Move this */
1097  uint32_t cp15_control_register, cp15_cacr, cp15_nacr;
1098  cortex_a_read_cp(target, &cp15_control_register, 15, 0, 1, 0, 0);
1099  LOG_DEBUG("cp15_control_register = 0x%08x", cp15_control_register);
1100 
1101  cortex_a_read_cp(target, &cp15_cacr, 15, 0, 1, 0, 2);
1102  LOG_DEBUG("cp15 Coprocessor Access Control Register = 0x%08x", cp15_cacr);
1103 
1104  cortex_a_read_cp(target, &cp15_nacr, 15, 0, 1, 1, 2);
1105  LOG_DEBUG("cp15 Nonsecure Access Control Register = 0x%08x", cp15_nacr);
1106 #endif
1107 
1108  /* Are we in an exception handler */
1109 /* armv4_5->exception_number = 0; */
1110  if (armv7a->post_debug_entry) {
1111  retval = armv7a->post_debug_entry(target);
1112  if (retval != ERROR_OK)
1113  return retval;
1114  }
1115 
1116  return retval;
1117 }
1118 
1120 {
1121  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1122  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1123  int retval;
1124 
1125  /* MRC p15,0,<Rt>,c1,c0,0 ; Read CP15 System Control Register */
1126  retval = armv7a->arm.mrc(target, 15,
1127  0, 0, /* op1, op2 */
1128  1, 0, /* CRn, CRm */
1129  &cortex_a->cp15_control_reg);
1130  if (retval != ERROR_OK)
1131  return retval;
1132  LOG_TARGET_DEBUG(target, "cp15_control_reg: %8.8" PRIx32,
1133  cortex_a->cp15_control_reg);
1134  cortex_a->cp15_control_reg_curr = cortex_a->cp15_control_reg;
1135 
1136  if (!armv7a->is_armv7r)
1138 
1139  if (!armv7a->armv7a_mmu.armv7a_cache.info_valid)
1141 
1142  if (armv7a->is_armv7r) {
1143  armv7a->armv7a_mmu.mmu_enabled = false;
1144  } else {
1145  armv7a->armv7a_mmu.mmu_enabled = cortex_a->cp15_control_reg & 0x1U;
1146  }
1148  cortex_a->cp15_control_reg & 0x4U;
1150  cortex_a->cp15_control_reg & 0x1000U;
1151  cortex_a->curr_mode = armv7a->arm.core_mode;
1152 
1153  /* switch to SVC mode to read DACR */
1154  arm_dpm_modeswitch(&armv7a->dpm, ARM_MODE_SVC);
1155  armv7a->arm.mrc(target, 15,
1156  0, 0, 3, 0,
1157  &cortex_a->cp15_dacr_reg);
1158 
1159  LOG_DEBUG("cp15_dacr_reg: %8.8" PRIx32,
1160  cortex_a->cp15_dacr_reg);
1161 
1162  arm_dpm_modeswitch(&armv7a->dpm, ARM_MODE_ANY);
1163  return ERROR_OK;
1164 }
1165 
1167  unsigned long bit_mask, unsigned long value)
1168 {
1169  struct armv7a_common *armv7a = target_to_armv7a(target);
1170  uint32_t dscr;
1171 
1172  /* Read DSCR */
1173  int retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
1174  armv7a->debug_base + CPUDBG_DSCR, &dscr);
1175  if (retval != ERROR_OK)
1176  return retval;
1177 
1178  /* clear bitfield */
1179  dscr &= ~bit_mask;
1180  /* put new value */
1181  dscr |= value & bit_mask;
1182 
1183  /* write new DSCR */
1184  return mem_ap_write_atomic_u32(armv7a->debug_ap,
1185  armv7a->debug_base + CPUDBG_DSCR, dscr);
1186 }
1187 
1188 /*
1189  * Single-step on ARMv7a/r is implemented through a HW breakpoint that hits
1190  * every instruction at any address except the address of the current
1191  * instruction.
1192  * Such HW breakpoint is never hit in case of a single instruction that jumps
1193  * on itself (infinite loop), or a WFI or a WFE. In this case, halt the CPU
1194  * after a timeout.
1195  * The jump on itself would be executed several times before the timeout forces
1196  * the halt, but this is not an issue. In ARMv7a/r there are few "pathological"
1197  * instructions, listed below, that jumps on itself and that can have side
1198  * effects if executed more than once; but they are not considered as real use
1199  * cases generated by a compiler.
1200  * Some example:
1201  * - 'pop {pc}' or multi register 'pop' including PC, when the new PC value is
1202  * the same value of current PC. The single step will not stop at the first
1203  * 'pop' and will continue taking values from the stack, modifying SP at each
1204  * iteration.
1205  * - 'rfeda', 'rfedb', 'rfeia', 'rfeib', when the new PC value is the same
1206  * value of current PC. The register provided to the instruction (usually SP)
1207  * will be incremented or decremented at each iteration.
1208  *
1209  * TODO: fix exit in case of error, cleaning HW breakpoints.
1210  */
1211 static int cortex_a_step(struct target *target, bool current, target_addr_t address,
1212  bool handle_breakpoints)
1213 {
1214  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1215  struct armv7a_common *armv7a = target_to_armv7a(target);
1216  struct arm *arm = &armv7a->arm;
1217  struct breakpoint *breakpoint = NULL;
1218  struct breakpoint stepbreakpoint;
1219  struct reg *r;
1220  int retval;
1221 
1222  if (target->state != TARGET_HALTED) {
1223  LOG_TARGET_ERROR(target, "not halted");
1224  return ERROR_TARGET_NOT_HALTED;
1225  }
1226 
1227  /* current = true: continue on current pc, otherwise continue at <address> */
1228  r = arm->pc;
1229  if (!current)
1230  buf_set_u32(r->value, 0, 32, address);
1231  else
1232  address = buf_get_u32(r->value, 0, 32);
1233 
1234  /* The front-end may request us not to handle breakpoints.
1235  * But since Cortex-A uses breakpoint for single step,
1236  * we MUST handle breakpoints.
1237  */
1238  handle_breakpoints = true;
1239  if (handle_breakpoints) {
1241  if (breakpoint)
1243  }
1244 
1245  /* Setup single step breakpoint */
1246  stepbreakpoint.address = address;
1247  stepbreakpoint.asid = 0;
1248  stepbreakpoint.length = (arm->core_state == ARM_STATE_THUMB)
1249  ? 2 : 4;
1250  stepbreakpoint.type = BKPT_HARD;
1251  stepbreakpoint.is_set = false;
1252 
1253  /* Disable interrupts during single step if requested */
1254  if (cortex_a->isrmasking_mode == CORTEX_A_ISRMASK_ON) {
1256  if (retval != ERROR_OK)
1257  return retval;
1258  }
1259 
1260  /* Break on IVA mismatch */
1261  cortex_a_set_breakpoint(target, &stepbreakpoint, 0x04);
1262 
1264 
1265  retval = cortex_a_resume(target, true, address, false, false);
1266  if (retval != ERROR_OK)
1267  return retval;
1268 
1269  // poll at least once before starting the timeout
1270  retval = cortex_a_poll(target);
1271  if (retval != ERROR_OK)
1272  return retval;
1273 
1274  int64_t then = timeval_ms() + 100;
1275  while (target->state != TARGET_HALTED) {
1276  if (timeval_ms() > then)
1277  break;
1278 
1279  retval = cortex_a_poll(target);
1280  if (retval != ERROR_OK)
1281  return retval;
1282  }
1283 
1284  if (target->state != TARGET_HALTED) {
1285  LOG_TARGET_DEBUG(target, "timeout waiting for target halt, try halt");
1286 
1287  retval = cortex_a_halt(target);
1288  if (retval != ERROR_OK)
1289  return retval;
1290 
1291  retval = cortex_a_poll(target);
1292  if (retval != ERROR_OK)
1293  return retval;
1294 
1295  if (target->state != TARGET_HALTED) {
1296  LOG_TARGET_ERROR(target, "timeout waiting for target halt");
1297  return ERROR_FAIL;
1298  }
1299  }
1300 
1301  cortex_a_unset_breakpoint(target, &stepbreakpoint);
1302 
1303  /* Re-enable interrupts if they were disabled */
1304  if (cortex_a->isrmasking_mode == CORTEX_A_ISRMASK_ON) {
1306  if (retval != ERROR_OK)
1307  return retval;
1308  }
1309 
1310 
1312 
1313  if (breakpoint)
1315 
1316  return ERROR_OK;
1317 }
1318 
1319 static int cortex_a_restore_context(struct target *target, bool bpwp)
1320 {
1321  struct armv7a_common *armv7a = target_to_armv7a(target);
1322 
1323  LOG_TARGET_DEBUG(target, " ");
1324 
1325  if (armv7a->pre_restore_context)
1326  armv7a->pre_restore_context(target);
1327 
1328  return arm_dpm_write_dirty_registers(&armv7a->dpm, bpwp);
1329 }
1330 
1331 /*
1332  * Cortex-A Breakpoint and watchpoint functions
1333  */
1334 
1335 /* Setup hardware Breakpoint Register Pair */
1337  struct breakpoint *breakpoint, uint8_t matchmode)
1338 {
1339  int retval;
1340  int brp_i = 0;
1341  uint32_t control;
1342  uint8_t byte_addr_select = 0x0F;
1343  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1344  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1345  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1346 
1347  if (breakpoint->is_set) {
1348  LOG_WARNING("breakpoint already set");
1349  return ERROR_OK;
1350  }
1351 
1352  if (breakpoint->type == BKPT_HARD) {
1353  while (brp_list[brp_i].used && (brp_i < cortex_a->brp_num))
1354  brp_i++;
1355  if (brp_i >= cortex_a->brp_num) {
1356  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1358  }
1359  breakpoint_hw_set(breakpoint, brp_i);
1360  if (breakpoint->length == 3) {
1361  /* Thumb-2 breakpoint: fixup to length 4 if word aligned,
1362  * set byte mask for length 2 if unaligned */
1363  if (IS_ALIGNED(breakpoint->address, 4))
1364  breakpoint->length = 4;
1365  else
1366  breakpoint->length = 2;
1367  }
1368  if (breakpoint->length == 2)
1369  byte_addr_select = (3 << (breakpoint->address & 0x02));
1370  control = ((matchmode & 0x7) << 20)
1371  | (byte_addr_select << 5)
1372  | (3 << 1) | 1;
1373  brp_list[brp_i].used = true;
1374  brp_list[brp_i].value = (breakpoint->address & 0xFFFFFFFC);
1375  brp_list[brp_i].control = control;
1376  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1377  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1378  brp_list[brp_i].value);
1379  if (retval != ERROR_OK)
1380  return retval;
1381  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1382  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1383  brp_list[brp_i].control);
1384  if (retval != ERROR_OK)
1385  return retval;
1386  LOG_DEBUG("brp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1387  brp_list[brp_i].control,
1388  brp_list[brp_i].value);
1389  } else if (breakpoint->type == BKPT_SOFT) {
1390  uint8_t code[4];
1391  if (breakpoint->length == 2) {
1392  /* length == 2: Thumb breakpoint */
1393  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1394  } else if (breakpoint->length == 3) {
1395  /* length == 3: Thumb-2 breakpoint, actual encoding is
1396  * a regular Thumb BKPT instruction but we replace a
1397  * 32bit Thumb-2 instruction, so fix-up the breakpoint
1398  * length
1399  */
1400  buf_set_u32(code, 0, 32, ARMV5_T_BKPT(0x11));
1401  breakpoint->length = 4;
1402  } else {
1403  /* length == 4, normal ARM breakpoint */
1404  buf_set_u32(code, 0, 32, ARMV5_BKPT(0x11));
1405  }
1406 
1407  /*
1408  * ARMv7-A/R fetches instructions in little-endian on both LE and BE CPUs.
1409  * But Cortex-R4 and Cortex-R5 big-endian require BE instructions.
1410  * https://developer.arm.com/documentation/den0042/a/Coding-for-Cortex-R-Processors/Endianness
1411  * https://developer.arm.com/documentation/den0013/d/Porting/Endianness
1412  */
1413  if ((((cortex_a->cpuid & CPUDBG_CPUID_MASK) == CPUDBG_CPUID_CORTEX_R4) ||
1414  ((cortex_a->cpuid & CPUDBG_CPUID_MASK) == CPUDBG_CPUID_CORTEX_R5)) &&
1416  // In place swapping is allowed
1417  buf_bswap32(code, code, 4);
1418  }
1419 
1420  retval = target_read_memory(target,
1421  breakpoint->address & 0xFFFFFFFE,
1422  breakpoint->length, 1,
1424  if (retval != ERROR_OK)
1425  return retval;
1426 
1427  /* make sure data cache is cleaned & invalidated down to PoC */
1429 
1430  retval = target_write_memory(target,
1431  breakpoint->address & 0xFFFFFFFE,
1432  breakpoint->length, 1, code);
1433  if (retval != ERROR_OK)
1434  return retval;
1435 
1436  /* update i-cache at breakpoint location */
1439 
1440  breakpoint->is_set = true;
1441  }
1442 
1443  return ERROR_OK;
1444 }
1445 
1447  struct breakpoint *breakpoint, uint8_t matchmode)
1448 {
1449  int retval = ERROR_FAIL;
1450  int brp_i = 0;
1451  uint32_t control;
1452  uint8_t byte_addr_select = 0x0F;
1453  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1454  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1455  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1456 
1457  if (breakpoint->is_set) {
1458  LOG_WARNING("breakpoint already set");
1459  return retval;
1460  }
1461  /*check available context BRPs*/
1462  while ((brp_list[brp_i].used ||
1463  (brp_list[brp_i].type != BRP_CONTEXT)) && (brp_i < cortex_a->brp_num))
1464  brp_i++;
1465 
1466  if (brp_i >= cortex_a->brp_num) {
1467  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1468  return ERROR_FAIL;
1469  }
1470 
1471  breakpoint_hw_set(breakpoint, brp_i);
1472  control = ((matchmode & 0x7) << 20)
1473  | (byte_addr_select << 5)
1474  | (3 << 1) | 1;
1475  brp_list[brp_i].used = true;
1476  brp_list[brp_i].value = (breakpoint->asid);
1477  brp_list[brp_i].control = control;
1478  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1479  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1480  brp_list[brp_i].value);
1481  if (retval != ERROR_OK)
1482  return retval;
1483  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1484  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1485  brp_list[brp_i].control);
1486  if (retval != ERROR_OK)
1487  return retval;
1488  LOG_DEBUG("brp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1489  brp_list[brp_i].control,
1490  brp_list[brp_i].value);
1491  return ERROR_OK;
1492 
1493 }
1494 
1496 {
1497  int retval = ERROR_FAIL;
1498  int brp_1 = 0; /* holds the contextID pair */
1499  int brp_2 = 0; /* holds the IVA pair */
1500  uint32_t control_ctx, control_iva;
1501  uint8_t ctx_byte_addr_select = 0x0F;
1502  uint8_t iva_byte_addr_select = 0x0F;
1503  uint8_t ctx_machmode = 0x03;
1504  uint8_t iva_machmode = 0x01;
1505  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1506  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1507  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1508 
1509  if (breakpoint->is_set) {
1510  LOG_WARNING("breakpoint already set");
1511  return retval;
1512  }
1513  /*check available context BRPs*/
1514  while ((brp_list[brp_1].used ||
1515  (brp_list[brp_1].type != BRP_CONTEXT)) && (brp_1 < cortex_a->brp_num))
1516  brp_1++;
1517 
1518  LOG_DEBUG("brp(CTX) found num: %d", brp_1);
1519  if (brp_1 >= cortex_a->brp_num) {
1520  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1521  return ERROR_FAIL;
1522  }
1523 
1524  while ((brp_list[brp_2].used ||
1525  (brp_list[brp_2].type != BRP_NORMAL)) && (brp_2 < cortex_a->brp_num))
1526  brp_2++;
1527 
1528  LOG_DEBUG("brp(IVA) found num: %d", brp_2);
1529  if (brp_2 >= cortex_a->brp_num) {
1530  LOG_ERROR("ERROR Can not find free Breakpoint Register Pair");
1531  return ERROR_FAIL;
1532  }
1533 
1534  breakpoint_hw_set(breakpoint, brp_1);
1535  breakpoint->linked_brp = brp_2;
1536  control_ctx = ((ctx_machmode & 0x7) << 20)
1537  | (brp_2 << 16)
1538  | (0 << 14)
1539  | (ctx_byte_addr_select << 5)
1540  | (3 << 1) | 1;
1541  brp_list[brp_1].used = true;
1542  brp_list[brp_1].value = (breakpoint->asid);
1543  brp_list[brp_1].control = control_ctx;
1544  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1545  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_1].brpn,
1546  brp_list[brp_1].value);
1547  if (retval != ERROR_OK)
1548  return retval;
1549  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1550  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_1].brpn,
1551  brp_list[brp_1].control);
1552  if (retval != ERROR_OK)
1553  return retval;
1554 
1555  control_iva = ((iva_machmode & 0x7) << 20)
1556  | (brp_1 << 16)
1557  | (iva_byte_addr_select << 5)
1558  | (3 << 1) | 1;
1559  brp_list[brp_2].used = true;
1560  brp_list[brp_2].value = (breakpoint->address & 0xFFFFFFFC);
1561  brp_list[brp_2].control = control_iva;
1562  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1563  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_2].brpn,
1564  brp_list[brp_2].value);
1565  if (retval != ERROR_OK)
1566  return retval;
1567  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1568  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_2].brpn,
1569  brp_list[brp_2].control);
1570  if (retval != ERROR_OK)
1571  return retval;
1572 
1573  return ERROR_OK;
1574 }
1575 
1577 {
1578  int retval;
1579  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1580  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1581  struct cortex_a_brp *brp_list = cortex_a->brp_list;
1582 
1583  if (!breakpoint->is_set) {
1584  LOG_WARNING("breakpoint not set");
1585  return ERROR_OK;
1586  }
1587 
1588  if (breakpoint->type == BKPT_HARD) {
1589  if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
1590  int brp_i = breakpoint->number;
1591  int brp_j = breakpoint->linked_brp;
1592  if (brp_i >= cortex_a->brp_num) {
1593  LOG_DEBUG("Invalid BRP number in breakpoint");
1594  return ERROR_OK;
1595  }
1596  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1597  brp_list[brp_i].control, brp_list[brp_i].value);
1598  brp_list[brp_i].used = false;
1599  brp_list[brp_i].value = 0;
1600  brp_list[brp_i].control = 0;
1601  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1602  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1603  brp_list[brp_i].control);
1604  if (retval != ERROR_OK)
1605  return retval;
1606  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1607  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1608  brp_list[brp_i].value);
1609  if (retval != ERROR_OK)
1610  return retval;
1611  if ((brp_j < 0) || (brp_j >= cortex_a->brp_num)) {
1612  LOG_DEBUG("Invalid BRP number in breakpoint");
1613  return ERROR_OK;
1614  }
1615  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_j,
1616  brp_list[brp_j].control, brp_list[brp_j].value);
1617  brp_list[brp_j].used = false;
1618  brp_list[brp_j].value = 0;
1619  brp_list[brp_j].control = 0;
1620  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1621  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_j].brpn,
1622  brp_list[brp_j].control);
1623  if (retval != ERROR_OK)
1624  return retval;
1625  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1626  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_j].brpn,
1627  brp_list[brp_j].value);
1628  if (retval != ERROR_OK)
1629  return retval;
1630  breakpoint->linked_brp = 0;
1631  breakpoint->is_set = false;
1632  return ERROR_OK;
1633 
1634  } else {
1635  int brp_i = breakpoint->number;
1636  if (brp_i >= cortex_a->brp_num) {
1637  LOG_DEBUG("Invalid BRP number in breakpoint");
1638  return ERROR_OK;
1639  }
1640  LOG_DEBUG("rbp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, brp_i,
1641  brp_list[brp_i].control, brp_list[brp_i].value);
1642  brp_list[brp_i].used = false;
1643  brp_list[brp_i].value = 0;
1644  brp_list[brp_i].control = 0;
1645  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1646  armv7a->debug_base + CPUDBG_BCR_BASE + 4 * brp_list[brp_i].brpn,
1647  brp_list[brp_i].control);
1648  if (retval != ERROR_OK)
1649  return retval;
1650  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1651  armv7a->debug_base + CPUDBG_BVR_BASE + 4 * brp_list[brp_i].brpn,
1652  brp_list[brp_i].value);
1653  if (retval != ERROR_OK)
1654  return retval;
1655  breakpoint->is_set = false;
1656  return ERROR_OK;
1657  }
1658  } else {
1659 
1660  /* make sure data cache is cleaned & invalidated down to PoC */
1662  breakpoint->length);
1663 
1664  /* restore original instruction (kept in target endianness) */
1665  if (breakpoint->length == 4) {
1666  retval = target_write_memory(target,
1667  breakpoint->address & 0xFFFFFFFE,
1668  4, 1, breakpoint->orig_instr);
1669  if (retval != ERROR_OK)
1670  return retval;
1671  } else {
1672  retval = target_write_memory(target,
1673  breakpoint->address & 0xFFFFFFFE,
1674  2, 1, breakpoint->orig_instr);
1675  if (retval != ERROR_OK)
1676  return retval;
1677  }
1678 
1679  /* update i-cache at breakpoint location */
1681  breakpoint->length);
1683  breakpoint->length);
1684  }
1685  breakpoint->is_set = false;
1686 
1687  return ERROR_OK;
1688 }
1689 
1691  struct breakpoint *breakpoint)
1692 {
1693  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1694 
1695  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1696  LOG_INFO("no hardware breakpoint available");
1698  }
1699 
1700  if (breakpoint->type == BKPT_HARD)
1701  cortex_a->brp_num_available--;
1702 
1703  return cortex_a_set_breakpoint(target, breakpoint, 0x00); /* Exact match */
1704 }
1705 
1707  struct breakpoint *breakpoint)
1708 {
1709  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1710 
1711  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1712  LOG_INFO("no hardware breakpoint available");
1714  }
1715 
1716  if (breakpoint->type == BKPT_HARD)
1717  cortex_a->brp_num_available--;
1718 
1719  return cortex_a_set_context_breakpoint(target, breakpoint, 0x02); /* asid match */
1720 }
1721 
1723  struct breakpoint *breakpoint)
1724 {
1725  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1726 
1727  if ((breakpoint->type == BKPT_HARD) && (cortex_a->brp_num_available < 1)) {
1728  LOG_INFO("no hardware breakpoint available");
1730  }
1731 
1732  if (breakpoint->type == BKPT_HARD)
1733  cortex_a->brp_num_available--;
1734 
1736 }
1737 
1738 
1740 {
1741  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1742 
1743 #if 0
1744 /* It is perfectly possible to remove breakpoints while the target is running */
1745  if (target->state != TARGET_HALTED) {
1746  LOG_WARNING("target not halted");
1747  return ERROR_TARGET_NOT_HALTED;
1748  }
1749 #endif
1750 
1751  if (breakpoint->is_set) {
1753  if (breakpoint->type == BKPT_HARD)
1754  cortex_a->brp_num_available++;
1755  }
1756 
1757 
1758  return ERROR_OK;
1759 }
1760 
1772 {
1773  int retval = ERROR_OK;
1774  int wrp_i = 0;
1775  uint32_t control;
1776  uint32_t address;
1777  uint8_t address_mask;
1778  uint8_t byte_address_select;
1779  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1780  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1781  struct cortex_a_wrp *wrp_list = cortex_a->wrp_list;
1782 
1783  if (watchpoint->is_set) {
1784  LOG_WARNING("watchpoint already set");
1785  return retval;
1786  }
1787 
1788  /* check available context WRPs */
1789  while (wrp_list[wrp_i].used && (wrp_i < cortex_a->wrp_num))
1790  wrp_i++;
1791 
1792  if (wrp_i >= cortex_a->wrp_num) {
1793  LOG_ERROR("ERROR Can not find free Watchpoint Register Pair");
1794  return ERROR_FAIL;
1795  }
1796 
1797  if (watchpoint->length == 0 || watchpoint->length > 0x80000000U ||
1798  (watchpoint->length & (watchpoint->length - 1))) {
1799  LOG_WARNING("watchpoint length must be a power of 2");
1800  return ERROR_FAIL;
1801  }
1802 
1803  if (watchpoint->address & (watchpoint->length - 1)) {
1804  LOG_WARNING("watchpoint address must be aligned at length");
1805  return ERROR_FAIL;
1806  }
1807 
1808  /* FIXME: ARM DDI 0406C: address_mask is optional. What to do if it's missing? */
1809  /* handle wp length 1 and 2 through byte select */
1810  switch (watchpoint->length) {
1811  case 1:
1812  byte_address_select = BIT(watchpoint->address & 0x3);
1813  address = watchpoint->address & ~0x3;
1814  address_mask = 0;
1815  break;
1816 
1817  case 2:
1818  byte_address_select = 0x03 << (watchpoint->address & 0x2);
1819  address = watchpoint->address & ~0x3;
1820  address_mask = 0;
1821  break;
1822 
1823  case 4:
1824  byte_address_select = 0x0f;
1826  address_mask = 0;
1827  break;
1828 
1829  default:
1830  byte_address_select = 0xff;
1832  address_mask = ilog2(watchpoint->length);
1833  break;
1834  }
1835 
1836  uint8_t load_store_access_control;
1837  switch (watchpoint->rw) {
1838  case WPT_READ:
1839  load_store_access_control = 1;
1840  break;
1841  case WPT_WRITE:
1842  load_store_access_control = 2;
1843  break;
1844  case WPT_ACCESS:
1845  load_store_access_control = 3;
1846  break;
1847  default:
1848  LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
1849  return ERROR_FAIL;
1850  };
1851 
1852  watchpoint_set(watchpoint, wrp_i);
1853  control = (address_mask << 24) |
1854  (byte_address_select << 5) |
1855  (load_store_access_control << 3) |
1856  (0x3 << 1) | 1;
1857  wrp_list[wrp_i].used = true;
1858  wrp_list[wrp_i].value = address;
1859  wrp_list[wrp_i].control = control;
1860 
1861  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1862  armv7a->debug_base + CPUDBG_WVR_BASE + 4 * wrp_list[wrp_i].wrpn,
1863  wrp_list[wrp_i].value);
1864  if (retval != ERROR_OK)
1865  return retval;
1866 
1867  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1868  armv7a->debug_base + CPUDBG_WCR_BASE + 4 * wrp_list[wrp_i].wrpn,
1869  wrp_list[wrp_i].control);
1870  if (retval != ERROR_OK)
1871  return retval;
1872 
1873  LOG_DEBUG("wp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, wrp_i,
1874  wrp_list[wrp_i].control,
1875  wrp_list[wrp_i].value);
1876 
1877  return ERROR_OK;
1878 }
1879 
1889 {
1890  int retval;
1891  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1892  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
1893  struct cortex_a_wrp *wrp_list = cortex_a->wrp_list;
1894 
1895  if (!watchpoint->is_set) {
1896  LOG_WARNING("watchpoint not set");
1897  return ERROR_OK;
1898  }
1899 
1900  int wrp_i = watchpoint->number;
1901  if (wrp_i >= cortex_a->wrp_num) {
1902  LOG_DEBUG("Invalid WRP number in watchpoint");
1903  return ERROR_OK;
1904  }
1905  LOG_DEBUG("wrp %i control 0x%0" PRIx32 " value 0x%0" PRIx32, wrp_i,
1906  wrp_list[wrp_i].control, wrp_list[wrp_i].value);
1907  wrp_list[wrp_i].used = false;
1908  wrp_list[wrp_i].value = 0;
1909  wrp_list[wrp_i].control = 0;
1910  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1911  armv7a->debug_base + CPUDBG_WCR_BASE + 4 * wrp_list[wrp_i].wrpn,
1912  wrp_list[wrp_i].control);
1913  if (retval != ERROR_OK)
1914  return retval;
1915  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
1916  armv7a->debug_base + CPUDBG_WVR_BASE + 4 * wrp_list[wrp_i].wrpn,
1917  wrp_list[wrp_i].value);
1918  if (retval != ERROR_OK)
1919  return retval;
1920  watchpoint->is_set = false;
1921 
1922  return ERROR_OK;
1923 }
1924 
1934 {
1935  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1936 
1937  if (cortex_a->wrp_num_available < 1) {
1938  LOG_INFO("no hardware watchpoint available");
1940  }
1941 
1942  int retval = cortex_a_set_watchpoint(target, watchpoint);
1943  if (retval != ERROR_OK)
1944  return retval;
1945 
1946  cortex_a->wrp_num_available--;
1947  return ERROR_OK;
1948 }
1949 
1959 {
1960  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
1961 
1962  if (watchpoint->is_set) {
1963  cortex_a->wrp_num_available++;
1965  }
1966  return ERROR_OK;
1967 }
1968 
1969 
1970 /*
1971  * Cortex-A Reset functions
1972  */
1973 
1975 {
1976  struct armv7a_common *armv7a = target_to_armv7a(target);
1977 
1978  LOG_DEBUG(" ");
1979 
1980  /* FIXME when halt is requested, make it work somehow... */
1981 
1982  /* This function can be called in "target not examined" state */
1983 
1984  /* Issue some kind of warm reset. */
1987  else if (jtag_get_reset_config() & RESET_HAS_SRST) {
1988  /* REVISIT handle "pulls" cases, if there's
1989  * hardware that needs them to work.
1990  */
1991 
1992  /*
1993  * FIXME: fix reset when transport is not JTAG. This is a temporary
1994  * work-around for release v0.10 that is not intended to stay!
1995  */
1996  if (!transport_is_jtag() ||
1999 
2000  } else {
2001  LOG_ERROR("%s: how to reset?", target_name(target));
2002  return ERROR_FAIL;
2003  }
2004 
2005  /* registers are now invalid */
2006  if (armv7a->arm.core_cache)
2008 
2010 
2011  return ERROR_OK;
2012 }
2013 
2015 {
2016  struct armv7a_common *armv7a = target_to_armv7a(target);
2017  int retval;
2018 
2019  LOG_DEBUG(" ");
2020 
2021  /* be certain SRST is off */
2023 
2024  if (target_was_examined(target)) {
2025  retval = cortex_a_poll(target);
2026  if (retval != ERROR_OK)
2027  return retval;
2028  }
2029 
2030  if (target->reset_halt) {
2031  if (target->state != TARGET_HALTED) {
2032  LOG_WARNING("%s: ran after reset and before halt ...",
2033  target_name(target));
2034  if (target_was_examined(target)) {
2035  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2036  armv7a->debug_base + CPUDBG_DRCR, DRCR_HALT);
2037  if (retval != ERROR_OK)
2038  return retval;
2039  } else
2041  }
2042  }
2043 
2044  return ERROR_OK;
2045 }
2046 
2047 static int cortex_a_set_dcc_mode(struct target *target, uint32_t mode, uint32_t *dscr)
2048 {
2049  /* Changes the mode of the DCC between non-blocking, stall, and fast mode.
2050  * New desired mode must be in mode. Current value of DSCR must be in
2051  * *dscr, which is updated with new value.
2052  *
2053  * This function elides actually sending the mode-change over the debug
2054  * interface if the mode is already set as desired.
2055  */
2056  uint32_t new_dscr = (*dscr & ~DSCR_EXT_DCC_MASK) | mode;
2057  if (new_dscr != *dscr) {
2058  struct armv7a_common *armv7a = target_to_armv7a(target);
2059  int retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2060  armv7a->debug_base + CPUDBG_DSCR, new_dscr);
2061  if (retval == ERROR_OK)
2062  *dscr = new_dscr;
2063  return retval;
2064  } else {
2065  return ERROR_OK;
2066  }
2067 }
2068 
2069 static int cortex_a_wait_dscr_bits(struct target *target, uint32_t mask,
2070  uint32_t value, uint32_t *dscr)
2071 {
2072  /* Waits until the specified bit(s) of DSCR take on a specified value. */
2073  struct armv7a_common *armv7a = target_to_armv7a(target);
2074  int64_t then;
2075  int retval;
2076 
2077  if ((*dscr & mask) == value)
2078  return ERROR_OK;
2079 
2080  then = timeval_ms();
2081  while (1) {
2082  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2083  armv7a->debug_base + CPUDBG_DSCR, dscr);
2084  if (retval != ERROR_OK) {
2085  LOG_ERROR("Could not read DSCR register");
2086  return retval;
2087  }
2088  if ((*dscr & mask) == value)
2089  break;
2090  if (timeval_ms() > then + 1000) {
2091  LOG_ERROR("timeout waiting for DSCR bit change");
2092  return ERROR_FAIL;
2093  }
2094  }
2095  return ERROR_OK;
2096 }
2097 
2098 static int cortex_a_read_copro(struct target *target, uint32_t opcode,
2099  uint32_t *data, uint32_t *dscr)
2100 {
2101  int retval;
2102  struct armv7a_common *armv7a = target_to_armv7a(target);
2103 
2104  /* Move from coprocessor to R0. */
2105  retval = cortex_a_exec_opcode(target, opcode, dscr);
2106  if (retval != ERROR_OK)
2107  return retval;
2108 
2109  /* Move from R0 to DTRTX. */
2110  retval = cortex_a_exec_opcode(target, ARMV4_5_MCR(14, 0, 0, 0, 5, 0), dscr);
2111  if (retval != ERROR_OK)
2112  return retval;
2113 
2114  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture
2115  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2116  * must also check TXfull_l). Most of the time this will be free
2117  * because TXfull_l will be set immediately and cached in dscr. */
2119  DSCR_DTRTX_FULL_LATCHED, dscr);
2120  if (retval != ERROR_OK)
2121  return retval;
2122 
2123  /* Read the value transferred to DTRTX. */
2124  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2125  armv7a->debug_base + CPUDBG_DTRTX, data);
2126  if (retval != ERROR_OK)
2127  return retval;
2128 
2129  return ERROR_OK;
2130 }
2131 
2132 static int cortex_a_read_dfar_dfsr(struct target *target, uint32_t *dfar,
2133  uint32_t *dfsr, uint32_t *dscr)
2134 {
2135  int retval;
2136 
2137  if (dfar) {
2138  retval = cortex_a_read_copro(target, ARMV4_5_MRC(15, 0, 0, 6, 0, 0), dfar, dscr);
2139  if (retval != ERROR_OK)
2140  return retval;
2141  }
2142 
2143  if (dfsr) {
2144  retval = cortex_a_read_copro(target, ARMV4_5_MRC(15, 0, 0, 5, 0, 0), dfsr, dscr);
2145  if (retval != ERROR_OK)
2146  return retval;
2147  }
2148 
2149  return ERROR_OK;
2150 }
2151 
2152 static int cortex_a_write_copro(struct target *target, uint32_t opcode,
2153  uint32_t data, uint32_t *dscr)
2154 {
2155  int retval;
2156  struct armv7a_common *armv7a = target_to_armv7a(target);
2157 
2158  /* Write the value into DTRRX. */
2159  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2160  armv7a->debug_base + CPUDBG_DTRRX, data);
2161  if (retval != ERROR_OK)
2162  return retval;
2163 
2164  /* Move from DTRRX to R0. */
2165  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), dscr);
2166  if (retval != ERROR_OK)
2167  return retval;
2168 
2169  /* Move from R0 to coprocessor. */
2170  retval = cortex_a_exec_opcode(target, opcode, dscr);
2171  if (retval != ERROR_OK)
2172  return retval;
2173 
2174  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture manual
2175  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2176  * check RXfull_l). Most of the time this will be free because RXfull_l
2177  * will be cleared immediately and cached in dscr. */
2179  if (retval != ERROR_OK)
2180  return retval;
2181 
2182  return ERROR_OK;
2183 }
2184 
2185 static int cortex_a_write_dfar_dfsr(struct target *target, uint32_t dfar,
2186  uint32_t dfsr, uint32_t *dscr)
2187 {
2188  int retval;
2189 
2190  retval = cortex_a_write_copro(target, ARMV4_5_MCR(15, 0, 0, 6, 0, 0), dfar, dscr);
2191  if (retval != ERROR_OK)
2192  return retval;
2193 
2194  retval = cortex_a_write_copro(target, ARMV4_5_MCR(15, 0, 0, 5, 0, 0), dfsr, dscr);
2195  if (retval != ERROR_OK)
2196  return retval;
2197 
2198  return ERROR_OK;
2199 }
2200 
2201 static int cortex_a_dfsr_to_error_code(uint32_t dfsr)
2202 {
2203  uint32_t status, upper4;
2204 
2205  if (dfsr & (1 << 9)) {
2206  /* LPAE format. */
2207  status = dfsr & 0x3f;
2208  upper4 = status >> 2;
2209  if (upper4 == 1 || upper4 == 2 || upper4 == 3 || upper4 == 15)
2211  else if (status == 33)
2213  else
2214  return ERROR_TARGET_DATA_ABORT;
2215  } else {
2216  /* Normal format. */
2217  status = ((dfsr >> 6) & 0x10) | (dfsr & 0xf);
2218  if (status == 1)
2220  else if (status == 5 || status == 7 || status == 3 || status == 6 ||
2221  status == 9 || status == 11 || status == 13 || status == 15)
2223  else
2224  return ERROR_TARGET_DATA_ABORT;
2225  }
2226 }
2227 
2229  uint32_t size, uint32_t count, const uint8_t *buffer, uint32_t *dscr)
2230 {
2231  /* Writes count objects of size size from *buffer. Old value of DSCR must
2232  * be in *dscr; updated to new value. This is slow because it works for
2233  * non-word-sized objects. Avoid unaligned accesses as they do not work
2234  * on memory address space without "Normal" attribute. If size == 4 and
2235  * the address is aligned, cortex_a_write_cpu_memory_fast should be
2236  * preferred.
2237  * Preconditions:
2238  * - Address is in R0.
2239  * - R0 is marked dirty.
2240  */
2241  struct armv7a_common *armv7a = target_to_armv7a(target);
2242  struct arm *arm = &armv7a->arm;
2243  int retval;
2244 
2245  /* Mark register R1 as dirty, to use for transferring data. */
2246  arm_reg_current(arm, 1)->dirty = true;
2247 
2248  /* Switch to non-blocking mode if not already in that mode. */
2250  if (retval != ERROR_OK)
2251  return retval;
2252 
2253  /* Go through the objects. */
2254  while (count) {
2255  /* Write the value to store into DTRRX. */
2256  uint32_t data, opcode;
2257  if (size == 1)
2258  data = *buffer;
2259  else if (size == 2)
2261  else
2263  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2264  armv7a->debug_base + CPUDBG_DTRRX, data);
2265  if (retval != ERROR_OK)
2266  return retval;
2267 
2268  /* Transfer the value from DTRRX to R1. */
2269  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), dscr);
2270  if (retval != ERROR_OK)
2271  return retval;
2272 
2273  /* Write the value transferred to R1 into memory. */
2274  if (size == 1)
2275  opcode = ARMV4_5_STRB_IP(1, 0);
2276  else if (size == 2)
2277  opcode = ARMV4_5_STRH_IP(1, 0);
2278  else
2279  opcode = ARMV4_5_STRW_IP(1, 0);
2280  retval = cortex_a_exec_opcode(target, opcode, dscr);
2281  if (retval != ERROR_OK)
2282  return retval;
2283 
2284  /* Check for faults and return early. */
2286  return ERROR_OK; /* A data fault is not considered a system failure. */
2287 
2288  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture
2289  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2290  * must also check RXfull_l). Most of the time this will be free
2291  * because RXfull_l will be cleared immediately and cached in dscr. */
2293  if (retval != ERROR_OK)
2294  return retval;
2295 
2296  /* Advance. */
2297  buffer += size;
2298  --count;
2299  }
2300 
2301  return ERROR_OK;
2302 }
2303 
2305  uint32_t count, const uint8_t *buffer, uint32_t *dscr)
2306 {
2307  /* Writes count objects of size 4 from *buffer. Old value of DSCR must be
2308  * in *dscr; updated to new value. This is fast but only works for
2309  * word-sized objects at aligned addresses.
2310  * Preconditions:
2311  * - Address is in R0 and must be a multiple of 4.
2312  * - R0 is marked dirty.
2313  */
2314  struct armv7a_common *armv7a = target_to_armv7a(target);
2315  int retval;
2316 
2317  /* Switch to fast mode if not already in that mode. */
2319  if (retval != ERROR_OK)
2320  return retval;
2321 
2322  /* Latch STC instruction. */
2323  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2324  armv7a->debug_base + CPUDBG_ITR, ARMV4_5_STC(0, 1, 0, 1, 14, 5, 0, 4));
2325  if (retval != ERROR_OK)
2326  return retval;
2327 
2328  /* Transfer all the data and issue all the instructions. */
2329  return mem_ap_write_buf_noincr(armv7a->debug_ap, buffer,
2330  4, count, armv7a->debug_base + CPUDBG_DTRRX);
2331 }
2332 
2334  uint32_t address, uint32_t size,
2335  uint32_t count, const uint8_t *buffer)
2336 {
2337  /* Write memory through the CPU. */
2338  int retval, final_retval;
2339  struct armv7a_common *armv7a = target_to_armv7a(target);
2340  struct arm *arm = &armv7a->arm;
2341  uint32_t dscr, orig_dfar, orig_dfsr, fault_dscr, fault_dfar, fault_dfsr;
2342 
2343  LOG_DEBUG("Writing CPU memory address 0x%" PRIx32 " size %" PRIu32 " count %" PRIu32,
2344  address, size, count);
2345  if (target->state != TARGET_HALTED) {
2346  LOG_TARGET_ERROR(target, "not halted");
2347  return ERROR_TARGET_NOT_HALTED;
2348  }
2349 
2350  if (!count)
2351  return ERROR_OK;
2352 
2353  /* Clear any abort. */
2354  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2356  if (retval != ERROR_OK)
2357  return retval;
2358 
2359  /* Read DSCR. */
2360  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2361  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2362  if (retval != ERROR_OK)
2363  return retval;
2364 
2365  /* Switch to non-blocking mode if not already in that mode. */
2367  if (retval != ERROR_OK)
2368  return retval;
2369 
2370  /* Mark R0 as dirty. */
2371  arm_reg_current(arm, 0)->dirty = true;
2372 
2373  /* Read DFAR and DFSR, as they will be modified in the event of a fault. */
2374  retval = cortex_a_read_dfar_dfsr(target, &orig_dfar, &orig_dfsr, &dscr);
2375  if (retval != ERROR_OK)
2376  return retval;
2377 
2378  /* Get the memory address into R0. */
2379  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2380  armv7a->debug_base + CPUDBG_DTRRX, address);
2381  if (retval != ERROR_OK)
2382  return retval;
2383  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), &dscr);
2384  if (retval != ERROR_OK)
2385  return retval;
2386 
2387  if (size == 4 && (address % 4) == 0) {
2388  /* We are doing a word-aligned transfer, so use fast mode. */
2390  } else {
2391  /* Use slow path. Adjust size for aligned accesses */
2392  switch (address % 4) {
2393  case 1:
2394  case 3:
2395  count *= size;
2396  size = 1;
2397  break;
2398  case 2:
2399  if (size == 4) {
2400  count *= 2;
2401  size = 2;
2402  }
2403  break;
2404  case 0:
2405  default:
2406  break;
2407  }
2409  }
2410 
2411  final_retval = retval;
2412 
2413  /* Switch to non-blocking mode if not already in that mode. */
2415  if (final_retval == ERROR_OK)
2416  final_retval = retval;
2417 
2418  /* Wait for last issued instruction to complete. */
2419  retval = cortex_a_wait_instrcmpl(target, &dscr, true);
2420  if (final_retval == ERROR_OK)
2421  final_retval = retval;
2422 
2423  /* Wait until DTRRX is empty (according to ARMv7-A/-R architecture manual
2424  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2425  * check RXfull_l). Most of the time this will be free because RXfull_l
2426  * will be cleared immediately and cached in dscr. However, don't do this
2427  * if there is fault, because then the instruction might not have completed
2428  * successfully. */
2429  if (!(dscr & DSCR_STICKY_ABORT_PRECISE)) {
2431  if (retval != ERROR_OK)
2432  return retval;
2433  }
2434 
2435  /* If there were any sticky abort flags, clear them. */
2437  fault_dscr = dscr;
2441  } else {
2442  fault_dscr = 0;
2443  }
2444 
2445  /* Handle synchronous data faults. */
2446  if (fault_dscr & DSCR_STICKY_ABORT_PRECISE) {
2447  if (final_retval == ERROR_OK) {
2448  /* Final return value will reflect cause of fault. */
2449  retval = cortex_a_read_dfar_dfsr(target, &fault_dfar, &fault_dfsr, &dscr);
2450  if (retval == ERROR_OK) {
2451  LOG_ERROR("data abort at 0x%08" PRIx32 ", dfsr = 0x%08" PRIx32, fault_dfar, fault_dfsr);
2452  final_retval = cortex_a_dfsr_to_error_code(fault_dfsr);
2453  } else
2454  final_retval = retval;
2455  }
2456  /* Fault destroyed DFAR/DFSR; restore them. */
2457  retval = cortex_a_write_dfar_dfsr(target, orig_dfar, orig_dfsr, &dscr);
2458  if (retval != ERROR_OK)
2459  LOG_ERROR("error restoring dfar/dfsr - dscr = 0x%08" PRIx32, dscr);
2460  }
2461 
2462  /* Handle asynchronous data faults. */
2463  if (fault_dscr & DSCR_STICKY_ABORT_IMPRECISE) {
2464  if (final_retval == ERROR_OK)
2465  /* No other error has been recorded so far, so keep this one. */
2466  final_retval = ERROR_TARGET_DATA_ABORT;
2467  }
2468 
2469  /* If the DCC is nonempty, clear it. */
2470  if (dscr & DSCR_DTRTX_FULL_LATCHED) {
2471  uint32_t dummy;
2472  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2473  armv7a->debug_base + CPUDBG_DTRTX, &dummy);
2474  if (final_retval == ERROR_OK)
2475  final_retval = retval;
2476  }
2477  if (dscr & DSCR_DTRRX_FULL_LATCHED) {
2478  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), &dscr);
2479  if (final_retval == ERROR_OK)
2480  final_retval = retval;
2481  }
2482 
2483  /* Done. */
2484  return final_retval;
2485 }
2486 
2488  uint32_t size, uint32_t count, uint8_t *buffer, uint32_t *dscr)
2489 {
2490  /* Reads count objects of size size into *buffer. Old value of DSCR must be
2491  * in *dscr; updated to new value. This is slow because it works for
2492  * non-word-sized objects. Avoid unaligned accesses as they do not work
2493  * on memory address space without "Normal" attribute. If size == 4 and
2494  * the address is aligned, cortex_a_read_cpu_memory_fast should be
2495  * preferred.
2496  * Preconditions:
2497  * - Address is in R0.
2498  * - R0 is marked dirty.
2499  */
2500  struct armv7a_common *armv7a = target_to_armv7a(target);
2501  struct arm *arm = &armv7a->arm;
2502  int retval;
2503 
2504  /* Mark register R1 as dirty, to use for transferring data. */
2505  arm_reg_current(arm, 1)->dirty = true;
2506 
2507  /* Switch to non-blocking mode if not already in that mode. */
2509  if (retval != ERROR_OK)
2510  return retval;
2511 
2512  /* Go through the objects. */
2513  while (count) {
2514  /* Issue a load of the appropriate size to R1. */
2515  uint32_t opcode, data;
2516  if (size == 1)
2517  opcode = ARMV4_5_LDRB_IP(1, 0);
2518  else if (size == 2)
2519  opcode = ARMV4_5_LDRH_IP(1, 0);
2520  else
2521  opcode = ARMV4_5_LDRW_IP(1, 0);
2522  retval = cortex_a_exec_opcode(target, opcode, dscr);
2523  if (retval != ERROR_OK)
2524  return retval;
2525 
2526  /* Issue a write of R1 to DTRTX. */
2527  retval = cortex_a_exec_opcode(target, ARMV4_5_MCR(14, 0, 1, 0, 5, 0), dscr);
2528  if (retval != ERROR_OK)
2529  return retval;
2530 
2531  /* Check for faults and return early. */
2533  return ERROR_OK; /* A data fault is not considered a system failure. */
2534 
2535  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture
2536  * manual section C8.4.3, checking InstrCmpl_l is not sufficient; one
2537  * must also check TXfull_l). Most of the time this will be free
2538  * because TXfull_l will be set immediately and cached in dscr. */
2540  DSCR_DTRTX_FULL_LATCHED, dscr);
2541  if (retval != ERROR_OK)
2542  return retval;
2543 
2544  /* Read the value transferred to DTRTX into the buffer. */
2545  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2546  armv7a->debug_base + CPUDBG_DTRTX, &data);
2547  if (retval != ERROR_OK)
2548  return retval;
2549  if (size == 1)
2550  *buffer = (uint8_t) data;
2551  else if (size == 2)
2552  target_buffer_set_u16(target, buffer, (uint16_t) data);
2553  else
2555 
2556  /* Advance. */
2557  buffer += size;
2558  --count;
2559  }
2560 
2561  return ERROR_OK;
2562 }
2563 
2565  uint32_t count, uint8_t *buffer, uint32_t *dscr)
2566 {
2567  /* Reads count objects of size 4 into *buffer. Old value of DSCR must be in
2568  * *dscr; updated to new value. This is fast but only works for word-sized
2569  * objects at aligned addresses.
2570  * Preconditions:
2571  * - Address is in R0 and must be a multiple of 4.
2572  * - R0 is marked dirty.
2573  */
2574  struct armv7a_common *armv7a = target_to_armv7a(target);
2575  uint32_t u32;
2576  int retval;
2577 
2578  /* Switch to non-blocking mode if not already in that mode. */
2580  if (retval != ERROR_OK)
2581  return retval;
2582 
2583  /* Issue the LDC instruction via a write to ITR. */
2584  retval = cortex_a_exec_opcode(target, ARMV4_5_LDC(0, 1, 0, 1, 14, 5, 0, 4), dscr);
2585  if (retval != ERROR_OK)
2586  return retval;
2587 
2588  count--;
2589 
2590  if (count > 0) {
2591  /* Switch to fast mode if not already in that mode. */
2593  if (retval != ERROR_OK)
2594  return retval;
2595 
2596  /* Latch LDC instruction. */
2597  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2598  armv7a->debug_base + CPUDBG_ITR, ARMV4_5_LDC(0, 1, 0, 1, 14, 5, 0, 4));
2599  if (retval != ERROR_OK)
2600  return retval;
2601 
2602  /* Read the value transferred to DTRTX into the buffer. Due to fast
2603  * mode rules, this blocks until the instruction finishes executing and
2604  * then reissues the read instruction to read the next word from
2605  * memory. The last read of DTRTX in this call reads the second-to-last
2606  * word from memory and issues the read instruction for the last word.
2607  */
2608  retval = mem_ap_read_buf_noincr(armv7a->debug_ap, buffer,
2609  4, count, armv7a->debug_base + CPUDBG_DTRTX);
2610  if (retval != ERROR_OK)
2611  return retval;
2612 
2613  /* Advance. */
2614  buffer += count * 4;
2615  }
2616 
2617  /* Wait for last issued instruction to complete. */
2618  retval = cortex_a_wait_instrcmpl(target, dscr, false);
2619  if (retval != ERROR_OK)
2620  return retval;
2621 
2622  /* Switch to non-blocking mode if not already in that mode. */
2624  if (retval != ERROR_OK)
2625  return retval;
2626 
2627  /* Check for faults and return early. */
2629  return ERROR_OK; /* A data fault is not considered a system failure. */
2630 
2631  /* Wait until DTRTX is full (according to ARMv7-A/-R architecture manual
2632  * section C8.4.3, checking InstrCmpl_l is not sufficient; one must also
2633  * check TXfull_l). Most of the time this will be free because TXfull_l
2634  * will be set immediately and cached in dscr. */
2636  DSCR_DTRTX_FULL_LATCHED, dscr);
2637  if (retval != ERROR_OK)
2638  return retval;
2639 
2640  /* Read the value transferred to DTRTX into the buffer. This is the last
2641  * word. */
2642  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2643  armv7a->debug_base + CPUDBG_DTRTX, &u32);
2644  if (retval != ERROR_OK)
2645  return retval;
2647 
2648  return ERROR_OK;
2649 }
2650 
2652  uint32_t address, uint32_t size,
2653  uint32_t count, uint8_t *buffer)
2654 {
2655  /* Read memory through the CPU. */
2656  int retval, final_retval;
2657  struct armv7a_common *armv7a = target_to_armv7a(target);
2658  struct arm *arm = &armv7a->arm;
2659  uint32_t dscr, orig_dfar, orig_dfsr, fault_dscr, fault_dfar, fault_dfsr;
2660 
2661  LOG_DEBUG("Reading CPU memory address 0x%" PRIx32 " size %" PRIu32 " count %" PRIu32,
2662  address, size, count);
2663  if (target->state != TARGET_HALTED) {
2664  LOG_TARGET_ERROR(target, "not halted");
2665  return ERROR_TARGET_NOT_HALTED;
2666  }
2667 
2668  if (!count)
2669  return ERROR_OK;
2670 
2671  /* Clear any abort. */
2672  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2674  if (retval != ERROR_OK)
2675  return retval;
2676 
2677  /* Read DSCR */
2678  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2679  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2680  if (retval != ERROR_OK)
2681  return retval;
2682 
2683  /* Switch to non-blocking mode if not already in that mode. */
2685  if (retval != ERROR_OK)
2686  return retval;
2687 
2688  /* Mark R0 as dirty. */
2689  arm_reg_current(arm, 0)->dirty = true;
2690 
2691  /* Read DFAR and DFSR, as they will be modified in the event of a fault. */
2692  retval = cortex_a_read_dfar_dfsr(target, &orig_dfar, &orig_dfsr, &dscr);
2693  if (retval != ERROR_OK)
2694  return retval;
2695 
2696  /* Get the memory address into R0. */
2697  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
2698  armv7a->debug_base + CPUDBG_DTRRX, address);
2699  if (retval != ERROR_OK)
2700  return retval;
2701  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 0, 0, 5, 0), &dscr);
2702  if (retval != ERROR_OK)
2703  return retval;
2704 
2705  if (size == 4 && (address % 4) == 0) {
2706  /* We are doing a word-aligned transfer, so use fast mode. */
2707  retval = cortex_a_read_cpu_memory_fast(target, count, buffer, &dscr);
2708  } else {
2709  /* Use slow path. Adjust size for aligned accesses */
2710  switch (address % 4) {
2711  case 1:
2712  case 3:
2713  count *= size;
2714  size = 1;
2715  break;
2716  case 2:
2717  if (size == 4) {
2718  count *= 2;
2719  size = 2;
2720  }
2721  break;
2722  case 0:
2723  default:
2724  break;
2725  }
2727  }
2728 
2729  final_retval = retval;
2730 
2731  /* Switch to non-blocking mode if not already in that mode. */
2733  if (final_retval == ERROR_OK)
2734  final_retval = retval;
2735 
2736  /* Wait for last issued instruction to complete. */
2737  retval = cortex_a_wait_instrcmpl(target, &dscr, true);
2738  if (final_retval == ERROR_OK)
2739  final_retval = retval;
2740 
2741  /* If there were any sticky abort flags, clear them. */
2743  fault_dscr = dscr;
2747  } else {
2748  fault_dscr = 0;
2749  }
2750 
2751  /* Handle synchronous data faults. */
2752  if (fault_dscr & DSCR_STICKY_ABORT_PRECISE) {
2753  if (final_retval == ERROR_OK) {
2754  /* Final return value will reflect cause of fault. */
2755  retval = cortex_a_read_dfar_dfsr(target, &fault_dfar, &fault_dfsr, &dscr);
2756  if (retval == ERROR_OK) {
2757  LOG_ERROR("data abort at 0x%08" PRIx32 ", dfsr = 0x%08" PRIx32, fault_dfar, fault_dfsr);
2758  final_retval = cortex_a_dfsr_to_error_code(fault_dfsr);
2759  } else
2760  final_retval = retval;
2761  }
2762  /* Fault destroyed DFAR/DFSR; restore them. */
2763  retval = cortex_a_write_dfar_dfsr(target, orig_dfar, orig_dfsr, &dscr);
2764  if (retval != ERROR_OK)
2765  LOG_ERROR("error restoring dfar/dfsr - dscr = 0x%08" PRIx32, dscr);
2766  }
2767 
2768  /* Handle asynchronous data faults. */
2769  if (fault_dscr & DSCR_STICKY_ABORT_IMPRECISE) {
2770  if (final_retval == ERROR_OK)
2771  /* No other error has been recorded so far, so keep this one. */
2772  final_retval = ERROR_TARGET_DATA_ABORT;
2773  }
2774 
2775  /* If the DCC is nonempty, clear it. */
2776  if (dscr & DSCR_DTRTX_FULL_LATCHED) {
2777  uint32_t dummy;
2778  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2779  armv7a->debug_base + CPUDBG_DTRTX, &dummy);
2780  if (final_retval == ERROR_OK)
2781  final_retval = retval;
2782  }
2783  if (dscr & DSCR_DTRRX_FULL_LATCHED) {
2784  retval = cortex_a_exec_opcode(target, ARMV4_5_MRC(14, 0, 1, 0, 5, 0), &dscr);
2785  if (final_retval == ERROR_OK)
2786  final_retval = retval;
2787  }
2788 
2789  /* Done. */
2790  return final_retval;
2791 }
2792 
2793 
2794 /*
2795  * Cortex-A Memory access
2796  *
2797  * This is same Cortex-M3 but we must also use the correct
2798  * ap number for every access.
2799  */
2800 
2802  target_addr_t address, uint32_t size,
2803  uint32_t count, uint8_t *buffer)
2804 {
2805  int retval;
2806 
2807  if (!count || !buffer)
2809 
2810  LOG_DEBUG("Reading memory at real address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2811  address, size, count);
2812 
2813  /* read memory through the CPU */
2817 
2818  return retval;
2819 }
2820 
2822  uint32_t size, uint32_t count, uint8_t *buffer)
2823 {
2824  int retval;
2825 
2826  /* cortex_a handles unaligned memory access */
2827  LOG_DEBUG("Reading memory at address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2828  address, size, count);
2829 
2833 
2834  return retval;
2835 }
2836 
2838  target_addr_t address, uint32_t size,
2839  uint32_t count, const uint8_t *buffer)
2840 {
2841  int retval;
2842 
2843  if (!count || !buffer)
2845 
2846  LOG_DEBUG("Writing memory to real address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2847  address, size, count);
2848 
2849  /* write memory through the CPU */
2853 
2854  return retval;
2855 }
2856 
2858  uint32_t size, uint32_t count, const uint8_t *buffer)
2859 {
2860  int retval;
2861 
2862  /* cortex_a handles unaligned memory access */
2863  LOG_DEBUG("Writing memory at address " TARGET_ADDR_FMT "; size %" PRIu32 "; count %" PRIu32,
2864  address, size, count);
2865 
2869  return retval;
2870 }
2871 
2873  uint32_t count, uint8_t *buffer)
2874 {
2875  uint32_t size;
2876 
2877  /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2878  * will have something to do with the size we leave to it. */
2879  for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2880  if (address & size) {
2881  int retval = target_read_memory(target, address, size, 1, buffer);
2882  if (retval != ERROR_OK)
2883  return retval;
2884  address += size;
2885  count -= size;
2886  buffer += size;
2887  }
2888  }
2889 
2890  /* Read the data with as large access size as possible. */
2891  for (; size > 0; size /= 2) {
2892  uint32_t aligned = count - count % size;
2893  if (aligned > 0) {
2894  int retval = target_read_memory(target, address, size, aligned / size, buffer);
2895  if (retval != ERROR_OK)
2896  return retval;
2897  address += aligned;
2898  count -= aligned;
2899  buffer += aligned;
2900  }
2901  }
2902 
2903  return ERROR_OK;
2904 }
2905 
2907  uint32_t count, const uint8_t *buffer)
2908 {
2909  uint32_t size;
2910 
2911  /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2912  * will have something to do with the size we leave to it. */
2913  for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2914  if (address & size) {
2915  int retval = target_write_memory(target, address, size, 1, buffer);
2916  if (retval != ERROR_OK)
2917  return retval;
2918  address += size;
2919  count -= size;
2920  buffer += size;
2921  }
2922  }
2923 
2924  /* Write the data with as large access size as possible. */
2925  for (; size > 0; size /= 2) {
2926  uint32_t aligned = count - count % size;
2927  if (aligned > 0) {
2928  int retval = target_write_memory(target, address, size, aligned / size, buffer);
2929  if (retval != ERROR_OK)
2930  return retval;
2931  address += aligned;
2932  count -= aligned;
2933  buffer += aligned;
2934  }
2935  }
2936 
2937  return ERROR_OK;
2938 }
2939 
2941 {
2942  struct target *target = priv;
2943  struct armv7a_common *armv7a = target_to_armv7a(target);
2944  int retval;
2945 
2947  return ERROR_OK;
2948  if (!target->dbg_msg_enabled)
2949  return ERROR_OK;
2950 
2951  if (target->state == TARGET_RUNNING) {
2952  uint32_t request;
2953  uint32_t dscr;
2954  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2955  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2956 
2957  /* check if we have data */
2958  int64_t then = timeval_ms();
2959  while ((dscr & DSCR_DTR_TX_FULL) && (retval == ERROR_OK)) {
2960  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2961  armv7a->debug_base + CPUDBG_DTRTX, &request);
2962  if (retval == ERROR_OK) {
2963  target_request(target, request);
2964  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
2965  armv7a->debug_base + CPUDBG_DSCR, &dscr);
2966  }
2967  if (timeval_ms() > then + 1000) {
2968  LOG_ERROR("Timeout waiting for dtr tx full");
2969  return ERROR_FAIL;
2970  }
2971  }
2972  }
2973 
2974  return ERROR_OK;
2975 }
2976 
2977 /*
2978  * Cortex-A target information and configuration
2979  */
2980 
2982 {
2983  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
2984  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
2985  struct adiv5_dap *swjdp = armv7a->arm.dap;
2987 
2988  int i;
2989  int retval = ERROR_OK;
2990  uint32_t didr, cpuid, dbg_osreg, dbg_idpfr1;
2991 
2992  if (!armv7a->debug_ap) {
2993  if (pc->ap_num == DP_APSEL_INVALID) {
2994  /* Search for the APB-AP - it is needed for access to debug registers */
2995  retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv7a->debug_ap);
2996  if (retval != ERROR_OK) {
2997  LOG_ERROR("Could not find APB-AP for debug access");
2998  return retval;
2999  }
3000  } else {
3001  armv7a->debug_ap = dap_get_ap(swjdp, pc->ap_num);
3002  if (!armv7a->debug_ap) {
3003  LOG_ERROR("Cannot get AP");
3004  return ERROR_FAIL;
3005  }
3006  }
3007  }
3008 
3009  retval = mem_ap_init(armv7a->debug_ap);
3010  if (retval != ERROR_OK) {
3011  LOG_ERROR("Could not initialize the APB-AP");
3012  return retval;
3013  }
3014 
3015  armv7a->debug_ap->memaccess_tck = 80;
3016 
3017  if (!target->dbgbase_set) {
3018  LOG_TARGET_DEBUG(target, "dbgbase is not set, trying to detect using the ROM table");
3019  /* Lookup Processor DAP */
3021  &armv7a->debug_base, target->coreid);
3022  if (retval != ERROR_OK) {
3023  LOG_TARGET_ERROR(target, "Can't detect dbgbase from the ROM table; you need to specify it explicitly");
3024  return retval;
3025  }
3026  LOG_DEBUG("Detected core %" PRId32 " dbgbase: " TARGET_ADDR_FMT,
3027  target->coreid, armv7a->debug_base);
3028  } else
3029  armv7a->debug_base = target->dbgbase;
3030 
3031  if ((armv7a->debug_base & (1UL<<31)) == 0)
3033  "Debug base address has bit 31 set to 0. Access to debug registers will likely fail!\n"
3034  "Please fix the target configuration");
3035 
3036  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3037  armv7a->debug_base + CPUDBG_DIDR, &didr);
3038  if (retval != ERROR_OK) {
3039  LOG_DEBUG("Examine %s failed", "DIDR");
3040  return retval;
3041  }
3042 
3043  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3044  armv7a->debug_base + CPUDBG_CPUID, &cpuid);
3045  if (retval != ERROR_OK) {
3046  LOG_DEBUG("Examine %s failed", "CPUID");
3047  return retval;
3048  }
3049 
3050  LOG_DEBUG("didr = 0x%08" PRIx32, didr);
3051  LOG_DEBUG("cpuid = 0x%08" PRIx32, cpuid);
3052 
3053  cortex_a->didr = didr;
3054  cortex_a->cpuid = cpuid;
3055 
3056  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3057  armv7a->debug_base + CPUDBG_PRSR, &dbg_osreg);
3058  if (retval != ERROR_OK)
3059  return retval;
3060  LOG_TARGET_DEBUG(target, "DBGPRSR 0x%" PRIx32, dbg_osreg);
3061 
3062  if ((dbg_osreg & PRSR_POWERUP_STATUS) == 0) {
3063  LOG_TARGET_ERROR(target, "powered down!");
3064  target->state = TARGET_UNKNOWN; /* TARGET_NO_POWER? */
3065  return ERROR_TARGET_INIT_FAILED;
3066  }
3067 
3068  if (dbg_osreg & PRSR_STICKY_RESET_STATUS)
3069  LOG_TARGET_DEBUG(target, "was reset!");
3070 
3071  /* Read DBGOSLSR and check if OSLK is implemented */
3072  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3073  armv7a->debug_base + CPUDBG_OSLSR, &dbg_osreg);
3074  if (retval != ERROR_OK)
3075  return retval;
3076  LOG_TARGET_DEBUG(target, "DBGOSLSR 0x%" PRIx32, dbg_osreg);
3077 
3078  /* check if OS Lock is implemented */
3079  if ((dbg_osreg & OSLSR_OSLM) == OSLSR_OSLM0 || (dbg_osreg & OSLSR_OSLM) == OSLSR_OSLM1) {
3080  /* check if OS Lock is set */
3081  if (dbg_osreg & OSLSR_OSLK) {
3082  LOG_TARGET_DEBUG(target, "OSLock set! Trying to unlock");
3083 
3084  retval = mem_ap_write_atomic_u32(armv7a->debug_ap,
3085  armv7a->debug_base + CPUDBG_OSLAR,
3086  0);
3087  if (retval == ERROR_OK)
3088  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3089  armv7a->debug_base + CPUDBG_OSLSR, &dbg_osreg);
3090 
3091  /* if we fail to access the register or cannot reset the OSLK bit, bail out */
3092  if (retval != ERROR_OK || (dbg_osreg & OSLSR_OSLK) != 0) {
3093  LOG_TARGET_ERROR(target, "OSLock sticky, core not powered?");
3094  target->state = TARGET_UNKNOWN; /* TARGET_NO_POWER? */
3095  return ERROR_TARGET_INIT_FAILED;
3096  }
3097  }
3098  }
3099 
3100  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3101  armv7a->debug_base + CPUDBG_ID_PFR1, &dbg_idpfr1);
3102  if (retval != ERROR_OK)
3103  return retval;
3104 
3105  if (dbg_idpfr1 & 0x000000f0) {
3106  LOG_TARGET_DEBUG(target, "has security extensions");
3108  }
3109  if (dbg_idpfr1 & 0x0000f000) {
3110  LOG_TARGET_DEBUG(target, "has virtualization extensions");
3111  /*
3112  * overwrite and simplify the checks.
3113  * virtualization extensions require implementation of security extension
3114  */
3116  }
3117 
3118  /* Avoid recreating the registers cache */
3119  if (!target_was_examined(target)) {
3120  retval = cortex_a_dpm_setup(cortex_a, didr);
3121  if (retval != ERROR_OK)
3122  return retval;
3123  }
3124 
3125  /* Setup Breakpoint Register Pairs */
3126  cortex_a->brp_num = ((didr >> 24) & 0x0F) + 1;
3127  cortex_a->brp_num_context = ((didr >> 20) & 0x0F) + 1;
3128  cortex_a->brp_num_available = cortex_a->brp_num;
3129  free(cortex_a->brp_list);
3130  cortex_a->brp_list = calloc(cortex_a->brp_num, sizeof(struct cortex_a_brp));
3131 /* cortex_a->brb_enabled = ????; */
3132  for (i = 0; i < cortex_a->brp_num; i++) {
3133  cortex_a->brp_list[i].used = false;
3134  if (i < (cortex_a->brp_num-cortex_a->brp_num_context))
3135  cortex_a->brp_list[i].type = BRP_NORMAL;
3136  else
3137  cortex_a->brp_list[i].type = BRP_CONTEXT;
3138  cortex_a->brp_list[i].value = 0;
3139  cortex_a->brp_list[i].control = 0;
3140  cortex_a->brp_list[i].brpn = i;
3141  }
3142 
3143  LOG_DEBUG("Configured %i hw breakpoints", cortex_a->brp_num);
3144 
3145  /* Setup Watchpoint Register Pairs */
3146  cortex_a->wrp_num = ((didr >> 28) & 0x0F) + 1;
3147  cortex_a->wrp_num_available = cortex_a->wrp_num;
3148  free(cortex_a->wrp_list);
3149  cortex_a->wrp_list = calloc(cortex_a->wrp_num, sizeof(struct cortex_a_wrp));
3150  for (i = 0; i < cortex_a->wrp_num; i++) {
3151  cortex_a->wrp_list[i].used = false;
3152  cortex_a->wrp_list[i].value = 0;
3153  cortex_a->wrp_list[i].control = 0;
3154  cortex_a->wrp_list[i].wrpn = i;
3155  }
3156 
3157  LOG_DEBUG("Configured %i hw watchpoints", cortex_a->wrp_num);
3158 
3159  /* select debug_ap as default */
3160  swjdp->apsel = armv7a->debug_ap->ap_num;
3161 
3163  return ERROR_OK;
3164 }
3165 
3166 static int cortex_a_examine(struct target *target)
3167 {
3168  int retval = ERROR_OK;
3169 
3170  /* Reestablish communication after target reset */
3171  retval = cortex_a_examine_first(target);
3172 
3173  /* Configure core debug access */
3174  if (retval == ERROR_OK)
3176 
3177  return retval;
3178 }
3179 
3180 /*
3181  * Cortex-A target creation and initialization
3182  */
3183 
3184 static int cortex_a_init_target(struct command_context *cmd_ctx,
3185  struct target *target)
3186 {
3187  /* examine_first() does a bunch of this */
3189  return ERROR_OK;
3190 }
3191 
3193  struct cortex_a_common *cortex_a, struct adiv5_dap *dap)
3194 {
3195  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
3196 
3197  /* Setup struct cortex_a_common */
3198  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3199  armv7a->arm.dap = dap;
3200 
3201  /* register arch-specific functions */
3202  armv7a->examine_debug_reason = NULL;
3203 
3205 
3206  armv7a->pre_restore_context = NULL;
3207 
3209 
3210 
3211 /* arm7_9->handle_target_request = cortex_a_handle_target_request; */
3212 
3213  /* REVISIT v7a setup should be in a v7a-specific routine */
3214  armv7a_init_arch_info(target, armv7a);
3217 
3218  return ERROR_OK;
3219 }
3220 
3222 {
3223  struct cortex_a_common *cortex_a;
3224  struct adiv5_private_config *pc;
3225 
3226  if (!target->private_config)
3227  return ERROR_FAIL;
3228 
3229  pc = (struct adiv5_private_config *)target->private_config;
3230 
3231  cortex_a = calloc(1, sizeof(struct cortex_a_common));
3232  if (!cortex_a) {
3233  LOG_ERROR("Out of memory");
3234  return ERROR_FAIL;
3235  }
3236  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3237  cortex_a->armv7a_common.is_armv7r = false;
3239 
3240  return cortex_a_init_arch_info(target, cortex_a, pc->dap);
3241 }
3242 
3244 {
3245  struct cortex_a_common *cortex_a;
3246  struct adiv5_private_config *pc;
3247 
3248  pc = (struct adiv5_private_config *)target->private_config;
3249  if (adiv5_verify_config(pc) != ERROR_OK)
3250  return ERROR_FAIL;
3251 
3252  cortex_a = calloc(1, sizeof(struct cortex_a_common));
3253  if (!cortex_a) {
3254  LOG_ERROR("Out of memory");
3255  return ERROR_FAIL;
3256  }
3257  cortex_a->common_magic = CORTEX_A_COMMON_MAGIC;
3258  cortex_a->armv7a_common.is_armv7r = true;
3259 
3260  return cortex_a_init_arch_info(target, cortex_a, pc->dap);
3261 }
3262 
3264 {
3265  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3266  struct armv7a_common *armv7a = &cortex_a->armv7a_common;
3267  struct arm_dpm *dpm = &armv7a->dpm;
3268  uint32_t dscr;
3269  int retval;
3270 
3271  if (target_was_examined(target)) {
3272  /* Disable halt for breakpoint, watchpoint and vector catch */
3273  retval = mem_ap_read_atomic_u32(armv7a->debug_ap,
3274  armv7a->debug_base + CPUDBG_DSCR, &dscr);
3275  if (retval == ERROR_OK)
3277  armv7a->debug_base + CPUDBG_DSCR,
3279  }
3280 
3281  if (armv7a->debug_ap)
3282  dap_put_ap(armv7a->debug_ap);
3283 
3284  free(cortex_a->wrp_list);
3285  free(cortex_a->brp_list);
3286  arm_free_reg_cache(dpm->arm);
3287  free(dpm->dbp);
3288  free(dpm->dwp);
3289  free(target->private_config);
3290  free(cortex_a);
3291 }
3292 
3293 static int cortex_a_mmu(struct target *target, bool *enabled)
3294 {
3295  struct armv7a_common *armv7a = target_to_armv7a(target);
3296 
3297  if (target->state != TARGET_HALTED) {
3298  LOG_TARGET_ERROR(target, "not halted");
3299  return ERROR_TARGET_NOT_HALTED;
3300  }
3301 
3302  if (armv7a->is_armv7r)
3303  *enabled = false;
3304  else
3306 
3307  return ERROR_OK;
3308 }
3309 
3310 static int cortex_a_virt2phys(struct target *target,
3311  target_addr_t virt, target_addr_t *phys)
3312 {
3313  int retval;
3314  bool mmu_enabled = false;
3315 
3316  /*
3317  * If the MMU was not enabled at debug entry, there is no
3318  * way of knowing if there was ever a valid configuration
3319  * for it and thus it's not safe to enable it. In this case,
3320  * just return the virtual address as physical.
3321  */
3322  cortex_a_mmu(target, &mmu_enabled);
3323  if (!mmu_enabled) {
3324  *phys = virt;
3325  return ERROR_OK;
3326  }
3327 
3328  /* mmu must be enable in order to get a correct translation */
3329  retval = cortex_a_mmu_modify(target, true);
3330  if (retval != ERROR_OK)
3331  return retval;
3332  return armv7a_mmu_translate_va_pa(target, (uint32_t)virt,
3333  phys, 1);
3334 }
3335 
3336 COMMAND_HANDLER(cortex_a_handle_cache_info_command)
3337 {
3339  struct armv7a_common *armv7a = target_to_armv7a(target);
3340 
3342  &armv7a->armv7a_mmu.armv7a_cache);
3343 }
3344 
3345 
3346 COMMAND_HANDLER(cortex_a_handle_dbginit_command)
3347 {
3349  if (!target_was_examined(target)) {
3350  LOG_ERROR("target not examined yet");
3351  return ERROR_FAIL;
3352  }
3353 
3355 }
3356 
3357 COMMAND_HANDLER(handle_cortex_a_mask_interrupts_command)
3358 {
3360  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3361 
3362  static const struct nvp nvp_maskisr_modes[] = {
3363  { .name = "off", .value = CORTEX_A_ISRMASK_OFF },
3364  { .name = "on", .value = CORTEX_A_ISRMASK_ON },
3365  { .name = NULL, .value = -1 },
3366  };
3367  const struct nvp *n;
3368 
3369  if (CMD_ARGC > 0) {
3370  n = nvp_name2value(nvp_maskisr_modes, CMD_ARGV[0]);
3371  if (!n->name) {
3372  LOG_ERROR("Unknown parameter: %s - should be off or on", CMD_ARGV[0]);
3374  }
3375 
3376  cortex_a->isrmasking_mode = n->value;
3377  }
3378 
3379  n = nvp_value2name(nvp_maskisr_modes, cortex_a->isrmasking_mode);
3380  command_print(CMD, "cortex_a interrupt mask %s", n->name);
3381 
3382  return ERROR_OK;
3383 }
3384 
3385 COMMAND_HANDLER(handle_cortex_a_dacrfixup_command)
3386 {
3388  struct cortex_a_common *cortex_a = target_to_cortex_a(target);
3389 
3390  static const struct nvp nvp_dacrfixup_modes[] = {
3391  { .name = "off", .value = CORTEX_A_DACRFIXUP_OFF },
3392  { .name = "on", .value = CORTEX_A_DACRFIXUP_ON },
3393  { .name = NULL, .value = -1 },
3394  };
3395  const struct nvp *n;
3396 
3397  if (CMD_ARGC > 0) {
3398  n = nvp_name2value(nvp_dacrfixup_modes, CMD_ARGV[0]);
3399  if (!n->name)
3401  cortex_a->dacrfixup_mode = n->value;
3402 
3403  }
3404 
3405  n = nvp_value2name(nvp_dacrfixup_modes, cortex_a->dacrfixup_mode);
3406  command_print(CMD, "cortex_a domain access control fixup %s", n->name);
3407 
3408  return ERROR_OK;
3409 }
3410 
3411 static const struct command_registration cortex_a_exec_command_handlers[] = {
3412  {
3413  .name = "cache_info",
3414  .handler = cortex_a_handle_cache_info_command,
3415  .mode = COMMAND_EXEC,
3416  .help = "display information about target caches",
3417  .usage = "",
3418  },
3419  {
3420  .name = "dbginit",
3421  .handler = cortex_a_handle_dbginit_command,
3422  .mode = COMMAND_EXEC,
3423  .help = "Initialize core debug",
3424  .usage = "",
3425  },
3426  {
3427  .name = "maskisr",
3428  .handler = handle_cortex_a_mask_interrupts_command,
3429  .mode = COMMAND_ANY,
3430  .help = "mask cortex_a interrupts",
3431  .usage = "['on'|'off']",
3432  },
3433  {
3434  .name = "dacrfixup",
3435  .handler = handle_cortex_a_dacrfixup_command,
3436  .mode = COMMAND_ANY,
3437  .help = "set domain access control (DACR) to all-manager "
3438  "on memory access",
3439  .usage = "['on'|'off']",
3440  },
3441  {
3442  .chain = armv7a_mmu_command_handlers,
3443  },
3444  {
3446  },
3447 
3449 };
3450 static const struct command_registration cortex_a_command_handlers[] = {
3451  {
3453  },
3454  {
3456  },
3457  {
3458  .name = "cortex_a",
3459  .mode = COMMAND_ANY,
3460  .help = "Cortex-A command group",
3461  .usage = "",
3463  },
3465 };
3466 
3467 struct target_type cortexa_target = {
3468  .name = "cortex_a",
3469 
3470  .poll = cortex_a_poll,
3471  .arch_state = armv7a_arch_state,
3472 
3473  .halt = cortex_a_halt,
3474  .resume = cortex_a_resume,
3475  .step = cortex_a_step,
3476 
3477  .assert_reset = cortex_a_assert_reset,
3478  .deassert_reset = cortex_a_deassert_reset,
3479 
3480  /* REVISIT allow exporting VFP3 registers ... */
3481  .get_gdb_arch = arm_get_gdb_arch,
3482  .get_gdb_reg_list = arm_get_gdb_reg_list,
3483 
3484  .read_memory = cortex_a_read_memory,
3485  .write_memory = cortex_a_write_memory,
3486 
3487  .read_buffer = cortex_a_read_buffer,
3488  .write_buffer = cortex_a_write_buffer,
3489 
3490  .checksum_memory = arm_checksum_memory,
3491  .blank_check_memory = arm_blank_check_memory,
3492 
3493  .run_algorithm = armv4_5_run_algorithm,
3494 
3495  .add_breakpoint = cortex_a_add_breakpoint,
3496  .add_context_breakpoint = cortex_a_add_context_breakpoint,
3497  .add_hybrid_breakpoint = cortex_a_add_hybrid_breakpoint,
3498  .remove_breakpoint = cortex_a_remove_breakpoint,
3499  .add_watchpoint = cortex_a_add_watchpoint,
3500  .remove_watchpoint = cortex_a_remove_watchpoint,
3501 
3502  .commands = cortex_a_command_handlers,
3503  .target_create = cortex_a_target_create,
3504  .target_jim_configure = adiv5_jim_configure,
3505  .init_target = cortex_a_init_target,
3506  .examine = cortex_a_examine,
3507  .deinit_target = cortex_a_deinit_target,
3508 
3509  .read_phys_memory = cortex_a_read_phys_memory,
3510  .write_phys_memory = cortex_a_write_phys_memory,
3511  .mmu = cortex_a_mmu,
3512  .virt2phys = cortex_a_virt2phys,
3513 
3514  .insn_set = armv4_5_insn_set,
3515 };
3516 
3517 static const struct command_registration cortex_r4_exec_command_handlers[] = {
3518  {
3519  .name = "dbginit",
3520  .handler = cortex_a_handle_dbginit_command,
3521  .mode = COMMAND_EXEC,
3522  .help = "Initialize core debug",
3523  .usage = "",
3524  },
3525  {
3526  .name = "maskisr",
3527  .handler = handle_cortex_a_mask_interrupts_command,
3528  .mode = COMMAND_EXEC,
3529  .help = "mask cortex_r4 interrupts",
3530  .usage = "['on'|'off']",
3531  },
3532 
3534 };
3535 static const struct command_registration cortex_r4_command_handlers[] = {
3536  {
3538  },
3539  {
3540  .name = "cortex_r4",
3541  .mode = COMMAND_ANY,
3542  .help = "Cortex-R4 command group",
3543  .usage = "",
3545  },
3547 };
3548 
3549 struct target_type cortexr4_target = {
3550  .name = "cortex_r4",
3551 
3552  .poll = cortex_a_poll,
3553  .arch_state = armv7a_arch_state,
3554 
3555  .halt = cortex_a_halt,
3556  .resume = cortex_a_resume,
3557  .step = cortex_a_step,
3558 
3559  .assert_reset = cortex_a_assert_reset,
3560  .deassert_reset = cortex_a_deassert_reset,
3561 
3562  /* REVISIT allow exporting VFP3 registers ... */
3563  .get_gdb_arch = arm_get_gdb_arch,
3564  .get_gdb_reg_list = arm_get_gdb_reg_list,
3565 
3566  .read_memory = cortex_a_read_phys_memory,
3567  .write_memory = cortex_a_write_phys_memory,
3568 
3569  .checksum_memory = arm_checksum_memory,
3570  .blank_check_memory = arm_blank_check_memory,
3571 
3572  .run_algorithm = armv4_5_run_algorithm,
3573 
3574  .add_breakpoint = cortex_a_add_breakpoint,
3575  .add_context_breakpoint = cortex_a_add_context_breakpoint,
3576  .add_hybrid_breakpoint = cortex_a_add_hybrid_breakpoint,
3577  .remove_breakpoint = cortex_a_remove_breakpoint,
3578  .add_watchpoint = cortex_a_add_watchpoint,
3579  .remove_watchpoint = cortex_a_remove_watchpoint,
3580 
3581  .commands = cortex_r4_command_handlers,
3582  .target_create = cortex_r4_target_create,
3583  .target_jim_configure = adiv5_jim_configure,
3584  .init_target = cortex_a_init_target,
3585  .examine = cortex_a_examine,
3586  .deinit_target = cortex_a_deinit_target,
3587 
3588  .insn_set = armv4_5_insn_set,
3589 };
#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:516
int armv4_5_insn_set(struct command_invocation *cmd, struct target *target, const char **insn_set)
Definition: armv4_5.c:1781
@ 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:1551
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:1625
const char * arm_get_gdb_arch(const struct target *target)
Definition: armv4_5.c:1220
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:1225
@ 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:776
@ 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:1200
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:1525
@ 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:280
int armv7a_handle_cache_info_command(struct command_invocation *cmd, struct armv7a_cache_common *armv7a_cache)
Definition: armv7a.c:182
int armv7a_read_ttbcr(struct target *target)
Definition: armv7a.c:118
int armv7a_arch_state(struct target *target)
Definition: armv7a.c:481
const struct command_registration armv7a_command_handlers[]
Definition: armv7a.c:510
int armv7a_init_arch_info(struct target *target, struct armv7a_common *armv7a)
Definition: armv7a.c:465
int armv7a_identify_cache(struct target *target)
Definition: armv7a.c:314
#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:2801
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:3450
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:2185
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:2906
static int cortex_a_restore_smp(struct target *target, bool handle_breakpoints)
Definition: cortex_a.c:966
static int cortex_a_read_buffer(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
Definition: cortex_a.c:2872
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:1958
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:3517
static const struct command_registration cortex_a_exec_command_handlers[]
Definition: cortex_a.c:3411
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:2487
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:2821
static int cortex_a_read_copro(struct target *target, uint32_t opcode, uint32_t *data, uint32_t *dscr)
Definition: cortex_a.c:2098
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:1319
static int cortex_a_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1739
static int cortex_a_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: cortex_a.c:1211
static int cortex_a_handle_target_request(void *priv)
Definition: cortex_a.c:2940
static int cortex_a_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Add a watchpoint to an Cortex-A target.
Definition: cortex_a.c:1933
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:1771
static int cortex_a_init_arch_info(struct target *target, struct cortex_a_common *cortex_a, struct adiv5_dap *dap)
Definition: cortex_a.c:3192
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:1119
struct target_type cortexr4_target
Definition: cortex_a.c:3549
static int update_halt_gdb(struct target *target)
Definition: cortex_a.c:687
static int cortex_a_read_cpu_memory_fast(struct target *target, uint32_t count, uint8_t *buffer, uint32_t *dscr)
Definition: cortex_a.c:2564
static int cortex_a_set_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1495
static int cortex_r4_target_create(struct target *target)
Definition: cortex_a.c:3243
static int cortex_a_add_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1722
static int cortex_a_examine(struct target *target)
Definition: cortex_a.c:3166
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:2228
static int cortex_a_halt_smp(struct target *target)
Definition: cortex_a.c:670
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:1706
static int cortex_a_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1576
static int cortex_a_set_dscr_bits(struct target *target, unsigned long bit_mask, unsigned long value)
Definition: cortex_a.c:1166
static int cortex_a_deassert_reset(struct target *target)
Definition: cortex_a.c:2014
static int cortex_a_target_create(struct target *target)
Definition: cortex_a.c:3221
static int cortex_a_write_copro(struct target *target, uint32_t opcode, uint32_t data, uint32_t *dscr)
Definition: cortex_a.c:2152
static int cortex_a_read_dfar_dfsr(struct target *target, uint32_t *dfar, uint32_t *dfsr, uint32_t *dscr)
Definition: cortex_a.c:2132
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:1888
static int cortex_a_set_dcc_mode(struct target *target, uint32_t mode, uint32_t *dscr)
Definition: cortex_a.c:2047
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:818
static int cortex_a_virt2phys(struct target *target, target_addr_t virt, target_addr_t *phys)
Definition: cortex_a.c:3310
static int cortex_a_examine_first(struct target *target)
Definition: cortex_a.c:2981
static int cortex_a_mmu(struct target *target, bool *enabled)
Definition: cortex_a.c:3293
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:3184
static int cortex_a_poll(struct target *target)
Definition: cortex_a.c:733
static void cortex_a_deinit_target(struct target *target)
Definition: cortex_a.c:3263
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:3535
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:2333
COMMAND_HANDLER(cortex_a_handle_cache_info_command)
Definition: cortex_a.c:3336
static int cortex_a_set_breakpoint(struct target *target, struct breakpoint *breakpoint, uint8_t matchmode)
Definition: cortex_a.c:1336
static int cortex_a_halt(struct target *target)
Definition: cortex_a.c:790
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:2304
static int cortex_a_set_context_breakpoint(struct target *target, struct breakpoint *breakpoint, uint8_t matchmode)
Definition: cortex_a.c:1446
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:2651
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:916
static int cortex_a_dfsr_to_error_code(uint32_t dfsr)
Definition: cortex_a.c:2201
static int cortex_a_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Definition: cortex_a.c:1690
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:1029
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:2857
static int cortex_a_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: cortex_a.c:993
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:2069
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:2837
static int cortex_a_assert_reset(struct target *target)
Definition: cortex_a.c:1974
struct target_type cortexa_target
Definition: cortex_a.c:3467
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:1837
int adapter_deassert_reset(void)
Definition: jtag/core.c:1909
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1744
int adapter_assert_reset(void)
Definition: jtag/core.c:1889
@ 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:27
const char * name
Name of this type of target.
Definition: target_type.h:32
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:1816
void target_free_all_working_areas(struct target *target)
Definition: target.c:2202
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:381
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:363
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1289
int target_register_timer_callback(int(*callback)(void *priv), unsigned int time_ms, enum target_timer_type type, void *priv)
The period is very approximate, the callback can happen much more often or much more rarely than spec...
Definition: target.c:1701
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:345
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1261
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4877
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4691
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:327
@ 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