1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel(R) Processor Trace PMU driver for perf
4 * Copyright (c) 2013-2014, Intel Corporation.
6 * Intel PT is specified in the Intel Architecture Instruction Set Extensions
7 * Programming Reference:
8 * http://software.intel.com/en-us/intel-isa-extensions
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
19 #include <asm/perf_event.h>
22 #include <asm/intel_pt.h>
23 #include <asm/intel-family.h>
25 #include "../perf_event.h"
28 static DEFINE_PER_CPU(struct pt
, pt_ctx
);
30 static struct pt_pmu pt_pmu
;
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.
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).
43 #define PT_CAP(_n, _l, _r, _m) \
44 [PT_CAP_ ## _n] = { .name = __stringify(_n), .leaf = _l, \
45 .reg = _r, .mask = _m }
47 static struct pt_cap_desc
{
53 PT_CAP(max_subleaf
, 0, CPUID_EAX
, 0xffffffff),
54 PT_CAP(cr3_filtering
, 0, CPUID_EBX
, BIT(0)),
55 PT_CAP(psb_cyc
, 0, CPUID_EBX
, BIT(1)),
56 PT_CAP(ip_filtering
, 0, CPUID_EBX
, BIT(2)),
57 PT_CAP(mtc
, 0, CPUID_EBX
, BIT(3)),
58 PT_CAP(ptwrite
, 0, CPUID_EBX
, BIT(4)),
59 PT_CAP(power_event_trace
, 0, CPUID_EBX
, BIT(5)),
60 PT_CAP(topa_output
, 0, CPUID_ECX
, BIT(0)),
61 PT_CAP(topa_multiple_entries
, 0, CPUID_ECX
, BIT(1)),
62 PT_CAP(single_range_output
, 0, CPUID_ECX
, BIT(2)),
63 PT_CAP(output_subsys
, 0, CPUID_ECX
, BIT(3)),
64 PT_CAP(payloads_lip
, 0, CPUID_ECX
, BIT(31)),
65 PT_CAP(num_address_ranges
, 1, CPUID_EAX
, 0x3),
66 PT_CAP(mtc_periods
, 1, CPUID_EAX
, 0xffff0000),
67 PT_CAP(cycle_thresholds
, 1, CPUID_EBX
, 0xffff),
68 PT_CAP(psb_periods
, 1, CPUID_EBX
, 0xffff0000),
71 u32
intel_pt_validate_cap(u32
*caps
, enum pt_capabilities capability
)
73 struct pt_cap_desc
*cd
= &pt_caps
[capability
];
74 u32 c
= caps
[cd
->leaf
* PT_CPUID_REGS_NUM
+ cd
->reg
];
75 unsigned int shift
= __ffs(cd
->mask
);
77 return (c
& cd
->mask
) >> shift
;
79 EXPORT_SYMBOL_GPL(intel_pt_validate_cap
);
81 u32
intel_pt_validate_hw_cap(enum pt_capabilities cap
)
83 return intel_pt_validate_cap(pt_pmu
.caps
, cap
);
85 EXPORT_SYMBOL_GPL(intel_pt_validate_hw_cap
);
87 static ssize_t
pt_cap_show(struct device
*cdev
,
88 struct device_attribute
*attr
,
91 struct dev_ext_attribute
*ea
=
92 container_of(attr
, struct dev_ext_attribute
, attr
);
93 enum pt_capabilities cap
= (long)ea
->var
;
95 return snprintf(buf
, PAGE_SIZE
, "%x\n", intel_pt_validate_hw_cap(cap
));
98 static struct attribute_group pt_cap_group __ro_after_init
= {
102 PMU_FORMAT_ATTR(pt
, "config:0" );
103 PMU_FORMAT_ATTR(cyc
, "config:1" );
104 PMU_FORMAT_ATTR(pwr_evt
, "config:4" );
105 PMU_FORMAT_ATTR(fup_on_ptw
, "config:5" );
106 PMU_FORMAT_ATTR(mtc
, "config:9" );
107 PMU_FORMAT_ATTR(tsc
, "config:10" );
108 PMU_FORMAT_ATTR(noretcomp
, "config:11" );
109 PMU_FORMAT_ATTR(ptw
, "config:12" );
110 PMU_FORMAT_ATTR(branch
, "config:13" );
111 PMU_FORMAT_ATTR(mtc_period
, "config:14-17" );
112 PMU_FORMAT_ATTR(cyc_thresh
, "config:19-22" );
113 PMU_FORMAT_ATTR(psb_period
, "config:24-27" );
115 static struct attribute
*pt_formats_attr
[] = {
116 &format_attr_pt
.attr
,
117 &format_attr_cyc
.attr
,
118 &format_attr_pwr_evt
.attr
,
119 &format_attr_fup_on_ptw
.attr
,
120 &format_attr_mtc
.attr
,
121 &format_attr_tsc
.attr
,
122 &format_attr_noretcomp
.attr
,
123 &format_attr_ptw
.attr
,
124 &format_attr_branch
.attr
,
125 &format_attr_mtc_period
.attr
,
126 &format_attr_cyc_thresh
.attr
,
127 &format_attr_psb_period
.attr
,
131 static struct attribute_group pt_format_group
= {
133 .attrs
= pt_formats_attr
,
137 pt_timing_attr_show(struct device
*dev
, struct device_attribute
*attr
,
140 struct perf_pmu_events_attr
*pmu_attr
=
141 container_of(attr
, struct perf_pmu_events_attr
, attr
);
143 switch (pmu_attr
->id
) {
145 return sprintf(page
, "%lu\n", pt_pmu
.max_nonturbo_ratio
);
147 return sprintf(page
, "%u:%u\n",
157 PMU_EVENT_ATTR(max_nonturbo_ratio
, timing_attr_max_nonturbo_ratio
, 0,
158 pt_timing_attr_show
);
159 PMU_EVENT_ATTR(tsc_art_ratio
, timing_attr_tsc_art_ratio
, 1,
160 pt_timing_attr_show
);
162 static struct attribute
*pt_timing_attr
[] = {
163 &timing_attr_max_nonturbo_ratio
.attr
.attr
,
164 &timing_attr_tsc_art_ratio
.attr
.attr
,
168 static struct attribute_group pt_timing_group
= {
169 .attrs
= pt_timing_attr
,
172 static const struct attribute_group
*pt_attr_groups
[] = {
179 static int __init
pt_pmu_hw_init(void)
181 struct dev_ext_attribute
*de_attrs
;
182 struct attribute
**attrs
;
188 rdmsrl(MSR_PLATFORM_INFO
, reg
);
189 pt_pmu
.max_nonturbo_ratio
= (reg
& 0xff00) >> 8;
192 * if available, read in TSC to core crystal clock ratio,
193 * otherwise, zero for numerator stands for "not enumerated"
196 if (boot_cpu_data
.cpuid_level
>= CPUID_TSC_LEAF
) {
197 u32 eax
, ebx
, ecx
, edx
;
199 cpuid(CPUID_TSC_LEAF
, &eax
, &ebx
, &ecx
, &edx
);
201 pt_pmu
.tsc_art_num
= ebx
;
202 pt_pmu
.tsc_art_den
= eax
;
205 /* model-specific quirks */
206 switch (boot_cpu_data
.x86_model
) {
207 case INTEL_FAM6_BROADWELL
:
208 case INTEL_FAM6_BROADWELL_D
:
209 case INTEL_FAM6_BROADWELL_G
:
210 case INTEL_FAM6_BROADWELL_X
:
211 /* not setting BRANCH_EN will #GP, erratum BDM106 */
212 pt_pmu
.branch_en_always_on
= true;
218 if (boot_cpu_has(X86_FEATURE_VMX
)) {
220 * Intel SDM, 36.5 "Tracing post-VMXON" says that
221 * "IA32_VMX_MISC[bit 14]" being 1 means PT can trace
224 rdmsrl(MSR_IA32_VMX_MISC
, reg
);
231 for (i
= 0; i
< PT_CPUID_LEAVES
; i
++) {
233 &pt_pmu
.caps
[CPUID_EAX
+ i
*PT_CPUID_REGS_NUM
],
234 &pt_pmu
.caps
[CPUID_EBX
+ i
*PT_CPUID_REGS_NUM
],
235 &pt_pmu
.caps
[CPUID_ECX
+ i
*PT_CPUID_REGS_NUM
],
236 &pt_pmu
.caps
[CPUID_EDX
+ i
*PT_CPUID_REGS_NUM
]);
240 size
= sizeof(struct attribute
*) * (ARRAY_SIZE(pt_caps
)+1);
241 attrs
= kzalloc(size
, GFP_KERNEL
);
245 size
= sizeof(struct dev_ext_attribute
) * (ARRAY_SIZE(pt_caps
)+1);
246 de_attrs
= kzalloc(size
, GFP_KERNEL
);
250 for (i
= 0; i
< ARRAY_SIZE(pt_caps
); i
++) {
251 struct dev_ext_attribute
*de_attr
= de_attrs
+ i
;
253 de_attr
->attr
.attr
.name
= pt_caps
[i
].name
;
255 sysfs_attr_init(&de_attr
->attr
.attr
);
257 de_attr
->attr
.attr
.mode
= S_IRUGO
;
258 de_attr
->attr
.show
= pt_cap_show
;
259 de_attr
->var
= (void *)i
;
261 attrs
[i
] = &de_attr
->attr
.attr
;
264 pt_cap_group
.attrs
= attrs
;
274 #define RTIT_CTL_CYC_PSB (RTIT_CTL_CYCLEACC | \
275 RTIT_CTL_CYC_THRESH | \
278 #define RTIT_CTL_MTC (RTIT_CTL_MTC_EN | \
281 #define RTIT_CTL_PTW (RTIT_CTL_PTW_EN | \
285 * Bit 0 (TraceEn) in the attr.config is meaningless as the
286 * corresponding bit in the RTIT_CTL can only be controlled
287 * by the driver; therefore, repurpose it to mean: pass
288 * through the bit that was previously assumed to be always
289 * on for PT, thereby allowing the user to *not* set it if
290 * they so wish. See also pt_event_valid() and pt_config().
292 #define RTIT_CTL_PASSTHROUGH RTIT_CTL_TRACEEN
294 #define PT_CONFIG_MASK (RTIT_CTL_TRACEEN | \
297 RTIT_CTL_BRANCH_EN | \
300 RTIT_CTL_PWR_EVT_EN | \
301 RTIT_CTL_FUP_ON_PTW | \
304 static bool pt_event_valid(struct perf_event
*event
)
306 u64 config
= event
->attr
.config
;
307 u64 allowed
, requested
;
309 if ((config
& PT_CONFIG_MASK
) != config
)
312 if (config
& RTIT_CTL_CYC_PSB
) {
313 if (!intel_pt_validate_hw_cap(PT_CAP_psb_cyc
))
316 allowed
= intel_pt_validate_hw_cap(PT_CAP_psb_periods
);
317 requested
= (config
& RTIT_CTL_PSB_FREQ
) >>
318 RTIT_CTL_PSB_FREQ_OFFSET
;
319 if (requested
&& (!(allowed
& BIT(requested
))))
322 allowed
= intel_pt_validate_hw_cap(PT_CAP_cycle_thresholds
);
323 requested
= (config
& RTIT_CTL_CYC_THRESH
) >>
324 RTIT_CTL_CYC_THRESH_OFFSET
;
325 if (requested
&& (!(allowed
& BIT(requested
))))
329 if (config
& RTIT_CTL_MTC
) {
331 * In the unlikely case that CPUID lists valid mtc periods,
332 * but not the mtc capability, drop out here.
334 * Spec says that setting mtc period bits while mtc bit in
335 * CPUID is 0 will #GP, so better safe than sorry.
337 if (!intel_pt_validate_hw_cap(PT_CAP_mtc
))
340 allowed
= intel_pt_validate_hw_cap(PT_CAP_mtc_periods
);
344 requested
= (config
& RTIT_CTL_MTC_RANGE
) >>
345 RTIT_CTL_MTC_RANGE_OFFSET
;
347 if (!(allowed
& BIT(requested
)))
351 if (config
& RTIT_CTL_PWR_EVT_EN
&&
352 !intel_pt_validate_hw_cap(PT_CAP_power_event_trace
))
355 if (config
& RTIT_CTL_PTW
) {
356 if (!intel_pt_validate_hw_cap(PT_CAP_ptwrite
))
359 /* FUPonPTW without PTW doesn't make sense */
360 if ((config
& RTIT_CTL_FUP_ON_PTW
) &&
361 !(config
& RTIT_CTL_PTW_EN
))
366 * Setting bit 0 (TraceEn in RTIT_CTL MSR) in the attr.config
367 * clears the assomption that BranchEn must always be enabled,
368 * as was the case with the first implementation of PT.
369 * If this bit is not set, the legacy behavior is preserved
370 * for compatibility with the older userspace.
372 * Re-using bit 0 for this purpose is fine because it is never
373 * directly set by the user; previous attempts at setting it in
374 * the attr.config resulted in -EINVAL.
376 if (config
& RTIT_CTL_PASSTHROUGH
) {
378 * Disallow not setting BRANCH_EN where BRANCH_EN is
381 if (pt_pmu
.branch_en_always_on
&&
382 !(config
& RTIT_CTL_BRANCH_EN
))
386 * Disallow BRANCH_EN without the PASSTHROUGH.
388 if (config
& RTIT_CTL_BRANCH_EN
)
396 * PT configuration helpers
397 * These all are cpu affine and operate on a local PT
400 static void pt_config_start(struct perf_event
*event
)
402 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
403 u64 ctl
= event
->hw
.config
;
405 ctl
|= RTIT_CTL_TRACEEN
;
406 if (READ_ONCE(pt
->vmx_on
))
407 perf_aux_output_flag(&pt
->handle
, PERF_AUX_FLAG_PARTIAL
);
409 wrmsrl(MSR_IA32_RTIT_CTL
, ctl
);
411 WRITE_ONCE(event
->hw
.config
, ctl
);
414 /* Address ranges and their corresponding msr configuration registers */
415 static const struct pt_address_range
{
418 unsigned int reg_off
;
419 } pt_address_ranges
[] = {
421 .msr_a
= MSR_IA32_RTIT_ADDR0_A
,
422 .msr_b
= MSR_IA32_RTIT_ADDR0_B
,
423 .reg_off
= RTIT_CTL_ADDR0_OFFSET
,
426 .msr_a
= MSR_IA32_RTIT_ADDR1_A
,
427 .msr_b
= MSR_IA32_RTIT_ADDR1_B
,
428 .reg_off
= RTIT_CTL_ADDR1_OFFSET
,
431 .msr_a
= MSR_IA32_RTIT_ADDR2_A
,
432 .msr_b
= MSR_IA32_RTIT_ADDR2_B
,
433 .reg_off
= RTIT_CTL_ADDR2_OFFSET
,
436 .msr_a
= MSR_IA32_RTIT_ADDR3_A
,
437 .msr_b
= MSR_IA32_RTIT_ADDR3_B
,
438 .reg_off
= RTIT_CTL_ADDR3_OFFSET
,
442 static u64
pt_config_filters(struct perf_event
*event
)
444 struct pt_filters
*filters
= event
->hw
.addr_filters
;
445 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
446 unsigned int range
= 0;
452 perf_event_addr_filters_sync(event
);
454 for (range
= 0; range
< filters
->nr_filters
; range
++) {
455 struct pt_filter
*filter
= &filters
->filter
[range
];
458 * Note, if the range has zero start/end addresses due
459 * to its dynamic object not being loaded yet, we just
460 * go ahead and program zeroed range, which will simply
461 * produce no data. Note^2: if executable code at 0x0
462 * is a concern, we can set up an "invalid" configuration
463 * such as msr_b < msr_a.
466 /* avoid redundant msr writes */
467 if (pt
->filters
.filter
[range
].msr_a
!= filter
->msr_a
) {
468 wrmsrl(pt_address_ranges
[range
].msr_a
, filter
->msr_a
);
469 pt
->filters
.filter
[range
].msr_a
= filter
->msr_a
;
472 if (pt
->filters
.filter
[range
].msr_b
!= filter
->msr_b
) {
473 wrmsrl(pt_address_ranges
[range
].msr_b
, filter
->msr_b
);
474 pt
->filters
.filter
[range
].msr_b
= filter
->msr_b
;
477 rtit_ctl
|= filter
->config
<< pt_address_ranges
[range
].reg_off
;
483 static void pt_config(struct perf_event
*event
)
485 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
486 struct pt_buffer
*buf
= perf_get_aux(&pt
->handle
);
489 /* First round: clear STATUS, in particular the PSB byte counter. */
490 if (!event
->hw
.config
) {
491 perf_event_itrace_started(event
);
492 wrmsrl(MSR_IA32_RTIT_STATUS
, 0);
495 reg
= pt_config_filters(event
);
496 reg
|= RTIT_CTL_TRACEEN
;
498 reg
|= RTIT_CTL_TOPA
;
501 * Previously, we had BRANCH_EN on by default, but now that PT has
502 * grown features outside of branch tracing, it is useful to allow
503 * the user to disable it. Setting bit 0 in the event's attr.config
504 * allows BRANCH_EN to pass through instead of being always on. See
505 * also the comment in pt_event_valid().
507 if (event
->attr
.config
& BIT(0)) {
508 reg
|= event
->attr
.config
& RTIT_CTL_BRANCH_EN
;
510 reg
|= RTIT_CTL_BRANCH_EN
;
513 if (!event
->attr
.exclude_kernel
)
515 if (!event
->attr
.exclude_user
)
518 reg
|= (event
->attr
.config
& PT_CONFIG_MASK
);
520 event
->hw
.config
= reg
;
521 pt_config_start(event
);
524 static void pt_config_stop(struct perf_event
*event
)
526 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
527 u64 ctl
= READ_ONCE(event
->hw
.config
);
529 /* may be already stopped by a PMI */
530 if (!(ctl
& RTIT_CTL_TRACEEN
))
533 ctl
&= ~RTIT_CTL_TRACEEN
;
534 if (!READ_ONCE(pt
->vmx_on
))
535 wrmsrl(MSR_IA32_RTIT_CTL
, ctl
);
537 WRITE_ONCE(event
->hw
.config
, ctl
);
540 * A wrmsr that disables trace generation serializes other PT
541 * registers and causes all data packets to be written to memory,
542 * but a fence is required for the data to become globally visible.
544 * The below WMB, separating data store and aux_head store matches
545 * the consumer's RMB that separates aux_head load and data load.
551 * struct topa - ToPA metadata
552 * @list: linkage to struct pt_buffer's list of tables
553 * @offset: offset of the first entry in this table in the buffer
554 * @size: total size of all entries in this table
555 * @last: index of the last initialized entry in this table
556 * @z_count: how many times the first entry repeats
559 struct list_head list
;
563 unsigned int z_count
;
567 * Keep ToPA table-related metadata on the same page as the actual table,
568 * taking up a few words from the top
571 #define TENTS_PER_PAGE \
572 ((PAGE_SIZE - sizeof(struct topa)) / sizeof(struct topa_entry))
575 * struct topa_page - page-sized ToPA table with metadata at the top
576 * @table: actual ToPA table entries, as understood by PT hardware
580 struct topa_entry table
[TENTS_PER_PAGE
];
584 static inline struct topa_page
*topa_to_page(struct topa
*topa
)
586 return container_of(topa
, struct topa_page
, topa
);
589 static inline struct topa_page
*topa_entry_to_page(struct topa_entry
*te
)
591 return (struct topa_page
*)((unsigned long)te
& PAGE_MASK
);
594 static inline phys_addr_t
topa_pfn(struct topa
*topa
)
596 return PFN_DOWN(virt_to_phys(topa_to_page(topa
)));
599 /* make -1 stand for the last table entry */
600 #define TOPA_ENTRY(t, i) \
602 ? &topa_to_page(t)->table[(t)->last] \
603 : &topa_to_page(t)->table[(i)])
604 #define TOPA_ENTRY_SIZE(t, i) (sizes(TOPA_ENTRY((t), (i))->size))
605 #define TOPA_ENTRY_PAGES(t, i) (1 << TOPA_ENTRY((t), (i))->size)
607 static void pt_config_buffer(struct pt_buffer
*buf
)
609 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
614 base
= buf
->data_pages
[0];
615 mask
= (buf
->nr_pages
* PAGE_SIZE
- 1) >> 7;
617 base
= topa_to_page(buf
->cur
)->table
;
618 mask
= (u64
)buf
->cur_idx
;
621 reg
= virt_to_phys(base
);
622 if (pt
->output_base
!= reg
) {
623 pt
->output_base
= reg
;
624 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE
, reg
);
627 reg
= 0x7f | (mask
<< 7) | ((u64
)buf
->output_off
<< 32);
628 if (pt
->output_mask
!= reg
) {
629 pt
->output_mask
= reg
;
630 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK
, reg
);
635 * topa_alloc() - allocate page-sized ToPA table
636 * @cpu: CPU on which to allocate.
637 * @gfp: Allocation flags.
639 * Return: On success, return the pointer to ToPA table page.
641 static struct topa
*topa_alloc(int cpu
, gfp_t gfp
)
643 int node
= cpu_to_node(cpu
);
644 struct topa_page
*tp
;
647 p
= alloc_pages_node(node
, gfp
| __GFP_ZERO
, 0);
651 tp
= page_address(p
);
655 * In case of singe-entry ToPA, always put the self-referencing END
656 * link as the 2nd entry in the table
658 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
)) {
659 TOPA_ENTRY(&tp
->topa
, 1)->base
= page_to_phys(p
) >> TOPA_SHIFT
;
660 TOPA_ENTRY(&tp
->topa
, 1)->end
= 1;
667 * topa_free() - free a page-sized ToPA table
668 * @topa: Table to deallocate.
670 static void topa_free(struct topa
*topa
)
672 free_page((unsigned long)topa
);
676 * topa_insert_table() - insert a ToPA table into a buffer
677 * @buf: PT buffer that's being extended.
678 * @topa: New topa table to be inserted.
680 * If it's the first table in this buffer, set up buffer's pointers
681 * accordingly; otherwise, add a END=1 link entry to @topa to the current
682 * "last" table and adjust the last table pointer to @topa.
684 static void topa_insert_table(struct pt_buffer
*buf
, struct topa
*topa
)
686 struct topa
*last
= buf
->last
;
688 list_add_tail(&topa
->list
, &buf
->tables
);
691 buf
->first
= buf
->last
= buf
->cur
= topa
;
695 topa
->offset
= last
->offset
+ last
->size
;
698 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
))
701 BUG_ON(last
->last
!= TENTS_PER_PAGE
- 1);
703 TOPA_ENTRY(last
, -1)->base
= topa_pfn(topa
);
704 TOPA_ENTRY(last
, -1)->end
= 1;
708 * topa_table_full() - check if a ToPA table is filled up
711 static bool topa_table_full(struct topa
*topa
)
713 /* single-entry ToPA is a special case */
714 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
))
717 return topa
->last
== TENTS_PER_PAGE
- 1;
721 * topa_insert_pages() - create a list of ToPA tables
722 * @buf: PT buffer being initialized.
723 * @gfp: Allocation flags.
725 * This initializes a list of ToPA tables with entries from
726 * the data_pages provided by rb_alloc_aux().
728 * Return: 0 on success or error code.
730 static int topa_insert_pages(struct pt_buffer
*buf
, int cpu
, gfp_t gfp
)
732 struct topa
*topa
= buf
->last
;
736 p
= virt_to_page(buf
->data_pages
[buf
->nr_pages
]);
738 order
= page_private(p
);
740 if (topa_table_full(topa
)) {
741 topa
= topa_alloc(cpu
, gfp
);
745 topa_insert_table(buf
, topa
);
748 if (topa
->z_count
== topa
->last
- 1) {
749 if (order
== TOPA_ENTRY(topa
, topa
->last
- 1)->size
)
753 TOPA_ENTRY(topa
, -1)->base
= page_to_phys(p
) >> TOPA_SHIFT
;
754 TOPA_ENTRY(topa
, -1)->size
= order
;
755 if (!buf
->snapshot
&&
756 !intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
)) {
757 TOPA_ENTRY(topa
, -1)->intr
= 1;
758 TOPA_ENTRY(topa
, -1)->stop
= 1;
762 topa
->size
+= sizes(order
);
764 buf
->nr_pages
+= 1ul << order
;
770 * pt_topa_dump() - print ToPA tables and their entries
773 static void pt_topa_dump(struct pt_buffer
*buf
)
777 list_for_each_entry(topa
, &buf
->tables
, list
) {
778 struct topa_page
*tp
= topa_to_page(topa
);
781 pr_debug("# table @%p, off %llx size %zx\n", tp
->table
,
782 topa
->offset
, topa
->size
);
783 for (i
= 0; i
< TENTS_PER_PAGE
; i
++) {
784 pr_debug("# entry @%p (%lx sz %u %c%c%c) raw=%16llx\n",
786 (unsigned long)tp
->table
[i
].base
<< TOPA_SHIFT
,
787 sizes(tp
->table
[i
].size
),
788 tp
->table
[i
].end
? 'E' : ' ',
789 tp
->table
[i
].intr
? 'I' : ' ',
790 tp
->table
[i
].stop
? 'S' : ' ',
791 *(u64
*)&tp
->table
[i
]);
792 if ((intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
) &&
793 tp
->table
[i
].stop
) ||
796 if (!i
&& topa
->z_count
)
803 * pt_buffer_advance() - advance to the next output region
806 * Advance the current pointers in the buffer to the next ToPA entry.
808 static void pt_buffer_advance(struct pt_buffer
*buf
)
813 if (buf
->cur_idx
== buf
->cur
->last
) {
814 if (buf
->cur
== buf
->last
)
815 buf
->cur
= buf
->first
;
817 buf
->cur
= list_entry(buf
->cur
->list
.next
, struct topa
,
824 * pt_update_head() - calculate current offsets and sizes
825 * @pt: Per-cpu pt context.
827 * Update buffer's current write pointer position and data size.
829 static void pt_update_head(struct pt
*pt
)
831 struct pt_buffer
*buf
= perf_get_aux(&pt
->handle
);
832 u64 topa_idx
, base
, old
;
835 local_set(&buf
->data_size
, buf
->output_off
);
839 /* offset of the first region in this table from the beginning of buf */
840 base
= buf
->cur
->offset
+ buf
->output_off
;
842 /* offset of the current output region within this table */
843 for (topa_idx
= 0; topa_idx
< buf
->cur_idx
; topa_idx
++)
844 base
+= TOPA_ENTRY_SIZE(buf
->cur
, topa_idx
);
847 local_set(&buf
->data_size
, base
);
849 old
= (local64_xchg(&buf
->head
, base
) &
850 ((buf
->nr_pages
<< PAGE_SHIFT
) - 1));
852 base
+= buf
->nr_pages
<< PAGE_SHIFT
;
854 local_add(base
- old
, &buf
->data_size
);
859 * pt_buffer_region() - obtain current output region's address
862 static void *pt_buffer_region(struct pt_buffer
*buf
)
864 return phys_to_virt(TOPA_ENTRY(buf
->cur
, buf
->cur_idx
)->base
<< TOPA_SHIFT
);
868 * pt_buffer_region_size() - obtain current output region's size
871 static size_t pt_buffer_region_size(struct pt_buffer
*buf
)
873 return TOPA_ENTRY_SIZE(buf
->cur
, buf
->cur_idx
);
877 * pt_handle_status() - take care of possible status conditions
878 * @pt: Per-cpu pt context.
880 static void pt_handle_status(struct pt
*pt
)
882 struct pt_buffer
*buf
= perf_get_aux(&pt
->handle
);
886 rdmsrl(MSR_IA32_RTIT_STATUS
, status
);
888 if (status
& RTIT_STATUS_ERROR
) {
889 pr_err_ratelimited("ToPA ERROR encountered, trying to recover\n");
891 status
&= ~RTIT_STATUS_ERROR
;
894 if (status
& RTIT_STATUS_STOPPED
) {
895 status
&= ~RTIT_STATUS_STOPPED
;
898 * On systems that only do single-entry ToPA, hitting STOP
899 * means we are already losing data; need to let the decoder
902 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
) ||
903 buf
->output_off
== pt_buffer_region_size(buf
)) {
904 perf_aux_output_flag(&pt
->handle
,
905 PERF_AUX_FLAG_TRUNCATED
);
911 * Also on single-entry ToPA implementations, interrupt will come
912 * before the output reaches its output region's boundary.
914 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
) &&
916 pt_buffer_region_size(buf
) - buf
->output_off
<= TOPA_PMI_MARGIN
) {
917 void *head
= pt_buffer_region(buf
);
919 /* everything within this margin needs to be zeroed out */
920 memset(head
+ buf
->output_off
, 0,
921 pt_buffer_region_size(buf
) -
927 pt_buffer_advance(buf
);
929 wrmsrl(MSR_IA32_RTIT_STATUS
, status
);
933 * pt_read_offset() - translate registers into buffer pointers
936 * Set buffer's output pointers from MSR values.
938 static void pt_read_offset(struct pt_buffer
*buf
)
940 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
941 struct topa_page
*tp
;
944 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE
, pt
->output_base
);
945 tp
= phys_to_virt(pt
->output_base
);
946 buf
->cur
= &tp
->topa
;
949 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK
, pt
->output_mask
);
950 /* offset within current output region */
951 buf
->output_off
= pt
->output_mask
>> 32;
952 /* index of current output region within this table */
954 buf
->cur_idx
= (pt
->output_mask
& 0xffffff80) >> 7;
957 static struct topa_entry
*
958 pt_topa_entry_for_page(struct pt_buffer
*buf
, unsigned int pg
)
960 struct topa_page
*tp
;
962 unsigned int idx
, cur_pg
= 0, z_pg
= 0, start_idx
= 0;
965 * Indicates a bug in the caller.
967 if (WARN_ON_ONCE(pg
>= buf
->nr_pages
))
971 * First, find the ToPA table where @pg fits. With high
972 * order allocations, there shouldn't be many of these.
974 list_for_each_entry(topa
, &buf
->tables
, list
) {
975 if (topa
->offset
+ topa
->size
> pg
<< PAGE_SHIFT
)
980 * Hitting this means we have a problem in the ToPA
989 * Indicates a problem in the ToPA allocation code.
991 if (WARN_ON_ONCE(topa
->last
== -1))
994 tp
= topa_to_page(topa
);
995 cur_pg
= PFN_DOWN(topa
->offset
);
997 z_pg
= TOPA_ENTRY_PAGES(topa
, 0) * (topa
->z_count
+ 1);
998 start_idx
= topa
->z_count
+ 1;
1002 * Multiple entries at the beginning of the table have the same size,
1003 * ideally all of them; if @pg falls there, the search is done.
1005 if (pg
>= cur_pg
&& pg
< cur_pg
+ z_pg
) {
1006 idx
= (pg
- cur_pg
) / TOPA_ENTRY_PAGES(topa
, 0);
1007 return &tp
->table
[idx
];
1011 * Otherwise, slow path: iterate through the remaining entries.
1013 for (idx
= start_idx
, cur_pg
+= z_pg
; idx
< topa
->last
; idx
++) {
1014 if (cur_pg
+ TOPA_ENTRY_PAGES(topa
, idx
) > pg
)
1015 return &tp
->table
[idx
];
1017 cur_pg
+= TOPA_ENTRY_PAGES(topa
, idx
);
1021 * Means we couldn't find a ToPA entry in the table that does match.
1028 static struct topa_entry
*
1029 pt_topa_prev_entry(struct pt_buffer
*buf
, struct topa_entry
*te
)
1031 unsigned long table
= (unsigned long)te
& ~(PAGE_SIZE
- 1);
1032 struct topa_page
*tp
;
1035 tp
= (struct topa_page
*)table
;
1036 if (tp
->table
!= te
)
1040 if (topa
== buf
->first
)
1043 topa
= list_prev_entry(topa
, list
);
1045 tp
= topa_to_page(topa
);
1047 return &tp
->table
[topa
->last
- 1];
1051 * pt_buffer_reset_markers() - place interrupt and stop bits in the buffer
1053 * @handle: Current output handle.
1055 * Place INT and STOP marks to prevent overwriting old data that the consumer
1056 * hasn't yet collected and waking up the consumer after a certain fraction of
1057 * the buffer has filled up. Only needed and sensible for non-snapshot counters.
1059 * This obviously relies on buf::head to figure out buffer markers, so it has
1060 * to be called after pt_buffer_reset_offsets() and before the hardware tracing
1063 static int pt_buffer_reset_markers(struct pt_buffer
*buf
,
1064 struct perf_output_handle
*handle
)
1067 unsigned long head
= local64_read(&buf
->head
);
1068 unsigned long idx
, npages
, wakeup
;
1073 /* can't stop in the middle of an output region */
1074 if (buf
->output_off
+ handle
->size
+ 1 < pt_buffer_region_size(buf
)) {
1075 perf_aux_output_flag(handle
, PERF_AUX_FLAG_TRUNCATED
);
1080 /* single entry ToPA is handled by marking all regions STOP=1 INT=1 */
1081 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
))
1084 /* clear STOP and INT from current entry */
1086 buf
->stop_te
->stop
= 0;
1087 buf
->stop_te
->intr
= 0;
1091 buf
->intr_te
->intr
= 0;
1093 /* how many pages till the STOP marker */
1094 npages
= handle
->size
>> PAGE_SHIFT
;
1096 /* if it's on a page boundary, fill up one more page */
1097 if (!offset_in_page(head
+ handle
->size
+ 1))
1100 idx
= (head
>> PAGE_SHIFT
) + npages
;
1101 idx
&= buf
->nr_pages
- 1;
1103 if (idx
!= buf
->stop_pos
) {
1104 buf
->stop_pos
= idx
;
1105 buf
->stop_te
= pt_topa_entry_for_page(buf
, idx
);
1106 buf
->stop_te
= pt_topa_prev_entry(buf
, buf
->stop_te
);
1109 wakeup
= handle
->wakeup
>> PAGE_SHIFT
;
1111 /* in the worst case, wake up the consumer one page before hard stop */
1112 idx
= (head
>> PAGE_SHIFT
) + npages
- 1;
1116 idx
&= buf
->nr_pages
- 1;
1117 if (idx
!= buf
->intr_pos
) {
1118 buf
->intr_pos
= idx
;
1119 buf
->intr_te
= pt_topa_entry_for_page(buf
, idx
);
1120 buf
->intr_te
= pt_topa_prev_entry(buf
, buf
->intr_te
);
1123 buf
->stop_te
->stop
= 1;
1124 buf
->stop_te
->intr
= 1;
1125 buf
->intr_te
->intr
= 1;
1131 * pt_buffer_reset_offsets() - adjust buffer's write pointers from aux_head
1133 * @head: Write pointer (aux_head) from AUX buffer.
1135 * Find the ToPA table and entry corresponding to given @head and set buffer's
1136 * "current" pointers accordingly. This is done after we have obtained the
1137 * current aux_head position from a successful call to perf_aux_output_begin()
1138 * to make sure the hardware is writing to the right place.
1140 * This function modifies buf::{cur,cur_idx,output_off} that will be programmed
1141 * into PT msrs when the tracing is enabled and buf::head and buf::data_size,
1142 * which are used to determine INT and STOP markers' locations by a subsequent
1143 * call to pt_buffer_reset_markers().
1145 static void pt_buffer_reset_offsets(struct pt_buffer
*buf
, unsigned long head
)
1147 struct topa_page
*cur_tp
;
1148 struct topa_entry
*te
;
1152 head
&= (buf
->nr_pages
<< PAGE_SHIFT
) - 1;
1155 pg
= (head
>> PAGE_SHIFT
) & (buf
->nr_pages
- 1);
1156 te
= pt_topa_entry_for_page(buf
, pg
);
1158 cur_tp
= topa_entry_to_page(te
);
1159 buf
->cur
= &cur_tp
->topa
;
1160 buf
->cur_idx
= te
- TOPA_ENTRY(buf
->cur
, 0);
1161 buf
->output_off
= head
& (pt_buffer_region_size(buf
) - 1);
1163 buf
->output_off
= head
;
1166 local64_set(&buf
->head
, head
);
1167 local_set(&buf
->data_size
, 0);
1171 * pt_buffer_fini_topa() - deallocate ToPA structure of a buffer
1174 static void pt_buffer_fini_topa(struct pt_buffer
*buf
)
1176 struct topa
*topa
, *iter
;
1181 list_for_each_entry_safe(topa
, iter
, &buf
->tables
, list
) {
1183 * right now, this is in free_aux() path only, so
1184 * no need to unlink this table from the list
1191 * pt_buffer_init_topa() - initialize ToPA table for pt buffer
1193 * @size: Total size of all regions within this ToPA.
1194 * @gfp: Allocation flags.
1196 static int pt_buffer_init_topa(struct pt_buffer
*buf
, int cpu
,
1197 unsigned long nr_pages
, gfp_t gfp
)
1202 topa
= topa_alloc(cpu
, gfp
);
1206 topa_insert_table(buf
, topa
);
1208 while (buf
->nr_pages
< nr_pages
) {
1209 err
= topa_insert_pages(buf
, cpu
, gfp
);
1211 pt_buffer_fini_topa(buf
);
1216 /* link last table to the first one, unless we're double buffering */
1217 if (intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
)) {
1218 TOPA_ENTRY(buf
->last
, -1)->base
= topa_pfn(buf
->first
);
1219 TOPA_ENTRY(buf
->last
, -1)->end
= 1;
1226 static int pt_buffer_try_single(struct pt_buffer
*buf
, int nr_pages
)
1228 struct page
*p
= virt_to_page(buf
->data_pages
[0]);
1229 int ret
= -ENOTSUPP
, order
= 0;
1232 * We can use single range output mode
1233 * + in snapshot mode, where we don't need interrupts;
1234 * + if the hardware supports it;
1235 * + if the entire buffer is one contiguous allocation.
1240 if (!intel_pt_validate_hw_cap(PT_CAP_single_range_output
))
1244 order
= page_private(p
);
1246 if (1 << order
!= nr_pages
)
1250 buf
->nr_pages
= nr_pages
;
1257 * pt_buffer_setup_aux() - set up topa tables for a PT buffer
1258 * @cpu: Cpu on which to allocate, -1 means current.
1259 * @pages: Array of pointers to buffer pages passed from perf core.
1260 * @nr_pages: Number of pages in the buffer.
1261 * @snapshot: If this is a snapshot/overwrite counter.
1263 * This is a pmu::setup_aux callback that sets up ToPA tables and all the
1264 * bookkeeping for an AUX buffer.
1266 * Return: Our private PT buffer structure.
1269 pt_buffer_setup_aux(struct perf_event
*event
, void **pages
,
1270 int nr_pages
, bool snapshot
)
1272 struct pt_buffer
*buf
;
1273 int node
, ret
, cpu
= event
->cpu
;
1279 * Only support AUX sampling in snapshot mode, where we don't
1282 if (event
->attr
.aux_sample_size
&& !snapshot
)
1286 cpu
= raw_smp_processor_id();
1287 node
= cpu_to_node(cpu
);
1289 buf
= kzalloc_node(sizeof(struct pt_buffer
), GFP_KERNEL
, node
);
1293 buf
->snapshot
= snapshot
;
1294 buf
->data_pages
= pages
;
1298 INIT_LIST_HEAD(&buf
->tables
);
1300 ret
= pt_buffer_try_single(buf
, nr_pages
);
1304 ret
= pt_buffer_init_topa(buf
, cpu
, nr_pages
, GFP_KERNEL
);
1314 * pt_buffer_free_aux() - perf AUX deallocation path callback
1317 static void pt_buffer_free_aux(void *data
)
1319 struct pt_buffer
*buf
= data
;
1321 pt_buffer_fini_topa(buf
);
1325 static int pt_addr_filters_init(struct perf_event
*event
)
1327 struct pt_filters
*filters
;
1328 int node
= event
->cpu
== -1 ? -1 : cpu_to_node(event
->cpu
);
1330 if (!intel_pt_validate_hw_cap(PT_CAP_num_address_ranges
))
1333 filters
= kzalloc_node(sizeof(struct pt_filters
), GFP_KERNEL
, node
);
1338 memcpy(filters
, event
->parent
->hw
.addr_filters
,
1341 event
->hw
.addr_filters
= filters
;
1346 static void pt_addr_filters_fini(struct perf_event
*event
)
1348 kfree(event
->hw
.addr_filters
);
1349 event
->hw
.addr_filters
= NULL
;
1352 static inline bool valid_kernel_ip(unsigned long ip
)
1354 return virt_addr_valid(ip
) && kernel_ip(ip
);
1357 static int pt_event_addr_filters_validate(struct list_head
*filters
)
1359 struct perf_addr_filter
*filter
;
1362 list_for_each_entry(filter
, filters
, entry
) {
1364 * PT doesn't support single address triggers and
1367 if (!filter
->size
||
1368 filter
->action
== PERF_ADDR_FILTER_ACTION_START
)
1371 if (!filter
->path
.dentry
) {
1372 if (!valid_kernel_ip(filter
->offset
))
1375 if (!valid_kernel_ip(filter
->offset
+ filter
->size
))
1379 if (++range
> intel_pt_validate_hw_cap(PT_CAP_num_address_ranges
))
1386 static void pt_event_addr_filters_sync(struct perf_event
*event
)
1388 struct perf_addr_filters_head
*head
= perf_event_addr_filters(event
);
1389 unsigned long msr_a
, msr_b
;
1390 struct perf_addr_filter_range
*fr
= event
->addr_filter_ranges
;
1391 struct pt_filters
*filters
= event
->hw
.addr_filters
;
1392 struct perf_addr_filter
*filter
;
1398 list_for_each_entry(filter
, &head
->list
, entry
) {
1399 if (filter
->path
.dentry
&& !fr
[range
].start
) {
1402 /* apply the offset */
1403 msr_a
= fr
[range
].start
;
1404 msr_b
= msr_a
+ fr
[range
].size
- 1;
1407 filters
->filter
[range
].msr_a
= msr_a
;
1408 filters
->filter
[range
].msr_b
= msr_b
;
1409 if (filter
->action
== PERF_ADDR_FILTER_ACTION_FILTER
)
1410 filters
->filter
[range
].config
= 1;
1412 filters
->filter
[range
].config
= 2;
1416 filters
->nr_filters
= range
;
1420 * intel_pt_interrupt() - PT PMI handler
1422 void intel_pt_interrupt(void)
1424 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1425 struct pt_buffer
*buf
;
1426 struct perf_event
*event
= pt
->handle
.event
;
1429 * There may be a dangling PT bit in the interrupt status register
1430 * after PT has been disabled by pt_event_stop(). Make sure we don't
1431 * do anything (particularly, re-enable) for this event here.
1433 if (!READ_ONCE(pt
->handle_nmi
))
1439 pt_config_stop(event
);
1441 buf
= perf_get_aux(&pt
->handle
);
1445 pt_read_offset(buf
);
1447 pt_handle_status(pt
);
1451 perf_aux_output_end(&pt
->handle
, local_xchg(&buf
->data_size
, 0));
1453 if (!event
->hw
.state
) {
1456 buf
= perf_aux_output_begin(&pt
->handle
, event
);
1458 event
->hw
.state
= PERF_HES_STOPPED
;
1462 pt_buffer_reset_offsets(buf
, pt
->handle
.head
);
1463 /* snapshot counters don't use PMI, so it's safe */
1464 ret
= pt_buffer_reset_markers(buf
, &pt
->handle
);
1466 perf_aux_output_end(&pt
->handle
, 0);
1470 pt_config_buffer(buf
);
1471 pt_config_start(event
);
1475 void intel_pt_handle_vmx(int on
)
1477 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1478 struct perf_event
*event
;
1479 unsigned long flags
;
1481 /* PT plays nice with VMX, do nothing */
1486 * VMXON will clear RTIT_CTL.TraceEn; we need to make
1487 * sure to not try to set it while VMX is on. Disable
1488 * interrupts to avoid racing with pmu callbacks;
1489 * concurrent PMI should be handled fine.
1491 local_irq_save(flags
);
1492 WRITE_ONCE(pt
->vmx_on
, on
);
1495 * If an AUX transaction is in progress, it will contain
1496 * gap(s), so flag it PARTIAL to inform the user.
1498 event
= pt
->handle
.event
;
1500 perf_aux_output_flag(&pt
->handle
,
1501 PERF_AUX_FLAG_PARTIAL
);
1503 /* Turn PTs back on */
1505 wrmsrl(MSR_IA32_RTIT_CTL
, event
->hw
.config
);
1507 local_irq_restore(flags
);
1509 EXPORT_SYMBOL_GPL(intel_pt_handle_vmx
);
1515 static void pt_event_start(struct perf_event
*event
, int mode
)
1517 struct hw_perf_event
*hwc
= &event
->hw
;
1518 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1519 struct pt_buffer
*buf
;
1521 buf
= perf_aux_output_begin(&pt
->handle
, event
);
1525 pt_buffer_reset_offsets(buf
, pt
->handle
.head
);
1526 if (!buf
->snapshot
) {
1527 if (pt_buffer_reset_markers(buf
, &pt
->handle
))
1531 WRITE_ONCE(pt
->handle_nmi
, 1);
1534 pt_config_buffer(buf
);
1540 perf_aux_output_end(&pt
->handle
, 0);
1542 hwc
->state
= PERF_HES_STOPPED
;
1545 static void pt_event_stop(struct perf_event
*event
, int mode
)
1547 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1550 * Protect against the PMI racing with disabling wrmsr,
1551 * see comment in intel_pt_interrupt().
1553 WRITE_ONCE(pt
->handle_nmi
, 0);
1555 pt_config_stop(event
);
1557 if (event
->hw
.state
== PERF_HES_STOPPED
)
1560 event
->hw
.state
= PERF_HES_STOPPED
;
1562 if (mode
& PERF_EF_UPDATE
) {
1563 struct pt_buffer
*buf
= perf_get_aux(&pt
->handle
);
1568 if (WARN_ON_ONCE(pt
->handle
.event
!= event
))
1571 pt_read_offset(buf
);
1573 pt_handle_status(pt
);
1579 local_xchg(&buf
->data_size
,
1580 buf
->nr_pages
<< PAGE_SHIFT
);
1581 perf_aux_output_end(&pt
->handle
, local_xchg(&buf
->data_size
, 0));
1585 static long pt_event_snapshot_aux(struct perf_event
*event
,
1586 struct perf_output_handle
*handle
,
1589 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1590 struct pt_buffer
*buf
= perf_get_aux(&pt
->handle
);
1591 unsigned long from
= 0, to
;
1594 if (WARN_ON_ONCE(!buf
))
1598 * Sampling is only allowed on snapshot events;
1599 * see pt_buffer_setup_aux().
1601 if (WARN_ON_ONCE(!buf
->snapshot
))
1605 * Here, handle_nmi tells us if the tracing is on
1607 if (READ_ONCE(pt
->handle_nmi
))
1608 pt_config_stop(event
);
1610 pt_read_offset(buf
);
1613 to
= local_read(&buf
->data_size
);
1615 from
= buf
->nr_pages
<< PAGE_SHIFT
;
1618 ret
= perf_output_copy_aux(&pt
->handle
, handle
, from
, to
);
1621 * If the tracing was on when we turned up, restart it.
1622 * Compiler barrier not needed as we couldn't have been
1623 * preempted by anything that touches pt->handle_nmi.
1626 pt_config_start(event
);
1631 static void pt_event_del(struct perf_event
*event
, int mode
)
1633 pt_event_stop(event
, PERF_EF_UPDATE
);
1636 static int pt_event_add(struct perf_event
*event
, int mode
)
1638 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1639 struct hw_perf_event
*hwc
= &event
->hw
;
1642 if (pt
->handle
.event
)
1645 if (mode
& PERF_EF_START
) {
1646 pt_event_start(event
, 0);
1648 if (hwc
->state
== PERF_HES_STOPPED
)
1651 hwc
->state
= PERF_HES_STOPPED
;
1660 static void pt_event_read(struct perf_event
*event
)
1664 static void pt_event_destroy(struct perf_event
*event
)
1666 pt_addr_filters_fini(event
);
1667 x86_del_exclusive(x86_lbr_exclusive_pt
);
1670 static int pt_event_init(struct perf_event
*event
)
1672 if (event
->attr
.type
!= pt_pmu
.pmu
.type
)
1675 if (!pt_event_valid(event
))
1678 if (x86_add_exclusive(x86_lbr_exclusive_pt
))
1681 if (pt_addr_filters_init(event
)) {
1682 x86_del_exclusive(x86_lbr_exclusive_pt
);
1686 event
->destroy
= pt_event_destroy
;
1691 void cpu_emergency_stop_pt(void)
1693 struct pt
*pt
= this_cpu_ptr(&pt_ctx
);
1695 if (pt
->handle
.event
)
1696 pt_event_stop(pt
->handle
.event
, PERF_EF_UPDATE
);
1699 int is_intel_pt_event(struct perf_event
*event
)
1701 return event
->pmu
== &pt_pmu
.pmu
;
1704 static __init
int pt_init(void)
1706 int ret
, cpu
, prior_warn
= 0;
1708 BUILD_BUG_ON(sizeof(struct topa
) > PAGE_SIZE
);
1710 if (!boot_cpu_has(X86_FEATURE_INTEL_PT
))
1714 for_each_online_cpu(cpu
) {
1717 ret
= rdmsrl_safe_on_cpu(cpu
, MSR_IA32_RTIT_CTL
, &ctl
);
1718 if (!ret
&& (ctl
& RTIT_CTL_TRACEEN
))
1724 x86_add_exclusive(x86_lbr_exclusive_pt
);
1725 pr_warn("PT is enabled at boot time, doing nothing\n");
1730 ret
= pt_pmu_hw_init();
1734 if (!intel_pt_validate_hw_cap(PT_CAP_topa_output
)) {
1735 pr_warn("ToPA output is not supported on this CPU\n");
1739 if (!intel_pt_validate_hw_cap(PT_CAP_topa_multiple_entries
))
1740 pt_pmu
.pmu
.capabilities
= PERF_PMU_CAP_AUX_NO_SG
;
1742 pt_pmu
.pmu
.capabilities
|= PERF_PMU_CAP_EXCLUSIVE
| PERF_PMU_CAP_ITRACE
;
1743 pt_pmu
.pmu
.attr_groups
= pt_attr_groups
;
1744 pt_pmu
.pmu
.task_ctx_nr
= perf_sw_context
;
1745 pt_pmu
.pmu
.event_init
= pt_event_init
;
1746 pt_pmu
.pmu
.add
= pt_event_add
;
1747 pt_pmu
.pmu
.del
= pt_event_del
;
1748 pt_pmu
.pmu
.start
= pt_event_start
;
1749 pt_pmu
.pmu
.stop
= pt_event_stop
;
1750 pt_pmu
.pmu
.snapshot_aux
= pt_event_snapshot_aux
;
1751 pt_pmu
.pmu
.read
= pt_event_read
;
1752 pt_pmu
.pmu
.setup_aux
= pt_buffer_setup_aux
;
1753 pt_pmu
.pmu
.free_aux
= pt_buffer_free_aux
;
1754 pt_pmu
.pmu
.addr_filters_sync
= pt_event_addr_filters_sync
;
1755 pt_pmu
.pmu
.addr_filters_validate
= pt_event_addr_filters_validate
;
1756 pt_pmu
.pmu
.nr_addr_filters
=
1757 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges
);
1759 ret
= perf_pmu_register(&pt_pmu
.pmu
, "intel_pt", -1);
1763 arch_initcall(pt_init
);