blob: b2b8dc9730574bb3659d682e2f634fb57908dc17 [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
135static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
136
137/*
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100138 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
139 * otherwise it becomes near impossible to get a reliable stack.
140 */
141
Andi Kleen1a78d932015-03-20 10:11:23 -0700142static void __intel_pmu_lbr_enable(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100143{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500144 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700145 u64 debugctl, lbr_select = 0, orig_debugctl;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100146
Andi Kleen1a78d932015-03-20 10:11:23 -0700147 /*
Andi Kleen425507f2015-05-10 12:22:46 -0700148 * No need to unfreeze manually, as v4 can do that as part
149 * of the GLOBAL_STATUS ack.
150 */
151 if (pmi && x86_pmu.version >= 4)
152 return;
153
154 /*
Andi Kleen1a78d932015-03-20 10:11:23 -0700155 * No need to reprogram LBR_SELECT in a PMI, as it
156 * did not change.
157 */
Kan Liang96f3eda2015-09-14 10:14:07 -0400158 if (cpuc->lbr_sel)
Andi Kleenb16a5b52015-10-20 11:46:34 -0700159 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100160 if (!pmi && cpuc->lbr_sel)
Yan, Zheng2c70d002014-11-04 21:56:10 -0500161 wrmsrl(MSR_LBR_SELECT, lbr_select);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100162
163 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700164 orig_debugctl = debugctl;
Yan, Zheng2c70d002014-11-04 21:56:10 -0500165 debugctl |= DEBUGCTLMSR_LBR;
166 /*
167 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
168 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
169 * may cause superfluous increase/decrease of LBR_TOS.
170 */
171 if (!(lbr_select & LBR_CALL_STACK))
172 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
Andi Kleencd1f11d2015-03-20 10:11:24 -0700173 if (orig_debugctl != debugctl)
174 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100175}
176
177static void __intel_pmu_lbr_disable(void)
178{
179 u64 debugctl;
180
181 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100182 debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100183 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
184}
185
Kan Liang9f354a72020-07-03 05:49:08 -0700186void intel_pmu_lbr_reset_32(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100187{
188 int i;
189
190 for (i = 0; i < x86_pmu.lbr_nr; i++)
191 wrmsrl(x86_pmu.lbr_from + i, 0);
192}
193
Kan Liang9f354a72020-07-03 05:49:08 -0700194void intel_pmu_lbr_reset_64(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100195{
196 int i;
197
198 for (i = 0; i < x86_pmu.lbr_nr; i++) {
199 wrmsrl(x86_pmu.lbr_from + i, 0);
200 wrmsrl(x86_pmu.lbr_to + i, 0);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700201 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
202 wrmsrl(MSR_LBR_INFO_0 + i, 0);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100203 }
204}
205
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300206void intel_pmu_lbr_reset(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100207{
Kan Liang8b077e4a2018-06-05 08:38:46 -0700208 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
209
Peter Zijlstra74846d32010-03-05 13:49:35 +0100210 if (!x86_pmu.lbr_nr)
211 return;
212
Kan Liang9f354a72020-07-03 05:49:08 -0700213 x86_pmu.lbr_reset();
Kan Liang8b077e4a2018-06-05 08:38:46 -0700214
215 cpuc->last_task_ctx = NULL;
216 cpuc->last_log_id = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100217}
218
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500219/*
220 * TOS = most recently recorded branch
221 */
222static inline u64 intel_pmu_lbr_tos(void)
223{
224 u64 tos;
225
226 rdmsrl(x86_pmu.lbr_tos, tos);
227 return tos;
228}
229
230enum {
231 LBR_NONE,
232 LBR_VALID,
233};
234
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700235/*
236 * For formats with LBR_TSX flags (e.g. LBR_FORMAT_EIP_FLAGS2), bits 61:62 in
237 * MSR_LAST_BRANCH_FROM_x are the TSX flags when TSX is supported, but when
238 * TSX is not supported they have no consistent behavior:
239 *
240 * - For wrmsr(), bits 61:62 are considered part of the sign extension.
241 * - For HW updates (branch captures) bits 61:62 are always OFF and are not
242 * part of the sign extension.
243 *
244 * Therefore, if:
245 *
246 * 1) LBR has TSX format
247 * 2) CPU has no TSX support enabled
248 *
249 * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
250 * value from rdmsr() must be converted to have a 61 bits sign extension,
251 * ignoring the TSX flags.
252 */
253static inline bool lbr_from_signext_quirk_needed(void)
254{
255 int lbr_format = x86_pmu.intel_cap.lbr_format;
256 bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
257 boot_cpu_has(X86_FEATURE_RTM);
258
259 return !tsx_support && (lbr_desc[lbr_format] & LBR_TSX);
260}
261
Valdis Klētnieksd9f3b452019-08-08 13:44:02 -0400262static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700263
264/* If quirk is enabled, ensure sign extension is 63 bits: */
265inline u64 lbr_from_signext_quirk_wr(u64 val)
266{
267 if (static_branch_unlikely(&lbr_from_quirk_key)) {
268 /*
269 * Sign extend into bits 61:62 while preserving bit 63.
270 *
271 * Quirk is enabled when TSX is disabled. Therefore TSX bits
272 * in val are always OFF and must be changed to be sign
273 * extension bits. Since bits 59:60 are guaranteed to be
274 * part of the sign extension bits, we can just copy them
275 * to 61:62.
276 */
277 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
278 }
279 return val;
280}
281
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700282/*
283 * If quirk is needed, ensure sign extension is 61 bits:
284 */
Colin Ian Kinge91c8d92017-06-29 10:14:06 +0100285static u64 lbr_from_signext_quirk_rd(u64 val)
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700286{
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200287 if (static_branch_unlikely(&lbr_from_quirk_key)) {
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700288 /*
289 * Quirk is on when TSX is not enabled. Therefore TSX
290 * flags must be read as OFF.
291 */
292 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200293 }
294 return val;
295}
296
297static inline void wrlbr_from(unsigned int idx, u64 val)
298{
299 val = lbr_from_signext_quirk_wr(val);
300 wrmsrl(x86_pmu.lbr_from + idx, val);
301}
302
303static inline void wrlbr_to(unsigned int idx, u64 val)
304{
305 wrmsrl(x86_pmu.lbr_to + idx, val);
306}
307
308static inline u64 rdlbr_from(unsigned int idx)
309{
310 u64 val;
311
312 rdmsrl(x86_pmu.lbr_from + idx, val);
313
314 return lbr_from_signext_quirk_rd(val);
315}
316
317static inline u64 rdlbr_to(unsigned int idx)
318{
319 u64 val;
320
Peter Zijlstraaefbc4d2016-06-30 11:49:08 +0200321 rdmsrl(x86_pmu.lbr_to + idx, val);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200322
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700323 return val;
324}
325
Kan Liang799571b2020-07-03 05:49:10 -0700326void intel_pmu_lbr_restore(void *ctx)
327{
328 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
329 struct x86_perf_task_context *task_ctx = ctx;
330 int i;
331 unsigned lbr_idx, mask;
332 u64 tos = task_ctx->tos;
333
334 mask = x86_pmu.lbr_nr - 1;
335 for (i = 0; i < task_ctx->valid_lbrs; i++) {
336 lbr_idx = (tos - i) & mask;
337 wrlbr_from(lbr_idx, task_ctx->lbr_from[i]);
338 wrlbr_to (lbr_idx, task_ctx->lbr_to[i]);
339
340 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
341 wrmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
342 }
343
344 for (; i < x86_pmu.lbr_nr; i++) {
345 lbr_idx = (tos - i) & mask;
346 wrlbr_from(lbr_idx, 0);
347 wrlbr_to(lbr_idx, 0);
348 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
349 wrmsrl(MSR_LBR_INFO_0 + lbr_idx, 0);
350 }
351
352 wrmsrl(x86_pmu.lbr_tos, tos);
353
354 if (cpuc->lbr_select)
355 wrmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
356}
357
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500358static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
359{
Kan Liang8b077e4a2018-06-05 08:38:46 -0700360 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500361 u64 tos;
362
363 if (task_ctx->lbr_callstack_users == 0 ||
364 task_ctx->lbr_stack_state == LBR_NONE) {
365 intel_pmu_lbr_reset();
366 return;
367 }
368
Andi Kleenb28ae952015-10-20 11:46:33 -0700369 tos = task_ctx->tos;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700370 /*
371 * Does not restore the LBR registers, if
372 * - No one else touched them, and
373 * - Did not enter C6
374 */
375 if ((task_ctx == cpuc->last_task_ctx) &&
376 (task_ctx->log_id == cpuc->last_log_id) &&
377 rdlbr_from(tos)) {
378 task_ctx->lbr_stack_state = LBR_NONE;
379 return;
380 }
381
Kan Liang799571b2020-07-03 05:49:10 -0700382 x86_pmu.lbr_restore(task_ctx);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200383
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500384 task_ctx->lbr_stack_state = LBR_NONE;
385}
386
Kan Liang799571b2020-07-03 05:49:10 -0700387void intel_pmu_lbr_save(void *ctx)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500388{
Kan Liang8b077e4a2018-06-05 08:38:46 -0700389 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Kan Liang799571b2020-07-03 05:49:10 -0700390 struct x86_perf_task_context *task_ctx = ctx;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500391 unsigned lbr_idx, mask;
Kan Liang0592e572018-06-05 08:38:45 -0700392 u64 tos, from;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200393 int i;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500394
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500395 mask = x86_pmu.lbr_nr - 1;
396 tos = intel_pmu_lbr_tos();
Kan Liang0592e572018-06-05 08:38:45 -0700397 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500398 lbr_idx = (tos - i) & mask;
Kan Liang0592e572018-06-05 08:38:45 -0700399 from = rdlbr_from(lbr_idx);
400 if (!from)
401 break;
402 task_ctx->lbr_from[i] = from;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200403 task_ctx->lbr_to[i] = rdlbr_to(lbr_idx);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700404 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
Andi Kleene0573362015-05-27 21:13:17 -0700405 rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500406 }
Kan Liang0592e572018-06-05 08:38:45 -0700407 task_ctx->valid_lbrs = i;
Andi Kleenb28ae952015-10-20 11:46:33 -0700408 task_ctx->tos = tos;
Kan Liang799571b2020-07-03 05:49:10 -0700409
410 if (cpuc->lbr_select)
411 rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel);
412}
413
414static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
415{
416 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
417
418 if (task_ctx->lbr_callstack_users == 0) {
419 task_ctx->lbr_stack_state = LBR_NONE;
420 return;
421 }
422
423 x86_pmu.lbr_save(task_ctx);
424
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500425 task_ctx->lbr_stack_state = LBR_VALID;
Kan Liang8b077e4a2018-06-05 08:38:46 -0700426
427 cpuc->last_task_ctx = task_ctx;
428 cpuc->last_log_id = ++task_ctx->log_id;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500429}
430
Alexey Budankov421ca862019-10-23 10:12:54 +0300431void intel_pmu_lbr_swap_task_ctx(struct perf_event_context *prev,
432 struct perf_event_context *next)
433{
434 struct x86_perf_task_context *prev_ctx_data, *next_ctx_data;
435
436 swap(prev->task_ctx_data, next->task_ctx_data);
437
438 /*
439 * Architecture specific synchronization makes sense in
440 * case both prev->task_ctx_data and next->task_ctx_data
441 * pointers are allocated.
442 */
443
444 prev_ctx_data = next->task_ctx_data;
445 next_ctx_data = prev->task_ctx_data;
446
447 if (!prev_ctx_data || !next_ctx_data)
448 return;
449
450 swap(prev_ctx_data->lbr_callstack_users,
451 next_ctx_data->lbr_callstack_users);
452}
453
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500454void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
455{
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200456 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500457 struct x86_perf_task_context *task_ctx;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500458
Jiri Olsadf6c3db2017-07-19 09:52:47 +0200459 if (!cpuc->lbr_users)
460 return;
461
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500462 /*
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500463 * If LBR callstack feature is enabled and the stack was saved when
464 * the task was scheduled out, restore the stack. Otherwise flush
465 * the LBR stack.
466 */
467 task_ctx = ctx ? ctx->task_ctx_data : NULL;
468 if (task_ctx) {
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200469 if (sched_in)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500470 __intel_pmu_lbr_restore(task_ctx);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200471 else
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500472 __intel_pmu_lbr_save(task_ctx);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500473 return;
474 }
475
476 /*
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200477 * Since a context switch can flip the address space and LBR entries
478 * are not tagged with an identifier, we need to wipe the LBR, even for
479 * per-cpu events. You simply cannot resolve the branches from the old
480 * address space.
481 */
482 if (sched_in)
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500483 intel_pmu_lbr_reset();
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500484}
485
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500486static inline bool branch_user_callstack(unsigned br_sel)
487{
488 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
489}
490
Peter Zijlstra68f70822016-07-06 18:02:43 +0200491void intel_pmu_lbr_add(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100492{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500493 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500494 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100495
496 if (!x86_pmu.lbr_nr)
497 return;
498
Like Xue1ad1ac2020-06-13 16:09:50 +0800499 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
500 cpuc->lbr_select = 1;
501
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100502 cpuc->br_sel = event->hw.branch_reg.reg;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100503
Peter Zijlstraa5dcff62016-07-07 19:37:52 +0200504 if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data) {
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500505 task_ctx = event->ctx->task_ctx_data;
506 task_ctx->lbr_callstack_users++;
507 }
508
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200509 /*
510 * Request pmu::sched_task() callback, which will fire inside the
511 * regular perf event scheduling, so that call will:
512 *
513 * - restore or wipe; when LBR-callstack,
514 * - wipe; otherwise,
515 *
516 * when this is from __perf_event_task_sched_in().
517 *
518 * However, if this is from perf_install_in_context(), no such callback
519 * will follow and we'll need to reset the LBR here if this is the
520 * first LBR event.
521 *
522 * The problem is, we cannot tell these cases apart... but we can
523 * exclude the biggest chunk of cases by looking at
524 * event->total_time_running. An event that has accrued runtime cannot
525 * be 'new'. Conversely, a new event can get installed through the
526 * context switch path for the first time.
527 */
Andi Kleend3617b982019-04-02 12:45:03 -0700528 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
529 cpuc->lbr_pebs_users++;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500530 perf_sched_cb_inc(event->ctx->pmu);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200531 if (!cpuc->lbr_users++ && !event->total_time_running)
532 intel_pmu_lbr_reset();
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100533}
534
Peter Zijlstra68f70822016-07-06 18:02:43 +0200535void intel_pmu_lbr_del(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100536{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500537 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500538 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100539
540 if (!x86_pmu.lbr_nr)
541 return;
542
Dan Carpenter5c381812016-10-14 10:29:08 +0300543 if (branch_user_callstack(cpuc->br_sel) &&
544 event->ctx->task_ctx_data) {
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500545 task_ctx = event->ctx->task_ctx_data;
546 task_ctx->lbr_callstack_users--;
547 }
548
Like Xue1ad1ac2020-06-13 16:09:50 +0800549 if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT)
550 cpuc->lbr_select = 0;
551
Andi Kleend3617b982019-04-02 12:45:03 -0700552 if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0)
553 cpuc->lbr_pebs_users--;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100554 cpuc->lbr_users--;
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100555 WARN_ON_ONCE(cpuc->lbr_users < 0);
Andi Kleend3617b982019-04-02 12:45:03 -0700556 WARN_ON_ONCE(cpuc->lbr_pebs_users < 0);
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500557 perf_sched_cb_dec(event->ctx->pmu);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100558}
559
Like Xue1ad1ac2020-06-13 16:09:50 +0800560static inline bool vlbr_exclude_host(void)
561{
562 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
563
564 return test_bit(INTEL_PMC_IDX_FIXED_VLBR,
565 (unsigned long *)&cpuc->intel_ctrl_guest_mask);
566}
567
Andi Kleen1a78d932015-03-20 10:11:23 -0700568void intel_pmu_lbr_enable_all(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100569{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500570 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100571
Like Xue1ad1ac2020-06-13 16:09:50 +0800572 if (cpuc->lbr_users && !vlbr_exclude_host())
Andi Kleen1a78d932015-03-20 10:11:23 -0700573 __intel_pmu_lbr_enable(pmi);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100574}
575
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300576void intel_pmu_lbr_disable_all(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100577{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500578 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100579
Like Xue1ad1ac2020-06-13 16:09:50 +0800580 if (cpuc->lbr_users && !vlbr_exclude_host())
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100581 __intel_pmu_lbr_disable();
582}
583
Kan Liangc301b1d2020-07-03 05:49:09 -0700584void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100585{
586 unsigned long mask = x86_pmu.lbr_nr - 1;
587 u64 tos = intel_pmu_lbr_tos();
588 int i;
589
Peter Zijlstra63fb3f92010-03-09 11:51:02 +0100590 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100591 unsigned long lbr_idx = (tos - i) & mask;
592 union {
593 struct {
594 u32 from;
595 u32 to;
596 };
597 u64 lbr;
598 } msr_lastbranch;
599
600 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
601
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100602 cpuc->lbr_entries[i].from = msr_lastbranch.from;
603 cpuc->lbr_entries[i].to = msr_lastbranch.to;
604 cpuc->lbr_entries[i].mispred = 0;
605 cpuc->lbr_entries[i].predicted = 0;
Peter Zijlstraf2200ac2017-04-11 10:10:28 +0200606 cpuc->lbr_entries[i].in_tx = 0;
607 cpuc->lbr_entries[i].abort = 0;
608 cpuc->lbr_entries[i].cycles = 0;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800609 cpuc->lbr_entries[i].type = 0;
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100610 cpuc->lbr_entries[i].reserved = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100611 }
612 cpuc->lbr_stack.nr = i;
Kan Liangdb278b92020-01-27 08:53:55 -0800613 cpuc->lbr_stack.hw_idx = tos;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100614}
615
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100616/*
617 * Due to lack of segmentation in Linux the effective address (offset)
618 * is the same as the linear address, allowing us to merge the LIP and EIP
619 * LBR formats.
620 */
Kan Liangc301b1d2020-07-03 05:49:09 -0700621void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100622{
Kan Liang0592e572018-06-05 08:38:45 -0700623 bool need_info = false, call_stack = false;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100624 unsigned long mask = x86_pmu.lbr_nr - 1;
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100625 int lbr_format = x86_pmu.intel_cap.lbr_format;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100626 u64 tos = intel_pmu_lbr_tos();
627 int i;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700628 int out = 0;
Andi Kleen90405aa2015-05-27 21:13:18 -0700629 int num = x86_pmu.lbr_nr;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100630
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100631 if (cpuc->lbr_sel) {
632 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
633 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
Kan Liang0592e572018-06-05 08:38:45 -0700634 call_stack = true;
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100635 }
Andi Kleen90405aa2015-05-27 21:13:18 -0700636
637 for (i = 0; i < num; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100638 unsigned long lbr_idx = (tos - i) & mask;
Andi Kleen135c5612013-06-17 17:36:51 -0700639 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
640 int skip = 0;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700641 u16 cycles = 0;
Andi Kleen135c5612013-06-17 17:36:51 -0700642 int lbr_flags = lbr_desc[lbr_format];
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100643
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200644 from = rdlbr_from(lbr_idx);
645 to = rdlbr_to(lbr_idx);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100646
Kan Liang0592e572018-06-05 08:38:45 -0700647 /*
648 * Read LBR call stack entries
649 * until invalid entry (0s) is detected.
650 */
651 if (call_stack && !from)
652 break;
653
Andi Kleenb16a5b52015-10-20 11:46:34 -0700654 if (lbr_format == LBR_FORMAT_INFO && need_info) {
Andi Kleen50eab8f2015-05-10 12:22:43 -0700655 u64 info;
656
657 rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
658 mis = !!(info & LBR_INFO_MISPRED);
659 pred = !mis;
660 in_tx = !!(info & LBR_INFO_IN_TX);
661 abort = !!(info & LBR_INFO_ABORT);
662 cycles = (info & LBR_INFO_CYCLES);
663 }
Kan Liang8b92c3a2016-04-15 00:42:47 -0700664
665 if (lbr_format == LBR_FORMAT_TIME) {
666 mis = !!(from & LBR_FROM_FLAG_MISPRED);
667 pred = !mis;
668 skip = 1;
669 cycles = ((to >> 48) & LBR_INFO_CYCLES);
670
671 to = (u64)((((s64)to) << 16) >> 16);
672 }
673
Andi Kleen135c5612013-06-17 17:36:51 -0700674 if (lbr_flags & LBR_EIP_FLAGS) {
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100675 mis = !!(from & LBR_FROM_FLAG_MISPRED);
676 pred = !mis;
Andi Kleen135c5612013-06-17 17:36:51 -0700677 skip = 1;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100678 }
Andi Kleen135c5612013-06-17 17:36:51 -0700679 if (lbr_flags & LBR_TSX) {
680 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
681 abort = !!(from & LBR_FROM_FLAG_ABORT);
682 skip = 3;
683 }
684 from = (u64)((((s64)from) << skip) >> skip);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100685
Andi Kleenb7af41a2013-09-20 07:40:44 -0700686 /*
687 * Some CPUs report duplicated abort records,
688 * with the second entry not having an abort bit set.
689 * Skip them here. This loop runs backwards,
690 * so we need to undo the previous record.
691 * If the abort just happened outside the window
692 * the extra entry cannot be removed.
693 */
694 if (abort && x86_pmu.lbr_double_abort && out > 0)
695 out--;
696
697 cpuc->lbr_entries[out].from = from;
698 cpuc->lbr_entries[out].to = to;
699 cpuc->lbr_entries[out].mispred = mis;
700 cpuc->lbr_entries[out].predicted = pred;
701 cpuc->lbr_entries[out].in_tx = in_tx;
702 cpuc->lbr_entries[out].abort = abort;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700703 cpuc->lbr_entries[out].cycles = cycles;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800704 cpuc->lbr_entries[out].type = 0;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700705 cpuc->lbr_entries[out].reserved = 0;
706 out++;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100707 }
Andi Kleenb7af41a2013-09-20 07:40:44 -0700708 cpuc->lbr_stack.nr = out;
Kan Liangdb278b92020-01-27 08:53:55 -0800709 cpuc->lbr_stack.hw_idx = tos;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100710}
711
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300712void intel_pmu_lbr_read(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100713{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500714 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100715
Andi Kleend3617b982019-04-02 12:45:03 -0700716 /*
717 * Don't read when all LBRs users are using adaptive PEBS.
718 *
719 * This could be smarter and actually check the event,
720 * but this simple approach seems to work for now.
721 */
Like Xue1ad1ac2020-06-13 16:09:50 +0800722 if (!cpuc->lbr_users || vlbr_exclude_host() ||
723 cpuc->lbr_users == cpuc->lbr_pebs_users)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100724 return;
725
Kan Liangc301b1d2020-07-03 05:49:09 -0700726 x86_pmu.lbr_read(cpuc);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100727
728 intel_pmu_lbr_filter(cpuc);
729}
730
731/*
732 * SW filter is used:
733 * - in case there is no HW filter
734 * - in case the HW filter has errata or limitations
735 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500736static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100737{
738 u64 br_type = event->attr.branch_sample_type;
739 int mask = 0;
740
741 if (br_type & PERF_SAMPLE_BRANCH_USER)
742 mask |= X86_BR_USER;
743
Stephane Eranian2b923c82013-05-21 12:53:37 +0200744 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100745 mask |= X86_BR_KERNEL;
746
747 /* we ignore BRANCH_HV here */
748
749 if (br_type & PERF_SAMPLE_BRANCH_ANY)
750 mask |= X86_BR_ANY;
751
752 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
753 mask |= X86_BR_ANY_CALL;
754
755 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
756 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
757
758 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
759 mask |= X86_BR_IND_CALL;
Andi Kleen135c5612013-06-17 17:36:51 -0700760
761 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
762 mask |= X86_BR_ABORT;
763
764 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
765 mask |= X86_BR_IN_TX;
766
767 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
768 mask |= X86_BR_NO_TX;
769
Anshuman Khandual37548912014-05-22 12:50:09 +0530770 if (br_type & PERF_SAMPLE_BRANCH_COND)
771 mask |= X86_BR_JCC;
772
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500773 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
774 if (!x86_pmu_has_lbr_callstack())
775 return -EOPNOTSUPP;
776 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
777 return -EINVAL;
778 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
779 X86_BR_CALL_STACK;
780 }
781
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200782 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
783 mask |= X86_BR_IND_JMP;
784
Stephane Eraniand8928192015-10-13 09:09:09 +0200785 if (br_type & PERF_SAMPLE_BRANCH_CALL)
786 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800787
788 if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
789 mask |= X86_BR_TYPE_SAVE;
790
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100791 /*
792 * stash actual user request into reg, it may
793 * be used by fixup code for some CPU
794 */
795 event->hw.branch_reg.reg = mask;
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500796 return 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100797}
798
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100799/*
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100800 * setup the HW LBR filter
801 * Used only when available, may not be enough to disambiguate
802 * all branches, may need the help of the SW filter
803 */
804static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
805{
806 struct hw_perf_event_extra *reg;
807 u64 br_type = event->attr.branch_sample_type;
Yan, Zheng27ac9052014-11-04 21:55:57 -0500808 u64 mask = 0, v;
809 int i;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100810
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100811 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
Yan, Zheng27ac9052014-11-04 21:55:57 -0500812 if (!(br_type & (1ULL << i)))
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100813 continue;
814
Yan, Zheng27ac9052014-11-04 21:55:57 -0500815 v = x86_pmu.lbr_sel_map[i];
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100816 if (v == LBR_NOT_SUPP)
817 return -EOPNOTSUPP;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100818
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100819 if (v != LBR_IGN)
820 mask |= v;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100821 }
Andi Kleenb16a5b52015-10-20 11:46:34 -0700822
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100823 reg = &event->hw.branch_reg;
824 reg->idx = EXTRA_REG_LBR;
825
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500826 /*
827 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
828 * in suppress mode. So LBR_SELECT should be set to
829 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
Kan Liangcf3beb72016-04-21 02:30:10 -0700830 * But the 10th bit LBR_CALL_STACK does not operate
831 * in suppress mode.
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500832 */
Kan Liangcf3beb72016-04-21 02:30:10 -0700833 reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100834
Andi Kleenb16a5b52015-10-20 11:46:34 -0700835 if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
836 (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
837 (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
838 reg->config |= LBR_NO_INFO;
839
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100840 return 0;
841}
842
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100843int intel_pmu_setup_lbr_filter(struct perf_event *event)
844{
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100845 int ret = 0;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100846
847 /*
848 * no LBR on this PMU
849 */
850 if (!x86_pmu.lbr_nr)
851 return -EOPNOTSUPP;
852
853 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100854 * setup SW LBR filter
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100855 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500856 ret = intel_pmu_setup_sw_lbr_filter(event);
857 if (ret)
858 return ret;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100859
860 /*
861 * setup HW LBR filter, if any
862 */
863 if (x86_pmu.lbr_sel_map)
864 ret = intel_pmu_setup_hw_lbr_filter(event);
865
866 return ret;
867}
868
869/*
870 * return the type of control flow change at address "from"
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800871 * instruction is not necessarily a branch (in case of interrupt).
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100872 *
873 * The branch type returned also includes the priv level of the
874 * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
875 *
876 * If a branch type is unknown OR the instruction cannot be
877 * decoded (e.g., text page not present), then X86_BR_NONE is
878 * returned.
879 */
Andi Kleen135c5612013-06-17 17:36:51 -0700880static int branch_type(unsigned long from, unsigned long to, int abort)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100881{
882 struct insn insn;
883 void *addr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800884 int bytes_read, bytes_left;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100885 int ret = X86_BR_NONE;
886 int ext, to_plm, from_plm;
887 u8 buf[MAX_INSN_SIZE];
888 int is64 = 0;
889
890 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
891 from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
892
893 /*
894 * maybe zero if lbr did not fill up after a reset by the time
895 * we get a PMU interrupt
896 */
897 if (from == 0 || to == 0)
898 return X86_BR_NONE;
899
Andi Kleen135c5612013-06-17 17:36:51 -0700900 if (abort)
901 return X86_BR_ABORT | to_plm;
902
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100903 if (from_plm == X86_BR_USER) {
904 /*
905 * can happen if measuring at the user level only
906 * and we interrupt in a kernel thread, e.g., idle.
907 */
908 if (!current->mm)
909 return X86_BR_NONE;
910
911 /* may fail if text not present */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800912 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
913 MAX_INSN_SIZE);
914 bytes_read = MAX_INSN_SIZE - bytes_left;
915 if (!bytes_read)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100916 return X86_BR_NONE;
917
918 addr = buf;
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200919 } else {
920 /*
921 * The LBR logs any address in the IP, even if the IP just
922 * faulted. This means userspace can control the from address.
923 * Ensure we don't blindy read any address by validating it is
924 * a known text address.
925 */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800926 if (kernel_text_address(from)) {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200927 addr = (void *)from;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800928 /*
929 * Assume we can get the maximum possible size
930 * when grabbing kernel data. This is not
931 * _strictly_ true since we could possibly be
932 * executing up next to a memory hole, but
933 * it is very unlikely to be a problem.
934 */
935 bytes_read = MAX_INSN_SIZE;
936 } else {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200937 return X86_BR_NONE;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800938 }
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200939 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100940
941 /*
942 * decoder needs to know the ABI especially
943 * on 64-bit systems running 32-bit apps
944 */
945#ifdef CONFIG_X86_64
946 is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
947#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800948 insn_init(&insn, addr, bytes_read, is64);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100949 insn_get_opcode(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800950 if (!insn.opcode.got)
951 return X86_BR_ABORT;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100952
953 switch (insn.opcode.bytes[0]) {
954 case 0xf:
955 switch (insn.opcode.bytes[1]) {
956 case 0x05: /* syscall */
957 case 0x34: /* sysenter */
958 ret = X86_BR_SYSCALL;
959 break;
960 case 0x07: /* sysret */
961 case 0x35: /* sysexit */
962 ret = X86_BR_SYSRET;
963 break;
964 case 0x80 ... 0x8f: /* conditional */
965 ret = X86_BR_JCC;
966 break;
967 default:
968 ret = X86_BR_NONE;
969 }
970 break;
971 case 0x70 ... 0x7f: /* conditional */
972 ret = X86_BR_JCC;
973 break;
974 case 0xc2: /* near ret */
975 case 0xc3: /* near ret */
976 case 0xca: /* far ret */
977 case 0xcb: /* far ret */
978 ret = X86_BR_RET;
979 break;
980 case 0xcf: /* iret */
981 ret = X86_BR_IRET;
982 break;
983 case 0xcc ... 0xce: /* int */
984 ret = X86_BR_INT;
985 break;
986 case 0xe8: /* call near rel */
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500987 insn_get_immediate(&insn);
988 if (insn.immediate1.value == 0) {
989 /* zero length call */
990 ret = X86_BR_ZERO_CALL;
991 break;
992 }
Gustavo A. R. Silva2b0fc372019-01-25 12:49:17 -0600993 /* fall through */
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100994 case 0x9a: /* call far absolute */
995 ret = X86_BR_CALL;
996 break;
997 case 0xe0 ... 0xe3: /* loop jmp */
998 ret = X86_BR_JCC;
999 break;
1000 case 0xe9 ... 0xeb: /* jmp */
1001 ret = X86_BR_JMP;
1002 break;
1003 case 0xff: /* call near absolute, call far absolute ind */
1004 insn_get_modrm(&insn);
1005 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
1006 switch (ext) {
1007 case 2: /* near ind call */
1008 case 3: /* far ind call */
1009 ret = X86_BR_IND_CALL;
1010 break;
1011 case 4:
1012 case 5:
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001013 ret = X86_BR_IND_JMP;
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001014 break;
1015 }
1016 break;
1017 default:
1018 ret = X86_BR_NONE;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001019 }
1020 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001021 * interrupts, traps, faults (and thus ring transition) may
1022 * occur on any instructions. Thus, to classify them correctly,
1023 * we need to first look at the from and to priv levels. If they
1024 * are different and to is in the kernel, then it indicates
1025 * a ring transition. If the from instruction is not a ring
1026 * transition instr (syscall, systenter, int), then it means
1027 * it was a irq, trap or fault.
1028 *
1029 * we have no way of detecting kernel to kernel faults.
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001030 */
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001031 if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
1032 && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
1033 ret = X86_BR_IRQ;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001034
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001035 /*
1036 * branch priv level determined by target as
1037 * is done by HW when LBR_SELECT is implemented
1038 */
1039 if (ret != X86_BR_NONE)
1040 ret |= to_plm;
1041
1042 return ret;
1043}
1044
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001045#define X86_BR_TYPE_MAP_MAX 16
1046
1047static int branch_map[X86_BR_TYPE_MAP_MAX] = {
1048 PERF_BR_CALL, /* X86_BR_CALL */
1049 PERF_BR_RET, /* X86_BR_RET */
1050 PERF_BR_SYSCALL, /* X86_BR_SYSCALL */
1051 PERF_BR_SYSRET, /* X86_BR_SYSRET */
1052 PERF_BR_UNKNOWN, /* X86_BR_INT */
1053 PERF_BR_UNKNOWN, /* X86_BR_IRET */
1054 PERF_BR_COND, /* X86_BR_JCC */
1055 PERF_BR_UNCOND, /* X86_BR_JMP */
1056 PERF_BR_UNKNOWN, /* X86_BR_IRQ */
1057 PERF_BR_IND_CALL, /* X86_BR_IND_CALL */
1058 PERF_BR_UNKNOWN, /* X86_BR_ABORT */
1059 PERF_BR_UNKNOWN, /* X86_BR_IN_TX */
1060 PERF_BR_UNKNOWN, /* X86_BR_NO_TX */
1061 PERF_BR_CALL, /* X86_BR_ZERO_CALL */
1062 PERF_BR_UNKNOWN, /* X86_BR_CALL_STACK */
1063 PERF_BR_IND, /* X86_BR_IND_JMP */
1064};
1065
1066static int
1067common_branch_type(int type)
1068{
1069 int i;
1070
1071 type >>= 2; /* skip X86_BR_USER and X86_BR_KERNEL */
1072
1073 if (type) {
1074 i = __ffs(type);
1075 if (i < X86_BR_TYPE_MAP_MAX)
1076 return branch_map[i];
1077 }
1078
1079 return PERF_BR_UNKNOWN;
1080}
1081
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001082/*
1083 * implement actual branch filter based on user demand.
1084 * Hardware may not exactly satisfy that request, thus
1085 * we need to inspect opcodes. Mismatched branches are
1086 * discarded. Therefore, the number of branches returned
1087 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
1088 */
1089static void
1090intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
1091{
1092 u64 from, to;
1093 int br_sel = cpuc->br_sel;
1094 int i, j, type;
1095 bool compress = false;
1096
1097 /* if sampling all branches, then nothing to filter */
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001098 if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
1099 ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001100 return;
1101
1102 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
1103
1104 from = cpuc->lbr_entries[i].from;
1105 to = cpuc->lbr_entries[i].to;
1106
Andi Kleen135c5612013-06-17 17:36:51 -07001107 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1108 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1109 if (cpuc->lbr_entries[i].in_tx)
1110 type |= X86_BR_IN_TX;
1111 else
1112 type |= X86_BR_NO_TX;
1113 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001114
1115 /* if type does not correspond, then discard */
1116 if (type == X86_BR_NONE || (br_sel & type) != type) {
1117 cpuc->lbr_entries[i].from = 0;
1118 compress = true;
1119 }
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001120
1121 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1122 cpuc->lbr_entries[i].type = common_branch_type(type);
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001123 }
1124
1125 if (!compress)
1126 return;
1127
1128 /* remove all entries with from=0 */
1129 for (i = 0; i < cpuc->lbr_stack.nr; ) {
1130 if (!cpuc->lbr_entries[i].from) {
1131 j = i;
1132 while (++j < cpuc->lbr_stack.nr)
1133 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1134 cpuc->lbr_stack.nr--;
1135 if (!cpuc->lbr_entries[i].from)
1136 continue;
1137 }
1138 i++;
1139 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001140}
1141
Kan Liangc22497f2019-04-02 12:45:02 -07001142void intel_pmu_store_pebs_lbrs(struct pebs_lbr *lbr)
1143{
1144 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
1145 int i;
1146
1147 cpuc->lbr_stack.nr = x86_pmu.lbr_nr;
Kan Liangdb278b92020-01-27 08:53:55 -08001148
1149 /* Cannot get TOS for large PEBS */
1150 if (cpuc->n_pebs == cpuc->n_large_pebs)
1151 cpuc->lbr_stack.hw_idx = -1ULL;
1152 else
1153 cpuc->lbr_stack.hw_idx = intel_pmu_lbr_tos();
1154
Kan Liangc22497f2019-04-02 12:45:02 -07001155 for (i = 0; i < x86_pmu.lbr_nr; i++) {
1156 u64 info = lbr->lbr[i].info;
1157 struct perf_branch_entry *e = &cpuc->lbr_entries[i];
1158
1159 e->from = lbr->lbr[i].from;
1160 e->to = lbr->lbr[i].to;
1161 e->mispred = !!(info & LBR_INFO_MISPRED);
1162 e->predicted = !(info & LBR_INFO_MISPRED);
1163 e->in_tx = !!(info & LBR_INFO_IN_TX);
1164 e->abort = !!(info & LBR_INFO_ABORT);
1165 e->cycles = info & LBR_INFO_CYCLES;
1166 e->reserved = 0;
1167 }
1168 intel_pmu_lbr_filter(cpuc);
1169}
1170
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001171/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001172 * Map interface branch filters onto LBR filters
1173 */
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001174static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001175 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1176 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1177 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1178 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1179 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
1180 | LBR_IND_JMP | LBR_FAR,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001181 /*
1182 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1183 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001184 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001185 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1186 /*
1187 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1188 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001189 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1190 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001191 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001192};
1193
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001194static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001195 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1196 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1197 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1198 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1199 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1200 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1201 | LBR_FAR,
1202 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1203 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001204 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001205 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001206};
1207
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001208static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001209 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1210 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1211 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1212 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1213 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1214 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1215 | LBR_FAR,
1216 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1217 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1218 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1219 | LBR_RETURN | LBR_CALL_STACK,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001220 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001221 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001222};
1223
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001224/* core */
Mathias Krause066ce642014-08-26 18:49:45 +02001225void __init intel_pmu_lbr_init_core(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001226{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001227 x86_pmu.lbr_nr = 4;
Stephane Eranian225ce532012-02-09 23:20:52 +01001228 x86_pmu.lbr_tos = MSR_LBR_TOS;
1229 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1230 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001231
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001232 /*
1233 * SW branch filter usage:
1234 * - compensate for lack of HW filter
1235 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001236}
1237
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001238/* nehalem/westmere */
Mathias Krause066ce642014-08-26 18:49:45 +02001239void __init intel_pmu_lbr_init_nhm(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001240{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001241 x86_pmu.lbr_nr = 16;
Stephane Eranian225ce532012-02-09 23:20:52 +01001242 x86_pmu.lbr_tos = MSR_LBR_TOS;
1243 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1244 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001245
1246 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1247 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1248
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001249 /*
1250 * SW branch filter usage:
1251 * - workaround LBR_SEL errata (see above)
1252 * - support syscall, sysret capture.
1253 * That requires LBR_FAR but that means far
1254 * jmp need to be filtered out
1255 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001256}
1257
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001258/* sandy bridge */
Mathias Krause066ce642014-08-26 18:49:45 +02001259void __init intel_pmu_lbr_init_snb(void)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001260{
1261 x86_pmu.lbr_nr = 16;
1262 x86_pmu.lbr_tos = MSR_LBR_TOS;
1263 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1264 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1265
1266 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1267 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1268
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001269 /*
1270 * SW branch filter usage:
1271 * - support syscall, sysret capture.
1272 * That requires LBR_FAR but that means far
1273 * jmp need to be filtered out
1274 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001275}
1276
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001277/* haswell */
1278void intel_pmu_lbr_init_hsw(void)
1279{
1280 x86_pmu.lbr_nr = 16;
1281 x86_pmu.lbr_tos = MSR_LBR_TOS;
1282 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1283 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1284
1285 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1286 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -07001287
1288 if (lbr_from_signext_quirk_needed())
1289 static_branch_enable(&lbr_from_quirk_key);
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001290}
1291
Andi Kleen9a92e162015-05-10 12:22:44 -07001292/* skylake */
1293__init void intel_pmu_lbr_init_skl(void)
1294{
1295 x86_pmu.lbr_nr = 32;
1296 x86_pmu.lbr_tos = MSR_LBR_TOS;
1297 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1298 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1299
1300 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1301 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1302
1303 /*
1304 * SW branch filter usage:
1305 * - support syscall, sysret capture.
1306 * That requires LBR_FAR but that means far
1307 * jmp need to be filtered out
1308 */
Andi Kleen9a92e162015-05-10 12:22:44 -07001309}
1310
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001311/* atom */
Mathias Krause066ce642014-08-26 18:49:45 +02001312void __init intel_pmu_lbr_init_atom(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001313{
Stephane Eranian88c9a652012-02-09 23:20:56 +01001314 /*
1315 * only models starting at stepping 10 seems
1316 * to have an operational LBR which can freeze
1317 * on PMU interrupt
1318 */
Stephane Eranian3ec18cd2012-08-20 11:24:21 +02001319 if (boot_cpu_data.x86_model == 28
Jia Zhangb3991512018-01-01 09:52:10 +08001320 && boot_cpu_data.x86_stepping < 10) {
Stephane Eranian88c9a652012-02-09 23:20:56 +01001321 pr_cont("LBR disabled due to erratum");
1322 return;
1323 }
1324
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001325 x86_pmu.lbr_nr = 8;
Stephane Eranian225ce532012-02-09 23:20:52 +01001326 x86_pmu.lbr_tos = MSR_LBR_TOS;
1327 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1328 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001329
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001330 /*
1331 * SW branch filter usage:
1332 * - compensate for lack of HW filter
1333 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001334}
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001335
Kan Liangf21d5ad2016-04-15 00:53:45 -07001336/* slm */
1337void __init intel_pmu_lbr_init_slm(void)
1338{
1339 x86_pmu.lbr_nr = 8;
1340 x86_pmu.lbr_tos = MSR_LBR_TOS;
1341 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1342 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1343
1344 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1345 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1346
1347 /*
1348 * SW branch filter usage:
1349 * - compensate for lack of HW filter
1350 */
1351 pr_cont("8-deep LBR, ");
1352}
1353
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001354/* Knights Landing */
1355void intel_pmu_lbr_init_knl(void)
1356{
1357 x86_pmu.lbr_nr = 8;
1358 x86_pmu.lbr_tos = MSR_LBR_TOS;
1359 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1360 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1361
1362 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1363 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
Jacek Tomaka16160c12018-08-02 09:38:30 +08001364
1365 /* Knights Landing does have MISPREDICT bit */
1366 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_LIP)
1367 x86_pmu.intel_cap.lbr_format = LBR_FORMAT_EIP_FLAGS;
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001368}
Like Xub2d65042020-06-13 16:09:48 +08001369
1370/**
1371 * x86_perf_get_lbr - get the LBR records information
1372 *
1373 * @lbr: the caller's memory to store the LBR records information
1374 *
1375 * Returns: 0 indicates the LBR info has been successfully obtained
1376 */
1377int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
1378{
1379 int lbr_fmt = x86_pmu.intel_cap.lbr_format;
1380
1381 lbr->nr = x86_pmu.lbr_nr;
1382 lbr->from = x86_pmu.lbr_from;
1383 lbr->to = x86_pmu.lbr_to;
1384 lbr->info = (lbr_fmt == LBR_FORMAT_INFO) ? MSR_LBR_INFO_0 : 0;
1385
1386 return 0;
1387}
1388EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
Like Xu097e4312020-06-13 16:09:49 +08001389
1390struct event_constraint vlbr_constraint =
Like Xue1ad1ac2020-06-13 16:09:50 +08001391 __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR),
1392 FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT);