perf_event_intel_pt.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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_attrs->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 idx, npages, end;
  509. if (buf->snapshot)
  510. return 0;
  511. /* can't stop in the middle of an output region */
  512. if (buf->output_off + handle->size + 1 <
  513. sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
  514. return -EINVAL;
  515. /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
  516. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  517. return 0;
  518. /* clear STOP and INT from current entry */
  519. buf->topa_index[buf->stop_pos]->stop = 0;
  520. buf->topa_index[buf->intr_pos]->intr = 0;
  521. if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
  522. npages = (handle->size + 1) >> PAGE_SHIFT;
  523. end = (local64_read(&buf->head) >> PAGE_SHIFT) + npages;
  524. /*if (end > handle->wakeup >> PAGE_SHIFT)
  525. end = handle->wakeup >> PAGE_SHIFT;*/
  526. idx = end & (buf->nr_pages - 1);
  527. buf->stop_pos = idx;
  528. idx = (local64_read(&buf->head) >> PAGE_SHIFT) + npages - 1;
  529. idx &= buf->nr_pages - 1;
  530. buf->intr_pos = idx;
  531. }
  532. buf->topa_index[buf->stop_pos]->stop = 1;
  533. buf->topa_index[buf->intr_pos]->intr = 1;
  534. return 0;
  535. }
  536. /**
  537. * pt_buffer_setup_topa_index() - build topa_index[] table of regions
  538. * @buf: PT buffer.
  539. *
  540. * topa_index[] references output regions indexed by offset into the
  541. * buffer for purposes of quick reverse lookup.
  542. */
  543. static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
  544. {
  545. struct topa *cur = buf->first, *prev = buf->last;
  546. struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
  547. *te_prev = TOPA_ENTRY(prev, prev->last - 1);
  548. int pg = 0, idx = 0, ntopa = 0;
  549. while (pg < buf->nr_pages) {
  550. int tidx;
  551. /* pages within one topa entry */
  552. for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
  553. buf->topa_index[pg] = te_prev;
  554. te_prev = te_cur;
  555. if (idx == cur->last - 1) {
  556. /* advance to next topa table */
  557. idx = 0;
  558. cur = list_entry(cur->list.next, struct topa, list);
  559. ntopa++;
  560. } else
  561. idx++;
  562. te_cur = TOPA_ENTRY(cur, idx);
  563. }
  564. }
  565. /**
  566. * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
  567. * @buf: PT buffer.
  568. * @head: Write pointer (aux_head) from AUX buffer.
  569. *
  570. * Find the ToPA table and entry corresponding to given @head and set buffer's
  571. * "current" pointers accordingly.
  572. */
  573. static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
  574. {
  575. int pg;
  576. if (buf->snapshot)
  577. head &= (buf->nr_pages << PAGE_SHIFT) - 1;
  578. pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
  579. pg = pt_topa_next_entry(buf, pg);
  580. buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
  581. buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
  582. (unsigned long)buf->cur) / sizeof(struct topa_entry);
  583. buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
  584. local64_set(&buf->head, head);
  585. local_set(&buf->data_size, 0);
  586. }
  587. /**
  588. * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
  589. * @buf: PT buffer.
  590. */
  591. static void pt_buffer_fini_topa(struct pt_buffer *buf)
  592. {
  593. struct topa *topa, *iter;
  594. list_for_each_entry_safe(topa, iter, &buf->tables, list) {
  595. /*
  596. * right now, this is in free_aux() path only, so
  597. * no need to unlink this table from the list
  598. */
  599. topa_free(topa);
  600. }
  601. }
  602. /**
  603. * pt_buffer_init_topa() - initialize ToPA table for pt buffer
  604. * @buf: PT buffer.
  605. * @size: Total size of all regions within this ToPA.
  606. * @gfp: Allocation flags.
  607. */
  608. static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
  609. gfp_t gfp)
  610. {
  611. struct topa *topa;
  612. int err;
  613. topa = topa_alloc(buf->cpu, gfp);
  614. if (!topa)
  615. return -ENOMEM;
  616. topa_insert_table(buf, topa);
  617. while (buf->nr_pages < nr_pages) {
  618. err = topa_insert_pages(buf, gfp);
  619. if (err) {
  620. pt_buffer_fini_topa(buf);
  621. return -ENOMEM;
  622. }
  623. }
  624. pt_buffer_setup_topa_index(buf);
  625. /* link last table to the first one, unless we're double buffering */
  626. if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
  627. TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
  628. TOPA_ENTRY(buf->last, -1)->end = 1;
  629. }
  630. pt_topa_dump(buf);
  631. return 0;
  632. }
  633. /**
  634. * pt_buffer_setup_aux() - set up topa tables for a PT buffer
  635. * @cpu: Cpu on which to allocate, -1 means current.
  636. * @pages: Array of pointers to buffer pages passed from perf core.
  637. * @nr_pages: Number of pages in the buffer.
  638. * @snapshot: If this is a snapshot/overwrite counter.
  639. *
  640. * This is a pmu::setup_aux callback that sets up ToPA tables and all the
  641. * bookkeeping for an AUX buffer.
  642. *
  643. * Return: Our private PT buffer structure.
  644. */
  645. static void *
  646. pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
  647. {
  648. struct pt_buffer *buf;
  649. int node, ret;
  650. if (!nr_pages)
  651. return NULL;
  652. if (cpu == -1)
  653. cpu = raw_smp_processor_id();
  654. node = cpu_to_node(cpu);
  655. buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
  656. GFP_KERNEL, node);
  657. if (!buf)
  658. return NULL;
  659. buf->cpu = cpu;
  660. buf->snapshot = snapshot;
  661. buf->data_pages = pages;
  662. INIT_LIST_HEAD(&buf->tables);
  663. ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
  664. if (ret) {
  665. kfree(buf);
  666. return NULL;
  667. }
  668. return buf;
  669. }
  670. /**
  671. * pt_buffer_free_aux() - perf AUX deallocation path callback
  672. * @data: PT buffer.
  673. */
  674. static void pt_buffer_free_aux(void *data)
  675. {
  676. struct pt_buffer *buf = data;
  677. pt_buffer_fini_topa(buf);
  678. kfree(buf);
  679. }
  680. /**
  681. * pt_buffer_is_full() - check if the buffer is full
  682. * @buf: PT buffer.
  683. * @pt: Per-cpu pt handle.
  684. *
  685. * If the user hasn't read data from the output region that aux_head
  686. * points to, the buffer is considered full: the user needs to read at
  687. * least this region and update aux_tail to point past it.
  688. */
  689. static bool pt_buffer_is_full(struct pt_buffer *buf, struct pt *pt)
  690. {
  691. if (buf->snapshot)
  692. return false;
  693. if (local_read(&buf->data_size) >= pt->handle.size)
  694. return true;
  695. return false;
  696. }
  697. /**
  698. * intel_pt_interrupt() - PT PMI handler
  699. */
  700. void intel_pt_interrupt(void)
  701. {
  702. struct pt *pt = this_cpu_ptr(&pt_ctx);
  703. struct pt_buffer *buf;
  704. struct perf_event *event = pt->handle.event;
  705. /*
  706. * There may be a dangling PT bit in the interrupt status register
  707. * after PT has been disabled by pt_event_stop(). Make sure we don't
  708. * do anything (particularly, re-enable) for this event here.
  709. */
  710. if (!ACCESS_ONCE(pt->handle_nmi))
  711. return;
  712. pt_config_start(false);
  713. if (!event)
  714. return;
  715. buf = perf_get_aux(&pt->handle);
  716. if (!buf)
  717. return;
  718. pt_read_offset(buf);
  719. pt_handle_status(pt);
  720. pt_update_head(pt);
  721. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  722. local_xchg(&buf->lost, 0));
  723. if (!event->hw.state) {
  724. int ret;
  725. buf = perf_aux_output_begin(&pt->handle, event);
  726. if (!buf) {
  727. event->hw.state = PERF_HES_STOPPED;
  728. return;
  729. }
  730. pt_buffer_reset_offsets(buf, pt->handle.head);
  731. ret = pt_buffer_reset_markers(buf, &pt->handle);
  732. if (ret) {
  733. perf_aux_output_end(&pt->handle, 0, true);
  734. return;
  735. }
  736. pt_config_buffer(buf->cur->table, buf->cur_idx,
  737. buf->output_off);
  738. wrmsrl(MSR_IA32_RTIT_STATUS, 0);
  739. pt_config(event);
  740. }
  741. }
  742. /*
  743. * PMU callbacks
  744. */
  745. static void pt_event_start(struct perf_event *event, int mode)
  746. {
  747. struct pt *pt = this_cpu_ptr(&pt_ctx);
  748. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  749. if (pt_is_running() || !buf || pt_buffer_is_full(buf, pt)) {
  750. event->hw.state = PERF_HES_STOPPED;
  751. return;
  752. }
  753. ACCESS_ONCE(pt->handle_nmi) = 1;
  754. event->hw.state = 0;
  755. pt_config_buffer(buf->cur->table, buf->cur_idx,
  756. buf->output_off);
  757. wrmsrl(MSR_IA32_RTIT_STATUS, 0);
  758. pt_config(event);
  759. }
  760. static void pt_event_stop(struct perf_event *event, int mode)
  761. {
  762. struct pt *pt = this_cpu_ptr(&pt_ctx);
  763. /*
  764. * Protect against the PMI racing with disabling wrmsr,
  765. * see comment in intel_pt_interrupt().
  766. */
  767. ACCESS_ONCE(pt->handle_nmi) = 0;
  768. pt_config_start(false);
  769. if (event->hw.state == PERF_HES_STOPPED)
  770. return;
  771. event->hw.state = PERF_HES_STOPPED;
  772. if (mode & PERF_EF_UPDATE) {
  773. struct pt *pt = this_cpu_ptr(&pt_ctx);
  774. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  775. if (!buf)
  776. return;
  777. if (WARN_ON_ONCE(pt->handle.event != event))
  778. return;
  779. pt_read_offset(buf);
  780. pt_handle_status(pt);
  781. pt_update_head(pt);
  782. }
  783. }
  784. static void pt_event_del(struct perf_event *event, int mode)
  785. {
  786. struct pt *pt = this_cpu_ptr(&pt_ctx);
  787. struct pt_buffer *buf;
  788. pt_event_stop(event, PERF_EF_UPDATE);
  789. buf = perf_get_aux(&pt->handle);
  790. if (buf) {
  791. if (buf->snapshot)
  792. pt->handle.head =
  793. local_xchg(&buf->data_size,
  794. buf->nr_pages << PAGE_SHIFT);
  795. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  796. local_xchg(&buf->lost, 0));
  797. }
  798. }
  799. static int pt_event_add(struct perf_event *event, int mode)
  800. {
  801. struct pt_buffer *buf;
  802. struct pt *pt = this_cpu_ptr(&pt_ctx);
  803. struct hw_perf_event *hwc = &event->hw;
  804. int ret = -EBUSY;
  805. if (pt->handle.event)
  806. goto fail;
  807. buf = perf_aux_output_begin(&pt->handle, event);
  808. ret = -EINVAL;
  809. if (!buf)
  810. goto fail_stop;
  811. pt_buffer_reset_offsets(buf, pt->handle.head);
  812. if (!buf->snapshot) {
  813. ret = pt_buffer_reset_markers(buf, &pt->handle);
  814. if (ret)
  815. goto fail_end_stop;
  816. }
  817. if (mode & PERF_EF_START) {
  818. pt_event_start(event, 0);
  819. ret = -EBUSY;
  820. if (hwc->state == PERF_HES_STOPPED)
  821. goto fail_end_stop;
  822. } else {
  823. hwc->state = PERF_HES_STOPPED;
  824. }
  825. return 0;
  826. fail_end_stop:
  827. perf_aux_output_end(&pt->handle, 0, true);
  828. fail_stop:
  829. hwc->state = PERF_HES_STOPPED;
  830. fail:
  831. return ret;
  832. }
  833. static void pt_event_read(struct perf_event *event)
  834. {
  835. }
  836. static void pt_event_destroy(struct perf_event *event)
  837. {
  838. x86_del_exclusive(x86_lbr_exclusive_pt);
  839. }
  840. static int pt_event_init(struct perf_event *event)
  841. {
  842. if (event->attr.type != pt_pmu.pmu.type)
  843. return -ENOENT;
  844. if (!pt_event_valid(event))
  845. return -EINVAL;
  846. if (x86_add_exclusive(x86_lbr_exclusive_pt))
  847. return -EBUSY;
  848. event->destroy = pt_event_destroy;
  849. return 0;
  850. }
  851. static __init int pt_init(void)
  852. {
  853. int ret, cpu, prior_warn = 0;
  854. BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
  855. get_online_cpus();
  856. for_each_online_cpu(cpu) {
  857. u64 ctl;
  858. ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
  859. if (!ret && (ctl & RTIT_CTL_TRACEEN))
  860. prior_warn++;
  861. }
  862. put_online_cpus();
  863. if (prior_warn) {
  864. x86_add_exclusive(x86_lbr_exclusive_pt);
  865. pr_warn("PT is enabled at boot time, doing nothing\n");
  866. return -EBUSY;
  867. }
  868. ret = pt_pmu_hw_init();
  869. if (ret)
  870. return ret;
  871. if (!pt_cap_get(PT_CAP_topa_output)) {
  872. pr_warn("ToPA output is not supported on this CPU\n");
  873. return -ENODEV;
  874. }
  875. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  876. pt_pmu.pmu.capabilities =
  877. PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
  878. pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
  879. pt_pmu.pmu.attr_groups = pt_attr_groups;
  880. pt_pmu.pmu.task_ctx_nr = perf_sw_context;
  881. pt_pmu.pmu.event_init = pt_event_init;
  882. pt_pmu.pmu.add = pt_event_add;
  883. pt_pmu.pmu.del = pt_event_del;
  884. pt_pmu.pmu.start = pt_event_start;
  885. pt_pmu.pmu.stop = pt_event_stop;
  886. pt_pmu.pmu.read = pt_event_read;
  887. pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
  888. pt_pmu.pmu.free_aux = pt_buffer_free_aux;
  889. ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
  890. return ret;
  891. }
  892. module_init(pt_init);