1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/perf_event.h>
3 #include <linux/types.h>
5 #include <asm/cpu_device_id.h>
6 #include <asm/perf_event.h>
9 #include "../perf_event.h"
12 * Intel LBR_SELECT bits
13 * Intel Vol3a, April 2011, Section 16.7 Table 16-10
15 * Hardware branch filter (not available on all CPUs)
17 #define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
18 #define LBR_USER_BIT 1 /* do not capture at ring > 0 */
19 #define LBR_JCC_BIT 2 /* do not capture conditional branches */
20 #define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
21 #define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
22 #define LBR_RETURN_BIT 5 /* do not capture near returns */
23 #define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
24 #define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
25 #define LBR_FAR_BIT 8 /* do not capture far branches */
26 #define LBR_CALL_STACK_BIT 9 /* enable call stack */
29 * Following bit only exists in Linux; we mask it out before writing it to
30 * the actual MSR. But it helps the constraint perf code to understand
31 * that this is a separate configuration.
33 #define LBR_NO_INFO_BIT 63 /* don't read LBR_INFO. */
35 #define LBR_KERNEL (1 << LBR_KERNEL_BIT)
36 #define LBR_USER (1 << LBR_USER_BIT)
37 #define LBR_JCC (1 << LBR_JCC_BIT)
38 #define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
39 #define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
40 #define LBR_RETURN (1 << LBR_RETURN_BIT)
41 #define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
42 #define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
43 #define LBR_FAR (1 << LBR_FAR_BIT)
44 #define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
45 #define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT)
47 #define LBR_PLM (LBR_KERNEL | LBR_USER)
49 #define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */
50 #define LBR_NOT_SUPP -1 /* LBR filter not supported */
51 #define LBR_IGN 0 /* ignored */
62 #define LBR_FROM_FLAG_MISPRED BIT_ULL(63)
63 #define LBR_FROM_FLAG_IN_TX BIT_ULL(62)
64 #define LBR_FROM_FLAG_ABORT BIT_ULL(61)
66 #define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59))
71 * Hardware branch filter for Arch LBR
73 #define ARCH_LBR_KERNEL_BIT 1 /* capture at ring0 */
74 #define ARCH_LBR_USER_BIT 2 /* capture at ring > 0 */
75 #define ARCH_LBR_CALL_STACK_BIT 3 /* enable call stack */
76 #define ARCH_LBR_JCC_BIT 16 /* capture conditional branches */
77 #define ARCH_LBR_REL_JMP_BIT 17 /* capture relative jumps */
78 #define ARCH_LBR_IND_JMP_BIT 18 /* capture indirect jumps */
79 #define ARCH_LBR_REL_CALL_BIT 19 /* capture relative calls */
80 #define ARCH_LBR_IND_CALL_BIT 20 /* capture indirect calls */
81 #define ARCH_LBR_RETURN_BIT 21 /* capture near returns */
82 #define ARCH_LBR_OTHER_BRANCH_BIT 22 /* capture other branches */
84 #define ARCH_LBR_KERNEL (1ULL << ARCH_LBR_KERNEL_BIT)
85 #define ARCH_LBR_USER (1ULL << ARCH_LBR_USER_BIT)
86 #define ARCH_LBR_CALL_STACK (1ULL << ARCH_LBR_CALL_STACK_BIT)
87 #define ARCH_LBR_JCC (1ULL << ARCH_LBR_JCC_BIT)
88 #define ARCH_LBR_REL_JMP (1ULL << ARCH_LBR_REL_JMP_BIT)
89 #define ARCH_LBR_IND_JMP (1ULL << ARCH_LBR_IND_JMP_BIT)
90 #define ARCH_LBR_REL_CALL (1ULL << ARCH_LBR_REL_CALL_BIT)
91 #define ARCH_LBR_IND_CALL (1ULL << ARCH_LBR_IND_CALL_BIT)
92 #define ARCH_LBR_RETURN (1ULL << ARCH_LBR_RETURN_BIT)
93 #define ARCH_LBR_OTHER_BRANCH (1ULL << ARCH_LBR_OTHER_BRANCH_BIT)
95 #define ARCH_LBR_ANY \
102 ARCH_LBR_OTHER_BRANCH)
104 #define ARCH_LBR_CTL_MASK 0x7f000e
106 static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
108 static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
110 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
111 return !!(config & ARCH_LBR_CALL_STACK);
113 return !!(config & LBR_CALL_STACK);
117 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
118 * otherwise it becomes near impossible to get a reliable stack.
121 static void __intel_pmu_lbr_enable(bool pmi)
123 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
124 u64 debugctl, lbr_select = 0, orig_debugctl;
127 * No need to unfreeze manually, as v4 can do that as part
128 * of the GLOBAL_STATUS ack.
130 if (pmi && x86_pmu.version >= 4)
134 * No need to reprogram LBR_SELECT in a PMI, as it
138 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
139 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && !pmi && cpuc->lbr_sel)
140 wrmsrl(MSR_LBR_SELECT, lbr_select);
142 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
143 orig_debugctl = debugctl;
145 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
146 debugctl |= DEBUGCTLMSR_LBR;
148 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
149 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
150 * may cause superfluous increase/decrease of LBR_TOS.
152 if (is_lbr_call_stack_bit_set(lbr_select))
153 debugctl &= ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
155 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
157 if (orig_debugctl != debugctl)
158 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
160 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
161 wrmsrl(MSR_ARCH_LBR_CTL, lbr_select | ARCH_LBR_CTL_LBREN);
164 void intel_pmu_lbr_reset_32(void)
168 for (i = 0; i < x86_pmu.lbr_nr; i++)
169 wrmsrl(x86_pmu.lbr_from + i, 0);
172 void intel_pmu_lbr_reset_64(void)
176 for (i = 0; i < x86_pmu.lbr_nr; i++) {
177 wrmsrl(x86_pmu.lbr_from + i, 0);
178 wrmsrl(x86_pmu.lbr_to + i, 0);
179 if (x86_pmu.lbr_has_info)
180 wrmsrl(x86_pmu.lbr_info + i, 0);
184 static void intel_pmu_arch_lbr_reset(void)
186 /* Write to ARCH_LBR_DEPTH MSR, all LBR entries are reset to 0 */
187 wrmsrl(MSR_ARCH_LBR_DEPTH, x86_pmu.lbr_nr);
190 void intel_pmu_lbr_reset(void)
192 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
199 cpuc->last_task_ctx = NULL;
200 cpuc->last_log_id = 0;
201 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && cpuc->lbr_select)
202 wrmsrl(MSR_LBR_SELECT, 0);
206 * TOS = most recently recorded branch
208 static inline u64 intel_pmu_lbr_tos(void)
212 rdmsrl(x86_pmu.lbr_tos, tos);
222 * For format LBR_FORMAT_EIP_FLAGS2, bits 61:62 in MSR_LAST_BRANCH_FROM_x
223 * are the TSX flags when TSX is supported, but when TSX is not supported
224 * they have no consistent behavior:
226 * - For wrmsr(), bits 61:62 are considered part of the sign extension.
227 * - For HW updates (branch captures) bits 61:62 are always OFF and are not
228 * part of the sign extension.
232 * 1) LBR format LBR_FORMAT_EIP_FLAGS2
233 * 2) CPU has no TSX support enabled
235 * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
236 * value from rdmsr() must be converted to have a 61 bits sign extension,
237 * ignoring the TSX flags.
239 static inline bool lbr_from_signext_quirk_needed(void)
241 bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
242 boot_cpu_has(X86_FEATURE_RTM);
247 static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
249 /* If quirk is enabled, ensure sign extension is 63 bits: */
250 inline u64 lbr_from_signext_quirk_wr(u64 val)
252 if (static_branch_unlikely(&lbr_from_quirk_key)) {
254 * Sign extend into bits 61:62 while preserving bit 63.
256 * Quirk is enabled when TSX is disabled. Therefore TSX bits
257 * in val are always OFF and must be changed to be sign
258 * extension bits. Since bits 59:60 are guaranteed to be
259 * part of the sign extension bits, we can just copy them
262 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
268 * If quirk is needed, ensure sign extension is 61 bits:
270 static u64 lbr_from_signext_quirk_rd(u64 val)
272 if (static_branch_unlikely(&lbr_from_quirk_key)) {
274 * Quirk is on when TSX is not enabled. Therefore TSX
275 * flags must be read as OFF.
277 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
282 static __always_inline void wrlbr_from(unsigned int idx, u64 val)
284 val = lbr_from_signext_quirk_wr(val);
285 wrmsrl(x86_pmu.lbr_from + idx, val);
288 static __always_inline void wrlbr_to(unsigned int idx, u64 val)
290 wrmsrl(x86_pmu.lbr_to + idx, val);
293 static __always_inline void wrlbr_info(unsigned int idx, u64 val)
295 wrmsrl(x86_pmu.lbr_info + idx, val);
298 static __always_inline u64 rdlbr_from(unsigned int idx, struct lbr_entry *lbr)
305 rdmsrl(x86_pmu.lbr_from + idx, val);
307 return lbr_from_signext_quirk_rd(val);
310 static __always_inline u64 rdlbr_to(unsigned int idx, struct lbr_entry *lbr)
317 rdmsrl(x86_pmu.lbr_to + idx, val);
322 static __always_inline u64 rdlbr_info(unsigned int idx, struct lbr_entry *lbr)
329 rdmsrl(x86_pmu.lbr_info + idx, val);
335 wrlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
337 wrlbr_from(idx, lbr->from);
338 wrlbr_to(idx, lbr->to);
340 wrlbr_info(idx, lbr->info);
344 rdlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
346 u64 from = rdlbr_from(idx, NULL);
348 /* Don't read invalid entry */
353 lbr->to = rdlbr_to(idx, NULL);
355 lbr->info = rdlbr_info(idx, NULL);
360 void intel_pmu_lbr_restore(void *ctx)
362 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
363 struct x86_perf_task_context *task_ctx = ctx;
364 bool need_info = x86_pmu.lbr_has_info;
365 u64 tos = task_ctx->tos;
366 unsigned lbr_idx, mask;
369 mask = x86_pmu.lbr_nr - 1;
370 for (i = 0; i < task_ctx->valid_lbrs; i++) {
371 lbr_idx = (tos - i) & mask;
372 wrlbr_all(&task_ctx->lbr[i], lbr_idx, need_info);
375 for (; i < x86_pmu.lbr_nr; i++) {
376 lbr_idx = (tos - i) & mask;
377 wrlbr_from(lbr_idx, 0);
378 wrlbr_to(lbr_idx, 0);
380 wrlbr_info(lbr_idx, 0);
383 wrmsrl(x86_pmu.lbr_tos, tos);
385 if (cpuc->lbr_select)
386 wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
389 static void intel_pmu_arch_lbr_restore(void *ctx)
391 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
392 struct lbr_entry *entries = task_ctx->entries;
395 /* Fast reset the LBRs before restore if the call stack is not full. */
396 if (!entries[x86_pmu.lbr_nr - 1].from)
397 intel_pmu_arch_lbr_reset();
399 for (i = 0; i < x86_pmu.lbr_nr; i++) {
400 if (!entries[i].from)
402 wrlbr_all(&entries[i], i, true);
407 * Restore the Architecture LBR state from the xsave area in the perf
408 * context data for the task via the XRSTORS instruction.
410 static void intel_pmu_arch_lbr_xrstors(void *ctx)
412 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
414 xrstors(&task_ctx->xsave, XFEATURE_MASK_LBR);
417 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
419 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
420 return x86_pmu.lbr_deep_c_reset && !rdlbr_from(0, NULL);
422 return !rdlbr_from(((struct x86_perf_task_context *)ctx)->tos, NULL);
425 static void __intel_pmu_lbr_restore(void *ctx)
427 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
429 if (task_context_opt(ctx)->lbr_callstack_users == 0 ||
430 task_context_opt(ctx)->lbr_stack_state == LBR_NONE) {
431 intel_pmu_lbr_reset();
436 * Does not restore the LBR registers, if
437 * - No one else touched them, and
438 * - Was not cleared in Cstate
440 if ((ctx == cpuc->last_task_ctx) &&
441 (task_context_opt(ctx)->log_id == cpuc->last_log_id) &&
442 !lbr_is_reset_in_cstate(ctx)) {
443 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
447 x86_pmu.lbr_restore(ctx);
449 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
452 void intel_pmu_lbr_save(void *ctx)
454 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
455 struct x86_perf_task_context *task_ctx = ctx;
456 bool need_info = x86_pmu.lbr_has_info;
457 unsigned lbr_idx, mask;
461 mask = x86_pmu.lbr_nr - 1;
462 tos = intel_pmu_lbr_tos();
463 for (i = 0; i < x86_pmu.lbr_nr; i++) {
464 lbr_idx = (tos - i) & mask;
465 if (!rdlbr_all(&task_ctx->lbr[i], lbr_idx, need_info))
468 task_ctx->valid_lbrs = i;
471 if (cpuc->lbr_select)
472 rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
475 static void intel_pmu_arch_lbr_save(void *ctx)
477 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
478 struct lbr_entry *entries = task_ctx->entries;
481 for (i = 0; i < x86_pmu.lbr_nr; i++) {
482 if (!rdlbr_all(&entries[i], i, true))
486 /* LBR call stack is not full. Reset is required in restore. */
487 if (i < x86_pmu.lbr_nr)
488 entries[x86_pmu.lbr_nr - 1].from = 0;
492 * Save the Architecture LBR state to the xsave area in the perf
493 * context data for the task via the XSAVES instruction.
495 static void intel_pmu_arch_lbr_xsaves(void *ctx)
497 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
499 xsaves(&task_ctx->xsave, XFEATURE_MASK_LBR);
502 static void __intel_pmu_lbr_save(void *ctx)
504 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
506 if (task_context_opt(ctx)->lbr_callstack_users == 0) {
507 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
511 x86_pmu.lbr_save(ctx);
513 task_context_opt(ctx)->lbr_stack_state = LBR_VALID;
515 cpuc->last_task_ctx = ctx;
516 cpuc->last_log_id = ++task_context_opt(ctx)->log_id;
519 void intel_pmu_lbr_swap_task_ctx(struct perf_event_pmu_context *prev_epc,
520 struct perf_event_pmu_context *next_epc)
522 void *prev_ctx_data, *next_ctx_data;
524 swap(prev_epc->task_ctx_data, next_epc->task_ctx_data);
527 * Architecture specific synchronization makes sense in case
528 * both prev_epc->task_ctx_data and next_epc->task_ctx_data
529 * pointers are allocated.
532 prev_ctx_data = next_epc->task_ctx_data;
533 next_ctx_data = prev_epc->task_ctx_data;
535 if (!prev_ctx_data || !next_ctx_data)
538 swap(task_context_opt(prev_ctx_data)->lbr_callstack_users,
539 task_context_opt(next_ctx_data)->lbr_callstack_users);
542 void intel_pmu_lbr_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
544 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
547 if (!cpuc->lbr_users)
551 * If LBR callstack feature is enabled and the stack was saved when
552 * the task was scheduled out, restore the stack. Otherwise flush
555 task_ctx = pmu_ctx ? pmu_ctx->task_ctx_data : NULL;
558 __intel_pmu_lbr_restore(task_ctx);
560 __intel_pmu_lbr_save(task_ctx);
565 * Since a context switch can flip the address space and LBR entries
566 * are not tagged with an identifier, we need to wipe the LBR, even for
567 * per-cpu events. You simply cannot resolve the branches from the old
571 intel_pmu_lbr_reset();
574 static inline bool branch_user_callstack(unsigned br_sel)
576 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
579 void intel_pmu_lbr_add(struct perf_event *event)
581 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
586 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
587 cpuc->lbr_select = 1;
589 cpuc->br_sel = event->hw.branch_reg.reg;
591 if (branch_user_callstack(cpuc->br_sel) && event->pmu_ctx->task_ctx_data)
592 task_context_opt(event->pmu_ctx->task_ctx_data)->lbr_callstack_users++;
595 * Request pmu::sched_task() callback, which will fire inside the
596 * regular perf event scheduling, so that call will:
598 * - restore or wipe; when LBR-callstack,
601 * when this is from __perf_event_task_sched_in().
603 * However, if this is from perf_install_in_context(), no such callback
604 * will follow and we'll need to reset the LBR here if this is the
607 * The problem is, we cannot tell these cases apart... but we can
608 * exclude the biggest chunk of cases by looking at
609 * event->total_time_running. An event that has accrued runtime cannot
610 * be 'new'. Conversely, a new event can get installed through the
611 * context switch path for the first time.
613 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
614 cpuc->lbr_pebs_users++;
615 perf_sched_cb_inc(event->pmu);
616 if (!cpuc->lbr_users++ && !event->total_time_running)
617 intel_pmu_lbr_reset();
620 void release_lbr_buffers(void)
622 struct kmem_cache *kmem_cache;
623 struct cpu_hw_events *cpuc;
626 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
629 for_each_possible_cpu(cpu) {
630 cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
631 kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
632 if (kmem_cache && cpuc->lbr_xsave) {
633 kmem_cache_free(kmem_cache, cpuc->lbr_xsave);
634 cpuc->lbr_xsave = NULL;
639 void reserve_lbr_buffers(void)
641 struct kmem_cache *kmem_cache;
642 struct cpu_hw_events *cpuc;
645 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
648 for_each_possible_cpu(cpu) {
649 cpuc = per_cpu_ptr(&cpu_hw_events, cpu);
650 kmem_cache = x86_get_pmu(cpu)->task_ctx_cache;
651 if (!kmem_cache || cpuc->lbr_xsave)
654 cpuc->lbr_xsave = kmem_cache_alloc_node(kmem_cache,
655 GFP_KERNEL | __GFP_ZERO,
660 void intel_pmu_lbr_del(struct perf_event *event)
662 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
667 if (branch_user_callstack(cpuc->br_sel) &&
668 event->pmu_ctx->task_ctx_data)
669 task_context_opt(event->pmu_ctx->task_ctx_data)->lbr_callstack_users--;
671 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
672 cpuc->lbr_select = 0;
674 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
675 cpuc->lbr_pebs_users--;
677 WARN_ON_ONCE(cpuc->lbr_users < 0);
678 WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
679 perf_sched_cb_dec(event->pmu);
682 * The logged occurrences information is only valid for the
683 * current LBR group. If another LBR group is scheduled in
684 * later, the information from the stale LBRs will be wrongly
685 * interpreted. Reset the LBRs here.
687 * Only clear once for a branch counter group with the leader
689 * - Cannot simply reset the LBRs with the !cpuc->lbr_users.
690 * Because it's possible that the last LBR user is not in a
691 * branch counter group, e.g., a branch_counters group +
692 * several normal LBR events.
693 * - The LBR reset can be done with any one of the events in a
694 * branch counter group, since they are always scheduled together.
695 * It's easy to force the leader event an LBR event.
697 if (is_branch_counters_group(event) && event == event->group_leader)
698 intel_pmu_lbr_reset();
701 static inline bool vlbr_exclude_host(void)
703 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
705 return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
706 (unsigned long *)&cpuc->intel_ctrl_guest_mask);
709 void intel_pmu_lbr_enable_all(bool pmi)
711 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
713 if (cpuc->lbr_users && !vlbr_exclude_host())
714 __intel_pmu_lbr_enable(pmi);
717 void intel_pmu_lbr_disable_all(void)
719 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
721 if (cpuc->lbr_users && !vlbr_exclude_host()) {
722 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
723 return __intel_pmu_arch_lbr_disable();
725 __intel_pmu_lbr_disable();
729 void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
731 unsigned long mask = x86_pmu.lbr_nr - 1;
732 struct perf_branch_entry *br = cpuc->lbr_entries;
733 u64 tos = intel_pmu_lbr_tos();
736 for (i = 0; i < x86_pmu.lbr_nr; i++) {
737 unsigned long lbr_idx = (tos - i) & mask;
746 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
748 perf_clear_branch_entry_bitfields(br);
750 br->from = msr_lastbranch.from;
751 br->to = msr_lastbranch.to;
754 cpuc->lbr_stack.nr = i;
755 cpuc->lbr_stack.hw_idx = tos;
759 * Due to lack of segmentation in Linux the effective address (offset)
760 * is the same as the linear address, allowing us to merge the LIP and EIP
763 void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
765 bool need_info = false, call_stack = false;
766 unsigned long mask = x86_pmu.lbr_nr - 1;
767 struct perf_branch_entry *br = cpuc->lbr_entries;
768 u64 tos = intel_pmu_lbr_tos();
771 int num = x86_pmu.lbr_nr;
774 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
775 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
779 for (i = 0; i < num; i++) {
780 unsigned long lbr_idx = (tos - i) & mask;
781 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
784 from = rdlbr_from(lbr_idx, NULL);
785 to = rdlbr_to(lbr_idx, NULL);
788 * Read LBR call stack entries
789 * until invalid entry (0s) is detected.
791 if (call_stack && !from)
794 if (x86_pmu.lbr_has_info) {
798 info = rdlbr_info(lbr_idx, NULL);
799 mis = !!(info & LBR_INFO_MISPRED);
801 cycles = (info & LBR_INFO_CYCLES);
802 if (x86_pmu.lbr_has_tsx) {
803 in_tx = !!(info & LBR_INFO_IN_TX);
804 abort = !!(info & LBR_INFO_ABORT);
810 if (x86_pmu.lbr_from_flags) {
811 mis = !!(from & LBR_FROM_FLAG_MISPRED);
815 if (x86_pmu.lbr_has_tsx) {
816 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
817 abort = !!(from & LBR_FROM_FLAG_ABORT);
820 from = (u64)((((s64)from) << skip) >> skip);
822 if (x86_pmu.lbr_to_cycles) {
823 cycles = ((to >> 48) & LBR_INFO_CYCLES);
824 to = (u64)((((s64)to) << 16) >> 16);
829 * Some CPUs report duplicated abort records,
830 * with the second entry not having an abort bit set.
831 * Skip them here. This loop runs backwards,
832 * so we need to undo the previous record.
833 * If the abort just happened outside the window
834 * the extra entry cannot be removed.
836 if (abort && x86_pmu.lbr_double_abort && out > 0)
839 perf_clear_branch_entry_bitfields(br+out);
842 br[out].mispred = mis;
843 br[out].predicted = pred;
844 br[out].in_tx = in_tx;
845 br[out].abort = abort;
846 br[out].cycles = cycles;
849 cpuc->lbr_stack.nr = out;
850 cpuc->lbr_stack.hw_idx = tos;
853 static DEFINE_STATIC_KEY_FALSE(x86_lbr_mispred);
854 static DEFINE_STATIC_KEY_FALSE(x86_lbr_cycles);
855 static DEFINE_STATIC_KEY_FALSE(x86_lbr_type);
857 static __always_inline int get_lbr_br_type(u64 info)
861 if (static_branch_likely(&x86_lbr_type))
862 type = (info & LBR_INFO_BR_TYPE) >> LBR_INFO_BR_TYPE_OFFSET;
867 static __always_inline bool get_lbr_mispred(u64 info)
871 if (static_branch_likely(&x86_lbr_mispred))
872 mispred = !!(info & LBR_INFO_MISPRED);
877 static __always_inline u16 get_lbr_cycles(u64 info)
879 u16 cycles = info & LBR_INFO_CYCLES;
881 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
882 (!static_branch_likely(&x86_lbr_cycles) ||
883 !(info & LBR_INFO_CYC_CNT_VALID)))
889 static_assert((64 - PERF_BRANCH_ENTRY_INFO_BITS_MAX) > LBR_INFO_BR_CNTR_NUM * LBR_INFO_BR_CNTR_BITS);
891 static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
892 struct lbr_entry *entries)
894 struct perf_branch_entry *e;
895 struct lbr_entry *lbr;
899 for (i = 0; i < x86_pmu.lbr_nr; i++) {
900 lbr = entries ? &entries[i] : NULL;
901 e = &cpuc->lbr_entries[i];
903 from = rdlbr_from(i, lbr);
905 * Read LBR entries until invalid entry (0s) is detected.
910 to = rdlbr_to(i, lbr);
911 info = rdlbr_info(i, lbr);
913 perf_clear_branch_entry_bitfields(e);
917 e->mispred = get_lbr_mispred(info);
918 e->predicted = !e->mispred;
919 e->in_tx = !!(info & LBR_INFO_IN_TX);
920 e->abort = !!(info & LBR_INFO_ABORT);
921 e->cycles = get_lbr_cycles(info);
922 e->type = get_lbr_br_type(info);
925 * Leverage the reserved field of cpuc->lbr_entries[i] to
926 * temporarily store the branch counters information.
927 * The later code will decide what content can be disclosed
928 * to the perf tool. Pleae see intel_pmu_lbr_counters_reorder().
930 e->reserved = (info >> LBR_INFO_BR_CNTR_OFFSET) & LBR_INFO_BR_CNTR_FULL_MASK;
933 cpuc->lbr_stack.nr = i;
937 * The enabled order may be different from the counter order.
938 * Update the lbr_counters with the enabled order.
940 static void intel_pmu_lbr_counters_reorder(struct cpu_hw_events *cpuc,
941 struct perf_event *event)
943 int i, j, pos = 0, order[X86_PMC_IDX_MAX];
944 struct perf_event *leader, *sibling;
947 leader = event->group_leader;
948 if (branch_sample_counters(leader))
949 order[pos++] = leader->hw.idx;
951 for_each_sibling_event(sibling, leader) {
952 if (!branch_sample_counters(sibling))
954 order[pos++] = sibling->hw.idx;
959 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
960 src = cpuc->lbr_entries[i].reserved;
962 for (j = 0; j < pos; j++) {
963 cnt = (src >> (order[j] * LBR_INFO_BR_CNTR_BITS)) & LBR_INFO_BR_CNTR_MASK;
964 dst |= cnt << j * LBR_INFO_BR_CNTR_BITS;
966 cpuc->lbr_counters[i] = dst;
967 cpuc->lbr_entries[i].reserved = 0;
971 void intel_pmu_lbr_save_brstack(struct perf_sample_data *data,
972 struct cpu_hw_events *cpuc,
973 struct perf_event *event)
975 if (is_branch_counters_group(event)) {
976 intel_pmu_lbr_counters_reorder(cpuc, event);
977 perf_sample_save_brstack(data, event, &cpuc->lbr_stack, cpuc->lbr_counters);
981 perf_sample_save_brstack(data, event, &cpuc->lbr_stack, NULL);
984 static void intel_pmu_arch_lbr_read(struct cpu_hw_events *cpuc)
986 intel_pmu_store_lbr(cpuc, NULL);
989 static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
991 struct x86_perf_task_context_arch_lbr_xsave *xsave = cpuc->lbr_xsave;
994 intel_pmu_store_lbr(cpuc, NULL);
997 xsaves(&xsave->xsave, XFEATURE_MASK_LBR);
999 intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
1002 void intel_pmu_lbr_read(void)
1004 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1007 * Don't read when all LBRs users are using adaptive PEBS.
1009 * This could be smarter and actually check the event,
1010 * but this simple approach seems to work for now.
1012 if (!cpuc->lbr_users || vlbr_exclude_host() ||
1013 cpuc->lbr_users == cpuc->lbr_pebs_users)
1016 x86_pmu.lbr_read(cpuc);
1018 intel_pmu_lbr_filter(cpuc);
1022 * SW filter is used:
1023 * - in case there is no HW filter
1024 * - in case the HW filter has errata or limitations
1026 static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
1028 u64 br_type = event->attr.branch_sample_type;
1031 if (br_type & PERF_SAMPLE_BRANCH_USER)
1032 mask |= X86_BR_USER;
1034 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
1035 mask |= X86_BR_KERNEL;
1037 /* we ignore BRANCH_HV here */
1039 if (br_type & PERF_SAMPLE_BRANCH_ANY)
1042 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
1043 mask |= X86_BR_ANY_CALL;
1045 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
1046 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
1048 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
1049 mask |= X86_BR_IND_CALL;
1051 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
1052 mask |= X86_BR_ABORT;
1054 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
1055 mask |= X86_BR_IN_TX;
1057 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
1058 mask |= X86_BR_NO_TX;
1060 if (br_type & PERF_SAMPLE_BRANCH_COND)
1063 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
1064 if (!x86_pmu_has_lbr_callstack())
1066 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
1068 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
1072 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
1073 mask |= X86_BR_IND_JMP;
1075 if (br_type & PERF_SAMPLE_BRANCH_CALL)
1076 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
1078 if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
1079 mask |= X86_BR_TYPE_SAVE;
1082 * stash actual user request into reg, it may
1083 * be used by fixup code for some CPU
1085 event->hw.branch_reg.reg = mask;
1090 * setup the HW LBR filter
1091 * Used only when available, may not be enough to disambiguate
1092 * all branches, may need the help of the SW filter
1094 static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
1096 struct hw_perf_event_extra *reg;
1097 u64 br_type = event->attr.branch_sample_type;
1101 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
1102 if (!(br_type & (1ULL << i)))
1105 v = x86_pmu.lbr_sel_map[i];
1106 if (v == LBR_NOT_SUPP)
1113 reg = &event->hw.branch_reg;
1114 reg->idx = EXTRA_REG_LBR;
1116 if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
1120 * The Arch LBR HW can retrieve the common branch types
1121 * from the LBR_INFO. It doesn't require the high overhead
1123 * Enable the branch type by default for the Arch LBR.
1125 reg->reg |= X86_BR_TYPE_SAVE;
1130 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
1131 * in suppress mode. So LBR_SELECT should be set to
1132 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
1133 * But the 10th bit LBR_CALL_STACK does not operate
1136 reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
1138 if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
1139 (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
1140 x86_pmu.lbr_has_info)
1141 reg->config |= LBR_NO_INFO;
1146 int intel_pmu_setup_lbr_filter(struct perf_event *event)
1151 * no LBR on this PMU
1153 if (!x86_pmu.lbr_nr)
1157 * setup SW LBR filter
1159 ret = intel_pmu_setup_sw_lbr_filter(event);
1164 * setup HW LBR filter, if any
1166 if (x86_pmu.lbr_sel_map)
1167 ret = intel_pmu_setup_hw_lbr_filter(event);
1173 ARCH_LBR_BR_TYPE_JCC = 0,
1174 ARCH_LBR_BR_TYPE_NEAR_IND_JMP = 1,
1175 ARCH_LBR_BR_TYPE_NEAR_REL_JMP = 2,
1176 ARCH_LBR_BR_TYPE_NEAR_IND_CALL = 3,
1177 ARCH_LBR_BR_TYPE_NEAR_REL_CALL = 4,
1178 ARCH_LBR_BR_TYPE_NEAR_RET = 5,
1179 ARCH_LBR_BR_TYPE_KNOWN_MAX = ARCH_LBR_BR_TYPE_NEAR_RET,
1181 ARCH_LBR_BR_TYPE_MAP_MAX = 16,
1184 static const int arch_lbr_br_type_map[ARCH_LBR_BR_TYPE_MAP_MAX] = {
1185 [ARCH_LBR_BR_TYPE_JCC] = X86_BR_JCC,
1186 [ARCH_LBR_BR_TYPE_NEAR_IND_JMP] = X86_BR_IND_JMP,
1187 [ARCH_LBR_BR_TYPE_NEAR_REL_JMP] = X86_BR_JMP,
1188 [ARCH_LBR_BR_TYPE_NEAR_IND_CALL] = X86_BR_IND_CALL,
1189 [ARCH_LBR_BR_TYPE_NEAR_REL_CALL] = X86_BR_CALL,
1190 [ARCH_LBR_BR_TYPE_NEAR_RET] = X86_BR_RET,
1194 * implement actual branch filter based on user demand.
1195 * Hardware may not exactly satisfy that request, thus
1196 * we need to inspect opcodes. Mismatched branches are
1197 * discarded. Therefore, the number of branches returned
1198 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1201 intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1204 int br_sel = cpuc->br_sel;
1205 int i, j, type, to_plm;
1206 bool compress = false;
1208 /* if sampling all branches, then nothing to filter */
1209 if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1210 ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
1213 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1215 from = cpuc->lbr_entries[i].from;
1216 to = cpuc->lbr_entries[i].to;
1217 type = cpuc->lbr_entries[i].type;
1220 * Parse the branch type recorded in LBR_x_INFO MSR.
1221 * Doesn't support OTHER_BRANCH decoding for now.
1222 * OTHER_BRANCH branch type still rely on software decoding.
1224 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
1225 type <= ARCH_LBR_BR_TYPE_KNOWN_MAX) {
1226 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
1227 type = arch_lbr_br_type_map[type] | to_plm;
1229 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1230 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1231 if (cpuc->lbr_entries[i].in_tx)
1232 type |= X86_BR_IN_TX;
1234 type |= X86_BR_NO_TX;
1237 /* if type does not correspond, then discard */
1238 if (type == X86_BR_NONE || (br_sel & type) != type) {
1239 cpuc->lbr_entries[i].from = 0;
1243 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1244 cpuc->lbr_entries[i].type = common_branch_type(type);
1250 /* remove all entries with from=0 */
1251 for (i = 0; i < cpuc->lbr_stack.nr; ) {
1252 if (!cpuc->lbr_entries[i].from) {
1254 while (++j < cpuc->lbr_stack.nr) {
1255 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1256 cpuc->lbr_counters[j-1] = cpuc->lbr_counters[j];
1258 cpuc->lbr_stack.nr--;
1259 if (!cpuc->lbr_entries[i].from)
1266 void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)
1268 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1270 /* Cannot get TOS for large PEBS and Arch LBR */
1271 if (static_cpu_has(X86_FEATURE_ARCH_LBR) ||
1272 (cpuc->n_pebs == cpuc->n_large_pebs))
1273 cpuc->lbr_stack.hw_idx = -1ULL;
1275 cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1277 intel_pmu_store_lbr(cpuc, lbr);
1278 intel_pmu_lbr_filter(cpuc);
1282 * Map interface branch filters onto LBR filters
1284 static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1285 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1286 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1287 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1288 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1289 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
1290 | LBR_IND_JMP | LBR_FAR,
1292 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1294 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
1295 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1297 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1299 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1300 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1301 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1304 static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1305 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1306 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1307 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1308 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1309 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1310 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1312 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1313 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1314 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1315 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
1318 static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1319 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1320 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1321 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1322 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1323 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1324 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1326 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1327 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1328 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1329 | LBR_RETURN | LBR_CALL_STACK,
1330 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
1331 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
1334 static int arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1335 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = ARCH_LBR_ANY,
1336 [PERF_SAMPLE_BRANCH_USER_SHIFT] = ARCH_LBR_USER,
1337 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = ARCH_LBR_KERNEL,
1338 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1339 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = ARCH_LBR_RETURN |
1340 ARCH_LBR_OTHER_BRANCH,
1341 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = ARCH_LBR_REL_CALL |
1343 ARCH_LBR_OTHER_BRANCH,
1344 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = ARCH_LBR_IND_CALL,
1345 [PERF_SAMPLE_BRANCH_COND_SHIFT] = ARCH_LBR_JCC,
1346 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = ARCH_LBR_REL_CALL |
1349 ARCH_LBR_CALL_STACK,
1350 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = ARCH_LBR_IND_JMP,
1351 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = ARCH_LBR_REL_CALL,
1355 void __init intel_pmu_lbr_init_core(void)
1358 x86_pmu.lbr_tos = MSR_LBR_TOS;
1359 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1360 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1363 * SW branch filter usage:
1364 * - compensate for lack of HW filter
1368 /* nehalem/westmere */
1369 void __init intel_pmu_lbr_init_nhm(void)
1371 x86_pmu.lbr_nr = 16;
1372 x86_pmu.lbr_tos = MSR_LBR_TOS;
1373 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1374 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1376 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1377 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1380 * SW branch filter usage:
1381 * - workaround LBR_SEL errata (see above)
1382 * - support syscall, sysret capture.
1383 * That requires LBR_FAR but that means far
1384 * jmp need to be filtered out
1389 void __init intel_pmu_lbr_init_snb(void)
1391 x86_pmu.lbr_nr = 16;
1392 x86_pmu.lbr_tos = MSR_LBR_TOS;
1393 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1394 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1396 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1397 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1400 * SW branch filter usage:
1401 * - support syscall, sysret capture.
1402 * That requires LBR_FAR but that means far
1403 * jmp need to be filtered out
1407 static inline struct kmem_cache *
1408 create_lbr_kmem_cache(size_t size, size_t align)
1410 return kmem_cache_create("x86_lbr", size, align, 0, NULL);
1414 void intel_pmu_lbr_init_hsw(void)
1416 size_t size = sizeof(struct x86_perf_task_context);
1418 x86_pmu.lbr_nr = 16;
1419 x86_pmu.lbr_tos = MSR_LBR_TOS;
1420 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1421 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1423 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1424 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1426 x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1430 __init void intel_pmu_lbr_init_skl(void)
1432 size_t size = sizeof(struct x86_perf_task_context);
1434 x86_pmu.lbr_nr = 32;
1435 x86_pmu.lbr_tos = MSR_LBR_TOS;
1436 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1437 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1438 x86_pmu.lbr_info = MSR_LBR_INFO_0;
1440 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1441 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1443 x86_get_pmu(smp_processor_id())->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1446 * SW branch filter usage:
1447 * - support syscall, sysret capture.
1448 * That requires LBR_FAR but that means far
1449 * jmp need to be filtered out
1454 void __init intel_pmu_lbr_init_atom(void)
1457 * only models starting at stepping 10 seems
1458 * to have an operational LBR which can freeze
1461 if (boot_cpu_data.x86_vfm == INTEL_ATOM_BONNELL
1462 && boot_cpu_data.x86_stepping < 10) {
1463 pr_cont("LBR disabled due to erratum");
1468 x86_pmu.lbr_tos = MSR_LBR_TOS;
1469 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1470 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1473 * SW branch filter usage:
1474 * - compensate for lack of HW filter
1479 void __init intel_pmu_lbr_init_slm(void)
1482 x86_pmu.lbr_tos = MSR_LBR_TOS;
1483 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1484 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1486 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1487 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1490 * SW branch filter usage:
1491 * - compensate for lack of HW filter
1493 pr_cont("8-deep LBR, ");
1496 /* Knights Landing */
1497 void intel_pmu_lbr_init_knl(void)
1500 x86_pmu.lbr_tos = MSR_LBR_TOS;
1501 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1502 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1504 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1505 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1507 /* Knights Landing does have MISPREDICT bit */
1508 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1509 x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
1512 void intel_pmu_lbr_init(void)
1514 switch (x86_pmu.intel_cap.lbr_format) {
1515 case LBR_FORMAT_EIP_FLAGS2:
1516 x86_pmu.lbr_has_tsx = 1;
1517 x86_pmu.lbr_from_flags = 1;
1518 if (lbr_from_signext_quirk_needed())
1519 static_branch_enable(&lbr_from_quirk_key);
1522 case LBR_FORMAT_EIP_FLAGS:
1523 x86_pmu.lbr_from_flags = 1;
1526 case LBR_FORMAT_INFO:
1527 x86_pmu.lbr_has_tsx = 1;
1529 case LBR_FORMAT_INFO2:
1530 x86_pmu.lbr_has_info = 1;
1533 case LBR_FORMAT_TIME:
1534 x86_pmu.lbr_from_flags = 1;
1535 x86_pmu.lbr_to_cycles = 1;
1539 if (x86_pmu.lbr_has_info) {
1541 * Only used in combination with baseline pebs.
1543 static_branch_enable(&x86_lbr_mispred);
1544 static_branch_enable(&x86_lbr_cycles);
1549 * LBR state size is variable based on the max number of registers.
1550 * This calculates the expected state size, which should match
1551 * what the hardware enumerates for the size of XFEATURE_LBR.
1553 static inline unsigned int get_lbr_state_size(void)
1555 return sizeof(struct arch_lbr_state) +
1556 x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1559 static bool is_arch_lbr_xsave_available(void)
1561 if (!boot_cpu_has(X86_FEATURE_XSAVES))
1565 * Check the LBR state with the corresponding software structure.
1566 * Disable LBR XSAVES support if the size doesn't match.
1568 if (xfeature_size(XFEATURE_LBR) == 0)
1571 if (WARN_ON(xfeature_size(XFEATURE_LBR) != get_lbr_state_size()))
1577 void __init intel_pmu_arch_lbr_init(void)
1579 struct pmu *pmu = x86_get_pmu(smp_processor_id());
1580 union cpuid28_eax eax;
1581 union cpuid28_ebx ebx;
1582 union cpuid28_ecx ecx;
1583 unsigned int unused_edx;
1584 bool arch_lbr_xsave;
1588 /* Arch LBR Capabilities */
1589 cpuid(28, &eax.full, &ebx.full, &ecx.full, &unused_edx);
1591 lbr_nr = fls(eax.split.lbr_depth_mask) * 8;
1593 goto clear_arch_lbr;
1595 /* Apply the max depth of Arch LBR */
1596 if (wrmsrl_safe(MSR_ARCH_LBR_DEPTH, lbr_nr))
1597 goto clear_arch_lbr;
1599 x86_pmu.lbr_depth_mask = eax.split.lbr_depth_mask;
1600 x86_pmu.lbr_deep_c_reset = eax.split.lbr_deep_c_reset;
1601 x86_pmu.lbr_lip = eax.split.lbr_lip;
1602 x86_pmu.lbr_cpl = ebx.split.lbr_cpl;
1603 x86_pmu.lbr_filter = ebx.split.lbr_filter;
1604 x86_pmu.lbr_call_stack = ebx.split.lbr_call_stack;
1605 x86_pmu.lbr_mispred = ecx.split.lbr_mispred;
1606 x86_pmu.lbr_timed_lbr = ecx.split.lbr_timed_lbr;
1607 x86_pmu.lbr_br_type = ecx.split.lbr_br_type;
1608 x86_pmu.lbr_counters = ecx.split.lbr_counters;
1609 x86_pmu.lbr_nr = lbr_nr;
1611 if (!!x86_pmu.lbr_counters)
1612 x86_pmu.flags |= PMU_FL_BR_CNTR;
1614 if (x86_pmu.lbr_mispred)
1615 static_branch_enable(&x86_lbr_mispred);
1616 if (x86_pmu.lbr_timed_lbr)
1617 static_branch_enable(&x86_lbr_cycles);
1618 if (x86_pmu.lbr_br_type)
1619 static_branch_enable(&x86_lbr_type);
1621 arch_lbr_xsave = is_arch_lbr_xsave_available();
1622 if (arch_lbr_xsave) {
1623 size = sizeof(struct x86_perf_task_context_arch_lbr_xsave) +
1624 get_lbr_state_size();
1625 pmu->task_ctx_cache = create_lbr_kmem_cache(size,
1629 if (!pmu->task_ctx_cache) {
1630 arch_lbr_xsave = false;
1632 size = sizeof(struct x86_perf_task_context_arch_lbr) +
1633 lbr_nr * sizeof(struct lbr_entry);
1634 pmu->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1637 x86_pmu.lbr_from = MSR_ARCH_LBR_FROM_0;
1638 x86_pmu.lbr_to = MSR_ARCH_LBR_TO_0;
1639 x86_pmu.lbr_info = MSR_ARCH_LBR_INFO_0;
1641 /* LBR callstack requires both CPL and Branch Filtering support */
1642 if (!x86_pmu.lbr_cpl ||
1643 !x86_pmu.lbr_filter ||
1644 !x86_pmu.lbr_call_stack)
1645 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_NOT_SUPP;
1647 if (!x86_pmu.lbr_cpl) {
1648 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_NOT_SUPP;
1649 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_NOT_SUPP;
1650 } else if (!x86_pmu.lbr_filter) {
1651 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_NOT_SUPP;
1652 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_NOT_SUPP;
1653 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_NOT_SUPP;
1654 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_NOT_SUPP;
1655 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_NOT_SUPP;
1656 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_NOT_SUPP;
1657 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_NOT_SUPP;
1660 x86_pmu.lbr_ctl_mask = ARCH_LBR_CTL_MASK;
1661 x86_pmu.lbr_ctl_map = arch_lbr_ctl_map;
1663 if (!x86_pmu.lbr_cpl && !x86_pmu.lbr_filter)
1664 x86_pmu.lbr_ctl_map = NULL;
1666 x86_pmu.lbr_reset = intel_pmu_arch_lbr_reset;
1667 if (arch_lbr_xsave) {
1668 x86_pmu.lbr_save = intel_pmu_arch_lbr_xsaves;
1669 x86_pmu.lbr_restore = intel_pmu_arch_lbr_xrstors;
1670 x86_pmu.lbr_read = intel_pmu_arch_lbr_read_xsave;
1673 x86_pmu.lbr_save = intel_pmu_arch_lbr_save;
1674 x86_pmu.lbr_restore = intel_pmu_arch_lbr_restore;
1675 x86_pmu.lbr_read = intel_pmu_arch_lbr_read;
1678 pr_cont("Architectural LBR, ");
1683 setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
1687 * x86_perf_get_lbr - get the LBR records information
1689 * @lbr: the caller's memory to store the LBR records information
1691 void x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1693 lbr->nr = x86_pmu.lbr_nr;
1694 lbr->from = x86_pmu.lbr_from;
1695 lbr->to = x86_pmu.lbr_to;
1696 lbr->info = x86_pmu.lbr_info;
1697 lbr->has_callstack = x86_pmu_has_lbr_callstack();
1699 EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1701 struct event_constraint vlbr_constraint =
1702 __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1703 FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);