target.c 129 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
Øyvind Harboe's avatar
Øyvind Harboe committed
5
 *   Copyright (C) 2007-2010 Ø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
 *   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

36
37
#include <helper/time_support.h>
#include <jtag/jtag.h>
38
#include <flash/nor/core.h>
39

40
#include "target.h"
41
#include "target_type.h"
42
#include "target_request.h"
43
#include "breakpoints.h"
44
45
46
#include "register.h"
#include "trace.h"
#include "image.h"
47
48


49
50
51
52
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);
53
static int target_register_user_commands(struct command_context *cmd_ctx);
54

55
/* targets */
56
57
58
59
60
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;
61
extern struct target_type arm946e_target;
62
63
64
65
66
67
68
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;
69
extern struct target_type cortexa9_target;
70
71
72
extern struct target_type arm11_target;
extern struct target_type mips_m4k_target;
extern struct target_type avr_target;
73
extern struct target_type dsp563xx_target;
Zachary T Welch's avatar
Zachary T Welch committed
74
extern struct target_type testee_target;
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
75
extern struct target_type avr32_ap7k_target;
76

77
static struct target_type *target_types[] =
78
79
80
81
82
83
{
	&arm7tdmi_target,
	&arm9tdmi_target,
	&arm920t_target,
	&arm720t_target,
	&arm966e_target,
84
	&arm946e_target,
85
	&arm926ejs_target,
86
	&fa526_target,
87
	&feroceon_target,
88
	&dragonite_target,
89
90
	&xscale_target,
	&cortexm3_target,
91
	&cortexa8_target,
92
	&cortexa9_target,
oharboe's avatar
oharboe committed
93
	&arm11_target,
ntfreak's avatar
ntfreak committed
94
	&mips_m4k_target,
95
	&avr_target,
96
	&dsp563xx_target,
Zachary T Welch's avatar
Zachary T Welch committed
97
	&testee_target,
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
98
	&avr32_ap7k_target,
99
100
101
	NULL,
};

Zachary T Welch's avatar
Zachary T Welch committed
102
struct target *all_targets = NULL;
103
104
static struct target_event_callback *target_event_callbacks = NULL;
static struct target_timer_callback *target_timer_callbacks = NULL;
105
static const int polling_interval = 100;
106

107
static const Jim_Nvp nvp_assert[] = {
108
109
110
111
112
113
114
	{ .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 }
115
116
};

117
static const Jim_Nvp nvp_error_target[] = {
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	{ .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 }
};

132
static const char *target_strerror_safe(int err)
133
134
135
{
	const Jim_Nvp *n;

136
	n = Jim_Nvp_value2name_simple(nvp_error_target, err);
zwelch's avatar
zwelch committed
137
	if (n->name == NULL) {
138
139
140
141
142
143
		return "unknown";
	} else {
		return n->name;
	}
}

