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