Lines Matching +full:is +full:- +full:decoded +full:- +full:cs

1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015-2018 Linaro Limited.
12 #include <linux/coresight-pmu.h>
22 #include "cs-etm.h"
23 #include "cs-etm-decoder/cs-etm-decoder.h"
37 #include "thread-stack.h"
40 #include "util/synthetic-events.h"
61 * Per-thread ignores the trace channel ID and instead assumes that
63 * which CPU it ran on. It also implies no context IDs so the TID is
143 * work with. One option is to modify to auxtrace_heap_XYZ() API or simply
151 #define SINK_UNSET ((u32) -1)
168 inode = intlist__find(etmq->traceid_list, trace_chan_id);
170 return -EINVAL;
172 metadata = inode->priv;
182 inode = intlist__find(etmq->traceid_list, trace_chan_id);
184 return -EINVAL;
186 metadata = inode->priv;
192 * The returned PID format is presented as an enum:
194 * CS_ETM_PIDFMT_CTXTID: CONTEXTIDR or CONTEXTIDR_EL1 is traced.
195 * CS_ETM_PIDFMT_CTXTID2: CONTEXTIDR_EL2 is traced.
204 * The result is cached in etm->pid_fmt so this function only needs to be called
213 /* CONTEXTIDR is traced */
218 /* CONTEXTIDR_EL2 is traced */
221 /* CONTEXTIDR_EL1 is traced */
231 return etmq->etm->pid_fmt;
238 struct int_node *inode = intlist__findnew(etmq->traceid_list, trace_chan_id);
242 return -ENOMEM;
244 /* Disallow re-mapping a different traceID to metadata pair. */
245 if (inode->priv) {
246 u64 *curr_cpu_data = inode->priv;
253 * are expected (but not supported) in per-thread mode,
256 if (etmq->etm->per_thread_decoding)
257 pr_err("CS_ETM: overlapping Trace IDs aren't currently supported in per-thread mode\n");
261 return -EINVAL;
271 return -EINVAL;
274 /* Skip re-adding the same mappings if everything matched */
279 inode->priv = cpu_metadata;
286 if (etm->per_thread_decoding)
287 return etm->queues.queue_array[0].priv;
289 return etm->queues.queue_array[cpu].priv;
298 * If the queue is unformatted then only save one mapping in the
299 * queue associated with that CPU so only one decoder is made.
302 if (etmq->format == UNFORMATTED)
310 for (unsigned int i = 0; i < etm->queues.nr_queues; ++i) {
313 etmq = etm->queues.queue_array[i].priv;
332 return -EINVAL;
355 * Check sink id hasn't changed in per-cpu mode. In per-thread mode,
356 * let it pass for now until an actual overlapping trace ID is hit. In
359 if (!etmq->etm->per_thread_decoding && etmq->sink_id != SINK_UNSET &&
360 etmq->sink_id != sink_id) {
362 return -EINVAL;
365 etmq->sink_id = sink_id;
368 for (unsigned int i = 0; i < etm->queues.nr_queues; ++i) {
369 struct cs_etm_queue *other_etmq = etm->queues.queue_array[i].priv;
372 if (other_etmq->sink_id != etmq->sink_id)
376 if (other_etmq->traceid_list == etmq->traceid_list)
380 if (!intlist__empty(etmq->traceid_list)) {
382 return -EINVAL;
385 etmq->own_traceid_list = NULL;
386 intlist__delete(etmq->traceid_list);
387 etmq->traceid_list = other_etmq->traceid_list;
418 return -EINVAL;
440 return -EINVAL;
453 for (i = 0; i < etm->num_cpu; i++) {
454 if (etm->metadata[i][CS_ETM_CPU] == (u64)cpu) {
459 return -1;
470 return (idx != -1) ? etm->metadata[idx] : NULL;
477 * The routine is tolerant of seeing multiple packets with the same association,
478 * but a CPU / Trace ID association changing during a session is an error.
490 hw_id = event->aux_output_hw_id.hw_id;
495 pr_err("CS ETM Trace: PERF_RECORD_AUX_OUTPUT_HW_ID version %d not supported. Please update Perf.\n",
497 return -EINVAL;
501 etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
502 if (!etm || !etm->metadata)
503 return -EINVAL;
506 evsel = evlist__event2evsel(session->evlist, event);
508 return -EINVAL;
513 if (cpu == -1) {
514 /* no CPU in the sample - possibly recorded with an old version of perf */
516 return -EINVAL;
529 * When a timestamp packet is encountered the backend code
530 * is stopped so that the front end has time to process packets
535 etmq->pending_timestamp_chan_id = trace_chan_id;
543 if (!etmq->pending_timestamp_chan_id)
547 *trace_chan_id = etmq->pending_timestamp_chan_id;
550 etmq->pending_timestamp_chan_id);
555 etmq->pending_timestamp_chan_id = 0;
558 return packet_queue->cs_timestamp;
565 queue->head = 0;
566 queue->tail = 0;
567 queue->packet_count = 0;
569 queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
570 queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
571 queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
572 queue->packet_buffer[i].instr_count = 0;
573 queue->packet_buffer[i].last_instr_taken_branch = false;
574 queue->packet_buffer[i].last_instr_size = 0;
575 queue->packet_buffer[i].last_instr_type = 0;
576 queue->packet_buffer[i].last_instr_subtype = 0;
577 queue->packet_buffer[i].last_instr_cond = 0;
578 queue->packet_buffer[i].flags = 0;
579 queue->packet_buffer[i].exception_number = UINT32_MAX;
580 queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
581 queue->packet_buffer[i].cpu = INT_MIN;
590 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
593 idx = (int)(intptr_t)inode->priv;
594 tidq = etmq->traceid_queues[idx];
595 cs_etm__clear_packet_queue(&tidq->packet_queue);
603 int rc = -ENOMEM;
605 struct cs_etm_auxtrace *etm = etmq->etm;
607 cs_etm__clear_packet_queue(&tidq->packet_queue);
609 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
610 tidq->trace_chan_id = trace_chan_id;
611 tidq->el = tidq->prev_packet_el = ocsd_EL_unknown;
612 tidq->thread = machine__findnew_thread(&etm->session->machines.host, -1,
613 queue->tid);
614 tidq->prev_packet_thread = machine__idle_thread(&etm->session->machines.host);
616 tidq->packet = zalloc(sizeof(struct cs_etm_packet));
617 if (!tidq->packet)
620 tidq->prev_packet = zalloc(sizeof(struct cs_etm_packet));
621 if (!tidq->prev_packet)
624 if (etm->synth_opts.last_branch) {
627 sz += etm->synth_opts.last_branch_sz *
629 tidq->last_branch = zalloc(sz);
630 if (!tidq->last_branch)
632 tidq->last_branch_rb = zalloc(sz);
633 if (!tidq->last_branch_rb)
637 tidq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
638 if (!tidq->event_buf)
644 zfree(&tidq->last_branch_rb);
645 zfree(&tidq->last_branch);
646 zfree(&tidq->prev_packet);
647 zfree(&tidq->packet);
659 struct cs_etm_auxtrace *etm = etmq->etm;
661 if (etm->per_thread_decoding)
664 traceid_queues_list = etmq->traceid_queues_list;
672 idx = (int)(intptr_t)inode->priv;
673 return etmq->traceid_queues[idx];
685 /* Memory for the inode is free'ed in cs_etm_free_traceid_queues () */
691 inode->priv = (void *)(intptr_t)idx;
697 traceid_queues = etmq->traceid_queues;
704 * memory is left untouched.
710 etmq->traceid_queues = traceid_queues;
712 return etmq->traceid_queues[idx];
732 return &tidq->packet_queue;
742 if (etm->synth_opts.branches || etm->synth_opts.last_branch ||
743 etm->synth_opts.instructions) {
749 * previous and current packets. This is because the previous
750 * packet is used for the 'from' IP for branch samples, so the
756 tmp = tidq->packet;
757 tidq->packet = tidq->prev_packet;
758 tidq->prev_packet = tmp;
759 tidq->prev_packet_el = tidq->el;
760 thread__put(tidq->prev_packet_thread);
761 tidq->prev_packet_thread = thread__get(tidq->thread);
773 snprintf(queue_nr, sizeof(queue_nr), "Qnr:%d; ", etmq->queue_nr);
777 if (len && (pkt_string[len-1] == '\n'))
788 t_params->protocol = cs_etm__get_v7_protocol_version(etmidr);
789 t_params->etmv3.reg_ctrl = metadata[CS_ETM_ETMCR];
790 t_params->etmv3.reg_trc_id = metadata[CS_ETM_ETMTRACEIDR];
796 t_params->protocol = CS_ETM_PROTO_ETMV4i;
797 t_params->etmv4.reg_idr0 = metadata[CS_ETMV4_TRCIDR0];
798 t_params->etmv4.reg_idr1 = metadata[CS_ETMV4_TRCIDR1];
799 t_params->etmv4.reg_idr2 = metadata[CS_ETMV4_TRCIDR2];
800 t_params->etmv4.reg_idr8 = metadata[CS_ETMV4_TRCIDR8];
801 t_params->etmv4.reg_configr = metadata[CS_ETMV4_TRCCONFIGR];
802 t_params->etmv4.reg_traceidr = metadata[CS_ETMV4_TRCTRACEIDR];
808 t_params->protocol = CS_ETM_PROTO_ETE;
809 t_params->ete.reg_idr0 = metadata[CS_ETE_TRCIDR0];
810 t_params->ete.reg_idr1 = metadata[CS_ETE_TRCIDR1];
811 t_params->ete.reg_idr2 = metadata[CS_ETE_TRCIDR2];
812 t_params->ete.reg_idr8 = metadata[CS_ETE_TRCIDR8];
813 t_params->ete.reg_configr = metadata[CS_ETE_TRCCONFIGR];
814 t_params->ete.reg_traceidr = metadata[CS_ETE_TRCTRACEIDR];
815 t_params->ete.reg_devarch = metadata[CS_ETE_TRCDEVARCH];
823 intlist__for_each_entry(inode, etmq->traceid_list) {
824 u64 *metadata = inode->priv;
840 return -EINVAL;
851 int ret = -EINVAL;
856 d_params->packet_printer = cs_etm__packet_dump;
857 d_params->operation = mode;
858 d_params->data = etmq;
859 d_params->formatted = etmq->format == FORMATTED;
860 d_params->fsyncs = false;
861 d_params->hsyncs = false;
862 d_params->frame_aligned = true;
879 cs_etm_decoder__get_name(etmq->decoder), buffer->size);
885 etmq->decoder, buffer->offset,
886 &((u8 *)buffer->data)[buffer_used],
887 buffer->size - buffer_used, &consumed);
892 } while (buffer_used < buffer->size);
894 cs_etm_decoder__reset(etmq->decoder);
900 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
906 if (!tool->ordered_events)
907 return -EINVAL;
909 if (etm->timeless_decoding) {
911 * Pass tid = -1 to process all queues. But likely they will have
914 return cs_etm__process_timeless_queues(etm, -1);
926 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
929 priv = (uintptr_t)inode->priv;
933 tidq = etmq->traceid_queues[idx];
934 thread__zput(tidq->thread);
935 thread__zput(tidq->prev_packet_thread);
936 zfree(&tidq->event_buf);
937 zfree(&tidq->last_branch);
938 zfree(&tidq->last_branch_rb);
939 zfree(&tidq->prev_packet);
940 zfree(&tidq->packet);
952 etmq->traceid_queues_list = NULL;
955 zfree(&etmq->traceid_queues);
966 cs_etm_decoder__free(etmq->decoder);
969 if (etmq->own_traceid_list) {
971 intlist__for_each_entry_safe(inode, tmp, etmq->own_traceid_list)
972 intlist__remove(etmq->own_traceid_list, inode);
975 intlist__delete(etmq->own_traceid_list);
984 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
987 struct auxtrace_queues *queues = &aux->queues;
989 for (i = 0; i < queues->nr_queues; i++) {
990 cs_etm__free_queue(queues->queue_array[i].priv);
991 queues->queue_array[i].priv = NULL;
1000 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
1004 session->auxtrace = NULL;
1006 for (i = 0; i < aux->num_cpu; i++)
1007 zfree(&aux->metadata[i]);
1009 zfree(&aux->metadata);
1016 struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
1020 return evsel->core.attr.type == aux->pmu_type;
1030 * running at EL1 assume everything is the host.
1033 return &etmq->etm->session->machines.host;
1036 * Not perfect, but otherwise assume anything in EL1 is the default
1037 * guest, and everything else is the host. Distinguishing between guest
1038 * and host userspaces isn't currently supported either. Neither is
1039 * multiple guest support. All this does is reduce the likeliness of
1045 return machines__find_guest(&etmq->etm->session->machines,
1052 return &etmq->etm->session->machines.host;
1111 assert(tidq->el == ocsd_EL1 || tidq->el == ocsd_EL0);
1113 assert(tidq->el == ocsd_EL2);
1115 assert(tidq->el == ocsd_EL3);
1118 cpumode = cs_etm__cpu_mode(etmq, address, tidq->el);
1120 if (!thread__find_map(tidq->thread, cpumode, address, &al))
1127 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR &&
1135 len = dso__data_read_offset(dso, maps__machine(thread__maps(tidq->thread)),
1139 ui__warning_once("CS ETM Trace: Missing DSO. Use 'perf archive' or debuginfod to export data from the traced system.\n"
1140 " Enable CONFIG_PROC_KCORE or use option '-k /path/to/vmlinux' for kernel symbols.\n");
1142 pr_err("CS ETM Trace: Debug data not found for address %#"PRIx64" in %s\n",
1161 etmq->traceid_queues_list = intlist__new(NULL);
1162 if (!etmq->traceid_queues_list)
1166 * Create an RB tree for traceID-metadata tuple. Since the conversion
1167 * has to be made for each packet that gets decoded, optimizing access
1168 * in anything other than a sequential array is worth doing.
1170 etmq->traceid_list = etmq->own_traceid_list = intlist__new(NULL);
1171 if (!etmq->traceid_list)
1177 intlist__delete(etmq->traceid_queues_list);
1187 struct cs_etm_queue *etmq = queue->priv;
1195 return -ENOMEM;
1197 queue->priv = etmq;
1198 etmq->etm = etm;
1199 etmq->queue_nr = queue_nr;
1200 queue->cpu = queue_nr; /* Placeholder, may be reset to -1 in per-thread mode */
1201 etmq->offset = 0;
1202 etmq->sink_id = SINK_UNSET;
1217 * We are under a CPU-wide trace scenario. As such we need to know
1221 * timestamp. The timestamp is then added to the auxtrace min heap
1235 * encountering a CS timestamp, a full packet queue or the end of
1256 * just decoded are useless since no timestamp has been
1269 * Note that packets decoded above are still in the traceID's packet
1273 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
1282 struct branch_stack *bs_src = tidq->last_branch_rb;
1283 struct branch_stack *bs_dst = tidq->last_branch;
1287 * Set the number of records before early exit: ->nr is used to
1288 * determine how many branches to copy from ->entries.
1290 bs_dst->nr = bs_src->nr;
1293 * Early exit when there is nothing to copy.
1295 if (!bs_src->nr)
1299 * As bs_src->entries is a circular buffer, we need to copy from it in
1301 * branch ->last_branch_pos until the end of bs_src->entries buffer.
1303 nr = etmq->etm->synth_opts.last_branch_sz - tidq->last_branch_pos;
1304 memcpy(&bs_dst->entries[0],
1305 &bs_src->entries[tidq->last_branch_pos],
1310 * of the bs_src->entries buffer and until the ->last_branch_pos element
1315 if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
1316 memcpy(&bs_dst->entries[nr],
1317 &bs_src->entries[0],
1318 sizeof(struct branch_entry) * tidq->last_branch_pos);
1325 tidq->last_branch_pos = 0;
1326 tidq->last_branch_rb->nr = 0;
1337 * T32 instruction size is indicated by bits[15:11] of the first
1338 * 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
1339 * denote a 32-bit instruction.
1350 if (packet->sample_type == CS_ETM_DISCONTINUITY ||
1351 packet->sample_type == CS_ETM_EXCEPTION)
1354 return packet->start_addr;
1361 if (packet->sample_type == CS_ETM_DISCONTINUITY)
1364 return packet->end_addr - packet->last_instr_size;
1372 if (packet->isa == CS_ETM_ISA_T32) {
1373 u64 addr = packet->start_addr;
1378 offset--;
1384 return packet->start_addr + offset * 4;
1390 struct branch_stack *bs = tidq->last_branch_rb;
1399 if (!tidq->last_branch_pos)
1400 tidq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
1402 tidq->last_branch_pos -= 1;
1404 be = &bs->entries[tidq->last_branch_pos];
1405 be->from = cs_etm__last_executed_instr(tidq->prev_packet);
1406 be->to = cs_etm__first_executed_instr(tidq->packet);
1408 be->flags.mispred = 0;
1409 be->flags.predicted = 1;
1412 * Increment bs->nr until reaching the number of last branches asked by
1415 if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
1416 bs->nr += 1;
1422 event->header.size = perf_event__sample_event_size(sample, type, 0);
1430 struct auxtrace_buffer *aux_buffer = etmq->buffer;
1434 queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
1442 etmq->buf_len = 0;
1446 etmq->buffer = aux_buffer;
1449 if (!aux_buffer->data) {
1451 int fd = perf_data__fd(etmq->etm->session->data);
1453 aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
1454 if (!aux_buffer->data)
1455 return -ENOMEM;
1462 etmq->buf_used = 0;
1463 etmq->buf_len = aux_buffer->size;
1464 etmq->buf = aux_buffer->data;
1466 return etmq->buf_len;
1475 if (tid != -1) {
1476 thread__zput(tidq->thread);
1477 tidq->thread = machine__find_thread(machine, -1, tid);
1481 if (!tidq->thread)
1482 tidq->thread = machine__idle_thread(machine);
1484 tidq->el = el;
1494 return -EINVAL;
1502 return !!etmq->etm->timeless_decoding;
1514 if (packet->sample_type == CS_ETM_DISCONTINUITY) {
1515 sample->insn_len = 0;
1520 * T32 instruction size might be 32-bit or 16-bit, decide by calling
1523 if (packet->isa == CS_ETM_ISA_T32)
1524 sample->insn_len = cs_etm__t32_instr_size(etmq, trace_chan_id,
1525 sample->ip);
1526 /* Otherwise, A64 and A32 instruction size are always 32-bit. */
1528 sample->insn_len = 4;
1530 cs_etm__mem_access(etmq, trace_chan_id, sample->ip, sample->insn_len,
1531 (void *)sample->insn, 0);
1536 struct cs_etm_auxtrace *etm = etmq->etm;
1538 if (etm->has_virtual_ts)
1539 return tsc_to_perf_time(cs_timestamp, &etm->tc);
1547 struct cs_etm_auxtrace *etm = etmq->etm;
1548 struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue;
1550 if (!etm->timeless_decoding && etm->has_virtual_ts)
1551 return packet_queue->cs_timestamp;
1553 return etm->latest_kernel_timestamp;
1561 struct cs_etm_auxtrace *etm = etmq->etm;
1562 union perf_event *event = tidq->event_buf;
1565 event->sample.header.type = PERF_RECORD_SAMPLE;
1566 event->sample.header.misc = cs_etm__cpu_mode(etmq, addr, tidq->el);
1567 event->sample.header.size = sizeof(struct perf_event_header);
1573 sample.pid = thread__pid(tidq->thread);
1574 sample.tid = thread__tid(tidq->thread);
1575 sample.id = etmq->etm->instructions_id;
1576 sample.stream_id = etmq->etm->instructions_id;
1578 sample.cpu = tidq->packet->cpu;
1579 sample.flags = tidq->prev_packet->flags;
1580 sample.cpumode = event->sample.header.misc;
1582 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample);
1584 if (etm->synth_opts.last_branch)
1585 sample.branch_stack = tidq->last_branch;
1587 if (etm->synth_opts.inject) {
1589 etm->instructions_sample_type);
1594 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1598 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1605 * The cs etm packet encodes an instruction range between a branch target
1612 struct cs_etm_auxtrace *etm = etmq->etm;
1614 union perf_event *event = tidq->event_buf;
1622 ip = cs_etm__last_executed_instr(tidq->prev_packet);
1624 event->sample.header.type = PERF_RECORD_SAMPLE;
1625 event->sample.header.misc = cs_etm__cpu_mode(etmq, ip,
1626 tidq->prev_packet_el);
1627 event->sample.header.size = sizeof(struct perf_event_header);
1633 sample.pid = thread__pid(tidq->prev_packet_thread);
1634 sample.tid = thread__tid(tidq->prev_packet_thread);
1635 sample.addr = cs_etm__first_executed_instr(tidq->packet);
1636 sample.id = etmq->etm->branches_id;
1637 sample.stream_id = etmq->etm->branches_id;
1639 sample.cpu = tidq->packet->cpu;
1640 sample.flags = tidq->prev_packet->flags;
1641 sample.cpumode = event->sample.header.misc;
1643 cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet,
1649 if (etm->synth_opts.last_branch) {
1652 .hw_idx = -1ULL,
1661 if (etm->synth_opts.inject) {
1663 etm->branches_sample_type);
1668 ret = perf_session__deliver_synth_event(etm->session, event, &sample);
1672 "CS ETM Trace: failed to deliver instruction event, error %d\n",
1681 struct evlist *evlist = session->evlist;
1689 if (evsel->core.attr.type == etm->pmu_type) {
1703 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
1706 if (etm->timeless_decoding)
1711 attr.exclude_user = evsel->core.attr.exclude_user;
1712 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1713 attr.exclude_hv = evsel->core.attr.exclude_hv;
1714 attr.exclude_host = evsel->core.attr.exclude_host;
1715 attr.exclude_guest = evsel->core.attr.exclude_guest;
1716 attr.sample_id_all = evsel->core.attr.sample_id_all;
1717 attr.read_format = evsel->core.attr.read_format;
1720 id = evsel->core.id[0] + 1000000000;
1725 if (etm->synth_opts.branches) {
1732 etm->branches_sample_type = attr.sample_type;
1733 etm->branches_id = id;
1738 if (etm->synth_opts.last_branch) {
1748 if (etm->synth_opts.instructions) {
1750 attr.sample_period = etm->synth_opts.period;
1751 etm->instructions_sample_period = attr.sample_period;
1755 etm->instructions_sample_type = attr.sample_type;
1756 etm->instructions_id = id;
1766 struct cs_etm_auxtrace *etm = etmq->etm;
1768 u8 trace_chan_id = tidq->trace_chan_id;
1772 instrs_prev = tidq->period_instructions;
1774 tidq->period_instructions += tidq->packet->instr_count;
1778 * PREV_PACKET is a branch.
1780 if (etm->synth_opts.last_branch &&
1781 tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1782 tidq->prev_packet->last_instr_taken_branch)
1785 if (etm->synth_opts.instructions &&
1786 tidq->period_instructions >= etm->instructions_sample_period) {
1800 * --------------------------------------------------
1807 * \---------------- -----------------/
1809 * tidq->packet->instr_count
1812 * every etm->instructions_sample_period instructions - as
1813 * defined on the perf command line. Sample(n) is being the
1817 * tidq->packet->instr_count represents the number of
1823 * etm->instructions_sample_period.
1826 * instructions, one is the tail of the old packet and another
1827 * is the head of the new coming packet, to generate
1830 * instructions will be used by later packet and it is assigned
1831 * to tidq->period_instructions for next round calculation.
1836 * entry conditions ensure that instrs_prev is less than
1837 * etm->instructions_sample_period.
1839 u64 offset = etm->instructions_sample_period - instrs_prev;
1843 if (etm->synth_opts.last_branch)
1846 while (tidq->period_instructions >=
1847 etm->instructions_sample_period) {
1849 * Calculate the address of the sampled instruction (-1
1850 * as sample is reported as though instruction has just
1855 tidq->packet, offset - 1);
1858 etm->instructions_sample_period);
1862 offset += etm->instructions_sample_period;
1863 tidq->period_instructions -=
1864 etm->instructions_sample_period;
1868 if (etm->synth_opts.branches) {
1872 if (tidq->prev_packet->sample_type == CS_ETM_DISCONTINUITY)
1876 if (tidq->prev_packet->sample_type == CS_ETM_RANGE &&
1877 tidq->prev_packet->last_instr_taken_branch)
1895 * When the exception packet is inserted, whether the last instruction
1896 * in previous range packet is taken branch or not, we need to force
1897 * to set 'prev_packet->last_instr_taken_branch' to true. This ensures
1899 * exception is trapped to kernel or before the exception returning.
1905 if (tidq->prev_packet->sample_type == CS_ETM_RANGE)
1906 tidq->prev_packet->last_instr_taken_branch = true;
1915 struct cs_etm_auxtrace *etm = etmq->etm;
1918 if (tidq->prev_packet->sample_type == CS_ETM_EMPTY)
1921 if (etmq->etm->synth_opts.last_branch &&
1922 etmq->etm->synth_opts.instructions &&
1923 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1936 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1940 tidq->period_instructions);
1944 tidq->period_instructions = 0;
1948 if (etm->synth_opts.branches &&
1949 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1959 if (etm->synth_opts.last_branch)
1971 * It has no new packet coming and 'etmq->packet' contains the stale
1979 if (etmq->etm->synth_opts.last_branch &&
1980 etmq->etm->synth_opts.instructions &&
1981 tidq->prev_packet->sample_type == CS_ETM_RANGE) {
1991 addr = cs_etm__last_executed_instr(tidq->prev_packet);
1995 tidq->period_instructions);
1999 tidq->period_instructions = 0;
2015 if (!etmq->buf_len) {
2021 * are contiguous, reset the decoder to force re-sync.
2023 ret = cs_etm_decoder__reset(etmq->decoder);
2028 return etmq->buf_len;
2040 switch (packet->isa) {
2043 * The SVC of T32 is defined in ARM DDI 0487D.a, F5.1.247:
2046 * +-----------------+--------+
2048 * +-----------------+--------+
2054 addr = end_addr - 2;
2063 * The SVC of A32 is defined in ARM DDI 0487D.a, F5.1.247:
2066 * +---------+---------+-------------------------+
2068 * +---------+---------+-------------------------+
2070 addr = end_addr - 4;
2080 * The SVC of A64 is defined in ARM DDI 0487D.a, C6.2.294:
2083 * +-----------------------+---------+-----------+
2085 * +-----------------------+---------+-----------+
2087 addr = end_addr - 4;
2105 u8 trace_chan_id = tidq->trace_chan_id;
2106 struct cs_etm_packet *packet = tidq->packet;
2107 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2110 if (packet->exception_number == CS_ETMV3_EXC_SVC)
2119 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2121 prev_packet->end_addr))
2131 struct cs_etm_packet *packet = tidq->packet;
2134 if (packet->exception_number == CS_ETMV3_EXC_DEBUG_HALT ||
2135 packet->exception_number == CS_ETMV3_EXC_ASYNC_DATA_ABORT ||
2136 packet->exception_number == CS_ETMV3_EXC_PE_RESET ||
2137 packet->exception_number == CS_ETMV3_EXC_IRQ ||
2138 packet->exception_number == CS_ETMV3_EXC_FIQ)
2142 if (packet->exception_number == CS_ETMV4_EXC_RESET ||
2143 packet->exception_number == CS_ETMV4_EXC_DEBUG_HALT ||
2144 packet->exception_number == CS_ETMV4_EXC_SYSTEM_ERROR ||
2145 packet->exception_number == CS_ETMV4_EXC_INST_DEBUG ||
2146 packet->exception_number == CS_ETMV4_EXC_DATA_DEBUG ||
2147 packet->exception_number == CS_ETMV4_EXC_IRQ ||
2148 packet->exception_number == CS_ETMV4_EXC_FIQ)
2158 u8 trace_chan_id = tidq->trace_chan_id;
2159 struct cs_etm_packet *packet = tidq->packet;
2160 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2163 if (packet->exception_number == CS_ETMV3_EXC_SMC ||
2164 packet->exception_number == CS_ETMV3_EXC_HYP ||
2165 packet->exception_number == CS_ETMV3_EXC_JAZELLE_THUMBEE ||
2166 packet->exception_number == CS_ETMV3_EXC_UNDEFINED_INSTR ||
2167 packet->exception_number == CS_ETMV3_EXC_PREFETCH_ABORT ||
2168 packet->exception_number == CS_ETMV3_EXC_DATA_FAULT ||
2169 packet->exception_number == CS_ETMV3_EXC_GENERIC)
2173 if (packet->exception_number == CS_ETMV4_EXC_TRAP ||
2174 packet->exception_number == CS_ETMV4_EXC_ALIGNMENT ||
2175 packet->exception_number == CS_ETMV4_EXC_INST_FAULT ||
2176 packet->exception_number == CS_ETMV4_EXC_DATA_FAULT)
2183 if (packet->exception_number == CS_ETMV4_EXC_CALL &&
2185 prev_packet->end_addr))
2195 if (packet->exception_number > CS_ETMV4_EXC_FIQ &&
2196 packet->exception_number <= CS_ETMV4_EXC_END)
2206 struct cs_etm_packet *packet = tidq->packet;
2207 struct cs_etm_packet *prev_packet = tidq->prev_packet;
2208 u8 trace_chan_id = tidq->trace_chan_id;
2212 switch (packet->sample_type) {
2219 if (packet->last_instr_type == OCSD_INSTR_BR &&
2220 packet->last_instr_subtype == OCSD_S_INSTR_NONE) {
2221 packet->flags = PERF_IP_FLAG_BRANCH;
2223 if (packet->last_instr_cond)
2224 packet->flags |= PERF_IP_FLAG_CONDITIONAL;
2228 * Immediate branch instruction with link (e.g. BL), this is
2231 if (packet->last_instr_type == OCSD_INSTR_BR &&
2232 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2233 packet->flags = PERF_IP_FLAG_BRANCH |
2237 * Indirect branch instruction with link (e.g. BLR), this is
2240 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2241 packet->last_instr_subtype == OCSD_S_INSTR_BR_LINK)
2242 packet->flags = PERF_IP_FLAG_BRANCH |
2247 * OCSD_S_INSTR_V7_IMPLIED_RET, this is explicit hint for
2250 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2251 packet->last_instr_subtype == OCSD_S_INSTR_V7_IMPLIED_RET)
2252 packet->flags = PERF_IP_FLAG_BRANCH |
2257 * this is used for function return, especially for functions
2260 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2261 packet->last_instr_subtype == OCSD_S_INSTR_NONE)
2262 packet->flags = PERF_IP_FLAG_BRANCH |
2266 if (packet->last_instr_type == OCSD_INSTR_BR_INDIRECT &&
2267 packet->last_instr_subtype == OCSD_S_INSTR_V8_RET)
2268 packet->flags = PERF_IP_FLAG_BRANCH |
2276 if (prev_packet->sample_type == CS_ETM_DISCONTINUITY)
2277 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2281 * If the previous packet is an exception return packet
2286 if (prev_packet->flags == (PERF_IP_FLAG_BRANCH |
2290 packet, packet->start_addr))
2291 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2297 * The trace is discontinuous, if the previous packet is
2301 if (prev_packet->sample_type == CS_ETM_RANGE)
2302 prev_packet->flags |= PERF_IP_FLAG_BRANCH |
2306 ret = cs_etm__get_magic(etmq, packet->trace_chan_id, &magic);
2310 /* The exception is for system call. */
2312 packet->flags = PERF_IP_FLAG_BRANCH |
2320 packet->flags = PERF_IP_FLAG_BRANCH |
2325 * Otherwise, exception is caused by trap, instruction &
2329 packet->flags = PERF_IP_FLAG_BRANCH |
2334 * When the exception packet is inserted, since exception
2335 * packet is not used standalone for generating samples
2338 * it is an exception taken branch.
2340 if (prev_packet->sample_type == CS_ETM_RANGE)
2341 prev_packet->flags = packet->flags;
2345 * When the exception return packet is inserted, since
2346 * exception return packet is not used standalone for
2349 * flags to tell perf it is an exception return branch.
2356 * reuse it for exception return packet, this is not reliable
2362 * packet is for system call or for other types. Thus the
2369 if (prev_packet->sample_type == CS_ETM_RANGE)
2370 prev_packet->flags = PERF_IP_FLAG_BRANCH |
2388 * Packets are decoded and added to the decoder's packet queue
2390 * processing stops or there is nothing left in the buffer. Normal
2394 ret = cs_etm_decoder__process_data_block(etmq->decoder,
2395 etmq->offset,
2396 &etmq->buf[etmq->buf_used],
2397 etmq->buf_len,
2402 etmq->offset += processed;
2403 etmq->buf_used += processed;
2404 etmq->buf_len -= processed;
2416 packet_queue = &tidq->packet_queue;
2421 tidq->packet);
2440 switch (tidq->packet->sample_type) {
2452 * If the exception packet is coming,
2470 pr_err("CS ETM Trace: empty packet\n");
2471 return -EINVAL;
2485 struct intlist *traceid_queues_list = etmq->traceid_queues_list;
2488 idx = (int)(intptr_t)inode->priv;
2489 tidq = etmq->traceid_queues[idx];
2509 return -EINVAL;
2530 } while (etmq->buf_len);
2561 * buffer. But here in per-cpu mode we need to iterate
2565 etmq->traceid_queues_list) {
2566 idx = (int)(intptr_t)inode->priv;
2567 tidq = etmq->traceid_queues[idx];
2570 } while (etmq->buf_len);
2572 intlist__for_each_entry(inode, etmq->traceid_queues_list) {
2573 idx = (int)(intptr_t)inode->priv;
2574 tidq = etmq->traceid_queues[idx];
2589 struct auxtrace_queues *queues = &etm->queues;
2591 for (i = 0; i < queues->nr_queues; i++) {
2592 struct auxtrace_queue *queue = &etm->queues.queue_array[i];
2593 struct cs_etm_queue *etmq = queue->priv;
2599 if (etm->per_thread_decoding) {
2606 if (tid == -1 || thread__tid(tidq->thread) == tid)
2626 * Pre-populate the heap with one entry from each queue so that we can
2629 for (i = 0; i < etm->queues.nr_queues; i++) {
2630 etmq = etm->queues.queue_array[i].priv;
2640 if (!etm->heap.heap_cnt)
2644 cs_queue_nr = etm->heap.heap_array[0].queue_nr;
2647 queue = &etm->queues.queue_array[queue_nr];
2648 etmq = queue->priv;
2654 auxtrace_heap__pop(&etm->heap);
2663 ret = -EINVAL;
2701 * there is no more traces to decode in the current
2704 * did not get a timestamp, there is no more traces to
2716 * just been decoded. They will be processed and synthesized
2721 ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
2733 if (etm->timeless_decoding)
2739 * trace is supported for guests which wouldn't need pids so this should
2742 th = machine__findnew_thread(&etm->session->machines.host,
2743 event->itrace_start.pid,
2744 event->itrace_start.tid);
2746 return -ENOMEM;
2757 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2760 * Context switch in per-thread mode are irrelevant since perf
2761 * will start/stop tracing as the process is scheduled.
2763 if (etm->timeless_decoding)
2777 * trace is supported for guests which wouldn't need pids so this should
2780 th = machine__findnew_thread(&etm->session->machines.host,
2781 event->context_switch.next_prev_pid,
2782 event->context_switch.next_prev_tid);
2784 return -ENOMEM;
2796 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2803 if (!tool->ordered_events) {
2805 return -EINVAL;
2808 switch (event->header.type) {
2811 * Don't need to wait for cs_etm__flush_events() in per-thread mode to
2813 * this thread. All this does is emit samples earlier than waiting for
2818 if (etm->per_thread_decoding && etm->timeless_decoding)
2820 event->fork.tid);
2835 if (sample->time && (sample->time != (u64)-1))
2836 etm->latest_kernel_timestamp = sample->time;
2853 * This is because the queues can contain multiple entries of the same
2856 for (i = 0; i < etm->queues.nr_queues; ++i)
2857 list_for_each_entry(buf, &etm->queues.queue_array[i].head, list)
2858 if (buf->reference == event->reference)
2859 cs_etm__dump_event(etm->queues.queue_array[i].priv, buf);
2866 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
2869 if (!etm->data_queued) {
2872 int fd = perf_data__fd(session->data);
2873 bool is_pipe = perf_data__is_pipe(session->data);
2875 int idx = event->auxtrace.idx;
2881 if (data_offset == -1)
2882 return -errno;
2885 err = auxtrace_queues__add_event(&etm->queues, session,
2892 cs_etm__dump_event(etm->queues.queue_array[idx].priv, buffer);
2896 dump_queued_data(etm, &event->auxtrace);
2904 struct evlist *evlist = etm->session->evlist;
2906 /* Override timeless mode with user input from --itrace=Z */
2907 if (etm->synth_opts.timeless_decoding) {
2908 etm->timeless_decoding = true;
2916 if (cs_etm__evsel_is_auxtrace(etm->session, evsel)) {
2917 etm->timeless_decoding =
2918 !(evsel->core.attr.config & BIT(ETM_OPT_TS));
2922 pr_err("CS ETM: Couldn't find ETM evsel\n");
2923 return -EINVAL;
2929 * For version 1 there is a per cpu nr_params entry. If we are handling
2959 for (k = CS_ETM_COMMON_BLK_MAX_V1 - 1; k < nr_in_params; k++)
2964 /* read version 1 info block - input and output nr_params may differ */
2969 /* if input has more params than output - skip excess */
2978 metadata[CS_ETM_NR_TRC_PARAMS] = nr_out_params - nr_cmn_params;
2994 * is reset across each buffer, so splitting the buffers up in advance has
3008 struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
3020 auxtrace_event = &auxtrace_event_union->auxtrace;
3021 if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
3022 return -EINVAL;
3024 if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
3025 auxtrace_event->header.size != sz) {
3026 return -EINVAL;
3030 * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
3033 * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
3036 if (auxtrace_event->cpu == (__u32) -1) {
3037 etm->per_thread_decoding = true;
3038 if (auxtrace_event->tid != sample->tid)
3040 } else if (auxtrace_event->cpu != sample->cpu) {
3041 if (etm->per_thread_decoding) {
3043 * Found a per-cpu buffer after a per-thread one was
3046 pr_err("CS ETM: Inconsistent per-thread/per-cpu mode.\n");
3047 return -EINVAL;
3052 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
3054 * Clamp size in snapshot mode. The buffer size is clamped in
3058 aux_size = min(aux_event->aux_size, auxtrace_event->size);
3064 aux_offset = aux_event->aux_offset - aux_size;
3066 aux_size = aux_event->aux_size;
3067 aux_offset = aux_event->aux_offset;
3070 if (aux_offset >= auxtrace_event->offset &&
3071 aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {
3072 struct cs_etm_queue *etmq = etm->queues.queue_array[auxtrace_event->idx].priv;
3081 file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
3083 pr_debug3("CS ETM: Queue buffer size: %#"PRI_lx64" offset: %#"PRI_lx64
3084 " tid: %d cpu: %d\n", aux_size, aux_offset, sample->tid, sample->cpu);
3085 err = auxtrace_queues__add_event(&etm->queues, session, &auxtrace_fragment,
3090 format = (aux_event->flags & PERF_AUX_FLAG_CORESIGHT_FORMAT_RAW) ?
3092 if (etmq->format != UNSET && format != etmq->format) {
3094 return -EINVAL;
3096 etmq->format = format;
3108 if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
3126 if (event->header.type != PERF_RECORD_AUX)
3129 if (event->header.size < sizeof(struct perf_record_aux))
3130 return -EINVAL;
3133 if (!event->aux.aux_size)
3140 evsel = evlist__event2evsel(session->evlist, event);
3142 return -EINVAL;
3150 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
3151 for (i = 0; i < auxtrace_index->nr; i++) {
3152 ent = &auxtrace_index->entries[i];
3153 ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
3154 ent->sz, &event->aux, &sample);
3168 pr_err("CS ETM: Couldn't find auxtrace buffer for aux_offset: %#"PRI_lx64
3169 " tid: %d cpu: %d\n", event->aux.aux_offset, sample.tid, sample.cpu);
3175 struct auxtrace_index *index = list_first_entry_or_null(&session->auxtrace_index,
3177 if (index && index->nr > 0)
3178 return perf_session__peek_events(session, session->header.data_offset,
3179 session->header.data_size,
3184 * buffers or no index at all). Fail silently as there is the possibility of
3185 * queueing them in cs_etm__process_auxtrace_event() if etm->data_queued is still
3194 (CS_##type##_##param - CS_ETM_COMMON_BLK_MAX_V1))
3244 return -EINVAL;
3261 int decoders = intlist__nr_entries(etmq->traceid_list);
3267 * Each queue can only contain data from one CPU when unformatted, so only one decoder is
3270 if (etmq->format == UNFORMATTED)
3288 etmq->decoder = cs_etm_decoder__new(decoders, &d_params,
3291 if (!etmq->decoder)
3298 if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
3299 0x0L, ((u64) -1L),
3307 cs_etm_decoder__free(etmq->decoder);
3310 return -EINVAL;
3315 struct auxtrace_queues *queues = &etm->queues;
3317 for (unsigned int i = 0; i < queues->nr_queues; i++) {
3318 bool empty = list_empty(&queues->queue_array[i].head);
3319 struct cs_etm_queue *etmq = queues->queue_array[i].priv;
3324 * etmq->format is unknown for empty queues.
3326 assert(empty || etmq->format != UNSET);
3340 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3342 struct perf_record_time_conv *tc = &session->time_conv;
3344 int total_size = auxtrace_info->header.size;
3354 ptr = (u64 *) auxtrace_info->priv;
3358 return -ENOMEM;
3364 * The metadata is stored in the auxtrace_info section and encodes
3365 * the configuration of the ARM embedded trace macrocell which is
3381 metadata[j] = cs_etm__create_meta_blk(ptr, &i, CS_ETE_PRIV_MAX, -1);
3383 ui__error("CS ETM Trace: Unrecognised magic number %#"PRIx64". File could be from a newer version of perf.\n",
3385 err = -EINVAL;
3390 err = -ENOMEM;
3405 priv_size = total_size - event_header_size - INFO_HEADER_SIZE;
3407 err = -EINVAL;
3414 err = -ENOMEM;
3423 etm->pid_fmt = cs_etm__init_pid_fmt(metadata[0]);
3425 err = auxtrace_queues__init_nr(&etm->queues, max_cpu + 1);
3429 for (unsigned int j = 0; j < etm->queues.nr_queues; ++j) {
3430 err = cs_etm__setup_queue(etm, &etm->queues.queue_array[j], j);
3435 if (session->itrace_synth_opts->set) {
3436 etm->synth_opts = *session->itrace_synth_opts;
3438 itrace_synth_opts__set_default(&etm->synth_opts,
3439 session->itrace_synth_opts->default_no_sample);
3440 etm->synth_opts.callchain = false;
3443 etm->session = session;
3445 etm->num_cpu = num_cpu;
3446 etm->pmu_type = (unsigned int) ((ptr[CS_PMU_TYPE_CPUS] >> 32) & 0xffffffff);
3447 etm->snapshot_mode = (ptr[CS_ETM_SNAPSHOT] != 0);
3448 etm->metadata = metadata;
3449 etm->auxtrace_type = auxtrace_info->type;
3451 if (etm->synth_opts.use_timestamp)
3454 * therefore the decoder cannot know if the timestamp trace is
3461 etm->has_virtual_ts = true;
3464 etm->has_virtual_ts = cs_etm__has_virtual_ts(metadata, num_cpu);
3466 if (!etm->has_virtual_ts)
3471 "if the Coresight timestamp on the platform is same with the kernel time.\n\n");
3473 etm->auxtrace.process_event = cs_etm__process_event;
3474 etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
3475 etm->auxtrace.flush_events = cs_etm__flush_events;
3476 etm->auxtrace.free_events = cs_etm__free_events;
3477 etm->auxtrace.free = cs_etm__free;
3478 etm->auxtrace.evsel_is_auxtrace = cs_etm__evsel_is_auxtrace;
3479 session->auxtrace = &etm->auxtrace;
3485 etm->tc.time_shift = tc->time_shift;
3486 etm->tc.time_mult = tc->time_mult;
3487 etm->tc.time_zero = tc->time_zero;
3489 etm->tc.time_cycles = tc->time_cycles;
3490 etm->tc.time_mask = tc->time_mask;
3491 etm->tc.cap_user_time_zero = tc->cap_user_time_zero;
3492 etm->tc.cap_user_time_short = tc->cap_user_time_short;
3510 * the same IDs as the old algorithm as far as is possible, unless there are clashes
3518 * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3525 err = perf_session__peek_events(session, session->header.data_offset,
3526 session->header.data_size,
3531 /* if no HW ID found this is a file with metadata values only, map from metadata */
3542 etm->data_queued = etm->queues.populated;
3546 auxtrace_queues__free(&etm->queues);
3547 session->auxtrace = NULL;
3551 /* No need to check @metadata[j], free(NULL) is supported */