perf_event_intel_pt.c 28 KB

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