blob: b40832419a279b6d5df1f4cbfc6926a4822804b0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
5 */
6
7#include <inttypes.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <errno.h>
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/types.h>
14#include <linux/zalloc.h>
15
16#include "session.h"
17#include "machine.h"
18#include "memswap.h"
19#include "sort.h"
20#include "tool.h"
21#include "event.h"
22#include "evlist.h"
23#include "evsel.h"
24#include "map.h"
25#include "color.h"
26#include "thread.h"
27#include "thread-stack.h"
28#include "symbol.h"
29#include "callchain.h"
30#include "dso.h"
31#include "debug.h"
32#include "auxtrace.h"
33#include "tsc.h"
34#include "intel-pt.h"
35#include "config.h"
36#include "util/synthetic-events.h"
37#include "time-utils.h"
38
39#include "../arch/x86/include/uapi/asm/perf_regs.h"
40
41#include "intel-pt-decoder/intel-pt-log.h"
42#include "intel-pt-decoder/intel-pt-decoder.h"
43#include "intel-pt-decoder/intel-pt-insn-decoder.h"
44#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
45
46#define MAX_TIMESTAMP (~0ULL)
47
48struct range {
49 u64 start;
50 u64 end;
51};
52
53struct intel_pt {
54 struct auxtrace auxtrace;
55 struct auxtrace_queues queues;
56 struct auxtrace_heap heap;
57 u32 auxtrace_type;
58 struct perf_session *session;
59 struct machine *machine;
60 struct evsel *switch_evsel;
61 struct thread *unknown_thread;
62 bool timeless_decoding;
63 bool sampling_mode;
64 bool snapshot_mode;
65 bool per_cpu_mmaps;
66 bool have_tsc;
67 bool data_queued;
68 bool est_tsc;
69 bool sync_switch;
70 bool mispred_all;
71 int have_sched_switch;
72 u32 pmu_type;
73 u64 kernel_start;
74 u64 switch_ip;
75 u64 ptss_ip;
76
77 struct perf_tsc_conversion tc;
78 bool cap_user_time_zero;
79
80 struct itrace_synth_opts synth_opts;
81
82 bool sample_instructions;
83 u64 instructions_sample_type;
84 u64 instructions_id;
85
86 bool sample_branches;
87 u32 branches_filter;
88 u64 branches_sample_type;
89 u64 branches_id;
90
91 bool sample_transactions;
92 u64 transactions_sample_type;
93 u64 transactions_id;
94
95 bool sample_ptwrites;
96 u64 ptwrites_sample_type;
97 u64 ptwrites_id;
98
99 bool sample_pwr_events;
100 u64 pwr_events_sample_type;
101 u64 mwait_id;
102 u64 pwre_id;
103 u64 exstop_id;
104 u64 pwrx_id;
105 u64 cbr_id;
106
107 bool sample_pebs;
108 struct evsel *pebs_evsel;
109
110 u64 tsc_bit;
111 u64 mtc_bit;
112 u64 mtc_freq_bits;
113 u32 tsc_ctc_ratio_n;
114 u32 tsc_ctc_ratio_d;
115 u64 cyc_bit;
116 u64 noretcomp_bit;
117 unsigned max_non_turbo_ratio;
118 unsigned cbr2khz;
119
120 unsigned long num_events;
121
122 char *filter;
123 struct addr_filters filts;
124
125 struct range *time_ranges;
126 unsigned int range_cnt;
127};
128
129enum switch_state {
130 INTEL_PT_SS_NOT_TRACING,
131 INTEL_PT_SS_UNKNOWN,
132 INTEL_PT_SS_TRACING,
133 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
134 INTEL_PT_SS_EXPECTING_SWITCH_IP,
135};
136
137struct intel_pt_queue {
138 struct intel_pt *pt;
139 unsigned int queue_nr;
140 struct auxtrace_buffer *buffer;
141 struct auxtrace_buffer *old_buffer;
142 void *decoder;
143 const struct intel_pt_state *state;
144 struct ip_callchain *chain;
145 struct branch_stack *last_branch;
146 struct branch_stack *last_branch_rb;
147 size_t last_branch_pos;
148 union perf_event *event_buf;
149 bool on_heap;
150 bool stop;
151 bool step_through_buffers;
152 bool use_buffer_pid_tid;
153 bool sync_switch;
154 pid_t pid, tid;
155 int cpu;
156 int switch_state;
157 pid_t next_tid;
158 struct thread *thread;
159 bool exclude_kernel;
160 bool have_sample;
161 u64 time;
162 u64 timestamp;
163 u64 sel_timestamp;
164 bool sel_start;
165 unsigned int sel_idx;
166 u32 flags;
167 u16 insn_len;
168 u64 last_insn_cnt;
169 u64 ipc_insn_cnt;
170 u64 ipc_cyc_cnt;
171 u64 last_in_insn_cnt;
172 u64 last_in_cyc_cnt;
173 u64 last_br_insn_cnt;
174 u64 last_br_cyc_cnt;
175 unsigned int cbr_seen;
176 char insn[INTEL_PT_INSN_BUF_SZ];
177};
178
179static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
180 unsigned char *buf, size_t len)
181{
182 struct intel_pt_pkt packet;
183 size_t pos = 0;
184 int ret, pkt_len, i;
185 char desc[INTEL_PT_PKT_DESC_MAX];
186 const char *color = PERF_COLOR_BLUE;
187 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
188
189 color_fprintf(stdout, color,
190 ". ... Intel Processor Trace data: size %zu bytes\n",
191 len);
192
193 while (len) {
194 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
195 if (ret > 0)
196 pkt_len = ret;
197 else
198 pkt_len = 1;
199 printf(".");
200 color_fprintf(stdout, color, " %08x: ", pos);
201 for (i = 0; i < pkt_len; i++)
202 color_fprintf(stdout, color, " %02x", buf[i]);
203 for (; i < 16; i++)
204 color_fprintf(stdout, color, " ");
205 if (ret > 0) {
206 ret = intel_pt_pkt_desc(&packet, desc,
207 INTEL_PT_PKT_DESC_MAX);
208 if (ret > 0)
209 color_fprintf(stdout, color, " %s\n", desc);
210 } else {
211 color_fprintf(stdout, color, " Bad packet!\n");
212 }
213 pos += pkt_len;
214 buf += pkt_len;
215 len -= pkt_len;
216 }
217}
218
219static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
220 size_t len)
221{
222 printf(".\n");
223 intel_pt_dump(pt, buf, len);
224}
225
226static void intel_pt_log_event(union perf_event *event)
227{
228 FILE *f = intel_pt_log_fp();
229
230 if (!intel_pt_enable_logging || !f)
231 return;
232
233 perf_event__fprintf(event, f);
234}
235
236static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
237 struct auxtrace_buffer *b)
238{
239 bool consecutive = false;
240 void *start;
241
242 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
243 pt->have_tsc, &consecutive);
244 if (!start)
245 return -EINVAL;
246 b->use_size = b->data + b->size - start;
247 b->use_data = start;
248 if (b->use_size && consecutive)
249 b->consecutive = true;
250 return 0;
251}
252
253static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
254 struct auxtrace_buffer *buffer,
255 struct auxtrace_buffer *old_buffer,
256 struct intel_pt_buffer *b)
257{
258 bool might_overlap;
259
260 if (!buffer->data) {
261 int fd = perf_data__fd(ptq->pt->session->data);
262
263 buffer->data = auxtrace_buffer__get_data(buffer, fd);
264 if (!buffer->data)
265 return -ENOMEM;
266 }
267
268 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
269 if (might_overlap && !buffer->consecutive && old_buffer &&
270 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
271 return -ENOMEM;
272
273 if (buffer->use_data) {
274 b->len = buffer->use_size;
275 b->buf = buffer->use_data;
276 } else {
277 b->len = buffer->size;
278 b->buf = buffer->data;
279 }
280 b->ref_timestamp = buffer->reference;
281
282 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
283 b->consecutive = false;
284 b->trace_nr = buffer->buffer_nr + 1;
285 } else {
286 b->consecutive = true;
287 }
288
289 return 0;
290}
291
292/* Do not drop buffers with references - refer intel_pt_get_trace() */
293static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
294 struct auxtrace_buffer *buffer)
295{
296 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
297 return;
298
299 auxtrace_buffer__drop_data(buffer);
300}
301
302/* Must be serialized with respect to intel_pt_get_trace() */
303static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
304 void *cb_data)
305{
306 struct intel_pt_queue *ptq = data;
307 struct auxtrace_buffer *buffer = ptq->buffer;
308 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
309 struct auxtrace_queue *queue;
310 int err = 0;
311
312 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
313
314 while (1) {
315 struct intel_pt_buffer b = { .len = 0 };
316
317 buffer = auxtrace_buffer__next(queue, buffer);
318 if (!buffer)
319 break;
320
321 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
322 if (err)
323 break;
324
325 if (b.len) {
326 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
327 old_buffer = buffer;
328 } else {
329 intel_pt_lookahead_drop_buffer(ptq, buffer);
330 continue;
331 }
332
333 err = cb(&b, cb_data);
334 if (err)
335 break;
336 }
337
338 if (buffer != old_buffer)
339 intel_pt_lookahead_drop_buffer(ptq, buffer);
340 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
341
342 return err;
343}
344
345/*
346 * This function assumes data is processed sequentially only.
347 * Must be serialized with respect to intel_pt_lookahead()
348 */
349static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
350{
351 struct intel_pt_queue *ptq = data;
352 struct auxtrace_buffer *buffer = ptq->buffer;
353 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
354 struct auxtrace_queue *queue;
355 int err;
356
357 if (ptq->stop) {
358 b->len = 0;
359 return 0;
360 }
361
362 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
363
364 buffer = auxtrace_buffer__next(queue, buffer);
365 if (!buffer) {
366 if (old_buffer)
367 auxtrace_buffer__drop_data(old_buffer);
368 b->len = 0;
369 return 0;
370 }
371
372 ptq->buffer = buffer;
373
374 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
375 if (err)
376 return err;
377
378 if (ptq->step_through_buffers)
379 ptq->stop = true;
380
381 if (b->len) {
382 if (old_buffer)
383 auxtrace_buffer__drop_data(old_buffer);
384 ptq->old_buffer = buffer;
385 } else {
386 auxtrace_buffer__drop_data(buffer);
387 return intel_pt_get_trace(b, data);
388 }
389
390 return 0;
391}
392
393struct intel_pt_cache_entry {
394 struct auxtrace_cache_entry entry;
395 u64 insn_cnt;
396 u64 byte_cnt;
397 enum intel_pt_insn_op op;
398 enum intel_pt_insn_branch branch;
399 int length;
400 int32_t rel;
401 char insn[INTEL_PT_INSN_BUF_SZ];
402};
403
404static int intel_pt_config_div(const char *var, const char *value, void *data)
405{
406 int *d = data;
407 long val;
408
409 if (!strcmp(var, "intel-pt.cache-divisor")) {
410 val = strtol(value, NULL, 0);
411 if (val > 0 && val <= INT_MAX)
412 *d = val;
413 }
414
415 return 0;
416}
417
418static int intel_pt_cache_divisor(void)
419{
420 static int d;
421
422 if (d)
423 return d;
424
425 perf_config(intel_pt_config_div, &d);
426
427 if (!d)
428 d = 64;
429
430 return d;
431}
432
433static unsigned int intel_pt_cache_size(struct dso *dso,
434 struct machine *machine)
435{
436 off_t size;
437
438 size = dso__data_size(dso, machine);
439 size /= intel_pt_cache_divisor();
440 if (size < 1000)
441 return 10;
442 if (size > (1 << 21))
443 return 21;
444 return 32 - __builtin_clz(size);
445}
446
447static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
448 struct machine *machine)
449{
450 struct auxtrace_cache *c;
451 unsigned int bits;
452
453 if (dso->auxtrace_cache)
454 return dso->auxtrace_cache;
455
456 bits = intel_pt_cache_size(dso, machine);
457
458 /* Ignoring cache creation failure */
459 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
460
461 dso->auxtrace_cache = c;
462
463 return c;
464}
465
466static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
467 u64 offset, u64 insn_cnt, u64 byte_cnt,
468 struct intel_pt_insn *intel_pt_insn)
469{
470 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
471 struct intel_pt_cache_entry *e;
472 int err;
473
474 if (!c)
475 return -ENOMEM;
476
477 e = auxtrace_cache__alloc_entry(c);
478 if (!e)
479 return -ENOMEM;
480
481 e->insn_cnt = insn_cnt;
482 e->byte_cnt = byte_cnt;
483 e->op = intel_pt_insn->op;
484 e->branch = intel_pt_insn->branch;
485 e->length = intel_pt_insn->length;
486 e->rel = intel_pt_insn->rel;
487 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
488
489 err = auxtrace_cache__add(c, offset, &e->entry);
490 if (err)
491 auxtrace_cache__free_entry(c, e);
492
493 return err;
494}
495
496static struct intel_pt_cache_entry *
497intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
498{
499 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
500
501 if (!c)
502 return NULL;
503
504 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
505}
506
507static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip)
508{
509 return ip >= pt->kernel_start ?
510 PERF_RECORD_MISC_KERNEL :
511 PERF_RECORD_MISC_USER;
512}
513
514static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
515 uint64_t *insn_cnt_ptr, uint64_t *ip,
516 uint64_t to_ip, uint64_t max_insn_cnt,
517 void *data)
518{
519 struct intel_pt_queue *ptq = data;
520 struct machine *machine = ptq->pt->machine;
521 struct thread *thread;
522 struct addr_location al;
523 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
524 ssize_t len;
525 int x86_64;
526 u8 cpumode;
527 u64 offset, start_offset, start_ip;
528 u64 insn_cnt = 0;
529 bool one_map = true;
530
531 intel_pt_insn->length = 0;
532
533 if (to_ip && *ip == to_ip)
534 goto out_no_cache;
535
536 cpumode = intel_pt_cpumode(ptq->pt, *ip);
537
538 thread = ptq->thread;
539 if (!thread) {
540 if (cpumode != PERF_RECORD_MISC_KERNEL)
541 return -EINVAL;
542 thread = ptq->pt->unknown_thread;
543 }
544
545 while (1) {
546 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
547 return -EINVAL;
548
549 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
550 dso__data_status_seen(al.map->dso,
551 DSO_DATA_STATUS_SEEN_ITRACE))
552 return -ENOENT;
553
554 offset = al.map->map_ip(al.map, *ip);
555
556 if (!to_ip && one_map) {
557 struct intel_pt_cache_entry *e;
558
559 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
560 if (e &&
561 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
562 *insn_cnt_ptr = e->insn_cnt;
563 *ip += e->byte_cnt;
564 intel_pt_insn->op = e->op;
565 intel_pt_insn->branch = e->branch;
566 intel_pt_insn->length = e->length;
567 intel_pt_insn->rel = e->rel;
568 memcpy(intel_pt_insn->buf, e->insn,
569 INTEL_PT_INSN_BUF_SZ);
570 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
571 return 0;
572 }
573 }
574
575 start_offset = offset;
576 start_ip = *ip;
577
578 /* Load maps to ensure dso->is_64_bit has been updated */
579 map__load(al.map);
580
581 x86_64 = al.map->dso->is_64_bit;
582
583 while (1) {
584 len = dso__data_read_offset(al.map->dso, machine,
585 offset, buf,
586 INTEL_PT_INSN_BUF_SZ);
587 if (len <= 0)
588 return -EINVAL;
589
590 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
591 return -EINVAL;
592
593 intel_pt_log_insn(intel_pt_insn, *ip);
594
595 insn_cnt += 1;
596
597 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
598 goto out;
599
600 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
601 goto out_no_cache;
602
603 *ip += intel_pt_insn->length;
604
605 if (to_ip && *ip == to_ip) {
606 intel_pt_insn->length = 0;
607 goto out_no_cache;
608 }
609
610 if (*ip >= al.map->end)
611 break;
612
613 offset += intel_pt_insn->length;
614 }
615 one_map = false;
616 }
617out:
618 *insn_cnt_ptr = insn_cnt;
619
620 if (!one_map)
621 goto out_no_cache;
622
623 /*
624 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
625 * entries.
626 */
627 if (to_ip) {
628 struct intel_pt_cache_entry *e;
629
630 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
631 if (e)
632 return 0;
633 }
634
635 /* Ignore cache errors */
636 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
637 *ip - start_ip, intel_pt_insn);
638
639 return 0;
640
641out_no_cache:
642 *insn_cnt_ptr = insn_cnt;
643 return 0;
644}
645
646static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
647 uint64_t offset, const char *filename)
648{
649 struct addr_filter *filt;
650 bool have_filter = false;
651 bool hit_tracestop = false;
652 bool hit_filter = false;
653
654 list_for_each_entry(filt, &pt->filts.head, list) {
655 if (filt->start)
656 have_filter = true;
657
658 if ((filename && !filt->filename) ||
659 (!filename && filt->filename) ||
660 (filename && strcmp(filename, filt->filename)))
661 continue;
662
663 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
664 continue;
665
666 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
667 ip, offset, filename ? filename : "[kernel]",
668 filt->start ? "filter" : "stop",
669 filt->addr, filt->size);
670
671 if (filt->start)
672 hit_filter = true;
673 else
674 hit_tracestop = true;
675 }
676
677 if (!hit_tracestop && !hit_filter)
678 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
679 ip, offset, filename ? filename : "[kernel]");
680
681 return hit_tracestop || (have_filter && !hit_filter);
682}
683
684static int __intel_pt_pgd_ip(uint64_t ip, void *data)
685{
686 struct intel_pt_queue *ptq = data;
687 struct thread *thread;
688 struct addr_location al;
689 u8 cpumode;
690 u64 offset;
691
692 if (ip >= ptq->pt->kernel_start)
693 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
694
695 cpumode = PERF_RECORD_MISC_USER;
696
697 thread = ptq->thread;
698 if (!thread)
699 return -EINVAL;
700
701 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
702 return -EINVAL;
703
704 offset = al.map->map_ip(al.map, ip);
705
706 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
707 al.map->dso->long_name);
708}
709
710static bool intel_pt_pgd_ip(uint64_t ip, void *data)
711{
712 return __intel_pt_pgd_ip(ip, data) > 0;
713}
714
715static bool intel_pt_get_config(struct intel_pt *pt,
716 struct perf_event_attr *attr, u64 *config)
717{
718 if (attr->type == pt->pmu_type) {
719 if (config)
720 *config = attr->config;
721 return true;
722 }
723
724 return false;
725}
726
727static bool intel_pt_exclude_kernel(struct intel_pt *pt)
728{
729 struct evsel *evsel;
730
731 evlist__for_each_entry(pt->session->evlist, evsel) {
732 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
733 !evsel->core.attr.exclude_kernel)
734 return false;
735 }
736 return true;
737}
738
739static bool intel_pt_return_compression(struct intel_pt *pt)
740{
741 struct evsel *evsel;
742 u64 config;
743
744 if (!pt->noretcomp_bit)
745 return true;
746
747 evlist__for_each_entry(pt->session->evlist, evsel) {
748 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
749 (config & pt->noretcomp_bit))
750 return false;
751 }
752 return true;
753}
754
755static bool intel_pt_branch_enable(struct intel_pt *pt)
756{
757 struct evsel *evsel;
758 u64 config;
759
760 evlist__for_each_entry(pt->session->evlist, evsel) {
761 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
762 (config & 1) && !(config & 0x2000))
763 return false;
764 }
765 return true;
766}
767
768static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
769{
770 struct evsel *evsel;
771 unsigned int shift;
772 u64 config;
773
774 if (!pt->mtc_freq_bits)
775 return 0;
776
777 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
778 config >>= 1;
779
780 evlist__for_each_entry(pt->session->evlist, evsel) {
781 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
782 return (config & pt->mtc_freq_bits) >> shift;
783 }
784 return 0;
785}
786
787static bool intel_pt_timeless_decoding(struct intel_pt *pt)
788{
789 struct evsel *evsel;
790 bool timeless_decoding = true;
791 u64 config;
792
793 if (!pt->tsc_bit || !pt->cap_user_time_zero)
794 return true;
795
796 evlist__for_each_entry(pt->session->evlist, evsel) {
797 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
798 return true;
799 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
800 if (config & pt->tsc_bit)
801 timeless_decoding = false;
802 else
803 return true;
804 }
805 }
806 return timeless_decoding;
807}
808
809static bool intel_pt_tracing_kernel(struct intel_pt *pt)
810{
811 struct evsel *evsel;
812
813 evlist__for_each_entry(pt->session->evlist, evsel) {
814 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
815 !evsel->core.attr.exclude_kernel)
816 return true;
817 }
818 return false;
819}
820
821static bool intel_pt_have_tsc(struct intel_pt *pt)
822{
823 struct evsel *evsel;
824 bool have_tsc = false;
825 u64 config;
826
827 if (!pt->tsc_bit)
828 return false;
829
830 evlist__for_each_entry(pt->session->evlist, evsel) {
831 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
832 if (config & pt->tsc_bit)
833 have_tsc = true;
834 else
835 return false;
836 }
837 }
838 return have_tsc;
839}
840
841static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
842{
843 u64 quot, rem;
844
845 quot = ns / pt->tc.time_mult;
846 rem = ns % pt->tc.time_mult;
847 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
848 pt->tc.time_mult;
849}
850
851static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
852 unsigned int queue_nr)
853{
854 struct intel_pt_params params = { .get_trace = 0, };
855 struct perf_env *env = pt->machine->env;
856 struct intel_pt_queue *ptq;
857
858 ptq = zalloc(sizeof(struct intel_pt_queue));
859 if (!ptq)
860 return NULL;
861
862 if (pt->synth_opts.callchain) {
863 size_t sz = sizeof(struct ip_callchain);
864
865 /* Add 1 to callchain_sz for callchain context */
866 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
867 ptq->chain = zalloc(sz);
868 if (!ptq->chain)
869 goto out_free;
870 }
871
872 if (pt->synth_opts.last_branch) {
873 size_t sz = sizeof(struct branch_stack);
874
875 sz += pt->synth_opts.last_branch_sz *
876 sizeof(struct branch_entry);
877 ptq->last_branch = zalloc(sz);
878 if (!ptq->last_branch)
879 goto out_free;
880 ptq->last_branch_rb = zalloc(sz);
881 if (!ptq->last_branch_rb)
882 goto out_free;
883 }
884
885 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
886 if (!ptq->event_buf)
887 goto out_free;
888
889 ptq->pt = pt;
890 ptq->queue_nr = queue_nr;
891 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
892 ptq->pid = -1;
893 ptq->tid = -1;
894 ptq->cpu = -1;
895 ptq->next_tid = -1;
896
897 params.get_trace = intel_pt_get_trace;
898 params.walk_insn = intel_pt_walk_next_insn;
899 params.lookahead = intel_pt_lookahead;
900 params.data = ptq;
901 params.return_compression = intel_pt_return_compression(pt);
902 params.branch_enable = intel_pt_branch_enable(pt);
903 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
904 params.mtc_period = intel_pt_mtc_period(pt);
905 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
906 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
907
908 if (pt->filts.cnt > 0)
909 params.pgd_ip = intel_pt_pgd_ip;
910
911 if (pt->synth_opts.instructions) {
912 if (pt->synth_opts.period) {
913 switch (pt->synth_opts.period_type) {
914 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
915 params.period_type =
916 INTEL_PT_PERIOD_INSTRUCTIONS;
917 params.period = pt->synth_opts.period;
918 break;
919 case PERF_ITRACE_PERIOD_TICKS:
920 params.period_type = INTEL_PT_PERIOD_TICKS;
921 params.period = pt->synth_opts.period;
922 break;
923 case PERF_ITRACE_PERIOD_NANOSECS:
924 params.period_type = INTEL_PT_PERIOD_TICKS;
925 params.period = intel_pt_ns_to_ticks(pt,
926 pt->synth_opts.period);
927 break;
928 default:
929 break;
930 }
931 }
932
933 if (!params.period) {
934 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
935 params.period = 1;
936 }
937 }
938
939 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
940 params.flags |= INTEL_PT_FUP_WITH_NLIP;
941
942 ptq->decoder = intel_pt_decoder_new(&params);
943 if (!ptq->decoder)
944 goto out_free;
945
946 return ptq;
947
948out_free:
949 zfree(&ptq->event_buf);
950 zfree(&ptq->last_branch);
951 zfree(&ptq->last_branch_rb);
952 zfree(&ptq->chain);
953 free(ptq);
954 return NULL;
955}
956
957static void intel_pt_free_queue(void *priv)
958{
959 struct intel_pt_queue *ptq = priv;
960
961 if (!ptq)
962 return;
963 thread__zput(ptq->thread);
964 intel_pt_decoder_free(ptq->decoder);
965 zfree(&ptq->event_buf);
966 zfree(&ptq->last_branch);
967 zfree(&ptq->last_branch_rb);
968 zfree(&ptq->chain);
969 free(ptq);
970}
971
972static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
973 struct auxtrace_queue *queue)
974{
975 struct intel_pt_queue *ptq = queue->priv;
976
977 if (queue->tid == -1 || pt->have_sched_switch) {
978 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
979 if (ptq->tid == -1)
980 ptq->pid = -1;
981 thread__zput(ptq->thread);
982 }
983
984 if (!ptq->thread && ptq->tid != -1)
985 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
986
987 if (ptq->thread) {
988 ptq->pid = ptq->thread->pid_;
989 if (queue->cpu == -1)
990 ptq->cpu = ptq->thread->cpu;
991 }
992}
993
994static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
995{
996 ptq->insn_len = 0;
997 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
998 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
999 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
1000 if (ptq->state->to_ip)
1001 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1002 PERF_IP_FLAG_ASYNC |
1003 PERF_IP_FLAG_INTERRUPT;
1004 else
1005 ptq->flags = PERF_IP_FLAG_BRANCH |
1006 PERF_IP_FLAG_TRACE_END;
1007 ptq->insn_len = 0;
1008 } else {
1009 if (ptq->state->from_ip)
1010 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1011 else
1012 ptq->flags = PERF_IP_FLAG_BRANCH |
1013 PERF_IP_FLAG_TRACE_BEGIN;
1014 if (ptq->state->flags & INTEL_PT_IN_TX)
1015 ptq->flags |= PERF_IP_FLAG_IN_TX;
1016 ptq->insn_len = ptq->state->insn_len;
1017 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
1018 }
1019
1020 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1021 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1022 if (ptq->state->type & INTEL_PT_TRACE_END)
1023 ptq->flags |= PERF_IP_FLAG_TRACE_END;
1024}
1025
1026static void intel_pt_setup_time_range(struct intel_pt *pt,
1027 struct intel_pt_queue *ptq)
1028{
1029 if (!pt->range_cnt)
1030 return;
1031
1032 ptq->sel_timestamp = pt->time_ranges[0].start;
1033 ptq->sel_idx = 0;
1034
1035 if (ptq->sel_timestamp) {
1036 ptq->sel_start = true;
1037 } else {
1038 ptq->sel_timestamp = pt->time_ranges[0].end;
1039 ptq->sel_start = false;
1040 }
1041}
1042
1043static int intel_pt_setup_queue(struct intel_pt *pt,
1044 struct auxtrace_queue *queue,
1045 unsigned int queue_nr)
1046{
1047 struct intel_pt_queue *ptq = queue->priv;
1048
1049 if (list_empty(&queue->head))
1050 return 0;
1051
1052 if (!ptq) {
1053 ptq = intel_pt_alloc_queue(pt, queue_nr);
1054 if (!ptq)
1055 return -ENOMEM;
1056 queue->priv = ptq;
1057
1058 if (queue->cpu != -1)
1059 ptq->cpu = queue->cpu;
1060 ptq->tid = queue->tid;
1061
1062 ptq->cbr_seen = UINT_MAX;
1063
1064 if (pt->sampling_mode && !pt->snapshot_mode &&
1065 pt->timeless_decoding)
1066 ptq->step_through_buffers = true;
1067
1068 ptq->sync_switch = pt->sync_switch;
1069
1070 intel_pt_setup_time_range(pt, ptq);
1071 }
1072
1073 if (!ptq->on_heap &&
1074 (!ptq->sync_switch ||
1075 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1076 const struct intel_pt_state *state;
1077 int ret;
1078
1079 if (pt->timeless_decoding)
1080 return 0;
1081
1082 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1083 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1084 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1085
1086 if (ptq->sel_start && ptq->sel_timestamp) {
1087 ret = intel_pt_fast_forward(ptq->decoder,
1088 ptq->sel_timestamp);
1089 if (ret)
1090 return ret;
1091 }
1092
1093 while (1) {
1094 state = intel_pt_decode(ptq->decoder);
1095 if (state->err) {
1096 if (state->err == INTEL_PT_ERR_NODATA) {
1097 intel_pt_log("queue %u has no timestamp\n",
1098 queue_nr);
1099 return 0;
1100 }
1101 continue;
1102 }
1103 if (state->timestamp)
1104 break;
1105 }
1106
1107 ptq->timestamp = state->timestamp;
1108 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1109 queue_nr, ptq->timestamp);
1110 ptq->state = state;
1111 ptq->have_sample = true;
1112 if (ptq->sel_start && ptq->sel_timestamp &&
1113 ptq->timestamp < ptq->sel_timestamp)
1114 ptq->have_sample = false;
1115 intel_pt_sample_flags(ptq);
1116 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1117 if (ret)
1118 return ret;
1119 ptq->on_heap = true;
1120 }
1121
1122 return 0;
1123}
1124
1125static int intel_pt_setup_queues(struct intel_pt *pt)
1126{
1127 unsigned int i;
1128 int ret;
1129
1130 for (i = 0; i < pt->queues.nr_queues; i++) {
1131 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1132 if (ret)
1133 return ret;
1134 }
1135 return 0;
1136}
1137
1138static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
1139{
1140 struct branch_stack *bs_src = ptq->last_branch_rb;
1141 struct branch_stack *bs_dst = ptq->last_branch;
1142 size_t nr = 0;
1143
1144 bs_dst->nr = bs_src->nr;
1145
1146 if (!bs_src->nr)
1147 return;
1148
1149 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1150 memcpy(&bs_dst->entries[0],
1151 &bs_src->entries[ptq->last_branch_pos],
1152 sizeof(struct branch_entry) * nr);
1153
1154 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1155 memcpy(&bs_dst->entries[nr],
1156 &bs_src->entries[0],
1157 sizeof(struct branch_entry) * ptq->last_branch_pos);
1158 }
1159}
1160
1161static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1162{
1163 ptq->last_branch_pos = 0;
1164 ptq->last_branch_rb->nr = 0;
1165}
1166
1167static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1168{
1169 const struct intel_pt_state *state = ptq->state;
1170 struct branch_stack *bs = ptq->last_branch_rb;
1171 struct branch_entry *be;
1172
1173 if (!ptq->last_branch_pos)
1174 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1175
1176 ptq->last_branch_pos -= 1;
1177
1178 be = &bs->entries[ptq->last_branch_pos];
1179 be->from = state->from_ip;
1180 be->to = state->to_ip;
1181 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1182 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1183 /* No support for mispredict */
1184 be->flags.mispred = ptq->pt->mispred_all;
1185
1186 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1187 bs->nr += 1;
1188}
1189
1190static inline bool intel_pt_skip_event(struct intel_pt *pt)
1191{
1192 return pt->synth_opts.initial_skip &&
1193 pt->num_events++ < pt->synth_opts.initial_skip;
1194}
1195
1196/*
1197 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
1198 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
1199 * from this decoder state.
1200 */
1201static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
1202{
1203 return pt->synth_opts.initial_skip &&
1204 pt->num_events + 4 < pt->synth_opts.initial_skip;
1205}
1206
1207static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1208 union perf_event *event,
1209 struct perf_sample *sample)
1210{
1211 event->sample.header.type = PERF_RECORD_SAMPLE;
1212 event->sample.header.size = sizeof(struct perf_event_header);
1213
1214 sample->pid = ptq->pid;
1215 sample->tid = ptq->tid;
1216 sample->cpu = ptq->cpu;
1217 sample->insn_len = ptq->insn_len;
1218 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1219}
1220
1221static void intel_pt_prep_b_sample(struct intel_pt *pt,
1222 struct intel_pt_queue *ptq,
1223 union perf_event *event,
1224 struct perf_sample *sample)
1225{
1226 intel_pt_prep_a_sample(ptq, event, sample);
1227
1228 if (!pt->timeless_decoding)
1229 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1230
1231 sample->ip = ptq->state->from_ip;
1232 sample->cpumode = intel_pt_cpumode(pt, sample->ip);
1233 sample->addr = ptq->state->to_ip;
1234 sample->period = 1;
1235 sample->flags = ptq->flags;
1236
1237 event->sample.header.misc = sample->cpumode;
1238}
1239
1240static int intel_pt_inject_event(union perf_event *event,
1241 struct perf_sample *sample, u64 type)
1242{
1243 event->header.size = perf_event__sample_event_size(sample, type, 0);
1244 return perf_event__synthesize_sample(event, type, 0, sample);
1245}
1246
1247static inline int intel_pt_opt_inject(struct intel_pt *pt,
1248 union perf_event *event,
1249 struct perf_sample *sample, u64 type)
1250{
1251 if (!pt->synth_opts.inject)
1252 return 0;
1253
1254 return intel_pt_inject_event(event, sample, type);
1255}
1256
1257static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1258 union perf_event *event,
1259 struct perf_sample *sample, u64 type)
1260{
1261 int ret;
1262
1263 ret = intel_pt_opt_inject(pt, event, sample, type);
1264 if (ret)
1265 return ret;
1266
1267 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1268 if (ret)
1269 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1270
1271 return ret;
1272}
1273
1274static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1275{
1276 struct intel_pt *pt = ptq->pt;
1277 union perf_event *event = ptq->event_buf;
1278 struct perf_sample sample = { .ip = 0, };
1279 struct dummy_branch_stack {
1280 u64 nr;
1281 struct branch_entry entries;
1282 } dummy_bs;
1283
1284 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1285 return 0;
1286
1287 if (intel_pt_skip_event(pt))
1288 return 0;
1289
1290 intel_pt_prep_b_sample(pt, ptq, event, &sample);
1291
1292 sample.id = ptq->pt->branches_id;
1293 sample.stream_id = ptq->pt->branches_id;
1294
1295 /*
1296 * perf report cannot handle events without a branch stack when using
1297 * SORT_MODE__BRANCH so make a dummy one.
1298 */
1299 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1300 dummy_bs = (struct dummy_branch_stack){
1301 .nr = 1,
1302 .entries = {
1303 .from = sample.ip,
1304 .to = sample.addr,
1305 },
1306 };
1307 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1308 }
1309
1310 if (ptq->state->flags & INTEL_PT_SAMPLE_IPC)
1311 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
1312 if (sample.cyc_cnt) {
1313 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1314 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1315 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1316 }
1317
1318 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1319 pt->branches_sample_type);
1320}
1321
1322static void intel_pt_prep_sample(struct intel_pt *pt,
1323 struct intel_pt_queue *ptq,
1324 union perf_event *event,
1325 struct perf_sample *sample)
1326{
1327 intel_pt_prep_b_sample(pt, ptq, event, sample);
1328
1329 if (pt->synth_opts.callchain) {
1330 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1331 pt->synth_opts.callchain_sz + 1,
1332 sample->ip, pt->kernel_start);
1333 sample->callchain = ptq->chain;
1334 }
1335
1336 if (pt->synth_opts.last_branch) {
1337 intel_pt_copy_last_branch_rb(ptq);
1338 sample->branch_stack = ptq->last_branch;
1339 }
1340}
1341
1342static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1343 struct intel_pt_queue *ptq,
1344 union perf_event *event,
1345 struct perf_sample *sample,
1346 u64 type)
1347{
1348 int ret;
1349
1350 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1351
1352 if (pt->synth_opts.last_branch)
1353 intel_pt_reset_last_branch_rb(ptq);
1354
1355 return ret;
1356}
1357
1358static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1359{
1360 struct intel_pt *pt = ptq->pt;
1361 union perf_event *event = ptq->event_buf;
1362 struct perf_sample sample = { .ip = 0, };
1363
1364 if (intel_pt_skip_event(pt))
1365 return 0;
1366
1367 intel_pt_prep_sample(pt, ptq, event, &sample);
1368
1369 sample.id = ptq->pt->instructions_id;
1370 sample.stream_id = ptq->pt->instructions_id;
1371 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1372
1373 if (ptq->state->flags & INTEL_PT_SAMPLE_IPC)
1374 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
1375 if (sample.cyc_cnt) {
1376 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1377 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1378 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1379 }
1380
1381 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1382
1383 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1384 pt->instructions_sample_type);
1385}
1386
1387static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1388{
1389 struct intel_pt *pt = ptq->pt;
1390 union perf_event *event = ptq->event_buf;
1391 struct perf_sample sample = { .ip = 0, };
1392
1393 if (intel_pt_skip_event(pt))
1394 return 0;
1395
1396 intel_pt_prep_sample(pt, ptq, event, &sample);
1397
1398 sample.id = ptq->pt->transactions_id;
1399 sample.stream_id = ptq->pt->transactions_id;
1400
1401 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1402 pt->transactions_sample_type);
1403}
1404
1405static void intel_pt_prep_p_sample(struct intel_pt *pt,
1406 struct intel_pt_queue *ptq,
1407 union perf_event *event,
1408 struct perf_sample *sample)
1409{
1410 intel_pt_prep_sample(pt, ptq, event, sample);
1411
1412 /*
1413 * Zero IP is used to mean "trace start" but that is not the case for
1414 * power or PTWRITE events with no IP, so clear the flags.
1415 */
1416 if (!sample->ip)
1417 sample->flags = 0;
1418}
1419
1420static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1421{
1422 struct intel_pt *pt = ptq->pt;
1423 union perf_event *event = ptq->event_buf;
1424 struct perf_sample sample = { .ip = 0, };
1425 struct perf_synth_intel_ptwrite raw;
1426
1427 if (intel_pt_skip_event(pt))
1428 return 0;
1429
1430 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1431
1432 sample.id = ptq->pt->ptwrites_id;
1433 sample.stream_id = ptq->pt->ptwrites_id;
1434
1435 raw.flags = 0;
1436 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1437 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1438
1439 sample.raw_size = perf_synth__raw_size(raw);
1440 sample.raw_data = perf_synth__raw_data(&raw);
1441
1442 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1443 pt->ptwrites_sample_type);
1444}
1445
1446static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1447{
1448 struct intel_pt *pt = ptq->pt;
1449 union perf_event *event = ptq->event_buf;
1450 struct perf_sample sample = { .ip = 0, };
1451 struct perf_synth_intel_cbr raw;
1452 u32 flags;
1453
1454 if (intel_pt_skip_cbr_event(pt))
1455 return 0;
1456
1457 ptq->cbr_seen = ptq->state->cbr;
1458
1459 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1460
1461 sample.id = ptq->pt->cbr_id;
1462 sample.stream_id = ptq->pt->cbr_id;
1463
1464 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1465 raw.flags = cpu_to_le32(flags);
1466 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1467 raw.reserved3 = 0;
1468
1469 sample.raw_size = perf_synth__raw_size(raw);
1470 sample.raw_data = perf_synth__raw_data(&raw);
1471
1472 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1473 pt->pwr_events_sample_type);
1474}
1475
1476static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1477{
1478 struct intel_pt *pt = ptq->pt;
1479 union perf_event *event = ptq->event_buf;
1480 struct perf_sample sample = { .ip = 0, };
1481 struct perf_synth_intel_mwait raw;
1482
1483 if (intel_pt_skip_event(pt))
1484 return 0;
1485
1486 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1487
1488 sample.id = ptq->pt->mwait_id;
1489 sample.stream_id = ptq->pt->mwait_id;
1490
1491 raw.reserved = 0;
1492 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1493
1494 sample.raw_size = perf_synth__raw_size(raw);
1495 sample.raw_data = perf_synth__raw_data(&raw);
1496
1497 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1498 pt->pwr_events_sample_type);
1499}
1500
1501static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1502{
1503 struct intel_pt *pt = ptq->pt;
1504 union perf_event *event = ptq->event_buf;
1505 struct perf_sample sample = { .ip = 0, };
1506 struct perf_synth_intel_pwre raw;
1507
1508 if (intel_pt_skip_event(pt))
1509 return 0;
1510
1511 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1512
1513 sample.id = ptq->pt->pwre_id;
1514 sample.stream_id = ptq->pt->pwre_id;
1515
1516 raw.reserved = 0;
1517 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1518
1519 sample.raw_size = perf_synth__raw_size(raw);
1520 sample.raw_data = perf_synth__raw_data(&raw);
1521
1522 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1523 pt->pwr_events_sample_type);
1524}
1525
1526static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1527{
1528 struct intel_pt *pt = ptq->pt;
1529 union perf_event *event = ptq->event_buf;
1530 struct perf_sample sample = { .ip = 0, };
1531 struct perf_synth_intel_exstop raw;
1532
1533 if (intel_pt_skip_event(pt))
1534 return 0;
1535
1536 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1537
1538 sample.id = ptq->pt->exstop_id;
1539 sample.stream_id = ptq->pt->exstop_id;
1540
1541 raw.flags = 0;
1542 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1543
1544 sample.raw_size = perf_synth__raw_size(raw);
1545 sample.raw_data = perf_synth__raw_data(&raw);
1546
1547 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1548 pt->pwr_events_sample_type);
1549}
1550
1551static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1552{
1553 struct intel_pt *pt = ptq->pt;
1554 union perf_event *event = ptq->event_buf;
1555 struct perf_sample sample = { .ip = 0, };
1556 struct perf_synth_intel_pwrx raw;
1557
1558 if (intel_pt_skip_event(pt))
1559 return 0;
1560
1561 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1562
1563 sample.id = ptq->pt->pwrx_id;
1564 sample.stream_id = ptq->pt->pwrx_id;
1565
1566 raw.reserved = 0;
1567 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1568
1569 sample.raw_size = perf_synth__raw_size(raw);
1570 sample.raw_data = perf_synth__raw_data(&raw);
1571
1572 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1573 pt->pwr_events_sample_type);
1574}
1575
1576/*
1577 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
1578 * intel_pt_add_gp_regs().
1579 */
1580static const int pebs_gp_regs[] = {
1581 [PERF_REG_X86_FLAGS] = 1,
1582 [PERF_REG_X86_IP] = 2,
1583 [PERF_REG_X86_AX] = 3,
1584 [PERF_REG_X86_CX] = 4,
1585 [PERF_REG_X86_DX] = 5,
1586 [PERF_REG_X86_BX] = 6,
1587 [PERF_REG_X86_SP] = 7,
1588 [PERF_REG_X86_BP] = 8,
1589 [PERF_REG_X86_SI] = 9,
1590 [PERF_REG_X86_DI] = 10,
1591 [PERF_REG_X86_R8] = 11,
1592 [PERF_REG_X86_R9] = 12,
1593 [PERF_REG_X86_R10] = 13,
1594 [PERF_REG_X86_R11] = 14,
1595 [PERF_REG_X86_R12] = 15,
1596 [PERF_REG_X86_R13] = 16,
1597 [PERF_REG_X86_R14] = 17,
1598 [PERF_REG_X86_R15] = 18,
1599};
1600
1601static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
1602 const struct intel_pt_blk_items *items,
1603 u64 regs_mask)
1604{
1605 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
1606 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
1607 u32 bit;
1608 int i;
1609
1610 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
1611 /* Get the PEBS gp_regs array index */
1612 int n = pebs_gp_regs[i] - 1;
1613
1614 if (n < 0)
1615 continue;
1616 /*
1617 * Add only registers that were requested (i.e. 'regs_mask') and
1618 * that were provided (i.e. 'mask'), and update the resulting
1619 * mask (i.e. 'intr_regs->mask') accordingly.
1620 */
1621 if (mask & 1 << n && regs_mask & bit) {
1622 intr_regs->mask |= bit;
1623 *pos++ = gp_regs[n];
1624 }
1625 }
1626
1627 return pos;
1628}
1629
1630#ifndef PERF_REG_X86_XMM0
1631#define PERF_REG_X86_XMM0 32
1632#endif
1633
1634static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
1635 const struct intel_pt_blk_items *items,
1636 u64 regs_mask)
1637{
1638 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
1639 const u64 *xmm = items->xmm;
1640
1641 /*
1642 * If there are any XMM registers, then there should be all of them.
1643 * Nevertheless, follow the logic to add only registers that were
1644 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
1645 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
1646 */
1647 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
1648
1649 for (; mask; mask >>= 1, xmm++) {
1650 if (mask & 1)
1651 *pos++ = *xmm;
1652 }
1653}
1654
1655#define LBR_INFO_MISPRED (1ULL << 63)
1656#define LBR_INFO_IN_TX (1ULL << 62)
1657#define LBR_INFO_ABORT (1ULL << 61)
1658#define LBR_INFO_CYCLES 0xffff
1659
1660/* Refer kernel's intel_pmu_store_pebs_lbrs() */
1661static u64 intel_pt_lbr_flags(u64 info)
1662{
1663 union {
1664 struct branch_flags flags;
1665 u64 result;
1666 } u = {
1667 .flags = {
1668 .mispred = !!(info & LBR_INFO_MISPRED),
1669 .predicted = !(info & LBR_INFO_MISPRED),
1670 .in_tx = !!(info & LBR_INFO_IN_TX),
1671 .abort = !!(info & LBR_INFO_ABORT),
1672 .cycles = info & LBR_INFO_CYCLES,
1673 }
1674 };
1675
1676 return u.result;
1677}
1678
1679static void intel_pt_add_lbrs(struct branch_stack *br_stack,
1680 const struct intel_pt_blk_items *items)
1681{
1682 u64 *to;
1683 int i;
1684
1685 br_stack->nr = 0;
1686
1687 to = &br_stack->entries[0].from;
1688
1689 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
1690 u32 mask = items->mask[i];
1691 const u64 *from = items->val[i];
1692
1693 for (; mask; mask >>= 3, from += 3) {
1694 if ((mask & 7) == 7) {
1695 *to++ = from[0];
1696 *to++ = from[1];
1697 *to++ = intel_pt_lbr_flags(from[2]);
1698 br_stack->nr += 1;
1699 }
1700 }
1701 }
1702}
1703
1704/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1705#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3)
1706
1707static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
1708{
1709 const struct intel_pt_blk_items *items = &ptq->state->items;
1710 struct perf_sample sample = { .ip = 0, };
1711 union perf_event *event = ptq->event_buf;
1712 struct intel_pt *pt = ptq->pt;
1713 struct evsel *evsel = pt->pebs_evsel;
1714 u64 sample_type = evsel->core.attr.sample_type;
1715 u64 id = evsel->core.id[0];
1716 u8 cpumode;
1717 u64 regs[8 * sizeof(sample.intr_regs.mask)];
1718
1719 if (intel_pt_skip_event(pt))
1720 return 0;
1721
1722 intel_pt_prep_a_sample(ptq, event, &sample);
1723
1724 sample.id = id;
1725 sample.stream_id = id;
1726
1727 if (!evsel->core.attr.freq)
1728 sample.period = evsel->core.attr.sample_period;
1729
1730 /* No support for non-zero CS base */
1731 if (items->has_ip)
1732 sample.ip = items->ip;
1733 else if (items->has_rip)
1734 sample.ip = items->rip;
1735 else
1736 sample.ip = ptq->state->from_ip;
1737
1738 /* No support for guest mode at this time */
1739 cpumode = sample.ip < ptq->pt->kernel_start ?
1740 PERF_RECORD_MISC_USER :
1741 PERF_RECORD_MISC_KERNEL;
1742
1743 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
1744
1745 sample.cpumode = cpumode;
1746
1747 if (sample_type & PERF_SAMPLE_TIME) {
1748 u64 timestamp = 0;
1749
1750 if (items->has_timestamp)
1751 timestamp = items->timestamp;
1752 else if (!pt->timeless_decoding)
1753 timestamp = ptq->timestamp;
1754 if (timestamp)
1755 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
1756 }
1757
1758 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
1759 pt->synth_opts.callchain) {
1760 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1761 pt->synth_opts.callchain_sz, sample.ip,
1762 pt->kernel_start);
1763 sample.callchain = ptq->chain;
1764 }
1765
1766 if (sample_type & PERF_SAMPLE_REGS_INTR &&
1767 (items->mask[INTEL_PT_GP_REGS_POS] ||
1768 items->mask[INTEL_PT_XMM_POS])) {
1769 u64 regs_mask = evsel->core.attr.sample_regs_intr;
1770 u64 *pos;
1771
1772 sample.intr_regs.abi = items->is_32_bit ?
1773 PERF_SAMPLE_REGS_ABI_32 :
1774 PERF_SAMPLE_REGS_ABI_64;
1775 sample.intr_regs.regs = regs;
1776
1777 pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
1778
1779 intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
1780 }
1781
1782 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1783 struct {
1784 struct branch_stack br_stack;
1785 struct branch_entry entries[LBRS_MAX];
1786 } br;
1787
1788 if (items->mask[INTEL_PT_LBR_0_POS] ||
1789 items->mask[INTEL_PT_LBR_1_POS] ||
1790 items->mask[INTEL_PT_LBR_2_POS]) {
1791 intel_pt_add_lbrs(&br.br_stack, items);
1792 sample.branch_stack = &br.br_stack;
1793 } else if (pt->synth_opts.last_branch) {
1794 intel_pt_copy_last_branch_rb(ptq);
1795 sample.branch_stack = ptq->last_branch;
1796 } else {
1797 br.br_stack.nr = 0;
1798 sample.branch_stack = &br.br_stack;
1799 }
1800 }
1801
1802 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
1803 sample.addr = items->mem_access_address;
1804
1805 if (sample_type & PERF_SAMPLE_WEIGHT) {
1806 /*
1807 * Refer kernel's setup_pebs_adaptive_sample_data() and
1808 * intel_hsw_weight().
1809 */
1810 if (items->has_mem_access_latency)
1811 sample.weight = items->mem_access_latency;
1812 if (!sample.weight && items->has_tsx_aux_info) {
1813 /* Cycles last block */
1814 sample.weight = (u32)items->tsx_aux_info;
1815 }
1816 }
1817
1818 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
1819 u64 ax = items->has_rax ? items->rax : 0;
1820 /* Refer kernel's intel_hsw_transaction() */
1821 u64 txn = (u8)(items->tsx_aux_info >> 32);
1822
1823 /* For RTM XABORTs also log the abort code from AX */
1824 if (txn & PERF_TXN_TRANSACTION && ax & 1)
1825 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1826 sample.transaction = txn;
1827 }
1828
1829 return intel_pt_deliver_synth_event(pt, ptq, event, &sample, sample_type);
1830}
1831
1832static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1833 pid_t pid, pid_t tid, u64 ip, u64 timestamp)
1834{
1835 union perf_event event;
1836 char msg[MAX_AUXTRACE_ERROR_MSG];
1837 int err;
1838
1839 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1840
1841 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1842 code, cpu, pid, tid, ip, msg, timestamp);
1843
1844 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1845 if (err)
1846 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1847 err);
1848
1849 return err;
1850}
1851
1852static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
1853 const struct intel_pt_state *state)
1854{
1855 struct intel_pt *pt = ptq->pt;
1856 u64 tm = ptq->timestamp;
1857
1858 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
1859
1860 return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
1861 ptq->tid, state->from_ip, tm);
1862}
1863
1864static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1865{
1866 struct auxtrace_queue *queue;
1867 pid_t tid = ptq->next_tid;
1868 int err;
1869
1870 if (tid == -1)
1871 return 0;
1872
1873 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1874
1875 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1876
1877 queue = &pt->queues.queue_array[ptq->queue_nr];
1878 intel_pt_set_pid_tid_cpu(pt, queue);
1879
1880 ptq->next_tid = -1;
1881
1882 return err;
1883}
1884
1885static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1886{
1887 struct intel_pt *pt = ptq->pt;
1888
1889 return ip == pt->switch_ip &&
1890 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1891 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1892 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1893}
1894
1895#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
1896 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
1897
1898static int intel_pt_sample(struct intel_pt_queue *ptq)
1899{
1900 const struct intel_pt_state *state = ptq->state;
1901 struct intel_pt *pt = ptq->pt;
1902 int err;
1903
1904 if (!ptq->have_sample)
1905 return 0;
1906
1907 ptq->have_sample = false;
1908
1909 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
1910 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
1911
1912 /*
1913 * Do PEBS first to allow for the possibility that the PEBS timestamp
1914 * precedes the current timestamp.
1915 */
1916 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
1917 err = intel_pt_synth_pebs_sample(ptq);
1918 if (err)
1919 return err;
1920 }
1921
1922 if (pt->sample_pwr_events) {
1923 if (ptq->state->cbr != ptq->cbr_seen) {
1924 err = intel_pt_synth_cbr_sample(ptq);
1925 if (err)
1926 return err;
1927 }
1928 if (state->type & INTEL_PT_PWR_EVT) {
1929 if (state->type & INTEL_PT_MWAIT_OP) {
1930 err = intel_pt_synth_mwait_sample(ptq);
1931 if (err)
1932 return err;
1933 }
1934 if (state->type & INTEL_PT_PWR_ENTRY) {
1935 err = intel_pt_synth_pwre_sample(ptq);
1936 if (err)
1937 return err;
1938 }
1939 if (state->type & INTEL_PT_EX_STOP) {
1940 err = intel_pt_synth_exstop_sample(ptq);
1941 if (err)
1942 return err;
1943 }
1944 if (state->type & INTEL_PT_PWR_EXIT) {
1945 err = intel_pt_synth_pwrx_sample(ptq);
1946 if (err)
1947 return err;
1948 }
1949 }
1950 }
1951
1952 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
1953 err = intel_pt_synth_instruction_sample(ptq);
1954 if (err)
1955 return err;
1956 }
1957
1958 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
1959 err = intel_pt_synth_transaction_sample(ptq);
1960 if (err)
1961 return err;
1962 }
1963
1964 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
1965 err = intel_pt_synth_ptwrite_sample(ptq);
1966 if (err)
1967 return err;
1968 }
1969
1970 if (!(state->type & INTEL_PT_BRANCH))
1971 return 0;
1972
1973 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
1974 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags, state->from_ip,
1975 state->to_ip, ptq->insn_len,
1976 state->trace_nr);
1977 else
1978 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
1979
1980 if (pt->sample_branches) {
1981 err = intel_pt_synth_branch_sample(ptq);
1982 if (err)
1983 return err;
1984 }
1985
1986 if (pt->synth_opts.last_branch)
1987 intel_pt_update_last_branch_rb(ptq);
1988
1989 if (!ptq->sync_switch)
1990 return 0;
1991
1992 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1993 switch (ptq->switch_state) {
1994 case INTEL_PT_SS_NOT_TRACING:
1995 case INTEL_PT_SS_UNKNOWN:
1996 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1997 err = intel_pt_next_tid(pt, ptq);
1998 if (err)
1999 return err;
2000 ptq->switch_state = INTEL_PT_SS_TRACING;
2001 break;
2002 default:
2003 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
2004 return 1;
2005 }
2006 } else if (!state->to_ip) {
2007 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2008 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
2009 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2010 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2011 state->to_ip == pt->ptss_ip &&
2012 (ptq->flags & PERF_IP_FLAG_CALL)) {
2013 ptq->switch_state = INTEL_PT_SS_TRACING;
2014 }
2015
2016 return 0;
2017}
2018
2019static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
2020{
2021 struct machine *machine = pt->machine;
2022 struct map *map;
2023 struct symbol *sym, *start;
2024 u64 ip, switch_ip = 0;
2025 const char *ptss;
2026
2027 if (ptss_ip)
2028 *ptss_ip = 0;
2029
2030 map = machine__kernel_map(machine);
2031 if (!map)
2032 return 0;
2033
2034 if (map__load(map))
2035 return 0;
2036
2037 start = dso__first_symbol(map->dso);
2038
2039 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2040 if (sym->binding == STB_GLOBAL &&
2041 !strcmp(sym->name, "__switch_to")) {
2042 ip = map->unmap_ip(map, sym->start);
2043 if (ip >= map->start && ip < map->end) {
2044 switch_ip = ip;
2045 break;
2046 }
2047 }
2048 }
2049
2050 if (!switch_ip || !ptss_ip)
2051 return 0;
2052
2053 if (pt->have_sched_switch == 1)
2054 ptss = "perf_trace_sched_switch";
2055 else
2056 ptss = "__perf_event_task_sched_out";
2057
2058 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2059 if (!strcmp(sym->name, ptss)) {
2060 ip = map->unmap_ip(map, sym->start);
2061 if (ip >= map->start && ip < map->end) {
2062 *ptss_ip = ip;
2063 break;
2064 }
2065 }
2066 }
2067
2068 return switch_ip;
2069}
2070
2071static void intel_pt_enable_sync_switch(struct intel_pt *pt)
2072{
2073 unsigned int i;
2074
2075 pt->sync_switch = true;
2076
2077 for (i = 0; i < pt->queues.nr_queues; i++) {
2078 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2079 struct intel_pt_queue *ptq = queue->priv;
2080
2081 if (ptq)
2082 ptq->sync_switch = true;
2083 }
2084}
2085
2086/*
2087 * To filter against time ranges, it is only necessary to look at the next start
2088 * or end time.
2089 */
2090static bool intel_pt_next_time(struct intel_pt_queue *ptq)
2091{
2092 struct intel_pt *pt = ptq->pt;
2093
2094 if (ptq->sel_start) {
2095 /* Next time is an end time */
2096 ptq->sel_start = false;
2097 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
2098 return true;
2099 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
2100 /* Next time is a start time */
2101 ptq->sel_start = true;
2102 ptq->sel_idx += 1;
2103 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
2104 return true;
2105 }
2106
2107 /* No next time */
2108 return false;
2109}
2110
2111static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
2112{
2113 int err;
2114
2115 while (1) {
2116 if (ptq->sel_start) {
2117 if (ptq->timestamp >= ptq->sel_timestamp) {
2118 /* After start time, so consider next time */
2119 intel_pt_next_time(ptq);
2120 if (!ptq->sel_timestamp) {
2121 /* No end time */
2122 return 0;
2123 }
2124 /* Check against end time */
2125 continue;
2126 }
2127 /* Before start time, so fast forward */
2128 ptq->have_sample = false;
2129 if (ptq->sel_timestamp > *ff_timestamp) {
2130 if (ptq->sync_switch) {
2131 intel_pt_next_tid(ptq->pt, ptq);
2132 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2133 }
2134 *ff_timestamp = ptq->sel_timestamp;
2135 err = intel_pt_fast_forward(ptq->decoder,
2136 ptq->sel_timestamp);
2137 if (err)
2138 return err;
2139 }
2140 return 0;
2141 } else if (ptq->timestamp > ptq->sel_timestamp) {
2142 /* After end time, so consider next time */
2143 if (!intel_pt_next_time(ptq)) {
2144 /* No next time range, so stop decoding */
2145 ptq->have_sample = false;
2146 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2147 return 1;
2148 }
2149 /* Check against next start time */
2150 continue;
2151 } else {
2152 /* Before end time */
2153 return 0;
2154 }
2155 }
2156}
2157
2158static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
2159{
2160 const struct intel_pt_state *state = ptq->state;
2161 struct intel_pt *pt = ptq->pt;
2162 u64 ff_timestamp = 0;
2163 int err;
2164
2165 if (!pt->kernel_start) {
2166 pt->kernel_start = machine__kernel_start(pt->machine);
2167 if (pt->per_cpu_mmaps &&
2168 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
2169 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
2170 !pt->sampling_mode) {
2171 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
2172 if (pt->switch_ip) {
2173 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
2174 pt->switch_ip, pt->ptss_ip);
2175 intel_pt_enable_sync_switch(pt);
2176 }
2177 }
2178 }
2179
2180 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
2181 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2182 while (1) {
2183 err = intel_pt_sample(ptq);
2184 if (err)
2185 return err;
2186
2187 state = intel_pt_decode(ptq->decoder);
2188 if (state->err) {
2189 if (state->err == INTEL_PT_ERR_NODATA)
2190 return 1;
2191 if (ptq->sync_switch &&
2192 state->from_ip >= pt->kernel_start) {
2193 ptq->sync_switch = false;
2194 intel_pt_next_tid(pt, ptq);
2195 }
2196 if (pt->synth_opts.errors) {
2197 err = intel_ptq_synth_error(ptq, state);
2198 if (err)
2199 return err;
2200 }
2201 continue;
2202 }
2203
2204 ptq->state = state;
2205 ptq->have_sample = true;
2206 intel_pt_sample_flags(ptq);
2207
2208 /* Use estimated TSC upon return to user space */
2209 if (pt->est_tsc &&
2210 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
2211 state->to_ip && state->to_ip < pt->kernel_start) {
2212 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2213 state->timestamp, state->est_timestamp);
2214 ptq->timestamp = state->est_timestamp;
2215 /* Use estimated TSC in unknown switch state */
2216 } else if (ptq->sync_switch &&
2217 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2218 intel_pt_is_switch_ip(ptq, state->to_ip) &&
2219 ptq->next_tid == -1) {
2220 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2221 state->timestamp, state->est_timestamp);
2222 ptq->timestamp = state->est_timestamp;
2223 } else if (state->timestamp > ptq->timestamp) {
2224 ptq->timestamp = state->timestamp;
2225 }
2226
2227 if (ptq->sel_timestamp) {
2228 err = intel_pt_time_filter(ptq, &ff_timestamp);
2229 if (err)
2230 return err;
2231 }
2232
2233 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
2234 *timestamp = ptq->timestamp;
2235 return 0;
2236 }
2237 }
2238 return 0;
2239}
2240
2241static inline int intel_pt_update_queues(struct intel_pt *pt)
2242{
2243 if (pt->queues.new_data) {
2244 pt->queues.new_data = false;
2245 return intel_pt_setup_queues(pt);
2246 }
2247 return 0;
2248}
2249
2250static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
2251{
2252 unsigned int queue_nr;
2253 u64 ts;
2254 int ret;
2255
2256 while (1) {
2257 struct auxtrace_queue *queue;
2258 struct intel_pt_queue *ptq;
2259
2260 if (!pt->heap.heap_cnt)
2261 return 0;
2262
2263 if (pt->heap.heap_array[0].ordinal >= timestamp)
2264 return 0;
2265
2266 queue_nr = pt->heap.heap_array[0].queue_nr;
2267 queue = &pt->queues.queue_array[queue_nr];
2268 ptq = queue->priv;
2269
2270 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
2271 queue_nr, pt->heap.heap_array[0].ordinal,
2272 timestamp);
2273
2274 auxtrace_heap__pop(&pt->heap);
2275
2276 if (pt->heap.heap_cnt) {
2277 ts = pt->heap.heap_array[0].ordinal + 1;
2278 if (ts > timestamp)
2279 ts = timestamp;
2280 } else {
2281 ts = timestamp;
2282 }
2283
2284 intel_pt_set_pid_tid_cpu(pt, queue);
2285
2286 ret = intel_pt_run_decoder(ptq, &ts);
2287
2288 if (ret < 0) {
2289 auxtrace_heap__add(&pt->heap, queue_nr, ts);
2290 return ret;
2291 }
2292
2293 if (!ret) {
2294 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
2295 if (ret < 0)
2296 return ret;
2297 } else {
2298 ptq->on_heap = false;
2299 }
2300 }
2301
2302 return 0;
2303}
2304
2305static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
2306 u64 time_)
2307{
2308 struct auxtrace_queues *queues = &pt->queues;
2309 unsigned int i;
2310 u64 ts = 0;
2311
2312 for (i = 0; i < queues->nr_queues; i++) {
2313 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2314 struct intel_pt_queue *ptq = queue->priv;
2315
2316 if (ptq && (tid == -1 || ptq->tid == tid)) {
2317 ptq->time = time_;
2318 intel_pt_set_pid_tid_cpu(pt, queue);
2319 intel_pt_run_decoder(ptq, &ts);
2320 }
2321 }
2322 return 0;
2323}
2324
2325static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
2326{
2327 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
2328 sample->pid, sample->tid, 0, sample->time);
2329}
2330
2331static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
2332{
2333 unsigned i, j;
2334
2335 if (cpu < 0 || !pt->queues.nr_queues)
2336 return NULL;
2337
2338 if ((unsigned)cpu >= pt->queues.nr_queues)
2339 i = pt->queues.nr_queues - 1;
2340 else
2341 i = cpu;
2342
2343 if (pt->queues.queue_array[i].cpu == cpu)
2344 return pt->queues.queue_array[i].priv;
2345
2346 for (j = 0; i > 0; j++) {
2347 if (pt->queues.queue_array[--i].cpu == cpu)
2348 return pt->queues.queue_array[i].priv;
2349 }
2350
2351 for (; j < pt->queues.nr_queues; j++) {
2352 if (pt->queues.queue_array[j].cpu == cpu)
2353 return pt->queues.queue_array[j].priv;
2354 }
2355
2356 return NULL;
2357}
2358
2359static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
2360 u64 timestamp)
2361{
2362 struct intel_pt_queue *ptq;
2363 int err;
2364
2365 if (!pt->sync_switch)
2366 return 1;
2367
2368 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2369 if (!ptq || !ptq->sync_switch)
2370 return 1;
2371
2372 switch (ptq->switch_state) {
2373 case INTEL_PT_SS_NOT_TRACING:
2374 break;
2375 case INTEL_PT_SS_UNKNOWN:
2376 case INTEL_PT_SS_TRACING:
2377 ptq->next_tid = tid;
2378 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
2379 return 0;
2380 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2381 if (!ptq->on_heap) {
2382 ptq->timestamp = perf_time_to_tsc(timestamp,
2383 &pt->tc);
2384 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
2385 ptq->timestamp);
2386 if (err)
2387 return err;
2388 ptq->on_heap = true;
2389 }
2390 ptq->switch_state = INTEL_PT_SS_TRACING;
2391 break;
2392 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2393 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
2394 break;
2395 default:
2396 break;
2397 }
2398
2399 ptq->next_tid = -1;
2400
2401 return 1;
2402}
2403
2404static int intel_pt_process_switch(struct intel_pt *pt,
2405 struct perf_sample *sample)
2406{
2407 struct evsel *evsel;
2408 pid_t tid;
2409 int cpu, ret;
2410
2411 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
2412 if (evsel != pt->switch_evsel)
2413 return 0;
2414
2415 tid = perf_evsel__intval(evsel, sample, "next_pid");
2416 cpu = sample->cpu;
2417
2418 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2419 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
2420 &pt->tc));
2421
2422 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2423 if (ret <= 0)
2424 return ret;
2425
2426 return machine__set_current_tid(pt->machine, cpu, -1, tid);
2427}
2428
2429static int intel_pt_context_switch_in(struct intel_pt *pt,
2430 struct perf_sample *sample)
2431{
2432 pid_t pid = sample->pid;
2433 pid_t tid = sample->tid;
2434 int cpu = sample->cpu;
2435
2436 if (pt->sync_switch) {
2437 struct intel_pt_queue *ptq;
2438
2439 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2440 if (ptq && ptq->sync_switch) {
2441 ptq->next_tid = -1;
2442 switch (ptq->switch_state) {
2443 case INTEL_PT_SS_NOT_TRACING:
2444 case INTEL_PT_SS_UNKNOWN:
2445 case INTEL_PT_SS_TRACING:
2446 break;
2447 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2448 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2449 ptq->switch_state = INTEL_PT_SS_TRACING;
2450 break;
2451 default:
2452 break;
2453 }
2454 }
2455 }
2456
2457 /*
2458 * If the current tid has not been updated yet, ensure it is now that
2459 * a "switch in" event has occurred.
2460 */
2461 if (machine__get_current_tid(pt->machine, cpu) == tid)
2462 return 0;
2463
2464 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2465}
2466
2467static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
2468 struct perf_sample *sample)
2469{
2470 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2471 pid_t pid, tid;
2472 int cpu, ret;
2473
2474 cpu = sample->cpu;
2475
2476 if (pt->have_sched_switch == 3) {
2477 if (!out)
2478 return intel_pt_context_switch_in(pt, sample);
2479 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
2480 pr_err("Expecting CPU-wide context switch event\n");
2481 return -EINVAL;
2482 }
2483 pid = event->context_switch.next_prev_pid;
2484 tid = event->context_switch.next_prev_tid;
2485 } else {
2486 if (out)
2487 return 0;
2488 pid = sample->pid;
2489 tid = sample->tid;
2490 }
2491
2492 if (tid == -1)
2493 intel_pt_log("context_switch event has no tid\n");
2494
2495 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2496 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
2497 &pt->tc));
2498
2499 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2500 if (ret <= 0)
2501 return ret;
2502
2503 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2504}
2505
2506static int intel_pt_process_itrace_start(struct intel_pt *pt,
2507 union perf_event *event,
2508 struct perf_sample *sample)
2509{
2510 if (!pt->per_cpu_mmaps)
2511 return 0;
2512
2513 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2514 sample->cpu, event->itrace_start.pid,
2515 event->itrace_start.tid, sample->time,
2516 perf_time_to_tsc(sample->time, &pt->tc));
2517
2518 return machine__set_current_tid(pt->machine, sample->cpu,
2519 event->itrace_start.pid,
2520 event->itrace_start.tid);
2521}
2522
2523static int intel_pt_process_event(struct perf_session *session,
2524 union perf_event *event,
2525 struct perf_sample *sample,
2526 struct perf_tool *tool)
2527{
2528 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2529 auxtrace);
2530 u64 timestamp;
2531 int err = 0;
2532
2533 if (dump_trace)
2534 return 0;
2535
2536 if (!tool->ordered_events) {
2537 pr_err("Intel Processor Trace requires ordered events\n");
2538 return -EINVAL;
2539 }
2540
2541 if (sample->time && sample->time != (u64)-1)
2542 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
2543 else
2544 timestamp = 0;
2545
2546 if (timestamp || pt->timeless_decoding) {
2547 err = intel_pt_update_queues(pt);
2548 if (err)
2549 return err;
2550 }
2551
2552 if (pt->timeless_decoding) {
2553 if (event->header.type == PERF_RECORD_EXIT) {
2554 err = intel_pt_process_timeless_queues(pt,
2555 event->fork.tid,
2556 sample->time);
2557 }
2558 } else if (timestamp) {
2559 err = intel_pt_process_queues(pt, timestamp);
2560 }
2561 if (err)
2562 return err;
2563
2564 if (event->header.type == PERF_RECORD_AUX &&
2565 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
2566 pt->synth_opts.errors) {
2567 err = intel_pt_lost(pt, sample);
2568 if (err)
2569 return err;
2570 }
2571
2572 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
2573 err = intel_pt_process_switch(pt, sample);
2574 else if (event->header.type == PERF_RECORD_ITRACE_START)
2575 err = intel_pt_process_itrace_start(pt, event, sample);
2576 else if (event->header.type == PERF_RECORD_SWITCH ||
2577 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2578 err = intel_pt_context_switch(pt, event, sample);
2579
2580 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
2581 event->header.type, sample->cpu, sample->time, timestamp);
2582 intel_pt_log_event(event);
2583
2584 return err;
2585}
2586
2587static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
2588{
2589 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2590 auxtrace);
2591 int ret;
2592
2593 if (dump_trace)
2594 return 0;
2595
2596 if (!tool->ordered_events)
2597 return -EINVAL;
2598
2599 ret = intel_pt_update_queues(pt);
2600 if (ret < 0)
2601 return ret;
2602
2603 if (pt->timeless_decoding)
2604 return intel_pt_process_timeless_queues(pt, -1,
2605 MAX_TIMESTAMP - 1);
2606
2607 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
2608}
2609
2610static void intel_pt_free_events(struct perf_session *session)
2611{
2612 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2613 auxtrace);
2614 struct auxtrace_queues *queues = &pt->queues;
2615 unsigned int i;
2616
2617 for (i = 0; i < queues->nr_queues; i++) {
2618 intel_pt_free_queue(queues->queue_array[i].priv);
2619 queues->queue_array[i].priv = NULL;
2620 }
2621 intel_pt_log_disable();
2622 auxtrace_queues__free(queues);
2623}
2624
2625static void intel_pt_free(struct perf_session *session)
2626{
2627 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2628 auxtrace);
2629
2630 auxtrace_heap__free(&pt->heap);
2631 intel_pt_free_events(session);
2632 session->auxtrace = NULL;
2633 thread__put(pt->unknown_thread);
2634 addr_filters__exit(&pt->filts);
2635 zfree(&pt->filter);
2636 zfree(&pt->time_ranges);
2637 free(pt);
2638}
2639
2640static int intel_pt_process_auxtrace_event(struct perf_session *session,
2641 union perf_event *event,
2642 struct perf_tool *tool __maybe_unused)
2643{
2644 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2645 auxtrace);
2646
2647 if (!pt->data_queued) {
2648 struct auxtrace_buffer *buffer;
2649 off_t data_offset;
2650 int fd = perf_data__fd(session->data);
2651 int err;
2652
2653 if (perf_data__is_pipe(session->data)) {
2654 data_offset = 0;
2655 } else {
2656 data_offset = lseek(fd, 0, SEEK_CUR);
2657 if (data_offset == -1)
2658 return -errno;
2659 }
2660
2661 err = auxtrace_queues__add_event(&pt->queues, session, event,
2662 data_offset, &buffer);
2663 if (err)
2664 return err;
2665
2666 /* Dump here now we have copied a piped trace out of the pipe */
2667 if (dump_trace) {
2668 if (auxtrace_buffer__get_data(buffer, fd)) {
2669 intel_pt_dump_event(pt, buffer->data,
2670 buffer->size);
2671 auxtrace_buffer__put_data(buffer);
2672 }
2673 }
2674 }
2675
2676 return 0;
2677}
2678
2679struct intel_pt_synth {
2680 struct perf_tool dummy_tool;
2681 struct perf_session *session;
2682};
2683
2684static int intel_pt_event_synth(struct perf_tool *tool,
2685 union perf_event *event,
2686 struct perf_sample *sample __maybe_unused,
2687 struct machine *machine __maybe_unused)
2688{
2689 struct intel_pt_synth *intel_pt_synth =
2690 container_of(tool, struct intel_pt_synth, dummy_tool);
2691
2692 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
2693 NULL);
2694}
2695
2696static int intel_pt_synth_event(struct perf_session *session, const char *name,
2697 struct perf_event_attr *attr, u64 id)
2698{
2699 struct intel_pt_synth intel_pt_synth;
2700 int err;
2701
2702 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2703 name, id, (u64)attr->sample_type);
2704
2705 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
2706 intel_pt_synth.session = session;
2707
2708 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
2709 &id, intel_pt_event_synth);
2710 if (err)
2711 pr_err("%s: failed to synthesize '%s' event type\n",
2712 __func__, name);
2713
2714 return err;
2715}
2716
2717static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
2718 const char *name)
2719{
2720 struct evsel *evsel;
2721
2722 evlist__for_each_entry(evlist, evsel) {
2723 if (evsel->core.id && evsel->core.id[0] == id) {
2724 if (evsel->name)
2725 zfree(&evsel->name);
2726 evsel->name = strdup(name);
2727 break;
2728 }
2729 }
2730}
2731
2732static struct evsel *intel_pt_evsel(struct intel_pt *pt,
2733 struct evlist *evlist)
2734{
2735 struct evsel *evsel;
2736
2737 evlist__for_each_entry(evlist, evsel) {
2738 if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids)
2739 return evsel;
2740 }
2741
2742 return NULL;
2743}
2744
2745static int intel_pt_synth_events(struct intel_pt *pt,
2746 struct perf_session *session)
2747{
2748 struct evlist *evlist = session->evlist;
2749 struct evsel *evsel = intel_pt_evsel(pt, evlist);
2750 struct perf_event_attr attr;
2751 u64 id;
2752 int err;
2753
2754 if (!evsel) {
2755 pr_debug("There are no selected events with Intel Processor Trace data\n");
2756 return 0;
2757 }
2758
2759 memset(&attr, 0, sizeof(struct perf_event_attr));
2760 attr.size = sizeof(struct perf_event_attr);
2761 attr.type = PERF_TYPE_HARDWARE;
2762 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
2763 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
2764 PERF_SAMPLE_PERIOD;
2765 if (pt->timeless_decoding)
2766 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
2767 else
2768 attr.sample_type |= PERF_SAMPLE_TIME;
2769 if (!pt->per_cpu_mmaps)
2770 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
2771 attr.exclude_user = evsel->core.attr.exclude_user;
2772 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
2773 attr.exclude_hv = evsel->core.attr.exclude_hv;
2774 attr.exclude_host = evsel->core.attr.exclude_host;
2775 attr.exclude_guest = evsel->core.attr.exclude_guest;
2776 attr.sample_id_all = evsel->core.attr.sample_id_all;
2777 attr.read_format = evsel->core.attr.read_format;
2778
2779 id = evsel->core.id[0] + 1000000000;
2780 if (!id)
2781 id = 1;
2782
2783 if (pt->synth_opts.branches) {
2784 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2785 attr.sample_period = 1;
2786 attr.sample_type |= PERF_SAMPLE_ADDR;
2787 err = intel_pt_synth_event(session, "branches", &attr, id);
2788 if (err)
2789 return err;
2790 pt->sample_branches = true;
2791 pt->branches_sample_type = attr.sample_type;
2792 pt->branches_id = id;
2793 id += 1;
2794 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
2795 }
2796
2797 if (pt->synth_opts.callchain)
2798 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2799 if (pt->synth_opts.last_branch)
2800 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2801
2802 if (pt->synth_opts.instructions) {
2803 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2804 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2805 attr.sample_period =
2806 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2807 else
2808 attr.sample_period = pt->synth_opts.period;
2809 err = intel_pt_synth_event(session, "instructions", &attr, id);
2810 if (err)
2811 return err;
2812 pt->sample_instructions = true;
2813 pt->instructions_sample_type = attr.sample_type;
2814 pt->instructions_id = id;
2815 id += 1;
2816 }
2817
2818 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
2819 attr.sample_period = 1;
2820
2821 if (pt->synth_opts.transactions) {
2822 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2823 err = intel_pt_synth_event(session, "transactions", &attr, id);
2824 if (err)
2825 return err;
2826 pt->sample_transactions = true;
2827 pt->transactions_sample_type = attr.sample_type;
2828 pt->transactions_id = id;
2829 intel_pt_set_event_name(evlist, id, "transactions");
2830 id += 1;
2831 }
2832
2833 attr.type = PERF_TYPE_SYNTH;
2834 attr.sample_type |= PERF_SAMPLE_RAW;
2835
2836 if (pt->synth_opts.ptwrites) {
2837 attr.config = PERF_SYNTH_INTEL_PTWRITE;
2838 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
2839 if (err)
2840 return err;
2841 pt->sample_ptwrites = true;
2842 pt->ptwrites_sample_type = attr.sample_type;
2843 pt->ptwrites_id = id;
2844 intel_pt_set_event_name(evlist, id, "ptwrite");
2845 id += 1;
2846 }
2847
2848 if (pt->synth_opts.pwr_events) {
2849 pt->sample_pwr_events = true;
2850 pt->pwr_events_sample_type = attr.sample_type;
2851
2852 attr.config = PERF_SYNTH_INTEL_CBR;
2853 err = intel_pt_synth_event(session, "cbr", &attr, id);
2854 if (err)
2855 return err;
2856 pt->cbr_id = id;
2857 intel_pt_set_event_name(evlist, id, "cbr");
2858 id += 1;
2859 }
2860
2861 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & 0x10)) {
2862 attr.config = PERF_SYNTH_INTEL_MWAIT;
2863 err = intel_pt_synth_event(session, "mwait", &attr, id);
2864 if (err)
2865 return err;
2866 pt->mwait_id = id;
2867 intel_pt_set_event_name(evlist, id, "mwait");
2868 id += 1;
2869
2870 attr.config = PERF_SYNTH_INTEL_PWRE;
2871 err = intel_pt_synth_event(session, "pwre", &attr, id);
2872 if (err)
2873 return err;
2874 pt->pwre_id = id;
2875 intel_pt_set_event_name(evlist, id, "pwre");
2876 id += 1;
2877
2878 attr.config = PERF_SYNTH_INTEL_EXSTOP;
2879 err = intel_pt_synth_event(session, "exstop", &attr, id);
2880 if (err)
2881 return err;
2882 pt->exstop_id = id;
2883 intel_pt_set_event_name(evlist, id, "exstop");
2884 id += 1;
2885
2886 attr.config = PERF_SYNTH_INTEL_PWRX;
2887 err = intel_pt_synth_event(session, "pwrx", &attr, id);
2888 if (err)
2889 return err;
2890 pt->pwrx_id = id;
2891 intel_pt_set_event_name(evlist, id, "pwrx");
2892 id += 1;
2893 }
2894
2895 return 0;
2896}
2897
2898static void intel_pt_setup_pebs_events(struct intel_pt *pt)
2899{
2900 struct evsel *evsel;
2901
2902 if (!pt->synth_opts.other_events)
2903 return;
2904
2905 evlist__for_each_entry(pt->session->evlist, evsel) {
2906 if (evsel->core.attr.aux_output && evsel->core.id) {
2907 pt->sample_pebs = true;
2908 pt->pebs_evsel = evsel;
2909 return;
2910 }
2911 }
2912}
2913
2914static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
2915{
2916 struct evsel *evsel;
2917
2918 evlist__for_each_entry_reverse(evlist, evsel) {
2919 const char *name = perf_evsel__name(evsel);
2920
2921 if (!strcmp(name, "sched:sched_switch"))
2922 return evsel;
2923 }
2924
2925 return NULL;
2926}
2927
2928static bool intel_pt_find_switch(struct evlist *evlist)
2929{
2930 struct evsel *evsel;
2931
2932 evlist__for_each_entry(evlist, evsel) {
2933 if (evsel->core.attr.context_switch)
2934 return true;
2935 }
2936
2937 return false;
2938}
2939
2940static int intel_pt_perf_config(const char *var, const char *value, void *data)
2941{
2942 struct intel_pt *pt = data;
2943
2944 if (!strcmp(var, "intel-pt.mispred-all"))
2945 pt->mispred_all = perf_config_bool(var, value);
2946
2947 return 0;
2948}
2949
2950/* Find least TSC which converts to ns or later */
2951static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
2952{
2953 u64 tsc, tm;
2954
2955 tsc = perf_time_to_tsc(ns, &pt->tc);
2956
2957 while (1) {
2958 tm = tsc_to_perf_time(tsc, &pt->tc);
2959 if (tm < ns)
2960 break;
2961 tsc -= 1;
2962 }
2963
2964 while (tm < ns)
2965 tm = tsc_to_perf_time(++tsc, &pt->tc);
2966
2967 return tsc;
2968}
2969
2970/* Find greatest TSC which converts to ns or earlier */
2971static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
2972{
2973 u64 tsc, tm;
2974
2975 tsc = perf_time_to_tsc(ns, &pt->tc);
2976
2977 while (1) {
2978 tm = tsc_to_perf_time(tsc, &pt->tc);
2979 if (tm > ns)
2980 break;
2981 tsc += 1;
2982 }
2983
2984 while (tm > ns)
2985 tm = tsc_to_perf_time(--tsc, &pt->tc);
2986
2987 return tsc;
2988}
2989
2990static int intel_pt_setup_time_ranges(struct intel_pt *pt,
2991 struct itrace_synth_opts *opts)
2992{
2993 struct perf_time_interval *p = opts->ptime_range;
2994 int n = opts->range_num;
2995 int i;
2996
2997 if (!n || !p || pt->timeless_decoding)
2998 return 0;
2999
3000 pt->time_ranges = calloc(n, sizeof(struct range));
3001 if (!pt->time_ranges)
3002 return -ENOMEM;
3003
3004 pt->range_cnt = n;
3005
3006 intel_pt_log("%s: %u range(s)\n", __func__, n);
3007
3008 for (i = 0; i < n; i++) {
3009 struct range *r = &pt->time_ranges[i];
3010 u64 ts = p[i].start;
3011 u64 te = p[i].end;
3012
3013 /*
3014 * Take care to ensure the TSC range matches the perf-time range
3015 * when converted back to perf-time.
3016 */
3017 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
3018 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
3019
3020 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
3021 i, ts, te);
3022 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
3023 i, r->start, r->end);
3024 }
3025
3026 return 0;
3027}
3028
3029static const char * const intel_pt_info_fmts[] = {
3030 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
3031 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
3032 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
3033 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
3034 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
3035 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
3036 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
3037 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
3038 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
3039 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
3040 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
3041 [INTEL_PT_MTC_FREQ_BITS] = " MTC freq bits %#"PRIx64"\n",
3042 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
3043 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
3044 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
3045 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
3046 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
3047};
3048
3049static void intel_pt_print_info(__u64 *arr, int start, int finish)
3050{
3051 int i;
3052
3053 if (!dump_trace)
3054 return;
3055
3056 for (i = start; i <= finish; i++) {
3057 const char *fmt = intel_pt_info_fmts[i];
3058
3059 if (fmt)
3060 fprintf(stdout, fmt, arr[i]);
3061 }
3062}
3063
3064static void intel_pt_print_info_str(const char *name, const char *str)
3065{
3066 if (!dump_trace)
3067 return;
3068
3069 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
3070}
3071
3072static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
3073{
3074 return auxtrace_info->header.size >=
3075 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
3076}
3077
3078int intel_pt_process_auxtrace_info(union perf_event *event,
3079 struct perf_session *session)
3080{
3081 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3082 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
3083 struct intel_pt *pt;
3084 void *info_end;
3085 __u64 *info;
3086 int err;
3087
3088 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
3089 min_sz)
3090 return -EINVAL;
3091
3092 pt = zalloc(sizeof(struct intel_pt));
3093 if (!pt)
3094 return -ENOMEM;
3095
3096 addr_filters__init(&pt->filts);
3097
3098 err = perf_config(intel_pt_perf_config, pt);
3099 if (err)
3100 goto err_free;
3101
3102 err = auxtrace_queues__init(&pt->queues);
3103 if (err)
3104 goto err_free;
3105
3106 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
3107
3108 pt->session = session;
3109 pt->machine = &session->machines.host; /* No kvm support */
3110 pt->auxtrace_type = auxtrace_info->type;
3111 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
3112 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
3113 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
3114 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
3115 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
3116 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
3117 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
3118 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
3119 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
3120 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
3121 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
3122 INTEL_PT_PER_CPU_MMAPS);
3123
3124 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
3125 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
3126 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
3127 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
3128 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
3129 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
3130 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
3131 INTEL_PT_CYC_BIT);
3132 }
3133
3134 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
3135 pt->max_non_turbo_ratio =
3136 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
3137 intel_pt_print_info(&auxtrace_info->priv[0],
3138 INTEL_PT_MAX_NONTURBO_RATIO,
3139 INTEL_PT_MAX_NONTURBO_RATIO);
3140 }
3141
3142 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
3143 info_end = (void *)info + auxtrace_info->header.size;
3144
3145 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
3146 size_t len;
3147
3148 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
3149 intel_pt_print_info(&auxtrace_info->priv[0],
3150 INTEL_PT_FILTER_STR_LEN,
3151 INTEL_PT_FILTER_STR_LEN);
3152 if (len) {
3153 const char *filter = (const char *)info;
3154
3155 len = roundup(len + 1, 8);
3156 info += len >> 3;
3157 if ((void *)info > info_end) {
3158 pr_err("%s: bad filter string length\n", __func__);
3159 err = -EINVAL;
3160 goto err_free_queues;
3161 }
3162 pt->filter = memdup(filter, len);
3163 if (!pt->filter) {
3164 err = -ENOMEM;
3165 goto err_free_queues;
3166 }
3167 if (session->header.needs_swap)
3168 mem_bswap_64(pt->filter, len);
3169 if (pt->filter[len - 1]) {
3170 pr_err("%s: filter string not null terminated\n", __func__);
3171 err = -EINVAL;
3172 goto err_free_queues;
3173 }
3174 err = addr_filters__parse_bare_filter(&pt->filts,
3175 filter);
3176 if (err)
3177 goto err_free_queues;
3178 }
3179 intel_pt_print_info_str("Filter string", pt->filter);
3180 }
3181
3182 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
3183 if (pt->timeless_decoding && !pt->tc.time_mult)
3184 pt->tc.time_mult = 1;
3185 pt->have_tsc = intel_pt_have_tsc(pt);
3186 pt->sampling_mode = false;
3187 pt->est_tsc = !pt->timeless_decoding;
3188
3189 pt->unknown_thread = thread__new(999999999, 999999999);
3190 if (!pt->unknown_thread) {
3191 err = -ENOMEM;
3192 goto err_free_queues;
3193 }
3194
3195 /*
3196 * Since this thread will not be kept in any rbtree not in a
3197 * list, initialize its list node so that at thread__put() the
3198 * current thread lifetime assuption is kept and we don't segfault
3199 * at list_del_init().
3200 */
3201 INIT_LIST_HEAD(&pt->unknown_thread->node);
3202
3203 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
3204 if (err)
3205 goto err_delete_thread;
3206 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
3207 err = -ENOMEM;
3208 goto err_delete_thread;
3209 }
3210
3211 pt->auxtrace.process_event = intel_pt_process_event;
3212 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
3213 pt->auxtrace.flush_events = intel_pt_flush;
3214 pt->auxtrace.free_events = intel_pt_free_events;
3215 pt->auxtrace.free = intel_pt_free;
3216 session->auxtrace = &pt->auxtrace;
3217
3218 if (dump_trace)
3219 return 0;
3220
3221 if (pt->have_sched_switch == 1) {
3222 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
3223 if (!pt->switch_evsel) {
3224 pr_err("%s: missing sched_switch event\n", __func__);
3225 err = -EINVAL;
3226 goto err_delete_thread;
3227 }
3228 } else if (pt->have_sched_switch == 2 &&
3229 !intel_pt_find_switch(session->evlist)) {
3230 pr_err("%s: missing context_switch attribute flag\n", __func__);
3231 err = -EINVAL;
3232 goto err_delete_thread;
3233 }
3234
3235 if (session->itrace_synth_opts->set) {
3236 pt->synth_opts = *session->itrace_synth_opts;
3237 } else {
3238 itrace_synth_opts__set_default(&pt->synth_opts,
3239 session->itrace_synth_opts->default_no_sample);
3240 if (!session->itrace_synth_opts->default_no_sample &&
3241 !session->itrace_synth_opts->inject) {
3242 pt->synth_opts.branches = false;
3243 pt->synth_opts.callchain = true;
3244 }
3245 pt->synth_opts.thread_stack =
3246 session->itrace_synth_opts->thread_stack;
3247 }
3248
3249 if (pt->synth_opts.log)
3250 intel_pt_log_enable();
3251
3252 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
3253 if (pt->tc.time_mult) {
3254 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
3255
3256 if (!pt->max_non_turbo_ratio)
3257 pt->max_non_turbo_ratio =
3258 (tsc_freq + 50000000) / 100000000;
3259 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
3260 intel_pt_log("Maximum non-turbo ratio %u\n",
3261 pt->max_non_turbo_ratio);
3262 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
3263 }
3264
3265 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
3266 if (err)
3267 goto err_delete_thread;
3268
3269 if (pt->synth_opts.calls)
3270 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
3271 PERF_IP_FLAG_TRACE_END;
3272 if (pt->synth_opts.returns)
3273 pt->branches_filter |= PERF_IP_FLAG_RETURN |
3274 PERF_IP_FLAG_TRACE_BEGIN;
3275
3276 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
3277 symbol_conf.use_callchain = true;
3278 if (callchain_register_param(&callchain_param) < 0) {
3279 symbol_conf.use_callchain = false;
3280 pt->synth_opts.callchain = false;
3281 }
3282 }
3283
3284 err = intel_pt_synth_events(pt, session);
3285 if (err)
3286 goto err_delete_thread;
3287
3288 intel_pt_setup_pebs_events(pt);
3289
3290 err = auxtrace_queues__process_index(&pt->queues, session);
3291 if (err)
3292 goto err_delete_thread;
3293
3294 if (pt->queues.populated)
3295 pt->data_queued = true;
3296
3297 if (pt->timeless_decoding)
3298 pr_debug2("Intel PT decoding without timestamps\n");
3299
3300 return 0;
3301
3302err_delete_thread:
3303 thread__zput(pt->unknown_thread);
3304err_free_queues:
3305 intel_pt_log_disable();
3306 auxtrace_queues__free(&pt->queues);
3307 session->auxtrace = NULL;
3308err_free:
3309 addr_filters__exit(&pt->filts);
3310 zfree(&pt->filter);
3311 zfree(&pt->time_ranges);
3312 free(pt);
3313 return err;
3314}