perf_event_intel_lbr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <linux/perf_event.h>
  2. #include <linux/types.h>
  3. #include <asm/perf_event.h>
  4. #include <asm/msr.h>
  5. #include "perf_event.h"
  6. enum {
  7. LBR_FORMAT_32 = 0x00,
  8. LBR_FORMAT_LIP = 0x01,
  9. LBR_FORMAT_EIP = 0x02,
  10. LBR_FORMAT_EIP_FLAGS = 0x03,
  11. };
  12. /*
  13. * We only support LBR implementations that have FREEZE_LBRS_ON_PMI
  14. * otherwise it becomes near impossible to get a reliable stack.
  15. */
  16. static void __intel_pmu_lbr_enable(void)
  17. {
  18. u64 debugctl;
  19. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  20. debugctl |= (DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
  21. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  22. }
  23. static void __intel_pmu_lbr_disable(void)
  24. {
  25. u64 debugctl;
  26. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  27. debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI);
  28. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
  29. }
  30. static void intel_pmu_lbr_reset_32(void)
  31. {
  32. int i;
  33. for (i = 0; i < x86_pmu.lbr_nr; i++)
  34. wrmsrl(x86_pmu.lbr_from + i, 0);
  35. }
  36. static void intel_pmu_lbr_reset_64(void)
  37. {
  38. int i;
  39. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  40. wrmsrl(x86_pmu.lbr_from + i, 0);
  41. wrmsrl(x86_pmu.lbr_to + i, 0);
  42. }
  43. }
  44. void intel_pmu_lbr_reset(void)
  45. {
  46. if (!x86_pmu.lbr_nr)
  47. return;
  48. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
  49. intel_pmu_lbr_reset_32();
  50. else
  51. intel_pmu_lbr_reset_64();
  52. }
  53. void intel_pmu_lbr_enable(struct perf_event *event)
  54. {
  55. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  56. if (!x86_pmu.lbr_nr)
  57. return;
  58. WARN_ON_ONCE(cpuc->enabled);
  59. /*
  60. * Reset the LBR stack if we changed task context to
  61. * avoid data leaks.
  62. */
  63. if (event->ctx->task && cpuc->lbr_context != event->ctx) {
  64. intel_pmu_lbr_reset();
  65. cpuc->lbr_context = event->ctx;
  66. }
  67. cpuc->lbr_users++;
  68. }
  69. void intel_pmu_lbr_disable(struct perf_event *event)
  70. {
  71. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  72. if (!x86_pmu.lbr_nr)
  73. return;
  74. cpuc->lbr_users--;
  75. WARN_ON_ONCE(cpuc->lbr_users < 0);
  76. if (cpuc->enabled && !cpuc->lbr_users)
  77. __intel_pmu_lbr_disable();
  78. }
  79. void intel_pmu_lbr_enable_all(void)
  80. {
  81. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  82. if (cpuc->lbr_users)
  83. __intel_pmu_lbr_enable();
  84. }
  85. void intel_pmu_lbr_disable_all(void)
  86. {
  87. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  88. if (cpuc->lbr_users)
  89. __intel_pmu_lbr_disable();
  90. }
  91. static inline u64 intel_pmu_lbr_tos(void)
  92. {
  93. u64 tos;
  94. rdmsrl(x86_pmu.lbr_tos, tos);
  95. return tos;
  96. }
  97. static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc)
  98. {
  99. unsigned long mask = x86_pmu.lbr_nr - 1;
  100. u64 tos = intel_pmu_lbr_tos();
  101. int i;
  102. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  103. unsigned long lbr_idx = (tos - i) & mask;
  104. union {
  105. struct {
  106. u32 from;
  107. u32 to;
  108. };
  109. u64 lbr;
  110. } msr_lastbranch;
  111. rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr);
  112. cpuc->lbr_entries[i].from = msr_lastbranch.from;
  113. cpuc->lbr_entries[i].to = msr_lastbranch.to;
  114. cpuc->lbr_entries[i].flags = 0;
  115. }
  116. cpuc->lbr_stack.nr = i;
  117. }
  118. #define LBR_FROM_FLAG_MISPRED (1ULL << 63)
  119. /*
  120. * Due to lack of segmentation in Linux the effective address (offset)
  121. * is the same as the linear address, allowing us to merge the LIP and EIP
  122. * LBR formats.
  123. */
  124. static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc)
  125. {
  126. unsigned long mask = x86_pmu.lbr_nr - 1;
  127. int lbr_format = x86_pmu.intel_cap.lbr_format;
  128. u64 tos = intel_pmu_lbr_tos();
  129. int i;
  130. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  131. unsigned long lbr_idx = (tos - i) & mask;
  132. u64 from, to, flags = 0;
  133. rdmsrl(x86_pmu.lbr_from + lbr_idx, from);
  134. rdmsrl(x86_pmu.lbr_to + lbr_idx, to);
  135. if (lbr_format == LBR_FORMAT_EIP_FLAGS) {
  136. flags = !!(from & LBR_FROM_FLAG_MISPRED);
  137. from = (u64)((((s64)from) << 1) >> 1);
  138. }
  139. cpuc->lbr_entries[i].from = from;
  140. cpuc->lbr_entries[i].to = to;
  141. cpuc->lbr_entries[i].flags = flags;
  142. }
  143. cpuc->lbr_stack.nr = i;
  144. }
  145. void intel_pmu_lbr_read(void)
  146. {
  147. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  148. if (!cpuc->lbr_users)
  149. return;
  150. if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32)
  151. intel_pmu_lbr_read_32(cpuc);
  152. else
  153. intel_pmu_lbr_read_64(cpuc);
  154. }
  155. void intel_pmu_lbr_init_core(void)
  156. {
  157. x86_pmu.lbr_nr = 4;
  158. x86_pmu.lbr_tos = 0x01c9;
  159. x86_pmu.lbr_from = 0x40;
  160. x86_pmu.lbr_to = 0x60;
  161. }
  162. void intel_pmu_lbr_init_nhm(void)
  163. {
  164. x86_pmu.lbr_nr = 16;
  165. x86_pmu.lbr_tos = 0x01c9;
  166. x86_pmu.lbr_from = 0x680;
  167. x86_pmu.lbr_to = 0x6c0;
  168. }
  169. void intel_pmu_lbr_init_atom(void)
  170. {
  171. x86_pmu.lbr_nr = 8;
  172. x86_pmu.lbr_tos = 0x01c9;
  173. x86_pmu.lbr_from = 0x40;
  174. x86_pmu.lbr_to = 0x60;
  175. }