target.c 172 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
 *   Copyright (C) 2011 Andreas Fritiofson                                 *
 *   andreas.fritiofson@gmail.com                                          *
 *                                                                         *
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     *
37
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
38
 ***************************************************************************/
39

40 41 42 43
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

44 45
#include <helper/time_support.h>
#include <jtag/jtag.h>
46
#include <flash/nor/core.h>
47

48
#include "target.h"
49
#include "target_type.h"
50
#include "target_request.h"
51
#include "breakpoints.h"
52 53 54
#include "register.h"
#include "trace.h"
#include "image.h"
55
#include "rtos/rtos.h"
56
#include "transport/transport.h"
57
#include "arm_cti.h"
58

59 60 61
/* default halt wait timeout (ms) */
#define DEFAULT_HALT_TIMEOUT 5000

62
static int target_read_buffer_default(struct target *target, target_addr_t address,
63
		uint32_t count, uint8_t *buffer);
64
static int target_write_buffer_default(struct target *target, target_addr_t address,
65
		uint32_t count, const uint8_t *buffer);
66
static int target_array2mem(Jim_Interp *interp, struct target *target,
67
		int argc, Jim_Obj * const *argv);
68
static int target_mem2array(Jim_Interp *interp, struct target *target,
69
		int argc, Jim_Obj * const *argv);
70
static int target_register_user_commands(struct command_context *cmd_ctx);
71 72 73 74
static int target_get_gdb_fileio_info_default(struct target *target,
		struct gdb_fileio_info *fileio_info);
static int target_gdb_fileio_end_default(struct target *target, int retcode,
		int fileio_errno, bool ctrl_c);
