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.
 
 
 
 
 
 

2233 lines
57 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. int retval;
  429. if (etm_reg->reg_info->mode == WO) {
  430. LOG_ERROR("BUG: can't read write-only register %s", r->name);
  431. return ERROR_INVALID_ARGUMENTS;
  432. }
  433. LOG_DEBUG("%s (%u)", r->name, reg_addr);
  434. retval = arm_jtag_scann(etm_reg->jtag_info, 0x6, TAP_IDLE);
  435. if (retval != ERROR_OK)
  436. return retval;
  437. retval = arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL, TAP_IDLE);
  438. if (retval != ERROR_OK)
  439. return retval;
  440. fields[0].num_bits = 32;
  441. fields[0].out_value = reg->value;
  442. fields[0].in_value = NULL;
  443. fields[0].check_value = NULL;
  444. fields[0].check_mask = NULL;
  445. fields[1].num_bits = 7;
  446. uint8_t temp1;
  447. fields[1].out_value = &temp1;
  448. buf_set_u32(&temp1, 0, 7, reg_addr);
  449. fields[1].in_value = NULL;
  450. fields[1].check_value = NULL;
  451. fields[1].check_mask = NULL;
  452. fields[2].num_bits = 1;
  453. uint8_t temp2;
  454. fields[2].out_value = &temp2;
  455. buf_set_u32(&temp2, 0, 1, 0);
  456. fields[2].in_value = NULL;
  457. fields[2].check_value = NULL;
  458. fields[2].check_mask = NULL;
  459. jtag_add_dr_scan(etm_reg->jtag_info->tap, 3, fields, TAP_IDLE);
  460. fields[0].in_value = reg->value;
  461. fields[0].check_value = check_value;
  462. fields[0].check_mask = check_mask;
  463. jtag_add_dr_scan_check(etm_reg->jtag_info->tap, 3, fields, TAP_IDLE);
  464. return ERROR_OK;
  465. }
  466. static int etm_set_reg(struct reg *reg, uint32_t value)
  467. {
  468. int retval;
  469. if ((retval = etm_write_reg(reg, value)) != ERROR_OK)
  470. {
  471. LOG_ERROR("BUG: error scheduling etm register write");
  472. return retval;
  473. }
  474. buf_set_u32(reg->value, 0, reg->size, value);
  475. reg->valid = 1;
  476. reg->dirty = 0;
  477. return ERROR_OK;
  478. }
  479. static int etm_set_reg_w_exec(struct reg *reg, uint8_t *buf)
  480. {
  481. int retval;
  482. etm_set_reg(reg, buf_get_u32(buf, 0, reg->size));
  483. if ((retval = jtag_execute_queue()) != ERROR_OK)
  484. {
  485. LOG_ERROR("register write failed");
  486. return retval;
  487. }
  488. return ERROR_OK;
  489. }
  490. static int etm_write_reg(struct reg *reg, uint32_t value)
  491. {
  492. struct etm_reg *etm_reg = reg->arch_info;
  493. const struct etm_reg_info *r = etm_reg->reg_info;
  494. uint8_t reg_addr = r->addr & 0x7f;
  495. struct scan_field fields[3];
  496. int retval;
  497. if (etm_reg->reg_info->mode == RO) {
  498. LOG_ERROR("BUG: can't write read--only register %s", r->name);
  499. return ERROR_INVALID_ARGUMENTS;
  500. }
  501. LOG_DEBUG("%s (%u): 0x%8.8" PRIx32 "", r->name, reg_addr, value);
  502. retval = arm_jtag_scann(etm_reg->jtag_info, 0x6, TAP_IDLE);
  503. if (retval != ERROR_OK)
  504. return retval;
  505. retval = arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL, TAP_IDLE);
  506. if (retval != ERROR_OK)
  507. return retval;
  508. fields[0].num_bits = 32;
  509. uint8_t tmp1[4];
  510. fields[0].out_value = tmp1;
  511. buf_set_u32(tmp1, 0, 32, value);
  512. fields[0].in_value = NULL;
  513. fields[1].num_bits = 7;
  514. uint8_t tmp2;
  515. fields[1].out_value = &tmp2;
  516. buf_set_u32(&tmp2, 0, 7, reg_addr);
  517. fields[1].in_value = NULL;
  518. fields[2].num_bits = 1;
  519. uint8_t tmp3;
  520. fields[2].out_value = &tmp3;
  521. buf_set_u32(&tmp3, 0, 1, 1);
  522. fields[2].in_value = NULL;
  523. jtag_add_dr_scan(etm_reg->jtag_info->tap, 3, fields, TAP_IDLE);
  524. return ERROR_OK;
  525. }
  526. /* ETM trace analysis functionality */
  527. static struct etm_capture_driver *etm_capture_drivers[] =
  528. {
  529. &etb_capture_driver,
  530. &etm_dummy_capture_driver,
  531. #if BUILD_OOCD_TRACE == 1
  532. &oocd_trace_capture_driver,
  533. #endif
  534. NULL
  535. };
  536. static int etm_read_instruction(struct etm_context *ctx, struct arm_instruction *instruction)
  537. {
  538. int i;
  539. int section = -1;
  540. size_t size_read;
  541. uint32_t opcode;
  542. int retval;
  543. if (!ctx->image)
  544. return ERROR_TRACE_IMAGE_UNAVAILABLE;
  545. /* search for the section the current instruction belongs to */
  546. for (i = 0; i < ctx->image->num_sections; i++)
  547. {
  548. if ((ctx->image->sections[i].base_address <= ctx->current_pc) &&
  549. (ctx->image->sections[i].base_address + ctx->image->sections[i].size > ctx->current_pc))
  550. {
  551. section = i;
  552. break;
  553. }
  554. }
  555. if (section == -1)
  556. {
  557. /* current instruction couldn't be found in the image */
  558. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  559. }
  560. if (ctx->core_state == ARM_STATE_ARM)
  561. {
  562. uint8_t buf[4];
  563. if ((retval = image_read_section(ctx->image, section,
  564. ctx->current_pc - ctx->image->sections[section].base_address,
  565. 4, buf, &size_read)) != ERROR_OK)
  566. {
  567. LOG_ERROR("error while reading instruction");
  568. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  569. }
  570. opcode = target_buffer_get_u32(ctx->target, buf);
  571. arm_evaluate_opcode(opcode, ctx->current_pc, instruction);
  572. }
  573. else if (ctx->core_state == ARM_STATE_THUMB)
  574. {
  575. uint8_t buf[2];
  576. if ((retval = image_read_section(ctx->image, section,
  577. ctx->current_pc - ctx->image->sections[section].base_address,
  578. 2, buf, &size_read)) != ERROR_OK)
  579. {
  580. LOG_ERROR("error while reading instruction");
  581. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  582. }
  583. opcode = target_buffer_get_u16(ctx->target, buf);
  584. thumb_evaluate_opcode(opcode, ctx->current_pc, instruction);
  585. }
  586. else if (ctx->core_state == ARM_STATE_JAZELLE)
  587. {
  588. LOG_ERROR("BUG: tracing of jazelle code not supported");
  589. return ERROR_FAIL;
  590. }
  591. else
  592. {
  593. LOG_ERROR("BUG: unknown core state encountered");
  594. return ERROR_FAIL;
  595. }
  596. return ERROR_OK;
  597. }
  598. static int etmv1_next_packet(struct etm_context *ctx, uint8_t *packet, int apo)
  599. {
  600. while (ctx->data_index < ctx->trace_depth)
  601. {
  602. /* if the caller specified an address packet offset, skip until the
  603. * we reach the n-th cycle marked with tracesync */
  604. if (apo > 0)
  605. {
  606. if (ctx->trace_data[ctx->data_index].flags & ETMV1_TRACESYNC_CYCLE)
  607. apo--;
  608. if (apo > 0)
  609. {
  610. ctx->data_index++;
  611. ctx->data_half = 0;
  612. }
  613. continue;
  614. }
  615. /* no tracedata output during a TD cycle
  616. * or in a trigger cycle */
  617. if ((ctx->trace_data[ctx->data_index].pipestat == STAT_TD)
  618. || (ctx->trace_data[ctx->data_index].flags & ETMV1_TRIGGER_CYCLE))
  619. {
  620. ctx->data_index++;
  621. ctx->data_half = 0;
  622. continue;
  623. }
  624. /* FIXME there are more port widths than these... */
  625. if ((ctx->control & ETM_PORT_WIDTH_MASK) == ETM_PORT_16BIT)
  626. {
  627. if (ctx->data_half == 0)
  628. {
  629. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  630. ctx->data_half = 1;
  631. }
  632. else
  633. {
  634. *packet = (ctx->trace_data[ctx->data_index].packet & 0xff00) >> 8;
  635. ctx->data_half = 0;
  636. ctx->data_index++;
  637. }
  638. }
  639. else if ((ctx->control & ETM_PORT_WIDTH_MASK) == ETM_PORT_8BIT)
  640. {
  641. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  642. ctx->data_index++;
  643. }
  644. else
  645. {
  646. /* on a 4-bit port, a packet will be output during two consecutive cycles */
  647. if (ctx->data_index > (ctx->trace_depth - 2))
  648. return -1;
  649. *packet = ctx->trace_data[ctx->data_index].packet & 0xf;
  650. *packet |= (ctx->trace_data[ctx->data_index + 1].packet & 0xf) << 4;
  651. ctx->data_index += 2;
  652. }
  653. return 0;
  654. }
  655. return -1;
  656. }
  657. static int etmv1_branch_address(struct etm_context *ctx)
  658. {
  659. int retval;
  660. uint8_t packet;
  661. int shift = 0;
  662. int apo;
  663. uint32_t i;
  664. /* quit analysis if less than two cycles are left in the trace
  665. * because we can't extract the APO */
  666. if (ctx->data_index > (ctx->trace_depth - 2))
  667. return -1;
  668. /* a BE could be output during an APO cycle, skip the current
  669. * and continue with the new one */
  670. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x4)
  671. return 1;
  672. if (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x4)
  673. return 2;
  674. /* address packet offset encoded in the next two cycles' pipestat bits */
  675. apo = ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x3;
  676. apo |= (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x3) << 2;
  677. /* count number of tracesync cycles between current pipe_index and data_index
  678. * i.e. the number of tracesyncs that data_index already passed by
  679. * to subtract them from the APO */
  680. for (i = ctx->pipe_index; i < ctx->data_index; i++)
  681. {
  682. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & ETMV1_TRACESYNC_CYCLE)
  683. apo--;
  684. }
  685. /* extract up to four 7-bit packets */
  686. do {
  687. if ((retval = etmv1_next_packet(ctx, &packet, (shift == 0) ? apo + 1 : 0)) != 0)
  688. return -1;
  689. ctx->last_branch &= ~(0x7f << shift);
  690. ctx->last_branch |= (packet & 0x7f) << shift;
  691. shift += 7;
  692. } while ((packet & 0x80) && (shift < 28));
  693. /* one last packet holding 4 bits of the address, plus the branch reason code */
  694. if ((shift == 28) && (packet & 0x80))
  695. {
  696. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  697. return -1;
  698. ctx->last_branch &= 0x0fffffff;
  699. ctx->last_branch |= (packet & 0x0f) << 28;
  700. ctx->last_branch_reason = (packet & 0x70) >> 4;
  701. shift += 4;
  702. }
  703. else
  704. {
  705. ctx->last_branch_reason = 0;
  706. }
  707. if (shift == 32)
  708. {
  709. ctx->pc_ok = 1;
  710. }
  711. /* if a full address was output, we might have branched into Jazelle state */
  712. if ((shift == 32) && (packet & 0x80))
  713. {
  714. ctx->core_state = ARM_STATE_JAZELLE;
  715. }
  716. else
  717. {
  718. /* if we didn't branch into Jazelle state, the current processor state is
  719. * encoded in bit 0 of the branch target address */
  720. if (ctx->last_branch & 0x1)
  721. {
  722. ctx->core_state = ARM_STATE_THUMB;
  723. ctx->last_branch &= ~0x1;
  724. }
  725. else
  726. {
  727. ctx->core_state = ARM_STATE_ARM;
  728. ctx->last_branch &= ~0x3;
  729. }
  730. }
  731. return 0;
  732. }
  733. static int etmv1_data(struct etm_context *ctx, int size, uint32_t *data)
  734. {
  735. int j;
  736. uint8_t buf[4];
  737. int retval;
  738. for (j = 0; j < size; j++)
  739. {
  740. if ((retval = etmv1_next_packet(ctx, &buf[j], 0)) != 0)
  741. return -1;
  742. }
  743. if (size == 8)
  744. {
  745. LOG_ERROR("TODO: add support for 64-bit values");
  746. return -1;
  747. }
  748. else if (size == 4)
  749. *data = target_buffer_get_u32(ctx->target, buf);
  750. else if (size == 2)
  751. *data = target_buffer_get_u16(ctx->target, buf);
  752. else if (size == 1)
  753. *data = buf[0];
  754. else
  755. return -1;
  756. return 0;
  757. }
  758. static int etmv1_analyze_trace(struct etm_context *ctx, struct command_context *cmd_ctx)
  759. {
  760. int retval;
  761. struct arm_instruction instruction;
  762. /* read the trace data if it wasn't read already */
  763. if (ctx->trace_depth == 0)
  764. ctx->capture_driver->read_trace(ctx);
  765. if (ctx->trace_depth == 0) {
  766. command_print(cmd_ctx, "Trace is empty.");
  767. return ERROR_OK;
  768. }
  769. /* start at the beginning of the captured trace */
  770. ctx->pipe_index = 0;
  771. ctx->data_index = 0;
  772. ctx->data_half = 0;
  773. /* neither the PC nor the data pointer are valid */
  774. ctx->pc_ok = 0;
  775. ctx->ptr_ok = 0;
  776. while (ctx->pipe_index < ctx->trace_depth)
  777. {
  778. uint8_t pipestat = ctx->trace_data[ctx->pipe_index].pipestat;
  779. uint32_t next_pc = ctx->current_pc;
  780. uint32_t old_data_index = ctx->data_index;
  781. uint32_t old_data_half = ctx->data_half;
  782. uint32_t old_index = ctx->pipe_index;
  783. uint32_t last_instruction = ctx->last_instruction;
  784. uint32_t cycles = 0;
  785. int current_pc_ok = ctx->pc_ok;
  786. if (ctx->trace_data[ctx->pipe_index].flags & ETMV1_TRIGGER_CYCLE)
  787. {
  788. command_print(cmd_ctx, "--- trigger ---");
  789. }
  790. /* instructions execute in IE/D or BE/D cycles */
  791. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  792. ctx->last_instruction = ctx->pipe_index;
  793. /* if we don't have a valid pc skip until we reach an indirect branch */
  794. if ((!ctx->pc_ok) && (pipestat != STAT_BE))
  795. {
  796. ctx->pipe_index++;
  797. continue;
  798. }
  799. /* any indirect branch could have interrupted instruction flow
  800. * - the branch reason code could indicate a trace discontinuity
  801. * - a branch to the exception vectors indicates an exception
  802. */
  803. if ((pipestat == STAT_BE) || (pipestat == STAT_BD))
  804. {
  805. /* backup current data index, to be able to consume the branch address
  806. * before examining data address and values
  807. */
  808. old_data_index = ctx->data_index;
  809. old_data_half = ctx->data_half;
  810. ctx->last_instruction = ctx->pipe_index;
  811. if ((retval = etmv1_branch_address(ctx)) != 0)
  812. {
  813. /* negative return value from etmv1_branch_address means we ran out of packets,
  814. * quit analysing the trace */
  815. if (retval < 0)
  816. break;
  817. /* a positive return values means the current branch was abandoned,
  818. * and a new branch was encountered in cycle ctx->pipe_index + retval;
  819. */
  820. LOG_WARNING("abandoned branch encountered, correctnes of analysis uncertain");
  821. ctx->pipe_index += retval;
  822. continue;
  823. }
  824. /* skip over APO cycles */
  825. ctx->pipe_index += 2;
  826. switch (ctx->last_branch_reason)
  827. {
  828. case 0x0: /* normal PC change */
  829. next_pc = ctx->last_branch;
  830. break;
  831. case 0x1: /* tracing enabled */
  832. command_print(cmd_ctx, "--- tracing enabled 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 0x2: /* trace restarted after FIFO overflow */
  838. command_print(cmd_ctx, "--- trace restarted after FIFO overflow 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 0x3: /* exit from debug state */
  844. command_print(cmd_ctx, "--- exit from debug state at 0x%8.8" PRIx32 " ---", ctx->last_branch);
  845. ctx->current_pc = ctx->last_branch;
  846. ctx->pipe_index++;
  847. continue;
  848. break;
  849. case 0x4: /* periodic synchronization point */
  850. next_pc = ctx->last_branch;
  851. /* if we had no valid PC prior to this synchronization point,
  852. * we have to move on with the next trace cycle
  853. */
  854. if (!current_pc_ok)
  855. {
  856. command_print(cmd_ctx, "--- periodic synchronization point at 0x%8.8" PRIx32 " ---", next_pc);
  857. ctx->current_pc = next_pc;
  858. ctx->pipe_index++;
  859. continue;
  860. }
  861. break;
  862. default: /* reserved */
  863. LOG_ERROR("BUG: branch reason code 0x%" PRIx32 " is reserved", ctx->last_branch_reason);
  864. return ERROR_FAIL;
  865. }
  866. /* if we got here the branch was a normal PC change
  867. * (or a periodic synchronization point, which means the same for that matter)
  868. * if we didn't accquire a complete PC continue with the next cycle
  869. */
  870. if (!ctx->pc_ok)
  871. continue;
  872. /* indirect branch to the exception vector means an exception occured */
  873. if ((ctx->last_branch <= 0x20)
  874. || ((ctx->last_branch >= 0xffff0000) && (ctx->last_branch <= 0xffff0020)))
  875. {
  876. if ((ctx->last_branch & 0xff) == 0x10)
  877. {
  878. command_print(cmd_ctx, "data abort");
  879. }
  880. else
  881. {
  882. command_print(cmd_ctx, "exception vector 0x%2.2" PRIx32 "", ctx->last_branch);
  883. ctx->current_pc = ctx->last_branch;
  884. ctx->pipe_index++;
  885. continue;
  886. }
  887. }
  888. }
  889. /* an instruction was executed (or not, depending on the condition flags)
  890. * retrieve it from the image for displaying */
  891. if (ctx->pc_ok && (pipestat != STAT_WT) && (pipestat != STAT_TD) &&
  892. !(((pipestat == STAT_BE) || (pipestat == STAT_BD)) &&
  893. ((ctx->last_branch_reason != 0x0) && (ctx->last_branch_reason != 0x4))))
  894. {
  895. if ((retval = etm_read_instruction(ctx, &instruction)) != ERROR_OK)
  896. {
  897. /* can't continue tracing with no image available */
  898. if (retval == ERROR_TRACE_IMAGE_UNAVAILABLE)
  899. {
  900. return retval;
  901. }
  902. else if (retval == ERROR_TRACE_INSTRUCTION_UNAVAILABLE)
  903. {
  904. /* TODO: handle incomplete images
  905. * for now we just quit the analsysis*/
  906. return retval;
  907. }
  908. }
  909. cycles = old_index - last_instruction;
  910. }
  911. if ((pipestat == STAT_ID) || (pipestat == STAT_BD))
  912. {
  913. uint32_t new_data_index = ctx->data_index;
  914. uint32_t new_data_half = ctx->data_half;
  915. /* in case of a branch with data, the branch target address was consumed before
  916. * we temporarily go back to the saved data index */
  917. if (pipestat == STAT_BD)
  918. {
  919. ctx->data_index = old_data_index;
  920. ctx->data_half = old_data_half;
  921. }
  922. if (ctx->control & ETM_CTRL_TRACE_ADDR)
  923. {
  924. uint8_t packet;
  925. int shift = 0;
  926. do {
  927. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  928. return ERROR_ETM_ANALYSIS_FAILED;
  929. ctx->last_ptr &= ~(0x7f << shift);
  930. ctx->last_ptr |= (packet & 0x7f) << shift;
  931. shift += 7;
  932. } while ((packet & 0x80) && (shift < 32));
  933. if (shift >= 32)
  934. ctx->ptr_ok = 1;
  935. if (ctx->ptr_ok)
  936. {
  937. command_print(cmd_ctx, "address: 0x%8.8" PRIx32 "", ctx->last_ptr);
  938. }
  939. }
  940. if (ctx->control & ETM_CTRL_TRACE_DATA)
  941. {
  942. if ((instruction.type == ARM_LDM) || (instruction.type == ARM_STM))
  943. {
  944. int i;
  945. for (i = 0; i < 16; i++)
  946. {
  947. if (instruction.info.load_store_multiple.register_list & (1 << i))
  948. {
  949. uint32_t data;
  950. if (etmv1_data(ctx, 4, &data) != 0)
  951. return ERROR_ETM_ANALYSIS_FAILED;
  952. command_print(cmd_ctx, "data: 0x%8.8" PRIx32 "", data);
  953. }
  954. }
  955. }
  956. else if ((instruction.type >= ARM_LDR) && (instruction.type <= ARM_STRH))
  957. {
  958. uint32_t data;
  959. if (etmv1_data(ctx, arm_access_size(&instruction), &data) != 0)
  960. return ERROR_ETM_ANALYSIS_FAILED;
  961. command_print(cmd_ctx, "data: 0x%8.8" PRIx32 "", data);
  962. }
  963. }
  964. /* restore data index after consuming BD address and data */
  965. if (pipestat == STAT_BD)
  966. {
  967. ctx->data_index = new_data_index;
  968. ctx->data_half = new_data_half;
  969. }
  970. }
  971. /* adjust PC */
  972. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  973. {
  974. if (((instruction.type == ARM_B) ||
  975. (instruction.type == ARM_BL) ||
  976. (instruction.type == ARM_BLX)) &&
  977. (instruction.info.b_bl_bx_blx.target_address != 0xffffffff))
  978. {
  979. next_pc = instruction.info.b_bl_bx_blx.target_address;
  980. }
  981. else
  982. {
  983. next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
  984. }
  985. }
  986. else if (pipestat == STAT_IN)
  987. {
  988. next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
  989. }
  990. if ((pipestat != STAT_TD) && (pipestat != STAT_WT))
  991. {
  992. char cycles_text[32] = "";
  993. /* if the trace was captured with cycle accurate tracing enabled,
  994. * output the number of cycles since the last executed instruction
  995. */
  996. if (ctx->control & ETM_CTRL_CYCLE_ACCURATE)
  997. {
  998. snprintf(cycles_text, 32, " (%i %s)",
  999. (int)cycles,
  1000. (cycles == 1) ? "cycle" : "cycles");
  1001. }
  1002. command_print(cmd_ctx, "%s%s%s",
  1003. instruction.text,
  1004. (pipestat == STAT_IN) ? " (not executed)" : "",
  1005. cycles_text);
  1006. ctx->current_pc = next_pc;
  1007. /* packets for an instruction don't start on or before the preceding
  1008. * functional pipestat (i.e. other than WT or TD)
  1009. */
  1010. if (ctx->data_index <= ctx->pipe_index)
  1011. {
  1012. ctx->data_index = ctx->pipe_index + 1;
  1013. ctx->data_half = 0;
  1014. }
  1015. }
  1016. ctx->pipe_index += 1;
  1017. }
  1018. return ERROR_OK;
  1019. }
  1020. static COMMAND_HELPER(handle_etm_tracemode_command_update,
  1021. uint32_t *mode)
  1022. {
  1023. uint32_t tracemode;
  1024. /* what parts of data access are traced? */
  1025. if (strcmp(CMD_ARGV[0], "none") == 0)
  1026. tracemode = 0;
  1027. else if (strcmp(CMD_ARGV[0], "data") == 0)
  1028. tracemode = ETM_CTRL_TRACE_DATA;
  1029. else if (strcmp(CMD_ARGV[0], "address") == 0)
  1030. tracemode = ETM_CTRL_TRACE_ADDR;
  1031. else if (strcmp(CMD_ARGV[0], "all") == 0)
  1032. tracemode = ETM_CTRL_TRACE_DATA | ETM_CTRL_TRACE_ADDR;
  1033. else
  1034. {
  1035. command_print(CMD_CTX, "invalid option '%s'", CMD_ARGV[0]);
  1036. return ERROR_INVALID_ARGUMENTS;
  1037. }
  1038. uint8_t context_id;
  1039. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], context_id);
  1040. switch (context_id)
  1041. {
  1042. case 0:
  1043. tracemode |= ETM_CTRL_CONTEXTID_NONE;
  1044. break;
  1045. case 8:
  1046. tracemode |= ETM_CTRL_CONTEXTID_8;
  1047. break;
  1048. case 16:
  1049. tracemode |= ETM_CTRL_CONTEXTID_16;
  1050. break;
  1051. case 32:
  1052. tracemode |= ETM_CTRL_CONTEXTID_32;
  1053. break;
  1054. default:
  1055. command_print(CMD_CTX, "invalid option '%s'", CMD_ARGV[1]);
  1056. return ERROR_INVALID_ARGUMENTS;
  1057. }
  1058. bool etmv1_cycle_accurate;
  1059. COMMAND_PARSE_ENABLE(CMD_ARGV[2], etmv1_cycle_accurate);
  1060. if (etmv1_cycle_accurate)
  1061. tracemode |= ETM_CTRL_CYCLE_ACCURATE;
  1062. bool etmv1_branch_output;
  1063. COMMAND_PARSE_ENABLE(CMD_ARGV[3], etmv1_branch_output);
  1064. if (etmv1_branch_output)
  1065. tracemode |= ETM_CTRL_BRANCH_OUTPUT;
  1066. /* IGNORED:
  1067. * - CPRT tracing (coprocessor register transfers)
  1068. * - debug request (causes debug entry on trigger)
  1069. * - stall on FIFOFULL (preventing tracedata lossage)
  1070. */
  1071. *mode = tracemode;
  1072. return ERROR_OK;
  1073. }
  1074. COMMAND_HANDLER(handle_etm_tracemode_command)
  1075. {
  1076. struct target *target = get_current_target(CMD_CTX);
  1077. struct arm *arm = target_to_arm(target);
  1078. struct etm_context *etm;
  1079. if (!is_arm(arm)) {
  1080. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1081. return ERROR_FAIL;
  1082. }
  1083. etm = arm->etm;
  1084. if (!etm) {
  1085. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1086. return ERROR_FAIL;
  1087. }
  1088. uint32_t tracemode = etm->control;
  1089. switch (CMD_ARGC)
  1090. {
  1091. case 0:
  1092. break;
  1093. case 4:
  1094. CALL_COMMAND_HANDLER(handle_etm_tracemode_command_update,
  1095. &tracemode);
  1096. break;
  1097. default:
  1098. command_print(CMD_CTX, "usage: tracemode "
  1099. "('none'|'data'|'address'|'all') "
  1100. "context_id_bits "
  1101. "('enable'|'disable') "
  1102. "('enable'|'disable')"
  1103. );
  1104. return ERROR_FAIL;
  1105. }
  1106. /**
  1107. * todo: fail if parameters were invalid for this hardware,
  1108. * or couldn't be written; display actual hardware state...
  1109. */
  1110. command_print(CMD_CTX, "current tracemode configuration:");
  1111. switch (tracemode & ETM_CTRL_TRACE_MASK)
  1112. {
  1113. default:
  1114. command_print(CMD_CTX, "data tracing: none");
  1115. break;
  1116. case ETM_CTRL_TRACE_DATA:
  1117. command_print(CMD_CTX, "data tracing: data only");
  1118. break;
  1119. case ETM_CTRL_TRACE_ADDR:
  1120. command_print(CMD_CTX, "data tracing: address only");
  1121. break;
  1122. case ETM_CTRL_TRACE_DATA | ETM_CTRL_TRACE_ADDR:
  1123. command_print(CMD_CTX, "data tracing: address and data");
  1124. break;
  1125. }
  1126. switch (tracemode & ETM_CTRL_CONTEXTID_MASK)
  1127. {
  1128. case ETM_CTRL_CONTEXTID_NONE:
  1129. command_print(CMD_CTX, "contextid tracing: none");
  1130. break;
  1131. case ETM_CTRL_CONTEXTID_8:
  1132. command_print(CMD_CTX, "contextid tracing: 8 bit");
  1133. break;
  1134. case ETM_CTRL_CONTEXTID_16:
  1135. command_print(CMD_CTX, "contextid tracing: 16 bit");
  1136. break;
  1137. case ETM_CTRL_CONTEXTID_32:
  1138. command_print(CMD_CTX, "contextid tracing: 32 bit");
  1139. break;
  1140. }
  1141. if (tracemode & ETM_CTRL_CYCLE_ACCURATE)
  1142. {
  1143. command_print(CMD_CTX, "cycle-accurate tracing enabled");
  1144. }
  1145. else
  1146. {
  1147. command_print(CMD_CTX, "cycle-accurate tracing disabled");
  1148. }
  1149. if (tracemode & ETM_CTRL_BRANCH_OUTPUT)
  1150. {
  1151. command_print(CMD_CTX, "full branch address output enabled");
  1152. }
  1153. else
  1154. {
  1155. command_print(CMD_CTX, "full branch address output disabled");
  1156. }
  1157. #define TRACEMODE_MASK ( \
  1158. ETM_CTRL_CONTEXTID_MASK \
  1159. | ETM_CTRL_BRANCH_OUTPUT \
  1160. | ETM_CTRL_CYCLE_ACCURATE \
  1161. | ETM_CTRL_TRACE_MASK \
  1162. )
  1163. /* only update ETM_CTRL register if tracemode changed */
  1164. if ((etm->control & TRACEMODE_MASK) != tracemode)
  1165. {
  1166. struct reg *etm_ctrl_reg;
  1167. etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
  1168. if (!etm_ctrl_reg)
  1169. return ERROR_FAIL;
  1170. etm->control &= ~TRACEMODE_MASK;
  1171. etm->control |= tracemode & TRACEMODE_MASK;
  1172. buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
  1173. etm_store_reg(etm_ctrl_reg);
  1174. /* invalidate old trace data */
  1175. etm->capture_status = TRACE_IDLE;
  1176. if (etm->trace_depth > 0)
  1177. {
  1178. free(etm->trace_data);
  1179. etm->trace_data = NULL;
  1180. }
  1181. etm->trace_depth = 0;
  1182. }
  1183. #undef TRACEMODE_MASK
  1184. return ERROR_OK;
  1185. }
  1186. COMMAND_HANDLER(handle_etm_config_command)
  1187. {
  1188. struct target *target;
  1189. struct arm *arm;
  1190. uint32_t portmode = 0x0;
  1191. struct etm_context *etm_ctx;
  1192. int i;
  1193. if (CMD_ARGC != 5)
  1194. return ERROR_COMMAND_SYNTAX_ERROR;
  1195. target = get_target(CMD_ARGV[0]);
  1196. if (!target)
  1197. {
  1198. LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
  1199. return ERROR_FAIL;
  1200. }
  1201. arm = target_to_arm(target);
  1202. if (!is_arm(arm)) {
  1203. command_print(CMD_CTX, "target '%s' is '%s'; not an ARM",
  1204. target_name(target),
  1205. target_type_name(target));
  1206. return ERROR_FAIL;
  1207. }
  1208. /* FIXME for ETMv3.0 and above -- and we don't yet know what ETM
  1209. * version we'll be using!! -- so we can't know how to validate
  1210. * params yet. "etm config" should likely be *AFTER* hookup...
  1211. *
  1212. * - Many more widths might be supported ... and we can easily
  1213. * check whether our setting "took".
  1214. *
  1215. * - The "clock" and "mode" bits are interpreted differently.
  1216. * See ARM IHI 0014O table 2-17 for the old behavior, and
  1217. * table 2-18 for the new. With ETB it's best to specify
  1218. * "normal full" ...
  1219. */
  1220. uint8_t port_width;
  1221. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], port_width);
  1222. switch (port_width)
  1223. {
  1224. /* before ETMv3.0 */
  1225. case 4:
  1226. portmode |= ETM_PORT_4BIT;
  1227. break;
  1228. case 8:
  1229. portmode |= ETM_PORT_8BIT;
  1230. break;
  1231. case 16:
  1232. portmode |= ETM_PORT_16BIT;
  1233. break;
  1234. /* ETMv3.0 and later*/
  1235. case 24:
  1236. portmode |= ETM_PORT_24BIT;
  1237. break;
  1238. case 32:
  1239. portmode |= ETM_PORT_32BIT;
  1240. break;
  1241. case 48:
  1242. portmode |= ETM_PORT_48BIT;
  1243. break;
  1244. case 64:
  1245. portmode |= ETM_PORT_64BIT;
  1246. break;
  1247. case 1:
  1248. portmode |= ETM_PORT_1BIT;
  1249. break;
  1250. case 2:
  1251. portmode |= ETM_PORT_2BIT;
  1252. break;
  1253. default:
  1254. command_print(CMD_CTX,
  1255. "unsupported ETM port width '%s'", CMD_ARGV[1]);
  1256. return ERROR_FAIL;
  1257. }
  1258. if (strcmp("normal", CMD_ARGV[2]) == 0)
  1259. {
  1260. portmode |= ETM_PORT_NORMAL;
  1261. }
  1262. else if (strcmp("multiplexed", CMD_ARGV[2]) == 0)
  1263. {
  1264. portmode |= ETM_PORT_MUXED;
  1265. }
  1266. else if (strcmp("demultiplexed", CMD_ARGV[2]) == 0)
  1267. {
  1268. portmode |= ETM_PORT_DEMUXED;
  1269. }
  1270. else
  1271. {
  1272. command_print(CMD_CTX, "unsupported ETM port mode '%s', must be 'normal', 'multiplexed' or 'demultiplexed'", CMD_ARGV[2]);
  1273. return ERROR_FAIL;
  1274. }
  1275. if (strcmp("half", CMD_ARGV[3]) == 0)
  1276. {
  1277. portmode |= ETM_PORT_HALF_CLOCK;
  1278. }
  1279. else if (strcmp("full", CMD_ARGV[3]) == 0)
  1280. {
  1281. portmode |= ETM_PORT_FULL_CLOCK;
  1282. }
  1283. else
  1284. {
  1285. command_print(CMD_CTX, "unsupported ETM port clocking '%s', must be 'full' or 'half'", CMD_ARGV[3]);
  1286. return ERROR_FAIL;
  1287. }
  1288. etm_ctx = calloc(1, sizeof(struct etm_context));
  1289. if (!etm_ctx) {
  1290. LOG_DEBUG("out of memory");
  1291. return ERROR_FAIL;
  1292. }
  1293. for (i = 0; etm_capture_drivers[i]; i++)
  1294. {
  1295. if (strcmp(CMD_ARGV[4], etm_capture_drivers[i]->name) == 0)
  1296. {
  1297. int retval = register_commands(CMD_CTX, NULL,
  1298. etm_capture_drivers[i]->commands);
  1299. if (ERROR_OK != retval)
  1300. {
  1301. free(etm_ctx);
  1302. return retval;
  1303. }
  1304. etm_ctx->capture_driver = etm_capture_drivers[i];
  1305. break;
  1306. }
  1307. }
  1308. if (!etm_capture_drivers[i])
  1309. {
  1310. /* no supported capture driver found, don't register an ETM */
  1311. free(etm_ctx);
  1312. LOG_ERROR("trace capture driver '%s' not found", CMD_ARGV[4]);
  1313. return ERROR_FAIL;
  1314. }
  1315. etm_ctx->target = target;
  1316. etm_ctx->trace_data = NULL;
  1317. etm_ctx->control = portmode;
  1318. etm_ctx->core_state = ARM_STATE_ARM;
  1319. arm->etm = etm_ctx;
  1320. return etm_register_user_commands(CMD_CTX);
  1321. }
  1322. COMMAND_HANDLER(handle_etm_info_command)
  1323. {
  1324. struct target *target;
  1325. struct arm *arm;
  1326. struct etm_context *etm;
  1327. struct reg *etm_sys_config_reg;
  1328. int max_port_size;
  1329. uint32_t config;
  1330. target = get_current_target(CMD_CTX);
  1331. arm = target_to_arm(target);
  1332. if (!is_arm(arm))
  1333. {
  1334. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1335. return ERROR_FAIL;
  1336. }
  1337. etm = arm->etm;
  1338. if (!etm)
  1339. {
  1340. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1341. return ERROR_FAIL;
  1342. }
  1343. command_print(CMD_CTX, "ETM v%d.%d",
  1344. etm->bcd_vers >> 4, etm->bcd_vers & 0xf);
  1345. command_print(CMD_CTX, "pairs of address comparators: %i",
  1346. (int) (etm->config >> 0) & 0x0f);
  1347. command_print(CMD_CTX, "data comparators: %i",
  1348. (int) (etm->config >> 4) & 0x0f);
  1349. command_print(CMD_CTX, "memory map decoders: %i",
  1350. (int) (etm->config >> 8) & 0x1f);
  1351. command_print(CMD_CTX, "number of counters: %i",
  1352. (int) (etm->config >> 13) & 0x07);
  1353. command_print(CMD_CTX, "sequencer %spresent",
  1354. (int) (etm->config & (1 << 16)) ? "" : "not ");
  1355. command_print(CMD_CTX, "number of ext. inputs: %i",
  1356. (int) (etm->config >> 17) & 0x07);
  1357. command_print(CMD_CTX, "number of ext. outputs: %i",
  1358. (int) (etm->config >> 20) & 0x07);
  1359. command_print(CMD_CTX, "FIFO full %spresent",
  1360. (int) (etm->config & (1 << 23)) ? "" : "not ");
  1361. if (etm->bcd_vers < 0x20)
  1362. command_print(CMD_CTX, "protocol version: %i",
  1363. (int) (etm->config >> 28) & 0x07);
  1364. else {
  1365. command_print(CMD_CTX,
  1366. "coprocessor and memory access %ssupported",
  1367. (etm->config & (1 << 26)) ? "" : "not ");
  1368. command_print(CMD_CTX, "trace start/stop %spresent",
  1369. (etm->config & (1 << 26)) ? "" : "not ");
  1370. command_print(CMD_CTX, "number of context comparators: %i",
  1371. (int) (etm->config >> 24) & 0x03);
  1372. }
  1373. /* SYS_CONFIG isn't present before ETMv1.2 */
  1374. etm_sys_config_reg = etm_reg_lookup(etm, ETM_SYS_CONFIG);
  1375. if (!etm_sys_config_reg)
  1376. return ERROR_OK;
  1377. etm_get_reg(etm_sys_config_reg);
  1378. config = buf_get_u32(etm_sys_config_reg->value, 0, 32);
  1379. LOG_DEBUG("ETM SYS CONFIG %08x", (unsigned) config);
  1380. max_port_size = config & 0x7;
  1381. if (etm->bcd_vers >= 0x30)
  1382. max_port_size |= (config >> 6) & 0x08;
  1383. switch (max_port_size)
  1384. {
  1385. /* before ETMv3.0 */
  1386. case 0:
  1387. max_port_size = 4;
  1388. break;
  1389. case 1:
  1390. max_port_size = 8;
  1391. break;
  1392. case 2:
  1393. max_port_size = 16;
  1394. break;
  1395. /* ETMv3.0 and later*/
  1396. case 3:
  1397. max_port_size = 24;
  1398. break;
  1399. case 4:
  1400. max_port_size = 32;
  1401. break;
  1402. case 5:
  1403. max_port_size = 48;
  1404. break;
  1405. case 6:
  1406. max_port_size = 64;
  1407. break;
  1408. case 8:
  1409. max_port_size = 1;
  1410. break;
  1411. case 9:
  1412. max_port_size = 2;
  1413. break;
  1414. default:
  1415. LOG_ERROR("Illegal max_port_size");
  1416. return ERROR_FAIL;
  1417. }
  1418. command_print(CMD_CTX, "max. port size: %i", max_port_size);
  1419. if (etm->bcd_vers < 0x30) {
  1420. command_print(CMD_CTX, "half-rate clocking %ssupported",
  1421. (config & (1 << 3)) ? "" : "not ");
  1422. command_print(CMD_CTX, "full-rate clocking %ssupported",
  1423. (config & (1 << 4)) ? "" : "not ");
  1424. command_print(CMD_CTX, "normal trace format %ssupported",
  1425. (config & (1 << 5)) ? "" : "not ");
  1426. command_print(CMD_CTX, "multiplex trace format %ssupported",
  1427. (config & (1 << 6)) ? "" : "not ");
  1428. command_print(CMD_CTX, "demultiplex trace format %ssupported",
  1429. (config & (1 << 7)) ? "" : "not ");
  1430. } else {
  1431. /* REVISIT show which size and format are selected ... */
  1432. command_print(CMD_CTX, "current port size %ssupported",
  1433. (config & (1 << 10)) ? "" : "not ");
  1434. command_print(CMD_CTX, "current trace format %ssupported",
  1435. (config & (1 << 11)) ? "" : "not ");
  1436. }
  1437. if (etm->bcd_vers >= 0x21)
  1438. command_print(CMD_CTX, "fetch comparisons %ssupported",
  1439. (config & (1 << 17)) ? "not " : "");
  1440. command_print(CMD_CTX, "FIFO full %ssupported",
  1441. (config & (1 << 8)) ? "" : "not ");
  1442. return ERROR_OK;
  1443. }
  1444. COMMAND_HANDLER(handle_etm_status_command)
  1445. {
  1446. struct target *target;
  1447. struct arm *arm;
  1448. struct etm_context *etm;
  1449. trace_status_t trace_status;
  1450. target = get_current_target(CMD_CTX);
  1451. arm = target_to_arm(target);
  1452. if (!is_arm(arm))
  1453. {
  1454. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1455. return ERROR_FAIL;
  1456. }
  1457. etm = arm->etm;
  1458. if (!etm)
  1459. {
  1460. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1461. return ERROR_FAIL;
  1462. }
  1463. /* ETM status */
  1464. if (etm->bcd_vers >= 0x11) {
  1465. struct reg *reg;
  1466. reg = etm_reg_lookup(etm, ETM_STATUS);
  1467. if (!reg)
  1468. return ERROR_FAIL;
  1469. if (etm_get_reg(reg) == ERROR_OK) {
  1470. unsigned s = buf_get_u32(reg->value, 0, reg->size);
  1471. command_print(CMD_CTX, "etm: %s%s%s%s",
  1472. /* bit(1) == progbit */
  1473. (etm->bcd_vers >= 0x12)
  1474. ? ((s & (1 << 1))
  1475. ? "disabled" : "enabled")
  1476. : "?",
  1477. ((s & (1 << 3)) && etm->bcd_vers >= 0x31)
  1478. ? " triggered" : "",
  1479. ((s & (1 << 2)) && etm->bcd_vers >= 0x12)
  1480. ? " start/stop" : "",
  1481. ((s & (1 << 0)) && etm->bcd_vers >= 0x11)
  1482. ? " untraced-overflow" : "");
  1483. } /* else ignore and try showing trace port status */
  1484. }
  1485. /* Trace Port Driver status */
  1486. trace_status = etm->capture_driver->status(etm);
  1487. if (trace_status == TRACE_IDLE)
  1488. {
  1489. command_print(CMD_CTX, "%s: idle", etm->capture_driver->name);
  1490. }
  1491. else
  1492. {
  1493. static char *completed = " completed";
  1494. static char *running = " is running";
  1495. static char *overflowed = ", overflowed";
  1496. static char *triggered = ", triggered";
  1497. command_print(CMD_CTX, "%s: trace collection%s%s%s",
  1498. etm->capture_driver->name,
  1499. (trace_status & TRACE_RUNNING) ? running : completed,
  1500. (trace_status & TRACE_OVERFLOWED) ? overflowed : "",
  1501. (trace_status & TRACE_TRIGGERED) ? triggered : "");
  1502. if (etm->trace_depth > 0)
  1503. {
  1504. command_print(CMD_CTX, "%i frames of trace data read",
  1505. (int)(etm->trace_depth));
  1506. }
  1507. }
  1508. return ERROR_OK;
  1509. }
  1510. COMMAND_HANDLER(handle_etm_image_command)
  1511. {
  1512. struct target *target;
  1513. struct arm *arm;
  1514. struct etm_context *etm_ctx;
  1515. if (CMD_ARGC < 1)
  1516. {
  1517. command_print(CMD_CTX, "usage: etm image <file> [base address] [type]");
  1518. return ERROR_FAIL;
  1519. }
  1520. target = get_current_target(CMD_CTX);
  1521. arm = target_to_arm(target);
  1522. if (!is_arm(arm))
  1523. {
  1524. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1525. return ERROR_FAIL;
  1526. }
  1527. etm_ctx = arm->etm;
  1528. if (!etm_ctx)
  1529. {
  1530. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1531. return ERROR_FAIL;
  1532. }
  1533. if (etm_ctx->image)
  1534. {
  1535. image_close(etm_ctx->image);
  1536. free(etm_ctx->image);
  1537. command_print(CMD_CTX, "previously loaded image found and closed");
  1538. }
  1539. etm_ctx->image = malloc(sizeof(struct image));
  1540. etm_ctx->image->base_address_set = 0;
  1541. etm_ctx->image->start_address_set = 0;
  1542. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1543. if (CMD_ARGC >= 2)
  1544. {
  1545. etm_ctx->image->base_address_set = 1;
  1546. COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], etm_ctx->image->base_address);
  1547. }
  1548. else
  1549. {
  1550. etm_ctx->image->base_address_set = 0;
  1551. }
  1552. if (image_open(etm_ctx->image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
  1553. {
  1554. free(etm_ctx->image);
  1555. etm_ctx->image = NULL;
  1556. return ERROR_FAIL;
  1557. }
  1558. return ERROR_OK;
  1559. }
  1560. COMMAND_HANDLER(handle_etm_dump_command)
  1561. {
  1562. struct fileio file;
  1563. struct target *target;
  1564. struct arm *arm;
  1565. struct etm_context *etm_ctx;
  1566. uint32_t i;
  1567. if (CMD_ARGC != 1)
  1568. {
  1569. command_print(CMD_CTX, "usage: etm dump <file>");
  1570. return ERROR_FAIL;
  1571. }
  1572. target = get_current_target(CMD_CTX);
  1573. arm = target_to_arm(target);
  1574. if (!is_arm(arm))
  1575. {
  1576. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1577. return ERROR_FAIL;
  1578. }
  1579. etm_ctx = arm->etm;
  1580. if (!etm_ctx)
  1581. {
  1582. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1583. return ERROR_FAIL;
  1584. }
  1585. if (etm_ctx->capture_driver->status == TRACE_IDLE)
  1586. {
  1587. command_print(CMD_CTX, "trace capture wasn't enabled, no trace data captured");
  1588. return ERROR_OK;
  1589. }
  1590. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1591. {
  1592. /* TODO: if on-the-fly capture is to be supported, this needs to be changed */
  1593. command_print(CMD_CTX, "trace capture not completed");
  1594. return ERROR_FAIL;
  1595. }
  1596. /* read the trace data if it wasn't read already */
  1597. if (etm_ctx->trace_depth == 0)
  1598. etm_ctx->capture_driver->read_trace(etm_ctx);
  1599. if (fileio_open(&file, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1600. {
  1601. return ERROR_FAIL;
  1602. }
  1603. fileio_write_u32(&file, etm_ctx->capture_status);
  1604. fileio_write_u32(&file, etm_ctx->control);
  1605. fileio_write_u32(&file, etm_ctx->trace_depth);
  1606. for (i = 0; i < etm_ctx->trace_depth; i++)
  1607. {
  1608. fileio_write_u32(&file, etm_ctx->trace_data[i].pipestat);
  1609. fileio_write_u32(&file, etm_ctx->trace_data[i].packet);
  1610. fileio_write_u32(&file, etm_ctx->trace_data[i].flags);
  1611. }
  1612. fileio_close(&file);
  1613. return ERROR_OK;
  1614. }
  1615. COMMAND_HANDLER(handle_etm_load_command)
  1616. {
  1617. struct fileio file;
  1618. struct target *target;
  1619. struct arm *arm;
  1620. struct etm_context *etm_ctx;
  1621. uint32_t i;
  1622. if (CMD_ARGC != 1)
  1623. {
  1624. command_print(CMD_CTX, "usage: etm load <file>");
  1625. return ERROR_FAIL;
  1626. }
  1627. target = get_current_target(CMD_CTX);
  1628. arm = target_to_arm(target);
  1629. if (!is_arm(arm))
  1630. {
  1631. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1632. return ERROR_FAIL;
  1633. }
  1634. etm_ctx = arm->etm;
  1635. if (!etm_ctx)
  1636. {
  1637. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1638. return ERROR_FAIL;
  1639. }
  1640. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1641. {
  1642. command_print(CMD_CTX, "trace capture running, stop first");
  1643. return ERROR_FAIL;
  1644. }
  1645. if (fileio_open(&file, CMD_ARGV[0], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  1646. {
  1647. return ERROR_FAIL;
  1648. }
  1649. int filesize;
  1650. int retval = fileio_size(&file, &filesize);
  1651. if (retval != ERROR_OK)
  1652. {
  1653. fileio_close(&file);
  1654. return retval;
  1655. }
  1656. if (filesize % 4)
  1657. {
  1658. command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data");
  1659. fileio_close(&file);
  1660. return ERROR_FAIL;
  1661. }
  1662. if (etm_ctx->trace_depth > 0)
  1663. {
  1664. free(etm_ctx->trace_data);
  1665. etm_ctx->trace_data = NULL;
  1666. }
  1667. {
  1668. uint32_t tmp;
  1669. fileio_read_u32(&file, &tmp); etm_ctx->capture_status = tmp;
  1670. fileio_read_u32(&file, &tmp); etm_ctx->control = tmp;
  1671. fileio_read_u32(&file, &etm_ctx->trace_depth);
  1672. }
  1673. etm_ctx->trace_data = malloc(sizeof(struct etmv1_trace_data) * etm_ctx->trace_depth);
  1674. if (etm_ctx->trace_data == NULL)
  1675. {
  1676. command_print(CMD_CTX, "not enough memory to perform operation");
  1677. fileio_close(&file);
  1678. return ERROR_FAIL;
  1679. }
  1680. for (i = 0; i < etm_ctx->trace_depth; i++)
  1681. {
  1682. uint32_t pipestat, packet, flags;
  1683. fileio_read_u32(&file, &pipestat);
  1684. fileio_read_u32(&file, &packet);
  1685. fileio_read_u32(&file, &flags);
  1686. etm_ctx->trace_data[i].pipestat = pipestat & 0xff;
  1687. etm_ctx->trace_data[i].packet = packet & 0xffff;
  1688. etm_ctx->trace_data[i].flags = flags;
  1689. }
  1690. fileio_close(&file);
  1691. return ERROR_OK;
  1692. }
  1693. COMMAND_HANDLER(handle_etm_start_command)
  1694. {
  1695. struct target *target;
  1696. struct arm *arm;
  1697. struct etm_context *etm_ctx;
  1698. struct reg *etm_ctrl_reg;
  1699. target = get_current_target(CMD_CTX);
  1700. arm = target_to_arm(target);
  1701. if (!is_arm(arm))
  1702. {
  1703. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1704. return ERROR_FAIL;
  1705. }
  1706. etm_ctx = arm->etm;
  1707. if (!etm_ctx)
  1708. {
  1709. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1710. return ERROR_FAIL;
  1711. }
  1712. /* invalidate old tracing data */
  1713. etm_ctx->capture_status = TRACE_IDLE;
  1714. if (etm_ctx->trace_depth > 0)
  1715. {
  1716. free(etm_ctx->trace_data);
  1717. etm_ctx->trace_data = NULL;
  1718. }
  1719. etm_ctx->trace_depth = 0;
  1720. etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
  1721. if (!etm_ctrl_reg)
  1722. return ERROR_FAIL;
  1723. etm_get_reg(etm_ctrl_reg);
  1724. /* Clear programming bit (10), set port selection bit (11) */
  1725. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x2);
  1726. etm_store_reg(etm_ctrl_reg);
  1727. jtag_execute_queue();
  1728. etm_ctx->capture_driver->start_capture(etm_ctx);
  1729. return ERROR_OK;
  1730. }
  1731. COMMAND_HANDLER(handle_etm_stop_command)
  1732. {
  1733. struct target *target;
  1734. struct arm *arm;
  1735. struct etm_context *etm_ctx;
  1736. struct reg *etm_ctrl_reg;
  1737. target = get_current_target(CMD_CTX);
  1738. arm = target_to_arm(target);
  1739. if (!is_arm(arm))
  1740. {
  1741. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1742. return ERROR_FAIL;
  1743. }
  1744. etm_ctx = arm->etm;
  1745. if (!etm_ctx)
  1746. {
  1747. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1748. return ERROR_FAIL;
  1749. }
  1750. etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
  1751. if (!etm_ctrl_reg)
  1752. return ERROR_FAIL;
  1753. etm_get_reg(etm_ctrl_reg);
  1754. /* Set programming bit (10), clear port selection bit (11) */
  1755. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x1);
  1756. etm_store_reg(etm_ctrl_reg);
  1757. jtag_execute_queue();
  1758. etm_ctx->capture_driver->stop_capture(etm_ctx);
  1759. return ERROR_OK;
  1760. }
  1761. COMMAND_HANDLER(handle_etm_trigger_debug_command)
  1762. {
  1763. struct target *target;
  1764. struct arm *arm;
  1765. struct etm_context *etm;
  1766. target = get_current_target(CMD_CTX);
  1767. arm = target_to_arm(target);
  1768. if (!is_arm(arm))
  1769. {
  1770. command_print(CMD_CTX, "ETM: %s isn't an ARM",
  1771. target_name(target));
  1772. return ERROR_FAIL;
  1773. }
  1774. etm = arm->etm;
  1775. if (!etm)
  1776. {
  1777. command_print(CMD_CTX, "ETM: no ETM configured for %s",
  1778. target_name(target));
  1779. return ERROR_FAIL;
  1780. }
  1781. if (CMD_ARGC == 1) {
  1782. struct reg *etm_ctrl_reg;
  1783. bool dbgrq;
  1784. etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
  1785. if (!etm_ctrl_reg)
  1786. return ERROR_FAIL;
  1787. COMMAND_PARSE_ENABLE(CMD_ARGV[0], dbgrq);
  1788. if (dbgrq)
  1789. etm->control |= ETM_CTRL_DBGRQ;
  1790. else
  1791. etm->control &= ~ETM_CTRL_DBGRQ;
  1792. /* etm->control will be written to hardware
  1793. * the next time an "etm start" is issued.
  1794. */
  1795. buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
  1796. }
  1797. command_print(CMD_CTX, "ETM: %s debug halt",
  1798. (etm->control & ETM_CTRL_DBGRQ)
  1799. ? "triggers"
  1800. : "does not trigger");
  1801. return ERROR_OK;
  1802. }
  1803. COMMAND_HANDLER(handle_etm_analyze_command)
  1804. {
  1805. struct target *target;
  1806. struct arm *arm;
  1807. struct etm_context *etm_ctx;
  1808. int retval;
  1809. target = get_current_target(CMD_CTX);
  1810. arm = target_to_arm(target);
  1811. if (!is_arm(arm))
  1812. {
  1813. command_print(CMD_CTX, "ETM: current target isn't an ARM");
  1814. return ERROR_FAIL;
  1815. }
  1816. etm_ctx = arm->etm;
  1817. if (!etm_ctx)
  1818. {
  1819. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  1820. return ERROR_FAIL;
  1821. }
  1822. if ((retval = etmv1_analyze_trace(etm_ctx, CMD_CTX)) != ERROR_OK)
  1823. {
  1824. /* FIX! error should be reported inside etmv1_analyze_trace() */
  1825. switch (retval)
  1826. {
  1827. case ERROR_ETM_ANALYSIS_FAILED:
  1828. command_print(CMD_CTX, "further analysis failed (corrupted trace data or just end of data");
  1829. break;
  1830. case ERROR_TRACE_INSTRUCTION_UNAVAILABLE:
  1831. command_print(CMD_CTX, "no instruction for current address available, analysis aborted");
  1832. break;
  1833. case ERROR_TRACE_IMAGE_UNAVAILABLE:
  1834. command_print(CMD_CTX, "no image available for trace analysis");
  1835. break;
  1836. default:
  1837. command_print(CMD_CTX, "unknown error");
  1838. }
  1839. }
  1840. return retval;
  1841. }
  1842. static const struct command_registration etm_config_command_handlers[] = {
  1843. {
  1844. /* NOTE: with ADIv5, ETMs are accessed by DAP operations,
  1845. * possibly over SWD, not JTAG scanchain 6 of 'target'.
  1846. *
  1847. * Also, these parameters don't match ETM v3+ modules...
  1848. */
  1849. .name = "config",
  1850. .handler = handle_etm_config_command,
  1851. .mode = COMMAND_CONFIG,
  1852. .help = "Set up ETM output port.",
  1853. .usage = "target port_width port_mode clocking capture_driver",
  1854. },
  1855. COMMAND_REGISTRATION_DONE
  1856. };
  1857. const struct command_registration etm_command_handlers[] = {
  1858. {
  1859. .name = "etm",
  1860. .mode = COMMAND_ANY,
  1861. .help = "Emebdded Trace Macrocell command group",
  1862. .chain = etm_config_command_handlers,
  1863. },
  1864. COMMAND_REGISTRATION_DONE
  1865. };
  1866. static const struct command_registration etm_exec_command_handlers[] = {
  1867. {
  1868. .name = "tracemode",
  1869. .handler = handle_etm_tracemode_command,
  1870. .mode = COMMAND_EXEC,
  1871. .help = "configure/display trace mode",
  1872. .usage = "('none'|'data'|'address'|'all') "
  1873. "context_id_bits "
  1874. "['enable'|'disable'] "
  1875. "['enable'|'disable']",
  1876. },
  1877. {
  1878. .name = "info",
  1879. .handler = handle_etm_info_command,
  1880. .mode = COMMAND_EXEC,
  1881. .help = "display info about the current target's ETM",
  1882. },
  1883. {
  1884. .name = "status",
  1885. .handler = handle_etm_status_command,
  1886. .mode = COMMAND_EXEC,
  1887. .help = "display current target's ETM status",
  1888. },
  1889. {
  1890. .name = "start",
  1891. .handler = handle_etm_start_command,
  1892. .mode = COMMAND_EXEC,
  1893. .help = "start ETM trace collection",
  1894. },
  1895. {
  1896. .name = "stop",
  1897. .handler = handle_etm_stop_command,
  1898. .mode = COMMAND_EXEC,
  1899. .help = "stop ETM trace collection",
  1900. },
  1901. {
  1902. .name = "trigger_debug",
  1903. .handler = handle_etm_trigger_debug_command,
  1904. .mode = COMMAND_EXEC,
  1905. .help = "enable/disable debug entry on trigger",
  1906. .usage = "['enable'|'disable']",
  1907. },
  1908. {
  1909. .name = "analyze",
  1910. .handler = handle_etm_analyze_command,
  1911. .mode = COMMAND_EXEC,
  1912. .help = "analyze collected ETM trace",
  1913. },
  1914. {
  1915. .name = "image",
  1916. .handler = handle_etm_image_command,
  1917. .mode = COMMAND_EXEC,
  1918. .help = "load image from file with optional offset",
  1919. .usage = "filename [offset]",
  1920. },
  1921. {
  1922. .name = "dump",
  1923. .handler = handle_etm_dump_command,
  1924. .mode = COMMAND_EXEC,
  1925. .help = "dump captured trace data to file",
  1926. .usage = "filename",
  1927. },
  1928. {
  1929. .name = "load",
  1930. .handler = handle_etm_load_command,
  1931. .mode = COMMAND_EXEC,
  1932. .help = "load trace data for analysis <file>",
  1933. },
  1934. COMMAND_REGISTRATION_DONE
  1935. };
  1936. static int etm_register_user_commands(struct command_context *cmd_ctx)
  1937. {
  1938. struct command *etm_cmd = command_find_in_context(cmd_ctx, "etm");
  1939. return register_commands(cmd_ctx, etm_cmd, etm_exec_command_handlers);
  1940. }