pt.c 35 KB

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