intel-pt.c 83.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
A
Adrian Hunter 已提交
2 3 4 5 6
/*
 * intel_pt.c: Intel Processor Trace support
 * Copyright (c) 2013-2015, Intel Corporation.
 */

7
#include <inttypes.h>
A
Adrian Hunter 已提交
8 9 10 11
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <linux/kernel.h>
12
#include <linux/string.h>
A
Adrian Hunter 已提交
13
#include <linux/types.h>
14
#include <linux/zalloc.h>
A
Adrian Hunter 已提交
15 16 17

#include "session.h"
#include "machine.h"
18
#include "memswap.h"
19
#include "sort.h"
A
Adrian Hunter 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "tool.h"
#include "event.h"
#include "evlist.h"
#include "evsel.h"
#include "map.h"
#include "color.h"
#include "thread.h"
#include "thread-stack.h"
#include "symbol.h"
#include "callchain.h"
#include "dso.h"
#include "debug.h"
#include "auxtrace.h"
#include "tsc.h"
#include "intel-pt.h"
35
#include "config.h"
36
#include "util/perf_api_probe.h"
37
#include "util/synthetic-events.h"
38
#include "time-utils.h"
A
Adrian Hunter 已提交
39

40 41
#include "../arch/x86/include/uapi/asm/perf_regs.h"

A
Adrian Hunter 已提交
42 43 44 45 46 47 48
#include "intel-pt-decoder/intel-pt-log.h"
#include "intel-pt-decoder/intel-pt-decoder.h"
#include "intel-pt-decoder/intel-pt-insn-decoder.h"
#include "intel-pt-decoder/intel-pt-pkt-decoder.h"

#define MAX_TIMESTAMP (~0ULL)

49 50 51 52 53
struct range {
	u64 start;
	u64 end;
};

A
Adrian Hunter 已提交
54 55 56 57 58 59 60
struct intel_pt {
	struct auxtrace auxtrace;
	struct auxtrace_queues queues;
	struct auxtrace_heap heap;
	u32 auxtrace_type;
	struct perf_session *session;
	struct machine *machine;
61
	struct evsel *switch_evsel;
A
Adrian Hunter 已提交
62 63 64 65 66 67 68 69 70
	struct thread *unknown_thread;
	bool timeless_decoding;
	bool sampling_mode;
	bool snapshot_mode;
	bool per_cpu_mmaps;
	bool have_tsc;
	bool data_queued;
	bool est_tsc;
	bool sync_switch;
71
	bool mispred_all;
72
	bool use_thread_stack;
73 74
	bool callstack;
	unsigned int br_stack_sz;
A
Adrian Hunter 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	int have_sched_switch;
	u32 pmu_type;
	u64 kernel_start;
	u64 switch_ip;
	u64 ptss_ip;

	struct perf_tsc_conversion tc;
	bool cap_user_time_zero;

	struct itrace_synth_opts synth_opts;

	bool sample_instructions;
	u64 instructions_sample_type;
	u64 instructions_id;

	bool sample_branches;
	u32 branches_filter;
	u64 branches_sample_type;
	u64 branches_id;

	bool sample_transactions;
	u64 transactions_sample_type;
	u64 transactions_id;

99 100 101 102 103 104 105 106 107 108 109 110
	bool sample_ptwrites;
	u64 ptwrites_sample_type;
	u64 ptwrites_id;

	bool sample_pwr_events;
	u64 pwr_events_sample_type;
	u64 mwait_id;
	u64 pwre_id;
	u64 exstop_id;
	u64 pwrx_id;
	u64 cbr_id;

111
	bool sample_pebs;
112
	struct evsel *pebs_evsel;
113

A
Adrian Hunter 已提交
114
	u64 tsc_bit;
115 116 117 118 119
	u64 mtc_bit;
	u64 mtc_freq_bits;
	u32 tsc_ctc_ratio_n;
	u32 tsc_ctc_ratio_d;
	u64 cyc_bit;
A
Adrian Hunter 已提交
120 121
	u64 noretcomp_bit;
	unsigned max_non_turbo_ratio;
122
	unsigned cbr2khz;
123 124

	unsigned long num_events;
125 126

	char *filter;
127
	struct addr_filters filts;
128 129 130

	struct range *time_ranges;
	unsigned int range_cnt;
131 132

	struct ip_callchain *chain;
A
Adrian Hunter 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
};

enum switch_state {
	INTEL_PT_SS_NOT_TRACING,
	INTEL_PT_SS_UNKNOWN,
	INTEL_PT_SS_TRACING,
	INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
	INTEL_PT_SS_EXPECTING_SWITCH_IP,
};

struct intel_pt_queue {
	struct intel_pt *pt;
	unsigned int queue_nr;
	struct auxtrace_buffer *buffer;
147
	struct auxtrace_buffer *old_buffer;
A
Adrian Hunter 已提交
148 149 150
	void *decoder;
	const struct intel_pt_state *state;
	struct ip_callchain *chain;
151
	struct branch_stack *last_branch;
A
Adrian Hunter 已提交
152 153 154 155 156
	union perf_event *event_buf;
	bool on_heap;
	bool stop;
	bool step_through_buffers;
	bool use_buffer_pid_tid;
A
Adrian Hunter 已提交
157
	bool sync_switch;
A
Adrian Hunter 已提交
158 159 160 161 162 163 164 165 166
	pid_t pid, tid;
	int cpu;
	int switch_state;
	pid_t next_tid;
	struct thread *thread;
	bool exclude_kernel;
	bool have_sample;
	u64 time;
	u64 timestamp;
167 168 169
	u64 sel_timestamp;
	bool sel_start;
	unsigned int sel_idx;
A
Adrian Hunter 已提交
170 171
	u32 flags;
	u16 insn_len;
172
	u64 last_insn_cnt;
173 174 175 176 177 178
	u64 ipc_insn_cnt;
	u64 ipc_cyc_cnt;
	u64 last_in_insn_cnt;
	u64 last_in_cyc_cnt;
	u64 last_br_insn_cnt;
	u64 last_br_cyc_cnt;
179
	unsigned int cbr_seen;
180
	char insn[INTEL_PT_INSN_BUF_SZ];
A
Adrian Hunter 已提交
181 182 183 184 185 186 187 188 189 190
};

static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
			  unsigned char *buf, size_t len)
{
	struct intel_pt_pkt packet;
	size_t pos = 0;
	int ret, pkt_len, i;
	char desc[INTEL_PT_PKT_DESC_MAX];
	const char *color = PERF_COLOR_BLUE;
191
	enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
A
Adrian Hunter 已提交
192 193 194 195 196 197

	color_fprintf(stdout, color,
		      ". ... Intel Processor Trace data: size %zu bytes\n",
		      len);

	while (len) {
198
		ret = intel_pt_get_packet(buf, len, &packet, &ctx);
A
Adrian Hunter 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		if (ret > 0)
			pkt_len = ret;
		else
			pkt_len = 1;
		printf(".");
		color_fprintf(stdout, color, "  %08x: ", pos);
		for (i = 0; i < pkt_len; i++)
			color_fprintf(stdout, color, " %02x", buf[i]);
		for (; i < 16; i++)
			color_fprintf(stdout, color, "   ");
		if (ret > 0) {
			ret = intel_pt_pkt_desc(&packet, desc,
						INTEL_PT_PKT_DESC_MAX);
			if (ret > 0)
				color_fprintf(stdout, color, " %s\n", desc);
		} else {
			color_fprintf(stdout, color, " Bad packet!\n");
		}
		pos += pkt_len;
		buf += pkt_len;
		len -= pkt_len;
	}
}

static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
				size_t len)
{
	printf(".\n");
	intel_pt_dump(pt, buf, len);
}

230 231 232 233 234 235 236 237 238 239
static void intel_pt_log_event(union perf_event *event)
{
	FILE *f = intel_pt_log_fp();

	if (!intel_pt_enable_logging || !f)
		return;

	perf_event__fprintf(event, f);
}

240 241 242 243 244 245 246 247 248 249
static void intel_pt_dump_sample(struct perf_session *session,
				 struct perf_sample *sample)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);

	printf("\n");
	intel_pt_dump(pt, sample->aux_sample.data, sample->aux_sample.size);
}

A
Adrian Hunter 已提交
250 251 252
static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
				   struct auxtrace_buffer *b)
{
253
	bool consecutive = false;
A
Adrian Hunter 已提交
254 255 256
	void *start;

	start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
257
				      pt->have_tsc, &consecutive);
A
Adrian Hunter 已提交
258 259 260 261
	if (!start)
		return -EINVAL;
	b->use_size = b->data + b->size - start;
	b->use_data = start;
262 263
	if (b->use_size && consecutive)
		b->consecutive = true;
A
Adrian Hunter 已提交
264 265 266
	return 0;
}

267 268 269 270
static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
			       struct auxtrace_buffer *buffer,
			       struct auxtrace_buffer *old_buffer,
			       struct intel_pt_buffer *b)
A
Adrian Hunter 已提交
271
{
272
	bool might_overlap;
A
Adrian Hunter 已提交
273 274

	if (!buffer->data) {
275
		int fd = perf_data__fd(ptq->pt->session->data);
A
Adrian Hunter 已提交
276 277 278 279 280 281

		buffer->data = auxtrace_buffer__get_data(buffer, fd);
		if (!buffer->data)
			return -ENOMEM;
	}

282 283
	might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
	if (might_overlap && !buffer->consecutive && old_buffer &&
A
Adrian Hunter 已提交
284 285 286 287 288 289 290 291 292 293 294 295
	    intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
		return -ENOMEM;

	if (buffer->use_data) {
		b->len = buffer->use_size;
		b->buf = buffer->use_data;
	} else {
		b->len = buffer->size;
		b->buf = buffer->data;
	}
	b->ref_timestamp = buffer->reference;

296
	if (!old_buffer || (might_overlap && !buffer->consecutive)) {
A
Adrian Hunter 已提交
297 298 299 300 301 302
		b->consecutive = false;
		b->trace_nr = buffer->buffer_nr + 1;
	} else {
		b->consecutive = true;
	}

303 304 305
	return 0;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/* Do not drop buffers with references - refer intel_pt_get_trace() */
static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
					   struct auxtrace_buffer *buffer)
{
	if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
		return;

	auxtrace_buffer__drop_data(buffer);
}

/* Must be serialized with respect to intel_pt_get_trace() */
static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
			      void *cb_data)
{
	struct intel_pt_queue *ptq = data;
	struct auxtrace_buffer *buffer = ptq->buffer;
	struct auxtrace_buffer *old_buffer = ptq->old_buffer;
	struct auxtrace_queue *queue;
	int err = 0;

	queue = &ptq->pt->queues.queue_array[ptq->queue_nr];

	while (1) {
		struct intel_pt_buffer b = { .len = 0 };

		buffer = auxtrace_buffer__next(queue, buffer);
		if (!buffer)
			break;

		err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
		if (err)
			break;

		if (b.len) {
			intel_pt_lookahead_drop_buffer(ptq, old_buffer);
			old_buffer = buffer;
		} else {
			intel_pt_lookahead_drop_buffer(ptq, buffer);
			continue;
		}

		err = cb(&b, cb_data);
		if (err)
			break;
	}

	if (buffer != old_buffer)
		intel_pt_lookahead_drop_buffer(ptq, buffer);
	intel_pt_lookahead_drop_buffer(ptq, old_buffer);

	return err;
}

/*
 * This function assumes data is processed sequentially only.
 * Must be serialized with respect to intel_pt_lookahead()
 */
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
{
	struct intel_pt_queue *ptq = data;
	struct auxtrace_buffer *buffer = ptq->buffer;
	struct auxtrace_buffer *old_buffer = ptq->old_buffer;
	struct auxtrace_queue *queue;
	int err;

	if (ptq->stop) {
		b->len = 0;
		return 0;
	}

	queue = &ptq->pt->queues.queue_array[ptq->queue_nr];

	buffer = auxtrace_buffer__next(queue, buffer);
	if (!buffer) {
		if (old_buffer)
			auxtrace_buffer__drop_data(old_buffer);
		b->len = 0;
		return 0;
	}

	ptq->buffer = buffer;

	err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
	if (err)
		return err;

A
Adrian Hunter 已提交
392 393 394
	if (ptq->step_through_buffers)
		ptq->stop = true;

395 396 397 398 399 400
	if (b->len) {
		if (old_buffer)
			auxtrace_buffer__drop_data(old_buffer);
		ptq->old_buffer = buffer;
	} else {
		auxtrace_buffer__drop_data(buffer);
A
Adrian Hunter 已提交
401
		return intel_pt_get_trace(b, data);
402
	}
A
Adrian Hunter 已提交
403 404 405 406 407 408 409 410 411 412 413 414

	return 0;
}

struct intel_pt_cache_entry {
	struct auxtrace_cache_entry	entry;
	u64				insn_cnt;
	u64				byte_cnt;
	enum intel_pt_insn_op		op;
	enum intel_pt_insn_branch	branch;
	int				length;
	int32_t				rel;
415
	char				insn[INTEL_PT_INSN_BUF_SZ];
A
Adrian Hunter 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
};

