perf_event_intel_lbr.c 27 KB

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