target.c 140 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
 *   Copyright (C) 2011 by Broadcom Corporation                            *
 *   Evan Hunter - ehunter@broadcom.com                                    *
 *                                                                         *
20
21
22
 *   Copyright (C) ST-Ericsson SA 2011                                     *
 *   michel.jaouen@stericsson.com : smp minimum support                    *
 *                                                                         *
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 *   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

42
43
#include <helper/time_support.h>
#include <jtag/jtag.h>
44
#include <flash/nor/core.h>
45

46
#include "target.h"
47
#include "target_type.h"
48
#include "target_request.h"
49
#include "breakpoints.h"
50
51
52
#include "register.h"
#include "trace.h"
#include "image.h"
53
#include "rtos/rtos.h"
54
55


56
57
58
static int target_read_buffer_default(struct target *target, uint32_t address,
		uint32_t size, uint8_t *buffer);
static int target_write_buffer_default(struct target *target, uint32_t address,
59
		uint32_t size, const uint8_t *buffer);
60
61
62
63
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);
64
static int target_register_user_commands(struct command_context *cmd_ctx);
65

66
/* targets */
67
68
69
70
71
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;
72
extern struct target_type arm946e_target;
73
74
75
76
77
78
79
80
81
82
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;
83
extern struct target_type dsp563xx_target;
84
extern struct target_type dsp5680xx_target;
Zachary T Welch's avatar
Zachary T Welch committed
85
extern struct target_type testee_target;
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
86
extern struct target_type avr32_ap7k_target;
87
extern struct target_type stm32_stlink_target;
88

89
static struct target_type *target_types[] =
90
91
92
93
94
95
{
	&arm7tdmi_target,
	&arm9tdmi_target,
	&arm920t_target,
	&arm720t_target,
	&arm966e_target,
96
	&arm946e_target,
97
	&arm926ejs_target,
98
	&fa526_target,
99
	&feroceon_target,
100
	&dragonite_target,
101
102
	&xscale_target,
	&cortexm3_target,
103
	&cortexa8_target,
oharboe's avatar
oharboe committed
104
	&arm11_target,
ntfreak's avatar
ntfreak committed
105
	&mips_m4k_target,
106
	&avr_target,
107
	&dsp563xx_target,
108
	&dsp5680xx_target,
Zachary T Welch's avatar
Zachary T Welch committed
109
	&testee_target,
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
110
	&avr32_ap7k_target,
111
	&stm32_stlink_target,
112
113
114
	NULL,
};

Zachary T Welch's avatar
Zachary T Welch committed
115
struct target *all_targets = NULL;
116
117
static struct target_event_callback *target_event_callbacks = NULL;
static struct target_timer_callback *target_timer_callbacks = NULL;
118
static const int polling_interval = 100;
119

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

130
static const Jim_Nvp nvp_error_target[] = {
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	{ .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 }
};

145
static const char *target_strerror_safe(int err)
146
147
148
{
	const Jim_Nvp *n;

149
	n = Jim_Nvp_value2name_simple(nvp_error_target, err);
zwelch's avatar
zwelch committed
150
	if (n->name == NULL) {
151
152
153
154
155
156
		return "unknown";
	} else {
		return n->name;
	}
}