144
static const Jim_Nvp nvp_target_event[] = {
145
146
	{ .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
	{ .value = TARGET_EVENT_OLD_pre_resume         , .name = "old-pre_resume" },
147

148
	{ .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
149
150
151
152
153
	{ .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" },

154
155
156
	{ .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
	{ .name = "gdb-end", .value = TARGET_EVENT_GDB_END },

157
	/* historical name */
158

159
	{ .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
160
161

	{ .value = TARGET_EVENT_RESET_ASSERT_PRE,    .name = "reset-assert-pre" },
162
	{ .value = TARGET_EVENT_RESET_ASSERT,        .name = "reset-assert" },
163
164
165
166
167
168
169
	{ .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" },
170
171
	{ .value = TARGET_EVENT_RESET_INIT,          .name = "reset-init" },
	{ .value = TARGET_EVENT_RESET_END,           .name = "reset-end" },
172

173
	{ .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
174
	{ .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
175

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	{ .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 }
193
194
};

195
static const Jim_Nvp nvp_target_state[] = {
196
197
198
199
200
201
	{ .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 },
202
203
};

204
static const Jim_Nvp nvp_target_debug_reason [] = {
205
206
207
208
209
210
211
212
213
214
	{ .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 },
};

215
static const Jim_Nvp nvp_target_endian[] = {
216
217
218
	{ .name = "big",    .value = TARGET_BIG_ENDIAN },
	{ .name = "little", .value = TARGET_LITTLE_ENDIAN },
	{ .name = "be",     .value = TARGET_BIG_ENDIAN },
219
	{ .name = "le",     .value = TARGET_LITTLE_ENDIAN },
220
221
222
	{ .name = NULL,     .value = -1 },
};

223
static const Jim_Nvp nvp_reset_modes[] = {
224
225
226
227
228
229
230
	{ .name = "unknown", .value = RESET_UNKNOWN },
	{ .name = "run"    , .value = RESET_RUN },
	{ .name = "halt"   , .value = RESET_HALT },
	{ .name = "init"   , .value = RESET_INIT },
	{ .name = NULL     , .value = -1 },
};

231
232
233
234
235
236
237
238
239
240
241
242
243
const char *debug_reason_name(struct target *t)
{
	const char *cp;

	cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
			t->debug_reason)->name;
	if (!cp) {
		LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
		cp = "(*BUG*unknown*BUG*)";
	}
	return cp;
}

244
const char *
Zachary T Welch's avatar
Zachary T Welch committed
245
target_state_name( struct target *t )
246
247
248
249
250
251
252
253
254
255
{
	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;
}

256
/* determine the number of the new target */
257
static int new_target_number(void)
258
{
Zachary T Welch's avatar
Zachary T Welch committed
259
	struct target *t;
260
261
262
263
264
	int x;

	/* number is 0 based */
	x = -1;
	t = all_targets;
zwelch's avatar
zwelch committed
265
266
	while (t) {
		if (x < t->target_number) {
267
268
269
270
			x = t->target_number;
		}
		t = t->next;
	}
zwelch's avatar
zwelch committed
271
	return x + 1;
272
273
}

274
/* read a uint32_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
275
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
276
277
278
279
280
281
282
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u32(buffer);
	else
		return be_to_h_u32(buffer);
}

Mathias K's avatar
Mathias K committed
283
284
285
286
287
288
289
290
291
/* read a uint24_t from a buffer in target memory endianness */
uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u24(buffer);
	else
		return be_to_h_u24(buffer);
}

zwelch's avatar
zwelch committed
292
/* read a uint16_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
293
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
294
295
296
297
298
299
300
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u16(buffer);
	else
		return be_to_h_u16(buffer);
}

301
/* read a uint8_t from a buffer in target memory endianness */
302
static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
303
304
305
306
{
	return *buffer & 0x0ff;
}

307
/* write a uint32_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
308
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
309
310
311
312
313
314
315
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u32_to_le(buffer, value);
	else
		h_u32_to_be(buffer, value);
}

Mathias K's avatar
Mathias K committed
316
317
318
319
320
321
322
323
324
/* write a uint24_t to a buffer in target memory endianness */
void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u24_to_le(buffer, value);
	else
		h_u24_to_be(buffer, value);
}

zwelch's avatar
zwelch committed
325
/* write a uint16_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
326
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
327
328
329
330
331
332
333
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u16_to_le(buffer, value);
	else
		h_u16_to_be(buffer, value);
}

334
/* write a uint8_t to a buffer in target memory endianness */
335
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
336
337
338
339
{
	*buffer = value;
}

340
/* return a pointer to a configured target; id is name or number */
Zachary T Welch's avatar
Zachary T Welch committed
341
struct target *get_target(const char *id)
342
{
Zachary T Welch's avatar
Zachary T Welch committed
343
	struct target *target;
344
345
346
347
348
349
350
351
352

	/* 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;
	}

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

355
	/* no match, try as number */
356
357
	unsigned num;
	if (parse_uint(id, &num) != ERROR_OK)
358
359
360
		return NULL;

	for (target = all_targets; target; target = target->next) {
361
362
363
		if (target->target_number == (int)num) {
			LOG_WARNING("use '%s' as target identifier, not '%u'",
					target->cmd_name, num);
364
			return target;
365
		}
366
367
368
369
370
	}

	return NULL;
}

371
/* returns a pointer to the n-th configured target */
Zachary T Welch's avatar
Zachary T Welch committed
372
static struct target *get_target_by_num(int num)
373
{
Zachary T Welch's avatar
Zachary T Welch committed
374
	struct target *target = all_targets;
375

zwelch's avatar
zwelch committed
376
377
	while (target) {
		if (target->target_number == num) {
378
			return target;
379
		}
380
381
382
383
384
385
		target = target->next;
	}

	return NULL;
}

386
struct target* get_current_target(struct command_context *cmd_ctx)
387
{
Zachary T Welch's avatar
Zachary T Welch committed
388
	struct target *target = get_target_by_num(cmd_ctx->current_target);
oharboe's avatar
oharboe committed
389

390
391
	if (target == NULL)
	{
392
		LOG_ERROR("BUG: current_target out of bounds");
393
394
395
		exit(-1);
	}

oharboe's avatar
oharboe committed
396
	return target;
397
398
}

Zachary T Welch's avatar
Zachary T Welch committed
399
int target_poll(struct target *target)
400
{
401
402
	int retval;

403
	/* We can't poll until after examine */
404
	if (!target_was_examined(target))
405
406
407
408
	{
		/* Fail silently lest we pollute the log */
		return ERROR_FAIL;
	}
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

	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;
432
433
}

Zachary T Welch's avatar
Zachary T Welch committed
434
int target_halt(struct target *target)
435
{
436
	int retval;
437
	/* We can't poll until after examine */
438
	if (!target_was_examined(target))
439
440
441
442
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
443
444
445
446
447
448
449
450
451

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

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

	return ERROR_OK;
452
453
}

David Brownell's avatar
David Brownell committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
 * Make the target (re)start executing using its saved execution
 * context (possibly with some modifications).
 *
 * @param target Which target should start executing.
 * @param current True to use the target's saved program counter instead
 *	of the address parameter
 * @param address Optionally used as the program counter.
 * @param handle_breakpoints True iff breakpoints at the resumption PC
 *	should be skipped.  (For example, maybe execution was stopped by
 *	such a breakpoint, in which case it would be counterprodutive to
 *	let it re-trigger.
 * @param debug_execution False if all working areas allocated by OpenOCD
 *	should be released and/or restored to their original contents.
 *	(This would for example be true to run some downloaded "helper"
 *	algorithm code, which resides in one such working buffer and uses
 *	another for data storage.)
 *
 * @todo Resolve the ambiguity about what the "debug_execution" flag
 * signifies.  For example, Target implementations don't agree on how
 * it relates to invalidation of the register cache, or to whether
 * breakpoints and watchpoints should be enabled.  (It would seem wrong
 * to enable breakpoints when running downloaded "helper" algorithms
 * (debug_execution true), since the breakpoints would be set to match
 * target firmware being debugged, not the helper algorithm.... and
 * enabling them could cause such helpers to malfunction (for example,
 * by overwriting data with a breakpoint instruction.  On the other
 * hand the infrastructure for running such helpers might use this
 * procedure but rely on hardware breakpoint to detect termination.)
 */
Zachary T Welch's avatar
Zachary T Welch committed
484
int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
485
{
486
	int retval;
oharboe's avatar
oharboe committed
487

488
	/* We can't poll until after examine */
489
	if (!target_was_examined(target))
490
491
492
493
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
494

David Brownell's avatar
David Brownell committed
495
496
497
	/* 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.
oharboe's avatar
oharboe committed
498
	 */
499
500
	if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
		return retval;
oharboe's avatar
oharboe committed
501

502
	return retval;
503
504
}

505
static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
506
507
{
	char buf[100];
508
	int retval;
509
	Jim_Nvp *n;
510
	n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
zwelch's avatar
zwelch committed
511
	if (n->name == NULL) {
512
513
514
515
		LOG_ERROR("invalid reset mode");
		return ERROR_FAIL;
	}

516
517
518
519
	/* 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.
	 */
520
521
522
	bool save_poll = jtag_poll_get_enabled();

	jtag_poll_set_enabled(false);
523

524
	sprintf(buf, "ocd_process_reset %s", n->name);
525
	retval = Jim_Eval(cmd_ctx->interp, buf);
526

527
	jtag_poll_set_enabled(save_poll);
528

zwelch's avatar
zwelch committed
529
	if (retval != JIM_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
530
531
		Jim_MakeErrorMessage(cmd_ctx->interp);
		command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
532
533
		return ERROR_FAIL;
	}
534
535

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

Øyvind Harboe's avatar
Øyvind Harboe committed
538
539
540
541
542
	struct target *target;
	for (target = all_targets; target; target = target->next) {
		target->type->check_reset(target);
	}

543
	return retval;
544
545
}

Zachary T Welch's avatar
Zachary T Welch committed
546
static int identity_virt2phys(struct target *target,
David Brownell's avatar
David Brownell committed
547
		uint32_t virtual, uint32_t *physical)
548
549
550
551
552
{
	*physical = virtual;
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
553
static int no_mmu(struct target *target, int *enabled)
554
{
555
	*enabled = 0;
556
557
558
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
559
static int default_examine(struct target *target)
560
{
561
	target_set_examined(target);
562
563
564
	return ERROR_OK;
}

Øyvind Harboe's avatar
Øyvind Harboe committed
565
566
567
568
569
570
/* no check by default */
static int default_check_reset(struct target *target)
{
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
571
int target_examine_one(struct target *target)
zwelch's avatar
zwelch committed
572
573
574
575
{
	return target->type->examine(target);
}

zwelch's avatar
zwelch committed
576
577
static int jtag_enable_callback(enum jtag_event event, void *priv)
{
Zachary T Welch's avatar
Zachary T Welch committed
578
	struct target *target = priv;
zwelch's avatar
zwelch committed
579
580
581
582
583
584
585
586
587

	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
588
/* Targets that correctly implement init + examine, i.e.
589
 * no communication with target during init:
oharboe's avatar
oharboe committed
590
591
 *
 * XScale
592
 */
ntfreak's avatar
ntfreak committed
593
int target_examine(void)
594
595
{
	int retval = ERROR_OK;
Zachary T Welch's avatar
Zachary T Welch committed
596
	struct target *target;
zwelch's avatar
zwelch committed
597
598

	for (target = all_targets; target; target = target->next)
599
	{
zwelch's avatar
zwelch committed
600
601
602
603
		/* defer examination, but don't skip it */
		if (!target->tap->enabled) {
			jtag_register_event_callback(jtag_enable_callback,
					target);
zwelch's avatar
zwelch committed
604
			continue;
zwelch's avatar
zwelch committed
605
		}
zwelch's avatar
zwelch committed
606
		if ((retval = target_examine_one(target)) != ERROR_OK)
607
608
609
610
			return retval;
	}
	return retval;
}
611
const char *target_type_name(struct target *target)
zwelch's avatar
zwelch committed
612
613
614
{
	return target->type->name;
}
615

Zachary T Welch's avatar
Zachary T Welch committed
616
static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
617
{
618
	if (!target_was_examined(target))
619
620
621
622
623
624
625
	{
		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
626
static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
627
{
628
	if (!target_was_examined(target))
629
630
631
632
633
634
635
	{
		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
636
static int target_soft_reset_halt_imp(struct target *target)
637
{
638
	if (!target_was_examined(target))
639
640
641
642
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
643
644
	if (!target->type->soft_reset_halt_imp) {
		LOG_ERROR("Target %s does not support soft_reset_halt",
645
				target_name(target));
646
647
		return ERROR_FAIL;
	}
648
649
650
	return target->type->soft_reset_halt_imp(target);
}

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**
 * Downloads a target-specific native code algorithm to the target,
 * and executes it.  * Note that some targets may need to set up, enable,
 * and tear down a breakpoint (hard or * soft) to detect algorithm
 * termination, while others may support  lower overhead schemes where
 * soft breakpoints embedded in the algorithm automatically terminate the
 * algorithm.
 *
 * @param target used to run the algorithm
 * @param arch_info target-specific description of the algorithm.
 */
int target_run_algorithm(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)
667
{
668
669
	int retval = ERROR_FAIL;

670
	if (!target_was_examined(target))
671
672
	{
		LOG_ERROR("Target not examined yet");
673
		goto done;
674
	}
David Brownell's avatar
David Brownell committed
675
	if (!target->type->run_algorithm) {
676
677
678
679
680
		LOG_ERROR("Target type '%s' does not support %s",
				target_type_name(target), __func__);
		goto done;
	}

681
	target->running_alg = true;
682
683
684
685
	retval = target->type->run_algorithm(target,
			num_mem_params, mem_params,
			num_reg_params, reg_param,
			entry_point, exit_point, timeout_ms, arch_info);
686
	target->running_alg = false;
687
688
689

done:
	return retval;
690
}
691

692

Zachary T Welch's avatar
Zachary T Welch committed
693
int target_read_memory(struct target *target,
694
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
695
696
697
698
{
	return target->type->read_memory(target, address, size, count, buffer);
}

699
static int target_read_phys_memory(struct target *target,
700
701
702
703
704
		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
705
int target_write_memory(struct target *target,
706
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
707
708
709
{
	return target->type->write_memory(target, address, size, count, buffer);
}
710

711
static int target_write_phys_memory(struct target *target,
712
713
714
715
716
		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
717
int target_bulk_write_memory(struct target *target,
718
		uint32_t address, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
719
720
721
722
{
	return target->type->bulk_write_memory(target, address, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
723
int target_add_breakpoint(struct target *target,
724
		struct breakpoint *breakpoint)
725
{
726
727
728
729
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
730
731
	return target->type->add_breakpoint(target, breakpoint);
}
Zachary T Welch's avatar
Zachary T Welch committed
732
int target_remove_breakpoint(struct target *target,
733
		struct breakpoint *breakpoint)
734
735
736
737
{
	return target->type->remove_breakpoint(target, breakpoint);
}

Zachary T Welch's avatar
Zachary T Welch committed
738
int target_add_watchpoint(struct target *target,
739
		struct watchpoint *watchpoint)
740
{
741
742
743
744
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
745
746
	return target->type->add_watchpoint(target, watchpoint);
}
Zachary T Welch's avatar
Zachary T Welch committed
747
int target_remove_watchpoint(struct target *target,
748
		struct watchpoint *watchpoint)
749
750
751
{
	return target->type->remove_watchpoint(target, watchpoint);
}
zwelch's avatar
zwelch committed
752

Zachary T Welch's avatar
Zachary T Welch committed
753
int target_get_gdb_reg_list(struct target *target,
Zachary T Welch's avatar
Zachary T Welch committed
754
		struct reg **reg_list[], int *reg_list_size)
zwelch's avatar
zwelch committed
755
756
757
{
	return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
}
Zachary T Welch's avatar
Zachary T Welch committed
758
int target_step(struct target *target,
759
		int current, uint32_t address, int handle_breakpoints)
zwelch's avatar
zwelch committed
760
761
762
763
{
	return target->type->step(target, current, address, handle_breakpoints);
}

zwelch's avatar
zwelch committed
764

765
766
767
768
769
/**
 * Reset the @c examined flag for the given target.
 * Pure paranoia -- targets are zeroed on allocation.
 */
static void target_reset_examined(struct target *target)
770
{
771
	target->examined = false;
772
773
}

774
static int
Zachary T Welch's avatar
Zachary T Welch committed
775
err_read_phys_memory(struct target *target, uint32_t address,
776
		uint32_t size, uint32_t count, uint8_t *buffer)
777
{
778
	LOG_ERROR("Not implemented: %s", __func__);
779
	return ERROR_FAIL;
780
781
}

782
static int
Zachary T Welch's avatar
Zachary T Welch committed
783
err_write_phys_memory(struct target *target, uint32_t address,
784
		uint32_t size, uint32_t count, uint8_t *buffer)
785
{
786
	LOG_ERROR("Not implemented: %s", __func__);
787
	return ERROR_FAIL;
788
789
}

790
791
static int handle_target(void *priv);

792
793
static int target_init_one(struct command_context *cmd_ctx,
		struct target *target)
794
{
795
	target_reset_examined(target);
oharboe's avatar
oharboe committed
796

797
798
799
	struct target_type *type = target->type;
	if (type->examine == NULL)
		type->examine = default_examine;
800

Øyvind Harboe's avatar
Øyvind Harboe committed
801
802
803
	if (type->check_reset== NULL)
		type->check_reset = default_check_reset;

804
805
806
807
808
809
	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
810

811
812
813
814
815
816
817
818
819
820
	/**
	 * @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
821

822
823
	type->read_memory_imp = target->type->read_memory;
	type->read_memory = target_read_memory_imp;
824

825
826
	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
827

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
	/* 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
	{
853
854
855
856
		/* 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.
		 */
857
858
859
860
		if (type->write_phys_memory || type->read_phys_memory
				|| type->virt2phys)
		{
			LOG_WARNING("type '%s' has bad MMU hooks", type->name);
861
		}
862
863
864
865
866

		type->mmu = no_mmu;
		type->write_phys_memory = type->write_memory;
		type->read_phys_memory = type->read_memory;
		type->virt2phys = identity_virt2phys;
867
	}
868
869
	return ERROR_OK;
}
oharboe's avatar
oharboe committed
870

871
static int target_init(struct command_context *cmd_ctx)
872
873
874
875
876
{
	struct target *target;
	int retval;

	for (target = all_targets; target; target = target->next)
877
	{
878
879
		retval = target_init_one(cmd_ctx, target);
		if (ERROR_OK != retval)
880
			return retval;
881
	}
oharboe's avatar
oharboe committed
882

883
884
885
886
887
888
889
890
	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,
891
			polling_interval, 1, cmd_ctx->interp);
892
893
894
	if (ERROR_OK != retval)
		return retval;

895
896
897
	return ERROR_OK;
}

898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
COMMAND_HANDLER(handle_target_init_command)
{
	if (CMD_ARGC != 0)
		return ERROR_COMMAND_SYNTAX_ERROR;

	static bool target_initialized = false;
	if (target_initialized)
	{
		LOG_INFO("'target init' has already been called");
		return ERROR_OK;
	}
	target_initialized = true;

	LOG_DEBUG("Initializing targets...");
	return target_init(CMD_CTX);
}

Zachary T Welch's avatar
Zachary T Welch committed
915
int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
916
{
917
	struct target_event_callback **callbacks_p = &target_event_callbacks;
oharboe's avatar
oharboe committed
918

919
920
921
922
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
923

924
925
926
927
928
929
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
930

931
	(*callbacks_p) = malloc(sizeof(struct target_event_callback));
932
933
934
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
oharboe's avatar
oharboe committed
935

936
937
938
939
940
	return ERROR_OK;
}

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

944
945
946
947
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
948

949
950
951
952
953
954
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
955

956
	(*callbacks_p) = malloc(sizeof(struct target_timer_callback));
957
958
959
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->periodic = periodic;
	(*callbacks_p)->time_ms = time_ms;
oharboe's avatar
oharboe committed
960

961
962
963
964
965
966
967
968
969
	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
970

971
972
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
oharboe's avatar
oharboe committed
973

974
975
976
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
977
int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
978
{
979
980
	struct target_event_callback **p = &target_event_callbacks;
	struct target_event_callback *c = target_event_callbacks;
oharboe's avatar
oharboe committed
981

982
983
984
985
	if (callback == NULL)
	{
		return ERROR_INVALID_ARGUMENTS;
	}
oharboe's avatar
oharboe committed
986

987
988
	while (c)
	{
989
		struct target_event_callback *next = c->next;
990
991
992
993
994
995
996
997
998
999
		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
1000