1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 ***************************************************************************/
15 #include "arm_disassembler.h"
17 #include "etm_dummy.h"
20 * ARM "Embedded Trace Macrocell" (ETM) support -- direct JTAG access.
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).
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.
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.
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
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 */
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.
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", },
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", },
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", },
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", },
90 /* REVISIT exclude VIEWDATA_CTRL2 when it's not there */
92 { 0x78, 12, WO
, 0x20, "ETM_sync_freq", },
93 { 0x7a, 22, RO
, 0x31, "ETM_config_code_ext", },
94 { 0x7b, 32, WO
, 0x31, "ETM_ext_input_select", },
95 { 0x7c, 32, WO
, 0x34, "ETM_trace_start_stop", },
96 { 0x7d, 8, WO
, 0x34, "ETM_behavior_control", },
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", },
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", }
130 #undef ADDR_COMPARATOR
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", }
149 #undef DATA_COMPARATOR
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", }
170 static const struct etm_reg_info etm_sequencer
[] = {
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 */
182 { ETM_SEQUENCER_STATE
, 2, RO
, 0x10, "ETM_sequencer_state", },
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, }
199 /* registers from 0x6c..0x7f were added after ETMv1.3 */
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", }
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
);
215 static const struct reg_arch_type etm_scan6_type
= {
217 .set
= etm_set_reg_w_exec
,
220 /* Look up register by ID ... most ETM instances only
221 * support a subset of the possible registers.
223 static struct reg
*etm_reg_lookup(struct etm_context
*etm_ctx
, unsigned id
)
225 struct reg_cache
*cache
= etm_ctx
->reg_cache
;
228 for (i
= 0; i
< cache
->num_regs
; i
++) {
229 struct etm_reg
*reg
= cache
->reg_list
[i
].arch_info
;
231 if (reg
->reg_info
->addr
== id
)
232 return &cache
->reg_list
[i
];
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
);
241 static void etm_reg_add(unsigned bcd_vers
, struct arm_jtag
*jtag_info
,
242 struct reg_cache
*cache
, struct etm_reg
*ereg
,
243 const struct etm_reg_info
*r
, unsigned nreg
)
245 struct reg
*reg
= cache
->reg_list
;
247 reg
+= cache
->num_regs
;
248 ereg
+= cache
->num_regs
;
250 /* add up to "nreg" registers from "r", if supported by this
251 * version of the ETM, to the specified cache.
253 for (; nreg
--; r
++) {
254 /* No more registers to add */
256 LOG_ERROR("etm_reg_add is requested to add non-existing registers, ETM config might be bogus");
260 /* this ETM may be too old to have some registers */
261 if (r
->bcd_vers
> bcd_vers
)
266 reg
->value
= ereg
->value
;
267 reg
->arch_info
= ereg
;
268 reg
->type
= &etm_scan6_type
;
273 ereg
->jtag_info
= jtag_info
;
278 struct reg_cache
*etm_build_reg_cache(struct target
*target
,
279 struct arm_jtag
*jtag_info
, struct etm_context
*etm_ctx
)
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 bcd_vers
, config
;
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
));
290 if (!reg_cache
|| !reg_list
|| !arch_info
) {
291 LOG_ERROR("No memory");
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;
301 /* add ETM_CONFIG, then parse its values to see
302 * which other registers exist in this ETM
304 etm_reg_add(0x10, jtag_info
, reg_cache
, arch_info
,
307 etm_get_reg(reg_list
);
308 etm_ctx
->config
= buf_get_u32(arch_info
->value
, 0, 32);
309 config
= etm_ctx
->config
;
311 /* figure ETM version then add base registers */
312 if (config
& (1 << 31)) {
313 LOG_WARNING("ETMv2+ support is incomplete");
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.
320 etm_reg_add(0x20, jtag_info
, reg_cache
, arch_info
,
322 etm_get_reg(reg_list
+ 1);
323 etm_ctx
->id
= buf_get_u32(
324 arch_info
[1].value
, 0, 32);
325 LOG_DEBUG("ETM ID: %08x", (unsigned) etm_ctx
->id
);
326 bcd_vers
= 0x10 + (((etm_ctx
->id
) >> 4) & 0xff);
329 switch (config
>> 28) {
346 LOG_WARNING("Bad ETMv1 protocol %d", config
>> 28);
350 etm_ctx
->bcd_vers
= bcd_vers
;
351 LOG_INFO("ETM v%d.%d", bcd_vers
>> 4, bcd_vers
& 0xf);
353 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
354 etm_basic
, ARRAY_SIZE(etm_basic
));
356 /* address and data comparators; counters; outputs */
357 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
358 etm_addr_comp
, 4 * (0x0f & (config
>> 0)));
359 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
360 etm_data_comp
, 2 * (0x0f & (config
>> 4)));
361 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
362 etm_counters
, 4 * (0x07 & (config
>> 13)));
363 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
364 etm_outputs
, (0x07 & (config
>> 20)));
366 /* FIFOFULL presence is optional
367 * REVISIT for ETMv1.2 and later, don't bother adding this
368 * unless ETM_SYS_CONFIG says it's also *supported* ...
370 if (config
& (1 << 23))
371 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
372 etm_fifofull
, ARRAY_SIZE(etm_fifofull
));
374 /* sequencer is optional (for state-dependant triggering) */
375 if (config
& (1 << 16))
376 etm_reg_add(bcd_vers
, jtag_info
, reg_cache
, arch_info
,
377 etm_sequencer
, ARRAY_SIZE(etm_sequencer
));
379 /* REVISIT could realloc and likely save half the memory
380 * in the two chunks we allocated...
383 /* the ETM might have an ETB connected */
384 if (strcmp(etm_ctx
->capture_driver
->name
, "etb") == 0) {
385 struct etb
*etb
= etm_ctx
->capture_driver_priv
;
388 LOG_ERROR("etb selected as etm capture driver, but no ETB configured");
392 reg_cache
->next
= etb_build_reg_cache(etb
);
394 etb
->reg_cache
= reg_cache
->next
;
397 etm_ctx
->reg_cache
= reg_cache
;
407 static int etm_read_reg(struct reg
*reg
)
409 return etm_read_reg_w_check(reg
, NULL
, NULL
);
412 static int etm_store_reg(struct reg
*reg
)
414 return etm_write_reg(reg
, buf_get_u32(reg
->value
, 0, reg
->size
));
417 int etm_setup(struct target
*target
)
420 uint32_t etm_ctrl_value
;
421 struct arm
*arm
= target_to_arm(target
);
422 struct etm_context
*etm_ctx
= arm
->etm
;
423 struct reg
*etm_ctrl_reg
;
425 etm_ctrl_reg
= etm_reg_lookup(etm_ctx
, ETM_CTRL
);
429 /* initialize some ETM control register settings */
430 etm_get_reg(etm_ctrl_reg
);
431 etm_ctrl_value
= buf_get_u32(etm_ctrl_reg
->value
, 0, 32);
433 /* clear the ETM powerdown bit (0) */
434 etm_ctrl_value
&= ~ETM_CTRL_POWERDOWN
;
436 /* configure port width (21,6:4), mode (13,17:16) and
437 * for older modules clocking (13)
439 etm_ctrl_value
= (etm_ctrl_value
440 & ~ETM_PORT_WIDTH_MASK
441 & ~ETM_PORT_MODE_MASK
443 & ~ETM_PORT_CLOCK_MASK
)
446 buf_set_u32(etm_ctrl_reg
->value
, 0, 32, etm_ctrl_value
);
447 etm_store_reg(etm_ctrl_reg
);
449 etm_ctx
->control
= etm_ctrl_value
;
451 retval
= jtag_execute_queue();
452 if (retval
!= ERROR_OK
)
455 /* REVISIT for ETMv3.0 and later, read ETM_sys_config to
456 * verify that those width and mode settings are OK ...
459 retval
= etm_ctx
->capture_driver
->init(etm_ctx
);
460 if (retval
!= ERROR_OK
) {
461 LOG_ERROR("ETM capture driver initialization failed");
467 static int etm_get_reg(struct reg
*reg
)
471 retval
= etm_read_reg(reg
);
472 if (retval
!= ERROR_OK
) {
473 LOG_ERROR("BUG: error scheduling etm register read");
477 retval
= jtag_execute_queue();
478 if (retval
!= ERROR_OK
) {
479 LOG_ERROR("register read failed");
486 static int etm_read_reg_w_check(struct reg
*reg
,
487 uint8_t *check_value
, uint8_t *check_mask
)
489 struct etm_reg
*etm_reg
= reg
->arch_info
;
491 const struct etm_reg_info
*r
= etm_reg
->reg_info
;
492 uint8_t reg_addr
= r
->addr
& 0x7f;
493 struct scan_field fields
[3];
496 if (etm_reg
->reg_info
->mode
== WO
) {
497 LOG_ERROR("BUG: can't read write-only register %s", r
->name
);
498 return ERROR_COMMAND_SYNTAX_ERROR
;
501 LOG_DEBUG("%s (%u)", r
->name
, reg_addr
);
503 retval
= arm_jtag_scann(etm_reg
->jtag_info
, 0x6, TAP_IDLE
);
504 if (retval
!= ERROR_OK
)
506 retval
= arm_jtag_set_instr(etm_reg
->jtag_info
->tap
,
507 etm_reg
->jtag_info
->intest_instr
,
510 if (retval
!= ERROR_OK
)
513 fields
[0].num_bits
= 32;
514 fields
[0].out_value
= reg
->value
;
515 fields
[0].in_value
= NULL
;
516 fields
[0].check_value
= NULL
;
517 fields
[0].check_mask
= NULL
;
519 fields
[1].num_bits
= 7;
521 fields
[1].out_value
= &temp1
;
522 buf_set_u32(&temp1
, 0, 7, reg_addr
);
523 fields
[1].in_value
= NULL
;
524 fields
[1].check_value
= NULL
;
525 fields
[1].check_mask
= NULL
;
527 fields
[2].num_bits
= 1;
529 fields
[2].out_value
= &temp2
;
530 buf_set_u32(&temp2
, 0, 1, 0);
531 fields
[2].in_value
= NULL
;
532 fields
[2].check_value
= NULL
;
533 fields
[2].check_mask
= NULL
;
535 jtag_add_dr_scan(etm_reg
->jtag_info
->tap
, 3, fields
, TAP_IDLE
);
537 fields
[0].in_value
= reg
->value
;
538 fields
[0].check_value
= check_value
;
539 fields
[0].check_mask
= check_mask
;
541 jtag_add_dr_scan_check(etm_reg
->jtag_info
->tap
, 3, fields
, TAP_IDLE
);
546 static int etm_set_reg(struct reg
*reg
, uint32_t value
)
548 int retval
= etm_write_reg(reg
, value
);
549 if (retval
!= ERROR_OK
) {
550 LOG_ERROR("BUG: error scheduling etm register write");
554 buf_set_u32(reg
->value
, 0, reg
->size
, value
);
561 static int etm_set_reg_w_exec(struct reg
*reg
, uint8_t *buf
)
565 etm_set_reg(reg
, buf_get_u32(buf
, 0, reg
->size
));
567 retval
= jtag_execute_queue();
568 if (retval
!= ERROR_OK
) {
569 LOG_ERROR("register write failed");
575 static int etm_write_reg(struct reg
*reg
, uint32_t value
)
577 struct etm_reg
*etm_reg
= reg
->arch_info
;
578 const struct etm_reg_info
*r
= etm_reg
->reg_info
;
579 uint8_t reg_addr
= r
->addr
& 0x7f;
580 struct scan_field fields
[3];
583 if (etm_reg
->reg_info
->mode
== RO
) {
584 LOG_ERROR("BUG: can't write read--only register %s", r
->name
);
585 return ERROR_COMMAND_SYNTAX_ERROR
;
588 LOG_DEBUG("%s (%u): 0x%8.8" PRIx32
"", r
->name
, reg_addr
, value
);
590 retval
= arm_jtag_scann(etm_reg
->jtag_info
, 0x6, TAP_IDLE
);
591 if (retval
!= ERROR_OK
)
593 retval
= arm_jtag_set_instr(etm_reg
->jtag_info
->tap
,
594 etm_reg
->jtag_info
->intest_instr
,
597 if (retval
!= ERROR_OK
)
600 fields
[0].num_bits
= 32;
602 fields
[0].out_value
= tmp1
;
603 buf_set_u32(tmp1
, 0, 32, value
);
604 fields
[0].in_value
= NULL
;
606 fields
[1].num_bits
= 7;
608 fields
[1].out_value
= &tmp2
;
609 buf_set_u32(&tmp2
, 0, 7, reg_addr
);
610 fields
[1].in_value
= NULL
;
612 fields
[2].num_bits
= 1;
614 fields
[2].out_value
= &tmp3
;
615 buf_set_u32(&tmp3
, 0, 1, 1);
616 fields
[2].in_value
= NULL
;
618 jtag_add_dr_scan(etm_reg
->jtag_info
->tap
, 3, fields
, TAP_IDLE
);
624 /* ETM trace analysis functionality */
626 static struct etm_capture_driver
*etm_capture_drivers
[] = {
628 &etm_dummy_capture_driver
,
632 static int etm_read_instruction(struct etm_context
*ctx
, struct arm_instruction
*instruction
)
640 return ERROR_TRACE_IMAGE_UNAVAILABLE
;
642 /* search for the section the current instruction belongs to */
643 for (unsigned int i
= 0; i
< ctx
->image
->num_sections
; i
++) {
644 if ((ctx
->image
->sections
[i
].base_address
<= ctx
->current_pc
) &&
645 (ctx
->image
->sections
[i
].base_address
+ ctx
->image
->sections
[i
].size
>
653 /* current instruction couldn't be found in the image */
654 return ERROR_TRACE_INSTRUCTION_UNAVAILABLE
;
657 if (ctx
->core_state
== ARM_STATE_ARM
) {
659 retval
= image_read_section(ctx
->image
, section
,
661 ctx
->image
->sections
[section
].base_address
,
663 if (retval
!= ERROR_OK
) {
664 LOG_ERROR("error while reading instruction");
665 return ERROR_TRACE_INSTRUCTION_UNAVAILABLE
;
667 opcode
= target_buffer_get_u32(ctx
->target
, buf
);
668 arm_evaluate_opcode(opcode
, ctx
->current_pc
, instruction
);
669 } else if (ctx
->core_state
== ARM_STATE_THUMB
) {
671 retval
= image_read_section(ctx
->image
, section
,
673 ctx
->image
->sections
[section
].base_address
,
675 if (retval
!= ERROR_OK
) {
676 LOG_ERROR("error while reading instruction");
677 return ERROR_TRACE_INSTRUCTION_UNAVAILABLE
;
679 opcode
= target_buffer_get_u16(ctx
->target
, buf
);
680 thumb_evaluate_opcode(opcode
, ctx
->current_pc
, instruction
);
681 } else if (ctx
->core_state
== ARM_STATE_JAZELLE
) {
682 LOG_ERROR("BUG: tracing of jazelle code not supported");
685 LOG_ERROR("BUG: unknown core state encountered");
692 static int etmv1_next_packet(struct etm_context
*ctx
, uint8_t *packet
, int apo
)
694 while (ctx
->data_index
< ctx
->trace_depth
) {
695 /* if the caller specified an address packet offset, skip until the
696 * we reach the n-th cycle marked with tracesync */
698 if (ctx
->trace_data
[ctx
->data_index
].flags
& ETMV1_TRACESYNC_CYCLE
)
708 /* no tracedata output during a TD cycle
709 * or in a trigger cycle */
710 if ((ctx
->trace_data
[ctx
->data_index
].pipestat
== STAT_TD
)
711 || (ctx
->trace_data
[ctx
->data_index
].flags
& ETMV1_TRIGGER_CYCLE
)) {
717 /* FIXME there are more port widths than these... */
718 if ((ctx
->control
& ETM_PORT_WIDTH_MASK
) == ETM_PORT_16BIT
) {
719 if (ctx
->data_half
== 0) {
720 *packet
= ctx
->trace_data
[ctx
->data_index
].packet
& 0xff;
723 *packet
= (ctx
->trace_data
[ctx
->data_index
].packet
& 0xff00) >> 8;
727 } else if ((ctx
->control
& ETM_PORT_WIDTH_MASK
) == ETM_PORT_8BIT
) {
728 *packet
= ctx
->trace_data
[ctx
->data_index
].packet
& 0xff;
731 /* on a 4-bit port, a packet will be output during two consecutive cycles */
732 if (ctx
->data_index
> (ctx
->trace_depth
- 2))
735 *packet
= ctx
->trace_data
[ctx
->data_index
].packet
& 0xf;
736 *packet
|= (ctx
->trace_data
[ctx
->data_index
+ 1].packet
& 0xf) << 4;
737 ctx
->data_index
+= 2;
746 static int etmv1_branch_address(struct etm_context
*ctx
)
754 /* quit analysis if less than two cycles are left in the trace
755 * because we can't extract the APO */
756 if (ctx
->data_index
> (ctx
->trace_depth
- 2))
759 /* a BE could be output during an APO cycle, skip the current
760 * and continue with the new one */
761 if (ctx
->trace_data
[ctx
->pipe_index
+ 1].pipestat
& 0x4)
763 if (ctx
->trace_data
[ctx
->pipe_index
+ 2].pipestat
& 0x4)
766 /* address packet offset encoded in the next two cycles' pipestat bits */
767 apo
= ctx
->trace_data
[ctx
->pipe_index
+ 1].pipestat
& 0x3;
768 apo
|= (ctx
->trace_data
[ctx
->pipe_index
+ 2].pipestat
& 0x3) << 2;
770 /* count number of tracesync cycles between current pipe_index and data_index
771 * i.e. the number of tracesyncs that data_index already passed by
772 * to subtract them from the APO */
773 for (i
= ctx
->pipe_index
; i
< ctx
->data_index
; i
++) {
774 if (ctx
->trace_data
[ctx
->pipe_index
+ 1].pipestat
& ETMV1_TRACESYNC_CYCLE
)
778 /* extract up to four 7-bit packets */
780 retval
= etmv1_next_packet(ctx
, &packet
, (shift
== 0) ? apo
+ 1 : 0);
783 ctx
->last_branch
&= ~(0x7f << shift
);
784 ctx
->last_branch
|= (packet
& 0x7f) << shift
;
786 } while ((packet
& 0x80) && (shift
< 28));
788 /* one last packet holding 4 bits of the address, plus the branch reason code */
789 if ((shift
== 28) && (packet
& 0x80)) {
790 retval
= etmv1_next_packet(ctx
, &packet
, 0);
793 ctx
->last_branch
&= 0x0fffffff;
794 ctx
->last_branch
|= (packet
& 0x0f) << 28;
795 ctx
->last_branch_reason
= (packet
& 0x70) >> 4;
798 ctx
->last_branch_reason
= 0;
803 /* if a full address was output, we might have branched into Jazelle state */
804 if ((shift
== 32) && (packet
& 0x80))
805 ctx
->core_state
= ARM_STATE_JAZELLE
;
807 /* if we didn't branch into Jazelle state, the current processor state is
808 * encoded in bit 0 of the branch target address */
809 if (ctx
->last_branch
& 0x1) {
810 ctx
->core_state
= ARM_STATE_THUMB
;
811 ctx
->last_branch
&= ~0x1;
813 ctx
->core_state
= ARM_STATE_ARM
;
814 ctx
->last_branch
&= ~0x3;
821 static int etmv1_data(struct etm_context
*ctx
, int size
, uint32_t *data
)
827 for (j
= 0; j
< size
; j
++) {
828 retval
= etmv1_next_packet(ctx
, &buf
[j
], 0);
834 LOG_ERROR("TODO: add support for 64-bit values");
836 } else if (size
== 4)
837 *data
= target_buffer_get_u32(ctx
->target
, buf
);
839 *data
= target_buffer_get_u16(ctx
->target
, buf
);
848 static int etmv1_analyze_trace(struct etm_context
*ctx
, struct command_invocation
*cmd
)
851 struct arm_instruction instruction
;
853 /* read the trace data if it wasn't read already */
854 if (ctx
->trace_depth
== 0)
855 ctx
->capture_driver
->read_trace(ctx
);
857 if (ctx
->trace_depth
== 0) {
858 command_print(cmd
, "Trace is empty.");
862 /* start at the beginning of the captured trace */
867 /* neither the PC nor the data pointer are valid */
871 while (ctx
->pipe_index
< ctx
->trace_depth
) {
872 uint8_t pipestat
= ctx
->trace_data
[ctx
->pipe_index
].pipestat
;
873 uint32_t next_pc
= ctx
->current_pc
;
874 uint32_t old_data_index
= ctx
->data_index
;
875 uint32_t old_data_half
= ctx
->data_half
;
876 uint32_t old_index
= ctx
->pipe_index
;
877 uint32_t last_instruction
= ctx
->last_instruction
;
879 int current_pc_ok
= ctx
->pc_ok
;
881 if (ctx
->trace_data
[ctx
->pipe_index
].flags
& ETMV1_TRIGGER_CYCLE
)
882 command_print(cmd
, "--- trigger ---");
884 /* instructions execute in IE/D or BE/D cycles */
885 if ((pipestat
== STAT_IE
) || (pipestat
== STAT_ID
))
886 ctx
->last_instruction
= ctx
->pipe_index
;
888 /* if we don't have a valid pc skip until we reach an indirect branch */
889 if ((!ctx
->pc_ok
) && (pipestat
!= STAT_BE
)) {
894 /* any indirect branch could have interrupted instruction flow
895 * - the branch reason code could indicate a trace discontinuity
896 * - a branch to the exception vectors indicates an exception
898 if ((pipestat
== STAT_BE
) || (pipestat
== STAT_BD
)) {
899 /* backup current data index, to be able to consume the branch address
900 * before examining data address and values
902 old_data_index
= ctx
->data_index
;
903 old_data_half
= ctx
->data_half
;
905 ctx
->last_instruction
= ctx
->pipe_index
;
907 retval
= etmv1_branch_address(ctx
);
909 /* negative return value from etmv1_branch_address means we ran out of packets,
910 * quit analysing the trace */
914 /* a positive return values means the current branch was abandoned,
915 * and a new branch was encountered in cycle ctx->pipe_index + retval;
918 "abandoned branch encountered, correctness of analysis uncertain");
919 ctx
->pipe_index
+= retval
;
923 /* skip over APO cycles */
924 ctx
->pipe_index
+= 2;
926 switch (ctx
->last_branch_reason
) {
927 case 0x0: /* normal PC change */
928 next_pc
= ctx
->last_branch
;
930 case 0x1: /* tracing enabled */
932 "--- tracing enabled at 0x%8.8" PRIx32
" ---",
934 ctx
->current_pc
= ctx
->last_branch
;
938 case 0x2: /* trace restarted after FIFO overflow */
940 "--- trace restarted after FIFO overflow at 0x%8.8" PRIx32
" ---",
942 ctx
->current_pc
= ctx
->last_branch
;
946 case 0x3: /* exit from debug state */
948 "--- exit from debug state at 0x%8.8" PRIx32
" ---",
950 ctx
->current_pc
= ctx
->last_branch
;
954 case 0x4: /* periodic synchronization point */
955 next_pc
= ctx
->last_branch
;
956 /* if we had no valid PC prior to this synchronization point,
957 * we have to move on with the next trace cycle
959 if (!current_pc_ok
) {
961 "--- periodic synchronization point at 0x%8.8" PRIx32
" ---",
963 ctx
->current_pc
= next_pc
;
968 default: /* reserved */
970 "BUG: branch reason code 0x%" PRIx32
" is reserved",
971 ctx
->last_branch_reason
);
975 /* if we got here the branch was a normal PC change
976 * (or a periodic synchronization point, which means the same for that matter)
977 * if we didn't acquire a complete PC continue with the next cycle
982 /* indirect branch to the exception vector means an exception occurred */
983 if ((ctx
->last_branch
<= 0x20)
984 || ((ctx
->last_branch
>= 0xffff0000) &&
985 (ctx
->last_branch
<= 0xffff0020))) {
986 if ((ctx
->last_branch
& 0xff) == 0x10)
987 command_print(cmd
, "data abort");
990 "exception vector 0x%2.2" PRIx32
"",
992 ctx
->current_pc
= ctx
->last_branch
;
999 /* an instruction was executed (or not, depending on the condition flags)
1000 * retrieve it from the image for displaying */
1001 if (ctx
->pc_ok
&& (pipestat
!= STAT_WT
) && (pipestat
!= STAT_TD
) &&
1002 !(((pipestat
== STAT_BE
) || (pipestat
== STAT_BD
)) &&
1003 ((ctx
->last_branch_reason
!= 0x0) && (ctx
->last_branch_reason
!= 0x4)))) {
1004 retval
= etm_read_instruction(ctx
, &instruction
);
1005 if (retval
!= ERROR_OK
) {
1006 /* can't continue tracing with no image available */
1007 if (retval
== ERROR_TRACE_IMAGE_UNAVAILABLE
)
1009 else if (retval
== ERROR_TRACE_INSTRUCTION_UNAVAILABLE
) {
1010 /* TODO: handle incomplete images
1011 * for now we just quit the analysis*/
1016 cycles
= old_index
- last_instruction
;
1019 if ((pipestat
== STAT_ID
) || (pipestat
== STAT_BD
)) {
1020 uint32_t new_data_index
= ctx
->data_index
;
1021 uint32_t new_data_half
= ctx
->data_half
;
1023 /* in case of a branch with data, the branch target address was consumed before
1024 * we temporarily go back to the saved data index */
1025 if (pipestat
== STAT_BD
) {
1026 ctx
->data_index
= old_data_index
;
1027 ctx
->data_half
= old_data_half
;
1030 if (ctx
->control
& ETM_CTRL_TRACE_ADDR
) {
1035 retval
= etmv1_next_packet(ctx
, &packet
, 0);
1037 return ERROR_ETM_ANALYSIS_FAILED
;
1038 ctx
->last_ptr
&= ~(0x7f << shift
);
1039 ctx
->last_ptr
|= (packet
& 0x7f) << shift
;
1041 } while ((packet
& 0x80) && (shift
< 32));
1048 "address: 0x%8.8" PRIx32
"",
1052 if (ctx
->control
& ETM_CTRL_TRACE_DATA
) {
1053 if ((instruction
.type
== ARM_LDM
) ||
1054 (instruction
.type
== ARM_STM
)) {
1056 for (i
= 0; i
< 16; i
++) {
1057 if (instruction
.info
.load_store_multiple
.register_list
1060 if (etmv1_data(ctx
, 4, &data
) != 0)
1061 return ERROR_ETM_ANALYSIS_FAILED
;
1063 "data: 0x%8.8" PRIx32
"",
1067 } else if ((instruction
.type
>= ARM_LDR
) &&
1068 (instruction
.type
<= ARM_STRH
)) {
1070 if (etmv1_data(ctx
, arm_access_size(&instruction
),
1072 return ERROR_ETM_ANALYSIS_FAILED
;
1073 command_print(cmd
, "data: 0x%8.8" PRIx32
"", data
);
1077 /* restore data index after consuming BD address and data */
1078 if (pipestat
== STAT_BD
) {
1079 ctx
->data_index
= new_data_index
;
1080 ctx
->data_half
= new_data_half
;
1085 if ((pipestat
== STAT_IE
) || (pipestat
== STAT_ID
)) {
1086 if (((instruction
.type
== ARM_B
) ||
1087 (instruction
.type
== ARM_BL
) ||
1088 (instruction
.type
== ARM_BLX
)) &&
1089 (instruction
.info
.b_bl_bx_blx
.target_address
!= 0xffffffff))
1090 next_pc
= instruction
.info
.b_bl_bx_blx
.target_address
;
1092 next_pc
+= (ctx
->core_state
== ARM_STATE_ARM
) ? 4 : 2;
1093 } else if (pipestat
== STAT_IN
)
1094 next_pc
+= (ctx
->core_state
== ARM_STATE_ARM
) ? 4 : 2;
1096 if ((pipestat
!= STAT_TD
) && (pipestat
!= STAT_WT
)) {
1097 char cycles_text
[32] = "";
1099 /* if the trace was captured with cycle accurate tracing enabled,
1100 * output the number of cycles since the last executed instruction
1102 if (ctx
->control
& ETM_CTRL_CYCLE_ACCURATE
) {
1103 snprintf(cycles_text
, 32, " (%i %s)",
1105 (cycles
== 1) ? "cycle" : "cycles");
1108 command_print(cmd
, "%s%s%s",
1110 (pipestat
== STAT_IN
) ? " (not executed)" : "",
1113 ctx
->current_pc
= next_pc
;
1115 /* packets for an instruction don't start on or before the preceding
1116 * functional pipestat (i.e. other than WT or TD)
1118 if (ctx
->data_index
<= ctx
->pipe_index
) {
1119 ctx
->data_index
= ctx
->pipe_index
+ 1;
1124 ctx
->pipe_index
+= 1;
1130 static COMMAND_HELPER(handle_etm_tracemode_command_update
,
1135 /* what parts of data access are traced? */
1136 if (strcmp(CMD_ARGV
[0], "none") == 0)
1138 else if (strcmp(CMD_ARGV
[0], "data") == 0)
1139 tracemode
= ETM_CTRL_TRACE_DATA
;
1140 else if (strcmp(CMD_ARGV
[0], "address") == 0)
1141 tracemode
= ETM_CTRL_TRACE_ADDR
;
1142 else if (strcmp(CMD_ARGV
[0], "all") == 0)
1143 tracemode
= ETM_CTRL_TRACE_DATA
| ETM_CTRL_TRACE_ADDR
;
1145 command_print(CMD
, "invalid option '%s'", CMD_ARGV
[0]);
1146 return ERROR_COMMAND_SYNTAX_ERROR
;
1150 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[1], context_id
);
1151 switch (context_id
) {
1153 tracemode
|= ETM_CTRL_CONTEXTID_NONE
;
1156 tracemode
|= ETM_CTRL_CONTEXTID_8
;
1159 tracemode
|= ETM_CTRL_CONTEXTID_16
;
1162 tracemode
|= ETM_CTRL_CONTEXTID_32
;
1165 command_print(CMD
, "invalid option '%s'", CMD_ARGV
[1]);
1166 return ERROR_COMMAND_SYNTAX_ERROR
;
1169 bool etmv1_cycle_accurate
;
1170 COMMAND_PARSE_ENABLE(CMD_ARGV
[2], etmv1_cycle_accurate
);
1171 if (etmv1_cycle_accurate
)
1172 tracemode
|= ETM_CTRL_CYCLE_ACCURATE
;
1174 bool etmv1_branch_output
;
1175 COMMAND_PARSE_ENABLE(CMD_ARGV
[3], etmv1_branch_output
);
1176 if (etmv1_branch_output
)
1177 tracemode
|= ETM_CTRL_BRANCH_OUTPUT
;
1180 * - CPRT tracing (coprocessor register transfers)
1181 * - debug request (causes debug entry on trigger)
1182 * - stall on FIFOFULL (preventing tracedata loss)
1189 COMMAND_HANDLER(handle_etm_tracemode_command
)
1191 struct target
*target
= get_current_target(CMD_CTX
);
1192 struct arm
*arm
= target_to_arm(target
);
1193 struct etm_context
*etm
;
1196 command_print(CMD
, "ETM: current target isn't an ARM");
1202 command_print(CMD
, "current target doesn't have an ETM configured");
1206 uint32_t tracemode
= etm
->control
;
1212 CALL_COMMAND_HANDLER(handle_etm_tracemode_command_update
,
1216 return ERROR_COMMAND_SYNTAX_ERROR
;
1220 * todo: fail if parameters were invalid for this hardware,
1221 * or couldn't be written; display actual hardware state...
1224 command_print(CMD
, "current tracemode configuration:");
1226 switch (tracemode
& ETM_CTRL_TRACE_MASK
) {
1228 command_print(CMD
, "data tracing: none");
1230 case ETM_CTRL_TRACE_DATA
:
1231 command_print(CMD
, "data tracing: data only");
1233 case ETM_CTRL_TRACE_ADDR
:
1234 command_print(CMD
, "data tracing: address only");
1236 case ETM_CTRL_TRACE_DATA
| ETM_CTRL_TRACE_ADDR
:
1237 command_print(CMD
, "data tracing: address and data");
1241 switch (tracemode
& ETM_CTRL_CONTEXTID_MASK
) {
1242 case ETM_CTRL_CONTEXTID_NONE
:
1243 command_print(CMD
, "contextid tracing: none");
1245 case ETM_CTRL_CONTEXTID_8
:
1246 command_print(CMD
, "contextid tracing: 8 bit");
1248 case ETM_CTRL_CONTEXTID_16
:
1249 command_print(CMD
, "contextid tracing: 16 bit");
1251 case ETM_CTRL_CONTEXTID_32
:
1252 command_print(CMD
, "contextid tracing: 32 bit");
1256 if (tracemode
& ETM_CTRL_CYCLE_ACCURATE
)
1257 command_print(CMD
, "cycle-accurate tracing enabled");
1259 command_print(CMD
, "cycle-accurate tracing disabled");
1261 if (tracemode
& ETM_CTRL_BRANCH_OUTPUT
)
1262 command_print(CMD
, "full branch address output enabled");
1264 command_print(CMD
, "full branch address output disabled");
1266 #define TRACEMODE_MASK ( \
1267 ETM_CTRL_CONTEXTID_MASK \
1268 | ETM_CTRL_BRANCH_OUTPUT \
1269 | ETM_CTRL_CYCLE_ACCURATE \
1270 | ETM_CTRL_TRACE_MASK \
1273 /* only update ETM_CTRL register if tracemode changed */
1274 if ((etm
->control
& TRACEMODE_MASK
) != tracemode
) {
1275 struct reg
*etm_ctrl_reg
;
1277 etm_ctrl_reg
= etm_reg_lookup(etm
, ETM_CTRL
);
1281 etm
->control
&= ~TRACEMODE_MASK
;
1282 etm
->control
|= tracemode
& TRACEMODE_MASK
;
1284 buf_set_u32(etm_ctrl_reg
->value
, 0, 32, etm
->control
);
1285 etm_store_reg(etm_ctrl_reg
);
1287 /* invalidate old trace data */
1288 etm
->capture_status
= TRACE_IDLE
;
1289 if (etm
->trace_depth
> 0) {
1290 free(etm
->trace_data
);
1291 etm
->trace_data
= NULL
;
1293 etm
->trace_depth
= 0;
1296 #undef TRACEMODE_MASK
1301 COMMAND_HANDLER(handle_etm_config_command
)
1303 struct target
*target
;
1305 uint32_t portmode
= 0x0;
1306 struct etm_context
*etm_ctx
;
1310 return ERROR_COMMAND_SYNTAX_ERROR
;
1312 target
= get_target(CMD_ARGV
[0]);
1314 LOG_ERROR("target '%s' not defined", CMD_ARGV
[0]);
1318 arm
= target_to_arm(target
);
1320 command_print(CMD
, "target '%s' is '%s'; not an ARM",
1321 target_name(target
),
1322 target_type_name(target
));
1326 /* FIXME for ETMv3.0 and above -- and we don't yet know what ETM
1327 * version we'll be using!! -- so we can't know how to validate
1328 * params yet. "etm config" should likely be *AFTER* hookup...
1330 * - Many more widths might be supported ... and we can easily
1331 * check whether our setting "took".
1333 * - The "clock" and "mode" bits are interpreted differently.
1334 * See ARM IHI 0014O table 2-17 for the old behaviour, and
1335 * table 2-18 for the new. With ETB it's best to specify
1339 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[1], port_width
);
1340 switch (port_width
) {
1341 /* before ETMv3.0 */
1343 portmode
|= ETM_PORT_4BIT
;
1346 portmode
|= ETM_PORT_8BIT
;
1349 portmode
|= ETM_PORT_16BIT
;
1351 /* ETMv3.0 and later*/
1353 portmode
|= ETM_PORT_24BIT
;
1356 portmode
|= ETM_PORT_32BIT
;
1359 portmode
|= ETM_PORT_48BIT
;
1362 portmode
|= ETM_PORT_64BIT
;
1365 portmode
|= ETM_PORT_1BIT
;
1368 portmode
|= ETM_PORT_2BIT
;
1372 "unsupported ETM port width '%s'", CMD_ARGV
[1]);
1376 if (strcmp("normal", CMD_ARGV
[2]) == 0)
1377 portmode
|= ETM_PORT_NORMAL
;
1378 else if (strcmp("multiplexed", CMD_ARGV
[2]) == 0)
1379 portmode
|= ETM_PORT_MUXED
;
1380 else if (strcmp("demultiplexed", CMD_ARGV
[2]) == 0)
1381 portmode
|= ETM_PORT_DEMUXED
;
1384 "unsupported ETM port mode '%s', must be 'normal', 'multiplexed' or 'demultiplexed'",
1389 if (strcmp("half", CMD_ARGV
[3]) == 0)
1390 portmode
|= ETM_PORT_HALF_CLOCK
;
1391 else if (strcmp("full", CMD_ARGV
[3]) == 0)
1392 portmode
|= ETM_PORT_FULL_CLOCK
;
1395 "unsupported ETM port clocking '%s', must be 'full' or 'half'",
1400 etm_ctx
= calloc(1, sizeof(struct etm_context
));
1402 LOG_DEBUG("out of memory");
1406 for (i
= 0; etm_capture_drivers
[i
]; i
++) {
1407 if (strcmp(CMD_ARGV
[4], etm_capture_drivers
[i
]->name
) == 0) {
1408 int retval
= register_commands(CMD_CTX
, NULL
, etm_capture_drivers
[i
]->commands
);
1409 if (retval
!= ERROR_OK
) {
1414 etm_ctx
->capture_driver
= etm_capture_drivers
[i
];
1420 if (!etm_capture_drivers
[i
]) {
1421 /* no supported capture driver found, don't register an ETM */
1423 LOG_ERROR("trace capture driver '%s' not found", CMD_ARGV
[4]);
1427 etm_ctx
->target
= target
;
1428 etm_ctx
->trace_data
= NULL
;
1429 etm_ctx
->control
= portmode
;
1430 etm_ctx
->core_state
= ARM_STATE_ARM
;
1434 return etm_register_user_commands(CMD_CTX
);
1437 COMMAND_HANDLER(handle_etm_info_command
)
1439 struct target
*target
;
1441 struct etm_context
*etm
;
1442 struct reg
*etm_sys_config_reg
;
1446 target
= get_current_target(CMD_CTX
);
1447 arm
= target_to_arm(target
);
1449 command_print(CMD
, "ETM: current target isn't an ARM");
1455 command_print(CMD
, "current target doesn't have an ETM configured");
1459 command_print(CMD
, "ETM v%d.%d",
1460 etm
->bcd_vers
>> 4, etm
->bcd_vers
& 0xf);
1461 command_print(CMD
, "pairs of address comparators: %i",
1462 (int) (etm
->config
>> 0) & 0x0f);
1463 command_print(CMD
, "data comparators: %i",
1464 (int) (etm
->config
>> 4) & 0x0f);
1465 command_print(CMD
, "memory map decoders: %i",
1466 (int) (etm
->config
>> 8) & 0x1f);
1467 command_print(CMD
, "number of counters: %i",
1468 (int) (etm
->config
>> 13) & 0x07);
1469 command_print(CMD
, "sequencer %spresent",
1470 (int) (etm
->config
& (1 << 16)) ? "" : "not ");
1471 command_print(CMD
, "number of ext. inputs: %i",
1472 (int) (etm
->config
>> 17) & 0x07);
1473 command_print(CMD
, "number of ext. outputs: %i",
1474 (int) (etm
->config
>> 20) & 0x07);
1475 command_print(CMD
, "FIFO full %spresent",
1476 (int) (etm
->config
& (1 << 23)) ? "" : "not ");
1477 if (etm
->bcd_vers
< 0x20)
1478 command_print(CMD
, "protocol version: %i",
1479 (int) (etm
->config
>> 28) & 0x07);
1482 "coprocessor and memory access %ssupported",
1483 (etm
->config
& (1 << 26)) ? "" : "not ");
1484 command_print(CMD
, "trace start/stop %spresent",
1485 (etm
->config
& (1 << 26)) ? "" : "not ");
1486 command_print(CMD
, "number of context comparators: %i",
1487 (int) (etm
->config
>> 24) & 0x03);
1490 /* SYS_CONFIG isn't present before ETMv1.2 */
1491 etm_sys_config_reg
= etm_reg_lookup(etm
, ETM_SYS_CONFIG
);
1492 if (!etm_sys_config_reg
)
1495 etm_get_reg(etm_sys_config_reg
);
1496 config
= buf_get_u32(etm_sys_config_reg
->value
, 0, 32);
1498 LOG_DEBUG("ETM SYS CONFIG %08x", (unsigned) config
);
1500 max_port_size
= config
& 0x7;
1501 if (etm
->bcd_vers
>= 0x30)
1502 max_port_size
|= (config
>> 6) & 0x08;
1503 switch (max_port_size
) {
1504 /* before ETMv3.0 */
1514 /* ETMv3.0 and later*/
1534 LOG_ERROR("Illegal max_port_size");
1537 command_print(CMD
, "max. port size: %i", max_port_size
);
1539 if (etm
->bcd_vers
< 0x30) {
1540 command_print(CMD
, "half-rate clocking %ssupported",
1541 (config
& (1 << 3)) ? "" : "not ");
1542 command_print(CMD
, "full-rate clocking %ssupported",
1543 (config
& (1 << 4)) ? "" : "not ");
1544 command_print(CMD
, "normal trace format %ssupported",
1545 (config
& (1 << 5)) ? "" : "not ");
1546 command_print(CMD
, "multiplex trace format %ssupported",
1547 (config
& (1 << 6)) ? "" : "not ");
1548 command_print(CMD
, "demultiplex trace format %ssupported",
1549 (config
& (1 << 7)) ? "" : "not ");
1551 /* REVISIT show which size and format are selected ... */
1552 command_print(CMD
, "current port size %ssupported",
1553 (config
& (1 << 10)) ? "" : "not ");
1554 command_print(CMD
, "current trace format %ssupported",
1555 (config
& (1 << 11)) ? "" : "not ");
1557 if (etm
->bcd_vers
>= 0x21)
1558 command_print(CMD
, "fetch comparisons %ssupported",
1559 (config
& (1 << 17)) ? "not " : "");
1560 command_print(CMD
, "FIFO full %ssupported",
1561 (config
& (1 << 8)) ? "" : "not ");
1566 COMMAND_HANDLER(handle_etm_status_command
)
1568 struct target
*target
;
1570 struct etm_context
*etm
;
1571 trace_status_t trace_status
;
1573 target
= get_current_target(CMD_CTX
);
1574 arm
= target_to_arm(target
);
1576 command_print(CMD
, "ETM: current target isn't an ARM");
1582 command_print(CMD
, "current target doesn't have an ETM configured");
1587 if (etm
->bcd_vers
>= 0x11) {
1590 reg
= etm_reg_lookup(etm
, ETM_STATUS
);
1593 if (etm_get_reg(reg
) == ERROR_OK
) {
1594 unsigned s
= buf_get_u32(reg
->value
, 0, reg
->size
);
1596 command_print(CMD
, "etm: %s%s%s%s",
1597 /* bit(1) == progbit */
1598 (etm
->bcd_vers
>= 0x12)
1600 ? "disabled" : "enabled")
1602 ((s
& (1 << 3)) && etm
->bcd_vers
>= 0x31)
1603 ? " triggered" : "",
1604 ((s
& (1 << 2)) && etm
->bcd_vers
>= 0x12)
1605 ? " start/stop" : "",
1606 ((s
& (1 << 0)) && etm
->bcd_vers
>= 0x11)
1607 ? " untraced-overflow" : "");
1608 } /* else ignore and try showing trace port status */
1611 /* Trace Port Driver status */
1612 trace_status
= etm
->capture_driver
->status(etm
);
1613 if (trace_status
== TRACE_IDLE
)
1614 command_print(CMD
, "%s: idle", etm
->capture_driver
->name
);
1616 static char *completed
= " completed";
1617 static char *running
= " is running";
1618 static char *overflowed
= ", overflowed";
1619 static char *triggered
= ", triggered";
1621 command_print(CMD
, "%s: trace collection%s%s%s",
1622 etm
->capture_driver
->name
,
1623 (trace_status
& TRACE_RUNNING
) ? running
: completed
,
1624 (trace_status
& TRACE_OVERFLOWED
) ? overflowed
: "",
1625 (trace_status
& TRACE_TRIGGERED
) ? triggered
: "");
1627 if (etm
->trace_depth
> 0) {
1628 command_print(CMD
, "%i frames of trace data read",
1629 (int)(etm
->trace_depth
));
1636 COMMAND_HANDLER(handle_etm_image_command
)
1638 struct target
*target
;
1640 struct etm_context
*etm_ctx
;
1643 return ERROR_COMMAND_SYNTAX_ERROR
;
1645 target
= get_current_target(CMD_CTX
);
1646 arm
= target_to_arm(target
);
1648 command_print(CMD
, "ETM: current target isn't an ARM");
1654 command_print(CMD
, "current target doesn't have an ETM configured");
1658 if (etm_ctx
->image
) {
1659 image_close(etm_ctx
->image
);
1660 free(etm_ctx
->image
);
1661 command_print(CMD
, "previously loaded image found and closed");
1664 etm_ctx
->image
= malloc(sizeof(struct image
));
1665 etm_ctx
->image
->base_address_set
= false;
1666 etm_ctx
->image
->start_address_set
= false;
1668 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1669 if (CMD_ARGC
>= 2) {
1670 etm_ctx
->image
->base_address_set
= true;
1671 COMMAND_PARSE_NUMBER(llong
, CMD_ARGV
[1], etm_ctx
->image
->base_address
);
1673 etm_ctx
->image
->base_address_set
= false;
1675 if (image_open(etm_ctx
->image
, CMD_ARGV
[0],
1676 (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
) {
1677 free(etm_ctx
->image
);
1678 etm_ctx
->image
= NULL
;
1685 COMMAND_HANDLER(handle_etm_dump_command
)
1687 struct fileio
*file
;
1688 struct target
*target
;
1690 struct etm_context
*etm_ctx
;
1694 return ERROR_COMMAND_SYNTAX_ERROR
;
1696 target
= get_current_target(CMD_CTX
);
1697 arm
= target_to_arm(target
);
1699 command_print(CMD
, "ETM: current target isn't an ARM");
1705 command_print(CMD
, "current target doesn't have an ETM configured");
1709 if (etm_ctx
->capture_driver
->status(etm_ctx
) == TRACE_IDLE
) {
1710 command_print(CMD
, "trace capture wasn't enabled, no trace data captured");
1714 if (etm_ctx
->capture_driver
->status(etm_ctx
) & TRACE_RUNNING
) {
1715 /* TODO: if on-the-fly capture is to be supported, this needs to be changed */
1716 command_print(CMD
, "trace capture not completed");
1720 /* read the trace data if it wasn't read already */
1721 if (etm_ctx
->trace_depth
== 0)
1722 etm_ctx
->capture_driver
->read_trace(etm_ctx
);
1724 if (fileio_open(&file
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
1727 fileio_write_u32(file
, etm_ctx
->capture_status
);
1728 fileio_write_u32(file
, etm_ctx
->control
);
1729 fileio_write_u32(file
, etm_ctx
->trace_depth
);
1731 for (i
= 0; i
< etm_ctx
->trace_depth
; i
++) {
1732 fileio_write_u32(file
, etm_ctx
->trace_data
[i
].pipestat
);
1733 fileio_write_u32(file
, etm_ctx
->trace_data
[i
].packet
);
1734 fileio_write_u32(file
, etm_ctx
->trace_data
[i
].flags
);
1742 COMMAND_HANDLER(handle_etm_load_command
)
1744 struct fileio
*file
;
1745 struct target
*target
;
1747 struct etm_context
*etm_ctx
;
1751 return ERROR_COMMAND_SYNTAX_ERROR
;
1753 target
= get_current_target(CMD_CTX
);
1754 arm
= target_to_arm(target
);
1756 command_print(CMD
, "ETM: current target isn't an ARM");
1762 command_print(CMD
, "current target doesn't have an ETM configured");
1766 if (etm_ctx
->capture_driver
->status(etm_ctx
) & TRACE_RUNNING
) {
1767 command_print(CMD
, "trace capture running, stop first");
1771 if (fileio_open(&file
, CMD_ARGV
[0], FILEIO_READ
, FILEIO_BINARY
) != ERROR_OK
)
1775 int retval
= fileio_size(file
, &filesize
);
1776 if (retval
!= ERROR_OK
) {
1782 command_print(CMD
, "size isn't a multiple of 4, no valid trace data");
1787 if (etm_ctx
->trace_depth
> 0) {
1788 free(etm_ctx
->trace_data
);
1789 etm_ctx
->trace_data
= NULL
;
1794 fileio_read_u32(file
, &tmp
); etm_ctx
->capture_status
= tmp
;
1795 fileio_read_u32(file
, &tmp
); etm_ctx
->control
= tmp
;
1796 fileio_read_u32(file
, &etm_ctx
->trace_depth
);
1798 etm_ctx
->trace_data
= malloc(sizeof(struct etmv1_trace_data
) * etm_ctx
->trace_depth
);
1799 if (!etm_ctx
->trace_data
) {
1800 command_print(CMD
, "not enough memory to perform operation");
1805 for (i
= 0; i
< etm_ctx
->trace_depth
; i
++) {
1806 uint32_t pipestat
, packet
, flags
;
1807 fileio_read_u32(file
, &pipestat
);
1808 fileio_read_u32(file
, &packet
);
1809 fileio_read_u32(file
, &flags
);
1810 etm_ctx
->trace_data
[i
].pipestat
= pipestat
& 0xff;
1811 etm_ctx
->trace_data
[i
].packet
= packet
& 0xffff;
1812 etm_ctx
->trace_data
[i
].flags
= flags
;
1820 COMMAND_HANDLER(handle_etm_start_command
)
1822 struct target
*target
;
1824 struct etm_context
*etm_ctx
;
1825 struct reg
*etm_ctrl_reg
;
1827 target
= get_current_target(CMD_CTX
);
1828 arm
= target_to_arm(target
);
1830 command_print(CMD
, "ETM: current target isn't an ARM");
1836 command_print(CMD
, "current target doesn't have an ETM configured");
1840 /* invalidate old tracing data */
1841 etm_ctx
->capture_status
= TRACE_IDLE
;
1842 if (etm_ctx
->trace_depth
> 0) {
1843 free(etm_ctx
->trace_data
);
1844 etm_ctx
->trace_data
= NULL
;
1846 etm_ctx
->trace_depth
= 0;
1848 etm_ctrl_reg
= etm_reg_lookup(etm_ctx
, ETM_CTRL
);
1852 etm_get_reg(etm_ctrl_reg
);
1854 /* Clear programming bit (10), set port selection bit (11) */
1855 buf_set_u32(etm_ctrl_reg
->value
, 10, 2, 0x2);
1857 etm_store_reg(etm_ctrl_reg
);
1858 jtag_execute_queue();
1860 etm_ctx
->capture_driver
->start_capture(etm_ctx
);
1865 COMMAND_HANDLER(handle_etm_stop_command
)
1867 struct target
*target
;
1869 struct etm_context
*etm_ctx
;
1870 struct reg
*etm_ctrl_reg
;
1872 target
= get_current_target(CMD_CTX
);
1873 arm
= target_to_arm(target
);
1875 command_print(CMD
, "ETM: current target isn't an ARM");
1881 command_print(CMD
, "current target doesn't have an ETM configured");
1885 etm_ctrl_reg
= etm_reg_lookup(etm_ctx
, ETM_CTRL
);
1889 etm_get_reg(etm_ctrl_reg
);
1891 /* Set programming bit (10), clear port selection bit (11) */
1892 buf_set_u32(etm_ctrl_reg
->value
, 10, 2, 0x1);
1894 etm_store_reg(etm_ctrl_reg
);
1895 jtag_execute_queue();
1897 etm_ctx
->capture_driver
->stop_capture(etm_ctx
);
1902 COMMAND_HANDLER(handle_etm_trigger_debug_command
)
1904 struct target
*target
;
1906 struct etm_context
*etm
;
1908 target
= get_current_target(CMD_CTX
);
1909 arm
= target_to_arm(target
);
1911 command_print(CMD
, "ETM: %s isn't an ARM",
1912 target_name(target
));
1918 command_print(CMD
, "ETM: no ETM configured for %s",
1919 target_name(target
));
1923 if (CMD_ARGC
== 1) {
1924 struct reg
*etm_ctrl_reg
;
1927 etm_ctrl_reg
= etm_reg_lookup(etm
, ETM_CTRL
);
1931 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], dbgrq
);
1933 etm
->control
|= ETM_CTRL_DBGRQ
;
1935 etm
->control
&= ~ETM_CTRL_DBGRQ
;
1937 /* etm->control will be written to hardware
1938 * the next time an "etm start" is issued.
1940 buf_set_u32(etm_ctrl_reg
->value
, 0, 32, etm
->control
);
1943 command_print(CMD
, "ETM: %s debug halt",
1944 (etm
->control
& ETM_CTRL_DBGRQ
)
1946 : "does not trigger");
1950 COMMAND_HANDLER(handle_etm_analyze_command
)
1952 struct target
*target
;
1954 struct etm_context
*etm_ctx
;
1957 target
= get_current_target(CMD_CTX
);
1958 arm
= target_to_arm(target
);
1960 command_print(CMD
, "ETM: current target isn't an ARM");
1966 command_print(CMD
, "current target doesn't have an ETM configured");
1970 retval
= etmv1_analyze_trace(etm_ctx
, CMD
);
1971 if (retval
!= ERROR_OK
) {
1972 /* FIX! error should be reported inside etmv1_analyze_trace() */
1974 case ERROR_ETM_ANALYSIS_FAILED
:
1976 "further analysis failed (corrupted trace data or just end of data");
1978 case ERROR_TRACE_INSTRUCTION_UNAVAILABLE
:
1980 "no instruction for current address available, analysis aborted");
1982 case ERROR_TRACE_IMAGE_UNAVAILABLE
:
1983 command_print(CMD
, "no image available for trace analysis");
1986 command_print(CMD
, "unknown error");
1993 static const struct command_registration etm_config_command_handlers
[] = {
1995 /* NOTE: with ADIv5, ETMs are accessed by DAP operations,
1996 * possibly over SWD, not JTAG scanchain 6 of 'target'.
1998 * Also, these parameters don't match ETM v3+ modules...
2001 .handler
= handle_etm_config_command
,
2002 .mode
= COMMAND_CONFIG
,
2003 .help
= "Set up ETM output port.",
2004 .usage
= "target port_width port_mode clocking capture_driver",
2006 COMMAND_REGISTRATION_DONE
2008 const struct command_registration etm_command_handlers
[] = {
2011 .mode
= COMMAND_ANY
,
2012 .help
= "Embedded Trace Macrocell command group",
2014 .chain
= etm_config_command_handlers
,
2016 COMMAND_REGISTRATION_DONE
2019 static const struct command_registration etm_exec_command_handlers
[] = {
2021 .name
= "tracemode",
2022 .handler
= handle_etm_tracemode_command
,
2023 .mode
= COMMAND_EXEC
,
2024 .help
= "configure/display trace mode",
2025 .usage
= "('none'|'data'|'address'|'all') "
2027 "['enable'|'disable'] "
2028 "['enable'|'disable']",
2032 .handler
= handle_etm_info_command
,
2033 .mode
= COMMAND_EXEC
,
2035 .help
= "display info about the current target's ETM",
2039 .handler
= handle_etm_status_command
,
2040 .mode
= COMMAND_EXEC
,
2042 .help
= "display current target's ETM status",
2046 .handler
= handle_etm_start_command
,
2047 .mode
= COMMAND_EXEC
,
2049 .help
= "start ETM trace collection",
2053 .handler
= handle_etm_stop_command
,
2054 .mode
= COMMAND_EXEC
,
2056 .help
= "stop ETM trace collection",
2059 .name
= "trigger_debug",
2060 .handler
= handle_etm_trigger_debug_command
,
2061 .mode
= COMMAND_EXEC
,
2062 .help
= "enable/disable debug entry on trigger",
2063 .usage
= "['enable'|'disable']",
2067 .handler
= handle_etm_analyze_command
,
2068 .mode
= COMMAND_EXEC
,
2070 .help
= "analyze collected ETM trace",
2074 .handler
= handle_etm_image_command
,
2075 .mode
= COMMAND_EXEC
,
2076 .help
= "load image from file with optional offset",
2077 .usage
= "<file> [base address] [type]",
2081 .handler
= handle_etm_dump_command
,
2082 .mode
= COMMAND_EXEC
,
2083 .help
= "dump captured trace data to file",
2084 .usage
= "filename",
2088 .handler
= handle_etm_load_command
,
2089 .mode
= COMMAND_EXEC
,
2091 .help
= "load trace data for analysis <file>",
2093 COMMAND_REGISTRATION_DONE
2096 static int etm_register_user_commands(struct command_context
*cmd_ctx
)
2098 return register_commands(cmd_ctx
, "etm", etm_exec_command_handlers
);