perf_event_intel_ds.c 17 KB

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