Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 2 | #include <linux/perf_event.h> |
| 3 | #include <linux/types.h> |
| 4 | |
| 5 | #include <asm/perf_event.h> |
| 6 | #include <asm/msr.h> |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 7 | #include <asm/insn.h> |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 8 | |
Borislav Petkov | 27f6d22 | 2016-02-10 10:55:23 +0100 | [diff] [blame] | 9 | #include "../perf_event.h" |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 10 | |
Colin Ian King | e91c8d9 | 2017-06-29 10:14:06 +0100 | [diff] [blame] | 11 | static const enum { |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 12 | 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 Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | /* |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 20 | * 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, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 34 | #define LBR_CALL_STACK_BIT 9 /* enable call stack */ |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 35 | |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 36 | /* |
| 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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 43 | #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, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 52 | #define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT) |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 53 | #define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT) |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 54 | |
| 55 | #define LBR_PLM (LBR_KERNEL | LBR_USER) |
| 56 | |
Kan Liang | cf3beb7 | 2016-04-21 02:30:10 -0700 | [diff] [blame] | 57 | #define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */ |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 58 | #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-Cisneros | 3812bba | 2016-06-21 11:31:12 -0700 | [diff] [blame] | 70 | #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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 73 | |
David Carrillo-Cisneros | 19fc9dd | 2016-06-21 11:31:11 -0700 | [diff] [blame] | 74 | #define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59)) |
| 75 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 76 | /* |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 77 | * x86control flow change classification |
| 78 | * x86control flow changes include branches, interrupts, traps, faults |
| 79 | */ |
| 80 | enum { |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 81 | X86_BR_NONE = 0, /* unknown */ |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 82 | |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 83 | X86_BR_USER = 1 << 0, /* branch target is user */ |
| 84 | X86_BR_KERNEL = 1 << 1, /* branch target is kernel */ |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 85 | |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 86 | 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, Zheng | aa54ae9 | 2014-11-04 21:56:11 -0500 | [diff] [blame] | 99 | X86_BR_ZERO_CALL = 1 << 15,/* zero length call */ |
| 100 | X86_BR_CALL_STACK = 1 << 16,/* call stack */ |
Stephane Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 101 | X86_BR_IND_JMP = 1 << 17,/* indirect jump */ |
Jin Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 102 | |
| 103 | X86_BR_TYPE_SAVE = 1 << 18,/* indicate to save branch type */ |
| 104 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL) |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 108 | #define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 109 | |
| 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 120 | X86_BR_ABORT |\ |
Yan, Zheng | aa54ae9 | 2014-11-04 21:56:11 -0500 | [diff] [blame] | 121 | X86_BR_IND_CALL |\ |
Stephane Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 122 | X86_BR_IND_JMP |\ |
Yan, Zheng | aa54ae9 | 2014-11-04 21:56:11 -0500 | [diff] [blame] | 123 | X86_BR_ZERO_CALL) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 124 | |
| 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, Zheng | aa54ae9 | 2014-11-04 21:56:11 -0500 | [diff] [blame] | 130 | X86_BR_ZERO_CALL |\ |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 131 | X86_BR_SYSCALL |\ |
| 132 | X86_BR_IRQ |\ |
| 133 | X86_BR_INT) |
| 134 | |
| 135 | static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc); |
| 136 | |
| 137 | /* |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 138 | * 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 Kleen | 1a78d93 | 2015-03-20 10:11:23 -0700 | [diff] [blame] | 142 | static void __intel_pmu_lbr_enable(bool pmi) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 143 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 144 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Andi Kleen | cd1f11d | 2015-03-20 10:11:24 -0700 | [diff] [blame] | 145 | u64 debugctl, lbr_select = 0, orig_debugctl; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 146 | |
Andi Kleen | 1a78d93 | 2015-03-20 10:11:23 -0700 | [diff] [blame] | 147 | /* |
Andi Kleen | 425507f | 2015-05-10 12:22:46 -0700 | [diff] [blame] | 148 | * 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 Kleen | 1a78d93 | 2015-03-20 10:11:23 -0700 | [diff] [blame] | 155 | * No need to reprogram LBR_SELECT in a PMI, as it |
| 156 | * did not change. |
| 157 | */ |
Kan Liang | 96f3eda | 2015-09-14 10:14:07 -0400 | [diff] [blame] | 158 | if (cpuc->lbr_sel) |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 159 | lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask; |
Stephane Eranian | 6fc2e83 | 2015-12-03 23:33:17 +0100 | [diff] [blame] | 160 | if (!pmi && cpuc->lbr_sel) |
Yan, Zheng | 2c70d00 | 2014-11-04 21:56:10 -0500 | [diff] [blame] | 161 | wrmsrl(MSR_LBR_SELECT, lbr_select); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 162 | |
| 163 | rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); |
Andi Kleen | cd1f11d | 2015-03-20 10:11:24 -0700 | [diff] [blame] | 164 | orig_debugctl = debugctl; |
Yan, Zheng | 2c70d00 | 2014-11-04 21:56:10 -0500 | [diff] [blame] | 165 | 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 Kleen | cd1f11d | 2015-03-20 10:11:24 -0700 | [diff] [blame] | 173 | if (orig_debugctl != debugctl) |
| 174 | wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void __intel_pmu_lbr_disable(void) |
| 178 | { |
| 179 | u64 debugctl; |
| 180 | |
| 181 | rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); |
Peter Zijlstra | 7c5ecaf | 2010-03-25 14:51:49 +0100 | [diff] [blame] | 182 | debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 183 | wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); |
| 184 | } |
| 185 | |
Kan Liang | 9f354a7 | 2020-07-03 05:49:08 -0700 | [diff] [blame] | 186 | void intel_pmu_lbr_reset_32(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 187 | { |
| 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 Liang | 9f354a7 | 2020-07-03 05:49:08 -0700 | [diff] [blame] | 194 | void intel_pmu_lbr_reset_64(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 195 | { |
| 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 Kleen | 50eab8f | 2015-05-10 12:22:43 -0700 | [diff] [blame] | 201 | if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO) |
| 202 | wrmsrl(MSR_LBR_INFO_0 + i, 0); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 206 | void intel_pmu_lbr_reset(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 207 | { |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 208 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
| 209 | |
Peter Zijlstra | 74846d3 | 2010-03-05 13:49:35 +0100 | [diff] [blame] | 210 | if (!x86_pmu.lbr_nr) |
| 211 | return; |
| 212 | |
Kan Liang | 9f354a7 | 2020-07-03 05:49:08 -0700 | [diff] [blame] | 213 | x86_pmu.lbr_reset(); |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 214 | |
| 215 | cpuc->last_task_ctx = NULL; |
| 216 | cpuc->last_log_id = 0; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 219 | /* |
| 220 | * TOS = most recently recorded branch |
| 221 | */ |
| 222 | static inline u64 intel_pmu_lbr_tos(void) |
| 223 | { |
| 224 | u64 tos; |
| 225 | |
| 226 | rdmsrl(x86_pmu.lbr_tos, tos); |
| 227 | return tos; |
| 228 | } |
| 229 | |
| 230 | enum { |
| 231 | LBR_NONE, |
| 232 | LBR_VALID, |
| 233 | }; |
| 234 | |
David Carrillo-Cisneros | 19fc9dd | 2016-06-21 11:31:11 -0700 | [diff] [blame] | 235 | /* |
| 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 | */ |
| 253 | static 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ētnieks | d9f3b45 | 2019-08-08 13:44:02 -0400 | [diff] [blame] | 262 | static DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key); |
David Carrillo-Cisneros | 19fc9dd | 2016-06-21 11:31:11 -0700 | [diff] [blame] | 263 | |
| 264 | /* If quirk is enabled, ensure sign extension is 63 bits: */ |
| 265 | inline 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-Cisneros | 71adae9 | 2016-06-21 11:31:13 -0700 | [diff] [blame] | 282 | /* |
| 283 | * If quirk is needed, ensure sign extension is 61 bits: |
| 284 | */ |
Colin Ian King | e91c8d9 | 2017-06-29 10:14:06 +0100 | [diff] [blame] | 285 | static u64 lbr_from_signext_quirk_rd(u64 val) |
David Carrillo-Cisneros | 71adae9 | 2016-06-21 11:31:13 -0700 | [diff] [blame] | 286 | { |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 287 | if (static_branch_unlikely(&lbr_from_quirk_key)) { |
David Carrillo-Cisneros | 71adae9 | 2016-06-21 11:31:13 -0700 | [diff] [blame] | 288 | /* |
| 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 Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 293 | } |
| 294 | return val; |
| 295 | } |
| 296 | |
| 297 | static 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 | |
| 303 | static inline void wrlbr_to(unsigned int idx, u64 val) |
| 304 | { |
| 305 | wrmsrl(x86_pmu.lbr_to + idx, val); |
| 306 | } |
| 307 | |
| 308 | static 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 | |
| 317 | static inline u64 rdlbr_to(unsigned int idx) |
| 318 | { |
| 319 | u64 val; |
| 320 | |
Peter Zijlstra | aefbc4d | 2016-06-30 11:49:08 +0200 | [diff] [blame] | 321 | rdmsrl(x86_pmu.lbr_to + idx, val); |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 322 | |
David Carrillo-Cisneros | 71adae9 | 2016-06-21 11:31:13 -0700 | [diff] [blame] | 323 | return val; |
| 324 | } |
| 325 | |
Kan Liang | 799571b | 2020-07-03 05:49:10 -0700 | [diff] [blame^] | 326 | void 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, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 358 | static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx) |
| 359 | { |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 360 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 361 | 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 Kleen | b28ae95 | 2015-10-20 11:46:33 -0700 | [diff] [blame] | 369 | tos = task_ctx->tos; |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 370 | /* |
| 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 Liang | 799571b | 2020-07-03 05:49:10 -0700 | [diff] [blame^] | 382 | x86_pmu.lbr_restore(task_ctx); |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 383 | |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 384 | task_ctx->lbr_stack_state = LBR_NONE; |
| 385 | } |
| 386 | |
Kan Liang | 799571b | 2020-07-03 05:49:10 -0700 | [diff] [blame^] | 387 | void intel_pmu_lbr_save(void *ctx) |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 388 | { |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 389 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Kan Liang | 799571b | 2020-07-03 05:49:10 -0700 | [diff] [blame^] | 390 | struct x86_perf_task_context *task_ctx = ctx; |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 391 | unsigned lbr_idx, mask; |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 392 | u64 tos, from; |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 393 | int i; |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 394 | |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 395 | mask = x86_pmu.lbr_nr - 1; |
| 396 | tos = intel_pmu_lbr_tos(); |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 397 | for (i = 0; i < x86_pmu.lbr_nr; i++) { |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 398 | lbr_idx = (tos - i) & mask; |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 399 | from = rdlbr_from(lbr_idx); |
| 400 | if (!from) |
| 401 | break; |
| 402 | task_ctx->lbr_from[i] = from; |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 403 | task_ctx->lbr_to[i] = rdlbr_to(lbr_idx); |
Andi Kleen | 50eab8f | 2015-05-10 12:22:43 -0700 | [diff] [blame] | 404 | if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO) |
Andi Kleen | e057336 | 2015-05-27 21:13:17 -0700 | [diff] [blame] | 405 | rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]); |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 406 | } |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 407 | task_ctx->valid_lbrs = i; |
Andi Kleen | b28ae95 | 2015-10-20 11:46:33 -0700 | [diff] [blame] | 408 | task_ctx->tos = tos; |
Kan Liang | 799571b | 2020-07-03 05:49:10 -0700 | [diff] [blame^] | 409 | |
| 410 | if (cpuc->lbr_select) |
| 411 | rdmsrl(MSR_LBR_SELECT, task_ctx->lbr_sel); |
| 412 | } |
| 413 | |
| 414 | static 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, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 425 | task_ctx->lbr_stack_state = LBR_VALID; |
Kan Liang | 8b077e4a | 2018-06-05 08:38:46 -0700 | [diff] [blame] | 426 | |
| 427 | cpuc->last_task_ctx = task_ctx; |
| 428 | cpuc->last_log_id = ++task_ctx->log_id; |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Alexey Budankov | 421ca86 | 2019-10-23 10:12:54 +0300 | [diff] [blame] | 431 | void 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, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 454 | void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in) |
| 455 | { |
Jiri Olsa | df6c3db | 2017-07-19 09:52:47 +0200 | [diff] [blame] | 456 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 457 | struct x86_perf_task_context *task_ctx; |
Yan, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 458 | |
Jiri Olsa | df6c3db | 2017-07-19 09:52:47 +0200 | [diff] [blame] | 459 | if (!cpuc->lbr_users) |
| 460 | return; |
| 461 | |
Yan, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 462 | /* |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 463 | * 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 Zijlstra | 3e2c1a6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 469 | if (sched_in) |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 470 | __intel_pmu_lbr_restore(task_ctx); |
Peter Zijlstra | 3e2c1a6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 471 | else |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 472 | __intel_pmu_lbr_save(task_ctx); |
Yan, Zheng | 76cb2c6 | 2014-11-04 21:56:05 -0500 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
| 476 | /* |
Peter Zijlstra | 3e2c1a6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 477 | * 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, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 483 | intel_pmu_lbr_reset(); |
Yan, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Yan, Zheng | 63f0c1d | 2014-11-04 21:56:04 -0500 | [diff] [blame] | 486 | static 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 Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 491 | void intel_pmu_lbr_add(struct perf_event *event) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 492 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 493 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Yan, Zheng | 63f0c1d | 2014-11-04 21:56:04 -0500 | [diff] [blame] | 494 | struct x86_perf_task_context *task_ctx; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 495 | |
| 496 | if (!x86_pmu.lbr_nr) |
| 497 | return; |
| 498 | |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 499 | if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT) |
| 500 | cpuc->lbr_select = 1; |
| 501 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 502 | cpuc->br_sel = event->hw.branch_reg.reg; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 503 | |
Peter Zijlstra | a5dcff6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 504 | if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data) { |
Yan, Zheng | 63f0c1d | 2014-11-04 21:56:04 -0500 | [diff] [blame] | 505 | task_ctx = event->ctx->task_ctx_data; |
| 506 | task_ctx->lbr_callstack_users++; |
| 507 | } |
| 508 | |
Peter Zijlstra | 3e2c1a6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 509 | /* |
| 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 Kleen | d3617b98 | 2019-04-02 12:45:03 -0700 | [diff] [blame] | 528 | if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0) |
| 529 | cpuc->lbr_pebs_users++; |
Yan, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 530 | perf_sched_cb_inc(event->ctx->pmu); |
Peter Zijlstra | 3e2c1a6 | 2016-07-07 19:37:52 +0200 | [diff] [blame] | 531 | if (!cpuc->lbr_users++ && !event->total_time_running) |
| 532 | intel_pmu_lbr_reset(); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Peter Zijlstra | 68f7082 | 2016-07-06 18:02:43 +0200 | [diff] [blame] | 535 | void intel_pmu_lbr_del(struct perf_event *event) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 536 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 537 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Yan, Zheng | 63f0c1d | 2014-11-04 21:56:04 -0500 | [diff] [blame] | 538 | struct x86_perf_task_context *task_ctx; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 539 | |
| 540 | if (!x86_pmu.lbr_nr) |
| 541 | return; |
| 542 | |
Dan Carpenter | 5c38181 | 2016-10-14 10:29:08 +0300 | [diff] [blame] | 543 | if (branch_user_callstack(cpuc->br_sel) && |
| 544 | event->ctx->task_ctx_data) { |
Yan, Zheng | 63f0c1d | 2014-11-04 21:56:04 -0500 | [diff] [blame] | 545 | task_ctx = event->ctx->task_ctx_data; |
| 546 | task_ctx->lbr_callstack_users--; |
| 547 | } |
| 548 | |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 549 | if (event->hw.flags & PERF_X86_EVENT_LBR_SELECT) |
| 550 | cpuc->lbr_select = 0; |
| 551 | |
Andi Kleen | d3617b98 | 2019-04-02 12:45:03 -0700 | [diff] [blame] | 552 | if (x86_pmu.intel_cap.pebs_baseline && event->attr.precise_ip > 0) |
| 553 | cpuc->lbr_pebs_users--; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 554 | cpuc->lbr_users--; |
Peter Zijlstra | b83a46e | 2010-03-08 13:51:12 +0100 | [diff] [blame] | 555 | WARN_ON_ONCE(cpuc->lbr_users < 0); |
Andi Kleen | d3617b98 | 2019-04-02 12:45:03 -0700 | [diff] [blame] | 556 | WARN_ON_ONCE(cpuc->lbr_pebs_users < 0); |
Yan, Zheng | 2a0ad3b | 2014-11-04 21:55:59 -0500 | [diff] [blame] | 557 | perf_sched_cb_dec(event->ctx->pmu); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 560 | static 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 Kleen | 1a78d93 | 2015-03-20 10:11:23 -0700 | [diff] [blame] | 568 | void intel_pmu_lbr_enable_all(bool pmi) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 569 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 570 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 571 | |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 572 | if (cpuc->lbr_users && !vlbr_exclude_host()) |
Andi Kleen | 1a78d93 | 2015-03-20 10:11:23 -0700 | [diff] [blame] | 573 | __intel_pmu_lbr_enable(pmi); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 576 | void intel_pmu_lbr_disable_all(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 577 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 578 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 579 | |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 580 | if (cpuc->lbr_users && !vlbr_exclude_host()) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 581 | __intel_pmu_lbr_disable(); |
| 582 | } |
| 583 | |
Kan Liang | c301b1d | 2020-07-03 05:49:09 -0700 | [diff] [blame] | 584 | void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 585 | { |
| 586 | unsigned long mask = x86_pmu.lbr_nr - 1; |
| 587 | u64 tos = intel_pmu_lbr_tos(); |
| 588 | int i; |
| 589 | |
Peter Zijlstra | 63fb3f9 | 2010-03-09 11:51:02 +0100 | [diff] [blame] | 590 | for (i = 0; i < x86_pmu.lbr_nr; i++) { |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 591 | 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 Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 602 | 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 Zijlstra | f2200ac | 2017-04-11 10:10:28 +0200 | [diff] [blame] | 606 | cpuc->lbr_entries[i].in_tx = 0; |
| 607 | cpuc->lbr_entries[i].abort = 0; |
| 608 | cpuc->lbr_entries[i].cycles = 0; |
Jin Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 609 | cpuc->lbr_entries[i].type = 0; |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 610 | cpuc->lbr_entries[i].reserved = 0; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 611 | } |
| 612 | cpuc->lbr_stack.nr = i; |
Kan Liang | db278b9 | 2020-01-27 08:53:55 -0800 | [diff] [blame] | 613 | cpuc->lbr_stack.hw_idx = tos; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 616 | /* |
| 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 Liang | c301b1d | 2020-07-03 05:49:09 -0700 | [diff] [blame] | 621 | void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 622 | { |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 623 | bool need_info = false, call_stack = false; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 624 | unsigned long mask = x86_pmu.lbr_nr - 1; |
Peter Zijlstra | 8db909a | 2010-03-03 17:07:40 +0100 | [diff] [blame] | 625 | int lbr_format = x86_pmu.intel_cap.lbr_format; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 626 | u64 tos = intel_pmu_lbr_tos(); |
| 627 | int i; |
Andi Kleen | b7af41a | 2013-09-20 07:40:44 -0700 | [diff] [blame] | 628 | int out = 0; |
Andi Kleen | 90405aa | 2015-05-27 21:13:18 -0700 | [diff] [blame] | 629 | int num = x86_pmu.lbr_nr; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 630 | |
Stephane Eranian | 6fc2e83 | 2015-12-03 23:33:17 +0100 | [diff] [blame] | 631 | if (cpuc->lbr_sel) { |
| 632 | need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO); |
| 633 | if (cpuc->lbr_sel->config & LBR_CALL_STACK) |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 634 | call_stack = true; |
Stephane Eranian | 6fc2e83 | 2015-12-03 23:33:17 +0100 | [diff] [blame] | 635 | } |
Andi Kleen | 90405aa | 2015-05-27 21:13:18 -0700 | [diff] [blame] | 636 | |
| 637 | for (i = 0; i < num; i++) { |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 638 | unsigned long lbr_idx = (tos - i) & mask; |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 639 | u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0; |
| 640 | int skip = 0; |
Andi Kleen | 50eab8f | 2015-05-10 12:22:43 -0700 | [diff] [blame] | 641 | u16 cycles = 0; |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 642 | int lbr_flags = lbr_desc[lbr_format]; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 643 | |
Peter Zijlstra | d4cf194 | 2016-06-23 10:44:49 +0200 | [diff] [blame] | 644 | from = rdlbr_from(lbr_idx); |
| 645 | to = rdlbr_to(lbr_idx); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 646 | |
Kan Liang | 0592e57 | 2018-06-05 08:38:45 -0700 | [diff] [blame] | 647 | /* |
| 648 | * Read LBR call stack entries |
| 649 | * until invalid entry (0s) is detected. |
| 650 | */ |
| 651 | if (call_stack && !from) |
| 652 | break; |
| 653 | |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 654 | if (lbr_format == LBR_FORMAT_INFO && need_info) { |
Andi Kleen | 50eab8f | 2015-05-10 12:22:43 -0700 | [diff] [blame] | 655 | 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 Liang | 8b92c3a | 2016-04-15 00:42:47 -0700 | [diff] [blame] | 664 | |
| 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 674 | if (lbr_flags & LBR_EIP_FLAGS) { |
Stephane Eranian | bce38cd | 2012-02-09 23:20:51 +0100 | [diff] [blame] | 675 | mis = !!(from & LBR_FROM_FLAG_MISPRED); |
| 676 | pred = !mis; |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 677 | skip = 1; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 678 | } |
Andi Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 679 | 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 Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 685 | |
Andi Kleen | b7af41a | 2013-09-20 07:40:44 -0700 | [diff] [blame] | 686 | /* |
| 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 Kleen | 50eab8f | 2015-05-10 12:22:43 -0700 | [diff] [blame] | 703 | cpuc->lbr_entries[out].cycles = cycles; |
Jin Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 704 | cpuc->lbr_entries[out].type = 0; |
Andi Kleen | b7af41a | 2013-09-20 07:40:44 -0700 | [diff] [blame] | 705 | cpuc->lbr_entries[out].reserved = 0; |
| 706 | out++; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 707 | } |
Andi Kleen | b7af41a | 2013-09-20 07:40:44 -0700 | [diff] [blame] | 708 | cpuc->lbr_stack.nr = out; |
Kan Liang | db278b9 | 2020-01-27 08:53:55 -0800 | [diff] [blame] | 709 | cpuc->lbr_stack.hw_idx = tos; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Kevin Winchester | de0428a | 2011-08-30 20:41:05 -0300 | [diff] [blame] | 712 | void intel_pmu_lbr_read(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 713 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 714 | struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 715 | |
Andi Kleen | d3617b98 | 2019-04-02 12:45:03 -0700 | [diff] [blame] | 716 | /* |
| 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 Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 722 | if (!cpuc->lbr_users || vlbr_exclude_host() || |
| 723 | cpuc->lbr_users == cpuc->lbr_pebs_users) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 724 | return; |
| 725 | |
Kan Liang | c301b1d | 2020-07-03 05:49:09 -0700 | [diff] [blame] | 726 | x86_pmu.lbr_read(cpuc); |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 727 | |
| 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, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 736 | static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 737 | { |
| 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 Eranian | 2b923c8 | 2013-05-21 12:53:37 +0200 | [diff] [blame] | 744 | if (br_type & PERF_SAMPLE_BRANCH_KERNEL) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 745 | 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 760 | |
| 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 Khandual | 3754891 | 2014-05-22 12:50:09 +0530 | [diff] [blame] | 770 | if (br_type & PERF_SAMPLE_BRANCH_COND) |
| 771 | mask |= X86_BR_JCC; |
| 772 | |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 773 | 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 Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 782 | if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP) |
| 783 | mask |= X86_BR_IND_JMP; |
| 784 | |
Stephane Eranian | d892819 | 2015-10-13 09:09:09 +0200 | [diff] [blame] | 785 | if (br_type & PERF_SAMPLE_BRANCH_CALL) |
| 786 | mask |= X86_BR_CALL | X86_BR_ZERO_CALL; |
Jin Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 787 | |
| 788 | if (br_type & PERF_SAMPLE_BRANCH_TYPE_SAVE) |
| 789 | mask |= X86_BR_TYPE_SAVE; |
| 790 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 791 | /* |
| 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, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 796 | return 0; |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 799 | /* |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 800 | * 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 | */ |
| 804 | static 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, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 808 | u64 mask = 0, v; |
| 809 | int i; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 810 | |
Peter Zijlstra | 2c44b19 | 2014-11-05 10:36:45 +0100 | [diff] [blame] | 811 | for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) { |
Yan, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 812 | if (!(br_type & (1ULL << i))) |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 813 | continue; |
| 814 | |
Yan, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 815 | v = x86_pmu.lbr_sel_map[i]; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 816 | if (v == LBR_NOT_SUPP) |
| 817 | return -EOPNOTSUPP; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 818 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 819 | if (v != LBR_IGN) |
| 820 | mask |= v; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 821 | } |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 822 | |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 823 | reg = &event->hw.branch_reg; |
| 824 | reg->idx = EXTRA_REG_LBR; |
| 825 | |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 826 | /* |
| 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 Liang | cf3beb7 | 2016-04-21 02:30:10 -0700 | [diff] [blame] | 830 | * But the 10th bit LBR_CALL_STACK does not operate |
| 831 | * in suppress mode. |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 832 | */ |
Kan Liang | cf3beb7 | 2016-04-21 02:30:10 -0700 | [diff] [blame] | 833 | reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK); |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 834 | |
Andi Kleen | b16a5b5 | 2015-10-20 11:46:34 -0700 | [diff] [blame] | 835 | 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 Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 843 | int intel_pmu_setup_lbr_filter(struct perf_event *event) |
| 844 | { |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 845 | int ret = 0; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * no LBR on this PMU |
| 849 | */ |
| 850 | if (!x86_pmu.lbr_nr) |
| 851 | return -EOPNOTSUPP; |
| 852 | |
| 853 | /* |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 854 | * setup SW LBR filter |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 855 | */ |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 856 | ret = intel_pmu_setup_sw_lbr_filter(event); |
| 857 | if (ret) |
| 858 | return ret; |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 859 | |
| 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 Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 871 | * instruction is not necessarily a branch (in case of interrupt). |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 872 | * |
| 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 880 | static int branch_type(unsigned long from, unsigned long to, int abort) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 881 | { |
| 882 | struct insn insn; |
| 883 | void *addr; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 884 | int bytes_read, bytes_left; |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 885 | 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 900 | if (abort) |
| 901 | return X86_BR_ABORT | to_plm; |
| 902 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 903 | 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 Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 912 | 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 Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 916 | return X86_BR_NONE; |
| 917 | |
| 918 | addr = buf; |
Peter Zijlstra | 6e15eb3 | 2013-05-03 14:11:24 +0200 | [diff] [blame] | 919 | } 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 Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 926 | if (kernel_text_address(from)) { |
Peter Zijlstra | 6e15eb3 | 2013-05-03 14:11:24 +0200 | [diff] [blame] | 927 | addr = (void *)from; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 928 | /* |
| 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 Zijlstra | 6e15eb3 | 2013-05-03 14:11:24 +0200 | [diff] [blame] | 937 | return X86_BR_NONE; |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 938 | } |
Peter Zijlstra | 6e15eb3 | 2013-05-03 14:11:24 +0200 | [diff] [blame] | 939 | } |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 940 | |
| 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 Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 948 | insn_init(&insn, addr, bytes_read, is64); |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 949 | insn_get_opcode(&insn); |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 950 | if (!insn.opcode.got) |
| 951 | return X86_BR_ABORT; |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 952 | |
| 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, Zheng | aa54ae9 | 2014-11-04 21:56:11 -0500 | [diff] [blame] | 987 | 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. Silva | 2b0fc37 | 2019-01-25 12:49:17 -0600 | [diff] [blame] | 993 | /* fall through */ |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 994 | 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 Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 1013 | ret = X86_BR_IND_JMP; |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1014 | break; |
| 1015 | } |
| 1016 | break; |
| 1017 | default: |
| 1018 | ret = X86_BR_NONE; |
Stephane Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1019 | } |
| 1020 | /* |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1021 | * 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 Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1030 | */ |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1031 | 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 Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1034 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1035 | /* |
| 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 Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 1045 | #define X86_BR_TYPE_MAP_MAX 16 |
| 1046 | |
| 1047 | static 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 | |
| 1066 | static int |
| 1067 | common_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 Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1082 | /* |
| 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 | */ |
| 1089 | static void |
| 1090 | intel_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 Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 1098 | if (((br_sel & X86_BR_ALL) == X86_BR_ALL) && |
| 1099 | ((br_sel & X86_BR_TYPE_SAVE) != X86_BR_TYPE_SAVE)) |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1100 | 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 Kleen | 135c561 | 2013-06-17 17:36:51 -0700 | [diff] [blame] | 1107 | 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 Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1114 | |
| 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 Yao | d5c7f9d | 2017-07-18 20:13:10 +0800 | [diff] [blame] | 1120 | |
| 1121 | if ((br_sel & X86_BR_TYPE_SAVE) == X86_BR_TYPE_SAVE) |
| 1122 | cpuc->lbr_entries[i].type = common_branch_type(type); |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1123 | } |
| 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 Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Kan Liang | c22497f | 2019-04-02 12:45:02 -0700 | [diff] [blame] | 1142 | void 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 Liang | db278b9 | 2020-01-27 08:53:55 -0800 | [diff] [blame] | 1148 | |
| 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 Liang | c22497f | 2019-04-02 12:45:02 -0700 | [diff] [blame] | 1155 | 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 Eranian | 60ce0fb | 2012-02-09 23:20:57 +0100 | [diff] [blame] | 1171 | /* |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1172 | * Map interface branch filters onto LBR filters |
| 1173 | */ |
Peter Zijlstra | 2c44b19 | 2014-11-05 10:36:45 +0100 | [diff] [blame] | 1174 | static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = { |
Yan, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 1175 | [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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1181 | /* |
| 1182 | * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches |
| 1183 | */ |
Yan, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 1184 | [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1185 | 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, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 1189 | [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP, |
| 1190 | [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC, |
Stephane Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 1191 | [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP, |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1192 | }; |
| 1193 | |
Peter Zijlstra | 2c44b19 | 2014-11-05 10:36:45 +0100 | [diff] [blame] | 1194 | static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = { |
Yan, Zheng | 27ac905 | 2014-11-04 21:55:57 -0500 | [diff] [blame] | 1195 | [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 Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 1204 | [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP, |
Stephane Eranian | d892819 | 2015-10-13 09:09:09 +0200 | [diff] [blame] | 1205 | [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL, |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1206 | }; |
| 1207 | |
Peter Zijlstra | 2c44b19 | 2014-11-05 10:36:45 +0100 | [diff] [blame] | 1208 | static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = { |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 1209 | [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 Eranian | 7b74cfb | 2015-05-14 23:09:59 +0200 | [diff] [blame] | 1220 | [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP, |
Stephane Eranian | d892819 | 2015-10-13 09:09:09 +0200 | [diff] [blame] | 1221 | [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL, |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 1222 | }; |
| 1223 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1224 | /* core */ |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1225 | void __init intel_pmu_lbr_init_core(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1226 | { |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1227 | x86_pmu.lbr_nr = 4; |
Stephane Eranian | 225ce53 | 2012-02-09 23:20:52 +0100 | [diff] [blame] | 1228 | 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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1231 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1232 | /* |
| 1233 | * SW branch filter usage: |
| 1234 | * - compensate for lack of HW filter |
| 1235 | */ |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1238 | /* nehalem/westmere */ |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1239 | void __init intel_pmu_lbr_init_nhm(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1240 | { |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1241 | x86_pmu.lbr_nr = 16; |
Stephane Eranian | 225ce53 | 2012-02-09 23:20:52 +0100 | [diff] [blame] | 1242 | 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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1245 | |
| 1246 | x86_pmu.lbr_sel_mask = LBR_SEL_MASK; |
| 1247 | x86_pmu.lbr_sel_map = nhm_lbr_sel_map; |
| 1248 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1249 | /* |
| 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 Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1258 | /* sandy bridge */ |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1259 | void __init intel_pmu_lbr_init_snb(void) |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1260 | { |
| 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 Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1269 | /* |
| 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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 1277 | /* haswell */ |
| 1278 | void 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-Cisneros | 19fc9dd | 2016-06-21 11:31:11 -0700 | [diff] [blame] | 1287 | |
| 1288 | if (lbr_from_signext_quirk_needed()) |
| 1289 | static_branch_enable(&lbr_from_quirk_key); |
Yan, Zheng | e9d7f7cd | 2014-11-04 21:56:00 -0500 | [diff] [blame] | 1290 | } |
| 1291 | |
Andi Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 1292 | /* 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 Kleen | 9a92e16 | 2015-05-10 12:22:44 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
Stephane Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1311 | /* atom */ |
Mathias Krause | 066ce64 | 2014-08-26 18:49:45 +0200 | [diff] [blame] | 1312 | void __init intel_pmu_lbr_init_atom(void) |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1313 | { |
Stephane Eranian | 88c9a65 | 2012-02-09 23:20:56 +0100 | [diff] [blame] | 1314 | /* |
| 1315 | * only models starting at stepping 10 seems |
| 1316 | * to have an operational LBR which can freeze |
| 1317 | * on PMU interrupt |
| 1318 | */ |
Stephane Eranian | 3ec18cd | 2012-08-20 11:24:21 +0200 | [diff] [blame] | 1319 | if (boot_cpu_data.x86_model == 28 |
Jia Zhang | b399151 | 2018-01-01 09:52:10 +0800 | [diff] [blame] | 1320 | && boot_cpu_data.x86_stepping < 10) { |
Stephane Eranian | 88c9a65 | 2012-02-09 23:20:56 +0100 | [diff] [blame] | 1321 | pr_cont("LBR disabled due to erratum"); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1325 | x86_pmu.lbr_nr = 8; |
Stephane Eranian | 225ce53 | 2012-02-09 23:20:52 +0100 | [diff] [blame] | 1326 | 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 Eranian | c5cc2cd | 2012-02-09 23:20:55 +0100 | [diff] [blame] | 1329 | |
Stephane Eranian | 3e702ff | 2012-02-09 23:20:58 +0100 | [diff] [blame] | 1330 | /* |
| 1331 | * SW branch filter usage: |
| 1332 | * - compensate for lack of HW filter |
| 1333 | */ |
Peter Zijlstra | caff2be | 2010-03-03 12:02:30 +0100 | [diff] [blame] | 1334 | } |
Harish Chegondi | 1e7b939 | 2015-12-07 14:28:18 -0800 | [diff] [blame] | 1335 | |
Kan Liang | f21d5ad | 2016-04-15 00:53:45 -0700 | [diff] [blame] | 1336 | /* slm */ |
| 1337 | void __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 Chegondi | 1e7b939 | 2015-12-07 14:28:18 -0800 | [diff] [blame] | 1354 | /* Knights Landing */ |
| 1355 | void 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 Tomaka | 16160c1 | 2018-08-02 09:38:30 +0800 | [diff] [blame] | 1364 | |
| 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 Chegondi | 1e7b939 | 2015-12-07 14:28:18 -0800 | [diff] [blame] | 1368 | } |
Like Xu | b2d6504 | 2020-06-13 16:09:48 +0800 | [diff] [blame] | 1369 | |
| 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 | */ |
| 1377 | int 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 | } |
| 1388 | EXPORT_SYMBOL_GPL(x86_perf_get_lbr); |
Like Xu | 097e431 | 2020-06-13 16:09:49 +0800 | [diff] [blame] | 1389 | |
| 1390 | struct event_constraint vlbr_constraint = |
Like Xu | e1ad1ac | 2020-06-13 16:09:50 +0800 | [diff] [blame] | 1391 | __EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT, (1ULL << INTEL_PMC_IDX_FIXED_VLBR), |
| 1392 | FIXED_EVENT_FLAGS, 1, 0, PERF_X86_EVENT_LBR_SELECT); |