1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
59 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
60 uint32_t size
, uint8_t *buffer
);
61 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
62 uint32_t size
, const uint8_t *buffer
);
63 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
64 int argc
, Jim_Obj
* const *argv
);
65 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 extern struct target_type arm7tdmi_target
;
71 extern struct target_type arm720t_target
;
72 extern struct target_type arm9tdmi_target
;
73 extern struct target_type arm920t_target
;
74 extern struct target_type arm966e_target
;
75 extern struct target_type arm946e_target
;
76 extern struct target_type arm926ejs_target
;
77 extern struct target_type fa526_target
;
78 extern struct target_type feroceon_target
;
79 extern struct target_type dragonite_target
;
80 extern struct target_type xscale_target
;
81 extern struct target_type cortexm3_target
;
82 extern struct target_type cortexa8_target
;
83 extern struct target_type cortexr4_target
;
84 extern struct target_type arm11_target
;
85 extern struct target_type mips_m4k_target
;
86 extern struct target_type avr_target
;
87 extern struct target_type dsp563xx_target
;
88 extern struct target_type dsp5680xx_target
;
89 extern struct target_type testee_target
;
90 extern struct target_type avr32_ap7k_target
;
91 extern struct target_type hla_target
;
92 extern struct target_type nds32_v2_target
;
93 extern struct target_type nds32_v3_target
;
94 extern struct target_type nds32_v3m_target
;
96 static struct target_type
*target_types
[] = {
125 struct target
*all_targets
;
126 static struct target_event_callback
*target_event_callbacks
;
127 static struct target_timer_callback
*target_timer_callbacks
;
128 static const int polling_interval
= 100;
130 static const Jim_Nvp nvp_assert
[] = {
131 { .name
= "assert", NVP_ASSERT
},
132 { .name
= "deassert", NVP_DEASSERT
},
133 { .name
= "T", NVP_ASSERT
},
134 { .name
= "F", NVP_DEASSERT
},
135 { .name
= "t", NVP_ASSERT
},
136 { .name
= "f", NVP_DEASSERT
},
137 { .name
= NULL
, .value
= -1 }
140 static const Jim_Nvp nvp_error_target
[] = {
141 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
142 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
143 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
144 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
145 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
146 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
147 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
148 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
149 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
150 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
151 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
152 { .value
= -1, .name
= NULL
}
155 static const char *target_strerror_safe(int err
)
159 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
166 static const Jim_Nvp nvp_target_event
[] = {
168 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
169 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
170 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
171 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
172 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
174 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
175 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
177 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
178 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
179 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
180 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
181 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
182 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
183 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
184 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
185 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
186 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
187 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
188 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
190 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
191 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
193 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
194 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
196 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
197 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
199 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
200 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
202 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
203 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
205 { .name
= NULL
, .value
= -1 }
208 static const Jim_Nvp nvp_target_state
[] = {
209 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
210 { .name
= "running", .value
= TARGET_RUNNING
},
211 { .name
= "halted", .value
= TARGET_HALTED
},
212 { .name
= "reset", .value
= TARGET_RESET
},
213 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
214 { .name
= NULL
, .value
= -1 },
217 static const Jim_Nvp nvp_target_debug_reason
[] = {
218 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
219 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
220 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
221 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
222 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
223 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
224 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
225 { .name
= NULL
, .value
= -1 },
228 static const Jim_Nvp nvp_target_endian
[] = {
229 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
230 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
231 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
232 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
233 { .name
= NULL
, .value
= -1 },
236 static const Jim_Nvp nvp_reset_modes
[] = {
237 { .name
= "unknown", .value
= RESET_UNKNOWN
},
238 { .name
= "run" , .value
= RESET_RUN
},
239 { .name
= "halt" , .value
= RESET_HALT
},
240 { .name
= "init" , .value
= RESET_INIT
},
241 { .name
= NULL
, .value
= -1 },
244 const char *debug_reason_name(struct target
*t
)
248 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
249 t
->debug_reason
)->name
;
251 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
252 cp
= "(*BUG*unknown*BUG*)";
257 const char *target_state_name(struct target
*t
)
260 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
262 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
263 cp
= "(*BUG*unknown*BUG*)";
268 /* determine the number of the new target */
269 static int new_target_number(void)
274 /* number is 0 based */
278 if (x
< t
->target_number
)
279 x
= t
->target_number
;
285 /* read a uint32_t from a buffer in target memory endianness */
286 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
288 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
289 return le_to_h_u32(buffer
);
291 return be_to_h_u32(buffer
);
294 /* read a uint24_t from a buffer in target memory endianness */
295 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
297 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
298 return le_to_h_u24(buffer
);
300 return be_to_h_u24(buffer
);
303 /* read a uint16_t from a buffer in target memory endianness */
304 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
306 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
307 return le_to_h_u16(buffer
);
309 return be_to_h_u16(buffer
);
312 /* read a uint8_t from a buffer in target memory endianness */
313 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
315 return *buffer
& 0x0ff;
318 /* write a uint32_t to a buffer in target memory endianness */
319 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
321 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
322 h_u32_to_le(buffer
, value
);
324 h_u32_to_be(buffer
, value
);
327 /* write a uint24_t to a buffer in target memory endianness */
328 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
330 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
331 h_u24_to_le(buffer
, value
);
333 h_u24_to_be(buffer
, value
);
336 /* write a uint16_t to a buffer in target memory endianness */
337 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
339 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
340 h_u16_to_le(buffer
, value
);
342 h_u16_to_be(buffer
, value
);
345 /* write a uint8_t to a buffer in target memory endianness */
346 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
351 /* write a uint32_t array to a buffer in target memory endianness */
352 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
355 for (i
= 0; i
< count
; i
++)
356 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
359 /* write a uint16_t array to a buffer in target memory endianness */
360 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
363 for (i
= 0; i
< count
; i
++)
364 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
367 /* write a uint32_t array to a buffer in target memory endianness */
368 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
371 for (i
= 0; i
< count
; i
++)
372 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
375 /* write a uint16_t array to a buffer in target memory endianness */
376 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
379 for (i
= 0; i
< count
; i
++)
380 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
383 /* return a pointer to a configured target; id is name or number */
384 struct target
*get_target(const char *id
)
386 struct target
*target
;
388 /* try as tcltarget name */
389 for (target
= all_targets
; target
; target
= target
->next
) {
390 if (target_name(target
) == NULL
)
392 if (strcmp(id
, target_name(target
)) == 0)
396 /* It's OK to remove this fallback sometime after August 2010 or so */
398 /* no match, try as number */
400 if (parse_uint(id
, &num
) != ERROR_OK
)
403 for (target
= all_targets
; target
; target
= target
->next
) {
404 if (target
->target_number
== (int)num
) {
405 LOG_WARNING("use '%s' as target identifier, not '%u'",
406 target_name(target
), num
);
414 /* returns a pointer to the n-th configured target */
415 static struct target
*get_target_by_num(int num
)
417 struct target
*target
= all_targets
;
420 if (target
->target_number
== num
)
422 target
= target
->next
;
428 struct target
*get_current_target(struct command_context
*cmd_ctx
)
430 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
432 if (target
== NULL
) {
433 LOG_ERROR("BUG: current_target out of bounds");
440 int target_poll(struct target
*target
)
444 /* We can't poll until after examine */
445 if (!target_was_examined(target
)) {
446 /* Fail silently lest we pollute the log */
450 retval
= target
->type
->poll(target
);
451 if (retval
!= ERROR_OK
)
454 if (target
->halt_issued
) {
455 if (target
->state
== TARGET_HALTED
)
456 target
->halt_issued
= false;
458 long long t
= timeval_ms() - target
->halt_issued_time
;
460 target
->halt_issued
= false;
461 LOG_INFO("Halt timed out, wake up GDB.");
462 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
470 int target_halt(struct target
*target
)
473 /* We can't poll until after examine */
474 if (!target_was_examined(target
)) {
475 LOG_ERROR("Target not examined yet");
479 retval
= target
->type
->halt(target
);
480 if (retval
!= ERROR_OK
)
483 target
->halt_issued
= true;
484 target
->halt_issued_time
= timeval_ms();
490 * Make the target (re)start executing using its saved execution
491 * context (possibly with some modifications).
493 * @param target Which target should start executing.
494 * @param current True to use the target's saved program counter instead
495 * of the address parameter
496 * @param address Optionally used as the program counter.
497 * @param handle_breakpoints True iff breakpoints at the resumption PC
498 * should be skipped. (For example, maybe execution was stopped by
499 * such a breakpoint, in which case it would be counterprodutive to
501 * @param debug_execution False if all working areas allocated by OpenOCD
502 * should be released and/or restored to their original contents.
503 * (This would for example be true to run some downloaded "helper"
504 * algorithm code, which resides in one such working buffer and uses
505 * another for data storage.)
507 * @todo Resolve the ambiguity about what the "debug_execution" flag
508 * signifies. For example, Target implementations don't agree on how
509 * it relates to invalidation of the register cache, or to whether
510 * breakpoints and watchpoints should be enabled. (It would seem wrong
511 * to enable breakpoints when running downloaded "helper" algorithms
512 * (debug_execution true), since the breakpoints would be set to match
513 * target firmware being debugged, not the helper algorithm.... and
514 * enabling them could cause such helpers to malfunction (for example,
515 * by overwriting data with a breakpoint instruction. On the other
516 * hand the infrastructure for running such helpers might use this
517 * procedure but rely on hardware breakpoint to detect termination.)
519 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
523 /* We can't poll until after examine */
524 if (!target_was_examined(target
)) {
525 LOG_ERROR("Target not examined yet");
529 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
531 /* note that resume *must* be asynchronous. The CPU can halt before
532 * we poll. The CPU can even halt at the current PC as a result of
533 * a software breakpoint being inserted by (a bug?) the application.
535 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
536 if (retval
!= ERROR_OK
)
539 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
544 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
549 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
550 if (n
->name
== NULL
) {
551 LOG_ERROR("invalid reset mode");
555 /* disable polling during reset to make reset event scripts
556 * more predictable, i.e. dr/irscan & pathmove in events will
557 * not have JTAG operations injected into the middle of a sequence.
559 bool save_poll
= jtag_poll_get_enabled();
561 jtag_poll_set_enabled(false);
563 sprintf(buf
, "ocd_process_reset %s", n
->name
);
564 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
566 jtag_poll_set_enabled(save_poll
);
568 if (retval
!= JIM_OK
) {
569 Jim_MakeErrorMessage(cmd_ctx
->interp
);
570 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
574 /* We want any events to be processed before the prompt */
575 retval
= target_call_timer_callbacks_now();
577 struct target
*target
;
578 for (target
= all_targets
; target
; target
= target
->next
)
579 target
->type
->check_reset(target
);
584 static int identity_virt2phys(struct target
*target
,
585 uint32_t virtual, uint32_t *physical
)
591 static int no_mmu(struct target
*target
, int *enabled
)
597 static int default_examine(struct target
*target
)
599 target_set_examined(target
);
603 /* no check by default */
604 static int default_check_reset(struct target
*target
)
609 int target_examine_one(struct target
*target
)
611 return target
->type
->examine(target
);
614 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
616 struct target
*target
= priv
;
618 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
621 jtag_unregister_event_callback(jtag_enable_callback
, target
);
623 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
625 int retval
= target_examine_one(target
);
626 if (retval
!= ERROR_OK
)
629 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
634 /* Targets that correctly implement init + examine, i.e.
635 * no communication with target during init:
639 int target_examine(void)
641 int retval
= ERROR_OK
;
642 struct target
*target
;
644 for (target
= all_targets
; target
; target
= target
->next
) {
645 /* defer examination, but don't skip it */
646 if (!target
->tap
->enabled
) {
647 jtag_register_event_callback(jtag_enable_callback
,
652 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
654 retval
= target_examine_one(target
);
655 if (retval
!= ERROR_OK
)
658 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
663 const char *target_type_name(struct target
*target
)
665 return target
->type
->name
;
668 static int target_soft_reset_halt(struct target
*target
)
670 if (!target_was_examined(target
)) {
671 LOG_ERROR("Target not examined yet");
674 if (!target
->type
->soft_reset_halt
) {
675 LOG_ERROR("Target %s does not support soft_reset_halt",
676 target_name(target
));
679 return target
->type
->soft_reset_halt(target
);
683 * Downloads a target-specific native code algorithm to the target,
684 * and executes it. * Note that some targets may need to set up, enable,
685 * and tear down a breakpoint (hard or * soft) to detect algorithm
686 * termination, while others may support lower overhead schemes where
687 * soft breakpoints embedded in the algorithm automatically terminate the
690 * @param target used to run the algorithm
691 * @param arch_info target-specific description of the algorithm.
693 int target_run_algorithm(struct target
*target
,
694 int num_mem_params
, struct mem_param
*mem_params
,
695 int num_reg_params
, struct reg_param
*reg_param
,
696 uint32_t entry_point
, uint32_t exit_point
,
697 int timeout_ms
, void *arch_info
)
699 int retval
= ERROR_FAIL
;
701 if (!target_was_examined(target
)) {
702 LOG_ERROR("Target not examined yet");
705 if (!target
->type
->run_algorithm
) {
706 LOG_ERROR("Target type '%s' does not support %s",
707 target_type_name(target
), __func__
);
711 target
->running_alg
= true;
712 retval
= target
->type
->run_algorithm(target
,
713 num_mem_params
, mem_params
,
714 num_reg_params
, reg_param
,
715 entry_point
, exit_point
, timeout_ms
, arch_info
);
716 target
->running_alg
= false;
723 * Downloads a target-specific native code algorithm to the target,
724 * executes and leaves it running.
726 * @param target used to run the algorithm
727 * @param arch_info target-specific description of the algorithm.
729 int target_start_algorithm(struct target
*target
,
730 int num_mem_params
, struct mem_param
*mem_params
,
731 int num_reg_params
, struct reg_param
*reg_params
,
732 uint32_t entry_point
, uint32_t exit_point
,
735 int retval
= ERROR_FAIL
;
737 if (!target_was_examined(target
)) {
738 LOG_ERROR("Target not examined yet");
741 if (!target
->type
->start_algorithm
) {
742 LOG_ERROR("Target type '%s' does not support %s",
743 target_type_name(target
), __func__
);
746 if (target
->running_alg
) {
747 LOG_ERROR("Target is already running an algorithm");
751 target
->running_alg
= true;
752 retval
= target
->type
->start_algorithm(target
,
753 num_mem_params
, mem_params
,
754 num_reg_params
, reg_params
,
755 entry_point
, exit_point
, arch_info
);
762 * Waits for an algorithm started with target_start_algorithm() to complete.
764 * @param target used to run the algorithm
765 * @param arch_info target-specific description of the algorithm.
767 int target_wait_algorithm(struct target
*target
,
768 int num_mem_params
, struct mem_param
*mem_params
,
769 int num_reg_params
, struct reg_param
*reg_params
,
770 uint32_t exit_point
, int timeout_ms
,
773 int retval
= ERROR_FAIL
;
775 if (!target
->type
->wait_algorithm
) {
776 LOG_ERROR("Target type '%s' does not support %s",
777 target_type_name(target
), __func__
);
780 if (!target
->running_alg
) {
781 LOG_ERROR("Target is not running an algorithm");
785 retval
= target
->type
->wait_algorithm(target
,
786 num_mem_params
, mem_params
,
787 num_reg_params
, reg_params
,
788 exit_point
, timeout_ms
, arch_info
);
789 if (retval
!= ERROR_TARGET_TIMEOUT
)
790 target
->running_alg
= false;
797 * Executes a target-specific native code algorithm in the target.
798 * It differs from target_run_algorithm in that the algorithm is asynchronous.
799 * Because of this it requires an compliant algorithm:
800 * see contrib/loaders/flash/stm32f1x.S for example.
802 * @param target used to run the algorithm
805 int target_run_flash_async_algorithm(struct target
*target
,
806 uint8_t *buffer
, uint32_t count
, int block_size
,
807 int num_mem_params
, struct mem_param
*mem_params
,
808 int num_reg_params
, struct reg_param
*reg_params
,
809 uint32_t buffer_start
, uint32_t buffer_size
,
810 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
815 /* Set up working area. First word is write pointer, second word is read pointer,
816 * rest is fifo data area. */
817 uint32_t wp_addr
= buffer_start
;
818 uint32_t rp_addr
= buffer_start
+ 4;
819 uint32_t fifo_start_addr
= buffer_start
+ 8;
820 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
822 uint32_t wp
= fifo_start_addr
;
823 uint32_t rp
= fifo_start_addr
;
825 /* validate block_size is 2^n */
826 assert(!block_size
|| !(block_size
& (block_size
- 1)));
828 retval
= target_write_u32(target
, wp_addr
, wp
);
829 if (retval
!= ERROR_OK
)
831 retval
= target_write_u32(target
, rp_addr
, rp
);
832 if (retval
!= ERROR_OK
)
835 /* Start up algorithm on target and let it idle while writing the first chunk */
836 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
837 num_reg_params
, reg_params
,
842 if (retval
!= ERROR_OK
) {
843 LOG_ERROR("error starting target flash write algorithm");
849 retval
= target_read_u32(target
, rp_addr
, &rp
);
850 if (retval
!= ERROR_OK
) {
851 LOG_ERROR("failed to get read pointer");
855 LOG_DEBUG("count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
, count
, wp
, rp
);
858 LOG_ERROR("flash write algorithm aborted by target");
859 retval
= ERROR_FLASH_OPERATION_FAILED
;
863 if ((rp
& (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
864 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
868 /* Count the number of bytes available in the fifo without
869 * crossing the wrap around. Make sure to not fill it completely,
870 * because that would make wp == rp and that's the empty condition. */
871 uint32_t thisrun_bytes
;
873 thisrun_bytes
= rp
- wp
- block_size
;
874 else if (rp
> fifo_start_addr
)
875 thisrun_bytes
= fifo_end_addr
- wp
;
877 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
879 if (thisrun_bytes
== 0) {
880 /* Throttle polling a bit if transfer is (much) faster than flash
881 * programming. The exact delay shouldn't matter as long as it's
882 * less than buffer size / flash speed. This is very unlikely to
883 * run when using high latency connections such as USB. */
886 /* to stop an infinite loop on some targets check and increment a timeout
887 * this issue was observed on a stellaris using the new ICDI interface */
888 if (timeout
++ >= 500) {
889 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
890 return ERROR_FLASH_OPERATION_FAILED
;
895 /* reset our timeout */
898 /* Limit to the amount of data we actually want to write */
899 if (thisrun_bytes
> count
* block_size
)
900 thisrun_bytes
= count
* block_size
;
902 /* Write data to fifo */
903 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
904 if (retval
!= ERROR_OK
)
907 /* Update counters and wrap write pointer */
908 buffer
+= thisrun_bytes
;
909 count
-= thisrun_bytes
/ block_size
;
911 if (wp
>= fifo_end_addr
)
912 wp
= fifo_start_addr
;
914 /* Store updated write pointer to target */
915 retval
= target_write_u32(target
, wp_addr
, wp
);
916 if (retval
!= ERROR_OK
)
920 if (retval
!= ERROR_OK
) {
921 /* abort flash write algorithm on target */
922 target_write_u32(target
, wp_addr
, 0);
925 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
926 num_reg_params
, reg_params
,
931 if (retval2
!= ERROR_OK
) {
932 LOG_ERROR("error waiting for target flash write algorithm");
939 int target_read_memory(struct target
*target
,
940 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
942 if (!target_was_examined(target
)) {
943 LOG_ERROR("Target not examined yet");
946 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
949 int target_read_phys_memory(struct target
*target
,
950 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
952 if (!target_was_examined(target
)) {
953 LOG_ERROR("Target not examined yet");
956 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
959 int target_write_memory(struct target
*target
,
960 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
962 if (!target_was_examined(target
)) {
963 LOG_ERROR("Target not examined yet");
966 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
969 int target_write_phys_memory(struct target
*target
,
970 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
972 if (!target_was_examined(target
)) {
973 LOG_ERROR("Target not examined yet");
976 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
979 static int target_bulk_write_memory_default(struct target
*target
,
980 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
982 return target_write_memory(target
, address
, 4, count
, buffer
);
985 int target_add_breakpoint(struct target
*target
,
986 struct breakpoint
*breakpoint
)
988 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
989 LOG_WARNING("target %s is not halted", target_name(target
));
990 return ERROR_TARGET_NOT_HALTED
;
992 return target
->type
->add_breakpoint(target
, breakpoint
);
995 int target_add_context_breakpoint(struct target
*target
,
996 struct breakpoint
*breakpoint
)
998 if (target
->state
!= TARGET_HALTED
) {
999 LOG_WARNING("target %s is not halted", target_name(target
));
1000 return ERROR_TARGET_NOT_HALTED
;
1002 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1005 int target_add_hybrid_breakpoint(struct target
*target
,
1006 struct breakpoint
*breakpoint
)
1008 if (target
->state
!= TARGET_HALTED
) {
1009 LOG_WARNING("target %s is not halted", target_name(target
));
1010 return ERROR_TARGET_NOT_HALTED
;
1012 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1015 int target_remove_breakpoint(struct target
*target
,
1016 struct breakpoint
*breakpoint
)
1018 return target
->type
->remove_breakpoint(target
, breakpoint
);
1021 int target_add_watchpoint(struct target
*target
,
1022 struct watchpoint
*watchpoint
)
1024 if (target
->state
!= TARGET_HALTED
) {
1025 LOG_WARNING("target %s is not halted", target_name(target
));
1026 return ERROR_TARGET_NOT_HALTED
;
1028 return target
->type
->add_watchpoint(target
, watchpoint
);
1030 int target_remove_watchpoint(struct target
*target
,
1031 struct watchpoint
*watchpoint
)
1033 return target
->type
->remove_watchpoint(target
, watchpoint
);
1036 int target_get_gdb_reg_list(struct target
*target
,
1037 struct reg
**reg_list
[], int *reg_list_size
)
1039 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
1041 int target_step(struct target
*target
,
1042 int current
, uint32_t address
, int handle_breakpoints
)
1044 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1048 * Reset the @c examined flag for the given target.
1049 * Pure paranoia -- targets are zeroed on allocation.
1051 static void target_reset_examined(struct target
*target
)
1053 target
->examined
= false;
1056 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
1057 uint32_t size
, uint32_t count
, uint8_t *buffer
)
1059 LOG_ERROR("Not implemented: %s", __func__
);
1063 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
1064 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1066 LOG_ERROR("Not implemented: %s", __func__
);
1070 static int handle_target(void *priv
);
1072 static int target_init_one(struct command_context
*cmd_ctx
,
1073 struct target
*target
)
1075 target_reset_examined(target
);
1077 struct target_type
*type
= target
->type
;
1078 if (type
->examine
== NULL
)
1079 type
->examine
= default_examine
;
1081 if (type
->check_reset
== NULL
)
1082 type
->check_reset
= default_check_reset
;
1084 assert(type
->init_target
!= NULL
);
1086 int retval
= type
->init_target(cmd_ctx
, target
);
1087 if (ERROR_OK
!= retval
) {
1088 LOG_ERROR("target '%s' init failed", target_name(target
));
1092 /* Sanity-check MMU support ... stub in what we must, to help
1093 * implement it in stages, but warn if we need to do so.
1096 if (type
->write_phys_memory
== NULL
) {
1097 LOG_ERROR("type '%s' is missing write_phys_memory",
1099 type
->write_phys_memory
= err_write_phys_memory
;
1101 if (type
->read_phys_memory
== NULL
) {
1102 LOG_ERROR("type '%s' is missing read_phys_memory",
1104 type
->read_phys_memory
= err_read_phys_memory
;
1106 if (type
->virt2phys
== NULL
) {
1107 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1108 type
->virt2phys
= identity_virt2phys
;
1111 /* Make sure no-MMU targets all behave the same: make no
1112 * distinction between physical and virtual addresses, and
1113 * ensure that virt2phys() is always an identity mapping.
1115 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1116 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1119 type
->write_phys_memory
= type
->write_memory
;
1120 type
->read_phys_memory
= type
->read_memory
;
1121 type
->virt2phys
= identity_virt2phys
;
1124 if (target
->type
->read_buffer
== NULL
)
1125 target
->type
->read_buffer
= target_read_buffer_default
;
1127 if (target
->type
->write_buffer
== NULL
)
1128 target
->type
->write_buffer
= target_write_buffer_default
;
1130 if (target
->type
->bulk_write_memory
== NULL
)
1131 target
->type
->bulk_write_memory
= target_bulk_write_memory_default
;
1136 static int target_init(struct command_context
*cmd_ctx
)
1138 struct target
*target
;
1141 for (target
= all_targets
; target
; target
= target
->next
) {
1142 retval
= target_init_one(cmd_ctx
, target
);
1143 if (ERROR_OK
!= retval
)
1150 retval
= target_register_user_commands(cmd_ctx
);
1151 if (ERROR_OK
!= retval
)
1154 retval
= target_register_timer_callback(&handle_target
,
1155 polling_interval
, 1, cmd_ctx
->interp
);
1156 if (ERROR_OK
!= retval
)
1162 COMMAND_HANDLER(handle_target_init_command
)
1167 return ERROR_COMMAND_SYNTAX_ERROR
;
1169 static bool target_initialized
;
1170 if (target_initialized
) {
1171 LOG_INFO("'target init' has already been called");
1174 target_initialized
= true;
1176 retval
= command_run_line(CMD_CTX
, "init_targets");
1177 if (ERROR_OK
!= retval
)
1180 retval
= command_run_line(CMD_CTX
, "init_board");
1181 if (ERROR_OK
!= retval
)
1184 LOG_DEBUG("Initializing targets...");
1185 return target_init(CMD_CTX
);
1188 int target_register_event_callback(int (*callback
)(struct target
*target
,
1189 enum target_event event
, void *priv
), void *priv
)
1191 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1193 if (callback
== NULL
)
1194 return ERROR_COMMAND_SYNTAX_ERROR
;
1197 while ((*callbacks_p
)->next
)
1198 callbacks_p
= &((*callbacks_p
)->next
);
1199 callbacks_p
= &((*callbacks_p
)->next
);
1202 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1203 (*callbacks_p
)->callback
= callback
;
1204 (*callbacks_p
)->priv
= priv
;
1205 (*callbacks_p
)->next
= NULL
;
1210 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1212 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1215 if (callback
== NULL
)
1216 return ERROR_COMMAND_SYNTAX_ERROR
;
1219 while ((*callbacks_p
)->next
)
1220 callbacks_p
= &((*callbacks_p
)->next
);
1221 callbacks_p
= &((*callbacks_p
)->next
);
1224 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1225 (*callbacks_p
)->callback
= callback
;
1226 (*callbacks_p
)->periodic
= periodic
;
1227 (*callbacks_p
)->time_ms
= time_ms
;
1229 gettimeofday(&now
, NULL
);
1230 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1231 time_ms
-= (time_ms
% 1000);
1232 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1233 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1234 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1235 (*callbacks_p
)->when
.tv_sec
+= 1;
1238 (*callbacks_p
)->priv
= priv
;
1239 (*callbacks_p
)->next
= NULL
;
1244 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1245 enum target_event event
, void *priv
), void *priv
)
1247 struct target_event_callback
**p
= &target_event_callbacks
;
1248 struct target_event_callback
*c
= target_event_callbacks
;
1250 if (callback
== NULL
)
1251 return ERROR_COMMAND_SYNTAX_ERROR
;
1254 struct target_event_callback
*next
= c
->next
;
1255 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1267 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1269 struct target_timer_callback
**p
= &target_timer_callbacks
;
1270 struct target_timer_callback
*c
= target_timer_callbacks
;
1272 if (callback
== NULL
)
1273 return ERROR_COMMAND_SYNTAX_ERROR
;
1276 struct target_timer_callback
*next
= c
->next
;
1277 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1289 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1291 struct target_event_callback
*callback
= target_event_callbacks
;
1292 struct target_event_callback
*next_callback
;
1294 if (event
== TARGET_EVENT_HALTED
) {
1295 /* execute early halted first */
1296 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1299 LOG_DEBUG("target event %i (%s)", event
,
1300 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1302 target_handle_event(target
, event
);
1305 next_callback
= callback
->next
;
1306 callback
->callback(target
, event
, callback
->priv
);
1307 callback
= next_callback
;
1313 static int target_timer_callback_periodic_restart(
1314 struct target_timer_callback
*cb
, struct timeval
*now
)
1316 int time_ms
= cb
->time_ms
;
1317 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1318 time_ms
-= (time_ms
% 1000);
1319 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1320 if (cb
->when
.tv_usec
> 1000000) {
1321 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1322 cb
->when
.tv_sec
+= 1;
1327 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1328 struct timeval
*now
)
1330 cb
->callback(cb
->priv
);
1333 return target_timer_callback_periodic_restart(cb
, now
);
1335 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1338 static int target_call_timer_callbacks_check_time(int checktime
)
1343 gettimeofday(&now
, NULL
);
1345 struct target_timer_callback
*callback
= target_timer_callbacks
;
1347 /* cleaning up may unregister and free this callback */
1348 struct target_timer_callback
*next_callback
= callback
->next
;
1350 bool call_it
= callback
->callback
&&
1351 ((!checktime
&& callback
->periodic
) ||
1352 now
.tv_sec
> callback
->when
.tv_sec
||
1353 (now
.tv_sec
== callback
->when
.tv_sec
&&
1354 now
.tv_usec
>= callback
->when
.tv_usec
));
1357 int retval
= target_call_timer_callback(callback
, &now
);
1358 if (retval
!= ERROR_OK
)
1362 callback
= next_callback
;
1368 int target_call_timer_callbacks(void)
1370 return target_call_timer_callbacks_check_time(1);
1373 /* invoke periodic callbacks immediately */
1374 int target_call_timer_callbacks_now(void)
1376 return target_call_timer_callbacks_check_time(0);
1379 /* Prints the working area layout for debug purposes */
1380 static void print_wa_layout(struct target
*target
)
1382 struct working_area
*c
= target
->working_areas
;
1385 LOG_DEBUG("%c%c 0x%08"PRIx32
"-0x%08"PRIx32
" (%"PRIu32
" bytes)",
1386 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1387 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1392 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1393 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1395 assert(area
->free
); /* Shouldn't split an allocated area */
1396 assert(size
<= area
->size
); /* Caller should guarantee this */
1398 /* Split only if not already the right size */
1399 if (size
< area
->size
) {
1400 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1405 new_wa
->next
= area
->next
;
1406 new_wa
->size
= area
->size
- size
;
1407 new_wa
->address
= area
->address
+ size
;
1408 new_wa
->backup
= NULL
;
1409 new_wa
->user
= NULL
;
1410 new_wa
->free
= true;
1412 area
->next
= new_wa
;
1415 /* If backup memory was allocated to this area, it has the wrong size
1416 * now so free it and it will be reallocated if/when needed */
1419 area
->backup
= NULL
;
1424 /* Merge all adjacent free areas into one */
1425 static void target_merge_working_areas(struct target
*target
)
1427 struct working_area
*c
= target
->working_areas
;
1429 while (c
&& c
->next
) {
1430 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1432 /* Find two adjacent free areas */
1433 if (c
->free
&& c
->next
->free
) {
1434 /* Merge the last into the first */
1435 c
->size
+= c
->next
->size
;
1437 /* Remove the last */
1438 struct working_area
*to_be_freed
= c
->next
;
1439 c
->next
= c
->next
->next
;
1440 if (to_be_freed
->backup
)
1441 free(to_be_freed
->backup
);
1444 /* If backup memory was allocated to the remaining area, it's has
1445 * the wrong size now */
1456 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1458 /* Reevaluate working area address based on MMU state*/
1459 if (target
->working_areas
== NULL
) {
1463 retval
= target
->type
->mmu(target
, &enabled
);
1464 if (retval
!= ERROR_OK
)
1468 if (target
->working_area_phys_spec
) {
1469 LOG_DEBUG("MMU disabled, using physical "
1470 "address for working memory 0x%08"PRIx32
,
1471 target
->working_area_phys
);
1472 target
->working_area
= target
->working_area_phys
;
1474 LOG_ERROR("No working memory available. "
1475 "Specify -work-area-phys to target.");
1476 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1479 if (target
->working_area_virt_spec
) {
1480 LOG_DEBUG("MMU enabled, using virtual "
1481 "address for working memory 0x%08"PRIx32
,
1482 target
->working_area_virt
);
1483 target
->working_area
= target
->working_area_virt
;
1485 LOG_ERROR("No working memory available. "
1486 "Specify -work-area-virt to target.");
1487 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1491 /* Set up initial working area on first call */
1492 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1494 new_wa
->next
= NULL
;
1495 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1496 new_wa
->address
= target
->working_area
;
1497 new_wa
->backup
= NULL
;
1498 new_wa
->user
= NULL
;
1499 new_wa
->free
= true;
1502 target
->working_areas
= new_wa
;
1505 /* only allocate multiples of 4 byte */
1507 size
= (size
+ 3) & (~3UL);
1509 struct working_area
*c
= target
->working_areas
;
1511 /* Find the first large enough working area */
1513 if (c
->free
&& c
->size
>= size
)
1519 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1521 /* Split the working area into the requested size */
1522 target_split_working_area(c
, size
);
1524 LOG_DEBUG("allocated new working area of %"PRIu32
" bytes at address 0x%08"PRIx32
, size
, c
->address
);
1526 if (target
->backup_working_area
) {
1527 if (c
->backup
== NULL
) {
1528 c
->backup
= malloc(c
->size
);
1529 if (c
->backup
== NULL
)
1533 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1534 if (retval
!= ERROR_OK
)
1538 /* mark as used, and return the new (reused) area */
1545 print_wa_layout(target
);
1550 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1554 retval
= target_alloc_working_area_try(target
, size
, area
);
1555 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1556 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1561 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1563 int retval
= ERROR_OK
;
1565 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1566 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1567 if (retval
!= ERROR_OK
)
1568 LOG_ERROR("failed to restore %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1569 area
->size
, area
->address
);
1575 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1576 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1578 int retval
= ERROR_OK
;
1584 retval
= target_restore_working_area(target
, area
);
1585 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1586 if (retval
!= ERROR_OK
)
1592 LOG_DEBUG("freed %"PRIu32
" bytes of working area at address 0x%08"PRIx32
,
1593 area
->size
, area
->address
);
1595 /* mark user pointer invalid */
1596 /* TODO: Is this really safe? It points to some previous caller's memory.
1597 * How could we know that the area pointer is still in that place and not
1598 * some other vital data? What's the purpose of this, anyway? */
1602 target_merge_working_areas(target
);
1604 print_wa_layout(target
);
1609 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1611 return target_free_working_area_restore(target
, area
, 1);
1614 /* free resources and restore memory, if restoring memory fails,
1615 * free up resources anyway
1617 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1619 struct working_area
*c
= target
->working_areas
;
1621 LOG_DEBUG("freeing all working areas");
1623 /* Loop through all areas, restoring the allocated ones and marking them as free */
1627 target_restore_working_area(target
, c
);
1629 *c
->user
= NULL
; /* Same as above */
1635 /* Run a merge pass to combine all areas into one */
1636 target_merge_working_areas(target
);
1638 print_wa_layout(target
);
1641 void target_free_all_working_areas(struct target
*target
)
1643 target_free_all_working_areas_restore(target
, 1);
1646 /* Find the largest number of bytes that can be allocated */
1647 uint32_t target_get_working_area_avail(struct target
*target
)
1649 struct working_area
*c
= target
->working_areas
;
1650 uint32_t max_size
= 0;
1653 return target
->working_area_size
;
1656 if (c
->free
&& max_size
< c
->size
)
1665 int target_arch_state(struct target
*target
)
1668 if (target
== NULL
) {
1669 LOG_USER("No target has been configured");
1673 LOG_USER("target state: %s", target_state_name(target
));
1675 if (target
->state
!= TARGET_HALTED
)
1678 retval
= target
->type
->arch_state(target
);
1682 /* Single aligned words are guaranteed to use 16 or 32 bit access
1683 * mode respectively, otherwise data is handled as quickly as
1686 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1688 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1689 (int)size
, (unsigned)address
);
1691 if (!target_was_examined(target
)) {
1692 LOG_ERROR("Target not examined yet");
1699 if ((address
+ size
- 1) < address
) {
1700 /* GDB can request this when e.g. PC is 0xfffffffc*/
1701 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1707 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1710 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1712 int retval
= ERROR_OK
;
1714 if (((address
% 2) == 0) && (size
== 2))
1715 return target_write_memory(target
, address
, 2, 1, buffer
);
1717 /* handle unaligned head bytes */
1719 uint32_t unaligned
= 4 - (address
% 4);
1721 if (unaligned
> size
)
1724 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1725 if (retval
!= ERROR_OK
)
1728 buffer
+= unaligned
;
1729 address
+= unaligned
;
1733 /* handle aligned words */
1735 int aligned
= size
- (size
% 4);
1737 /* use bulk writes above a certain limit. This may have to be changed */
1738 if (aligned
> 128) {
1739 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1740 if (retval
!= ERROR_OK
)
1743 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1744 if (retval
!= ERROR_OK
)
1753 /* handle tail writes of less than 4 bytes */
1755 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1756 if (retval
!= ERROR_OK
)
1763 /* Single aligned words are guaranteed to use 16 or 32 bit access
1764 * mode respectively, otherwise data is handled as quickly as
1767 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1769 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1770 (int)size
, (unsigned)address
);
1772 if (!target_was_examined(target
)) {
1773 LOG_ERROR("Target not examined yet");
1780 if ((address
+ size
- 1) < address
) {
1781 /* GDB can request this when e.g. PC is 0xfffffffc*/
1782 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1788 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1791 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1793 int retval
= ERROR_OK
;
1795 if (((address
% 2) == 0) && (size
== 2))
1796 return target_read_memory(target
, address
, 2, 1, buffer
);
1798 /* handle unaligned head bytes */
1800 uint32_t unaligned
= 4 - (address
% 4);
1802 if (unaligned
> size
)
1805 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1806 if (retval
!= ERROR_OK
)
1809 buffer
+= unaligned
;
1810 address
+= unaligned
;
1814 /* handle aligned words */
1816 int aligned
= size
- (size
% 4);
1818 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1819 if (retval
!= ERROR_OK
)
1827 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1829 int aligned
= size
- (size
% 2);
1830 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1831 if (retval
!= ERROR_OK
)
1838 /* handle tail writes of less than 4 bytes */
1840 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1841 if (retval
!= ERROR_OK
)
1848 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1853 uint32_t checksum
= 0;
1854 if (!target_was_examined(target
)) {
1855 LOG_ERROR("Target not examined yet");
1859 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1860 if (retval
!= ERROR_OK
) {
1861 buffer
= malloc(size
);
1862 if (buffer
== NULL
) {
1863 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1864 return ERROR_COMMAND_SYNTAX_ERROR
;
1866 retval
= target_read_buffer(target
, address
, size
, buffer
);
1867 if (retval
!= ERROR_OK
) {
1872 /* convert to target endianness */
1873 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1874 uint32_t target_data
;
1875 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1876 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1879 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1888 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1891 if (!target_was_examined(target
)) {
1892 LOG_ERROR("Target not examined yet");
1896 if (target
->type
->blank_check_memory
== 0)
1897 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1899 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1904 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1906 uint8_t value_buf
[4];
1907 if (!target_was_examined(target
)) {
1908 LOG_ERROR("Target not examined yet");
1912 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1914 if (retval
== ERROR_OK
) {
1915 *value
= target_buffer_get_u32(target
, value_buf
);
1916 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1921 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1928 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1930 uint8_t value_buf
[2];
1931 if (!target_was_examined(target
)) {
1932 LOG_ERROR("Target not examined yet");
1936 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1938 if (retval
== ERROR_OK
) {
1939 *value
= target_buffer_get_u16(target
, value_buf
);
1940 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1945 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1952 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1954 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1955 if (!target_was_examined(target
)) {
1956 LOG_ERROR("Target not examined yet");
1960 if (retval
== ERROR_OK
) {
1961 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1966 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1973 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1976 uint8_t value_buf
[4];
1977 if (!target_was_examined(target
)) {
1978 LOG_ERROR("Target not examined yet");
1982 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1986 target_buffer_set_u32(target
, value_buf
, value
);
1987 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1988 if (retval
!= ERROR_OK
)
1989 LOG_DEBUG("failed: %i", retval
);
1994 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1997 uint8_t value_buf
[2];
1998 if (!target_was_examined(target
)) {
1999 LOG_ERROR("Target not examined yet");
2003 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
2007 target_buffer_set_u16(target
, value_buf
, value
);
2008 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2009 if (retval
!= ERROR_OK
)
2010 LOG_DEBUG("failed: %i", retval
);
2015 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
2018 if (!target_was_examined(target
)) {
2019 LOG_ERROR("Target not examined yet");
2023 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
2026 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2027 if (retval
!= ERROR_OK
)
2028 LOG_DEBUG("failed: %i", retval
);
2033 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2035 struct target
*target
= get_target(name
);
2036 if (target
== NULL
) {
2037 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2040 if (!target
->tap
->enabled
) {
2041 LOG_USER("Target: TAP %s is disabled, "
2042 "can't be the current target\n",
2043 target
->tap
->dotted_name
);
2047 cmd_ctx
->current_target
= target
->target_number
;
2052 COMMAND_HANDLER(handle_targets_command
)
2054 int retval
= ERROR_OK
;
2055 if (CMD_ARGC
== 1) {
2056 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2057 if (retval
== ERROR_OK
) {
2063 struct target
*target
= all_targets
;
2064 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2065 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2070 if (target
->tap
->enabled
)
2071 state
= target_state_name(target
);
2073 state
= "tap-disabled";
2075 if (CMD_CTX
->current_target
== target
->target_number
)
2078 /* keep columns lined up to match the headers above */
2079 command_print(CMD_CTX
,
2080 "%2d%c %-18s %-10s %-6s %-18s %s",
2081 target
->target_number
,
2083 target_name(target
),
2084 target_type_name(target
),
2085 Jim_Nvp_value2name_simple(nvp_target_endian
,
2086 target
->endianness
)->name
,
2087 target
->tap
->dotted_name
,
2089 target
= target
->next
;
2095 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2097 static int powerDropout
;
2098 static int srstAsserted
;
2100 static int runPowerRestore
;
2101 static int runPowerDropout
;
2102 static int runSrstAsserted
;
2103 static int runSrstDeasserted
;
2105 static int sense_handler(void)
2107 static int prevSrstAsserted
;
2108 static int prevPowerdropout
;
2110 int retval
= jtag_power_dropout(&powerDropout
);
2111 if (retval
!= ERROR_OK
)
2115 powerRestored
= prevPowerdropout
&& !powerDropout
;
2117 runPowerRestore
= 1;
2119 long long current
= timeval_ms();
2120 static long long lastPower
;
2121 int waitMore
= lastPower
+ 2000 > current
;
2122 if (powerDropout
&& !waitMore
) {
2123 runPowerDropout
= 1;
2124 lastPower
= current
;
2127 retval
= jtag_srst_asserted(&srstAsserted
);
2128 if (retval
!= ERROR_OK
)
2132 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2134 static long long lastSrst
;
2135 waitMore
= lastSrst
+ 2000 > current
;
2136 if (srstDeasserted
&& !waitMore
) {
2137 runSrstDeasserted
= 1;
2141 if (!prevSrstAsserted
&& srstAsserted
)
2142 runSrstAsserted
= 1;
2144 prevSrstAsserted
= srstAsserted
;
2145 prevPowerdropout
= powerDropout
;
2147 if (srstDeasserted
|| powerRestored
) {
2148 /* Other than logging the event we can't do anything here.
2149 * Issuing a reset is a particularly bad idea as we might
2150 * be inside a reset already.
2157 /* process target state changes */
2158 static int handle_target(void *priv
)
2160 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2161 int retval
= ERROR_OK
;
2163 if (!is_jtag_poll_safe()) {
2164 /* polling is disabled currently */
2168 /* we do not want to recurse here... */
2169 static int recursive
;
2173 /* danger! running these procedures can trigger srst assertions and power dropouts.
2174 * We need to avoid an infinite loop/recursion here and we do that by
2175 * clearing the flags after running these events.
2177 int did_something
= 0;
2178 if (runSrstAsserted
) {
2179 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2180 Jim_Eval(interp
, "srst_asserted");
2183 if (runSrstDeasserted
) {
2184 Jim_Eval(interp
, "srst_deasserted");
2187 if (runPowerDropout
) {
2188 LOG_INFO("Power dropout detected, running power_dropout proc.");
2189 Jim_Eval(interp
, "power_dropout");
2192 if (runPowerRestore
) {
2193 Jim_Eval(interp
, "power_restore");
2197 if (did_something
) {
2198 /* clear detect flags */
2202 /* clear action flags */
2204 runSrstAsserted
= 0;
2205 runSrstDeasserted
= 0;
2206 runPowerRestore
= 0;
2207 runPowerDropout
= 0;
2212 /* Poll targets for state changes unless that's globally disabled.
2213 * Skip targets that are currently disabled.
2215 for (struct target
*target
= all_targets
;
2216 is_jtag_poll_safe() && target
;
2217 target
= target
->next
) {
2218 if (!target
->tap
->enabled
)
2221 if (target
->backoff
.times
> target
->backoff
.count
) {
2222 /* do not poll this time as we failed previously */
2223 target
->backoff
.count
++;
2226 target
->backoff
.count
= 0;
2228 /* only poll target if we've got power and srst isn't asserted */
2229 if (!powerDropout
&& !srstAsserted
) {
2230 /* polling may fail silently until the target has been examined */
2231 retval
= target_poll(target
);
2232 if (retval
!= ERROR_OK
) {
2233 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2234 if (target
->backoff
.times
* polling_interval
< 5000) {
2235 target
->backoff
.times
*= 2;
2236 target
->backoff
.times
++;
2238 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2239 target_name(target
),
2240 target
->backoff
.times
* polling_interval
);
2242 /* Tell GDB to halt the debugger. This allows the user to
2243 * run monitor commands to handle the situation.
2245 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2248 /* Since we succeeded, we reset backoff count */
2249 if (target
->backoff
.times
> 0)
2250 LOG_USER("Polling target %s succeeded again", target_name(target
));
2251 target
->backoff
.times
= 0;
2258 COMMAND_HANDLER(handle_reg_command
)
2260 struct target
*target
;
2261 struct reg
*reg
= NULL
;
2267 target
= get_current_target(CMD_CTX
);
2269 /* list all available registers for the current target */
2270 if (CMD_ARGC
== 0) {
2271 struct reg_cache
*cache
= target
->reg_cache
;
2277 command_print(CMD_CTX
, "===== %s", cache
->name
);
2279 for (i
= 0, reg
= cache
->reg_list
;
2280 i
< cache
->num_regs
;
2281 i
++, reg
++, count
++) {
2282 /* only print cached values if they are valid */
2284 value
= buf_to_str(reg
->value
,
2286 command_print(CMD_CTX
,
2287 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2295 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2300 cache
= cache
->next
;
2306 /* access a single register by its ordinal number */
2307 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2309 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2311 struct reg_cache
*cache
= target
->reg_cache
;
2315 for (i
= 0; i
< cache
->num_regs
; i
++) {
2316 if (count
++ == num
) {
2317 reg
= &cache
->reg_list
[i
];
2323 cache
= cache
->next
;
2327 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2328 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2332 /* access a single register by its name */
2333 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2336 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2341 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2343 /* display a register */
2344 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2345 && (CMD_ARGV
[1][0] <= '9')))) {
2346 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2349 if (reg
->valid
== 0)
2350 reg
->type
->get(reg
);
2351 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2352 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2357 /* set register value */
2358 if (CMD_ARGC
== 2) {
2359 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2362 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2364 reg
->type
->set(reg
, buf
);
2366 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2367 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2375 return ERROR_COMMAND_SYNTAX_ERROR
;
2378 COMMAND_HANDLER(handle_poll_command
)
2380 int retval
= ERROR_OK
;
2381 struct target
*target
= get_current_target(CMD_CTX
);
2383 if (CMD_ARGC
== 0) {
2384 command_print(CMD_CTX
, "background polling: %s",
2385 jtag_poll_get_enabled() ? "on" : "off");
2386 command_print(CMD_CTX
, "TAP: %s (%s)",
2387 target
->tap
->dotted_name
,
2388 target
->tap
->enabled
? "enabled" : "disabled");
2389 if (!target
->tap
->enabled
)
2391 retval
= target_poll(target
);
2392 if (retval
!= ERROR_OK
)
2394 retval
= target_arch_state(target
);
2395 if (retval
!= ERROR_OK
)
2397 } else if (CMD_ARGC
== 1) {
2399 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2400 jtag_poll_set_enabled(enable
);
2402 return ERROR_COMMAND_SYNTAX_ERROR
;
2407 COMMAND_HANDLER(handle_wait_halt_command
)
2410 return ERROR_COMMAND_SYNTAX_ERROR
;
2413 if (1 == CMD_ARGC
) {
2414 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2415 if (ERROR_OK
!= retval
)
2416 return ERROR_COMMAND_SYNTAX_ERROR
;
2417 /* convert seconds (given) to milliseconds (needed) */
2421 struct target
*target
= get_current_target(CMD_CTX
);
2422 return target_wait_state(target
, TARGET_HALTED
, ms
);
2425 /* wait for target state to change. The trick here is to have a low
2426 * latency for short waits and not to suck up all the CPU time
2429 * After 500ms, keep_alive() is invoked
2431 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2434 long long then
= 0, cur
;
2438 retval
= target_poll(target
);
2439 if (retval
!= ERROR_OK
)
2441 if (target
->state
== state
)
2446 then
= timeval_ms();
2447 LOG_DEBUG("waiting for target %s...",
2448 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2454 if ((cur
-then
) > ms
) {
2455 LOG_ERROR("timed out while waiting for target %s",
2456 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2464 COMMAND_HANDLER(handle_halt_command
)
2468 struct target
*target
= get_current_target(CMD_CTX
);
2469 int retval
= target_halt(target
);
2470 if (ERROR_OK
!= retval
)
2473 if (CMD_ARGC
== 1) {
2474 unsigned wait_local
;
2475 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2476 if (ERROR_OK
!= retval
)
2477 return ERROR_COMMAND_SYNTAX_ERROR
;
2482 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2485 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2487 struct target
*target
= get_current_target(CMD_CTX
);
2489 LOG_USER("requesting target halt and executing a soft reset");
2491 target_soft_reset_halt(target
);
2496 COMMAND_HANDLER(handle_reset_command
)
2499 return ERROR_COMMAND_SYNTAX_ERROR
;
2501 enum target_reset_mode reset_mode
= RESET_RUN
;
2502 if (CMD_ARGC
== 1) {
2504 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2505 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2506 return ERROR_COMMAND_SYNTAX_ERROR
;
2507 reset_mode
= n
->value
;
2510 /* reset *all* targets */
2511 return target_process_reset(CMD_CTX
, reset_mode
);
2515 COMMAND_HANDLER(handle_resume_command
)
2519 return ERROR_COMMAND_SYNTAX_ERROR
;
2521 struct target
*target
= get_current_target(CMD_CTX
);
2523 /* with no CMD_ARGV, resume from current pc, addr = 0,
2524 * with one arguments, addr = CMD_ARGV[0],
2525 * handle breakpoints, not debugging */
2527 if (CMD_ARGC
== 1) {
2528 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2532 return target_resume(target
, current
, addr
, 1, 0);
2535 COMMAND_HANDLER(handle_step_command
)
2538 return ERROR_COMMAND_SYNTAX_ERROR
;
2542 /* with no CMD_ARGV, step from current pc, addr = 0,
2543 * with one argument addr = CMD_ARGV[0],
2544 * handle breakpoints, debugging */
2547 if (CMD_ARGC
== 1) {
2548 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2552 struct target
*target
= get_current_target(CMD_CTX
);
2554 return target
->type
->step(target
, current_pc
, addr
, 1);
2557 static void handle_md_output(struct command_context
*cmd_ctx
,
2558 struct target
*target
, uint32_t address
, unsigned size
,
2559 unsigned count
, const uint8_t *buffer
)
2561 const unsigned line_bytecnt
= 32;
2562 unsigned line_modulo
= line_bytecnt
/ size
;
2564 char output
[line_bytecnt
* 4 + 1];
2565 unsigned output_len
= 0;
2567 const char *value_fmt
;
2570 value_fmt
= "%8.8x ";
2573 value_fmt
= "%4.4x ";
2576 value_fmt
= "%2.2x ";
2579 /* "can't happen", caller checked */
2580 LOG_ERROR("invalid memory read size: %u", size
);
2584 for (unsigned i
= 0; i
< count
; i
++) {
2585 if (i
% line_modulo
== 0) {
2586 output_len
+= snprintf(output
+ output_len
,
2587 sizeof(output
) - output_len
,
2589 (unsigned)(address
+ (i
*size
)));
2593 const uint8_t *value_ptr
= buffer
+ i
* size
;
2596 value
= target_buffer_get_u32(target
, value_ptr
);
2599 value
= target_buffer_get_u16(target
, value_ptr
);
2604 output_len
+= snprintf(output
+ output_len
,
2605 sizeof(output
) - output_len
,
2608 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2609 command_print(cmd_ctx
, "%s", output
);
2615 COMMAND_HANDLER(handle_md_command
)
2618 return ERROR_COMMAND_SYNTAX_ERROR
;
2621 switch (CMD_NAME
[2]) {
2632 return ERROR_COMMAND_SYNTAX_ERROR
;
2635 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2636 int (*fn
)(struct target
*target
,
2637 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2641 fn
= target_read_phys_memory
;
2643 fn
= target_read_memory
;
2644 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2645 return ERROR_COMMAND_SYNTAX_ERROR
;
2648 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2652 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2654 uint8_t *buffer
= calloc(count
, size
);
2656 struct target
*target
= get_current_target(CMD_CTX
);
2657 int retval
= fn(target
, address
, size
, count
, buffer
);
2658 if (ERROR_OK
== retval
)
2659 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2666 typedef int (*target_write_fn
)(struct target
*target
,
2667 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2669 static int target_write_memory_fast(struct target
*target
,
2670 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2672 return target_write_buffer(target
, address
, size
* count
, buffer
);
2675 static int target_fill_mem(struct target
*target
,
2684 /* We have to write in reasonably large chunks to be able
2685 * to fill large memory areas with any sane speed */
2686 const unsigned chunk_size
= 16384;
2687 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2688 if (target_buf
== NULL
) {
2689 LOG_ERROR("Out of memory");
2693 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2694 switch (data_size
) {
2696 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2699 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2702 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2709 int retval
= ERROR_OK
;
2711 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2714 if (current
> chunk_size
)
2715 current
= chunk_size
;
2716 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2717 if (retval
!= ERROR_OK
)
2719 /* avoid GDB timeouts */
2728 COMMAND_HANDLER(handle_mw_command
)
2731 return ERROR_COMMAND_SYNTAX_ERROR
;
2732 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2737 fn
= target_write_phys_memory
;
2739 fn
= target_write_memory_fast
;
2740 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2741 return ERROR_COMMAND_SYNTAX_ERROR
;
2744 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2747 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2751 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2753 struct target
*target
= get_current_target(CMD_CTX
);
2755 switch (CMD_NAME
[2]) {
2766 return ERROR_COMMAND_SYNTAX_ERROR
;
2769 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2772 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2773 uint32_t *min_address
, uint32_t *max_address
)
2775 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2776 return ERROR_COMMAND_SYNTAX_ERROR
;
2778 /* a base address isn't always necessary,
2779 * default to 0x0 (i.e. don't relocate) */
2780 if (CMD_ARGC
>= 2) {
2782 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2783 image
->base_address
= addr
;
2784 image
->base_address_set
= 1;
2786 image
->base_address_set
= 0;
2788 image
->start_address_set
= 0;
2791 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2792 if (CMD_ARGC
== 5) {
2793 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2794 /* use size (given) to find max (required) */
2795 *max_address
+= *min_address
;
2798 if (*min_address
> *max_address
)
2799 return ERROR_COMMAND_SYNTAX_ERROR
;
2804 COMMAND_HANDLER(handle_load_image_command
)
2808 uint32_t image_size
;
2809 uint32_t min_address
= 0;
2810 uint32_t max_address
= 0xffffffff;
2814 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2815 &image
, &min_address
, &max_address
);
2816 if (ERROR_OK
!= retval
)
2819 struct target
*target
= get_current_target(CMD_CTX
);
2821 struct duration bench
;
2822 duration_start(&bench
);
2824 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2829 for (i
= 0; i
< image
.num_sections
; i
++) {
2830 buffer
= malloc(image
.sections
[i
].size
);
2831 if (buffer
== NULL
) {
2832 command_print(CMD_CTX
,
2833 "error allocating buffer for section (%d bytes)",
2834 (int)(image
.sections
[i
].size
));
2838 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2839 if (retval
!= ERROR_OK
) {
2844 uint32_t offset
= 0;
2845 uint32_t length
= buf_cnt
;
2847 /* DANGER!!! beware of unsigned comparision here!!! */
2849 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2850 (image
.sections
[i
].base_address
< max_address
)) {
2852 if (image
.sections
[i
].base_address
< min_address
) {
2853 /* clip addresses below */
2854 offset
+= min_address
-image
.sections
[i
].base_address
;
2858 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2859 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2861 retval
= target_write_buffer(target
,
2862 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2863 if (retval
!= ERROR_OK
) {
2867 image_size
+= length
;
2868 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2869 (unsigned int)length
,
2870 image
.sections
[i
].base_address
+ offset
);
2876 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2877 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2878 "in %fs (%0.3f KiB/s)", image_size
,
2879 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2882 image_close(&image
);
2888 COMMAND_HANDLER(handle_dump_image_command
)
2890 struct fileio fileio
;
2892 int retval
, retvaltemp
;
2893 uint32_t address
, size
;
2894 struct duration bench
;
2895 struct target
*target
= get_current_target(CMD_CTX
);
2898 return ERROR_COMMAND_SYNTAX_ERROR
;
2900 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2901 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2903 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2904 buffer
= malloc(buf_size
);
2908 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2909 if (retval
!= ERROR_OK
) {
2914 duration_start(&bench
);
2917 size_t size_written
;
2918 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2919 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2920 if (retval
!= ERROR_OK
)
2923 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2924 if (retval
!= ERROR_OK
)
2927 size
-= this_run_size
;
2928 address
+= this_run_size
;
2933 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2935 retval
= fileio_size(&fileio
, &filesize
);
2936 if (retval
!= ERROR_OK
)
2938 command_print(CMD_CTX
,
2939 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2940 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2943 retvaltemp
= fileio_close(&fileio
);
2944 if (retvaltemp
!= ERROR_OK
)
2950 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2954 uint32_t image_size
;
2957 uint32_t checksum
= 0;
2958 uint32_t mem_checksum
= 0;
2962 struct target
*target
= get_current_target(CMD_CTX
);
2965 return ERROR_COMMAND_SYNTAX_ERROR
;
2968 LOG_ERROR("no target selected");
2972 struct duration bench
;
2973 duration_start(&bench
);
2975 if (CMD_ARGC
>= 2) {
2977 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2978 image
.base_address
= addr
;
2979 image
.base_address_set
= 1;
2981 image
.base_address_set
= 0;
2982 image
.base_address
= 0x0;
2985 image
.start_address_set
= 0;
2987 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2988 if (retval
!= ERROR_OK
)
2994 for (i
= 0; i
< image
.num_sections
; i
++) {
2995 buffer
= malloc(image
.sections
[i
].size
);
2996 if (buffer
== NULL
) {
2997 command_print(CMD_CTX
,
2998 "error allocating buffer for section (%d bytes)",
2999 (int)(image
.sections
[i
].size
));
3002 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3003 if (retval
!= ERROR_OK
) {
3009 /* calculate checksum of image */
3010 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3011 if (retval
!= ERROR_OK
) {
3016 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3017 if (retval
!= ERROR_OK
) {
3022 if (checksum
!= mem_checksum
) {
3023 /* failed crc checksum, fall back to a binary compare */
3027 LOG_ERROR("checksum mismatch - attempting binary compare");
3029 data
= (uint8_t *)malloc(buf_cnt
);
3031 /* Can we use 32bit word accesses? */
3033 int count
= buf_cnt
;
3034 if ((count
% 4) == 0) {
3038 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3039 if (retval
== ERROR_OK
) {
3041 for (t
= 0; t
< buf_cnt
; t
++) {
3042 if (data
[t
] != buffer
[t
]) {
3043 command_print(CMD_CTX
,
3044 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3046 (unsigned)(t
+ image
.sections
[i
].base_address
),
3049 if (diffs
++ >= 127) {
3050 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3062 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
3063 image
.sections
[i
].base_address
,
3068 image_size
+= buf_cnt
;
3071 command_print(CMD_CTX
, "No more differences found.");
3074 retval
= ERROR_FAIL
;
3075 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3076 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3077 "in %fs (%0.3f KiB/s)", image_size
,
3078 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3081 image_close(&image
);
3086 COMMAND_HANDLER(handle_verify_image_command
)
3088 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
3091 COMMAND_HANDLER(handle_test_image_command
)
3093 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
3096 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3098 struct target
*target
= get_current_target(cmd_ctx
);
3099 struct breakpoint
*breakpoint
= target
->breakpoints
;
3100 while (breakpoint
) {
3101 if (breakpoint
->type
== BKPT_SOFT
) {
3102 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3103 breakpoint
->length
, 16);
3104 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
3105 breakpoint
->address
,
3107 breakpoint
->set
, buf
);
3110 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3111 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3113 breakpoint
->length
, breakpoint
->set
);
3114 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3115 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3116 breakpoint
->address
,
3117 breakpoint
->length
, breakpoint
->set
);
3118 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3121 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
3122 breakpoint
->address
,
3123 breakpoint
->length
, breakpoint
->set
);
3126 breakpoint
= breakpoint
->next
;
3131 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3132 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3134 struct target
*target
= get_current_target(cmd_ctx
);
3137 int retval
= breakpoint_add(target
, addr
, length
, hw
);
3138 if (ERROR_OK
== retval
)
3139 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
3141 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3144 } else if (addr
== 0) {
3145 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3146 if (ERROR_OK
== retval
)
3147 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3149 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3153 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3154 if (ERROR_OK
== retval
)
3155 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3157 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3164 COMMAND_HANDLER(handle_bp_command
)
3173 return handle_bp_command_list(CMD_CTX
);
3177 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3178 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3179 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3182 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3184 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3186 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3189 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3190 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3192 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3193 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3195 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3200 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3201 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3202 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3203 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3206 return ERROR_COMMAND_SYNTAX_ERROR
;
3210 COMMAND_HANDLER(handle_rbp_command
)
3213 return ERROR_COMMAND_SYNTAX_ERROR
;
3216 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3218 struct target
*target
= get_current_target(CMD_CTX
);
3219 breakpoint_remove(target
, addr
);
3224 COMMAND_HANDLER(handle_wp_command
)
3226 struct target
*target
= get_current_target(CMD_CTX
);
3228 if (CMD_ARGC
== 0) {
3229 struct watchpoint
*watchpoint
= target
->watchpoints
;
3231 while (watchpoint
) {
3232 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
3233 ", len: 0x%8.8" PRIx32
3234 ", r/w/a: %i, value: 0x%8.8" PRIx32
3235 ", mask: 0x%8.8" PRIx32
,
3236 watchpoint
->address
,
3238 (int)watchpoint
->rw
,
3241 watchpoint
= watchpoint
->next
;
3246 enum watchpoint_rw type
= WPT_ACCESS
;
3248 uint32_t length
= 0;
3249 uint32_t data_value
= 0x0;
3250 uint32_t data_mask
= 0xffffffff;
3254 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3257 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3260 switch (CMD_ARGV
[2][0]) {
3271 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3272 return ERROR_COMMAND_SYNTAX_ERROR
;
3276 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3277 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3281 return ERROR_COMMAND_SYNTAX_ERROR
;
3284 int retval
= watchpoint_add(target
, addr
, length
, type
,
3285 data_value
, data_mask
);
3286 if (ERROR_OK
!= retval
)
3287 LOG_ERROR("Failure setting watchpoints");
3292 COMMAND_HANDLER(handle_rwp_command
)
3295 return ERROR_COMMAND_SYNTAX_ERROR
;
3298 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3300 struct target
*target
= get_current_target(CMD_CTX
);
3301 watchpoint_remove(target
, addr
);
3307 * Translate a virtual address to a physical address.
3309 * The low-level target implementation must have logged a detailed error
3310 * which is forwarded to telnet/GDB session.
3312 COMMAND_HANDLER(handle_virt2phys_command
)
3315 return ERROR_COMMAND_SYNTAX_ERROR
;
3318 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3321 struct target
*target
= get_current_target(CMD_CTX
);
3322 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3323 if (retval
== ERROR_OK
)
3324 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3329 static void writeData(FILE *f
, const void *data
, size_t len
)
3331 size_t written
= fwrite(data
, 1, len
, f
);
3333 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3336 static void writeLong(FILE *f
, int l
)
3339 for (i
= 0; i
< 4; i
++) {
3340 char c
= (l
>> (i
*8))&0xff;
3341 writeData(f
, &c
, 1);
3346 static void writeString(FILE *f
, char *s
)
3348 writeData(f
, s
, strlen(s
));
3351 /* Dump a gmon.out histogram file. */
3352 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3355 FILE *f
= fopen(filename
, "w");
3358 writeString(f
, "gmon");
3359 writeLong(f
, 0x00000001); /* Version */
3360 writeLong(f
, 0); /* padding */
3361 writeLong(f
, 0); /* padding */
3362 writeLong(f
, 0); /* padding */
3364 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3365 writeData(f
, &zero
, 1);
3367 /* figure out bucket size */
3368 uint32_t min
= samples
[0];
3369 uint32_t max
= samples
[0];
3370 for (i
= 0; i
< sampleNum
; i
++) {
3371 if (min
> samples
[i
])
3373 if (max
< samples
[i
])
3377 int addressSpace
= (max
- min
+ 1);
3378 assert(addressSpace
>= 2);
3380 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3381 uint32_t length
= addressSpace
;
3382 if (length
> maxBuckets
)
3383 length
= maxBuckets
;
3384 int *buckets
= malloc(sizeof(int)*length
);
3385 if (buckets
== NULL
) {
3389 memset(buckets
, 0, sizeof(int) * length
);
3390 for (i
= 0; i
< sampleNum
; i
++) {
3391 uint32_t address
= samples
[i
];
3392 long long a
= address
- min
;
3393 long long b
= length
- 1;
3394 long long c
= addressSpace
- 1;
3395 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3399 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3400 writeLong(f
, min
); /* low_pc */
3401 writeLong(f
, max
); /* high_pc */
3402 writeLong(f
, length
); /* # of samples */
3403 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3404 writeString(f
, "seconds");
3405 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3406 writeData(f
, &zero
, 1);
3407 writeString(f
, "s");
3409 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3411 char *data
= malloc(2 * length
);
3413 for (i
= 0; i
< length
; i
++) {
3418 data
[i
* 2] = val
&0xff;
3419 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3422 writeData(f
, data
, length
* 2);
3430 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3431 * which will be used as a random sampling of PC */
3432 COMMAND_HANDLER(handle_profile_command
)
3434 struct target
*target
= get_current_target(CMD_CTX
);
3435 struct timeval timeout
, now
;
3437 gettimeofday(&timeout
, NULL
);
3439 return ERROR_COMMAND_SYNTAX_ERROR
;
3441 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3443 timeval_add_time(&timeout
, offset
, 0);
3446 * @todo: Some cores let us sample the PC without the
3447 * annoying halt/resume step; for example, ARMv7 PCSR.
3448 * Provide a way to use that more efficient mechanism.
3451 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3453 static const int maxSample
= 10000;
3454 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3455 if (samples
== NULL
)
3459 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3460 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3462 int retval
= ERROR_OK
;
3464 target_poll(target
);
3465 if (target
->state
== TARGET_HALTED
) {
3466 uint32_t t
= *((uint32_t *)reg
->value
);
3467 samples
[numSamples
++] = t
;
3468 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3469 retval
= target_resume(target
, 1, 0, 0, 0);
3470 target_poll(target
);
3471 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3472 } else if (target
->state
== TARGET_RUNNING
) {
3473 /* We want to quickly sample the PC. */
3474 retval
= target_halt(target
);
3475 if (retval
!= ERROR_OK
) {
3480 command_print(CMD_CTX
, "Target not halted or running");
3484 if (retval
!= ERROR_OK
)
3487 gettimeofday(&now
, NULL
);
3488 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3489 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3490 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3491 retval
= target_poll(target
);
3492 if (retval
!= ERROR_OK
) {
3496 if (target
->state
== TARGET_HALTED
) {
3497 /* current pc, addr = 0, do not handle
3498 * breakpoints, not debugging */
3499 target_resume(target
, 1, 0, 0, 0);
3501 retval
= target_poll(target
);
3502 if (retval
!= ERROR_OK
) {
3506 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3507 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3516 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3519 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3522 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3526 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3527 valObjPtr
= Jim_NewIntObj(interp
, val
);
3528 if (!nameObjPtr
|| !valObjPtr
) {
3533 Jim_IncrRefCount(nameObjPtr
);
3534 Jim_IncrRefCount(valObjPtr
);
3535 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3536 Jim_DecrRefCount(interp
, nameObjPtr
);
3537 Jim_DecrRefCount(interp
, valObjPtr
);
3539 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3543 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3545 struct command_context
*context
;
3546 struct target
*target
;
3548 context
= current_command_context(interp
);
3549 assert(context
!= NULL
);
3551 target
= get_current_target(context
);
3552 if (target
== NULL
) {
3553 LOG_ERROR("mem2array: no current target");
3557 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3560 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3568 const char *varname
;
3572 /* argv[1] = name of array to receive the data
3573 * argv[2] = desired width
3574 * argv[3] = memory address
3575 * argv[4] = count of times to read
3578 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3581 varname
= Jim_GetString(argv
[0], &len
);
3582 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3584 e
= Jim_GetLong(interp
, argv
[1], &l
);
3589 e
= Jim_GetLong(interp
, argv
[2], &l
);
3593 e
= Jim_GetLong(interp
, argv
[3], &l
);
3608 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3609 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3613 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3614 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3617 if ((addr
+ (len
* width
)) < addr
) {
3618 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3619 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3622 /* absurd transfer size? */
3624 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3625 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3630 ((width
== 2) && ((addr
& 1) == 0)) ||
3631 ((width
== 4) && ((addr
& 3) == 0))) {
3635 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3636 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3639 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);