perf_event_intel_ds.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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);
  240. data.period = event->hw.last_period;
  241. regs.ip = 0;
  242. /*
  243. * Prepare a generic sample, i.e. fill in the invariant fields.
  244. * We will overwrite the from and to address before we output
  245. * the sample.
  246. */
  247. perf_prepare_sample(&header, &data, event, &regs);
  248. if (perf_output_begin(&handle, event, header.size * (top - at)))
  249. return 1;
  250. for (; at < top; at++) {
  251. data.ip = at->from;
  252. data.addr = at->to;
  253. perf_output_sample(&handle, &header, &data, event);
  254. }
  255. perf_output_end(&handle);
  256. /* There's new data available. */
  257. event->hw.interrupts++;
  258. event->pending_kill = POLL_IN;
  259. return 1;
  260. }
  261. /*
  262. * PEBS
  263. */
  264. struct event_constraint intel_core2_pebs_event_constraints[] = {
  265. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  266. INTEL_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  267. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  268. INTEL_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  269. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  270. EVENT_CONSTRAINT_END
  271. };
  272. struct event_constraint intel_atom_pebs_event_constraints[] = {
  273. INTEL_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  274. INTEL_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  275. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  276. EVENT_CONSTRAINT_END
  277. };
  278. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  279. INTEL_EVENT_CONSTRAINT(0x0b, 0xf), /* MEM_INST_RETIRED.* */
  280. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  281. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  282. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  283. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  284. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  285. INTEL_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  286. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  287. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  288. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  289. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  290. EVENT_CONSTRAINT_END
  291. };
  292. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  293. INTEL_EVENT_CONSTRAINT(0x0b, 0xf), /* MEM_INST_RETIRED.* */
  294. INTEL_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  295. INTEL_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  296. INTEL_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  297. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  298. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  299. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  300. INTEL_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  301. INTEL_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  302. INTEL_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  303. INTEL_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  304. EVENT_CONSTRAINT_END
  305. };
  306. struct event_constraint intel_snb_pebs_event_constraints[] = {
  307. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  308. INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  309. INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
  310. INTEL_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  311. INTEL_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  312. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.* */
  313. INTEL_UEVENT_CONSTRAINT(0x11d0, 0xf), /* MEM_UOP_RETIRED.STLB_MISS_LOADS */
  314. INTEL_UEVENT_CONSTRAINT(0x12d0, 0xf), /* MEM_UOP_RETIRED.STLB_MISS_STORES */
  315. INTEL_UEVENT_CONSTRAINT(0x21d0, 0xf), /* MEM_UOP_RETIRED.LOCK_LOADS */
  316. INTEL_UEVENT_CONSTRAINT(0x22d0, 0xf), /* MEM_UOP_RETIRED.LOCK_STORES */
  317. INTEL_UEVENT_CONSTRAINT(0x41d0, 0xf), /* MEM_UOP_RETIRED.SPLIT_LOADS */
  318. INTEL_UEVENT_CONSTRAINT(0x42d0, 0xf), /* MEM_UOP_RETIRED.SPLIT_STORES */
  319. INTEL_UEVENT_CONSTRAINT(0x81d0, 0xf), /* MEM_UOP_RETIRED.ANY_LOADS */
  320. INTEL_UEVENT_CONSTRAINT(0x82d0, 0xf), /* MEM_UOP_RETIRED.ANY_STORES */
  321. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  322. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  323. INTEL_UEVENT_CONSTRAINT(0x02d4, 0xf), /* MEM_LOAD_UOPS_MISC_RETIRED.LLC_MISS */
  324. EVENT_CONSTRAINT_END
  325. };
  326. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  327. {
  328. struct event_constraint *c;
  329. if (!event->attr.precise_ip)
  330. return NULL;
  331. if (x86_pmu.pebs_constraints) {
  332. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  333. if ((event->hw.config & c->cmask) == c->code)
  334. return c;
  335. }
  336. }
  337. return &emptyconstraint;
  338. }
  339. void intel_pmu_pebs_enable(struct perf_event *event)
  340. {
  341. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  342. struct hw_perf_event *hwc = &event->hw;
  343. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  344. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  345. }
  346. void intel_pmu_pebs_disable(struct perf_event *event)
  347. {
  348. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  349. struct hw_perf_event *hwc = &event->hw;
  350. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  351. if (cpuc->enabled)
  352. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  353. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  354. }
  355. void intel_pmu_pebs_enable_all(void)
  356. {
  357. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  358. if (cpuc->pebs_enabled)
  359. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  360. }
  361. void intel_pmu_pebs_disable_all(void)
  362. {
  363. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  364. if (cpuc->pebs_enabled)
  365. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  366. }
  367. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  368. {
  369. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  370. unsigned long from = cpuc->lbr_entries[0].from;
  371. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  372. unsigned long ip = regs->ip;
  373. int is_64bit = 0;
  374. /*
  375. * We don't need to fixup if the PEBS assist is fault like
  376. */
  377. if (!x86_pmu.intel_cap.pebs_trap)
  378. return 1;
  379. /*
  380. * No LBR entry, no basic block, no rewinding
  381. */
  382. if (!cpuc->lbr_stack.nr || !from || !to)
  383. return 0;
  384. /*
  385. * Basic blocks should never cross user/kernel boundaries
  386. */
  387. if (kernel_ip(ip) != kernel_ip(to))
  388. return 0;
  389. /*
  390. * unsigned math, either ip is before the start (impossible) or
  391. * the basic block is larger than 1 page (sanity)
  392. */
  393. if ((ip - to) > PAGE_SIZE)
  394. return 0;
  395. /*
  396. * We sampled a branch insn, rewind using the LBR stack
  397. */
  398. if (ip == to) {
  399. regs->ip = from;
  400. return 1;
  401. }
  402. do {
  403. struct insn insn;
  404. u8 buf[MAX_INSN_SIZE];
  405. void *kaddr;
  406. old_to = to;
  407. if (!kernel_ip(ip)) {
  408. int bytes, size = MAX_INSN_SIZE;
  409. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  410. if (bytes != size)
  411. return 0;
  412. kaddr = buf;
  413. } else
  414. kaddr = (void *)to;
  415. #ifdef CONFIG_X86_64
  416. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  417. #endif
  418. insn_init(&insn, kaddr, is_64bit);
  419. insn_get_length(&insn);
  420. to += insn.length;
  421. } while (to < ip);
  422. if (to == ip) {
  423. regs->ip = old_to;
  424. return 1;
  425. }
  426. /*
  427. * Even though we decoded the basic block, the instruction stream
  428. * never matched the given IP, either the TO or the IP got corrupted.
  429. */
  430. return 0;
  431. }
  432. static void __intel_pmu_pebs_event(struct perf_event *event,
  433. struct pt_regs *iregs, void *__pebs)
  434. {
  435. /*
  436. * We cast to pebs_record_core since that is a subset of
  437. * both formats and we don't use the other fields in this
  438. * routine.
  439. */
  440. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  441. struct pebs_record_core *pebs = __pebs;
  442. struct perf_sample_data data;
  443. struct pt_regs regs;
  444. if (!intel_pmu_save_and_restart(event))
  445. return;
  446. perf_sample_data_init(&data, 0);
  447. data.period = event->hw.last_period;
  448. /*
  449. * We use the interrupt regs as a base because the PEBS record
  450. * does not contain a full regs set, specifically it seems to
  451. * lack segment descriptors, which get used by things like
  452. * user_mode().
  453. *
  454. * In the simple case fix up only the IP and BP,SP regs, for
  455. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  456. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  457. */
  458. regs = *iregs;
  459. regs.ip = pebs->ip;
  460. regs.bp = pebs->bp;
  461. regs.sp = pebs->sp;
  462. if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(&regs))
  463. regs.flags |= PERF_EFLAGS_EXACT;
  464. else
  465. regs.flags &= ~PERF_EFLAGS_EXACT;
  466. if (has_branch_stack(event))
  467. data.br_stack = &cpuc->lbr_stack;
  468. if (perf_event_overflow(event, &data, &regs))
  469. x86_pmu_stop(event, 0);
  470. }
  471. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  472. {
  473. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  474. struct debug_store *ds = cpuc->ds;
  475. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  476. struct pebs_record_core *at, *top;
  477. int n;
  478. if (!x86_pmu.pebs_active)
  479. return;
  480. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  481. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  482. /*
  483. * Whatever else happens, drain the thing
  484. */
  485. ds->pebs_index = ds->pebs_buffer_base;
  486. if (!test_bit(0, cpuc->active_mask))
  487. return;
  488. WARN_ON_ONCE(!event);
  489. if (!event->attr.precise_ip)
  490. return;
  491. n = top - at;
  492. if (n <= 0)
  493. return;
  494. /*
  495. * Should not happen, we program the threshold at 1 and do not
  496. * set a reset value.
  497. */
  498. WARN_ON_ONCE(n > 1);
  499. at += n - 1;
  500. __intel_pmu_pebs_event(event, iregs, at);
  501. }
  502. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  503. {
  504. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  505. struct debug_store *ds = cpuc->ds;
  506. struct pebs_record_nhm *at, *top;
  507. struct perf_event *event = NULL;
  508. u64 status = 0;
  509. int bit, n;
  510. if (!x86_pmu.pebs_active)
  511. return;
  512. at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  513. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  514. ds->pebs_index = ds->pebs_buffer_base;
  515. n = top - at;
  516. if (n <= 0)
  517. return;
  518. /*
  519. * Should not happen, we program the threshold at 1 and do not
  520. * set a reset value.
  521. */
  522. WARN_ON_ONCE(n > MAX_PEBS_EVENTS);
  523. for ( ; at < top; at++) {
  524. for_each_set_bit(bit, (unsigned long *)&at->status, MAX_PEBS_EVENTS) {
  525. event = cpuc->events[bit];
  526. if (!test_bit(bit, cpuc->active_mask))
  527. continue;
  528. WARN_ON_ONCE(!event);
  529. if (!event->attr.precise_ip)
  530. continue;
  531. if (__test_and_set_bit(bit, (unsigned long *)&status))
  532. continue;
  533. break;
  534. }
  535. if (!event || bit >= MAX_PEBS_EVENTS)
  536. continue;
  537. __intel_pmu_pebs_event(event, iregs, at);
  538. }
  539. }
  540. /*
  541. * BTS, PEBS probe and setup
  542. */
  543. void intel_ds_init(void)
  544. {
  545. /*
  546. * No support for 32bit formats
  547. */
  548. if (!boot_cpu_has(X86_FEATURE_DTES64))
  549. return;
  550. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  551. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  552. if (x86_pmu.pebs) {
  553. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  554. int format = x86_pmu.intel_cap.pebs_format;
  555. switch (format) {
  556. case 0:
  557. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  558. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  559. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  560. break;
  561. case 1:
  562. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  563. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  564. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  565. break;
  566. default:
  567. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  568. x86_pmu.pebs = 0;
  569. }
  570. }
  571. }
  572. void perf_restore_debug_store(void)
  573. {
  574. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  575. if (!x86_pmu.bts && !x86_pmu.pebs)
  576. return;
  577. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  578. }