lbr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. #include <linux/perf_event.h>
  2. #include <linux/types.h>
  3. #include <asm/perf_event.h>
  4. #include <asm/msr.h>
  5. #include <asm/insn.h>
  6. #include "../perf_event.h"
  7. enum {
  8. LBR_FORMAT_32 = 0x00,
  9. LBR_FORMAT_LIP = 0x01,
  10. LBR_FORMAT_EIP = 0x02,
  11. LBR_FORMAT_EIP_FLAGS = 0x03,
  12. LBR_FORMAT_EIP_FLAGS2 = 0x04,
  13. LBR_FORMAT_INFO = 0x05,
  14. LBR_FORMAT_TIME = 0x06,
  15. LBR_FORMAT_MAX_KNOWN = LBR_FORMAT_TIME,
  16. };
  17. static enum {
  18. LBR_EIP_FLAGS = 1,
  19. LBR_TSX = 2,
  20. } lbr_desc[LBR_FORMAT_MAX_KNOWN + 1] = {
  21. [LBR_FORMAT_EIP_FLAGS] = LBR_EIP_FLAGS,
  22. [LBR_FORMAT_EIP_FLAGS2] = LBR_EIP_FLAGS | LBR_TSX,
  23. };
  24. /*
  25. * Intel LBR_SELECT bits
  26. * Intel Vol3a, April 2011, Section 16.7 Table 16-10
  27. *
  28. * Hardware branch filter (not available on all CPUs)
  29. */
  30. #define LBR_KERNEL_BIT 0 /* do not capture at ring0 */
  31. #define LBR_USER_BIT 1 /* do not capture at ring > 0 */
  32. #define LBR_JCC_BIT 2 /* do not capture conditional branches */
  33. #define LBR_REL_CALL_BIT 3 /* do not capture relative calls */
  34. #define LBR_IND_CALL_BIT 4 /* do not capture indirect calls */
  35. #define LBR_RETURN_BIT 5 /* do not capture near returns */
  36. #define LBR_IND_JMP_BIT 6 /* do not capture indirect jumps */
  37. #define LBR_REL_JMP_BIT 7 /* do not capture relative jumps */
  38. #define LBR_FAR_BIT 8 /* do not capture far branches */
  39. #define LBR_CALL_STACK_BIT 9 /* enable call stack */
  40. /*
  41. * Following bit only exists in Linux; we mask it out before writing it to
  42. * the actual MSR. But it helps the constraint perf code to understand
  43. * that this is a separate configuration.
  44. */
  45. #define LBR_NO_INFO_BIT 63 /* don't read LBR_INFO. */
  46. #define LBR_KERNEL (1 << LBR_KERNEL_BIT)
  47. #define LBR_USER (1 << LBR_USER_BIT)
  48. #define LBR_JCC (1 << LBR_JCC_BIT)
  49. #define LBR_REL_CALL (1 << LBR_REL_CALL_BIT)
  50. #define LBR_IND_CALL (1 << LBR_IND_CALL_BIT)
  51. #define LBR_RETURN (1 << LBR_RETURN_BIT)
  52. #define LBR_REL_JMP (1 << LBR_REL_JMP_BIT)
  53. #define LBR_IND_JMP (1 << LBR_IND_JMP_BIT)
  54. #define LBR_FAR (1 << LBR_FAR_BIT)
  55. #define LBR_CALL_STACK (1 << LBR_CALL_STACK_BIT)
  56. #define LBR_NO_INFO (1ULL << LBR_NO_INFO_BIT)
  57. #define LBR_PLM (LBR_KERNEL | LBR_USER)
  58. #define LBR_SEL_MASK 0x3ff /* valid bits in LBR_SELECT */
  59. #define LBR_NOT_SUPP -1 /* LBR filter not supported */
  60. #define LBR_IGN 0 /* ignored */
  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. #define LBR_FROM_FLAG_MISPRED BIT_ULL(63)
  70. #define LBR_FROM_FLAG_IN_TX BIT_ULL(62)
  71. #define LBR_FROM_FLAG_ABORT BIT_ULL(61)
  72. #define LBR_FROM_SIGNEXT_2MSB (BIT_ULL(60) | BIT_ULL(59))
  73. /*
  74. * x86control flow change classification
  75. * x86control flow changes include branches, interrupts, traps, faults
  76. */
  77. enum {
  78. X86_BR_NONE = 0, /* unknown */
  79. X86_BR_USER = 1 << 0, /* branch target is user */
  80. X86_BR_KERNEL = 1 << 1, /* branch target is kernel */
  81. X86_BR_CALL = 1 << 2, /* call */
  82. X86_BR_RET = 1 << 3, /* return */
  83. X86_BR_SYSCALL = 1 << 4, /* syscall */
  84. X86_BR_SYSRET = 1 << 5, /* syscall return */
  85. X86_BR_INT = 1 << 6, /* sw interrupt */
  86. X86_BR_IRET = 1 << 7, /* return from interrupt */
  87. X86_BR_JCC = 1 << 8, /* conditional */
  88. X86_BR_JMP = 1 << 9, /* jump */
  89. X86_BR_IRQ = 1 << 10,/* hw interrupt or trap or fault */
  90. X86_BR_IND_CALL = 1 << 11,/* indirect calls */
  91. X86_BR_ABORT = 1 << 12,/* transaction abort */
  92. X86_BR_IN_TX = 1 << 13,/* in transaction */
  93. X86_BR_NO_TX = 1 << 14,/* not in transaction */
  94. X86_BR_ZERO_CALL = 1 << 15,/* zero length call */
  95. X86_BR_CALL_STACK = 1 << 16,/* call stack */
  96. X86_BR_IND_JMP = 1 << 17,/* indirect jump */
  97. };
  98. #define X86_BR_PLM (X86_BR_USER | X86_BR_KERNEL)
  99. #define X86_BR_ANYTX (X86_BR_NO_TX | X86_BR_IN_TX)
  100. #define X86_BR_ANY \
  101. (X86_BR_CALL |\
  102. X86_BR_RET |\
  103. X86_BR_SYSCALL |\
  104. X86_BR_SYSRET |\
  105. X86_BR_INT |\
  106. X86_BR_IRET |\
  107. X86_BR_JCC |\
  108. X86_BR_JMP |\
  109. X86_BR_IRQ |\
  110. X86_BR_ABORT |\
  111. X86_BR_IND_CALL |\
  112. X86_BR_IND_JMP |\
  113. X86_BR_ZERO_CALL)
  114. #define X86_BR_ALL (X86_BR_PLM | X86_BR_ANY)
  115. #define X86_BR_ANY_CALL \
  116. (X86_BR_CALL |\
  117. X86_BR_IND_CALL |\
  118. X86_BR_ZERO_CALL |\
  119. X86_BR_SYSCALL |\
  120. X86_BR_IRQ |\
  121. X86_BR_INT)
  122. static void intel_pmu_lbr_filter(struct cpu_hw_events *cpuc);
  123. /*
  124. * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
  125. * otherwise it becomes near impossible to get a reliable stack.
  126. */
  127. static void __intel_pmu_lbr_enable(bool pmi)
  128. {
  129. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  130. u64 debugctl, lbr_select = 0, orig_debugctl;
  131. /*
  132. * No need to unfreeze manually, as v4 can do that as part
  133. * of the GLOBAL_STATUS ack.
  134. */
  135. if (pmi && x86_pmu.version >= 4)
  136. return;
  137. /*
  138. * No need to reprogram LBR_SELECT in a PMI, as it
  139. * did not change.
  140. */
  141. if (cpuc->lbr_sel)
  142. lbr_select = cpuc->lbr_sel->config & x86_pmu.lbr_sel_mask;
  143. if (!pmi && cpuc->lbr_sel)
  144. wrmsrl(MSR_LBR_SELECT, lbr_select);
  145. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  146. orig_debugctl = debugctl;
  147. debugctl |= DEBUGCTLMSR_LBR;
  148. /*
  149. * LBR callstack does not work well with FREEZE_LBRS_ON_PMI.
  150. * If FREEZE_LBRS_ON_PMI is set, PMI near call/return instructions
  151. * may cause superfluous increase/decrease of LBR_TOS.
  152. */
  153. if (!(lbr_select & LBR_CALL_STACK))
  154. debugctl |= DEBUGCTLMSR_FREEZE_LBRS_ON_PMI;
  155. if (orig_debugctl != debugctl)
  156. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  157. }
  158. static void __intel_pmu_lbr_disable(void)
  159. {
  160. u64 debugctl;
  161. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  162. debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
  163. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  164. }
  165. static void intel_pmu_lbr_reset_32(void)
  166. {
  167. int i;
  168. for (i = 0; i < x86_pmu.lbr_nr; i++)
  169. wrmsrl(x86_pmu.lbr_from + i, 0);
  170. }
  171. static void intel_pmu_lbr_reset_64(void)
  172. {
  173. int i;
  174. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  175. wrmsrl(x86_pmu.lbr_from + i, 0);
  176. wrmsrl(x86_pmu.lbr_to + i, 0);
  177. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
  178. wrmsrl(MSR_LBR_INFO_0 + i, 0);
  179. }
  180. }
  181. void intel_pmu_lbr_reset(void)
  182. {
  183. if (!x86_pmu.lbr_nr)
  184. return;
  185. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
  186. intel_pmu_lbr_reset_32();
  187. else
  188. intel_pmu_lbr_reset_64();
  189. }
  190. /*
  191. * TOS = most recently recorded branch
  192. */
  193. static inline u64 intel_pmu_lbr_tos(void)
  194. {
  195. u64 tos;
  196. rdmsrl(x86_pmu.lbr_tos, tos);
  197. return tos;
  198. }
  199. enum {
  200. LBR_NONE,
  201. LBR_VALID,
  202. };
  203. /*
  204. * For formats with LBR_TSX flags (e.g. LBR_FORMAT_EIP_FLAGS2), bits 61:62 in
  205. * MSR_LAST_BRANCH_FROM_x are the TSX flags when TSX is supported, but when
  206. * TSX is not supported they have no consistent behavior:
  207. *
  208. * - For wrmsr(), bits 61:62 are considered part of the sign extension.
  209. * - For HW updates (branch captures) bits 61:62 are always OFF and are not
  210. * part of the sign extension.
  211. *
  212. * Therefore, if:
  213. *
  214. * 1) LBR has TSX format
  215. * 2) CPU has no TSX support enabled
  216. *
  217. * ... then any value passed to wrmsr() must be sign extended to 63 bits and any
  218. * value from rdmsr() must be converted to have a 61 bits sign extension,
  219. * ignoring the TSX flags.
  220. */
  221. static inline bool lbr_from_signext_quirk_needed(void)
  222. {
  223. int lbr_format = x86_pmu.intel_cap.lbr_format;
  224. bool tsx_support = boot_cpu_has(X86_FEATURE_HLE) ||
  225. boot_cpu_has(X86_FEATURE_RTM);
  226. return !tsx_support && (lbr_desc[lbr_format] & LBR_TSX);
  227. }
  228. DEFINE_STATIC_KEY_FALSE(lbr_from_quirk_key);
  229. /* If quirk is enabled, ensure sign extension is 63 bits: */
  230. inline u64 lbr_from_signext_quirk_wr(u64 val)
  231. {
  232. if (static_branch_unlikely(&lbr_from_quirk_key)) {
  233. /*
  234. * Sign extend into bits 61:62 while preserving bit 63.
  235. *
  236. * Quirk is enabled when TSX is disabled. Therefore TSX bits
  237. * in val are always OFF and must be changed to be sign
  238. * extension bits. Since bits 59:60 are guaranteed to be
  239. * part of the sign extension bits, we can just copy them
  240. * to 61:62.
  241. */
  242. val |= (LBR_FROM_SIGNEXT_2MSB & val) << 2;
  243. }
  244. return val;
  245. }
  246. /*
  247. * If quirk is needed, ensure sign extension is 61 bits:
  248. */
  249. u64 lbr_from_signext_quirk_rd(u64 val)
  250. {
  251. if (static_branch_unlikely(&lbr_from_quirk_key)) {
  252. /*
  253. * Quirk is on when TSX is not enabled. Therefore TSX
  254. * flags must be read as OFF.
  255. */
  256. val &= ~(LBR_FROM_FLAG_IN_TX | LBR_FROM_FLAG_ABORT);
  257. }
  258. return val;
  259. }
  260. static inline void wrlbr_from(unsigned int idx, u64 val)
  261. {
  262. val = lbr_from_signext_quirk_wr(val);
  263. wrmsrl(x86_pmu.lbr_from + idx, val);
  264. }
  265. static inline void wrlbr_to(unsigned int idx, u64 val)
  266. {
  267. wrmsrl(x86_pmu.lbr_to + idx, val);
  268. }
  269. static inline u64 rdlbr_from(unsigned int idx)
  270. {
  271. u64 val;
  272. rdmsrl(x86_pmu.lbr_from + idx, val);
  273. return lbr_from_signext_quirk_rd(val);
  274. }
  275. static inline u64 rdlbr_to(unsigned int idx)
  276. {
  277. u64 val;
  278. rdmsrl(x86_pmu.lbr_to + idx, val);
  279. return val;
  280. }
  281. static void __intel_pmu_lbr_restore(struct x86_perf_task_context *task_ctx)
  282. {
  283. int i;
  284. unsigned lbr_idx, mask;
  285. u64 tos;
  286. if (task_ctx->lbr_callstack_users == 0 ||
  287. task_ctx->lbr_stack_state == LBR_NONE) {
  288. intel_pmu_lbr_reset();
  289. return;
  290. }
  291. mask = x86_pmu.lbr_nr - 1;
  292. tos = task_ctx->tos;
  293. for (i = 0; i < tos; i++) {
  294. lbr_idx = (tos - i) & mask;
  295. wrlbr_from(lbr_idx, task_ctx->lbr_from[i]);
  296. wrlbr_to (lbr_idx, task_ctx->lbr_to[i]);
  297. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
  298. wrmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
  299. }
  300. wrmsrl(x86_pmu.lbr_tos, tos);
  301. task_ctx->lbr_stack_state = LBR_NONE;
  302. }
  303. static void __intel_pmu_lbr_save(struct x86_perf_task_context *task_ctx)
  304. {
  305. unsigned lbr_idx, mask;
  306. u64 tos;
  307. int i;
  308. if (task_ctx->lbr_callstack_users == 0) {
  309. task_ctx->lbr_stack_state = LBR_NONE;
  310. return;
  311. }
  312. mask = x86_pmu.lbr_nr - 1;
  313. tos = intel_pmu_lbr_tos();
  314. for (i = 0; i < tos; i++) {
  315. lbr_idx = (tos - i) & mask;
  316. task_ctx->lbr_from[i] = rdlbr_from(lbr_idx);
  317. task_ctx->lbr_to[i] = rdlbr_to(lbr_idx);
  318. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO)
  319. rdmsrl(MSR_LBR_INFO_0 + lbr_idx, task_ctx->lbr_info[i]);
  320. }
  321. task_ctx->tos = tos;
  322. task_ctx->lbr_stack_state = LBR_VALID;
  323. }
  324. void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in)
  325. {
  326. struct x86_perf_task_context *task_ctx;
  327. /*
  328. * If LBR callstack feature is enabled and the stack was saved when
  329. * the task was scheduled out, restore the stack. Otherwise flush
  330. * the LBR stack.
  331. */
  332. task_ctx = ctx ? ctx->task_ctx_data : NULL;
  333. if (task_ctx) {
  334. if (sched_in)
  335. __intel_pmu_lbr_restore(task_ctx);
  336. else
  337. __intel_pmu_lbr_save(task_ctx);
  338. return;
  339. }
  340. /*
  341. * Since a context switch can flip the address space and LBR entries
  342. * are not tagged with an identifier, we need to wipe the LBR, even for
  343. * per-cpu events. You simply cannot resolve the branches from the old
  344. * address space.
  345. */
  346. if (sched_in)
  347. intel_pmu_lbr_reset();
  348. }
  349. static inline bool branch_user_callstack(unsigned br_sel)
  350. {
  351. return (br_sel & X86_BR_USER) && (br_sel & X86_BR_CALL_STACK);
  352. }
  353. void intel_pmu_lbr_add(struct perf_event *event)
  354. {
  355. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  356. struct x86_perf_task_context *task_ctx;
  357. if (!x86_pmu.lbr_nr)
  358. return;
  359. cpuc->br_sel = event->hw.branch_reg.reg;
  360. if (branch_user_callstack(cpuc->br_sel) && event->ctx->task_ctx_data) {
  361. task_ctx = event->ctx->task_ctx_data;
  362. task_ctx->lbr_callstack_users++;
  363. }
  364. /*
  365. * Request pmu::sched_task() callback, which will fire inside the
  366. * regular perf event scheduling, so that call will:
  367. *
  368. * - restore or wipe; when LBR-callstack,
  369. * - wipe; otherwise,
  370. *
  371. * when this is from __perf_event_task_sched_in().
  372. *
  373. * However, if this is from perf_install_in_context(), no such callback
  374. * will follow and we'll need to reset the LBR here if this is the
  375. * first LBR event.
  376. *
  377. * The problem is, we cannot tell these cases apart... but we can
  378. * exclude the biggest chunk of cases by looking at
  379. * event->total_time_running. An event that has accrued runtime cannot
  380. * be 'new'. Conversely, a new event can get installed through the
  381. * context switch path for the first time.
  382. */
  383. perf_sched_cb_inc(event->ctx->pmu);
  384. if (!cpuc->lbr_users++ && !event->total_time_running)
  385. intel_pmu_lbr_reset();
  386. }
  387. void intel_pmu_lbr_del(struct perf_event *event)
  388. {
  389. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  390. struct x86_perf_task_context *task_ctx;
  391. if (!x86_pmu.lbr_nr)
  392. return;
  393. if (branch_user_callstack(cpuc->br_sel) &&
  394. event->ctx->task_ctx_data) {
  395. task_ctx = event->ctx->task_ctx_data;
  396. task_ctx->lbr_callstack_users--;
  397. }
  398. cpuc->lbr_users--;
  399. WARN_ON_ONCE(cpuc->lbr_users < 0);
  400. perf_sched_cb_dec(event->ctx->pmu);
  401. }
  402. void intel_pmu_lbr_enable_all(bool pmi)
  403. {
  404. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  405. if (cpuc->lbr_users)
  406. __intel_pmu_lbr_enable(pmi);
  407. }
  408. void intel_pmu_lbr_disable_all(void)
  409. {
  410. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  411. if (cpuc->lbr_users)
  412. __intel_pmu_lbr_disable();
  413. }
  414. static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
  415. {
  416. unsigned long mask = x86_pmu.lbr_nr - 1;
  417. u64 tos = intel_pmu_lbr_tos();
  418. int i;
  419. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  420. unsigned long lbr_idx = (tos - i) & mask;
  421. union {
  422. struct {
  423. u32 from;
  424. u32 to;
  425. };
  426. u64 lbr;
  427. } msr_lastbranch;
  428. rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
  429. cpuc->lbr_entries[i].from = msr_lastbranch.from;
  430. cpuc->lbr_entries[i].to = msr_lastbranch.to;
  431. cpuc->lbr_entries[i].mispred = 0;
  432. cpuc->lbr_entries[i].predicted = 0;
  433. cpuc->lbr_entries[i].in_tx = 0;
  434. cpuc->lbr_entries[i].abort = 0;
  435. cpuc->lbr_entries[i].cycles = 0;
  436. cpuc->lbr_entries[i].reserved = 0;
  437. }
  438. cpuc->lbr_stack.nr = i;
  439. }
  440. /*
  441. * Due to lack of segmentation in Linux the effective address (offset)
  442. * is the same as the linear address, allowing us to merge the LIP and EIP
  443. * LBR formats.
  444. */
  445. static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
  446. {
  447. bool need_info = false;
  448. unsigned long mask = x86_pmu.lbr_nr - 1;
  449. int lbr_format = x86_pmu.intel_cap.lbr_format;
  450. u64 tos = intel_pmu_lbr_tos();
  451. int i;
  452. int out = 0;
  453. int num = x86_pmu.lbr_nr;
  454. if (cpuc->lbr_sel) {
  455. need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
  456. if (cpuc->lbr_sel->config & LBR_CALL_STACK)
  457. num = tos;
  458. }
  459. for (i = 0; i < num; i++) {
  460. unsigned long lbr_idx = (tos - i) & mask;
  461. u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
  462. int skip = 0;
  463. u16 cycles = 0;
  464. int lbr_flags = lbr_desc[lbr_format];
  465. from = rdlbr_from(lbr_idx);
  466. to = rdlbr_to(lbr_idx);
  467. if (lbr_format == LBR_FORMAT_INFO && need_info) {
  468. u64 info;
  469. rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
  470. mis = !!(info & LBR_INFO_MISPRED);
  471. pred = !mis;
  472. in_tx = !!(info & LBR_INFO_IN_TX);
  473. abort = !!(info & LBR_INFO_ABORT);
  474. cycles = (info & LBR_INFO_CYCLES);
  475. }
  476. if (lbr_format == LBR_FORMAT_TIME) {
  477. mis = !!(from & LBR_FROM_FLAG_MISPRED);
  478. pred = !mis;
  479. skip = 1;
  480. cycles = ((to >> 48) & LBR_INFO_CYCLES);
  481. to = (u64)((((s64)to) << 16) >> 16);
  482. }
  483. if (lbr_flags & LBR_EIP_FLAGS) {
  484. mis = !!(from & LBR_FROM_FLAG_MISPRED);
  485. pred = !mis;
  486. skip = 1;
  487. }
  488. if (lbr_flags & LBR_TSX) {
  489. in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
  490. abort = !!(from & LBR_FROM_FLAG_ABORT);
  491. skip = 3;
  492. }
  493. from = (u64)((((s64)from) << skip) >> skip);
  494. /*
  495. * Some CPUs report duplicated abort records,
  496. * with the second entry not having an abort bit set.
  497. * Skip them here. This loop runs backwards,
  498. * so we need to undo the previous record.
  499. * If the abort just happened outside the window
  500. * the extra entry cannot be removed.
  501. */
  502. if (abort && x86_pmu.lbr_double_abort && out > 0)
  503. out--;
  504. cpuc->lbr_entries[out].from = from;
  505. cpuc->lbr_entries[out].to = to;
  506. cpuc->lbr_entries[out].mispred = mis;
  507. cpuc->lbr_entries[out].predicted = pred;
  508. cpuc->lbr_entries[out].in_tx = in_tx;
  509. cpuc->lbr_entries[out].abort = abort;
  510. cpuc->lbr_entries[out].cycles = cycles;
  511. cpuc->lbr_entries[out].reserved = 0;
  512. out++;
  513. }
  514. cpuc->lbr_stack.nr = out;
  515. }
  516. void intel_pmu_lbr_read(void)
  517. {
  518. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  519. if (!cpuc->lbr_users)
  520. return;
  521. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
  522. intel_pmu_lbr_read_32(cpuc);
  523. else
  524. intel_pmu_lbr_read_64(cpuc);
  525. intel_pmu_lbr_filter(cpuc);
  526. }
  527. /*
  528. * SW filter is used:
  529. * - in case there is no HW filter
  530. * - in case the HW filter has errata or limitations
  531. */
  532. static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
  533. {
  534. u64 br_type = event->attr.branch_sample_type;
  535. int mask = 0;
  536. if (br_type & PERF_SAMPLE_BRANCH_USER)
  537. mask |= X86_BR_USER;
  538. if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
  539. mask |= X86_BR_KERNEL;
  540. /* we ignore BRANCH_HV here */
  541. if (br_type & PERF_SAMPLE_BRANCH_ANY)
  542. mask |= X86_BR_ANY;
  543. if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
  544. mask |= X86_BR_ANY_CALL;
  545. if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  546. mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
  547. if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
  548. mask |= X86_BR_IND_CALL;
  549. if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
  550. mask |= X86_BR_ABORT;
  551. if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
  552. mask |= X86_BR_IN_TX;
  553. if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
  554. mask |= X86_BR_NO_TX;
  555. if (br_type & PERF_SAMPLE_BRANCH_COND)
  556. mask |= X86_BR_JCC;
  557. if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
  558. if (!x86_pmu_has_lbr_callstack())
  559. return -EOPNOTSUPP;
  560. if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
  561. return -EINVAL;
  562. mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
  563. X86_BR_CALL_STACK;
  564. }
  565. if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
  566. mask |= X86_BR_IND_JMP;
  567. if (br_type & PERF_SAMPLE_BRANCH_CALL)
  568. mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
  569. /*
  570. * stash actual user request into reg, it may
  571. * be used by fixup code for some CPU
  572. */
  573. event->hw.branch_reg.reg = mask;
  574. return 0;
  575. }
  576. /*
  577. * setup the HW LBR filter
  578. * Used only when available, may not be enough to disambiguate
  579. * all branches, may need the help of the SW filter
  580. */
  581. static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
  582. {
  583. struct hw_perf_event_extra *reg;
  584. u64 br_type = event->attr.branch_sample_type;
  585. u64 mask = 0, v;
  586. int i;
  587. for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
  588. if (!(br_type & (1ULL << i)))
  589. continue;
  590. v = x86_pmu.lbr_sel_map[i];
  591. if (v == LBR_NOT_SUPP)
  592. return -EOPNOTSUPP;
  593. if (v != LBR_IGN)
  594. mask |= v;
  595. }
  596. reg = &event->hw.branch_reg;
  597. reg->idx = EXTRA_REG_LBR;
  598. /*
  599. * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
  600. * in suppress mode. So LBR_SELECT should be set to
  601. * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
  602. * But the 10th bit LBR_CALL_STACK does not operate
  603. * in suppress mode.
  604. */
  605. reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
  606. if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
  607. (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
  608. (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
  609. reg->config |= LBR_NO_INFO;
  610. return 0;
  611. }
  612. int intel_pmu_setup_lbr_filter(struct perf_event *event)
  613. {
  614. int ret = 0;
  615. /*
  616. * no LBR on this PMU
  617. */
  618. if (!x86_pmu.lbr_nr)
  619. return -EOPNOTSUPP;
  620. /*
  621. * setup SW LBR filter
  622. */
  623. ret = intel_pmu_setup_sw_lbr_filter(event);
  624. if (ret)
  625. return ret;
  626. /*
  627. * setup HW LBR filter, if any
  628. */
  629. if (x86_pmu.lbr_sel_map)
  630. ret = intel_pmu_setup_hw_lbr_filter(event);
  631. return ret;
  632. }
  633. /*
  634. * return the type of control flow change at address "from"
  635. * instruction is not necessarily a branch (in case of interrupt).
  636. *
  637. * The branch type returned also includes the priv level of the
  638. * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
  639. *
  640. * If a branch type is unknown OR the instruction cannot be
  641. * decoded (e.g., text page not present), then X86_BR_NONE is
  642. * returned.
  643. */
  644. static int branch_type(unsigned long from, unsigned long to, int abort)
  645. {
  646. struct insn insn;
  647. void *addr;
  648. int bytes_read, bytes_left;
  649. int ret = X86_BR_NONE;
  650. int ext, to_plm, from_plm;
  651. u8 buf[MAX_INSN_SIZE];
  652. int is64 = 0;
  653. to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
  654. from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
  655. /*
  656. * maybe zero if lbr did not fill up after a reset by the time
  657. * we get a PMU interrupt
  658. */
  659. if (from == 0 || to == 0)
  660. return X86_BR_NONE;
  661. if (abort)
  662. return X86_BR_ABORT | to_plm;
  663. if (from_plm == X86_BR_USER) {
  664. /*
  665. * can happen if measuring at the user level only
  666. * and we interrupt in a kernel thread, e.g., idle.
  667. */
  668. if (!current->mm)
  669. return X86_BR_NONE;
  670. /* may fail if text not present */
  671. bytes_left = copy_from_user_nmi(buf, (void __user *)from,
  672. MAX_INSN_SIZE);
  673. bytes_read = MAX_INSN_SIZE - bytes_left;
  674. if (!bytes_read)
  675. return X86_BR_NONE;
  676. addr = buf;
  677. } else {
  678. /*
  679. * The LBR logs any address in the IP, even if the IP just
  680. * faulted. This means userspace can control the from address.
  681. * Ensure we don't blindy read any address by validating it is
  682. * a known text address.
  683. */
  684. if (kernel_text_address(from)) {
  685. addr = (void *)from;
  686. /*
  687. * Assume we can get the maximum possible size
  688. * when grabbing kernel data. This is not
  689. * _strictly_ true since we could possibly be
  690. * executing up next to a memory hole, but
  691. * it is very unlikely to be a problem.
  692. */
  693. bytes_read = MAX_INSN_SIZE;
  694. } else {
  695. return X86_BR_NONE;
  696. }
  697. }
  698. /*
  699. * decoder needs to know the ABI especially
  700. * on 64-bit systems running 32-bit apps
  701. */
  702. #ifdef CONFIG_X86_64
  703. is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
  704. #endif
  705. insn_init(&insn, addr, bytes_read, is64);
  706. insn_get_opcode(&insn);
  707. if (!insn.opcode.got)
  708. return X86_BR_ABORT;
  709. switch (insn.opcode.bytes[0]) {
  710. case 0xf:
  711. switch (insn.opcode.bytes[1]) {
  712. case 0x05: /* syscall */
  713. case 0x34: /* sysenter */
  714. ret = X86_BR_SYSCALL;
  715. break;
  716. case 0x07: /* sysret */
  717. case 0x35: /* sysexit */
  718. ret = X86_BR_SYSRET;
  719. break;
  720. case 0x80 ... 0x8f: /* conditional */
  721. ret = X86_BR_JCC;
  722. break;
  723. default:
  724. ret = X86_BR_NONE;
  725. }
  726. break;
  727. case 0x70 ... 0x7f: /* conditional */
  728. ret = X86_BR_JCC;
  729. break;
  730. case 0xc2: /* near ret */
  731. case 0xc3: /* near ret */
  732. case 0xca: /* far ret */
  733. case 0xcb: /* far ret */
  734. ret = X86_BR_RET;
  735. break;
  736. case 0xcf: /* iret */
  737. ret = X86_BR_IRET;
  738. break;
  739. case 0xcc ... 0xce: /* int */
  740. ret = X86_BR_INT;
  741. break;
  742. case 0xe8: /* call near rel */
  743. insn_get_immediate(&insn);
  744. if (insn.immediate1.value == 0) {
  745. /* zero length call */
  746. ret = X86_BR_ZERO_CALL;
  747. break;
  748. }
  749. case 0x9a: /* call far absolute */
  750. ret = X86_BR_CALL;
  751. break;
  752. case 0xe0 ... 0xe3: /* loop jmp */
  753. ret = X86_BR_JCC;
  754. break;
  755. case 0xe9 ... 0xeb: /* jmp */
  756. ret = X86_BR_JMP;
  757. break;
  758. case 0xff: /* call near absolute, call far absolute ind */
  759. insn_get_modrm(&insn);
  760. ext = (insn.modrm.bytes[0] >> 3) & 0x7;
  761. switch (ext) {
  762. case 2: /* near ind call */
  763. case 3: /* far ind call */
  764. ret = X86_BR_IND_CALL;
  765. break;
  766. case 4:
  767. case 5:
  768. ret = X86_BR_IND_JMP;
  769. break;
  770. }
  771. break;
  772. default:
  773. ret = X86_BR_NONE;
  774. }
  775. /*
  776. * interrupts, traps, faults (and thus ring transition) may
  777. * occur on any instructions. Thus, to classify them correctly,
  778. * we need to first look at the from and to priv levels. If they
  779. * are different and to is in the kernel, then it indicates
  780. * a ring transition. If the from instruction is not a ring
  781. * transition instr (syscall, systenter, int), then it means
  782. * it was a irq, trap or fault.
  783. *
  784. * we have no way of detecting kernel to kernel faults.
  785. */
  786. if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
  787. && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
  788. ret = X86_BR_IRQ;
  789. /*
  790. * branch priv level determined by target as
  791. * is done by HW when LBR_SELECT is implemented
  792. */
  793. if (ret != X86_BR_NONE)
  794. ret |= to_plm;
  795. return ret;
  796. }
  797. /*
  798. * implement actual branch filter based on user demand.
  799. * Hardware may not exactly satisfy that request, thus
  800. * we need to inspect opcodes. Mismatched branches are
  801. * discarded. Therefore, the number of branches returned
  802. * in PERF_SAMPLE_BRANCH_STACK sample may vary.
  803. */
  804. static void
  805. intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
  806. {
  807. u64 from, to;
  808. int br_sel = cpuc->br_sel;
  809. int i, j, type;
  810. bool compress = false;
  811. /* if sampling all branches, then nothing to filter */
  812. if ((br_sel & X86_BR_ALL) == X86_BR_ALL)
  813. return;
  814. for (i = 0; i < cpuc->lbr_stack.nr; i++) {
  815. from = cpuc->lbr_entries[i].from;
  816. to = cpuc->lbr_entries[i].to;
  817. type = branch_type(from, to, cpuc->lbr_entries[i].abort);
  818. if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
  819. if (cpuc->lbr_entries[i].in_tx)
  820. type |= X86_BR_IN_TX;
  821. else
  822. type |= X86_BR_NO_TX;
  823. }
  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. if (!compress)
  831. return;
  832. /* remove all entries with from=0 */
  833. for (i = 0; i < cpuc->lbr_stack.nr; ) {
  834. if (!cpuc->lbr_entries[i].from) {
  835. j = i;
  836. while (++j < cpuc->lbr_stack.nr)
  837. cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
  838. cpuc->lbr_stack.nr--;
  839. if (!cpuc->lbr_entries[i].from)
  840. continue;
  841. }
  842. i++;
  843. }
  844. }
  845. /*
  846. * Map interface branch filters onto LBR filters
  847. */
  848. static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  849. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  850. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  851. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  852. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  853. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
  854. | LBR_IND_JMP | LBR_FAR,
  855. /*
  856. * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
  857. */
  858. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
  859. LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
  860. /*
  861. * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
  862. */
  863. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
  864. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  865. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  866. };
  867. static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  868. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  869. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  870. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  871. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  872. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
  873. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  874. | LBR_FAR,
  875. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
  876. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  877. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  878. [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
  879. };
  880. static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  881. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  882. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  883. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  884. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  885. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
  886. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  887. | LBR_FAR,
  888. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
  889. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  890. [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  891. | LBR_RETURN | LBR_CALL_STACK,
  892. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  893. [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
  894. };
  895. /* core */
  896. void __init intel_pmu_lbr_init_core(void)
  897. {
  898. x86_pmu.lbr_nr = 4;
  899. x86_pmu.lbr_tos = MSR_LBR_TOS;
  900. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  901. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  902. /*
  903. * SW branch filter usage:
  904. * - compensate for lack of HW filter
  905. */
  906. }
  907. /* nehalem/westmere */
  908. void __init intel_pmu_lbr_init_nhm(void)
  909. {
  910. x86_pmu.lbr_nr = 16;
  911. x86_pmu.lbr_tos = MSR_LBR_TOS;
  912. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  913. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  914. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  915. x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
  916. /*
  917. * SW branch filter usage:
  918. * - workaround LBR_SEL errata (see above)
  919. * - support syscall, sysret capture.
  920. * That requires LBR_FAR but that means far
  921. * jmp need to be filtered out
  922. */
  923. }
  924. /* sandy bridge */
  925. void __init intel_pmu_lbr_init_snb(void)
  926. {
  927. x86_pmu.lbr_nr = 16;
  928. x86_pmu.lbr_tos = MSR_LBR_TOS;
  929. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  930. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  931. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  932. x86_pmu.lbr_sel_map = snb_lbr_sel_map;
  933. /*
  934. * SW branch filter usage:
  935. * - support syscall, sysret capture.
  936. * That requires LBR_FAR but that means far
  937. * jmp need to be filtered out
  938. */
  939. }
  940. /* haswell */
  941. void intel_pmu_lbr_init_hsw(void)
  942. {
  943. x86_pmu.lbr_nr = 16;
  944. x86_pmu.lbr_tos = MSR_LBR_TOS;
  945. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  946. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  947. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  948. x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
  949. if (lbr_from_signext_quirk_needed())
  950. static_branch_enable(&lbr_from_quirk_key);
  951. }
  952. /* skylake */
  953. __init void intel_pmu_lbr_init_skl(void)
  954. {
  955. x86_pmu.lbr_nr = 32;
  956. x86_pmu.lbr_tos = MSR_LBR_TOS;
  957. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  958. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  959. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  960. x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
  961. /*
  962. * SW branch filter usage:
  963. * - support syscall, sysret capture.
  964. * That requires LBR_FAR but that means far
  965. * jmp need to be filtered out
  966. */
  967. }
  968. /* atom */
  969. void __init intel_pmu_lbr_init_atom(void)
  970. {
  971. /*
  972. * only models starting at stepping 10 seems
  973. * to have an operational LBR which can freeze
  974. * on PMU interrupt
  975. */
  976. if (boot_cpu_data.x86_model == 28
  977. && boot_cpu_data.x86_stepping < 10) {
  978. pr_cont("LBR disabled due to erratum");
  979. return;
  980. }
  981. x86_pmu.lbr_nr = 8;
  982. x86_pmu.lbr_tos = MSR_LBR_TOS;
  983. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  984. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  985. /*
  986. * SW branch filter usage:
  987. * - compensate for lack of HW filter
  988. */
  989. }
  990. /* slm */
  991. void __init intel_pmu_lbr_init_slm(void)
  992. {
  993. x86_pmu.lbr_nr = 8;
  994. x86_pmu.lbr_tos = MSR_LBR_TOS;
  995. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  996. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  997. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  998. x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
  999. /*
  1000. * SW branch filter usage:
  1001. * - compensate for lack of HW filter
  1002. */
  1003. pr_cont("8-deep LBR, ");
  1004. }
  1005. /* Knights Landing */
  1006. void intel_pmu_lbr_init_knl(void)
  1007. {
  1008. x86_pmu.lbr_nr = 8;
  1009. x86_pmu.lbr_tos = MSR_LBR_TOS;
  1010. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  1011. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  1012. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  1013. x86_pmu.lbr_sel_map = snb_lbr_sel_map;
  1014. }