blob: 2fb57373beca72f5121458f0ec2cb9cdae751634 [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
8#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,
17 LBR_FORMAT_MAX_KNOWN = LBR_FORMAT_INFO,
Andi Kleen135c5612013-06-17 17:36:51 -070018};
19
20static enum {
21 LBR_EIP_FLAGS = 1,
22 LBR_TSX = 2,
23} lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
24 [LBR_FORMAT_EIP_FLAGS] = LBR_EIP_FLAGS,
25 [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
Peter Zijlstracaff2be2010-03-03 12:02:30 +010026};
27
28/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010029 * Intel LBR_SELECT bits
30 * Intel Vol3a, April 2011, Section 16.7 Table 16-10
31 *
32 * Hardware branch filter (not available on all CPUs)
33 */
34#define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
35#define LBR_USER_BIT 1 /* do not capture at ring > 0 */
36#define LBR_JCC_BIT 2 /* do not capture conditional branches */
37#define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
38#define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
39#define LBR_RETURN_BIT 5 /* do not capture near returns */
40#define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
41#define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
42#define LBR_FAR_BIT 8 /* do not capture far branches */
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -050043#define LBR_CALL_STACK_BIT 9 /* enable call stack */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010044
45#define LBR_KERNEL (1 << LBR_KERNEL_BIT)
46#define LBR_USER (1 << LBR_USER_BIT)
47#define LBR_JCC (1 << LBR_JCC_BIT)
48#define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
49#define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
50#define LBR_RETURN (1 << LBR_RETURN_BIT)
51#define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
52#define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
53#define LBR_FAR (1 << LBR_FAR_BIT)
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -050054#define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010055
56#define LBR_PLM (LBR_KERNEL | LBR_USER)
57
58#define LBR_SEL_MASK 0x1ff /* valid bits in LBR_SELECT */
59#define LBR_NOT_SUPP -1 /* LBR filter not supported */
60#define LBR_IGN 0 /* ignored */
61
62#define LBR_ANY \
63 (LBR_JCC |\
64 LBR_REL_CALL |\
65 LBR_IND_CALL |\
66 LBR_RETURN |\
67 LBR_REL_JMP |\
68 LBR_IND_JMP |\
69 LBR_FAR)
70
71#define LBR_FROM_FLAG_MISPRED (1ULL << 63)
Andi Kleen135c5612013-06-17 17:36:51 -070072#define LBR_FROM_FLAG_IN_TX (1ULL << 62)
73#define LBR_FROM_FLAG_ABORT (1ULL << 61)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +010074
75/*
Stephane Eranian3e702ff2012-02-09 23:20:58 +010076 * x86control flow change classification
77 * x86control flow changes include branches, interrupts, traps, faults
78 */
79enum {
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -050080 X86_BR_NONE = 0, /* unknown */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010081
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -050082 X86_BR_USER = 1 << 0, /* branch target is user */
83 X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
Stephane Eranian3e702ff2012-02-09 23:20:58 +010084
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -050085 X86_BR_CALL = 1 << 2, /* call */
86 X86_BR_RET = 1 << 3, /* return */
87 X86_BR_SYSCALL = 1 << 4, /* syscall */
88 X86_BR_SYSRET = 1 << 5, /* syscall return */
89 X86_BR_INT = 1 << 6, /* sw interrupt */
90 X86_BR_IRET = 1 << 7, /* return from interrupt */
91 X86_BR_JCC = 1 << 8, /* conditional */
92 X86_BR_JMP = 1 << 9, /* jump */
93 X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
94 X86_BR_IND_CALL = 1 << 11,/* indirect calls */
95 X86_BR_ABORT = 1 << 12,/* transaction abort */
96 X86_BR_IN_TX = 1 << 13,/* in transaction */
97 X86_BR_NO_TX = 1 << 14,/* not in transaction */
Yan, Zhengaa54ae92014-11-04 21:56:11 -050098 X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
99 X86_BR_CALL_STACK = 1 << 16,/* call stack */
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200100 X86_BR_IND_JMP = 1 << 17,/* indirect jump */
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100101};
102
103#define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
Andi Kleen135c5612013-06-17 17:36:51 -0700104#define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100105
106#define X86_BR_ANY \
107 (X86_BR_CALL |\
108 X86_BR_RET |\
109 X86_BR_SYSCALL |\
110 X86_BR_SYSRET |\
111 X86_BR_INT |\
112 X86_BR_IRET |\
113 X86_BR_JCC |\
114 X86_BR_JMP |\
115 X86_BR_IRQ |\
Andi Kleen135c5612013-06-17 17:36:51 -0700116 X86_BR_ABORT |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500117 X86_BR_IND_CALL |\
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200118 X86_BR_IND_JMP |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500119 X86_BR_ZERO_CALL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100120
121#define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
122
123#define X86_BR_ANY_CALL \
124 (X86_BR_CALL |\
125 X86_BR_IND_CALL |\
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500126 X86_BR_ZERO_CALL |\
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100127 X86_BR_SYSCALL |\
128 X86_BR_IRQ |\
129 X86_BR_INT)
130
131static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
132
133/*
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100134 * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
135 * otherwise it becomes near impossible to get a reliable stack.
136 */
137
Andi Kleen1a78d932015-03-20 10:11:23 -0700138static void __intel_pmu_lbr_enable(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100139{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500140 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700141 u64 debugctl, lbr_select = 0, orig_debugctl;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100142
Andi Kleen1a78d932015-03-20 10:11:23 -0700143 /*
144 * No need to reprogram LBR_SELECT in a PMI, as it
145 * did not change.
146 */
147 if (cpuc->lbr_sel && !pmi) {
Yan, Zheng2c70d002014-11-04 21:56:10 -0500148 lbr_select = cpuc->lbr_sel->config;
149 wrmsrl(MSR_LBR_SELECT, lbr_select);
150 }
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100151
152 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Andi Kleencd1f11d2015-03-20 10:11:24 -0700153 orig_debugctl = debugctl;
Yan, Zheng2c70d002014-11-04 21:56:10 -0500154 debugctl |= DEBUGCTLMSR_LBR;
155 /*
156 * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
157 * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
158 * may cause superfluous increase/decrease of LBR_TOS.
159 */
160 if (!(lbr_select & LBR_CALL_STACK))
161 debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
Andi Kleencd1f11d2015-03-20 10:11:24 -0700162 if (orig_debugctl != debugctl)
163 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100164}
165
166static void __intel_pmu_lbr_disable(void)
167{
168 u64 debugctl;
169
170 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
Peter Zijlstra7c5ecaf2010-03-25 14:51:49 +0100171 debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100172 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
173}
174
175static void intel_pmu_lbr_reset_32(void)
176{
177 int i;
178
179 for (i = 0; i < x86_pmu.lbr_nr; i++)
180 wrmsrl(x86_pmu.lbr_from + i, 0);
181}
182
183static void intel_pmu_lbr_reset_64(void)
184{
185 int i;
186
187 for (i = 0; i < x86_pmu.lbr_nr; i++) {
188 wrmsrl(x86_pmu.lbr_from + i, 0);
189 wrmsrl(x86_pmu.lbr_to + i, 0);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700190 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
191 wrmsrl(MSR_LBR_INFO_0 + i, 0);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100192 }
193}
194
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300195void intel_pmu_lbr_reset(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100196{
Peter Zijlstra74846d32010-03-05 13:49:35 +0100197 if (!x86_pmu.lbr_nr)
198 return;
199
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100200 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100201 intel_pmu_lbr_reset_32();
202 else
203 intel_pmu_lbr_reset_64();
204}
205
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500206/*
207 * TOS = most recently recorded branch
208 */
209static inline u64 intel_pmu_lbr_tos(void)
210{
211 u64 tos;
212
213 rdmsrl(x86_pmu.lbr_tos, tos);
214 return tos;
215}
216
217enum {
218 LBR_NONE,
219 LBR_VALID,
220};
221
222static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
223{
224 int i;
225 unsigned lbr_idx, mask;
226 u64 tos;
227
228 if (task_ctx->lbr_callstack_users == 0 ||
229 task_ctx->lbr_stack_state == LBR_NONE) {
230 intel_pmu_lbr_reset();
231 return;
232 }
233
234 mask = x86_pmu.lbr_nr - 1;
235 tos = intel_pmu_lbr_tos();
236 for (i = 0; i < x86_pmu.lbr_nr; i++) {
237 lbr_idx = (tos - i) & mask;
238 wrmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
239 wrmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700240 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
241 wrmsrl(MSR_LBR_INFO_0 + i, task_ctx->lbr_info[i]);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500242 }
243 task_ctx->lbr_stack_state = LBR_NONE;
244}
245
246static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
247{
248 int i;
249 unsigned lbr_idx, mask;
250 u64 tos;
251
252 if (task_ctx->lbr_callstack_users == 0) {
253 task_ctx->lbr_stack_state = LBR_NONE;
254 return;
255 }
256
257 mask = x86_pmu.lbr_nr - 1;
258 tos = intel_pmu_lbr_tos();
259 for (i = 0; i < x86_pmu.lbr_nr; i++) {
260 lbr_idx = (tos - i) & mask;
261 rdmsrl(x86_pmu.lbr_from + lbr_idx, task_ctx->lbr_from[i]);
262 rdmsrl(x86_pmu.lbr_to + lbr_idx, task_ctx->lbr_to[i]);
Andi Kleen50eab8f2015-05-10 12:22:43 -0700263 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
264 rdmsrl(MSR_LBR_INFO_0 + i, task_ctx->lbr_info[i]);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500265 }
266 task_ctx->lbr_stack_state = LBR_VALID;
267}
268
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500269void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
270{
271 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500272 struct x86_perf_task_context *task_ctx;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500273
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500274 /*
Yan, Zheng76cb2c62014-11-04 21:56:05 -0500275 * If LBR callstack feature is enabled and the stack was saved when
276 * the task was scheduled out, restore the stack. Otherwise flush
277 * the LBR stack.
278 */
279 task_ctx = ctx ? ctx->task_ctx_data : NULL;
280 if (task_ctx) {
281 if (sched_in) {
282 __intel_pmu_lbr_restore(task_ctx);
283 cpuc->lbr_context = ctx;
284 } else {
285 __intel_pmu_lbr_save(task_ctx);
286 }
287 return;
288 }
289
290 /*
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500291 * When sampling the branck stack in system-wide, it may be
292 * necessary to flush the stack on context switch. This happens
293 * when the branch stack does not tag its entries with the pid
294 * of the current task. Otherwise it becomes impossible to
295 * associate a branch entry with a task. This ambiguity is more
296 * likely to appear when the branch stack supports priv level
297 * filtering and the user sets it to monitor only at the user
298 * level (which could be a useful measurement in system-wide
299 * mode). In that case, the risk is high of having a branch
300 * stack with branch from multiple tasks.
301 */
302 if (sched_in) {
303 intel_pmu_lbr_reset();
304 cpuc->lbr_context = ctx;
305 }
306}
307
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500308static inline bool branch_user_callstack(unsigned br_sel)
309{
310 return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
311}
312
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300313void intel_pmu_lbr_enable(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100314{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500315 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500316 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100317
318 if (!x86_pmu.lbr_nr)
319 return;
320
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100321 /*
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100322 * Reset the LBR stack if we changed task context to
323 * avoid data leaks.
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100324 */
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100325 if (event->ctx->task && cpuc->lbr_context != event->ctx) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100326 intel_pmu_lbr_reset();
327 cpuc->lbr_context = event->ctx;
328 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100329 cpuc->br_sel = event->hw.branch_reg.reg;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100330
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500331 if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
332 event->ctx->task_ctx_data) {
333 task_ctx = event->ctx->task_ctx_data;
334 task_ctx->lbr_callstack_users++;
335 }
336
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100337 cpuc->lbr_users++;
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500338 perf_sched_cb_inc(event->ctx->pmu);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100339}
340
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300341void intel_pmu_lbr_disable(struct perf_event *event)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100342{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500343 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500344 struct x86_perf_task_context *task_ctx;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100345
346 if (!x86_pmu.lbr_nr)
347 return;
348
Yan, Zheng63f0c1d2014-11-04 21:56:04 -0500349 if (branch_user_callstack(cpuc->br_sel) && event->ctx &&
350 event->ctx->task_ctx_data) {
351 task_ctx = event->ctx->task_ctx_data;
352 task_ctx->lbr_callstack_users--;
353 }
354
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100355 cpuc->lbr_users--;
Peter Zijlstrab83a46e2010-03-08 13:51:12 +0100356 WARN_ON_ONCE(cpuc->lbr_users < 0);
Yan, Zheng2a0ad3b2014-11-04 21:55:59 -0500357 perf_sched_cb_dec(event->ctx->pmu);
Peter Zijlstra2df202b2010-03-06 13:48:54 +0100358
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100359 if (cpuc->enabled && !cpuc->lbr_users) {
Peter Zijlstra2df202b2010-03-06 13:48:54 +0100360 __intel_pmu_lbr_disable();
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100361 /* avoid stale pointer */
362 cpuc->lbr_context = NULL;
363 }
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100364}
365
Andi Kleen1a78d932015-03-20 10:11:23 -0700366void intel_pmu_lbr_enable_all(bool pmi)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100367{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500368 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100369
370 if (cpuc->lbr_users)
Andi Kleen1a78d932015-03-20 10:11:23 -0700371 __intel_pmu_lbr_enable(pmi);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100372}
373
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300374void intel_pmu_lbr_disable_all(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100375{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500376 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100377
378 if (cpuc->lbr_users)
379 __intel_pmu_lbr_disable();
380}
381
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100382static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
383{
384 unsigned long mask = x86_pmu.lbr_nr - 1;
385 u64 tos = intel_pmu_lbr_tos();
386 int i;
387
Peter Zijlstra63fb3f92010-03-09 11:51:02 +0100388 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100389 unsigned long lbr_idx = (tos - i) & mask;
390 union {
391 struct {
392 u32 from;
393 u32 to;
394 };
395 u64 lbr;
396 } msr_lastbranch;
397
398 rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
399
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100400 cpuc->lbr_entries[i].from = msr_lastbranch.from;
401 cpuc->lbr_entries[i].to = msr_lastbranch.to;
402 cpuc->lbr_entries[i].mispred = 0;
403 cpuc->lbr_entries[i].predicted = 0;
404 cpuc->lbr_entries[i].reserved = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100405 }
406 cpuc->lbr_stack.nr = i;
407}
408
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100409/*
410 * Due to lack of segmentation in Linux the effective address (offset)
411 * is the same as the linear address, allowing us to merge the LIP and EIP
412 * LBR formats.
413 */
414static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
415{
416 unsigned long mask = x86_pmu.lbr_nr - 1;
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100417 int lbr_format = x86_pmu.intel_cap.lbr_format;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100418 u64 tos = intel_pmu_lbr_tos();
419 int i;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700420 int out = 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100421
Peter Zijlstra63fb3f92010-03-09 11:51:02 +0100422 for (i = 0; i < x86_pmu.lbr_nr; i++) {
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100423 unsigned long lbr_idx = (tos - i) & mask;
Andi Kleen135c5612013-06-17 17:36:51 -0700424 u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
425 int skip = 0;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700426 u16 cycles = 0;
Andi Kleen135c5612013-06-17 17:36:51 -0700427 int lbr_flags = lbr_desc[lbr_format];
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100428
429 rdmsrl(x86_pmu.lbr_from + lbr_idx, from);
430 rdmsrl(x86_pmu.lbr_to + lbr_idx, to);
431
Andi Kleen50eab8f2015-05-10 12:22:43 -0700432 if (lbr_format == LBR_FORMAT_INFO) {
433 u64 info;
434
435 rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
436 mis = !!(info & LBR_INFO_MISPRED);
437 pred = !mis;
438 in_tx = !!(info & LBR_INFO_IN_TX);
439 abort = !!(info & LBR_INFO_ABORT);
440 cycles = (info & LBR_INFO_CYCLES);
441 }
Andi Kleen135c5612013-06-17 17:36:51 -0700442 if (lbr_flags & LBR_EIP_FLAGS) {
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100443 mis = !!(from & LBR_FROM_FLAG_MISPRED);
444 pred = !mis;
Andi Kleen135c5612013-06-17 17:36:51 -0700445 skip = 1;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100446 }
Andi Kleen135c5612013-06-17 17:36:51 -0700447 if (lbr_flags & LBR_TSX) {
448 in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
449 abort = !!(from & LBR_FROM_FLAG_ABORT);
450 skip = 3;
451 }
452 from = (u64)((((s64)from) << skip) >> skip);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100453
Andi Kleenb7af41a2013-09-20 07:40:44 -0700454 /*
455 * Some CPUs report duplicated abort records,
456 * with the second entry not having an abort bit set.
457 * Skip them here. This loop runs backwards,
458 * so we need to undo the previous record.
459 * If the abort just happened outside the window
460 * the extra entry cannot be removed.
461 */
462 if (abort && x86_pmu.lbr_double_abort && out > 0)
463 out--;
464
465 cpuc->lbr_entries[out].from = from;
466 cpuc->lbr_entries[out].to = to;
467 cpuc->lbr_entries[out].mispred = mis;
468 cpuc->lbr_entries[out].predicted = pred;
469 cpuc->lbr_entries[out].in_tx = in_tx;
470 cpuc->lbr_entries[out].abort = abort;
Andi Kleen50eab8f2015-05-10 12:22:43 -0700471 cpuc->lbr_entries[out].cycles = cycles;
Andi Kleenb7af41a2013-09-20 07:40:44 -0700472 cpuc->lbr_entries[out].reserved = 0;
473 out++;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100474 }
Andi Kleenb7af41a2013-09-20 07:40:44 -0700475 cpuc->lbr_stack.nr = out;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100476}
477
Kevin Winchesterde0428a2011-08-30 20:41:05 -0300478void intel_pmu_lbr_read(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100479{
Christoph Lameter89cbc762014-08-17 12:30:40 -0500480 struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100481
482 if (!cpuc->lbr_users)
483 return;
484
Peter Zijlstra8db909a2010-03-03 17:07:40 +0100485 if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100486 intel_pmu_lbr_read_32(cpuc);
487 else
488 intel_pmu_lbr_read_64(cpuc);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100489
490 intel_pmu_lbr_filter(cpuc);
491}
492
493/*
494 * SW filter is used:
495 * - in case there is no HW filter
496 * - in case the HW filter has errata or limitations
497 */
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500498static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100499{
500 u64 br_type = event->attr.branch_sample_type;
501 int mask = 0;
502
503 if (br_type & PERF_SAMPLE_BRANCH_USER)
504 mask |= X86_BR_USER;
505
Stephane Eranian2b923c82013-05-21 12:53:37 +0200506 if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100507 mask |= X86_BR_KERNEL;
508
509 /* we ignore BRANCH_HV here */
510
511 if (br_type & PERF_SAMPLE_BRANCH_ANY)
512 mask |= X86_BR_ANY;
513
514 if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
515 mask |= X86_BR_ANY_CALL;
516
517 if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
518 mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
519
520 if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
521 mask |= X86_BR_IND_CALL;
Andi Kleen135c5612013-06-17 17:36:51 -0700522
523 if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
524 mask |= X86_BR_ABORT;
525
526 if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
527 mask |= X86_BR_IN_TX;
528
529 if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
530 mask |= X86_BR_NO_TX;
531
Anshuman Khandual37548912014-05-22 12:50:09 +0530532 if (br_type & PERF_SAMPLE_BRANCH_COND)
533 mask |= X86_BR_JCC;
534
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500535 if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
536 if (!x86_pmu_has_lbr_callstack())
537 return -EOPNOTSUPP;
538 if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
539 return -EINVAL;
540 mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
541 X86_BR_CALL_STACK;
542 }
543
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200544 if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
545 mask |= X86_BR_IND_JMP;
546
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100547 /*
548 * stash actual user request into reg, it may
549 * be used by fixup code for some CPU
550 */
551 event->hw.branch_reg.reg = mask;
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500552 return 0;
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100553}
554
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100555/*
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100556 * setup the HW LBR filter
557 * Used only when available, may not be enough to disambiguate
558 * all branches, may need the help of the SW filter
559 */
560static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
561{
562 struct hw_perf_event_extra *reg;
563 u64 br_type = event->attr.branch_sample_type;
Yan, Zheng27ac9052014-11-04 21:55:57 -0500564 u64 mask = 0, v;
565 int i;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100566
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100567 for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
Yan, Zheng27ac9052014-11-04 21:55:57 -0500568 if (!(br_type & (1ULL << i)))
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100569 continue;
570
Yan, Zheng27ac9052014-11-04 21:55:57 -0500571 v = x86_pmu.lbr_sel_map[i];
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100572 if (v == LBR_NOT_SUPP)
573 return -EOPNOTSUPP;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100574
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100575 if (v != LBR_IGN)
576 mask |= v;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100577 }
578 reg = &event->hw.branch_reg;
579 reg->idx = EXTRA_REG_LBR;
580
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500581 /*
582 * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
583 * in suppress mode. So LBR_SELECT should be set to
584 * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
585 */
586 reg->config = mask ^ x86_pmu.lbr_sel_mask;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100587
588 return 0;
589}
590
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100591int intel_pmu_setup_lbr_filter(struct perf_event *event)
592{
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100593 int ret = 0;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100594
595 /*
596 * no LBR on this PMU
597 */
598 if (!x86_pmu.lbr_nr)
599 return -EOPNOTSUPP;
600
601 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100602 * setup SW LBR filter
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100603 */
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500604 ret = intel_pmu_setup_sw_lbr_filter(event);
605 if (ret)
606 return ret;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100607
608 /*
609 * setup HW LBR filter, if any
610 */
611 if (x86_pmu.lbr_sel_map)
612 ret = intel_pmu_setup_hw_lbr_filter(event);
613
614 return ret;
615}
616
617/*
618 * return the type of control flow change at address "from"
619 * intruction is not necessarily a branch (in case of interrupt).
620 *
621 * The branch type returned also includes the priv level of the
622 * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
623 *
624 * If a branch type is unknown OR the instruction cannot be
625 * decoded (e.g., text page not present), then X86_BR_NONE is
626 * returned.
627 */
Andi Kleen135c5612013-06-17 17:36:51 -0700628static int branch_type(unsigned long from, unsigned long to, int abort)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100629{
630 struct insn insn;
631 void *addr;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800632 int bytes_read, bytes_left;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100633 int ret = X86_BR_NONE;
634 int ext, to_plm, from_plm;
635 u8 buf[MAX_INSN_SIZE];
636 int is64 = 0;
637
638 to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
639 from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
640
641 /*
642 * maybe zero if lbr did not fill up after a reset by the time
643 * we get a PMU interrupt
644 */
645 if (from == 0 || to == 0)
646 return X86_BR_NONE;
647
Andi Kleen135c5612013-06-17 17:36:51 -0700648 if (abort)
649 return X86_BR_ABORT | to_plm;
650
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100651 if (from_plm == X86_BR_USER) {
652 /*
653 * can happen if measuring at the user level only
654 * and we interrupt in a kernel thread, e.g., idle.
655 */
656 if (!current->mm)
657 return X86_BR_NONE;
658
659 /* may fail if text not present */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800660 bytes_left = copy_from_user_nmi(buf, (void __user *)from,
661 MAX_INSN_SIZE);
662 bytes_read = MAX_INSN_SIZE - bytes_left;
663 if (!bytes_read)
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100664 return X86_BR_NONE;
665
666 addr = buf;
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200667 } else {
668 /*
669 * The LBR logs any address in the IP, even if the IP just
670 * faulted. This means userspace can control the from address.
671 * Ensure we don't blindy read any address by validating it is
672 * a known text address.
673 */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800674 if (kernel_text_address(from)) {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200675 addr = (void *)from;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800676 /*
677 * Assume we can get the maximum possible size
678 * when grabbing kernel data. This is not
679 * _strictly_ true since we could possibly be
680 * executing up next to a memory hole, but
681 * it is very unlikely to be a problem.
682 */
683 bytes_read = MAX_INSN_SIZE;
684 } else {
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200685 return X86_BR_NONE;
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800686 }
Peter Zijlstra6e15eb32013-05-03 14:11:24 +0200687 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100688
689 /*
690 * decoder needs to know the ABI especially
691 * on 64-bit systems running 32-bit apps
692 */
693#ifdef CONFIG_X86_64
694 is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
695#endif
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800696 insn_init(&insn, addr, bytes_read, is64);
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100697 insn_get_opcode(&insn);
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800698 if (!insn.opcode.got)
699 return X86_BR_ABORT;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100700
701 switch (insn.opcode.bytes[0]) {
702 case 0xf:
703 switch (insn.opcode.bytes[1]) {
704 case 0x05: /* syscall */
705 case 0x34: /* sysenter */
706 ret = X86_BR_SYSCALL;
707 break;
708 case 0x07: /* sysret */
709 case 0x35: /* sysexit */
710 ret = X86_BR_SYSRET;
711 break;
712 case 0x80 ... 0x8f: /* conditional */
713 ret = X86_BR_JCC;
714 break;
715 default:
716 ret = X86_BR_NONE;
717 }
718 break;
719 case 0x70 ... 0x7f: /* conditional */
720 ret = X86_BR_JCC;
721 break;
722 case 0xc2: /* near ret */
723 case 0xc3: /* near ret */
724 case 0xca: /* far ret */
725 case 0xcb: /* far ret */
726 ret = X86_BR_RET;
727 break;
728 case 0xcf: /* iret */
729 ret = X86_BR_IRET;
730 break;
731 case 0xcc ... 0xce: /* int */
732 ret = X86_BR_INT;
733 break;
734 case 0xe8: /* call near rel */
Yan, Zhengaa54ae92014-11-04 21:56:11 -0500735 insn_get_immediate(&insn);
736 if (insn.immediate1.value == 0) {
737 /* zero length call */
738 ret = X86_BR_ZERO_CALL;
739 break;
740 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100741 case 0x9a: /* call far absolute */
742 ret = X86_BR_CALL;
743 break;
744 case 0xe0 ... 0xe3: /* loop jmp */
745 ret = X86_BR_JCC;
746 break;
747 case 0xe9 ... 0xeb: /* jmp */
748 ret = X86_BR_JMP;
749 break;
750 case 0xff: /* call near absolute, call far absolute ind */
751 insn_get_modrm(&insn);
752 ext = (insn.modrm.bytes[0] >> 3) & 0x7;
753 switch (ext) {
754 case 2: /* near ind call */
755 case 3: /* far ind call */
756 ret = X86_BR_IND_CALL;
757 break;
758 case 4:
759 case 5:
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200760 ret = X86_BR_IND_JMP;
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100761 break;
762 }
763 break;
764 default:
765 ret = X86_BR_NONE;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100766 }
767 /*
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100768 * interrupts, traps, faults (and thus ring transition) may
769 * occur on any instructions. Thus, to classify them correctly,
770 * we need to first look at the from and to priv levels. If they
771 * are different and to is in the kernel, then it indicates
772 * a ring transition. If the from instruction is not a ring
773 * transition instr (syscall, systenter, int), then it means
774 * it was a irq, trap or fault.
775 *
776 * we have no way of detecting kernel to kernel faults.
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100777 */
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100778 if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
779 && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
780 ret = X86_BR_IRQ;
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100781
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100782 /*
783 * branch priv level determined by target as
784 * is done by HW when LBR_SELECT is implemented
785 */
786 if (ret != X86_BR_NONE)
787 ret |= to_plm;
788
789 return ret;
790}
791
792/*
793 * implement actual branch filter based on user demand.
794 * Hardware may not exactly satisfy that request, thus
795 * we need to inspect opcodes. Mismatched branches are
796 * discarded. Therefore, the number of branches returned
797 * in PERF_SAMPLE_BRANCH_STACK sample may vary.
798 */
799static void
800intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
801{
802 u64 from, to;
803 int br_sel = cpuc->br_sel;
804 int i, j, type;
805 bool compress = false;
806
807 /* if sampling all branches, then nothing to filter */
808 if ((br_sel & X86_BR_ALL) == X86_BR_ALL)
809 return;
810
811 for (i = 0; i < cpuc->lbr_stack.nr; i++) {
812
813 from = cpuc->lbr_entries[i].from;
814 to = cpuc->lbr_entries[i].to;
815
Andi Kleen135c5612013-06-17 17:36:51 -0700816 type = branch_type(from, to, cpuc->lbr_entries[i].abort);
817 if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
818 if (cpuc->lbr_entries[i].in_tx)
819 type |= X86_BR_IN_TX;
820 else
821 type |= X86_BR_NO_TX;
822 }
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100823
824 /* if type does not correspond, then discard */
825 if (type == X86_BR_NONE || (br_sel & type) != type) {
826 cpuc->lbr_entries[i].from = 0;
827 compress = true;
828 }
829 }
830
831 if (!compress)
832 return;
833
834 /* remove all entries with from=0 */
835 for (i = 0; i < cpuc->lbr_stack.nr; ) {
836 if (!cpuc->lbr_entries[i].from) {
837 j = i;
838 while (++j < cpuc->lbr_stack.nr)
839 cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
840 cpuc->lbr_stack.nr--;
841 if (!cpuc->lbr_entries[i].from)
842 continue;
843 }
844 i++;
845 }
Stephane Eranian60ce0fb2012-02-09 23:20:57 +0100846}
847
848/*
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100849 * Map interface branch filters onto LBR filters
850 */
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100851static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -0500852 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
853 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
854 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
855 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
856 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
857 | LBR_IND_JMP | LBR_FAR,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100858 /*
859 * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
860 */
Yan, Zheng27ac9052014-11-04 21:55:57 -0500861 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100862 LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
863 /*
864 * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
865 */
Yan, Zheng27ac9052014-11-04 21:55:57 -0500866 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
867 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200868 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100869};
870
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100871static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zheng27ac9052014-11-04 21:55:57 -0500872 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
873 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
874 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
875 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
876 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
877 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
878 | LBR_FAR,
879 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
880 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200881 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100882};
883
Peter Zijlstra2c44b192014-11-05 10:36:45 +0100884static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500885 [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
886 [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
887 [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
888 [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
889 [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
890 [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
891 | LBR_FAR,
892 [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
893 [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
894 [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
895 | LBR_RETURN | LBR_CALL_STACK,
Stephane Eranian7b74cfb2015-05-14 23:09:59 +0200896 [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500897};
898
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100899/* core */
Mathias Krause066ce642014-08-26 18:49:45 +0200900void __init intel_pmu_lbr_init_core(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100901{
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100902 x86_pmu.lbr_nr = 4;
Stephane Eranian225ce532012-02-09 23:20:52 +0100903 x86_pmu.lbr_tos = MSR_LBR_TOS;
904 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
905 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100906
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100907 /*
908 * SW branch filter usage:
909 * - compensate for lack of HW filter
910 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100911 pr_cont("4-deep LBR, ");
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100912}
913
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100914/* nehalem/westmere */
Mathias Krause066ce642014-08-26 18:49:45 +0200915void __init intel_pmu_lbr_init_nhm(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100916{
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100917 x86_pmu.lbr_nr = 16;
Stephane Eranian225ce532012-02-09 23:20:52 +0100918 x86_pmu.lbr_tos = MSR_LBR_TOS;
919 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
920 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100921
922 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
923 x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
924
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100925 /*
926 * SW branch filter usage:
927 * - workaround LBR_SEL errata (see above)
928 * - support syscall, sysret capture.
929 * That requires LBR_FAR but that means far
930 * jmp need to be filtered out
931 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100932 pr_cont("16-deep LBR, ");
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100933}
934
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100935/* sandy bridge */
Mathias Krause066ce642014-08-26 18:49:45 +0200936void __init intel_pmu_lbr_init_snb(void)
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100937{
938 x86_pmu.lbr_nr = 16;
939 x86_pmu.lbr_tos = MSR_LBR_TOS;
940 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
941 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
942
943 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
944 x86_pmu.lbr_sel_map = snb_lbr_sel_map;
945
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100946 /*
947 * SW branch filter usage:
948 * - support syscall, sysret capture.
949 * That requires LBR_FAR but that means far
950 * jmp need to be filtered out
951 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100952 pr_cont("16-deep LBR, ");
953}
954
Yan, Zhenge9d7f7c2014-11-04 21:56:00 -0500955/* haswell */
956void intel_pmu_lbr_init_hsw(void)
957{
958 x86_pmu.lbr_nr = 16;
959 x86_pmu.lbr_tos = MSR_LBR_TOS;
960 x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
961 x86_pmu.lbr_to = MSR_LBR_NHM_TO;
962
963 x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
964 x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
965
966 pr_cont("16-deep LBR, ");
967}
968
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100969/* atom */
Mathias Krause066ce642014-08-26 18:49:45 +0200970void __init intel_pmu_lbr_init_atom(void)
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100971{
Stephane Eranian88c9a652012-02-09 23:20:56 +0100972 /*
973 * only models starting at stepping 10 seems
974 * to have an operational LBR which can freeze
975 * on PMU interrupt
976 */
Stephane Eranian3ec18cd2012-08-20 11:24:21 +0200977 if (boot_cpu_data.x86_model == 28
978 && boot_cpu_data.x86_mask < 10) {
Stephane Eranian88c9a652012-02-09 23:20:56 +0100979 pr_cont("LBR disabled due to erratum");
980 return;
981 }
982
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100983 x86_pmu.lbr_nr = 8;
Stephane Eranian225ce532012-02-09 23:20:52 +0100984 x86_pmu.lbr_tos = MSR_LBR_TOS;
985 x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
986 x86_pmu.lbr_to = MSR_LBR_CORE_TO;
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100987
Stephane Eranian3e702ff2012-02-09 23:20:58 +0100988 /*
989 * SW branch filter usage:
990 * - compensate for lack of HW filter
991 */
Stephane Eranianc5cc2cd2012-02-09 23:20:55 +0100992 pr_cont("8-deep LBR, ");
Peter Zijlstracaff2be2010-03-03 12:02:30 +0100993}