pt.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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 "pt.h"
  29. static DEFINE_PER_CPU(struct pt, pt_ctx);
  30. static struct pt_pmu pt_pmu;
  31. /*
  32. * Capabilities of Intel PT hardware, such as number of address bits or
  33. * supported output schemes, are cached and exported to userspace as "caps"
  34. * attribute group of pt pmu device
  35. * (/sys/bus/event_source/devices/intel_pt/caps/) so that userspace can store
  36. * relevant bits together with intel_pt traces.
  37. *
  38. * These are necessary for both trace decoding (payloads_lip, contains address
  39. * width encoded in IP-related packets), and event configuration (bitmasks with
  40. * permitted values for certain bit fields).
  41. */
  42. #define PT_CAP(_n, _l, _r, _m) \
  43. [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
  44. .reg = _r, .mask = _m }
  45. static struct pt_cap_desc {
  46. const char *name;
  47. u32 leaf;
  48. u8 reg;
  49. u32 mask;
  50. } pt_caps[] = {
  51. PT_CAP(max_subleaf, 0, CPUID_EAX, 0xffffffff),
  52. PT_CAP(cr3_filtering, 0, CPUID_EBX, BIT(0)),
  53. PT_CAP(psb_cyc, 0, CPUID_EBX, BIT(1)),
  54. PT_CAP(ip_filtering, 0, CPUID_EBX, BIT(2)),
  55. PT_CAP(mtc, 0, CPUID_EBX, BIT(3)),
  56. PT_CAP(ptwrite, 0, CPUID_EBX, BIT(4)),
  57. PT_CAP(power_event_trace, 0, CPUID_EBX, BIT(5)),
  58. PT_CAP(topa_output, 0, CPUID_ECX, BIT(0)),
  59. PT_CAP(topa_multiple_entries, 0, CPUID_ECX, BIT(1)),
  60. PT_CAP(single_range_output, 0, CPUID_ECX, BIT(2)),
  61. PT_CAP(payloads_lip, 0, CPUID_ECX, BIT(31)),
  62. PT_CAP(num_address_ranges, 1, CPUID_EAX, 0x3),
  63. PT_CAP(mtc_periods, 1, CPUID_EAX, 0xffff0000),
  64. PT_CAP(cycle_thresholds, 1, CPUID_EBX, 0xffff),
  65. PT_CAP(psb_periods, 1, CPUID_EBX, 0xffff0000),
  66. };
  67. static u32 pt_cap_get(enum pt_capabilities cap)
  68. {
  69. struct pt_cap_desc *cd = &pt_caps[cap];
  70. u32 c = pt_pmu.caps[cd->leaf * PT_CPUID_REGS_NUM + cd->reg];
  71. unsigned int shift = __ffs(cd->mask);
  72. return (c & cd->mask) >> shift;
  73. }
  74. static ssize_t pt_cap_show(struct device *cdev,
  75. struct device_attribute *attr,
  76. char *buf)
  77. {
  78. struct dev_ext_attribute *ea =
  79. container_of(attr, struct dev_ext_attribute, attr);
  80. enum pt_capabilities cap = (long)ea->var;
  81. return snprintf(buf, PAGE_SIZE, "%x\n", pt_cap_get(cap));
  82. }
  83. static struct attribute_group pt_cap_group = {
  84. .name = "caps",
  85. };
  86. PMU_FORMAT_ATTR(cyc, "config:1" );
  87. PMU_FORMAT_ATTR(mtc, "config:9" );
  88. PMU_FORMAT_ATTR(tsc, "config:10" );
  89. PMU_FORMAT_ATTR(noretcomp, "config:11" );
  90. PMU_FORMAT_ATTR(mtc_period, "config:14-17" );
  91. PMU_FORMAT_ATTR(cyc_thresh, "config:19-22" );
  92. PMU_FORMAT_ATTR(psb_period, "config:24-27" );
  93. static struct attribute *pt_formats_attr[] = {
  94. &format_attr_cyc.attr,
  95. &format_attr_mtc.attr,
  96. &format_attr_tsc.attr,
  97. &format_attr_noretcomp.attr,
  98. &format_attr_mtc_period.attr,
  99. &format_attr_cyc_thresh.attr,
  100. &format_attr_psb_period.attr,
  101. NULL,
  102. };
  103. static struct attribute_group pt_format_group = {
  104. .name = "format",
  105. .attrs = pt_formats_attr,
  106. };
  107. static ssize_t
  108. pt_timing_attr_show(struct device *dev, struct device_attribute *attr,
  109. char *page)
  110. {
  111. struct perf_pmu_events_attr *pmu_attr =
  112. container_of(attr, struct perf_pmu_events_attr, attr);
  113. switch (pmu_attr->id) {
  114. case 0:
  115. return sprintf(page, "%lu\n", pt_pmu.max_nonturbo_ratio);
  116. case 1:
  117. return sprintf(page, "%u:%u\n",
  118. pt_pmu.tsc_art_num,
  119. pt_pmu.tsc_art_den);
  120. default:
  121. break;
  122. }
  123. return -EINVAL;
  124. }
  125. PMU_EVENT_ATTR(max_nonturbo_ratio, timing_attr_max_nonturbo_ratio, 0,
  126. pt_timing_attr_show);
  127. PMU_EVENT_ATTR(tsc_art_ratio, timing_attr_tsc_art_ratio, 1,
  128. pt_timing_attr_show);
  129. static struct attribute *pt_timing_attr[] = {
  130. &timing_attr_max_nonturbo_ratio.attr.attr,
  131. &timing_attr_tsc_art_ratio.attr.attr,
  132. NULL,
  133. };
  134. static struct attribute_group pt_timing_group = {
  135. .attrs = pt_timing_attr,
  136. };
  137. static const struct attribute_group *pt_attr_groups[] = {
  138. &pt_cap_group,
  139. &pt_format_group,
  140. &pt_timing_group,
  141. NULL,
  142. };
  143. static int __init pt_pmu_hw_init(void)
  144. {
  145. struct dev_ext_attribute *de_attrs;
  146. struct attribute **attrs;
  147. size_t size;
  148. u64 reg;
  149. int ret;
  150. long i;
  151. rdmsrl(MSR_PLATFORM_INFO, reg);
  152. pt_pmu.max_nonturbo_ratio = (reg & 0xff00) >> 8;
  153. /*
  154. * if available, read in TSC to core crystal clock ratio,
  155. * otherwise, zero for numerator stands for "not enumerated"
  156. * as per SDM
  157. */
  158. if (boot_cpu_data.cpuid_level >= CPUID_TSC_LEAF) {
  159. u32 eax, ebx, ecx, edx;
  160. cpuid(CPUID_TSC_LEAF, &eax, &ebx, &ecx, &edx);
  161. pt_pmu.tsc_art_num = ebx;
  162. pt_pmu.tsc_art_den = eax;
  163. }
  164. if (boot_cpu_has(X86_FEATURE_VMX)) {
  165. /*
  166. * Intel SDM, 36.5 "Tracing post-VMXON" says that
  167. * "IA32_VMX_MISC[bit 14]" being 1 means PT can trace
  168. * post-VMXON.
  169. */
  170. rdmsrl(MSR_IA32_VMX_MISC, reg);
  171. if (reg & BIT(14))
  172. pt_pmu.vmx = true;
  173. }
  174. attrs = NULL;
  175. for (i = 0; i < PT_CPUID_LEAVES; i++) {
  176. cpuid_count(20, i,
  177. &pt_pmu.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM],
  178. &pt_pmu.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM],
  179. &pt_pmu.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM],
  180. &pt_pmu.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM]);
  181. }
  182. ret = -ENOMEM;
  183. size = sizeof(struct attribute *) * (ARRAY_SIZE(pt_caps)+1);
  184. attrs = kzalloc(size, GFP_KERNEL);
  185. if (!attrs)
  186. goto fail;
  187. size = sizeof(struct dev_ext_attribute) * (ARRAY_SIZE(pt_caps)+1);
  188. de_attrs = kzalloc(size, GFP_KERNEL);
  189. if (!de_attrs)
  190. goto fail;
  191. for (i = 0; i < ARRAY_SIZE(pt_caps); i++) {
  192. struct dev_ext_attribute *de_attr = de_attrs + i;
  193. de_attr->attr.attr.name = pt_caps[i].name;
  194. sysfs_attr_init(&de_attr->attr.attr);
  195. de_attr->attr.attr.mode = S_IRUGO;
  196. de_attr->attr.show = pt_cap_show;
  197. de_attr->var = (void *)i;
  198. attrs[i] = &de_attr->attr.attr;
  199. }
  200. pt_cap_group.attrs = attrs;
  201. return 0;
  202. fail:
  203. kfree(attrs);
  204. return ret;
  205. }
  206. #define RTIT_CTL_CYC_PSB (RTIT_CTL_CYCLEACC | \
  207. RTIT_CTL_CYC_THRESH | \
  208. RTIT_CTL_PSB_FREQ)
  209. #define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
  210. RTIT_CTL_MTC_RANGE)
  211. #define RTIT_CTL_PTW (RTIT_CTL_PTW_EN | \
  212. RTIT_CTL_FUP_ON_PTW)
  213. #define PT_CONFIG_MASK (RTIT_CTL_TSC_EN | \
  214. RTIT_CTL_DISRETC | \
  215. RTIT_CTL_CYC_PSB | \
  216. RTIT_CTL_MTC | \
  217. RTIT_CTL_PWR_EVT_EN | \
  218. RTIT_CTL_FUP_ON_PTW | \
  219. RTIT_CTL_PTW_EN)
  220. static bool pt_event_valid(struct perf_event *event)
  221. {
  222. u64 config = event->attr.config;
  223. u64 allowed, requested;
  224. if ((config & PT_CONFIG_MASK) != config)
  225. return false;
  226. if (config & RTIT_CTL_CYC_PSB) {
  227. if (!pt_cap_get(PT_CAP_psb_cyc))
  228. return false;
  229. allowed = pt_cap_get(PT_CAP_psb_periods);
  230. requested = (config & RTIT_CTL_PSB_FREQ) >>
  231. RTIT_CTL_PSB_FREQ_OFFSET;
  232. if (requested && (!(allowed & BIT(requested))))
  233. return false;
  234. allowed = pt_cap_get(PT_CAP_cycle_thresholds);
  235. requested = (config & RTIT_CTL_CYC_THRESH) >>
  236. RTIT_CTL_CYC_THRESH_OFFSET;
  237. if (requested && (!(allowed & BIT(requested))))
  238. return false;
  239. }
  240. if (config & RTIT_CTL_MTC) {
  241. /*
  242. * In the unlikely case that CPUID lists valid mtc periods,
  243. * but not the mtc capability, drop out here.
  244. *
  245. * Spec says that setting mtc period bits while mtc bit in
  246. * CPUID is 0 will #GP, so better safe than sorry.
  247. */
  248. if (!pt_cap_get(PT_CAP_mtc))
  249. return false;
  250. allowed = pt_cap_get(PT_CAP_mtc_periods);
  251. if (!allowed)
  252. return false;
  253. requested = (config & RTIT_CTL_MTC_RANGE) >>
  254. RTIT_CTL_MTC_RANGE_OFFSET;
  255. if (!(allowed & BIT(requested)))
  256. return false;
  257. }
  258. if (config & RTIT_CTL_PWR_EVT_EN &&
  259. !pt_cap_get(PT_CAP_power_event_trace))
  260. return false;
  261. if (config & RTIT_CTL_PTW) {
  262. if (!pt_cap_get(PT_CAP_ptwrite))
  263. return false;
  264. /* FUPonPTW without PTW doesn't make sense */
  265. if ((config & RTIT_CTL_FUP_ON_PTW) &&
  266. !(config & RTIT_CTL_PTW_EN))
  267. return false;
  268. }
  269. return true;
  270. }
  271. /*
  272. * PT configuration helpers
  273. * These all are cpu affine and operate on a local PT
  274. */
  275. /* Address ranges and their corresponding msr configuration registers */
  276. static const struct pt_address_range {
  277. unsigned long msr_a;
  278. unsigned long msr_b;
  279. unsigned int reg_off;
  280. } pt_address_ranges[] = {
  281. {
  282. .msr_a = MSR_IA32_RTIT_ADDR0_A,
  283. .msr_b = MSR_IA32_RTIT_ADDR0_B,
  284. .reg_off = RTIT_CTL_ADDR0_OFFSET,
  285. },
  286. {
  287. .msr_a = MSR_IA32_RTIT_ADDR1_A,
  288. .msr_b = MSR_IA32_RTIT_ADDR1_B,
  289. .reg_off = RTIT_CTL_ADDR1_OFFSET,
  290. },
  291. {
  292. .msr_a = MSR_IA32_RTIT_ADDR2_A,
  293. .msr_b = MSR_IA32_RTIT_ADDR2_B,
  294. .reg_off = RTIT_CTL_ADDR2_OFFSET,
  295. },
  296. {
  297. .msr_a = MSR_IA32_RTIT_ADDR3_A,
  298. .msr_b = MSR_IA32_RTIT_ADDR3_B,
  299. .reg_off = RTIT_CTL_ADDR3_OFFSET,
  300. }
  301. };
  302. static u64 pt_config_filters(struct perf_event *event)
  303. {
  304. struct pt_filters *filters = event->hw.addr_filters;
  305. struct pt *pt = this_cpu_ptr(&pt_ctx);
  306. unsigned int range = 0;
  307. u64 rtit_ctl = 0;
  308. if (!filters)
  309. return 0;
  310. perf_event_addr_filters_sync(event);
  311. for (range = 0; range < filters->nr_filters; range++) {
  312. struct pt_filter *filter = &filters->filter[range];
  313. /*
  314. * Note, if the range has zero start/end addresses due
  315. * to its dynamic object not being loaded yet, we just
  316. * go ahead and program zeroed range, which will simply
  317. * produce no data. Note^2: if executable code at 0x0
  318. * is a concern, we can set up an "invalid" configuration
  319. * such as msr_b < msr_a.
  320. */
  321. /* avoid redundant msr writes */
  322. if (pt->filters.filter[range].msr_a != filter->msr_a) {
  323. wrmsrl(pt_address_ranges[range].msr_a, filter->msr_a);
  324. pt->filters.filter[range].msr_a = filter->msr_a;
  325. }
  326. if (pt->filters.filter[range].msr_b != filter->msr_b) {
  327. wrmsrl(pt_address_ranges[range].msr_b, filter->msr_b);
  328. pt->filters.filter[range].msr_b = filter->msr_b;
  329. }
  330. rtit_ctl |= filter->config << pt_address_ranges[range].reg_off;
  331. }
  332. return rtit_ctl;
  333. }
  334. static void pt_config(struct perf_event *event)
  335. {
  336. u64 reg;
  337. if (!event->hw.itrace_started) {
  338. event->hw.itrace_started = 1;
  339. wrmsrl(MSR_IA32_RTIT_STATUS, 0);
  340. }
  341. reg = pt_config_filters(event);
  342. reg |= RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
  343. if (!event->attr.exclude_kernel)
  344. reg |= RTIT_CTL_OS;
  345. if (!event->attr.exclude_user)
  346. reg |= RTIT_CTL_USR;
  347. reg |= (event->attr.config & PT_CONFIG_MASK);
  348. event->hw.config = reg;
  349. wrmsrl(MSR_IA32_RTIT_CTL, reg);
  350. }
  351. static void pt_config_stop(struct perf_event *event)
  352. {
  353. u64 ctl = READ_ONCE(event->hw.config);
  354. /* may be already stopped by a PMI */
  355. if (!(ctl & RTIT_CTL_TRACEEN))
  356. return;
  357. ctl &= ~RTIT_CTL_TRACEEN;
  358. wrmsrl(MSR_IA32_RTIT_CTL, ctl);
  359. WRITE_ONCE(event->hw.config, ctl);
  360. /*
  361. * A wrmsr that disables trace generation serializes other PT
  362. * registers and causes all data packets to be written to memory,
  363. * but a fence is required for the data to become globally visible.
  364. *
  365. * The below WMB, separating data store and aux_head store matches
  366. * the consumer's RMB that separates aux_head load and data load.
  367. */
  368. wmb();
  369. }
  370. static void pt_config_buffer(void *buf, unsigned int topa_idx,
  371. unsigned int output_off)
  372. {
  373. u64 reg;
  374. wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, virt_to_phys(buf));
  375. reg = 0x7f | ((u64)topa_idx << 7) | ((u64)output_off << 32);
  376. wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, reg);
  377. }
  378. /*
  379. * Keep ToPA table-related metadata on the same page as the actual table,
  380. * taking up a few words from the top
  381. */
  382. #define TENTS_PER_PAGE (((PAGE_SIZE - 40) / sizeof(struct topa_entry)) - 1)
  383. /**
  384. * struct topa - page-sized ToPA table with metadata at the top
  385. * @table: actual ToPA table entries, as understood by PT hardware
  386. * @list: linkage to struct pt_buffer's list of tables
  387. * @phys: physical address of this page
  388. * @offset: offset of the first entry in this table in the buffer
  389. * @size: total size of all entries in this table
  390. * @last: index of the last initialized entry in this table
  391. */
  392. struct topa {
  393. struct topa_entry table[TENTS_PER_PAGE];
  394. struct list_head list;
  395. u64 phys;
  396. u64 offset;
  397. size_t size;
  398. int last;
  399. };
  400. /* make -1 stand for the last table entry */
  401. #define TOPA_ENTRY(t, i) ((i) == -1 ? &(t)->table[(t)->last] : &(t)->table[(i)])
  402. /**
  403. * topa_alloc() - allocate page-sized ToPA table
  404. * @cpu: CPU on which to allocate.
  405. * @gfp: Allocation flags.
  406. *
  407. * Return: On success, return the pointer to ToPA table page.
  408. */
  409. static struct topa *topa_alloc(int cpu, gfp_t gfp)
  410. {
  411. int node = cpu_to_node(cpu);
  412. struct topa *topa;
  413. struct page *p;
  414. p = alloc_pages_node(node, gfp | __GFP_ZERO, 0);
  415. if (!p)
  416. return NULL;
  417. topa = page_address(p);
  418. topa->last = 0;
  419. topa->phys = page_to_phys(p);
  420. /*
  421. * In case of singe-entry ToPA, always put the self-referencing END
  422. * link as the 2nd entry in the table
  423. */
  424. if (!pt_cap_get(PT_CAP_topa_multiple_entries)) {
  425. TOPA_ENTRY(topa, 1)->base = topa->phys >> TOPA_SHIFT;
  426. TOPA_ENTRY(topa, 1)->end = 1;
  427. }
  428. return topa;
  429. }
  430. /**
  431. * topa_free() - free a page-sized ToPA table
  432. * @topa: Table to deallocate.
  433. */
  434. static void topa_free(struct topa *topa)
  435. {
  436. free_page((unsigned long)topa);
  437. }
  438. /**
  439. * topa_insert_table() - insert a ToPA table into a buffer
  440. * @buf: PT buffer that's being extended.
  441. * @topa: New topa table to be inserted.
  442. *
  443. * If it's the first table in this buffer, set up buffer's pointers
  444. * accordingly; otherwise, add a END=1 link entry to @topa to the current
  445. * "last" table and adjust the last table pointer to @topa.
  446. */
  447. static void topa_insert_table(struct pt_buffer *buf, struct topa *topa)
  448. {
  449. struct topa *last = buf->last;
  450. list_add_tail(&topa->list, &buf->tables);
  451. if (!buf->first) {
  452. buf->first = buf->last = buf->cur = topa;
  453. return;
  454. }
  455. topa->offset = last->offset + last->size;
  456. buf->last = topa;
  457. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  458. return;
  459. BUG_ON(last->last != TENTS_PER_PAGE - 1);
  460. TOPA_ENTRY(last, -1)->base = topa->phys >> TOPA_SHIFT;
  461. TOPA_ENTRY(last, -1)->end = 1;
  462. }
  463. /**
  464. * topa_table_full() - check if a ToPA table is filled up
  465. * @topa: ToPA table.
  466. */
  467. static bool topa_table_full(struct topa *topa)
  468. {
  469. /* single-entry ToPA is a special case */
  470. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  471. return !!topa->last;
  472. return topa->last == TENTS_PER_PAGE - 1;
  473. }
  474. /**
  475. * topa_insert_pages() - create a list of ToPA tables
  476. * @buf: PT buffer being initialized.
  477. * @gfp: Allocation flags.
  478. *
  479. * This initializes a list of ToPA tables with entries from
  480. * the data_pages provided by rb_alloc_aux().
  481. *
  482. * Return: 0 on success or error code.
  483. */
  484. static int topa_insert_pages(struct pt_buffer *buf, gfp_t gfp)
  485. {
  486. struct topa *topa = buf->last;
  487. int order = 0;
  488. struct page *p;
  489. p = virt_to_page(buf->data_pages[buf->nr_pages]);
  490. if (PagePrivate(p))
  491. order = page_private(p);
  492. if (topa_table_full(topa)) {
  493. topa = topa_alloc(buf->cpu, gfp);
  494. if (!topa)
  495. return -ENOMEM;
  496. topa_insert_table(buf, topa);
  497. }
  498. TOPA_ENTRY(topa, -1)->base = page_to_phys(p) >> TOPA_SHIFT;
  499. TOPA_ENTRY(topa, -1)->size = order;
  500. if (!buf->snapshot && !pt_cap_get(PT_CAP_topa_multiple_entries)) {
  501. TOPA_ENTRY(topa, -1)->intr = 1;
  502. TOPA_ENTRY(topa, -1)->stop = 1;
  503. }
  504. topa->last++;
  505. topa->size += sizes(order);
  506. buf->nr_pages += 1ul << order;
  507. return 0;
  508. }
  509. /**
  510. * pt_topa_dump() - print ToPA tables and their entries
  511. * @buf: PT buffer.
  512. */
  513. static void pt_topa_dump(struct pt_buffer *buf)
  514. {
  515. struct topa *topa;
  516. list_for_each_entry(topa, &buf->tables, list) {
  517. int i;
  518. pr_debug("# table @%p (%016Lx), off %llx size %zx\n", topa->table,
  519. topa->phys, topa->offset, topa->size);
  520. for (i = 0; i < TENTS_PER_PAGE; i++) {
  521. pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
  522. &topa->table[i],
  523. (unsigned long)topa->table[i].base << TOPA_SHIFT,
  524. sizes(topa->table[i].size),
  525. topa->table[i].end ? 'E' : ' ',
  526. topa->table[i].intr ? 'I' : ' ',
  527. topa->table[i].stop ? 'S' : ' ',
  528. *(u64 *)&topa->table[i]);
  529. if ((pt_cap_get(PT_CAP_topa_multiple_entries) &&
  530. topa->table[i].stop) ||
  531. topa->table[i].end)
  532. break;
  533. }
  534. }
  535. }
  536. /**
  537. * pt_buffer_advance() - advance to the next output region
  538. * @buf: PT buffer.
  539. *
  540. * Advance the current pointers in the buffer to the next ToPA entry.
  541. */
  542. static void pt_buffer_advance(struct pt_buffer *buf)
  543. {
  544. buf->output_off = 0;
  545. buf->cur_idx++;
  546. if (buf->cur_idx == buf->cur->last) {
  547. if (buf->cur == buf->last)
  548. buf->cur = buf->first;
  549. else
  550. buf->cur = list_entry(buf->cur->list.next, struct topa,
  551. list);
  552. buf->cur_idx = 0;
  553. }
  554. }
  555. /**
  556. * pt_update_head() - calculate current offsets and sizes
  557. * @pt: Per-cpu pt context.
  558. *
  559. * Update buffer's current write pointer position and data size.
  560. */
  561. static void pt_update_head(struct pt *pt)
  562. {
  563. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  564. u64 topa_idx, base, old;
  565. /* offset of the first region in this table from the beginning of buf */
  566. base = buf->cur->offset + buf->output_off;
  567. /* offset of the current output region within this table */
  568. for (topa_idx = 0; topa_idx < buf->cur_idx; topa_idx++)
  569. base += sizes(buf->cur->table[topa_idx].size);
  570. if (buf->snapshot) {
  571. local_set(&buf->data_size, base);
  572. } else {
  573. old = (local64_xchg(&buf->head, base) &
  574. ((buf->nr_pages << PAGE_SHIFT) - 1));
  575. if (base < old)
  576. base += buf->nr_pages << PAGE_SHIFT;
  577. local_add(base - old, &buf->data_size);
  578. }
  579. }
  580. /**
  581. * pt_buffer_region() - obtain current output region's address
  582. * @buf: PT buffer.
  583. */
  584. static void *pt_buffer_region(struct pt_buffer *buf)
  585. {
  586. return phys_to_virt(buf->cur->table[buf->cur_idx].base << TOPA_SHIFT);
  587. }
  588. /**
  589. * pt_buffer_region_size() - obtain current output region's size
  590. * @buf: PT buffer.
  591. */
  592. static size_t pt_buffer_region_size(struct pt_buffer *buf)
  593. {
  594. return sizes(buf->cur->table[buf->cur_idx].size);
  595. }
  596. /**
  597. * pt_handle_status() - take care of possible status conditions
  598. * @pt: Per-cpu pt context.
  599. */
  600. static void pt_handle_status(struct pt *pt)
  601. {
  602. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  603. int advance = 0;
  604. u64 status;
  605. rdmsrl(MSR_IA32_RTIT_STATUS, status);
  606. if (status & RTIT_STATUS_ERROR) {
  607. pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
  608. pt_topa_dump(buf);
  609. status &= ~RTIT_STATUS_ERROR;
  610. }
  611. if (status & RTIT_STATUS_STOPPED) {
  612. status &= ~RTIT_STATUS_STOPPED;
  613. /*
  614. * On systems that only do single-entry ToPA, hitting STOP
  615. * means we are already losing data; need to let the decoder
  616. * know.
  617. */
  618. if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
  619. buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
  620. local_inc(&buf->lost);
  621. advance++;
  622. }
  623. }
  624. /*
  625. * Also on single-entry ToPA implementations, interrupt will come
  626. * before the output reaches its output region's boundary.
  627. */
  628. if (!pt_cap_get(PT_CAP_topa_multiple_entries) && !buf->snapshot &&
  629. pt_buffer_region_size(buf) - buf->output_off <= TOPA_PMI_MARGIN) {
  630. void *head = pt_buffer_region(buf);
  631. /* everything within this margin needs to be zeroed out */
  632. memset(head + buf->output_off, 0,
  633. pt_buffer_region_size(buf) -
  634. buf->output_off);
  635. advance++;
  636. }
  637. if (advance)
  638. pt_buffer_advance(buf);
  639. wrmsrl(MSR_IA32_RTIT_STATUS, status);
  640. }
  641. /**
  642. * pt_read_offset() - translate registers into buffer pointers
  643. * @buf: PT buffer.
  644. *
  645. * Set buffer's output pointers from MSR values.
  646. */
  647. static void pt_read_offset(struct pt_buffer *buf)
  648. {
  649. u64 offset, base_topa;
  650. rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, base_topa);
  651. buf->cur = phys_to_virt(base_topa);
  652. rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, offset);
  653. /* offset within current output region */
  654. buf->output_off = offset >> 32;
  655. /* index of current output region within this table */
  656. buf->cur_idx = (offset & 0xffffff80) >> 7;
  657. }
  658. /**
  659. * pt_topa_next_entry() - obtain index of the first page in the next ToPA entry
  660. * @buf: PT buffer.
  661. * @pg: Page offset in the buffer.
  662. *
  663. * When advancing to the next output region (ToPA entry), given a page offset
  664. * into the buffer, we need to find the offset of the first page in the next
  665. * region.
  666. */
  667. static unsigned int pt_topa_next_entry(struct pt_buffer *buf, unsigned int pg)
  668. {
  669. struct topa_entry *te = buf->topa_index[pg];
  670. /* one region */
  671. if (buf->first == buf->last && buf->first->last == 1)
  672. return pg;
  673. do {
  674. pg++;
  675. pg &= buf->nr_pages - 1;
  676. } while (buf->topa_index[pg] == te);
  677. return pg;
  678. }
  679. /**
  680. * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
  681. * @buf: PT buffer.
  682. * @handle: Current output handle.
  683. *
  684. * Place INT and STOP marks to prevent overwriting old data that the consumer
  685. * hasn't yet collected and waking up the consumer after a certain fraction of
  686. * the buffer has filled up. Only needed and sensible for non-snapshot counters.
  687. *
  688. * This obviously relies on buf::head to figure out buffer markers, so it has
  689. * to be called after pt_buffer_reset_offsets() and before the hardware tracing
  690. * is enabled.
  691. */
  692. static int pt_buffer_reset_markers(struct pt_buffer *buf,
  693. struct perf_output_handle *handle)
  694. {
  695. unsigned long head = local64_read(&buf->head);
  696. unsigned long idx, npages, wakeup;
  697. /* can't stop in the middle of an output region */
  698. if (buf->output_off + handle->size + 1 <
  699. sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size))
  700. return -EINVAL;
  701. /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
  702. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  703. return 0;
  704. /* clear STOP and INT from current entry */
  705. buf->topa_index[buf->stop_pos]->stop = 0;
  706. buf->topa_index[buf->stop_pos]->intr = 0;
  707. buf->topa_index[buf->intr_pos]->intr = 0;
  708. /* how many pages till the STOP marker */
  709. npages = handle->size >> PAGE_SHIFT;
  710. /* if it's on a page boundary, fill up one more page */
  711. if (!offset_in_page(head + handle->size + 1))
  712. npages++;
  713. idx = (head >> PAGE_SHIFT) + npages;
  714. idx &= buf->nr_pages - 1;
  715. buf->stop_pos = idx;
  716. wakeup = handle->wakeup >> PAGE_SHIFT;
  717. /* in the worst case, wake up the consumer one page before hard stop */
  718. idx = (head >> PAGE_SHIFT) + npages - 1;
  719. if (idx > wakeup)
  720. idx = wakeup;
  721. idx &= buf->nr_pages - 1;
  722. buf->intr_pos = idx;
  723. buf->topa_index[buf->stop_pos]->stop = 1;
  724. buf->topa_index[buf->stop_pos]->intr = 1;
  725. buf->topa_index[buf->intr_pos]->intr = 1;
  726. return 0;
  727. }
  728. /**
  729. * pt_buffer_setup_topa_index() - build topa_index[] table of regions
  730. * @buf: PT buffer.
  731. *
  732. * topa_index[] references output regions indexed by offset into the
  733. * buffer for purposes of quick reverse lookup.
  734. */
  735. static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
  736. {
  737. struct topa *cur = buf->first, *prev = buf->last;
  738. struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
  739. *te_prev = TOPA_ENTRY(prev, prev->last - 1);
  740. int pg = 0, idx = 0;
  741. while (pg < buf->nr_pages) {
  742. int tidx;
  743. /* pages within one topa entry */
  744. for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
  745. buf->topa_index[pg] = te_prev;
  746. te_prev = te_cur;
  747. if (idx == cur->last - 1) {
  748. /* advance to next topa table */
  749. idx = 0;
  750. cur = list_entry(cur->list.next, struct topa, list);
  751. } else {
  752. idx++;
  753. }
  754. te_cur = TOPA_ENTRY(cur, idx);
  755. }
  756. }
  757. /**
  758. * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
  759. * @buf: PT buffer.
  760. * @head: Write pointer (aux_head) from AUX buffer.
  761. *
  762. * Find the ToPA table and entry corresponding to given @head and set buffer's
  763. * "current" pointers accordingly. This is done after we have obtained the
  764. * current aux_head position from a successful call to perf_aux_output_begin()
  765. * to make sure the hardware is writing to the right place.
  766. *
  767. * This function modifies buf::{cur,cur_idx,output_off} that will be programmed
  768. * into PT msrs when the tracing is enabled and buf::head and buf::data_size,
  769. * which are used to determine INT and STOP markers' locations by a subsequent
  770. * call to pt_buffer_reset_markers().
  771. */
  772. static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
  773. {
  774. int pg;
  775. if (buf->snapshot)
  776. head &= (buf->nr_pages << PAGE_SHIFT) - 1;
  777. pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
  778. pg = pt_topa_next_entry(buf, pg);
  779. buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
  780. buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
  781. (unsigned long)buf->cur) / sizeof(struct topa_entry);
  782. buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
  783. local64_set(&buf->head, head);
  784. local_set(&buf->data_size, 0);
  785. }
  786. /**
  787. * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
  788. * @buf: PT buffer.
  789. */
  790. static void pt_buffer_fini_topa(struct pt_buffer *buf)
  791. {
  792. struct topa *topa, *iter;
  793. list_for_each_entry_safe(topa, iter, &buf->tables, list) {
  794. /*
  795. * right now, this is in free_aux() path only, so
  796. * no need to unlink this table from the list
  797. */
  798. topa_free(topa);
  799. }
  800. }
  801. /**
  802. * pt_buffer_init_topa() - initialize ToPA table for pt buffer
  803. * @buf: PT buffer.
  804. * @size: Total size of all regions within this ToPA.
  805. * @gfp: Allocation flags.
  806. */
  807. static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
  808. gfp_t gfp)
  809. {
  810. struct topa *topa;
  811. int err;
  812. topa = topa_alloc(buf->cpu, gfp);
  813. if (!topa)
  814. return -ENOMEM;
  815. topa_insert_table(buf, topa);
  816. while (buf->nr_pages < nr_pages) {
  817. err = topa_insert_pages(buf, gfp);
  818. if (err) {
  819. pt_buffer_fini_topa(buf);
  820. return -ENOMEM;
  821. }
  822. }
  823. pt_buffer_setup_topa_index(buf);
  824. /* link last table to the first one, unless we're double buffering */
  825. if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
  826. TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
  827. TOPA_ENTRY(buf->last, -1)->end = 1;
  828. }
  829. pt_topa_dump(buf);
  830. return 0;
  831. }
  832. /**
  833. * pt_buffer_setup_aux() - set up topa tables for a PT buffer
  834. * @cpu: Cpu on which to allocate, -1 means current.
  835. * @pages: Array of pointers to buffer pages passed from perf core.
  836. * @nr_pages: Number of pages in the buffer.
  837. * @snapshot: If this is a snapshot/overwrite counter.
  838. *
  839. * This is a pmu::setup_aux callback that sets up ToPA tables and all the
  840. * bookkeeping for an AUX buffer.
  841. *
  842. * Return: Our private PT buffer structure.
  843. */
  844. static void *
  845. pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
  846. {
  847. struct pt_buffer *buf;
  848. int node, ret;
  849. if (!nr_pages)
  850. return NULL;
  851. if (cpu == -1)
  852. cpu = raw_smp_processor_id();
  853. node = cpu_to_node(cpu);
  854. buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
  855. GFP_KERNEL, node);
  856. if (!buf)
  857. return NULL;
  858. buf->cpu = cpu;
  859. buf->snapshot = snapshot;
  860. buf->data_pages = pages;
  861. INIT_LIST_HEAD(&buf->tables);
  862. ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
  863. if (ret) {
  864. kfree(buf);
  865. return NULL;
  866. }
  867. return buf;
  868. }
  869. /**
  870. * pt_buffer_free_aux() - perf AUX deallocation path callback
  871. * @data: PT buffer.
  872. */
  873. static void pt_buffer_free_aux(void *data)
  874. {
  875. struct pt_buffer *buf = data;
  876. pt_buffer_fini_topa(buf);
  877. kfree(buf);
  878. }
  879. static int pt_addr_filters_init(struct perf_event *event)
  880. {
  881. struct pt_filters *filters;
  882. int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);
  883. if (!pt_cap_get(PT_CAP_num_address_ranges))
  884. return 0;
  885. filters = kzalloc_node(sizeof(struct pt_filters), GFP_KERNEL, node);
  886. if (!filters)
  887. return -ENOMEM;
  888. if (event->parent)
  889. memcpy(filters, event->parent->hw.addr_filters,
  890. sizeof(*filters));
  891. event->hw.addr_filters = filters;
  892. return 0;
  893. }
  894. static void pt_addr_filters_fini(struct perf_event *event)
  895. {
  896. kfree(event->hw.addr_filters);
  897. event->hw.addr_filters = NULL;
  898. }
  899. static inline bool valid_kernel_ip(unsigned long ip)
  900. {
  901. return virt_addr_valid(ip) && kernel_ip(ip);
  902. }
  903. static int pt_event_addr_filters_validate(struct list_head *filters)
  904. {
  905. struct perf_addr_filter *filter;
  906. int range = 0;
  907. list_for_each_entry(filter, filters, entry) {
  908. /* PT doesn't support single address triggers */
  909. if (!filter->range || !filter->size)
  910. return -EOPNOTSUPP;
  911. if (!filter->inode) {
  912. if (!valid_kernel_ip(filter->offset))
  913. return -EINVAL;
  914. if (!valid_kernel_ip(filter->offset + filter->size))
  915. return -EINVAL;
  916. }
  917. if (++range > pt_cap_get(PT_CAP_num_address_ranges))
  918. return -EOPNOTSUPP;
  919. }
  920. return 0;
  921. }
  922. static void pt_event_addr_filters_sync(struct perf_event *event)
  923. {
  924. struct perf_addr_filters_head *head = perf_event_addr_filters(event);
  925. unsigned long msr_a, msr_b, *offs = event->addr_filters_offs;
  926. struct pt_filters *filters = event->hw.addr_filters;
  927. struct perf_addr_filter *filter;
  928. int range = 0;
  929. if (!filters)
  930. return;
  931. list_for_each_entry(filter, &head->list, entry) {
  932. if (filter->inode && !offs[range]) {
  933. msr_a = msr_b = 0;
  934. } else {
  935. /* apply the offset */
  936. msr_a = filter->offset + offs[range];
  937. msr_b = filter->size + msr_a - 1;
  938. }
  939. filters->filter[range].msr_a = msr_a;
  940. filters->filter[range].msr_b = msr_b;
  941. filters->filter[range].config = filter->filter ? 1 : 2;
  942. range++;
  943. }
  944. filters->nr_filters = range;
  945. }
  946. /**
  947. * intel_pt_interrupt() - PT PMI handler
  948. */
  949. void intel_pt_interrupt(void)
  950. {
  951. struct pt *pt = this_cpu_ptr(&pt_ctx);
  952. struct pt_buffer *buf;
  953. struct perf_event *event = pt->handle.event;
  954. /*
  955. * There may be a dangling PT bit in the interrupt status register
  956. * after PT has been disabled by pt_event_stop(). Make sure we don't
  957. * do anything (particularly, re-enable) for this event here.
  958. */
  959. if (!READ_ONCE(pt->handle_nmi))
  960. return;
  961. /*
  962. * If VMX is on and PT does not support it, don't touch anything.
  963. */
  964. if (READ_ONCE(pt->vmx_on))
  965. return;
  966. if (!event)
  967. return;
  968. pt_config_stop(event);
  969. buf = perf_get_aux(&pt->handle);
  970. if (!buf)
  971. return;
  972. pt_read_offset(buf);
  973. pt_handle_status(pt);
  974. pt_update_head(pt);
  975. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  976. local_xchg(&buf->lost, 0));
  977. if (!event->hw.state) {
  978. int ret;
  979. buf = perf_aux_output_begin(&pt->handle, event);
  980. if (!buf) {
  981. event->hw.state = PERF_HES_STOPPED;
  982. return;
  983. }
  984. pt_buffer_reset_offsets(buf, pt->handle.head);
  985. /* snapshot counters don't use PMI, so it's safe */
  986. ret = pt_buffer_reset_markers(buf, &pt->handle);
  987. if (ret) {
  988. perf_aux_output_end(&pt->handle, 0, true);
  989. return;
  990. }
  991. pt_config_buffer(buf->cur->table, buf->cur_idx,
  992. buf->output_off);
  993. pt_config(event);
  994. }
  995. }
  996. void intel_pt_handle_vmx(int on)
  997. {
  998. struct pt *pt = this_cpu_ptr(&pt_ctx);
  999. struct perf_event *event;
  1000. unsigned long flags;
  1001. /* PT plays nice with VMX, do nothing */
  1002. if (pt_pmu.vmx)
  1003. return;
  1004. /*
  1005. * VMXON will clear RTIT_CTL.TraceEn; we need to make
  1006. * sure to not try to set it while VMX is on. Disable
  1007. * interrupts to avoid racing with pmu callbacks;
  1008. * concurrent PMI should be handled fine.
  1009. */
  1010. local_irq_save(flags);
  1011. WRITE_ONCE(pt->vmx_on, on);
  1012. if (on) {
  1013. /* prevent pt_config_stop() from writing RTIT_CTL */
  1014. event = pt->handle.event;
  1015. if (event)
  1016. event->hw.config = 0;
  1017. }
  1018. local_irq_restore(flags);
  1019. }
  1020. EXPORT_SYMBOL_GPL(intel_pt_handle_vmx);
  1021. /*
  1022. * PMU callbacks
  1023. */
  1024. static void pt_event_start(struct perf_event *event, int mode)
  1025. {
  1026. struct hw_perf_event *hwc = &event->hw;
  1027. struct pt *pt = this_cpu_ptr(&pt_ctx);
  1028. struct pt_buffer *buf;
  1029. if (READ_ONCE(pt->vmx_on))
  1030. return;
  1031. buf = perf_aux_output_begin(&pt->handle, event);
  1032. if (!buf)
  1033. goto fail_stop;
  1034. pt_buffer_reset_offsets(buf, pt->handle.head);
  1035. if (!buf->snapshot) {
  1036. if (pt_buffer_reset_markers(buf, &pt->handle))
  1037. goto fail_end_stop;
  1038. }
  1039. WRITE_ONCE(pt->handle_nmi, 1);
  1040. hwc->state = 0;
  1041. pt_config_buffer(buf->cur->table, buf->cur_idx,
  1042. buf->output_off);
  1043. pt_config(event);
  1044. return;
  1045. fail_end_stop:
  1046. perf_aux_output_end(&pt->handle, 0, true);
  1047. fail_stop:
  1048. hwc->state = PERF_HES_STOPPED;
  1049. }
  1050. static void pt_event_stop(struct perf_event *event, int mode)
  1051. {
  1052. struct pt *pt = this_cpu_ptr(&pt_ctx);
  1053. /*
  1054. * Protect against the PMI racing with disabling wrmsr,
  1055. * see comment in intel_pt_interrupt().
  1056. */
  1057. WRITE_ONCE(pt->handle_nmi, 0);
  1058. pt_config_stop(event);
  1059. if (event->hw.state == PERF_HES_STOPPED)
  1060. return;
  1061. event->hw.state = PERF_HES_STOPPED;
  1062. if (mode & PERF_EF_UPDATE) {
  1063. struct pt_buffer *buf = perf_get_aux(&pt->handle);
  1064. if (!buf)
  1065. return;
  1066. if (WARN_ON_ONCE(pt->handle.event != event))
  1067. return;
  1068. pt_read_offset(buf);
  1069. pt_handle_status(pt);
  1070. pt_update_head(pt);
  1071. if (buf->snapshot)
  1072. pt->handle.head =
  1073. local_xchg(&buf->data_size,
  1074. buf->nr_pages << PAGE_SHIFT);
  1075. perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0),
  1076. local_xchg(&buf->lost, 0));
  1077. }
  1078. }
  1079. static void pt_event_del(struct perf_event *event, int mode)
  1080. {
  1081. pt_event_stop(event, PERF_EF_UPDATE);
  1082. }
  1083. static int pt_event_add(struct perf_event *event, int mode)
  1084. {
  1085. struct pt *pt = this_cpu_ptr(&pt_ctx);
  1086. struct hw_perf_event *hwc = &event->hw;
  1087. int ret = -EBUSY;
  1088. if (pt->handle.event)
  1089. goto fail;
  1090. if (mode & PERF_EF_START) {
  1091. pt_event_start(event, 0);
  1092. ret = -EINVAL;
  1093. if (hwc->state == PERF_HES_STOPPED)
  1094. goto fail;
  1095. } else {
  1096. hwc->state = PERF_HES_STOPPED;
  1097. }
  1098. ret = 0;
  1099. fail:
  1100. return ret;
  1101. }
  1102. static void pt_event_read(struct perf_event *event)
  1103. {
  1104. }
  1105. static void pt_event_destroy(struct perf_event *event)
  1106. {
  1107. pt_addr_filters_fini(event);
  1108. x86_del_exclusive(x86_lbr_exclusive_pt);
  1109. }
  1110. static int pt_event_init(struct perf_event *event)
  1111. {
  1112. if (event->attr.type != pt_pmu.pmu.type)
  1113. return -ENOENT;
  1114. if (!pt_event_valid(event))
  1115. return -EINVAL;
  1116. if (x86_add_exclusive(x86_lbr_exclusive_pt))
  1117. return -EBUSY;
  1118. if (pt_addr_filters_init(event)) {
  1119. x86_del_exclusive(x86_lbr_exclusive_pt);
  1120. return -ENOMEM;
  1121. }
  1122. event->destroy = pt_event_destroy;
  1123. return 0;
  1124. }
  1125. void cpu_emergency_stop_pt(void)
  1126. {
  1127. struct pt *pt = this_cpu_ptr(&pt_ctx);
  1128. if (pt->handle.event)
  1129. pt_event_stop(pt->handle.event, PERF_EF_UPDATE);
  1130. }
  1131. static __init int pt_init(void)
  1132. {
  1133. int ret, cpu, prior_warn = 0;
  1134. BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
  1135. if (!boot_cpu_has(X86_FEATURE_INTEL_PT))
  1136. return -ENODEV;
  1137. get_online_cpus();
  1138. for_each_online_cpu(cpu) {
  1139. u64 ctl;
  1140. ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
  1141. if (!ret && (ctl & RTIT_CTL_TRACEEN))
  1142. prior_warn++;
  1143. }
  1144. put_online_cpus();
  1145. if (prior_warn) {
  1146. x86_add_exclusive(x86_lbr_exclusive_pt);
  1147. pr_warn("PT is enabled at boot time, doing nothing\n");
  1148. return -EBUSY;
  1149. }
  1150. ret = pt_pmu_hw_init();
  1151. if (ret)
  1152. return ret;
  1153. if (!pt_cap_get(PT_CAP_topa_output)) {
  1154. pr_warn("ToPA output is not supported on this CPU\n");
  1155. return -ENODEV;
  1156. }
  1157. if (!pt_cap_get(PT_CAP_topa_multiple_entries))
  1158. pt_pmu.pmu.capabilities =
  1159. PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
  1160. pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
  1161. pt_pmu.pmu.attr_groups = pt_attr_groups;
  1162. pt_pmu.pmu.task_ctx_nr = perf_sw_context;
  1163. pt_pmu.pmu.event_init = pt_event_init;
  1164. pt_pmu.pmu.add = pt_event_add;
  1165. pt_pmu.pmu.del = pt_event_del;
  1166. pt_pmu.pmu.start = pt_event_start;
  1167. pt_pmu.pmu.stop = pt_event_stop;
  1168. pt_pmu.pmu.read = pt_event_read;
  1169. pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
  1170. pt_pmu.pmu.free_aux = pt_buffer_free_aux;
  1171. pt_pmu.pmu.addr_filters_sync = pt_event_addr_filters_sync;
  1172. pt_pmu.pmu.addr_filters_validate = pt_event_addr_filters_validate;
  1173. pt_pmu.pmu.nr_addr_filters =
  1174. pt_cap_get(PT_CAP_num_address_ranges);
  1175. ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
  1176. return ret;
  1177. }
  1178. arch_initcall(pt_init);