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"
39 #include <tools/libc_compat.h>
40 #include "util/synthetic-events.h"
41 #include "util/util.h"
43 struct cs_etm_auxtrace {
44 struct auxtrace auxtrace;
45 struct auxtrace_queues queues;
46 struct auxtrace_heap heap;
47 struct itrace_synth_opts synth_opts;
48 struct perf_session *session;
49 struct machine *machine;
50 struct thread *unknown_thread;
51 struct perf_tsc_conversion tc;
54 * Timeless has no timestamps in the trace so overlapping mmap lookups
55 * are less accurate but produces smaller trace data. We use context IDs
56 * in the trace instead of matching timestamps with fork records so
57 * they're not really needed in the general case. Overlapping mmaps
58 * happen in cases like between a fork and an exec.
60 bool timeless_decoding;
63 * Per-thread ignores the trace channel ID and instead assumes that
64 * everything in a buffer comes from the same process regardless of
65 * which CPU it ran on. It also implies no context IDs so the TID is
66 * taken from the auxtrace buffer.
68 bool per_thread_decoding;
71 bool has_virtual_ts; /* Virtual/Kernel timestamps in the trace. */
74 u64 latest_kernel_timestamp;
76 u64 branches_sample_type;
78 u64 instructions_sample_type;
79 u64 instructions_sample_period;
82 unsigned int pmu_type;
85 struct cs_etm_traceid_queue {
88 u64 period_instructions;
89 size_t last_branch_pos;
90 union perf_event *event_buf;
91 struct thread *thread;
92 struct branch_stack *last_branch;
93 struct branch_stack *last_branch_rb;
94 struct cs_etm_packet *prev_packet;
95 struct cs_etm_packet *packet;
96 struct cs_etm_packet_queue packet_queue;
100 struct cs_etm_auxtrace *etm;
101 struct cs_etm_decoder *decoder;
102 struct auxtrace_buffer *buffer;
103 unsigned int queue_nr;
104 u8 pending_timestamp_chan_id;
106 const unsigned char *buf;
107 size_t buf_len, buf_used;
108 /* Conversion between traceID and index in traceid_queues array */
109 struct intlist *traceid_queues_list;
110 struct cs_etm_traceid_queue **traceid_queues;
113 /* RB tree for quick conversion between traceID and metadata pointers */
114 static struct intlist *traceid_list;
116 static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm);
117 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
119 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
120 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
122 /* PTMs ETMIDR [11:8] set to b0011 */
123 #define ETMIDR_PTM_VERSION 0x00000300
126 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
127 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
128 * encode the etm queue number as the upper 16 bit and the channel as
131 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
132 (queue_nr << 16 | trace_chan_id)
133 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
134 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
136 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
138 etmidr &= ETMIDR_PTM_VERSION;
140 if (etmidr == ETMIDR_PTM_VERSION)
141 return CS_ETM_PROTO_PTM;
143 return CS_ETM_PROTO_ETMV3;
146 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
148 struct int_node *inode;
151 inode = intlist__find(traceid_list, trace_chan_id);
155 metadata = inode->priv;
156 *magic = metadata[CS_ETM_MAGIC];
160 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
162 struct int_node *inode;
165 inode = intlist__find(traceid_list, trace_chan_id);
169 metadata = inode->priv;
170 *cpu = (int)metadata[CS_ETM_CPU];
175 * The returned PID format is presented by two bits:
177 * Bit ETM_OPT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced;
178 * Bit ETM_OPT_CTXTID2: CONTEXTIDR_EL2 is traced.
180 * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
181 * are enabled at the same time when the session runs on an EL2 kernel.
182 * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
183 * recorded in the trace data, the tool will selectively use
184 * CONTEXTIDR_EL2 as PID.
186 int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt)
188 struct int_node *inode;
191 inode = intlist__find(traceid_list, trace_chan_id);
195 metadata = inode->priv;
197 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
198 val = metadata[CS_ETM_ETMCR];
199 /* CONTEXTIDR is traced */
200 if (val & BIT(ETM_OPT_CTXTID))
201 *pid_fmt = BIT(ETM_OPT_CTXTID);
203 val = metadata[CS_ETMV4_TRCCONFIGR];
204 /* CONTEXTIDR_EL2 is traced */
205 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
206 *pid_fmt = BIT(ETM_OPT_CTXTID2);
207 /* CONTEXTIDR_EL1 is traced */
208 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
209 *pid_fmt = BIT(ETM_OPT_CTXTID);
215 static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
217 struct int_node *inode;
219 /* Get an RB node for this CPU */
220 inode = intlist__findnew(traceid_list, trace_chan_id);
222 /* Something went wrong, no need to continue */
227 * The node for that CPU should not be taken.
228 * Back out if that's the case.
233 /* All good, associate the traceID with the metadata pointer */
234 inode->priv = cpu_metadata;
239 static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata)
241 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
243 switch (cs_etm_magic) {
244 case __perf_cs_etmv3_magic:
245 *trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] &
246 CORESIGHT_TRACE_ID_VAL_MASK);
248 case __perf_cs_etmv4_magic:
249 case __perf_cs_ete_magic:
250 *trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] &
251 CORESIGHT_TRACE_ID_VAL_MASK);
260 * update metadata trace ID from the value found in the AUX_HW_INFO packet.
261 * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present.
263 static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
265 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
267 switch (cs_etm_magic) {
268 case __perf_cs_etmv3_magic:
269 cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id;
271 case __perf_cs_etmv4_magic:
272 case __perf_cs_ete_magic:
273 cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id;
283 * FIELD_GET (linux/bitfield.h) not available outside kernel code,
284 * and the header contains too many dependencies to just copy over,
285 * so roll our own based on the original
287 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
288 #define FIELD_GET(_mask, _reg) \
290 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
294 * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event.
296 * The payload associates the Trace ID and the CPU.
297 * The routine is tolerant of seeing multiple packets with the same association,
298 * but a CPU / Trace ID association changing during a session is an error.
300 static int cs_etm__process_aux_output_hw_id(struct perf_session *session,
301 union perf_event *event)
303 struct cs_etm_auxtrace *etm;
304 struct perf_sample sample;
305 struct int_node *inode;
309 int cpu, version, err;
310 u8 trace_chan_id, curr_chan_id;
312 /* extract and parse the HW ID */
313 hw_id = event->aux_output_hw_id.hw_id;
314 version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id);
315 trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id);
317 /* check that we can handle this version */
318 if (version > CS_AUX_HW_ID_CURR_VERSION)
321 /* get access to the etm metadata */
322 etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
323 if (!etm || !etm->metadata)
326 /* parse the sample to get the CPU */
327 evsel = evlist__event2evsel(session->evlist, event);
330 err = evsel__parse_sample(evsel, event, &sample);
335 /* no CPU in the sample - possibly recorded with an old version of perf */
336 pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record.");
340 /* See if the ID is mapped to a CPU, and it matches the current CPU */
341 inode = intlist__find(traceid_list, trace_chan_id);
343 cpu_data = inode->priv;
344 if ((int)cpu_data[CS_ETM_CPU] != cpu) {
345 pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n");
349 /* check that the mapped ID matches */
350 err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data);
353 if (curr_chan_id != trace_chan_id) {
354 pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n");
358 /* mapped and matched - return OK */
362 /* not one we've seen before - lets map it */
363 cpu_data = etm->metadata[cpu];
364 err = cs_etm__map_trace_id(trace_chan_id, cpu_data);
369 * if we are picking up the association from the packet, need to plug
370 * the correct trace ID into the metadata for setting up decoders later.
372 err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data);
376 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
380 * When a timestamp packet is encountered the backend code
381 * is stopped so that the front end has time to process packets
382 * that were accumulated in the traceID queue. Since there can
383 * be more than one channel per cs_etm_queue, we need to specify
384 * what traceID queue needs servicing.
386 etmq->pending_timestamp_chan_id = trace_chan_id;
389 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
392 struct cs_etm_packet_queue *packet_queue;
394 if (!etmq->pending_timestamp_chan_id)
398 *trace_chan_id = etmq->pending_timestamp_chan_id;
400 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
401 etmq->pending_timestamp_chan_id);
405 /* Acknowledge pending status */
406 etmq->pending_timestamp_chan_id = 0;
408 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
409 return packet_queue->cs_timestamp;
412 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
418 queue->packet_count = 0;
419 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
420 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
421 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
422 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
423 queue->packet_buffer[i].instr_count = 0;
424 queue->packet_buffer[i].last_instr_taken_branch = false;
425 queue->packet_buffer[i].last_instr_size = 0;
426 queue->packet_buffer[i].last_instr_type = 0;
427 queue->packet_buffer[i].last_instr_subtype = 0;
428 queue->packet_buffer[i].last_instr_cond = 0;
429 queue->packet_buffer[i].flags = 0;
430 queue->packet_buffer[i].exception_number = UINT32_MAX;
431 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
432 queue->packet_buffer[i].cpu = INT_MIN;
436 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
439 struct int_node *inode;
440 struct cs_etm_traceid_queue *tidq;
441 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
443 intlist__for_each_entry(inode, traceid_queues_list) {
444 idx = (int)(intptr_t)inode->priv;
445 tidq = etmq->traceid_queues[idx];
446 cs_etm__clear_packet_queue(&tidq->packet_queue);
450 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
451 struct cs_etm_traceid_queue *tidq,
455 struct auxtrace_queue *queue;
456 struct cs_etm_auxtrace *etm = etmq->etm;
458 cs_etm__clear_packet_queue(&tidq->packet_queue);
460 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
461 tidq->tid = queue->tid;
463 tidq->trace_chan_id = trace_chan_id;
465 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
469 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
470 if (!tidq->prev_packet)
473 if (etm->synth_opts.last_branch) {
474 size_t sz = sizeof(struct branch_stack);
476 sz += etm->synth_opts.last_branch_sz *
477 sizeof(struct branch_entry);
478 tidq->last_branch = zalloc(sz);
479 if (!tidq->last_branch)
481 tidq->last_branch_rb = zalloc(sz);
482 if (!tidq->last_branch_rb)
486 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
487 if (!tidq->event_buf)
493 zfree(&tidq->last_branch_rb);
494 zfree(&tidq->last_branch);
495 zfree(&tidq->prev_packet);
496 zfree(&tidq->packet);
501 static struct cs_etm_traceid_queue
502 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
505 struct int_node *inode;
506 struct intlist *traceid_queues_list;
507 struct cs_etm_traceid_queue *tidq, **traceid_queues;
508 struct cs_etm_auxtrace *etm = etmq->etm;
510 if (etm->per_thread_decoding)
511 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
513 traceid_queues_list = etmq->traceid_queues_list;
516 * Check if the traceid_queue exist for this traceID by looking
519 inode = intlist__find(traceid_queues_list, trace_chan_id);
521 idx = (int)(intptr_t)inode->priv;
522 return etmq->traceid_queues[idx];
525 /* We couldn't find a traceid_queue for this traceID, allocate one */
526 tidq = malloc(sizeof(*tidq));
530 memset(tidq, 0, sizeof(*tidq));
532 /* Get a valid index for the new traceid_queue */
533 idx = intlist__nr_entries(traceid_queues_list);
534 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
535 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
539 /* Associate this traceID with this index */
540 inode->priv = (void *)(intptr_t)idx;
542 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
545 /* Grow the traceid_queues array by one unit */
546 traceid_queues = etmq->traceid_queues;
547 traceid_queues = reallocarray(traceid_queues,
549 sizeof(*traceid_queues));
552 * On failure reallocarray() returns NULL and the original block of
553 * memory is left untouched.
558 traceid_queues[idx] = tidq;
559 etmq->traceid_queues = traceid_queues;
561 return etmq->traceid_queues[idx];
565 * Function intlist__remove() removes the inode from the list
566 * and delete the memory associated to it.
568 intlist__remove(traceid_queues_list, inode);
574 struct cs_etm_packet_queue
575 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
577 struct cs_etm_traceid_queue *tidq;
579 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
581 return &tidq->packet_queue;
586 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
587 struct cs_etm_traceid_queue *tidq)
589 struct cs_etm_packet *tmp;
591 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
592 etm->synth_opts.instructions) {
594 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
595 * the next incoming packet.
598 tidq->packet = tidq->prev_packet;
599 tidq->prev_packet = tmp;
603 static void cs_etm__packet_dump(const char *pkt_string)
605 const char *color = PERF_COLOR_BLUE;
606 int len = strlen(pkt_string);
608 if (len && (pkt_string[len-1] == '\n'))
609 color_fprintf(stdout, color, " %s", pkt_string);
611 color_fprintf(stdout, color, " %s\n", pkt_string);
616 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
617 struct cs_etm_auxtrace *etm, int idx,
620 u64 **metadata = etm->metadata;
622 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
623 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
624 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
627 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
628 struct cs_etm_auxtrace *etm, int idx)
630 u64 **metadata = etm->metadata;
632 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
633 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
634 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
635 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
636 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
637 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
638 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
641 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
642 struct cs_etm_auxtrace *etm, int idx)
644 u64 **metadata = etm->metadata;
646 t_params[idx].protocol = CS_ETM_PROTO_ETE;
647 t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETE_TRCIDR0];
648 t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETE_TRCIDR1];
649 t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETE_TRCIDR2];
650 t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETE_TRCIDR8];
651 t_params[idx].ete.reg_configr = metadata[idx][CS_ETE_TRCCONFIGR];
652 t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETE_TRCTRACEIDR];
653 t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
656 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
657 struct cs_etm_auxtrace *etm,
664 for (i = 0; i < decoders; i++) {
665 architecture = etm->metadata[i][CS_ETM_MAGIC];
667 switch (architecture) {
668 case __perf_cs_etmv3_magic:
669 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
670 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
672 case __perf_cs_etmv4_magic:
673 cs_etm__set_trace_param_etmv4(t_params, etm, i);
675 case __perf_cs_ete_magic:
676 cs_etm__set_trace_param_ete(t_params, etm, i);
686 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
687 struct cs_etm_queue *etmq,
688 enum cs_etm_decoder_operation mode,
693 if (!(mode < CS_ETM_OPERATION_MAX))
696 d_params->packet_printer = cs_etm__packet_dump;
697 d_params->operation = mode;
698 d_params->data = etmq;
699 d_params->formatted = formatted;
700 d_params->fsyncs = false;
701 d_params->hsyncs = false;
702 d_params->frame_aligned = true;
709 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
710 struct auxtrace_buffer *buffer)
713 const char *color = PERF_COLOR_BLUE;
714 size_t buffer_used = 0;
716 fprintf(stdout, "\n");
717 color_fprintf(stdout, color,
718 ". ... CoreSight %s Trace data: size %#zx bytes\n",
719 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
724 ret = cs_etm_decoder__process_data_block(
725 etmq->decoder, buffer->offset,
726 &((u8 *)buffer->data)[buffer_used],
727 buffer->size - buffer_used, &consumed);
731 buffer_used += consumed;
732 } while (buffer_used < buffer->size);
734 cs_etm_decoder__reset(etmq->decoder);
737 static int cs_etm__flush_events(struct perf_session *session,
738 struct perf_tool *tool)
740 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
741 struct cs_etm_auxtrace,
746 if (!tool->ordered_events)
749 if (etm->timeless_decoding) {
751 * Pass tid = -1 to process all queues. But likely they will have
752 * already been processed on PERF_RECORD_EXIT anyway.
754 return cs_etm__process_timeless_queues(etm, -1);
757 return cs_etm__process_timestamped_queues(etm);
760 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
764 struct int_node *inode, *tmp;
765 struct cs_etm_traceid_queue *tidq;
766 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
768 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
769 priv = (uintptr_t)inode->priv;
772 /* Free this traceid_queue from the array */
773 tidq = etmq->traceid_queues[idx];
774 thread__zput(tidq->thread);
775 zfree(&tidq->event_buf);
776 zfree(&tidq->last_branch);
777 zfree(&tidq->last_branch_rb);
778 zfree(&tidq->prev_packet);
779 zfree(&tidq->packet);
783 * Function intlist__remove() removes the inode from the list
784 * and delete the memory associated to it.
786 intlist__remove(traceid_queues_list, inode);
789 /* Then the RB tree itself */
790 intlist__delete(traceid_queues_list);
791 etmq->traceid_queues_list = NULL;
793 /* finally free the traceid_queues array */
794 zfree(&etmq->traceid_queues);
797 static void cs_etm__free_queue(void *priv)
799 struct cs_etm_queue *etmq = priv;
804 cs_etm_decoder__free(etmq->decoder);
805 cs_etm__free_traceid_queues(etmq);
809 static void cs_etm__free_events(struct perf_session *session)
812 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
813 struct cs_etm_auxtrace,
815 struct auxtrace_queues *queues = &aux->queues;
817 for (i = 0; i < queues->nr_queues; i++) {
818 cs_etm__free_queue(queues->queue_array[i].priv);
819 queues->queue_array[i].priv = NULL;
822 auxtrace_queues__free(queues);
825 static void cs_etm__free(struct perf_session *session)
828 struct int_node *inode, *tmp;
829 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
830 struct cs_etm_auxtrace,
832 cs_etm__free_events(session);
833 session->auxtrace = NULL;
835 /* First remove all traceID/metadata nodes for the RB tree */
836 intlist__for_each_entry_safe(inode, tmp, traceid_list)
837 intlist__remove(traceid_list, inode);
838 /* Then the RB tree itself */
839 intlist__delete(traceid_list);
841 for (i = 0; i < aux->num_cpu; i++)
842 zfree(&aux->metadata[i]);
844 thread__zput(aux->unknown_thread);
845 zfree(&aux->metadata);
849 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
852 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
853 struct cs_etm_auxtrace,
856 return evsel->core.attr.type == aux->pmu_type;
859 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
861 struct machine *machine;
863 machine = etmq->etm->machine;
865 if (address >= machine__kernel_start(machine)) {
866 if (machine__is_host(machine))
867 return PERF_RECORD_MISC_KERNEL;
869 return PERF_RECORD_MISC_GUEST_KERNEL;
871 if (machine__is_host(machine))
872 return PERF_RECORD_MISC_USER;
874 return PERF_RECORD_MISC_GUEST_USER;
876 return PERF_RECORD_MISC_HYPERVISOR;
880 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
881 u64 address, size_t size, u8 *buffer)
886 struct thread *thread;
887 struct machine *machine;
888 struct addr_location al;
890 struct cs_etm_traceid_queue *tidq;
895 machine = etmq->etm->machine;
896 cpumode = cs_etm__cpu_mode(etmq, address);
897 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
901 thread = tidq->thread;
903 if (cpumode != PERF_RECORD_MISC_KERNEL)
905 thread = etmq->etm->unknown_thread;
908 if (!thread__find_map(thread, cpumode, address, &al))
911 dso = map__dso(al.map);
915 if (dso->data.status == DSO_DATA_STATUS_ERROR &&
916 dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))
919 offset = map__map_ip(al.map, address);
923 len = dso__data_read_offset(dso, machine, offset, buffer, size);
926 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
927 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
928 if (!dso->auxtrace_warned) {
929 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
931 dso->long_name ? dso->long_name : "Unknown");
932 dso->auxtrace_warned = true;
940 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
943 struct cs_etm_decoder_params d_params;
944 struct cs_etm_trace_params *t_params = NULL;
945 struct cs_etm_queue *etmq;
947 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
950 int decoders = formatted ? etm->num_cpu : 1;
952 etmq = zalloc(sizeof(*etmq));
956 etmq->traceid_queues_list = intlist__new(NULL);
957 if (!etmq->traceid_queues_list)
960 /* Use metadata to fill in trace parameters for trace decoder */
961 t_params = zalloc(sizeof(*t_params) * decoders);
966 if (cs_etm__init_trace_params(t_params, etm, decoders))
969 /* Set decoder parameters to decode trace packets */
970 if (cs_etm__init_decoder_params(&d_params, etmq,
971 dump_trace ? CS_ETM_OPERATION_PRINT :
972 CS_ETM_OPERATION_DECODE,
976 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
983 * Register a function to handle all memory accesses required by
984 * the trace decoder library.
986 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
989 goto out_free_decoder;
995 cs_etm_decoder__free(etmq->decoder);
997 intlist__delete(etmq->traceid_queues_list);
1003 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
1004 struct auxtrace_queue *queue,
1005 unsigned int queue_nr,
1008 struct cs_etm_queue *etmq = queue->priv;
1010 if (list_empty(&queue->head) || etmq)
1013 etmq = cs_etm__alloc_queue(etm, formatted);
1020 etmq->queue_nr = queue_nr;
1026 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
1027 struct cs_etm_queue *etmq,
1028 unsigned int queue_nr)
1031 unsigned int cs_queue_nr;
1036 * We are under a CPU-wide trace scenario. As such we need to know
1037 * when the code that generated the traces started to execute so that
1038 * it can be correlated with execution on other CPUs. So we get a
1039 * handle on the beginning of traces and decode until we find a
1040 * timestamp. The timestamp is then added to the auxtrace min heap
1041 * in order to know what nibble (of all the etmqs) to decode first.
1045 * Fetch an aux_buffer from this etmq. Bail if no more
1046 * blocks or an error has been encountered.
1048 ret = cs_etm__get_data_block(etmq);
1053 * Run decoder on the trace block. The decoder will stop when
1054 * encountering a CS timestamp, a full packet queue or the end of
1055 * trace for that block.
1057 ret = cs_etm__decode_data_block(etmq);
1062 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
1063 * the timestamp calculation for us.
1065 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
1067 /* We found a timestamp, no need to continue. */
1072 * We didn't find a timestamp so empty all the traceid packet
1073 * queues before looking for another timestamp packet, either
1074 * in the current data block or a new one. Packets that were
1075 * just decoded are useless since no timestamp has been
1076 * associated with them. As such simply discard them.
1078 cs_etm__clear_all_packet_queues(etmq);
1082 * We have a timestamp. Add it to the min heap to reflect when
1083 * instructions conveyed by the range packets of this traceID queue
1084 * started to execute. Once the same has been done for all the traceID
1085 * queues of each etmq, redenring and decoding can start in
1086 * chronological order.
1088 * Note that packets decoded above are still in the traceID's packet
1089 * queue and will be processed in cs_etm__process_timestamped_queues().
1091 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
1092 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
1098 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
1099 struct cs_etm_traceid_queue *tidq)
1101 struct branch_stack *bs_src = tidq->last_branch_rb;
1102 struct branch_stack *bs_dst = tidq->last_branch;
1106 * Set the number of records before early exit: ->nr is used to
1107 * determine how many branches to copy from ->entries.
1109 bs_dst->nr = bs_src->nr;
1112 * Early exit when there is nothing to copy.
1118 * As bs_src->entries is a circular buffer, we need to copy from it in
1119 * two steps. First, copy the branches from the most recently inserted
1120 * branch ->last_branch_pos until the end of bs_src->entries buffer.
1122 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
1123 memcpy(&bs_dst->entries[0],
1124 &bs_src->entries[tidq->last_branch_pos],
1125 sizeof(struct branch_entry) * nr);
1128 * If we wrapped around at least once, the branches from the beginning
1129 * of the bs_src->entries buffer and until the ->last_branch_pos element
1130 * are older valid branches: copy them over. The total number of
1131 * branches copied over will be equal to the number of branches asked by
1132 * the user in last_branch_sz.
1134 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
1135 memcpy(&bs_dst->entries[nr],
1136 &bs_src->entries[0],
1137 sizeof(struct branch_entry) * tidq->last_branch_pos);
1142 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
1144 tidq->last_branch_pos = 0;
1145 tidq->last_branch_rb->nr = 0;
1148 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
1149 u8 trace_chan_id, u64 addr)
1153 cs_etm__mem_access(etmq, trace_chan_id, addr,
1154 ARRAY_SIZE(instrBytes), instrBytes);
1156 * T32 instruction size is indicated by bits[15:11] of the first
1157 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
1158 * denote a 32-bit instruction.
1160 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
1163 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
1165 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1166 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1169 return packet->start_addr;
1173 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
1175 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1176 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1179 return packet->end_addr - packet->last_instr_size;
1182 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
1184 const struct cs_etm_packet *packet,
1187 if (packet->isa == CS_ETM_ISA_T32) {
1188 u64 addr = packet->start_addr;
1191 addr += cs_etm__t32_instr_size(etmq,
1192 trace_chan_id, addr);
1198 /* Assume a 4 byte instruction size (A32/A64) */
1199 return packet->start_addr + offset * 4;
1202 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1203 struct cs_etm_traceid_queue *tidq)
1205 struct branch_stack *bs = tidq->last_branch_rb;
1206 struct branch_entry *be;
1209 * The branches are recorded in a circular buffer in reverse
1210 * chronological order: we start recording from the last element of the
1211 * buffer down. After writing the first element of the stack, move the
1212 * insert position back to the end of the buffer.
1214 if (!tidq->last_branch_pos)
1215 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1217 tidq->last_branch_pos -= 1;
1219 be = &bs->entries[tidq->last_branch_pos];
1220 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1221 be->to = cs_etm__first_executed_instr(tidq->packet);
1222 /* No support for mispredict */
1223 be->flags.mispred = 0;
1224 be->flags.predicted = 1;
1227 * Increment bs->nr until reaching the number of last branches asked by
1228 * the user on the command line.
1230 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1234 static int cs_etm__inject_event(union perf_event *event,
1235 struct perf_sample *sample, u64 type)
1237 event->header.size = perf_event__sample_event_size(sample, type, 0);
1238 return perf_event__synthesize_sample(event, type, 0, sample);
1243 cs_etm__get_trace(struct cs_etm_queue *etmq)
1245 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1246 struct auxtrace_buffer *old_buffer = aux_buffer;
1247 struct auxtrace_queue *queue;
1249 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1251 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1253 /* If no more data, drop the previous auxtrace_buffer and return */
1256 auxtrace_buffer__drop_data(old_buffer);
1261 etmq->buffer = aux_buffer;
1263 /* If the aux_buffer doesn't have data associated, try to load it */
1264 if (!aux_buffer->data) {
1265 /* get the file desc associated with the perf data file */
1266 int fd = perf_data__fd(etmq->etm->session->data);
1268 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1269 if (!aux_buffer->data)
1273 /* If valid, drop the previous buffer */
1275 auxtrace_buffer__drop_data(old_buffer);
1278 etmq->buf_len = aux_buffer->size;
1279 etmq->buf = aux_buffer->data;
1281 return etmq->buf_len;
1284 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1285 struct cs_etm_traceid_queue *tidq)
1287 if ((!tidq->thread) && (tidq->tid != -1))
1288 tidq->thread = machine__find_thread(etm->machine, -1,
1292 tidq->pid = tidq->thread->pid_;
1295 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1296 pid_t tid, u8 trace_chan_id)
1298 int cpu, err = -EINVAL;
1299 struct cs_etm_auxtrace *etm = etmq->etm;
1300 struct cs_etm_traceid_queue *tidq;
1302 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1306 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1309 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1314 thread__zput(tidq->thread);
1316 cs_etm__set_pid_tid_cpu(etm, tidq);
1320 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1322 return !!etmq->etm->timeless_decoding;
1325 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1327 const struct cs_etm_packet *packet,
1328 struct perf_sample *sample)
1331 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1332 * packet, so directly bail out with 'insn_len' = 0.
1334 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1335 sample->insn_len = 0;
1340 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1341 * cs_etm__t32_instr_size().
1343 if (packet->isa == CS_ETM_ISA_T32)
1344 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1346 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1348 sample->insn_len = 4;
1350 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1351 sample->insn_len, (void *)sample->insn);
1354 u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp)
1356 struct cs_etm_auxtrace *etm = etmq->etm;
1358 if (etm->has_virtual_ts)
1359 return tsc_to_perf_time(cs_timestamp, &etm->tc);
1361 return cs_timestamp;
1364 static inline u64 cs_etm__resolve_sample_time(struct cs_etm_queue *etmq,
1365 struct cs_etm_traceid_queue *tidq)
1367 struct cs_etm_auxtrace *etm = etmq->etm;
1368 struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue;
1370 if (!etm->timeless_decoding && etm->has_virtual_ts)
1371 return packet_queue->cs_timestamp;
1373 return etm->latest_kernel_timestamp;
1376 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1377 struct cs_etm_traceid_queue *tidq,
1378 u64 addr, u64 period)
1381 struct cs_etm_auxtrace *etm = etmq->etm;
1382 union perf_event *event = tidq->event_buf;
1383 struct perf_sample sample = {.ip = 0,};
1385 event->sample.header.type = PERF_RECORD_SAMPLE;
1386 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1387 event->sample.header.size = sizeof(struct perf_event_header);
1389 /* Set time field based on etm auxtrace config. */
1390 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1393 sample.pid = tidq->pid;
1394 sample.tid = tidq->tid;
1395 sample.id = etmq->etm->instructions_id;
1396 sample.stream_id = etmq->etm->instructions_id;
1397 sample.period = period;
1398 sample.cpu = tidq->packet->cpu;
1399 sample.flags = tidq->prev_packet->flags;
1400 sample.cpumode = event->sample.header.misc;
1402 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1404 if (etm->synth_opts.last_branch)
1405 sample.branch_stack = tidq->last_branch;
1407 if (etm->synth_opts.inject) {
1408 ret = cs_etm__inject_event(event, &sample,
1409 etm->instructions_sample_type);
1414 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1418 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1425 * The cs etm packet encodes an instruction range between a branch target
1426 * and the next taken branch. Generate sample accordingly.
1428 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1429 struct cs_etm_traceid_queue *tidq)
1432 struct cs_etm_auxtrace *etm = etmq->etm;
1433 struct perf_sample sample = {.ip = 0,};
1434 union perf_event *event = tidq->event_buf;
1435 struct dummy_branch_stack {
1438 struct branch_entry entries;
1442 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1444 event->sample.header.type = PERF_RECORD_SAMPLE;
1445 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1446 event->sample.header.size = sizeof(struct perf_event_header);
1448 /* Set time field based on etm auxtrace config. */
1449 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1452 sample.pid = tidq->pid;
1453 sample.tid = tidq->tid;
1454 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1455 sample.id = etmq->etm->branches_id;
1456 sample.stream_id = etmq->etm->branches_id;
1458 sample.cpu = tidq->packet->cpu;
1459 sample.flags = tidq->prev_packet->flags;
1460 sample.cpumode = event->sample.header.misc;
1462 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1466 * perf report cannot handle events without a branch stack
1468 if (etm->synth_opts.last_branch) {
1469 dummy_bs = (struct dummy_branch_stack){
1477 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1480 if (etm->synth_opts.inject) {
1481 ret = cs_etm__inject_event(event, &sample,
1482 etm->branches_sample_type);
1487 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1491 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1497 struct cs_etm_synth {
1498 struct perf_tool dummy_tool;
1499 struct perf_session *session;
1502 static int cs_etm__event_synth(struct perf_tool *tool,
1503 union perf_event *event,
1504 struct perf_sample *sample __maybe_unused,
1505 struct machine *machine __maybe_unused)
1507 struct cs_etm_synth *cs_etm_synth =
1508 container_of(tool, struct cs_etm_synth, dummy_tool);
1510 return perf_session__deliver_synth_event(cs_etm_synth->session,
1514 static int cs_etm__synth_event(struct perf_session *session,
1515 struct perf_event_attr *attr, u64 id)
1517 struct cs_etm_synth cs_etm_synth;
1519 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1520 cs_etm_synth.session = session;
1522 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1523 &id, cs_etm__event_synth);
1526 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1527 struct perf_session *session)
1529 struct evlist *evlist = session->evlist;
1530 struct evsel *evsel;
1531 struct perf_event_attr attr;
1536 evlist__for_each_entry(evlist, evsel) {
1537 if (evsel->core.attr.type == etm->pmu_type) {
1544 pr_debug("No selected events with CoreSight Trace data\n");
1548 memset(&attr, 0, sizeof(struct perf_event_attr));
1549 attr.size = sizeof(struct perf_event_attr);
1550 attr.type = PERF_TYPE_HARDWARE;
1551 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1552 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1554 if (etm->timeless_decoding)
1555 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1557 attr.sample_type |= PERF_SAMPLE_TIME;
1559 attr.exclude_user = evsel->core.attr.exclude_user;
1560 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1561 attr.exclude_hv = evsel->core.attr.exclude_hv;
1562 attr.exclude_host = evsel->core.attr.exclude_host;
1563 attr.exclude_guest = evsel->core.attr.exclude_guest;
1564 attr.sample_id_all = evsel->core.attr.sample_id_all;
1565 attr.read_format = evsel->core.attr.read_format;
1567 /* create new id val to be a fixed offset from evsel id */
1568 id = evsel->core.id[0] + 1000000000;
1573 if (etm->synth_opts.branches) {
1574 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1575 attr.sample_period = 1;
1576 attr.sample_type |= PERF_SAMPLE_ADDR;
1577 err = cs_etm__synth_event(session, &attr, id);
1580 etm->branches_sample_type = attr.sample_type;
1581 etm->branches_id = id;
1583 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1586 if (etm->synth_opts.last_branch) {
1587 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1589 * We don't use the hardware index, but the sample generation
1590 * code uses the new format branch_stack with this field,
1591 * so the event attributes must indicate that it's present.
1593 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1596 if (etm->synth_opts.instructions) {
1597 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1598 attr.sample_period = etm->synth_opts.period;
1599 etm->instructions_sample_period = attr.sample_period;
1600 err = cs_etm__synth_event(session, &attr, id);
1603 etm->instructions_sample_type = attr.sample_type;
1604 etm->instructions_id = id;
1611 static int cs_etm__sample(struct cs_etm_queue *etmq,
1612 struct cs_etm_traceid_queue *tidq)
1614 struct cs_etm_auxtrace *etm = etmq->etm;
1616 u8 trace_chan_id = tidq->trace_chan_id;
1619 /* Get instructions remainder from previous packet */
1620 instrs_prev = tidq->period_instructions;
1622 tidq->period_instructions += tidq->packet->instr_count;
1625 * Record a branch when the last instruction in
1626 * PREV_PACKET is a branch.
1628 if (etm->synth_opts.last_branch &&
1629 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1630 tidq->prev_packet->last_instr_taken_branch)
1631 cs_etm__update_last_branch_rb(etmq, tidq);
1633 if (etm->synth_opts.instructions &&
1634 tidq->period_instructions >= etm->instructions_sample_period) {
1636 * Emit instruction sample periodically
1637 * TODO: allow period to be defined in cycles and clock time
1641 * Below diagram demonstrates the instruction samples
1644 * Instrs Instrs Instrs Instrs
1645 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1648 * --------------------------------------------------
1652 * instructions(Pi) instructions(Pi')
1655 * \---------------- -----------------/
1657 * tidq->packet->instr_count
1659 * Instrs Sample(n...) are the synthesised samples occurring
1660 * every etm->instructions_sample_period instructions - as
1661 * defined on the perf command line. Sample(n) is being the
1662 * last sample before the current etm packet, n+1 to n+3
1663 * samples are generated from the current etm packet.
1665 * tidq->packet->instr_count represents the number of
1666 * instructions in the current etm packet.
1668 * Period instructions (Pi) contains the number of
1669 * instructions executed after the sample point(n) from the
1670 * previous etm packet. This will always be less than
1671 * etm->instructions_sample_period.
1673 * When generate new samples, it combines with two parts
1674 * instructions, one is the tail of the old packet and another
1675 * is the head of the new coming packet, to generate
1676 * sample(n+1); sample(n+2) and sample(n+3) consume the
1677 * instructions with sample period. After sample(n+3), the rest
1678 * instructions will be used by later packet and it is assigned
1679 * to tidq->period_instructions for next round calculation.
1683 * Get the initial offset into the current packet instructions;
1684 * entry conditions ensure that instrs_prev is less than
1685 * etm->instructions_sample_period.
1687 u64 offset = etm->instructions_sample_period - instrs_prev;
1690 /* Prepare last branches for instruction sample */
1691 if (etm->synth_opts.last_branch)
1692 cs_etm__copy_last_branch_rb(etmq, tidq);
1694 while (tidq->period_instructions >=
1695 etm->instructions_sample_period) {
1697 * Calculate the address of the sampled instruction (-1
1698 * as sample is reported as though instruction has just
1699 * been executed, but PC has not advanced to next
1702 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1703 tidq->packet, offset - 1);
1704 ret = cs_etm__synth_instruction_sample(
1706 etm->instructions_sample_period);
1710 offset += etm->instructions_sample_period;
1711 tidq->period_instructions -=
1712 etm->instructions_sample_period;
1716 if (etm->synth_opts.branches) {
1717 bool generate_sample = false;
1719 /* Generate sample for tracing on packet */
1720 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1721 generate_sample = true;
1723 /* Generate sample for branch taken packet */
1724 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1725 tidq->prev_packet->last_instr_taken_branch)
1726 generate_sample = true;
1728 if (generate_sample) {
1729 ret = cs_etm__synth_branch_sample(etmq, tidq);
1735 cs_etm__packet_swap(etm, tidq);
1740 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1743 * When the exception packet is inserted, whether the last instruction
1744 * in previous range packet is taken branch or not, we need to force
1745 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1746 * to generate branch sample for the instruction range before the
1747 * exception is trapped to kernel or before the exception returning.
1749 * The exception packet includes the dummy address values, so don't
1750 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1751 * for generating instruction and branch samples.
1753 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1754 tidq->prev_packet->last_instr_taken_branch = true;
1759 static int cs_etm__flush(struct cs_etm_queue *etmq,
1760 struct cs_etm_traceid_queue *tidq)
1763 struct cs_etm_auxtrace *etm = etmq->etm;
1765 /* Handle start tracing packet */
1766 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1769 if (etmq->etm->synth_opts.last_branch &&
1770 etmq->etm->synth_opts.instructions &&
1771 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1774 /* Prepare last branches for instruction sample */
1775 cs_etm__copy_last_branch_rb(etmq, tidq);
1778 * Generate a last branch event for the branches left in the
1779 * circular buffer at the end of the trace.
1781 * Use the address of the end of the last reported execution
1784 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1786 err = cs_etm__synth_instruction_sample(
1788 tidq->period_instructions);
1792 tidq->period_instructions = 0;
1796 if (etm->synth_opts.branches &&
1797 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1798 err = cs_etm__synth_branch_sample(etmq, tidq);
1804 cs_etm__packet_swap(etm, tidq);
1806 /* Reset last branches after flush the trace */
1807 if (etm->synth_opts.last_branch)
1808 cs_etm__reset_last_branch_rb(tidq);
1813 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1814 struct cs_etm_traceid_queue *tidq)
1819 * It has no new packet coming and 'etmq->packet' contains the stale
1820 * packet which was set at the previous time with packets swapping;
1821 * so skip to generate branch sample to avoid stale packet.
1823 * For this case only flush branch stack and generate a last branch
1824 * event for the branches left in the circular buffer at the end of
1827 if (etmq->etm->synth_opts.last_branch &&
1828 etmq->etm->synth_opts.instructions &&
1829 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1832 /* Prepare last branches for instruction sample */
1833 cs_etm__copy_last_branch_rb(etmq, tidq);
1836 * Use the address of the end of the last reported execution
1839 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1841 err = cs_etm__synth_instruction_sample(
1843 tidq->period_instructions);
1847 tidq->period_instructions = 0;
1853 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1855 * Returns: < 0 if error
1856 * = 0 if no more auxtrace_buffer to read
1857 * > 0 if the current buffer isn't empty yet
1859 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1863 if (!etmq->buf_len) {
1864 ret = cs_etm__get_trace(etmq);
1868 * We cannot assume consecutive blocks in the data file
1869 * are contiguous, reset the decoder to force re-sync.
1871 ret = cs_etm_decoder__reset(etmq->decoder);
1876 return etmq->buf_len;
1879 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1880 struct cs_etm_packet *packet,
1883 /* Initialise to keep compiler happy */
1888 switch (packet->isa) {
1889 case CS_ETM_ISA_T32:
1891 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1894 * +-----------------+--------+
1895 * | 1 1 0 1 1 1 1 1 | imm8 |
1896 * +-----------------+--------+
1898 * According to the specification, it only defines SVC for T32
1899 * with 16 bits instruction and has no definition for 32bits;
1900 * so below only read 2 bytes as instruction size for T32.
1902 addr = end_addr - 2;
1903 cs_etm__mem_access(etmq, trace_chan_id, addr,
1904 sizeof(instr16), (u8 *)&instr16);
1905 if ((instr16 & 0xFF00) == 0xDF00)
1909 case CS_ETM_ISA_A32:
1911 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1913 * b'31 b'28 b'27 b'24
1914 * +---------+---------+-------------------------+
1915 * | !1111 | 1 1 1 1 | imm24 |
1916 * +---------+---------+-------------------------+
1918 addr = end_addr - 4;
1919 cs_etm__mem_access(etmq, trace_chan_id, addr,
1920 sizeof(instr32), (u8 *)&instr32);
1921 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1922 (instr32 & 0xF0000000) != 0xF0000000)
1926 case CS_ETM_ISA_A64:
1928 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1931 * +-----------------------+---------+-----------+
1932 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1933 * +-----------------------+---------+-----------+
1935 addr = end_addr - 4;
1936 cs_etm__mem_access(etmq, trace_chan_id, addr,
1937 sizeof(instr32), (u8 *)&instr32);
1938 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1942 case CS_ETM_ISA_UNKNOWN:
1950 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1951 struct cs_etm_traceid_queue *tidq, u64 magic)
1953 u8 trace_chan_id = tidq->trace_chan_id;
1954 struct cs_etm_packet *packet = tidq->packet;
1955 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1957 if (magic == __perf_cs_etmv3_magic)
1958 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1962 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1963 * HVC cases; need to check if it's SVC instruction based on
1966 if (magic == __perf_cs_etmv4_magic) {
1967 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1968 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1969 prev_packet->end_addr))
1976 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1979 struct cs_etm_packet *packet = tidq->packet;
1981 if (magic == __perf_cs_etmv3_magic)
1982 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1983 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1984 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1985 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1986 packet->exception_number == CS_ETMV3_EXC_FIQ)
1989 if (magic == __perf_cs_etmv4_magic)
1990 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1991 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1992 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1993 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1994 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1995 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1996 packet->exception_number == CS_ETMV4_EXC_FIQ)
2002 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
2003 struct cs_etm_traceid_queue *tidq,
2006 u8 trace_chan_id = tidq->trace_chan_id;
2007 struct cs_etm_packet *packet = tidq->packet;
2008 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2010 if (magic == __perf_cs_etmv3_magic)
2011 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
2012 packet->exception_number == CS_ETMV3_EXC_HYP ||
2013 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
2014 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
2015 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
2016 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
2017 packet->exception_number == CS_ETMV3_EXC_GENERIC)
2020 if (magic == __perf_cs_etmv4_magic) {
2021 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
2022 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
2023 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
2024 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
2028 * For CS_ETMV4_EXC_CALL, except SVC other instructions
2029 * (SMC, HVC) are taken as sync exceptions.
2031 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2032 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2033 prev_packet->end_addr))
2037 * ETMv4 has 5 bits for exception number; if the numbers
2038 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
2039 * they are implementation defined exceptions.
2041 * For this case, simply take it as sync exception.
2043 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
2044 packet->exception_number <= CS_ETMV4_EXC_END)
2051 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
2052 struct cs_etm_traceid_queue *tidq)
2054 struct cs_etm_packet *packet = tidq->packet;
2055 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2056 u8 trace_chan_id = tidq->trace_chan_id;
2060 switch (packet->sample_type) {
2063 * Immediate branch instruction without neither link nor
2064 * return flag, it's normal branch instruction within
2067 if (packet->last_instr_type == OCSD_INSTR_BR &&
2068 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
2069 packet->flags = PERF_IP_FLAG_BRANCH;
2071 if (packet->last_instr_cond)
2072 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
2076 * Immediate branch instruction with link (e.g. BL), this is
2077 * branch instruction for function call.
2079 if (packet->last_instr_type == OCSD_INSTR_BR &&
2080 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2081 packet->flags = PERF_IP_FLAG_BRANCH |
2085 * Indirect branch instruction with link (e.g. BLR), this is
2086 * branch instruction for function call.
2088 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2089 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2090 packet->flags = PERF_IP_FLAG_BRANCH |
2094 * Indirect branch instruction with subtype of
2095 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
2096 * function return for A32/T32.
2098 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2099 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
2100 packet->flags = PERF_IP_FLAG_BRANCH |
2101 PERF_IP_FLAG_RETURN;
2104 * Indirect branch instruction without link (e.g. BR), usually
2105 * this is used for function return, especially for functions
2106 * within dynamic link lib.
2108 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2109 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
2110 packet->flags = PERF_IP_FLAG_BRANCH |
2111 PERF_IP_FLAG_RETURN;
2113 /* Return instruction for function return. */
2114 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2115 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
2116 packet->flags = PERF_IP_FLAG_BRANCH |
2117 PERF_IP_FLAG_RETURN;
2120 * Decoder might insert a discontinuity in the middle of
2121 * instruction packets, fixup prev_packet with flag
2122 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
2124 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
2125 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2126 PERF_IP_FLAG_TRACE_BEGIN;
2129 * If the previous packet is an exception return packet
2130 * and the return address just follows SVC instruction,
2131 * it needs to calibrate the previous packet sample flags
2132 * as PERF_IP_FLAG_SYSCALLRET.
2134 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
2135 PERF_IP_FLAG_RETURN |
2136 PERF_IP_FLAG_INTERRUPT) &&
2137 cs_etm__is_svc_instr(etmq, trace_chan_id,
2138 packet, packet->start_addr))
2139 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2140 PERF_IP_FLAG_RETURN |
2141 PERF_IP_FLAG_SYSCALLRET;
2143 case CS_ETM_DISCONTINUITY:
2145 * The trace is discontinuous, if the previous packet is
2146 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
2147 * for previous packet.
2149 if (prev_packet->sample_type == CS_ETM_RANGE)
2150 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2151 PERF_IP_FLAG_TRACE_END;
2153 case CS_ETM_EXCEPTION:
2154 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
2158 /* The exception is for system call. */
2159 if (cs_etm__is_syscall(etmq, tidq, magic))
2160 packet->flags = PERF_IP_FLAG_BRANCH |
2162 PERF_IP_FLAG_SYSCALLRET;
2164 * The exceptions are triggered by external signals from bus,
2165 * interrupt controller, debug module, PE reset or halt.
2167 else if (cs_etm__is_async_exception(tidq, magic))
2168 packet->flags = PERF_IP_FLAG_BRANCH |
2170 PERF_IP_FLAG_ASYNC |
2171 PERF_IP_FLAG_INTERRUPT;
2173 * Otherwise, exception is caused by trap, instruction &
2174 * data fault, or alignment errors.
2176 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
2177 packet->flags = PERF_IP_FLAG_BRANCH |
2179 PERF_IP_FLAG_INTERRUPT;
2182 * When the exception packet is inserted, since exception
2183 * packet is not used standalone for generating samples
2184 * and it's affiliation to the previous instruction range
2185 * packet; so set previous range packet flags to tell perf
2186 * it is an exception taken branch.
2188 if (prev_packet->sample_type == CS_ETM_RANGE)
2189 prev_packet->flags = packet->flags;
2191 case CS_ETM_EXCEPTION_RET:
2193 * When the exception return packet is inserted, since
2194 * exception return packet is not used standalone for
2195 * generating samples and it's affiliation to the previous
2196 * instruction range packet; so set previous range packet
2197 * flags to tell perf it is an exception return branch.
2199 * The exception return can be for either system call or
2200 * other exception types; unfortunately the packet doesn't
2201 * contain exception type related info so we cannot decide
2202 * the exception type purely based on exception return packet.
2203 * If we record the exception number from exception packet and
2204 * reuse it for exception return packet, this is not reliable
2205 * due the trace can be discontinuity or the interrupt can
2206 * be nested, thus the recorded exception number cannot be
2207 * used for exception return packet for these two cases.
2209 * For exception return packet, we only need to distinguish the
2210 * packet is for system call or for other types. Thus the
2211 * decision can be deferred when receive the next packet which
2212 * contains the return address, based on the return address we
2213 * can read out the previous instruction and check if it's a
2214 * system call instruction and then calibrate the sample flag
2217 if (prev_packet->sample_type == CS_ETM_RANGE)
2218 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2219 PERF_IP_FLAG_RETURN |
2220 PERF_IP_FLAG_INTERRUPT;
2230 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2233 size_t processed = 0;
2236 * Packets are decoded and added to the decoder's packet queue
2237 * until the decoder packet processing callback has requested that
2238 * processing stops or there is nothing left in the buffer. Normal
2239 * operations that stop processing are a timestamp packet or a full
2240 * decoder buffer queue.
2242 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2244 &etmq->buf[etmq->buf_used],
2250 etmq->offset += processed;
2251 etmq->buf_used += processed;
2252 etmq->buf_len -= processed;
2258 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2259 struct cs_etm_traceid_queue *tidq)
2262 struct cs_etm_packet_queue *packet_queue;
2264 packet_queue = &tidq->packet_queue;
2266 /* Process each packet in this chunk */
2268 ret = cs_etm_decoder__get_packet(packet_queue,
2272 * Stop processing this chunk on
2273 * end of data or error
2278 * Since packet addresses are swapped in packet
2279 * handling within below switch() statements,
2280 * thus setting sample flags must be called
2281 * prior to switch() statement to use address
2282 * information before packets swapping.
2284 ret = cs_etm__set_sample_flags(etmq, tidq);
2288 switch (tidq->packet->sample_type) {
2291 * If the packet contains an instruction
2292 * range, generate instruction sequence
2295 cs_etm__sample(etmq, tidq);
2297 case CS_ETM_EXCEPTION:
2298 case CS_ETM_EXCEPTION_RET:
2300 * If the exception packet is coming,
2301 * make sure the previous instruction
2302 * range packet to be handled properly.
2304 cs_etm__exception(tidq);
2306 case CS_ETM_DISCONTINUITY:
2308 * Discontinuity in trace, flush
2309 * previous branch stack
2311 cs_etm__flush(etmq, tidq);
2315 * Should not receive empty packet,
2318 pr_err("CS ETM Trace: empty packet\n");
2328 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2331 struct int_node *inode;
2332 struct cs_etm_traceid_queue *tidq;
2333 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2335 intlist__for_each_entry(inode, traceid_queues_list) {
2336 idx = (int)(intptr_t)inode->priv;
2337 tidq = etmq->traceid_queues[idx];
2339 /* Ignore return value */
2340 cs_etm__process_traceid_queue(etmq, tidq);
2343 * Generate an instruction sample with the remaining
2344 * branchstack entries.
2346 cs_etm__flush(etmq, tidq);
2350 static int cs_etm__run_per_thread_timeless_decoder(struct cs_etm_queue *etmq)
2353 struct cs_etm_traceid_queue *tidq;
2355 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2359 /* Go through each buffer in the queue and decode them one by one */
2361 err = cs_etm__get_data_block(etmq);
2365 /* Run trace decoder until buffer consumed or end of trace */
2367 err = cs_etm__decode_data_block(etmq);
2372 * Process each packet in this chunk, nothing to do if
2373 * an error occurs other than hoping the next one will
2376 err = cs_etm__process_traceid_queue(etmq, tidq);
2378 } while (etmq->buf_len);
2381 /* Flush any remaining branch stack entries */
2382 err = cs_etm__end_block(etmq, tidq);
2388 static int cs_etm__run_per_cpu_timeless_decoder(struct cs_etm_queue *etmq)
2391 struct cs_etm_traceid_queue *tidq;
2392 struct int_node *inode;
2394 /* Go through each buffer in the queue and decode them one by one */
2396 err = cs_etm__get_data_block(etmq);
2400 /* Run trace decoder until buffer consumed or end of trace */
2402 err = cs_etm__decode_data_block(etmq);
2407 * cs_etm__run_per_thread_timeless_decoder() runs on a
2408 * single traceID queue because each TID has a separate
2409 * buffer. But here in per-cpu mode we need to iterate
2410 * over each channel instead.
2412 intlist__for_each_entry(inode,
2413 etmq->traceid_queues_list) {
2414 idx = (int)(intptr_t)inode->priv;
2415 tidq = etmq->traceid_queues[idx];
2416 cs_etm__process_traceid_queue(etmq, tidq);
2418 } while (etmq->buf_len);
2420 intlist__for_each_entry(inode, etmq->traceid_queues_list) {
2421 idx = (int)(intptr_t)inode->priv;
2422 tidq = etmq->traceid_queues[idx];
2423 /* Flush any remaining branch stack entries */
2424 err = cs_etm__end_block(etmq, tidq);
2433 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2437 struct auxtrace_queues *queues = &etm->queues;
2439 for (i = 0; i < queues->nr_queues; i++) {
2440 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2441 struct cs_etm_queue *etmq = queue->priv;
2442 struct cs_etm_traceid_queue *tidq;
2448 * Per-cpu mode has contextIDs in the trace and the decoder
2449 * calls cs_etm__set_pid_tid_cpu() automatically so no need
2452 if (etm->per_thread_decoding) {
2453 tidq = cs_etm__etmq_get_traceid_queue(
2454 etmq, CS_ETM_PER_THREAD_TRACEID);
2459 if ((tid == -1) || (tidq->tid == tid)) {
2460 cs_etm__set_pid_tid_cpu(etm, tidq);
2461 cs_etm__run_per_thread_timeless_decoder(etmq);
2464 cs_etm__run_per_cpu_timeless_decoder(etmq);
2470 static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
2473 unsigned int cs_queue_nr, queue_nr, i;
2476 struct auxtrace_queue *queue;
2477 struct cs_etm_queue *etmq;
2478 struct cs_etm_traceid_queue *tidq;
2481 * Pre-populate the heap with one entry from each queue so that we can
2482 * start processing in time order across all queues.
2484 for (i = 0; i < etm->queues.nr_queues; i++) {
2485 etmq = etm->queues.queue_array[i].priv;
2489 ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2495 if (!etm->heap.heap_cnt)
2498 /* Take the entry at the top of the min heap */
2499 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2500 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2501 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2502 queue = &etm->queues.queue_array[queue_nr];
2506 * Remove the top entry from the heap since we are about
2509 auxtrace_heap__pop(&etm->heap);
2511 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2514 * No traceID queue has been allocated for this traceID,
2515 * which means something somewhere went very wrong. No
2516 * other choice than simply exit.
2523 * Packets associated with this timestamp are already in
2524 * the etmq's traceID queue, so process them.
2526 ret = cs_etm__process_traceid_queue(etmq, tidq);
2531 * Packets for this timestamp have been processed, time to
2532 * move on to the next timestamp, fetching a new auxtrace_buffer
2536 ret = cs_etm__get_data_block(etmq);
2541 * No more auxtrace_buffers to process in this etmq, simply
2542 * move on to another entry in the auxtrace_heap.
2547 ret = cs_etm__decode_data_block(etmq);
2551 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2553 if (!cs_timestamp) {
2555 * Function cs_etm__decode_data_block() returns when
2556 * there is no more traces to decode in the current
2557 * auxtrace_buffer OR when a timestamp has been
2558 * encountered on any of the traceID queues. Since we
2559 * did not get a timestamp, there is no more traces to
2560 * process in this auxtrace_buffer. As such empty and
2561 * flush all traceID queues.
2563 cs_etm__clear_all_traceid_queues(etmq);
2565 /* Fetch another auxtrace_buffer for this etmq */
2570 * Add to the min heap the timestamp for packets that have
2571 * just been decoded. They will be processed and synthesized
2572 * during the next call to cs_etm__process_traceid_queue() for
2573 * this queue/traceID.
2575 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2576 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2583 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2584 union perf_event *event)
2588 if (etm->timeless_decoding)
2592 * Add the tid/pid to the log so that we can get a match when
2593 * we get a contextID from the decoder.
2595 th = machine__findnew_thread(etm->machine,
2596 event->itrace_start.pid,
2597 event->itrace_start.tid);
2606 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2607 union perf_event *event)
2610 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2613 * Context switch in per-thread mode are irrelevant since perf
2614 * will start/stop tracing as the process is scheduled.
2616 if (etm->timeless_decoding)
2620 * SWITCH_IN events carry the next process to be switched out while
2621 * SWITCH_OUT events carry the process to be switched in. As such
2622 * we don't care about IN events.
2628 * Add the tid/pid to the log so that we can get a match when
2629 * we get a contextID from the decoder.
2631 th = machine__findnew_thread(etm->machine,
2632 event->context_switch.next_prev_pid,
2633 event->context_switch.next_prev_tid);
2642 static int cs_etm__process_event(struct perf_session *session,
2643 union perf_event *event,
2644 struct perf_sample *sample,
2645 struct perf_tool *tool)
2647 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2648 struct cs_etm_auxtrace,
2654 if (!tool->ordered_events) {
2655 pr_err("CoreSight ETM Trace requires ordered events\n");
2659 switch (event->header.type) {
2660 case PERF_RECORD_EXIT:
2662 * Don't need to wait for cs_etm__flush_events() in per-thread mode to
2663 * start the decode because we know there will be no more trace from
2664 * this thread. All this does is emit samples earlier than waiting for
2665 * the flush in other modes, but with timestamps it makes sense to wait
2666 * for flush so that events from different threads are interleaved
2669 if (etm->per_thread_decoding && etm->timeless_decoding)
2670 return cs_etm__process_timeless_queues(etm,
2674 case PERF_RECORD_ITRACE_START:
2675 return cs_etm__process_itrace_start(etm, event);
2677 case PERF_RECORD_SWITCH_CPU_WIDE:
2678 return cs_etm__process_switch_cpu_wide(etm, event);
2680 case PERF_RECORD_AUX:
2682 * Record the latest kernel timestamp available in the header
2683 * for samples so that synthesised samples occur from this point
2686 if (sample->time && (sample->time != (u64)-1))
2687 etm->latest_kernel_timestamp = sample->time;
2697 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2698 struct perf_record_auxtrace *event)
2700 struct auxtrace_buffer *buf;
2703 * Find all buffers with same reference in the queues and dump them.
2704 * This is because the queues can contain multiple entries of the same
2705 * buffer that were split on aux records.
2707 for (i = 0; i < etm->queues.nr_queues; ++i)
2708 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2709 if (buf->reference == event->reference)
2710 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2713 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2714 union perf_event *event,
2715 struct perf_tool *tool __maybe_unused)
2717 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2718 struct cs_etm_auxtrace,
2720 if (!etm->data_queued) {
2721 struct auxtrace_buffer *buffer;
2723 int fd = perf_data__fd(session->data);
2724 bool is_pipe = perf_data__is_pipe(session->data);
2726 int idx = event->auxtrace.idx;
2731 data_offset = lseek(fd, 0, SEEK_CUR);
2732 if (data_offset == -1)
2736 err = auxtrace_queues__add_event(&etm->queues, session,
2737 event, data_offset, &buffer);
2742 * Knowing if the trace is formatted or not requires a lookup of
2743 * the aux record so only works in non-piped mode where data is
2744 * queued in cs_etm__queue_aux_records(). Always assume
2745 * formatted in piped mode (true).
2747 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2753 if (auxtrace_buffer__get_data(buffer, fd)) {
2754 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2755 auxtrace_buffer__put_data(buffer);
2757 } else if (dump_trace)
2758 dump_queued_data(etm, &event->auxtrace);
2763 static int cs_etm__setup_timeless_decoding(struct cs_etm_auxtrace *etm)
2765 struct evsel *evsel;
2766 struct evlist *evlist = etm->session->evlist;
2768 /* Override timeless mode with user input from --itrace=Z */
2769 if (etm->synth_opts.timeless_decoding) {
2770 etm->timeless_decoding = true;
2775 * Find the cs_etm evsel and look at what its timestamp setting was
2777 evlist__for_each_entry(evlist, evsel)
2778 if (cs_etm__evsel_is_auxtrace(etm->session, evsel)) {
2779 etm->timeless_decoding =
2780 !(evsel->core.attr.config & BIT(ETM_OPT_TS));
2784 pr_err("CS ETM: Couldn't find ETM evsel\n");
2789 * Read a single cpu parameter block from the auxtrace_info priv block.
2791 * For version 1 there is a per cpu nr_params entry. If we are handling
2792 * version 1 file, then there may be less, the same, or more params
2793 * indicated by this value than the compile time number we understand.
2795 * For a version 0 info block, there are a fixed number, and we need to
2796 * fill out the nr_param value in the metadata we create.
2798 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2799 int out_blk_size, int nr_params_v0)
2801 u64 *metadata = NULL;
2803 int nr_in_params, nr_out_params, nr_cmn_params;
2806 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2810 /* read block current index & version */
2811 i = *buff_in_offset;
2812 hdr_version = buff_in[CS_HEADER_VERSION];
2815 /* read version 0 info block into a version 1 metadata block */
2816 nr_in_params = nr_params_v0;
2817 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2818 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2819 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2820 /* remaining block params at offset +1 from source */
2821 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2822 metadata[k + 1] = buff_in[i + k];
2823 /* version 0 has 2 common params */
2826 /* read version 1 info block - input and output nr_params may differ */
2827 /* version 1 has 3 common params */
2829 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2831 /* if input has more params than output - skip excess */
2832 nr_out_params = nr_in_params + nr_cmn_params;
2833 if (nr_out_params > out_blk_size)
2834 nr_out_params = out_blk_size;
2836 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2837 metadata[k] = buff_in[i + k];
2839 /* record the actual nr params we copied */
2840 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2843 /* adjust in offset by number of in params used */
2844 i += nr_in_params + nr_cmn_params;
2845 *buff_in_offset = i;
2850 * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2851 * on the bounds of aux_event, if it matches with the buffer that's at
2854 * Normally, whole auxtrace buffers would be added to the queue. But we
2855 * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2856 * is reset across each buffer, so splitting the buffers up in advance has
2859 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2860 struct perf_record_aux *aux_event, struct perf_sample *sample)
2863 char buf[PERF_SAMPLE_MAX_SIZE];
2864 union perf_event *auxtrace_event_union;
2865 struct perf_record_auxtrace *auxtrace_event;
2866 union perf_event auxtrace_fragment;
2867 __u64 aux_offset, aux_size;
2871 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2872 struct cs_etm_auxtrace,
2876 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2877 * from looping through the auxtrace index.
2879 err = perf_session__peek_event(session, file_offset, buf,
2880 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2883 auxtrace_event = &auxtrace_event_union->auxtrace;
2884 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2887 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2888 auxtrace_event->header.size != sz) {
2893 * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
2894 * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a
2895 * CPU as we set this always for the AUX_OUTPUT_HW_ID event.
2896 * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
2897 * Return 'not found' if mismatch.
2899 if (auxtrace_event->cpu == (__u32) -1) {
2900 etm->per_thread_decoding = true;
2901 if (auxtrace_event->tid != sample->tid)
2903 } else if (auxtrace_event->cpu != sample->cpu) {
2904 if (etm->per_thread_decoding) {
2906 * Found a per-cpu buffer after a per-thread one was
2909 pr_err("CS ETM: Inconsistent per-thread/per-cpu mode.\n");
2915 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2917 * Clamp size in snapshot mode. The buffer size is clamped in
2918 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
2921 aux_size = min(aux_event->aux_size, auxtrace_event->size);
2924 * In this mode, the head also points to the end of the buffer so aux_offset
2925 * needs to have the size subtracted so it points to the beginning as in normal mode
2927 aux_offset = aux_event->aux_offset - aux_size;
2929 aux_size = aux_event->aux_size;
2930 aux_offset = aux_event->aux_offset;
2933 if (aux_offset >= auxtrace_event->offset &&
2934 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
2936 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
2937 * based on the sizes of the aux event, and queue that fragment.
2939 auxtrace_fragment.auxtrace = *auxtrace_event;
2940 auxtrace_fragment.auxtrace.size = aux_size;
2941 auxtrace_fragment.auxtrace.offset = aux_offset;
2942 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
2944 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
2945 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
2946 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
2951 idx = auxtrace_event->idx;
2952 formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
2953 return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2957 /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
2961 static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event,
2962 u64 offset __maybe_unused, void *data __maybe_unused)
2964 /* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */
2965 if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
2966 (*(int *)data)++; /* increment found count */
2967 return cs_etm__process_aux_output_hw_id(session, event);
2972 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
2973 u64 offset __maybe_unused, void *data __maybe_unused)
2975 struct perf_sample sample;
2977 struct auxtrace_index_entry *ent;
2978 struct auxtrace_index *auxtrace_index;
2979 struct evsel *evsel;
2982 /* Don't care about any other events, we're only queuing buffers for AUX events */
2983 if (event->header.type != PERF_RECORD_AUX)
2986 if (event->header.size < sizeof(struct perf_record_aux))
2989 /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
2990 if (!event->aux.aux_size)
2994 * Parse the sample, we need the sample_id_all data that comes after the event so that the
2995 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
2997 evsel = evlist__event2evsel(session->evlist, event);
3000 ret = evsel__parse_sample(evsel, event, &sample);
3005 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
3007 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
3008 for (i = 0; i < auxtrace_index->nr; i++) {
3009 ent = &auxtrace_index->entries[i];
3010 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
3011 ent->sz, &event->aux, &sample);
3013 * Stop search on error or successful values. Continue search on
3022 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
3023 * don't exit with an error because it will still be possible to decode other aux records.
3025 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
3026 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
3030 static int cs_etm__queue_aux_records(struct perf_session *session)
3032 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
3033 struct auxtrace_index, list);
3034 if (index && index->nr > 0)
3035 return perf_session__peek_events(session, session->header.data_offset,
3036 session->header.data_size,
3037 cs_etm__queue_aux_records_cb, NULL);
3040 * We would get here if there are no entries in the index (either no auxtrace
3041 * buffers or no index at all). Fail silently as there is the possibility of
3042 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
3045 * In that scenario, buffers will not be split by AUX records.
3050 #define HAS_PARAM(j, type, param) (metadata[(j)][CS_ETM_NR_TRC_PARAMS] <= \
3051 (CS_##type##_##param - CS_ETM_COMMON_BLK_MAX_V1))
3054 * Loop through the ETMs and complain if we find at least one where ts_source != 1 (virtual
3057 static bool cs_etm__has_virtual_ts(u64 **metadata, int num_cpu)
3061 for (j = 0; j < num_cpu; j++) {
3062 switch (metadata[j][CS_ETM_MAGIC]) {
3063 case __perf_cs_etmv4_magic:
3064 if (HAS_PARAM(j, ETMV4, TS_SOURCE) || metadata[j][CS_ETMV4_TS_SOURCE] != 1)
3067 case __perf_cs_ete_magic:
3068 if (HAS_PARAM(j, ETE, TS_SOURCE) || metadata[j][CS_ETE_TS_SOURCE] != 1)
3072 /* Unknown / unsupported magic number. */
3079 /* map trace ids to correct metadata block, from information in metadata */
3080 static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata)
3086 for (i = 0; i < num_cpu; i++) {
3087 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3088 switch (cs_etm_magic) {
3089 case __perf_cs_etmv3_magic:
3090 metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3091 trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]);
3093 case __perf_cs_etmv4_magic:
3094 case __perf_cs_ete_magic:
3095 metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3096 trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]);
3099 /* unknown magic number */
3102 err = cs_etm__map_trace_id(trace_chan_id, metadata[i]);
3110 * If we found AUX_HW_ID packets, then set any metadata marked as unused to the
3111 * unused value to reduce the number of unneeded decoders created.
3113 static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata)
3118 for (i = 0; i < num_cpu; i++) {
3119 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3120 switch (cs_etm_magic) {
3121 case __perf_cs_etmv3_magic:
3122 if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3123 metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3125 case __perf_cs_etmv4_magic:
3126 case __perf_cs_ete_magic:
3127 if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3128 metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3131 /* unknown magic number */
3138 int cs_etm__process_auxtrace_info_full(union perf_event *event,
3139 struct perf_session *session)
3141 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3142 struct cs_etm_auxtrace *etm = NULL;
3143 struct perf_record_time_conv *tc = &session->time_conv;
3144 int event_header_size = sizeof(struct perf_event_header);
3145 int total_size = auxtrace_info->header.size;
3149 int aux_hw_id_found;
3152 u64 **metadata = NULL;
3155 * Create an RB tree for traceID-metadata tuple. Since the conversion
3156 * has to be made for each packet that gets decoded, optimizing access
3157 * in anything other than a sequential array is worth doing.
3159 traceid_list = intlist__new(NULL);
3163 /* First the global part */
3164 ptr = (u64 *) auxtrace_info->priv;
3165 num_cpu = ptr[CS_PMU_TYPE_CPUS] & 0xffffffff;
3166 metadata = zalloc(sizeof(*metadata) * num_cpu);
3169 goto err_free_traceid_list;
3172 /* Start parsing after the common part of the header */
3173 i = CS_HEADER_VERSION_MAX;
3176 * The metadata is stored in the auxtrace_info section and encodes
3177 * the configuration of the ARM embedded trace macrocell which is
3178 * required by the trace decoder to properly decode the trace due
3179 * to its highly compressed nature.
3181 for (j = 0; j < num_cpu; j++) {
3182 if (ptr[i] == __perf_cs_etmv3_magic) {
3184 cs_etm__create_meta_blk(ptr, &i,
3186 CS_ETM_NR_TRC_PARAMS_V0);
3187 } else if (ptr[i] == __perf_cs_etmv4_magic) {
3189 cs_etm__create_meta_blk(ptr, &i,
3191 CS_ETMV4_NR_TRC_PARAMS_V0);
3192 } else if (ptr[i] == __perf_cs_ete_magic) {
3193 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
3195 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
3198 goto err_free_metadata;
3203 goto err_free_metadata;
3208 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
3209 * CS_ETMV4_PRIV_MAX mark how many double words are in the
3210 * global metadata, and each cpu's metadata respectively.
3211 * The following tests if the correct number of double words was
3212 * present in the auxtrace info section.
3214 priv_size = total_size - event_header_size - INFO_HEADER_SIZE;
3215 if (i * 8 != priv_size) {
3217 goto err_free_metadata;
3220 etm = zalloc(sizeof(*etm));
3224 goto err_free_metadata;
3227 err = auxtrace_queues__init(&etm->queues);
3231 if (session->itrace_synth_opts->set) {
3232 etm->synth_opts = *session->itrace_synth_opts;
3234 itrace_synth_opts__set_default(&etm->synth_opts,
3235 session->itrace_synth_opts->default_no_sample);
3236 etm->synth_opts.callchain = false;
3239 etm->session = session;
3240 etm->machine = &session->machines.host;
3242 etm->num_cpu = num_cpu;
3243 etm->pmu_type = (unsigned int) ((ptr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
3244 etm->snapshot_mode = (ptr[CS_ETM_SNAPSHOT] != 0);
3245 etm->metadata = metadata;
3246 etm->auxtrace_type = auxtrace_info->type;
3248 /* Use virtual timestamps if all ETMs report ts_source = 1 */
3249 etm->has_virtual_ts = cs_etm__has_virtual_ts(metadata, num_cpu);
3251 if (!etm->has_virtual_ts)
3252 ui__warning("Virtual timestamps are not enabled, or not supported by the traced system.\n"
3253 "The time field of the samples will not be set accurately.\n\n");
3255 etm->auxtrace.process_event = cs_etm__process_event;
3256 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3257 etm->auxtrace.flush_events = cs_etm__flush_events;
3258 etm->auxtrace.free_events = cs_etm__free_events;
3259 etm->auxtrace.free = cs_etm__free;
3260 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3261 session->auxtrace = &etm->auxtrace;
3263 err = cs_etm__setup_timeless_decoding(etm);
3267 etm->unknown_thread = thread__new(999999999, 999999999);
3268 if (!etm->unknown_thread) {
3270 goto err_free_queues;
3274 * Initialize list node so that at thread__zput() we can avoid
3275 * segmentation fault at list_del_init().
3277 INIT_LIST_HEAD(&etm->unknown_thread->node);
3279 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
3281 goto err_delete_thread;
3283 if (thread__init_maps(etm->unknown_thread, etm->machine)) {
3285 goto err_delete_thread;
3288 etm->tc.time_shift = tc->time_shift;
3289 etm->tc.time_mult = tc->time_mult;
3290 etm->tc.time_zero = tc->time_zero;
3291 if (event_contains(*tc, time_cycles)) {
3292 etm->tc.time_cycles = tc->time_cycles;
3293 etm->tc.time_mask = tc->time_mask;
3294 etm->tc.cap_user_time_zero = tc->cap_user_time_zero;
3295 etm->tc.cap_user_time_short = tc->cap_user_time_short;
3297 err = cs_etm__synth_events(etm, session);
3299 goto err_delete_thread;
3302 * Map Trace ID values to CPU metadata.
3304 * Trace metadata will always contain Trace ID values from the legacy algorithm. If the
3305 * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata
3306 * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set.
3308 * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use
3309 * the same IDs as the old algorithm as far as is possible, unless there are clashes
3310 * in which case a different value will be used. This means an older perf may still
3311 * be able to record and read files generate on a newer system.
3313 * For a perf able to interpret AUX_HW_ID packets we first check for the presence of
3314 * those packets. If they are there then the values will be mapped and plugged into
3315 * the metadata. We then set any remaining metadata values with the used flag to a
3316 * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required.
3318 * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3319 * then we map Trace ID values to CPU directly from the metadata - clearing any unused
3323 /* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */
3324 aux_hw_id_found = 0;
3325 err = perf_session__peek_events(session, session->header.data_offset,
3326 session->header.data_size,
3327 cs_etm__process_aux_hw_id_cb, &aux_hw_id_found);
3329 goto err_delete_thread;
3331 /* if HW ID found then clear any unused metadata ID values */
3332 if (aux_hw_id_found)
3333 err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata);
3334 /* otherwise, this is a file with metadata values only, map from metadata */
3336 err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3339 goto err_delete_thread;
3341 err = cs_etm__queue_aux_records(session);
3343 goto err_delete_thread;
3345 etm->data_queued = etm->queues.populated;
3349 thread__zput(etm->unknown_thread);
3351 auxtrace_queues__free(&etm->queues);
3352 session->auxtrace = NULL;
3356 /* No need to check @metadata[j], free(NULL) is supported */
3357 for (j = 0; j < num_cpu; j++)
3358 zfree(&metadata[j]);
3360 err_free_traceid_list:
3361 intlist__delete(traceid_list);