1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
5 * Author: Tor Jeremiassen <tor@ti.com>
6 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
9 #include <linux/bitops.h>
10 #include <linux/coresight-pmu.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
17 #include <opencsd/ocsd_if_types.h>
23 #include "cs-etm-decoder/cs-etm-decoder.h"
32 #include "map_symbol.h"
37 #include "thread-stack.h"
38 #include <tools/libc_compat.h>
39 #include "util/synthetic-events.h"
41 struct cs_etm_auxtrace {
42 struct auxtrace auxtrace;
43 struct auxtrace_queues queues;
44 struct auxtrace_heap heap;
45 struct itrace_synth_opts synth_opts;
46 struct perf_session *session;
47 struct machine *machine;
48 struct thread *unknown_thread;
55 u64 latest_kernel_timestamp;
57 u64 branches_sample_type;
59 u64 instructions_sample_type;
60 u64 instructions_sample_period;
63 unsigned int pmu_type;
66 struct cs_etm_traceid_queue {
69 u64 period_instructions;
70 size_t last_branch_pos;
71 union perf_event *event_buf;
72 struct thread *thread;
73 struct branch_stack *last_branch;
74 struct branch_stack *last_branch_rb;
75 struct cs_etm_packet *prev_packet;
76 struct cs_etm_packet *packet;
77 struct cs_etm_packet_queue packet_queue;
81 struct cs_etm_auxtrace *etm;
82 struct cs_etm_decoder *decoder;
83 struct auxtrace_buffer *buffer;
84 unsigned int queue_nr;
85 u8 pending_timestamp_chan_id;
87 const unsigned char *buf;
88 size_t buf_len, buf_used;
89 /* Conversion between traceID and index in traceid_queues array */
90 struct intlist *traceid_queues_list;
91 struct cs_etm_traceid_queue **traceid_queues;
94 /* RB tree for quick conversion between traceID and metadata pointers */
95 static struct intlist *traceid_list;
97 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
98 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
100 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
101 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
103 /* PTMs ETMIDR [11:8] set to b0011 */
104 #define ETMIDR_PTM_VERSION 0x00000300
107 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
108 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
109 * encode the etm queue number as the upper 16 bit and the channel as
112 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
113 (queue_nr << 16 | trace_chan_id)
114 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
115 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
117 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
119 etmidr &= ETMIDR_PTM_VERSION;
121 if (etmidr == ETMIDR_PTM_VERSION)
122 return CS_ETM_PROTO_PTM;
124 return CS_ETM_PROTO_ETMV3;
127 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
129 struct int_node *inode;
132 inode = intlist__find(traceid_list, trace_chan_id);
136 metadata = inode->priv;
137 *magic = metadata[CS_ETM_MAGIC];
141 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
143 struct int_node *inode;
146 inode = intlist__find(traceid_list, trace_chan_id);
150 metadata = inode->priv;
151 *cpu = (int)metadata[CS_ETM_CPU];
156 * The returned PID format is presented by two bits:
158 * Bit ETM_OPT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced;
159 * Bit ETM_OPT_CTXTID2: CONTEXTIDR_EL2 is traced.
161 * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
162 * are enabled at the same time when the session runs on an EL2 kernel.
163 * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
164 * recorded in the trace data, the tool will selectively use
165 * CONTEXTIDR_EL2 as PID.
167 int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt)
169 struct int_node *inode;
172 inode = intlist__find(traceid_list, trace_chan_id);
176 metadata = inode->priv;
178 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
179 val = metadata[CS_ETM_ETMCR];
180 /* CONTEXTIDR is traced */
181 if (val & BIT(ETM_OPT_CTXTID))
182 *pid_fmt = BIT(ETM_OPT_CTXTID);
184 val = metadata[CS_ETMV4_TRCCONFIGR];
185 /* CONTEXTIDR_EL2 is traced */
186 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
187 *pid_fmt = BIT(ETM_OPT_CTXTID2);
188 /* CONTEXTIDR_EL1 is traced */
189 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
190 *pid_fmt = BIT(ETM_OPT_CTXTID);
196 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
200 * When a timestamp packet is encountered the backend code
201 * is stopped so that the front end has time to process packets
202 * that were accumulated in the traceID queue. Since there can
203 * be more than one channel per cs_etm_queue, we need to specify
204 * what traceID queue needs servicing.
206 etmq->pending_timestamp_chan_id = trace_chan_id;
209 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
212 struct cs_etm_packet_queue *packet_queue;
214 if (!etmq->pending_timestamp_chan_id)
218 *trace_chan_id = etmq->pending_timestamp_chan_id;
220 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
221 etmq->pending_timestamp_chan_id);
225 /* Acknowledge pending status */
226 etmq->pending_timestamp_chan_id = 0;
228 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
229 return packet_queue->cs_timestamp;
232 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
238 queue->packet_count = 0;
239 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
240 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
241 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
242 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
243 queue->packet_buffer[i].instr_count = 0;
244 queue->packet_buffer[i].last_instr_taken_branch = false;
245 queue->packet_buffer[i].last_instr_size = 0;
246 queue->packet_buffer[i].last_instr_type = 0;
247 queue->packet_buffer[i].last_instr_subtype = 0;
248 queue->packet_buffer[i].last_instr_cond = 0;
249 queue->packet_buffer[i].flags = 0;
250 queue->packet_buffer[i].exception_number = UINT32_MAX;
251 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
252 queue->packet_buffer[i].cpu = INT_MIN;
256 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
259 struct int_node *inode;
260 struct cs_etm_traceid_queue *tidq;
261 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
263 intlist__for_each_entry(inode, traceid_queues_list) {
264 idx = (int)(intptr_t)inode->priv;
265 tidq = etmq->traceid_queues[idx];
266 cs_etm__clear_packet_queue(&tidq->packet_queue);
270 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
271 struct cs_etm_traceid_queue *tidq,
275 struct auxtrace_queue *queue;
276 struct cs_etm_auxtrace *etm = etmq->etm;
278 cs_etm__clear_packet_queue(&tidq->packet_queue);
280 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
281 tidq->tid = queue->tid;
283 tidq->trace_chan_id = trace_chan_id;
285 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
289 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
290 if (!tidq->prev_packet)
293 if (etm->synth_opts.last_branch) {
294 size_t sz = sizeof(struct branch_stack);
296 sz += etm->synth_opts.last_branch_sz *
297 sizeof(struct branch_entry);
298 tidq->last_branch = zalloc(sz);
299 if (!tidq->last_branch)
301 tidq->last_branch_rb = zalloc(sz);
302 if (!tidq->last_branch_rb)
306 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
307 if (!tidq->event_buf)
313 zfree(&tidq->last_branch_rb);
314 zfree(&tidq->last_branch);
315 zfree(&tidq->prev_packet);
316 zfree(&tidq->packet);
321 static struct cs_etm_traceid_queue
322 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
325 struct int_node *inode;
326 struct intlist *traceid_queues_list;
327 struct cs_etm_traceid_queue *tidq, **traceid_queues;
328 struct cs_etm_auxtrace *etm = etmq->etm;
330 if (etm->timeless_decoding)
331 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
333 traceid_queues_list = etmq->traceid_queues_list;
336 * Check if the traceid_queue exist for this traceID by looking
339 inode = intlist__find(traceid_queues_list, trace_chan_id);
341 idx = (int)(intptr_t)inode->priv;
342 return etmq->traceid_queues[idx];
345 /* We couldn't find a traceid_queue for this traceID, allocate one */
346 tidq = malloc(sizeof(*tidq));
350 memset(tidq, 0, sizeof(*tidq));
352 /* Get a valid index for the new traceid_queue */
353 idx = intlist__nr_entries(traceid_queues_list);
354 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
355 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
359 /* Associate this traceID with this index */
360 inode->priv = (void *)(intptr_t)idx;
362 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
365 /* Grow the traceid_queues array by one unit */
366 traceid_queues = etmq->traceid_queues;
367 traceid_queues = reallocarray(traceid_queues,
369 sizeof(*traceid_queues));
372 * On failure reallocarray() returns NULL and the original block of
373 * memory is left untouched.
378 traceid_queues[idx] = tidq;
379 etmq->traceid_queues = traceid_queues;
381 return etmq->traceid_queues[idx];
385 * Function intlist__remove() removes the inode from the list
386 * and delete the memory associated to it.
388 intlist__remove(traceid_queues_list, inode);
394 struct cs_etm_packet_queue
395 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
397 struct cs_etm_traceid_queue *tidq;
399 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
401 return &tidq->packet_queue;
406 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
407 struct cs_etm_traceid_queue *tidq)
409 struct cs_etm_packet *tmp;
411 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
412 etm->synth_opts.instructions) {
414 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
415 * the next incoming packet.
418 tidq->packet = tidq->prev_packet;
419 tidq->prev_packet = tmp;
423 static void cs_etm__packet_dump(const char *pkt_string)
425 const char *color = PERF_COLOR_BLUE;
426 int len = strlen(pkt_string);
428 if (len && (pkt_string[len-1] == '\n'))
429 color_fprintf(stdout, color, " %s", pkt_string);
431 color_fprintf(stdout, color, " %s\n", pkt_string);
436 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
437 struct cs_etm_auxtrace *etm, int idx,
440 u64 **metadata = etm->metadata;
442 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
443 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
444 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
447 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
448 struct cs_etm_auxtrace *etm, int idx)
450 u64 **metadata = etm->metadata;
452 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
453 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
454 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
455 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
456 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
457 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
458 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
461 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
462 struct cs_etm_auxtrace *etm, int idx)
464 u64 **metadata = etm->metadata;
466 t_params[idx].protocol = CS_ETM_PROTO_ETE;
467 t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
468 t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
469 t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
470 t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
471 t_params[idx].ete.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
472 t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
473 t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
476 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
477 struct cs_etm_auxtrace *etm,
484 for (i = 0; i < decoders; i++) {
485 architecture = etm->metadata[i][CS_ETM_MAGIC];
487 switch (architecture) {
488 case __perf_cs_etmv3_magic:
489 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
490 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
492 case __perf_cs_etmv4_magic:
493 cs_etm__set_trace_param_etmv4(t_params, etm, i);
495 case __perf_cs_ete_magic:
496 cs_etm__set_trace_param_ete(t_params, etm, i);
506 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
507 struct cs_etm_queue *etmq,
508 enum cs_etm_decoder_operation mode,
513 if (!(mode < CS_ETM_OPERATION_MAX))
516 d_params->packet_printer = cs_etm__packet_dump;
517 d_params->operation = mode;
518 d_params->data = etmq;
519 d_params->formatted = formatted;
520 d_params->fsyncs = false;
521 d_params->hsyncs = false;
522 d_params->frame_aligned = true;
529 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
530 struct auxtrace_buffer *buffer)
533 const char *color = PERF_COLOR_BLUE;
534 size_t buffer_used = 0;
536 fprintf(stdout, "\n");
537 color_fprintf(stdout, color,
538 ". ... CoreSight %s Trace data: size %#zx bytes\n",
539 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
544 ret = cs_etm_decoder__process_data_block(
545 etmq->decoder, buffer->offset,
546 &((u8 *)buffer->data)[buffer_used],
547 buffer->size - buffer_used, &consumed);
551 buffer_used += consumed;
552 } while (buffer_used < buffer->size);
554 cs_etm_decoder__reset(etmq->decoder);
557 static int cs_etm__flush_events(struct perf_session *session,
558 struct perf_tool *tool)
560 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
561 struct cs_etm_auxtrace,
566 if (!tool->ordered_events)
569 if (etm->timeless_decoding)
570 return cs_etm__process_timeless_queues(etm, -1);
572 return cs_etm__process_queues(etm);
575 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
579 struct int_node *inode, *tmp;
580 struct cs_etm_traceid_queue *tidq;
581 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
583 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
584 priv = (uintptr_t)inode->priv;
587 /* Free this traceid_queue from the array */
588 tidq = etmq->traceid_queues[idx];
589 thread__zput(tidq->thread);
590 zfree(&tidq->event_buf);
591 zfree(&tidq->last_branch);
592 zfree(&tidq->last_branch_rb);
593 zfree(&tidq->prev_packet);
594 zfree(&tidq->packet);
598 * Function intlist__remove() removes the inode from the list
599 * and delete the memory associated to it.
601 intlist__remove(traceid_queues_list, inode);
604 /* Then the RB tree itself */
605 intlist__delete(traceid_queues_list);
606 etmq->traceid_queues_list = NULL;
608 /* finally free the traceid_queues array */
609 zfree(&etmq->traceid_queues);
612 static void cs_etm__free_queue(void *priv)
614 struct cs_etm_queue *etmq = priv;
619 cs_etm_decoder__free(etmq->decoder);
620 cs_etm__free_traceid_queues(etmq);
624 static void cs_etm__free_events(struct perf_session *session)
627 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
628 struct cs_etm_auxtrace,
630 struct auxtrace_queues *queues = &aux->queues;
632 for (i = 0; i < queues->nr_queues; i++) {
633 cs_etm__free_queue(queues->queue_array[i].priv);
634 queues->queue_array[i].priv = NULL;
637 auxtrace_queues__free(queues);
640 static void cs_etm__free(struct perf_session *session)
643 struct int_node *inode, *tmp;
644 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
645 struct cs_etm_auxtrace,
647 cs_etm__free_events(session);
648 session->auxtrace = NULL;
650 /* First remove all traceID/metadata nodes for the RB tree */
651 intlist__for_each_entry_safe(inode, tmp, traceid_list)
652 intlist__remove(traceid_list, inode);
653 /* Then the RB tree itself */
654 intlist__delete(traceid_list);
656 for (i = 0; i < aux->num_cpu; i++)
657 zfree(&aux->metadata[i]);
659 thread__zput(aux->unknown_thread);
660 zfree(&aux->metadata);
664 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
667 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
668 struct cs_etm_auxtrace,
671 return evsel->core.attr.type == aux->pmu_type;
674 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
676 struct machine *machine;
678 machine = etmq->etm->machine;
680 if (address >= machine__kernel_start(machine)) {
681 if (machine__is_host(machine))
682 return PERF_RECORD_MISC_KERNEL;
684 return PERF_RECORD_MISC_GUEST_KERNEL;
686 if (machine__is_host(machine))
687 return PERF_RECORD_MISC_USER;
689 return PERF_RECORD_MISC_GUEST_USER;
691 return PERF_RECORD_MISC_HYPERVISOR;
695 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
696 u64 address, size_t size, u8 *buffer)
701 struct thread *thread;
702 struct machine *machine;
703 struct addr_location al;
704 struct cs_etm_traceid_queue *tidq;
709 machine = etmq->etm->machine;
710 cpumode = cs_etm__cpu_mode(etmq, address);
711 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
715 thread = tidq->thread;
717 if (cpumode != PERF_RECORD_MISC_KERNEL)
719 thread = etmq->etm->unknown_thread;
722 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
725 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
726 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
729 offset = al.map->map_ip(al.map, address);
733 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
736 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
737 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
738 if (!al.map->dso->auxtrace_warned) {
739 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
741 al.map->dso->long_name ? al.map->dso->long_name : "Unknown");
742 al.map->dso->auxtrace_warned = true;
750 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
753 struct cs_etm_decoder_params d_params;
754 struct cs_etm_trace_params *t_params = NULL;
755 struct cs_etm_queue *etmq;
757 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
760 int decoders = formatted ? etm->num_cpu : 1;
762 etmq = zalloc(sizeof(*etmq));
766 etmq->traceid_queues_list = intlist__new(NULL);
767 if (!etmq->traceid_queues_list)
770 /* Use metadata to fill in trace parameters for trace decoder */
771 t_params = zalloc(sizeof(*t_params) * decoders);
776 if (cs_etm__init_trace_params(t_params, etm, decoders))
779 /* Set decoder parameters to decode trace packets */
780 if (cs_etm__init_decoder_params(&d_params, etmq,
781 dump_trace ? CS_ETM_OPERATION_PRINT :
782 CS_ETM_OPERATION_DECODE,
786 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
793 * Register a function to handle all memory accesses required by
794 * the trace decoder library.
796 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
799 goto out_free_decoder;
805 cs_etm_decoder__free(etmq->decoder);
807 intlist__delete(etmq->traceid_queues_list);
813 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
814 struct auxtrace_queue *queue,
815 unsigned int queue_nr,
818 struct cs_etm_queue *etmq = queue->priv;
820 if (list_empty(&queue->head) || etmq)
823 etmq = cs_etm__alloc_queue(etm, formatted);
830 etmq->queue_nr = queue_nr;
836 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
837 struct cs_etm_queue *etmq,
838 unsigned int queue_nr)
841 unsigned int cs_queue_nr;
846 * We are under a CPU-wide trace scenario. As such we need to know
847 * when the code that generated the traces started to execute so that
848 * it can be correlated with execution on other CPUs. So we get a
849 * handle on the beginning of traces and decode until we find a
850 * timestamp. The timestamp is then added to the auxtrace min heap
851 * in order to know what nibble (of all the etmqs) to decode first.
855 * Fetch an aux_buffer from this etmq. Bail if no more
856 * blocks or an error has been encountered.
858 ret = cs_etm__get_data_block(etmq);
863 * Run decoder on the trace block. The decoder will stop when
864 * encountering a CS timestamp, a full packet queue or the end of
865 * trace for that block.
867 ret = cs_etm__decode_data_block(etmq);
872 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
873 * the timestamp calculation for us.
875 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
877 /* We found a timestamp, no need to continue. */
882 * We didn't find a timestamp so empty all the traceid packet
883 * queues before looking for another timestamp packet, either
884 * in the current data block or a new one. Packets that were
885 * just decoded are useless since no timestamp has been
886 * associated with them. As such simply discard them.
888 cs_etm__clear_all_packet_queues(etmq);
892 * We have a timestamp. Add it to the min heap to reflect when
893 * instructions conveyed by the range packets of this traceID queue
894 * started to execute. Once the same has been done for all the traceID
895 * queues of each etmq, redenring and decoding can start in
896 * chronological order.
898 * Note that packets decoded above are still in the traceID's packet
899 * queue and will be processed in cs_etm__process_queues().
901 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
902 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
908 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
909 struct cs_etm_traceid_queue *tidq)
911 struct branch_stack *bs_src = tidq->last_branch_rb;
912 struct branch_stack *bs_dst = tidq->last_branch;
916 * Set the number of records before early exit: ->nr is used to
917 * determine how many branches to copy from ->entries.
919 bs_dst->nr = bs_src->nr;
922 * Early exit when there is nothing to copy.
928 * As bs_src->entries is a circular buffer, we need to copy from it in
929 * two steps. First, copy the branches from the most recently inserted
930 * branch ->last_branch_pos until the end of bs_src->entries buffer.
932 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
933 memcpy(&bs_dst->entries[0],
934 &bs_src->entries[tidq->last_branch_pos],
935 sizeof(struct branch_entry) * nr);
938 * If we wrapped around at least once, the branches from the beginning
939 * of the bs_src->entries buffer and until the ->last_branch_pos element
940 * are older valid branches: copy them over. The total number of
941 * branches copied over will be equal to the number of branches asked by
942 * the user in last_branch_sz.
944 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
945 memcpy(&bs_dst->entries[nr],
947 sizeof(struct branch_entry) * tidq->last_branch_pos);
952 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
954 tidq->last_branch_pos = 0;
955 tidq->last_branch_rb->nr = 0;
958 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
959 u8 trace_chan_id, u64 addr)
963 cs_etm__mem_access(etmq, trace_chan_id, addr,
964 ARRAY_SIZE(instrBytes), instrBytes);
966 * T32 instruction size is indicated by bits[15:11] of the first
967 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
968 * denote a 32-bit instruction.
970 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
973 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
975 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
976 if (packet->sample_type == CS_ETM_DISCONTINUITY)
979 return packet->start_addr;
983 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
985 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
986 if (packet->sample_type == CS_ETM_DISCONTINUITY)
989 return packet->end_addr - packet->last_instr_size;
992 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
994 const struct cs_etm_packet *packet,
997 if (packet->isa == CS_ETM_ISA_T32) {
998 u64 addr = packet->start_addr;
1001 addr += cs_etm__t32_instr_size(etmq,
1002 trace_chan_id, addr);
1008 /* Assume a 4 byte instruction size (A32/A64) */
1009 return packet->start_addr + offset * 4;
1012 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1013 struct cs_etm_traceid_queue *tidq)
1015 struct branch_stack *bs = tidq->last_branch_rb;
1016 struct branch_entry *be;
1019 * The branches are recorded in a circular buffer in reverse
1020 * chronological order: we start recording from the last element of the
1021 * buffer down. After writing the first element of the stack, move the
1022 * insert position back to the end of the buffer.
1024 if (!tidq->last_branch_pos)
1025 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1027 tidq->last_branch_pos -= 1;
1029 be = &bs->entries[tidq->last_branch_pos];
1030 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1031 be->to = cs_etm__first_executed_instr(tidq->packet);
1032 /* No support for mispredict */
1033 be->flags.mispred = 0;
1034 be->flags.predicted = 1;
1037 * Increment bs->nr until reaching the number of last branches asked by
1038 * the user on the command line.
1040 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1044 static int cs_etm__inject_event(union perf_event *event,
1045 struct perf_sample *sample, u64 type)
1047 event->header.size = perf_event__sample_event_size(sample, type, 0);
1048 return perf_event__synthesize_sample(event, type, 0, sample);
1053 cs_etm__get_trace(struct cs_etm_queue *etmq)
1055 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1056 struct auxtrace_buffer *old_buffer = aux_buffer;
1057 struct auxtrace_queue *queue;
1059 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1061 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1063 /* If no more data, drop the previous auxtrace_buffer and return */
1066 auxtrace_buffer__drop_data(old_buffer);
1071 etmq->buffer = aux_buffer;
1073 /* If the aux_buffer doesn't have data associated, try to load it */
1074 if (!aux_buffer->data) {
1075 /* get the file desc associated with the perf data file */
1076 int fd = perf_data__fd(etmq->etm->session->data);
1078 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1079 if (!aux_buffer->data)
1083 /* If valid, drop the previous buffer */
1085 auxtrace_buffer__drop_data(old_buffer);
1088 etmq->buf_len = aux_buffer->size;
1089 etmq->buf = aux_buffer->data;
1091 return etmq->buf_len;
1094 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1095 struct cs_etm_traceid_queue *tidq)
1097 if ((!tidq->thread) && (tidq->tid != -1))
1098 tidq->thread = machine__find_thread(etm->machine, -1,
1102 tidq->pid = tidq->thread->pid_;
1105 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1106 pid_t tid, u8 trace_chan_id)
1108 int cpu, err = -EINVAL;
1109 struct cs_etm_auxtrace *etm = etmq->etm;
1110 struct cs_etm_traceid_queue *tidq;
1112 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1116 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1119 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1124 thread__zput(tidq->thread);
1126 cs_etm__set_pid_tid_cpu(etm, tidq);
1130 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1132 return !!etmq->etm->timeless_decoding;
1135 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1137 const struct cs_etm_packet *packet,
1138 struct perf_sample *sample)
1141 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1142 * packet, so directly bail out with 'insn_len' = 0.
1144 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1145 sample->insn_len = 0;
1150 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1151 * cs_etm__t32_instr_size().
1153 if (packet->isa == CS_ETM_ISA_T32)
1154 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1156 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1158 sample->insn_len = 4;
1160 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1161 sample->insn_len, (void *)sample->insn);
1164 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1165 struct cs_etm_traceid_queue *tidq,
1166 u64 addr, u64 period)
1169 struct cs_etm_auxtrace *etm = etmq->etm;
1170 union perf_event *event = tidq->event_buf;
1171 struct perf_sample sample = {.ip = 0,};
1173 event->sample.header.type = PERF_RECORD_SAMPLE;
1174 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1175 event->sample.header.size = sizeof(struct perf_event_header);
1177 if (!etm->timeless_decoding)
1178 sample.time = etm->latest_kernel_timestamp;
1180 sample.pid = tidq->pid;
1181 sample.tid = tidq->tid;
1182 sample.id = etmq->etm->instructions_id;
1183 sample.stream_id = etmq->etm->instructions_id;
1184 sample.period = period;
1185 sample.cpu = tidq->packet->cpu;
1186 sample.flags = tidq->prev_packet->flags;
1187 sample.cpumode = event->sample.header.misc;
1189 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1191 if (etm->synth_opts.last_branch)
1192 sample.branch_stack = tidq->last_branch;
1194 if (etm->synth_opts.inject) {
1195 ret = cs_etm__inject_event(event, &sample,
1196 etm->instructions_sample_type);
1201 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1205 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1212 * The cs etm packet encodes an instruction range between a branch target
1213 * and the next taken branch. Generate sample accordingly.
1215 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1216 struct cs_etm_traceid_queue *tidq)
1219 struct cs_etm_auxtrace *etm = etmq->etm;
1220 struct perf_sample sample = {.ip = 0,};
1221 union perf_event *event = tidq->event_buf;
1222 struct dummy_branch_stack {
1225 struct branch_entry entries;
1229 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1231 event->sample.header.type = PERF_RECORD_SAMPLE;
1232 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1233 event->sample.header.size = sizeof(struct perf_event_header);
1235 if (!etm->timeless_decoding)
1236 sample.time = etm->latest_kernel_timestamp;
1238 sample.pid = tidq->pid;
1239 sample.tid = tidq->tid;
1240 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1241 sample.id = etmq->etm->branches_id;
1242 sample.stream_id = etmq->etm->branches_id;
1244 sample.cpu = tidq->packet->cpu;
1245 sample.flags = tidq->prev_packet->flags;
1246 sample.cpumode = event->sample.header.misc;
1248 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1252 * perf report cannot handle events without a branch stack
1254 if (etm->synth_opts.last_branch) {
1255 dummy_bs = (struct dummy_branch_stack){
1263 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1266 if (etm->synth_opts.inject) {
1267 ret = cs_etm__inject_event(event, &sample,
1268 etm->branches_sample_type);
1273 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1277 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1283 struct cs_etm_synth {
1284 struct perf_tool dummy_tool;
1285 struct perf_session *session;
1288 static int cs_etm__event_synth(struct perf_tool *tool,
1289 union perf_event *event,
1290 struct perf_sample *sample __maybe_unused,
1291 struct machine *machine __maybe_unused)
1293 struct cs_etm_synth *cs_etm_synth =
1294 container_of(tool, struct cs_etm_synth, dummy_tool);
1296 return perf_session__deliver_synth_event(cs_etm_synth->session,
1300 static int cs_etm__synth_event(struct perf_session *session,
1301 struct perf_event_attr *attr, u64 id)
1303 struct cs_etm_synth cs_etm_synth;
1305 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1306 cs_etm_synth.session = session;
1308 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1309 &id, cs_etm__event_synth);
1312 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1313 struct perf_session *session)
1315 struct evlist *evlist = session->evlist;
1316 struct evsel *evsel;
1317 struct perf_event_attr attr;
1322 evlist__for_each_entry(evlist, evsel) {
1323 if (evsel->core.attr.type == etm->pmu_type) {
1330 pr_debug("No selected events with CoreSight Trace data\n");
1334 memset(&attr, 0, sizeof(struct perf_event_attr));
1335 attr.size = sizeof(struct perf_event_attr);
1336 attr.type = PERF_TYPE_HARDWARE;
1337 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1338 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1340 if (etm->timeless_decoding)
1341 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1343 attr.sample_type |= PERF_SAMPLE_TIME;
1345 attr.exclude_user = evsel->core.attr.exclude_user;
1346 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1347 attr.exclude_hv = evsel->core.attr.exclude_hv;
1348 attr.exclude_host = evsel->core.attr.exclude_host;
1349 attr.exclude_guest = evsel->core.attr.exclude_guest;
1350 attr.sample_id_all = evsel->core.attr.sample_id_all;
1351 attr.read_format = evsel->core.attr.read_format;
1353 /* create new id val to be a fixed offset from evsel id */
1354 id = evsel->core.id[0] + 1000000000;
1359 if (etm->synth_opts.branches) {
1360 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1361 attr.sample_period = 1;
1362 attr.sample_type |= PERF_SAMPLE_ADDR;
1363 err = cs_etm__synth_event(session, &attr, id);
1366 etm->branches_sample_type = attr.sample_type;
1367 etm->branches_id = id;
1369 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1372 if (etm->synth_opts.last_branch) {
1373 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1375 * We don't use the hardware index, but the sample generation
1376 * code uses the new format branch_stack with this field,
1377 * so the event attributes must indicate that it's present.
1379 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1382 if (etm->synth_opts.instructions) {
1383 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1384 attr.sample_period = etm->synth_opts.period;
1385 etm->instructions_sample_period = attr.sample_period;
1386 err = cs_etm__synth_event(session, &attr, id);
1389 etm->instructions_sample_type = attr.sample_type;
1390 etm->instructions_id = id;
1397 static int cs_etm__sample(struct cs_etm_queue *etmq,
1398 struct cs_etm_traceid_queue *tidq)
1400 struct cs_etm_auxtrace *etm = etmq->etm;
1402 u8 trace_chan_id = tidq->trace_chan_id;
1405 /* Get instructions remainder from previous packet */
1406 instrs_prev = tidq->period_instructions;
1408 tidq->period_instructions += tidq->packet->instr_count;
1411 * Record a branch when the last instruction in
1412 * PREV_PACKET is a branch.
1414 if (etm->synth_opts.last_branch &&
1415 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1416 tidq->prev_packet->last_instr_taken_branch)
1417 cs_etm__update_last_branch_rb(etmq, tidq);
1419 if (etm->synth_opts.instructions &&
1420 tidq->period_instructions >= etm->instructions_sample_period) {
1422 * Emit instruction sample periodically
1423 * TODO: allow period to be defined in cycles and clock time
1427 * Below diagram demonstrates the instruction samples
1430 * Instrs Instrs Instrs Instrs
1431 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1434 * --------------------------------------------------
1438 * instructions(Pi) instructions(Pi')
1441 * \---------------- -----------------/
1443 * tidq->packet->instr_count
1445 * Instrs Sample(n...) are the synthesised samples occurring
1446 * every etm->instructions_sample_period instructions - as
1447 * defined on the perf command line. Sample(n) is being the
1448 * last sample before the current etm packet, n+1 to n+3
1449 * samples are generated from the current etm packet.
1451 * tidq->packet->instr_count represents the number of
1452 * instructions in the current etm packet.
1454 * Period instructions (Pi) contains the the number of
1455 * instructions executed after the sample point(n) from the
1456 * previous etm packet. This will always be less than
1457 * etm->instructions_sample_period.
1459 * When generate new samples, it combines with two parts
1460 * instructions, one is the tail of the old packet and another
1461 * is the head of the new coming packet, to generate
1462 * sample(n+1); sample(n+2) and sample(n+3) consume the
1463 * instructions with sample period. After sample(n+3), the rest
1464 * instructions will be used by later packet and it is assigned
1465 * to tidq->period_instructions for next round calculation.
1469 * Get the initial offset into the current packet instructions;
1470 * entry conditions ensure that instrs_prev is less than
1471 * etm->instructions_sample_period.
1473 u64 offset = etm->instructions_sample_period - instrs_prev;
1476 /* Prepare last branches for instruction sample */
1477 if (etm->synth_opts.last_branch)
1478 cs_etm__copy_last_branch_rb(etmq, tidq);
1480 while (tidq->period_instructions >=
1481 etm->instructions_sample_period) {
1483 * Calculate the address of the sampled instruction (-1
1484 * as sample is reported as though instruction has just
1485 * been executed, but PC has not advanced to next
1488 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1489 tidq->packet, offset - 1);
1490 ret = cs_etm__synth_instruction_sample(
1492 etm->instructions_sample_period);
1496 offset += etm->instructions_sample_period;
1497 tidq->period_instructions -=
1498 etm->instructions_sample_period;
1502 if (etm->synth_opts.branches) {
1503 bool generate_sample = false;
1505 /* Generate sample for tracing on packet */
1506 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1507 generate_sample = true;
1509 /* Generate sample for branch taken packet */
1510 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1511 tidq->prev_packet->last_instr_taken_branch)
1512 generate_sample = true;
1514 if (generate_sample) {
1515 ret = cs_etm__synth_branch_sample(etmq, tidq);
1521 cs_etm__packet_swap(etm, tidq);
1526 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1529 * When the exception packet is inserted, whether the last instruction
1530 * in previous range packet is taken branch or not, we need to force
1531 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1532 * to generate branch sample for the instruction range before the
1533 * exception is trapped to kernel or before the exception returning.
1535 * The exception packet includes the dummy address values, so don't
1536 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1537 * for generating instruction and branch samples.
1539 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1540 tidq->prev_packet->last_instr_taken_branch = true;
1545 static int cs_etm__flush(struct cs_etm_queue *etmq,
1546 struct cs_etm_traceid_queue *tidq)
1549 struct cs_etm_auxtrace *etm = etmq->etm;
1551 /* Handle start tracing packet */
1552 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1555 if (etmq->etm->synth_opts.last_branch &&
1556 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1559 /* Prepare last branches for instruction sample */
1560 cs_etm__copy_last_branch_rb(etmq, tidq);
1563 * Generate a last branch event for the branches left in the
1564 * circular buffer at the end of the trace.
1566 * Use the address of the end of the last reported execution
1569 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1571 err = cs_etm__synth_instruction_sample(
1573 tidq->period_instructions);
1577 tidq->period_instructions = 0;
1581 if (etm->synth_opts.branches &&
1582 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1583 err = cs_etm__synth_branch_sample(etmq, tidq);
1589 cs_etm__packet_swap(etm, tidq);
1591 /* Reset last branches after flush the trace */
1592 if (etm->synth_opts.last_branch)
1593 cs_etm__reset_last_branch_rb(tidq);
1598 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1599 struct cs_etm_traceid_queue *tidq)
1604 * It has no new packet coming and 'etmq->packet' contains the stale
1605 * packet which was set at the previous time with packets swapping;
1606 * so skip to generate branch sample to avoid stale packet.
1608 * For this case only flush branch stack and generate a last branch
1609 * event for the branches left in the circular buffer at the end of
1612 if (etmq->etm->synth_opts.last_branch &&
1613 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1616 /* Prepare last branches for instruction sample */
1617 cs_etm__copy_last_branch_rb(etmq, tidq);
1620 * Use the address of the end of the last reported execution
1623 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1625 err = cs_etm__synth_instruction_sample(
1627 tidq->period_instructions);
1631 tidq->period_instructions = 0;
1637 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1639 * Returns: < 0 if error
1640 * = 0 if no more auxtrace_buffer to read
1641 * > 0 if the current buffer isn't empty yet
1643 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1647 if (!etmq->buf_len) {
1648 ret = cs_etm__get_trace(etmq);
1652 * We cannot assume consecutive blocks in the data file
1653 * are contiguous, reset the decoder to force re-sync.
1655 ret = cs_etm_decoder__reset(etmq->decoder);
1660 return etmq->buf_len;
1663 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1664 struct cs_etm_packet *packet,
1667 /* Initialise to keep compiler happy */
1672 switch (packet->isa) {
1673 case CS_ETM_ISA_T32:
1675 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1678 * +-----------------+--------+
1679 * | 1 1 0 1 1 1 1 1 | imm8 |
1680 * +-----------------+--------+
1682 * According to the specification, it only defines SVC for T32
1683 * with 16 bits instruction and has no definition for 32bits;
1684 * so below only read 2 bytes as instruction size for T32.
1686 addr = end_addr - 2;
1687 cs_etm__mem_access(etmq, trace_chan_id, addr,
1688 sizeof(instr16), (u8 *)&instr16);
1689 if ((instr16 & 0xFF00) == 0xDF00)
1693 case CS_ETM_ISA_A32:
1695 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1697 * b'31 b'28 b'27 b'24
1698 * +---------+---------+-------------------------+
1699 * | !1111 | 1 1 1 1 | imm24 |
1700 * +---------+---------+-------------------------+
1702 addr = end_addr - 4;
1703 cs_etm__mem_access(etmq, trace_chan_id, addr,
1704 sizeof(instr32), (u8 *)&instr32);
1705 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1706 (instr32 & 0xF0000000) != 0xF0000000)
1710 case CS_ETM_ISA_A64:
1712 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1715 * +-----------------------+---------+-----------+
1716 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1717 * +-----------------------+---------+-----------+
1719 addr = end_addr - 4;
1720 cs_etm__mem_access(etmq, trace_chan_id, addr,
1721 sizeof(instr32), (u8 *)&instr32);
1722 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1726 case CS_ETM_ISA_UNKNOWN:
1734 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1735 struct cs_etm_traceid_queue *tidq, u64 magic)
1737 u8 trace_chan_id = tidq->trace_chan_id;
1738 struct cs_etm_packet *packet = tidq->packet;
1739 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1741 if (magic == __perf_cs_etmv3_magic)
1742 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1746 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1747 * HVC cases; need to check if it's SVC instruction based on
1750 if (magic == __perf_cs_etmv4_magic) {
1751 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1752 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1753 prev_packet->end_addr))
1760 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1763 struct cs_etm_packet *packet = tidq->packet;
1765 if (magic == __perf_cs_etmv3_magic)
1766 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1767 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1768 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1769 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1770 packet->exception_number == CS_ETMV3_EXC_FIQ)
1773 if (magic == __perf_cs_etmv4_magic)
1774 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1775 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1776 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1777 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1778 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1779 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1780 packet->exception_number == CS_ETMV4_EXC_FIQ)
1786 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1787 struct cs_etm_traceid_queue *tidq,
1790 u8 trace_chan_id = tidq->trace_chan_id;
1791 struct cs_etm_packet *packet = tidq->packet;
1792 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1794 if (magic == __perf_cs_etmv3_magic)
1795 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1796 packet->exception_number == CS_ETMV3_EXC_HYP ||
1797 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1798 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1799 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1800 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1801 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1804 if (magic == __perf_cs_etmv4_magic) {
1805 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1806 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1807 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1808 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1812 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1813 * (SMC, HVC) are taken as sync exceptions.
1815 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1816 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1817 prev_packet->end_addr))
1821 * ETMv4 has 5 bits for exception number; if the numbers
1822 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1823 * they are implementation defined exceptions.
1825 * For this case, simply take it as sync exception.
1827 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1828 packet->exception_number <= CS_ETMV4_EXC_END)
1835 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1836 struct cs_etm_traceid_queue *tidq)
1838 struct cs_etm_packet *packet = tidq->packet;
1839 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1840 u8 trace_chan_id = tidq->trace_chan_id;
1844 switch (packet->sample_type) {
1847 * Immediate branch instruction without neither link nor
1848 * return flag, it's normal branch instruction within
1851 if (packet->last_instr_type == OCSD_INSTR_BR &&
1852 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1853 packet->flags = PERF_IP_FLAG_BRANCH;
1855 if (packet->last_instr_cond)
1856 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1860 * Immediate branch instruction with link (e.g. BL), this is
1861 * branch instruction for function call.
1863 if (packet->last_instr_type == OCSD_INSTR_BR &&
1864 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1865 packet->flags = PERF_IP_FLAG_BRANCH |
1869 * Indirect branch instruction with link (e.g. BLR), this is
1870 * branch instruction for function call.
1872 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1873 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1874 packet->flags = PERF_IP_FLAG_BRANCH |
1878 * Indirect branch instruction with subtype of
1879 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1880 * function return for A32/T32.
1882 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1883 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1884 packet->flags = PERF_IP_FLAG_BRANCH |
1885 PERF_IP_FLAG_RETURN;
1888 * Indirect branch instruction without link (e.g. BR), usually
1889 * this is used for function return, especially for functions
1890 * within dynamic link lib.
1892 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1893 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1894 packet->flags = PERF_IP_FLAG_BRANCH |
1895 PERF_IP_FLAG_RETURN;
1897 /* Return instruction for function return. */
1898 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1899 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1900 packet->flags = PERF_IP_FLAG_BRANCH |
1901 PERF_IP_FLAG_RETURN;
1904 * Decoder might insert a discontinuity in the middle of
1905 * instruction packets, fixup prev_packet with flag
1906 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1908 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1909 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1910 PERF_IP_FLAG_TRACE_BEGIN;
1913 * If the previous packet is an exception return packet
1914 * and the return address just follows SVC instruction,
1915 * it needs to calibrate the previous packet sample flags
1916 * as PERF_IP_FLAG_SYSCALLRET.
1918 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1919 PERF_IP_FLAG_RETURN |
1920 PERF_IP_FLAG_INTERRUPT) &&
1921 cs_etm__is_svc_instr(etmq, trace_chan_id,
1922 packet, packet->start_addr))
1923 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1924 PERF_IP_FLAG_RETURN |
1925 PERF_IP_FLAG_SYSCALLRET;
1927 case CS_ETM_DISCONTINUITY:
1929 * The trace is discontinuous, if the previous packet is
1930 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1931 * for previous packet.
1933 if (prev_packet->sample_type == CS_ETM_RANGE)
1934 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1935 PERF_IP_FLAG_TRACE_END;
1937 case CS_ETM_EXCEPTION:
1938 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1942 /* The exception is for system call. */
1943 if (cs_etm__is_syscall(etmq, tidq, magic))
1944 packet->flags = PERF_IP_FLAG_BRANCH |
1946 PERF_IP_FLAG_SYSCALLRET;
1948 * The exceptions are triggered by external signals from bus,
1949 * interrupt controller, debug module, PE reset or halt.
1951 else if (cs_etm__is_async_exception(tidq, magic))
1952 packet->flags = PERF_IP_FLAG_BRANCH |
1954 PERF_IP_FLAG_ASYNC |
1955 PERF_IP_FLAG_INTERRUPT;
1957 * Otherwise, exception is caused by trap, instruction &
1958 * data fault, or alignment errors.
1960 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1961 packet->flags = PERF_IP_FLAG_BRANCH |
1963 PERF_IP_FLAG_INTERRUPT;
1966 * When the exception packet is inserted, since exception
1967 * packet is not used standalone for generating samples
1968 * and it's affiliation to the previous instruction range
1969 * packet; so set previous range packet flags to tell perf
1970 * it is an exception taken branch.
1972 if (prev_packet->sample_type == CS_ETM_RANGE)
1973 prev_packet->flags = packet->flags;
1975 case CS_ETM_EXCEPTION_RET:
1977 * When the exception return packet is inserted, since
1978 * exception return packet is not used standalone for
1979 * generating samples and it's affiliation to the previous
1980 * instruction range packet; so set previous range packet
1981 * flags to tell perf it is an exception return branch.
1983 * The exception return can be for either system call or
1984 * other exception types; unfortunately the packet doesn't
1985 * contain exception type related info so we cannot decide
1986 * the exception type purely based on exception return packet.
1987 * If we record the exception number from exception packet and
1988 * reuse it for exception return packet, this is not reliable
1989 * due the trace can be discontinuity or the interrupt can
1990 * be nested, thus the recorded exception number cannot be
1991 * used for exception return packet for these two cases.
1993 * For exception return packet, we only need to distinguish the
1994 * packet is for system call or for other types. Thus the
1995 * decision can be deferred when receive the next packet which
1996 * contains the return address, based on the return address we
1997 * can read out the previous instruction and check if it's a
1998 * system call instruction and then calibrate the sample flag
2001 if (prev_packet->sample_type == CS_ETM_RANGE)
2002 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2003 PERF_IP_FLAG_RETURN |
2004 PERF_IP_FLAG_INTERRUPT;
2014 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2017 size_t processed = 0;
2020 * Packets are decoded and added to the decoder's packet queue
2021 * until the decoder packet processing callback has requested that
2022 * processing stops or there is nothing left in the buffer. Normal
2023 * operations that stop processing are a timestamp packet or a full
2024 * decoder buffer queue.
2026 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2028 &etmq->buf[etmq->buf_used],
2034 etmq->offset += processed;
2035 etmq->buf_used += processed;
2036 etmq->buf_len -= processed;
2042 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2043 struct cs_etm_traceid_queue *tidq)
2046 struct cs_etm_packet_queue *packet_queue;
2048 packet_queue = &tidq->packet_queue;
2050 /* Process each packet in this chunk */
2052 ret = cs_etm_decoder__get_packet(packet_queue,
2056 * Stop processing this chunk on
2057 * end of data or error
2062 * Since packet addresses are swapped in packet
2063 * handling within below switch() statements,
2064 * thus setting sample flags must be called
2065 * prior to switch() statement to use address
2066 * information before packets swapping.
2068 ret = cs_etm__set_sample_flags(etmq, tidq);
2072 switch (tidq->packet->sample_type) {
2075 * If the packet contains an instruction
2076 * range, generate instruction sequence
2079 cs_etm__sample(etmq, tidq);
2081 case CS_ETM_EXCEPTION:
2082 case CS_ETM_EXCEPTION_RET:
2084 * If the exception packet is coming,
2085 * make sure the previous instruction
2086 * range packet to be handled properly.
2088 cs_etm__exception(tidq);
2090 case CS_ETM_DISCONTINUITY:
2092 * Discontinuity in trace, flush
2093 * previous branch stack
2095 cs_etm__flush(etmq, tidq);
2099 * Should not receive empty packet,
2102 pr_err("CS ETM Trace: empty packet\n");
2112 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2115 struct int_node *inode;
2116 struct cs_etm_traceid_queue *tidq;
2117 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2119 intlist__for_each_entry(inode, traceid_queues_list) {
2120 idx = (int)(intptr_t)inode->priv;
2121 tidq = etmq->traceid_queues[idx];
2123 /* Ignore return value */
2124 cs_etm__process_traceid_queue(etmq, tidq);
2127 * Generate an instruction sample with the remaining
2128 * branchstack entries.
2130 cs_etm__flush(etmq, tidq);
2134 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2137 struct cs_etm_traceid_queue *tidq;
2139 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2143 /* Go through each buffer in the queue and decode them one by one */
2145 err = cs_etm__get_data_block(etmq);
2149 /* Run trace decoder until buffer consumed or end of trace */
2151 err = cs_etm__decode_data_block(etmq);
2156 * Process each packet in this chunk, nothing to do if
2157 * an error occurs other than hoping the next one will
2160 err = cs_etm__process_traceid_queue(etmq, tidq);
2162 } while (etmq->buf_len);
2165 /* Flush any remaining branch stack entries */
2166 err = cs_etm__end_block(etmq, tidq);
2172 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2176 struct auxtrace_queues *queues = &etm->queues;
2178 for (i = 0; i < queues->nr_queues; i++) {
2179 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2180 struct cs_etm_queue *etmq = queue->priv;
2181 struct cs_etm_traceid_queue *tidq;
2186 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2187 CS_ETM_PER_THREAD_TRACEID);
2192 if ((tid == -1) || (tidq->tid == tid)) {
2193 cs_etm__set_pid_tid_cpu(etm, tidq);
2194 cs_etm__run_decoder(etmq);
2201 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2204 unsigned int cs_queue_nr, queue_nr, i;
2207 struct auxtrace_queue *queue;
2208 struct cs_etm_queue *etmq;
2209 struct cs_etm_traceid_queue *tidq;
2212 * Pre-populate the heap with one entry from each queue so that we can
2213 * start processing in time order across all queues.
2215 for (i = 0; i < etm->queues.nr_queues; i++) {
2216 etmq = etm->queues.queue_array[i].priv;
2220 ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2226 if (!etm->heap.heap_cnt)
2229 /* Take the entry at the top of the min heap */
2230 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2231 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2232 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2233 queue = &etm->queues.queue_array[queue_nr];
2237 * Remove the top entry from the heap since we are about
2240 auxtrace_heap__pop(&etm->heap);
2242 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2245 * No traceID queue has been allocated for this traceID,
2246 * which means something somewhere went very wrong. No
2247 * other choice than simply exit.
2254 * Packets associated with this timestamp are already in
2255 * the etmq's traceID queue, so process them.
2257 ret = cs_etm__process_traceid_queue(etmq, tidq);
2262 * Packets for this timestamp have been processed, time to
2263 * move on to the next timestamp, fetching a new auxtrace_buffer
2267 ret = cs_etm__get_data_block(etmq);
2272 * No more auxtrace_buffers to process in this etmq, simply
2273 * move on to another entry in the auxtrace_heap.
2278 ret = cs_etm__decode_data_block(etmq);
2282 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2284 if (!cs_timestamp) {
2286 * Function cs_etm__decode_data_block() returns when
2287 * there is no more traces to decode in the current
2288 * auxtrace_buffer OR when a timestamp has been
2289 * encountered on any of the traceID queues. Since we
2290 * did not get a timestamp, there is no more traces to
2291 * process in this auxtrace_buffer. As such empty and
2292 * flush all traceID queues.
2294 cs_etm__clear_all_traceid_queues(etmq);
2296 /* Fetch another auxtrace_buffer for this etmq */
2301 * Add to the min heap the timestamp for packets that have
2302 * just been decoded. They will be processed and synthesized
2303 * during the next call to cs_etm__process_traceid_queue() for
2304 * this queue/traceID.
2306 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2307 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2314 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2315 union perf_event *event)
2319 if (etm->timeless_decoding)
2323 * Add the tid/pid to the log so that we can get a match when
2324 * we get a contextID from the decoder.
2326 th = machine__findnew_thread(etm->machine,
2327 event->itrace_start.pid,
2328 event->itrace_start.tid);
2337 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2338 union perf_event *event)
2341 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2344 * Context switch in per-thread mode are irrelevant since perf
2345 * will start/stop tracing as the process is scheduled.
2347 if (etm->timeless_decoding)
2351 * SWITCH_IN events carry the next process to be switched out while
2352 * SWITCH_OUT events carry the process to be switched in. As such
2353 * we don't care about IN events.
2359 * Add the tid/pid to the log so that we can get a match when
2360 * we get a contextID from the decoder.
2362 th = machine__findnew_thread(etm->machine,
2363 event->context_switch.next_prev_pid,
2364 event->context_switch.next_prev_tid);
2373 static int cs_etm__process_event(struct perf_session *session,
2374 union perf_event *event,
2375 struct perf_sample *sample,
2376 struct perf_tool *tool)
2378 u64 sample_kernel_timestamp;
2379 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2380 struct cs_etm_auxtrace,
2386 if (!tool->ordered_events) {
2387 pr_err("CoreSight ETM Trace requires ordered events\n");
2391 if (sample->time && (sample->time != (u64) -1))
2392 sample_kernel_timestamp = sample->time;
2394 sample_kernel_timestamp = 0;
2397 * Don't wait for cs_etm__flush_events() in per-thread/timeless mode to start the decode. We
2398 * need the tid of the PERF_RECORD_EXIT event to assign to the synthesised samples because
2399 * ETM_OPT_CTXTID is not enabled.
2401 if (etm->timeless_decoding &&
2402 event->header.type == PERF_RECORD_EXIT)
2403 return cs_etm__process_timeless_queues(etm,
2406 if (event->header.type == PERF_RECORD_ITRACE_START)
2407 return cs_etm__process_itrace_start(etm, event);
2408 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2409 return cs_etm__process_switch_cpu_wide(etm, event);
2411 if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) {
2413 * Record the latest kernel timestamp available in the header
2414 * for samples so that synthesised samples occur from this point
2417 etm->latest_kernel_timestamp = sample_kernel_timestamp;
2423 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2424 struct perf_record_auxtrace *event)
2426 struct auxtrace_buffer *buf;
2429 * Find all buffers with same reference in the queues and dump them.
2430 * This is because the queues can contain multiple entries of the same
2431 * buffer that were split on aux records.
2433 for (i = 0; i < etm->queues.nr_queues; ++i)
2434 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2435 if (buf->reference == event->reference)
2436 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2439 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2440 union perf_event *event,
2441 struct perf_tool *tool __maybe_unused)
2443 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2444 struct cs_etm_auxtrace,
2446 if (!etm->data_queued) {
2447 struct auxtrace_buffer *buffer;
2449 int fd = perf_data__fd(session->data);
2450 bool is_pipe = perf_data__is_pipe(session->data);
2452 int idx = event->auxtrace.idx;
2457 data_offset = lseek(fd, 0, SEEK_CUR);
2458 if (data_offset == -1)
2462 err = auxtrace_queues__add_event(&etm->queues, session,
2463 event, data_offset, &buffer);
2468 * Knowing if the trace is formatted or not requires a lookup of
2469 * the aux record so only works in non-piped mode where data is
2470 * queued in cs_etm__queue_aux_records(). Always assume
2471 * formatted in piped mode (true).
2473 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2479 if (auxtrace_buffer__get_data(buffer, fd)) {
2480 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2481 auxtrace_buffer__put_data(buffer);
2483 } else if (dump_trace)
2484 dump_queued_data(etm, &event->auxtrace);
2489 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2491 struct evsel *evsel;
2492 struct evlist *evlist = etm->session->evlist;
2493 bool timeless_decoding = true;
2495 /* Override timeless mode with user input from --itrace=Z */
2496 if (etm->synth_opts.timeless_decoding)
2500 * Circle through the list of event and complain if we find one
2501 * with the time bit set.
2503 evlist__for_each_entry(evlist, evsel) {
2504 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2505 timeless_decoding = false;
2508 return timeless_decoding;
2511 static const char * const cs_etm_global_header_fmts[] = {
2512 [CS_HEADER_VERSION] = " Header version %llx\n",
2513 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2514 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2517 static const char * const cs_etm_priv_fmts[] = {
2518 [CS_ETM_MAGIC] = " Magic number %llx\n",
2519 [CS_ETM_CPU] = " CPU %lld\n",
2520 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2521 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2522 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2523 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2524 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2527 static const char * const cs_etmv4_priv_fmts[] = {
2528 [CS_ETM_MAGIC] = " Magic number %llx\n",
2529 [CS_ETM_CPU] = " CPU %lld\n",
2530 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2531 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2532 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2533 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2534 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2535 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2536 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2537 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2538 [CS_ETE_TRCDEVARCH] = " TRCDEVARCH %llx\n"
2541 static const char * const param_unk_fmt =
2542 " Unknown parameter [%d] %llx\n";
2543 static const char * const magic_unk_fmt =
2544 " Magic number Unknown %llx\n";
2546 static int cs_etm__print_cpu_metadata_v0(__u64 *val, int *offset)
2548 int i = *offset, j, nr_params = 0, fmt_offset;
2551 /* check magic value */
2552 magic = val[i + CS_ETM_MAGIC];
2553 if ((magic != __perf_cs_etmv3_magic) &&
2554 (magic != __perf_cs_etmv4_magic)) {
2555 /* failure - note bad magic value */
2556 fprintf(stdout, magic_unk_fmt, magic);
2560 /* print common header block */
2561 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_MAGIC], val[i++]);
2562 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_CPU], val[i++]);
2564 if (magic == __perf_cs_etmv3_magic) {
2565 nr_params = CS_ETM_NR_TRC_PARAMS_V0;
2566 fmt_offset = CS_ETM_ETMCR;
2567 /* after common block, offset format index past NR_PARAMS */
2568 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2569 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2570 } else if (magic == __perf_cs_etmv4_magic) {
2571 nr_params = CS_ETMV4_NR_TRC_PARAMS_V0;
2572 fmt_offset = CS_ETMV4_TRCCONFIGR;
2573 /* after common block, offset format index past NR_PARAMS */
2574 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2575 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2581 static int cs_etm__print_cpu_metadata_v1(__u64 *val, int *offset)
2583 int i = *offset, j, total_params = 0;
2586 magic = val[i + CS_ETM_MAGIC];
2587 /* total params to print is NR_PARAMS + common block size for v1 */
2588 total_params = val[i + CS_ETM_NR_TRC_PARAMS] + CS_ETM_COMMON_BLK_MAX_V1;
2590 if (magic == __perf_cs_etmv3_magic) {
2591 for (j = 0; j < total_params; j++, i++) {
2592 /* if newer record - could be excess params */
2593 if (j >= CS_ETM_PRIV_MAX)
2594 fprintf(stdout, param_unk_fmt, j, val[i]);
2596 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2598 } else if (magic == __perf_cs_etmv4_magic || magic == __perf_cs_ete_magic) {
2600 * ETE and ETMv4 can be printed in the same block because the number of parameters
2601 * is saved and they share the list of parameter names. ETE is also only supported
2604 for (j = 0; j < total_params; j++, i++) {
2605 /* if newer record - could be excess params */
2606 if (j >= CS_ETE_PRIV_MAX)
2607 fprintf(stdout, param_unk_fmt, j, val[i]);
2609 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2612 /* failure - note bad magic value and error out */
2613 fprintf(stdout, magic_unk_fmt, magic);
2620 static void cs_etm__print_auxtrace_info(__u64 *val, int num)
2622 int i, cpu = 0, version, err;
2624 /* bail out early on bad header version */
2626 if (version > CS_HEADER_CURRENT_VERSION) {
2627 /* failure.. return */
2628 fprintf(stdout, " Unknown Header Version = %x, ", version);
2629 fprintf(stdout, "Version supported <= %x\n", CS_HEADER_CURRENT_VERSION);
2633 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2634 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2636 for (i = CS_HEADER_VERSION_MAX; cpu < num; cpu++) {
2638 err = cs_etm__print_cpu_metadata_v0(val, &i);
2639 else if (version == 1)
2640 err = cs_etm__print_cpu_metadata_v1(val, &i);
2647 * Read a single cpu parameter block from the auxtrace_info priv block.
2649 * For version 1 there is a per cpu nr_params entry. If we are handling
2650 * version 1 file, then there may be less, the same, or more params
2651 * indicated by this value than the compile time number we understand.
2653 * For a version 0 info block, there are a fixed number, and we need to
2654 * fill out the nr_param value in the metadata we create.
2656 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2657 int out_blk_size, int nr_params_v0)
2659 u64 *metadata = NULL;
2661 int nr_in_params, nr_out_params, nr_cmn_params;
2664 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2668 /* read block current index & version */
2669 i = *buff_in_offset;
2670 hdr_version = buff_in[CS_HEADER_VERSION];
2673 /* read version 0 info block into a version 1 metadata block */
2674 nr_in_params = nr_params_v0;
2675 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2676 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2677 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2678 /* remaining block params at offset +1 from source */
2679 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2680 metadata[k + 1] = buff_in[i + k];
2681 /* version 0 has 2 common params */
2684 /* read version 1 info block - input and output nr_params may differ */
2685 /* version 1 has 3 common params */
2687 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2689 /* if input has more params than output - skip excess */
2690 nr_out_params = nr_in_params + nr_cmn_params;
2691 if (nr_out_params > out_blk_size)
2692 nr_out_params = out_blk_size;
2694 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2695 metadata[k] = buff_in[i + k];
2697 /* record the actual nr params we copied */
2698 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2701 /* adjust in offset by number of in params used */
2702 i += nr_in_params + nr_cmn_params;
2703 *buff_in_offset = i;
2708 * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2709 * on the bounds of aux_event, if it matches with the buffer that's at
2712 * Normally, whole auxtrace buffers would be added to the queue. But we
2713 * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2714 * is reset across each buffer, so splitting the buffers up in advance has
2717 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2718 struct perf_record_aux *aux_event, struct perf_sample *sample)
2721 char buf[PERF_SAMPLE_MAX_SIZE];
2722 union perf_event *auxtrace_event_union;
2723 struct perf_record_auxtrace *auxtrace_event;
2724 union perf_event auxtrace_fragment;
2725 __u64 aux_offset, aux_size;
2729 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2730 struct cs_etm_auxtrace,
2734 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2735 * from looping through the auxtrace index.
2737 err = perf_session__peek_event(session, file_offset, buf,
2738 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2741 auxtrace_event = &auxtrace_event_union->auxtrace;
2742 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2745 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2746 auxtrace_event->header.size != sz) {
2751 * In per-thread mode, CPU is set to -1, but TID will be set instead. See
2752 * auxtrace_mmap_params__set_idx(). Return 'not found' if neither CPU nor TID match.
2754 if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) ||
2755 auxtrace_event->cpu != sample->cpu)
2758 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2760 * Clamp size in snapshot mode. The buffer size is clamped in
2761 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
2764 aux_size = min(aux_event->aux_size, auxtrace_event->size);
2767 * In this mode, the head also points to the end of the buffer so aux_offset
2768 * needs to have the size subtracted so it points to the beginning as in normal mode
2770 aux_offset = aux_event->aux_offset - aux_size;
2772 aux_size = aux_event->aux_size;
2773 aux_offset = aux_event->aux_offset;
2776 if (aux_offset >= auxtrace_event->offset &&
2777 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
2779 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
2780 * based on the sizes of the aux event, and queue that fragment.
2782 auxtrace_fragment.auxtrace = *auxtrace_event;
2783 auxtrace_fragment.auxtrace.size = aux_size;
2784 auxtrace_fragment.auxtrace.offset = aux_offset;
2785 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
2787 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
2788 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
2789 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
2794 idx = auxtrace_event->idx;
2795 formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
2796 return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2800 /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
2804 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
2805 u64 offset __maybe_unused, void *data __maybe_unused)
2807 struct perf_sample sample;
2809 struct auxtrace_index_entry *ent;
2810 struct auxtrace_index *auxtrace_index;
2811 struct evsel *evsel;
2814 /* Don't care about any other events, we're only queuing buffers for AUX events */
2815 if (event->header.type != PERF_RECORD_AUX)
2818 if (event->header.size < sizeof(struct perf_record_aux))
2821 /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
2822 if (!event->aux.aux_size)
2826 * Parse the sample, we need the sample_id_all data that comes after the event so that the
2827 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
2829 evsel = evlist__event2evsel(session->evlist, event);
2832 ret = evsel__parse_sample(evsel, event, &sample);
2837 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
2839 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
2840 for (i = 0; i < auxtrace_index->nr; i++) {
2841 ent = &auxtrace_index->entries[i];
2842 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
2843 ent->sz, &event->aux, &sample);
2845 * Stop search on error or successful values. Continue search on
2854 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
2855 * don't exit with an error because it will still be possible to decode other aux records.
2857 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
2858 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
2862 static int cs_etm__queue_aux_records(struct perf_session *session)
2864 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
2865 struct auxtrace_index, list);
2866 if (index && index->nr > 0)
2867 return perf_session__peek_events(session, session->header.data_offset,
2868 session->header.data_size,
2869 cs_etm__queue_aux_records_cb, NULL);
2872 * We would get here if there are no entries in the index (either no auxtrace
2873 * buffers or no index at all). Fail silently as there is the possibility of
2874 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
2877 * In that scenario, buffers will not be split by AUX records.
2882 int cs_etm__process_auxtrace_info(union perf_event *event,
2883 struct perf_session *session)
2885 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
2886 struct cs_etm_auxtrace *etm = NULL;
2887 struct int_node *inode;
2888 unsigned int pmu_type;
2889 int event_header_size = sizeof(struct perf_event_header);
2890 int info_header_size;
2891 int total_size = auxtrace_info->header.size;
2893 int num_cpu, trcidr_idx;
2896 u64 *ptr, *hdr = NULL;
2897 u64 **metadata = NULL;
2901 * sizeof(auxtrace_info_event::type) +
2902 * sizeof(auxtrace_info_event::reserved) == 8
2904 info_header_size = 8;
2906 if (total_size < (event_header_size + info_header_size))
2909 priv_size = total_size - event_header_size - info_header_size;
2911 /* First the global part */
2912 ptr = (u64 *) auxtrace_info->priv;
2914 /* Look for version of the header */
2915 hdr_version = ptr[0];
2916 if (hdr_version > CS_HEADER_CURRENT_VERSION) {
2917 /* print routine will print an error on bad version */
2919 cs_etm__print_auxtrace_info(auxtrace_info->priv, 0);
2923 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_MAX);
2927 /* Extract header information - see cs-etm.h for format */
2928 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2930 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2931 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2935 * Create an RB tree for traceID-metadata tuple. Since the conversion
2936 * has to be made for each packet that gets decoded, optimizing access
2937 * in anything other than a sequential array is worth doing.
2939 traceid_list = intlist__new(NULL);
2940 if (!traceid_list) {
2945 metadata = zalloc(sizeof(*metadata) * num_cpu);
2948 goto err_free_traceid_list;
2952 * The metadata is stored in the auxtrace_info section and encodes
2953 * the configuration of the ARM embedded trace macrocell which is
2954 * required by the trace decoder to properly decode the trace due
2955 * to its highly compressed nature.
2957 for (j = 0; j < num_cpu; j++) {
2958 if (ptr[i] == __perf_cs_etmv3_magic) {
2960 cs_etm__create_meta_blk(ptr, &i,
2962 CS_ETM_NR_TRC_PARAMS_V0);
2964 /* The traceID is our handle */
2965 trcidr_idx = CS_ETM_ETMTRACEIDR;
2967 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2969 cs_etm__create_meta_blk(ptr, &i,
2971 CS_ETMV4_NR_TRC_PARAMS_V0);
2973 /* The traceID is our handle */
2974 trcidr_idx = CS_ETMV4_TRCTRACEIDR;
2975 } else if (ptr[i] == __perf_cs_ete_magic) {
2976 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
2978 /* ETE shares first part of metadata with ETMv4 */
2979 trcidr_idx = CS_ETMV4_TRCTRACEIDR;
2981 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
2984 goto err_free_metadata;
2989 goto err_free_metadata;
2992 /* Get an RB node for this CPU */
2993 inode = intlist__findnew(traceid_list, metadata[j][trcidr_idx]);
2995 /* Something went wrong, no need to continue */
2998 goto err_free_metadata;
3002 * The node for that CPU should not be taken.
3003 * Back out if that's the case.
3007 goto err_free_metadata;
3009 /* All good, associate the traceID with the metadata pointer */
3010 inode->priv = metadata[j];
3014 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
3015 * CS_ETMV4_PRIV_MAX mark how many double words are in the
3016 * global metadata, and each cpu's metadata respectively.
3017 * The following tests if the correct number of double words was
3018 * present in the auxtrace info section.
3020 if (i * 8 != priv_size) {
3022 goto err_free_metadata;
3025 etm = zalloc(sizeof(*etm));
3029 goto err_free_metadata;
3032 err = auxtrace_queues__init(&etm->queues);
3036 if (session->itrace_synth_opts->set) {
3037 etm->synth_opts = *session->itrace_synth_opts;
3039 itrace_synth_opts__set_default(&etm->synth_opts,
3040 session->itrace_synth_opts->default_no_sample);
3041 etm->synth_opts.callchain = false;
3044 etm->session = session;
3045 etm->machine = &session->machines.host;
3047 etm->num_cpu = num_cpu;
3048 etm->pmu_type = pmu_type;
3049 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
3050 etm->metadata = metadata;
3051 etm->auxtrace_type = auxtrace_info->type;
3052 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
3054 etm->auxtrace.process_event = cs_etm__process_event;
3055 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3056 etm->auxtrace.flush_events = cs_etm__flush_events;
3057 etm->auxtrace.free_events = cs_etm__free_events;
3058 etm->auxtrace.free = cs_etm__free;
3059 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3060 session->auxtrace = &etm->auxtrace;
3062 etm->unknown_thread = thread__new(999999999, 999999999);
3063 if (!etm->unknown_thread) {
3065 goto err_free_queues;
3069 * Initialize list node so that at thread__zput() we can avoid
3070 * segmentation fault at list_del_init().
3072 INIT_LIST_HEAD(&etm->unknown_thread->node);
3074 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
3076 goto err_delete_thread;
3078 if (thread__init_maps(etm->unknown_thread, etm->machine)) {
3080 goto err_delete_thread;
3084 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
3087 err = cs_etm__synth_events(etm, session);
3089 goto err_delete_thread;
3091 err = cs_etm__queue_aux_records(session);
3093 goto err_delete_thread;
3095 etm->data_queued = etm->queues.populated;
3097 * Print warning in pipe mode, see cs_etm__process_auxtrace_event() and
3098 * cs_etm__queue_aux_fragment() for details relating to limitations.
3100 if (!etm->data_queued)
3101 pr_warning("CS ETM warning: Coresight decode and TRBE support requires random file access.\n"
3102 "Continuing with best effort decoding in piped mode.\n\n");
3107 thread__zput(etm->unknown_thread);
3109 auxtrace_queues__free(&etm->queues);
3110 session->auxtrace = NULL;
3114 /* No need to check @metadata[j], free(NULL) is supported */
3115 for (j = 0; j < num_cpu; j++)
3116 zfree(&metadata[j]);
3118 err_free_traceid_list:
3119 intlist__delete(traceid_list);
3123 * At this point, as a minimum we have valid header. Dump the rest of
3124 * the info section - the print routines will error out on structural
3128 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);