static int intel_pt_config_div(const char *var, const char *value, void *data)
{
	int *d = data;
	long val;

	if (!strcmp(var, "intel-pt.cache-divisor")) {
		val = strtol(value, NULL, 0);
		if (val > 0 && val <= INT_MAX)
			*d = val;
	}

	return 0;
}

static int intel_pt_cache_divisor(void)
{
	static int d;

	if (d)
		return d;

	perf_config(intel_pt_config_div, &d);

	if (!d)
		d = 64;

	return d;
}

static unsigned int intel_pt_cache_size(struct dso *dso,
					struct machine *machine)
{
	off_t size;

	size = dso__data_size(dso, machine);
	size /= intel_pt_cache_divisor();
	if (size < 1000)
		return 10;
	if (size > (1 << 21))
		return 21;
	return 32 - __builtin_clz(size);
}

static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
					     struct machine *machine)
{
	struct auxtrace_cache *c;
	unsigned int bits;

	if (dso->auxtrace_cache)
		return dso->auxtrace_cache;

	bits = intel_pt_cache_size(dso, machine);

	/* Ignoring cache creation failure */
	c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);

	dso->auxtrace_cache = c;

	return c;
}

static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
			      u64 offset, u64 insn_cnt, u64 byte_cnt,
			      struct intel_pt_insn *intel_pt_insn)
{
	struct auxtrace_cache *c = intel_pt_cache(dso, machine);
	struct intel_pt_cache_entry *e;
	int err;

	if (!c)
		return -ENOMEM;

	e = auxtrace_cache__alloc_entry(c);
	if (!e)
		return -ENOMEM;

	e->insn_cnt = insn_cnt;
	e->byte_cnt = byte_cnt;
	e->op = intel_pt_insn->op;
	e->branch = intel_pt_insn->branch;
	e->length = intel_pt_insn->length;
	e->rel = intel_pt_insn->rel;
501
	memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
A
Adrian Hunter 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

	err = auxtrace_cache__add(c, offset, &e->entry);
	if (err)
		auxtrace_cache__free_entry(c, e);

	return err;
}

static struct intel_pt_cache_entry *
intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
{
	struct auxtrace_cache *c = intel_pt_cache(dso, machine);

	if (!c)
		return NULL;

	return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
}

521 522 523 524 525 526 527
static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip)
{
	return ip >= pt->kernel_start ?
	       PERF_RECORD_MISC_KERNEL :
	       PERF_RECORD_MISC_USER;
}

A
Adrian Hunter 已提交
528 529 530 531 532 533 534 535 536
static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
				   uint64_t *insn_cnt_ptr, uint64_t *ip,
				   uint64_t to_ip, uint64_t max_insn_cnt,
				   void *data)
{
	struct intel_pt_queue *ptq = data;
	struct machine *machine = ptq->pt->machine;
	struct thread *thread;
	struct addr_location al;
537
	unsigned char buf[INTEL_PT_INSN_BUF_SZ];
A
Adrian Hunter 已提交
538 539 540 541 542 543 544
	ssize_t len;
	int x86_64;
	u8 cpumode;
	u64 offset, start_offset, start_ip;
	u64 insn_cnt = 0;
	bool one_map = true;

545 546
	intel_pt_insn->length = 0;

A
Adrian Hunter 已提交
547 548 549
	if (to_ip && *ip == to_ip)
		goto out_no_cache;

550
	cpumode = intel_pt_cpumode(ptq->pt, *ip);
A
Adrian Hunter 已提交
551 552 553 554 555 556 557 558 559

	thread = ptq->thread;
	if (!thread) {
		if (cpumode != PERF_RECORD_MISC_KERNEL)
			return -EINVAL;
		thread = ptq->pt->unknown_thread;
	}

	while (1) {
560
		if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
A
Adrian Hunter 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
			return -EINVAL;

		if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
		    dso__data_status_seen(al.map->dso,
					  DSO_DATA_STATUS_SEEN_ITRACE))
			return -ENOENT;

		offset = al.map->map_ip(al.map, *ip);

		if (!to_ip && one_map) {
			struct intel_pt_cache_entry *e;

			e = intel_pt_cache_lookup(al.map->dso, machine, offset);
			if (e &&
			    (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
				*insn_cnt_ptr = e->insn_cnt;
				*ip += e->byte_cnt;
				intel_pt_insn->op = e->op;
				intel_pt_insn->branch = e->branch;
				intel_pt_insn->length = e->length;
				intel_pt_insn->rel = e->rel;
582 583
				memcpy(intel_pt_insn->buf, e->insn,
				       INTEL_PT_INSN_BUF_SZ);
A
Adrian Hunter 已提交
584 585 586 587 588 589 590 591 592
				intel_pt_log_insn_no_data(intel_pt_insn, *ip);
				return 0;
			}
		}

		start_offset = offset;
		start_ip = *ip;

		/* Load maps to ensure dso->is_64_bit has been updated */
593
		map__load(al.map);
A
Adrian Hunter 已提交
594 595 596 597 598

		x86_64 = al.map->dso->is_64_bit;

		while (1) {
			len = dso__data_read_offset(al.map->dso, machine,
599 600
						    offset, buf,
						    INTEL_PT_INSN_BUF_SZ);
A
Adrian Hunter 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
			if (len <= 0)
				return -EINVAL;

			if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
				return -EINVAL;

			intel_pt_log_insn(intel_pt_insn, *ip);

			insn_cnt += 1;

			if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
				goto out;

			if (max_insn_cnt && insn_cnt >= max_insn_cnt)
				goto out_no_cache;

			*ip += intel_pt_insn->length;

			if (to_ip && *ip == to_ip)
				goto out_no_cache;

			if (*ip >= al.map->end)
				break;

			offset += intel_pt_insn->length;
		}
		one_map = false;
	}
out:
	*insn_cnt_ptr = insn_cnt;

	if (!one_map)
		goto out_no_cache;

	/*
	 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
	 * entries.
	 */
	if (to_ip) {
		struct intel_pt_cache_entry *e;

		e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
		if (e)
			return 0;
	}

	/* Ignore cache errors */
	intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
			   *ip - start_ip, intel_pt_insn);

	return 0;

out_no_cache:
	*insn_cnt_ptr = insn_cnt;
	return 0;
}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
				  uint64_t offset, const char *filename)
{
	struct addr_filter *filt;
	bool have_filter   = false;
	bool hit_tracestop = false;
	bool hit_filter    = false;

	list_for_each_entry(filt, &pt->filts.head, list) {
		if (filt->start)
			have_filter = true;

		if ((filename && !filt->filename) ||
		    (!filename && filt->filename) ||
		    (filename && strcmp(filename, filt->filename)))
			continue;

		if (!(offset >= filt->addr && offset < filt->addr + filt->size))
			continue;

		intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
			     ip, offset, filename ? filename : "[kernel]",
			     filt->start ? "filter" : "stop",
			     filt->addr, filt->size);

		if (filt->start)
			hit_filter = true;
		else
			hit_tracestop = true;
	}

	if (!hit_tracestop && !hit_filter)
		intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
			     ip, offset, filename ? filename : "[kernel]");

	return hit_tracestop || (have_filter && !hit_filter);
}

static int __intel_pt_pgd_ip(uint64_t ip, void *data)
{
	struct intel_pt_queue *ptq = data;
	struct thread *thread;
	struct addr_location al;
	u8 cpumode;
	u64 offset;

	if (ip >= ptq->pt->kernel_start)
		return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);

	cpumode = PERF_RECORD_MISC_USER;

	thread = ptq->thread;
	if (!thread)
		return -EINVAL;

713
	if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
714 715 716 717 718 719 720 721 722 723 724 725 726
		return -EINVAL;

	offset = al.map->map_ip(al.map, ip);

	return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
				     al.map->dso->long_name);
}

static bool intel_pt_pgd_ip(uint64_t ip, void *data)
{
	return __intel_pt_pgd_ip(ip, data) > 0;
}

A
Adrian Hunter 已提交
727 728 729 730 731 732 733 734 735 736 737 738 739 740
static bool intel_pt_get_config(struct intel_pt *pt,
				struct perf_event_attr *attr, u64 *config)
{
	if (attr->type == pt->pmu_type) {
		if (config)
			*config = attr->config;
		return true;
	}

	return false;
}

static bool intel_pt_exclude_kernel(struct intel_pt *pt)
{
741
	struct evsel *evsel;
A
Adrian Hunter 已提交
742

743
	evlist__for_each_entry(pt->session->evlist, evsel) {
744 745
		if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
		    !evsel->core.attr.exclude_kernel)
A
Adrian Hunter 已提交
746 747 748 749 750 751 752
			return false;
	}
	return true;
}

static bool intel_pt_return_compression(struct intel_pt *pt)
{
753
	struct evsel *evsel;
A
Adrian Hunter 已提交
754 755 756 757 758
	u64 config;

	if (!pt->noretcomp_bit)
		return true;

759
	evlist__for_each_entry(pt->session->evlist, evsel) {
760
		if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
A
Adrian Hunter 已提交
761 762 763 764 765 766
		    (config & pt->noretcomp_bit))
			return false;
	}
	return true;
}

767 768
static bool intel_pt_branch_enable(struct intel_pt *pt)
{
769
	struct evsel *evsel;
770 771 772
	u64 config;

	evlist__for_each_entry(pt->session->evlist, evsel) {
773
		if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
774 775 776 777 778 779
		    (config & 1) && !(config & 0x2000))
			return false;
	}
	return true;
}

780 781
static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
{
782
	struct evsel *evsel;
783 784 785 786 787 788 789 790 791
	unsigned int shift;
	u64 config;

	if (!pt->mtc_freq_bits)
		return 0;

	for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
		config >>= 1;

792
	evlist__for_each_entry(pt->session->evlist, evsel) {
793
		if (intel_pt_get_config(pt, &evsel->core.attr, &config))
794 795 796 797 798
			return (config & pt->mtc_freq_bits) >> shift;
	}
	return 0;
}

A
Adrian Hunter 已提交
799 800
static bool intel_pt_timeless_decoding(struct intel_pt *pt)
{
801
	struct evsel *evsel;
A
Adrian Hunter 已提交
802 803 804 805 806 807
	bool timeless_decoding = true;
	u64 config;

	if (!pt->tsc_bit || !pt->cap_user_time_zero)
		return true;

808
	evlist__for_each_entry(pt->session->evlist, evsel) {
809
		if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
A
Adrian Hunter 已提交
810
			return true;
811
		if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
A
Adrian Hunter 已提交
812 813 814 815 816 817 818 819 820 821 822
			if (config & pt->tsc_bit)
				timeless_decoding = false;
			else
				return true;
		}
	}
	return timeless_decoding;
}

static bool intel_pt_tracing_kernel(struct intel_pt *pt)
{
823
	struct evsel *evsel;
A
Adrian Hunter 已提交
824

825
	evlist__for_each_entry(pt->session->evlist, evsel) {
826 827
		if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
		    !evsel->core.attr.exclude_kernel)
A
Adrian Hunter 已提交
828 829 830 831 832 833 834
			return true;
	}
	return false;
}

static bool intel_pt_have_tsc(struct intel_pt *pt)
{
835
	struct evsel *evsel;
A
Adrian Hunter 已提交
836 837 838 839 840 841
	bool have_tsc = false;
	u64 config;

	if (!pt->tsc_bit)
		return false;

842
	evlist__for_each_entry(pt->session->evlist, evsel) {
843
		if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
A
Adrian Hunter 已提交
844 845 846 847 848 849 850 851 852
			if (config & pt->tsc_bit)
				have_tsc = true;
			else
				return false;
		}
	}
	return have_tsc;
}

853 854 855 856 857 858 859 860 861 862 863 864
static bool intel_pt_sampling_mode(struct intel_pt *pt)
{
	struct evsel *evsel;

	evlist__for_each_entry(pt->session->evlist, evsel) {
		if ((evsel->core.attr.sample_type & PERF_SAMPLE_AUX) &&
		    evsel->core.attr.aux_sample_size)
			return true;
	}
	return false;
}

A
Adrian Hunter 已提交
865 866 867 868 869 870 871 872 873 874
static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
{
	u64 quot, rem;

	quot = ns / pt->tc.time_mult;
	rem  = ns % pt->tc.time_mult;
	return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
		pt->tc.time_mult;
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt)
{
	size_t sz = sizeof(struct ip_callchain);

	/* Add 1 to callchain_sz for callchain context */
	sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
	return zalloc(sz);
}

static int intel_pt_callchain_init(struct intel_pt *pt)
{
	struct evsel *evsel;

	evlist__for_each_entry(pt->session->evlist, evsel) {
		if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN))
			evsel->synth_sample_type |= PERF_SAMPLE_CALLCHAIN;
	}

	pt->chain = intel_pt_alloc_chain(pt);
	if (!pt->chain)
		return -ENOMEM;

	return 0;
}

