perf_event_intel_ds.c 17 KB

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