blob: 354e9ff2978cfef9cf8f6cc2d22e5a739086c5db [file] [log] [blame]
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001/*
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
19#undef DEBUG
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/types.h>
24#include <linux/slab.h>
25#include <linux/device.h>
26
27#include <asm/perf_event.h>
28#include <asm/insn.h>
29#include <asm/io.h>
Takao Indoh24cc12b2015-11-04 14:22:32 +090030#include <asm/intel_pt.h>
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020031
Borislav Petkov27f6d222016-02-10 10:55:23 +010032#include "../perf_event.h"
Borislav Petkovfd1c6012016-02-10 10:55:13 +010033#include "pt.h"
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020034
35static DEFINE_PER_CPU(struct pt, pt_ctx);
36
37static struct pt_pmu pt_pmu;
38
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020039/*
40 * Capabilities of Intel PT hardware, such as number of address bits or
41 * supported output schemes, are cached and exported to userspace as "caps"
42 * attribute group of pt pmu device
43 * (/sys/bus/event_source/devices/intel_pt/caps/) so that userspace can store
44 * relevant bits together with intel_pt traces.
45 *
46 * These are necessary for both trace decoding (payloads_lip, contains address
47 * width encoded in IP-related packets), and event configuration (bitmasks with
48 * permitted values for certain bit fields).
49 */
50#define PT_CAP(_n, _l, _r, _m) \
51 [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
52 .reg = _r, .mask = _m }
53
54static struct pt_cap_desc {
55 const char *name;
56 u32 leaf;
57 u8 reg;
58 u32 mask;
59} pt_caps[] = {
He Chen47f10a32016-11-11 17:25:34 +080060 PT_CAP(max_subleaf, 0, CPUID_EAX, 0xffffffff),
61 PT_CAP(cr3_filtering, 0, CPUID_EBX, BIT(0)),
62 PT_CAP(psb_cyc, 0, CPUID_EBX, BIT(1)),
63 PT_CAP(ip_filtering, 0, CPUID_EBX, BIT(2)),
64 PT_CAP(mtc, 0, CPUID_EBX, BIT(3)),
65 PT_CAP(ptwrite, 0, CPUID_EBX, BIT(4)),
66 PT_CAP(power_event_trace, 0, CPUID_EBX, BIT(5)),
67 PT_CAP(topa_output, 0, CPUID_ECX, BIT(0)),
68 PT_CAP(topa_multiple_entries, 0, CPUID_ECX, BIT(1)),
69 PT_CAP(single_range_output, 0, CPUID_ECX, BIT(2)),
70 PT_CAP(payloads_lip, 0, CPUID_ECX, BIT(31)),
71 PT_CAP(num_address_ranges, 1, CPUID_EAX, 0x3),
72 PT_CAP(mtc_periods, 1, CPUID_EAX, 0xffff0000),
73 PT_CAP(cycle_thresholds, 1, CPUID_EBX, 0xffff),
74 PT_CAP(psb_periods, 1, CPUID_EBX, 0xffff0000),
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020075};
76
77static u32 pt_cap_get(enum pt_capabilities cap)
78{
79 struct pt_cap_desc *cd = &pt_caps[cap];
Takao Indoh709bc872015-08-04 18:36:55 +090080 u32 c = pt_pmu.caps[cd->leaf * PT_CPUID_REGS_NUM + cd->reg];
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +020081 unsigned int shift = __ffs(cd->mask);
82
83 return (c & cd->mask) >> shift;
84}
85
86static ssize_t pt_cap_show(struct device *cdev,
87 struct device_attribute *attr,
88 char *buf)
89{
90 struct dev_ext_attribute *ea =
91 container_of(attr, struct dev_ext_attribute, attr);
92 enum pt_capabilities cap = (long)ea->var;
93
94 return snprintf(buf, PAGE_SIZE, "%x\n", pt_cap_get(cap));
95}
96
97static struct attribute_group pt_cap_group = {
98 .name = "caps",
99};
100
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300101PMU_FORMAT_ATTR(cyc, "config:1" );
Alexander Shishkin54436242017-01-27 17:16:43 +0200102PMU_FORMAT_ATTR(pwr_evt, "config:4" );
103PMU_FORMAT_ATTR(fup_on_ptw, "config:5" );
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300104PMU_FORMAT_ATTR(mtc, "config:9" );
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200105PMU_FORMAT_ATTR(tsc, "config:10" );
106PMU_FORMAT_ATTR(noretcomp, "config:11" );
Alexander Shishkin54436242017-01-27 17:16:43 +0200107PMU_FORMAT_ATTR(ptw, "config:12" );
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300108PMU_FORMAT_ATTR(mtc_period, "config:14-17" );
109PMU_FORMAT_ATTR(cyc_thresh, "config:19-22" );
110PMU_FORMAT_ATTR(psb_period, "config:24-27" );
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200111
112static struct attribute *pt_formats_attr[] = {
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300113 &format_attr_cyc.attr,
Alexander Shishkin54436242017-01-27 17:16:43 +0200114 &format_attr_pwr_evt.attr,
115 &format_attr_fup_on_ptw.attr,
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300116 &format_attr_mtc.attr,
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200117 &format_attr_tsc.attr,
118 &format_attr_noretcomp.attr,
Alexander Shishkin54436242017-01-27 17:16:43 +0200119 &format_attr_ptw.attr,
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300120 &format_attr_mtc_period.attr,
121 &format_attr_cyc_thresh.attr,
122 &format_attr_psb_period.attr,
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200123 NULL,
124};
125
126static struct attribute_group pt_format_group = {
127 .name = "format",
128 .attrs = pt_formats_attr,
129};
130
Alexander Shishkin65c7e6f2015-08-19 17:02:10 +0300131static ssize_t
132pt_timing_attr_show(struct device *dev, struct device_attribute *attr,
133 char *page)
134{
135 struct perf_pmu_events_attr *pmu_attr =
136 container_of(attr, struct perf_pmu_events_attr, attr);
137
138 switch (pmu_attr->id) {
139 case 0:
140 return sprintf(page, "%lu\n", pt_pmu.max_nonturbo_ratio);
141 case 1:
142 return sprintf(page, "%u:%u\n",
143 pt_pmu.tsc_art_num,
144 pt_pmu.tsc_art_den);
145 default:
146 break;
147 }
148
149 return -EINVAL;
150}
151
152PMU_EVENT_ATTR(max_nonturbo_ratio, timing_attr_max_nonturbo_ratio, 0,
153 pt_timing_attr_show);
154PMU_EVENT_ATTR(tsc_art_ratio, timing_attr_tsc_art_ratio, 1,
155 pt_timing_attr_show);
156
157static struct attribute *pt_timing_attr[] = {
158 &timing_attr_max_nonturbo_ratio.attr.attr,
159 &timing_attr_tsc_art_ratio.attr.attr,
160 NULL,
161};
162
163static struct attribute_group pt_timing_group = {
164 .attrs = pt_timing_attr,
165};
166
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200167static const struct attribute_group *pt_attr_groups[] = {
168 &pt_cap_group,
169 &pt_format_group,
Alexander Shishkin65c7e6f2015-08-19 17:02:10 +0300170 &pt_timing_group,
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200171 NULL,
172};
173
174static int __init pt_pmu_hw_init(void)
175{
176 struct dev_ext_attribute *de_attrs;
177 struct attribute **attrs;
178 size_t size;
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300179 u64 reg;
Ingo Molnar066450b2015-04-12 11:11:21 +0200180 int ret;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200181 long i;
182
Alexander Shishkin65c7e6f2015-08-19 17:02:10 +0300183 rdmsrl(MSR_PLATFORM_INFO, reg);
184 pt_pmu.max_nonturbo_ratio = (reg & 0xff00) >> 8;
185
186 /*
187 * if available, read in TSC to core crystal clock ratio,
188 * otherwise, zero for numerator stands for "not enumerated"
189 * as per SDM
190 */
191 if (boot_cpu_data.cpuid_level >= CPUID_TSC_LEAF) {
192 u32 eax, ebx, ecx, edx;
193
194 cpuid(CPUID_TSC_LEAF, &eax, &ebx, &ecx, &edx);
195
196 pt_pmu.tsc_art_num = ebx;
197 pt_pmu.tsc_art_den = eax;
198 }
199
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300200 if (boot_cpu_has(X86_FEATURE_VMX)) {
201 /*
202 * Intel SDM, 36.5 "Tracing post-VMXON" says that
203 * "IA32_VMX_MISC[bit 14]" being 1 means PT can trace
204 * post-VMXON.
205 */
206 rdmsrl(MSR_IA32_VMX_MISC, reg);
207 if (reg & BIT(14))
208 pt_pmu.vmx = true;
209 }
210
Ingo Molnar066450b2015-04-12 11:11:21 +0200211 attrs = NULL;
Ingo Molnar066450b2015-04-12 11:11:21 +0200212
213 for (i = 0; i < PT_CPUID_LEAVES; i++) {
214 cpuid_count(20, i,
He Chen47f10a32016-11-11 17:25:34 +0800215 &pt_pmu.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM],
216 &pt_pmu.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM],
217 &pt_pmu.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM],
218 &pt_pmu.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM]);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200219 }
220
Ingo Molnar066450b2015-04-12 11:11:21 +0200221 ret = -ENOMEM;
222 size = sizeof(struct attribute *) * (ARRAY_SIZE(pt_caps)+1);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200223 attrs = kzalloc(size, GFP_KERNEL);
224 if (!attrs)
Ingo Molnar066450b2015-04-12 11:11:21 +0200225 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200226
Ingo Molnar066450b2015-04-12 11:11:21 +0200227 size = sizeof(struct dev_ext_attribute) * (ARRAY_SIZE(pt_caps)+1);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200228 de_attrs = kzalloc(size, GFP_KERNEL);
229 if (!de_attrs)
Ingo Molnar066450b2015-04-12 11:11:21 +0200230 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200231
232 for (i = 0; i < ARRAY_SIZE(pt_caps); i++) {
Ingo Molnar066450b2015-04-12 11:11:21 +0200233 struct dev_ext_attribute *de_attr = de_attrs + i;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200234
Ingo Molnar066450b2015-04-12 11:11:21 +0200235 de_attr->attr.attr.name = pt_caps[i].name;
236
Alexander Shishkinb44a2b52015-06-04 16:31:47 +0300237 sysfs_attr_init(&de_attr->attr.attr);
Ingo Molnar066450b2015-04-12 11:11:21 +0200238
239 de_attr->attr.attr.mode = S_IRUGO;
240 de_attr->attr.show = pt_cap_show;
241 de_attr->var = (void *)i;
242
243 attrs[i] = &de_attr->attr.attr;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200244 }
245
246 pt_cap_group.attrs = attrs;
Ingo Molnar066450b2015-04-12 11:11:21 +0200247
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200248 return 0;
249
Ingo Molnar066450b2015-04-12 11:11:21 +0200250fail:
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200251 kfree(attrs);
252
Ingo Molnar066450b2015-04-12 11:11:21 +0200253 return ret;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200254}
255
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300256#define RTIT_CTL_CYC_PSB (RTIT_CTL_CYCLEACC | \
257 RTIT_CTL_CYC_THRESH | \
258 RTIT_CTL_PSB_FREQ)
259
260#define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
261 RTIT_CTL_MTC_RANGE)
262
Alexander Shishkin8ee83b22016-09-16 16:48:19 +0300263#define RTIT_CTL_PTW (RTIT_CTL_PTW_EN | \
264 RTIT_CTL_FUP_ON_PTW)
265
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300266#define PT_CONFIG_MASK (RTIT_CTL_TSC_EN | \
267 RTIT_CTL_DISRETC | \
268 RTIT_CTL_CYC_PSB | \
Alexander Shishkin8ee83b22016-09-16 16:48:19 +0300269 RTIT_CTL_MTC | \
270 RTIT_CTL_PWR_EVT_EN | \
271 RTIT_CTL_FUP_ON_PTW | \
272 RTIT_CTL_PTW_EN)
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200273
274static bool pt_event_valid(struct perf_event *event)
275{
276 u64 config = event->attr.config;
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300277 u64 allowed, requested;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200278
279 if ((config & PT_CONFIG_MASK) != config)
280 return false;
281
Alexander Shishkinb1bf72d2015-07-30 16:15:31 +0300282 if (config & RTIT_CTL_CYC_PSB) {
283 if (!pt_cap_get(PT_CAP_psb_cyc))
284 return false;
285
286 allowed = pt_cap_get(PT_CAP_psb_periods);
287 requested = (config & RTIT_CTL_PSB_FREQ) >>
288 RTIT_CTL_PSB_FREQ_OFFSET;
289 if (requested && (!(allowed & BIT(requested))))
290 return false;
291
292 allowed = pt_cap_get(PT_CAP_cycle_thresholds);
293 requested = (config & RTIT_CTL_CYC_THRESH) >>
294 RTIT_CTL_CYC_THRESH_OFFSET;
295 if (requested && (!(allowed & BIT(requested))))
296 return false;
297 }
298
299 if (config & RTIT_CTL_MTC) {
300 /*
301 * In the unlikely case that CPUID lists valid mtc periods,
302 * but not the mtc capability, drop out here.
303 *
304 * Spec says that setting mtc period bits while mtc bit in
305 * CPUID is 0 will #GP, so better safe than sorry.
306 */
307 if (!pt_cap_get(PT_CAP_mtc))
308 return false;
309
310 allowed = pt_cap_get(PT_CAP_mtc_periods);
311 if (!allowed)
312 return false;
313
314 requested = (config & RTIT_CTL_MTC_RANGE) >>
315 RTIT_CTL_MTC_RANGE_OFFSET;
316
317 if (!(allowed & BIT(requested)))
318 return false;
319 }
320
Alexander Shishkin8ee83b22016-09-16 16:48:19 +0300321 if (config & RTIT_CTL_PWR_EVT_EN &&
322 !pt_cap_get(PT_CAP_power_event_trace))
323 return false;
324
325 if (config & RTIT_CTL_PTW) {
326 if (!pt_cap_get(PT_CAP_ptwrite))
327 return false;
328
329 /* FUPonPTW without PTW doesn't make sense */
330 if ((config & RTIT_CTL_FUP_ON_PTW) &&
331 !(config & RTIT_CTL_PTW_EN))
332 return false;
333 }
334
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200335 return true;
336}
337
338/*
339 * PT configuration helpers
340 * These all are cpu affine and operate on a local PT
341 */
342
Alexander Shishkineadf48c2016-04-27 18:44:47 +0300343/* Address ranges and their corresponding msr configuration registers */
344static const struct pt_address_range {
345 unsigned long msr_a;
346 unsigned long msr_b;
347 unsigned int reg_off;
348} pt_address_ranges[] = {
349 {
350 .msr_a = MSR_IA32_RTIT_ADDR0_A,
351 .msr_b = MSR_IA32_RTIT_ADDR0_B,
352 .reg_off = RTIT_CTL_ADDR0_OFFSET,
353 },
354 {
355 .msr_a = MSR_IA32_RTIT_ADDR1_A,
356 .msr_b = MSR_IA32_RTIT_ADDR1_B,
357 .reg_off = RTIT_CTL_ADDR1_OFFSET,
358 },
359 {
360 .msr_a = MSR_IA32_RTIT_ADDR2_A,
361 .msr_b = MSR_IA32_RTIT_ADDR2_B,
362 .reg_off = RTIT_CTL_ADDR2_OFFSET,
363 },
364 {
365 .msr_a = MSR_IA32_RTIT_ADDR3_A,
366 .msr_b = MSR_IA32_RTIT_ADDR3_B,
367 .reg_off = RTIT_CTL_ADDR3_OFFSET,
368 }
369};
370
371static u64 pt_config_filters(struct perf_event *event)
372{
373 struct pt_filters *filters = event->hw.addr_filters;
374 struct pt *pt = this_cpu_ptr(&pt_ctx);
375 unsigned int range = 0;
376 u64 rtit_ctl = 0;
377
378 if (!filters)
379 return 0;
380
381 perf_event_addr_filters_sync(event);
382
383 for (range = 0; range < filters->nr_filters; range++) {
384 struct pt_filter *filter = &filters->filter[range];
385
386 /*
387 * Note, if the range has zero start/end addresses due
388 * to its dynamic object not being loaded yet, we just
389 * go ahead and program zeroed range, which will simply
390 * produce no data. Note^2: if executable code at 0x0
391 * is a concern, we can set up an "invalid" configuration
392 * such as msr_b < msr_a.
393 */
394
395 /* avoid redundant msr writes */
396 if (pt->filters.filter[range].msr_a != filter->msr_a) {
397 wrmsrl(pt_address_ranges[range].msr_a, filter->msr_a);
398 pt->filters.filter[range].msr_a = filter->msr_a;
399 }
400
401 if (pt->filters.filter[range].msr_b != filter->msr_b) {
402 wrmsrl(pt_address_ranges[range].msr_b, filter->msr_b);
403 pt->filters.filter[range].msr_b = filter->msr_b;
404 }
405
406 rtit_ctl |= filter->config << pt_address_ranges[range].reg_off;
407 }
408
409 return rtit_ctl;
410}
411
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200412static void pt_config(struct perf_event *event)
413{
Alexander Shishkinee368422017-02-20 15:33:52 +0200414 struct pt *pt = this_cpu_ptr(&pt_ctx);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200415 u64 reg;
416
Alexander Shishkin9a6694c2015-07-30 16:48:24 +0300417 if (!event->hw.itrace_started) {
418 event->hw.itrace_started = 1;
419 wrmsrl(MSR_IA32_RTIT_STATUS, 0);
420 }
421
Alexander Shishkineadf48c2016-04-27 18:44:47 +0300422 reg = pt_config_filters(event);
423 reg |= RTIT_CTL_TOPA | RTIT_CTL_BRANCH_EN | RTIT_CTL_TRACEEN;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200424
425 if (!event->attr.exclude_kernel)
426 reg |= RTIT_CTL_OS;
427 if (!event->attr.exclude_user)
428 reg |= RTIT_CTL_USR;
429
430 reg |= (event->attr.config & PT_CONFIG_MASK);
431
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300432 event->hw.config = reg;
Alexander Shishkinee368422017-02-20 15:33:52 +0200433 if (READ_ONCE(pt->vmx_on))
434 perf_aux_output_flag(&pt->handle, PERF_AUX_FLAG_PARTIAL);
435 else
436 wrmsrl(MSR_IA32_RTIT_CTL, reg);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200437}
438
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300439static void pt_config_stop(struct perf_event *event)
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200440{
Alexander Shishkinee368422017-02-20 15:33:52 +0200441 struct pt *pt = this_cpu_ptr(&pt_ctx);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300442 u64 ctl = READ_ONCE(event->hw.config);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200443
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300444 /* may be already stopped by a PMI */
445 if (!(ctl & RTIT_CTL_TRACEEN))
446 return;
447
448 ctl &= ~RTIT_CTL_TRACEEN;
Alexander Shishkinee368422017-02-20 15:33:52 +0200449 if (!READ_ONCE(pt->vmx_on))
450 wrmsrl(MSR_IA32_RTIT_CTL, ctl);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200451
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300452 WRITE_ONCE(event->hw.config, ctl);
453
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200454 /*
455 * A wrmsr that disables trace generation serializes other PT
456 * registers and causes all data packets to be written to memory,
457 * but a fence is required for the data to become globally visible.
458 *
459 * The below WMB, separating data store and aux_head store matches
460 * the consumer's RMB that separates aux_head load and data load.
461 */
Alexander Shishkin1c5ac212016-03-29 17:43:10 +0300462 wmb();
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200463}
464
465static void pt_config_buffer(void *buf, unsigned int topa_idx,
466 unsigned int output_off)
467{
468 u64 reg;
469
470 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, virt_to_phys(buf));
471
472 reg = 0x7f | ((u64)topa_idx << 7) | ((u64)output_off << 32);
473
474 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, reg);
475}
476
477/*
478 * Keep ToPA table-related metadata on the same page as the actual table,
479 * taking up a few words from the top
480 */
481
482#define TENTS_PER_PAGE (((PAGE_SIZE - 40) / sizeof(struct topa_entry)) - 1)
483
484/**
485 * struct topa - page-sized ToPA table with metadata at the top
486 * @table: actual ToPA table entries, as understood by PT hardware
487 * @list: linkage to struct pt_buffer's list of tables
488 * @phys: physical address of this page
489 * @offset: offset of the first entry in this table in the buffer
490 * @size: total size of all entries in this table
491 * @last: index of the last initialized entry in this table
492 */
493struct topa {
494 struct topa_entry table[TENTS_PER_PAGE];
495 struct list_head list;
496 u64 phys;
497 u64 offset;
498 size_t size;
499 int last;
500};
501
502/* make -1 stand for the last table entry */
503#define TOPA_ENTRY(t, i) ((i) == -1 ? &(t)->table[(t)->last] : &(t)->table[(i)])
504
505/**
506 * topa_alloc() - allocate page-sized ToPA table
507 * @cpu: CPU on which to allocate.
508 * @gfp: Allocation flags.
509 *
510 * Return: On success, return the pointer to ToPA table page.
511 */
512static struct topa *topa_alloc(int cpu, gfp_t gfp)
513{
514 int node = cpu_to_node(cpu);
515 struct topa *topa;
516 struct page *p;
517
518 p = alloc_pages_node(node, gfp | __GFP_ZERO, 0);
519 if (!p)
520 return NULL;
521
522 topa = page_address(p);
523 topa->last = 0;
524 topa->phys = page_to_phys(p);
525
526 /*
527 * In case of singe-entry ToPA, always put the self-referencing END
528 * link as the 2nd entry in the table
529 */
530 if (!pt_cap_get(PT_CAP_topa_multiple_entries)) {
531 TOPA_ENTRY(topa, 1)->base = topa->phys >> TOPA_SHIFT;
532 TOPA_ENTRY(topa, 1)->end = 1;
533 }
534
535 return topa;
536}
537
538/**
539 * topa_free() - free a page-sized ToPA table
540 * @topa: Table to deallocate.
541 */
542static void topa_free(struct topa *topa)
543{
544 free_page((unsigned long)topa);
545}
546
547/**
548 * topa_insert_table() - insert a ToPA table into a buffer
549 * @buf: PT buffer that's being extended.
550 * @topa: New topa table to be inserted.
551 *
552 * If it's the first table in this buffer, set up buffer's pointers
553 * accordingly; otherwise, add a END=1 link entry to @topa to the current
554 * "last" table and adjust the last table pointer to @topa.
555 */
556static void topa_insert_table(struct pt_buffer *buf, struct topa *topa)
557{
558 struct topa *last = buf->last;
559
560 list_add_tail(&topa->list, &buf->tables);
561
562 if (!buf->first) {
563 buf->first = buf->last = buf->cur = topa;
564 return;
565 }
566
567 topa->offset = last->offset + last->size;
568 buf->last = topa;
569
570 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
571 return;
572
573 BUG_ON(last->last != TENTS_PER_PAGE - 1);
574
575 TOPA_ENTRY(last, -1)->base = topa->phys >> TOPA_SHIFT;
576 TOPA_ENTRY(last, -1)->end = 1;
577}
578
579/**
580 * topa_table_full() - check if a ToPA table is filled up
581 * @topa: ToPA table.
582 */
583static bool topa_table_full(struct topa *topa)
584{
585 /* single-entry ToPA is a special case */
586 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
587 return !!topa->last;
588
589 return topa->last == TENTS_PER_PAGE - 1;
590}
591
592/**
593 * topa_insert_pages() - create a list of ToPA tables
594 * @buf: PT buffer being initialized.
595 * @gfp: Allocation flags.
596 *
597 * This initializes a list of ToPA tables with entries from
598 * the data_pages provided by rb_alloc_aux().
599 *
600 * Return: 0 on success or error code.
601 */
602static int topa_insert_pages(struct pt_buffer *buf, gfp_t gfp)
603{
604 struct topa *topa = buf->last;
605 int order = 0;
606 struct page *p;
607
608 p = virt_to_page(buf->data_pages[buf->nr_pages]);
609 if (PagePrivate(p))
610 order = page_private(p);
611
612 if (topa_table_full(topa)) {
613 topa = topa_alloc(buf->cpu, gfp);
614 if (!topa)
615 return -ENOMEM;
616
617 topa_insert_table(buf, topa);
618 }
619
620 TOPA_ENTRY(topa, -1)->base = page_to_phys(p) >> TOPA_SHIFT;
621 TOPA_ENTRY(topa, -1)->size = order;
622 if (!buf->snapshot && !pt_cap_get(PT_CAP_topa_multiple_entries)) {
623 TOPA_ENTRY(topa, -1)->intr = 1;
624 TOPA_ENTRY(topa, -1)->stop = 1;
625 }
626
627 topa->last++;
628 topa->size += sizes(order);
629
630 buf->nr_pages += 1ul << order;
631
632 return 0;
633}
634
635/**
636 * pt_topa_dump() - print ToPA tables and their entries
637 * @buf: PT buffer.
638 */
639static void pt_topa_dump(struct pt_buffer *buf)
640{
641 struct topa *topa;
642
643 list_for_each_entry(topa, &buf->tables, list) {
644 int i;
645
Ingo Molnar2e54a5b2015-04-02 17:57:59 +0200646 pr_debug("# table @%p (%016Lx), off %llx size %zx\n", topa->table,
647 topa->phys, topa->offset, topa->size);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200648 for (i = 0; i < TENTS_PER_PAGE; i++) {
649 pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
650 &topa->table[i],
651 (unsigned long)topa->table[i].base << TOPA_SHIFT,
652 sizes(topa->table[i].size),
653 topa->table[i].end ? 'E' : ' ',
654 topa->table[i].intr ? 'I' : ' ',
655 topa->table[i].stop ? 'S' : ' ',
656 *(u64 *)&topa->table[i]);
657 if ((pt_cap_get(PT_CAP_topa_multiple_entries) &&
658 topa->table[i].stop) ||
659 topa->table[i].end)
660 break;
661 }
662 }
663}
664
665/**
666 * pt_buffer_advance() - advance to the next output region
667 * @buf: PT buffer.
668 *
669 * Advance the current pointers in the buffer to the next ToPA entry.
670 */
671static void pt_buffer_advance(struct pt_buffer *buf)
672{
673 buf->output_off = 0;
674 buf->cur_idx++;
675
676 if (buf->cur_idx == buf->cur->last) {
677 if (buf->cur == buf->last)
678 buf->cur = buf->first;
679 else
680 buf->cur = list_entry(buf->cur->list.next, struct topa,
681 list);
682 buf->cur_idx = 0;
683 }
684}
685
686/**
687 * pt_update_head() - calculate current offsets and sizes
688 * @pt: Per-cpu pt context.
689 *
690 * Update buffer's current write pointer position and data size.
691 */
692static void pt_update_head(struct pt *pt)
693{
694 struct pt_buffer *buf = perf_get_aux(&pt->handle);
695 u64 topa_idx, base, old;
696
697 /* offset of the first region in this table from the beginning of buf */
698 base = buf->cur->offset + buf->output_off;
699
700 /* offset of the current output region within this table */
701 for (topa_idx = 0; topa_idx < buf->cur_idx; topa_idx++)
702 base += sizes(buf->cur->table[topa_idx].size);
703
704 if (buf->snapshot) {
705 local_set(&buf->data_size, base);
706 } else {
707 old = (local64_xchg(&buf->head, base) &
708 ((buf->nr_pages << PAGE_SHIFT) - 1));
709 if (base < old)
710 base += buf->nr_pages << PAGE_SHIFT;
711
712 local_add(base - old, &buf->data_size);
713 }
714}
715
716/**
717 * pt_buffer_region() - obtain current output region's address
718 * @buf: PT buffer.
719 */
720static void *pt_buffer_region(struct pt_buffer *buf)
721{
722 return phys_to_virt(buf->cur->table[buf->cur_idx].base << TOPA_SHIFT);
723}
724
725/**
726 * pt_buffer_region_size() - obtain current output region's size
727 * @buf: PT buffer.
728 */
729static size_t pt_buffer_region_size(struct pt_buffer *buf)
730{
731 return sizes(buf->cur->table[buf->cur_idx].size);
732}
733
734/**
735 * pt_handle_status() - take care of possible status conditions
736 * @pt: Per-cpu pt context.
737 */
738static void pt_handle_status(struct pt *pt)
739{
740 struct pt_buffer *buf = perf_get_aux(&pt->handle);
741 int advance = 0;
742 u64 status;
743
744 rdmsrl(MSR_IA32_RTIT_STATUS, status);
745
746 if (status & RTIT_STATUS_ERROR) {
747 pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
748 pt_topa_dump(buf);
749 status &= ~RTIT_STATUS_ERROR;
750 }
751
752 if (status & RTIT_STATUS_STOPPED) {
753 status &= ~RTIT_STATUS_STOPPED;
754
755 /*
756 * On systems that only do single-entry ToPA, hitting STOP
757 * means we are already losing data; need to let the decoder
758 * know.
759 */
760 if (!pt_cap_get(PT_CAP_topa_multiple_entries) ||
761 buf->output_off == sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200762 perf_aux_output_flag(&pt->handle,
763 PERF_AUX_FLAG_TRUNCATED);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200764 advance++;
765 }
766 }
767
768 /*
769 * Also on single-entry ToPA implementations, interrupt will come
770 * before the output reaches its output region's boundary.
771 */
772 if (!pt_cap_get(PT_CAP_topa_multiple_entries) && !buf->snapshot &&
773 pt_buffer_region_size(buf) - buf->output_off <= TOPA_PMI_MARGIN) {
774 void *head = pt_buffer_region(buf);
775
776 /* everything within this margin needs to be zeroed out */
777 memset(head + buf->output_off, 0,
778 pt_buffer_region_size(buf) -
779 buf->output_off);
780 advance++;
781 }
782
783 if (advance)
784 pt_buffer_advance(buf);
785
786 wrmsrl(MSR_IA32_RTIT_STATUS, status);
787}
788
789/**
790 * pt_read_offset() - translate registers into buffer pointers
791 * @buf: PT buffer.
792 *
793 * Set buffer's output pointers from MSR values.
794 */
795static void pt_read_offset(struct pt_buffer *buf)
796{
797 u64 offset, base_topa;
798
799 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, base_topa);
800 buf->cur = phys_to_virt(base_topa);
801
802 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, offset);
803 /* offset within current output region */
804 buf->output_off = offset >> 32;
805 /* index of current output region within this table */
806 buf->cur_idx = (offset & 0xffffff80) >> 7;
807}
808
809/**
810 * pt_topa_next_entry() - obtain index of the first page in the next ToPA entry
811 * @buf: PT buffer.
812 * @pg: Page offset in the buffer.
813 *
814 * When advancing to the next output region (ToPA entry), given a page offset
815 * into the buffer, we need to find the offset of the first page in the next
816 * region.
817 */
818static unsigned int pt_topa_next_entry(struct pt_buffer *buf, unsigned int pg)
819{
820 struct topa_entry *te = buf->topa_index[pg];
821
822 /* one region */
823 if (buf->first == buf->last && buf->first->last == 1)
824 return pg;
825
826 do {
827 pg++;
828 pg &= buf->nr_pages - 1;
829 } while (buf->topa_index[pg] == te);
830
831 return pg;
832}
833
834/**
835 * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
836 * @buf: PT buffer.
837 * @handle: Current output handle.
838 *
839 * Place INT and STOP marks to prevent overwriting old data that the consumer
Alexander Shishkincf302bf2015-04-21 16:16:15 +0300840 * hasn't yet collected and waking up the consumer after a certain fraction of
841 * the buffer has filled up. Only needed and sensible for non-snapshot counters.
842 *
843 * This obviously relies on buf::head to figure out buffer markers, so it has
844 * to be called after pt_buffer_reset_offsets() and before the hardware tracing
845 * is enabled.
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200846 */
847static int pt_buffer_reset_markers(struct pt_buffer *buf,
848 struct perf_output_handle *handle)
849
850{
Alexander Shishkinf73ec482015-05-22 18:30:22 +0300851 unsigned long head = local64_read(&buf->head);
852 unsigned long idx, npages, wakeup;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200853
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200854 /* can't stop in the middle of an output region */
855 if (buf->output_off + handle->size + 1 <
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200856 sizes(TOPA_ENTRY(buf->cur, buf->cur_idx)->size)) {
857 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200858 return -EINVAL;
Will Deaconf4c0b0a2017-02-20 15:33:50 +0200859 }
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200860
861
862 /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
863 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
864 return 0;
865
866 /* clear STOP and INT from current entry */
867 buf->topa_index[buf->stop_pos]->stop = 0;
Alexander Shishkin5fbe4782016-05-10 16:18:32 +0300868 buf->topa_index[buf->stop_pos]->intr = 0;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200869 buf->topa_index[buf->intr_pos]->intr = 0;
870
Alexander Shishkinf73ec482015-05-22 18:30:22 +0300871 /* how many pages till the STOP marker */
872 npages = handle->size >> PAGE_SHIFT;
873
874 /* if it's on a page boundary, fill up one more page */
875 if (!offset_in_page(head + handle->size + 1))
876 npages++;
877
878 idx = (head >> PAGE_SHIFT) + npages;
879 idx &= buf->nr_pages - 1;
880 buf->stop_pos = idx;
881
882 wakeup = handle->wakeup >> PAGE_SHIFT;
883
884 /* in the worst case, wake up the consumer one page before hard stop */
885 idx = (head >> PAGE_SHIFT) + npages - 1;
886 if (idx > wakeup)
887 idx = wakeup;
888
889 idx &= buf->nr_pages - 1;
890 buf->intr_pos = idx;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200891
892 buf->topa_index[buf->stop_pos]->stop = 1;
Alexander Shishkin5fbe4782016-05-10 16:18:32 +0300893 buf->topa_index[buf->stop_pos]->intr = 1;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200894 buf->topa_index[buf->intr_pos]->intr = 1;
895
896 return 0;
897}
898
899/**
900 * pt_buffer_setup_topa_index() - build topa_index[] table of regions
901 * @buf: PT buffer.
902 *
903 * topa_index[] references output regions indexed by offset into the
904 * buffer for purposes of quick reverse lookup.
905 */
906static void pt_buffer_setup_topa_index(struct pt_buffer *buf)
907{
908 struct topa *cur = buf->first, *prev = buf->last;
909 struct topa_entry *te_cur = TOPA_ENTRY(cur, 0),
910 *te_prev = TOPA_ENTRY(prev, prev->last - 1);
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300911 int pg = 0, idx = 0;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200912
913 while (pg < buf->nr_pages) {
914 int tidx;
915
916 /* pages within one topa entry */
917 for (tidx = 0; tidx < 1 << te_cur->size; tidx++, pg++)
918 buf->topa_index[pg] = te_prev;
919
920 te_prev = te_cur;
921
922 if (idx == cur->last - 1) {
923 /* advance to next topa table */
924 idx = 0;
925 cur = list_entry(cur->list.next, struct topa, list);
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300926 } else {
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200927 idx++;
Alexander Shishkin74387bc2015-04-21 16:16:13 +0300928 }
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200929 te_cur = TOPA_ENTRY(cur, idx);
930 }
931
932}
933
934/**
935 * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
936 * @buf: PT buffer.
937 * @head: Write pointer (aux_head) from AUX buffer.
938 *
939 * Find the ToPA table and entry corresponding to given @head and set buffer's
Alexander Shishkin5b1dbd12015-04-21 16:16:16 +0300940 * "current" pointers accordingly. This is done after we have obtained the
941 * current aux_head position from a successful call to perf_aux_output_begin()
942 * to make sure the hardware is writing to the right place.
943 *
944 * This function modifies buf::{cur,cur_idx,output_off} that will be programmed
945 * into PT msrs when the tracing is enabled and buf::head and buf::data_size,
946 * which are used to determine INT and STOP markers' locations by a subsequent
947 * call to pt_buffer_reset_markers().
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +0200948 */
949static void pt_buffer_reset_offsets(struct pt_buffer *buf, unsigned long head)
950{
951 int pg;
952
953 if (buf->snapshot)
954 head &= (buf->nr_pages << PAGE_SHIFT) - 1;
955
956 pg = (head >> PAGE_SHIFT) & (buf->nr_pages - 1);
957 pg = pt_topa_next_entry(buf, pg);
958
959 buf->cur = (struct topa *)((unsigned long)buf->topa_index[pg] & PAGE_MASK);
960 buf->cur_idx = ((unsigned long)buf->topa_index[pg] -
961 (unsigned long)buf->cur) / sizeof(struct topa_entry);
962 buf->output_off = head & (sizes(buf->cur->table[buf->cur_idx].size) - 1);
963
964 local64_set(&buf->head, head);
965 local_set(&buf->data_size, 0);
966}
967
968/**
969 * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
970 * @buf: PT buffer.
971 */
972static void pt_buffer_fini_topa(struct pt_buffer *buf)
973{
974 struct topa *topa, *iter;
975
976 list_for_each_entry_safe(topa, iter, &buf->tables, list) {
977 /*
978 * right now, this is in free_aux() path only, so
979 * no need to unlink this table from the list
980 */
981 topa_free(topa);
982 }
983}
984
985/**
986 * pt_buffer_init_topa() - initialize ToPA table for pt buffer
987 * @buf: PT buffer.
988 * @size: Total size of all regions within this ToPA.
989 * @gfp: Allocation flags.
990 */
991static int pt_buffer_init_topa(struct pt_buffer *buf, unsigned long nr_pages,
992 gfp_t gfp)
993{
994 struct topa *topa;
995 int err;
996
997 topa = topa_alloc(buf->cpu, gfp);
998 if (!topa)
999 return -ENOMEM;
1000
1001 topa_insert_table(buf, topa);
1002
1003 while (buf->nr_pages < nr_pages) {
1004 err = topa_insert_pages(buf, gfp);
1005 if (err) {
1006 pt_buffer_fini_topa(buf);
1007 return -ENOMEM;
1008 }
1009 }
1010
1011 pt_buffer_setup_topa_index(buf);
1012
1013 /* link last table to the first one, unless we're double buffering */
1014 if (pt_cap_get(PT_CAP_topa_multiple_entries)) {
1015 TOPA_ENTRY(buf->last, -1)->base = buf->first->phys >> TOPA_SHIFT;
1016 TOPA_ENTRY(buf->last, -1)->end = 1;
1017 }
1018
1019 pt_topa_dump(buf);
1020 return 0;
1021}
1022
1023/**
1024 * pt_buffer_setup_aux() - set up topa tables for a PT buffer
1025 * @cpu: Cpu on which to allocate, -1 means current.
1026 * @pages: Array of pointers to buffer pages passed from perf core.
1027 * @nr_pages: Number of pages in the buffer.
1028 * @snapshot: If this is a snapshot/overwrite counter.
1029 *
1030 * This is a pmu::setup_aux callback that sets up ToPA tables and all the
1031 * bookkeeping for an AUX buffer.
1032 *
1033 * Return: Our private PT buffer structure.
1034 */
1035static void *
1036pt_buffer_setup_aux(int cpu, void **pages, int nr_pages, bool snapshot)
1037{
1038 struct pt_buffer *buf;
1039 int node, ret;
1040
1041 if (!nr_pages)
1042 return NULL;
1043
1044 if (cpu == -1)
1045 cpu = raw_smp_processor_id();
1046 node = cpu_to_node(cpu);
1047
1048 buf = kzalloc_node(offsetof(struct pt_buffer, topa_index[nr_pages]),
1049 GFP_KERNEL, node);
1050 if (!buf)
1051 return NULL;
1052
1053 buf->cpu = cpu;
1054 buf->snapshot = snapshot;
1055 buf->data_pages = pages;
1056
1057 INIT_LIST_HEAD(&buf->tables);
1058
1059 ret = pt_buffer_init_topa(buf, nr_pages, GFP_KERNEL);
1060 if (ret) {
1061 kfree(buf);
1062 return NULL;
1063 }
1064
1065 return buf;
1066}
1067
1068/**
1069 * pt_buffer_free_aux() - perf AUX deallocation path callback
1070 * @data: PT buffer.
1071 */
1072static void pt_buffer_free_aux(void *data)
1073{
1074 struct pt_buffer *buf = data;
1075
1076 pt_buffer_fini_topa(buf);
1077 kfree(buf);
1078}
1079
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001080static int pt_addr_filters_init(struct perf_event *event)
1081{
1082 struct pt_filters *filters;
1083 int node = event->cpu == -1 ? -1 : cpu_to_node(event->cpu);
1084
1085 if (!pt_cap_get(PT_CAP_num_address_ranges))
1086 return 0;
1087
1088 filters = kzalloc_node(sizeof(struct pt_filters), GFP_KERNEL, node);
1089 if (!filters)
1090 return -ENOMEM;
1091
1092 if (event->parent)
1093 memcpy(filters, event->parent->hw.addr_filters,
1094 sizeof(*filters));
1095
1096 event->hw.addr_filters = filters;
1097
1098 return 0;
1099}
1100
1101static void pt_addr_filters_fini(struct perf_event *event)
1102{
1103 kfree(event->hw.addr_filters);
1104 event->hw.addr_filters = NULL;
1105}
1106
Alexander Shishkinddfdad92016-09-15 18:13:51 +03001107static inline bool valid_kernel_ip(unsigned long ip)
1108{
1109 return virt_addr_valid(ip) && kernel_ip(ip);
1110}
1111
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001112static int pt_event_addr_filters_validate(struct list_head *filters)
1113{
1114 struct perf_addr_filter *filter;
1115 int range = 0;
1116
1117 list_for_each_entry(filter, filters, entry) {
1118 /* PT doesn't support single address triggers */
Alexander Shishkin95f60082016-09-15 18:13:50 +03001119 if (!filter->range || !filter->size)
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001120 return -EOPNOTSUPP;
1121
Alexander Shishkin1155baf2016-09-15 18:13:52 +03001122 if (!filter->inode) {
1123 if (!valid_kernel_ip(filter->offset))
1124 return -EINVAL;
1125
1126 if (!valid_kernel_ip(filter->offset + filter->size))
1127 return -EINVAL;
1128 }
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001129
1130 if (++range > pt_cap_get(PT_CAP_num_address_ranges))
1131 return -EOPNOTSUPP;
1132 }
1133
1134 return 0;
1135}
1136
1137static void pt_event_addr_filters_sync(struct perf_event *event)
1138{
1139 struct perf_addr_filters_head *head = perf_event_addr_filters(event);
1140 unsigned long msr_a, msr_b, *offs = event->addr_filters_offs;
1141 struct pt_filters *filters = event->hw.addr_filters;
1142 struct perf_addr_filter *filter;
1143 int range = 0;
1144
1145 if (!filters)
1146 return;
1147
1148 list_for_each_entry(filter, &head->list, entry) {
1149 if (filter->inode && !offs[range]) {
1150 msr_a = msr_b = 0;
1151 } else {
1152 /* apply the offset */
1153 msr_a = filter->offset + offs[range];
Alexander Shishkin95f60082016-09-15 18:13:50 +03001154 msr_b = filter->size + msr_a - 1;
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001155 }
1156
1157 filters->filter[range].msr_a = msr_a;
1158 filters->filter[range].msr_b = msr_b;
1159 filters->filter[range].config = filter->filter ? 1 : 2;
1160 range++;
1161 }
1162
1163 filters->nr_filters = range;
1164}
1165
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001166/**
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001167 * intel_pt_interrupt() - PT PMI handler
1168 */
1169void intel_pt_interrupt(void)
1170{
1171 struct pt *pt = this_cpu_ptr(&pt_ctx);
1172 struct pt_buffer *buf;
1173 struct perf_event *event = pt->handle.event;
1174
1175 /*
1176 * There may be a dangling PT bit in the interrupt status register
1177 * after PT has been disabled by pt_event_stop(). Make sure we don't
1178 * do anything (particularly, re-enable) for this event here.
1179 */
Alexander Shishkin1b6de592016-04-28 18:35:44 +03001180 if (!READ_ONCE(pt->handle_nmi))
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001181 return;
1182
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001183 if (!event)
1184 return;
1185
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001186 pt_config_stop(event);
1187
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001188 buf = perf_get_aux(&pt->handle);
1189 if (!buf)
1190 return;
1191
1192 pt_read_offset(buf);
1193
1194 pt_handle_status(pt);
1195
1196 pt_update_head(pt);
1197
Will Deaconf4c0b0a2017-02-20 15:33:50 +02001198 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0));
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001199
1200 if (!event->hw.state) {
1201 int ret;
1202
1203 buf = perf_aux_output_begin(&pt->handle, event);
1204 if (!buf) {
1205 event->hw.state = PERF_HES_STOPPED;
1206 return;
1207 }
1208
1209 pt_buffer_reset_offsets(buf, pt->handle.head);
Alexander Shishkincf302bf2015-04-21 16:16:15 +03001210 /* snapshot counters don't use PMI, so it's safe */
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001211 ret = pt_buffer_reset_markers(buf, &pt->handle);
1212 if (ret) {
Will Deaconf4c0b0a2017-02-20 15:33:50 +02001213 perf_aux_output_end(&pt->handle, 0);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001214 return;
1215 }
1216
1217 pt_config_buffer(buf->cur->table, buf->cur_idx,
1218 buf->output_off);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001219 pt_config(event);
1220 }
1221}
1222
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001223void intel_pt_handle_vmx(int on)
1224{
1225 struct pt *pt = this_cpu_ptr(&pt_ctx);
1226 struct perf_event *event;
1227 unsigned long flags;
1228
1229 /* PT plays nice with VMX, do nothing */
1230 if (pt_pmu.vmx)
1231 return;
1232
1233 /*
1234 * VMXON will clear RTIT_CTL.TraceEn; we need to make
1235 * sure to not try to set it while VMX is on. Disable
1236 * interrupts to avoid racing with pmu callbacks;
1237 * concurrent PMI should be handled fine.
1238 */
1239 local_irq_save(flags);
1240 WRITE_ONCE(pt->vmx_on, on);
1241
Alexander Shishkinee368422017-02-20 15:33:52 +02001242 /*
1243 * If an AUX transaction is in progress, it will contain
1244 * gap(s), so flag it PARTIAL to inform the user.
1245 */
1246 event = pt->handle.event;
1247 if (event)
1248 perf_aux_output_flag(&pt->handle,
1249 PERF_AUX_FLAG_PARTIAL);
1250
1251 /* Turn PTs back on */
1252 if (!on && event)
1253 wrmsrl(MSR_IA32_RTIT_CTL, event->hw.config);
1254
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001255 local_irq_restore(flags);
1256}
1257EXPORT_SYMBOL_GPL(intel_pt_handle_vmx);
1258
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001259/*
1260 * PMU callbacks
1261 */
1262
1263static void pt_event_start(struct perf_event *event, int mode)
1264{
Alexander Shishkin66d21902016-03-04 15:42:48 +02001265 struct hw_perf_event *hwc = &event->hw;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001266 struct pt *pt = this_cpu_ptr(&pt_ctx);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001267 struct pt_buffer *buf;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001268
Alexander Shishkin66d21902016-03-04 15:42:48 +02001269 buf = perf_aux_output_begin(&pt->handle, event);
1270 if (!buf)
1271 goto fail_stop;
1272
1273 pt_buffer_reset_offsets(buf, pt->handle.head);
1274 if (!buf->snapshot) {
1275 if (pt_buffer_reset_markers(buf, &pt->handle))
1276 goto fail_end_stop;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001277 }
1278
Alexander Shishkin1b6de592016-04-28 18:35:44 +03001279 WRITE_ONCE(pt->handle_nmi, 1);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001280 hwc->state = 0;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001281
1282 pt_config_buffer(buf->cur->table, buf->cur_idx,
1283 buf->output_off);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001284 pt_config(event);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001285
1286 return;
1287
1288fail_end_stop:
Will Deaconf4c0b0a2017-02-20 15:33:50 +02001289 perf_aux_output_end(&pt->handle, 0);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001290fail_stop:
1291 hwc->state = PERF_HES_STOPPED;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001292}
1293
1294static void pt_event_stop(struct perf_event *event, int mode)
1295{
1296 struct pt *pt = this_cpu_ptr(&pt_ctx);
1297
1298 /*
1299 * Protect against the PMI racing with disabling wrmsr,
1300 * see comment in intel_pt_interrupt().
1301 */
Alexander Shishkin1b6de592016-04-28 18:35:44 +03001302 WRITE_ONCE(pt->handle_nmi, 0);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03001303
1304 pt_config_stop(event);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001305
1306 if (event->hw.state == PERF_HES_STOPPED)
1307 return;
1308
1309 event->hw.state = PERF_HES_STOPPED;
1310
1311 if (mode & PERF_EF_UPDATE) {
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001312 struct pt_buffer *buf = perf_get_aux(&pt->handle);
1313
1314 if (!buf)
1315 return;
1316
1317 if (WARN_ON_ONCE(pt->handle.event != event))
1318 return;
1319
1320 pt_read_offset(buf);
1321
1322 pt_handle_status(pt);
1323
1324 pt_update_head(pt);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001325
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001326 if (buf->snapshot)
1327 pt->handle.head =
1328 local_xchg(&buf->data_size,
1329 buf->nr_pages << PAGE_SHIFT);
Will Deaconf4c0b0a2017-02-20 15:33:50 +02001330 perf_aux_output_end(&pt->handle, local_xchg(&buf->data_size, 0));
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001331 }
1332}
1333
Alexander Shishkin66d21902016-03-04 15:42:48 +02001334static void pt_event_del(struct perf_event *event, int mode)
1335{
1336 pt_event_stop(event, PERF_EF_UPDATE);
1337}
1338
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001339static int pt_event_add(struct perf_event *event, int mode)
1340{
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001341 struct pt *pt = this_cpu_ptr(&pt_ctx);
1342 struct hw_perf_event *hwc = &event->hw;
1343 int ret = -EBUSY;
1344
1345 if (pt->handle.event)
Ingo Molnar0c992412015-04-16 12:38:30 +02001346 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001347
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001348 if (mode & PERF_EF_START) {
1349 pt_event_start(event, 0);
Alexander Shishkin66d21902016-03-04 15:42:48 +02001350 ret = -EINVAL;
Ingo Molnar0c992412015-04-16 12:38:30 +02001351 if (hwc->state == PERF_HES_STOPPED)
Alexander Shishkin66d21902016-03-04 15:42:48 +02001352 goto fail;
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001353 } else {
1354 hwc->state = PERF_HES_STOPPED;
1355 }
1356
Alexander Shishkin66d21902016-03-04 15:42:48 +02001357 ret = 0;
Ingo Molnar0c992412015-04-16 12:38:30 +02001358fail:
Alexander Shishkin66d21902016-03-04 15:42:48 +02001359
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001360 return ret;
1361}
1362
1363static void pt_event_read(struct perf_event *event)
1364{
1365}
1366
1367static void pt_event_destroy(struct perf_event *event)
1368{
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001369 pt_addr_filters_fini(event);
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001370 x86_del_exclusive(x86_lbr_exclusive_pt);
1371}
1372
1373static int pt_event_init(struct perf_event *event)
1374{
1375 if (event->attr.type != pt_pmu.pmu.type)
1376 return -ENOENT;
1377
1378 if (!pt_event_valid(event))
1379 return -EINVAL;
1380
1381 if (x86_add_exclusive(x86_lbr_exclusive_pt))
1382 return -EBUSY;
1383
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001384 if (pt_addr_filters_init(event)) {
1385 x86_del_exclusive(x86_lbr_exclusive_pt);
1386 return -ENOMEM;
1387 }
1388
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001389 event->destroy = pt_event_destroy;
1390
1391 return 0;
1392}
1393
Takao Indoh24cc12b2015-11-04 14:22:32 +09001394void cpu_emergency_stop_pt(void)
1395{
1396 struct pt *pt = this_cpu_ptr(&pt_ctx);
1397
1398 if (pt->handle.event)
1399 pt_event_stop(pt->handle.event, PERF_EF_UPDATE);
1400}
1401
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001402static __init int pt_init(void)
1403{
1404 int ret, cpu, prior_warn = 0;
1405
1406 BUILD_BUG_ON(sizeof(struct topa) > PAGE_SIZE);
Huaitong Han73fdeb62015-08-31 16:21:02 +08001407
Alexander Shishkine465de12016-04-06 17:35:07 +03001408 if (!boot_cpu_has(X86_FEATURE_INTEL_PT))
Huaitong Han73fdeb62015-08-31 16:21:02 +08001409 return -ENODEV;
1410
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001411 get_online_cpus();
1412 for_each_online_cpu(cpu) {
1413 u64 ctl;
1414
1415 ret = rdmsrl_safe_on_cpu(cpu, MSR_IA32_RTIT_CTL, &ctl);
1416 if (!ret && (ctl & RTIT_CTL_TRACEEN))
1417 prior_warn++;
1418 }
1419 put_online_cpus();
1420
1421 if (prior_warn) {
1422 x86_add_exclusive(x86_lbr_exclusive_pt);
1423 pr_warn("PT is enabled at boot time, doing nothing\n");
1424
1425 return -EBUSY;
1426 }
1427
1428 ret = pt_pmu_hw_init();
1429 if (ret)
1430 return ret;
1431
1432 if (!pt_cap_get(PT_CAP_topa_output)) {
1433 pr_warn("ToPA output is not supported on this CPU\n");
1434 return -ENODEV;
1435 }
1436
1437 if (!pt_cap_get(PT_CAP_topa_multiple_entries))
1438 pt_pmu.pmu.capabilities =
1439 PERF_PMU_CAP_AUX_NO_SG | PERF_PMU_CAP_AUX_SW_DOUBLEBUF;
1440
1441 pt_pmu.pmu.capabilities |= PERF_PMU_CAP_EXCLUSIVE | PERF_PMU_CAP_ITRACE;
Alexander Shishkineadf48c2016-04-27 18:44:47 +03001442 pt_pmu.pmu.attr_groups = pt_attr_groups;
1443 pt_pmu.pmu.task_ctx_nr = perf_sw_context;
1444 pt_pmu.pmu.event_init = pt_event_init;
1445 pt_pmu.pmu.add = pt_event_add;
1446 pt_pmu.pmu.del = pt_event_del;
1447 pt_pmu.pmu.start = pt_event_start;
1448 pt_pmu.pmu.stop = pt_event_stop;
1449 pt_pmu.pmu.read = pt_event_read;
1450 pt_pmu.pmu.setup_aux = pt_buffer_setup_aux;
1451 pt_pmu.pmu.free_aux = pt_buffer_free_aux;
1452 pt_pmu.pmu.addr_filters_sync = pt_event_addr_filters_sync;
1453 pt_pmu.pmu.addr_filters_validate = pt_event_addr_filters_validate;
1454 pt_pmu.pmu.nr_addr_filters =
1455 pt_cap_get(PT_CAP_num_address_ranges);
1456
Alexander Shishkin52ca9ce2015-01-30 12:39:52 +02001457 ret = perf_pmu_register(&pt_pmu.pmu, "intel_pt", -1);
1458
1459 return ret;
1460}
Paul Gortmaker5b00c1e2015-05-01 21:57:34 -04001461arch_initcall(pt_init);