static void intel_pt_add_callchain(struct intel_pt *pt,
				   struct perf_sample *sample)
{
	struct thread *thread = machine__findnew_thread(pt->machine,
							sample->pid,
							sample->tid);

	thread_stack__sample_late(thread, sample->cpu, pt->chain,
				  pt->synth_opts.callchain_sz + 1, sample->ip,
				  pt->kernel_start);

	sample->callchain = pt->chain;
}

A
Adrian Hunter 已提交
914 915 916 917
static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
						   unsigned int queue_nr)
{
	struct intel_pt_params params = { .get_trace = 0, };
918
	struct perf_env *env = pt->machine->env;
A
Adrian Hunter 已提交
919 920 921 922 923 924 925
	struct intel_pt_queue *ptq;

	ptq = zalloc(sizeof(struct intel_pt_queue));
	if (!ptq)
		return NULL;

	if (pt->synth_opts.callchain) {
926
		ptq->chain = intel_pt_alloc_chain(pt);
A
Adrian Hunter 已提交
927 928 929 930
		if (!ptq->chain)
			goto out_free;
	}

931 932 933
	if (pt->synth_opts.last_branch) {
		size_t sz = sizeof(struct branch_stack);

934
		sz += pt->br_stack_sz * sizeof(struct branch_entry);
935 936 937 938 939
		ptq->last_branch = zalloc(sz);
		if (!ptq->last_branch)
			goto out_free;
	}

A
Adrian Hunter 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953
	ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
	if (!ptq->event_buf)
		goto out_free;

	ptq->pt = pt;
	ptq->queue_nr = queue_nr;
	ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
	ptq->pid = -1;
	ptq->tid = -1;
	ptq->cpu = -1;
	ptq->next_tid = -1;

	params.get_trace = intel_pt_get_trace;
	params.walk_insn = intel_pt_walk_next_insn;
954
	params.lookahead = intel_pt_lookahead;
A
Adrian Hunter 已提交
955 956
	params.data = ptq;
	params.return_compression = intel_pt_return_compression(pt);
957
	params.branch_enable = intel_pt_branch_enable(pt);
A
Adrian Hunter 已提交
958
	params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
959 960 961
	params.mtc_period = intel_pt_mtc_period(pt);
	params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
	params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
A
Adrian Hunter 已提交
962

963 964 965
	if (pt->filts.cnt > 0)
		params.pgd_ip = intel_pt_pgd_ip;

A
Adrian Hunter 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	if (pt->synth_opts.instructions) {
		if (pt->synth_opts.period) {
			switch (pt->synth_opts.period_type) {
			case PERF_ITRACE_PERIOD_INSTRUCTIONS:
				params.period_type =
						INTEL_PT_PERIOD_INSTRUCTIONS;
				params.period = pt->synth_opts.period;
				break;
			case PERF_ITRACE_PERIOD_TICKS:
				params.period_type = INTEL_PT_PERIOD_TICKS;
				params.period = pt->synth_opts.period;
				break;
			case PERF_ITRACE_PERIOD_NANOSECS:
				params.period_type = INTEL_PT_PERIOD_TICKS;
				params.period = intel_pt_ns_to_ticks(pt,
							pt->synth_opts.period);
				break;
			default:
				break;
			}
		}

		if (!params.period) {
			params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
990
			params.period = 1;
A
Adrian Hunter 已提交
991 992 993
		}
	}

994 995 996
	if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
		params.flags |= INTEL_PT_FUP_WITH_NLIP;

A
Adrian Hunter 已提交
997 998 999 1000 1001 1002 1003 1004
	ptq->decoder = intel_pt_decoder_new(&params);
	if (!ptq->decoder)
		goto out_free;

	return ptq;

out_free:
	zfree(&ptq->event_buf);
1005
	zfree(&ptq->last_branch);
A
Adrian Hunter 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	zfree(&ptq->chain);
	free(ptq);
	return NULL;
}

static void intel_pt_free_queue(void *priv)
{
	struct intel_pt_queue *ptq = priv;

	if (!ptq)
		return;
	thread__zput(ptq->thread);
	intel_pt_decoder_free(ptq->decoder);
	zfree(&ptq->event_buf);
1020
	zfree(&ptq->last_branch);
A
Adrian Hunter 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	zfree(&ptq->chain);
	free(ptq);
}

static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
				     struct auxtrace_queue *queue)
{
	struct intel_pt_queue *ptq = queue->priv;

	if (queue->tid == -1 || pt->have_sched_switch) {
		ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
		thread__zput(ptq->thread);
	}

	if (!ptq->thread && ptq->tid != -1)
		ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);

	if (ptq->thread) {
		ptq->pid = ptq->thread->pid_;
		if (queue->cpu == -1)
			ptq->cpu = ptq->thread->cpu;
	}
}

static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
{
	if (ptq->state->flags & INTEL_PT_ABORT_TX) {
		ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
	} else if (ptq->state->flags & INTEL_PT_ASYNC) {
		if (ptq->state->to_ip)
			ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
				     PERF_IP_FLAG_ASYNC |
				     PERF_IP_FLAG_INTERRUPT;
		else
			ptq->flags = PERF_IP_FLAG_BRANCH |
				     PERF_IP_FLAG_TRACE_END;
		ptq->insn_len = 0;
	} else {
		if (ptq->state->from_ip)
			ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
		else
			ptq->flags = PERF_IP_FLAG_BRANCH |
				     PERF_IP_FLAG_TRACE_BEGIN;
		if (ptq->state->flags & INTEL_PT_IN_TX)
			ptq->flags |= PERF_IP_FLAG_IN_TX;
		ptq->insn_len = ptq->state->insn_len;
1067
		memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
A
Adrian Hunter 已提交
1068
	}
1069 1070 1071 1072 1073

	if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
		ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
	if (ptq->state->type & INTEL_PT_TRACE_END)
		ptq->flags |= PERF_IP_FLAG_TRACE_END;
A
Adrian Hunter 已提交
1074 1075
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
static void intel_pt_setup_time_range(struct intel_pt *pt,
				      struct intel_pt_queue *ptq)
{
	if (!pt->range_cnt)
		return;

	ptq->sel_timestamp = pt->time_ranges[0].start;
	ptq->sel_idx = 0;

	if (ptq->sel_timestamp) {
		ptq->sel_start = true;
	} else {
		ptq->sel_timestamp = pt->time_ranges[0].end;
		ptq->sel_start = false;
	}
}

A
Adrian Hunter 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
static int intel_pt_setup_queue(struct intel_pt *pt,
				struct auxtrace_queue *queue,
				unsigned int queue_nr)
{
	struct intel_pt_queue *ptq = queue->priv;

	if (list_empty(&queue->head))
		return 0;

	if (!ptq) {
		ptq = intel_pt_alloc_queue(pt, queue_nr);
		if (!ptq)
			return -ENOMEM;
		queue->priv = ptq;

		if (queue->cpu != -1)
			ptq->cpu = queue->cpu;
		ptq->tid = queue->tid;

1112 1113
		ptq->cbr_seen = UINT_MAX;

1114 1115 1116
		if (pt->sampling_mode && !pt->snapshot_mode &&
		    pt->timeless_decoding)
			ptq->step_through_buffers = true;
A
Adrian Hunter 已提交
1117 1118

		ptq->sync_switch = pt->sync_switch;
1119 1120

		intel_pt_setup_time_range(pt, ptq);
A
Adrian Hunter 已提交
1121 1122 1123
	}

	if (!ptq->on_heap &&
A
Adrian Hunter 已提交
1124
	    (!ptq->sync_switch ||
A
Adrian Hunter 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	     ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
		const struct intel_pt_state *state;
		int ret;

		if (pt->timeless_decoding)
			return 0;

		intel_pt_log("queue %u getting timestamp\n", queue_nr);
		intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
			     queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1135 1136 1137 1138 1139 1140 1141 1142

		if (ptq->sel_start && ptq->sel_timestamp) {
			ret = intel_pt_fast_forward(ptq->decoder,
						    ptq->sel_timestamp);
			if (ret)
				return ret;
		}

A
Adrian Hunter 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
		while (1) {
			state = intel_pt_decode(ptq->decoder);
			if (state->err) {
				if (state->err == INTEL_PT_ERR_NODATA) {
					intel_pt_log("queue %u has no timestamp\n",
						     queue_nr);
					return 0;
				}
				continue;
			}
			if (state->timestamp)
				break;
		}

		ptq->timestamp = state->timestamp;
		intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
			     queue_nr, ptq->timestamp);
		ptq->state = state;
		ptq->have_sample = true;
1162 1163 1164
		if (ptq->sel_start && ptq->sel_timestamp &&
		    ptq->timestamp < ptq->sel_timestamp)
			ptq->have_sample = false;
A
Adrian Hunter 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		intel_pt_sample_flags(ptq);
		ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
		if (ret)
			return ret;
		ptq->on_heap = true;
	}

	return 0;
}

static int intel_pt_setup_queues(struct intel_pt *pt)
{
	unsigned int i;
	int ret;

	for (i = 0; i < pt->queues.nr_queues; i++) {
		ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
		if (ret)
			return ret;
	}
	return 0;
}

1188 1189 1190 1191 1192 1193
static inline bool intel_pt_skip_event(struct intel_pt *pt)
{
	return pt->synth_opts.initial_skip &&
	       pt->num_events++ < pt->synth_opts.initial_skip;
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
/*
 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
 * from this decoder state.
 */
static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
{
	return pt->synth_opts.initial_skip &&
	       pt->num_events + 4 < pt->synth_opts.initial_skip;
}

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
				   union perf_event *event,
				   struct perf_sample *sample)
{
	event->sample.header.type = PERF_RECORD_SAMPLE;
	event->sample.header.size = sizeof(struct perf_event_header);

	sample->pid = ptq->pid;
	sample->tid = ptq->tid;
	sample->cpu = ptq->cpu;
	sample->insn_len = ptq->insn_len;
	memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
}

1219 1220 1221 1222 1223
static void intel_pt_prep_b_sample(struct intel_pt *pt,
				   struct intel_pt_queue *ptq,
				   union perf_event *event,
				   struct perf_sample *sample)
{
1224 1225
	intel_pt_prep_a_sample(ptq, event, sample);

1226 1227 1228 1229
	if (!pt->timeless_decoding)
		sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);

	sample->ip = ptq->state->from_ip;
1230
	sample->cpumode = intel_pt_cpumode(pt, sample->ip);
1231 1232 1233
	sample->addr = ptq->state->to_ip;
	sample->period = 1;
	sample->flags = ptq->flags;
1234 1235

	event->sample.header.misc = sample->cpumode;
1236 1237
}

A
Adrian Hunter 已提交
1238
static int intel_pt_inject_event(union perf_event *event,
1239
				 struct perf_sample *sample, u64 type)
A
Adrian Hunter 已提交
1240 1241
{
	event->header.size = perf_event__sample_event_size(sample, type, 0);
1242
	return perf_event__synthesize_sample(event, type, 0, sample);
A
Adrian Hunter 已提交
1243 1244
}

1245 1246 1247 1248 1249 1250 1251
static inline int intel_pt_opt_inject(struct intel_pt *pt,
				      union perf_event *event,
				      struct perf_sample *sample, u64 type)
{
	if (!pt->synth_opts.inject)
		return 0;

1252
	return intel_pt_inject_event(event, sample, type);
1253 1254
}

1255 1256 1257
static int intel_pt_deliver_synth_event(struct intel_pt *pt,
					union perf_event *event,
					struct perf_sample *sample, u64 type)
A
Adrian Hunter 已提交
1258 1259
{
	int ret;
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

	ret = intel_pt_opt_inject(pt, event, sample, type);
	if (ret)
		return ret;

	ret = perf_session__deliver_synth_event(pt->session, event, sample);
	if (ret)
		pr_err("Intel PT: failed to deliver event, error %d\n", ret);

	return ret;
}

static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
{
A
Adrian Hunter 已提交
1274 1275 1276
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
1277 1278
	struct dummy_branch_stack {
		u64			nr;
1279
		u64			hw_idx;
1280 1281
		struct branch_entry	entries;
	} dummy_bs;
A
Adrian Hunter 已提交
1282

1283 1284 1285
	if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
		return 0;

1286
	if (intel_pt_skip_event(pt))
1287 1288
		return 0;

1289
	intel_pt_prep_b_sample(pt, ptq, event, &sample);
A
Adrian Hunter 已提交
1290 1291 1292 1293

	sample.id = ptq->pt->branches_id;
	sample.stream_id = ptq->pt->branches_id;

1294 1295 1296 1297 1298 1299 1300
	/*
	 * perf report cannot handle events without a branch stack when using
	 * SORT_MODE__BRANCH so make a dummy one.
	 */
	if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
		dummy_bs = (struct dummy_branch_stack){
			.nr = 1,
1301
			.hw_idx = -1ULL,
1302 1303 1304 1305 1306 1307 1308 1309
			.entries = {
				.from = sample.ip,
				.to = sample.addr,
			},
		};
		sample.branch_stack = (struct branch_stack *)&dummy_bs;
	}

