perf_event_intel_pt.c 26 KB

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