1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
5 * Author: Tor Jeremiassen <tor@ti.com>
6 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
9 #include <linux/bitops.h>
10 #include <linux/coresight-pmu.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
17 #include <opencsd/ocsd_if_types.h>
23 #include "cs-etm-decoder/cs-etm-decoder.h"
32 #include "map_symbol.h"
37 #include "thread-stack.h"
38 #include <tools/libc_compat.h>
39 #include "util/synthetic-events.h"
41 struct cs_etm_auxtrace {
42 struct auxtrace auxtrace;
43 struct auxtrace_queues queues;
44 struct auxtrace_heap heap;
45 struct itrace_synth_opts synth_opts;
46 struct perf_session *session;
47 struct machine *machine;
48 struct thread *unknown_thread;
54 u8 sample_instructions;
57 u64 latest_kernel_timestamp;
59 u64 branches_sample_type;
61 u64 instructions_sample_type;
62 u64 instructions_sample_period;
66 unsigned int pmu_type;
69 struct cs_etm_traceid_queue {
72 u64 period_instructions;
73 size_t last_branch_pos;
74 union perf_event *event_buf;
75 struct thread *thread;
76 struct branch_stack *last_branch;
77 struct branch_stack *last_branch_rb;
78 struct cs_etm_packet *prev_packet;
79 struct cs_etm_packet *packet;
80 struct cs_etm_packet_queue packet_queue;
84 struct cs_etm_auxtrace *etm;
85 struct cs_etm_decoder *decoder;
86 struct auxtrace_buffer *buffer;
87 unsigned int queue_nr;
88 u8 pending_timestamp_chan_id;
90 const unsigned char *buf;
91 size_t buf_len, buf_used;
92 /* Conversion between traceID and index in traceid_queues array */
93 struct intlist *traceid_queues_list;
94 struct cs_etm_traceid_queue **traceid_queues;
97 /* RB tree for quick conversion between traceID and metadata pointers */
98 static struct intlist *traceid_list;
100 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
101 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
102 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
104 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
105 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
107 /* PTMs ETMIDR [11:8] set to b0011 */
108 #define ETMIDR_PTM_VERSION 0x00000300
111 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
112 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
113 * encode the etm queue number as the upper 16 bit and the channel as
116 #define TO_CS_QUEUE_NR(queue_nr, trace_chan_id) \
117 (queue_nr << 16 | trace_chan_id)
118 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
119 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
121 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
123 etmidr &= ETMIDR_PTM_VERSION;
125 if (etmidr == ETMIDR_PTM_VERSION)
126 return CS_ETM_PROTO_PTM;
128 return CS_ETM_PROTO_ETMV3;
131 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
133 struct int_node *inode;
136 inode = intlist__find(traceid_list, trace_chan_id);
140 metadata = inode->priv;
141 *magic = metadata[CS_ETM_MAGIC];
145 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
147 struct int_node *inode;
150 inode = intlist__find(traceid_list, trace_chan_id);
154 metadata = inode->priv;
155 *cpu = (int)metadata[CS_ETM_CPU];
160 * The returned PID format is presented by two bits:
162 * Bit ETM_OPT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced;
163 * Bit ETM_OPT_CTXTID2: CONTEXTIDR_EL2 is traced.
165 * It's possible that the two bits ETM_OPT_CTXTID and ETM_OPT_CTXTID2
166 * are enabled at the same time when the session runs on an EL2 kernel.
167 * This means the CONTEXTIDR_EL1 and CONTEXTIDR_EL2 both will be
168 * recorded in the trace data, the tool will selectively use
169 * CONTEXTIDR_EL2 as PID.
171 int cs_etm__get_pid_fmt(u8 trace_chan_id, u64 *pid_fmt)
173 struct int_node *inode;
176 inode = intlist__find(traceid_list, trace_chan_id);
180 metadata = inode->priv;
182 if (metadata[CS_ETM_MAGIC] == __perf_cs_etmv3_magic) {
183 val = metadata[CS_ETM_ETMCR];
184 /* CONTEXTIDR is traced */
185 if (val & BIT(ETM_OPT_CTXTID))
186 *pid_fmt = BIT(ETM_OPT_CTXTID);
188 val = metadata[CS_ETMV4_TRCCONFIGR];
189 /* CONTEXTIDR_EL2 is traced */
190 if (val & (BIT(ETM4_CFG_BIT_VMID) | BIT(ETM4_CFG_BIT_VMID_OPT)))
191 *pid_fmt = BIT(ETM_OPT_CTXTID2);
192 /* CONTEXTIDR_EL1 is traced */
193 else if (val & BIT(ETM4_CFG_BIT_CTXTID))
194 *pid_fmt = BIT(ETM_OPT_CTXTID);
200 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
204 * When a timestamp packet is encountered the backend code
205 * is stopped so that the front end has time to process packets
206 * that were accumulated in the traceID queue. Since there can
207 * be more than one channel per cs_etm_queue, we need to specify
208 * what traceID queue needs servicing.
210 etmq->pending_timestamp_chan_id = trace_chan_id;
213 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
216 struct cs_etm_packet_queue *packet_queue;
218 if (!etmq->pending_timestamp_chan_id)
222 *trace_chan_id = etmq->pending_timestamp_chan_id;
224 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
225 etmq->pending_timestamp_chan_id);
229 /* Acknowledge pending status */
230 etmq->pending_timestamp_chan_id = 0;
232 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
233 return packet_queue->cs_timestamp;
236 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
242 queue->packet_count = 0;
243 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
244 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
245 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
246 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
247 queue->packet_buffer[i].instr_count = 0;
248 queue->packet_buffer[i].last_instr_taken_branch = false;
249 queue->packet_buffer[i].last_instr_size = 0;
250 queue->packet_buffer[i].last_instr_type = 0;
251 queue->packet_buffer[i].last_instr_subtype = 0;
252 queue->packet_buffer[i].last_instr_cond = 0;
253 queue->packet_buffer[i].flags = 0;
254 queue->packet_buffer[i].exception_number = UINT32_MAX;
255 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
256 queue->packet_buffer[i].cpu = INT_MIN;
260 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
263 struct int_node *inode;
264 struct cs_etm_traceid_queue *tidq;
265 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
267 intlist__for_each_entry(inode, traceid_queues_list) {
268 idx = (int)(intptr_t)inode->priv;
269 tidq = etmq->traceid_queues[idx];
270 cs_etm__clear_packet_queue(&tidq->packet_queue);
274 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
275 struct cs_etm_traceid_queue *tidq,
279 struct auxtrace_queue *queue;
280 struct cs_etm_auxtrace *etm = etmq->etm;
282 cs_etm__clear_packet_queue(&tidq->packet_queue);
284 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
285 tidq->tid = queue->tid;
287 tidq->trace_chan_id = trace_chan_id;
289 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
293 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
294 if (!tidq->prev_packet)
297 if (etm->synth_opts.last_branch) {
298 size_t sz = sizeof(struct branch_stack);
300 sz += etm->synth_opts.last_branch_sz *
301 sizeof(struct branch_entry);
302 tidq->last_branch = zalloc(sz);
303 if (!tidq->last_branch)
305 tidq->last_branch_rb = zalloc(sz);
306 if (!tidq->last_branch_rb)
310 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
311 if (!tidq->event_buf)
317 zfree(&tidq->last_branch_rb);
318 zfree(&tidq->last_branch);
319 zfree(&tidq->prev_packet);
320 zfree(&tidq->packet);
325 static struct cs_etm_traceid_queue
326 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
329 struct int_node *inode;
330 struct intlist *traceid_queues_list;
331 struct cs_etm_traceid_queue *tidq, **traceid_queues;
332 struct cs_etm_auxtrace *etm = etmq->etm;
334 if (etm->timeless_decoding)
335 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
337 traceid_queues_list = etmq->traceid_queues_list;
340 * Check if the traceid_queue exist for this traceID by looking
343 inode = intlist__find(traceid_queues_list, trace_chan_id);
345 idx = (int)(intptr_t)inode->priv;
346 return etmq->traceid_queues[idx];
349 /* We couldn't find a traceid_queue for this traceID, allocate one */
350 tidq = malloc(sizeof(*tidq));
354 memset(tidq, 0, sizeof(*tidq));
356 /* Get a valid index for the new traceid_queue */
357 idx = intlist__nr_entries(traceid_queues_list);
358 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
359 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
363 /* Associate this traceID with this index */
364 inode->priv = (void *)(intptr_t)idx;
366 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
369 /* Grow the traceid_queues array by one unit */
370 traceid_queues = etmq->traceid_queues;
371 traceid_queues = reallocarray(traceid_queues,
373 sizeof(*traceid_queues));
376 * On failure reallocarray() returns NULL and the original block of
377 * memory is left untouched.
382 traceid_queues[idx] = tidq;
383 etmq->traceid_queues = traceid_queues;
385 return etmq->traceid_queues[idx];
389 * Function intlist__remove() removes the inode from the list
390 * and delete the memory associated to it.
392 intlist__remove(traceid_queues_list, inode);
398 struct cs_etm_packet_queue
399 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
401 struct cs_etm_traceid_queue *tidq;
403 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
405 return &tidq->packet_queue;
410 static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm,
411 struct cs_etm_traceid_queue *tidq)
413 struct cs_etm_packet *tmp;
415 if (etm->sample_branches || etm->synth_opts.last_branch ||
416 etm->sample_instructions) {
418 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
419 * the next incoming packet.
422 tidq->packet = tidq->prev_packet;
423 tidq->prev_packet = tmp;
427 static void cs_etm__packet_dump(const char *pkt_string)
429 const char *color = PERF_COLOR_BLUE;
430 int len = strlen(pkt_string);
432 if (len && (pkt_string[len-1] == '\n'))
433 color_fprintf(stdout, color, " %s", pkt_string);
435 color_fprintf(stdout, color, " %s\n", pkt_string);
440 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
441 struct cs_etm_auxtrace *etm, int idx,
444 u64 **metadata = etm->metadata;
446 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
447 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
448 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
451 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
452 struct cs_etm_auxtrace *etm, int idx)
454 u64 **metadata = etm->metadata;
456 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
457 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
458 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
459 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
460 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
461 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
462 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
465 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
466 struct cs_etm_auxtrace *etm)
472 for (i = 0; i < etm->num_cpu; i++) {
473 architecture = etm->metadata[i][CS_ETM_MAGIC];
475 switch (architecture) {
476 case __perf_cs_etmv3_magic:
477 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
478 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
480 case __perf_cs_etmv4_magic:
481 cs_etm__set_trace_param_etmv4(t_params, etm, i);
491 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
492 struct cs_etm_queue *etmq,
493 enum cs_etm_decoder_operation mode)
497 if (!(mode < CS_ETM_OPERATION_MAX))
500 d_params->packet_printer = cs_etm__packet_dump;
501 d_params->operation = mode;
502 d_params->data = etmq;
503 d_params->formatted = true;
504 d_params->fsyncs = false;
505 d_params->hsyncs = false;
506 d_params->frame_aligned = true;
513 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
514 struct auxtrace_buffer *buffer)
517 const char *color = PERF_COLOR_BLUE;
518 struct cs_etm_decoder_params d_params;
519 struct cs_etm_trace_params *t_params;
520 struct cs_etm_decoder *decoder;
521 size_t buffer_used = 0;
523 fprintf(stdout, "\n");
524 color_fprintf(stdout, color,
525 ". ... CoreSight ETM Trace data: size %zu bytes\n",
528 /* Use metadata to fill in trace parameters for trace decoder */
529 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
534 if (cs_etm__init_trace_params(t_params, etm))
537 /* Set decoder parameters to simply print the trace packets */
538 if (cs_etm__init_decoder_params(&d_params, NULL,
539 CS_ETM_OPERATION_PRINT))
542 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
549 ret = cs_etm_decoder__process_data_block(
550 decoder, buffer->offset,
551 &((u8 *)buffer->data)[buffer_used],
552 buffer->size - buffer_used, &consumed);
556 buffer_used += consumed;
557 } while (buffer_used < buffer->size);
559 cs_etm_decoder__free(decoder);
565 static int cs_etm__flush_events(struct perf_session *session,
566 struct perf_tool *tool)
569 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
570 struct cs_etm_auxtrace,
575 if (!tool->ordered_events)
578 ret = cs_etm__update_queues(etm);
583 if (etm->timeless_decoding)
584 return cs_etm__process_timeless_queues(etm, -1);
586 return cs_etm__process_queues(etm);
589 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
593 struct int_node *inode, *tmp;
594 struct cs_etm_traceid_queue *tidq;
595 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
597 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
598 priv = (uintptr_t)inode->priv;
601 /* Free this traceid_queue from the array */
602 tidq = etmq->traceid_queues[idx];
603 thread__zput(tidq->thread);
604 zfree(&tidq->event_buf);
605 zfree(&tidq->last_branch);
606 zfree(&tidq->last_branch_rb);
607 zfree(&tidq->prev_packet);
608 zfree(&tidq->packet);
612 * Function intlist__remove() removes the inode from the list
613 * and delete the memory associated to it.
615 intlist__remove(traceid_queues_list, inode);
618 /* Then the RB tree itself */
619 intlist__delete(traceid_queues_list);
620 etmq->traceid_queues_list = NULL;
622 /* finally free the traceid_queues array */
623 zfree(&etmq->traceid_queues);
626 static void cs_etm__free_queue(void *priv)
628 struct cs_etm_queue *etmq = priv;
633 cs_etm_decoder__free(etmq->decoder);
634 cs_etm__free_traceid_queues(etmq);
638 static void cs_etm__free_events(struct perf_session *session)
641 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
642 struct cs_etm_auxtrace,
644 struct auxtrace_queues *queues = &aux->queues;
646 for (i = 0; i < queues->nr_queues; i++) {
647 cs_etm__free_queue(queues->queue_array[i].priv);
648 queues->queue_array[i].priv = NULL;
651 auxtrace_queues__free(queues);
654 static void cs_etm__free(struct perf_session *session)
657 struct int_node *inode, *tmp;
658 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
659 struct cs_etm_auxtrace,
661 cs_etm__free_events(session);
662 session->auxtrace = NULL;
664 /* First remove all traceID/metadata nodes for the RB tree */
665 intlist__for_each_entry_safe(inode, tmp, traceid_list)
666 intlist__remove(traceid_list, inode);
667 /* Then the RB tree itself */
668 intlist__delete(traceid_list);
670 for (i = 0; i < aux->num_cpu; i++)
671 zfree(&aux->metadata[i]);
673 thread__zput(aux->unknown_thread);
674 zfree(&aux->metadata);
678 static bool cs_etm__evsel_is_auxtrace(struct perf_session *session,
681 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
682 struct cs_etm_auxtrace,
685 return evsel->core.attr.type == aux->pmu_type;
688 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
690 struct machine *machine;
692 machine = etmq->etm->machine;
694 if (address >= etmq->etm->kernel_start) {
695 if (machine__is_host(machine))
696 return PERF_RECORD_MISC_KERNEL;
698 return PERF_RECORD_MISC_GUEST_KERNEL;
700 if (machine__is_host(machine))
701 return PERF_RECORD_MISC_USER;
703 return PERF_RECORD_MISC_GUEST_USER;
705 return PERF_RECORD_MISC_HYPERVISOR;
709 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
710 u64 address, size_t size, u8 *buffer)
715 struct thread *thread;
716 struct machine *machine;
717 struct addr_location al;
718 struct cs_etm_traceid_queue *tidq;
723 machine = etmq->etm->machine;
724 cpumode = cs_etm__cpu_mode(etmq, address);
725 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
729 thread = tidq->thread;
731 if (cpumode != PERF_RECORD_MISC_KERNEL)
733 thread = etmq->etm->unknown_thread;
736 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
739 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
740 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
743 offset = al.map->map_ip(al.map, address);
747 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
755 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
757 struct cs_etm_decoder_params d_params;
758 struct cs_etm_trace_params *t_params = NULL;
759 struct cs_etm_queue *etmq;
761 etmq = zalloc(sizeof(*etmq));
765 etmq->traceid_queues_list = intlist__new(NULL);
766 if (!etmq->traceid_queues_list)
769 /* Use metadata to fill in trace parameters for trace decoder */
770 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
775 if (cs_etm__init_trace_params(t_params, etm))
778 /* Set decoder parameters to decode trace packets */
779 if (cs_etm__init_decoder_params(&d_params, etmq,
780 CS_ETM_OPERATION_DECODE))
783 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
789 * Register a function to handle all memory accesses required by
790 * the trace decoder library.
792 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
795 goto out_free_decoder;
801 cs_etm_decoder__free(etmq->decoder);
803 intlist__delete(etmq->traceid_queues_list);
809 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
810 struct auxtrace_queue *queue,
811 unsigned int queue_nr)
814 unsigned int cs_queue_nr;
817 struct cs_etm_queue *etmq = queue->priv;
819 if (list_empty(&queue->head) || etmq)
822 etmq = cs_etm__alloc_queue(etm);
831 etmq->queue_nr = queue_nr;
834 if (etm->timeless_decoding)
838 * We are under a CPU-wide trace scenario. As such we need to know
839 * when the code that generated the traces started to execute so that
840 * it can be correlated with execution on other CPUs. So we get a
841 * handle on the beginning of traces and decode until we find a
842 * timestamp. The timestamp is then added to the auxtrace min heap
843 * in order to know what nibble (of all the etmqs) to decode first.
847 * Fetch an aux_buffer from this etmq. Bail if no more
848 * blocks or an error has been encountered.
850 ret = cs_etm__get_data_block(etmq);
855 * Run decoder on the trace block. The decoder will stop when
856 * encountering a CS timestamp, a full packet queue or the end of
857 * trace for that block.
859 ret = cs_etm__decode_data_block(etmq);
864 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
865 * the timestamp calculation for us.
867 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
869 /* We found a timestamp, no need to continue. */
874 * We didn't find a timestamp so empty all the traceid packet
875 * queues before looking for another timestamp packet, either
876 * in the current data block or a new one. Packets that were
877 * just decoded are useless since no timestamp has been
878 * associated with them. As such simply discard them.
880 cs_etm__clear_all_packet_queues(etmq);
884 * We have a timestamp. Add it to the min heap to reflect when
885 * instructions conveyed by the range packets of this traceID queue
886 * started to execute. Once the same has been done for all the traceID
887 * queues of each etmq, redenring and decoding can start in
888 * chronological order.
890 * Note that packets decoded above are still in the traceID's packet
891 * queue and will be processed in cs_etm__process_queues().
893 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
894 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
899 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
904 if (!etm->kernel_start)
905 etm->kernel_start = machine__kernel_start(etm->machine);
907 for (i = 0; i < etm->queues.nr_queues; i++) {
908 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
916 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
918 if (etm->queues.new_data) {
919 etm->queues.new_data = false;
920 return cs_etm__setup_queues(etm);
927 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
928 struct cs_etm_traceid_queue *tidq)
930 struct branch_stack *bs_src = tidq->last_branch_rb;
931 struct branch_stack *bs_dst = tidq->last_branch;
935 * Set the number of records before early exit: ->nr is used to
936 * determine how many branches to copy from ->entries.
938 bs_dst->nr = bs_src->nr;
941 * Early exit when there is nothing to copy.
947 * As bs_src->entries is a circular buffer, we need to copy from it in
948 * two steps. First, copy the branches from the most recently inserted
949 * branch ->last_branch_pos until the end of bs_src->entries buffer.
951 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
952 memcpy(&bs_dst->entries[0],
953 &bs_src->entries[tidq->last_branch_pos],
954 sizeof(struct branch_entry) * nr);
957 * If we wrapped around at least once, the branches from the beginning
958 * of the bs_src->entries buffer and until the ->last_branch_pos element
959 * are older valid branches: copy them over. The total number of
960 * branches copied over will be equal to the number of branches asked by
961 * the user in last_branch_sz.
963 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
964 memcpy(&bs_dst->entries[nr],
966 sizeof(struct branch_entry) * tidq->last_branch_pos);
971 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
973 tidq->last_branch_pos = 0;
974 tidq->last_branch_rb->nr = 0;
977 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
978 u8 trace_chan_id, u64 addr)
982 cs_etm__mem_access(etmq, trace_chan_id, addr,
983 ARRAY_SIZE(instrBytes), instrBytes);
985 * T32 instruction size is indicated by bits[15:11] of the first
986 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
987 * denote a 32-bit instruction.
989 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
992 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
994 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
995 if (packet->sample_type == CS_ETM_DISCONTINUITY)
998 return packet->start_addr;
1002 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
1004 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
1005 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1008 return packet->end_addr - packet->last_instr_size;
1011 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
1013 const struct cs_etm_packet *packet,
1016 if (packet->isa == CS_ETM_ISA_T32) {
1017 u64 addr = packet->start_addr;
1020 addr += cs_etm__t32_instr_size(etmq,
1021 trace_chan_id, addr);
1027 /* Assume a 4 byte instruction size (A32/A64) */
1028 return packet->start_addr + offset * 4;
1031 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
1032 struct cs_etm_traceid_queue *tidq)
1034 struct branch_stack *bs = tidq->last_branch_rb;
1035 struct branch_entry *be;
1038 * The branches are recorded in a circular buffer in reverse
1039 * chronological order: we start recording from the last element of the
1040 * buffer down. After writing the first element of the stack, move the
1041 * insert position back to the end of the buffer.
1043 if (!tidq->last_branch_pos)
1044 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1046 tidq->last_branch_pos -= 1;
1048 be = &bs->entries[tidq->last_branch_pos];
1049 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1050 be->to = cs_etm__first_executed_instr(tidq->packet);
1051 /* No support for mispredict */
1052 be->flags.mispred = 0;
1053 be->flags.predicted = 1;
1056 * Increment bs->nr until reaching the number of last branches asked by
1057 * the user on the command line.
1059 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1063 static int cs_etm__inject_event(union perf_event *event,
1064 struct perf_sample *sample, u64 type)
1066 event->header.size = perf_event__sample_event_size(sample, type, 0);
1067 return perf_event__synthesize_sample(event, type, 0, sample);
1072 cs_etm__get_trace(struct cs_etm_queue *etmq)
1074 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1075 struct auxtrace_buffer *old_buffer = aux_buffer;
1076 struct auxtrace_queue *queue;
1078 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1080 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1082 /* If no more data, drop the previous auxtrace_buffer and return */
1085 auxtrace_buffer__drop_data(old_buffer);
1090 etmq->buffer = aux_buffer;
1092 /* If the aux_buffer doesn't have data associated, try to load it */
1093 if (!aux_buffer->data) {
1094 /* get the file desc associated with the perf data file */
1095 int fd = perf_data__fd(etmq->etm->session->data);
1097 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1098 if (!aux_buffer->data)
1102 /* If valid, drop the previous buffer */
1104 auxtrace_buffer__drop_data(old_buffer);
1107 etmq->buf_len = aux_buffer->size;
1108 etmq->buf = aux_buffer->data;
1110 return etmq->buf_len;
1113 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1114 struct cs_etm_traceid_queue *tidq)
1116 if ((!tidq->thread) && (tidq->tid != -1))
1117 tidq->thread = machine__find_thread(etm->machine, -1,
1121 tidq->pid = tidq->thread->pid_;
1124 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1125 pid_t tid, u8 trace_chan_id)
1127 int cpu, err = -EINVAL;
1128 struct cs_etm_auxtrace *etm = etmq->etm;
1129 struct cs_etm_traceid_queue *tidq;
1131 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1135 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1138 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1143 thread__zput(tidq->thread);
1145 cs_etm__set_pid_tid_cpu(etm, tidq);
1149 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1151 return !!etmq->etm->timeless_decoding;
1154 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1156 const struct cs_etm_packet *packet,
1157 struct perf_sample *sample)
1160 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1161 * packet, so directly bail out with 'insn_len' = 0.
1163 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1164 sample->insn_len = 0;
1169 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1170 * cs_etm__t32_instr_size().
1172 if (packet->isa == CS_ETM_ISA_T32)
1173 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1175 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1177 sample->insn_len = 4;
1179 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1180 sample->insn_len, (void *)sample->insn);
1183 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1184 struct cs_etm_traceid_queue *tidq,
1185 u64 addr, u64 period)
1188 struct cs_etm_auxtrace *etm = etmq->etm;
1189 union perf_event *event = tidq->event_buf;
1190 struct perf_sample sample = {.ip = 0,};
1192 event->sample.header.type = PERF_RECORD_SAMPLE;
1193 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1194 event->sample.header.size = sizeof(struct perf_event_header);
1196 if (!etm->timeless_decoding)
1197 sample.time = etm->latest_kernel_timestamp;
1199 sample.pid = tidq->pid;
1200 sample.tid = tidq->tid;
1201 sample.id = etmq->etm->instructions_id;
1202 sample.stream_id = etmq->etm->instructions_id;
1203 sample.period = period;
1204 sample.cpu = tidq->packet->cpu;
1205 sample.flags = tidq->prev_packet->flags;
1206 sample.cpumode = event->sample.header.misc;
1208 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1210 if (etm->synth_opts.last_branch)
1211 sample.branch_stack = tidq->last_branch;
1213 if (etm->synth_opts.inject) {
1214 ret = cs_etm__inject_event(event, &sample,
1215 etm->instructions_sample_type);
1220 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1224 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1231 * The cs etm packet encodes an instruction range between a branch target
1232 * and the next taken branch. Generate sample accordingly.
1234 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1235 struct cs_etm_traceid_queue *tidq)
1238 struct cs_etm_auxtrace *etm = etmq->etm;
1239 struct perf_sample sample = {.ip = 0,};
1240 union perf_event *event = tidq->event_buf;
1241 struct dummy_branch_stack {
1244 struct branch_entry entries;
1248 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1250 event->sample.header.type = PERF_RECORD_SAMPLE;
1251 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1252 event->sample.header.size = sizeof(struct perf_event_header);
1254 if (!etm->timeless_decoding)
1255 sample.time = etm->latest_kernel_timestamp;
1257 sample.pid = tidq->pid;
1258 sample.tid = tidq->tid;
1259 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1260 sample.id = etmq->etm->branches_id;
1261 sample.stream_id = etmq->etm->branches_id;
1263 sample.cpu = tidq->packet->cpu;
1264 sample.flags = tidq->prev_packet->flags;
1265 sample.cpumode = event->sample.header.misc;
1267 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1271 * perf report cannot handle events without a branch stack
1273 if (etm->synth_opts.last_branch) {
1274 dummy_bs = (struct dummy_branch_stack){
1282 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1285 if (etm->synth_opts.inject) {
1286 ret = cs_etm__inject_event(event, &sample,
1287 etm->branches_sample_type);
1292 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1296 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1302 struct cs_etm_synth {
1303 struct perf_tool dummy_tool;
1304 struct perf_session *session;
1307 static int cs_etm__event_synth(struct perf_tool *tool,
1308 union perf_event *event,
1309 struct perf_sample *sample __maybe_unused,
1310 struct machine *machine __maybe_unused)
1312 struct cs_etm_synth *cs_etm_synth =
1313 container_of(tool, struct cs_etm_synth, dummy_tool);
1315 return perf_session__deliver_synth_event(cs_etm_synth->session,
1319 static int cs_etm__synth_event(struct perf_session *session,
1320 struct perf_event_attr *attr, u64 id)
1322 struct cs_etm_synth cs_etm_synth;
1324 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1325 cs_etm_synth.session = session;
1327 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1328 &id, cs_etm__event_synth);
1331 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1332 struct perf_session *session)
1334 struct evlist *evlist = session->evlist;
1335 struct evsel *evsel;
1336 struct perf_event_attr attr;
1341 evlist__for_each_entry(evlist, evsel) {
1342 if (evsel->core.attr.type == etm->pmu_type) {
1349 pr_debug("No selected events with CoreSight Trace data\n");
1353 memset(&attr, 0, sizeof(struct perf_event_attr));
1354 attr.size = sizeof(struct perf_event_attr);
1355 attr.type = PERF_TYPE_HARDWARE;
1356 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1357 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1359 if (etm->timeless_decoding)
1360 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1362 attr.sample_type |= PERF_SAMPLE_TIME;
1364 attr.exclude_user = evsel->core.attr.exclude_user;
1365 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1366 attr.exclude_hv = evsel->core.attr.exclude_hv;
1367 attr.exclude_host = evsel->core.attr.exclude_host;
1368 attr.exclude_guest = evsel->core.attr.exclude_guest;
1369 attr.sample_id_all = evsel->core.attr.sample_id_all;
1370 attr.read_format = evsel->core.attr.read_format;
1372 /* create new id val to be a fixed offset from evsel id */
1373 id = evsel->core.id[0] + 1000000000;
1378 if (etm->synth_opts.branches) {
1379 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1380 attr.sample_period = 1;
1381 attr.sample_type |= PERF_SAMPLE_ADDR;
1382 err = cs_etm__synth_event(session, &attr, id);
1385 etm->sample_branches = true;
1386 etm->branches_sample_type = attr.sample_type;
1387 etm->branches_id = id;
1389 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1392 if (etm->synth_opts.last_branch) {
1393 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1395 * We don't use the hardware index, but the sample generation
1396 * code uses the new format branch_stack with this field,
1397 * so the event attributes must indicate that it's present.
1399 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
1402 if (etm->synth_opts.instructions) {
1403 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1404 attr.sample_period = etm->synth_opts.period;
1405 etm->instructions_sample_period = attr.sample_period;
1406 err = cs_etm__synth_event(session, &attr, id);
1409 etm->sample_instructions = true;
1410 etm->instructions_sample_type = attr.sample_type;
1411 etm->instructions_id = id;
1418 static int cs_etm__sample(struct cs_etm_queue *etmq,
1419 struct cs_etm_traceid_queue *tidq)
1421 struct cs_etm_auxtrace *etm = etmq->etm;
1423 u8 trace_chan_id = tidq->trace_chan_id;
1426 /* Get instructions remainder from previous packet */
1427 instrs_prev = tidq->period_instructions;
1429 tidq->period_instructions += tidq->packet->instr_count;
1432 * Record a branch when the last instruction in
1433 * PREV_PACKET is a branch.
1435 if (etm->synth_opts.last_branch &&
1436 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1437 tidq->prev_packet->last_instr_taken_branch)
1438 cs_etm__update_last_branch_rb(etmq, tidq);
1440 if (etm->sample_instructions &&
1441 tidq->period_instructions >= etm->instructions_sample_period) {
1443 * Emit instruction sample periodically
1444 * TODO: allow period to be defined in cycles and clock time
1448 * Below diagram demonstrates the instruction samples
1451 * Instrs Instrs Instrs Instrs
1452 * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3)
1455 * --------------------------------------------------
1459 * instructions(Pi) instructions(Pi')
1462 * \---------------- -----------------/
1464 * tidq->packet->instr_count
1466 * Instrs Sample(n...) are the synthesised samples occurring
1467 * every etm->instructions_sample_period instructions - as
1468 * defined on the perf command line. Sample(n) is being the
1469 * last sample before the current etm packet, n+1 to n+3
1470 * samples are generated from the current etm packet.
1472 * tidq->packet->instr_count represents the number of
1473 * instructions in the current etm packet.
1475 * Period instructions (Pi) contains the the number of
1476 * instructions executed after the sample point(n) from the
1477 * previous etm packet. This will always be less than
1478 * etm->instructions_sample_period.
1480 * When generate new samples, it combines with two parts
1481 * instructions, one is the tail of the old packet and another
1482 * is the head of the new coming packet, to generate
1483 * sample(n+1); sample(n+2) and sample(n+3) consume the
1484 * instructions with sample period. After sample(n+3), the rest
1485 * instructions will be used by later packet and it is assigned
1486 * to tidq->period_instructions for next round calculation.
1490 * Get the initial offset into the current packet instructions;
1491 * entry conditions ensure that instrs_prev is less than
1492 * etm->instructions_sample_period.
1494 u64 offset = etm->instructions_sample_period - instrs_prev;
1497 /* Prepare last branches for instruction sample */
1498 if (etm->synth_opts.last_branch)
1499 cs_etm__copy_last_branch_rb(etmq, tidq);
1501 while (tidq->period_instructions >=
1502 etm->instructions_sample_period) {
1504 * Calculate the address of the sampled instruction (-1
1505 * as sample is reported as though instruction has just
1506 * been executed, but PC has not advanced to next
1509 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1510 tidq->packet, offset - 1);
1511 ret = cs_etm__synth_instruction_sample(
1513 etm->instructions_sample_period);
1517 offset += etm->instructions_sample_period;
1518 tidq->period_instructions -=
1519 etm->instructions_sample_period;
1523 if (etm->sample_branches) {
1524 bool generate_sample = false;
1526 /* Generate sample for tracing on packet */
1527 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1528 generate_sample = true;
1530 /* Generate sample for branch taken packet */
1531 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1532 tidq->prev_packet->last_instr_taken_branch)
1533 generate_sample = true;
1535 if (generate_sample) {
1536 ret = cs_etm__synth_branch_sample(etmq, tidq);
1542 cs_etm__packet_swap(etm, tidq);
1547 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1550 * When the exception packet is inserted, whether the last instruction
1551 * in previous range packet is taken branch or not, we need to force
1552 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1553 * to generate branch sample for the instruction range before the
1554 * exception is trapped to kernel or before the exception returning.
1556 * The exception packet includes the dummy address values, so don't
1557 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1558 * for generating instruction and branch samples.
1560 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1561 tidq->prev_packet->last_instr_taken_branch = true;
1566 static int cs_etm__flush(struct cs_etm_queue *etmq,
1567 struct cs_etm_traceid_queue *tidq)
1570 struct cs_etm_auxtrace *etm = etmq->etm;
1572 /* Handle start tracing packet */
1573 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1576 if (etmq->etm->synth_opts.last_branch &&
1577 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1580 /* Prepare last branches for instruction sample */
1581 cs_etm__copy_last_branch_rb(etmq, tidq);
1584 * Generate a last branch event for the branches left in the
1585 * circular buffer at the end of the trace.
1587 * Use the address of the end of the last reported execution
1590 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1592 err = cs_etm__synth_instruction_sample(
1594 tidq->period_instructions);
1598 tidq->period_instructions = 0;
1602 if (etm->sample_branches &&
1603 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1604 err = cs_etm__synth_branch_sample(etmq, tidq);
1610 cs_etm__packet_swap(etm, tidq);
1612 /* Reset last branches after flush the trace */
1613 if (etm->synth_opts.last_branch)
1614 cs_etm__reset_last_branch_rb(tidq);
1619 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1620 struct cs_etm_traceid_queue *tidq)
1625 * It has no new packet coming and 'etmq->packet' contains the stale
1626 * packet which was set at the previous time with packets swapping;
1627 * so skip to generate branch sample to avoid stale packet.
1629 * For this case only flush branch stack and generate a last branch
1630 * event for the branches left in the circular buffer at the end of
1633 if (etmq->etm->synth_opts.last_branch &&
1634 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1637 /* Prepare last branches for instruction sample */
1638 cs_etm__copy_last_branch_rb(etmq, tidq);
1641 * Use the address of the end of the last reported execution
1644 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1646 err = cs_etm__synth_instruction_sample(
1648 tidq->period_instructions);
1652 tidq->period_instructions = 0;
1658 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1660 * Returns: < 0 if error
1661 * = 0 if no more auxtrace_buffer to read
1662 * > 0 if the current buffer isn't empty yet
1664 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1668 if (!etmq->buf_len) {
1669 ret = cs_etm__get_trace(etmq);
1673 * We cannot assume consecutive blocks in the data file
1674 * are contiguous, reset the decoder to force re-sync.
1676 ret = cs_etm_decoder__reset(etmq->decoder);
1681 return etmq->buf_len;
1684 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1685 struct cs_etm_packet *packet,
1688 /* Initialise to keep compiler happy */
1693 switch (packet->isa) {
1694 case CS_ETM_ISA_T32:
1696 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1699 * +-----------------+--------+
1700 * | 1 1 0 1 1 1 1 1 | imm8 |
1701 * +-----------------+--------+
1703 * According to the specification, it only defines SVC for T32
1704 * with 16 bits instruction and has no definition for 32bits;
1705 * so below only read 2 bytes as instruction size for T32.
1707 addr = end_addr - 2;
1708 cs_etm__mem_access(etmq, trace_chan_id, addr,
1709 sizeof(instr16), (u8 *)&instr16);
1710 if ((instr16 & 0xFF00) == 0xDF00)
1714 case CS_ETM_ISA_A32:
1716 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1718 * b'31 b'28 b'27 b'24
1719 * +---------+---------+-------------------------+
1720 * | !1111 | 1 1 1 1 | imm24 |
1721 * +---------+---------+-------------------------+
1723 addr = end_addr - 4;
1724 cs_etm__mem_access(etmq, trace_chan_id, addr,
1725 sizeof(instr32), (u8 *)&instr32);
1726 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1727 (instr32 & 0xF0000000) != 0xF0000000)
1731 case CS_ETM_ISA_A64:
1733 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1736 * +-----------------------+---------+-----------+
1737 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1738 * +-----------------------+---------+-----------+
1740 addr = end_addr - 4;
1741 cs_etm__mem_access(etmq, trace_chan_id, addr,
1742 sizeof(instr32), (u8 *)&instr32);
1743 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1747 case CS_ETM_ISA_UNKNOWN:
1755 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1756 struct cs_etm_traceid_queue *tidq, u64 magic)
1758 u8 trace_chan_id = tidq->trace_chan_id;
1759 struct cs_etm_packet *packet = tidq->packet;
1760 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1762 if (magic == __perf_cs_etmv3_magic)
1763 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1767 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1768 * HVC cases; need to check if it's SVC instruction based on
1771 if (magic == __perf_cs_etmv4_magic) {
1772 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1773 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1774 prev_packet->end_addr))
1781 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1784 struct cs_etm_packet *packet = tidq->packet;
1786 if (magic == __perf_cs_etmv3_magic)
1787 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1788 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1789 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1790 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1791 packet->exception_number == CS_ETMV3_EXC_FIQ)
1794 if (magic == __perf_cs_etmv4_magic)
1795 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1796 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1797 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1798 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1799 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1800 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1801 packet->exception_number == CS_ETMV4_EXC_FIQ)
1807 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1808 struct cs_etm_traceid_queue *tidq,
1811 u8 trace_chan_id = tidq->trace_chan_id;
1812 struct cs_etm_packet *packet = tidq->packet;
1813 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1815 if (magic == __perf_cs_etmv3_magic)
1816 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1817 packet->exception_number == CS_ETMV3_EXC_HYP ||
1818 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1819 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1820 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1821 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1822 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1825 if (magic == __perf_cs_etmv4_magic) {
1826 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1827 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1828 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1829 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1833 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1834 * (SMC, HVC) are taken as sync exceptions.
1836 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1837 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1838 prev_packet->end_addr))
1842 * ETMv4 has 5 bits for exception number; if the numbers
1843 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1844 * they are implementation defined exceptions.
1846 * For this case, simply take it as sync exception.
1848 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1849 packet->exception_number <= CS_ETMV4_EXC_END)
1856 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1857 struct cs_etm_traceid_queue *tidq)
1859 struct cs_etm_packet *packet = tidq->packet;
1860 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1861 u8 trace_chan_id = tidq->trace_chan_id;
1865 switch (packet->sample_type) {
1868 * Immediate branch instruction without neither link nor
1869 * return flag, it's normal branch instruction within
1872 if (packet->last_instr_type == OCSD_INSTR_BR &&
1873 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1874 packet->flags = PERF_IP_FLAG_BRANCH;
1876 if (packet->last_instr_cond)
1877 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1881 * Immediate branch instruction with link (e.g. BL), this is
1882 * branch instruction for function call.
1884 if (packet->last_instr_type == OCSD_INSTR_BR &&
1885 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1886 packet->flags = PERF_IP_FLAG_BRANCH |
1890 * Indirect branch instruction with link (e.g. BLR), this is
1891 * branch instruction for function call.
1893 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1894 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1895 packet->flags = PERF_IP_FLAG_BRANCH |
1899 * Indirect branch instruction with subtype of
1900 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1901 * function return for A32/T32.
1903 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1904 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1905 packet->flags = PERF_IP_FLAG_BRANCH |
1906 PERF_IP_FLAG_RETURN;
1909 * Indirect branch instruction without link (e.g. BR), usually
1910 * this is used for function return, especially for functions
1911 * within dynamic link lib.
1913 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1914 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1915 packet->flags = PERF_IP_FLAG_BRANCH |
1916 PERF_IP_FLAG_RETURN;
1918 /* Return instruction for function return. */
1919 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1920 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1921 packet->flags = PERF_IP_FLAG_BRANCH |
1922 PERF_IP_FLAG_RETURN;
1925 * Decoder might insert a discontinuity in the middle of
1926 * instruction packets, fixup prev_packet with flag
1927 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1929 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1930 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1931 PERF_IP_FLAG_TRACE_BEGIN;
1934 * If the previous packet is an exception return packet
1935 * and the return address just follows SVC instruction,
1936 * it needs to calibrate the previous packet sample flags
1937 * as PERF_IP_FLAG_SYSCALLRET.
1939 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1940 PERF_IP_FLAG_RETURN |
1941 PERF_IP_FLAG_INTERRUPT) &&
1942 cs_etm__is_svc_instr(etmq, trace_chan_id,
1943 packet, packet->start_addr))
1944 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1945 PERF_IP_FLAG_RETURN |
1946 PERF_IP_FLAG_SYSCALLRET;
1948 case CS_ETM_DISCONTINUITY:
1950 * The trace is discontinuous, if the previous packet is
1951 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1952 * for previous packet.
1954 if (prev_packet->sample_type == CS_ETM_RANGE)
1955 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1956 PERF_IP_FLAG_TRACE_END;
1958 case CS_ETM_EXCEPTION:
1959 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1963 /* The exception is for system call. */
1964 if (cs_etm__is_syscall(etmq, tidq, magic))
1965 packet->flags = PERF_IP_FLAG_BRANCH |
1967 PERF_IP_FLAG_SYSCALLRET;
1969 * The exceptions are triggered by external signals from bus,
1970 * interrupt controller, debug module, PE reset or halt.
1972 else if (cs_etm__is_async_exception(tidq, magic))
1973 packet->flags = PERF_IP_FLAG_BRANCH |
1975 PERF_IP_FLAG_ASYNC |
1976 PERF_IP_FLAG_INTERRUPT;
1978 * Otherwise, exception is caused by trap, instruction &
1979 * data fault, or alignment errors.
1981 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1982 packet->flags = PERF_IP_FLAG_BRANCH |
1984 PERF_IP_FLAG_INTERRUPT;
1987 * When the exception packet is inserted, since exception
1988 * packet is not used standalone for generating samples
1989 * and it's affiliation to the previous instruction range
1990 * packet; so set previous range packet flags to tell perf
1991 * it is an exception taken branch.
1993 if (prev_packet->sample_type == CS_ETM_RANGE)
1994 prev_packet->flags = packet->flags;
1996 case CS_ETM_EXCEPTION_RET:
1998 * When the exception return packet is inserted, since
1999 * exception return packet is not used standalone for
2000 * generating samples and it's affiliation to the previous
2001 * instruction range packet; so set previous range packet
2002 * flags to tell perf it is an exception return branch.
2004 * The exception return can be for either system call or
2005 * other exception types; unfortunately the packet doesn't
2006 * contain exception type related info so we cannot decide
2007 * the exception type purely based on exception return packet.
2008 * If we record the exception number from exception packet and
2009 * reuse it for exception return packet, this is not reliable
2010 * due the trace can be discontinuity or the interrupt can
2011 * be nested, thus the recorded exception number cannot be
2012 * used for exception return packet for these two cases.
2014 * For exception return packet, we only need to distinguish the
2015 * packet is for system call or for other types. Thus the
2016 * decision can be deferred when receive the next packet which
2017 * contains the return address, based on the return address we
2018 * can read out the previous instruction and check if it's a
2019 * system call instruction and then calibrate the sample flag
2022 if (prev_packet->sample_type == CS_ETM_RANGE)
2023 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2024 PERF_IP_FLAG_RETURN |
2025 PERF_IP_FLAG_INTERRUPT;
2035 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
2038 size_t processed = 0;
2041 * Packets are decoded and added to the decoder's packet queue
2042 * until the decoder packet processing callback has requested that
2043 * processing stops or there is nothing left in the buffer. Normal
2044 * operations that stop processing are a timestamp packet or a full
2045 * decoder buffer queue.
2047 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2049 &etmq->buf[etmq->buf_used],
2055 etmq->offset += processed;
2056 etmq->buf_used += processed;
2057 etmq->buf_len -= processed;
2063 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
2064 struct cs_etm_traceid_queue *tidq)
2067 struct cs_etm_packet_queue *packet_queue;
2069 packet_queue = &tidq->packet_queue;
2071 /* Process each packet in this chunk */
2073 ret = cs_etm_decoder__get_packet(packet_queue,
2077 * Stop processing this chunk on
2078 * end of data or error
2083 * Since packet addresses are swapped in packet
2084 * handling within below switch() statements,
2085 * thus setting sample flags must be called
2086 * prior to switch() statement to use address
2087 * information before packets swapping.
2089 ret = cs_etm__set_sample_flags(etmq, tidq);
2093 switch (tidq->packet->sample_type) {
2096 * If the packet contains an instruction
2097 * range, generate instruction sequence
2100 cs_etm__sample(etmq, tidq);
2102 case CS_ETM_EXCEPTION:
2103 case CS_ETM_EXCEPTION_RET:
2105 * If the exception packet is coming,
2106 * make sure the previous instruction
2107 * range packet to be handled properly.
2109 cs_etm__exception(tidq);
2111 case CS_ETM_DISCONTINUITY:
2113 * Discontinuity in trace, flush
2114 * previous branch stack
2116 cs_etm__flush(etmq, tidq);
2120 * Should not receive empty packet,
2123 pr_err("CS ETM Trace: empty packet\n");
2133 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2136 struct int_node *inode;
2137 struct cs_etm_traceid_queue *tidq;
2138 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2140 intlist__for_each_entry(inode, traceid_queues_list) {
2141 idx = (int)(intptr_t)inode->priv;
2142 tidq = etmq->traceid_queues[idx];
2144 /* Ignore return value */
2145 cs_etm__process_traceid_queue(etmq, tidq);
2148 * Generate an instruction sample with the remaining
2149 * branchstack entries.
2151 cs_etm__flush(etmq, tidq);
2155 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2158 struct cs_etm_traceid_queue *tidq;
2160 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2164 /* Go through each buffer in the queue and decode them one by one */
2166 err = cs_etm__get_data_block(etmq);
2170 /* Run trace decoder until buffer consumed or end of trace */
2172 err = cs_etm__decode_data_block(etmq);
2177 * Process each packet in this chunk, nothing to do if
2178 * an error occurs other than hoping the next one will
2181 err = cs_etm__process_traceid_queue(etmq, tidq);
2183 } while (etmq->buf_len);
2186 /* Flush any remaining branch stack entries */
2187 err = cs_etm__end_block(etmq, tidq);
2193 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2197 struct auxtrace_queues *queues = &etm->queues;
2199 for (i = 0; i < queues->nr_queues; i++) {
2200 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2201 struct cs_etm_queue *etmq = queue->priv;
2202 struct cs_etm_traceid_queue *tidq;
2207 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2208 CS_ETM_PER_THREAD_TRACEID);
2213 if ((tid == -1) || (tidq->tid == tid)) {
2214 cs_etm__set_pid_tid_cpu(etm, tidq);
2215 cs_etm__run_decoder(etmq);
2222 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2225 unsigned int cs_queue_nr, queue_nr;
2228 struct auxtrace_queue *queue;
2229 struct cs_etm_queue *etmq;
2230 struct cs_etm_traceid_queue *tidq;
2233 if (!etm->heap.heap_cnt)
2236 /* Take the entry at the top of the min heap */
2237 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2238 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2239 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2240 queue = &etm->queues.queue_array[queue_nr];
2244 * Remove the top entry from the heap since we are about
2247 auxtrace_heap__pop(&etm->heap);
2249 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2252 * No traceID queue has been allocated for this traceID,
2253 * which means something somewhere went very wrong. No
2254 * other choice than simply exit.
2261 * Packets associated with this timestamp are already in
2262 * the etmq's traceID queue, so process them.
2264 ret = cs_etm__process_traceid_queue(etmq, tidq);
2269 * Packets for this timestamp have been processed, time to
2270 * move on to the next timestamp, fetching a new auxtrace_buffer
2274 ret = cs_etm__get_data_block(etmq);
2279 * No more auxtrace_buffers to process in this etmq, simply
2280 * move on to another entry in the auxtrace_heap.
2285 ret = cs_etm__decode_data_block(etmq);
2289 cs_timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2291 if (!cs_timestamp) {
2293 * Function cs_etm__decode_data_block() returns when
2294 * there is no more traces to decode in the current
2295 * auxtrace_buffer OR when a timestamp has been
2296 * encountered on any of the traceID queues. Since we
2297 * did not get a timestamp, there is no more traces to
2298 * process in this auxtrace_buffer. As such empty and
2299 * flush all traceID queues.
2301 cs_etm__clear_all_traceid_queues(etmq);
2303 /* Fetch another auxtrace_buffer for this etmq */
2308 * Add to the min heap the timestamp for packets that have
2309 * just been decoded. They will be processed and synthesized
2310 * during the next call to cs_etm__process_traceid_queue() for
2311 * this queue/traceID.
2313 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2314 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2321 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2322 union perf_event *event)
2326 if (etm->timeless_decoding)
2330 * Add the tid/pid to the log so that we can get a match when
2331 * we get a contextID from the decoder.
2333 th = machine__findnew_thread(etm->machine,
2334 event->itrace_start.pid,
2335 event->itrace_start.tid);
2344 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2345 union perf_event *event)
2348 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2351 * Context switch in per-thread mode are irrelevant since perf
2352 * will start/stop tracing as the process is scheduled.
2354 if (etm->timeless_decoding)
2358 * SWITCH_IN events carry the next process to be switched out while
2359 * SWITCH_OUT events carry the process to be switched in. As such
2360 * we don't care about IN events.
2366 * Add the tid/pid to the log so that we can get a match when
2367 * we get a contextID from the decoder.
2369 th = machine__findnew_thread(etm->machine,
2370 event->context_switch.next_prev_pid,
2371 event->context_switch.next_prev_tid);
2380 static int cs_etm__process_event(struct perf_session *session,
2381 union perf_event *event,
2382 struct perf_sample *sample,
2383 struct perf_tool *tool)
2386 u64 sample_kernel_timestamp;
2387 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2388 struct cs_etm_auxtrace,
2394 if (!tool->ordered_events) {
2395 pr_err("CoreSight ETM Trace requires ordered events\n");
2399 if (sample->time && (sample->time != (u64) -1))
2400 sample_kernel_timestamp = sample->time;
2402 sample_kernel_timestamp = 0;
2404 if (sample_kernel_timestamp || etm->timeless_decoding) {
2405 err = cs_etm__update_queues(etm);
2411 * Don't wait for cs_etm__flush_events() in per-thread/timeless mode to start the decode. We
2412 * need the tid of the PERF_RECORD_EXIT event to assign to the synthesised samples because
2413 * ETM_OPT_CTXTID is not enabled.
2415 if (etm->timeless_decoding &&
2416 event->header.type == PERF_RECORD_EXIT)
2417 return cs_etm__process_timeless_queues(etm,
2420 if (event->header.type == PERF_RECORD_ITRACE_START)
2421 return cs_etm__process_itrace_start(etm, event);
2422 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2423 return cs_etm__process_switch_cpu_wide(etm, event);
2425 if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) {
2427 * Record the latest kernel timestamp available in the header
2428 * for samples so that synthesised samples occur from this point
2431 etm->latest_kernel_timestamp = sample_kernel_timestamp;
2437 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2438 union perf_event *event,
2439 struct perf_tool *tool __maybe_unused)
2441 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2442 struct cs_etm_auxtrace,
2444 if (!etm->data_queued) {
2445 struct auxtrace_buffer *buffer;
2447 int fd = perf_data__fd(session->data);
2448 bool is_pipe = perf_data__is_pipe(session->data);
2454 data_offset = lseek(fd, 0, SEEK_CUR);
2455 if (data_offset == -1)
2459 err = auxtrace_queues__add_event(&etm->queues, session,
2460 event, data_offset, &buffer);
2465 if (auxtrace_buffer__get_data(buffer, fd)) {
2466 cs_etm__dump_event(etm, buffer);
2467 auxtrace_buffer__put_data(buffer);
2474 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2476 struct evsel *evsel;
2477 struct evlist *evlist = etm->session->evlist;
2478 bool timeless_decoding = true;
2480 /* Override timeless mode with user input from --itrace=Z */
2481 if (etm->synth_opts.timeless_decoding)
2485 * Circle through the list of event and complain if we find one
2486 * with the time bit set.
2488 evlist__for_each_entry(evlist, evsel) {
2489 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2490 timeless_decoding = false;
2493 return timeless_decoding;
2496 static const char * const cs_etm_global_header_fmts[] = {
2497 [CS_HEADER_VERSION] = " Header version %llx\n",
2498 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2499 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2502 static const char * const cs_etm_priv_fmts[] = {
2503 [CS_ETM_MAGIC] = " Magic number %llx\n",
2504 [CS_ETM_CPU] = " CPU %lld\n",
2505 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2506 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2507 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2508 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2509 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2512 static const char * const cs_etmv4_priv_fmts[] = {
2513 [CS_ETM_MAGIC] = " Magic number %llx\n",
2514 [CS_ETM_CPU] = " CPU %lld\n",
2515 [CS_ETM_NR_TRC_PARAMS] = " NR_TRC_PARAMS %llx\n",
2516 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2517 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2518 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2519 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2520 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2521 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2522 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2525 static const char * const param_unk_fmt =
2526 " Unknown parameter [%d] %llx\n";
2527 static const char * const magic_unk_fmt =
2528 " Magic number Unknown %llx\n";
2530 static int cs_etm__print_cpu_metadata_v0(__u64 *val, int *offset)
2532 int i = *offset, j, nr_params = 0, fmt_offset;
2535 /* check magic value */
2536 magic = val[i + CS_ETM_MAGIC];
2537 if ((magic != __perf_cs_etmv3_magic) &&
2538 (magic != __perf_cs_etmv4_magic)) {
2539 /* failure - note bad magic value */
2540 fprintf(stdout, magic_unk_fmt, magic);
2544 /* print common header block */
2545 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_MAGIC], val[i++]);
2546 fprintf(stdout, cs_etm_priv_fmts[CS_ETM_CPU], val[i++]);
2548 if (magic == __perf_cs_etmv3_magic) {
2549 nr_params = CS_ETM_NR_TRC_PARAMS_V0;
2550 fmt_offset = CS_ETM_ETMCR;
2551 /* after common block, offset format index past NR_PARAMS */
2552 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2553 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2554 } else if (magic == __perf_cs_etmv4_magic) {
2555 nr_params = CS_ETMV4_NR_TRC_PARAMS_V0;
2556 fmt_offset = CS_ETMV4_TRCCONFIGR;
2557 /* after common block, offset format index past NR_PARAMS */
2558 for (j = fmt_offset; j < nr_params + fmt_offset; j++, i++)
2559 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2565 static int cs_etm__print_cpu_metadata_v1(__u64 *val, int *offset)
2567 int i = *offset, j, total_params = 0;
2570 magic = val[i + CS_ETM_MAGIC];
2571 /* total params to print is NR_PARAMS + common block size for v1 */
2572 total_params = val[i + CS_ETM_NR_TRC_PARAMS] + CS_ETM_COMMON_BLK_MAX_V1;
2574 if (magic == __perf_cs_etmv3_magic) {
2575 for (j = 0; j < total_params; j++, i++) {
2576 /* if newer record - could be excess params */
2577 if (j >= CS_ETM_PRIV_MAX)
2578 fprintf(stdout, param_unk_fmt, j, val[i]);
2580 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2582 } else if (magic == __perf_cs_etmv4_magic) {
2583 for (j = 0; j < total_params; j++, i++) {
2584 /* if newer record - could be excess params */
2585 if (j >= CS_ETMV4_PRIV_MAX)
2586 fprintf(stdout, param_unk_fmt, j, val[i]);
2588 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2591 /* failure - note bad magic value and error out */
2592 fprintf(stdout, magic_unk_fmt, magic);
2599 static void cs_etm__print_auxtrace_info(__u64 *val, int num)
2601 int i, cpu = 0, version, err;
2603 /* bail out early on bad header version */
2605 if (version > CS_HEADER_CURRENT_VERSION) {
2606 /* failure.. return */
2607 fprintf(stdout, " Unknown Header Version = %x, ", version);
2608 fprintf(stdout, "Version supported <= %x\n", CS_HEADER_CURRENT_VERSION);
2612 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2613 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2615 for (i = CS_HEADER_VERSION_MAX; cpu < num; cpu++) {
2617 err = cs_etm__print_cpu_metadata_v0(val, &i);
2618 else if (version == 1)
2619 err = cs_etm__print_cpu_metadata_v1(val, &i);
2626 * Read a single cpu parameter block from the auxtrace_info priv block.
2628 * For version 1 there is a per cpu nr_params entry. If we are handling
2629 * version 1 file, then there may be less, the same, or more params
2630 * indicated by this value than the compile time number we understand.
2632 * For a version 0 info block, there are a fixed number, and we need to
2633 * fill out the nr_param value in the metadata we create.
2635 static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
2636 int out_blk_size, int nr_params_v0)
2638 u64 *metadata = NULL;
2640 int nr_in_params, nr_out_params, nr_cmn_params;
2643 metadata = zalloc(sizeof(*metadata) * out_blk_size);
2647 /* read block current index & version */
2648 i = *buff_in_offset;
2649 hdr_version = buff_in[CS_HEADER_VERSION];
2652 /* read version 0 info block into a version 1 metadata block */
2653 nr_in_params = nr_params_v0;
2654 metadata[CS_ETM_MAGIC] = buff_in[i + CS_ETM_MAGIC];
2655 metadata[CS_ETM_CPU] = buff_in[i + CS_ETM_CPU];
2656 metadata[CS_ETM_NR_TRC_PARAMS] = nr_in_params;
2657 /* remaining block params at offset +1 from source */
2658 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2659 metadata[k + 1] = buff_in[i + k];
2660 /* version 0 has 2 common params */
2663 /* read version 1 info block - input and output nr_params may differ */
2664 /* version 1 has 3 common params */
2666 nr_in_params = buff_in[i + CS_ETM_NR_TRC_PARAMS];
2668 /* if input has more params than output - skip excess */
2669 nr_out_params = nr_in_params + nr_cmn_params;
2670 if (nr_out_params > out_blk_size)
2671 nr_out_params = out_blk_size;
2673 for (k = CS_ETM_MAGIC; k < nr_out_params; k++)
2674 metadata[k] = buff_in[i + k];
2676 /* record the actual nr params we copied */
2677 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2680 /* adjust in offset by number of in params used */
2681 i += nr_in_params + nr_cmn_params;
2682 *buff_in_offset = i;
2687 * Puts a fragment of an auxtrace buffer into the auxtrace queues based
2688 * on the bounds of aux_event, if it matches with the buffer that's at
2691 * Normally, whole auxtrace buffers would be added to the queue. But we
2692 * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
2693 * is reset across each buffer, so splitting the buffers up in advance has
2696 static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_offset, size_t sz,
2697 struct perf_record_aux *aux_event, struct perf_sample *sample)
2700 char buf[PERF_SAMPLE_MAX_SIZE];
2701 union perf_event *auxtrace_event_union;
2702 struct perf_record_auxtrace *auxtrace_event;
2703 union perf_event auxtrace_fragment;
2704 __u64 aux_offset, aux_size;
2706 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2707 struct cs_etm_auxtrace,
2711 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
2712 * from looping through the auxtrace index.
2714 err = perf_session__peek_event(session, file_offset, buf,
2715 PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
2718 auxtrace_event = &auxtrace_event_union->auxtrace;
2719 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
2722 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
2723 auxtrace_event->header.size != sz) {
2728 * In per-thread mode, CPU is set to -1, but TID will be set instead. See
2729 * auxtrace_mmap_params__set_idx(). Return 'not found' if neither CPU nor TID match.
2731 if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) ||
2732 auxtrace_event->cpu != sample->cpu)
2735 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
2737 * Clamp size in snapshot mode. The buffer size is clamped in
2738 * __auxtrace_mmap__read() for snapshots, so the aux record size doesn't reflect
2741 aux_size = min(aux_event->aux_size, auxtrace_event->size);
2744 * In this mode, the head also points to the end of the buffer so aux_offset
2745 * needs to have the size subtracted so it points to the beginning as in normal mode
2747 aux_offset = aux_event->aux_offset - aux_size;
2749 aux_size = aux_event->aux_size;
2750 aux_offset = aux_event->aux_offset;
2753 if (aux_offset >= auxtrace_event->offset &&
2754 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
2756 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
2757 * based on the sizes of the aux event, and queue that fragment.
2759 auxtrace_fragment.auxtrace = *auxtrace_event;
2760 auxtrace_fragment.auxtrace.size = aux_size;
2761 auxtrace_fragment.auxtrace.offset = aux_offset;
2762 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
2764 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
2765 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
2766 return auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
2770 /* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
2774 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
2775 u64 offset __maybe_unused, void *data __maybe_unused)
2777 struct perf_sample sample;
2779 struct auxtrace_index_entry *ent;
2780 struct auxtrace_index *auxtrace_index;
2781 struct evsel *evsel;
2784 /* Don't care about any other events, we're only queuing buffers for AUX events */
2785 if (event->header.type != PERF_RECORD_AUX)
2788 if (event->header.size < sizeof(struct perf_record_aux))
2791 /* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
2792 if (!event->aux.aux_size)
2796 * Parse the sample, we need the sample_id_all data that comes after the event so that the
2797 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
2799 evsel = evlist__event2evsel(session->evlist, event);
2802 ret = evsel__parse_sample(evsel, event, &sample);
2807 * Loop through the auxtrace index to find the buffer that matches up with this aux event.
2809 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
2810 for (i = 0; i < auxtrace_index->nr; i++) {
2811 ent = &auxtrace_index->entries[i];
2812 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
2813 ent->sz, &event->aux, &sample);
2815 * Stop search on error or successful values. Continue search on
2824 * Couldn't find the buffer corresponding to this aux record, something went wrong. Warn but
2825 * don't exit with an error because it will still be possible to decode other aux records.
2827 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
2828 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
2832 static int cs_etm__queue_aux_records(struct perf_session *session)
2834 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
2835 struct auxtrace_index, list);
2836 if (index && index->nr > 0)
2837 return perf_session__peek_events(session, session->header.data_offset,
2838 session->header.data_size,
2839 cs_etm__queue_aux_records_cb, NULL);
2842 * We would get here if there are no entries in the index (either no auxtrace
2843 * buffers or no index at all). Fail silently as there is the possibility of
2844 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
2847 * In that scenario, buffers will not be split by AUX records.
2852 int cs_etm__process_auxtrace_info(union perf_event *event,
2853 struct perf_session *session)
2855 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
2856 struct cs_etm_auxtrace *etm = NULL;
2857 struct int_node *inode;
2858 unsigned int pmu_type;
2859 int event_header_size = sizeof(struct perf_event_header);
2860 int info_header_size;
2861 int total_size = auxtrace_info->header.size;
2863 int num_cpu, trcidr_idx;
2866 u64 *ptr, *hdr = NULL;
2867 u64 **metadata = NULL;
2871 * sizeof(auxtrace_info_event::type) +
2872 * sizeof(auxtrace_info_event::reserved) == 8
2874 info_header_size = 8;
2876 if (total_size < (event_header_size + info_header_size))
2879 priv_size = total_size - event_header_size - info_header_size;
2881 /* First the global part */
2882 ptr = (u64 *) auxtrace_info->priv;
2884 /* Look for version of the header */
2885 hdr_version = ptr[0];
2886 if (hdr_version > CS_HEADER_CURRENT_VERSION) {
2887 /* print routine will print an error on bad version */
2889 cs_etm__print_auxtrace_info(auxtrace_info->priv, 0);
2893 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_MAX);
2897 /* Extract header information - see cs-etm.h for format */
2898 for (i = 0; i < CS_HEADER_VERSION_MAX; i++)
2900 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2901 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2905 * Create an RB tree for traceID-metadata tuple. Since the conversion
2906 * has to be made for each packet that gets decoded, optimizing access
2907 * in anything other than a sequential array is worth doing.
2909 traceid_list = intlist__new(NULL);
2910 if (!traceid_list) {
2915 metadata = zalloc(sizeof(*metadata) * num_cpu);
2918 goto err_free_traceid_list;
2922 * The metadata is stored in the auxtrace_info section and encodes
2923 * the configuration of the ARM embedded trace macrocell which is
2924 * required by the trace decoder to properly decode the trace due
2925 * to its highly compressed nature.
2927 for (j = 0; j < num_cpu; j++) {
2928 if (ptr[i] == __perf_cs_etmv3_magic) {
2930 cs_etm__create_meta_blk(ptr, &i,
2932 CS_ETM_NR_TRC_PARAMS_V0);
2934 /* The traceID is our handle */
2935 trcidr_idx = CS_ETM_ETMTRACEIDR;
2937 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2939 cs_etm__create_meta_blk(ptr, &i,
2941 CS_ETMV4_NR_TRC_PARAMS_V0);
2943 /* The traceID is our handle */
2944 trcidr_idx = CS_ETMV4_TRCTRACEIDR;
2949 goto err_free_metadata;
2952 /* Get an RB node for this CPU */
2953 inode = intlist__findnew(traceid_list, metadata[j][trcidr_idx]);
2955 /* Something went wrong, no need to continue */
2958 goto err_free_metadata;
2962 * The node for that CPU should not be taken.
2963 * Back out if that's the case.
2967 goto err_free_metadata;
2969 /* All good, associate the traceID with the metadata pointer */
2970 inode->priv = metadata[j];
2974 * Each of CS_HEADER_VERSION_MAX, CS_ETM_PRIV_MAX and
2975 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2976 * global metadata, and each cpu's metadata respectively.
2977 * The following tests if the correct number of double words was
2978 * present in the auxtrace info section.
2980 if (i * 8 != priv_size) {
2982 goto err_free_metadata;
2985 etm = zalloc(sizeof(*etm));
2989 goto err_free_metadata;
2992 err = auxtrace_queues__init(&etm->queues);
2996 if (session->itrace_synth_opts->set) {
2997 etm->synth_opts = *session->itrace_synth_opts;
2999 itrace_synth_opts__set_default(&etm->synth_opts,
3000 session->itrace_synth_opts->default_no_sample);
3001 etm->synth_opts.callchain = false;
3004 etm->session = session;
3005 etm->machine = &session->machines.host;
3007 etm->num_cpu = num_cpu;
3008 etm->pmu_type = pmu_type;
3009 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
3010 etm->metadata = metadata;
3011 etm->auxtrace_type = auxtrace_info->type;
3012 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
3014 etm->auxtrace.process_event = cs_etm__process_event;
3015 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3016 etm->auxtrace.flush_events = cs_etm__flush_events;
3017 etm->auxtrace.free_events = cs_etm__free_events;
3018 etm->auxtrace.free = cs_etm__free;
3019 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3020 session->auxtrace = &etm->auxtrace;
3022 etm->unknown_thread = thread__new(999999999, 999999999);
3023 if (!etm->unknown_thread) {
3025 goto err_free_queues;
3029 * Initialize list node so that at thread__zput() we can avoid
3030 * segmentation fault at list_del_init().
3032 INIT_LIST_HEAD(&etm->unknown_thread->node);
3034 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
3036 goto err_delete_thread;
3038 if (thread__init_maps(etm->unknown_thread, etm->machine)) {
3040 goto err_delete_thread;
3044 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
3048 err = cs_etm__synth_events(etm, session);
3050 goto err_delete_thread;
3052 err = cs_etm__queue_aux_records(session);
3054 goto err_delete_thread;
3056 etm->data_queued = etm->queues.populated;
3061 thread__zput(etm->unknown_thread);
3063 auxtrace_queues__free(&etm->queues);
3064 session->auxtrace = NULL;
3068 /* No need to check @metadata[j], free(NULL) is supported */
3069 for (j = 0; j < num_cpu; j++)
3070 zfree(&metadata[j]);
3072 err_free_traceid_list:
3073 intlist__delete(traceid_list);
3077 * At this point, as a minimum we have valid header. Dump the rest of
3078 * the info section - the print routines will error out on structural
3082 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);