ds.c 42.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
#include <linux/bitops.h>
#include <linux/types.h>
#include <linux/slab.h>
5

6
#include <asm/cpu_entry_area.h>
7
#include <asm/perf_event.h>
8
#include <asm/insn.h>
9

10
#include "../perf_event.h"
11

12 13 14
/* Waste a full page so it can be mapped into the cpu_entry_area */
DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);

15 16 17
/* The size of a BTS record in bytes: */
#define BTS_RECORD_SIZE		24

18
#define PEBS_FIXUP_SIZE		PAGE_SIZE
19 20 21 22 23 24 25 26 27 28 29 30

/*
 * pebs_record_32 for p4 and core not supported

struct pebs_record_32 {
	u32 flags, ip;
	u32 ax, bc, cx, dx;
	u32 si, di, bp, sp;
};

 */

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
union intel_x86_pebs_dse {
	u64 val;
	struct {
		unsigned int ld_dse:4;
		unsigned int ld_stlb_miss:1;
		unsigned int ld_locked:1;
		unsigned int ld_reserved:26;
	};
	struct {
		unsigned int st_l1d_hit:1;
		unsigned int st_reserved1:3;
		unsigned int st_stlb_miss:1;
		unsigned int st_locked:1;
		unsigned int st_reserved2:26;
	};
};


/*
 * Map PEBS Load Latency Data Source encodings to generic
 * memory data source information
 */
#define P(a, b) PERF_MEM_S(a, b)
#define OP_LH (P(OP, LOAD) | P(LVL, HIT))
55 56
#define LEVEL(x) P(LVLNUM, x)
#define REM P(REMOTE, REMOTE)
57 58
#define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))

59 60
/* Version for Sandy Bridge and later */
static u64 pebs_data_source[] = {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
	OP_LH | P(LVL, L1)  | LEVEL(L1) | P(SNOOP, NONE),  /* 0x01: L1 local */
	OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
	OP_LH | P(LVL, L2)  | LEVEL(L2) | P(SNOOP, NONE),  /* 0x03: L2 hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, NONE),  /* 0x04: L3 hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, MISS),  /* 0x05: L3 hit, snoop miss */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HIT),   /* 0x06: L3 hit, snoop hit */
	OP_LH | P(LVL, L3)  | LEVEL(L3) | P(SNOOP, HITM),  /* 0x07: L3 hit, snoop hitm */
	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x08: L3 miss snoop hit */
	OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | P(SNOOP, HIT),       /* 0x0a: L3 miss, shared */
	OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT),  /* 0x0b: L3 miss, shared */
	OP_LH | P(LVL, LOC_RAM)  | LEVEL(RAM) | SNOOP_NONE_MISS,     /* 0x0c: L3 miss, excl */
	OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
	OP_LH | P(LVL, IO)  | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
	OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
77 78
};

79 80 81
/* Patch up minor differences in the bits */
void __init intel_pmu_pebs_data_source_nhm(void)
{
82 83 84 85 86 87 88 89 90 91 92 93 94 95
	pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
	pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
	pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
}

void __init intel_pmu_pebs_data_source_skl(bool pmem)
{
	u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);

	pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
	pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
	pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
	pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
	pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static u64 precise_store_data(u64 status)
{
	union intel_x86_pebs_dse dse;
	u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);

	dse.val = status;

	/*
	 * bit 4: TLB access
	 * 1 = stored missed 2nd level TLB
	 *
	 * so it either hit the walker or the OS
	 * otherwise hit 2nd level TLB
	 */
	if (dse.st_stlb_miss)
		val |= P(TLB, MISS);
	else
		val |= P(TLB, HIT);

	/*
	 * bit 0: hit L1 data cache
	 * if not set, then all we know is that
	 * it missed L1D
	 */
	if (dse.st_l1d_hit)
		val |= P(LVL, HIT);
	else
		val |= P(LVL, MISS);

	/*
	 * bit 5: Locked prefix
	 */
	if (dse.st_locked)
		val |= P(LOCK, LOCKED);

	return val;
}

136
static u64 precise_datala_hsw(struct perf_event *event, u64 status)
137 138 139
{
	union perf_mem_data_src dse;

140 141 142 143 144 145
	dse.val = PERF_MEM_NA;

	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
		dse.mem_op = PERF_MEM_OP_STORE;
	else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
		dse.mem_op = PERF_MEM_OP_LOAD;
146 147 148 149 150 151 152 153 154

	/*
	 * L1 info only valid for following events:
	 *
	 * MEM_UOPS_RETIRED.STLB_MISS_STORES
	 * MEM_UOPS_RETIRED.LOCK_STORES
	 * MEM_UOPS_RETIRED.SPLIT_STORES
	 * MEM_UOPS_RETIRED.ALL_STORES
	 */
155 156 157 158 159 160
	if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
		if (status & 1)
			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
		else
			dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
	}
161 162 163
	return dse.val;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static u64 load_latency_data(u64 status)
{
	union intel_x86_pebs_dse dse;
	u64 val;

	dse.val = status;

	/*
	 * use the mapping table for bit 0-3
	 */
	val = pebs_data_source[dse.ld_dse];

	/*
	 * Nehalem models do not support TLB, Lock infos
	 */
179
	if (x86_pmu.pebs_no_tlb) {
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		val |= P(TLB, NA) | P(LOCK, NA);
		return val;
	}
	/*
	 * bit 4: TLB access
	 * 0 = did not miss 2nd level TLB
	 * 1 = missed 2nd level TLB
	 */
	if (dse.ld_stlb_miss)
		val |= P(TLB, MISS) | P(TLB, L2);
	else
		val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);

	/*
	 * bit 5: locked prefix
	 */
	if (dse.ld_locked)
		val |= P(LOCK, LOCKED);

	return val;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
struct pebs_record_core {
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
};

struct pebs_record_nhm {
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
	u64 status, dla, dse, lat;
};

219 220 221 222
/*
 * Same as pebs_record_nhm, with two additional fields.
 */
struct pebs_record_hsw {
223 224 225 226 227 228
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
	u64 status, dla, dse, lat;
229
	u64 real_ip, tsx_tuning;
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
};

union hsw_tsx_tuning {
	struct {
		u32 cycles_last_block     : 32,
		    hle_abort		  : 1,
		    rtm_abort		  : 1,
		    instruction_abort     : 1,
		    non_instruction_abort : 1,
		    retry		  : 1,
		    data_conflict	  : 1,
		    capacity_writes	  : 1,
		    capacity_reads	  : 1;
	};
	u64	    value;
245 246
};

247 248
#define PEBS_HSW_TSX_FLAGS	0xff00000000ULL

249 250 251 252 253 254 255 256 257 258 259 260 261
/* Same as HSW, plus TSC */

struct pebs_record_skl {
	u64 flags, ip;
	u64 ax, bx, cx, dx;
	u64 si, di, bp, sp;
	u64 r8,  r9,  r10, r11;
	u64 r12, r13, r14, r15;
	u64 status, dla, dse, lat;
	u64 real_ip, tsx_tuning;
	u64 tsc;
};

262
void init_debug_store_on_cpu(int cpu)
263 264 265 266 267 268 269 270 271 272 273
{
	struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;

	if (!ds)
		return;

	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
		     (u32)((u64)(unsigned long)ds),
		     (u32)((u64)(unsigned long)ds >> 32));
}

274
void fini_debug_store_on_cpu(int cpu)
275 276 277 278 279 280 281
{
	if (!per_cpu(cpu_hw_events, cpu).ds)
		return;

	wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
}

282 283
static DEFINE_PER_CPU(void *, insn_buffer);

284
static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
285
{
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	phys_addr_t pa;
	size_t msz = 0;

	pa = virt_to_phys(addr);
	for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
		cea_set_pte(cea, pa, prot);
}

static void ds_clear_cea(void *cea, size_t size)
{
	size_t msz = 0;

	for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
		cea_set_pte(cea, 0, PAGE_NONE);
}

static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
{
	unsigned int order = get_order(size);
305
	int node = cpu_to_node(cpu);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	struct page *page;

	page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
	return page ? page_address(page) : NULL;
}

static void dsfree_pages(const void *buffer, size_t size)
{
	if (buffer)
		free_pages((unsigned long)buffer, get_order(size));
}

static int alloc_pebs_buffer(int cpu)
{
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	size_t bsiz = x86_pmu.pebs_buffer_size;
	int max, node = cpu_to_node(cpu);
	void *buffer, *ibuffer, *cea;
325 326 327 328

	if (!x86_pmu.pebs)
		return 0;

329
	buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
330 331 332
	if (unlikely(!buffer))
		return -ENOMEM;

333 334 335 336 337 338 339
	/*
	 * HSW+ already provides us the eventing ip; no need to allocate this
	 * buffer then.
	 */
	if (x86_pmu.intel_cap.pebs_format < 2) {
		ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
		if (!ibuffer) {
340
			dsfree_pages(buffer, bsiz);
341 342 343 344
			return -ENOMEM;
		}
		per_cpu(insn_buffer, cpu) = ibuffer;
	}
345 346 347 348 349
	hwev->ds_pebs_vaddr = buffer;
	/* Update the cpu entry area mapping */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
	ds->pebs_buffer_base = (unsigned long) cea;
	ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
350
	ds->pebs_index = ds->pebs_buffer_base;
351 352
	max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
	ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
353 354 355
	return 0;
}

356 357
static void release_pebs_buffer(int cpu)
{
358 359 360
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	void *cea;
361 362 363 364

	if (!ds || !x86_pmu.pebs)
		return;

365 366 367
	kfree(per_cpu(insn_buffer, cpu));
	per_cpu(insn_buffer, cpu) = NULL;

368 369 370
	/* Clear the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
	ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
371
	ds->pebs_buffer_base = 0;
372 373
	dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
	hwev->ds_pebs_vaddr = NULL;
374 375
}

376 377
static int alloc_bts_buffer(int cpu)
{
378 379 380 381
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	void *buffer, *cea;
	int max;
382 383 384 385

	if (!x86_pmu.bts)
		return 0;

386
	buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
387 388
	if (unlikely(!buffer)) {
		WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
389
		return -ENOMEM;
390
	}
391 392 393 394 395
	hwev->ds_bts_vaddr = buffer;
	/* Update the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
	ds->bts_buffer_base = (unsigned long) cea;
	ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
396
	ds->bts_index = ds->bts_buffer_base;
397 398 399
	max = BTS_RECORD_SIZE * (BTS_BUFFER_SIZE / BTS_RECORD_SIZE);
	ds->bts_absolute_maximum = ds->bts_buffer_base + max;
	ds->bts_interrupt_threshold = ds->bts_absolute_maximum - (max / 16);
400 401 402
	return 0;
}

403 404
static void release_bts_buffer(int cpu)
{
405 406 407
	struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
	struct debug_store *ds = hwev->ds;
	void *cea;
408 409 410 411

	if (!ds || !x86_pmu.bts)
		return;

412 413 414
	/* Clear the fixmap */
	cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
	ds_clear_cea(cea, BTS_BUFFER_SIZE);
415
	ds->bts_buffer_base = 0;
416 417
	dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
	hwev->ds_bts_vaddr = NULL;
418 419
}

