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/kernel.h>
10 #include <linux/bitfield.h>
11 #include <linux/bitops.h>
12 #include <linux/coresight-pmu.h>
13 #include <linux/err.h>
14 #include <linux/log2.h>
15 #include <linux/types.h>
16 #include <linux/zalloc.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 perf_tsc_conversion tc;
52 * Timeless has no timestamps in the trace so overlapping mmap lookups
53 * are less accurate but produces smaller trace data. We use context IDs
54 * in the trace instead of matching timestamps with fork records so
55 * they're not really needed in the general case. Overlapping mmaps
56 * happen in cases like between a fork and an exec.
58 bool timeless_decoding;
61 * Per-thread ignores the trace channel ID and instead assumes that
62 * everything in a buffer comes from the same process regardless of
63 * which CPU it ran on. It also implies no context IDs so the TID is
64 * taken from the auxtrace buffer.
66 bool per_thread_decoding;
69 bool has_virtual_ts; /* Virtual/Kernel timestamps in the trace. */
72 u64 latest_kernel_timestamp;
74 u64 branches_sample_type;
76 u64 instructions_sample_type;
77 u64 instructions_sample_period;
80 unsigned int pmu_type;
81 enum cs_etm_pid_fmt pid_fmt;
84 struct cs_etm_traceid_queue {
86 u64 period_instructions;
87 size_t last_branch_pos;
88 union perf_event *event_buf;
89 struct thread *thread;
90 struct thread *prev_packet_thread;
91 ocsd_ex_level prev_packet_el;
93 struct branch_stack *last_branch;
94 struct branch_stack *last_branch_rb;
95 struct cs_etm_packet *prev_packet;
96 struct cs_etm_packet *packet;
97 struct cs_etm_packet_queue packet_queue;
100 struct cs_etm_queue {
101 struct cs_etm_auxtrace *etm;
102 struct cs_etm_decoder *decoder;
103 struct auxtrace_buffer *buffer;
104 unsigned int queue_nr;
105 u8 pending_timestamp_chan_id;
107 const unsigned char *buf;
108 size_t buf_len, buf_used;
109 /* Conversion between traceID and index in traceid_queues array */
110 struct intlist *traceid_queues_list;
111 struct cs_etm_traceid_queue **traceid_queues;
114 /* RB tree for quick conversion between traceID and metadata pointers */
115 static struct intlist *traceid_list;
117 static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm);
118 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
120 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
121 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
123 /* PTMs ETMIDR [11:8] set to b0011 */
124 #define ETMIDR_PTM_VERSION 0x00000300
127 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
128 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
129 * encode the etm queue number as the upper 16 bit and the channel as
132 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
133 (queue_nr << 16 | trace_chan_id)
134 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
135 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
137 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
139 etmidr &= ETMIDR_PTM_VERSION;
141 if (etmidr == ETMIDR_PTM_VERSION)
142 return CS_ETM_PROTO_PTM;
144 return CS_ETM_PROTO_ETMV3;
147 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
149 struct int_node *inode;
152 inode = intlist__find(traceid_list, trace_chan_id);
156 metadata = inode->priv;
157 *magic = metadata[CS_ETM_MAGIC];
161 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
163 struct int_node *inode;
166 inode = intlist__find(traceid_list, trace_chan_id);
170 metadata = inode->priv;
171 *cpu = (int)metadata[CS_ETM_CPU];
176 * The returned PID format is presented as an enum:
178 * CS_ETM_PIDFMT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced.
179 * CS_ETM_PIDFMT_CTXTID2: CONTEXTIDR_EL2 is traced.
180 * CS_ETM_PIDFMT_NONE: No context IDs
182 * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
183 * are enabled at the same time when the session runs on an EL2 kernel.
184 * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
185 * recorded in the trace data, the tool will selectively use
186 * CONTEXTIDR_EL2 as PID.
188 * The result is cached in etm->pid_fmt so this function only needs to be called
189 * when processing the aux info.
191 static enum cs_etm_pid_fmt cs_etm__init_pid_fmt(u64 *metadata)
195 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
196 val = metadata[CS_ETM_ETMCR];
197 /* CONTEXTIDR is traced */
198 if (val & BIT(ETM_OPT_CTXTID))
199 return CS_ETM_PIDFMT_CTXTID;
201 val = metadata[CS_ETMV4_TRCCONFIGR];
202 /* CONTEXTIDR_EL2 is traced */
203 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
204 return CS_ETM_PIDFMT_CTXTID2;
205 /* CONTEXTIDR_EL1 is traced */
206 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
207 return CS_ETM_PIDFMT_CTXTID;
210 return CS_ETM_PIDFMT_NONE;
213 enum cs_etm_pid_fmt cs_etm__get_pid_fmt(struct cs_etm_queue *etmq)
215 return etmq->etm->pid_fmt;
218 static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
220 struct int_node *inode;
222 /* Get an RB node for this CPU */
223 inode = intlist__findnew(traceid_list, trace_chan_id);
225 /* Something went wrong, no need to continue */
230 * The node for that CPU should not be taken.
231 * Back out if that's the case.
236 /* All good, associate the traceID with the metadata pointer */
237 inode->priv = cpu_metadata;
242 static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata)
244 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
246 switch (cs_etm_magic) {
247 case __perf_cs_etmv3_magic:
248 *trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] &
249 CORESIGHT_TRACE_ID_VAL_MASK);
251 case __perf_cs_etmv4_magic:
252 case __perf_cs_ete_magic:
253 *trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] &
254 CORESIGHT_TRACE_ID_VAL_MASK);
263 * update metadata trace ID from the value found in the AUX_HW_INFO packet.
264 * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present.
266 static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
268 u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
270 switch (cs_etm_magic) {
271 case __perf_cs_etmv3_magic:
272 cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id;
274 case __perf_cs_etmv4_magic:
275 case __perf_cs_ete_magic:
276 cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id;
286 * Get a metadata for a specific cpu from an array.
289 static u64 *get_cpu_data(struct cs_etm_auxtrace *etm, int cpu)
292 u64 *metadata = NULL;
294 for (i = 0; i < etm->num_cpu; i++) {
295 if (etm->metadata[i][CS_ETM_CPU] == (u64)cpu) {
296 metadata = etm->metadata[i];
305 * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event.
307 * The payload associates the Trace ID and the CPU.
308 * The routine is tolerant of seeing multiple packets with the same association,
309 * but a CPU / Trace ID association changing during a session is an error.
311 static int cs_etm__process_aux_output_hw_id(struct perf_session *session,
312 union perf_event *event)
314 struct cs_etm_auxtrace *etm;
315 struct perf_sample sample;
316 struct int_node *inode;
320 int cpu, version, err;
321 u8 trace_chan_id, curr_chan_id;
323 /* extract and parse the HW ID */
324 hw_id = event->aux_output_hw_id.hw_id;
325 version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id);
326 trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id);
328 /* check that we can handle this version */
329 if (version > CS_AUX_HW_ID_CURR_VERSION)
332 /* get access to the etm metadata */
333 etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
334 if (!etm || !etm->metadata)
337 /* parse the sample to get the CPU */
338 evsel = evlist__event2evsel(session->evlist, event);
341 err = evsel__parse_sample(evsel, event, &sample);
346 /* no CPU in the sample - possibly recorded with an old version of perf */
347 pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record.");
351 /* See if the ID is mapped to a CPU, and it matches the current CPU */
352 inode = intlist__find(traceid_list, trace_chan_id);
354 cpu_data = inode->priv;
355 if ((int)cpu_data[CS_ETM_CPU] != cpu) {
356 pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n");
360 /* check that the mapped ID matches */
361 err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data);
364 if (curr_chan_id != trace_chan_id) {
365 pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n");
369 /* mapped and matched - return OK */
373 cpu_data = get_cpu_data(etm, cpu);
374 if (cpu_data == NULL)
377 /* not one we've seen before - lets map it */
378 err = cs_etm__map_trace_id(trace_chan_id, cpu_data);
383 * if we are picking up the association from the packet, need to plug
384 * the correct trace ID into the metadata for setting up decoders later.
386 err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data);
390 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
394 * When a timestamp packet is encountered the backend code
395 * is stopped so that the front end has time to process packets
396 * that were accumulated in the traceID queue. Since there can
397 * be more than one channel per cs_etm_queue, we need to specify
398 * what traceID queue needs servicing.
400 etmq->pending_timestamp_chan_id = trace_chan_id;
403 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
406 struct cs_etm_packet_queue *packet_queue;
408 if (!etmq->pending_timestamp_chan_id)
412 *trace_chan_id = etmq->pending_timestamp_chan_id;
414 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
415 etmq->pending_timestamp_chan_id);
419 /* Acknowledge pending status */
420 etmq->pending_timestamp_chan_id = 0;
422 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
423 return packet_queue->cs_timestamp;
426 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
432 queue->packet_count = 0;
433 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
434 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
435 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
436 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
437 queue->packet_buffer[i].instr_count = 0;
438 queue->packet_buffer[i].last_instr_taken_branch = false;
439 queue->packet_buffer[i].last_instr_size = 0;
440 queue->packet_buffer[i].last_instr_type = 0;
441 queue->packet_buffer[i].last_instr_subtype = 0;
442 queue->packet_buffer[i].last_instr_cond = 0;
443 queue->packet_buffer[i].flags = 0;
444 queue->packet_buffer[i].exception_number = UINT32_MAX;
445 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
446 queue->packet_buffer[i].cpu = INT_MIN;
450 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
453 struct int_node *inode;
454 struct cs_etm_traceid_queue *tidq;
455 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
457 intlist__for_each_entry(inode, traceid_queues_list) {
458 idx = (int)(intptr_t)inode->priv;
459 tidq = etmq->traceid_queues[idx];
460 cs_etm__clear_packet_queue(&tidq->packet_queue);
464 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
465 struct cs_etm_traceid_queue *tidq,
469 struct auxtrace_queue *queue;
470 struct cs_etm_auxtrace *etm = etmq->etm;
472 cs_etm__clear_packet_queue(&tidq->packet_queue);
474 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
475 tidq->trace_chan_id = trace_chan_id;
476 tidq->el = tidq->prev_packet_el = ocsd_EL_unknown;
477 tidq->thread = machine__findnew_thread(&etm->session->machines.host, -1,
479 tidq->prev_packet_thread = machine__idle_thread(&etm->session->machines.host);
481 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
485 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
486 if (!tidq->prev_packet)
489 if (etm->synth_opts.last_branch) {
490 size_t sz = sizeof(struct branch_stack);
492 sz += etm->synth_opts.last_branch_sz *
493 sizeof(struct branch_entry);
494 tidq->last_branch = zalloc(sz);
495 if (!tidq->last_branch)
497 tidq->last_branch_rb = zalloc(sz);
498 if (!tidq->last_branch_rb)
502 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
503 if (!tidq->event_buf)
509 zfree(&tidq->last_branch_rb);
510 zfree(&tidq->last_branch);
511 zfree(&tidq->prev_packet);
512 zfree(&tidq->packet);
517 static struct cs_etm_traceid_queue
518 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
521 struct int_node *inode;
522 struct intlist *traceid_queues_list;
523 struct cs_etm_traceid_queue *tidq, **traceid_queues;
524 struct cs_etm_auxtrace *etm = etmq->etm;
526 if (etm->per_thread_decoding)
527 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
529 traceid_queues_list = etmq->traceid_queues_list;
532 * Check if the traceid_queue exist for this traceID by looking
535 inode = intlist__find(traceid_queues_list, trace_chan_id);
537 idx = (int)(intptr_t)inode->priv;
538 return etmq->traceid_queues[idx];
541 /* We couldn't find a traceid_queue for this traceID, allocate one */
542 tidq = malloc(sizeof(*tidq));
546 memset(tidq, 0, sizeof(*tidq));
548 /* Get a valid index for the new traceid_queue */
549 idx = intlist__nr_entries(traceid_queues_list);
550 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
551 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
555 /* Associate this traceID with this index */
556 inode->priv = (void *)(intptr_t)idx;
558 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
561 /* Grow the traceid_queues array by one unit */
562 traceid_queues = etmq->traceid_queues;
563 traceid_queues = reallocarray(traceid_queues,
565 sizeof(*traceid_queues));
568 * On failure reallocarray() returns NULL and the original block of
569 * memory is left untouched.
574 traceid_queues[idx] = tidq;
575 etmq->traceid_queues = traceid_queues;
577 return etmq->traceid_queues[idx];
581 * Function intlist__remove() removes the inode from the list
582 * and delete the memory associated to it.
584 intlist__remove(traceid_queues_list, inode);
590 struct cs_etm_packet_queue
591 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
593 struct cs_etm_traceid_queue *tidq;
595 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
597 return &tidq->packet_queue;
602 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
603 struct cs_etm_traceid_queue *tidq)
605 struct cs_etm_packet *tmp;
607 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
608 etm->synth_opts.instructions) {
610 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
611 * the next incoming packet.
613 * Threads and exception levels are also tracked for both the
614 * previous and current packets. This is because the previous
615 * packet is used for the 'from' IP for branch samples, so the
616 * thread at that time must also be assigned to that sample.
617 * Across discontinuity packets the thread can change, so by
618 * tracking the thread for the previous packet the branch sample
619 * will have the correct info.
622 tidq->packet = tidq->prev_packet;
623 tidq->prev_packet = tmp;
624 tidq->prev_packet_el = tidq->el;
625 thread__put(tidq->prev_packet_thread);
626 tidq->prev_packet_thread = thread__get(tidq->thread);
630 static void cs_etm__packet_dump(const char *pkt_string)
632 const char *color = PERF_COLOR_BLUE;
633 int len = strlen(pkt_string);
635 if (len && (pkt_string[len-1] == '\n'))
636 color_fprintf(stdout, color, " %s", pkt_string);
638 color_fprintf(stdout, color, " %s\n", pkt_string);
643 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
644 struct cs_etm_auxtrace *etm, int idx,
647 u64 **metadata = etm->metadata;
649 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
650 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
651 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
654 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
655 struct cs_etm_auxtrace *etm, int idx)
657 u64 **metadata = etm->metadata;
659 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
660 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
661 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
662 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
663 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
664 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
665 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
668 static void cs_etm__set_trace_param_ete(struct cs_etm_trace_params *t_params,
669 struct cs_etm_auxtrace *etm, int idx)
671 u64 **metadata = etm->metadata;
673 t_params[idx].protocol = CS_ETM_PROTO_ETE;
674 t_params[idx].ete.reg_idr0 = metadata[idx][CS_ETE_TRCIDR0];
675 t_params[idx].ete.reg_idr1 = metadata[idx][CS_ETE_TRCIDR1];
676 t_params[idx].ete.reg_idr2 = metadata[idx][CS_ETE_TRCIDR2];
677 t_params[idx].ete.reg_idr8 = metadata[idx][CS_ETE_TRCIDR8];
678 t_params[idx].ete.reg_configr = metadata[idx][CS_ETE_TRCCONFIGR];
679 t_params[idx].ete.reg_traceidr = metadata[idx][CS_ETE_TRCTRACEIDR];
680 t_params[idx].ete.reg_devarch = metadata[idx][CS_ETE_TRCDEVARCH];
683 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
684 struct cs_etm_auxtrace *etm,
691 for (i = 0; i < decoders; i++) {
692 architecture = etm->metadata[i][CS_ETM_MAGIC];
694 switch (architecture) {
695 case __perf_cs_etmv3_magic:
696 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
697 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
699 case __perf_cs_etmv4_magic:
700 cs_etm__set_trace_param_etmv4(t_params, etm, i);
702 case __perf_cs_ete_magic:
703 cs_etm__set_trace_param_ete(t_params, etm, i);
713 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
714 struct cs_etm_queue *etmq,
715 enum cs_etm_decoder_operation mode,
720 if (!(mode < CS_ETM_OPERATION_MAX))
723 d_params->packet_printer = cs_etm__packet_dump;
724 d_params->operation = mode;
725 d_params->data = etmq;
726 d_params->formatted = formatted;
727 d_params->fsyncs = false;
728 d_params->hsyncs = false;
729 d_params->frame_aligned = true;
736 static void cs_etm__dump_event(struct cs_etm_queue *etmq,
737 struct auxtrace_buffer *buffer)
740 const char *color = PERF_COLOR_BLUE;
741 size_t buffer_used = 0;
743 fprintf(stdout, "\n");
744 color_fprintf(stdout, color,
745 ". ... CoreSight %s Trace data: size %#zx bytes\n",
746 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
751 ret = cs_etm_decoder__process_data_block(
752 etmq->decoder, buffer->offset,
753 &((u8 *)buffer->data)[buffer_used],
754 buffer->size - buffer_used, &consumed);
758 buffer_used += consumed;
759 } while (buffer_used < buffer->size);
761 cs_etm_decoder__reset(etmq->decoder);
764 static int cs_etm__flush_events(struct perf_session *session,
765 struct perf_tool *tool)
767 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
768 struct cs_etm_auxtrace,
773 if (!tool->ordered_events)
776 if (etm->timeless_decoding) {
778 * Pass tid = -1 to process all queues. But likely they will have
779 * already been processed on PERF_RECORD_EXIT anyway.
781 return cs_etm__process_timeless_queues(etm, -1);
784 return cs_etm__process_timestamped_queues(etm);
787 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
791 struct int_node *inode, *tmp;
792 struct cs_etm_traceid_queue *tidq;
793 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
795 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
796 priv = (uintptr_t)inode->priv;
799 /* Free this traceid_queue from the array */
800 tidq = etmq->traceid_queues[idx];
801 thread__zput(tidq->thread);
802 thread__zput(tidq->prev_packet_thread);
803 zfree(&tidq->event_buf);
804 zfree(&tidq->last_branch);
805 zfree(&tidq->last_branch_rb);
806 zfree(&tidq->prev_packet);
807 zfree(&tidq->packet);
811 * Function intlist__remove() removes the inode from the list
812 * and delete the memory associated to it.
814 intlist__remove(traceid_queues_list, inode);
817 /* Then the RB tree itself */
818 intlist__delete(traceid_queues_list);
819 etmq->traceid_queues_list = NULL;
821 /* finally free the traceid_queues array */
822 zfree(&etmq->traceid_queues);
825 static void cs_etm__free_queue(void *priv)
827 struct cs_etm_queue *etmq = priv;
832 cs_etm_decoder__free(etmq->decoder);
833 cs_etm__free_traceid_queues(etmq);
837 static void cs_etm__free_events(struct perf_session *session)
840 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
841 struct cs_etm_auxtrace,
843 struct auxtrace_queues *queues = &aux->queues;
845 for (i = 0; i < queues->nr_queues; i++) {
846 cs_etm__free_queue(queues->queue_array[i].priv);
847 queues->queue_array[i].priv = NULL;
850 auxtrace_queues__free(queues);
853 static void cs_etm__free(struct perf_session *session)
856 struct int_node *inode, *tmp;
857 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
858 struct cs_etm_auxtrace,
860 cs_etm__free_events(session);
861 session->auxtrace = NULL;
863 /* First remove all traceID/metadata nodes for the RB tree */
864 intlist__for_each_entry_safe(inode, tmp, traceid_list)
865 intlist__remove(traceid_list, inode);
866 /* Then the RB tree itself */
867 intlist__delete(traceid_list);
869 for (i = 0; i < aux->num_cpu; i++)
870 zfree(&aux->metadata[i]);
872 zfree(&aux->metadata);
876 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
879 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
880 struct cs_etm_auxtrace,
883 return evsel->core.attr.type == aux->pmu_type;
886 static struct machine *cs_etm__get_machine(struct cs_etm_queue *etmq,
889 enum cs_etm_pid_fmt pid_fmt = cs_etm__get_pid_fmt(etmq);
892 * For any virtualisation based on nVHE (e.g. pKVM), or host kernels
893 * running at EL1 assume everything is the host.
895 if (pid_fmt == CS_ETM_PIDFMT_CTXTID)
896 return &etmq->etm->session->machines.host;
899 * Not perfect, but otherwise assume anything in EL1 is the default
900 * guest, and everything else is the host. Distinguishing between guest
901 * and host userspaces isn't currently supported either. Neither is
902 * multiple guest support. All this does is reduce the likeliness of
903 * decode errors where we look into the host kernel maps when it should
904 * have been the guest maps.
908 return machines__find_guest(&etmq->etm->session->machines,
909 DEFAULT_GUEST_KERNEL_ID);
913 case ocsd_EL_unknown:
915 return &etmq->etm->session->machines.host;
919 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address,
922 struct machine *machine = cs_etm__get_machine(etmq, el);
924 if (address >= machine__kernel_start(machine)) {
925 if (machine__is_host(machine))
926 return PERF_RECORD_MISC_KERNEL;
928 return PERF_RECORD_MISC_GUEST_KERNEL;
930 if (machine__is_host(machine))
931 return PERF_RECORD_MISC_USER;
934 * Can't really happen at the moment because
935 * cs_etm__get_machine() will always return
936 * machines.host for any non EL1 trace.
938 return PERF_RECORD_MISC_GUEST_USER;
943 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
944 u64 address, size_t size, u8 *buffer,
945 const ocsd_mem_space_acc_t mem_space)
950 struct addr_location al;
952 struct cs_etm_traceid_queue *tidq;
958 addr_location__init(&al);
959 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
964 * We've already tracked EL along side the PID in cs_etm__set_thread()
965 * so double check that it matches what OpenCSD thinks as well. It
966 * doesn't distinguish between EL0 and EL1 for this mem access callback
967 * so we had to do the extra tracking. Skip validation if it's any of
970 if (!(mem_space == OCSD_MEM_SPACE_ANY ||
971 mem_space == OCSD_MEM_SPACE_N || mem_space == OCSD_MEM_SPACE_S)) {
972 if (mem_space & OCSD_MEM_SPACE_EL1N) {
973 /* Includes both non secure EL1 and EL0 */
974 assert(tidq->el == ocsd_EL1 || tidq->el == ocsd_EL0);
975 } else if (mem_space & OCSD_MEM_SPACE_EL2)
976 assert(tidq->el == ocsd_EL2);
977 else if (mem_space & OCSD_MEM_SPACE_EL3)
978 assert(tidq->el == ocsd_EL3);
981 cpumode = cs_etm__cpu_mode(etmq, address, tidq->el);
983 if (!thread__find_map(tidq->thread, cpumode, address, &al))
986 dso = map__dso(al.map);
990 if (dso->data.status == DSO_DATA_STATUS_ERROR &&
991 dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE))
994 offset = map__map_ip(al.map, address);
998 len = dso__data_read_offset(dso, maps__machine(thread__maps(tidq->thread)),
999 offset, buffer, size);
1002 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
1003 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
1004 if (!dso->auxtrace_warned) {
1005 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
1007 dso->long_name ? dso->long_name : "Unknown");
1008 dso->auxtrace_warned = true;
1014 addr_location__exit(&al);
1018 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
1021 struct cs_etm_decoder_params d_params;
1022 struct cs_etm_trace_params *t_params = NULL;
1023 struct cs_etm_queue *etmq;
1025 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
1028 int decoders = formatted ? etm->num_cpu : 1;
1030 etmq = zalloc(sizeof(*etmq));
1034 etmq->traceid_queues_list = intlist__new(NULL);
1035 if (!etmq->traceid_queues_list)
1038 /* Use metadata to fill in trace parameters for trace decoder */
1039 t_params = zalloc(sizeof(*t_params) * decoders);
1044 if (cs_etm__init_trace_params(t_params, etm, decoders))
1047 /* Set decoder parameters to decode trace packets */
1048 if (cs_etm__init_decoder_params(&d_params, etmq,
1049 dump_trace ? CS_ETM_OPERATION_PRINT :
1050 CS_ETM_OPERATION_DECODE,
1054 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
1061 * Register a function to handle all memory accesses required by
1062 * the trace decoder library.
1064 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
1066 cs_etm__mem_access))
1067 goto out_free_decoder;
1073 cs_etm_decoder__free(etmq->decoder);
1075 intlist__delete(etmq->traceid_queues_list);
1081 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
1082 struct auxtrace_queue *queue,
1083 unsigned int queue_nr,
1086 struct cs_etm_queue *etmq = queue->priv;
1088 if (list_empty(&queue->head) || etmq)
1091 etmq = cs_etm__alloc_queue(etm, formatted);
1098 etmq->queue_nr = queue_nr;
1104 static int cs_etm__queue_first_cs_timestamp(struct cs_etm_auxtrace *etm,
1105 struct cs_etm_queue *etmq,
1106 unsigned int queue_nr)
1109 unsigned int cs_queue_nr;
1114 * We are under a CPU-wide trace scenario. As such we need to know
1115 * when the code that generated the traces started to execute so that
1116 * it can be correlated with execution on other CPUs. So we get a
1117 * handle on the beginning of traces and decode until we find a
1118 * timestamp. The timestamp is then added to the auxtrace min heap
1119 * in order to know what nibble (of all the etmqs) to decode first.
1123 * Fetch an aux_buffer from this etmq. Bail if no more
1124 * blocks or an error has been encountered.
1126 ret = cs_etm__get_data_block(etmq);
1131 * Run decoder on the trace block. The decoder will stop when
1132 * encountering a CS timestamp, a full packet queue or the end of
1133 * trace for that block.
1135 ret = cs_etm__decode_data_block(etmq);
1140 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
1141 * the timestamp calculation for us.
1143 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
1145 /* We found a timestamp, no need to continue. */
1150 * We didn't find a timestamp so empty all the traceid packet
1151 * queues before looking for another timestamp packet, either
1152 * in the current data block or a new one. Packets that were
1153 * just decoded are useless since no timestamp has been
1154 * associated with them. As such simply discard them.
1156 cs_etm__clear_all_packet_queues(etmq);
1160 * We have a timestamp. Add it to the min heap to reflect when
1161 * instructions conveyed by the range packets of this traceID queue
1162 * started to execute. Once the same has been done for all the traceID
1163 * queues of each etmq, redenring and decoding can start in
1164 * chronological order.
1166 * Note that packets decoded above are still in the traceID's packet
1167 * queue and will be processed in cs_etm__process_timestamped_queues().
1169 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
1170 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
1176 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
1177 struct cs_etm_traceid_queue *tidq)
1179 struct branch_stack *bs_src = tidq->last_branch_rb;
1180 struct branch_stack *bs_dst = tidq->last_branch;
1184 * Set the number of records before early exit: ->nr is used to
1185 * determine how many branches to copy from ->entries.
1187 bs_dst->nr = bs_src->nr;
1190 * Early exit when there is nothing to copy.
1196 * As bs_src->entries is a circular buffer, we need to copy from it in
1197 * two steps. First, copy the branches from the most recently inserted
1198 * branch ->last_branch_pos until the end of bs_src->entries buffer.
1200 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
1201 memcpy(&bs_dst->entries[0],
1202 &bs_src->entries[tidq->last_branch_pos],
1203 sizeof(struct branch_entry) * nr);
1206 * If we wrapped around at least once, the branches from the beginning
1207 * of the bs_src->entries buffer and until the ->last_branch_pos element
1208 * are older valid branches: copy them over. The total number of
1209 * branches copied over will be equal to the number of branches asked by
1210 * the user in last_branch_sz.
1212 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
1213 memcpy(&bs_dst->entries[nr],
1214 &bs_src->entries[0],
1215 sizeof(struct branch_entry) * tidq->last_branch_pos);
1220 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
1222 tidq->last_branch_pos = 0;
1223 tidq->last_branch_rb->nr = 0;
1226 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
1227 u8 trace_chan_id, u64 addr)
1231 cs_etm__mem_access(etmq, trace_chan_id, addr, ARRAY_SIZE(instrBytes),
1234 * T32 instruction size is indicated by bits[15:11] of the first
1235 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
1236 * denote a 32-bit instruction.
1238 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
1241 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
1243 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1244 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1247 return packet->start_addr;
1251 u64 cs_etm__last_executed_instr(const 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->end_addr - packet->last_instr_size;
1260 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
1262 const struct cs_etm_packet *packet,
1265 if (packet->isa == CS_ETM_ISA_T32) {
1266 u64 addr = packet->start_addr;
1269 addr += cs_etm__t32_instr_size(etmq,
1270 trace_chan_id, addr);
1276 /* Assume a 4 byte instruction size (A32/A64) */
1277 return packet->start_addr + offset * 4;
1280 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1281 struct cs_etm_traceid_queue *tidq)
1283 struct branch_stack *bs = tidq->last_branch_rb;
1284 struct branch_entry *be;
1287 * The branches are recorded in a circular buffer in reverse
1288 * chronological order: we start recording from the last element of the
1289 * buffer down. After writing the first element of the stack, move the
1290 * insert position back to the end of the buffer.
1292 if (!tidq->last_branch_pos)
1293 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1295 tidq->last_branch_pos -= 1;
1297 be = &bs->entries[tidq->last_branch_pos];
1298 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1299 be->to = cs_etm__first_executed_instr(tidq->packet);
1300 /* No support for mispredict */
1301 be->flags.mispred = 0;
1302 be->flags.predicted = 1;
1305 * Increment bs->nr until reaching the number of last branches asked by
1306 * the user on the command line.
1308 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1312 static int cs_etm__inject_event(union perf_event *event,
1313 struct perf_sample *sample, u64 type)
1315 event->header.size = perf_event__sample_event_size(sample, type, 0);
1316 return perf_event__synthesize_sample(event, type, 0, sample);
1321 cs_etm__get_trace(struct cs_etm_queue *etmq)
1323 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1324 struct auxtrace_buffer *old_buffer = aux_buffer;
1325 struct auxtrace_queue *queue;
1327 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1329 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1331 /* If no more data, drop the previous auxtrace_buffer and return */
1334 auxtrace_buffer__drop_data(old_buffer);
1339 etmq->buffer = aux_buffer;
1341 /* If the aux_buffer doesn't have data associated, try to load it */
1342 if (!aux_buffer->data) {
1343 /* get the file desc associated with the perf data file */
1344 int fd = perf_data__fd(etmq->etm->session->data);
1346 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1347 if (!aux_buffer->data)
1351 /* If valid, drop the previous buffer */
1353 auxtrace_buffer__drop_data(old_buffer);
1356 etmq->buf_len = aux_buffer->size;
1357 etmq->buf = aux_buffer->data;
1359 return etmq->buf_len;
1362 static void cs_etm__set_thread(struct cs_etm_queue *etmq,
1363 struct cs_etm_traceid_queue *tidq, pid_t tid,
1366 struct machine *machine = cs_etm__get_machine(etmq, el);
1369 thread__zput(tidq->thread);
1370 tidq->thread = machine__find_thread(machine, -1, tid);
1373 /* Couldn't find a known thread */
1375 tidq->thread = machine__idle_thread(machine);
1380 int cs_etm__etmq_set_tid_el(struct cs_etm_queue *etmq, pid_t tid,
1381 u8 trace_chan_id, ocsd_ex_level el)
1383 struct cs_etm_traceid_queue *tidq;
1385 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1389 cs_etm__set_thread(etmq, tidq, tid, el);
1393 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1395 return !!etmq->etm->timeless_decoding;
1398 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1400 const struct cs_etm_packet *packet,
1401 struct perf_sample *sample)
1404 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1405 * packet, so directly bail out with 'insn_len' = 0.
1407 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1408 sample->insn_len = 0;
1413 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1414 * cs_etm__t32_instr_size().
1416 if (packet->isa == CS_ETM_ISA_T32)
1417 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1419 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1421 sample->insn_len = 4;
1423 cs_etm__mem_access(etmq, trace_chan_id, sample->ip, sample->insn_len,
1424 (void *)sample->insn, 0);
1427 u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp)
1429 struct cs_etm_auxtrace *etm = etmq->etm;
1431 if (etm->has_virtual_ts)
1432 return tsc_to_perf_time(cs_timestamp, &etm->tc);
1434 return cs_timestamp;
1437 static inline u64 cs_etm__resolve_sample_time(struct cs_etm_queue *etmq,
1438 struct cs_etm_traceid_queue *tidq)
1440 struct cs_etm_auxtrace *etm = etmq->etm;
1441 struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue;
1443 if (!etm->timeless_decoding && etm->has_virtual_ts)
1444 return packet_queue->cs_timestamp;
1446 return etm->latest_kernel_timestamp;
1449 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1450 struct cs_etm_traceid_queue *tidq,
1451 u64 addr, u64 period)
1454 struct cs_etm_auxtrace *etm = etmq->etm;
1455 union perf_event *event = tidq->event_buf;
1456 struct perf_sample sample = {.ip = 0,};
1458 event->sample.header.type = PERF_RECORD_SAMPLE;
1459 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr, tidq->el);
1460 event->sample.header.size = sizeof(struct perf_event_header);
1462 /* Set time field based on etm auxtrace config. */
1463 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1466 sample.pid = thread__pid(tidq->thread);
1467 sample.tid = thread__tid(tidq->thread);
1468 sample.id = etmq->etm->instructions_id;
1469 sample.stream_id = etmq->etm->instructions_id;
1470 sample.period = period;
1471 sample.cpu = tidq->packet->cpu;
1472 sample.flags = tidq->prev_packet->flags;
1473 sample.cpumode = event->sample.header.misc;
1475 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1477 if (etm->synth_opts.last_branch)
1478 sample.branch_stack = tidq->last_branch;
1480 if (etm->synth_opts.inject) {
1481 ret = cs_etm__inject_event(event, &sample,
1482 etm->instructions_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",
1498 * The cs etm packet encodes an instruction range between a branch target
1499 * and the next taken branch. Generate sample accordingly.
1501 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1502 struct cs_etm_traceid_queue *tidq)
1505 struct cs_etm_auxtrace *etm = etmq->etm;
1506 struct perf_sample sample = {.ip = 0,};
1507 union perf_event *event = tidq->event_buf;
1508 struct dummy_branch_stack {
1511 struct branch_entry entries;
1515 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1517 event->sample.header.type = PERF_RECORD_SAMPLE;
1518 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip,
1519 tidq->prev_packet_el);
1520 event->sample.header.size = sizeof(struct perf_event_header);
1522 /* Set time field based on etm auxtrace config. */
1523 sample.time = cs_etm__resolve_sample_time(etmq, tidq);
1526 sample.pid = thread__pid(tidq->prev_packet_thread);
1527 sample.tid = thread__tid(tidq->prev_packet_thread);
1528 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1529 sample.id = etmq->etm->branches_id;
1530 sample.stream_id = etmq->etm->branches_id;
1532 sample.cpu = tidq->packet->cpu;
1533 sample.flags = tidq->prev_packet->flags;
1534 sample.cpumode = event->sample.header.misc;
1536 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1540 * perf report cannot handle events without a branch stack
1542 if (etm->synth_opts.last_branch) {
1543 dummy_bs = (struct dummy_branch_stack){
1551 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1554 if (etm->synth_opts.inject) {
1555 ret = cs_etm__inject_event(event, &sample,
1556 etm->branches_sample_type);
1561 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1565 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1571 struct cs_etm_synth {
1572 struct perf_tool dummy_tool;
1573 struct perf_session *session;
1576 static int cs_etm__event_synth(struct perf_tool *tool,
1577 union perf_event *event,
1578 struct perf_sample *sample __maybe_unused,
1579 struct machine *machine __maybe_unused)
1581 struct cs_etm_synth *cs_etm_synth =
1582 container_of(tool, struct cs_etm_synth, dummy_tool);
1584 return perf_session__deliver_synth_event(cs_etm_synth->session,
1588 static int cs_etm__synth_event(struct perf_session *session,
1589 struct perf_event_attr *attr, u64 id)
1591 struct cs_etm_synth cs_etm_synth;
1593 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1594 cs_etm_synth.session = session;
1596 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1597 &id, cs_etm__event_synth);
1600 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1601 struct perf_session *session)
1603 struct evlist *evlist = session->evlist;
1604 struct evsel *evsel;
1605 struct perf_event_attr attr;
1610 evlist__for_each_entry(evlist, evsel) {
1611 if (evsel->core.attr.type == etm->pmu_type) {
1618 pr_debug("No selected events with CoreSight Trace data\n");
1622 memset(&attr, 0, sizeof(struct perf_event_attr));
1623 attr.size = sizeof(struct perf_event_attr);
1624 attr.type = PERF_TYPE_HARDWARE;
1625 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1626 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1628 if (etm->timeless_decoding)
1629 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1631 attr.sample_type |= PERF_SAMPLE_TIME;
1633 attr.exclude_user = evsel->core.attr.exclude_user;
1634 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1635 attr.exclude_hv = evsel->core.attr.exclude_hv;
1636 attr.exclude_host = evsel->core.attr.exclude_host;
1637 attr.exclude_guest = evsel->core.attr.exclude_guest;
1638 attr.sample_id_all = evsel->core.attr.sample_id_all;
1639 attr.read_format = evsel->core.attr.read_format;
1641 /* create new id val to be a fixed offset from evsel id */
1642 id = evsel->core.id[0] + 1000000000;
1647 if (etm->synth_opts.branches) {
1648 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1649 attr.sample_period = 1;
1650 attr.sample_type |= PERF_SAMPLE_ADDR;
1651 err = cs_etm__synth_event(session, &attr, id);
1654 etm->branches_sample_type = attr.sample_type;
1655 etm->branches_id = id;
1657 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1660 if (etm->synth_opts.last_branch) {
1661 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1663 * We don't use the hardware index, but the sample generation
1664 * code uses the new format branch_stack with this field,
1665 * so the event attributes must indicate that it's present.
1667 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1670 if (etm->synth_opts.instructions) {
1671 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1672 attr.sample_period = etm->synth_opts.period;
1673 etm->instructions_sample_period = attr.sample_period;
1674 err = cs_etm__synth_event(session, &attr, id);
1677 etm->instructions_sample_type = attr.sample_type;
1678 etm->instructions_id = id;
1685 static int cs_etm__sample(struct cs_etm_queue *etmq,
1686 struct cs_etm_traceid_queue *tidq)
1688 struct cs_etm_auxtrace *etm = etmq->etm;
1690 u8 trace_chan_id = tidq->trace_chan_id;
1693 /* Get instructions remainder from previous packet */
1694 instrs_prev = tidq->period_instructions;
1696 tidq->period_instructions += tidq->packet->instr_count;
1699 * Record a branch when the last instruction in
1700 * PREV_PACKET is a branch.
1702 if (etm->synth_opts.last_branch &&
1703 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1704 tidq->prev_packet->last_instr_taken_branch)
1705 cs_etm__update_last_branch_rb(etmq, tidq);
1707 if (etm->synth_opts.instructions &&
1708 tidq->period_instructions >= etm->instructions_sample_period) {
1710 * Emit instruction sample periodically
1711 * TODO: allow period to be defined in cycles and clock time
1715 * Below diagram demonstrates the instruction samples
1718 * Instrs Instrs Instrs Instrs
1719 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1722 * --------------------------------------------------
1726 * instructions(Pi) instructions(Pi')
1729 * \---------------- -----------------/
1731 * tidq->packet->instr_count
1733 * Instrs Sample(n...) are the synthesised samples occurring
1734 * every etm->instructions_sample_period instructions - as
1735 * defined on the perf command line. Sample(n) is being the
1736 * last sample before the current etm packet, n+1 to n+3
1737 * samples are generated from the current etm packet.
1739 * tidq->packet->instr_count represents the number of
1740 * instructions in the current etm packet.
1742 * Period instructions (Pi) contains the number of
1743 * instructions executed after the sample point(n) from the
1744 * previous etm packet. This will always be less than
1745 * etm->instructions_sample_period.
1747 * When generate new samples, it combines with two parts
1748 * instructions, one is the tail of the old packet and another
1749 * is the head of the new coming packet, to generate
1750 * sample(n+1); sample(n+2) and sample(n+3) consume the
1751 * instructions with sample period. After sample(n+3), the rest
1752 * instructions will be used by later packet and it is assigned
1753 * to tidq->period_instructions for next round calculation.
1757 * Get the initial offset into the current packet instructions;
1758 * entry conditions ensure that instrs_prev is less than
1759 * etm->instructions_sample_period.
1761 u64 offset = etm->instructions_sample_period - instrs_prev;
1764 /* Prepare last branches for instruction sample */
1765 if (etm->synth_opts.last_branch)
1766 cs_etm__copy_last_branch_rb(etmq, tidq);
1768 while (tidq->period_instructions >=
1769 etm->instructions_sample_period) {
1771 * Calculate the address of the sampled instruction (-1
1772 * as sample is reported as though instruction has just
1773 * been executed, but PC has not advanced to next
1776 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1777 tidq->packet, offset - 1);
1778 ret = cs_etm__synth_instruction_sample(
1780 etm->instructions_sample_period);
1784 offset += etm->instructions_sample_period;
1785 tidq->period_instructions -=
1786 etm->instructions_sample_period;
1790 if (etm->synth_opts.branches) {
1791 bool generate_sample = false;
1793 /* Generate sample for tracing on packet */
1794 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1795 generate_sample = true;
1797 /* Generate sample for branch taken packet */
1798 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1799 tidq->prev_packet->last_instr_taken_branch)
1800 generate_sample = true;
1802 if (generate_sample) {
1803 ret = cs_etm__synth_branch_sample(etmq, tidq);
1809 cs_etm__packet_swap(etm, tidq);
1814 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1817 * When the exception packet is inserted, whether the last instruction
1818 * in previous range packet is taken branch or not, we need to force
1819 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1820 * to generate branch sample for the instruction range before the
1821 * exception is trapped to kernel or before the exception returning.
1823 * The exception packet includes the dummy address values, so don't
1824 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1825 * for generating instruction and branch samples.
1827 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1828 tidq->prev_packet->last_instr_taken_branch = true;
1833 static int cs_etm__flush(struct cs_etm_queue *etmq,
1834 struct cs_etm_traceid_queue *tidq)
1837 struct cs_etm_auxtrace *etm = etmq->etm;
1839 /* Handle start tracing packet */
1840 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1843 if (etmq->etm->synth_opts.last_branch &&
1844 etmq->etm->synth_opts.instructions &&
1845 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1848 /* Prepare last branches for instruction sample */
1849 cs_etm__copy_last_branch_rb(etmq, tidq);
1852 * Generate a last branch event for the branches left in the
1853 * circular buffer at the end of the trace.
1855 * Use the address of the end of the last reported execution
1858 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1860 err = cs_etm__synth_instruction_sample(
1862 tidq->period_instructions);
1866 tidq->period_instructions = 0;
1870 if (etm->synth_opts.branches &&
1871 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1872 err = cs_etm__synth_branch_sample(etmq, tidq);
1878 cs_etm__packet_swap(etm, tidq);
1880 /* Reset last branches after flush the trace */
1881 if (etm->synth_opts.last_branch)
1882 cs_etm__reset_last_branch_rb(tidq);
1887 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1888 struct cs_etm_traceid_queue *tidq)
1893 * It has no new packet coming and 'etmq->packet' contains the stale
1894 * packet which was set at the previous time with packets swapping;
1895 * so skip to generate branch sample to avoid stale packet.
1897 * For this case only flush branch stack and generate a last branch
1898 * event for the branches left in the circular buffer at the end of
1901 if (etmq->etm->synth_opts.last_branch &&
1902 etmq->etm->synth_opts.instructions &&
1903 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1906 /* Prepare last branches for instruction sample */
1907 cs_etm__copy_last_branch_rb(etmq, tidq);
1910 * Use the address of the end of the last reported execution
1913 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1915 err = cs_etm__synth_instruction_sample(
1917 tidq->period_instructions);
1921 tidq->period_instructions = 0;
1927 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1929 * Returns: < 0 if error
1930 * = 0 if no more auxtrace_buffer to read
1931 * > 0 if the current buffer isn't empty yet
1933 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1937 if (!etmq->buf_len) {
1938 ret = cs_etm__get_trace(etmq);
1942 * We cannot assume consecutive blocks in the data file
1943 * are contiguous, reset the decoder to force re-sync.
1945 ret = cs_etm_decoder__reset(etmq->decoder);
1950 return etmq->buf_len;
1953 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1954 struct cs_etm_packet *packet,
1957 /* Initialise to keep compiler happy */
1962 switch (packet->isa) {
1963 case CS_ETM_ISA_T32:
1965 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1968 * +-----------------+--------+
1969 * | 1 1 0 1 1 1 1 1 | imm8 |
1970 * +-----------------+--------+
1972 * According to the specification, it only defines SVC for T32
1973 * with 16 bits instruction and has no definition for 32bits;
1974 * so below only read 2 bytes as instruction size for T32.
1976 addr = end_addr - 2;
1977 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr16),
1979 if ((instr16 & 0xFF00) == 0xDF00)
1983 case CS_ETM_ISA_A32:
1985 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1987 * b'31 b'28 b'27 b'24
1988 * +---------+---------+-------------------------+
1989 * | !1111 | 1 1 1 1 | imm24 |
1990 * +---------+---------+-------------------------+
1992 addr = end_addr - 4;
1993 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr32),
1995 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1996 (instr32 & 0xF0000000) != 0xF0000000)
2000 case CS_ETM_ISA_A64:
2002 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
2005 * +-----------------------+---------+-----------+
2006 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
2007 * +-----------------------+---------+-----------+
2009 addr = end_addr - 4;
2010 cs_etm__mem_access(etmq, trace_chan_id, addr, sizeof(instr32),
2012 if ((instr32 & 0xFFE0001F) == 0xd4000001)
2016 case CS_ETM_ISA_UNKNOWN:
2024 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
2025 struct cs_etm_traceid_queue *tidq, u64 magic)
2027 u8 trace_chan_id = tidq->trace_chan_id;
2028 struct cs_etm_packet *packet = tidq->packet;
2029 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2031 if (magic == __perf_cs_etmv3_magic)
2032 if (packet->exception_number == CS_ETMV3_EXC_SVC)
2036 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
2037 * HVC cases; need to check if it's SVC instruction based on
2040 if (magic == __perf_cs_etmv4_magic) {
2041 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2042 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2043 prev_packet->end_addr))
2050 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
2053 struct cs_etm_packet *packet = tidq->packet;
2055 if (magic == __perf_cs_etmv3_magic)
2056 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
2057 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
2058 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
2059 packet->exception_number == CS_ETMV3_EXC_IRQ ||
2060 packet->exception_number == CS_ETMV3_EXC_FIQ)
2063 if (magic == __perf_cs_etmv4_magic)
2064 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
2065 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
2066 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
2067 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
2068 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
2069 packet->exception_number == CS_ETMV4_EXC_IRQ ||
2070 packet->exception_number == CS_ETMV4_EXC_FIQ)
2076 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
2077 struct cs_etm_traceid_queue *tidq,
2080 u8 trace_chan_id = tidq->trace_chan_id;
2081 struct cs_etm_packet *packet = tidq->packet;
2082 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2084 if (magic == __perf_cs_etmv3_magic)
2085 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
2086 packet->exception_number == CS_ETMV3_EXC_HYP ||
2087 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
2088 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
2089 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
2090 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
2091 packet->exception_number == CS_ETMV3_EXC_GENERIC)
2094 if (magic == __perf_cs_etmv4_magic) {
2095 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
2096 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
2097 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
2098 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
2102 * For CS_ETMV4_EXC_CALL, except SVC other instructions
2103 * (SMC, HVC) are taken as sync exceptions.
2105 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2106 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
2107 prev_packet->end_addr))
2111 * ETMv4 has 5 bits for exception number; if the numbers
2112 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
2113 * they are implementation defined exceptions.
2115 * For this case, simply take it as sync exception.
2117 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
2118 packet->exception_number <= CS_ETMV4_EXC_END)
2125 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
2126 struct cs_etm_traceid_queue *tidq)
2128 struct cs_etm_packet *packet = tidq->packet;
2129 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2130 u8 trace_chan_id = tidq->trace_chan_id;
2134 switch (packet->sample_type) {
2137 * Immediate branch instruction without neither link nor
2138 * return flag, it's normal branch instruction within
2141 if (packet->last_instr_type == OCSD_INSTR_BR &&
2142 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
2143 packet->flags = PERF_IP_FLAG_BRANCH;
2145 if (packet->last_instr_cond)
2146 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
2150 * Immediate branch instruction with link (e.g. BL), this is
2151 * branch instruction for function call.
2153 if (packet->last_instr_type == OCSD_INSTR_BR &&
2154 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2155 packet->flags = PERF_IP_FLAG_BRANCH |
2159 * Indirect branch instruction with link (e.g. BLR), this is
2160 * branch instruction for function call.
2162 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2163 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2164 packet->flags = PERF_IP_FLAG_BRANCH |
2168 * Indirect branch instruction with subtype of
2169 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
2170 * function return for A32/T32.
2172 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2173 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
2174 packet->flags = PERF_IP_FLAG_BRANCH |
2175 PERF_IP_FLAG_RETURN;
2178 * Indirect branch instruction without link (e.g. BR), usually
2179 * this is used for function return, especially for functions
2180 * within dynamic link lib.
2182 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2183 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
2184 packet->flags = PERF_IP_FLAG_BRANCH |
2185 PERF_IP_FLAG_RETURN;
2187 /* Return instruction for function return. */
2188 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2189 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
2190 packet->flags = PERF_IP_FLAG_BRANCH |
2191 PERF_IP_FLAG_RETURN;
2194 * Decoder might insert a discontinuity in the middle of
2195 * instruction packets, fixup prev_packet with flag
2196 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
2198 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
2199 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2200 PERF_IP_FLAG_TRACE_BEGIN;
2203 * If the previous packet is an exception return packet
2204 * and the return address just follows SVC instruction,
2205 * it needs to calibrate the previous packet sample flags
2206 * as PERF_IP_FLAG_SYSCALLRET.
2208 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
2209 PERF_IP_FLAG_RETURN |
2210 PERF_IP_FLAG_INTERRUPT) &&
2211 cs_etm__is_svc_instr(etmq, trace_chan_id,
2212 packet, packet->start_addr))
2213 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2214 PERF_IP_FLAG_RETURN |
2215 PERF_IP_FLAG_SYSCALLRET;
2217 case CS_ETM_DISCONTINUITY:
2219 * The trace is discontinuous, if the previous packet is
2220 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
2221 * for previous packet.
2223 if (prev_packet->sample_type == CS_ETM_RANGE)
2224 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2225 PERF_IP_FLAG_TRACE_END;
2227 case CS_ETM_EXCEPTION:
2228 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
2232 /* The exception is for system call. */
2233 if (cs_etm__is_syscall(etmq, tidq, magic))
2234 packet->flags = PERF_IP_FLAG_BRANCH |
2236 PERF_IP_FLAG_SYSCALLRET;
2238 * The exceptions are triggered by external signals from bus,
2239 * interrupt controller, debug module, PE reset or halt.
2241 else if (cs_etm__is_async_exception(tidq, magic))
2242 packet->flags = PERF_IP_FLAG_BRANCH |
2244 PERF_IP_FLAG_ASYNC |
2245 PERF_IP_FLAG_INTERRUPT;
2247 * Otherwise, exception is caused by trap, instruction &
2248 * data fault, or alignment errors.
2250 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
2251 packet->flags = PERF_IP_FLAG_BRANCH |
2253 PERF_IP_FLAG_INTERRUPT;
2256 * When the exception packet is inserted, since exception
2257 * packet is not used standalone for generating samples
2258 * and it's affiliation to the previous instruction range
2259 * packet; so set previous range packet flags to tell perf
2260 * it is an exception taken branch.
2262 if (prev_packet->sample_type == CS_ETM_RANGE)
2263 prev_packet->flags = packet->flags;
2265 case CS_ETM_EXCEPTION_RET:
2267 * When the exception return packet is inserted, since
2268 * exception return packet is not used standalone for
2269 * generating samples and it's affiliation to the previous
2270 * instruction range packet; so set previous range packet
2271 * flags to tell perf it is an exception return branch.
2273 * The exception return can be for either system call or
2274 * other exception types; unfortunately the packet doesn't
2275 * contain exception type related info so we cannot decide
2276 * the exception type purely based on exception return packet.
2277 * If we record the exception number from exception packet and
2278 * reuse it for exception return packet, this is not reliable
2279 * due the trace can be discontinuity or the interrupt can
2280 * be nested, thus the recorded exception number cannot be
2281 * used for exception return packet for these two cases.
2283 * For exception return packet, we only need to distinguish the
2284 * packet is for system call or for other types. Thus the
2285 * decision can be deferred when receive the next packet which
2286 * contains the return address, based on the return address we
2287 * can read out the previous instruction and check if it's a
2288 * system call instruction and then calibrate the sample flag
2291 if (prev_packet->sample_type == CS_ETM_RANGE)
2292 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2293 PERF_IP_FLAG_RETURN |
2294 PERF_IP_FLAG_INTERRUPT;
2304 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2307 size_t processed = 0;
2310 * Packets are decoded and added to the decoder's packet queue
2311 * until the decoder packet processing callback has requested that
2312 * processing stops or there is nothing left in the buffer. Normal
2313 * operations that stop processing are a timestamp packet or a full
2314 * decoder buffer queue.
2316 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2318 &etmq->buf[etmq->buf_used],
2324 etmq->offset += processed;
2325 etmq->buf_used += processed;
2326 etmq->buf_len -= processed;
2332 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2333 struct cs_etm_traceid_queue *tidq)
2336 struct cs_etm_packet_queue *packet_queue;
2338 packet_queue = &tidq->packet_queue;
2340 /* Process each packet in this chunk */
2342 ret = cs_etm_decoder__get_packet(packet_queue,
2346 * Stop processing this chunk on
2347 * end of data or error
2352 * Since packet addresses are swapped in packet
2353 * handling within below switch() statements,
2354 * thus setting sample flags must be called
2355 * prior to switch() statement to use address
2356 * information before packets swapping.
2358 ret = cs_etm__set_sample_flags(etmq, tidq);
2362 switch (tidq->packet->sample_type) {
2365 * If the packet contains an instruction
2366 * range, generate instruction sequence
2369 cs_etm__sample(etmq, tidq);
2371 case CS_ETM_EXCEPTION:
2372 case CS_ETM_EXCEPTION_RET:
2374 * If the exception packet is coming,
2375 * make sure the previous instruction
2376 * range packet to be handled properly.
2378 cs_etm__exception(tidq);
2380 case CS_ETM_DISCONTINUITY:
2382 * Discontinuity in trace, flush
2383 * previous branch stack
2385 cs_etm__flush(etmq, tidq);
2389 * Should not receive empty packet,
2392 pr_err("CS ETM Trace: empty packet\n");
2402 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2405 struct int_node *inode;
2406 struct cs_etm_traceid_queue *tidq;
2407 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2409 intlist__for_each_entry(inode, traceid_queues_list) {
2410 idx = (int)(intptr_t)inode->priv;
2411 tidq = etmq->traceid_queues[idx];
2413 /* Ignore return value */
2414 cs_etm__process_traceid_queue(etmq, tidq);
2417 * Generate an instruction sample with the remaining
2418 * branchstack entries.
2420 cs_etm__flush(etmq, tidq);
2424 static int cs_etm__run_per_thread_timeless_decoder(struct cs_etm_queue *etmq)
2427 struct cs_etm_traceid_queue *tidq;
2429 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2433 /* Go through each buffer in the queue and decode them one by one */
2435 err = cs_etm__get_data_block(etmq);
2439 /* Run trace decoder until buffer consumed or end of trace */
2441 err = cs_etm__decode_data_block(etmq);
2446 * Process each packet in this chunk, nothing to do if
2447 * an error occurs other than hoping the next one will
2450 err = cs_etm__process_traceid_queue(etmq, tidq);
2452 } while (etmq->buf_len);
2455 /* Flush any remaining branch stack entries */
2456 err = cs_etm__end_block(etmq, tidq);
2462 static int cs_etm__run_per_cpu_timeless_decoder(struct cs_etm_queue *etmq)
2465 struct cs_etm_traceid_queue *tidq;
2466 struct int_node *inode;
2468 /* Go through each buffer in the queue and decode them one by one */
2470 err = cs_etm__get_data_block(etmq);
2474 /* Run trace decoder until buffer consumed or end of trace */
2476 err = cs_etm__decode_data_block(etmq);
2481 * cs_etm__run_per_thread_timeless_decoder() runs on a
2482 * single traceID queue because each TID has a separate
2483 * buffer. But here in per-cpu mode we need to iterate
2484 * over each channel instead.
2486 intlist__for_each_entry(inode,
2487 etmq->traceid_queues_list) {
2488 idx = (int)(intptr_t)inode->priv;
2489 tidq = etmq->traceid_queues[idx];
2490 cs_etm__process_traceid_queue(etmq, tidq);
2492 } while (etmq->buf_len);
2494 intlist__for_each_entry(inode, etmq->traceid_queues_list) {
2495 idx = (int)(intptr_t)inode->priv;
2496 tidq = etmq->traceid_queues[idx];
2497 /* Flush any remaining branch stack entries */
2498 err = cs_etm__end_block(etmq, tidq);
2507 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2511 struct auxtrace_queues *queues = &etm->queues;
2513 for (i = 0; i < queues->nr_queues; i++) {
2514 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2515 struct cs_etm_queue *etmq = queue->priv;
2516 struct cs_etm_traceid_queue *tidq;
2521 if (etm->per_thread_decoding) {
2522 tidq = cs_etm__etmq_get_traceid_queue(
2523 etmq, CS_ETM_PER_THREAD_TRACEID);
2528 if (tid == -1 || thread__tid(tidq->thread) == tid)
2529 cs_etm__run_per_thread_timeless_decoder(etmq);
2531 cs_etm__run_per_cpu_timeless_decoder(etmq);
2537 static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
2540 unsigned int cs_queue_nr, queue_nr, i;
2543 struct auxtrace_queue *queue;
2544 struct cs_etm_queue *etmq;
2545 struct cs_etm_traceid_queue *tidq;
2548 * Pre-populate the heap with one entry from each queue so that we can
2549 * start processing in time order across all queues.
2551 for (i = 0; i < etm->queues.nr_queues; i++) {
2552 etmq = etm->queues.queue_array[i].priv;
2556 ret = cs_etm__queue_first_cs_timestamp(etm, etmq, i);
2562 if (!etm->heap.heap_cnt)
2565 /* Take the entry at the top of the min heap */
2566 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2567 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2568 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2569 queue = &etm->queues.queue_array[queue_nr];
2573 * Remove the top entry from the heap since we are about
2576 auxtrace_heap__pop(&etm->heap);
2578 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2581 * No traceID queue has been allocated for this traceID,
2582 * which means something somewhere went very wrong. No
2583 * other choice than simply exit.
2590 * Packets associated with this timestamp are already in
2591 * the etmq's traceID queue, so process them.
2593 ret = cs_etm__process_traceid_queue(etmq, tidq);
2598 * Packets for this timestamp have been processed, time to
2599 * move on to the next timestamp, fetching a new auxtrace_buffer
2603 ret = cs_etm__get_data_block(etmq);
2608 * No more auxtrace_buffers to process in this etmq, simply
2609 * move on to another entry in the auxtrace_heap.
2614 ret = cs_etm__decode_data_block(etmq);
2618 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2620 if (!cs_timestamp) {
2622 * Function cs_etm__decode_data_block() returns when
2623 * there is no more traces to decode in the current
2624 * auxtrace_buffer OR when a timestamp has been
2625 * encountered on any of the traceID queues. Since we
2626 * did not get a timestamp, there is no more traces to
2627 * process in this auxtrace_buffer. As such empty and
2628 * flush all traceID queues.
2630 cs_etm__clear_all_traceid_queues(etmq);
2632 /* Fetch another auxtrace_buffer for this etmq */
2637 * Add to the min heap the timestamp for packets that have
2638 * just been decoded. They will be processed and synthesized
2639 * during the next call to cs_etm__process_traceid_queue() for
2640 * this queue/traceID.
2642 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2643 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2650 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2651 union perf_event *event)
2655 if (etm->timeless_decoding)
2659 * Add the tid/pid to the log so that we can get a match when we get a
2660 * contextID from the decoder. Only track for the host: only kernel
2661 * trace is supported for guests which wouldn't need pids so this should
2664 th = machine__findnew_thread(&etm->session->machines.host,
2665 event->itrace_start.pid,
2666 event->itrace_start.tid);
2675 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2676 union perf_event *event)
2679 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2682 * Context switch in per-thread mode are irrelevant since perf
2683 * will start/stop tracing as the process is scheduled.
2685 if (etm->timeless_decoding)
2689 * SWITCH_IN events carry the next process to be switched out while
2690 * SWITCH_OUT events carry the process to be switched in. As such
2691 * we don't care about IN events.
2697 * Add the tid/pid to the log so that we can get a match when we get a
2698 * contextID from the decoder. Only track for the host: only kernel
2699 * trace is supported for guests which wouldn't need pids so this should
2702 th = machine__findnew_thread(&etm->session->machines.host,
2703 event->context_switch.next_prev_pid,
2704 event->context_switch.next_prev_tid);
2713 static int cs_etm__process_event(struct perf_session *session,
2714 union perf_event *event,
2715 struct perf_sample *sample,
2716 struct perf_tool *tool)
2718 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2719 struct cs_etm_auxtrace,
2725 if (!tool->ordered_events) {
2726 pr_err("CoreSight ETM Trace requires ordered events\n");
2730 switch (event->header.type) {
2731 case PERF_RECORD_EXIT:
2733 * Don't need to wait for cs_etm__flush_events() in per-thread mode to
2734 * start the decode because we know there will be no more trace from
2735 * this thread. All this does is emit samples earlier than waiting for
2736 * the flush in other modes, but with timestamps it makes sense to wait
2737 * for flush so that events from different threads are interleaved
2740 if (etm->per_thread_decoding && etm->timeless_decoding)
2741 return cs_etm__process_timeless_queues(etm,
2745 case PERF_RECORD_ITRACE_START:
2746 return cs_etm__process_itrace_start(etm, event);
2748 case PERF_RECORD_SWITCH_CPU_WIDE:
2749 return cs_etm__process_switch_cpu_wide(etm, event);
2751 case PERF_RECORD_AUX:
2753 * Record the latest kernel timestamp available in the header
2754 * for samples so that synthesised samples occur from this point
2757 if (sample->time && (sample->time != (u64)-1))
2758 etm->latest_kernel_timestamp = sample->time;
2768 static void dump_queued_data(struct cs_etm_auxtrace *etm,
2769 struct perf_record_auxtrace *event)
2771 struct auxtrace_buffer *buf;
2774 * Find all buffers with same reference in the queues and dump them.
2775 * This is because the queues can contain multiple entries of the same
2776 * buffer that were split on aux records.
2778 for (i = 0; i < etm->queues.nr_queues; ++i)
2779 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2780 if (buf->reference == event->reference)
2781 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2784 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2785 union perf_event *event,
2786 struct perf_tool *tool __maybe_unused)
2788 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2789 struct cs_etm_auxtrace,
2791 if (!etm->data_queued) {
2792 struct auxtrace_buffer *buffer;
2794 int fd = perf_data__fd(session->data);
2795 bool is_pipe = perf_data__is_pipe(session->data);
2797 int idx = event->auxtrace.idx;
2802 data_offset = lseek(fd, 0, SEEK_CUR);
2803 if (data_offset == -1)
2807 err = auxtrace_queues__add_event(&etm->queues, session,
2808 event, data_offset, &buffer);
2813 * Knowing if the trace is formatted or not requires a lookup of
2814 * the aux record so only works in non-piped mode where data is
2815 * queued in cs_etm__queue_aux_records(). Always assume
2816 * formatted in piped mode (true).
2818 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
2824 if (auxtrace_buffer__get_data(buffer, fd)) {
2825 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2826 auxtrace_buffer__put_data(buffer);
2828 } else if (dump_trace)
2829 dump_queued_data(etm, &event->auxtrace);
2834 static int cs_etm__setup_timeless_decoding(struct cs_etm_auxtrace *etm)
2836 struct evsel *evsel;
2837 struct evlist *evlist = etm->session->evlist;
2839 /* Override timeless mode with user input from --itrace=Z */
2840 if (etm->synth_opts.timeless_decoding) {
2841 etm->timeless_decoding = true;
2846 * Find the cs_etm evsel and look at what its timestamp setting was
2848 evlist__for_each_entry(evlist, evsel)
2849 if (cs_etm__evsel_is_auxtrace(etm->session, evsel)) {
2850 etm->timeless_decoding =
2851 !(evsel->core.attr.config & BIT(ETM_OPT_TS));
2855 pr_err("CS ETM: Couldn't find ETM evsel\n");
2860 * Read a single cpu parameter block from the auxtrace_info priv block.
2862 * For version 1 there is a per cpu nr_params entry. If we are handling
2863 * version 1 file, then there may be less, the same, or more params
2864 * indicated by this value than the compile time number we understand.
2866 * For a version 0 info block, there are a fixed number, and we need to
2867 * fill out the nr_param value in the metadata we create.
2869 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2870 int out_blk_size, int nr_params_v0)
2872 u64 *metadata = NULL;
2874 int nr_in_params, nr_out_params, nr_cmn_params;
2877 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2881 /* read block current index & version */
2882 i = *buff_in_offset;
2883 hdr_version = buff_in[CS_HEADER_VERSION];
2886 /* read version 0 info block into a version 1 metadata block */
2887 nr_in_params = nr_params_v0;
2888 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2889 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2890 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2891 /* remaining block params at offset +1 from source */
2892 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2893 metadata[k + 1] = buff_in[i + k];
2894 /* version 0 has 2 common params */
2897 /* read version 1 info block - input and output nr_params may differ */
2898 /* version 1 has 3 common params */
2900 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2902 /* if input has more params than output - skip excess */
2903 nr_out_params = nr_in_params + nr_cmn_params;
2904 if (nr_out_params > out_blk_size)
2905 nr_out_params = out_blk_size;
2907 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2908 metadata[k] = buff_in[i + k];
2910 /* record the actual nr params we copied */
2911 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2914 /* adjust in offset by number of in params used */
2915 i += nr_in_params + nr_cmn_params;
2916 *buff_in_offset = i;
2921 * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2922 * on the bounds of aux_event, if it matches with the buffer that's at
2925 * Normally, whole auxtrace buffers would be added to the queue. But we
2926 * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2927 * is reset across each buffer, so splitting the buffers up in advance has
2930 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2931 struct perf_record_aux *aux_event, struct perf_sample *sample)
2934 char buf[PERF_SAMPLE_MAX_SIZE];
2935 union perf_event *auxtrace_event_union;
2936 struct perf_record_auxtrace *auxtrace_event;
2937 union perf_event auxtrace_fragment;
2938 __u64 aux_offset, aux_size;
2942 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2943 struct cs_etm_auxtrace,
2947 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2948 * from looping through the auxtrace index.
2950 err = perf_session__peek_event(session, file_offset, buf,
2951 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2954 auxtrace_event = &auxtrace_event_union->auxtrace;
2955 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2958 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2959 auxtrace_event->header.size != sz) {
2964 * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
2965 * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a
2966 * CPU as we set this always for the AUX_OUTPUT_HW_ID event.
2967 * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
2968 * Return 'not found' if mismatch.
2970 if (auxtrace_event->cpu == (__u32) -1) {
2971 etm->per_thread_decoding = true;
2972 if (auxtrace_event->tid != sample->tid)
2974 } else if (auxtrace_event->cpu != sample->cpu) {
2975 if (etm->per_thread_decoding) {
2977 * Found a per-cpu buffer after a per-thread one was
2980 pr_err("CS ETM: Inconsistent per-thread/per-cpu mode.\n");
2986 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2988 * Clamp size in snapshot mode. The buffer size is clamped in
2989 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
2992 aux_size = min(aux_event->aux_size, auxtrace_event->size);
2995 * In this mode, the head also points to the end of the buffer so aux_offset
2996 * needs to have the size subtracted so it points to the beginning as in normal mode
2998 aux_offset = aux_event->aux_offset - aux_size;
3000 aux_size = aux_event->aux_size;
3001 aux_offset = aux_event->aux_offset;
3004 if (aux_offset >= auxtrace_event->offset &&
3005 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
3007 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
3008 * based on the sizes of the aux event, and queue that fragment.
3010 auxtrace_fragment.auxtrace = *auxtrace_event;
3011 auxtrace_fragment.auxtrace.size = aux_size;
3012 auxtrace_fragment.auxtrace.offset = aux_offset;
3013 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
3015 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
3016 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
3017 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
3022 idx = auxtrace_event->idx;
3023 formatted = !(aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW);
3024 return cs_etm__setup_queue(etm, &etm->queues.queue_array[idx],
3028 /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
3032 static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event,
3033 u64 offset __maybe_unused, void *data __maybe_unused)
3035 /* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */
3036 if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
3037 (*(int *)data)++; /* increment found count */
3038 return cs_etm__process_aux_output_hw_id(session, event);
3043 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
3044 u64 offset __maybe_unused, void *data __maybe_unused)
3046 struct perf_sample sample;
3048 struct auxtrace_index_entry *ent;
3049 struct auxtrace_index *auxtrace_index;
3050 struct evsel *evsel;
3053 /* Don't care about any other events, we're only queuing buffers for AUX events */
3054 if (event->header.type != PERF_RECORD_AUX)
3057 if (event->header.size < sizeof(struct perf_record_aux))
3060 /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
3061 if (!event->aux.aux_size)
3065 * Parse the sample, we need the sample_id_all data that comes after the event so that the
3066 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
3068 evsel = evlist__event2evsel(session->evlist, event);
3071 ret = evsel__parse_sample(evsel, event, &sample);
3076 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
3078 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
3079 for (i = 0; i < auxtrace_index->nr; i++) {
3080 ent = &auxtrace_index->entries[i];
3081 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
3082 ent->sz, &event->aux, &sample);
3084 * Stop search on error or successful values. Continue search on
3093 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
3094 * don't exit with an error because it will still be possible to decode other aux records.
3096 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
3097 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
3101 static int cs_etm__queue_aux_records(struct perf_session *session)
3103 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
3104 struct auxtrace_index, list);
3105 if (index && index->nr > 0)
3106 return perf_session__peek_events(session, session->header.data_offset,
3107 session->header.data_size,
3108 cs_etm__queue_aux_records_cb, NULL);
3111 * We would get here if there are no entries in the index (either no auxtrace
3112 * buffers or no index at all). Fail silently as there is the possibility of
3113 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
3116 * In that scenario, buffers will not be split by AUX records.
3121 #define HAS_PARAM(j, type, param) (metadata[(j)][CS_ETM_NR_TRC_PARAMS] <= \
3122 (CS_##type##_##param - CS_ETM_COMMON_BLK_MAX_V1))
3125 * Loop through the ETMs and complain if we find at least one where ts_source != 1 (virtual
3128 static bool cs_etm__has_virtual_ts(u64 **metadata, int num_cpu)
3132 for (j = 0; j < num_cpu; j++) {
3133 switch (metadata[j][CS_ETM_MAGIC]) {
3134 case __perf_cs_etmv4_magic:
3135 if (HAS_PARAM(j, ETMV4, TS_SOURCE) || metadata[j][CS_ETMV4_TS_SOURCE] != 1)
3138 case __perf_cs_ete_magic:
3139 if (HAS_PARAM(j, ETE, TS_SOURCE) || metadata[j][CS_ETE_TS_SOURCE] != 1)
3143 /* Unknown / unsupported magic number. */
3150 /* map trace ids to correct metadata block, from information in metadata */
3151 static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata)
3157 for (i = 0; i < num_cpu; i++) {
3158 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3159 switch (cs_etm_magic) {
3160 case __perf_cs_etmv3_magic:
3161 metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3162 trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]);
3164 case __perf_cs_etmv4_magic:
3165 case __perf_cs_ete_magic:
3166 metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3167 trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]);
3170 /* unknown magic number */
3173 err = cs_etm__map_trace_id(trace_chan_id, metadata[i]);
3181 * If we found AUX_HW_ID packets, then set any metadata marked as unused to the
3182 * unused value to reduce the number of unneeded decoders created.
3184 static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata)
3189 for (i = 0; i < num_cpu; i++) {
3190 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3191 switch (cs_etm_magic) {
3192 case __perf_cs_etmv3_magic:
3193 if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3194 metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3196 case __perf_cs_etmv4_magic:
3197 case __perf_cs_ete_magic:
3198 if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3199 metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3202 /* unknown magic number */
3209 int cs_etm__process_auxtrace_info_full(union perf_event *event,
3210 struct perf_session *session)
3212 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3213 struct cs_etm_auxtrace *etm = NULL;
3214 struct perf_record_time_conv *tc = &session->time_conv;
3215 int event_header_size = sizeof(struct perf_event_header);
3216 int total_size = auxtrace_info->header.size;
3220 int aux_hw_id_found;
3223 u64 **metadata = NULL;
3226 * Create an RB tree for traceID-metadata tuple. Since the conversion
3227 * has to be made for each packet that gets decoded, optimizing access
3228 * in anything other than a sequential array is worth doing.
3230 traceid_list = intlist__new(NULL);
3234 /* First the global part */
3235 ptr = (u64 *) auxtrace_info->priv;
3236 num_cpu = ptr[CS_PMU_TYPE_CPUS] & 0xffffffff;
3237 metadata = zalloc(sizeof(*metadata) * num_cpu);
3240 goto err_free_traceid_list;
3243 /* Start parsing after the common part of the header */
3244 i = CS_HEADER_VERSION_MAX;
3247 * The metadata is stored in the auxtrace_info section and encodes
3248 * the configuration of the ARM embedded trace macrocell which is
3249 * required by the trace decoder to properly decode the trace due
3250 * to its highly compressed nature.
3252 for (j = 0; j < num_cpu; j++) {
3253 if (ptr[i] == __perf_cs_etmv3_magic) {
3255 cs_etm__create_meta_blk(ptr, &i,
3257 CS_ETM_NR_TRC_PARAMS_V0);
3258 } else if (ptr[i] == __perf_cs_etmv4_magic) {
3260 cs_etm__create_meta_blk(ptr, &i,
3262 CS_ETMV4_NR_TRC_PARAMS_V0);
3263 } else if (ptr[i] == __perf_cs_ete_magic) {
3264 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
3266 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
3269 goto err_free_metadata;
3274 goto err_free_metadata;
3279 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
3280 * CS_ETMV4_PRIV_MAX mark how many double words are in the
3281 * global metadata, and each cpu's metadata respectively.
3282 * The following tests if the correct number of double words was
3283 * present in the auxtrace info section.
3285 priv_size = total_size - event_header_size - INFO_HEADER_SIZE;
3286 if (i * 8 != priv_size) {
3288 goto err_free_metadata;
3291 etm = zalloc(sizeof(*etm));
3295 goto err_free_metadata;
3299 * As all the ETMs run at the same exception level, the system should
3300 * have the same PID format crossing CPUs. So cache the PID format
3301 * and reuse it for sequential decoding.
3303 etm->pid_fmt = cs_etm__init_pid_fmt(metadata[0]);
3305 err = auxtrace_queues__init(&etm->queues);
3309 if (session->itrace_synth_opts->set) {
3310 etm->synth_opts = *session->itrace_synth_opts;
3312 itrace_synth_opts__set_default(&etm->synth_opts,
3313 session->itrace_synth_opts->default_no_sample);
3314 etm->synth_opts.callchain = false;
3317 etm->session = session;
3319 etm->num_cpu = num_cpu;
3320 etm->pmu_type = (unsigned int) ((ptr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
3321 etm->snapshot_mode = (ptr[CS_ETM_SNAPSHOT] != 0);
3322 etm->metadata = metadata;
3323 etm->auxtrace_type = auxtrace_info->type;
3325 /* Use virtual timestamps if all ETMs report ts_source = 1 */
3326 etm->has_virtual_ts = cs_etm__has_virtual_ts(metadata, num_cpu);
3328 if (!etm->has_virtual_ts)
3329 ui__warning("Virtual timestamps are not enabled, or not supported by the traced system.\n"
3330 "The time field of the samples will not be set accurately.\n\n");
3332 etm->auxtrace.process_event = cs_etm__process_event;
3333 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3334 etm->auxtrace.flush_events = cs_etm__flush_events;
3335 etm->auxtrace.free_events = cs_etm__free_events;
3336 etm->auxtrace.free = cs_etm__free;
3337 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3338 session->auxtrace = &etm->auxtrace;
3340 err = cs_etm__setup_timeless_decoding(etm);
3344 etm->tc.time_shift = tc->time_shift;
3345 etm->tc.time_mult = tc->time_mult;
3346 etm->tc.time_zero = tc->time_zero;
3347 if (event_contains(*tc, time_cycles)) {
3348 etm->tc.time_cycles = tc->time_cycles;
3349 etm->tc.time_mask = tc->time_mask;
3350 etm->tc.cap_user_time_zero = tc->cap_user_time_zero;
3351 etm->tc.cap_user_time_short = tc->cap_user_time_short;
3353 err = cs_etm__synth_events(etm, session);
3355 goto err_free_queues;
3358 * Map Trace ID values to CPU metadata.
3360 * Trace metadata will always contain Trace ID values from the legacy algorithm. If the
3361 * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata
3362 * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set.
3364 * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use
3365 * the same IDs as the old algorithm as far as is possible, unless there are clashes
3366 * in which case a different value will be used. This means an older perf may still
3367 * be able to record and read files generate on a newer system.
3369 * For a perf able to interpret AUX_HW_ID packets we first check for the presence of
3370 * those packets. If they are there then the values will be mapped and plugged into
3371 * the metadata. We then set any remaining metadata values with the used flag to a
3372 * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required.
3374 * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3375 * then we map Trace ID values to CPU directly from the metadata - clearing any unused
3379 /* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */
3380 aux_hw_id_found = 0;
3381 err = perf_session__peek_events(session, session->header.data_offset,
3382 session->header.data_size,
3383 cs_etm__process_aux_hw_id_cb, &aux_hw_id_found);
3385 goto err_free_queues;
3387 /* if HW ID found then clear any unused metadata ID values */
3388 if (aux_hw_id_found)
3389 err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata);
3390 /* otherwise, this is a file with metadata values only, map from metadata */
3392 err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3395 goto err_free_queues;
3397 err = cs_etm__queue_aux_records(session);
3399 goto err_free_queues;
3401 etm->data_queued = etm->queues.populated;
3405 auxtrace_queues__free(&etm->queues);
3406 session->auxtrace = NULL;
3410 /* No need to check @metadata[j], free(NULL) is supported */
3411 for (j = 0; j < num_cpu; j++)
3412 zfree(&metadata[j]);
3414 err_free_traceid_list:
3415 intlist__delete(traceid_list);