perf_event_intel_pt.c 26 KB

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