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