target.c 120 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007-2009 Øyvind Harboe                                 *
6
7
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
 *   Copyright (C) 2008, Duane Ellis                                       *
 *   openocd@duaneeellis.com                                               *
 *                                                                         *
11
12
13
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
14
15
16
 *   Copyright (C) 2008 by Rick Altherr                                    *
 *   kc8apf@kc8apf.net>                                                    *
 *                                                                         *
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "target.h"
37
#include "target_type.h"
38
#include "target_request.h"
39
#include "breakpoints.h"
40
41
42
43
#include "time_support.h"
#include "register.h"
#include "trace.h"
#include "image.h"
44
45
46
#include "jtag.h"


Zachary T Welch's avatar
Zachary T Welch committed
47
48
static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
49

50
/* targets */
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
extern struct target_type arm7tdmi_target;
extern struct target_type arm720t_target;
extern struct target_type arm9tdmi_target;
extern struct target_type arm920t_target;
extern struct target_type arm966e_target;
extern struct target_type arm926ejs_target;
extern struct target_type fa526_target;
extern struct target_type feroceon_target;
extern struct target_type dragonite_target;
extern struct target_type xscale_target;
extern struct target_type cortexm3_target;
extern struct target_type cortexa8_target;
extern struct target_type arm11_target;
extern struct target_type mips_m4k_target;
extern struct target_type avr_target;
Zachary T Welch's avatar
Zachary T Welch committed
66
extern struct target_type testee_target;
67
68

struct target_type *target_types[] =
69
70
71
72
73
74
75
{
	&arm7tdmi_target,
	&arm9tdmi_target,
	&arm920t_target,
	&arm720t_target,
	&arm966e_target,
	&arm926ejs_target,
76
	&fa526_target,
77
	&feroceon_target,
78
	&dragonite_target,
79
80
	&xscale_target,
	&cortexm3_target,
81
	&cortexa8_target,
oharboe's avatar
oharboe committed
82
	&arm11_target,
ntfreak's avatar
ntfreak committed
83
	&mips_m4k_target,
84
	&avr_target,
Zachary T Welch's avatar
Zachary T Welch committed
85
	&testee_target,
86
87
88
	NULL,
};

Zachary T Welch's avatar
Zachary T Welch committed
89
struct target *all_targets = NULL;
90
struct target_event_callback *target_event_callbacks = NULL;
91
struct target_timer_callback *target_timer_callbacks = NULL;
92

