blob: 556a893508daeb9aca14806ba9779de982208771 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Adrian Hunter90e457f2015-07-17 19:33:41 +03002/*
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
Adrian Hunter90e457f2015-07-17 19:33:41 +03005 */
6
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03007#include <inttypes.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +03008#include <stdio.h>
9#include <stdbool.h>
10#include <errno.h>
11#include <linux/kernel.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030012#include <linux/string.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +030013#include <linux/types.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030014#include <linux/zalloc.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +030015
Adrian Hunter90e457f2015-07-17 19:33:41 +030016#include "session.h"
17#include "machine.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030018#include "memswap.h"
Adrian Hunterf14445e2015-09-25 16:15:45 +030019#include "sort.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030020#include "tool.h"
21#include "event.h"
22#include "evlist.h"
23#include "evsel.h"
24#include "map.h"
25#include "color.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030026#include "thread.h"
27#include "thread-stack.h"
28#include "symbol.h"
29#include "callchain.h"
30#include "dso.h"
31#include "debug.h"
32#include "auxtrace.h"
33#include "tsc.h"
34#include "intel-pt.h"
Taeung Song41840d22016-06-23 17:55:17 +090035#include "config.h"
Arnaldo Carvalho de Melo40c7d242020-05-05 11:49:08 -030036#include "util/perf_api_probe.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030037#include "util/synthetic-events.h"
Adrian Hunter2c47db92019-06-04 16:00:09 +030038#include "time-utils.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030039
Adrian Hunter9e9a6182019-06-10 10:27:59 +030040#include "../arch/x86/include/uapi/asm/perf_regs.h"
41
Adrian Hunter90e457f2015-07-17 19:33:41 +030042#include "intel-pt-decoder/intel-pt-log.h"
43#include "intel-pt-decoder/intel-pt-decoder.h"
44#include "intel-pt-decoder/intel-pt-insn-decoder.h"
45#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
46
47#define MAX_TIMESTAMP (~0ULL)
48
Adrian Hunter2c47db92019-06-04 16:00:09 +030049struct range {
50 u64 start;
51 u64 end;
52};
53
Adrian Hunter90e457f2015-07-17 19:33:41 +030054struct intel_pt {
55 struct auxtrace auxtrace;
56 struct auxtrace_queues queues;
57 struct auxtrace_heap heap;
58 u32 auxtrace_type;
59 struct perf_session *session;
60 struct machine *machine;
Jiri Olsa32dcd022019-07-21 13:23:51 +020061 struct evsel *switch_evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +030062 struct thread *unknown_thread;
63 bool timeless_decoding;
64 bool sampling_mode;
65 bool snapshot_mode;
66 bool per_cpu_mmaps;
67 bool have_tsc;
68 bool data_queued;
69 bool est_tsc;
70 bool sync_switch;
Adrian Hunterba11ba62015-09-25 16:15:56 +030071 bool mispred_all;
Adrian Hunter1ef998f2020-04-29 18:07:44 +030072 bool use_thread_stack;
Adrian Huntercf888e02020-04-29 18:07:45 +030073 bool callstack;
74 unsigned int br_stack_sz;
Adrian Hunterf0a0251c2020-04-29 18:07:49 +030075 unsigned int br_stack_sz_plus;
Adrian Hunter90e457f2015-07-17 19:33:41 +030076 int have_sched_switch;
77 u32 pmu_type;
78 u64 kernel_start;
79 u64 switch_ip;
80 u64 ptss_ip;
Adrian Hunter5ac35d72021-04-30 10:03:06 +030081 u64 first_timestamp;
Adrian Hunter90e457f2015-07-17 19:33:41 +030082
83 struct perf_tsc_conversion tc;
84 bool cap_user_time_zero;
85
86 struct itrace_synth_opts synth_opts;
87
88 bool sample_instructions;
89 u64 instructions_sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +030090 u64 instructions_id;
91
92 bool sample_branches;
93 u32 branches_filter;
94 u64 branches_sample_type;
95 u64 branches_id;
96
97 bool sample_transactions;
98 u64 transactions_sample_type;
99 u64 transactions_id;
100
Adrian Hunter37973072017-06-30 11:36:45 +0300101 bool sample_ptwrites;
102 u64 ptwrites_sample_type;
103 u64 ptwrites_id;
104
105 bool sample_pwr_events;
106 u64 pwr_events_sample_type;
107 u64 mwait_id;
108 u64 pwre_id;
109 u64 exstop_id;
110 u64 pwrx_id;
111 u64 cbr_id;
Adrian Hunterc840cbf2021-02-05 19:53:50 +0200112 u64 psb_id;
Adrian Hunter37973072017-06-30 11:36:45 +0300113
Adrian Hunterdedcc0e2021-09-07 19:39:03 +0300114 bool single_pebs;
Adrian Huntere62ca652019-06-10 10:27:56 +0300115 bool sample_pebs;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200116 struct evsel *pebs_evsel;
Adrian Huntere62ca652019-06-10 10:27:56 +0300117
Adrian Hunter90e457f2015-07-17 19:33:41 +0300118 u64 tsc_bit;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300119 u64 mtc_bit;
120 u64 mtc_freq_bits;
121 u32 tsc_ctc_ratio_n;
122 u32 tsc_ctc_ratio_d;
123 u64 cyc_bit;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300124 u64 noretcomp_bit;
125 unsigned max_non_turbo_ratio;
Adrian Hunter37973072017-06-30 11:36:45 +0300126 unsigned cbr2khz;
Adrian Hunterb4b046f2021-07-01 20:51:32 +0300127 int max_loops;
Andi Kleend1706b32016-03-28 10:45:38 -0700128
129 unsigned long num_events;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +0300130
131 char *filter;
Adrian Hunter2acee102016-09-23 17:38:48 +0300132 struct addr_filters filts;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300133
134 struct range *time_ranges;
135 unsigned int range_cnt;
Adrian Hunter2855c052020-04-01 13:16:08 +0300136
137 struct ip_callchain *chain;
Adrian Hunterf0a0251c2020-04-29 18:07:49 +0300138 struct branch_stack *br_stack;
Adrian Hunter0fc9d332021-04-30 10:03:05 +0300139
140 u64 dflt_tsc_offset;
141 struct rb_root vmcs_info;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300142};
143
144enum switch_state {
145 INTEL_PT_SS_NOT_TRACING,
146 INTEL_PT_SS_UNKNOWN,
147 INTEL_PT_SS_TRACING,
148 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
149 INTEL_PT_SS_EXPECTING_SWITCH_IP,
150};
151
Adrian Hunterdedcc0e2021-09-07 19:39:03 +0300152/* applicable_counters is 64-bits */
153#define INTEL_PT_MAX_PEBS 64
154
155struct intel_pt_pebs_event {
156 struct evsel *evsel;
157 u64 id;
158};
159
Adrian Hunter90e457f2015-07-17 19:33:41 +0300160struct intel_pt_queue {
161 struct intel_pt *pt;
162 unsigned int queue_nr;
163 struct auxtrace_buffer *buffer;
Adrian Hunter9c665062018-03-07 16:02:27 +0200164 struct auxtrace_buffer *old_buffer;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300165 void *decoder;
166 const struct intel_pt_state *state;
167 struct ip_callchain *chain;
Adrian Hunterf14445e2015-09-25 16:15:45 +0300168 struct branch_stack *last_branch;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300169 union perf_event *event_buf;
170 bool on_heap;
171 bool stop;
172 bool step_through_buffers;
173 bool use_buffer_pid_tid;
Adrian Hunter63d8e382018-03-07 16:02:22 +0200174 bool sync_switch;
Adrian Hunterf2b91382021-10-27 11:03:31 +0300175 bool sample_ipc;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300176 pid_t pid, tid;
177 int cpu;
178 int switch_state;
179 pid_t next_tid;
180 struct thread *thread;
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200181 struct machine *guest_machine;
182 struct thread *unknown_guest_thread;
183 pid_t guest_machine_pid;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300184 bool exclude_kernel;
185 bool have_sample;
186 u64 time;
187 u64 timestamp;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300188 u64 sel_timestamp;
189 bool sel_start;
190 unsigned int sel_idx;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300191 u32 flags;
192 u16 insn_len;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300193 u64 last_insn_cnt;
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +0300194 u64 ipc_insn_cnt;
195 u64 ipc_cyc_cnt;
196 u64 last_in_insn_cnt;
197 u64 last_in_cyc_cnt;
198 u64 last_br_insn_cnt;
199 u64 last_br_cyc_cnt;
Adrian Hunter5fe2cf72019-06-22 12:32:45 +0300200 unsigned int cbr_seen;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300201 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunterdedcc0e2021-09-07 19:39:03 +0300202 struct intel_pt_pebs_event pebs[INTEL_PT_MAX_PEBS];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300203};
204
205static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
206 unsigned char *buf, size_t len)
207{
208 struct intel_pt_pkt packet;
209 size_t pos = 0;
210 int ret, pkt_len, i;
211 char desc[INTEL_PT_PKT_DESC_MAX];
212 const char *color = PERF_COLOR_BLUE;
Adrian Hunteredff7802019-06-10 10:27:53 +0300213 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300214
215 color_fprintf(stdout, color,
216 ". ... Intel Processor Trace data: size %zu bytes\n",
217 len);
218
219 while (len) {
Adrian Hunteredff7802019-06-10 10:27:53 +0300220 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300221 if (ret > 0)
222 pkt_len = ret;
223 else
224 pkt_len = 1;
225 printf(".");
226 color_fprintf(stdout, color, " %08x: ", pos);
227 for (i = 0; i < pkt_len; i++)
228 color_fprintf(stdout, color, " %02x", buf[i]);
229 for (; i < 16; i++)
230 color_fprintf(stdout, color, " ");
231 if (ret > 0) {
232 ret = intel_pt_pkt_desc(&packet, desc,
233 INTEL_PT_PKT_DESC_MAX);
234 if (ret > 0)
235 color_fprintf(stdout, color, " %s\n", desc);
236 } else {
237 color_fprintf(stdout, color, " Bad packet!\n");
238 }
239 pos += pkt_len;
240 buf += pkt_len;
241 len -= pkt_len;
242 }
243}
244
245static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
246 size_t len)
247{
248 printf(".\n");
249 intel_pt_dump(pt, buf, len);
250}
251
Adrian Hunter93f8be22018-11-05 09:35:04 +0200252static void intel_pt_log_event(union perf_event *event)
253{
254 FILE *f = intel_pt_log_fp();
255
256 if (!intel_pt_enable_logging || !f)
257 return;
258
Adrian Hunter7eeb9852020-05-12 15:19:22 +0300259 perf_event__fprintf(event, NULL, f);
Adrian Hunter93f8be22018-11-05 09:35:04 +0200260}
261
Adrian Hunterdbd13432019-11-15 14:42:24 +0200262static void intel_pt_dump_sample(struct perf_session *session,
263 struct perf_sample *sample)
264{
265 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
266 auxtrace);
267
268 printf("\n");
269 intel_pt_dump(pt, sample->aux_sample.data, sample->aux_sample.size);
270}
271
Adrian Hunterd4575f52020-07-10 18:11:01 +0300272static bool intel_pt_log_events(struct intel_pt *pt, u64 tm)
Adrian Hunter8b83fcc2020-07-10 18:11:00 +0300273{
Adrian Hunterd4575f52020-07-10 18:11:01 +0300274 struct perf_time_interval *range = pt->synth_opts.ptime_range;
275 int n = pt->synth_opts.range_num;
276
277 if (pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
278 return true;
279
280 if (pt->synth_opts.log_minus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
281 return false;
282
283 /* perf_time__ranges_skip_sample does not work if time is zero */
284 if (!tm)
285 tm = 1;
286
287 return !n || !perf_time__ranges_skip_sample(range, n, tm);
Adrian Hunter8b83fcc2020-07-10 18:11:00 +0300288}
289
Adrian Hunter0fc9d332021-04-30 10:03:05 +0300290static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs(struct rb_root *rb_root,
291 u64 vmcs,
292 u64 dflt_tsc_offset)
293{
294 struct rb_node **p = &rb_root->rb_node;
295 struct rb_node *parent = NULL;
296 struct intel_pt_vmcs_info *v;
297
298 while (*p) {
299 parent = *p;
300 v = rb_entry(parent, struct intel_pt_vmcs_info, rb_node);
301
302 if (v->vmcs == vmcs)
303 return v;
304
305 if (vmcs < v->vmcs)
306 p = &(*p)->rb_left;
307 else
308 p = &(*p)->rb_right;
309 }
310
311 v = zalloc(sizeof(*v));
312 if (v) {
313 v->vmcs = vmcs;
314 v->tsc_offset = dflt_tsc_offset;
315 v->reliable = dflt_tsc_offset;
316
317 rb_link_node(&v->rb_node, parent, p);
318 rb_insert_color(&v->rb_node, rb_root);
319 }
320
321 return v;
322}
323
324static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs_info(void *data, uint64_t vmcs)
325{
326 struct intel_pt_queue *ptq = data;
327 struct intel_pt *pt = ptq->pt;
328
329 if (!vmcs && !pt->dflt_tsc_offset)
330 return NULL;
331
332 return intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, pt->dflt_tsc_offset);
333}
334
335static void intel_pt_free_vmcs_info(struct intel_pt *pt)
336{
337 struct intel_pt_vmcs_info *v;
338 struct rb_node *n;
339
340 n = rb_first(&pt->vmcs_info);
341 while (n) {
342 v = rb_entry(n, struct intel_pt_vmcs_info, rb_node);
343 n = rb_next(n);
344 rb_erase(&v->rb_node, &pt->vmcs_info);
345 free(v);
346 }
347}
348
Adrian Hunter90e457f2015-07-17 19:33:41 +0300349static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
350 struct auxtrace_buffer *b)
351{
Adrian Hunter117db4b2018-03-07 16:02:21 +0200352 bool consecutive = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300353 void *start;
354
355 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
Adrian Hunter335358c2021-04-30 10:03:04 +0300356 pt->have_tsc, &consecutive,
357 pt->synth_opts.vm_time_correlation);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300358 if (!start)
359 return -EINVAL;
Adrian Hunter335358c2021-04-30 10:03:04 +0300360 /*
361 * In the case of vm_time_correlation, the overlap might contain TSC
362 * packets that will not be fixed, and that will then no longer work for
363 * overlap detection. Avoid that by zeroing out the overlap.
364 */
365 if (pt->synth_opts.vm_time_correlation)
366 memset(b->data, 0, start - b->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300367 b->use_size = b->data + b->size - start;
368 b->use_data = start;
Adrian Hunter117db4b2018-03-07 16:02:21 +0200369 if (b->use_size && consecutive)
370 b->consecutive = true;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300371 return 0;
372}
373
Adrian Huntere96f7df2019-06-04 16:00:07 +0300374static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
375 struct auxtrace_buffer *buffer,
376 struct auxtrace_buffer *old_buffer,
377 struct intel_pt_buffer *b)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300378{
Adrian Hunter599a5be2018-03-07 16:02:29 +0200379 bool might_overlap;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300380
Adrian Hunter90e457f2015-07-17 19:33:41 +0300381 if (!buffer->data) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100382 int fd = perf_data__fd(ptq->pt->session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300383
384 buffer->data = auxtrace_buffer__get_data(buffer, fd);
385 if (!buffer->data)
386 return -ENOMEM;
387 }
388
Adrian Hunter599a5be2018-03-07 16:02:29 +0200389 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
390 if (might_overlap && !buffer->consecutive && old_buffer &&
Adrian Hunter90e457f2015-07-17 19:33:41 +0300391 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
392 return -ENOMEM;
393
Adrian Hunter90e457f2015-07-17 19:33:41 +0300394 if (buffer->use_data) {
395 b->len = buffer->use_size;
396 b->buf = buffer->use_data;
397 } else {
398 b->len = buffer->size;
399 b->buf = buffer->data;
400 }
401 b->ref_timestamp = buffer->reference;
402
Adrian Hunter599a5be2018-03-07 16:02:29 +0200403 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300404 b->consecutive = false;
405 b->trace_nr = buffer->buffer_nr + 1;
406 } else {
407 b->consecutive = true;
408 }
409
Adrian Huntere96f7df2019-06-04 16:00:07 +0300410 return 0;
411}
412
Adrian Hunterda9000a2019-06-04 16:00:08 +0300413/* Do not drop buffers with references - refer intel_pt_get_trace() */
414static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
415 struct auxtrace_buffer *buffer)
416{
417 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
418 return;
419
420 auxtrace_buffer__drop_data(buffer);
421}
422
423/* Must be serialized with respect to intel_pt_get_trace() */
424static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
425 void *cb_data)
426{
427 struct intel_pt_queue *ptq = data;
428 struct auxtrace_buffer *buffer = ptq->buffer;
429 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
430 struct auxtrace_queue *queue;
431 int err = 0;
432
433 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
434
435 while (1) {
436 struct intel_pt_buffer b = { .len = 0 };
437
438 buffer = auxtrace_buffer__next(queue, buffer);
439 if (!buffer)
440 break;
441
442 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
443 if (err)
444 break;
445
446 if (b.len) {
447 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
448 old_buffer = buffer;
449 } else {
450 intel_pt_lookahead_drop_buffer(ptq, buffer);
451 continue;
452 }
453
454 err = cb(&b, cb_data);
455 if (err)
456 break;
457 }
458
459 if (buffer != old_buffer)
460 intel_pt_lookahead_drop_buffer(ptq, buffer);
461 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
462
463 return err;
464}
465
466/*
467 * This function assumes data is processed sequentially only.
468 * Must be serialized with respect to intel_pt_lookahead()
469 */
Adrian Huntere96f7df2019-06-04 16:00:07 +0300470static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
471{
472 struct intel_pt_queue *ptq = data;
473 struct auxtrace_buffer *buffer = ptq->buffer;
474 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
475 struct auxtrace_queue *queue;
476 int err;
477
478 if (ptq->stop) {
479 b->len = 0;
480 return 0;
481 }
482
483 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
484
485 buffer = auxtrace_buffer__next(queue, buffer);
486 if (!buffer) {
487 if (old_buffer)
488 auxtrace_buffer__drop_data(old_buffer);
489 b->len = 0;
490 return 0;
491 }
492
493 ptq->buffer = buffer;
494
495 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
496 if (err)
497 return err;
498
Adrian Hunter90e457f2015-07-17 19:33:41 +0300499 if (ptq->step_through_buffers)
500 ptq->stop = true;
501
Adrian Hunter9c665062018-03-07 16:02:27 +0200502 if (b->len) {
503 if (old_buffer)
504 auxtrace_buffer__drop_data(old_buffer);
505 ptq->old_buffer = buffer;
506 } else {
507 auxtrace_buffer__drop_data(buffer);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300508 return intel_pt_get_trace(b, data);
Adrian Hunter9c665062018-03-07 16:02:27 +0200509 }
Adrian Hunter90e457f2015-07-17 19:33:41 +0300510
511 return 0;
512}
513
514struct intel_pt_cache_entry {
515 struct auxtrace_cache_entry entry;
516 u64 insn_cnt;
517 u64 byte_cnt;
518 enum intel_pt_insn_op op;
519 enum intel_pt_insn_branch branch;
520 int length;
521 int32_t rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300522 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300523};
524
525static int intel_pt_config_div(const char *var, const char *value, void *data)
526{
527 int *d = data;
528 long val;
529
530 if (!strcmp(var, "intel-pt.cache-divisor")) {
531 val = strtol(value, NULL, 0);
532 if (val > 0 && val <= INT_MAX)
533 *d = val;
534 }
535
536 return 0;
537}
538
539static int intel_pt_cache_divisor(void)
540{
541 static int d;
542
543 if (d)
544 return d;
545
546 perf_config(intel_pt_config_div, &d);
547
548 if (!d)
549 d = 64;
550
551 return d;
552}
553
554static unsigned int intel_pt_cache_size(struct dso *dso,
555 struct machine *machine)
556{
557 off_t size;
558
559 size = dso__data_size(dso, machine);
560 size /= intel_pt_cache_divisor();
561 if (size < 1000)
562 return 10;
563 if (size > (1 << 21))
564 return 21;
565 return 32 - __builtin_clz(size);
566}
567
568static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
569 struct machine *machine)
570{
571 struct auxtrace_cache *c;
572 unsigned int bits;
573
574 if (dso->auxtrace_cache)
575 return dso->auxtrace_cache;
576
577 bits = intel_pt_cache_size(dso, machine);
578
579 /* Ignoring cache creation failure */
580 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
581
582 dso->auxtrace_cache = c;
583
584 return c;
585}
586
587static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
588 u64 offset, u64 insn_cnt, u64 byte_cnt,
589 struct intel_pt_insn *intel_pt_insn)
590{
591 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
592 struct intel_pt_cache_entry *e;
593 int err;
594
595 if (!c)
596 return -ENOMEM;
597
598 e = auxtrace_cache__alloc_entry(c);
599 if (!e)
600 return -ENOMEM;
601
602 e->insn_cnt = insn_cnt;
603 e->byte_cnt = byte_cnt;
604 e->op = intel_pt_insn->op;
605 e->branch = intel_pt_insn->branch;
606 e->length = intel_pt_insn->length;
607 e->rel = intel_pt_insn->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300608 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300609
610 err = auxtrace_cache__add(c, offset, &e->entry);
611 if (err)
612 auxtrace_cache__free_entry(c, e);
613
614 return err;
615}
616
617static struct intel_pt_cache_entry *
618intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
619{
620 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
621
622 if (!c)
623 return NULL;
624
625 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
626}
627
Adrian Hunterb22f90a2020-05-12 15:19:20 +0300628static void intel_pt_cache_invalidate(struct dso *dso, struct machine *machine,
629 u64 offset)
630{
631 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
632
633 if (!c)
634 return;
635
636 auxtrace_cache__remove(dso->auxtrace_cache, offset);
637}
638
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200639static inline bool intel_pt_guest_kernel_ip(uint64_t ip)
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200640{
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200641 /* Assumes 64-bit kernel */
642 return ip & (1ULL << 63);
643}
644
645static inline u8 intel_pt_nr_cpumode(struct intel_pt_queue *ptq, uint64_t ip, bool nr)
646{
647 if (nr) {
648 return intel_pt_guest_kernel_ip(ip) ?
649 PERF_RECORD_MISC_GUEST_KERNEL :
650 PERF_RECORD_MISC_GUEST_USER;
651 }
652
653 return ip >= ptq->pt->kernel_start ?
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200654 PERF_RECORD_MISC_KERNEL :
655 PERF_RECORD_MISC_USER;
656}
657
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200658static inline u8 intel_pt_cpumode(struct intel_pt_queue *ptq, uint64_t from_ip, uint64_t to_ip)
659{
660 /* No support for non-zero CS base */
661 if (from_ip)
662 return intel_pt_nr_cpumode(ptq, from_ip, ptq->state->from_nr);
663 return intel_pt_nr_cpumode(ptq, to_ip, ptq->state->to_nr);
664}
665
666static int intel_pt_get_guest(struct intel_pt_queue *ptq)
667{
668 struct machines *machines = &ptq->pt->session->machines;
669 struct machine *machine;
670 pid_t pid = ptq->pid <= 0 ? DEFAULT_GUEST_KERNEL_ID : ptq->pid;
671
672 if (ptq->guest_machine && pid == ptq->guest_machine_pid)
673 return 0;
674
675 ptq->guest_machine = NULL;
676 thread__zput(ptq->unknown_guest_thread);
677
678 machine = machines__find_guest(machines, pid);
679 if (!machine)
680 return -1;
681
682 ptq->unknown_guest_thread = machine__idle_thread(machine);
683 if (!ptq->unknown_guest_thread)
684 return -1;
685
686 ptq->guest_machine = machine;
687 ptq->guest_machine_pid = pid;
688
689 return 0;
690}
691
Adrian Hunter90e457f2015-07-17 19:33:41 +0300692static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
693 uint64_t *insn_cnt_ptr, uint64_t *ip,
694 uint64_t to_ip, uint64_t max_insn_cnt,
695 void *data)
696{
697 struct intel_pt_queue *ptq = data;
698 struct machine *machine = ptq->pt->machine;
699 struct thread *thread;
700 struct addr_location al;
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300701 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300702 ssize_t len;
703 int x86_64;
704 u8 cpumode;
705 u64 offset, start_offset, start_ip;
706 u64 insn_cnt = 0;
707 bool one_map = true;
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200708 bool nr;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300709
Andi Kleenfaaa8762016-10-07 16:42:26 +0300710 intel_pt_insn->length = 0;
711
Adrian Hunter90e457f2015-07-17 19:33:41 +0300712 if (to_ip && *ip == to_ip)
713 goto out_no_cache;
714
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200715 nr = ptq->state->to_nr;
716 cpumode = intel_pt_nr_cpumode(ptq, *ip, nr);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300717
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200718 if (nr) {
719 if (cpumode != PERF_RECORD_MISC_GUEST_KERNEL ||
720 intel_pt_get_guest(ptq))
Adrian Hunter90e457f2015-07-17 19:33:41 +0300721 return -EINVAL;
Adrian Hunter6e86bfd2021-02-18 11:57:57 +0200722 machine = ptq->guest_machine;
723 thread = ptq->unknown_guest_thread;
724 } else {
725 thread = ptq->thread;
726 if (!thread) {
727 if (cpumode != PERF_RECORD_MISC_KERNEL)
728 return -EINVAL;
729 thread = ptq->pt->unknown_thread;
730 }
Adrian Hunter90e457f2015-07-17 19:33:41 +0300731 }
732
733 while (1) {
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300734 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300735 return -EINVAL;
736
737 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
738 dso__data_status_seen(al.map->dso,
739 DSO_DATA_STATUS_SEEN_ITRACE))
740 return -ENOENT;
741
742 offset = al.map->map_ip(al.map, *ip);
743
744 if (!to_ip && one_map) {
745 struct intel_pt_cache_entry *e;
746
747 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
748 if (e &&
749 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
750 *insn_cnt_ptr = e->insn_cnt;
751 *ip += e->byte_cnt;
752 intel_pt_insn->op = e->op;
753 intel_pt_insn->branch = e->branch;
754 intel_pt_insn->length = e->length;
755 intel_pt_insn->rel = e->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300756 memcpy(intel_pt_insn->buf, e->insn,
757 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300758 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
759 return 0;
760 }
761 }
762
763 start_offset = offset;
764 start_ip = *ip;
765
766 /* Load maps to ensure dso->is_64_bit has been updated */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300767 map__load(al.map);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300768
769 x86_64 = al.map->dso->is_64_bit;
770
771 while (1) {
772 len = dso__data_read_offset(al.map->dso, machine,
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300773 offset, buf,
774 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300775 if (len <= 0)
776 return -EINVAL;
777
778 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
779 return -EINVAL;
780
781 intel_pt_log_insn(intel_pt_insn, *ip);
782
783 insn_cnt += 1;
784
785 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
786 goto out;
787
788 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
789 goto out_no_cache;
790
791 *ip += intel_pt_insn->length;
792
Adrian Hunterc954eb72021-05-19 10:45:14 +0300793 if (to_ip && *ip == to_ip) {
794 intel_pt_insn->length = 0;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300795 goto out_no_cache;
Adrian Hunterc954eb72021-05-19 10:45:14 +0300796 }
Adrian Hunter90e457f2015-07-17 19:33:41 +0300797
798 if (*ip >= al.map->end)
799 break;
800
801 offset += intel_pt_insn->length;
802 }
803 one_map = false;
804 }
805out:
806 *insn_cnt_ptr = insn_cnt;
807
808 if (!one_map)
809 goto out_no_cache;
810
811 /*
812 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
813 * entries.
814 */
815 if (to_ip) {
816 struct intel_pt_cache_entry *e;
817
818 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
819 if (e)
820 return 0;
821 }
822
823 /* Ignore cache errors */
824 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
825 *ip - start_ip, intel_pt_insn);
826
827 return 0;
828
829out_no_cache:
830 *insn_cnt_ptr = insn_cnt;
831 return 0;
832}
833
Adrian Hunter2acee102016-09-23 17:38:48 +0300834static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
835 uint64_t offset, const char *filename)
836{
837 struct addr_filter *filt;
838 bool have_filter = false;
839 bool hit_tracestop = false;
840 bool hit_filter = false;
841
842 list_for_each_entry(filt, &pt->filts.head, list) {
843 if (filt->start)
844 have_filter = true;
845
846 if ((filename && !filt->filename) ||
847 (!filename && filt->filename) ||
848 (filename && strcmp(filename, filt->filename)))
849 continue;
850
851 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
852 continue;
853
854 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
855 ip, offset, filename ? filename : "[kernel]",
856 filt->start ? "filter" : "stop",
857 filt->addr, filt->size);
858
859 if (filt->start)
860 hit_filter = true;
861 else
862 hit_tracestop = true;
863 }
864
865 if (!hit_tracestop && !hit_filter)
866 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
867 ip, offset, filename ? filename : "[kernel]");
868
869 return hit_tracestop || (have_filter && !hit_filter);
870}
871
872static int __intel_pt_pgd_ip(uint64_t ip, void *data)
873{
874 struct intel_pt_queue *ptq = data;
875 struct thread *thread;
876 struct addr_location al;
877 u8 cpumode;
878 u64 offset;
879
Adrian Hunter65faca52021-02-18 11:57:58 +0200880 if (ptq->state->to_nr) {
881 if (intel_pt_guest_kernel_ip(ip))
882 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
883 /* No support for decoding guest user space */
884 return -EINVAL;
885 } else if (ip >= ptq->pt->kernel_start) {
Adrian Hunter2acee102016-09-23 17:38:48 +0300886 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
Adrian Hunter65faca52021-02-18 11:57:58 +0200887 }
Adrian Hunter2acee102016-09-23 17:38:48 +0300888
889 cpumode = PERF_RECORD_MISC_USER;
890
891 thread = ptq->thread;
892 if (!thread)
893 return -EINVAL;
894
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300895 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
Adrian Hunter2acee102016-09-23 17:38:48 +0300896 return -EINVAL;
897
898 offset = al.map->map_ip(al.map, ip);
899
900 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
901 al.map->dso->long_name);
902}
903
904static bool intel_pt_pgd_ip(uint64_t ip, void *data)
905{
906 return __intel_pt_pgd_ip(ip, data) > 0;
907}
908
Adrian Hunter90e457f2015-07-17 19:33:41 +0300909static bool intel_pt_get_config(struct intel_pt *pt,
910 struct perf_event_attr *attr, u64 *config)
911{
912 if (attr->type == pt->pmu_type) {
913 if (config)
914 *config = attr->config;
915 return true;
916 }
917
918 return false;
919}
920
921static bool intel_pt_exclude_kernel(struct intel_pt *pt)
922{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200923 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300924
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300925 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200926 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
927 !evsel->core.attr.exclude_kernel)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300928 return false;
929 }
930 return true;
931}
932
933static bool intel_pt_return_compression(struct intel_pt *pt)
934{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200935 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300936 u64 config;
937
938 if (!pt->noretcomp_bit)
939 return true;
940
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300941 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200942 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +0300943 (config & pt->noretcomp_bit))
944 return false;
945 }
946 return true;
947}
948
Adrian Hunter83959812017-05-26 11:17:11 +0300949static bool intel_pt_branch_enable(struct intel_pt *pt)
950{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200951 struct evsel *evsel;
Adrian Hunter83959812017-05-26 11:17:11 +0300952 u64 config;
953
954 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200955 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
Adrian Hunter83959812017-05-26 11:17:11 +0300956 (config & 1) && !(config & 0x2000))
957 return false;
958 }
959 return true;
960}
961
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300962static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
963{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200964 struct evsel *evsel;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300965 unsigned int shift;
966 u64 config;
967
968 if (!pt->mtc_freq_bits)
969 return 0;
970
971 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
972 config >>= 1;
973
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300974 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200975 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300976 return (config & pt->mtc_freq_bits) >> shift;
977 }
978 return 0;
979}
980
Adrian Hunter90e457f2015-07-17 19:33:41 +0300981static bool intel_pt_timeless_decoding(struct intel_pt *pt)
982{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200983 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300984 bool timeless_decoding = true;
985 u64 config;
986
Adrian Huntere9d64732021-04-30 10:03:00 +0300987 if (!pt->tsc_bit || !pt->cap_user_time_zero || pt->synth_opts.timeless_decoding)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300988 return true;
989
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300990 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200991 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
Adrian Hunter90e457f2015-07-17 19:33:41 +0300992 return true;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200993 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300994 if (config & pt->tsc_bit)
995 timeless_decoding = false;
996 else
997 return true;
998 }
999 }
1000 return timeless_decoding;
1001}
1002
1003static bool intel_pt_tracing_kernel(struct intel_pt *pt)
1004{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001005 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001006
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001007 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001008 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
1009 !evsel->core.attr.exclude_kernel)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001010 return true;
1011 }
1012 return false;
1013}
1014
1015static bool intel_pt_have_tsc(struct intel_pt *pt)
1016{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001017 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001018 bool have_tsc = false;
1019 u64 config;
1020
1021 if (!pt->tsc_bit)
1022 return false;
1023
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001024 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001025 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001026 if (config & pt->tsc_bit)
1027 have_tsc = true;
1028 else
1029 return false;
1030 }
1031 }
1032 return have_tsc;
1033}
1034
Adrian Huntere3ff42b2021-04-30 10:03:09 +03001035static bool intel_pt_have_mtc(struct intel_pt *pt)
1036{
1037 struct evsel *evsel;
1038 u64 config;
1039
1040 evlist__for_each_entry(pt->session->evlist, evsel) {
1041 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1042 (config & pt->mtc_bit))
1043 return true;
1044 }
1045 return false;
1046}
1047
Adrian Hunterdbd13432019-11-15 14:42:24 +02001048static bool intel_pt_sampling_mode(struct intel_pt *pt)
1049{
1050 struct evsel *evsel;
1051
1052 evlist__for_each_entry(pt->session->evlist, evsel) {
1053 if ((evsel->core.attr.sample_type & PERF_SAMPLE_AUX) &&
1054 evsel->core.attr.aux_sample_size)
1055 return true;
1056 }
1057 return false;
1058}
1059
Adrian Hunter6af4b602021-02-05 19:53:49 +02001060static u64 intel_pt_ctl(struct intel_pt *pt)
1061{
1062 struct evsel *evsel;
1063 u64 config;
1064
1065 evlist__for_each_entry(pt->session->evlist, evsel) {
1066 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
1067 return config;
1068 }
1069 return 0;
1070}
1071
Adrian Hunter90e457f2015-07-17 19:33:41 +03001072static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
1073{
1074 u64 quot, rem;
1075
1076 quot = ns / pt->tc.time_mult;
1077 rem = ns % pt->tc.time_mult;
1078 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
1079 pt->tc.time_mult;
1080}
1081
Adrian Hunter2855c052020-04-01 13:16:08 +03001082static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt)
1083{
1084 size_t sz = sizeof(struct ip_callchain);
1085
1086 /* Add 1 to callchain_sz for callchain context */
1087 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
1088 return zalloc(sz);
1089}
1090
1091static int intel_pt_callchain_init(struct intel_pt *pt)
1092{
1093 struct evsel *evsel;
1094
1095 evlist__for_each_entry(pt->session->evlist, evsel) {
1096 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN))
1097 evsel->synth_sample_type |= PERF_SAMPLE_CALLCHAIN;
1098 }
1099
1100 pt->chain = intel_pt_alloc_chain(pt);
1101 if (!pt->chain)
1102 return -ENOMEM;
1103
1104 return 0;
1105}
1106
1107static void intel_pt_add_callchain(struct intel_pt *pt,
1108 struct perf_sample *sample)
1109{
1110 struct thread *thread = machine__findnew_thread(pt->machine,
1111 sample->pid,
1112 sample->tid);
1113
1114 thread_stack__sample_late(thread, sample->cpu, pt->chain,
1115 pt->synth_opts.callchain_sz + 1, sample->ip,
1116 pt->kernel_start);
1117
1118 sample->callchain = pt->chain;
1119}
1120
Adrian Hunter961224d2020-05-16 15:35:48 +03001121static struct branch_stack *intel_pt_alloc_br_stack(unsigned int entry_cnt)
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03001122{
1123 size_t sz = sizeof(struct branch_stack);
1124
Adrian Hunter961224d2020-05-16 15:35:48 +03001125 sz += entry_cnt * sizeof(struct branch_entry);
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03001126 return zalloc(sz);
1127}
1128
1129static int intel_pt_br_stack_init(struct intel_pt *pt)
1130{
1131 struct evsel *evsel;
1132
1133 evlist__for_each_entry(pt->session->evlist, evsel) {
1134 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK))
1135 evsel->synth_sample_type |= PERF_SAMPLE_BRANCH_STACK;
1136 }
1137
Adrian Hunter961224d2020-05-16 15:35:48 +03001138 pt->br_stack = intel_pt_alloc_br_stack(pt->br_stack_sz);
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03001139 if (!pt->br_stack)
1140 return -ENOMEM;
1141
1142 return 0;
1143}
1144
1145static void intel_pt_add_br_stack(struct intel_pt *pt,
1146 struct perf_sample *sample)
1147{
1148 struct thread *thread = machine__findnew_thread(pt->machine,
1149 sample->pid,
1150 sample->tid);
1151
1152 thread_stack__br_sample_late(thread, sample->cpu, pt->br_stack,
1153 pt->br_stack_sz, sample->ip,
1154 pt->kernel_start);
1155
1156 sample->branch_stack = pt->br_stack;
1157}
1158
Adrian Hunter961224d2020-05-16 15:35:48 +03001159/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1160#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U)
1161
Adrian Hunter90e457f2015-07-17 19:33:41 +03001162static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
1163 unsigned int queue_nr)
1164{
1165 struct intel_pt_params params = { .get_trace = 0, };
Adrian Hunter9fb52332018-05-31 13:23:45 +03001166 struct perf_env *env = pt->machine->env;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001167 struct intel_pt_queue *ptq;
1168
1169 ptq = zalloc(sizeof(struct intel_pt_queue));
1170 if (!ptq)
1171 return NULL;
1172
1173 if (pt->synth_opts.callchain) {
Adrian Hunter2855c052020-04-01 13:16:08 +03001174 ptq->chain = intel_pt_alloc_chain(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001175 if (!ptq->chain)
1176 goto out_free;
1177 }
1178
Adrian Hunter961224d2020-05-16 15:35:48 +03001179 if (pt->synth_opts.last_branch || pt->synth_opts.other_events) {
1180 unsigned int entry_cnt = max(LBRS_MAX, pt->br_stack_sz);
1181
1182 ptq->last_branch = intel_pt_alloc_br_stack(entry_cnt);
Adrian Hunterf14445e2015-09-25 16:15:45 +03001183 if (!ptq->last_branch)
1184 goto out_free;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001185 }
1186
Adrian Hunter90e457f2015-07-17 19:33:41 +03001187 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
1188 if (!ptq->event_buf)
1189 goto out_free;
1190
1191 ptq->pt = pt;
1192 ptq->queue_nr = queue_nr;
1193 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
1194 ptq->pid = -1;
1195 ptq->tid = -1;
1196 ptq->cpu = -1;
1197 ptq->next_tid = -1;
1198
1199 params.get_trace = intel_pt_get_trace;
1200 params.walk_insn = intel_pt_walk_next_insn;
Adrian Hunterda9000a2019-06-04 16:00:08 +03001201 params.lookahead = intel_pt_lookahead;
Adrian Hunter0fc9d332021-04-30 10:03:05 +03001202 params.findnew_vmcs_info = intel_pt_findnew_vmcs_info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001203 params.data = ptq;
1204 params.return_compression = intel_pt_return_compression(pt);
Adrian Hunter83959812017-05-26 11:17:11 +03001205 params.branch_enable = intel_pt_branch_enable(pt);
Adrian Hunter6af4b602021-02-05 19:53:49 +02001206 params.ctl = intel_pt_ctl(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001207 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03001208 params.mtc_period = intel_pt_mtc_period(pt);
1209 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
1210 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
Adrian Hunter7c1b16b2020-07-10 18:11:03 +03001211 params.quick = pt->synth_opts.quick;
Adrian Huntere3ff42b2021-04-30 10:03:09 +03001212 params.vm_time_correlation = pt->synth_opts.vm_time_correlation;
1213 params.vm_tm_corr_dry_run = pt->synth_opts.vm_tm_corr_dry_run;
Adrian Hunter5ac35d72021-04-30 10:03:06 +03001214 params.first_timestamp = pt->first_timestamp;
Adrian Hunterb4b046f2021-07-01 20:51:32 +03001215 params.max_loops = pt->max_loops;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001216
Adrian Hunter2acee102016-09-23 17:38:48 +03001217 if (pt->filts.cnt > 0)
1218 params.pgd_ip = intel_pt_pgd_ip;
1219
Adrian Hunter90e457f2015-07-17 19:33:41 +03001220 if (pt->synth_opts.instructions) {
1221 if (pt->synth_opts.period) {
1222 switch (pt->synth_opts.period_type) {
1223 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
1224 params.period_type =
1225 INTEL_PT_PERIOD_INSTRUCTIONS;
1226 params.period = pt->synth_opts.period;
1227 break;
1228 case PERF_ITRACE_PERIOD_TICKS:
1229 params.period_type = INTEL_PT_PERIOD_TICKS;
1230 params.period = pt->synth_opts.period;
1231 break;
1232 case PERF_ITRACE_PERIOD_NANOSECS:
1233 params.period_type = INTEL_PT_PERIOD_TICKS;
1234 params.period = intel_pt_ns_to_ticks(pt,
1235 pt->synth_opts.period);
1236 break;
1237 default:
1238 break;
1239 }
1240 }
1241
1242 if (!params.period) {
1243 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
Adrian Huntere1791342015-09-25 16:15:32 +03001244 params.period = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001245 }
1246 }
1247
Adrian Hunter9fb52332018-05-31 13:23:45 +03001248 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
1249 params.flags |= INTEL_PT_FUP_WITH_NLIP;
1250
Adrian Hunter90e457f2015-07-17 19:33:41 +03001251 ptq->decoder = intel_pt_decoder_new(&params);
1252 if (!ptq->decoder)
1253 goto out_free;
1254
1255 return ptq;
1256
1257out_free:
1258 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +03001259 zfree(&ptq->last_branch);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001260 zfree(&ptq->chain);
1261 free(ptq);
1262 return NULL;
1263}
1264
1265static void intel_pt_free_queue(void *priv)
1266{
1267 struct intel_pt_queue *ptq = priv;
1268
1269 if (!ptq)
1270 return;
1271 thread__zput(ptq->thread);
Adrian Hunter6e86bfd2021-02-18 11:57:57 +02001272 thread__zput(ptq->unknown_guest_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001273 intel_pt_decoder_free(ptq->decoder);
1274 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +03001275 zfree(&ptq->last_branch);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001276 zfree(&ptq->chain);
1277 free(ptq);
1278}
1279
Adrian Hunter5ac35d72021-04-30 10:03:06 +03001280static void intel_pt_first_timestamp(struct intel_pt *pt, u64 timestamp)
1281{
1282 unsigned int i;
1283
1284 pt->first_timestamp = timestamp;
1285
1286 for (i = 0; i < pt->queues.nr_queues; i++) {
1287 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1288 struct intel_pt_queue *ptq = queue->priv;
1289
1290 if (ptq && ptq->decoder)
1291 intel_pt_set_first_timestamp(ptq->decoder, timestamp);
1292 }
1293}
1294
Adrian Hunter90e457f2015-07-17 19:33:41 +03001295static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
1296 struct auxtrace_queue *queue)
1297{
1298 struct intel_pt_queue *ptq = queue->priv;
1299
1300 if (queue->tid == -1 || pt->have_sched_switch) {
1301 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
Adrian Hunter7d537a82020-09-09 11:49:23 +03001302 if (ptq->tid == -1)
1303 ptq->pid = -1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001304 thread__zput(ptq->thread);
1305 }
1306
1307 if (!ptq->thread && ptq->tid != -1)
1308 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
1309
1310 if (ptq->thread) {
1311 ptq->pid = ptq->thread->pid_;
1312 if (queue->cpu == -1)
1313 ptq->cpu = ptq->thread->cpu;
1314 }
1315}
1316
1317static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
1318{
Adrian Hunterc954eb72021-05-19 10:45:14 +03001319 ptq->insn_len = 0;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001320 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
1321 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
1322 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
Adrian Hunter695fc452021-02-18 11:57:59 +02001323 if (!ptq->state->to_ip)
1324 ptq->flags = PERF_IP_FLAG_BRANCH |
1325 PERF_IP_FLAG_TRACE_END;
1326 else if (ptq->state->from_nr && !ptq->state->to_nr)
1327 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1328 PERF_IP_FLAG_VMEXIT;
1329 else
Adrian Hunter90e457f2015-07-17 19:33:41 +03001330 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1331 PERF_IP_FLAG_ASYNC |
1332 PERF_IP_FLAG_INTERRUPT;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001333 } else {
1334 if (ptq->state->from_ip)
1335 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1336 else
1337 ptq->flags = PERF_IP_FLAG_BRANCH |
1338 PERF_IP_FLAG_TRACE_BEGIN;
1339 if (ptq->state->flags & INTEL_PT_IN_TX)
1340 ptq->flags |= PERF_IP_FLAG_IN_TX;
1341 ptq->insn_len = ptq->state->insn_len;
Andi Kleenfaaa8762016-10-07 16:42:26 +03001342 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001343 }
Adrian Hunterc6b5da02018-09-20 16:00:47 +03001344
1345 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1346 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1347 if (ptq->state->type & INTEL_PT_TRACE_END)
1348 ptq->flags |= PERF_IP_FLAG_TRACE_END;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001349}
1350
Adrian Hunter2c47db92019-06-04 16:00:09 +03001351static void intel_pt_setup_time_range(struct intel_pt *pt,
1352 struct intel_pt_queue *ptq)
1353{
1354 if (!pt->range_cnt)
1355 return;
1356
1357 ptq->sel_timestamp = pt->time_ranges[0].start;
1358 ptq->sel_idx = 0;
1359
1360 if (ptq->sel_timestamp) {
1361 ptq->sel_start = true;
1362 } else {
1363 ptq->sel_timestamp = pt->time_ranges[0].end;
1364 ptq->sel_start = false;
1365 }
1366}
1367
Adrian Hunter90e457f2015-07-17 19:33:41 +03001368static int intel_pt_setup_queue(struct intel_pt *pt,
1369 struct auxtrace_queue *queue,
1370 unsigned int queue_nr)
1371{
1372 struct intel_pt_queue *ptq = queue->priv;
1373
1374 if (list_empty(&queue->head))
1375 return 0;
1376
1377 if (!ptq) {
1378 ptq = intel_pt_alloc_queue(pt, queue_nr);
1379 if (!ptq)
1380 return -ENOMEM;
1381 queue->priv = ptq;
1382
1383 if (queue->cpu != -1)
1384 ptq->cpu = queue->cpu;
1385 ptq->tid = queue->tid;
1386
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001387 ptq->cbr_seen = UINT_MAX;
1388
Adrian Hunter1c071c82018-03-07 16:02:26 +02001389 if (pt->sampling_mode && !pt->snapshot_mode &&
1390 pt->timeless_decoding)
1391 ptq->step_through_buffers = true;
Adrian Hunter63d8e382018-03-07 16:02:22 +02001392
1393 ptq->sync_switch = pt->sync_switch;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001394
1395 intel_pt_setup_time_range(pt, ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001396 }
1397
1398 if (!ptq->on_heap &&
Adrian Hunter63d8e382018-03-07 16:02:22 +02001399 (!ptq->sync_switch ||
Adrian Hunter90e457f2015-07-17 19:33:41 +03001400 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1401 const struct intel_pt_state *state;
1402 int ret;
1403
1404 if (pt->timeless_decoding)
1405 return 0;
1406
1407 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1408 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1409 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
Adrian Hunter2c47db92019-06-04 16:00:09 +03001410
1411 if (ptq->sel_start && ptq->sel_timestamp) {
1412 ret = intel_pt_fast_forward(ptq->decoder,
1413 ptq->sel_timestamp);
1414 if (ret)
1415 return ret;
1416 }
1417
Adrian Hunter90e457f2015-07-17 19:33:41 +03001418 while (1) {
1419 state = intel_pt_decode(ptq->decoder);
1420 if (state->err) {
1421 if (state->err == INTEL_PT_ERR_NODATA) {
1422 intel_pt_log("queue %u has no timestamp\n",
1423 queue_nr);
1424 return 0;
1425 }
1426 continue;
1427 }
1428 if (state->timestamp)
1429 break;
1430 }
1431
1432 ptq->timestamp = state->timestamp;
1433 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1434 queue_nr, ptq->timestamp);
1435 ptq->state = state;
1436 ptq->have_sample = true;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001437 if (ptq->sel_start && ptq->sel_timestamp &&
1438 ptq->timestamp < ptq->sel_timestamp)
1439 ptq->have_sample = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001440 intel_pt_sample_flags(ptq);
1441 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1442 if (ret)
1443 return ret;
1444 ptq->on_heap = true;
1445 }
1446
1447 return 0;
1448}
1449
1450static int intel_pt_setup_queues(struct intel_pt *pt)
1451{
1452 unsigned int i;
1453 int ret;
1454
1455 for (i = 0; i < pt->queues.nr_queues; i++) {
1456 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1457 if (ret)
1458 return ret;
1459 }
1460 return 0;
1461}
1462
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001463static inline bool intel_pt_skip_event(struct intel_pt *pt)
1464{
1465 return pt->synth_opts.initial_skip &&
1466 pt->num_events++ < pt->synth_opts.initial_skip;
1467}
1468
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001469/*
1470 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
1471 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
1472 * from this decoder state.
1473 */
1474static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
1475{
1476 return pt->synth_opts.initial_skip &&
1477 pt->num_events + 4 < pt->synth_opts.initial_skip;
1478}
1479
Adrian Hunter0dfded32019-06-10 10:27:57 +03001480static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1481 union perf_event *event,
1482 struct perf_sample *sample)
1483{
1484 event->sample.header.type = PERF_RECORD_SAMPLE;
1485 event->sample.header.size = sizeof(struct perf_event_header);
1486
1487 sample->pid = ptq->pid;
1488 sample->tid = ptq->tid;
1489 sample->cpu = ptq->cpu;
1490 sample->insn_len = ptq->insn_len;
1491 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1492}
1493
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001494static void intel_pt_prep_b_sample(struct intel_pt *pt,
1495 struct intel_pt_queue *ptq,
1496 union perf_event *event,
1497 struct perf_sample *sample)
1498{
Adrian Hunter0dfded32019-06-10 10:27:57 +03001499 intel_pt_prep_a_sample(ptq, event, sample);
1500
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001501 if (!pt->timeless_decoding)
1502 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1503
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001504 sample->ip = ptq->state->from_ip;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001505 sample->addr = ptq->state->to_ip;
Adrian Hunter6e86bfd2021-02-18 11:57:57 +02001506 sample->cpumode = intel_pt_cpumode(ptq, sample->ip, sample->addr);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001507 sample->period = 1;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001508 sample->flags = ptq->flags;
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001509
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001510 event->sample.header.misc = sample->cpumode;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001511}
1512
Adrian Hunter90e457f2015-07-17 19:33:41 +03001513static int intel_pt_inject_event(union perf_event *event,
Adrian Huntera10eb532018-01-16 15:14:50 +02001514 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001515{
1516 event->header.size = perf_event__sample_event_size(sample, type, 0);
Adrian Hunter936f1f32018-01-16 15:14:52 +02001517 return perf_event__synthesize_sample(event, type, 0, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001518}
1519
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001520static inline int intel_pt_opt_inject(struct intel_pt *pt,
1521 union perf_event *event,
1522 struct perf_sample *sample, u64 type)
1523{
1524 if (!pt->synth_opts.inject)
1525 return 0;
1526
Adrian Huntera10eb532018-01-16 15:14:50 +02001527 return intel_pt_inject_event(event, sample, type);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001528}
1529
Adrian Huntercf888e02020-04-29 18:07:45 +03001530static int intel_pt_deliver_synth_event(struct intel_pt *pt,
1531 union perf_event *event,
1532 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001533{
1534 int ret;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001535
1536 ret = intel_pt_opt_inject(pt, event, sample, type);
1537 if (ret)
1538 return ret;
1539
1540 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1541 if (ret)
1542 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1543
1544 return ret;
1545}
1546
1547static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1548{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001549 struct intel_pt *pt = ptq->pt;
1550 union perf_event *event = ptq->event_buf;
1551 struct perf_sample sample = { .ip = 0, };
Adrian Hunterf14445e2015-09-25 16:15:45 +03001552 struct dummy_branch_stack {
1553 u64 nr;
Kan Liang42bbabe2020-02-28 08:30:00 -08001554 u64 hw_idx;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001555 struct branch_entry entries;
1556 } dummy_bs;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001557
Adrian Hunter385e3302015-09-25 16:15:44 +03001558 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1559 return 0;
1560
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001561 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001562 return 0;
1563
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001564 intel_pt_prep_b_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001565
Adrian Hunter90e457f2015-07-17 19:33:41 +03001566 sample.id = ptq->pt->branches_id;
1567 sample.stream_id = ptq->pt->branches_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001568
Adrian Hunterf14445e2015-09-25 16:15:45 +03001569 /*
1570 * perf report cannot handle events without a branch stack when using
1571 * SORT_MODE__BRANCH so make a dummy one.
1572 */
1573 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1574 dummy_bs = (struct dummy_branch_stack){
1575 .nr = 1,
Kan Liang42bbabe2020-02-28 08:30:00 -08001576 .hw_idx = -1ULL,
Adrian Hunterf14445e2015-09-25 16:15:45 +03001577 .entries = {
1578 .from = sample.ip,
1579 .to = sample.addr,
1580 },
1581 };
1582 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1583 }
1584
Adrian Hunterf2b91382021-10-27 11:03:31 +03001585 if (ptq->sample_ipc)
Adrian Hunter20aa3972021-02-05 19:53:48 +02001586 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001587 if (sample.cyc_cnt) {
1588 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1589 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1590 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1591 }
1592
Adrian Huntercf888e02020-04-29 18:07:45 +03001593 return intel_pt_deliver_synth_event(pt, event, &sample,
1594 pt->branches_sample_type);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001595}
1596
1597static void intel_pt_prep_sample(struct intel_pt *pt,
1598 struct intel_pt_queue *ptq,
1599 union perf_event *event,
1600 struct perf_sample *sample)
1601{
1602 intel_pt_prep_b_sample(pt, ptq, event, sample);
1603
1604 if (pt->synth_opts.callchain) {
Adrian Hunter256d92b2018-12-21 14:06:19 +02001605 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
Adrian Hunter24248302018-10-31 11:10:42 +02001606 pt->synth_opts.callchain_sz + 1,
1607 sample->ip, pt->kernel_start);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001608 sample->callchain = ptq->chain;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001609 }
1610
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001611 if (pt->synth_opts.last_branch) {
Adrian Huntercf888e02020-04-29 18:07:45 +03001612 thread_stack__br_sample(ptq->thread, ptq->cpu, ptq->last_branch,
1613 pt->br_stack_sz);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001614 sample->branch_stack = ptq->last_branch;
1615 }
1616}
1617
Adrian Hunter90e457f2015-07-17 19:33:41 +03001618static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1619{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001620 struct intel_pt *pt = ptq->pt;
1621 union perf_event *event = ptq->event_buf;
1622 struct perf_sample sample = { .ip = 0, };
1623
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001624 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001625 return 0;
1626
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001627 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001628
Adrian Hunter90e457f2015-07-17 19:33:41 +03001629 sample.id = ptq->pt->instructions_id;
1630 sample.stream_id = ptq->pt->instructions_id;
Adrian Hunter7c1b16b2020-07-10 18:11:03 +03001631 if (pt->synth_opts.quick)
1632 sample.period = 1;
1633 else
1634 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001635
Adrian Hunterf2b91382021-10-27 11:03:31 +03001636 if (ptq->sample_ipc)
Adrian Hunter20aa3972021-02-05 19:53:48 +02001637 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001638 if (sample.cyc_cnt) {
1639 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1640 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1641 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1642 }
1643
Adrian Hunter2a21d032015-07-17 19:33:48 +03001644 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1645
Adrian Huntercf888e02020-04-29 18:07:45 +03001646 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001647 pt->instructions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001648}
1649
1650static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1651{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001652 struct intel_pt *pt = ptq->pt;
1653 union perf_event *event = ptq->event_buf;
1654 struct perf_sample sample = { .ip = 0, };
1655
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001656 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001657 return 0;
1658
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001659 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001660
Adrian Hunter90e457f2015-07-17 19:33:41 +03001661 sample.id = ptq->pt->transactions_id;
1662 sample.stream_id = ptq->pt->transactions_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001663
Adrian Huntercf888e02020-04-29 18:07:45 +03001664 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001665 pt->transactions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001666}
1667
Adrian Hunter37973072017-06-30 11:36:45 +03001668static void intel_pt_prep_p_sample(struct intel_pt *pt,
1669 struct intel_pt_queue *ptq,
1670 union perf_event *event,
1671 struct perf_sample *sample)
1672{
1673 intel_pt_prep_sample(pt, ptq, event, sample);
1674
1675 /*
1676 * Zero IP is used to mean "trace start" but that is not the case for
1677 * power or PTWRITE events with no IP, so clear the flags.
1678 */
1679 if (!sample->ip)
1680 sample->flags = 0;
1681}
1682
1683static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1684{
1685 struct intel_pt *pt = ptq->pt;
1686 union perf_event *event = ptq->event_buf;
1687 struct perf_sample sample = { .ip = 0, };
1688 struct perf_synth_intel_ptwrite raw;
1689
1690 if (intel_pt_skip_event(pt))
1691 return 0;
1692
1693 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1694
1695 sample.id = ptq->pt->ptwrites_id;
1696 sample.stream_id = ptq->pt->ptwrites_id;
1697
1698 raw.flags = 0;
1699 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1700 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1701
1702 sample.raw_size = perf_synth__raw_size(raw);
1703 sample.raw_data = perf_synth__raw_data(&raw);
1704
Adrian Huntercf888e02020-04-29 18:07:45 +03001705 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001706 pt->ptwrites_sample_type);
1707}
1708
1709static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1710{
1711 struct intel_pt *pt = ptq->pt;
1712 union perf_event *event = ptq->event_buf;
1713 struct perf_sample sample = { .ip = 0, };
1714 struct perf_synth_intel_cbr raw;
1715 u32 flags;
1716
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001717 if (intel_pt_skip_cbr_event(pt))
Adrian Hunter37973072017-06-30 11:36:45 +03001718 return 0;
1719
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001720 ptq->cbr_seen = ptq->state->cbr;
1721
Adrian Hunter37973072017-06-30 11:36:45 +03001722 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1723
1724 sample.id = ptq->pt->cbr_id;
1725 sample.stream_id = ptq->pt->cbr_id;
1726
1727 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1728 raw.flags = cpu_to_le32(flags);
1729 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1730 raw.reserved3 = 0;
1731
1732 sample.raw_size = perf_synth__raw_size(raw);
1733 sample.raw_data = perf_synth__raw_data(&raw);
1734
Adrian Huntercf888e02020-04-29 18:07:45 +03001735 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001736 pt->pwr_events_sample_type);
1737}
1738
Adrian Hunterc840cbf2021-02-05 19:53:50 +02001739static int intel_pt_synth_psb_sample(struct intel_pt_queue *ptq)
1740{
1741 struct intel_pt *pt = ptq->pt;
1742 union perf_event *event = ptq->event_buf;
1743 struct perf_sample sample = { .ip = 0, };
1744 struct perf_synth_intel_psb raw;
1745
1746 if (intel_pt_skip_event(pt))
1747 return 0;
1748
1749 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1750
1751 sample.id = ptq->pt->psb_id;
1752 sample.stream_id = ptq->pt->psb_id;
1753 sample.flags = 0;
1754
1755 raw.reserved = 0;
1756 raw.offset = ptq->state->psb_offset;
1757
1758 sample.raw_size = perf_synth__raw_size(raw);
1759 sample.raw_data = perf_synth__raw_data(&raw);
1760
1761 return intel_pt_deliver_synth_event(pt, event, &sample,
1762 pt->pwr_events_sample_type);
1763}
1764
Adrian Hunter37973072017-06-30 11:36:45 +03001765static int intel_pt_synth_mwait_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 perf_synth_intel_mwait raw;
1771
1772 if (intel_pt_skip_event(pt))
1773 return 0;
1774
1775 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1776
1777 sample.id = ptq->pt->mwait_id;
1778 sample.stream_id = ptq->pt->mwait_id;
1779
1780 raw.reserved = 0;
1781 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1782
1783 sample.raw_size = perf_synth__raw_size(raw);
1784 sample.raw_data = perf_synth__raw_data(&raw);
1785
Adrian Huntercf888e02020-04-29 18:07:45 +03001786 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001787 pt->pwr_events_sample_type);
1788}
1789
1790static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1791{
1792 struct intel_pt *pt = ptq->pt;
1793 union perf_event *event = ptq->event_buf;
1794 struct perf_sample sample = { .ip = 0, };
1795 struct perf_synth_intel_pwre raw;
1796
1797 if (intel_pt_skip_event(pt))
1798 return 0;
1799
1800 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1801
1802 sample.id = ptq->pt->pwre_id;
1803 sample.stream_id = ptq->pt->pwre_id;
1804
1805 raw.reserved = 0;
1806 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1807
1808 sample.raw_size = perf_synth__raw_size(raw);
1809 sample.raw_data = perf_synth__raw_data(&raw);
1810
Adrian Huntercf888e02020-04-29 18:07:45 +03001811 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001812 pt->pwr_events_sample_type);
1813}
1814
1815static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1816{
1817 struct intel_pt *pt = ptq->pt;
1818 union perf_event *event = ptq->event_buf;
1819 struct perf_sample sample = { .ip = 0, };
1820 struct perf_synth_intel_exstop raw;
1821
1822 if (intel_pt_skip_event(pt))
1823 return 0;
1824
1825 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1826
1827 sample.id = ptq->pt->exstop_id;
1828 sample.stream_id = ptq->pt->exstop_id;
1829
1830 raw.flags = 0;
1831 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1832
1833 sample.raw_size = perf_synth__raw_size(raw);
1834 sample.raw_data = perf_synth__raw_data(&raw);
1835
Adrian Huntercf888e02020-04-29 18:07:45 +03001836 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001837 pt->pwr_events_sample_type);
1838}
1839
1840static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1841{
1842 struct intel_pt *pt = ptq->pt;
1843 union perf_event *event = ptq->event_buf;
1844 struct perf_sample sample = { .ip = 0, };
1845 struct perf_synth_intel_pwrx raw;
1846
1847 if (intel_pt_skip_event(pt))
1848 return 0;
1849
1850 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1851
1852 sample.id = ptq->pt->pwrx_id;
1853 sample.stream_id = ptq->pt->pwrx_id;
1854
1855 raw.reserved = 0;
1856 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1857
1858 sample.raw_size = perf_synth__raw_size(raw);
1859 sample.raw_data = perf_synth__raw_data(&raw);
1860
Adrian Huntercf888e02020-04-29 18:07:45 +03001861 return intel_pt_deliver_synth_event(pt, event, &sample,
Adrian Hunter37973072017-06-30 11:36:45 +03001862 pt->pwr_events_sample_type);
1863}
1864
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001865/*
1866 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
1867 * intel_pt_add_gp_regs().
1868 */
1869static const int pebs_gp_regs[] = {
1870 [PERF_REG_X86_FLAGS] = 1,
1871 [PERF_REG_X86_IP] = 2,
1872 [PERF_REG_X86_AX] = 3,
1873 [PERF_REG_X86_CX] = 4,
1874 [PERF_REG_X86_DX] = 5,
1875 [PERF_REG_X86_BX] = 6,
1876 [PERF_REG_X86_SP] = 7,
1877 [PERF_REG_X86_BP] = 8,
1878 [PERF_REG_X86_SI] = 9,
1879 [PERF_REG_X86_DI] = 10,
1880 [PERF_REG_X86_R8] = 11,
1881 [PERF_REG_X86_R9] = 12,
1882 [PERF_REG_X86_R10] = 13,
1883 [PERF_REG_X86_R11] = 14,
1884 [PERF_REG_X86_R12] = 15,
1885 [PERF_REG_X86_R13] = 16,
1886 [PERF_REG_X86_R14] = 17,
1887 [PERF_REG_X86_R15] = 18,
1888};
1889
1890static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
1891 const struct intel_pt_blk_items *items,
1892 u64 regs_mask)
1893{
1894 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
1895 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
1896 u32 bit;
1897 int i;
1898
1899 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
1900 /* Get the PEBS gp_regs array index */
1901 int n = pebs_gp_regs[i] - 1;
1902
1903 if (n < 0)
1904 continue;
1905 /*
1906 * Add only registers that were requested (i.e. 'regs_mask') and
1907 * that were provided (i.e. 'mask'), and update the resulting
1908 * mask (i.e. 'intr_regs->mask') accordingly.
1909 */
1910 if (mask & 1 << n && regs_mask & bit) {
1911 intr_regs->mask |= bit;
1912 *pos++ = gp_regs[n];
1913 }
1914 }
1915
1916 return pos;
1917}
1918
Adrian Hunter143d34a2019-06-10 10:28:00 +03001919#ifndef PERF_REG_X86_XMM0
1920#define PERF_REG_X86_XMM0 32
1921#endif
1922
1923static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
1924 const struct intel_pt_blk_items *items,
1925 u64 regs_mask)
1926{
1927 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
1928 const u64 *xmm = items->xmm;
1929
1930 /*
1931 * If there are any XMM registers, then there should be all of them.
1932 * Nevertheless, follow the logic to add only registers that were
1933 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
1934 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
1935 */
1936 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
1937
1938 for (; mask; mask >>= 1, xmm++) {
1939 if (mask & 1)
1940 *pos++ = *xmm;
1941 }
1942}
1943
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001944#define LBR_INFO_MISPRED (1ULL << 63)
1945#define LBR_INFO_IN_TX (1ULL << 62)
1946#define LBR_INFO_ABORT (1ULL << 61)
1947#define LBR_INFO_CYCLES 0xffff
1948
1949/* Refer kernel's intel_pmu_store_pebs_lbrs() */
1950static u64 intel_pt_lbr_flags(u64 info)
1951{
1952 union {
1953 struct branch_flags flags;
1954 u64 result;
Kan Liangff165622020-03-19 13:25:12 -07001955 } u;
1956
1957 u.result = 0;
1958 u.flags.mispred = !!(info & LBR_INFO_MISPRED);
1959 u.flags.predicted = !(info & LBR_INFO_MISPRED);
1960 u.flags.in_tx = !!(info & LBR_INFO_IN_TX);
1961 u.flags.abort = !!(info & LBR_INFO_ABORT);
1962 u.flags.cycles = info & LBR_INFO_CYCLES;
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001963
1964 return u.result;
1965}
1966
1967static void intel_pt_add_lbrs(struct branch_stack *br_stack,
1968 const struct intel_pt_blk_items *items)
1969{
1970 u64 *to;
1971 int i;
1972
1973 br_stack->nr = 0;
1974
1975 to = &br_stack->entries[0].from;
1976
1977 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
1978 u32 mask = items->mask[i];
1979 const u64 *from = items->val[i];
1980
1981 for (; mask; mask >>= 3, from += 3) {
1982 if ((mask & 7) == 7) {
1983 *to++ = from[0];
1984 *to++ = from[1];
1985 *to++ = intel_pt_lbr_flags(from[2]);
1986 br_stack->nr += 1;
1987 }
1988 }
1989 }
1990}
1991
Adrian Hunterdedcc0e2021-09-07 19:39:03 +03001992static int intel_pt_do_synth_pebs_sample(struct intel_pt_queue *ptq, struct evsel *evsel, u64 id)
Adrian Huntere62ca652019-06-10 10:27:56 +03001993{
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001994 const struct intel_pt_blk_items *items = &ptq->state->items;
1995 struct perf_sample sample = { .ip = 0, };
1996 union perf_event *event = ptq->event_buf;
1997 struct intel_pt *pt = ptq->pt;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001998 u64 sample_type = evsel->core.attr.sample_type;
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001999 u8 cpumode;
Adrian Hunter4c95ad22020-06-30 16:39:35 +03002000 u64 regs[8 * sizeof(sample.intr_regs.mask)];
Adrian Hunter9d0bc532019-06-10 10:27:58 +03002001
2002 if (intel_pt_skip_event(pt))
2003 return 0;
2004
2005 intel_pt_prep_a_sample(ptq, event, &sample);
2006
2007 sample.id = id;
2008 sample.stream_id = id;
2009
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002010 if (!evsel->core.attr.freq)
2011 sample.period = evsel->core.attr.sample_period;
Adrian Hunter9d0bc532019-06-10 10:27:58 +03002012
2013 /* No support for non-zero CS base */
2014 if (items->has_ip)
2015 sample.ip = items->ip;
2016 else if (items->has_rip)
2017 sample.ip = items->rip;
2018 else
2019 sample.ip = ptq->state->from_ip;
2020
Adrian Hunter6e86bfd2021-02-18 11:57:57 +02002021 cpumode = intel_pt_cpumode(ptq, sample.ip, 0);
Adrian Hunter9d0bc532019-06-10 10:27:58 +03002022
2023 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
2024
2025 sample.cpumode = cpumode;
2026
2027 if (sample_type & PERF_SAMPLE_TIME) {
2028 u64 timestamp = 0;
2029
2030 if (items->has_timestamp)
2031 timestamp = items->timestamp;
2032 else if (!pt->timeless_decoding)
2033 timestamp = ptq->timestamp;
2034 if (timestamp)
2035 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
2036 }
2037
Adrian Huntere01f0ef2019-06-10 10:28:03 +03002038 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
2039 pt->synth_opts.callchain) {
2040 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
2041 pt->synth_opts.callchain_sz, sample.ip,
2042 pt->kernel_start);
2043 sample.callchain = ptq->chain;
2044 }
2045
Adrian Hunter9e9a6182019-06-10 10:27:59 +03002046 if (sample_type & PERF_SAMPLE_REGS_INTR &&
Adrian Hunter4c95ad22020-06-30 16:39:35 +03002047 (items->mask[INTEL_PT_GP_REGS_POS] ||
2048 items->mask[INTEL_PT_XMM_POS])) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002049 u64 regs_mask = evsel->core.attr.sample_regs_intr;
Adrian Hunter143d34a2019-06-10 10:28:00 +03002050 u64 *pos;
Adrian Hunter9e9a6182019-06-10 10:27:59 +03002051
2052 sample.intr_regs.abi = items->is_32_bit ?
2053 PERF_SAMPLE_REGS_ABI_32 :
2054 PERF_SAMPLE_REGS_ABI_64;
2055 sample.intr_regs.regs = regs;
2056
Adrian Hunter143d34a2019-06-10 10:28:00 +03002057 pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
2058
2059 intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
Adrian Hunter9e9a6182019-06-10 10:27:59 +03002060 }
2061
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002062 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002063 if (items->mask[INTEL_PT_LBR_0_POS] ||
2064 items->mask[INTEL_PT_LBR_1_POS] ||
2065 items->mask[INTEL_PT_LBR_2_POS]) {
Adrian Hunter961224d2020-05-16 15:35:48 +03002066 intel_pt_add_lbrs(ptq->last_branch, items);
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002067 } else if (pt->synth_opts.last_branch) {
Adrian Huntercf888e02020-04-29 18:07:45 +03002068 thread_stack__br_sample(ptq->thread, ptq->cpu,
2069 ptq->last_branch,
2070 pt->br_stack_sz);
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002071 } else {
Adrian Hunter961224d2020-05-16 15:35:48 +03002072 ptq->last_branch->nr = 0;
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002073 }
Adrian Hunter961224d2020-05-16 15:35:48 +03002074 sample.branch_stack = ptq->last_branch;
Adrian Hunteraa62afd2019-06-10 10:28:01 +03002075 }
2076
Adrian Hunter975846e2019-06-10 10:28:02 +03002077 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
2078 sample.addr = items->mem_access_address;
2079
Kan Liangea8d0ed2021-02-02 12:09:09 -08002080 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
Adrian Hunter975846e2019-06-10 10:28:02 +03002081 /*
2082 * Refer kernel's setup_pebs_adaptive_sample_data() and
2083 * intel_hsw_weight().
2084 */
Kan Liangea8d0ed2021-02-02 12:09:09 -08002085 if (items->has_mem_access_latency) {
2086 u64 weight = items->mem_access_latency >> 32;
2087
2088 /*
2089 * Starts from SPR, the mem access latency field
2090 * contains both cache latency [47:32] and instruction
2091 * latency [15:0]. The cache latency is the same as the
2092 * mem access latency on previous platforms.
2093 *
2094 * In practice, no memory access could last than 4G
2095 * cycles. Use latency >> 32 to distinguish the
2096 * different format of the mem access latency field.
2097 */
Kan Liang590db422021-02-02 12:09:10 -08002098 if (weight > 0) {
Kan Liangea8d0ed2021-02-02 12:09:09 -08002099 sample.weight = weight & 0xffff;
Kan Liang590db422021-02-02 12:09:10 -08002100 sample.ins_lat = items->mem_access_latency & 0xffff;
2101 } else
Kan Liangea8d0ed2021-02-02 12:09:09 -08002102 sample.weight = items->mem_access_latency;
2103 }
Adrian Hunter975846e2019-06-10 10:28:02 +03002104 if (!sample.weight && items->has_tsx_aux_info) {
2105 /* Cycles last block */
2106 sample.weight = (u32)items->tsx_aux_info;
2107 }
2108 }
2109
2110 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
2111 u64 ax = items->has_rax ? items->rax : 0;
2112 /* Refer kernel's intel_hsw_transaction() */
2113 u64 txn = (u8)(items->tsx_aux_info >> 32);
2114
2115 /* For RTM XABORTs also log the abort code from AX */
2116 if (txn & PERF_TXN_TRANSACTION && ax & 1)
2117 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
2118 sample.transaction = txn;
2119 }
2120
Adrian Huntercf888e02020-04-29 18:07:45 +03002121 return intel_pt_deliver_synth_event(pt, event, &sample, sample_type);
Adrian Huntere62ca652019-06-10 10:27:56 +03002122}
2123
Adrian Hunterdedcc0e2021-09-07 19:39:03 +03002124static int intel_pt_synth_single_pebs_sample(struct intel_pt_queue *ptq)
2125{
2126 struct intel_pt *pt = ptq->pt;
2127 struct evsel *evsel = pt->pebs_evsel;
2128 u64 id = evsel->core.id[0];
2129
2130 return intel_pt_do_synth_pebs_sample(ptq, evsel, id);
2131}
2132
2133static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
2134{
2135 const struct intel_pt_blk_items *items = &ptq->state->items;
2136 struct intel_pt_pebs_event *pe;
2137 struct intel_pt *pt = ptq->pt;
2138 int err = -EINVAL;
2139 int hw_id;
2140
2141 if (!items->has_applicable_counters || !items->applicable_counters) {
2142 if (!pt->single_pebs)
2143 pr_err("PEBS-via-PT record with no applicable_counters\n");
2144 return intel_pt_synth_single_pebs_sample(ptq);
2145 }
2146
2147 for_each_set_bit(hw_id, (unsigned long *)&items->applicable_counters, INTEL_PT_MAX_PEBS) {
2148 pe = &ptq->pebs[hw_id];
2149 if (!pe->evsel) {
2150 if (!pt->single_pebs)
2151 pr_err("PEBS-via-PT record with no matching event, hw_id %d\n",
2152 hw_id);
2153 return intel_pt_synth_single_pebs_sample(ptq);
2154 }
2155 err = intel_pt_do_synth_pebs_sample(ptq, pe->evsel, pe->id);
2156 if (err)
2157 return err;
2158 }
2159
2160 return err;
2161}
2162
Adrian Hunter90e457f2015-07-17 19:33:41 +03002163static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02002164 pid_t pid, pid_t tid, u64 ip, u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002165{
2166 union perf_event event;
2167 char msg[MAX_AUXTRACE_ERROR_MSG];
2168 int err;
2169
Adrian Hunter1d846ae2020-07-10 18:10:58 +03002170 if (pt->synth_opts.error_minus_flags) {
2171 if (code == INTEL_PT_ERR_OVR &&
2172 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_OVERFLOW)
2173 return 0;
2174 if (code == INTEL_PT_ERR_LOST &&
2175 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_DATA_LOST)
2176 return 0;
2177 }
2178
Adrian Hunter90e457f2015-07-17 19:33:41 +03002179 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
2180
2181 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
Adrian Hunter16bd4322019-02-06 12:39:47 +02002182 code, cpu, pid, tid, ip, msg, timestamp);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002183
2184 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
2185 if (err)
2186 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
2187 err);
2188
2189 return err;
2190}
2191
Adrian Hunter16bd4322019-02-06 12:39:47 +02002192static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
2193 const struct intel_pt_state *state)
2194{
2195 struct intel_pt *pt = ptq->pt;
2196 u64 tm = ptq->timestamp;
2197
2198 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
2199
2200 return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
2201 ptq->tid, state->from_ip, tm);
2202}
2203
Adrian Hunter90e457f2015-07-17 19:33:41 +03002204static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
2205{
2206 struct auxtrace_queue *queue;
2207 pid_t tid = ptq->next_tid;
2208 int err;
2209
2210 if (tid == -1)
2211 return 0;
2212
2213 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
2214
2215 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
2216
2217 queue = &pt->queues.queue_array[ptq->queue_nr];
2218 intel_pt_set_pid_tid_cpu(pt, queue);
2219
2220 ptq->next_tid = -1;
2221
2222 return err;
2223}
2224
2225static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
2226{
2227 struct intel_pt *pt = ptq->pt;
2228
2229 return ip == pt->switch_ip &&
2230 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
2231 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
2232 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
2233}
2234
Adrian Hunter37973072017-06-30 11:36:45 +03002235#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03002236 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
Adrian Hunter37973072017-06-30 11:36:45 +03002237
Adrian Hunter90e457f2015-07-17 19:33:41 +03002238static int intel_pt_sample(struct intel_pt_queue *ptq)
2239{
2240 const struct intel_pt_state *state = ptq->state;
2241 struct intel_pt *pt = ptq->pt;
2242 int err;
2243
2244 if (!ptq->have_sample)
2245 return 0;
2246
2247 ptq->have_sample = false;
2248
Adrian Hunterf2b91382021-10-27 11:03:31 +03002249 if (pt->synth_opts.approx_ipc) {
2250 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2251 ptq->ipc_cyc_cnt = ptq->state->cycles;
2252 ptq->sample_ipc = true;
2253 } else {
2254 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2255 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
2256 ptq->sample_ipc = ptq->state->flags & INTEL_PT_SAMPLE_IPC;
2257 }
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03002258
Adrian Huntere62ca652019-06-10 10:27:56 +03002259 /*
2260 * Do PEBS first to allow for the possibility that the PEBS timestamp
2261 * precedes the current timestamp.
2262 */
2263 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
2264 err = intel_pt_synth_pebs_sample(ptq);
2265 if (err)
2266 return err;
2267 }
2268
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03002269 if (pt->sample_pwr_events) {
Adrian Hunterc840cbf2021-02-05 19:53:50 +02002270 if (state->type & INTEL_PT_PSB_EVT) {
2271 err = intel_pt_synth_psb_sample(ptq);
2272 if (err)
2273 return err;
2274 }
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03002275 if (ptq->state->cbr != ptq->cbr_seen) {
Adrian Hunter37973072017-06-30 11:36:45 +03002276 err = intel_pt_synth_cbr_sample(ptq);
2277 if (err)
2278 return err;
2279 }
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03002280 if (state->type & INTEL_PT_PWR_EVT) {
2281 if (state->type & INTEL_PT_MWAIT_OP) {
2282 err = intel_pt_synth_mwait_sample(ptq);
2283 if (err)
2284 return err;
2285 }
2286 if (state->type & INTEL_PT_PWR_ENTRY) {
2287 err = intel_pt_synth_pwre_sample(ptq);
2288 if (err)
2289 return err;
2290 }
2291 if (state->type & INTEL_PT_EX_STOP) {
2292 err = intel_pt_synth_exstop_sample(ptq);
2293 if (err)
2294 return err;
2295 }
2296 if (state->type & INTEL_PT_PWR_EXIT) {
2297 err = intel_pt_synth_pwrx_sample(ptq);
2298 if (err)
2299 return err;
2300 }
Adrian Hunter37973072017-06-30 11:36:45 +03002301 }
2302 }
2303
Adrian Hunter406a1802017-05-26 11:17:29 +03002304 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002305 err = intel_pt_synth_instruction_sample(ptq);
2306 if (err)
2307 return err;
2308 }
2309
Adrian Hunter406a1802017-05-26 11:17:29 +03002310 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002311 err = intel_pt_synth_transaction_sample(ptq);
2312 if (err)
2313 return err;
2314 }
2315
Adrian Hunter37973072017-06-30 11:36:45 +03002316 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
2317 err = intel_pt_synth_ptwrite_sample(ptq);
2318 if (err)
2319 return err;
2320 }
2321
Adrian Hunter90e457f2015-07-17 19:33:41 +03002322 if (!(state->type & INTEL_PT_BRANCH))
2323 return 0;
2324
Adrian Huntercf888e02020-04-29 18:07:45 +03002325 if (pt->use_thread_stack) {
2326 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags,
2327 state->from_ip, state->to_ip, ptq->insn_len,
2328 state->trace_nr, pt->callstack,
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03002329 pt->br_stack_sz_plus,
Adrian Huntercf888e02020-04-29 18:07:45 +03002330 pt->mispred_all);
2331 } else {
Adrian Hunter256d92b2018-12-21 14:06:19 +02002332 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
Adrian Huntercf888e02020-04-29 18:07:45 +03002333 }
Adrian Hunter90e457f2015-07-17 19:33:41 +03002334
2335 if (pt->sample_branches) {
Adrian Hunter19854e42021-02-18 11:58:00 +02002336 if (state->from_nr != state->to_nr &&
2337 state->from_ip && state->to_ip) {
2338 struct intel_pt_state *st = (struct intel_pt_state *)state;
2339 u64 to_ip = st->to_ip;
2340 u64 from_ip = st->from_ip;
2341
2342 /*
2343 * perf cannot handle having different machines for ip
2344 * and addr, so create 2 branches.
2345 */
2346 st->to_ip = 0;
2347 err = intel_pt_synth_branch_sample(ptq);
2348 if (err)
2349 return err;
2350 st->from_ip = 0;
2351 st->to_ip = to_ip;
2352 err = intel_pt_synth_branch_sample(ptq);
2353 st->from_ip = from_ip;
2354 } else {
2355 err = intel_pt_synth_branch_sample(ptq);
2356 }
Adrian Hunter90e457f2015-07-17 19:33:41 +03002357 if (err)
2358 return err;
2359 }
2360
Adrian Hunter63d8e382018-03-07 16:02:22 +02002361 if (!ptq->sync_switch)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002362 return 0;
2363
2364 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
2365 switch (ptq->switch_state) {
Adrian Hunterdbcb82b2018-05-31 13:23:42 +03002366 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002367 case INTEL_PT_SS_UNKNOWN:
2368 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2369 err = intel_pt_next_tid(pt, ptq);
2370 if (err)
2371 return err;
2372 ptq->switch_state = INTEL_PT_SS_TRACING;
2373 break;
2374 default:
2375 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
2376 return 1;
2377 }
2378 } else if (!state->to_ip) {
2379 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2380 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
2381 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2382 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2383 state->to_ip == pt->ptss_ip &&
2384 (ptq->flags & PERF_IP_FLAG_CALL)) {
2385 ptq->switch_state = INTEL_PT_SS_TRACING;
2386 }
2387
2388 return 0;
2389}
2390
Adrian Hunter86c27862015-08-13 12:40:57 +03002391static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002392{
Adrian Hunter86c27862015-08-13 12:40:57 +03002393 struct machine *machine = pt->machine;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002394 struct map *map;
2395 struct symbol *sym, *start;
2396 u64 ip, switch_ip = 0;
Adrian Hunter86c27862015-08-13 12:40:57 +03002397 const char *ptss;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002398
2399 if (ptss_ip)
2400 *ptss_ip = 0;
2401
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002402 map = machine__kernel_map(machine);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002403 if (!map)
2404 return 0;
2405
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002406 if (map__load(map))
Adrian Hunter90e457f2015-07-17 19:33:41 +03002407 return 0;
2408
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -03002409 start = dso__first_symbol(map->dso);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002410
2411 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2412 if (sym->binding == STB_GLOBAL &&
2413 !strcmp(sym->name, "__switch_to")) {
2414 ip = map->unmap_ip(map, sym->start);
2415 if (ip >= map->start && ip < map->end) {
2416 switch_ip = ip;
2417 break;
2418 }
2419 }
2420 }
2421
2422 if (!switch_ip || !ptss_ip)
2423 return 0;
2424
Adrian Hunter86c27862015-08-13 12:40:57 +03002425 if (pt->have_sched_switch == 1)
2426 ptss = "perf_trace_sched_switch";
2427 else
2428 ptss = "__perf_event_task_sched_out";
2429
Adrian Hunter90e457f2015-07-17 19:33:41 +03002430 for (sym = start; sym; sym = dso__next_symbol(sym)) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002431 if (!strcmp(sym->name, ptss)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002432 ip = map->unmap_ip(map, sym->start);
2433 if (ip >= map->start && ip < map->end) {
2434 *ptss_ip = ip;
2435 break;
2436 }
2437 }
2438 }
2439
2440 return switch_ip;
2441}
2442
Adrian Hunter63d8e382018-03-07 16:02:22 +02002443static void intel_pt_enable_sync_switch(struct intel_pt *pt)
2444{
2445 unsigned int i;
2446
2447 pt->sync_switch = true;
2448
2449 for (i = 0; i < pt->queues.nr_queues; i++) {
2450 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2451 struct intel_pt_queue *ptq = queue->priv;
2452
2453 if (ptq)
2454 ptq->sync_switch = true;
2455 }
2456}
2457
Adrian Hunter2c47db92019-06-04 16:00:09 +03002458/*
2459 * To filter against time ranges, it is only necessary to look at the next start
2460 * or end time.
2461 */
2462static bool intel_pt_next_time(struct intel_pt_queue *ptq)
2463{
2464 struct intel_pt *pt = ptq->pt;
2465
2466 if (ptq->sel_start) {
2467 /* Next time is an end time */
2468 ptq->sel_start = false;
2469 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
2470 return true;
2471 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
2472 /* Next time is a start time */
2473 ptq->sel_start = true;
2474 ptq->sel_idx += 1;
2475 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
2476 return true;
2477 }
2478
2479 /* No next time */
2480 return false;
2481}
2482
2483static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
2484{
2485 int err;
2486
2487 while (1) {
2488 if (ptq->sel_start) {
2489 if (ptq->timestamp >= ptq->sel_timestamp) {
2490 /* After start time, so consider next time */
2491 intel_pt_next_time(ptq);
2492 if (!ptq->sel_timestamp) {
2493 /* No end time */
2494 return 0;
2495 }
2496 /* Check against end time */
2497 continue;
2498 }
2499 /* Before start time, so fast forward */
2500 ptq->have_sample = false;
2501 if (ptq->sel_timestamp > *ff_timestamp) {
2502 if (ptq->sync_switch) {
2503 intel_pt_next_tid(ptq->pt, ptq);
2504 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2505 }
2506 *ff_timestamp = ptq->sel_timestamp;
2507 err = intel_pt_fast_forward(ptq->decoder,
2508 ptq->sel_timestamp);
2509 if (err)
2510 return err;
2511 }
2512 return 0;
2513 } else if (ptq->timestamp > ptq->sel_timestamp) {
2514 /* After end time, so consider next time */
2515 if (!intel_pt_next_time(ptq)) {
2516 /* No next time range, so stop decoding */
2517 ptq->have_sample = false;
2518 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2519 return 1;
2520 }
2521 /* Check against next start time */
2522 continue;
2523 } else {
2524 /* Before end time */
2525 return 0;
2526 }
2527 }
2528}
2529
Adrian Hunter90e457f2015-07-17 19:33:41 +03002530static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
2531{
2532 const struct intel_pt_state *state = ptq->state;
2533 struct intel_pt *pt = ptq->pt;
Adrian Hunter2c47db92019-06-04 16:00:09 +03002534 u64 ff_timestamp = 0;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002535 int err;
2536
2537 if (!pt->kernel_start) {
2538 pt->kernel_start = machine__kernel_start(pt->machine);
Adrian Hunter86c27862015-08-13 12:40:57 +03002539 if (pt->per_cpu_mmaps &&
2540 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002541 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
Adrian Huntere3ff42b2021-04-30 10:03:09 +03002542 !pt->sampling_mode && !pt->synth_opts.vm_time_correlation) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002543 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002544 if (pt->switch_ip) {
2545 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
2546 pt->switch_ip, pt->ptss_ip);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002547 intel_pt_enable_sync_switch(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002548 }
2549 }
2550 }
2551
2552 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
2553 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2554 while (1) {
2555 err = intel_pt_sample(ptq);
2556 if (err)
2557 return err;
2558
2559 state = intel_pt_decode(ptq->decoder);
2560 if (state->err) {
2561 if (state->err == INTEL_PT_ERR_NODATA)
2562 return 1;
Adrian Hunter63d8e382018-03-07 16:02:22 +02002563 if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002564 state->from_ip >= pt->kernel_start) {
Adrian Hunter63d8e382018-03-07 16:02:22 +02002565 ptq->sync_switch = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002566 intel_pt_next_tid(pt, ptq);
2567 }
2568 if (pt->synth_opts.errors) {
Adrian Hunter16bd4322019-02-06 12:39:47 +02002569 err = intel_ptq_synth_error(ptq, state);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002570 if (err)
2571 return err;
2572 }
2573 continue;
2574 }
2575
2576 ptq->state = state;
2577 ptq->have_sample = true;
2578 intel_pt_sample_flags(ptq);
2579
2580 /* Use estimated TSC upon return to user space */
2581 if (pt->est_tsc &&
2582 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
2583 state->to_ip && state->to_ip < pt->kernel_start) {
2584 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2585 state->timestamp, state->est_timestamp);
2586 ptq->timestamp = state->est_timestamp;
2587 /* Use estimated TSC in unknown switch state */
Adrian Hunter63d8e382018-03-07 16:02:22 +02002588 } else if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002589 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2590 intel_pt_is_switch_ip(ptq, state->to_ip) &&
2591 ptq->next_tid == -1) {
2592 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2593 state->timestamp, state->est_timestamp);
2594 ptq->timestamp = state->est_timestamp;
2595 } else if (state->timestamp > ptq->timestamp) {
2596 ptq->timestamp = state->timestamp;
2597 }
2598
Adrian Hunter2c47db92019-06-04 16:00:09 +03002599 if (ptq->sel_timestamp) {
2600 err = intel_pt_time_filter(ptq, &ff_timestamp);
2601 if (err)
2602 return err;
2603 }
2604
Adrian Hunter90e457f2015-07-17 19:33:41 +03002605 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
2606 *timestamp = ptq->timestamp;
2607 return 0;
2608 }
2609 }
2610 return 0;
2611}
2612
2613static inline int intel_pt_update_queues(struct intel_pt *pt)
2614{
2615 if (pt->queues.new_data) {
2616 pt->queues.new_data = false;
2617 return intel_pt_setup_queues(pt);
2618 }
2619 return 0;
2620}
2621
2622static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
2623{
2624 unsigned int queue_nr;
2625 u64 ts;
2626 int ret;
2627
2628 while (1) {
2629 struct auxtrace_queue *queue;
2630 struct intel_pt_queue *ptq;
2631
2632 if (!pt->heap.heap_cnt)
2633 return 0;
2634
2635 if (pt->heap.heap_array[0].ordinal >= timestamp)
2636 return 0;
2637
2638 queue_nr = pt->heap.heap_array[0].queue_nr;
2639 queue = &pt->queues.queue_array[queue_nr];
2640 ptq = queue->priv;
2641
2642 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
2643 queue_nr, pt->heap.heap_array[0].ordinal,
2644 timestamp);
2645
2646 auxtrace_heap__pop(&pt->heap);
2647
2648 if (pt->heap.heap_cnt) {
2649 ts = pt->heap.heap_array[0].ordinal + 1;
2650 if (ts > timestamp)
2651 ts = timestamp;
2652 } else {
2653 ts = timestamp;
2654 }
2655
2656 intel_pt_set_pid_tid_cpu(pt, queue);
2657
2658 ret = intel_pt_run_decoder(ptq, &ts);
2659
2660 if (ret < 0) {
2661 auxtrace_heap__add(&pt->heap, queue_nr, ts);
2662 return ret;
2663 }
2664
2665 if (!ret) {
2666 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
2667 if (ret < 0)
2668 return ret;
2669 } else {
2670 ptq->on_heap = false;
2671 }
2672 }
2673
2674 return 0;
2675}
2676
2677static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
2678 u64 time_)
2679{
2680 struct auxtrace_queues *queues = &pt->queues;
2681 unsigned int i;
2682 u64 ts = 0;
2683
2684 for (i = 0; i < queues->nr_queues; i++) {
2685 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2686 struct intel_pt_queue *ptq = queue->priv;
2687
2688 if (ptq && (tid == -1 || ptq->tid == tid)) {
2689 ptq->time = time_;
2690 intel_pt_set_pid_tid_cpu(pt, queue);
2691 intel_pt_run_decoder(ptq, &ts);
2692 }
2693 }
2694 return 0;
2695}
2696
Adrian Hunterdbd13432019-11-15 14:42:24 +02002697static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq,
2698 struct auxtrace_queue *queue,
2699 struct perf_sample *sample)
2700{
2701 struct machine *m = ptq->pt->machine;
2702
2703 ptq->pid = sample->pid;
2704 ptq->tid = sample->tid;
2705 ptq->cpu = queue->cpu;
2706
2707 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
2708 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2709
2710 thread__zput(ptq->thread);
2711
2712 if (ptq->tid == -1)
2713 return;
2714
2715 if (ptq->pid == -1) {
2716 ptq->thread = machine__find_thread(m, -1, ptq->tid);
2717 if (ptq->thread)
2718 ptq->pid = ptq->thread->pid_;
2719 return;
2720 }
2721
2722 ptq->thread = machine__findnew_thread(m, ptq->pid, ptq->tid);
2723}
2724
2725static int intel_pt_process_timeless_sample(struct intel_pt *pt,
2726 struct perf_sample *sample)
2727{
2728 struct auxtrace_queue *queue;
2729 struct intel_pt_queue *ptq;
2730 u64 ts = 0;
2731
2732 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
2733 if (!queue)
2734 return -EINVAL;
2735
2736 ptq = queue->priv;
2737 if (!ptq)
2738 return 0;
2739
2740 ptq->stop = false;
2741 ptq->time = sample->time;
2742 intel_pt_sample_set_pid_tid_cpu(ptq, queue, sample);
2743 intel_pt_run_decoder(ptq, &ts);
2744 return 0;
2745}
2746
Adrian Hunter90e457f2015-07-17 19:33:41 +03002747static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
2748{
2749 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02002750 sample->pid, sample->tid, 0, sample->time);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002751}
2752
2753static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
2754{
2755 unsigned i, j;
2756
2757 if (cpu < 0 || !pt->queues.nr_queues)
2758 return NULL;
2759
2760 if ((unsigned)cpu >= pt->queues.nr_queues)
2761 i = pt->queues.nr_queues - 1;
2762 else
2763 i = cpu;
2764
2765 if (pt->queues.queue_array[i].cpu == cpu)
2766 return pt->queues.queue_array[i].priv;
2767
2768 for (j = 0; i > 0; j++) {
2769 if (pt->queues.queue_array[--i].cpu == cpu)
2770 return pt->queues.queue_array[i].priv;
2771 }
2772
2773 for (; j < pt->queues.nr_queues; j++) {
2774 if (pt->queues.queue_array[j].cpu == cpu)
2775 return pt->queues.queue_array[j].priv;
2776 }
2777
2778 return NULL;
2779}
2780
Adrian Hunter86c27862015-08-13 12:40:57 +03002781static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
2782 u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002783{
2784 struct intel_pt_queue *ptq;
Adrian Hunter86c27862015-08-13 12:40:57 +03002785 int err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002786
2787 if (!pt->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002788 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002789
2790 ptq = intel_pt_cpu_to_ptq(pt, cpu);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002791 if (!ptq || !ptq->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002792 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002793
2794 switch (ptq->switch_state) {
2795 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002796 break;
2797 case INTEL_PT_SS_UNKNOWN:
2798 case INTEL_PT_SS_TRACING:
2799 ptq->next_tid = tid;
2800 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
2801 return 0;
2802 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2803 if (!ptq->on_heap) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002804 ptq->timestamp = perf_time_to_tsc(timestamp,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002805 &pt->tc);
2806 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
2807 ptq->timestamp);
2808 if (err)
2809 return err;
2810 ptq->on_heap = true;
2811 }
2812 ptq->switch_state = INTEL_PT_SS_TRACING;
2813 break;
2814 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002815 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
2816 break;
2817 default:
2818 break;
2819 }
Adrian Hunter86c27862015-08-13 12:40:57 +03002820
Adrian Hunter14f1cfd2019-04-12 14:38:30 +03002821 ptq->next_tid = -1;
2822
Adrian Hunter86c27862015-08-13 12:40:57 +03002823 return 1;
2824}
2825
2826static int intel_pt_process_switch(struct intel_pt *pt,
2827 struct perf_sample *sample)
2828{
Adrian Hunter86c27862015-08-13 12:40:57 +03002829 pid_t tid;
2830 int cpu, ret;
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03002831 struct evsel *evsel = evlist__id2evsel(pt->session->evlist, sample->id);
Adrian Hunter86c27862015-08-13 12:40:57 +03002832
Adrian Hunter86c27862015-08-13 12:40:57 +03002833 if (evsel != pt->switch_evsel)
2834 return 0;
2835
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -03002836 tid = evsel__intval(evsel, sample, "next_pid");
Adrian Hunter86c27862015-08-13 12:40:57 +03002837 cpu = sample->cpu;
2838
2839 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2840 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
2841 &pt->tc));
2842
2843 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2844 if (ret <= 0)
2845 return ret;
2846
Adrian Hunter90e457f2015-07-17 19:33:41 +03002847 return machine__set_current_tid(pt->machine, cpu, -1, tid);
2848}
2849
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002850static int intel_pt_context_switch_in(struct intel_pt *pt,
2851 struct perf_sample *sample)
2852{
2853 pid_t pid = sample->pid;
2854 pid_t tid = sample->tid;
2855 int cpu = sample->cpu;
2856
2857 if (pt->sync_switch) {
2858 struct intel_pt_queue *ptq;
2859
2860 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2861 if (ptq && ptq->sync_switch) {
2862 ptq->next_tid = -1;
2863 switch (ptq->switch_state) {
2864 case INTEL_PT_SS_NOT_TRACING:
2865 case INTEL_PT_SS_UNKNOWN:
2866 case INTEL_PT_SS_TRACING:
2867 break;
2868 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2869 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2870 ptq->switch_state = INTEL_PT_SS_TRACING;
2871 break;
2872 default:
2873 break;
2874 }
2875 }
2876 }
2877
2878 /*
2879 * If the current tid has not been updated yet, ensure it is now that
2880 * a "switch in" event has occurred.
2881 */
2882 if (machine__get_current_tid(pt->machine, cpu) == tid)
2883 return 0;
2884
2885 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2886}
2887
Adrian Hunter86c27862015-08-13 12:40:57 +03002888static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
2889 struct perf_sample *sample)
2890{
2891 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2892 pid_t pid, tid;
2893 int cpu, ret;
2894
2895 cpu = sample->cpu;
2896
2897 if (pt->have_sched_switch == 3) {
2898 if (!out)
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002899 return intel_pt_context_switch_in(pt, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002900 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
2901 pr_err("Expecting CPU-wide context switch event\n");
2902 return -EINVAL;
2903 }
2904 pid = event->context_switch.next_prev_pid;
2905 tid = event->context_switch.next_prev_tid;
2906 } else {
2907 if (out)
2908 return 0;
2909 pid = sample->pid;
2910 tid = sample->tid;
2911 }
2912
Adrian Hunter7d537a82020-09-09 11:49:23 +03002913 if (tid == -1)
2914 intel_pt_log("context_switch event has no tid\n");
Adrian Hunter86c27862015-08-13 12:40:57 +03002915
Adrian Hunter86c27862015-08-13 12:40:57 +03002916 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2917 if (ret <= 0)
2918 return ret;
2919
2920 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2921}
2922
Adrian Hunter90e457f2015-07-17 19:33:41 +03002923static int intel_pt_process_itrace_start(struct intel_pt *pt,
2924 union perf_event *event,
2925 struct perf_sample *sample)
2926{
2927 if (!pt->per_cpu_mmaps)
2928 return 0;
2929
2930 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2931 sample->cpu, event->itrace_start.pid,
2932 event->itrace_start.tid, sample->time,
2933 perf_time_to_tsc(sample->time, &pt->tc));
2934
2935 return machine__set_current_tid(pt->machine, sample->cpu,
2936 event->itrace_start.pid,
2937 event->itrace_start.tid);
2938}
2939
Adrian Hunterdedcc0e2021-09-07 19:39:03 +03002940static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt,
2941 union perf_event *event,
2942 struct perf_sample *sample)
2943{
2944 u64 hw_id = event->aux_output_hw_id.hw_id;
2945 struct auxtrace_queue *queue;
2946 struct intel_pt_queue *ptq;
2947 struct evsel *evsel;
2948
2949 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
2950 evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id);
2951 if (!queue || !queue->priv || !evsel || hw_id > INTEL_PT_MAX_PEBS) {
2952 pr_err("Bad AUX output hardware ID\n");
2953 return -EINVAL;
2954 }
2955
2956 ptq = queue->priv;
2957
2958 ptq->pebs[hw_id].evsel = evsel;
2959 ptq->pebs[hw_id].id = sample->id;
2960
2961 return 0;
2962}
2963
Adrian Hunterb22f90a2020-05-12 15:19:20 +03002964static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr,
2965 struct addr_location *al)
2966{
2967 if (!al->map || addr < al->map->start || addr >= al->map->end) {
2968 if (!thread__find_map(thread, cpumode, addr, al))
2969 return -1;
2970 }
2971
2972 return 0;
2973}
2974
2975/* Invalidate all instruction cache entries that overlap the text poke */
2976static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)
2977{
2978 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2979 u64 addr = event->text_poke.addr + event->text_poke.new_len - 1;
2980 /* Assume text poke begins in a basic block no more than 4096 bytes */
2981 int cnt = 4096 + event->text_poke.new_len;
2982 struct thread *thread = pt->unknown_thread;
2983 struct addr_location al = { .map = NULL };
2984 struct machine *machine = pt->machine;
2985 struct intel_pt_cache_entry *e;
2986 u64 offset;
2987
2988 if (!event->text_poke.new_len)
2989 return 0;
2990
2991 for (; cnt; cnt--, addr--) {
2992 if (intel_pt_find_map(thread, cpumode, addr, &al)) {
2993 if (addr < event->text_poke.addr)
2994 return 0;
2995 continue;
2996 }
2997
2998 if (!al.map->dso || !al.map->dso->auxtrace_cache)
2999 continue;
3000
3001 offset = al.map->map_ip(al.map, addr);
3002
3003 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
3004 if (!e)
3005 continue;
3006
3007 if (addr + e->byte_cnt + e->length <= event->text_poke.addr) {
3008 /*
3009 * No overlap. Working backwards there cannot be another
3010 * basic block that overlaps the text poke if there is a
3011 * branch instruction before the text poke address.
3012 */
3013 if (e->branch != INTEL_PT_BR_NO_BRANCH)
3014 return 0;
3015 } else {
3016 intel_pt_cache_invalidate(al.map->dso, machine, offset);
3017 intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n",
3018 al.map->dso->long_name, addr);
3019 }
3020 }
3021
3022 return 0;
3023}
3024
Adrian Hunter90e457f2015-07-17 19:33:41 +03003025static int intel_pt_process_event(struct perf_session *session,
3026 union perf_event *event,
3027 struct perf_sample *sample,
3028 struct perf_tool *tool)
3029{
3030 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3031 auxtrace);
3032 u64 timestamp;
3033 int err = 0;
3034
3035 if (dump_trace)
3036 return 0;
3037
3038 if (!tool->ordered_events) {
3039 pr_err("Intel Processor Trace requires ordered events\n");
3040 return -EINVAL;
3041 }
3042
Adrian Hunter81cd60c2015-08-20 11:51:32 +03003043 if (sample->time && sample->time != (u64)-1)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003044 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3045 else
3046 timestamp = 0;
3047
3048 if (timestamp || pt->timeless_decoding) {
3049 err = intel_pt_update_queues(pt);
3050 if (err)
3051 return err;
3052 }
3053
3054 if (pt->timeless_decoding) {
Adrian Hunterdbd13432019-11-15 14:42:24 +02003055 if (pt->sampling_mode) {
3056 if (sample->aux_sample.size)
3057 err = intel_pt_process_timeless_sample(pt,
3058 sample);
3059 } else if (event->header.type == PERF_RECORD_EXIT) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003060 err = intel_pt_process_timeless_queues(pt,
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03003061 event->fork.tid,
Adrian Hunter90e457f2015-07-17 19:33:41 +03003062 sample->time);
3063 }
3064 } else if (timestamp) {
Adrian Hunter5ac35d72021-04-30 10:03:06 +03003065 if (!pt->first_timestamp)
3066 intel_pt_first_timestamp(pt, timestamp);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003067 err = intel_pt_process_queues(pt, timestamp);
3068 }
3069 if (err)
3070 return err;
3071
Adrian Hunter2855c052020-04-01 13:16:08 +03003072 if (event->header.type == PERF_RECORD_SAMPLE) {
3073 if (pt->synth_opts.add_callchain && !sample->callchain)
3074 intel_pt_add_callchain(pt, sample);
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03003075 if (pt->synth_opts.add_last_branch && !sample->branch_stack)
3076 intel_pt_add_br_stack(pt, sample);
Adrian Hunter2855c052020-04-01 13:16:08 +03003077 }
3078
Adrian Hunter90e457f2015-07-17 19:33:41 +03003079 if (event->header.type == PERF_RECORD_AUX &&
3080 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
3081 pt->synth_opts.errors) {
3082 err = intel_pt_lost(pt, sample);
3083 if (err)
3084 return err;
3085 }
3086
3087 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
3088 err = intel_pt_process_switch(pt, sample);
3089 else if (event->header.type == PERF_RECORD_ITRACE_START)
3090 err = intel_pt_process_itrace_start(pt, event, sample);
Adrian Hunterdedcc0e2021-09-07 19:39:03 +03003091 else if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID)
3092 err = intel_pt_process_aux_output_hw_id(pt, event, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03003093 else if (event->header.type == PERF_RECORD_SWITCH ||
3094 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
3095 err = intel_pt_context_switch(pt, event, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003096
Adrian Hunterb22f90a2020-05-12 15:19:20 +03003097 if (!err && event->header.type == PERF_RECORD_TEXT_POKE)
3098 err = intel_pt_text_poke(pt, event);
3099
Adrian Hunterd4575f52020-07-10 18:11:01 +03003100 if (intel_pt_enable_logging && intel_pt_log_events(pt, sample->time)) {
Adrian Hunter8b83fcc2020-07-10 18:11:00 +03003101 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
3102 event->header.type, sample->cpu, sample->time, timestamp);
3103 intel_pt_log_event(event);
3104 }
Adrian Hunter90e457f2015-07-17 19:33:41 +03003105
3106 return err;
3107}
3108
3109static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
3110{
3111 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3112 auxtrace);
3113 int ret;
3114
3115 if (dump_trace)
3116 return 0;
3117
3118 if (!tool->ordered_events)
3119 return -EINVAL;
3120
3121 ret = intel_pt_update_queues(pt);
3122 if (ret < 0)
3123 return ret;
3124
3125 if (pt->timeless_decoding)
3126 return intel_pt_process_timeless_queues(pt, -1,
3127 MAX_TIMESTAMP - 1);
3128
3129 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
3130}
3131
3132static void intel_pt_free_events(struct perf_session *session)
3133{
3134 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3135 auxtrace);
3136 struct auxtrace_queues *queues = &pt->queues;
3137 unsigned int i;
3138
3139 for (i = 0; i < queues->nr_queues; i++) {
3140 intel_pt_free_queue(queues->queue_array[i].priv);
3141 queues->queue_array[i].priv = NULL;
3142 }
3143 intel_pt_log_disable();
3144 auxtrace_queues__free(queues);
3145}
3146
3147static void intel_pt_free(struct perf_session *session)
3148{
3149 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3150 auxtrace);
3151
3152 auxtrace_heap__free(&pt->heap);
3153 intel_pt_free_events(session);
3154 session->auxtrace = NULL;
Adrian Hunter0fc9d332021-04-30 10:03:05 +03003155 intel_pt_free_vmcs_info(pt);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03003156 thread__put(pt->unknown_thread);
Adrian Hunter2acee102016-09-23 17:38:48 +03003157 addr_filters__exit(&pt->filts);
Adrian Hunter2855c052020-04-01 13:16:08 +03003158 zfree(&pt->chain);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003159 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03003160 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003161 free(pt);
3162}
3163
Adrian Hunter6b52bb02020-04-01 13:15:59 +03003164static bool intel_pt_evsel_is_auxtrace(struct perf_session *session,
3165 struct evsel *evsel)
3166{
3167 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3168 auxtrace);
3169
3170 return evsel->core.attr.type == pt->pmu_type;
3171}
3172
Adrian Hunter90e457f2015-07-17 19:33:41 +03003173static int intel_pt_process_auxtrace_event(struct perf_session *session,
3174 union perf_event *event,
3175 struct perf_tool *tool __maybe_unused)
3176{
3177 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3178 auxtrace);
3179
Adrian Hunter90e457f2015-07-17 19:33:41 +03003180 if (!pt->data_queued) {
3181 struct auxtrace_buffer *buffer;
3182 off_t data_offset;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003183 int fd = perf_data__fd(session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003184 int err;
3185
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003186 if (perf_data__is_pipe(session->data)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003187 data_offset = 0;
3188 } else {
3189 data_offset = lseek(fd, 0, SEEK_CUR);
3190 if (data_offset == -1)
3191 return -errno;
3192 }
3193
3194 err = auxtrace_queues__add_event(&pt->queues, session, event,
3195 data_offset, &buffer);
3196 if (err)
3197 return err;
3198
3199 /* Dump here now we have copied a piped trace out of the pipe */
3200 if (dump_trace) {
3201 if (auxtrace_buffer__get_data(buffer, fd)) {
3202 intel_pt_dump_event(pt, buffer->data,
3203 buffer->size);
3204 auxtrace_buffer__put_data(buffer);
3205 }
3206 }
3207 }
3208
3209 return 0;
3210}
3211
Adrian Hunterdbd13432019-11-15 14:42:24 +02003212static int intel_pt_queue_data(struct perf_session *session,
3213 struct perf_sample *sample,
3214 union perf_event *event, u64 data_offset)
3215{
3216 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3217 auxtrace);
3218 u64 timestamp;
3219
3220 if (event) {
3221 return auxtrace_queues__add_event(&pt->queues, session, event,
3222 data_offset, NULL);
3223 }
3224
3225 if (sample->time && sample->time != (u64)-1)
3226 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3227 else
3228 timestamp = 0;
3229
3230 return auxtrace_queues__add_sample(&pt->queues, session, sample,
3231 data_offset, timestamp);
3232}
3233
Adrian Hunter90e457f2015-07-17 19:33:41 +03003234struct intel_pt_synth {
3235 struct perf_tool dummy_tool;
3236 struct perf_session *session;
3237};
3238
3239static int intel_pt_event_synth(struct perf_tool *tool,
3240 union perf_event *event,
3241 struct perf_sample *sample __maybe_unused,
3242 struct machine *machine __maybe_unused)
3243{
3244 struct intel_pt_synth *intel_pt_synth =
3245 container_of(tool, struct intel_pt_synth, dummy_tool);
3246
3247 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
3248 NULL);
3249}
3250
Adrian Hunter63a22cd2017-05-26 11:17:31 +03003251static int intel_pt_synth_event(struct perf_session *session, const char *name,
Adrian Hunter90e457f2015-07-17 19:33:41 +03003252 struct perf_event_attr *attr, u64 id)
3253{
3254 struct intel_pt_synth intel_pt_synth;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03003255 int err;
3256
3257 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
3258 name, id, (u64)attr->sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003259
3260 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
3261 intel_pt_synth.session = session;
3262
Adrian Hunter63a22cd2017-05-26 11:17:31 +03003263 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
3264 &id, intel_pt_event_synth);
3265 if (err)
3266 pr_err("%s: failed to synthesize '%s' event type\n",
3267 __func__, name);
3268
3269 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003270}
3271
Jiri Olsa63503db2019-07-21 13:23:52 +02003272static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03003273 const char *name)
3274{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003275 struct evsel *evsel;
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03003276
3277 evlist__for_each_entry(evlist, evsel) {
Jiri Olsadeaf3212019-09-02 22:12:26 +02003278 if (evsel->core.id && evsel->core.id[0] == id) {
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03003279 if (evsel->name)
3280 zfree(&evsel->name);
3281 evsel->name = strdup(name);
3282 break;
3283 }
3284 }
3285}
3286
Jiri Olsa32dcd022019-07-21 13:23:51 +02003287static struct evsel *intel_pt_evsel(struct intel_pt *pt,
Jiri Olsa63503db2019-07-21 13:23:52 +02003288 struct evlist *evlist)
Adrian Hunter85a564d2017-05-26 11:17:30 +03003289{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003290 struct evsel *evsel;
Adrian Hunter85a564d2017-05-26 11:17:30 +03003291
3292 evlist__for_each_entry(evlist, evsel) {
Jiri Olsae7eb9002019-09-02 22:15:47 +02003293 if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids)
Adrian Hunter85a564d2017-05-26 11:17:30 +03003294 return evsel;
3295 }
3296
3297 return NULL;
3298}
3299
Adrian Hunter90e457f2015-07-17 19:33:41 +03003300static int intel_pt_synth_events(struct intel_pt *pt,
3301 struct perf_session *session)
3302{
Jiri Olsa63503db2019-07-21 13:23:52 +02003303 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003304 struct evsel *evsel = intel_pt_evsel(pt, evlist);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003305 struct perf_event_attr attr;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003306 u64 id;
3307 int err;
3308
Adrian Hunter85a564d2017-05-26 11:17:30 +03003309 if (!evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003310 pr_debug("There are no selected events with Intel Processor Trace data\n");
3311 return 0;
3312 }
3313
3314 memset(&attr, 0, sizeof(struct perf_event_attr));
3315 attr.size = sizeof(struct perf_event_attr);
3316 attr.type = PERF_TYPE_HARDWARE;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003317 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003318 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
3319 PERF_SAMPLE_PERIOD;
3320 if (pt->timeless_decoding)
3321 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
3322 else
3323 attr.sample_type |= PERF_SAMPLE_TIME;
3324 if (!pt->per_cpu_mmaps)
3325 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003326 attr.exclude_user = evsel->core.attr.exclude_user;
3327 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
3328 attr.exclude_hv = evsel->core.attr.exclude_hv;
3329 attr.exclude_host = evsel->core.attr.exclude_host;
3330 attr.exclude_guest = evsel->core.attr.exclude_guest;
3331 attr.sample_id_all = evsel->core.attr.sample_id_all;
3332 attr.read_format = evsel->core.attr.read_format;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003333
Jiri Olsadeaf3212019-09-02 22:12:26 +02003334 id = evsel->core.id[0] + 1000000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003335 if (!id)
3336 id = 1;
3337
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03003338 if (pt->synth_opts.branches) {
3339 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
3340 attr.sample_period = 1;
3341 attr.sample_type |= PERF_SAMPLE_ADDR;
3342 err = intel_pt_synth_event(session, "branches", &attr, id);
3343 if (err)
3344 return err;
3345 pt->sample_branches = true;
3346 pt->branches_sample_type = attr.sample_type;
3347 pt->branches_id = id;
3348 id += 1;
3349 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
3350 }
3351
3352 if (pt->synth_opts.callchain)
3353 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
Al Granta3473062020-08-19 16:47:51 +08003354 if (pt->synth_opts.last_branch) {
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03003355 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
Al Granta3473062020-08-19 16:47:51 +08003356 /*
3357 * We don't use the hardware index, but the sample generation
3358 * code uses the new format branch_stack with this field,
3359 * so the event attributes must indicate that it's present.
3360 */
3361 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
3362 }
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03003363
Adrian Hunter90e457f2015-07-17 19:33:41 +03003364 if (pt->synth_opts.instructions) {
3365 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
3366 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
3367 attr.sample_period =
3368 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
3369 else
3370 attr.sample_period = pt->synth_opts.period;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03003371 err = intel_pt_synth_event(session, "instructions", &attr, id);
3372 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003373 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003374 pt->sample_instructions = true;
3375 pt->instructions_sample_type = attr.sample_type;
3376 pt->instructions_id = id;
3377 id += 1;
3378 }
3379
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03003380 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
3381 attr.sample_period = 1;
3382
Adrian Hunter90e457f2015-07-17 19:33:41 +03003383 if (pt->synth_opts.transactions) {
3384 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03003385 err = intel_pt_synth_event(session, "transactions", &attr, id);
3386 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003387 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003388 pt->sample_transactions = true;
Adrian Hunter21160742017-05-26 11:17:18 +03003389 pt->transactions_sample_type = attr.sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003390 pt->transactions_id = id;
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03003391 intel_pt_set_event_name(evlist, id, "transactions");
Adrian Hunter90e457f2015-07-17 19:33:41 +03003392 id += 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003393 }
3394
Adrian Hunter37973072017-06-30 11:36:45 +03003395 attr.type = PERF_TYPE_SYNTH;
3396 attr.sample_type |= PERF_SAMPLE_RAW;
3397
3398 if (pt->synth_opts.ptwrites) {
3399 attr.config = PERF_SYNTH_INTEL_PTWRITE;
3400 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
3401 if (err)
3402 return err;
3403 pt->sample_ptwrites = true;
3404 pt->ptwrites_sample_type = attr.sample_type;
3405 pt->ptwrites_id = id;
3406 intel_pt_set_event_name(evlist, id, "ptwrite");
3407 id += 1;
3408 }
3409
3410 if (pt->synth_opts.pwr_events) {
3411 pt->sample_pwr_events = true;
3412 pt->pwr_events_sample_type = attr.sample_type;
3413
3414 attr.config = PERF_SYNTH_INTEL_CBR;
3415 err = intel_pt_synth_event(session, "cbr", &attr, id);
3416 if (err)
3417 return err;
3418 pt->cbr_id = id;
3419 intel_pt_set_event_name(evlist, id, "cbr");
3420 id += 1;
Adrian Hunterc840cbf2021-02-05 19:53:50 +02003421
3422 attr.config = PERF_SYNTH_INTEL_PSB;
3423 err = intel_pt_synth_event(session, "psb", &attr, id);
3424 if (err)
3425 return err;
3426 pt->psb_id = id;
3427 intel_pt_set_event_name(evlist, id, "psb");
3428 id += 1;
Adrian Hunter37973072017-06-30 11:36:45 +03003429 }
3430
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003431 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & 0x10)) {
Adrian Hunter37973072017-06-30 11:36:45 +03003432 attr.config = PERF_SYNTH_INTEL_MWAIT;
3433 err = intel_pt_synth_event(session, "mwait", &attr, id);
3434 if (err)
3435 return err;
3436 pt->mwait_id = id;
3437 intel_pt_set_event_name(evlist, id, "mwait");
3438 id += 1;
3439
3440 attr.config = PERF_SYNTH_INTEL_PWRE;
3441 err = intel_pt_synth_event(session, "pwre", &attr, id);
3442 if (err)
3443 return err;
3444 pt->pwre_id = id;
3445 intel_pt_set_event_name(evlist, id, "pwre");
3446 id += 1;
3447
3448 attr.config = PERF_SYNTH_INTEL_EXSTOP;
3449 err = intel_pt_synth_event(session, "exstop", &attr, id);
3450 if (err)
3451 return err;
3452 pt->exstop_id = id;
3453 intel_pt_set_event_name(evlist, id, "exstop");
3454 id += 1;
3455
3456 attr.config = PERF_SYNTH_INTEL_PWRX;
3457 err = intel_pt_synth_event(session, "pwrx", &attr, id);
3458 if (err)
3459 return err;
3460 pt->pwrx_id = id;
3461 intel_pt_set_event_name(evlist, id, "pwrx");
3462 id += 1;
3463 }
3464
Adrian Hunter90e457f2015-07-17 19:33:41 +03003465 return 0;
3466}
3467
Adrian Hunter9e64cef2019-08-06 11:46:04 +03003468static void intel_pt_setup_pebs_events(struct intel_pt *pt)
3469{
3470 struct evsel *evsel;
3471
3472 if (!pt->synth_opts.other_events)
3473 return;
3474
3475 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsadeaf3212019-09-02 22:12:26 +02003476 if (evsel->core.attr.aux_output && evsel->core.id) {
Adrian Hunterdedcc0e2021-09-07 19:39:03 +03003477 if (pt->single_pebs) {
3478 pt->single_pebs = false;
3479 return;
3480 }
3481 pt->single_pebs = true;
Adrian Hunter9e64cef2019-08-06 11:46:04 +03003482 pt->sample_pebs = true;
3483 pt->pebs_evsel = evsel;
Adrian Hunter9e64cef2019-08-06 11:46:04 +03003484 }
3485 }
3486}
3487
Jiri Olsa63503db2019-07-21 13:23:52 +02003488static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003489{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003490 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003491
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003492 evlist__for_each_entry_reverse(evlist, evsel) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03003493 const char *name = evsel__name(evsel);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003494
3495 if (!strcmp(name, "sched:sched_switch"))
3496 return evsel;
3497 }
3498
3499 return NULL;
3500}
3501
Jiri Olsa63503db2019-07-21 13:23:52 +02003502static bool intel_pt_find_switch(struct evlist *evlist)
Adrian Hunter86c27862015-08-13 12:40:57 +03003503{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003504 struct evsel *evsel;
Adrian Hunter86c27862015-08-13 12:40:57 +03003505
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003506 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003507 if (evsel->core.attr.context_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03003508 return true;
3509 }
3510
3511 return false;
3512}
3513
Adrian Hunterba11ba62015-09-25 16:15:56 +03003514static int intel_pt_perf_config(const char *var, const char *value, void *data)
3515{
3516 struct intel_pt *pt = data;
3517
3518 if (!strcmp(var, "intel-pt.mispred-all"))
3519 pt->mispred_all = perf_config_bool(var, value);
3520
Adrian Hunterb4b046f2021-07-01 20:51:32 +03003521 if (!strcmp(var, "intel-pt.max-loops"))
3522 perf_config_int(&pt->max_loops, var, value);
3523
Adrian Hunterba11ba62015-09-25 16:15:56 +03003524 return 0;
3525}
3526
Adrian Hunter2c47db92019-06-04 16:00:09 +03003527/* Find least TSC which converts to ns or later */
3528static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
3529{
3530 u64 tsc, tm;
3531
3532 tsc = perf_time_to_tsc(ns, &pt->tc);
3533
3534 while (1) {
3535 tm = tsc_to_perf_time(tsc, &pt->tc);
3536 if (tm < ns)
3537 break;
3538 tsc -= 1;
3539 }
3540
3541 while (tm < ns)
3542 tm = tsc_to_perf_time(++tsc, &pt->tc);
3543
3544 return tsc;
3545}
3546
3547/* Find greatest TSC which converts to ns or earlier */
3548static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
3549{
3550 u64 tsc, tm;
3551
3552 tsc = perf_time_to_tsc(ns, &pt->tc);
3553
3554 while (1) {
3555 tm = tsc_to_perf_time(tsc, &pt->tc);
3556 if (tm > ns)
3557 break;
3558 tsc += 1;
3559 }
3560
3561 while (tm > ns)
3562 tm = tsc_to_perf_time(--tsc, &pt->tc);
3563
3564 return tsc;
3565}
3566
3567static int intel_pt_setup_time_ranges(struct intel_pt *pt,
3568 struct itrace_synth_opts *opts)
3569{
3570 struct perf_time_interval *p = opts->ptime_range;
3571 int n = opts->range_num;
3572 int i;
3573
3574 if (!n || !p || pt->timeless_decoding)
3575 return 0;
3576
3577 pt->time_ranges = calloc(n, sizeof(struct range));
3578 if (!pt->time_ranges)
3579 return -ENOMEM;
3580
3581 pt->range_cnt = n;
3582
3583 intel_pt_log("%s: %u range(s)\n", __func__, n);
3584
3585 for (i = 0; i < n; i++) {
3586 struct range *r = &pt->time_ranges[i];
3587 u64 ts = p[i].start;
3588 u64 te = p[i].end;
3589
3590 /*
3591 * Take care to ensure the TSC range matches the perf-time range
3592 * when converted back to perf-time.
3593 */
3594 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
3595 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
3596
3597 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
3598 i, ts, te);
3599 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
3600 i, r->start, r->end);
3601 }
3602
3603 return 0;
3604}
3605
Adrian Huntere3ff42b2021-04-30 10:03:09 +03003606static int intel_pt_parse_vm_tm_corr_arg(struct intel_pt *pt, char **args)
3607{
3608 struct intel_pt_vmcs_info *vmcs_info;
3609 u64 tsc_offset, vmcs;
3610 char *p = *args;
3611
3612 errno = 0;
3613
3614 p = skip_spaces(p);
3615 if (!*p)
3616 return 1;
3617
3618 tsc_offset = strtoull(p, &p, 0);
3619 if (errno)
3620 return -errno;
3621 p = skip_spaces(p);
3622 if (*p != ':') {
3623 pt->dflt_tsc_offset = tsc_offset;
3624 *args = p;
3625 return 0;
3626 }
3627 while (1) {
3628 vmcs = strtoull(p, &p, 0);
3629 if (errno)
3630 return -errno;
3631 if (!vmcs)
3632 return -EINVAL;
3633 vmcs_info = intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, tsc_offset);
3634 if (!vmcs_info)
3635 return -ENOMEM;
3636 p = skip_spaces(p);
3637 if (*p != ',')
3638 break;
3639 p += 1;
3640 }
3641 *args = p;
3642 return 0;
3643}
3644
3645static int intel_pt_parse_vm_tm_corr_args(struct intel_pt *pt)
3646{
3647 char *args = pt->synth_opts.vm_tm_corr_args;
3648 int ret;
3649
3650 if (!args)
3651 return 0;
3652
3653 do {
3654 ret = intel_pt_parse_vm_tm_corr_arg(pt, &args);
3655 } while (!ret);
3656
3657 if (ret < 0) {
3658 pr_err("Failed to parse VM Time Correlation options\n");
3659 return ret;
3660 }
3661
3662 return 0;
3663}
3664
Adrian Hunter90e457f2015-07-17 19:33:41 +03003665static const char * const intel_pt_info_fmts[] = {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03003666 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
3667 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
3668 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
3669 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
3670 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
3671 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
3672 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
3673 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
3674 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
3675 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
3676 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
3677 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
3678 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
3679 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003680 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003681 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
Adrian Hunter90e457f2015-07-17 19:33:41 +03003682};
3683
Jiri Olsa9a8dad02019-08-28 15:57:02 +02003684static void intel_pt_print_info(__u64 *arr, int start, int finish)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003685{
3686 int i;
3687
3688 if (!dump_trace)
3689 return;
3690
3691 for (i = start; i <= finish; i++)
3692 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
3693}
3694
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003695static void intel_pt_print_info_str(const char *name, const char *str)
3696{
3697 if (!dump_trace)
3698 return;
3699
3700 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
3701}
3702
Jiri Olsa72932372019-08-28 15:57:16 +02003703static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
Adrian Hunter40b746a2016-09-23 17:38:44 +03003704{
3705 return auxtrace_info->header.size >=
Jiri Olsa72932372019-08-28 15:57:16 +02003706 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
Adrian Hunter40b746a2016-09-23 17:38:44 +03003707}
3708
Adrian Hunter90e457f2015-07-17 19:33:41 +03003709int intel_pt_process_auxtrace_info(union perf_event *event,
3710 struct perf_session *session)
3711{
Jiri Olsa72932372019-08-28 15:57:16 +02003712 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003713 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
3714 struct intel_pt *pt;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003715 void *info_end;
Jiri Olsa9a8dad02019-08-28 15:57:02 +02003716 __u64 *info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003717 int err;
3718
Jiri Olsa72932372019-08-28 15:57:16 +02003719 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
Adrian Hunter90e457f2015-07-17 19:33:41 +03003720 min_sz)
3721 return -EINVAL;
3722
3723 pt = zalloc(sizeof(struct intel_pt));
3724 if (!pt)
3725 return -ENOMEM;
3726
Adrian Hunter0fc9d332021-04-30 10:03:05 +03003727 pt->vmcs_info = RB_ROOT;
3728
Adrian Hunter2acee102016-09-23 17:38:48 +03003729 addr_filters__init(&pt->filts);
3730
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03003731 err = perf_config(intel_pt_perf_config, pt);
3732 if (err)
3733 goto err_free;
Adrian Hunterba11ba62015-09-25 16:15:56 +03003734
Adrian Hunter90e457f2015-07-17 19:33:41 +03003735 err = auxtrace_queues__init(&pt->queues);
3736 if (err)
3737 goto err_free;
3738
Adrian Hunter856ecd62021-04-30 10:02:59 +03003739 if (session->itrace_synth_opts->set) {
3740 pt->synth_opts = *session->itrace_synth_opts;
3741 } else {
3742 struct itrace_synth_opts *opts = session->itrace_synth_opts;
3743
3744 itrace_synth_opts__set_default(&pt->synth_opts, opts->default_no_sample);
3745 if (!opts->default_no_sample && !opts->inject) {
3746 pt->synth_opts.branches = false;
3747 pt->synth_opts.callchain = true;
3748 pt->synth_opts.add_callchain = true;
3749 }
3750 pt->synth_opts.thread_stack = opts->thread_stack;
3751 }
3752
Adrian Hunter624ff632021-10-27 11:03:34 +03003753 if (!(pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT))
3754 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
3755
Adrian Hunter90e457f2015-07-17 19:33:41 +03003756 pt->session = session;
3757 pt->machine = &session->machines.host; /* No kvm support */
3758 pt->auxtrace_type = auxtrace_info->type;
3759 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
3760 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
3761 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
3762 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
3763 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
3764 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
3765 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
3766 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
3767 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
3768 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
3769 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
3770 INTEL_PT_PER_CPU_MMAPS);
3771
Adrian Hunter40b746a2016-09-23 17:38:44 +03003772 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03003773 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
3774 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
3775 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
3776 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
3777 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
3778 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
3779 INTEL_PT_CYC_BIT);
3780 }
3781
Adrian Hunter40b746a2016-09-23 17:38:44 +03003782 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003783 pt->max_non_turbo_ratio =
3784 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
3785 intel_pt_print_info(&auxtrace_info->priv[0],
3786 INTEL_PT_MAX_NONTURBO_RATIO,
3787 INTEL_PT_MAX_NONTURBO_RATIO);
3788 }
3789
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003790 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
3791 info_end = (void *)info + auxtrace_info->header.size;
3792
3793 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
3794 size_t len;
3795
3796 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
3797 intel_pt_print_info(&auxtrace_info->priv[0],
3798 INTEL_PT_FILTER_STR_LEN,
3799 INTEL_PT_FILTER_STR_LEN);
3800 if (len) {
3801 const char *filter = (const char *)info;
3802
3803 len = roundup(len + 1, 8);
3804 info += len >> 3;
3805 if ((void *)info > info_end) {
3806 pr_err("%s: bad filter string length\n", __func__);
3807 err = -EINVAL;
3808 goto err_free_queues;
3809 }
3810 pt->filter = memdup(filter, len);
3811 if (!pt->filter) {
3812 err = -ENOMEM;
3813 goto err_free_queues;
3814 }
3815 if (session->header.needs_swap)
3816 mem_bswap_64(pt->filter, len);
3817 if (pt->filter[len - 1]) {
3818 pr_err("%s: filter string not null terminated\n", __func__);
3819 err = -EINVAL;
3820 goto err_free_queues;
3821 }
Adrian Hunter2acee102016-09-23 17:38:48 +03003822 err = addr_filters__parse_bare_filter(&pt->filts,
3823 filter);
3824 if (err)
3825 goto err_free_queues;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003826 }
3827 intel_pt_print_info_str("Filter string", pt->filter);
3828 }
3829
Adrian Hunter90e457f2015-07-17 19:33:41 +03003830 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
Adrian Hunter07633382019-03-01 12:35:36 +02003831 if (pt->timeless_decoding && !pt->tc.time_mult)
3832 pt->tc.time_mult = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003833 pt->have_tsc = intel_pt_have_tsc(pt);
Adrian Hunterdbd13432019-11-15 14:42:24 +02003834 pt->sampling_mode = intel_pt_sampling_mode(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003835 pt->est_tsc = !pt->timeless_decoding;
3836
Adrian Huntere3ff42b2021-04-30 10:03:09 +03003837 if (pt->synth_opts.vm_time_correlation) {
3838 if (pt->timeless_decoding) {
3839 pr_err("Intel PT has no time information for VM Time Correlation\n");
3840 err = -EINVAL;
3841 goto err_free_queues;
3842 }
3843 if (session->itrace_synth_opts->ptime_range) {
3844 pr_err("Time ranges cannot be specified with VM Time Correlation\n");
3845 err = -EINVAL;
3846 goto err_free_queues;
3847 }
3848 /* Currently TSC Offset is calculated using MTC packets */
3849 if (!intel_pt_have_mtc(pt)) {
3850 pr_err("MTC packets must have been enabled for VM Time Correlation\n");
3851 err = -EINVAL;
3852 goto err_free_queues;
3853 }
3854 err = intel_pt_parse_vm_tm_corr_args(pt);
3855 if (err)
3856 goto err_free_queues;
3857 }
3858
Adrian Hunter90e457f2015-07-17 19:33:41 +03003859 pt->unknown_thread = thread__new(999999999, 999999999);
3860 if (!pt->unknown_thread) {
3861 err = -ENOMEM;
3862 goto err_free_queues;
3863 }
Adrian Hunter3a4acda2016-02-01 03:21:04 +00003864
3865 /*
3866 * Since this thread will not be kept in any rbtree not in a
3867 * list, initialize its list node so that at thread__put() the
Ingo Molnar4d39c892021-03-23 17:09:15 +01003868 * current thread lifetime assumption is kept and we don't segfault
Adrian Hunter3a4acda2016-02-01 03:21:04 +00003869 * at list_del_init().
3870 */
3871 INIT_LIST_HEAD(&pt->unknown_thread->node);
3872
Adrian Hunter90e457f2015-07-17 19:33:41 +03003873 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
3874 if (err)
3875 goto err_delete_thread;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03003876 if (thread__init_maps(pt->unknown_thread, pt->machine)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003877 err = -ENOMEM;
3878 goto err_delete_thread;
3879 }
3880
3881 pt->auxtrace.process_event = intel_pt_process_event;
3882 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
Adrian Hunterdbd13432019-11-15 14:42:24 +02003883 pt->auxtrace.queue_data = intel_pt_queue_data;
3884 pt->auxtrace.dump_auxtrace_sample = intel_pt_dump_sample;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003885 pt->auxtrace.flush_events = intel_pt_flush;
3886 pt->auxtrace.free_events = intel_pt_free_events;
3887 pt->auxtrace.free = intel_pt_free;
Adrian Hunter6b52bb02020-04-01 13:15:59 +03003888 pt->auxtrace.evsel_is_auxtrace = intel_pt_evsel_is_auxtrace;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003889 session->auxtrace = &pt->auxtrace;
3890
3891 if (dump_trace)
3892 return 0;
3893
3894 if (pt->have_sched_switch == 1) {
3895 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
3896 if (!pt->switch_evsel) {
3897 pr_err("%s: missing sched_switch event\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003898 err = -EINVAL;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003899 goto err_delete_thread;
3900 }
Adrian Hunter86c27862015-08-13 12:40:57 +03003901 } else if (pt->have_sched_switch == 2 &&
3902 !intel_pt_find_switch(session->evlist)) {
3903 pr_err("%s: missing context_switch attribute flag\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003904 err = -EINVAL;
Adrian Hunter86c27862015-08-13 12:40:57 +03003905 goto err_delete_thread;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003906 }
3907
Adrian Hunter90e457f2015-07-17 19:33:41 +03003908 if (pt->synth_opts.log)
3909 intel_pt_log_enable();
3910
3911 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
3912 if (pt->tc.time_mult) {
3913 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
3914
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003915 if (!pt->max_non_turbo_ratio)
3916 pt->max_non_turbo_ratio =
3917 (tsc_freq + 50000000) / 100000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003918 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
3919 intel_pt_log("Maximum non-turbo ratio %u\n",
3920 pt->max_non_turbo_ratio);
Adrian Hunter37973072017-06-30 11:36:45 +03003921 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003922 }
3923
Leo Yan323fd742019-07-08 22:39:36 +08003924 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
3925 if (err)
3926 goto err_delete_thread;
Adrian Hunter2c47db92019-06-04 16:00:09 +03003927
Adrian Hunter90e457f2015-07-17 19:33:41 +03003928 if (pt->synth_opts.calls)
3929 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
3930 PERF_IP_FLAG_TRACE_END;
3931 if (pt->synth_opts.returns)
3932 pt->branches_filter |= PERF_IP_FLAG_RETURN |
3933 PERF_IP_FLAG_TRACE_BEGIN;
3934
Adrian Hunter2855c052020-04-01 13:16:08 +03003935 if ((pt->synth_opts.callchain || pt->synth_opts.add_callchain) &&
3936 !symbol_conf.use_callchain) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003937 symbol_conf.use_callchain = true;
3938 if (callchain_register_param(&callchain_param) < 0) {
3939 symbol_conf.use_callchain = false;
3940 pt->synth_opts.callchain = false;
Adrian Hunter2855c052020-04-01 13:16:08 +03003941 pt->synth_opts.add_callchain = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003942 }
3943 }
3944
Adrian Hunter2855c052020-04-01 13:16:08 +03003945 if (pt->synth_opts.add_callchain) {
3946 err = intel_pt_callchain_init(pt);
3947 if (err)
3948 goto err_delete_thread;
3949 }
3950
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03003951 if (pt->synth_opts.last_branch || pt->synth_opts.add_last_branch) {
Adrian Huntercf888e02020-04-29 18:07:45 +03003952 pt->br_stack_sz = pt->synth_opts.last_branch_sz;
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03003953 pt->br_stack_sz_plus = pt->br_stack_sz;
3954 }
3955
3956 if (pt->synth_opts.add_last_branch) {
3957 err = intel_pt_br_stack_init(pt);
3958 if (err)
3959 goto err_delete_thread;
3960 /*
3961 * Additional branch stack size to cater for tracing from the
3962 * actual sample ip to where the sample time is recorded.
3963 * Measured at about 200 branches, but generously set to 1024.
3964 * If kernel space is not being traced, then add just 1 for the
3965 * branch to kernel space.
3966 */
3967 if (intel_pt_tracing_kernel(pt))
3968 pt->br_stack_sz_plus += 1024;
3969 else
3970 pt->br_stack_sz_plus += 1;
3971 }
Adrian Huntercf888e02020-04-29 18:07:45 +03003972
Adrian Hunter1ef998f2020-04-29 18:07:44 +03003973 pt->use_thread_stack = pt->synth_opts.callchain ||
3974 pt->synth_opts.add_callchain ||
Adrian Huntercf888e02020-04-29 18:07:45 +03003975 pt->synth_opts.thread_stack ||
Adrian Hunterf0a0251c2020-04-29 18:07:49 +03003976 pt->synth_opts.last_branch ||
3977 pt->synth_opts.add_last_branch;
Adrian Huntercf888e02020-04-29 18:07:45 +03003978
3979 pt->callstack = pt->synth_opts.callchain ||
3980 pt->synth_opts.add_callchain ||
3981 pt->synth_opts.thread_stack;
Adrian Hunter1ef998f2020-04-29 18:07:44 +03003982
Adrian Hunter90e457f2015-07-17 19:33:41 +03003983 err = intel_pt_synth_events(pt, session);
3984 if (err)
3985 goto err_delete_thread;
3986
Adrian Hunter9e64cef2019-08-06 11:46:04 +03003987 intel_pt_setup_pebs_events(pt);
3988
Adrian Hunterdbd13432019-11-15 14:42:24 +02003989 if (pt->sampling_mode || list_empty(&session->auxtrace_index))
3990 err = auxtrace_queue_data(session, true, true);
3991 else
3992 err = auxtrace_queues__process_index(&pt->queues, session);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003993 if (err)
3994 goto err_delete_thread;
3995
3996 if (pt->queues.populated)
3997 pt->data_queued = true;
3998
3999 if (pt->timeless_decoding)
4000 pr_debug2("Intel PT decoding without timestamps\n");
4001
4002 return 0;
4003
4004err_delete_thread:
Adrian Hunter2855c052020-04-01 13:16:08 +03004005 zfree(&pt->chain);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03004006 thread__zput(pt->unknown_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03004007err_free_queues:
4008 intel_pt_log_disable();
4009 auxtrace_queues__free(&pt->queues);
4010 session->auxtrace = NULL;
4011err_free:
Adrian Hunter2acee102016-09-23 17:38:48 +03004012 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03004013 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03004014 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03004015 free(pt);
4016 return err;
4017}