]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/kernel/cpu/perf_event_intel_lbr.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[karo-tx-linux.git] / arch / x86 / kernel / cpu / perf_event_intel_lbr.c
1 #include <linux/perf_event.h>
2 #include <linux/types.h>
3
4 #include <asm/perf_event.h>
5 #include <asm/msr.h>
6 #include <asm/insn.h>
7
8 #include "perf_event.h"
9
10 enum {
11         LBR_FORMAT_32           = 0x00,
12         LBR_FORMAT_LIP          = 0x01,
13         LBR_FORMAT_EIP          = 0x02,
14         LBR_FORMAT_EIP_FLAGS    = 0x03,
15         LBR_FORMAT_EIP_FLAGS2   = 0x04,
16         LBR_FORMAT_INFO         = 0x05,
17         LBR_FORMAT_MAX_KNOWN    = LBR_FORMAT_INFO,
18 };
19
20 static enum {
21         LBR_EIP_FLAGS           = 1,
22         LBR_TSX                 = 2,
23 } lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
24         [LBR_FORMAT_EIP_FLAGS]  = LBR_EIP_FLAGS,
25         [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
26 };
27
28 /*
29  * Intel LBR_SELECT bits
30  * Intel Vol3a, April 2011, Section 16.7 Table 16-10
31  *
32  * Hardware branch filter (not available on all CPUs)
33  */
34 #define LBR_KERNEL_BIT          0 /* do not capture at ring0 */
35 #define LBR_USER_BIT            1 /* do not capture at ring > 0 */
36 #define LBR_JCC_BIT             2 /* do not capture conditional branches */
37 #define LBR_REL_CALL_BIT        3 /* do not capture relative calls */
38 #define LBR_IND_CALL_BIT        4 /* do not capture indirect calls */
39 #define LBR_RETURN_BIT          5 /* do not capture near returns */
40 #define LBR_IND_JMP_BIT         6 /* do not capture indirect jumps */
41 #define LBR_REL_JMP_BIT         7 /* do not capture relative jumps */
42 #define LBR_FAR_BIT             8 /* do not capture far branches */
43 #define LBR_CALL_STACK_BIT      9 /* enable call stack */
44
45 #define LBR_KERNEL      (1 << LBR_KERNEL_BIT)
46 #define LBR_USER        (1 << LBR_USER_BIT)
47 #define LBR_JCC         (1 << LBR_JCC_BIT)
48 #define LBR_REL_CALL    (1 << LBR_REL_CALL_BIT)
49 #define LBR_IND_CALL    (1 << LBR_IND_CALL_BIT)
50 #define LBR_RETURN      (1 << LBR_RETURN_BIT)
51 #define LBR_REL_JMP     (1 << LBR_REL_JMP_BIT)
52 #define LBR_IND_JMP     (1 << LBR_IND_JMP_BIT)
53 #define LBR_FAR         (1 << LBR_FAR_BIT)
54 #define LBR_CALL_STACK  (1 << LBR_CALL_STACK_BIT)
55
56 #define LBR_PLM (LBR_KERNEL | LBR_USER)
57
58 #define LBR_SEL_MASK    0x1ff   /* valid bits in LBR_SELECT */
59 #define LBR_NOT_SUPP    -1      /* LBR filter not supported */
60 #define LBR_IGN         0       /* ignored */
61
62 #define LBR_ANY          \
63         (LBR_JCC        |\
64          LBR_REL_CALL   |\
65          LBR_IND_CALL   |\
66          LBR_RETURN     |\
67          LBR_REL_JMP    |\
68          LBR_IND_JMP    |\
69          LBR_FAR)
70
71 #define LBR_FROM_FLAG_MISPRED  (1ULL << 63)
72 #define LBR_FROM_FLAG_IN_TX    (1ULL << 62)
73 #define LBR_FROM_FLAG_ABORT    (1ULL << 61)
74
75 /*
76  * x86control flow change classification
77  * x86control flow changes include branches, interrupts, traps, faults
78  */
79 enum {
80         X86_BR_NONE             = 0,      /* unknown */
81
82         X86_BR_USER             = 1 << 0, /* branch target is user */
83         X86_BR_KERNEL           = 1 << 1, /* branch target is kernel */
84
85         X86_BR_CALL             = 1 << 2, /* call */
86         X86_BR_RET              = 1 << 3, /* return */
87         X86_BR_SYSCALL          = 1 << 4, /* syscall */
88         X86_BR_SYSRET           = 1 << 5, /* syscall return */
89         X86_BR_INT              = 1 << 6, /* sw interrupt */
90         X86_BR_IRET             = 1 << 7, /* return from interrupt */
91         X86_BR_JCC              = 1 << 8, /* conditional */
92         X86_BR_JMP              = 1 << 9, /* jump */
93         X86_BR_IRQ              = 1 << 10,/* hw interrupt or trap or fault */
94         X86_BR_IND_CALL         = 1 << 11,/* indirect calls */
95         X86_BR_ABORT            = 1 << 12,/* transaction abort */
96         X86_BR_IN_TX            = 1 << 13,/* in transaction */
97         X86_BR_NO_TX            = 1 << 14,/* not in transaction */
98         X86_BR_ZERO_CALL        = 1 << 15,/* zero length call */
99         X86_BR_CALL_STACK       = 1 << 16,/* call stack */
100         X86_BR_IND_JMP          = 1 << 17,/* indirect jump */
101 };
102
103 #define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
104 #define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
105
106 #define X86_BR_ANY       \
107         (X86_BR_CALL    |\
108          X86_BR_RET     |\
109          X86_BR_SYSCALL |\
110          X86_BR_SYSRET  |\
111          X86_BR_INT     |\
112          X86_BR_IRET    |\
113          X86_BR_JCC     |\
114          X86_BR_JMP      |\
115          X86_BR_IRQ      |\
116          X86_BR_ABORT    |\
117          X86_BR_IND_CALL |\
118          X86_BR_IND_JMP  |\
119          X86_BR_ZERO_CALL)
120
121 #define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
122
123 #define X86_BR_ANY_CALL          \
124         (X86_BR_CALL            |\
125          X86_BR_IND_CALL        |\
126          X86_BR_ZERO_CALL       |\
127          X86_BR_SYSCALL         |\
128          X86_BR_IRQ             |\
129          X86_BR_INT)
130
131 static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
132
133 /*
134  * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
135  * otherwise it becomes near impossible to get a reliable stack.
136  */
137
138 static void __intel_pmu_lbr_enable(bool pmi)
139 {
140         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
141         u64 debugctl, lbr_select = 0, orig_debugctl;
142
143         /*
144          * No need to unfreeze manually, as v4 can do that as part
145          * of the GLOBAL_STATUS ack.
146          */
147         if (pmi && x86_pmu.version >= 4)
148                 return;
149
150         /*
151          * No need to reprogram LBR_SELECT in a PMI, as it
152          * did not change.
153          */
154         if (cpuc->lbr_sel && !pmi) {
155                 lbr_select = cpuc->lbr_sel->config;
156                 wrmsrl(MSR_LBR_SELECT, lbr_select);
157         }
158
159         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
160         orig_debugctl = debugctl;
161         debugctl |= DEBUGCTLMSR_LBR;
162         /*
163          * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
164          * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
165          * may cause superfluous increase/decrease of LBR_TOS.
166          */
167         if (!(lbr_select & LBR_CALL_STACK))
168                 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
169         if (orig_debugctl != debugctl)
170                 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
171 }
172
173 static void __intel_pmu_lbr_disable(void)
174 {
175         u64 debugctl;
176
177         rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
178         debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
179         wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
180 }
181
182 static void intel_pmu_lbr_reset_32(void)
183 {
184         int i;
185
186         for (i = 0; i < x86_pmu.lbr_nr; i++)
187                 wrmsrl(x86_pmu.lbr_from + i, 0);
188 }
189
190 static void intel_pmu_lbr_reset_64(void)
191 {
192         int i;
193
194         for (i = 0; i < x86_pmu.lbr_nr; i++) {
195                 wrmsrl(x86_pmu.lbr_from + i, 0);
196                 wrmsrl(x86_pmu.lbr_to   + i, 0);
197                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
198                         wrmsrl(MSR_LBR_INFO_0 + i, 0);
199         }
200 }
201
202 void intel_pmu_lbr_reset(void)
203 {
204         if (!x86_pmu.lbr_nr)
205                 return;
206
207         if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
208                 intel_pmu_lbr_reset_32();
209         else
210                 intel_pmu_lbr_reset_64();
211 }
212
213 /*
214  * TOS = most recently recorded branch
215  */
216 static inline u64 intel_pmu_lbr_tos(void)
217 {
218         u64 tos;
219
220         rdmsrl(x86_pmu.lbr_tos, tos);
221         return tos;
222 }
223
224 enum {
225         LBR_NONE,
226         LBR_VALID,
227 };
228
229 static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
230 {
231         int i;
232         unsigned lbr_idx, mask;
233         u64 tos;
234
235         if (task_ctx->lbr_callstack_users == 0 ||
236             task_ctx->lbr_stack_state == LBR_NONE) {
237                 intel_pmu_lbr_reset();
238                 return;
239         }
240
241         mask = x86_pmu.lbr_nr - 1;
242         tos = intel_pmu_lbr_tos();
243         for (i = 0; i < tos; i++) {
244                 lbr_idx = (tos - i) & mask;
245                 wrmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
246                 wrmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
247                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
248                         wrmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
249         }
250         task_ctx->lbr_stack_state = LBR_NONE;
251 }
252
253 static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
254 {
255         int i;
256         unsigned lbr_idx, mask;
257         u64 tos;
258
259         if (task_ctx->lbr_callstack_users == 0) {
260                 task_ctx->lbr_stack_state = LBR_NONE;
261                 return;
262         }
263
264         mask = x86_pmu.lbr_nr - 1;
265         tos = intel_pmu_lbr_tos();
266         for (i = 0; i < tos; i++) {
267                 lbr_idx = (tos - i) & mask;
268                 rdmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
269                 rdmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
270                 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
271                         rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
272         }
273         task_ctx->lbr_stack_state = LBR_VALID;
274 }
275
276 void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
277 {
278         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
279         struct x86_perf_task_context *task_ctx;
280
281         /*
282          * If LBR callstack feature is enabled and the stack was saved when
283          * the task was scheduled out, restore the stack. Otherwise flush
284          * the LBR stack.
285          */
286         task_ctx = ctx ? ctx->task_ctx_data : NULL;
287         if (task_ctx) {
288                 if (sched_in) {
289                         __intel_pmu_lbr_restore(task_ctx);
290                         cpuc->lbr_context = ctx;
291                 } else {
292                         __intel_pmu_lbr_save(task_ctx);
293                 }
294                 return;
295         }
296
297         /*
298          * When sampling the branck stack in system-wide, it may be
299          * necessary to flush the stack on context switch. This happens
300          * when the branch stack does not tag its entries with the pid
301          * of the current task. Otherwise it becomes impossible to
302          * associate a branch entry with a task. This ambiguity is more
303          * likely to appear when the branch stack supports priv level
304          * filtering and the user sets it to monitor only at the user
305          * level (which could be a useful measurement in system-wide
306          * mode). In that case, the risk is high of having a branch
307          * stack with branch from multiple tasks.
308          */
309         if (sched_in) {
310                 intel_pmu_lbr_reset();
311                 cpuc->lbr_context = ctx;
312         }
313 }
314
315 static inline bool branch_user_callstack(unsigned br_sel)
316 {
317         return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
318 }
319
320 void intel_pmu_lbr_enable(struct perf_event *event)
321 {
322         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
323         struct x86_perf_task_context *task_ctx;
324
325         if (!x86_pmu.lbr_nr)
326                 return;
327
328         /*
329          * Reset the LBR stack if we changed task context to
330          * avoid data leaks.
331          */
332         if (event->ctx->task && cpuc->lbr_context != event->ctx) {
333                 intel_pmu_lbr_reset();
334                 cpuc->lbr_context = event->ctx;
335         }
336         cpuc->br_sel = event->hw.branch_reg.reg;
337
338         if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
339                                         event->ctx->task_ctx_data) {
340                 task_ctx = event->ctx->task_ctx_data;
341                 task_ctx->lbr_callstack_users++;
342         }
343
344         cpuc->lbr_users++;
345         perf_sched_cb_inc(event->ctx->pmu);
346 }
347
348 void intel_pmu_lbr_disable(struct perf_event *event)
349 {
350         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
351         struct x86_perf_task_context *task_ctx;
352
353         if (!x86_pmu.lbr_nr)
354                 return;
355
356         if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
357                                         event->ctx->task_ctx_data) {
358                 task_ctx = event->ctx->task_ctx_data;
359                 task_ctx->lbr_callstack_users--;
360         }
361
362         cpuc->lbr_users--;
363         WARN_ON_ONCE(cpuc->lbr_users < 0);
364         perf_sched_cb_dec(event->ctx->pmu);
365
366         if (cpuc->enabled && !cpuc->lbr_users) {
367                 __intel_pmu_lbr_disable();
368                 /* avoid stale pointer */
369                 cpuc->lbr_context = NULL;
370         }
371 }
372
373 void intel_pmu_lbr_enable_all(bool pmi)
374 {
375         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
376
377         if (cpuc->lbr_users)
378                 __intel_pmu_lbr_enable(pmi);
379 }
380
381 void intel_pmu_lbr_disable_all(void)
382 {
383         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
384
385         if (cpuc->lbr_users)
386                 __intel_pmu_lbr_disable();
387 }
388
389 static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
390 {
391         unsigned long mask = x86_pmu.lbr_nr - 1;
392         u64 tos = intel_pmu_lbr_tos();
393         int i;
394
395         for (i = 0; i < x86_pmu.lbr_nr; i++) {
396                 unsigned long lbr_idx = (tos - i) & mask;
397                 union {
398                         struct {
399                                 u32 from;
400                                 u32 to;
401                         };
402                         u64     lbr;
403                 } msr_lastbranch;
404
405                 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
406
407                 cpuc->lbr_entries[i].from       = msr_lastbranch.from;
408                 cpuc->lbr_entries[i].to         = msr_lastbranch.to;
409                 cpuc->lbr_entries[i].mispred    = 0;
410                 cpuc->lbr_entries[i].predicted  = 0;
411                 cpuc->lbr_entries[i].reserved   = 0;
412         }
413         cpuc->lbr_stack.nr = i;
414 }
415
416 /*
417  * Due to lack of segmentation in Linux the effective address (offset)
418  * is the same as the linear address, allowing us to merge the LIP and EIP
419  * LBR formats.
420  */
421 static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
422 {
423         unsigned long mask = x86_pmu.lbr_nr - 1;
424         int lbr_format = x86_pmu.intel_cap.lbr_format;
425         u64 tos = intel_pmu_lbr_tos();
426         int i;
427         int out = 0;
428         int num = x86_pmu.lbr_nr;
429
430         if (cpuc->lbr_sel->config & LBR_CALL_STACK)
431                 num = tos;
432
433         for (i = 0; i < num; i++) {
434                 unsigned long lbr_idx = (tos - i) & mask;
435                 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
436                 int skip = 0;
437                 u16 cycles = 0;
438                 int lbr_flags = lbr_desc[lbr_format];
439
440                 rdmsrl(x86_pmu.lbr_from + lbr_idx, from);
441                 rdmsrl(x86_pmu.lbr_to   + lbr_idx, to);
442
443                 if (lbr_format == LBR_FORMAT_INFO) {
444                         u64 info;
445
446                         rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
447                         mis = !!(info & LBR_INFO_MISPRED);
448                         pred = !mis;
449                         in_tx = !!(info & LBR_INFO_IN_TX);
450                         abort = !!(info & LBR_INFO_ABORT);
451                         cycles = (info & LBR_INFO_CYCLES);
452                 }
453                 if (lbr_flags & LBR_EIP_FLAGS) {
454                         mis = !!(from & LBR_FROM_FLAG_MISPRED);
455                         pred = !mis;
456                         skip = 1;
457                 }
458                 if (lbr_flags & LBR_TSX) {
459                         in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
460                         abort = !!(from & LBR_FROM_FLAG_ABORT);
461                         skip = 3;
462                 }
463                 from = (u64)((((s64)from) << skip) >> skip);
464
465                 /*
466                  * Some CPUs report duplicated abort records,
467                  * with the second entry not having an abort bit set.
468                  * Skip them here. This loop runs backwards,
469                  * so we need to undo the previous record.
470                  * If the abort just happened outside the window
471                  * the extra entry cannot be removed.
472                  */
473                 if (abort && x86_pmu.lbr_double_abort && out > 0)
474                         out--;
475
476                 cpuc->lbr_entries[out].from      = from;
477                 cpuc->lbr_entries[out].to        = to;
478                 cpuc->lbr_entries[out].mispred   = mis;
479                 cpuc->lbr_entries[out].predicted = pred;
480                 cpuc->lbr_entries[out].in_tx     = in_tx;
481                 cpuc->lbr_entries[out].abort     = abort;
482                 cpuc->lbr_entries[out].cycles    = cycles;
483                 cpuc->lbr_entries[out].reserved  = 0;
484                 out++;
485         }
486         cpuc->lbr_stack.nr = out;
487 }
488
489 void intel_pmu_lbr_read(void)
490 {
491         struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
492
493         if (!cpuc->lbr_users)
494                 return;
495
496         if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
497                 intel_pmu_lbr_read_32(cpuc);
498         else
499                 intel_pmu_lbr_read_64(cpuc);
500
501         intel_pmu_lbr_filter(cpuc);
502 }
503
504 /*
505  * SW filter is used:
506  * - in case there is no HW filter
507  * - in case the HW filter has errata or limitations
508  */
509 static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
510 {
511         u64 br_type = event->attr.branch_sample_type;
512         int mask = 0;
513
514         if (br_type & PERF_SAMPLE_BRANCH_USER)
515                 mask |= X86_BR_USER;
516
517         if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
518                 mask |= X86_BR_KERNEL;
519
520         /* we ignore BRANCH_HV here */
521
522         if (br_type & PERF_SAMPLE_BRANCH_ANY)
523                 mask |= X86_BR_ANY;
524
525         if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
526                 mask |= X86_BR_ANY_CALL;
527
528         if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
529                 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
530
531         if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
532                 mask |= X86_BR_IND_CALL;
533
534         if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
535                 mask |= X86_BR_ABORT;
536
537         if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
538                 mask |= X86_BR_IN_TX;
539
540         if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
541                 mask |= X86_BR_NO_TX;
542
543         if (br_type & PERF_SAMPLE_BRANCH_COND)
544                 mask |= X86_BR_JCC;
545
546         if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
547                 if (!x86_pmu_has_lbr_callstack())
548                         return -EOPNOTSUPP;
549                 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
550                         return -EINVAL;
551                 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
552                         X86_BR_CALL_STACK;
553         }
554
555         if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
556                 mask |= X86_BR_IND_JMP;
557
558         /*
559          * stash actual user request into reg, it may
560          * be used by fixup code for some CPU
561          */
562         event->hw.branch_reg.reg = mask;
563         return 0;
564 }
565
566 /*
567  * setup the HW LBR filter
568  * Used only when available, may not be enough to disambiguate
569  * all branches, may need the help of the SW filter
570  */
571 static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
572 {
573         struct hw_perf_event_extra *reg;
574         u64 br_type = event->attr.branch_sample_type;
575         u64 mask = 0, v;
576         int i;
577
578         for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
579                 if (!(br_type & (1ULL << i)))
580                         continue;
581
582                 v = x86_pmu.lbr_sel_map[i];
583                 if (v == LBR_NOT_SUPP)
584                         return -EOPNOTSUPP;
585
586                 if (v != LBR_IGN)
587                         mask |= v;
588         }
589         reg = &event->hw.branch_reg;
590         reg->idx = EXTRA_REG_LBR;
591
592         /*
593          * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
594          * in suppress mode. So LBR_SELECT should be set to
595          * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
596          */
597         reg->config = mask ^ x86_pmu.lbr_sel_mask;
598
599         return 0;
600 }
601
602 int intel_pmu_setup_lbr_filter(struct perf_event *event)
603 {
604         int ret = 0;
605
606         /*
607          * no LBR on this PMU
608          */
609         if (!x86_pmu.lbr_nr)
610                 return -EOPNOTSUPP;
611
612         /*
613          * setup SW LBR filter
614          */
615         ret = intel_pmu_setup_sw_lbr_filter(event);
616         if (ret)
617                 return ret;
618
619         /*
620          * setup HW LBR filter, if any
621          */
622         if (x86_pmu.lbr_sel_map)
623                 ret = intel_pmu_setup_hw_lbr_filter(event);
624
625         return ret;
626 }
627
628 /*
629  * return the type of control flow change at address "from"
630  * intruction is not necessarily a branch (in case of interrupt).
631  *
632  * The branch type returned also includes the priv level of the
633  * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
634  *
635  * If a branch type is unknown OR the instruction cannot be
636  * decoded (e.g., text page not present), then X86_BR_NONE is
637  * returned.
638  */
639 static int branch_type(unsigned long from, unsigned long to, int abort)
640 {
641         struct insn insn;
642         void *addr;
643         int bytes_read, bytes_left;
644         int ret = X86_BR_NONE;
645         int ext, to_plm, from_plm;
646         u8 buf[MAX_INSN_SIZE];
647         int is64 = 0;
648
649         to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
650         from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
651
652         /*
653          * maybe zero if lbr did not fill up after a reset by the time
654          * we get a PMU interrupt
655          */
656         if (from == 0 || to == 0)
657                 return X86_BR_NONE;
658
659         if (abort)
660                 return X86_BR_ABORT | to_plm;
661
662         if (from_plm == X86_BR_USER) {
663                 /*
664                  * can happen if measuring at the user level only
665                  * and we interrupt in a kernel thread, e.g., idle.
666                  */
667                 if (!current->mm)
668                         return X86_BR_NONE;
669
670                 /* may fail if text not present */
671                 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
672                                                 MAX_INSN_SIZE);
673                 bytes_read = MAX_INSN_SIZE - bytes_left;
674                 if (!bytes_read)
675                         return X86_BR_NONE;
676
677                 addr = buf;
678         } else {
679                 /*
680                  * The LBR logs any address in the IP, even if the IP just
681                  * faulted. This means userspace can control the from address.
682                  * Ensure we don't blindy read any address by validating it is
683                  * a known text address.
684                  */
685                 if (kernel_text_address(from)) {
686                         addr = (void *)from;
687                         /*
688                          * Assume we can get the maximum possible size
689                          * when grabbing kernel data.  This is not
690                          * _strictly_ true since we could possibly be
691                          * executing up next to a memory hole, but
692                          * it is very unlikely to be a problem.
693                          */
694                         bytes_read = MAX_INSN_SIZE;
695                 } else {
696                         return X86_BR_NONE;
697                 }
698         }
699
700         /*
701          * decoder needs to know the ABI especially
702          * on 64-bit systems running 32-bit apps
703          */
704 #ifdef CONFIG_X86_64
705         is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
706 #endif
707         insn_init(&insn, addr, bytes_read, is64);
708         insn_get_opcode(&insn);
709         if (!insn.opcode.got)
710                 return X86_BR_ABORT;
711
712         switch (insn.opcode.bytes[0]) {
713         case 0xf:
714                 switch (insn.opcode.bytes[1]) {
715                 case 0x05: /* syscall */
716                 case 0x34: /* sysenter */
717                         ret = X86_BR_SYSCALL;
718                         break;
719                 case 0x07: /* sysret */
720                 case 0x35: /* sysexit */
721                         ret = X86_BR_SYSRET;
722                         break;
723                 case 0x80 ... 0x8f: /* conditional */
724                         ret = X86_BR_JCC;
725                         break;
726                 default:
727                         ret = X86_BR_NONE;
728                 }
729                 break;
730         case 0x70 ... 0x7f: /* conditional */
731                 ret = X86_BR_JCC;
732                 break;
733         case 0xc2: /* near ret */
734         case 0xc3: /* near ret */
735         case 0xca: /* far ret */
736         case 0xcb: /* far ret */
737                 ret = X86_BR_RET;
738                 break;
739         case 0xcf: /* iret */
740                 ret = X86_BR_IRET;
741                 break;
742         case 0xcc ... 0xce: /* int */
743                 ret = X86_BR_INT;
744                 break;
745         case 0xe8: /* call near rel */
746                 insn_get_immediate(&insn);
747                 if (insn.immediate1.value == 0) {
748                         /* zero length call */
749                         ret = X86_BR_ZERO_CALL;
750                         break;
751                 }
752         case 0x9a: /* call far absolute */
753                 ret = X86_BR_CALL;
754                 break;
755         case 0xe0 ... 0xe3: /* loop jmp */
756                 ret = X86_BR_JCC;
757                 break;
758         case 0xe9 ... 0xeb: /* jmp */
759                 ret = X86_BR_JMP;
760                 break;
761         case 0xff: /* call near absolute, call far absolute ind */
762                 insn_get_modrm(&insn);
763                 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
764                 switch (ext) {
765                 case 2: /* near ind call */
766                 case 3: /* far ind call */
767                         ret = X86_BR_IND_CALL;
768                         break;
769                 case 4:
770                 case 5:
771                         ret = X86_BR_IND_JMP;
772                         break;
773                 }
774                 break;
775         default:
776                 ret = X86_BR_NONE;
777         }
778         /*
779          * interrupts, traps, faults (and thus ring transition) may
780          * occur on any instructions. Thus, to classify them correctly,
781          * we need to first look at the from and to priv levels. If they
782          * are different and to is in the kernel, then it indicates
783          * a ring transition. If the from instruction is not a ring
784          * transition instr (syscall, systenter, int), then it means
785          * it was a irq, trap or fault.
786          *
787          * we have no way of detecting kernel to kernel faults.
788          */
789         if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
790             && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
791                 ret = X86_BR_IRQ;
792
793         /*
794          * branch priv level determined by target as
795          * is done by HW when LBR_SELECT is implemented
796          */
797         if (ret != X86_BR_NONE)
798                 ret |= to_plm;
799
800         return ret;
801 }
802
803 /*
804  * implement actual branch filter based on user demand.
805  * Hardware may not exactly satisfy that request, thus
806  * we need to inspect opcodes. Mismatched branches are
807  * discarded. Therefore, the number of branches returned
808  * in PERF_SAMPLE_BRANCH_STACK sample may vary.
809  */
810 static void
811 intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
812 {
813         u64 from, to;
814         int br_sel = cpuc->br_sel;
815         int i, j, type;
816         bool compress = false;
817
818         /* if sampling all branches, then nothing to filter */
819         if ((br_sel & X86_BR_ALL) == X86_BR_ALL)
820                 return;
821
822         for (i = 0; i < cpuc->lbr_stack.nr; i++) {
823
824                 from = cpuc->lbr_entries[i].from;
825                 to = cpuc->lbr_entries[i].to;
826
827                 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
828                 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
829                         if (cpuc->lbr_entries[i].in_tx)
830                                 type |= X86_BR_IN_TX;
831                         else
832                                 type |= X86_BR_NO_TX;
833                 }
834
835                 /* if type does not correspond, then discard */
836                 if (type == X86_BR_NONE || (br_sel & type) != type) {
837                         cpuc->lbr_entries[i].from = 0;
838                         compress = true;
839                 }
840         }
841
842         if (!compress)
843                 return;
844
845         /* remove all entries with from=0 */
846         for (i = 0; i < cpuc->lbr_stack.nr; ) {
847                 if (!cpuc->lbr_entries[i].from) {
848                         j = i;
849                         while (++j < cpuc->lbr_stack.nr)
850                                 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
851                         cpuc->lbr_stack.nr--;
852                         if (!cpuc->lbr_entries[i].from)
853                                 continue;
854                 }
855                 i++;
856         }
857 }
858
859 /*
860  * Map interface branch filters onto LBR filters
861  */
862 static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
863         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
864         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
865         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
866         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
867         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_REL_JMP
868                                                 | LBR_IND_JMP | LBR_FAR,
869         /*
870          * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
871          */
872         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
873          LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
874         /*
875          * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
876          */
877         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
878         [PERF_SAMPLE_BRANCH_COND_SHIFT]     = LBR_JCC,
879         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
880 };
881
882 static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
883         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
884         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
885         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
886         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
887         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_FAR,
888         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]     = LBR_REL_CALL | LBR_IND_CALL
889                                                 | LBR_FAR,
890         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]     = LBR_IND_CALL,
891         [PERF_SAMPLE_BRANCH_COND_SHIFT]         = LBR_JCC,
892         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]     = LBR_IND_JMP,
893 };
894
895 static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
896         [PERF_SAMPLE_BRANCH_ANY_SHIFT]          = LBR_ANY,
897         [PERF_SAMPLE_BRANCH_USER_SHIFT]         = LBR_USER,
898         [PERF_SAMPLE_BRANCH_KERNEL_SHIFT]       = LBR_KERNEL,
899         [PERF_SAMPLE_BRANCH_HV_SHIFT]           = LBR_IGN,
900         [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT]   = LBR_RETURN | LBR_FAR,
901         [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT]     = LBR_REL_CALL | LBR_IND_CALL
902                                                 | LBR_FAR,
903         [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT]     = LBR_IND_CALL,
904         [PERF_SAMPLE_BRANCH_COND_SHIFT]         = LBR_JCC,
905         [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT]   = LBR_REL_CALL | LBR_IND_CALL
906                                                 | LBR_RETURN | LBR_CALL_STACK,
907         [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT]     = LBR_IND_JMP,
908 };
909
910 /* core */
911 void __init intel_pmu_lbr_init_core(void)
912 {
913         x86_pmu.lbr_nr     = 4;
914         x86_pmu.lbr_tos    = MSR_LBR_TOS;
915         x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
916         x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
917
918         /*
919          * SW branch filter usage:
920          * - compensate for lack of HW filter
921          */
922         pr_cont("4-deep LBR, ");
923 }
924
925 /* nehalem/westmere */
926 void __init intel_pmu_lbr_init_nhm(void)
927 {
928         x86_pmu.lbr_nr     = 16;
929         x86_pmu.lbr_tos    = MSR_LBR_TOS;
930         x86_pmu.lbr_from   = MSR_LBR_NHM_FROM;
931         x86_pmu.lbr_to     = MSR_LBR_NHM_TO;
932
933         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
934         x86_pmu.lbr_sel_map  = nhm_lbr_sel_map;
935
936         /*
937          * SW branch filter usage:
938          * - workaround LBR_SEL errata (see above)
939          * - support syscall, sysret capture.
940          *   That requires LBR_FAR but that means far
941          *   jmp need to be filtered out
942          */
943         pr_cont("16-deep LBR, ");
944 }
945
946 /* sandy bridge */
947 void __init intel_pmu_lbr_init_snb(void)
948 {
949         x86_pmu.lbr_nr   = 16;
950         x86_pmu.lbr_tos  = MSR_LBR_TOS;
951         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
952         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
953
954         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
955         x86_pmu.lbr_sel_map  = snb_lbr_sel_map;
956
957         /*
958          * SW branch filter usage:
959          * - support syscall, sysret capture.
960          *   That requires LBR_FAR but that means far
961          *   jmp need to be filtered out
962          */
963         pr_cont("16-deep LBR, ");
964 }
965
966 /* haswell */
967 void intel_pmu_lbr_init_hsw(void)
968 {
969         x86_pmu.lbr_nr   = 16;
970         x86_pmu.lbr_tos  = MSR_LBR_TOS;
971         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
972         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
973
974         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
975         x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
976
977         pr_cont("16-deep LBR, ");
978 }
979
980 /* skylake */
981 __init void intel_pmu_lbr_init_skl(void)
982 {
983         x86_pmu.lbr_nr   = 32;
984         x86_pmu.lbr_tos  = MSR_LBR_TOS;
985         x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
986         x86_pmu.lbr_to   = MSR_LBR_NHM_TO;
987
988         x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
989         x86_pmu.lbr_sel_map  = hsw_lbr_sel_map;
990
991         /*
992          * SW branch filter usage:
993          * - support syscall, sysret capture.
994          *   That requires LBR_FAR but that means far
995          *   jmp need to be filtered out
996          */
997         pr_cont("32-deep LBR, ");
998 }
999
1000 /* atom */
1001 void __init intel_pmu_lbr_init_atom(void)
1002 {
1003         /*
1004          * only models starting at stepping 10 seems
1005          * to have an operational LBR which can freeze
1006          * on PMU interrupt
1007          */
1008         if (boot_cpu_data.x86_model == 28
1009             && boot_cpu_data.x86_mask < 10) {
1010                 pr_cont("LBR disabled due to erratum");
1011                 return;
1012         }
1013
1014         x86_pmu.lbr_nr     = 8;
1015         x86_pmu.lbr_tos    = MSR_LBR_TOS;
1016         x86_pmu.lbr_from   = MSR_LBR_CORE_FROM;
1017         x86_pmu.lbr_to     = MSR_LBR_CORE_TO;
1018
1019         /*
1020          * SW branch filter usage:
1021          * - compensate for lack of HW filter
1022          */
1023         pr_cont("8-deep LBR, ");
1024 }