1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
8 #include <linux/perf_event.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
28 #include "thread-stack.h"
30 #include "callchain.h"
37 #include "util/perf_api_probe.h"
38 #include "util/synthetic-events.h"
39 #include "time-utils.h"
41 #include "../arch/x86/include/uapi/asm/perf_regs.h"
43 #include "intel-pt-decoder/intel-pt-log.h"
44 #include "intel-pt-decoder/intel-pt-decoder.h"
45 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
46 #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
48 #define MAX_TIMESTAMP (~0ULL)
50 #define INTEL_PT_CFG_PASS_THRU BIT_ULL(0)
51 #define INTEL_PT_CFG_PWR_EVT_EN BIT_ULL(4)
52 #define INTEL_PT_CFG_BRANCH_EN BIT_ULL(13)
53 #define INTEL_PT_CFG_EVT_EN BIT_ULL(31)
54 #define INTEL_PT_CFG_TNT_DIS BIT_ULL(55)
62 struct auxtrace auxtrace;
63 struct auxtrace_queues queues;
64 struct auxtrace_heap heap;
66 struct perf_session *session;
67 struct machine *machine;
68 struct evsel *switch_evsel;
69 struct thread *unknown_thread;
70 bool timeless_decoding;
78 bool sync_switch_not_supported;
80 bool use_thread_stack;
83 bool have_guest_sideband;
84 unsigned int br_stack_sz;
85 unsigned int br_stack_sz_plus;
86 int have_sched_switch;
93 struct perf_tsc_conversion tc;
94 bool cap_user_time_zero;
96 struct itrace_synth_opts synth_opts;
98 bool sample_instructions;
99 u64 instructions_sample_type;
103 u64 cycles_sample_type;
106 bool sample_branches;
108 u64 branches_sample_type;
111 bool sample_transactions;
112 u64 transactions_sample_type;
115 bool sample_ptwrites;
116 u64 ptwrites_sample_type;
119 bool sample_pwr_events;
120 u64 pwr_events_sample_type;
130 int pebs_data_src_fmt;
131 struct evsel *pebs_evsel;
136 u64 iflag_chg_sample_type;
146 unsigned max_non_turbo_ratio;
150 unsigned long num_events;
153 struct addr_filters filts;
155 struct range *time_ranges;
156 unsigned int range_cnt;
158 struct ip_callchain *chain;
159 struct branch_stack *br_stack;
162 struct rb_root vmcs_info;
166 INTEL_PT_SS_NOT_TRACING,
169 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
170 INTEL_PT_SS_EXPECTING_SWITCH_IP,
173 /* applicable_counters is 64-bits */
174 #define INTEL_PT_MAX_PEBS 64
176 struct intel_pt_pebs_event {
182 struct intel_pt_queue {
184 unsigned int queue_nr;
185 struct auxtrace_buffer *buffer;
186 struct auxtrace_buffer *old_buffer;
188 const struct intel_pt_state *state;
189 struct ip_callchain *chain;
190 struct branch_stack *last_branch;
191 union perf_event *event_buf;
194 bool step_through_buffers;
195 bool use_buffer_pid_tid;
202 struct thread *thread;
203 struct machine *guest_machine;
204 struct thread *guest_thread;
205 struct thread *unknown_guest_thread;
206 pid_t guest_machine_pid;
216 unsigned int sel_idx;
222 u64 last_in_insn_cnt;
224 u64 last_cy_insn_cnt;
226 u64 last_br_insn_cnt;
228 unsigned int cbr_seen;
229 char insn[INTEL_PT_INSN_BUF_SZ];
230 struct intel_pt_pebs_event pebs[INTEL_PT_MAX_PEBS];
233 static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
234 unsigned char *buf, size_t len)
236 struct intel_pt_pkt packet;
239 char desc[INTEL_PT_PKT_DESC_MAX];
240 const char *color = PERF_COLOR_BLUE;
241 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
243 color_fprintf(stdout, color,
244 ". ... Intel Processor Trace data: size %zu bytes\n",
248 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
254 color_fprintf(stdout, color, " %08zx: ", pos);
255 for (i = 0; i < pkt_len; i++)
256 color_fprintf(stdout, color, " %02x", buf[i]);
258 color_fprintf(stdout, color, " ");
260 ret = intel_pt_pkt_desc(&packet, desc,
261 INTEL_PT_PKT_DESC_MAX);
263 color_fprintf(stdout, color, " %s\n", desc);
265 color_fprintf(stdout, color, " Bad packet!\n");
273 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
277 intel_pt_dump(pt, buf, len);
280 static void intel_pt_log_event(union perf_event *event)
282 FILE *f = intel_pt_log_fp();
284 if (!intel_pt_enable_logging || !f)
287 perf_event__fprintf(event, NULL, f);
290 static void intel_pt_dump_sample(struct perf_session *session,
291 struct perf_sample *sample)
293 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
297 intel_pt_dump(pt, sample->aux_sample.data, sample->aux_sample.size);
300 static bool intel_pt_log_events(struct intel_pt *pt, u64 tm)
302 struct perf_time_interval *range = pt->synth_opts.ptime_range;
303 int n = pt->synth_opts.range_num;
305 if (pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
308 if (pt->synth_opts.log_minus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
311 /* perf_time__ranges_skip_sample does not work if time is zero */
315 return !n || !perf_time__ranges_skip_sample(range, n, tm);
318 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs(struct rb_root *rb_root,
322 struct rb_node **p = &rb_root->rb_node;
323 struct rb_node *parent = NULL;
324 struct intel_pt_vmcs_info *v;
328 v = rb_entry(parent, struct intel_pt_vmcs_info, rb_node);
339 v = zalloc(sizeof(*v));
342 v->tsc_offset = dflt_tsc_offset;
343 v->reliable = dflt_tsc_offset;
345 rb_link_node(&v->rb_node, parent, p);
346 rb_insert_color(&v->rb_node, rb_root);
352 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs_info(void *data, uint64_t vmcs)
354 struct intel_pt_queue *ptq = data;
355 struct intel_pt *pt = ptq->pt;
357 if (!vmcs && !pt->dflt_tsc_offset)
360 return intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, pt->dflt_tsc_offset);
363 static void intel_pt_free_vmcs_info(struct intel_pt *pt)
365 struct intel_pt_vmcs_info *v;
368 n = rb_first(&pt->vmcs_info);
370 v = rb_entry(n, struct intel_pt_vmcs_info, rb_node);
372 rb_erase(&v->rb_node, &pt->vmcs_info);
377 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
378 struct auxtrace_buffer *b)
380 bool consecutive = false;
383 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
384 pt->have_tsc, &consecutive,
385 pt->synth_opts.vm_time_correlation);
389 * In the case of vm_time_correlation, the overlap might contain TSC
390 * packets that will not be fixed, and that will then no longer work for
391 * overlap detection. Avoid that by zeroing out the overlap.
393 if (pt->synth_opts.vm_time_correlation)
394 memset(b->data, 0, start - b->data);
395 b->use_size = b->data + b->size - start;
397 if (b->use_size && consecutive)
398 b->consecutive = true;
402 static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
403 struct auxtrace_buffer *buffer,
404 struct auxtrace_buffer *old_buffer,
405 struct intel_pt_buffer *b)
410 int fd = perf_data__fd(ptq->pt->session->data);
412 buffer->data = auxtrace_buffer__get_data(buffer, fd);
417 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
418 if (might_overlap && !buffer->consecutive && old_buffer &&
419 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
422 if (buffer->use_data) {
423 b->len = buffer->use_size;
424 b->buf = buffer->use_data;
426 b->len = buffer->size;
427 b->buf = buffer->data;
429 b->ref_timestamp = buffer->reference;
431 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
432 b->consecutive = false;
433 b->trace_nr = buffer->buffer_nr + 1;
435 b->consecutive = true;
441 /* Do not drop buffers with references - refer intel_pt_get_trace() */
442 static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
443 struct auxtrace_buffer *buffer)
445 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
448 auxtrace_buffer__drop_data(buffer);
451 /* Must be serialized with respect to intel_pt_get_trace() */
452 static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
455 struct intel_pt_queue *ptq = data;
456 struct auxtrace_buffer *buffer = ptq->buffer;
457 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
458 struct auxtrace_queue *queue;
461 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
464 struct intel_pt_buffer b = { .len = 0 };
466 buffer = auxtrace_buffer__next(queue, buffer);
470 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
475 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
478 intel_pt_lookahead_drop_buffer(ptq, buffer);
482 err = cb(&b, cb_data);
487 if (buffer != old_buffer)
488 intel_pt_lookahead_drop_buffer(ptq, buffer);
489 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
495 * This function assumes data is processed sequentially only.
496 * Must be serialized with respect to intel_pt_lookahead()
498 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
500 struct intel_pt_queue *ptq = data;
501 struct auxtrace_buffer *buffer = ptq->buffer;
502 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
503 struct auxtrace_queue *queue;
511 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
513 buffer = auxtrace_buffer__next(queue, buffer);
516 auxtrace_buffer__drop_data(old_buffer);
521 ptq->buffer = buffer;
523 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
527 if (ptq->step_through_buffers)
532 auxtrace_buffer__drop_data(old_buffer);
533 ptq->old_buffer = buffer;
535 auxtrace_buffer__drop_data(buffer);
536 return intel_pt_get_trace(b, data);
542 struct intel_pt_cache_entry {
543 struct auxtrace_cache_entry entry;
546 enum intel_pt_insn_op op;
547 enum intel_pt_insn_branch branch;
548 bool emulated_ptwrite;
551 char insn[INTEL_PT_INSN_BUF_SZ];
554 static int intel_pt_config_div(const char *var, const char *value, void *data)
559 if (!strcmp(var, "intel-pt.cache-divisor")) {
560 val = strtol(value, NULL, 0);
561 if (val > 0 && val <= INT_MAX)
568 static int intel_pt_cache_divisor(void)
575 perf_config(intel_pt_config_div, &d);
583 static unsigned int intel_pt_cache_size(struct dso *dso,
584 struct machine *machine)
588 size = dso__data_size(dso, machine);
589 size /= intel_pt_cache_divisor();
592 if (size > (1 << 21))
594 return 32 - __builtin_clz(size);
597 static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
598 struct machine *machine)
600 struct auxtrace_cache *c;
603 if (dso__auxtrace_cache(dso))
604 return dso__auxtrace_cache(dso);
606 bits = intel_pt_cache_size(dso, machine);
608 /* Ignoring cache creation failure */
609 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
611 dso__set_auxtrace_cache(dso, c);
616 static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
617 u64 offset, u64 insn_cnt, u64 byte_cnt,
618 struct intel_pt_insn *intel_pt_insn)
620 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
621 struct intel_pt_cache_entry *e;
627 e = auxtrace_cache__alloc_entry(c);
631 e->insn_cnt = insn_cnt;
632 e->byte_cnt = byte_cnt;
633 e->op = intel_pt_insn->op;
634 e->branch = intel_pt_insn->branch;
635 e->emulated_ptwrite = intel_pt_insn->emulated_ptwrite;
636 e->length = intel_pt_insn->length;
637 e->rel = intel_pt_insn->rel;
638 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
640 err = auxtrace_cache__add(c, offset, &e->entry);
642 auxtrace_cache__free_entry(c, e);
647 static struct intel_pt_cache_entry *
648 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
650 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
655 return auxtrace_cache__lookup(dso__auxtrace_cache(dso), offset);
658 static void intel_pt_cache_invalidate(struct dso *dso, struct machine *machine,
661 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
666 auxtrace_cache__remove(dso__auxtrace_cache(dso), offset);
669 static inline bool intel_pt_guest_kernel_ip(uint64_t ip)
671 /* Assumes 64-bit kernel */
672 return ip & (1ULL << 63);
675 static inline u8 intel_pt_nr_cpumode(struct intel_pt_queue *ptq, uint64_t ip, bool nr)
678 return intel_pt_guest_kernel_ip(ip) ?
679 PERF_RECORD_MISC_GUEST_KERNEL :
680 PERF_RECORD_MISC_GUEST_USER;
683 return ip >= ptq->pt->kernel_start ?
684 PERF_RECORD_MISC_KERNEL :
685 PERF_RECORD_MISC_USER;
688 static inline u8 intel_pt_cpumode(struct intel_pt_queue *ptq, uint64_t from_ip, uint64_t to_ip)
690 /* No support for non-zero CS base */
692 return intel_pt_nr_cpumode(ptq, from_ip, ptq->state->from_nr);
693 return intel_pt_nr_cpumode(ptq, to_ip, ptq->state->to_nr);
696 static int intel_pt_get_guest(struct intel_pt_queue *ptq)
698 struct machines *machines = &ptq->pt->session->machines;
699 struct machine *machine;
700 pid_t pid = ptq->pid <= 0 ? DEFAULT_GUEST_KERNEL_ID : ptq->pid;
702 if (ptq->guest_machine && pid == ptq->guest_machine->pid)
705 ptq->guest_machine = NULL;
706 thread__zput(ptq->unknown_guest_thread);
708 if (symbol_conf.guest_code) {
709 thread__zput(ptq->guest_thread);
710 ptq->guest_thread = machines__findnew_guest_code(machines, pid);
713 machine = machines__find_guest(machines, pid);
717 ptq->unknown_guest_thread = machine__idle_thread(machine);
718 if (!ptq->unknown_guest_thread)
721 ptq->guest_machine = machine;
726 static inline bool intel_pt_jmp_16(struct intel_pt_insn *intel_pt_insn)
728 return intel_pt_insn->rel == 16 && intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL;
731 #define PTWRITE_MAGIC "\x0f\x0bperf,ptwrite "
732 #define PTWRITE_MAGIC_LEN 16
734 static bool intel_pt_emulated_ptwrite(struct dso *dso, struct machine *machine, u64 offset)
736 unsigned char buf[PTWRITE_MAGIC_LEN];
739 len = dso__data_read_offset(dso, machine, offset, buf, PTWRITE_MAGIC_LEN);
740 if (len == PTWRITE_MAGIC_LEN && !memcmp(buf, PTWRITE_MAGIC, PTWRITE_MAGIC_LEN)) {
741 intel_pt_log("Emulated ptwrite signature found\n");
744 intel_pt_log("Emulated ptwrite signature not found\n");
748 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
749 uint64_t *insn_cnt_ptr, uint64_t *ip,
750 uint64_t to_ip, uint64_t max_insn_cnt,
753 struct intel_pt_queue *ptq = data;
754 struct machine *machine = ptq->pt->machine;
755 struct thread *thread;
756 struct addr_location al;
757 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
761 u64 offset, start_offset, start_ip;
767 addr_location__init(&al);
768 intel_pt_insn->length = 0;
769 intel_pt_insn->op = INTEL_PT_OP_OTHER;
771 if (to_ip && *ip == to_ip)
774 nr = ptq->state->to_nr;
775 cpumode = intel_pt_nr_cpumode(ptq, *ip, nr);
778 if (ptq->pt->have_guest_sideband) {
779 if (!ptq->guest_machine || ptq->guest_machine_pid != ptq->pid) {
780 intel_pt_log("ERROR: guest sideband but no guest machine\n");
784 } else if ((!symbol_conf.guest_code && cpumode != PERF_RECORD_MISC_GUEST_KERNEL) ||
785 intel_pt_get_guest(ptq)) {
786 intel_pt_log("ERROR: no guest machine\n");
790 machine = ptq->guest_machine;
791 thread = ptq->guest_thread;
793 if (cpumode != PERF_RECORD_MISC_GUEST_KERNEL) {
794 intel_pt_log("ERROR: no guest thread\n");
798 thread = ptq->unknown_guest_thread;
801 thread = ptq->thread;
803 if (cpumode != PERF_RECORD_MISC_KERNEL) {
804 intel_pt_log("ERROR: no thread\n");
808 thread = ptq->pt->unknown_thread;
815 if (!thread__find_map(thread, cpumode, *ip, &al) || !map__dso(al.map)) {
817 intel_pt_log("ERROR: thread has no dso for %#" PRIx64 "\n", *ip);
819 intel_pt_log("ERROR: thread has no map for %#" PRIx64 "\n", *ip);
820 addr_location__exit(&al);
824 dso = map__dso(al.map);
826 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR &&
827 dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) {
832 offset = map__map_ip(al.map, *ip);
834 if (!to_ip && one_map) {
835 struct intel_pt_cache_entry *e;
837 e = intel_pt_cache_lookup(dso, machine, offset);
839 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
840 *insn_cnt_ptr = e->insn_cnt;
842 intel_pt_insn->op = e->op;
843 intel_pt_insn->branch = e->branch;
844 intel_pt_insn->emulated_ptwrite = e->emulated_ptwrite;
845 intel_pt_insn->length = e->length;
846 intel_pt_insn->rel = e->rel;
847 memcpy(intel_pt_insn->buf, e->insn, INTEL_PT_INSN_BUF_SZ);
848 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
854 start_offset = offset;
857 /* Load maps to ensure dso->is_64_bit has been updated */
860 x86_64 = dso__is_64_bit(dso);
863 len = dso__data_read_offset(dso, machine,
865 INTEL_PT_INSN_BUF_SZ);
867 intel_pt_log("ERROR: failed to read at offset %#" PRIx64 " ",
869 if (intel_pt_enable_logging)
870 dso__fprintf(dso, intel_pt_log_fp());
875 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn)) {
880 intel_pt_log_insn(intel_pt_insn, *ip);
884 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH) {
888 if (!intel_pt_jmp_16(intel_pt_insn))
890 /* Check for emulated ptwrite */
891 offs = offset + intel_pt_insn->length;
892 eptw = intel_pt_emulated_ptwrite(dso, machine, offs);
893 intel_pt_insn->emulated_ptwrite = eptw;
897 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
900 *ip += intel_pt_insn->length;
902 if (to_ip && *ip == to_ip) {
903 intel_pt_insn->length = 0;
904 intel_pt_insn->op = INTEL_PT_OP_OTHER;
908 if (*ip >= map__end(al.map))
911 offset += intel_pt_insn->length;
916 *insn_cnt_ptr = insn_cnt;
922 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
926 struct intel_pt_cache_entry *e;
928 e = intel_pt_cache_lookup(map__dso(al.map), machine, start_offset);
933 /* Ignore cache errors */
934 intel_pt_cache_add(map__dso(al.map), machine, start_offset, insn_cnt,
935 *ip - start_ip, intel_pt_insn);
938 addr_location__exit(&al);
942 *insn_cnt_ptr = insn_cnt;
943 addr_location__exit(&al);
947 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
948 uint64_t offset, const char *filename)
950 struct addr_filter *filt;
951 bool have_filter = false;
952 bool hit_tracestop = false;
953 bool hit_filter = false;
955 list_for_each_entry(filt, &pt->filts.head, list) {
959 if ((filename && !filt->filename) ||
960 (!filename && filt->filename) ||
961 (filename && strcmp(filename, filt->filename)))
964 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
967 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
968 ip, offset, filename ? filename : "[kernel]",
969 filt->start ? "filter" : "stop",
970 filt->addr, filt->size);
975 hit_tracestop = true;
978 if (!hit_tracestop && !hit_filter)
979 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
980 ip, offset, filename ? filename : "[kernel]");
982 return hit_tracestop || (have_filter && !hit_filter);
985 static int __intel_pt_pgd_ip(uint64_t ip, void *data)
987 struct intel_pt_queue *ptq = data;
988 struct thread *thread;
989 struct addr_location al;
994 if (ptq->state->to_nr) {
995 if (intel_pt_guest_kernel_ip(ip))
996 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
997 /* No support for decoding guest user space */
999 } else if (ip >= ptq->pt->kernel_start) {
1000 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
1003 cpumode = PERF_RECORD_MISC_USER;
1005 thread = ptq->thread;
1009 addr_location__init(&al);
1010 if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map))
1013 offset = map__map_ip(al.map, ip);
1015 res = intel_pt_match_pgd_ip(ptq->pt, ip, offset, dso__long_name(map__dso(al.map)));
1016 addr_location__exit(&al);
1020 static bool intel_pt_pgd_ip(uint64_t ip, void *data)
1022 return __intel_pt_pgd_ip(ip, data) > 0;
1025 static bool intel_pt_get_config(struct intel_pt *pt,
1026 struct perf_event_attr *attr, u64 *config)
1028 if (attr->type == pt->pmu_type) {
1030 *config = attr->config;
1037 static bool intel_pt_exclude_kernel(struct intel_pt *pt)
1039 struct evsel *evsel;
1041 evlist__for_each_entry(pt->session->evlist, evsel) {
1042 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
1043 !evsel->core.attr.exclude_kernel)
1049 static bool intel_pt_return_compression(struct intel_pt *pt)
1051 struct evsel *evsel;
1054 if (!pt->noretcomp_bit)
1057 evlist__for_each_entry(pt->session->evlist, evsel) {
1058 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1059 (config & pt->noretcomp_bit))
1065 static bool intel_pt_branch_enable(struct intel_pt *pt)
1067 struct evsel *evsel;
1070 evlist__for_each_entry(pt->session->evlist, evsel) {
1071 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1072 (config & INTEL_PT_CFG_PASS_THRU) &&
1073 !(config & INTEL_PT_CFG_BRANCH_EN))
1079 static bool intel_pt_disabled_tnt(struct intel_pt *pt)
1081 struct evsel *evsel;
1084 evlist__for_each_entry(pt->session->evlist, evsel) {
1085 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1086 config & INTEL_PT_CFG_TNT_DIS)
1092 static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
1094 struct evsel *evsel;
1098 if (!pt->mtc_freq_bits)
1101 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
1104 evlist__for_each_entry(pt->session->evlist, evsel) {
1105 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
1106 return (config & pt->mtc_freq_bits) >> shift;
1111 static bool intel_pt_timeless_decoding(struct intel_pt *pt)
1113 struct evsel *evsel;
1114 bool timeless_decoding = true;
1117 if (!pt->tsc_bit || !pt->cap_user_time_zero || pt->synth_opts.timeless_decoding)
1120 evlist__for_each_entry(pt->session->evlist, evsel) {
1121 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
1123 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
1124 if (config & pt->tsc_bit)
1125 timeless_decoding = false;
1130 return timeless_decoding;
1133 static bool intel_pt_tracing_kernel(struct intel_pt *pt)
1135 struct evsel *evsel;
1137 evlist__for_each_entry(pt->session->evlist, evsel) {
1138 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
1139 !evsel->core.attr.exclude_kernel)
1145 static bool intel_pt_have_tsc(struct intel_pt *pt)
1147 struct evsel *evsel;
1148 bool have_tsc = false;
1154 evlist__for_each_entry(pt->session->evlist, evsel) {
1155 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
1156 if (config & pt->tsc_bit)
1165 static bool intel_pt_have_mtc(struct intel_pt *pt)
1167 struct evsel *evsel;
1170 evlist__for_each_entry(pt->session->evlist, evsel) {
1171 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1172 (config & pt->mtc_bit))
1178 static bool intel_pt_sampling_mode(struct intel_pt *pt)
1180 struct evsel *evsel;
1182 evlist__for_each_entry(pt->session->evlist, evsel) {
1183 if ((evsel->core.attr.sample_type & PERF_SAMPLE_AUX) &&
1184 evsel->core.attr.aux_sample_size)
1190 static u64 intel_pt_ctl(struct intel_pt *pt)
1192 struct evsel *evsel;
1195 evlist__for_each_entry(pt->session->evlist, evsel) {
1196 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
1202 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
1206 quot = ns / pt->tc.time_mult;
1207 rem = ns % pt->tc.time_mult;
1208 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
1212 static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt)
1214 size_t sz = sizeof(struct ip_callchain);
1216 /* Add 1 to callchain_sz for callchain context */
1217 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
1221 static int intel_pt_callchain_init(struct intel_pt *pt)
1223 struct evsel *evsel;
1225 evlist__for_each_entry(pt->session->evlist, evsel) {
1226 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN))
1227 evsel->synth_sample_type |= PERF_SAMPLE_CALLCHAIN;
1230 pt->chain = intel_pt_alloc_chain(pt);
1237 static void intel_pt_add_callchain(struct intel_pt *pt,
1238 struct perf_sample *sample)
1240 struct thread *thread = machine__findnew_thread(pt->machine,
1244 thread_stack__sample_late(thread, sample->cpu, pt->chain,
1245 pt->synth_opts.callchain_sz + 1, sample->ip,
1248 sample->callchain = pt->chain;
1251 static struct branch_stack *intel_pt_alloc_br_stack(unsigned int entry_cnt)
1253 size_t sz = sizeof(struct branch_stack);
1255 sz += entry_cnt * sizeof(struct branch_entry);
1259 static int intel_pt_br_stack_init(struct intel_pt *pt)
1261 struct evsel *evsel;
1263 evlist__for_each_entry(pt->session->evlist, evsel) {
1264 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK))
1265 evsel->synth_sample_type |= PERF_SAMPLE_BRANCH_STACK;
1268 pt->br_stack = intel_pt_alloc_br_stack(pt->br_stack_sz);
1275 static void intel_pt_add_br_stack(struct intel_pt *pt,
1276 struct perf_sample *sample)
1278 struct thread *thread = machine__findnew_thread(pt->machine,
1282 thread_stack__br_sample_late(thread, sample->cpu, pt->br_stack,
1283 pt->br_stack_sz, sample->ip,
1286 sample->branch_stack = pt->br_stack;
1287 thread__put(thread);
1290 /* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1291 #define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U)
1293 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
1294 unsigned int queue_nr)
1296 struct intel_pt_params params = { .get_trace = 0, };
1297 struct perf_env *env = pt->machine->env;
1298 struct intel_pt_queue *ptq;
1300 ptq = zalloc(sizeof(struct intel_pt_queue));
1304 if (pt->synth_opts.callchain) {
1305 ptq->chain = intel_pt_alloc_chain(pt);
1310 if (pt->synth_opts.last_branch || pt->synth_opts.other_events) {
1311 unsigned int entry_cnt = max(LBRS_MAX, pt->br_stack_sz);
1313 ptq->last_branch = intel_pt_alloc_br_stack(entry_cnt);
1314 if (!ptq->last_branch)
1318 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
1319 if (!ptq->event_buf)
1323 ptq->queue_nr = queue_nr;
1324 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
1330 params.get_trace = intel_pt_get_trace;
1331 params.walk_insn = intel_pt_walk_next_insn;
1332 params.lookahead = intel_pt_lookahead;
1333 params.findnew_vmcs_info = intel_pt_findnew_vmcs_info;
1335 params.return_compression = intel_pt_return_compression(pt);
1336 params.branch_enable = intel_pt_branch_enable(pt);
1337 params.ctl = intel_pt_ctl(pt);
1338 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
1339 params.mtc_period = intel_pt_mtc_period(pt);
1340 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
1341 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
1342 params.quick = pt->synth_opts.quick;
1343 params.vm_time_correlation = pt->synth_opts.vm_time_correlation;
1344 params.vm_tm_corr_dry_run = pt->synth_opts.vm_tm_corr_dry_run;
1345 params.first_timestamp = pt->first_timestamp;
1346 params.max_loops = pt->max_loops;
1348 /* Cannot walk code without TNT, so force 'quick' mode */
1349 if (params.branch_enable && intel_pt_disabled_tnt(pt) && !params.quick)
1352 if (pt->filts.cnt > 0)
1353 params.pgd_ip = intel_pt_pgd_ip;
1355 if (pt->synth_opts.instructions || pt->synth_opts.cycles) {
1356 if (pt->synth_opts.period) {
1357 switch (pt->synth_opts.period_type) {
1358 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
1359 params.period_type =
1360 INTEL_PT_PERIOD_INSTRUCTIONS;
1361 params.period = pt->synth_opts.period;
1363 case PERF_ITRACE_PERIOD_TICKS:
1364 params.period_type = INTEL_PT_PERIOD_TICKS;
1365 params.period = pt->synth_opts.period;
1367 case PERF_ITRACE_PERIOD_NANOSECS:
1368 params.period_type = INTEL_PT_PERIOD_TICKS;
1369 params.period = intel_pt_ns_to_ticks(pt,
1370 pt->synth_opts.period);
1377 if (!params.period) {
1378 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
1383 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
1384 params.flags |= INTEL_PT_FUP_WITH_NLIP;
1386 ptq->decoder = intel_pt_decoder_new(¶ms);
1393 zfree(&ptq->event_buf);
1394 zfree(&ptq->last_branch);
1400 static void intel_pt_free_queue(void *priv)
1402 struct intel_pt_queue *ptq = priv;
1406 thread__zput(ptq->thread);
1407 thread__zput(ptq->guest_thread);
1408 thread__zput(ptq->unknown_guest_thread);
1409 intel_pt_decoder_free(ptq->decoder);
1410 zfree(&ptq->event_buf);
1411 zfree(&ptq->last_branch);
1416 static void intel_pt_first_timestamp(struct intel_pt *pt, u64 timestamp)
1420 pt->first_timestamp = timestamp;
1422 for (i = 0; i < pt->queues.nr_queues; i++) {
1423 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1424 struct intel_pt_queue *ptq = queue->priv;
1426 if (ptq && ptq->decoder)
1427 intel_pt_set_first_timestamp(ptq->decoder, timestamp);
1431 static int intel_pt_get_guest_from_sideband(struct intel_pt_queue *ptq)
1433 struct machines *machines = &ptq->pt->session->machines;
1434 struct machine *machine;
1435 pid_t machine_pid = ptq->pid;
1439 if (machine_pid <= 0)
1440 return 0; /* Not a guest machine */
1442 machine = machines__find(machines, machine_pid);
1444 return 0; /* Not a guest machine */
1446 if (ptq->guest_machine != machine) {
1447 ptq->guest_machine = NULL;
1448 thread__zput(ptq->guest_thread);
1449 thread__zput(ptq->unknown_guest_thread);
1451 ptq->unknown_guest_thread = machine__find_thread(machine, 0, 0);
1452 if (!ptq->unknown_guest_thread)
1454 ptq->guest_machine = machine;
1457 vcpu = ptq->thread ? thread__guest_cpu(ptq->thread) : -1;
1461 tid = machine__get_current_tid(machine, vcpu);
1463 if (ptq->guest_thread && thread__tid(ptq->guest_thread) != tid)
1464 thread__zput(ptq->guest_thread);
1466 if (!ptq->guest_thread) {
1467 ptq->guest_thread = machine__find_thread(machine, -1, tid);
1468 if (!ptq->guest_thread)
1472 ptq->guest_machine_pid = machine_pid;
1473 ptq->guest_pid = thread__pid(ptq->guest_thread);
1474 ptq->guest_tid = tid;
1480 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
1481 struct auxtrace_queue *queue)
1483 struct intel_pt_queue *ptq = queue->priv;
1485 if (queue->tid == -1 || pt->have_sched_switch) {
1486 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
1489 thread__zput(ptq->thread);
1492 if (!ptq->thread && ptq->tid != -1)
1493 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
1496 ptq->pid = thread__pid(ptq->thread);
1497 if (queue->cpu == -1)
1498 ptq->cpu = thread__cpu(ptq->thread);
1501 if (pt->have_guest_sideband && intel_pt_get_guest_from_sideband(ptq)) {
1502 ptq->guest_machine_pid = 0;
1503 ptq->guest_pid = -1;
1504 ptq->guest_tid = -1;
1509 static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
1511 struct intel_pt *pt = ptq->pt;
1514 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
1515 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
1516 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
1517 if (!ptq->state->to_ip)
1518 ptq->flags = PERF_IP_FLAG_BRANCH |
1519 PERF_IP_FLAG_ASYNC |
1520 PERF_IP_FLAG_TRACE_END;
1521 else if (ptq->state->from_nr && !ptq->state->to_nr)
1522 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1523 PERF_IP_FLAG_ASYNC |
1524 PERF_IP_FLAG_VMEXIT;
1526 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1527 PERF_IP_FLAG_ASYNC |
1528 PERF_IP_FLAG_INTERRUPT;
1530 if (ptq->state->from_ip)
1531 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1533 ptq->flags = PERF_IP_FLAG_BRANCH |
1534 PERF_IP_FLAG_TRACE_BEGIN;
1535 if (ptq->state->flags & INTEL_PT_IN_TX)
1536 ptq->flags |= PERF_IP_FLAG_IN_TX;
1537 ptq->insn_len = ptq->state->insn_len;
1538 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
1541 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1542 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1543 if (ptq->state->type & INTEL_PT_TRACE_END)
1544 ptq->flags |= PERF_IP_FLAG_TRACE_END;
1546 if (pt->cap_event_trace) {
1547 if (ptq->state->type & INTEL_PT_IFLAG_CHG) {
1548 if (!ptq->state->from_iflag)
1549 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE;
1550 if (ptq->state->from_iflag != ptq->state->to_iflag)
1551 ptq->flags |= PERF_IP_FLAG_INTR_TOGGLE;
1552 } else if (!ptq->state->to_iflag) {
1553 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE;
1558 static void intel_pt_setup_time_range(struct intel_pt *pt,
1559 struct intel_pt_queue *ptq)
1564 ptq->sel_timestamp = pt->time_ranges[0].start;
1567 if (ptq->sel_timestamp) {
1568 ptq->sel_start = true;
1570 ptq->sel_timestamp = pt->time_ranges[0].end;
1571 ptq->sel_start = false;
1575 static int intel_pt_setup_queue(struct intel_pt *pt,
1576 struct auxtrace_queue *queue,
1577 unsigned int queue_nr)
1579 struct intel_pt_queue *ptq = queue->priv;
1581 if (list_empty(&queue->head))
1585 ptq = intel_pt_alloc_queue(pt, queue_nr);
1590 if (queue->cpu != -1)
1591 ptq->cpu = queue->cpu;
1592 ptq->tid = queue->tid;
1594 ptq->cbr_seen = UINT_MAX;
1596 if (pt->sampling_mode && !pt->snapshot_mode &&
1597 pt->timeless_decoding)
1598 ptq->step_through_buffers = true;
1600 ptq->sync_switch = pt->sync_switch;
1602 intel_pt_setup_time_range(pt, ptq);
1605 if (!ptq->on_heap &&
1606 (!ptq->sync_switch ||
1607 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1608 const struct intel_pt_state *state;
1611 if (pt->timeless_decoding)
1614 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1615 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1616 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1618 if (ptq->sel_start && ptq->sel_timestamp) {
1619 ret = intel_pt_fast_forward(ptq->decoder,
1620 ptq->sel_timestamp);
1626 state = intel_pt_decode(ptq->decoder);
1628 if (state->err == INTEL_PT_ERR_NODATA) {
1629 intel_pt_log("queue %u has no timestamp\n",
1635 if (state->timestamp)
1639 ptq->timestamp = state->timestamp;
1640 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1641 queue_nr, ptq->timestamp);
1643 ptq->have_sample = true;
1644 if (ptq->sel_start && ptq->sel_timestamp &&
1645 ptq->timestamp < ptq->sel_timestamp)
1646 ptq->have_sample = false;
1647 intel_pt_sample_flags(ptq);
1648 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1651 ptq->on_heap = true;
1657 static int intel_pt_setup_queues(struct intel_pt *pt)
1662 for (i = 0; i < pt->queues.nr_queues; i++) {
1663 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1670 static inline bool intel_pt_skip_event(struct intel_pt *pt)
1672 return pt->synth_opts.initial_skip &&
1673 pt->num_events++ < pt->synth_opts.initial_skip;
1677 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
1678 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
1679 * from this decoder state.
1681 static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
1683 return pt->synth_opts.initial_skip &&
1684 pt->num_events + 4 < pt->synth_opts.initial_skip;
1687 static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1688 union perf_event *event,
1689 struct perf_sample *sample)
1691 event->sample.header.type = PERF_RECORD_SAMPLE;
1692 event->sample.header.size = sizeof(struct perf_event_header);
1694 sample->pid = ptq->pid;
1695 sample->tid = ptq->tid;
1697 if (ptq->pt->have_guest_sideband) {
1698 if ((ptq->state->from_ip && ptq->state->from_nr) ||
1699 (ptq->state->to_ip && ptq->state->to_nr)) {
1700 sample->pid = ptq->guest_pid;
1701 sample->tid = ptq->guest_tid;
1702 sample->machine_pid = ptq->guest_machine_pid;
1703 sample->vcpu = ptq->vcpu;
1707 sample->cpu = ptq->cpu;
1708 sample->insn_len = ptq->insn_len;
1709 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1712 static void intel_pt_prep_b_sample(struct intel_pt *pt,
1713 struct intel_pt_queue *ptq,
1714 union perf_event *event,
1715 struct perf_sample *sample)
1717 intel_pt_prep_a_sample(ptq, event, sample);
1719 if (!pt->timeless_decoding)
1720 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1722 sample->ip = ptq->state->from_ip;
1723 sample->addr = ptq->state->to_ip;
1724 sample->cpumode = intel_pt_cpumode(ptq, sample->ip, sample->addr);
1726 sample->flags = ptq->flags;
1728 event->sample.header.misc = sample->cpumode;
1731 static int intel_pt_inject_event(union perf_event *event,
1732 struct perf_sample *sample, u64 type)
1734 event->header.size = perf_event__sample_event_size(sample, type, 0);
1735 return perf_event__synthesize_sample(event, type, 0, sample);
1738 static inline int intel_pt_opt_inject(struct intel_pt *pt,
1739 union perf_event *event,
1740 struct perf_sample *sample, u64 type)
1742 if (!pt->synth_opts.inject)
1745 return intel_pt_inject_event(event, sample, type);
1748 static int intel_pt_deliver_synth_event(struct intel_pt *pt,
1749 union perf_event *event,
1750 struct perf_sample *sample, u64 type)
1754 ret = intel_pt_opt_inject(pt, event, sample, type);
1758 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1760 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1765 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1767 struct intel_pt *pt = ptq->pt;
1768 union perf_event *event = ptq->event_buf;
1769 struct perf_sample sample;
1770 struct dummy_branch_stack {
1773 struct branch_entry entries;
1777 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1780 if (intel_pt_skip_event(pt))
1783 perf_sample__init(&sample, /*all=*/true);
1784 intel_pt_prep_b_sample(pt, ptq, event, &sample);
1786 sample.id = ptq->pt->branches_id;
1787 sample.stream_id = ptq->pt->branches_id;
1790 * perf report cannot handle events without a branch stack when using
1791 * SORT_MODE__BRANCH so make a dummy one.
1793 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1794 dummy_bs = (struct dummy_branch_stack){
1802 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1805 if (ptq->sample_ipc)
1806 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
1807 if (sample.cyc_cnt) {
1808 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1809 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1810 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1813 perf_sample__exit(&sample);
1814 ret = intel_pt_deliver_synth_event(pt, event, &sample,
1815 pt->branches_sample_type);
1819 static void intel_pt_prep_sample(struct intel_pt *pt,
1820 struct intel_pt_queue *ptq,
1821 union perf_event *event,
1822 struct perf_sample *sample)
1824 intel_pt_prep_b_sample(pt, ptq, event, sample);
1826 if (pt->synth_opts.callchain) {
1827 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1828 pt->synth_opts.callchain_sz + 1,
1829 sample->ip, pt->kernel_start);
1830 sample->callchain = ptq->chain;
1833 if (pt->synth_opts.last_branch) {
1834 thread_stack__br_sample(ptq->thread, ptq->cpu, ptq->last_branch,
1836 sample->branch_stack = ptq->last_branch;
1840 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1842 struct intel_pt *pt = ptq->pt;
1843 union perf_event *event = ptq->event_buf;
1844 struct perf_sample sample;
1847 if (intel_pt_skip_event(pt))
1850 perf_sample__init(&sample, /*all=*/true);
1851 intel_pt_prep_sample(pt, ptq, event, &sample);
1853 sample.id = ptq->pt->instructions_id;
1854 sample.stream_id = ptq->pt->instructions_id;
1855 if (pt->synth_opts.quick)
1858 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1860 if (ptq->sample_ipc)
1861 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
1862 if (sample.cyc_cnt) {
1863 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1864 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1865 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1868 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1870 ret = intel_pt_deliver_synth_event(pt, event, &sample,
1871 pt->instructions_sample_type);
1872 perf_sample__exit(&sample);
1876 static int intel_pt_synth_cycle_sample(struct intel_pt_queue *ptq)
1878 struct intel_pt *pt = ptq->pt;
1879 union perf_event *event = ptq->event_buf;
1880 struct perf_sample sample;
1884 if (ptq->sample_ipc)
1885 period = ptq->ipc_cyc_cnt - ptq->last_cy_cyc_cnt;
1887 if (!period || intel_pt_skip_event(pt))
1890 perf_sample__init(&sample, /*all=*/true);
1891 intel_pt_prep_sample(pt, ptq, event, &sample);
1893 sample.id = ptq->pt->cycles_id;
1894 sample.stream_id = ptq->pt->cycles_id;
1895 sample.period = period;
1897 sample.cyc_cnt = period;
1898 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_cy_insn_cnt;
1899 ptq->last_cy_insn_cnt = ptq->ipc_insn_cnt;
1900 ptq->last_cy_cyc_cnt = ptq->ipc_cyc_cnt;
1902 ret = intel_pt_deliver_synth_event(pt, event, &sample, pt->cycles_sample_type);
1903 perf_sample__exit(&sample);
1907 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1909 struct intel_pt *pt = ptq->pt;
1910 union perf_event *event = ptq->event_buf;
1911 struct perf_sample sample;
1914 if (intel_pt_skip_event(pt))
1917 perf_sample__init(&sample, /*all=*/true);
1918 intel_pt_prep_sample(pt, ptq, event, &sample);
1920 sample.id = ptq->pt->transactions_id;
1921 sample.stream_id = ptq->pt->transactions_id;
1923 ret = intel_pt_deliver_synth_event(pt, event, &sample,
1924 pt->transactions_sample_type);
1925 perf_sample__exit(&sample);
1929 static void intel_pt_prep_p_sample(struct intel_pt *pt,
1930 struct intel_pt_queue *ptq,
1931 union perf_event *event,
1932 struct perf_sample *sample)
1934 intel_pt_prep_sample(pt, ptq, event, sample);
1937 * Zero IP is used to mean "trace start" but that is not the case for
1938 * power or PTWRITE events with no IP, so clear the flags.
1944 static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1946 struct intel_pt *pt = ptq->pt;
1947 union perf_event *event = ptq->event_buf;
1948 struct perf_sample sample = { .ip = 0, };
1949 struct perf_synth_intel_ptwrite raw;
1951 if (intel_pt_skip_event(pt))
1954 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1956 sample.id = ptq->pt->ptwrites_id;
1957 sample.stream_id = ptq->pt->ptwrites_id;
1960 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1961 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1963 sample.raw_size = perf_synth__raw_size(raw);
1964 sample.raw_data = perf_synth__raw_data(&raw);
1966 return intel_pt_deliver_synth_event(pt, event, &sample,
1967 pt->ptwrites_sample_type);
1970 static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1972 struct intel_pt *pt = ptq->pt;
1973 union perf_event *event = ptq->event_buf;
1974 struct perf_sample sample;
1975 struct perf_synth_intel_cbr raw;
1979 if (intel_pt_skip_cbr_event(pt))
1982 ptq->cbr_seen = ptq->state->cbr;
1984 perf_sample__init(&sample, /*all=*/true);
1985 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1987 sample.id = ptq->pt->cbr_id;
1988 sample.stream_id = ptq->pt->cbr_id;
1990 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1991 raw.flags = cpu_to_le32(flags);
1992 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1995 sample.raw_size = perf_synth__raw_size(raw);
1996 sample.raw_data = perf_synth__raw_data(&raw);
1998 ret = intel_pt_deliver_synth_event(pt, event, &sample,
1999 pt->pwr_events_sample_type);
2000 perf_sample__exit(&sample);
2004 static int intel_pt_synth_psb_sample(struct intel_pt_queue *ptq)
2006 struct intel_pt *pt = ptq->pt;
2007 union perf_event *event = ptq->event_buf;
2008 struct perf_sample sample;
2009 struct perf_synth_intel_psb raw;
2012 if (intel_pt_skip_event(pt))
2015 perf_sample__init(&sample, /*all=*/true);
2016 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2018 sample.id = ptq->pt->psb_id;
2019 sample.stream_id = ptq->pt->psb_id;
2023 raw.offset = ptq->state->psb_offset;
2025 sample.raw_size = perf_synth__raw_size(raw);
2026 sample.raw_data = perf_synth__raw_data(&raw);
2028 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2029 pt->pwr_events_sample_type);
2030 perf_sample__exit(&sample);
2034 static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
2036 struct intel_pt *pt = ptq->pt;
2037 union perf_event *event = ptq->event_buf;
2038 struct perf_sample sample;
2039 struct perf_synth_intel_mwait raw;
2042 if (intel_pt_skip_event(pt))
2045 perf_sample__init(&sample, /*all=*/true);
2046 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2048 sample.id = ptq->pt->mwait_id;
2049 sample.stream_id = ptq->pt->mwait_id;
2052 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
2054 sample.raw_size = perf_synth__raw_size(raw);
2055 sample.raw_data = perf_synth__raw_data(&raw);
2057 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2058 pt->pwr_events_sample_type);
2059 perf_sample__exit(&sample);
2063 static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
2065 struct intel_pt *pt = ptq->pt;
2066 union perf_event *event = ptq->event_buf;
2067 struct perf_sample sample;
2068 struct perf_synth_intel_pwre raw;
2071 if (intel_pt_skip_event(pt))
2074 perf_sample__init(&sample, /*all=*/true);
2075 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2077 sample.id = ptq->pt->pwre_id;
2078 sample.stream_id = ptq->pt->pwre_id;
2081 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
2083 sample.raw_size = perf_synth__raw_size(raw);
2084 sample.raw_data = perf_synth__raw_data(&raw);
2086 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2087 pt->pwr_events_sample_type);
2088 perf_sample__exit(&sample);
2092 static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
2094 struct intel_pt *pt = ptq->pt;
2095 union perf_event *event = ptq->event_buf;
2096 struct perf_sample sample;
2097 struct perf_synth_intel_exstop raw;
2100 if (intel_pt_skip_event(pt))
2103 perf_sample__init(&sample, /*all=*/true);
2104 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2106 sample.id = ptq->pt->exstop_id;
2107 sample.stream_id = ptq->pt->exstop_id;
2110 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
2112 sample.raw_size = perf_synth__raw_size(raw);
2113 sample.raw_data = perf_synth__raw_data(&raw);
2115 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2116 pt->pwr_events_sample_type);
2117 perf_sample__exit(&sample);
2121 static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
2123 struct intel_pt *pt = ptq->pt;
2124 union perf_event *event = ptq->event_buf;
2125 struct perf_sample sample;
2126 struct perf_synth_intel_pwrx raw;
2129 if (intel_pt_skip_event(pt))
2132 perf_sample__init(&sample, /*all=*/true);
2133 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2135 sample.id = ptq->pt->pwrx_id;
2136 sample.stream_id = ptq->pt->pwrx_id;
2139 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
2141 sample.raw_size = perf_synth__raw_size(raw);
2142 sample.raw_data = perf_synth__raw_data(&raw);
2144 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2145 pt->pwr_events_sample_type);
2146 perf_sample__exit(&sample);
2151 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
2152 * intel_pt_add_gp_regs().
2154 static const int pebs_gp_regs[] = {
2155 [PERF_REG_X86_FLAGS] = 1,
2156 [PERF_REG_X86_IP] = 2,
2157 [PERF_REG_X86_AX] = 3,
2158 [PERF_REG_X86_CX] = 4,
2159 [PERF_REG_X86_DX] = 5,
2160 [PERF_REG_X86_BX] = 6,
2161 [PERF_REG_X86_SP] = 7,
2162 [PERF_REG_X86_BP] = 8,
2163 [PERF_REG_X86_SI] = 9,
2164 [PERF_REG_X86_DI] = 10,
2165 [PERF_REG_X86_R8] = 11,
2166 [PERF_REG_X86_R9] = 12,
2167 [PERF_REG_X86_R10] = 13,
2168 [PERF_REG_X86_R11] = 14,
2169 [PERF_REG_X86_R12] = 15,
2170 [PERF_REG_X86_R13] = 16,
2171 [PERF_REG_X86_R14] = 17,
2172 [PERF_REG_X86_R15] = 18,
2175 static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
2176 const struct intel_pt_blk_items *items,
2179 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
2180 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
2184 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
2185 /* Get the PEBS gp_regs array index */
2186 int n = pebs_gp_regs[i] - 1;
2191 * Add only registers that were requested (i.e. 'regs_mask') and
2192 * that were provided (i.e. 'mask'), and update the resulting
2193 * mask (i.e. 'intr_regs->mask') accordingly.
2195 if (mask & 1 << n && regs_mask & bit) {
2196 intr_regs->mask |= bit;
2197 *pos++ = gp_regs[n];
2204 #ifndef PERF_REG_X86_XMM0
2205 #define PERF_REG_X86_XMM0 32
2208 static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
2209 const struct intel_pt_blk_items *items,
2212 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
2213 const u64 *xmm = items->xmm;
2216 * If there are any XMM registers, then there should be all of them.
2217 * Nevertheless, follow the logic to add only registers that were
2218 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
2219 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
2221 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
2223 for (; mask; mask >>= 1, xmm++) {
2229 #define LBR_INFO_MISPRED (1ULL << 63)
2230 #define LBR_INFO_IN_TX (1ULL << 62)
2231 #define LBR_INFO_ABORT (1ULL << 61)
2232 #define LBR_INFO_CYCLES 0xffff
2234 /* Refer kernel's intel_pmu_store_pebs_lbrs() */
2235 static u64 intel_pt_lbr_flags(u64 info)
2238 struct branch_flags flags;
2243 u.flags.mispred = !!(info & LBR_INFO_MISPRED);
2244 u.flags.predicted = !(info & LBR_INFO_MISPRED);
2245 u.flags.in_tx = !!(info & LBR_INFO_IN_TX);
2246 u.flags.abort = !!(info & LBR_INFO_ABORT);
2247 u.flags.cycles = info & LBR_INFO_CYCLES;
2252 static void intel_pt_add_lbrs(struct branch_stack *br_stack,
2253 const struct intel_pt_blk_items *items)
2260 to = &br_stack->entries[0].from;
2262 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
2263 u32 mask = items->mask[i];
2264 const u64 *from = items->val[i];
2266 for (; mask; mask >>= 3, from += 3) {
2267 if ((mask & 7) == 7) {
2270 *to++ = intel_pt_lbr_flags(from[2]);
2277 #define P(a, b) PERF_MEM_S(a, b)
2278 #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
2279 #define LEVEL(x) P(LVLNUM, x)
2280 #define REM P(REMOTE, REMOTE)
2281 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
2283 #define PERF_PEBS_DATA_SOURCE_GRT_MAX 0x10
2284 #define PERF_PEBS_DATA_SOURCE_GRT_MASK (PERF_PEBS_DATA_SOURCE_GRT_MAX - 1)
2286 /* Based on kernel __intel_pmu_pebs_data_source_grt() and pebs_data_source */
2287 static const u64 pebs_data_source_grt[PERF_PEBS_DATA_SOURCE_GRT_MAX] = {
2288 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA), /* L3 miss|SNP N/A */
2289 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* L1 hit|SNP None */
2290 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* LFB/MAB hit|SNP None */
2291 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* L2 hit|SNP None */
2292 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* L3 hit|SNP None */
2293 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* L3 hit|SNP Hit */
2294 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* L3 hit|SNP HitM */
2295 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* L3 hit|SNP HitM */
2296 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD), /* L3 hit|SNP Fwd */
2297 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* Remote L3 hit|SNP HitM */
2298 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* RAM hit|SNP Hit */
2299 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* Remote L3 hit|SNP Hit */
2300 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* RAM hit|SNP None or Miss */
2301 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* Remote RAM hit|SNP None or Miss */
2302 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* I/O hit|SNP None */
2303 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* Uncached hit|SNP None */
2306 /* Based on kernel __intel_pmu_pebs_data_source_cmt() and pebs_data_source */
2307 static const u64 pebs_data_source_cmt[PERF_PEBS_DATA_SOURCE_GRT_MAX] = {
2308 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA), /* L3 miss|SNP N/A */
2309 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* L1 hit|SNP None */
2310 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* LFB/MAB hit|SNP None */
2311 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* L2 hit|SNP None */
2312 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* L3 hit|SNP None */
2313 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* L3 hit|SNP Hit */
2314 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* L3 hit|SNP HitM */
2315 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOPX, FWD), /* L3 hit|SNP HitM */
2316 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* L3 hit|SNP Fwd */
2317 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* Remote L3 hit|SNP HitM */
2318 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, NONE), /* RAM hit|SNP Hit */
2319 OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE), /* Remote L3 hit|SNP Hit */
2320 OP_LH | LEVEL(RAM) | REM | P(SNOOPX, FWD), /* RAM hit|SNP None or Miss */
2321 OP_LH | LEVEL(RAM) | REM | P(SNOOP, HITM), /* Remote RAM hit|SNP None or Miss */
2322 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* I/O hit|SNP None */
2323 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* Uncached hit|SNP None */
2326 /* Based on kernel pebs_set_tlb_lock() */
2327 static inline void pebs_set_tlb_lock(u64 *val, bool tlb, bool lock)
2331 * 0 = did not miss 2nd level TLB
2332 * 1 = missed 2nd level TLB
2335 *val |= P(TLB, MISS) | P(TLB, L2);
2337 *val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
2341 *val |= P(LOCK, LOCKED);
2344 /* Based on kernel __grt_latency_data() */
2345 static u64 intel_pt_grt_latency_data(u8 dse, bool tlb, bool lock, bool blk,
2346 const u64 *pebs_data_source)
2350 dse &= PERF_PEBS_DATA_SOURCE_GRT_MASK;
2351 val = pebs_data_source[dse];
2353 pebs_set_tlb_lock(&val, tlb, lock);
2356 val |= P(BLK, DATA);
2363 /* Default value for data source */
2364 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
2365 PERF_MEM_S(LVL, NA) |\
2366 PERF_MEM_S(SNOOP, NA) |\
2367 PERF_MEM_S(LOCK, NA) |\
2368 PERF_MEM_S(TLB, NA) |\
2369 PERF_MEM_S(LVLNUM, NA))
2371 enum DATA_SRC_FORMAT {
2372 DATA_SRC_FORMAT_ERR = -1,
2373 DATA_SRC_FORMAT_NA = 0,
2374 DATA_SRC_FORMAT_GRT = 1,
2375 DATA_SRC_FORMAT_CMT = 2,
2378 /* Based on kernel grt_latency_data() and cmt_latency_data */
2379 static u64 intel_pt_get_data_src(u64 mem_aux_info, int data_src_fmt)
2381 switch (data_src_fmt) {
2382 case DATA_SRC_FORMAT_GRT: {
2387 unsigned int locked:1;
2388 unsigned int stlb_miss:1;
2389 unsigned int fwd_blk:1;
2390 unsigned int reserved:25;
2392 } x = {.val = mem_aux_info};
2393 return intel_pt_grt_latency_data(x.dse, x.stlb_miss, x.locked, x.fwd_blk,
2394 pebs_data_source_grt);
2396 case DATA_SRC_FORMAT_CMT: {
2401 unsigned int locked:1;
2402 unsigned int stlb_miss:1;
2403 unsigned int fwd_blk:1;
2404 unsigned int reserved:24;
2406 } x = {.val = mem_aux_info};
2407 return intel_pt_grt_latency_data(x.dse, x.stlb_miss, x.locked, x.fwd_blk,
2408 pebs_data_source_cmt);
2415 static int intel_pt_do_synth_pebs_sample(struct intel_pt_queue *ptq, struct evsel *evsel,
2416 u64 id, int data_src_fmt)
2418 const struct intel_pt_blk_items *items = &ptq->state->items;
2419 struct perf_sample sample;
2420 union perf_event *event = ptq->event_buf;
2421 struct intel_pt *pt = ptq->pt;
2422 u64 sample_type = evsel->core.attr.sample_type;
2424 u64 regs[8 * sizeof(sample.intr_regs->mask)];
2427 if (intel_pt_skip_event(pt))
2430 perf_sample__init(&sample, /*all=*/true);
2431 intel_pt_prep_a_sample(ptq, event, &sample);
2434 sample.stream_id = id;
2436 if (!evsel->core.attr.freq)
2437 sample.period = evsel->core.attr.sample_period;
2439 /* No support for non-zero CS base */
2441 sample.ip = items->ip;
2442 else if (items->has_rip)
2443 sample.ip = items->rip;
2445 sample.ip = ptq->state->from_ip;
2447 cpumode = intel_pt_cpumode(ptq, sample.ip, 0);
2449 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
2451 sample.cpumode = cpumode;
2453 if (sample_type & PERF_SAMPLE_TIME) {
2456 if (items->has_timestamp)
2457 timestamp = items->timestamp;
2458 else if (!pt->timeless_decoding)
2459 timestamp = ptq->timestamp;
2461 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
2464 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
2465 pt->synth_opts.callchain) {
2466 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
2467 pt->synth_opts.callchain_sz, sample.ip,
2469 sample.callchain = ptq->chain;
2472 if (sample_type & PERF_SAMPLE_REGS_INTR &&
2473 (items->mask[INTEL_PT_GP_REGS_POS] ||
2474 items->mask[INTEL_PT_XMM_POS])) {
2475 u64 regs_mask = evsel->core.attr.sample_regs_intr;
2477 struct regs_dump *intr_regs = perf_sample__intr_regs(&sample);
2479 intr_regs->abi = items->is_32_bit ?
2480 PERF_SAMPLE_REGS_ABI_32 :
2481 PERF_SAMPLE_REGS_ABI_64;
2482 intr_regs->regs = regs;
2484 pos = intel_pt_add_gp_regs(intr_regs, regs, items, regs_mask);
2486 intel_pt_add_xmm(intr_regs, pos, items, regs_mask);
2489 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
2490 if (items->mask[INTEL_PT_LBR_0_POS] ||
2491 items->mask[INTEL_PT_LBR_1_POS] ||
2492 items->mask[INTEL_PT_LBR_2_POS]) {
2493 intel_pt_add_lbrs(ptq->last_branch, items);
2494 } else if (pt->synth_opts.last_branch) {
2495 thread_stack__br_sample(ptq->thread, ptq->cpu,
2499 ptq->last_branch->nr = 0;
2501 sample.branch_stack = ptq->last_branch;
2504 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
2505 sample.addr = items->mem_access_address;
2507 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
2509 * Refer kernel's setup_pebs_adaptive_sample_data() and
2510 * intel_hsw_weight().
2512 if (items->has_mem_access_latency) {
2513 u64 weight = items->mem_access_latency >> 32;
2516 * Starts from SPR, the mem access latency field
2517 * contains both cache latency [47:32] and instruction
2518 * latency [15:0]. The cache latency is the same as the
2519 * mem access latency on previous platforms.
2521 * In practice, no memory access could last than 4G
2522 * cycles. Use latency >> 32 to distinguish the
2523 * different format of the mem access latency field.
2526 sample.weight = weight & 0xffff;
2527 sample.ins_lat = items->mem_access_latency & 0xffff;
2529 sample.weight = items->mem_access_latency;
2531 if (!sample.weight && items->has_tsx_aux_info) {
2532 /* Cycles last block */
2533 sample.weight = (u32)items->tsx_aux_info;
2537 if (sample_type & PERF_SAMPLE_DATA_SRC) {
2538 if (items->has_mem_aux_info && data_src_fmt) {
2539 if (data_src_fmt < 0) {
2540 pr_err("Intel PT missing data_src info\n");
2543 sample.data_src = intel_pt_get_data_src(items->mem_aux_info, data_src_fmt);
2545 sample.data_src = PERF_MEM_NA;
2549 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
2550 u64 ax = items->has_rax ? items->rax : 0;
2551 /* Refer kernel's intel_hsw_transaction() */
2552 u64 txn = (u8)(items->tsx_aux_info >> 32);
2554 /* For RTM XABORTs also log the abort code from AX */
2555 if (txn & PERF_TXN_TRANSACTION && ax & 1)
2556 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
2557 sample.transaction = txn;
2560 ret = intel_pt_deliver_synth_event(pt, event, &sample, sample_type);
2561 perf_sample__exit(&sample);
2565 static int intel_pt_synth_single_pebs_sample(struct intel_pt_queue *ptq)
2567 struct intel_pt *pt = ptq->pt;
2568 struct evsel *evsel = pt->pebs_evsel;
2569 int data_src_fmt = pt->pebs_data_src_fmt;
2570 u64 id = evsel->core.id[0];
2572 return intel_pt_do_synth_pebs_sample(ptq, evsel, id, data_src_fmt);
2575 static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
2577 const struct intel_pt_blk_items *items = &ptq->state->items;
2578 struct intel_pt_pebs_event *pe;
2579 struct intel_pt *pt = ptq->pt;
2583 if (!items->has_applicable_counters || !items->applicable_counters) {
2584 if (!pt->single_pebs)
2585 pr_err("PEBS-via-PT record with no applicable_counters\n");
2586 return intel_pt_synth_single_pebs_sample(ptq);
2589 for_each_set_bit(hw_id, (unsigned long *)&items->applicable_counters, INTEL_PT_MAX_PEBS) {
2590 pe = &ptq->pebs[hw_id];
2592 if (!pt->single_pebs)
2593 pr_err("PEBS-via-PT record with no matching event, hw_id %d\n",
2595 return intel_pt_synth_single_pebs_sample(ptq);
2597 err = intel_pt_do_synth_pebs_sample(ptq, pe->evsel, pe->id, pe->data_src_fmt);
2605 static int intel_pt_synth_events_sample(struct intel_pt_queue *ptq)
2607 struct intel_pt *pt = ptq->pt;
2608 union perf_event *event = ptq->event_buf;
2609 struct perf_sample sample;
2611 struct perf_synth_intel_evt cfe;
2612 struct perf_synth_intel_evd evd[INTEL_PT_MAX_EVDS];
2616 if (intel_pt_skip_event(pt))
2619 perf_sample__init(&sample, /*all=*/true);
2620 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2622 sample.id = ptq->pt->evt_id;
2623 sample.stream_id = ptq->pt->evt_id;
2625 raw.cfe.type = ptq->state->cfe_type;
2626 raw.cfe.reserved = 0;
2627 raw.cfe.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
2628 raw.cfe.vector = ptq->state->cfe_vector;
2629 raw.cfe.evd_cnt = ptq->state->evd_cnt;
2631 for (i = 0; i < ptq->state->evd_cnt; i++) {
2633 raw.evd[i].evd_type = ptq->state->evd[i].type;
2634 raw.evd[i].payload = ptq->state->evd[i].payload;
2637 sample.raw_size = perf_synth__raw_size(raw) +
2638 ptq->state->evd_cnt * sizeof(struct perf_synth_intel_evd);
2639 sample.raw_data = perf_synth__raw_data(&raw);
2641 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2642 pt->evt_sample_type);
2643 perf_sample__exit(&sample);
2647 static int intel_pt_synth_iflag_chg_sample(struct intel_pt_queue *ptq)
2649 struct intel_pt *pt = ptq->pt;
2650 union perf_event *event = ptq->event_buf;
2651 struct perf_sample sample;
2652 struct perf_synth_intel_iflag_chg raw;
2655 if (intel_pt_skip_event(pt))
2658 perf_sample__init(&sample, /*all=*/true);
2659 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2661 sample.id = ptq->pt->iflag_chg_id;
2662 sample.stream_id = ptq->pt->iflag_chg_id;
2665 raw.iflag = ptq->state->to_iflag;
2667 if (ptq->state->type & INTEL_PT_BRANCH) {
2669 raw.branch_ip = ptq->state->to_ip;
2673 sample.flags = ptq->flags;
2675 sample.raw_size = perf_synth__raw_size(raw);
2676 sample.raw_data = perf_synth__raw_data(&raw);
2678 ret = intel_pt_deliver_synth_event(pt, event, &sample,
2679 pt->iflag_chg_sample_type);
2680 perf_sample__exit(&sample);
2684 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
2685 pid_t pid, pid_t tid, u64 ip, u64 timestamp,
2686 pid_t machine_pid, int vcpu)
2688 bool dump_log_on_error = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR;
2689 bool log_on_stdout = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT;
2690 union perf_event event;
2691 char msg[MAX_AUXTRACE_ERROR_MSG];
2694 if (pt->synth_opts.error_minus_flags) {
2695 if (code == INTEL_PT_ERR_OVR &&
2696 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_OVERFLOW)
2698 if (code == INTEL_PT_ERR_LOST &&
2699 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_DATA_LOST)
2703 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
2705 auxtrace_synth_guest_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
2706 code, cpu, pid, tid, ip, msg, timestamp,
2709 if (intel_pt_enable_logging && !log_on_stdout) {
2710 FILE *fp = intel_pt_log_fp();
2713 perf_event__fprintf_auxtrace_error(&event, fp);
2716 if (code != INTEL_PT_ERR_LOST && dump_log_on_error)
2717 intel_pt_log_dump_buf();
2719 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
2721 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
2727 static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
2728 const struct intel_pt_state *state)
2730 struct intel_pt *pt = ptq->pt;
2731 u64 tm = ptq->timestamp;
2732 pid_t machine_pid = 0;
2733 pid_t pid = ptq->pid;
2734 pid_t tid = ptq->tid;
2737 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
2739 if (pt->have_guest_sideband && state->from_nr) {
2740 machine_pid = ptq->guest_machine_pid;
2742 pid = ptq->guest_pid;
2743 tid = ptq->guest_tid;
2746 return intel_pt_synth_error(pt, state->err, ptq->cpu, pid, tid,
2747 state->from_ip, tm, machine_pid, vcpu);
2750 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
2752 struct auxtrace_queue *queue;
2753 pid_t tid = ptq->next_tid;
2759 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
2761 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
2763 queue = &pt->queues.queue_array[ptq->queue_nr];
2764 intel_pt_set_pid_tid_cpu(pt, queue);
2771 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
2773 struct intel_pt *pt = ptq->pt;
2775 return ip == pt->switch_ip &&
2776 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
2777 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
2778 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
2781 #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
2782 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
2784 static int intel_pt_sample(struct intel_pt_queue *ptq)
2786 const struct intel_pt_state *state = ptq->state;
2787 struct intel_pt *pt = ptq->pt;
2790 if (!ptq->have_sample)
2793 ptq->have_sample = false;
2795 if (pt->synth_opts.approx_ipc) {
2796 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2797 ptq->ipc_cyc_cnt = ptq->state->cycles;
2798 ptq->sample_ipc = true;
2800 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2801 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
2802 ptq->sample_ipc = ptq->state->flags & INTEL_PT_SAMPLE_IPC;
2805 /* Ensure guest code maps are set up */
2806 if (symbol_conf.guest_code && (state->from_nr || state->to_nr))
2807 intel_pt_get_guest(ptq);
2810 * Do PEBS first to allow for the possibility that the PEBS timestamp
2811 * precedes the current timestamp.
2813 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
2814 err = intel_pt_synth_pebs_sample(ptq);
2819 if (pt->synth_opts.intr_events) {
2820 if (state->type & INTEL_PT_EVT) {
2821 err = intel_pt_synth_events_sample(ptq);
2825 if (state->type & INTEL_PT_IFLAG_CHG) {
2826 err = intel_pt_synth_iflag_chg_sample(ptq);
2832 if (pt->sample_pwr_events) {
2833 if (state->type & INTEL_PT_PSB_EVT) {
2834 err = intel_pt_synth_psb_sample(ptq);
2838 if (ptq->state->cbr != ptq->cbr_seen) {
2839 err = intel_pt_synth_cbr_sample(ptq);
2843 if (state->type & INTEL_PT_PWR_EVT) {
2844 if (state->type & INTEL_PT_MWAIT_OP) {
2845 err = intel_pt_synth_mwait_sample(ptq);
2849 if (state->type & INTEL_PT_PWR_ENTRY) {
2850 err = intel_pt_synth_pwre_sample(ptq);
2854 if (state->type & INTEL_PT_EX_STOP) {
2855 err = intel_pt_synth_exstop_sample(ptq);
2859 if (state->type & INTEL_PT_PWR_EXIT) {
2860 err = intel_pt_synth_pwrx_sample(ptq);
2867 if (state->type & INTEL_PT_INSTRUCTION) {
2868 if (pt->sample_instructions) {
2869 err = intel_pt_synth_instruction_sample(ptq);
2873 if (pt->sample_cycles) {
2874 err = intel_pt_synth_cycle_sample(ptq);
2880 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
2881 err = intel_pt_synth_transaction_sample(ptq);
2886 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
2887 err = intel_pt_synth_ptwrite_sample(ptq);
2892 if (!(state->type & INTEL_PT_BRANCH))
2895 if (pt->use_thread_stack) {
2896 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags,
2897 state->from_ip, state->to_ip, ptq->insn_len,
2898 state->trace_nr, pt->callstack,
2899 pt->br_stack_sz_plus,
2902 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
2905 if (pt->sample_branches) {
2906 if (state->from_nr != state->to_nr &&
2907 state->from_ip && state->to_ip) {
2908 struct intel_pt_state *st = (struct intel_pt_state *)state;
2909 u64 to_ip = st->to_ip;
2910 u64 from_ip = st->from_ip;
2913 * perf cannot handle having different machines for ip
2914 * and addr, so create 2 branches.
2917 err = intel_pt_synth_branch_sample(ptq);
2922 err = intel_pt_synth_branch_sample(ptq);
2923 st->from_ip = from_ip;
2925 err = intel_pt_synth_branch_sample(ptq);
2931 if (!ptq->sync_switch)
2934 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
2935 switch (ptq->switch_state) {
2936 case INTEL_PT_SS_NOT_TRACING:
2937 case INTEL_PT_SS_UNKNOWN:
2938 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2939 err = intel_pt_next_tid(pt, ptq);
2942 ptq->switch_state = INTEL_PT_SS_TRACING;
2945 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
2948 } else if (!state->to_ip) {
2949 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2950 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
2951 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2952 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2953 state->to_ip == pt->ptss_ip &&
2954 (ptq->flags & PERF_IP_FLAG_CALL)) {
2955 ptq->switch_state = INTEL_PT_SS_TRACING;
2961 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
2963 struct machine *machine = pt->machine;
2965 struct symbol *sym, *start;
2966 u64 ip, switch_ip = 0;
2972 map = machine__kernel_map(machine);
2979 start = dso__first_symbol(map__dso(map));
2981 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2982 if (sym->binding == STB_GLOBAL &&
2983 !strcmp(sym->name, "__switch_to")) {
2984 ip = map__unmap_ip(map, sym->start);
2985 if (ip >= map__start(map) && ip < map__end(map)) {
2992 if (!switch_ip || !ptss_ip)
2995 if (pt->have_sched_switch == 1)
2996 ptss = "perf_trace_sched_switch";
2998 ptss = "__perf_event_task_sched_out";
3000 for (sym = start; sym; sym = dso__next_symbol(sym)) {
3001 if (!strcmp(sym->name, ptss)) {
3002 ip = map__unmap_ip(map, sym->start);
3003 if (ip >= map__start(map) && ip < map__end(map)) {
3013 static void intel_pt_enable_sync_switch(struct intel_pt *pt)
3017 if (pt->sync_switch_not_supported)
3020 pt->sync_switch = true;
3022 for (i = 0; i < pt->queues.nr_queues; i++) {
3023 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
3024 struct intel_pt_queue *ptq = queue->priv;
3027 ptq->sync_switch = true;
3031 static void intel_pt_disable_sync_switch(struct intel_pt *pt)
3035 pt->sync_switch = false;
3037 for (i = 0; i < pt->queues.nr_queues; i++) {
3038 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
3039 struct intel_pt_queue *ptq = queue->priv;
3042 ptq->sync_switch = false;
3043 intel_pt_next_tid(pt, ptq);
3049 * To filter against time ranges, it is only necessary to look at the next start
3052 static bool intel_pt_next_time(struct intel_pt_queue *ptq)
3054 struct intel_pt *pt = ptq->pt;
3056 if (ptq->sel_start) {
3057 /* Next time is an end time */
3058 ptq->sel_start = false;
3059 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
3061 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
3062 /* Next time is a start time */
3063 ptq->sel_start = true;
3065 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
3073 static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
3078 if (ptq->sel_start) {
3079 if (ptq->timestamp >= ptq->sel_timestamp) {
3080 /* After start time, so consider next time */
3081 intel_pt_next_time(ptq);
3082 if (!ptq->sel_timestamp) {
3086 /* Check against end time */
3089 /* Before start time, so fast forward */
3090 ptq->have_sample = false;
3091 if (ptq->sel_timestamp > *ff_timestamp) {
3092 if (ptq->sync_switch) {
3093 intel_pt_next_tid(ptq->pt, ptq);
3094 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
3096 *ff_timestamp = ptq->sel_timestamp;
3097 err = intel_pt_fast_forward(ptq->decoder,
3098 ptq->sel_timestamp);
3103 } else if (ptq->timestamp > ptq->sel_timestamp) {
3104 /* After end time, so consider next time */
3105 if (!intel_pt_next_time(ptq)) {
3106 /* No next time range, so stop decoding */
3107 ptq->have_sample = false;
3108 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
3111 /* Check against next start time */
3114 /* Before end time */
3120 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
3122 const struct intel_pt_state *state = ptq->state;
3123 struct intel_pt *pt = ptq->pt;
3124 u64 ff_timestamp = 0;
3127 if (!pt->kernel_start) {
3128 pt->kernel_start = machine__kernel_start(pt->machine);
3129 if (pt->per_cpu_mmaps &&
3130 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
3131 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
3132 !pt->sampling_mode && !pt->synth_opts.vm_time_correlation) {
3133 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
3134 if (pt->switch_ip) {
3135 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
3136 pt->switch_ip, pt->ptss_ip);
3137 intel_pt_enable_sync_switch(pt);
3142 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
3143 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
3145 err = intel_pt_sample(ptq);
3149 state = intel_pt_decode(ptq->decoder);
3151 if (state->err == INTEL_PT_ERR_NODATA)
3153 if (ptq->sync_switch &&
3154 state->from_ip >= pt->kernel_start) {
3155 ptq->sync_switch = false;
3156 intel_pt_next_tid(pt, ptq);
3158 ptq->timestamp = state->est_timestamp;
3159 if (pt->synth_opts.errors) {
3160 err = intel_ptq_synth_error(ptq, state);
3168 ptq->have_sample = true;
3169 intel_pt_sample_flags(ptq);
3171 /* Use estimated TSC upon return to user space */
3173 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
3174 state->to_ip && state->to_ip < pt->kernel_start) {
3175 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
3176 state->timestamp, state->est_timestamp);
3177 ptq->timestamp = state->est_timestamp;
3178 /* Use estimated TSC in unknown switch state */
3179 } else if (ptq->sync_switch &&
3180 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
3181 intel_pt_is_switch_ip(ptq, state->to_ip) &&
3182 ptq->next_tid == -1) {
3183 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
3184 state->timestamp, state->est_timestamp);
3185 ptq->timestamp = state->est_timestamp;
3186 } else if (state->timestamp > ptq->timestamp) {
3187 ptq->timestamp = state->timestamp;
3190 if (ptq->sel_timestamp) {
3191 err = intel_pt_time_filter(ptq, &ff_timestamp);
3196 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
3197 *timestamp = ptq->timestamp;
3204 static inline int intel_pt_update_queues(struct intel_pt *pt)
3206 if (pt->queues.new_data) {
3207 pt->queues.new_data = false;
3208 return intel_pt_setup_queues(pt);
3213 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
3215 unsigned int queue_nr;
3220 struct auxtrace_queue *queue;
3221 struct intel_pt_queue *ptq;
3223 if (!pt->heap.heap_cnt)
3226 if (pt->heap.heap_array[0].ordinal >= timestamp)
3229 queue_nr = pt->heap.heap_array[0].queue_nr;
3230 queue = &pt->queues.queue_array[queue_nr];
3233 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
3234 queue_nr, pt->heap.heap_array[0].ordinal,
3237 auxtrace_heap__pop(&pt->heap);
3239 if (pt->heap.heap_cnt) {
3240 ts = pt->heap.heap_array[0].ordinal + 1;
3247 intel_pt_set_pid_tid_cpu(pt, queue);
3249 ret = intel_pt_run_decoder(ptq, &ts);
3252 auxtrace_heap__add(&pt->heap, queue_nr, ts);
3257 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
3261 ptq->on_heap = false;
3268 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
3271 struct auxtrace_queues *queues = &pt->queues;
3275 for (i = 0; i < queues->nr_queues; i++) {
3276 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
3277 struct intel_pt_queue *ptq = queue->priv;
3279 if (ptq && (tid == -1 || ptq->tid == tid)) {
3281 intel_pt_set_pid_tid_cpu(pt, queue);
3282 intel_pt_run_decoder(ptq, &ts);
3288 static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq,
3289 struct auxtrace_queue *queue,
3290 struct perf_sample *sample)
3292 struct machine *m = ptq->pt->machine;
3294 ptq->pid = sample->pid;
3295 ptq->tid = sample->tid;
3296 ptq->cpu = queue->cpu;
3298 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
3299 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
3301 thread__zput(ptq->thread);
3306 if (ptq->pid == -1) {
3307 ptq->thread = machine__find_thread(m, -1, ptq->tid);
3309 ptq->pid = thread__pid(ptq->thread);
3313 ptq->thread = machine__findnew_thread(m, ptq->pid, ptq->tid);
3316 static int intel_pt_process_timeless_sample(struct intel_pt *pt,
3317 struct perf_sample *sample)
3319 struct auxtrace_queue *queue;
3320 struct intel_pt_queue *ptq;
3323 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
3332 ptq->time = sample->time;
3333 intel_pt_sample_set_pid_tid_cpu(ptq, queue, sample);
3334 intel_pt_run_decoder(ptq, &ts);
3338 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
3340 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
3341 sample->pid, sample->tid, 0, sample->time,
3342 sample->machine_pid, sample->vcpu);
3345 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
3349 if (cpu < 0 || !pt->queues.nr_queues)
3352 if ((unsigned)cpu >= pt->queues.nr_queues)
3353 i = pt->queues.nr_queues - 1;
3357 if (pt->queues.queue_array[i].cpu == cpu)
3358 return pt->queues.queue_array[i].priv;
3360 for (j = 0; i > 0; j++) {
3361 if (pt->queues.queue_array[--i].cpu == cpu)
3362 return pt->queues.queue_array[i].priv;
3365 for (; j < pt->queues.nr_queues; j++) {
3366 if (pt->queues.queue_array[j].cpu == cpu)
3367 return pt->queues.queue_array[j].priv;
3373 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
3376 struct intel_pt_queue *ptq;
3379 if (!pt->sync_switch)
3382 ptq = intel_pt_cpu_to_ptq(pt, cpu);
3383 if (!ptq || !ptq->sync_switch)
3386 switch (ptq->switch_state) {
3387 case INTEL_PT_SS_NOT_TRACING:
3389 case INTEL_PT_SS_UNKNOWN:
3390 case INTEL_PT_SS_TRACING:
3391 ptq->next_tid = tid;
3392 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
3394 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
3395 if (!ptq->on_heap) {
3396 ptq->timestamp = perf_time_to_tsc(timestamp,
3398 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
3402 ptq->on_heap = true;
3404 ptq->switch_state = INTEL_PT_SS_TRACING;
3406 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
3407 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
3418 #ifdef HAVE_LIBTRACEEVENT
3419 static int intel_pt_process_switch(struct intel_pt *pt,
3420 struct perf_sample *sample)
3424 struct evsel *evsel = evlist__id2evsel(pt->session->evlist, sample->id);
3426 if (evsel != pt->switch_evsel)
3429 tid = evsel__intval(evsel, sample, "next_pid");
3432 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
3433 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
3436 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
3440 return machine__set_current_tid(pt->machine, cpu, -1, tid);
3442 #endif /* HAVE_LIBTRACEEVENT */
3444 static int intel_pt_context_switch_in(struct intel_pt *pt,
3445 struct perf_sample *sample)
3447 pid_t pid = sample->pid;
3448 pid_t tid = sample->tid;
3449 int cpu = sample->cpu;
3451 if (pt->sync_switch) {
3452 struct intel_pt_queue *ptq;
3454 ptq = intel_pt_cpu_to_ptq(pt, cpu);
3455 if (ptq && ptq->sync_switch) {
3457 switch (ptq->switch_state) {
3458 case INTEL_PT_SS_NOT_TRACING:
3459 case INTEL_PT_SS_UNKNOWN:
3460 case INTEL_PT_SS_TRACING:
3462 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
3463 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
3464 ptq->switch_state = INTEL_PT_SS_TRACING;
3473 * If the current tid has not been updated yet, ensure it is now that
3474 * a "switch in" event has occurred.
3476 if (machine__get_current_tid(pt->machine, cpu) == tid)
3479 return machine__set_current_tid(pt->machine, cpu, pid, tid);
3482 static int intel_pt_guest_context_switch(struct intel_pt *pt,
3483 union perf_event *event,
3484 struct perf_sample *sample)
3486 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
3487 struct machines *machines = &pt->session->machines;
3488 struct machine *machine = machines__find(machines, sample->machine_pid);
3490 pt->have_guest_sideband = true;
3493 * sync_switch cannot handle guest machines at present, so just disable
3496 pt->sync_switch_not_supported = true;
3497 if (pt->sync_switch)
3498 intel_pt_disable_sync_switch(pt);
3506 return machine__set_current_tid(machine, sample->vcpu, sample->pid, sample->tid);
3509 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
3510 struct perf_sample *sample)
3512 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
3516 if (perf_event__is_guest(event))
3517 return intel_pt_guest_context_switch(pt, event, sample);
3521 if (pt->have_sched_switch == 3) {
3523 return intel_pt_context_switch_in(pt, sample);
3524 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
3525 pr_err("Expecting CPU-wide context switch event\n");
3528 pid = event->context_switch.next_prev_pid;
3529 tid = event->context_switch.next_prev_tid;
3538 intel_pt_log("context_switch event has no tid\n");
3540 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
3544 return machine__set_current_tid(pt->machine, cpu, pid, tid);
3547 static int intel_pt_process_itrace_start(struct intel_pt *pt,
3548 union perf_event *event,
3549 struct perf_sample *sample)
3551 if (!pt->per_cpu_mmaps)
3554 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
3555 sample->cpu, event->itrace_start.pid,
3556 event->itrace_start.tid, sample->time,
3557 perf_time_to_tsc(sample->time, &pt->tc));
3559 return machine__set_current_tid(pt->machine, sample->cpu,
3560 event->itrace_start.pid,
3561 event->itrace_start.tid);
3565 * Events with data_src are identified by L1_Hit_Indication
3566 * refer https://github.com/intel/perfmon
3568 static int intel_pt_data_src_fmt(struct intel_pt *pt, struct evsel *evsel)
3570 struct perf_env *env = pt->machine->env;
3571 int fmt = DATA_SRC_FORMAT_NA;
3574 return DATA_SRC_FORMAT_ERR;
3577 * PEBS-via-PT is only supported on E-core non-hybrid. Of those only
3578 * Gracemont and Crestmont have data_src. Check for:
3579 * Alderlake N (Gracemont)
3580 * Sierra Forest (Crestmont)
3581 * Grand Ridge (Crestmont)
3584 if (!strncmp(env->cpuid, "GenuineIntel,6,190,", 19))
3585 fmt = DATA_SRC_FORMAT_GRT;
3587 if (!strncmp(env->cpuid, "GenuineIntel,6,175,", 19) ||
3588 !strncmp(env->cpuid, "GenuineIntel,6,182,", 19))
3589 fmt = DATA_SRC_FORMAT_CMT;
3591 if (fmt == DATA_SRC_FORMAT_NA)
3595 * Only data_src events are:
3596 * mem-loads event=0xd0,umask=0x5
3597 * mem-stores event=0xd0,umask=0x6
3599 if (evsel->core.attr.type == PERF_TYPE_RAW &&
3600 ((evsel->core.attr.config & 0xffff) == 0x5d0 ||
3601 (evsel->core.attr.config & 0xffff) == 0x6d0))
3604 return DATA_SRC_FORMAT_NA;
3607 static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt,
3608 union perf_event *event,
3609 struct perf_sample *sample)
3611 u64 hw_id = event->aux_output_hw_id.hw_id;
3612 struct auxtrace_queue *queue;
3613 struct intel_pt_queue *ptq;
3614 struct evsel *evsel;
3616 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
3617 evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id);
3618 if (!queue || !queue->priv || !evsel || hw_id > INTEL_PT_MAX_PEBS) {
3619 pr_err("Bad AUX output hardware ID\n");
3625 ptq->pebs[hw_id].evsel = evsel;
3626 ptq->pebs[hw_id].id = sample->id;
3627 ptq->pebs[hw_id].data_src_fmt = intel_pt_data_src_fmt(pt, evsel);
3632 static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr,
3633 struct addr_location *al)
3635 if (!al->map || addr < map__start(al->map) || addr >= map__end(al->map)) {
3636 if (!thread__find_map(thread, cpumode, addr, al))
3643 /* Invalidate all instruction cache entries that overlap the text poke */
3644 static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)
3646 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
3647 u64 addr = event->text_poke.addr + event->text_poke.new_len - 1;
3648 /* Assume text poke begins in a basic block no more than 4096 bytes */
3649 int cnt = 4096 + event->text_poke.new_len;
3650 struct thread *thread = pt->unknown_thread;
3651 struct addr_location al;
3652 struct machine *machine = pt->machine;
3653 struct intel_pt_cache_entry *e;
3657 addr_location__init(&al);
3658 if (!event->text_poke.new_len)
3661 for (; cnt; cnt--, addr--) {
3664 if (intel_pt_find_map(thread, cpumode, addr, &al)) {
3665 if (addr < event->text_poke.addr)
3670 dso = map__dso(al.map);
3671 if (!dso || !dso__auxtrace_cache(dso))
3674 offset = map__map_ip(al.map, addr);
3676 e = intel_pt_cache_lookup(dso, machine, offset);
3680 if (addr + e->byte_cnt + e->length <= event->text_poke.addr) {
3682 * No overlap. Working backwards there cannot be another
3683 * basic block that overlaps the text poke if there is a
3684 * branch instruction before the text poke address.
3686 if (e->branch != INTEL_PT_BR_NO_BRANCH)
3689 intel_pt_cache_invalidate(dso, machine, offset);
3690 intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n",
3691 dso__long_name(dso), addr);
3695 addr_location__exit(&al);
3699 static int intel_pt_process_event(struct perf_session *session,
3700 union perf_event *event,
3701 struct perf_sample *sample,
3702 const struct perf_tool *tool)
3704 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3712 if (!tool->ordered_events) {
3713 pr_err("Intel Processor Trace requires ordered events\n");
3717 if (sample->time && sample->time != (u64)-1)
3718 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3722 if (timestamp || pt->timeless_decoding) {
3723 err = intel_pt_update_queues(pt);
3728 if (pt->timeless_decoding) {
3729 if (pt->sampling_mode) {
3730 if (sample->aux_sample.size)
3731 err = intel_pt_process_timeless_sample(pt,
3733 } else if (event->header.type == PERF_RECORD_EXIT) {
3734 err = intel_pt_process_timeless_queues(pt,
3738 } else if (timestamp) {
3739 if (!pt->first_timestamp)
3740 intel_pt_first_timestamp(pt, timestamp);
3741 err = intel_pt_process_queues(pt, timestamp);
3746 if (event->header.type == PERF_RECORD_SAMPLE) {
3747 if (pt->synth_opts.add_callchain && !sample->callchain)
3748 intel_pt_add_callchain(pt, sample);
3749 if (pt->synth_opts.add_last_branch && !sample->branch_stack)
3750 intel_pt_add_br_stack(pt, sample);
3753 if (event->header.type == PERF_RECORD_AUX &&
3754 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
3755 pt->synth_opts.errors) {
3756 err = intel_pt_lost(pt, sample);
3761 #ifdef HAVE_LIBTRACEEVENT
3762 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
3763 err = intel_pt_process_switch(pt, sample);
3766 if (event->header.type == PERF_RECORD_ITRACE_START)
3767 err = intel_pt_process_itrace_start(pt, event, sample);
3768 else if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID)
3769 err = intel_pt_process_aux_output_hw_id(pt, event, sample);
3770 else if (event->header.type == PERF_RECORD_SWITCH ||
3771 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
3772 err = intel_pt_context_switch(pt, event, sample);
3774 if (!err && event->header.type == PERF_RECORD_TEXT_POKE)
3775 err = intel_pt_text_poke(pt, event);
3777 if (intel_pt_enable_logging && intel_pt_log_events(pt, sample->time)) {
3778 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
3779 event->header.type, sample->cpu, sample->time, timestamp);
3780 intel_pt_log_event(event);
3786 static int intel_pt_flush(struct perf_session *session, const struct perf_tool *tool)
3788 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3795 if (!tool->ordered_events)
3798 ret = intel_pt_update_queues(pt);
3802 if (pt->timeless_decoding)
3803 return intel_pt_process_timeless_queues(pt, -1,
3806 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
3809 static void intel_pt_free_events(struct perf_session *session)
3811 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3813 struct auxtrace_queues *queues = &pt->queues;
3816 for (i = 0; i < queues->nr_queues; i++) {
3817 intel_pt_free_queue(queues->queue_array[i].priv);
3818 queues->queue_array[i].priv = NULL;
3820 intel_pt_log_disable();
3821 auxtrace_queues__free(queues);
3824 static void intel_pt_free(struct perf_session *session)
3826 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3829 auxtrace_heap__free(&pt->heap);
3830 intel_pt_free_events(session);
3831 session->auxtrace = NULL;
3832 intel_pt_free_vmcs_info(pt);
3833 thread__put(pt->unknown_thread);
3834 addr_filters__exit(&pt->filts);
3837 zfree(&pt->time_ranges);
3838 zfree(&pt->br_stack);
3842 static bool intel_pt_evsel_is_auxtrace(struct perf_session *session,
3843 struct evsel *evsel)
3845 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3848 return evsel->core.attr.type == pt->pmu_type;
3851 static int intel_pt_process_auxtrace_event(struct perf_session *session,
3852 union perf_event *event,
3853 const struct perf_tool *tool __maybe_unused)
3855 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3858 if (!pt->data_queued) {
3859 struct auxtrace_buffer *buffer;
3861 int fd = perf_data__fd(session->data);
3864 if (perf_data__is_pipe(session->data)) {
3867 data_offset = lseek(fd, 0, SEEK_CUR);
3868 if (data_offset == -1)
3872 err = auxtrace_queues__add_event(&pt->queues, session, event,
3873 data_offset, &buffer);
3877 /* Dump here now we have copied a piped trace out of the pipe */
3879 if (auxtrace_buffer__get_data(buffer, fd)) {
3880 intel_pt_dump_event(pt, buffer->data,
3882 auxtrace_buffer__put_data(buffer);
3890 static int intel_pt_queue_data(struct perf_session *session,
3891 struct perf_sample *sample,
3892 union perf_event *event, u64 data_offset)
3894 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3899 return auxtrace_queues__add_event(&pt->queues, session, event,
3903 if (sample->time && sample->time != (u64)-1)
3904 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3908 return auxtrace_queues__add_sample(&pt->queues, session, sample,
3909 data_offset, timestamp);
3912 static int intel_pt_synth_event(struct perf_session *session, const char *name,
3913 struct perf_event_attr *attr, u64 id)
3917 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
3918 name, id, (u64)attr->sample_type);
3920 err = perf_session__deliver_synth_attr_event(session, attr, id);
3922 pr_err("%s: failed to synthesize '%s' event type\n",
3928 static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
3931 struct evsel *evsel;
3933 evlist__for_each_entry(evlist, evsel) {
3934 if (evsel->core.id && evsel->core.id[0] == id) {
3936 zfree(&evsel->name);
3937 evsel->name = strdup(name);
3943 static struct evsel *intel_pt_evsel(struct intel_pt *pt,
3944 struct evlist *evlist)
3946 struct evsel *evsel;
3948 evlist__for_each_entry(evlist, evsel) {
3949 if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids)
3956 static int intel_pt_synth_events(struct intel_pt *pt,
3957 struct perf_session *session)
3959 struct evlist *evlist = session->evlist;
3960 struct evsel *evsel = intel_pt_evsel(pt, evlist);
3961 struct perf_event_attr attr;
3966 pr_debug("There are no selected events with Intel Processor Trace data\n");
3970 memset(&attr, 0, sizeof(struct perf_event_attr));
3971 attr.size = sizeof(struct perf_event_attr);
3972 attr.type = PERF_TYPE_HARDWARE;
3973 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
3974 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
3976 if (pt->timeless_decoding)
3977 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
3979 attr.sample_type |= PERF_SAMPLE_TIME;
3980 if (!pt->per_cpu_mmaps)
3981 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
3982 attr.exclude_user = evsel->core.attr.exclude_user;
3983 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
3984 attr.exclude_hv = evsel->core.attr.exclude_hv;
3985 attr.exclude_host = evsel->core.attr.exclude_host;
3986 attr.exclude_guest = evsel->core.attr.exclude_guest;
3987 attr.sample_id_all = evsel->core.attr.sample_id_all;
3988 attr.read_format = evsel->core.attr.read_format;
3990 id = evsel->core.id[0] + 1000000000;
3994 if (pt->synth_opts.branches) {
3995 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
3996 attr.sample_period = 1;
3997 attr.sample_type |= PERF_SAMPLE_ADDR;
3998 err = intel_pt_synth_event(session, "branches", &attr, id);
4001 pt->sample_branches = true;
4002 pt->branches_sample_type = attr.sample_type;
4003 pt->branches_id = id;
4005 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
4008 if (pt->synth_opts.callchain)
4009 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
4010 if (pt->synth_opts.last_branch) {
4011 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
4013 * We don't use the hardware index, but the sample generation
4014 * code uses the new format branch_stack with this field,
4015 * so the event attributes must indicate that it's present.
4017 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
4020 if (pt->synth_opts.instructions) {
4021 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
4022 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
4023 attr.sample_period =
4024 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
4026 attr.sample_period = pt->synth_opts.period;
4027 err = intel_pt_synth_event(session, "instructions", &attr, id);
4030 pt->sample_instructions = true;
4031 pt->instructions_sample_type = attr.sample_type;
4032 pt->instructions_id = id;
4036 if (pt->synth_opts.cycles) {
4037 attr.config = PERF_COUNT_HW_CPU_CYCLES;
4038 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
4039 attr.sample_period =
4040 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
4042 attr.sample_period = pt->synth_opts.period;
4043 err = intel_pt_synth_event(session, "cycles", &attr, id);
4046 pt->sample_cycles = true;
4047 pt->cycles_sample_type = attr.sample_type;
4052 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
4053 attr.sample_period = 1;
4055 if (pt->synth_opts.transactions) {
4056 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
4057 err = intel_pt_synth_event(session, "transactions", &attr, id);
4060 pt->sample_transactions = true;
4061 pt->transactions_sample_type = attr.sample_type;
4062 pt->transactions_id = id;
4063 intel_pt_set_event_name(evlist, id, "transactions");
4067 attr.type = PERF_TYPE_SYNTH;
4068 attr.sample_type |= PERF_SAMPLE_RAW;
4070 if (pt->synth_opts.ptwrites) {
4071 attr.config = PERF_SYNTH_INTEL_PTWRITE;
4072 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
4075 pt->sample_ptwrites = true;
4076 pt->ptwrites_sample_type = attr.sample_type;
4077 pt->ptwrites_id = id;
4078 intel_pt_set_event_name(evlist, id, "ptwrite");
4082 if (pt->synth_opts.pwr_events) {
4083 pt->sample_pwr_events = true;
4084 pt->pwr_events_sample_type = attr.sample_type;
4086 attr.config = PERF_SYNTH_INTEL_CBR;
4087 err = intel_pt_synth_event(session, "cbr", &attr, id);
4091 intel_pt_set_event_name(evlist, id, "cbr");
4094 attr.config = PERF_SYNTH_INTEL_PSB;
4095 err = intel_pt_synth_event(session, "psb", &attr, id);
4099 intel_pt_set_event_name(evlist, id, "psb");
4103 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & INTEL_PT_CFG_PWR_EVT_EN)) {
4104 attr.config = PERF_SYNTH_INTEL_MWAIT;
4105 err = intel_pt_synth_event(session, "mwait", &attr, id);
4109 intel_pt_set_event_name(evlist, id, "mwait");
4112 attr.config = PERF_SYNTH_INTEL_PWRE;
4113 err = intel_pt_synth_event(session, "pwre", &attr, id);
4117 intel_pt_set_event_name(evlist, id, "pwre");
4120 attr.config = PERF_SYNTH_INTEL_EXSTOP;
4121 err = intel_pt_synth_event(session, "exstop", &attr, id);
4125 intel_pt_set_event_name(evlist, id, "exstop");
4128 attr.config = PERF_SYNTH_INTEL_PWRX;
4129 err = intel_pt_synth_event(session, "pwrx", &attr, id);
4133 intel_pt_set_event_name(evlist, id, "pwrx");
4137 if (pt->synth_opts.intr_events && (evsel->core.attr.config & INTEL_PT_CFG_EVT_EN)) {
4138 attr.config = PERF_SYNTH_INTEL_EVT;
4139 err = intel_pt_synth_event(session, "evt", &attr, id);
4142 pt->evt_sample_type = attr.sample_type;
4144 intel_pt_set_event_name(evlist, id, "evt");
4148 if (pt->synth_opts.intr_events && pt->cap_event_trace) {
4149 attr.config = PERF_SYNTH_INTEL_IFLAG_CHG;
4150 err = intel_pt_synth_event(session, "iflag", &attr, id);
4153 pt->iflag_chg_sample_type = attr.sample_type;
4154 pt->iflag_chg_id = id;
4155 intel_pt_set_event_name(evlist, id, "iflag");
4162 static void intel_pt_setup_pebs_events(struct intel_pt *pt)
4164 struct evsel *evsel;
4166 if (!pt->synth_opts.other_events)
4169 evlist__for_each_entry(pt->session->evlist, evsel) {
4170 if (evsel->core.attr.aux_output && evsel->core.id) {
4171 if (pt->single_pebs) {
4172 pt->single_pebs = false;
4175 pt->single_pebs = true;
4176 pt->sample_pebs = true;
4177 pt->pebs_data_src_fmt = intel_pt_data_src_fmt(pt, evsel);
4178 pt->pebs_evsel = evsel;
4183 static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
4185 struct evsel *evsel;
4187 evlist__for_each_entry_reverse(evlist, evsel) {
4188 const char *name = evsel__name(evsel);
4190 if (!strcmp(name, "sched:sched_switch"))
4197 static bool intel_pt_find_switch(struct evlist *evlist)
4199 struct evsel *evsel;
4201 evlist__for_each_entry(evlist, evsel) {
4202 if (evsel->core.attr.context_switch)
4209 static int intel_pt_perf_config(const char *var, const char *value, void *data)
4211 struct intel_pt *pt = data;
4213 if (!strcmp(var, "intel-pt.mispred-all"))
4214 pt->mispred_all = perf_config_bool(var, value);
4216 if (!strcmp(var, "intel-pt.max-loops"))
4217 perf_config_int(&pt->max_loops, var, value);
4222 /* Find least TSC which converts to ns or later */
4223 static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
4227 tsc = perf_time_to_tsc(ns, &pt->tc);
4230 tm = tsc_to_perf_time(tsc, &pt->tc);
4237 tm = tsc_to_perf_time(++tsc, &pt->tc);
4242 /* Find greatest TSC which converts to ns or earlier */
4243 static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
4247 tsc = perf_time_to_tsc(ns, &pt->tc);
4250 tm = tsc_to_perf_time(tsc, &pt->tc);
4257 tm = tsc_to_perf_time(--tsc, &pt->tc);
4262 static int intel_pt_setup_time_ranges(struct intel_pt *pt,
4263 struct itrace_synth_opts *opts)
4265 struct perf_time_interval *p = opts->ptime_range;
4266 int n = opts->range_num;
4269 if (!n || !p || pt->timeless_decoding)
4272 pt->time_ranges = calloc(n, sizeof(struct range));
4273 if (!pt->time_ranges)
4278 intel_pt_log("%s: %u range(s)\n", __func__, n);
4280 for (i = 0; i < n; i++) {
4281 struct range *r = &pt->time_ranges[i];
4282 u64 ts = p[i].start;
4286 * Take care to ensure the TSC range matches the perf-time range
4287 * when converted back to perf-time.
4289 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
4290 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
4292 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
4294 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
4295 i, r->start, r->end);
4301 static int intel_pt_parse_vm_tm_corr_arg(struct intel_pt *pt, char **args)
4303 struct intel_pt_vmcs_info *vmcs_info;
4304 u64 tsc_offset, vmcs;
4313 tsc_offset = strtoull(p, &p, 0);
4318 pt->dflt_tsc_offset = tsc_offset;
4324 vmcs = strtoull(p, &p, 0);
4329 vmcs_info = intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, tsc_offset);
4341 static int intel_pt_parse_vm_tm_corr_args(struct intel_pt *pt)
4343 char *args = pt->synth_opts.vm_tm_corr_args;
4350 ret = intel_pt_parse_vm_tm_corr_arg(pt, &args);
4354 pr_err("Failed to parse VM Time Correlation options\n");
4361 static const char * const intel_pt_info_fmts[] = {
4362 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
4363 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
4364 [INTEL_PT_TIME_MULT] = " Time Multiplier %"PRIu64"\n",
4365 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
4366 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
4367 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
4368 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
4369 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
4370 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
4371 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
4372 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
4373 [INTEL_PT_MTC_FREQ_BITS] = " MTC freq bits %#"PRIx64"\n",
4374 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
4375 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
4376 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
4377 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
4378 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
4381 static void intel_pt_print_info(__u64 *arr, int start, int finish)
4388 for (i = start; i <= finish; i++) {
4389 const char *fmt = intel_pt_info_fmts[i];
4392 fprintf(stdout, fmt, arr[i]);
4396 static void intel_pt_print_info_str(const char *name, const char *str)
4401 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
4404 static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
4406 return auxtrace_info->header.size >=
4407 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
4410 int intel_pt_process_auxtrace_info(union perf_event *event,
4411 struct perf_session *session)
4413 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
4414 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
4415 struct intel_pt *pt;
4420 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
4424 pt = zalloc(sizeof(struct intel_pt));
4428 pt->vmcs_info = RB_ROOT;
4430 addr_filters__init(&pt->filts);
4432 err = perf_config(intel_pt_perf_config, pt);
4436 err = auxtrace_queues__init(&pt->queues);
4440 if (session->itrace_synth_opts->set) {
4441 pt->synth_opts = *session->itrace_synth_opts;
4443 struct itrace_synth_opts *opts = session->itrace_synth_opts;
4445 itrace_synth_opts__set_default(&pt->synth_opts, opts->default_no_sample);
4446 if (!opts->default_no_sample && !opts->inject) {
4447 pt->synth_opts.branches = false;
4448 pt->synth_opts.callchain = true;
4449 pt->synth_opts.add_callchain = true;
4451 pt->synth_opts.thread_stack = opts->thread_stack;
4454 if (!(pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT))
4455 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
4457 pt->session = session;
4458 pt->machine = &session->machines.host; /* No kvm support */
4459 pt->auxtrace_type = auxtrace_info->type;
4460 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
4461 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
4462 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
4463 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
4464 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
4465 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
4466 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
4467 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
4468 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
4469 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
4470 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
4471 INTEL_PT_PER_CPU_MMAPS);
4473 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
4474 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
4475 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
4476 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
4477 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
4478 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
4479 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
4483 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
4484 pt->max_non_turbo_ratio =
4485 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
4486 intel_pt_print_info(&auxtrace_info->priv[0],
4487 INTEL_PT_MAX_NONTURBO_RATIO,
4488 INTEL_PT_MAX_NONTURBO_RATIO);
4491 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
4492 info_end = (void *)auxtrace_info + auxtrace_info->header.size;
4494 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
4497 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
4498 intel_pt_print_info(&auxtrace_info->priv[0],
4499 INTEL_PT_FILTER_STR_LEN,
4500 INTEL_PT_FILTER_STR_LEN);
4502 const char *filter = (const char *)info;
4504 len = roundup(len + 1, 8);
4506 if ((void *)info > info_end) {
4507 pr_err("%s: bad filter string length\n", __func__);
4509 goto err_free_queues;
4511 pt->filter = memdup(filter, len);
4514 goto err_free_queues;
4516 if (session->header.needs_swap)
4517 mem_bswap_64(pt->filter, len);
4518 if (pt->filter[len - 1]) {
4519 pr_err("%s: filter string not null terminated\n", __func__);
4521 goto err_free_queues;
4523 err = addr_filters__parse_bare_filter(&pt->filts,
4526 goto err_free_queues;
4528 intel_pt_print_info_str("Filter string", pt->filter);
4531 if ((void *)info < info_end) {
4532 pt->cap_event_trace = *info++;
4534 fprintf(stdout, " Cap Event Trace %d\n",
4535 pt->cap_event_trace);
4538 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
4539 if (pt->timeless_decoding && !pt->tc.time_mult)
4540 pt->tc.time_mult = 1;
4541 pt->have_tsc = intel_pt_have_tsc(pt);
4542 pt->sampling_mode = intel_pt_sampling_mode(pt);
4543 pt->est_tsc = !pt->timeless_decoding;
4545 if (pt->synth_opts.vm_time_correlation) {
4546 if (pt->timeless_decoding) {
4547 pr_err("Intel PT has no time information for VM Time Correlation\n");
4549 goto err_free_queues;
4551 if (session->itrace_synth_opts->ptime_range) {
4552 pr_err("Time ranges cannot be specified with VM Time Correlation\n");
4554 goto err_free_queues;
4556 /* Currently TSC Offset is calculated using MTC packets */
4557 if (!intel_pt_have_mtc(pt)) {
4558 pr_err("MTC packets must have been enabled for VM Time Correlation\n");
4560 goto err_free_queues;
4562 err = intel_pt_parse_vm_tm_corr_args(pt);
4564 goto err_free_queues;
4567 pt->unknown_thread = thread__new(999999999, 999999999);
4568 if (!pt->unknown_thread) {
4570 goto err_free_queues;
4573 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
4575 goto err_delete_thread;
4576 if (thread__init_maps(pt->unknown_thread, pt->machine)) {
4578 goto err_delete_thread;
4581 pt->auxtrace.process_event = intel_pt_process_event;
4582 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
4583 pt->auxtrace.queue_data = intel_pt_queue_data;
4584 pt->auxtrace.dump_auxtrace_sample = intel_pt_dump_sample;
4585 pt->auxtrace.flush_events = intel_pt_flush;
4586 pt->auxtrace.free_events = intel_pt_free_events;
4587 pt->auxtrace.free = intel_pt_free;
4588 pt->auxtrace.evsel_is_auxtrace = intel_pt_evsel_is_auxtrace;
4589 session->auxtrace = &pt->auxtrace;
4594 if (pt->have_sched_switch == 1) {
4595 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
4596 if (!pt->switch_evsel) {
4597 pr_err("%s: missing sched_switch event\n", __func__);
4599 goto err_delete_thread;
4601 } else if (pt->have_sched_switch == 2 &&
4602 !intel_pt_find_switch(session->evlist)) {
4603 pr_err("%s: missing context_switch attribute flag\n", __func__);
4605 goto err_delete_thread;
4608 if (pt->synth_opts.log) {
4609 bool log_on_error = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR;
4610 unsigned int log_on_error_size = pt->synth_opts.log_on_error_size;
4612 intel_pt_log_enable(log_on_error, log_on_error_size);
4615 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
4616 if (pt->tc.time_mult) {
4617 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
4619 if (!pt->max_non_turbo_ratio)
4620 pt->max_non_turbo_ratio =
4621 (tsc_freq + 50000000) / 100000000;
4622 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
4623 intel_pt_log("Maximum non-turbo ratio %u\n",
4624 pt->max_non_turbo_ratio);
4625 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
4628 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
4630 goto err_delete_thread;
4632 if (pt->synth_opts.calls)
4633 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
4634 PERF_IP_FLAG_TRACE_END;
4635 if (pt->synth_opts.returns)
4636 pt->branches_filter |= PERF_IP_FLAG_RETURN |
4637 PERF_IP_FLAG_TRACE_BEGIN;
4639 if ((pt->synth_opts.callchain || pt->synth_opts.add_callchain) &&
4640 !symbol_conf.use_callchain) {
4641 symbol_conf.use_callchain = true;
4642 if (callchain_register_param(&callchain_param) < 0) {
4643 symbol_conf.use_callchain = false;
4644 pt->synth_opts.callchain = false;
4645 pt->synth_opts.add_callchain = false;
4649 if (pt->synth_opts.add_callchain) {
4650 err = intel_pt_callchain_init(pt);
4652 goto err_delete_thread;
4655 if (pt->synth_opts.last_branch || pt->synth_opts.add_last_branch) {
4656 pt->br_stack_sz = pt->synth_opts.last_branch_sz;
4657 pt->br_stack_sz_plus = pt->br_stack_sz;
4660 if (pt->synth_opts.add_last_branch) {
4661 err = intel_pt_br_stack_init(pt);
4663 goto err_delete_thread;
4665 * Additional branch stack size to cater for tracing from the
4666 * actual sample ip to where the sample time is recorded.
4667 * Measured at about 200 branches, but generously set to 1024.
4668 * If kernel space is not being traced, then add just 1 for the
4669 * branch to kernel space.
4671 if (intel_pt_tracing_kernel(pt))
4672 pt->br_stack_sz_plus += 1024;
4674 pt->br_stack_sz_plus += 1;
4677 pt->use_thread_stack = pt->synth_opts.callchain ||
4678 pt->synth_opts.add_callchain ||
4679 pt->synth_opts.thread_stack ||
4680 pt->synth_opts.last_branch ||
4681 pt->synth_opts.add_last_branch;
4683 pt->callstack = pt->synth_opts.callchain ||
4684 pt->synth_opts.add_callchain ||
4685 pt->synth_opts.thread_stack;
4687 err = intel_pt_synth_events(pt, session);
4689 goto err_delete_thread;
4691 intel_pt_setup_pebs_events(pt);
4693 if (perf_data__is_pipe(session->data)) {
4694 pr_warning("WARNING: Intel PT with pipe mode is not recommended.\n"
4695 " The output cannot relied upon. In particular,\n"
4696 " timestamps and the order of events may be incorrect.\n");
4699 if (pt->sampling_mode || list_empty(&session->auxtrace_index))
4700 err = auxtrace_queue_data(session, true, true);
4702 err = auxtrace_queues__process_index(&pt->queues, session);
4704 goto err_delete_thread;
4706 if (pt->queues.populated)
4707 pt->data_queued = true;
4709 if (pt->timeless_decoding)
4710 pr_debug2("Intel PT decoding without timestamps\n");
4716 thread__zput(pt->unknown_thread);
4718 intel_pt_log_disable();
4719 auxtrace_queues__free(&pt->queues);
4720 session->auxtrace = NULL;
4722 addr_filters__exit(&pt->filts);
4724 zfree(&pt->time_ranges);