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/err.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/types.h>
14 #include <linux/zalloc.h>
16 #include <opencsd/ocsd_if_types.h>
22 #include "cs-etm-decoder/cs-etm-decoder.h"
33 #include "thread-stack.h"
34 #include <tools/libc_compat.h>
37 #define MAX_TIMESTAMP (~0ULL)
39 struct cs_etm_auxtrace {
40 struct auxtrace auxtrace;
41 struct auxtrace_queues queues;
42 struct auxtrace_heap heap;
43 struct itrace_synth_opts synth_opts;
44 struct perf_session *session;
45 struct machine *machine;
46 struct thread *unknown_thread;
52 u8 sample_instructions;
56 u64 branches_sample_type;
58 u64 instructions_sample_type;
59 u64 instructions_sample_period;
63 unsigned int pmu_type;
66 struct cs_etm_traceid_queue {
69 u64 period_instructions;
70 size_t last_branch_pos;
71 union perf_event *event_buf;
72 struct thread *thread;
73 struct branch_stack *last_branch;
74 struct branch_stack *last_branch_rb;
75 struct cs_etm_packet *prev_packet;
76 struct cs_etm_packet *packet;
77 struct cs_etm_packet_queue packet_queue;
81 struct cs_etm_auxtrace *etm;
82 struct cs_etm_decoder *decoder;
83 struct auxtrace_buffer *buffer;
84 unsigned int queue_nr;
87 const unsigned char *buf;
88 size_t buf_len, buf_used;
89 /* Conversion between traceID and index in traceid_queues array */
90 struct intlist *traceid_queues_list;
91 struct cs_etm_traceid_queue **traceid_queues;
94 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
95 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm);
96 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
98 static int cs_etm__get_data_block(struct cs_etm_queue *etmq);
99 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq);
101 /* PTMs ETMIDR [11:8] set to b0011 */
102 #define ETMIDR_PTM_VERSION 0x00000300
105 * A struct auxtrace_heap_item only has a queue_nr and a timestamp to
106 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
107 * encode the etm queue number as the upper 16 bit and the channel as
110 #define TO_CS_QUEUE_NR(queue_nr, trace_id_chan) \
111 (queue_nr << 16 | trace_chan_id)
112 #define TO_QUEUE_NR(cs_queue_nr) (cs_queue_nr >> 16)
113 #define TO_TRACE_CHAN_ID(cs_queue_nr) (cs_queue_nr & 0x0000ffff)
115 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
117 etmidr &= ETMIDR_PTM_VERSION;
119 if (etmidr == ETMIDR_PTM_VERSION)
120 return CS_ETM_PROTO_PTM;
122 return CS_ETM_PROTO_ETMV3;
125 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
127 struct int_node *inode;
130 inode = intlist__find(traceid_list, trace_chan_id);
134 metadata = inode->priv;
135 *magic = metadata[CS_ETM_MAGIC];
139 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
141 struct int_node *inode;
144 inode = intlist__find(traceid_list, trace_chan_id);
148 metadata = inode->priv;
149 *cpu = (int)metadata[CS_ETM_CPU];
153 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
157 * Wnen a timestamp packet is encountered the backend code
158 * is stopped so that the front end has time to process packets
159 * that were accumulated in the traceID queue. Since there can
160 * be more than one channel per cs_etm_queue, we need to specify
161 * what traceID queue needs servicing.
163 etmq->pending_timestamp = trace_chan_id;
166 static u64 cs_etm__etmq_get_timestamp(struct cs_etm_queue *etmq,
169 struct cs_etm_packet_queue *packet_queue;
171 if (!etmq->pending_timestamp)
175 *trace_chan_id = etmq->pending_timestamp;
177 packet_queue = cs_etm__etmq_get_packet_queue(etmq,
178 etmq->pending_timestamp);
182 /* Acknowledge pending status */
183 etmq->pending_timestamp = 0;
185 /* See function cs_etm_decoder__do_{hard|soft}_timestamp() */
186 return packet_queue->timestamp;
189 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
195 queue->packet_count = 0;
196 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
197 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
198 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
199 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
200 queue->packet_buffer[i].instr_count = 0;
201 queue->packet_buffer[i].last_instr_taken_branch = false;
202 queue->packet_buffer[i].last_instr_size = 0;
203 queue->packet_buffer[i].last_instr_type = 0;
204 queue->packet_buffer[i].last_instr_subtype = 0;
205 queue->packet_buffer[i].last_instr_cond = 0;
206 queue->packet_buffer[i].flags = 0;
207 queue->packet_buffer[i].exception_number = UINT32_MAX;
208 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
209 queue->packet_buffer[i].cpu = INT_MIN;
213 static void cs_etm__clear_all_packet_queues(struct cs_etm_queue *etmq)
216 struct int_node *inode;
217 struct cs_etm_traceid_queue *tidq;
218 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
220 intlist__for_each_entry(inode, traceid_queues_list) {
221 idx = (int)(intptr_t)inode->priv;
222 tidq = etmq->traceid_queues[idx];
223 cs_etm__clear_packet_queue(&tidq->packet_queue);
227 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
228 struct cs_etm_traceid_queue *tidq,
232 struct auxtrace_queue *queue;
233 struct cs_etm_auxtrace *etm = etmq->etm;
235 cs_etm__clear_packet_queue(&tidq->packet_queue);
237 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
238 tidq->tid = queue->tid;
240 tidq->trace_chan_id = trace_chan_id;
242 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
246 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
247 if (!tidq->prev_packet)
250 if (etm->synth_opts.last_branch) {
251 size_t sz = sizeof(struct branch_stack);
253 sz += etm->synth_opts.last_branch_sz *
254 sizeof(struct branch_entry);
255 tidq->last_branch = zalloc(sz);
256 if (!tidq->last_branch)
258 tidq->last_branch_rb = zalloc(sz);
259 if (!tidq->last_branch_rb)
263 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
264 if (!tidq->event_buf)
270 zfree(&tidq->last_branch_rb);
271 zfree(&tidq->last_branch);
272 zfree(&tidq->prev_packet);
273 zfree(&tidq->packet);
278 static struct cs_etm_traceid_queue
279 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
282 struct int_node *inode;
283 struct intlist *traceid_queues_list;
284 struct cs_etm_traceid_queue *tidq, **traceid_queues;
285 struct cs_etm_auxtrace *etm = etmq->etm;
287 if (etm->timeless_decoding)
288 trace_chan_id = CS_ETM_PER_THREAD_TRACEID;
290 traceid_queues_list = etmq->traceid_queues_list;
293 * Check if the traceid_queue exist for this traceID by looking
296 inode = intlist__find(traceid_queues_list, trace_chan_id);
298 idx = (int)(intptr_t)inode->priv;
299 return etmq->traceid_queues[idx];
302 /* We couldn't find a traceid_queue for this traceID, allocate one */
303 tidq = malloc(sizeof(*tidq));
307 memset(tidq, 0, sizeof(*tidq));
309 /* Get a valid index for the new traceid_queue */
310 idx = intlist__nr_entries(traceid_queues_list);
311 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
312 inode = intlist__findnew(traceid_queues_list, trace_chan_id);
316 /* Associate this traceID with this index */
317 inode->priv = (void *)(intptr_t)idx;
319 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
322 /* Grow the traceid_queues array by one unit */
323 traceid_queues = etmq->traceid_queues;
324 traceid_queues = reallocarray(traceid_queues,
326 sizeof(*traceid_queues));
329 * On failure reallocarray() returns NULL and the original block of
330 * memory is left untouched.
335 traceid_queues[idx] = tidq;
336 etmq->traceid_queues = traceid_queues;
338 return etmq->traceid_queues[idx];
342 * Function intlist__remove() removes the inode from the list
343 * and delete the memory associated to it.
345 intlist__remove(traceid_queues_list, inode);
351 struct cs_etm_packet_queue
352 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
354 struct cs_etm_traceid_queue *tidq;
356 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
358 return &tidq->packet_queue;
363 static void cs_etm__packet_dump(const char *pkt_string)
365 const char *color = PERF_COLOR_BLUE;
366 int len = strlen(pkt_string);
368 if (len && (pkt_string[len-1] == '\n'))
369 color_fprintf(stdout, color, " %s", pkt_string);
371 color_fprintf(stdout, color, " %s\n", pkt_string);
376 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
377 struct cs_etm_auxtrace *etm, int idx,
380 u64 **metadata = etm->metadata;
382 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
383 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
384 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
387 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
388 struct cs_etm_auxtrace *etm, int idx)
390 u64 **metadata = etm->metadata;
392 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
393 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
394 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
395 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
396 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
397 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
398 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
401 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
402 struct cs_etm_auxtrace *etm)
408 for (i = 0; i < etm->num_cpu; i++) {
409 architecture = etm->metadata[i][CS_ETM_MAGIC];
411 switch (architecture) {
412 case __perf_cs_etmv3_magic:
413 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
414 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
416 case __perf_cs_etmv4_magic:
417 cs_etm__set_trace_param_etmv4(t_params, etm, i);
427 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
428 struct cs_etm_queue *etmq,
429 enum cs_etm_decoder_operation mode)
433 if (!(mode < CS_ETM_OPERATION_MAX))
436 d_params->packet_printer = cs_etm__packet_dump;
437 d_params->operation = mode;
438 d_params->data = etmq;
439 d_params->formatted = true;
440 d_params->fsyncs = false;
441 d_params->hsyncs = false;
442 d_params->frame_aligned = true;
449 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
450 struct auxtrace_buffer *buffer)
453 const char *color = PERF_COLOR_BLUE;
454 struct cs_etm_decoder_params d_params;
455 struct cs_etm_trace_params *t_params;
456 struct cs_etm_decoder *decoder;
457 size_t buffer_used = 0;
459 fprintf(stdout, "\n");
460 color_fprintf(stdout, color,
461 ". ... CoreSight ETM Trace data: size %zu bytes\n",
464 /* Use metadata to fill in trace parameters for trace decoder */
465 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
470 if (cs_etm__init_trace_params(t_params, etm))
473 /* Set decoder parameters to simply print the trace packets */
474 if (cs_etm__init_decoder_params(&d_params, NULL,
475 CS_ETM_OPERATION_PRINT))
478 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
485 ret = cs_etm_decoder__process_data_block(
486 decoder, buffer->offset,
487 &((u8 *)buffer->data)[buffer_used],
488 buffer->size - buffer_used, &consumed);
492 buffer_used += consumed;
493 } while (buffer_used < buffer->size);
495 cs_etm_decoder__free(decoder);
501 static int cs_etm__flush_events(struct perf_session *session,
502 struct perf_tool *tool)
505 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
506 struct cs_etm_auxtrace,
511 if (!tool->ordered_events)
514 ret = cs_etm__update_queues(etm);
519 if (etm->timeless_decoding)
520 return cs_etm__process_timeless_queues(etm, -1);
522 return cs_etm__process_queues(etm);
525 static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
529 struct int_node *inode, *tmp;
530 struct cs_etm_traceid_queue *tidq;
531 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
533 intlist__for_each_entry_safe(inode, tmp, traceid_queues_list) {
534 priv = (uintptr_t)inode->priv;
537 /* Free this traceid_queue from the array */
538 tidq = etmq->traceid_queues[idx];
539 thread__zput(tidq->thread);
540 zfree(&tidq->event_buf);
541 zfree(&tidq->last_branch);
542 zfree(&tidq->last_branch_rb);
543 zfree(&tidq->prev_packet);
544 zfree(&tidq->packet);
548 * Function intlist__remove() removes the inode from the list
549 * and delete the memory associated to it.
551 intlist__remove(traceid_queues_list, inode);
554 /* Then the RB tree itself */
555 intlist__delete(traceid_queues_list);
556 etmq->traceid_queues_list = NULL;
558 /* finally free the traceid_queues array */
559 zfree(&etmq->traceid_queues);
562 static void cs_etm__free_queue(void *priv)
564 struct cs_etm_queue *etmq = priv;
569 cs_etm_decoder__free(etmq->decoder);
570 cs_etm__free_traceid_queues(etmq);
574 static void cs_etm__free_events(struct perf_session *session)
577 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
578 struct cs_etm_auxtrace,
580 struct auxtrace_queues *queues = &aux->queues;
582 for (i = 0; i < queues->nr_queues; i++) {
583 cs_etm__free_queue(queues->queue_array[i].priv);
584 queues->queue_array[i].priv = NULL;
587 auxtrace_queues__free(queues);
590 static void cs_etm__free(struct perf_session *session)
593 struct int_node *inode, *tmp;
594 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
595 struct cs_etm_auxtrace,
597 cs_etm__free_events(session);
598 session->auxtrace = NULL;
600 /* First remove all traceID/metadata nodes for the RB tree */
601 intlist__for_each_entry_safe(inode, tmp, traceid_list)
602 intlist__remove(traceid_list, inode);
603 /* Then the RB tree itself */
604 intlist__delete(traceid_list);
606 for (i = 0; i < aux->num_cpu; i++)
607 zfree(&aux->metadata[i]);
609 thread__zput(aux->unknown_thread);
610 zfree(&aux->metadata);
614 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
616 struct machine *machine;
618 machine = etmq->etm->machine;
620 if (address >= etmq->etm->kernel_start) {
621 if (machine__is_host(machine))
622 return PERF_RECORD_MISC_KERNEL;
624 return PERF_RECORD_MISC_GUEST_KERNEL;
626 if (machine__is_host(machine))
627 return PERF_RECORD_MISC_USER;
629 return PERF_RECORD_MISC_GUEST_USER;
631 return PERF_RECORD_MISC_HYPERVISOR;
635 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
636 u64 address, size_t size, u8 *buffer)
641 struct thread *thread;
642 struct machine *machine;
643 struct addr_location al;
644 struct cs_etm_traceid_queue *tidq;
649 machine = etmq->etm->machine;
650 cpumode = cs_etm__cpu_mode(etmq, address);
651 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
655 thread = tidq->thread;
657 if (cpumode != PERF_RECORD_MISC_KERNEL)
659 thread = etmq->etm->unknown_thread;
662 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
665 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
666 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
669 offset = al.map->map_ip(al.map, address);
673 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
681 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
683 struct cs_etm_decoder_params d_params;
684 struct cs_etm_trace_params *t_params = NULL;
685 struct cs_etm_queue *etmq;
687 etmq = zalloc(sizeof(*etmq));
691 etmq->traceid_queues_list = intlist__new(NULL);
692 if (!etmq->traceid_queues_list)
695 /* Use metadata to fill in trace parameters for trace decoder */
696 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
701 if (cs_etm__init_trace_params(t_params, etm))
704 /* Set decoder parameters to decode trace packets */
705 if (cs_etm__init_decoder_params(&d_params, etmq,
706 CS_ETM_OPERATION_DECODE))
709 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
715 * Register a function to handle all memory accesses required by
716 * the trace decoder library.
718 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
721 goto out_free_decoder;
727 cs_etm_decoder__free(etmq->decoder);
729 intlist__delete(etmq->traceid_queues_list);
735 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
736 struct auxtrace_queue *queue,
737 unsigned int queue_nr)
740 unsigned int cs_queue_nr;
743 struct cs_etm_queue *etmq = queue->priv;
745 if (list_empty(&queue->head) || etmq)
748 etmq = cs_etm__alloc_queue(etm);
757 etmq->queue_nr = queue_nr;
760 if (etm->timeless_decoding)
764 * We are under a CPU-wide trace scenario. As such we need to know
765 * when the code that generated the traces started to execute so that
766 * it can be correlated with execution on other CPUs. So we get a
767 * handle on the beginning of traces and decode until we find a
768 * timestamp. The timestamp is then added to the auxtrace min heap
769 * in order to know what nibble (of all the etmqs) to decode first.
773 * Fetch an aux_buffer from this etmq. Bail if no more
774 * blocks or an error has been encountered.
776 ret = cs_etm__get_data_block(etmq);
781 * Run decoder on the trace block. The decoder will stop when
782 * encountering a timestamp, a full packet queue or the end of
783 * trace for that block.
785 ret = cs_etm__decode_data_block(etmq);
790 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
791 * the timestamp calculation for us.
793 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
795 /* We found a timestamp, no need to continue. */
800 * We didn't find a timestamp so empty all the traceid packet
801 * queues before looking for another timestamp packet, either
802 * in the current data block or a new one. Packets that were
803 * just decoded are useless since no timestamp has been
804 * associated with them. As such simply discard them.
806 cs_etm__clear_all_packet_queues(etmq);
810 * We have a timestamp. Add it to the min heap to reflect when
811 * instructions conveyed by the range packets of this traceID queue
812 * started to execute. Once the same has been done for all the traceID
813 * queues of each etmq, redenring and decoding can start in
814 * chronological order.
816 * Note that packets decoded above are still in the traceID's packet
817 * queue and will be processed in cs_etm__process_queues().
819 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_id_chan);
820 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
825 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
830 if (!etm->kernel_start)
831 etm->kernel_start = machine__kernel_start(etm->machine);
833 for (i = 0; i < etm->queues.nr_queues; i++) {
834 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
842 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
844 if (etm->queues.new_data) {
845 etm->queues.new_data = false;
846 return cs_etm__setup_queues(etm);
853 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
854 struct cs_etm_traceid_queue *tidq)
856 struct branch_stack *bs_src = tidq->last_branch_rb;
857 struct branch_stack *bs_dst = tidq->last_branch;
861 * Set the number of records before early exit: ->nr is used to
862 * determine how many branches to copy from ->entries.
864 bs_dst->nr = bs_src->nr;
867 * Early exit when there is nothing to copy.
873 * As bs_src->entries is a circular buffer, we need to copy from it in
874 * two steps. First, copy the branches from the most recently inserted
875 * branch ->last_branch_pos until the end of bs_src->entries buffer.
877 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
878 memcpy(&bs_dst->entries[0],
879 &bs_src->entries[tidq->last_branch_pos],
880 sizeof(struct branch_entry) * nr);
883 * If we wrapped around at least once, the branches from the beginning
884 * of the bs_src->entries buffer and until the ->last_branch_pos element
885 * are older valid branches: copy them over. The total number of
886 * branches copied over will be equal to the number of branches asked by
887 * the user in last_branch_sz.
889 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
890 memcpy(&bs_dst->entries[nr],
892 sizeof(struct branch_entry) * tidq->last_branch_pos);
897 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
899 tidq->last_branch_pos = 0;
900 tidq->last_branch_rb->nr = 0;
903 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
904 u8 trace_chan_id, u64 addr)
908 cs_etm__mem_access(etmq, trace_chan_id, addr,
909 ARRAY_SIZE(instrBytes), instrBytes);
911 * T32 instruction size is indicated by bits[15:11] of the first
912 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
913 * denote a 32-bit instruction.
915 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
918 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
920 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
921 if (packet->sample_type == CS_ETM_DISCONTINUITY)
924 return packet->start_addr;
928 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
930 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
931 if (packet->sample_type == CS_ETM_DISCONTINUITY)
934 return packet->end_addr - packet->last_instr_size;
937 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
939 const struct cs_etm_packet *packet,
942 if (packet->isa == CS_ETM_ISA_T32) {
943 u64 addr = packet->start_addr;
946 addr += cs_etm__t32_instr_size(etmq,
947 trace_chan_id, addr);
953 /* Assume a 4 byte instruction size (A32/A64) */
954 return packet->start_addr + offset * 4;
957 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
958 struct cs_etm_traceid_queue *tidq)
960 struct branch_stack *bs = tidq->last_branch_rb;
961 struct branch_entry *be;
964 * The branches are recorded in a circular buffer in reverse
965 * chronological order: we start recording from the last element of the
966 * buffer down. After writing the first element of the stack, move the
967 * insert position back to the end of the buffer.
969 if (!tidq->last_branch_pos)
970 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
972 tidq->last_branch_pos -= 1;
974 be = &bs->entries[tidq->last_branch_pos];
975 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
976 be->to = cs_etm__first_executed_instr(tidq->packet);
977 /* No support for mispredict */
978 be->flags.mispred = 0;
979 be->flags.predicted = 1;
982 * Increment bs->nr until reaching the number of last branches asked by
983 * the user on the command line.
985 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
989 static int cs_etm__inject_event(union perf_event *event,
990 struct perf_sample *sample, u64 type)
992 event->header.size = perf_event__sample_event_size(sample, type, 0);
993 return perf_event__synthesize_sample(event, type, 0, sample);
998 cs_etm__get_trace(struct cs_etm_queue *etmq)
1000 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1001 struct auxtrace_buffer *old_buffer = aux_buffer;
1002 struct auxtrace_queue *queue;
1004 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1006 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
1008 /* If no more data, drop the previous auxtrace_buffer and return */
1011 auxtrace_buffer__drop_data(old_buffer);
1016 etmq->buffer = aux_buffer;
1018 /* If the aux_buffer doesn't have data associated, try to load it */
1019 if (!aux_buffer->data) {
1020 /* get the file desc associated with the perf data file */
1021 int fd = perf_data__fd(etmq->etm->session->data);
1023 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1024 if (!aux_buffer->data)
1028 /* If valid, drop the previous buffer */
1030 auxtrace_buffer__drop_data(old_buffer);
1033 etmq->buf_len = aux_buffer->size;
1034 etmq->buf = aux_buffer->data;
1036 return etmq->buf_len;
1039 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
1040 struct cs_etm_traceid_queue *tidq)
1042 if ((!tidq->thread) && (tidq->tid != -1))
1043 tidq->thread = machine__find_thread(etm->machine, -1,
1047 tidq->pid = tidq->thread->pid_;
1050 int cs_etm__etmq_set_tid(struct cs_etm_queue *etmq,
1051 pid_t tid, u8 trace_chan_id)
1053 int cpu, err = -EINVAL;
1054 struct cs_etm_auxtrace *etm = etmq->etm;
1055 struct cs_etm_traceid_queue *tidq;
1057 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
1061 if (cs_etm__get_cpu(trace_chan_id, &cpu) < 0)
1064 err = machine__set_current_tid(etm->machine, cpu, tid, tid);
1069 thread__zput(tidq->thread);
1071 cs_etm__set_pid_tid_cpu(etm, tidq);
1075 bool cs_etm__etmq_is_timeless(struct cs_etm_queue *etmq)
1077 return !!etmq->etm->timeless_decoding;
1080 static void cs_etm__copy_insn(struct cs_etm_queue *etmq,
1082 const struct cs_etm_packet *packet,
1083 struct perf_sample *sample)
1086 * It's pointless to read instructions for the CS_ETM_DISCONTINUITY
1087 * packet, so directly bail out with 'insn_len' = 0.
1089 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1090 sample->insn_len = 0;
1095 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1096 * cs_etm__t32_instr_size().
1098 if (packet->isa == CS_ETM_ISA_T32)
1099 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1101 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1103 sample->insn_len = 4;
1105 cs_etm__mem_access(etmq, trace_chan_id, sample->ip,
1106 sample->insn_len, (void *)sample->insn);
1109 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
1110 struct cs_etm_traceid_queue *tidq,
1111 u64 addr, u64 period)
1114 struct cs_etm_auxtrace *etm = etmq->etm;
1115 union perf_event *event = tidq->event_buf;
1116 struct perf_sample sample = {.ip = 0,};
1118 event->sample.header.type = PERF_RECORD_SAMPLE;
1119 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
1120 event->sample.header.size = sizeof(struct perf_event_header);
1123 sample.pid = tidq->pid;
1124 sample.tid = tidq->tid;
1125 sample.id = etmq->etm->instructions_id;
1126 sample.stream_id = etmq->etm->instructions_id;
1127 sample.period = period;
1128 sample.cpu = tidq->packet->cpu;
1129 sample.flags = tidq->prev_packet->flags;
1130 sample.cpumode = event->sample.header.misc;
1132 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1134 if (etm->synth_opts.last_branch) {
1135 cs_etm__copy_last_branch_rb(etmq, tidq);
1136 sample.branch_stack = tidq->last_branch;
1139 if (etm->synth_opts.inject) {
1140 ret = cs_etm__inject_event(event, &sample,
1141 etm->instructions_sample_type);
1146 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1150 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1153 if (etm->synth_opts.last_branch)
1154 cs_etm__reset_last_branch_rb(tidq);
1160 * The cs etm packet encodes an instruction range between a branch target
1161 * and the next taken branch. Generate sample accordingly.
1163 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
1164 struct cs_etm_traceid_queue *tidq)
1167 struct cs_etm_auxtrace *etm = etmq->etm;
1168 struct perf_sample sample = {.ip = 0,};
1169 union perf_event *event = tidq->event_buf;
1170 struct dummy_branch_stack {
1172 struct branch_entry entries;
1176 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1178 event->sample.header.type = PERF_RECORD_SAMPLE;
1179 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
1180 event->sample.header.size = sizeof(struct perf_event_header);
1183 sample.pid = tidq->pid;
1184 sample.tid = tidq->tid;
1185 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1186 sample.id = etmq->etm->branches_id;
1187 sample.stream_id = etmq->etm->branches_id;
1189 sample.cpu = tidq->packet->cpu;
1190 sample.flags = tidq->prev_packet->flags;
1191 sample.cpumode = event->sample.header.misc;
1193 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1197 * perf report cannot handle events without a branch stack
1199 if (etm->synth_opts.last_branch) {
1200 dummy_bs = (struct dummy_branch_stack){
1207 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1210 if (etm->synth_opts.inject) {
1211 ret = cs_etm__inject_event(event, &sample,
1212 etm->branches_sample_type);
1217 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1221 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1227 struct cs_etm_synth {
1228 struct perf_tool dummy_tool;
1229 struct perf_session *session;
1232 static int cs_etm__event_synth(struct perf_tool *tool,
1233 union perf_event *event,
1234 struct perf_sample *sample __maybe_unused,
1235 struct machine *machine __maybe_unused)
1237 struct cs_etm_synth *cs_etm_synth =
1238 container_of(tool, struct cs_etm_synth, dummy_tool);
1240 return perf_session__deliver_synth_event(cs_etm_synth->session,
1244 static int cs_etm__synth_event(struct perf_session *session,
1245 struct perf_event_attr *attr, u64 id)
1247 struct cs_etm_synth cs_etm_synth;
1249 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
1250 cs_etm_synth.session = session;
1252 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
1253 &id, cs_etm__event_synth);
1256 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
1257 struct perf_session *session)
1259 struct evlist *evlist = session->evlist;
1260 struct evsel *evsel;
1261 struct perf_event_attr attr;
1266 evlist__for_each_entry(evlist, evsel) {
1267 if (evsel->core.attr.type == etm->pmu_type) {
1274 pr_debug("No selected events with CoreSight Trace data\n");
1278 memset(&attr, 0, sizeof(struct perf_event_attr));
1279 attr.size = sizeof(struct perf_event_attr);
1280 attr.type = PERF_TYPE_HARDWARE;
1281 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1282 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1284 if (etm->timeless_decoding)
1285 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1287 attr.sample_type |= PERF_SAMPLE_TIME;
1289 attr.exclude_user = evsel->core.attr.exclude_user;
1290 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1291 attr.exclude_hv = evsel->core.attr.exclude_hv;
1292 attr.exclude_host = evsel->core.attr.exclude_host;
1293 attr.exclude_guest = evsel->core.attr.exclude_guest;
1294 attr.sample_id_all = evsel->core.attr.sample_id_all;
1295 attr.read_format = evsel->core.attr.read_format;
1297 /* create new id val to be a fixed offset from evsel id */
1298 id = evsel->id[0] + 1000000000;
1303 if (etm->synth_opts.branches) {
1304 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1305 attr.sample_period = 1;
1306 attr.sample_type |= PERF_SAMPLE_ADDR;
1307 err = cs_etm__synth_event(session, &attr, id);
1310 etm->sample_branches = true;
1311 etm->branches_sample_type = attr.sample_type;
1312 etm->branches_id = id;
1314 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1317 if (etm->synth_opts.last_branch)
1318 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1320 if (etm->synth_opts.instructions) {
1321 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1322 attr.sample_period = etm->synth_opts.period;
1323 etm->instructions_sample_period = attr.sample_period;
1324 err = cs_etm__synth_event(session, &attr, id);
1327 etm->sample_instructions = true;
1328 etm->instructions_sample_type = attr.sample_type;
1329 etm->instructions_id = id;
1336 static int cs_etm__sample(struct cs_etm_queue *etmq,
1337 struct cs_etm_traceid_queue *tidq)
1339 struct cs_etm_auxtrace *etm = etmq->etm;
1340 struct cs_etm_packet *tmp;
1342 u8 trace_chan_id = tidq->trace_chan_id;
1343 u64 instrs_executed = tidq->packet->instr_count;
1345 tidq->period_instructions += instrs_executed;
1348 * Record a branch when the last instruction in
1349 * PREV_PACKET is a branch.
1351 if (etm->synth_opts.last_branch &&
1352 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1353 tidq->prev_packet->last_instr_taken_branch)
1354 cs_etm__update_last_branch_rb(etmq, tidq);
1356 if (etm->sample_instructions &&
1357 tidq->period_instructions >= etm->instructions_sample_period) {
1359 * Emit instruction sample periodically
1360 * TODO: allow period to be defined in cycles and clock time
1363 /* Get number of instructions executed after the sample point */
1364 u64 instrs_over = tidq->period_instructions -
1365 etm->instructions_sample_period;
1368 * Calculate the address of the sampled instruction (-1 as
1369 * sample is reported as though instruction has just been
1370 * executed, but PC has not advanced to next instruction)
1372 u64 offset = (instrs_executed - instrs_over - 1);
1373 u64 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1374 tidq->packet, offset);
1376 ret = cs_etm__synth_instruction_sample(
1377 etmq, tidq, addr, etm->instructions_sample_period);
1381 /* Carry remaining instructions into next sample period */
1382 tidq->period_instructions = instrs_over;
1385 if (etm->sample_branches) {
1386 bool generate_sample = false;
1388 /* Generate sample for tracing on packet */
1389 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1390 generate_sample = true;
1392 /* Generate sample for branch taken packet */
1393 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1394 tidq->prev_packet->last_instr_taken_branch)
1395 generate_sample = true;
1397 if (generate_sample) {
1398 ret = cs_etm__synth_branch_sample(etmq, tidq);
1404 if (etm->sample_branches || etm->synth_opts.last_branch) {
1406 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1407 * the next incoming packet.
1410 tidq->packet = tidq->prev_packet;
1411 tidq->prev_packet = tmp;
1417 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1420 * When the exception packet is inserted, whether the last instruction
1421 * in previous range packet is taken branch or not, we need to force
1422 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1423 * to generate branch sample for the instruction range before the
1424 * exception is trapped to kernel or before the exception returning.
1426 * The exception packet includes the dummy address values, so don't
1427 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1428 * for generating instruction and branch samples.
1430 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1431 tidq->prev_packet->last_instr_taken_branch = true;
1436 static int cs_etm__flush(struct cs_etm_queue *etmq,
1437 struct cs_etm_traceid_queue *tidq)
1440 struct cs_etm_auxtrace *etm = etmq->etm;
1441 struct cs_etm_packet *tmp;
1443 /* Handle start tracing packet */
1444 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1447 if (etmq->etm->synth_opts.last_branch &&
1448 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1450 * Generate a last branch event for the branches left in the
1451 * circular buffer at the end of the trace.
1453 * Use the address of the end of the last reported execution
1456 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1458 err = cs_etm__synth_instruction_sample(
1460 tidq->period_instructions);
1464 tidq->period_instructions = 0;
1468 if (etm->sample_branches &&
1469 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1470 err = cs_etm__synth_branch_sample(etmq, tidq);
1476 if (etm->sample_branches || etm->synth_opts.last_branch) {
1478 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1479 * the next incoming packet.
1482 tidq->packet = tidq->prev_packet;
1483 tidq->prev_packet = tmp;
1489 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1490 struct cs_etm_traceid_queue *tidq)
1495 * It has no new packet coming and 'etmq->packet' contains the stale
1496 * packet which was set at the previous time with packets swapping;
1497 * so skip to generate branch sample to avoid stale packet.
1499 * For this case only flush branch stack and generate a last branch
1500 * event for the branches left in the circular buffer at the end of
1503 if (etmq->etm->synth_opts.last_branch &&
1504 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1506 * Use the address of the end of the last reported execution
1509 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1511 err = cs_etm__synth_instruction_sample(
1513 tidq->period_instructions);
1517 tidq->period_instructions = 0;
1523 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1525 * Returns: < 0 if error
1526 * = 0 if no more auxtrace_buffer to read
1527 * > 0 if the current buffer isn't empty yet
1529 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1533 if (!etmq->buf_len) {
1534 ret = cs_etm__get_trace(etmq);
1538 * We cannot assume consecutive blocks in the data file
1539 * are contiguous, reset the decoder to force re-sync.
1541 ret = cs_etm_decoder__reset(etmq->decoder);
1546 return etmq->buf_len;
1549 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1550 struct cs_etm_packet *packet,
1553 /* Initialise to keep compiler happy */
1558 switch (packet->isa) {
1559 case CS_ETM_ISA_T32:
1561 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1564 * +-----------------+--------+
1565 * | 1 1 0 1 1 1 1 1 | imm8 |
1566 * +-----------------+--------+
1568 * According to the specifiction, it only defines SVC for T32
1569 * with 16 bits instruction and has no definition for 32bits;
1570 * so below only read 2 bytes as instruction size for T32.
1572 addr = end_addr - 2;
1573 cs_etm__mem_access(etmq, trace_chan_id, addr,
1574 sizeof(instr16), (u8 *)&instr16);
1575 if ((instr16 & 0xFF00) == 0xDF00)
1579 case CS_ETM_ISA_A32:
1581 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1583 * b'31 b'28 b'27 b'24
1584 * +---------+---------+-------------------------+
1585 * | !1111 | 1 1 1 1 | imm24 |
1586 * +---------+---------+-------------------------+
1588 addr = end_addr - 4;
1589 cs_etm__mem_access(etmq, trace_chan_id, addr,
1590 sizeof(instr32), (u8 *)&instr32);
1591 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1592 (instr32 & 0xF0000000) != 0xF0000000)
1596 case CS_ETM_ISA_A64:
1598 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1601 * +-----------------------+---------+-----------+
1602 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1603 * +-----------------------+---------+-----------+
1605 addr = end_addr - 4;
1606 cs_etm__mem_access(etmq, trace_chan_id, addr,
1607 sizeof(instr32), (u8 *)&instr32);
1608 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1612 case CS_ETM_ISA_UNKNOWN:
1620 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1621 struct cs_etm_traceid_queue *tidq, u64 magic)
1623 u8 trace_chan_id = tidq->trace_chan_id;
1624 struct cs_etm_packet *packet = tidq->packet;
1625 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1627 if (magic == __perf_cs_etmv3_magic)
1628 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1632 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1633 * HVC cases; need to check if it's SVC instruction based on
1636 if (magic == __perf_cs_etmv4_magic) {
1637 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1638 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1639 prev_packet->end_addr))
1646 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1649 struct cs_etm_packet *packet = tidq->packet;
1651 if (magic == __perf_cs_etmv3_magic)
1652 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1653 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1654 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1655 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1656 packet->exception_number == CS_ETMV3_EXC_FIQ)
1659 if (magic == __perf_cs_etmv4_magic)
1660 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1661 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1662 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1663 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1664 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1665 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1666 packet->exception_number == CS_ETMV4_EXC_FIQ)
1672 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1673 struct cs_etm_traceid_queue *tidq,
1676 u8 trace_chan_id = tidq->trace_chan_id;
1677 struct cs_etm_packet *packet = tidq->packet;
1678 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1680 if (magic == __perf_cs_etmv3_magic)
1681 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1682 packet->exception_number == CS_ETMV3_EXC_HYP ||
1683 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1684 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1685 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1686 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1687 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1690 if (magic == __perf_cs_etmv4_magic) {
1691 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1692 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1693 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1694 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1698 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1699 * (SMC, HVC) are taken as sync exceptions.
1701 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1702 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1703 prev_packet->end_addr))
1707 * ETMv4 has 5 bits for exception number; if the numbers
1708 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1709 * they are implementation defined exceptions.
1711 * For this case, simply take it as sync exception.
1713 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1714 packet->exception_number <= CS_ETMV4_EXC_END)
1721 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1722 struct cs_etm_traceid_queue *tidq)
1724 struct cs_etm_packet *packet = tidq->packet;
1725 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1726 u8 trace_chan_id = tidq->trace_chan_id;
1730 switch (packet->sample_type) {
1733 * Immediate branch instruction without neither link nor
1734 * return flag, it's normal branch instruction within
1737 if (packet->last_instr_type == OCSD_INSTR_BR &&
1738 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1739 packet->flags = PERF_IP_FLAG_BRANCH;
1741 if (packet->last_instr_cond)
1742 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1746 * Immediate branch instruction with link (e.g. BL), this is
1747 * branch instruction for function call.
1749 if (packet->last_instr_type == OCSD_INSTR_BR &&
1750 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1751 packet->flags = PERF_IP_FLAG_BRANCH |
1755 * Indirect branch instruction with link (e.g. BLR), this is
1756 * branch instruction for function call.
1758 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1759 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1760 packet->flags = PERF_IP_FLAG_BRANCH |
1764 * Indirect branch instruction with subtype of
1765 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1766 * function return for A32/T32.
1768 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1769 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1770 packet->flags = PERF_IP_FLAG_BRANCH |
1771 PERF_IP_FLAG_RETURN;
1774 * Indirect branch instruction without link (e.g. BR), usually
1775 * this is used for function return, especially for functions
1776 * within dynamic link lib.
1778 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1779 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1780 packet->flags = PERF_IP_FLAG_BRANCH |
1781 PERF_IP_FLAG_RETURN;
1783 /* Return instruction for function return. */
1784 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1785 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1786 packet->flags = PERF_IP_FLAG_BRANCH |
1787 PERF_IP_FLAG_RETURN;
1790 * Decoder might insert a discontinuity in the middle of
1791 * instruction packets, fixup prev_packet with flag
1792 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1794 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1795 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1796 PERF_IP_FLAG_TRACE_BEGIN;
1799 * If the previous packet is an exception return packet
1800 * and the return address just follows SVC instuction,
1801 * it needs to calibrate the previous packet sample flags
1802 * as PERF_IP_FLAG_SYSCALLRET.
1804 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1805 PERF_IP_FLAG_RETURN |
1806 PERF_IP_FLAG_INTERRUPT) &&
1807 cs_etm__is_svc_instr(etmq, trace_chan_id,
1808 packet, packet->start_addr))
1809 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1810 PERF_IP_FLAG_RETURN |
1811 PERF_IP_FLAG_SYSCALLRET;
1813 case CS_ETM_DISCONTINUITY:
1815 * The trace is discontinuous, if the previous packet is
1816 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1817 * for previous packet.
1819 if (prev_packet->sample_type == CS_ETM_RANGE)
1820 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1821 PERF_IP_FLAG_TRACE_END;
1823 case CS_ETM_EXCEPTION:
1824 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1828 /* The exception is for system call. */
1829 if (cs_etm__is_syscall(etmq, tidq, magic))
1830 packet->flags = PERF_IP_FLAG_BRANCH |
1832 PERF_IP_FLAG_SYSCALLRET;
1834 * The exceptions are triggered by external signals from bus,
1835 * interrupt controller, debug module, PE reset or halt.
1837 else if (cs_etm__is_async_exception(tidq, magic))
1838 packet->flags = PERF_IP_FLAG_BRANCH |
1840 PERF_IP_FLAG_ASYNC |
1841 PERF_IP_FLAG_INTERRUPT;
1843 * Otherwise, exception is caused by trap, instruction &
1844 * data fault, or alignment errors.
1846 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1847 packet->flags = PERF_IP_FLAG_BRANCH |
1849 PERF_IP_FLAG_INTERRUPT;
1852 * When the exception packet is inserted, since exception
1853 * packet is not used standalone for generating samples
1854 * and it's affiliation to the previous instruction range
1855 * packet; so set previous range packet flags to tell perf
1856 * it is an exception taken branch.
1858 if (prev_packet->sample_type == CS_ETM_RANGE)
1859 prev_packet->flags = packet->flags;
1861 case CS_ETM_EXCEPTION_RET:
1863 * When the exception return packet is inserted, since
1864 * exception return packet is not used standalone for
1865 * generating samples and it's affiliation to the previous
1866 * instruction range packet; so set previous range packet
1867 * flags to tell perf it is an exception return branch.
1869 * The exception return can be for either system call or
1870 * other exception types; unfortunately the packet doesn't
1871 * contain exception type related info so we cannot decide
1872 * the exception type purely based on exception return packet.
1873 * If we record the exception number from exception packet and
1874 * reuse it for excpetion return packet, this is not reliable
1875 * due the trace can be discontinuity or the interrupt can
1876 * be nested, thus the recorded exception number cannot be
1877 * used for exception return packet for these two cases.
1879 * For exception return packet, we only need to distinguish the
1880 * packet is for system call or for other types. Thus the
1881 * decision can be deferred when receive the next packet which
1882 * contains the return address, based on the return address we
1883 * can read out the previous instruction and check if it's a
1884 * system call instruction and then calibrate the sample flag
1887 if (prev_packet->sample_type == CS_ETM_RANGE)
1888 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1889 PERF_IP_FLAG_RETURN |
1890 PERF_IP_FLAG_INTERRUPT;
1900 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
1903 size_t processed = 0;
1906 * Packets are decoded and added to the decoder's packet queue
1907 * until the decoder packet processing callback has requested that
1908 * processing stops or there is nothing left in the buffer. Normal
1909 * operations that stop processing are a timestamp packet or a full
1910 * decoder buffer queue.
1912 ret = cs_etm_decoder__process_data_block(etmq->decoder,
1914 &etmq->buf[etmq->buf_used],
1920 etmq->offset += processed;
1921 etmq->buf_used += processed;
1922 etmq->buf_len -= processed;
1928 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
1929 struct cs_etm_traceid_queue *tidq)
1932 struct cs_etm_packet_queue *packet_queue;
1934 packet_queue = &tidq->packet_queue;
1936 /* Process each packet in this chunk */
1938 ret = cs_etm_decoder__get_packet(packet_queue,
1942 * Stop processing this chunk on
1943 * end of data or error
1948 * Since packet addresses are swapped in packet
1949 * handling within below switch() statements,
1950 * thus setting sample flags must be called
1951 * prior to switch() statement to use address
1952 * information before packets swapping.
1954 ret = cs_etm__set_sample_flags(etmq, tidq);
1958 switch (tidq->packet->sample_type) {
1961 * If the packet contains an instruction
1962 * range, generate instruction sequence
1965 cs_etm__sample(etmq, tidq);
1967 case CS_ETM_EXCEPTION:
1968 case CS_ETM_EXCEPTION_RET:
1970 * If the exception packet is coming,
1971 * make sure the previous instruction
1972 * range packet to be handled properly.
1974 cs_etm__exception(tidq);
1976 case CS_ETM_DISCONTINUITY:
1978 * Discontinuity in trace, flush
1979 * previous branch stack
1981 cs_etm__flush(etmq, tidq);
1985 * Should not receive empty packet,
1988 pr_err("CS ETM Trace: empty packet\n");
1998 static void cs_etm__clear_all_traceid_queues(struct cs_etm_queue *etmq)
2001 struct int_node *inode;
2002 struct cs_etm_traceid_queue *tidq;
2003 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2005 intlist__for_each_entry(inode, traceid_queues_list) {
2006 idx = (int)(intptr_t)inode->priv;
2007 tidq = etmq->traceid_queues[idx];
2009 /* Ignore return value */
2010 cs_etm__process_traceid_queue(etmq, tidq);
2013 * Generate an instruction sample with the remaining
2014 * branchstack entries.
2016 cs_etm__flush(etmq, tidq);
2020 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
2023 struct cs_etm_traceid_queue *tidq;
2025 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
2029 /* Go through each buffer in the queue and decode them one by one */
2031 err = cs_etm__get_data_block(etmq);
2035 /* Run trace decoder until buffer consumed or end of trace */
2037 err = cs_etm__decode_data_block(etmq);
2042 * Process each packet in this chunk, nothing to do if
2043 * an error occurs other than hoping the next one will
2046 err = cs_etm__process_traceid_queue(etmq, tidq);
2048 } while (etmq->buf_len);
2051 /* Flush any remaining branch stack entries */
2052 err = cs_etm__end_block(etmq, tidq);
2058 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
2062 struct auxtrace_queues *queues = &etm->queues;
2064 for (i = 0; i < queues->nr_queues; i++) {
2065 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2066 struct cs_etm_queue *etmq = queue->priv;
2067 struct cs_etm_traceid_queue *tidq;
2072 tidq = cs_etm__etmq_get_traceid_queue(etmq,
2073 CS_ETM_PER_THREAD_TRACEID);
2078 if ((tid == -1) || (tidq->tid == tid)) {
2079 cs_etm__set_pid_tid_cpu(etm, tidq);
2080 cs_etm__run_decoder(etmq);
2087 static int cs_etm__process_queues(struct cs_etm_auxtrace *etm)
2090 unsigned int cs_queue_nr, queue_nr;
2093 struct auxtrace_queue *queue;
2094 struct cs_etm_queue *etmq;
2095 struct cs_etm_traceid_queue *tidq;
2098 if (!etm->heap.heap_cnt)
2101 /* Take the entry at the top of the min heap */
2102 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2103 queue_nr = TO_QUEUE_NR(cs_queue_nr);
2104 trace_chan_id = TO_TRACE_CHAN_ID(cs_queue_nr);
2105 queue = &etm->queues.queue_array[queue_nr];
2109 * Remove the top entry from the heap since we are about
2112 auxtrace_heap__pop(&etm->heap);
2114 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
2117 * No traceID queue has been allocated for this traceID,
2118 * which means something somewhere went very wrong. No
2119 * other choice than simply exit.
2126 * Packets associated with this timestamp are already in
2127 * the etmq's traceID queue, so process them.
2129 ret = cs_etm__process_traceid_queue(etmq, tidq);
2134 * Packets for this timestamp have been processed, time to
2135 * move on to the next timestamp, fetching a new auxtrace_buffer
2139 ret = cs_etm__get_data_block(etmq);
2144 * No more auxtrace_buffers to process in this etmq, simply
2145 * move on to another entry in the auxtrace_heap.
2150 ret = cs_etm__decode_data_block(etmq);
2154 timestamp = cs_etm__etmq_get_timestamp(etmq, &trace_chan_id);
2158 * Function cs_etm__decode_data_block() returns when
2159 * there is no more traces to decode in the current
2160 * auxtrace_buffer OR when a timestamp has been
2161 * encountered on any of the traceID queues. Since we
2162 * did not get a timestamp, there is no more traces to
2163 * process in this auxtrace_buffer. As such empty and
2164 * flush all traceID queues.
2166 cs_etm__clear_all_traceid_queues(etmq);
2168 /* Fetch another auxtrace_buffer for this etmq */
2173 * Add to the min heap the timestamp for packets that have
2174 * just been decoded. They will be processed and synthesized
2175 * during the next call to cs_etm__process_traceid_queue() for
2176 * this queue/traceID.
2178 cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
2179 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, timestamp);
2186 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
2187 union perf_event *event)
2191 if (etm->timeless_decoding)
2195 * Add the tid/pid to the log so that we can get a match when
2196 * we get a contextID from the decoder.
2198 th = machine__findnew_thread(etm->machine,
2199 event->itrace_start.pid,
2200 event->itrace_start.tid);
2209 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
2210 union perf_event *event)
2213 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2216 * Context switch in per-thread mode are irrelevant since perf
2217 * will start/stop tracing as the process is scheduled.
2219 if (etm->timeless_decoding)
2223 * SWITCH_IN events carry the next process to be switched out while
2224 * SWITCH_OUT events carry the process to be switched in. As such
2225 * we don't care about IN events.
2231 * Add the tid/pid to the log so that we can get a match when
2232 * we get a contextID from the decoder.
2234 th = machine__findnew_thread(etm->machine,
2235 event->context_switch.next_prev_pid,
2236 event->context_switch.next_prev_tid);
2245 static int cs_etm__process_event(struct perf_session *session,
2246 union perf_event *event,
2247 struct perf_sample *sample,
2248 struct perf_tool *tool)
2252 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2253 struct cs_etm_auxtrace,
2259 if (!tool->ordered_events) {
2260 pr_err("CoreSight ETM Trace requires ordered events\n");
2264 if (sample->time && (sample->time != (u64) -1))
2265 timestamp = sample->time;
2269 if (timestamp || etm->timeless_decoding) {
2270 err = cs_etm__update_queues(etm);
2275 if (etm->timeless_decoding &&
2276 event->header.type == PERF_RECORD_EXIT)
2277 return cs_etm__process_timeless_queues(etm,
2280 if (event->header.type == PERF_RECORD_ITRACE_START)
2281 return cs_etm__process_itrace_start(etm, event);
2282 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2283 return cs_etm__process_switch_cpu_wide(etm, event);
2285 if (!etm->timeless_decoding &&
2286 event->header.type == PERF_RECORD_AUX)
2287 return cs_etm__process_queues(etm);
2292 static int cs_etm__process_auxtrace_event(struct perf_session *session,
2293 union perf_event *event,
2294 struct perf_tool *tool __maybe_unused)
2296 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2297 struct cs_etm_auxtrace,
2299 if (!etm->data_queued) {
2300 struct auxtrace_buffer *buffer;
2302 int fd = perf_data__fd(session->data);
2303 bool is_pipe = perf_data__is_pipe(session->data);
2309 data_offset = lseek(fd, 0, SEEK_CUR);
2310 if (data_offset == -1)
2314 err = auxtrace_queues__add_event(&etm->queues, session,
2315 event, data_offset, &buffer);
2320 if (auxtrace_buffer__get_data(buffer, fd)) {
2321 cs_etm__dump_event(etm, buffer);
2322 auxtrace_buffer__put_data(buffer);
2329 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
2331 struct evsel *evsel;
2332 struct evlist *evlist = etm->session->evlist;
2333 bool timeless_decoding = true;
2336 * Circle through the list of event and complain if we find one
2337 * with the time bit set.
2339 evlist__for_each_entry(evlist, evsel) {
2340 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
2341 timeless_decoding = false;
2344 return timeless_decoding;
2347 static const char * const cs_etm_global_header_fmts[] = {
2348 [CS_HEADER_VERSION_0] = " Header version %llx\n",
2349 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
2350 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
2353 static const char * const cs_etm_priv_fmts[] = {
2354 [CS_ETM_MAGIC] = " Magic number %llx\n",
2355 [CS_ETM_CPU] = " CPU %lld\n",
2356 [CS_ETM_ETMCR] = " ETMCR %llx\n",
2357 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
2358 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
2359 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
2362 static const char * const cs_etmv4_priv_fmts[] = {
2363 [CS_ETM_MAGIC] = " Magic number %llx\n",
2364 [CS_ETM_CPU] = " CPU %lld\n",
2365 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
2366 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
2367 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
2368 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
2369 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
2370 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
2371 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
2374 static void cs_etm__print_auxtrace_info(__u64 *val, int num)
2378 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2379 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
2381 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
2382 if (val[i] == __perf_cs_etmv3_magic)
2383 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
2384 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
2385 else if (val[i] == __perf_cs_etmv4_magic)
2386 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
2387 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
2389 /* failure.. return */
2394 int cs_etm__process_auxtrace_info(union perf_event *event,
2395 struct perf_session *session)
2397 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
2398 struct cs_etm_auxtrace *etm = NULL;
2399 struct int_node *inode;
2400 unsigned int pmu_type;
2401 int event_header_size = sizeof(struct perf_event_header);
2402 int info_header_size;
2403 int total_size = auxtrace_info->header.size;
2406 int err = 0, idx = -1;
2408 u64 *ptr, *hdr = NULL;
2409 u64 **metadata = NULL;
2412 * sizeof(auxtrace_info_event::type) +
2413 * sizeof(auxtrace_info_event::reserved) == 8
2415 info_header_size = 8;
2417 if (total_size < (event_header_size + info_header_size))
2420 priv_size = total_size - event_header_size - info_header_size;
2422 /* First the global part */
2423 ptr = (u64 *) auxtrace_info->priv;
2425 /* Look for version '0' of the header */
2429 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
2433 /* Extract header information - see cs-etm.h for format */
2434 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2436 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2437 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2441 * Create an RB tree for traceID-metadata tuple. Since the conversion
2442 * has to be made for each packet that gets decoded, optimizing access
2443 * in anything other than a sequential array is worth doing.
2445 traceid_list = intlist__new(NULL);
2446 if (!traceid_list) {
2451 metadata = zalloc(sizeof(*metadata) * num_cpu);
2454 goto err_free_traceid_list;
2458 * The metadata is stored in the auxtrace_info section and encodes
2459 * the configuration of the ARM embedded trace macrocell which is
2460 * required by the trace decoder to properly decode the trace due
2461 * to its highly compressed nature.
2463 for (j = 0; j < num_cpu; j++) {
2464 if (ptr[i] == __perf_cs_etmv3_magic) {
2465 metadata[j] = zalloc(sizeof(*metadata[j]) *
2469 goto err_free_metadata;
2471 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
2472 metadata[j][k] = ptr[i + k];
2474 /* The traceID is our handle */
2475 idx = metadata[j][CS_ETM_ETMTRACEIDR];
2476 i += CS_ETM_PRIV_MAX;
2477 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2478 metadata[j] = zalloc(sizeof(*metadata[j]) *
2482 goto err_free_metadata;
2484 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
2485 metadata[j][k] = ptr[i + k];
2487 /* The traceID is our handle */
2488 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
2489 i += CS_ETMV4_PRIV_MAX;
2492 /* Get an RB node for this CPU */
2493 inode = intlist__findnew(traceid_list, idx);
2495 /* Something went wrong, no need to continue */
2498 goto err_free_metadata;
2502 * The node for that CPU should not be taken.
2503 * Back out if that's the case.
2507 goto err_free_metadata;
2509 /* All good, associate the traceID with the metadata pointer */
2510 inode->priv = metadata[j];
2514 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2515 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2516 * global metadata, and each cpu's metadata respectively.
2517 * The following tests if the correct number of double words was
2518 * present in the auxtrace info section.
2520 if (i * 8 != priv_size) {
2522 goto err_free_metadata;
2525 etm = zalloc(sizeof(*etm));
2529 goto err_free_metadata;
2532 err = auxtrace_queues__init(&etm->queues);
2536 etm->session = session;
2537 etm->machine = &session->machines.host;
2539 etm->num_cpu = num_cpu;
2540 etm->pmu_type = pmu_type;
2541 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
2542 etm->metadata = metadata;
2543 etm->auxtrace_type = auxtrace_info->type;
2544 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
2546 etm->auxtrace.process_event = cs_etm__process_event;
2547 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
2548 etm->auxtrace.flush_events = cs_etm__flush_events;
2549 etm->auxtrace.free_events = cs_etm__free_events;
2550 etm->auxtrace.free = cs_etm__free;
2551 session->auxtrace = &etm->auxtrace;
2553 etm->unknown_thread = thread__new(999999999, 999999999);
2554 if (!etm->unknown_thread) {
2556 goto err_free_queues;
2560 * Initialize list node so that at thread__zput() we can avoid
2561 * segmentation fault at list_del_init().
2563 INIT_LIST_HEAD(&etm->unknown_thread->node);
2565 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
2567 goto err_delete_thread;
2569 if (thread__init_map_groups(etm->unknown_thread, etm->machine)) {
2571 goto err_delete_thread;
2575 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
2579 if (session->itrace_synth_opts->set) {
2580 etm->synth_opts = *session->itrace_synth_opts;
2582 itrace_synth_opts__set_default(&etm->synth_opts,
2583 session->itrace_synth_opts->default_no_sample);
2584 etm->synth_opts.callchain = false;
2587 err = cs_etm__synth_events(etm, session);
2589 goto err_delete_thread;
2591 err = auxtrace_queues__process_index(&etm->queues, session);
2593 goto err_delete_thread;
2595 etm->data_queued = etm->queues.populated;
2600 thread__zput(etm->unknown_thread);
2602 auxtrace_queues__free(&etm->queues);
2603 session->auxtrace = NULL;
2607 /* No need to check @metadata[j], free(NULL) is supported */
2608 for (j = 0; j < num_cpu; j++)
2609 zfree(&metadata[j]);
2611 err_free_traceid_list:
2612 intlist__delete(traceid_list);