420 421
static int alloc_ds_buffer(int cpu)
{
422
	struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
423

424
	memset(ds, 0, sizeof(*ds));
425 426 427 428 429 430 431 432 433
	per_cpu(cpu_hw_events, cpu).ds = ds;
	return 0;
}

static void release_ds_buffer(int cpu)
{
	per_cpu(cpu_hw_events, cpu).ds = NULL;
}

434
void release_ds_buffers(void)
435 436 437 438 439 440 441 442 443 444 445
{
	int cpu;

	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

	get_online_cpus();
	for_each_online_cpu(cpu)
		fini_debug_store_on_cpu(cpu);

	for_each_possible_cpu(cpu) {
446 447
		release_pebs_buffer(cpu);
		release_bts_buffer(cpu);
448
		release_ds_buffer(cpu);
449 450 451 452
	}
	put_online_cpus();
}

453
void reserve_ds_buffers(void)
454
{
455 456 457 458 459
	int bts_err = 0, pebs_err = 0;
	int cpu;

	x86_pmu.bts_active = 0;
	x86_pmu.pebs_active = 0;
460 461

	if (!x86_pmu.bts && !x86_pmu.pebs)
462
		return;
463

464 465 466 467 468 469
	if (!x86_pmu.bts)
		bts_err = 1;

	if (!x86_pmu.pebs)
		pebs_err = 1;

470 471 472
	get_online_cpus();

	for_each_possible_cpu(cpu) {
473 474 475 476
		if (alloc_ds_buffer(cpu)) {
			bts_err = 1;
			pebs_err = 1;
		}
477

478 479 480 481 482
		if (!bts_err && alloc_bts_buffer(cpu))
			bts_err = 1;

		if (!pebs_err && alloc_pebs_buffer(cpu))
			pebs_err = 1;
483

484
		if (bts_err && pebs_err)
485
			break;
486 487 488 489 490 491
	}

	if (bts_err) {
		for_each_possible_cpu(cpu)
			release_bts_buffer(cpu);
	}
492

493 494 495
	if (pebs_err) {
		for_each_possible_cpu(cpu)
			release_pebs_buffer(cpu);
496 497
	}

498 499 500 501 502 503 504 505 506 507
	if (bts_err && pebs_err) {
		for_each_possible_cpu(cpu)
			release_ds_buffer(cpu);
	} else {
		if (x86_pmu.bts && !bts_err)
			x86_pmu.bts_active = 1;

		if (x86_pmu.pebs && !pebs_err)
			x86_pmu.pebs_active = 1;

508 509 510 511 512 513 514 515 516 517 518
		for_each_online_cpu(cpu)
			init_debug_store_on_cpu(cpu);
	}

	put_online_cpus();
}

/*
 * BTS
 */

519
struct event_constraint bts_constraint =
520
	EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
521

522
void intel_pmu_enable_bts(u64 config)
523 524 525 526 527
{
	unsigned long debugctlmsr;

	debugctlmsr = get_debugctlmsr();

528 529
	debugctlmsr |= DEBUGCTLMSR_TR;
	debugctlmsr |= DEBUGCTLMSR_BTS;
530 531
	if (config & ARCH_PERFMON_EVENTSEL_INT)
		debugctlmsr |= DEBUGCTLMSR_BTINT;
532 533

	if (!(config & ARCH_PERFMON_EVENTSEL_OS))
534
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
535 536

	if (!(config & ARCH_PERFMON_EVENTSEL_USR))
537
		debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
538 539 540 541

	update_debugctlmsr(debugctlmsr);
}

542
void intel_pmu_disable_bts(void)
543
{
544
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
545 546 547 548 549 550 551 552
	unsigned long debugctlmsr;

	if (!cpuc->ds)
		return;

	debugctlmsr = get_debugctlmsr();

	debugctlmsr &=
553 554
		~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
		  DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
555 556 557 558

	update_debugctlmsr(debugctlmsr);
}

559
int intel_pmu_drain_bts_buffer(void)
560
{
561
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
562 563 564 565 566 567
	struct debug_store *ds = cpuc->ds;
	struct bts_record {
		u64	from;
		u64	to;
		u64	flags;
	};
568
	struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
569
	struct bts_record *at, *base, *top;
570 571 572
	struct perf_output_handle handle;
	struct perf_event_header header;
	struct perf_sample_data data;
573
	unsigned long skip = 0;
574 575 576
	struct pt_regs regs;

	if (!event)
577
		return 0;
578

579
	if (!x86_pmu.bts_active)
580
		return 0;
581

582 583
	base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
	top  = (struct bts_record *)(unsigned long)ds->bts_index;
584

585
	if (top <= base)
586
		return 0;
587

588 589
	memset(&regs, 0, sizeof(regs));

590 591
	ds->bts_index = ds->bts_buffer_base;

592
	perf_sample_data_init(&data, 0, event->hw.last_period);
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	/*
	 * BTS leaks kernel addresses in branches across the cpl boundary,
	 * such as traps or system calls, so unless the user is asking for
	 * kernel tracing (and right now it's not possible), we'd need to
	 * filter them out. But first we need to count how many of those we
	 * have in the current batch. This is an extra O(n) pass, however,
	 * it's much faster than the other one especially considering that
	 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
	 * alloc_bts_buffer()).
	 */
	for (at = base; at < top; at++) {
		/*
		 * Note that right now *this* BTS code only works if
		 * attr::exclude_kernel is set, but let's keep this extra
		 * check here in case that changes.
		 */
		if (event->attr.exclude_kernel &&
		    (kernel_ip(at->from) || kernel_ip(at->to)))
			skip++;
	}

615 616 617 618 619
	/*
	 * Prepare a generic sample, i.e. fill in the invariant fields.
	 * We will overwrite the from and to address before we output
	 * the sample.
	 */
P
Peter Zijlstra 已提交
620
	rcu_read_lock();
621 622
	perf_prepare_sample(&header, &data, event, &regs);

623 624
	if (perf_output_begin(&handle, event, header.size *
			      (top - base - skip)))
P
Peter Zijlstra 已提交
625
		goto unlock;
626

627 628 629 630 631 632
	for (at = base; at < top; at++) {
		/* Filter out any records that contain kernel addresses. */
		if (event->attr.exclude_kernel &&
		    (kernel_ip(at->from) || kernel_ip(at->to)))
			continue;

633 634 635 636 637 638 639 640 641 642 643
		data.ip		= at->from;
		data.addr	= at->to;

		perf_output_sample(&handle, &header, &data, event);
	}

	perf_output_end(&handle);

	/* There's new data available. */
	event->hw.interrupts++;
	event->pending_kill = POLL_IN;
P
Peter Zijlstra 已提交
644 645
unlock:
	rcu_read_unlock();
646
	return 1;
647 648
}

649 650 651 652 653 654 655
static inline void intel_pmu_drain_pebs_buffer(void)
{
	struct pt_regs regs;

	x86_pmu.drain_pebs(&regs);
}

656 657 658
/*
 * PEBS
 */
659
struct event_constraint intel_core2_pebs_event_constraints[] = {
660 661 662 663 664
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
665 666
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
667 668 669
	EVENT_CONSTRAINT_END
};

670
struct event_constraint intel_atom_pebs_event_constraints[] = {
671 672 673
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1),    /* MEM_LOAD_RETIRED.* */
674 675
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
676 677
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
678 679 680
	EVENT_CONSTRAINT_END
};

681
struct event_constraint intel_slm_pebs_event_constraints[] = {
682 683
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
684 685
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
686 687 688
	EVENT_CONSTRAINT_END
};

689 690 691 692 693 694
struct event_constraint intel_glm_pebs_event_constraints[] = {
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
	EVENT_CONSTRAINT_END
};

695 696 697 698 699 700
struct event_constraint intel_glp_pebs_event_constraints[] = {
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
	EVENT_CONSTRAINT_END
};

701
struct event_constraint intel_nehalem_pebs_event_constraints[] = {
702
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
703 704 705
	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INST_RETIRED.ANY */
706
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
707 708 709 710 711 712
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
713 714
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
715 716 717
	EVENT_CONSTRAINT_END
};

718
struct event_constraint intel_westmere_pebs_event_constraints[] = {
719
	INTEL_PLD_CONSTRAINT(0x100b, 0xf),      /* MEM_INST_RETIRED.* */
720 721 722
	INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf),    /* MEM_UNCORE_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf),    /* INSTR_RETIRED.* */
723
	INTEL_EVENT_CONSTRAINT(0xc2, 0xf),    /* UOPS_RETIRED.* */
724 725 726 727 728 729
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf),    /* BR_MISP_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf),    /* SSEX_UOPS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf),    /* FP_ASSIST.* */
730 731
	/* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
732 733 734
	EVENT_CONSTRAINT_END
};

735
struct event_constraint intel_snb_pebs_event_constraints[] = {
736
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
737
	INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
738
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
739 740
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
741 742 743 744
        INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
        INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
745 746
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
747 748 749
	EVENT_CONSTRAINT_END
};

750
struct event_constraint intel_ivb_pebs_event_constraints[] = {
751
        INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
752
        INTEL_PLD_CONSTRAINT(0x01cd, 0x8),    /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
753
	INTEL_PST_CONSTRAINT(0x02cd, 0x8),    /* MEM_TRANS_RETIRED.PRECISE_STORES */
754 755
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
756 757
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
758 759 760 761
	INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf),    /* MEM_UOP_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf),    /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
	INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf),    /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
762 763
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
764 765 766
        EVENT_CONSTRAINT_END
};

767
struct event_constraint intel_hsw_pebs_event_constraints[] = {
768
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
769 770 771
	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
772 773
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
774
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
775 776 777 778 779 780 781 782 783 784
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
785 786 787 788 789
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
	EVENT_CONSTRAINT_END
};

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
struct event_constraint intel_bdw_pebs_event_constraints[] = {
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
	INTEL_PLD_CONSTRAINT(0x01cd, 0xf),    /* MEM_TRANS_RETIRED.* */
	/* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_UOPS_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
	EVENT_CONSTRAINT_END
};


814 815
struct event_constraint intel_skl_pebs_event_constraints[] = {
	INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2),	/* INST_RETIRED.PREC_DIST */
