1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2006 by Magnus Lundin *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
27 * Cortex-M3(tm) TRM, ARM DDI 0337E (r1p1) and 0337G (r2p0) *
29 ***************************************************************************/
34 #include "breakpoints.h"
35 #include "cortex_m3.h"
36 #include "target_request.h"
37 #include "target_type.h"
38 #include "arm_disassembler.h"
40 #include "arm_opcodes.h"
43 /* NOTE: most of this should work fine for the Cortex-M1 and
44 * Cortex-M0 cores too, although they're ARMv6-M not ARMv7-M.
48 /* forward declarations */
49 static int cortex_m3_set_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
);
50 static int cortex_m3_unset_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
);
51 static void cortex_m3_enable_watchpoints(struct target
*target
);
52 static int cortex_m3_store_core_reg_u32(struct target
*target
,
53 enum armv7m_regtype type
, uint32_t num
, uint32_t value
);
55 #ifdef ARMV7_GDB_HACKS
56 extern uint8_t armv7m_gdb_dummy_cpsr_value
[];
57 extern struct reg armv7m_gdb_dummy_cpsr_reg
;
60 static int cortexm3_dap_read_coreregister_u32(struct swjdp_common
*swjdp
,
61 uint32_t *value
, int regnum
)
66 /* because the DCB_DCRDR is used for the emulated dcc channel
67 * we have to save/restore the DCB_DCRDR when used */
69 mem_ap_read_u32(swjdp
, DCB_DCRDR
, &dcrdr
);
71 swjdp
->trans_mode
= TRANS_MODE_COMPOSITE
;
73 /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
74 dap_setup_accessport(swjdp
, CSW_32BIT
| CSW_ADDRINC_OFF
, DCB_DCRSR
& 0xFFFFFFF0);
75 dap_ap_write_reg_u32(swjdp
, AP_REG_BD0
| (DCB_DCRSR
& 0xC), regnum
);
77 /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
78 dap_setup_accessport(swjdp
, CSW_32BIT
| CSW_ADDRINC_OFF
, DCB_DCRDR
& 0xFFFFFFF0);
79 dap_ap_read_reg_u32(swjdp
, AP_REG_BD0
| (DCB_DCRDR
& 0xC), value
);
81 retval
= swjdp_transaction_endcheck(swjdp
);
83 /* restore DCB_DCRDR - this needs to be in a seperate
84 * transaction otherwise the emulated DCC channel breaks */
85 if (retval
== ERROR_OK
)
86 retval
= mem_ap_write_atomic_u32(swjdp
, DCB_DCRDR
, dcrdr
);
91 static int cortexm3_dap_write_coreregister_u32(struct swjdp_common
*swjdp
,
92 uint32_t value
, int regnum
)
97 /* because the DCB_DCRDR is used for the emulated dcc channel
98 * we have to save/restore the DCB_DCRDR when used */
100 mem_ap_read_u32(swjdp
, DCB_DCRDR
, &dcrdr
);
102 swjdp
->trans_mode
= TRANS_MODE_COMPOSITE
;
104 /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
105 dap_setup_accessport(swjdp
, CSW_32BIT
| CSW_ADDRINC_OFF
, DCB_DCRDR
& 0xFFFFFFF0);
106 dap_ap_write_reg_u32(swjdp
, AP_REG_BD0
| (DCB_DCRDR
& 0xC), value
);
108 /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR); */
109 dap_setup_accessport(swjdp
, CSW_32BIT
| CSW_ADDRINC_OFF
, DCB_DCRSR
& 0xFFFFFFF0);
110 dap_ap_write_reg_u32(swjdp
, AP_REG_BD0
| (DCB_DCRSR
& 0xC), regnum
| DCRSR_WnR
);
112 retval
= swjdp_transaction_endcheck(swjdp
);
114 /* restore DCB_DCRDR - this needs to be in a seperate
115 * transaction otherwise the emulated DCC channel breaks */
116 if (retval
== ERROR_OK
)
117 retval
= mem_ap_write_atomic_u32(swjdp
, DCB_DCRDR
, dcrdr
);
122 static int cortex_m3_write_debug_halt_mask(struct target
*target
,
123 uint32_t mask_on
, uint32_t mask_off
)
125 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
126 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
128 /* mask off status bits */
129 cortex_m3
->dcb_dhcsr
&= ~((0xFFFF << 16) | mask_off
);
130 /* create new register mask */
131 cortex_m3
->dcb_dhcsr
|= DBGKEY
| C_DEBUGEN
| mask_on
;
133 return mem_ap_write_atomic_u32(swjdp
, DCB_DHCSR
, cortex_m3
->dcb_dhcsr
);
136 static int cortex_m3_clear_halt(struct target
*target
)
138 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
139 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
141 /* clear step if any */
142 cortex_m3_write_debug_halt_mask(target
, C_HALT
, C_STEP
);
144 /* Read Debug Fault Status Register */
145 mem_ap_read_atomic_u32(swjdp
, NVIC_DFSR
, &cortex_m3
->nvic_dfsr
);
146 /* Clear Debug Fault Status */
147 mem_ap_write_atomic_u32(swjdp
, NVIC_DFSR
, cortex_m3
->nvic_dfsr
);
148 LOG_DEBUG(" NVIC_DFSR 0x%" PRIx32
"", cortex_m3
->nvic_dfsr
);
153 static int cortex_m3_single_step_core(struct target
*target
)
155 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
156 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
159 /* backup dhcsr reg */
160 dhcsr_save
= cortex_m3
->dcb_dhcsr
;
162 /* mask interrupts if not done already */
163 if (!(cortex_m3
->dcb_dhcsr
& C_MASKINTS
))
164 mem_ap_write_atomic_u32(swjdp
, DCB_DHCSR
, DBGKEY
| C_MASKINTS
| C_HALT
| C_DEBUGEN
);
165 mem_ap_write_atomic_u32(swjdp
, DCB_DHCSR
, DBGKEY
| C_MASKINTS
| C_STEP
| C_DEBUGEN
);
168 /* restore dhcsr reg */
169 cortex_m3
->dcb_dhcsr
= dhcsr_save
;
170 cortex_m3_clear_halt(target
);
175 static int cortex_m3_endreset_event(struct target
*target
)
179 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
180 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
181 struct cortex_m3_fp_comparator
*fp_list
= cortex_m3
->fp_comparator_list
;
182 struct cortex_m3_dwt_comparator
*dwt_list
= cortex_m3
->dwt_comparator_list
;
184 mem_ap_read_atomic_u32(swjdp
, DCB_DEMCR
, &dcb_demcr
);
185 LOG_DEBUG("DCB_DEMCR = 0x%8.8" PRIx32
"",dcb_demcr
);
187 /* this regsiter is used for emulated dcc channel */
188 mem_ap_write_u32(swjdp
, DCB_DCRDR
, 0);
190 /* Enable debug requests */
191 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
192 if (!(cortex_m3
->dcb_dhcsr
& C_DEBUGEN
))
193 mem_ap_write_u32(swjdp
, DCB_DHCSR
, DBGKEY
| C_DEBUGEN
);
195 /* clear any interrupt masking */
196 cortex_m3_write_debug_halt_mask(target
, 0, C_MASKINTS
);
198 /* Enable trace and dwt */
199 mem_ap_write_u32(swjdp
, DCB_DEMCR
, TRCENA
| VC_HARDERR
| VC_BUSERR
);
200 /* Monitor bus faults */
201 mem_ap_write_u32(swjdp
, NVIC_SHCSR
, SHCSR_BUSFAULTENA
);
204 target_write_u32(target
, FP_CTRL
, 3);
205 cortex_m3
->fpb_enabled
= 1;
207 /* Restore FPB registers */
208 for (i
= 0; i
< cortex_m3
->fp_num_code
+ cortex_m3
->fp_num_lit
; i
++)
210 target_write_u32(target
, fp_list
[i
].fpcr_address
, fp_list
[i
].fpcr_value
);
213 /* Restore DWT registers */
214 for (i
= 0; i
< cortex_m3
->dwt_num_comp
; i
++)
216 target_write_u32(target
, dwt_list
[i
].dwt_comparator_address
+ 0,
218 target_write_u32(target
, dwt_list
[i
].dwt_comparator_address
+ 4,
220 target_write_u32(target
, dwt_list
[i
].dwt_comparator_address
+ 8,
221 dwt_list
[i
].function
);
223 swjdp_transaction_endcheck(swjdp
);
225 register_cache_invalidate(cortex_m3
->armv7m
.core_cache
);
227 /* make sure we have latest dhcsr flags */
228 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
233 static int cortex_m3_examine_debug_reason(struct target
*target
)
235 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
237 /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
238 /* only check the debug reason if we don't know it already */
240 if ((target
->debug_reason
!= DBG_REASON_DBGRQ
)
241 && (target
->debug_reason
!= DBG_REASON_SINGLESTEP
))
243 if (cortex_m3
->nvic_dfsr
& DFSR_BKPT
)
245 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
246 if (cortex_m3
->nvic_dfsr
& DFSR_DWTTRAP
)
247 target
->debug_reason
= DBG_REASON_WPTANDBKPT
;
249 else if (cortex_m3
->nvic_dfsr
& DFSR_DWTTRAP
)
250 target
->debug_reason
= DBG_REASON_WATCHPOINT
;
251 else if (cortex_m3
->nvic_dfsr
& DFSR_VCATCH
)
252 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
253 else /* EXTERNAL, HALTED */
254 target
->debug_reason
= DBG_REASON_UNDEFINED
;
260 static int cortex_m3_examine_exception_reason(struct target
*target
)
262 uint32_t shcsr
, except_sr
, cfsr
= -1, except_ar
= -1;
263 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
264 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
266 mem_ap_read_u32(swjdp
, NVIC_SHCSR
, &shcsr
);
267 switch (armv7m
->exception_number
)
271 case 3: /* Hard Fault */
272 mem_ap_read_atomic_u32(swjdp
, NVIC_HFSR
, &except_sr
);
273 if (except_sr
& 0x40000000)
275 mem_ap_read_u32(swjdp
, NVIC_CFSR
, &cfsr
);
278 case 4: /* Memory Management */
279 mem_ap_read_u32(swjdp
, NVIC_CFSR
, &except_sr
);
280 mem_ap_read_u32(swjdp
, NVIC_MMFAR
, &except_ar
);
282 case 5: /* Bus Fault */
283 mem_ap_read_u32(swjdp
, NVIC_CFSR
, &except_sr
);
284 mem_ap_read_u32(swjdp
, NVIC_BFAR
, &except_ar
);
286 case 6: /* Usage Fault */
287 mem_ap_read_u32(swjdp
, NVIC_CFSR
, &except_sr
);
289 case 11: /* SVCall */
291 case 12: /* Debug Monitor */
292 mem_ap_read_u32(swjdp
, NVIC_DFSR
, &except_sr
);
294 case 14: /* PendSV */
296 case 15: /* SysTick */
302 swjdp_transaction_endcheck(swjdp
);
303 LOG_DEBUG("%s SHCSR 0x%" PRIx32
", SR 0x%" PRIx32
", CFSR 0x%" PRIx32
", AR 0x%" PRIx32
"", armv7m_exception_string(armv7m
->exception_number
), \
304 shcsr
, except_sr
, cfsr
, except_ar
);
308 static int cortex_m3_debug_entry(struct target
*target
)
313 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
314 struct armv7m_common
*armv7m
= &cortex_m3
->armv7m
;
315 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
319 cortex_m3_clear_halt(target
);
320 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
322 if ((retval
= armv7m
->examine_debug_reason(target
)) != ERROR_OK
)
325 /* Examine target state and mode */
326 /* First load register acessible through core debug port*/
327 int num_regs
= armv7m
->core_cache
->num_regs
;
329 for (i
= 0; i
< num_regs
; i
++)
331 if (!armv7m
->core_cache
->reg_list
[i
].valid
)
332 armv7m
->read_core_reg(target
, i
);
335 xPSR
= buf_get_u32(armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].value
, 0, 32);
337 #ifdef ARMV7_GDB_HACKS
338 /* FIXME this breaks on scan chains with more than one Cortex-M3.
339 * Instead, each CM3 should have its own dummy value...
341 /* copy real xpsr reg for gdb, setting thumb bit */
342 buf_set_u32(armv7m_gdb_dummy_cpsr_value
, 0, 32, xPSR
);
343 buf_set_u32(armv7m_gdb_dummy_cpsr_value
, 5, 1, 1);
344 armv7m_gdb_dummy_cpsr_reg
.valid
= armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].valid
;
345 armv7m_gdb_dummy_cpsr_reg
.dirty
= armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].dirty
;
348 /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
351 armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].dirty
= armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].valid
;
352 cortex_m3_store_core_reg_u32(target
, ARMV7M_REGISTER_CORE_GP
, 16, xPSR
&~ 0xff);
355 /* Are we in an exception handler */
358 armv7m
->core_mode
= ARMV7M_MODE_HANDLER
;
359 armv7m
->exception_number
= (xPSR
& 0x1FF);
363 armv7m
->core_mode
= buf_get_u32(armv7m
->core_cache
->reg_list
[ARMV7M_CONTROL
].value
, 0, 1);
364 armv7m
->exception_number
= 0;
367 if (armv7m
->exception_number
)
369 cortex_m3_examine_exception_reason(target
);
372 LOG_DEBUG("entered debug state in core mode: %s at PC 0x%" PRIx32
", target->state: %s",
373 armv7m_mode_strings
[armv7m
->core_mode
],
374 *(uint32_t*)(armv7m
->core_cache
->reg_list
[15].value
),
375 target_state_name(target
));
377 if (armv7m
->post_debug_entry
)
378 armv7m
->post_debug_entry(target
);
383 static int cortex_m3_poll(struct target
*target
)
386 enum target_state prev_target_state
= target
->state
;
387 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
388 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
390 /* Read from Debug Halting Control and Status Register */
391 retval
= mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
392 if (retval
!= ERROR_OK
)
394 target
->state
= TARGET_UNKNOWN
;
398 if (cortex_m3
->dcb_dhcsr
& S_RESET_ST
)
400 /* check if still in reset */
401 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
403 if (cortex_m3
->dcb_dhcsr
& S_RESET_ST
)
405 target
->state
= TARGET_RESET
;
410 if (target
->state
== TARGET_RESET
)
412 /* Cannot switch context while running so endreset is called with target->state == TARGET_RESET */
413 LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%" PRIx32
"", cortex_m3
->dcb_dhcsr
);
414 cortex_m3_endreset_event(target
);
415 target
->state
= TARGET_RUNNING
;
416 prev_target_state
= TARGET_RUNNING
;
419 if (cortex_m3
->dcb_dhcsr
& S_HALT
)
421 target
->state
= TARGET_HALTED
;
423 if ((prev_target_state
== TARGET_RUNNING
) || (prev_target_state
== TARGET_RESET
))
425 if ((retval
= cortex_m3_debug_entry(target
)) != ERROR_OK
)
428 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
430 if (prev_target_state
== TARGET_DEBUG_RUNNING
)
433 if ((retval
= cortex_m3_debug_entry(target
)) != ERROR_OK
)
436 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_HALTED
);
440 /* REVISIT when S_SLEEP is set, it's in a Sleep or DeepSleep state.
441 * How best to model low power modes?
444 if (target
->state
== TARGET_UNKNOWN
)
446 /* check if processor is retiring instructions */
447 if (cortex_m3
->dcb_dhcsr
& S_RETIRE_ST
)
449 target
->state
= TARGET_RUNNING
;
457 static int cortex_m3_halt(struct target
*target
)
459 LOG_DEBUG("target->state: %s",
460 target_state_name(target
));
462 if (target
->state
== TARGET_HALTED
)
464 LOG_DEBUG("target was already halted");
468 if (target
->state
== TARGET_UNKNOWN
)
470 LOG_WARNING("target was in unknown state when halt was requested");
473 if (target
->state
== TARGET_RESET
)
475 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST
) && jtag_get_srst())
477 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
478 return ERROR_TARGET_FAILURE
;
482 /* we came here in a reset_halt or reset_init sequence
483 * debug entry was already prepared in cortex_m3_prepare_reset_halt()
485 target
->debug_reason
= DBG_REASON_DBGRQ
;
491 /* Write to Debug Halting Control and Status Register */
492 cortex_m3_write_debug_halt_mask(target
, C_HALT
, 0);
494 target
->debug_reason
= DBG_REASON_DBGRQ
;
499 static int cortex_m3_soft_reset_halt(struct target
*target
)
501 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
502 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
503 uint32_t dcb_dhcsr
= 0;
504 int retval
, timeout
= 0;
506 /* Enter debug state on reset, cf. end_reset_event() */
507 mem_ap_write_u32(swjdp
, DCB_DEMCR
, TRCENA
| VC_HARDERR
| VC_BUSERR
| VC_CORERESET
);
509 /* Request a reset */
510 mem_ap_write_atomic_u32(swjdp
, NVIC_AIRCR
, AIRCR_VECTKEY
| AIRCR_VECTRESET
);
511 target
->state
= TARGET_RESET
;
513 /* registers are now invalid */
514 register_cache_invalidate(cortex_m3
->armv7m
.core_cache
);
516 while (timeout
< 100)
518 retval
= mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &dcb_dhcsr
);
519 if (retval
== ERROR_OK
)
521 mem_ap_read_atomic_u32(swjdp
, NVIC_DFSR
, &cortex_m3
->nvic_dfsr
);
522 if ((dcb_dhcsr
& S_HALT
) && (cortex_m3
->nvic_dfsr
& DFSR_VCATCH
))
524 LOG_DEBUG("system reset-halted, dcb_dhcsr 0x%" PRIx32
", nvic_dfsr 0x%" PRIx32
"", dcb_dhcsr
, cortex_m3
->nvic_dfsr
);
525 cortex_m3_poll(target
);
529 LOG_DEBUG("waiting for system reset-halt, dcb_dhcsr 0x%" PRIx32
", %i ms", dcb_dhcsr
, timeout
);
538 static void cortex_m3_enable_breakpoints(struct target
*target
)
540 struct breakpoint
*breakpoint
= target
->breakpoints
;
542 /* set any pending breakpoints */
545 if (breakpoint
->set
== 0)
546 cortex_m3_set_breakpoint(target
, breakpoint
);
547 breakpoint
= breakpoint
->next
;
551 static int cortex_m3_resume(struct target
*target
, int current
,
552 uint32_t address
, int handle_breakpoints
, int debug_execution
)
554 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
555 struct breakpoint
*breakpoint
= NULL
;
558 if (target
->state
!= TARGET_HALTED
)
560 LOG_WARNING("target not halted");
561 return ERROR_TARGET_NOT_HALTED
;
564 if (!debug_execution
)
566 target_free_all_working_areas(target
);
567 cortex_m3_enable_breakpoints(target
);
568 cortex_m3_enable_watchpoints(target
);
573 /* Disable interrupts */
574 /* We disable interrupts in the PRIMASK register instead of masking with C_MASKINTS,
575 * This is probably the same issue as Cortex-M3 Errata 377493:
576 * C_MASKINTS in parallel with disabled interrupts can cause local faults to not be taken. */
577 buf_set_u32(armv7m
->core_cache
->reg_list
[ARMV7M_PRIMASK
].value
, 0, 32, 1);
578 armv7m
->core_cache
->reg_list
[ARMV7M_PRIMASK
].dirty
= 1;
579 armv7m
->core_cache
->reg_list
[ARMV7M_PRIMASK
].valid
= 1;
581 /* Make sure we are in Thumb mode */
582 buf_set_u32(armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].value
, 0, 32,
583 buf_get_u32(armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].value
, 0, 32) | (1 << 24));
584 armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].dirty
= 1;
585 armv7m
->core_cache
->reg_list
[ARMV7M_xPSR
].valid
= 1;
588 /* current = 1: continue on current pc, otherwise continue at <address> */
591 buf_set_u32(armv7m
->core_cache
->reg_list
[15].value
, 0, 32, address
);
592 armv7m
->core_cache
->reg_list
[15].dirty
= 1;
593 armv7m
->core_cache
->reg_list
[15].valid
= 1;
596 resume_pc
= buf_get_u32(armv7m
->core_cache
->reg_list
[15].value
, 0, 32);
598 armv7m_restore_context(target
);
600 /* the front-end may request us not to handle breakpoints */
601 if (handle_breakpoints
)
603 /* Single step past breakpoint at current address */
604 if ((breakpoint
= breakpoint_find(target
, resume_pc
)))
606 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32
" (ID: %d)",
608 breakpoint
->unique_id
);
609 cortex_m3_unset_breakpoint(target
, breakpoint
);
610 cortex_m3_single_step_core(target
);
611 cortex_m3_set_breakpoint(target
, breakpoint
);
616 cortex_m3_write_debug_halt_mask(target
, 0, C_HALT
);
618 target
->debug_reason
= DBG_REASON_NOTHALTED
;
620 /* registers are now invalid */
621 register_cache_invalidate(armv7m
->core_cache
);
623 if (!debug_execution
)
625 target
->state
= TARGET_RUNNING
;
626 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
627 LOG_DEBUG("target resumed at 0x%" PRIx32
"", resume_pc
);
631 target
->state
= TARGET_DEBUG_RUNNING
;
632 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_RESUMED
);
633 LOG_DEBUG("target debug resumed at 0x%" PRIx32
"", resume_pc
);
639 /* int irqstepcount = 0; */
640 static int cortex_m3_step(struct target
*target
, int current
,
641 uint32_t address
, int handle_breakpoints
)
643 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
644 struct armv7m_common
*armv7m
= &cortex_m3
->armv7m
;
645 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
646 struct breakpoint
*breakpoint
= NULL
;
648 if (target
->state
!= TARGET_HALTED
)
650 LOG_WARNING("target not halted");
651 return ERROR_TARGET_NOT_HALTED
;
654 /* current = 1: continue on current pc, otherwise continue at <address> */
656 buf_set_u32(cortex_m3
->armv7m
.core_cache
->reg_list
[15].value
,
659 /* the front-end may request us not to handle breakpoints */
660 if (handle_breakpoints
) {
661 breakpoint
= breakpoint_find(target
, buf_get_u32(armv7m
662 ->core_cache
->reg_list
[15].value
, 0, 32));
664 cortex_m3_unset_breakpoint(target
, breakpoint
);
667 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
669 armv7m_restore_context(target
);
671 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
673 /* set step and clear halt */
674 cortex_m3_write_debug_halt_mask(target
, C_STEP
, C_HALT
);
675 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
677 /* registers are now invalid */
678 register_cache_invalidate(cortex_m3
->armv7m
.core_cache
);
681 cortex_m3_set_breakpoint(target
, breakpoint
);
683 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
" nvic_icsr = 0x%" PRIx32
"", cortex_m3
->dcb_dhcsr
, cortex_m3
->nvic_icsr
);
685 cortex_m3_debug_entry(target
);
686 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
688 LOG_DEBUG("target stepped dcb_dhcsr = 0x%" PRIx32
" nvic_icsr = 0x%" PRIx32
"", cortex_m3
->dcb_dhcsr
, cortex_m3
->nvic_icsr
);
692 static int cortex_m3_assert_reset(struct target
*target
)
694 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
695 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
698 LOG_DEBUG("target->state: %s",
699 target_state_name(target
));
701 enum reset_types jtag_reset_config
= jtag_get_reset_config();
704 * We can reset Cortex-M3 targets using just the NVIC without
705 * requiring SRST, getting a SoC reset (or a core-only reset)
706 * instead of a system reset.
708 if (!(jtag_reset_config
& RESET_HAS_SRST
))
711 /* Enable debug requests */
712 mem_ap_read_atomic_u32(swjdp
, DCB_DHCSR
, &cortex_m3
->dcb_dhcsr
);
713 if (!(cortex_m3
->dcb_dhcsr
& C_DEBUGEN
))
714 mem_ap_write_u32(swjdp
, DCB_DHCSR
, DBGKEY
| C_DEBUGEN
);
716 mem_ap_write_u32(swjdp
, DCB_DCRDR
, 0);
718 if (!target
->reset_halt
)
720 /* Set/Clear C_MASKINTS in a separate operation */
721 if (cortex_m3
->dcb_dhcsr
& C_MASKINTS
)
722 mem_ap_write_atomic_u32(swjdp
, DCB_DHCSR
, DBGKEY
| C_DEBUGEN
| C_HALT
);
724 /* clear any debug flags before resuming */
725 cortex_m3_clear_halt(target
);
727 /* clear C_HALT in dhcsr reg */
728 cortex_m3_write_debug_halt_mask(target
, 0, C_HALT
);
730 /* Enter debug state on reset, cf. end_reset_event() */
731 mem_ap_write_u32(swjdp
, DCB_DEMCR
, TRCENA
| VC_HARDERR
| VC_BUSERR
);
735 /* Enter debug state on reset, cf. end_reset_event() */
736 mem_ap_write_atomic_u32(swjdp
, DCB_DEMCR
, TRCENA
| VC_HARDERR
| VC_BUSERR
| VC_CORERESET
);
740 * When nRST is asserted on most Stellaris devices, it clears some of
741 * the debug state. The ARMv7M and Cortex-M3 TRMs say that's wrong;
742 * and OpenOCD depends on those TRMs. So we won't use SRST on those
743 * chips. (Only power-on reset should affect debug state, beyond a
744 * few specified bits; not the chip's nRST input, wired to SRST.)
746 * REVISIT current errata specs don't seem to cover this issue.
747 * Do we have more details than this email?
748 * https://lists.berlios.de/pipermail
749 * /openocd-development/2008-August/003065.html
751 if (strcmp(target
->variant
, "lm3s") == 0)
753 /* Check for silicon revisions with the issue. */
756 if (target_read_u32(target
, 0x400fe000, &did0
) == ERROR_OK
)
758 switch ((did0
>> 16) & 0xff)
761 /* all Sandstorm suffer issue */
767 /* Fury and DustDevil rev A have
768 * this nRST problem. It should
769 * be fixed in rev B silicon.
771 if (((did0
>> 8) & 0xff) == 0)
775 /* Tempest should be fine. */
783 /* default to asserting srst */
784 if (jtag_reset_config
& RESET_SRST_PULLS_TRST
)
786 jtag_add_reset(1, 1);
790 jtag_add_reset(0, 1);
795 /* Use a standard Cortex-M3 software reset mechanism.
796 * SYSRESETREQ will reset SoC peripherals outside the
797 * core, like watchdog timers, if the SoC wires it up
798 * correctly. Else VECRESET can reset just the core.
800 mem_ap_write_atomic_u32(swjdp
, NVIC_AIRCR
,
801 AIRCR_VECTKEY
| AIRCR_SYSRESETREQ
);
802 LOG_DEBUG("Using Cortex-M3 SYSRESETREQ");
805 /* I do not know why this is necessary, but it
806 * fixes strange effects (step/resume cause NMI
807 * after reset) on LM3S6918 -- Michael Schwingen
810 mem_ap_read_atomic_u32(swjdp
, NVIC_AIRCR
, &tmp
);
814 target
->state
= TARGET_RESET
;
815 jtag_add_sleep(50000);
817 register_cache_invalidate(cortex_m3
->armv7m
.core_cache
);
819 if (target
->reset_halt
)
822 if ((retval
= target_halt(target
)) != ERROR_OK
)
829 static int cortex_m3_deassert_reset(struct target
*target
)
831 LOG_DEBUG("target->state: %s",
832 target_state_name(target
));
834 /* deassert reset lines */
835 jtag_add_reset(0, 0);
841 cortex_m3_set_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
846 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
847 struct cortex_m3_fp_comparator
*comparator_list
= cortex_m3
->fp_comparator_list
;
851 LOG_WARNING("breakpoint (BPID: %d) already set", breakpoint
->unique_id
);
855 if (cortex_m3
->auto_bp_type
)
857 breakpoint
->type
= (breakpoint
->address
< 0x20000000) ? BKPT_HARD
: BKPT_SOFT
;
860 if (breakpoint
->type
== BKPT_HARD
)
862 while (comparator_list
[fp_num
].used
&& (fp_num
< cortex_m3
->fp_num_code
))
864 if (fp_num
>= cortex_m3
->fp_num_code
)
866 LOG_ERROR("Can not find free FPB Comparator!");
869 breakpoint
->set
= fp_num
+ 1;
870 hilo
= (breakpoint
->address
& 0x2) ? FPCR_REPLACE_BKPT_HIGH
: FPCR_REPLACE_BKPT_LOW
;
871 comparator_list
[fp_num
].used
= 1;
872 comparator_list
[fp_num
].fpcr_value
= (breakpoint
->address
& 0x1FFFFFFC) | hilo
| 1;
873 target_write_u32(target
, comparator_list
[fp_num
].fpcr_address
, comparator_list
[fp_num
].fpcr_value
);
874 LOG_DEBUG("fpc_num %i fpcr_value 0x%" PRIx32
"", fp_num
, comparator_list
[fp_num
].fpcr_value
);
875 if (!cortex_m3
->fpb_enabled
)
877 LOG_DEBUG("FPB wasn't enabled, do it now");
878 target_write_u32(target
, FP_CTRL
, 3);
881 else if (breakpoint
->type
== BKPT_SOFT
)
884 buf_set_u32(code
, 0, 32, ARMV5_T_BKPT(0x11));
885 if ((retval
= target_read_memory(target
, breakpoint
->address
& 0xFFFFFFFE, breakpoint
->length
, 1, breakpoint
->orig_instr
)) != ERROR_OK
)
889 if ((retval
= target_write_memory(target
, breakpoint
->address
& 0xFFFFFFFE, breakpoint
->length
, 1, code
)) != ERROR_OK
)
893 breakpoint
->set
= 0x11; /* Any nice value but 0 */
896 LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32
" Length: %d (set=%d)",
897 breakpoint
->unique_id
,
898 (int)(breakpoint
->type
),
907 cortex_m3_unset_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
910 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
911 struct cortex_m3_fp_comparator
* comparator_list
= cortex_m3
->fp_comparator_list
;
913 if (!breakpoint
->set
)
915 LOG_WARNING("breakpoint not set");
919 LOG_DEBUG("BPID: %d, Type: %d, Address: 0x%08" PRIx32
" Length: %d (set=%d)",
920 breakpoint
->unique_id
,
921 (int)(breakpoint
->type
),
926 if (breakpoint
->type
== BKPT_HARD
)
928 int fp_num
= breakpoint
->set
- 1;
929 if ((fp_num
< 0) || (fp_num
>= cortex_m3
->fp_num_code
))
931 LOG_DEBUG("Invalid FP Comparator number in breakpoint");
934 comparator_list
[fp_num
].used
= 0;
935 comparator_list
[fp_num
].fpcr_value
= 0;
936 target_write_u32(target
, comparator_list
[fp_num
].fpcr_address
, comparator_list
[fp_num
].fpcr_value
);
940 /* restore original instruction (kept in target endianness) */
941 if (breakpoint
->length
== 4)
943 if ((retval
= target_write_memory(target
, breakpoint
->address
& 0xFFFFFFFE, 4, 1, breakpoint
->orig_instr
)) != ERROR_OK
)
950 if ((retval
= target_write_memory(target
, breakpoint
->address
& 0xFFFFFFFE, 2, 1, breakpoint
->orig_instr
)) != ERROR_OK
)
962 cortex_m3_add_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
964 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
966 if (cortex_m3
->auto_bp_type
)
968 breakpoint
->type
= (breakpoint
->address
< 0x20000000) ? BKPT_HARD
: BKPT_SOFT
;
969 #ifdef ARMV7_GDB_HACKS
970 if (breakpoint
->length
!= 2) {
971 /* XXX Hack: Replace all breakpoints with length != 2 with
972 * a hardware breakpoint. */
973 breakpoint
->type
= BKPT_HARD
;
974 breakpoint
->length
= 2;
979 if ((breakpoint
->type
== BKPT_HARD
) && (breakpoint
->address
>= 0x20000000))
981 LOG_INFO("flash patch comparator requested outside code memory region");
982 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
985 if ((breakpoint
->type
== BKPT_SOFT
) && (breakpoint
->address
< 0x20000000))
987 LOG_INFO("soft breakpoint requested in code (flash) memory region");
988 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
991 if ((breakpoint
->type
== BKPT_HARD
) && (cortex_m3
->fp_code_available
< 1))
993 LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
994 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
997 if ((breakpoint
->length
!= 2))
999 LOG_INFO("only breakpoints of two bytes length supported");
1000 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1003 if (breakpoint
->type
== BKPT_HARD
)
1004 cortex_m3
->fp_code_available
--;
1005 cortex_m3_set_breakpoint(target
, breakpoint
);
1011 cortex_m3_remove_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
1013 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1015 /* REVISIT why check? FBP can be updated with core running ... */
1016 if (target
->state
!= TARGET_HALTED
)
1018 LOG_WARNING("target not halted");
1019 return ERROR_TARGET_NOT_HALTED
;
1022 if (cortex_m3
->auto_bp_type
)
1024 breakpoint
->type
= (breakpoint
->address
< 0x20000000) ? BKPT_HARD
: BKPT_SOFT
;
1027 if (breakpoint
->set
)
1029 cortex_m3_unset_breakpoint(target
, breakpoint
);
1032 if (breakpoint
->type
== BKPT_HARD
)
1033 cortex_m3
->fp_code_available
++;
1039 cortex_m3_set_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
1042 uint32_t mask
, temp
;
1043 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1045 /* watchpoint params were validated earlier */
1047 temp
= watchpoint
->length
;
1054 /* REVISIT Don't fully trust these "not used" records ... users
1055 * may set up breakpoints by hand, e.g. dual-address data value
1056 * watchpoint using comparator #1; comparator #0 matching cycle
1057 * count; send data trace info through ITM and TPIU; etc
1059 struct cortex_m3_dwt_comparator
*comparator
;
1061 for (comparator
= cortex_m3
->dwt_comparator_list
;
1062 comparator
->used
&& dwt_num
< cortex_m3
->dwt_num_comp
;
1063 comparator
++, dwt_num
++)
1065 if (dwt_num
>= cortex_m3
->dwt_num_comp
)
1067 LOG_ERROR("Can not find free DWT Comparator");
1070 comparator
->used
= 1;
1071 watchpoint
->set
= dwt_num
+ 1;
1073 comparator
->comp
= watchpoint
->address
;
1074 target_write_u32(target
, comparator
->dwt_comparator_address
+ 0,
1077 comparator
->mask
= mask
;
1078 target_write_u32(target
, comparator
->dwt_comparator_address
+ 4,
1081 switch (watchpoint
->rw
) {
1083 comparator
->function
= 5;
1086 comparator
->function
= 6;
1089 comparator
->function
= 7;
1092 target_write_u32(target
, comparator
->dwt_comparator_address
+ 8,
1093 comparator
->function
);
1095 LOG_DEBUG("Watchpoint (ID %d) DWT%d 0x%08x 0x%x 0x%05x",
1096 watchpoint
->unique_id
, dwt_num
,
1097 (unsigned) comparator
->comp
,
1098 (unsigned) comparator
->mask
,
1099 (unsigned) comparator
->function
);
1104 cortex_m3_unset_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
1106 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1107 struct cortex_m3_dwt_comparator
*comparator
;
1110 if (!watchpoint
->set
)
1112 LOG_WARNING("watchpoint (wpid: %d) not set",
1113 watchpoint
->unique_id
);
1117 dwt_num
= watchpoint
->set
- 1;
1119 LOG_DEBUG("Watchpoint (ID %d) DWT%d address: 0x%08x clear",
1120 watchpoint
->unique_id
, dwt_num
,
1121 (unsigned) watchpoint
->address
);
1123 if ((dwt_num
< 0) || (dwt_num
>= cortex_m3
->dwt_num_comp
))
1125 LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
1129 comparator
= cortex_m3
->dwt_comparator_list
+ dwt_num
;
1130 comparator
->used
= 0;
1131 comparator
->function
= 0;
1132 target_write_u32(target
, comparator
->dwt_comparator_address
+ 8,
1133 comparator
->function
);
1135 watchpoint
->set
= 0;
1141 cortex_m3_add_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
1143 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1145 if (cortex_m3
->dwt_comp_available
< 1)
1147 LOG_DEBUG("no comparators?");
1148 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1151 /* hardware doesn't support data value masking */
1152 if (watchpoint
->mask
!= ~(uint32_t)0) {
1153 LOG_DEBUG("watchpoint value masks not supported");
1154 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1157 /* hardware allows address masks of up to 32K */
1160 for (mask
= 0; mask
< 16; mask
++) {
1161 if ((1u << mask
) == watchpoint
->length
)
1165 LOG_DEBUG("unsupported watchpoint length");
1166 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1168 if (watchpoint
->address
& ((1 << mask
) - 1)) {
1169 LOG_DEBUG("watchpoint address is unaligned");
1170 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1173 /* Caller doesn't seem to be able to describe watching for data
1174 * values of zero; that flags "no value".
1176 * REVISIT This DWT may well be able to watch for specific data
1177 * values. Requires comparator #1 to set DATAVMATCH and match
1178 * the data, and another comparator (DATAVADDR0) matching addr.
1180 if (watchpoint
->value
) {
1181 LOG_DEBUG("data value watchpoint not YET supported");
1182 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1185 cortex_m3
->dwt_comp_available
--;
1186 LOG_DEBUG("dwt_comp_available: %d", cortex_m3
->dwt_comp_available
);
1192 cortex_m3_remove_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
1194 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1196 /* REVISIT why check? DWT can be updated with core running ... */
1197 if (target
->state
!= TARGET_HALTED
)
1199 LOG_WARNING("target not halted");
1200 return ERROR_TARGET_NOT_HALTED
;
1203 if (watchpoint
->set
)
1205 cortex_m3_unset_watchpoint(target
, watchpoint
);
1208 cortex_m3
->dwt_comp_available
++;
1209 LOG_DEBUG("dwt_comp_available: %d", cortex_m3
->dwt_comp_available
);
1214 static void cortex_m3_enable_watchpoints(struct target
*target
)
1216 struct watchpoint
*watchpoint
= target
->watchpoints
;
1218 /* set any pending watchpoints */
1221 if (watchpoint
->set
== 0)
1222 cortex_m3_set_watchpoint(target
, watchpoint
);
1223 watchpoint
= watchpoint
->next
;
1227 static int cortex_m3_load_core_reg_u32(struct target
*target
,
1228 enum armv7m_regtype type
, uint32_t num
, uint32_t * value
)
1231 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1232 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1234 /* NOTE: we "know" here that the register identifiers used
1235 * in the v7m header match the Cortex-M3 Debug Core Register
1236 * Selector values for R0..R15, xPSR, MSP, and PSP.
1240 /* read a normal core register */
1241 retval
= cortexm3_dap_read_coreregister_u32(swjdp
, value
, num
);
1243 if (retval
!= ERROR_OK
)
1245 LOG_ERROR("JTAG failure %i",retval
);
1246 return ERROR_JTAG_DEVICE_ERROR
;
1248 LOG_DEBUG("load from core reg %i value 0x%" PRIx32
"",(int)num
,*value
);
1251 case ARMV7M_PRIMASK
:
1252 case ARMV7M_BASEPRI
:
1253 case ARMV7M_FAULTMASK
:
1254 case ARMV7M_CONTROL
:
1255 /* Cortex-M3 packages these four registers as bitfields
1256 * in one Debug Core register. So say r0 and r2 docs;
1257 * it was removed from r1 docs, but still works.
1259 cortexm3_dap_read_coreregister_u32(swjdp
, value
, 20);
1263 case ARMV7M_PRIMASK
:
1264 *value
= buf_get_u32((uint8_t*)value
, 0, 1);
1267 case ARMV7M_BASEPRI
:
1268 *value
= buf_get_u32((uint8_t*)value
, 8, 8);
1271 case ARMV7M_FAULTMASK
:
1272 *value
= buf_get_u32((uint8_t*)value
, 16, 1);
1275 case ARMV7M_CONTROL
:
1276 *value
= buf_get_u32((uint8_t*)value
, 24, 2);
1280 LOG_DEBUG("load from special reg %i value 0x%" PRIx32
"", (int)num
, *value
);
1284 return ERROR_INVALID_ARGUMENTS
;
1290 static int cortex_m3_store_core_reg_u32(struct target
*target
,
1291 enum armv7m_regtype type
, uint32_t num
, uint32_t value
)
1295 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1296 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1298 #ifdef ARMV7_GDB_HACKS
1299 /* If the LR register is being modified, make sure it will put us
1300 * in "thumb" mode, or an INVSTATE exception will occur. This is a
1301 * hack to deal with the fact that gdb will sometimes "forge"
1302 * return addresses, and doesn't set the LSB correctly (i.e., when
1303 * printing expressions containing function calls, it sets LR = 0.)
1304 * Valid exception return codes have bit 0 set too.
1306 if (num
== ARMV7M_R14
)
1310 /* NOTE: we "know" here that the register identifiers used
1311 * in the v7m header match the Cortex-M3 Debug Core Register
1312 * Selector values for R0..R15, xPSR, MSP, and PSP.
1316 retval
= cortexm3_dap_write_coreregister_u32(swjdp
, value
, num
);
1317 if (retval
!= ERROR_OK
)
1319 LOG_ERROR("JTAG failure %i", retval
);
1320 armv7m
->core_cache
->reg_list
[num
].dirty
= armv7m
->core_cache
->reg_list
[num
].valid
;
1321 return ERROR_JTAG_DEVICE_ERROR
;
1323 LOG_DEBUG("write core reg %i value 0x%" PRIx32
"", (int)num
, value
);
1326 case ARMV7M_PRIMASK
:
1327 case ARMV7M_BASEPRI
:
1328 case ARMV7M_FAULTMASK
:
1329 case ARMV7M_CONTROL
:
1330 /* Cortex-M3 packages these four registers as bitfields
1331 * in one Debug Core register. So say r0 and r2 docs;
1332 * it was removed from r1 docs, but still works.
1334 cortexm3_dap_read_coreregister_u32(swjdp
, ®
, 20);
1338 case ARMV7M_PRIMASK
:
1339 buf_set_u32((uint8_t*)®
, 0, 1, value
);
1342 case ARMV7M_BASEPRI
:
1343 buf_set_u32((uint8_t*)®
, 8, 8, value
);
1346 case ARMV7M_FAULTMASK
:
1347 buf_set_u32((uint8_t*)®
, 16, 1, value
);
1350 case ARMV7M_CONTROL
:
1351 buf_set_u32((uint8_t*)®
, 24, 2, value
);
1355 cortexm3_dap_write_coreregister_u32(swjdp
, reg
, 20);
1357 LOG_DEBUG("write special reg %i value 0x%" PRIx32
" ", (int)num
, value
);
1361 return ERROR_INVALID_ARGUMENTS
;
1367 static int cortex_m3_read_memory(struct target
*target
, uint32_t address
,
1368 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1370 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1371 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1372 int retval
= ERROR_INVALID_ARGUMENTS
;
1374 /* cortex_m3 handles unaligned memory access */
1375 if (count
&& buffer
) {
1378 retval
= mem_ap_read_buf_u32(swjdp
, buffer
, 4 * count
, address
);
1381 retval
= mem_ap_read_buf_u16(swjdp
, buffer
, 2 * count
, address
);
1384 retval
= mem_ap_read_buf_u8(swjdp
, buffer
, count
, address
);
1392 static int cortex_m3_write_memory(struct target
*target
, uint32_t address
,
1393 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1395 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1396 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1397 int retval
= ERROR_INVALID_ARGUMENTS
;
1399 if (count
&& buffer
) {
1402 retval
= mem_ap_write_buf_u32(swjdp
, buffer
, 4 * count
, address
);
1405 retval
= mem_ap_write_buf_u16(swjdp
, buffer
, 2 * count
, address
);
1408 retval
= mem_ap_write_buf_u8(swjdp
, buffer
, count
, address
);
1416 static int cortex_m3_bulk_write_memory(struct target
*target
, uint32_t address
,
1417 uint32_t count
, uint8_t *buffer
)
1419 return cortex_m3_write_memory(target
, address
, 4, count
, buffer
);
1422 static int cortex_m3_init_target(struct command_context
*cmd_ctx
,
1423 struct target
*target
)
1425 armv7m_build_reg_cache(target
);
1429 /* REVISIT cache valid/dirty bits are unmaintained. We could set "valid"
1430 * on r/w if the core is not running, and clear on resume or reset ... or
1431 * at least, in a post_restore_context() method.
1434 struct dwt_reg_state
{
1435 struct target
*target
;
1437 uint32_t value
; /* scratch/cache */
1440 static int cortex_m3_dwt_get_reg(struct reg
*reg
)
1442 struct dwt_reg_state
*state
= reg
->arch_info
;
1444 return target_read_u32(state
->target
, state
->addr
, &state
->value
);
1447 static int cortex_m3_dwt_set_reg(struct reg
*reg
, uint8_t *buf
)
1449 struct dwt_reg_state
*state
= reg
->arch_info
;
1451 return target_write_u32(state
->target
, state
->addr
,
1452 buf_get_u32(buf
, 0, reg
->size
));
1461 static struct dwt_reg dwt_base_regs
[] = {
1462 { DWT_CTRL
, "dwt_ctrl", 32, },
1463 { DWT_CYCCNT
, "dwt_cyccnt", 32, },
1464 /* plus some 8 bit counters, useful for profiling with TPIU */
1467 static struct dwt_reg dwt_comp
[] = {
1468 #define DWT_COMPARATOR(i) \
1469 { DWT_COMP0 + 0x10 * (i), "dwt_" #i "_comp", 32, }, \
1470 { DWT_MASK0 + 0x10 * (i), "dwt_" #i "_mask", 4, }, \
1471 { DWT_FUNCTION0 + 0x10 * (i), "dwt_" #i "_function", 32, }
1476 #undef DWT_COMPARATOR
1479 static const struct reg_arch_type dwt_reg_type
= {
1480 .get
= cortex_m3_dwt_get_reg
,
1481 .set
= cortex_m3_dwt_set_reg
,
1485 cortex_m3_dwt_addreg(struct target
*t
, struct reg
*r
, struct dwt_reg
*d
)
1487 struct dwt_reg_state
*state
;
1489 state
= calloc(1, sizeof *state
);
1492 state
->addr
= d
->addr
;
1497 r
->value
= &state
->value
;
1498 r
->arch_info
= state
;
1499 r
->type
= &dwt_reg_type
;
1503 cortex_m3_dwt_setup(struct cortex_m3_common
*cm3
, struct target
*target
)
1506 struct reg_cache
*cache
;
1507 struct cortex_m3_dwt_comparator
*comparator
;
1510 target_read_u32(target
, DWT_CTRL
, &dwtcr
);
1512 LOG_DEBUG("no DWT");
1516 cm3
->dwt_num_comp
= (dwtcr
>> 28) & 0xF;
1517 cm3
->dwt_comp_available
= cm3
->dwt_num_comp
;
1518 cm3
->dwt_comparator_list
= calloc(cm3
->dwt_num_comp
,
1519 sizeof(struct cortex_m3_dwt_comparator
));
1520 if (!cm3
->dwt_comparator_list
) {
1522 cm3
->dwt_num_comp
= 0;
1523 LOG_ERROR("out of mem");
1527 cache
= calloc(1, sizeof *cache
);
1530 free(cm3
->dwt_comparator_list
);
1533 cache
->name
= "cortex-m3 dwt registers";
1534 cache
->num_regs
= 2 + cm3
->dwt_num_comp
* 3;
1535 cache
->reg_list
= calloc(cache
->num_regs
, sizeof *cache
->reg_list
);
1536 if (!cache
->reg_list
) {
1541 for (reg
= 0; reg
< 2; reg
++)
1542 cortex_m3_dwt_addreg(target
, cache
->reg_list
+ reg
,
1543 dwt_base_regs
+ reg
);
1545 comparator
= cm3
->dwt_comparator_list
;
1546 for (i
= 0; i
< cm3
->dwt_num_comp
; i
++, comparator
++) {
1549 comparator
->dwt_comparator_address
= DWT_COMP0
+ 0x10 * i
;
1550 for (j
= 0; j
< 3; j
++, reg
++)
1551 cortex_m3_dwt_addreg(target
, cache
->reg_list
+ reg
,
1552 dwt_comp
+ 3 * i
+ j
);
1555 *register_get_last_cache_p(&target
->reg_cache
) = cache
;
1556 cm3
->dwt_cache
= cache
;
1558 LOG_DEBUG("DWT dwtcr 0x%" PRIx32
", comp %d, watch%s",
1559 dwtcr
, cm3
->dwt_num_comp
,
1560 (dwtcr
& (0xf << 24)) ? " only" : "/trigger");
1562 /* REVISIT: if num_comp > 1, check whether comparator #1 can
1563 * implement single-address data value watchpoints ... so we
1564 * won't need to check it later, when asked to set one up.
1568 static int cortex_m3_examine(struct target
*target
)
1571 uint32_t cpuid
, fpcr
;
1573 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1574 struct swjdp_common
*swjdp
= &cortex_m3
->armv7m
.swjdp_info
;
1576 if ((retval
= ahbap_debugport_init(swjdp
)) != ERROR_OK
)
1579 if (!target_was_examined(target
))
1581 target_set_examined(target
);
1583 /* Read from Device Identification Registers */
1584 retval
= target_read_u32(target
, CPUID
, &cpuid
);
1585 if (retval
!= ERROR_OK
)
1588 if (((cpuid
>> 4) & 0xc3f) == 0xc23)
1589 LOG_DEBUG("CORTEX-M3 processor detected");
1590 LOG_DEBUG("cpuid: 0x%8.8" PRIx32
"", cpuid
);
1592 /* NOTE: FPB and DWT are both optional. */
1595 target_read_u32(target
, FP_CTRL
, &fpcr
);
1596 cortex_m3
->auto_bp_type
= 1;
1597 cortex_m3
->fp_num_code
= ((fpcr
>> 8) & 0x70) | ((fpcr
>> 4) & 0xF); /* bits [14:12] and [7:4] */
1598 cortex_m3
->fp_num_lit
= (fpcr
>> 8) & 0xF;
1599 cortex_m3
->fp_code_available
= cortex_m3
->fp_num_code
;
1600 cortex_m3
->fp_comparator_list
= calloc(cortex_m3
->fp_num_code
+ cortex_m3
->fp_num_lit
, sizeof(struct cortex_m3_fp_comparator
));
1601 cortex_m3
->fpb_enabled
= fpcr
& 1;
1602 for (i
= 0; i
< cortex_m3
->fp_num_code
+ cortex_m3
->fp_num_lit
; i
++)
1604 cortex_m3
->fp_comparator_list
[i
].type
= (i
< cortex_m3
->fp_num_code
) ? FPCR_CODE
: FPCR_LITERAL
;
1605 cortex_m3
->fp_comparator_list
[i
].fpcr_address
= FP_COMP0
+ 4 * i
;
1607 LOG_DEBUG("FPB fpcr 0x%" PRIx32
", numcode %i, numlit %i", fpcr
, cortex_m3
->fp_num_code
, cortex_m3
->fp_num_lit
);
1610 cortex_m3_dwt_setup(cortex_m3
, target
);
1616 static int cortex_m3_dcc_read(struct swjdp_common
*swjdp
, uint8_t *value
, uint8_t *ctrl
)
1620 mem_ap_read_buf_u16(swjdp
, (uint8_t*)&dcrdr
, 1, DCB_DCRDR
);
1621 *ctrl
= (uint8_t)dcrdr
;
1622 *value
= (uint8_t)(dcrdr
>> 8);
1624 LOG_DEBUG("data 0x%x ctrl 0x%x", *value
, *ctrl
);
1626 /* write ack back to software dcc register
1627 * signify we have read data */
1628 if (dcrdr
& (1 << 0))
1631 mem_ap_write_buf_u16(swjdp
, (uint8_t*)&dcrdr
, 1, DCB_DCRDR
);
1637 static int cortex_m3_target_request_data(struct target
*target
,
1638 uint32_t size
, uint8_t *buffer
)
1640 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1641 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1646 for (i
= 0; i
< (size
* 4); i
++)
1648 cortex_m3_dcc_read(swjdp
, &data
, &ctrl
);
1655 static int cortex_m3_handle_target_request(void *priv
)
1657 struct target
*target
= priv
;
1658 if (!target_was_examined(target
))
1660 struct armv7m_common
*armv7m
= target_to_armv7m(target
);
1661 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1663 if (!target
->dbg_msg_enabled
)
1666 if (target
->state
== TARGET_RUNNING
)
1671 cortex_m3_dcc_read(swjdp
, &data
, &ctrl
);
1673 /* check if we have data */
1674 if (ctrl
& (1 << 0))
1678 /* we assume target is quick enough */
1680 cortex_m3_dcc_read(swjdp
, &data
, &ctrl
);
1681 request
|= (data
<< 8);
1682 cortex_m3_dcc_read(swjdp
, &data
, &ctrl
);
1683 request
|= (data
<< 16);
1684 cortex_m3_dcc_read(swjdp
, &data
, &ctrl
);
1685 request
|= (data
<< 24);
1686 target_request(target
, request
);
1693 static int cortex_m3_init_arch_info(struct target
*target
,
1694 struct cortex_m3_common
*cortex_m3
, struct jtag_tap
*tap
)
1697 struct armv7m_common
*armv7m
= &cortex_m3
->armv7m
;
1699 armv7m_init_arch_info(target
, armv7m
);
1701 /* prepare JTAG information for the new target */
1702 cortex_m3
->jtag_info
.tap
= tap
;
1703 cortex_m3
->jtag_info
.scann_size
= 4;
1705 armv7m
->swjdp_info
.dp_select_value
= -1;
1706 armv7m
->swjdp_info
.ap_csw_value
= -1;
1707 armv7m
->swjdp_info
.ap_tar_value
= -1;
1708 armv7m
->swjdp_info
.jtag_info
= &cortex_m3
->jtag_info
;
1709 armv7m
->swjdp_info
.memaccess_tck
= 8;
1710 armv7m
->swjdp_info
.tar_autoincr_block
= (1 << 12); /* Cortex-M3 has 4096 bytes autoincrement range */
1712 /* register arch-specific functions */
1713 armv7m
->examine_debug_reason
= cortex_m3_examine_debug_reason
;
1715 armv7m
->post_debug_entry
= NULL
;
1717 armv7m
->pre_restore_context
= NULL
;
1718 armv7m
->post_restore_context
= NULL
;
1720 armv7m
->load_core_reg_u32
= cortex_m3_load_core_reg_u32
;
1721 armv7m
->store_core_reg_u32
= cortex_m3_store_core_reg_u32
;
1723 target_register_timer_callback(cortex_m3_handle_target_request
, 1, 1, target
);
1725 if ((retval
= arm_jtag_setup_connection(&cortex_m3
->jtag_info
)) != ERROR_OK
)
1733 static int cortex_m3_target_create(struct target
*target
, Jim_Interp
*interp
)
1735 struct cortex_m3_common
*cortex_m3
= calloc(1,sizeof(struct cortex_m3_common
));
1737 cortex_m3
->common_magic
= CORTEX_M3_COMMON_MAGIC
;
1738 cortex_m3_init_arch_info(target
, cortex_m3
, target
->tap
);
1743 /*--------------------------------------------------------------------------*/
1745 static int cortex_m3_verify_pointer(struct command_context
*cmd_ctx
,
1746 struct cortex_m3_common
*cm3
)
1748 if (cm3
->common_magic
!= CORTEX_M3_COMMON_MAGIC
) {
1749 command_print(cmd_ctx
, "target is not a Cortex-M3");
1750 return ERROR_TARGET_INVALID
;
1756 * Only stuff below this line should need to verify that its target
1757 * is a Cortex-M3. Everything else should have indirected through the
1758 * cortexm3_target structure, which is only used with CM3 targets.
1762 * REVISIT Thumb2 disassembly should work for all ARMv7 cores, as well
1763 * as at least ARM-1156T2. The interesting thing about Cortex-M is
1764 * that *only* Thumb2 disassembly matters. There are also some small
1765 * additions to Thumb2 that are specific to ARMv7-M.
1767 COMMAND_HANDLER(handle_cortex_m3_disassemble_command
)
1770 struct target
*target
= get_current_target(CMD_CTX
);
1771 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1773 unsigned long count
= 1;
1774 struct arm_instruction cur_instruction
;
1776 retval
= cortex_m3_verify_pointer(CMD_CTX
, cortex_m3
);
1777 if (retval
!= ERROR_OK
)
1783 COMMAND_PARSE_NUMBER(ulong
, CMD_ARGV
[1], count
);
1786 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1789 command_print(CMD_CTX
,
1790 "usage: cortex_m3 disassemble <address> [<count>]");
1795 retval
= thumb2_opcode(target
, address
, &cur_instruction
);
1796 if (retval
!= ERROR_OK
)
1798 command_print(CMD_CTX
, "%s", cur_instruction
.text
);
1799 address
+= cur_instruction
.instruction_size
;
1805 static const struct {
1809 { "hard_err", VC_HARDERR
, },
1810 { "int_err", VC_INTERR
, },
1811 { "bus_err", VC_BUSERR
, },
1812 { "state_err", VC_STATERR
, },
1813 { "chk_err", VC_CHKERR
, },
1814 { "nocp_err", VC_NOCPERR
, },
1815 { "mm_err", VC_MMERR
, },
1816 { "reset", VC_CORERESET
, },
1819 COMMAND_HANDLER(handle_cortex_m3_vector_catch_command
)
1821 struct target
*target
= get_current_target(CMD_CTX
);
1822 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1823 struct armv7m_common
*armv7m
= &cortex_m3
->armv7m
;
1824 struct swjdp_common
*swjdp
= &armv7m
->swjdp_info
;
1828 retval
= cortex_m3_verify_pointer(CMD_CTX
, cortex_m3
);
1829 if (retval
!= ERROR_OK
)
1832 mem_ap_read_atomic_u32(swjdp
, DCB_DEMCR
, &demcr
);
1837 if (CMD_ARGC
== 1) {
1838 if (strcmp(CMD_ARGV
[0], "all") == 0) {
1839 catch = VC_HARDERR
| VC_INTERR
| VC_BUSERR
1840 | VC_STATERR
| VC_CHKERR
| VC_NOCPERR
1841 | VC_MMERR
| VC_CORERESET
;
1843 } else if (strcmp(CMD_ARGV
[0], "none") == 0) {
1847 while (CMD_ARGC
-- > 0) {
1849 for (i
= 0; i
< ARRAY_SIZE(vec_ids
); i
++) {
1850 if (strcmp(CMD_ARGV
[CMD_ARGC
], vec_ids
[i
].name
) != 0)
1852 catch |= vec_ids
[i
].mask
;
1855 if (i
== ARRAY_SIZE(vec_ids
)) {
1856 LOG_ERROR("No CM3 vector '%s'", CMD_ARGV
[CMD_ARGC
]);
1857 return ERROR_INVALID_ARGUMENTS
;
1864 /* write, but don't assume it stuck */
1865 mem_ap_write_u32(swjdp
, DCB_DEMCR
, demcr
);
1866 mem_ap_read_atomic_u32(swjdp
, DCB_DEMCR
, &demcr
);
1869 for (unsigned i
= 0; i
< ARRAY_SIZE(vec_ids
); i
++)
1871 command_print(CMD_CTX
, "%9s: %s", vec_ids
[i
].name
,
1872 (demcr
& vec_ids
[i
].mask
) ? "catch" : "ignore");
1878 COMMAND_HANDLER(handle_cortex_m3_mask_interrupts_command
)
1880 struct target
*target
= get_current_target(CMD_CTX
);
1881 struct cortex_m3_common
*cortex_m3
= target_to_cm3(target
);
1884 retval
= cortex_m3_verify_pointer(CMD_CTX
, cortex_m3
);
1885 if (retval
!= ERROR_OK
)
1888 if (target
->state
!= TARGET_HALTED
)
1890 command_print(CMD_CTX
, "target must be stopped for \"%s\" command", CMD_NAME
);
1897 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1898 uint32_t mask_on
= C_HALT
| (enable
? C_MASKINTS
: 0);
1899 uint32_t mask_off
= enable
? 0 : C_MASKINTS
;
1900 cortex_m3_write_debug_halt_mask(target
, mask_on
, mask_off
);
1903 command_print(CMD_CTX
, "cortex_m3 interrupt mask %s",
1904 (cortex_m3
->dcb_dhcsr
& C_MASKINTS
) ? "on" : "off");
1909 static const struct command_registration cortex_m3_exec_command_handlers
[] = {
1911 .name
= "disassemble",
1912 .handler
= &handle_cortex_m3_disassemble_command
,
1913 .mode
= COMMAND_EXEC
,
1914 .help
= "disassemble Thumb2 instructions",
1915 .usage
= "<address> [<count>]",
1919 .handler
= &handle_cortex_m3_mask_interrupts_command
,
1920 .mode
= COMMAND_EXEC
,
1921 .help
= "mask cortex_m3 interrupts",
1922 .usage
= "['on'|'off']",
1925 .name
= "vector_catch",
1926 .handler
= &handle_cortex_m3_vector_catch_command
,
1927 .mode
= COMMAND_EXEC
,
1928 .help
= "catch hardware vectors",
1929 .usage
= "['all'|'none'|<list>]",
1931 COMMAND_REGISTRATION_DONE
1933 static const struct command_registration cortex_m3_command_handlers
[] = {
1935 .chain
= armv7m_command_handlers
,
1938 .name
= "cortex_m3",
1939 .mode
= COMMAND_ANY
,
1940 .help
= "Cortex-M3 command group",
1941 .chain
= cortex_m3_exec_command_handlers
,
1943 COMMAND_REGISTRATION_DONE
1946 struct target_type cortexm3_target
=
1948 .name
= "cortex_m3",
1950 .poll
= cortex_m3_poll
,
1951 .arch_state
= armv7m_arch_state
,
1953 .target_request_data
= cortex_m3_target_request_data
,
1955 .halt
= cortex_m3_halt
,
1956 .resume
= cortex_m3_resume
,
1957 .step
= cortex_m3_step
,
1959 .assert_reset
= cortex_m3_assert_reset
,
1960 .deassert_reset
= cortex_m3_deassert_reset
,
1961 .soft_reset_halt
= cortex_m3_soft_reset_halt
,
1963 .get_gdb_reg_list
= armv7m_get_gdb_reg_list
,
1965 .read_memory
= cortex_m3_read_memory
,
1966 .write_memory
= cortex_m3_write_memory
,
1967 .bulk_write_memory
= cortex_m3_bulk_write_memory
,
1968 .checksum_memory
= armv7m_checksum_memory
,
1969 .blank_check_memory
= armv7m_blank_check_memory
,
1971 .run_algorithm
= armv7m_run_algorithm
,
1973 .add_breakpoint
= cortex_m3_add_breakpoint
,
1974 .remove_breakpoint
= cortex_m3_remove_breakpoint
,
1975 .add_watchpoint
= cortex_m3_add_watchpoint
,
1976 .remove_watchpoint
= cortex_m3_remove_watchpoint
,
1978 .commands
= cortex_m3_command_handlers
,
1979 .target_create
= cortex_m3_target_create
,
1980 .init_target
= cortex_m3_init_target
,
1981 .examine
= cortex_m3_examine
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)