1310 1311 1312 1313 1314 1315 1316
	sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
	if (sample.cyc_cnt) {
		sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
		ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
		ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
	}

1317 1318
	return intel_pt_deliver_synth_event(pt, event, &sample,
					    pt->branches_sample_type);
A
Adrian Hunter 已提交
1319 1320
}

1321 1322 1323 1324
static void intel_pt_prep_sample(struct intel_pt *pt,
				 struct intel_pt_queue *ptq,
				 union perf_event *event,
				 struct perf_sample *sample)
A
Adrian Hunter 已提交
1325
{
1326
	intel_pt_prep_b_sample(pt, ptq, event, sample);
1327

A
Adrian Hunter 已提交
1328
	if (pt->synth_opts.callchain) {
1329
		thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1330 1331
				     pt->synth_opts.callchain_sz + 1,
				     sample->ip, pt->kernel_start);
1332
		sample->callchain = ptq->chain;
A
Adrian Hunter 已提交
1333 1334
	}

1335
	if (pt->synth_opts.last_branch) {
1336 1337
		thread_stack__br_sample(ptq->thread, ptq->cpu, ptq->last_branch,
					pt->br_stack_sz);
1338
		sample->branch_stack = ptq->last_branch;
1339
	}
1340
}
1341

1342
static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
A
Adrian Hunter 已提交
1343 1344 1345 1346 1347
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };

1348
	if (intel_pt_skip_event(pt))
1349 1350
		return 0;

1351
	intel_pt_prep_sample(pt, ptq, event, &sample);
A
Adrian Hunter 已提交
1352

1353 1354 1355
	sample.id = ptq->pt->instructions_id;
	sample.stream_id = ptq->pt->instructions_id;
	sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
A
Adrian Hunter 已提交
1356

1357 1358 1359 1360 1361 1362 1363
	sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
	if (sample.cyc_cnt) {
		sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
		ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
		ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
	}

1364
	ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
A
Adrian Hunter 已提交
1365

1366
	return intel_pt_deliver_synth_event(pt, event, &sample,
1367 1368
					    pt->instructions_sample_type);
}
A
Adrian Hunter 已提交
1369

1370 1371 1372 1373 1374
static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
1375

1376 1377
	if (intel_pt_skip_event(pt))
		return 0;
A
Adrian Hunter 已提交
1378

1379
	intel_pt_prep_sample(pt, ptq, event, &sample);
A
Adrian Hunter 已提交
1380

1381 1382
	sample.id = ptq->pt->transactions_id;
	sample.stream_id = ptq->pt->transactions_id;
1383

1384
	return intel_pt_deliver_synth_event(pt, event, &sample,
1385
					    pt->transactions_sample_type);
A
Adrian Hunter 已提交
1386 1387
}

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
static void intel_pt_prep_p_sample(struct intel_pt *pt,
				   struct intel_pt_queue *ptq,
				   union perf_event *event,
				   struct perf_sample *sample)
{
	intel_pt_prep_sample(pt, ptq, event, sample);

	/*
	 * Zero IP is used to mean "trace start" but that is not the case for
	 * power or PTWRITE events with no IP, so clear the flags.
	 */
	if (!sample->ip)
		sample->flags = 0;
}

static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_ptwrite raw;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->ptwrites_id;
	sample.stream_id = ptq->pt->ptwrites_id;

	raw.flags = 0;
	raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
	raw.payload = cpu_to_le64(ptq->state->ptw_payload);

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1425
	return intel_pt_deliver_synth_event(pt, event, &sample,
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
					    pt->ptwrites_sample_type);
}

static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_cbr raw;
	u32 flags;

1437
	if (intel_pt_skip_cbr_event(pt))
1438 1439
		return 0;

1440 1441
	ptq->cbr_seen = ptq->state->cbr;

1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->cbr_id;
	sample.stream_id = ptq->pt->cbr_id;

	flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
	raw.flags = cpu_to_le32(flags);
	raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
	raw.reserved3 = 0;

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1455
	return intel_pt_deliver_synth_event(pt, event, &sample,
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
					    pt->pwr_events_sample_type);
}

static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_mwait raw;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->mwait_id;
	sample.stream_id = ptq->pt->mwait_id;

	raw.reserved = 0;
	raw.payload = cpu_to_le64(ptq->state->mwait_payload);

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1480
	return intel_pt_deliver_synth_event(pt, event, &sample,
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
					    pt->pwr_events_sample_type);
}

static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_pwre raw;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->pwre_id;
	sample.stream_id = ptq->pt->pwre_id;

	raw.reserved = 0;
	raw.payload = cpu_to_le64(ptq->state->pwre_payload);

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1505
	return intel_pt_deliver_synth_event(pt, event, &sample,
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
					    pt->pwr_events_sample_type);
}

static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_exstop raw;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->exstop_id;
	sample.stream_id = ptq->pt->exstop_id;

	raw.flags = 0;
	raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1530
	return intel_pt_deliver_synth_event(pt, event, &sample,
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
					    pt->pwr_events_sample_type);
}

static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;
	union perf_event *event = ptq->event_buf;
	struct perf_sample sample = { .ip = 0, };
	struct perf_synth_intel_pwrx raw;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_p_sample(pt, ptq, event, &sample);

	sample.id = ptq->pt->pwrx_id;
	sample.stream_id = ptq->pt->pwrx_id;

	raw.reserved = 0;
	raw.payload = cpu_to_le64(ptq->state->pwrx_payload);

	sample.raw_size = perf_synth__raw_size(raw);
	sample.raw_data = perf_synth__raw_data(&raw);

1555
	return intel_pt_deliver_synth_event(pt, event, &sample,
1556 1557 1558
					    pt->pwr_events_sample_type);
}

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
/*
 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
 * intel_pt_add_gp_regs().
 */
static const int pebs_gp_regs[] = {
	[PERF_REG_X86_FLAGS]	= 1,
	[PERF_REG_X86_IP]	= 2,
	[PERF_REG_X86_AX]	= 3,
	[PERF_REG_X86_CX]	= 4,
	[PERF_REG_X86_DX]	= 5,
	[PERF_REG_X86_BX]	= 6,
	[PERF_REG_X86_SP]	= 7,
	[PERF_REG_X86_BP]	= 8,
	[PERF_REG_X86_SI]	= 9,
	[PERF_REG_X86_DI]	= 10,
	[PERF_REG_X86_R8]	= 11,
	[PERF_REG_X86_R9]	= 12,
	[PERF_REG_X86_R10]	= 13,
	[PERF_REG_X86_R11]	= 14,
	[PERF_REG_X86_R12]	= 15,
	[PERF_REG_X86_R13]	= 16,
	[PERF_REG_X86_R14]	= 17,
	[PERF_REG_X86_R15]	= 18,
};

static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
				 const struct intel_pt_blk_items *items,
				 u64 regs_mask)
{
	const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
	u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
	u32 bit;
	int i;

	for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
		/* Get the PEBS gp_regs array index */
		int n = pebs_gp_regs[i] - 1;

		if (n < 0)
			continue;
		/*
		 * Add only registers that were requested (i.e. 'regs_mask') and
		 * that were provided (i.e. 'mask'), and update the resulting
		 * mask (i.e. 'intr_regs->mask') accordingly.
		 */
		if (mask & 1 << n && regs_mask & bit) {
			intr_regs->mask |= bit;
			*pos++ = gp_regs[n];
		}
	}

	return pos;
}

1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
#ifndef PERF_REG_X86_XMM0
#define PERF_REG_X86_XMM0 32
#endif

static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
			     const struct intel_pt_blk_items *items,
			     u64 regs_mask)
{
	u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
	const u64 *xmm = items->xmm;

	/*
	 * If there are any XMM registers, then there should be all of them.
	 * Nevertheless, follow the logic to add only registers that were
	 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
	 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
	 */
	intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;

	for (; mask; mask >>= 1, xmm++) {
		if (mask & 1)
			*pos++ = *xmm;
	}
}

1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
#define LBR_INFO_MISPRED	(1ULL << 63)
#define LBR_INFO_IN_TX		(1ULL << 62)
#define LBR_INFO_ABORT		(1ULL << 61)
#define LBR_INFO_CYCLES		0xffff

/* Refer kernel's intel_pmu_store_pebs_lbrs() */
static u64 intel_pt_lbr_flags(u64 info)
{
	union {
		struct branch_flags flags;
		u64 result;
1649 1650 1651 1652 1653 1654 1655 1656
	} u;

	u.result	  = 0;
	u.flags.mispred	  = !!(info & LBR_INFO_MISPRED);
	u.flags.predicted = !(info & LBR_INFO_MISPRED);
	u.flags.in_tx	  = !!(info & LBR_INFO_IN_TX);
	u.flags.abort	  = !!(info & LBR_INFO_ABORT);
	u.flags.cycles	  = info & LBR_INFO_CYCLES;
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688

	return u.result;
}

static void intel_pt_add_lbrs(struct branch_stack *br_stack,
			      const struct intel_pt_blk_items *items)
{
	u64 *to;
	int i;

	br_stack->nr = 0;

	to = &br_stack->entries[0].from;

	for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
		u32 mask = items->mask[i];
		const u64 *from = items->val[i];

		for (; mask; mask >>= 3, from += 3) {
			if ((mask & 7) == 7) {
				*to++ = from[0];
				*to++ = from[1];
				*to++ = intel_pt_lbr_flags(from[2]);
				br_stack->nr += 1;
			}
		}
	}
}

/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3)

1689
static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
1690
{
1691 1692 1693 1694
	const struct intel_pt_blk_items *items = &ptq->state->items;
	struct perf_sample sample = { .ip = 0, };
	union perf_event *event = ptq->event_buf;
	struct intel_pt *pt = ptq->pt;
1695
	struct evsel *evsel = pt->pebs_evsel;
1696
	u64 sample_type = evsel->core.attr.sample_type;
1697
	u64 id = evsel->core.id[0];
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
	u8 cpumode;

	if (intel_pt_skip_event(pt))
		return 0;

	intel_pt_prep_a_sample(ptq, event, &sample);

	sample.id = id;
	sample.stream_id = id;

1708 1709
	if (!evsel->core.attr.freq)
		sample.period = evsel->core.attr.sample_period;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

	/* No support for non-zero CS base */
	if (items->has_ip)
		sample.ip = items->ip;
	else if (items->has_rip)
		sample.ip = items->rip;
	else
		sample.ip = ptq->state->from_ip;

	/* No support for guest mode at this time */
	cpumode = sample.ip < ptq->pt->kernel_start ?
		  PERF_RECORD_MISC_USER :
		  PERF_RECORD_MISC_KERNEL;

	event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;

	sample.cpumode = cpumode;

	if (sample_type & PERF_SAMPLE_TIME) {
		u64 timestamp = 0;

		if (items->has_timestamp)
			timestamp = items->timestamp;
		else if (!pt->timeless_decoding)
			timestamp = ptq->timestamp;
		if (timestamp)
			sample.time = tsc_to_perf_time(timestamp, &pt->tc);
	}

1739 1740 1741 1742 1743 1744 1745 1746
	if (sample_type & PERF_SAMPLE_CALLCHAIN &&
	    pt->synth_opts.callchain) {
		thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
				     pt->synth_opts.callchain_sz, sample.ip,
				     pt->kernel_start);
		sample.callchain = ptq->chain;
	}

1747 1748 1749
	if (sample_type & PERF_SAMPLE_REGS_INTR &&
	    items->mask[INTEL_PT_GP_REGS_POS]) {
		u64 regs[sizeof(sample.intr_regs.mask)];
1750
		u64 regs_mask = evsel->core.attr.sample_regs_intr;
1751
		u64 *pos;
1752 1753 1754 1755 1756 1757

		sample.intr_regs.abi = items->is_32_bit ?
				       PERF_SAMPLE_REGS_ABI_32 :
				       PERF_SAMPLE_REGS_ABI_64;
		sample.intr_regs.regs = regs;

1758 1759 1760
		pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);

		intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
1761 1762
	}

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
	if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
		struct {
			struct branch_stack br_stack;
			struct branch_entry entries[LBRS_MAX];
		} br;

		if (items->mask[INTEL_PT_LBR_0_POS] ||
		    items->mask[INTEL_PT_LBR_1_POS] ||
		    items->mask[INTEL_PT_LBR_2_POS]) {
			intel_pt_add_lbrs(&br.br_stack, items);
			sample.branch_stack = &br.br_stack;
		} else if (pt->synth_opts.last_branch) {
1775 1776 1777
			thread_stack__br_sample(ptq->thread, ptq->cpu,
						ptq->last_branch,
						pt->br_stack_sz);
1778 1779 1780 1781 1782 1783 1784
			sample.branch_stack = ptq->last_branch;
		} else {
			br.br_stack.nr = 0;
			sample.branch_stack = &br.br_stack;
		}
	}

