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