perf_event_intel_lbr.c 26 KB

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