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.
 
 
 
 
 
 

2204 lines
56 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "arm.h"
  24. #include "etm.h"
  25. #include "etb.h"
  26. #include "image.h"
  27. #include "arm_disassembler.h"
  28. #include "register.h"
  29. #include "etm_dummy.h"
  30. #if BUILD_OOCD_TRACE == 1
  31. #include "oocd_trace.h"
  32. #endif
  33. /*
  34. * ARM "Embedded Trace Macrocell" (ETM) support -- direct JTAG access.
  35. *
  36. * ETM modules collect instruction and/or data trace information, compress
  37. * it, and transfer it to a debugging host through either a (buffered) trace
  38. * port (often a 38-pin Mictor connector) or an Embedded Trace Buffer (ETB).
  39. *
  40. * There are several generations of these modules. Original versions have
  41. * JTAG access through a dedicated scan chain. Recent versions have added
  42. * access via coprocessor instructions, memory addressing, and the ARM Debug
  43. * Interface v5 (ADIv5); and phased out direct JTAG access.
  44. *
  45. * This code supports up to the ETMv1.3 architecture, as seen in ETM9 and
  46. * most common ARM9 systems. Note: "CoreSight ETM9" implements ETMv3.2,
  47. * implying non-JTAG connectivity options.
  48. *
  49. * Relevant documentation includes:
  50. * ARM DDI 0157G ... ETM9 (r2p2) Technical Reference Manual
  51. * ARM DDI 0315B ... CoreSight ETM9 (r0p1) Technical Reference Manual
  52. * ARM IHI 0014O ... Embedded Trace Macrocell, Architecture Specification
  53. */
  54. enum {
  55. RO, /* read/only */
  56. WO, /* write/only */
  57. RW, /* read/write */
  58. };
  59. struct etm_reg_info {
  60. uint8_t addr;
  61. uint8_t size; /* low-N of 32 bits */
  62. uint8_t mode; /* RO, WO, RW */
  63. uint8_t bcd_vers; /* 1.0, 2.0, etc */
  64. char *name;
  65. };
  66. /*
  67. * Registers 0..0x7f are JTAG-addressable using scanchain 6.
  68. * (Or on some processors, through coprocessor operations.)
  69. * Newer versions of ETM make some W/O registers R/W, and
  70. * provide definitions for some previously-unused bits.
  71. */
  72. /* core registers used to version/configure the ETM */
  73. static const struct etm_reg_info etm_core[] = {
  74. /* NOTE: we "know" the order here ... */
  75. { ETM_CONFIG, 32, RO, 0x10, "ETM_config", },
  76. { ETM_ID, 32, RO, 0x20, "ETM_id", },
  77. };
  78. /* basic registers that are always there given the right ETM version */
  79. static const struct etm_reg_info etm_basic[] = {
  80. /* ETM Trace Registers */
  81. { ETM_CTRL, 32, RW, 0x10, "ETM_ctrl", },
  82. { ETM_TRIG_EVENT, 17, WO, 0x10, "ETM_trig_event", },
  83. { ETM_ASIC_CTRL, 8, WO, 0x10, "ETM_asic_ctrl", },
  84. { ETM_STATUS, 3, RO, 0x11, "ETM_status", },
  85. { ETM_SYS_CONFIG, 9, RO, 0x12, "ETM_sys_config", },
  86. /* TraceEnable configuration */
  87. { ETM_TRACE_RESOURCE_CTRL, 32, WO, 0x12, "ETM_trace_resource_ctrl", },
  88. { ETM_TRACE_EN_CTRL2, 16, WO, 0x12, "ETM_trace_en_ctrl2", },
  89. { ETM_TRACE_EN_EVENT, 17, WO, 0x10, "ETM_trace_en_event", },
  90. { ETM_TRACE_EN_CTRL1, 26, WO, 0x10, "ETM_trace_en_ctrl1", },
  91. /* ViewData configuration (data trace) */
  92. { ETM_VIEWDATA_EVENT, 17, WO, 0x10, "ETM_viewdata_event", },
  93. { ETM_VIEWDATA_CTRL1, 32, WO, 0x10, "ETM_viewdata_ctrl1", },
  94. { ETM_VIEWDATA_CTRL2, 32, WO, 0x10, "ETM_viewdata_ctrl2", },
  95. { ETM_VIEWDATA_CTRL3, 17, WO, 0x10, "ETM_viewdata_ctrl3", },
  96. /* REVISIT exclude VIEWDATA_CTRL2 when it's not there */
  97. { 0x78, 12, WO, 0x20, "ETM_sync_freq", },
  98. { 0x7a, 22, RO, 0x31, "ETM_config_code_ext", },
  99. { 0x7b, 32, WO, 0x31, "ETM_ext_input_select", },
  100. { 0x7c, 32, WO, 0x34, "ETM_trace_start_stop", },
  101. { 0x7d, 8, WO, 0x34, "ETM_behavior_control", },
  102. };
  103. static const struct etm_reg_info etm_fifofull[] = {
  104. /* FIFOFULL configuration */
  105. { ETM_FIFOFULL_REGION, 25, WO, 0x10, "ETM_fifofull_region", },
  106. { ETM_FIFOFULL_LEVEL, 8, WO, 0x10, "ETM_fifofull_level", },
  107. };
  108. static const struct etm_reg_info etm_addr_comp[] = {
  109. /* Address comparator register pairs */
  110. #define ADDR_COMPARATOR(i) \
  111. { ETM_ADDR_COMPARATOR_VALUE + (i) - 1, 32, WO, 0x10, \
  112. "ETM_addr_" #i "_comparator_value", }, \
  113. { ETM_ADDR_ACCESS_TYPE + (i) - 1, 7, WO, 0x10, \
  114. "ETM_addr_" #i "_access_type", }
  115. ADDR_COMPARATOR(1),
  116. ADDR_COMPARATOR(2),
  117. ADDR_COMPARATOR(3),
  118. ADDR_COMPARATOR(4),
  119. ADDR_COMPARATOR(5),
  120. ADDR_COMPARATOR(6),
  121. ADDR_COMPARATOR(7),
  122. ADDR_COMPARATOR(8),
  123. ADDR_COMPARATOR(9),
  124. ADDR_COMPARATOR(10),
  125. ADDR_COMPARATOR(11),
  126. ADDR_COMPARATOR(12),
  127. ADDR_COMPARATOR(13),
  128. ADDR_COMPARATOR(14),
  129. ADDR_COMPARATOR(15),
  130. ADDR_COMPARATOR(16),
  131. #undef ADDR_COMPARATOR
  132. };
  133. static const struct etm_reg_info etm_data_comp[] = {
  134. /* Data Value Comparators (NOTE: odd addresses are reserved) */
  135. #define DATA_COMPARATOR(i) \
  136. { ETM_DATA_COMPARATOR_VALUE + 2*(i) - 1, 32, WO, 0x10, \
  137. "ETM_data_" #i "_comparator_value", }, \
  138. { ETM_DATA_COMPARATOR_MASK + 2*(i) - 1, 32, WO, 0x10, \
  139. "ETM_data_" #i "_comparator_mask", }
  140. DATA_COMPARATOR(1),
  141. DATA_COMPARATOR(2),
  142. DATA_COMPARATOR(3),
  143. DATA_COMPARATOR(4),
  144. DATA_COMPARATOR(5),
  145. DATA_COMPARATOR(6),
  146. DATA_COMPARATOR(7),
  147. DATA_COMPARATOR(8),
  148. #undef DATA_COMPARATOR
  149. };
  150. static const struct etm_reg_info etm_counters[] = {
  151. #define ETM_COUNTER(i) \
  152. { ETM_COUNTER_RELOAD_VALUE + (i) - 1, 16, WO, 0x10, \
  153. "ETM_counter_" #i "_reload_value", }, \
  154. { ETM_COUNTER_ENABLE + (i) - 1, 18, WO, 0x10, \
  155. "ETM_counter_" #i "_enable", }, \
  156. { ETM_COUNTER_RELOAD_EVENT + (i) - 1, 17, WO, 0x10, \
  157. "ETM_counter_" #i "_reload_event", }, \
  158. { ETM_COUNTER_VALUE + (i) - 1, 16, RO, 0x10, \
  159. "ETM_counter_" #i "_value", }
  160. ETM_COUNTER(1),
  161. ETM_COUNTER(2),
  162. ETM_COUNTER(3),
  163. ETM_COUNTER(4),
  164. #undef ETM_COUNTER
  165. };
  166. static const struct etm_reg_info etm_sequencer[] = {
  167. #define ETM_SEQ(i) \
  168. { ETM_SEQUENCER_EVENT + (i), 17, WO, 0x10, \
  169. "ETM_sequencer_event" #i, }
  170. ETM_SEQ(0), /* 1->2 */
  171. ETM_SEQ(1), /* 2->1 */
  172. ETM_SEQ(2), /* 2->3 */
  173. ETM_SEQ(3), /* 3->1 */
  174. ETM_SEQ(4), /* 3->2 */
  175. ETM_SEQ(5), /* 1->3 */
  176. #undef ETM_SEQ
  177. /* 0x66 reserved */
  178. { ETM_SEQUENCER_STATE, 2, RO, 0x10, "ETM_sequencer_state", },
  179. };
  180. static const struct etm_reg_info etm_outputs[] = {
  181. #define ETM_OUTPUT(i) \
  182. { ETM_EXTERNAL_OUTPUT + (i) - 1, 17, WO, 0x10, \
  183. "ETM_external_output" #i, }
  184. ETM_OUTPUT(1),
  185. ETM_OUTPUT(2),
  186. ETM_OUTPUT(3),
  187. ETM_OUTPUT(4),
  188. #undef ETM_OUTPUT
  189. };
  190. #if 0
  191. /* registers from 0x6c..0x7f were added after ETMv1.3 */
  192. /* Context ID Comparators */
  193. { 0x6c, 32, RO, 0x20, "ETM_contextid_comparator_value1", }
  194. { 0x6d, 32, RO, 0x20, "ETM_contextid_comparator_value2", }
  195. { 0x6e, 32, RO, 0x20, "ETM_contextid_comparator_value3", }
  196. { 0x6f, 32, RO, 0x20, "ETM_contextid_comparator_mask", }
  197. #endif
  198. static int etm_get_reg(struct reg *reg);
  199. static int etm_read_reg_w_check(struct reg *reg,
  200. uint8_t* check_value, uint8_t* check_mask);
  201. static int etm_register_user_commands(struct command_context *cmd_ctx);
  202. static int etm_set_reg_w_exec(struct reg *reg, uint8_t *buf);
  203. static int etm_write_reg(struct reg *reg, uint32_t value);
  204. static const struct reg_arch_type etm_scan6_type = {
  205. .get = etm_get_reg,
  206. .set = etm_set_reg_w_exec,
  207. };
  208. /* Look up register by ID ... most ETM instances only
  209. * support a subset of the possible registers.
  210. */
  211. static struct reg *etm_reg_lookup(struct etm_context *etm_ctx, unsigned id)
  212. {
  213. struct reg_cache *cache = etm_ctx->reg_cache;
  214. unsigned i;
  215. for (i = 0; i < cache->num_regs; i++) {
  216. struct etm_reg *reg = cache->reg_list[i].arch_info;
  217. if (reg->reg_info->addr == id)
  218. return &cache->reg_list[i];
  219. }
  220. /* caller asking for nonexistent register is a bug! */
  221. /* REVISIT say which of the N targets was involved */
  222. LOG_ERROR("ETM: register 0x%02x not available", id);
  223. return NULL;
  224. }
  225. static void etm_reg_add(unsigned bcd_vers, struct arm_jtag *jtag_info,
  226. struct reg_cache *cache, struct etm_reg *ereg,
  227. const struct etm_reg_info *r, unsigned nreg)
  228. {
  229. struct reg *reg = cache->reg_list;
  230. reg += cache->num_regs;
  231. ereg += cache->num_regs;
  232. /* add up to "nreg" registers from "r", if supported by this
  233. * version of the ETM, to the specified cache.
  234. */
  235. for (; nreg--; r++) {
  236. /* this ETM may be too old to have some registers */
  237. if (r->bcd_vers > bcd_vers)
  238. continue;
  239. reg->name = r->name;
  240. reg->size = r->size;
  241. reg->value = &ereg->value;
  242. reg->arch_info = ereg;
  243. reg->type = &etm_scan6_type;
  244. reg++;
  245. cache->num_regs++;
  246. ereg->reg_info = r;
  247. ereg->jtag_info = jtag_info;
  248. ereg++;
  249. }
  250. }
  251. struct reg_cache *etm_build_reg_cache(struct target *target,
  252. struct arm_jtag *jtag_info, struct etm_context *etm_ctx)
  253. {
  254. struct reg_cache *reg_cache = malloc(sizeof(struct reg_cache));
  255. struct reg *reg_list = NULL;
  256. struct etm_reg *arch_info = NULL;
  257. unsigned bcd_vers, config;
  258. /* the actual registers are kept in two arrays */
  259. reg_list = calloc(128, sizeof(struct reg));
  260. arch_info = calloc(128, sizeof(struct etm_reg));
  261. /* fill in values for the reg cache */
  262. reg_cache->name = "etm registers";
  263. reg_cache->next = NULL;
  264. reg_cache->reg_list = reg_list;
  265. reg_cache->num_regs = 0;
  266. /* add ETM_CONFIG, then parse its values to see
  267. * which other registers exist in this ETM
  268. */
  269. etm_reg_add(0x10, jtag_info, reg_cache, arch_info,
  270. etm_core, 1);
  271. etm_get_reg(reg_list);
  272. etm_ctx->config = buf_get_u32((void *)&arch_info->value, 0, 32);
  273. config = etm_ctx->config;
  274. /* figure ETM version then add base registers */
  275. if (config & (1 << 31)) {
  276. bcd_vers = 0x20;
  277. LOG_WARNING("ETMv2+ support is incomplete");
  278. /* REVISIT more registers may exist; they may now be
  279. * readable; more register bits have defined meanings;
  280. * don't presume trace start/stop support is present;
  281. * and include any context ID comparator registers.
  282. */
  283. etm_reg_add(0x20, jtag_info, reg_cache, arch_info,
  284. etm_core + 1, 1);
  285. etm_get_reg(reg_list + 1);
  286. etm_ctx->id = buf_get_u32(
  287. (void *)&arch_info[1].value, 0, 32);
  288. LOG_DEBUG("ETM ID: %08x", (unsigned) etm_ctx->id);
  289. bcd_vers = 0x10 + (((etm_ctx->id) >> 4) & 0xff);
  290. } else {
  291. switch (config >> 28) {
  292. case 7:
  293. case 5:
  294. case 3:
  295. bcd_vers = 0x13;
  296. break;
  297. case 4:
  298. case 2:
  299. bcd_vers = 0x12;
  300. break;
  301. case 1:
  302. bcd_vers = 0x11;
  303. break;
  304. case 0:
  305. bcd_vers = 0x10;
  306. break;
  307. default:
  308. LOG_WARNING("Bad ETMv1 protocol %d", config >> 28);
  309. goto fail;
  310. }
  311. }
  312. etm_ctx->bcd_vers = bcd_vers;
  313. LOG_INFO("ETM v%d.%d", bcd_vers >> 4, bcd_vers & 0xf);
  314. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  315. etm_basic, ARRAY_SIZE(etm_basic));
  316. /* address and data comparators; counters; outputs */
  317. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  318. etm_addr_comp, 4 * (0x0f & (config >> 0)));
  319. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  320. etm_data_comp, 2 * (0x0f & (config >> 4)));
  321. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  322. etm_counters, 4 * (0x07 & (config >> 13)));
  323. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  324. etm_outputs, (0x07 & (config >> 20)));
  325. /* FIFOFULL presence is optional
  326. * REVISIT for ETMv1.2 and later, don't bother adding this
  327. * unless ETM_SYS_CONFIG says it's also *supported* ...
  328. */
  329. if (config & (1 << 23))
  330. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  331. etm_fifofull, ARRAY_SIZE(etm_fifofull));
  332. /* sequencer is optional (for state-dependant triggering) */
  333. if (config & (1 << 16))
  334. etm_reg_add(bcd_vers, jtag_info, reg_cache, arch_info,
  335. etm_sequencer, ARRAY_SIZE(etm_sequencer));
  336. /* REVISIT could realloc and likely save half the memory
  337. * in the two chunks we allocated...
  338. */
  339. /* the ETM might have an ETB connected */
  340. if (strcmp(etm_ctx->capture_driver->name, "etb") == 0)
  341. {
  342. struct etb *etb = etm_ctx->capture_driver_priv;
  343. if (!etb)
  344. {
  345. LOG_ERROR("etb selected as etm capture driver, but no ETB configured");
  346. goto fail;
  347. }
  348. reg_cache->next = etb_build_reg_cache(etb);
  349. etb->reg_cache = reg_cache->next;
  350. }
  351. etm_ctx->reg_cache = reg_cache;
  352. return reg_cache;
  353. fail:
  354. free(reg_cache);
  355. free(reg_list);
  356. free(arch_info);
  357. return NULL;
  358. }
  359. static int etm_read_reg(struct reg *reg)
  360. {
  361. return etm_read_reg_w_check(reg, NULL, NULL);
  362. }
  363. static int etm_store_reg(struct reg *reg)
  364. {
  365. return etm_write_reg(reg, buf_get_u32(reg->value, 0, reg->size));
  366. }
  367. int etm_setup(struct target *target)
  368. {
  369. int retval;
  370. uint32_t etm_ctrl_value;
  371. struct arm *arm = target_to_arm(target);
  372. struct etm_context *etm_ctx = arm->etm;
  373. struct reg *etm_ctrl_reg;
  374. etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
  375. if (!etm_ctrl_reg)
  376. return ERROR_OK;
  377. /* initialize some ETM control register settings */
  378. etm_get_reg(etm_ctrl_reg);
  379. etm_ctrl_value = buf_get_u32(etm_ctrl_reg->value, 0, 32);
  380. /* clear the ETM powerdown bit (0) */
  381. etm_ctrl_value &= ~ETM_CTRL_POWERDOWN;
  382. /* configure port width (21,6:4), mode (13,17:16) and
  383. * for older modules clocking (13)
  384. */
  385. etm_ctrl_value = (etm_ctrl_value
  386. & ~ETM_PORT_WIDTH_MASK
  387. & ~ETM_PORT_MODE_MASK
  388. & ~ETM_CTRL_DBGRQ
  389. & ~ETM_PORT_CLOCK_MASK)
  390. | etm_ctx->control;
  391. buf_set_u32(etm_ctrl_reg->value, 0, 32, etm_ctrl_value);
  392. etm_store_reg(etm_ctrl_reg);
  393. etm_ctx->control = etm_ctrl_value;
  394. if ((retval = jtag_execute_queue()) != ERROR_OK)
  395. return retval;
  396. /* REVISIT for ETMv3.0 and later, read ETM_sys_config to
  397. * verify that those width and mode settings are OK ...
  398. */
  399. if ((retval = etm_ctx->capture_driver->init(etm_ctx)) != ERROR_OK)
  400. {
  401. LOG_ERROR("ETM capture driver initialization failed");
  402. return retval;
  403. }
  404. return ERROR_OK;
  405. }
  406. static int etm_get_reg(struct reg *reg)
  407. {
  408. int retval;
  409. if ((retval = etm_read_reg(reg)) != ERROR_OK)
  410. {
  411. LOG_ERROR("BUG: error scheduling etm register read");
  412. return retval;
  413. }
  414. if ((retval = jtag_execute_queue()) != ERROR_OK)
  415. {
  416. LOG_ERROR("register read failed");
  417. return retval;
  418. }
  419. return ERROR_OK;
  420. }
  421. static int etm_read_reg_w_check(struct reg *reg,
  422. uint8_t* check_value, uint8_t* check_mask)
  423. {
  424. struct etm_reg *etm_reg = reg->arch_info;
  425. const struct etm_reg_info *r = etm_reg->reg_info;
  426. uint8_t reg_addr = r->addr & 0x7f;
  427. struct scan_field fields[3];
  428. if (etm_reg->reg_info->mode == WO) {
  429. LOG_ERROR("BUG: can't read write-only register %s", r->name);
  430. return ERROR_INVALID_ARGUMENTS;
  431. }
  432. LOG_DEBUG("%s (%u)", r->name, reg_addr);
  433. jtag_set_end_state(TAP_IDLE);
  434. arm_jtag_scann(etm_reg->jtag_info, 0x6);
  435. arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL);
  436. fields[0].tap = etm_reg->jtag_info->tap;
  437. fields[0].num_bits = 32;
  438. fields[0].out_value = reg->value;
  439. fields[0].in_value = NULL;
  440. fields[0].check_value = NULL;
  441. fields[0].check_mask = NULL;
  442. fields[1].tap = etm_reg->jtag_info->tap;
  443. fields[1].num_bits = 7;
  444. fields[1].out_value = malloc(1);
  445. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  446. fields[1].in_value = NULL;
  447. fields[1].check_value = NULL;
  448. fields[1].check_mask = NULL;
  449. fields[2].tap = etm_reg->jtag_info->tap;
  450. fields[2].num_bits = 1;
  451. fields[2].out_value = malloc(1);
  452. buf_set_u32(fields[2].out_value, 0, 1, 0);
  453. fields[2].in_value = NULL;
  454. fields[2].check_value = NULL;
  455. fields[2].check_mask = NULL;
  456. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  457. fields[0].in_value = reg->value;
  458. fields[0].check_value = check_value;
  459. fields[0].check_mask = check_mask;
  460. jtag_add_dr_scan_check(3, fields, jtag_get_end_state());
  461. free(fields[1].out_value);
  462. free(fields[2].out_value);
  463. return ERROR_OK;
  464. }
  465. static int etm_set_reg(struct reg *reg, uint32_t value)
  466. {
  467. int retval;
  468. if ((retval = etm_write_reg(reg, value)) != ERROR_OK)
  469. {
  470. LOG_ERROR("BUG: error scheduling etm register write");
  471. return retval;
  472. }
  473. buf_set_u32(reg->value, 0, reg->size, value);
  474. reg->valid = 1;
  475. reg->dirty = 0;
  476. return ERROR_OK;
  477. }
  478. static int etm_set_reg_w_exec(struct reg *reg, uint8_t *buf)
  479. {
  480. int retval;
  481. etm_set_reg(reg, buf_get_u32(buf, 0, reg->size));
  482. if ((retval = jtag_execute_queue()) != ERROR_OK)
  483. {
  484. LOG_ERROR("register write failed");
  485. return retval;
  486. }
  487. return ERROR_OK;
  488. }
  489. static int etm_write_reg(struct reg *reg, uint32_t value)
  490. {
  491. struct etm_reg *etm_reg = reg->arch_info;
  492. const struct etm_reg_info *r = etm_reg->reg_info;
  493. uint8_t reg_addr = r->addr & 0x7f;
  494. struct scan_field fields[3];
  495. if (etm_reg->reg_info->mode == RO) {
  496. LOG_ERROR("BUG: can't write read--only register %s", r->name);
  497. return ERROR_INVALID_ARGUMENTS;
  498. }
  499. LOG_DEBUG("%s (%u): 0x%8.8" PRIx32 "", r->name, reg_addr, value);
  500. jtag_set_end_state(TAP_IDLE);
  501. arm_jtag_scann(etm_reg->jtag_info, 0x6);
  502. arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL);
  503. fields[0].tap = etm_reg->jtag_info->tap;
  504. fields[0].num_bits = 32;
  505. uint8_t tmp1[4];
  506. fields[0].out_value = tmp1;
  507. buf_set_u32(fields[0].out_value, 0, 32, value);
  508. fields[0].in_value = NULL;
  509. fields[1].tap = etm_reg->jtag_info->tap;
  510. fields[1].num_bits = 7;
  511. uint8_t tmp2;
  512. fields[1].out_value = &tmp2;
  513. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  514. fields[1].in_value = NULL;
  515. fields[2].tap = etm_reg->jtag_info->tap;
  516. fields[2].num_bits = 1;
  517. uint8_t tmp3;
  518. fields[2].out_value = &tmp3;
  519. buf_set_u32(fields[2].out_value, 0, 1, 1);
  520. fields[2].in_value = NULL;
  521. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  522. return ERROR_OK;
  523. }
  524. /* ETM trace analysis functionality */
  525. static struct etm_capture_driver *etm_capture_drivers[] =
  526. {
  527. &etb_capture_driver,
  528. &etm_dummy_capture_driver,
  529. #if BUILD_OOCD_TRACE == 1
  530. &oocd_trace_capture_driver,
  531. #endif
  532. NULL
  533. };
  534. static int etm_read_instruction(struct etm_context *ctx, struct arm_instruction *instruction)
  535. {
  536. int i;
  537. int section = -1;
  538. size_t size_read;
  539. uint32_t opcode;
  540. int retval;
  541. if (!ctx->image)
  542. return ERROR_TRACE_IMAGE_UNAVAILABLE;
  543. /* search for the section the current instruction belongs to */
  544. for (i = 0; i < ctx->image->num_sections; i++)
  545. {
  546. if ((ctx->image->sections[i].base_address <= ctx->current_pc) &&
  547. (ctx->image->sections[i].base_address + ctx->image->sections[i].size > ctx->current_pc))
  548. {
  549. section = i;
  550. break;
  551. }
  552. }
  553. if (section == -1)
  554. {
  555. /* current instruction couldn't be found in the image */
  556. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  557. }
  558. if (ctx->core_state == ARM_STATE_ARM)
  559. {
  560. uint8_t buf[4];
  561. if ((retval = image_read_section(ctx->image, section,
  562. ctx->current_pc - ctx->image->sections[section].base_address,
  563. 4, buf, &size_read)) != ERROR_OK)
  564. {
  565. LOG_ERROR("error while reading instruction: %i", retval);
  566. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  567. }
  568. opcode = target_buffer_get_u32(ctx->target, buf);
  569. arm_evaluate_opcode(opcode, ctx->current_pc, instruction);
  570. }
  571. else if (ctx->core_state == ARM_STATE_THUMB)
  572. {
  573. uint8_t buf[2];
  574. if ((retval = image_read_section(ctx->image, section,
  575. ctx->current_pc - ctx->image->sections[section].base_address,
  576. 2, buf, &size_read)) != ERROR_OK)
  577. {
  578. LOG_ERROR("error while reading instruction: %i", retval);
  579. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  580. }
  581. opcode = target_buffer_get_u16(ctx->target, buf);
  582. thumb_evaluate_opcode(opcode, ctx->current_pc, instruction);
  583. }
  584. else if (ctx->core_state == ARM_STATE_JAZELLE)
  585. {
  586. LOG_ERROR("BUG: tracing of jazelle code not supported");
  587. return ERROR_FAIL;
  588. }
  589. else
  590. {
  591. LOG_ERROR("BUG: unknown core state encountered");
  592. return ERROR_FAIL;
  593. }
  594. return ERROR_OK;
  595. }
  596. static int etmv1_next_packet(struct etm_context *ctx, uint8_t *packet, int apo)
  597. {
  598. while (ctx->data_index < ctx->trace_depth)
  599. {
  600. /* if the caller specified an address packet offset, skip until the
  601. * we reach the n-th cycle marked with tracesync */
  602. if (apo > 0)
  603. {
  604. if (ctx->trace_data[ctx->data_index].flags & ETMV1_TRACESYNC_CYCLE)
  605. apo--;
  606. if (apo > 0)
  607. {
  608. ctx->data_index++;
  609. ctx->data_half = 0;
  610. }
  611. continue;
  612. }
  613. /* no tracedata output during a TD cycle
  614. * or in a trigger cycle */
  615. if ((ctx->trace_data[ctx->data_index].pipestat == STAT_TD)
  616. || (ctx->trace_data[ctx->data_index].flags & ETMV1_TRIGGER_CYCLE))
  617. {
  618. ctx->data_index++;
  619. ctx->data_half = 0;
  620. continue;
  621. }
  622. /* FIXME there are more port widths than these... */
  623. if ((ctx->control & ETM_PORT_WIDTH_MASK) == ETM_PORT_16BIT)
  624. {
  625. if (ctx->data_half == 0)
  626. {
  627. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  628. ctx->data_half = 1;
  629. }
  630. else
  631. {
  632. *packet = (ctx->trace_data[ctx->data_index].packet & 0xff00) >> 8;
  633. ctx->data_half = 0;
  634. ctx->data_index++;
  635. }
  636. }
  637. else if ((ctx->control & ETM_PORT_WIDTH_MASK) == ETM_PORT_8BIT)
  638. {
  639. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  640. ctx->data_index++;
  641. }
  642. else
  643. {
  644. /* on a 4-bit port, a packet will be output during two consecutive cycles */
  645. if (ctx->data_index > (ctx->trace_depth - 2))
  646. return -1;
  647. *packet = ctx->trace_data[ctx->data_index].packet & 0xf;
  648. *packet |= (ctx->trace_data[ctx->data_index + 1].packet & 0xf) << 4;
  649. ctx->data_index += 2;
  650. }
  651. return 0;
  652. }
  653. return -1;
  654. }
  655. static int etmv1_branch_address(struct etm_context *ctx)
  656. {
  657. int retval;
  658. uint8_t packet;
  659. int shift = 0;
  660. int apo;
  661. uint32_t i;
  662. /* quit analysis if less than two cycles are left in the trace
  663. * because we can't extract the APO */
  664. if (ctx->data_index > (ctx->trace_depth - 2))
  665. return -1;
  666. /* a BE could be output during an APO cycle, skip the current
  667. * and continue with the new one */
  668. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x4)
  669. return 1;
  670. if (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x4)
  671. return 2;
  672. /* address packet offset encoded in the next two cycles' pipestat bits */
  673. apo = ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x3;
  674. apo |= (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x3) << 2;
  675. /* count number of tracesync cycles between current pipe_index and data_index
  676. * i.e. the number of tracesyncs that data_index already passed by
  677. * to subtract them from the APO */
  678. for (i = ctx->pipe_index; i < ctx->data_index; i++)
  679. {
  680. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & ETMV1_TRACESYNC_CYCLE)
  681. apo--;
  682. }
  683. /* extract up to four 7-bit packets */
  684. do {
  685. if ((retval = etmv1_next_packet(ctx, &packet, (shift == 0) ? apo + 1 : 0)) != 0)
  686. return -1;
  687. ctx->last_branch &= ~(0x7f << shift);
  688. ctx->last_branch |= (packet & 0x7f) << shift;
  689. shift += 7;
  690. } while ((packet & 0x80) && (shift < 28));
  691. /* one last packet holding 4 bits of the address, plus the branch reason code */
  692. if ((shift == 28) && (packet & 0x80))
  693. {
  694. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  695. return -1;
  696. ctx->last_branch &= 0x0fffffff;
  697. ctx->last_branch |= (packet & 0x0f) << 28;
  698. ctx->last_branch_reason = (packet & 0x70) >> 4;
  699. shift += 4;
  700. }
  701. else
  702. {
  703. ctx->last_branch_reason = 0;
  704. }
  705. if (shift == 32)
  706. {
  707. ctx->pc_ok = 1;
  708. }
  709. /* if a full address was output, we might have branched into Jazelle state */
  710. if ((shift == 32) && (packet & 0x80))
  711. {
  712. ctx->core_state = ARM_STATE_JAZELLE;
  713. }
  714. else
  715. {
  716. /* if we didn't branch into Jazelle state, the current processor state is
  717. * encoded in bit 0 of the branch target address */
  718. if (ctx->last_branch & 0x1)
  719. {
  720. ctx->core_state = ARM_STATE_THUMB;
  721. ctx->last_branch &= ~0x1;
  722. }
  723. else
  724. {
  725. ctx->core_state = ARM_STATE_ARM;
  726. ctx->last_branch &= ~0x3;
  727. }
  728. }
  729. return 0;
  730. }
  731. static int etmv1_data(struct etm_context *ctx, int size, uint32_t *data)
  732. {
  733. int j;
  734. uint8_t buf[4];
  735. int retval;
  736. for (j = 0; j < size; j++)
  737. {
  738. if ((retval = etmv1_next_packet(ctx, &buf[j], 0)) != 0)
  739. return -1;
  740. }
  741. if (size == 8)
  742. {
  743. LOG_ERROR("TODO: add support for 64-bit values");
  744. return -1;
  745. }
  746. else if (size == 4)
  747. *data = target_buffer_get_u32(ctx->target, buf);
  748. else if (size == 2)
  749. *data = target_buffer_get_u16(ctx->target, buf);
  750. else if (size == 1)
  751. *data = buf[0];
  752. else
  753. return -1;
  754. return 0;
  755. }
  756. static int etmv1_analyze_trace(struct etm_context *ctx, struct command_context *cmd_ctx)
  757. {
  758. int retval;
  759. struct arm_instruction instruction;
  760. /* read the trace data if it wasn't read already */
  761. if (ctx->trace_depth == 0)
  762. ctx->capture_driver->read_trace(ctx);
  763. /* start at the beginning of the captured trace */
  764. ctx->pipe_index = 0;
  765. ctx->data_index = 0;
  766. ctx->data_half = 0;
  767. /* neither the PC nor the data pointer are valid */
  768. ctx->pc_ok = 0;
  769. ctx->ptr_ok = 0;
  770. while (ctx->pipe_index < ctx->trace_depth)
  771. {
  772. uint8_t pipestat = ctx->trace_data[ctx->pipe_index].pipestat;
  773. uint32_t next_pc = ctx->current_pc;
  774. uint32_t old_data_index = ctx->data_index;
  775. uint32_t old_data_half = ctx->data_half;
  776. uint32_t old_index = ctx->pipe_index;
  777. uint32_t last_instruction = ctx->last_instruction;
  778. uint32_t cycles = 0;
  779. int current_pc_ok = ctx->pc_ok;
  780. if (ctx->trace_data[ctx->pipe_index].flags & ETMV1_TRIGGER_CYCLE)
  781. {
  782. command_print(cmd_ctx, "--- trigger ---");
  783. }
  784. /* instructions execute in IE/D or BE/D cycles */
  785. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  786. ctx->last_instruction = ctx->pipe_index;
  787. /* if we don't have a valid pc skip until we reach an indirect branch */
  788. if ((!ctx->pc_ok) && (pipestat != STAT_BE))
  789. {
  790. ctx->pipe_index++;
  791. continue;
  792. }
  793. /* any indirect branch could have interrupted instruction flow
  794. * - the branch reason code could indicate a trace discontinuity
  795. * - a branch to the exception vectors indicates an exception
  796. */
  797. if ((pipestat == STAT_BE) || (pipestat == STAT_BD))
  798. {
  799. /* backup current data index, to be able to consume the branch address
  800. * before examining data address and values
  801. */
  802. old_data_index = ctx->data_index;
  803. old_data_half = ctx->data_half;
  804. ctx->last_instruction = ctx->pipe_index;
  805. if ((retval = etmv1_branch_address(ctx)) != 0)
  806. {
  807. /* negative return value from etmv1_branch_address means we ran out of packets,
  808. * quit analysing the trace */
  809. if (retval < 0)
  810. break;
  811. /* a positive return values means the current branch was abandoned,
  812. * and a new branch was encountered in cycle ctx->pipe_index + retval;
  813. */
  814. LOG_WARNING("abandoned branch encountered, correctnes of analysis uncertain");
  815. ctx->pipe_index += retval;
  816. continue;
  817. }
  818. /* skip over APO cycles */
  819. ctx->pipe_index += 2;
  820. switch (ctx->last_branch_reason)
  821. {
  822. case 0x0: /* normal PC change */
  823. next_pc = ctx->last_branch;
  824. break;
  825. case 0x1: /* tracing enabled */
  826. command_print(cmd_ctx, "--- tracing enabled at 0x%8.8" PRIx32 " ---", ctx->last_branch);
  827. ctx->current_pc = ctx->last_branch;
  828. ctx->pipe_index++;
  829. continue;
  830. break;
  831. case 0x2: /* trace restarted after FIFO overflow */
  832. command_print(cmd_ctx, "--- trace restarted after FIFO overflow at 0x%8.8" PRIx32 " ---", ctx->last_branch);
  833. ctx->current_pc = ctx->last_branch;
  834. ctx->pipe_index++;
  835. continue;
  836. break;
  837. case 0x3: /* exit from debug state */
  838. command_print(cmd_ctx, "--- exit from debug state at 0x%8.8" PRIx32 " ---", ctx->last_branch);
  839. ctx->current_pc = ctx->last_branch;
  840. ctx->pipe_index++;
  841. continue;
  842. break;
  843. case 0x4: /* periodic synchronization point */
  844. next_pc = ctx->last_branch;
  845. /* if we had no valid PC prior to this synchronization point,
  846. * we have to move on with the next trace cycle
  847. */
  848. if (!current_pc_ok)
  849. {
  850. command_print(cmd_ctx, "--- periodic synchronization point at 0x%8.8" PRIx32 " ---", next_pc);
  851. ctx->current_pc = next_pc;
  852. ctx->pipe_index++;
  853. continue;
  854. }
  855. break;
  856. default: /* reserved */
  857. LOG_ERROR("BUG: branch reason code 0x%" PRIx32 " is reserved", ctx->last_branch_reason);
  858. return ERROR_FAIL;
  859. }
  860. /* if we got here the branch was a normal PC change
  861. * (or a periodic synchronization point, which means the same for that matter)
  862. * if we didn't accquire a complete PC continue with the next cycle
  863. */
  864. if (!ctx->pc_ok)
  865. continue;
  866. /* indirect branch to the exception vector means an exception occured */
  867. if ((ctx->last_branch <= 0x20)
  868. || ((ctx->last_branch >= 0xffff0000) && (ctx->last_branch <= 0xffff0020)))
  869. {
  870. if ((ctx->last_branch & 0xff) == 0x10)
  871. {
  872. command_print(cmd_ctx, "data abort");
  873. }
  874. else
  875. {
  876. command_print(cmd_ctx, "exception vector 0x%2.2" PRIx32 "", ctx->last_branch);
  877. ctx->current_pc = ctx->last_branch;
  878. ctx->pipe_index++;
  879. continue;
  880. }
  881. }
  882. }
  883. /* an instruction was executed (or not, depending on the condition flags)
  884. * retrieve it from the image for displaying */
  885. if (ctx->pc_ok && (pipestat != STAT_WT) && (pipestat != STAT_TD) &&
  886. !(((pipestat == STAT_BE) || (pipestat == STAT_BD)) &&
  887. ((ctx->last_branch_reason != 0x0) && (ctx->last_branch_reason != 0x4))))
  888. {
  889. if ((retval = etm_read_instruction(ctx, &instruction)) != ERROR_OK)
  890. {
  891. /* can't continue tracing with no image available */
  892. if (retval == ERROR_TRACE_IMAGE_UNAVAILABLE)
  893. {
  894. return retval;
  895. }
  896. else if (retval == ERROR_TRACE_INSTRUCTION_UNAVAILABLE)
  897. {
  898. /* TODO: handle incomplete images
  899. * for now we just quit the analsysis*/
  900. return retval;
  901. }
  902. }
  903. cycles = old_index - last_instruction;
  904. }
  905. if ((pipestat == STAT_ID) || (pipestat == STAT_BD))
  906. {
  907. uint32_t new_data_index = ctx->data_index;
  908. uint32_t new_data_half = ctx->data_half;
  909. /* in case of a branch with data, the branch target address was consumed before
  910. * we temporarily go back to the saved data index */
  911. if (pipestat == STAT_BD)
  912. {
  913. ctx->data_index = old_data_index;
  914. ctx->data_half = old_data_half;
  915. }
  916. if (ctx->control & ETM_CTRL_TRACE_ADDR)
  917. {
  918. uint8_t packet;
  919. int shift = 0;
  920. do {
  921. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  922. return ERROR_ETM_ANALYSIS_FAILED;
  923. ctx->last_ptr &= ~(0x7f << shift);
  924. ctx->last_ptr |= (packet & 0x7f) << shift;
  925. shift += 7;
  926. } while ((packet & 0x80) && (shift < 32));
  927. if (shift >= 32)
  928. ctx->ptr_ok = 1;
  929. if (ctx->ptr_ok)
  930. {
  931. command_print(cmd_ctx, "address: 0x%8.8" PRIx32 "", ctx->last_ptr);
  932. }
  933. }
  934. if (ctx->control & ETM_CTRL_TRACE_DATA)
  935. {
  936. if ((instruction.type == ARM_LDM) || (instruction.type == ARM_STM))
  937. {
  938. int i;
  939. for (i = 0; i < 16; i++)
  940. {
  941. if (instruction.info.load_store_multiple.register_list & (1 << i))
  942. {
  943. uint32_t data;
  944. if (etmv1_data(ctx, 4, &data) != 0)
  945. return ERROR_ETM_ANALYSIS_FAILED;
  946. command_print(cmd_ctx, "data: 0x%8.8" PRIx32 "", data);
  947. }
  948. }
  949. }
  950. else if ((instruction.type >= ARM_LDR) && (instruction.type <= ARM_STRH))
  951. {
  952. uint32_t data;
  953. if (etmv1_data(ctx, arm_access_size(&instruction), &data) != 0)
  954. return ERROR_ETM_ANALYSIS_FAILED;
  955. command_print(cmd_ctx, "data: 0x%8.8" PRIx32 "", data);
  956. }
  957. }
  958. /* restore data index after consuming BD address and data */
  959. if (pipestat == STAT_BD)
  960. {
  961. ctx->data_index = new_data_index;
  962. ctx->data_half = new_data_half;
  963. }
  964. }
  965. /* adjust PC */
  966. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  967. {
  968. if (((instruction.type == ARM_B) ||
  969. (instruction.type == ARM_BL) ||
  970. (instruction.type == ARM_BLX)) &&
  971. (instruction.info.b_bl_bx_blx.target_address != 0xffffffff))
  972. {
  973. next_pc = instruction.info.b_bl_bx_blx.target_address;
  974. }
  975. else
  976. {
  977. next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
  978. }
  979. }
  980. else if (pipestat == STAT_IN)
  981. {
  982. next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
  983. }
  984. if ((pipestat != STAT_TD) && (pipestat != STAT_WT))
  985. {
  986. char cycles_text[32] = "";
  987. /* if the trace was captured with cycle accurate tracing enabled,
  988. * output the number of cycles since the last executed instruction
  989. */
  990. if (ctx->control & ETM_CTRL_CYCLE_ACCURATE)
  991. {
  992. snprintf(cycles_text, 32, " (%i %s)",
  993. (int)cycles,
  994. (cycles == 1) ? "cycle" : "cycles");
  995. }
  996. command_print(cmd_ctx, "%s%s%s",
  997. instruction.text,
  998. (pipestat == STAT_IN) ? " (not executed)" : "",
  999. cycles_text);
  1000. ctx->current_pc = next_pc;
  1001. /* packets for an instruction don't start on or before the preceding
  1002. * functional pipestat (i.e. other than WT or TD)
  1003. */
  1004. if (ctx->data_index <= ctx->pipe_index)
  1005. {
  1006. ctx->data_index = ctx->pipe_index + 1;
  1007. ctx->data_half = 0;
  1008. }
  1009. }
  1010. ctx->pipe_index += 1;
  1011. }
  1012. return ERROR_OK;
  1013. }
  1014. static COMMAND_HELPER(handle_etm_tracemode_command_update,
  1015. uint32_t *mode)
  1016. {
  1017. uint32_t tracemode;
  1018. /* what parts of data access are traced? */
  1019. if (strcmp(CMD_ARGV[0], "none") == 0)
  1020. tracemode = 0;
  1021. else if (strcmp(CMD_ARGV[0], "data") == 0)
  1022. tracemode = ETM_CTRL_TRACE_DATA;
  1023. else if (strcmp(CMD_ARGV[0], "address") == 0)
  1024. tracemode = ETM_CTRL_TRACE_ADDR;
  1025. else if (strcmp(CMD_ARGV[0], "all") == 0)
  1026. tracemode = ETM_CTRL_TRACE_DATA | ETM_CTRL_TRACE_ADDR;
  1027. else
  1028. {
  1029. command_print(CMD_CTX, "invalid option '%s'", CMD_ARGV[0]);
  1030. return ERROR_INVALID_ARGUMENTS;
  1031. }
  1032. uint8_t context_id;
  1033. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], context_id);
  1034. switch (context_id)
  1035. {
  1036. case 0:
  1037. tracemode |= ETM_CTRL_CONTEXTID_NONE;
  1038. break;
  1039. case 8:
  1040. tracemode |= ETM_CTRL_CONTEXTID_8;
  1041. break;
  1042. case 16:
  1043. tracemode |= ETM_CTRL_CONTEXTID_16;
  1044. break;
  1045. case 32:
  1046. tracemode |= ETM_CTRL_CONTEXTID_32;
  1047. break;
  1048. default:
  1049. command_print(CMD_CTX, "invalid option '%s'", CMD_ARGV[1]);
  1050. return ERROR_INVALID_ARGUMENTS;
  1051. }
  1052. bool etmv1_cycle_accurate;
  1053. COMMAND_PARSE_ENABLE(CMD_ARGV[2], etmv1_cycle_accurate);
  1054. if (etmv1_cycle_accurate)
  1055. tracemode |= ETM_CTRL_CYCLE_ACCURATE;
  1056. bool etmv1_branch_output;
  1057. COMMAND_PARSE_ENABLE(CMD_ARGV[3], etmv1_branch_output);
  1058. if (etmv1_branch_output)
  1059. tracemode |= ETM_CTRL_BRANCH_OUTPUT;
  1060. /* IGNORED:
  1061. * - CPRT tracing (coprocessor register transfers)
  1062. * - debug request (causes debug entry on trigger)
  1063. * - stall on FIFOFULL (preventing tracedata lossage)
  1064. */
  1065. *mode = tracemode;
  1066. return ERROR_OK;
  1067. }
  1068. COMMAND_HANDLER(handle_etm_tracemode_command)
  1069. {
  1070. struct target *target = get_current_target(CMD_CTX);
  1071. struct arm *arm = target_to_arm(target);
  1072. struct etm_context *etm;
  1073. if (!is_arm(arm)) {
  1074. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1075. return ERROR_FAIL;
  1076. }
  1077. etm = arm->etm;
  1078. if (!etm) {
  1079. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1080. return ERROR_FAIL;
  1081. }
  1082. uint32_t tracemode = etm->control;
  1083. switch (CMD_ARGC)
  1084. {
  1085. case 0:
  1086. break;
  1087. case 4:
  1088. CALL_COMMAND_HANDLER(handle_etm_tracemode_command_update, &tracemode);
  1089. break;
  1090. default:
  1091. command_print(CMD_CTX, "usage: configure trace mode "
  1092. "<none | data | address | all> "
  1093. "<context id bits> <cycle accurate> <branch output>");
  1094. return ERROR_FAIL;
  1095. }
  1096. /**
  1097. * todo: fail if parameters were invalid for this hardware,
  1098. * or couldn't be written; display actual hardware state...
  1099. */
  1100. command_print(CMD_CTX, "current tracemode configuration:");
  1101. switch (tracemode & ETM_CTRL_TRACE_MASK)
  1102. {
  1103. default:
  1104. command_print(CMD_CTX, "data tracing: none");
  1105. break;
  1106. case ETM_CTRL_TRACE_DATA:
  1107. command_print(CMD_CTX, "data tracing: data only");
  1108. break;
  1109. case ETM_CTRL_TRACE_ADDR:
  1110. command_print(CMD_CTX, "data tracing: address only");
  1111. break;
  1112. case ETM_CTRL_TRACE_DATA | ETM_CTRL_TRACE_ADDR:
  1113. command_print(CMD_CTX, "data tracing: address and data");
  1114. break;
  1115. }
  1116. switch (tracemode & ETM_CTRL_CONTEXTID_MASK)
  1117. {
  1118. case ETM_CTRL_CONTEXTID_NONE:
  1119. command_print(CMD_CTX, "contextid tracing: none");
  1120. break;
  1121. case ETM_CTRL_CONTEXTID_8:
  1122. command_print(CMD_CTX, "contextid tracing: 8 bit");
  1123. break;
  1124. case ETM_CTRL_CONTEXTID_16:
  1125. command_print(CMD_CTX, "contextid tracing: 16 bit");
  1126. break;
  1127. case ETM_CTRL_CONTEXTID_32:
  1128. command_print(CMD_CTX, "contextid tracing: 32 bit");
  1129. break;
  1130. }
  1131. if (tracemode & ETM_CTRL_CYCLE_ACCURATE)
  1132. {
  1133. command_print(CMD_CTX, "cycle-accurate tracing enabled");
  1134. }
  1135. else
  1136. {
  1137. command_print(CMD_CTX, "cycle-accurate tracing disabled");
  1138. }
  1139. if (tracemode & ETM_CTRL_BRANCH_OUTPUT)
  1140. {
  1141. command_print(CMD_CTX, "full branch address output enabled");
  1142. }
  1143. else
  1144. {
  1145. command_print(CMD_CTX, "full branch address output disabled");
  1146. }
  1147. #define TRACEMODE_MASK ( \
  1148. ETM_CTRL_CONTEXTID_MASK \
  1149. | ETM_CTRL_BRANCH_OUTPUT \
  1150. | ETM_CTRL_CYCLE_ACCURATE \
  1151. | ETM_CTRL_TRACE_MASK \
  1152. )
  1153. /* only update ETM_CTRL register if tracemode changed */
  1154. if ((etm->control & TRACEMODE_MASK) != tracemode)
  1155. {
  1156. struct reg *etm_ctrl_reg;
  1157. etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
  1158. if (!etm_ctrl_reg)
  1159. return ERROR_FAIL;
  1160. etm->control &= ~TRACEMODE_MASK;
  1161. etm->control |= tracemode & TRACEMODE_MASK;
  1162. buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
  1163. etm_store_reg(etm_ctrl_reg);
  1164. /* invalidate old trace data */
  1165. etm->capture_status = TRACE_IDLE;
  1166. if (etm->trace_depth > 0)
  1167. {
  1168. free(etm->trace_data);
  1169. etm->trace_data = NULL;
  1170. }
  1171. etm->trace_depth = 0;
  1172. }
  1173. #undef TRACEMODE_MASK
  1174. return ERROR_OK;
  1175. }
  1176. COMMAND_HANDLER(handle_etm_config_command)
  1177. {
  1178. struct target *target;
  1179. struct arm *arm;
  1180. uint32_t portmode = 0x0;
  1181. struct etm_context *etm_ctx;
  1182. int i;
  1183. if (CMD_ARGC != 5)
  1184. return ERROR_COMMAND_SYNTAX_ERROR;
  1185. target = get_target(CMD_ARGV[0]);
  1186. if (!target)
  1187. {
  1188. LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
  1189. return ERROR_FAIL;
  1190. }
  1191. arm = target_to_arm(target);
  1192. if (!is_arm(arm)) {
  1193. command_print(CMD_CTX, "target '%s' is '%s'; not an ARM",
  1194. target_name(target),
  1195. target_type_name(target));
  1196. return ERROR_FAIL;
  1197. }
  1198. /* FIXME for ETMv3.0 and above -- and we don't yet know what ETM
  1199. * version we'll be using!! -- so we can't know how to validate
  1200. * params yet. "etm config" should likely be *AFTER* hookup...
  1201. *
  1202. * - Many more widths might be supported ... and we can easily
  1203. * check whether our setting "took".
  1204. *
  1205. * - The "clock" and "mode" bits are interpreted differently.
  1206. * See ARM IHI 0014O table 2-17 for the old behavior, and
  1207. * table 2-18 for the new. With ETB it's best to specify
  1208. * "normal full" ...
  1209. */
  1210. uint8_t port_width;
  1211. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], port_width);
  1212. switch (port_width)
  1213. {
  1214. /* before ETMv3.0 */
  1215. case 4:
  1216. portmode |= ETM_PORT_4BIT;
  1217. break;
  1218. case 8:
  1219. portmode |= ETM_PORT_8BIT;
  1220. break;
  1221. case 16:
  1222. portmode |= ETM_PORT_16BIT;
  1223. break;
  1224. /* ETMv3.0 and later*/
  1225. case 24:
  1226. portmode |= ETM_PORT_24BIT;
  1227. break;
  1228. case 32:
  1229. portmode |= ETM_PORT_32BIT;
  1230. break;
  1231. case 48:
  1232. portmode |= ETM_PORT_48BIT;
  1233. break;
  1234. case 64:
  1235. portmode |= ETM_PORT_64BIT;
  1236. break;
  1237. case 1:
  1238. portmode |= ETM_PORT_1BIT;
  1239. break;
  1240. case 2:
  1241. portmode |= ETM_PORT_2BIT;
  1242. break;
  1243. default:
  1244. command_print(CMD_CTX,
  1245. "unsupported ETM port width '%s'", CMD_ARGV[1]);
  1246. return ERROR_FAIL;
  1247. }
  1248. if (strcmp("normal", CMD_ARGV[2]) == 0)
  1249. {
  1250. portmode |= ETM_PORT_NORMAL;
  1251. }
  1252. else if (strcmp("multiplexed", CMD_ARGV[2]) == 0)
  1253. {
  1254. portmode |= ETM_PORT_MUXED;
  1255. }
  1256. else if (strcmp("demultiplexed", CMD_ARGV[2]) == 0)
  1257. {
  1258. portmode |= ETM_PORT_DEMUXED;
  1259. }
  1260. else
  1261. {
  1262. command_print(CMD_CTX, "unsupported ETM port mode '%s', must be 'normal', 'multiplexed' or 'demultiplexed'", CMD_ARGV[2]);
  1263. return ERROR_FAIL;
  1264. }
  1265. if (strcmp("half", CMD_ARGV[3]) == 0)
  1266. {
  1267. portmode |= ETM_PORT_HALF_CLOCK;
  1268. }
  1269. else if (strcmp("full", CMD_ARGV[3]) == 0)
  1270. {
  1271. portmode |= ETM_PORT_FULL_CLOCK;
  1272. }
  1273. else
  1274. {
  1275. command_print(CMD_CTX, "unsupported ETM port clocking '%s', must be 'full' or 'half'", CMD_ARGV[3]);
  1276. return ERROR_FAIL;
  1277. }
  1278. etm_ctx = calloc(1, sizeof(struct etm_context));
  1279. if (!etm_ctx) {
  1280. LOG_DEBUG("out of memory");
  1281. return ERROR_FAIL;
  1282. }
  1283. for (i = 0; etm_capture_drivers[i]; i++)
  1284. {
  1285. if (strcmp(CMD_ARGV[4], etm_capture_drivers[i]->name) == 0)
  1286. {
  1287. int retval = register_commands(CMD_CTX, NULL,
  1288. etm_capture_drivers[i]->commands);
  1289. if (ERROR_OK != retval)
  1290. {
  1291. free(etm_ctx);
  1292. return retval;
  1293. }
  1294. etm_ctx->capture_driver = etm_capture_drivers[i];
  1295. break;
  1296. }
  1297. }
  1298. if (!etm_capture_drivers[i])
  1299. {
  1300. /* no supported capture driver found, don't register an ETM */
  1301. free(etm_ctx);
  1302. LOG_ERROR("trace capture driver '%s' not found", CMD_ARGV[4]);
  1303. return ERROR_FAIL;
  1304. }
  1305. etm_ctx->target = target;
  1306. etm_ctx->trace_data = NULL;
  1307. etm_ctx->control = portmode;
  1308. etm_ctx->core_state = ARM_STATE_ARM;
  1309. arm->etm = etm_ctx;
  1310. return etm_register_user_commands(CMD_CTX);
  1311. }
  1312. COMMAND_HANDLER(handle_etm_info_command)
  1313. {
  1314. struct target *target;
  1315. struct arm *arm;
  1316. struct etm_context *etm;
  1317. struct reg *etm_sys_config_reg;
  1318. int max_port_size;
  1319. uint32_t config;
  1320. target = get_current_target(CMD_CTX);
  1321. arm = target_to_arm(target);
  1322. if (!is_arm(arm))
  1323. {
  1324. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1325. return ERROR_FAIL;
  1326. }
  1327. etm = arm->etm;
  1328. if (!etm)
  1329. {
  1330. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1331. return ERROR_FAIL;
  1332. }
  1333. command_print(CMD_CTX, "ETM v%d.%d",
  1334. etm->bcd_vers >> 4, etm->bcd_vers & 0xf);
  1335. command_print(CMD_CTX, "pairs of address comparators: %i",
  1336. (int) (etm->config >> 0) & 0x0f);
  1337. command_print(CMD_CTX, "data comparators: %i",
  1338. (int) (etm->config >> 4) & 0x0f);
  1339. command_print(CMD_CTX, "memory map decoders: %i",
  1340. (int) (etm->config >> 8) & 0x1f);
  1341. command_print(CMD_CTX, "number of counters: %i",
  1342. (int) (etm->config >> 13) & 0x07);
  1343. command_print(CMD_CTX, "sequencer %spresent",
  1344. (int) (etm->config & (1 << 16)) ? "" : "not ");
  1345. command_print(CMD_CTX, "number of ext. inputs: %i",
  1346. (int) (etm->config >> 17) & 0x07);
  1347. command_print(CMD_CTX, "number of ext. outputs: %i",
  1348. (int) (etm->config >> 20) & 0x07);
  1349. command_print(CMD_CTX, "FIFO full %spresent",
  1350. (int) (etm->config & (1 << 23)) ? "" : "not ");
  1351. if (etm->bcd_vers < 0x20)
  1352. command_print(CMD_CTX, "protocol version: %i",
  1353. (int) (etm->config >> 28) & 0x07);
  1354. else {
  1355. command_print(CMD_CTX,
  1356. "coprocessor and memory access %ssupported",
  1357. (etm->config & (1 << 26)) ? "" : "not ");
  1358. command_print(CMD_CTX, "trace start/stop %spresent",
  1359. (etm->config & (1 << 26)) ? "" : "not ");
  1360. command_print(CMD_CTX, "number of context comparators: %i",
  1361. (int) (etm->config >> 24) & 0x03);
  1362. }
  1363. /* SYS_CONFIG isn't present before ETMv1.2 */
  1364. etm_sys_config_reg = etm_reg_lookup(etm, ETM_SYS_CONFIG);
  1365. if (!etm_sys_config_reg)
  1366. return ERROR_OK;
  1367. etm_get_reg(etm_sys_config_reg);
  1368. config = buf_get_u32(etm_sys_config_reg->value, 0, 32);
  1369. LOG_DEBUG("ETM SYS CONFIG %08x", (unsigned) config);
  1370. max_port_size = config & 0x7;
  1371. if (etm->bcd_vers >= 0x30)
  1372. max_port_size |= (config >> 6) & 0x08;
  1373. switch (max_port_size)
  1374. {
  1375. /* before ETMv3.0 */
  1376. case 0:
  1377. max_port_size = 4;
  1378. break;
  1379. case 1:
  1380. max_port_size = 8;
  1381. break;
  1382. case 2:
  1383. max_port_size = 16;
  1384. break;
  1385. /* ETMv3.0 and later*/
  1386. case 3:
  1387. max_port_size = 24;
  1388. break;
  1389. case 4:
  1390. max_port_size = 32;
  1391. break;
  1392. case 5:
  1393. max_port_size = 48;
  1394. break;
  1395. case 6:
  1396. max_port_size = 64;
  1397. break;
  1398. case 8:
  1399. max_port_size = 1;
  1400. break;
  1401. case 9:
  1402. max_port_size = 2;
  1403. break;
  1404. default:
  1405. LOG_ERROR("Illegal max_port_size");
  1406. return ERROR_FAIL;
  1407. }
  1408. command_print(CMD_CTX, "max. port size: %i", max_port_size);
  1409. if (etm->bcd_vers < 0x30) {
  1410. command_print(CMD_CTX, "half-rate clocking %ssupported",
  1411. (config & (1 << 3)) ? "" : "not ");
  1412. command_print(CMD_CTX, "full-rate clocking %ssupported",
  1413. (config & (1 << 4)) ? "" : "not ");
  1414. command_print(CMD_CTX, "normal trace format %ssupported",
  1415. (config & (1 << 5)) ? "" : "not ");
  1416. command_print(CMD_CTX, "multiplex trace format %ssupported",
  1417. (config & (1 << 6)) ? "" : "not ");
  1418. command_print(CMD_CTX, "demultiplex trace format %ssupported",
  1419. (config & (1 << 7)) ? "" : "not ");
  1420. } else {
  1421. /* REVISIT show which size and format are selected ... */
  1422. command_print(CMD_CTX, "current port size %ssupported",
  1423. (config & (1 << 10)) ? "" : "not ");
  1424. command_print(CMD_CTX, "current trace format %ssupported",
  1425. (config & (1 << 11)) ? "" : "not ");
  1426. }
  1427. if (etm->bcd_vers >= 0x21)
  1428. command_print(CMD_CTX, "fetch comparisons %ssupported",
  1429. (config & (1 << 17)) ? "not " : "");
  1430. command_print(CMD_CTX, "FIFO full %ssupported",
  1431. (config & (1 << 8)) ? "" : "not ");
  1432. return ERROR_OK;
  1433. }
  1434. COMMAND_HANDLER(handle_etm_status_command)
  1435. {
  1436. struct target *target;
  1437. struct arm *arm;
  1438. struct etm_context *etm;
  1439. trace_status_t trace_status;
  1440. target = get_current_target(CMD_CTX);
  1441. arm = target_to_arm(target);
  1442. if (!is_arm(arm))
  1443. {
  1444. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1445. return ERROR_FAIL;
  1446. }
  1447. etm = arm->etm;
  1448. if (!etm)
  1449. {
  1450. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1451. return ERROR_FAIL;
  1452. }
  1453. /* ETM status */
  1454. if (etm->bcd_vers >= 0x11) {
  1455. struct reg *reg;
  1456. reg = etm_reg_lookup(etm, ETM_STATUS);
  1457. if (!reg)
  1458. return ERROR_FAIL;
  1459. if (etm_get_reg(reg) == ERROR_OK) {
  1460. unsigned s = buf_get_u32(reg->value, 0, reg->size);
  1461. command_print(CMD_CTX, "etm: %s%s%s%s",
  1462. /* bit(1) == progbit */
  1463. (etm->bcd_vers >= 0x12)
  1464. ? ((s & (1 << 1))
  1465. ? "disabled" : "enabled")
  1466. : "?",
  1467. ((s & (1 << 3)) && etm->bcd_vers >= 0x31)
  1468. ? " triggered" : "",
  1469. ((s & (1 << 2)) && etm->bcd_vers >= 0x12)
  1470. ? " start/stop" : "",
  1471. ((s & (1 << 0)) && etm->bcd_vers >= 0x11)
  1472. ? " untraced-overflow" : "");
  1473. } /* else ignore and try showing trace port status */
  1474. }
  1475. /* Trace Port Driver status */
  1476. trace_status = etm->capture_driver->status(etm);
  1477. if (trace_status == TRACE_IDLE)
  1478. {
  1479. command_print(CMD_CTX, "%s: idle", etm->capture_driver->name);
  1480. }
  1481. else
  1482. {
  1483. static char *completed = " completed";
  1484. static char *running = " is running";
  1485. static char *overflowed = ", overflowed";
  1486. static char *triggered = ", triggered";
  1487. command_print(CMD_CTX, "%s: trace collection%s%s%s",
  1488. etm->capture_driver->name,
  1489. (trace_status & TRACE_RUNNING) ? running : completed,
  1490. (trace_status & TRACE_OVERFLOWED) ? overflowed : "",
  1491. (trace_status & TRACE_TRIGGERED) ? triggered : "");
  1492. if (etm->trace_depth > 0)
  1493. {
  1494. command_print(CMD_CTX, "%i frames of trace data read",
  1495. (int)(etm->trace_depth));
  1496. }
  1497. }
  1498. return ERROR_OK;
  1499. }
  1500. COMMAND_HANDLER(handle_etm_image_command)
  1501. {
  1502. struct target *target;
  1503. struct arm *arm;
  1504. struct etm_context *etm_ctx;
  1505. if (CMD_ARGC < 1)
  1506. {
  1507. command_print(CMD_CTX, "usage: etm image <file> [base address] [type]");
  1508. return ERROR_FAIL;
  1509. }
  1510. target = get_current_target(CMD_CTX);
  1511. arm = target_to_arm(target);
  1512. if (!is_arm(arm))
  1513. {
  1514. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1515. return ERROR_FAIL;
  1516. }
  1517. etm_ctx = arm->etm;
  1518. if (!etm_ctx)
  1519. {
  1520. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1521. return ERROR_FAIL;
  1522. }
  1523. if (etm_ctx->image)
  1524. {
  1525. image_close(etm_ctx->image);
  1526. free(etm_ctx->image);
  1527. command_print(CMD_CTX, "previously loaded image found and closed");
  1528. }
  1529. etm_ctx->image = malloc(sizeof(struct image));
  1530. etm_ctx->image->base_address_set = 0;
  1531. etm_ctx->image->start_address_set = 0;
  1532. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1533. if (CMD_ARGC >= 2)
  1534. {
  1535. etm_ctx->image->base_address_set = 1;
  1536. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], etm_ctx->image->base_address);
  1537. }
  1538. else
  1539. {
  1540. etm_ctx->image->base_address_set = 0;
  1541. }
  1542. if (image_open(etm_ctx->image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
  1543. {
  1544. free(etm_ctx->image);
  1545. etm_ctx->image = NULL;
  1546. return ERROR_FAIL;
  1547. }
  1548. return ERROR_OK;
  1549. }
  1550. COMMAND_HANDLER(handle_etm_dump_command)
  1551. {
  1552. struct fileio file;
  1553. struct target *target;
  1554. struct arm *arm;
  1555. struct etm_context *etm_ctx;
  1556. uint32_t i;
  1557. if (CMD_ARGC != 1)
  1558. {
  1559. command_print(CMD_CTX, "usage: etm dump <file>");
  1560. return ERROR_FAIL;
  1561. }
  1562. target = get_current_target(CMD_CTX);
  1563. arm = target_to_arm(target);
  1564. if (!is_arm(arm))
  1565. {
  1566. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1567. return ERROR_FAIL;
  1568. }
  1569. etm_ctx = arm->etm;
  1570. if (!etm_ctx)
  1571. {
  1572. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1573. return ERROR_FAIL;
  1574. }
  1575. if (etm_ctx->capture_driver->status == TRACE_IDLE)
  1576. {
  1577. command_print(CMD_CTX, "trace capture wasn't enabled, no trace data captured");
  1578. return ERROR_OK;
  1579. }
  1580. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1581. {
  1582. /* TODO: if on-the-fly capture is to be supported, this needs to be changed */
  1583. command_print(CMD_CTX, "trace capture not completed");
  1584. return ERROR_FAIL;
  1585. }
  1586. /* read the trace data if it wasn't read already */
  1587. if (etm_ctx->trace_depth == 0)
  1588. etm_ctx->capture_driver->read_trace(etm_ctx);
  1589. if (fileio_open(&file, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1590. {
  1591. return ERROR_FAIL;
  1592. }
  1593. fileio_write_u32(&file, etm_ctx->capture_status);
  1594. fileio_write_u32(&file, etm_ctx->control);
  1595. fileio_write_u32(&file, etm_ctx->trace_depth);
  1596. for (i = 0; i < etm_ctx->trace_depth; i++)
  1597. {
  1598. fileio_write_u32(&file, etm_ctx->trace_data[i].pipestat);
  1599. fileio_write_u32(&file, etm_ctx->trace_data[i].packet);
  1600. fileio_write_u32(&file, etm_ctx->trace_data[i].flags);
  1601. }
  1602. fileio_close(&file);
  1603. return ERROR_OK;
  1604. }
  1605. COMMAND_HANDLER(handle_etm_load_command)
  1606. {
  1607. struct fileio file;
  1608. struct target *target;
  1609. struct arm *arm;
  1610. struct etm_context *etm_ctx;
  1611. uint32_t i;
  1612. if (CMD_ARGC != 1)
  1613. {
  1614. command_print(CMD_CTX, "usage: etm load <file>");
  1615. return ERROR_FAIL;
  1616. }
  1617. target = get_current_target(CMD_CTX);
  1618. arm = target_to_arm(target);
  1619. if (!is_arm(arm))
  1620. {
  1621. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1622. return ERROR_FAIL;
  1623. }
  1624. etm_ctx = arm->etm;
  1625. if (!etm_ctx)
  1626. {
  1627. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1628. return ERROR_FAIL;
  1629. }
  1630. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1631. {
  1632. command_print(CMD_CTX, "trace capture running, stop first");
  1633. return ERROR_FAIL;
  1634. }
  1635. if (fileio_open(&file, CMD_ARGV[0], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  1636. {
  1637. return ERROR_FAIL;
  1638. }
  1639. if (file.size % 4)
  1640. {
  1641. command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data");
  1642. fileio_close(&file);
  1643. return ERROR_FAIL;
  1644. }
  1645. if (etm_ctx->trace_depth > 0)
  1646. {
  1647. free(etm_ctx->trace_data);
  1648. etm_ctx->trace_data = NULL;
  1649. }
  1650. {
  1651. uint32_t tmp;
  1652. fileio_read_u32(&file, &tmp); etm_ctx->capture_status = tmp;
  1653. fileio_read_u32(&file, &tmp); etm_ctx->control = tmp;
  1654. fileio_read_u32(&file, &etm_ctx->trace_depth);
  1655. }
  1656. etm_ctx->trace_data = malloc(sizeof(struct etmv1_trace_data) * etm_ctx->trace_depth);
  1657. if (etm_ctx->trace_data == NULL)
  1658. {
  1659. command_print(CMD_CTX, "not enough memory to perform operation");
  1660. fileio_close(&file);
  1661. return ERROR_FAIL;
  1662. }
  1663. for (i = 0; i < etm_ctx->trace_depth; i++)
  1664. {
  1665. uint32_t pipestat, packet, flags;
  1666. fileio_read_u32(&file, &pipestat);
  1667. fileio_read_u32(&file, &packet);
  1668. fileio_read_u32(&file, &flags);
  1669. etm_ctx->trace_data[i].pipestat = pipestat & 0xff;
  1670. etm_ctx->trace_data[i].packet = packet & 0xffff;
  1671. etm_ctx->trace_data[i].flags = flags;
  1672. }
  1673. fileio_close(&file);
  1674. return ERROR_OK;
  1675. }
  1676. COMMAND_HANDLER(handle_etm_start_command)
  1677. {
  1678. struct target *target;
  1679. struct arm *arm;
  1680. struct etm_context *etm_ctx;
  1681. struct reg *etm_ctrl_reg;
  1682. target = get_current_target(CMD_CTX);
  1683. arm = target_to_arm(target);
  1684. if (!is_arm(arm))
  1685. {
  1686. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1687. return ERROR_FAIL;
  1688. }
  1689. etm_ctx = arm->etm;
  1690. if (!etm_ctx)
  1691. {
  1692. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1693. return ERROR_FAIL;
  1694. }
  1695. /* invalidate old tracing data */
  1696. etm_ctx->capture_status = TRACE_IDLE;
  1697. if (etm_ctx->trace_depth > 0)
  1698. {
  1699. free(etm_ctx->trace_data);
  1700. etm_ctx->trace_data = NULL;
  1701. }
  1702. etm_ctx->trace_depth = 0;
  1703. etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
  1704. if (!etm_ctrl_reg)
  1705. return ERROR_FAIL;
  1706. etm_get_reg(etm_ctrl_reg);
  1707. /* Clear programming bit (10), set port selection bit (11) */
  1708. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x2);
  1709. etm_store_reg(etm_ctrl_reg);
  1710. jtag_execute_queue();
  1711. etm_ctx->capture_driver->start_capture(etm_ctx);
  1712. return ERROR_OK;
  1713. }
  1714. COMMAND_HANDLER(handle_etm_stop_command)
  1715. {
  1716. struct target *target;
  1717. struct arm *arm;
  1718. struct etm_context *etm_ctx;
  1719. struct reg *etm_ctrl_reg;
  1720. target = get_current_target(CMD_CTX);
  1721. arm = target_to_arm(target);
  1722. if (!is_arm(arm))
  1723. {
  1724. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1725. return ERROR_FAIL;
  1726. }
  1727. etm_ctx = arm->etm;
  1728. if (!etm_ctx)
  1729. {
  1730. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1731. return ERROR_FAIL;
  1732. }
  1733. etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
  1734. if (!etm_ctrl_reg)
  1735. return ERROR_FAIL;
  1736. etm_get_reg(etm_ctrl_reg);
  1737. /* Set programming bit (10), clear port selection bit (11) */
  1738. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x1);
  1739. etm_store_reg(etm_ctrl_reg);
  1740. jtag_execute_queue();
  1741. etm_ctx->capture_driver->stop_capture(etm_ctx);
  1742. return ERROR_OK;
  1743. }
  1744. COMMAND_HANDLER(handle_etm_trigger_debug_command)
  1745. {
  1746. struct target *target;
  1747. struct arm *arm;
  1748. struct etm_context *etm;
  1749. target = get_current_target(CMD_CTX);
  1750. arm = target_to_arm(target);
  1751. if (!is_arm(arm))
  1752. {
  1753. command_print(CMD_CTX, "ETM: %s isn't an ARM",
  1754. target_name(target));
  1755. return ERROR_FAIL;
  1756. }
  1757. etm = arm->etm;
  1758. if (!etm)
  1759. {
  1760. command_print(CMD_CTX, "ETM: no ETM configured for %s",
  1761. target_name(target));
  1762. return ERROR_FAIL;
  1763. }
  1764. if (CMD_ARGC == 1) {
  1765. struct reg *etm_ctrl_reg;
  1766. bool dbgrq;
  1767. etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
  1768. if (!etm_ctrl_reg)
  1769. return ERROR_FAIL;
  1770. COMMAND_PARSE_ENABLE(CMD_ARGV[0], dbgrq);
  1771. if (dbgrq)
  1772. etm->control |= ETM_CTRL_DBGRQ;
  1773. else
  1774. etm->control &= ~ETM_CTRL_DBGRQ;
  1775. /* etm->control will be written to hardware
  1776. * the next time an "etm start" is issued.
  1777. */
  1778. buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
  1779. }
  1780. command_print(CMD_CTX, "ETM: %s debug halt",
  1781. (etm->control & ETM_CTRL_DBGRQ)
  1782. ? "triggers"
  1783. : "does not trigger");
  1784. return ERROR_OK;
  1785. }
  1786. COMMAND_HANDLER(handle_etm_analyze_command)
  1787. {
  1788. struct target *target;
  1789. struct arm *arm;
  1790. struct etm_context *etm_ctx;
  1791. int retval;
  1792. target = get_current_target(CMD_CTX);
  1793. arm = target_to_arm(target);
  1794. if (!is_arm(arm))
  1795. {
  1796. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1797. return ERROR_FAIL;
  1798. }
  1799. etm_ctx = arm->etm;
  1800. if (!etm_ctx)
  1801. {
  1802. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1803. return ERROR_FAIL;
  1804. }
  1805. if ((retval = etmv1_analyze_trace(etm_ctx, CMD_CTX)) != ERROR_OK)
  1806. {
  1807. switch (retval)
  1808. {
  1809. case ERROR_ETM_ANALYSIS_FAILED:
  1810. command_print(CMD_CTX, "further analysis failed (corrupted trace data or just end of data");
  1811. break;
  1812. case ERROR_TRACE_INSTRUCTION_UNAVAILABLE:
  1813. command_print(CMD_CTX, "no instruction for current address available, analysis aborted");
  1814. break;
  1815. case ERROR_TRACE_IMAGE_UNAVAILABLE:
  1816. command_print(CMD_CTX, "no image available for trace analysis");
  1817. break;
  1818. default:
  1819. command_print(CMD_CTX, "unknown error: %i", retval);
  1820. }
  1821. }
  1822. return retval;
  1823. }
  1824. static const struct command_registration etm_config_command_handlers[] = {
  1825. {
  1826. .name = "config",
  1827. .handler = &handle_etm_config_command,
  1828. .mode = COMMAND_CONFIG,
  1829. .usage = "<target> <port_width> <port_mode> "
  1830. "<clocking> <capture_driver>",
  1831. },
  1832. COMMAND_REGISTRATION_DONE
  1833. };
  1834. const struct command_registration etm_command_handlers[] = {
  1835. {
  1836. .name = "etm",
  1837. .mode = COMMAND_ANY,
  1838. .help = "Emebdded Trace Macrocell command group",
  1839. .chain = etm_config_command_handlers,
  1840. },
  1841. COMMAND_REGISTRATION_DONE
  1842. };
  1843. static const struct command_registration etm_exec_command_handlers[] = {
  1844. {
  1845. .name = "tracemode", handle_etm_tracemode_command,
  1846. .mode = COMMAND_EXEC,
  1847. .help = "configure/display trace mode",
  1848. .usage = "<none | data | address | all> "
  1849. "<context_id_bits> <cycle_accurate> <branch_output>",
  1850. },
  1851. {
  1852. .name = "info",
  1853. .handler = &handle_etm_info_command,
  1854. .mode = COMMAND_EXEC,
  1855. .help = "display info about the current target's ETM",
  1856. },
  1857. {
  1858. .name = "status",
  1859. .handler = &handle_etm_status_command,
  1860. .mode = COMMAND_EXEC,
  1861. .help = "display current target's ETM status",
  1862. },
  1863. {
  1864. .name = "start",
  1865. .handler = &handle_etm_start_command,
  1866. .mode = COMMAND_EXEC,
  1867. .help = "start ETM trace collection",
  1868. },
  1869. {
  1870. .name = "stop",
  1871. .handler = &handle_etm_stop_command,
  1872. .mode = COMMAND_EXEC,
  1873. .help = "stop ETM trace collection",
  1874. },
  1875. {
  1876. .name = "trigger_debug",
  1877. .handler = handle_etm_trigger_debug_command,
  1878. .mode = COMMAND_EXEC,
  1879. .help = "enable/disable debug entry on trigger",
  1880. .usage = "(enable | disable)",
  1881. },
  1882. {
  1883. .name = "analyze",
  1884. .handler = handle_etm_analyze_command,
  1885. .mode = COMMAND_EXEC,
  1886. .help = "analyze collected ETM trace",
  1887. },
  1888. {
  1889. .name = "image",
  1890. .handler = &handle_etm_image_command,
  1891. .mode = COMMAND_EXEC,
  1892. .help = "load image from <file> [base address]",
  1893. },
  1894. {
  1895. .name = "dump",
  1896. .handler = &handle_etm_dump_command,
  1897. .mode = COMMAND_EXEC,
  1898. .help = "dump captured trace data <file>",
  1899. },
  1900. {
  1901. .name = "load",
  1902. .handler = &handle_etm_load_command,
  1903. .mode = COMMAND_EXEC,
  1904. .help = "load trace data for analysis <file>",
  1905. },
  1906. COMMAND_REGISTRATION_DONE
  1907. };
  1908. static int etm_register_user_commands(struct command_context *cmd_ctx)
  1909. {
  1910. struct command *etm_cmd = command_find_in_context(cmd_ctx, "etm");
  1911. return register_commands(cmd_ctx, etm_cmd, etm_exec_command_handlers);
  1912. }