816 817
	/* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
818 819
	/* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
	INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
820 821 822 823 824 825 826 827 828 829 830 831
	INTEL_PLD_CONSTRAINT(0x1cd, 0xf),		      /* MEM_TRANS_RETIRED.* */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
	INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf),    /* MEM_LOAD_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf),    /* MEM_LOAD_L3_HIT_RETIRED.* */
	INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf),    /* MEM_LOAD_L3_MISS_RETIRED.* */
832 833
	/* Allow all events as PEBS with no flags */
	INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
834 835 836
	EVENT_CONSTRAINT_END
};

837
struct event_constraint *intel_pebs_constraints(struct perf_event *event)
838 839 840
{
	struct event_constraint *c;

P
Peter Zijlstra 已提交
841
	if (!event->attr.precise_ip)
842 843 844 845
		return NULL;

	if (x86_pmu.pebs_constraints) {
		for_each_event_constraint(c, x86_pmu.pebs_constraints) {
846 847
			if ((event->hw.config & c->cmask) == c->code) {
				event->hw.flags |= c->flags;
848
				return c;
849
			}
850 851 852 853 854 855
		}
	}

	return &emptyconstraint;
}

856 857 858 859 860 861 862 863 864 865
/*
 * We need the sched_task callback even for per-cpu events when we use
 * the large interrupt threshold, such that we can provide PID and TID
 * to PEBS samples.
 */
static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
{
	return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
}

866 867 868 869 870 871 872 873
void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);

	if (!sched_in && pebs_needs_sched_cb(cpuc))
		intel_pmu_drain_pebs_buffer();
}

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
{
	struct debug_store *ds = cpuc->ds;
	u64 threshold;

	if (cpuc->n_pebs == cpuc->n_large_pebs) {
		threshold = ds->pebs_absolute_maximum -
			x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
	} else {
		threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
	}

	ds->pebs_interrupt_threshold = threshold;
}

static void
pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu)
{
892 893 894 895 896 897 898
	/*
	 * Make sure we get updated with the first PEBS
	 * event. It will trigger also during removal, but
	 * that does not hurt:
	 */
	bool update = cpuc->n_pebs == 1;

899 900 901 902 903 904
	if (needed_cb != pebs_needs_sched_cb(cpuc)) {
		if (!needed_cb)
			perf_sched_cb_inc(pmu);
		else
			perf_sched_cb_dec(pmu);

905
		update = true;
906
	}
907 908 909

	if (update)
		pebs_update_threshold(cpuc);
910 911
}

912
void intel_pmu_pebs_add(struct perf_event *event)
913
{
914 915 916 917 918 919 920 921 922
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	struct hw_perf_event *hwc = &event->hw;
	bool needed_cb = pebs_needs_sched_cb(cpuc);

	cpuc->n_pebs++;
	if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
		cpuc->n_large_pebs++;

	pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
923 924
}

925
void intel_pmu_pebs_enable(struct perf_event *event)
926
{
927
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
928
	struct hw_perf_event *hwc = &event->hw;
929
	struct debug_store *ds = cpuc->ds;
930

931 932
	hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;

933
	cpuc->pebs_enabled |= 1ULL << hwc->idx;
934 935 936

	if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
		cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
937 938
	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
		cpuc->pebs_enabled |= 1ULL << 63;
939

940
	/*
941 942
	 * Use auto-reload if possible to save a MSR write in the PMI.
	 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
943
	 */
944 945 946
	if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
		ds->pebs_event_reset[hwc->idx] =
			(u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
947 948
	} else {
		ds->pebs_event_reset[hwc->idx] = 0;
949
	}
950 951
}

952
void intel_pmu_pebs_del(struct perf_event *event)
953 954 955 956 957 958 959 960
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	struct hw_perf_event *hwc = &event->hw;
	bool needed_cb = pebs_needs_sched_cb(cpuc);

	cpuc->n_pebs--;
	if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
		cpuc->n_large_pebs--;
961

962
	pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
963 964
}

965
void intel_pmu_pebs_disable(struct perf_event *event)
966
{
967
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
968
	struct hw_perf_event *hwc = &event->hw;
969

970
	if (cpuc->n_pebs == cpuc->n_large_pebs)
971
		intel_pmu_drain_pebs_buffer();
972

973
	cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
974

975
	if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
976
		cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
977
	else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
978 979
		cpuc->pebs_enabled &= ~(1ULL << 63);

980
	if (cpuc->enabled)
981
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
982 983 984 985

	hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
}

986
void intel_pmu_pebs_enable_all(void)
987
{
988
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
989 990 991 992 993

	if (cpuc->pebs_enabled)
		wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
}

994
void intel_pmu_pebs_disable_all(void)
995
{
996
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
997 998 999 1000 1001

	if (cpuc->pebs_enabled)
		wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
}

