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