perf_event_intel_pt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Intel(R) Processor Trace PMU driver for perf
  3. * Copyright (c) 2013-2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Intel PT is specified in the Intel Architecture Instruction Set Extensions
  15. * Programming Reference:
  16. * http://software.intel.com/en-us/intel-isa-extensions
  17. */
  18. #undef DEBUG
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/types.h>
  21. #include <linux/slab.h>
  22. #include <linux/device.h>
  23. #include <asm/perf_event.h>
  24. #include <asm/insn.h>
  25. #include <asm/io.h>
  26. #include "perf_event.h"
  27. #include "intel_pt.h"
  28. static DEFINE_PER_CPU(struct pt, pt_ctx);
  29. static struct pt_pmu pt_pmu;
  30. enum cpuid_regs {
  31. CR_EAX = 0,
  32. CR_ECX,
  33. CR_EDX,
  34. CR_EBX
  35. };
  36. /*
  37. * Capabilities of Intel PT hardware, such as number of address bits or
  38. * supported output schemes, are cached and exported to userspace as "caps"
  39. * attribute group of pt pmu device
  40. * (/sys/bus/event_source/devices/intel_pt/caps/) so that userspace can store
  41. * relevant bits together with intel_pt traces.
  42. *
  43. * These are necessary for both trace decoding (payloads_lip, contains address
  44. * width encoded in IP-related packets), and event configuration (bitmasks with
  45. * permitted values for certain bit fields).
  46. */
  47. #define PT_CAP(_n, _l, _r, _m) \
  48. [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
  49. .reg = _r, .mask = _m }
  50. static struct pt_cap_desc {
  51. const char *name;
  52. u32 leaf;
  53. u8 reg;
  54. u32 mask;
  55. } pt_caps[] = {
  56. PT_CAP(max_subleaf, 0, CR_EAX, 0xffffffff),
  57. PT_CAP(cr3_filtering, 0, CR_EBX, BIT(0)),
  58. PT_CAP(psb_cyc, 0, CR_EBX, BIT(1)),
  59. PT_CAP(mtc, 0, CR_EBX, BIT(3)),
  60. PT_CAP(topa_output, 0, CR_ECX, BIT(0)),
  61. PT_CAP(topa_multiple_entries, 0, CR_ECX, BIT(1)),
  62. PT_CAP(single_range_output, 0, CR_ECX, BIT(2)),
  63. PT_CAP(payloads_lip, 0, CR_ECX, BIT(31)),
  64. PT_CAP(mtc_periods, 1, CR_EAX, 0xffff0000),
  65. PT_CAP(cycle_thresholds, 1, CR_EBX, 0xffff),
  66. PT_CAP(psb_periods, 1, CR_EBX, 0xffff0000),
  67. };
  68. static u32 pt_cap_get(enum pt_capabilities cap)
  69. {
  70. struct pt_cap_desc *cd = &pt_caps[cap];
  71. u32 c = pt_pmu.caps[cd->leaf * PT_CPUID_REGS_NUM + cd->reg];
  72. unsigned int shift = __ffs(cd->mask);
  73. return (c & cd->mask) >> shift;
  74. }
  75. static ssize_t pt_cap_show(struct device *cdev,
  76. struct device_attribute *attr,
  77. char *buf)
  78. {
  79. struct dev_ext_attribute *ea =
  80. container_of(attr, struct dev_ext_attribute, attr);
  81. enum pt_capabilities cap = (long)ea->var;
  82. return snprintf(buf, PAGE_SIZE, "%x\n", pt_cap_get(cap));
  83. }
  84. static struct attribute_group pt_cap_group = {
  85. .name = "caps",
  86. };
  87. PMU_FORMAT_ATTR(cyc, "config:1" );
  88. PMU_FORMAT_ATTR(mtc, "config:9" );
  89. PMU_FORMAT_ATTR(tsc, "config:10" );
  90. PMU_FORMAT_ATTR(noretcomp, "config:11" );
  91. PMU_FORMAT_ATTR(mtc_period, "config:14-17" );
  92. PMU_FORMAT_ATTR(cyc_thresh, "config:19-22" );
  93. PMU_FORMAT_ATTR(psb_period, "config:24-27" );
  94. static struct attribute *pt_formats_attr[] = {
  95. &format_attr_cyc.attr,
  96. &format_attr_mtc.attr,
  97. &format_attr_tsc.attr,
  98. &format_attr_noretcomp.attr,
  99. &format_attr_mtc_period.attr,
  100. &format_attr_cyc_thresh.attr,
  101. &format_attr_psb_period.attr,
  102. NULL,
  103. };
  104. static struct attribute_group pt_format_group = {
  105. .name = "format",
  106. .attrs = pt_formats_attr,
  107. };
  108. static const struct attribute_group *pt_attr_groups[] = {
  109. &pt_cap_group,
  110. &pt_format_group,
  111. NULL,
  112. };
  113. static int __init pt_pmu_hw_init(void)
  114. {
  115. struct dev_ext_attribute *de_attrs;
  116. struct attribute **attrs;
  117. size_t size;
  118. int ret;
  119. long i;
  120. attrs = NULL;
  121. for (i = 0; i < PT_CPUID_LEAVES; i++) {
  122. cpuid_count(20, i,
  123. &pt_pmu.caps[CR_EAX + i*PT_CPUID_REGS_NUM],
  124. &pt_pmu.caps[CR_EBX + i*PT_CPUID_REGS_NUM],
  125. &pt_pmu.caps[CR_ECX + i*PT_CPUID_REGS_NUM],
  126. &pt_pmu.caps[CR_EDX + i*PT_CPUID_REGS_NUM]);
  127. }
  128. ret = -ENOMEM;
  129. size = sizeof(struct attribute *) * (ARRAY_SIZE(pt_caps)+1);
  130. attrs = kzalloc(size, GFP_KERNEL);
  131. if (!attrs)
  132. goto fail;
  133. size = sizeof(struct dev_ext_attribute) * (ARRAY_SIZE(pt_caps)+1);
  134. de_attrs = kzalloc(size, GFP_KERNEL);
  135. if (!de_attrs)
  136. goto fail;
  137. for (i = 0; i < ARRAY_SIZE(pt_caps); i++) {
  138. struct dev_ext_attribute *de_attr = de_attrs + i;
  139. de_attr->attr.attr.name = pt_caps[i].name;
  140. sysfs_attr_init(&de_attr->attr.attr);
  141. de_attr->attr.attr.mode = S_IRUGO;
  142. de_attr->attr.show = pt_cap_show;
  143. de_attr->var = (void *)i;
  144. attrs[i] = &de_attr->attr.attr;
  145. }
  146. pt_cap_group.attrs = attrs;
  147. return 0;
  148. fail:
  149. kfree(attrs);
  150. return ret;
  151. }
  152. #define RTIT_CTL_CYC_PSB (RTIT_CTL_CYCLEACC | \
  153. RTIT_CTL_CYC_THRESH | \
  154. RTIT_CTL_PSB_FREQ)
  155. #define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
  156. RTIT_CTL_MTC_RANGE)
  157. #define PT_CONFIG_MASK (RTIT_CTL_TSC_EN | \
  158. RTIT_CTL_DISRETC | \
  159. RTIT_CTL_CYC_PSB | \
  160. RTIT_CTL_MTC)
  161. static bool pt_event_valid(struct perf_event *event)
  162. {
  163. u64 config = event->attr.config;
  164. u64 allowed, requested;
  165. if ((config & PT_CONFIG_MASK) != config)
  166. return false;
  167. if (config & RTIT_CTL_CYC_PSB) {
  168. if (!pt_cap_get(PT_CAP_psb_cyc))
  169. return false;
  170. allowed = pt_cap_get(PT_CAP_psb_periods);
  171. requested = (config & RTIT_CTL_PSB_FREQ) >>
  172. RTIT_CTL_PSB_FREQ_OFFSET;
  173. if (requested && (!(allowed & BIT(requested))))
  174. return false;
  175. allowed = pt_cap_get(PT_CAP_cycle_thresholds);
  176. requested = (config & RTIT_CTL_CYC_THRESH) >>
  177. RTIT_CTL_CYC_THRESH_OFFSET;
  178. if (requested && (!(allowed & BIT(requested))))
  179. return false;
  180. }
  181. if (config & RTIT_CTL_MTC) {
  182. /*
  183. * In the unlikely case that CPUID lists valid mtc periods,
  184. * but not the mtc capability, drop out here.
  185. *
  186. * Spec says that setting mtc period bits while mtc bit in
  187. * CPUID is 0 will #GP, so better safe than sorry.
  188. */
  189. if (!pt_cap_get(PT_CAP_mtc))
  190. return false;
  191. allowed = pt_cap_get(PT_CAP_mtc_periods);
  192. if (!allowed)
  193. return false;
  194. requested = (config & RTIT_CTL_MTC_RANGE) >>
  195. RTIT_CTL_MTC_RANGE_OFFSET;
  196. if (!(allowed & BIT(requested)))
  197. return false;
  198. }
  199. return true;
  200. }
  201. /*
  202. * PT configuration helpers
  203. * These all are cpu affine and operate on a local PT
  204. */
  205. static void pt_config(struct perf_event *event)
  206. {
  207. u64 reg;
  208. if (!event->hw.itrace_started) {
  209. event->hw.itrace_started = 1;
  210. wrmsrl(MSR_IA32_RTIT_STATUS, 0);
  211. }
  212. reg = RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
  213. if (!event->attr.exclude_kernel)
  214. reg |= RTIT_CTL_OS;
  215. if (!event->attr.exclude_user)
  216. reg |= RTIT_CTL_USR;
  217. reg |= (event->attr.config & PT_CONFIG_MASK);
  218. wrmsrl(MSR_IA32_RTIT_CTL, reg);
  219. }
  220. static void pt_config_start(bool start)
  221. {
  222. u64 ctl;
  223. rdmsrl(MSR_IA32_RTIT_CTL, ctl);
  224. if (start)
  225. ctl |= RTIT_CTL_TRACEEN;
  226. else
  227. ctl &= ~RTIT_CTL_TRACEEN;
  228. wrmsrl(MSR_IA32_RTIT_CTL, ctl);
  229. /*
  230. * A wrmsr that disables trace generation serializes other PT
  231. * registers and causes all data packets to be written to memory,
  232. * but a fence is required for the data to become globally visible.
  233. *
  234. * The below WMB, separating data store and aux_head store matches
  235. * the consumer's RMB that separates aux_head load and data load.
  236. */
  237. if (!start)
  238. wmb();
  239. }
  240. static void pt_config_buffer(void *buf, unsigned int topa_idx,
  241. unsigned int output_off)
  242. {
  243. u64 reg;
  244. wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, virt_to_phys(buf));
  245. reg = 0x7f | ((u64)topa_idx << 7) | ((u64)output_off << 32);
  246. wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, reg);
  247. }
  248. /*
  249. * Keep ToPA table-related metadata on the same page as the actual table,
  250. * taking up a few words from the top
  251. */
  252. #define TENTS_PER_PAGE (((PAGE_SIZE - 40) / sizeof(struct topa_entry)) - 1)
  253. /**
  254. * struct topa - page-sized ToPA table with metadata at the top
  255. * @table: actual ToPA table entries, as understood by PT hardware
  256. * @list: linkage to struct pt_buffer's list of tables
  257. * @phys: physical address of this page
  258. * @offset: offset of the first entry in this table in the buffer
  259. * @size: total size of all entries in this table
  260. * @last: index of the last initialized entry in this table
  261. */
  262. struct topa {
  263. struct topa_entry table[TENTS_PER_PAGE];
  264. struct list_head list;
  265. u64 phys;
  266. u64 offset;
  267. size_t size;
  268. int last;
  269. };
  270. /* make -1 stand for the last table entry */
  271. #define TOPA_ENTRY(t, i) ((i) == -1 ? &(t)->table[(t)->last] : &(t)->table[(i)])
  272. /**
  273. * topa_alloc() - allocate page-sized ToPA table
  274. * @cpu: CPU on which to allocate.
  275. * @gfp: Allocation flags.
  276. *
  277. * Return: On success, return the pointer to ToPA table page.
  278. */
  279. static struct topa *topa_alloc(int cpu, gfp_t gfp)
  280. {
  281. int node = cpu_to_node(cpu);
  282. struct topa *topa;
  283. struct page *p;
  284. p = alloc_pages_node(node, gfp | __GFP_ZERO, 0);
  285. if (!p)
  286. return NULL;
  287. topa = page_address(p);
  288. topa->last = 0;
  289. topa->phys = page_to_phys(p);
  290. /*
  291. * In case of singe-entry ToPA, always put the self-referencing END
  292. * link as the 2nd entry in the table
  293. */
  294. if (!pt_cap_get(PT_CAP_topa_multiple_entries)) {
  295. TOPA_ENTRY(topa, 1)->base = topa->phys >> TOPA_SHIFT;
  296. TOPA_ENTRY(topa, 1)->end = 1;
  297. }
  298. return topa;
  299. }
  300. /**
  301. * topa_free() - free a page-sized ToPA table
  302. * @topa: Table to deallocate.
  303. */
  304. static void topa_free(struct topa *topa)
  305. {
  306. free_page((unsigned long)topa);
  307. }
  308. /**
  309. * topa_insert_table() - insert a ToPA table into a buffer
  310. * @buf: PT buffer that's being extended.
  311. * @topa: New topa table to be inserted.
  312. *
  313. * If it's the first table in this buffer, set up buffer's pointers
  314. * accordingly; otherwise, add a END=1 link entry to @topa to the current
  315. * "last" table and adjust the last table pointer to @topa.
  316. */
  317. static void topa_insert_table(struct pt_buffer *buf, struct topa *topa)
  318. {
  319. struct topa *last = buf->last;
  320. list_add_tail(&topa->list, &buf->tables);
  321. if (!buf->first) {
  322. buf->first = buf->last = buf->cur = topa;
  323. return;
  324. }
  325. topa->offset = last->offset + last->size;
  326. buf->last = topa;
  327. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  328. return;
  329. BUG_ON(last->last != TENTS_PER_PAGE - 1);
  330. TOPA_ENTRY(last, -1)->base = topa->phys >> TOPA_SHIFT;
  331. TOPA_ENTRY(last, -1)->end = 1;
  332. }
  333. /**
  334. * topa_table_full() - check if a ToPA table is filled up
  335. * @topa: ToPA table.
  336. */
  337. static bool topa_table_full(struct topa *topa)
  338. {
  339. /* single-entry ToPA is a special case */
  340. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  341. return !!topa->last;
  342. return topa->last == TENTS_PER_PAGE - 1;
  343. }
  344. /**
  345. * topa_insert_pages() - create a list of ToPA tables
  346. * @buf: PT buffer being initialized.
  347. * @gfp: Allocation flags.
  348. *
  349. * This initializes a list of ToPA tables with entries from
  350. * the data_pages provided by rb_alloc_aux().
  351. *
  352. * Return: 0 on success or error code.
  353. */
  354. static int topa_insert_pages(struct pt_buffer *buf, gfp_t gfp)
  355. {
  356. struct topa *topa = buf->last;
  357. int order = 0;
  358. struct page *p;
  359. p = virt_to_page(buf->data_pages[buf->nr_pages]);
  360. if (PagePrivate(p))
  361. order = page_private(p);
  362. if (topa_table_full(topa)) {
  363. topa = topa_alloc(buf->cpu, gfp);
  364. if (!topa)
  365. return -ENOMEM;
  366. topa_insert_table(buf, topa);
  367. }
  368. TOPA_ENTRY(topa, -1)->base = page_to_phys(p) >> TOPA_SHIFT;
  369. TOPA_ENTRY(topa, -1)->size = order;
  370. if (!buf->snapshot && !pt_cap_get(PT_CAP_topa_multiple_entries)) {
  371. TOPA_ENTRY(topa, -1)->intr = 1;
  372. TOPA_ENTRY(topa, -1)->stop = 1;
  373. }
  374. topa->last++;
  375. topa->size += sizes(order);
  376. buf->nr_pages += 1ul << order;
  377. return 0;
  378. }
  379. /**
  380. * pt_topa_dump() - print ToPA tables and their entries
  381. * @buf: PT buffer.
  382. */
  383. static void pt_topa_dump(struct pt_buffer *buf)
  384. {
  385. struct topa *topa;
  386. list_for_each_entry(topa, &buf->tables, list) {
  387. int i;
  388. pr_debug("# table @%p (%016Lx), off %llx size %zx\n", topa->table,
  389. topa->phys, topa->offset, topa->size);
  390. for (i = 0; i < TENTS_PER_PAGE; i++) {
  391. pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
  392. &topa->table[i],
  393. (unsigned long)topa->table[i].base << TOPA_SHIFT,
  394. sizes(topa->table[i].size),
  395. topa->table[i].end ? 'E' : ' ',
  396. topa->table[i].intr ? 'I' : ' ',
  397. topa->table[i].stop ? 'S' : ' ',
  398. *(u64 *)&topa->table[i]);
  399. if ((pt_cap_get(PT_CAP_topa_multiple_entries) &&
  400. topa->table[i].stop) ||
  401. topa->table[i].end)
  402. break;
  403. }
  404. }
  405. }
  406. /**
  407. * pt_buffer_advance() - advance to the next output region
  408. * @buf: PT buffer.
  409. *
  410. * Advance the current pointers in the buffer to the next ToPA entry.
  411. */
  412. static void pt_buffer_advance(struct pt_buffer *buf)
  413. {
  414. buf->output_off = 0;
  415. buf->cur_idx++;
  416. if (buf->cur_idx == buf->cur->last) {
  417. if (buf->cur == buf->last)
  418. buf->cur = buf->first;
  419. else
  420. buf->cur = list_entry(buf->cur->list.next, struct topa,
  421. list);
  422. buf->cur_idx = 0;
  423. }
  424. }
  425. /**
  426. * pt_update_head() - calculate current offsets and sizes
  427. * @pt: Per-cpu pt context.
  428. *
  429. * Update buffer's current write pointer position and data size.
  430. */
  431. static void pt_update_head(struct pt *pt)
  432. {
  433. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  434. u64 topa_idx, base, old;
  435. /* offset of the first region in this table from the beginning of buf */
  436. base = buf->cur->offset + buf->output_off;
  437. /* offset of the current output region within this table */
  438. for (topa_idx = 0; topa_idx < buf->cur_idx; topa_idx++)
  439. base += sizes(buf->cur->table[topa_idx].size);
  440. if (buf->snapshot) {
  441. local_set(&buf->data_size, base);
  442. } else {
  443. old = (local64_xchg(&buf->head, base) &
  444. ((buf->nr_pages << PAGE_SHIFT) - 1));
  445. if (base < old)
  446. base += buf->nr_pages << PAGE_SHIFT;
  447. local_add(base - old, &buf->data_size);
  448. }
  449. }
  450. /**
  451. * pt_buffer_region() - obtain current output region's address
  452. * @buf: PT buffer.
  453. */
  454. static void *pt_buffer_region(struct pt_buffer *buf)
  455. {
  456. return phys_to_virt(buf->cur->table[buf->cur_idx].base << TOPA_SHIFT);
  457. }
  458. /**
  459. * pt_buffer_region_size() - obtain current output region's size
  460. * @buf: PT buffer.
  461. */
  462. static size_t pt_buffer_region_size(struct pt_buffer *buf)
  463. {
  464. return sizes(buf->cur->table[buf->cur_idx].size);
  465. }
  466. /**
  467. * pt_handle_status() - take care of possible status conditions
  468. * @pt: Per-cpu pt context.
  469. */
  470. static void pt_handle_status(struct pt *pt)
  471. {
  472. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  473. int advance = 0;
  474. u64 status;
  475. rdmsrl(MSR_IA32_RTIT_STATUS, status);
  476. if (status & RTIT_STATUS_ERROR) {
  477. pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
  478. pt_topa_dump(buf);
  479. status &= ~RTIT_STATUS_ERROR;
  480. }
  481. if (status & RTIT_STATUS_STOPPED) {
  482. status &= ~RTIT_STATUS_STOPPED;
  483. /*
  484. * On systems that only do single-entry ToPA, hitting STOP
  485. * means we are already losing data; need to let the decoder
  486. * know.
  487. */
  488. if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
  489. buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
  490. local_inc(&buf->lost);
  491. advance++;
  492. }
  493. }
  494. /*
  495. * Also on single-entry ToPA implementations, interrupt will come
  496. * before the output reaches its output region's boundary.
  497. */
  498. if (!pt_cap_get(PT_CAP_topa_multiple_entries) && !buf->snapshot &&
  499. pt_buffer_region_size(buf) - buf->output_off <= TOPA_PMI_MARGIN) {
  500. void *head = pt_buffer_region(buf);
  501. /* everything within this margin needs to be zeroed out */
  502. memset(head + buf->output_off, 0,
  503. pt_buffer_region_size(buf) -
  504. buf->output_off);
  505. advance++;
  506. }
  507. if (advance)
  508. pt_buffer_advance(buf);
  509. wrmsrl(MSR_IA32_RTIT_STATUS, status);
  510. }
  511. /**
  512. * pt_read_offset() - translate registers into buffer pointers
  513. * @buf: PT buffer.
  514. *
  515. * Set buffer's output pointers from MSR values.
  516. */
  517. static void pt_read_offset(struct pt_buffer *buf)
  518. {
  519. u64 offset, base_topa;
  520. rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, base_topa);
  521. buf->cur = phys_to_virt(base_topa);
  522. rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, offset);
  523. /* offset within current output region */
  524. buf->output_off = offset >> 32;
  525. /* index of current output region within this table */
  526. buf->cur_idx = (offset & 0xffffff80) >> 7;
  527. }
  528. /**
  529. * pt_topa_next_entry() - obtain index of the first page in the next ToPA entry
  530. * @buf: PT buffer.
  531. * @pg: Page offset in the buffer.
  532. *
  533. * When advancing to the next output region (ToPA entry), given a page offset
  534. * into the buffer, we need to find the offset of the first page in the next
  535. * region.
  536. */
  537. static unsigned int pt_topa_next_entry(struct pt_buffer *buf, unsigned int pg)
  538. {
  539. struct topa_entry *te = buf->topa_index[pg];
  540. /* one region */
  541. if (buf->first == buf->last && buf->first->last == 1)
  542. return pg;
  543. do {
  544. pg++;
  545. pg &= buf->nr_pages - 1;
  546. } while (buf->topa_index[pg] == te);
  547. return pg;
  548. }
  549. /**
  550. * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
  551. * @buf: PT buffer.
  552. * @handle: Current output handle.
  553. *
  554. * Place INT and STOP marks to prevent overwriting old data that the consumer
  555. * hasn't yet collected and waking up the consumer after a certain fraction of
  556. * the buffer has filled up. Only needed and sensible for non-snapshot counters.
  557. *
  558. * This obviously relies on buf::head to figure out buffer markers, so it has
  559. * to be called after pt_buffer_reset_offsets() and before the hardware tracing
  560. * is enabled.
  561. */
  562. static int pt_buffer_reset_markers(struct pt_buffer *buf,
  563. struct perf_output_handle *handle)
  564. {
  565. unsigned long head = local64_read(&buf->head);
  566. unsigned long idx, npages, wakeup;
  567. /* can't stop in the middle of an output region */
  568. if (buf->output_off + handle->size + 1 <
  569. sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
  570. return -EINVAL;
  571. /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
  572. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  573. return 0;
  574. /* clear STOP and INT from current entry */
  575. buf->topa_index[buf->stop_pos]->stop = 0;
  576. buf->topa_index[buf->intr_pos]->intr = 0;
  577. /* how many pages till the STOP marker */
  578. npages = handle->size >> PAGE_SHIFT;
  579. /* if it's on a page boundary, fill up one more page */
  580. if (!offset_in_page(head + handle->size + 1))
  581. npages++;
  582. idx = (head >> PAGE_SHIFT) + npages;
  583. idx &= buf->nr_pages - 1;
  584. buf->stop_pos = idx;
  585. wakeup = handle->wakeup >> PAGE_SHIFT;
  586. /* in the worst case, wake up the consumer one page before hard stop */
  587. idx = (head >> PAGE_SHIFT) + npages - 1;
  588. if (idx > wakeup)
  589. idx = wakeup;
  590. idx &= buf->nr_pages - 1;
  591. buf->intr_pos = idx;
  592. buf->topa_index[buf->stop_pos]->stop = 1;
  593. buf->topa_index[buf->intr_pos]->intr = 1;
  594. return 0;
  595. }
  596. /**
  597. * pt_buffer_setup_topa_index() - build topa_index[] table of regions
  598. * @buf: PT buffer.
  599. *
  600. * topa_index[] references output regions indexed by offset into the
  601. * buffer for purposes of quick reverse lookup.
  602. */
  603. static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
  604. {
  605. struct topa *cur = buf->first, *prev = buf->last;
  606. struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
  607. *te_prev = TOPA_ENTRY(prev, prev->last - 1);
  608. int pg = 0, idx = 0;
  609. while (pg < buf->nr_pages) {
  610. int tidx;
  611. /* pages within one topa entry */
  612. for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
  613. buf->topa_index[pg] = te_prev;
  614. te_prev = te_cur;
  615. if (idx == cur->last - 1) {
  616. /* advance to next topa table */
  617. idx = 0;
  618. cur = list_entry(cur->list.next, struct topa, list);
  619. } else {
  620. idx++;
  621. }
  622. te_cur = TOPA_ENTRY(cur, idx);
  623. }
  624. }
  625. /**
  626. * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
  627. * @buf: PT buffer.
  628. * @head: Write pointer (aux_head) from AUX buffer.
  629. *
  630. * Find the ToPA table and entry corresponding to given @head and set buffer's
  631. * "current" pointers accordingly. This is done after we have obtained the
  632. * current aux_head position from a successful call to perf_aux_output_begin()
  633. * to make sure the hardware is writing to the right place.
  634. *
  635. * This function modifies buf::{cur,cur_idx,output_off} that will be programmed
  636. * into PT msrs when the tracing is enabled and buf::head and buf::data_size,
  637. * which are used to determine INT and STOP markers' locations by a subsequent
  638. * call to pt_buffer_reset_markers().
  639. */
  640. static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
  641. {
  642. int pg;
  643. if (buf->snapshot)
  644. head &= (buf->nr_pages << PAGE_SHIFT) - 1;
  645. pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
  646. pg = pt_topa_next_entry(buf, pg);
  647. buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
  648. buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
  649. (unsigned long)buf->cur) / sizeof(struct topa_entry);
  650. buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
  651. local64_set(&buf->head, head);
  652. local_set(&buf->data_size, 0);
  653. }
  654. /**
  655. * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
  656. * @buf: PT buffer.
  657. */
  658. static void pt_buffer_fini_topa(struct pt_buffer *buf)
  659. {
  660. struct topa *topa, *iter;
  661. list_for_each_entry_safe(topa, iter, &buf->tables, list) {
  662. /*
  663. * right now, this is in free_aux() path only, so
  664. * no need to unlink this table from the list
  665. */
  666. topa_free(topa);
  667. }
  668. }
  669. /**
  670. * pt_buffer_init_topa() - initialize ToPA table for pt buffer
  671. * @buf: PT buffer.
  672. * @size: Total size of all regions within this ToPA.
  673. * @gfp: Allocation flags.
  674. */
  675. static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
  676. gfp_t gfp)
  677. {
  678. struct topa *topa;
  679. int err;
  680. topa = topa_alloc(buf->cpu, gfp);
  681. if (!topa)
  682. return -ENOMEM;
  683. topa_insert_table(buf, topa);
  684. while (buf->nr_pages < nr_pages) {
  685. err = topa_insert_pages(buf, gfp);
  686. if (err) {
  687. pt_buffer_fini_topa(buf);
  688. return -ENOMEM;
  689. }
  690. }
  691. pt_buffer_setup_topa_index(buf);
  692. /* link last table to the first one, unless we're double buffering */
  693. if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
  694. TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
  695. TOPA_ENTRY(buf->last, -1)->end = 1;
  696. }
  697. pt_topa_dump(buf);
  698. return 0;
  699. }
  700. /**
  701. * pt_buffer_setup_aux() - set up topa tables for a PT buffer
  702. * @cpu: Cpu on which to allocate, -1 means current.
  703. * @pages: Array of pointers to buffer pages passed from perf core.
  704. * @nr_pages: Number of pages in the buffer.
  705. * @snapshot: If this is a snapshot/overwrite counter.
  706. *
  707. * This is a pmu::setup_aux callback that sets up ToPA tables and all the
  708. * bookkeeping for an AUX buffer.
  709. *
  710. * Return: Our private PT buffer structure.
  711. */
  712. static void *
  713. pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
  714. {
  715. struct pt_buffer *buf;
  716. int node, ret;
  717. if (!nr_pages)
  718. return NULL;
  719. if (cpu == -1)
  720. cpu = raw_smp_processor_id();
  721. node = cpu_to_node(cpu);
  722. buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
  723. GFP_KERNEL, node);
  724. if (!buf)
  725. return NULL;
  726. buf->cpu = cpu;
  727. buf->snapshot = snapshot;
  728. buf->data_pages = pages;
  729. INIT_LIST_HEAD(&buf->tables);
  730. ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
  731. if (ret) {
  732. kfree(buf);
  733. return NULL;
  734. }
  735. return buf;
  736. }
  737. /**
  738. * pt_buffer_free_aux() - perf AUX deallocation path callback
  739. * @data: PT buffer.
  740. */
  741. static void pt_buffer_free_aux(void *data)
  742. {
  743. struct pt_buffer *buf = data;
  744. pt_buffer_fini_topa(buf);
  745. kfree(buf);
  746. }
  747. /**
  748. * pt_buffer_is_full() - check if the buffer is full
  749. * @buf: PT buffer.
  750. * @pt: Per-cpu pt handle.
  751. *
  752. * If the user hasn't read data from the output region that aux_head
  753. * points to, the buffer is considered full: the user needs to read at
  754. * least this region and update aux_tail to point past it.
  755. */
  756. static bool pt_buffer_is_full(struct pt_buffer *buf, struct pt *pt)
  757. {
  758. if (buf->snapshot)
  759. return false;
  760. if (local_read(&buf->data_size) >= pt->handle.size)
  761. return true;
  762. return false;
  763. }
  764. /**
  765. * intel_pt_interrupt() - PT PMI handler
  766. */
  767. void intel_pt_interrupt(void)
  768. {
  769. struct pt *pt = this_cpu_ptr(&pt_ctx);
  770. struct pt_buffer *buf;
  771. struct perf_event *event = pt->handle.event;
  772. /*
  773. * There may be a dangling PT bit in the interrupt status register
  774. * after PT has been disabled by pt_event_stop(). Make sure we don't
  775. * do anything (particularly, re-enable) for this event here.
  776. */
  777. if (!ACCESS_ONCE(pt->handle_nmi))
  778. return;
  779. pt_config_start(false);
  780. if (!event)
  781. return;
  782. buf = perf_get_aux(&pt->handle);
  783. if (!buf)
  784. return;
  785. pt_read_offset(buf);
  786. pt_handle_status(pt);
  787. pt_update_head(pt);
  788. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  789. local_xchg(&buf->lost, 0));
  790. if (!event->hw.state) {
  791. int ret;
  792. buf = perf_aux_output_begin(&pt->handle, event);
  793. if (!buf) {
  794. event->hw.state = PERF_HES_STOPPED;
  795. return;
  796. }
  797. pt_buffer_reset_offsets(buf, pt->handle.head);
  798. /* snapshot counters don't use PMI, so it's safe */
  799. ret = pt_buffer_reset_markers(buf, &pt->handle);
  800. if (ret) {
  801. perf_aux_output_end(&pt->handle, 0, true);
  802. return;
  803. }
  804. pt_config_buffer(buf->cur->table, buf->cur_idx,
  805. buf->output_off);
  806. pt_config(event);
  807. }
  808. }
  809. /*
  810. * PMU callbacks
  811. */
  812. static void pt_event_start(struct perf_event *event, int mode)
  813. {
  814. struct pt *pt = this_cpu_ptr(&pt_ctx);
  815. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  816. if (!buf || pt_buffer_is_full(buf, pt)) {
  817. event->hw.state = PERF_HES_STOPPED;
  818. return;
  819. }
  820. ACCESS_ONCE(pt->handle_nmi) = 1;
  821. event->hw.state = 0;
  822. pt_config_buffer(buf->cur->table, buf->cur_idx,
  823. buf->output_off);
  824. pt_config(event);
  825. }
  826. static void pt_event_stop(struct perf_event *event, int mode)
  827. {
  828. struct pt *pt = this_cpu_ptr(&pt_ctx);
  829. /*
  830. * Protect against the PMI racing with disabling wrmsr,
  831. * see comment in intel_pt_interrupt().
  832. */
  833. ACCESS_ONCE(pt->handle_nmi) = 0;
  834. pt_config_start(false);
  835. if (event->hw.state == PERF_HES_STOPPED)
  836. return;
  837. event->hw.state = PERF_HES_STOPPED;
  838. if (mode & PERF_EF_UPDATE) {
  839. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  840. if (!buf)
  841. return;
  842. if (WARN_ON_ONCE(pt->handle.event != event))
  843. return;
  844. pt_read_offset(buf);
  845. pt_handle_status(pt);
  846. pt_update_head(pt);
  847. }
  848. }
  849. static void pt_event_del(struct perf_event *event, int mode)
  850. {
  851. struct pt *pt = this_cpu_ptr(&pt_ctx);
  852. struct pt_buffer *buf;
  853. pt_event_stop(event, PERF_EF_UPDATE);
  854. buf = perf_get_aux(&pt->handle);
  855. if (buf) {
  856. if (buf->snapshot)
  857. pt->handle.head =
  858. local_xchg(&buf->data_size,
  859. buf->nr_pages << PAGE_SHIFT);
  860. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  861. local_xchg(&buf->lost, 0));
  862. }
  863. }
  864. static int pt_event_add(struct perf_event *event, int mode)
  865. {
  866. struct pt_buffer *buf;
  867. struct pt *pt = this_cpu_ptr(&pt_ctx);
  868. struct hw_perf_event *hwc = &event->hw;
  869. int ret = -EBUSY;
  870. if (pt->handle.event)
  871. goto fail;
  872. buf = perf_aux_output_begin(&pt->handle, event);
  873. ret = -EINVAL;
  874. if (!buf)
  875. goto fail_stop;
  876. pt_buffer_reset_offsets(buf, pt->handle.head);
  877. if (!buf->snapshot) {
  878. ret = pt_buffer_reset_markers(buf, &pt->handle);
  879. if (ret)
  880. goto fail_end_stop;
  881. }
  882. if (mode & PERF_EF_START) {
  883. pt_event_start(event, 0);
  884. ret = -EBUSY;
  885. if (hwc->state == PERF_HES_STOPPED)
  886. goto fail_end_stop;
  887. } else {
  888. hwc->state = PERF_HES_STOPPED;
  889. }
  890. return 0;
  891. fail_end_stop:
  892. perf_aux_output_end(&pt->handle, 0, true);
  893. fail_stop:
  894. hwc->state = PERF_HES_STOPPED;
  895. fail:
  896. return ret;
  897. }
  898. static void pt_event_read(struct perf_event *event)
  899. {
  900. }
  901. static void pt_event_destroy(struct perf_event *event)
  902. {
  903. x86_del_exclusive(x86_lbr_exclusive_pt);
  904. }
  905. static int pt_event_init(struct perf_event *event)
  906. {
  907. if (event->attr.type != pt_pmu.pmu.type)
  908. return -ENOENT;
  909. if (!pt_event_valid(event))
  910. return -EINVAL;
  911. if (x86_add_exclusive(x86_lbr_exclusive_pt))
  912. return -EBUSY;
  913. event->destroy = pt_event_destroy;
  914. return 0;
  915. }
  916. static __init int pt_init(void)
  917. {
  918. int ret, cpu, prior_warn = 0;
  919. BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
  920. if (!test_cpu_cap(&boot_cpu_data, X86_FEATURE_INTEL_PT))
  921. return -ENODEV;
  922. get_online_cpus();
  923. for_each_online_cpu(cpu) {
  924. u64 ctl;
  925. ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
  926. if (!ret && (ctl & RTIT_CTL_TRACEEN))
  927. prior_warn++;
  928. }
  929. put_online_cpus();
  930. if (prior_warn) {
  931. x86_add_exclusive(x86_lbr_exclusive_pt);
  932. pr_warn("PT is enabled at boot time, doing nothing\n");
  933. return -EBUSY;
  934. }
  935. ret = pt_pmu_hw_init();
  936. if (ret)
  937. return ret;
  938. if (!pt_cap_get(PT_CAP_topa_output)) {
  939. pr_warn("ToPA output is not supported on this CPU\n");
  940. return -ENODEV;
  941. }
  942. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  943. pt_pmu.pmu.capabilities =
  944. PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
  945. pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
  946. pt_pmu.pmu.attr_groups = pt_attr_groups;
  947. pt_pmu.pmu.task_ctx_nr = perf_sw_context;
  948. pt_pmu.pmu.event_init = pt_event_init;
  949. pt_pmu.pmu.add = pt_event_add;
  950. pt_pmu.pmu.del = pt_event_del;
  951. pt_pmu.pmu.start = pt_event_start;
  952. pt_pmu.pmu.stop = pt_event_stop;
  953. pt_pmu.pmu.read = pt_event_read;
  954. pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
  955. pt_pmu.pmu.free_aux = pt_buffer_free_aux;
  956. ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
  957. return ret;
  958. }
  959. arch_initcall(pt_init);