Introduce ARCv2 architecture related code
[openocd.git] / src / target / target.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
19 * *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
22 * *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
25 * *
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. *
30 * *
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. *
35 * *
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 ***************************************************************************/
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
47
48 #include "target.h"
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
52 #include "register.h"
53 #include "trace.h"
54 #include "image.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
57 #include "arm_cti.h"
58
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
61
62 static int target_read_buffer_default(struct target *target, target_addr_t address,
63 uint32_t count, uint8_t *buffer);
64 static int target_write_buffer_default(struct target *target, target_addr_t address,
65 uint32_t count, const uint8_t *buffer);
66 static int target_array2mem(Jim_Interp *interp, struct target *target,
67 int argc, Jim_Obj * const *argv);
68 static int target_mem2array(Jim_Interp *interp, struct target *target,
69 int argc, Jim_Obj * const *argv);
70 static int target_register_user_commands(struct command_context *cmd_ctx);
71 static int target_get_gdb_fileio_info_default(struct target *target,
72 struct gdb_fileio_info *fileio_info);
73 static int target_gdb_fileio_end_default(struct target *target, int retcode,
74 int fileio_errno, bool ctrl_c);
75 static int target_profiling_default(struct target *target, uint32_t *samples,
76 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
77
78 /* targets */
79 extern struct target_type arm7tdmi_target;
80 extern struct target_type arm720t_target;
81 extern struct target_type arm9tdmi_target;
82 extern struct target_type arm920t_target;
83 extern struct target_type arm966e_target;
84 extern struct target_type arm946e_target;
85 extern struct target_type arm926ejs_target;
86 extern struct target_type fa526_target;
87 extern struct target_type feroceon_target;
88 extern struct target_type dragonite_target;
89 extern struct target_type xscale_target;
90 extern struct target_type cortexm_target;
91 extern struct target_type cortexa_target;
92 extern struct target_type aarch64_target;
93 extern struct target_type cortexr4_target;
94 extern struct target_type arm11_target;
95 extern struct target_type ls1_sap_target;
96 extern struct target_type mips_m4k_target;
97 extern struct target_type mips_mips64_target;
98 extern struct target_type avr_target;
99 extern struct target_type dsp563xx_target;
100 extern struct target_type dsp5680xx_target;
101 extern struct target_type testee_target;
102 extern struct target_type avr32_ap7k_target;
103 extern struct target_type hla_target;
104 extern struct target_type nds32_v2_target;
105 extern struct target_type nds32_v3_target;
106 extern struct target_type nds32_v3m_target;
107 extern struct target_type or1k_target;
108 extern struct target_type quark_x10xx_target;
109 extern struct target_type quark_d20xx_target;
110 extern struct target_type stm8_target;
111 extern struct target_type riscv_target;
112 extern struct target_type mem_ap_target;
113 extern struct target_type esirisc_target;
114 extern struct target_type arcv2_target;
115
116 static struct target_type *target_types[] = {
117 &arm7tdmi_target,
118 &arm9tdmi_target,
119 &arm920t_target,
120 &arm720t_target,
121 &arm966e_target,
122 &arm946e_target,
123 &arm926ejs_target,
124 &fa526_target,
125 &feroceon_target,
126 &dragonite_target,
127 &xscale_target,
128 &cortexm_target,
129 &cortexa_target,
130 &cortexr4_target,
131 &arm11_target,
132 &ls1_sap_target,
133 &mips_m4k_target,
134 &avr_target,
135 &dsp563xx_target,
136 &dsp5680xx_target,
137 &testee_target,
138 &avr32_ap7k_target,
139 &hla_target,
140 &nds32_v2_target,
141 &nds32_v3_target,
142 &nds32_v3m_target,
143 &or1k_target,
144 &quark_x10xx_target,
145 &quark_d20xx_target,
146 &stm8_target,
147 &riscv_target,
148 &mem_ap_target,
149 &esirisc_target,
150 &arcv2_target,
151 #if BUILD_TARGET64
152 &aarch64_target,
153 &mips_mips64_target,
154 #endif
155 NULL,
156 };
157
158 struct target *all_targets;
159 static struct target_event_callback *target_event_callbacks;
160 static struct target_timer_callback *target_timer_callbacks;
161 LIST_HEAD(target_reset_callback_list);
162 LIST_HEAD(target_trace_callback_list);
163 static const int polling_interval = 100;
164
165 static const Jim_Nvp nvp_assert[] = {
166 { .name = "assert", NVP_ASSERT },
167 { .name = "deassert", NVP_DEASSERT },
168 { .name = "T", NVP_ASSERT },
169 { .name = "F", NVP_DEASSERT },
170 { .name = "t", NVP_ASSERT },
171 { .name = "f", NVP_DEASSERT },
172 { .name = NULL, .value = -1 }
173 };
174
175 static const Jim_Nvp nvp_error_target[] = {
176 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
177 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
178 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
179 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
180 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
181 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
182 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
183 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
184 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
185 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
186 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
187 { .value = -1, .name = NULL }
188 };
189
190 static const char *target_strerror_safe(int err)
191 {
192 const Jim_Nvp *n;
193
194 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
195 if (n->name == NULL)
196 return "unknown";
197 else
198 return n->name;
199 }
200
201 static const Jim_Nvp nvp_target_event[] = {
202
203 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
204 { .value = TARGET_EVENT_HALTED, .name = "halted" },
205 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
206 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
207 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
208
209 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
210 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
211
212 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
213 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
214 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
215 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
216 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
217 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
218 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
219 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
220
221 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
222 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
223
224 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
225 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
226
227 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
228 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
229
230 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
231 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
232
233 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
234 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
235
236 { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
237
238 { .name = NULL, .value = -1 }
239 };
240
241 static const Jim_Nvp nvp_target_state[] = {
242 { .name = "unknown", .value = TARGET_UNKNOWN },
243 { .name = "running", .value = TARGET_RUNNING },
244 { .name = "halted", .value = TARGET_HALTED },
245 { .name = "reset", .value = TARGET_RESET },
246 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
247 { .name = NULL, .value = -1 },
248 };
249
250 static const Jim_Nvp nvp_target_debug_reason[] = {
251 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
252 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
253 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
254 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
255 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
256 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
257 { .name = "program-exit" , .value = DBG_REASON_EXIT },
258 { .name = "exception-catch" , .value = DBG_REASON_EXC_CATCH },
259 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
260 { .name = NULL, .value = -1 },
261 };
262
263 static const Jim_Nvp nvp_target_endian[] = {
264 { .name = "big", .value = TARGET_BIG_ENDIAN },
265 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
266 { .name = "be", .value = TARGET_BIG_ENDIAN },
267 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
268 { .name = NULL, .value = -1 },
269 };
270
271 static const Jim_Nvp nvp_reset_modes[] = {
272 { .name = "unknown", .value = RESET_UNKNOWN },
273 { .name = "run" , .value = RESET_RUN },
274 { .name = "halt" , .value = RESET_HALT },
275 { .name = "init" , .value = RESET_INIT },
276 { .name = NULL , .value = -1 },
277 };
278
279 const char *debug_reason_name(struct target *t)
280 {
281 const char *cp;
282
283 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
284 t->debug_reason)->name;
285 if (!cp) {
286 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
287 cp = "(*BUG*unknown*BUG*)";
288 }
289 return cp;
290 }
291
292 const char *target_state_name(struct target *t)
293 {
294 const char *cp;
295 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
296 if (!cp) {
297 LOG_ERROR("Invalid target state: %d", (int)(t->state));
298 cp = "(*BUG*unknown*BUG*)";
299 }
300
301 if (!target_was_examined(t) && t->defer_examine)
302 cp = "examine deferred";
303
304 return cp;
305 }
306
307 const char *target_event_name(enum target_event event)
308 {
309 const char *cp;
310 cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
311 if (!cp) {
312 LOG_ERROR("Invalid target event: %d", (int)(event));
313 cp = "(*BUG*unknown*BUG*)";
314 }
315 return cp;
316 }
317
318 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
319 {
320 const char *cp;
321 cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
322 if (!cp) {
323 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
324 cp = "(*BUG*unknown*BUG*)";
325 }
326 return cp;
327 }
328
329 /* determine the number of the new target */
330 static int new_target_number(void)
331 {
332 struct target *t;
333 int x;
334
335 /* number is 0 based */
336 x = -1;
337 t = all_targets;
338 while (t) {
339 if (x < t->target_number)
340 x = t->target_number;
341 t = t->next;
342 }
343 return x + 1;
344 }
345
346 /* read a uint64_t from a buffer in target memory endianness */
347 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
348 {
349 if (target->endianness == TARGET_LITTLE_ENDIAN)
350 return le_to_h_u64(buffer);
351 else
352 return be_to_h_u64(buffer);
353 }
354
355 /* read a uint32_t from a buffer in target memory endianness */
356 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
357 {
358 if (target->endianness == TARGET_LITTLE_ENDIAN)
359 return le_to_h_u32(buffer);
360 else
361 return be_to_h_u32(buffer);
362 }
363
364 /* read a uint24_t from a buffer in target memory endianness */
365 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
366 {
367 if (target->endianness == TARGET_LITTLE_ENDIAN)
368 return le_to_h_u24(buffer);
369 else
370 return be_to_h_u24(buffer);
371 }
372
373 /* read a uint16_t from a buffer in target memory endianness */
374 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
375 {
376 if (target->endianness == TARGET_LITTLE_ENDIAN)
377 return le_to_h_u16(buffer);
378 else
379 return be_to_h_u16(buffer);
380 }
381
382 /* write a uint64_t to a buffer in target memory endianness */
383 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
384 {
385 if (target->endianness == TARGET_LITTLE_ENDIAN)
386 h_u64_to_le(buffer, value);
387 else
388 h_u64_to_be(buffer, value);
389 }
390
391 /* write a uint32_t to a buffer in target memory endianness */
392 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
393 {
394 if (target->endianness == TARGET_LITTLE_ENDIAN)
395 h_u32_to_le(buffer, value);
396 else
397 h_u32_to_be(buffer, value);
398 }
399
400 /* write a uint24_t to a buffer in target memory endianness */
401 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
402 {
403 if (target->endianness == TARGET_LITTLE_ENDIAN)
404 h_u24_to_le(buffer, value);
405 else
406 h_u24_to_be(buffer, value);
407 }
408
409 /* write a uint16_t to a buffer in target memory endianness */
410 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
411 {
412 if (target->endianness == TARGET_LITTLE_ENDIAN)
413 h_u16_to_le(buffer, value);
414 else
415 h_u16_to_be(buffer, value);
416 }
417
418 /* write a uint8_t to a buffer in target memory endianness */
419 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
420 {
421 *buffer = value;
422 }
423
424 /* write a uint64_t array to a buffer in target memory endianness */
425 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
426 {
427 uint32_t i;
428 for (i = 0; i < count; i++)
429 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
430 }
431
432 /* write a uint32_t array to a buffer in target memory endianness */
433 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
434 {
435 uint32_t i;
436 for (i = 0; i < count; i++)
437 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
438 }
439
440 /* write a uint16_t array to a buffer in target memory endianness */
441 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
442 {
443 uint32_t i;
444 for (i = 0; i < count; i++)
445 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
446 }
447
448 /* write a uint64_t array to a buffer in target memory endianness */
449 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
450 {
451 uint32_t i;
452 for (i = 0; i < count; i++)
453 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
454 }
455
456 /* write a uint32_t array to a buffer in target memory endianness */
457 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
458 {
459 uint32_t i;
460 for (i = 0; i < count; i++)
461 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
462 }
463
464 /* write a uint16_t array to a buffer in target memory endianness */
465 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
466 {
467 uint32_t i;
468 for (i = 0; i < count; i++)
469 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
470 }
471
472 /* return a pointer to a configured target; id is name or number */
473 struct target *get_target(const char *id)
474 {
475 struct target *target;
476
477 /* try as tcltarget name */
478 for (target = all_targets; target; target = target->next) {
479 if (target_name(target) == NULL)
480 continue;
481 if (strcmp(id, target_name(target)) == 0)
482 return target;
483 }
484
485 /* It's OK to remove this fallback sometime after August 2010 or so */
486
487 /* no match, try as number */
488 unsigned num;
489 if (parse_uint(id, &num) != ERROR_OK)
490 return NULL;
491
492 for (target = all_targets; target; target = target->next) {
493 if (target->target_number == (int)num) {
494 LOG_WARNING("use '%s' as target identifier, not '%u'",
495 target_name(target), num);
496 return target;
497 }
498 }
499
500 return NULL;
501 }
502
503 /* returns a pointer to the n-th configured target */
504 struct target *get_target_by_num(int num)
505 {
506 struct target *target = all_targets;
507
508 while (target) {
509 if (target->target_number == num)
510 return target;
511 target = target->next;
512 }
513
514 return NULL;
515 }
516
517 struct target *get_current_target(struct command_context *cmd_ctx)
518 {
519 struct target *target = get_current_target_or_null(cmd_ctx);
520
521 if (target == NULL) {
522 LOG_ERROR("BUG: current_target out of bounds");
523 exit(-1);
524 }
525
526 return target;
527 }
528
529 struct target *get_current_target_or_null(struct command_context *cmd_ctx)
530 {
531 return cmd_ctx->current_target_override
532 ? cmd_ctx->current_target_override
533 : cmd_ctx->current_target;
534 }
535
536 int target_poll(struct target *target)
537 {
538 int retval;
539
540 /* We can't poll until after examine */
541 if (!target_was_examined(target)) {
542 /* Fail silently lest we pollute the log */
543 return ERROR_FAIL;
544 }
545
546 retval = target->type->poll(target);
547 if (retval != ERROR_OK)
548 return retval;
549
550 if (target->halt_issued) {
551 if (target->state == TARGET_HALTED)
552 target->halt_issued = false;
553 else {
554 int64_t t = timeval_ms() - target->halt_issued_time;
555 if (t > DEFAULT_HALT_TIMEOUT) {
556 target->halt_issued = false;
557 LOG_INFO("Halt timed out, wake up GDB.");
558 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
559 }
560 }
561 }
562
563 return ERROR_OK;
564 }
565
566 int target_halt(struct target *target)
567 {
568 int retval;
569 /* We can't poll until after examine */
570 if (!target_was_examined(target)) {
571 LOG_ERROR("Target not examined yet");
572 return ERROR_FAIL;
573 }
574
575 retval = target->type->halt(target);
576 if (retval != ERROR_OK)
577 return retval;
578
579 target->halt_issued = true;
580 target->halt_issued_time = timeval_ms();
581
582 return ERROR_OK;
583 }
584
585 /**
586 * Make the target (re)start executing using its saved execution
587 * context (possibly with some modifications).
588 *
589 * @param target Which target should start executing.
590 * @param current True to use the target's saved program counter instead
591 * of the address parameter
592 * @param address Optionally used as the program counter.
593 * @param handle_breakpoints True iff breakpoints at the resumption PC
594 * should be skipped. (For example, maybe execution was stopped by
595 * such a breakpoint, in which case it would be counterprodutive to
596 * let it re-trigger.
597 * @param debug_execution False if all working areas allocated by OpenOCD
598 * should be released and/or restored to their original contents.
599 * (This would for example be true to run some downloaded "helper"
600 * algorithm code, which resides in one such working buffer and uses
601 * another for data storage.)
602 *
603 * @todo Resolve the ambiguity about what the "debug_execution" flag
604 * signifies. For example, Target implementations don't agree on how
605 * it relates to invalidation of the register cache, or to whether
606 * breakpoints and watchpoints should be enabled. (It would seem wrong
607 * to enable breakpoints when running downloaded "helper" algorithms
608 * (debug_execution true), since the breakpoints would be set to match
609 * target firmware being debugged, not the helper algorithm.... and
610 * enabling them could cause such helpers to malfunction (for example,
611 * by overwriting data with a breakpoint instruction. On the other
612 * hand the infrastructure for running such helpers might use this
613 * procedure but rely on hardware breakpoint to detect termination.)
614 */
615 int target_resume(struct target *target, int current, target_addr_t address,
616 int handle_breakpoints, int debug_execution)
617 {
618 int retval;
619
620 /* We can't poll until after examine */
621 if (!target_was_examined(target)) {
622 LOG_ERROR("Target not examined yet");
623 return ERROR_FAIL;
624 }
625
626 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
627
628 /* note that resume *must* be asynchronous. The CPU can halt before
629 * we poll. The CPU can even halt at the current PC as a result of
630 * a software breakpoint being inserted by (a bug?) the application.
631 */
632 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
633 if (retval != ERROR_OK)
634 return retval;
635
636 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
637
638 return retval;
639 }
640
641 static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
642 {
643 char buf[100];
644 int retval;
645 Jim_Nvp *n;
646 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
647 if (n->name == NULL) {
648 LOG_ERROR("invalid reset mode");
649 return ERROR_FAIL;
650 }
651
652 struct target *target;
653 for (target = all_targets; target; target = target->next)
654 target_call_reset_callbacks(target, reset_mode);
655
656 /* disable polling during reset to make reset event scripts
657 * more predictable, i.e. dr/irscan & pathmove in events will
658 * not have JTAG operations injected into the middle of a sequence.
659 */
660 bool save_poll = jtag_poll_get_enabled();
661
662 jtag_poll_set_enabled(false);
663
664 sprintf(buf, "ocd_process_reset %s", n->name);
665 retval = Jim_Eval(cmd->ctx->interp, buf);
666
667 jtag_poll_set_enabled(save_poll);
668
669 if (retval != JIM_OK) {
670 Jim_MakeErrorMessage(cmd->ctx->interp);
671 command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
672 return ERROR_FAIL;
673 }
674
675 /* We want any events to be processed before the prompt */
676 retval = target_call_timer_callbacks_now();
677
678 for (target = all_targets; target; target = target->next) {
679 target->type->check_reset(target);
680 target->running_alg = false;
681 }
682
683 return retval;
684 }
685
686 static int identity_virt2phys(struct target *target,
687 target_addr_t virtual, target_addr_t *physical)
688 {
689 *physical = virtual;
690 return ERROR_OK;
691 }
692
693 static int no_mmu(struct target *target, int *enabled)
694 {
695 *enabled = 0;
696 return ERROR_OK;
697 }
698
699 static int default_examine(struct target *target)
700 {
701 target_set_examined(target);
702 return ERROR_OK;
703 }
704
705 /* no check by default */
706 static int default_check_reset(struct target *target)
707 {
708 return ERROR_OK;
709 }
710
711 int target_examine_one(struct target *target)
712 {
713 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
714
715 int retval = target->type->examine(target);
716 if (retval != ERROR_OK)
717 return retval;
718
719 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
720
721 return ERROR_OK;
722 }
723
724 static int jtag_enable_callback(enum jtag_event event, void *priv)
725 {
726 struct target *target = priv;
727
728 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
729 return ERROR_OK;
730
731 jtag_unregister_event_callback(jtag_enable_callback, target);
732
733 return target_examine_one(target);
734 }
735
736 /* Targets that correctly implement init + examine, i.e.
737 * no communication with target during init:
738 *
739 * XScale
740 */
741 int target_examine(void)
742 {
743 int retval = ERROR_OK;
744 struct target *target;
745
746 for (target = all_targets; target; target = target->next) {
747 /* defer examination, but don't skip it */
748 if (!target->tap->enabled) {
749 jtag_register_event_callback(jtag_enable_callback,
750 target);
751 continue;
752 }
753
754 if (target->defer_examine)
755 continue;
756
757 retval = target_examine_one(target);
758 if (retval != ERROR_OK)
759 return retval;
760 }
761 return retval;
762 }
763
764 const char *target_type_name(struct target *target)
765 {
766 return target->type->name;
767 }
768
769 static int target_soft_reset_halt(struct target *target)
770 {
771 if (!target_was_examined(target)) {
772 LOG_ERROR("Target not examined yet");
773 return ERROR_FAIL;
774 }
775 if (!target->type->soft_reset_halt) {
776 LOG_ERROR("Target %s does not support soft_reset_halt",
777 target_name(target));
778 return ERROR_FAIL;
779 }
780 return target->type->soft_reset_halt(target);
781 }
782
783 /**
784 * Downloads a target-specific native code algorithm to the target,
785 * and executes it. * Note that some targets may need to set up, enable,
786 * and tear down a breakpoint (hard or * soft) to detect algorithm
787 * termination, while others may support lower overhead schemes where
788 * soft breakpoints embedded in the algorithm automatically terminate the
789 * algorithm.
790 *
791 * @param target used to run the algorithm
792 * @param arch_info target-specific description of the algorithm.
793 */
794 int target_run_algorithm(struct target *target,
795 int num_mem_params, struct mem_param *mem_params,
796 int num_reg_params, struct reg_param *reg_param,
797 uint32_t entry_point, uint32_t exit_point,
798 int timeout_ms, void *arch_info)
799 {
800 int retval = ERROR_FAIL;
801
802 if (!target_was_examined(target)) {
803 LOG_ERROR("Target not examined yet");
804 goto done;
805 }
806 if (!target->type->run_algorithm) {
807 LOG_ERROR("Target type '%s' does not support %s",
808 target_type_name(target), __func__);
809 goto done;
810 }
811
812 target->running_alg = true;
813 retval = target->type->run_algorithm(target,
814 num_mem_params, mem_params,
815 num_reg_params, reg_param,
816 entry_point, exit_point, timeout_ms, arch_info);
817 target->running_alg = false;
818
819 done:
820 return retval;
821 }
822
823 /**
824 * Executes a target-specific native code algorithm and leaves it running.
825 *
826 * @param target used to run the algorithm
827 * @param arch_info target-specific description of the algorithm.
828 */
829 int target_start_algorithm(struct target *target,
830 int num_mem_params, struct mem_param *mem_params,
831 int num_reg_params, struct reg_param *reg_params,
832 uint32_t entry_point, uint32_t exit_point,
833 void *arch_info)
834 {
835 int retval = ERROR_FAIL;
836
837 if (!target_was_examined(target)) {
838 LOG_ERROR("Target not examined yet");
839 goto done;
840 }
841 if (!target->type->start_algorithm) {
842 LOG_ERROR("Target type '%s' does not support %s",
843 target_type_name(target), __func__);
844 goto done;
845 }
846 if (target->running_alg) {
847 LOG_ERROR("Target is already running an algorithm");
848 goto done;
849 }
850
851 target->running_alg = true;
852 retval = target->type->start_algorithm(target,
853 num_mem_params, mem_params,
854 num_reg_params, reg_params,
855 entry_point, exit_point, arch_info);
856
857 done:
858 return retval;
859 }
860
861 /**
862 * Waits for an algorithm started with target_start_algorithm() to complete.
863 *
864 * @param target used to run the algorithm
865 * @param arch_info target-specific description of the algorithm.
866 */
867 int target_wait_algorithm(struct target *target,
868 int num_mem_params, struct mem_param *mem_params,
869 int num_reg_params, struct reg_param *reg_params,
870 uint32_t exit_point, int timeout_ms,
871 void *arch_info)
872 {
873 int retval = ERROR_FAIL;
874
875 if (!target->type->wait_algorithm) {
876 LOG_ERROR("Target type '%s' does not support %s",
877 target_type_name(target), __func__);
878 goto done;
879 }
880 if (!target->running_alg) {
881 LOG_ERROR("Target is not running an algorithm");
882 goto done;
883 }
884
885 retval = target->type->wait_algorithm(target,
886 num_mem_params, mem_params,
887 num_reg_params, reg_params,
888 exit_point, timeout_ms, arch_info);
889 if (retval != ERROR_TARGET_TIMEOUT)
890 target->running_alg = false;
891
892 done:
893 return retval;
894 }
895
896 /**
897 * Streams data to a circular buffer on target intended for consumption by code
898 * running asynchronously on target.
899 *
900 * This is intended for applications where target-specific native code runs
901 * on the target, receives data from the circular buffer, does something with
902 * it (most likely writing it to a flash memory), and advances the circular
903 * buffer pointer.
904 *
905 * This assumes that the helper algorithm has already been loaded to the target,
906 * but has not been started yet. Given memory and register parameters are passed
907 * to the algorithm.
908 *
909 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
910 * following format:
911 *
912 * [buffer_start + 0, buffer_start + 4):
913 * Write Pointer address (aka head). Written and updated by this
914 * routine when new data is written to the circular buffer.
915 * [buffer_start + 4, buffer_start + 8):
916 * Read Pointer address (aka tail). Updated by code running on the
917 * target after it consumes data.
918 * [buffer_start + 8, buffer_start + buffer_size):
919 * Circular buffer contents.
920 *
921 * See contrib/loaders/flash/stm32f1x.S for an example.
922 *
923 * @param target used to run the algorithm
924 * @param buffer address on the host where data to be sent is located
925 * @param count number of blocks to send
926 * @param block_size size in bytes of each block
927 * @param num_mem_params count of memory-based params to pass to algorithm
928 * @param mem_params memory-based params to pass to algorithm
929 * @param num_reg_params count of register-based params to pass to algorithm
930 * @param reg_params memory-based params to pass to algorithm
931 * @param buffer_start address on the target of the circular buffer structure
932 * @param buffer_size size of the circular buffer structure
933 * @param entry_point address on the target to execute to start the algorithm
934 * @param exit_point address at which to set a breakpoint to catch the
935 * end of the algorithm; can be 0 if target triggers a breakpoint itself
936 */
937
938 int target_run_flash_async_algorithm(struct target *target,
939 const uint8_t *buffer, uint32_t count, int block_size,
940 int num_mem_params, struct mem_param *mem_params,
941 int num_reg_params, struct reg_param *reg_params,
942 uint32_t buffer_start, uint32_t buffer_size,
943 uint32_t entry_point, uint32_t exit_point, void *arch_info)
944 {
945 int retval;
946 int timeout = 0;
947
948 const uint8_t *buffer_orig = buffer;
949
950 /* Set up working area. First word is write pointer, second word is read pointer,
951 * rest is fifo data area. */
952 uint32_t wp_addr = buffer_start;
953 uint32_t rp_addr = buffer_start + 4;
954 uint32_t fifo_start_addr = buffer_start + 8;
955 uint32_t fifo_end_addr = buffer_start + buffer_size;
956
957 uint32_t wp = fifo_start_addr;
958 uint32_t rp = fifo_start_addr;
959
960 /* validate block_size is 2^n */
961 assert(!block_size || !(block_size & (block_size - 1)));
962
963 retval = target_write_u32(target, wp_addr, wp);
964 if (retval != ERROR_OK)
965 return retval;
966 retval = target_write_u32(target, rp_addr, rp);
967 if (retval != ERROR_OK)
968 return retval;
969
970 /* Start up algorithm on target and let it idle while writing the first chunk */
971 retval = target_start_algorithm(target, num_mem_params, mem_params,
972 num_reg_params, reg_params,
973 entry_point,
974 exit_point,
975 arch_info);
976
977 if (retval != ERROR_OK) {
978 LOG_ERROR("error starting target flash write algorithm");
979 return retval;
980 }
981
982 while (count > 0) {
983
984 retval = target_read_u32(target, rp_addr, &rp);
985 if (retval != ERROR_OK) {
986 LOG_ERROR("failed to get read pointer");
987 break;
988 }
989
990 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
991 (size_t) (buffer - buffer_orig), count, wp, rp);
992
993 if (rp == 0) {
994 LOG_ERROR("flash write algorithm aborted by target");
995 retval = ERROR_FLASH_OPERATION_FAILED;
996 break;
997 }
998
999 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1000 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1001 break;
1002 }
1003
1004 /* Count the number of bytes available in the fifo without
1005 * crossing the wrap around. Make sure to not fill it completely,
1006 * because that would make wp == rp and that's the empty condition. */
1007 uint32_t thisrun_bytes;
1008 if (rp > wp)
1009 thisrun_bytes = rp - wp - block_size;
1010 else if (rp > fifo_start_addr)
1011 thisrun_bytes = fifo_end_addr - wp;
1012 else
1013 thisrun_bytes = fifo_end_addr - wp - block_size;
1014
1015 if (thisrun_bytes == 0) {
1016 /* Throttle polling a bit if transfer is (much) faster than flash
1017 * programming. The exact delay shouldn't matter as long as it's
1018 * less than buffer size / flash speed. This is very unlikely to
1019 * run when using high latency connections such as USB. */
1020 alive_sleep(10);
1021
1022 /* to stop an infinite loop on some targets check and increment a timeout
1023 * this issue was observed on a stellaris using the new ICDI interface */
1024 if (timeout++ >= 500) {
1025 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1026 return ERROR_FLASH_OPERATION_FAILED;
1027 }
1028 continue;
1029 }
1030
1031 /* reset our timeout */
1032 timeout = 0;
1033
1034 /* Limit to the amount of data we actually want to write */
1035 if (thisrun_bytes > count * block_size)
1036 thisrun_bytes = count * block_size;
1037
1038 /* Write data to fifo */
1039 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1040 if (retval != ERROR_OK)
1041 break;
1042
1043 /* Update counters and wrap write pointer */
1044 buffer += thisrun_bytes;
1045 count -= thisrun_bytes / block_size;
1046 wp += thisrun_bytes;
1047 if (wp >= fifo_end_addr)
1048 wp = fifo_start_addr;
1049
1050 /* Store updated write pointer to target */
1051 retval = target_write_u32(target, wp_addr, wp);
1052 if (retval != ERROR_OK)
1053 break;
1054
1055 /* Avoid GDB timeouts */
1056 keep_alive();
1057 }
1058
1059 if (retval != ERROR_OK) {
1060 /* abort flash write algorithm on target */
1061 target_write_u32(target, wp_addr, 0);
1062 }
1063
1064 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1065 num_reg_params, reg_params,
1066 exit_point,
1067 10000,
1068 arch_info);
1069
1070 if (retval2 != ERROR_OK) {
1071 LOG_ERROR("error waiting for target flash write algorithm");
1072 retval = retval2;
1073 }
1074
1075 if (retval == ERROR_OK) {
1076 /* check if algorithm set rp = 0 after fifo writer loop finished */
1077 retval = target_read_u32(target, rp_addr, &rp);
1078 if (retval == ERROR_OK && rp == 0) {
1079 LOG_ERROR("flash write algorithm aborted by target");
1080 retval = ERROR_FLASH_OPERATION_FAILED;
1081 }
1082 }
1083
1084 return retval;
1085 }
1086
1087 int target_read_memory(struct target *target,
1088 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1089 {
1090 if (!target_was_examined(target)) {
1091 LOG_ERROR("Target not examined yet");
1092 return ERROR_FAIL;
1093 }
1094 if (!target->type->read_memory) {
1095 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1096 return ERROR_FAIL;
1097 }
1098 return target->type->read_memory(target, address, size, count, buffer);
1099 }
1100
1101 int target_read_phys_memory(struct target *target,
1102 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1103 {
1104 if (!target_was_examined(target)) {
1105 LOG_ERROR("Target not examined yet");
1106 return ERROR_FAIL;
1107 }
1108 if (!target->type->read_phys_memory) {
1109 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1110 return ERROR_FAIL;
1111 }
1112 return target->type->read_phys_memory(target, address, size, count, buffer);
1113 }
1114
1115 int target_write_memory(struct target *target,
1116 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1117 {
1118 if (!target_was_examined(target)) {
1119 LOG_ERROR("Target not examined yet");
1120 return ERROR_FAIL;
1121 }
1122 if (!target->type->write_memory) {
1123 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1124 return ERROR_FAIL;
1125 }
1126 return target->type->write_memory(target, address, size, count, buffer);
1127 }
1128
1129 int target_write_phys_memory(struct target *target,
1130 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1131 {
1132 if (!target_was_examined(target)) {
1133 LOG_ERROR("Target not examined yet");
1134 return ERROR_FAIL;
1135 }
1136 if (!target->type->write_phys_memory) {
1137 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1138 return ERROR_FAIL;
1139 }
1140 return target->type->write_phys_memory(target, address, size, count, buffer);
1141 }
1142
1143 int target_add_breakpoint(struct target *target,
1144 struct breakpoint *breakpoint)
1145 {
1146 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1147 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target));
1148 return ERROR_TARGET_NOT_HALTED;
1149 }
1150 return target->type->add_breakpoint(target, breakpoint);
1151 }
1152
1153 int target_add_context_breakpoint(struct target *target,
1154 struct breakpoint *breakpoint)
1155 {
1156 if (target->state != TARGET_HALTED) {
1157 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target));
1158 return ERROR_TARGET_NOT_HALTED;
1159 }
1160 return target->type->add_context_breakpoint(target, breakpoint);
1161 }
1162
1163 int target_add_hybrid_breakpoint(struct target *target,
1164 struct breakpoint *breakpoint)
1165 {
1166 if (target->state != TARGET_HALTED) {
1167 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target));
1168 return ERROR_TARGET_NOT_HALTED;
1169 }
1170 return target->type->add_hybrid_breakpoint(target, breakpoint);
1171 }
1172
1173 int target_remove_breakpoint(struct target *target,
1174 struct breakpoint *breakpoint)
1175 {
1176 return target->type->remove_breakpoint(target, breakpoint);
1177 }
1178
1179 int target_add_watchpoint(struct target *target,
1180 struct watchpoint *watchpoint)
1181 {
1182 if (target->state != TARGET_HALTED) {
1183 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target));
1184 return ERROR_TARGET_NOT_HALTED;
1185 }
1186 return target->type->add_watchpoint(target, watchpoint);
1187 }
1188 int target_remove_watchpoint(struct target *target,
1189 struct watchpoint *watchpoint)
1190 {
1191 return target->type->remove_watchpoint(target, watchpoint);
1192 }
1193 int target_hit_watchpoint(struct target *target,
1194 struct watchpoint **hit_watchpoint)
1195 {
1196 if (target->state != TARGET_HALTED) {
1197 LOG_WARNING("target %s is not halted (hit watchpoint)", target->cmd_name);
1198 return ERROR_TARGET_NOT_HALTED;
1199 }
1200
1201 if (target->type->hit_watchpoint == NULL) {
1202 /* For backward compatible, if hit_watchpoint is not implemented,
1203 * return ERROR_FAIL such that gdb_server will not take the nonsense
1204 * information. */
1205 return ERROR_FAIL;
1206 }
1207
1208 return target->type->hit_watchpoint(target, hit_watchpoint);
1209 }
1210
1211 const char *target_get_gdb_arch(struct target *target)
1212 {
1213 if (target->type->get_gdb_arch == NULL)
1214 return NULL;
1215 return target->type->get_gdb_arch(target);
1216 }
1217
1218 int target_get_gdb_reg_list(struct target *target,
1219 struct reg **reg_list[], int *reg_list_size,
1220 enum target_register_class reg_class)
1221 {
1222 int result = target->type->get_gdb_reg_list(target, reg_list,
1223 reg_list_size, reg_class);
1224 if (result != ERROR_OK) {
1225 *reg_list = NULL;
1226 *reg_list_size = 0;
1227 }
1228 return result;
1229 }
1230
1231 int target_get_gdb_reg_list_noread(struct target *target,
1232 struct reg **reg_list[], int *reg_list_size,
1233 enum target_register_class reg_class)
1234 {
1235 if (target->type->get_gdb_reg_list_noread &&
1236 target->type->get_gdb_reg_list_noread(target, reg_list,
1237 reg_list_size, reg_class) == ERROR_OK)
1238 return ERROR_OK;
1239 return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1240 }
1241
1242 bool target_supports_gdb_connection(struct target *target)
1243 {
1244 /*
1245 * based on current code, we can simply exclude all the targets that
1246 * don't provide get_gdb_reg_list; this could change with new targets.
1247 */
1248 return !!target->type->get_gdb_reg_list;
1249 }
1250
1251 int target_step(struct target *target,
1252 int current, target_addr_t address, int handle_breakpoints)
1253 {
1254 return target->type->step(target, current, address, handle_breakpoints);
1255 }
1256
1257 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1258 {
1259 if (target->state != TARGET_HALTED) {
1260 LOG_WARNING("target %s is not halted (gdb fileio)", target->cmd_name);
1261 return ERROR_TARGET_NOT_HALTED;
1262 }
1263 return target->type->get_gdb_fileio_info(target, fileio_info);
1264 }
1265
1266 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1267 {
1268 if (target->state != TARGET_HALTED) {
1269 LOG_WARNING("target %s is not halted (gdb fileio end)", target->cmd_name);
1270 return ERROR_TARGET_NOT_HALTED;
1271 }
1272 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1273 }
1274
1275 target_addr_t target_address_max(struct target *target)
1276 {
1277 unsigned bits = target_address_bits(target);
1278 if (sizeof(target_addr_t) * 8 == bits)
1279 return (target_addr_t) -1;
1280 else
1281 return (((target_addr_t) 1) << bits) - 1;
1282 }
1283
1284 unsigned target_address_bits(struct target *target)
1285 {
1286 if (target->type->address_bits)
1287 return target->type->address_bits(target);
1288 return 32;
1289 }
1290
1291 int target_profiling(struct target *target, uint32_t *samples,
1292 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1293 {
1294 if (target->state != TARGET_HALTED) {
1295 LOG_WARNING("target %s is not halted (profiling)", target->cmd_name);
1296 return ERROR_TARGET_NOT_HALTED;
1297 }
1298 return target->type->profiling(target, samples, max_num_samples,
1299 num_samples, seconds);
1300 }
1301
1302 /**
1303 * Reset the @c examined flag for the given target.
1304 * Pure paranoia -- targets are zeroed on allocation.
1305 */
1306 static void target_reset_examined(struct target *target)
1307 {
1308 target->examined = false;
1309 }
1310
1311 static int handle_target(void *priv);
1312
1313 static int target_init_one(struct command_context *cmd_ctx,
1314 struct target *target)
1315 {
1316 target_reset_examined(target);
1317
1318 struct target_type *type = target->type;
1319 if (type->examine == NULL)
1320 type->examine = default_examine;
1321
1322 if (type->check_reset == NULL)
1323 type->check_reset = default_check_reset;
1324
1325 assert(type->init_target != NULL);
1326
1327 int retval = type->init_target(cmd_ctx, target);
1328 if (ERROR_OK != retval) {
1329 LOG_ERROR("target '%s' init failed", target_name(target));
1330 return retval;
1331 }
1332
1333 /* Sanity-check MMU support ... stub in what we must, to help
1334 * implement it in stages, but warn if we need to do so.
1335 */
1336 if (type->mmu) {
1337 if (type->virt2phys == NULL) {
1338 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1339 type->virt2phys = identity_virt2phys;
1340 }
1341 } else {
1342 /* Make sure no-MMU targets all behave the same: make no
1343 * distinction between physical and virtual addresses, and
1344 * ensure that virt2phys() is always an identity mapping.
1345 */
1346 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1347 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1348
1349 type->mmu = no_mmu;
1350 type->write_phys_memory = type->write_memory;
1351 type->read_phys_memory = type->read_memory;
1352 type->virt2phys = identity_virt2phys;
1353 }
1354
1355 if (target->type->read_buffer == NULL)
1356 target->type->read_buffer = target_read_buffer_default;
1357
1358 if (target->type->write_buffer == NULL)
1359 target->type->write_buffer = target_write_buffer_default;
1360
1361 if (target->type->get_gdb_fileio_info == NULL)
1362 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1363
1364 if (target->type->gdb_fileio_end == NULL)
1365 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1366
1367 if (target->type->profiling == NULL)
1368 target->type->profiling = target_profiling_default;
1369
1370 return ERROR_OK;
1371 }
1372
1373 static int target_init(struct command_context *cmd_ctx)
1374 {
1375 struct target *target;
1376 int retval;
1377
1378 for (target = all_targets; target; target = target->next) {
1379 retval = target_init_one(cmd_ctx, target);
1380 if (ERROR_OK != retval)
1381 return retval;
1382 }
1383
1384 if (!all_targets)
1385 return ERROR_OK;
1386
1387 retval = target_register_user_commands(cmd_ctx);
1388 if (ERROR_OK != retval)
1389 return retval;
1390
1391 retval = target_register_timer_callback(&handle_target,
1392 polling_interval, TARGET_TIMER_TYPE_PERIODIC, cmd_ctx->interp);
1393 if (ERROR_OK != retval)
1394 return retval;
1395
1396 return ERROR_OK;
1397 }
1398
1399 COMMAND_HANDLER(handle_target_init_command)
1400 {
1401 int retval;
1402
1403 if (CMD_ARGC != 0)
1404 return ERROR_COMMAND_SYNTAX_ERROR;
1405
1406 static bool target_initialized;
1407 if (target_initialized) {
1408 LOG_INFO("'target init' has already been called");
1409 return ERROR_OK;
1410 }
1411 target_initialized = true;
1412
1413 retval = command_run_line(CMD_CTX, "init_targets");
1414 if (ERROR_OK != retval)
1415 return retval;
1416
1417 retval = command_run_line(CMD_CTX, "init_target_events");
1418 if (ERROR_OK != retval)
1419 return retval;
1420
1421 retval = command_run_line(CMD_CTX, "init_board");
1422 if (ERROR_OK != retval)
1423 return retval;
1424
1425 LOG_DEBUG("Initializing targets...");
1426 return target_init(CMD_CTX);
1427 }
1428
1429 int target_register_event_callback(int (*callback)(struct target *target,
1430 enum target_event event, void *priv), void *priv)
1431 {
1432 struct target_event_callback **callbacks_p = &target_event_callbacks;
1433
1434 if (callback == NULL)
1435 return ERROR_COMMAND_SYNTAX_ERROR;
1436
1437 if (*callbacks_p) {
1438 while ((*callbacks_p)->next)
1439 callbacks_p = &((*callbacks_p)->next);
1440 callbacks_p = &((*callbacks_p)->next);
1441 }
1442
1443 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1444 (*callbacks_p)->callback = callback;
1445 (*callbacks_p)->priv = priv;
1446 (*callbacks_p)->next = NULL;
1447
1448 return ERROR_OK;
1449 }
1450
1451 int target_register_reset_callback(int (*callback)(struct target *target,
1452 enum target_reset_mode reset_mode, void *priv), void *priv)
1453 {
1454 struct target_reset_callback *entry;
1455
1456 if (callback == NULL)
1457 return ERROR_COMMAND_SYNTAX_ERROR;
1458
1459 entry = malloc(sizeof(struct target_reset_callback));
1460 if (entry == NULL) {
1461 LOG_ERROR("error allocating buffer for reset callback entry");
1462 return ERROR_COMMAND_SYNTAX_ERROR;
1463 }
1464
1465 entry->callback = callback;
1466 entry->priv = priv;
1467 list_add(&entry->list, &target_reset_callback_list);
1468
1469
1470 return ERROR_OK;
1471 }
1472
1473 int target_register_trace_callback(int (*callback)(struct target *target,
1474 size_t len, uint8_t *data, void *priv), void *priv)
1475 {
1476 struct target_trace_callback *entry;
1477
1478 if (callback == NULL)
1479 return ERROR_COMMAND_SYNTAX_ERROR;
1480
1481 entry = malloc(sizeof(struct target_trace_callback));
1482 if (entry == NULL) {
1483 LOG_ERROR("error allocating buffer for trace callback entry");
1484 return ERROR_COMMAND_SYNTAX_ERROR;
1485 }
1486
1487 entry->callback = callback;
1488 entry->priv = priv;
1489 list_add(&entry->list, &target_trace_callback_list);
1490
1491
1492 return ERROR_OK;
1493 }
1494
1495 int target_register_timer_callback(int (*callback)(void *priv),
1496 unsigned int time_ms, enum target_timer_type type, void *priv)
1497 {
1498 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1499
1500 if (callback == NULL)
1501 return ERROR_COMMAND_SYNTAX_ERROR;
1502
1503 if (*callbacks_p) {
1504 while ((*callbacks_p)->next)
1505 callbacks_p = &((*callbacks_p)->next);
1506 callbacks_p = &((*callbacks_p)->next);
1507 }
1508
1509 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1510 (*callbacks_p)->callback = callback;
1511 (*callbacks_p)->type = type;
1512 (*callbacks_p)->time_ms = time_ms;
1513 (*callbacks_p)->removed = false;
1514
1515 gettimeofday(&(*callbacks_p)->when, NULL);
1516 timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000);
1517
1518 (*callbacks_p)->priv = priv;
1519 (*callbacks_p)->next = NULL;
1520
1521 return ERROR_OK;
1522 }
1523
1524 int target_unregister_event_callback(int (*callback)(struct target *target,
1525 enum target_event event, void *priv), void *priv)
1526 {
1527 struct target_event_callback **p = &target_event_callbacks;
1528 struct target_event_callback *c = target_event_callbacks;
1529
1530 if (callback == NULL)
1531 return ERROR_COMMAND_SYNTAX_ERROR;
1532
1533 while (c) {
1534 struct target_event_callback *next = c->next;
1535 if ((c->callback == callback) && (c->priv == priv)) {
1536 *p = next;
1537 free(c);
1538 return ERROR_OK;
1539 } else
1540 p = &(c->next);
1541 c = next;
1542 }
1543
1544 return ERROR_OK;
1545 }
1546
1547 int target_unregister_reset_callback(int (*callback)(struct target *target,
1548 enum target_reset_mode reset_mode, void *priv), void *priv)
1549 {
1550 struct target_reset_callback *entry;
1551
1552 if (callback == NULL)
1553 return ERROR_COMMAND_SYNTAX_ERROR;
1554
1555 list_for_each_entry(entry, &target_reset_callback_list, list) {
1556 if (entry->callback == callback && entry->priv == priv) {
1557 list_del(&entry->list);
1558 free(entry);
1559 break;
1560 }
1561 }
1562
1563 return ERROR_OK;
1564 }
1565
1566 int target_unregister_trace_callback(int (*callback)(struct target *target,
1567 size_t len, uint8_t *data, void *priv), void *priv)
1568 {
1569 struct target_trace_callback *entry;
1570
1571 if (callback == NULL)
1572 return ERROR_COMMAND_SYNTAX_ERROR;
1573
1574 list_for_each_entry(entry, &target_trace_callback_list, list) {
1575 if (entry->callback == callback && entry->priv == priv) {
1576 list_del(&entry->list);
1577 free(entry);
1578 break;
1579 }
1580 }
1581
1582 return ERROR_OK;
1583 }
1584
1585 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1586 {
1587 if (callback == NULL)
1588 return ERROR_COMMAND_SYNTAX_ERROR;
1589
1590 for (struct target_timer_callback *c = target_timer_callbacks;
1591 c; c = c->next) {
1592 if ((c->callback == callback) && (c->priv == priv)) {
1593 c->removed = true;
1594 return ERROR_OK;
1595 }
1596 }
1597
1598 return ERROR_FAIL;
1599 }
1600
1601 int target_call_event_callbacks(struct target *target, enum target_event event)
1602 {
1603 struct target_event_callback *callback = target_event_callbacks;
1604 struct target_event_callback *next_callback;
1605
1606 if (event == TARGET_EVENT_HALTED) {
1607 /* execute early halted first */
1608 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1609 }
1610
1611 LOG_DEBUG("target event %i (%s) for core %s", event,
1612 Jim_Nvp_value2name_simple(nvp_target_event, event)->name,
1613 target_name(target));
1614
1615 target_handle_event(target, event);
1616
1617 while (callback) {
1618 next_callback = callback->next;
1619 callback->callback(target, event, callback->priv);
1620 callback = next_callback;
1621 }
1622
1623 return ERROR_OK;
1624 }
1625
1626 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1627 {
1628 struct target_reset_callback *callback;
1629
1630 LOG_DEBUG("target reset %i (%s)", reset_mode,
1631 Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1632
1633 list_for_each_entry(callback, &target_reset_callback_list, list)
1634 callback->callback(target, reset_mode, callback->priv);
1635
1636 return ERROR_OK;
1637 }
1638
1639 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1640 {
1641 struct target_trace_callback *callback;
1642
1643 list_for_each_entry(callback, &target_trace_callback_list, list)
1644 callback->callback(target, len, data, callback->priv);
1645
1646 return ERROR_OK;
1647 }
1648
1649 static int target_timer_callback_periodic_restart(
1650 struct target_timer_callback *cb, struct timeval *now)
1651 {
1652 cb->when = *now;
1653 timeval_add_time(&cb->when, 0, cb->time_ms * 1000L);
1654 return ERROR_OK;
1655 }
1656
1657 static int target_call_timer_callback(struct target_timer_callback *cb,
1658 struct timeval *now)
1659 {
1660 cb->callback(cb->priv);
1661
1662 if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1663 return target_timer_callback_periodic_restart(cb, now);
1664
1665 return target_unregister_timer_callback(cb->callback, cb->priv);
1666 }
1667
1668 static int target_call_timer_callbacks_check_time(int checktime)
1669 {
1670 static bool callback_processing;
1671
1672 /* Do not allow nesting */
1673 if (callback_processing)
1674 return ERROR_OK;
1675
1676 callback_processing = true;
1677
1678 keep_alive();
1679
1680 struct timeval now;
1681 gettimeofday(&now, NULL);
1682
1683 /* Store an address of the place containing a pointer to the
1684 * next item; initially, that's a standalone "root of the
1685 * list" variable. */
1686 struct target_timer_callback **callback = &target_timer_callbacks;
1687 while (callback && *callback) {
1688 if ((*callback)->removed) {
1689 struct target_timer_callback *p = *callback;
1690 *callback = (*callback)->next;
1691 free(p);
1692 continue;
1693 }
1694
1695 bool call_it = (*callback)->callback &&
1696 ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1697 timeval_compare(&now, &(*callback)->when) >= 0);
1698
1699 if (call_it)
1700 target_call_timer_callback(*callback, &now);
1701
1702 callback = &(*callback)->next;
1703 }
1704
1705 callback_processing = false;
1706 return ERROR_OK;
1707 }
1708
1709 int target_call_timer_callbacks(void)
1710 {
1711 return target_call_timer_callbacks_check_time(1);
1712 }
1713
1714 /* invoke periodic callbacks immediately */
1715 int target_call_timer_callbacks_now(void)
1716 {
1717 return target_call_timer_callbacks_check_time(0);
1718 }
1719
1720 /* Prints the working area layout for debug purposes */
1721 static void print_wa_layout(struct target *target)
1722 {
1723 struct working_area *c = target->working_areas;
1724
1725 while (c) {
1726 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1727 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1728 c->address, c->address + c->size - 1, c->size);
1729 c = c->next;
1730 }
1731 }
1732
1733 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1734 static void target_split_working_area(struct working_area *area, uint32_t size)
1735 {
1736 assert(area->free); /* Shouldn't split an allocated area */
1737 assert(size <= area->size); /* Caller should guarantee this */
1738
1739 /* Split only if not already the right size */
1740 if (size < area->size) {
1741 struct working_area *new_wa = malloc(sizeof(*new_wa));
1742
1743 if (new_wa == NULL)
1744 return;
1745
1746 new_wa->next = area->next;
1747 new_wa->size = area->size - size;
1748 new_wa->address = area->address + size;
1749 new_wa->backup = NULL;
1750 new_wa->user = NULL;
1751 new_wa->free = true;
1752
1753 area->next = new_wa;
1754 area->size = size;
1755
1756 /* If backup memory was allocated to this area, it has the wrong size
1757 * now so free it and it will be reallocated if/when needed */
1758 if (area->backup) {
1759 free(area->backup);
1760 area->backup = NULL;
1761 }
1762 }
1763 }
1764
1765 /* Merge all adjacent free areas into one */
1766 static void target_merge_working_areas(struct target *target)
1767 {
1768 struct working_area *c = target->working_areas;
1769
1770 while (c && c->next) {
1771 assert(c->next->address == c->address + c->size); /* This is an invariant */
1772
1773 /* Find two adjacent free areas */
1774 if (c->free && c->next->free) {
1775 /* Merge the last into the first */
1776 c->size += c->next->size;
1777
1778 /* Remove the last */
1779 struct working_area *to_be_freed = c->next;
1780 c->next = c->next->next;
1781 if (to_be_freed->backup)
1782 free(to_be_freed->backup);
1783 free(to_be_freed);
1784
1785 /* If backup memory was allocated to the remaining area, it's has
1786 * the wrong size now */
1787 if (c->backup) {
1788 free(c->backup);
1789 c->backup = NULL;
1790 }
1791 } else {
1792 c = c->next;
1793 }
1794 }
1795 }
1796
1797 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1798 {
1799 /* Reevaluate working area address based on MMU state*/
1800 if (target->working_areas == NULL) {
1801 int retval;
1802 int enabled;
1803
1804 retval = target->type->mmu(target, &enabled);
1805 if (retval != ERROR_OK)
1806 return retval;
1807
1808 if (!enabled) {
1809 if (target->working_area_phys_spec) {
1810 LOG_DEBUG("MMU disabled, using physical "
1811 "address for working memory " TARGET_ADDR_FMT,
1812 target->working_area_phys);
1813 target->working_area = target->working_area_phys;
1814 } else {
1815 LOG_ERROR("No working memory available. "
1816 "Specify -work-area-phys to target.");
1817 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1818 }
1819 } else {
1820 if (target->working_area_virt_spec) {
1821 LOG_DEBUG("MMU enabled, using virtual "
1822 "address for working memory " TARGET_ADDR_FMT,
1823 target->working_area_virt);
1824 target->working_area = target->working_area_virt;
1825 } else {
1826 LOG_ERROR("No working memory available. "
1827 "Specify -work-area-virt to target.");
1828 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1829 }
1830 }
1831
1832 /* Set up initial working area on first call */
1833 struct working_area *new_wa = malloc(sizeof(*new_wa));
1834 if (new_wa) {
1835 new_wa->next = NULL;
1836 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1837 new_wa->address = target->working_area;
1838 new_wa->backup = NULL;
1839 new_wa->user = NULL;
1840 new_wa->free = true;
1841 }
1842
1843 target->working_areas = new_wa;
1844 }
1845
1846 /* only allocate multiples of 4 byte */
1847 if (size % 4)
1848 size = (size + 3) & (~3UL);
1849
1850 struct working_area *c = target->working_areas;
1851
1852 /* Find the first large enough working area */
1853 while (c) {
1854 if (c->free && c->size >= size)
1855 break;
1856 c = c->next;
1857 }
1858
1859 if (c == NULL)
1860 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1861
1862 /* Split the working area into the requested size */
1863 target_split_working_area(c, size);
1864
1865 LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
1866 size, c->address);
1867
1868 if (target->backup_working_area) {
1869 if (c->backup == NULL) {
1870 c->backup = malloc(c->size);
1871 if (c->backup == NULL)
1872 return ERROR_FAIL;
1873 }
1874
1875 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1876 if (retval != ERROR_OK)
1877 return retval;
1878 }
1879
1880 /* mark as used, and return the new (reused) area */
1881 c->free = false;
1882 *area = c;
1883
1884 /* user pointer */
1885 c->user = area;
1886
1887 print_wa_layout(target);
1888
1889 return ERROR_OK;
1890 }
1891
1892 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1893 {
1894 int retval;
1895
1896 retval = target_alloc_working_area_try(target, size, area);
1897 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1898 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1899 return retval;
1900
1901 }
1902
1903 static int target_restore_working_area(struct target *target, struct working_area *area)
1904 {
1905 int retval = ERROR_OK;
1906
1907 if (target->backup_working_area && area->backup != NULL) {
1908 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1909 if (retval != ERROR_OK)
1910 LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1911 area->size, area->address);
1912 }
1913
1914 return retval;
1915 }
1916
1917 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1918 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1919 {
1920 int retval = ERROR_OK;
1921
1922 if (area->free)
1923 return retval;
1924
1925 if (restore) {
1926 retval = target_restore_working_area(target, area);
1927 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1928 if (retval != ERROR_OK)
1929 return retval;
1930 }
1931
1932 area->free = true;
1933
1934 LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1935 area->size, area->address);
1936
1937 /* mark user pointer invalid */
1938 /* TODO: Is this really safe? It points to some previous caller's memory.
1939 * How could we know that the area pointer is still in that place and not
1940 * some other vital data? What's the purpose of this, anyway? */
1941 *area->user = NULL;
1942 area->user = NULL;
1943
1944 target_merge_working_areas(target);
1945
1946 print_wa_layout(target);
1947
1948 return retval;
1949 }
1950
1951 int target_free_working_area(struct target *target, struct working_area *area)
1952 {
1953 return target_free_working_area_restore(target, area, 1);
1954 }
1955
1956 /* free resources and restore memory, if restoring memory fails,
1957 * free up resources anyway
1958 */
1959 static void target_free_all_working_areas_restore(struct target *target, int restore)
1960 {
1961 struct working_area *c = target->working_areas;
1962
1963 LOG_DEBUG("freeing all working areas");
1964
1965 /* Loop through all areas, restoring the allocated ones and marking them as free */
1966 while (c) {
1967 if (!c->free) {
1968 if (restore)
1969 target_restore_working_area(target, c);
1970 c->free = true;
1971 *c->user = NULL; /* Same as above */
1972 c->user = NULL;
1973 }
1974 c = c->next;
1975 }
1976
1977 /* Run a merge pass to combine all areas into one */
1978 target_merge_working_areas(target);
1979
1980 print_wa_layout(target);
1981 }
1982
1983 void target_free_all_working_areas(struct target *target)
1984 {
1985 target_free_all_working_areas_restore(target, 1);
1986
1987 /* Now we have none or only one working area marked as free */
1988 if (target->working_areas) {
1989 /* Free the last one to allow on-the-fly moving and resizing */
1990 free(target->working_areas->backup);
1991 free(target->working_areas);
1992 target->working_areas = NULL;
1993 }
1994 }
1995
1996 /* Find the largest number of bytes that can be allocated */
1997 uint32_t target_get_working_area_avail(struct target *target)
1998 {
1999 struct working_area *c = target->working_areas;
2000 uint32_t max_size = 0;
2001
2002 if (c == NULL)
2003 return target->working_area_size;
2004
2005 while (c) {
2006 if (c->free && max_size < c->size)
2007 max_size = c->size;
2008
2009 c = c->next;
2010 }
2011
2012 return max_size;
2013 }
2014
2015 static void target_destroy(struct target *target)
2016 {
2017 if (target->type->deinit_target)
2018 target->type->deinit_target(target);
2019
2020 if (target->semihosting)
2021 free(target->semihosting);
2022
2023 jtag_unregister_event_callback(jtag_enable_callback, target);
2024
2025 struct target_event_action *teap = target->event_action;
2026 while (teap) {
2027 struct target_event_action *next = teap->next;
2028 Jim_DecrRefCount(teap->interp, teap->body);
2029 free(teap);
2030 teap = next;
2031 }
2032
2033 target_free_all_working_areas(target);
2034
2035 /* release the targets SMP list */
2036 if (target->smp) {
2037 struct target_list *head = target->head;
2038 while (head != NULL) {
2039 struct target_list *pos = head->next;
2040 head->target->smp = 0;
2041 free(head);
2042 head = pos;
2043 }
2044 target->smp = 0;
2045 }
2046
2047 free(target->gdb_port_override);
2048 free(target->type);
2049 free(target->trace_info);
2050 free(target->fileio_info);
2051 free(target->cmd_name);
2052 free(target);
2053 }
2054
2055 void target_quit(void)
2056 {
2057 struct target_event_callback *pe = target_event_callbacks;
2058 while (pe) {
2059 struct target_event_callback *t = pe->next;
2060 free(pe);
2061 pe = t;
2062 }
2063 target_event_callbacks = NULL;
2064
2065 struct target_timer_callback *pt = target_timer_callbacks;
2066 while (pt) {
2067 struct target_timer_callback *t = pt->next;
2068 free(pt);
2069 pt = t;
2070 }
2071 target_timer_callbacks = NULL;
2072
2073 for (struct target *target = all_targets; target;) {
2074 struct target *tmp;
2075
2076 tmp = target->next;
2077 target_destroy(target);
2078 target = tmp;
2079 }
2080
2081 all_targets = NULL;
2082 }
2083
2084 int target_arch_state(struct target *target)
2085 {
2086 int retval;
2087 if (target == NULL) {
2088 LOG_WARNING("No target has been configured");
2089 return ERROR_OK;
2090 }
2091
2092 if (target->state != TARGET_HALTED)
2093 return ERROR_OK;
2094
2095 retval = target->type->arch_state(target);
2096 return retval;
2097 }
2098
2099 static int target_get_gdb_fileio_info_default(struct target *target,
2100 struct gdb_fileio_info *fileio_info)
2101 {
2102 /* If target does not support semi-hosting function, target
2103 has no need to provide .get_gdb_fileio_info callback.
2104 It just return ERROR_FAIL and gdb_server will return "Txx"
2105 as target halted every time. */
2106 return ERROR_FAIL;
2107 }
2108
2109 static int target_gdb_fileio_end_default(struct target *target,
2110 int retcode, int fileio_errno, bool ctrl_c)
2111 {
2112 return ERROR_OK;
2113 }
2114
2115 static int target_profiling_default(struct target *target, uint32_t *samples,
2116 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2117 {
2118 struct timeval timeout, now;
2119
2120 gettimeofday(&timeout, NULL);
2121 timeval_add_time(&timeout, seconds, 0);
2122
2123 LOG_INFO("Starting profiling. Halting and resuming the"
2124 " target as often as we can...");
2125
2126 uint32_t sample_count = 0;
2127 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2128 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2129
2130 int retval = ERROR_OK;
2131 for (;;) {
2132 target_poll(target);
2133 if (target->state == TARGET_HALTED) {
2134 uint32_t t = buf_get_u32(reg->value, 0, 32);
2135 samples[sample_count++] = t;
2136 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2137 retval = target_resume(target, 1, 0, 0, 0);
2138 target_poll(target);
2139 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2140 } else if (target->state == TARGET_RUNNING) {
2141 /* We want to quickly sample the PC. */
2142 retval = target_halt(target);
2143 } else {
2144 LOG_INFO("Target not halted or running");
2145 retval = ERROR_OK;
2146 break;
2147 }
2148
2149 if (retval != ERROR_OK)
2150 break;
2151
2152 gettimeofday(&now, NULL);
2153 if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2154 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2155 break;
2156 }
2157 }
2158
2159 *num_samples = sample_count;
2160 return retval;
2161 }
2162
2163 /* Single aligned words are guaranteed to use 16 or 32 bit access
2164 * mode respectively, otherwise data is handled as quickly as
2165 * possible
2166 */
2167 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2168 {
2169 LOG_DEBUG("writing buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2170 size, address);
2171
2172 if (!target_was_examined(target)) {
2173 LOG_ERROR("Target not examined yet");
2174 return ERROR_FAIL;
2175 }
2176
2177 if (size == 0)
2178 return ERROR_OK;
2179
2180 if ((address + size - 1) < address) {
2181 /* GDB can request this when e.g. PC is 0xfffffffc */
2182 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2183 address,
2184 size);
2185 return ERROR_FAIL;
2186 }
2187
2188 return target->type->write_buffer(target, address, size, buffer);
2189 }
2190
2191 static int target_write_buffer_default(struct target *target,
2192 target_addr_t address, uint32_t count, const uint8_t *buffer)
2193 {
2194 uint32_t size;
2195
2196 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2197 * will have something to do with the size we leave to it. */
2198 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2199 if (address & size) {
2200 int retval = target_write_memory(target, address, size, 1, buffer);
2201 if (retval != ERROR_OK)
2202 return retval;
2203 address += size;
2204 count -= size;
2205 buffer += size;
2206 }
2207 }
2208
2209 /* Write the data with as large access size as possible. */
2210 for (; size > 0; size /= 2) {
2211 uint32_t aligned = count - count % size;
2212 if (aligned > 0) {
2213 int retval = target_write_memory(target, address, size, aligned / size, buffer);
2214 if (retval != ERROR_OK)
2215 return retval;
2216 address += aligned;
2217 count -= aligned;
2218 buffer += aligned;
2219 }
2220 }
2221
2222 return ERROR_OK;
2223 }
2224
2225 /* Single aligned words are guaranteed to use 16 or 32 bit access
2226 * mode respectively, otherwise data is handled as quickly as
2227 * possible
2228 */
2229 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2230 {
2231 LOG_DEBUG("reading buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2232 size, address);
2233
2234 if (!target_was_examined(target)) {
2235 LOG_ERROR("Target not examined yet");
2236 return ERROR_FAIL;
2237 }
2238
2239 if (size == 0)
2240 return ERROR_OK;
2241
2242 if ((address + size - 1) < address) {
2243 /* GDB can request this when e.g. PC is 0xfffffffc */
2244 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2245 address,
2246 size);
2247 return ERROR_FAIL;
2248 }
2249
2250 return target->type->read_buffer(target, address, size, buffer);
2251 }
2252
2253 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2254 {
2255 uint32_t size;
2256
2257 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2258 * will have something to do with the size we leave to it. */
2259 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2260 if (address & size) {
2261 int retval = target_read_memory(target, address, size, 1, buffer);
2262 if (retval != ERROR_OK)
2263 return retval;
2264 address += size;
2265 count -= size;
2266 buffer += size;
2267 }
2268 }
2269
2270 /* Read the data with as large access size as possible. */
2271 for (; size > 0; size /= 2) {
2272 uint32_t aligned = count - count % size;
2273 if (aligned > 0) {
2274 int retval = target_read_memory(target, address, size, aligned / size, buffer);
2275 if (retval != ERROR_OK)
2276 return retval;
2277 address += aligned;
2278 count -= aligned;
2279 buffer += aligned;
2280 }
2281 }
2282
2283 return ERROR_OK;
2284 }
2285
2286 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
2287 {
2288 uint8_t *buffer;
2289 int retval;
2290 uint32_t i;
2291 uint32_t checksum = 0;
2292 if (!target_was_examined(target)) {
2293 LOG_ERROR("Target not examined yet");
2294 return ERROR_FAIL;
2295 }
2296
2297 retval = target->type->checksum_memory(target, address, size, &checksum);
2298 if (retval != ERROR_OK) {
2299 buffer = malloc(size);
2300 if (buffer == NULL) {
2301 LOG_ERROR("error allocating buffer for section (%" PRId32 " bytes)", size);
2302 return ERROR_COMMAND_SYNTAX_ERROR;
2303 }
2304 retval = target_read_buffer(target, address, size, buffer);
2305 if (retval != ERROR_OK) {
2306 free(buffer);
2307 return retval;
2308 }
2309
2310 /* convert to target endianness */
2311 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2312 uint32_t target_data;
2313 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2314 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2315 }
2316
2317 retval = image_calculate_checksum(buffer, size, &checksum);
2318 free(buffer);
2319 }
2320
2321 *crc = checksum;
2322
2323 return retval;
2324 }
2325
2326 int target_blank_check_memory(struct target *target,
2327 struct target_memory_check_block *blocks, int num_blocks,
2328 uint8_t erased_value)
2329 {
2330 if (!target_was_examined(target)) {
2331 LOG_ERROR("Target not examined yet");
2332 return ERROR_FAIL;
2333 }
2334
2335 if (target->type->blank_check_memory == NULL)
2336 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2337
2338 return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
2339 }
2340
2341 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2342 {
2343 uint8_t value_buf[8];
2344 if (!target_was_examined(target)) {
2345 LOG_ERROR("Target not examined yet");
2346 return ERROR_FAIL;
2347 }
2348
2349 int retval = target_read_memory(target, address, 8, 1, value_buf);
2350
2351 if (retval == ERROR_OK) {
2352 *value = target_buffer_get_u64(target, value_buf);
2353 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2354 address,
2355 *value);
2356 } else {
2357 *value = 0x0;
2358 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2359 address);
2360 }
2361
2362 return retval;
2363 }
2364
2365 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2366 {
2367 uint8_t value_buf[4];
2368 if (!target_was_examined(target)) {
2369 LOG_ERROR("Target not examined yet");
2370 return ERROR_FAIL;
2371 }
2372
2373 int retval = target_read_memory(target, address, 4, 1, value_buf);
2374
2375 if (retval == ERROR_OK) {
2376 *value = target_buffer_get_u32(target, value_buf);
2377 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2378 address,
2379 *value);
2380 } else {
2381 *value = 0x0;
2382 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2383 address);
2384 }
2385
2386 return retval;
2387 }
2388
2389 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2390 {
2391 uint8_t value_buf[2];
2392 if (!target_was_examined(target)) {
2393 LOG_ERROR("Target not examined yet");
2394 return ERROR_FAIL;
2395 }
2396
2397 int retval = target_read_memory(target, address, 2, 1, value_buf);
2398
2399 if (retval == ERROR_OK) {
2400 *value = target_buffer_get_u16(target, value_buf);
2401 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2402 address,
2403 *value);
2404 } else {
2405 *value = 0x0;
2406 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2407 address);
2408 }
2409
2410 return retval;
2411 }
2412
2413 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2414 {
2415 if (!target_was_examined(target)) {
2416 LOG_ERROR("Target not examined yet");
2417 return ERROR_FAIL;
2418 }
2419
2420 int retval = target_read_memory(target, address, 1, 1, value);
2421
2422 if (retval == ERROR_OK) {
2423 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2424 address,
2425 *value);
2426 } else {
2427 *value = 0x0;
2428 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2429 address);
2430 }
2431
2432 return retval;
2433 }
2434
2435 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2436 {
2437 int retval;
2438 uint8_t value_buf[8];
2439 if (!target_was_examined(target)) {
2440 LOG_ERROR("Target not examined yet");
2441 return ERROR_FAIL;
2442 }
2443
2444 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2445 address,
2446 value);
2447
2448 target_buffer_set_u64(target, value_buf, value);
2449 retval = target_write_memory(target, address, 8, 1, value_buf);
2450 if (retval != ERROR_OK)
2451 LOG_DEBUG("failed: %i", retval);
2452
2453 return retval;
2454 }
2455
2456 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2457 {
2458 int retval;
2459 uint8_t value_buf[4];
2460 if (!target_was_examined(target)) {
2461 LOG_ERROR("Target not examined yet");
2462 return ERROR_FAIL;
2463 }
2464
2465 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2466 address,
2467 value);
2468
2469 target_buffer_set_u32(target, value_buf, value);
2470 retval = target_write_memory(target, address, 4, 1, value_buf);
2471 if (retval != ERROR_OK)
2472 LOG_DEBUG("failed: %i", retval);
2473
2474 return retval;
2475 }
2476
2477 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2478 {
2479 int retval;
2480 uint8_t value_buf[2];
2481 if (!target_was_examined(target)) {
2482 LOG_ERROR("Target not examined yet");
2483 return ERROR_FAIL;
2484 }
2485
2486 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2487 address,
2488 value);
2489
2490 target_buffer_set_u16(target, value_buf, value);
2491 retval = target_write_memory(target, address, 2, 1, value_buf);
2492 if (retval != ERROR_OK)
2493 LOG_DEBUG("failed: %i", retval);
2494
2495 return retval;
2496 }
2497
2498 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2499 {
2500 int retval;
2501 if (!target_was_examined(target)) {
2502 LOG_ERROR("Target not examined yet");
2503 return ERROR_FAIL;
2504 }
2505
2506 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2507 address, value);
2508
2509 retval = target_write_memory(target, address, 1, 1, &value);
2510 if (retval != ERROR_OK)
2511 LOG_DEBUG("failed: %i", retval);
2512
2513 return retval;
2514 }
2515
2516 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2517 {
2518 int retval;
2519 uint8_t value_buf[8];
2520 if (!target_was_examined(target)) {
2521 LOG_ERROR("Target not examined yet");
2522 return ERROR_FAIL;
2523 }
2524
2525 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2526 address,
2527 value);
2528
2529 target_buffer_set_u64(target, value_buf, value);
2530 retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2531 if (retval != ERROR_OK)
2532 LOG_DEBUG("failed: %i", retval);
2533
2534 return retval;
2535 }
2536
2537 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2538 {
2539 int retval;
2540 uint8_t value_buf[4];
2541 if (!target_was_examined(target)) {
2542 LOG_ERROR("Target not examined yet");
2543 return ERROR_FAIL;
2544 }
2545
2546 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2547 address,
2548 value);
2549
2550 target_buffer_set_u32(target, value_buf, value);
2551 retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2552 if (retval != ERROR_OK)
2553 LOG_DEBUG("failed: %i", retval);
2554
2555 return retval;
2556 }
2557
2558 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2559 {
2560 int retval;
2561 uint8_t value_buf[2];
2562 if (!target_was_examined(target)) {
2563 LOG_ERROR("Target not examined yet");
2564 return ERROR_FAIL;
2565 }
2566
2567 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2568 address,
2569 value);
2570
2571 target_buffer_set_u16(target, value_buf, value);
2572 retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2573 if (retval != ERROR_OK)
2574 LOG_DEBUG("failed: %i", retval);
2575
2576 return retval;
2577 }
2578
2579 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2580 {
2581 int retval;
2582 if (!target_was_examined(target)) {
2583 LOG_ERROR("Target not examined yet");
2584 return ERROR_FAIL;
2585 }
2586
2587 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2588 address, value);
2589
2590 retval = target_write_phys_memory(target, address, 1, 1, &value);
2591 if (retval != ERROR_OK)
2592 LOG_DEBUG("failed: %i", retval);
2593
2594 return retval;
2595 }
2596
2597 static int find_target(struct command_invocation *cmd, const char *name)
2598 {
2599 struct target *target = get_target(name);
2600 if (target == NULL) {
2601 command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2602 return ERROR_FAIL;
2603 }
2604 if (!target->tap->enabled) {
2605 command_print(cmd, "Target: TAP %s is disabled, "
2606 "can't be the current target\n",
2607 target->tap->dotted_name);
2608 return ERROR_FAIL;
2609 }
2610
2611 cmd->ctx->current_target = target;
2612 if (cmd->ctx->current_target_override)
2613 cmd->ctx->current_target_override = target;
2614
2615 return ERROR_OK;
2616 }
2617
2618
2619 COMMAND_HANDLER(handle_targets_command)
2620 {
2621 int retval = ERROR_OK;
2622 if (CMD_ARGC == 1) {
2623 retval = find_target(CMD, CMD_ARGV[0]);
2624 if (retval == ERROR_OK) {
2625 /* we're done! */
2626 return retval;
2627 }
2628 }
2629
2630 struct target *target = all_targets;
2631 command_print(CMD, " TargetName Type Endian TapName State ");
2632 command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2633 while (target) {
2634 const char *state;
2635 char marker = ' ';
2636
2637 if (target->tap->enabled)
2638 state = target_state_name(target);
2639 else
2640 state = "tap-disabled";
2641
2642 if (CMD_CTX->current_target == target)
2643 marker = '*';
2644
2645 /* keep columns lined up to match the headers above */
2646 command_print(CMD,
2647 "%2d%c %-18s %-10s %-6s %-18s %s",
2648 target->target_number,
2649 marker,
2650 target_name(target),
2651 target_type_name(target),
2652 Jim_Nvp_value2name_simple(nvp_target_endian,
2653 target->endianness)->name,
2654 target->tap->dotted_name,
2655 state);
2656 target = target->next;
2657 }
2658
2659 return retval;
2660 }
2661
2662 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2663
2664 static int powerDropout;
2665 static int srstAsserted;
2666
2667 static int runPowerRestore;
2668 static int runPowerDropout;
2669 static int runSrstAsserted;
2670 static int runSrstDeasserted;
2671
2672 static int sense_handler(void)
2673 {
2674 static int prevSrstAsserted;
2675 static int prevPowerdropout;
2676
2677 int retval = jtag_power_dropout(&powerDropout);
2678 if (retval != ERROR_OK)
2679 return retval;
2680
2681 int powerRestored;
2682 powerRestored = prevPowerdropout && !powerDropout;
2683 if (powerRestored)
2684 runPowerRestore = 1;
2685
2686 int64_t current = timeval_ms();
2687 static int64_t lastPower;
2688 bool waitMore = lastPower + 2000 > current;
2689 if (powerDropout && !waitMore) {
2690 runPowerDropout = 1;
2691 lastPower = current;
2692 }
2693
2694 retval = jtag_srst_asserted(&srstAsserted);
2695 if (retval != ERROR_OK)
2696 return retval;
2697
2698 int srstDeasserted;
2699 srstDeasserted = prevSrstAsserted && !srstAsserted;
2700
2701 static int64_t lastSrst;
2702 waitMore = lastSrst + 2000 > current;
2703 if (srstDeasserted && !waitMore) {
2704 runSrstDeasserted = 1;
2705 lastSrst = current;
2706 }
2707
2708 if (!prevSrstAsserted && srstAsserted)
2709 runSrstAsserted = 1;
2710
2711 prevSrstAsserted = srstAsserted;
2712 prevPowerdropout = powerDropout;
2713
2714 if (srstDeasserted || powerRestored) {
2715 /* Other than logging the event we can't do anything here.
2716 * Issuing a reset is a particularly bad idea as we might
2717 * be inside a reset already.
2718 */
2719 }
2720
2721 return ERROR_OK;
2722 }
2723
2724 /* process target state changes */
2725 static int handle_target(void *priv)
2726 {
2727 Jim_Interp *interp = (Jim_Interp *)priv;
2728 int retval = ERROR_OK;
2729
2730 if (!is_jtag_poll_safe()) {
2731 /* polling is disabled currently */
2732 return ERROR_OK;
2733 }
2734
2735 /* we do not want to recurse here... */
2736 static int recursive;
2737 if (!recursive) {
2738 recursive = 1;
2739 sense_handler();
2740 /* danger! running these procedures can trigger srst assertions and power dropouts.
2741 * We need to avoid an infinite loop/recursion here and we do that by
2742 * clearing the flags after running these events.
2743 */
2744 int did_something = 0;
2745 if (runSrstAsserted) {
2746 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2747 Jim_Eval(interp, "srst_asserted");
2748 did_something = 1;
2749 }
2750 if (runSrstDeasserted) {
2751 Jim_Eval(interp, "srst_deasserted");
2752 did_something = 1;
2753 }
2754 if (runPowerDropout) {
2755 LOG_INFO("Power dropout detected, running power_dropout proc.");
2756 Jim_Eval(interp, "power_dropout");
2757 did_something = 1;
2758 }
2759 if (runPowerRestore) {
2760 Jim_Eval(interp, "power_restore");
2761 did_something = 1;
2762 }
2763
2764 if (did_something) {
2765 /* clear detect flags */
2766 sense_handler();
2767 }
2768
2769 /* clear action flags */
2770
2771 runSrstAsserted = 0;
2772 runSrstDeasserted = 0;
2773 runPowerRestore = 0;
2774 runPowerDropout = 0;
2775
2776 recursive = 0;
2777 }
2778
2779 /* Poll targets for state changes unless that's globally disabled.
2780 * Skip targets that are currently disabled.
2781 */
2782 for (struct target *target = all_targets;
2783 is_jtag_poll_safe() && target;
2784 target = target->next) {
2785
2786 if (!target_was_examined(target))
2787 continue;
2788
2789 if (!target->tap->enabled)
2790 continue;
2791
2792 if (target->backoff.times > target->backoff.count) {
2793 /* do not poll this time as we failed previously */
2794 target->backoff.count++;
2795 continue;
2796 }
2797 target->backoff.count = 0;
2798
2799 /* only poll target if we've got power and srst isn't asserted */
2800 if (!powerDropout && !srstAsserted) {
2801 /* polling may fail silently until the target has been examined */
2802 retval = target_poll(target);
2803 if (retval != ERROR_OK) {
2804 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2805 if (target->backoff.times * polling_interval < 5000) {
2806 target->backoff.times *= 2;
2807 target->backoff.times++;
2808 }
2809
2810 /* Tell GDB to halt the debugger. This allows the user to
2811 * run monitor commands to handle the situation.
2812 */
2813 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2814 }
2815 if (target->backoff.times > 0) {
2816 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2817 target_reset_examined(target);
2818 retval = target_examine_one(target);
2819 /* Target examination could have failed due to unstable connection,
2820 * but we set the examined flag anyway to repoll it later */
2821 if (retval != ERROR_OK) {
2822 target->examined = true;
2823 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2824 target->backoff.times * polling_interval);
2825 return retval;
2826 }
2827 }
2828
2829 /* Since we succeeded, we reset backoff count */
2830 target->backoff.times = 0;
2831 }
2832 }
2833
2834 return retval;
2835 }
2836
2837 COMMAND_HANDLER(handle_reg_command)
2838 {
2839 struct target *target;
2840 struct reg *reg = NULL;
2841 unsigned count = 0;
2842 char *value;
2843
2844 LOG_DEBUG("-");
2845
2846 target = get_current_target(CMD_CTX);
2847
2848 /* list all available registers for the current target */
2849 if (CMD_ARGC == 0) {
2850 struct reg_cache *cache = target->reg_cache;
2851
2852 count = 0;
2853 while (cache) {
2854 unsigned i;
2855
2856 command_print(CMD, "===== %s", cache->name);
2857
2858 for (i = 0, reg = cache->reg_list;
2859 i < cache->num_regs;
2860 i++, reg++, count++) {
2861 if (reg->exist == false)
2862 continue;
2863 /* only print cached values if they are valid */
2864 if (reg->valid) {
2865 value = buf_to_str(reg->value,
2866 reg->size, 16);
2867 command_print(CMD,
2868 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2869 count, reg->name,
2870 reg->size, value,
2871 reg->dirty
2872 ? " (dirty)"
2873 : "");
2874 free(value);
2875 } else {
2876 command_print(CMD, "(%i) %s (/%" PRIu32 ")",
2877 count, reg->name,
2878 reg->size) ;
2879 }
2880 }
2881 cache = cache->next;
2882 }
2883
2884 return ERROR_OK;
2885 }
2886
2887 /* access a single register by its ordinal number */
2888 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2889 unsigned num;
2890 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2891
2892 struct reg_cache *cache = target->reg_cache;
2893 count = 0;
2894 while (cache) {
2895 unsigned i;
2896 for (i = 0; i < cache->num_regs; i++) {
2897 if (count++ == num) {
2898 reg = &cache->reg_list[i];
2899 break;
2900 }
2901 }
2902 if (reg)
2903 break;
2904 cache = cache->next;
2905 }
2906
2907 if (!reg) {
2908 command_print(CMD, "%i is out of bounds, the current target "
2909 "has only %i registers (0 - %i)", num, count, count - 1);
2910 return ERROR_OK;
2911 }
2912 } else {
2913 /* access a single register by its name */
2914 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2915
2916 if (!reg)
2917 goto not_found;
2918 }
2919
2920 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2921
2922 if (!reg->exist)
2923 goto not_found;
2924
2925 /* display a register */
2926 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2927 && (CMD_ARGV[1][0] <= '9')))) {
2928 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2929 reg->valid = 0;
2930
2931 if (reg->valid == 0)
2932 reg->type->get(reg);
2933 value = buf_to_str(reg->value, reg->size, 16);
2934 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2935 free(value);
2936 return ERROR_OK;
2937 }
2938
2939 /* set register value */
2940 if (CMD_ARGC == 2) {
2941 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2942 if (buf == NULL)
2943 return ERROR_FAIL;
2944 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2945
2946 reg->type->set(reg, buf);
2947
2948 value = buf_to_str(reg->value, reg->size, 16);
2949 command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2950 free(value);
2951
2952 free(buf);
2953
2954 return ERROR_OK;
2955 }
2956
2957 return ERROR_COMMAND_SYNTAX_ERROR;
2958
2959 not_found:
2960 command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
2961 return ERROR_OK;
2962 }
2963
2964 COMMAND_HANDLER(handle_poll_command)
2965 {
2966 int retval = ERROR_OK;
2967 struct target *target = get_current_target(CMD_CTX);
2968
2969 if (CMD_ARGC == 0) {
2970 command_print(CMD, "background polling: %s",
2971 jtag_poll_get_enabled() ? "on" : "off");
2972 command_print(CMD, "TAP: %s (%s)",
2973 target->tap->dotted_name,
2974 target->tap->enabled ? "enabled" : "disabled");
2975 if (!target->tap->enabled)
2976 return ERROR_OK;
2977 retval = target_poll(target);
2978 if (retval != ERROR_OK)
2979 return retval;
2980 retval = target_arch_state(target);
2981 if (retval != ERROR_OK)
2982 return retval;
2983 } else if (CMD_ARGC == 1) {
2984 bool enable;
2985 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2986 jtag_poll_set_enabled(enable);
2987 } else
2988 return ERROR_COMMAND_SYNTAX_ERROR;
2989
2990 return retval;
2991 }
2992
2993 COMMAND_HANDLER(handle_wait_halt_command)
2994 {
2995 if (CMD_ARGC > 1)
2996 return ERROR_COMMAND_SYNTAX_ERROR;
2997
2998 unsigned ms = DEFAULT_HALT_TIMEOUT;
2999 if (1 == CMD_ARGC) {
3000 int retval = parse_uint(CMD_ARGV[0], &ms);
3001 if (ERROR_OK != retval)
3002 return ERROR_COMMAND_SYNTAX_ERROR;
3003 }
3004
3005 struct target *target = get_current_target(CMD_CTX);
3006 return target_wait_state(target, TARGET_HALTED, ms);
3007 }
3008
3009 /* wait for target state to change. The trick here is to have a low
3010 * latency for short waits and not to suck up all the CPU time
3011 * on longer waits.
3012 *
3013 * After 500ms, keep_alive() is invoked
3014 */
3015 int target_wait_state(struct target *target, enum target_state state, int ms)
3016 {
3017 int retval;
3018 int64_t then = 0, cur;
3019 bool once = true;
3020
3021 for (;;) {
3022 retval = target_poll(target);
3023 if (retval != ERROR_OK)
3024 return retval;
3025 if (target->state == state)
3026 break;
3027 cur = timeval_ms();
3028 if (once) {
3029 once = false;
3030 then = timeval_ms();
3031 LOG_DEBUG("waiting for target %s...",
3032 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3033 }
3034
3035 if (cur-then > 500)
3036 keep_alive();
3037
3038 if ((cur-then) > ms) {
3039 LOG_ERROR("timed out while waiting for target %s",
3040 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3041 return ERROR_FAIL;
3042 }
3043 }
3044
3045 return ERROR_OK;
3046 }
3047
3048 COMMAND_HANDLER(handle_halt_command)
3049 {
3050 LOG_DEBUG("-");
3051
3052 struct target *target = get_current_target(CMD_CTX);
3053
3054 target->verbose_halt_msg = true;
3055
3056 int retval = target_halt(target);
3057 if (ERROR_OK != retval)
3058 return retval;
3059
3060 if (CMD_ARGC == 1) {
3061 unsigned wait_local;
3062 retval = parse_uint(CMD_ARGV[0], &wait_local);
3063 if (ERROR_OK != retval)
3064 return ERROR_COMMAND_SYNTAX_ERROR;
3065 if (!wait_local)
3066 return ERROR_OK;
3067 }
3068
3069 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3070 }
3071
3072 COMMAND_HANDLER(handle_soft_reset_halt_command)
3073 {
3074 struct target *target = get_current_target(CMD_CTX);
3075
3076 LOG_USER("requesting target halt and executing a soft reset");
3077
3078 target_soft_reset_halt(target);
3079
3080 return ERROR_OK;
3081 }
3082
3083 COMMAND_HANDLER(handle_reset_command)
3084 {
3085 if (CMD_ARGC > 1)
3086 return ERROR_COMMAND_SYNTAX_ERROR;
3087
3088 enum target_reset_mode reset_mode = RESET_RUN;
3089 if (CMD_ARGC == 1) {
3090 const Jim_Nvp *n;
3091 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
3092 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
3093 return ERROR_COMMAND_SYNTAX_ERROR;
3094 reset_mode = n->value;
3095 }
3096
3097 /* reset *all* targets */
3098 return target_process_reset(CMD, reset_mode);
3099 }
3100
3101
3102 COMMAND_HANDLER(handle_resume_command)
3103 {
3104 int current = 1;
3105 if (CMD_ARGC > 1)
3106 return ERROR_COMMAND_SYNTAX_ERROR;
3107
3108 struct target *target = get_current_target(CMD_CTX);
3109
3110 /* with no CMD_ARGV, resume from current pc, addr = 0,
3111 * with one arguments, addr = CMD_ARGV[0],
3112 * handle breakpoints, not debugging */
3113 target_addr_t addr = 0;
3114 if (CMD_ARGC == 1) {
3115 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3116 current = 0;
3117 }
3118
3119 return target_resume(target, current, addr, 1, 0);
3120 }
3121
3122 COMMAND_HANDLER(handle_step_command)
3123 {
3124 if (CMD_ARGC > 1)
3125 return ERROR_COMMAND_SYNTAX_ERROR;
3126
3127 LOG_DEBUG("-");
3128
3129 /* with no CMD_ARGV, step from current pc, addr = 0,
3130 * with one argument addr = CMD_ARGV[0],
3131 * handle breakpoints, debugging */
3132 target_addr_t addr = 0;
3133 int current_pc = 1;
3134 if (CMD_ARGC == 1) {
3135 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3136 current_pc = 0;
3137 }
3138
3139 struct target *target = get_current_target(CMD_CTX);
3140
3141 return target->type->step(target, current_pc, addr, 1);
3142 }
3143
3144 void target_handle_md_output(struct command_invocation *cmd,
3145 struct target *target, target_addr_t address, unsigned size,
3146 unsigned count, const uint8_t *buffer)
3147 {
3148 const unsigned line_bytecnt = 32;
3149 unsigned line_modulo = line_bytecnt / size;
3150
3151 char output[line_bytecnt * 4 + 1];
3152 unsigned output_len = 0;
3153
3154 const char *value_fmt;
3155 switch (size) {
3156 case 8:
3157 value_fmt = "%16.16"PRIx64" ";
3158 break;
3159 case 4:
3160 value_fmt = "%8.8"PRIx64" ";
3161 break;
3162 case 2:
3163 value_fmt = "%4.4"PRIx64" ";
3164 break;
3165 case 1:
3166 value_fmt = "%2.2"PRIx64" ";
3167 break;
3168 default:
3169 /* "can't happen", caller checked */
3170 LOG_ERROR("invalid memory read size: %u", size);
3171 return;
3172 }
3173
3174 for (unsigned i = 0; i < count; i++) {
3175 if (i % line_modulo == 0) {
3176 output_len += snprintf(output + output_len,
3177 sizeof(output) - output_len,
3178 TARGET_ADDR_FMT ": ",
3179 (address + (i * size)));
3180 }
3181
3182 uint64_t value = 0;
3183 const uint8_t *value_ptr = buffer + i * size;
3184 switch (size) {
3185 case 8:
3186 value = target_buffer_get_u64(target, value_ptr);
3187 break;
3188 case 4:
3189 value = target_buffer_get_u32(target, value_ptr);
3190 break;
3191 case 2:
3192 value = target_buffer_get_u16(target, value_ptr);
3193 break;
3194 case 1:
3195 value = *value_ptr;
3196 }
3197 output_len += snprintf(output + output_len