perf_event_intel_pt.c 28 KB

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