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