blob: 0edda489cf3663350b5f50d006fdacb2a38ce61a [file] [log] [blame]
Kevin Winchesterde0428a2011-08-30 20:41:05 -03001#include <linux/perf_event.h>
2#include <linux/types.h>
3
4#include <asm/perf_event.h>
5#include <asm/msr.h>
Stephane Eranian3e702ff2012-02-09 23:20:58 +01006#include <asm/insn.h>
Kevin Winchesterde0428a2011-08-30 20:41:05 -03007
Borislav Petkov27f6d222016-02-10 10:55:23 +01008#include "../perf_event.h"
Peter Zijlstracaff2be2010-03-03 12:02:30 +01009
10enum {
11 LBR_FORMAT_32 = 0x00,
12 LBR_FORMAT_LIP = 0x01,
13 LBR_FORMAT_EIP = 0x02,
14 LBR_FORMAT_EIP_FLAGS = 0x03,
Andi Kleen135c5612013-06-17 17:36:51 -070015 LBR_FORMAT_EIP_FLAGS2 = 0x04,
Andi Kleen50eab8f2015-05-10 12:22:43 -070016 LBR_FORMAT_INFO = 0x05,
Kan Liang8b92c3a2016-04-15 00:42:47 -070017 LBR_FORMAT_TIME = 0x06,
18 LBR_FORMAT_MAX_KNOWN = LBR_FORMAT_TIME,
Andi Kleen135c5612013-06-17 17:36:51 -070019};
20
Colin Ian Kinge91c8d92017-06-29 10:14:06 +010021static const enum {
Andi Kleen135c5612013-06-17 17:36:51 -070022 LBR_EIP_FLAGS = 1,
23 LBR_TSX = 2,
24} lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
25 [LBR_FORMAT_EIP_FLAGS] = LBR_EIP_FLAGS,
26 [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
Peter Zijlstracaff2be2010-03-03 12:02:30 +010027};
28
29/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010030 * Intel LBR_SELECT bits
31 * Intel Vol3a, April 2011, Section 16.7 Table 16-10
32 *
33 * Hardware branch filter (not available on all CPUs)
34 */
35#define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
36#define LBR_USER_BIT 1 /* do not capture at ring > 0 */
37#define LBR_JCC_BIT 2 /* do not capture conditional branches */
38#define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
39#define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
40#define LBR_RETURN_BIT 5 /* do not capture near returns */
41#define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
42#define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
43#define LBR_FAR_BIT 8 /* do not capture far branches */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050044#define LBR_CALL_STACK_BIT 9 /* enable call stack */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010045
Andi Kleenb16a5b52015-10-20 11:46:34 -070046/*
47 * Following bit only exists in Linux; we mask it out before writing it to
48 * the actual MSR. But it helps the constraint perf code to understand
49 * that this is a separate configuration.
50 */
51#define LBR_NO_INFO_BIT 63 /* don't read LBR_INFO. */
52
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010053#define LBR_KERNEL (1 << LBR_KERNEL_BIT)
54#define LBR_USER (1 << LBR_USER_BIT)
55#define LBR_JCC (1 << LBR_JCC_BIT)
56#define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
57#define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
58#define LBR_RETURN (1 << LBR_RETURN_BIT)
59#define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
60#define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
61#define LBR_FAR (1 << LBR_FAR_BIT)
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050062#define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
Andi Kleenb16a5b52015-10-20 11:46:34 -070063#define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010064
65#define LBR_PLM (LBR_KERNEL | LBR_USER)
66
Kan Liangcf3beb72016-04-21 02:30:10 -070067#define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010068#define LBR_NOT_SUPP -1 /* LBR filter not supported */
69#define LBR_IGN 0 /* ignored */
70
71#define LBR_ANY \
72 (LBR_JCC |\
73 LBR_REL_CALL |\
74 LBR_IND_CALL |\
75 LBR_RETURN |\
76 LBR_REL_JMP |\
77 LBR_IND_JMP |\
78 LBR_FAR)
79
David Carrillo-Cisneros3812bba2016-06-21 11:31:12 -070080#define LBR_FROM_FLAG_MISPRED BIT_ULL(63)
81#define LBR_FROM_FLAG_IN_TX BIT_ULL(62)
82#define LBR_FROM_FLAG_ABORT BIT_ULL(61)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010083
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -070084#define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59))
85
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010086/*
Stephane Eranian3e702ff2012-02-09 23:20:58 +010087 * x86control flow change classification
88 * x86control flow changes include branches, interrupts, traps, faults
89 */
90enum {
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050091 X86_BR_NONE = 0, /* unknown */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010092
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050093 X86_BR_USER = 1 << 0, /* branch target is user */
94 X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010095
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -050096 X86_BR_CALL = 1 << 2, /* call */
97 X86_BR_RET = 1 << 3, /* return */
98 X86_BR_SYSCALL = 1 << 4, /* syscall */
99 X86_BR_SYSRET = 1 << 5, /* syscall return */
100 X86_BR_INT = 1 << 6, /* sw interrupt */
101 X86_BR_IRET = 1 << 7, /* return from interrupt */
102 X86_BR_JCC = 1 << 8, /* conditional */
103 X86_BR_JMP = 1 << 9, /* jump */
104 X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
105 X86_BR_IND_CALL = 1 << 11,/* indirect calls */
106 X86_BR_ABORT = 1 << 12,/* transaction abort */
107 X86_BR_IN_TX = 1 << 13,/* in transaction */
108 X86_BR_NO_TX = 1 << 14,/* not in transaction */
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500109 X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
110 X86_BR_CALL_STACK = 1 << 16,/* call stack */
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200111 X86_BR_IND_JMP = 1 << 17,/* indirect jump */
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800112
113 X86_BR_TYPE_SAVE = 1 << 18,/* indicate to save branch type */
114
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100115};
116
117#define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
Andi Kleen135c5612013-06-17 17:36:51 -0700118#define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100119
120#define X86_BR_ANY \
121 (X86_BR_CALL |\
122 X86_BR_RET |\
123 X86_BR_SYSCALL |\
124 X86_BR_SYSRET |\
125 X86_BR_INT |\
126 X86_BR_IRET |\
127 X86_BR_JCC |\
128 X86_BR_JMP |\
129 X86_BR_IRQ |\
Andi Kleen135c5612013-06-17 17:36:51 -0700130 X86_BR_ABORT |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500131 X86_BR_IND_CALL |\
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200132 X86_BR_IND_JMP |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500133 X86_BR_ZERO_CALL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100134
135#define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
136
137#define X86_BR_ANY_CALL \
138 (X86_BR_CALL |\
139 X86_BR_IND_CALL |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500140 X86_BR_ZERO_CALL |\
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100141 X86_BR_SYSCALL |\
142 X86_BR_IRQ |\
143 X86_BR_INT)
144
145static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
146
147/*
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100148 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
149 * otherwise it becomes near impossible to get a reliable stack.
150 */
151
Andi Kleen1a78d932015-03-20 10:11:23 -0700152static void __intel_pmu_lbr_enable(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100153{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500154 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700155 u64 debugctl, lbr_select = 0, orig_debugctl;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100156
Andi Kleen1a78d932015-03-20 10:11:23 -0700157 /*
Andi Kleen425507f2015-05-10 12:22:46 -0700158 * No need to unfreeze manually, as v4 can do that as part
159 * of the GLOBAL_STATUS ack.
160 */
161 if (pmi && x86_pmu.version >= 4)
162 return;
163
164 /*
Andi Kleen1a78d932015-03-20 10:11:23 -0700165 * No need to reprogram LBR_SELECT in a PMI, as it
166 * did not change.
167 */
Kan Liang96f3eda2015-09-14 10:14:07 -0400168 if (cpuc->lbr_sel)
Andi Kleenb16a5b52015-10-20 11:46:34 -0700169 lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100170 if (!pmi && cpuc->lbr_sel)
Yan, Zheng2c70d002014-11-04 21:56:10 -0500171 wrmsrl(MSR_LBR_SELECT, lbr_select);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100172
173 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700174 orig_debugctl = debugctl;
Yan, Zheng2c70d002014-11-04 21:56:10 -0500175 debugctl |= DEBUGCTLMSR_LBR;
176 /*
177 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
178 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
179 * may cause superfluous increase/decrease of LBR_TOS.
180 */
181 if (!(lbr_select & LBR_CALL_STACK))
182 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
Andi Kleencd1f11d2015-03-20 10:11:24 -0700183 if (orig_debugctl != debugctl)
184 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100185}
186
187static void __intel_pmu_lbr_disable(void)
188{
189 u64 debugctl;
190
191 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100192 debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100193 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
194}
195
196static void intel_pmu_lbr_reset_32(void)
197{
198 int i;
199
200 for (i = 0; i < x86_pmu.lbr_nr; i++)
201 wrmsrl(x86_pmu.lbr_from + i, 0);
202}
203
204static void intel_pmu_lbr_reset_64(void)
205{
206 int i;
207
208 for (i = 0; i < x86_pmu.lbr_nr; i++) {
209 wrmsrl(x86_pmu.lbr_from + i, 0);
210 wrmsrl(x86_pmu.lbr_to + i, 0);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700211 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
212 wrmsrl(MSR_LBR_INFO_0 + i, 0);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100213 }
214}
215
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300216void intel_pmu_lbr_reset(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100217{
Peter Zijlstra74846d32010-03-05 13:49:35 +0100218 if (!x86_pmu.lbr_nr)
219 return;
220
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100221 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100222 intel_pmu_lbr_reset_32();
223 else
224 intel_pmu_lbr_reset_64();
225}
226
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500227/*
228 * TOS = most recently recorded branch
229 */
230static inline u64 intel_pmu_lbr_tos(void)
231{
232 u64 tos;
233
234 rdmsrl(x86_pmu.lbr_tos, tos);
235 return tos;
236}
237
238enum {
239 LBR_NONE,
240 LBR_VALID,
241};
242
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -0700243/*
244 * For formats with LBR_TSX flags (e.g. LBR_FORMAT_EIP_FLAGS2), bits 61:62 in
245 * MSR_LAST_BRANCH_FROM_x are the TSX flags when TSX is supported, but when
246 * TSX is not supported they have no consistent behavior:
247 *
248 * - For wrmsr(), bits 61:62 are considered part of the sign extension.
249 * - For HW updates (branch captures) bits 61:62 are always OFF and are not
250 * part of the sign extension.
251 *
252 * Therefore, if:
253 *
254 * 1) LBR has TSX format
255 * 2) CPU has no TSX support enabled
256 *
257 * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
258 * value from rdmsr() must be converted to have a 61 bits sign extension,
259 * ignoring the TSX flags.
260 */
261static inline bool lbr_from_signext_quirk_needed(void)
262{
263 int lbr_format = x86_pmu.intel_cap.lbr_format;
264 bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
265 boot_cpu_has(X86_FEATURE_RTM);
266
267 return !tsx_support && (lbr_desc[lbr_format] & LBR_TSX);
268}
269
270DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
271
272/* If quirk is enabled, ensure sign extension is 63 bits: */
273inline u64 lbr_from_signext_quirk_wr(u64 val)
274{
275 if (static_branch_unlikely(&lbr_from_quirk_key)) {
276 /*
277 * Sign extend into bits 61:62 while preserving bit 63.
278 *
279 * Quirk is enabled when TSX is disabled. Therefore TSX bits
280 * in val are always OFF and must be changed to be sign
281 * extension bits. Since bits 59:60 are guaranteed to be
282 * part of the sign extension bits, we can just copy them
283 * to 61:62.
284 */
285 val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
286 }
287 return val;
288}
289
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700290/*
291 * If quirk is needed, ensure sign extension is 61 bits:
292 */
Colin Ian Kinge91c8d92017-06-29 10:14:06 +0100293static u64 lbr_from_signext_quirk_rd(u64 val)
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700294{
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200295 if (static_branch_unlikely(&lbr_from_quirk_key)) {
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700296 /*
297 * Quirk is on when TSX is not enabled. Therefore TSX
298 * flags must be read as OFF.
299 */
300 val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200301 }
302 return val;
303}
304
305static inline void wrlbr_from(unsigned int idx, u64 val)
306{
307 val = lbr_from_signext_quirk_wr(val);
308 wrmsrl(x86_pmu.lbr_from + idx, val);
309}
310
311static inline void wrlbr_to(unsigned int idx, u64 val)
312{
313 wrmsrl(x86_pmu.lbr_to + idx, val);
314}
315
316static inline u64 rdlbr_from(unsigned int idx)
317{
318 u64 val;
319
320 rdmsrl(x86_pmu.lbr_from + idx, val);
321
322 return lbr_from_signext_quirk_rd(val);
323}
324
325static inline u64 rdlbr_to(unsigned int idx)
326{
327 u64 val;
328
Peter Zijlstraaefbc4d2016-06-30 11:49:08 +0200329 rdmsrl(x86_pmu.lbr_to + idx, val);
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200330
David Carrillo-Cisneros71adae92016-06-21 11:31:13 -0700331 return val;
332}
333
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500334static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
335{
336 int i;
337 unsigned lbr_idx, mask;
338 u64 tos;
339
340 if (task_ctx->lbr_callstack_users == 0 ||
341 task_ctx->lbr_stack_state == LBR_NONE) {
342 intel_pmu_lbr_reset();
343 return;
344 }
345
346 mask = x86_pmu.lbr_nr - 1;
Andi Kleenb28ae952015-10-20 11:46:33 -0700347 tos = task_ctx->tos;
Andi Kleen90405aa2015-05-27 21:13:18 -0700348 for (i = 0; i < tos; i++) {
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500349 lbr_idx = (tos - i) & mask;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200350 wrlbr_from(lbr_idx, task_ctx->lbr_from[i]);
351 wrlbr_to (lbr_idx, task_ctx->lbr_to[i]);
352
Andi Kleen50eab8f2015-05-10 12:22:43 -0700353 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
Andi Kleene0573362015-05-27 21:13:17 -0700354 wrmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500355 }
Andi Kleenb28ae952015-10-20 11:46:33 -0700356 wrmsrl(x86_pmu.lbr_tos, tos);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500357 task_ctx->lbr_stack_state = LBR_NONE;
358}
359
360static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
361{
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500362 unsigned lbr_idx, mask;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200363 u64 tos;
364 int i;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500365
366 if (task_ctx->lbr_callstack_users == 0) {
367 task_ctx->lbr_stack_state = LBR_NONE;
368 return;
369 }
370
371 mask = x86_pmu.lbr_nr - 1;
372 tos = intel_pmu_lbr_tos();
Andi Kleen90405aa2015-05-27 21:13:18 -0700373 for (i = 0; i < tos; i++) {
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500374 lbr_idx = (tos - i) & mask;
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200375 task_ctx->lbr_from[i] = rdlbr_from(lbr_idx);
376 task_ctx->lbr_to[i] = rdlbr_to(lbr_idx);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700377 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
Andi Kleene0573362015-05-27 21:13:17 -0700378 rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500379 }
Andi Kleenb28ae952015-10-20 11:46:33 -0700380 task_ctx->tos = tos;
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500381 task_ctx->lbr_stack_state = LBR_VALID;
382}
383
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500384void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
385{
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500386 struct x86_perf_task_context *task_ctx;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500387
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500388 /*
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500389 * If LBR callstack feature is enabled and the stack was saved when
390 * the task was scheduled out, restore the stack. Otherwise flush
391 * the LBR stack.
392 */
393 task_ctx = ctx ? ctx->task_ctx_data : NULL;
394 if (task_ctx) {
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200395 if (sched_in)
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500396 __intel_pmu_lbr_restore(task_ctx);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200397 else
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500398 __intel_pmu_lbr_save(task_ctx);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500399 return;
400 }
401
402 /*
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200403 * Since a context switch can flip the address space and LBR entries
404 * are not tagged with an identifier, we need to wipe the LBR, even for
405 * per-cpu events. You simply cannot resolve the branches from the old
406 * address space.
407 */
408 if (sched_in)
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500409 intel_pmu_lbr_reset();
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500410}
411
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500412static inline bool branch_user_callstack(unsigned br_sel)
413{
414 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
415}
416
Peter Zijlstra68f70822016-07-06 18:02:43 +0200417void intel_pmu_lbr_add(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100418{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500419 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500420 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100421
422 if (!x86_pmu.lbr_nr)
423 return;
424
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100425 cpuc->br_sel = event->hw.branch_reg.reg;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100426
Peter Zijlstraa5dcff62016-07-07 19:37:52 +0200427 if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data) {
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500428 task_ctx = event->ctx->task_ctx_data;
429 task_ctx->lbr_callstack_users++;
430 }
431
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200432 /*
433 * Request pmu::sched_task() callback, which will fire inside the
434 * regular perf event scheduling, so that call will:
435 *
436 * - restore or wipe; when LBR-callstack,
437 * - wipe; otherwise,
438 *
439 * when this is from __perf_event_task_sched_in().
440 *
441 * However, if this is from perf_install_in_context(), no such callback
442 * will follow and we'll need to reset the LBR here if this is the
443 * first LBR event.
444 *
445 * The problem is, we cannot tell these cases apart... but we can
446 * exclude the biggest chunk of cases by looking at
447 * event->total_time_running. An event that has accrued runtime cannot
448 * be 'new'. Conversely, a new event can get installed through the
449 * context switch path for the first time.
450 */
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500451 perf_sched_cb_inc(event->ctx->pmu);
Peter Zijlstra3e2c1a62016-07-07 19:37:52 +0200452 if (!cpuc->lbr_users++ && !event->total_time_running)
453 intel_pmu_lbr_reset();
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100454}
455
Peter Zijlstra68f70822016-07-06 18:02:43 +0200456void intel_pmu_lbr_del(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100457{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500458 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500459 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100460
461 if (!x86_pmu.lbr_nr)
462 return;
463
Dan Carpenter5c381812016-10-14 10:29:08 +0300464 if (branch_user_callstack(cpuc->br_sel) &&
465 event->ctx->task_ctx_data) {
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500466 task_ctx = event->ctx->task_ctx_data;
467 task_ctx->lbr_callstack_users--;
468 }
469
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100470 cpuc->lbr_users--;
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100471 WARN_ON_ONCE(cpuc->lbr_users < 0);
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500472 perf_sched_cb_dec(event->ctx->pmu);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100473}
474
Andi Kleen1a78d932015-03-20 10:11:23 -0700475void intel_pmu_lbr_enable_all(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100476{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500477 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100478
479 if (cpuc->lbr_users)
Andi Kleen1a78d932015-03-20 10:11:23 -0700480 __intel_pmu_lbr_enable(pmi);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100481}
482
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300483void intel_pmu_lbr_disable_all(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100484{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500485 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100486
487 if (cpuc->lbr_users)
488 __intel_pmu_lbr_disable();
489}
490
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100491static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
492{
493 unsigned long mask = x86_pmu.lbr_nr - 1;
494 u64 tos = intel_pmu_lbr_tos();
495 int i;
496
Peter Zijlstra63fb3f92010-03-09 11:51:02 +0100497 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100498 unsigned long lbr_idx = (tos - i) & mask;
499 union {
500 struct {
501 u32 from;
502 u32 to;
503 };
504 u64 lbr;
505 } msr_lastbranch;
506
507 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
508
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100509 cpuc->lbr_entries[i].from = msr_lastbranch.from;
510 cpuc->lbr_entries[i].to = msr_lastbranch.to;
511 cpuc->lbr_entries[i].mispred = 0;
512 cpuc->lbr_entries[i].predicted = 0;
Peter Zijlstraf2200ac2017-04-11 10:10:28 +0200513 cpuc->lbr_entries[i].in_tx = 0;
514 cpuc->lbr_entries[i].abort = 0;
515 cpuc->lbr_entries[i].cycles = 0;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800516 cpuc->lbr_entries[i].type = 0;
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100517 cpuc->lbr_entries[i].reserved = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100518 }
519 cpuc->lbr_stack.nr = i;
520}
521
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100522/*
523 * Due to lack of segmentation in Linux the effective address (offset)
524 * is the same as the linear address, allowing us to merge the LIP and EIP
525 * LBR formats.
526 */
527static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
528{
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100529 bool need_info = false;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100530 unsigned long mask = x86_pmu.lbr_nr - 1;
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100531 int lbr_format = x86_pmu.intel_cap.lbr_format;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100532 u64 tos = intel_pmu_lbr_tos();
533 int i;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700534 int out = 0;
Andi Kleen90405aa2015-05-27 21:13:18 -0700535 int num = x86_pmu.lbr_nr;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100536
Stephane Eranian6fc2e832015-12-03 23:33:17 +0100537 if (cpuc->lbr_sel) {
538 need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
539 if (cpuc->lbr_sel->config & LBR_CALL_STACK)
540 num = tos;
541 }
Andi Kleen90405aa2015-05-27 21:13:18 -0700542
543 for (i = 0; i < num; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100544 unsigned long lbr_idx = (tos - i) & mask;
Andi Kleen135c5612013-06-17 17:36:51 -0700545 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
546 int skip = 0;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700547 u16 cycles = 0;
Andi Kleen135c5612013-06-17 17:36:51 -0700548 int lbr_flags = lbr_desc[lbr_format];
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100549
Peter Zijlstrad4cf1942016-06-23 10:44:49 +0200550 from = rdlbr_from(lbr_idx);
551 to = rdlbr_to(lbr_idx);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100552
Andi Kleenb16a5b52015-10-20 11:46:34 -0700553 if (lbr_format == LBR_FORMAT_INFO && need_info) {
Andi Kleen50eab8f2015-05-10 12:22:43 -0700554 u64 info;
555
556 rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
557 mis = !!(info & LBR_INFO_MISPRED);
558 pred = !mis;
559 in_tx = !!(info & LBR_INFO_IN_TX);
560 abort = !!(info & LBR_INFO_ABORT);
561 cycles = (info & LBR_INFO_CYCLES);
562 }
Kan Liang8b92c3a2016-04-15 00:42:47 -0700563
564 if (lbr_format == LBR_FORMAT_TIME) {
565 mis = !!(from & LBR_FROM_FLAG_MISPRED);
566 pred = !mis;
567 skip = 1;
568 cycles = ((to >> 48) & LBR_INFO_CYCLES);
569
570 to = (u64)((((s64)to) << 16) >> 16);
571 }
572
Andi Kleen135c5612013-06-17 17:36:51 -0700573 if (lbr_flags & LBR_EIP_FLAGS) {
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100574 mis = !!(from & LBR_FROM_FLAG_MISPRED);
575 pred = !mis;
Andi Kleen135c5612013-06-17 17:36:51 -0700576 skip = 1;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100577 }
Andi Kleen135c5612013-06-17 17:36:51 -0700578 if (lbr_flags & LBR_TSX) {
579 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
580 abort = !!(from & LBR_FROM_FLAG_ABORT);
581 skip = 3;
582 }
583 from = (u64)((((s64)from) << skip) >> skip);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100584
Andi Kleenb7af41a2013-09-20 07:40:44 -0700585 /*
586 * Some CPUs report duplicated abort records,
587 * with the second entry not having an abort bit set.
588 * Skip them here. This loop runs backwards,
589 * so we need to undo the previous record.
590 * If the abort just happened outside the window
591 * the extra entry cannot be removed.
592 */
593 if (abort && x86_pmu.lbr_double_abort && out > 0)
594 out--;
595
596 cpuc->lbr_entries[out].from = from;
597 cpuc->lbr_entries[out].to = to;
598 cpuc->lbr_entries[out].mispred = mis;
599 cpuc->lbr_entries[out].predicted = pred;
600 cpuc->lbr_entries[out].in_tx = in_tx;
601 cpuc->lbr_entries[out].abort = abort;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700602 cpuc->lbr_entries[out].cycles = cycles;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800603 cpuc->lbr_entries[out].type = 0;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700604 cpuc->lbr_entries[out].reserved = 0;
605 out++;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100606 }
Andi Kleenb7af41a2013-09-20 07:40:44 -0700607 cpuc->lbr_stack.nr = out;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100608}
609
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300610void intel_pmu_lbr_read(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100611{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500612 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100613
614 if (!cpuc->lbr_users)
615 return;
616
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100617 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100618 intel_pmu_lbr_read_32(cpuc);
619 else
620 intel_pmu_lbr_read_64(cpuc);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100621
622 intel_pmu_lbr_filter(cpuc);
623}
624
625/*
626 * SW filter is used:
627 * - in case there is no HW filter
628 * - in case the HW filter has errata or limitations
629 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500630static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100631{
632 u64 br_type = event->attr.branch_sample_type;
633 int mask = 0;
634
635 if (br_type & PERF_SAMPLE_BRANCH_USER)
636 mask |= X86_BR_USER;
637
Stephane Eranian2b923c82013-05-21 12:53:37 +0200638 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100639 mask |= X86_BR_KERNEL;
640
641 /* we ignore BRANCH_HV here */
642
643 if (br_type & PERF_SAMPLE_BRANCH_ANY)
644 mask |= X86_BR_ANY;
645
646 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
647 mask |= X86_BR_ANY_CALL;
648
649 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
650 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
651
652 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
653 mask |= X86_BR_IND_CALL;
Andi Kleen135c5612013-06-17 17:36:51 -0700654
655 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
656 mask |= X86_BR_ABORT;
657
658 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
659 mask |= X86_BR_IN_TX;
660
661 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
662 mask |= X86_BR_NO_TX;
663
Anshuman Khandual37548912014-05-22 12:50:09 +0530664 if (br_type & PERF_SAMPLE_BRANCH_COND)
665 mask |= X86_BR_JCC;
666
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500667 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
668 if (!x86_pmu_has_lbr_callstack())
669 return -EOPNOTSUPP;
670 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
671 return -EINVAL;
672 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
673 X86_BR_CALL_STACK;
674 }
675
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200676 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
677 mask |= X86_BR_IND_JMP;
678
Stephane Eraniand8928192015-10-13 09:09:09 +0200679 if (br_type & PERF_SAMPLE_BRANCH_CALL)
680 mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800681
682 if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE)
683 mask |= X86_BR_TYPE_SAVE;
684
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100685 /*
686 * stash actual user request into reg, it may
687 * be used by fixup code for some CPU
688 */
689 event->hw.branch_reg.reg = mask;
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500690 return 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100691}
692
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100693/*
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100694 * setup the HW LBR filter
695 * Used only when available, may not be enough to disambiguate
696 * all branches, may need the help of the SW filter
697 */
698static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
699{
700 struct hw_perf_event_extra *reg;
701 u64 br_type = event->attr.branch_sample_type;
Yan, Zheng27ac9052014-11-04 21:55:57 -0500702 u64 mask = 0, v;
703 int i;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100704
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100705 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
Yan, Zheng27ac9052014-11-04 21:55:57 -0500706 if (!(br_type & (1ULL << i)))
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100707 continue;
708
Yan, Zheng27ac9052014-11-04 21:55:57 -0500709 v = x86_pmu.lbr_sel_map[i];
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100710 if (v == LBR_NOT_SUPP)
711 return -EOPNOTSUPP;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100712
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100713 if (v != LBR_IGN)
714 mask |= v;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100715 }
Andi Kleenb16a5b52015-10-20 11:46:34 -0700716
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100717 reg = &event->hw.branch_reg;
718 reg->idx = EXTRA_REG_LBR;
719
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500720 /*
721 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
722 * in suppress mode. So LBR_SELECT should be set to
723 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
Kan Liangcf3beb72016-04-21 02:30:10 -0700724 * But the 10th bit LBR_CALL_STACK does not operate
725 * in suppress mode.
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500726 */
Kan Liangcf3beb72016-04-21 02:30:10 -0700727 reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100728
Andi Kleenb16a5b52015-10-20 11:46:34 -0700729 if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
730 (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
731 (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
732 reg->config |= LBR_NO_INFO;
733
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100734 return 0;
735}
736
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100737int intel_pmu_setup_lbr_filter(struct perf_event *event)
738{
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100739 int ret = 0;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100740
741 /*
742 * no LBR on this PMU
743 */
744 if (!x86_pmu.lbr_nr)
745 return -EOPNOTSUPP;
746
747 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100748 * setup SW LBR filter
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100749 */
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -0500750 ret = intel_pmu_setup_sw_lbr_filter(event);
751 if (ret)
752 return ret;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100753
754 /*
755 * setup HW LBR filter, if any
756 */
757 if (x86_pmu.lbr_sel_map)
758 ret = intel_pmu_setup_hw_lbr_filter(event);
759
760 return ret;
761}
762
763/*
764 * return the type of control flow change at address "from"
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800765 * instruction is not necessarily a branch (in case of interrupt).
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100766 *
767 * The branch type returned also includes the priv level of the
768 * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
769 *
770 * If a branch type is unknown OR the instruction cannot be
771 * decoded (e.g., text page not present), then X86_BR_NONE is
772 * returned.
773 */
Andi Kleen135c5612013-06-17 17:36:51 -0700774static int branch_type(unsigned long from, unsigned long to, int abort)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100775{
776 struct insn insn;
777 void *addr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800778 int bytes_read, bytes_left;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100779 int ret = X86_BR_NONE;
780 int ext, to_plm, from_plm;
781 u8 buf[MAX_INSN_SIZE];
782 int is64 = 0;
783
784 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
785 from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
786
787 /*
788 * maybe zero if lbr did not fill up after a reset by the time
789 * we get a PMU interrupt
790 */
791 if (from == 0 || to == 0)
792 return X86_BR_NONE;
793
Andi Kleen135c5612013-06-17 17:36:51 -0700794 if (abort)
795 return X86_BR_ABORT | to_plm;
796
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100797 if (from_plm == X86_BR_USER) {
798 /*
799 * can happen if measuring at the user level only
800 * and we interrupt in a kernel thread, e.g., idle.
801 */
802 if (!current->mm)
803 return X86_BR_NONE;
804
805 /* may fail if text not present */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800806 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
807 MAX_INSN_SIZE);
808 bytes_read = MAX_INSN_SIZE - bytes_left;
809 if (!bytes_read)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100810 return X86_BR_NONE;
811
812 addr = buf;
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200813 } else {
814 /*
815 * The LBR logs any address in the IP, even if the IP just
816 * faulted. This means userspace can control the from address.
817 * Ensure we don't blindy read any address by validating it is
818 * a known text address.
819 */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800820 if (kernel_text_address(from)) {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200821 addr = (void *)from;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800822 /*
823 * Assume we can get the maximum possible size
824 * when grabbing kernel data. This is not
825 * _strictly_ true since we could possibly be
826 * executing up next to a memory hole, but
827 * it is very unlikely to be a problem.
828 */
829 bytes_read = MAX_INSN_SIZE;
830 } else {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200831 return X86_BR_NONE;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800832 }
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200833 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100834
835 /*
836 * decoder needs to know the ABI especially
837 * on 64-bit systems running 32-bit apps
838 */
839#ifdef CONFIG_X86_64
840 is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
841#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800842 insn_init(&insn, addr, bytes_read, is64);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100843 insn_get_opcode(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800844 if (!insn.opcode.got)
845 return X86_BR_ABORT;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100846
847 switch (insn.opcode.bytes[0]) {
848 case 0xf:
849 switch (insn.opcode.bytes[1]) {
850 case 0x05: /* syscall */
851 case 0x34: /* sysenter */
852 ret = X86_BR_SYSCALL;
853 break;
854 case 0x07: /* sysret */
855 case 0x35: /* sysexit */
856 ret = X86_BR_SYSRET;
857 break;
858 case 0x80 ... 0x8f: /* conditional */
859 ret = X86_BR_JCC;
860 break;
861 default:
862 ret = X86_BR_NONE;
863 }
864 break;
865 case 0x70 ... 0x7f: /* conditional */
866 ret = X86_BR_JCC;
867 break;
868 case 0xc2: /* near ret */
869 case 0xc3: /* near ret */
870 case 0xca: /* far ret */
871 case 0xcb: /* far ret */
872 ret = X86_BR_RET;
873 break;
874 case 0xcf: /* iret */
875 ret = X86_BR_IRET;
876 break;
877 case 0xcc ... 0xce: /* int */
878 ret = X86_BR_INT;
879 break;
880 case 0xe8: /* call near rel */
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500881 insn_get_immediate(&insn);
882 if (insn.immediate1.value == 0) {
883 /* zero length call */
884 ret = X86_BR_ZERO_CALL;
885 break;
886 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100887 case 0x9a: /* call far absolute */
888 ret = X86_BR_CALL;
889 break;
890 case 0xe0 ... 0xe3: /* loop jmp */
891 ret = X86_BR_JCC;
892 break;
893 case 0xe9 ... 0xeb: /* jmp */
894 ret = X86_BR_JMP;
895 break;
896 case 0xff: /* call near absolute, call far absolute ind */
897 insn_get_modrm(&insn);
898 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
899 switch (ext) {
900 case 2: /* near ind call */
901 case 3: /* far ind call */
902 ret = X86_BR_IND_CALL;
903 break;
904 case 4:
905 case 5:
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200906 ret = X86_BR_IND_JMP;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100907 break;
908 }
909 break;
910 default:
911 ret = X86_BR_NONE;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100912 }
913 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100914 * interrupts, traps, faults (and thus ring transition) may
915 * occur on any instructions. Thus, to classify them correctly,
916 * we need to first look at the from and to priv levels. If they
917 * are different and to is in the kernel, then it indicates
918 * a ring transition. If the from instruction is not a ring
919 * transition instr (syscall, systenter, int), then it means
920 * it was a irq, trap or fault.
921 *
922 * we have no way of detecting kernel to kernel faults.
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100923 */
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100924 if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
925 && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
926 ret = X86_BR_IRQ;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100927
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100928 /*
929 * branch priv level determined by target as
930 * is done by HW when LBR_SELECT is implemented
931 */
932 if (ret != X86_BR_NONE)
933 ret |= to_plm;
934
935 return ret;
936}
937
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800938#define X86_BR_TYPE_MAP_MAX 16
939
940static int branch_map[X86_BR_TYPE_MAP_MAX] = {
941 PERF_BR_CALL, /* X86_BR_CALL */
942 PERF_BR_RET, /* X86_BR_RET */
943 PERF_BR_SYSCALL, /* X86_BR_SYSCALL */
944 PERF_BR_SYSRET, /* X86_BR_SYSRET */
945 PERF_BR_UNKNOWN, /* X86_BR_INT */
946 PERF_BR_UNKNOWN, /* X86_BR_IRET */
947 PERF_BR_COND, /* X86_BR_JCC */
948 PERF_BR_UNCOND, /* X86_BR_JMP */
949 PERF_BR_UNKNOWN, /* X86_BR_IRQ */
950 PERF_BR_IND_CALL, /* X86_BR_IND_CALL */
951 PERF_BR_UNKNOWN, /* X86_BR_ABORT */
952 PERF_BR_UNKNOWN, /* X86_BR_IN_TX */
953 PERF_BR_UNKNOWN, /* X86_BR_NO_TX */
954 PERF_BR_CALL, /* X86_BR_ZERO_CALL */
955 PERF_BR_UNKNOWN, /* X86_BR_CALL_STACK */
956 PERF_BR_IND, /* X86_BR_IND_JMP */
957};
958
959static int
960common_branch_type(int type)
961{
962 int i;
963
964 type >>= 2; /* skip X86_BR_USER and X86_BR_KERNEL */
965
966 if (type) {
967 i = __ffs(type);
968 if (i < X86_BR_TYPE_MAP_MAX)
969 return branch_map[i];
970 }
971
972 return PERF_BR_UNKNOWN;
973}
974
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100975/*
976 * implement actual branch filter based on user demand.
977 * Hardware may not exactly satisfy that request, thus
978 * we need to inspect opcodes. Mismatched branches are
979 * discarded. Therefore, the number of branches returned
980 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
981 */
982static void
983intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
984{
985 u64 from, to;
986 int br_sel = cpuc->br_sel;
987 int i, j, type;
988 bool compress = false;
989
990 /* if sampling all branches, then nothing to filter */
Jin Yaod5c7f9d2017-07-18 20:13:10 +0800991 if (((br_sel & X86_BR_ALL) == X86_BR_ALL) &&
992 ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE))
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100993 return;
994
995 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
996
997 from = cpuc->lbr_entries[i].from;
998 to = cpuc->lbr_entries[i].to;
999
Andi Kleen135c5612013-06-17 17:36:51 -07001000 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
1001 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
1002 if (cpuc->lbr_entries[i].in_tx)
1003 type |= X86_BR_IN_TX;
1004 else
1005 type |= X86_BR_NO_TX;
1006 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001007
1008 /* if type does not correspond, then discard */
1009 if (type == X86_BR_NONE || (br_sel & type) != type) {
1010 cpuc->lbr_entries[i].from = 0;
1011 compress = true;
1012 }
Jin Yaod5c7f9d2017-07-18 20:13:10 +08001013
1014 if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE)
1015 cpuc->lbr_entries[i].type = common_branch_type(type);
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001016 }
1017
1018 if (!compress)
1019 return;
1020
1021 /* remove all entries with from=0 */
1022 for (i = 0; i < cpuc->lbr_stack.nr; ) {
1023 if (!cpuc->lbr_entries[i].from) {
1024 j = i;
1025 while (++j < cpuc->lbr_stack.nr)
1026 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
1027 cpuc->lbr_stack.nr--;
1028 if (!cpuc->lbr_entries[i].from)
1029 continue;
1030 }
1031 i++;
1032 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +01001033}
1034
1035/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001036 * Map interface branch filters onto LBR filters
1037 */
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001038static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001039 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1040 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1041 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1042 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1043 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
1044 | LBR_IND_JMP | LBR_FAR,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001045 /*
1046 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
1047 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001048 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001049 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
1050 /*
1051 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
1052 */
Yan, Zheng27ac9052014-11-04 21:55:57 -05001053 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
1054 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001055 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001056};
1057
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001058static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -05001059 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1060 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1061 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1062 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1063 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1064 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1065 | LBR_FAR,
1066 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1067 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001068 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001069 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001070};
1071
Peter Zijlstra2c44b192014-11-05 10:36:45 +01001072static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001073 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
1074 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
1075 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
1076 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
1077 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
1078 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1079 | LBR_FAR,
1080 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
1081 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
1082 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
1083 | LBR_RETURN | LBR_CALL_STACK,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +02001084 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eraniand8928192015-10-13 09:09:09 +02001085 [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001086};
1087
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001088/* core */
Mathias Krause066ce642014-08-26 18:49:45 +02001089void __init intel_pmu_lbr_init_core(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001090{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001091 x86_pmu.lbr_nr = 4;
Stephane Eranian225ce532012-02-09 23:20:52 +01001092 x86_pmu.lbr_tos = MSR_LBR_TOS;
1093 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1094 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001095
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001096 /*
1097 * SW branch filter usage:
1098 * - compensate for lack of HW filter
1099 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001100}
1101
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001102/* nehalem/westmere */
Mathias Krause066ce642014-08-26 18:49:45 +02001103void __init intel_pmu_lbr_init_nhm(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001104{
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001105 x86_pmu.lbr_nr = 16;
Stephane Eranian225ce532012-02-09 23:20:52 +01001106 x86_pmu.lbr_tos = MSR_LBR_TOS;
1107 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1108 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001109
1110 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1111 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1112
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001113 /*
1114 * SW branch filter usage:
1115 * - workaround LBR_SEL errata (see above)
1116 * - support syscall, sysret capture.
1117 * That requires LBR_FAR but that means far
1118 * jmp need to be filtered out
1119 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001120}
1121
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001122/* sandy bridge */
Mathias Krause066ce642014-08-26 18:49:45 +02001123void __init intel_pmu_lbr_init_snb(void)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001124{
1125 x86_pmu.lbr_nr = 16;
1126 x86_pmu.lbr_tos = MSR_LBR_TOS;
1127 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1128 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1129
1130 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1131 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
1132
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001133 /*
1134 * SW branch filter usage:
1135 * - support syscall, sysret capture.
1136 * That requires LBR_FAR but that means far
1137 * jmp need to be filtered out
1138 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001139}
1140
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001141/* haswell */
1142void intel_pmu_lbr_init_hsw(void)
1143{
1144 x86_pmu.lbr_nr = 16;
1145 x86_pmu.lbr_tos = MSR_LBR_TOS;
1146 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1147 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1148
1149 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1150 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
David Carrillo-Cisneros19fc9dd2016-06-21 11:31:11 -07001151
1152 if (lbr_from_signext_quirk_needed())
1153 static_branch_enable(&lbr_from_quirk_key);
Yan, Zhenge9d7f7cd2014-11-04 21:56:00 -05001154}
1155
Andi Kleen9a92e162015-05-10 12:22:44 -07001156/* skylake */
1157__init void intel_pmu_lbr_init_skl(void)
1158{
1159 x86_pmu.lbr_nr = 32;
1160 x86_pmu.lbr_tos = MSR_LBR_TOS;
1161 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1162 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1163
1164 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1165 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
1166
1167 /*
1168 * SW branch filter usage:
1169 * - support syscall, sysret capture.
1170 * That requires LBR_FAR but that means far
1171 * jmp need to be filtered out
1172 */
Andi Kleen9a92e162015-05-10 12:22:44 -07001173}
1174
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001175/* atom */
Mathias Krause066ce642014-08-26 18:49:45 +02001176void __init intel_pmu_lbr_init_atom(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001177{
Stephane Eranian88c9a652012-02-09 23:20:56 +01001178 /*
1179 * only models starting at stepping 10 seems
1180 * to have an operational LBR which can freeze
1181 * on PMU interrupt
1182 */
Stephane Eranian3ec18cd2012-08-20 11:24:21 +02001183 if (boot_cpu_data.x86_model == 28
1184 && boot_cpu_data.x86_mask < 10) {
Stephane Eranian88c9a652012-02-09 23:20:56 +01001185 pr_cont("LBR disabled due to erratum");
1186 return;
1187 }
1188
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001189 x86_pmu.lbr_nr = 8;
Stephane Eranian225ce532012-02-09 23:20:52 +01001190 x86_pmu.lbr_tos = MSR_LBR_TOS;
1191 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1192 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +01001193
Stephane Eranian3e702ff2012-02-09 23:20:58 +01001194 /*
1195 * SW branch filter usage:
1196 * - compensate for lack of HW filter
1197 */
Peter Zijlstracaff2be2010-03-03 12:02:30 +01001198}
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001199
Kan Liangf21d5ad2016-04-15 00:53:45 -07001200/* slm */
1201void __init intel_pmu_lbr_init_slm(void)
1202{
1203 x86_pmu.lbr_nr = 8;
1204 x86_pmu.lbr_tos = MSR_LBR_TOS;
1205 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
1206 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
1207
1208 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1209 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
1210
1211 /*
1212 * SW branch filter usage:
1213 * - compensate for lack of HW filter
1214 */
1215 pr_cont("8-deep LBR, ");
1216}
1217
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001218/* Knights Landing */
1219void intel_pmu_lbr_init_knl(void)
1220{
1221 x86_pmu.lbr_nr = 8;
1222 x86_pmu.lbr_tos = MSR_LBR_TOS;
1223 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
1224 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
1225
1226 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
1227 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
Harish Chegondi1e7b9392015-12-07 14:28:18 -08001228}