1002 1003
static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
{
1004
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1005 1006 1007
	unsigned long from = cpuc->lbr_entries[0].from;
	unsigned long old_to, to = cpuc->lbr_entries[0].to;
	unsigned long ip = regs->ip;
1008
	int is_64bit = 0;
1009
	void *kaddr;
1010
	int size;
1011

1012 1013 1014 1015 1016 1017
	/*
	 * We don't need to fixup if the PEBS assist is fault like
	 */
	if (!x86_pmu.intel_cap.pebs_trap)
		return 1;

P
Peter Zijlstra 已提交
1018 1019 1020
	/*
	 * No LBR entry, no basic block, no rewinding
	 */
1021 1022 1023
	if (!cpuc->lbr_stack.nr || !from || !to)
		return 0;

P
Peter Zijlstra 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	/*
	 * Basic blocks should never cross user/kernel boundaries
	 */
	if (kernel_ip(ip) != kernel_ip(to))
		return 0;

	/*
	 * unsigned math, either ip is before the start (impossible) or
	 * the basic block is larger than 1 page (sanity)
	 */
1034
	if ((ip - to) > PEBS_FIXUP_SIZE)
1035 1036 1037 1038 1039 1040
		return 0;

	/*
	 * We sampled a branch insn, rewind using the LBR stack
	 */
	if (ip == to) {
1041
		set_linear_ip(regs, from);
1042 1043 1044
		return 1;
	}

1045
	size = ip - to;
1046
	if (!kernel_ip(ip)) {
1047
		int bytes;
1048 1049
		u8 *buf = this_cpu_read(insn_buffer);

1050
		/* 'size' must fit our buffer, see above */
1051
		bytes = copy_from_user_nmi(buf, (void __user *)to, size);
1052
		if (bytes != 0)
1053 1054 1055 1056 1057 1058 1059
			return 0;

		kaddr = buf;
	} else {
		kaddr = (void *)to;
	}

1060 1061 1062 1063 1064
	do {
		struct insn insn;

		old_to = to;

1065 1066 1067
#ifdef CONFIG_X86_64
		is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
#endif
1068
		insn_init(&insn, kaddr, size, is_64bit);
1069
		insn_get_length(&insn);
1070 1071 1072 1073 1074 1075 1076 1077
		/*
		 * Make sure there was not a problem decoding the
		 * instruction and getting the length.  This is
		 * doubly important because we have an infinite
		 * loop if insn.length=0.
		 */
		if (!insn.length)
			break;
1078

1079
		to += insn.length;
1080
		kaddr += insn.length;
1081
		size -= insn.length;
1082 1083 1084
	} while (to < ip);

	if (to == ip) {
1085
		set_linear_ip(regs, old_to);
1086 1087 1088
		return 1;
	}

P
Peter Zijlstra 已提交
1089 1090 1091 1092
	/*
	 * Even though we decoded the basic block, the instruction stream
	 * never matched the given IP, either the TO or the IP got corrupted.
	 */
1093 1094 1095
	return 0;
}

1096
static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
1097 1098 1099 1100 1101 1102 1103 1104
{
	if (pebs->tsx_tuning) {
		union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
		return tsx.cycles_last_block;
	}
	return 0;
}

1105
static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
1106 1107 1108 1109 1110 1111 1112 1113 1114
{
	u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;

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

1115 1116 1117 1118
static void setup_pebs_sample_data(struct perf_event *event,
				   struct pt_regs *iregs, void *__pebs,
				   struct perf_sample_data *data,
				   struct pt_regs *regs)
1119
{
1120 1121 1122 1123
#define PERF_X86_EVENT_PEBS_HSW_PREC \
		(PERF_X86_EVENT_PEBS_ST_HSW | \
		 PERF_X86_EVENT_PEBS_LD_HSW | \
		 PERF_X86_EVENT_PEBS_NA_HSW)
1124
	/*
1125 1126
	 * We cast to the biggest pebs_record but are careful not to
	 * unconditionally access the 'extra' entries.
1127
	 */
1128
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1129
	struct pebs_record_skl *pebs = __pebs;
1130
	u64 sample_type;
1131 1132
	int fll, fst, dsrc;
	int fl = event->hw.flags;
1133

1134 1135 1136
	if (pebs == NULL)
		return;

1137 1138 1139 1140 1141
	sample_type = event->attr.sample_type;
	dsrc = sample_type & PERF_SAMPLE_DATA_SRC;

	fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
	fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
1142

1143
	perf_sample_data_init(data, 0, event->hw.last_period);
1144

1145
	data->period = event->hw.last_period;
1146 1147

	/*
1148
	 * Use latency for weight (only avail with PEBS-LL)
1149
	 */
1150
	if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
1151
		data->weight = pebs->lat;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

	/*
	 * data.data_src encodes the data source
	 */
	if (dsrc) {
		u64 val = PERF_MEM_NA;
		if (fll)
			val = load_latency_data(pebs->dse);
		else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
			val = precise_datala_hsw(event, pebs->dse);
		else if (fst)
			val = precise_store_data(pebs->dse);
1164
		data->data_src.val = val;
1165 1166
	}

1167
	/*
1168 1169 1170
	 * We use the interrupt regs as a base because the PEBS record does not
	 * contain a full regs set, specifically it seems to lack segment
	 * descriptors, which get used by things like user_mode().
1171
	 *
1172 1173 1174 1175 1176 1177
	 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
	 *
	 * We must however always use BP,SP from iregs for the unwinder to stay
	 * sane; the record BP,SP can point into thin air when the record is
	 * from a previous PMI context or an (I)RET happend between the record
	 * and PMI.
1178
	 */
1179 1180 1181
	*regs = *iregs;
	regs->flags = pebs->flags;
	set_linear_ip(regs, pebs->ip);
1182

1183
	if (sample_type & PERF_SAMPLE_REGS_INTR) {
1184 1185 1186 1187 1188 1189 1190
		regs->ax = pebs->ax;
		regs->bx = pebs->bx;
		regs->cx = pebs->cx;
		regs->dx = pebs->dx;
		regs->si = pebs->si;
		regs->di = pebs->di;

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		/*
		 * Per the above; only set BP,SP if we don't need callchains.
		 *
		 * XXX: does this make sense?
		 */
		if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
			regs->bp = pebs->bp;
			regs->sp = pebs->sp;
		}

		/*
		 * Preserve PERF_EFLAGS_VM from set_linear_ip().
		 */
		regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM);
1205
#ifndef CONFIG_X86_32
1206 1207 1208 1209 1210 1211 1212 1213
		regs->r8 = pebs->r8;
		regs->r9 = pebs->r9;
		regs->r10 = pebs->r10;
		regs->r11 = pebs->r11;
		regs->r12 = pebs->r12;
		regs->r13 = pebs->r13;
		regs->r14 = pebs->r14;
		regs->r15 = pebs->r15;
1214 1215 1216
#endif
	}

