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