blob: cb1a0495339b826058cdfd56c1d5332b63390c60 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kevin Winchesterde0428a2011-08-30 20:41:05 -03002#include <linux/perf_event.h>
3#include <linux/types.h>
4
5#include <asm/perf_event.h>
6#include <asm/msr.h>
Stephane Eranian3e702ff2012-02-09 23:20:58 +01007#include <asm/insn.h>
Kevin Winchesterde0428a2011-08-30 20:41:05 -03008
Borislav Petkov27f6d222016-02-10 10:55:23 +01009#include "../perf_event.h"
Peter Zijlstracaff2be2010-03-03 12:02:30 +010010
Colin Ian Kinge91c8d92017-06-29 10:14:06 +010011static const enum {
Andi Kleen135c5612013-06-17 17:36:51 -070012 LBR_EIP_FLAGS = 1,
13 LBR_TSX = 2,
14} lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
15 [LBR_FORMAT_EIP_FLAGS] = LBR_EIP_FLAGS,
16 [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
Peter Zijlstracaff2be2010-03-03 12:02:30 +010017};
18
19/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010020 * Intel LBR_SELECT bits
21 * Intel Vol3a, April 2011, Section 16.7 Table 16-10
22 *
23 * Hardware branch filter (not available on all CPUs)
24 */
25#define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
26#define LBR_USER_BIT 1 /* do not capture at ring > 0 */
27#define LBR_JCC_BIT 2 /* do not capture conditional branches */
28#define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
29#define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
30#define LBR_RETURN_BIT 5 /* do not capture near returns */
31#define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
32#define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
33#define LBR_FAR_BIT 8 /* do not capture far branches */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050034#define LBR_CALL_STACK_BIT 9 /* enable call stack */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010035
Andi Kleenb16a5b52015-10-20 11:46:34 -070036/*
37 * Following bit only exists in Linux; we mask it out before writing it to
38 * the actual MSR. But it helps the constraint perf code to understand
39 * that this is a separate configuration.
40 */
41#define LBR_NO_INFO_BIT 63 /* don't read LBR_INFO. */
42
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010043#define LBR_KERNEL (1 << LBR_KERNEL_BIT)
44#define LBR_USER (1 << LBR_USER_BIT)
45#define LBR_JCC (1 << LBR_JCC_BIT)
46#define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
47#define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
48#define LBR_RETURN (1 << LBR_RETURN_BIT)
49#define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
50#define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
51#define LBR_FAR (1 << LBR_FAR_BIT)
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050052#define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
Andi Kleenb16a5b52015-10-20 11:46:34 -070053#define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010054
55#define LBR_PLM (LBR_KERNEL | LBR_USER)
56
Kan Liangcf3beb72016-04-21 02:30:10 -070057#define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010058#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
David Carrillo-Cisneros3812bba2016-06-21 11:31:12 -070070#define LBR_FROM_FLAG_MISPRED BIT_ULL(63)
71#define LBR_FROM_FLAG_IN_TX BIT_ULL(62)
72#define LBR_FROM_FLAG_ABORT BIT_ULL(61)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010073
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -070074#define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59))
75
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010076/*
Stephane Eranian3e702ff2012-02-09 23:20:58 +010077 * x86control flow change classification
78 * x86control flow changes include branches, interrupts, traps, faults
79 */
80enum {
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050081 X86_BR_NONE = 0, /* unknown */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010082
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050083 X86_BR_USER = 1 << 0, /* branch target is user */
84 X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010085
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050086 X86_BR_CALL = 1 << 2, /* call */
87 X86_BR_RET = 1 << 3, /* return */
88 X86_BR_SYSCALL = 1 << 4, /* syscall */
89 X86_BR_SYSRET = 1 << 5, /* syscall return */
90 X86_BR_INT = 1 << 6, /* sw interrupt */
91 X86_BR_IRET = 1 << 7, /* return from interrupt */
92 X86_BR_JCC = 1 << 8, /* conditional */
93 X86_BR_JMP = 1 << 9, /* jump */
94 X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
95 X86_BR_IND_CALL = 1 << 11,/* indirect calls */
96 X86_BR_ABORT = 1 << 12,/* transaction abort */
97 X86_BR_IN_TX = 1 << 13,/* in transaction */
98 X86_BR_NO_TX = 1 << 14,/* not in transaction */
Yan, Zhengaa54ae92014-11-04 21:56:11 -050099 X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
100 X86_BR_CALL_STACK = 1 << 16,/* call stack */
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200101 X86_BR_IND_JMP = 1 << 17,/* indirect jump */
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800102
103 X86_BR_TYPE_SAVE = 1 << 18,/* indicate to save branch type */
104
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100105};
106
107#define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
Andi Kleen135c5612013-06-17 17:36:51 -0700108#define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100109
110#define X86_BR_ANY \
111 (X86_BR_CALL |\
112 X86_BR_RET |\
113 X86_BR_SYSCALL |\
114 X86_BR_SYSRET |\
115 X86_BR_INT |\
116 X86_BR_IRET |\
117 X86_BR_JCC |\
118 X86_BR_JMP |\
119 X86_BR_IRQ |\
Andi Kleen135c5612013-06-17 17:36:51 -0700120 X86_BR_ABORT |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500121 X86_BR_IND_CALL |\
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200122 X86_BR_IND_JMP |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500123 X86_BR_ZERO_CALL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100124
125#define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
126
127#define X86_BR_ANY_CALL \
128 (X86_BR_CALL |\
129 X86_BR_IND_CALL |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500130 X86_BR_ZERO_CALL |\
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100131 X86_BR_SYSCALL |\
132 X86_BR_IRQ |\
133 X86_BR_INT)
134
Kan Liang49d81842020-07-03 05:49:15 -0700135/*
136 * Intel LBR_CTL bits
137 *
138 * Hardware branch filter for Arch LBR
139 */
140#define ARCH_LBR_KERNEL_BIT 1 /* capture at ring0 */
141#define ARCH_LBR_USER_BIT 2 /* capture at ring > 0 */
142#define ARCH_LBR_CALL_STACK_BIT 3 /* enable call stack */
143#define ARCH_LBR_JCC_BIT 16 /* capture conditional branches */
144#define ARCH_LBR_REL_JMP_BIT 17 /* capture relative jumps */
145#define ARCH_LBR_IND_JMP_BIT 18 /* capture indirect jumps */
146#define ARCH_LBR_REL_CALL_BIT 19 /* capture relative calls */
147#define ARCH_LBR_IND_CALL_BIT 20 /* capture indirect calls */
148#define ARCH_LBR_RETURN_BIT 21 /* capture near returns */
149#define ARCH_LBR_OTHER_BRANCH_BIT 22 /* capture other branches */
150
151#define ARCH_LBR_KERNEL (1ULL << ARCH_LBR_KERNEL_BIT)
152#define ARCH_LBR_USER (1ULL << ARCH_LBR_USER_BIT)
153#define ARCH_LBR_CALL_STACK (1ULL << ARCH_LBR_CALL_STACK_BIT)
154#define ARCH_LBR_JCC (1ULL << ARCH_LBR_JCC_BIT)
155#define ARCH_LBR_REL_JMP (1ULL << ARCH_LBR_REL_JMP_BIT)
156#define ARCH_LBR_IND_JMP (1ULL << ARCH_LBR_IND_JMP_BIT)
157#define ARCH_LBR_REL_CALL (1ULL << ARCH_LBR_REL_CALL_BIT)
158#define ARCH_LBR_IND_CALL (1ULL << ARCH_LBR_IND_CALL_BIT)
159#define ARCH_LBR_RETURN (1ULL << ARCH_LBR_RETURN_BIT)
160#define ARCH_LBR_OTHER_BRANCH (1ULL << ARCH_LBR_OTHER_BRANCH_BIT)
161
162#define ARCH_LBR_ANY \
163 (ARCH_LBR_JCC |\
164 ARCH_LBR_REL_JMP |\
165 ARCH_LBR_IND_JMP |\
166 ARCH_LBR_REL_CALL |\
167 ARCH_LBR_IND_CALL |\
168 ARCH_LBR_RETURN |\
169 ARCH_LBR_OTHER_BRANCH)
170
171#define ARCH_LBR_CTL_MASK 0x7f000e
172
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100173static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
174
Kan Liang47125db2020-07-03 05:49:20 -0700175static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
176{
177 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
178 return !!(config & ARCH_LBR_CALL_STACK);
179
180 return !!(config & LBR_CALL_STACK);
181}
182
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100183/*
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100184 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
185 * otherwise it becomes near impossible to get a reliable stack.
186 */
187
Andi Kleen1a78d932015-03-20 10:11:23 -0700188static void __intel_pmu_lbr_enable(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100189{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500190 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700191 u64 debugctl, lbr_select = 0, orig_debugctl;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100192
Andi Kleen1a78d932015-03-20 10:11:23 -0700193 /*
Andi Kleen425507f2015-05-10 12:22:46 -0700194 * No need to unfreeze manually, as v4 can do that as part
195 * of the GLOBAL_STATUS ack.
196 */
197 if (pmi && x86_pmu.version >= 4)
198 return;
199
200 /*
Andi Kleen1a78d932015-03-20 10:11:23 -0700201 * No need to reprogram LBR_SELECT in a PMI, as it
202 * did not change.
203 */
Kan Liang96f3eda2015-09-14 10:14:07 -0400204 if (cpuc->lbr_sel)
Andi Kleenb16a5b52015-10-20 11:46:34 -0700205 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
Kan Liang47125db2020-07-03 05:49:20 -0700206 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) && !pmi && cpuc->lbr_sel)
Yan, Zheng2c70d002014-11-04 21:56:10 -0500207 wrmsrl(MSR_LBR_SELECT, lbr_select);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100208
209 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700210 orig_debugctl = debugctl;
Kan Liang47125db2020-07-03 05:49:20 -0700211
212 if (!static_cpu_has(X86_FEATURE_ARCH_LBR))
213 debugctl |= DEBUGCTLMSR_LBR;
Yan, Zheng2c70d002014-11-04 21:56:10 -0500214 /*
215 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
216 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
217 * may cause superfluous increase/decrease of LBR_TOS.
218 */
Kan Liang47125db2020-07-03 05:49:20 -0700219 if (is_lbr_call_stack_bit_set(lbr_select))
220 debugctl &= ~DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
221 else
Yan, Zheng2c70d002014-11-04 21:56:10 -0500222 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
Kan Liang47125db2020-07-03 05:49:20 -0700223
Andi Kleencd1f11d2015-03-20 10:11:24 -0700224 if (orig_debugctl != debugctl)
225 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Kan Liang47125db2020-07-03 05:49:20 -0700226
227 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
228 wrmsrl(MSR_ARCH_LBR_CTL, lbr_select | ARCH_LBR_CTL_LBREN);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100229}
230
231static void __intel_pmu_lbr_disable(void)
232{
233 u64 debugctl;
234
Kan Liang47125db2020-07-03 05:49:20 -0700235 if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
236 wrmsrl(MSR_ARCH_LBR_CTL, 0);
237 return;
238 }
239
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100240 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100241 debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100242 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
243}
244
Kan Liang9f354a72020-07-03 05:49:08 -0700245void intel_pmu_lbr_reset_32(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100246{
247 int i;
248
249 for (i = 0; i < x86_pmu.lbr_nr; i++)
250 wrmsrl(x86_pmu.lbr_from + i, 0);
251}
252
Kan Liang9f354a72020-07-03 05:49:08 -0700253void intel_pmu_lbr_reset_64(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100254{
255 int i;
256
257 for (i = 0; i < x86_pmu.lbr_nr; i++) {
258 wrmsrl(x86_pmu.lbr_from + i, 0);
259 wrmsrl(x86_pmu.lbr_to + i, 0);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700260 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
Kan Liangfda1f992020-07-03 05:49:18 -0700261 wrmsrl(x86_pmu.lbr_info + i, 0);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100262 }
263}
264
Kan Liang47125db2020-07-03 05:49:20 -0700265static void intel_pmu_arch_lbr_reset(void)
266{
267 /* Write to ARCH_LBR_DEPTH MSR, all LBR entries are reset to 0 */
268 wrmsrl(MSR_ARCH_LBR_DEPTH, x86_pmu.lbr_nr);
269}
270
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300271void intel_pmu_lbr_reset(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100272{
Kan Liang8b077e4a2018-06-05 08:38:46 -0700273 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
274
Peter Zijlstra74846d32010-03-05 13:49:35 +0100275 if (!x86_pmu.lbr_nr)
276 return;
277
Kan Liang9f354a72020-07-03 05:49:08 -0700278 x86_pmu.lbr_reset();
Kan Liang8b077e4a2018-06-05 08:38:46 -0700279
280 cpuc->last_task_ctx = NULL;
281 cpuc->last_log_id = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100282}
283
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500284/*
285 * TOS = most recently recorded branch
286 */
287static inline u64 intel_pmu_lbr_tos(void)
288{
289 u64 tos;
290
291 rdmsrl(x86_pmu.lbr_tos, tos);
292 return tos;
293}
294
295enum {
296 LBR_NONE,
297 LBR_VALID,
298};
299
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700300/*
301 * For formats with LBR_TSX flags (e.g. LBR_FORMAT_EIP_FLAGS2), bits 61:62 in
302 * MSR_LAST_BRANCH_FROM_x are the TSX flags when TSX is supported, but when
303 * TSX is not supported they have no consistent behavior:
304 *
305 * - For wrmsr(), bits 61:62 are considered part of the sign extension.
306 * - For HW updates (branch captures) bits 61:62 are always OFF and are not
307 * part of the sign extension.
308 *
309 * Therefore, if:
310 *
311 * 1) LBR has TSX format
312 * 2) CPU has no TSX support enabled
313 *
314 * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
315 * value from rdmsr() must be converted to have a 61 bits sign extension,
316 * ignoring the TSX flags.
317 */
318static inline bool lbr_from_signext_quirk_needed(void)
319{
320 int lbr_format = x86_pmu.intel_cap.lbr_format;
321 bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
322 boot_cpu_has(X86_FEATURE_RTM);
323
324 return !tsx_support && (lbr_desc[lbr_format] & LBR_TSX);
325}
326
Valdis Klētnieksd9f3b452019-08-08 13:44:02 -0400327static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700328
329/* If quirk is enabled, ensure sign extension is 63 bits: */
330inline u64 lbr_from_signext_quirk_wr(u64 val)
331{
332 if (static_branch_unlikely(&lbr_from_quirk_key)) {
333 /*
334 * Sign extend into bits 61:62 while preserving bit 63.
335 *
336 * Quirk is enabled when TSX is disabled. Therefore TSX bits
337 * in val are always OFF and must be changed to be sign
338 * extension bits. Since bits 59:60 are guaranteed to be
339 * part of the sign extension bits, we can just copy them
340 * to 61:62.
341 */
342 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
343 }
344 return val;
345}
346
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700347/*
348 * If quirk is needed, ensure sign extension is 61 bits:
349 */
Colin Ian Kinge91c8d92017-06-29 10:14:06 +0100350static u64 lbr_from_signext_quirk_rd(u64 val)
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700351{
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200352 if (static_branch_unlikely(&lbr_from_quirk_key)) {
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700353 /*
354 * Quirk is on when TSX is not enabled. Therefore TSX
355 * flags must be read as OFF.
356 */
357 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200358 }
359 return val;
360}
361
Kan Liang020d91e2020-07-03 05:49:17 -0700362static __always_inline void wrlbr_from(unsigned int idx, u64 val)
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200363{
364 val = lbr_from_signext_quirk_wr(val);
365 wrmsrl(x86_pmu.lbr_from + idx, val);
366}
367
Kan Liang020d91e2020-07-03 05:49:17 -0700368static __always_inline void wrlbr_to(unsigned int idx, u64 val)
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200369{
370 wrmsrl(x86_pmu.lbr_to + idx, val);
371}
372
Kan Liangfda1f992020-07-03 05:49:18 -0700373static __always_inline void wrlbr_info(unsigned int idx, u64 val)
374{
375 wrmsrl(x86_pmu.lbr_info + idx, val);
376}
377
Kan Liang631618a2020-07-03 05:49:19 -0700378static __always_inline u64 rdlbr_from(unsigned int idx, struct lbr_entry *lbr)
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200379{
380 u64 val;
381
Kan Liang631618a2020-07-03 05:49:19 -0700382 if (lbr)
383 return lbr->from;
384
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200385 rdmsrl(x86_pmu.lbr_from + idx, val);
386
387 return lbr_from_signext_quirk_rd(val);
388}
389
Kan Liang631618a2020-07-03 05:49:19 -0700390static __always_inline u64 rdlbr_to(unsigned int idx, struct lbr_entry *lbr)
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200391{
392 u64 val;
393
Kan Liang631618a2020-07-03 05:49:19 -0700394 if (lbr)
395 return lbr->to;
396
Peter Zijlstraaefbc4d2016-06-30 11:49:08 +0200397 rdmsrl(x86_pmu.lbr_to + idx, val);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200398
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700399 return val;
400}
401
Kan Liang631618a2020-07-03 05:49:19 -0700402static __always_inline u64 rdlbr_info(unsigned int idx, struct lbr_entry *lbr)
Kan Liangfda1f992020-07-03 05:49:18 -0700403{
404 u64 val;
405
Kan Liang631618a2020-07-03 05:49:19 -0700406 if (lbr)
407 return lbr->info;
408
Kan Liangfda1f992020-07-03 05:49:18 -0700409 rdmsrl(x86_pmu.lbr_info + idx, val);
410
411 return val;
412}
413
414static inline void
415wrlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
416{
417 wrlbr_from(idx, lbr->from);
418 wrlbr_to(idx, lbr->to);
419 if (need_info)
420 wrlbr_info(idx, lbr->info);
421}
422
423static inline bool
424rdlbr_all(struct lbr_entry *lbr, unsigned int idx, bool need_info)
425{
Kan Liang631618a2020-07-03 05:49:19 -0700426 u64 from = rdlbr_from(idx, NULL);
Kan Liangfda1f992020-07-03 05:49:18 -0700427
428 /* Don't read invalid entry */
429 if (!from)
430 return false;
431
432 lbr->from = from;
Kan Liang631618a2020-07-03 05:49:19 -0700433 lbr->to = rdlbr_to(idx, NULL);
Kan Liangfda1f992020-07-03 05:49:18 -0700434 if (need_info)
Kan Liang631618a2020-07-03 05:49:19 -0700435 lbr->info = rdlbr_info(idx, NULL);
Kan Liangfda1f992020-07-03 05:49:18 -0700436
437 return true;
438}
439
Kan Liang799571b2020-07-03 05:49:10 -0700440void intel_pmu_lbr_restore(void *ctx)
441{
Kan Liangfda1f992020-07-03 05:49:18 -0700442 bool need_info = x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO;
Kan Liang799571b2020-07-03 05:49:10 -0700443 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
444 struct x86_perf_task_context *task_ctx = ctx;
445 int i;
446 unsigned lbr_idx, mask;
447 u64 tos = task_ctx->tos;
448
449 mask = x86_pmu.lbr_nr - 1;
450 for (i = 0; i < task_ctx->valid_lbrs; i++) {
451 lbr_idx = (tos - i) & mask;
Kan Liangfda1f992020-07-03 05:49:18 -0700452 wrlbr_all(&task_ctx->lbr[i], lbr_idx, need_info);
Kan Liang799571b2020-07-03 05:49:10 -0700453 }
454
455 for (; i < x86_pmu.lbr_nr; i++) {
456 lbr_idx = (tos - i) & mask;
457 wrlbr_from(lbr_idx, 0);
458 wrlbr_to(lbr_idx, 0);
459 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
Kan Liangfda1f992020-07-03 05:49:18 -0700460 wrlbr_info(lbr_idx, 0);
Kan Liang799571b2020-07-03 05:49:10 -0700461 }
462
463 wrmsrl(x86_pmu.lbr_tos, tos);
464
465 if (cpuc->lbr_select)
466 wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
467}
468
Kan Liang47125db2020-07-03 05:49:20 -0700469static void intel_pmu_arch_lbr_restore(void *ctx)
470{
471 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
472 struct lbr_entry *entries = task_ctx->entries;
473 int i;
474
475 /* Fast reset the LBRs before restore if the call stack is not full. */
476 if (!entries[x86_pmu.lbr_nr - 1].from)
477 intel_pmu_arch_lbr_reset();
478
479 for (i = 0; i < x86_pmu.lbr_nr; i++) {
480 if (!entries[i].from)
481 break;
482 wrlbr_all(&entries[i], i, true);
483 }
484}
485
Kan Liangce711ea2020-07-03 05:49:28 -0700486/*
487 * Restore the Architecture LBR state from the xsave area in the perf
488 * context data for the task via the XRSTORS instruction.
489 */
490static void intel_pmu_arch_lbr_xrstors(void *ctx)
491{
492 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
493
494 copy_kernel_to_dynamic_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
495}
496
Kan Liangf42be862020-07-03 05:49:12 -0700497static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
Kan Liang530bfff2020-07-03 05:49:11 -0700498{
Kan Liang47125db2020-07-03 05:49:20 -0700499 if (static_cpu_has(X86_FEATURE_ARCH_LBR))
500 return x86_pmu.lbr_deep_c_reset && !rdlbr_from(0, NULL);
501
Kan Liang631618a2020-07-03 05:49:19 -0700502 return !rdlbr_from(((struct x86_perf_task_context *)ctx)->tos, NULL);
Kan Liang530bfff2020-07-03 05:49:11 -0700503}
504
Kan Liangf42be862020-07-03 05:49:12 -0700505static void __intel_pmu_lbr_restore(void *ctx)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500506{
Kan Liang8b077e4a2018-06-05 08:38:46 -0700507 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500508
Kan Liangf42be862020-07-03 05:49:12 -0700509 if (task_context_opt(ctx)->lbr_callstack_users == 0 ||
510 task_context_opt(ctx)->lbr_stack_state == LBR_NONE) {
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500511 intel_pmu_lbr_reset();
512 return;
513 }
514
Kan Liang8b077e4a2018-06-05 08:38:46 -0700515 /*
516 * Does not restore the LBR registers, if
517 * - No one else touched them, and
Kan Liang530bfff2020-07-03 05:49:11 -0700518 * - Was not cleared in Cstate
Kan Liang8b077e4a2018-06-05 08:38:46 -0700519 */
Kan Liangf42be862020-07-03 05:49:12 -0700520 if ((ctx == cpuc->last_task_ctx) &&
521 (task_context_opt(ctx)->log_id == cpuc->last_log_id) &&
522 !lbr_is_reset_in_cstate(ctx)) {
523 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700524 return;
525 }
526
Kan Liangf42be862020-07-03 05:49:12 -0700527 x86_pmu.lbr_restore(ctx);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200528
Kan Liangf42be862020-07-03 05:49:12 -0700529 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500530}
531
Kan Liang799571b2020-07-03 05:49:10 -0700532void intel_pmu_lbr_save(void *ctx)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500533{
Kan Liangfda1f992020-07-03 05:49:18 -0700534 bool need_info = x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700535 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Kan Liang799571b2020-07-03 05:49:10 -0700536 struct x86_perf_task_context *task_ctx = ctx;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500537 unsigned lbr_idx, mask;
Kan Liangfda1f992020-07-03 05:49:18 -0700538 u64 tos;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200539 int i;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500540
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500541 mask = x86_pmu.lbr_nr - 1;
542 tos = intel_pmu_lbr_tos();
Kan Liang0592e572018-06-05 08:38:45 -0700543 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500544 lbr_idx = (tos - i) & mask;
Kan Liangfda1f992020-07-03 05:49:18 -0700545 if (!rdlbr_all(&task_ctx->lbr[i], lbr_idx, need_info))
Kan Liang0592e572018-06-05 08:38:45 -0700546 break;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500547 }
Kan Liang0592e572018-06-05 08:38:45 -0700548 task_ctx->valid_lbrs = i;
Andi Kleenb28ae952015-10-20 11:46:33 -0700549 task_ctx->tos = tos;
Kan Liang799571b2020-07-03 05:49:10 -0700550
551 if (cpuc->lbr_select)
552 rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
553}
554
Kan Liang47125db2020-07-03 05:49:20 -0700555static void intel_pmu_arch_lbr_save(void *ctx)
556{
557 struct x86_perf_task_context_arch_lbr *task_ctx = ctx;
558 struct lbr_entry *entries = task_ctx->entries;
559 int i;
560
561 for (i = 0; i < x86_pmu.lbr_nr; i++) {
562 if (!rdlbr_all(&entries[i], i, true))
563 break;
564 }
565
566 /* LBR call stack is not full. Reset is required in restore. */
567 if (i < x86_pmu.lbr_nr)
568 entries[x86_pmu.lbr_nr - 1].from = 0;
569}
570
Kan Liangce711ea2020-07-03 05:49:28 -0700571/*
572 * Save the Architecture LBR state to the xsave area in the perf
573 * context data for the task via the XSAVES instruction.
574 */
575static void intel_pmu_arch_lbr_xsaves(void *ctx)
576{
577 struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
578
579 copy_dynamic_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
580}
581
Kan Liangf42be862020-07-03 05:49:12 -0700582static void __intel_pmu_lbr_save(void *ctx)
Kan Liang799571b2020-07-03 05:49:10 -0700583{
584 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
585
Kan Liangf42be862020-07-03 05:49:12 -0700586 if (task_context_opt(ctx)->lbr_callstack_users == 0) {
587 task_context_opt(ctx)->lbr_stack_state = LBR_NONE;
Kan Liang799571b2020-07-03 05:49:10 -0700588 return;
589 }
590
Kan Liangf42be862020-07-03 05:49:12 -0700591 x86_pmu.lbr_save(ctx);
Kan Liang799571b2020-07-03 05:49:10 -0700592
Kan Liangf42be862020-07-03 05:49:12 -0700593 task_context_opt(ctx)->lbr_stack_state = LBR_VALID;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700594
Kan Liangf42be862020-07-03 05:49:12 -0700595 cpuc->last_task_ctx = ctx;
596 cpuc->last_log_id = ++task_context_opt(ctx)->log_id;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500597}
598
Alexey Budankov421ca862019-10-23 10:12:54 +0300599void intel_pmu_lbr_swap_task_ctx(struct perf_event_context *prev,
600 struct perf_event_context *next)
601{
Kan Liangf42be862020-07-03 05:49:12 -0700602 void *prev_ctx_data, *next_ctx_data;
Alexey Budankov421ca862019-10-23 10:12:54 +0300603
604 swap(prev->task_ctx_data, next->task_ctx_data);
605
606 /*
607 * Architecture specific synchronization makes sense in
608 * case both prev->task_ctx_data and next->task_ctx_data
609 * pointers are allocated.
610 */
611
612 prev_ctx_data = next->task_ctx_data;
613 next_ctx_data = prev->task_ctx_data;
614
615 if (!prev_ctx_data || !next_ctx_data)
616 return;
617
Kan Liangf42be862020-07-03 05:49:12 -0700618 swap(task_context_opt(prev_ctx_data)->lbr_callstack_users,
619 task_context_opt(next_ctx_data)->lbr_callstack_users);
Alexey Budankov421ca862019-10-23 10:12:54 +0300620}
621
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500622void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
623{
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200624 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Kan Liangf42be862020-07-03 05:49:12 -0700625 void *task_ctx;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500626
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200627 if (!cpuc->lbr_users)
628 return;
629
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500630 /*
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500631 * If LBR callstack feature is enabled and the stack was saved when
632 * the task was scheduled out, restore the stack. Otherwise flush
633 * the LBR stack.
634 */
635 task_ctx = ctx ? ctx->task_ctx_data : NULL;
636 if (task_ctx) {
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200637 if (sched_in)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500638 __intel_pmu_lbr_restore(task_ctx);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200639 else
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500640 __intel_pmu_lbr_save(task_ctx);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500641 return;
642 }
643
644 /*
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200645 * Since a context switch can flip the address space and LBR entries
646 * are not tagged with an identifier, we need to wipe the LBR, even for
647 * per-cpu events. You simply cannot resolve the branches from the old
648 * address space.
649 */
650 if (sched_in)
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500651 intel_pmu_lbr_reset();
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500652}
653
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500654static inline bool branch_user_callstack(unsigned br_sel)
655{
656 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
657}
658
Peter Zijlstra68f70822016-07-06 18:02:43 +0200659void intel_pmu_lbr_add(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100660{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500661 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100662
663 if (!x86_pmu.lbr_nr)
664 return;
665
Like Xue1ad1ac2020-06-13 16:09:50 +0800666 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
667 cpuc->lbr_select = 1;
668
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100669 cpuc->br_sel = event->hw.branch_reg.reg;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100670
Kan Liangf42be862020-07-03 05:49:12 -0700671 if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data)
672 task_context_opt(event->ctx->task_ctx_data)->lbr_callstack_users++;
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500673
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200674 /*
675 * Request pmu::sched_task() callback, which will fire inside the
676 * regular perf event scheduling, so that call will:
677 *
678 * - restore or wipe; when LBR-callstack,
679 * - wipe; otherwise,
680 *
681 * when this is from __perf_event_task_sched_in().
682 *
683 * However, if this is from perf_install_in_context(), no such callback
684 * will follow and we'll need to reset the LBR here if this is the
685 * first LBR event.
686 *
687 * The problem is, we cannot tell these cases apart... but we can
688 * exclude the biggest chunk of cases by looking at
689 * event->total_time_running. An event that has accrued runtime cannot
690 * be 'new'. Conversely, a new event can get installed through the
691 * context switch path for the first time.
692 */
Andi Kleend3617b982019-04-02 12:45:03 -0700693 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
694 cpuc->lbr_pebs_users++;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500695 perf_sched_cb_inc(event->ctx->pmu);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200696 if (!cpuc->lbr_users++ && !event->total_time_running)
697 intel_pmu_lbr_reset();
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100698}
699
Peter Zijlstra68f70822016-07-06 18:02:43 +0200700void intel_pmu_lbr_del(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100701{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500702 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100703
704 if (!x86_pmu.lbr_nr)
705 return;
706
Dan Carpenter5c381812016-10-14 10:29:08 +0300707 if (branch_user_callstack(cpuc->br_sel) &&
Kan Liangf42be862020-07-03 05:49:12 -0700708 event->ctx->task_ctx_data)
709 task_context_opt(event->ctx->task_ctx_data)->lbr_callstack_users--;
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500710
Like Xue1ad1ac2020-06-13 16:09:50 +0800711 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
712 cpuc->lbr_select = 0;
713
Andi Kleend3617b982019-04-02 12:45:03 -0700714 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
715 cpuc->lbr_pebs_users--;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100716 cpuc->lbr_users--;
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100717 WARN_ON_ONCE(cpuc->lbr_users < 0);
Andi Kleend3617b982019-04-02 12:45:03 -0700718 WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500719 perf_sched_cb_dec(event->ctx->pmu);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100720}
721
Like Xue1ad1ac2020-06-13 16:09:50 +0800722static inline bool vlbr_exclude_host(void)
723{
724 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
725
726 return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
727 (unsigned long *)&cpuc->intel_ctrl_guest_mask);
728}
729
Andi Kleen1a78d932015-03-20 10:11:23 -0700730void intel_pmu_lbr_enable_all(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100731{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500732 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100733
Like Xue1ad1ac2020-06-13 16:09:50 +0800734 if (cpuc->lbr_users && !vlbr_exclude_host())
Andi Kleen1a78d932015-03-20 10:11:23 -0700735 __intel_pmu_lbr_enable(pmi);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100736}
737
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300738void intel_pmu_lbr_disable_all(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100739{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500740 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100741
Like Xue1ad1ac2020-06-13 16:09:50 +0800742 if (cpuc->lbr_users && !vlbr_exclude_host())
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100743 __intel_pmu_lbr_disable();
744}
745
Kan Liangc301b1d2020-07-03 05:49:09 -0700746void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100747{
748 unsigned long mask = x86_pmu.lbr_nr - 1;
749 u64 tos = intel_pmu_lbr_tos();
750 int i;
751
Peter Zijlstra63fb3f92010-03-09 11:51:02 +0100752 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100753 unsigned long lbr_idx = (tos - i) & mask;
754 union {
755 struct {
756 u32 from;
757 u32 to;
758 };
759 u64 lbr;
760 } msr_lastbranch;
761
762 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
763
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100764 cpuc->lbr_entries[i].from = msr_lastbranch.from;
765 cpuc->lbr_entries[i].to = msr_lastbranch.to;
766 cpuc->lbr_entries[i].mispred = 0;
767 cpuc->lbr_entries[i].predicted = 0;
Peter Zijlstraf2200ac2017-04-11 10:10:28 +0200768 cpuc->lbr_entries[i].in_tx = 0;
769 cpuc->lbr_entries[i].abort = 0;
770 cpuc->lbr_entries[i].cycles = 0;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800771 cpuc->lbr_entries[i].type = 0;
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100772 cpuc->lbr_entries[i].reserved = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100773 }
774 cpuc->lbr_stack.nr = i;
Kan Liangdb278b92020-01-27 08:53:55 -0800775 cpuc->lbr_stack.hw_idx = tos;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100776}
777
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100778/*
779 * Due to lack of segmentation in Linux the effective address (offset)
780 * is the same as the linear address, allowing us to merge the LIP and EIP
781 * LBR formats.
782 */
Kan Liangc301b1d2020-07-03 05:49:09 -0700783void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100784{
Kan Liang0592e572018-06-05 08:38:45 -0700785 bool need_info = false, call_stack = false;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100786 unsigned long mask = x86_pmu.lbr_nr - 1;
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100787 int lbr_format = x86_pmu.intel_cap.lbr_format;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100788 u64 tos = intel_pmu_lbr_tos();
789 int i;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700790 int out = 0;
Andi Kleen90405aa2015-05-27 21:13:18 -0700791 int num = x86_pmu.lbr_nr;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100792
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100793 if (cpuc->lbr_sel) {
794 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
795 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
Kan Liang0592e572018-06-05 08:38:45 -0700796 call_stack = true;
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100797 }
Andi Kleen90405aa2015-05-27 21:13:18 -0700798
799 for (i = 0; i < num; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100800 unsigned long lbr_idx = (tos - i) & mask;
Andi Kleen135c5612013-06-17 17:36:51 -0700801 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
802 int skip = 0;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700803 u16 cycles = 0;
Andi Kleen135c5612013-06-17 17:36:51 -0700804 int lbr_flags = lbr_desc[lbr_format];
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100805
Kan Liang631618a2020-07-03 05:49:19 -0700806 from = rdlbr_from(lbr_idx, NULL);
807 to = rdlbr_to(lbr_idx, NULL);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100808
Kan Liang0592e572018-06-05 08:38:45 -0700809 /*
810 * Read LBR call stack entries
811 * until invalid entry (0s) is detected.
812 */
813 if (call_stack && !from)
814 break;
815
Andi Kleenb16a5b52015-10-20 11:46:34 -0700816 if (lbr_format == LBR_FORMAT_INFO && need_info) {
Andi Kleen50eab8f2015-05-10 12:22:43 -0700817 u64 info;
818
Kan Liang631618a2020-07-03 05:49:19 -0700819 info = rdlbr_info(lbr_idx, NULL);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700820 mis = !!(info & LBR_INFO_MISPRED);
821 pred = !mis;
822 in_tx = !!(info & LBR_INFO_IN_TX);
823 abort = !!(info & LBR_INFO_ABORT);
824 cycles = (info & LBR_INFO_CYCLES);
825 }
Kan Liang8b92c3a2016-04-15 00:42:47 -0700826
827 if (lbr_format == LBR_FORMAT_TIME) {
828 mis = !!(from & LBR_FROM_FLAG_MISPRED);
829 pred = !mis;
830 skip = 1;
831 cycles = ((to >> 48) & LBR_INFO_CYCLES);
832
833 to = (u64)((((s64)to) << 16) >> 16);
834 }
835
Andi Kleen135c5612013-06-17 17:36:51 -0700836 if (lbr_flags & LBR_EIP_FLAGS) {
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100837 mis = !!(from & LBR_FROM_FLAG_MISPRED);
838 pred = !mis;
Andi Kleen135c5612013-06-17 17:36:51 -0700839 skip = 1;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100840 }
Andi Kleen135c5612013-06-17 17:36:51 -0700841 if (lbr_flags & LBR_TSX) {
842 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
843 abort = !!(from & LBR_FROM_FLAG_ABORT);
844 skip = 3;
845 }
846 from = (u64)((((s64)from) << skip) >> skip);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100847
Andi Kleenb7af41a2013-09-20 07:40:44 -0700848 /*
849 * Some CPUs report duplicated abort records,
850 * with the second entry not having an abort bit set.
851 * Skip them here. This loop runs backwards,
852 * so we need to undo the previous record.
853 * If the abort just happened outside the window
854 * the extra entry cannot be removed.
855 */
856 if (abort && x86_pmu.lbr_double_abort && out > 0)
857 out--;
858
859 cpuc->lbr_entries[out].from = from;
860 cpuc->lbr_entries[out].to = to;
861 cpuc->lbr_entries[out].mispred = mis;
862 cpuc->lbr_entries[out].predicted = pred;
863 cpuc->lbr_entries[out].in_tx = in_tx;
864 cpuc->lbr_entries[out].abort = abort;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700865 cpuc->lbr_entries[out].cycles = cycles;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800866 cpuc->lbr_entries[out].type = 0;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700867 cpuc->lbr_entries[out].reserved = 0;
868 out++;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100869 }
Andi Kleenb7af41a2013-09-20 07:40:44 -0700870 cpuc->lbr_stack.nr = out;
Kan Liangdb278b92020-01-27 08:53:55 -0800871 cpuc->lbr_stack.hw_idx = tos;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100872}
873
Kan Liang47125db2020-07-03 05:49:20 -0700874static __always_inline int get_lbr_br_type(u64 info)
875{
876 if (!static_cpu_has(X86_FEATURE_ARCH_LBR) || !x86_pmu.lbr_br_type)
877 return 0;
878
879 return (info & LBR_INFO_BR_TYPE) >> LBR_INFO_BR_TYPE_OFFSET;
880}
881
882static __always_inline bool get_lbr_mispred(u64 info)
883{
884 if (static_cpu_has(X86_FEATURE_ARCH_LBR) && !x86_pmu.lbr_mispred)
885 return 0;
886
887 return !!(info & LBR_INFO_MISPRED);
888}
889
890static __always_inline bool get_lbr_predicted(u64 info)
891{
892 if (static_cpu_has(X86_FEATURE_ARCH_LBR) && !x86_pmu.lbr_mispred)
893 return 0;
894
895 return !(info & LBR_INFO_MISPRED);
896}
897
898static __always_inline bool get_lbr_cycles(u64 info)
899{
900 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
901 !(x86_pmu.lbr_timed_lbr && info & LBR_INFO_CYC_CNT_VALID))
902 return 0;
903
904 return info & LBR_INFO_CYCLES;
905}
906
Kan Liang631618a2020-07-03 05:49:19 -0700907static void intel_pmu_store_lbr(struct cpu_hw_events *cpuc,
908 struct lbr_entry *entries)
909{
910 struct perf_branch_entry *e;
911 struct lbr_entry *lbr;
912 u64 from, to, info;
913 int i;
914
915 for (i = 0; i < x86_pmu.lbr_nr; i++) {
916 lbr = entries ? &entries[i] : NULL;
917 e = &cpuc->lbr_entries[i];
918
919 from = rdlbr_from(i, lbr);
920 /*
921 * Read LBR entries until invalid entry (0s) is detected.
922 */
923 if (!from)
924 break;
925
926 to = rdlbr_to(i, lbr);
927 info = rdlbr_info(i, lbr);
928
929 e->from = from;
930 e->to = to;
Kan Liang47125db2020-07-03 05:49:20 -0700931 e->mispred = get_lbr_mispred(info);
932 e->predicted = get_lbr_predicted(info);
Kan Liang631618a2020-07-03 05:49:19 -0700933 e->in_tx = !!(info & LBR_INFO_IN_TX);
934 e->abort = !!(info & LBR_INFO_ABORT);
Kan Liang47125db2020-07-03 05:49:20 -0700935 e->cycles = get_lbr_cycles(info);
936 e->type = get_lbr_br_type(info);
Kan Liang631618a2020-07-03 05:49:19 -0700937 e->reserved = 0;
938 }
939
940 cpuc->lbr_stack.nr = i;
941}
942
Kan Liang47125db2020-07-03 05:49:20 -0700943static void intel_pmu_arch_lbr_read(struct cpu_hw_events *cpuc)
944{
945 intel_pmu_store_lbr(cpuc, NULL);
946}
947
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300948void intel_pmu_lbr_read(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100949{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500950 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100951
Andi Kleend3617b982019-04-02 12:45:03 -0700952 /*
953 * Don't read when all LBRs users are using adaptive PEBS.
954 *
955 * This could be smarter and actually check the event,
956 * but this simple approach seems to work for now.
957 */
Like Xue1ad1ac2020-06-13 16:09:50 +0800958 if (!cpuc->lbr_users || vlbr_exclude_host() ||
959 cpuc->lbr_users == cpuc->lbr_pebs_users)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100960 return;
961
Kan Liangc301b1d2020-07-03 05:49:09 -0700962 x86_pmu.lbr_read(cpuc);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100963
964 intel_pmu_lbr_filter(cpuc);
965}
966
967/*
968 * SW filter is used:
969 * - in case there is no HW filter
970 * - in case the HW filter has errata or limitations
971 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500972static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100973{
974 u64 br_type = event->attr.branch_sample_type;
975 int mask = 0;
976
977 if (br_type & PERF_SAMPLE_BRANCH_USER)
978 mask |= X86_BR_USER;
979
Stephane Eranian2b923c82013-05-21 12:53:37 +0200980 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100981 mask |= X86_BR_KERNEL;
982
983 /* we ignore BRANCH_HV here */
984
985 if (br_type & PERF_SAMPLE_BRANCH_ANY)
986 mask |= X86_BR_ANY;
987
988 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
989 mask |= X86_BR_ANY_CALL;
990
991 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
992 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
993
994 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
995 mask |= X86_BR_IND_CALL;
Andi Kleen135c5612013-06-17 17:36:51 -0700996
997 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
998 mask |= X86_BR_ABORT;
999
1000 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
1001 mask |= X86_BR_IN_TX;
1002
1003 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
1004 mask |= X86_BR_NO_TX;
1005
Anshuman Khandual37548912014-05-22 12:50:09 +05301006 if (br_type & PERF_SAMPLE_BRANCH_COND)
1007 mask |= X86_BR_JCC;
1008
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001009 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
1010 if (!x86_pmu_has_lbr_callstack())
1011 return -EOPNOTSUPP;
1012 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
1013 return -EINVAL;
1014 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
1015 X86_BR_CALL_STACK;
1016 }
1017
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001018 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
1019 mask |= X86_BR_IND_JMP;
1020
Stephane Eraniand8928192015-10-13 09:09:09 +02001021 if (br_type & PERF_SAMPLE_BRANCH_CALL)
1022 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001023
1024 if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
1025 mask |= X86_BR_TYPE_SAVE;
1026
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001027 /*
1028 * stash actual user request into reg, it may
1029 * be used by fixup code for some CPU
1030 */
1031 event->hw.branch_reg.reg = mask;
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001032 return 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001033}
1034
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001035/*
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001036 * setup the HW LBR filter
1037 * Used only when available, may not be enough to disambiguate
1038 * all branches, may need the help of the SW filter
1039 */
1040static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
1041{
1042 struct hw_perf_event_extra *reg;
1043 u64 br_type = event->attr.branch_sample_type;
Yan, Zheng27ac9052014-11-04 21:55:57 -05001044 u64 mask = 0, v;
1045 int i;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001046
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001047 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001048 if (!(br_type & (1ULL << i)))
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001049 continue;
1050
Yan, Zheng27ac9052014-11-04 21:55:57 -05001051 v = x86_pmu.lbr_sel_map[i];
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001052 if (v == LBR_NOT_SUPP)
1053 return -EOPNOTSUPP;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001054
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001055 if (v != LBR_IGN)
1056 mask |= v;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001057 }
Andi Kleenb16a5b52015-10-20 11:46:34 -07001058
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001059 reg = &event->hw.branch_reg;
1060 reg->idx = EXTRA_REG_LBR;
1061
Kan Liang49d81842020-07-03 05:49:15 -07001062 if (static_cpu_has(X86_FEATURE_ARCH_LBR)) {
1063 reg->config = mask;
1064 return 0;
1065 }
1066
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001067 /*
1068 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
1069 * in suppress mode. So LBR_SELECT should be set to
1070 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
Kan Liangcf3beb72016-04-21 02:30:10 -07001071 * But the 10th bit LBR_CALL_STACK does not operate
1072 * in suppress mode.
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001073 */
Kan Liangcf3beb72016-04-21 02:30:10 -07001074 reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001075
Andi Kleenb16a5b52015-10-20 11:46:34 -07001076 if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
1077 (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
1078 (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
1079 reg->config |= LBR_NO_INFO;
1080
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001081 return 0;
1082}
1083
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001084int intel_pmu_setup_lbr_filter(struct perf_event *event)
1085{
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001086 int ret = 0;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001087
1088 /*
1089 * no LBR on this PMU
1090 */
1091 if (!x86_pmu.lbr_nr)
1092 return -EOPNOTSUPP;
1093
1094 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001095 * setup SW LBR filter
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001096 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001097 ret = intel_pmu_setup_sw_lbr_filter(event);
1098 if (ret)
1099 return ret;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001100
1101 /*
1102 * setup HW LBR filter, if any
1103 */
1104 if (x86_pmu.lbr_sel_map)
1105 ret = intel_pmu_setup_hw_lbr_filter(event);
1106
1107 return ret;
1108}
1109
1110/*
1111 * return the type of control flow change at address "from"
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08001112 * instruction is not necessarily a branch (in case of interrupt).
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001113 *
1114 * The branch type returned also includes the priv level of the
1115 * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
1116 *
1117 * If a branch type is unknown OR the instruction cannot be
1118 * decoded (e.g., text page not present), then X86_BR_NONE is
1119 * returned.
1120 */
Andi Kleen135c5612013-06-17 17:36:51 -07001121static int branch_type(unsigned long from, unsigned long to, int abort)
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001122{
1123 struct insn insn;
1124 void *addr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001125 int bytes_read, bytes_left;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001126 int ret = X86_BR_NONE;
1127 int ext, to_plm, from_plm;
1128 u8 buf[MAX_INSN_SIZE];
1129 int is64 = 0;
1130
1131 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
1132 from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
1133
1134 /*
1135 * maybe zero if lbr did not fill up after a reset by the time
1136 * we get a PMU interrupt
1137 */
1138 if (from == 0 || to == 0)
1139 return X86_BR_NONE;
1140
Andi Kleen135c5612013-06-17 17:36:51 -07001141 if (abort)
1142 return X86_BR_ABORT | to_plm;
1143
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001144 if (from_plm == X86_BR_USER) {
1145 /*
1146 * can happen if measuring at the user level only
1147 * and we interrupt in a kernel thread, e.g., idle.
1148 */
1149 if (!current->mm)
1150 return X86_BR_NONE;
1151
1152 /* may fail if text not present */
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001153 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
1154 MAX_INSN_SIZE);
1155 bytes_read = MAX_INSN_SIZE - bytes_left;
1156 if (!bytes_read)
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001157 return X86_BR_NONE;
1158
1159 addr = buf;
Peter Zijlstra6e15eb32013-05-03 14:11:24 +02001160 } else {
1161 /*
1162 * The LBR logs any address in the IP, even if the IP just
1163 * faulted. This means userspace can control the from address.
1164 * Ensure we don't blindy read any address by validating it is
1165 * a known text address.
1166 */
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001167 if (kernel_text_address(from)) {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +02001168 addr = (void *)from;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001169 /*
1170 * Assume we can get the maximum possible size
1171 * when grabbing kernel data. This is not
1172 * _strictly_ true since we could possibly be
1173 * executing up next to a memory hole, but
1174 * it is very unlikely to be a problem.
1175 */
1176 bytes_read = MAX_INSN_SIZE;
1177 } else {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +02001178 return X86_BR_NONE;
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001179 }
Peter Zijlstra6e15eb32013-05-03 14:11:24 +02001180 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001181
1182 /*
1183 * decoder needs to know the ABI especially
1184 * on 64-bit systems running 32-bit apps
1185 */
1186#ifdef CONFIG_X86_64
1187 is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
1188#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001189 insn_init(&insn, addr, bytes_read, is64);
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001190 insn_get_opcode(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -08001191 if (!insn.opcode.got)
1192 return X86_BR_ABORT;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001193
1194 switch (insn.opcode.bytes[0]) {
1195 case 0xf:
1196 switch (insn.opcode.bytes[1]) {
1197 case 0x05: /* syscall */
1198 case 0x34: /* sysenter */
1199 ret = X86_BR_SYSCALL;
1200 break;
1201 case 0x07: /* sysret */
1202 case 0x35: /* sysexit */
1203 ret = X86_BR_SYSRET;
1204 break;
1205 case 0x80 ... 0x8f: /* conditional */
1206 ret = X86_BR_JCC;
1207 break;
1208 default:
1209 ret = X86_BR_NONE;
1210 }
1211 break;
1212 case 0x70 ... 0x7f: /* conditional */
1213 ret = X86_BR_JCC;
1214 break;
1215 case 0xc2: /* near ret */
1216 case 0xc3: /* near ret */
1217 case 0xca: /* far ret */
1218 case 0xcb: /* far ret */
1219 ret = X86_BR_RET;
1220 break;
1221 case 0xcf: /* iret */
1222 ret = X86_BR_IRET;
1223 break;
1224 case 0xcc ... 0xce: /* int */
1225 ret = X86_BR_INT;
1226 break;
1227 case 0xe8: /* call near rel */
Yan, Zhengaa54ae92014-11-04 21:56:11 -05001228 insn_get_immediate(&insn);
1229 if (insn.immediate1.value == 0) {
1230 /* zero length call */
1231 ret = X86_BR_ZERO_CALL;
1232 break;
1233 }
Gustavo A. R. Silva2b0fc372019-01-25 12:49:17 -06001234 /* fall through */
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001235 case 0x9a: /* call far absolute */
1236 ret = X86_BR_CALL;
1237 break;
1238 case 0xe0 ... 0xe3: /* loop jmp */
1239 ret = X86_BR_JCC;
1240 break;
1241 case 0xe9 ... 0xeb: /* jmp */
1242 ret = X86_BR_JMP;
1243 break;
1244 case 0xff: /* call near absolute, call far absolute ind */
1245 insn_get_modrm(&insn);
1246 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
1247 switch (ext) {
1248 case 2: /* near ind call */
1249 case 3: /* far ind call */
1250 ret = X86_BR_IND_CALL;
1251 break;
1252 case 4:
1253 case 5:
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001254 ret = X86_BR_IND_JMP;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001255 break;
1256 }
1257 break;
1258 default:
1259 ret = X86_BR_NONE;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001260 }
1261 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001262 * interrupts, traps, faults (and thus ring transition) may
1263 * occur on any instructions. Thus, to classify them correctly,
1264 * we need to first look at the from and to priv levels. If they
1265 * are different and to is in the kernel, then it indicates
1266 * a ring transition. If the from instruction is not a ring
1267 * transition instr (syscall, systenter, int), then it means
1268 * it was a irq, trap or fault.
1269 *
1270 * we have no way of detecting kernel to kernel faults.
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001271 */
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001272 if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
1273 && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
1274 ret = X86_BR_IRQ;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001275
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001276 /*
1277 * branch priv level determined by target as
1278 * is done by HW when LBR_SELECT is implemented
1279 */
1280 if (ret != X86_BR_NONE)
1281 ret |= to_plm;
1282
1283 return ret;
1284}
1285
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001286#define X86_BR_TYPE_MAP_MAX 16
1287
1288static int branch_map[X86_BR_TYPE_MAP_MAX] = {
1289 PERF_BR_CALL, /* X86_BR_CALL */
1290 PERF_BR_RET, /* X86_BR_RET */
1291 PERF_BR_SYSCALL, /* X86_BR_SYSCALL */
1292 PERF_BR_SYSRET, /* X86_BR_SYSRET */
1293 PERF_BR_UNKNOWN, /* X86_BR_INT */
1294 PERF_BR_UNKNOWN, /* X86_BR_IRET */
1295 PERF_BR_COND, /* X86_BR_JCC */
1296 PERF_BR_UNCOND, /* X86_BR_JMP */
1297 PERF_BR_UNKNOWN, /* X86_BR_IRQ */
1298 PERF_BR_IND_CALL, /* X86_BR_IND_CALL */
1299 PERF_BR_UNKNOWN, /* X86_BR_ABORT */
1300 PERF_BR_UNKNOWN, /* X86_BR_IN_TX */
1301 PERF_BR_UNKNOWN, /* X86_BR_NO_TX */
1302 PERF_BR_CALL, /* X86_BR_ZERO_CALL */
1303 PERF_BR_UNKNOWN, /* X86_BR_CALL_STACK */
1304 PERF_BR_IND, /* X86_BR_IND_JMP */
1305};
1306
1307static int
1308common_branch_type(int type)
1309{
1310 int i;
1311
1312 type >>= 2; /* skip X86_BR_USER and X86_BR_KERNEL */
1313
1314 if (type) {
1315 i = __ffs(type);
1316 if (i < X86_BR_TYPE_MAP_MAX)
1317 return branch_map[i];
1318 }
1319
1320 return PERF_BR_UNKNOWN;
1321}
1322
Kan Liang47125db2020-07-03 05:49:20 -07001323enum {
1324 ARCH_LBR_BR_TYPE_JCC = 0,
1325 ARCH_LBR_BR_TYPE_NEAR_IND_JMP = 1,
1326 ARCH_LBR_BR_TYPE_NEAR_REL_JMP = 2,
1327 ARCH_LBR_BR_TYPE_NEAR_IND_CALL = 3,
1328 ARCH_LBR_BR_TYPE_NEAR_REL_CALL = 4,
1329 ARCH_LBR_BR_TYPE_NEAR_RET = 5,
1330 ARCH_LBR_BR_TYPE_KNOWN_MAX = ARCH_LBR_BR_TYPE_NEAR_RET,
1331
1332 ARCH_LBR_BR_TYPE_MAP_MAX = 16,
1333};
1334
1335static const int arch_lbr_br_type_map[ARCH_LBR_BR_TYPE_MAP_MAX] = {
1336 [ARCH_LBR_BR_TYPE_JCC] = X86_BR_JCC,
1337 [ARCH_LBR_BR_TYPE_NEAR_IND_JMP] = X86_BR_IND_JMP,
1338 [ARCH_LBR_BR_TYPE_NEAR_REL_JMP] = X86_BR_JMP,
1339 [ARCH_LBR_BR_TYPE_NEAR_IND_CALL] = X86_BR_IND_CALL,
1340 [ARCH_LBR_BR_TYPE_NEAR_REL_CALL] = X86_BR_CALL,
1341 [ARCH_LBR_BR_TYPE_NEAR_RET] = X86_BR_RET,
1342};
1343
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001344/*
1345 * implement actual branch filter based on user demand.
1346 * Hardware may not exactly satisfy that request, thus
1347 * we need to inspect opcodes. Mismatched branches are
1348 * discarded. Therefore, the number of branches returned
1349 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1350 */
1351static void
1352intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1353{
1354 u64 from, to;
1355 int br_sel = cpuc->br_sel;
Kan Liang47125db2020-07-03 05:49:20 -07001356 int i, j, type, to_plm;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001357 bool compress = false;
1358
1359 /* if sampling all branches, then nothing to filter */
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001360 if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1361 ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001362 return;
1363
1364 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1365
1366 from = cpuc->lbr_entries[i].from;
1367 to = cpuc->lbr_entries[i].to;
Kan Liang47125db2020-07-03 05:49:20 -07001368 type = cpuc->lbr_entries[i].type;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001369
Kan Liang47125db2020-07-03 05:49:20 -07001370 /*
1371 * Parse the branch type recorded in LBR_x_INFO MSR.
1372 * Doesn't support OTHER_BRANCH decoding for now.
1373 * OTHER_BRANCH branch type still rely on software decoding.
1374 */
1375 if (static_cpu_has(X86_FEATURE_ARCH_LBR) &&
1376 type <= ARCH_LBR_BR_TYPE_KNOWN_MAX) {
1377 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
1378 type = arch_lbr_br_type_map[type] | to_plm;
1379 } else
1380 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
Andi Kleen135c5612013-06-17 17:36:51 -07001381 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1382 if (cpuc->lbr_entries[i].in_tx)
1383 type |= X86_BR_IN_TX;
1384 else
1385 type |= X86_BR_NO_TX;
1386 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001387
1388 /* if type does not correspond, then discard */
1389 if (type == X86_BR_NONE || (br_sel & type) != type) {
1390 cpuc->lbr_entries[i].from = 0;
1391 compress = true;
1392 }
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001393
1394 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1395 cpuc->lbr_entries[i].type = common_branch_type(type);
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001396 }
1397
1398 if (!compress)
1399 return;
1400
1401 /* remove all entries with from=0 */
1402 for (i = 0; i < cpuc->lbr_stack.nr; ) {
1403 if (!cpuc->lbr_entries[i].from) {
1404 j = i;
1405 while (++j < cpuc->lbr_stack.nr)
1406 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1407 cpuc->lbr_stack.nr--;
1408 if (!cpuc->lbr_entries[i].from)
1409 continue;
1410 }
1411 i++;
1412 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001413}
1414
Kan Liang56249862020-07-03 05:49:16 -07001415void intel_pmu_store_pebs_lbrs(struct lbr_entry *lbr)
Kan Liangc22497f2019-04-02 12:45:02 -07001416{
1417 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Kan Liangdb278b92020-01-27 08:53:55 -08001418
Kan Liang47125db2020-07-03 05:49:20 -07001419 /* Cannot get TOS for large PEBS and Arch LBR */
1420 if (static_cpu_has(X86_FEATURE_ARCH_LBR) ||
1421 (cpuc->n_pebs == cpuc->n_large_pebs))
Kan Liangdb278b92020-01-27 08:53:55 -08001422 cpuc->lbr_stack.hw_idx = -1ULL;
1423 else
1424 cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1425
Kan Liang631618a2020-07-03 05:49:19 -07001426 intel_pmu_store_lbr(cpuc, lbr);
Kan Liangc22497f2019-04-02 12:45:02 -07001427 intel_pmu_lbr_filter(cpuc);
1428}
1429
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001430/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001431 * Map interface branch filters onto LBR filters
1432 */
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001433static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001434 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1435 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1436 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1437 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1438 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
1439 | LBR_IND_JMP | LBR_FAR,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001440 /*
1441 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1442 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001443 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001444 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1445 /*
1446 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1447 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001448 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1449 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001450 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001451};
1452
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001453static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001454 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1455 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1456 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1457 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1458 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1459 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1460 | LBR_FAR,
1461 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1462 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001463 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001464 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001465};
1466
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001467static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001468 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1469 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1470 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1471 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1472 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1473 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1474 | LBR_FAR,
1475 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1476 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1477 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1478 | LBR_RETURN | LBR_CALL_STACK,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001479 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001480 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001481};
1482
Kan Liang47125db2020-07-03 05:49:20 -07001483static int arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
1484 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = ARCH_LBR_ANY,
1485 [PERF_SAMPLE_BRANCH_USER_SHIFT] = ARCH_LBR_USER,
1486 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = ARCH_LBR_KERNEL,
1487 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1488 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = ARCH_LBR_RETURN |
1489 ARCH_LBR_OTHER_BRANCH,
1490 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = ARCH_LBR_REL_CALL |
1491 ARCH_LBR_IND_CALL |
1492 ARCH_LBR_OTHER_BRANCH,
1493 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = ARCH_LBR_IND_CALL,
1494 [PERF_SAMPLE_BRANCH_COND_SHIFT] = ARCH_LBR_JCC,
1495 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = ARCH_LBR_REL_CALL |
1496 ARCH_LBR_IND_CALL |
1497 ARCH_LBR_RETURN |
1498 ARCH_LBR_CALL_STACK,
1499 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = ARCH_LBR_IND_JMP,
1500 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = ARCH_LBR_REL_CALL,
1501};
1502
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001503/* core */
Mathias Krause066ce642014-08-26 18:49:45 +02001504void __init intel_pmu_lbr_init_core(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001505{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001506 x86_pmu.lbr_nr = 4;
Stephane Eranian225ce532012-02-09 23:20:52 +01001507 x86_pmu.lbr_tos = MSR_LBR_TOS;
1508 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1509 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001510
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001511 /*
1512 * SW branch filter usage:
1513 * - compensate for lack of HW filter
1514 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001515}
1516
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001517/* nehalem/westmere */
Mathias Krause066ce642014-08-26 18:49:45 +02001518void __init intel_pmu_lbr_init_nhm(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001519{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001520 x86_pmu.lbr_nr = 16;
Stephane Eranian225ce532012-02-09 23:20:52 +01001521 x86_pmu.lbr_tos = MSR_LBR_TOS;
1522 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1523 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001524
1525 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1526 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1527
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001528 /*
1529 * SW branch filter usage:
1530 * - workaround LBR_SEL errata (see above)
1531 * - support syscall, sysret capture.
1532 * That requires LBR_FAR but that means far
1533 * jmp need to be filtered out
1534 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001535}
1536
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001537/* sandy bridge */
Mathias Krause066ce642014-08-26 18:49:45 +02001538void __init intel_pmu_lbr_init_snb(void)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001539{
1540 x86_pmu.lbr_nr = 16;
1541 x86_pmu.lbr_tos = MSR_LBR_TOS;
1542 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1543 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1544
1545 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1546 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1547
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001548 /*
1549 * SW branch filter usage:
1550 * - support syscall, sysret capture.
1551 * That requires LBR_FAR but that means far
1552 * jmp need to be filtered out
1553 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001554}
1555
Kan Liang33cad2842020-07-03 05:49:23 -07001556static inline struct kmem_cache *
1557create_lbr_kmem_cache(size_t size, size_t align)
1558{
1559 return kmem_cache_create("x86_lbr", size, align, 0, NULL);
1560}
1561
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001562/* haswell */
1563void intel_pmu_lbr_init_hsw(void)
1564{
Kan Liang33cad2842020-07-03 05:49:23 -07001565 size_t size = sizeof(struct x86_perf_task_context);
1566
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001567 x86_pmu.lbr_nr = 16;
1568 x86_pmu.lbr_tos = MSR_LBR_TOS;
1569 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1570 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1571
1572 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1573 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -07001574
Kan Liang33cad2842020-07-03 05:49:23 -07001575 x86_get_pmu()->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1576
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -07001577 if (lbr_from_signext_quirk_needed())
1578 static_branch_enable(&lbr_from_quirk_key);
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001579}
1580
Andi Kleen9a92e162015-05-10 12:22:44 -07001581/* skylake */
1582__init void intel_pmu_lbr_init_skl(void)
1583{
Kan Liang33cad2842020-07-03 05:49:23 -07001584 size_t size = sizeof(struct x86_perf_task_context);
1585
Andi Kleen9a92e162015-05-10 12:22:44 -07001586 x86_pmu.lbr_nr = 32;
1587 x86_pmu.lbr_tos = MSR_LBR_TOS;
1588 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1589 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
Kan Liangfda1f992020-07-03 05:49:18 -07001590 x86_pmu.lbr_info = MSR_LBR_INFO_0;
Andi Kleen9a92e162015-05-10 12:22:44 -07001591
1592 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1593 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1594
Kan Liang33cad2842020-07-03 05:49:23 -07001595 x86_get_pmu()->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1596
Andi Kleen9a92e162015-05-10 12:22:44 -07001597 /*
1598 * SW branch filter usage:
1599 * - support syscall, sysret capture.
1600 * That requires LBR_FAR but that means far
1601 * jmp need to be filtered out
1602 */
Andi Kleen9a92e162015-05-10 12:22:44 -07001603}
1604
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001605/* atom */
Mathias Krause066ce642014-08-26 18:49:45 +02001606void __init intel_pmu_lbr_init_atom(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001607{
Stephane Eranian88c9a652012-02-09 23:20:56 +01001608 /*
1609 * only models starting at stepping 10 seems
1610 * to have an operational LBR which can freeze
1611 * on PMU interrupt
1612 */
Stephane Eranian3ec18cd2012-08-20 11:24:21 +02001613 if (boot_cpu_data.x86_model == 28
Jia Zhangb3991512018-01-01 09:52:10 +08001614 && boot_cpu_data.x86_stepping < 10) {
Stephane Eranian88c9a652012-02-09 23:20:56 +01001615 pr_cont("LBR disabled due to erratum");
1616 return;
1617 }
1618
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001619 x86_pmu.lbr_nr = 8;
Stephane Eranian225ce532012-02-09 23:20:52 +01001620 x86_pmu.lbr_tos = MSR_LBR_TOS;
1621 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1622 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001623
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001624 /*
1625 * SW branch filter usage:
1626 * - compensate for lack of HW filter
1627 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001628}
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001629
Kan Liangf21d5ad2016-04-15 00:53:45 -07001630/* slm */
1631void __init intel_pmu_lbr_init_slm(void)
1632{
1633 x86_pmu.lbr_nr = 8;
1634 x86_pmu.lbr_tos = MSR_LBR_TOS;
1635 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1636 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1637
1638 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1639 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1640
1641 /*
1642 * SW branch filter usage:
1643 * - compensate for lack of HW filter
1644 */
1645 pr_cont("8-deep LBR, ");
1646}
1647
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001648/* Knights Landing */
1649void intel_pmu_lbr_init_knl(void)
1650{
1651 x86_pmu.lbr_nr = 8;
1652 x86_pmu.lbr_tos = MSR_LBR_TOS;
1653 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1654 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1655
1656 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1657 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
Jacek Tomaka16160c12018-08-02 09:38:30 +08001658
1659 /* Knights Landing does have MISPREDICT bit */
1660 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1661 x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001662}
Like Xub2d65042020-06-13 16:09:48 +08001663
Kan Liangce711ea2020-07-03 05:49:28 -07001664/*
1665 * LBR state size is variable based on the max number of registers.
1666 * This calculates the expected state size, which should match
1667 * what the hardware enumerates for the size of XFEATURE_LBR.
1668 */
1669static inline unsigned int get_lbr_state_size(void)
1670{
1671 return sizeof(struct arch_lbr_state) +
1672 x86_pmu.lbr_nr * sizeof(struct lbr_entry);
1673}
1674
1675static bool is_arch_lbr_xsave_available(void)
1676{
1677 if (!boot_cpu_has(X86_FEATURE_XSAVES))
1678 return false;
1679
1680 /*
1681 * Check the LBR state with the corresponding software structure.
1682 * Disable LBR XSAVES support if the size doesn't match.
1683 */
1684 if (WARN_ON(xfeature_size(XFEATURE_LBR) != get_lbr_state_size()))
1685 return false;
1686
1687 return true;
1688}
1689
Kan Liang47125db2020-07-03 05:49:20 -07001690void __init intel_pmu_arch_lbr_init(void)
1691{
Kan Liangce711ea2020-07-03 05:49:28 -07001692 struct pmu *pmu = x86_get_pmu();
Kan Liang47125db2020-07-03 05:49:20 -07001693 union cpuid28_eax eax;
1694 union cpuid28_ebx ebx;
1695 union cpuid28_ecx ecx;
1696 unsigned int unused_edx;
Kan Liangce711ea2020-07-03 05:49:28 -07001697 bool arch_lbr_xsave;
Kan Liang33cad2842020-07-03 05:49:23 -07001698 size_t size;
Kan Liang47125db2020-07-03 05:49:20 -07001699 u64 lbr_nr;
1700
1701 /* Arch LBR Capabilities */
1702 cpuid(28, &eax.full, &ebx.full, &ecx.full, &unused_edx);
1703
1704 lbr_nr = fls(eax.split.lbr_depth_mask) * 8;
1705 if (!lbr_nr)
1706 goto clear_arch_lbr;
1707
1708 /* Apply the max depth of Arch LBR */
1709 if (wrmsrl_safe(MSR_ARCH_LBR_DEPTH, lbr_nr))
1710 goto clear_arch_lbr;
1711
1712 x86_pmu.lbr_depth_mask = eax.split.lbr_depth_mask;
1713 x86_pmu.lbr_deep_c_reset = eax.split.lbr_deep_c_reset;
1714 x86_pmu.lbr_lip = eax.split.lbr_lip;
1715 x86_pmu.lbr_cpl = ebx.split.lbr_cpl;
1716 x86_pmu.lbr_filter = ebx.split.lbr_filter;
1717 x86_pmu.lbr_call_stack = ebx.split.lbr_call_stack;
1718 x86_pmu.lbr_mispred = ecx.split.lbr_mispred;
1719 x86_pmu.lbr_timed_lbr = ecx.split.lbr_timed_lbr;
1720 x86_pmu.lbr_br_type = ecx.split.lbr_br_type;
1721 x86_pmu.lbr_nr = lbr_nr;
1722
Kan Liangce711ea2020-07-03 05:49:28 -07001723
1724 arch_lbr_xsave = is_arch_lbr_xsave_available();
1725 if (arch_lbr_xsave) {
1726 size = sizeof(struct x86_perf_task_context_arch_lbr_xsave) +
1727 get_lbr_state_size();
1728 pmu->task_ctx_cache = create_lbr_kmem_cache(size,
1729 XSAVE_ALIGNMENT);
1730 }
1731
1732 if (!pmu->task_ctx_cache) {
1733 arch_lbr_xsave = false;
1734
1735 size = sizeof(struct x86_perf_task_context_arch_lbr) +
1736 lbr_nr * sizeof(struct lbr_entry);
1737 pmu->task_ctx_cache = create_lbr_kmem_cache(size, 0);
1738 }
Kan Liang47125db2020-07-03 05:49:20 -07001739
1740 x86_pmu.lbr_from = MSR_ARCH_LBR_FROM_0;
1741 x86_pmu.lbr_to = MSR_ARCH_LBR_TO_0;
1742 x86_pmu.lbr_info = MSR_ARCH_LBR_INFO_0;
1743
1744 /* LBR callstack requires both CPL and Branch Filtering support */
1745 if (!x86_pmu.lbr_cpl ||
1746 !x86_pmu.lbr_filter ||
1747 !x86_pmu.lbr_call_stack)
1748 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_NOT_SUPP;
1749
1750 if (!x86_pmu.lbr_cpl) {
1751 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_NOT_SUPP;
1752 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_NOT_SUPP;
1753 } else if (!x86_pmu.lbr_filter) {
1754 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_NOT_SUPP;
1755 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_NOT_SUPP;
1756 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_NOT_SUPP;
1757 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_NOT_SUPP;
1758 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_NOT_SUPP;
1759 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_NOT_SUPP;
1760 arch_lbr_ctl_map[PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_NOT_SUPP;
1761 }
1762
1763 x86_pmu.lbr_ctl_mask = ARCH_LBR_CTL_MASK;
1764 x86_pmu.lbr_ctl_map = arch_lbr_ctl_map;
1765
1766 if (!x86_pmu.lbr_cpl && !x86_pmu.lbr_filter)
1767 x86_pmu.lbr_ctl_map = NULL;
1768
1769 x86_pmu.lbr_reset = intel_pmu_arch_lbr_reset;
1770 x86_pmu.lbr_read = intel_pmu_arch_lbr_read;
Kan Liangce711ea2020-07-03 05:49:28 -07001771 if (arch_lbr_xsave) {
1772 x86_pmu.lbr_save = intel_pmu_arch_lbr_xsaves;
1773 x86_pmu.lbr_restore = intel_pmu_arch_lbr_xrstors;
1774 pr_cont("XSAVE ");
1775 } else {
1776 x86_pmu.lbr_save = intel_pmu_arch_lbr_save;
1777 x86_pmu.lbr_restore = intel_pmu_arch_lbr_restore;
1778 }
Kan Liang47125db2020-07-03 05:49:20 -07001779
1780 pr_cont("Architectural LBR, ");
1781
1782 return;
1783
1784clear_arch_lbr:
1785 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_ARCH_LBR);
1786}
1787
Like Xub2d65042020-06-13 16:09:48 +08001788/**
1789 * x86_perf_get_lbr - get the LBR records information
1790 *
1791 * @lbr: the caller's memory to store the LBR records information
1792 *
1793 * Returns: 0 indicates the LBR info has been successfully obtained
1794 */
1795int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1796{
1797 int lbr_fmt = x86_pmu.intel_cap.lbr_format;
1798
1799 lbr->nr = x86_pmu.lbr_nr;
1800 lbr->from = x86_pmu.lbr_from;
1801 lbr->to = x86_pmu.lbr_to;
Kan Liangfda1f992020-07-03 05:49:18 -07001802 lbr->info = (lbr_fmt == LBR_FORMAT_INFO) ? x86_pmu.lbr_info : 0;
Like Xub2d65042020-06-13 16:09:48 +08001803
1804 return 0;
1805}
1806EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
Like Xu097e4312020-06-13 16:09:49 +08001807
1808struct event_constraint vlbr_constraint =
Like Xue1ad1ac2020-06-13 16:09:50 +08001809 __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1810 FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);