ds.c 43 KB

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