1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
	if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
		sample.addr = items->mem_access_address;

	if (sample_type & PERF_SAMPLE_WEIGHT) {
		/*
		 * Refer kernel's setup_pebs_adaptive_sample_data() and
		 * intel_hsw_weight().
		 */
		if (items->has_mem_access_latency)
			sample.weight = items->mem_access_latency;
		if (!sample.weight && items->has_tsx_aux_info) {
			/* Cycles last block */
			sample.weight = (u32)items->tsx_aux_info;
		}
	}

	if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
		u64 ax = items->has_rax ? items->rax : 0;
		/* Refer kernel's intel_hsw_transaction() */
		u64 txn = (u8)(items->tsx_aux_info >> 32);

		/* For RTM XABORTs also log the abort code from AX */
		if (txn & PERF_TXN_TRANSACTION && ax & 1)
			txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
		sample.transaction = txn;
	}

1812
	return intel_pt_deliver_synth_event(pt, event, &sample, sample_type);
1813 1814
}

A
Adrian Hunter 已提交
1815
static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1816
				pid_t pid, pid_t tid, u64 ip, u64 timestamp)
A
Adrian Hunter 已提交
1817 1818 1819 1820 1821 1822 1823 1824
{
	union perf_event event;
	char msg[MAX_AUXTRACE_ERROR_MSG];
	int err;

	intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);

	auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1825
			     code, cpu, pid, tid, ip, msg, timestamp);
A
Adrian Hunter 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834

	err = perf_session__deliver_synth_event(pt->session, &event, NULL);
	if (err)
		pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
		       err);

	return err;
}

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
				 const struct intel_pt_state *state)
{
	struct intel_pt *pt = ptq->pt;
	u64 tm = ptq->timestamp;

	tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);

	return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
				    ptq->tid, state->from_ip, tm);
}

A
Adrian Hunter 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
{
	struct auxtrace_queue *queue;
	pid_t tid = ptq->next_tid;
	int err;

	if (tid == -1)
		return 0;

	intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);

	err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);

	queue = &pt->queues.queue_array[ptq->queue_nr];
	intel_pt_set_pid_tid_cpu(pt, queue);

	ptq->next_tid = -1;

	return err;
}

static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
{
	struct intel_pt *pt = ptq->pt;

	return ip == pt->switch_ip &&
	       (ptq->flags & PERF_IP_FLAG_BRANCH) &&
	       !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
			       PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
}

1878
#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
1879
			  INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
1880

A
Adrian Hunter 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
static int intel_pt_sample(struct intel_pt_queue *ptq)
{
	const struct intel_pt_state *state = ptq->state;
	struct intel_pt *pt = ptq->pt;
	int err;

	if (!ptq->have_sample)
		return 0;

	ptq->have_sample = false;

1892 1893 1894 1895 1896 1897 1898 1899 1900
	if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) {
		/*
		 * Cycle count and instruction count only go together to create
		 * a valid IPC ratio when the cycle count changes.
		 */
		ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
		ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
	}

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	/*
	 * Do PEBS first to allow for the possibility that the PEBS timestamp
	 * precedes the current timestamp.
	 */
	if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
		err = intel_pt_synth_pebs_sample(ptq);
		if (err)
			return err;
	}

1911 1912
	if (pt->sample_pwr_events) {
		if (ptq->state->cbr != ptq->cbr_seen) {
1913 1914 1915 1916
			err = intel_pt_synth_cbr_sample(ptq);
			if (err)
				return err;
		}
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
		if (state->type & INTEL_PT_PWR_EVT) {
			if (state->type & INTEL_PT_MWAIT_OP) {
				err = intel_pt_synth_mwait_sample(ptq);
				if (err)
					return err;
			}
			if (state->type & INTEL_PT_PWR_ENTRY) {
				err = intel_pt_synth_pwre_sample(ptq);
				if (err)
					return err;
			}
			if (state->type & INTEL_PT_EX_STOP) {
				err = intel_pt_synth_exstop_sample(ptq);
				if (err)
					return err;
			}
			if (state->type & INTEL_PT_PWR_EXIT) {
				err = intel_pt_synth_pwrx_sample(ptq);
				if (err)
					return err;
			}
1938 1939 1940
		}
	}

1941
	if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
A
Adrian Hunter 已提交
1942 1943 1944 1945 1946
		err = intel_pt_synth_instruction_sample(ptq);
		if (err)
			return err;
	}

1947
	if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
A
Adrian Hunter 已提交
1948 1949 1950 1951 1952
		err = intel_pt_synth_transaction_sample(ptq);
		if (err)
			return err;
	}

1953 1954 1955 1956 1957 1958
	if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
		err = intel_pt_synth_ptwrite_sample(ptq);
		if (err)
			return err;
	}

A
Adrian Hunter 已提交
1959 1960 1961
	if (!(state->type & INTEL_PT_BRANCH))
		return 0;

1962 1963 1964 1965 1966 1967 1968
	if (pt->use_thread_stack) {
		thread_stack__event(ptq->thread, ptq->cpu, ptq->flags,
				    state->from_ip, state->to_ip, ptq->insn_len,
				    state->trace_nr, pt->callstack,
				    pt->br_stack_sz,
				    pt->mispred_all);
	} else {
1969
		thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
1970
	}
A
Adrian Hunter 已提交
1971 1972 1973 1974 1975 1976 1977

	if (pt->sample_branches) {
		err = intel_pt_synth_branch_sample(ptq);
		if (err)
			return err;
	}

A
Adrian Hunter 已提交
1978
	if (!ptq->sync_switch)
A
Adrian Hunter 已提交
1979 1980 1981 1982
		return 0;

	if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
		switch (ptq->switch_state) {
1983
		case INTEL_PT_SS_NOT_TRACING:
A
Adrian Hunter 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
		case INTEL_PT_SS_UNKNOWN:
		case INTEL_PT_SS_EXPECTING_SWITCH_IP:
			err = intel_pt_next_tid(pt, ptq);
			if (err)
				return err;
			ptq->switch_state = INTEL_PT_SS_TRACING;
			break;
		default:
			ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
			return 1;
		}
	} else if (!state->to_ip) {
		ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
	} else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
		ptq->switch_state = INTEL_PT_SS_UNKNOWN;
	} else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
		   state->to_ip == pt->ptss_ip &&
		   (ptq->flags & PERF_IP_FLAG_CALL)) {
		ptq->switch_state = INTEL_PT_SS_TRACING;
	}

	return 0;
}

2008
static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
A
Adrian Hunter 已提交
2009
{
2010
	struct machine *machine = pt->machine;
A
Adrian Hunter 已提交
2011 2012 2013
	struct map *map;
	struct symbol *sym, *start;
	u64 ip, switch_ip = 0;
2014
	const char *ptss;
A
Adrian Hunter 已提交
2015 2016 2017 2018

	if (ptss_ip)
		*ptss_ip = 0;

2019
	map = machine__kernel_map(machine);
A
Adrian Hunter 已提交
2020 2021 2022
	if (!map)
		return 0;

2023
	if (map__load(map))
A
Adrian Hunter 已提交
2024 2025
		return 0;

2026
	start = dso__first_symbol(map->dso);
A
Adrian Hunter 已提交
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041

	for (sym = start; sym; sym = dso__next_symbol(sym)) {
		if (sym->binding == STB_GLOBAL &&
		    !strcmp(sym->name, "__switch_to")) {
			ip = map->unmap_ip(map, sym->start);
			if (ip >= map->start && ip < map->end) {
				switch_ip = ip;
				break;
			}
		}
	}

	if (!switch_ip || !ptss_ip)
		return 0;

2042 2043 2044 2045 2046
	if (pt->have_sched_switch == 1)
		ptss = "perf_trace_sched_switch";
	else
		ptss = "__perf_event_task_sched_out";

A
Adrian Hunter 已提交
2047
	for (sym = start; sym; sym = dso__next_symbol(sym)) {
2048
		if (!strcmp(sym->name, ptss)) {
A
Adrian Hunter 已提交
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
			ip = map->unmap_ip(map, sym->start);
			if (ip >= map->start && ip < map->end) {
				*ptss_ip = ip;
				break;
			}
		}
	}

	return switch_ip;
}

A
Adrian Hunter 已提交
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
static void intel_pt_enable_sync_switch(struct intel_pt *pt)
{
	unsigned int i;

	pt->sync_switch = true;

	for (i = 0; i < pt->queues.nr_queues; i++) {
		struct auxtrace_queue *queue = &pt->queues.queue_array[i];
		struct intel_pt_queue *ptq = queue->priv;

		if (ptq)
			ptq->sync_switch = true;
	}
}

2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
/*
 * To filter against time ranges, it is only necessary to look at the next start
 * or end time.
 */
static bool intel_pt_next_time(struct intel_pt_queue *ptq)
{
	struct intel_pt *pt = ptq->pt;

	if (ptq->sel_start) {
		/* Next time is an end time */
		ptq->sel_start = false;
		ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
		return true;
	} else if (ptq->sel_idx + 1 < pt->range_cnt) {
		/* Next time is a start time */
		ptq->sel_start = true;
		ptq->sel_idx += 1;
		ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
		return true;
	}

	/* No next time */
	return false;
}

static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
{
	int err;

	while (1) {
		if (ptq->sel_start) {
			if (ptq->timestamp >= ptq->sel_timestamp) {
				/* After start time, so consider next time */
				intel_pt_next_time(ptq);
				if (!ptq->sel_timestamp) {
					/* No end time */
					return 0;
				}
				/* Check against end time */
				continue;
			}
			/* Before start time, so fast forward */
			ptq->have_sample = false;
			if (ptq->sel_timestamp > *ff_timestamp) {
				if (ptq->sync_switch) {
					intel_pt_next_tid(ptq->pt, ptq);
					ptq->switch_state = INTEL_PT_SS_UNKNOWN;
				}
				*ff_timestamp = ptq->sel_timestamp;
				err = intel_pt_fast_forward(ptq->decoder,
							    ptq->sel_timestamp);
				if (err)
					return err;
			}
			return 0;
		} else if (ptq->timestamp > ptq->sel_timestamp) {
			/* After end time, so consider next time */
			if (!intel_pt_next_time(ptq)) {
				/* No next time range, so stop decoding */
				ptq->have_sample = false;
				ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
				return 1;
			}
			/* Check against next start time */
			continue;
		} else {
			/* Before end time */
			return 0;
		}
	}
}

A
Adrian Hunter 已提交
2147 2148 2149 2150
static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
{
	const struct intel_pt_state *state = ptq->state;
	struct intel_pt *pt = ptq->pt;
2151
	u64 ff_timestamp = 0;
A
Adrian Hunter 已提交
2152 2153 2154 2155
	int err;

	if (!pt->kernel_start) {
		pt->kernel_start = machine__kernel_start(pt->machine);
2156 2157
		if (pt->per_cpu_mmaps &&
		    (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
A
Adrian Hunter 已提交
2158 2159
		    !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
		    !pt->sampling_mode) {
2160
			pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
A
Adrian Hunter 已提交
2161 2162 2163
			if (pt->switch_ip) {
				intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
					     pt->switch_ip, pt->ptss_ip);
A
Adrian Hunter 已提交
2164
				intel_pt_enable_sync_switch(pt);
A
Adrian Hunter 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
			}
		}
	}

	intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
		     ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
	while (1) {
		err = intel_pt_sample(ptq);
		if (err)
			return err;

		state = intel_pt_decode(ptq->decoder);
		if (state->err) {
			if (state->err == INTEL_PT_ERR_NODATA)
				return 1;
A
Adrian Hunter 已提交
2180
			if (ptq->sync_switch &&
A
Adrian Hunter 已提交
2181
			    state->from_ip >= pt->kernel_start) {
A
Adrian Hunter 已提交
2182
				ptq->sync_switch = false;
A
Adrian Hunter 已提交
2183 2184 2185
				intel_pt_next_tid(pt, ptq);
			}
			if (pt->synth_opts.errors) {
2186
				err = intel_ptq_synth_error(ptq, state);
A
Adrian Hunter 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
				if (err)
					return err;
			}
			continue;
		}

		ptq->state = state;
		ptq->have_sample = true;
		intel_pt_sample_flags(ptq);

		/* Use estimated TSC upon return to user space */
		if (pt->est_tsc &&
		    (state->from_ip >= pt->kernel_start || !state->from_ip) &&
		    state->to_ip && state->to_ip < pt->kernel_start) {
			intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
				     state->timestamp, state->est_timestamp);
			ptq->timestamp = state->est_timestamp;
		/* Use estimated TSC in unknown switch state */
A
Adrian Hunter 已提交
2205
		} else if (ptq->sync_switch &&
A
Adrian Hunter 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
			   ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
			   intel_pt_is_switch_ip(ptq, state->to_ip) &&
			   ptq->next_tid == -1) {
			intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
				     state->timestamp, state->est_timestamp);
			ptq->timestamp = state->est_timestamp;
		} else if (state->timestamp > ptq->timestamp) {
			ptq->timestamp = state->timestamp;
		}

