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