lbr.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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].reserved = 0;
  434. }
  435. cpuc->lbr_stack.nr = i;
  436. }
  437. /*
  438. * Due to lack of segmentation in Linux the effective address (offset)
  439. * is the same as the linear address, allowing us to merge the LIP and EIP
  440. * LBR formats.
  441. */
  442. static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
  443. {
  444. bool need_info = false;
  445. unsigned long mask = x86_pmu.lbr_nr - 1;
  446. int lbr_format = x86_pmu.intel_cap.lbr_format;
  447. u64 tos = intel_pmu_lbr_tos();
  448. int i;
  449. int out = 0;
  450. int num = x86_pmu.lbr_nr;
  451. if (cpuc->lbr_sel) {
  452. need_info = !(cpuc->lbr_sel->config & LBR_NO_INFO);
  453. if (cpuc->lbr_sel->config & LBR_CALL_STACK)
  454. num = tos;
  455. }
  456. for (i = 0; i < num; i++) {
  457. unsigned long lbr_idx = (tos - i) & mask;
  458. u64 from, to, mis = 0, pred = 0, in_tx = 0, abort = 0;
  459. int skip = 0;
  460. u16 cycles = 0;
  461. int lbr_flags = lbr_desc[lbr_format];
  462. from = rdlbr_from(lbr_idx);
  463. to = rdlbr_to(lbr_idx);
  464. if (lbr_format == LBR_FORMAT_INFO && need_info) {
  465. u64 info;
  466. rdmsrl(MSR_LBR_INFO_0 + lbr_idx, info);
  467. mis = !!(info & LBR_INFO_MISPRED);
  468. pred = !mis;
  469. in_tx = !!(info & LBR_INFO_IN_TX);
  470. abort = !!(info & LBR_INFO_ABORT);
  471. cycles = (info & LBR_INFO_CYCLES);
  472. }
  473. if (lbr_format == LBR_FORMAT_TIME) {
  474. mis = !!(from & LBR_FROM_FLAG_MISPRED);
  475. pred = !mis;
  476. skip = 1;
  477. cycles = ((to >> 48) & LBR_INFO_CYCLES);
  478. to = (u64)((((s64)to) << 16) >> 16);
  479. }
  480. if (lbr_flags & LBR_EIP_FLAGS) {
  481. mis = !!(from & LBR_FROM_FLAG_MISPRED);
  482. pred = !mis;
  483. skip = 1;
  484. }
  485. if (lbr_flags & LBR_TSX) {
  486. in_tx = !!(from & LBR_FROM_FLAG_IN_TX);
  487. abort = !!(from & LBR_FROM_FLAG_ABORT);
  488. skip = 3;
  489. }
  490. from = (u64)((((s64)from) << skip) >> skip);
  491. /*
  492. * Some CPUs report duplicated abort records,
  493. * with the second entry not having an abort bit set.
  494. * Skip them here. This loop runs backwards,
  495. * so we need to undo the previous record.
  496. * If the abort just happened outside the window
  497. * the extra entry cannot be removed.
  498. */
  499. if (abort && x86_pmu.lbr_double_abort && out > 0)
  500. out--;
  501. cpuc->lbr_entries[out].from = from;
  502. cpuc->lbr_entries[out].to = to;
  503. cpuc->lbr_entries[out].mispred = mis;
  504. cpuc->lbr_entries[out].predicted = pred;
  505. cpuc->lbr_entries[out].in_tx = in_tx;
  506. cpuc->lbr_entries[out].abort = abort;
  507. cpuc->lbr_entries[out].cycles = cycles;
  508. cpuc->lbr_entries[out].reserved = 0;
  509. out++;
  510. }
  511. cpuc->lbr_stack.nr = out;
  512. }
  513. void intel_pmu_lbr_read(void)
  514. {
  515. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  516. if (!cpuc->lbr_users)
  517. return;
  518. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
  519. intel_pmu_lbr_read_32(cpuc);
  520. else
  521. intel_pmu_lbr_read_64(cpuc);
  522. intel_pmu_lbr_filter(cpuc);
  523. }
  524. /*
  525. * SW filter is used:
  526. * - in case there is no HW filter
  527. * - in case the HW filter has errata or limitations
  528. */
  529. static int intel_pmu_setup_sw_lbr_filter(struct perf_event *event)
  530. {
  531. u64 br_type = event->attr.branch_sample_type;
  532. int mask = 0;
  533. if (br_type & PERF_SAMPLE_BRANCH_USER)
  534. mask |= X86_BR_USER;
  535. if (br_type & PERF_SAMPLE_BRANCH_KERNEL)
  536. mask |= X86_BR_KERNEL;
  537. /* we ignore BRANCH_HV here */
  538. if (br_type & PERF_SAMPLE_BRANCH_ANY)
  539. mask |= X86_BR_ANY;
  540. if (br_type & PERF_SAMPLE_BRANCH_ANY_CALL)
  541. mask |= X86_BR_ANY_CALL;
  542. if (br_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  543. mask |= X86_BR_RET | X86_BR_IRET | X86_BR_SYSRET;
  544. if (br_type & PERF_SAMPLE_BRANCH_IND_CALL)
  545. mask |= X86_BR_IND_CALL;
  546. if (br_type & PERF_SAMPLE_BRANCH_ABORT_TX)
  547. mask |= X86_BR_ABORT;
  548. if (br_type & PERF_SAMPLE_BRANCH_IN_TX)
  549. mask |= X86_BR_IN_TX;
  550. if (br_type & PERF_SAMPLE_BRANCH_NO_TX)
  551. mask |= X86_BR_NO_TX;
  552. if (br_type & PERF_SAMPLE_BRANCH_COND)
  553. mask |= X86_BR_JCC;
  554. if (br_type & PERF_SAMPLE_BRANCH_CALL_STACK) {
  555. if (!x86_pmu_has_lbr_callstack())
  556. return -EOPNOTSUPP;
  557. if (mask & ~(X86_BR_USER | X86_BR_KERNEL))
  558. return -EINVAL;
  559. mask |= X86_BR_CALL | X86_BR_IND_CALL | X86_BR_RET |
  560. X86_BR_CALL_STACK;
  561. }
  562. if (br_type & PERF_SAMPLE_BRANCH_IND_JUMP)
  563. mask |= X86_BR_IND_JMP;
  564. if (br_type & PERF_SAMPLE_BRANCH_CALL)
  565. mask |= X86_BR_CALL | X86_BR_ZERO_CALL;
  566. /*
  567. * stash actual user request into reg, it may
  568. * be used by fixup code for some CPU
  569. */
  570. event->hw.branch_reg.reg = mask;
  571. return 0;
  572. }
  573. /*
  574. * setup the HW LBR filter
  575. * Used only when available, may not be enough to disambiguate
  576. * all branches, may need the help of the SW filter
  577. */
  578. static int intel_pmu_setup_hw_lbr_filter(struct perf_event *event)
  579. {
  580. struct hw_perf_event_extra *reg;
  581. u64 br_type = event->attr.branch_sample_type;
  582. u64 mask = 0, v;
  583. int i;
  584. for (i = 0; i < PERF_SAMPLE_BRANCH_MAX_SHIFT; i++) {
  585. if (!(br_type & (1ULL << i)))
  586. continue;
  587. v = x86_pmu.lbr_sel_map[i];
  588. if (v == LBR_NOT_SUPP)
  589. return -EOPNOTSUPP;
  590. if (v != LBR_IGN)
  591. mask |= v;
  592. }
  593. reg = &event->hw.branch_reg;
  594. reg->idx = EXTRA_REG_LBR;
  595. /*
  596. * The first 9 bits (LBR_SEL_MASK) in LBR_SELECT operate
  597. * in suppress mode. So LBR_SELECT should be set to
  598. * (~mask & LBR_SEL_MASK) | (mask & ~LBR_SEL_MASK)
  599. * But the 10th bit LBR_CALL_STACK does not operate
  600. * in suppress mode.
  601. */
  602. reg->config = mask ^ (x86_pmu.lbr_sel_mask & ~LBR_CALL_STACK);
  603. if ((br_type & PERF_SAMPLE_BRANCH_NO_CYCLES) &&
  604. (br_type & PERF_SAMPLE_BRANCH_NO_FLAGS) &&
  605. (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_INFO))
  606. reg->config |= LBR_NO_INFO;
  607. return 0;
  608. }
  609. int intel_pmu_setup_lbr_filter(struct perf_event *event)
  610. {
  611. int ret = 0;
  612. /*
  613. * no LBR on this PMU
  614. */
  615. if (!x86_pmu.lbr_nr)
  616. return -EOPNOTSUPP;
  617. /*
  618. * setup SW LBR filter
  619. */
  620. ret = intel_pmu_setup_sw_lbr_filter(event);
  621. if (ret)
  622. return ret;
  623. /*
  624. * setup HW LBR filter, if any
  625. */
  626. if (x86_pmu.lbr_sel_map)
  627. ret = intel_pmu_setup_hw_lbr_filter(event);
  628. return ret;
  629. }
  630. /*
  631. * return the type of control flow change at address "from"
  632. * instruction is not necessarily a branch (in case of interrupt).
  633. *
  634. * The branch type returned also includes the priv level of the
  635. * target of the control flow change (X86_BR_USER, X86_BR_KERNEL).
  636. *
  637. * If a branch type is unknown OR the instruction cannot be
  638. * decoded (e.g., text page not present), then X86_BR_NONE is
  639. * returned.
  640. */
  641. static int branch_type(unsigned long from, unsigned long to, int abort)
  642. {
  643. struct insn insn;
  644. void *addr;
  645. int bytes_read, bytes_left;
  646. int ret = X86_BR_NONE;
  647. int ext, to_plm, from_plm;
  648. u8 buf[MAX_INSN_SIZE];
  649. int is64 = 0;
  650. to_plm = kernel_ip(to) ? X86_BR_KERNEL : X86_BR_USER;
  651. from_plm = kernel_ip(from) ? X86_BR_KERNEL : X86_BR_USER;
  652. /*
  653. * maybe zero if lbr did not fill up after a reset by the time
  654. * we get a PMU interrupt
  655. */
  656. if (from == 0 || to == 0)
  657. return X86_BR_NONE;
  658. if (abort)
  659. return X86_BR_ABORT | to_plm;
  660. if (from_plm == X86_BR_USER) {
  661. /*
  662. * can happen if measuring at the user level only
  663. * and we interrupt in a kernel thread, e.g., idle.
  664. */
  665. if (!current->mm)
  666. return X86_BR_NONE;
  667. /* may fail if text not present */
  668. bytes_left = copy_from_user_nmi(buf, (void __user *)from,
  669. MAX_INSN_SIZE);
  670. bytes_read = MAX_INSN_SIZE - bytes_left;
  671. if (!bytes_read)
  672. return X86_BR_NONE;
  673. addr = buf;
  674. } else {
  675. /*
  676. * The LBR logs any address in the IP, even if the IP just
  677. * faulted. This means userspace can control the from address.
  678. * Ensure we don't blindy read any address by validating it is
  679. * a known text address.
  680. */
  681. if (kernel_text_address(from)) {
  682. addr = (void *)from;
  683. /*
  684. * Assume we can get the maximum possible size
  685. * when grabbing kernel data. This is not
  686. * _strictly_ true since we could possibly be
  687. * executing up next to a memory hole, but
  688. * it is very unlikely to be a problem.
  689. */
  690. bytes_read = MAX_INSN_SIZE;
  691. } else {
  692. return X86_BR_NONE;
  693. }
  694. }
  695. /*
  696. * decoder needs to know the ABI especially
  697. * on 64-bit systems running 32-bit apps
  698. */
  699. #ifdef CONFIG_X86_64
  700. is64 = kernel_ip((unsigned long)addr) || !test_thread_flag(TIF_IA32);
  701. #endif
  702. insn_init(&insn, addr, bytes_read, is64);
  703. insn_get_opcode(&insn);
  704. if (!insn.opcode.got)
  705. return X86_BR_ABORT;
  706. switch (insn.opcode.bytes[0]) {
  707. case 0xf:
  708. switch (insn.opcode.bytes[1]) {
  709. case 0x05: /* syscall */
  710. case 0x34: /* sysenter */
  711. ret = X86_BR_SYSCALL;
  712. break;
  713. case 0x07: /* sysret */
  714. case 0x35: /* sysexit */
  715. ret = X86_BR_SYSRET;
  716. break;
  717. case 0x80 ... 0x8f: /* conditional */
  718. ret = X86_BR_JCC;
  719. break;
  720. default:
  721. ret = X86_BR_NONE;
  722. }
  723. break;
  724. case 0x70 ... 0x7f: /* conditional */
  725. ret = X86_BR_JCC;
  726. break;
  727. case 0xc2: /* near ret */
  728. case 0xc3: /* near ret */
  729. case 0xca: /* far ret */
  730. case 0xcb: /* far ret */
  731. ret = X86_BR_RET;
  732. break;
  733. case 0xcf: /* iret */
  734. ret = X86_BR_IRET;
  735. break;
  736. case 0xcc ... 0xce: /* int */
  737. ret = X86_BR_INT;
  738. break;
  739. case 0xe8: /* call near rel */
  740. insn_get_immediate(&insn);
  741. if (insn.immediate1.value == 0) {
  742. /* zero length call */
  743. ret = X86_BR_ZERO_CALL;
  744. break;
  745. }
  746. case 0x9a: /* call far absolute */
  747. ret = X86_BR_CALL;
  748. break;
  749. case 0xe0 ... 0xe3: /* loop jmp */
  750. ret = X86_BR_JCC;
  751. break;
  752. case 0xe9 ... 0xeb: /* jmp */
  753. ret = X86_BR_JMP;
  754. break;
  755. case 0xff: /* call near absolute, call far absolute ind */
  756. insn_get_modrm(&insn);
  757. ext = (insn.modrm.bytes[0] >> 3) & 0x7;
  758. switch (ext) {
  759. case 2: /* near ind call */
  760. case 3: /* far ind call */
  761. ret = X86_BR_IND_CALL;
  762. break;
  763. case 4:
  764. case 5:
  765. ret = X86_BR_IND_JMP;
  766. break;
  767. }
  768. break;
  769. default:
  770. ret = X86_BR_NONE;
  771. }
  772. /*
  773. * interrupts, traps, faults (and thus ring transition) may
  774. * occur on any instructions. Thus, to classify them correctly,
  775. * we need to first look at the from and to priv levels. If they
  776. * are different and to is in the kernel, then it indicates
  777. * a ring transition. If the from instruction is not a ring
  778. * transition instr (syscall, systenter, int), then it means
  779. * it was a irq, trap or fault.
  780. *
  781. * we have no way of detecting kernel to kernel faults.
  782. */
  783. if (from_plm == X86_BR_USER && to_plm == X86_BR_KERNEL
  784. && ret != X86_BR_SYSCALL && ret != X86_BR_INT)
  785. ret = X86_BR_IRQ;
  786. /*
  787. * branch priv level determined by target as
  788. * is done by HW when LBR_SELECT is implemented
  789. */
  790. if (ret != X86_BR_NONE)
  791. ret |= to_plm;
  792. return ret;
  793. }
  794. /*
  795. * implement actual branch filter based on user demand.
  796. * Hardware may not exactly satisfy that request, thus
  797. * we need to inspect opcodes. Mismatched branches are
  798. * discarded. Therefore, the number of branches returned
  799. * in PERF_SAMPLE_BRANCH_STACK sample may vary.
  800. */
  801. static void
  802. intel_pmu_lbr_filter(struct cpu_hw_events *cpuc)
  803. {
  804. u64 from, to;
  805. int br_sel = cpuc->br_sel;
  806. int i, j, type;
  807. bool compress = false;
  808. /* if sampling all branches, then nothing to filter */
  809. if ((br_sel & X86_BR_ALL) == X86_BR_ALL)
  810. return;
  811. for (i = 0; i < cpuc->lbr_stack.nr; i++) {
  812. from = cpuc->lbr_entries[i].from;
  813. to = cpuc->lbr_entries[i].to;
  814. type = branch_type(from, to, cpuc->lbr_entries[i].abort);
  815. if (type != X86_BR_NONE && (br_sel & X86_BR_ANYTX)) {
  816. if (cpuc->lbr_entries[i].in_tx)
  817. type |= X86_BR_IN_TX;
  818. else
  819. type |= X86_BR_NO_TX;
  820. }
  821. /* if type does not correspond, then discard */
  822. if (type == X86_BR_NONE || (br_sel & type) != type) {
  823. cpuc->lbr_entries[i].from = 0;
  824. compress = true;
  825. }
  826. }
  827. if (!compress)
  828. return;
  829. /* remove all entries with from=0 */
  830. for (i = 0; i < cpuc->lbr_stack.nr; ) {
  831. if (!cpuc->lbr_entries[i].from) {
  832. j = i;
  833. while (++j < cpuc->lbr_stack.nr)
  834. cpuc->lbr_entries[j-1] = cpuc->lbr_entries[j];
  835. cpuc->lbr_stack.nr--;
  836. if (!cpuc->lbr_entries[i].from)
  837. continue;
  838. }
  839. i++;
  840. }
  841. }
  842. /*
  843. * Map interface branch filters onto LBR filters
  844. */
  845. static const int nhm_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  846. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  847. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  848. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  849. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  850. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_REL_JMP
  851. | LBR_IND_JMP | LBR_FAR,
  852. /*
  853. * NHM/WSM erratum: must include REL_JMP+IND_JMP to get CALL branches
  854. */
  855. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] =
  856. LBR_REL_CALL | LBR_IND_CALL | LBR_REL_JMP | LBR_IND_JMP | LBR_FAR,
  857. /*
  858. * NHM/WSM erratum: must include IND_JMP to capture IND_CALL
  859. */
  860. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL | LBR_IND_JMP,
  861. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  862. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  863. };
  864. static const int snb_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  865. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  866. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  867. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  868. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  869. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
  870. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  871. | LBR_FAR,
  872. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
  873. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  874. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  875. [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
  876. };
  877. static const int hsw_lbr_sel_map[PERF_SAMPLE_BRANCH_MAX_SHIFT] = {
  878. [PERF_SAMPLE_BRANCH_ANY_SHIFT] = LBR_ANY,
  879. [PERF_SAMPLE_BRANCH_USER_SHIFT] = LBR_USER,
  880. [PERF_SAMPLE_BRANCH_KERNEL_SHIFT] = LBR_KERNEL,
  881. [PERF_SAMPLE_BRANCH_HV_SHIFT] = LBR_IGN,
  882. [PERF_SAMPLE_BRANCH_ANY_RETURN_SHIFT] = LBR_RETURN | LBR_FAR,
  883. [PERF_SAMPLE_BRANCH_ANY_CALL_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  884. | LBR_FAR,
  885. [PERF_SAMPLE_BRANCH_IND_CALL_SHIFT] = LBR_IND_CALL,
  886. [PERF_SAMPLE_BRANCH_COND_SHIFT] = LBR_JCC,
  887. [PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] = LBR_REL_CALL | LBR_IND_CALL
  888. | LBR_RETURN | LBR_CALL_STACK,
  889. [PERF_SAMPLE_BRANCH_IND_JUMP_SHIFT] = LBR_IND_JMP,
  890. [PERF_SAMPLE_BRANCH_CALL_SHIFT] = LBR_REL_CALL,
  891. };
  892. /* core */
  893. void __init intel_pmu_lbr_init_core(void)
  894. {
  895. x86_pmu.lbr_nr = 4;
  896. x86_pmu.lbr_tos = MSR_LBR_TOS;
  897. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  898. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  899. /*
  900. * SW branch filter usage:
  901. * - compensate for lack of HW filter
  902. */
  903. }
  904. /* nehalem/westmere */
  905. void __init intel_pmu_lbr_init_nhm(void)
  906. {
  907. x86_pmu.lbr_nr = 16;
  908. x86_pmu.lbr_tos = MSR_LBR_TOS;
  909. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  910. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  911. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  912. x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
  913. /*
  914. * SW branch filter usage:
  915. * - workaround LBR_SEL errata (see above)
  916. * - support syscall, sysret capture.
  917. * That requires LBR_FAR but that means far
  918. * jmp need to be filtered out
  919. */
  920. }
  921. /* sandy bridge */
  922. void __init intel_pmu_lbr_init_snb(void)
  923. {
  924. x86_pmu.lbr_nr = 16;
  925. x86_pmu.lbr_tos = MSR_LBR_TOS;
  926. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  927. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  928. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  929. x86_pmu.lbr_sel_map = snb_lbr_sel_map;
  930. /*
  931. * SW branch filter usage:
  932. * - support syscall, sysret capture.
  933. * That requires LBR_FAR but that means far
  934. * jmp need to be filtered out
  935. */
  936. }
  937. /* haswell */
  938. void intel_pmu_lbr_init_hsw(void)
  939. {
  940. x86_pmu.lbr_nr = 16;
  941. x86_pmu.lbr_tos = MSR_LBR_TOS;
  942. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  943. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  944. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  945. x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
  946. if (lbr_from_signext_quirk_needed())
  947. static_branch_enable(&lbr_from_quirk_key);
  948. }
  949. /* skylake */
  950. __init void intel_pmu_lbr_init_skl(void)
  951. {
  952. x86_pmu.lbr_nr = 32;
  953. x86_pmu.lbr_tos = MSR_LBR_TOS;
  954. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  955. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  956. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  957. x86_pmu.lbr_sel_map = hsw_lbr_sel_map;
  958. /*
  959. * SW branch filter usage:
  960. * - support syscall, sysret capture.
  961. * That requires LBR_FAR but that means far
  962. * jmp need to be filtered out
  963. */
  964. }
  965. /* atom */
  966. void __init intel_pmu_lbr_init_atom(void)
  967. {
  968. /*
  969. * only models starting at stepping 10 seems
  970. * to have an operational LBR which can freeze
  971. * on PMU interrupt
  972. */
  973. if (boot_cpu_data.x86_model == 28
  974. && boot_cpu_data.x86_mask < 10) {
  975. pr_cont("LBR disabled due to erratum");
  976. return;
  977. }
  978. x86_pmu.lbr_nr = 8;
  979. x86_pmu.lbr_tos = MSR_LBR_TOS;
  980. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  981. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  982. /*
  983. * SW branch filter usage:
  984. * - compensate for lack of HW filter
  985. */
  986. }
  987. /* slm */
  988. void __init intel_pmu_lbr_init_slm(void)
  989. {
  990. x86_pmu.lbr_nr = 8;
  991. x86_pmu.lbr_tos = MSR_LBR_TOS;
  992. x86_pmu.lbr_from = MSR_LBR_CORE_FROM;
  993. x86_pmu.lbr_to = MSR_LBR_CORE_TO;
  994. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  995. x86_pmu.lbr_sel_map = nhm_lbr_sel_map;
  996. /*
  997. * SW branch filter usage:
  998. * - compensate for lack of HW filter
  999. */
  1000. pr_cont("8-deep LBR, ");
  1001. }
  1002. /* Knights Landing */
  1003. void intel_pmu_lbr_init_knl(void)
  1004. {
  1005. x86_pmu.lbr_nr = 8;
  1006. x86_pmu.lbr_tos = MSR_LBR_TOS;
  1007. x86_pmu.lbr_from = MSR_LBR_NHM_FROM;
  1008. x86_pmu.lbr_to = MSR_LBR_NHM_TO;
  1009. x86_pmu.lbr_sel_mask = LBR_SEL_MASK;
  1010. x86_pmu.lbr_sel_map = snb_lbr_sel_map;
  1011. }