perf_event_intel_ds.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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, *top;
  413. struct perf_output_handle handle;
  414. struct perf_event_header header;
  415. struct perf_sample_data data;
  416. struct pt_regs regs;
  417. if (!event)
  418. return 0;
  419. if (!x86_pmu.bts_active)
  420. return 0;
  421. at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
  422. top = (struct bts_record *)(unsigned long)ds->bts_index;
  423. if (top <= at)
  424. return 0;
  425. memset(&regs, 0, sizeof(regs));
  426. ds->bts_index = ds->bts_buffer_base;
  427. perf_sample_data_init(&data, 0, event->hw.last_period);
  428. /*
  429. * Prepare a generic sample, i.e. fill in the invariant fields.
  430. * We will overwrite the from and to address before we output
  431. * the sample.
  432. */
  433. perf_prepare_sample(&header, &data, event, &regs);
  434. if (perf_output_begin(&handle, event, header.size * (top - at)))
  435. return 1;
  436. for (; at < top; at++) {
  437. data.ip = at->from;
  438. data.addr = at->to;
  439. perf_output_sample(&handle, &header, &data, event);
  440. }
  441. perf_output_end(&handle);
  442. /* There's new data available. */
  443. event->hw.interrupts++;
  444. event->pending_kill = POLL_IN;
  445. return 1;
  446. }
  447. static inline void intel_pmu_drain_pebs_buffer(void)
  448. {
  449. struct pt_regs regs;
  450. x86_pmu.drain_pebs(&regs);
  451. }
  452. void intel_pmu_pebs_sched_task(struct perf_event_context *ctx, bool sched_in)
  453. {
  454. if (!sched_in)
  455. intel_pmu_drain_pebs_buffer();
  456. }
  457. /*
  458. * PEBS
  459. */
  460. struct event_constraint intel_core2_pebs_event_constraints[] = {
  461. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  462. INTEL_FLAGS_UEVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */
  463. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */
  464. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */
  465. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  466. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  467. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  468. EVENT_CONSTRAINT_END
  469. };
  470. struct event_constraint intel_atom_pebs_event_constraints[] = {
  471. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c0, 0x1), /* INST_RETIRED.ANY */
  472. INTEL_FLAGS_UEVENT_CONSTRAINT(0x00c5, 0x1), /* MISPREDICTED_BRANCH_RETIRED */
  473. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED.* */
  474. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  475. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x01),
  476. EVENT_CONSTRAINT_END
  477. };
  478. struct event_constraint intel_slm_pebs_event_constraints[] = {
  479. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  480. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x1),
  481. /* Allow all events as PEBS with no flags */
  482. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1),
  483. EVENT_CONSTRAINT_END
  484. };
  485. struct event_constraint intel_nehalem_pebs_event_constraints[] = {
  486. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  487. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  488. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  489. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INST_RETIRED.ANY */
  490. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  491. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  492. INTEL_FLAGS_UEVENT_CONSTRAINT(0x02c5, 0xf), /* BR_MISP_RETIRED.NEAR_CALL */
  493. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  494. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  495. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  496. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  497. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  498. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  499. EVENT_CONSTRAINT_END
  500. };
  501. struct event_constraint intel_westmere_pebs_event_constraints[] = {
  502. INTEL_PLD_CONSTRAINT(0x100b, 0xf), /* MEM_INST_RETIRED.* */
  503. INTEL_FLAGS_EVENT_CONSTRAINT(0x0f, 0xf), /* MEM_UNCORE_RETIRED.* */
  504. INTEL_FLAGS_UEVENT_CONSTRAINT(0x010c, 0xf), /* MEM_STORE_RETIRED.DTLB_MISS */
  505. INTEL_FLAGS_EVENT_CONSTRAINT(0xc0, 0xf), /* INSTR_RETIRED.* */
  506. INTEL_EVENT_CONSTRAINT(0xc2, 0xf), /* UOPS_RETIRED.* */
  507. INTEL_FLAGS_EVENT_CONSTRAINT(0xc4, 0xf), /* BR_INST_RETIRED.* */
  508. INTEL_FLAGS_EVENT_CONSTRAINT(0xc5, 0xf), /* BR_MISP_RETIRED.* */
  509. INTEL_FLAGS_EVENT_CONSTRAINT(0xc7, 0xf), /* SSEX_UOPS_RETIRED.* */
  510. INTEL_FLAGS_UEVENT_CONSTRAINT(0x20c8, 0xf), /* ITLB_MISS_RETIRED */
  511. INTEL_FLAGS_EVENT_CONSTRAINT(0xcb, 0xf), /* MEM_LOAD_RETIRED.* */
  512. INTEL_FLAGS_EVENT_CONSTRAINT(0xf7, 0xf), /* FP_ASSIST.* */
  513. /* INST_RETIRED.ANY_P, inv=1, cmask=16 (cycles:p). */
  514. INTEL_FLAGS_EVENT_CONSTRAINT(0x108000c0, 0x0f),
  515. EVENT_CONSTRAINT_END
  516. };
  517. struct event_constraint intel_snb_pebs_event_constraints[] = {
  518. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  519. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  520. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  521. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  522. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  523. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  524. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  525. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  526. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  527. /* Allow all events as PEBS with no flags */
  528. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  529. EVENT_CONSTRAINT_END
  530. };
  531. struct event_constraint intel_ivb_pebs_event_constraints[] = {
  532. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  533. INTEL_PLD_CONSTRAINT(0x01cd, 0x8), /* MEM_TRANS_RETIRED.LAT_ABOVE_THR */
  534. INTEL_PST_CONSTRAINT(0x02cd, 0x8), /* MEM_TRANS_RETIRED.PRECISE_STORES */
  535. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  536. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  537. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOP_RETIRED.* */
  538. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  539. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  540. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  541. /* Allow all events as PEBS with no flags */
  542. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  543. EVENT_CONSTRAINT_END
  544. };
  545. struct event_constraint intel_hsw_pebs_event_constraints[] = {
  546. INTEL_FLAGS_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
  547. INTEL_PLD_CONSTRAINT(0x01cd, 0xf), /* MEM_TRANS_RETIRED.* */
  548. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  549. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  550. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  551. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
  552. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
  553. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
  554. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XLD(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
  555. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
  556. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
  557. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_XST(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
  558. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  559. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd2, 0xf), /* MEM_LOAD_UOPS_L3_HIT_RETIRED.* */
  560. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_XLD(0xd3, 0xf), /* MEM_LOAD_UOPS_L3_MISS_RETIRED.* */
  561. /* Allow all events as PEBS with no flags */
  562. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  563. EVENT_CONSTRAINT_END
  564. };
  565. struct event_constraint intel_skl_pebs_event_constraints[] = {
  566. INTEL_FLAGS_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
  567. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
  568. /* UOPS_RETIRED.ALL, inv=1, cmask=16 (cycles:p). */
  569. INTEL_FLAGS_EVENT_CONSTRAINT(0x108001c2, 0xf),
  570. INTEL_PLD_CONSTRAINT(0x1cd, 0xf), /* MEM_TRANS_RETIRED.* */
  571. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x11d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_LOADS */
  572. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x12d0, 0xf), /* MEM_INST_RETIRED.STLB_MISS_STORES */
  573. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x21d0, 0xf), /* MEM_INST_RETIRED.LOCK_LOADS */
  574. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x22d0, 0xf), /* MEM_INST_RETIRED.LOCK_STORES */
  575. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x41d0, 0xf), /* MEM_INST_RETIRED.SPLIT_LOADS */
  576. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x42d0, 0xf), /* MEM_INST_RETIRED.SPLIT_STORES */
  577. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(0x81d0, 0xf), /* MEM_INST_RETIRED.ALL_LOADS */
  578. INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(0x82d0, 0xf), /* MEM_INST_RETIRED.ALL_STORES */
  579. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
  580. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
  581. INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(0xd3, 0xf), /* MEM_LOAD_L3_MISS_RETIRED.* */
  582. /* Allow all events as PEBS with no flags */
  583. INTEL_ALL_EVENT_CONSTRAINT(0, 0xf),
  584. EVENT_CONSTRAINT_END
  585. };
  586. struct event_constraint *intel_pebs_constraints(struct perf_event *event)
  587. {
  588. struct event_constraint *c;
  589. if (!event->attr.precise_ip)
  590. return NULL;
  591. if (x86_pmu.pebs_constraints) {
  592. for_each_event_constraint(c, x86_pmu.pebs_constraints) {
  593. if ((event->hw.config & c->cmask) == c->code) {
  594. event->hw.flags |= c->flags;
  595. return c;
  596. }
  597. }
  598. }
  599. return &emptyconstraint;
  600. }
  601. static inline bool pebs_is_enabled(struct cpu_hw_events *cpuc)
  602. {
  603. return (cpuc->pebs_enabled & ((1ULL << MAX_PEBS_EVENTS) - 1));
  604. }
  605. void intel_pmu_pebs_enable(struct perf_event *event)
  606. {
  607. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  608. struct hw_perf_event *hwc = &event->hw;
  609. struct debug_store *ds = cpuc->ds;
  610. bool first_pebs;
  611. u64 threshold;
  612. hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT;
  613. first_pebs = !pebs_is_enabled(cpuc);
  614. cpuc->pebs_enabled |= 1ULL << hwc->idx;
  615. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  616. cpuc->pebs_enabled |= 1ULL << (hwc->idx + 32);
  617. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  618. cpuc->pebs_enabled |= 1ULL << 63;
  619. /*
  620. * When the event is constrained enough we can use a larger
  621. * threshold and run the event with less frequent PMI.
  622. */
  623. if (hwc->flags & PERF_X86_EVENT_FREERUNNING) {
  624. threshold = ds->pebs_absolute_maximum -
  625. x86_pmu.max_pebs_events * x86_pmu.pebs_record_size;
  626. if (first_pebs)
  627. perf_sched_cb_inc(event->ctx->pmu);
  628. } else {
  629. threshold = ds->pebs_buffer_base + x86_pmu.pebs_record_size;
  630. /*
  631. * If not all events can use larger buffer,
  632. * roll back to threshold = 1
  633. */
  634. if (!first_pebs &&
  635. (ds->pebs_interrupt_threshold > threshold))
  636. perf_sched_cb_dec(event->ctx->pmu);
  637. }
  638. /* Use auto-reload if possible to save a MSR write in the PMI */
  639. if (hwc->flags & PERF_X86_EVENT_AUTO_RELOAD) {
  640. ds->pebs_event_reset[hwc->idx] =
  641. (u64)(-hwc->sample_period) & x86_pmu.cntval_mask;
  642. }
  643. if (first_pebs || ds->pebs_interrupt_threshold > threshold)
  644. ds->pebs_interrupt_threshold = threshold;
  645. }
  646. void intel_pmu_pebs_disable(struct perf_event *event)
  647. {
  648. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  649. struct hw_perf_event *hwc = &event->hw;
  650. struct debug_store *ds = cpuc->ds;
  651. bool large_pebs = ds->pebs_interrupt_threshold >
  652. ds->pebs_buffer_base + x86_pmu.pebs_record_size;
  653. if (large_pebs)
  654. intel_pmu_drain_pebs_buffer();
  655. cpuc->pebs_enabled &= ~(1ULL << hwc->idx);
  656. if (event->hw.flags & PERF_X86_EVENT_PEBS_LDLAT)
  657. cpuc->pebs_enabled &= ~(1ULL << (hwc->idx + 32));
  658. else if (event->hw.flags & PERF_X86_EVENT_PEBS_ST)
  659. cpuc->pebs_enabled &= ~(1ULL << 63);
  660. if (large_pebs && !pebs_is_enabled(cpuc))
  661. perf_sched_cb_dec(event->ctx->pmu);
  662. if (cpuc->enabled)
  663. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  664. hwc->config |= ARCH_PERFMON_EVENTSEL_INT;
  665. }
  666. void intel_pmu_pebs_enable_all(void)
  667. {
  668. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  669. if (cpuc->pebs_enabled)
  670. wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled);
  671. }
  672. void intel_pmu_pebs_disable_all(void)
  673. {
  674. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  675. if (cpuc->pebs_enabled)
  676. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  677. }
  678. static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
  679. {
  680. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  681. unsigned long from = cpuc->lbr_entries[0].from;
  682. unsigned long old_to, to = cpuc->lbr_entries[0].to;
  683. unsigned long ip = regs->ip;
  684. int is_64bit = 0;
  685. void *kaddr;
  686. int size;
  687. /*
  688. * We don't need to fixup if the PEBS assist is fault like
  689. */
  690. if (!x86_pmu.intel_cap.pebs_trap)
  691. return 1;
  692. /*
  693. * No LBR entry, no basic block, no rewinding
  694. */
  695. if (!cpuc->lbr_stack.nr || !from || !to)
  696. return 0;
  697. /*
  698. * Basic blocks should never cross user/kernel boundaries
  699. */
  700. if (kernel_ip(ip) != kernel_ip(to))
  701. return 0;
  702. /*
  703. * unsigned math, either ip is before the start (impossible) or
  704. * the basic block is larger than 1 page (sanity)
  705. */
  706. if ((ip - to) > PEBS_FIXUP_SIZE)
  707. return 0;
  708. /*
  709. * We sampled a branch insn, rewind using the LBR stack
  710. */
  711. if (ip == to) {
  712. set_linear_ip(regs, from);
  713. return 1;
  714. }
  715. size = ip - to;
  716. if (!kernel_ip(ip)) {
  717. int bytes;
  718. u8 *buf = this_cpu_read(insn_buffer);
  719. /* 'size' must fit our buffer, see above */
  720. bytes = copy_from_user_nmi(buf, (void __user *)to, size);
  721. if (bytes != 0)
  722. return 0;
  723. kaddr = buf;
  724. } else {
  725. kaddr = (void *)to;
  726. }
  727. do {
  728. struct insn insn;
  729. old_to = to;
  730. #ifdef CONFIG_X86_64
  731. is_64bit = kernel_ip(to) || !test_thread_flag(TIF_IA32);
  732. #endif
  733. insn_init(&insn, kaddr, size, is_64bit);
  734. insn_get_length(&insn);
  735. /*
  736. * Make sure there was not a problem decoding the
  737. * instruction and getting the length. This is
  738. * doubly important because we have an infinite
  739. * loop if insn.length=0.
  740. */
  741. if (!insn.length)
  742. break;
  743. to += insn.length;
  744. kaddr += insn.length;
  745. size -= insn.length;
  746. } while (to < ip);
  747. if (to == ip) {
  748. set_linear_ip(regs, old_to);
  749. return 1;
  750. }
  751. /*
  752. * Even though we decoded the basic block, the instruction stream
  753. * never matched the given IP, either the TO or the IP got corrupted.
  754. */
  755. return 0;
  756. }
  757. static inline u64 intel_hsw_weight(struct pebs_record_skl *pebs)
  758. {
  759. if (pebs->tsx_tuning) {
  760. union hsw_tsx_tuning tsx = { .value = pebs->tsx_tuning };
  761. return tsx.cycles_last_block;
  762. }
  763. return 0;
  764. }
  765. static inline u64 intel_hsw_transaction(struct pebs_record_skl *pebs)
  766. {
  767. u64 txn = (pebs->tsx_tuning & PEBS_HSW_TSX_FLAGS) >> 32;
  768. /* For RTM XABORTs also log the abort code from AX */
  769. if ((txn & PERF_TXN_TRANSACTION) && (pebs->ax & 1))
  770. txn |= ((pebs->ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
  771. return txn;
  772. }
  773. static void setup_pebs_sample_data(struct perf_event *event,
  774. struct pt_regs *iregs, void *__pebs,
  775. struct perf_sample_data *data,
  776. struct pt_regs *regs)
  777. {
  778. #define PERF_X86_EVENT_PEBS_HSW_PREC \
  779. (PERF_X86_EVENT_PEBS_ST_HSW | \
  780. PERF_X86_EVENT_PEBS_LD_HSW | \
  781. PERF_X86_EVENT_PEBS_NA_HSW)
  782. /*
  783. * We cast to the biggest pebs_record but are careful not to
  784. * unconditionally access the 'extra' entries.
  785. */
  786. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  787. struct pebs_record_skl *pebs = __pebs;
  788. u64 sample_type;
  789. int fll, fst, dsrc;
  790. int fl = event->hw.flags;
  791. if (pebs == NULL)
  792. return;
  793. sample_type = event->attr.sample_type;
  794. dsrc = sample_type & PERF_SAMPLE_DATA_SRC;
  795. fll = fl & PERF_X86_EVENT_PEBS_LDLAT;
  796. fst = fl & (PERF_X86_EVENT_PEBS_ST | PERF_X86_EVENT_PEBS_HSW_PREC);
  797. perf_sample_data_init(data, 0, event->hw.last_period);
  798. data->period = event->hw.last_period;
  799. /*
  800. * Use latency for weight (only avail with PEBS-LL)
  801. */
  802. if (fll && (sample_type & PERF_SAMPLE_WEIGHT))
  803. data->weight = pebs->lat;
  804. /*
  805. * data.data_src encodes the data source
  806. */
  807. if (dsrc) {
  808. u64 val = PERF_MEM_NA;
  809. if (fll)
  810. val = load_latency_data(pebs->dse);
  811. else if (fst && (fl & PERF_X86_EVENT_PEBS_HSW_PREC))
  812. val = precise_datala_hsw(event, pebs->dse);
  813. else if (fst)
  814. val = precise_store_data(pebs->dse);
  815. data->data_src.val = val;
  816. }
  817. /*
  818. * We use the interrupt regs as a base because the PEBS record
  819. * does not contain a full regs set, specifically it seems to
  820. * lack segment descriptors, which get used by things like
  821. * user_mode().
  822. *
  823. * In the simple case fix up only the IP and BP,SP regs, for
  824. * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly.
  825. * A possible PERF_SAMPLE_REGS will have to transfer all regs.
  826. */
  827. *regs = *iregs;
  828. regs->flags = pebs->flags;
  829. set_linear_ip(regs, pebs->ip);
  830. regs->bp = pebs->bp;
  831. regs->sp = pebs->sp;
  832. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  833. regs->ax = pebs->ax;
  834. regs->bx = pebs->bx;
  835. regs->cx = pebs->cx;
  836. regs->dx = pebs->dx;
  837. regs->si = pebs->si;
  838. regs->di = pebs->di;
  839. regs->bp = pebs->bp;
  840. regs->sp = pebs->sp;
  841. regs->flags = pebs->flags;
  842. #ifndef CONFIG_X86_32
  843. regs->r8 = pebs->r8;
  844. regs->r9 = pebs->r9;
  845. regs->r10 = pebs->r10;
  846. regs->r11 = pebs->r11;
  847. regs->r12 = pebs->r12;
  848. regs->r13 = pebs->r13;
  849. regs->r14 = pebs->r14;
  850. regs->r15 = pebs->r15;
  851. #endif
  852. }
  853. if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format >= 2) {
  854. regs->ip = pebs->real_ip;
  855. regs->flags |= PERF_EFLAGS_EXACT;
  856. } else if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(regs))
  857. regs->flags |= PERF_EFLAGS_EXACT;
  858. else
  859. regs->flags &= ~PERF_EFLAGS_EXACT;
  860. if ((sample_type & PERF_SAMPLE_ADDR) &&
  861. x86_pmu.intel_cap.pebs_format >= 1)
  862. data->addr = pebs->dla;
  863. if (x86_pmu.intel_cap.pebs_format >= 2) {
  864. /* Only set the TSX weight when no memory weight. */
  865. if ((sample_type & PERF_SAMPLE_WEIGHT) && !fll)
  866. data->weight = intel_hsw_weight(pebs);
  867. if (sample_type & PERF_SAMPLE_TRANSACTION)
  868. data->txn = intel_hsw_transaction(pebs);
  869. }
  870. /*
  871. * v3 supplies an accurate time stamp, so we use that
  872. * for the time stamp.
  873. *
  874. * We can only do this for the default trace clock.
  875. */
  876. if (x86_pmu.intel_cap.pebs_format >= 3 &&
  877. event->attr.use_clockid == 0)
  878. data->time = native_sched_clock_from_tsc(pebs->tsc);
  879. if (has_branch_stack(event))
  880. data->br_stack = &cpuc->lbr_stack;
  881. }
  882. static inline void *
  883. get_next_pebs_record_by_bit(void *base, void *top, int bit)
  884. {
  885. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  886. void *at;
  887. u64 pebs_status;
  888. if (base == NULL)
  889. return NULL;
  890. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  891. struct pebs_record_nhm *p = at;
  892. if (test_bit(bit, (unsigned long *)&p->status)) {
  893. /* PEBS v3 has accurate status bits */
  894. if (x86_pmu.intel_cap.pebs_format >= 3)
  895. return at;
  896. if (p->status == (1 << bit))
  897. return at;
  898. /* clear non-PEBS bit and re-check */
  899. pebs_status = p->status & cpuc->pebs_enabled;
  900. pebs_status &= (1ULL << MAX_PEBS_EVENTS) - 1;
  901. if (pebs_status == (1 << bit))
  902. return at;
  903. }
  904. }
  905. return NULL;
  906. }
  907. static void __intel_pmu_pebs_event(struct perf_event *event,
  908. struct pt_regs *iregs,
  909. void *base, void *top,
  910. int bit, int count)
  911. {
  912. struct perf_sample_data data;
  913. struct pt_regs regs;
  914. void *at = get_next_pebs_record_by_bit(base, top, bit);
  915. if (!intel_pmu_save_and_restart(event) &&
  916. !(event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD))
  917. return;
  918. while (count > 1) {
  919. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  920. perf_event_output(event, &data, &regs);
  921. at += x86_pmu.pebs_record_size;
  922. at = get_next_pebs_record_by_bit(at, top, bit);
  923. count--;
  924. }
  925. setup_pebs_sample_data(event, iregs, at, &data, &regs);
  926. /*
  927. * All but the last records are processed.
  928. * The last one is left to be able to call the overflow handler.
  929. */
  930. if (perf_event_overflow(event, &data, &regs)) {
  931. x86_pmu_stop(event, 0);
  932. return;
  933. }
  934. }
  935. static void intel_pmu_drain_pebs_core(struct pt_regs *iregs)
  936. {
  937. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  938. struct debug_store *ds = cpuc->ds;
  939. struct perf_event *event = cpuc->events[0]; /* PMC0 only */
  940. struct pebs_record_core *at, *top;
  941. int n;
  942. if (!x86_pmu.pebs_active)
  943. return;
  944. at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base;
  945. top = (struct pebs_record_core *)(unsigned long)ds->pebs_index;
  946. /*
  947. * Whatever else happens, drain the thing
  948. */
  949. ds->pebs_index = ds->pebs_buffer_base;
  950. if (!test_bit(0, cpuc->active_mask))
  951. return;
  952. WARN_ON_ONCE(!event);
  953. if (!event->attr.precise_ip)
  954. return;
  955. n = (top - at) / x86_pmu.pebs_record_size;
  956. if (n <= 0)
  957. return;
  958. __intel_pmu_pebs_event(event, iregs, at, top, 0, n);
  959. }
  960. static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs)
  961. {
  962. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  963. struct debug_store *ds = cpuc->ds;
  964. struct perf_event *event;
  965. void *base, *at, *top;
  966. short counts[MAX_PEBS_EVENTS] = {};
  967. short error[MAX_PEBS_EVENTS] = {};
  968. int bit, i;
  969. if (!x86_pmu.pebs_active)
  970. return;
  971. base = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base;
  972. top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index;
  973. ds->pebs_index = ds->pebs_buffer_base;
  974. if (unlikely(base >= top))
  975. return;
  976. for (at = base; at < top; at += x86_pmu.pebs_record_size) {
  977. struct pebs_record_nhm *p = at;
  978. u64 pebs_status;
  979. /* PEBS v3 has accurate status bits */
  980. if (x86_pmu.intel_cap.pebs_format >= 3) {
  981. for_each_set_bit(bit, (unsigned long *)&p->status,
  982. MAX_PEBS_EVENTS)
  983. counts[bit]++;
  984. continue;
  985. }
  986. pebs_status = p->status & cpuc->pebs_enabled;
  987. pebs_status &= (1ULL << x86_pmu.max_pebs_events) - 1;
  988. bit = find_first_bit((unsigned long *)&pebs_status,
  989. x86_pmu.max_pebs_events);
  990. if (WARN(bit >= x86_pmu.max_pebs_events,
  991. "PEBS record without PEBS event! status=%Lx pebs_enabled=%Lx active_mask=%Lx",
  992. (unsigned long long)p->status, (unsigned long long)cpuc->pebs_enabled,
  993. *(unsigned long long *)cpuc->active_mask))
  994. continue;
  995. /*
  996. * The PEBS hardware does not deal well with the situation
  997. * when events happen near to each other and multiple bits
  998. * are set. But it should happen rarely.
  999. *
  1000. * If these events include one PEBS and multiple non-PEBS
  1001. * events, it doesn't impact PEBS record. The record will
  1002. * be handled normally. (slow path)
  1003. *
  1004. * If these events include two or more PEBS events, the
  1005. * records for the events can be collapsed into a single
  1006. * one, and it's not possible to reconstruct all events
  1007. * that caused the PEBS record. It's called collision.
  1008. * If collision happened, the record will be dropped.
  1009. */
  1010. if (p->status != (1ULL << bit)) {
  1011. for_each_set_bit(i, (unsigned long *)&pebs_status,
  1012. x86_pmu.max_pebs_events)
  1013. error[i]++;
  1014. continue;
  1015. }
  1016. counts[bit]++;
  1017. }
  1018. for (bit = 0; bit < x86_pmu.max_pebs_events; bit++) {
  1019. if ((counts[bit] == 0) && (error[bit] == 0))
  1020. continue;
  1021. event = cpuc->events[bit];
  1022. WARN_ON_ONCE(!event);
  1023. WARN_ON_ONCE(!event->attr.precise_ip);
  1024. /* log dropped samples number */
  1025. if (error[bit])
  1026. perf_log_lost_samples(event, error[bit]);
  1027. if (counts[bit]) {
  1028. __intel_pmu_pebs_event(event, iregs, base,
  1029. top, bit, counts[bit]);
  1030. }
  1031. }
  1032. }
  1033. /*
  1034. * BTS, PEBS probe and setup
  1035. */
  1036. void __init intel_ds_init(void)
  1037. {
  1038. /*
  1039. * No support for 32bit formats
  1040. */
  1041. if (!boot_cpu_has(X86_FEATURE_DTES64))
  1042. return;
  1043. x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS);
  1044. x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS);
  1045. if (x86_pmu.pebs) {
  1046. char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-';
  1047. int format = x86_pmu.intel_cap.pebs_format;
  1048. switch (format) {
  1049. case 0:
  1050. printk(KERN_CONT "PEBS fmt0%c, ", pebs_type);
  1051. x86_pmu.pebs_record_size = sizeof(struct pebs_record_core);
  1052. x86_pmu.drain_pebs = intel_pmu_drain_pebs_core;
  1053. break;
  1054. case 1:
  1055. printk(KERN_CONT "PEBS fmt1%c, ", pebs_type);
  1056. x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm);
  1057. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1058. break;
  1059. case 2:
  1060. pr_cont("PEBS fmt2%c, ", pebs_type);
  1061. x86_pmu.pebs_record_size = sizeof(struct pebs_record_hsw);
  1062. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1063. break;
  1064. case 3:
  1065. pr_cont("PEBS fmt3%c, ", pebs_type);
  1066. x86_pmu.pebs_record_size =
  1067. sizeof(struct pebs_record_skl);
  1068. x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm;
  1069. x86_pmu.free_running_flags |= PERF_SAMPLE_TIME;
  1070. break;
  1071. default:
  1072. printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type);
  1073. x86_pmu.pebs = 0;
  1074. }
  1075. }
  1076. }
  1077. void perf_restore_debug_store(void)
  1078. {
  1079. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  1080. if (!x86_pmu.bts && !x86_pmu.pebs)
  1081. return;
  1082. wrmsrl(MSR_IA32_DS_AREA, (unsigned long)ds);
  1083. }