1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * intel_pt.c: Intel Processor Trace support 4 * Copyright (c) 2013-2015, Intel Corporation. 5 */ 6 7 #include <inttypes.h> 8 #include <linux/perf_event.h> 9 #include <stdio.h> 10 #include <stdbool.h> 11 #include <errno.h> 12 #include <linux/kernel.h> 13 #include <linux/string.h> 14 #include <linux/types.h> 15 #include <linux/zalloc.h> 16 17 #include "session.h" 18 #include "machine.h" 19 #include "memswap.h" 20 #include "sort.h" 21 #include "tool.h" 22 #include "event.h" 23 #include "evlist.h" 24 #include "evsel.h" 25 #include "map.h" 26 #include "color.h" 27 #include "thread.h" 28 #include "thread-stack.h" 29 #include "symbol.h" 30 #include "callchain.h" 31 #include "dso.h" 32 #include "debug.h" 33 #include "auxtrace.h" 34 #include "tsc.h" 35 #include "intel-pt.h" 36 #include "config.h" 37 #include "util/perf_api_probe.h" 38 #include "util/synthetic-events.h" 39 #include "time-utils.h" 40 41 #include "../arch/x86/include/uapi/asm/perf_regs.h" 42 43 #include "intel-pt-decoder/intel-pt-log.h" 44 #include "intel-pt-decoder/intel-pt-decoder.h" 45 #include "intel-pt-decoder/intel-pt-insn-decoder.h" 46 #include "intel-pt-decoder/intel-pt-pkt-decoder.h" 47 48 #define MAX_TIMESTAMP (~0ULL) 49 50 #define INTEL_PT_CFG_PASS_THRU BIT_ULL(0) 51 #define INTEL_PT_CFG_PWR_EVT_EN BIT_ULL(4) 52 #define INTEL_PT_CFG_BRANCH_EN BIT_ULL(13) 53 #define INTEL_PT_CFG_EVT_EN BIT_ULL(31) 54 #define INTEL_PT_CFG_TNT_DIS BIT_ULL(55) 55 56 struct range { 57 u64 start; 58 u64 end; 59 }; 60 61 struct intel_pt { 62 struct auxtrace auxtrace; 63 struct auxtrace_queues queues; 64 struct auxtrace_heap heap; 65 u32 auxtrace_type; 66 struct perf_session *session; 67 struct machine *machine; 68 struct evsel *switch_evsel; 69 struct thread *unknown_thread; 70 bool timeless_decoding; 71 bool sampling_mode; 72 bool snapshot_mode; 73 bool per_cpu_mmaps; 74 bool have_tsc; 75 bool data_queued; 76 bool est_tsc; 77 bool sync_switch; 78 bool sync_switch_not_supported; 79 bool mispred_all; 80 bool use_thread_stack; 81 bool callstack; 82 bool cap_event_trace; 83 bool have_guest_sideband; 84 unsigned int br_stack_sz; 85 unsigned int br_stack_sz_plus; 86 int have_sched_switch; 87 u32 pmu_type; 88 u64 kernel_start; 89 u64 switch_ip; 90 u64 ptss_ip; 91 u64 first_timestamp; 92 93 struct perf_tsc_conversion tc; 94 bool cap_user_time_zero; 95 96 struct itrace_synth_opts synth_opts; 97 98 bool sample_instructions; 99 u64 instructions_sample_type; 100 u64 instructions_id; 101 102 bool sample_cycles; 103 u64 cycles_sample_type; 104 u64 cycles_id; 105 106 bool sample_branches; 107 u32 branches_filter; 108 u64 branches_sample_type; 109 u64 branches_id; 110 111 bool sample_transactions; 112 u64 transactions_sample_type; 113 u64 transactions_id; 114 115 bool sample_ptwrites; 116 u64 ptwrites_sample_type; 117 u64 ptwrites_id; 118 119 bool sample_pwr_events; 120 u64 pwr_events_sample_type; 121 u64 mwait_id; 122 u64 pwre_id; 123 u64 exstop_id; 124 u64 pwrx_id; 125 u64 cbr_id; 126 u64 psb_id; 127 128 bool single_pebs; 129 bool sample_pebs; 130 struct evsel *pebs_evsel; 131 132 u64 evt_sample_type; 133 u64 evt_id; 134 135 u64 iflag_chg_sample_type; 136 u64 iflag_chg_id; 137 138 u64 tsc_bit; 139 u64 mtc_bit; 140 u64 mtc_freq_bits; 141 u32 tsc_ctc_ratio_n; 142 u32 tsc_ctc_ratio_d; 143 u64 cyc_bit; 144 u64 noretcomp_bit; 145 unsigned max_non_turbo_ratio; 146 unsigned cbr2khz; 147 int max_loops; 148 149 unsigned long num_events; 150 151 char *filter; 152 struct addr_filters filts; 153 154 struct range *time_ranges; 155 unsigned int range_cnt; 156 157 struct ip_callchain *chain; 158 struct branch_stack *br_stack; 159 160 u64 dflt_tsc_offset; 161 struct rb_root vmcs_info; 162 }; 163 164 enum switch_state { 165 INTEL_PT_SS_NOT_TRACING, 166 INTEL_PT_SS_UNKNOWN, 167 INTEL_PT_SS_TRACING, 168 INTEL_PT_SS_EXPECTING_SWITCH_EVENT, 169 INTEL_PT_SS_EXPECTING_SWITCH_IP, 170 }; 171 172 /* applicable_counters is 64-bits */ 173 #define INTEL_PT_MAX_PEBS 64 174 175 struct intel_pt_pebs_event { 176 struct evsel *evsel; 177 u64 id; 178 }; 179 180 struct intel_pt_queue { 181 struct intel_pt *pt; 182 unsigned int queue_nr; 183 struct auxtrace_buffer *buffer; 184 struct auxtrace_buffer *old_buffer; 185 void *decoder; 186 const struct intel_pt_state *state; 187 struct ip_callchain *chain; 188 struct branch_stack *last_branch; 189 union perf_event *event_buf; 190 bool on_heap; 191 bool stop; 192 bool step_through_buffers; 193 bool use_buffer_pid_tid; 194 bool sync_switch; 195 bool sample_ipc; 196 pid_t pid, tid; 197 int cpu; 198 int switch_state; 199 pid_t next_tid; 200 struct thread *thread; 201 struct machine *guest_machine; 202 struct thread *guest_thread; 203 struct thread *unknown_guest_thread; 204 pid_t guest_machine_pid; 205 pid_t guest_pid; 206 pid_t guest_tid; 207 int vcpu; 208 bool exclude_kernel; 209 bool have_sample; 210 u64 time; 211 u64 timestamp; 212 u64 sel_timestamp; 213 bool sel_start; 214 unsigned int sel_idx; 215 u32 flags; 216 u16 insn_len; 217 u64 last_insn_cnt; 218 u64 ipc_insn_cnt; 219 u64 ipc_cyc_cnt; 220 u64 last_in_insn_cnt; 221 u64 last_in_cyc_cnt; 222 u64 last_cy_insn_cnt; 223 u64 last_cy_cyc_cnt; 224 u64 last_br_insn_cnt; 225 u64 last_br_cyc_cnt; 226 unsigned int cbr_seen; 227 char insn[INTEL_PT_INSN_BUF_SZ]; 228 struct intel_pt_pebs_event pebs[INTEL_PT_MAX_PEBS]; 229 }; 230 231 static void intel_pt_dump(struct intel_pt *pt __maybe_unused, 232 unsigned char *buf, size_t len) 233 { 234 struct intel_pt_pkt packet; 235 size_t pos = 0; 236 int ret, pkt_len, i; 237 char desc[INTEL_PT_PKT_DESC_MAX]; 238 const char *color = PERF_COLOR_BLUE; 239 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX; 240 241 color_fprintf(stdout, color, 242 ". ... Intel Processor Trace data: size %zu bytes\n", 243 len); 244 245 while (len) { 246 ret = intel_pt_get_packet(buf, len, &packet, &ctx); 247 if (ret > 0) 248 pkt_len = ret; 249 else 250 pkt_len = 1; 251 printf("."); 252 color_fprintf(stdout, color, " %08zx: ", pos); 253 for (i = 0; i < pkt_len; i++) 254 color_fprintf(stdout, color, " %02x", buf[i]); 255 for (; i < 16; i++) 256 color_fprintf(stdout, color, " "); 257 if (ret > 0) { 258 ret = intel_pt_pkt_desc(&packet, desc, 259 INTEL_PT_PKT_DESC_MAX); 260 if (ret > 0) 261 color_fprintf(stdout, color, " %s\n", desc); 262 } else { 263 color_fprintf(stdout, color, " Bad packet!\n"); 264 } 265 pos += pkt_len; 266 buf += pkt_len; 267 len -= pkt_len; 268 } 269 } 270 271 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf, 272 size_t len) 273 { 274 printf(".\n"); 275 intel_pt_dump(pt, buf, len); 276 } 277 278 static void intel_pt_log_event(union perf_event *event) 279 { 280 FILE *f = intel_pt_log_fp(); 281 282 if (!intel_pt_enable_logging || !f) 283 return; 284 285 perf_event__fprintf(event, NULL, f); 286 } 287 288 static void intel_pt_dump_sample(struct perf_session *session, 289 struct perf_sample *sample) 290 { 291 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 292 auxtrace); 293 294 printf("\n"); 295 intel_pt_dump(pt, sample->aux_sample.data, sample->aux_sample.size); 296 } 297 298 static bool intel_pt_log_events(struct intel_pt *pt, u64 tm) 299 { 300 struct perf_time_interval *range = pt->synth_opts.ptime_range; 301 int n = pt->synth_opts.range_num; 302 303 if (pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS) 304 return true; 305 306 if (pt->synth_opts.log_minus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS) 307 return false; 308 309 /* perf_time__ranges_skip_sample does not work if time is zero */ 310 if (!tm) 311 tm = 1; 312 313 return !n || !perf_time__ranges_skip_sample(range, n, tm); 314 } 315 316 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs(struct rb_root *rb_root, 317 u64 vmcs, 318 u64 dflt_tsc_offset) 319 { 320 struct rb_node **p = &rb_root->rb_node; 321 struct rb_node *parent = NULL; 322 struct intel_pt_vmcs_info *v; 323 324 while (*p) { 325 parent = *p; 326 v = rb_entry(parent, struct intel_pt_vmcs_info, rb_node); 327 328 if (v->vmcs == vmcs) 329 return v; 330 331 if (vmcs < v->vmcs) 332 p = &(*p)->rb_left; 333 else 334 p = &(*p)->rb_right; 335 } 336 337 v = zalloc(sizeof(*v)); 338 if (v) { 339 v->vmcs = vmcs; 340 v->tsc_offset = dflt_tsc_offset; 341 v->reliable = dflt_tsc_offset; 342 343 rb_link_node(&v->rb_node, parent, p); 344 rb_insert_color(&v->rb_node, rb_root); 345 } 346 347 return v; 348 } 349 350 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs_info(void *data, uint64_t vmcs) 351 { 352 struct intel_pt_queue *ptq = data; 353 struct intel_pt *pt = ptq->pt; 354 355 if (!vmcs && !pt->dflt_tsc_offset) 356 return NULL; 357 358 return intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, pt->dflt_tsc_offset); 359 } 360 361 static void intel_pt_free_vmcs_info(struct intel_pt *pt) 362 { 363 struct intel_pt_vmcs_info *v; 364 struct rb_node *n; 365 366 n = rb_first(&pt->vmcs_info); 367 while (n) { 368 v = rb_entry(n, struct intel_pt_vmcs_info, rb_node); 369 n = rb_next(n); 370 rb_erase(&v->rb_node, &pt->vmcs_info); 371 free(v); 372 } 373 } 374 375 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a, 376 struct auxtrace_buffer *b) 377 { 378 bool consecutive = false; 379 void *start; 380 381 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size, 382 pt->have_tsc, &consecutive, 383 pt->synth_opts.vm_time_correlation); 384 if (!start) 385 return -EINVAL; 386 /* 387 * In the case of vm_time_correlation, the overlap might contain TSC 388 * packets that will not be fixed, and that will then no longer work for 389 * overlap detection. Avoid that by zeroing out the overlap. 390 */ 391 if (pt->synth_opts.vm_time_correlation) 392 memset(b->data, 0, start - b->data); 393 b->use_size = b->data + b->size - start; 394 b->use_data = start; 395 if (b->use_size && consecutive) 396 b->consecutive = true; 397 return 0; 398 } 399 400 static int intel_pt_get_buffer(struct intel_pt_queue *ptq, 401 struct auxtrace_buffer *buffer, 402 struct auxtrace_buffer *old_buffer, 403 struct intel_pt_buffer *b) 404 { 405 bool might_overlap; 406 407 if (!buffer->data) { 408 int fd = perf_data__fd(ptq->pt->session->data); 409 410 buffer->data = auxtrace_buffer__get_data(buffer, fd); 411 if (!buffer->data) 412 return -ENOMEM; 413 } 414 415 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode; 416 if (might_overlap && !buffer->consecutive && old_buffer && 417 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer)) 418 return -ENOMEM; 419 420 if (buffer->use_data) { 421 b->len = buffer->use_size; 422 b->buf = buffer->use_data; 423 } else { 424 b->len = buffer->size; 425 b->buf = buffer->data; 426 } 427 b->ref_timestamp = buffer->reference; 428 429 if (!old_buffer || (might_overlap && !buffer->consecutive)) { 430 b->consecutive = false; 431 b->trace_nr = buffer->buffer_nr + 1; 432 } else { 433 b->consecutive = true; 434 } 435 436 return 0; 437 } 438 439 /* Do not drop buffers with references - refer intel_pt_get_trace() */ 440 static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq, 441 struct auxtrace_buffer *buffer) 442 { 443 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer) 444 return; 445 446 auxtrace_buffer__drop_data(buffer); 447 } 448 449 /* Must be serialized with respect to intel_pt_get_trace() */ 450 static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb, 451 void *cb_data) 452 { 453 struct intel_pt_queue *ptq = data; 454 struct auxtrace_buffer *buffer = ptq->buffer; 455 struct auxtrace_buffer *old_buffer = ptq->old_buffer; 456 struct auxtrace_queue *queue; 457 int err = 0; 458 459 queue = &ptq->pt->queues.queue_array[ptq->queue_nr]; 460 461 while (1) { 462 struct intel_pt_buffer b = { .len = 0 }; 463 464 buffer = auxtrace_buffer__next(queue, buffer); 465 if (!buffer) 466 break; 467 468 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b); 469 if (err) 470 break; 471 472 if (b.len) { 473 intel_pt_lookahead_drop_buffer(ptq, old_buffer); 474 old_buffer = buffer; 475 } else { 476 intel_pt_lookahead_drop_buffer(ptq, buffer); 477 continue; 478 } 479 480 err = cb(&b, cb_data); 481 if (err) 482 break; 483 } 484 485 if (buffer != old_buffer) 486 intel_pt_lookahead_drop_buffer(ptq, buffer); 487 intel_pt_lookahead_drop_buffer(ptq, old_buffer); 488 489 return err; 490 } 491 492 /* 493 * This function assumes data is processed sequentially only. 494 * Must be serialized with respect to intel_pt_lookahead() 495 */ 496 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data) 497 { 498 struct intel_pt_queue *ptq = data; 499 struct auxtrace_buffer *buffer = ptq->buffer; 500 struct auxtrace_buffer *old_buffer = ptq->old_buffer; 501 struct auxtrace_queue *queue; 502 int err; 503 504 if (ptq->stop) { 505 b->len = 0; 506 return 0; 507 } 508 509 queue = &ptq->pt->queues.queue_array[ptq->queue_nr]; 510 511 buffer = auxtrace_buffer__next(queue, buffer); 512 if (!buffer) { 513 if (old_buffer) 514 auxtrace_buffer__drop_data(old_buffer); 515 b->len = 0; 516 return 0; 517 } 518 519 ptq->buffer = buffer; 520 521 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b); 522 if (err) 523 return err; 524 525 if (ptq->step_through_buffers) 526 ptq->stop = true; 527 528 if (b->len) { 529 if (old_buffer) 530 auxtrace_buffer__drop_data(old_buffer); 531 ptq->old_buffer = buffer; 532 } else { 533 auxtrace_buffer__drop_data(buffer); 534 return intel_pt_get_trace(b, data); 535 } 536 537 return 0; 538 } 539 540 struct intel_pt_cache_entry { 541 struct auxtrace_cache_entry entry; 542 u64 insn_cnt; 543 u64 byte_cnt; 544 enum intel_pt_insn_op op; 545 enum intel_pt_insn_branch branch; 546 bool emulated_ptwrite; 547 int length; 548 int32_t rel; 549 char insn[INTEL_PT_INSN_BUF_SZ]; 550 }; 551 552 static int intel_pt_config_div(const char *var, const char *value, void *data) 553 { 554 int *d = data; 555 long val; 556 557 if (!strcmp(var, "intel-pt.cache-divisor")) { 558 val = strtol(value, NULL, 0); 559 if (val > 0 && val <= INT_MAX) 560 *d = val; 561 } 562 563 return 0; 564 } 565 566 static int intel_pt_cache_divisor(void) 567 { 568 static int d; 569 570 if (d) 571 return d; 572 573 perf_config(intel_pt_config_div, &d); 574 575 if (!d) 576 d = 64; 577 578 return d; 579 } 580 581 static unsigned int intel_pt_cache_size(struct dso *dso, 582 struct machine *machine) 583 { 584 off_t size; 585 586 size = dso__data_size(dso, machine); 587 size /= intel_pt_cache_divisor(); 588 if (size < 1000) 589 return 10; 590 if (size > (1 << 21)) 591 return 21; 592 return 32 - __builtin_clz(size); 593 } 594 595 static struct auxtrace_cache *intel_pt_cache(struct dso *dso, 596 struct machine *machine) 597 { 598 struct auxtrace_cache *c; 599 unsigned int bits; 600 601 if (dso__auxtrace_cache(dso)) 602 return dso__auxtrace_cache(dso); 603 604 bits = intel_pt_cache_size(dso, machine); 605 606 /* Ignoring cache creation failure */ 607 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200); 608 609 dso__set_auxtrace_cache(dso, c); 610 611 return c; 612 } 613 614 static int intel_pt_cache_add(struct dso *dso, struct machine *machine, 615 u64 offset, u64 insn_cnt, u64 byte_cnt, 616 struct intel_pt_insn *intel_pt_insn) 617 { 618 struct auxtrace_cache *c = intel_pt_cache(dso, machine); 619 struct intel_pt_cache_entry *e; 620 int err; 621 622 if (!c) 623 return -ENOMEM; 624 625 e = auxtrace_cache__alloc_entry(c); 626 if (!e) 627 return -ENOMEM; 628 629 e->insn_cnt = insn_cnt; 630 e->byte_cnt = byte_cnt; 631 e->op = intel_pt_insn->op; 632 e->branch = intel_pt_insn->branch; 633 e->emulated_ptwrite = intel_pt_insn->emulated_ptwrite; 634 e->length = intel_pt_insn->length; 635 e->rel = intel_pt_insn->rel; 636 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ); 637 638 err = auxtrace_cache__add(c, offset, &e->entry); 639 if (err) 640 auxtrace_cache__free_entry(c, e); 641 642 return err; 643 } 644 645 static struct intel_pt_cache_entry * 646 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset) 647 { 648 struct auxtrace_cache *c = intel_pt_cache(dso, machine); 649 650 if (!c) 651 return NULL; 652 653 return auxtrace_cache__lookup(dso__auxtrace_cache(dso), offset); 654 } 655 656 static void intel_pt_cache_invalidate(struct dso *dso, struct machine *machine, 657 u64 offset) 658 { 659 struct auxtrace_cache *c = intel_pt_cache(dso, machine); 660 661 if (!c) 662 return; 663 664 auxtrace_cache__remove(dso__auxtrace_cache(dso), offset); 665 } 666 667 static inline bool intel_pt_guest_kernel_ip(uint64_t ip) 668 { 669 /* Assumes 64-bit kernel */ 670 return ip & (1ULL << 63); 671 } 672 673 static inline u8 intel_pt_nr_cpumode(struct intel_pt_queue *ptq, uint64_t ip, bool nr) 674 { 675 if (nr) { 676 return intel_pt_guest_kernel_ip(ip) ? 677 PERF_RECORD_MISC_GUEST_KERNEL : 678 PERF_RECORD_MISC_GUEST_USER; 679 } 680 681 return ip >= ptq->pt->kernel_start ? 682 PERF_RECORD_MISC_KERNEL : 683 PERF_RECORD_MISC_USER; 684 } 685 686 static inline u8 intel_pt_cpumode(struct intel_pt_queue *ptq, uint64_t from_ip, uint64_t to_ip) 687 { 688 /* No support for non-zero CS base */ 689 if (from_ip) 690 return intel_pt_nr_cpumode(ptq, from_ip, ptq->state->from_nr); 691 return intel_pt_nr_cpumode(ptq, to_ip, ptq->state->to_nr); 692 } 693 694 static int intel_pt_get_guest(struct intel_pt_queue *ptq) 695 { 696 struct machines *machines = &ptq->pt->session->machines; 697 struct machine *machine; 698 pid_t pid = ptq->pid <= 0 ? DEFAULT_GUEST_KERNEL_ID : ptq->pid; 699 700 if (ptq->guest_machine && pid == ptq->guest_machine->pid) 701 return 0; 702 703 ptq->guest_machine = NULL; 704 thread__zput(ptq->unknown_guest_thread); 705 706 if (symbol_conf.guest_code) { 707 thread__zput(ptq->guest_thread); 708 ptq->guest_thread = machines__findnew_guest_code(machines, pid); 709 } 710 711 machine = machines__find_guest(machines, pid); 712 if (!machine) 713 return -1; 714 715 ptq->unknown_guest_thread = machine__idle_thread(machine); 716 if (!ptq->unknown_guest_thread) 717 return -1; 718 719 ptq->guest_machine = machine; 720 721 return 0; 722 } 723 724 static inline bool intel_pt_jmp_16(struct intel_pt_insn *intel_pt_insn) 725 { 726 return intel_pt_insn->rel == 16 && intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL; 727 } 728 729 #define PTWRITE_MAGIC "\x0f\x0bperf,ptwrite " 730 #define PTWRITE_MAGIC_LEN 16 731 732 static bool intel_pt_emulated_ptwrite(struct dso *dso, struct machine *machine, u64 offset) 733 { 734 unsigned char buf[PTWRITE_MAGIC_LEN]; 735 ssize_t len; 736 737 len = dso__data_read_offset(dso, machine, offset, buf, PTWRITE_MAGIC_LEN); 738 if (len == PTWRITE_MAGIC_LEN && !memcmp(buf, PTWRITE_MAGIC, PTWRITE_MAGIC_LEN)) { 739 intel_pt_log("Emulated ptwrite signature found\n"); 740 return true; 741 } 742 intel_pt_log("Emulated ptwrite signature not found\n"); 743 return false; 744 } 745 746 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn, 747 uint64_t *insn_cnt_ptr, uint64_t *ip, 748 uint64_t to_ip, uint64_t max_insn_cnt, 749 void *data) 750 { 751 struct intel_pt_queue *ptq = data; 752 struct machine *machine = ptq->pt->machine; 753 struct thread *thread; 754 struct addr_location al; 755 unsigned char buf[INTEL_PT_INSN_BUF_SZ]; 756 ssize_t len; 757 int x86_64, ret = 0; 758 u8 cpumode; 759 u64 offset, start_offset, start_ip; 760 u64 insn_cnt = 0; 761 bool one_map = true; 762 bool nr; 763 764 765 addr_location__init(&al); 766 intel_pt_insn->length = 0; 767 intel_pt_insn->op = INTEL_PT_OP_OTHER; 768 769 if (to_ip && *ip == to_ip) 770 goto out_no_cache; 771 772 nr = ptq->state->to_nr; 773 cpumode = intel_pt_nr_cpumode(ptq, *ip, nr); 774 775 if (nr) { 776 if (ptq->pt->have_guest_sideband) { 777 if (!ptq->guest_machine || ptq->guest_machine_pid != ptq->pid) { 778 intel_pt_log("ERROR: guest sideband but no guest machine\n"); 779 ret = -EINVAL; 780 goto out_ret; 781 } 782 } else if ((!symbol_conf.guest_code && cpumode != PERF_RECORD_MISC_GUEST_KERNEL) || 783 intel_pt_get_guest(ptq)) { 784 intel_pt_log("ERROR: no guest machine\n"); 785 ret = -EINVAL; 786 goto out_ret; 787 } 788 machine = ptq->guest_machine; 789 thread = ptq->guest_thread; 790 if (!thread) { 791 if (cpumode != PERF_RECORD_MISC_GUEST_KERNEL) { 792 intel_pt_log("ERROR: no guest thread\n"); 793 ret = -EINVAL; 794 goto out_ret; 795 } 796 thread = ptq->unknown_guest_thread; 797 } 798 } else { 799 thread = ptq->thread; 800 if (!thread) { 801 if (cpumode != PERF_RECORD_MISC_KERNEL) { 802 intel_pt_log("ERROR: no thread\n"); 803 ret = -EINVAL; 804 goto out_ret; 805 } 806 thread = ptq->pt->unknown_thread; 807 } 808 } 809 810 while (1) { 811 struct dso *dso; 812 813 if (!thread__find_map(thread, cpumode, *ip, &al) || !map__dso(al.map)) { 814 if (al.map) 815 intel_pt_log("ERROR: thread has no dso for %#" PRIx64 "\n", *ip); 816 else 817 intel_pt_log("ERROR: thread has no map for %#" PRIx64 "\n", *ip); 818 addr_location__exit(&al); 819 ret = -EINVAL; 820 goto out_ret; 821 } 822 dso = map__dso(al.map); 823 824 if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR && 825 dso__data_status_seen(dso, DSO_DATA_STATUS_SEEN_ITRACE)) { 826 ret = -ENOENT; 827 goto out_ret; 828 } 829 830 offset = map__map_ip(al.map, *ip); 831 832 if (!to_ip && one_map) { 833 struct intel_pt_cache_entry *e; 834 835 e = intel_pt_cache_lookup(dso, machine, offset); 836 if (e && 837 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) { 838 *insn_cnt_ptr = e->insn_cnt; 839 *ip += e->byte_cnt; 840 intel_pt_insn->op = e->op; 841 intel_pt_insn->branch = e->branch; 842 intel_pt_insn->emulated_ptwrite = e->emulated_ptwrite; 843 intel_pt_insn->length = e->length; 844 intel_pt_insn->rel = e->rel; 845 memcpy(intel_pt_insn->buf, e->insn, INTEL_PT_INSN_BUF_SZ); 846 intel_pt_log_insn_no_data(intel_pt_insn, *ip); 847 ret = 0; 848 goto out_ret; 849 } 850 } 851 852 start_offset = offset; 853 start_ip = *ip; 854 855 /* Load maps to ensure dso->is_64_bit has been updated */ 856 map__load(al.map); 857 858 x86_64 = dso__is_64_bit(dso); 859 860 while (1) { 861 len = dso__data_read_offset(dso, machine, 862 offset, buf, 863 INTEL_PT_INSN_BUF_SZ); 864 if (len <= 0) { 865 intel_pt_log("ERROR: failed to read at offset %#" PRIx64 " ", 866 offset); 867 if (intel_pt_enable_logging) 868 dso__fprintf(dso, intel_pt_log_fp()); 869 ret = -EINVAL; 870 goto out_ret; 871 } 872 873 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn)) { 874 ret = -EINVAL; 875 goto out_ret; 876 } 877 878 intel_pt_log_insn(intel_pt_insn, *ip); 879 880 insn_cnt += 1; 881 882 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH) { 883 bool eptw; 884 u64 offs; 885 886 if (!intel_pt_jmp_16(intel_pt_insn)) 887 goto out; 888 /* Check for emulated ptwrite */ 889 offs = offset + intel_pt_insn->length; 890 eptw = intel_pt_emulated_ptwrite(dso, machine, offs); 891 intel_pt_insn->emulated_ptwrite = eptw; 892 goto out; 893 } 894 895 if (max_insn_cnt && insn_cnt >= max_insn_cnt) 896 goto out_no_cache; 897 898 *ip += intel_pt_insn->length; 899 900 if (to_ip && *ip == to_ip) { 901 intel_pt_insn->length = 0; 902 intel_pt_insn->op = INTEL_PT_OP_OTHER; 903 goto out_no_cache; 904 } 905 906 if (*ip >= map__end(al.map)) 907 break; 908 909 offset += intel_pt_insn->length; 910 } 911 one_map = false; 912 } 913 out: 914 *insn_cnt_ptr = insn_cnt; 915 916 if (!one_map) 917 goto out_no_cache; 918 919 /* 920 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate 921 * entries. 922 */ 923 if (to_ip) { 924 struct intel_pt_cache_entry *e; 925 926 e = intel_pt_cache_lookup(map__dso(al.map), machine, start_offset); 927 if (e) 928 goto out_ret; 929 } 930 931 /* Ignore cache errors */ 932 intel_pt_cache_add(map__dso(al.map), machine, start_offset, insn_cnt, 933 *ip - start_ip, intel_pt_insn); 934 935 out_ret: 936 addr_location__exit(&al); 937 return ret; 938 939 out_no_cache: 940 *insn_cnt_ptr = insn_cnt; 941 addr_location__exit(&al); 942 return 0; 943 } 944 945 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip, 946 uint64_t offset, const char *filename) 947 { 948 struct addr_filter *filt; 949 bool have_filter = false; 950 bool hit_tracestop = false; 951 bool hit_filter = false; 952 953 list_for_each_entry(filt, &pt->filts.head, list) { 954 if (filt->start) 955 have_filter = true; 956 957 if ((filename && !filt->filename) || 958 (!filename && filt->filename) || 959 (filename && strcmp(filename, filt->filename))) 960 continue; 961 962 if (!(offset >= filt->addr && offset < filt->addr + filt->size)) 963 continue; 964 965 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n", 966 ip, offset, filename ? filename : "[kernel]", 967 filt->start ? "filter" : "stop", 968 filt->addr, filt->size); 969 970 if (filt->start) 971 hit_filter = true; 972 else 973 hit_tracestop = true; 974 } 975 976 if (!hit_tracestop && !hit_filter) 977 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n", 978 ip, offset, filename ? filename : "[kernel]"); 979 980 return hit_tracestop || (have_filter && !hit_filter); 981 } 982 983 static int __intel_pt_pgd_ip(uint64_t ip, void *data) 984 { 985 struct intel_pt_queue *ptq = data; 986 struct thread *thread; 987 struct addr_location al; 988 u8 cpumode; 989 u64 offset; 990 int res; 991 992 if (ptq->state->to_nr) { 993 if (intel_pt_guest_kernel_ip(ip)) 994 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL); 995 /* No support for decoding guest user space */ 996 return -EINVAL; 997 } else if (ip >= ptq->pt->kernel_start) { 998 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL); 999 } 1000 1001 cpumode = PERF_RECORD_MISC_USER; 1002 1003 thread = ptq->thread; 1004 if (!thread) 1005 return -EINVAL; 1006 1007 addr_location__init(&al); 1008 if (!thread__find_map(thread, cpumode, ip, &al) || !map__dso(al.map)) 1009 return -EINVAL; 1010 1011 offset = map__map_ip(al.map, ip); 1012 1013 res = intel_pt_match_pgd_ip(ptq->pt, ip, offset, dso__long_name(map__dso(al.map))); 1014 addr_location__exit(&al); 1015 return res; 1016 } 1017 1018 static bool intel_pt_pgd_ip(uint64_t ip, void *data) 1019 { 1020 return __intel_pt_pgd_ip(ip, data) > 0; 1021 } 1022 1023 static bool intel_pt_get_config(struct intel_pt *pt, 1024 struct perf_event_attr *attr, u64 *config) 1025 { 1026 if (attr->type == pt->pmu_type) { 1027 if (config) 1028 *config = attr->config; 1029 return true; 1030 } 1031 1032 return false; 1033 } 1034 1035 static bool intel_pt_exclude_kernel(struct intel_pt *pt) 1036 { 1037 struct evsel *evsel; 1038 1039 evlist__for_each_entry(pt->session->evlist, evsel) { 1040 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) && 1041 !evsel->core.attr.exclude_kernel) 1042 return false; 1043 } 1044 return true; 1045 } 1046 1047 static bool intel_pt_return_compression(struct intel_pt *pt) 1048 { 1049 struct evsel *evsel; 1050 u64 config; 1051 1052 if (!pt->noretcomp_bit) 1053 return true; 1054 1055 evlist__for_each_entry(pt->session->evlist, evsel) { 1056 if (intel_pt_get_config(pt, &evsel->core.attr, &config) && 1057 (config & pt->noretcomp_bit)) 1058 return false; 1059 } 1060 return true; 1061 } 1062 1063 static bool intel_pt_branch_enable(struct intel_pt *pt) 1064 { 1065 struct evsel *evsel; 1066 u64 config; 1067 1068 evlist__for_each_entry(pt->session->evlist, evsel) { 1069 if (intel_pt_get_config(pt, &evsel->core.attr, &config) && 1070 (config & INTEL_PT_CFG_PASS_THRU) && 1071 !(config & INTEL_PT_CFG_BRANCH_EN)) 1072 return false; 1073 } 1074 return true; 1075 } 1076 1077 static bool intel_pt_disabled_tnt(struct intel_pt *pt) 1078 { 1079 struct evsel *evsel; 1080 u64 config; 1081 1082 evlist__for_each_entry(pt->session->evlist, evsel) { 1083 if (intel_pt_get_config(pt, &evsel->core.attr, &config) && 1084 config & INTEL_PT_CFG_TNT_DIS) 1085 return true; 1086 } 1087 return false; 1088 } 1089 1090 static unsigned int intel_pt_mtc_period(struct intel_pt *pt) 1091 { 1092 struct evsel *evsel; 1093 unsigned int shift; 1094 u64 config; 1095 1096 if (!pt->mtc_freq_bits) 1097 return 0; 1098 1099 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++) 1100 config >>= 1; 1101 1102 evlist__for_each_entry(pt->session->evlist, evsel) { 1103 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) 1104 return (config & pt->mtc_freq_bits) >> shift; 1105 } 1106 return 0; 1107 } 1108 1109 static bool intel_pt_timeless_decoding(struct intel_pt *pt) 1110 { 1111 struct evsel *evsel; 1112 bool timeless_decoding = true; 1113 u64 config; 1114 1115 if (!pt->tsc_bit || !pt->cap_user_time_zero || pt->synth_opts.timeless_decoding) 1116 return true; 1117 1118 evlist__for_each_entry(pt->session->evlist, evsel) { 1119 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME)) 1120 return true; 1121 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) { 1122 if (config & pt->tsc_bit) 1123 timeless_decoding = false; 1124 else 1125 return true; 1126 } 1127 } 1128 return timeless_decoding; 1129 } 1130 1131 static bool intel_pt_tracing_kernel(struct intel_pt *pt) 1132 { 1133 struct evsel *evsel; 1134 1135 evlist__for_each_entry(pt->session->evlist, evsel) { 1136 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) && 1137 !evsel->core.attr.exclude_kernel) 1138 return true; 1139 } 1140 return false; 1141 } 1142 1143 static bool intel_pt_have_tsc(struct intel_pt *pt) 1144 { 1145 struct evsel *evsel; 1146 bool have_tsc = false; 1147 u64 config; 1148 1149 if (!pt->tsc_bit) 1150 return false; 1151 1152 evlist__for_each_entry(pt->session->evlist, evsel) { 1153 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) { 1154 if (config & pt->tsc_bit) 1155 have_tsc = true; 1156 else 1157 return false; 1158 } 1159 } 1160 return have_tsc; 1161 } 1162 1163 static bool intel_pt_have_mtc(struct intel_pt *pt) 1164 { 1165 struct evsel *evsel; 1166 u64 config; 1167 1168 evlist__for_each_entry(pt->session->evlist, evsel) { 1169 if (intel_pt_get_config(pt, &evsel->core.attr, &config) && 1170 (config & pt->mtc_bit)) 1171 return true; 1172 } 1173 return false; 1174 } 1175 1176 static bool intel_pt_sampling_mode(struct intel_pt *pt) 1177 { 1178 struct evsel *evsel; 1179 1180 evlist__for_each_entry(pt->session->evlist, evsel) { 1181 if ((evsel->core.attr.sample_type & PERF_SAMPLE_AUX) && 1182 evsel->core.attr.aux_sample_size) 1183 return true; 1184 } 1185 return false; 1186 } 1187 1188 static u64 intel_pt_ctl(struct intel_pt *pt) 1189 { 1190 struct evsel *evsel; 1191 u64 config; 1192 1193 evlist__for_each_entry(pt->session->evlist, evsel) { 1194 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) 1195 return config; 1196 } 1197 return 0; 1198 } 1199 1200 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns) 1201 { 1202 u64 quot, rem; 1203 1204 quot = ns / pt->tc.time_mult; 1205 rem = ns % pt->tc.time_mult; 1206 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) / 1207 pt->tc.time_mult; 1208 } 1209 1210 static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt) 1211 { 1212 size_t sz = sizeof(struct ip_callchain); 1213 1214 /* Add 1 to callchain_sz for callchain context */ 1215 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64); 1216 return zalloc(sz); 1217 } 1218 1219 static int intel_pt_callchain_init(struct intel_pt *pt) 1220 { 1221 struct evsel *evsel; 1222 1223 evlist__for_each_entry(pt->session->evlist, evsel) { 1224 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN)) 1225 evsel->synth_sample_type |= PERF_SAMPLE_CALLCHAIN; 1226 } 1227 1228 pt->chain = intel_pt_alloc_chain(pt); 1229 if (!pt->chain) 1230 return -ENOMEM; 1231 1232 return 0; 1233 } 1234 1235 static void intel_pt_add_callchain(struct intel_pt *pt, 1236 struct perf_sample *sample) 1237 { 1238 struct thread *thread = machine__findnew_thread(pt->machine, 1239 sample->pid, 1240 sample->tid); 1241 1242 thread_stack__sample_late(thread, sample->cpu, pt->chain, 1243 pt->synth_opts.callchain_sz + 1, sample->ip, 1244 pt->kernel_start); 1245 1246 sample->callchain = pt->chain; 1247 } 1248 1249 static struct branch_stack *intel_pt_alloc_br_stack(unsigned int entry_cnt) 1250 { 1251 size_t sz = sizeof(struct branch_stack); 1252 1253 sz += entry_cnt * sizeof(struct branch_entry); 1254 return zalloc(sz); 1255 } 1256 1257 static int intel_pt_br_stack_init(struct intel_pt *pt) 1258 { 1259 struct evsel *evsel; 1260 1261 evlist__for_each_entry(pt->session->evlist, evsel) { 1262 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK)) 1263 evsel->synth_sample_type |= PERF_SAMPLE_BRANCH_STACK; 1264 } 1265 1266 pt->br_stack = intel_pt_alloc_br_stack(pt->br_stack_sz); 1267 if (!pt->br_stack) 1268 return -ENOMEM; 1269 1270 return 0; 1271 } 1272 1273 static void intel_pt_add_br_stack(struct intel_pt *pt, 1274 struct perf_sample *sample) 1275 { 1276 struct thread *thread = machine__findnew_thread(pt->machine, 1277 sample->pid, 1278 sample->tid); 1279 1280 thread_stack__br_sample_late(thread, sample->cpu, pt->br_stack, 1281 pt->br_stack_sz, sample->ip, 1282 pt->kernel_start); 1283 1284 sample->branch_stack = pt->br_stack; 1285 thread__put(thread); 1286 } 1287 1288 /* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ 1289 #define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U) 1290 1291 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt, 1292 unsigned int queue_nr) 1293 { 1294 struct intel_pt_params params = { .get_trace = 0, }; 1295 struct perf_env *env = pt->machine->env; 1296 struct intel_pt_queue *ptq; 1297 1298 ptq = zalloc(sizeof(struct intel_pt_queue)); 1299 if (!ptq) 1300 return NULL; 1301 1302 if (pt->synth_opts.callchain) { 1303 ptq->chain = intel_pt_alloc_chain(pt); 1304 if (!ptq->chain) 1305 goto out_free; 1306 } 1307 1308 if (pt->synth_opts.last_branch || pt->synth_opts.other_events) { 1309 unsigned int entry_cnt = max(LBRS_MAX, pt->br_stack_sz); 1310 1311 ptq->last_branch = intel_pt_alloc_br_stack(entry_cnt); 1312 if (!ptq->last_branch) 1313 goto out_free; 1314 } 1315 1316 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE); 1317 if (!ptq->event_buf) 1318 goto out_free; 1319 1320 ptq->pt = pt; 1321 ptq->queue_nr = queue_nr; 1322 ptq->exclude_kernel = intel_pt_exclude_kernel(pt); 1323 ptq->pid = -1; 1324 ptq->tid = -1; 1325 ptq->cpu = -1; 1326 ptq->next_tid = -1; 1327 1328 params.get_trace = intel_pt_get_trace; 1329 params.walk_insn = intel_pt_walk_next_insn; 1330 params.lookahead = intel_pt_lookahead; 1331 params.findnew_vmcs_info = intel_pt_findnew_vmcs_info; 1332 params.data = ptq; 1333 params.return_compression = intel_pt_return_compression(pt); 1334 params.branch_enable = intel_pt_branch_enable(pt); 1335 params.ctl = intel_pt_ctl(pt); 1336 params.max_non_turbo_ratio = pt->max_non_turbo_ratio; 1337 params.mtc_period = intel_pt_mtc_period(pt); 1338 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n; 1339 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d; 1340 params.quick = pt->synth_opts.quick; 1341 params.vm_time_correlation = pt->synth_opts.vm_time_correlation; 1342 params.vm_tm_corr_dry_run = pt->synth_opts.vm_tm_corr_dry_run; 1343 params.first_timestamp = pt->first_timestamp; 1344 params.max_loops = pt->max_loops; 1345 1346 /* Cannot walk code without TNT, so force 'quick' mode */ 1347 if (params.branch_enable && intel_pt_disabled_tnt(pt) && !params.quick) 1348 params.quick = 1; 1349 1350 if (pt->filts.cnt > 0) 1351 params.pgd_ip = intel_pt_pgd_ip; 1352 1353 if (pt->synth_opts.instructions || pt->synth_opts.cycles) { 1354 if (pt->synth_opts.period) { 1355 switch (pt->synth_opts.period_type) { 1356 case PERF_ITRACE_PERIOD_INSTRUCTIONS: 1357 params.period_type = 1358 INTEL_PT_PERIOD_INSTRUCTIONS; 1359 params.period = pt->synth_opts.period; 1360 break; 1361 case PERF_ITRACE_PERIOD_TICKS: 1362 params.period_type = INTEL_PT_PERIOD_TICKS; 1363 params.period = pt->synth_opts.period; 1364 break; 1365 case PERF_ITRACE_PERIOD_NANOSECS: 1366 params.period_type = INTEL_PT_PERIOD_TICKS; 1367 params.period = intel_pt_ns_to_ticks(pt, 1368 pt->synth_opts.period); 1369 break; 1370 default: 1371 break; 1372 } 1373 } 1374 1375 if (!params.period) { 1376 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS; 1377 params.period = 1; 1378 } 1379 } 1380 1381 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18)) 1382 params.flags |= INTEL_PT_FUP_WITH_NLIP; 1383 1384 ptq->decoder = intel_pt_decoder_new(¶ms); 1385 if (!ptq->decoder) 1386 goto out_free; 1387 1388 return ptq; 1389 1390 out_free: 1391 zfree(&ptq->event_buf); 1392 zfree(&ptq->last_branch); 1393 zfree(&ptq->chain); 1394 free(ptq); 1395 return NULL; 1396 } 1397 1398 static void intel_pt_free_queue(void *priv) 1399 { 1400 struct intel_pt_queue *ptq = priv; 1401 1402 if (!ptq) 1403 return; 1404 thread__zput(ptq->thread); 1405 thread__zput(ptq->guest_thread); 1406 thread__zput(ptq->unknown_guest_thread); 1407 intel_pt_decoder_free(ptq->decoder); 1408 zfree(&ptq->event_buf); 1409 zfree(&ptq->last_branch); 1410 zfree(&ptq->chain); 1411 free(ptq); 1412 } 1413 1414 static void intel_pt_first_timestamp(struct intel_pt *pt, u64 timestamp) 1415 { 1416 unsigned int i; 1417 1418 pt->first_timestamp = timestamp; 1419 1420 for (i = 0; i < pt->queues.nr_queues; i++) { 1421 struct auxtrace_queue *queue = &pt->queues.queue_array[i]; 1422 struct intel_pt_queue *ptq = queue->priv; 1423 1424 if (ptq && ptq->decoder) 1425 intel_pt_set_first_timestamp(ptq->decoder, timestamp); 1426 } 1427 } 1428 1429 static int intel_pt_get_guest_from_sideband(struct intel_pt_queue *ptq) 1430 { 1431 struct machines *machines = &ptq->pt->session->machines; 1432 struct machine *machine; 1433 pid_t machine_pid = ptq->pid; 1434 pid_t tid; 1435 int vcpu; 1436 1437 if (machine_pid <= 0) 1438 return 0; /* Not a guest machine */ 1439 1440 machine = machines__find(machines, machine_pid); 1441 if (!machine) 1442 return 0; /* Not a guest machine */ 1443 1444 if (ptq->guest_machine != machine) { 1445 ptq->guest_machine = NULL; 1446 thread__zput(ptq->guest_thread); 1447 thread__zput(ptq->unknown_guest_thread); 1448 1449 ptq->unknown_guest_thread = machine__find_thread(machine, 0, 0); 1450 if (!ptq->unknown_guest_thread) 1451 return -1; 1452 ptq->guest_machine = machine; 1453 } 1454 1455 vcpu = ptq->thread ? thread__guest_cpu(ptq->thread) : -1; 1456 if (vcpu < 0) 1457 return -1; 1458 1459 tid = machine__get_current_tid(machine, vcpu); 1460 1461 if (ptq->guest_thread && thread__tid(ptq->guest_thread) != tid) 1462 thread__zput(ptq->guest_thread); 1463 1464 if (!ptq->guest_thread) { 1465 ptq->guest_thread = machine__find_thread(machine, -1, tid); 1466 if (!ptq->guest_thread) 1467 return -1; 1468 } 1469 1470 ptq->guest_machine_pid = machine_pid; 1471 ptq->guest_pid = thread__pid(ptq->guest_thread); 1472 ptq->guest_tid = tid; 1473 ptq->vcpu = vcpu; 1474 1475 return 0; 1476 } 1477 1478 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt, 1479 struct auxtrace_queue *queue) 1480 { 1481 struct intel_pt_queue *ptq = queue->priv; 1482 1483 if (queue->tid == -1 || pt->have_sched_switch) { 1484 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu); 1485 if (ptq->tid == -1) 1486 ptq->pid = -1; 1487 thread__zput(ptq->thread); 1488 } 1489 1490 if (!ptq->thread && ptq->tid != -1) 1491 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid); 1492 1493 if (ptq->thread) { 1494 ptq->pid = thread__pid(ptq->thread); 1495 if (queue->cpu == -1) 1496 ptq->cpu = thread__cpu(ptq->thread); 1497 } 1498 1499 if (pt->have_guest_sideband && intel_pt_get_guest_from_sideband(ptq)) { 1500 ptq->guest_machine_pid = 0; 1501 ptq->guest_pid = -1; 1502 ptq->guest_tid = -1; 1503 ptq->vcpu = -1; 1504 } 1505 } 1506 1507 static void intel_pt_sample_flags(struct intel_pt_queue *ptq) 1508 { 1509 struct intel_pt *pt = ptq->pt; 1510 1511 ptq->insn_len = 0; 1512 if (ptq->state->flags & INTEL_PT_ABORT_TX) { 1513 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT; 1514 } else if (ptq->state->flags & INTEL_PT_ASYNC) { 1515 if (!ptq->state->to_ip) 1516 ptq->flags = PERF_IP_FLAG_BRANCH | 1517 PERF_IP_FLAG_ASYNC | 1518 PERF_IP_FLAG_TRACE_END; 1519 else if (ptq->state->from_nr && !ptq->state->to_nr) 1520 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | 1521 PERF_IP_FLAG_ASYNC | 1522 PERF_IP_FLAG_VMEXIT; 1523 else 1524 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | 1525 PERF_IP_FLAG_ASYNC | 1526 PERF_IP_FLAG_INTERRUPT; 1527 } else { 1528 if (ptq->state->from_ip) 1529 ptq->flags = intel_pt_insn_type(ptq->state->insn_op); 1530 else 1531 ptq->flags = PERF_IP_FLAG_BRANCH | 1532 PERF_IP_FLAG_TRACE_BEGIN; 1533 if (ptq->state->flags & INTEL_PT_IN_TX) 1534 ptq->flags |= PERF_IP_FLAG_IN_TX; 1535 ptq->insn_len = ptq->state->insn_len; 1536 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ); 1537 } 1538 1539 if (ptq->state->type & INTEL_PT_TRACE_BEGIN) 1540 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN; 1541 if (ptq->state->type & INTEL_PT_TRACE_END) 1542 ptq->flags |= PERF_IP_FLAG_TRACE_END; 1543 1544 if (pt->cap_event_trace) { 1545 if (ptq->state->type & INTEL_PT_IFLAG_CHG) { 1546 if (!ptq->state->from_iflag) 1547 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE; 1548 if (ptq->state->from_iflag != ptq->state->to_iflag) 1549 ptq->flags |= PERF_IP_FLAG_INTR_TOGGLE; 1550 } else if (!ptq->state->to_iflag) { 1551 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE; 1552 } 1553 } 1554 } 1555 1556 static void intel_pt_setup_time_range(struct intel_pt *pt, 1557 struct intel_pt_queue *ptq) 1558 { 1559 if (!pt->range_cnt) 1560 return; 1561 1562 ptq->sel_timestamp = pt->time_ranges[0].start; 1563 ptq->sel_idx = 0; 1564 1565 if (ptq->sel_timestamp) { 1566 ptq->sel_start = true; 1567 } else { 1568 ptq->sel_timestamp = pt->time_ranges[0].end; 1569 ptq->sel_start = false; 1570 } 1571 } 1572 1573 static int intel_pt_setup_queue(struct intel_pt *pt, 1574 struct auxtrace_queue *queue, 1575 unsigned int queue_nr) 1576 { 1577 struct intel_pt_queue *ptq = queue->priv; 1578 1579 if (list_empty(&queue->head)) 1580 return 0; 1581 1582 if (!ptq) { 1583 ptq = intel_pt_alloc_queue(pt, queue_nr); 1584 if (!ptq) 1585 return -ENOMEM; 1586 queue->priv = ptq; 1587 1588 if (queue->cpu != -1) 1589 ptq->cpu = queue->cpu; 1590 ptq->tid = queue->tid; 1591 1592 ptq->cbr_seen = UINT_MAX; 1593 1594 if (pt->sampling_mode && !pt->snapshot_mode && 1595 pt->timeless_decoding) 1596 ptq->step_through_buffers = true; 1597 1598 ptq->sync_switch = pt->sync_switch; 1599 1600 intel_pt_setup_time_range(pt, ptq); 1601 } 1602 1603 if (!ptq->on_heap && 1604 (!ptq->sync_switch || 1605 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) { 1606 const struct intel_pt_state *state; 1607 int ret; 1608 1609 if (pt->timeless_decoding) 1610 return 0; 1611 1612 intel_pt_log("queue %u getting timestamp\n", queue_nr); 1613 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n", 1614 queue_nr, ptq->cpu, ptq->pid, ptq->tid); 1615 1616 if (ptq->sel_start && ptq->sel_timestamp) { 1617 ret = intel_pt_fast_forward(ptq->decoder, 1618 ptq->sel_timestamp); 1619 if (ret) 1620 return ret; 1621 } 1622 1623 while (1) { 1624 state = intel_pt_decode(ptq->decoder); 1625 if (state->err) { 1626 if (state->err == INTEL_PT_ERR_NODATA) { 1627 intel_pt_log("queue %u has no timestamp\n", 1628 queue_nr); 1629 return 0; 1630 } 1631 continue; 1632 } 1633 if (state->timestamp) 1634 break; 1635 } 1636 1637 ptq->timestamp = state->timestamp; 1638 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n", 1639 queue_nr, ptq->timestamp); 1640 ptq->state = state; 1641 ptq->have_sample = true; 1642 if (ptq->sel_start && ptq->sel_timestamp && 1643 ptq->timestamp < ptq->sel_timestamp) 1644 ptq->have_sample = false; 1645 intel_pt_sample_flags(ptq); 1646 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp); 1647 if (ret) 1648 return ret; 1649 ptq->on_heap = true; 1650 } 1651 1652 return 0; 1653 } 1654 1655 static int intel_pt_setup_queues(struct intel_pt *pt) 1656 { 1657 unsigned int i; 1658 int ret; 1659 1660 for (i = 0; i < pt->queues.nr_queues; i++) { 1661 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i); 1662 if (ret) 1663 return ret; 1664 } 1665 return 0; 1666 } 1667 1668 static inline bool intel_pt_skip_event(struct intel_pt *pt) 1669 { 1670 return pt->synth_opts.initial_skip && 1671 pt->num_events++ < pt->synth_opts.initial_skip; 1672 } 1673 1674 /* 1675 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen. 1676 * Also ensure CBR is first non-skipped event by allowing for 4 more samples 1677 * from this decoder state. 1678 */ 1679 static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt) 1680 { 1681 return pt->synth_opts.initial_skip && 1682 pt->num_events + 4 < pt->synth_opts.initial_skip; 1683 } 1684 1685 static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq, 1686 union perf_event *event, 1687 struct perf_sample *sample) 1688 { 1689 event->sample.header.type = PERF_RECORD_SAMPLE; 1690 event->sample.header.size = sizeof(struct perf_event_header); 1691 1692 sample->pid = ptq->pid; 1693 sample->tid = ptq->tid; 1694 1695 if (ptq->pt->have_guest_sideband) { 1696 if ((ptq->state->from_ip && ptq->state->from_nr) || 1697 (ptq->state->to_ip && ptq->state->to_nr)) { 1698 sample->pid = ptq->guest_pid; 1699 sample->tid = ptq->guest_tid; 1700 sample->machine_pid = ptq->guest_machine_pid; 1701 sample->vcpu = ptq->vcpu; 1702 } 1703 } 1704 1705 sample->cpu = ptq->cpu; 1706 sample->insn_len = ptq->insn_len; 1707 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ); 1708 } 1709 1710 static void intel_pt_prep_b_sample(struct intel_pt *pt, 1711 struct intel_pt_queue *ptq, 1712 union perf_event *event, 1713 struct perf_sample *sample) 1714 { 1715 intel_pt_prep_a_sample(ptq, event, sample); 1716 1717 if (!pt->timeless_decoding) 1718 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc); 1719 1720 sample->ip = ptq->state->from_ip; 1721 sample->addr = ptq->state->to_ip; 1722 sample->cpumode = intel_pt_cpumode(ptq, sample->ip, sample->addr); 1723 sample->period = 1; 1724 sample->flags = ptq->flags; 1725 1726 event->sample.header.misc = sample->cpumode; 1727 } 1728 1729 static int intel_pt_inject_event(union perf_event *event, 1730 struct perf_sample *sample, u64 type) 1731 { 1732 event->header.size = perf_event__sample_event_size(sample, type, 0); 1733 return perf_event__synthesize_sample(event, type, 0, sample); 1734 } 1735 1736 static inline int intel_pt_opt_inject(struct intel_pt *pt, 1737 union perf_event *event, 1738 struct perf_sample *sample, u64 type) 1739 { 1740 if (!pt->synth_opts.inject) 1741 return 0; 1742 1743 return intel_pt_inject_event(event, sample, type); 1744 } 1745 1746 static int intel_pt_deliver_synth_event(struct intel_pt *pt, 1747 union perf_event *event, 1748 struct perf_sample *sample, u64 type) 1749 { 1750 int ret; 1751 1752 ret = intel_pt_opt_inject(pt, event, sample, type); 1753 if (ret) 1754 return ret; 1755 1756 ret = perf_session__deliver_synth_event(pt->session, event, sample); 1757 if (ret) 1758 pr_err("Intel PT: failed to deliver event, error %d\n", ret); 1759 1760 return ret; 1761 } 1762 1763 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq) 1764 { 1765 struct intel_pt *pt = ptq->pt; 1766 union perf_event *event = ptq->event_buf; 1767 struct perf_sample sample; 1768 struct dummy_branch_stack { 1769 u64 nr; 1770 u64 hw_idx; 1771 struct branch_entry entries; 1772 } dummy_bs; 1773 int ret; 1774 1775 if (pt->branches_filter && !(pt->branches_filter & ptq->flags)) 1776 return 0; 1777 1778 if (intel_pt_skip_event(pt)) 1779 return 0; 1780 1781 perf_sample__init(&sample, /*all=*/true); 1782 intel_pt_prep_b_sample(pt, ptq, event, &sample); 1783 1784 sample.id = ptq->pt->branches_id; 1785 sample.stream_id = ptq->pt->branches_id; 1786 1787 /* 1788 * perf report cannot handle events without a branch stack when using 1789 * SORT_MODE__BRANCH so make a dummy one. 1790 */ 1791 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) { 1792 dummy_bs = (struct dummy_branch_stack){ 1793 .nr = 1, 1794 .hw_idx = -1ULL, 1795 .entries = { 1796 .from = sample.ip, 1797 .to = sample.addr, 1798 }, 1799 }; 1800 sample.branch_stack = (struct branch_stack *)&dummy_bs; 1801 } 1802 1803 if (ptq->sample_ipc) 1804 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt; 1805 if (sample.cyc_cnt) { 1806 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt; 1807 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt; 1808 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt; 1809 } 1810 1811 perf_sample__exit(&sample); 1812 ret = intel_pt_deliver_synth_event(pt, event, &sample, 1813 pt->branches_sample_type); 1814 return ret; 1815 } 1816 1817 static void intel_pt_prep_sample(struct intel_pt *pt, 1818 struct intel_pt_queue *ptq, 1819 union perf_event *event, 1820 struct perf_sample *sample) 1821 { 1822 intel_pt_prep_b_sample(pt, ptq, event, sample); 1823 1824 if (pt->synth_opts.callchain) { 1825 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain, 1826 pt->synth_opts.callchain_sz + 1, 1827 sample->ip, pt->kernel_start); 1828 sample->callchain = ptq->chain; 1829 } 1830 1831 if (pt->synth_opts.last_branch) { 1832 thread_stack__br_sample(ptq->thread, ptq->cpu, ptq->last_branch, 1833 pt->br_stack_sz); 1834 sample->branch_stack = ptq->last_branch; 1835 } 1836 } 1837 1838 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq) 1839 { 1840 struct intel_pt *pt = ptq->pt; 1841 union perf_event *event = ptq->event_buf; 1842 struct perf_sample sample; 1843 int ret; 1844 1845 if (intel_pt_skip_event(pt)) 1846 return 0; 1847 1848 perf_sample__init(&sample, /*all=*/true); 1849 intel_pt_prep_sample(pt, ptq, event, &sample); 1850 1851 sample.id = ptq->pt->instructions_id; 1852 sample.stream_id = ptq->pt->instructions_id; 1853 if (pt->synth_opts.quick) 1854 sample.period = 1; 1855 else 1856 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt; 1857 1858 if (ptq->sample_ipc) 1859 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt; 1860 if (sample.cyc_cnt) { 1861 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt; 1862 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt; 1863 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt; 1864 } 1865 1866 ptq->last_insn_cnt = ptq->state->tot_insn_cnt; 1867 1868 ret = intel_pt_deliver_synth_event(pt, event, &sample, 1869 pt->instructions_sample_type); 1870 perf_sample__exit(&sample); 1871 return ret; 1872 } 1873 1874 static int intel_pt_synth_cycle_sample(struct intel_pt_queue *ptq) 1875 { 1876 struct intel_pt *pt = ptq->pt; 1877 union perf_event *event = ptq->event_buf; 1878 struct perf_sample sample; 1879 u64 period = 0; 1880 int ret; 1881 1882 if (ptq->sample_ipc) 1883 period = ptq->ipc_cyc_cnt - ptq->last_cy_cyc_cnt; 1884 1885 if (!period || intel_pt_skip_event(pt)) 1886 return 0; 1887 1888 perf_sample__init(&sample, /*all=*/true); 1889 intel_pt_prep_sample(pt, ptq, event, &sample); 1890 1891 sample.id = ptq->pt->cycles_id; 1892 sample.stream_id = ptq->pt->cycles_id; 1893 sample.period = period; 1894 1895 sample.cyc_cnt = period; 1896 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_cy_insn_cnt; 1897 ptq->last_cy_insn_cnt = ptq->ipc_insn_cnt; 1898 ptq->last_cy_cyc_cnt = ptq->ipc_cyc_cnt; 1899 1900 ret = intel_pt_deliver_synth_event(pt, event, &sample, pt->cycles_sample_type); 1901 perf_sample__exit(&sample); 1902 return ret; 1903 } 1904 1905 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq) 1906 { 1907 struct intel_pt *pt = ptq->pt; 1908 union perf_event *event = ptq->event_buf; 1909 struct perf_sample sample; 1910 int ret; 1911 1912 if (intel_pt_skip_event(pt)) 1913 return 0; 1914 1915 perf_sample__init(&sample, /*all=*/true); 1916 intel_pt_prep_sample(pt, ptq, event, &sample); 1917 1918 sample.id = ptq->pt->transactions_id; 1919 sample.stream_id = ptq->pt->transactions_id; 1920 1921 ret = intel_pt_deliver_synth_event(pt, event, &sample, 1922 pt->transactions_sample_type); 1923 perf_sample__exit(&sample); 1924 return ret; 1925 } 1926 1927 static void intel_pt_prep_p_sample(struct intel_pt *pt, 1928 struct intel_pt_queue *ptq, 1929 union perf_event *event, 1930 struct perf_sample *sample) 1931 { 1932 intel_pt_prep_sample(pt, ptq, event, sample); 1933 1934 /* 1935 * Zero IP is used to mean "trace start" but that is not the case for 1936 * power or PTWRITE events with no IP, so clear the flags. 1937 */ 1938 if (!sample->ip) 1939 sample->flags = 0; 1940 } 1941 1942 static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq) 1943 { 1944 struct intel_pt *pt = ptq->pt; 1945 union perf_event *event = ptq->event_buf; 1946 struct perf_sample sample = { .ip = 0, }; 1947 struct perf_synth_intel_ptwrite raw; 1948 1949 if (intel_pt_skip_event(pt)) 1950 return 0; 1951 1952 intel_pt_prep_p_sample(pt, ptq, event, &sample); 1953 1954 sample.id = ptq->pt->ptwrites_id; 1955 sample.stream_id = ptq->pt->ptwrites_id; 1956 1957 raw.flags = 0; 1958 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP); 1959 raw.payload = cpu_to_le64(ptq->state->ptw_payload); 1960 1961 sample.raw_size = perf_synth__raw_size(raw); 1962 sample.raw_data = perf_synth__raw_data(&raw); 1963 1964 return intel_pt_deliver_synth_event(pt, event, &sample, 1965 pt->ptwrites_sample_type); 1966 } 1967 1968 static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq) 1969 { 1970 struct intel_pt *pt = ptq->pt; 1971 union perf_event *event = ptq->event_buf; 1972 struct perf_sample sample; 1973 struct perf_synth_intel_cbr raw; 1974 u32 flags; 1975 int ret; 1976 1977 if (intel_pt_skip_cbr_event(pt)) 1978 return 0; 1979 1980 ptq->cbr_seen = ptq->state->cbr; 1981 1982 perf_sample__init(&sample, /*all=*/true); 1983 intel_pt_prep_p_sample(pt, ptq, event, &sample); 1984 1985 sample.id = ptq->pt->cbr_id; 1986 sample.stream_id = ptq->pt->cbr_id; 1987 1988 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16); 1989 raw.flags = cpu_to_le32(flags); 1990 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz); 1991 raw.reserved3 = 0; 1992 1993 sample.raw_size = perf_synth__raw_size(raw); 1994 sample.raw_data = perf_synth__raw_data(&raw); 1995 1996 ret = intel_pt_deliver_synth_event(pt, event, &sample, 1997 pt->pwr_events_sample_type); 1998 perf_sample__exit(&sample); 1999 return ret; 2000 } 2001 2002 static int intel_pt_synth_psb_sample(struct intel_pt_queue *ptq) 2003 { 2004 struct intel_pt *pt = ptq->pt; 2005 union perf_event *event = ptq->event_buf; 2006 struct perf_sample sample; 2007 struct perf_synth_intel_psb raw; 2008 int ret; 2009 2010 if (intel_pt_skip_event(pt)) 2011 return 0; 2012 2013 perf_sample__init(&sample, /*all=*/true); 2014 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2015 2016 sample.id = ptq->pt->psb_id; 2017 sample.stream_id = ptq->pt->psb_id; 2018 sample.flags = 0; 2019 2020 raw.reserved = 0; 2021 raw.offset = ptq->state->psb_offset; 2022 2023 sample.raw_size = perf_synth__raw_size(raw); 2024 sample.raw_data = perf_synth__raw_data(&raw); 2025 2026 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2027 pt->pwr_events_sample_type); 2028 perf_sample__exit(&sample); 2029 return ret; 2030 } 2031 2032 static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq) 2033 { 2034 struct intel_pt *pt = ptq->pt; 2035 union perf_event *event = ptq->event_buf; 2036 struct perf_sample sample; 2037 struct perf_synth_intel_mwait raw; 2038 int ret; 2039 2040 if (intel_pt_skip_event(pt)) 2041 return 0; 2042 2043 perf_sample__init(&sample, /*all=*/true); 2044 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2045 2046 sample.id = ptq->pt->mwait_id; 2047 sample.stream_id = ptq->pt->mwait_id; 2048 2049 raw.reserved = 0; 2050 raw.payload = cpu_to_le64(ptq->state->mwait_payload); 2051 2052 sample.raw_size = perf_synth__raw_size(raw); 2053 sample.raw_data = perf_synth__raw_data(&raw); 2054 2055 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2056 pt->pwr_events_sample_type); 2057 perf_sample__exit(&sample); 2058 return ret; 2059 } 2060 2061 static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq) 2062 { 2063 struct intel_pt *pt = ptq->pt; 2064 union perf_event *event = ptq->event_buf; 2065 struct perf_sample sample; 2066 struct perf_synth_intel_pwre raw; 2067 int ret; 2068 2069 if (intel_pt_skip_event(pt)) 2070 return 0; 2071 2072 perf_sample__init(&sample, /*all=*/true); 2073 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2074 2075 sample.id = ptq->pt->pwre_id; 2076 sample.stream_id = ptq->pt->pwre_id; 2077 2078 raw.reserved = 0; 2079 raw.payload = cpu_to_le64(ptq->state->pwre_payload); 2080 2081 sample.raw_size = perf_synth__raw_size(raw); 2082 sample.raw_data = perf_synth__raw_data(&raw); 2083 2084 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2085 pt->pwr_events_sample_type); 2086 perf_sample__exit(&sample); 2087 return ret; 2088 } 2089 2090 static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq) 2091 { 2092 struct intel_pt *pt = ptq->pt; 2093 union perf_event *event = ptq->event_buf; 2094 struct perf_sample sample; 2095 struct perf_synth_intel_exstop raw; 2096 int ret; 2097 2098 if (intel_pt_skip_event(pt)) 2099 return 0; 2100 2101 perf_sample__init(&sample, /*all=*/true); 2102 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2103 2104 sample.id = ptq->pt->exstop_id; 2105 sample.stream_id = ptq->pt->exstop_id; 2106 2107 raw.flags = 0; 2108 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP); 2109 2110 sample.raw_size = perf_synth__raw_size(raw); 2111 sample.raw_data = perf_synth__raw_data(&raw); 2112 2113 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2114 pt->pwr_events_sample_type); 2115 perf_sample__exit(&sample); 2116 return ret; 2117 } 2118 2119 static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq) 2120 { 2121 struct intel_pt *pt = ptq->pt; 2122 union perf_event *event = ptq->event_buf; 2123 struct perf_sample sample; 2124 struct perf_synth_intel_pwrx raw; 2125 int ret; 2126 2127 if (intel_pt_skip_event(pt)) 2128 return 0; 2129 2130 perf_sample__init(&sample, /*all=*/true); 2131 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2132 2133 sample.id = ptq->pt->pwrx_id; 2134 sample.stream_id = ptq->pt->pwrx_id; 2135 2136 raw.reserved = 0; 2137 raw.payload = cpu_to_le64(ptq->state->pwrx_payload); 2138 2139 sample.raw_size = perf_synth__raw_size(raw); 2140 sample.raw_data = perf_synth__raw_data(&raw); 2141 2142 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2143 pt->pwr_events_sample_type); 2144 perf_sample__exit(&sample); 2145 return ret; 2146 } 2147 2148 /* 2149 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer 2150 * intel_pt_add_gp_regs(). 2151 */ 2152 static const int pebs_gp_regs[] = { 2153 [PERF_REG_X86_FLAGS] = 1, 2154 [PERF_REG_X86_IP] = 2, 2155 [PERF_REG_X86_AX] = 3, 2156 [PERF_REG_X86_CX] = 4, 2157 [PERF_REG_X86_DX] = 5, 2158 [PERF_REG_X86_BX] = 6, 2159 [PERF_REG_X86_SP] = 7, 2160 [PERF_REG_X86_BP] = 8, 2161 [PERF_REG_X86_SI] = 9, 2162 [PERF_REG_X86_DI] = 10, 2163 [PERF_REG_X86_R8] = 11, 2164 [PERF_REG_X86_R9] = 12, 2165 [PERF_REG_X86_R10] = 13, 2166 [PERF_REG_X86_R11] = 14, 2167 [PERF_REG_X86_R12] = 15, 2168 [PERF_REG_X86_R13] = 16, 2169 [PERF_REG_X86_R14] = 17, 2170 [PERF_REG_X86_R15] = 18, 2171 }; 2172 2173 static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos, 2174 const struct intel_pt_blk_items *items, 2175 u64 regs_mask) 2176 { 2177 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS]; 2178 u32 mask = items->mask[INTEL_PT_GP_REGS_POS]; 2179 u32 bit; 2180 int i; 2181 2182 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) { 2183 /* Get the PEBS gp_regs array index */ 2184 int n = pebs_gp_regs[i] - 1; 2185 2186 if (n < 0) 2187 continue; 2188 /* 2189 * Add only registers that were requested (i.e. 'regs_mask') and 2190 * that were provided (i.e. 'mask'), and update the resulting 2191 * mask (i.e. 'intr_regs->mask') accordingly. 2192 */ 2193 if (mask & 1 << n && regs_mask & bit) { 2194 intr_regs->mask |= bit; 2195 *pos++ = gp_regs[n]; 2196 } 2197 } 2198 2199 return pos; 2200 } 2201 2202 #ifndef PERF_REG_X86_XMM0 2203 #define PERF_REG_X86_XMM0 32 2204 #endif 2205 2206 static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos, 2207 const struct intel_pt_blk_items *items, 2208 u64 regs_mask) 2209 { 2210 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0); 2211 const u64 *xmm = items->xmm; 2212 2213 /* 2214 * If there are any XMM registers, then there should be all of them. 2215 * Nevertheless, follow the logic to add only registers that were 2216 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'), 2217 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly. 2218 */ 2219 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0; 2220 2221 for (; mask; mask >>= 1, xmm++) { 2222 if (mask & 1) 2223 *pos++ = *xmm; 2224 } 2225 } 2226 2227 #define LBR_INFO_MISPRED (1ULL << 63) 2228 #define LBR_INFO_IN_TX (1ULL << 62) 2229 #define LBR_INFO_ABORT (1ULL << 61) 2230 #define LBR_INFO_CYCLES 0xffff 2231 2232 /* Refer kernel's intel_pmu_store_pebs_lbrs() */ 2233 static u64 intel_pt_lbr_flags(u64 info) 2234 { 2235 union { 2236 struct branch_flags flags; 2237 u64 result; 2238 } u; 2239 2240 u.result = 0; 2241 u.flags.mispred = !!(info & LBR_INFO_MISPRED); 2242 u.flags.predicted = !(info & LBR_INFO_MISPRED); 2243 u.flags.in_tx = !!(info & LBR_INFO_IN_TX); 2244 u.flags.abort = !!(info & LBR_INFO_ABORT); 2245 u.flags.cycles = info & LBR_INFO_CYCLES; 2246 2247 return u.result; 2248 } 2249 2250 static void intel_pt_add_lbrs(struct branch_stack *br_stack, 2251 const struct intel_pt_blk_items *items) 2252 { 2253 u64 *to; 2254 int i; 2255 2256 br_stack->nr = 0; 2257 2258 to = &br_stack->entries[0].from; 2259 2260 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) { 2261 u32 mask = items->mask[i]; 2262 const u64 *from = items->val[i]; 2263 2264 for (; mask; mask >>= 3, from += 3) { 2265 if ((mask & 7) == 7) { 2266 *to++ = from[0]; 2267 *to++ = from[1]; 2268 *to++ = intel_pt_lbr_flags(from[2]); 2269 br_stack->nr += 1; 2270 } 2271 } 2272 } 2273 } 2274 2275 static int intel_pt_do_synth_pebs_sample(struct intel_pt_queue *ptq, struct evsel *evsel, u64 id) 2276 { 2277 const struct intel_pt_blk_items *items = &ptq->state->items; 2278 struct perf_sample sample; 2279 union perf_event *event = ptq->event_buf; 2280 struct intel_pt *pt = ptq->pt; 2281 u64 sample_type = evsel->core.attr.sample_type; 2282 u8 cpumode; 2283 u64 regs[8 * sizeof(sample.intr_regs->mask)]; 2284 int ret; 2285 2286 if (intel_pt_skip_event(pt)) 2287 return 0; 2288 2289 perf_sample__init(&sample, /*all=*/true); 2290 intel_pt_prep_a_sample(ptq, event, &sample); 2291 2292 sample.id = id; 2293 sample.stream_id = id; 2294 2295 if (!evsel->core.attr.freq) 2296 sample.period = evsel->core.attr.sample_period; 2297 2298 /* No support for non-zero CS base */ 2299 if (items->has_ip) 2300 sample.ip = items->ip; 2301 else if (items->has_rip) 2302 sample.ip = items->rip; 2303 else 2304 sample.ip = ptq->state->from_ip; 2305 2306 cpumode = intel_pt_cpumode(ptq, sample.ip, 0); 2307 2308 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP; 2309 2310 sample.cpumode = cpumode; 2311 2312 if (sample_type & PERF_SAMPLE_TIME) { 2313 u64 timestamp = 0; 2314 2315 if (items->has_timestamp) 2316 timestamp = items->timestamp; 2317 else if (!pt->timeless_decoding) 2318 timestamp = ptq->timestamp; 2319 if (timestamp) 2320 sample.time = tsc_to_perf_time(timestamp, &pt->tc); 2321 } 2322 2323 if (sample_type & PERF_SAMPLE_CALLCHAIN && 2324 pt->synth_opts.callchain) { 2325 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain, 2326 pt->synth_opts.callchain_sz, sample.ip, 2327 pt->kernel_start); 2328 sample.callchain = ptq->chain; 2329 } 2330 2331 if (sample_type & PERF_SAMPLE_REGS_INTR && 2332 (items->mask[INTEL_PT_GP_REGS_POS] || 2333 items->mask[INTEL_PT_XMM_POS])) { 2334 u64 regs_mask = evsel->core.attr.sample_regs_intr; 2335 u64 *pos; 2336 struct regs_dump *intr_regs = perf_sample__intr_regs(&sample); 2337 2338 intr_regs->abi = items->is_32_bit ? 2339 PERF_SAMPLE_REGS_ABI_32 : 2340 PERF_SAMPLE_REGS_ABI_64; 2341 intr_regs->regs = regs; 2342 2343 pos = intel_pt_add_gp_regs(intr_regs, regs, items, regs_mask); 2344 2345 intel_pt_add_xmm(intr_regs, pos, items, regs_mask); 2346 } 2347 2348 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { 2349 if (items->mask[INTEL_PT_LBR_0_POS] || 2350 items->mask[INTEL_PT_LBR_1_POS] || 2351 items->mask[INTEL_PT_LBR_2_POS]) { 2352 intel_pt_add_lbrs(ptq->last_branch, items); 2353 } else if (pt->synth_opts.last_branch) { 2354 thread_stack__br_sample(ptq->thread, ptq->cpu, 2355 ptq->last_branch, 2356 pt->br_stack_sz); 2357 } else { 2358 ptq->last_branch->nr = 0; 2359 } 2360 sample.branch_stack = ptq->last_branch; 2361 } 2362 2363 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address) 2364 sample.addr = items->mem_access_address; 2365 2366 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) { 2367 /* 2368 * Refer kernel's setup_pebs_adaptive_sample_data() and 2369 * intel_hsw_weight(). 2370 */ 2371 if (items->has_mem_access_latency) { 2372 u64 weight = items->mem_access_latency >> 32; 2373 2374 /* 2375 * Starts from SPR, the mem access latency field 2376 * contains both cache latency [47:32] and instruction 2377 * latency [15:0]. The cache latency is the same as the 2378 * mem access latency on previous platforms. 2379 * 2380 * In practice, no memory access could last than 4G 2381 * cycles. Use latency >> 32 to distinguish the 2382 * different format of the mem access latency field. 2383 */ 2384 if (weight > 0) { 2385 sample.weight = weight & 0xffff; 2386 sample.ins_lat = items->mem_access_latency & 0xffff; 2387 } else 2388 sample.weight = items->mem_access_latency; 2389 } 2390 if (!sample.weight && items->has_tsx_aux_info) { 2391 /* Cycles last block */ 2392 sample.weight = (u32)items->tsx_aux_info; 2393 } 2394 } 2395 2396 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) { 2397 u64 ax = items->has_rax ? items->rax : 0; 2398 /* Refer kernel's intel_hsw_transaction() */ 2399 u64 txn = (u8)(items->tsx_aux_info >> 32); 2400 2401 /* For RTM XABORTs also log the abort code from AX */ 2402 if (txn & PERF_TXN_TRANSACTION && ax & 1) 2403 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT; 2404 sample.transaction = txn; 2405 } 2406 2407 ret = intel_pt_deliver_synth_event(pt, event, &sample, sample_type); 2408 perf_sample__exit(&sample); 2409 return ret; 2410 } 2411 2412 static int intel_pt_synth_single_pebs_sample(struct intel_pt_queue *ptq) 2413 { 2414 struct intel_pt *pt = ptq->pt; 2415 struct evsel *evsel = pt->pebs_evsel; 2416 u64 id = evsel->core.id[0]; 2417 2418 return intel_pt_do_synth_pebs_sample(ptq, evsel, id); 2419 } 2420 2421 static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq) 2422 { 2423 const struct intel_pt_blk_items *items = &ptq->state->items; 2424 struct intel_pt_pebs_event *pe; 2425 struct intel_pt *pt = ptq->pt; 2426 int err = -EINVAL; 2427 int hw_id; 2428 2429 if (!items->has_applicable_counters || !items->applicable_counters) { 2430 if (!pt->single_pebs) 2431 pr_err("PEBS-via-PT record with no applicable_counters\n"); 2432 return intel_pt_synth_single_pebs_sample(ptq); 2433 } 2434 2435 for_each_set_bit(hw_id, (unsigned long *)&items->applicable_counters, INTEL_PT_MAX_PEBS) { 2436 pe = &ptq->pebs[hw_id]; 2437 if (!pe->evsel) { 2438 if (!pt->single_pebs) 2439 pr_err("PEBS-via-PT record with no matching event, hw_id %d\n", 2440 hw_id); 2441 return intel_pt_synth_single_pebs_sample(ptq); 2442 } 2443 err = intel_pt_do_synth_pebs_sample(ptq, pe->evsel, pe->id); 2444 if (err) 2445 return err; 2446 } 2447 2448 return err; 2449 } 2450 2451 static int intel_pt_synth_events_sample(struct intel_pt_queue *ptq) 2452 { 2453 struct intel_pt *pt = ptq->pt; 2454 union perf_event *event = ptq->event_buf; 2455 struct perf_sample sample; 2456 struct { 2457 struct perf_synth_intel_evt cfe; 2458 struct perf_synth_intel_evd evd[INTEL_PT_MAX_EVDS]; 2459 } raw; 2460 int i, ret; 2461 2462 if (intel_pt_skip_event(pt)) 2463 return 0; 2464 2465 perf_sample__init(&sample, /*all=*/true); 2466 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2467 2468 sample.id = ptq->pt->evt_id; 2469 sample.stream_id = ptq->pt->evt_id; 2470 2471 raw.cfe.type = ptq->state->cfe_type; 2472 raw.cfe.reserved = 0; 2473 raw.cfe.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP); 2474 raw.cfe.vector = ptq->state->cfe_vector; 2475 raw.cfe.evd_cnt = ptq->state->evd_cnt; 2476 2477 for (i = 0; i < ptq->state->evd_cnt; i++) { 2478 raw.evd[i].et = 0; 2479 raw.evd[i].evd_type = ptq->state->evd[i].type; 2480 raw.evd[i].payload = ptq->state->evd[i].payload; 2481 } 2482 2483 sample.raw_size = perf_synth__raw_size(raw) + 2484 ptq->state->evd_cnt * sizeof(struct perf_synth_intel_evd); 2485 sample.raw_data = perf_synth__raw_data(&raw); 2486 2487 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2488 pt->evt_sample_type); 2489 perf_sample__exit(&sample); 2490 return ret; 2491 } 2492 2493 static int intel_pt_synth_iflag_chg_sample(struct intel_pt_queue *ptq) 2494 { 2495 struct intel_pt *pt = ptq->pt; 2496 union perf_event *event = ptq->event_buf; 2497 struct perf_sample sample; 2498 struct perf_synth_intel_iflag_chg raw; 2499 int ret; 2500 2501 if (intel_pt_skip_event(pt)) 2502 return 0; 2503 2504 perf_sample__init(&sample, /*all=*/true); 2505 intel_pt_prep_p_sample(pt, ptq, event, &sample); 2506 2507 sample.id = ptq->pt->iflag_chg_id; 2508 sample.stream_id = ptq->pt->iflag_chg_id; 2509 2510 raw.flags = 0; 2511 raw.iflag = ptq->state->to_iflag; 2512 2513 if (ptq->state->type & INTEL_PT_BRANCH) { 2514 raw.via_branch = 1; 2515 raw.branch_ip = ptq->state->to_ip; 2516 } else { 2517 sample.addr = 0; 2518 } 2519 sample.flags = ptq->flags; 2520 2521 sample.raw_size = perf_synth__raw_size(raw); 2522 sample.raw_data = perf_synth__raw_data(&raw); 2523 2524 ret = intel_pt_deliver_synth_event(pt, event, &sample, 2525 pt->iflag_chg_sample_type); 2526 perf_sample__exit(&sample); 2527 return ret; 2528 } 2529 2530 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu, 2531 pid_t pid, pid_t tid, u64 ip, u64 timestamp, 2532 pid_t machine_pid, int vcpu) 2533 { 2534 bool dump_log_on_error = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR; 2535 bool log_on_stdout = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT; 2536 union perf_event event; 2537 char msg[MAX_AUXTRACE_ERROR_MSG]; 2538 int err; 2539 2540 if (pt->synth_opts.error_minus_flags) { 2541 if (code == INTEL_PT_ERR_OVR && 2542 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_OVERFLOW) 2543 return 0; 2544 if (code == INTEL_PT_ERR_LOST && 2545 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_DATA_LOST) 2546 return 0; 2547 } 2548 2549 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG); 2550 2551 auxtrace_synth_guest_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE, 2552 code, cpu, pid, tid, ip, msg, timestamp, 2553 machine_pid, vcpu); 2554 2555 if (intel_pt_enable_logging && !log_on_stdout) { 2556 FILE *fp = intel_pt_log_fp(); 2557 2558 if (fp) 2559 perf_event__fprintf_auxtrace_error(&event, fp); 2560 } 2561 2562 if (code != INTEL_PT_ERR_LOST && dump_log_on_error) 2563 intel_pt_log_dump_buf(); 2564 2565 err = perf_session__deliver_synth_event(pt->session, &event, NULL); 2566 if (err) 2567 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n", 2568 err); 2569 2570 return err; 2571 } 2572 2573 static int intel_ptq_synth_error(struct intel_pt_queue *ptq, 2574 const struct intel_pt_state *state) 2575 { 2576 struct intel_pt *pt = ptq->pt; 2577 u64 tm = ptq->timestamp; 2578 pid_t machine_pid = 0; 2579 pid_t pid = ptq->pid; 2580 pid_t tid = ptq->tid; 2581 int vcpu = -1; 2582 2583 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc); 2584 2585 if (pt->have_guest_sideband && state->from_nr) { 2586 machine_pid = ptq->guest_machine_pid; 2587 vcpu = ptq->vcpu; 2588 pid = ptq->guest_pid; 2589 tid = ptq->guest_tid; 2590 } 2591 2592 return intel_pt_synth_error(pt, state->err, ptq->cpu, pid, tid, 2593 state->from_ip, tm, machine_pid, vcpu); 2594 } 2595 2596 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq) 2597 { 2598 struct auxtrace_queue *queue; 2599 pid_t tid = ptq->next_tid; 2600 int err; 2601 2602 if (tid == -1) 2603 return 0; 2604 2605 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid); 2606 2607 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid); 2608 2609 queue = &pt->queues.queue_array[ptq->queue_nr]; 2610 intel_pt_set_pid_tid_cpu(pt, queue); 2611 2612 ptq->next_tid = -1; 2613 2614 return err; 2615 } 2616 2617 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip) 2618 { 2619 struct intel_pt *pt = ptq->pt; 2620 2621 return ip == pt->switch_ip && 2622 (ptq->flags & PERF_IP_FLAG_BRANCH) && 2623 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC | 2624 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT)); 2625 } 2626 2627 #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \ 2628 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT) 2629 2630 static int intel_pt_sample(struct intel_pt_queue *ptq) 2631 { 2632 const struct intel_pt_state *state = ptq->state; 2633 struct intel_pt *pt = ptq->pt; 2634 int err; 2635 2636 if (!ptq->have_sample) 2637 return 0; 2638 2639 ptq->have_sample = false; 2640 2641 if (pt->synth_opts.approx_ipc) { 2642 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt; 2643 ptq->ipc_cyc_cnt = ptq->state->cycles; 2644 ptq->sample_ipc = true; 2645 } else { 2646 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt; 2647 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt; 2648 ptq->sample_ipc = ptq->state->flags & INTEL_PT_SAMPLE_IPC; 2649 } 2650 2651 /* Ensure guest code maps are set up */ 2652 if (symbol_conf.guest_code && (state->from_nr || state->to_nr)) 2653 intel_pt_get_guest(ptq); 2654 2655 /* 2656 * Do PEBS first to allow for the possibility that the PEBS timestamp 2657 * precedes the current timestamp. 2658 */ 2659 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) { 2660 err = intel_pt_synth_pebs_sample(ptq); 2661 if (err) 2662 return err; 2663 } 2664 2665 if (pt->synth_opts.intr_events) { 2666 if (state->type & INTEL_PT_EVT) { 2667 err = intel_pt_synth_events_sample(ptq); 2668 if (err) 2669 return err; 2670 } 2671 if (state->type & INTEL_PT_IFLAG_CHG) { 2672 err = intel_pt_synth_iflag_chg_sample(ptq); 2673 if (err) 2674 return err; 2675 } 2676 } 2677 2678 if (pt->sample_pwr_events) { 2679 if (state->type & INTEL_PT_PSB_EVT) { 2680 err = intel_pt_synth_psb_sample(ptq); 2681 if (err) 2682 return err; 2683 } 2684 if (ptq->state->cbr != ptq->cbr_seen) { 2685 err = intel_pt_synth_cbr_sample(ptq); 2686 if (err) 2687 return err; 2688 } 2689 if (state->type & INTEL_PT_PWR_EVT) { 2690 if (state->type & INTEL_PT_MWAIT_OP) { 2691 err = intel_pt_synth_mwait_sample(ptq); 2692 if (err) 2693 return err; 2694 } 2695 if (state->type & INTEL_PT_PWR_ENTRY) { 2696 err = intel_pt_synth_pwre_sample(ptq); 2697 if (err) 2698 return err; 2699 } 2700 if (state->type & INTEL_PT_EX_STOP) { 2701 err = intel_pt_synth_exstop_sample(ptq); 2702 if (err) 2703 return err; 2704 } 2705 if (state->type & INTEL_PT_PWR_EXIT) { 2706 err = intel_pt_synth_pwrx_sample(ptq); 2707 if (err) 2708 return err; 2709 } 2710 } 2711 } 2712 2713 if (state->type & INTEL_PT_INSTRUCTION) { 2714 if (pt->sample_instructions) { 2715 err = intel_pt_synth_instruction_sample(ptq); 2716 if (err) 2717 return err; 2718 } 2719 if (pt->sample_cycles) { 2720 err = intel_pt_synth_cycle_sample(ptq); 2721 if (err) 2722 return err; 2723 } 2724 } 2725 2726 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) { 2727 err = intel_pt_synth_transaction_sample(ptq); 2728 if (err) 2729 return err; 2730 } 2731 2732 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) { 2733 err = intel_pt_synth_ptwrite_sample(ptq); 2734 if (err) 2735 return err; 2736 } 2737 2738 if (!(state->type & INTEL_PT_BRANCH)) 2739 return 0; 2740 2741 if (pt->use_thread_stack) { 2742 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags, 2743 state->from_ip, state->to_ip, ptq->insn_len, 2744 state->trace_nr, pt->callstack, 2745 pt->br_stack_sz_plus, 2746 pt->mispred_all); 2747 } else { 2748 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr); 2749 } 2750 2751 if (pt->sample_branches) { 2752 if (state->from_nr != state->to_nr && 2753 state->from_ip && state->to_ip) { 2754 struct intel_pt_state *st = (struct intel_pt_state *)state; 2755 u64 to_ip = st->to_ip; 2756 u64 from_ip = st->from_ip; 2757 2758 /* 2759 * perf cannot handle having different machines for ip 2760 * and addr, so create 2 branches. 2761 */ 2762 st->to_ip = 0; 2763 err = intel_pt_synth_branch_sample(ptq); 2764 if (err) 2765 return err; 2766 st->from_ip = 0; 2767 st->to_ip = to_ip; 2768 err = intel_pt_synth_branch_sample(ptq); 2769 st->from_ip = from_ip; 2770 } else { 2771 err = intel_pt_synth_branch_sample(ptq); 2772 } 2773 if (err) 2774 return err; 2775 } 2776 2777 if (!ptq->sync_switch) 2778 return 0; 2779 2780 if (intel_pt_is_switch_ip(ptq, state->to_ip)) { 2781 switch (ptq->switch_state) { 2782 case INTEL_PT_SS_NOT_TRACING: 2783 case INTEL_PT_SS_UNKNOWN: 2784 case INTEL_PT_SS_EXPECTING_SWITCH_IP: 2785 err = intel_pt_next_tid(pt, ptq); 2786 if (err) 2787 return err; 2788 ptq->switch_state = INTEL_PT_SS_TRACING; 2789 break; 2790 default: 2791 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT; 2792 return 1; 2793 } 2794 } else if (!state->to_ip) { 2795 ptq->switch_state = INTEL_PT_SS_NOT_TRACING; 2796 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) { 2797 ptq->switch_state = INTEL_PT_SS_UNKNOWN; 2798 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN && 2799 state->to_ip == pt->ptss_ip && 2800 (ptq->flags & PERF_IP_FLAG_CALL)) { 2801 ptq->switch_state = INTEL_PT_SS_TRACING; 2802 } 2803 2804 return 0; 2805 } 2806 2807 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip) 2808 { 2809 struct machine *machine = pt->machine; 2810 struct map *map; 2811 struct symbol *sym, *start; 2812 u64 ip, switch_ip = 0; 2813 const char *ptss; 2814 2815 if (ptss_ip) 2816 *ptss_ip = 0; 2817 2818 map = machine__kernel_map(machine); 2819 if (!map) 2820 return 0; 2821 2822 if (map__load(map)) 2823 return 0; 2824 2825 start = dso__first_symbol(map__dso(map)); 2826 2827 for (sym = start; sym; sym = dso__next_symbol(sym)) { 2828 if (sym->binding == STB_GLOBAL && 2829 !strcmp(sym->name, "__switch_to")) { 2830 ip = map__unmap_ip(map, sym->start); 2831 if (ip >= map__start(map) && ip < map__end(map)) { 2832 switch_ip = ip; 2833 break; 2834 } 2835 } 2836 } 2837 2838 if (!switch_ip || !ptss_ip) 2839 return 0; 2840 2841 if (pt->have_sched_switch == 1) 2842 ptss = "perf_trace_sched_switch"; 2843 else 2844 ptss = "__perf_event_task_sched_out"; 2845 2846 for (sym = start; sym; sym = dso__next_symbol(sym)) { 2847 if (!strcmp(sym->name, ptss)) { 2848 ip = map__unmap_ip(map, sym->start); 2849 if (ip >= map__start(map) && ip < map__end(map)) { 2850 *ptss_ip = ip; 2851 break; 2852 } 2853 } 2854 } 2855 2856 return switch_ip; 2857 } 2858 2859 static void intel_pt_enable_sync_switch(struct intel_pt *pt) 2860 { 2861 unsigned int i; 2862 2863 if (pt->sync_switch_not_supported) 2864 return; 2865 2866 pt->sync_switch = true; 2867 2868 for (i = 0; i < pt->queues.nr_queues; i++) { 2869 struct auxtrace_queue *queue = &pt->queues.queue_array[i]; 2870 struct intel_pt_queue *ptq = queue->priv; 2871 2872 if (ptq) 2873 ptq->sync_switch = true; 2874 } 2875 } 2876 2877 static void intel_pt_disable_sync_switch(struct intel_pt *pt) 2878 { 2879 unsigned int i; 2880 2881 pt->sync_switch = false; 2882 2883 for (i = 0; i < pt->queues.nr_queues; i++) { 2884 struct auxtrace_queue *queue = &pt->queues.queue_array[i]; 2885 struct intel_pt_queue *ptq = queue->priv; 2886 2887 if (ptq) { 2888 ptq->sync_switch = false; 2889 intel_pt_next_tid(pt, ptq); 2890 } 2891 } 2892 } 2893 2894 /* 2895 * To filter against time ranges, it is only necessary to look at the next start 2896 * or end time. 2897 */ 2898 static bool intel_pt_next_time(struct intel_pt_queue *ptq) 2899 { 2900 struct intel_pt *pt = ptq->pt; 2901 2902 if (ptq->sel_start) { 2903 /* Next time is an end time */ 2904 ptq->sel_start = false; 2905 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end; 2906 return true; 2907 } else if (ptq->sel_idx + 1 < pt->range_cnt) { 2908 /* Next time is a start time */ 2909 ptq->sel_start = true; 2910 ptq->sel_idx += 1; 2911 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start; 2912 return true; 2913 } 2914 2915 /* No next time */ 2916 return false; 2917 } 2918 2919 static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp) 2920 { 2921 int err; 2922 2923 while (1) { 2924 if (ptq->sel_start) { 2925 if (ptq->timestamp >= ptq->sel_timestamp) { 2926 /* After start time, so consider next time */ 2927 intel_pt_next_time(ptq); 2928 if (!ptq->sel_timestamp) { 2929 /* No end time */ 2930 return 0; 2931 } 2932 /* Check against end time */ 2933 continue; 2934 } 2935 /* Before start time, so fast forward */ 2936 ptq->have_sample = false; 2937 if (ptq->sel_timestamp > *ff_timestamp) { 2938 if (ptq->sync_switch) { 2939 intel_pt_next_tid(ptq->pt, ptq); 2940 ptq->switch_state = INTEL_PT_SS_UNKNOWN; 2941 } 2942 *ff_timestamp = ptq->sel_timestamp; 2943 err = intel_pt_fast_forward(ptq->decoder, 2944 ptq->sel_timestamp); 2945 if (err) 2946 return err; 2947 } 2948 return 0; 2949 } else if (ptq->timestamp > ptq->sel_timestamp) { 2950 /* After end time, so consider next time */ 2951 if (!intel_pt_next_time(ptq)) { 2952 /* No next time range, so stop decoding */ 2953 ptq->have_sample = false; 2954 ptq->switch_state = INTEL_PT_SS_NOT_TRACING; 2955 return 1; 2956 } 2957 /* Check against next start time */ 2958 continue; 2959 } else { 2960 /* Before end time */ 2961 return 0; 2962 } 2963 } 2964 } 2965 2966 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp) 2967 { 2968 const struct intel_pt_state *state = ptq->state; 2969 struct intel_pt *pt = ptq->pt; 2970 u64 ff_timestamp = 0; 2971 int err; 2972 2973 if (!pt->kernel_start) { 2974 pt->kernel_start = machine__kernel_start(pt->machine); 2975 if (pt->per_cpu_mmaps && 2976 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) && 2977 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) && 2978 !pt->sampling_mode && !pt->synth_opts.vm_time_correlation) { 2979 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip); 2980 if (pt->switch_ip) { 2981 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n", 2982 pt->switch_ip, pt->ptss_ip); 2983 intel_pt_enable_sync_switch(pt); 2984 } 2985 } 2986 } 2987 2988 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n", 2989 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid); 2990 while (1) { 2991 err = intel_pt_sample(ptq); 2992 if (err) 2993 return err; 2994 2995 state = intel_pt_decode(ptq->decoder); 2996 if (state->err) { 2997 if (state->err == INTEL_PT_ERR_NODATA) 2998 return 1; 2999 if (ptq->sync_switch && 3000 state->from_ip >= pt->kernel_start) { 3001 ptq->sync_switch = false; 3002 intel_pt_next_tid(pt, ptq); 3003 } 3004 ptq->timestamp = state->est_timestamp; 3005 if (pt->synth_opts.errors) { 3006 err = intel_ptq_synth_error(ptq, state); 3007 if (err) 3008 return err; 3009 } 3010 continue; 3011 } 3012 3013 ptq->state = state; 3014 ptq->have_sample = true; 3015 intel_pt_sample_flags(ptq); 3016 3017 /* Use estimated TSC upon return to user space */ 3018 if (pt->est_tsc && 3019 (state->from_ip >= pt->kernel_start || !state->from_ip) && 3020 state->to_ip && state->to_ip < pt->kernel_start) { 3021 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n", 3022 state->timestamp, state->est_timestamp); 3023 ptq->timestamp = state->est_timestamp; 3024 /* Use estimated TSC in unknown switch state */ 3025 } else if (ptq->sync_switch && 3026 ptq->switch_state == INTEL_PT_SS_UNKNOWN && 3027 intel_pt_is_switch_ip(ptq, state->to_ip) && 3028 ptq->next_tid == -1) { 3029 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n", 3030 state->timestamp, state->est_timestamp); 3031 ptq->timestamp = state->est_timestamp; 3032 } else if (state->timestamp > ptq->timestamp) { 3033 ptq->timestamp = state->timestamp; 3034 } 3035 3036 if (ptq->sel_timestamp) { 3037 err = intel_pt_time_filter(ptq, &ff_timestamp); 3038 if (err) 3039 return err; 3040 } 3041 3042 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) { 3043 *timestamp = ptq->timestamp; 3044 return 0; 3045 } 3046 } 3047 return 0; 3048 } 3049 3050 static inline int intel_pt_update_queues(struct intel_pt *pt) 3051 { 3052 if (pt->queues.new_data) { 3053 pt->queues.new_data = false; 3054 return intel_pt_setup_queues(pt); 3055 } 3056 return 0; 3057 } 3058 3059 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp) 3060 { 3061 unsigned int queue_nr; 3062 u64 ts; 3063 int ret; 3064 3065 while (1) { 3066 struct auxtrace_queue *queue; 3067 struct intel_pt_queue *ptq; 3068 3069 if (!pt->heap.heap_cnt) 3070 return 0; 3071 3072 if (pt->heap.heap_array[0].ordinal >= timestamp) 3073 return 0; 3074 3075 queue_nr = pt->heap.heap_array[0].queue_nr; 3076 queue = &pt->queues.queue_array[queue_nr]; 3077 ptq = queue->priv; 3078 3079 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n", 3080 queue_nr, pt->heap.heap_array[0].ordinal, 3081 timestamp); 3082 3083 auxtrace_heap__pop(&pt->heap); 3084 3085 if (pt->heap.heap_cnt) { 3086 ts = pt->heap.heap_array[0].ordinal + 1; 3087 if (ts > timestamp) 3088 ts = timestamp; 3089 } else { 3090 ts = timestamp; 3091 } 3092 3093 intel_pt_set_pid_tid_cpu(pt, queue); 3094 3095 ret = intel_pt_run_decoder(ptq, &ts); 3096 3097 if (ret < 0) { 3098 auxtrace_heap__add(&pt->heap, queue_nr, ts); 3099 return ret; 3100 } 3101 3102 if (!ret) { 3103 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts); 3104 if (ret < 0) 3105 return ret; 3106 } else { 3107 ptq->on_heap = false; 3108 } 3109 } 3110 3111 return 0; 3112 } 3113 3114 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid, 3115 u64 time_) 3116 { 3117 struct auxtrace_queues *queues = &pt->queues; 3118 unsigned int i; 3119 u64 ts = 0; 3120 3121 for (i = 0; i < queues->nr_queues; i++) { 3122 struct auxtrace_queue *queue = &pt->queues.queue_array[i]; 3123 struct intel_pt_queue *ptq = queue->priv; 3124 3125 if (ptq && (tid == -1 || ptq->tid == tid)) { 3126 ptq->time = time_; 3127 intel_pt_set_pid_tid_cpu(pt, queue); 3128 intel_pt_run_decoder(ptq, &ts); 3129 } 3130 } 3131 return 0; 3132 } 3133 3134 static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq, 3135 struct auxtrace_queue *queue, 3136 struct perf_sample *sample) 3137 { 3138 struct machine *m = ptq->pt->machine; 3139 3140 ptq->pid = sample->pid; 3141 ptq->tid = sample->tid; 3142 ptq->cpu = queue->cpu; 3143 3144 intel_pt_log("queue %u cpu %d pid %d tid %d\n", 3145 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid); 3146 3147 thread__zput(ptq->thread); 3148 3149 if (ptq->tid == -1) 3150 return; 3151 3152 if (ptq->pid == -1) { 3153 ptq->thread = machine__find_thread(m, -1, ptq->tid); 3154 if (ptq->thread) 3155 ptq->pid = thread__pid(ptq->thread); 3156 return; 3157 } 3158 3159 ptq->thread = machine__findnew_thread(m, ptq->pid, ptq->tid); 3160 } 3161 3162 static int intel_pt_process_timeless_sample(struct intel_pt *pt, 3163 struct perf_sample *sample) 3164 { 3165 struct auxtrace_queue *queue; 3166 struct intel_pt_queue *ptq; 3167 u64 ts = 0; 3168 3169 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session); 3170 if (!queue) 3171 return -EINVAL; 3172 3173 ptq = queue->priv; 3174 if (!ptq) 3175 return 0; 3176 3177 ptq->stop = false; 3178 ptq->time = sample->time; 3179 intel_pt_sample_set_pid_tid_cpu(ptq, queue, sample); 3180 intel_pt_run_decoder(ptq, &ts); 3181 return 0; 3182 } 3183 3184 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample) 3185 { 3186 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu, 3187 sample->pid, sample->tid, 0, sample->time, 3188 sample->machine_pid, sample->vcpu); 3189 } 3190 3191 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu) 3192 { 3193 unsigned i, j; 3194 3195 if (cpu < 0 || !pt->queues.nr_queues) 3196 return NULL; 3197 3198 if ((unsigned)cpu >= pt->queues.nr_queues) 3199 i = pt->queues.nr_queues - 1; 3200 else 3201 i = cpu; 3202 3203 if (pt->queues.queue_array[i].cpu == cpu) 3204 return pt->queues.queue_array[i].priv; 3205 3206 for (j = 0; i > 0; j++) { 3207 if (pt->queues.queue_array[--i].cpu == cpu) 3208 return pt->queues.queue_array[i].priv; 3209 } 3210 3211 for (; j < pt->queues.nr_queues; j++) { 3212 if (pt->queues.queue_array[j].cpu == cpu) 3213 return pt->queues.queue_array[j].priv; 3214 } 3215 3216 return NULL; 3217 } 3218 3219 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid, 3220 u64 timestamp) 3221 { 3222 struct intel_pt_queue *ptq; 3223 int err; 3224 3225 if (!pt->sync_switch) 3226 return 1; 3227 3228 ptq = intel_pt_cpu_to_ptq(pt, cpu); 3229 if (!ptq || !ptq->sync_switch) 3230 return 1; 3231 3232 switch (ptq->switch_state) { 3233 case INTEL_PT_SS_NOT_TRACING: 3234 break; 3235 case INTEL_PT_SS_UNKNOWN: 3236 case INTEL_PT_SS_TRACING: 3237 ptq->next_tid = tid; 3238 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP; 3239 return 0; 3240 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT: 3241 if (!ptq->on_heap) { 3242 ptq->timestamp = perf_time_to_tsc(timestamp, 3243 &pt->tc); 3244 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr, 3245 ptq->timestamp); 3246 if (err) 3247 return err; 3248 ptq->on_heap = true; 3249 } 3250 ptq->switch_state = INTEL_PT_SS_TRACING; 3251 break; 3252 case INTEL_PT_SS_EXPECTING_SWITCH_IP: 3253 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu); 3254 break; 3255 default: 3256 break; 3257 } 3258 3259 ptq->next_tid = -1; 3260 3261 return 1; 3262 } 3263 3264 #ifdef HAVE_LIBTRACEEVENT 3265 static int intel_pt_process_switch(struct intel_pt *pt, 3266 struct perf_sample *sample) 3267 { 3268 pid_t tid; 3269 int cpu, ret; 3270 struct evsel *evsel = evlist__id2evsel(pt->session->evlist, sample->id); 3271 3272 if (evsel != pt->switch_evsel) 3273 return 0; 3274 3275 tid = evsel__intval(evsel, sample, "next_pid"); 3276 cpu = sample->cpu; 3277 3278 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n", 3279 cpu, tid, sample->time, perf_time_to_tsc(sample->time, 3280 &pt->tc)); 3281 3282 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time); 3283 if (ret <= 0) 3284 return ret; 3285 3286 return machine__set_current_tid(pt->machine, cpu, -1, tid); 3287 } 3288 #endif /* HAVE_LIBTRACEEVENT */ 3289 3290 static int intel_pt_context_switch_in(struct intel_pt *pt, 3291 struct perf_sample *sample) 3292 { 3293 pid_t pid = sample->pid; 3294 pid_t tid = sample->tid; 3295 int cpu = sample->cpu; 3296 3297 if (pt->sync_switch) { 3298 struct intel_pt_queue *ptq; 3299 3300 ptq = intel_pt_cpu_to_ptq(pt, cpu); 3301 if (ptq && ptq->sync_switch) { 3302 ptq->next_tid = -1; 3303 switch (ptq->switch_state) { 3304 case INTEL_PT_SS_NOT_TRACING: 3305 case INTEL_PT_SS_UNKNOWN: 3306 case INTEL_PT_SS_TRACING: 3307 break; 3308 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT: 3309 case INTEL_PT_SS_EXPECTING_SWITCH_IP: 3310 ptq->switch_state = INTEL_PT_SS_TRACING; 3311 break; 3312 default: 3313 break; 3314 } 3315 } 3316 } 3317 3318 /* 3319 * If the current tid has not been updated yet, ensure it is now that 3320 * a "switch in" event has occurred. 3321 */ 3322 if (machine__get_current_tid(pt->machine, cpu) == tid) 3323 return 0; 3324 3325 return machine__set_current_tid(pt->machine, cpu, pid, tid); 3326 } 3327 3328 static int intel_pt_guest_context_switch(struct intel_pt *pt, 3329 union perf_event *event, 3330 struct perf_sample *sample) 3331 { 3332 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; 3333 struct machines *machines = &pt->session->machines; 3334 struct machine *machine = machines__find(machines, sample->machine_pid); 3335 3336 pt->have_guest_sideband = true; 3337 3338 /* 3339 * sync_switch cannot handle guest machines at present, so just disable 3340 * it. 3341 */ 3342 pt->sync_switch_not_supported = true; 3343 if (pt->sync_switch) 3344 intel_pt_disable_sync_switch(pt); 3345 3346 if (out) 3347 return 0; 3348 3349 if (!machine) 3350 return -EINVAL; 3351 3352 return machine__set_current_tid(machine, sample->vcpu, sample->pid, sample->tid); 3353 } 3354 3355 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event, 3356 struct perf_sample *sample) 3357 { 3358 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; 3359 pid_t pid, tid; 3360 int cpu, ret; 3361 3362 if (perf_event__is_guest(event)) 3363 return intel_pt_guest_context_switch(pt, event, sample); 3364 3365 cpu = sample->cpu; 3366 3367 if (pt->have_sched_switch == 3) { 3368 if (!out) 3369 return intel_pt_context_switch_in(pt, sample); 3370 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) { 3371 pr_err("Expecting CPU-wide context switch event\n"); 3372 return -EINVAL; 3373 } 3374 pid = event->context_switch.next_prev_pid; 3375 tid = event->context_switch.next_prev_tid; 3376 } else { 3377 if (out) 3378 return 0; 3379 pid = sample->pid; 3380 tid = sample->tid; 3381 } 3382 3383 if (tid == -1) 3384 intel_pt_log("context_switch event has no tid\n"); 3385 3386 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time); 3387 if (ret <= 0) 3388 return ret; 3389 3390 return machine__set_current_tid(pt->machine, cpu, pid, tid); 3391 } 3392 3393 static int intel_pt_process_itrace_start(struct intel_pt *pt, 3394 union perf_event *event, 3395 struct perf_sample *sample) 3396 { 3397 if (!pt->per_cpu_mmaps) 3398 return 0; 3399 3400 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n", 3401 sample->cpu, event->itrace_start.pid, 3402 event->itrace_start.tid, sample->time, 3403 perf_time_to_tsc(sample->time, &pt->tc)); 3404 3405 return machine__set_current_tid(pt->machine, sample->cpu, 3406 event->itrace_start.pid, 3407 event->itrace_start.tid); 3408 } 3409 3410 static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt, 3411 union perf_event *event, 3412 struct perf_sample *sample) 3413 { 3414 u64 hw_id = event->aux_output_hw_id.hw_id; 3415 struct auxtrace_queue *queue; 3416 struct intel_pt_queue *ptq; 3417 struct evsel *evsel; 3418 3419 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session); 3420 evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id); 3421 if (!queue || !queue->priv || !evsel || hw_id > INTEL_PT_MAX_PEBS) { 3422 pr_err("Bad AUX output hardware ID\n"); 3423 return -EINVAL; 3424 } 3425 3426 ptq = queue->priv; 3427 3428 ptq->pebs[hw_id].evsel = evsel; 3429 ptq->pebs[hw_id].id = sample->id; 3430 3431 return 0; 3432 } 3433 3434 static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr, 3435 struct addr_location *al) 3436 { 3437 if (!al->map || addr < map__start(al->map) || addr >= map__end(al->map)) { 3438 if (!thread__find_map(thread, cpumode, addr, al)) 3439 return -1; 3440 } 3441 3442 return 0; 3443 } 3444 3445 /* Invalidate all instruction cache entries that overlap the text poke */ 3446 static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event) 3447 { 3448 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 3449 u64 addr = event->text_poke.addr + event->text_poke.new_len - 1; 3450 /* Assume text poke begins in a basic block no more than 4096 bytes */ 3451 int cnt = 4096 + event->text_poke.new_len; 3452 struct thread *thread = pt->unknown_thread; 3453 struct addr_location al; 3454 struct machine *machine = pt->machine; 3455 struct intel_pt_cache_entry *e; 3456 u64 offset; 3457 int ret = 0; 3458 3459 addr_location__init(&al); 3460 if (!event->text_poke.new_len) 3461 goto out; 3462 3463 for (; cnt; cnt--, addr--) { 3464 struct dso *dso; 3465 3466 if (intel_pt_find_map(thread, cpumode, addr, &al)) { 3467 if (addr < event->text_poke.addr) 3468 goto out; 3469 continue; 3470 } 3471 3472 dso = map__dso(al.map); 3473 if (!dso || !dso__auxtrace_cache(dso)) 3474 continue; 3475 3476 offset = map__map_ip(al.map, addr); 3477 3478 e = intel_pt_cache_lookup(dso, machine, offset); 3479 if (!e) 3480 continue; 3481 3482 if (addr + e->byte_cnt + e->length <= event->text_poke.addr) { 3483 /* 3484 * No overlap. Working backwards there cannot be another 3485 * basic block that overlaps the text poke if there is a 3486 * branch instruction before the text poke address. 3487 */ 3488 if (e->branch != INTEL_PT_BR_NO_BRANCH) 3489 goto out; 3490 } else { 3491 intel_pt_cache_invalidate(dso, machine, offset); 3492 intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n", 3493 dso__long_name(dso), addr); 3494 } 3495 } 3496 out: 3497 addr_location__exit(&al); 3498 return ret; 3499 } 3500 3501 static int intel_pt_process_event(struct perf_session *session, 3502 union perf_event *event, 3503 struct perf_sample *sample, 3504 const struct perf_tool *tool) 3505 { 3506 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3507 auxtrace); 3508 u64 timestamp; 3509 int err = 0; 3510 3511 if (dump_trace) 3512 return 0; 3513 3514 if (!tool->ordered_events) { 3515 pr_err("Intel Processor Trace requires ordered events\n"); 3516 return -EINVAL; 3517 } 3518 3519 if (sample->time && sample->time != (u64)-1) 3520 timestamp = perf_time_to_tsc(sample->time, &pt->tc); 3521 else 3522 timestamp = 0; 3523 3524 if (timestamp || pt->timeless_decoding) { 3525 err = intel_pt_update_queues(pt); 3526 if (err) 3527 return err; 3528 } 3529 3530 if (pt->timeless_decoding) { 3531 if (pt->sampling_mode) { 3532 if (sample->aux_sample.size) 3533 err = intel_pt_process_timeless_sample(pt, 3534 sample); 3535 } else if (event->header.type == PERF_RECORD_EXIT) { 3536 err = intel_pt_process_timeless_queues(pt, 3537 event->fork.tid, 3538 sample->time); 3539 } 3540 } else if (timestamp) { 3541 if (!pt->first_timestamp) 3542 intel_pt_first_timestamp(pt, timestamp); 3543 err = intel_pt_process_queues(pt, timestamp); 3544 } 3545 if (err) 3546 return err; 3547 3548 if (event->header.type == PERF_RECORD_SAMPLE) { 3549 if (pt->synth_opts.add_callchain && !sample->callchain) 3550 intel_pt_add_callchain(pt, sample); 3551 if (pt->synth_opts.add_last_branch && !sample->branch_stack) 3552 intel_pt_add_br_stack(pt, sample); 3553 } 3554 3555 if (event->header.type == PERF_RECORD_AUX && 3556 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) && 3557 pt->synth_opts.errors) { 3558 err = intel_pt_lost(pt, sample); 3559 if (err) 3560 return err; 3561 } 3562 3563 #ifdef HAVE_LIBTRACEEVENT 3564 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE) 3565 err = intel_pt_process_switch(pt, sample); 3566 else 3567 #endif 3568 if (event->header.type == PERF_RECORD_ITRACE_START) 3569 err = intel_pt_process_itrace_start(pt, event, sample); 3570 else if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) 3571 err = intel_pt_process_aux_output_hw_id(pt, event, sample); 3572 else if (event->header.type == PERF_RECORD_SWITCH || 3573 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) 3574 err = intel_pt_context_switch(pt, event, sample); 3575 3576 if (!err && event->header.type == PERF_RECORD_TEXT_POKE) 3577 err = intel_pt_text_poke(pt, event); 3578 3579 if (intel_pt_enable_logging && intel_pt_log_events(pt, sample->time)) { 3580 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ", 3581 event->header.type, sample->cpu, sample->time, timestamp); 3582 intel_pt_log_event(event); 3583 } 3584 3585 return err; 3586 } 3587 3588 static int intel_pt_flush(struct perf_session *session, const struct perf_tool *tool) 3589 { 3590 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3591 auxtrace); 3592 int ret; 3593 3594 if (dump_trace) 3595 return 0; 3596 3597 if (!tool->ordered_events) 3598 return -EINVAL; 3599 3600 ret = intel_pt_update_queues(pt); 3601 if (ret < 0) 3602 return ret; 3603 3604 if (pt->timeless_decoding) 3605 return intel_pt_process_timeless_queues(pt, -1, 3606 MAX_TIMESTAMP - 1); 3607 3608 return intel_pt_process_queues(pt, MAX_TIMESTAMP); 3609 } 3610 3611 static void intel_pt_free_events(struct perf_session *session) 3612 { 3613 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3614 auxtrace); 3615 struct auxtrace_queues *queues = &pt->queues; 3616 unsigned int i; 3617 3618 for (i = 0; i < queues->nr_queues; i++) { 3619 intel_pt_free_queue(queues->queue_array[i].priv); 3620 queues->queue_array[i].priv = NULL; 3621 } 3622 intel_pt_log_disable(); 3623 auxtrace_queues__free(queues); 3624 } 3625 3626 static void intel_pt_free(struct perf_session *session) 3627 { 3628 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3629 auxtrace); 3630 3631 auxtrace_heap__free(&pt->heap); 3632 intel_pt_free_events(session); 3633 session->auxtrace = NULL; 3634 intel_pt_free_vmcs_info(pt); 3635 thread__put(pt->unknown_thread); 3636 addr_filters__exit(&pt->filts); 3637 zfree(&pt->chain); 3638 zfree(&pt->filter); 3639 zfree(&pt->time_ranges); 3640 zfree(&pt->br_stack); 3641 free(pt); 3642 } 3643 3644 static bool intel_pt_evsel_is_auxtrace(struct perf_session *session, 3645 struct evsel *evsel) 3646 { 3647 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3648 auxtrace); 3649 3650 return evsel->core.attr.type == pt->pmu_type; 3651 } 3652 3653 static int intel_pt_process_auxtrace_event(struct perf_session *session, 3654 union perf_event *event, 3655 const struct perf_tool *tool __maybe_unused) 3656 { 3657 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3658 auxtrace); 3659 3660 if (!pt->data_queued) { 3661 struct auxtrace_buffer *buffer; 3662 off_t data_offset; 3663 int fd = perf_data__fd(session->data); 3664 int err; 3665 3666 if (perf_data__is_pipe(session->data)) { 3667 data_offset = 0; 3668 } else { 3669 data_offset = lseek(fd, 0, SEEK_CUR); 3670 if (data_offset == -1) 3671 return -errno; 3672 } 3673 3674 err = auxtrace_queues__add_event(&pt->queues, session, event, 3675 data_offset, &buffer); 3676 if (err) 3677 return err; 3678 3679 /* Dump here now we have copied a piped trace out of the pipe */ 3680 if (dump_trace) { 3681 if (auxtrace_buffer__get_data(buffer, fd)) { 3682 intel_pt_dump_event(pt, buffer->data, 3683 buffer->size); 3684 auxtrace_buffer__put_data(buffer); 3685 } 3686 } 3687 } 3688 3689 return 0; 3690 } 3691 3692 static int intel_pt_queue_data(struct perf_session *session, 3693 struct perf_sample *sample, 3694 union perf_event *event, u64 data_offset) 3695 { 3696 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt, 3697 auxtrace); 3698 u64 timestamp; 3699 3700 if (event) { 3701 return auxtrace_queues__add_event(&pt->queues, session, event, 3702 data_offset, NULL); 3703 } 3704 3705 if (sample->time && sample->time != (u64)-1) 3706 timestamp = perf_time_to_tsc(sample->time, &pt->tc); 3707 else 3708 timestamp = 0; 3709 3710 return auxtrace_queues__add_sample(&pt->queues, session, sample, 3711 data_offset, timestamp); 3712 } 3713 3714 static int intel_pt_synth_event(struct perf_session *session, const char *name, 3715 struct perf_event_attr *attr, u64 id) 3716 { 3717 int err; 3718 3719 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n", 3720 name, id, (u64)attr->sample_type); 3721 3722 err = perf_session__deliver_synth_attr_event(session, attr, id); 3723 if (err) 3724 pr_err("%s: failed to synthesize '%s' event type\n", 3725 __func__, name); 3726 3727 return err; 3728 } 3729 3730 static void intel_pt_set_event_name(struct evlist *evlist, u64 id, 3731 const char *name) 3732 { 3733 struct evsel *evsel; 3734 3735 evlist__for_each_entry(evlist, evsel) { 3736 if (evsel->core.id && evsel->core.id[0] == id) { 3737 if (evsel->name) 3738 zfree(&evsel->name); 3739 evsel->name = strdup(name); 3740 break; 3741 } 3742 } 3743 } 3744 3745 static struct evsel *intel_pt_evsel(struct intel_pt *pt, 3746 struct evlist *evlist) 3747 { 3748 struct evsel *evsel; 3749 3750 evlist__for_each_entry(evlist, evsel) { 3751 if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids) 3752 return evsel; 3753 } 3754 3755 return NULL; 3756 } 3757 3758 static int intel_pt_synth_events(struct intel_pt *pt, 3759 struct perf_session *session) 3760 { 3761 struct evlist *evlist = session->evlist; 3762 struct evsel *evsel = intel_pt_evsel(pt, evlist); 3763 struct perf_event_attr attr; 3764 u64 id; 3765 int err; 3766 3767 if (!evsel) { 3768 pr_debug("There are no selected events with Intel Processor Trace data\n"); 3769 return 0; 3770 } 3771 3772 memset(&attr, 0, sizeof(struct perf_event_attr)); 3773 attr.size = sizeof(struct perf_event_attr); 3774 attr.type = PERF_TYPE_HARDWARE; 3775 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK; 3776 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | 3777 PERF_SAMPLE_PERIOD; 3778 if (pt->timeless_decoding) 3779 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME; 3780 else 3781 attr.sample_type |= PERF_SAMPLE_TIME; 3782 if (!pt->per_cpu_mmaps) 3783 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU; 3784 attr.exclude_user = evsel->core.attr.exclude_user; 3785 attr.exclude_kernel = evsel->core.attr.exclude_kernel; 3786 attr.exclude_hv = evsel->core.attr.exclude_hv; 3787 attr.exclude_host = evsel->core.attr.exclude_host; 3788 attr.exclude_guest = evsel->core.attr.exclude_guest; 3789 attr.sample_id_all = evsel->core.attr.sample_id_all; 3790 attr.read_format = evsel->core.attr.read_format; 3791 3792 id = evsel->core.id[0] + 1000000000; 3793 if (!id) 3794 id = 1; 3795 3796 if (pt->synth_opts.branches) { 3797 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; 3798 attr.sample_period = 1; 3799 attr.sample_type |= PERF_SAMPLE_ADDR; 3800 err = intel_pt_synth_event(session, "branches", &attr, id); 3801 if (err) 3802 return err; 3803 pt->sample_branches = true; 3804 pt->branches_sample_type = attr.sample_type; 3805 pt->branches_id = id; 3806 id += 1; 3807 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR; 3808 } 3809 3810 if (pt->synth_opts.callchain) 3811 attr.sample_type |= PERF_SAMPLE_CALLCHAIN; 3812 if (pt->synth_opts.last_branch) { 3813 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK; 3814 /* 3815 * We don't use the hardware index, but the sample generation 3816 * code uses the new format branch_stack with this field, 3817 * so the event attributes must indicate that it's present. 3818 */ 3819 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX; 3820 } 3821 3822 if (pt->synth_opts.instructions) { 3823 attr.config = PERF_COUNT_HW_INSTRUCTIONS; 3824 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS) 3825 attr.sample_period = 3826 intel_pt_ns_to_ticks(pt, pt->synth_opts.period); 3827 else 3828 attr.sample_period = pt->synth_opts.period; 3829 err = intel_pt_synth_event(session, "instructions", &attr, id); 3830 if (err) 3831 return err; 3832 pt->sample_instructions = true; 3833 pt->instructions_sample_type = attr.sample_type; 3834 pt->instructions_id = id; 3835 id += 1; 3836 } 3837 3838 if (pt->synth_opts.cycles) { 3839 attr.config = PERF_COUNT_HW_CPU_CYCLES; 3840 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS) 3841 attr.sample_period = 3842 intel_pt_ns_to_ticks(pt, pt->synth_opts.period); 3843 else 3844 attr.sample_period = pt->synth_opts.period; 3845 err = intel_pt_synth_event(session, "cycles", &attr, id); 3846 if (err) 3847 return err; 3848 pt->sample_cycles = true; 3849 pt->cycles_sample_type = attr.sample_type; 3850 pt->cycles_id = id; 3851 id += 1; 3852 } 3853 3854 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD; 3855 attr.sample_period = 1; 3856 3857 if (pt->synth_opts.transactions) { 3858 attr.config = PERF_COUNT_HW_INSTRUCTIONS; 3859 err = intel_pt_synth_event(session, "transactions", &attr, id); 3860 if (err) 3861 return err; 3862 pt->sample_transactions = true; 3863 pt->transactions_sample_type = attr.sample_type; 3864 pt->transactions_id = id; 3865 intel_pt_set_event_name(evlist, id, "transactions"); 3866 id += 1; 3867 } 3868 3869 attr.type = PERF_TYPE_SYNTH; 3870 attr.sample_type |= PERF_SAMPLE_RAW; 3871 3872 if (pt->synth_opts.ptwrites) { 3873 attr.config = PERF_SYNTH_INTEL_PTWRITE; 3874 err = intel_pt_synth_event(session, "ptwrite", &attr, id); 3875 if (err) 3876 return err; 3877 pt->sample_ptwrites = true; 3878 pt->ptwrites_sample_type = attr.sample_type; 3879 pt->ptwrites_id = id; 3880 intel_pt_set_event_name(evlist, id, "ptwrite"); 3881 id += 1; 3882 } 3883 3884 if (pt->synth_opts.pwr_events) { 3885 pt->sample_pwr_events = true; 3886 pt->pwr_events_sample_type = attr.sample_type; 3887 3888 attr.config = PERF_SYNTH_INTEL_CBR; 3889 err = intel_pt_synth_event(session, "cbr", &attr, id); 3890 if (err) 3891 return err; 3892 pt->cbr_id = id; 3893 intel_pt_set_event_name(evlist, id, "cbr"); 3894 id += 1; 3895 3896 attr.config = PERF_SYNTH_INTEL_PSB; 3897 err = intel_pt_synth_event(session, "psb", &attr, id); 3898 if (err) 3899 return err; 3900 pt->psb_id = id; 3901 intel_pt_set_event_name(evlist, id, "psb"); 3902 id += 1; 3903 } 3904 3905 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & INTEL_PT_CFG_PWR_EVT_EN)) { 3906 attr.config = PERF_SYNTH_INTEL_MWAIT; 3907 err = intel_pt_synth_event(session, "mwait", &attr, id); 3908 if (err) 3909 return err; 3910 pt->mwait_id = id; 3911 intel_pt_set_event_name(evlist, id, "mwait"); 3912 id += 1; 3913 3914 attr.config = PERF_SYNTH_INTEL_PWRE; 3915 err = intel_pt_synth_event(session, "pwre", &attr, id); 3916 if (err) 3917 return err; 3918 pt->pwre_id = id; 3919 intel_pt_set_event_name(evlist, id, "pwre"); 3920 id += 1; 3921 3922 attr.config = PERF_SYNTH_INTEL_EXSTOP; 3923 err = intel_pt_synth_event(session, "exstop", &attr, id); 3924 if (err) 3925 return err; 3926 pt->exstop_id = id; 3927 intel_pt_set_event_name(evlist, id, "exstop"); 3928 id += 1; 3929 3930 attr.config = PERF_SYNTH_INTEL_PWRX; 3931 err = intel_pt_synth_event(session, "pwrx", &attr, id); 3932 if (err) 3933 return err; 3934 pt->pwrx_id = id; 3935 intel_pt_set_event_name(evlist, id, "pwrx"); 3936 id += 1; 3937 } 3938 3939 if (pt->synth_opts.intr_events && (evsel->core.attr.config & INTEL_PT_CFG_EVT_EN)) { 3940 attr.config = PERF_SYNTH_INTEL_EVT; 3941 err = intel_pt_synth_event(session, "evt", &attr, id); 3942 if (err) 3943 return err; 3944 pt->evt_sample_type = attr.sample_type; 3945 pt->evt_id = id; 3946 intel_pt_set_event_name(evlist, id, "evt"); 3947 id += 1; 3948 } 3949 3950 if (pt->synth_opts.intr_events && pt->cap_event_trace) { 3951 attr.config = PERF_SYNTH_INTEL_IFLAG_CHG; 3952 err = intel_pt_synth_event(session, "iflag", &attr, id); 3953 if (err) 3954 return err; 3955 pt->iflag_chg_sample_type = attr.sample_type; 3956 pt->iflag_chg_id = id; 3957 intel_pt_set_event_name(evlist, id, "iflag"); 3958 id += 1; 3959 } 3960 3961 return 0; 3962 } 3963 3964 static void intel_pt_setup_pebs_events(struct intel_pt *pt) 3965 { 3966 struct evsel *evsel; 3967 3968 if (!pt->synth_opts.other_events) 3969 return; 3970 3971 evlist__for_each_entry(pt->session->evlist, evsel) { 3972 if (evsel->core.attr.aux_output && evsel->core.id) { 3973 if (pt->single_pebs) { 3974 pt->single_pebs = false; 3975 return; 3976 } 3977 pt->single_pebs = true; 3978 pt->sample_pebs = true; 3979 pt->pebs_evsel = evsel; 3980 } 3981 } 3982 } 3983 3984 static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist) 3985 { 3986 struct evsel *evsel; 3987 3988 evlist__for_each_entry_reverse(evlist, evsel) { 3989 const char *name = evsel__name(evsel); 3990 3991 if (!strcmp(name, "sched:sched_switch")) 3992 return evsel; 3993 } 3994 3995 return NULL; 3996 } 3997 3998 static bool intel_pt_find_switch(struct evlist *evlist) 3999 { 4000 struct evsel *evsel; 4001 4002 evlist__for_each_entry(evlist, evsel) { 4003 if (evsel->core.attr.context_switch) 4004 return true; 4005 } 4006 4007 return false; 4008 } 4009 4010 static int intel_pt_perf_config(const char *var, const char *value, void *data) 4011 { 4012 struct intel_pt *pt = data; 4013 4014 if (!strcmp(var, "intel-pt.mispred-all")) 4015 pt->mispred_all = perf_config_bool(var, value); 4016 4017 if (!strcmp(var, "intel-pt.max-loops")) 4018 perf_config_int(&pt->max_loops, var, value); 4019 4020 return 0; 4021 } 4022 4023 /* Find least TSC which converts to ns or later */ 4024 static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt) 4025 { 4026 u64 tsc, tm; 4027 4028 tsc = perf_time_to_tsc(ns, &pt->tc); 4029 4030 while (1) { 4031 tm = tsc_to_perf_time(tsc, &pt->tc); 4032 if (tm < ns) 4033 break; 4034 tsc -= 1; 4035 } 4036 4037 while (tm < ns) 4038 tm = tsc_to_perf_time(++tsc, &pt->tc); 4039 4040 return tsc; 4041 } 4042 4043 /* Find greatest TSC which converts to ns or earlier */ 4044 static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt) 4045 { 4046 u64 tsc, tm; 4047 4048 tsc = perf_time_to_tsc(ns, &pt->tc); 4049 4050 while (1) { 4051 tm = tsc_to_perf_time(tsc, &pt->tc); 4052 if (tm > ns) 4053 break; 4054 tsc += 1; 4055 } 4056 4057 while (tm > ns) 4058 tm = tsc_to_perf_time(--tsc, &pt->tc); 4059 4060 return tsc; 4061 } 4062 4063 static int intel_pt_setup_time_ranges(struct intel_pt *pt, 4064 struct itrace_synth_opts *opts) 4065 { 4066 struct perf_time_interval *p = opts->ptime_range; 4067 int n = opts->range_num; 4068 int i; 4069 4070 if (!n || !p || pt->timeless_decoding) 4071 return 0; 4072 4073 pt->time_ranges = calloc(n, sizeof(struct range)); 4074 if (!pt->time_ranges) 4075 return -ENOMEM; 4076 4077 pt->range_cnt = n; 4078 4079 intel_pt_log("%s: %u range(s)\n", __func__, n); 4080 4081 for (i = 0; i < n; i++) { 4082 struct range *r = &pt->time_ranges[i]; 4083 u64 ts = p[i].start; 4084 u64 te = p[i].end; 4085 4086 /* 4087 * Take care to ensure the TSC range matches the perf-time range 4088 * when converted back to perf-time. 4089 */ 4090 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0; 4091 r->end = te ? intel_pt_tsc_end(te, pt) : 0; 4092 4093 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n", 4094 i, ts, te); 4095 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n", 4096 i, r->start, r->end); 4097 } 4098 4099 return 0; 4100 } 4101 4102 static int intel_pt_parse_vm_tm_corr_arg(struct intel_pt *pt, char **args) 4103 { 4104 struct intel_pt_vmcs_info *vmcs_info; 4105 u64 tsc_offset, vmcs; 4106 char *p = *args; 4107 4108 errno = 0; 4109 4110 p = skip_spaces(p); 4111 if (!*p) 4112 return 1; 4113 4114 tsc_offset = strtoull(p, &p, 0); 4115 if (errno) 4116 return -errno; 4117 p = skip_spaces(p); 4118 if (*p != ':') { 4119 pt->dflt_tsc_offset = tsc_offset; 4120 *args = p; 4121 return 0; 4122 } 4123 p += 1; 4124 while (1) { 4125 vmcs = strtoull(p, &p, 0); 4126 if (errno) 4127 return -errno; 4128 if (!vmcs) 4129 return -EINVAL; 4130 vmcs_info = intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, tsc_offset); 4131 if (!vmcs_info) 4132 return -ENOMEM; 4133 p = skip_spaces(p); 4134 if (*p != ',') 4135 break; 4136 p += 1; 4137 } 4138 *args = p; 4139 return 0; 4140 } 4141 4142 static int intel_pt_parse_vm_tm_corr_args(struct intel_pt *pt) 4143 { 4144 char *args = pt->synth_opts.vm_tm_corr_args; 4145 int ret; 4146 4147 if (!args) 4148 return 0; 4149 4150 do { 4151 ret = intel_pt_parse_vm_tm_corr_arg(pt, &args); 4152 } while (!ret); 4153 4154 if (ret < 0) { 4155 pr_err("Failed to parse VM Time Correlation options\n"); 4156 return ret; 4157 } 4158 4159 return 0; 4160 } 4161 4162 static const char * const intel_pt_info_fmts[] = { 4163 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n", 4164 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n", 4165 [INTEL_PT_TIME_MULT] = " Time Multiplier %"PRIu64"\n", 4166 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n", 4167 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n", 4168 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n", 4169 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n", 4170 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n", 4171 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n", 4172 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n", 4173 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n", 4174 [INTEL_PT_MTC_FREQ_BITS] = " MTC freq bits %#"PRIx64"\n", 4175 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n", 4176 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n", 4177 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n", 4178 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n", 4179 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n", 4180 }; 4181 4182 static void intel_pt_print_info(__u64 *arr, int start, int finish) 4183 { 4184 int i; 4185 4186 if (!dump_trace) 4187 return; 4188 4189 for (i = start; i <= finish; i++) { 4190 const char *fmt = intel_pt_info_fmts[i]; 4191 4192 if (fmt) 4193 fprintf(stdout, fmt, arr[i]); 4194 } 4195 } 4196 4197 static void intel_pt_print_info_str(const char *name, const char *str) 4198 { 4199 if (!dump_trace) 4200 return; 4201 4202 fprintf(stdout, " %-20s%s\n", name, str ? str : ""); 4203 } 4204 4205 static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos) 4206 { 4207 return auxtrace_info->header.size >= 4208 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1)); 4209 } 4210 4211 int intel_pt_process_auxtrace_info(union perf_event *event, 4212 struct perf_session *session) 4213 { 4214 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info; 4215 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS; 4216 struct intel_pt *pt; 4217 void *info_end; 4218 __u64 *info; 4219 int err; 4220 4221 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) + 4222 min_sz) 4223 return -EINVAL; 4224 4225 pt = zalloc(sizeof(struct intel_pt)); 4226 if (!pt) 4227 return -ENOMEM; 4228 4229 pt->vmcs_info = RB_ROOT; 4230 4231 addr_filters__init(&pt->filts); 4232 4233 err = perf_config(intel_pt_perf_config, pt); 4234 if (err) 4235 goto err_free; 4236 4237 err = auxtrace_queues__init(&pt->queues); 4238 if (err) 4239 goto err_free; 4240 4241 if (session->itrace_synth_opts->set) { 4242 pt->synth_opts = *session->itrace_synth_opts; 4243 } else { 4244 struct itrace_synth_opts *opts = session->itrace_synth_opts; 4245 4246 itrace_synth_opts__set_default(&pt->synth_opts, opts->default_no_sample); 4247 if (!opts->default_no_sample && !opts->inject) { 4248 pt->synth_opts.branches = false; 4249 pt->synth_opts.callchain = true; 4250 pt->synth_opts.add_callchain = true; 4251 } 4252 pt->synth_opts.thread_stack = opts->thread_stack; 4253 } 4254 4255 if (!(pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT)) 4256 intel_pt_log_set_name(INTEL_PT_PMU_NAME); 4257 4258 pt->session = session; 4259 pt->machine = &session->machines.host; /* No kvm support */ 4260 pt->auxtrace_type = auxtrace_info->type; 4261 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE]; 4262 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT]; 4263 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT]; 4264 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO]; 4265 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO]; 4266 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT]; 4267 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT]; 4268 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH]; 4269 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE]; 4270 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS]; 4271 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE, 4272 INTEL_PT_PER_CPU_MMAPS); 4273 4274 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) { 4275 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT]; 4276 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS]; 4277 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N]; 4278 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D]; 4279 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT]; 4280 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT, 4281 INTEL_PT_CYC_BIT); 4282 } 4283 4284 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) { 4285 pt->max_non_turbo_ratio = 4286 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO]; 4287 intel_pt_print_info(&auxtrace_info->priv[0], 4288 INTEL_PT_MAX_NONTURBO_RATIO, 4289 INTEL_PT_MAX_NONTURBO_RATIO); 4290 } 4291 4292 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1; 4293 info_end = (void *)auxtrace_info + auxtrace_info->header.size; 4294 4295 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) { 4296 size_t len; 4297 4298 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN]; 4299 intel_pt_print_info(&auxtrace_info->priv[0], 4300 INTEL_PT_FILTER_STR_LEN, 4301 INTEL_PT_FILTER_STR_LEN); 4302 if (len) { 4303 const char *filter = (const char *)info; 4304 4305 len = roundup(len + 1, 8); 4306 info += len >> 3; 4307 if ((void *)info > info_end) { 4308 pr_err("%s: bad filter string length\n", __func__); 4309 err = -EINVAL; 4310 goto err_free_queues; 4311 } 4312 pt->filter = memdup(filter, len); 4313 if (!pt->filter) { 4314 err = -ENOMEM; 4315 goto err_free_queues; 4316 } 4317 if (session->header.needs_swap) 4318 mem_bswap_64(pt->filter, len); 4319 if (pt->filter[len - 1]) { 4320 pr_err("%s: filter string not null terminated\n", __func__); 4321 err = -EINVAL; 4322 goto err_free_queues; 4323 } 4324 err = addr_filters__parse_bare_filter(&pt->filts, 4325 filter); 4326 if (err) 4327 goto err_free_queues; 4328 } 4329 intel_pt_print_info_str("Filter string", pt->filter); 4330 } 4331 4332 if ((void *)info < info_end) { 4333 pt->cap_event_trace = *info++; 4334 if (dump_trace) 4335 fprintf(stdout, " Cap Event Trace %d\n", 4336 pt->cap_event_trace); 4337 } 4338 4339 pt->timeless_decoding = intel_pt_timeless_decoding(pt); 4340 if (pt->timeless_decoding && !pt->tc.time_mult) 4341 pt->tc.time_mult = 1; 4342 pt->have_tsc = intel_pt_have_tsc(pt); 4343 pt->sampling_mode = intel_pt_sampling_mode(pt); 4344 pt->est_tsc = !pt->timeless_decoding; 4345 4346 if (pt->synth_opts.vm_time_correlation) { 4347 if (pt->timeless_decoding) { 4348 pr_err("Intel PT has no time information for VM Time Correlation\n"); 4349 err = -EINVAL; 4350 goto err_free_queues; 4351 } 4352 if (session->itrace_synth_opts->ptime_range) { 4353 pr_err("Time ranges cannot be specified with VM Time Correlation\n"); 4354 err = -EINVAL; 4355 goto err_free_queues; 4356 } 4357 /* Currently TSC Offset is calculated using MTC packets */ 4358 if (!intel_pt_have_mtc(pt)) { 4359 pr_err("MTC packets must have been enabled for VM Time Correlation\n"); 4360 err = -EINVAL; 4361 goto err_free_queues; 4362 } 4363 err = intel_pt_parse_vm_tm_corr_args(pt); 4364 if (err) 4365 goto err_free_queues; 4366 } 4367 4368 pt->unknown_thread = thread__new(999999999, 999999999); 4369 if (!pt->unknown_thread) { 4370 err = -ENOMEM; 4371 goto err_free_queues; 4372 } 4373 4374 err = thread__set_comm(pt->unknown_thread, "unknown", 0); 4375 if (err) 4376 goto err_delete_thread; 4377 if (thread__init_maps(pt->unknown_thread, pt->machine)) { 4378 err = -ENOMEM; 4379 goto err_delete_thread; 4380 } 4381 4382 pt->auxtrace.process_event = intel_pt_process_event; 4383 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event; 4384 pt->auxtrace.queue_data = intel_pt_queue_data; 4385 pt->auxtrace.dump_auxtrace_sample = intel_pt_dump_sample; 4386 pt->auxtrace.flush_events = intel_pt_flush; 4387 pt->auxtrace.free_events = intel_pt_free_events; 4388 pt->auxtrace.free = intel_pt_free; 4389 pt->auxtrace.evsel_is_auxtrace = intel_pt_evsel_is_auxtrace; 4390 session->auxtrace = &pt->auxtrace; 4391 4392 if (dump_trace) 4393 return 0; 4394 4395 if (pt->have_sched_switch == 1) { 4396 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist); 4397 if (!pt->switch_evsel) { 4398 pr_err("%s: missing sched_switch event\n", __func__); 4399 err = -EINVAL; 4400 goto err_delete_thread; 4401 } 4402 } else if (pt->have_sched_switch == 2 && 4403 !intel_pt_find_switch(session->evlist)) { 4404 pr_err("%s: missing context_switch attribute flag\n", __func__); 4405 err = -EINVAL; 4406 goto err_delete_thread; 4407 } 4408 4409 if (pt->synth_opts.log) { 4410 bool log_on_error = pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR; 4411 unsigned int log_on_error_size = pt->synth_opts.log_on_error_size; 4412 4413 intel_pt_log_enable(log_on_error, log_on_error_size); 4414 } 4415 4416 /* Maximum non-turbo ratio is TSC freq / 100 MHz */ 4417 if (pt->tc.time_mult) { 4418 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000); 4419 4420 if (!pt->max_non_turbo_ratio) 4421 pt->max_non_turbo_ratio = 4422 (tsc_freq + 50000000) / 100000000; 4423 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq); 4424 intel_pt_log("Maximum non-turbo ratio %u\n", 4425 pt->max_non_turbo_ratio); 4426 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000; 4427 } 4428 4429 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts); 4430 if (err) 4431 goto err_delete_thread; 4432 4433 if (pt->synth_opts.calls) 4434 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | 4435 PERF_IP_FLAG_TRACE_END; 4436 if (pt->synth_opts.returns) 4437 pt->branches_filter |= PERF_IP_FLAG_RETURN | 4438 PERF_IP_FLAG_TRACE_BEGIN; 4439 4440 if ((pt->synth_opts.callchain || pt->synth_opts.add_callchain) && 4441 !symbol_conf.use_callchain) { 4442 symbol_conf.use_callchain = true; 4443 if (callchain_register_param(&callchain_param) < 0) { 4444 symbol_conf.use_callchain = false; 4445 pt->synth_opts.callchain = false; 4446 pt->synth_opts.add_callchain = false; 4447 } 4448 } 4449 4450 if (pt->synth_opts.add_callchain) { 4451 err = intel_pt_callchain_init(pt); 4452 if (err) 4453 goto err_delete_thread; 4454 } 4455 4456 if (pt->synth_opts.last_branch || pt->synth_opts.add_last_branch) { 4457 pt->br_stack_sz = pt->synth_opts.last_branch_sz; 4458 pt->br_stack_sz_plus = pt->br_stack_sz; 4459 } 4460 4461 if (pt->synth_opts.add_last_branch) { 4462 err = intel_pt_br_stack_init(pt); 4463 if (err) 4464 goto err_delete_thread; 4465 /* 4466 * Additional branch stack size to cater for tracing from the 4467 * actual sample ip to where the sample time is recorded. 4468 * Measured at about 200 branches, but generously set to 1024. 4469 * If kernel space is not being traced, then add just 1 for the 4470 * branch to kernel space. 4471 */ 4472 if (intel_pt_tracing_kernel(pt)) 4473 pt->br_stack_sz_plus += 1024; 4474 else 4475 pt->br_stack_sz_plus += 1; 4476 } 4477 4478 pt->use_thread_stack = pt->synth_opts.callchain || 4479 pt->synth_opts.add_callchain || 4480 pt->synth_opts.thread_stack || 4481 pt->synth_opts.last_branch || 4482 pt->synth_opts.add_last_branch; 4483 4484 pt->callstack = pt->synth_opts.callchain || 4485 pt->synth_opts.add_callchain || 4486 pt->synth_opts.thread_stack; 4487 4488 err = intel_pt_synth_events(pt, session); 4489 if (err) 4490 goto err_delete_thread; 4491 4492 intel_pt_setup_pebs_events(pt); 4493 4494 if (perf_data__is_pipe(session->data)) { 4495 pr_warning("WARNING: Intel PT with pipe mode is not recommended.\n" 4496 " The output cannot relied upon. In particular,\n" 4497 " timestamps and the order of events may be incorrect.\n"); 4498 } 4499 4500 if (pt->sampling_mode || list_empty(&session->auxtrace_index)) 4501 err = auxtrace_queue_data(session, true, true); 4502 else 4503 err = auxtrace_queues__process_index(&pt->queues, session); 4504 if (err) 4505 goto err_delete_thread; 4506 4507 if (pt->queues.populated) 4508 pt->data_queued = true; 4509 4510 if (pt->timeless_decoding) 4511 pr_debug2("Intel PT decoding without timestamps\n"); 4512 4513 return 0; 4514 4515 err_delete_thread: 4516 zfree(&pt->chain); 4517 thread__zput(pt->unknown_thread); 4518 err_free_queues: 4519 intel_pt_log_disable(); 4520 auxtrace_queues__free(&pt->queues); 4521 session->auxtrace = NULL; 4522 err_free: 4523 addr_filters__exit(&pt->filts); 4524 zfree(&pt->filter); 4525 zfree(&pt->time_ranges); 4526 free(pt); 4527 return err; 4528 } 4529