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 {
66 u64 period_instructions;
67 size_t last_branch_pos;
68 union perf_event *event_buf;
69 struct thread *thread;
70 struct branch_stack *last_branch;
71 struct branch_stack *last_branch_rb;
72 struct cs_etm_packet *prev_packet;
73 struct cs_etm_packet *packet;
74 struct cs_etm_packet_queue packet_queue;
78 struct cs_etm_auxtrace *etm;
79 struct cs_etm_decoder *decoder;
80 struct auxtrace_buffer *buffer;
81 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 auxtrace_queue *queue;
163 struct cs_etm_auxtrace *etm = etmq->etm;
165 cs_etm__clear_packet_queue(&tidq->packet_queue);
167 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
168 tidq->tid = queue->tid;
170 tidq->trace_chan_id = trace_chan_id;
172 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
176 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
177 if (!tidq->prev_packet)
180 if (etm->synth_opts.last_branch) {
181 size_t sz = sizeof(struct branch_stack);
183 sz += etm->synth_opts.last_branch_sz *
184 sizeof(struct branch_entry);
185 tidq->last_branch = zalloc(sz);
186 if (!tidq->last_branch)
188 tidq->last_branch_rb = zalloc(sz);
189 if (!tidq->last_branch_rb)
193 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
194 if (!tidq->event_buf)
200 zfree(&tidq->last_branch_rb);
201 zfree(&tidq->last_branch);
202 zfree(&tidq->prev_packet);
203 zfree(&tidq->packet);
208 static struct cs_etm_traceid_queue
209 *cs_etm__etmq_get_traceid_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
211 struct cs_etm_traceid_queue *tidq;
212 struct cs_etm_auxtrace *etm = etmq->etm;
214 if (!etm->timeless_decoding)
217 tidq = etmq->traceid_queues;
222 tidq = malloc(sizeof(*tidq));
226 memset(tidq, 0, sizeof(*tidq));
228 if (cs_etm__init_traceid_queue(etmq, tidq, trace_chan_id))
231 etmq->traceid_queues = tidq;
233 return etmq->traceid_queues;
241 struct cs_etm_packet_queue
242 *cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
244 struct cs_etm_traceid_queue *tidq;
246 tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
248 return &tidq->packet_queue;
253 static void cs_etm__packet_dump(const char *pkt_string)
255 const char *color = PERF_COLOR_BLUE;
256 int len = strlen(pkt_string);
258 if (len && (pkt_string[len-1] == '\n'))
259 color_fprintf(stdout, color, " %s", pkt_string);
261 color_fprintf(stdout, color, " %s\n", pkt_string);
266 static void cs_etm__set_trace_param_etmv3(struct cs_etm_trace_params *t_params,
267 struct cs_etm_auxtrace *etm, int idx,
270 u64 **metadata = etm->metadata;
272 t_params[idx].protocol = cs_etm__get_v7_protocol_version(etmidr);
273 t_params[idx].etmv3.reg_ctrl = metadata[idx][CS_ETM_ETMCR];
274 t_params[idx].etmv3.reg_trc_id = metadata[idx][CS_ETM_ETMTRACEIDR];
277 static void cs_etm__set_trace_param_etmv4(struct cs_etm_trace_params *t_params,
278 struct cs_etm_auxtrace *etm, int idx)
280 u64 **metadata = etm->metadata;
282 t_params[idx].protocol = CS_ETM_PROTO_ETMV4i;
283 t_params[idx].etmv4.reg_idr0 = metadata[idx][CS_ETMV4_TRCIDR0];
284 t_params[idx].etmv4.reg_idr1 = metadata[idx][CS_ETMV4_TRCIDR1];
285 t_params[idx].etmv4.reg_idr2 = metadata[idx][CS_ETMV4_TRCIDR2];
286 t_params[idx].etmv4.reg_idr8 = metadata[idx][CS_ETMV4_TRCIDR8];
287 t_params[idx].etmv4.reg_configr = metadata[idx][CS_ETMV4_TRCCONFIGR];
288 t_params[idx].etmv4.reg_traceidr = metadata[idx][CS_ETMV4_TRCTRACEIDR];
291 static int cs_etm__init_trace_params(struct cs_etm_trace_params *t_params,
292 struct cs_etm_auxtrace *etm)
298 for (i = 0; i < etm->num_cpu; i++) {
299 architecture = etm->metadata[i][CS_ETM_MAGIC];
301 switch (architecture) {
302 case __perf_cs_etmv3_magic:
303 etmidr = etm->metadata[i][CS_ETM_ETMIDR];
304 cs_etm__set_trace_param_etmv3(t_params, etm, i, etmidr);
306 case __perf_cs_etmv4_magic:
307 cs_etm__set_trace_param_etmv4(t_params, etm, i);
317 static int cs_etm__init_decoder_params(struct cs_etm_decoder_params *d_params,
318 struct cs_etm_queue *etmq,
319 enum cs_etm_decoder_operation mode)
323 if (!(mode < CS_ETM_OPERATION_MAX))
326 d_params->packet_printer = cs_etm__packet_dump;
327 d_params->operation = mode;
328 d_params->data = etmq;
329 d_params->formatted = true;
330 d_params->fsyncs = false;
331 d_params->hsyncs = false;
332 d_params->frame_aligned = true;
339 static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
340 struct auxtrace_buffer *buffer)
343 const char *color = PERF_COLOR_BLUE;
344 struct cs_etm_decoder_params d_params;
345 struct cs_etm_trace_params *t_params;
346 struct cs_etm_decoder *decoder;
347 size_t buffer_used = 0;
349 fprintf(stdout, "\n");
350 color_fprintf(stdout, color,
351 ". ... CoreSight ETM Trace data: size %zu bytes\n",
354 /* Use metadata to fill in trace parameters for trace decoder */
355 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
360 if (cs_etm__init_trace_params(t_params, etm))
363 /* Set decoder parameters to simply print the trace packets */
364 if (cs_etm__init_decoder_params(&d_params, NULL,
365 CS_ETM_OPERATION_PRINT))
368 decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
375 ret = cs_etm_decoder__process_data_block(
376 decoder, buffer->offset,
377 &((u8 *)buffer->data)[buffer_used],
378 buffer->size - buffer_used, &consumed);
382 buffer_used += consumed;
383 } while (buffer_used < buffer->size);
385 cs_etm_decoder__free(decoder);
391 static int cs_etm__flush_events(struct perf_session *session,
392 struct perf_tool *tool)
395 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
396 struct cs_etm_auxtrace,
401 if (!tool->ordered_events)
404 if (!etm->timeless_decoding)
407 ret = cs_etm__update_queues(etm);
412 return cs_etm__process_timeless_queues(etm, -1);
415 static void cs_etm__free_queue(void *priv)
417 struct cs_etm_queue *etmq = priv;
422 thread__zput(etmq->traceid_queues->thread);
423 cs_etm_decoder__free(etmq->decoder);
424 zfree(&etmq->traceid_queues->event_buf);
425 zfree(&etmq->traceid_queues->last_branch);
426 zfree(&etmq->traceid_queues->last_branch_rb);
427 zfree(&etmq->traceid_queues->prev_packet);
428 zfree(&etmq->traceid_queues->packet);
429 zfree(&etmq->traceid_queues);
433 static void cs_etm__free_events(struct perf_session *session)
436 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
437 struct cs_etm_auxtrace,
439 struct auxtrace_queues *queues = &aux->queues;
441 for (i = 0; i < queues->nr_queues; i++) {
442 cs_etm__free_queue(queues->queue_array[i].priv);
443 queues->queue_array[i].priv = NULL;
446 auxtrace_queues__free(queues);
449 static void cs_etm__free(struct perf_session *session)
452 struct int_node *inode, *tmp;
453 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
454 struct cs_etm_auxtrace,
456 cs_etm__free_events(session);
457 session->auxtrace = NULL;
459 /* First remove all traceID/metadata nodes for the RB tree */
460 intlist__for_each_entry_safe(inode, tmp, traceid_list)
461 intlist__remove(traceid_list, inode);
462 /* Then the RB tree itself */
463 intlist__delete(traceid_list);
465 for (i = 0; i < aux->num_cpu; i++)
466 zfree(&aux->metadata[i]);
468 thread__zput(aux->unknown_thread);
469 zfree(&aux->metadata);
473 static u8 cs_etm__cpu_mode(struct cs_etm_queue *etmq, u64 address)
475 struct machine *machine;
477 machine = etmq->etm->machine;
479 if (address >= etmq->etm->kernel_start) {
480 if (machine__is_host(machine))
481 return PERF_RECORD_MISC_KERNEL;
483 return PERF_RECORD_MISC_GUEST_KERNEL;
485 if (machine__is_host(machine))
486 return PERF_RECORD_MISC_USER;
488 return PERF_RECORD_MISC_GUEST_USER;
490 return PERF_RECORD_MISC_HYPERVISOR;
494 static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
495 u64 address, size_t size, u8 *buffer)
500 struct thread *thread;
501 struct machine *machine;
502 struct addr_location al;
509 machine = etmq->etm->machine;
510 cpumode = cs_etm__cpu_mode(etmq, address);
512 thread = etmq->traceid_queues->thread;
514 if (cpumode != PERF_RECORD_MISC_KERNEL)
516 thread = etmq->etm->unknown_thread;
519 if (!thread__find_map(thread, cpumode, address, &al) || !al.map->dso)
522 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
523 dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
526 offset = al.map->map_ip(al.map, address);
530 len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
538 static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm)
540 struct cs_etm_decoder_params d_params;
541 struct cs_etm_trace_params *t_params = NULL;
542 struct cs_etm_queue *etmq;
544 etmq = zalloc(sizeof(*etmq));
548 /* Use metadata to fill in trace parameters for trace decoder */
549 t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
554 if (cs_etm__init_trace_params(t_params, etm))
557 /* Set decoder parameters to decode trace packets */
558 if (cs_etm__init_decoder_params(&d_params, etmq,
559 CS_ETM_OPERATION_DECODE))
562 etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
568 * Register a function to handle all memory accesses required by
569 * the trace decoder library.
571 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
574 goto out_free_decoder;
580 cs_etm_decoder__free(etmq->decoder);
587 static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
588 struct auxtrace_queue *queue,
589 unsigned int queue_nr)
592 struct cs_etm_queue *etmq = queue->priv;
594 if (list_empty(&queue->head) || etmq)
597 etmq = cs_etm__alloc_queue(etm);
606 etmq->queue_nr = queue_nr;
613 static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
618 if (!etm->kernel_start)
619 etm->kernel_start = machine__kernel_start(etm->machine);
621 for (i = 0; i < etm->queues.nr_queues; i++) {
622 ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
630 static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
632 if (etm->queues.new_data) {
633 etm->queues.new_data = false;
634 return cs_etm__setup_queues(etm);
641 void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq,
642 struct cs_etm_traceid_queue *tidq)
644 struct branch_stack *bs_src = tidq->last_branch_rb;
645 struct branch_stack *bs_dst = tidq->last_branch;
649 * Set the number of records before early exit: ->nr is used to
650 * determine how many branches to copy from ->entries.
652 bs_dst->nr = bs_src->nr;
655 * Early exit when there is nothing to copy.
661 * As bs_src->entries is a circular buffer, we need to copy from it in
662 * two steps. First, copy the branches from the most recently inserted
663 * branch ->last_branch_pos until the end of bs_src->entries buffer.
665 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
666 memcpy(&bs_dst->entries[0],
667 &bs_src->entries[tidq->last_branch_pos],
668 sizeof(struct branch_entry) * nr);
671 * If we wrapped around at least once, the branches from the beginning
672 * of the bs_src->entries buffer and until the ->last_branch_pos element
673 * are older valid branches: copy them over. The total number of
674 * branches copied over will be equal to the number of branches asked by
675 * the user in last_branch_sz.
677 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
678 memcpy(&bs_dst->entries[nr],
680 sizeof(struct branch_entry) * tidq->last_branch_pos);
685 void cs_etm__reset_last_branch_rb(struct cs_etm_traceid_queue *tidq)
687 tidq->last_branch_pos = 0;
688 tidq->last_branch_rb->nr = 0;
691 static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
692 u8 trace_chan_id, u64 addr)
696 cs_etm__mem_access(etmq, trace_chan_id, addr,
697 ARRAY_SIZE(instrBytes), instrBytes);
699 * T32 instruction size is indicated by bits[15:11] of the first
700 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
701 * denote a 32-bit instruction.
703 return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
706 static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
708 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
709 if (packet->sample_type == CS_ETM_DISCONTINUITY)
712 return packet->start_addr;
716 u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
718 /* Returns 0 for the CS_ETM_DISCONTINUITY packet */
719 if (packet->sample_type == CS_ETM_DISCONTINUITY)
722 return packet->end_addr - packet->last_instr_size;
725 static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
727 const struct cs_etm_packet *packet,
730 if (packet->isa == CS_ETM_ISA_T32) {
731 u64 addr = packet->start_addr;
734 addr += cs_etm__t32_instr_size(etmq,
735 trace_chan_id, addr);
741 /* Assume a 4 byte instruction size (A32/A64) */
742 return packet->start_addr + offset * 4;
745 static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq,
746 struct cs_etm_traceid_queue *tidq)
748 struct branch_stack *bs = tidq->last_branch_rb;
749 struct branch_entry *be;
752 * The branches are recorded in a circular buffer in reverse
753 * chronological order: we start recording from the last element of the
754 * buffer down. After writing the first element of the stack, move the
755 * insert position back to the end of the buffer.
757 if (!tidq->last_branch_pos)
758 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
760 tidq->last_branch_pos -= 1;
762 be = &bs->entries[tidq->last_branch_pos];
763 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
764 be->to = cs_etm__first_executed_instr(tidq->packet);
765 /* No support for mispredict */
766 be->flags.mispred = 0;
767 be->flags.predicted = 1;
770 * Increment bs->nr until reaching the number of last branches asked by
771 * the user on the command line.
773 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
777 static int cs_etm__inject_event(union perf_event *event,
778 struct perf_sample *sample, u64 type)
780 event->header.size = perf_event__sample_event_size(sample, type, 0);
781 return perf_event__synthesize_sample(event, type, 0, sample);
786 cs_etm__get_trace(struct cs_etm_queue *etmq)
788 struct auxtrace_buffer *aux_buffer = etmq->buffer;
789 struct auxtrace_buffer *old_buffer = aux_buffer;
790 struct auxtrace_queue *queue;
792 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
794 aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
796 /* If no more data, drop the previous auxtrace_buffer and return */
799 auxtrace_buffer__drop_data(old_buffer);
804 etmq->buffer = aux_buffer;
806 /* If the aux_buffer doesn't have data associated, try to load it */
807 if (!aux_buffer->data) {
808 /* get the file desc associated with the perf data file */
809 int fd = perf_data__fd(etmq->etm->session->data);
811 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
812 if (!aux_buffer->data)
816 /* If valid, drop the previous buffer */
818 auxtrace_buffer__drop_data(old_buffer);
821 etmq->buf_len = aux_buffer->size;
822 etmq->buf = aux_buffer->data;
824 return etmq->buf_len;
827 static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
828 struct auxtrace_queue *queue,
829 struct cs_etm_traceid_queue *tidq)
831 /* CPU-wide tracing isn't supported yet */
832 if (queue->tid == -1)
835 if ((!tidq->thread) && (tidq->tid != -1))
836 tidq->thread = machine__find_thread(etm->machine, -1,
840 tidq->pid = tidq->thread->pid_;
843 static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
844 struct cs_etm_traceid_queue *tidq,
845 u64 addr, u64 period)
848 struct cs_etm_auxtrace *etm = etmq->etm;
849 union perf_event *event = tidq->event_buf;
850 struct perf_sample sample = {.ip = 0,};
852 event->sample.header.type = PERF_RECORD_SAMPLE;
853 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
854 event->sample.header.size = sizeof(struct perf_event_header);
857 sample.pid = tidq->pid;
858 sample.tid = tidq->tid;
859 sample.id = etmq->etm->instructions_id;
860 sample.stream_id = etmq->etm->instructions_id;
861 sample.period = period;
862 sample.cpu = tidq->packet->cpu;
863 sample.flags = tidq->prev_packet->flags;
865 sample.cpumode = event->sample.header.misc;
867 if (etm->synth_opts.last_branch) {
868 cs_etm__copy_last_branch_rb(etmq, tidq);
869 sample.branch_stack = tidq->last_branch;
872 if (etm->synth_opts.inject) {
873 ret = cs_etm__inject_event(event, &sample,
874 etm->instructions_sample_type);
879 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
883 "CS ETM Trace: failed to deliver instruction event, error %d\n",
886 if (etm->synth_opts.last_branch)
887 cs_etm__reset_last_branch_rb(tidq);
893 * The cs etm packet encodes an instruction range between a branch target
894 * and the next taken branch. Generate sample accordingly.
896 static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
897 struct cs_etm_traceid_queue *tidq)
900 struct cs_etm_auxtrace *etm = etmq->etm;
901 struct perf_sample sample = {.ip = 0,};
902 union perf_event *event = tidq->event_buf;
903 struct dummy_branch_stack {
905 struct branch_entry entries;
909 ip = cs_etm__last_executed_instr(tidq->prev_packet);
911 event->sample.header.type = PERF_RECORD_SAMPLE;
912 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
913 event->sample.header.size = sizeof(struct perf_event_header);
916 sample.pid = tidq->pid;
917 sample.tid = tidq->tid;
918 sample.addr = cs_etm__first_executed_instr(tidq->packet);
919 sample.id = etmq->etm->branches_id;
920 sample.stream_id = etmq->etm->branches_id;
922 sample.cpu = tidq->packet->cpu;
923 sample.flags = tidq->prev_packet->flags;
924 sample.cpumode = event->sample.header.misc;
927 * perf report cannot handle events without a branch stack
929 if (etm->synth_opts.last_branch) {
930 dummy_bs = (struct dummy_branch_stack){
937 sample.branch_stack = (struct branch_stack *)&dummy_bs;
940 if (etm->synth_opts.inject) {
941 ret = cs_etm__inject_event(event, &sample,
942 etm->branches_sample_type);
947 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
951 "CS ETM Trace: failed to deliver instruction event, error %d\n",
957 struct cs_etm_synth {
958 struct perf_tool dummy_tool;
959 struct perf_session *session;
962 static int cs_etm__event_synth(struct perf_tool *tool,
963 union perf_event *event,
964 struct perf_sample *sample __maybe_unused,
965 struct machine *machine __maybe_unused)
967 struct cs_etm_synth *cs_etm_synth =
968 container_of(tool, struct cs_etm_synth, dummy_tool);
970 return perf_session__deliver_synth_event(cs_etm_synth->session,
974 static int cs_etm__synth_event(struct perf_session *session,
975 struct perf_event_attr *attr, u64 id)
977 struct cs_etm_synth cs_etm_synth;
979 memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
980 cs_etm_synth.session = session;
982 return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
983 &id, cs_etm__event_synth);
986 static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
987 struct perf_session *session)
989 struct perf_evlist *evlist = session->evlist;
990 struct perf_evsel *evsel;
991 struct perf_event_attr attr;
996 evlist__for_each_entry(evlist, evsel) {
997 if (evsel->attr.type == etm->pmu_type) {
1004 pr_debug("No selected events with CoreSight Trace data\n");
1008 memset(&attr, 0, sizeof(struct perf_event_attr));
1009 attr.size = sizeof(struct perf_event_attr);
1010 attr.type = PERF_TYPE_HARDWARE;
1011 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
1012 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1014 if (etm->timeless_decoding)
1015 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1017 attr.sample_type |= PERF_SAMPLE_TIME;
1019 attr.exclude_user = evsel->attr.exclude_user;
1020 attr.exclude_kernel = evsel->attr.exclude_kernel;
1021 attr.exclude_hv = evsel->attr.exclude_hv;
1022 attr.exclude_host = evsel->attr.exclude_host;
1023 attr.exclude_guest = evsel->attr.exclude_guest;
1024 attr.sample_id_all = evsel->attr.sample_id_all;
1025 attr.read_format = evsel->attr.read_format;
1027 /* create new id val to be a fixed offset from evsel id */
1028 id = evsel->id[0] + 1000000000;
1033 if (etm->synth_opts.branches) {
1034 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
1035 attr.sample_period = 1;
1036 attr.sample_type |= PERF_SAMPLE_ADDR;
1037 err = cs_etm__synth_event(session, &attr, id);
1040 etm->sample_branches = true;
1041 etm->branches_sample_type = attr.sample_type;
1042 etm->branches_id = id;
1044 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
1047 if (etm->synth_opts.last_branch)
1048 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1050 if (etm->synth_opts.instructions) {
1051 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1052 attr.sample_period = etm->synth_opts.period;
1053 etm->instructions_sample_period = attr.sample_period;
1054 err = cs_etm__synth_event(session, &attr, id);
1057 etm->sample_instructions = true;
1058 etm->instructions_sample_type = attr.sample_type;
1059 etm->instructions_id = id;
1066 static int cs_etm__sample(struct cs_etm_queue *etmq,
1067 struct cs_etm_traceid_queue *tidq)
1069 struct cs_etm_auxtrace *etm = etmq->etm;
1070 struct cs_etm_packet *tmp;
1072 u8 trace_chan_id = tidq->trace_chan_id;
1073 u64 instrs_executed = tidq->packet->instr_count;
1075 tidq->period_instructions += instrs_executed;
1078 * Record a branch when the last instruction in
1079 * PREV_PACKET is a branch.
1081 if (etm->synth_opts.last_branch &&
1082 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1083 tidq->prev_packet->last_instr_taken_branch)
1084 cs_etm__update_last_branch_rb(etmq, tidq);
1086 if (etm->sample_instructions &&
1087 tidq->period_instructions >= etm->instructions_sample_period) {
1089 * Emit instruction sample periodically
1090 * TODO: allow period to be defined in cycles and clock time
1093 /* Get number of instructions executed after the sample point */
1094 u64 instrs_over = tidq->period_instructions -
1095 etm->instructions_sample_period;
1098 * Calculate the address of the sampled instruction (-1 as
1099 * sample is reported as though instruction has just been
1100 * executed, but PC has not advanced to next instruction)
1102 u64 offset = (instrs_executed - instrs_over - 1);
1103 u64 addr = cs_etm__instr_addr(etmq, trace_chan_id,
1104 tidq->packet, offset);
1106 ret = cs_etm__synth_instruction_sample(
1107 etmq, tidq, addr, etm->instructions_sample_period);
1111 /* Carry remaining instructions into next sample period */
1112 tidq->period_instructions = instrs_over;
1115 if (etm->sample_branches) {
1116 bool generate_sample = false;
1118 /* Generate sample for tracing on packet */
1119 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1120 generate_sample = true;
1122 /* Generate sample for branch taken packet */
1123 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1124 tidq->prev_packet->last_instr_taken_branch)
1125 generate_sample = true;
1127 if (generate_sample) {
1128 ret = cs_etm__synth_branch_sample(etmq, tidq);
1134 if (etm->sample_branches || etm->synth_opts.last_branch) {
1136 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1137 * the next incoming packet.
1140 tidq->packet = tidq->prev_packet;
1141 tidq->prev_packet = tmp;
1147 static int cs_etm__exception(struct cs_etm_traceid_queue *tidq)
1150 * When the exception packet is inserted, whether the last instruction
1151 * in previous range packet is taken branch or not, we need to force
1152 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1153 * to generate branch sample for the instruction range before the
1154 * exception is trapped to kernel or before the exception returning.
1156 * The exception packet includes the dummy address values, so don't
1157 * swap PACKET with PREV_PACKET. This keeps PREV_PACKET to be useful
1158 * for generating instruction and branch samples.
1160 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1161 tidq->prev_packet->last_instr_taken_branch = true;
1166 static int cs_etm__flush(struct cs_etm_queue *etmq,
1167 struct cs_etm_traceid_queue *tidq)
1170 struct cs_etm_auxtrace *etm = etmq->etm;
1171 struct cs_etm_packet *tmp;
1173 /* Handle start tracing packet */
1174 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1177 if (etmq->etm->synth_opts.last_branch &&
1178 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1180 * Generate a last branch event for the branches left in the
1181 * circular buffer at the end of the trace.
1183 * Use the address of the end of the last reported execution
1186 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1188 err = cs_etm__synth_instruction_sample(
1190 tidq->period_instructions);
1194 tidq->period_instructions = 0;
1198 if (etm->sample_branches &&
1199 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1200 err = cs_etm__synth_branch_sample(etmq, tidq);
1206 if (etm->sample_branches || etm->synth_opts.last_branch) {
1208 * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
1209 * the next incoming packet.
1212 tidq->packet = tidq->prev_packet;
1213 tidq->prev_packet = tmp;
1219 static int cs_etm__end_block(struct cs_etm_queue *etmq,
1220 struct cs_etm_traceid_queue *tidq)
1225 * It has no new packet coming and 'etmq->packet' contains the stale
1226 * packet which was set at the previous time with packets swapping;
1227 * so skip to generate branch sample to avoid stale packet.
1229 * For this case only flush branch stack and generate a last branch
1230 * event for the branches left in the circular buffer at the end of
1233 if (etmq->etm->synth_opts.last_branch &&
1234 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1236 * Use the address of the end of the last reported execution
1239 u64 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1241 err = cs_etm__synth_instruction_sample(
1243 tidq->period_instructions);
1247 tidq->period_instructions = 0;
1253 * cs_etm__get_data_block: Fetch a block from the auxtrace_buffer queue
1255 * Returns: < 0 if error
1256 * = 0 if no more auxtrace_buffer to read
1257 * > 0 if the current buffer isn't empty yet
1259 static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
1263 if (!etmq->buf_len) {
1264 ret = cs_etm__get_trace(etmq);
1268 * We cannot assume consecutive blocks in the data file
1269 * are contiguous, reset the decoder to force re-sync.
1271 ret = cs_etm_decoder__reset(etmq->decoder);
1276 return etmq->buf_len;
1279 static bool cs_etm__is_svc_instr(struct cs_etm_queue *etmq, u8 trace_chan_id,
1280 struct cs_etm_packet *packet,
1287 switch (packet->isa) {
1288 case CS_ETM_ISA_T32:
1290 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
1293 * +-----------------+--------+
1294 * | 1 1 0 1 1 1 1 1 | imm8 |
1295 * +-----------------+--------+
1297 * According to the specifiction, it only defines SVC for T32
1298 * with 16 bits instruction and has no definition for 32bits;
1299 * so below only read 2 bytes as instruction size for T32.
1301 addr = end_addr - 2;
1302 cs_etm__mem_access(etmq, trace_chan_id, addr,
1303 sizeof(instr16), (u8 *)&instr16);
1304 if ((instr16 & 0xFF00) == 0xDF00)
1308 case CS_ETM_ISA_A32:
1310 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
1312 * b'31 b'28 b'27 b'24
1313 * +---------+---------+-------------------------+
1314 * | !1111 | 1 1 1 1 | imm24 |
1315 * +---------+---------+-------------------------+
1317 addr = end_addr - 4;
1318 cs_etm__mem_access(etmq, trace_chan_id, addr,
1319 sizeof(instr32), (u8 *)&instr32);
1320 if ((instr32 & 0x0F000000) == 0x0F000000 &&
1321 (instr32 & 0xF0000000) != 0xF0000000)
1325 case CS_ETM_ISA_A64:
1327 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
1330 * +-----------------------+---------+-----------+
1331 * | 1 1 0 1 0 1 0 0 0 0 0 | imm16 | 0 0 0 0 1 |
1332 * +-----------------------+---------+-----------+
1334 addr = end_addr - 4;
1335 cs_etm__mem_access(etmq, trace_chan_id, addr,
1336 sizeof(instr32), (u8 *)&instr32);
1337 if ((instr32 & 0xFFE0001F) == 0xd4000001)
1341 case CS_ETM_ISA_UNKNOWN:
1349 static bool cs_etm__is_syscall(struct cs_etm_queue *etmq,
1350 struct cs_etm_traceid_queue *tidq, u64 magic)
1352 u8 trace_chan_id = tidq->trace_chan_id;
1353 struct cs_etm_packet *packet = tidq->packet;
1354 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1356 if (magic == __perf_cs_etmv3_magic)
1357 if (packet->exception_number == CS_ETMV3_EXC_SVC)
1361 * ETMv4 exception type CS_ETMV4_EXC_CALL covers SVC, SMC and
1362 * HVC cases; need to check if it's SVC instruction based on
1365 if (magic == __perf_cs_etmv4_magic) {
1366 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1367 cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1368 prev_packet->end_addr))
1375 static bool cs_etm__is_async_exception(struct cs_etm_traceid_queue *tidq,
1378 struct cs_etm_packet *packet = tidq->packet;
1380 if (magic == __perf_cs_etmv3_magic)
1381 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
1382 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
1383 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
1384 packet->exception_number == CS_ETMV3_EXC_IRQ ||
1385 packet->exception_number == CS_ETMV3_EXC_FIQ)
1388 if (magic == __perf_cs_etmv4_magic)
1389 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
1390 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
1391 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
1392 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
1393 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
1394 packet->exception_number == CS_ETMV4_EXC_IRQ ||
1395 packet->exception_number == CS_ETMV4_EXC_FIQ)
1401 static bool cs_etm__is_sync_exception(struct cs_etm_queue *etmq,
1402 struct cs_etm_traceid_queue *tidq,
1405 u8 trace_chan_id = tidq->trace_chan_id;
1406 struct cs_etm_packet *packet = tidq->packet;
1407 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1409 if (magic == __perf_cs_etmv3_magic)
1410 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
1411 packet->exception_number == CS_ETMV3_EXC_HYP ||
1412 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
1413 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
1414 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
1415 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
1416 packet->exception_number == CS_ETMV3_EXC_GENERIC)
1419 if (magic == __perf_cs_etmv4_magic) {
1420 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
1421 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
1422 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
1423 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
1427 * For CS_ETMV4_EXC_CALL, except SVC other instructions
1428 * (SMC, HVC) are taken as sync exceptions.
1430 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
1431 !cs_etm__is_svc_instr(etmq, trace_chan_id, prev_packet,
1432 prev_packet->end_addr))
1436 * ETMv4 has 5 bits for exception number; if the numbers
1437 * are in the range ( CS_ETMV4_EXC_FIQ, CS_ETMV4_EXC_END ]
1438 * they are implementation defined exceptions.
1440 * For this case, simply take it as sync exception.
1442 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
1443 packet->exception_number <= CS_ETMV4_EXC_END)
1450 static int cs_etm__set_sample_flags(struct cs_etm_queue *etmq,
1451 struct cs_etm_traceid_queue *tidq)
1453 struct cs_etm_packet *packet = tidq->packet;
1454 struct cs_etm_packet *prev_packet = tidq->prev_packet;
1455 u8 trace_chan_id = tidq->trace_chan_id;
1459 switch (packet->sample_type) {
1462 * Immediate branch instruction without neither link nor
1463 * return flag, it's normal branch instruction within
1466 if (packet->last_instr_type == OCSD_INSTR_BR &&
1467 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
1468 packet->flags = PERF_IP_FLAG_BRANCH;
1470 if (packet->last_instr_cond)
1471 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
1475 * Immediate branch instruction with link (e.g. BL), this is
1476 * branch instruction for function call.
1478 if (packet->last_instr_type == OCSD_INSTR_BR &&
1479 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1480 packet->flags = PERF_IP_FLAG_BRANCH |
1484 * Indirect branch instruction with link (e.g. BLR), this is
1485 * branch instruction for function call.
1487 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1488 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
1489 packet->flags = PERF_IP_FLAG_BRANCH |
1493 * Indirect branch instruction with subtype of
1494 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
1495 * function return for A32/T32.
1497 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1498 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
1499 packet->flags = PERF_IP_FLAG_BRANCH |
1500 PERF_IP_FLAG_RETURN;
1503 * Indirect branch instruction without link (e.g. BR), usually
1504 * this is used for function return, especially for functions
1505 * within dynamic link lib.
1507 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1508 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
1509 packet->flags = PERF_IP_FLAG_BRANCH |
1510 PERF_IP_FLAG_RETURN;
1512 /* Return instruction for function return. */
1513 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
1514 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
1515 packet->flags = PERF_IP_FLAG_BRANCH |
1516 PERF_IP_FLAG_RETURN;
1519 * Decoder might insert a discontinuity in the middle of
1520 * instruction packets, fixup prev_packet with flag
1521 * PERF_IP_FLAG_TRACE_BEGIN to indicate restarting trace.
1523 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1524 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1525 PERF_IP_FLAG_TRACE_BEGIN;
1528 * If the previous packet is an exception return packet
1529 * and the return address just follows SVC instuction,
1530 * it needs to calibrate the previous packet sample flags
1531 * as PERF_IP_FLAG_SYSCALLRET.
1533 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
1534 PERF_IP_FLAG_RETURN |
1535 PERF_IP_FLAG_INTERRUPT) &&
1536 cs_etm__is_svc_instr(etmq, trace_chan_id,
1537 packet, packet->start_addr))
1538 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1539 PERF_IP_FLAG_RETURN |
1540 PERF_IP_FLAG_SYSCALLRET;
1542 case CS_ETM_DISCONTINUITY:
1544 * The trace is discontinuous, if the previous packet is
1545 * instruction packet, set flag PERF_IP_FLAG_TRACE_END
1546 * for previous packet.
1548 if (prev_packet->sample_type == CS_ETM_RANGE)
1549 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
1550 PERF_IP_FLAG_TRACE_END;
1552 case CS_ETM_EXCEPTION:
1553 ret = cs_etm__get_magic(packet->trace_chan_id, &magic);
1557 /* The exception is for system call. */
1558 if (cs_etm__is_syscall(etmq, tidq, magic))
1559 packet->flags = PERF_IP_FLAG_BRANCH |
1561 PERF_IP_FLAG_SYSCALLRET;
1563 * The exceptions are triggered by external signals from bus,
1564 * interrupt controller, debug module, PE reset or halt.
1566 else if (cs_etm__is_async_exception(tidq, magic))
1567 packet->flags = PERF_IP_FLAG_BRANCH |
1569 PERF_IP_FLAG_ASYNC |
1570 PERF_IP_FLAG_INTERRUPT;
1572 * Otherwise, exception is caused by trap, instruction &
1573 * data fault, or alignment errors.
1575 else if (cs_etm__is_sync_exception(etmq, tidq, magic))
1576 packet->flags = PERF_IP_FLAG_BRANCH |
1578 PERF_IP_FLAG_INTERRUPT;
1581 * When the exception packet is inserted, since exception
1582 * packet is not used standalone for generating samples
1583 * and it's affiliation to the previous instruction range
1584 * packet; so set previous range packet flags to tell perf
1585 * it is an exception taken branch.
1587 if (prev_packet->sample_type == CS_ETM_RANGE)
1588 prev_packet->flags = packet->flags;
1590 case CS_ETM_EXCEPTION_RET:
1592 * When the exception return packet is inserted, since
1593 * exception return packet is not used standalone for
1594 * generating samples and it's affiliation to the previous
1595 * instruction range packet; so set previous range packet
1596 * flags to tell perf it is an exception return branch.
1598 * The exception return can be for either system call or
1599 * other exception types; unfortunately the packet doesn't
1600 * contain exception type related info so we cannot decide
1601 * the exception type purely based on exception return packet.
1602 * If we record the exception number from exception packet and
1603 * reuse it for excpetion return packet, this is not reliable
1604 * due the trace can be discontinuity or the interrupt can
1605 * be nested, thus the recorded exception number cannot be
1606 * used for exception return packet for these two cases.
1608 * For exception return packet, we only need to distinguish the
1609 * packet is for system call or for other types. Thus the
1610 * decision can be deferred when receive the next packet which
1611 * contains the return address, based on the return address we
1612 * can read out the previous instruction and check if it's a
1613 * system call instruction and then calibrate the sample flag
1616 if (prev_packet->sample_type == CS_ETM_RANGE)
1617 prev_packet->flags = PERF_IP_FLAG_BRANCH |
1618 PERF_IP_FLAG_RETURN |
1619 PERF_IP_FLAG_INTERRUPT;
1629 static int cs_etm__decode_data_block(struct cs_etm_queue *etmq)
1632 size_t processed = 0;
1635 * Packets are decoded and added to the decoder's packet queue
1636 * until the decoder packet processing callback has requested that
1637 * processing stops or there is nothing left in the buffer. Normal
1638 * operations that stop processing are a timestamp packet or a full
1639 * decoder buffer queue.
1641 ret = cs_etm_decoder__process_data_block(etmq->decoder,
1643 &etmq->buf[etmq->buf_used],
1649 etmq->offset += processed;
1650 etmq->buf_used += processed;
1651 etmq->buf_len -= processed;
1657 static int cs_etm__process_traceid_queue(struct cs_etm_queue *etmq,
1658 struct cs_etm_traceid_queue *tidq)
1661 struct cs_etm_packet_queue *packet_queue;
1663 packet_queue = &tidq->packet_queue;
1665 /* Process each packet in this chunk */
1667 ret = cs_etm_decoder__get_packet(packet_queue,
1671 * Stop processing this chunk on
1672 * end of data or error
1677 * Since packet addresses are swapped in packet
1678 * handling within below switch() statements,
1679 * thus setting sample flags must be called
1680 * prior to switch() statement to use address
1681 * information before packets swapping.
1683 ret = cs_etm__set_sample_flags(etmq, tidq);
1687 switch (tidq->packet->sample_type) {
1690 * If the packet contains an instruction
1691 * range, generate instruction sequence
1694 cs_etm__sample(etmq, tidq);
1696 case CS_ETM_EXCEPTION:
1697 case CS_ETM_EXCEPTION_RET:
1699 * If the exception packet is coming,
1700 * make sure the previous instruction
1701 * range packet to be handled properly.
1703 cs_etm__exception(tidq);
1705 case CS_ETM_DISCONTINUITY:
1707 * Discontinuity in trace, flush
1708 * previous branch stack
1710 cs_etm__flush(etmq, tidq);
1714 * Should not receive empty packet,
1717 pr_err("CS ETM Trace: empty packet\n");
1727 static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
1730 struct cs_etm_traceid_queue *tidq;
1732 tidq = cs_etm__etmq_get_traceid_queue(etmq, CS_ETM_PER_THREAD_TRACEID);
1736 /* Go through each buffer in the queue and decode them one by one */
1738 err = cs_etm__get_data_block(etmq);
1742 /* Run trace decoder until buffer consumed or end of trace */
1744 err = cs_etm__decode_data_block(etmq);
1749 * Process each packet in this chunk, nothing to do if
1750 * an error occurs other than hoping the next one will
1753 err = cs_etm__process_traceid_queue(etmq, tidq);
1755 } while (etmq->buf_len);
1758 /* Flush any remaining branch stack entries */
1759 err = cs_etm__end_block(etmq, tidq);
1765 static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
1769 struct auxtrace_queues *queues = &etm->queues;
1771 for (i = 0; i < queues->nr_queues; i++) {
1772 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
1773 struct cs_etm_queue *etmq = queue->priv;
1774 struct cs_etm_traceid_queue *tidq;
1779 tidq = cs_etm__etmq_get_traceid_queue(etmq,
1780 CS_ETM_PER_THREAD_TRACEID);
1785 if ((tid == -1) || (tidq->tid == tid)) {
1786 cs_etm__set_pid_tid_cpu(etm, queue, tidq);
1787 cs_etm__run_decoder(etmq);
1794 static int cs_etm__process_itrace_start(struct cs_etm_auxtrace *etm,
1795 union perf_event *event)
1799 if (etm->timeless_decoding)
1803 * Add the tid/pid to the log so that we can get a match when
1804 * we get a contextID from the decoder.
1806 th = machine__findnew_thread(etm->machine,
1807 event->itrace_start.pid,
1808 event->itrace_start.tid);
1817 static int cs_etm__process_switch_cpu_wide(struct cs_etm_auxtrace *etm,
1818 union perf_event *event)
1821 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1824 * Context switch in per-thread mode are irrelevant since perf
1825 * will start/stop tracing as the process is scheduled.
1827 if (etm->timeless_decoding)
1831 * SWITCH_IN events carry the next process to be switched out while
1832 * SWITCH_OUT events carry the process to be switched in. As such
1833 * we don't care about IN events.
1839 * Add the tid/pid to the log so that we can get a match when
1840 * we get a contextID from the decoder.
1842 th = machine__findnew_thread(etm->machine,
1843 event->context_switch.next_prev_pid,
1844 event->context_switch.next_prev_tid);
1853 static int cs_etm__process_event(struct perf_session *session,
1854 union perf_event *event,
1855 struct perf_sample *sample,
1856 struct perf_tool *tool)
1860 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1861 struct cs_etm_auxtrace,
1867 if (!tool->ordered_events) {
1868 pr_err("CoreSight ETM Trace requires ordered events\n");
1872 if (!etm->timeless_decoding)
1875 if (sample->time && (sample->time != (u64) -1))
1876 timestamp = sample->time;
1880 if (timestamp || etm->timeless_decoding) {
1881 err = cs_etm__update_queues(etm);
1886 if (event->header.type == PERF_RECORD_EXIT)
1887 return cs_etm__process_timeless_queues(etm,
1890 if (event->header.type == PERF_RECORD_ITRACE_START)
1891 return cs_etm__process_itrace_start(etm, event);
1892 else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
1893 return cs_etm__process_switch_cpu_wide(etm, event);
1898 static int cs_etm__process_auxtrace_event(struct perf_session *session,
1899 union perf_event *event,
1900 struct perf_tool *tool __maybe_unused)
1902 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
1903 struct cs_etm_auxtrace,
1905 if (!etm->data_queued) {
1906 struct auxtrace_buffer *buffer;
1908 int fd = perf_data__fd(session->data);
1909 bool is_pipe = perf_data__is_pipe(session->data);
1915 data_offset = lseek(fd, 0, SEEK_CUR);
1916 if (data_offset == -1)
1920 err = auxtrace_queues__add_event(&etm->queues, session,
1921 event, data_offset, &buffer);
1926 if (auxtrace_buffer__get_data(buffer, fd)) {
1927 cs_etm__dump_event(etm, buffer);
1928 auxtrace_buffer__put_data(buffer);
1935 static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
1937 struct perf_evsel *evsel;
1938 struct perf_evlist *evlist = etm->session->evlist;
1939 bool timeless_decoding = true;
1942 * Circle through the list of event and complain if we find one
1943 * with the time bit set.
1945 evlist__for_each_entry(evlist, evsel) {
1946 if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
1947 timeless_decoding = false;
1950 return timeless_decoding;
1953 static const char * const cs_etm_global_header_fmts[] = {
1954 [CS_HEADER_VERSION_0] = " Header version %llx\n",
1955 [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
1956 [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
1959 static const char * const cs_etm_priv_fmts[] = {
1960 [CS_ETM_MAGIC] = " Magic number %llx\n",
1961 [CS_ETM_CPU] = " CPU %lld\n",
1962 [CS_ETM_ETMCR] = " ETMCR %llx\n",
1963 [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
1964 [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
1965 [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
1968 static const char * const cs_etmv4_priv_fmts[] = {
1969 [CS_ETM_MAGIC] = " Magic number %llx\n",
1970 [CS_ETM_CPU] = " CPU %lld\n",
1971 [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
1972 [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
1973 [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
1974 [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
1975 [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
1976 [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
1977 [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
1980 static void cs_etm__print_auxtrace_info(u64 *val, int num)
1984 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
1985 fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
1987 for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
1988 if (val[i] == __perf_cs_etmv3_magic)
1989 for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
1990 fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
1991 else if (val[i] == __perf_cs_etmv4_magic)
1992 for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
1993 fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
1995 /* failure.. return */
2000 int cs_etm__process_auxtrace_info(union perf_event *event,
2001 struct perf_session *session)
2003 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2004 struct cs_etm_auxtrace *etm = NULL;
2005 struct int_node *inode;
2006 unsigned int pmu_type;
2007 int event_header_size = sizeof(struct perf_event_header);
2008 int info_header_size;
2009 int total_size = auxtrace_info->header.size;
2012 int err = 0, idx = -1;
2014 u64 *ptr, *hdr = NULL;
2015 u64 **metadata = NULL;
2018 * sizeof(auxtrace_info_event::type) +
2019 * sizeof(auxtrace_info_event::reserved) == 8
2021 info_header_size = 8;
2023 if (total_size < (event_header_size + info_header_size))
2026 priv_size = total_size - event_header_size - info_header_size;
2028 /* First the global part */
2029 ptr = (u64 *) auxtrace_info->priv;
2031 /* Look for version '0' of the header */
2035 hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
2039 /* Extract header information - see cs-etm.h for format */
2040 for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
2042 num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
2043 pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
2047 * Create an RB tree for traceID-metadata tuple. Since the conversion
2048 * has to be made for each packet that gets decoded, optimizing access
2049 * in anything other than a sequential array is worth doing.
2051 traceid_list = intlist__new(NULL);
2052 if (!traceid_list) {
2057 metadata = zalloc(sizeof(*metadata) * num_cpu);
2060 goto err_free_traceid_list;
2064 * The metadata is stored in the auxtrace_info section and encodes
2065 * the configuration of the ARM embedded trace macrocell which is
2066 * required by the trace decoder to properly decode the trace due
2067 * to its highly compressed nature.
2069 for (j = 0; j < num_cpu; j++) {
2070 if (ptr[i] == __perf_cs_etmv3_magic) {
2071 metadata[j] = zalloc(sizeof(*metadata[j]) *
2075 goto err_free_metadata;
2077 for (k = 0; k < CS_ETM_PRIV_MAX; k++)
2078 metadata[j][k] = ptr[i + k];
2080 /* The traceID is our handle */
2081 idx = metadata[j][CS_ETM_ETMTRACEIDR];
2082 i += CS_ETM_PRIV_MAX;
2083 } else if (ptr[i] == __perf_cs_etmv4_magic) {
2084 metadata[j] = zalloc(sizeof(*metadata[j]) *
2088 goto err_free_metadata;
2090 for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
2091 metadata[j][k] = ptr[i + k];
2093 /* The traceID is our handle */
2094 idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
2095 i += CS_ETMV4_PRIV_MAX;
2098 /* Get an RB node for this CPU */
2099 inode = intlist__findnew(traceid_list, idx);
2101 /* Something went wrong, no need to continue */
2103 err = PTR_ERR(inode);
2104 goto err_free_metadata;
2108 * The node for that CPU should not be taken.
2109 * Back out if that's the case.
2113 goto err_free_metadata;
2115 /* All good, associate the traceID with the metadata pointer */
2116 inode->priv = metadata[j];
2120 * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
2121 * CS_ETMV4_PRIV_MAX mark how many double words are in the
2122 * global metadata, and each cpu's metadata respectively.
2123 * The following tests if the correct number of double words was
2124 * present in the auxtrace info section.
2126 if (i * 8 != priv_size) {
2128 goto err_free_metadata;
2131 etm = zalloc(sizeof(*etm));
2135 goto err_free_metadata;
2138 err = auxtrace_queues__init(&etm->queues);
2142 etm->session = session;
2143 etm->machine = &session->machines.host;
2145 etm->num_cpu = num_cpu;
2146 etm->pmu_type = pmu_type;
2147 etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
2148 etm->metadata = metadata;
2149 etm->auxtrace_type = auxtrace_info->type;
2150 etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
2152 etm->auxtrace.process_event = cs_etm__process_event;
2153 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
2154 etm->auxtrace.flush_events = cs_etm__flush_events;
2155 etm->auxtrace.free_events = cs_etm__free_events;
2156 etm->auxtrace.free = cs_etm__free;
2157 session->auxtrace = &etm->auxtrace;
2159 etm->unknown_thread = thread__new(999999999, 999999999);
2160 if (!etm->unknown_thread)
2161 goto err_free_queues;
2164 * Initialize list node so that at thread__zput() we can avoid
2165 * segmentation fault at list_del_init().
2167 INIT_LIST_HEAD(&etm->unknown_thread->node);
2169 err = thread__set_comm(etm->unknown_thread, "unknown", 0);
2171 goto err_delete_thread;
2173 if (thread__init_map_groups(etm->unknown_thread, etm->machine))
2174 goto err_delete_thread;
2177 cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
2181 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2182 etm->synth_opts = *session->itrace_synth_opts;
2184 itrace_synth_opts__set_default(&etm->synth_opts,
2185 session->itrace_synth_opts->default_no_sample);
2186 etm->synth_opts.callchain = false;
2189 err = cs_etm__synth_events(etm, session);
2191 goto err_delete_thread;
2193 err = auxtrace_queues__process_index(&etm->queues, session);
2195 goto err_delete_thread;
2197 etm->data_queued = etm->queues.populated;
2202 thread__zput(etm->unknown_thread);
2204 auxtrace_queues__free(&etm->queues);
2205 session->auxtrace = NULL;
2209 /* No need to check @metadata[j], free(NULL) is supported */
2210 for (j = 0; j < num_cpu; j++)
2213 err_free_traceid_list:
2214 intlist__delete(traceid_list);