93
94
95
96
97
98
99
100
const Jim_Nvp nvp_assert[] = {
	{ .name = "assert", NVP_ASSERT },
	{ .name = "deassert", NVP_DEASSERT },
	{ .name = "T", NVP_ASSERT },
	{ .name = "F", NVP_DEASSERT },
	{ .name = "t", NVP_ASSERT },
	{ .name = "f", NVP_DEASSERT },
	{ .name = NULL, .value = -1 }
101
102
};

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const Jim_Nvp nvp_error_target[] = {
	{ .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
	{ .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
	{ .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
	{ .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
	{ .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
	{ .value = ERROR_TARGET_UNALIGNED_ACCESS   , .name = "err-unaligned-access" },
	{ .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
	{ .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
	{ .value = ERROR_TARGET_TRANSLATION_FAULT  , .name = "err-translation-fault" },
	{ .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
	{ .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
	{ .value = -1, .name = NULL }
};

118
const char *target_strerror_safe(int err)
119
120
121
{
	const Jim_Nvp *n;

122
	n = Jim_Nvp_value2name_simple(nvp_error_target, err);
zwelch's avatar
zwelch committed
123
	if (n->name == NULL) {
124
125
126
127
128
129
		return "unknown";
	} else {
		return n->name;
	}
}

130
static const Jim_Nvp nvp_target_event[] = {
131
132
	{ .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
	{ .value = TARGET_EVENT_OLD_pre_resume         , .name = "old-pre_resume" },
133

134
	{ .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
135
136
137
138
139
	{ .value = TARGET_EVENT_HALTED, .name = "halted" },
	{ .value = TARGET_EVENT_RESUMED, .name = "resumed" },
	{ .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
	{ .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },

140
141
142
	{ .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
	{ .name = "gdb-end", .value = TARGET_EVENT_GDB_END },

143
	/* historical name */
144

145
	{ .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
146
147

	{ .value = TARGET_EVENT_RESET_ASSERT_PRE,    .name = "reset-assert-pre" },
148
	{ .value = TARGET_EVENT_RESET_ASSERT,        .name = "reset-assert" },
149
150
151
152
153
154
155
	{ .value = TARGET_EVENT_RESET_ASSERT_POST,   .name = "reset-assert-post" },
	{ .value = TARGET_EVENT_RESET_DEASSERT_PRE,  .name = "reset-deassert-pre" },
	{ .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
	{ .value = TARGET_EVENT_RESET_HALT_PRE,      .name = "reset-halt-pre" },
	{ .value = TARGET_EVENT_RESET_HALT_POST,     .name = "reset-halt-post" },
	{ .value = TARGET_EVENT_RESET_WAIT_PRE,      .name = "reset-wait-pre" },
	{ .value = TARGET_EVENT_RESET_WAIT_POST,     .name = "reset-wait-post" },
156
157
	{ .value = TARGET_EVENT_RESET_INIT,          .name = "reset-init" },
	{ .value = TARGET_EVENT_RESET_END,           .name = "reset-end" },
158

159
	{ .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
160
	{ .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	{ .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
	{ .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },

	{ .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
	{ .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },

	{ .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
	{ .value = TARGET_EVENT_GDB_FLASH_WRITE_END  , .name = "gdb-flash-write-end"   },

	{ .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
	{ .value = TARGET_EVENT_GDB_FLASH_ERASE_END  , .name = "gdb-flash-erase-end" },

	{ .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
	{ .value = TARGET_EVENT_RESUMED     , .name = "resume-ok" },
	{ .value = TARGET_EVENT_RESUME_END  , .name = "resume-end" },

	{ .name = NULL, .value = -1 }
179
180
};

181
182
183
184
185
186
187
const Jim_Nvp nvp_target_state[] = {
	{ .name = "unknown", .value = TARGET_UNKNOWN },
	{ .name = "running", .value = TARGET_RUNNING },
	{ .name = "halted",  .value = TARGET_HALTED },
	{ .name = "reset",   .value = TARGET_RESET },
	{ .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
	{ .name = NULL, .value = -1 },
188
189
};

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const Jim_Nvp nvp_target_debug_reason [] = {
	{ .name = "debug-request"            , .value = DBG_REASON_DBGRQ },
	{ .name = "breakpoint"               , .value = DBG_REASON_BREAKPOINT },
	{ .name = "watchpoint"               , .value = DBG_REASON_WATCHPOINT },
	{ .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
	{ .name = "single-step"              , .value = DBG_REASON_SINGLESTEP },
	{ .name = "target-not-halted"        , .value = DBG_REASON_NOTHALTED  },
	{ .name = "undefined"                , .value = DBG_REASON_UNDEFINED },
	{ .name = NULL, .value = -1 },
};

const Jim_Nvp nvp_target_endian[] = {
	{ .name = "big",    .value = TARGET_BIG_ENDIAN },
	{ .name = "little", .value = TARGET_LITTLE_ENDIAN },
	{ .name = "be",     .value = TARGET_BIG_ENDIAN },
205
	{ .name = "le",     .value = TARGET_LITTLE_ENDIAN },
206
207
208
	{ .name = NULL,     .value = -1 },
};

209
210
211
212
213
214
215
216
const Jim_Nvp nvp_reset_modes[] = {
	{ .name = "unknown", .value = RESET_UNKNOWN },
	{ .name = "run"    , .value = RESET_RUN },
	{ .name = "halt"   , .value = RESET_HALT },
	{ .name = "init"   , .value = RESET_INIT },
	{ .name = NULL     , .value = -1 },
};

217
const char *
Zachary T Welch's avatar
Zachary T Welch committed
218
target_state_name( struct target *t )
219
220
221
222
223
224
225
226
227
228
{
	const char *cp;
	cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
	if( !cp ){
		LOG_ERROR("Invalid target state: %d", (int)(t->state));
		cp = "(*BUG*unknown*BUG*)";
	}
	return cp;
}

229
/* determine the number of the new target */
230
static int new_target_number(void)
231
{
Zachary T Welch's avatar
Zachary T Welch committed
232
	struct target *t;
233
234
235
236
237
	int x;

	/* number is 0 based */
	x = -1;
	t = all_targets;
zwelch's avatar
zwelch committed
238
239
	while (t) {
		if (x < t->target_number) {
240
241
242
243
			x = t->target_number;
		}
		t = t->next;
	}
zwelch's avatar
zwelch committed
244
	return x + 1;
245
246
}

247
/* read a uint32_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
248
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
249
250
251
252
253
254
255
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u32(buffer);
	else
		return be_to_h_u32(buffer);
}

zwelch's avatar
zwelch committed
256
/* read a uint16_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
257
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
258
259
260
261
262
263
264
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u16(buffer);
	else
		return be_to_h_u16(buffer);
}

265
/* read a uint8_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
266
uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
267
268
269
270
{
	return *buffer & 0x0ff;
}

271
/* write a uint32_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
272
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
273
274
275
276
277
278
279
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u32_to_le(buffer, value);
	else
		h_u32_to_be(buffer, value);
}

zwelch's avatar
zwelch committed
280
/* write a uint16_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
281
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
282
283
284
285
286
287
288
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u16_to_le(buffer, value);
	else
		h_u16_to_be(buffer, value);
}

289
/* write a uint8_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
290
void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
291
292
293
294
{
	*buffer = value;
}

295
/* return a pointer to a configured target; id is name or number */
Zachary T Welch's avatar
Zachary T Welch committed
296
struct target *get_target(const char *id)
297
{
Zachary T Welch's avatar
Zachary T Welch committed
298
	struct target *target;
299
300
301
302
303
304
305
306
307

	/* try as tcltarget name */
	for (target = all_targets; target; target = target->next) {
		if (target->cmd_name == NULL)
			continue;
		if (strcmp(id, target->cmd_name) == 0)
			return target;
	}

308
309
	/* It's OK to remove this fallback sometime after August 2010 or so */

310
	/* no match, try as number */
311
312
	unsigned num;
	if (parse_uint(id, &num) != ERROR_OK)
313
314
315
		return NULL;

	for (target = all_targets; target; target = target->next) {
316
317
318
		if (target->target_number == (int)num) {
			LOG_WARNING("use '%s' as target identifier, not '%u'",
					target->cmd_name, num);
319
			return target;
320
		}
321
322
323
324
325
	}

	return NULL;
}

326
/* returns a pointer to the n-th configured target */
Zachary T Welch's avatar
Zachary T Welch committed
327
static struct target *get_target_by_num(int num)
328
{
Zachary T Welch's avatar
Zachary T Welch committed
329
	struct target *target = all_targets;
330

zwelch's avatar
zwelch committed
331
332
	while (target) {
		if (target->target_number == num) {
333
			return target;
334
		}
335
336
337
338
339
340
		target = target->next;
	}

	return NULL;
}

341
struct target* get_current_target(struct command_context *cmd_ctx)
342
{
Zachary T Welch's avatar
Zachary T Welch committed
343
	struct target *target = get_target_by_num(cmd_ctx->current_target);
oharboe's avatar
oharboe committed
344

345
346
	if (target == NULL)
	{
347
		LOG_ERROR("BUG: current_target out of bounds");
348
349
350
		exit(-1);
	}

oharboe's avatar
oharboe committed
351
	return target;
352
353
}

Zachary T Welch's avatar
Zachary T Welch committed
354
int target_poll(struct target *target)
355
{
356
357
	int retval;

358
	/* We can't poll until after examine */
359
	if (!target_was_examined(target))
360
361
362
363
	{
		/* Fail silently lest we pollute the log */
		return ERROR_FAIL;
	}
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

	retval = target->type->poll(target);
	if (retval != ERROR_OK)
		return retval;

	if (target->halt_issued)
	{
		if (target->state == TARGET_HALTED)
		{
			target->halt_issued = false;
		} else
		{
			long long t = timeval_ms() - target->halt_issued_time;
			if (t>1000)
			{
				target->halt_issued = false;
				LOG_INFO("Halt timed out, wake up GDB.");
				target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
			}
		}
	}

	return ERROR_OK;
387
388
}

Zachary T Welch's avatar
Zachary T Welch committed
389
int target_halt(struct target *target)
390
{
391
	int retval;
392
	/* We can't poll until after examine */
393
	if (!target_was_examined(target))
394
395
396
397
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
398
399
400
401
402
403
404
405
406

	retval = target->type->halt(target);
	if (retval != ERROR_OK)
		return retval;

	target->halt_issued = true;
	target->halt_issued_time = timeval_ms();

	return ERROR_OK;
407
408
}

Zachary T Welch's avatar
Zachary T Welch committed
409
int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
410
{
411
	int retval;
oharboe's avatar
oharboe committed
412

413
	/* We can't poll until after examine */
414
	if (!target_was_examined(target))
415
416
417
418
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
419

oharboe's avatar
oharboe committed
420
421
422
423
	/* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
	 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
	 * the application.
	 */
424
425
	if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
		return retval;
oharboe's avatar
oharboe committed
426

427
	return retval;
428
429
}

430
int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
431
432
{
	char buf[100];
433
	int retval;
434
	Jim_Nvp *n;
435
	n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
zwelch's avatar
zwelch committed
436
	if (n->name == NULL) {
437
438
439
440
		LOG_ERROR("invalid reset mode");
		return ERROR_FAIL;
	}

441
442
443
444
	/* disable polling during reset to make reset event scripts
	 * more predictable, i.e. dr/irscan & pathmove in events will
	 * not have JTAG operations injected into the middle of a sequence.
	 */
445
446
447
	bool save_poll = jtag_poll_get_enabled();

	jtag_poll_set_enabled(false);
448

449
	sprintf(buf, "ocd_process_reset %s", n->name);
450
	retval = Jim_Eval(cmd_ctx->interp, buf);
451

452
	jtag_poll_set_enabled(save_poll);
453

zwelch's avatar
zwelch committed
454
	if (retval != JIM_OK) {
455
		Jim_PrintErrorMessage(cmd_ctx->interp);
456
457
		return ERROR_FAIL;
	}
458
459

	/* We want any events to be processed before the prompt */
460
	retval = target_call_timer_callbacks_now();
461

462
	return retval;
463
464
}

Zachary T Welch's avatar
Zachary T Welch committed
465
static int identity_virt2phys(struct target *target,
David Brownell's avatar
David Brownell committed
466
		uint32_t virtual, uint32_t *physical)
467
468
469
470
471
{
	*physical = virtual;
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
472
static int no_mmu(struct target *target, int *enabled)
473
{
474
	*enabled = 0;
475
476
477
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
478
static int default_examine(struct target *target)
479
{
480
	target_set_examined(target);
481
482
483
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
484
int target_examine_one(struct target *target)
zwelch's avatar
zwelch committed
485
486
487
488
{
	return target->type->examine(target);
}

zwelch's avatar
zwelch committed
489
490
static int jtag_enable_callback(enum jtag_event event, void *priv)
{
Zachary T Welch's avatar
Zachary T Welch committed
491
	struct target *target = priv;
zwelch's avatar
zwelch committed
492
493
494
495
496
497
498
499
500

	if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
		return ERROR_OK;

	jtag_unregister_event_callback(jtag_enable_callback, target);
	return target_examine_one(target);
}


zwelch's avatar
zwelch committed
501
/* Targets that correctly implement init + examine, i.e.
502
 * no communication with target during init:
oharboe's avatar
oharboe committed
503
504
 *
 * XScale
505
 */
ntfreak's avatar
ntfreak committed
506
int target_examine(void)
507
508
{
	int retval = ERROR_OK;
Zachary T Welch's avatar
Zachary T Welch committed
509
	struct target *target;
zwelch's avatar
zwelch committed
510
511

	for (target = all_targets; target; target = target->next)
512
	{
zwelch's avatar
zwelch committed
513
514
515
516
		/* defer examination, but don't skip it */
		if (!target->tap->enabled) {
			jtag_register_event_callback(jtag_enable_callback,
					target);
zwelch's avatar
zwelch committed
517
			continue;
zwelch's avatar
zwelch committed
518
		}
zwelch's avatar
zwelch committed
519
		if ((retval = target_examine_one(target)) != ERROR_OK)
520
521
522
523
			return retval;
	}
	return retval;
}
524
const char *target_type_name(struct target *target)
zwelch's avatar
zwelch committed
525
526
527
{
	return target->type->name;
}
528

Zachary T Welch's avatar
Zachary T Welch committed
529
static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
530
{
531
	if (!target_was_examined(target))
532
533
534
535
536
537
538
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
	return target->type->write_memory_imp(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
539
static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
540
{
541
	if (!target_was_examined(target))
542
543
544
545
546
547
548
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
	return target->type->read_memory_imp(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
549
static int target_soft_reset_halt_imp(struct target *target)
550
{
551
	if (!target_was_examined(target))
552
553
554
555
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
556
557
	if (!target->type->soft_reset_halt_imp) {
		LOG_ERROR("Target %s does not support soft_reset_halt",
558
				target_name(target));
559
560
		return ERROR_FAIL;
	}
561
562
563
	return target->type->soft_reset_halt_imp(target);
}

Zachary T Welch's avatar
Zachary T Welch committed
564
static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
565
{
566
	if (!target_was_examined(target))
567
568
569
570
571
572
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
	return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
}
573

Zachary T Welch's avatar
Zachary T Welch committed
574
int target_read_memory(struct target *target,
575
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
576
577
578
579
{
	return target->type->read_memory(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
580
int target_read_phys_memory(struct target *target,
581
582
583
584
585
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
{
	return target->type->read_phys_memory(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
586
int target_write_memory(struct target *target,
587
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
588
589
590
{
	return target->type->write_memory(target, address, size, count, buffer);
}
591

Zachary T Welch's avatar
Zachary T Welch committed
592
int target_write_phys_memory(struct target *target,
593
594
595
596
597
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
{
	return target->type->write_phys_memory(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
598
int target_bulk_write_memory(struct target *target,
599
		uint32_t address, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
600
601
602
603
{
	return target->type->bulk_write_memory(target, address, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
604
int target_add_breakpoint(struct target *target,
605
		struct breakpoint *breakpoint)
606
{
607
608
609
610
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
611
612
	return target->type->add_breakpoint(target, breakpoint);
}
Zachary T Welch's avatar
Zachary T Welch committed
613
int target_remove_breakpoint(struct target *target,
614
		struct breakpoint *breakpoint)
615
616
617
618
{
	return target->type->remove_breakpoint(target, breakpoint);
}

Zachary T Welch's avatar
Zachary T Welch committed
619
int target_add_watchpoint(struct target *target,
620
		struct watchpoint *watchpoint)
621
{
622
623
624
625
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
626
627
	return target->type->add_watchpoint(target, watchpoint);
}
Zachary T Welch's avatar
Zachary T Welch committed
628
int target_remove_watchpoint(struct target *target,
629
		struct watchpoint *watchpoint)
630
631
632
{
	return target->type->remove_watchpoint(target, watchpoint);
}
zwelch's avatar
zwelch committed
633

Zachary T Welch's avatar
Zachary T Welch committed
634
int target_get_gdb_reg_list(struct target *target,
Zachary T Welch's avatar
Zachary T Welch committed
635
		struct reg **reg_list[], int *reg_list_size)
zwelch's avatar
zwelch committed
636
637
638
{
	return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
}
Zachary T Welch's avatar
Zachary T Welch committed
639
int target_step(struct target *target,
640
		int current, uint32_t address, int handle_breakpoints)
zwelch's avatar
zwelch committed
641
642
643
644
{
	return target->type->step(target, current, address, handle_breakpoints);
}

zwelch's avatar
zwelch committed
645

Zachary T Welch's avatar
Zachary T Welch committed
646
int target_run_algorithm(struct target *target,
647
		int num_mem_params, struct mem_param *mem_params,
648
		int num_reg_params, struct reg_param *reg_param,
649
		uint32_t entry_point, uint32_t exit_point,
zwelch's avatar
zwelch committed
650
651
652
653
654
655
656
		int timeout_ms, void *arch_info)
{
	return target->type->run_algorithm(target,
			num_mem_params, mem_params, num_reg_params, reg_param,
			entry_point, exit_point, timeout_ms, arch_info);
}

657
658
659
660
661
/**
 * Reset the @c examined flag for the given target.
 * Pure paranoia -- targets are zeroed on allocation.
 */
static void target_reset_examined(struct target *target)
662
{
663
	target->examined = false;
664
665
}

666
static int
Zachary T Welch's avatar
Zachary T Welch committed
667
err_read_phys_memory(struct target *target, uint32_t address,
668
		uint32_t size, uint32_t count, uint8_t *buffer)
669
{
670
	LOG_ERROR("Not implemented: %s", __func__);
671
	return ERROR_FAIL;
672
673
}

674
static int
Zachary T Welch's avatar
Zachary T Welch committed
675
err_write_phys_memory(struct target *target, uint32_t address,
676
		uint32_t size, uint32_t count, uint8_t *buffer)
677
{
678
	LOG_ERROR("Not implemented: %s", __func__);
679
	return ERROR_FAIL;
680
681
}

682
683
static int handle_target(void *priv);

684
685
static int target_init_one(struct command_context *cmd_ctx,
		struct target *target)
686
{
687
	target_reset_examined(target);
oharboe's avatar
oharboe committed
688

689
690
691
	struct target_type *type = target->type;
	if (type->examine == NULL)
		type->examine = default_examine;
692

693
694
695
696
697
698
	int retval = type->init_target(cmd_ctx, target);
	if (ERROR_OK != retval)
	{
		LOG_ERROR("target '%s' init failed", target_name(target));
		return retval;
	}
oharboe's avatar
oharboe committed
699

700
701
702
703
704
705
706
707
708
709
	/**
	 * @todo get rid of those *memory_imp() methods, now that all
	 * callers are using target_*_memory() accessors ... and make
	 * sure the "physical" paths handle the same issues.
	 */
	/* a non-invasive way(in terms of patches) to add some code that
	 * runs before the type->write/read_memory implementation
	 */
	type->write_memory_imp = target->type->write_memory;
	type->write_memory = target_write_memory_imp;
oharboe's avatar
oharboe committed
710

711
712
	type->read_memory_imp = target->type->read_memory;
	type->read_memory = target_read_memory_imp;
713

714
715
	type->soft_reset_halt_imp = target->type->soft_reset_halt;
	type->soft_reset_halt = target_soft_reset_halt_imp;
David Brownell's avatar
David Brownell committed
716

717
718
	type->run_algorithm_imp = target->type->run_algorithm;
	type->run_algorithm = target_run_algorithm_imp;
719

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
	/* Sanity-check MMU support ... stub in what we must, to help
	 * implement it in stages, but warn if we need to do so.
	 */
	if (type->mmu)
	{
		if (type->write_phys_memory == NULL)
		{
			LOG_ERROR("type '%s' is missing write_phys_memory",
					type->name);
			type->write_phys_memory = err_write_phys_memory;
		}
		if (type->read_phys_memory == NULL)
		{
			LOG_ERROR("type '%s' is missing read_phys_memory",
					type->name);
			type->read_phys_memory = err_read_phys_memory;
		}
		if (type->virt2phys == NULL)
		{
			LOG_ERROR("type '%s' is missing virt2phys", type->name);
			type->virt2phys = identity_virt2phys;
		}
	}
	else
	{
745
746
747
748
		/* Make sure no-MMU targets all behave the same:  make no
		 * distinction between physical and virtual addresses, and
		 * ensure that virt2phys() is always an identity mapping.
		 */
749
750
751
752
		if (type->write_phys_memory || type->read_phys_memory
				|| type->virt2phys)
		{
			LOG_WARNING("type '%s' has bad MMU hooks", type->name);
753
		}
754
755
756
757
758

		type->mmu = no_mmu;
		type->write_phys_memory = type->write_memory;
		type->read_phys_memory = type->read_memory;
		type->virt2phys = identity_virt2phys;
759
	}
760
761
	return ERROR_OK;
}
oharboe's avatar
oharboe committed
762

763
764
765
766
767
768
int target_init(struct command_context *cmd_ctx)
{
	struct target *target;
	int retval;

	for (target = all_targets; target; target = target->next)
769
	{
770
771
		retval = target_init_one(cmd_ctx, target);
		if (ERROR_OK != retval)
772
			return retval;
773
	}
oharboe's avatar
oharboe committed
774

775
776
777
778
779
780
781
782
783
784
785
786
	if (!all_targets)
		return ERROR_OK;

	retval = target_register_user_commands(cmd_ctx);
	if (ERROR_OK != retval)
		return retval;

	retval = target_register_timer_callback(&handle_target,
			100, 1, cmd_ctx->interp);
	if (ERROR_OK != retval)
		return retval;

787
788
789
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
790
int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
791
{
792
	struct target_event_callback **callbacks_p = &target_event_callbacks;
oharboe's avatar
oharboe committed
793

794
795
796
797
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
798

799
800
801
802
803
804
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
805

806
	(*callbacks_p) = malloc(sizeof(struct target_event_callback));
807
808
809
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
oharboe's avatar
oharboe committed
810

811
812
813
814
815
	return ERROR_OK;
}

int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
{
816
	struct target_timer_callback **callbacks_p = &target_timer_callbacks;
817
	struct timeval now;
oharboe's avatar
oharboe committed
818

819
820
821
822
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
823

824
825
826
827
828
829
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
830

831
	(*callbacks_p) = malloc(sizeof(struct target_timer_callback));
832
833
834
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->periodic = periodic;
	(*callbacks_p)->time_ms = time_ms;
oharboe's avatar
oharboe committed
835

836
837
838
839
840
841
842
843
844
	gettimeofday(&now, NULL);
	(*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
	time_ms -= (time_ms % 1000);
	(*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
	if ((*callbacks_p)->when.tv_usec > 1000000)
	{
		(*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
		(*callbacks_p)->when.tv_sec += 1;
	}
oharboe's avatar
oharboe committed
845

846
847
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
oharboe's avatar
oharboe committed
848

849
850
851
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
852
int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
853
{
854
855
	struct target_event_callback **p = &target_event_callbacks;
	struct target_event_callback *c = target_event_callbacks;
oharboe's avatar
oharboe committed
856

857
858
859
860
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
861

862
863
	while (c)
	{
864
		struct target_event_callback *next = c->next;
865
866
867
868
869
870
871
872
873
874
		if ((c->callback == callback) && (c->priv == priv))
		{
			*p = next;
			free(c);
			return ERROR_OK;
		}
		else
			p = &(c->next);
		c = next;
	}
oharboe's avatar
oharboe committed
875

876
877
878
879
880
	return ERROR_OK;
}

int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
{
881
882
	struct target_timer_callback **p = &target_timer_callbacks;
	struct target_timer_callback *c = target_timer_callbacks;
oharboe's avatar
oharboe committed
883

884
885
886
887
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
888

889
890
	while (c)
	{
891
		struct target_timer_callback *next = c->next;
892
893
894
895
896
897
898
899
900
901
		if ((c->callback == callback) && (c->priv == priv))
		{
			*p = next;
			free(c);
			return ERROR_OK;
		}
		else
			p = &(c->next);
		c = next;
	}
oharboe's avatar
oharboe committed
902

903
904
905
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
906
int target_call_event_callbacks(struct target *target, enum target_event event)
907
{
908
909
	struct target_event_callback *callback = target_event_callbacks;
	struct target_event_callback *next_callback;
oharboe's avatar
oharboe committed
910

911
912
913
	if (event == TARGET_EVENT_HALTED)
	{
		/* execute early halted first */
914
		target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
915
916
	}

917
918
	LOG_DEBUG("target event %i (%s)",
			  event,
919
			  Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
oharboe's avatar
oharboe committed
920

921
	target_handle_event(target, event);
922

923
924
925
926
927
928
	while (callback)
	{
		next_callback = callback->next;
		callback->callback(target, event, callback->priv);
		callback = next_callback;
	}
oharboe's avatar
oharboe committed
929

930
931
932
	return ERROR_OK;
}

933
static int target_timer_callback_periodic_restart(
934
		struct target_timer_callback *cb, struct timeval *now)
935
{
936
937
938
939
940
941
942
943
944
945
946
947
	int time_ms = cb->time_ms;
	cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
	time_ms -= (time_ms % 1000);
	cb->when.tv_sec = now->tv_sec + time_ms / 1000;
	if (cb->when.tv_usec > 1000000)
	{
		cb->when.tv_usec = cb->when.tv_usec - 1000000;
		cb->when.tv_sec += 1;
	}
	return ERROR_OK;
}

948
static int target_call_timer_callback(struct target_timer_callback *cb,
949
950
951
		struct timeval *now)
{
	cb->callback(cb->priv);
952

953
954
955
956
957
958
959
960
	if (cb->periodic)
		return target_timer_callback_periodic_restart(cb, now);

	return target_unregister_timer_callback(cb->callback, cb->priv);
}

static int target_call_timer_callbacks_check_time(int checktime)
{
oharboe's avatar
   
oharboe committed
961
962
	keep_alive();

963
	struct timeval now;
964
	gettimeofday(&now, NULL);
oharboe's avatar
oharboe committed
965

966
	struct target_timer_callback *callback = target_timer_callbacks;
967
968
	while (callback)
	{
969
		// cleaning up may unregister and free this callback
970
		struct target_timer_callback *next_callback = callback->next;
971
972
973
974
975
976

		bool call_it = callback->callback &&
			((!checktime && callback->periodic) ||
			  now.tv_sec > callback->when.tv_sec ||
			 (now.tv_sec == callback->when.tv_sec &&
			  now.tv_usec >= callback->when.tv_usec));
oharboe's avatar
oharboe committed
977

978
		if (call_it)
979
		{
980
981
982
			int retval = target_call_timer_callback(callback, &now);
			if (retval != ERROR_OK)
				return retval;
983
		}
oharboe's avatar
oharboe committed
984

985
986
		callback = next_callback;
	}
oharboe's avatar
oharboe committed
987

988
989
990
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
991
int target_call_timer_callbacks(void)
992
993
994
995
996
{
	return target_call_timer_callbacks_check_time(1);
}

/* invoke periodic callbacks immediately */
oharboe's avatar
oharboe committed
997
int target_call_timer_callbacks_now(void)
998
{