2 * Copyright (C) 2009 by David Brownell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <jtag/jtag.h>
26 #include "breakpoints.h"
27 #include "target_type.h"
28 #include "arm_opcodes.h"
33 * Implements various ARM DPM operations using architectural debug registers.
34 * These routines layer over core-specific communication methods to cope with
35 * implementation differences between cores like ARM1136 and Cortex-A8.
37 * The "Debug Programmers' Model" (DPM) for ARMv6 and ARMv7 is defined by
38 * Part C (Debug Architecture) of the ARM Architecture Reference Manual,
39 * ARMv7-A and ARMv7-R edition (ARM DDI 0406B). In OpenOCD, DPM operations
40 * are abstracted through internal programming interfaces to share code and
41 * to minimize needless differences in debug behavior between cores.
44 /*----------------------------------------------------------------------*/
50 /* Read coprocessor */
51 static int dpm_mrc(struct target *target, int cpnum,
52 uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
55 struct arm *arm = target_to_arm(target);
56 struct arm_dpm *dpm = arm->dpm;
59 retval = dpm->prepare(dpm);
60 if (retval != ERROR_OK)
63 LOG_DEBUG("MRC p%d, %d, r0, c%d, c%d, %d", cpnum,
65 (int) CRm, (int) op2);
67 /* read coprocessor register into R0; return via DCC */
68 retval = dpm->instr_read_data_r0(dpm,
69 ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
72 /* (void) */ dpm->finish(dpm);
76 static int dpm_mcr(struct target *target, int cpnum,
77 uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
80 struct arm *arm = target_to_arm(target);
81 struct arm_dpm *dpm = arm->dpm;
84 retval = dpm->prepare(dpm);
85 if (retval != ERROR_OK)
88 LOG_DEBUG("MCR p%d, %d, r0, c%d, c%d, %d", cpnum,
90 (int) CRm, (int) op2);
92 /* read DCC into r0; then write coprocessor register from R0 */
93 retval = dpm->instr_write_data_r0(dpm,
94 ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
97 /* (void) */ dpm->finish(dpm);
101 /*----------------------------------------------------------------------*/
104 * Register access utilities
107 /* Toggles between recorded core mode (USR, SVC, etc) and a temporary one.
108 * Routines *must* restore the original mode before returning!!
110 int dpm_modeswitch(struct arm_dpm *dpm, enum arm_mode mode)
115 /* restore previous mode */
116 if (mode == ARM_MODE_ANY)
117 cpsr = buf_get_u32(dpm->arm->cpsr->value, 0, 32);
119 /* else force to the specified mode */
123 retval = dpm->instr_write_data_r0(dpm, ARMV4_5_MSR_GP(0, 0xf, 0), cpsr);
124 if (retval != ERROR_OK)
127 if (dpm->instr_cpsr_sync)
128 retval = dpm->instr_cpsr_sync(dpm);
134 static int dpm_read_reg32(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
137 int retval = ERROR_FAIL;
141 /* return via DCC: "MCR p14, 0, Rnum, c0, c5, 0" */
142 retval = dpm->instr_read_data_dcc(dpm,
143 ARMV4_5_MCR(14, 0, regnum, 0, 5, 0),
147 * "MOV r0, pc"; then return via DCC */
148 retval = dpm->instr_read_data_r0(dpm, 0xe1a0000f, &value);
150 /* NOTE: this seems like a slightly awkward place to update
151 * this value ... but if the PC gets written (the only way
152 * to change what we compute), the arch spec says subsequent
153 * reads return values which are "unpredictable". So this
154 * is always right except in those broken-by-intent cases.
156 switch (dpm->arm->core_state) {
160 case ARM_STATE_THUMB:
161 case ARM_STATE_THUMB_EE:
164 case ARM_STATE_JAZELLE:
165 /* core-specific ... ? */
166 LOG_WARNING("Jazelle PC adjustment unknown");
168 case ARM_STATE_AARCH64:
169 LOG_ERROR("AARCH64: 32bit read requested");
174 /* 16: "MRS r0, CPSR"; then return via DCC
175 * 17: "MRS r0, SPSR"; then return via DCC
177 retval = dpm->instr_read_data_r0(dpm,
178 ARMV4_5_MRS(0, regnum & 1),
183 if (retval == ERROR_OK) {
184 buf_set_u32(r->value, 0, 32, value);
187 LOG_DEBUG("READ: %s, %8.8x", r->name, (unsigned) value);
193 static int dpm_read_reg64(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
197 int retval = ERROR_FAIL;
201 i = 0xd5130400 + regnum; /* msr dbgdtr_el0,reg */
202 retval = dpm->instr_read_data_dcc_64(dpm, i, &value);
206 retval = dpm->instr_read_data_r0_64(dpm, i, &value);
210 retval = dpm->instr_read_data_r0_64(dpm, i, &value);
214 retval = dpm->instr_read_data_r0_64(dpm, i, &value);
221 if (retval == ERROR_OK) {
222 buf_set_u64(r->value, 0, 64, value);
225 LOG_DEBUG("READ: %s, %16.16llx", r->name, (long long)value);
232 /* just read the register -- rely on the core mode being right */
233 static int dpm_read_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
236 return dpm_read_reg64(dpm, r, regnum);
238 return dpm_read_reg32(dpm, r, regnum);
241 static int dpm_write_reg32(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
243 int retval = ERROR_FAIL;
244 uint32_t value = buf_get_u32(r->value, 0, 32);
248 /* load register from DCC: "MRC p14, 0, Rnum, c0, c5, 0" */
249 retval = dpm->instr_write_data_dcc(dpm,
250 ARMV4_5_MRC(14, 0, regnum, 0, 5, 0),
254 * read r0 from DCC; then "MOV pc, r0" */
255 retval = dpm->instr_write_data_r0(dpm, 0xe1a0f000, value);
258 /* 16: read r0 from DCC, then "MSR r0, CPSR_cxsf"
259 * 17: read r0 from DCC, then "MSR r0, SPSR_cxsf"
261 retval = dpm->instr_write_data_r0(dpm,
262 ARMV4_5_MSR_GP(0, 0xf, regnum & 1),
264 if (retval != ERROR_OK)
267 if (regnum == 16 && dpm->instr_cpsr_sync)
268 retval = dpm->instr_cpsr_sync(dpm);
273 if (retval == ERROR_OK) {
275 LOG_DEBUG("WRITE: %s, %8.8x", r->name, (unsigned) value);
282 * Write to program counter and switch the core state (arm/thumb) according to
285 static int dpm_write_pc_core_state(struct arm_dpm *dpm, struct reg *r)
287 uint32_t value = buf_get_u32(r->value, 0, 32);
289 /* read r0 from DCC; then "BX r0" */
290 return dpm->instr_write_data_r0(dpm, ARMV4_5_BX(0), value);
293 static int dpm_write_reg64(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
295 int retval = ERROR_FAIL;
297 uint64_t value = buf_get_u64(r->value, 0, 64);
301 i = 0xd5330400 + regnum;
302 retval = dpm->instr_write_data_dcc_64(dpm, i, value);
306 retval = dpm->instr_write_data_r0_64(dpm, i, value);
309 LOG_DEBUG("register %s (%16.16llx) not defined", r->name,
310 (unsigned long long)value);
314 if (retval == ERROR_OK) {
316 LOG_DEBUG("WRITE: %s, %16.16llx", r->name, (unsigned long long)value);
322 /* just write the register -- rely on the core mode being right */
323 static int dpm_write_reg(struct arm_dpm *dpm, struct reg *r, unsigned regnum)
326 return dpm_write_reg64(dpm, r, regnum);
328 return dpm_write_reg32(dpm, r, regnum);
331 static int arm_dpm_read_current_registers_i(struct arm_dpm *dpm)
333 struct arm *arm = dpm->arm;
334 uint32_t cpsr, instr, core_regs;
337 enum arm_state core_state = arm->core_state;
339 retval = dpm->prepare(dpm);
340 if (retval != ERROR_OK)
343 /* read R0 first (it's used for scratch), then CPSR */
344 r = arm->core_cache->reg_list + 0;
346 retval = dpm_read_reg(dpm, r, 0);
347 if (retval != ERROR_OK)
352 if (core_state == ARM_STATE_AARCH64)
353 instr = 0xd53b4500; /* mrs x0, dspsr_el0 */
355 instr = ARMV4_5_MRS(0, 0);
356 retval = dpm->instr_read_data_r0(dpm, instr, &cpsr);
357 if (retval != ERROR_OK)
360 /* update core mode and state, plus shadow mapping for R8..R14 */
361 arm_set_cpsr(arm, cpsr);
362 if (core_state == ARM_STATE_AARCH64)
363 /* arm_set_cpsr changes core_state, restore it for now */
364 arm->core_state = ARM_STATE_AARCH64;
366 core_regs = arm->core_cache->num_regs;
368 /* REVISIT we can probably avoid reading R1..R14, saving time... */
369 for (unsigned i = 1; i < core_regs; i++) {
370 r = dpm->arm_reg_current(arm, i);
374 retval = dpm_read_reg(dpm, r, i);
375 if (retval != ERROR_OK)
379 /* NOTE: SPSR ignored (if it's even relevant). */
381 /* REVISIT the debugger can trigger various exceptions. See the
382 * ARMv7A architecture spec, section C5.7, for more info about
383 * what defenses are needed; v6 debug has the most issues.
387 /* (void) */ dpm->finish(dpm);
392 * Read basic registers of the the current context: R0 to R15, and CPSR;
393 * sets the core mode (such as USR or IRQ) and state (such as ARM or Thumb).
394 * In normal operation this is called on entry to halting debug state,
395 * possibly after some other operations supporting restore of debug state
396 * or making sure the CPU is fully idle (drain write buffer, etc).
398 int arm_dpm_read_current_registers(struct arm_dpm *dpm)
400 return arm_dpm_read_current_registers_i(dpm);
403 int arm_dpm_read_current_registers_64(struct arm_dpm *dpm)
405 return arm_dpm_read_current_registers_i(dpm);
408 /* Avoid needless I/O ... leave breakpoints and watchpoints alone
409 * unless they're removed, or need updating because of single-stepping
410 * or running debugger code.
412 static int dpm_maybe_update_bpwp(struct arm_dpm *dpm, bool bpwp,
413 struct dpm_bpwp *xp, int *set_p)
415 int retval = ERROR_OK;
422 /* removed or startup; we must disable it */
427 /* disabled, but we must set it */
428 xp->dirty = disable = false;
433 /* set, but we must temporarily disable it */
434 xp->dirty = disable = true;
439 retval = dpm->bpwp_disable(dpm, xp->number);
441 retval = dpm->bpwp_enable(dpm, xp->number,
442 xp->address, xp->control);
444 if (retval != ERROR_OK)
445 LOG_ERROR("%s: can't %s HW %spoint %d",
446 disable ? "disable" : "enable",
447 target_name(dpm->arm->target),
448 (xp->number < 16) ? "break" : "watch",
454 static int dpm_add_breakpoint(struct target *target, struct breakpoint *bp);
457 static int arm_dpm_write_dirty_registers_32(struct arm_dpm *dpm)
459 struct arm *arm = dpm->arm;
460 struct reg_cache *cache = arm->core_cache;
464 /* Scan the registers until we find one that's both dirty and
465 * eligible for flushing. Flush that and everything else that
466 * shares the same core mode setting. Typically this won't
467 * actually find anything to do...
470 enum arm_mode mode = ARM_MODE_ANY;
474 /* check everything except our scratch register R0 */
475 for (unsigned i = 1; i < cache->num_regs; i++) {
479 /* also skip PC, CPSR, and non-dirty */
482 if (arm->cpsr == cache->reg_list + i)
484 if (!cache->reg_list[i].dirty)
487 r = cache->reg_list[i].arch_info;
490 /* may need to pick and set a mode */
495 mode = tmode = r->mode;
497 /* cope with special cases */
500 /* r8..r12 "anything but FIQ" case;
501 * we "know" core mode is accurate
502 * since we haven't changed it yet
504 if (arm->core_mode == ARM_MODE_FIQ
507 tmode = ARM_MODE_USR;
515 /* REVISIT error checks */
516 if (tmode != ARM_MODE_ANY) {
517 retval = dpm_modeswitch(dpm, tmode);
518 if (retval != ERROR_OK)
525 retval = dpm_write_reg(dpm,
528 if (retval != ERROR_OK)
534 /* Restore original CPSR ... assuming either that we changed it,
535 * or it's dirty. Must write PC to ensure the return address is
536 * defined, and must not write it before CPSR.
538 retval = dpm_modeswitch(dpm, ARM_MODE_ANY);
539 if (retval != ERROR_OK)
541 arm->cpsr->dirty = false;
543 /* restore the PC, make sure to also switch the core state
544 * to whatever it was set to with "arm core_state" command.
545 * target code will have set PC to an appropriate resume address.
547 retval = dpm_write_pc_core_state(dpm, arm->pc);
548 if (retval != ERROR_OK)
550 /* on Cortex-A5 (as found on NXP VF610 SoC), BX instruction
551 * executed in debug state doesn't appear to set the PC,
552 * explicitly set it with a "MOV pc, r0". This doesn't influence
553 * CPSR on Cortex-A9 so it should be OK. Maybe due to different
556 retval = dpm_write_reg(dpm, arm->pc, 15);
557 if (retval != ERROR_OK)
559 arm->pc->dirty = false;
561 /* flush R0 -- it's *very* dirty by now */
562 retval = dpm_write_reg(dpm, &cache->reg_list[0], 0);
563 if (retval != ERROR_OK)
565 cache->reg_list[0].dirty = false;
571 static int arm_dpm_write_dirty_registers_64(struct arm_dpm *dpm)
573 struct arm *arm = dpm->arm;
574 struct reg_cache *cache = arm->core_cache;
577 /* Scan the registers until we find one that's both dirty and
578 * eligible for flushing. Flush that and everything else that
579 * shares the same core mode setting. Typically this won't
580 * actually find anything to do...
583 /* check everything except our scratch register R0 */
584 for (unsigned i = 1; i <= 32; i++) {
588 if (!cache->reg_list[i].dirty)
591 r = cache->reg_list[i].arch_info;
593 retval = dpm_write_reg(dpm,
596 if (retval != ERROR_OK)
600 /* flush R0 -- it's *very* dirty by now */
601 retval = dpm_write_reg(dpm, &cache->reg_list[0], 0);
602 if (retval != ERROR_OK)
604 cache->reg_list[0].dirty = false;
611 * Writes all modified core registers for all processor modes. In normal
612 * operation this is called on exit from halting debug state.
614 * @param dpm: represents the processor
615 * @param bpwp: true ensures breakpoints and watchpoints are set,
616 * false ensures they are cleared
618 int arm_dpm_write_dirty_registers(struct arm_dpm *dpm, bool bpwp)
620 struct arm *arm = dpm->arm;
621 struct reg_cache *cache = arm->core_cache;
624 retval = dpm->prepare(dpm);
625 if (retval != ERROR_OK)
628 /* If we're managing hardware breakpoints for this core, enable
629 * or disable them as requested.
631 * REVISIT We don't yet manage them for ANY cores. Eventually
632 * we should be able to assume we handle them; but until then,
633 * cope with the hand-crafted breakpoint code.
635 if (arm->target->type->add_breakpoint == dpm_add_breakpoint) {
636 for (unsigned i = 0; i < dpm->nbp; i++) {
637 struct dpm_bp *dbp = dpm->dbp + i;
638 struct breakpoint *bp = dbp->bp;
640 retval = dpm_maybe_update_bpwp(dpm, bpwp, &dbp->bpwp,
641 bp ? &bp->set : NULL);
642 if (retval != ERROR_OK)
647 /* enable/disable watchpoints */
648 for (unsigned i = 0; i < dpm->nwp; i++) {
649 struct dpm_wp *dwp = dpm->dwp + i;
650 struct watchpoint *wp = dwp->wp;
652 retval = dpm_maybe_update_bpwp(dpm, bpwp, &dwp->bpwp,
653 wp ? &wp->set : NULL);
654 if (retval != ERROR_OK)
658 /* NOTE: writes to breakpoint and watchpoint registers might
659 * be queued, and need (efficient/batched) flushing later.
662 if (cache->reg_list[0].size == 64)
663 retval = arm_dpm_write_dirty_registers_64(dpm);
665 retval = arm_dpm_write_dirty_registers_32(dpm);
667 /* (void) */ dpm->finish(dpm);
672 /* Returns ARM_MODE_ANY or temporary mode to use while reading the
673 * specified register ... works around flakiness from ARM core calls.
674 * Caller already filtered out SPSR access; mode is never MODE_SYS
677 static enum arm_mode dpm_mapmode(struct arm *arm,
678 unsigned num, enum arm_mode mode)
680 enum arm_mode amode = arm->core_mode;
682 /* don't switch if the mode is already correct */
683 if (amode == ARM_MODE_SYS)
684 amode = ARM_MODE_USR;
689 /* don't switch for non-shadowed registers (r0..r7, r15/pc, cpsr) */
694 /* r8..r12 aren't shadowed for anything except FIQ */
696 if (mode == ARM_MODE_FIQ)
699 /* r13/sp, and r14/lr are always shadowed */
704 LOG_WARNING("invalid register #%u", num);
712 * Standard ARM register accessors ... there are three methods
713 * in "struct arm", to support individual read/write and bulk read
717 static int arm_dpm_read_core_reg(struct target *target, struct reg *r,
718 int regnum, enum arm_mode mode)
720 struct arm_dpm *dpm = target_to_arm(target)->dpm;
723 if (regnum < 0 || regnum > 16)
724 return ERROR_COMMAND_SYNTAX_ERROR;
727 if (mode != ARM_MODE_ANY)
730 mode = dpm_mapmode(dpm->arm, regnum, mode);
732 /* REVISIT what happens if we try to read SPSR in a core mode
733 * which has no such register?
736 retval = dpm->prepare(dpm);
737 if (retval != ERROR_OK)
740 if (mode != ARM_MODE_ANY) {
741 retval = dpm_modeswitch(dpm, mode);
742 if (retval != ERROR_OK)
746 retval = dpm_read_reg(dpm, r, regnum);
747 if (retval != ERROR_OK)
749 /* always clean up, regardless of error */
751 if (mode != ARM_MODE_ANY)
752 /* (void) */ dpm_modeswitch(dpm, ARM_MODE_ANY);
755 /* (void) */ dpm->finish(dpm);
759 static int arm_dpm_write_core_reg(struct target *target, struct reg *r,
760 int regnum, enum arm_mode mode, uint8_t *value)
762 struct arm_dpm *dpm = target_to_arm(target)->dpm;
766 if (regnum < 0 || regnum > 16)
767 return ERROR_COMMAND_SYNTAX_ERROR;
770 if (mode != ARM_MODE_ANY)
773 mode = dpm_mapmode(dpm->arm, regnum, mode);
775 /* REVISIT what happens if we try to write SPSR in a core mode
776 * which has no such register?
779 retval = dpm->prepare(dpm);
780 if (retval != ERROR_OK)
783 if (mode != ARM_MODE_ANY) {
784 retval = dpm_modeswitch(dpm, mode);
785 if (retval != ERROR_OK)
789 retval = dpm_write_reg(dpm, r, regnum);
790 /* always clean up, regardless of error */
792 if (mode != ARM_MODE_ANY)
793 /* (void) */ dpm_modeswitch(dpm, ARM_MODE_ANY);
796 /* (void) */ dpm->finish(dpm);
800 static int arm_dpm_full_context(struct target *target)
802 struct arm *arm = target_to_arm(target);
803 struct arm_dpm *dpm = arm->dpm;
804 struct reg_cache *cache = arm->core_cache;
808 retval = dpm->prepare(dpm);
809 if (retval != ERROR_OK)
813 enum arm_mode mode = ARM_MODE_ANY;
817 /* We "know" arm_dpm_read_current_registers() was called so
818 * the unmapped registers (R0..R7, PC, AND CPSR) and some
819 * view of R8..R14 are current. We also "know" oddities of
820 * register mapping: special cases for R8..R12 and SPSR.
822 * Pick some mode with unread registers and read them all.
825 for (unsigned i = 0; i < cache->num_regs; i++) {
828 if (cache->reg_list[i].valid)
830 r = cache->reg_list[i].arch_info;
832 /* may need to pick a mode and set CPSR */
837 /* For regular (ARM_MODE_ANY) R8..R12
838 * in case we've entered debug state
839 * in FIQ mode we need to patch mode.
841 if (mode != ARM_MODE_ANY)
842 retval = dpm_modeswitch(dpm, mode);
844 retval = dpm_modeswitch(dpm, ARM_MODE_USR);
846 if (retval != ERROR_OK)
852 /* CPSR was read, so "R16" must mean SPSR */
853 retval = dpm_read_reg(dpm,
855 (r->num == 16) ? 17 : r->num);
856 if (retval != ERROR_OK)
862 retval = dpm_modeswitch(dpm, ARM_MODE_ANY);
863 /* (void) */ dpm->finish(dpm);
869 /*----------------------------------------------------------------------*/
872 * Breakpoint and Watchpoint support.
874 * Hardware {break,watch}points are usually left active, to minimize
875 * debug entry/exit costs. When they are set or cleared, it's done in
876 * batches. Also, DPM-conformant hardware can update debug registers
877 * regardless of whether the CPU is running or halted ... though that
878 * fact isn't currently leveraged.
881 static int dpm_bpwp_setup(struct arm_dpm *dpm, struct dpm_bpwp *xp,
882 uint32_t addr, uint32_t length)
886 control = (1 << 0) /* enable */
887 | (3 << 1); /* both user and privileged access */
889 /* Match 1, 2, or all 4 byte addresses in this word.
891 * FIXME: v7 hardware allows lengths up to 2 GB for BP and WP.
892 * Support larger length, when addr is suitably aligned. In
893 * particular, allow watchpoints on 8 byte "double" values.
895 * REVISIT allow watchpoints on unaligned 2-bit values; and on
896 * v7 hardware, unaligned 4-byte ones too.
900 control |= (1 << (addr & 3)) << 5;
903 /* require 2-byte alignment */
905 control |= (3 << (addr & 2)) << 5;
910 /* require 4-byte alignment */
917 LOG_ERROR("unsupported {break,watch}point length/alignment");
918 return ERROR_COMMAND_SYNTAX_ERROR;
921 /* other shared control bits:
922 * bits 15:14 == 0 ... both secure and nonsecure states (v6.1+ only)
923 * bit 20 == 0 ... not linked to a context ID
924 * bit 28:24 == 0 ... not ignoring N LSBs (v7 only)
927 xp->address = addr & ~3;
928 xp->control = control;
931 LOG_DEBUG("BPWP: addr %8.8" PRIx32 ", control %" PRIx32 ", number %d",
932 xp->address, control, xp->number);
934 /* hardware is updated in write_dirty_registers() */
938 static int dpm_add_breakpoint(struct target *target, struct breakpoint *bp)
940 struct arm *arm = target_to_arm(target);
941 struct arm_dpm *dpm = arm->dpm;
942 int retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
945 return ERROR_COMMAND_SYNTAX_ERROR;
946 if (!dpm->bpwp_enable)
949 /* FIXME we need a generic solution for software breakpoints. */
950 if (bp->type == BKPT_SOFT)
951 LOG_DEBUG("using HW bkpt, not SW...");
953 for (unsigned i = 0; i < dpm->nbp; i++) {
954 if (!dpm->dbp[i].bp) {
955 retval = dpm_bpwp_setup(dpm, &dpm->dbp[i].bpwp,
956 bp->address, bp->length);
957 if (retval == ERROR_OK)
966 static int dpm_remove_breakpoint(struct target *target, struct breakpoint *bp)
968 struct arm *arm = target_to_arm(target);
969 struct arm_dpm *dpm = arm->dpm;
970 int retval = ERROR_COMMAND_SYNTAX_ERROR;
972 for (unsigned i = 0; i < dpm->nbp; i++) {
973 if (dpm->dbp[i].bp == bp) {
974 dpm->dbp[i].bp = NULL;
975 dpm->dbp[i].bpwp.dirty = true;
977 /* hardware is updated in write_dirty_registers() */
986 static int dpm_watchpoint_setup(struct arm_dpm *dpm, unsigned index_t,
987 struct watchpoint *wp)
990 struct dpm_wp *dwp = dpm->dwp + index_t;
993 /* this hardware doesn't support data value matching or masking */
994 if (wp->value || wp->mask != ~(uint32_t)0) {
995 LOG_DEBUG("watchpoint values and masking not supported");
996 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
999 retval = dpm_bpwp_setup(dpm, &dwp->bpwp, wp->address, wp->length);
1000 if (retval != ERROR_OK)
1003 control = dwp->bpwp.control;
1015 dwp->bpwp.control = control;
1017 dpm->dwp[index_t].wp = wp;
1022 static int dpm_add_watchpoint(struct target *target, struct watchpoint *wp)
1024 struct arm *arm = target_to_arm(target);
1025 struct arm_dpm *dpm = arm->dpm;
1026 int retval = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1028 if (dpm->bpwp_enable) {
1029 for (unsigned i = 0; i < dpm->nwp; i++) {
1030 if (!dpm->dwp[i].wp) {
1031 retval = dpm_watchpoint_setup(dpm, i, wp);
1040 static int dpm_remove_watchpoint(struct target *target, struct watchpoint *wp)
1042 struct arm *arm = target_to_arm(target);
1043 struct arm_dpm *dpm = arm->dpm;
1044 int retval = ERROR_COMMAND_SYNTAX_ERROR;
1046 for (unsigned i = 0; i < dpm->nwp; i++) {
1047 if (dpm->dwp[i].wp == wp) {
1048 dpm->dwp[i].wp = NULL;
1049 dpm->dwp[i].bpwp.dirty = true;
1051 /* hardware is updated in write_dirty_registers() */
1060 void arm_dpm_report_wfar(struct arm_dpm *dpm, uint32_t addr)
1062 switch (dpm->arm->core_state) {
1066 case ARM_STATE_THUMB:
1067 case ARM_STATE_THUMB_EE:
1070 case ARM_STATE_JAZELLE:
1071 case ARM_STATE_AARCH64:
1078 /*----------------------------------------------------------------------*/
1081 * Other debug and support utilities
1084 void arm_dpm_report_dscr(struct arm_dpm *dpm, uint32_t dscr)
1086 struct target *target = dpm->arm->target;
1090 /* Examine debug reason */
1091 switch (DSCR_ENTRY(dscr)) {
1092 case 6: /* Data abort (v6 only) */
1093 case 7: /* Prefetch abort (v6 only) */
1094 /* FALL THROUGH -- assume a v6 core in abort mode */
1095 case 0: /* HALT request from debugger */
1096 case 4: /* EDBGRQ */
1097 target->debug_reason = DBG_REASON_DBGRQ;
1099 case 1: /* HW breakpoint */
1100 case 3: /* SW BKPT */
1101 case 5: /* vector catch */
1102 target->debug_reason = DBG_REASON_BREAKPOINT;
1104 case 2: /* asynch watchpoint */
1105 case 10:/* precise watchpoint */
1106 target->debug_reason = DBG_REASON_WATCHPOINT;
1109 target->debug_reason = DBG_REASON_UNDEFINED;
1114 /*----------------------------------------------------------------------*/
1117 * Setup and management support.
1121 * Hooks up this DPM to its associated target; call only once.
1122 * Initially this only covers the register cache.
1124 * Oh, and watchpoints. Yeah.
1126 int arm_dpm_setup(struct arm_dpm *dpm)
1128 struct arm *arm = dpm->arm;
1129 struct target *target = arm->target;
1130 struct reg_cache *cache = 0;
1134 /* register access setup */
1135 arm->full_context = arm_dpm_full_context;
1136 arm->read_core_reg = arm->read_core_reg ? : arm_dpm_read_core_reg;
1137 arm->write_core_reg = arm->write_core_reg ? : arm_dpm_write_core_reg;
1139 if (arm->core_cache != NULL) {
1140 if (arm->core_state == ARM_STATE_AARCH64) {
1141 cache = armv8_build_reg_cache(target);
1142 target->reg_cache = cache;
1144 cache = arm_build_reg_cache(target, arm);
1145 *register_get_last_cache_p(&target->reg_cache) = cache;
1152 /* coprocessor access setup */
1156 /* breakpoint setup -- optional until it works everywhere */
1157 if (!target->type->add_breakpoint) {
1158 target->type->add_breakpoint = dpm_add_breakpoint;
1159 target->type->remove_breakpoint = dpm_remove_breakpoint;
1162 /* watchpoint setup */
1163 target->type->add_watchpoint = dpm_add_watchpoint;
1164 target->type->remove_watchpoint = dpm_remove_watchpoint;
1167 if (dpm->arm_reg_current == 0)
1168 dpm->arm_reg_current = arm_reg_current;
1170 /* FIXME add vector catch support */
1172 if (arm->core_state == ARM_STATE_AARCH64) {
1173 dpm->nbp = 1 + ((dpm->didr >> 24) & 0xf);
1174 dpm->nwp = 1 + ((dpm->didr >> 28) & 0xf);
1176 dpm->nbp = 1 + ((dpm->didr >> 12) & 0xf);
1177 dpm->nwp = 1 + ((dpm->didr >> 20) & 0xf);
1180 dpm->dbp = calloc(dpm->nbp, sizeof *dpm->dbp);
1181 dpm->dwp = calloc(dpm->nwp, sizeof *dpm->dwp);
1183 if (!dpm->dbp || !dpm->dwp) {
1189 LOG_INFO("%s: hardware has %d breakpoints, %d watchpoints",
1190 target_name(target), dpm->nbp, dpm->nwp);
1192 /* REVISIT ... and some of those breakpoints could match
1193 * execution context IDs...
1200 * Reinitializes DPM state at the beginning of a new debug session
1201 * or after a reset which may have affected the debug module.
1203 int arm_dpm_initialize(struct arm_dpm *dpm)
1205 /* Disable all breakpoints and watchpoints at startup. */
1206 if (dpm->bpwp_disable) {
1209 for (i = 0; i < dpm->nbp; i++) {
1210 dpm->dbp[i].bpwp.number = i;
1211 (void) dpm->bpwp_disable(dpm, i);
1213 for (i = 0; i < dpm->nwp; i++) {
1214 dpm->dwp[i].bpwp.number = 16 + i;
1215 (void) dpm->bpwp_disable(dpm, 16 + i);
1218 LOG_WARNING("%s: can't disable breakpoints and watchpoints",
1219 target_name(dpm->arm->target));