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>
15 #include <opencsd/ocsd_if_types.h>
21 #include "cs-etm-decoder/cs-etm-decoder.h"
30 #include "thread_map.h"
31 #include "thread-stack.h"
34 #define MAX_TIMESTAMP (~0ULL)
36 struct cs_etm_auxtrace {
37 struct auxtrace auxtrace;
38 struct auxtrace_queues queues;
39 struct auxtrace_heap heap;
40 struct itrace_synth_opts synth_opts;
41 struct perf_session *session;
42 struct machine *machine;
43 struct thread *unknown_thread;
49 u8 sample_instructions;
53 u64 branches_sample_type;
55 u64 instructions_sample_type;
56 u64 instructions_sample_period;
60 unsigned int pmu_type;
63 struct cs_etm_traceid_queue {
65 u64 period_instructions;
66 size_t last_branch_pos;
67 union perf_event *event_buf;
68 struct thread *thread;
69 struct branch_stack *last_branch;
70 struct branch_stack *last_branch_rb;
71 struct cs_etm_packet *prev_packet;
72 struct cs_etm_packet *packet;
73 struct cs_etm_packet_queue packet_queue;
77 struct cs_etm_auxtrace *etm;
78 struct cs_etm_decoder *decoder;
79 struct auxtrace_buffer *buffer;
80 unsigned int queue_nr;
83 const unsigned char *buf;
84 size_t buf_len, buf_used;
85 struct cs_etm_traceid_queue *traceid_queues;
88 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
89 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
92 /* PTMs ETMIDR [11:8] set to b0011 */
93 #define ETMIDR_PTM_VERSION 0x00000300
95 static u32 cs_etm__get_v7_protocol_version(u32 etmidr)
97 etmidr &= ETMIDR_PTM_VERSION;
99 if (etmidr == ETMIDR_PTM_VERSION)
100 return CS_ETM_PROTO_PTM;
102 return CS_ETM_PROTO_ETMV3;
105 static int cs_etm__get_magic(u8 trace_chan_id, u64 *magic)
107 struct int_node *inode;
110 inode = intlist__find(traceid_list, trace_chan_id);
114 metadata = inode->priv;
115 *magic = metadata[CS_ETM_MAGIC];
119 int cs_etm__get_cpu(u8 trace_chan_id, int *cpu)
121 struct int_node *inode;
124 inode = intlist__find(traceid_list, trace_chan_id);
128 metadata = inode->priv;
129 *cpu = (int)metadata[CS_ETM_CPU];
133 static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
139 queue->packet_count = 0;
140 for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
141 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
142 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
143 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
144 queue->packet_buffer[i].instr_count = 0;
145 queue->packet_buffer[i].last_instr_taken_branch = false;
146 queue->packet_buffer[i].last_instr_size = 0;
147 queue->packet_buffer[i].last_instr_type = 0;
148 queue->packet_buffer[i].last_instr_subtype = 0;
149 queue->packet_buffer[i].last_instr_cond = 0;
150 queue->packet_buffer[i].flags = 0;
151 queue->packet_buffer[i].exception_number = UINT32_MAX;
152 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
153 queue->packet_buffer[i].cpu = INT_MIN;
157 static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
158 struct cs_etm_traceid_queue *tidq,
162 struct cs_etm_auxtrace *etm = etmq->etm;
164 cs_etm__clear_packet_queue(&tidq->packet_queue);
166 tidq->trace_chan_id = trace_chan_id;
168 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
172 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
173 if (!tidq->prev_packet)
176 if (etm->synth_opts.last_branch) {
177 size_t sz = sizeof(struct branch_stack);
179 sz += etm->synth_opts.last_branch_sz *
180 sizeof(struct branch_entry);
181 tidq->last_branch = zalloc(sz);
182 if (!tidq->last_branch)
184 tidq->last_branch_rb = zalloc(sz);
185 if (!tidq->last_branch_rb)
189 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
190 if (!tidq->event_buf)
196 zfree(&tidq->last_branch_rb);
197 zfree(&tidq->last_branch);
198 zfree(&tidq->prev_packet);
199 zfree(&tidq->packet);
204 static struct cs_etm_traceid_queue
205 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
207 struct cs_etm_traceid_queue *tidq;
208 struct cs_etm_auxtrace *etm = etmq->etm;
210 if (!etm->timeless_decoding)
213 tidq = etmq->traceid_queues;
218 tidq = malloc(sizeof(*tidq));
222 memset(tidq, 0, sizeof(*tidq));
224 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
227 etmq->traceid_queues = tidq;
229 return etmq->traceid_queues;
237 struct cs_etm_packet_queue
238 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
240 struct cs_etm_traceid_queue *tidq;
242 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
244 return &tidq->packet_queue;
249 static void cs_etm__packet_dump(const char *pkt_string)
251 const char *color = PERF_COLOR_BLUE;
252 int len = strlen(pkt_string);
254 if (len && (pkt_string[len-1] == '\n'))
255 color_fprintf(stdout, color, " %s", pkt_string);
257 color_fprintf(stdout, color, " %s\n", pkt_string);
262 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
263 struct cs_etm_auxtrace *etm, int idx,
266 u64 **metadata = etm->metadata;
268 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
269 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
270 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
273 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
274 struct cs_etm_auxtrace *etm, int idx)
276 u64 **metadata = etm->metadata;
278 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
279 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
280 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
281 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
282 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
283 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
284 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
287 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
288 struct cs_etm_auxtrace *etm)
294 for (i = 0; i < etm->num_cpu; i++) {
295 architecture = etm->metadata[i][CS_ETM_MAGIC];
297 switch (architecture) {
298 case __perf_cs_etmv3_magic:
299 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
300 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
302 case __perf_cs_etmv4_magic:
303 cs_etm__set_trace_param_etmv4(t_params, etm, i);
313 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
314 struct cs_etm_queue *etmq,
315 enum cs_etm_decoder_operation mode)
319 if (!(mode < CS_ETM_OPERATION_MAX))
322 d_params->packet_printer = cs_etm__packet_dump;
323 d_params->operation = mode;
324 d_params->data = etmq;
325 d_params->formatted = true;
326 d_params->fsyncs = false;
327 d_params->hsyncs = false;
328 d_params->frame_aligned = true;
335 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
336 struct auxtrace_buffer *buffer)
339 const char *color = PERF_COLOR_BLUE;
340 struct cs_etm_decoder_params d_params;
341 struct cs_etm_trace_params *t_params;
342 struct cs_etm_decoder *decoder;
343 size_t buffer_used = 0;
345 fprintf(stdout, "\n");
346 color_fprintf(stdout, color,
347 ". ... CoreSight ETM Trace data: size %zu bytes\n",
350 /* Use metadata to fill in trace parameters for trace decoder */
351 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
356 if (cs_etm__init_trace_params(t_params, etm))
359 /* Set decoder parameters to simply print the trace packets */
360 if (cs_etm__init_decoder_params(&d_params, NULL,
361 CS_ETM_OPERATION_PRINT))
364 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
371 ret = cs_etm_decoder__process_data_block(
372 decoder, buffer->offset,
373 &((u8 *)buffer->data)[buffer_used],
374 buffer->size - buffer_used, &consumed);
378 buffer_used += consumed;
379 } while (buffer_used < buffer->size);
381 cs_etm_decoder__free(decoder);
387 static int cs_etm__flush_events(struct perf_session *session,
388 struct perf_tool *tool)
391 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
392 struct cs_etm_auxtrace,
397 if (!tool->ordered_events)
400 if (!etm->timeless_decoding)
403 ret = cs_etm__update_queues(etm);
408 return cs_etm__process_timeless_queues(etm, -1);
411 static void cs_etm__free_queue(void *priv)
413 struct cs_etm_queue *etmq = priv;
418 thread__zput(etmq->traceid_queues->thread);
419 cs_etm_decoder__free(etmq->decoder);
420 zfree(&etmq->traceid_queues->event_buf);
421 zfree(&etmq->traceid_queues->last_branch);
422 zfree(&etmq->traceid_queues->last_branch_rb);
423 zfree(&etmq->traceid_queues->prev_packet);
424 zfree(&etmq->traceid_queues->packet);
425 zfree(&etmq->traceid_queues);
429 static void cs_etm__free_events(struct perf_session *session)
432 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
433 struct cs_etm_auxtrace,
435 struct auxtrace_queues *queues = &aux->queues;
437 for (i = 0; i < queues->nr_queues; i++) {
438 cs_etm__free_queue(queues->queue_array[i].priv);
439 queues->queue_array[i].priv = NULL;
442 auxtrace_queues__free(queues);
445 static void cs_etm__free(struct perf_session *session)
448 struct int_node *inode, *tmp;
449 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
450 struct cs_etm_auxtrace,
452 cs_etm__free_events(session);
453 session->auxtrace = NULL;
455 /* First remove all traceID/metadata nodes for the RB tree */
456 intlist__for_each_entry_safe(inode, tmp, traceid_list)
457 intlist__remove(traceid_list, inode);
458 /* Then the RB tree itself */
459 intlist__delete(traceid_list);
461 for (i = 0; i < aux->num_cpu; i++)
462 zfree(&aux->metadata[i]);
464 thread__zput(aux->unknown_thread);
465 zfree(&aux->metadata);
469 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
471 struct machine *machine;
473 machine = etmq->etm->machine;
475 if (address >= etmq->etm->kernel_start) {
476 if (machine__is_host(machine))
477 return PERF_RECORD_MISC_KERNEL;
479 return PERF_RECORD_MISC_GUEST_KERNEL;
481 if (machine__is_host(machine))
482 return PERF_RECORD_MISC_USER;
484 return PERF_RECORD_MISC_GUEST_USER;
486 return PERF_RECORD_MISC_HYPERVISOR;
490 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u64 address,
491 size_t size, u8 *buffer)
496 struct thread *thread;
497 struct machine *machine;
498 struct addr_location al;
503 machine = etmq->etm->machine;
504 cpumode = cs_etm__cpu_mode(etmq, address);
506 thread = etmq->traceid_queues->thread;
508 if (cpumode != PERF_RECORD_MISC_KERNEL)
510 thread = etmq->etm->unknown_thread;
513 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
516 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
517 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
520 offset = al.map->map_ip(al.map, address);
524 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
532 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
534 struct cs_etm_decoder_params d_params;
535 struct cs_etm_trace_params *t_params = NULL;
536 struct cs_etm_queue *etmq;
538 etmq = zalloc(sizeof(*etmq));
542 /* Use metadata to fill in trace parameters for trace decoder */
543 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
548 if (cs_etm__init_trace_params(t_params, etm))
551 /* Set decoder parameters to decode trace packets */
552 if (cs_etm__init_decoder_params(&d_params, etmq,
553 CS_ETM_OPERATION_DECODE))
556 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
562 * Register a function to handle all memory accesses required by
563 * the trace decoder library.
565 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
568 goto out_free_decoder;
574 cs_etm_decoder__free(etmq->decoder);
581 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
582 struct auxtrace_queue *queue,
583 unsigned int queue_nr)
586 struct cs_etm_queue *etmq = queue->priv;
588 if (list_empty(&queue->head) || etmq)
591 etmq = cs_etm__alloc_queue(etm);
600 etmq->queue_nr = queue_nr;
601 etmq->tid = queue->tid;
609 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
614 if (!etm->kernel_start)
615 etm->kernel_start = machine__kernel_start(etm->machine);
617 for (i = 0; i < etm->queues.nr_queues; i++) {
618 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
626 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
628 if (etm->queues.new_data) {
629 etm->queues.new_data = false;
630 return cs_etm__setup_queues(etm);
637 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
638 struct cs_etm_traceid_queue *tidq)
640 struct branch_stack *bs_src = tidq->last_branch_rb;
641 struct branch_stack *bs_dst = tidq->last_branch;
645 * Set the number of records before early exit: ->nr is used to
646 * determine how many branches to copy from ->entries.
648 bs_dst->nr = bs_src->nr;
651 * Early exit when there is nothing to copy.
657 * As bs_src->entries is a circular buffer, we need to copy from it in
658 * two steps. First, copy the branches from the most recently inserted
659 * branch ->last_branch_pos until the end of bs_src->entries buffer.
661 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
662 memcpy(&bs_dst->entries[0],
663 &bs_src->entries[tidq->last_branch_pos],
664 sizeof(struct branch_entry) * nr);
667 * If we wrapped around at least once, the branches from the beginning
668 * of the bs_src->entries buffer and until the ->last_branch_pos element
669 * are older valid branches: copy them over. The total number of
670 * branches copied over will be equal to the number of branches asked by
671 * the user in last_branch_sz.
673 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
674 memcpy(&bs_dst->entries[nr],
676 sizeof(struct branch_entry) * tidq->last_branch_pos);
681 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
683 tidq->last_branch_pos = 0;
684 tidq->last_branch_rb->nr = 0;
687 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
691 cs_etm__mem_access(etmq, addr, ARRAY_SIZE(instrBytes), instrBytes);
693 * T32 instruction size is indicated by bits[15:11] of the first
694 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
695 * denote a 32-bit instruction.
697 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
700 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
702 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
703 if (packet->sample_type == CS_ETM_DISCONTINUITY)
706 return packet->start_addr;
710 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
712 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
713 if (packet->sample_type == CS_ETM_DISCONTINUITY)
716 return packet->end_addr - packet->last_instr_size;
719 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
720 const struct cs_etm_packet *packet,
723 if (packet->isa == CS_ETM_ISA_T32) {
724 u64 addr = packet->start_addr;
727 addr += cs_etm__t32_instr_size(etmq, addr);
733 /* Assume a 4 byte instruction size (A32/A64) */
734 return packet->start_addr + offset * 4;
737 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
738 struct cs_etm_traceid_queue *tidq)
740 struct branch_stack *bs = tidq->last_branch_rb;
741 struct branch_entry *be;
744 * The branches are recorded in a circular buffer in reverse
745 * chronological order: we start recording from the last element of the
746 * buffer down. After writing the first element of the stack, move the
747 * insert position back to the end of the buffer.
749 if (!tidq->last_branch_pos)
750 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
752 tidq->last_branch_pos -= 1;
754 be = &bs->entries[tidq->last_branch_pos];
755 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
756 be->to = cs_etm__first_executed_instr(tidq->packet);
757 /* No support for mispredict */
758 be->flags.mispred = 0;
759 be->flags.predicted = 1;
762 * Increment bs->nr until reaching the number of last branches asked by
763 * the user on the command line.
765 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
769 static int cs_etm__inject_event(union perf_event *event,
770 struct perf_sample *sample, u64 type)
772 event->header.size = perf_event__sample_event_size(sample, type, 0);
773 return perf_event__synthesize_sample(event, type, 0, sample);
778 cs_etm__get_trace(struct cs_etm_queue *etmq)
780 struct auxtrace_buffer *aux_buffer = etmq->buffer;
781 struct auxtrace_buffer *old_buffer = aux_buffer;
782 struct auxtrace_queue *queue;
784 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
786 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
788 /* If no more data, drop the previous auxtrace_buffer and return */
791 auxtrace_buffer__drop_data(old_buffer);
796 etmq->buffer = aux_buffer;
798 /* If the aux_buffer doesn't have data associated, try to load it */
799 if (!aux_buffer->data) {
800 /* get the file desc associated with the perf data file */
801 int fd = perf_data__fd(etmq->etm->session->data);
803 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
804 if (!aux_buffer->data)
808 /* If valid, drop the previous buffer */
810 auxtrace_buffer__drop_data(old_buffer);
813 etmq->buf_len = aux_buffer->size;
814 etmq->buf = aux_buffer->data;
816 return etmq->buf_len;
819 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
820 struct auxtrace_queue *queue)
822 struct cs_etm_traceid_queue *tidq;
823 struct cs_etm_queue *etmq = queue->priv;
825 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
827 /* CPU-wide tracing isn't supported yet */
828 if (queue->tid == -1)
831 if ((!tidq->thread) && (etmq->tid != -1))
832 tidq->thread = machine__find_thread(etm->machine, -1,
836 etmq->pid = tidq->thread->pid_;
839 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
840 struct cs_etm_traceid_queue *tidq,
841 u64 addr, u64 period)
844 struct cs_etm_auxtrace *etm = etmq->etm;
845 union perf_event *event = tidq->event_buf;
846 struct perf_sample sample = {.ip = 0,};
848 event->sample.header.type = PERF_RECORD_SAMPLE;
849 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
850 event->sample.header.size = sizeof(struct perf_event_header);
853 sample.pid = etmq->pid;
854 sample.tid = etmq->tid;
855 sample.id = etmq->etm->instructions_id;
856 sample.stream_id = etmq->etm->instructions_id;
857 sample.period = period;
858 sample.cpu = tidq->packet->cpu;
859 sample.flags = tidq->prev_packet->flags;
861 sample.cpumode = event->sample.header.misc;
863 if (etm->synth_opts.last_branch) {
864 cs_etm__copy_last_branch_rb(etmq, tidq);
865 sample.branch_stack = tidq->last_branch;
868 if (etm->synth_opts.inject) {
869 ret = cs_etm__inject_event(event, &sample,
870 etm->instructions_sample_type);
875 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
879 "CS ETM Trace: failed to deliver instruction event, error %d\n",
882 if (etm->synth_opts.last_branch)
883 cs_etm__reset_last_branch_rb(tidq);
889 * The cs etm packet encodes an instruction range between a branch target
890 * and the next taken branch. Generate sample accordingly.
892 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
893 struct cs_etm_traceid_queue *tidq)
896 struct cs_etm_auxtrace *etm = etmq->etm;
897 struct perf_sample sample = {.ip = 0,};
898 union perf_event *event = tidq->event_buf;
899 struct dummy_branch_stack {
901 struct branch_entry entries;
905 ip = cs_etm__last_executed_instr(tidq->prev_packet);
907 event->sample.header.type = PERF_RECORD_SAMPLE;
908 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
909 event->sample.header.size = sizeof(struct perf_event_header);
912 sample.pid = etmq->pid;
913 sample.tid = etmq->tid;
914 sample.addr = cs_etm__first_executed_instr(tidq->packet);
915 sample.id = etmq->etm->branches_id;
916 sample.stream_id = etmq->etm->branches_id;
918 sample.cpu = tidq->packet->cpu;
919 sample.flags = tidq->prev_packet->flags;
920 sample.cpumode = event->sample.header.misc;
923 * perf report cannot handle events without a branch stack
925 if (etm->synth_opts.last_branch) {
926 dummy_bs = (struct dummy_branch_stack){
933 sample.branch_stack = (struct branch_stack *)&dummy_bs;
936 if (etm->synth_opts.inject) {
937 ret = cs_etm__inject_event(event, &sample,
938 etm->branches_sample_type);
943 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
947 "CS ETM Trace: failed to deliver instruction event, error %d\n",
953 struct cs_etm_synth {
954 struct perf_tool dummy_tool;
955 struct perf_session *session;
958 static int cs_etm__event_synth(struct perf_tool *tool,
959 union perf_event *event,
960 struct perf_sample *sample __maybe_unused,
961 struct machine *machine __maybe_unused)
963 struct cs_etm_synth *cs_etm_synth =
964 container_of(tool, struct cs_etm_synth, dummy_tool);
966 return perf_session__deliver_synth_event(cs_etm_synth->session,
970 static int cs_etm__synth_event(struct perf_session *session,
971 struct perf_event_attr *attr, u64 id)
973 struct cs_etm_synth cs_etm_synth;
975 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
976 cs_etm_synth.session = session;
978 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
979 &id, cs_etm__event_synth);
982 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
983 struct perf_session *session)
985 struct perf_evlist *evlist = session->evlist;
986 struct perf_evsel *evsel;
987 struct perf_event_attr attr;
992 evlist__for_each_entry(evlist, evsel) {
993 if (evsel->attr.type == etm->pmu_type) {
1000 pr_debug("No selected events with CoreSight Trace data\n");
1004 memset(&attr, 0, sizeof(struct perf_event_attr));
1005 attr.size = sizeof(struct perf_event_attr);
1006 attr.type = PERF_TYPE_HARDWARE;
1007 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
1008 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1010 if (etm->timeless_decoding)
1011 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1013 attr.sample_type |= PERF_SAMPLE_TIME;
1015 attr.exclude_user = evsel->attr.exclude_user;
1016 attr.exclude_kernel = evsel->attr.exclude_kernel;
1017 attr.exclude_hv = evsel->attr.exclude_hv;
1018 attr.exclude_host = evsel->attr.exclude_host;
1019 attr.exclude_guest = evsel->attr.exclude_guest;
1020 attr.sample_id_all = evsel->attr.sample_id_all;
1021 attr.read_format = evsel->attr.read_format;
1023 /* create new id val to be a fixed offset from evsel id */
1024 id = evsel->id[0] + 1000000000;
1029 if (etm->synth_opts.branches) {
1030 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1031 attr.sample_period = 1;
1032 attr.sample_type |= PERF_SAMPLE_ADDR;
1033 err = cs_etm__synth_event(session, &attr, id);
1036 etm->sample_branches = true;
1037 etm->branches_sample_type = attr.sample_type;
1038 etm->branches_id = id;
1040 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1043 if (etm->synth_opts.last_branch)
1044 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1046 if (etm->synth_opts.instructions) {
1047 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1048 attr.sample_period = etm->synth_opts.period;
1049 etm->instructions_sample_period = attr.sample_period;
1050 err = cs_etm__synth_event(session, &attr, id);
1053 etm->sample_instructions = true;
1054 etm->instructions_sample_type = attr.sample_type;
1055 etm->instructions_id = id;
1062 static int cs_etm__sample(struct cs_etm_queue *etmq,
1063 struct cs_etm_traceid_queue *tidq)
1065 struct cs_etm_auxtrace *etm = etmq->etm;
1066 struct cs_etm_packet *tmp;
1068 u64 instrs_executed = tidq->packet->instr_count;
1070 tidq->period_instructions += instrs_executed;
1073 * Record a branch when the last instruction in
1074 * PREV_PACKET is a branch.
1076 if (etm->synth_opts.last_branch &&
1077 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1078 tidq->prev_packet->last_instr_taken_branch)
1079 cs_etm__update_last_branch_rb(etmq, tidq);
1081 if (etm->sample_instructions &&
1082 tidq->period_instructions >= etm->instructions_sample_period) {
1084 * Emit instruction sample periodically
1085 * TODO: allow period to be defined in cycles and clock time
1088 /* Get number of instructions executed after the sample point */
1089 u64 instrs_over = tidq->period_instructions -
1090 etm->instructions_sample_period;
1093 * Calculate the address of the sampled instruction (-1 as
1094 * sample is reported as though instruction has just been
1095 * executed, but PC has not advanced to next instruction)
1097 u64 offset = (instrs_executed - instrs_over - 1);
1098 u64 addr = cs_etm__instr_addr(etmq, tidq->packet, offset);
1100 ret = cs_etm__synth_instruction_sample(
1101 etmq, tidq, addr, etm->instructions_sample_period);
1105 /* Carry remaining instructions into next sample period */
1106 tidq->period_instructions = instrs_over;
1109 if (etm->sample_branches) {
1110 bool generate_sample = false;
1112 /* Generate sample for tracing on packet */
1113 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1114 generate_sample = true;
1116 /* Generate sample for branch taken packet */
1117 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1118 tidq->prev_packet->last_instr_taken_branch)
1119 generate_sample = true;
1121 if (generate_sample) {
1122 ret = cs_etm__synth_branch_sample(etmq, tidq);
1128 if (etm->sample_branches || etm->synth_opts.last_branch) {
1130 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1131 * the next incoming packet.
1134 tidq->packet = tidq->prev_packet;
1135 tidq->prev_packet = tmp;
1141 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1144 * When the exception packet is inserted, whether the last instruction
1145 * in previous range packet is taken branch or not, we need to force
1146 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1147 * to generate branch sample for the instruction range before the
1148 * exception is trapped to kernel or before the exception returning.
1150 * The exception packet includes the dummy address values, so don't
1151 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1152 * for generating instruction and branch samples.
1154 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1155 tidq->prev_packet->last_instr_taken_branch = true;
1160 static int cs_etm__flush(struct cs_etm_queue *etmq,
1161 struct cs_etm_traceid_queue *tidq)
1164 struct cs_etm_auxtrace *etm = etmq->etm;
1165 struct cs_etm_packet *tmp;
1167 /* Handle start tracing packet */
1168 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1171 if (etmq->etm->synth_opts.last_branch &&
1172 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1174 * Generate a last branch event for the branches left in the
1175 * circular buffer at the end of the trace.
1177 * Use the address of the end of the last reported execution
1180 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1182 err = cs_etm__synth_instruction_sample(
1184 tidq->period_instructions);
1188 tidq->period_instructions = 0;
1192 if (etm->sample_branches &&
1193 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1194 err = cs_etm__synth_branch_sample(etmq, tidq);
1200 if (etm->sample_branches || etm->synth_opts.last_branch) {
1202 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1203 * the next incoming packet.
1206 tidq->packet = tidq->prev_packet;
1207 tidq->prev_packet = tmp;
1213 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1214 struct cs_etm_traceid_queue *tidq)
1219 * It has no new packet coming and 'etmq->packet' contains the stale
1220 * packet which was set at the previous time with packets swapping;
1221 * so skip to generate branch sample to avoid stale packet.
1223 * For this case only flush branch stack and generate a last branch
1224 * event for the branches left in the circular buffer at the end of
1227 if (etmq->etm->synth_opts.last_branch &&
1228 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1230 * Use the address of the end of the last reported execution
1233 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1235 err = cs_etm__synth_instruction_sample(
1237 tidq->period_instructions);
1241 tidq->period_instructions = 0;
1247 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1249 * Returns: < 0 if error
1250 * = 0 if no more auxtrace_buffer to read
1251 * > 0 if the current buffer isn't empty yet
1253 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1257 if (!etmq->buf_len) {
1258 ret = cs_etm__get_trace(etmq);
1262 * We cannot assume consecutive blocks in the data file
1263 * are contiguous, reset the decoder to force re-sync.
1265 ret = cs_etm_decoder__reset(etmq->decoder);
1270 return etmq->buf_len;
1273 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq,
1274 struct cs_etm_packet *packet,
1281 switch (packet->isa) {
1282 case CS_ETM_ISA_T32:
1284 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1287 * +-----------------+--------+
1288 * | 1 1 0 1 1 1 1 1 | imm8 |
1289 * +-----------------+--------+
1291 * According to the specifiction, it only defines SVC for T32
1292 * with 16 bits instruction and has no definition for 32bits;
1293 * so below only read 2 bytes as instruction size for T32.
1295 addr = end_addr - 2;
1296 cs_etm__mem_access(etmq, addr, sizeof(instr16), (u8 *)&instr16);
1297 if ((instr16 & 0xFF00) == 0xDF00)
1301 case CS_ETM_ISA_A32:
1303 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1305 * b'31 b'28 b'27 b'24
1306 * +---------+---------+-------------------------+
1307 * | !1111 | 1 1 1 1 | imm24 |
1308 * +---------+---------+-------------------------+
1310 addr = end_addr - 4;
1311 cs_etm__mem_access(etmq, addr, sizeof(instr32), (u8 *)&instr32);
1312 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1313 (instr32 & 0xF0000000) != 0xF0000000)
1317 case CS_ETM_ISA_A64:
1319 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1322 * +-----------------------+---------+-----------+
1323 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1324 * +-----------------------+---------+-----------+
1326 addr = end_addr - 4;
1327 cs_etm__mem_access(etmq, addr, sizeof(instr32), (u8 *)&instr32);
1328 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1332 case CS_ETM_ISA_UNKNOWN:
1340 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1341 struct cs_etm_traceid_queue *tidq, u64 magic)
1343 struct cs_etm_packet *packet = tidq->packet;
1344 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1346 if (magic == __perf_cs_etmv3_magic)
1347 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1351 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1352 * HVC cases; need to check if it's SVC instruction based on
1355 if (magic == __perf_cs_etmv4_magic) {
1356 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1357 cs_etm__is_svc_instr(etmq, prev_packet,
1358 prev_packet->end_addr))
1365 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1368 struct cs_etm_packet *packet = tidq->packet;
1370 if (magic == __perf_cs_etmv3_magic)
1371 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1372 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1373 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1374 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1375 packet->exception_number == CS_ETMV3_EXC_FIQ)
1378 if (magic == __perf_cs_etmv4_magic)
1379 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1380 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1381 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1382 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1383 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1384 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1385 packet->exception_number == CS_ETMV4_EXC_FIQ)
1391 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1392 struct cs_etm_traceid_queue *tidq,
1395 struct cs_etm_packet *packet = tidq->packet;
1396 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1398 if (magic == __perf_cs_etmv3_magic)
1399 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1400 packet->exception_number == CS_ETMV3_EXC_HYP ||
1401 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1402 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1403 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1404 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1405 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1408 if (magic == __perf_cs_etmv4_magic) {
1409 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1410 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1411 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1412 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1416 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1417 * (SMC, HVC) are taken as sync exceptions.
1419 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1420 !cs_etm__is_svc_instr(etmq, prev_packet,
1421 prev_packet->end_addr))
1425 * ETMv4 has 5 bits for exception number; if the numbers
1426 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1427 * they are implementation defined exceptions.
1429 * For this case, simply take it as sync exception.
1431 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1432 packet->exception_number <= CS_ETMV4_EXC_END)
1439 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1440 struct cs_etm_traceid_queue *tidq)
1442 struct cs_etm_packet *packet = tidq->packet;
1443 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1447 switch (packet->sample_type) {
1450 * Immediate branch instruction without neither link nor
1451 * return flag, it's normal branch instruction within
1454 if (packet->last_instr_type == OCSD_INSTR_BR &&
1455 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1456 packet->flags = PERF_IP_FLAG_BRANCH;
1458 if (packet->last_instr_cond)
1459 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1463 * Immediate branch instruction with link (e.g. BL), this is
1464 * branch instruction for function call.
1466 if (packet->last_instr_type == OCSD_INSTR_BR &&
1467 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1468 packet->flags = PERF_IP_FLAG_BRANCH |
1472 * Indirect branch instruction with link (e.g. BLR), this is
1473 * branch instruction for function call.
1475 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1476 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1477 packet->flags = PERF_IP_FLAG_BRANCH |
1481 * Indirect branch instruction with subtype of
1482 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1483 * function return for A32/T32.
1485 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1486 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1487 packet->flags = PERF_IP_FLAG_BRANCH |
1488 PERF_IP_FLAG_RETURN;
1491 * Indirect branch instruction without link (e.g. BR), usually
1492 * this is used for function return, especially for functions
1493 * within dynamic link lib.
1495 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1496 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1497 packet->flags = PERF_IP_FLAG_BRANCH |
1498 PERF_IP_FLAG_RETURN;
1500 /* Return instruction for function return. */
1501 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1502 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1503 packet->flags = PERF_IP_FLAG_BRANCH |
1504 PERF_IP_FLAG_RETURN;
1507 * Decoder might insert a discontinuity in the middle of
1508 * instruction packets, fixup prev_packet with flag
1509 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1511 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1512 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1513 PERF_IP_FLAG_TRACE_BEGIN;
1516 * If the previous packet is an exception return packet
1517 * and the return address just follows SVC instuction,
1518 * it needs to calibrate the previous packet sample flags
1519 * as PERF_IP_FLAG_SYSCALLRET.
1521 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1522 PERF_IP_FLAG_RETURN |
1523 PERF_IP_FLAG_INTERRUPT) &&
1524 cs_etm__is_svc_instr(etmq, packet, packet->start_addr))
1525 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1526 PERF_IP_FLAG_RETURN |
1527 PERF_IP_FLAG_SYSCALLRET;
1529 case CS_ETM_DISCONTINUITY:
1531 * The trace is discontinuous, if the previous packet is
1532 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1533 * for previous packet.
1535 if (prev_packet->sample_type == CS_ETM_RANGE)
1536 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1537 PERF_IP_FLAG_TRACE_END;
1539 case CS_ETM_EXCEPTION:
1540 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1544 /* The exception is for system call. */
1545 if (cs_etm__is_syscall(etmq, tidq, magic))
1546 packet->flags = PERF_IP_FLAG_BRANCH |
1548 PERF_IP_FLAG_SYSCALLRET;
1550 * The exceptions are triggered by external signals from bus,
1551 * interrupt controller, debug module, PE reset or halt.
1553 else if (cs_etm__is_async_exception(tidq, magic))
1554 packet->flags = PERF_IP_FLAG_BRANCH |
1556 PERF_IP_FLAG_ASYNC |
1557 PERF_IP_FLAG_INTERRUPT;
1559 * Otherwise, exception is caused by trap, instruction &
1560 * data fault, or alignment errors.
1562 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1563 packet->flags = PERF_IP_FLAG_BRANCH |
1565 PERF_IP_FLAG_INTERRUPT;
1568 * When the exception packet is inserted, since exception
1569 * packet is not used standalone for generating samples
1570 * and it's affiliation to the previous instruction range
1571 * packet; so set previous range packet flags to tell perf
1572 * it is an exception taken branch.
1574 if (prev_packet->sample_type == CS_ETM_RANGE)
1575 prev_packet->flags = packet->flags;
1577 case CS_ETM_EXCEPTION_RET:
1579 * When the exception return packet is inserted, since
1580 * exception return packet is not used standalone for
1581 * generating samples and it's affiliation to the previous
1582 * instruction range packet; so set previous range packet
1583 * flags to tell perf it is an exception return branch.
1585 * The exception return can be for either system call or
1586 * other exception types; unfortunately the packet doesn't
1587 * contain exception type related info so we cannot decide
1588 * the exception type purely based on exception return packet.
1589 * If we record the exception number from exception packet and
1590 * reuse it for excpetion return packet, this is not reliable
1591 * due the trace can be discontinuity or the interrupt can
1592 * be nested, thus the recorded exception number cannot be
1593 * used for exception return packet for these two cases.
1595 * For exception return packet, we only need to distinguish the
1596 * packet is for system call or for other types. Thus the
1597 * decision can be deferred when receive the next packet which
1598 * contains the return address, based on the return address we
1599 * can read out the previous instruction and check if it's a
1600 * system call instruction and then calibrate the sample flag
1603 if (prev_packet->sample_type == CS_ETM_RANGE)
1604 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1605 PERF_IP_FLAG_RETURN |
1606 PERF_IP_FLAG_INTERRUPT;
1616 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
1619 size_t processed = 0;
1622 * Packets are decoded and added to the decoder's packet queue
1623 * until the decoder packet processing callback has requested that
1624 * processing stops or there is nothing left in the buffer. Normal
1625 * operations that stop processing are a timestamp packet or a full
1626 * decoder buffer queue.
1628 ret = cs_etm_decoder__process_data_block(etmq->decoder,
1630 &etmq->buf[etmq->buf_used],
1636 etmq->offset += processed;
1637 etmq->buf_used += processed;
1638 etmq->buf_len -= processed;
1644 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
1645 struct cs_etm_traceid_queue *tidq)
1648 struct cs_etm_packet_queue *packet_queue;
1650 packet_queue = &tidq->packet_queue;
1652 /* Process each packet in this chunk */
1654 ret = cs_etm_decoder__get_packet(packet_queue,
1658 * Stop processing this chunk on
1659 * end of data or error
1664 * Since packet addresses are swapped in packet
1665 * handling within below switch() statements,
1666 * thus setting sample flags must be called
1667 * prior to switch() statement to use address
1668 * information before packets swapping.
1670 ret = cs_etm__set_sample_flags(etmq, tidq);
1674 switch (tidq->packet->sample_type) {
1677 * If the packet contains an instruction
1678 * range, generate instruction sequence
1681 cs_etm__sample(etmq, tidq);
1683 case CS_ETM_EXCEPTION:
1684 case CS_ETM_EXCEPTION_RET:
1686 * If the exception packet is coming,
1687 * make sure the previous instruction
1688 * range packet to be handled properly.
1690 cs_etm__exception(tidq);
1692 case CS_ETM_DISCONTINUITY:
1694 * Discontinuity in trace, flush
1695 * previous branch stack
1697 cs_etm__flush(etmq, tidq);
1701 * Should not receive empty packet,
1704 pr_err("CS ETM Trace: empty packet\n");
1714 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
1717 struct cs_etm_traceid_queue *tidq;
1719 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
1723 /* Go through each buffer in the queue and decode them one by one */
1725 err = cs_etm__get_data_block(etmq);
1729 /* Run trace decoder until buffer consumed or end of trace */
1731 err = cs_etm__decode_data_block(etmq);
1736 * Process each packet in this chunk, nothing to do if
1737 * an error occurs other than hoping the next one will
1740 err = cs_etm__process_traceid_queue(etmq, tidq);
1742 } while (etmq->buf_len);
1745 /* Flush any remaining branch stack entries */
1746 err = cs_etm__end_block(etmq, tidq);
1752 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
1756 struct auxtrace_queues *queues = &etm->queues;
1758 for (i = 0; i < queues->nr_queues; i++) {
1759 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
1760 struct cs_etm_queue *etmq = queue->priv;
1762 if (etmq && ((tid == -1) || (etmq->tid == tid))) {
1763 cs_etm__set_pid_tid_cpu(etm, queue);
1764 cs_etm__run_decoder(etmq);
1771 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
1772 union perf_event *event)
1776 if (etm->timeless_decoding)
1780 * Add the tid/pid to the log so that we can get a match when
1781 * we get a contextID from the decoder.
1783 th = machine__findnew_thread(etm->machine,
1784 event->itrace_start.pid,
1785 event->itrace_start.tid);
1794 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
1795 union perf_event *event)
1798 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1801 * Context switch in per-thread mode are irrelevant since perf
1802 * will start/stop tracing as the process is scheduled.
1804 if (etm->timeless_decoding)
1808 * SWITCH_IN events carry the next process to be switched out while
1809 * SWITCH_OUT events carry the process to be switched in. As such
1810 * we don't care about IN events.
1816 * Add the tid/pid to the log so that we can get a match when
1817 * we get a contextID from the decoder.
1819 th = machine__findnew_thread(etm->machine,
1820 event->context_switch.next_prev_pid,
1821 event->context_switch.next_prev_tid);
1830 static int cs_etm__process_event(struct perf_session *session,
1831 union perf_event *event,
1832 struct perf_sample *sample,
1833 struct perf_tool *tool)
1837 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1838 struct cs_etm_auxtrace,
1844 if (!tool->ordered_events) {
1845 pr_err("CoreSight ETM Trace requires ordered events\n");
1849 if (!etm->timeless_decoding)
1852 if (sample->time && (sample->time != (u64) -1))
1853 timestamp = sample->time;
1857 if (timestamp || etm->timeless_decoding) {
1858 err = cs_etm__update_queues(etm);
1863 if (event->header.type == PERF_RECORD_EXIT)
1864 return cs_etm__process_timeless_queues(etm,
1867 if (event->header.type == PERF_RECORD_ITRACE_START)
1868 return cs_etm__process_itrace_start(etm, event);
1869 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
1870 return cs_etm__process_switch_cpu_wide(etm, event);
1875 static int cs_etm__process_auxtrace_event(struct perf_session *session,
1876 union perf_event *event,
1877 struct perf_tool *tool __maybe_unused)
1879 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1880 struct cs_etm_auxtrace,
1882 if (!etm->data_queued) {
1883 struct auxtrace_buffer *buffer;
1885 int fd = perf_data__fd(session->data);
1886 bool is_pipe = perf_data__is_pipe(session->data);
1892 data_offset = lseek(fd, 0, SEEK_CUR);
1893 if (data_offset == -1)
1897 err = auxtrace_queues__add_event(&etm->queues, session,
1898 event, data_offset, &buffer);
1903 if (auxtrace_buffer__get_data(buffer, fd)) {
1904 cs_etm__dump_event(etm, buffer);
1905 auxtrace_buffer__put_data(buffer);
1912 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
1914 struct perf_evsel *evsel;
1915 struct perf_evlist *evlist = etm->session->evlist;
1916 bool timeless_decoding = true;
1919 * Circle through the list of event and complain if we find one
1920 * with the time bit set.
1922 evlist__for_each_entry(evlist, evsel) {
1923 if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
1924 timeless_decoding = false;
1927 return timeless_decoding;
1930 static const char * const cs_etm_global_header_fmts[] = {
1931 [CS_HEADER_VERSION_0] = " Header version %llx\n",
1932 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
1933 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
1936 static const char * const cs_etm_priv_fmts[] = {
1937 [CS_ETM_MAGIC] = " Magic number %llx\n",
1938 [CS_ETM_CPU] = " CPU %lld\n",
1939 [CS_ETM_ETMCR] = " ETMCR %llx\n",
1940 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
1941 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
1942 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
1945 static const char * const cs_etmv4_priv_fmts[] = {
1946 [CS_ETM_MAGIC] = " Magic number %llx\n",
1947 [CS_ETM_CPU] = " CPU %lld\n",
1948 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
1949 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
1950 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
1951 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
1952 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
1953 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
1954 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
1957 static void cs_etm__print_auxtrace_info(u64 *val, int num)
1961 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
1962 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
1964 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
1965 if (val[i] == __perf_cs_etmv3_magic)
1966 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
1967 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
1968 else if (val[i] == __perf_cs_etmv4_magic)
1969 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
1970 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
1972 /* failure.. return */
1977 int cs_etm__process_auxtrace_info(union perf_event *event,
1978 struct perf_session *session)
1980 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
1981 struct cs_etm_auxtrace *etm = NULL;
1982 struct int_node *inode;
1983 unsigned int pmu_type;
1984 int event_header_size = sizeof(struct perf_event_header);
1985 int info_header_size;
1986 int total_size = auxtrace_info->header.size;
1989 int err = 0, idx = -1;
1991 u64 *ptr, *hdr = NULL;
1992 u64 **metadata = NULL;
1995 * sizeof(auxtrace_info_event::type) +
1996 * sizeof(auxtrace_info_event::reserved) == 8
1998 info_header_size = 8;
2000 if (total_size < (event_header_size + info_header_size))
2003 priv_size = total_size - event_header_size - info_header_size;
2005 /* First the global part */
2006 ptr = (u64 *) auxtrace_info->priv;
2008 /* Look for version '0' of the header */
2012 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
2016 /* Extract header information - see cs-etm.h for format */
2017 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2019 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2020 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2024 * Create an RB tree for traceID-metadata tuple. Since the conversion
2025 * has to be made for each packet that gets decoded, optimizing access
2026 * in anything other than a sequential array is worth doing.
2028 traceid_list = intlist__new(NULL);
2029 if (!traceid_list) {
2034 metadata = zalloc(sizeof(*metadata) * num_cpu);
2037 goto err_free_traceid_list;
2041 * The metadata is stored in the auxtrace_info section and encodes
2042 * the configuration of the ARM embedded trace macrocell which is
2043 * required by the trace decoder to properly decode the trace due
2044 * to its highly compressed nature.
2046 for (j = 0; j < num_cpu; j++) {
2047 if (ptr[i] == __perf_cs_etmv3_magic) {
2048 metadata[j] = zalloc(sizeof(*metadata[j]) *
2052 goto err_free_metadata;
2054 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
2055 metadata[j][k] = ptr[i + k];
2057 /* The traceID is our handle */
2058 idx = metadata[j][CS_ETM_ETMTRACEIDR];
2059 i += CS_ETM_PRIV_MAX;
2060 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2061 metadata[j] = zalloc(sizeof(*metadata[j]) *
2065 goto err_free_metadata;
2067 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
2068 metadata[j][k] = ptr[i + k];
2070 /* The traceID is our handle */
2071 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
2072 i += CS_ETMV4_PRIV_MAX;
2075 /* Get an RB node for this CPU */
2076 inode = intlist__findnew(traceid_list, idx);
2078 /* Something went wrong, no need to continue */
2080 err = PTR_ERR(inode);
2081 goto err_free_metadata;
2085 * The node for that CPU should not be taken.
2086 * Back out if that's the case.
2090 goto err_free_metadata;
2092 /* All good, associate the traceID with the metadata pointer */
2093 inode->priv = metadata[j];
2097 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2098 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2099 * global metadata, and each cpu's metadata respectively.
2100 * The following tests if the correct number of double words was
2101 * present in the auxtrace info section.
2103 if (i * 8 != priv_size) {
2105 goto err_free_metadata;
2108 etm = zalloc(sizeof(*etm));
2112 goto err_free_metadata;
2115 err = auxtrace_queues__init(&etm->queues);
2119 etm->session = session;
2120 etm->machine = &session->machines.host;
2122 etm->num_cpu = num_cpu;
2123 etm->pmu_type = pmu_type;
2124 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
2125 etm->metadata = metadata;
2126 etm->auxtrace_type = auxtrace_info->type;
2127 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
2129 etm->auxtrace.process_event = cs_etm__process_event;
2130 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
2131 etm->auxtrace.flush_events = cs_etm__flush_events;
2132 etm->auxtrace.free_events = cs_etm__free_events;
2133 etm->auxtrace.free = cs_etm__free;
2134 session->auxtrace = &etm->auxtrace;
2136 etm->unknown_thread = thread__new(999999999, 999999999);
2137 if (!etm->unknown_thread)
2138 goto err_free_queues;
2141 * Initialize list node so that at thread__zput() we can avoid
2142 * segmentation fault at list_del_init().
2144 INIT_LIST_HEAD(&etm->unknown_thread->node);
2146 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
2148 goto err_delete_thread;
2150 if (thread__init_map_groups(etm->unknown_thread, etm->machine))
2151 goto err_delete_thread;
2154 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
2158 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2159 etm->synth_opts = *session->itrace_synth_opts;
2161 itrace_synth_opts__set_default(&etm->synth_opts,
2162 session->itrace_synth_opts->default_no_sample);
2163 etm->synth_opts.callchain = false;
2166 err = cs_etm__synth_events(etm, session);
2168 goto err_delete_thread;
2170 err = auxtrace_queues__process_index(&etm->queues, session);
2172 goto err_delete_thread;
2174 etm->data_queued = etm->queues.populated;
2179 thread__zput(etm->unknown_thread);
2181 auxtrace_queues__free(&etm->queues);
2182 session->auxtrace = NULL;
2186 /* No need to check @metadata[j], free(NULL) is supported */
2187 for (j = 0; j < num_cpu; j++)
2190 err_free_traceid_list:
2191 intlist__delete(traceid_list);