2216 2217 2218 2219 2220 2221
		if (ptq->sel_timestamp) {
			err = intel_pt_time_filter(ptq, &ff_timestamp);
			if (err)
				return err;
		}

A
Adrian Hunter 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
		if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
			*timestamp = ptq->timestamp;
			return 0;
		}
	}
	return 0;
}

static inline int intel_pt_update_queues(struct intel_pt *pt)
{
	if (pt->queues.new_data) {
		pt->queues.new_data = false;
		return intel_pt_setup_queues(pt);
	}
	return 0;
}

static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
{
	unsigned int queue_nr;
	u64 ts;
	int ret;

	while (1) {
		struct auxtrace_queue *queue;
		struct intel_pt_queue *ptq;

		if (!pt->heap.heap_cnt)
			return 0;

		if (pt->heap.heap_array[0].ordinal >= timestamp)
			return 0;

		queue_nr = pt->heap.heap_array[0].queue_nr;
		queue = &pt->queues.queue_array[queue_nr];
		ptq = queue->priv;

		intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
			     queue_nr, pt->heap.heap_array[0].ordinal,
			     timestamp);

		auxtrace_heap__pop(&pt->heap);

		if (pt->heap.heap_cnt) {
			ts = pt->heap.heap_array[0].ordinal + 1;
			if (ts > timestamp)
				ts = timestamp;
		} else {
			ts = timestamp;
		}

		intel_pt_set_pid_tid_cpu(pt, queue);

		ret = intel_pt_run_decoder(ptq, &ts);

		if (ret < 0) {
			auxtrace_heap__add(&pt->heap, queue_nr, ts);
			return ret;
		}

		if (!ret) {
			ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
			if (ret < 0)
				return ret;
		} else {
			ptq->on_heap = false;
		}
	}

	return 0;
}

static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
					    u64 time_)
{
	struct auxtrace_queues *queues = &pt->queues;
	unsigned int i;
	u64 ts = 0;

	for (i = 0; i < queues->nr_queues; i++) {
		struct auxtrace_queue *queue = &pt->queues.queue_array[i];
		struct intel_pt_queue *ptq = queue->priv;

		if (ptq && (tid == -1 || ptq->tid == tid)) {
			ptq->time = time_;
			intel_pt_set_pid_tid_cpu(pt, queue);
			intel_pt_run_decoder(ptq, &ts);
		}
	}
	return 0;
}

2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq,
					    struct auxtrace_queue *queue,
					    struct perf_sample *sample)
{
	struct machine *m = ptq->pt->machine;

	ptq->pid = sample->pid;
	ptq->tid = sample->tid;
	ptq->cpu = queue->cpu;

	intel_pt_log("queue %u cpu %d pid %d tid %d\n",
		     ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);

	thread__zput(ptq->thread);

	if (ptq->tid == -1)
		return;

	if (ptq->pid == -1) {
		ptq->thread = machine__find_thread(m, -1, ptq->tid);
		if (ptq->thread)
			ptq->pid = ptq->thread->pid_;
		return;
	}

	ptq->thread = machine__findnew_thread(m, ptq->pid, ptq->tid);
}

static int intel_pt_process_timeless_sample(struct intel_pt *pt,
					    struct perf_sample *sample)
{
	struct auxtrace_queue *queue;
	struct intel_pt_queue *ptq;
	u64 ts = 0;

	queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
	if (!queue)
		return -EINVAL;

	ptq = queue->priv;
	if (!ptq)
		return 0;

	ptq->stop = false;
	ptq->time = sample->time;
	intel_pt_sample_set_pid_tid_cpu(ptq, queue, sample);
	intel_pt_run_decoder(ptq, &ts);
	return 0;
}

A
Adrian Hunter 已提交
2364 2365 2366
static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
{
	return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
2367
				    sample->pid, sample->tid, 0, sample->time);
A
Adrian Hunter 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
}

static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
{
	unsigned i, j;

	if (cpu < 0 || !pt->queues.nr_queues)
		return NULL;

	if ((unsigned)cpu >= pt->queues.nr_queues)
		i = pt->queues.nr_queues - 1;
	else
		i = cpu;

	if (pt->queues.queue_array[i].cpu == cpu)
		return pt->queues.queue_array[i].priv;

	for (j = 0; i > 0; j++) {
		if (pt->queues.queue_array[--i].cpu == cpu)
			return pt->queues.queue_array[i].priv;
	}

	for (; j < pt->queues.nr_queues; j++) {
		if (pt->queues.queue_array[j].cpu == cpu)
			return pt->queues.queue_array[j].priv;
	}

	return NULL;
}

2398 2399
static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
				u64 timestamp)
A
Adrian Hunter 已提交
2400 2401
{
	struct intel_pt_queue *ptq;
2402
	int err;
A
Adrian Hunter 已提交
2403 2404

	if (!pt->sync_switch)
2405
		return 1;
A
Adrian Hunter 已提交
2406 2407

	ptq = intel_pt_cpu_to_ptq(pt, cpu);
A
Adrian Hunter 已提交
2408
	if (!ptq || !ptq->sync_switch)
2409
		return 1;
A
Adrian Hunter 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420

	switch (ptq->switch_state) {
	case INTEL_PT_SS_NOT_TRACING:
		break;
	case INTEL_PT_SS_UNKNOWN:
	case INTEL_PT_SS_TRACING:
		ptq->next_tid = tid;
		ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
		return 0;
	case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
		if (!ptq->on_heap) {
2421
			ptq->timestamp = perf_time_to_tsc(timestamp,
A
Adrian Hunter 已提交
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
							  &pt->tc);
			err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
						 ptq->timestamp);
			if (err)
				return err;
			ptq->on_heap = true;
		}
		ptq->switch_state = INTEL_PT_SS_TRACING;
		break;
	case INTEL_PT_SS_EXPECTING_SWITCH_IP:
		intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
		break;
	default:
		break;
	}
2437

2438 2439
	ptq->next_tid = -1;

2440 2441 2442 2443 2444 2445
	return 1;
}

static int intel_pt_process_switch(struct intel_pt *pt,
				   struct perf_sample *sample)
{
2446
	struct evsel *evsel;
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
	pid_t tid;
	int cpu, ret;

	evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
	if (evsel != pt->switch_evsel)
		return 0;

	tid = perf_evsel__intval(evsel, sample, "next_pid");
	cpu = sample->cpu;

	intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
		     cpu, tid, sample->time, perf_time_to_tsc(sample->time,
		     &pt->tc));

	ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
	if (ret <= 0)
		return ret;

A
Adrian Hunter 已提交
2465 2466 2467
	return machine__set_current_tid(pt->machine, cpu, -1, tid);
}

2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
static int intel_pt_context_switch_in(struct intel_pt *pt,
				      struct perf_sample *sample)
{
	pid_t pid = sample->pid;
	pid_t tid = sample->tid;
	int cpu = sample->cpu;

	if (pt->sync_switch) {
		struct intel_pt_queue *ptq;

		ptq = intel_pt_cpu_to_ptq(pt, cpu);
		if (ptq && ptq->sync_switch) {
			ptq->next_tid = -1;
			switch (ptq->switch_state) {
			case INTEL_PT_SS_NOT_TRACING:
			case INTEL_PT_SS_UNKNOWN:
			case INTEL_PT_SS_TRACING:
				break;
			case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
			case INTEL_PT_SS_EXPECTING_SWITCH_IP:
				ptq->switch_state = INTEL_PT_SS_TRACING;
				break;
			default:
				break;
			}
		}
	}

	/*
	 * If the current tid has not been updated yet, ensure it is now that
	 * a "switch in" event has occurred.
	 */
	if (machine__get_current_tid(pt->machine, cpu) == tid)
		return 0;

	return machine__set_current_tid(pt->machine, cpu, pid, tid);
}

2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
				   struct perf_sample *sample)
{
	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
	pid_t pid, tid;
	int cpu, ret;

	cpu = sample->cpu;

	if (pt->have_sched_switch == 3) {
		if (!out)
2517
			return intel_pt_context_switch_in(pt, sample);
2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
		if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
			pr_err("Expecting CPU-wide context switch event\n");
			return -EINVAL;
		}
		pid = event->context_switch.next_prev_pid;
		tid = event->context_switch.next_prev_tid;
	} else {
		if (out)
			return 0;
		pid = sample->pid;
		tid = sample->tid;
	}

	if (tid == -1) {
		pr_err("context_switch event has no tid\n");
		return -EINVAL;
	}

	intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
		     cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
		     &pt->tc));

	ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
	if (ret <= 0)
		return ret;

	return machine__set_current_tid(pt->machine, cpu, pid, tid);
}

A
Adrian Hunter 已提交
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
static int intel_pt_process_itrace_start(struct intel_pt *pt,
					 union perf_event *event,
					 struct perf_sample *sample)
{
	if (!pt->per_cpu_mmaps)
		return 0;

	intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
		     sample->cpu, event->itrace_start.pid,
		     event->itrace_start.tid, sample->time,
		     perf_time_to_tsc(sample->time, &pt->tc));

	return machine__set_current_tid(pt->machine, sample->cpu,
					event->itrace_start.pid,
					event->itrace_start.tid);
}

static int intel_pt_process_event(struct perf_session *session,
				  union perf_event *event,
				  struct perf_sample *sample,
				  struct perf_tool *tool)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);
	u64 timestamp;
	int err = 0;

	if (dump_trace)
		return 0;

	if (!tool->ordered_events) {
		pr_err("Intel Processor Trace requires ordered events\n");
		return -EINVAL;
	}

2582
	if (sample->time && sample->time != (u64)-1)
A
Adrian Hunter 已提交
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
		timestamp = perf_time_to_tsc(sample->time, &pt->tc);
	else
		timestamp = 0;

	if (timestamp || pt->timeless_decoding) {
		err = intel_pt_update_queues(pt);
		if (err)
			return err;
	}

	if (pt->timeless_decoding) {
2594 2595 2596 2597 2598
		if (pt->sampling_mode) {
			if (sample->aux_sample.size)
				err = intel_pt_process_timeless_sample(pt,
								       sample);
		} else if (event->header.type == PERF_RECORD_EXIT) {
A
Adrian Hunter 已提交
2599
			err = intel_pt_process_timeless_queues(pt,
2600
							       event->fork.tid,
A
Adrian Hunter 已提交
2601 2602 2603 2604 2605 2606 2607 2608
							       sample->time);
		}
	} else if (timestamp) {
		err = intel_pt_process_queues(pt, timestamp);
	}
	if (err)
		return err;

2609 2610 2611 2612 2613
	if (event->header.type == PERF_RECORD_SAMPLE) {
		if (pt->synth_opts.add_callchain && !sample->callchain)
			intel_pt_add_callchain(pt, sample);
	}

A
Adrian Hunter 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
	if (event->header.type == PERF_RECORD_AUX &&
	    (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
	    pt->synth_opts.errors) {
		err = intel_pt_lost(pt, sample);
		if (err)
			return err;
	}

	if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
		err = intel_pt_process_switch(pt, sample);
	else if (event->header.type == PERF_RECORD_ITRACE_START)
		err = intel_pt_process_itrace_start(pt, event, sample);
2626 2627 2628
	else if (event->header.type == PERF_RECORD_SWITCH ||
		 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
		err = intel_pt_context_switch(pt, event, sample);
A
Adrian Hunter 已提交
2629

2630 2631 2632
	intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
		     event->header.type, sample->cpu, sample->time, timestamp);
	intel_pt_log_event(event);
A
Adrian Hunter 已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682

	return err;
}

static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);
	int ret;

	if (dump_trace)
		return 0;

	if (!tool->ordered_events)
		return -EINVAL;

	ret = intel_pt_update_queues(pt);
	if (ret < 0)
		return ret;

	if (pt->timeless_decoding)
		return intel_pt_process_timeless_queues(pt, -1,
							MAX_TIMESTAMP - 1);

	return intel_pt_process_queues(pt, MAX_TIMESTAMP);
}

static void intel_pt_free_events(struct perf_session *session)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);
	struct auxtrace_queues *queues = &pt->queues;
	unsigned int i;

	for (i = 0; i < queues->nr_queues; i++) {
		intel_pt_free_queue(queues->queue_array[i].priv);
		queues->queue_array[i].priv = NULL;
	}
	intel_pt_log_disable();
	auxtrace_queues__free(queues);
}

static void intel_pt_free(struct perf_session *session)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);

	auxtrace_heap__free(&pt->heap);
	intel_pt_free_events(session);
	session->auxtrace = NULL;
2683
	thread__put(pt->unknown_thread);
2684
	addr_filters__exit(&pt->filts);
2685
	zfree(&pt->chain);
2686
	zfree(&pt->filter);
2687
	zfree(&pt->time_ranges);
A
Adrian Hunter 已提交
2688 2689 2690
	free(pt);
}

2691 2692 2693 2694 2695 2696 2697 2698 2699
static bool intel_pt_evsel_is_auxtrace(struct perf_session *session,
				       struct evsel *evsel)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);

	return evsel->core.attr.type == pt->pmu_type;
}