1217
	if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
1218 1219 1220 1221
		regs->ip = pebs->real_ip;
		regs->flags |= PERF_EFLAGS_EXACT;
	} else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
		regs->flags |= PERF_EFLAGS_EXACT;
1222
	else
1223
		regs->flags &= ~PERF_EFLAGS_EXACT;
1224

1225
	if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
1226
	    x86_pmu.intel_cap.pebs_format >= 1)
1227
		data->addr = pebs->dla;
1228

1229 1230
	if (x86_pmu.intel_cap.pebs_format >= 2) {
		/* Only set the TSX weight when no memory weight. */
1231
		if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
1232
			data->weight = intel_hsw_weight(pebs);
1233

1234
		if (sample_type & PERF_SAMPLE_TRANSACTION)
1235
			data->txn = intel_hsw_transaction(pebs);
1236
	}
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	/*
	 * v3 supplies an accurate time stamp, so we use that
	 * for the time stamp.
	 *
	 * We can only do this for the default trace clock.
	 */
	if (x86_pmu.intel_cap.pebs_format >= 3 &&
		event->attr.use_clockid == 0)
		data->time = native_sched_clock_from_tsc(pebs->tsc);

1248
	if (has_branch_stack(event))
1249 1250 1251
		data->br_stack = &cpuc->lbr_stack;
}

1252 1253 1254 1255 1256 1257 1258
static inline void *
get_next_pebs_record_by_bit(void *base, void *top, int bit)
{
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
	void *at;
	u64 pebs_status;

1259 1260 1261 1262 1263 1264 1265
	/*
	 * fmt0 does not have a status bitfield (does not use
	 * perf_record_nhm format)
	 */
	if (x86_pmu.intel_cap.pebs_format < 1)
		return base;

1266 1267 1268 1269 1270 1271 1272
	if (base == NULL)
		return NULL;

	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
		struct pebs_record_nhm *p = at;

		if (test_bit(bit, (unsigned long *)&p->status)) {
1273 1274 1275
			/* PEBS v3 has accurate status bits */
			if (x86_pmu.intel_cap.pebs_format >= 3)
				return at;
1276 1277 1278 1279 1280 1281

			if (p->status == (1 << bit))
				return at;

			/* clear non-PEBS bit and re-check */
			pebs_status = p->status & cpuc->pebs_enabled;
1282
			pebs_status &= PEBS_COUNTER_MASK;
1283 1284 1285 1286 1287 1288 1289
			if (pebs_status == (1 << bit))
				return at;
		}
	}
	return NULL;
}

1290
static void __intel_pmu_pebs_event(struct perf_event *event,
1291 1292 1293
				   struct pt_regs *iregs,
				   void *base, void *top,
				   int bit, int count)
