perf_event_intel_pt.c 28 KB

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