A
Adrian Hunter 已提交
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
static int intel_pt_process_auxtrace_event(struct perf_session *session,
					   union perf_event *event,
					   struct perf_tool *tool __maybe_unused)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);

	if (!pt->data_queued) {
		struct auxtrace_buffer *buffer;
		off_t data_offset;
2710
		int fd = perf_data__fd(session->data);
A
Adrian Hunter 已提交
2711 2712
		int err;

2713
		if (perf_data__is_pipe(session->data)) {
A
Adrian Hunter 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
			data_offset = 0;
		} else {
			data_offset = lseek(fd, 0, SEEK_CUR);
			if (data_offset == -1)
				return -errno;
		}

		err = auxtrace_queues__add_event(&pt->queues, session, event,
						 data_offset, &buffer);
		if (err)
			return err;

		/* Dump here now we have copied a piped trace out of the pipe */
		if (dump_trace) {
			if (auxtrace_buffer__get_data(buffer, fd)) {
				intel_pt_dump_event(pt, buffer->data,
						    buffer->size);
				auxtrace_buffer__put_data(buffer);
			}
		}
	}

	return 0;
}

2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
static int intel_pt_queue_data(struct perf_session *session,
			       struct perf_sample *sample,
			       union perf_event *event, u64 data_offset)
{
	struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
					   auxtrace);
	u64 timestamp;

	if (event) {
		return auxtrace_queues__add_event(&pt->queues, session, event,
						  data_offset, NULL);
	}

	if (sample->time && sample->time != (u64)-1)
		timestamp = perf_time_to_tsc(sample->time, &pt->tc);
	else
		timestamp = 0;

	return auxtrace_queues__add_sample(&pt->queues, session, sample,
					   data_offset, timestamp);
}

A
Adrian Hunter 已提交
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
struct intel_pt_synth {
	struct perf_tool dummy_tool;
	struct perf_session *session;
};

static int intel_pt_event_synth(struct perf_tool *tool,
				union perf_event *event,
				struct perf_sample *sample __maybe_unused,
				struct machine *machine __maybe_unused)
{
	struct intel_pt_synth *intel_pt_synth =
			container_of(tool, struct intel_pt_synth, dummy_tool);

	return perf_session__deliver_synth_event(intel_pt_synth->session, event,
						 NULL);
}

2778
static int intel_pt_synth_event(struct perf_session *session, const char *name,
A
Adrian Hunter 已提交
2779 2780 2781
				struct perf_event_attr *attr, u64 id)
{
	struct intel_pt_synth intel_pt_synth;
2782 2783 2784 2785
	int err;

	pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
		 name, id, (u64)attr->sample_type);
A
Adrian Hunter 已提交
2786 2787 2788 2789

	memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
	intel_pt_synth.session = session;

2790 2791 2792 2793 2794 2795 2796
	err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
					  &id, intel_pt_event_synth);
	if (err)
		pr_err("%s: failed to synthesize '%s' event type\n",
		       __func__, name);

	return err;
A
Adrian Hunter 已提交
2797 2798
}

2799
static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
2800 2801
				    const char *name)
{
2802
	struct evsel *evsel;
2803 2804

	evlist__for_each_entry(evlist, evsel) {
2805
		if (evsel->core.id && evsel->core.id[0] == id) {
2806 2807 2808 2809 2810 2811 2812 2813
			if (evsel->name)
				zfree(&evsel->name);
			evsel->name = strdup(name);
			break;
		}
	}
}

2814
static struct evsel *intel_pt_evsel(struct intel_pt *pt,
2815
					 struct evlist *evlist)
2816
{
2817
	struct evsel *evsel;
2818 2819

	evlist__for_each_entry(evlist, evsel) {
2820
		if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids)
2821 2822 2823 2824 2825 2826
			return evsel;
	}

	return NULL;
}

A
Adrian Hunter 已提交
2827 2828 2829
static int intel_pt_synth_events(struct intel_pt *pt,
				 struct perf_session *session)
{
2830
	struct evlist *evlist = session->evlist;
2831
	struct evsel *evsel = intel_pt_evsel(pt, evlist);
A
Adrian Hunter 已提交
2832 2833 2834 2835
	struct perf_event_attr attr;
	u64 id;
	int err;

2836
	if (!evsel) {
A
Adrian Hunter 已提交
2837 2838 2839 2840 2841 2842 2843
		pr_debug("There are no selected events with Intel Processor Trace data\n");
		return 0;
	}

	memset(&attr, 0, sizeof(struct perf_event_attr));
	attr.size = sizeof(struct perf_event_attr);
	attr.type = PERF_TYPE_HARDWARE;
2844
	attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
A
Adrian Hunter 已提交
2845 2846 2847 2848 2849 2850 2851 2852
	attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
			    PERF_SAMPLE_PERIOD;
	if (pt->timeless_decoding)
		attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
	else
		attr.sample_type |= PERF_SAMPLE_TIME;
	if (!pt->per_cpu_mmaps)
		attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
2853 2854 2855 2856 2857 2858 2859
	attr.exclude_user = evsel->core.attr.exclude_user;
	attr.exclude_kernel = evsel->core.attr.exclude_kernel;
	attr.exclude_hv = evsel->core.attr.exclude_hv;
	attr.exclude_host = evsel->core.attr.exclude_host;
	attr.exclude_guest = evsel->core.attr.exclude_guest;
	attr.sample_id_all = evsel->core.attr.sample_id_all;
	attr.read_format = evsel->core.attr.read_format;
A
Adrian Hunter 已提交
2860

2861
	id = evsel->core.id[0] + 1000000000;
A
Adrian Hunter 已提交
2862 2863 2864
	if (!id)
		id = 1;

2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
	if (pt->synth_opts.branches) {
		attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
		attr.sample_period = 1;
		attr.sample_type |= PERF_SAMPLE_ADDR;
		err = intel_pt_synth_event(session, "branches", &attr, id);
		if (err)
			return err;
		pt->sample_branches = true;
		pt->branches_sample_type = attr.sample_type;
		pt->branches_id = id;
		id += 1;
		attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
	}

	if (pt->synth_opts.callchain)
		attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
	if (pt->synth_opts.last_branch)
		attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;

A
Adrian Hunter 已提交
2884 2885 2886 2887 2888 2889 2890
	if (pt->synth_opts.instructions) {
		attr.config = PERF_COUNT_HW_INSTRUCTIONS;
		if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
			attr.sample_period =
				intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
		else
			attr.sample_period = pt->synth_opts.period;
2891 2892
		err = intel_pt_synth_event(session, "instructions", &attr, id);
		if (err)
A
Adrian Hunter 已提交
2893 2894 2895 2896 2897 2898 2899
			return err;
		pt->sample_instructions = true;
		pt->instructions_sample_type = attr.sample_type;
		pt->instructions_id = id;
		id += 1;
	}

2900 2901 2902
	attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
	attr.sample_period = 1;

A
Adrian Hunter 已提交
2903 2904
	if (pt->synth_opts.transactions) {
		attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2905 2906
		err = intel_pt_synth_event(session, "transactions", &attr, id);
		if (err)
A
Adrian Hunter 已提交
2907 2908
			return err;
		pt->sample_transactions = true;
2909
		pt->transactions_sample_type = attr.sample_type;
A
Adrian Hunter 已提交
2910
		pt->transactions_id = id;
2911
		intel_pt_set_event_name(evlist, id, "transactions");
A
Adrian Hunter 已提交
2912 2913 2914
		id += 1;
	}

2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
	attr.type = PERF_TYPE_SYNTH;
	attr.sample_type |= PERF_SAMPLE_RAW;

	if (pt->synth_opts.ptwrites) {
		attr.config = PERF_SYNTH_INTEL_PTWRITE;
		err = intel_pt_synth_event(session, "ptwrite", &attr, id);
		if (err)
			return err;
		pt->sample_ptwrites = true;
		pt->ptwrites_sample_type = attr.sample_type;
		pt->ptwrites_id = id;
		intel_pt_set_event_name(evlist, id, "ptwrite");
		id += 1;
	}

	if (pt->synth_opts.pwr_events) {
		pt->sample_pwr_events = true;
		pt->pwr_events_sample_type = attr.sample_type;

		attr.config = PERF_SYNTH_INTEL_CBR;
		err = intel_pt_synth_event(session, "cbr", &attr, id);
		if (err)
			return err;
		pt->cbr_id = id;
		intel_pt_set_event_name(evlist, id, "cbr");
		id += 1;
	}

2943
	if (pt->synth_opts.pwr_events && (evsel->core.attr.config & 0x10)) {
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
		attr.config = PERF_SYNTH_INTEL_MWAIT;
		err = intel_pt_synth_event(session, "mwait", &attr, id);
		if (err)
			return err;
		pt->mwait_id = id;
		intel_pt_set_event_name(evlist, id, "mwait");
		id += 1;

		attr.config = PERF_SYNTH_INTEL_PWRE;
		err = intel_pt_synth_event(session, "pwre", &attr, id);
		if (err)
			return err;
		pt->pwre_id = id;
		intel_pt_set_event_name(evlist, id, "pwre");
		id += 1;

		attr.config = PERF_SYNTH_INTEL_EXSTOP;
		err = intel_pt_synth_event(session, "exstop", &attr, id);
		if (err)
			return err;
		pt->exstop_id = id;
		intel_pt_set_event_name(evlist, id, "exstop");
		id += 1;

		attr.config = PERF_SYNTH_INTEL_PWRX;
		err = intel_pt_synth_event(session, "pwrx", &attr, id);
		if (err)
			return err;
		pt->pwrx_id = id;
		intel_pt_set_event_name(evlist, id, "pwrx");
		id += 1;
	}

A
Adrian Hunter 已提交
2977 2978 2979
	return 0;
}

2980 2981 2982 2983 2984 2985 2986 2987
static void intel_pt_setup_pebs_events(struct intel_pt *pt)
{
	struct evsel *evsel;

	if (!pt->synth_opts.other_events)
		return;

	evlist__for_each_entry(pt->session->evlist, evsel) {
2988
		if (evsel->core.attr.aux_output && evsel->core.id) {
2989 2990 2991 2992 2993 2994 2995
			pt->sample_pebs = true;
			pt->pebs_evsel = evsel;
			return;
		}
	}
}

2996
static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
A
Adrian Hunter 已提交
2997
{
2998
	struct evsel *evsel;
A
Adrian Hunter 已提交
2999

3000
	evlist__for_each_entry_reverse(evlist, evsel) {
A
Adrian Hunter 已提交
3001 3002 3003 3004 3005 3006 3007 3008 3009
		const char *name = perf_evsel__name(evsel);

		if (!strcmp(name, "sched:sched_switch"))
			return evsel;
	}

	return NULL;
}

3010
static bool intel_pt_find_switch(struct evlist *evlist)
3011
{
3012
	struct evsel *evsel;
3013

3014
	evlist__for_each_entry(evlist, evsel) {
3015
		if (evsel->core.attr.context_switch)
3016 3017 3018 3019 3020 3021
			return true;
	}

	return false;
}

3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
static int intel_pt_perf_config(const char *var, const char *value, void *data)
{
	struct intel_pt *pt = data;

	if (!strcmp(var, "intel-pt.mispred-all"))
		pt->mispred_all = perf_config_bool(var, value);

	return 0;
}

3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
/* Find least TSC which converts to ns or later */
static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
{
	u64 tsc, tm;

	tsc = perf_time_to_tsc(ns, &pt->tc);

	while (1) {
		tm = tsc_to_perf_time(tsc, &pt->tc);
		if (tm < ns)
			break;
		tsc -= 1;
	}

	while (tm < ns)
		tm = tsc_to_perf_time(++tsc, &pt->tc);

	return tsc;
}

/* Find greatest TSC which converts to ns or earlier */
static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
{
	u64 tsc, tm;

	tsc = perf_time_to_tsc(ns, &pt->tc);

	while (1) {
		tm = tsc_to_perf_time(tsc, &pt->tc);
		if (tm > ns)
			break;
		tsc += 1;
	}

	while (tm > ns)
		tm = tsc_to_perf_time(--tsc, &pt->tc);

	return tsc;
}

static int intel_pt_setup_time_ranges(struct intel_pt *pt,
				      struct itrace_synth_opts *opts)
{
	struct perf_time_interval *p = opts->ptime_range;
	int n = opts->range_num;
	int i;

	if (!n || !p || pt->timeless_decoding)
		return 0;

	pt->time_ranges = calloc(n, sizeof(struct range));
	if (!pt->time_ranges)
		return -ENOMEM;

	pt->range_cnt = n;

	intel_pt_log("%s: %u range(s)\n", __func__, n);

	for (i = 0; i < n; i++) {
		struct range *r = &pt->time_ranges[i];
		u64 ts = p[i].start;
		u64 te = p[i].end;

		/*
		 * Take care to ensure the TSC range matches the perf-time range
		 * when converted back to perf-time.
		 */
		r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
		r->end   = te ? intel_pt_tsc_end(te, pt) : 0;

		intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
			     i, ts, te);
		intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
			     i, r->start, r->end);
	}

	return 0;
}

