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, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type cortexr4_target
;
92 extern struct target_type arm11_target
;
93 extern struct target_type ls1_sap_target
;
94 extern struct target_type mips_m4k_target
;
95 extern struct target_type avr_target
;
96 extern struct target_type dsp563xx_target
;
97 extern struct target_type dsp5680xx_target
;
98 extern struct target_type testee_target
;
99 extern struct target_type avr32_ap7k_target
;
100 extern struct target_type hla_target
;
101 extern struct target_type nds32_v2_target
;
102 extern struct target_type nds32_v3_target
;
103 extern struct target_type nds32_v3m_target
;
104 extern struct target_type or1k_target
;
105 extern struct target_type quark_x10xx_target
;
106 extern struct target_type quark_d20xx_target
;
108 static struct target_type
*target_types
[] = {
141 struct target
*all_targets
;
142 static struct target_event_callback
*target_event_callbacks
;
143 static struct target_timer_callback
*target_timer_callbacks
;
144 LIST_HEAD(target_reset_callback_list
);
145 LIST_HEAD(target_trace_callback_list
);
146 static const int polling_interval
= 100;
148 static const Jim_Nvp nvp_assert
[] = {
149 { .name
= "assert", NVP_ASSERT
},
150 { .name
= "deassert", NVP_DEASSERT
},
151 { .name
= "T", NVP_ASSERT
},
152 { .name
= "F", NVP_DEASSERT
},
153 { .name
= "t", NVP_ASSERT
},
154 { .name
= "f", NVP_DEASSERT
},
155 { .name
= NULL
, .value
= -1 }
158 static const Jim_Nvp nvp_error_target
[] = {
159 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
160 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
161 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
162 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
163 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
164 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
165 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
166 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
167 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
168 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
169 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
170 { .value
= -1, .name
= NULL
}
173 static const char *target_strerror_safe(int err
)
177 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
184 static const Jim_Nvp nvp_target_event
[] = {
186 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
187 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
188 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
189 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
190 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
192 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
193 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
195 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
196 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
197 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
198 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
199 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
200 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
201 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
202 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
203 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
204 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
205 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
206 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
208 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
209 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
211 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
212 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
214 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
215 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
217 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
218 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
220 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
221 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
223 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
225 { .name
= NULL
, .value
= -1 }
228 static const Jim_Nvp nvp_target_state
[] = {
229 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
230 { .name
= "running", .value
= TARGET_RUNNING
},
231 { .name
= "halted", .value
= TARGET_HALTED
},
232 { .name
= "reset", .value
= TARGET_RESET
},
233 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
234 { .name
= NULL
, .value
= -1 },
237 static const Jim_Nvp nvp_target_debug_reason
[] = {
238 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
239 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
240 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
241 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
242 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
243 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
244 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
245 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
246 { .name
= NULL
, .value
= -1 },
249 static const Jim_Nvp nvp_target_endian
[] = {
250 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
251 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
252 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= NULL
, .value
= -1 },
257 static const Jim_Nvp nvp_reset_modes
[] = {
258 { .name
= "unknown", .value
= RESET_UNKNOWN
},
259 { .name
= "run" , .value
= RESET_RUN
},
260 { .name
= "halt" , .value
= RESET_HALT
},
261 { .name
= "init" , .value
= RESET_INIT
},
262 { .name
= NULL
, .value
= -1 },
265 const char *debug_reason_name(struct target
*t
)
269 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
270 t
->debug_reason
)->name
;
272 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
273 cp
= "(*BUG*unknown*BUG*)";
278 const char *target_state_name(struct target
*t
)
281 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
283 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
284 cp
= "(*BUG*unknown*BUG*)";
287 if (!target_was_examined(t
) && t
->defer_examine
)
288 cp
= "examine deferred";
293 const char *target_event_name(enum target_event event
)
296 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
298 LOG_ERROR("Invalid target event: %d", (int)(event
));
299 cp
= "(*BUG*unknown*BUG*)";
304 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
307 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
309 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
310 cp
= "(*BUG*unknown*BUG*)";
315 /* determine the number of the new target */
316 static int new_target_number(void)
321 /* number is 0 based */
325 if (x
< t
->target_number
)
326 x
= t
->target_number
;
332 /* read a uint64_t from a buffer in target memory endianness */
333 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
335 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
336 return le_to_h_u64(buffer
);
338 return be_to_h_u64(buffer
);
341 /* read a uint32_t from a buffer in target memory endianness */
342 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
344 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
345 return le_to_h_u32(buffer
);
347 return be_to_h_u32(buffer
);
350 /* read a uint24_t from a buffer in target memory endianness */
351 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
353 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
354 return le_to_h_u24(buffer
);
356 return be_to_h_u24(buffer
);
359 /* read a uint16_t from a buffer in target memory endianness */
360 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
362 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
363 return le_to_h_u16(buffer
);
365 return be_to_h_u16(buffer
);
368 /* read a uint8_t from a buffer in target memory endianness */
369 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
371 return *buffer
& 0x0ff;
374 /* write a uint64_t to a buffer in target memory endianness */
375 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 h_u64_to_le(buffer
, value
);
380 h_u64_to_be(buffer
, value
);
383 /* write a uint32_t to a buffer in target memory endianness */
384 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 h_u32_to_le(buffer
, value
);
389 h_u32_to_be(buffer
, value
);
392 /* write a uint24_t to a buffer in target memory endianness */
393 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 h_u24_to_le(buffer
, value
);
398 h_u24_to_be(buffer
, value
);
401 /* write a uint16_t to a buffer in target memory endianness */
402 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u16_to_le(buffer
, value
);
407 h_u16_to_be(buffer
, value
);
410 /* write a uint8_t to a buffer in target memory endianness */
411 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
416 /* write a uint64_t array to a buffer in target memory endianness */
417 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
420 for (i
= 0; i
< count
; i
++)
421 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
424 /* write a uint32_t array to a buffer in target memory endianness */
425 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
428 for (i
= 0; i
< count
; i
++)
429 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
432 /* write a uint16_t array to a buffer in target memory endianness */
433 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
436 for (i
= 0; i
< count
; i
++)
437 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
440 /* write a uint64_t array to a buffer in target memory endianness */
441 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
444 for (i
= 0; i
< count
; i
++)
445 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
448 /* write a uint32_t array to a buffer in target memory endianness */
449 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
452 for (i
= 0; i
< count
; i
++)
453 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
456 /* write a uint16_t array to a buffer in target memory endianness */
457 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
460 for (i
= 0; i
< count
; i
++)
461 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
464 /* return a pointer to a configured target; id is name or number */
465 struct target
*get_target(const char *id
)
467 struct target
*target
;
469 /* try as tcltarget name */
470 for (target
= all_targets
; target
; target
= target
->next
) {
471 if (target_name(target
) == NULL
)
473 if (strcmp(id
, target_name(target
)) == 0)
477 /* It's OK to remove this fallback sometime after August 2010 or so */
479 /* no match, try as number */
481 if (parse_uint(id
, &num
) != ERROR_OK
)
484 for (target
= all_targets
; target
; target
= target
->next
) {
485 if (target
->target_number
== (int)num
) {
486 LOG_WARNING("use '%s' as target identifier, not '%u'",
487 target_name(target
), num
);
495 /* returns a pointer to the n-th configured target */
496 struct target
*get_target_by_num(int num
)
498 struct target
*target
= all_targets
;
501 if (target
->target_number
== num
)
503 target
= target
->next
;
509 struct target
*get_current_target(struct command_context
*cmd_ctx
)
511 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
513 if (target
== NULL
) {
514 LOG_ERROR("BUG: current_target out of bounds");
521 int target_poll(struct target
*target
)
525 /* We can't poll until after examine */
526 if (!target_was_examined(target
)) {
527 /* Fail silently lest we pollute the log */
531 retval
= target
->type
->poll(target
);
532 if (retval
!= ERROR_OK
)
535 if (target
->halt_issued
) {
536 if (target
->state
== TARGET_HALTED
)
537 target
->halt_issued
= false;
539 int64_t t
= timeval_ms() - target
->halt_issued_time
;
540 if (t
> DEFAULT_HALT_TIMEOUT
) {
541 target
->halt_issued
= false;
542 LOG_INFO("Halt timed out, wake up GDB.");
543 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
551 int target_halt(struct target
*target
)
554 /* We can't poll until after examine */
555 if (!target_was_examined(target
)) {
556 LOG_ERROR("Target not examined yet");
560 retval
= target
->type
->halt(target
);
561 if (retval
!= ERROR_OK
)
564 target
->halt_issued
= true;
565 target
->halt_issued_time
= timeval_ms();
571 * Make the target (re)start executing using its saved execution
572 * context (possibly with some modifications).
574 * @param target Which target should start executing.
575 * @param current True to use the target's saved program counter instead
576 * of the address parameter
577 * @param address Optionally used as the program counter.
578 * @param handle_breakpoints True iff breakpoints at the resumption PC
579 * should be skipped. (For example, maybe execution was stopped by
580 * such a breakpoint, in which case it would be counterprodutive to
582 * @param debug_execution False if all working areas allocated by OpenOCD
583 * should be released and/or restored to their original contents.
584 * (This would for example be true to run some downloaded "helper"
585 * algorithm code, which resides in one such working buffer and uses
586 * another for data storage.)
588 * @todo Resolve the ambiguity about what the "debug_execution" flag
589 * signifies. For example, Target implementations don't agree on how
590 * it relates to invalidation of the register cache, or to whether
591 * breakpoints and watchpoints should be enabled. (It would seem wrong
592 * to enable breakpoints when running downloaded "helper" algorithms
593 * (debug_execution true), since the breakpoints would be set to match
594 * target firmware being debugged, not the helper algorithm.... and
595 * enabling them could cause such helpers to malfunction (for example,
596 * by overwriting data with a breakpoint instruction. On the other
597 * hand the infrastructure for running such helpers might use this
598 * procedure but rely on hardware breakpoint to detect termination.)
600 int target_resume(struct target
*target
, int current
, target_addr_t address
,
601 int handle_breakpoints
, int debug_execution
)
605 /* We can't poll until after examine */
606 if (!target_was_examined(target
)) {
607 LOG_ERROR("Target not examined yet");
611 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
613 /* note that resume *must* be asynchronous. The CPU can halt before
614 * we poll. The CPU can even halt at the current PC as a result of
615 * a software breakpoint being inserted by (a bug?) the application.
617 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
618 if (retval
!= ERROR_OK
)
621 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
626 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
631 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
632 if (n
->name
== NULL
) {
633 LOG_ERROR("invalid reset mode");
637 struct target
*target
;
638 for (target
= all_targets
; target
; target
= target
->next
)
639 target_call_reset_callbacks(target
, reset_mode
);
641 /* disable polling during reset to make reset event scripts
642 * more predictable, i.e. dr/irscan & pathmove in events will
643 * not have JTAG operations injected into the middle of a sequence.
645 bool save_poll
= jtag_poll_get_enabled();
647 jtag_poll_set_enabled(false);
649 sprintf(buf
, "ocd_process_reset %s", n
->name
);
650 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
652 jtag_poll_set_enabled(save_poll
);
654 if (retval
!= JIM_OK
) {
655 Jim_MakeErrorMessage(cmd_ctx
->interp
);
656 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
660 /* We want any events to be processed before the prompt */
661 retval
= target_call_timer_callbacks_now();
663 for (target
= all_targets
; target
; target
= target
->next
) {
664 target
->type
->check_reset(target
);
665 target
->running_alg
= false;
671 static int identity_virt2phys(struct target
*target
,
672 target_addr_t
virtual, target_addr_t
*physical
)
678 static int no_mmu(struct target
*target
, int *enabled
)
684 static int default_examine(struct target
*target
)
686 target_set_examined(target
);
690 /* no check by default */
691 static int default_check_reset(struct target
*target
)
696 int target_examine_one(struct target
*target
)
698 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
700 int retval
= target
->type
->examine(target
);
701 if (retval
!= ERROR_OK
)
704 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
709 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
711 struct target
*target
= priv
;
713 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
716 jtag_unregister_event_callback(jtag_enable_callback
, target
);
718 return target_examine_one(target
);
721 /* Targets that correctly implement init + examine, i.e.
722 * no communication with target during init:
726 int target_examine(void)
728 int retval
= ERROR_OK
;
729 struct target
*target
;
731 for (target
= all_targets
; target
; target
= target
->next
) {
732 /* defer examination, but don't skip it */
733 if (!target
->tap
->enabled
) {
734 jtag_register_event_callback(jtag_enable_callback
,
739 if (target
->defer_examine
)
742 retval
= target_examine_one(target
);
743 if (retval
!= ERROR_OK
)
749 const char *target_type_name(struct target
*target
)
751 return target
->type
->name
;
754 static int target_soft_reset_halt(struct target
*target
)
756 if (!target_was_examined(target
)) {
757 LOG_ERROR("Target not examined yet");
760 if (!target
->type
->soft_reset_halt
) {
761 LOG_ERROR("Target %s does not support soft_reset_halt",
762 target_name(target
));
765 return target
->type
->soft_reset_halt(target
);
769 * Downloads a target-specific native code algorithm to the target,
770 * and executes it. * Note that some targets may need to set up, enable,
771 * and tear down a breakpoint (hard or * soft) to detect algorithm
772 * termination, while others may support lower overhead schemes where
773 * soft breakpoints embedded in the algorithm automatically terminate the
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_run_algorithm(struct target
*target
,
780 int num_mem_params
, struct mem_param
*mem_params
,
781 int num_reg_params
, struct reg_param
*reg_param
,
782 uint32_t entry_point
, uint32_t exit_point
,
783 int timeout_ms
, void *arch_info
)
785 int retval
= ERROR_FAIL
;
787 if (!target_was_examined(target
)) {
788 LOG_ERROR("Target not examined yet");
791 if (!target
->type
->run_algorithm
) {
792 LOG_ERROR("Target type '%s' does not support %s",
793 target_type_name(target
), __func__
);
797 target
->running_alg
= true;
798 retval
= target
->type
->run_algorithm(target
,
799 num_mem_params
, mem_params
,
800 num_reg_params
, reg_param
,
801 entry_point
, exit_point
, timeout_ms
, arch_info
);
802 target
->running_alg
= false;
809 * Downloads a target-specific native code algorithm to the target,
810 * executes and leaves it running.
812 * @param target used to run the algorithm
813 * @param arch_info target-specific description of the algorithm.
815 int target_start_algorithm(struct target
*target
,
816 int num_mem_params
, struct mem_param
*mem_params
,
817 int num_reg_params
, struct reg_param
*reg_params
,
818 uint32_t entry_point
, uint32_t exit_point
,
821 int retval
= ERROR_FAIL
;
823 if (!target_was_examined(target
)) {
824 LOG_ERROR("Target not examined yet");
827 if (!target
->type
->start_algorithm
) {
828 LOG_ERROR("Target type '%s' does not support %s",
829 target_type_name(target
), __func__
);
832 if (target
->running_alg
) {
833 LOG_ERROR("Target is already running an algorithm");
837 target
->running_alg
= true;
838 retval
= target
->type
->start_algorithm(target
,
839 num_mem_params
, mem_params
,
840 num_reg_params
, reg_params
,
841 entry_point
, exit_point
, arch_info
);
848 * Waits for an algorithm started with target_start_algorithm() to complete.
850 * @param target used to run the algorithm
851 * @param arch_info target-specific description of the algorithm.
853 int target_wait_algorithm(struct target
*target
,
854 int num_mem_params
, struct mem_param
*mem_params
,
855 int num_reg_params
, struct reg_param
*reg_params
,
856 uint32_t exit_point
, int timeout_ms
,
859 int retval
= ERROR_FAIL
;
861 if (!target
->type
->wait_algorithm
) {
862 LOG_ERROR("Target type '%s' does not support %s",
863 target_type_name(target
), __func__
);
866 if (!target
->running_alg
) {
867 LOG_ERROR("Target is not running an algorithm");
871 retval
= target
->type
->wait_algorithm(target
,
872 num_mem_params
, mem_params
,
873 num_reg_params
, reg_params
,
874 exit_point
, timeout_ms
, arch_info
);
875 if (retval
!= ERROR_TARGET_TIMEOUT
)
876 target
->running_alg
= false;
883 * Executes a target-specific native code algorithm in the target.
884 * It differs from target_run_algorithm in that the algorithm is asynchronous.
885 * Because of this it requires an compliant algorithm:
886 * see contrib/loaders/flash/stm32f1x.S for example.
888 * @param target used to run the algorithm
891 int target_run_flash_async_algorithm(struct target
*target
,
892 const uint8_t *buffer
, uint32_t count
, int block_size
,
893 int num_mem_params
, struct mem_param
*mem_params
,
894 int num_reg_params
, struct reg_param
*reg_params
,
895 uint32_t buffer_start
, uint32_t buffer_size
,
896 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
901 const uint8_t *buffer_orig
= buffer
;
903 /* Set up working area. First word is write pointer, second word is read pointer,
904 * rest is fifo data area. */
905 uint32_t wp_addr
= buffer_start
;
906 uint32_t rp_addr
= buffer_start
+ 4;
907 uint32_t fifo_start_addr
= buffer_start
+ 8;
908 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
910 uint32_t wp
= fifo_start_addr
;
911 uint32_t rp
= fifo_start_addr
;
913 /* validate block_size is 2^n */
914 assert(!block_size
|| !(block_size
& (block_size
- 1)));
916 retval
= target_write_u32(target
, wp_addr
, wp
);
917 if (retval
!= ERROR_OK
)
919 retval
= target_write_u32(target
, rp_addr
, rp
);
920 if (retval
!= ERROR_OK
)
923 /* Start up algorithm on target and let it idle while writing the first chunk */
924 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
925 num_reg_params
, reg_params
,
930 if (retval
!= ERROR_OK
) {
931 LOG_ERROR("error starting target flash write algorithm");
937 retval
= target_read_u32(target
, rp_addr
, &rp
);
938 if (retval
!= ERROR_OK
) {
939 LOG_ERROR("failed to get read pointer");
943 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
944 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
947 LOG_ERROR("flash write algorithm aborted by target");
948 retval
= ERROR_FLASH_OPERATION_FAILED
;
952 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
953 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
957 /* Count the number of bytes available in the fifo without
958 * crossing the wrap around. Make sure to not fill it completely,
959 * because that would make wp == rp and that's the empty condition. */
960 uint32_t thisrun_bytes
;
962 thisrun_bytes
= rp
- wp
- block_size
;
963 else if (rp
> fifo_start_addr
)
964 thisrun_bytes
= fifo_end_addr
- wp
;
966 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
968 if (thisrun_bytes
== 0) {
969 /* Throttle polling a bit if transfer is (much) faster than flash
970 * programming. The exact delay shouldn't matter as long as it's
971 * less than buffer size / flash speed. This is very unlikely to
972 * run when using high latency connections such as USB. */
975 /* to stop an infinite loop on some targets check and increment a timeout
976 * this issue was observed on a stellaris using the new ICDI interface */
977 if (timeout
++ >= 500) {
978 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
979 return ERROR_FLASH_OPERATION_FAILED
;
984 /* reset our timeout */
987 /* Limit to the amount of data we actually want to write */
988 if (thisrun_bytes
> count
* block_size
)
989 thisrun_bytes
= count
* block_size
;
991 /* Write data to fifo */
992 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
993 if (retval
!= ERROR_OK
)
996 /* Update counters and wrap write pointer */
997 buffer
+= thisrun_bytes
;
998 count
-= thisrun_bytes
/ block_size
;
1000 if (wp
>= fifo_end_addr
)
1001 wp
= fifo_start_addr
;
1003 /* Store updated write pointer to target */
1004 retval
= target_write_u32(target
, wp_addr
, wp
);
1005 if (retval
!= ERROR_OK
)
1009 if (retval
!= ERROR_OK
) {
1010 /* abort flash write algorithm on target */
1011 target_write_u32(target
, wp_addr
, 0);
1014 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1015 num_reg_params
, reg_params
,
1020 if (retval2
!= ERROR_OK
) {
1021 LOG_ERROR("error waiting for target flash write algorithm");
1025 if (retval
== ERROR_OK
) {
1026 /* check if algorithm set rp = 0 after fifo writer loop finished */
1027 retval
= target_read_u32(target
, rp_addr
, &rp
);
1028 if (retval
== ERROR_OK
&& rp
== 0) {
1029 LOG_ERROR("flash write algorithm aborted by target");
1030 retval
= ERROR_FLASH_OPERATION_FAILED
;
1037 int target_read_memory(struct target
*target
,
1038 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1040 if (!target_was_examined(target
)) {
1041 LOG_ERROR("Target not examined yet");
1044 if (!target
->type
->read_memory
) {
1045 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1048 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1051 int target_read_phys_memory(struct target
*target
,
1052 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1054 if (!target_was_examined(target
)) {
1055 LOG_ERROR("Target not examined yet");
1058 if (!target
->type
->read_phys_memory
) {
1059 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1062 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1065 int target_write_memory(struct target
*target
,
1066 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1068 if (!target_was_examined(target
)) {
1069 LOG_ERROR("Target not examined yet");
1072 if (!target
->type
->write_memory
) {
1073 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1076 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1079 int target_write_phys_memory(struct target
*target
,
1080 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1082 if (!target_was_examined(target
)) {
1083 LOG_ERROR("Target not examined yet");
1086 if (!target
->type
->write_phys_memory
) {
1087 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1090 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1093 int target_add_breakpoint(struct target
*target
,
1094 struct breakpoint
*breakpoint
)
1096 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1097 LOG_WARNING("target %s is not halted", target_name(target
));
1098 return ERROR_TARGET_NOT_HALTED
;
1100 return target
->type
->add_breakpoint(target
, breakpoint
);
1103 int target_add_context_breakpoint(struct target
*target
,
1104 struct breakpoint
*breakpoint
)
1106 if (target
->state
!= TARGET_HALTED
) {
1107 LOG_WARNING("target %s is not halted", target_name(target
));
1108 return ERROR_TARGET_NOT_HALTED
;
1110 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1113 int target_add_hybrid_breakpoint(struct target
*target
,
1114 struct breakpoint
*breakpoint
)
1116 if (target
->state
!= TARGET_HALTED
) {
1117 LOG_WARNING("target %s is not halted", target_name(target
));
1118 return ERROR_TARGET_NOT_HALTED
;
1120 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1123 int target_remove_breakpoint(struct target
*target
,
1124 struct breakpoint
*breakpoint
)
1126 return target
->type
->remove_breakpoint(target
, breakpoint
);
1129 int target_add_watchpoint(struct target
*target
,
1130 struct watchpoint
*watchpoint
)
1132 if (target
->state
!= TARGET_HALTED
) {
1133 LOG_WARNING("target %s is not halted", target_name(target
));
1134 return ERROR_TARGET_NOT_HALTED
;
1136 return target
->type
->add_watchpoint(target
, watchpoint
);
1138 int target_remove_watchpoint(struct target
*target
,
1139 struct watchpoint
*watchpoint
)
1141 return target
->type
->remove_watchpoint(target
, watchpoint
);
1143 int target_hit_watchpoint(struct target
*target
,
1144 struct watchpoint
**hit_watchpoint
)
1146 if (target
->state
!= TARGET_HALTED
) {
1147 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1148 return ERROR_TARGET_NOT_HALTED
;
1151 if (target
->type
->hit_watchpoint
== NULL
) {
1152 /* For backward compatible, if hit_watchpoint is not implemented,
1153 * return ERROR_FAIL such that gdb_server will not take the nonsense
1158 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1161 int target_get_gdb_reg_list(struct target
*target
,
1162 struct reg
**reg_list
[], int *reg_list_size
,
1163 enum target_register_class reg_class
)
1165 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1167 int target_step(struct target
*target
,
1168 int current
, target_addr_t address
, int handle_breakpoints
)
1170 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1173 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1175 if (target
->state
!= TARGET_HALTED
) {
1176 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1177 return ERROR_TARGET_NOT_HALTED
;
1179 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1182 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1184 if (target
->state
!= TARGET_HALTED
) {
1185 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1186 return ERROR_TARGET_NOT_HALTED
;
1188 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1191 int target_profiling(struct target
*target
, uint32_t *samples
,
1192 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1194 if (target
->state
!= TARGET_HALTED
) {
1195 LOG_WARNING("target %s is not halted", target
->cmd_name
);
1196 return ERROR_TARGET_NOT_HALTED
;
1198 return target
->type
->profiling(target
, samples
, max_num_samples
,
1199 num_samples
, seconds
);
1203 * Reset the @c examined flag for the given target.
1204 * Pure paranoia -- targets are zeroed on allocation.
1206 static void target_reset_examined(struct target
*target
)
1208 target
->examined
= false;
1211 static int handle_target(void *priv
);
1213 static int target_init_one(struct command_context
*cmd_ctx
,
1214 struct target
*target
)
1216 target_reset_examined(target
);
1218 struct target_type
*type
= target
->type
;
1219 if (type
->examine
== NULL
)
1220 type
->examine
= default_examine
;
1222 if (type
->check_reset
== NULL
)
1223 type
->check_reset
= default_check_reset
;
1225 assert(type
->init_target
!= NULL
);
1227 int retval
= type
->init_target(cmd_ctx
, target
);
1228 if (ERROR_OK
!= retval
) {
1229 LOG_ERROR("target '%s' init failed", target_name(target
));
1233 /* Sanity-check MMU support ... stub in what we must, to help
1234 * implement it in stages, but warn if we need to do so.
1237 if (type
->virt2phys
== NULL
) {
1238 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1239 type
->virt2phys
= identity_virt2phys
;
1242 /* Make sure no-MMU targets all behave the same: make no
1243 * distinction between physical and virtual addresses, and
1244 * ensure that virt2phys() is always an identity mapping.
1246 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1247 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1250 type
->write_phys_memory
= type
->write_memory
;
1251 type
->read_phys_memory
= type
->read_memory
;
1252 type
->virt2phys
= identity_virt2phys
;
1255 if (target
->type
->read_buffer
== NULL
)
1256 target
->type
->read_buffer
= target_read_buffer_default
;
1258 if (target
->type
->write_buffer
== NULL
)
1259 target
->type
->write_buffer
= target_write_buffer_default
;
1261 if (target
->type
->get_gdb_fileio_info
== NULL
)
1262 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1264 if (target
->type
->gdb_fileio_end
== NULL
)
1265 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1267 if (target
->type
->profiling
== NULL
)
1268 target
->type
->profiling
= target_profiling_default
;
1273 static int target_init(struct command_context
*cmd_ctx
)
1275 struct target
*target
;
1278 for (target
= all_targets
; target
; target
= target
->next
) {
1279 retval
= target_init_one(cmd_ctx
, target
);
1280 if (ERROR_OK
!= retval
)
1287 retval
= target_register_user_commands(cmd_ctx
);
1288 if (ERROR_OK
!= retval
)
1291 retval
= target_register_timer_callback(&handle_target
,
1292 polling_interval
, 1, cmd_ctx
->interp
);
1293 if (ERROR_OK
!= retval
)
1299 COMMAND_HANDLER(handle_target_init_command
)
1304 return ERROR_COMMAND_SYNTAX_ERROR
;
1306 static bool target_initialized
;
1307 if (target_initialized
) {
1308 LOG_INFO("'target init' has already been called");
1311 target_initialized
= true;
1313 retval
= command_run_line(CMD_CTX
, "init_targets");
1314 if (ERROR_OK
!= retval
)
1317 retval
= command_run_line(CMD_CTX
, "init_target_events");
1318 if (ERROR_OK
!= retval
)
1321 retval
= command_run_line(CMD_CTX
, "init_board");
1322 if (ERROR_OK
!= retval
)
1325 LOG_DEBUG("Initializing targets...");
1326 return target_init(CMD_CTX
);
1329 int target_register_event_callback(int (*callback
)(struct target
*target
,
1330 enum target_event event
, void *priv
), void *priv
)
1332 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1334 if (callback
== NULL
)
1335 return ERROR_COMMAND_SYNTAX_ERROR
;
1338 while ((*callbacks_p
)->next
)
1339 callbacks_p
= &((*callbacks_p
)->next
);
1340 callbacks_p
= &((*callbacks_p
)->next
);
1343 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1344 (*callbacks_p
)->callback
= callback
;
1345 (*callbacks_p
)->priv
= priv
;
1346 (*callbacks_p
)->next
= NULL
;
1351 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1352 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1354 struct target_reset_callback
*entry
;
1356 if (callback
== NULL
)
1357 return ERROR_COMMAND_SYNTAX_ERROR
;
1359 entry
= malloc(sizeof(struct target_reset_callback
));
1360 if (entry
== NULL
) {
1361 LOG_ERROR("error allocating buffer for reset callback entry");
1362 return ERROR_COMMAND_SYNTAX_ERROR
;
1365 entry
->callback
= callback
;
1367 list_add(&entry
->list
, &target_reset_callback_list
);
1373 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1374 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1376 struct target_trace_callback
*entry
;
1378 if (callback
== NULL
)
1379 return ERROR_COMMAND_SYNTAX_ERROR
;
1381 entry
= malloc(sizeof(struct target_trace_callback
));
1382 if (entry
== NULL
) {
1383 LOG_ERROR("error allocating buffer for trace callback entry");
1384 return ERROR_COMMAND_SYNTAX_ERROR
;
1387 entry
->callback
= callback
;
1389 list_add(&entry
->list
, &target_trace_callback_list
);
1395 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1397 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1400 if (callback
== NULL
)
1401 return ERROR_COMMAND_SYNTAX_ERROR
;
1404 while ((*callbacks_p
)->next
)
1405 callbacks_p
= &((*callbacks_p
)->next
);
1406 callbacks_p
= &((*callbacks_p
)->next
);
1409 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1410 (*callbacks_p
)->callback
= callback
;
1411 (*callbacks_p
)->periodic
= periodic
;
1412 (*callbacks_p
)->time_ms
= time_ms
;
1413 (*callbacks_p
)->removed
= false;
1415 gettimeofday(&now
, NULL
);
1416 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1417 time_ms
-= (time_ms
% 1000);
1418 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1419 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1420 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1421 (*callbacks_p
)->when
.tv_sec
+= 1;
1424 (*callbacks_p
)->priv
= priv
;
1425 (*callbacks_p
)->next
= NULL
;
1430 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1431 enum target_event event
, void *priv
), void *priv
)
1433 struct target_event_callback
**p
= &target_event_callbacks
;
1434 struct target_event_callback
*c
= target_event_callbacks
;
1436 if (callback
== NULL
)
1437 return ERROR_COMMAND_SYNTAX_ERROR
;
1440 struct target_event_callback
*next
= c
->next
;
1441 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1453 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1454 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1456 struct target_reset_callback
*entry
;
1458 if (callback
== NULL
)
1459 return ERROR_COMMAND_SYNTAX_ERROR
;
1461 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1462 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1463 list_del(&entry
->list
);
1472 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1473 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1475 struct target_trace_callback
*entry
;
1477 if (callback
== NULL
)
1478 return ERROR_COMMAND_SYNTAX_ERROR
;
1480 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1481 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1482 list_del(&entry
->list
);
1491 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1493 if (callback
== NULL
)
1494 return ERROR_COMMAND_SYNTAX_ERROR
;
1496 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1498 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1507 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1509 struct target_event_callback
*callback
= target_event_callbacks
;
1510 struct target_event_callback
*next_callback
;
1512 if (event
== TARGET_EVENT_HALTED
) {
1513 /* execute early halted first */
1514 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1517 LOG_DEBUG("target event %i (%s)", event
,
1518 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1520 target_handle_event(target
, event
);
1523 next_callback
= callback
->next
;
1524 callback
->callback(target
, event
, callback
->priv
);
1525 callback
= next_callback
;
1531 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1533 struct target_reset_callback
*callback
;
1535 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1536 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1538 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1539 callback
->callback(target
, reset_mode
, callback
->priv
);
1544 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1546 struct target_trace_callback
*callback
;
1548 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1549 callback
->callback(target
, len
, data
, callback
->priv
);
1554 static int target_timer_callback_periodic_restart(
1555 struct target_timer_callback
*cb
, struct timeval
*now
)
1557 int time_ms
= cb
->time_ms
;
1558 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1559 time_ms
-= (time_ms
% 1000);
1560 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1561 if (cb
->when
.tv_usec
> 1000000) {
1562 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1563 cb
->when
.tv_sec
+= 1;
1568 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1569 struct timeval
*now
)
1571 cb
->callback(cb
->priv
);
1574 return target_timer_callback_periodic_restart(cb
, now
);
1576 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1579 static int target_call_timer_callbacks_check_time(int checktime
)
1581 static bool callback_processing
;
1583 /* Do not allow nesting */
1584 if (callback_processing
)
1587 callback_processing
= true;
1592 gettimeofday(&now
, NULL
);
1594 /* Store an address of the place containing a pointer to the
1595 * next item; initially, that's a standalone "root of the
1596 * list" variable. */
1597 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1599 if ((*callback
)->removed
) {
1600 struct target_timer_callback
*p
= *callback
;
1601 *callback
= (*callback
)->next
;
1606 bool call_it
= (*callback
)->callback
&&
1607 ((!checktime
&& (*callback
)->periodic
) ||
1608 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1609 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1610 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1613 target_call_timer_callback(*callback
, &now
);
1615 callback
= &(*callback
)->next
;
1618 callback_processing
= false;
1622 int target_call_timer_callbacks(void)
1624 return target_call_timer_callbacks_check_time(1);
1627 /* invoke periodic callbacks immediately */
1628 int target_call_timer_callbacks_now(void)
1630 return target_call_timer_callbacks_check_time(0);
1633 /* Prints the working area layout for debug purposes */
1634 static void print_wa_layout(struct target
*target
)
1636 struct working_area
*c
= target
->working_areas
;
1639 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1640 c
->backup ?
'b' : ' ', c
->free ?
' ' : '*',
1641 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1646 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1647 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1649 assert(area
->free
); /* Shouldn't split an allocated area */
1650 assert(size
<= area
->size
); /* Caller should guarantee this */
1652 /* Split only if not already the right size */
1653 if (size
< area
->size
) {
1654 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1659 new_wa
->next
= area
->next
;
1660 new_wa
->size
= area
->size
- size
;
1661 new_wa
->address
= area
->address
+ size
;
1662 new_wa
->backup
= NULL
;
1663 new_wa
->user
= NULL
;
1664 new_wa
->free
= true;
1666 area
->next
= new_wa
;
1669 /* If backup memory was allocated to this area, it has the wrong size
1670 * now so free it and it will be reallocated if/when needed */
1673 area
->backup
= NULL
;
1678 /* Merge all adjacent free areas into one */
1679 static void target_merge_working_areas(struct target
*target
)
1681 struct working_area
*c
= target
->working_areas
;
1683 while (c
&& c
->next
) {
1684 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1686 /* Find two adjacent free areas */
1687 if (c
->free
&& c
->next
->free
) {
1688 /* Merge the last into the first */
1689 c
->size
+= c
->next
->size
;
1691 /* Remove the last */
1692 struct working_area
*to_be_freed
= c
->next
;
1693 c
->next
= c
->next
->next
;
1694 if (to_be_freed
->backup
)
1695 free(to_be_freed
->backup
);
1698 /* If backup memory was allocated to the remaining area, it's has
1699 * the wrong size now */
1710 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1712 /* Reevaluate working area address based on MMU state*/
1713 if (target
->working_areas
== NULL
) {
1717 retval
= target
->type
->mmu(target
, &enabled
);
1718 if (retval
!= ERROR_OK
)
1722 if (target
->working_area_phys_spec
) {
1723 LOG_DEBUG("MMU disabled, using physical "
1724 "address for working memory " TARGET_ADDR_FMT
,
1725 target
->working_area_phys
);
1726 target
->working_area
= target
->working_area_phys
;
1728 LOG_ERROR("No working memory available. "
1729 "Specify -work-area-phys to target.");
1730 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1733 if (target
->working_area_virt_spec
) {
1734 LOG_DEBUG("MMU enabled, using virtual "
1735 "address for working memory " TARGET_ADDR_FMT
,
1736 target
->working_area_virt
);
1737 target
->working_area
= target
->working_area_virt
;
1739 LOG_ERROR("No working memory available. "
1740 "Specify -work-area-virt to target.");
1741 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1745 /* Set up initial working area on first call */
1746 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1748 new_wa
->next
= NULL
;
1749 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1750 new_wa
->address
= target
->working_area
;
1751 new_wa
->backup
= NULL
;
1752 new_wa
->user
= NULL
;
1753 new_wa
->free
= true;
1756 target
->working_areas
= new_wa
;
1759 /* only allocate multiples of 4 byte */
1761 size
= (size
+ 3) & (~3UL);
1763 struct working_area
*c
= target
->working_areas
;
1765 /* Find the first large enough working area */
1767 if (c
->free
&& c
->size
>= size
)
1773 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1775 /* Split the working area into the requested size */
1776 target_split_working_area(c
, size
);
1778 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1781 if (target
->backup_working_area
) {
1782 if (c
->backup
== NULL
) {
1783 c
->backup
= malloc(c
->size
);
1784 if (c
->backup
== NULL
)
1788 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1789 if (retval
!= ERROR_OK
)
1793 /* mark as used, and return the new (reused) area */
1800 print_wa_layout(target
);
1805 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1809 retval
= target_alloc_working_area_try(target
, size
, area
);
1810 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1811 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1816 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1818 int retval
= ERROR_OK
;
1820 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1821 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1822 if (retval
!= ERROR_OK
)
1823 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1824 area
->size
, area
->address
);
1830 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1831 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1833 int retval
= ERROR_OK
;
1839 retval
= target_restore_working_area(target
, area
);
1840 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1841 if (retval
!= ERROR_OK
)
1847 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1848 area
->size
, area
->address
);
1850 /* mark user pointer invalid */
1851 /* TODO: Is this really safe? It points to some previous caller's memory.
1852 * How could we know that the area pointer is still in that place and not
1853 * some other vital data? What's the purpose of this, anyway? */
1857 target_merge_working_areas(target
);
1859 print_wa_layout(target
);
1864 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1866 return target_free_working_area_restore(target
, area
, 1);
1869 void target_quit(void)
1871 struct target_event_callback
*pe
= target_event_callbacks
;
1873 struct target_event_callback
*t
= pe
->next
;
1877 target_event_callbacks
= NULL
;
1879 struct target_timer_callback
*pt
= target_timer_callbacks
;
1881 struct target_timer_callback
*t
= pt
->next
;
1885 target_timer_callbacks
= NULL
;
1887 for (struct target
*target
= all_targets
;
1888 target
; target
= target
->next
) {
1889 if (target
->type
->deinit_target
)
1890 target
->type
->deinit_target(target
);
1894 /* free resources and restore memory, if restoring memory fails,
1895 * free up resources anyway
1897 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1899 struct working_area
*c
= target
->working_areas
;
1901 LOG_DEBUG("freeing all working areas");
1903 /* Loop through all areas, restoring the allocated ones and marking them as free */
1907 target_restore_working_area(target
, c
);
1909 *c
->user
= NULL
; /* Same as above */
1915 /* Run a merge pass to combine all areas into one */
1916 target_merge_working_areas(target
);
1918 print_wa_layout(target
);
1921 void target_free_all_working_areas(struct target
*target
)
1923 target_free_all_working_areas_restore(target
, 1);
1926 /* Find the largest number of bytes that can be allocated */
1927 uint32_t target_get_working_area_avail(struct target
*target
)
1929 struct working_area
*c
= target
->working_areas
;
1930 uint32_t max_size
= 0;
1933 return target
->working_area_size
;
1936 if (c
->free
&& max_size
< c
->size
)
1945 int target_arch_state(struct target
*target
)
1948 if (target
== NULL
) {
1949 LOG_WARNING("No target has been configured");
1953 if (target
->state
!= TARGET_HALTED
)
1956 retval
= target
->type
->arch_state(target
);
1960 static int target_get_gdb_fileio_info_default(struct target
*target
,
1961 struct gdb_fileio_info
*fileio_info
)
1963 /* If target does not support semi-hosting function, target
1964 has no need to provide .get_gdb_fileio_info callback.
1965 It just return ERROR_FAIL and gdb_server will return "Txx"
1966 as target halted every time. */
1970 static int target_gdb_fileio_end_default(struct target
*target
,
1971 int retcode
, int fileio_errno
, bool ctrl_c
)
1976 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1977 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1979 struct timeval timeout
, now
;
1981 gettimeofday(&timeout
, NULL
);
1982 timeval_add_time(&timeout
, seconds
, 0);
1984 LOG_INFO("Starting profiling. Halting and resuming the"
1985 " target as often as we can...");
1987 uint32_t sample_count
= 0;
1988 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1989 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1991 int retval
= ERROR_OK
;
1993 target_poll(target
);
1994 if (target
->state
== TARGET_HALTED
) {
1995 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1996 samples
[sample_count
++] = t
;
1997 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1998 retval
= target_resume(target
, 1, 0, 0, 0);
1999 target_poll(target
);
2000 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2001 } else if (target
->state
== TARGET_RUNNING
) {
2002 /* We want to quickly sample the PC. */
2003 retval
= target_halt(target
);
2005 LOG_INFO("Target not halted or running");
2010 if (retval
!= ERROR_OK
)
2013 gettimeofday(&now
, NULL
);
2014 if ((sample_count
>= max_num_samples
) ||
2015 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2016 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2021 *num_samples
= sample_count
;
2025 /* Single aligned words are guaranteed to use 16 or 32 bit access
2026 * mode respectively, otherwise data is handled as quickly as
2029 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2031 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2034 if (!target_was_examined(target
)) {
2035 LOG_ERROR("Target not examined yet");
2042 if ((address
+ size
- 1) < address
) {
2043 /* GDB can request this when e.g. PC is 0xfffffffc */
2044 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2050 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2053 static int target_write_buffer_default(struct target
*target
,
2054 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2058 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2059 * will have something to do with the size we leave to it. */
2060 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2061 if (address
& size
) {
2062 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2063 if (retval
!= ERROR_OK
)
2071 /* Write the data with as large access size as possible. */
2072 for (; size
> 0; size
/= 2) {
2073 uint32_t aligned
= count
- count
% size
;
2075 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2076 if (retval
!= ERROR_OK
)
2087 /* Single aligned words are guaranteed to use 16 or 32 bit access
2088 * mode respectively, otherwise data is handled as quickly as
2091 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2093 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2096 if (!target_was_examined(target
)) {
2097 LOG_ERROR("Target not examined yet");
2104 if ((address
+ size
- 1) < address
) {
2105 /* GDB can request this when e.g. PC is 0xfffffffc */
2106 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2112 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2115 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2119 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2120 * will have something to do with the size we leave to it. */
2121 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2122 if (address
& size
) {
2123 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2124 if (retval
!= ERROR_OK
)
2132 /* Read the data with as large access size as possible. */
2133 for (; size
> 0; size
/= 2) {
2134 uint32_t aligned
= count
- count
% size
;
2136 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2137 if (retval
!= ERROR_OK
)
2148 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2153 uint32_t checksum
= 0;
2154 if (!target_was_examined(target
)) {
2155 LOG_ERROR("Target not examined yet");
2159 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2160 if (retval
!= ERROR_OK
) {
2161 buffer
= malloc(size
);
2162 if (buffer
== NULL
) {
2163 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2164 return ERROR_COMMAND_SYNTAX_ERROR
;
2166 retval
= target_read_buffer(target
, address
, size
, buffer
);
2167 if (retval
!= ERROR_OK
) {
2172 /* convert to target endianness */
2173 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2174 uint32_t target_data
;
2175 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2176 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2179 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2188 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2189 uint8_t erased_value
)
2192 if (!target_was_examined(target
)) {
2193 LOG_ERROR("Target not examined yet");
2197 if (target
->type
->blank_check_memory
== 0)
2198 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2200 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2205 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2207 uint8_t value_buf
[8];
2208 if (!target_was_examined(target
)) {
2209 LOG_ERROR("Target not examined yet");
2213 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2215 if (retval
== ERROR_OK
) {
2216 *value
= target_buffer_get_u64(target
, value_buf
);
2217 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2222 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2229 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2231 uint8_t value_buf
[4];
2232 if (!target_was_examined(target
)) {
2233 LOG_ERROR("Target not examined yet");
2237 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2239 if (retval
== ERROR_OK
) {
2240 *value
= target_buffer_get_u32(target
, value_buf
);
2241 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2246 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2253 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2255 uint8_t value_buf
[2];
2256 if (!target_was_examined(target
)) {
2257 LOG_ERROR("Target not examined yet");
2261 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2263 if (retval
== ERROR_OK
) {
2264 *value
= target_buffer_get_u16(target
, value_buf
);
2265 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2270 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2277 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2279 if (!target_was_examined(target
)) {
2280 LOG_ERROR("Target not examined yet");
2284 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2286 if (retval
== ERROR_OK
) {
2287 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2292 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2299 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2302 uint8_t value_buf
[8];
2303 if (!target_was_examined(target
)) {
2304 LOG_ERROR("Target not examined yet");
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2312 target_buffer_set_u64(target
, value_buf
, value
);
2313 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2314 if (retval
!= ERROR_OK
)
2315 LOG_DEBUG("failed: %i", retval
);
2320 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2323 uint8_t value_buf
[4];
2324 if (!target_was_examined(target
)) {
2325 LOG_ERROR("Target not examined yet");
2329 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2333 target_buffer_set_u32(target
, value_buf
, value
);
2334 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2335 if (retval
!= ERROR_OK
)
2336 LOG_DEBUG("failed: %i", retval
);
2341 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2344 uint8_t value_buf
[2];
2345 if (!target_was_examined(target
)) {
2346 LOG_ERROR("Target not examined yet");
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2354 target_buffer_set_u16(target
, value_buf
, value
);
2355 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2356 if (retval
!= ERROR_OK
)
2357 LOG_DEBUG("failed: %i", retval
);
2362 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2365 if (!target_was_examined(target
)) {
2366 LOG_ERROR("Target not examined yet");
2370 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2373 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2374 if (retval
!= ERROR_OK
)
2375 LOG_DEBUG("failed: %i", retval
);
2380 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2383 uint8_t value_buf
[8];
2384 if (!target_was_examined(target
)) {
2385 LOG_ERROR("Target not examined yet");
2389 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2393 target_buffer_set_u64(target
, value_buf
, value
);
2394 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2395 if (retval
!= ERROR_OK
)
2396 LOG_DEBUG("failed: %i", retval
);
2401 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2404 uint8_t value_buf
[4];
2405 if (!target_was_examined(target
)) {
2406 LOG_ERROR("Target not examined yet");
2410 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2414 target_buffer_set_u32(target
, value_buf
, value
);
2415 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2416 if (retval
!= ERROR_OK
)
2417 LOG_DEBUG("failed: %i", retval
);
2422 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2425 uint8_t value_buf
[2];
2426 if (!target_was_examined(target
)) {
2427 LOG_ERROR("Target not examined yet");
2431 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2435 target_buffer_set_u16(target
, value_buf
, value
);
2436 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2437 if (retval
!= ERROR_OK
)
2438 LOG_DEBUG("failed: %i", retval
);
2443 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2446 if (!target_was_examined(target
)) {
2447 LOG_ERROR("Target not examined yet");
2451 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2454 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2455 if (retval
!= ERROR_OK
)
2456 LOG_DEBUG("failed: %i", retval
);
2461 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2463 struct target
*target
= get_target(name
);
2464 if (target
== NULL
) {
2465 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2468 if (!target
->tap
->enabled
) {
2469 LOG_USER("Target: TAP %s is disabled, "
2470 "can't be the current target\n",
2471 target
->tap
->dotted_name
);
2475 cmd_ctx
->current_target
= target
->target_number
;
2480 COMMAND_HANDLER(handle_targets_command
)
2482 int retval
= ERROR_OK
;
2483 if (CMD_ARGC
== 1) {
2484 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2485 if (retval
== ERROR_OK
) {
2491 struct target
*target
= all_targets
;
2492 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2493 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2498 if (target
->tap
->enabled
)
2499 state
= target_state_name(target
);
2501 state
= "tap-disabled";
2503 if (CMD_CTX
->current_target
== target
->target_number
)
2506 /* keep columns lined up to match the headers above */
2507 command_print(CMD_CTX
,
2508 "%2d%c %-18s %-10s %-6s %-18s %s",
2509 target
->target_number
,
2511 target_name(target
),
2512 target_type_name(target
),
2513 Jim_Nvp_value2name_simple(nvp_target_endian
,
2514 target
->endianness
)->name
,
2515 target
->tap
->dotted_name
,
2517 target
= target
->next
;
2523 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2525 static int powerDropout
;
2526 static int srstAsserted
;
2528 static int runPowerRestore
;
2529 static int runPowerDropout
;
2530 static int runSrstAsserted
;
2531 static int runSrstDeasserted
;
2533 static int sense_handler(void)
2535 static int prevSrstAsserted
;
2536 static int prevPowerdropout
;
2538 int retval
= jtag_power_dropout(&powerDropout
);
2539 if (retval
!= ERROR_OK
)
2543 powerRestored
= prevPowerdropout
&& !powerDropout
;
2545 runPowerRestore
= 1;
2547 int64_t current
= timeval_ms();
2548 static int64_t lastPower
;
2549 bool waitMore
= lastPower
+ 2000 > current
;
2550 if (powerDropout
&& !waitMore
) {
2551 runPowerDropout
= 1;
2552 lastPower
= current
;
2555 retval
= jtag_srst_asserted(&srstAsserted
);
2556 if (retval
!= ERROR_OK
)
2560 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2562 static int64_t lastSrst
;
2563 waitMore
= lastSrst
+ 2000 > current
;
2564 if (srstDeasserted
&& !waitMore
) {
2565 runSrstDeasserted
= 1;
2569 if (!prevSrstAsserted
&& srstAsserted
)
2570 runSrstAsserted
= 1;
2572 prevSrstAsserted
= srstAsserted
;
2573 prevPowerdropout
= powerDropout
;
2575 if (srstDeasserted
|| powerRestored
) {
2576 /* Other than logging the event we can't do anything here.
2577 * Issuing a reset is a particularly bad idea as we might
2578 * be inside a reset already.
2585 /* process target state changes */
2586 static int handle_target(void *priv
)
2588 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2589 int retval
= ERROR_OK
;
2591 if (!is_jtag_poll_safe()) {
2592 /* polling is disabled currently */
2596 /* we do not want to recurse here... */
2597 static int recursive
;
2601 /* danger! running these procedures can trigger srst assertions and power dropouts.
2602 * We need to avoid an infinite loop/recursion here and we do that by
2603 * clearing the flags after running these events.
2605 int did_something
= 0;
2606 if (runSrstAsserted
) {
2607 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2608 Jim_Eval(interp
, "srst_asserted");
2611 if (runSrstDeasserted
) {
2612 Jim_Eval(interp
, "srst_deasserted");
2615 if (runPowerDropout
) {
2616 LOG_INFO("Power dropout detected, running power_dropout proc.");
2617 Jim_Eval(interp
, "power_dropout");
2620 if (runPowerRestore
) {
2621 Jim_Eval(interp
, "power_restore");
2625 if (did_something
) {
2626 /* clear detect flags */
2630 /* clear action flags */
2632 runSrstAsserted
= 0;
2633 runSrstDeasserted
= 0;
2634 runPowerRestore
= 0;
2635 runPowerDropout
= 0;
2640 /* Poll targets for state changes unless that's globally disabled.
2641 * Skip targets that are currently disabled.
2643 for (struct target
*target
= all_targets
;
2644 is_jtag_poll_safe() && target
;
2645 target
= target
->next
) {
2647 if (!target_was_examined(target
))
2650 if (!target
->tap
->enabled
)
2653 if (target
->backoff
.times
> target
->backoff
.count
) {
2654 /* do not poll this time as we failed previously */
2655 target
->backoff
.count
++;
2658 target
->backoff
.count
= 0;
2660 /* only poll target if we've got power and srst isn't asserted */
2661 if (!powerDropout
&& !srstAsserted
) {
2662 /* polling may fail silently until the target has been examined */
2663 retval
= target_poll(target
);
2664 if (retval
!= ERROR_OK
) {
2665 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2666 if (target
->backoff
.times
* polling_interval
< 5000) {
2667 target
->backoff
.times
*= 2;
2668 target
->backoff
.times
++;
2671 /* Tell GDB to halt the debugger. This allows the user to
2672 * run monitor commands to handle the situation.
2674 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2676 if (target
->backoff
.times
> 0) {
2677 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2678 target_reset_examined(target
);
2679 retval
= target_examine_one(target
);
2680 /* Target examination could have failed due to unstable connection,
2681 * but we set the examined flag anyway to repoll it later */
2682 if (retval
!= ERROR_OK
) {
2683 target
->examined
= true;
2684 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2685 target
->backoff
.times
* polling_interval
);
2690 /* Since we succeeded, we reset backoff count */
2691 target
->backoff
.times
= 0;
2698 COMMAND_HANDLER(handle_reg_command
)
2700 struct target
*target
;
2701 struct reg
*reg
= NULL
;
2707 target
= get_current_target(CMD_CTX
);
2709 /* list all available registers for the current target */
2710 if (CMD_ARGC
== 0) {
2711 struct reg_cache
*cache
= target
->reg_cache
;
2717 command_print(CMD_CTX
, "===== %s", cache
->name
);
2719 for (i
= 0, reg
= cache
->reg_list
;
2720 i
< cache
->num_regs
;
2721 i
++, reg
++, count
++) {
2722 /* only print cached values if they are valid */
2724 value
= buf_to_str(reg
->value
,
2726 command_print(CMD_CTX
,
2727 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2735 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2740 cache
= cache
->next
;
2746 /* access a single register by its ordinal number */
2747 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2749 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2751 struct reg_cache
*cache
= target
->reg_cache
;
2755 for (i
= 0; i
< cache
->num_regs
; i
++) {
2756 if (count
++ == num
) {
2757 reg
= &cache
->reg_list
[i
];
2763 cache
= cache
->next
;
2767 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2768 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2772 /* access a single register by its name */
2773 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2776 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2781 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2783 /* display a register */
2784 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2785 && (CMD_ARGV
[1][0] <= '9')))) {
2786 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2789 if (reg
->valid
== 0)
2790 reg
->type
->get(reg
);
2791 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2792 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2797 /* set register value */
2798 if (CMD_ARGC
== 2) {
2799 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2802 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2804 reg
->type
->set(reg
, buf
);
2806 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2807 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2815 return ERROR_COMMAND_SYNTAX_ERROR
;
2818 COMMAND_HANDLER(handle_poll_command
)
2820 int retval
= ERROR_OK
;
2821 struct target
*target
= get_current_target(CMD_CTX
);
2823 if (CMD_ARGC
== 0) {
2824 command_print(CMD_CTX
, "background polling: %s",
2825 jtag_poll_get_enabled() ?
"on" : "off");
2826 command_print(CMD_CTX
, "TAP: %s (%s)",
2827 target
->tap
->dotted_name
,
2828 target
->tap
->enabled ?
"enabled" : "disabled");
2829 if (!target
->tap
->enabled
)
2831 retval
= target_poll(target
);
2832 if (retval
!= ERROR_OK
)
2834 retval
= target_arch_state(target
);
2835 if (retval
!= ERROR_OK
)
2837 } else if (CMD_ARGC
== 1) {
2839 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2840 jtag_poll_set_enabled(enable
);
2842 return ERROR_COMMAND_SYNTAX_ERROR
;
2847 COMMAND_HANDLER(handle_wait_halt_command
)
2850 return ERROR_COMMAND_SYNTAX_ERROR
;
2852 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2853 if (1 == CMD_ARGC
) {
2854 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2855 if (ERROR_OK
!= retval
)
2856 return ERROR_COMMAND_SYNTAX_ERROR
;
2859 struct target
*target
= get_current_target(CMD_CTX
);
2860 return target_wait_state(target
, TARGET_HALTED
, ms
);
2863 /* wait for target state to change. The trick here is to have a low
2864 * latency for short waits and not to suck up all the CPU time
2867 * After 500ms, keep_alive() is invoked
2869 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2872 int64_t then
= 0, cur
;
2876 retval
= target_poll(target
);
2877 if (retval
!= ERROR_OK
)
2879 if (target
->state
== state
)
2884 then
= timeval_ms();
2885 LOG_DEBUG("waiting for target %s...",
2886 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2892 if ((cur
-then
) > ms
) {
2893 LOG_ERROR("timed out while waiting for target %s",
2894 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2902 COMMAND_HANDLER(handle_halt_command
)
2906 struct target
*target
= get_current_target(CMD_CTX
);
2907 int retval
= target_halt(target
);
2908 if (ERROR_OK
!= retval
)
2911 if (CMD_ARGC
== 1) {
2912 unsigned wait_local
;
2913 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2914 if (ERROR_OK
!= retval
)
2915 return ERROR_COMMAND_SYNTAX_ERROR
;
2920 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2923 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2925 struct target
*target
= get_current_target(CMD_CTX
);
2927 LOG_USER("requesting target halt and executing a soft reset");
2929 target_soft_reset_halt(target
);
2934 COMMAND_HANDLER(handle_reset_command
)
2937 return ERROR_COMMAND_SYNTAX_ERROR
;
2939 enum target_reset_mode reset_mode
= RESET_RUN
;
2940 if (CMD_ARGC
== 1) {
2942 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2943 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2944 return ERROR_COMMAND_SYNTAX_ERROR
;
2945 reset_mode
= n
->value
;
2948 /* reset *all* targets */
2949 return target_process_reset(CMD_CTX
, reset_mode
);
2953 COMMAND_HANDLER(handle_resume_command
)
2957 return ERROR_COMMAND_SYNTAX_ERROR
;
2959 struct target
*target
= get_current_target(CMD_CTX
);
2961 /* with no CMD_ARGV, resume from current pc, addr = 0,
2962 * with one arguments, addr = CMD_ARGV[0],
2963 * handle breakpoints, not debugging */
2964 target_addr_t addr
= 0;
2965 if (CMD_ARGC
== 1) {
2966 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2970 return target_resume(target
, current
, addr
, 1, 0);
2973 COMMAND_HANDLER(handle_step_command
)
2976 return ERROR_COMMAND_SYNTAX_ERROR
;
2980 /* with no CMD_ARGV, step from current pc, addr = 0,
2981 * with one argument addr = CMD_ARGV[0],
2982 * handle breakpoints, debugging */
2983 target_addr_t addr
= 0;
2985 if (CMD_ARGC
== 1) {
2986 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2990 struct target
*target
= get_current_target(CMD_CTX
);
2992 return target
->type
->step(target
, current_pc
, addr
, 1);
2995 static void handle_md_output(struct command_context
*cmd_ctx
,
2996 struct target
*target
, target_addr_t address
, unsigned size
,
2997 unsigned count
, const uint8_t *buffer
)
2999 const unsigned line_bytecnt
= 32;
3000 unsigned line_modulo
= line_bytecnt
/ size
;
3002 char output
[line_bytecnt
* 4 + 1];
3003 unsigned output_len
= 0;
3005 const char *value_fmt
;
3008 value_fmt
= "%16.16llx ";
3011 value_fmt
= "%8.8x ";
3014 value_fmt
= "%4.4x ";
3017 value_fmt
= "%2.2x ";
3020 /* "can't happen", caller checked */
3021 LOG_ERROR("invalid memory read size: %u", size
);
3025 for (unsigned i
= 0; i
< count
; i
++) {
3026 if (i
% line_modulo
== 0) {
3027 output_len
+= snprintf(output
+ output_len
,
3028 sizeof(output
) - output_len
,
3029 TARGET_ADDR_FMT
": ",
3030 (address
+ (i
* size
)));
3034 const uint8_t *value_ptr
= buffer
+ i
* size
;
3037 value
= target_buffer_get_u64(target
, value_ptr
);
3040 value
= target_buffer_get_u32(target
, value_ptr
);
3043 value
= target_buffer_get_u16(target
, value_ptr
);
3048 output_len
+= snprintf(output
+ output_len
,
3049 sizeof(output
) - output_len
,
3052 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3053 command_print(cmd_ctx
, "%s", output
);
3059 COMMAND_HANDLER(handle_md_command
)
3062 return ERROR_COMMAND_SYNTAX_ERROR
;
3065 switch (CMD_NAME
[2]) {
3079 return ERROR_COMMAND_SYNTAX_ERROR
;
3082 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3083 int (*fn
)(struct target
*target
,
3084 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3088 fn
= target_read_phys_memory
;
3090 fn
= target_read_memory
;
3091 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3092 return ERROR_COMMAND_SYNTAX_ERROR
;
3094 target_addr_t address
;
3095 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3099 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3101 uint8_t *buffer
= calloc(count
, size
);
3103 struct target
*target
= get_current_target(CMD_CTX
);
3104 int retval
= fn(target
, address
, size
, count
, buffer
);
3105 if (ERROR_OK
== retval
)
3106 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3113 typedef int (*target_write_fn
)(struct target
*target
,
3114 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3116 static int target_fill_mem(struct target
*target
,
3117 target_addr_t address
,
3125 /* We have to write in reasonably large chunks to be able
3126 * to fill large memory areas with any sane speed */
3127 const unsigned chunk_size
= 16384;
3128 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3129 if (target_buf
== NULL
) {
3130 LOG_ERROR("Out of memory");
3134 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3135 switch (data_size
) {
3137 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3140 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3143 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3146 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3153 int retval
= ERROR_OK
;
3155 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3158 if (current
> chunk_size
)
3159 current
= chunk_size
;
3160 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3161 if (retval
!= ERROR_OK
)
3163 /* avoid GDB timeouts */
3172 COMMAND_HANDLER(handle_mw_command
)
3175 return ERROR_COMMAND_SYNTAX_ERROR
;
3176 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3181 fn
= target_write_phys_memory
;
3183 fn
= target_write_memory
;
3184 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))