157
static const Jim_Nvp nvp_target_event[] = {
158
159
	{ .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
	{ .value = TARGET_EVENT_OLD_pre_resume         , .name = "old-pre_resume" },
160

161
	{ .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
162
163
164
165
166
	{ .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" },

167
168
169
	{ .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
	{ .name = "gdb-end", .value = TARGET_EVENT_GDB_END },

170
	/* historical name */
171

172
	{ .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
173
174

	{ .value = TARGET_EVENT_RESET_ASSERT_PRE,    .name = "reset-assert-pre" },
175
	{ .value = TARGET_EVENT_RESET_ASSERT,        .name = "reset-assert" },
176
177
178
179
180
181
182
	{ .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" },
183
184
	{ .value = TARGET_EVENT_RESET_INIT,          .name = "reset-init" },
	{ .value = TARGET_EVENT_RESET_END,           .name = "reset-end" },
185

186
	{ .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
187
	{ .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
188

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	{ .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 }
206
207
};

208
static const Jim_Nvp nvp_target_state[] = {
209
210
211
212
213
214
	{ .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 },
215
216
};

217
static const Jim_Nvp nvp_target_debug_reason [] = {
218
219
220
221
222
223
224
225
226
227
	{ .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 },
};

228
static const Jim_Nvp nvp_target_endian[] = {
229
230
231
	{ .name = "big",    .value = TARGET_BIG_ENDIAN },
	{ .name = "little", .value = TARGET_LITTLE_ENDIAN },
	{ .name = "be",     .value = TARGET_BIG_ENDIAN },
232
	{ .name = "le",     .value = TARGET_LITTLE_ENDIAN },
233
234
235
	{ .name = NULL,     .value = -1 },
};

236
static const Jim_Nvp nvp_reset_modes[] = {
237
238
239
240
241
242
243
	{ .name = "unknown", .value = RESET_UNKNOWN },
	{ .name = "run"    , .value = RESET_RUN },
	{ .name = "halt"   , .value = RESET_HALT },
	{ .name = "init"   , .value = RESET_INIT },
	{ .name = NULL     , .value = -1 },
};

244
245
246
247
248
249
250
251
252
253
254
255
256
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;
}

257
const char *
Zachary T Welch's avatar
Zachary T Welch committed
258
target_state_name( struct target *t )
259
260
261
262
263
264
265
266
267
268
{
	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;
}

269
/* determine the number of the new target */
270
static int new_target_number(void)
271
{
Zachary T Welch's avatar
Zachary T Welch committed
272
	struct target *t;
273
274
275
276
277
	int x;

	/* number is 0 based */
	x = -1;
	t = all_targets;
zwelch's avatar
zwelch committed
278
279
	while (t) {
		if (x < t->target_number) {
280
281
282
283
			x = t->target_number;
		}
		t = t->next;
	}
zwelch's avatar
zwelch committed
284
	return x + 1;
285
286
}

287
/* read a uint32_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
288
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
289
290
291
292
293
294
295
{
	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
296
297
298
299
300
301
302
303
304
/* 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
305
/* read a uint16_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
306
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
307
308
309
310
311
312
313
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u16(buffer);
	else
		return be_to_h_u16(buffer);
}

314
/* read a uint8_t from a buffer in target memory endianness */
315
static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
316
317
318
319
{
	return *buffer & 0x0ff;
}

320
/* write a uint32_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
321
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
322
323
324
325
326
327
328
{
	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
329
330
331
332
333
334
335
336
337
/* 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
338
/* write a uint16_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
339
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
340
341
342
343
344
345
346
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u16_to_le(buffer, value);
	else
		h_u16_to_be(buffer, value);
}

347
/* write a uint8_t to a buffer in target memory endianness */
348
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
349
350
351
352
{
	*buffer = value;
}

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* write a uint32_t array to a buffer in target memory endianness */
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
{
	uint32_t i;
	for(i = 0; i < count; i ++)
		dstbuf[i] = target_buffer_get_u32(target,&buffer[i*4]);
}

/* write a uint16_t array to a buffer in target memory endianness */
void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
{
	uint32_t i;
	for(i = 0; i < count; i ++)
		dstbuf[i] = target_buffer_get_u16(target,&buffer[i*2]);
}

/* write a uint32_t array to a buffer in target memory endianness */
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
{
	uint32_t i;
	for(i = 0; i < count; i ++)
		target_buffer_set_u32(target,&buffer[i*4],srcbuf[i]);
}

/* write a uint16_t array to a buffer in target memory endianness */
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
{
	uint32_t i;
	for(i = 0; i < count; i ++)
		target_buffer_set_u16(target,&buffer[i*2],srcbuf[i]);
}

385
/* return a pointer to a configured target; id is name or number */
Zachary T Welch's avatar
Zachary T Welch committed
386
struct target *get_target(const char *id)
387
{
Zachary T Welch's avatar
Zachary T Welch committed
388
	struct target *target;
389
390
391
392
393
394
395
396
397

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

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

400
	/* no match, try as number */
401
402
	unsigned num;
	if (parse_uint(id, &num) != ERROR_OK)
403
404
405
		return NULL;

	for (target = all_targets; target; target = target->next) {
406
407
408
		if (target->target_number == (int)num) {
			LOG_WARNING("use '%s' as target identifier, not '%u'",
					target->cmd_name, num);
409
			return target;
410
		}
411
412
413
414
415
	}

	return NULL;
}

416
/* returns a pointer to the n-th configured target */
Zachary T Welch's avatar
Zachary T Welch committed
417
static struct target *get_target_by_num(int num)
418
{
Zachary T Welch's avatar
Zachary T Welch committed
419
	struct target *target = all_targets;
420

zwelch's avatar
zwelch committed
421
422
	while (target) {
		if (target->target_number == num) {
423
			return target;
424
		}
425
426
427
428
429
430
		target = target->next;
	}

	return NULL;
}

431
struct target* get_current_target(struct command_context *cmd_ctx)
432
{
Zachary T Welch's avatar
Zachary T Welch committed
433
	struct target *target = get_target_by_num(cmd_ctx->current_target);
oharboe's avatar
oharboe committed
434

435
436
	if (target == NULL)
	{
437
		LOG_ERROR("BUG: current_target out of bounds");
438
439
440
		exit(-1);
	}

oharboe's avatar
oharboe committed
441
	return target;
442
443
}

Zachary T Welch's avatar
Zachary T Welch committed
444
int target_poll(struct target *target)
445
{
446
447
	int retval;

448
	/* We can't poll until after examine */
449
	if (!target_was_examined(target))
450
451
452
453
	{
		/* Fail silently lest we pollute the log */
		return ERROR_FAIL;
	}
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

	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;
477
478
}

Zachary T Welch's avatar
Zachary T Welch committed
479
int target_halt(struct target *target)
480
{
481
	int retval;
482
	/* We can't poll until after examine */
483
	if (!target_was_examined(target))
484
485
486
487
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
488
489
490
491
492
493
494
495
496

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

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

	return ERROR_OK;
497
498
}

David Brownell's avatar
David Brownell committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
 * 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
529
int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
530
{
531
	int retval;
oharboe's avatar
oharboe committed
532

533
	/* We can't poll until after examine */
534
	if (!target_was_examined(target))
535
536
537
538
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
539

David Brownell's avatar
David Brownell committed
540
541
542
	/* 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
543
	 */
544
545
	if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
		return retval;
oharboe's avatar
oharboe committed
546

547
	return retval;
548
549
}

550
static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
551
552
{
	char buf[100];
553
	int retval;
554
	Jim_Nvp *n;
555
	n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
zwelch's avatar
zwelch committed
556
	if (n->name == NULL) {
557
558
559
560
		LOG_ERROR("invalid reset mode");
		return ERROR_FAIL;
	}

561
562
563
564
	/* 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.
	 */
565
566
567
	bool save_poll = jtag_poll_get_enabled();

	jtag_poll_set_enabled(false);
568

569
	sprintf(buf, "ocd_process_reset %s", n->name);
570
	retval = Jim_Eval(cmd_ctx->interp, buf);
571

572
	jtag_poll_set_enabled(save_poll);
573

zwelch's avatar
zwelch committed
574
	if (retval != JIM_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
575
576
		Jim_MakeErrorMessage(cmd_ctx->interp);
		command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
577
578
		return ERROR_FAIL;
	}
579
580

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

Øyvind Harboe's avatar
Øyvind Harboe committed
583
584
585
586
587
	struct target *target;
	for (target = all_targets; target; target = target->next) {
		target->type->check_reset(target);
	}

588
	return retval;
589
590
}

Zachary T Welch's avatar
Zachary T Welch committed
591
static int identity_virt2phys(struct target *target,
David Brownell's avatar
David Brownell committed
592
		uint32_t virtual, uint32_t *physical)
593
594
595
596
597
{
	*physical = virtual;
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
598
static int no_mmu(struct target *target, int *enabled)
599
{
600
	*enabled = 0;
601
602
603
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
604
static int default_examine(struct target *target)
605
{
606
	target_set_examined(target);
607
608
609
	return ERROR_OK;
}

Øyvind Harboe's avatar
Øyvind Harboe committed
610
611
612
613
614
615
/* no check by default */
static int default_check_reset(struct target *target)
{
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
616
int target_examine_one(struct target *target)
zwelch's avatar
zwelch committed
617
618
619
620
{
	return target->type->examine(target);
}

zwelch's avatar
zwelch committed
621
622
static int jtag_enable_callback(enum jtag_event event, void *priv)
{
Zachary T Welch's avatar
Zachary T Welch committed
623
	struct target *target = priv;
zwelch's avatar
zwelch committed
624
625
626
627
628
629
630
631
632

	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
633
/* Targets that correctly implement init + examine, i.e.
634
 * no communication with target during init:
oharboe's avatar
oharboe committed
635
636
 *
 * XScale
637
 */
ntfreak's avatar
ntfreak committed
638
int target_examine(void)
639
640
{
	int retval = ERROR_OK;
Zachary T Welch's avatar
Zachary T Welch committed
641
	struct target *target;
zwelch's avatar
zwelch committed
642
643

	for (target = all_targets; target; target = target->next)
644
	{
zwelch's avatar
zwelch committed
645
646
647
648
		/* defer examination, but don't skip it */
		if (!target->tap->enabled) {
			jtag_register_event_callback(jtag_enable_callback,
					target);
zwelch's avatar
zwelch committed
649
			continue;
zwelch's avatar
zwelch committed
650
		}
zwelch's avatar
zwelch committed
651
		if ((retval = target_examine_one(target)) != ERROR_OK)
652
653
654
655
			return retval;
	}
	return retval;
}
656
const char *target_type_name(struct target *target)
zwelch's avatar
zwelch committed
657
658
659
{
	return target->type->name;
}
660

661
static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
662
{
663
	if (!target_was_examined(target))
664
665
666
667
668
669
670
	{
		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
671
static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
672
{
673
	if (!target_was_examined(target))
674
675
676
677
678
679
680
	{
		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
681
static int target_soft_reset_halt_imp(struct target *target)
682
{
683
	if (!target_was_examined(target))
684
685
686
687
	{
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
688
689
	if (!target->type->soft_reset_halt_imp) {
		LOG_ERROR("Target %s does not support soft_reset_halt",
690
				target_name(target));
691
692
		return ERROR_FAIL;
	}
693
694
695
	return target->type->soft_reset_halt_imp(target);
}

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/**
 * 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)
712
{
713
714
	int retval = ERROR_FAIL;

715
	if (!target_was_examined(target))
716
717
	{
		LOG_ERROR("Target not examined yet");
718
		goto done;
719
	}
David Brownell's avatar
David Brownell committed
720
	if (!target->type->run_algorithm) {
721
722
723
724
725
		LOG_ERROR("Target type '%s' does not support %s",
				target_type_name(target), __func__);
		goto done;
	}

726
	target->running_alg = true;
727
728
729
730
	retval = target->type->run_algorithm(target,
			num_mem_params, mem_params,
			num_reg_params, reg_param,
			entry_point, exit_point, timeout_ms, arch_info);
731
	target->running_alg = false;
732
733
734

done:
	return retval;
735
}
736

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/**
 * Downloads a target-specific native code algorithm to the target,
 * executes and leaves it running.
 *
 * @param target used to run the algorithm
 * @param arch_info target-specific description of the algorithm.
 */
int target_start_algorithm(struct target *target,
		int num_mem_params, struct mem_param *mem_params,
		int num_reg_params, struct reg_param *reg_params,
		uint32_t entry_point, uint32_t exit_point,
		void *arch_info)
{
	int retval = ERROR_FAIL;

	if (!target_was_examined(target))
	{
		LOG_ERROR("Target not examined yet");
		goto done;
	}
	if (!target->type->start_algorithm) {
		LOG_ERROR("Target type '%s' does not support %s",
				target_type_name(target), __func__);
		goto done;
	}
	if (target->running_alg) {
		LOG_ERROR("Target is already running an algorithm");
		goto done;
	}

	target->running_alg = true;
	retval = target->type->start_algorithm(target,
			num_mem_params, mem_params,
			num_reg_params, reg_params,
			entry_point, exit_point, arch_info);

done:
	return retval;
}

/**
 * Waits for an algorithm started with target_start_algorithm() to complete.
 *
 * @param target used to run the algorithm
 * @param arch_info target-specific description of the algorithm.
 */
int target_wait_algorithm(struct target *target,
		int num_mem_params, struct mem_param *mem_params,
		int num_reg_params, struct reg_param *reg_params,
		uint32_t exit_point, int timeout_ms,
		void *arch_info)
{
	int retval = ERROR_FAIL;

	if (!target->type->wait_algorithm) {
		LOG_ERROR("Target type '%s' does not support %s",
				target_type_name(target), __func__);
		goto done;
	}
	if (!target->running_alg) {
		LOG_ERROR("Target is not running an algorithm");
		goto done;
	}

	retval = target->type->wait_algorithm(target,
			num_mem_params, mem_params,
			num_reg_params, reg_params,
			exit_point, timeout_ms, arch_info);
	if (retval != ERROR_TARGET_TIMEOUT)
		target->running_alg = false;

done:
	return retval;
}

812

Zachary T Welch's avatar
Zachary T Welch committed
813
int target_read_memory(struct target *target,
814
		uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
zwelch's avatar
zwelch committed
815
816
817
818
{
	return target->type->read_memory(target, address, size, count, buffer);
}

819
static int target_read_phys_memory(struct target *target,
820
821
822
823
824
		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
825
int target_write_memory(struct target *target,
826
		uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
zwelch's avatar
zwelch committed
827
828
829
{
	return target->type->write_memory(target, address, size, count, buffer);
}
830

831
static int target_write_phys_memory(struct target *target,
832
		uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
833
834
835
836
{
	return target->type->write_phys_memory(target, address, size, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
837
int target_bulk_write_memory(struct target *target,
838
		uint32_t address, uint32_t count, const uint8_t *buffer)
zwelch's avatar
zwelch committed
839
840
841
842
{
	return target->type->bulk_write_memory(target, address, count, buffer);
}

Zachary T Welch's avatar
Zachary T Welch committed
843
int target_add_breakpoint(struct target *target,
844
		struct breakpoint *breakpoint)
845
{
846
	if ((target->state != TARGET_HALTED)&&(breakpoint->type!=BKPT_HARD)) {
847
848
849
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
850
851
	return target->type->add_breakpoint(target, breakpoint);
}
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

int target_add_context_breakpoint(struct target *target,
		struct breakpoint *breakpoint)
{
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
	return target->type->add_context_breakpoint(target, breakpoint);
}

int target_add_hybrid_breakpoint(struct target *target,
		struct breakpoint *breakpoint)
{
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
	return target->type->add_hybrid_breakpoint(target, breakpoint);
}

Zachary T Welch's avatar
Zachary T Welch committed
873
int target_remove_breakpoint(struct target *target,
874
		struct breakpoint *breakpoint)
875
876
877
878
{
	return target->type->remove_breakpoint(target, breakpoint);
}

Zachary T Welch's avatar
Zachary T Welch committed
879
int target_add_watchpoint(struct target *target,
880
		struct watchpoint *watchpoint)
881
{
882
883
884
885
	if (target->state != TARGET_HALTED) {
		LOG_WARNING("target %s is not halted", target->cmd_name);
		return ERROR_TARGET_NOT_HALTED;
	}
886
887
	return target->type->add_watchpoint(target, watchpoint);
}
Zachary T Welch's avatar
Zachary T Welch committed
888
int target_remove_watchpoint(struct target *target,
889
		struct watchpoint *watchpoint)
890
891
892
{
	return target->type->remove_watchpoint(target, watchpoint);
}
zwelch's avatar
zwelch committed
893

Zachary T Welch's avatar
Zachary T Welch committed
894
int target_get_gdb_reg_list(struct target *target,
Zachary T Welch's avatar
Zachary T Welch committed
895
		struct reg **reg_list[], int *reg_list_size)
zwelch's avatar
zwelch committed
896
897
898
{
	return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
}
Zachary T Welch's avatar
Zachary T Welch committed
899
int target_step(struct target *target,
900
		int current, uint32_t address, int handle_breakpoints)
zwelch's avatar
zwelch committed
901
902
903
904
{
	return target->type->step(target, current, address, handle_breakpoints);
}

zwelch's avatar
zwelch committed
905

906
907
908
909
910
/**
 * Reset the @c examined flag for the given target.
 * Pure paranoia -- targets are zeroed on allocation.
 */
static void target_reset_examined(struct target *target)
911
{
912
	target->examined = false;
913
914
}

915
static int
Zachary T Welch's avatar
Zachary T Welch committed
916
err_read_phys_memory(struct target *target, uint32_t address,
917
		uint32_t size, uint32_t count, uint8_t *buffer)
918
{
919
	LOG_ERROR("Not implemented: %s", __func__);
920
	return ERROR_FAIL;
921
922
}

923
static int
Zachary T Welch's avatar
Zachary T Welch committed
924
err_write_phys_memory(struct target *target, uint32_t address,
925
		uint32_t size, uint32_t count, const uint8_t *buffer)
926
{
927
	LOG_ERROR("Not implemented: %s", __func__);
928
	return ERROR_FAIL;
929
930
}

931
932
static int handle_target(void *priv);

933
934
static int target_init_one(struct command_context *cmd_ctx,
		struct target *target)
935
{
936
	target_reset_examined(target);
oharboe's avatar
oharboe committed
937

938
939
940
	struct target_type *type = target->type;
	if (type->examine == NULL)
		type->examine = default_examine;
941

Øyvind Harboe's avatar
Øyvind Harboe committed
942
943
944
	if (type->check_reset== NULL)
		type->check_reset = default_check_reset;

Mathias K's avatar
Mathias K committed
945
946
	assert(type->init_target != NULL);

947
948
949
950
951
952
	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
953

954
955
956
957
958
959
960
961
962
963
	/**
	 * @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
964

965
966
	type->read_memory_imp = target->type->read_memory;
	type->read_memory = target_read_memory_imp;
967

968
969
	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
970

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
	/* 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
	{
996
997
998
999
		/* 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.
		 */
1000
1001
1002
1003
		if (type->write_phys_memory || type->read_phys_memory
				|| type->virt2phys)
		{
			LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1004
		}
1005
1006
1007
1008
1009

		type->mmu = no_mmu;
		type->write_phys_memory = type->write_memory;
		type->read_phys_memory = type->read_memory;
		type->virt2phys = identity_virt2phys;
1010
	}
1011
1012
1013
1014
1015
1016
1017

	if (target->type->read_buffer == NULL)
		target->type->read_buffer = target_read_buffer_default;

	if (target->type->write_buffer == NULL)
		target->type->write_buffer = target_write_buffer_default;

1018
1019
	return ERROR_OK;
}
oharboe's avatar
oharboe committed
1020

1021
static int target_init(struct command_context *cmd_ctx)
1022
1023
1024
1025
1026
{
	struct target *target;
	int retval;

	for (target = all_targets; target; target = target->next)
1027
	{
1028
1029
		retval = target_init_one(cmd_ctx, target);
		if (ERROR_OK != retval)
1030
			return retval;
1031
	}
oharboe's avatar
oharboe committed
1032

1033
1034
1035
1036
1037
1038
1039
1040
	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,
1041
			polling_interval, 1, cmd_ctx->interp);
1042
1043
1044
	if (ERROR_OK != retval)
		return retval;

1045
1046
1047
	return ERROR_OK;
}

1048
1049
COMMAND_HANDLER(handle_target_init_command)
{
1050
1051
	int retval;

1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
	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;

1063
1064
1065
1066
	retval = command_run_line(CMD_CTX, "init_targets");
	if (ERROR_OK != retval)
		return retval;

1067
1068
1069
1070
	LOG_DEBUG("Initializing targets...");
	return target_init(CMD_CTX);
}

Zachary T Welch's avatar
Zachary T Welch committed
1071
int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1072
{
1073
	struct target_event_callback **callbacks_p = &target_event_callbacks;
oharboe's avatar
oharboe committed
1074

1075
1076
	if (callback == NULL)
	{
1077
		return ERROR_COMMAND_SYNTAX_ERROR;
1078
	}
oharboe's avatar
oharboe committed
1079

1080
1081
1082
1083
1084
1085
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
1086

1087
	(*callbacks_p) = malloc(sizeof(struct target_event_callback));
1088
1089
1090
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->priv = priv;
	(*callbacks_p)->next = NULL;
oharboe's avatar
oharboe committed
1091

1092
1093
1094
1095
1096
	return ERROR_OK;
}

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

1100
1101
	if (callback == NULL)
	{
1102
		return ERROR_COMMAND_SYNTAX_ERROR;
1103
	}
oharboe's avatar
oharboe committed
1104

1105
1106
1107
1108
1109
1110
	if (*callbacks_p)
	{
		while ((*callbacks_p)->next)
			callbacks_p = &((*callbacks_p)->next);
		callbacks_p = &((*callbacks_p)->next);
	}
oharboe's avatar
oharboe committed
1111

1112
	(*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1113
1114
1115
	(*callbacks_p)->callback = callback;
	(*callbacks_p)->periodic = periodic;
	(*callbacks_p)->time_ms = time_ms;
oharboe's avatar
oharboe committed
1116

1117
1118
1119
1120
1121
1122
1123
1124
1125
	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
1126