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.
 
 
 
 
 
 

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