1 /***************************************************************************
2 * Copyright (C) 2008 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
5 * Copyright (C) 2008 by David T.L. Wong *
7 * Copyright (C) 2009 by David N. Claffey <dnclaffey@gmail.com> *
9 * Copyright (C) 2011 by Drasko DRASKOVIC *
10 * drasko.draskovic@gmail.com *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
26 ***************************************************************************/
32 #include "breakpoints.h"
35 #include "mips32_dmaacc.h"
36 #include "target_type.h"
39 static void mips_m4k_enable_breakpoints(struct target
*target
);
40 static void mips_m4k_enable_watchpoints(struct target
*target
);
41 static int mips_m4k_set_breakpoint(struct target
*target
,
42 struct breakpoint
*breakpoint
);
43 static int mips_m4k_unset_breakpoint(struct target
*target
,
44 struct breakpoint
*breakpoint
);
45 static int mips_m4k_internal_restore(struct target
*target
, int current
,
46 uint32_t address
, int handle_breakpoints
,
48 static int mips_m4k_halt(struct target
*target
);
49 static int mips_m4k_bulk_write_memory(struct target
*target
, uint32_t address
,
50 uint32_t count
, const uint8_t *buffer
);
52 static int mips_m4k_examine_debug_reason(struct target
*target
)
54 uint32_t break_status
;
57 if ((target
->debug_reason
!= DBG_REASON_DBGRQ
)
58 && (target
->debug_reason
!= DBG_REASON_SINGLESTEP
)) {
59 /* get info about inst breakpoint support */
60 retval
= target_read_u32(target
, EJTAG_IBS
, &break_status
);
61 if (retval
!= ERROR_OK
)
63 if (break_status
& 0x1f) {
64 /* we have halted on a breakpoint */
65 retval
= target_write_u32(target
, EJTAG_IBS
, 0);
66 if (retval
!= ERROR_OK
)
68 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
71 /* get info about data breakpoint support */
72 retval
= target_read_u32(target
, EJTAG_DBS
, &break_status
);
73 if (retval
!= ERROR_OK
)
75 if (break_status
& 0x1f) {
76 /* we have halted on a breakpoint */
77 retval
= target_write_u32(target
, EJTAG_DBS
, 0);
78 if (retval
!= ERROR_OK
)
80 target
->debug_reason
= DBG_REASON_WATCHPOINT
;
87 static int mips_m4k_debug_entry(struct target
*target
)
89 struct mips32_common
*mips32
= target_to_mips32(target
);
90 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
92 mips32_save_context(target
);
94 /* make sure stepping disabled, SSt bit in CP0 debug register cleared */
95 mips_ejtag_config_step(ejtag_info
, 0);
97 /* make sure break unit configured */
98 mips32_configure_break_unit(target
);
100 /* attempt to find halt reason */
101 mips_m4k_examine_debug_reason(target
);
103 /* default to mips32 isa, it will be changed below if required */
104 mips32
->isa_mode
= MIPS32_ISA_MIPS32
;
106 if (ejtag_info
->impcode
& EJTAG_IMP_MIPS16
)
107 mips32
->isa_mode
= buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 1);
109 LOG_DEBUG("entered debug state at PC 0x%" PRIx32
", target->state: %s",
110 buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32),
111 target_state_name(target
));
116 static struct target
*get_mips_m4k(struct target
*target
, int32_t coreid
)
118 struct target_list
*head
;
122 while (head
!= (struct target_list
*)NULL
) {
124 if ((curr
->coreid
== coreid
) && (curr
->state
== TARGET_HALTED
))
131 static int mips_m4k_halt_smp(struct target
*target
)
133 int retval
= ERROR_OK
;
134 struct target_list
*head
;
137 while (head
!= (struct target_list
*)NULL
) {
140 if ((curr
!= target
) && (curr
->state
!= TARGET_HALTED
))
141 ret
= mips_m4k_halt(curr
);
143 if (ret
!= ERROR_OK
) {
144 LOG_ERROR("halt failed target->coreid: %d", curr
->coreid
);
152 static int update_halt_gdb(struct target
*target
)
154 int retval
= ERROR_OK
;
155 if (target
->gdb_service
->core
[0] == -1) {
156 target
->gdb_service
->target
= target
;
157 target
->gdb_service
->core
[0] = target
->coreid
;
158 retval
= mips_m4k_halt_smp(target
);
163 static int mips_m4k_poll(struct target
*target
)
165 int retval
= ERROR_OK
;
166 struct mips32_common
*mips32
= target_to_mips32(target
);
167 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
168 uint32_t ejtag_ctrl
= ejtag_info
->ejtag_ctrl
;
169 enum target_state prev_target_state
= target
->state
;
171 /* toggle to another core is done by gdb as follow */
172 /* maint packet J core_id */
174 /* the next polling trigger an halt event sent to gdb */
175 if ((target
->state
== TARGET_HALTED
) && (target
->smp
) &&
176 (target
->gdb_service
) &&
177 (target
->gdb_service
->target
== NULL
)) {
178 target
->gdb_service
->target
=
179 get_mips_m4k(target
, target
->gdb_service
->core
[1]);
180 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
184 /* read ejtag control reg */
185 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
186 retval
= mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
187 if (retval
!= ERROR_OK
)
190 /* clear this bit before handling polling
191 * as after reset registers will read zero */
192 if (ejtag_ctrl
& EJTAG_CTRL_ROCC
) {
193 /* we have detected a reset, clear flag
194 * otherwise ejtag will not work */
195 ejtag_ctrl
= ejtag_info
->ejtag_ctrl
& ~EJTAG_CTRL_ROCC
;
197 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
198 retval
= mips_ejtag_drscan_32(ejtag_info
, &ejtag_ctrl
);
199 if (retval
!= ERROR_OK
)
201 LOG_DEBUG("Reset Detected");
204 /* check for processor halted */
205 if (ejtag_ctrl
& EJTAG_CTRL_BRKST
) {
206 if ((target
->state
!= TARGET_HALTED
)
207 && (target
->state
!= TARGET_DEBUG_RUNNING
)) {
208 if (target
->state
== TARGET_UNKNOWN
)
209 LOG_DEBUG("EJTAG_CTRL_BRKST already set during server startup.");
211 /* OpenOCD was was probably started on the board with EJTAG_CTRL_BRKST already set
212 * (maybe put on by HALT-ing the board in the previous session).
214 * Force enable debug entry for this session.
216 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_NORMALBOOT
);
217 target
->state
= TARGET_HALTED
;
218 retval
= mips_m4k_debug_entry(target
);
219 if (retval
!= ERROR_OK
)
223 ((prev_target_state
== TARGET_RUNNING
)
224 || (prev_target_state
== TARGET_RESET
))) {
225 retval
= update_halt_gdb(target
);
226 if (retval
!= ERROR_OK
)
229 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
230 } else if (target
->state
== TARGET_DEBUG_RUNNING
) {
231 target
->state
= TARGET_HALTED
;
233 retval
= mips_m4k_debug_entry(target
);
234 if (retval
!= ERROR_OK
)
238 retval
= update_halt_gdb(target
);
239 if (retval
!= ERROR_OK
)
243 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_HALTED
);
246 target
->state
= TARGET_RUNNING
;
248 /* LOG_DEBUG("ctrl = 0x%08X", ejtag_ctrl); */
253 static int mips_m4k_halt(struct target
*target
)
255 struct mips32_common
*mips32
= target_to_mips32(target
);
256 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
258 LOG_DEBUG("target->state: %s", target_state_name(target
));
260 if (target
->state
== TARGET_HALTED
) {
261 LOG_DEBUG("target was already halted");
265 if (target
->state
== TARGET_UNKNOWN
)
266 LOG_WARNING("target was in unknown state when halt was requested");
268 if (target
->state
== TARGET_RESET
) {
269 if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST
) && jtag_get_srst()) {
270 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
271 return ERROR_TARGET_FAILURE
;
273 /* we came here in a reset_halt or reset_init sequence
274 * debug entry was already prepared in mips_m4k_assert_reset()
276 target
->debug_reason
= DBG_REASON_DBGRQ
;
282 /* break processor */
283 mips_ejtag_enter_debug(ejtag_info
);
285 target
->debug_reason
= DBG_REASON_DBGRQ
;
290 static int mips_m4k_assert_reset(struct target
*target
)
292 struct mips_m4k_common
*mips_m4k
= target_to_m4k(target
);
293 struct mips_ejtag
*ejtag_info
= &mips_m4k
->mips32
.ejtag_info
;
295 LOG_DEBUG("target->state: %s",
296 target_state_name(target
));
298 enum reset_types jtag_reset_config
= jtag_get_reset_config();
300 /* some cores support connecting while srst is asserted
301 * use that mode is it has been configured */
303 bool srst_asserted
= false;
305 if (!(jtag_reset_config
& RESET_SRST_PULLS_TRST
) &&
306 (jtag_reset_config
& RESET_SRST_NO_GATING
)) {
307 jtag_add_reset(0, 1);
308 srst_asserted
= true;
311 if (target
->reset_halt
) {
312 /* use hardware to catch reset */
313 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_EJTAGBOOT
);
315 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_NORMALBOOT
);
317 if (jtag_reset_config
& RESET_HAS_SRST
) {
318 /* here we should issue a srst only, but we may have to assert trst as well */
319 if (jtag_reset_config
& RESET_SRST_PULLS_TRST
)
320 jtag_add_reset(1, 1);
321 else if (!srst_asserted
)
322 jtag_add_reset(0, 1);
324 if (mips_m4k
->is_pic32mx
) {
325 LOG_DEBUG("Using MTAP reset to reset processor...");
327 /* use microchip specific MTAP reset */
328 mips_ejtag_set_instr(ejtag_info
, MTAP_SW_MTAP
);
329 mips_ejtag_set_instr(ejtag_info
, MTAP_COMMAND
);
331 mips_ejtag_drscan_8_out(ejtag_info
, MCHP_ASERT_RST
);
332 mips_ejtag_drscan_8_out(ejtag_info
, MCHP_DE_ASSERT_RST
);
333 mips_ejtag_set_instr(ejtag_info
, MTAP_SW_ETAP
);
335 /* use ejtag reset - not supported by all cores */
336 uint32_t ejtag_ctrl
= ejtag_info
->ejtag_ctrl
| EJTAG_CTRL_PRRST
| EJTAG_CTRL_PERRST
;
337 LOG_DEBUG("Using EJTAG reset (PRRST) to reset processor...");
338 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
);
339 mips_ejtag_drscan_32_out(ejtag_info
, ejtag_ctrl
);
343 target
->state
= TARGET_RESET
;
344 jtag_add_sleep(50000);
346 register_cache_invalidate(mips_m4k
->mips32
.core_cache
);
348 if (target
->reset_halt
) {
349 int retval
= target_halt(target
);
350 if (retval
!= ERROR_OK
)
357 static int mips_m4k_deassert_reset(struct target
*target
)
359 LOG_DEBUG("target->state: %s", target_state_name(target
));
361 /* deassert reset lines */
362 jtag_add_reset(0, 0);
367 static int mips_m4k_soft_reset_halt(struct target
*target
)
373 static int mips_m4k_single_step_core(struct target
*target
)
375 struct mips32_common
*mips32
= target_to_mips32(target
);
376 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
378 /* configure single step mode */
379 mips_ejtag_config_step(ejtag_info
, 1);
381 /* disable interrupts while stepping */
382 mips32_enable_interrupts(target
, 0);
384 /* exit debug mode */
385 mips_ejtag_exit_debug(ejtag_info
);
387 mips_m4k_debug_entry(target
);
392 static int mips_m4k_restore_smp(struct target
*target
, uint32_t address
, int handle_breakpoints
)
394 int retval
= ERROR_OK
;
395 struct target_list
*head
;
399 while (head
!= (struct target_list
*)NULL
) {
402 if ((curr
!= target
) && (curr
->state
!= TARGET_RUNNING
)) {
403 /* resume current address , not in step mode */
404 ret
= mips_m4k_internal_restore(curr
, 1, address
,
405 handle_breakpoints
, 0);
407 if (ret
!= ERROR_OK
) {
408 LOG_ERROR("target->coreid :%d failed to resume at address :0x%x",
409 curr
->coreid
, address
);
418 static int mips_m4k_internal_restore(struct target
*target
, int current
,
419 uint32_t address
, int handle_breakpoints
, int debug_execution
)
421 struct mips32_common
*mips32
= target_to_mips32(target
);
422 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
423 struct breakpoint
*breakpoint
= NULL
;
426 if (target
->state
!= TARGET_HALTED
) {
427 LOG_WARNING("target not halted");
428 return ERROR_TARGET_NOT_HALTED
;
431 if (!debug_execution
) {
432 target_free_all_working_areas(target
);
433 mips_m4k_enable_breakpoints(target
);
434 mips_m4k_enable_watchpoints(target
);
437 /* current = 1: continue on current pc, otherwise continue at <address> */
439 buf_set_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32, address
);
440 mips32
->core_cache
->reg_list
[MIPS32_PC
].dirty
= 1;
441 mips32
->core_cache
->reg_list
[MIPS32_PC
].valid
= 1;
444 if (ejtag_info
->impcode
& EJTAG_IMP_MIPS16
)
445 buf_set_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 1, mips32
->isa_mode
);
450 resume_pc
= buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32);
452 mips32_restore_context(target
);
454 /* the front-end may request us not to handle breakpoints */
455 if (handle_breakpoints
) {
456 /* Single step past breakpoint at current address */
457 breakpoint
= breakpoint_find(target
, resume_pc
);
459 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32
"", breakpoint
->address
);
460 mips_m4k_unset_breakpoint(target
, breakpoint
);
461 mips_m4k_single_step_core(target
);
462 mips_m4k_set_breakpoint(target
, breakpoint
);
466 /* enable interrupts if we are running */
467 mips32_enable_interrupts(target
, !debug_execution
);
469 /* exit debug mode */
470 mips_ejtag_exit_debug(ejtag_info
);
471 target
->debug_reason
= DBG_REASON_NOTHALTED
;
473 /* registers are now invalid */
474 register_cache_invalidate(mips32
->core_cache
);
476 if (!debug_execution
) {
477 target
->state
= TARGET_RUNNING
;
478 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
479 LOG_DEBUG("target resumed at 0x%" PRIx32
"", resume_pc
);
481 target
->state
= TARGET_DEBUG_RUNNING
;
482 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_RESUMED
);
483 LOG_DEBUG("target debug resumed at 0x%" PRIx32
"", resume_pc
);
489 static int mips_m4k_resume(struct target
*target
, int current
,
490 uint32_t address
, int handle_breakpoints
, int debug_execution
)
492 int retval
= ERROR_OK
;
494 /* dummy resume for smp toggle in order to reduce gdb impact */
495 if ((target
->smp
) && (target
->gdb_service
->core
[1] != -1)) {
496 /* simulate a start and halt of target */
497 target
->gdb_service
->target
= NULL
;
498 target
->gdb_service
->core
[0] = target
->gdb_service
->core
[1];
499 /* fake resume at next poll we play the target core[1], see poll*/
500 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
504 retval
= mips_m4k_internal_restore(target
, current
, address
,
508 if (retval
== ERROR_OK
&& target
->smp
) {
509 target
->gdb_service
->core
[0] = -1;
510 retval
= mips_m4k_restore_smp(target
, address
, handle_breakpoints
);
516 static int mips_m4k_step(struct target
*target
, int current
,
517 uint32_t address
, int handle_breakpoints
)
519 /* get pointers to arch-specific information */
520 struct mips32_common
*mips32
= target_to_mips32(target
);
521 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
522 struct breakpoint
*breakpoint
= NULL
;
524 if (target
->state
!= TARGET_HALTED
) {
525 LOG_WARNING("target not halted");
526 return ERROR_TARGET_NOT_HALTED
;
529 /* current = 1: continue on current pc, otherwise continue at <address> */
531 buf_set_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32, address
);
532 mips32
->core_cache
->reg_list
[MIPS32_PC
].dirty
= 1;
533 mips32
->core_cache
->reg_list
[MIPS32_PC
].valid
= 1;
536 /* the front-end may request us not to handle breakpoints */
537 if (handle_breakpoints
) {
538 breakpoint
= breakpoint_find(target
,
539 buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32));
541 mips_m4k_unset_breakpoint(target
, breakpoint
);
544 /* restore context */
545 mips32_restore_context(target
);
547 /* configure single step mode */
548 mips_ejtag_config_step(ejtag_info
, 1);
550 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
552 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
554 /* disable interrupts while stepping */
555 mips32_enable_interrupts(target
, 0);
557 /* exit debug mode */
558 mips_ejtag_exit_debug(ejtag_info
);
560 /* registers are now invalid */
561 register_cache_invalidate(mips32
->core_cache
);
563 LOG_DEBUG("target stepped ");
564 mips_m4k_debug_entry(target
);
567 mips_m4k_set_breakpoint(target
, breakpoint
);
569 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
574 static void mips_m4k_enable_breakpoints(struct target
*target
)
576 struct breakpoint
*breakpoint
= target
->breakpoints
;
578 /* set any pending breakpoints */
580 if (breakpoint
->set
== 0)
581 mips_m4k_set_breakpoint(target
, breakpoint
);
582 breakpoint
= breakpoint
->next
;
586 static int mips_m4k_set_breakpoint(struct target
*target
,
587 struct breakpoint
*breakpoint
)
589 struct mips32_common
*mips32
= target_to_mips32(target
);
590 struct mips32_comparator
*comparator_list
= mips32
->inst_break_list
;
593 if (breakpoint
->set
) {
594 LOG_WARNING("breakpoint already set");
598 if (breakpoint
->type
== BKPT_HARD
) {
601 while (comparator_list
[bp_num
].used
&& (bp_num
< mips32
->num_inst_bpoints
))
603 if (bp_num
>= mips32
->num_inst_bpoints
) {
604 LOG_ERROR("Can not find free FP Comparator(bpid: %d)",
605 breakpoint
->unique_id
);
606 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
608 breakpoint
->set
= bp_num
+ 1;
609 comparator_list
[bp_num
].used
= 1;
610 comparator_list
[bp_num
].bp_value
= breakpoint
->address
;
611 target_write_u32(target
, comparator_list
[bp_num
].reg_address
,
612 comparator_list
[bp_num
].bp_value
);
613 target_write_u32(target
, comparator_list
[bp_num
].reg_address
+ 0x08, 0x00000000);
614 target_write_u32(target
, comparator_list
[bp_num
].reg_address
+ 0x18, 1);
615 LOG_DEBUG("bpid: %d, bp_num %i bp_value 0x%" PRIx32
"",
616 breakpoint
->unique_id
,
617 bp_num
, comparator_list
[bp_num
].bp_value
);
618 } else if (breakpoint
->type
== BKPT_SOFT
) {
619 LOG_DEBUG("bpid: %d", breakpoint
->unique_id
);
620 if (breakpoint
->length
== 4) {
621 uint32_t verify
= 0xffffffff;
623 retval
= target_read_memory(target
, breakpoint
->address
, breakpoint
->length
, 1,
624 breakpoint
->orig_instr
);
625 if (retval
!= ERROR_OK
)
627 retval
= target_write_u32(target
, breakpoint
->address
, MIPS32_SDBBP
);
628 if (retval
!= ERROR_OK
)
631 retval
= target_read_u32(target
, breakpoint
->address
, &verify
);
632 if (retval
!= ERROR_OK
)
634 if (verify
!= MIPS32_SDBBP
) {
635 LOG_ERROR("Unable to set 32bit breakpoint at address %08" PRIx32
636 " - check that memory is read/writable", breakpoint
->address
);
640 uint16_t verify
= 0xffff;
642 retval
= target_read_memory(target
, breakpoint
->address
, breakpoint
->length
, 1,
643 breakpoint
->orig_instr
);
644 if (retval
!= ERROR_OK
)
646 retval
= target_write_u16(target
, breakpoint
->address
, MIPS16_SDBBP
);
647 if (retval
!= ERROR_OK
)
650 retval
= target_read_u16(target
, breakpoint
->address
, &verify
);
651 if (retval
!= ERROR_OK
)
653 if (verify
!= MIPS16_SDBBP
) {
654 LOG_ERROR("Unable to set 16bit breakpoint at address %08" PRIx32
655 " - check that memory is read/writable", breakpoint
->address
);
660 breakpoint
->set
= 20; /* Any nice value but 0 */
666 static int mips_m4k_unset_breakpoint(struct target
*target
,
667 struct breakpoint
*breakpoint
)
669 /* get pointers to arch-specific information */
670 struct mips32_common
*mips32
= target_to_mips32(target
);
671 struct mips32_comparator
*comparator_list
= mips32
->inst_break_list
;
674 if (!breakpoint
->set
) {
675 LOG_WARNING("breakpoint not set");
679 if (breakpoint
->type
== BKPT_HARD
) {
680 int bp_num
= breakpoint
->set
- 1;
681 if ((bp_num
< 0) || (bp_num
>= mips32
->num_inst_bpoints
)) {
682 LOG_DEBUG("Invalid FP Comparator number in breakpoint (bpid: %d)",
683 breakpoint
->unique_id
);
686 LOG_DEBUG("bpid: %d - releasing hw: %d",
687 breakpoint
->unique_id
,
689 comparator_list
[bp_num
].used
= 0;
690 comparator_list
[bp_num
].bp_value
= 0;
691 target_write_u32(target
, comparator_list
[bp_num
].reg_address
+ 0x18, 0);
694 /* restore original instruction (kept in target endianness) */
695 LOG_DEBUG("bpid: %d", breakpoint
->unique_id
);
696 if (breakpoint
->length
== 4) {
697 uint32_t current_instr
;
699 /* check that user program has not modified breakpoint instruction */
700 retval
= target_read_memory(target
, breakpoint
->address
, 4, 1,
701 (uint8_t *)¤t_instr
);
702 if (retval
!= ERROR_OK
)
706 * target_read_memory() gets us data in _target_ endianess.
707 * If we want to use this data on the host for comparisons with some macros
708 * we must first transform it to _host_ endianess using target_buffer_get_u32().
710 current_instr
= target_buffer_get_u32(target
, (uint8_t *)¤t_instr
);
712 if (current_instr
== MIPS32_SDBBP
) {
713 retval
= target_write_memory(target
, breakpoint
->address
, 4, 1,
714 breakpoint
->orig_instr
);
715 if (retval
!= ERROR_OK
)
719 uint16_t current_instr
;
721 /* check that user program has not modified breakpoint instruction */
722 retval
= target_read_memory(target
, breakpoint
->address
, 2, 1,
723 (uint8_t *)¤t_instr
);
724 if (retval
!= ERROR_OK
)
726 current_instr
= target_buffer_get_u16(target
, (uint8_t *)¤t_instr
);
727 if (current_instr
== MIPS16_SDBBP
) {
728 retval
= target_write_memory(target
, breakpoint
->address
, 2, 1,
729 breakpoint
->orig_instr
);
730 if (retval
!= ERROR_OK
)
740 static int mips_m4k_add_breakpoint(struct target
*target
, struct breakpoint
*breakpoint
)
742 struct mips32_common
*mips32
= target_to_mips32(target
);
744 if (breakpoint
->type
== BKPT_HARD
) {
745 if (mips32
->num_inst_bpoints_avail
< 1) {
746 LOG_INFO("no hardware breakpoint available");
747 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
750 mips32
->num_inst_bpoints_avail
--;
753 return mips_m4k_set_breakpoint(target
, breakpoint
);
756 static int mips_m4k_remove_breakpoint(struct target
*target
,
757 struct breakpoint
*breakpoint
)
759 /* get pointers to arch-specific information */
760 struct mips32_common
*mips32
= target_to_mips32(target
);
762 if (target
->state
!= TARGET_HALTED
) {
763 LOG_WARNING("target not halted");
764 return ERROR_TARGET_NOT_HALTED
;
768 mips_m4k_unset_breakpoint(target
, breakpoint
);
770 if (breakpoint
->type
== BKPT_HARD
)
771 mips32
->num_inst_bpoints_avail
++;
776 static int mips_m4k_set_watchpoint(struct target
*target
,
777 struct watchpoint
*watchpoint
)
779 struct mips32_common
*mips32
= target_to_mips32(target
);
780 struct mips32_comparator
*comparator_list
= mips32
->data_break_list
;
783 * watchpoint enabled, ignore all byte lanes in value register
784 * and exclude both load and store accesses from watchpoint
785 * condition evaluation
787 int enable
= EJTAG_DBCn_NOSB
| EJTAG_DBCn_NOLB
| EJTAG_DBCn_BE
|
788 (0xff << EJTAG_DBCn_BLM_SHIFT
);
790 if (watchpoint
->set
) {
791 LOG_WARNING("watchpoint already set");
795 while (comparator_list
[wp_num
].used
&& (wp_num
< mips32
->num_data_bpoints
))
797 if (wp_num
>= mips32
->num_data_bpoints
) {
798 LOG_ERROR("Can not find free FP Comparator");
802 if (watchpoint
->length
!= 4) {
803 LOG_ERROR("Only watchpoints of length 4 are supported");
804 return ERROR_TARGET_UNALIGNED_ACCESS
;
807 if (watchpoint
->address
% 4) {
808 LOG_ERROR("Watchpoints address should be word aligned");
809 return ERROR_TARGET_UNALIGNED_ACCESS
;
812 switch (watchpoint
->rw
) {
814 enable
&= ~EJTAG_DBCn_NOLB
;
817 enable
&= ~EJTAG_DBCn_NOSB
;
820 enable
&= ~(EJTAG_DBCn_NOLB
| EJTAG_DBCn_NOSB
);
823 LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
826 watchpoint
->set
= wp_num
+ 1;
827 comparator_list
[wp_num
].used
= 1;
828 comparator_list
[wp_num
].bp_value
= watchpoint
->address
;
829 target_write_u32(target
, comparator_list
[wp_num
].reg_address
, comparator_list
[wp_num
].bp_value
);
830 target_write_u32(target
, comparator_list
[wp_num
].reg_address
+ 0x08, 0x00000000);
831 target_write_u32(target
, comparator_list
[wp_num
].reg_address
+ 0x10, 0x00000000);
832 target_write_u32(target
, comparator_list
[wp_num
].reg_address
+ 0x18, enable
);
833 target_write_u32(target
, comparator_list
[wp_num
].reg_address
+ 0x20, 0);
834 LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32
"", wp_num
, comparator_list
[wp_num
].bp_value
);
839 static int mips_m4k_unset_watchpoint(struct target
*target
,
840 struct watchpoint
*watchpoint
)
842 /* get pointers to arch-specific information */
843 struct mips32_common
*mips32
= target_to_mips32(target
);
844 struct mips32_comparator
*comparator_list
= mips32
->data_break_list
;
846 if (!watchpoint
->set
) {
847 LOG_WARNING("watchpoint not set");
851 int wp_num
= watchpoint
->set
- 1;
852 if ((wp_num
< 0) || (wp_num
>= mips32
->num_data_bpoints
)) {
853 LOG_DEBUG("Invalid FP Comparator number in watchpoint");
856 comparator_list
[wp_num
].used
= 0;
857 comparator_list
[wp_num
].bp_value
= 0;
858 target_write_u32(target
, comparator_list
[wp_num
].reg_address
+ 0x18, 0);
864 static int mips_m4k_add_watchpoint(struct target
*target
, struct watchpoint
*watchpoint
)
866 struct mips32_common
*mips32
= target_to_mips32(target
);
868 if (mips32
->num_data_bpoints_avail
< 1) {
869 LOG_INFO("no hardware watchpoints available");
870 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
873 mips32
->num_data_bpoints_avail
--;
875 mips_m4k_set_watchpoint(target
, watchpoint
);
879 static int mips_m4k_remove_watchpoint(struct target
*target
,
880 struct watchpoint
*watchpoint
)
882 /* get pointers to arch-specific information */
883 struct mips32_common
*mips32
= target_to_mips32(target
);
885 if (target
->state
!= TARGET_HALTED
) {
886 LOG_WARNING("target not halted");
887 return ERROR_TARGET_NOT_HALTED
;
891 mips_m4k_unset_watchpoint(target
, watchpoint
);
893 mips32
->num_data_bpoints_avail
++;
898 static void mips_m4k_enable_watchpoints(struct target
*target
)
900 struct watchpoint
*watchpoint
= target
->watchpoints
;
902 /* set any pending watchpoints */
904 if (watchpoint
->set
== 0)
905 mips_m4k_set_watchpoint(target
, watchpoint
);
906 watchpoint
= watchpoint
->next
;
910 static int mips_m4k_read_memory(struct target
*target
, uint32_t address
,
911 uint32_t size
, uint32_t count
, uint8_t *buffer
)
913 struct mips32_common
*mips32
= target_to_mips32(target
);
914 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
916 LOG_DEBUG("address: 0x%8.8" PRIx32
", size: 0x%8.8" PRIx32
", count: 0x%8.8" PRIx32
"",
917 address
, size
, count
);
919 if (target
->state
!= TARGET_HALTED
) {
920 LOG_WARNING("target not halted");
921 return ERROR_TARGET_NOT_HALTED
;
924 /* sanitize arguments */
925 if (((size
!= 4) && (size
!= 2) && (size
!= 1)) || (count
== 0) || !(buffer
))
926 return ERROR_COMMAND_SYNTAX_ERROR
;
928 if (((size
== 4) && (address
& 0x3u
)) || ((size
== 2) && (address
& 0x1u
)))
929 return ERROR_TARGET_UNALIGNED_ACCESS
;
931 /* since we don't know if buffer is aligned, we allocate new mem that is always aligned */
935 t
= malloc(count
* size
* sizeof(uint8_t));
937 LOG_ERROR("Out of memory");
943 /* if noDMA off, use DMAACC mode for memory read */
945 if (ejtag_info
->impcode
& EJTAG_IMP_NODMA
)
946 retval
= mips32_pracc_read_mem(ejtag_info
, address
, size
, count
, t
);
948 retval
= mips32_dmaacc_read_mem(ejtag_info
, address
, size
, count
, t
);
950 /* mips32_..._read_mem with size 4/2 returns uint32_t/uint16_t in host */
951 /* endianness, but byte array should represent target endianness */
952 if (ERROR_OK
== retval
) {
955 target_buffer_set_u32_array(target
, buffer
, count
, t
);
958 target_buffer_set_u16_array(target
, buffer
, count
, t
);
963 if ((size
> 1) && (t
!= NULL
))
969 static int mips_m4k_write_memory(struct target
*target
, uint32_t address
,
970 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
972 struct mips32_common
*mips32
= target_to_mips32(target
);
973 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
975 LOG_DEBUG("address: 0x%8.8" PRIx32
", size: 0x%8.8" PRIx32
", count: 0x%8.8" PRIx32
"",
976 address
, size
, count
);
978 if (target
->state
!= TARGET_HALTED
) {
979 LOG_WARNING("target not halted");
980 return ERROR_TARGET_NOT_HALTED
;
983 if (size
== 4 && count
> 32) {
984 int retval
= mips_m4k_bulk_write_memory(target
, address
, count
, buffer
);
985 if (retval
== ERROR_OK
)
987 LOG_WARNING("Falling back to non-bulk write");
990 /* sanitize arguments */
991 if (((size
!= 4) && (size
!= 2) && (size
!= 1)) || (count
== 0) || !(buffer
))
992 return ERROR_COMMAND_SYNTAX_ERROR
;
994 if (((size
== 4) && (address
& 0x3u
)) || ((size
== 2) && (address
& 0x1u
)))
995 return ERROR_TARGET_UNALIGNED_ACCESS
;
997 /** correct endianess if we have word or hword access */
1000 /* mips32_..._write_mem with size 4/2 requires uint32_t/uint16_t in host */
1001 /* endianness, but byte array represents target endianness */
1002 t
= malloc(count
* size
* sizeof(uint8_t));
1004 LOG_ERROR("Out of memory");
1010 target_buffer_get_u32_array(target
, buffer
, count
, (uint32_t *)t
);
1013 target_buffer_get_u16_array(target
, buffer
, count
, (uint16_t *)t
);
1019 /* if noDMA off, use DMAACC mode for memory write */
1021 if (ejtag_info
->impcode
& EJTAG_IMP_NODMA
)
1022 retval
= mips32_pracc_write_mem(ejtag_info
, address
, size
, count
, (void *)buffer
);
1024 retval
= mips32_dmaacc_write_mem(ejtag_info
, address
, size
, count
, (void *)buffer
);
1029 if (ERROR_OK
!= retval
)
1035 static int mips_m4k_init_target(struct command_context
*cmd_ctx
,
1036 struct target
*target
)
1038 mips32_build_reg_cache(target
);
1043 static int mips_m4k_init_arch_info(struct target
*target
,
1044 struct mips_m4k_common
*mips_m4k
, struct jtag_tap
*tap
)
1046 struct mips32_common
*mips32
= &mips_m4k
->mips32
;
1048 mips_m4k
->common_magic
= MIPSM4K_COMMON_MAGIC
;
1050 /* initialize mips4k specific info */
1051 mips32_init_arch_info(target
, mips32
, tap
);
1052 mips32
->arch_info
= mips_m4k
;
1057 static int mips_m4k_target_create(struct target
*target
, Jim_Interp
*interp
)
1059 struct mips_m4k_common
*mips_m4k
= calloc(1, sizeof(struct mips_m4k_common
));
1061 mips_m4k_init_arch_info(target
, mips_m4k
, target
->tap
);
1066 static int mips_m4k_examine(struct target
*target
)
1069 struct mips_m4k_common
*mips_m4k
= target_to_m4k(target
);
1070 struct mips_ejtag
*ejtag_info
= &mips_m4k
->mips32
.ejtag_info
;
1071 uint32_t idcode
= 0;
1073 if (!target_was_examined(target
)) {
1074 retval
= mips_ejtag_get_idcode(ejtag_info
, &idcode
);
1075 if (retval
!= ERROR_OK
)
1077 ejtag_info
->idcode
= idcode
;
1079 if (((idcode
>> 1) & 0x7FF) == 0x29) {
1080 /* we are using a pic32mx so select ejtag port
1081 * as it is not selected by default */
1082 mips_ejtag_set_instr(ejtag_info
, MTAP_SW_ETAP
);
1083 LOG_DEBUG("PIC32MX Detected - using EJTAG Interface");
1084 mips_m4k
->is_pic32mx
= true;
1088 /* init rest of ejtag interface */
1089 retval
= mips_ejtag_init(ejtag_info
);
1090 if (retval
!= ERROR_OK
)
1093 retval
= mips32_examine(target
);
1094 if (retval
!= ERROR_OK
)
1100 static int mips_m4k_bulk_write_memory(struct target
*target
, uint32_t address
,
1101 uint32_t count
, const uint8_t *buffer
)
1103 struct mips32_common
*mips32
= target_to_mips32(target
);
1104 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
1108 LOG_DEBUG("address: 0x%8.8" PRIx32
", count: 0x%8.8" PRIx32
"", address
, count
);
1110 /* check alignment */
1112 return ERROR_TARGET_UNALIGNED_ACCESS
;
1114 if (mips32
->fast_data_area
== NULL
) {
1115 /* Get memory for block write handler
1116 * we preserve this area between calls and gain a speed increase
1117 * of about 3kb/sec when writing flash
1118 * this will be released/nulled by the system when the target is resumed or reset */
1119 retval
= target_alloc_working_area(target
,
1120 MIPS32_FASTDATA_HANDLER_SIZE
,
1121 &mips32
->fast_data_area
);
1122 if (retval
!= ERROR_OK
) {
1123 LOG_ERROR("No working area available");
1127 /* reset fastadata state so the algo get reloaded */
1128 ejtag_info
->fast_access_save
= -1;
1131 /* mips32_pracc_fastdata_xfer requires uint32_t in host endianness, */
1132 /* but byte array represents target endianness */
1134 t
= malloc(count
* sizeof(uint32_t));
1136 LOG_ERROR("Out of memory");
1140 target_buffer_get_u32_array(target
, buffer
, count
, t
);
1142 retval
= mips32_pracc_fastdata_xfer(ejtag_info
, mips32
->fast_data_area
, write_t
, address
,
1148 if (retval
!= ERROR_OK
)
1149 LOG_ERROR("Fastdata access Failed");
1154 static int mips_m4k_verify_pointer(struct command_context
*cmd_ctx
,
1155 struct mips_m4k_common
*mips_m4k
)
1157 if (mips_m4k
->common_magic
!= MIPSM4K_COMMON_MAGIC
) {
1158 command_print(cmd_ctx
, "target is not an MIPS_M4K");
1159 return ERROR_TARGET_INVALID
;
1164 COMMAND_HANDLER(mips_m4k_handle_cp0_command
)
1167 struct target
*target
= get_current_target(CMD_CTX
);
1168 struct mips_m4k_common
*mips_m4k
= target_to_m4k(target
);
1169 struct mips_ejtag
*ejtag_info
= &mips_m4k
->mips32
.ejtag_info
;
1171 retval
= mips_m4k_verify_pointer(CMD_CTX
, mips_m4k
);
1172 if (retval
!= ERROR_OK
)
1175 if (target
->state
!= TARGET_HALTED
) {
1176 command_print(CMD_CTX
, "target must be stopped for \"%s\" command", CMD_NAME
);
1180 /* two or more argument, access a single register/select (write if third argument is given) */
1182 return ERROR_COMMAND_SYNTAX_ERROR
;
1184 uint32_t cp0_reg
, cp0_sel
;
1185 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], cp0_reg
);
1186 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], cp0_sel
);
1188 if (CMD_ARGC
== 2) {
1190 retval
= mips32_cp0_read(ejtag_info
, &value
, cp0_reg
, cp0_sel
);
1191 if (retval
!= ERROR_OK
) {
1192 command_print(CMD_CTX
,
1193 "couldn't access reg %" PRIi32
,
1197 command_print(CMD_CTX
, "cp0 reg %" PRIi32
", select %" PRIi32
": %8.8" PRIx32
,
1198 cp0_reg
, cp0_sel
, value
);
1200 } else if (CMD_ARGC
== 3) {
1202 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], value
);
1203 retval
= mips32_cp0_write(ejtag_info
, value
, cp0_reg
, cp0_sel
);
1204 if (retval
!= ERROR_OK
) {
1205 command_print(CMD_CTX
,
1206 "couldn't access cp0 reg %" PRIi32
", select %" PRIi32
,
1210 command_print(CMD_CTX
, "cp0 reg %" PRIi32
", select %" PRIi32
": %8.8" PRIx32
,
1211 cp0_reg
, cp0_sel
, value
);
1218 COMMAND_HANDLER(mips_m4k_handle_smp_off_command
)
1220 struct target
*target
= get_current_target(CMD_CTX
);
1221 /* check target is an smp target */
1222 struct target_list
*head
;
1223 struct target
*curr
;
1224 head
= target
->head
;
1226 if (head
!= (struct target_list
*)NULL
) {
1227 while (head
!= (struct target_list
*)NULL
) {
1228 curr
= head
->target
;
1232 /* fixes the target display to the debugger */
1233 target
->gdb_service
->target
= target
;
1238 COMMAND_HANDLER(mips_m4k_handle_smp_on_command
)
1240 struct target
*target
= get_current_target(CMD_CTX
);
1241 struct target_list
*head
;
1242 struct target
*curr
;
1243 head
= target
->head
;
1244 if (head
!= (struct target_list
*)NULL
) {
1246 while (head
!= (struct target_list
*)NULL
) {
1247 curr
= head
->target
;
1255 COMMAND_HANDLER(mips_m4k_handle_smp_gdb_command
)
1257 struct target
*target
= get_current_target(CMD_CTX
);
1258 int retval
= ERROR_OK
;
1259 struct target_list
*head
;
1260 head
= target
->head
;
1261 if (head
!= (struct target_list
*)NULL
) {
1262 if (CMD_ARGC
== 1) {
1264 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], coreid
);
1265 if (ERROR_OK
!= retval
)
1267 target
->gdb_service
->core
[1] = coreid
;
1270 command_print(CMD_CTX
, "gdb coreid %d -> %d", target
->gdb_service
->core
[0]
1271 , target
->gdb_service
->core
[1]);
1276 COMMAND_HANDLER(mips_m4k_handle_scan_delay_command
)
1278 struct target
*target
= get_current_target(CMD_CTX
);
1279 struct mips_m4k_common
*mips_m4k
= target_to_m4k(target
);
1280 struct mips_ejtag
*ejtag_info
= &mips_m4k
->mips32
.ejtag_info
;
1283 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], ejtag_info
->scan_delay
);
1284 else if (CMD_ARGC
> 1)
1285 return ERROR_COMMAND_SYNTAX_ERROR
;
1287 command_print(CMD_CTX
, "scan delay: %d nsec", ejtag_info
->scan_delay
);
1288 if (ejtag_info
->scan_delay
>= 20000000) {
1289 ejtag_info
->mode
= 0;
1290 command_print(CMD_CTX
, "running in legacy mode");
1292 ejtag_info
->mode
= 1;
1293 command_print(CMD_CTX
, "running in fast queued mode");
1299 static const struct command_registration mips_m4k_exec_command_handlers
[] = {
1302 .handler
= mips_m4k_handle_cp0_command
,
1303 .mode
= COMMAND_EXEC
,
1304 .usage
= "regnum [value]",
1305 .help
= "display/modify cp0 register",
1309 .handler
= mips_m4k_handle_smp_off_command
,
1310 .mode
= COMMAND_EXEC
,
1311 .help
= "Stop smp handling",
1316 .handler
= mips_m4k_handle_smp_on_command
,
1317 .mode
= COMMAND_EXEC
,
1318 .help
= "Restart smp handling",
1323 .handler
= mips_m4k_handle_smp_gdb_command
,
1324 .mode
= COMMAND_EXEC
,
1325 .help
= "display/fix current core played to gdb",
1329 .name
= "scan_delay",
1330 .handler
= mips_m4k_handle_scan_delay_command
,
1331 .mode
= COMMAND_ANY
,
1332 .help
= "display/set scan delay in nano seconds",
1335 COMMAND_REGISTRATION_DONE
1338 const struct command_registration mips_m4k_command_handlers
[] = {
1340 .chain
= mips32_command_handlers
,
1344 .mode
= COMMAND_ANY
,
1345 .help
= "mips_m4k command group",
1347 .chain
= mips_m4k_exec_command_handlers
,
1349 COMMAND_REGISTRATION_DONE
1352 struct target_type mips_m4k_target
= {
1355 .poll
= mips_m4k_poll
,
1356 .arch_state
= mips32_arch_state
,
1358 .target_request_data
= NULL
,
1360 .halt
= mips_m4k_halt
,
1361 .resume
= mips_m4k_resume
,
1362 .step
= mips_m4k_step
,
1364 .assert_reset
= mips_m4k_assert_reset
,
1365 .deassert_reset
= mips_m4k_deassert_reset
,
1366 .soft_reset_halt
= mips_m4k_soft_reset_halt
,
1368 .get_gdb_reg_list
= mips32_get_gdb_reg_list
,
1370 .read_memory
= mips_m4k_read_memory
,
1371 .write_memory
= mips_m4k_write_memory
,
1372 .checksum_memory
= mips32_checksum_memory
,
1373 .blank_check_memory
= mips32_blank_check_memory
,
1375 .run_algorithm
= mips32_run_algorithm
,
1377 .add_breakpoint
= mips_m4k_add_breakpoint
,
1378 .remove_breakpoint
= mips_m4k_remove_breakpoint
,
1379 .add_watchpoint
= mips_m4k_add_watchpoint
,
1380 .remove_watchpoint
= mips_m4k_remove_watchpoint
,
1382 .commands
= mips_m4k_command_handlers
,
1383 .target_create
= mips_m4k_target_create
,
1384 .init_target
= mips_m4k_init_target
,
1385 .examine
= mips_m4k_examine
,