pt.c 38 KB

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