perf_event_intel_ds.c 18 KB

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