75 76
static int target_profiling_default(struct target *target, uint32_t *samples,
		uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
77

78
/* targets */
79 80 81 82 83
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;
84
extern struct target_type arm946e_target;
85 86 87 88 89
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;
90
extern struct target_type cortexm_target;
91
extern struct target_type cortexa_target;
92
extern struct target_type aarch64_target;
93
extern struct target_type cortexr4_target;
94
extern struct target_type arm11_target;
95
extern struct target_type ls1_sap_target;
96 97
extern struct target_type mips_m4k_target;
extern struct target_type avr_target;
98
extern struct target_type dsp563xx_target;
99
extern struct target_type dsp5680xx_target;
Zachary T Welch's avatar
Zachary T Welch committed
100
extern struct target_type testee_target;
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
101
extern struct target_type avr32_ap7k_target;
102
extern struct target_type hla_target;
103 104 105
extern struct target_type nds32_v2_target;
extern struct target_type nds32_v3_target;
extern struct target_type nds32_v3m_target;
Franck Jullien's avatar
Franck Jullien committed
106
extern struct target_type or1k_target;
107
extern struct target_type quark_x10xx_target;
108
extern struct target_type quark_d20xx_target;
Ake Rehnman's avatar
Ake Rehnman committed
109
extern struct target_type stm8_target;
Tim Newsome's avatar
Tim Newsome committed
110
extern struct target_type riscv_target;
111
extern struct target_type mem_ap_target;
112
extern struct target_type esirisc_target;
113

114
static struct target_type *target_types[] = {
115 116 117 118 119
	&arm7tdmi_target,
	&arm9tdmi_target,
	&arm920t_target,
	&arm720t_target,
	&arm966e_target,
120
	&arm946e_target,
121
	&arm926ejs_target,
122
	&fa526_target,
123
	&feroceon_target,
124
	&dragonite_target,
125
	&xscale_target,
126
	&cortexm_target,
127
	&cortexa_target,
128
	&cortexr4_target,
oharboe's avatar
oharboe committed
129
	&arm11_target,
130
	&ls1_sap_target,
ntfreak's avatar
ntfreak committed
131
	&mips_m4k_target,
132
	&avr_target,
133
	&dsp563xx_target,
134
	&dsp5680xx_target,
Zachary T Welch's avatar
Zachary T Welch committed
135
	&testee_target,
Oleksandr Tymoshenko's avatar
Oleksandr Tymoshenko committed
136
	&avr32_ap7k_target,
137
	&hla_target,
138 139 140
	&nds32_v2_target,
	&nds32_v3_target,
	&nds32_v3m_target,
Franck Jullien's avatar
Franck Jullien committed
141
	&or1k_target,
142
	&quark_x10xx_target,
143
	&quark_d20xx_target,
Ake Rehnman's avatar
Ake Rehnman committed
144
	&stm8_target,
Tim Newsome's avatar
Tim Newsome committed
145
	&riscv_target,
146 147
	&mem_ap_target,
	&esirisc_target,
148 149 150
#if BUILD_TARGET64
	&aarch64_target,
#endif
151 152 153
	NULL,
};

154 155 156
struct target *all_targets;
static struct target_event_callback *target_event_callbacks;
static struct target_timer_callback *target_timer_callbacks;
157
LIST_HEAD(target_reset_callback_list);
Austin Morton's avatar
Austin Morton committed
158
LIST_HEAD(target_trace_callback_list);
159
static const int polling_interval = 100;
160

161
static const Jim_Nvp nvp_assert[] = {
162 163 164 165 166 167 168
	{ .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 }
169 170
};

171
static const Jim_Nvp nvp_error_target[] = {
172 173 174 175 176 177 178 179 180 181 182 183 184 185
	{ .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 }
};

186
static const char *target_strerror_safe(int err)
187 188 189
{
	const Jim_Nvp *n;

190
	n = Jim_Nvp_value2name_simple(nvp_error_target, err);
191
	if (n->name == NULL)
192
		return "unknown";
193
	else
194 195 196
		return n->name;
}

197
static const Jim_Nvp nvp_target_event[] = {
198

199
	{ .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
200 201 202 203 204
	{ .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" },

205 206 207
	{ .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
	{ .name = "gdb-end", .value = TARGET_EVENT_GDB_END },

208
	{ .value = TARGET_EVENT_RESET_START,         .name = "reset-start" },
209
	{ .value = TARGET_EVENT_RESET_ASSERT_PRE,    .name = "reset-assert-pre" },
210
	{ .value = TARGET_EVENT_RESET_ASSERT,        .name = "reset-assert" },
211 212 213
	{ .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" },
214 215
	{ .value = TARGET_EVENT_RESET_INIT,          .name = "reset-init" },
	{ .value = TARGET_EVENT_RESET_END,           .name = "reset-end" },
216

217
	{ .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
218
	{ .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
219

220 221 222 223 224 225 226 227 228 229 230 231
	{ .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" },

232 233
	{ .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },

234
	{ .name = NULL, .value = -1 }
235 236
};

237
static const Jim_Nvp nvp_target_state[] = {
238 239 240 241 242 243
	{ .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 },
244 245
};

246
static const Jim_Nvp nvp_target_debug_reason[] = {
247 248 249 250 251 252
	{ .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  },
253
	{ .name = "program-exit"             , .value = DBG_REASON_EXIT },
254
	{ .name = "exception-catch"          , .value = DBG_REASON_EXC_CATCH },
255 256 257 258
	{ .name = "undefined"                , .value = DBG_REASON_UNDEFINED },
	{ .name = NULL, .value = -1 },
};

259
static const Jim_Nvp nvp_target_endian[] = {
260 261 262
	{ .name = "big",    .value = TARGET_BIG_ENDIAN },
	{ .name = "little", .value = TARGET_LITTLE_ENDIAN },
	{ .name = "be",     .value = TARGET_BIG_ENDIAN },
263
	{ .name = "le",     .value = TARGET_LITTLE_ENDIAN },
264 265 266
	{ .name = NULL,     .value = -1 },
};

267
static const Jim_Nvp nvp_reset_modes[] = {
268 269 270 271 272 273 274
	{ .name = "unknown", .value = RESET_UNKNOWN },
	{ .name = "run"    , .value = RESET_RUN },
	{ .name = "halt"   , .value = RESET_HALT },
	{ .name = "init"   , .value = RESET_INIT },
	{ .name = NULL     , .value = -1 },
};

275 276 277 278 279 280 281 282 283 284 285 286 287
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;
}

288
const char *target_state_name(struct target *t)
289 290 291
{
	const char *cp;
	cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
292
	if (!cp) {
293 294 295
		LOG_ERROR("Invalid target state: %d", (int)(t->state));
		cp = "(*BUG*unknown*BUG*)";
	}
296 297 298 299

	if (!target_was_examined(t) && t->defer_examine)
		cp = "examine deferred";

300 301 302
	return cp;
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
const char *target_event_name(enum target_event event)
{
	const char *cp;
	cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
	if (!cp) {
		LOG_ERROR("Invalid target event: %d", (int)(event));
		cp = "(*BUG*unknown*BUG*)";
	}
	return cp;
}

const char *target_reset_mode_name(enum target_reset_mode reset_mode)
{
	const char *cp;
	cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
	if (!cp) {
		LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
		cp = "(*BUG*unknown*BUG*)";
	}
	return cp;
}

325
/* determine the number of the new target */
326
static int new_target_number(void)
327
{
Zachary T Welch's avatar
Zachary T Welch committed
328
	struct target *t;
329 330 331 332 333
	int x;

	/* number is 0 based */
	x = -1;
	t = all_targets;
zwelch's avatar
zwelch committed
334
	while (t) {
335
		if (x < t->target_number)
336 337 338
			x = t->target_number;
		t = t->next;
	}
zwelch's avatar
zwelch committed
339
	return x + 1;
340 341
}

342 343 344 345 346 347 348 349 350
/* read a uint64_t from a buffer in target memory endianness */
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u64(buffer);
	else
		return be_to_h_u64(buffer);
}

351
/* read a uint32_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
352
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
353 354 355 356 357 358 359
{
	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
360 361 362 363 364 365 366 367 368
/* 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
369
/* read a uint16_t from a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
370
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
371 372 373 374 375 376 377
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		return le_to_h_u16(buffer);
	else
		return be_to_h_u16(buffer);
}

378
/* read a uint8_t from a buffer in target memory endianness */
379
static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
380 381 382 383
{
	return *buffer & 0x0ff;
}

384 385 386 387 388 389 390 391 392
/* write a uint64_t to a buffer in target memory endianness */
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u64_to_le(buffer, value);
	else
		h_u64_to_be(buffer, value);
}

393
/* write a uint32_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
394
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
395 396 397 398 399 400 401
{
	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
402 403 404 405 406 407 408 409 410
/* 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
411
/* write a uint16_t to a buffer in target memory endianness */
Zachary T Welch's avatar
Zachary T Welch committed
412
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
413 414 415 416 417 418 419
{
	if (target->endianness == TARGET_LITTLE_ENDIAN)
		h_u16_to_le(buffer, value);
	else
		h_u16_to_be(buffer, value);
}

420
/* write a uint8_t to a buffer in target memory endianness */
421
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
422 423 424 425
{
	*buffer = value;
}

426 427 428 429 430 431 432 433
/* write a uint64_t array to a buffer in target memory endianness */
void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
{
	uint32_t i;
	for (i = 0; i < count; i++)
		dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
}

434 435 436 437
/* 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;
438 439
	for (i = 0; i < count; i++)
		dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
440 441 442 443 444 445
}

/* 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;
446 447
	for (i = 0; i < count; i++)
		dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
448 449
}

450 451 452 453 454 455 456 457
/* write a uint64_t array to a buffer in target memory endianness */
void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
{
	uint32_t i;
	for (i = 0; i < count; i++)
		target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
}

458
/* write a uint32_t array to a buffer in target memory endianness */
459
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
460 461
{
	uint32_t i;
462 463
	for (i = 0; i < count; i++)
		target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
464 465 466
}

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

474
/* return a pointer to a configured target; id is name or number */
Zachary T Welch's avatar
Zachary T Welch committed
475
struct target *get_target(const char *id)
476
{
Zachary T Welch's avatar
Zachary T Welch committed
477
	struct target *target;
478 479 480

	/* try as tcltarget name */
	for (target = all_targets; target; target = target->next) {
481
		if (target_name(target) == NULL)
482
			continue;
483
		if (strcmp(id, target_name(target)) == 0)
484 485 486
			return target;
	}

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

489
	/* no match, try as number */
490 491
	unsigned num;
	if (parse_uint(id, &num) != ERROR_OK)
492 493 494
		return NULL;

	for (target = all_targets; target; target = target->next) {
495 496
		if (target->target_number == (int)num) {
			LOG_WARNING("use '%s' as target identifier, not '%u'",
497
					target_name(target), num);
498
			return target;
499
		}
500 501 502 503 504
	}

	return NULL;
}

505
/* returns a pointer to the n-th configured target */
506
struct target *get_target_by_num(int num)
507
{
Zachary T Welch's avatar
Zachary T Welch committed
508
	struct target *target = all_targets;
509

zwelch's avatar
zwelch committed
510
	while (target) {
511
		if (target->target_number == num)
512 513 514 515 516 517 518
			return target;
		target = target->next;
	}

	return NULL;
}

519
struct target *get_current_target(struct command_context *cmd_ctx)
520
{
521
	struct target *target = get_current_target_or_null(cmd_ctx);
oharboe's avatar
oharboe committed
522

523
	if (target == NULL) {
524
		LOG_ERROR("BUG: current_target out of bounds");
525 526 527
		exit(-1);
	}

oharboe's avatar
oharboe committed
528
	return target;
529 530
}

531 532 533 534 535 536 537
struct target *get_current_target_or_null(struct command_context *cmd_ctx)
{
	return cmd_ctx->current_target_override
		? cmd_ctx->current_target_override
		: cmd_ctx->current_target;
}

Zachary T Welch's avatar
Zachary T Welch committed
538
int target_poll(struct target *target)
539
{
540 541
	int retval;

542
	/* We can't poll until after examine */
543
	if (!target_was_examined(target)) {
544 545 546
		/* Fail silently lest we pollute the log */
		return ERROR_FAIL;
	}
547 548 549 550 551

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

552
	if (target->halt_issued) {
553 554
		if (target->state == TARGET_HALTED)
			target->halt_issued = false;
555
		else {
Andreas Färber's avatar
Andreas Färber committed
556
			int64_t t = timeval_ms() - target->halt_issued_time;
557
			if (t > DEFAULT_HALT_TIMEOUT) {
558 559 560 561 562 563 564 565
				target->halt_issued = false;
				LOG_INFO("Halt timed out, wake up GDB.");
				target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
			}
		}
	}

	return ERROR_OK;
566 567
}

Zachary T Welch's avatar
Zachary T Welch committed
568
int target_halt(struct target *target)
569
{
570
	int retval;
571
	/* We can't poll until after examine */
572
	if (!target_was_examined(target)) {
573 574 575
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
576 577 578 579 580 581 582 583 584

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

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

	return ERROR_OK;
585 586
}

David Brownell's avatar
David Brownell committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
/**
 * 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.)
 */
617 618
int target_resume(struct target *target, int current, target_addr_t address,
		int handle_breakpoints, int debug_execution)
619
{
620
	int retval;
oharboe's avatar
oharboe committed
621

622
	/* We can't poll until after examine */
623
	if (!target_was_examined(target)) {
624 625 626
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
oharboe's avatar
oharboe committed
627

628 629
	target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);

David Brownell's avatar
David Brownell committed
630 631 632
	/* 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
633
	 */
634 635
	retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
	if (retval != ERROR_OK)
636
		return retval;
oharboe's avatar
oharboe committed
637

638 639
	target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);

640
	return retval;
641 642
}

643
static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
644 645
{
	char buf[100];
646
	int retval;
647
	Jim_Nvp *n;
648
	n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
zwelch's avatar
zwelch committed
649
	if (n->name == NULL) {
650 651 652 653
		LOG_ERROR("invalid reset mode");
		return ERROR_FAIL;
	}

654 655 656 657
	struct target *target;
	for (target = all_targets; target; target = target->next)
		target_call_reset_callbacks(target, reset_mode);

658 659 660 661
	/* 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.
	 */
662 663 664
	bool save_poll = jtag_poll_get_enabled();

	jtag_poll_set_enabled(false);
665

666
	sprintf(buf, "ocd_process_reset %s", n->name);
667
	retval = Jim_Eval(cmd_ctx->interp, buf);
668

669
	jtag_poll_set_enabled(save_poll);
670

zwelch's avatar
zwelch committed
671
	if (retval != JIM_OK) {
Øyvind Harboe's avatar
Øyvind Harboe committed
672
		Jim_MakeErrorMessage(cmd_ctx->interp);
673
		command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
674 675
		return ERROR_FAIL;
	}
676 677

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

680
	for (target = all_targets; target; target = target->next) {
Øyvind Harboe's avatar
Øyvind Harboe committed
681
		target->type->check_reset(target);
682 683
		target->running_alg = false;
	}
Øyvind Harboe's avatar
Øyvind Harboe committed
684

685
	return retval;
686 687
}

Zachary T Welch's avatar
Zachary T Welch committed
688
static int identity_virt2phys(struct target *target,
689
		target_addr_t virtual, target_addr_t *physical)
690 691 692 693 694
{
	*physical = virtual;
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
695
static int no_mmu(struct target *target, int *enabled)
696
{
697
	*enabled = 0;
698 699 700
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
701
static int default_examine(struct target *target)
702
{
703
	target_set_examined(target);
704 705 706
	return ERROR_OK;
}

Øyvind Harboe's avatar
Øyvind Harboe committed
707 708 709 710 711 712
/* no check by default */
static int default_check_reset(struct target *target)
{
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
713
int target_examine_one(struct target *target)
zwelch's avatar
zwelch committed
714
{
715 716 717 718 719 720 721 722 723
	target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);

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

	target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);

	return ERROR_OK;
zwelch's avatar
zwelch committed
724 725
}

zwelch's avatar
zwelch committed
726 727
static int jtag_enable_callback(enum jtag_event event, void *priv)
{
Zachary T Welch's avatar
Zachary T Welch committed
728
	struct target *target = priv;
zwelch's avatar
zwelch committed
729 730 731 732 733 734

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

	jtag_unregister_event_callback(jtag_enable_callback, target);

735
	return target_examine_one(target);
736
}
zwelch's avatar
zwelch committed
737

zwelch's avatar
zwelch committed
738
/* Targets that correctly implement init + examine, i.e.
739
 * no communication with target during init:
oharboe's avatar
oharboe committed
740 741
 *
 * XScale
742
 */
ntfreak's avatar
ntfreak committed
743
int target_examine(void)
744 745
{
	int retval = ERROR_OK;
Zachary T Welch's avatar
Zachary T Welch committed
746
	struct target *target;
zwelch's avatar
zwelch committed
747

748
	for (target = all_targets; target; target = target->next) {
zwelch's avatar
zwelch committed
749 750 751 752
		/* defer examination, but don't skip it */
		if (!target->tap->enabled) {
			jtag_register_event_callback(jtag_enable_callback,
					target);
zwelch's avatar
zwelch committed
753
			continue;
zwelch's avatar
zwelch committed
754
		}
755

756 757 758
		if (target->defer_examine)
			continue;

759 760
		retval = target_examine_one(target);
		if (retval != ERROR_OK)
761 762 763 764
			return retval;
	}
	return retval;
}
765

766
const char *target_type_name(struct target *target)
zwelch's avatar
zwelch committed
767 768 769
{
	return target->type->name;
}
770

771
static int target_soft_reset_halt(struct target *target)
772
{
773
	if (!target_was_examined(target)) {
774 775 776
		LOG_ERROR("Target not examined yet");
		return ERROR_FAIL;
	}
777
	if (!target->type->soft_reset_halt) {
778
		LOG_ERROR("Target %s does not support soft_reset_halt",
779
				target_name(target));
780 781
		return ERROR_FAIL;
	}
782
	return target->type->soft_reset_halt(target);
783 784
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
/**
 * 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)
801
{
802 803
	int retval = ERROR_FAIL;

804
	if (!target_was_examined(target)) {
805
		LOG_ERROR("Target not examined yet");
806
		goto done;
807
	}
David Brownell's avatar
David Brownell committed
808
	if (!target->type->run_algorithm) {
809 810 811 812 813
		LOG_ERROR("Target type '%s' does not support %s",
				target_type_name(target), __func__);
		goto done;
	}

814
	target->running_alg = true;
815 816 817 818
	retval = target->type->run_algorithm(target,
			num_mem_params, mem_params,
			num_reg_params, reg_param,
			entry_point, exit_point, timeout_ms, arch_info);
819
	target->running_alg = false;
820 821 822

done:
	return retval;
823
}
824

825
/**
Christopher Head's avatar
Christopher Head committed
826
 * Executes a target-specific native code algorithm and leaves it running.
827 828 829 830 831 832 833 834 835 836 837 838
 *
 * @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;

839
	if (!target_was_examined(target)) {
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
		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",<