perf_event_intel_ds.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #include <linux/bitops.h>
  2. #include <linux/types.h>
  3. #include <linux/slab.h>
  4. #include <asm/perf_event.h>
  5. #include <asm/insn.h>
  6. #include "perf_event.h"
  7. /* The size of a BTS record in bytes: */
  8. #define BTS_RECORD_SIZE 24
  9. #define BTS_BUFFER_SIZE (PAGE_SIZE << 4)
  10. #define PEBS_BUFFER_SIZE PAGE_SIZE
  11. /*
  12. * pebs_record_32 for p4 and core not supported
  13. struct pebs_record_32 {
  14. u32 flags, ip;
  15. u32 ax, bc, cx, dx;
  16. u32 si, di, bp, sp;
  17. };
  18. */
  19. struct pebs_record_core {
  20. u64 flags, ip;
  21. u64 ax, bx, cx, dx;
  22. u64 si, di, bp, sp;
  23. u64 r8, r9, r10, r11;
  24. u64 r12, r13, r14, r15;
  25. };
  26. struct pebs_record_nhm {
  27. u64 flags, ip;
  28. u64 ax, bx, cx, dx;
  29. u64 si, di, bp, sp;
  30. u64 r8, r9, r10, r11;
  31. u64 r12, r13, r14, r15;
  32. u64 status, dla, dse, lat;
  33. };
  34. void init_debug_store_on_cpu(int cpu)
  35. {
  36. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  37. if (!ds)
  38. return;
  39. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  40. (u32)((u64)(unsigned long)ds),
  41. (u32)((u64)(unsigned long)ds >> 32));
  42. }
  43. void fini_debug_store_on_cpu(int cpu)
  44. {
  45. if (!per_cpu(cpu_hw_events, cpu).ds)
  46. return;
  47. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  48. }
  49. static int alloc_pebs_buffer(int cpu)
  50. {
  51. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  52. int node = cpu_to_node(cpu);
  53. int max, thresh = 1; /* always use a single PEBS record */
  54. void *buffer;
  55. if (!x86_pmu.pebs)
  56. return 0;
  57. buffer = kmalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  58. if (unlikely(!buffer))
  59. return -ENOMEM;
  60. max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;
  61. ds->pebs_buffer_base = (u64)(unsigned long)buffer;
  62. ds->pebs_index = ds->pebs_buffer_base;
  63. ds->pebs_absolute_maximum = ds->pebs_buffer_base +
  64. max * x86_pmu.pebs_record_size;
  65. ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
  66. thresh * x86_pmu.pebs_record_size;
  67. return 0;
  68. }
  69. static void release_pebs_buffer(int cpu)
  70. {
  71. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  72. if (!ds || !x86_pmu.pebs)
  73. return;
  74. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  75. ds->pebs_buffer_base = 0;
  76. }
  77. static int alloc_bts_buffer(int cpu)
  78. {
  79. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  80. int node = cpu_to_node(cpu);
  81. int max, thresh;
  82. void *buffer;
  83. if (!x86_pmu.bts)
  84. return 0;
  85. buffer = kmalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_ZERO, node);
  86. if (unlikely(!buffer))
  87. return -ENOMEM;
  88. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  89. thresh = max / 16;
  90. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  91. ds->bts_index = ds->bts_buffer_base;
  92. ds->bts_absolute_maximum = ds->bts_buffer_base +
  93. max * BTS_RECORD_SIZE;
  94. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  95. thresh * BTS_RECORD_SIZE;
  96. return 0;
  97. }
  98. static void release_bts_buffer(int cpu)
  99. {
  100. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  101. if (!ds || !x86_pmu.bts)
  102. return;
  103. kfree((void *)(unsigned long)ds->bts_buffer_base);
  104. ds->bts_buffer_base = 0;
  105. }
  106. static int alloc_ds_buffer(int cpu)
  107. {
  108. int node = cpu_to_node(cpu);
  109. struct debug_store *ds;
  110. ds = kmalloc_node(sizeof(*ds), GFP_KERNEL | __GFP_ZERO, node);
  111. if (unlikely(!ds))
  112. return -ENOMEM;
  113. per_cpu(cpu_hw_events, cpu).ds = ds;
  114. return 0;
  115. }
  116. static void release_ds_buffer(int cpu)
  117. {
  118. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  119. if (!ds)
  120. return;
  121. per_cpu(cpu_hw_events, cpu).ds = NULL;
  122. kfree(ds);
  123. }
  124. void release_ds_buffers(void)
  125. {
  126. int cpu;
  127. if (!x86_pmu.bts && !x86_pmu.pebs)
  128. return;
  129. get_online_cpus();
  130. for_each_online_cpu(cpu)
  131. fini_debug_store_on_cpu(cpu);
  132. for_each_possible_cpu(cpu) {
  133. release_pebs_buffer(cpu);
  134. release_bts_buffer(cpu);
  135. release_ds_buffer(cpu);
  136. }
  137. put_online_cpus();
  138. }
  139. void reserve_ds_buffers(void)
  140. {
  141. int bts_err = 0, pebs_err = 0;
  142. int cpu;
  143. x86_pmu.bts_active = 0;
  144. x86_pmu.pebs_active = 0;
  145. if (!x86_pmu.bts && !x86_pmu.pebs)
  146. return;
  147. if (!x86_pmu.bts)
  148. bts_err = 1;
  149. if (!x86_pmu.pebs)
  150. pebs_err = 1;
  151. get_online_cpus();
  152. for_each_possible_cpu(cpu) {
  153. if (alloc_ds_buffer(cpu)) {
  154. bts_err = 1;
  155. pebs_err = 1;
  156. }
  157. if (!bts_err && alloc_bts_buffer(cpu))
  158. bts_err = 1;
  159. if (!pebs_err && alloc_pebs_buffer(cpu))
  160. pebs_err = 1;
  161. if (bts_err && pebs_err)
  162. break;
  163. }
  164. if (bts_err) {
  165. for_each_possible_cpu(cpu)
  166. release_bts_buffer(cpu);
  167. }
  168. if (pebs_err) {
  169. for_each_possible_cpu(cpu)
  170. release_pebs_buffer(cpu);
  171. }
  172. if (bts_err && pebs_err) {
  173. for_each_possible_cpu(cpu)
  174. release_ds_buffer(cpu);
  175. } else {
  176. if (x86_pmu.bts && !bts_err)
  177. x86_pmu.bts_active = 1;
  178. if (x86_pmu.pebs && !pebs_err)
  179. x86_pmu.pebs_active = 1;
  180. for_each_online_cpu(cpu)
  181. init_debug_store_on_cpu(cpu);
  182. }
  183. put_online_cpus();
  184. }
  185. /*
  186. * BTS
  187. */
  188. struct event_constraint bts_constraint =
  189. EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0);
  190. void intel_pmu_enable_bts(u64 config)
  191. {
  192. unsigned long debugctlmsr;
  193. debugctlmsr = get_debugctlmsr();
  194. debugctlmsr |= DEBUGCTLMSR_TR;
  195. debugctlmsr |= DEBUGCTLMSR_BTS;
  196. debugctlmsr |= DEBUGCTLMSR_BTINT;
  197. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  198. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  199. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  200. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  201. update_debugctlmsr(debugctlmsr);
  202. }
  203. void intel_pmu_disable_bts(void)
  204. {
  205. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  206. unsigned long debugctlmsr;
  207. if (!cpuc->ds)
  208. return;
  209. debugctlmsr = get_debugctlmsr();
  210. debugctlmsr &=
  211. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  212. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  213. update_debugctlmsr(debugctlmsr);
  214. }
  215. int intel_pmu_drain_bts_buffer(void)
  216. {
  217. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  218. struct debug_store *ds = cpuc->ds;
  219. struct bts_record {
  220. u64 from;
  221. u64 to;
  222. u64 flags;
  223. };
  224. struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
  225. struct bts_record *at, *top;
  226. struct perf_output_handle handle;
  227. struct perf_event_header header;
  228. struct perf_sample_data data;
  229. struct pt_regs regs;
  230. if (!event)
  231. return 0;
  232. if (!x86_pmu.bts_active)
  233. return 0;
  234. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  235. top = (struct bts_record *)(unsigned long)ds->bts_index;
  236. if (top <= at)
  237. return 0;
  238. ds->bts_index = ds->bts_buffer_base;
  239. perf_sample_data_init(&data, 0, event->hw.last_period);
  240. regs.ip = 0;
  241. /*
  242. * Prepare a generic sample, i.e. fill in the invariant fields.
  243. * We will overwrite the from and to address before we output
  244. * the sample.
  245. */
  246. perf_prepare_sample(&header, &data, event, &regs);
  247. if (perf_output_begin(&handle, event, header.size * (top - at)))
  248. return 1;
  249. for (; at < top; at++) {
  250. data.ip = at->from;
  251. data.addr = at->to;
  252. perf_output_sample(&handle, &header, &data, event);
  253. }
  254. perf_output_end(&handle);
  255. /* There's new data available. */
  256. event->hw.interrupts++;
  257. event->pending_kill = POLL_IN;
  258. return 1;
  259. }
  260. /*
  261. * PEBS
  262. */
  263. struct event_constraint intel_core2_pebs_event_constraints[] = {
  264. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  265. INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  266. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  267. INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  268. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  269. EVENT_CONSTRAINT_END
  270. };
  271. struct event_constraint intel_atom_pebs_event_constraints[] = {
  272. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  273. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  274. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  275. EVENT_CONSTRAINT_END
  276. };
  277. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  278. INTEL_EVENT_CONSTRAINT(0x0b, 0xf), /* MEM_INST_RETIRED.* */
  279. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  280. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  281. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  282. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  283. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  284. INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  285. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  286. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  287. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  288. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  289. EVENT_CONSTRAINT_END
  290. };
  291. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  292. INTEL_EVENT_CONSTRAINT(0x0b, 0xf), /* MEM_INST_RETIRED.* */
  293. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  294. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  295. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  296. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  297. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  298. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  299. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  300. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  301. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  302. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  303. EVENT_CONSTRAINT_END
  304. };
  305. struct event_constraint intel_snb_pebs_event_constraints[] = {
  306. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  307. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  308. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  309. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  310. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  311. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.* */
  312. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  313. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  314. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  315. INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
  316. EVENT_CONSTRAINT_END
  317. };
  318. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  319. {
  320. struct event_constraint *c;
  321. if (!event->attr.precise_ip)
  322. return NULL;
  323. if (x86_pmu.pebs_constraints) {
  324. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  325. if ((event->hw.config & c->cmask) == c->code)
  326. return c;
  327. }
  328. }
  329. return &emptyconstraint;
  330. }
  331. void intel_pmu_pebs_enable(struct perf_event *event)
  332. {
  333. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  334. struct hw_perf_event *hwc = &event->hw;
  335. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  336. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  337. }
  338. void intel_pmu_pebs_disable(struct perf_event *event)
  339. {
  340. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  341. struct hw_perf_event *hwc = &event->hw;
  342. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  343. if (cpuc->enabled)
  344. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  345. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  346. }
  347. void intel_pmu_pebs_enable_all(void)
  348. {
  349. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  350. if (cpuc->pebs_enabled)
  351. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  352. }
  353. void intel_pmu_pebs_disable_all(void)
  354. {
  355. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  356. if (cpuc->pebs_enabled)
  357. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  358. }
  359. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  360. {
  361. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  362. unsigned long from = cpuc->lbr_entries[0].from;
  363. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  364. unsigned long ip = regs->ip;
  365. int is_64bit = 0;
  366. /*
  367. * We don't need to fixup if the PEBS assist is fault like
  368. */
  369. if (!x86_pmu.intel_cap.pebs_trap)
  370. return 1;
  371. /*
  372. * No LBR entry, no basic block, no rewinding
  373. */
  374. if (!cpuc->lbr_stack.nr || !from || !to)
  375. return 0;
  376. /*
  377. * Basic blocks should never cross user/kernel boundaries
  378. */
  379. if (kernel_ip(ip) != kernel_ip(to))
  380. return 0;
  381. /*
  382. * unsigned math, either ip is before the start (impossible) or
  383. * the basic block is larger than 1 page (sanity)
  384. */
  385. if ((ip - to) > PAGE_SIZE)
  386. return 0;
  387. /*
  388. * We sampled a branch insn, rewind using the LBR stack
  389. */
  390. if (ip == to) {
  391. regs->ip = from;
  392. return 1;
  393. }
  394. do {
  395. struct insn insn;
  396. u8 buf[MAX_INSN_SIZE];
  397. void *kaddr;
  398. old_to = to;
  399. if (!kernel_ip(ip)) {
  400. int bytes, size = MAX_INSN_SIZE;
  401. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  402. if (bytes != size)
  403. return 0;
  404. kaddr = buf;
  405. } else
  406. kaddr = (void *)to;
  407. #ifdef CONFIG_X86_64
  408. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  409. #endif
  410. insn_init(&insn, kaddr, is_64bit);
  411. insn_get_length(&insn);
  412. to += insn.length;
  413. } while (to < ip);
  414. if (to == ip) {
  415. regs->ip = old_to;
  416. return 1;
  417. }
  418. /*
  419. * Even though we decoded the basic block, the instruction stream
  420. * never matched the given IP, either the TO or the IP got corrupted.
  421. */
  422. return 0;
  423. }
  424. static void __intel_pmu_pebs_event(struct perf_event *event,
  425. struct pt_regs *iregs, void *__pebs)
  426. {
  427. /*
  428. * We cast to pebs_record_core since that is a subset of
  429. * both formats and we don't use the other fields in this
  430. * routine.
  431. */
  432. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  433. struct pebs_record_core *pebs = __pebs;
  434. struct perf_sample_data data;
  435. struct pt_regs regs;
  436. if (!intel_pmu_save_and_restart(event))
  437. return;
  438. perf_sample_data_init(&data, 0, event->hw.last_period);
  439. /*
  440. * We use the interrupt regs as a base because the PEBS record
  441. * does not contain a full regs set, specifically it seems to
  442. * lack segment descriptors, which get used by things like
  443. * user_mode().
  444. *
  445. * In the simple case fix up only the IP and BP,SP regs, for
  446. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  447. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  448. */
  449. regs = *iregs;
  450. regs.ip = pebs->ip;
  451. regs.bp = pebs->bp;
  452. regs.sp = pebs->sp;
  453. if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  454. regs.flags |= PERF_EFLAGS_EXACT;
  455. else
  456. regs.flags &= ~PERF_EFLAGS_EXACT;
  457. if (has_branch_stack(event))
  458. data.br_stack = &cpuc->lbr_stack;
  459. if (perf_event_overflow(event, &data, &regs))
  460. x86_pmu_stop(event, 0);
  461. }
  462. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  463. {
  464. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  465. struct debug_store *ds = cpuc->ds;
  466. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  467. struct pebs_record_core *at, *top;
  468. int n;
  469. if (!x86_pmu.pebs_active)
  470. return;
  471. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  472. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  473. /*
  474. * Whatever else happens, drain the thing
  475. */
  476. ds->pebs_index = ds->pebs_buffer_base;
  477. if (!test_bit(0, cpuc->active_mask))
  478. return;
  479. WARN_ON_ONCE(!event);
  480. if (!event->attr.precise_ip)
  481. return;
  482. n = top - at;
  483. if (n <= 0)
  484. return;
  485. /*
  486. * Should not happen, we program the threshold at 1 and do not
  487. * set a reset value.
  488. */
  489. WARN_ON_ONCE(n > 1);
  490. at += n - 1;
  491. __intel_pmu_pebs_event(event, iregs, at);
  492. }
  493. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  494. {
  495. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  496. struct debug_store *ds = cpuc->ds;
  497. struct pebs_record_nhm *at, *top;
  498. struct perf_event *event = NULL;
  499. u64 status = 0;
  500. int bit, n;
  501. if (!x86_pmu.pebs_active)
  502. return;
  503. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  504. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  505. ds->pebs_index = ds->pebs_buffer_base;
  506. n = top - at;
  507. if (n <= 0)
  508. return;
  509. /*
  510. * Should not happen, we program the threshold at 1 and do not
  511. * set a reset value.
  512. */
  513. WARN_ON_ONCE(n > MAX_PEBS_EVENTS);
  514. for ( ; at < top; at++) {
  515. for_each_set_bit(bit, (unsigned long *)&at->status, MAX_PEBS_EVENTS) {
  516. event = cpuc->events[bit];
  517. if (!test_bit(bit, cpuc->active_mask))
  518. continue;
  519. WARN_ON_ONCE(!event);
  520. if (!event->attr.precise_ip)
  521. continue;
  522. if (__test_and_set_bit(bit, (unsigned long *)&status))
  523. continue;
  524. break;
  525. }
  526. if (!event || bit >= MAX_PEBS_EVENTS)
  527. continue;
  528. __intel_pmu_pebs_event(event, iregs, at);
  529. }
  530. }
  531. /*
  532. * BTS, PEBS probe and setup
  533. */
  534. void intel_ds_init(void)
  535. {
  536. /*
  537. * No support for 32bit formats
  538. */
  539. if (!boot_cpu_has(X86_FEATURE_DTES64))
  540. return;
  541. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  542. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  543. if (x86_pmu.pebs) {
  544. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  545. int format = x86_pmu.intel_cap.pebs_format;
  546. switch (format) {
  547. case 0:
  548. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  549. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  550. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  551. break;
  552. case 1:
  553. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  554. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  555. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  556. break;
  557. default:
  558. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  559. x86_pmu.pebs = 0;
  560. }
  561. }
  562. }