Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 2 | #include <linux/bitops.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <linux/slab.h> |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 5 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 6 | #include <asm/cpu_entry_area.h> |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 7 | #include <asm/perf_event.h> |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 8 | #include <asm/tlbflush.h> |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 9 | #include <asm/insn.h> |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 10 | |
Borislav Petkov | 27f6d22 | 2016-02-10 10:55:23 +0100 | [diff] [blame] | 11 | #include "../perf_event.h" |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 12 | |
Thomas Gleixner | 10043e0 | 2017-12-04 15:07:49 +0100 | [diff] [blame] | 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); |
| 15 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 16 | /* The size of a BTS record in bytes: */ |
| 17 | #define BTS_RECORD_SIZE 24 |
| 18 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 19 | #define PEBS_FIXUP_SIZE PAGE_SIZE |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * pebs_record_32 for p4 and core not supported |
| 23 | |
| 24 | struct pebs_record_32 { |
| 25 | u32 flags, ip; |
| 26 | u32 ax, bc, cx, dx; |
| 27 | u32 si, di, bp, sp; |
| 28 | }; |
| 29 | |
| 30 | */ |
| 31 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 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; |
| 39 | }; |
| 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; |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * Map PEBS Load Latency Data Source encodings to generic |
| 52 | * memory data source information |
| 53 | */ |
| 54 | #define P(a, b) PERF_MEM_S(a, b) |
| 55 | #define OP_LH (P(OP, LOAD) | P(LVL, HIT)) |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 56 | #define LEVEL(x) P(LVLNUM, x) |
| 57 | #define REM P(REMOTE, REMOTE) |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 58 | #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS)) |
| 59 | |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 60 | /* Version for Sandy Bridge and later */ |
| 61 | static u64 pebs_data_source[] = { |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 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 */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 80 | /* Patch up minor differences in the bits */ |
| 81 | void __init intel_pmu_pebs_data_source_nhm(void) |
| 82 | { |
Andi Kleen | 6ae5fa6 | 2017-08-16 15:21:54 -0700 | [diff] [blame] | 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); |
| 86 | } |
| 87 | |
| 88 | void __init intel_pmu_pebs_data_source_skl(bool pmem) |
| 89 | { |
| 90 | u64 pmem_or_l4 = pmem ? LEVEL(PMEM) : LEVEL(L4); |
| 91 | |
| 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); |
Andi Kleen | e17dc65 | 2016-03-01 14:25:24 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 99 | static u64 precise_store_data(u64 status) |
| 100 | { |
| 101 | union intel_x86_pebs_dse dse; |
| 102 | u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2); |
| 103 | |
| 104 | dse.val = status; |
| 105 | |
| 106 | /* |
| 107 | * bit 4: TLB access |
| 108 | * 1 = stored missed 2nd level TLB |
| 109 | * |
| 110 | * so it either hit the walker or the OS |
| 111 | * otherwise hit 2nd level TLB |
| 112 | */ |
| 113 | if (dse.st_stlb_miss) |
| 114 | val |= P(TLB, MISS); |
| 115 | else |
| 116 | val |= P(TLB, HIT); |
| 117 | |
| 118 | /* |
| 119 | * bit 0: hit L1 data cache |
| 120 | * if not set, then all we know is that |
| 121 | * it missed L1D |
| 122 | */ |
| 123 | if (dse.st_l1d_hit) |
| 124 | val |= P(LVL, HIT); |
| 125 | else |
| 126 | val |= P(LVL, MISS); |
| 127 | |
| 128 | /* |
| 129 | * bit 5: Locked prefix |
| 130 | */ |
| 131 | if (dse.st_locked) |
| 132 | val |= P(LOCK, LOCKED); |
| 133 | |
| 134 | return val; |
| 135 | } |
| 136 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 137 | static u64 precise_datala_hsw(struct perf_event *event, u64 status) |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 138 | { |
| 139 | union perf_mem_data_src dse; |
| 140 | |
Stephane Eranian | 770eee1 | 2014-08-11 21:27:12 +0200 | [diff] [blame] | 141 | dse.val = PERF_MEM_NA; |
| 142 | |
| 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; |
Stephane Eranian | 722e76e | 2014-05-15 17:56:44 +0200 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * L1 info only valid for following events: |
| 150 | * |
| 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 |
| 155 | */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 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; |
| 161 | } |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 162 | return dse.val; |
| 163 | } |
| 164 | |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 165 | static u64 load_latency_data(u64 status) |
| 166 | { |
| 167 | union intel_x86_pebs_dse dse; |
| 168 | u64 val; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 169 | |
| 170 | dse.val = status; |
| 171 | |
| 172 | /* |
| 173 | * use the mapping table for bit 0-3 |
| 174 | */ |
| 175 | val = pebs_data_source[dse.ld_dse]; |
| 176 | |
| 177 | /* |
| 178 | * Nehalem models do not support TLB, Lock infos |
| 179 | */ |
Andi Kleen | 9529835 | 2017-08-16 15:21:53 -0700 | [diff] [blame] | 180 | if (x86_pmu.pebs_no_tlb) { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 181 | val |= P(TLB, NA) | P(LOCK, NA); |
| 182 | return val; |
| 183 | } |
| 184 | /* |
| 185 | * bit 4: TLB access |
| 186 | * 0 = did not miss 2nd level TLB |
| 187 | * 1 = missed 2nd level TLB |
| 188 | */ |
| 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); |
| 193 | |
| 194 | /* |
| 195 | * bit 5: locked prefix |
| 196 | */ |
| 197 | if (dse.ld_locked) |
| 198 | val |= P(LOCK, LOCKED); |
| 199 | |
| 200 | return val; |
| 201 | } |
| 202 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 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; |
| 209 | }; |
| 210 | |
| 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; |
| 218 | }; |
| 219 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Same as pebs_record_nhm, with two additional fields. |
| 222 | */ |
| 223 | struct pebs_record_hsw { |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 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; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 230 | u64 real_ip, tsx_tuning; |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
| 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; |
| 244 | }; |
| 245 | u64 value; |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 248 | #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL |
| 249 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 250 | /* Same as HSW, plus TSC */ |
| 251 | |
| 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; |
| 261 | }; |
| 262 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 263 | void init_debug_store_on_cpu(int cpu) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 264 | { |
| 265 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; |
| 266 | |
| 267 | if (!ds) |
| 268 | return; |
| 269 | |
| 270 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, |
| 271 | (u32)((u64)(unsigned long)ds), |
| 272 | (u32)((u64)(unsigned long)ds >> 32)); |
| 273 | } |
| 274 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 275 | void fini_debug_store_on_cpu(int cpu) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 276 | { |
| 277 | if (!per_cpu(cpu_hw_events, cpu).ds) |
| 278 | return; |
| 279 | |
| 280 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); |
| 281 | } |
| 282 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 283 | static DEFINE_PER_CPU(void *, insn_buffer); |
| 284 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 285 | static void ds_update_cea(void *cea, void *addr, size_t size, pgprot_t prot) |
| 286 | { |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 287 | unsigned long start = (unsigned long)cea; |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 288 | phys_addr_t pa; |
| 289 | size_t msz = 0; |
| 290 | |
| 291 | pa = virt_to_phys(addr); |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 292 | |
| 293 | preempt_disable(); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 294 | for (; msz < size; msz += PAGE_SIZE, pa += PAGE_SIZE, cea += PAGE_SIZE) |
| 295 | cea_set_pte(cea, pa, prot); |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * This is a cross-CPU update of the cpu_entry_area, we must shoot down |
| 299 | * all TLB entries for it. |
| 300 | */ |
| 301 | flush_tlb_kernel_range(start, start + size); |
| 302 | preempt_enable(); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void ds_clear_cea(void *cea, size_t size) |
| 306 | { |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 307 | unsigned long start = (unsigned long)cea; |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 308 | size_t msz = 0; |
| 309 | |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 310 | preempt_disable(); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 311 | for (; msz < size; msz += PAGE_SIZE, cea += PAGE_SIZE) |
| 312 | cea_set_pte(cea, 0, PAGE_NONE); |
Peter Zijlstra | 42f3bdc | 2018-01-04 18:07:12 +0100 | [diff] [blame] | 313 | |
| 314 | flush_tlb_kernel_range(start, start + size); |
| 315 | preempt_enable(); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void *dsalloc_pages(size_t size, gfp_t flags, int cpu) |
| 319 | { |
| 320 | unsigned int order = get_order(size); |
| 321 | int node = cpu_to_node(cpu); |
| 322 | struct page *page; |
| 323 | |
| 324 | page = __alloc_pages_node(node, flags | __GFP_ZERO, order); |
| 325 | return page ? page_address(page) : NULL; |
| 326 | } |
| 327 | |
| 328 | static void dsfree_pages(const void *buffer, size_t size) |
| 329 | { |
| 330 | if (buffer) |
| 331 | free_pages((unsigned long)buffer, get_order(size)); |
| 332 | } |
| 333 | |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 334 | static int alloc_pebs_buffer(int cpu) |
| 335 | { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 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; |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 341 | |
| 342 | if (!x86_pmu.pebs) |
| 343 | return 0; |
| 344 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 345 | buffer = dsalloc_pages(bsiz, GFP_KERNEL, cpu); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 346 | if (unlikely(!buffer)) |
| 347 | return -ENOMEM; |
| 348 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 349 | /* |
| 350 | * HSW+ already provides us the eventing ip; no need to allocate this |
| 351 | * buffer then. |
| 352 | */ |
| 353 | if (x86_pmu.intel_cap.pebs_format < 2) { |
| 354 | ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node); |
| 355 | if (!ibuffer) { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 356 | dsfree_pages(buffer, bsiz); |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 357 | return -ENOMEM; |
| 358 | } |
| 359 | per_cpu(insn_buffer, cpu) = ibuffer; |
| 360 | } |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 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); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 366 | ds->pebs_index = ds->pebs_buffer_base; |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 367 | max = x86_pmu.pebs_record_size * (bsiz / x86_pmu.pebs_record_size); |
| 368 | ds->pebs_absolute_maximum = ds->pebs_buffer_base + max; |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 372 | static void release_pebs_buffer(int cpu) |
| 373 | { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 374 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 375 | void *cea; |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 376 | |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 377 | if (!x86_pmu.pebs) |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 378 | return; |
| 379 | |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 380 | kfree(per_cpu(insn_buffer, cpu)); |
| 381 | per_cpu(insn_buffer, cpu) = NULL; |
| 382 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 383 | /* Clear the fixmap */ |
| 384 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.pebs_buffer; |
| 385 | ds_clear_cea(cea, x86_pmu.pebs_buffer_size); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 386 | dsfree_pages(hwev->ds_pebs_vaddr, x86_pmu.pebs_buffer_size); |
| 387 | hwev->ds_pebs_vaddr = NULL; |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 390 | static int alloc_bts_buffer(int cpu) |
| 391 | { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 392 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
| 393 | struct debug_store *ds = hwev->ds; |
| 394 | void *buffer, *cea; |
| 395 | int max; |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 396 | |
| 397 | if (!x86_pmu.bts) |
| 398 | return 0; |
| 399 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 400 | buffer = dsalloc_pages(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, cpu); |
David Rientjes | 4485154 | 2014-06-30 16:04:08 -0700 | [diff] [blame] | 401 | if (unlikely(!buffer)) { |
| 402 | WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 403 | return -ENOMEM; |
David Rientjes | 4485154 | 2014-06-30 16:04:08 -0700 | [diff] [blame] | 404 | } |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 405 | hwev->ds_bts_vaddr = buffer; |
| 406 | /* Update the fixmap */ |
| 407 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; |
| 408 | ds->bts_buffer_base = (unsigned long) cea; |
| 409 | ds_update_cea(cea, buffer, BTS_BUFFER_SIZE, PAGE_KERNEL); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 410 | ds->bts_index = ds->bts_buffer_base; |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 411 | max = BTS_RECORD_SIZE * (BTS_BUFFER_SIZE / BTS_RECORD_SIZE); |
| 412 | ds->bts_absolute_maximum = ds->bts_buffer_base + max; |
| 413 | ds->bts_interrupt_threshold = ds->bts_absolute_maximum - (max / 16); |
Peter Zijlstra | 5ee25c8 | 2010-10-19 14:15:04 +0200 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 417 | static void release_bts_buffer(int cpu) |
| 418 | { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 419 | struct cpu_hw_events *hwev = per_cpu_ptr(&cpu_hw_events, cpu); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 420 | void *cea; |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 421 | |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 422 | if (!x86_pmu.bts) |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 423 | return; |
| 424 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 425 | /* Clear the fixmap */ |
| 426 | cea = &get_cpu_entry_area(cpu)->cpu_debug_buffers.bts_buffer; |
| 427 | ds_clear_cea(cea, BTS_BUFFER_SIZE); |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 428 | dsfree_pages(hwev->ds_bts_vaddr, BTS_BUFFER_SIZE); |
| 429 | hwev->ds_bts_vaddr = NULL; |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 432 | static int alloc_ds_buffer(int cpu) |
| 433 | { |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 434 | struct debug_store *ds = &get_cpu_entry_area(cpu)->cpu_debug_store; |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 435 | |
Hugh Dickins | c1961a4 | 2017-12-04 15:07:50 +0100 | [diff] [blame] | 436 | memset(ds, 0, sizeof(*ds)); |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 437 | per_cpu(cpu_hw_events, cpu).ds = ds; |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static void release_ds_buffer(int cpu) |
| 442 | { |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 443 | per_cpu(cpu_hw_events, cpu).ds = NULL; |
Peter Zijlstra | 65af94b | 2010-10-19 14:37:23 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 446 | void release_ds_buffers(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 447 | { |
| 448 | int cpu; |
| 449 | |
| 450 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 451 | return; |
| 452 | |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 453 | for_each_possible_cpu(cpu) |
| 454 | release_ds_buffer(cpu); |
| 455 | |
| 456 | for_each_possible_cpu(cpu) { |
| 457 | /* |
| 458 | * Again, ignore errors from offline CPUs, they will no longer |
| 459 | * observe cpu_hw_events.ds and not program the DS_AREA when |
| 460 | * they come up. |
| 461 | */ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 462 | fini_debug_store_on_cpu(cpu); |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 463 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 464 | |
| 465 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | b39f88a | 2010-10-19 14:08:29 +0200 | [diff] [blame] | 466 | release_pebs_buffer(cpu); |
| 467 | release_bts_buffer(cpu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 468 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 471 | void reserve_ds_buffers(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 472 | { |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 473 | int bts_err = 0, pebs_err = 0; |
| 474 | int cpu; |
| 475 | |
| 476 | x86_pmu.bts_active = 0; |
| 477 | x86_pmu.pebs_active = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 478 | |
| 479 | if (!x86_pmu.bts && !x86_pmu.pebs) |
Peter Zijlstra | f80c9e3 | 2010-10-19 14:50:02 +0200 | [diff] [blame] | 480 | return; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 481 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 482 | if (!x86_pmu.bts) |
| 483 | bts_err = 1; |
| 484 | |
| 485 | if (!x86_pmu.pebs) |
| 486 | pebs_err = 1; |
| 487 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 488 | for_each_possible_cpu(cpu) { |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 489 | if (alloc_ds_buffer(cpu)) { |
| 490 | bts_err = 1; |
| 491 | pebs_err = 1; |
| 492 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 493 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 494 | if (!bts_err && alloc_bts_buffer(cpu)) |
| 495 | bts_err = 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 496 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 497 | if (!pebs_err && alloc_pebs_buffer(cpu)) |
| 498 | pebs_err = 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 499 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 500 | if (bts_err && pebs_err) |
| 501 | break; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 504 | if (bts_err) { |
| 505 | for_each_possible_cpu(cpu) |
| 506 | release_bts_buffer(cpu); |
| 507 | } |
| 508 | |
| 509 | if (pebs_err) { |
| 510 | for_each_possible_cpu(cpu) |
| 511 | release_pebs_buffer(cpu); |
| 512 | } |
| 513 | |
| 514 | if (bts_err && pebs_err) { |
| 515 | for_each_possible_cpu(cpu) |
| 516 | release_ds_buffer(cpu); |
| 517 | } else { |
| 518 | if (x86_pmu.bts && !bts_err) |
| 519 | x86_pmu.bts_active = 1; |
| 520 | |
| 521 | if (x86_pmu.pebs && !pebs_err) |
| 522 | x86_pmu.pebs_active = 1; |
| 523 | |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 524 | for_each_possible_cpu(cpu) { |
| 525 | /* |
| 526 | * Ignores wrmsr_on_cpu() errors for offline CPUs they |
| 527 | * will get this call through intel_pmu_cpu_starting(). |
| 528 | */ |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 529 | init_debug_store_on_cpu(cpu); |
Peter Zijlstra | efe951d | 2018-01-10 19:23:08 +0100 | [diff] [blame] | 530 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 531 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
| 535 | * BTS |
| 536 | */ |
| 537 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 538 | struct event_constraint bts_constraint = |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 539 | EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 540 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 541 | void intel_pmu_enable_bts(u64 config) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 542 | { |
| 543 | unsigned long debugctlmsr; |
| 544 | |
| 545 | debugctlmsr = get_debugctlmsr(); |
| 546 | |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 547 | debugctlmsr |= DEBUGCTLMSR_TR; |
| 548 | debugctlmsr |= DEBUGCTLMSR_BTS; |
Alexander Shishkin | 8062382 | 2015-01-30 12:40:35 +0200 | [diff] [blame] | 549 | if (config & ARCH_PERFMON_EVENTSEL_INT) |
| 550 | debugctlmsr |= DEBUGCTLMSR_BTINT; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 551 | |
| 552 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 553 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 554 | |
| 555 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 556 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 557 | |
| 558 | update_debugctlmsr(debugctlmsr); |
| 559 | } |
| 560 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 561 | void intel_pmu_disable_bts(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 562 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 563 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 564 | unsigned long debugctlmsr; |
| 565 | |
| 566 | if (!cpuc->ds) |
| 567 | return; |
| 568 | |
| 569 | debugctlmsr = get_debugctlmsr(); |
| 570 | |
| 571 | debugctlmsr &= |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 572 | ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | |
| 573 | DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 574 | |
| 575 | update_debugctlmsr(debugctlmsr); |
| 576 | } |
| 577 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 578 | int intel_pmu_drain_bts_buffer(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 579 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 580 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 581 | struct debug_store *ds = cpuc->ds; |
| 582 | struct bts_record { |
| 583 | u64 from; |
| 584 | u64 to; |
| 585 | u64 flags; |
| 586 | }; |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 587 | struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS]; |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 588 | struct bts_record *at, *base, *top; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 589 | struct perf_output_handle handle; |
| 590 | struct perf_event_header header; |
| 591 | struct perf_sample_data data; |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 592 | unsigned long skip = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 593 | struct pt_regs regs; |
| 594 | |
| 595 | if (!event) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 596 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 597 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 598 | if (!x86_pmu.bts_active) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 599 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 600 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 601 | base = (struct bts_record *)(unsigned long)ds->bts_buffer_base; |
| 602 | top = (struct bts_record *)(unsigned long)ds->bts_index; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 603 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 604 | if (top <= base) |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 605 | return 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 606 | |
Stephane Eranian | 0e48026 | 2013-03-19 16:10:38 +0100 | [diff] [blame] | 607 | memset(®s, 0, sizeof(regs)); |
| 608 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 609 | ds->bts_index = ds->bts_buffer_base; |
| 610 | |
Robert Richter | fd0d000 | 2012-04-02 20:19:08 +0200 | [diff] [blame] | 611 | perf_sample_data_init(&data, 0, event->hw.last_period); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 612 | |
| 613 | /* |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 614 | * BTS leaks kernel addresses in branches across the cpl boundary, |
| 615 | * such as traps or system calls, so unless the user is asking for |
| 616 | * kernel tracing (and right now it's not possible), we'd need to |
| 617 | * filter them out. But first we need to count how many of those we |
| 618 | * have in the current batch. This is an extra O(n) pass, however, |
| 619 | * it's much faster than the other one especially considering that |
| 620 | * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the |
| 621 | * alloc_bts_buffer()). |
| 622 | */ |
| 623 | for (at = base; at < top; at++) { |
| 624 | /* |
| 625 | * Note that right now *this* BTS code only works if |
| 626 | * attr::exclude_kernel is set, but let's keep this extra |
| 627 | * check here in case that changes. |
| 628 | */ |
| 629 | if (event->attr.exclude_kernel && |
| 630 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 631 | skip++; |
| 632 | } |
| 633 | |
| 634 | /* |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 635 | * Prepare a generic sample, i.e. fill in the invariant fields. |
| 636 | * We will overwrite the from and to address before we output |
| 637 | * the sample. |
| 638 | */ |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 639 | rcu_read_lock(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 640 | perf_prepare_sample(&header, &data, event, ®s); |
| 641 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 642 | if (perf_output_begin(&handle, event, header.size * |
| 643 | (top - base - skip))) |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 644 | goto unlock; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 645 | |
Alexander Shishkin | a09d31f4 | 2015-08-31 17:09:27 +0300 | [diff] [blame] | 646 | for (at = base; at < top; at++) { |
| 647 | /* Filter out any records that contain kernel addresses. */ |
| 648 | if (event->attr.exclude_kernel && |
| 649 | (kernel_ip(at->from) || kernel_ip(at->to))) |
| 650 | continue; |
| 651 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 652 | data.ip = at->from; |
| 653 | data.addr = at->to; |
| 654 | |
| 655 | perf_output_sample(&handle, &header, &data, event); |
| 656 | } |
| 657 | |
| 658 | perf_output_end(&handle); |
| 659 | |
| 660 | /* There's new data available. */ |
| 661 | event->hw.interrupts++; |
| 662 | event->pending_kill = POLL_IN; |
Peter Zijlstra | e8d8a90 | 2016-03-18 17:31:27 +0100 | [diff] [blame] | 663 | unlock: |
| 664 | rcu_read_unlock(); |
Stephane Eranian | b0b2072 | 2010-09-10 13:28:01 +0200 | [diff] [blame] | 665 | return 1; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Yan, Zheng | 9c964ef | 2015-05-06 15:33:51 -0400 | [diff] [blame] | 668 | static inline void intel_pmu_drain_pebs_buffer(void) |
| 669 | { |
| 670 | struct pt_regs regs; |
| 671 | |
| 672 | x86_pmu.drain_pebs(®s); |
| 673 | } |
| 674 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 675 | /* |
| 676 | * PEBS |
| 677 | */ |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 678 | struct event_constraint intel_core2_pebs_event_constraints[] = { |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 679 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 680 | INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ |
| 681 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ |
| 682 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ |
| 683 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 684 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 685 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 686 | EVENT_CONSTRAINT_END |
| 687 | }; |
| 688 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 689 | struct event_constraint intel_atom_pebs_event_constraints[] = { |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 690 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */ |
| 691 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */ |
| 692 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 693 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 694 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01), |
Stephane Eranian | 673d188 | 2015-12-03 21:03:10 +0100 | [diff] [blame] | 695 | /* Allow all events as PEBS with no flags */ |
| 696 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
Stephane Eranian | 17e3162 | 2011-03-02 17:05:01 +0200 | [diff] [blame] | 697 | EVENT_CONSTRAINT_END |
| 698 | }; |
| 699 | |
Yan, Zheng | 1fa6418 | 2013-07-18 17:02:24 +0800 | [diff] [blame] | 700 | struct event_constraint intel_slm_pebs_event_constraints[] = { |
Kan Liang | 3363673 | 2015-01-12 17:42:21 +0000 | [diff] [blame] | 701 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 702 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1), |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 703 | /* Allow all events as PEBS with no flags */ |
| 704 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
Yan, Zheng | 1fa6418 | 2013-07-18 17:02:24 +0800 | [diff] [blame] | 705 | EVENT_CONSTRAINT_END |
| 706 | }; |
| 707 | |
Kan Liang | 8b92c3a | 2016-04-15 00:42:47 -0700 | [diff] [blame] | 708 | struct event_constraint intel_glm_pebs_event_constraints[] = { |
| 709 | /* Allow all events as PEBS with no flags */ |
| 710 | INTEL_ALL_EVENT_CONSTRAINT(0, 0x1), |
| 711 | EVENT_CONSTRAINT_END |
| 712 | }; |
| 713 | |
Kan Liang | dd0b06b | 2017-07-12 09:44:23 -0400 | [diff] [blame] | 714 | struct event_constraint intel_glp_pebs_event_constraints[] = { |
| 715 | /* Allow all events as PEBS with no flags */ |
| 716 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 717 | EVENT_CONSTRAINT_END |
| 718 | }; |
| 719 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 720 | struct event_constraint intel_nehalem_pebs_event_constraints[] = { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 721 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 722 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 723 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 724 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */ |
Lin Ming | 7d5d02d | 2011-03-09 23:21:29 +0800 | [diff] [blame] | 725 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 726 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 727 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */ |
| 728 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 729 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 730 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 731 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 732 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 733 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Stephane Eranian | 17e3162 | 2011-03-02 17:05:01 +0200 | [diff] [blame] | 734 | EVENT_CONSTRAINT_END |
| 735 | }; |
| 736 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 737 | struct event_constraint intel_westmere_pebs_event_constraints[] = { |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 738 | INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 739 | INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */ |
| 740 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */ |
| 741 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */ |
Lin Ming | 7d5d02d | 2011-03-09 23:21:29 +0800 | [diff] [blame] | 742 | INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */ |
Andi Kleen | af4bdcf | 2014-09-24 07:34:48 -0700 | [diff] [blame] | 743 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */ |
| 744 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */ |
| 745 | INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */ |
| 746 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */ |
| 747 | INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 748 | INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */ |
Peter Zijlstra | 517e634 | 2015-04-11 12:16:22 +0200 | [diff] [blame] | 749 | /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */ |
| 750 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 751 | EVENT_CONSTRAINT_END |
| 752 | }; |
| 753 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 754 | struct event_constraint intel_snb_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 755 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 756 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 757 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 758 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 759 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 760 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 761 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 762 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 763 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 764 | /* Allow all events as PEBS with no flags */ |
| 765 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Lin Ming | b06b3d4 | 2011-03-02 21:27:04 +0800 | [diff] [blame] | 766 | EVENT_CONSTRAINT_END |
| 767 | }; |
| 768 | |
Stephane Eranian | 20a36e3 | 2012-09-11 01:07:01 +0200 | [diff] [blame] | 769 | struct event_constraint intel_ivb_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 770 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 771 | INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */ |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 772 | INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 773 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 774 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 775 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 776 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 777 | INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */ |
| 778 | INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 779 | INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */ |
| 780 | INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 781 | /* Allow all events as PEBS with no flags */ |
| 782 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Stephane Eranian | 20a36e3 | 2012-09-11 01:07:01 +0200 | [diff] [blame] | 783 | EVENT_CONSTRAINT_END |
| 784 | }; |
| 785 | |
Andi Kleen | 3044318 | 2013-06-17 17:36:49 -0700 | [diff] [blame] | 786 | struct event_constraint intel_hsw_pebs_event_constraints[] = { |
Andi Kleen | 0dbc947 | 2014-09-24 07:34:47 -0700 | [diff] [blame] | 787 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 788 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 789 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 790 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 791 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 792 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 793 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
Maria Dimakopoulou | b63b4b4 | 2014-11-17 20:07:00 +0100 | [diff] [blame] | 794 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 795 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 796 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 797 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 798 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 799 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 800 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 801 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 802 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 803 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
Andi Kleen | 86a0446 | 2014-08-11 21:27:10 +0200 | [diff] [blame] | 804 | /* Allow all events as PEBS with no flags */ |
| 805 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
Andi Kleen | 3044318 | 2013-06-17 17:36:49 -0700 | [diff] [blame] | 806 | EVENT_CONSTRAINT_END |
| 807 | }; |
| 808 | |
Stephane Eranian | b3e6246 | 2016-03-03 20:50:42 +0100 | [diff] [blame] | 809 | struct event_constraint intel_bdw_pebs_event_constraints[] = { |
| 810 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */ |
| 811 | INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 812 | /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */ |
| 813 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf), |
| 814 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 815 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
| 816 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */ |
| 817 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */ |
| 818 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */ |
| 819 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */ |
| 820 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */ |
| 821 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */ |
| 822 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */ |
| 823 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */ |
| 824 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */ |
| 825 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */ |
| 826 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */ |
| 827 | /* Allow all events as PEBS with no flags */ |
| 828 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 829 | EVENT_CONSTRAINT_END |
| 830 | }; |
| 831 | |
| 832 | |
Andi Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 833 | struct event_constraint intel_skl_pebs_event_constraints[] = { |
| 834 | INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */ |
Andi Kleen | 7246976 | 2015-12-04 03:50:52 -0800 | [diff] [blame] | 835 | /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */ |
| 836 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2), |
Andi Kleen | 442f5c7 | 2015-12-04 03:50:32 -0800 | [diff] [blame] | 837 | /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */ |
| 838 | INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f), |
Andi Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 839 | INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */ |
| 840 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */ |
| 841 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */ |
| 842 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */ |
| 843 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */ |
| 844 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */ |
| 845 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */ |
| 846 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */ |
| 847 | INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */ |
| 848 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */ |
| 849 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */ |
| 850 | INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */ |
| 851 | /* Allow all events as PEBS with no flags */ |
| 852 | INTEL_ALL_EVENT_CONSTRAINT(0, 0xf), |
| 853 | EVENT_CONSTRAINT_END |
| 854 | }; |
| 855 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 856 | struct event_constraint *intel_pebs_constraints(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 857 | { |
| 858 | struct event_constraint *c; |
| 859 | |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 860 | if (!event->attr.precise_ip) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 861 | return NULL; |
| 862 | |
| 863 | if (x86_pmu.pebs_constraints) { |
| 864 | for_each_event_constraint(c, x86_pmu.pebs_constraints) { |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 865 | if ((event->hw.config & c->cmask) == c->code) { |
| 866 | event->hw.flags |= c->flags; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 867 | return c; |
Stephane Eranian | 9fac2cf | 2013-01-24 16:10:27 +0100 | [diff] [blame] | 868 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
| 872 | return &emptyconstraint; |
| 873 | } |
| 874 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 875 | /* |
| 876 | * We need the sched_task callback even for per-cpu events when we use |
| 877 | * the large interrupt threshold, such that we can provide PID and TID |
| 878 | * to PEBS samples. |
| 879 | */ |
| 880 | static inline bool pebs_needs_sched_cb(struct cpu_hw_events *cpuc) |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 881 | { |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 882 | return cpuc->n_pebs && (cpuc->n_pebs == cpuc->n_large_pebs); |
| 883 | } |
| 884 | |
Jiri Olsa | df6c3db | 2017-07-19 09:52:47 +0200 | [diff] [blame] | 885 | void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in) |
| 886 | { |
| 887 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 888 | |
| 889 | if (!sched_in && pebs_needs_sched_cb(cpuc)) |
| 890 | intel_pmu_drain_pebs_buffer(); |
| 891 | } |
| 892 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 893 | static inline void pebs_update_threshold(struct cpu_hw_events *cpuc) |
| 894 | { |
| 895 | struct debug_store *ds = cpuc->ds; |
| 896 | u64 threshold; |
| 897 | |
| 898 | if (cpuc->n_pebs == cpuc->n_large_pebs) { |
| 899 | threshold = ds->pebs_absolute_maximum - |
| 900 | x86_pmu.max_pebs_events * x86_pmu.pebs_record_size; |
| 901 | } else { |
| 902 | threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size; |
| 903 | } |
| 904 | |
| 905 | ds->pebs_interrupt_threshold = threshold; |
| 906 | } |
| 907 | |
| 908 | static void |
| 909 | pebs_update_state(bool needed_cb, struct cpu_hw_events *cpuc, struct pmu *pmu) |
| 910 | { |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 911 | /* |
| 912 | * Make sure we get updated with the first PEBS |
| 913 | * event. It will trigger also during removal, but |
| 914 | * that does not hurt: |
| 915 | */ |
| 916 | bool update = cpuc->n_pebs == 1; |
| 917 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 918 | if (needed_cb != pebs_needs_sched_cb(cpuc)) { |
| 919 | if (!needed_cb) |
| 920 | perf_sched_cb_inc(pmu); |
| 921 | else |
| 922 | perf_sched_cb_dec(pmu); |
| 923 | |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 924 | update = true; |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 925 | } |
Jiri Olsa | b6a32f0 | 2016-08-18 11:09:52 +0200 | [diff] [blame] | 926 | |
| 927 | if (update) |
| 928 | pebs_update_threshold(cpuc); |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Peter Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 931 | void intel_pmu_pebs_add(struct perf_event *event) |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 932 | { |
| 933 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 934 | struct hw_perf_event *hwc = &event->hw; |
| 935 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 936 | |
| 937 | cpuc->n_pebs++; |
| 938 | if (hwc->flags & PERF_X86_EVENT_FREERUNNING) |
| 939 | cpuc->n_large_pebs++; |
| 940 | |
| 941 | pebs_update_state(needed_cb, cpuc, event->ctx->pmu); |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 942 | } |
| 943 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 944 | void intel_pmu_pebs_enable(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 945 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 946 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 947 | struct hw_perf_event *hwc = &event->hw; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 948 | struct debug_store *ds = cpuc->ds; |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 949 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 950 | hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; |
| 951 | |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 952 | cpuc->pebs_enabled |= 1ULL << hwc->idx; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 953 | |
| 954 | if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) |
| 955 | cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32); |
Stephane Eranian | 9ad64c0 | 2013-01-24 16:10:34 +0100 | [diff] [blame] | 956 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
| 957 | cpuc->pebs_enabled |= 1ULL << 63; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 958 | |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 959 | /* |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 960 | * Use auto-reload if possible to save a MSR write in the PMI. |
| 961 | * This must be done in pmu::start(), because PERF_EVENT_IOC_PERIOD. |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 962 | */ |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 963 | if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { |
| 964 | ds->pebs_event_reset[hwc->idx] = |
| 965 | (u64)(-hwc->sample_period) & x86_pmu.cntval_mask; |
Jiri Olsa | dc853e2 | 2017-07-14 18:35:51 +0200 | [diff] [blame] | 966 | } else { |
| 967 | ds->pebs_event_reset[hwc->idx] = 0; |
Yan, Zheng | 851559e | 2015-05-06 15:33:47 -0400 | [diff] [blame] | 968 | } |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 969 | } |
Yan, Zheng | 3569c0d | 2015-05-06 15:33:50 -0400 | [diff] [blame] | 970 | |
Peter Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 971 | void intel_pmu_pebs_del(struct perf_event *event) |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 972 | { |
| 973 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 974 | struct hw_perf_event *hwc = &event->hw; |
| 975 | bool needed_cb = pebs_needs_sched_cb(cpuc); |
| 976 | |
| 977 | cpuc->n_pebs--; |
| 978 | if (hwc->flags & PERF_X86_EVENT_FREERUNNING) |
| 979 | cpuc->n_large_pebs--; |
| 980 | |
| 981 | pebs_update_state(needed_cb, cpuc, event->ctx->pmu); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 984 | void intel_pmu_pebs_disable(struct perf_event *event) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 985 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 986 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 987 | struct hw_perf_event *hwc = &event->hw; |
Liang, Kan | 2a853e1 | 2015-07-03 20:08:27 +0000 | [diff] [blame] | 988 | |
Peter Zijlstra | 09e61b4f | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 989 | if (cpuc->n_pebs == cpuc->n_large_pebs) |
Liang, Kan | 2a853e1 | 2015-07-03 20:08:27 +0000 | [diff] [blame] | 990 | intel_pmu_drain_pebs_buffer(); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 991 | |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 992 | cpuc->pebs_enabled &= ~(1ULL << hwc->idx); |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 993 | |
Peter Zijlstra | b371b59 | 2015-05-21 10:57:13 +0200 | [diff] [blame] | 994 | if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT) |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 995 | cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32)); |
Peter Zijlstra | b371b59 | 2015-05-21 10:57:13 +0200 | [diff] [blame] | 996 | else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST) |
Stephane Eranian | 983433b | 2013-06-21 16:20:41 +0200 | [diff] [blame] | 997 | cpuc->pebs_enabled &= ~(1ULL << 63); |
| 998 | |
Peter Zijlstra | 4807e3d | 2010-03-06 13:47:07 +0100 | [diff] [blame] | 999 | if (cpuc->enabled) |
Peter Zijlstra | ad0e6cf | 2010-03-06 19:49:06 +0100 | [diff] [blame] | 1000 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1001 | |
| 1002 | hwc->config |= ARCH_PERFMON_EVENTSEL_INT; |
| 1003 | } |
| 1004 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 1005 | void intel_pmu_pebs_enable_all(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1006 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1007 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1008 | |
| 1009 | if (cpuc->pebs_enabled) |
| 1010 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); |
| 1011 | } |
| 1012 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 1013 | void intel_pmu_pebs_disable_all(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1014 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1015 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1016 | |
| 1017 | if (cpuc->pebs_enabled) |
| 1018 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
| 1019 | } |
| 1020 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1021 | static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) |
| 1022 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1023 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1024 | unsigned long from = cpuc->lbr_entries[0].from; |
| 1025 | unsigned long old_to, to = cpuc->lbr_entries[0].to; |
| 1026 | unsigned long ip = regs->ip; |
Peter Zijlstra | 57d1c0c | 2011-10-07 13:36:40 +0200 | [diff] [blame] | 1027 | int is_64bit = 0; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1028 | void *kaddr; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1029 | int size; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1030 | |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 1031 | /* |
| 1032 | * We don't need to fixup if the PEBS assist is fault like |
| 1033 | */ |
| 1034 | if (!x86_pmu.intel_cap.pebs_trap) |
| 1035 | return 1; |
| 1036 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 1037 | /* |
| 1038 | * No LBR entry, no basic block, no rewinding |
| 1039 | */ |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1040 | if (!cpuc->lbr_stack.nr || !from || !to) |
| 1041 | return 0; |
| 1042 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 1043 | /* |
| 1044 | * Basic blocks should never cross user/kernel boundaries |
| 1045 | */ |
| 1046 | if (kernel_ip(ip) != kernel_ip(to)) |
| 1047 | return 0; |
| 1048 | |
| 1049 | /* |
| 1050 | * unsigned math, either ip is before the start (impossible) or |
| 1051 | * the basic block is larger than 1 page (sanity) |
| 1052 | */ |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1053 | if ((ip - to) > PEBS_FIXUP_SIZE) |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1054 | return 0; |
| 1055 | |
| 1056 | /* |
| 1057 | * We sampled a branch insn, rewind using the LBR stack |
| 1058 | */ |
| 1059 | if (ip == to) { |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 1060 | set_linear_ip(regs, from); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1061 | return 1; |
| 1062 | } |
| 1063 | |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1064 | size = ip - to; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1065 | if (!kernel_ip(ip)) { |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1066 | int bytes; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1067 | u8 *buf = this_cpu_read(insn_buffer); |
| 1068 | |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1069 | /* 'size' must fit our buffer, see above */ |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1070 | bytes = copy_from_user_nmi(buf, (void __user *)to, size); |
Peter Zijlstra | 0a19684 | 2013-10-30 21:16:22 +0100 | [diff] [blame] | 1071 | if (bytes != 0) |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1072 | return 0; |
| 1073 | |
| 1074 | kaddr = buf; |
| 1075 | } else { |
| 1076 | kaddr = (void *)to; |
| 1077 | } |
| 1078 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1079 | do { |
| 1080 | struct insn insn; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1081 | |
| 1082 | old_to = to; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1083 | |
Peter Zijlstra | 57d1c0c | 2011-10-07 13:36:40 +0200 | [diff] [blame] | 1084 | #ifdef CONFIG_X86_64 |
| 1085 | is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32); |
| 1086 | #endif |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1087 | insn_init(&insn, kaddr, size, is_64bit); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1088 | insn_get_length(&insn); |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1089 | /* |
| 1090 | * Make sure there was not a problem decoding the |
| 1091 | * instruction and getting the length. This is |
| 1092 | * doubly important because we have an infinite |
| 1093 | * loop if insn.length=0. |
| 1094 | */ |
| 1095 | if (!insn.length) |
| 1096 | break; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1097 | |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1098 | to += insn.length; |
Peter Zijlstra | 9536c8d | 2013-10-15 12:14:04 +0200 | [diff] [blame] | 1099 | kaddr += insn.length; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 1100 | size -= insn.length; |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1101 | } while (to < ip); |
| 1102 | |
| 1103 | if (to == ip) { |
Peter Zijlstra | d07bdfd | 2012-07-10 09:42:15 +0200 | [diff] [blame] | 1104 | set_linear_ip(regs, old_to); |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1105 | return 1; |
| 1106 | } |
| 1107 | |
Peter Zijlstra | a562b18 | 2010-03-05 16:29:14 +0100 | [diff] [blame] | 1108 | /* |
| 1109 | * Even though we decoded the basic block, the instruction stream |
| 1110 | * never matched the given IP, either the TO or the IP got corrupted. |
| 1111 | */ |
Peter Zijlstra | ef21f68 | 2010-03-03 13:12:23 +0100 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1115 | static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs) |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 1116 | { |
| 1117 | if (pebs->tsx_tuning) { |
| 1118 | union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning }; |
| 1119 | return tsx.cycles_last_block; |
| 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1124 | static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs) |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1125 | { |
| 1126 | u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32; |
| 1127 | |
| 1128 | /* For RTM XABORTs also log the abort code from AX */ |
| 1129 | if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1)) |
| 1130 | txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; |
| 1131 | return txn; |
| 1132 | } |
| 1133 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1134 | static void setup_pebs_sample_data(struct perf_event *event, |
| 1135 | struct pt_regs *iregs, void *__pebs, |
| 1136 | struct perf_sample_data *data, |
| 1137 | struct pt_regs *regs) |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1138 | { |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1139 | #define PERF_X86_EVENT_PEBS_HSW_PREC \ |
| 1140 | (PERF_X86_EVENT_PEBS_ST_HSW | \ |
| 1141 | PERF_X86_EVENT_PEBS_LD_HSW | \ |
| 1142 | PERF_X86_EVENT_PEBS_NA_HSW) |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1143 | /* |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1144 | * We cast to the biggest pebs_record but are careful not to |
| 1145 | * unconditionally access the 'extra' entries. |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1146 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1147 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1148 | struct pebs_record_skl *pebs = __pebs; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1149 | u64 sample_type; |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1150 | int fll, fst, dsrc; |
| 1151 | int fl = event->hw.flags; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1152 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1153 | if (pebs == NULL) |
| 1154 | return; |
| 1155 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1156 | sample_type = event->attr.sample_type; |
| 1157 | dsrc = sample_type & PERF_SAMPLE_DATA_SRC; |
| 1158 | |
| 1159 | fll = fl & PERF_X86_EVENT_PEBS_LDLAT; |
| 1160 | fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC); |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1161 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1162 | perf_sample_data_init(data, 0, event->hw.last_period); |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1163 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1164 | data->period = event->hw.last_period; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1165 | |
| 1166 | /* |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1167 | * Use latency for weight (only avail with PEBS-LL) |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1168 | */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1169 | if (fll && (sample_type & PERF_SAMPLE_WEIGHT)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1170 | data->weight = pebs->lat; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1171 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1172 | /* |
| 1173 | * data.data_src encodes the data source |
| 1174 | */ |
| 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); |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1183 | data->data_src.val = val; |
Stephane Eranian | f20093e | 2013-01-24 16:10:32 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1186 | /* |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 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(). |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1190 | * |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 1191 | * In the simple case fix up only the IP for PERF_SAMPLE_IP. |
| 1192 | * |
| 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. |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1197 | */ |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1198 | *regs = *iregs; |
| 1199 | regs->flags = pebs->flags; |
| 1200 | set_linear_ip(regs, pebs->ip); |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1201 | |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1202 | if (sample_type & PERF_SAMPLE_REGS_INTR) { |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1203 | regs->ax = pebs->ax; |
| 1204 | regs->bx = pebs->bx; |
| 1205 | regs->cx = pebs->cx; |
| 1206 | regs->dx = pebs->dx; |
| 1207 | regs->si = pebs->si; |
| 1208 | regs->di = pebs->di; |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1209 | |
Peter Zijlstra | b800058 | 2016-11-17 18:17:31 +0100 | [diff] [blame] | 1210 | /* |
| 1211 | * Per the above; only set BP,SP if we don't need callchains. |
| 1212 | * |
| 1213 | * XXX: does this make sense? |
| 1214 | */ |
| 1215 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { |
| 1216 | regs->bp = pebs->bp; |
| 1217 | regs->sp = pebs->sp; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * Preserve PERF_EFLAGS_VM from set_linear_ip(). |
| 1222 | */ |
| 1223 | regs->flags = pebs->flags | (regs->flags & PERF_EFLAGS_VM); |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1224 | #ifndef CONFIG_X86_32 |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1225 | regs->r8 = pebs->r8; |
| 1226 | regs->r9 = pebs->r9; |
| 1227 | regs->r10 = pebs->r10; |
| 1228 | regs->r11 = pebs->r11; |
| 1229 | regs->r12 = pebs->r12; |
| 1230 | regs->r13 = pebs->r13; |
| 1231 | regs->r14 = pebs->r14; |
| 1232 | regs->r15 = pebs->r15; |
Stephane Eranian | aea4855 | 2014-09-24 13:48:38 +0200 | [diff] [blame] | 1233 | #endif |
| 1234 | } |
| 1235 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1236 | if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) { |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1237 | regs->ip = pebs->real_ip; |
| 1238 | regs->flags |= PERF_EFLAGS_EXACT; |
| 1239 | } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs)) |
| 1240 | regs->flags |= PERF_EFLAGS_EXACT; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1241 | else |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1242 | regs->flags &= ~PERF_EFLAGS_EXACT; |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1243 | |
Kan Liang | fc7ce9c | 2017-08-28 20:52:49 -0400 | [diff] [blame] | 1244 | if ((sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR)) && |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1245 | x86_pmu.intel_cap.pebs_format >= 1) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1246 | data->addr = pebs->dla; |
Andi Kleen | f9134f3 | 2013-06-17 17:36:52 -0700 | [diff] [blame] | 1247 | |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1248 | if (x86_pmu.intel_cap.pebs_format >= 2) { |
| 1249 | /* Only set the TSX weight when no memory weight. */ |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1250 | if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1251 | data->weight = intel_hsw_weight(pebs); |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1252 | |
Stephane Eranian | c8aab2e | 2014-08-11 21:27:13 +0200 | [diff] [blame] | 1253 | if (sample_type & PERF_SAMPLE_TRANSACTION) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1254 | data->txn = intel_hsw_transaction(pebs); |
Andi Kleen | a405bad | 2013-09-20 07:40:40 -0700 | [diff] [blame] | 1255 | } |
Andi Kleen | 748e86a | 2013-09-05 20:37:39 -0700 | [diff] [blame] | 1256 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1257 | /* |
| 1258 | * v3 supplies an accurate time stamp, so we use that |
| 1259 | * for the time stamp. |
| 1260 | * |
| 1261 | * We can only do this for the default trace clock. |
| 1262 | */ |
| 1263 | if (x86_pmu.intel_cap.pebs_format >= 3 && |
| 1264 | event->attr.use_clockid == 0) |
| 1265 | data->time = native_sched_clock_from_tsc(pebs->tsc); |
| 1266 | |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1267 | if (has_branch_stack(event)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1268 | data->br_stack = &cpuc->lbr_stack; |
| 1269 | } |
| 1270 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1271 | static inline void * |
| 1272 | get_next_pebs_record_by_bit(void *base, void *top, int bit) |
| 1273 | { |
| 1274 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 1275 | void *at; |
| 1276 | u64 pebs_status; |
| 1277 | |
Stephane Eranian | 1424a09 | 2015-12-03 23:33:18 +0100 | [diff] [blame] | 1278 | /* |
| 1279 | * fmt0 does not have a status bitfield (does not use |
| 1280 | * perf_record_nhm format) |
| 1281 | */ |
| 1282 | if (x86_pmu.intel_cap.pebs_format < 1) |
| 1283 | return base; |
| 1284 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1285 | if (base == NULL) |
| 1286 | return NULL; |
| 1287 | |
| 1288 | for (at = base; at < top; at += x86_pmu.pebs_record_size) { |
| 1289 | struct pebs_record_nhm *p = at; |
| 1290 | |
| 1291 | if (test_bit(bit, (unsigned long *)&p->status)) { |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1292 | /* PEBS v3 has accurate status bits */ |
| 1293 | if (x86_pmu.intel_cap.pebs_format >= 3) |
| 1294 | return at; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1295 | |
| 1296 | if (p->status == (1 << bit)) |
| 1297 | return at; |
| 1298 | |
| 1299 | /* clear non-PEBS bit and re-check */ |
| 1300 | pebs_status = p->status & cpuc->pebs_enabled; |
Kan Liang | fd583ad | 2017-04-04 15:14:06 -0400 | [diff] [blame] | 1301 | pebs_status &= PEBS_COUNTER_MASK; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1302 | if (pebs_status == (1 << bit)) |
| 1303 | return at; |
| 1304 | } |
| 1305 | } |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
Kan Liang | 5bee2cc | 2018-02-12 14:20:33 -0800 | [diff] [blame^] | 1309 | void intel_pmu_auto_reload_read(struct perf_event *event) |
| 1310 | { |
| 1311 | WARN_ON(!(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)); |
| 1312 | |
| 1313 | perf_pmu_disable(event->pmu); |
| 1314 | intel_pmu_drain_pebs_buffer(); |
| 1315 | perf_pmu_enable(event->pmu); |
| 1316 | } |
| 1317 | |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1318 | /* |
| 1319 | * Special variant of intel_pmu_save_and_restart() for auto-reload. |
| 1320 | */ |
| 1321 | static int |
| 1322 | intel_pmu_save_and_restart_reload(struct perf_event *event, int count) |
| 1323 | { |
| 1324 | struct hw_perf_event *hwc = &event->hw; |
| 1325 | int shift = 64 - x86_pmu.cntval_bits; |
| 1326 | u64 period = hwc->sample_period; |
| 1327 | u64 prev_raw_count, new_raw_count; |
| 1328 | s64 new, old; |
| 1329 | |
| 1330 | WARN_ON(!period); |
| 1331 | |
| 1332 | /* |
| 1333 | * drain_pebs() only happens when the PMU is disabled. |
| 1334 | */ |
| 1335 | WARN_ON(this_cpu_read(cpu_hw_events.enabled)); |
| 1336 | |
| 1337 | prev_raw_count = local64_read(&hwc->prev_count); |
| 1338 | rdpmcl(hwc->event_base_rdpmc, new_raw_count); |
| 1339 | local64_set(&hwc->prev_count, new_raw_count); |
| 1340 | |
| 1341 | /* |
| 1342 | * Since the counter increments a negative counter value and |
| 1343 | * overflows on the sign switch, giving the interval: |
| 1344 | * |
| 1345 | * [-period, 0] |
| 1346 | * |
| 1347 | * the difference between two consequtive reads is: |
| 1348 | * |
| 1349 | * A) value2 - value1; |
| 1350 | * when no overflows have happened in between, |
| 1351 | * |
| 1352 | * B) (0 - value1) + (value2 - (-period)); |
| 1353 | * when one overflow happened in between, |
| 1354 | * |
| 1355 | * C) (0 - value1) + (n - 1) * (period) + (value2 - (-period)); |
| 1356 | * when @n overflows happened in between. |
| 1357 | * |
| 1358 | * Here A) is the obvious difference, B) is the extension to the |
| 1359 | * discrete interval, where the first term is to the top of the |
| 1360 | * interval and the second term is from the bottom of the next |
| 1361 | * interval and C) the extension to multiple intervals, where the |
| 1362 | * middle term is the whole intervals covered. |
| 1363 | * |
| 1364 | * An equivalent of C, by reduction, is: |
| 1365 | * |
| 1366 | * value2 - value1 + n * period |
| 1367 | */ |
| 1368 | new = ((s64)(new_raw_count << shift) >> shift); |
| 1369 | old = ((s64)(prev_raw_count << shift) >> shift); |
| 1370 | local64_add(new - old + count * period, &event->count); |
| 1371 | |
| 1372 | perf_event_update_userpage(event); |
| 1373 | |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1377 | static void __intel_pmu_pebs_event(struct perf_event *event, |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1378 | struct pt_regs *iregs, |
| 1379 | void *base, void *top, |
| 1380 | int bit, int count) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1381 | { |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1382 | struct hw_perf_event *hwc = &event->hw; |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1383 | struct perf_sample_data data; |
| 1384 | struct pt_regs regs; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1385 | void *at = get_next_pebs_record_by_bit(base, top, bit); |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1386 | |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1387 | if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) { |
| 1388 | /* |
| 1389 | * Now, auto-reload is only enabled in fixed period mode. |
| 1390 | * The reload value is always hwc->sample_period. |
| 1391 | * May need to change it, if auto-reload is enabled in |
| 1392 | * freq mode later. |
| 1393 | */ |
| 1394 | intel_pmu_save_and_restart_reload(event, count); |
| 1395 | } else if (!intel_pmu_save_and_restart(event)) |
Yan, Zheng | 43cf763 | 2015-05-06 15:33:48 -0400 | [diff] [blame] | 1396 | return; |
| 1397 | |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1398 | while (count > 1) { |
| 1399 | setup_pebs_sample_data(event, iregs, at, &data, ®s); |
| 1400 | perf_event_output(event, &data, ®s); |
| 1401 | at += x86_pmu.pebs_record_size; |
| 1402 | at = get_next_pebs_record_by_bit(at, top, bit); |
| 1403 | count--; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1404 | } |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1405 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1406 | setup_pebs_sample_data(event, iregs, at, &data, ®s); |
| 1407 | |
| 1408 | /* |
| 1409 | * All but the last records are processed. |
| 1410 | * The last one is left to be able to call the overflow handler. |
| 1411 | */ |
| 1412 | if (perf_event_overflow(event, &data, ®s)) { |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 1413 | x86_pmu_stop(event, 0); |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1414 | return; |
| 1415 | } |
| 1416 | |
Peter Zijlstra | 2b0b5c6 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1419 | static void intel_pmu_drain_pebs_core(struct pt_regs *iregs) |
| 1420 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1421 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1422 | struct debug_store *ds = cpuc->ds; |
| 1423 | struct perf_event *event = cpuc->events[0]; /* PMC0 only */ |
| 1424 | struct pebs_record_core *at, *top; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1425 | int n; |
| 1426 | |
Peter Zijlstra | 6809b6e | 2010-10-19 14:22:50 +0200 | [diff] [blame] | 1427 | if (!x86_pmu.pebs_active) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1428 | return; |
| 1429 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1430 | at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; |
| 1431 | top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; |
| 1432 | |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1433 | /* |
| 1434 | * Whatever else happens, drain the thing |
| 1435 | */ |
| 1436 | ds->pebs_index = ds->pebs_buffer_base; |
| 1437 | |
| 1438 | if (!test_bit(0, cpuc->active_mask)) |
Peter Zijlstra | 8f4aebd | 2010-03-06 13:26:11 +0100 | [diff] [blame] | 1439 | return; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1440 | |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1441 | WARN_ON_ONCE(!event); |
| 1442 | |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1443 | if (!event->attr.precise_ip) |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1444 | return; |
| 1445 | |
Stephane Eranian | 1424a09 | 2015-12-03 23:33:18 +0100 | [diff] [blame] | 1446 | n = top - at; |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1447 | if (n <= 0) { |
| 1448 | if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) |
| 1449 | intel_pmu_save_and_restart_reload(event, 0); |
Peter Zijlstra | d80c750 | 2010-03-09 11:41:02 +0100 | [diff] [blame] | 1450 | return; |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1451 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1452 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1453 | __intel_pmu_pebs_event(event, iregs, at, top, 0, n); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1454 | } |
| 1455 | |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1456 | static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1457 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1458 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1459 | struct debug_store *ds = cpuc->ds; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1460 | struct perf_event *event; |
| 1461 | void *base, *at, *top; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1462 | short counts[MAX_PEBS_EVENTS] = {}; |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1463 | short error[MAX_PEBS_EVENTS] = {}; |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1464 | int bit, i; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1465 | |
| 1466 | if (!x86_pmu.pebs_active) |
| 1467 | return; |
| 1468 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1469 | base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1470 | top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1471 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1472 | ds->pebs_index = ds->pebs_buffer_base; |
| 1473 | |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1474 | if (unlikely(base >= top)) { |
| 1475 | /* |
| 1476 | * The drain_pebs() could be called twice in a short period |
| 1477 | * for auto-reload event in pmu::read(). There are no |
| 1478 | * overflows have happened in between. |
| 1479 | * It needs to call intel_pmu_save_and_restart_reload() to |
| 1480 | * update the event->count for this case. |
| 1481 | */ |
| 1482 | for_each_set_bit(bit, (unsigned long *)&cpuc->pebs_enabled, |
| 1483 | x86_pmu.max_pebs_events) { |
| 1484 | event = cpuc->events[bit]; |
| 1485 | if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD) |
| 1486 | intel_pmu_save_and_restart_reload(event, 0); |
| 1487 | } |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1488 | return; |
Kan Liang | d31fc13 | 2018-02-12 14:20:31 -0800 | [diff] [blame] | 1489 | } |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1490 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1491 | for (at = base; at < top; at += x86_pmu.pebs_record_size) { |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1492 | struct pebs_record_nhm *p = at; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1493 | u64 pebs_status; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1494 | |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1495 | pebs_status = p->status & cpuc->pebs_enabled; |
| 1496 | pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1; |
| 1497 | |
| 1498 | /* PEBS v3 has more accurate status bits */ |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1499 | if (x86_pmu.intel_cap.pebs_format >= 3) { |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1500 | for_each_set_bit(bit, (unsigned long *)&pebs_status, |
| 1501 | x86_pmu.max_pebs_events) |
Peter Zijlstra | a3d8654 | 2015-05-12 15:18:18 +0200 | [diff] [blame] | 1502 | counts[bit]++; |
| 1503 | |
| 1504 | continue; |
| 1505 | } |
| 1506 | |
Andi Kleen | 01330d7 | 2015-12-03 13:22:20 -0800 | [diff] [blame] | 1507 | /* |
| 1508 | * On some CPUs the PEBS status can be zero when PEBS is |
| 1509 | * racing with clearing of GLOBAL_STATUS. |
| 1510 | * |
| 1511 | * Normally we would drop that record, but in the |
| 1512 | * case when there is only a single active PEBS event |
| 1513 | * we can assume it's for that event. |
| 1514 | */ |
| 1515 | if (!pebs_status && cpuc->pebs_enabled && |
| 1516 | !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1))) |
| 1517 | pebs_status = cpuc->pebs_enabled; |
| 1518 | |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1519 | bit = find_first_bit((unsigned long *)&pebs_status, |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1520 | x86_pmu.max_pebs_events); |
Andi Kleen | 957ea1f | 2015-12-03 13:22:19 -0800 | [diff] [blame] | 1521 | if (bit >= x86_pmu.max_pebs_events) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1522 | continue; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1523 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1524 | /* |
| 1525 | * The PEBS hardware does not deal well with the situation |
| 1526 | * when events happen near to each other and multiple bits |
| 1527 | * are set. But it should happen rarely. |
| 1528 | * |
| 1529 | * If these events include one PEBS and multiple non-PEBS |
| 1530 | * events, it doesn't impact PEBS record. The record will |
| 1531 | * be handled normally. (slow path) |
| 1532 | * |
| 1533 | * If these events include two or more PEBS events, the |
| 1534 | * records for the events can be collapsed into a single |
| 1535 | * one, and it's not possible to reconstruct all events |
| 1536 | * that caused the PEBS record. It's called collision. |
| 1537 | * If collision happened, the record will be dropped. |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1538 | */ |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1539 | if (p->status != (1ULL << bit)) { |
| 1540 | for_each_set_bit(i, (unsigned long *)&pebs_status, |
| 1541 | x86_pmu.max_pebs_events) |
| 1542 | error[i]++; |
| 1543 | continue; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1544 | } |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1545 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1546 | counts[bit]++; |
| 1547 | } |
| 1548 | |
| 1549 | for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) { |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1550 | if ((counts[bit] == 0) && (error[bit] == 0)) |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1551 | continue; |
Peter Zijlstra | 75f8085 | 2015-07-15 14:35:46 +0200 | [diff] [blame] | 1552 | |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1553 | event = cpuc->events[bit]; |
Peter Zijlstra | 8ef9b84 | 2016-09-07 14:42:55 +0200 | [diff] [blame] | 1554 | if (WARN_ON_ONCE(!event)) |
| 1555 | continue; |
| 1556 | |
| 1557 | if (WARN_ON_ONCE(!event->attr.precise_ip)) |
| 1558 | continue; |
Yan, Zheng | 2150908 | 2015-05-06 15:33:49 -0400 | [diff] [blame] | 1559 | |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1560 | /* log dropped samples number */ |
Jiri Olsa | 475113d | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 1561 | if (error[bit]) { |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1562 | perf_log_lost_samples(event, error[bit]); |
| 1563 | |
Jiri Olsa | 475113d | 2016-12-28 14:31:03 +0100 | [diff] [blame] | 1564 | if (perf_event_account_interrupt(event)) |
| 1565 | x86_pmu_stop(event, 0); |
| 1566 | } |
| 1567 | |
Kan Liang | f38b0db | 2015-05-10 15:13:14 -0400 | [diff] [blame] | 1568 | if (counts[bit]) { |
| 1569 | __intel_pmu_pebs_event(event, iregs, base, |
| 1570 | top, bit, counts[bit]); |
| 1571 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1572 | } |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * BTS, PEBS probe and setup |
| 1577 | */ |
| 1578 | |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1579 | void __init intel_ds_init(void) |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1580 | { |
| 1581 | /* |
| 1582 | * No support for 32bit formats |
| 1583 | */ |
| 1584 | if (!boot_cpu_has(X86_FEATURE_DTES64)) |
| 1585 | return; |
| 1586 | |
| 1587 | x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); |
| 1588 | x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 1589 | x86_pmu.pebs_buffer_size = PEBS_BUFFER_SIZE; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1590 | if (x86_pmu.pebs) { |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 1591 | char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; |
| 1592 | int format = x86_pmu.intel_cap.pebs_format; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1593 | |
| 1594 | switch (format) { |
| 1595 | case 0: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1596 | pr_cont("PEBS fmt0%c, ", pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1597 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); |
Jiri Olsa | e72daf3 | 2016-03-01 20:03:52 +0100 | [diff] [blame] | 1598 | /* |
| 1599 | * Using >PAGE_SIZE buffers makes the WRMSR to |
| 1600 | * PERF_GLOBAL_CTRL in intel_pmu_enable_all() |
| 1601 | * mysteriously hang on Core2. |
| 1602 | * |
| 1603 | * As a workaround, we don't do this. |
| 1604 | */ |
| 1605 | x86_pmu.pebs_buffer_size = PAGE_SIZE; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1606 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1607 | break; |
| 1608 | |
| 1609 | case 1: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1610 | pr_cont("PEBS fmt1%c, ", pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1611 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); |
| 1612 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1613 | break; |
| 1614 | |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1615 | case 2: |
| 1616 | pr_cont("PEBS fmt2%c, ", pebs_type); |
| 1617 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw); |
Peter Zijlstra | d2beea4 | 2013-09-12 13:00:47 +0200 | [diff] [blame] | 1618 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Andi Kleen | 130768b | 2013-06-17 17:36:47 -0700 | [diff] [blame] | 1619 | break; |
| 1620 | |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1621 | case 3: |
| 1622 | pr_cont("PEBS fmt3%c, ", pebs_type); |
| 1623 | x86_pmu.pebs_record_size = |
| 1624 | sizeof(struct pebs_record_skl); |
| 1625 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; |
Andi Kleen | a7b58d2 | 2015-05-27 21:13:14 -0700 | [diff] [blame] | 1626 | x86_pmu.free_running_flags |= PERF_SAMPLE_TIME; |
Andi Kleen | 2f7ebf2 | 2015-05-10 12:22:40 -0700 | [diff] [blame] | 1627 | break; |
| 1628 | |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1629 | default: |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1630 | pr_cont("no PEBS fmt%d%c, ", format, pebs_type); |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1631 | x86_pmu.pebs = 0; |
Peter Zijlstra | ca03770 | 2010-03-02 19:52:12 +0100 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | } |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1635 | |
| 1636 | void perf_restore_debug_store(void) |
| 1637 | { |
Linus Torvalds | 2a6e06b | 2013-03-17 15:44:43 -0700 | [diff] [blame] | 1638 | struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds); |
| 1639 | |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1640 | if (!x86_pmu.bts && !x86_pmu.pebs) |
| 1641 | return; |
| 1642 | |
Linus Torvalds | 2a6e06b | 2013-03-17 15:44:43 -0700 | [diff] [blame] | 1643 | wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds); |
Stephane Eranian | 1d9d863 | 2013-03-15 14:26:07 +0100 | [diff] [blame] | 1644 | } |