mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / x86 / events / intel / ds.c
blob550b7814ef92b12b26a7f3e86ee8a4179285abda
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/types.h>
4 #include <linux/slab.h>
6 #include <asm/cpu_entry_area.h>
7 #include <asm/perf_event.h>
8 #include <asm/tlbflush.h>
9 #include <asm/insn.h>
11 #include "../perf_event.h"
13 /* Waste a full page so it can be mapped into the cpu_entry_area */
14 DEFINE_PER_CPU_PAGE_ALIGNED(struct debug_store, cpu_debug_store);
16 /* The size of a BTS record in bytes: */
17 #define BTS_RECORD_SIZE 24
19 #define PEBS_FIXUP_SIZE PAGE_SIZE
22 * pebs_record_32 for p4 and core not supported
24 struct pebs_record_32 {
25 u32 flags, ip;
26 u32 ax, bc, cx, dx;
27 u32 si, di, bp, sp;
32 union intel_x86_pebs_dse {
33 u64 val;
34 struct {
35 unsigned int ld_dse:4;
36 unsigned int ld_stlb_miss:1;
37 unsigned int ld_locked:1;
38 unsigned int ld_reserved:26;
40 struct {
41 unsigned int st_l1d_hit:1;
42 unsigned int st_reserved1:3;
43 unsigned int st_stlb_miss:1;
44 unsigned int st_locked:1;
45 unsigned int st_reserved2:26;
51 * Map PEBS Load Latency Data Source encodings to generic
52 * memory data source information
54 #define P(a, b) PERF_MEM_S(a, b)
55 #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
56 #define LEVEL(x) P(LVLNUM, x)
57 #define REM P(REMOTE, REMOTE)
58 #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
60 /* Version for Sandy Bridge and later */
61 static u64 pebs_data_source[] = {
62 P(OP, LOAD) | P(LVL, MISS) | LEVEL(L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
63 OP_LH | P(LVL, L1) | LEVEL(L1) | P(SNOOP, NONE), /* 0x01: L1 local */
64 OP_LH | P(LVL, LFB) | LEVEL(LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
65 OP_LH | P(LVL, L2) | LEVEL(L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
66 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
67 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
68 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
69 OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
70 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
71 OP_LH | P(LVL, REM_CCE1) | REM | LEVEL(L3) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
72 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
73 OP_LH | P(LVL, REM_RAM1) | REM | LEVEL(L3) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
74 OP_LH | P(LVL, LOC_RAM) | LEVEL(RAM) | SNOOP_NONE_MISS, /* 0x0c: L3 miss, excl */
75 OP_LH | P(LVL, REM_RAM1) | LEVEL(RAM) | REM | SNOOP_NONE_MISS, /* 0x0d: L3 miss, excl */
76 OP_LH | P(LVL, IO) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0e: I/O */
77 OP_LH | P(LVL, UNC) | LEVEL(NA) | P(SNOOP, NONE), /* 0x0f: uncached */
80 /* Patch up minor differences in the bits */
81 void __init intel_pmu_pebs_data_source_nhm(void)
83 pebs_data_source[0x05] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
84 pebs_data_source[0x06] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
85 pebs_data_source[0x07] = OP_LH | P(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
88 void __init intel_pmu_pebs_data_source_skl(bool pmem)
90 u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4);
92 pebs_data_source[0x08] = OP_LH | pmem_or_l4 | P(SNOOP, HIT);
93 pebs_data_source[0x09] = OP_LH | pmem_or_l4 | REM | P(SNOOP, HIT);
94 pebs_data_source[0x0b] = OP_LH | LEVEL(RAM) | REM | P(SNOOP, NONE);
95 pebs_data_source[0x0c] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOPX, FWD);
96 pebs_data_source[0x0d] = OP_LH | LEVEL(ANY_CACHE) | REM | P(SNOOP, HITM);
99 static u64 precise_store_data(u64 status)
101 union intel_x86_pebs_dse dse;
102 u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
104 dse.val = status;
107 * bit 4: TLB access
108 * 1 = stored missed 2nd level TLB
110 * so it either hit the walker or the OS
111 * otherwise hit 2nd level TLB
113 if (dse.st_stlb_miss)
114 val |= P(TLB, MISS);
115 else
116 val |= P(TLB, HIT);
119 * bit 0: hit L1 data cache
120 * if not set, then all we know is that
121 * it missed L1D
123 if (dse.st_l1d_hit)
124 val |= P(LVL, HIT);
125 else
126 val |= P(LVL, MISS);
129 * bit 5: Locked prefix
131 if (dse.st_locked)
132 val |= P(LOCK, LOCKED);
134 return val;
137 static u64 precise_datala_hsw(struct perf_event *event, u64 status)
139 union perf_mem_data_src dse;
141 dse.val = PERF_MEM_NA;
143 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
144 dse.mem_op = PERF_MEM_OP_STORE;
145 else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
146 dse.mem_op = PERF_MEM_OP_LOAD;
149 * L1 info only valid for following events:
151 * MEM_UOPS_RETIRED.STLB_MISS_STORES
152 * MEM_UOPS_RETIRED.LOCK_STORES
153 * MEM_UOPS_RETIRED.SPLIT_STORES
154 * MEM_UOPS_RETIRED.ALL_STORES
156 if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
157 if (status & 1)
158 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
159 else
160 dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
162 return dse.val;
165 static u64 load_latency_data(u64 status)
167 union intel_x86_pebs_dse dse;
168 u64 val;
170 dse.val = status;
173 * use the mapping table for bit 0-3
175 val = pebs_data_source[dse.ld_dse];
178 * Nehalem models do not support TLB, Lock infos
180 if (x86_pmu.pebs_no_tlb) {
181 val |= P(TLB, NA) | P(LOCK, NA);
182 return val;
185 * bit 4: TLB access
186 * 0 = did not miss 2nd level TLB
187 * 1 = missed 2nd level TLB
189 if (dse.ld_stlb_miss)
190 val |= P(TLB, MISS) | P(TLB, L2);
191 else
192 val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
195 * bit 5: locked prefix
197 if (dse.ld_locked)
198 val |= P(LOCK, LOCKED);
200 return val;
203 struct pebs_record_core {
204 u64 flags, ip;
205 u64 ax, bx, cx, dx;
206 u64 si, di, bp, sp;
207 u64 r8, r9, r10, r11;
208 u64 r12, r13, r14, r15;
211 struct pebs_record_nhm {
212 u64 flags, ip;
213 u64 ax, bx, cx, dx;
214 u64 si, di, bp, sp;
215 u64 r8, r9, r10, r11;
216 u64 r12, r13, r14, r15;
217 u64 status, dla, dse, lat;
221 * Same as pebs_record_nhm, with two additional fields.
223 struct pebs_record_hsw {
224 u64 flags, ip;
225 u64 ax, bx, cx, dx;
226 u64 si, di, bp, sp;
227 u64 r8, r9, r10, r11;
228 u64 r12, r13, r14, r15;
229 u64 status, dla, dse, lat;
230 u64 real_ip, tsx_tuning;
233 union hsw_tsx_tuning {
234 struct {
235 u32 cycles_last_block : 32,
236 hle_abort : 1,
237 rtm_abort : 1,
238 instruction_abort : 1,
239 non_instruction_abort : 1,
240 retry : 1,
241 data_conflict : 1,
242 capacity_writes : 1,
243 capacity_reads : 1;
245 u64 value;
248 #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
250 /* Same as HSW, plus TSC */
252 struct pebs_record_skl {
253 u64 flags, ip;
254 u64 ax, bx, cx, dx;
255 u64 si, di, bp, sp;
256 u64 r8, r9, r10, r11;
257 u64 r12, r13, r14, r15;
258 u64 status, dla, dse, lat;
259 u64 real_ip, tsx_tuning;
260 u64 tsc;
263 void init_debug_store_on_cpu(int cpu)
265 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
267 if (!ds)
268 return;
270 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
271 (u32)((u64)(unsigned long)ds),
272 (u32)((u64)(unsigned long)ds >> 32));
275 void fini_debug_store_on_cpu(int cpu)
277 if (!per_cpu(cpu_hw_events, cpu).ds)
278 return;
280 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
283 static DEFINE_PER_CPU(void *, insn_buffer);
285 static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot)
287 unsigned long start = (unsigned long)cea;
288 phys_addr_t pa;
289 size_t msz = 0;
291 pa = virt_to_phys(addr);
293 preempt_disable();
294 for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE)
295 cea_set_pte(cea, pa, prot);
298 * This is a cross-CPU update of the cpu_entry_area, we must shoot down
299 * all TLB entries for it.
301 flush_tlb_kernel_range(start, start + size);
302 preempt_enable();
305 static void ds_clear_cea(void *cea, size_t size)
307 unsigned long start = (unsigned long)cea;
308 size_t msz = 0;
310 preempt_disable();
311 for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE)
312 cea_set_pte(cea, 0, PAGE_NONE);
314 flush_tlb_kernel_range(start, start + size);
315 preempt_enable();
318 static void *dsalloc_pages(size_t size, gfp_t flags, int cpu)
320 unsigned int order = get_order(size);
321 int node = cpu_to_node(cpu);
322 struct page *page;
324 page = __alloc_pages_node(node, flags | __GFP_ZERO, order);
325 return page ? page_address(page) : NULL;
328 static void dsfree_pages(const void *buffer, size_t size)
330 if (buffer)
331 free_pages((unsigned long)buffer, get_order(size));
334 static int alloc_pebs_buffer(int cpu)
336 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
337 struct debug_store *ds = hwev->ds;
338 size_t bsiz = x86_pmu.pebs_buffer_size;
339 int max, node = cpu_to_node(cpu);
340 void *buffer, *ibuffer, *cea;
342 if (!x86_pmu.pebs)
343 return 0;
345 buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu);
346 if (unlikely(!buffer))
347 return -ENOMEM;
350 * HSW+ already provides us the eventing ip; no need to allocate this
351 * buffer then.
353 if (x86_pmu.intel_cap.pebs_format < 2) {
354 ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
355 if (!ibuffer) {
356 dsfree_pages(buffer, bsiz);
357 return -ENOMEM;
359 per_cpu(insn_buffer, cpu) = ibuffer;
361 hwev->ds_pebs_vaddr = buffer;
362 /* Update the cpu entry area mapping */
363 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
364 ds->pebs_buffer_base = (unsigned long) cea;
365 ds_update_cea(cea, buffer, bsiz, PAGE_KERNEL);
366 ds->pebs_index = ds->pebs_buffer_base;
367 max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size);
368 ds->pebs_absolute_maximum = ds->pebs_buffer_base + max;
369 return 0;
372 static void release_pebs_buffer(int cpu)
374 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
375 struct debug_store *ds = hwev->ds;
376 void *cea;
378 if (!ds || !x86_pmu.pebs)
379 return;
381 kfree(per_cpu(insn_buffer, cpu));
382 per_cpu(insn_buffer, cpu) = NULL;
384 /* Clear the fixmap */
385 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer;
386 ds_clear_cea(cea, x86_pmu.pebs_buffer_size);
387 ds->pebs_buffer_base = 0;
388 dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size);
389 hwev->ds_pebs_vaddr = NULL;
392 static int alloc_bts_buffer(int cpu)
394 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
395 struct debug_store *ds = hwev->ds;
396 void *buffer, *cea;
397 int max;
399 if (!x86_pmu.bts)
400 return 0;
402 buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu);
403 if (unlikely(!buffer)) {
404 WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
405 return -ENOMEM;
407 hwev->ds_bts_vaddr = buffer;
408 /* Update the fixmap */
409 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
410 ds->bts_buffer_base = (unsigned long) cea;
411 ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL);
412 ds->bts_index = ds->bts_buffer_base;
413 max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
414 ds->bts_absolute_maximum = ds->bts_buffer_base +
415 max * BTS_RECORD_SIZE;
416 ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
417 (max / 16) * BTS_RECORD_SIZE;
418 return 0;
421 static void release_bts_buffer(int cpu)
423 struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu);
424 struct debug_store *ds = hwev->ds;
425 void *cea;
427 if (!ds || !x86_pmu.bts)
428 return;
430 /* Clear the fixmap */
431 cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer;
432 ds_clear_cea(cea, BTS_BUFFER_SIZE);
433 ds->bts_buffer_base = 0;
434 dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE);
435 hwev->ds_bts_vaddr = NULL;
438 static int alloc_ds_buffer(int cpu)
440 struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store;
442 memset(ds, 0, sizeof(*ds));
443 per_cpu(cpu_hw_events, cpu).ds = ds;
444 return 0;
447 static void release_ds_buffer(int cpu)
449 per_cpu(cpu_hw_events, cpu).ds = NULL;
452 void release_ds_buffers(void)
454 int cpu;
456 if (!x86_pmu.bts && !x86_pmu.pebs)
457 return;
459 get_online_cpus();
460 for_each_online_cpu(cpu)
461 fini_debug_store_on_cpu(cpu);
463 for_each_possible_cpu(cpu) {
464 release_pebs_buffer(cpu);
465 release_bts_buffer(cpu);
466 release_ds_buffer(cpu);
468 put_online_cpus();
471 void reserve_ds_buffers(void)
473 int bts_err = 0, pebs_err = 0;
474 int cpu;
476 x86_pmu.bts_active = 0;
477 x86_pmu.pebs_active = 0;
479 if (!x86_pmu.bts && !x86_pmu.pebs)
480 return;
482 if (!x86_pmu.bts)
483 bts_err = 1;
485 if (!x86_pmu.pebs)
486 pebs_err = 1;
488 get_online_cpus();
490 for_each_possible_cpu(cpu) {
491 if (alloc_ds_buffer(cpu)) {
492 bts_err = 1;
493 pebs_err = 1;
496 if (!bts_err && alloc_bts_buffer(cpu))
497 bts_err = 1;
499 if (!pebs_err && alloc_pebs_buffer(cpu))
500 pebs_err = 1;
502 if (bts_err && pebs_err)
503 break;
506 if (bts_err) {
507 for_each_possible_cpu(cpu)
508 release_bts_buffer(cpu);
511 if (pebs_err) {
512 for_each_possible_cpu(cpu)
513 release_pebs_buffer(cpu);
516 if (bts_err && pebs_err) {
517 for_each_possible_cpu(cpu)
518 release_ds_buffer(cpu);
519 } else {
520 if (x86_pmu.bts && !bts_err)
521 x86_pmu.bts_active = 1;
523 if (x86_pmu.pebs && !pebs_err)
524 x86_pmu.pebs_active = 1;
526 for_each_online_cpu(cpu)
527 init_debug_store_on_cpu(cpu);
530 put_online_cpus();
534 * BTS
537 struct event_constraint bts_constraint =
538 EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
540 void intel_pmu_enable_bts(u64 config)
542 unsigned long debugctlmsr;
544 debugctlmsr = get_debugctlmsr();
546 debugctlmsr |= DEBUGCTLMSR_TR;
547 debugctlmsr |= DEBUGCTLMSR_BTS;
548 if (config & ARCH_PERFMON_EVENTSEL_INT)
549 debugctlmsr |= DEBUGCTLMSR_BTINT;
551 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
552 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
554 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
555 debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
557 update_debugctlmsr(debugctlmsr);
560 void intel_pmu_disable_bts(void)
562 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
563 unsigned long debugctlmsr;
565 if (!cpuc->ds)
566 return;
568 debugctlmsr = get_debugctlmsr();
570 debugctlmsr &=
571 ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
572 DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
574 update_debugctlmsr(debugctlmsr);
577 int intel_pmu_drain_bts_buffer(void)
579 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
580 struct debug_store *ds = cpuc->ds;
581 struct bts_record {
582 u64 from;
583 u64 to;
584 u64 flags;
586 struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
587 struct bts_record *at, *base, *top;
588 struct perf_output_handle handle;
589 struct perf_event_header header;
590 struct perf_sample_data data;
591 unsigned long skip = 0;
592 struct pt_regs regs;
594 if (!event)
595 return 0;
597 if (!x86_pmu.bts_active)
598 return 0;
600 base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
601 top = (struct bts_record *)(unsigned long)ds->bts_index;
603 if (top <= base)
604 return 0;
606 memset(&regs, 0, sizeof(regs));
608 ds->bts_index = ds->bts_buffer_base;
610 perf_sample_data_init(&data, 0, event->hw.last_period);
613 * BTS leaks kernel addresses in branches across the cpl boundary,
614 * such as traps or system calls, so unless the user is asking for
615 * kernel tracing (and right now it's not possible), we'd need to
616 * filter them out. But first we need to count how many of those we
617 * have in the current batch. This is an extra O(n) pass, however,
618 * it's much faster than the other one especially considering that
619 * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
620 * alloc_bts_buffer()).
622 for (at = base; at < top; at++) {
624 * Note that right now *this* BTS code only works if
625 * attr::exclude_kernel is set, but let's keep this extra
626 * check here in case that changes.
628 if (event->attr.exclude_kernel &&
629 (kernel_ip(at->from) || kernel_ip(at->to)))
630 skip++;
634 * Prepare a generic sample, i.e. fill in the invariant fields.
635 * We will overwrite the from and to address before we output
636 * the sample.
638 rcu_read_lock();
639 perf_prepare_sample(&header, &data, event, &regs);
641 if (perf_output_begin(&handle, event, header.size *
642 (top - base - skip)))
643 goto unlock;
645 for (at = base; at < top; at++) {
646 /* Filter out any records that contain kernel addresses. */
647 if (event->attr.exclude_kernel &&
648 (kernel_ip(at->from) || kernel_ip(at->to)))
649 continue;
651 data.ip = at->from;
652 data.addr = at->to;
654 perf_output_sample(&handle, &header, &data, event);
657 perf_output_end(&handle);
659 /* There's new data available. */
660 event->hw.interrupts++;
661 event->pending_kill = POLL_IN;
662 unlock:
663 rcu_read_unlock();
664 return 1;
667 static inline void intel_pmu_drain_pebs_buffer(void)
669 struct pt_regs regs;
671 x86_pmu.drain_pebs(&regs);
675 * PEBS
677 struct event_constraint intel_core2_pebs_event_constraints[] = {
678 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
679 INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
680 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
681 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
682 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
683 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
684 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
685 EVENT_CONSTRAINT_END
688 struct event_constraint intel_atom_pebs_event_constraints[] = {
689 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
690 INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
691 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
692 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
693 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x01),
694 /* Allow all events as PEBS with no flags */
695 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
696 EVENT_CONSTRAINT_END
699 struct event_constraint intel_slm_pebs_event_constraints[] = {
700 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
701 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x1),
702 /* Allow all events as PEBS with no flags */
703 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
704 EVENT_CONSTRAINT_END
707 struct event_constraint intel_glm_pebs_event_constraints[] = {
708 /* Allow all events as PEBS with no flags */
709 INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
710 EVENT_CONSTRAINT_END
713 struct event_constraint intel_glp_pebs_event_constraints[] = {
714 /* Allow all events as PEBS with no flags */
715 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
716 EVENT_CONSTRAINT_END
719 struct event_constraint intel_nehalem_pebs_event_constraints[] = {
720 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
721 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
722 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
723 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
724 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
725 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
726 INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
727 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
728 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
729 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
730 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
731 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
732 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
733 EVENT_CONSTRAINT_END
736 struct event_constraint intel_westmere_pebs_event_constraints[] = {
737 INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
738 INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
739 INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
740 INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
741 INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
742 INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
743 INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
744 INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
745 INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
746 INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
747 INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
748 /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
749 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
750 EVENT_CONSTRAINT_END
753 struct event_constraint intel_snb_pebs_event_constraints[] = {
754 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
755 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
756 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
757 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
758 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
759 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
760 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
761 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
762 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
763 /* Allow all events as PEBS with no flags */
764 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
765 EVENT_CONSTRAINT_END
768 struct event_constraint intel_ivb_pebs_event_constraints[] = {
769 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
770 INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
771 INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
772 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
773 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
774 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
775 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
776 INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
777 INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
778 INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
779 INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
780 /* Allow all events as PEBS with no flags */
781 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
782 EVENT_CONSTRAINT_END
785 struct event_constraint intel_hsw_pebs_event_constraints[] = {
786 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
787 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
788 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
789 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
790 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
791 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
792 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
793 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
794 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
795 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
796 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
797 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
798 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
799 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
800 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
801 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
802 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
803 /* Allow all events as PEBS with no flags */
804 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
805 EVENT_CONSTRAINT_END
808 struct event_constraint intel_bdw_pebs_event_constraints[] = {
809 INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
810 INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
811 /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
812 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c2, 0xf),
813 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
814 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
815 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
816 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
817 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
818 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
819 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
820 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
821 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
822 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
823 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
824 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
825 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
826 /* Allow all events as PEBS with no flags */
827 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
828 EVENT_CONSTRAINT_END
832 struct event_constraint intel_skl_pebs_event_constraints[] = {
833 INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
834 /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
835 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108001c0, 0x2),
836 /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
837 INTEL_FLAGS_UEVENT_CONSTRAINT(0x108000c0, 0x0f),
838 INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
839 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
840 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
841 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
842 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
843 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
844 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
845 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
846 INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
847 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
848 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
849 INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
850 /* Allow all events as PEBS with no flags */
851 INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
852 EVENT_CONSTRAINT_END
855 struct event_constraint *intel_pebs_constraints(struct perf_event *event)
857 struct event_constraint *c;
859 if (!event->attr.precise_ip)
860 return NULL;
862 if (x86_pmu.pebs_constraints) {
863 for_each_event_constraint(c, x86_pmu.pebs_constraints) {
864 if ((event->hw.config & c->cmask) == c->code) {
865 event->hw.flags |= c->flags;
866 return c;
871 return &emptyconstraint;
875 * We need the sched_task callback even for per-cpu events when we use
876 * the large interrupt threshold, such that we can provide PID and TID
877 * to PEBS samples.
879 static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc)
881 return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs);
884 void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
886 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
888 if (!sched_in && pebs_needs_sched_cb(cpuc))
889 intel_pmu_drain_pebs_buffer();
892 static inline void pebs_update_threshold(struct cpu_hw_events *cpuc)
894 struct debug_store *ds = cpuc->ds;
895 u64 threshold;
897 if (cpuc->n_pebs == cpuc->n_large_pebs) {
898 threshold = ds->pebs_absolute_maximum -
899 x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
900 } else {
901 threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
904 ds->pebs_interrupt_threshold = threshold;
907 static void
908 pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu)
911 * Make sure we get updated with the first PEBS
912 * event. It will trigger also during removal, but
913 * that does not hurt:
915 bool update = cpuc->n_pebs == 1;
917 if (needed_cb != pebs_needs_sched_cb(cpuc)) {
918 if (!needed_cb)
919 perf_sched_cb_inc(pmu);
920 else
921 perf_sched_cb_dec(pmu);
923 update = true;
926 if (update)
927 pebs_update_threshold(cpuc);
930 void intel_pmu_pebs_add(struct perf_event *event)
932 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
933 struct hw_perf_event *hwc = &event->hw;
934 bool needed_cb = pebs_needs_sched_cb(cpuc);
936 cpuc->n_pebs++;
937 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
938 cpuc->n_large_pebs++;
940 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
943 void intel_pmu_pebs_enable(struct perf_event *event)
945 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
946 struct hw_perf_event *hwc = &event->hw;
947 struct debug_store *ds = cpuc->ds;
949 hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
951 cpuc->pebs_enabled |= 1ULL << hwc->idx;
953 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
954 cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
955 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
956 cpuc->pebs_enabled |= 1ULL << 63;
959 * Use auto-reload if possible to save a MSR write in the PMI.
960 * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD.
962 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
963 ds->pebs_event_reset[hwc->idx] =
964 (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
965 } else {
966 ds->pebs_event_reset[hwc->idx] = 0;
970 void intel_pmu_pebs_del(struct perf_event *event)
972 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
973 struct hw_perf_event *hwc = &event->hw;
974 bool needed_cb = pebs_needs_sched_cb(cpuc);
976 cpuc->n_pebs--;
977 if (hwc->flags & PERF_X86_EVENT_FREERUNNING)
978 cpuc->n_large_pebs--;
980 pebs_update_state(needed_cb, cpuc, event->ctx->pmu);
983 void intel_pmu_pebs_disable(struct perf_event *event)
985 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
986 struct hw_perf_event *hwc = &event->hw;
988 if (cpuc->n_pebs == cpuc->n_large_pebs)
989 intel_pmu_drain_pebs_buffer();
991 cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
993 if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
994 cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
995 else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
996 cpuc->pebs_enabled &= ~(1ULL << 63);
998 if (cpuc->enabled)
999 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1001 hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
1004 void intel_pmu_pebs_enable_all(void)
1006 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1008 if (cpuc->pebs_enabled)
1009 wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
1012 void intel_pmu_pebs_disable_all(void)
1014 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1016 if (cpuc->pebs_enabled)
1017 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1020 static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
1022 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1023 unsigned long from = cpuc->lbr_entries[0].from;
1024 unsigned long old_to, to = cpuc->lbr_entries[0].to;
1025 unsigned long ip = regs->ip;
1026 int is_64bit = 0;
1027 void *kaddr;
1028 int size;
1031 * We don't need to fixup if the PEBS assist is fault like
1033 if (!x86_pmu.intel_cap.pebs_trap)
1034 return 1;
1037 * No LBR entry, no basic block, no rewinding
1039 if (!cpuc->lbr_stack.nr || !from || !to)
1040 return 0;
1043 * Basic blocks should never cross user/kernel boundaries
1045 if (kernel_ip(ip) != kernel_ip(to))
1046 return 0;
1049 * unsigned math, either ip is before the start (impossible) or
1050 * the basic block is larger than 1 page (sanity)
1052 if ((ip - to) > PEBS_FIXUP_SIZE)
1053 return 0;
1056 * We sampled a branch insn, rewind using the LBR stack
1058 if (ip == to) {
1059 set_linear_ip(regs, from);
1060 return 1;
1063 size = ip - to;
1064 if (!kernel_ip(ip)) {
1065 int bytes;
1066 u8 *buf = this_cpu_read(insn_buffer);
1068 /* 'size' must fit our buffer, see above */
1069 bytes = copy_from_user_nmi(buf, (void __user *)to, size);
1070 if (bytes != 0)
1071 return 0;
1073 kaddr = buf;
1074 } else {
1075 kaddr = (void *)to;
1078 do {
1079 struct insn insn;
1081 old_to = to;
1083 #ifdef CONFIG_X86_64
1084 is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
1085 #endif
1086 insn_init(&insn, kaddr, size, is_64bit);
1087 insn_get_length(&insn);
1089 * Make sure there was not a problem decoding the
1090 * instruction and getting the length. This is
1091 * doubly important because we have an infinite
1092 * loop if insn.length=0.
1094 if (!insn.length)
1095 break;
1097 to += insn.length;
1098 kaddr += insn.length;
1099 size -= insn.length;
1100 } while (to < ip);
1102 if (to == ip) {
1103 set_linear_ip(regs, old_to);
1104 return 1;
1108 * Even though we decoded the basic block, the instruction stream
1109 * never matched the given IP, either the TO or the IP got corrupted.
1111 return 0;
1114 static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
1116 if (pebs->tsx_tuning) {
1117 union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
1118 return tsx.cycles_last_block;
1120 return 0;
1123 static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
1125 u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
1127 /* For RTM XABORTs also log the abort code from AX */
1128 if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
1129 txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1130 return txn;
1133 static void setup_pebs_sample_data(struct perf_event *event,
1134 struct pt_regs *iregs, void *__pebs,
1135 struct perf_sample_data *data,
1136 struct pt_regs *regs)
1138 #define PERF_X86_EVENT_PEBS_HSW_PREC \
1139 (PERF_X86_EVENT_PEBS_ST_HSW | \
1140 PERF_X86_EVENT_PEBS_LD_HSW | \
1141 PERF_X86_EVENT_PEBS_NA_HSW)
1143 * We cast to the biggest pebs_record but are careful not to
1144 * unconditionally access the 'extra' entries.
1146 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1147 struct pebs_record_skl *pebs = __pebs;
1148 u64 sample_type;
1149 int fll, fst, dsrc;
1150 int fl = event->hw.flags;
1152 if (pebs == NULL)
1153 return;
1155 regs->flags &= ~PERF_EFLAGS_EXACT;
1156 sample_type = event->attr.sample_type;
1157 dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
1159 fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
1160 fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
1162 perf_sample_data_init(data, 0, event->hw.last_period);
1164 data->period = event->hw.last_period;
1167 * Use latency for weight (only avail with PEBS-LL)
1169 if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
1170 data->weight = pebs->lat;
1173 * data.data_src encodes the data source
1175 if (dsrc) {
1176 u64 val = PERF_MEM_NA;
1177 if (fll)
1178 val = load_latency_data(pebs->dse);
1179 else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
1180 val = precise_datala_hsw(event, pebs->dse);
1181 else if (fst)
1182 val = precise_store_data(pebs->dse);
1183 data->data_src.val = val;
1187 * We use the interrupt regs as a base because the PEBS record does not
1188 * contain a full regs set, specifically it seems to lack segment
1189 * descriptors, which get used by things like user_mode().
1191 * In the simple case fix up only the IP for PERF_SAMPLE_IP.
1193 * We must however always use BP,SP from iregs for the unwinder to stay
1194 * sane; the record BP,SP can point into thin air when the record is
1195 * from a previous PMI context or an (I)RET happend between the record
1196 * and PMI.
1198 *regs = *iregs;
1199 regs->flags = pebs->flags;
1201 if (sample_type & PERF_SAMPLE_REGS_INTR) {
1202 regs->ax = pebs->ax;
1203 regs->bx = pebs->bx;
1204 regs->cx = pebs->cx;
1205 regs->dx = pebs->dx;
1206 regs->si = pebs->si;
1207 regs->di = pebs->di;
1210 * Per the above; only set BP,SP if we don't need callchains.
1212 * XXX: does this make sense?
1214 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1215 regs->bp = pebs->bp;
1216 regs->sp = pebs->sp;
1220 * Preserve PERF_EFLAGS_VM from set_linear_ip().
1222 regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM);
1223 #ifndef CONFIG_X86_32
1224 regs->r8 = pebs->r8;
1225 regs->r9 = pebs->r9;
1226 regs->r10 = pebs->r10;
1227 regs->r11 = pebs->r11;
1228 regs->r12 = pebs->r12;
1229 regs->r13 = pebs->r13;
1230 regs->r14 = pebs->r14;
1231 regs->r15 = pebs->r15;
1232 #endif
1235 if (event->attr.precise_ip > 1) {
1236 /* Haswell and later have the eventing IP, so use it: */
1237 if (x86_pmu.intel_cap.pebs_format >= 2) {
1238 set_linear_ip(regs, pebs->real_ip);
1239 regs->flags |= PERF_EFLAGS_EXACT;
1240 } else {
1241 /* Otherwise use PEBS off-by-1 IP: */
1242 set_linear_ip(regs, pebs->ip);
1244 /* ... and try to fix it up using the LBR entries: */
1245 if (intel_pmu_pebs_fixup_ip(regs))
1246 regs->flags |= PERF_EFLAGS_EXACT;
1248 } else
1249 set_linear_ip(regs, pebs->ip);
1252 if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) &&
1253 x86_pmu.intel_cap.pebs_format >= 1)
1254 data->addr = pebs->dla;
1256 if (x86_pmu.intel_cap.pebs_format >= 2) {
1257 /* Only set the TSX weight when no memory weight. */
1258 if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
1259 data->weight = intel_hsw_weight(pebs);
1261 if (sample_type & PERF_SAMPLE_TRANSACTION)
1262 data->txn = intel_hsw_transaction(pebs);
1266 * v3 supplies an accurate time stamp, so we use that
1267 * for the time stamp.
1269 * We can only do this for the default trace clock.
1271 if (x86_pmu.intel_cap.pebs_format >= 3 &&
1272 event->attr.use_clockid == 0)
1273 data->time = native_sched_clock_from_tsc(pebs->tsc);
1275 if (has_branch_stack(event))
1276 data->br_stack = &cpuc->lbr_stack;
1279 static inline void *
1280 get_next_pebs_record_by_bit(void *base, void *top, int bit)
1282 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1283 void *at;
1284 u64 pebs_status;
1287 * fmt0 does not have a status bitfield (does not use
1288 * perf_record_nhm format)
1290 if (x86_pmu.intel_cap.pebs_format < 1)
1291 return base;
1293 if (base == NULL)
1294 return NULL;
1296 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1297 struct pebs_record_nhm *p = at;
1299 if (test_bit(bit, (unsigned long *)&p->status)) {
1300 /* PEBS v3 has accurate status bits */
1301 if (x86_pmu.intel_cap.pebs_format >= 3)
1302 return at;
1304 if (p->status == (1 << bit))
1305 return at;
1307 /* clear non-PEBS bit and re-check */
1308 pebs_status = p->status & cpuc->pebs_enabled;
1309 pebs_status &= PEBS_COUNTER_MASK;
1310 if (pebs_status == (1 << bit))
1311 return at;
1314 return NULL;
1318 * Special variant of intel_pmu_save_and_restart() for auto-reload.
1320 static int
1321 intel_pmu_save_and_restart_reload(struct perf_event *event, int count)
1323 struct hw_perf_event *hwc = &event->hw;
1324 int shift = 64 - x86_pmu.cntval_bits;
1325 u64 period = hwc->sample_period;
1326 u64 prev_raw_count, new_raw_count;
1327 s64 new, old;
1329 WARN_ON(!period);
1332 * drain_pebs() only happens when the PMU is disabled.
1334 WARN_ON(this_cpu_read(cpu_hw_events.enabled));
1336 prev_raw_count = local64_read(&hwc->prev_count);
1337 rdpmcl(hwc->event_base_rdpmc, new_raw_count);
1338 local64_set(&hwc->prev_count, new_raw_count);
1341 * Since the counter increments a negative counter value and
1342 * overflows on the sign switch, giving the interval:
1344 * [-period, 0]
1346 * the difference between two consequtive reads is:
1348 * A) value2 - value1;
1349 * when no overflows have happened in between,
1351 * B) (0 - value1) + (value2 - (-period));
1352 * when one overflow happened in between,
1354 * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period));
1355 * when @n overflows happened in between.
1357 * Here A) is the obvious difference, B) is the extension to the
1358 * discrete interval, where the first term is to the top of the
1359 * interval and the second term is from the bottom of the next
1360 * interval and C) the extension to multiple intervals, where the
1361 * middle term is the whole intervals covered.
1363 * An equivalent of C, by reduction, is:
1365 * value2 - value1 + n * period
1367 new = ((s64)(new_raw_count << shift) >> shift);
1368 old = ((s64)(prev_raw_count << shift) >> shift);
1369 local64_add(new - old + count * period, &event->count);
1371 local64_set(&hwc->period_left, -new);
1373 perf_event_update_userpage(event);
1375 return 0;
1378 static void __intel_pmu_pebs_event(struct perf_event *event,
1379 struct pt_regs *iregs,
1380 void *base, void *top,
1381 int bit, int count)
1383 struct hw_perf_event *hwc = &event->hw;
1384 struct perf_sample_data data;
1385 struct pt_regs regs;
1386 void *at = get_next_pebs_record_by_bit(base, top, bit);
1388 if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
1390 * Now, auto-reload is only enabled in fixed period mode.
1391 * The reload value is always hwc->sample_period.
1392 * May need to change it, if auto-reload is enabled in
1393 * freq mode later.
1395 intel_pmu_save_and_restart_reload(event, count);
1396 } else if (!intel_pmu_save_and_restart(event))
1397 return;
1399 while (count > 1) {
1400 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1401 perf_event_output(event, &data, &regs);
1402 at += x86_pmu.pebs_record_size;
1403 at = get_next_pebs_record_by_bit(at, top, bit);
1404 count--;
1407 setup_pebs_sample_data(event, iregs, at, &data, &regs);
1410 * All but the last records are processed.
1411 * The last one is left to be able to call the overflow handler.
1413 if (perf_event_overflow(event, &data, &regs)) {
1414 x86_pmu_stop(event, 0);
1415 return;
1420 static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
1422 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1423 struct debug_store *ds = cpuc->ds;
1424 struct perf_event *event = cpuc->events[0]; /* PMC0 only */
1425 struct pebs_record_core *at, *top;
1426 int n;
1428 if (!x86_pmu.pebs_active)
1429 return;
1431 at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
1432 top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
1435 * Whatever else happens, drain the thing
1437 ds->pebs_index = ds->pebs_buffer_base;
1439 if (!test_bit(0, cpuc->active_mask))
1440 return;
1442 WARN_ON_ONCE(!event);
1444 if (!event->attr.precise_ip)
1445 return;
1447 n = top - at;
1448 if (n <= 0) {
1449 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
1450 intel_pmu_save_and_restart_reload(event, 0);
1451 return;
1454 __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
1457 static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
1459 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1460 struct debug_store *ds = cpuc->ds;
1461 struct perf_event *event;
1462 void *base, *at, *top;
1463 short counts[MAX_PEBS_EVENTS] = {};
1464 short error[MAX_PEBS_EVENTS] = {};
1465 int bit, i;
1467 if (!x86_pmu.pebs_active)
1468 return;
1470 base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
1471 top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
1473 ds->pebs_index = ds->pebs_buffer_base;
1475 if (unlikely(base >= top)) {
1477 * The drain_pebs() could be called twice in a short period
1478 * for auto-reload event in pmu::read(). There are no
1479 * overflows have happened in between.
1480 * It needs to call intel_pmu_save_and_restart_reload() to
1481 * update the event->count for this case.
1483 for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled,
1484 x86_pmu.max_pebs_events) {
1485 event = cpuc->events[bit];
1486 if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
1487 intel_pmu_save_and_restart_reload(event, 0);
1489 return;
1492 for (at = base; at < top; at += x86_pmu.pebs_record_size) {
1493 struct pebs_record_nhm *p = at;
1494 u64 pebs_status;
1496 pebs_status = p->status & cpuc->pebs_enabled;
1497 pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
1499 /* PEBS v3 has more accurate status bits */
1500 if (x86_pmu.intel_cap.pebs_format >= 3) {
1501 for_each_set_bit(bit, (unsigned long *)&pebs_status,
1502 x86_pmu.max_pebs_events)
1503 counts[bit]++;
1505 continue;
1509 * On some CPUs the PEBS status can be zero when PEBS is
1510 * racing with clearing of GLOBAL_STATUS.
1512 * Normally we would drop that record, but in the
1513 * case when there is only a single active PEBS event
1514 * we can assume it's for that event.
1516 if (!pebs_status && cpuc->pebs_enabled &&
1517 !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
1518 pebs_status = cpuc->pebs_enabled;
1520 bit = find_first_bit((unsigned long *)&pebs_status,
1521 x86_pmu.max_pebs_events);
1522 if (bit >= x86_pmu.max_pebs_events)
1523 continue;
1526 * The PEBS hardware does not deal well with the situation
1527 * when events happen near to each other and multiple bits
1528 * are set. But it should happen rarely.
1530 * If these events include one PEBS and multiple non-PEBS
1531 * events, it doesn't impact PEBS record. The record will
1532 * be handled normally. (slow path)
1534 * If these events include two or more PEBS events, the
1535 * records for the events can be collapsed into a single
1536 * one, and it's not possible to reconstruct all events
1537 * that caused the PEBS record. It's called collision.
1538 * If collision happened, the record will be dropped.
1540 if (p->status != (1ULL << bit)) {
1541 for_each_set_bit(i, (unsigned long *)&pebs_status,
1542 x86_pmu.max_pebs_events)
1543 error[i]++;
1544 continue;
1547 counts[bit]++;
1550 for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
1551 if ((counts[bit] == 0) && (error[bit] == 0))
1552 continue;
1554 event = cpuc->events[bit];
1555 if (WARN_ON_ONCE(!event))
1556 continue;
1558 if (WARN_ON_ONCE(!event->attr.precise_ip))
1559 continue;
1561 /* log dropped samples number */
1562 if (error[bit]) {
1563 perf_log_lost_samples(event, error[bit]);
1565 if (perf_event_account_interrupt(event))
1566 x86_pmu_stop(event, 0);
1569 if (counts[bit]) {
1570 __intel_pmu_pebs_event(event, iregs, base,
1571 top, bit, counts[bit]);
1577 * BTS, PEBS probe and setup
1580 void __init intel_ds_init(void)
1583 * No support for 32bit formats
1585 if (!boot_cpu_has(X86_FEATURE_DTES64))
1586 return;
1588 x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
1589 x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
1590 x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE;
1591 if (x86_pmu.pebs) {
1592 char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
1593 int format = x86_pmu.intel_cap.pebs_format;
1595 switch (format) {
1596 case 0:
1597 pr_cont("PEBS fmt0%c, ", pebs_type);
1598 x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
1600 * Using >PAGE_SIZE buffers makes the WRMSR to
1601 * PERF_GLOBAL_CTRL in intel_pmu_enable_all()
1602 * mysteriously hang on Core2.
1604 * As a workaround, we don't do this.
1606 x86_pmu.pebs_buffer_size = PAGE_SIZE;
1607 x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
1608 break;
1610 case 1:
1611 pr_cont("PEBS fmt1%c, ", pebs_type);
1612 x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
1613 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1614 break;
1616 case 2:
1617 pr_cont("PEBS fmt2%c, ", pebs_type);
1618 x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
1619 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1620 break;
1622 case 3:
1623 pr_cont("PEBS fmt3%c, ", pebs_type);
1624 x86_pmu.pebs_record_size =
1625 sizeof(struct pebs_record_skl);
1626 x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
1627 x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
1628 break;
1630 default:
1631 pr_cont("no PEBS fmt%d%c, ", format, pebs_type);
1632 x86_pmu.pebs = 0;
1637 void perf_restore_debug_store(void)
1639 struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
1641 if (!x86_pmu.bts && !x86_pmu.pebs)
1642 return;
1644 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);