1294 1295 1296
{
	struct perf_sample_data data;
	struct pt_regs regs;
1297
	void *at = get_next_pebs_record_by_bit(base, top, bit);
1298

1299 1300
	if (!intel_pmu_save_and_restart(event) &&
	    !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
1301 1302
		return;

1303 1304 1305 1306 1307 1308
	while (count > 1) {
		setup_pebs_sample_data(event, iregs, at, &data, &regs);
		perf_event_output(event, &data, &regs);
		at += x86_pmu.pebs_record_size;
		at = get_next_pebs_record_by_bit(at, top, bit);
		count--;
1309 1310 1311
	}

	setup_pebs_sample_data(event, iregs, at, &data, &regs);
1312

1313 1314 1315 1316 1317
	/*
	 * All but the last records are processed.
	 * The last one is left to be able to call the overflow handler.
	 */
	if (perf_event_overflow(event, &data, &regs)) {
P
Peter Zijlstra 已提交
1318
		x86_pmu_stop(event, 0);
1319 1320 1321
		return;
	}

1322 1323
}

1324 1325
static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
{
1326
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1327 1328 1329 1330 1331
	struct debug_store *ds = cpuc->ds;
	struct perf_event *event = cpuc->events[0]; /* PMC0 only */
	struct pebs_record_core *at, *top;
	int n;

1332
	if (!x86_pmu.pebs_active)
1333 1334 1335 1336 1337
		return;

	at  = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
	top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;

1338 1339 1340 1341 1342 1343
	/*
	 * Whatever else happens, drain the thing
	 */
	ds->pebs_index = ds->pebs_buffer_base;

	if (!test_bit(0, cpuc->active_mask))
P
Peter Zijlstra 已提交
1344
		return;
1345

1346 1347
	WARN_ON_ONCE(!event);

P
Peter Zijlstra 已提交
1348
	if (!event->attr.precise_ip)
1349 1350
		return;

1351
	n = top - at;
1352 1353
	if (n <= 0)
		return;
1354

1355
	__intel_pmu_pebs_event(event, iregs, at, top, 0, n);
1356 1357
}

1358
static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
1359
{
1360
	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1361
	struct debug_store *ds = cpuc->ds;
1362 1363 1364
	struct perf_event *event;
	void *base, *at, *top;
	short counts[MAX_PEBS_EVENTS] = {};
1365
	short error[MAX_PEBS_EVENTS] = {};
1366
	int bit, i;
1367 1368 1369 1370

	if (!x86_pmu.pebs_active)
		return;

1371
	base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
1372
	top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
1373 1374 1375

	ds->pebs_index = ds->pebs_buffer_base;

1376
	if (unlikely(base >= top))
1377 1378
		return;

1379
	for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1380
		struct pebs_record_nhm *p = at;
1381
		u64 pebs_status;
1382

1383 1384 1385 1386
		pebs_status = p->status & cpuc->pebs_enabled;
		pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;

		/* PEBS v3 has more accurate status bits */
1387
		if (x86_pmu.intel_cap.pebs_format >= 3) {
1388 1389
			for_each_set_bit(bit, (unsigned long *)&pebs_status,
					 x86_pmu.max_pebs_events)
1390 1391 1392 1393 1394
				counts[bit]++;

			continue;
		}

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
		/*
		 * On some CPUs the PEBS status can be zero when PEBS is
		 * racing with clearing of GLOBAL_STATUS.
		 *
		 * Normally we would drop that record, but in the
		 * case when there is only a single active PEBS event
		 * we can assume it's for that event.
		 */
		if (!pebs_status && cpuc->pebs_enabled &&
			!(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
			pebs_status = cpuc->pebs_enabled;

1407
		bit = find_first_bit((unsigned long *)&pebs_status,
1408
					x86_pmu.max_pebs_events);
1409
		if (bit >= x86_pmu.max_pebs_events)
1410
			continue;
1411

1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		/*
		 * The PEBS hardware does not deal well with the situation
		 * when events happen near to each other and multiple bits
		 * are set. But it should happen rarely.
		 *
		 * If these events include one PEBS and multiple non-PEBS
		 * events, it doesn't impact PEBS record. The record will
		 * be handled normally. (slow path)
		 *
		 * If these events include two or more PEBS events, the
		 * records for the events can be collapsed into a single
		 * one, and it's not possible to reconstruct all events
		 * that caused the PEBS record. It's called collision.
		 * If collision happened, the record will be dropped.
		 */
1427 1428 1429 1430 1431
		if (p->status != (1ULL << bit)) {
			for_each_set_bit(i, (unsigned long *)&pebs_status,
					 x86_pmu.max_pebs_events)
				error[i]++;
			continue;
1432
		}
1433

1434 1435
		counts[bit]++;
	}
1436

1437
	for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
1438
		if ((counts[bit] == 0) && (error[bit] == 0))
1439
			continue;
1440

1441
		event = cpuc->events[bit];
1442 1443 1444 1445 1446
		if (WARN_ON_ONCE(!event))
			continue;

		if (WARN_ON_ONCE(!event->attr.precise_ip))
			continue;
1447

1448
		/* log dropped samples number */
1449
		if (error[bit]) {
1450 1451
			perf_log_lost_samples(event, error[bit]);

1452 1453 1454 1455
			if (perf_event_account_interrupt(event))
				x86_pmu_stop(event, 0);
		}

1456 1457 1458 1459
		if (counts[bit]) {
			__intel_pmu_pebs_event(event, iregs, base,
					       top, bit, counts[bit]);
		}
1460 1461 1462 1463 1464 1465 1466
	}
}

/*
 * BTS, PEBS probe and setup
 */

1467
void __init intel_ds_init(void)
1468 1469 1470 1471 1472 1473 1474 1475 1476
{
	/*
	 * No support for 32bit formats
	 */
	if (!boot_cpu_has(X86_FEATURE_DTES64))
		return;

	x86_pmu.bts  = boot_cpu_has(X86_FEATURE_BTS);
	x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
1477
	x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
1478
	if (x86_pmu.pebs) {
1479 1480
		char pebs_type = x86_pmu.intel_cap.pebs_trap ?  '+' : '-';
		int format = x86_pmu.intel_cap.pebs_format;
1481 1482 1483

		switch (format) {
		case 0:
1484
			pr_cont("PEBS fmt0%c, ", pebs_type);
1485
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
1486 1487 1488 1489 1490 1491 1492 1493
			/*
			 * Using >PAGE_SIZE buffers makes the WRMSR to
			 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
			 * mysteriously hang on Core2.
			 *
			 * As a workaround, we don't do this.
			 */
			x86_pmu.pebs_buffer_size = PAGE_SIZE;
1494 1495 1496 1497
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
			break;

		case 1:
1498
			pr_cont("PEBS fmt1%c, ", pebs_type);
1499 1500 1501 1502
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
			break;

1503 1504 1505
		case 2:
			pr_cont("PEBS fmt2%c, ", pebs_type);
			x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
1506
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1507 1508
			break;

1509 1510 1511 1512 1513
		case 3:
			pr_cont("PEBS fmt3%c, ", pebs_type);
			x86_pmu.pebs_record_size =
						sizeof(struct pebs_record_skl);
			x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1514
			x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
1515 1516
			break;

1517
		default:
1518
			pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
1519 1520 1521 1522
			x86_pmu.pebs = 0;
		}
	}
}
1523 1524 1525

void perf_restore_debug_store(void)
{
1526 1527
	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);

1528 1529 1530
	if (!x86_pmu.bts && !x86_pmu.pebs)
		return;

1531
	wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
1532
}