blob: e768c02ef2ab92531c0ec19ecdd48b54c5526e45 [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 <errno.h>
8#include <stdbool.h>
9#include <linux/kernel.h>
10#include <linux/types.h>
11#include <linux/bitops.h>
12#include <linux/log2.h>
13#include <linux/zalloc.h>
14#include <cpuid.h>
15
16#include "../../util/session.h"
17#include "../../util/event.h"
18#include "../../util/evlist.h"
19#include "../../util/evsel.h"
20#include "../../util/cpumap.h"
21#include "../../util/mmap.h"
22#include <subcmd/parse-options.h>
23#include "../../util/parse-events.h"
24#include "../../util/pmu.h"
25#include "../../util/debug.h"
26#include "../../util/auxtrace.h"
27#include "../../util/record.h"
28#include "../../util/target.h"
29#include "../../util/tsc.h"
30#include <internal/lib.h> // page_size
31#include "../../util/intel-pt.h"
32
33#define KiB(x) ((x) * 1024)
34#define MiB(x) ((x) * 1024 * 1024)
35#define KiB_MASK(x) (KiB(x) - 1)
36#define MiB_MASK(x) (MiB(x) - 1)
37
38#define INTEL_PT_PSB_PERIOD_NEAR 256
39
40struct intel_pt_snapshot_ref {
41 void *ref_buf;
42 size_t ref_offset;
43 bool wrapped;
44};
45
46struct intel_pt_recording {
47 struct auxtrace_record itr;
48 struct perf_pmu *intel_pt_pmu;
49 int have_sched_switch;
50 struct evlist *evlist;
51 bool snapshot_mode;
52 bool snapshot_init_done;
53 size_t snapshot_size;
54 size_t snapshot_ref_buf_size;
55 int snapshot_ref_cnt;
56 struct intel_pt_snapshot_ref *snapshot_refs;
57 size_t priv_size;
58};
59
60static int intel_pt_parse_terms_with_default(struct list_head *formats,
61 const char *str,
62 u64 *config)
63{
64 struct list_head *terms;
65 struct perf_event_attr attr = { .size = 0, };
66 int err;
67
68 terms = malloc(sizeof(struct list_head));
69 if (!terms)
70 return -ENOMEM;
71
72 INIT_LIST_HEAD(terms);
73
74 err = parse_events_terms(terms, str);
75 if (err)
76 goto out_free;
77
78 attr.config = *config;
79 err = perf_pmu__config_terms(formats, &attr, terms, true, NULL);
80 if (err)
81 goto out_free;
82
83 *config = attr.config;
84out_free:
85 parse_events_terms__delete(terms);
86 return err;
87}
88
89static int intel_pt_parse_terms(struct list_head *formats, const char *str,
90 u64 *config)
91{
92 *config = 0;
93 return intel_pt_parse_terms_with_default(formats, str, config);
94}
95
96static u64 intel_pt_masked_bits(u64 mask, u64 bits)
97{
98 const u64 top_bit = 1ULL << 63;
99 u64 res = 0;
100 int i;
101
102 for (i = 0; i < 64; i++) {
103 if (mask & top_bit) {
104 res <<= 1;
105 if (bits & top_bit)
106 res |= 1;
107 }
108 mask <<= 1;
109 bits <<= 1;
110 }
111
112 return res;
113}
114
115static int intel_pt_read_config(struct perf_pmu *intel_pt_pmu, const char *str,
116 struct evlist *evlist, u64 *res)
117{
118 struct evsel *evsel;
119 u64 mask;
120
121 *res = 0;
122
123 mask = perf_pmu__format_bits(&intel_pt_pmu->format, str);
124 if (!mask)
125 return -EINVAL;
126
127 evlist__for_each_entry(evlist, evsel) {
128 if (evsel->core.attr.type == intel_pt_pmu->type) {
129 *res = intel_pt_masked_bits(mask, evsel->core.attr.config);
130 return 0;
131 }
132 }
133
134 return -EINVAL;
135}
136
137static size_t intel_pt_psb_period(struct perf_pmu *intel_pt_pmu,
138 struct evlist *evlist)
139{
140 u64 val;
141 int err, topa_multiple_entries;
142 size_t psb_period;
143
144 if (perf_pmu__scan_file(intel_pt_pmu, "caps/topa_multiple_entries",
145 "%d", &topa_multiple_entries) != 1)
146 topa_multiple_entries = 0;
147
148 /*
149 * Use caps/topa_multiple_entries to indicate early hardware that had
150 * extra frequent PSBs.
151 */
152 if (!topa_multiple_entries) {
153 psb_period = 256;
154 goto out;
155 }
156
157 err = intel_pt_read_config(intel_pt_pmu, "psb_period", evlist, &val);
158 if (err)
159 val = 0;
160
161 psb_period = 1 << (val + 11);
162out:
163 pr_debug2("%s psb_period %zu\n", intel_pt_pmu->name, psb_period);
164 return psb_period;
165}
166
167static int intel_pt_pick_bit(int bits, int target)
168{
169 int pos, pick = -1;
170
171 for (pos = 0; bits; bits >>= 1, pos++) {
172 if (bits & 1) {
173 if (pos <= target || pick < 0)
174 pick = pos;
175 if (pos >= target)
176 break;
177 }
178 }
179
180 return pick;
181}
182
183static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu)
184{
185 char buf[256];
186 int mtc, mtc_periods = 0, mtc_period;
187 int psb_cyc, psb_periods, psb_period;
188 int pos = 0;
189 u64 config;
190 char c;
191
192 pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc");
193
194 if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc", "%d",
195 &mtc) != 1)
196 mtc = 1;
197
198 if (mtc) {
199 if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc_periods", "%x",
200 &mtc_periods) != 1)
201 mtc_periods = 0;
202 if (mtc_periods) {
203 mtc_period = intel_pt_pick_bit(mtc_periods, 3);
204 pos += scnprintf(buf + pos, sizeof(buf) - pos,
205 ",mtc,mtc_period=%d", mtc_period);
206 }
207 }
208
209 if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d",
210 &psb_cyc) != 1)
211 psb_cyc = 1;
212
213 if (psb_cyc && mtc_periods) {
214 if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x",
215 &psb_periods) != 1)
216 psb_periods = 0;
217 if (psb_periods) {
218 psb_period = intel_pt_pick_bit(psb_periods, 3);
219 pos += scnprintf(buf + pos, sizeof(buf) - pos,
220 ",psb_period=%d", psb_period);
221 }
222 }
223
224 if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 &&
225 perf_pmu__scan_file(intel_pt_pmu, "format/branch", "%c", &c) == 1)
226 pos += scnprintf(buf + pos, sizeof(buf) - pos, ",pt,branch");
227
228 pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf);
229
230 intel_pt_parse_terms(&intel_pt_pmu->format, buf, &config);
231
232 return config;
233}
234
235static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
236 struct record_opts *opts,
237 const char *str)
238{
239 struct intel_pt_recording *ptr =
240 container_of(itr, struct intel_pt_recording, itr);
241 unsigned long long snapshot_size = 0;
242 char *endptr;
243
244 if (str) {
245 snapshot_size = strtoull(str, &endptr, 0);
246 if (*endptr || snapshot_size > SIZE_MAX)
247 return -1;
248 }
249
250 opts->auxtrace_snapshot_mode = true;
251 opts->auxtrace_snapshot_size = snapshot_size;
252
253 ptr->snapshot_size = snapshot_size;
254
255 return 0;
256}
257
258struct perf_event_attr *
259intel_pt_pmu_default_config(struct perf_pmu *intel_pt_pmu)
260{
261 struct perf_event_attr *attr;
262
263 attr = zalloc(sizeof(struct perf_event_attr));
264 if (!attr)
265 return NULL;
266
267 attr->config = intel_pt_default_config(intel_pt_pmu);
268
269 intel_pt_pmu->selectable = true;
270
271 return attr;
272}
273
274static const char *intel_pt_find_filter(struct evlist *evlist,
275 struct perf_pmu *intel_pt_pmu)
276{
277 struct evsel *evsel;
278
279 evlist__for_each_entry(evlist, evsel) {
280 if (evsel->core.attr.type == intel_pt_pmu->type)
281 return evsel->filter;
282 }
283
284 return NULL;
285}
286
287static size_t intel_pt_filter_bytes(const char *filter)
288{
289 size_t len = filter ? strlen(filter) : 0;
290
291 return len ? roundup(len + 1, 8) : 0;
292}
293
294static size_t
295intel_pt_info_priv_size(struct auxtrace_record *itr, struct evlist *evlist)
296{
297 struct intel_pt_recording *ptr =
298 container_of(itr, struct intel_pt_recording, itr);
299 const char *filter = intel_pt_find_filter(evlist, ptr->intel_pt_pmu);
300
301 ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX * sizeof(u64)) +
302 intel_pt_filter_bytes(filter);
303
304 return ptr->priv_size;
305}
306
307static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
308{
309 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
310
311 __get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
312 *n = ebx;
313 *d = eax;
314}
315
316static int intel_pt_info_fill(struct auxtrace_record *itr,
317 struct perf_session *session,
318 struct perf_record_auxtrace_info *auxtrace_info,
319 size_t priv_size)
320{
321 struct intel_pt_recording *ptr =
322 container_of(itr, struct intel_pt_recording, itr);
323 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
324 struct perf_event_mmap_page *pc;
325 struct perf_tsc_conversion tc = { .time_mult = 0, };
326 bool cap_user_time_zero = false, per_cpu_mmaps;
327 u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
328 u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
329 unsigned long max_non_turbo_ratio;
330 size_t filter_str_len;
331 const char *filter;
332 __u64 *info;
333 int err;
334
335 if (priv_size != ptr->priv_size)
336 return -EINVAL;
337
338 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
339 intel_pt_parse_terms(&intel_pt_pmu->format, "noretcomp",
340 &noretcomp_bit);
341 intel_pt_parse_terms(&intel_pt_pmu->format, "mtc", &mtc_bit);
342 mtc_freq_bits = perf_pmu__format_bits(&intel_pt_pmu->format,
343 "mtc_period");
344 intel_pt_parse_terms(&intel_pt_pmu->format, "cyc", &cyc_bit);
345
346 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
347
348 if (perf_pmu__scan_file(intel_pt_pmu, "max_nonturbo_ratio",
349 "%lu", &max_non_turbo_ratio) != 1)
350 max_non_turbo_ratio = 0;
351
352 filter = intel_pt_find_filter(session->evlist, ptr->intel_pt_pmu);
353 filter_str_len = filter ? strlen(filter) : 0;
354
355 if (!session->evlist->core.nr_mmaps)
356 return -EINVAL;
357
358 pc = session->evlist->mmap[0].core.base;
359 if (pc) {
360 err = perf_read_tsc_conversion(pc, &tc);
361 if (err) {
362 if (err != -EOPNOTSUPP)
363 return err;
364 } else {
365 cap_user_time_zero = tc.time_mult != 0;
366 }
367 if (!cap_user_time_zero)
368 ui__warning("Intel Processor Trace: TSC not available\n");
369 }
370
371 per_cpu_mmaps = !perf_cpu_map__empty(session->evlist->core.cpus);
372
373 auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
374 auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
375 auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
376 auxtrace_info->priv[INTEL_PT_TIME_MULT] = tc.time_mult;
377 auxtrace_info->priv[INTEL_PT_TIME_ZERO] = tc.time_zero;
378 auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO] = cap_user_time_zero;
379 auxtrace_info->priv[INTEL_PT_TSC_BIT] = tsc_bit;
380 auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT] = noretcomp_bit;
381 auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH] = ptr->have_sched_switch;
382 auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE] = ptr->snapshot_mode;
383 auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS] = per_cpu_mmaps;
384 auxtrace_info->priv[INTEL_PT_MTC_BIT] = mtc_bit;
385 auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS] = mtc_freq_bits;
386 auxtrace_info->priv[INTEL_PT_TSC_CTC_N] = tsc_ctc_ratio_n;
387 auxtrace_info->priv[INTEL_PT_TSC_CTC_D] = tsc_ctc_ratio_d;
388 auxtrace_info->priv[INTEL_PT_CYC_BIT] = cyc_bit;
389 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO] = max_non_turbo_ratio;
390 auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] = filter_str_len;
391
392 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
393
394 if (filter_str_len) {
395 size_t len = intel_pt_filter_bytes(filter);
396
397 strncpy((char *)info, filter, len);
398 info += len >> 3;
399 }
400
401 return 0;
402}
403
404static int intel_pt_track_switches(struct evlist *evlist)
405{
406 const char *sched_switch = "sched:sched_switch";
407 struct evsel *evsel;
408 int err;
409
410 if (!perf_evlist__can_select_event(evlist, sched_switch))
411 return -EPERM;
412
413 err = parse_events(evlist, sched_switch, NULL);
414 if (err) {
415 pr_debug2("%s: failed to parse %s, error %d\n",
416 __func__, sched_switch, err);
417 return err;
418 }
419
420 evsel = evlist__last(evlist);
421
422 perf_evsel__set_sample_bit(evsel, CPU);
423 perf_evsel__set_sample_bit(evsel, TIME);
424
425 evsel->core.system_wide = true;
426 evsel->no_aux_samples = true;
427 evsel->immediate = true;
428
429 return 0;
430}
431
432static void intel_pt_valid_str(char *str, size_t len, u64 valid)
433{
434 unsigned int val, last = 0, state = 1;
435 int p = 0;
436
437 str[0] = '\0';
438
439 for (val = 0; val <= 64; val++, valid >>= 1) {
440 if (valid & 1) {
441 last = val;
442 switch (state) {
443 case 0:
444 p += scnprintf(str + p, len - p, ",");
445 /* Fall through */
446 case 1:
447 p += scnprintf(str + p, len - p, "%u", val);
448 state = 2;
449 break;
450 case 2:
451 state = 3;
452 break;
453 case 3:
454 state = 4;
455 break;
456 default:
457 break;
458 }
459 } else {
460 switch (state) {
461 case 3:
462 p += scnprintf(str + p, len - p, ",%u", last);
463 state = 0;
464 break;
465 case 4:
466 p += scnprintf(str + p, len - p, "-%u", last);
467 state = 0;
468 break;
469 default:
470 break;
471 }
472 if (state != 1)
473 state = 0;
474 }
475 }
476}
477
478static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu,
479 const char *caps, const char *name,
480 const char *supported, u64 config)
481{
482 char valid_str[256];
483 unsigned int shift;
484 unsigned long long valid;
485 u64 bits;
486 int ok;
487
488 if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1)
489 valid = 0;
490
491 if (supported &&
492 perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok)
493 valid = 0;
494
495 valid |= 1;
496
497 bits = perf_pmu__format_bits(&intel_pt_pmu->format, name);
498
499 config &= bits;
500
501 for (shift = 0; bits && !(bits & 1); shift++)
502 bits >>= 1;
503
504 config >>= shift;
505
506 if (config > 63)
507 goto out_err;
508
509 if (valid & (1 << config))
510 return 0;
511out_err:
512 intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
513 pr_err("Invalid %s for %s. Valid values are: %s\n",
514 name, INTEL_PT_PMU_NAME, valid_str);
515 return -EINVAL;
516}
517
518static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
519 struct evsel *evsel)
520{
521 int err;
522 char c;
523
524 if (!evsel)
525 return 0;
526
527 /*
528 * If supported, force pass-through config term (pt=1) even if user
529 * sets pt=0, which avoids senseless kernel errors.
530 */
531 if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 &&
532 !(evsel->core.attr.config & 1)) {
533 pr_warning("pt=0 doesn't make sense, forcing pt=1\n");
534 evsel->core.attr.config |= 1;
535 }
536
537 err = intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds",
538 "cyc_thresh", "caps/psb_cyc",
539 evsel->core.attr.config);
540 if (err)
541 return err;
542
543 err = intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods",
544 "mtc_period", "caps/mtc",
545 evsel->core.attr.config);
546 if (err)
547 return err;
548
549 return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods",
550 "psb_period", "caps/psb_cyc",
551 evsel->core.attr.config);
552}
553
554/*
555 * Currently, there is not enough information to disambiguate different PEBS
556 * events, so only allow one.
557 */
558static bool intel_pt_too_many_aux_output(struct evlist *evlist)
559{
560 struct evsel *evsel;
561 int aux_output_cnt = 0;
562
563 evlist__for_each_entry(evlist, evsel)
564 aux_output_cnt += !!evsel->core.attr.aux_output;
565
566 if (aux_output_cnt > 1) {
567 pr_err(INTEL_PT_PMU_NAME " supports at most one event with aux-output\n");
568 return true;
569 }
570
571 return false;
572}
573
574static int intel_pt_recording_options(struct auxtrace_record *itr,
575 struct evlist *evlist,
576 struct record_opts *opts)
577{
578 struct intel_pt_recording *ptr =
579 container_of(itr, struct intel_pt_recording, itr);
580 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
581 bool have_timing_info, need_immediate = false;
582 struct evsel *evsel, *intel_pt_evsel = NULL;
583 const struct perf_cpu_map *cpus = evlist->core.cpus;
584 bool privileged = perf_event_paranoid_check(-1);
585 u64 tsc_bit;
586 int err;
587
588 ptr->evlist = evlist;
589 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
590
591 evlist__for_each_entry(evlist, evsel) {
592 if (evsel->core.attr.type == intel_pt_pmu->type) {
593 if (intel_pt_evsel) {
594 pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
595 return -EINVAL;
596 }
597 evsel->core.attr.freq = 0;
598 evsel->core.attr.sample_period = 1;
599 evsel->no_aux_samples = true;
600 intel_pt_evsel = evsel;
601 opts->full_auxtrace = true;
602 }
603 }
604
605 if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
606 pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
607 return -EINVAL;
608 }
609
610 if (opts->use_clockid) {
611 pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
612 return -EINVAL;
613 }
614
615 if (intel_pt_too_many_aux_output(evlist))
616 return -EINVAL;
617
618 if (!opts->full_auxtrace)
619 return 0;
620
621 err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
622 if (err)
623 return err;
624
625 /* Set default sizes for snapshot mode */
626 if (opts->auxtrace_snapshot_mode) {
627 size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
628
629 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
630 if (privileged) {
631 opts->auxtrace_mmap_pages = MiB(4) / page_size;
632 } else {
633 opts->auxtrace_mmap_pages = KiB(128) / page_size;
634 if (opts->mmap_pages == UINT_MAX)
635 opts->mmap_pages = KiB(256) / page_size;
636 }
637 } else if (!opts->auxtrace_mmap_pages && !privileged &&
638 opts->mmap_pages == UINT_MAX) {
639 opts->mmap_pages = KiB(256) / page_size;
640 }
641 if (!opts->auxtrace_snapshot_size)
642 opts->auxtrace_snapshot_size =
643 opts->auxtrace_mmap_pages * (size_t)page_size;
644 if (!opts->auxtrace_mmap_pages) {
645 size_t sz = opts->auxtrace_snapshot_size;
646
647 sz = round_up(sz, page_size) / page_size;
648 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
649 }
650 if (opts->auxtrace_snapshot_size >
651 opts->auxtrace_mmap_pages * (size_t)page_size) {
652 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
653 opts->auxtrace_snapshot_size,
654 opts->auxtrace_mmap_pages * (size_t)page_size);
655 return -EINVAL;
656 }
657 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
658 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
659 return -EINVAL;
660 }
661 pr_debug2("Intel PT snapshot size: %zu\n",
662 opts->auxtrace_snapshot_size);
663 if (psb_period &&
664 opts->auxtrace_snapshot_size <= psb_period +
665 INTEL_PT_PSB_PERIOD_NEAR)
666 ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
667 opts->auxtrace_snapshot_size, psb_period);
668 }
669
670 /* Set default sizes for full trace mode */
671 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
672 if (privileged) {
673 opts->auxtrace_mmap_pages = MiB(4) / page_size;
674 } else {
675 opts->auxtrace_mmap_pages = KiB(128) / page_size;
676 if (opts->mmap_pages == UINT_MAX)
677 opts->mmap_pages = KiB(256) / page_size;
678 }
679 }
680
681 /* Validate auxtrace_mmap_pages */
682 if (opts->auxtrace_mmap_pages) {
683 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
684 size_t min_sz;
685
686 if (opts->auxtrace_snapshot_mode)
687 min_sz = KiB(4);
688 else
689 min_sz = KiB(8);
690
691 if (sz < min_sz || !is_power_of_2(sz)) {
692 pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
693 min_sz / 1024);
694 return -EINVAL;
695 }
696 }
697
698 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
699
700 if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit))
701 have_timing_info = true;
702 else
703 have_timing_info = false;
704
705 /*
706 * Per-cpu recording needs sched_switch events to distinguish different
707 * threads.
708 */
709 if (have_timing_info && !perf_cpu_map__empty(cpus)) {
710 if (perf_can_record_switch_events()) {
711 bool cpu_wide = !target__none(&opts->target) &&
712 !target__has_task(&opts->target);
713
714 if (!cpu_wide && perf_can_record_cpu_wide()) {
715 struct evsel *switch_evsel;
716
717 err = parse_events(evlist, "dummy:u", NULL);
718 if (err)
719 return err;
720
721 switch_evsel = evlist__last(evlist);
722
723 switch_evsel->core.attr.freq = 0;
724 switch_evsel->core.attr.sample_period = 1;
725 switch_evsel->core.attr.context_switch = 1;
726
727 switch_evsel->core.system_wide = true;
728 switch_evsel->no_aux_samples = true;
729 switch_evsel->immediate = true;
730
731 perf_evsel__set_sample_bit(switch_evsel, TID);
732 perf_evsel__set_sample_bit(switch_evsel, TIME);
733 perf_evsel__set_sample_bit(switch_evsel, CPU);
734 perf_evsel__reset_sample_bit(switch_evsel, BRANCH_STACK);
735
736 opts->record_switch_events = false;
737 ptr->have_sched_switch = 3;
738 } else {
739 opts->record_switch_events = true;
740 need_immediate = true;
741 if (cpu_wide)
742 ptr->have_sched_switch = 3;
743 else
744 ptr->have_sched_switch = 2;
745 }
746 } else {
747 err = intel_pt_track_switches(evlist);
748 if (err == -EPERM)
749 pr_debug2("Unable to select sched:sched_switch\n");
750 else if (err)
751 return err;
752 else
753 ptr->have_sched_switch = 1;
754 }
755 }
756
757 if (intel_pt_evsel) {
758 /*
759 * To obtain the auxtrace buffer file descriptor, the auxtrace
760 * event must come first.
761 */
762 perf_evlist__to_front(evlist, intel_pt_evsel);
763 /*
764 * In the case of per-cpu mmaps, we need the CPU on the
765 * AUX event.
766 */
767 if (!perf_cpu_map__empty(cpus))
768 perf_evsel__set_sample_bit(intel_pt_evsel, CPU);
769 }
770
771 /* Add dummy event to keep tracking */
772 if (opts->full_auxtrace) {
773 struct evsel *tracking_evsel;
774
775 err = parse_events(evlist, "dummy:u", NULL);
776 if (err)
777 return err;
778
779 tracking_evsel = evlist__last(evlist);
780
781 perf_evlist__set_tracking_event(evlist, tracking_evsel);
782
783 tracking_evsel->core.attr.freq = 0;
784 tracking_evsel->core.attr.sample_period = 1;
785
786 tracking_evsel->no_aux_samples = true;
787 if (need_immediate)
788 tracking_evsel->immediate = true;
789
790 /* In per-cpu case, always need the time of mmap events etc */
791 if (!perf_cpu_map__empty(cpus)) {
792 perf_evsel__set_sample_bit(tracking_evsel, TIME);
793 /* And the CPU for switch events */
794 perf_evsel__set_sample_bit(tracking_evsel, CPU);
795 }
796 perf_evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
797 }
798
799 /*
800 * Warn the user when we do not have enough information to decode i.e.
801 * per-cpu with no sched_switch (except workload-only).
802 */
803 if (!ptr->have_sched_switch && !perf_cpu_map__empty(cpus) &&
804 !target__none(&opts->target))
805 ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
806
807 return 0;
808}
809
810static int intel_pt_snapshot_start(struct auxtrace_record *itr)
811{
812 struct intel_pt_recording *ptr =
813 container_of(itr, struct intel_pt_recording, itr);
814 struct evsel *evsel;
815
816 evlist__for_each_entry(ptr->evlist, evsel) {
817 if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
818 return evsel__disable(evsel);
819 }
820 return -EINVAL;
821}
822
823static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
824{
825 struct intel_pt_recording *ptr =
826 container_of(itr, struct intel_pt_recording, itr);
827 struct evsel *evsel;
828
829 evlist__for_each_entry(ptr->evlist, evsel) {
830 if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
831 return evsel__enable(evsel);
832 }
833 return -EINVAL;
834}
835
836static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
837{
838 const size_t sz = sizeof(struct intel_pt_snapshot_ref);
839 int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
840 struct intel_pt_snapshot_ref *refs;
841
842 if (!new_cnt)
843 new_cnt = 16;
844
845 while (new_cnt <= idx)
846 new_cnt *= 2;
847
848 refs = calloc(new_cnt, sz);
849 if (!refs)
850 return -ENOMEM;
851
852 memcpy(refs, ptr->snapshot_refs, cnt * sz);
853
854 ptr->snapshot_refs = refs;
855 ptr->snapshot_ref_cnt = new_cnt;
856
857 return 0;
858}
859
860static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
861{
862 int i;
863
864 for (i = 0; i < ptr->snapshot_ref_cnt; i++)
865 zfree(&ptr->snapshot_refs[i].ref_buf);
866 zfree(&ptr->snapshot_refs);
867}
868
869static void intel_pt_recording_free(struct auxtrace_record *itr)
870{
871 struct intel_pt_recording *ptr =
872 container_of(itr, struct intel_pt_recording, itr);
873
874 intel_pt_free_snapshot_refs(ptr);
875 free(ptr);
876}
877
878static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
879 size_t snapshot_buf_size)
880{
881 size_t ref_buf_size = ptr->snapshot_ref_buf_size;
882 void *ref_buf;
883
884 ref_buf = zalloc(ref_buf_size);
885 if (!ref_buf)
886 return -ENOMEM;
887
888 ptr->snapshot_refs[idx].ref_buf = ref_buf;
889 ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
890
891 return 0;
892}
893
894static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
895 size_t snapshot_buf_size)
896{
897 const size_t max_size = 256 * 1024;
898 size_t buf_size = 0, psb_period;
899
900 if (ptr->snapshot_size <= 64 * 1024)
901 return 0;
902
903 psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
904 if (psb_period)
905 buf_size = psb_period * 2;
906
907 if (!buf_size || buf_size > max_size)
908 buf_size = max_size;
909
910 if (buf_size >= snapshot_buf_size)
911 return 0;
912
913 if (buf_size >= ptr->snapshot_size / 2)
914 return 0;
915
916 return buf_size;
917}
918
919static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
920 size_t snapshot_buf_size)
921{
922 if (ptr->snapshot_init_done)
923 return 0;
924
925 ptr->snapshot_init_done = true;
926
927 ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
928 snapshot_buf_size);
929
930 return 0;
931}
932
933/**
934 * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
935 * @buf1: first buffer
936 * @compare_size: number of bytes to compare
937 * @buf2: second buffer (a circular buffer)
938 * @offs2: offset in second buffer
939 * @buf2_size: size of second buffer
940 *
941 * The comparison allows for the possibility that the bytes to compare in the
942 * circular buffer are not contiguous. It is assumed that @compare_size <=
943 * @buf2_size. This function returns %false if the bytes are identical, %true
944 * otherwise.
945 */
946static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
947 void *buf2, size_t offs2, size_t buf2_size)
948{
949 size_t end2 = offs2 + compare_size, part_size;
950
951 if (end2 <= buf2_size)
952 return memcmp(buf1, buf2 + offs2, compare_size);
953
954 part_size = end2 - buf2_size;
955 if (memcmp(buf1, buf2 + offs2, part_size))
956 return true;
957
958 compare_size -= part_size;
959
960 return memcmp(buf1 + part_size, buf2, compare_size);
961}
962
963static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
964 size_t ref_size, size_t buf_size,
965 void *data, size_t head)
966{
967 size_t ref_end = ref_offset + ref_size;
968
969 if (ref_end > buf_size) {
970 if (head > ref_offset || head < ref_end - buf_size)
971 return true;
972 } else if (head > ref_offset && head < ref_end) {
973 return true;
974 }
975
976 return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
977 buf_size);
978}
979
980static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
981 void *data, size_t head)
982{
983 if (head >= ref_size) {
984 memcpy(ref_buf, data + head - ref_size, ref_size);
985 } else {
986 memcpy(ref_buf, data, head);
987 ref_size -= head;
988 memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
989 }
990}
991
992static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
993 struct auxtrace_mmap *mm, unsigned char *data,
994 u64 head)
995{
996 struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
997 bool wrapped;
998
999 wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
1000 ptr->snapshot_ref_buf_size, mm->len,
1001 data, head);
1002
1003 intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
1004 data, head);
1005
1006 return wrapped;
1007}
1008
1009static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
1010{
1011 int i, a, b;
1012
1013 b = buf_size >> 3;
1014 a = b - 512;
1015 if (a < 0)
1016 a = 0;
1017
1018 for (i = a; i < b; i++) {
1019 if (data[i])
1020 return true;
1021 }
1022
1023 return false;
1024}
1025
1026static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
1027 struct auxtrace_mmap *mm, unsigned char *data,
1028 u64 *head, u64 *old)
1029{
1030 struct intel_pt_recording *ptr =
1031 container_of(itr, struct intel_pt_recording, itr);
1032 bool wrapped;
1033 int err;
1034
1035 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
1036 __func__, idx, (size_t)*old, (size_t)*head);
1037
1038 err = intel_pt_snapshot_init(ptr, mm->len);
1039 if (err)
1040 goto out_err;
1041
1042 if (idx >= ptr->snapshot_ref_cnt) {
1043 err = intel_pt_alloc_snapshot_refs(ptr, idx);
1044 if (err)
1045 goto out_err;
1046 }
1047
1048 if (ptr->snapshot_ref_buf_size) {
1049 if (!ptr->snapshot_refs[idx].ref_buf) {
1050 err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
1051 if (err)
1052 goto out_err;
1053 }
1054 wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
1055 } else {
1056 wrapped = ptr->snapshot_refs[idx].wrapped;
1057 if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
1058 ptr->snapshot_refs[idx].wrapped = true;
1059 wrapped = true;
1060 }
1061 }
1062
1063 /*
1064 * In full trace mode 'head' continually increases. However in snapshot
1065 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
1066 * are adjusted to match the full trace case which expects that 'old' is
1067 * always less than 'head'.
1068 */
1069 if (wrapped) {
1070 *old = *head;
1071 *head += mm->len;
1072 } else {
1073 if (mm->mask)
1074 *old &= mm->mask;
1075 else
1076 *old %= mm->len;
1077 if (*old > *head)
1078 *head += mm->len;
1079 }
1080
1081 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
1082 __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
1083
1084 return 0;
1085
1086out_err:
1087 pr_err("%s: failed, error %d\n", __func__, err);
1088 return err;
1089}
1090
1091static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
1092{
1093 return rdtsc();
1094}
1095
1096static int intel_pt_read_finish(struct auxtrace_record *itr, int idx)
1097{
1098 struct intel_pt_recording *ptr =
1099 container_of(itr, struct intel_pt_recording, itr);
1100 struct evsel *evsel;
1101
1102 evlist__for_each_entry(ptr->evlist, evsel) {
1103 if (evsel->core.attr.type == ptr->intel_pt_pmu->type) {
1104 if (evsel->disabled)
1105 return 0;
1106 return perf_evlist__enable_event_idx(ptr->evlist, evsel,
1107 idx);
1108 }
1109 }
1110 return -EINVAL;
1111}
1112
1113struct auxtrace_record *intel_pt_recording_init(int *err)
1114{
1115 struct perf_pmu *intel_pt_pmu = perf_pmu__find(INTEL_PT_PMU_NAME);
1116 struct intel_pt_recording *ptr;
1117
1118 if (!intel_pt_pmu)
1119 return NULL;
1120
1121 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
1122 *err = -errno;
1123 return NULL;
1124 }
1125
1126 ptr = zalloc(sizeof(struct intel_pt_recording));
1127 if (!ptr) {
1128 *err = -ENOMEM;
1129 return NULL;
1130 }
1131
1132 ptr->intel_pt_pmu = intel_pt_pmu;
1133 ptr->itr.recording_options = intel_pt_recording_options;
1134 ptr->itr.info_priv_size = intel_pt_info_priv_size;
1135 ptr->itr.info_fill = intel_pt_info_fill;
1136 ptr->itr.free = intel_pt_recording_free;
1137 ptr->itr.snapshot_start = intel_pt_snapshot_start;
1138 ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
1139 ptr->itr.find_snapshot = intel_pt_find_snapshot;
1140 ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
1141 ptr->itr.reference = intel_pt_reference;
1142 ptr->itr.read_finish = intel_pt_read_finish;
1143 return &ptr->itr;
1144}