You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

526 lines
19 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007-2010 √ėyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * Copyright (C) 2011 by Broadcom Corporation *
  12. * Evan Hunter - ehunter@broadcom.com *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  28. ***************************************************************************/
  29. #ifndef TARGET_H
  30. #define TARGET_H
  31. #include <helper/types.h>
  32. struct reg;
  33. struct trace;
  34. struct command_context;
  35. struct breakpoint;
  36. struct watchpoint;
  37. struct mem_param;
  38. struct reg_param;
  39. /*
  40. * TARGET_UNKNOWN = 0: we don't know anything about the target yet
  41. * TARGET_RUNNING = 1: the target is executing user code
  42. * TARGET_HALTED = 2: the target is not executing code, and ready to talk to the
  43. * debugger. on an xscale it means that the debug handler is executing
  44. * TARGET_RESET = 3: the target is being held in reset (only a temporary state,
  45. * not sure how this is used with all the recent changes)
  46. * TARGET_DEBUG_RUNNING = 4: the target is running, but it is executing code on
  47. * behalf of the debugger (e.g. algorithm for flashing)
  48. *
  49. * also see: target_state_name();
  50. */
  51. enum target_state
  52. {
  53. TARGET_UNKNOWN = 0,
  54. TARGET_RUNNING = 1,
  55. TARGET_HALTED = 2,
  56. TARGET_RESET = 3,
  57. TARGET_DEBUG_RUNNING = 4,
  58. };
  59. enum nvp_assert {
  60. NVP_DEASSERT,
  61. NVP_ASSERT,
  62. };
  63. enum target_reset_mode
  64. {
  65. RESET_UNKNOWN = 0,
  66. RESET_RUN = 1, /* reset and let target run */
  67. RESET_HALT = 2, /* reset and halt target out of reset */
  68. RESET_INIT = 3, /* reset and halt target out of reset, then run init script */
  69. };
  70. enum target_debug_reason
  71. {
  72. DBG_REASON_DBGRQ = 0,
  73. DBG_REASON_BREAKPOINT = 1,
  74. DBG_REASON_WATCHPOINT = 2,
  75. DBG_REASON_WPTANDBKPT = 3,
  76. DBG_REASON_SINGLESTEP = 4,
  77. DBG_REASON_NOTHALTED = 5,
  78. DBG_REASON_UNDEFINED = 6
  79. };
  80. enum target_endianness
  81. {
  82. TARGET_ENDIAN_UNKNOWN = 0,
  83. TARGET_BIG_ENDIAN = 1, TARGET_LITTLE_ENDIAN = 2
  84. };
  85. struct working_area
  86. {
  87. uint32_t address;
  88. uint32_t size;
  89. bool free;
  90. uint8_t *backup;
  91. struct working_area **user;
  92. struct working_area *next;
  93. };
  94. // target_type.h contains the full definitionof struct targe_type
  95. struct target
  96. {
  97. struct target_type *type; /* target type definition (name, access functions) */
  98. const char *cmd_name; /* tcl Name of target */
  99. int target_number; /* DO NOT USE! field to be removed in 2010 */
  100. struct jtag_tap *tap; /* where on the jtag chain is this */
  101. int coreid; /* which device on the TAP? */
  102. const char *variant; /* what variant of this chip is it? */
  103. /**
  104. * Indicates whether this target has been examined.
  105. *
  106. * Do @b not access this field directly, use target_was_examined()
  107. * or target_set_examined().
  108. */
  109. bool examined;
  110. /** true iff the target is currently running a downloaded
  111. * "algorithm" instetad of arbitrary user code. OpenOCD code
  112. * invoking algorithms is trusted to maintain correctness of
  113. * any cached state (e.g. for flash status), which arbitrary
  114. * code will have no reason to know about.
  115. */
  116. bool running_alg;
  117. struct target_event_action *event_action;
  118. int reset_halt; /* attempt resetting the CPU into the halted mode? */
  119. uint32_t working_area; /* working area (initialized RAM). Evaluated
  120. * upon first allocation from virtual/physical address. */
  121. bool working_area_virt_spec; /* virtual address specified? */
  122. uint32_t working_area_virt; /* virtual address */
  123. bool working_area_phys_spec; /* virtual address specified? */
  124. uint32_t working_area_phys; /* physical address */
  125. uint32_t working_area_size; /* size in bytes */
  126. uint32_t backup_working_area; /* whether the content of the working area has to be preserved */
  127. struct working_area *working_areas;/* list of allocated working areas */
  128. enum target_debug_reason debug_reason;/* reason why the target entered debug state */
  129. enum target_endianness endianness; /* target endianness */
  130. // also see: target_state_name()
  131. enum target_state state; /* the current backend-state (running, halted, ...) */
  132. struct reg_cache *reg_cache; /* the first register cache of the target (core regs) */
  133. struct breakpoint *breakpoints; /* list of breakpoints */
  134. struct watchpoint *watchpoints; /* list of watchpoints */
  135. struct trace *trace_info; /* generic trace information */
  136. struct debug_msg_receiver *dbgmsg;/* list of debug message receivers */
  137. uint32_t dbg_msg_enabled; /* debug message status */
  138. void *arch_info; /* architecture specific information */
  139. struct target *next; /* next target in list */
  140. int display; /* display async info in telnet session. Do not display
  141. * lots of halted/resumed info when stepping in debugger. */
  142. bool halt_issued; /* did we transition to halted state? */
  143. long long halt_issued_time; /* Note time when halt was issued */
  144. bool dbgbase_set; /* By default the debug base is not set */
  145. uint32_t dbgbase; /* Really a Cortex-A specific option, but there is no
  146. system in place to support target specific options
  147. currently. */
  148. struct rtos *rtos; /* Instance of Real Time Operating System support */
  149. bool rtos_auto_detect; /* A flag that indicates that the RTOS has been specified as "auto"
  150. * and must be detected when symbols are offered */
  151. };
  152. /** Returns the instance-specific name of the specified target. */
  153. static inline const char *target_name(struct target *target)
  154. {
  155. return target->cmd_name;
  156. }
  157. const char *debug_reason_name(struct target *t);
  158. enum target_event
  159. {
  160. /* LD historical names
  161. * - Prior to the great TCL change
  162. * - June/July/Aug 2008
  163. * - Duane Ellis */
  164. TARGET_EVENT_OLD_gdb_program_config,
  165. TARGET_EVENT_OLD_pre_resume,
  166. /* allow GDB to do stuff before others handle the halted event,
  167. * this is in lieu of defining ordering of invocation of events,
  168. * which would be more complicated
  169. *
  170. * Telling GDB to halt does not mean that the target stopped running,
  171. * simply that we're dropping out of GDB's waiting for step or continue.
  172. *
  173. * This can be useful when e.g. detecting power dropout.
  174. */
  175. TARGET_EVENT_GDB_HALT,
  176. TARGET_EVENT_HALTED, /* target entered debug state from normal execution or reset */
  177. TARGET_EVENT_RESUMED, /* target resumed to normal execution */
  178. TARGET_EVENT_RESUME_START,
  179. TARGET_EVENT_RESUME_END,
  180. TARGET_EVENT_GDB_START, /* debugger started execution (step/run) */
  181. TARGET_EVENT_GDB_END, /* debugger stopped execution (step/run) */
  182. TARGET_EVENT_RESET_START,
  183. TARGET_EVENT_RESET_ASSERT_PRE,
  184. TARGET_EVENT_RESET_ASSERT, /* C code uses this instead of SRST */
  185. TARGET_EVENT_RESET_ASSERT_POST,
  186. TARGET_EVENT_RESET_DEASSERT_PRE,
  187. TARGET_EVENT_RESET_DEASSERT_POST,
  188. TARGET_EVENT_RESET_HALT_PRE,
  189. TARGET_EVENT_RESET_HALT_POST,
  190. TARGET_EVENT_RESET_WAIT_PRE,
  191. TARGET_EVENT_RESET_WAIT_POST,
  192. TARGET_EVENT_RESET_INIT,
  193. TARGET_EVENT_RESET_END,
  194. TARGET_EVENT_DEBUG_HALTED, /* target entered debug state, but was executing on behalf of the debugger */
  195. TARGET_EVENT_DEBUG_RESUMED, /* target resumed to execute on behalf of the debugger */
  196. TARGET_EVENT_EXAMINE_START,
  197. TARGET_EVENT_EXAMINE_END,
  198. TARGET_EVENT_GDB_ATTACH,
  199. TARGET_EVENT_GDB_DETACH,
  200. TARGET_EVENT_GDB_FLASH_ERASE_START,
  201. TARGET_EVENT_GDB_FLASH_ERASE_END,
  202. TARGET_EVENT_GDB_FLASH_WRITE_START,
  203. TARGET_EVENT_GDB_FLASH_WRITE_END,
  204. };
  205. struct target_event_action {
  206. enum target_event event;
  207. struct Jim_Interp *interp;
  208. struct Jim_Obj *body;
  209. int has_percent;
  210. struct target_event_action *next;
  211. };
  212. bool target_has_event_action(struct target *target, enum target_event event);
  213. struct target_event_callback
  214. {
  215. int (*callback)(struct target *target, enum target_event event, void *priv);
  216. void *priv;
  217. struct target_event_callback *next;
  218. };
  219. struct target_timer_callback
  220. {
  221. int (*callback)(void *priv);
  222. int time_ms;
  223. int periodic;
  224. struct timeval when;
  225. void *priv;
  226. struct target_timer_callback *next;
  227. };
  228. int target_register_commands(struct command_context *cmd_ctx);
  229. int target_examine(void);
  230. int target_register_event_callback(
  231. int (*callback)(struct target *target,
  232. enum target_event event, void *priv),
  233. void *priv);
  234. int target_unregister_event_callback(
  235. int (*callback)(struct target *target,
  236. enum target_event event, void *priv),
  237. void *priv);
  238. /* Poll the status of the target, detect any error conditions and report them.
  239. *
  240. * Also note that this fn will clear such error conditions, so a subsequent
  241. * invocation will then succeed.
  242. *
  243. * These error conditions can be "sticky" error conditions. E.g. writing
  244. * to memory could be implemented as an open loop and if memory writes
  245. * fails, then a note is made of it, the error is sticky, but the memory
  246. * write loop still runs to completion. This improves performance in the
  247. * normal case as there is no need to verify that every single write succeed,
  248. * yet it is possible to detect error condtions.
  249. */
  250. int target_poll(struct target *target);
  251. int target_resume(struct target *target, int current, uint32_t address,
  252. int handle_breakpoints, int debug_execution);
  253. int target_halt(struct target *target);
  254. int target_call_event_callbacks(struct target *target, enum target_event event);
  255. /**
  256. * The period is very approximate, the callback can happen much more often
  257. * or much more rarely than specified
  258. */
  259. int target_register_timer_callback(int (*callback)(void *priv),
  260. int time_ms, int periodic, void *priv);
  261. int target_call_timer_callbacks(void);
  262. /**
  263. * Invoke this to ensure that e.g. polling timer callbacks happen before
  264. * a syncrhonous command completes.
  265. */
  266. int target_call_timer_callbacks_now(void);
  267. struct target* get_current_target(struct command_context *cmd_ctx);
  268. struct target *get_target(const char *id);
  269. /**
  270. * Get the target type name.
  271. *
  272. * This routine is a wrapper for the target->type->name field.
  273. * Note that this is not an instance-specific name for his target.
  274. */
  275. const char *target_type_name(struct target *target);
  276. /**
  277. * Examine the specified @a target, letting it perform any
  278. * initialization that requires JTAG access.
  279. *
  280. * This routine is a wrapper for target->type->examine.
  281. */
  282. int target_examine_one(struct target *target);
  283. /// @returns @c true if target_set_examined() has been called.
  284. static inline bool target_was_examined(struct target *target)
  285. {
  286. return target->examined;
  287. }
  288. /// Sets the @c examined flag for the given target.
  289. /// Use in target->type->examine() after one-time setup is done.
  290. static inline void target_set_examined(struct target *target)
  291. {
  292. target->examined = true;
  293. }
  294. /**
  295. * Add the @a breakpoint for @a target.
  296. *
  297. * This routine is a wrapper for target->type->add_breakpoint.
  298. */
  299. int target_add_breakpoint(struct target *target,
  300. struct breakpoint *breakpoint);
  301. /**
  302. * Remove the @a breakpoint for @a target.
  303. *
  304. * This routine is a wrapper for target->type->remove_breakpoint.
  305. */
  306. int target_remove_breakpoint(struct target *target,
  307. struct breakpoint *breakpoint);
  308. /**
  309. * Add the @a watchpoint for @a target.
  310. *
  311. * This routine is a wrapper for target->type->add_watchpoint.
  312. */
  313. int target_add_watchpoint(struct target *target,
  314. struct watchpoint *watchpoint);
  315. /**
  316. * Remove the @a watchpoint for @a target.
  317. *
  318. * This routine is a wrapper for target->type->remove_watchpoint.
  319. */
  320. int target_remove_watchpoint(struct target *target,
  321. struct watchpoint *watchpoint);
  322. /**
  323. * Obtain the registers for GDB.
  324. *
  325. * This routine is a wrapper for target->type->get_gdb_reg_list.
  326. */
  327. int target_get_gdb_reg_list(struct target *target,
  328. struct reg **reg_list[], int *reg_list_size);
  329. /**
  330. * Step the target.
  331. *
  332. * This routine is a wrapper for target->type->step.
  333. */
  334. int target_step(struct target *target,
  335. int current, uint32_t address, int handle_breakpoints);
  336. /**
  337. * Run an algorithm on the @a target given.
  338. *
  339. * This routine is a wrapper for target->type->run_algorithm.
  340. */
  341. int target_run_algorithm(struct target *target,
  342. int num_mem_params, struct mem_param *mem_params,
  343. int num_reg_params, struct reg_param *reg_param,
  344. uint32_t entry_point, uint32_t exit_point,
  345. int timeout_ms, void *arch_info);
  346. /**
  347. * Read @a count items of @a size bytes from the memory of @a target at
  348. * the @a address given.
  349. *
  350. * This routine is a wrapper for target->type->read_memory.
  351. */
  352. int target_read_memory(struct target *target,
  353. uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
  354. /**
  355. * Write @a count items of @a size bytes to the memory of @a target at
  356. * the @a address given. @a address must be aligned to @a size
  357. * in target memory.
  358. *
  359. * The endianness is the same in the host and target memory for this
  360. * function.
  361. *
  362. * \todo TODO:
  363. * Really @a buffer should have been defined as "const void *" and
  364. * @a buffer should have been aligned to @a size in the host memory.
  365. *
  366. * This is not enforced via e.g. assert's today and e.g. the
  367. * target_write_buffer fn breaks this assumption.
  368. *
  369. * This routine is wrapper for target->type->write_memory.
  370. */
  371. int target_write_memory(struct target *target,
  372. uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
  373. /**
  374. * Write @a count items of 4 bytes to the memory of @a target at
  375. * the @a address given. Because it operates only on whole words,
  376. * this should be faster than target_write_memory().
  377. *
  378. * This routine is wrapper for target->type->bulk_write_memory.
  379. */
  380. int target_bulk_write_memory(struct target *target,
  381. uint32_t address, uint32_t count, const uint8_t *buffer);
  382. /*
  383. * Write to target memory using the virtual address.
  384. *
  385. * Note that this fn is used to implement software breakpoints. Targets
  386. * can implement support for software breakpoints to memory marked as read
  387. * only by making this fn write to ram even if it is read only(MMU or
  388. * MPUs).
  389. *
  390. * It is sufficient to implement for writing a single word(16 or 32 in
  391. * ARM32/16 bit case) to write the breakpoint to ram.
  392. *
  393. * The target should also take care of "other things" to make sure that
  394. * software breakpoints can be written using this function. E.g.
  395. * when there is a separate instruction and data cache, this fn must
  396. * make sure that the instruction cache is synced up to the potential
  397. * code change that can happen as a result of the memory write(typically
  398. * by invalidating the cache).
  399. *
  400. * The high level wrapper fn in target.c will break down this memory write
  401. * request to multiple write requests to the target driver to e.g. guarantee
  402. * that writing 4 bytes to an aligned address happens with a single 32 bit
  403. * write operation, thus making this fn suitable to e.g. write to special
  404. * peripheral registers which do not support byte operations.
  405. */
  406. int target_write_buffer(struct target *target,
  407. uint32_t address, uint32_t size, const uint8_t *buffer);
  408. int target_read_buffer(struct target *target,
  409. uint32_t address, uint32_t size, uint8_t *buffer);
  410. int target_checksum_memory(struct target *target,
  411. uint32_t address, uint32_t size, uint32_t* crc);
  412. int target_blank_check_memory(struct target *target,
  413. uint32_t address, uint32_t size, uint32_t* blank);
  414. int target_wait_state(struct target *target, enum target_state state, int ms);
  415. /** Return the *name* of this targets current state */
  416. const char *target_state_name( struct target *target );
  417. /* DANGER!!!!!
  418. *
  419. * if "area" passed in to target_alloc_working_area() points to a memory
  420. * location that goes out of scope (e.g. a pointer on the stack), then
  421. * the caller of target_alloc_working_area() is responsible for invoking
  422. * target_free_working_area() before "area" goes out of scope.
  423. *
  424. * target_free_all_working_areas() will NULL out the "area" pointer
  425. * upon resuming or resetting the CPU.
  426. *
  427. */
  428. int target_alloc_working_area(struct target *target,
  429. uint32_t size, struct working_area **area);
  430. /* Same as target_alloc_working_area, except that no error is logged
  431. * when ERROR_TARGET_RESOURCE_NOT_AVAILABLE is returned.
  432. *
  433. * This allows the calling code to *try* to allocate target memory
  434. * and have a fallback to another behavior(slower?).
  435. */
  436. int target_alloc_working_area_try(struct target *target,
  437. uint32_t size, struct working_area **area);
  438. int target_free_working_area(struct target *target, struct working_area *area);
  439. void target_free_all_working_areas(struct target *target);
  440. extern struct target *all_targets;
  441. uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer);
  442. uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer);
  443. uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer);
  444. void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value);
  445. void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value);
  446. void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value);
  447. int target_read_u32(struct target *target, uint32_t address, uint32_t *value);
  448. int target_read_u16(struct target *target, uint32_t address, uint16_t *value);
  449. int target_read_u8(struct target *target, uint32_t address, uint8_t *value);
  450. int target_write_u32(struct target *target, uint32_t address, uint32_t value);
  451. int target_write_u16(struct target *target, uint32_t address, uint16_t value);
  452. int target_write_u8(struct target *target, uint32_t address, uint8_t value);
  453. /* Issues USER() statements with target state information */
  454. int target_arch_state(struct target *target);
  455. void target_handle_event(struct target *t, enum target_event e);
  456. #define ERROR_TARGET_INVALID (-300)
  457. #define ERROR_TARGET_INIT_FAILED (-301)
  458. #define ERROR_TARGET_TIMEOUT (-302)
  459. #define ERROR_TARGET_NOT_HALTED (-304)
  460. #define ERROR_TARGET_FAILURE (-305)
  461. #define ERROR_TARGET_UNALIGNED_ACCESS (-306)
  462. #define ERROR_TARGET_DATA_ABORT (-307)
  463. #define ERROR_TARGET_RESOURCE_NOT_AVAILABLE (-308)
  464. #define ERROR_TARGET_TRANSLATION_FAULT (-309)
  465. #define ERROR_TARGET_NOT_RUNNING (-310)
  466. #define ERROR_TARGET_NOT_EXAMINED (-311)
  467. extern bool get_target_reset_nag(void);
  468. #endif /* TARGET_H */