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