A
Adrian Hunter 已提交
3111
static const char * const intel_pt_info_fmts[] = {
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
	[INTEL_PT_PMU_TYPE]		= "  PMU Type            %"PRId64"\n",
	[INTEL_PT_TIME_SHIFT]		= "  Time Shift          %"PRIu64"\n",
	[INTEL_PT_TIME_MULT]		= "  Time Muliplier      %"PRIu64"\n",
	[INTEL_PT_TIME_ZERO]		= "  Time Zero           %"PRIu64"\n",
	[INTEL_PT_CAP_USER_TIME_ZERO]	= "  Cap Time Zero       %"PRId64"\n",
	[INTEL_PT_TSC_BIT]		= "  TSC bit             %#"PRIx64"\n",
	[INTEL_PT_NORETCOMP_BIT]	= "  NoRETComp bit       %#"PRIx64"\n",
	[INTEL_PT_HAVE_SCHED_SWITCH]	= "  Have sched_switch   %"PRId64"\n",
	[INTEL_PT_SNAPSHOT_MODE]	= "  Snapshot mode       %"PRId64"\n",
	[INTEL_PT_PER_CPU_MMAPS]	= "  Per-cpu maps        %"PRId64"\n",
	[INTEL_PT_MTC_BIT]		= "  MTC bit             %#"PRIx64"\n",
	[INTEL_PT_TSC_CTC_N]		= "  TSC:CTC numerator   %"PRIu64"\n",
	[INTEL_PT_TSC_CTC_D]		= "  TSC:CTC denominator %"PRIu64"\n",
	[INTEL_PT_CYC_BIT]		= "  CYC bit             %#"PRIx64"\n",
3126
	[INTEL_PT_MAX_NONTURBO_RATIO]	= "  Max non-turbo ratio %"PRIu64"\n",
3127
	[INTEL_PT_FILTER_STR_LEN]	= "  Filter string len.  %"PRIu64"\n",
A
Adrian Hunter 已提交
3128 3129
};

3130
static void intel_pt_print_info(__u64 *arr, int start, int finish)
A
Adrian Hunter 已提交
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
{
	int i;

	if (!dump_trace)
		return;

	for (i = start; i <= finish; i++)
		fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
}

3141 3142 3143 3144 3145 3146 3147 3148
static void intel_pt_print_info_str(const char *name, const char *str)
{
	if (!dump_trace)
		return;

	fprintf(stdout, "  %-20s%s\n", name, str ? str : "");
}

3149
static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
3150 3151
{
	return auxtrace_info->header.size >=
3152
		sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
3153 3154
}

A
Adrian Hunter 已提交
3155 3156 3157
int intel_pt_process_auxtrace_info(union perf_event *event,
				   struct perf_session *session)
{
3158
	struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
A
Adrian Hunter 已提交
3159 3160
	size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
	struct intel_pt *pt;
3161
	void *info_end;
3162
	__u64 *info;
A
Adrian Hunter 已提交
3163 3164
	int err;

3165
	if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
A
Adrian Hunter 已提交
3166 3167 3168 3169 3170 3171 3172
					min_sz)
		return -EINVAL;

	pt = zalloc(sizeof(struct intel_pt));
	if (!pt)
		return -ENOMEM;

3173 3174
	addr_filters__init(&pt->filts);

3175 3176 3177
	err = perf_config(intel_pt_perf_config, pt);
	if (err)
		goto err_free;
3178

A
Adrian Hunter 已提交
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
	err = auxtrace_queues__init(&pt->queues);
	if (err)
		goto err_free;

	intel_pt_log_set_name(INTEL_PT_PMU_NAME);

	pt->session = session;
	pt->machine = &session->machines.host; /* No kvm support */
	pt->auxtrace_type = auxtrace_info->type;
	pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
	pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
	pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
	pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
	pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
	pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
	pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
	pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
	pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
	pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
	intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
			    INTEL_PT_PER_CPU_MMAPS);

3201
	if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
3202 3203 3204 3205 3206 3207 3208 3209 3210
		pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
		pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
		pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
		pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
		pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
		intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
				    INTEL_PT_CYC_BIT);
	}

3211
	if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
3212 3213 3214 3215 3216 3217 3218
		pt->max_non_turbo_ratio =
			auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
		intel_pt_print_info(&auxtrace_info->priv[0],
				    INTEL_PT_MAX_NONTURBO_RATIO,
				    INTEL_PT_MAX_NONTURBO_RATIO);
	}

3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
	info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
	info_end = (void *)info + auxtrace_info->header.size;

	if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
		size_t len;

		len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
		intel_pt_print_info(&auxtrace_info->priv[0],
				    INTEL_PT_FILTER_STR_LEN,
				    INTEL_PT_FILTER_STR_LEN);
		if (len) {
			const char *filter = (const char *)info;

			len = roundup(len + 1, 8);
			info += len >> 3;
			if ((void *)info > info_end) {
				pr_err("%s: bad filter string length\n", __func__);
				err = -EINVAL;
				goto err_free_queues;
			}
			pt->filter = memdup(filter, len);
			if (!pt->filter) {
				err = -ENOMEM;
				goto err_free_queues;
			}
			if (session->header.needs_swap)
				mem_bswap_64(pt->filter, len);
			if (pt->filter[len - 1]) {
				pr_err("%s: filter string not null terminated\n", __func__);
				err = -EINVAL;
				goto err_free_queues;
			}
3251 3252 3253 3254
			err = addr_filters__parse_bare_filter(&pt->filts,
							      filter);
			if (err)
				goto err_free_queues;
3255 3256 3257 3258
		}
		intel_pt_print_info_str("Filter string", pt->filter);
	}

A
Adrian Hunter 已提交
3259
	pt->timeless_decoding = intel_pt_timeless_decoding(pt);
3260 3261
	if (pt->timeless_decoding && !pt->tc.time_mult)
		pt->tc.time_mult = 1;
A
Adrian Hunter 已提交
3262
	pt->have_tsc = intel_pt_have_tsc(pt);
3263
	pt->sampling_mode = intel_pt_sampling_mode(pt);
A
Adrian Hunter 已提交
3264 3265 3266 3267 3268 3269 3270
	pt->est_tsc = !pt->timeless_decoding;

	pt->unknown_thread = thread__new(999999999, 999999999);
	if (!pt->unknown_thread) {
		err = -ENOMEM;
		goto err_free_queues;
	}
3271 3272 3273 3274 3275 3276 3277 3278 3279

	/*
	 * Since this thread will not be kept in any rbtree not in a
	 * list, initialize its list node so that at thread__put() the
	 * current thread lifetime assuption is kept and we don't segfault
	 * at list_del_init().
	 */
	INIT_LIST_HEAD(&pt->unknown_thread->node);

A
Adrian Hunter 已提交
3280 3281 3282
	err = thread__set_comm(pt->unknown_thread, "unknown", 0);
	if (err)
		goto err_delete_thread;
3283
	if (thread__init_maps(pt->unknown_thread, pt->machine)) {
A
Adrian Hunter 已提交
3284 3285 3286 3287 3288 3289
		err = -ENOMEM;
		goto err_delete_thread;
	}

	pt->auxtrace.process_event = intel_pt_process_event;
	pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
3290 3291
	pt->auxtrace.queue_data = intel_pt_queue_data;
	pt->auxtrace.dump_auxtrace_sample = intel_pt_dump_sample;
A
Adrian Hunter 已提交
3292 3293 3294
	pt->auxtrace.flush_events = intel_pt_flush;
	pt->auxtrace.free_events = intel_pt_free_events;
	pt->auxtrace.free = intel_pt_free;
3295
	pt->auxtrace.evsel_is_auxtrace = intel_pt_evsel_is_auxtrace;
A
Adrian Hunter 已提交
3296 3297 3298 3299 3300 3301 3302 3303 3304
	session->auxtrace = &pt->auxtrace;

	if (dump_trace)
		return 0;

	if (pt->have_sched_switch == 1) {
		pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
		if (!pt->switch_evsel) {
			pr_err("%s: missing sched_switch event\n", __func__);
3305
			err = -EINVAL;
A
Adrian Hunter 已提交
3306 3307
			goto err_delete_thread;
		}
3308 3309 3310
	} else if (pt->have_sched_switch == 2 &&
		   !intel_pt_find_switch(session->evlist)) {
		pr_err("%s: missing context_switch attribute flag\n", __func__);
3311
		err = -EINVAL;
3312
		goto err_delete_thread;
A
Adrian Hunter 已提交
3313 3314
	}

3315
	if (session->itrace_synth_opts->set) {
A
Adrian Hunter 已提交
3316 3317
		pt->synth_opts = *session->itrace_synth_opts;
	} else {
3318 3319
		itrace_synth_opts__set_default(&pt->synth_opts,
				session->itrace_synth_opts->default_no_sample);
3320 3321
		if (!session->itrace_synth_opts->default_no_sample &&
		    !session->itrace_synth_opts->inject) {
A
Adrian Hunter 已提交
3322 3323
			pt->synth_opts.branches = false;
			pt->synth_opts.callchain = true;
3324
			pt->synth_opts.add_callchain = true;
A
Adrian Hunter 已提交
3325
		}
3326
		pt->synth_opts.thread_stack =
3327
				session->itrace_synth_opts->thread_stack;
A
Adrian Hunter 已提交
3328 3329 3330 3331 3332 3333 3334 3335 3336
	}

	if (pt->synth_opts.log)
		intel_pt_log_enable();

	/* Maximum non-turbo ratio is TSC freq / 100 MHz */
	if (pt->tc.time_mult) {
		u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);

3337 3338 3339
		if (!pt->max_non_turbo_ratio)
			pt->max_non_turbo_ratio =
					(tsc_freq + 50000000) / 100000000;
A
Adrian Hunter 已提交
3340 3341 3342
		intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
		intel_pt_log("Maximum non-turbo ratio %u\n",
			     pt->max_non_turbo_ratio);
3343
		pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
A
Adrian Hunter 已提交
3344 3345
	}

3346 3347 3348
	err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
	if (err)
		goto err_delete_thread;
3349

A
Adrian Hunter 已提交
3350 3351 3352 3353 3354 3355 3356
	if (pt->synth_opts.calls)
		pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
				       PERF_IP_FLAG_TRACE_END;
	if (pt->synth_opts.returns)
		pt->branches_filter |= PERF_IP_FLAG_RETURN |
				       PERF_IP_FLAG_TRACE_BEGIN;

3357 3358
	if ((pt->synth_opts.callchain || pt->synth_opts.add_callchain) &&
	    !symbol_conf.use_callchain) {
A
Adrian Hunter 已提交
3359 3360 3361 3362
		symbol_conf.use_callchain = true;
		if (callchain_register_param(&callchain_param) < 0) {
			symbol_conf.use_callchain = false;
			pt->synth_opts.callchain = false;
3363
			pt->synth_opts.add_callchain = false;
A
Adrian Hunter 已提交
3364 3365 3366
		}
	}

3367 3368 3369 3370 3371 3372
	if (pt->synth_opts.add_callchain) {
		err = intel_pt_callchain_init(pt);
		if (err)
			goto err_delete_thread;
	}

3373 3374 3375
	if (pt->synth_opts.last_branch)
		pt->br_stack_sz = pt->synth_opts.last_branch_sz;

3376 3377
	pt->use_thread_stack = pt->synth_opts.callchain ||
			       pt->synth_opts.add_callchain ||
3378 3379 3380 3381 3382 3383
			       pt->synth_opts.thread_stack ||
			       pt->synth_opts.last_branch;

	pt->callstack = pt->synth_opts.callchain ||
			pt->synth_opts.add_callchain ||
			pt->synth_opts.thread_stack;
3384

A
Adrian Hunter 已提交
3385 3386 3387 3388
	err = intel_pt_synth_events(pt, session);
	if (err)
		goto err_delete_thread;

3389 3390
	intel_pt_setup_pebs_events(pt);

3391 3392 3393 3394
	if (pt->sampling_mode || list_empty(&session->auxtrace_index))
		err = auxtrace_queue_data(session, true, true);
	else
		err = auxtrace_queues__process_index(&pt->queues, session);
A
Adrian Hunter 已提交
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
	if (err)
		goto err_delete_thread;

	if (pt->queues.populated)
		pt->data_queued = true;

	if (pt->timeless_decoding)
		pr_debug2("Intel PT decoding without timestamps\n");

	return 0;

err_delete_thread:
3407
	zfree(&pt->chain);
3408
	thread__zput(pt->unknown_thread);
A
Adrian Hunter 已提交
3409 3410 3411 3412 3413
err_free_queues:
	intel_pt_log_disable();
	auxtrace_queues__free(&pt->queues);
	session->auxtrace = NULL;
err_free:
3414
	addr_filters__exit(&pt->filts);
3415
	zfree(&pt->filter);
3416
	zfree(&pt->time_ranges);
A
Adrian Hunter 已提交
3417 3418 3419
	free(pt);
	return err;
}