perf_event_intel_lbr.c 4.5 KB

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