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.
 
 
 
 
 
 

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