1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
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>
22 #include "cs-etm-decoder/cs-etm-decoder.h"
31 #include "map_symbol.h"
36 #include "thread-stack.h"
38 #include <tools/libc_compat.h>
39 #include "util/synthetic-events.h"
40 #include "util/util.h"
42 struct cs_etm_auxtrace {
43 struct auxtrace auxtrace;
44 struct auxtrace_queues queues;
45 struct auxtrace_heap heap;
46 struct itrace_synth_opts synth_opts;
47 struct perf_session *session;
48 struct perf_tsc_conversion tc;
51 * Timeless has no timestamps in the trace so overlapping mmap lookups
52 * are less accurate but produces smaller trace data. We use context IDs
53 * in the trace instead of matching timestamps with fork records so
54 * they're not really needed in the general case. Overlapping mmaps
55 * happen in cases like between a fork and an exec.
57 bool timeless_decoding;
60 * Per-thread ignores the trace channel ID and instead assumes that
61 * everything in a buffer comes from the same process regardless of
62 * which CPU it ran on. It also implies no context IDs so the TID is
63 * taken from the auxtrace buffer.
65 bool per_thread_decoding;
68 bool has_virtual_ts; /* Virtual/Kernel timestamps in the trace. */
71 u64 latest_kernel_timestamp;
73 u64 branches_sample_type;
75 u64 instructions_sample_type;
76 u64 instructions_sample_period;
79 unsigned int pmu_type;
80 enum cs_etm_pid_fmt pid_fmt;
83 struct cs_etm_traceid_queue {
85 u64 period_instructions;
86 size_t last_branch_pos;
87 union perf_event *event_buf;
88 struct thread *thread;
89 struct thread *prev_packet_thread;
90 ocsd_ex_level prev_packet_el;
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 as an enum:
177 * CS_ETM_PIDFMT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced.
178 * CS_ETM_PIDFMT_CTXTID2: CONTEXTIDR_EL2 is traced.
179 * CS_ETM_PIDFMT_NONE: No context IDs
181 * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
182 * are enabled at the same time when the session runs on an EL2 kernel.
183 * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
184 * recorded in the trace data, the tool will selectively use
185 * CONTEXTIDR_EL2 as PID.
187 * The result is cached in etm->pid_fmt so this function only needs to be called
188 * when processing the aux info.
190 static enum cs_etm_pid_fmt cs_etm__init_pid_fmt(u64 *metadata)
194 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
195 val = metadata[CS_ETM_ETMCR];
196 /* CONTEXTIDR is traced */
197 if (val & BIT(ETM_OPT_CTXTID))
198 return CS_ETM_PIDFMT_CTXTID;
200 val = metadata[CS_ETMV4_TRCCONFIGR];
201 /* CONTEXTIDR_EL2 is traced */
202 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
203 return CS_ETM_PIDFMT_CTXTID2;
204 /* CONTEXTIDR_EL1 is traced */
205 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
206 return CS_ETM_PIDFMT_CTXTID;
209 return CS_ETM_PIDFMT_NONE;
212 enum cs_etm_pid_fmt cs_etm__get_pid_fmt(struct cs_etm_queue *etmq)
214 return etmq->etm->pid_fmt;
217 static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
219 struct int_node *inode;
221 /* Get an RB node for this CPU */
222 inode = intlist__findnew(traceid_list, trace_chan_id);
224 /* Something went wrong, no need to continue */
229 * The node for that CPU should not be taken.
230 * Back out if that's the case.
235 /* All good, associate the traceID with the metadata pointer */
236 inode->priv = cpu_metadata;
241 static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata)
243 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
245 switch (cs_etm_magic) {
246 case __perf_cs_etmv3_magic:
247 *trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] &
248 CORESIGHT_TRACE_ID_VAL_MASK);
250 case __perf_cs_etmv4_magic:
251 case __perf_cs_ete_magic:
252 *trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] &
253 CORESIGHT_TRACE_ID_VAL_MASK);
262 * update metadata trace ID from the value found in the AUX_HW_INFO packet.
263 * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present.
265 static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
267 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
269 switch (cs_etm_magic) {
270 case __perf_cs_etmv3_magic:
271 cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id;
273 case __perf_cs_etmv4_magic:
274 case __perf_cs_ete_magic:
275 cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id;
285 * FIELD_GET (linux/bitfield.h) not available outside kernel code,
286 * and the header contains too many dependencies to just copy over,
287 * so roll our own based on the original
289 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
290 #define FIELD_GET(_mask, _reg) \
292 (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
296 * Get a metadata for a specific cpu from an array.
299 static u64 *get_cpu_data(struct cs_etm_auxtrace *etm, int cpu)
302 u64 *metadata = NULL;
304 for (i = 0; i < etm->num_cpu; i++) {
305 if (etm->metadata[i][CS_ETM_CPU] == (u64)cpu) {
306 metadata = etm->metadata[i];
315 * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event.
317 * The payload associates the Trace ID and the CPU.
318 * The routine is tolerant of seeing multiple packets with the same association,
319 * but a CPU / Trace ID association changing during a session is an error.
321 static int cs_etm__process_aux_output_hw_id(struct perf_session *session,
322 union perf_event *event)
324 struct cs_etm_auxtrace *etm;
325 struct perf_sample sample;
326 struct int_node *inode;
330 int cpu, version, err;
331 u8 trace_chan_id, curr_chan_id;
333 /* extract and parse the HW ID */
334 hw_id = event->aux_output_hw_id.hw_id;
335 version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id);
336 trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id);
338 /* check that we can handle this version */
339 if (version > CS_AUX_HW_ID_CURR_VERSION)
342 /* get access to the etm metadata */
343 etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
344 if (!etm || !etm->metadata)
347 /* parse the sample to get the CPU */
348 evsel = evlist__event2evsel(session->evlist, event);
351 err = evsel__parse_sample(evsel, event, &sample);
356 /* no CPU in the sample - possibly recorded with an old version of perf */
357 pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record.");
361 /* See if the ID is mapped to a CPU, and it matches the current CPU */
362 inode = intlist__find(traceid_list, trace_chan_id);
364 cpu_data = inode->priv;
365 if ((int)cpu_data[CS_ETM_CPU] != cpu) {
366 pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n");
370 /* check that the mapped ID matches */
371 err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data);
374 if (curr_chan_id != trace_chan_id) {
375 pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n");
379 /* mapped and matched - return OK */
383 cpu_data = get_cpu_data(etm, cpu);
384 if (cpu_data == NULL)
387 /* not one we've seen before - lets map it */
388 err = cs_etm__map_trace_id(trace_chan_id, cpu_data);
393 * if we are picking up the association from the packet, need to plug
394 * the correct trace ID into the metadata for setting up decoders later.
396 err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data);
400 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
404 * When a timestamp packet is encountered the backend code
405 * is stopped so that the front end has time to process packets
406 * that were accumulated in the traceID queue. Since there can
407 * be more than one channel per cs_etm_queue, we need to specify
408 * what traceID queue needs servicing.
410 etmq->pending_timestamp_chan_id = trace_chan_id;
413 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
416 struct cs_etm_packet_queue *packet_queue;
418 if (!etmq->pending_timestamp_chan_id)
422 *trace_chan_id = etmq->pending_timestamp_chan_id;
424 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
425 etmq->pending_timestamp_chan_id);
429 /* Acknowledge pending status */
430 etmq->pending_timestamp_chan_id = 0;
432 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
433 return packet_queue->cs_timestamp;
436 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
442 queue->packet_count = 0;
443 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
444 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
445 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
446 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
447 queue->packet_buffer[i].instr_count = 0;
448 queue->packet_buffer[i].last_instr_taken_branch = false;
449 queue->packet_buffer[i].last_instr_size = 0;
450 queue->packet_buffer[i].last_instr_type = 0;
451 queue->packet_buffer[i].last_instr_subtype = 0;
452 queue->packet_buffer[i].last_instr_cond = 0;
453 queue->packet_buffer[i].flags = 0;
454 queue->packet_buffer[i].exception_number = UINT32_MAX;
455 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
456 queue->packet_buffer[i].cpu = INT_MIN;
460 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
463 struct int_node *inode;
464 struct cs_etm_traceid_queue *tidq;
465 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
467 intlist__for_each_entry(inode, traceid_queues_list) {
468 idx = (int)(intptr_t)inode->priv;
469 tidq = etmq->traceid_queues[idx];
470 cs_etm__clear_packet_queue(&tidq->packet_queue);
474 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
475 struct cs_etm_traceid_queue *tidq,
479 struct auxtrace_queue *queue;
480 struct cs_etm_auxtrace *etm = etmq->etm;
482 cs_etm__clear_packet_queue(&tidq->packet_queue);
484 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
485 tidq->trace_chan_id = trace_chan_id;
486 tidq->el = tidq->prev_packet_el = ocsd_EL_unknown;
487 tidq->thread = machine__findnew_thread(&etm->session->machines.host, -1,
489 tidq->prev_packet_thread = machine__idle_thread(&etm->session->machines.host);
491 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
495 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
496 if (!tidq->prev_packet)
499 if (etm->synth_opts.last_branch) {
500 size_t sz = sizeof(struct branch_stack);
502 sz += etm->synth_opts.last_branch_sz *
503 sizeof(struct branch_entry);
504 tidq->last_branch = zalloc(sz);
505 if (!tidq->last_branch)
507 tidq->last_branch_rb = zalloc(sz);
508 if (!tidq->last_branch_rb)
512 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
513 if (!tidq->event_buf)
519 zfree(&tidq->last_branch_rb);
520 zfree(&tidq->last_branch);
521 zfree(&tidq->prev_packet);
522 zfree(&tidq->packet);
527 static struct cs_etm_traceid_queue
528 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
531 struct int_node *inode;
532 struct intlist *traceid_queues_list;
533 struct cs_etm_traceid_queue *tidq, **traceid_queues;
534 struct cs_etm_auxtrace *etm = etmq->etm;
536 if (etm->per_thread_decoding)
537 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
539 traceid_queues_list = etmq->traceid_queues_list;
542 * Check if the traceid_queue exist for this traceID by looking
545 inode = intlist__find(traceid_queues_list, trace_chan_id);
547 idx = (int)(intptr_t)inode->priv;
548 return etmq->traceid_queues[idx];
551 /* We couldn't find a traceid_queue for this traceID, allocate one */
552 tidq = malloc(sizeof(*tidq));
556 memset(tidq, 0, sizeof(*tidq));
558 /* Get a valid index for the new traceid_queue */
559 idx = intlist__nr_entries(traceid_queues_list);
560 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
561 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
565 /* Associate this traceID with this index */
566 inode->priv = (void *)(intptr_t)idx;
568 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
571 /* Grow the traceid_queues array by one unit */
572 traceid_queues = etmq->traceid_queues;
573 traceid_queues = reallocarray(traceid_queues,
575 sizeof(*traceid_queues));
578 * On failure reallocarray() returns NULL and the original block of
579 * memory is left untouched.
584 traceid_queues[idx] = tidq;
585 etmq->traceid_queues = traceid_queues;
587 return etmq->traceid_queues[idx];
591 * Function intlist__remove() removes the inode from the list
592 * and delete the memory associated to it.
594 intlist__remove(traceid_queues_list, inode);
600 struct cs_etm_packet_queue
601 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
603 struct cs_etm_traceid_queue *tidq;
605 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
607 return &tidq->packet_queue;
612 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
613 struct cs_etm_traceid_queue *tidq)
615 struct cs_etm_packet *tmp;
617 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
618 etm->synth_opts.instructions) {
620 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
621 * the next incoming packet.
623 * Threads and exception levels are also tracked for both the
624 * previous and current packets. This is because the previous
625 * packet is used for the 'from' IP for branch samples, so the
626 * thread at that time must also be assigned to that sample.
627 * Across discontinuity packets the thread can change, so by
628 * tracking the thread for the previous packet the branch sample
629 * will have the correct info.
632 tidq->packet = tidq->prev_packet;
633 tidq->prev_packet = tmp;
634 tidq->prev_packet_el = tidq->el;
635 thread__put(tidq->prev_packet_thread);
636 tidq->prev_packet_thread = thread__get(tidq->thread);
640 static void cs_etm__packet_dump(const char *pkt_string)
642 const char *color = PERF_COLOR_BLUE;
643 int len = strlen(pkt_string);
645 if (len && (pkt_string[len-1] == '\n'))
646 color_fprintf(stdout, color, " %s", pkt_string);
648 color_fprintf(stdout, color, " %s\n", pkt_string);
653 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
654 struct cs_etm_auxtrace *etm, int idx,
657 u64 **metadata = etm->metadata;
659 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
660 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
661 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
664 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
665 struct cs_etm_auxtrace *etm, int idx)
667 u64 **metadata = etm->metadata;
669 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
670 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
671 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
672 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
673 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
674 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
675 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
678 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
679 struct cs_etm_auxtrace *etm, int idx)
681 u64 **metadata = etm->metadata;
683 t_params[idx].protocol = CS_ETM_PROTO_ETE;
684 t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETE_TRCIDR0];
685 t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETE_TRCIDR1];
686 t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETE_TRCIDR2];
687 t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETE_TRCIDR8];
688 t_params[idx].ete.reg_configr = metadata[idx][CS_ETE_TRCCONFIGR];
689 t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETE_TRCTRACEIDR];
690 t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
693 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
694 struct cs_etm_auxtrace *etm,
701 for (i = 0; i < decoders; i++) {
702 architecture = etm->metadata[i][CS_ETM_MAGIC];
704 switch (architecture) {
705 case __perf_cs_etmv3_magic:
706 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
707 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
709 case __perf_cs_etmv4_magic:
710 cs_etm__set_trace_param_etmv4(t_params, etm, i);
712 case __perf_cs_ete_magic:
713 cs_etm__set_trace_param_ete(t_params, etm, i);
723 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
724 struct cs_etm_queue *etmq,
725 enum cs_etm_decoder_operation mode,
730 if (!(mode < CS_ETM_OPERATION_MAX))
733 d_params->packet_printer = cs_etm__packet_dump;
734 d_params->operation = mode;
735 d_params->data = etmq;
736 d_params->formatted = formatted;
737 d_params->fsyncs = false;
738 d_params->hsyncs = false;
739 d_params->frame_aligned = true;
746 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
747 struct auxtrace_buffer *buffer)
750 const char *color = PERF_COLOR_BLUE;
751 size_t buffer_used = 0;
753 fprintf(stdout, "\n");
754 color_fprintf(stdout, color,
755 ". ... CoreSight %s Trace data: size %#zx bytes\n",
756 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
761 ret = cs_etm_decoder__process_data_block(
762 etmq->decoder, buffer->offset,
763 &((u8 *)buffer->data)[buffer_used],
764 buffer->size - buffer_used, &consumed);
768 buffer_used += consumed;
769 } while (buffer_used < buffer->size);
771 cs_etm_decoder__reset(etmq->decoder);
774 static int cs_etm__flush_events(struct perf_session *session,
775 struct perf_tool *tool)
777 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
778 struct cs_etm_auxtrace,
783 if (!tool->ordered_events)
786 if (etm->timeless_decoding) {
788 * Pass tid = -1 to process all queues. But likely they will have
789 * already been processed on PERF_RECORD_EXIT anyway.
791 return cs_etm__process_timeless_queues(etm, -1);
794 return cs_etm__process_timestamped_queues(etm);
797 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
801 struct int_node *inode, *tmp;
802 struct cs_etm_traceid_queue *tidq;
803 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
805 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
806 priv = (uintptr_t)inode->priv;
809 /* Free this traceid_queue from the array */
810 tidq = etmq->traceid_queues[idx];
811 thread__zput(tidq->thread);
812 thread__zput(tidq->prev_packet_thread);
813 zfree(&tidq->event_buf);
814 zfree(&tidq->last_branch);
815 zfree(&tidq->last_branch_rb);
816 zfree(&tidq->prev_packet);
817 zfree(&tidq->packet);
821 * Function intlist__remove() removes the inode from the list
822 * and delete the memory associated to it.
824 intlist__remove(traceid_queues_list, inode);
827 /* Then the RB tree itself */
828 intlist__delete(traceid_queues_list);
829 etmq->traceid_queues_list = NULL;
831 /* finally free the traceid_queues array */
832 zfree(&etmq->traceid_queues);
835 static void cs_etm__free_queue(void *priv)
837 struct cs_etm_queue *etmq = priv;
842 cs_etm_decoder__free(etmq->decoder);
843 cs_etm__free_traceid_queues(etmq);
847 static void cs_etm__free_events(struct perf_session *session)
850 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
851 struct cs_etm_auxtrace,
853 struct auxtrace_queues *queues = &aux->queues;
855 for (i = 0; i < queues->nr_queues; i++) {
856 cs_etm__free_queue(queues->queue_array[i].priv);
857 queues->queue_array[i].priv = NULL;
860 auxtrace_queues__free(queues);
863 static void cs_etm__free(struct perf_session *session)
866 struct int_node *inode, *tmp;
867 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
868 struct cs_etm_auxtrace,
870 cs_etm__free_events(session);
871 session->auxtrace = NULL;
873 /* First remove all traceID/metadata nodes for the RB tree */
874 intlist__for_each_entry_safe(inode, tmp, traceid_list)
875 intlist__remove(traceid_list, inode);
876 /* Then the RB tree itself */
877 intlist__delete(traceid_list);
879 for (i = 0; i < aux->num_cpu; i++)
880 zfree(&aux->metadata[i]);
882 zfree(&aux->metadata);
886 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
889 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
890 struct cs_etm_auxtrace,
893 return evsel->core.attr.type == aux->pmu_type;
896 static struct machine *cs_etm__get_machine(struct cs_etm_queue *etmq,
899 enum cs_etm_pid_fmt pid_fmt = cs_etm__get_pid_fmt(etmq);
902 * For any virtualisation based on nVHE (e.g. pKVM), or host kernels
903 * running at EL1 assume everything is the host.
905 if (pid_fmt == CS_ETM_PIDFMT_CTXTID)
906 return &etmq->etm->session->machines.host;
909 * Not perfect, but otherwise assume anything in EL1 is the default
910 * guest, and everything else is the host. Distinguishing between guest
911 * and host userspaces isn't currently supported either. Neither is
912 * multiple guest support. All this does is reduce the likeliness of
913 * decode errors where we look into the host kernel maps when it should
914 * have been the guest maps.
918 return machines__find_guest(&etmq->etm->session->machines,
919 DEFAULT_GUEST_KERNEL_ID);
923 case ocsd_EL_unknown:
925 return &etmq->etm->session->machines.host;
929 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address,
932 struct machine *machine = cs_etm__get_machine(etmq, el);
934 if (address >= machine__kernel_start(machine)) {
935 if (machine__is_host(machine))
936 return PERF_RECORD_MISC_KERNEL;
938 return PERF_RECORD_MISC_GUEST_KERNEL;
940 if (machine__is_host(machine))
941 return PERF_RECORD_MISC_USER;
944 * Can't really happen at the moment because
945 * cs_etm__get_machine() will always return
946 * machines.host for any non EL1 trace.
948 return PERF_RECORD_MISC_GUEST_USER;
953 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
954 u64 address, size_t size, u8 *buffer,
955 const ocsd_mem_space_acc_t mem_space)
960 struct addr_location al;
962 struct cs_etm_traceid_queue *tidq;
968 addr_location__init(&al);
969 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
974 * We've already tracked EL along side the PID in cs_etm__set_thread()
975 * so double check that it matches what OpenCSD thinks as well. It
976 * doesn't distinguish between EL0 and EL1 for this mem access callback
977 * so we had to do the extra tracking. Skip validation if it's any of
980 if (!(mem_space == OCSD_MEM_SPACE_ANY ||
981 mem_space == OCSD_MEM_SPACE_N || mem_space == OCSD_MEM_SPACE_S)) {
982 if (mem_space & OCSD_MEM_SPACE_EL1N) {
983 /* Includes both non secure EL1 and EL0 */
984 assert(tidq->el == ocsd_EL1 || tidq->el == ocsd_EL0);
985 } else if (mem_space & OCSD_MEM_SPACE_EL2)
986 assert(tidq->el == ocsd_EL2);
987 else if (mem_space & OCSD_MEM_SPACE_EL3)
988 assert(tidq->el == ocsd_EL3);
991 cpumode = cs_etm__cpu_mode(etmq, address, tidq->el);
993 if (!thread__find_map(tidq->thread, cpumode, address, &al))
996 dso = map__dso(al.map);
1000 if (dso->data.status == DSO_DATA_STATUS_ERROR &&
1001 dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))
1004 offset = map__map_ip(al.map, address);
1008 len = dso__data_read_offset(dso, maps__machine(thread__maps(tidq->thread)),
1009 offset, buffer, size);
1012 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
1013 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
1014 if (!dso->auxtrace_warned) {
1015 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
1017 dso->long_name ? dso->long_name : "Unknown");
1018 dso->auxtrace_warned = true;
1024 addr_location__exit(&al);
1028 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
1031 struct cs_etm_decoder_params d_params;
1032 struct cs_etm_trace_params *t_params = NULL;
1033 struct cs_etm_queue *etmq;
1035 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
1038 int decoders = formatted ? etm->num_cpu : 1;
1040 etmq = zalloc(sizeof(*etmq));
1044 etmq->traceid_queues_list = intlist__new(NULL);
1045 if (!etmq->traceid_queues_list)
1048 /* Use metadata to fill in trace parameters for trace decoder */
1049 t_params = zalloc(sizeof(*t_params) * decoders);
1054 if (cs_etm__init_trace_params(t_params, etm, decoders))
1057 /* Set decoder parameters to decode trace packets */
1058 if (cs_etm__init_decoder_params(&d_params, etmq,
1059 dump_trace ? CS_ETM_OPERATION_PRINT :
1060 CS_ETM_OPERATION_DECODE,
1064 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
1071 * Register a function to handle all memory accesses required by
1072 * the trace decoder library.
1074 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
1076 cs_etm__mem_access))
1077 goto out_free_decoder;
1083 cs_etm_decoder__free(etmq->decoder);
1085 intlist__delete(etmq->traceid_queues_list);
1091 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
1092 struct auxtrace_queue *queue,
1093 unsigned int queue_nr,
1096 struct cs_etm_queue *etmq = queue->priv;
1098 if (list_empty(&queue->head) || etmq)
1101 etmq = cs_etm__alloc_queue(etm, formatted);
1108 etmq->queue_nr = queue_nr;
1114 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
1115 struct cs_etm_queue *etmq,
1116 unsigned int queue_nr)
1119 unsigned int cs_queue_nr;
1124 * We are under a CPU-wide trace scenario. As such we need to know
1125 * when the code that generated the traces started to execute so that
1126 * it can be correlated with execution on other CPUs. So we get a
1127 * handle on the beginning of traces and decode until we find a
1128 * timestamp. The timestamp is then added to the auxtrace min heap
1129 * in order to know what nibble (of all the etmqs) to decode first.
1133 * Fetch an aux_buffer from this etmq. Bail if no more
1134 * blocks or an error has been encountered.
1136 ret = cs_etm__get_data_block(etmq);
1141 * Run decoder on the trace block. The decoder will stop when
1142 * encountering a CS timestamp, a full packet queue or the end of
1143 * trace for that block.
1145 ret = cs_etm__decode_data_block(etmq);
1150 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
1151 * the timestamp calculation for us.
1153 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
1155 /* We found a timestamp, no need to continue. */
1160 * We didn't find a timestamp so empty all the traceid packet
1161 * queues before looking for another timestamp packet, either
1162 * in the current data block or a new one. Packets that were
1163 * just decoded are useless since no timestamp has been
1164 * associated with them. As such simply discard them.
1166 cs_etm__clear_all_packet_queues(etmq);
1170 * We have a timestamp. Add it to the min heap to reflect when
1171 * instructions conveyed by the range packets of this traceID queue
1172 * started to execute. Once the same has been done for all the traceID
1173 * queues of each etmq, redenring and decoding can start in
1174 * chronological order.
1176 * Note that packets decoded above are still in the traceID's packet
1177 * queue and will be processed in cs_etm__process_timestamped_queues().
1179 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
1180 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
1186 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
1187 struct cs_etm_traceid_queue *tidq)
1189 struct branch_stack *bs_src = tidq->last_branch_rb;
1190 struct branch_stack *bs_dst = tidq->last_branch;
1194 * Set the number of records before early exit: ->nr is used to
1195 * determine how many branches to copy from ->entries.
1197 bs_dst->nr = bs_src->nr;
1200 * Early exit when there is nothing to copy.
1206 * As bs_src->entries is a circular buffer, we need to copy from it in
1207 * two steps. First, copy the branches from the most recently inserted
1208 * branch ->last_branch_pos until the end of bs_src->entries buffer.
1210 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
1211 memcpy(&bs_dst->entries[0],
1212 &bs_src->entries[tidq->last_branch_pos],
1213 sizeof(struct branch_entry) * nr);
1216 * If we wrapped around at least once, the branches from the beginning
1217 * of the bs_src->entries buffer and until the ->last_branch_pos element
1218 * are older valid branches: copy them over. The total number of
1219 * branches copied over will be equal to the number of branches asked by
1220 * the user in last_branch_sz.
1222 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
1223 memcpy(&bs_dst->entries[nr],
1224 &bs_src->entries[0],
1225 sizeof(struct branch_entry) * tidq->last_branch_pos);
1230 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
1232 tidq->last_branch_pos = 0;
1233 tidq->last_branch_rb->nr = 0;
1236 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
1237 u8 trace_chan_id, u64 addr)
1241 cs_etm__mem_access(etmq, trace_chan_id, addr, ARRAY_SIZE(instrBytes),
1244 * T32 instruction size is indicated by bits[15:11] of the first
1245 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
1246 * denote a 32-bit instruction.
1248 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
1251 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
1253 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1254 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1257 return packet->start_addr;
1261 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
1263 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1264 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1267 return packet->end_addr - packet->last_instr_size;
1270 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
1272 const struct cs_etm_packet *packet,
1275 if (packet->isa == CS_ETM_ISA_T32) {
1276 u64 addr = packet->start_addr;
1279 addr += cs_etm__t32_instr_size(etmq,
1280 trace_chan_id, addr);
1286 /* Assume a 4 byte instruction size (A32/A64) */
1287 return packet->start_addr + offset * 4;
1290 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1291 struct cs_etm_traceid_queue *tidq)
1293 struct branch_stack *bs = tidq->last_branch_rb;
1294 struct branch_entry *be;
1297 * The branches are recorded in a circular buffer in reverse
1298 * chronological order: we start recording from the last element of the
1299 * buffer down. After writing the first element of the stack, move the
1300 * insert position back to the end of the buffer.
1302 if (!tidq->last_branch_pos)
1303 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1305 tidq->last_branch_pos -= 1;
1307 be = &bs->entries[tidq->last_branch_pos];
1308 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1309 be->to = cs_etm__first_executed_instr(tidq->packet);
1310 /* No support for mispredict */
1311 be->flags.mispred = 0;
1312 be->flags.predicted = 1;
1315 * Increment bs->nr until reaching the number of last branches asked by
1316 * the user on the command line.
1318 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1322 static int cs_etm__inject_event(union perf_event *event,
1323 struct perf_sample *sample, u64 type)
1325 event->header.size = perf_event__sample_event_size(sample, type, 0);
1326 return perf_event__synthesize_sample(event, type, 0, sample);
1331 cs_etm__get_trace(struct cs_etm_queue *etmq)
1333 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1334 struct auxtrace_buffer *old_buffer = aux_buffer;
1335 struct auxtrace_queue *queue;
1337 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1339 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1341 /* If no more data, drop the previous auxtrace_buffer and return */
1344 auxtrace_buffer__drop_data(old_buffer);
1349 etmq->buffer = aux_buffer;
1351 /* If the aux_buffer doesn't have data associated, try to load it */
1352 if (!aux_buffer->data) {
1353 /* get the file desc associated with the perf data file */
1354 int fd = perf_data__fd(etmq->etm->session->data);
1356 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1357 if (!aux_buffer->data)
1361 /* If valid, drop the previous buffer */
1363 auxtrace_buffer__drop_data(old_buffer);
1366 etmq->buf_len = aux_buffer->size;
1367 etmq->buf = aux_buffer->data;
1369 return etmq->buf_len;
1372 static void cs_etm__set_thread(struct cs_etm_queue *etmq,
1373 struct cs_etm_traceid_queue *tidq, pid_t tid,
1376 struct machine *machine = cs_etm__get_machine(etmq, el);
1379 thread__zput(tidq->thread);
1380 tidq->thread = machine__find_thread(machine, -1, tid);
1383 /* Couldn't find a known thread */
1385 tidq->thread = machine__idle_thread(machine);
1390 int cs_etm__etmq_set_tid_el(struct cs_etm_queue *etmq, pid_t tid,
1391 u8 trace_chan_id, ocsd_ex_level el)
1393 struct cs_etm_traceid_queue *tidq;
1395 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1399 cs_etm__set_thread(etmq, tidq, tid, el);
1403 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1405 return !!etmq->etm->timeless_decoding;
1408 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1410 const struct cs_etm_packet *packet,
1411 struct perf_sample *sample)
1414 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1415 * packet, so directly bail out with 'insn_len' = 0.
1417 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1418 sample->insn_len = 0;
1423 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1424 * cs_etm__t32_instr_size().
1426 if (packet->isa == CS_ETM_ISA_T32)
1427 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1429 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1431 sample->insn_len = 4;
1433 cs_etm__mem_access(etmq, trace_chan_id, sample->ip, sample->insn_len,
1434 (void *)sample->insn, 0);
1437 u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp)
1439 struct cs_etm_auxtrace *etm = etmq->etm;
1441 if (etm->has_virtual_ts)
1442 return tsc_to_perf_time(cs_timestamp, &etm->tc);
1444 return cs_timestamp;
1447 static inline u64 cs_etm__resolve_sample_time(struct cs_etm_queue *etmq,
1448 struct cs_etm_traceid_queue *tidq)
1450 struct cs_etm_auxtrace *etm = etmq->etm;
1451 struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue;
1453 if (!etm->timeless_decoding && etm->has_virtual_ts)
1454 return packet_queue->cs_timestamp;
1456 return etm->latest_kernel_timestamp;
1459 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1460 struct cs_etm_traceid_queue *tidq,
1461 u64 addr, u64 period)
1464 struct cs_etm_auxtrace *etm = etmq->etm;
1465 union perf_event *event = tidq->event_buf;
1466 struct perf_sample sample = {.ip = 0,};
1468 event->sample.header.type = PERF_RECORD_SAMPLE;
1469 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr, tidq->el);
1470 event->sample.header.size = sizeof(struct perf_event_header);
1472 /* Set time field based on etm auxtrace config. */
1473 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1476 sample.pid = thread__pid(tidq->thread);
1477 sample.tid = thread__tid(tidq->thread);
1478 sample.id = etmq->etm->instructions_id;
1479 sample.stream_id = etmq->etm->instructions_id;
1480 sample.period = period;
1481 sample.cpu = tidq->packet->cpu;
1482 sample.flags = tidq->prev_packet->flags;
1483 sample.cpumode = event->sample.header.misc;
1485 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1487 if (etm->synth_opts.last_branch)
1488 sample.branch_stack = tidq->last_branch;
1490 if (etm->synth_opts.inject) {
1491 ret = cs_etm__inject_event(event, &sample,
1492 etm->instructions_sample_type);
1497 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1501 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1508 * The cs etm packet encodes an instruction range between a branch target
1509 * and the next taken branch. Generate sample accordingly.
1511 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1512 struct cs_etm_traceid_queue *tidq)
1515 struct cs_etm_auxtrace *etm = etmq->etm;
1516 struct perf_sample sample = {.ip = 0,};
1517 union perf_event *event = tidq->event_buf;
1518 struct dummy_branch_stack {
1521 struct branch_entry entries;
1525 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1527 event->sample.header.type = PERF_RECORD_SAMPLE;
1528 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip,
1529 tidq->prev_packet_el);
1530 event->sample.header.size = sizeof(struct perf_event_header);
1532 /* Set time field based on etm auxtrace config. */
1533 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1536 sample.pid = thread__pid(tidq->prev_packet_thread);
1537 sample.tid = thread__tid(tidq->prev_packet_thread);
1538 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1539 sample.id = etmq->etm->branches_id;
1540 sample.stream_id = etmq->etm->branches_id;
1542 sample.cpu = tidq->packet->cpu;
1543 sample.flags = tidq->prev_packet->flags;
1544 sample.cpumode = event->sample.header.misc;
1546 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1550 * perf report cannot handle events without a branch stack
1552 if (etm->synth_opts.last_branch) {
1553 dummy_bs = (struct dummy_branch_stack){
1561 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1564 if (etm->synth_opts.inject) {
1565 ret = cs_etm__inject_event(event, &sample,
1566 etm->branches_sample_type);
1571 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1575 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1581 struct cs_etm_synth {
1582 struct perf_tool dummy_tool;
1583 struct perf_session *session;
1586 static int cs_etm__event_synth(struct perf_tool *tool,
1587 union perf_event *event,
1588 struct perf_sample *sample __maybe_unused,
1589 struct machine *machine __maybe_unused)
1591 struct cs_etm_synth *cs_etm_synth =
1592 container_of(tool, struct cs_etm_synth, dummy_tool);
1594 return perf_session__deliver_synth_event(cs_etm_synth->session,
1598 static int cs_etm__synth_event(struct perf_session *session,
1599 struct perf_event_attr *attr, u64 id)
1601 struct cs_etm_synth cs_etm_synth;
1603 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1604 cs_etm_synth.session = session;
1606 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1607 &id, cs_etm__event_synth);
1610 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1611 struct perf_session *session)
1613 struct evlist *evlist = session->evlist;
1614 struct evsel *evsel;
1615 struct perf_event_attr attr;
1620 evlist__for_each_entry(evlist, evsel) {
1621 if (evsel->core.attr.type == etm->pmu_type) {
1628 pr_debug("No selected events with CoreSight Trace data\n");
1632 memset(&attr, 0, sizeof(struct perf_event_attr));
1633 attr.size = sizeof(struct perf_event_attr);
1634 attr.type = PERF_TYPE_HARDWARE;
1635 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1636 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1638 if (etm->timeless_decoding)
1639 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1641 attr.sample_type |= PERF_SAMPLE_TIME;
1643 attr.exclude_user = evsel->core.attr.exclude_user;
1644 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1645 attr.exclude_hv = evsel->core.attr.exclude_hv;
1646 attr.exclude_host = evsel->core.attr.exclude_host;
1647 attr.exclude_guest = evsel->core.attr.exclude_guest;
1648 attr.sample_id_all = evsel->core.attr.sample_id_all;
1649 attr.read_format = evsel->core.attr.read_format;
1651 /* create new id val to be a fixed offset from evsel id */
1652 id = evsel->core.id[0] + 1000000000;
1657 if (etm->synth_opts.branches) {
1658 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1659 attr.sample_period = 1;
1660 attr.sample_type |= PERF_SAMPLE_ADDR;
1661 err = cs_etm__synth_event(session, &attr, id);
1664 etm->branches_sample_type = attr.sample_type;
1665 etm->branches_id = id;
1667 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1670 if (etm->synth_opts.last_branch) {
1671 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1673 * We don't use the hardware index, but the sample generation
1674 * code uses the new format branch_stack with this field,
1675 * so the event attributes must indicate that it's present.
1677 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1680 if (etm->synth_opts.instructions) {
1681 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1682 attr.sample_period = etm->synth_opts.period;
1683 etm->instructions_sample_period = attr.sample_period;
1684 err = cs_etm__synth_event(session, &attr, id);
1687 etm->instructions_sample_type = attr.sample_type;
1688 etm->instructions_id = id;
1695 static int cs_etm__sample(struct cs_etm_queue *etmq,
1696 struct cs_etm_traceid_queue *tidq)
1698 struct cs_etm_auxtrace *etm = etmq->etm;
1700 u8 trace_chan_id = tidq->trace_chan_id;
1703 /* Get instructions remainder from previous packet */
1704 instrs_prev = tidq->period_instructions;
1706 tidq->period_instructions += tidq->packet->instr_count;
1709 * Record a branch when the last instruction in
1710 * PREV_PACKET is a branch.
1712 if (etm->synth_opts.last_branch &&
1713 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1714 tidq->prev_packet->last_instr_taken_branch)
1715 cs_etm__update_last_branch_rb(etmq, tidq);
1717 if (etm->synth_opts.instructions &&
1718 tidq->period_instructions >= etm->instructions_sample_period) {
1720 * Emit instruction sample periodically
1721 * TODO: allow period to be defined in cycles and clock time
1725 * Below diagram demonstrates the instruction samples
1728 * Instrs Instrs Instrs Instrs
1729 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1732 * --------------------------------------------------
1736 * instructions(Pi) instructions(Pi')
1739 * \---------------- -----------------/
1741 * tidq->packet->instr_count
1743 * Instrs Sample(n...) are the synthesised samples occurring
1744 * every etm->instructions_sample_period instructions - as
1745 * defined on the perf command line. Sample(n) is being the
1746 * last sample before the current etm packet, n+1 to n+3
1747 * samples are generated from the current etm packet.
1749 * tidq->packet->instr_count represents the number of
1750 * instructions in the current etm packet.
1752 * Period instructions (Pi) contains the number of
1753 * instructions executed after the sample point(n) from the
1754 * previous etm packet. This will always be less than
1755 * etm->instructions_sample_period.
1757 * When generate new samples, it combines with two parts
1758 * instructions, one is the tail of the old packet and another
1759 * is the head of the new coming packet, to generate
1760 * sample(n+1); sample(n+2) and sample(n+3) consume the
1761 * instructions with sample period. After sample(n+3), the rest
1762 * instructions will be used by later packet and it is assigned
1763 * to tidq->period_instructions for next round calculation.
1767 * Get the initial offset into the current packet instructions;
1768 * entry conditions ensure that instrs_prev is less than
1769 * etm->instructions_sample_period.
1771 u64 offset = etm->instructions_sample_period - instrs_prev;
1774 /* Prepare last branches for instruction sample */
1775 if (etm->synth_opts.last_branch)
1776 cs_etm__copy_last_branch_rb(etmq, tidq);
1778 while (tidq->period_instructions >=
1779 etm->instructions_sample_period) {
1781 * Calculate the address of the sampled instruction (-1
1782 * as sample is reported as though instruction has just
1783 * been executed, but PC has not advanced to next
1786 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1787 tidq->packet, offset - 1);
1788 ret = cs_etm__synth_instruction_sample(
1790 etm->instructions_sample_period);
1794 offset += etm->instructions_sample_period;
1795 tidq->period_instructions -=
1796 etm->instructions_sample_period;
1800 if (etm->synth_opts.branches) {
1801 bool generate_sample = false;
1803 /* Generate sample for tracing on packet */
1804 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1805 generate_sample = true;
1807 /* Generate sample for branch taken packet */
1808 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1809 tidq->prev_packet->last_instr_taken_branch)
1810 generate_sample = true;
1812 if (generate_sample) {
1813 ret = cs_etm__synth_branch_sample(etmq, tidq);
1819 cs_etm__packet_swap(etm, tidq);
1824 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1827 * When the exception packet is inserted, whether the last instruction
1828 * in previous range packet is taken branch or not, we need to force
1829 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1830 * to generate branch sample for the instruction range before the
1831 * exception is trapped to kernel or before the exception returning.
1833 * The exception packet includes the dummy address values, so don't
1834 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1835 * for generating instruction and branch samples.
1837 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1838 tidq->prev_packet->last_instr_taken_branch = true;
1843 static int cs_etm__flush(struct cs_etm_queue *etmq,
1844 struct cs_etm_traceid_queue *tidq)
1847 struct cs_etm_auxtrace *etm = etmq->etm;
1849 /* Handle start tracing packet */
1850 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1853 if (etmq->etm->synth_opts.last_branch &&
1854 etmq->etm->synth_opts.instructions &&
1855 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1858 /* Prepare last branches for instruction sample */
1859 cs_etm__copy_last_branch_rb(etmq, tidq);
1862 * Generate a last branch event for the branches left in the
1863 * circular buffer at the end of the trace.
1865 * Use the address of the end of the last reported execution
1868 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1870 err = cs_etm__synth_instruction_sample(
1872 tidq->period_instructions);
1876 tidq->period_instructions = 0;
1880 if (etm->synth_opts.branches &&
1881 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1882 err = cs_etm__synth_branch_sample(etmq, tidq);
1888 cs_etm__packet_swap(etm, tidq);
1890 /* Reset last branches after flush the trace */
1891 if (etm->synth_opts.last_branch)
1892 cs_etm__reset_last_branch_rb(tidq);
1897 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1898 struct cs_etm_traceid_queue *tidq)
1903 * It has no new packet coming and 'etmq->packet' contains the stale
1904 * packet which was set at the previous time with packets swapping;
1905 * so skip to generate branch sample to avoid stale packet.
1907 * For this case only flush branch stack and generate a last branch
1908 * event for the branches left in the circular buffer at the end of
1911 if (etmq->etm->synth_opts.last_branch &&
1912 etmq->etm->synth_opts.instructions &&
1913 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1916 /* Prepare last branches for instruction sample */
1917 cs_etm__copy_last_branch_rb(etmq, tidq);
1920 * Use the address of the end of the last reported execution
1923 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1925 err = cs_etm__synth_instruction_sample(
1927 tidq->period_instructions);
1931 tidq->period_instructions = 0;
1937 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1939 * Returns: < 0 if error
1940 * = 0 if no more auxtrace_buffer to read
1941 * > 0 if the current buffer isn't empty yet
1943 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1947 if (!etmq->buf_len) {
1948 ret = cs_etm__get_trace(etmq);
1952 * We cannot assume consecutive blocks in the data file
1953 * are contiguous, reset the decoder to force re-sync.
1955 ret = cs_etm_decoder__reset(etmq->decoder);
1960 return etmq->buf_len;
1963 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1964 struct cs_etm_packet *packet,
1967 /* Initialise to keep compiler happy */
1972 switch (packet->isa) {
1973 case CS_ETM_ISA_T32:
1975 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1978 * +-----------------+--------+
1979 * | 1 1 0 1 1 1 1 1 | imm8 |
1980 * +-----------------+--------+
1982 * According to the specification, it only defines SVC for T32
1983 * with 16 bits instruction and has no definition for 32bits;
1984 * so below only read 2 bytes as instruction size for T32.
1986 addr = end_addr - 2;
1987 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr16),
1989 if ((instr16 & 0xFF00) == 0xDF00)
1993 case CS_ETM_ISA_A32:
1995 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1997 * b'31 b'28 b'27 b'24
1998 * +---------+---------+-------------------------+
1999 * | !1111 | 1 1 1 1 | imm24 |
2000 * +---------+---------+-------------------------+
2002 addr = end_addr - 4;
2003 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr32),
2005 if ((instr32 & 0x0F000000) == 0x0F000000 &&
2006 (instr32 & 0xF0000000) != 0xF0000000)
2010 case CS_ETM_ISA_A64:
2012 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
2015 * +-----------------------+---------+-----------+
2016 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
2017 * +-----------------------+---------+-----------+
2019 addr = end_addr - 4;
2020 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr32),
2022 if ((instr32 & 0xFFE0001F) == 0xd4000001)
2026 case CS_ETM_ISA_UNKNOWN:
2034 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
2035 struct cs_etm_traceid_queue *tidq, u64 magic)
2037 u8 trace_chan_id = tidq->trace_chan_id;
2038 struct cs_etm_packet *packet = tidq->packet;
2039 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2041 if (magic == __perf_cs_etmv3_magic)
2042 if (packet->exception_number == CS_ETMV3_EXC_SVC)
2046 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
2047 * HVC cases; need to check if it's SVC instruction based on
2050 if (magic == __perf_cs_etmv4_magic) {
2051 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2052 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2053 prev_packet->end_addr))
2060 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
2063 struct cs_etm_packet *packet = tidq->packet;
2065 if (magic == __perf_cs_etmv3_magic)
2066 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
2067 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
2068 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
2069 packet->exception_number == CS_ETMV3_EXC_IRQ ||
2070 packet->exception_number == CS_ETMV3_EXC_FIQ)
2073 if (magic == __perf_cs_etmv4_magic)
2074 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
2075 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
2076 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
2077 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
2078 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
2079 packet->exception_number == CS_ETMV4_EXC_IRQ ||
2080 packet->exception_number == CS_ETMV4_EXC_FIQ)
2086 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
2087 struct cs_etm_traceid_queue *tidq,
2090 u8 trace_chan_id = tidq->trace_chan_id;
2091 struct cs_etm_packet *packet = tidq->packet;
2092 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2094 if (magic == __perf_cs_etmv3_magic)
2095 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
2096 packet->exception_number == CS_ETMV3_EXC_HYP ||
2097 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
2098 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
2099 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
2100 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
2101 packet->exception_number == CS_ETMV3_EXC_GENERIC)
2104 if (magic == __perf_cs_etmv4_magic) {
2105 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
2106 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
2107 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
2108 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
2112 * For CS_ETMV4_EXC_CALL, except SVC other instructions
2113 * (SMC, HVC) are taken as sync exceptions.
2115 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2116 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2117 prev_packet->end_addr))
2121 * ETMv4 has 5 bits for exception number; if the numbers
2122 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
2123 * they are implementation defined exceptions.
2125 * For this case, simply take it as sync exception.
2127 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
2128 packet->exception_number <= CS_ETMV4_EXC_END)
2135 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
2136 struct cs_etm_traceid_queue *tidq)
2138 struct cs_etm_packet *packet = tidq->packet;
2139 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2140 u8 trace_chan_id = tidq->trace_chan_id;
2144 switch (packet->sample_type) {
2147 * Immediate branch instruction without neither link nor
2148 * return flag, it's normal branch instruction within
2151 if (packet->last_instr_type == OCSD_INSTR_BR &&
2152 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
2153 packet->flags = PERF_IP_FLAG_BRANCH;
2155 if (packet->last_instr_cond)
2156 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
2160 * Immediate branch instruction with link (e.g. BL), this is
2161 * branch instruction for function call.
2163 if (packet->last_instr_type == OCSD_INSTR_BR &&
2164 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2165 packet->flags = PERF_IP_FLAG_BRANCH |
2169 * Indirect branch instruction with link (e.g. BLR), this is
2170 * branch instruction for function call.
2172 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2173 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2174 packet->flags = PERF_IP_FLAG_BRANCH |
2178 * Indirect branch instruction with subtype of
2179 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
2180 * function return for A32/T32.
2182 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2183 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
2184 packet->flags = PERF_IP_FLAG_BRANCH |
2185 PERF_IP_FLAG_RETURN;
2188 * Indirect branch instruction without link (e.g. BR), usually
2189 * this is used for function return, especially for functions
2190 * within dynamic link lib.
2192 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2193 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
2194 packet->flags = PERF_IP_FLAG_BRANCH |
2195 PERF_IP_FLAG_RETURN;
2197 /* Return instruction for function return. */
2198 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2199 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
2200 packet->flags = PERF_IP_FLAG_BRANCH |
2201 PERF_IP_FLAG_RETURN;
2204 * Decoder might insert a discontinuity in the middle of
2205 * instruction packets, fixup prev_packet with flag
2206 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
2208 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
2209 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2210 PERF_IP_FLAG_TRACE_BEGIN;
2213 * If the previous packet is an exception return packet
2214 * and the return address just follows SVC instruction,
2215 * it needs to calibrate the previous packet sample flags
2216 * as PERF_IP_FLAG_SYSCALLRET.
2218 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
2219 PERF_IP_FLAG_RETURN |
2220 PERF_IP_FLAG_INTERRUPT) &&
2221 cs_etm__is_svc_instr(etmq, trace_chan_id,
2222 packet, packet->start_addr))
2223 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2224 PERF_IP_FLAG_RETURN |
2225 PERF_IP_FLAG_SYSCALLRET;
2227 case CS_ETM_DISCONTINUITY:
2229 * The trace is discontinuous, if the previous packet is
2230 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
2231 * for previous packet.
2233 if (prev_packet->sample_type == CS_ETM_RANGE)
2234 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2235 PERF_IP_FLAG_TRACE_END;
2237 case CS_ETM_EXCEPTION:
2238 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
2242 /* The exception is for system call. */
2243 if (cs_etm__is_syscall(etmq, tidq, magic))
2244 packet->flags = PERF_IP_FLAG_BRANCH |
2246 PERF_IP_FLAG_SYSCALLRET;
2248 * The exceptions are triggered by external signals from bus,
2249 * interrupt controller, debug module, PE reset or halt.
2251 else if (cs_etm__is_async_exception(tidq, magic))
2252 packet->flags = PERF_IP_FLAG_BRANCH |
2254 PERF_IP_FLAG_ASYNC |
2255 PERF_IP_FLAG_INTERRUPT;
2257 * Otherwise, exception is caused by trap, instruction &
2258 * data fault, or alignment errors.
2260 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
2261 packet->flags = PERF_IP_FLAG_BRANCH |
2263 PERF_IP_FLAG_INTERRUPT;
2266 * When the exception packet is inserted, since exception
2267 * packet is not used standalone for generating samples
2268 * and it's affiliation to the previous instruction range
2269 * packet; so set previous range packet flags to tell perf
2270 * it is an exception taken branch.
2272 if (prev_packet->sample_type == CS_ETM_RANGE)
2273 prev_packet->flags = packet->flags;
2275 case CS_ETM_EXCEPTION_RET:
2277 * When the exception return packet is inserted, since
2278 * exception return packet is not used standalone for
2279 * generating samples and it's affiliation to the previous
2280 * instruction range packet; so set previous range packet
2281 * flags to tell perf it is an exception return branch.
2283 * The exception return can be for either system call or
2284 * other exception types; unfortunately the packet doesn't
2285 * contain exception type related info so we cannot decide
2286 * the exception type purely based on exception return packet.
2287 * If we record the exception number from exception packet and
2288 * reuse it for exception return packet, this is not reliable
2289 * due the trace can be discontinuity or the interrupt can
2290 * be nested, thus the recorded exception number cannot be
2291 * used for exception return packet for these two cases.
2293 * For exception return packet, we only need to distinguish the
2294 * packet is for system call or for other types. Thus the
2295 * decision can be deferred when receive the next packet which
2296 * contains the return address, based on the return address we
2297 * can read out the previous instruction and check if it's a
2298 * system call instruction and then calibrate the sample flag
2301 if (prev_packet->sample_type == CS_ETM_RANGE)
2302 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2303 PERF_IP_FLAG_RETURN |
2304 PERF_IP_FLAG_INTERRUPT;
2314 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2317 size_t processed = 0;
2320 * Packets are decoded and added to the decoder's packet queue
2321 * until the decoder packet processing callback has requested that
2322 * processing stops or there is nothing left in the buffer. Normal
2323 * operations that stop processing are a timestamp packet or a full
2324 * decoder buffer queue.
2326 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2328 &etmq->buf[etmq->buf_used],
2334 etmq->offset += processed;
2335 etmq->buf_used += processed;
2336 etmq->buf_len -= processed;
2342 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2343 struct cs_etm_traceid_queue *tidq)
2346 struct cs_etm_packet_queue *packet_queue;
2348 packet_queue = &tidq->packet_queue;
2350 /* Process each packet in this chunk */
2352 ret = cs_etm_decoder__get_packet(packet_queue,
2356 * Stop processing this chunk on
2357 * end of data or error
2362 * Since packet addresses are swapped in packet
2363 * handling within below switch() statements,
2364 * thus setting sample flags must be called
2365 * prior to switch() statement to use address
2366 * information before packets swapping.
2368 ret = cs_etm__set_sample_flags(etmq, tidq);
2372 switch (tidq->packet->sample_type) {
2375 * If the packet contains an instruction
2376 * range, generate instruction sequence
2379 cs_etm__sample(etmq, tidq);
2381 case CS_ETM_EXCEPTION:
2382 case CS_ETM_EXCEPTION_RET:
2384 * If the exception packet is coming,
2385 * make sure the previous instruction
2386 * range packet to be handled properly.
2388 cs_etm__exception(tidq);
2390 case CS_ETM_DISCONTINUITY:
2392 * Discontinuity in trace, flush
2393 * previous branch stack
2395 cs_etm__flush(etmq, tidq);
2399 * Should not receive empty packet,
2402 pr_err("CS ETM Trace: empty packet\n");
2412 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2415 struct int_node *inode;
2416 struct cs_etm_traceid_queue *tidq;
2417 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2419 intlist__for_each_entry(inode, traceid_queues_list) {
2420 idx = (int)(intptr_t)inode->priv;
2421 tidq = etmq->traceid_queues[idx];
2423 /* Ignore return value */
2424 cs_etm__process_traceid_queue(etmq, tidq);
2427 * Generate an instruction sample with the remaining
2428 * branchstack entries.
2430 cs_etm__flush(etmq, tidq);
2434 static int cs_etm__run_per_thread_timeless_decoder(struct cs_etm_queue *etmq)
2437 struct cs_etm_traceid_queue *tidq;
2439 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2443 /* Go through each buffer in the queue and decode them one by one */
2445 err = cs_etm__get_data_block(etmq);
2449 /* Run trace decoder until buffer consumed or end of trace */
2451 err = cs_etm__decode_data_block(etmq);
2456 * Process each packet in this chunk, nothing to do if
2457 * an error occurs other than hoping the next one will
2460 err = cs_etm__process_traceid_queue(etmq, tidq);
2462 } while (etmq->buf_len);
2465 /* Flush any remaining branch stack entries */
2466 err = cs_etm__end_block(etmq, tidq);
2472 static int cs_etm__run_per_cpu_timeless_decoder(struct cs_etm_queue *etmq)
2475 struct cs_etm_traceid_queue *tidq;
2476 struct int_node *inode;
2478 /* Go through each buffer in the queue and decode them one by one */
2480 err = cs_etm__get_data_block(etmq);
2484 /* Run trace decoder until buffer consumed or end of trace */
2486 err = cs_etm__decode_data_block(etmq);
2491 * cs_etm__run_per_thread_timeless_decoder() runs on a
2492 * single traceID queue because each TID has a separate
2493 * buffer. But here in per-cpu mode we need to iterate
2494 * over each channel instead.
2496 intlist__for_each_entry(inode,
2497 etmq->traceid_queues_list) {
2498 idx = (int)(intptr_t)inode->priv;
2499 tidq = etmq->traceid_queues[idx];
2500 cs_etm__process_traceid_queue(etmq, tidq);
2502 } while (etmq->buf_len);
2504 intlist__for_each_entry(inode, etmq->traceid_queues_list) {
2505 idx = (int)(intptr_t)inode->priv;
2506 tidq = etmq->traceid_queues[idx];
2507 /* Flush any remaining branch stack entries */
2508 err = cs_etm__end_block(etmq, tidq);
2517 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2521 struct auxtrace_queues *queues = &etm->queues;
2523 for (i = 0; i < queues->nr_queues; i++) {
2524 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2525 struct cs_etm_queue *etmq = queue->priv;
2526 struct cs_etm_traceid_queue *tidq;
2531 if (etm->per_thread_decoding) {
2532 tidq = cs_etm__etmq_get_traceid_queue(
2533 etmq, CS_ETM_PER_THREAD_TRACEID);
2538 if (tid == -1 || thread__tid(tidq->thread) == tid)
2539 cs_etm__run_per_thread_timeless_decoder(etmq);
2541 cs_etm__run_per_cpu_timeless_decoder(etmq);
2547 static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
2550 unsigned int cs_queue_nr, queue_nr, i;
2553 struct auxtrace_queue *queue;
2554 struct cs_etm_queue *etmq;
2555 struct cs_etm_traceid_queue *tidq;
2558 * Pre-populate the heap with one entry from each queue so that we can
2559 * start processing in time order across all queues.
2561 for (i = 0; i < etm->queues.nr_queues; i++) {
2562 etmq = etm->queues.queue_array[i].priv;
2566 ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2572 if (!etm->heap.heap_cnt)
2575 /* Take the entry at the top of the min heap */
2576 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2577 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2578 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2579 queue = &etm->queues.queue_array[queue_nr];
2583 * Remove the top entry from the heap since we are about
2586 auxtrace_heap__pop(&etm->heap);
2588 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2591 * No traceID queue has been allocated for this traceID,
2592 * which means something somewhere went very wrong. No
2593 * other choice than simply exit.
2600 * Packets associated with this timestamp are already in
2601 * the etmq's traceID queue, so process them.
2603 ret = cs_etm__process_traceid_queue(etmq, tidq);
2608 * Packets for this timestamp have been processed, time to
2609 * move on to the next timestamp, fetching a new auxtrace_buffer
2613 ret = cs_etm__get_data_block(etmq);
2618 * No more auxtrace_buffers to process in this etmq, simply
2619 * move on to another entry in the auxtrace_heap.
2624 ret = cs_etm__decode_data_block(etmq);
2628 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2630 if (!cs_timestamp) {
2632 * Function cs_etm__decode_data_block() returns when
2633 * there is no more traces to decode in the current
2634 * auxtrace_buffer OR when a timestamp has been
2635 * encountered on any of the traceID queues. Since we
2636 * did not get a timestamp, there is no more traces to
2637 * process in this auxtrace_buffer. As such empty and
2638 * flush all traceID queues.
2640 cs_etm__clear_all_traceid_queues(etmq);
2642 /* Fetch another auxtrace_buffer for this etmq */
2647 * Add to the min heap the timestamp for packets that have
2648 * just been decoded. They will be processed and synthesized
2649 * during the next call to cs_etm__process_traceid_queue() for
2650 * this queue/traceID.
2652 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2653 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2660 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2661 union perf_event *event)
2665 if (etm->timeless_decoding)
2669 * Add the tid/pid to the log so that we can get a match when we get a
2670 * contextID from the decoder. Only track for the host: only kernel
2671 * trace is supported for guests which wouldn't need pids so this should
2674 th = machine__findnew_thread(&etm->session->machines.host,
2675 event->itrace_start.pid,
2676 event->itrace_start.tid);
2685 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2686 union perf_event *event)
2689 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2692 * Context switch in per-thread mode are irrelevant since perf
2693 * will start/stop tracing as the process is scheduled.
2695 if (etm->timeless_decoding)
2699 * SWITCH_IN events carry the next process to be switched out while
2700 * SWITCH_OUT events carry the process to be switched in. As such
2701 * we don't care about IN events.
2707 * Add the tid/pid to the log so that we can get a match when we get a
2708 * contextID from the decoder. Only track for the host: only kernel
2709 * trace is supported for guests which wouldn't need pids so this should
2712 th = machine__findnew_thread(&etm->session->machines.host,
2713 event->context_switch.next_prev_pid,
2714 event->context_switch.next_prev_tid);
2723 static int cs_etm__process_event(struct perf_session *session,
2724 union perf_event *event,
2725 struct perf_sample *sample,
2726 struct perf_tool *tool)
2728 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2729 struct cs_etm_auxtrace,
2735 if (!tool->ordered_events) {
2736 pr_err("CoreSight ETM Trace requires ordered events\n");
2740 switch (event->header.type) {
2741 case PERF_RECORD_EXIT:
2743 * Don't need to wait for cs_etm__flush_events() in per-thread mode to
2744 * start the decode because we know there will be no more trace from
2745 * this thread. All this does is emit samples earlier than waiting for
2746 * the flush in other modes, but with timestamps it makes sense to wait
2747 * for flush so that events from different threads are interleaved
2750 if (etm->per_thread_decoding && etm->timeless_decoding)
2751 return cs_etm__process_timeless_queues(etm,
2755 case PERF_RECORD_ITRACE_START:
2756 return cs_etm__process_itrace_start(etm, event);
2758 case PERF_RECORD_SWITCH_CPU_WIDE:
2759 return cs_etm__process_switch_cpu_wide(etm, event);
2761 case PERF_RECORD_AUX:
2763 * Record the latest kernel timestamp available in the header
2764 * for samples so that synthesised samples occur from this point
2767 if (sample->time && (sample->time != (u64)-1))
2768 etm->latest_kernel_timestamp = sample->time;
2778 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2779 struct perf_record_auxtrace *event)
2781 struct auxtrace_buffer *buf;
2784 * Find all buffers with same reference in the queues and dump them.
2785 * This is because the queues can contain multiple entries of the same
2786 * buffer that were split on aux records.
2788 for (i = 0; i < etm->queues.nr_queues; ++i)
2789 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2790 if (buf->reference == event->reference)
2791 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2794 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2795 union perf_event *event,
2796 struct perf_tool *tool __maybe_unused)
2798 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2799 struct cs_etm_auxtrace,
2801 if (!etm->data_queued) {
2802 struct auxtrace_buffer *buffer;
2804 int fd = perf_data__fd(session->data);
2805 bool is_pipe = perf_data__is_pipe(session->data);
2807 int idx = event->auxtrace.idx;
2812 data_offset = lseek(fd, 0, SEEK_CUR);
2813 if (data_offset == -1)
2817 err = auxtrace_queues__add_event(&etm->queues, session,
2818 event, data_offset, &buffer);
2823 * Knowing if the trace is formatted or not requires a lookup of
2824 * the aux record so only works in non-piped mode where data is
2825 * queued in cs_etm__queue_aux_records(). Always assume
2826 * formatted in piped mode (true).
2828 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2834 if (auxtrace_buffer__get_data(buffer, fd)) {
2835 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2836 auxtrace_buffer__put_data(buffer);
2838 } else if (dump_trace)
2839 dump_queued_data(etm, &event->auxtrace);
2844 static int cs_etm__setup_timeless_decoding(struct cs_etm_auxtrace *etm)
2846 struct evsel *evsel;
2847 struct evlist *evlist = etm->session->evlist;
2849 /* Override timeless mode with user input from --itrace=Z */
2850 if (etm->synth_opts.timeless_decoding) {
2851 etm->timeless_decoding = true;
2856 * Find the cs_etm evsel and look at what its timestamp setting was
2858 evlist__for_each_entry(evlist, evsel)
2859 if (cs_etm__evsel_is_auxtrace(etm->session, evsel)) {
2860 etm->timeless_decoding =
2861 !(evsel->core.attr.config & BIT(ETM_OPT_TS));
2865 pr_err("CS ETM: Couldn't find ETM evsel\n");
2870 * Read a single cpu parameter block from the auxtrace_info priv block.
2872 * For version 1 there is a per cpu nr_params entry. If we are handling
2873 * version 1 file, then there may be less, the same, or more params
2874 * indicated by this value than the compile time number we understand.
2876 * For a version 0 info block, there are a fixed number, and we need to
2877 * fill out the nr_param value in the metadata we create.
2879 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2880 int out_blk_size, int nr_params_v0)
2882 u64 *metadata = NULL;
2884 int nr_in_params, nr_out_params, nr_cmn_params;
2887 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2891 /* read block current index & version */
2892 i = *buff_in_offset;
2893 hdr_version = buff_in[CS_HEADER_VERSION];
2896 /* read version 0 info block into a version 1 metadata block */
2897 nr_in_params = nr_params_v0;
2898 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2899 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2900 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2901 /* remaining block params at offset +1 from source */
2902 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2903 metadata[k + 1] = buff_in[i + k];
2904 /* version 0 has 2 common params */
2907 /* read version 1 info block - input and output nr_params may differ */
2908 /* version 1 has 3 common params */
2910 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2912 /* if input has more params than output - skip excess */
2913 nr_out_params = nr_in_params + nr_cmn_params;
2914 if (nr_out_params > out_blk_size)
2915 nr_out_params = out_blk_size;
2917 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2918 metadata[k] = buff_in[i + k];
2920 /* record the actual nr params we copied */
2921 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2924 /* adjust in offset by number of in params used */
2925 i += nr_in_params + nr_cmn_params;
2926 *buff_in_offset = i;
2931 * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2932 * on the bounds of aux_event, if it matches with the buffer that's at
2935 * Normally, whole auxtrace buffers would be added to the queue. But we
2936 * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2937 * is reset across each buffer, so splitting the buffers up in advance has
2940 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2941 struct perf_record_aux *aux_event, struct perf_sample *sample)
2944 char buf[PERF_SAMPLE_MAX_SIZE];
2945 union perf_event *auxtrace_event_union;
2946 struct perf_record_auxtrace *auxtrace_event;
2947 union perf_event auxtrace_fragment;
2948 __u64 aux_offset, aux_size;
2952 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2953 struct cs_etm_auxtrace,
2957 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2958 * from looping through the auxtrace index.
2960 err = perf_session__peek_event(session, file_offset, buf,
2961 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2964 auxtrace_event = &auxtrace_event_union->auxtrace;
2965 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2968 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2969 auxtrace_event->header.size != sz) {
2974 * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
2975 * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a
2976 * CPU as we set this always for the AUX_OUTPUT_HW_ID event.
2977 * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
2978 * Return 'not found' if mismatch.
2980 if (auxtrace_event->cpu == (__u32) -1) {
2981 etm->per_thread_decoding = true;
2982 if (auxtrace_event->tid != sample->tid)
2984 } else if (auxtrace_event->cpu != sample->cpu) {
2985 if (etm->per_thread_decoding) {
2987 * Found a per-cpu buffer after a per-thread one was
2990 pr_err("CS ETM: Inconsistent per-thread/per-cpu mode.\n");
2996 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2998 * Clamp size in snapshot mode. The buffer size is clamped in
2999 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
3002 aux_size = min(aux_event->aux_size, auxtrace_event->size);
3005 * In this mode, the head also points to the end of the buffer so aux_offset
3006 * needs to have the size subtracted so it points to the beginning as in normal mode
3008 aux_offset = aux_event->aux_offset - aux_size;
3010 aux_size = aux_event->aux_size;
3011 aux_offset = aux_event->aux_offset;
3014 if (aux_offset >= auxtrace_event->offset &&
3015 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
3017 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
3018 * based on the sizes of the aux event, and queue that fragment.
3020 auxtrace_fragment.auxtrace = *auxtrace_event;
3021 auxtrace_fragment.auxtrace.size = aux_size;
3022 auxtrace_fragment.auxtrace.offset = aux_offset;
3023 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
3025 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
3026 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
3027 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
3032 idx = auxtrace_event->idx;
3033 formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
3034 return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
3038 /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
3042 static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event,
3043 u64 offset __maybe_unused, void *data __maybe_unused)
3045 /* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */
3046 if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
3047 (*(int *)data)++; /* increment found count */
3048 return cs_etm__process_aux_output_hw_id(session, event);
3053 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
3054 u64 offset __maybe_unused, void *data __maybe_unused)
3056 struct perf_sample sample;
3058 struct auxtrace_index_entry *ent;
3059 struct auxtrace_index *auxtrace_index;
3060 struct evsel *evsel;
3063 /* Don't care about any other events, we're only queuing buffers for AUX events */
3064 if (event->header.type != PERF_RECORD_AUX)
3067 if (event->header.size < sizeof(struct perf_record_aux))
3070 /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
3071 if (!event->aux.aux_size)
3075 * Parse the sample, we need the sample_id_all data that comes after the event so that the
3076 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
3078 evsel = evlist__event2evsel(session->evlist, event);
3081 ret = evsel__parse_sample(evsel, event, &sample);
3086 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
3088 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
3089 for (i = 0; i < auxtrace_index->nr; i++) {
3090 ent = &auxtrace_index->entries[i];
3091 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
3092 ent->sz, &event->aux, &sample);
3094 * Stop search on error or successful values. Continue search on
3103 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
3104 * don't exit with an error because it will still be possible to decode other aux records.
3106 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
3107 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
3111 static int cs_etm__queue_aux_records(struct perf_session *session)
3113 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
3114 struct auxtrace_index, list);
3115 if (index && index->nr > 0)
3116 return perf_session__peek_events(session, session->header.data_offset,
3117 session->header.data_size,
3118 cs_etm__queue_aux_records_cb, NULL);
3121 * We would get here if there are no entries in the index (either no auxtrace
3122 * buffers or no index at all). Fail silently as there is the possibility of
3123 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
3126 * In that scenario, buffers will not be split by AUX records.
3131 #define HAS_PARAM(j, type, param) (metadata[(j)][CS_ETM_NR_TRC_PARAMS] <= \
3132 (CS_##type##_##param - CS_ETM_COMMON_BLK_MAX_V1))
3135 * Loop through the ETMs and complain if we find at least one where ts_source != 1 (virtual
3138 static bool cs_etm__has_virtual_ts(u64 **metadata, int num_cpu)
3142 for (j = 0; j < num_cpu; j++) {
3143 switch (metadata[j][CS_ETM_MAGIC]) {
3144 case __perf_cs_etmv4_magic:
3145 if (HAS_PARAM(j, ETMV4, TS_SOURCE) || metadata[j][CS_ETMV4_TS_SOURCE] != 1)
3148 case __perf_cs_ete_magic:
3149 if (HAS_PARAM(j, ETE, TS_SOURCE) || metadata[j][CS_ETE_TS_SOURCE] != 1)
3153 /* Unknown / unsupported magic number. */
3160 /* map trace ids to correct metadata block, from information in metadata */
3161 static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata)
3167 for (i = 0; i < num_cpu; i++) {
3168 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3169 switch (cs_etm_magic) {
3170 case __perf_cs_etmv3_magic:
3171 metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3172 trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]);
3174 case __perf_cs_etmv4_magic:
3175 case __perf_cs_ete_magic:
3176 metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3177 trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]);
3180 /* unknown magic number */
3183 err = cs_etm__map_trace_id(trace_chan_id, metadata[i]);
3191 * If we found AUX_HW_ID packets, then set any metadata marked as unused to the
3192 * unused value to reduce the number of unneeded decoders created.
3194 static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata)
3199 for (i = 0; i < num_cpu; i++) {
3200 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3201 switch (cs_etm_magic) {
3202 case __perf_cs_etmv3_magic:
3203 if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3204 metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3206 case __perf_cs_etmv4_magic:
3207 case __perf_cs_ete_magic:
3208 if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3209 metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3212 /* unknown magic number */
3219 int cs_etm__process_auxtrace_info_full(union perf_event *event,
3220 struct perf_session *session)
3222 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3223 struct cs_etm_auxtrace *etm = NULL;
3224 struct perf_record_time_conv *tc = &session->time_conv;
3225 int event_header_size = sizeof(struct perf_event_header);
3226 int total_size = auxtrace_info->header.size;
3230 int aux_hw_id_found;
3233 u64 **metadata = NULL;
3236 * Create an RB tree for traceID-metadata tuple. Since the conversion
3237 * has to be made for each packet that gets decoded, optimizing access
3238 * in anything other than a sequential array is worth doing.
3240 traceid_list = intlist__new(NULL);
3244 /* First the global part */
3245 ptr = (u64 *) auxtrace_info->priv;
3246 num_cpu = ptr[CS_PMU_TYPE_CPUS] & 0xffffffff;
3247 metadata = zalloc(sizeof(*metadata) * num_cpu);
3250 goto err_free_traceid_list;
3253 /* Start parsing after the common part of the header */
3254 i = CS_HEADER_VERSION_MAX;
3257 * The metadata is stored in the auxtrace_info section and encodes
3258 * the configuration of the ARM embedded trace macrocell which is
3259 * required by the trace decoder to properly decode the trace due
3260 * to its highly compressed nature.
3262 for (j = 0; j < num_cpu; j++) {
3263 if (ptr[i] == __perf_cs_etmv3_magic) {
3265 cs_etm__create_meta_blk(ptr, &i,
3267 CS_ETM_NR_TRC_PARAMS_V0);
3268 } else if (ptr[i] == __perf_cs_etmv4_magic) {
3270 cs_etm__create_meta_blk(ptr, &i,
3272 CS_ETMV4_NR_TRC_PARAMS_V0);
3273 } else if (ptr[i] == __perf_cs_ete_magic) {
3274 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
3276 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
3279 goto err_free_metadata;
3284 goto err_free_metadata;
3289 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
3290 * CS_ETMV4_PRIV_MAX mark how many double words are in the
3291 * global metadata, and each cpu's metadata respectively.
3292 * The following tests if the correct number of double words was
3293 * present in the auxtrace info section.
3295 priv_size = total_size - event_header_size - INFO_HEADER_SIZE;
3296 if (i * 8 != priv_size) {
3298 goto err_free_metadata;
3301 etm = zalloc(sizeof(*etm));
3305 goto err_free_metadata;
3309 * As all the ETMs run at the same exception level, the system should
3310 * have the same PID format crossing CPUs. So cache the PID format
3311 * and reuse it for sequential decoding.
3313 etm->pid_fmt = cs_etm__init_pid_fmt(metadata[0]);
3315 err = auxtrace_queues__init(&etm->queues);
3319 if (session->itrace_synth_opts->set) {
3320 etm->synth_opts = *session->itrace_synth_opts;
3322 itrace_synth_opts__set_default(&etm->synth_opts,
3323 session->itrace_synth_opts->default_no_sample);
3324 etm->synth_opts.callchain = false;
3327 etm->session = session;
3329 etm->num_cpu = num_cpu;
3330 etm->pmu_type = (unsigned int) ((ptr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
3331 etm->snapshot_mode = (ptr[CS_ETM_SNAPSHOT] != 0);
3332 etm->metadata = metadata;
3333 etm->auxtrace_type = auxtrace_info->type;
3335 /* Use virtual timestamps if all ETMs report ts_source = 1 */
3336 etm->has_virtual_ts = cs_etm__has_virtual_ts(metadata, num_cpu);
3338 if (!etm->has_virtual_ts)
3339 ui__warning("Virtual timestamps are not enabled, or not supported by the traced system.\n"
3340 "The time field of the samples will not be set accurately.\n\n");
3342 etm->auxtrace.process_event = cs_etm__process_event;
3343 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3344 etm->auxtrace.flush_events = cs_etm__flush_events;
3345 etm->auxtrace.free_events = cs_etm__free_events;
3346 etm->auxtrace.free = cs_etm__free;
3347 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3348 session->auxtrace = &etm->auxtrace;
3350 err = cs_etm__setup_timeless_decoding(etm);
3354 etm->tc.time_shift = tc->time_shift;
3355 etm->tc.time_mult = tc->time_mult;
3356 etm->tc.time_zero = tc->time_zero;
3357 if (event_contains(*tc, time_cycles)) {
3358 etm->tc.time_cycles = tc->time_cycles;
3359 etm->tc.time_mask = tc->time_mask;
3360 etm->tc.cap_user_time_zero = tc->cap_user_time_zero;
3361 etm->tc.cap_user_time_short = tc->cap_user_time_short;
3363 err = cs_etm__synth_events(etm, session);
3365 goto err_free_queues;
3368 * Map Trace ID values to CPU metadata.
3370 * Trace metadata will always contain Trace ID values from the legacy algorithm. If the
3371 * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata
3372 * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set.
3374 * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use
3375 * the same IDs as the old algorithm as far as is possible, unless there are clashes
3376 * in which case a different value will be used. This means an older perf may still
3377 * be able to record and read files generate on a newer system.
3379 * For a perf able to interpret AUX_HW_ID packets we first check for the presence of
3380 * those packets. If they are there then the values will be mapped and plugged into
3381 * the metadata. We then set any remaining metadata values with the used flag to a
3382 * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required.
3384 * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3385 * then we map Trace ID values to CPU directly from the metadata - clearing any unused
3389 /* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */
3390 aux_hw_id_found = 0;
3391 err = perf_session__peek_events(session, session->header.data_offset,
3392 session->header.data_size,
3393 cs_etm__process_aux_hw_id_cb, &aux_hw_id_found);
3395 goto err_free_queues;
3397 /* if HW ID found then clear any unused metadata ID values */
3398 if (aux_hw_id_found)
3399 err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata);
3400 /* otherwise, this is a file with metadata values only, map from metadata */
3402 err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3405 goto err_free_queues;
3407 err = cs_etm__queue_aux_records(session);
3409 goto err_free_queues;
3411 etm->data_queued = etm->queues.populated;
3415 auxtrace_queues__free(&etm->queues);
3416 session->auxtrace = NULL;
3420 /* No need to check @metadata[j], free(NULL) is supported */
3421 for (j = 0; j < num_cpu; j++)
3422 zfree(&metadata[j]);
3424 err_free_traceid_list:
3425 intlist__delete(traceid_list);