perf_event_intel_ds.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 << INTEL_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[INTEL_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_ivb_pebs_event_constraints[] = {
  319. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  320. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  321. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  322. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  323. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  324. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.* */
  325. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  326. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  327. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  328. INTEL_EVENT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  329. EVENT_CONSTRAINT_END
  330. };
  331. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  332. {
  333. struct event_constraint *c;
  334. if (!event->attr.precise_ip)
  335. return NULL;
  336. if (x86_pmu.pebs_constraints) {
  337. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  338. if ((event->hw.config & c->cmask) == c->code)
  339. return c;
  340. }
  341. }
  342. return &emptyconstraint;
  343. }
  344. void intel_pmu_pebs_enable(struct perf_event *event)
  345. {
  346. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  347. struct hw_perf_event *hwc = &event->hw;
  348. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  349. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  350. }
  351. void intel_pmu_pebs_disable(struct perf_event *event)
  352. {
  353. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  354. struct hw_perf_event *hwc = &event->hw;
  355. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  356. if (cpuc->enabled)
  357. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  358. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  359. }
  360. void intel_pmu_pebs_enable_all(void)
  361. {
  362. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  363. if (cpuc->pebs_enabled)
  364. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  365. }
  366. void intel_pmu_pebs_disable_all(void)
  367. {
  368. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  369. if (cpuc->pebs_enabled)
  370. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  371. }
  372. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  373. {
  374. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  375. unsigned long from = cpuc->lbr_entries[0].from;
  376. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  377. unsigned long ip = regs->ip;
  378. int is_64bit = 0;
  379. /*
  380. * We don't need to fixup if the PEBS assist is fault like
  381. */
  382. if (!x86_pmu.intel_cap.pebs_trap)
  383. return 1;
  384. /*
  385. * No LBR entry, no basic block, no rewinding
  386. */
  387. if (!cpuc->lbr_stack.nr || !from || !to)
  388. return 0;
  389. /*
  390. * Basic blocks should never cross user/kernel boundaries
  391. */
  392. if (kernel_ip(ip) != kernel_ip(to))
  393. return 0;
  394. /*
  395. * unsigned math, either ip is before the start (impossible) or
  396. * the basic block is larger than 1 page (sanity)
  397. */
  398. if ((ip - to) > PAGE_SIZE)
  399. return 0;
  400. /*
  401. * We sampled a branch insn, rewind using the LBR stack
  402. */
  403. if (ip == to) {
  404. set_linear_ip(regs, from);
  405. return 1;
  406. }
  407. do {
  408. struct insn insn;
  409. u8 buf[MAX_INSN_SIZE];
  410. void *kaddr;
  411. old_to = to;
  412. if (!kernel_ip(ip)) {
  413. int bytes, size = MAX_INSN_SIZE;
  414. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  415. if (bytes != size)
  416. return 0;
  417. kaddr = buf;
  418. } else
  419. kaddr = (void *)to;
  420. #ifdef CONFIG_X86_64
  421. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  422. #endif
  423. insn_init(&insn, kaddr, is_64bit);
  424. insn_get_length(&insn);
  425. to += insn.length;
  426. } while (to < ip);
  427. if (to == ip) {
  428. set_linear_ip(regs, old_to);
  429. return 1;
  430. }
  431. /*
  432. * Even though we decoded the basic block, the instruction stream
  433. * never matched the given IP, either the TO or the IP got corrupted.
  434. */
  435. return 0;
  436. }
  437. static void __intel_pmu_pebs_event(struct perf_event *event,
  438. struct pt_regs *iregs, void *__pebs)
  439. {
  440. /*
  441. * We cast to pebs_record_core since that is a subset of
  442. * both formats and we don't use the other fields in this
  443. * routine.
  444. */
  445. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  446. struct pebs_record_core *pebs = __pebs;
  447. struct perf_sample_data data;
  448. struct pt_regs regs;
  449. if (!intel_pmu_save_and_restart(event))
  450. return;
  451. perf_sample_data_init(&data, 0, event->hw.last_period);
  452. /*
  453. * We use the interrupt regs as a base because the PEBS record
  454. * does not contain a full regs set, specifically it seems to
  455. * lack segment descriptors, which get used by things like
  456. * user_mode().
  457. *
  458. * In the simple case fix up only the IP and BP,SP regs, for
  459. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  460. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  461. */
  462. regs = *iregs;
  463. regs.flags = pebs->flags;
  464. set_linear_ip(&regs, pebs->ip);
  465. regs.bp = pebs->bp;
  466. regs.sp = pebs->sp;
  467. if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  468. regs.flags |= PERF_EFLAGS_EXACT;
  469. else
  470. regs.flags &= ~PERF_EFLAGS_EXACT;
  471. if (has_branch_stack(event))
  472. data.br_stack = &cpuc->lbr_stack;
  473. if (perf_event_overflow(event, &data, &regs))
  474. x86_pmu_stop(event, 0);
  475. }
  476. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  477. {
  478. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  479. struct debug_store *ds = cpuc->ds;
  480. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  481. struct pebs_record_core *at, *top;
  482. int n;
  483. if (!x86_pmu.pebs_active)
  484. return;
  485. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  486. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  487. /*
  488. * Whatever else happens, drain the thing
  489. */
  490. ds->pebs_index = ds->pebs_buffer_base;
  491. if (!test_bit(0, cpuc->active_mask))
  492. return;
  493. WARN_ON_ONCE(!event);
  494. if (!event->attr.precise_ip)
  495. return;
  496. n = top - at;
  497. if (n <= 0)
  498. return;
  499. /*
  500. * Should not happen, we program the threshold at 1 and do not
  501. * set a reset value.
  502. */
  503. WARN_ONCE(n > 1, "bad leftover pebs %d\n", n);
  504. at += n - 1;
  505. __intel_pmu_pebs_event(event, iregs, at);
  506. }
  507. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  508. {
  509. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  510. struct debug_store *ds = cpuc->ds;
  511. struct pebs_record_nhm *at, *top;
  512. struct perf_event *event = NULL;
  513. u64 status = 0;
  514. int bit, n;
  515. if (!x86_pmu.pebs_active)
  516. return;
  517. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  518. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  519. ds->pebs_index = ds->pebs_buffer_base;
  520. n = top - at;
  521. if (n <= 0)
  522. return;
  523. /*
  524. * Should not happen, we program the threshold at 1 and do not
  525. * set a reset value.
  526. */
  527. WARN_ONCE(n > x86_pmu.max_pebs_events, "Unexpected number of pebs records %d\n", n);
  528. for ( ; at < top; at++) {
  529. for_each_set_bit(bit, (unsigned long *)&at->status, x86_pmu.max_pebs_events) {
  530. event = cpuc->events[bit];
  531. if (!test_bit(bit, cpuc->active_mask))
  532. continue;
  533. WARN_ON_ONCE(!event);
  534. if (!event->attr.precise_ip)
  535. continue;
  536. if (__test_and_set_bit(bit, (unsigned long *)&status))
  537. continue;
  538. break;
  539. }
  540. if (!event || bit >= x86_pmu.max_pebs_events)
  541. continue;
  542. __intel_pmu_pebs_event(event, iregs, at);
  543. }
  544. }
  545. /*
  546. * BTS, PEBS probe and setup
  547. */
  548. void intel_ds_init(void)
  549. {
  550. /*
  551. * No support for 32bit formats
  552. */
  553. if (!boot_cpu_has(X86_FEATURE_DTES64))
  554. return;
  555. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  556. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  557. if (x86_pmu.pebs) {
  558. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  559. int format = x86_pmu.intel_cap.pebs_format;
  560. switch (format) {
  561. case 0:
  562. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  563. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  564. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  565. break;
  566. case 1:
  567. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  568. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  569. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  570. break;
  571. default:
  572. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  573. x86_pmu.pebs = 0;
  574. }
  575. }
  576. }