perf_event_intel_ds.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 << 4)
  11. #define PEBS_FIXUP_SIZE PAGE_SIZE
  12. /*
  13. * pebs_record_32 for p4 and core not supported
  14. struct pebs_record_32 {
  15. u32 flags, ip;
  16. u32 ax, bc, cx, dx;
  17. u32 si, di, bp, sp;
  18. };
  19. */
  20. union intel_x86_pebs_dse {
  21. u64 val;
  22. struct {
  23. unsigned int ld_dse:4;
  24. unsigned int ld_stlb_miss:1;
  25. unsigned int ld_locked:1;
  26. unsigned int ld_reserved:26;
  27. };
  28. struct {
  29. unsigned int st_l1d_hit:1;
  30. unsigned int st_reserved1:3;
  31. unsigned int st_stlb_miss:1;
  32. unsigned int st_locked:1;
  33. unsigned int st_reserved2:26;
  34. };
  35. };
  36. /*
  37. * Map PEBS Load Latency Data Source encodings to generic
  38. * memory data source information
  39. */
  40. #define P(a, b) PERF_MEM_S(a, b)
  41. #define OP_LH (P(OP, LOAD) | P(LVL, HIT))
  42. #define SNOOP_NONE_MISS (P(SNOOP, NONE) | P(SNOOP, MISS))
  43. static const u64 pebs_data_source[] = {
  44. P(OP, LOAD) | P(LVL, MISS) | P(LVL, L3) | P(SNOOP, NA),/* 0x00:ukn L3 */
  45. OP_LH | P(LVL, L1) | P(SNOOP, NONE), /* 0x01: L1 local */
  46. OP_LH | P(LVL, LFB) | P(SNOOP, NONE), /* 0x02: LFB hit */
  47. OP_LH | P(LVL, L2) | P(SNOOP, NONE), /* 0x03: L2 hit */
  48. OP_LH | P(LVL, L3) | P(SNOOP, NONE), /* 0x04: L3 hit */
  49. OP_LH | P(LVL, L3) | P(SNOOP, MISS), /* 0x05: L3 hit, snoop miss */
  50. OP_LH | P(LVL, L3) | P(SNOOP, HIT), /* 0x06: L3 hit, snoop hit */
  51. OP_LH | P(LVL, L3) | P(SNOOP, HITM), /* 0x07: L3 hit, snoop hitm */
  52. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HIT), /* 0x08: L3 miss snoop hit */
  53. OP_LH | P(LVL, REM_CCE1) | P(SNOOP, HITM), /* 0x09: L3 miss snoop hitm*/
  54. OP_LH | P(LVL, LOC_RAM) | P(SNOOP, HIT), /* 0x0a: L3 miss, shared */
  55. OP_LH | P(LVL, REM_RAM1) | P(SNOOP, HIT), /* 0x0b: L3 miss, shared */
  56. OP_LH | P(LVL, LOC_RAM) | SNOOP_NONE_MISS,/* 0x0c: L3 miss, excl */
  57. OP_LH | P(LVL, REM_RAM1) | SNOOP_NONE_MISS,/* 0x0d: L3 miss, excl */
  58. OP_LH | P(LVL, IO) | P(SNOOP, NONE), /* 0x0e: I/O */
  59. OP_LH | P(LVL, UNC) | P(SNOOP, NONE), /* 0x0f: uncached */
  60. };
  61. static u64 precise_store_data(u64 status)
  62. {
  63. union intel_x86_pebs_dse dse;
  64. u64 val = P(OP, STORE) | P(SNOOP, NA) | P(LVL, L1) | P(TLB, L2);
  65. dse.val = status;
  66. /*
  67. * bit 4: TLB access
  68. * 1 = stored missed 2nd level TLB
  69. *
  70. * so it either hit the walker or the OS
  71. * otherwise hit 2nd level TLB
  72. */
  73. if (dse.st_stlb_miss)
  74. val |= P(TLB, MISS);
  75. else
  76. val |= P(TLB, HIT);
  77. /*
  78. * bit 0: hit L1 data cache
  79. * if not set, then all we know is that
  80. * it missed L1D
  81. */
  82. if (dse.st_l1d_hit)
  83. val |= P(LVL, HIT);
  84. else
  85. val |= P(LVL, MISS);
  86. /*
  87. * bit 5: Locked prefix
  88. */
  89. if (dse.st_locked)
  90. val |= P(LOCK, LOCKED);
  91. return val;
  92. }
  93. static u64 precise_datala_hsw(struct perf_event *event, u64 status)
  94. {
  95. union perf_mem_data_src dse;
  96. dse.val = PERF_MEM_NA;
  97. if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW)
  98. dse.mem_op = PERF_MEM_OP_STORE;
  99. else if (event->hw.flags & PERF_X86_EVENT_PEBS_LD_HSW)
  100. dse.mem_op = PERF_MEM_OP_LOAD;
  101. /*
  102. * L1 info only valid for following events:
  103. *
  104. * MEM_UOPS_RETIRED.STLB_MISS_STORES
  105. * MEM_UOPS_RETIRED.LOCK_STORES
  106. * MEM_UOPS_RETIRED.SPLIT_STORES
  107. * MEM_UOPS_RETIRED.ALL_STORES
  108. */
  109. if (event->hw.flags & PERF_X86_EVENT_PEBS_ST_HSW) {
  110. if (status & 1)
  111. dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
  112. else
  113. dse.mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_MISS;
  114. }
  115. return dse.val;
  116. }
  117. static u64 load_latency_data(u64 status)
  118. {
  119. union intel_x86_pebs_dse dse;
  120. u64 val;
  121. int model = boot_cpu_data.x86_model;
  122. int fam = boot_cpu_data.x86;
  123. dse.val = status;
  124. /*
  125. * use the mapping table for bit 0-3
  126. */
  127. val = pebs_data_source[dse.ld_dse];
  128. /*
  129. * Nehalem models do not support TLB, Lock infos
  130. */
  131. if (fam == 0x6 && (model == 26 || model == 30
  132. || model == 31 || model == 46)) {
  133. val |= P(TLB, NA) | P(LOCK, NA);
  134. return val;
  135. }
  136. /*
  137. * bit 4: TLB access
  138. * 0 = did not miss 2nd level TLB
  139. * 1 = missed 2nd level TLB
  140. */
  141. if (dse.ld_stlb_miss)
  142. val |= P(TLB, MISS) | P(TLB, L2);
  143. else
  144. val |= P(TLB, HIT) | P(TLB, L1) | P(TLB, L2);
  145. /*
  146. * bit 5: locked prefix
  147. */
  148. if (dse.ld_locked)
  149. val |= P(LOCK, LOCKED);
  150. return val;
  151. }
  152. struct pebs_record_core {
  153. u64 flags, ip;
  154. u64 ax, bx, cx, dx;
  155. u64 si, di, bp, sp;
  156. u64 r8, r9, r10, r11;
  157. u64 r12, r13, r14, r15;
  158. };
  159. struct pebs_record_nhm {
  160. u64 flags, ip;
  161. u64 ax, bx, cx, dx;
  162. u64 si, di, bp, sp;
  163. u64 r8, r9, r10, r11;
  164. u64 r12, r13, r14, r15;
  165. u64 status, dla, dse, lat;
  166. };
  167. /*
  168. * Same as pebs_record_nhm, with two additional fields.
  169. */
  170. struct pebs_record_hsw {
  171. u64 flags, ip;
  172. u64 ax, bx, cx, dx;
  173. u64 si, di, bp, sp;
  174. u64 r8, r9, r10, r11;
  175. u64 r12, r13, r14, r15;
  176. u64 status, dla, dse, lat;
  177. u64 real_ip, tsx_tuning;
  178. };
  179. union hsw_tsx_tuning {
  180. struct {
  181. u32 cycles_last_block : 32,
  182. hle_abort : 1,
  183. rtm_abort : 1,
  184. instruction_abort : 1,
  185. non_instruction_abort : 1,
  186. retry : 1,
  187. data_conflict : 1,
  188. capacity_writes : 1,
  189. capacity_reads : 1;
  190. };
  191. u64 value;
  192. };
  193. #define PEBS_HSW_TSX_FLAGS 0xff00000000ULL
  194. /* Same as HSW, plus TSC */
  195. struct pebs_record_skl {
  196. u64 flags, ip;
  197. u64 ax, bx, cx, dx;
  198. u64 si, di, bp, sp;
  199. u64 r8, r9, r10, r11;
  200. u64 r12, r13, r14, r15;
  201. u64 status, dla, dse, lat;
  202. u64 real_ip, tsx_tuning;
  203. u64 tsc;
  204. };
  205. void init_debug_store_on_cpu(int cpu)
  206. {
  207. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  208. if (!ds)
  209. return;
  210. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
  211. (u32)((u64)(unsigned long)ds),
  212. (u32)((u64)(unsigned long)ds >> 32));
  213. }
  214. void fini_debug_store_on_cpu(int cpu)
  215. {
  216. if (!per_cpu(cpu_hw_events, cpu).ds)
  217. return;
  218. wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
  219. }
  220. static DEFINE_PER_CPU(void *, insn_buffer);
  221. static int alloc_pebs_buffer(int cpu)
  222. {
  223. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  224. int node = cpu_to_node(cpu);
  225. int max;
  226. void *buffer, *ibuffer;
  227. if (!x86_pmu.pebs)
  228. return 0;
  229. buffer = kzalloc_node(PEBS_BUFFER_SIZE, GFP_KERNEL, node);
  230. if (unlikely(!buffer))
  231. return -ENOMEM;
  232. /*
  233. * HSW+ already provides us the eventing ip; no need to allocate this
  234. * buffer then.
  235. */
  236. if (x86_pmu.intel_cap.pebs_format < 2) {
  237. ibuffer = kzalloc_node(PEBS_FIXUP_SIZE, GFP_KERNEL, node);
  238. if (!ibuffer) {
  239. kfree(buffer);
  240. return -ENOMEM;
  241. }
  242. per_cpu(insn_buffer, cpu) = ibuffer;
  243. }
  244. max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size;
  245. ds->pebs_buffer_base = (u64)(unsigned long)buffer;
  246. ds->pebs_index = ds->pebs_buffer_base;
  247. ds->pebs_absolute_maximum = ds->pebs_buffer_base +
  248. max * x86_pmu.pebs_record_size;
  249. return 0;
  250. }
  251. static void release_pebs_buffer(int cpu)
  252. {
  253. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  254. if (!ds || !x86_pmu.pebs)
  255. return;
  256. kfree(per_cpu(insn_buffer, cpu));
  257. per_cpu(insn_buffer, cpu) = NULL;
  258. kfree((void *)(unsigned long)ds->pebs_buffer_base);
  259. ds->pebs_buffer_base = 0;
  260. }
  261. static int alloc_bts_buffer(int cpu)
  262. {
  263. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  264. int node = cpu_to_node(cpu);
  265. int max, thresh;
  266. void *buffer;
  267. if (!x86_pmu.bts)
  268. return 0;
  269. buffer = kzalloc_node(BTS_BUFFER_SIZE, GFP_KERNEL | __GFP_NOWARN, node);
  270. if (unlikely(!buffer)) {
  271. WARN_ONCE(1, "%s: BTS buffer allocation failure\n", __func__);
  272. return -ENOMEM;
  273. }
  274. max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE;
  275. thresh = max / 16;
  276. ds->bts_buffer_base = (u64)(unsigned long)buffer;
  277. ds->bts_index = ds->bts_buffer_base;
  278. ds->bts_absolute_maximum = ds->bts_buffer_base +
  279. max * BTS_RECORD_SIZE;
  280. ds->bts_interrupt_threshold = ds->bts_absolute_maximum -
  281. thresh * BTS_RECORD_SIZE;
  282. return 0;
  283. }
  284. static void release_bts_buffer(int cpu)
  285. {
  286. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  287. if (!ds || !x86_pmu.bts)
  288. return;
  289. kfree((void *)(unsigned long)ds->bts_buffer_base);
  290. ds->bts_buffer_base = 0;
  291. }
  292. static int alloc_ds_buffer(int cpu)
  293. {
  294. int node = cpu_to_node(cpu);
  295. struct debug_store *ds;
  296. ds = kzalloc_node(sizeof(*ds), GFP_KERNEL, node);
  297. if (unlikely(!ds))
  298. return -ENOMEM;
  299. per_cpu(cpu_hw_events, cpu).ds = ds;
  300. return 0;
  301. }
  302. static void release_ds_buffer(int cpu)
  303. {
  304. struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
  305. if (!ds)
  306. return;
  307. per_cpu(cpu_hw_events, cpu).ds = NULL;
  308. kfree(ds);
  309. }
  310. void release_ds_buffers(void)
  311. {
  312. int cpu;
  313. if (!x86_pmu.bts && !x86_pmu.pebs)
  314. return;
  315. get_online_cpus();
  316. for_each_online_cpu(cpu)
  317. fini_debug_store_on_cpu(cpu);
  318. for_each_possible_cpu(cpu) {
  319. release_pebs_buffer(cpu);
  320. release_bts_buffer(cpu);
  321. release_ds_buffer(cpu);
  322. }
  323. put_online_cpus();
  324. }
  325. void reserve_ds_buffers(void)
  326. {
  327. int bts_err = 0, pebs_err = 0;
  328. int cpu;
  329. x86_pmu.bts_active = 0;
  330. x86_pmu.pebs_active = 0;
  331. if (!x86_pmu.bts && !x86_pmu.pebs)
  332. return;
  333. if (!x86_pmu.bts)
  334. bts_err = 1;
  335. if (!x86_pmu.pebs)
  336. pebs_err = 1;
  337. get_online_cpus();
  338. for_each_possible_cpu(cpu) {
  339. if (alloc_ds_buffer(cpu)) {
  340. bts_err = 1;
  341. pebs_err = 1;
  342. }
  343. if (!bts_err && alloc_bts_buffer(cpu))
  344. bts_err = 1;
  345. if (!pebs_err && alloc_pebs_buffer(cpu))
  346. pebs_err = 1;
  347. if (bts_err && pebs_err)
  348. break;
  349. }
  350. if (bts_err) {
  351. for_each_possible_cpu(cpu)
  352. release_bts_buffer(cpu);
  353. }
  354. if (pebs_err) {
  355. for_each_possible_cpu(cpu)
  356. release_pebs_buffer(cpu);
  357. }
  358. if (bts_err && pebs_err) {
  359. for_each_possible_cpu(cpu)
  360. release_ds_buffer(cpu);
  361. } else {
  362. if (x86_pmu.bts && !bts_err)
  363. x86_pmu.bts_active = 1;
  364. if (x86_pmu.pebs && !pebs_err)
  365. x86_pmu.pebs_active = 1;
  366. for_each_online_cpu(cpu)
  367. init_debug_store_on_cpu(cpu);
  368. }
  369. put_online_cpus();
  370. }
  371. /*
  372. * BTS
  373. */
  374. struct event_constraint bts_constraint =
  375. EVENT_CONSTRAINT(0, 1ULL << INTEL_PMC_IDX_FIXED_BTS, 0);
  376. void intel_pmu_enable_bts(u64 config)
  377. {
  378. unsigned long debugctlmsr;
  379. debugctlmsr = get_debugctlmsr();
  380. debugctlmsr |= DEBUGCTLMSR_TR;
  381. debugctlmsr |= DEBUGCTLMSR_BTS;
  382. if (config & ARCH_PERFMON_EVENTSEL_INT)
  383. debugctlmsr |= DEBUGCTLMSR_BTINT;
  384. if (!(config & ARCH_PERFMON_EVENTSEL_OS))
  385. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS;
  386. if (!(config & ARCH_PERFMON_EVENTSEL_USR))
  387. debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR;
  388. update_debugctlmsr(debugctlmsr);
  389. }
  390. void intel_pmu_disable_bts(void)
  391. {
  392. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  393. unsigned long debugctlmsr;
  394. if (!cpuc->ds)
  395. return;
  396. debugctlmsr = get_debugctlmsr();
  397. debugctlmsr &=
  398. ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT |
  399. DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR);
  400. update_debugctlmsr(debugctlmsr);
  401. }
  402. int intel_pmu_drain_bts_buffer(void)
  403. {
  404. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  405. struct debug_store *ds = cpuc->ds;
  406. struct bts_record {
  407. u64 from;
  408. u64 to;
  409. u64 flags;
  410. };
  411. struct perf_event *event = cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  412. struct bts_record *at, *base, *top;
  413. struct perf_output_handle handle;
  414. struct perf_event_header header;
  415. struct perf_sample_data data;
  416. unsigned long skip = 0;
  417. struct pt_regs regs;
  418. if (!event)
  419. return 0;
  420. if (!x86_pmu.bts_active)
  421. return 0;
  422. base = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  423. top = (struct bts_record *)(unsigned long)ds->bts_index;
  424. if (top <= base)
  425. return 0;
  426. memset(&regs, 0, sizeof(regs));
  427. ds->bts_index = ds->bts_buffer_base;
  428. perf_sample_data_init(&data, 0, event->hw.last_period);
  429. /*
  430. * BTS leaks kernel addresses in branches across the cpl boundary,
  431. * such as traps or system calls, so unless the user is asking for
  432. * kernel tracing (and right now it's not possible), we'd need to
  433. * filter them out. But first we need to count how many of those we
  434. * have in the current batch. This is an extra O(n) pass, however,
  435. * it's much faster than the other one especially considering that
  436. * n <= 2560 (BTS_BUFFER_SIZE / BTS_RECORD_SIZE * 15/16; see the
  437. * alloc_bts_buffer()).
  438. */
  439. for (at = base; at < top; at++) {
  440. /*
  441. * Note that right now *this* BTS code only works if
  442. * attr::exclude_kernel is set, but let's keep this extra
  443. * check here in case that changes.
  444. */
  445. if (event->attr.exclude_kernel &&
  446. (kernel_ip(at->from) || kernel_ip(at->to)))
  447. skip++;
  448. }
  449. /*
  450. * Prepare a generic sample, i.e. fill in the invariant fields.
  451. * We will overwrite the from and to address before we output
  452. * the sample.
  453. */
  454. perf_prepare_sample(&header, &data, event, &regs);
  455. if (perf_output_begin(&handle, event, header.size *
  456. (top - base - skip)))
  457. return 1;
  458. for (at = base; at < top; at++) {
  459. /* Filter out any records that contain kernel addresses. */
  460. if (event->attr.exclude_kernel &&
  461. (kernel_ip(at->from) || kernel_ip(at->to)))
  462. continue;
  463. data.ip = at->from;
  464. data.addr = at->to;
  465. perf_output_sample(&handle, &header, &data, event);
  466. }
  467. perf_output_end(&handle);
  468. /* There's new data available. */
  469. event->hw.interrupts++;
  470. event->pending_kill = POLL_IN;
  471. return 1;
  472. }
  473. static inline void intel_pmu_drain_pebs_buffer(void)
  474. {
  475. struct pt_regs regs;
  476. x86_pmu.drain_pebs(&regs);
  477. }
  478. void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
  479. {
  480. if (!sched_in)
  481. intel_pmu_drain_pebs_buffer();
  482. }
  483. /*
  484. * PEBS
  485. */
  486. struct event_constraint intel_core2_pebs_event_constraints[] = {
  487. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  488. INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  489. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  490. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  491. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  492. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  493. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  494. EVENT_CONSTRAINT_END
  495. };
  496. struct event_constraint intel_atom_pebs_event_constraints[] = {
  497. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  498. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  499. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  500. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  501. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  502. /* Allow all events as PEBS with no flags */
  503. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
  504. EVENT_CONSTRAINT_END
  505. };
  506. struct event_constraint intel_slm_pebs_event_constraints[] = {
  507. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  508. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
  509. /* Allow all events as PEBS with no flags */
  510. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
  511. EVENT_CONSTRAINT_END
  512. };
  513. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  514. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  515. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  516. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  517. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  518. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  519. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  520. INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  521. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  522. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  523. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  524. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  525. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  526. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  527. EVENT_CONSTRAINT_END
  528. };
  529. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  530. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  531. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  532. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  533. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  534. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  535. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  536. INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  537. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  538. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  539. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  540. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  541. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  542. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  543. EVENT_CONSTRAINT_END
  544. };
  545. struct event_constraint intel_snb_pebs_event_constraints[] = {
  546. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  547. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  548. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  549. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  550. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  551. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  552. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  553. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  554. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  555. /* Allow all events as PEBS with no flags */
  556. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  557. EVENT_CONSTRAINT_END
  558. };
  559. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  560. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  561. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  562. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  563. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  564. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  565. /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
  566. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
  567. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  568. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  569. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  570. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  571. /* Allow all events as PEBS with no flags */
  572. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  573. EVENT_CONSTRAINT_END
  574. };
  575. struct event_constraint intel_hsw_pebs_event_constraints[] = {
  576. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  577. INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
  578. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  579. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  580. /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
  581. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
  582. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  583. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
  584. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
  585. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
  586. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
  587. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
  588. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
  589. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
  590. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  591. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
  592. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
  593. /* Allow all events as PEBS with no flags */
  594. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  595. EVENT_CONSTRAINT_END
  596. };
  597. struct event_constraint intel_skl_pebs_event_constraints[] = {
  598. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
  599. /* INST_RETIRED.PREC_DIST, inv=1, cmask=16 (cycles:ppp). */
  600. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c0, 0x2),
  601. /* INST_RETIRED.TOTAL_CYCLES_PS (inv=1, cmask=16) (cycles:p). */
  602. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  603. INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
  604. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
  605. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
  606. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
  607. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
  608. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
  609. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
  610. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
  611. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
  612. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
  613. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
  614. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
  615. /* Allow all events as PEBS with no flags */
  616. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  617. EVENT_CONSTRAINT_END
  618. };
  619. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  620. {
  621. struct event_constraint *c;
  622. if (!event->attr.precise_ip)
  623. return NULL;
  624. if (x86_pmu.pebs_constraints) {
  625. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  626. if ((event->hw.config & c->cmask) == c->code) {
  627. event->hw.flags |= c->flags;
  628. return c;
  629. }
  630. }
  631. }
  632. return &emptyconstraint;
  633. }
  634. static inline bool pebs_is_enabled(struct cpu_hw_events *cpuc)
  635. {
  636. return (cpuc->pebs_enabled & ((1ULL << MAX_PEBS_EVENTS) - 1));
  637. }
  638. void intel_pmu_pebs_enable(struct perf_event *event)
  639. {
  640. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  641. struct hw_perf_event *hwc = &event->hw;
  642. struct debug_store *ds = cpuc->ds;
  643. bool first_pebs;
  644. u64 threshold;
  645. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  646. first_pebs = !pebs_is_enabled(cpuc);
  647. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  648. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  649. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  650. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  651. cpuc->pebs_enabled |= 1ULL << 63;
  652. /*
  653. * When the event is constrained enough we can use a larger
  654. * threshold and run the event with less frequent PMI.
  655. */
  656. if (hwc->flags & PERF_X86_EVENT_FREERUNNING) {
  657. threshold = ds->pebs_absolute_maximum -
  658. x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
  659. if (first_pebs)
  660. perf_sched_cb_inc(event->ctx->pmu);
  661. } else {
  662. threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
  663. /*
  664. * If not all events can use larger buffer,
  665. * roll back to threshold = 1
  666. */
  667. if (!first_pebs &&
  668. (ds->pebs_interrupt_threshold > threshold))
  669. perf_sched_cb_dec(event->ctx->pmu);
  670. }
  671. /* Use auto-reload if possible to save a MSR write in the PMI */
  672. if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
  673. ds->pebs_event_reset[hwc->idx] =
  674. (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
  675. }
  676. if (first_pebs || ds->pebs_interrupt_threshold > threshold)
  677. ds->pebs_interrupt_threshold = threshold;
  678. }
  679. void intel_pmu_pebs_disable(struct perf_event *event)
  680. {
  681. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  682. struct hw_perf_event *hwc = &event->hw;
  683. struct debug_store *ds = cpuc->ds;
  684. bool large_pebs = ds->pebs_interrupt_threshold >
  685. ds->pebs_buffer_base + x86_pmu.pebs_record_size;
  686. if (large_pebs)
  687. intel_pmu_drain_pebs_buffer();
  688. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  689. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  690. cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
  691. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  692. cpuc->pebs_enabled &= ~(1ULL << 63);
  693. if (large_pebs && !pebs_is_enabled(cpuc))
  694. perf_sched_cb_dec(event->ctx->pmu);
  695. if (cpuc->enabled)
  696. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  697. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  698. }
  699. void intel_pmu_pebs_enable_all(void)
  700. {
  701. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  702. if (cpuc->pebs_enabled)
  703. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  704. }
  705. void intel_pmu_pebs_disable_all(void)
  706. {
  707. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  708. if (cpuc->pebs_enabled)
  709. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  710. }
  711. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  712. {
  713. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  714. unsigned long from = cpuc->lbr_entries[0].from;
  715. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  716. unsigned long ip = regs->ip;
  717. int is_64bit = 0;
  718. void *kaddr;
  719. int size;
  720. /*
  721. * We don't need to fixup if the PEBS assist is fault like
  722. */
  723. if (!x86_pmu.intel_cap.pebs_trap)
  724. return 1;
  725. /*
  726. * No LBR entry, no basic block, no rewinding
  727. */
  728. if (!cpuc->lbr_stack.nr || !from || !to)
  729. return 0;
  730. /*
  731. * Basic blocks should never cross user/kernel boundaries
  732. */
  733. if (kernel_ip(ip) != kernel_ip(to))
  734. return 0;
  735. /*
  736. * unsigned math, either ip is before the start (impossible) or
  737. * the basic block is larger than 1 page (sanity)
  738. */
  739. if ((ip - to) > PEBS_FIXUP_SIZE)
  740. return 0;
  741. /*
  742. * We sampled a branch insn, rewind using the LBR stack
  743. */
  744. if (ip == to) {
  745. set_linear_ip(regs, from);
  746. return 1;
  747. }
  748. size = ip - to;
  749. if (!kernel_ip(ip)) {
  750. int bytes;
  751. u8 *buf = this_cpu_read(insn_buffer);
  752. /* 'size' must fit our buffer, see above */
  753. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  754. if (bytes != 0)
  755. return 0;
  756. kaddr = buf;
  757. } else {
  758. kaddr = (void *)to;
  759. }
  760. do {
  761. struct insn insn;
  762. old_to = to;
  763. #ifdef CONFIG_X86_64
  764. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  765. #endif
  766. insn_init(&insn, kaddr, size, is_64bit);
  767. insn_get_length(&insn);
  768. /*
  769. * Make sure there was not a problem decoding the
  770. * instruction and getting the length. This is
  771. * doubly important because we have an infinite
  772. * loop if insn.length=0.
  773. */
  774. if (!insn.length)
  775. break;
  776. to += insn.length;
  777. kaddr += insn.length;
  778. size -= insn.length;
  779. } while (to < ip);
  780. if (to == ip) {
  781. set_linear_ip(regs, old_to);
  782. return 1;
  783. }
  784. /*
  785. * Even though we decoded the basic block, the instruction stream
  786. * never matched the given IP, either the TO or the IP got corrupted.
  787. */
  788. return 0;
  789. }
  790. static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
  791. {
  792. if (pebs->tsx_tuning) {
  793. union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
  794. return tsx.cycles_last_block;
  795. }
  796. return 0;
  797. }
  798. static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
  799. {
  800. u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
  801. /* For RTM XABORTs also log the abort code from AX */
  802. if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
  803. txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
  804. return txn;
  805. }
  806. static void setup_pebs_sample_data(struct perf_event *event,
  807. struct pt_regs *iregs, void *__pebs,
  808. struct perf_sample_data *data,
  809. struct pt_regs *regs)
  810. {
  811. #define PERF_X86_EVENT_PEBS_HSW_PREC \
  812. (PERF_X86_EVENT_PEBS_ST_HSW | \
  813. PERF_X86_EVENT_PEBS_LD_HSW | \
  814. PERF_X86_EVENT_PEBS_NA_HSW)
  815. /*
  816. * We cast to the biggest pebs_record but are careful not to
  817. * unconditionally access the 'extra' entries.
  818. */
  819. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  820. struct pebs_record_skl *pebs = __pebs;
  821. u64 sample_type;
  822. int fll, fst, dsrc;
  823. int fl = event->hw.flags;
  824. if (pebs == NULL)
  825. return;
  826. sample_type = event->attr.sample_type;
  827. dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
  828. fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
  829. fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
  830. perf_sample_data_init(data, 0, event->hw.last_period);
  831. data->period = event->hw.last_period;
  832. /*
  833. * Use latency for weight (only avail with PEBS-LL)
  834. */
  835. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  836. data->weight = pebs->lat;
  837. /*
  838. * data.data_src encodes the data source
  839. */
  840. if (dsrc) {
  841. u64 val = PERF_MEM_NA;
  842. if (fll)
  843. val = load_latency_data(pebs->dse);
  844. else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
  845. val = precise_datala_hsw(event, pebs->dse);
  846. else if (fst)
  847. val = precise_store_data(pebs->dse);
  848. data->data_src.val = val;
  849. }
  850. /*
  851. * We use the interrupt regs as a base because the PEBS record
  852. * does not contain a full regs set, specifically it seems to
  853. * lack segment descriptors, which get used by things like
  854. * user_mode().
  855. *
  856. * In the simple case fix up only the IP and BP,SP regs, for
  857. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  858. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  859. */
  860. *regs = *iregs;
  861. regs->flags = pebs->flags;
  862. set_linear_ip(regs, pebs->ip);
  863. regs->bp = pebs->bp;
  864. regs->sp = pebs->sp;
  865. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  866. regs->ax = pebs->ax;
  867. regs->bx = pebs->bx;
  868. regs->cx = pebs->cx;
  869. regs->dx = pebs->dx;
  870. regs->si = pebs->si;
  871. regs->di = pebs->di;
  872. regs->bp = pebs->bp;
  873. regs->sp = pebs->sp;
  874. regs->flags = pebs->flags;
  875. #ifndef CONFIG_X86_32
  876. regs->r8 = pebs->r8;
  877. regs->r9 = pebs->r9;
  878. regs->r10 = pebs->r10;
  879. regs->r11 = pebs->r11;
  880. regs->r12 = pebs->r12;
  881. regs->r13 = pebs->r13;
  882. regs->r14 = pebs->r14;
  883. regs->r15 = pebs->r15;
  884. #endif
  885. }
  886. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
  887. regs->ip = pebs->real_ip;
  888. regs->flags |= PERF_EFLAGS_EXACT;
  889. } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
  890. regs->flags |= PERF_EFLAGS_EXACT;
  891. else
  892. regs->flags &= ~PERF_EFLAGS_EXACT;
  893. if ((sample_type & PERF_SAMPLE_ADDR) &&
  894. x86_pmu.intel_cap.pebs_format >= 1)
  895. data->addr = pebs->dla;
  896. if (x86_pmu.intel_cap.pebs_format >= 2) {
  897. /* Only set the TSX weight when no memory weight. */
  898. if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
  899. data->weight = intel_hsw_weight(pebs);
  900. if (sample_type & PERF_SAMPLE_TRANSACTION)
  901. data->txn = intel_hsw_transaction(pebs);
  902. }
  903. /*
  904. * v3 supplies an accurate time stamp, so we use that
  905. * for the time stamp.
  906. *
  907. * We can only do this for the default trace clock.
  908. */
  909. if (x86_pmu.intel_cap.pebs_format >= 3 &&
  910. event->attr.use_clockid == 0)
  911. data->time = native_sched_clock_from_tsc(pebs->tsc);
  912. if (has_branch_stack(event))
  913. data->br_stack = &cpuc->lbr_stack;
  914. }
  915. static inline void *
  916. get_next_pebs_record_by_bit(void *base, void *top, int bit)
  917. {
  918. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  919. void *at;
  920. u64 pebs_status;
  921. /*
  922. * fmt0 does not have a status bitfield (does not use
  923. * perf_record_nhm format)
  924. */
  925. if (x86_pmu.intel_cap.pebs_format < 1)
  926. return base;
  927. if (base == NULL)
  928. return NULL;
  929. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  930. struct pebs_record_nhm *p = at;
  931. if (test_bit(bit, (unsigned long *)&p->status)) {
  932. /* PEBS v3 has accurate status bits */
  933. if (x86_pmu.intel_cap.pebs_format >= 3)
  934. return at;
  935. if (p->status == (1 << bit))
  936. return at;
  937. /* clear non-PEBS bit and re-check */
  938. pebs_status = p->status & cpuc->pebs_enabled;
  939. pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
  940. if (pebs_status == (1 << bit))
  941. return at;
  942. }
  943. }
  944. return NULL;
  945. }
  946. static void __intel_pmu_pebs_event(struct perf_event *event,
  947. struct pt_regs *iregs,
  948. void *base, void *top,
  949. int bit, int count)
  950. {
  951. struct perf_sample_data data;
  952. struct pt_regs regs;
  953. void *at = get_next_pebs_record_by_bit(base, top, bit);
  954. if (!intel_pmu_save_and_restart(event) &&
  955. !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
  956. return;
  957. while (count > 1) {
  958. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  959. perf_event_output(event, &data, &regs);
  960. at += x86_pmu.pebs_record_size;
  961. at = get_next_pebs_record_by_bit(at, top, bit);
  962. count--;
  963. }
  964. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  965. /*
  966. * All but the last records are processed.
  967. * The last one is left to be able to call the overflow handler.
  968. */
  969. if (perf_event_overflow(event, &data, &regs)) {
  970. x86_pmu_stop(event, 0);
  971. return;
  972. }
  973. }
  974. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  975. {
  976. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  977. struct debug_store *ds = cpuc->ds;
  978. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  979. struct pebs_record_core *at, *top;
  980. int n;
  981. if (!x86_pmu.pebs_active)
  982. return;
  983. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  984. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  985. /*
  986. * Whatever else happens, drain the thing
  987. */
  988. ds->pebs_index = ds->pebs_buffer_base;
  989. if (!test_bit(0, cpuc->active_mask))
  990. return;
  991. WARN_ON_ONCE(!event);
  992. if (!event->attr.precise_ip)
  993. return;
  994. n = top - at;
  995. if (n <= 0)
  996. return;
  997. __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
  998. }
  999. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  1000. {
  1001. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1002. struct debug_store *ds = cpuc->ds;
  1003. struct perf_event *event;
  1004. void *base, *at, *top;
  1005. short counts[MAX_PEBS_EVENTS] = {};
  1006. short error[MAX_PEBS_EVENTS] = {};
  1007. int bit, i;
  1008. if (!x86_pmu.pebs_active)
  1009. return;
  1010. base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  1011. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  1012. ds->pebs_index = ds->pebs_buffer_base;
  1013. if (unlikely(base >= top))
  1014. return;
  1015. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  1016. struct pebs_record_nhm *p = at;
  1017. u64 pebs_status;
  1018. /* PEBS v3 has accurate status bits */
  1019. if (x86_pmu.intel_cap.pebs_format >= 3) {
  1020. for_each_set_bit(bit, (unsigned long *)&p->status,
  1021. MAX_PEBS_EVENTS)
  1022. counts[bit]++;
  1023. continue;
  1024. }
  1025. pebs_status = p->status & cpuc->pebs_enabled;
  1026. pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
  1027. /*
  1028. * On some CPUs the PEBS status can be zero when PEBS is
  1029. * racing with clearing of GLOBAL_STATUS.
  1030. *
  1031. * Normally we would drop that record, but in the
  1032. * case when there is only a single active PEBS event
  1033. * we can assume it's for that event.
  1034. */
  1035. if (!pebs_status && cpuc->pebs_enabled &&
  1036. !(cpuc->pebs_enabled & (cpuc->pebs_enabled-1)))
  1037. pebs_status = cpuc->pebs_enabled;
  1038. bit = find_first_bit((unsigned long *)&pebs_status,
  1039. x86_pmu.max_pebs_events);
  1040. if (bit >= x86_pmu.max_pebs_events)
  1041. continue;
  1042. /*
  1043. * The PEBS hardware does not deal well with the situation
  1044. * when events happen near to each other and multiple bits
  1045. * are set. But it should happen rarely.
  1046. *
  1047. * If these events include one PEBS and multiple non-PEBS
  1048. * events, it doesn't impact PEBS record. The record will
  1049. * be handled normally. (slow path)
  1050. *
  1051. * If these events include two or more PEBS events, the
  1052. * records for the events can be collapsed into a single
  1053. * one, and it's not possible to reconstruct all events
  1054. * that caused the PEBS record. It's called collision.
  1055. * If collision happened, the record will be dropped.
  1056. */
  1057. if (p->status != (1ULL << bit)) {
  1058. for_each_set_bit(i, (unsigned long *)&pebs_status,
  1059. x86_pmu.max_pebs_events)
  1060. error[i]++;
  1061. continue;
  1062. }
  1063. counts[bit]++;
  1064. }
  1065. for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
  1066. if ((counts[bit] == 0) && (error[bit] == 0))
  1067. continue;
  1068. event = cpuc->events[bit];
  1069. WARN_ON_ONCE(!event);
  1070. WARN_ON_ONCE(!event->attr.precise_ip);
  1071. /* log dropped samples number */
  1072. if (error[bit])
  1073. perf_log_lost_samples(event, error[bit]);
  1074. if (counts[bit]) {
  1075. __intel_pmu_pebs_event(event, iregs, base,
  1076. top, bit, counts[bit]);
  1077. }
  1078. }
  1079. }
  1080. /*
  1081. * BTS, PEBS probe and setup
  1082. */
  1083. void __init intel_ds_init(void)
  1084. {
  1085. /*
  1086. * No support for 32bit formats
  1087. */
  1088. if (!boot_cpu_has(X86_FEATURE_DTES64))
  1089. return;
  1090. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  1091. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  1092. if (x86_pmu.pebs) {
  1093. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  1094. int format = x86_pmu.intel_cap.pebs_format;
  1095. switch (format) {
  1096. case 0:
  1097. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  1098. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  1099. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  1100. break;
  1101. case 1:
  1102. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  1103. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  1104. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1105. break;
  1106. case 2:
  1107. pr_cont("PEBS fmt2%c, ", pebs_type);
  1108. x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
  1109. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1110. break;
  1111. case 3:
  1112. pr_cont("PEBS fmt3%c, ", pebs_type);
  1113. x86_pmu.pebs_record_size =
  1114. sizeof(struct pebs_record_skl);
  1115. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1116. x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
  1117. break;
  1118. default:
  1119. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  1120. x86_pmu.pebs = 0;
  1121. }
  1122. }
  1123. }
  1124. void perf_restore_debug_store(void)
  1125. {
  1126. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  1127. if (!x86_pmu.bts && !x86_pmu.pebs)
  1128. return;
  1129. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  1130. }