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  break;
937  case 0x2: /* trace restarted after FIFO overflow */
939  "--- trace restarted after FIFO overflow at 0x%8.8" PRIx32 " ---",
940  ctx->last_branch);
941  ctx->current_pc = ctx->last_branch;
942  ctx->pipe_index++;
943  continue;
944  break;
945  case 0x3: /* exit from debug state */
947  "--- exit from debug state at 0x%8.8" PRIx32 " ---",
948  ctx->last_branch);
949  ctx->current_pc = ctx->last_branch;
950  ctx->pipe_index++;
951  continue;
952  break;
953  case 0x4: /* periodic synchronization point */
954  next_pc = ctx->last_branch;
955  /* if we had no valid PC prior to this synchronization point,
956  * we have to move on with the next trace cycle
957  */
958  if (!current_pc_ok) {
960  "--- periodic synchronization point at 0x%8.8" PRIx32 " ---",
961  next_pc);
962  ctx->current_pc = next_pc;
963  ctx->pipe_index++;
964  continue;
965  }
966  break;
967  default: /* reserved */
968  LOG_ERROR(
969  "BUG: branch reason code 0x%" PRIx32 " is reserved",
970  ctx->last_branch_reason);
971  return ERROR_FAIL;
972  }
973 
974  /* if we got here the branch was a normal PC change
975  * (or a periodic synchronization point, which means the same for that matter)
976  * if we didn't acquire a complete PC continue with the next cycle
977  */
978  if (!ctx->pc_ok)
979  continue;
980 
981  /* indirect branch to the exception vector means an exception occurred */
982  if ((ctx->last_branch <= 0x20)
983  || ((ctx->last_branch >= 0xffff0000) &&
984  (ctx->last_branch <= 0xffff0020))) {
985  if ((ctx->last_branch & 0xff) == 0x10)
986  command_print(cmd, "data abort");
987  else {
989  "exception vector 0x%2.2" PRIx32 "",
990  ctx->last_branch);
991  ctx->current_pc = ctx->last_branch;
992  ctx->pipe_index++;
993  continue;
994  }
995  }
996  }
997 
998  /* an instruction was executed (or not, depending on the condition flags)
999  * retrieve it from the image for displaying */
1000  if (ctx->pc_ok && (pipestat != STAT_WT) && (pipestat != STAT_TD) &&
1001  !(((pipestat == STAT_BE) || (pipestat == STAT_BD)) &&
1002  ((ctx->last_branch_reason != 0x0) && (ctx->last_branch_reason != 0x4)))) {
1003  retval = etm_read_instruction(ctx, &instruction);
1004  if (retval != ERROR_OK) {
1005  /* can't continue tracing with no image available */
1006  if (retval == ERROR_TRACE_IMAGE_UNAVAILABLE)
1007  return retval;
1008  else if (retval == ERROR_TRACE_INSTRUCTION_UNAVAILABLE) {
1009  /* TODO: handle incomplete images
1010  * for now we just quit the analysis*/
1011  return retval;
1012  }
1013  }
1014 
1015  cycles = old_index - last_instruction;
1016  }
1017 
1018  if ((pipestat == STAT_ID) || (pipestat == STAT_BD)) {
1019  uint32_t new_data_index = ctx->data_index;
1020  uint32_t new_data_half = ctx->data_half;
1021 
1022  /* in case of a branch with data, the branch target address was consumed before
1023  * we temporarily go back to the saved data index */
1024  if (pipestat == STAT_BD) {
1025  ctx->data_index = old_data_index;
1026  ctx->data_half = old_data_half;
1027  }
1028 
1029  if (ctx->control & ETM_CTRL_TRACE_ADDR) {
1030  uint8_t packet;
1031  int shift = 0;
1032 
1033  do {
1034  retval = etmv1_next_packet(ctx, &packet, 0);
1035  if (retval != 0)
1037  ctx->last_ptr &= ~(0x7f << shift);
1038  ctx->last_ptr |= (packet & 0x7f) << shift;
1039  shift += 7;
1040  } while ((packet & 0x80) && (shift < 32));
1041 
1042  if (shift >= 32)
1043  ctx->ptr_ok = 1;
1044 
1045  if (ctx->ptr_ok)
1047  "address: 0x%8.8" PRIx32 "",
1048  ctx->last_ptr);
1049  }
1050 
1051  if (ctx->control & ETM_CTRL_TRACE_DATA) {
1052  if ((instruction.type == ARM_LDM) ||
1053  (instruction.type == ARM_STM)) {
1054  int i;
1055  for (i = 0; i < 16; i++) {
1056  if (instruction.info.load_store_multiple.register_list
1057  & (1 << i)) {
1058  uint32_t data;
1059  if (etmv1_data(ctx, 4, &data) != 0)
1062  "data: 0x%8.8" PRIx32 "",
1063  data);
1064  }
1065  }
1066  } else if ((instruction.type >= ARM_LDR) &&
1067  (instruction.type <= ARM_STRH)) {
1068  uint32_t data;
1069  if (etmv1_data(ctx, arm_access_size(&instruction),
1070  &data) != 0)
1072  command_print(cmd, "data: 0x%8.8" PRIx32 "", data);
1073  }
1074  }
1075 
1076  /* restore data index after consuming BD address and data */
1077  if (pipestat == STAT_BD) {
1078  ctx->data_index = new_data_index;
1079  ctx->data_half = new_data_half;
1080  }
1081  }
1082 
1083  /* adjust PC */
1084  if ((pipestat == STAT_IE) || (pipestat == STAT_ID)) {
1085  if (((instruction.type == ARM_B) ||
1086  (instruction.type == ARM_BL) ||
1087  (instruction.type == ARM_BLX)) &&
1088  (instruction.info.b_bl_bx_blx.target_address != 0xffffffff))
1089  next_pc = instruction.info.b_bl_bx_blx.target_address;
1090  else
1091  next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
1092  } else if (pipestat == STAT_IN)
1093  next_pc += (ctx->core_state == ARM_STATE_ARM) ? 4 : 2;
1094 
1095  if ((pipestat != STAT_TD) && (pipestat != STAT_WT)) {
1096  char cycles_text[32] = "";
1097 
1098  /* if the trace was captured with cycle accurate tracing enabled,
1099  * output the number of cycles since the last executed instruction
1100  */
1101  if (ctx->control & ETM_CTRL_CYCLE_ACCURATE) {
1102  snprintf(cycles_text, 32, " (%i %s)",
1103  (int)cycles,
1104  (cycles == 1) ? "cycle" : "cycles");
1105  }
1106 
1107  command_print(cmd, "%s%s%s",
1108  instruction.text,
1109  (pipestat == STAT_IN) ? " (not executed)" : "",
1110  cycles_text);
1111 
1112  ctx->current_pc = next_pc;
1113 
1114  /* packets for an instruction don't start on or before the preceding
1115  * functional pipestat (i.e. other than WT or TD)
1116  */
1117  if (ctx->data_index <= ctx->pipe_index) {
1118  ctx->data_index = ctx->pipe_index + 1;
1119  ctx->data_half = 0;
1120  }
1121  }
1122 
1123  ctx->pipe_index += 1;
1124  }
1125 
1126  return ERROR_OK;
1127 }
1128 
1129 static COMMAND_HELPER(handle_etm_tracemode_command_update,
1130  uint32_t *mode)
1131 {
1132  uint32_t tracemode;
1133 
1134  /* what parts of data access are traced? */
1135  if (strcmp(CMD_ARGV[0], "none") == 0)
1136  tracemode = 0;
1137  else if (strcmp(CMD_ARGV[0], "data") == 0)
1138  tracemode = ETM_CTRL_TRACE_DATA;
1139  else if (strcmp(CMD_ARGV[0], "address") == 0)
1140  tracemode = ETM_CTRL_TRACE_ADDR;
1141  else if (strcmp(CMD_ARGV[0], "all") == 0)
1143  else {
1144  command_print(CMD, "invalid option '%s'", CMD_ARGV[0]);
1146  }
1147 
1148  uint8_t context_id;
1149  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], context_id);
1150  switch (context_id) {
1151  case 0:
1152  tracemode |= ETM_CTRL_CONTEXTID_NONE;
1153  break;
1154  case 8:
1155  tracemode |= ETM_CTRL_CONTEXTID_8;
1156  break;
1157  case 16:
1158  tracemode |= ETM_CTRL_CONTEXTID_16;
1159  break;
1160  case 32:
1161  tracemode |= ETM_CTRL_CONTEXTID_32;
1162  break;
1163  default:
1164  command_print(CMD, "invalid option '%s'", CMD_ARGV[1]);
1166  }
1167 
1168  bool etmv1_cycle_accurate;
1169  COMMAND_PARSE_ENABLE(CMD_ARGV[2], etmv1_cycle_accurate);
1170  if (etmv1_cycle_accurate)
1171  tracemode |= ETM_CTRL_CYCLE_ACCURATE;
1172 
1173  bool etmv1_branch_output;
1174  COMMAND_PARSE_ENABLE(CMD_ARGV[3], etmv1_branch_output);
1175  if (etmv1_branch_output)
1176  tracemode |= ETM_CTRL_BRANCH_OUTPUT;
1177 
1178  /* IGNORED:
1179  * - CPRT tracing (coprocessor register transfers)
1180  * - debug request (causes debug entry on trigger)
1181  * - stall on FIFOFULL (preventing tracedata loss)
1182  */
1183  *mode = tracemode;
1184 
1185  return ERROR_OK;
1186 }
1187 
1188 COMMAND_HANDLER(handle_etm_tracemode_command)
1189 {
1191  struct arm *arm = target_to_arm(target);
1192  struct etm_context *etm;
1193 
1194  if (!is_arm(arm)) {
1195  command_print(CMD, "ETM: current target isn't an ARM");
1196  return ERROR_FAIL;
1197  }
1198 
1199  etm = arm->etm;
1200  if (!etm) {
1201  command_print(CMD, "current target doesn't have an ETM configured");
1202  return ERROR_FAIL;
1203  }
1204 
1205  uint32_t tracemode = etm->control;
1206 
1207  switch (CMD_ARGC) {
1208  case 0:
1209  break;
1210  case 4:
1211  CALL_COMMAND_HANDLER(handle_etm_tracemode_command_update,
1212  &tracemode);
1213  break;
1214  default:
1216  }
1217 
1223  command_print(CMD, "current tracemode configuration:");
1224 
1225  switch (tracemode & ETM_CTRL_TRACE_MASK) {
1226  default:
1227  command_print(CMD, "data tracing: none");
1228  break;
1229  case ETM_CTRL_TRACE_DATA:
1230  command_print(CMD, "data tracing: data only");
1231  break;
1232  case ETM_CTRL_TRACE_ADDR:
1233  command_print(CMD, "data tracing: address only");
1234  break;
1236  command_print(CMD, "data tracing: address and data");
1237  break;
1238  }
1239 
1240  switch (tracemode & ETM_CTRL_CONTEXTID_MASK) {
1242  command_print(CMD, "contextid tracing: none");
1243  break;
1244  case ETM_CTRL_CONTEXTID_8:
1245  command_print(CMD, "contextid tracing: 8 bit");
1246  break;
1247  case ETM_CTRL_CONTEXTID_16:
1248  command_print(CMD, "contextid tracing: 16 bit");
1249  break;
1250  case ETM_CTRL_CONTEXTID_32:
1251  command_print(CMD, "contextid tracing: 32 bit");
1252  break;
1253  }
1254 
1255  if (tracemode & ETM_CTRL_CYCLE_ACCURATE)
1256  command_print(CMD, "cycle-accurate tracing enabled");
1257  else
1258  command_print(CMD, "cycle-accurate tracing disabled");
1259 
1260  if (tracemode & ETM_CTRL_BRANCH_OUTPUT)
1261  command_print(CMD, "full branch address output enabled");
1262  else
1263  command_print(CMD, "full branch address output disabled");
1264 
1265 #define TRACEMODE_MASK ( \
1266  ETM_CTRL_CONTEXTID_MASK \
1267  | ETM_CTRL_BRANCH_OUTPUT \
1268  | ETM_CTRL_CYCLE_ACCURATE \
1269  | ETM_CTRL_TRACE_MASK \
1270  )
1271 
1272  /* only update ETM_CTRL register if tracemode changed */
1273  if ((etm->control & TRACEMODE_MASK) != tracemode) {
1274  struct reg *etm_ctrl_reg;
1275 
1276  etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
1277  if (!etm_ctrl_reg)
1278  return ERROR_FAIL;
1279 
1280  etm->control &= ~TRACEMODE_MASK;
1281  etm->control |= tracemode & TRACEMODE_MASK;
1282 
1283  buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
1284  etm_store_reg(etm_ctrl_reg);
1285 
1286  /* invalidate old trace data */
1287  etm->capture_status = TRACE_IDLE;
1288  if (etm->trace_depth > 0) {
1289  free(etm->trace_data);
1290  etm->trace_data = NULL;
1291  }
1292  etm->trace_depth = 0;
1293  }
1294 
1295 #undef TRACEMODE_MASK
1296 
1297  return ERROR_OK;
1298 }
1299 
1300 COMMAND_HANDLER(handle_etm_config_command)
1301 {
1302  struct target *target;
1303  struct arm *arm;
1304  uint32_t portmode = 0x0;
1305  struct etm_context *etm_ctx;
1306  int i;
1307 
1308  if (CMD_ARGC != 5)
1310 
1311  target = get_target(CMD_ARGV[0]);
1312  if (!target) {
1313  LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
1314  return ERROR_FAIL;
1315  }
1316 
1318  if (!is_arm(arm)) {
1319  command_print(CMD, "target '%s' is '%s'; not an ARM",
1322  return ERROR_FAIL;
1323  }
1324 
1325  /* FIXME for ETMv3.0 and above -- and we don't yet know what ETM
1326  * version we'll be using!! -- so we can't know how to validate
1327  * params yet. "etm config" should likely be *AFTER* hookup...
1328  *
1329  * - Many more widths might be supported ... and we can easily
1330  * check whether our setting "took".
1331  *
1332  * - The "clock" and "mode" bits are interpreted differently.
1333  * See ARM IHI 0014O table 2-17 for the old behaviour, and
1334  * table 2-18 for the new. With ETB it's best to specify
1335  * "normal full" ...
1336  */
1337  uint8_t port_width;
1338  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], port_width);
1339  switch (port_width) {
1340  /* before ETMv3.0 */
1341  case 4:
1342  portmode |= ETM_PORT_4BIT;
1343  break;
1344  case 8:
1345  portmode |= ETM_PORT_8BIT;
1346  break;
1347  case 16:
1348  portmode |= ETM_PORT_16BIT;
1349  break;
1350  /* ETMv3.0 and later*/
1351  case 24:
1352  portmode |= ETM_PORT_24BIT;
1353  break;
1354  case 32:
1355  portmode |= ETM_PORT_32BIT;
1356  break;
1357  case 48:
1358  portmode |= ETM_PORT_48BIT;
1359  break;
1360  case 64:
1361  portmode |= ETM_PORT_64BIT;
1362  break;
1363  case 1:
1364  portmode |= ETM_PORT_1BIT;
1365  break;
1366  case 2:
1367  portmode |= ETM_PORT_2BIT;
1368  break;
1369  default:
1371  "unsupported ETM port width '%s'", CMD_ARGV[1]);
1372  return ERROR_FAIL;
1373  }
1374 
1375  if (strcmp("normal", CMD_ARGV[2]) == 0)
1376  portmode |= ETM_PORT_NORMAL;
1377  else if (strcmp("multiplexed", CMD_ARGV[2]) == 0)
1378  portmode |= ETM_PORT_MUXED;
1379  else if (strcmp("demultiplexed", CMD_ARGV[2]) == 0)
1380  portmode |= ETM_PORT_DEMUXED;
1381  else {
1383  "unsupported ETM port mode '%s', must be 'normal', 'multiplexed' or 'demultiplexed'",
1384  CMD_ARGV[2]);
1385  return ERROR_FAIL;
1386  }
1387 
1388  if (strcmp("half", CMD_ARGV[3]) == 0)
1389  portmode |= ETM_PORT_HALF_CLOCK;
1390  else if (strcmp("full", CMD_ARGV[3]) == 0)
1391  portmode |= ETM_PORT_FULL_CLOCK;
1392  else {
1394  "unsupported ETM port clocking '%s', must be 'full' or 'half'",
1395  CMD_ARGV[3]);
1396  return ERROR_FAIL;
1397  }
1398 
1399  etm_ctx = calloc(1, sizeof(struct etm_context));
1400  if (!etm_ctx) {
1401  LOG_DEBUG("out of memory");
1402  return ERROR_FAIL;
1403  }
1404 
1405  for (i = 0; etm_capture_drivers[i]; i++) {
1406  if (strcmp(CMD_ARGV[4], etm_capture_drivers[i]->name) == 0) {
1407  int retval = register_commands(CMD_CTX, NULL, etm_capture_drivers[i]->commands);
1408  if (retval != ERROR_OK) {
1409  free(etm_ctx);
1410  return retval;
1411  }
1412 
1413  etm_ctx->capture_driver = etm_capture_drivers[i];
1414 
1415  break;
1416  }
1417  }
1418 
1419  if (!etm_capture_drivers[i]) {
1420  /* no supported capture driver found, don't register an ETM */
1421  free(etm_ctx);
1422  LOG_ERROR("trace capture driver '%s' not found", CMD_ARGV[4]);
1423  return ERROR_FAIL;
1424  }
1425 
1426  etm_ctx->target = target;
1427  etm_ctx->trace_data = NULL;
1428  etm_ctx->control = portmode;
1429  etm_ctx->core_state = ARM_STATE_ARM;
1430 
1431  arm->etm = etm_ctx;
1432 
1434 }
1435 
1436 COMMAND_HANDLER(handle_etm_info_command)
1437 {
1438  struct target *target;
1439  struct arm *arm;
1440  struct etm_context *etm;
1441  struct reg *etm_sys_config_reg;
1442  int max_port_size;
1443  uint32_t config;
1444 
1447  if (!is_arm(arm)) {
1448  command_print(CMD, "ETM: current target isn't an ARM");
1449  return ERROR_FAIL;
1450  }
1451 
1452  etm = arm->etm;
1453  if (!etm) {
1454  command_print(CMD, "current target doesn't have an ETM configured");
1455  return ERROR_FAIL;
1456  }
1457 
1458  command_print(CMD, "ETM v%d.%d",
1459  etm->bcd_vers >> 4, etm->bcd_vers & 0xf);
1460  command_print(CMD, "pairs of address comparators: %i",
1461  (int) (etm->config >> 0) & 0x0f);
1462  command_print(CMD, "data comparators: %i",
1463  (int) (etm->config >> 4) & 0x0f);
1464  command_print(CMD, "memory map decoders: %i",
1465  (int) (etm->config >> 8) & 0x1f);
1466  command_print(CMD, "number of counters: %i",
1467  (int) (etm->config >> 13) & 0x07);
1468  command_print(CMD, "sequencer %spresent",
1469  (int) (etm->config & (1 << 16)) ? "" : "not ");
1470  command_print(CMD, "number of ext. inputs: %i",
1471  (int) (etm->config >> 17) & 0x07);
1472  command_print(CMD, "number of ext. outputs: %i",
1473  (int) (etm->config >> 20) & 0x07);
1474  command_print(CMD, "FIFO full %spresent",
1475  (int) (etm->config & (1 << 23)) ? "" : "not ");
1476  if (etm->bcd_vers < 0x20)
1477  command_print(CMD, "protocol version: %i",
1478  (int) (etm->config >> 28) & 0x07);
1479  else {
1481  "coprocessor and memory access %ssupported",
1482  (etm->config & (1 << 26)) ? "" : "not ");
1483  command_print(CMD, "trace start/stop %spresent",
1484  (etm->config & (1 << 26)) ? "" : "not ");
1485  command_print(CMD, "number of context comparators: %i",
1486  (int) (etm->config >> 24) & 0x03);
1487  }
1488 
1489  /* SYS_CONFIG isn't present before ETMv1.2 */
1490  etm_sys_config_reg = etm_reg_lookup(etm, ETM_SYS_CONFIG);
1491  if (!etm_sys_config_reg)
1492  return ERROR_OK;
1493 
1494  etm_get_reg(etm_sys_config_reg);
1495  config = buf_get_u32(etm_sys_config_reg->value, 0, 32);
1496 
1497  LOG_DEBUG("ETM SYS CONFIG %08" PRIx32, config);
1498 
1499  max_port_size = config & 0x7;
1500  if (etm->bcd_vers >= 0x30)
1501  max_port_size |= (config >> 6) & 0x08;
1502  switch (max_port_size) {
1503  /* before ETMv3.0 */
1504  case 0:
1505  max_port_size = 4;
1506  break;
1507  case 1:
1508  max_port_size = 8;
1509  break;
1510  case 2:
1511  max_port_size = 16;
1512  break;
1513  /* ETMv3.0 and later*/
1514  case 3:
1515  max_port_size = 24;
1516  break;
1517  case 4:
1518  max_port_size = 32;
1519  break;
1520  case 5:
1521  max_port_size = 48;
1522  break;
1523  case 6:
1524  max_port_size = 64;
1525  break;
1526  case 8:
1527  max_port_size = 1;
1528  break;
1529  case 9:
1530  max_port_size = 2;
1531  break;
1532  default:
1533  LOG_ERROR("Illegal max_port_size");
1534  return ERROR_FAIL;
1535  }
1536  command_print(CMD, "max. port size: %i", max_port_size);
1537 
1538  if (etm->bcd_vers < 0x30) {
1539  command_print(CMD, "half-rate clocking %ssupported",
1540  (config & (1 << 3)) ? "" : "not ");
1541  command_print(CMD, "full-rate clocking %ssupported",
1542  (config & (1 << 4)) ? "" : "not ");
1543  command_print(CMD, "normal trace format %ssupported",
1544  (config & (1 << 5)) ? "" : "not ");
1545  command_print(CMD, "multiplex trace format %ssupported",
1546  (config & (1 << 6)) ? "" : "not ");
1547  command_print(CMD, "demultiplex trace format %ssupported",
1548  (config & (1 << 7)) ? "" : "not ");
1549  } else {
1550  /* REVISIT show which size and format are selected ... */
1551  command_print(CMD, "current port size %ssupported",
1552  (config & (1 << 10)) ? "" : "not ");
1553  command_print(CMD, "current trace format %ssupported",
1554  (config & (1 << 11)) ? "" : "not ");
1555  }
1556  if (etm->bcd_vers >= 0x21)
1557  command_print(CMD, "fetch comparisons %ssupported",
1558  (config & (1 << 17)) ? "not " : "");
1559  command_print(CMD, "FIFO full %ssupported",
1560  (config & (1 << 8)) ? "" : "not ");
1561 
1562  return ERROR_OK;
1563 }
1564 
1565 COMMAND_HANDLER(handle_etm_status_command)
1566 {
1567  struct target *target;
1568  struct arm *arm;
1569  struct etm_context *etm;
1571 
1574  if (!is_arm(arm)) {
1575  command_print(CMD, "ETM: current target isn't an ARM");
1576  return ERROR_FAIL;
1577  }
1578 
1579  etm = arm->etm;
1580  if (!etm) {
1581  command_print(CMD, "current target doesn't have an ETM configured");
1582  return ERROR_FAIL;
1583  }
1584 
1585  /* ETM status */
1586  if (etm->bcd_vers >= 0x11) {
1587  struct reg *reg;
1588 
1589  reg = etm_reg_lookup(etm, ETM_STATUS);
1590  if (!reg)
1591  return ERROR_FAIL;
1592  if (etm_get_reg(reg) == ERROR_OK) {
1593  unsigned int s = buf_get_u32(reg->value, 0, reg->size);
1594 
1595  command_print(CMD, "etm: %s%s%s%s",
1596  /* bit(1) == progbit */
1597  (etm->bcd_vers >= 0x12)
1598  ? ((s & (1 << 1))
1599  ? "disabled" : "enabled")
1600  : "?",
1601  ((s & (1 << 3)) && etm->bcd_vers >= 0x31)
1602  ? " triggered" : "",
1603  ((s & (1 << 2)) && etm->bcd_vers >= 0x12)
1604  ? " start/stop" : "",
1605  ((s & (1 << 0)) && etm->bcd_vers >= 0x11)
1606  ? " untraced-overflow" : "");
1607  } /* else ignore and try showing trace port status */
1608  }
1609 
1610  /* Trace Port Driver status */
1611  trace_status = etm->capture_driver->status(etm);
1612  if (trace_status == TRACE_IDLE)
1613  command_print(CMD, "%s: idle", etm->capture_driver->name);
1614  else {
1615  static char *completed = " completed";
1616  static char *running = " is running";
1617  static char *overflowed = ", overflowed";
1618  static char *triggered = ", triggered";
1619 
1620  command_print(CMD, "%s: trace collection%s%s%s",
1621  etm->capture_driver->name,
1622  (trace_status & TRACE_RUNNING) ? running : completed,
1623  (trace_status & TRACE_OVERFLOWED) ? overflowed : "",
1624  (trace_status & TRACE_TRIGGERED) ? triggered : "");
1625 
1626  if (etm->trace_depth > 0) {
1627  command_print(CMD, "%i frames of trace data read",
1628  (int)(etm->trace_depth));
1629  }
1630  }
1631 
1632  return ERROR_OK;
1633 }
1634 
1635 COMMAND_HANDLER(handle_etm_image_command)
1636 {
1637  struct target *target;
1638  struct arm *arm;
1639  struct etm_context *etm_ctx;
1640 
1641  if (CMD_ARGC < 1)
1643 
1646  if (!is_arm(arm)) {
1647  command_print(CMD, "ETM: current target isn't an ARM");
1648  return ERROR_FAIL;
1649  }
1650 
1651  etm_ctx = arm->etm;
1652  if (!etm_ctx) {
1653  command_print(CMD, "current target doesn't have an ETM configured");
1654  return ERROR_FAIL;
1655  }
1656 
1657  if (etm_ctx->image) {
1658  image_close(etm_ctx->image);
1659  free(etm_ctx->image);
1660  command_print(CMD, "previously loaded image found and closed");
1661  }
1662 
1663  etm_ctx->image = malloc(sizeof(struct image));
1664  etm_ctx->image->base_address_set = false;
1665  etm_ctx->image->start_address_set = false;
1666 
1667  /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1668  if (CMD_ARGC >= 2) {
1669  etm_ctx->image->base_address_set = true;
1670  COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], etm_ctx->image->base_address);
1671  } else
1672  etm_ctx->image->base_address_set = false;
1673 
1674  if (image_open(etm_ctx->image, CMD_ARGV[0],
1675  (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK) {
1676  free(etm_ctx->image);
1677  etm_ctx->image = NULL;
1678  return ERROR_FAIL;
1679  }
1680 
1681  return ERROR_OK;
1682 }
1683 
1684 COMMAND_HANDLER(handle_etm_dump_command)
1685 {
1686  struct fileio *file;
1687  struct target *target;
1688  struct arm *arm;
1689  struct etm_context *etm_ctx;
1690  uint32_t i;
1691 
1692  if (CMD_ARGC != 1)
1694 
1697  if (!is_arm(arm)) {
1698  command_print(CMD, "ETM: current target isn't an ARM");
1699  return ERROR_FAIL;
1700  }
1701 
1702  etm_ctx = arm->etm;
1703  if (!etm_ctx) {
1704  command_print(CMD, "current target doesn't have an ETM configured");
1705  return ERROR_FAIL;
1706  }
1707 
1708  if (etm_ctx->capture_driver->status(etm_ctx) == TRACE_IDLE) {
1709  command_print(CMD, "trace capture wasn't enabled, no trace data captured");
1710  return ERROR_OK;
1711  }
1712 
1713  if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING) {
1714  /* TODO: if on-the-fly capture is to be supported, this needs to be changed */
1715  command_print(CMD, "trace capture not completed");
1716  return ERROR_FAIL;
1717  }
1718 
1719  /* read the trace data if it wasn't read already */
1720  if (etm_ctx->trace_depth == 0)
1721  etm_ctx->capture_driver->read_trace(etm_ctx);
1722 
1724  return ERROR_FAIL;
1725 
1726  fileio_write_u32(file, etm_ctx->capture_status);
1727  fileio_write_u32(file, etm_ctx->control);
1728  fileio_write_u32(file, etm_ctx->trace_depth);
1729 
1730  for (i = 0; i < etm_ctx->trace_depth; i++) {
1731  fileio_write_u32(file, etm_ctx->trace_data[i].pipestat);
1732  fileio_write_u32(file, etm_ctx->trace_data[i].packet);
1733  fileio_write_u32(file, etm_ctx->trace_data[i].flags);
1734  }
1735 
1736  fileio_close(file);
1737 
1738  return ERROR_OK;
1739 }
1740 
1741 COMMAND_HANDLER(handle_etm_load_command)
1742 {
1743  struct fileio *file;
1744  struct target *target;
1745  struct arm *arm;
1746  struct etm_context *etm_ctx;
1747  uint32_t i;
1748 
1749  if (CMD_ARGC != 1)
1751 
1754  if (!is_arm(arm)) {
1755  command_print(CMD, "ETM: current target isn't an ARM");
1756  return ERROR_FAIL;
1757  }
1758 
1759  etm_ctx = arm->etm;
1760  if (!etm_ctx) {
1761  command_print(CMD, "current target doesn't have an ETM configured");
1762  return ERROR_FAIL;
1763  }
1764 
1765  if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING) {
1766  command_print(CMD, "trace capture running, stop first");
1767  return ERROR_FAIL;
1768  }
1769 
1771  return ERROR_FAIL;
1772 
1773  size_t filesize;
1774  int retval = fileio_size(file, &filesize);
1775  if (retval != ERROR_OK) {
1776  fileio_close(file);
1777  return retval;
1778  }
1779 
1780  if (filesize % 4) {
1781  command_print(CMD, "size isn't a multiple of 4, no valid trace data");
1782  fileio_close(file);
1783  return ERROR_FAIL;
1784  }
1785 
1786  if (etm_ctx->trace_depth > 0) {
1787  free(etm_ctx->trace_data);
1788  etm_ctx->trace_data = NULL;
1789  }
1790 
1791  {
1792  uint32_t tmp;
1793  fileio_read_u32(file, &tmp); etm_ctx->capture_status = tmp;
1794  fileio_read_u32(file, &tmp); etm_ctx->control = tmp;
1795  fileio_read_u32(file, &etm_ctx->trace_depth);
1796  }
1797  etm_ctx->trace_data = malloc(sizeof(struct etmv1_trace_data) * etm_ctx->trace_depth);
1798  if (!etm_ctx->trace_data) {
1799  command_print(CMD, "not enough memory to perform operation");
1800  fileio_close(file);
1801  return ERROR_FAIL;
1802  }
1803 
1804  for (i = 0; i < etm_ctx->trace_depth; i++) {
1805  uint32_t pipestat, packet, flags;
1806  fileio_read_u32(file, &pipestat);
1807  fileio_read_u32(file, &packet);
1808  fileio_read_u32(file, &flags);
1809  etm_ctx->trace_data[i].pipestat = pipestat & 0xff;
1810  etm_ctx->trace_data[i].packet = packet & 0xffff;
1811  etm_ctx->trace_data[i].flags = flags;
1812  }
1813 
1814  fileio_close(file);
1815 
1816  return ERROR_OK;
1817 }
1818 
1819 COMMAND_HANDLER(handle_etm_start_command)
1820 {
1821  struct target *target;
1822  struct arm *arm;
1823  struct etm_context *etm_ctx;
1824  struct reg *etm_ctrl_reg;
1825 
1828  if (!is_arm(arm)) {
1829  command_print(CMD, "ETM: current target isn't an ARM");
1830  return ERROR_FAIL;
1831  }
1832 
1833  etm_ctx = arm->etm;
1834  if (!etm_ctx) {
1835  command_print(CMD, "current target doesn't have an ETM configured");
1836  return ERROR_FAIL;
1837  }
1838 
1839  /* invalidate old tracing data */
1840  etm_ctx->capture_status = TRACE_IDLE;
1841  if (etm_ctx->trace_depth > 0) {
1842  free(etm_ctx->trace_data);
1843  etm_ctx->trace_data = NULL;
1844  }
1845  etm_ctx->trace_depth = 0;
1846 
1847  etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
1848  if (!etm_ctrl_reg)
1849  return ERROR_FAIL;
1850 
1851  etm_get_reg(etm_ctrl_reg);
1852 
1853  /* Clear programming bit (10), set port selection bit (11) */
1854  buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x2);
1855 
1856  etm_store_reg(etm_ctrl_reg);
1858 
1859  etm_ctx->capture_driver->start_capture(etm_ctx);
1860 
1861  return ERROR_OK;
1862 }
1863 
1864 COMMAND_HANDLER(handle_etm_stop_command)
1865 {
1866  struct target *target;
1867  struct arm *arm;
1868  struct etm_context *etm_ctx;
1869  struct reg *etm_ctrl_reg;
1870 
1873  if (!is_arm(arm)) {
1874  command_print(CMD, "ETM: current target isn't an ARM");
1875  return ERROR_FAIL;
1876  }
1877 
1878  etm_ctx = arm->etm;
1879  if (!etm_ctx) {
1880  command_print(CMD, "current target doesn't have an ETM configured");
1881  return ERROR_FAIL;
1882  }
1883 
1884  etm_ctrl_reg = etm_reg_lookup(etm_ctx, ETM_CTRL);
1885  if (!etm_ctrl_reg)
1886  return ERROR_FAIL;
1887 
1888  etm_get_reg(etm_ctrl_reg);
1889 
1890  /* Set programming bit (10), clear port selection bit (11) */
1891  buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x1);
1892 
1893  etm_store_reg(etm_ctrl_reg);
1895 
1896  etm_ctx->capture_driver->stop_capture(etm_ctx);
1897 
1898  return ERROR_OK;
1899 }
1900 
1901 COMMAND_HANDLER(handle_etm_trigger_debug_command)
1902 {
1903  struct target *target;
1904  struct arm *arm;
1905  struct etm_context *etm;
1906 
1909  if (!is_arm(arm)) {
1910  command_print(CMD, "ETM: %s isn't an ARM",
1911  target_name(target));
1912  return ERROR_FAIL;
1913  }
1914 
1915  etm = arm->etm;
1916  if (!etm) {
1917  command_print(CMD, "ETM: no ETM configured for %s",
1918  target_name(target));
1919  return ERROR_FAIL;
1920  }
1921 
1922  if (CMD_ARGC == 1) {
1923  struct reg *etm_ctrl_reg;
1924  bool dbgrq;
1925 
1926  etm_ctrl_reg = etm_reg_lookup(etm, ETM_CTRL);
1927  if (!etm_ctrl_reg)
1928  return ERROR_FAIL;
1929 
1930  COMMAND_PARSE_ENABLE(CMD_ARGV[0], dbgrq);
1931  if (dbgrq)
1932  etm->control |= ETM_CTRL_DBGRQ;
1933  else
1934  etm->control &= ~ETM_CTRL_DBGRQ;
1935 
1936  /* etm->control will be written to hardware
1937  * the next time an "etm start" is issued.
1938  */
1939  buf_set_u32(etm_ctrl_reg->value, 0, 32, etm->control);
1940  }
1941 
1942  command_print(CMD, "ETM: %s debug halt",
1943  (etm->control & ETM_CTRL_DBGRQ)
1944  ? "triggers"
1945  : "does not trigger");
1946  return ERROR_OK;
1947 }
1948 
1949 COMMAND_HANDLER(handle_etm_analyze_command)
1950 {
1951  struct target *target;
1952  struct arm *arm;
1953  struct etm_context *etm_ctx;
1954  int retval;
1955 
1958  if (!is_arm(arm)) {
1959  command_print(CMD, "ETM: current target isn't an ARM");
1960  return ERROR_FAIL;
1961  }
1962 
1963  etm_ctx = arm->etm;
1964  if (!etm_ctx) {
1965  command_print(CMD, "current target doesn't have an ETM configured");
1966  return ERROR_FAIL;
1967  }
1968 
1969  retval = etmv1_analyze_trace(etm_ctx, CMD);
1970  if (retval != ERROR_OK) {
1971  /* FIX! error should be reported inside etmv1_analyze_trace() */
1972  switch (retval) {
1975  "further analysis failed (corrupted trace data or just end of data");
1976  break;
1979  "no instruction for current address available, analysis aborted");
1980  break;
1982  command_print(CMD, "no image available for trace analysis");
1983  break;
1984  default:
1985  command_print(CMD, "unknown error");
1986  }
1987  }
1988 
1989  return retval;
1990 }
1991 
1992 static const struct command_registration etm_config_command_handlers[] = {
1993  {
1994  /* NOTE: with ADIv5, ETMs are accessed by DAP operations,
1995  * possibly over SWD, not JTAG scanchain 6 of 'target'.
1996  *
1997  * Also, these parameters don't match ETM v3+ modules...
1998  */
1999  .name = "config",
2000  .handler = handle_etm_config_command,
2001  .mode = COMMAND_CONFIG,
2002  .help = "Set up ETM output port.",
2003  .usage = "target port_width port_mode clocking capture_driver",
2004  },
2006 };
2007 const struct command_registration etm_command_handlers[] = {
2008  {
2009  .name = "etm",
2010  .mode = COMMAND_ANY,
2011  .help = "Embedded Trace Macrocell command group",
2012  .usage = "",
2013  .chain = etm_config_command_handlers,
2014  },
2016 };
2017 
2018 static const struct command_registration etm_exec_command_handlers[] = {
2019  {
2020  .name = "tracemode",
2021  .handler = handle_etm_tracemode_command,
2022  .mode = COMMAND_EXEC,
2023  .help = "configure/display trace mode",
2024  .usage = "('none'|'data'|'address'|'all') "
2025  "context_id_bits "
2026  "['enable'|'disable'] "
2027  "['enable'|'disable']",
2028  },
2029  {
2030  .name = "info",
2031  .handler = handle_etm_info_command,
2032  .mode = COMMAND_EXEC,
2033  .usage = "",
2034  .help = "display info about the current target's ETM",
2035  },
2036  {
2037  .name = "status",
2038  .handler = handle_etm_status_command,
2039  .mode = COMMAND_EXEC,
2040  .usage = "",
2041  .help = "display current target's ETM status",
2042  },
2043  {
2044  .name = "start",
2045  .handler = handle_etm_start_command,
2046  .mode = COMMAND_EXEC,
2047  .usage = "",
2048  .help = "start ETM trace collection",
2049  },
2050  {
2051  .name = "stop",
2052  .handler = handle_etm_stop_command,
2053  .mode = COMMAND_EXEC,
2054  .usage = "",
2055  .help = "stop ETM trace collection",
2056  },
2057  {
2058  .name = "trigger_debug",
2059  .handler = handle_etm_trigger_debug_command,
2060  .mode = COMMAND_EXEC,
2061  .help = "enable/disable debug entry on trigger",
2062  .usage = "['enable'|'disable']",
2063  },
2064  {
2065  .name = "analyze",
2066  .handler = handle_etm_analyze_command,
2067  .mode = COMMAND_EXEC,
2068  .usage = "",
2069  .help = "analyze collected ETM trace",
2070  },
2071  {
2072  .name = "image",
2073  .handler = handle_etm_image_command,
2074  .mode = COMMAND_EXEC,
2075  .help = "load image from file with optional offset",
2076  .usage = "<file> [base address] [type]",
2077  },
2078  {
2079  .name = "dump",
2080  .handler = handle_etm_dump_command,
2081  .mode = COMMAND_EXEC,
2082  .help = "dump captured trace data to file",
2083  .usage = "filename",
2084  },
2085  {
2086  .name = "load",
2087  .handler = handle_etm_load_command,
2088  .mode = COMMAND_EXEC,
2089  .usage = "",
2090  .help = "load trace data for analysis <file>",
2091  },
2093 };
2094 
2095 static int etm_register_user_commands(struct command_context *cmd_ctx)
2096 {
2097  return register_commands(cmd_ctx, "etm", etm_exec_command_handlers);
2098 }
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, tap_state_t 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, tap_state_t end_state)
Definition: arm_jtag.h:31
enum arm_mode mode
Definition: armv4_5.c:277
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:443
#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:402
#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:533
#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:442
#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:253
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:274
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
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
static const struct command_registration etm_config_command_handlers[]
Definition: etm.c:1992
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
@ RO
Definition: etm.c:42
@ WO
Definition: etm.c:43
@ RW
Definition: etm.c:44
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:2007
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:2018
static COMMAND_HELPER(handle_etm_tracemode_command_update, uint32_t *mode)
Definition: etm.c:1129
#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:1188
#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:2095
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
#define ERROR_ETM_ANALYSIS_FAILED
Definition: etm.h:211
@ 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
@ 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
@ 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:1211
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:1079
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:957
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1043
void jtag_add_dr_scan_check(struct jtag_tap *active, int in_num_fields, struct scan_field *in_fields, tap_state_t state)
A version of jtag_add_dr_scan() that uses the check_value/mask fields.
Definition: jtag/core.c:445
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t 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:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
struct target * target
Definition: rtt/rtt.c:26
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
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::@69 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:235
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
trace_status_t(* 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
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
trace_status_t capture_status
Definition: etm.h:156
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:116
struct target * get_target(const char *id)
Definition: target.c:433
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:334
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:316
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:736
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
#define ERROR_TRACE_INSTRUCTION_UNAVAILABLE
Definition: trace.h:48
enum trace_status trace_status_t
#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