blob: 825a6a3b03a1696b4b63a3593836d2ab912fb64c [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>
12#include <linux/types.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030013#include <linux/zalloc.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +030014
15#include "../perf.h"
16#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"
Adrian Hunter2c47db92019-06-04 16:00:09 +030036#include "time-utils.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030037
Adrian Hunter9e9a6182019-06-10 10:27:59 +030038#include "../arch/x86/include/uapi/asm/perf_regs.h"
39
Adrian Hunter90e457f2015-07-17 19:33:41 +030040#include "intel-pt-decoder/intel-pt-log.h"
41#include "intel-pt-decoder/intel-pt-decoder.h"
42#include "intel-pt-decoder/intel-pt-insn-decoder.h"
43#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
44
45#define MAX_TIMESTAMP (~0ULL)
46
Adrian Hunter2c47db92019-06-04 16:00:09 +030047struct range {
48 u64 start;
49 u64 end;
50};
51
Adrian Hunter90e457f2015-07-17 19:33:41 +030052struct intel_pt {
53 struct auxtrace auxtrace;
54 struct auxtrace_queues queues;
55 struct auxtrace_heap heap;
56 u32 auxtrace_type;
57 struct perf_session *session;
58 struct machine *machine;
Jiri Olsa32dcd022019-07-21 13:23:51 +020059 struct evsel *switch_evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +030060 struct thread *unknown_thread;
61 bool timeless_decoding;
62 bool sampling_mode;
63 bool snapshot_mode;
64 bool per_cpu_mmaps;
65 bool have_tsc;
66 bool data_queued;
67 bool est_tsc;
68 bool sync_switch;
Adrian Hunterba11ba62015-09-25 16:15:56 +030069 bool mispred_all;
Adrian Hunter90e457f2015-07-17 19:33:41 +030070 int have_sched_switch;
71 u32 pmu_type;
72 u64 kernel_start;
73 u64 switch_ip;
74 u64 ptss_ip;
75
76 struct perf_tsc_conversion tc;
77 bool cap_user_time_zero;
78
79 struct itrace_synth_opts synth_opts;
80
81 bool sample_instructions;
82 u64 instructions_sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +030083 u64 instructions_id;
84
85 bool sample_branches;
86 u32 branches_filter;
87 u64 branches_sample_type;
88 u64 branches_id;
89
90 bool sample_transactions;
91 u64 transactions_sample_type;
92 u64 transactions_id;
93
Adrian Hunter37973072017-06-30 11:36:45 +030094 bool sample_ptwrites;
95 u64 ptwrites_sample_type;
96 u64 ptwrites_id;
97
98 bool sample_pwr_events;
99 u64 pwr_events_sample_type;
100 u64 mwait_id;
101 u64 pwre_id;
102 u64 exstop_id;
103 u64 pwrx_id;
104 u64 cbr_id;
105
Adrian Huntere62ca652019-06-10 10:27:56 +0300106 bool sample_pebs;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200107 struct evsel *pebs_evsel;
Adrian Huntere62ca652019-06-10 10:27:56 +0300108
Adrian Hunter90e457f2015-07-17 19:33:41 +0300109 u64 tsc_bit;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300110 u64 mtc_bit;
111 u64 mtc_freq_bits;
112 u32 tsc_ctc_ratio_n;
113 u32 tsc_ctc_ratio_d;
114 u64 cyc_bit;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300115 u64 noretcomp_bit;
116 unsigned max_non_turbo_ratio;
Adrian Hunter37973072017-06-30 11:36:45 +0300117 unsigned cbr2khz;
Andi Kleend1706b32016-03-28 10:45:38 -0700118
119 unsigned long num_events;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +0300120
121 char *filter;
Adrian Hunter2acee102016-09-23 17:38:48 +0300122 struct addr_filters filts;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300123
124 struct range *time_ranges;
125 unsigned int range_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300126};
127
128enum switch_state {
129 INTEL_PT_SS_NOT_TRACING,
130 INTEL_PT_SS_UNKNOWN,
131 INTEL_PT_SS_TRACING,
132 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
133 INTEL_PT_SS_EXPECTING_SWITCH_IP,
134};
135
136struct intel_pt_queue {
137 struct intel_pt *pt;
138 unsigned int queue_nr;
139 struct auxtrace_buffer *buffer;
Adrian Hunter9c665062018-03-07 16:02:27 +0200140 struct auxtrace_buffer *old_buffer;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300141 void *decoder;
142 const struct intel_pt_state *state;
143 struct ip_callchain *chain;
Adrian Hunterf14445e2015-09-25 16:15:45 +0300144 struct branch_stack *last_branch;
145 struct branch_stack *last_branch_rb;
146 size_t last_branch_pos;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300147 union perf_event *event_buf;
148 bool on_heap;
149 bool stop;
150 bool step_through_buffers;
151 bool use_buffer_pid_tid;
Adrian Hunter63d8e382018-03-07 16:02:22 +0200152 bool sync_switch;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300153 pid_t pid, tid;
154 int cpu;
155 int switch_state;
156 pid_t next_tid;
157 struct thread *thread;
158 bool exclude_kernel;
159 bool have_sample;
160 u64 time;
161 u64 timestamp;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300162 u64 sel_timestamp;
163 bool sel_start;
164 unsigned int sel_idx;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300165 u32 flags;
166 u16 insn_len;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300167 u64 last_insn_cnt;
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +0300168 u64 ipc_insn_cnt;
169 u64 ipc_cyc_cnt;
170 u64 last_in_insn_cnt;
171 u64 last_in_cyc_cnt;
172 u64 last_br_insn_cnt;
173 u64 last_br_cyc_cnt;
Adrian Hunter5fe2cf72019-06-22 12:32:45 +0300174 unsigned int cbr_seen;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300175 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300176};
177
178static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
179 unsigned char *buf, size_t len)
180{
181 struct intel_pt_pkt packet;
182 size_t pos = 0;
183 int ret, pkt_len, i;
184 char desc[INTEL_PT_PKT_DESC_MAX];
185 const char *color = PERF_COLOR_BLUE;
Adrian Hunteredff7802019-06-10 10:27:53 +0300186 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300187
188 color_fprintf(stdout, color,
189 ". ... Intel Processor Trace data: size %zu bytes\n",
190 len);
191
192 while (len) {
Adrian Hunteredff7802019-06-10 10:27:53 +0300193 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300194 if (ret > 0)
195 pkt_len = ret;
196 else
197 pkt_len = 1;
198 printf(".");
199 color_fprintf(stdout, color, " %08x: ", pos);
200 for (i = 0; i < pkt_len; i++)
201 color_fprintf(stdout, color, " %02x", buf[i]);
202 for (; i < 16; i++)
203 color_fprintf(stdout, color, " ");
204 if (ret > 0) {
205 ret = intel_pt_pkt_desc(&packet, desc,
206 INTEL_PT_PKT_DESC_MAX);
207 if (ret > 0)
208 color_fprintf(stdout, color, " %s\n", desc);
209 } else {
210 color_fprintf(stdout, color, " Bad packet!\n");
211 }
212 pos += pkt_len;
213 buf += pkt_len;
214 len -= pkt_len;
215 }
216}
217
218static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
219 size_t len)
220{
221 printf(".\n");
222 intel_pt_dump(pt, buf, len);
223}
224
Adrian Hunter93f8be22018-11-05 09:35:04 +0200225static void intel_pt_log_event(union perf_event *event)
226{
227 FILE *f = intel_pt_log_fp();
228
229 if (!intel_pt_enable_logging || !f)
230 return;
231
232 perf_event__fprintf(event, f);
233}
234
Adrian Hunter90e457f2015-07-17 19:33:41 +0300235static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
236 struct auxtrace_buffer *b)
237{
Adrian Hunter117db4b2018-03-07 16:02:21 +0200238 bool consecutive = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300239 void *start;
240
241 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
Adrian Hunter117db4b2018-03-07 16:02:21 +0200242 pt->have_tsc, &consecutive);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300243 if (!start)
244 return -EINVAL;
245 b->use_size = b->data + b->size - start;
246 b->use_data = start;
Adrian Hunter117db4b2018-03-07 16:02:21 +0200247 if (b->use_size && consecutive)
248 b->consecutive = true;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300249 return 0;
250}
251
Adrian Huntere96f7df2019-06-04 16:00:07 +0300252static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
253 struct auxtrace_buffer *buffer,
254 struct auxtrace_buffer *old_buffer,
255 struct intel_pt_buffer *b)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300256{
Adrian Hunter599a5be2018-03-07 16:02:29 +0200257 bool might_overlap;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300258
Adrian Hunter90e457f2015-07-17 19:33:41 +0300259 if (!buffer->data) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100260 int fd = perf_data__fd(ptq->pt->session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300261
262 buffer->data = auxtrace_buffer__get_data(buffer, fd);
263 if (!buffer->data)
264 return -ENOMEM;
265 }
266
Adrian Hunter599a5be2018-03-07 16:02:29 +0200267 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
268 if (might_overlap && !buffer->consecutive && old_buffer &&
Adrian Hunter90e457f2015-07-17 19:33:41 +0300269 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
270 return -ENOMEM;
271
Adrian Hunter90e457f2015-07-17 19:33:41 +0300272 if (buffer->use_data) {
273 b->len = buffer->use_size;
274 b->buf = buffer->use_data;
275 } else {
276 b->len = buffer->size;
277 b->buf = buffer->data;
278 }
279 b->ref_timestamp = buffer->reference;
280
Adrian Hunter599a5be2018-03-07 16:02:29 +0200281 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300282 b->consecutive = false;
283 b->trace_nr = buffer->buffer_nr + 1;
284 } else {
285 b->consecutive = true;
286 }
287
Adrian Huntere96f7df2019-06-04 16:00:07 +0300288 return 0;
289}
290
Adrian Hunterda9000a2019-06-04 16:00:08 +0300291/* Do not drop buffers with references - refer intel_pt_get_trace() */
292static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
293 struct auxtrace_buffer *buffer)
294{
295 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
296 return;
297
298 auxtrace_buffer__drop_data(buffer);
299}
300
301/* Must be serialized with respect to intel_pt_get_trace() */
302static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
303 void *cb_data)
304{
305 struct intel_pt_queue *ptq = data;
306 struct auxtrace_buffer *buffer = ptq->buffer;
307 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
308 struct auxtrace_queue *queue;
309 int err = 0;
310
311 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
312
313 while (1) {
314 struct intel_pt_buffer b = { .len = 0 };
315
316 buffer = auxtrace_buffer__next(queue, buffer);
317 if (!buffer)
318 break;
319
320 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
321 if (err)
322 break;
323
324 if (b.len) {
325 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
326 old_buffer = buffer;
327 } else {
328 intel_pt_lookahead_drop_buffer(ptq, buffer);
329 continue;
330 }
331
332 err = cb(&b, cb_data);
333 if (err)
334 break;
335 }
336
337 if (buffer != old_buffer)
338 intel_pt_lookahead_drop_buffer(ptq, buffer);
339 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
340
341 return err;
342}
343
344/*
345 * This function assumes data is processed sequentially only.
346 * Must be serialized with respect to intel_pt_lookahead()
347 */
Adrian Huntere96f7df2019-06-04 16:00:07 +0300348static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
349{
350 struct intel_pt_queue *ptq = data;
351 struct auxtrace_buffer *buffer = ptq->buffer;
352 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
353 struct auxtrace_queue *queue;
354 int err;
355
356 if (ptq->stop) {
357 b->len = 0;
358 return 0;
359 }
360
361 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
362
363 buffer = auxtrace_buffer__next(queue, buffer);
364 if (!buffer) {
365 if (old_buffer)
366 auxtrace_buffer__drop_data(old_buffer);
367 b->len = 0;
368 return 0;
369 }
370
371 ptq->buffer = buffer;
372
373 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
374 if (err)
375 return err;
376
Adrian Hunter90e457f2015-07-17 19:33:41 +0300377 if (ptq->step_through_buffers)
378 ptq->stop = true;
379
Adrian Hunter9c665062018-03-07 16:02:27 +0200380 if (b->len) {
381 if (old_buffer)
382 auxtrace_buffer__drop_data(old_buffer);
383 ptq->old_buffer = buffer;
384 } else {
385 auxtrace_buffer__drop_data(buffer);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300386 return intel_pt_get_trace(b, data);
Adrian Hunter9c665062018-03-07 16:02:27 +0200387 }
Adrian Hunter90e457f2015-07-17 19:33:41 +0300388
389 return 0;
390}
391
392struct intel_pt_cache_entry {
393 struct auxtrace_cache_entry entry;
394 u64 insn_cnt;
395 u64 byte_cnt;
396 enum intel_pt_insn_op op;
397 enum intel_pt_insn_branch branch;
398 int length;
399 int32_t rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300400 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300401};
402
403static int intel_pt_config_div(const char *var, const char *value, void *data)
404{
405 int *d = data;
406 long val;
407
408 if (!strcmp(var, "intel-pt.cache-divisor")) {
409 val = strtol(value, NULL, 0);
410 if (val > 0 && val <= INT_MAX)
411 *d = val;
412 }
413
414 return 0;
415}
416
417static int intel_pt_cache_divisor(void)
418{
419 static int d;
420
421 if (d)
422 return d;
423
424 perf_config(intel_pt_config_div, &d);
425
426 if (!d)
427 d = 64;
428
429 return d;
430}
431
432static unsigned int intel_pt_cache_size(struct dso *dso,
433 struct machine *machine)
434{
435 off_t size;
436
437 size = dso__data_size(dso, machine);
438 size /= intel_pt_cache_divisor();
439 if (size < 1000)
440 return 10;
441 if (size > (1 << 21))
442 return 21;
443 return 32 - __builtin_clz(size);
444}
445
446static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
447 struct machine *machine)
448{
449 struct auxtrace_cache *c;
450 unsigned int bits;
451
452 if (dso->auxtrace_cache)
453 return dso->auxtrace_cache;
454
455 bits = intel_pt_cache_size(dso, machine);
456
457 /* Ignoring cache creation failure */
458 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
459
460 dso->auxtrace_cache = c;
461
462 return c;
463}
464
465static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
466 u64 offset, u64 insn_cnt, u64 byte_cnt,
467 struct intel_pt_insn *intel_pt_insn)
468{
469 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
470 struct intel_pt_cache_entry *e;
471 int err;
472
473 if (!c)
474 return -ENOMEM;
475
476 e = auxtrace_cache__alloc_entry(c);
477 if (!e)
478 return -ENOMEM;
479
480 e->insn_cnt = insn_cnt;
481 e->byte_cnt = byte_cnt;
482 e->op = intel_pt_insn->op;
483 e->branch = intel_pt_insn->branch;
484 e->length = intel_pt_insn->length;
485 e->rel = intel_pt_insn->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300486 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300487
488 err = auxtrace_cache__add(c, offset, &e->entry);
489 if (err)
490 auxtrace_cache__free_entry(c, e);
491
492 return err;
493}
494
495static struct intel_pt_cache_entry *
496intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
497{
498 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
499
500 if (!c)
501 return NULL;
502
503 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
504}
505
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200506static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip)
507{
508 return ip >= pt->kernel_start ?
509 PERF_RECORD_MISC_KERNEL :
510 PERF_RECORD_MISC_USER;
511}
512
Adrian Hunter90e457f2015-07-17 19:33:41 +0300513static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
514 uint64_t *insn_cnt_ptr, uint64_t *ip,
515 uint64_t to_ip, uint64_t max_insn_cnt,
516 void *data)
517{
518 struct intel_pt_queue *ptq = data;
519 struct machine *machine = ptq->pt->machine;
520 struct thread *thread;
521 struct addr_location al;
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300522 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300523 ssize_t len;
524 int x86_64;
525 u8 cpumode;
526 u64 offset, start_offset, start_ip;
527 u64 insn_cnt = 0;
528 bool one_map = true;
529
Andi Kleenfaaa8762016-10-07 16:42:26 +0300530 intel_pt_insn->length = 0;
531
Adrian Hunter90e457f2015-07-17 19:33:41 +0300532 if (to_ip && *ip == to_ip)
533 goto out_no_cache;
534
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200535 cpumode = intel_pt_cpumode(ptq->pt, *ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300536
537 thread = ptq->thread;
538 if (!thread) {
539 if (cpumode != PERF_RECORD_MISC_KERNEL)
540 return -EINVAL;
541 thread = ptq->pt->unknown_thread;
542 }
543
544 while (1) {
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300545 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300546 return -EINVAL;
547
548 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
549 dso__data_status_seen(al.map->dso,
550 DSO_DATA_STATUS_SEEN_ITRACE))
551 return -ENOENT;
552
553 offset = al.map->map_ip(al.map, *ip);
554
555 if (!to_ip && one_map) {
556 struct intel_pt_cache_entry *e;
557
558 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
559 if (e &&
560 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
561 *insn_cnt_ptr = e->insn_cnt;
562 *ip += e->byte_cnt;
563 intel_pt_insn->op = e->op;
564 intel_pt_insn->branch = e->branch;
565 intel_pt_insn->length = e->length;
566 intel_pt_insn->rel = e->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300567 memcpy(intel_pt_insn->buf, e->insn,
568 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300569 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
570 return 0;
571 }
572 }
573
574 start_offset = offset;
575 start_ip = *ip;
576
577 /* Load maps to ensure dso->is_64_bit has been updated */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300578 map__load(al.map);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300579
580 x86_64 = al.map->dso->is_64_bit;
581
582 while (1) {
583 len = dso__data_read_offset(al.map->dso, machine,
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300584 offset, buf,
585 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300586 if (len <= 0)
587 return -EINVAL;
588
589 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
590 return -EINVAL;
591
592 intel_pt_log_insn(intel_pt_insn, *ip);
593
594 insn_cnt += 1;
595
596 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
597 goto out;
598
599 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
600 goto out_no_cache;
601
602 *ip += intel_pt_insn->length;
603
604 if (to_ip && *ip == to_ip)
605 goto out_no_cache;
606
607 if (*ip >= al.map->end)
608 break;
609
610 offset += intel_pt_insn->length;
611 }
612 one_map = false;
613 }
614out:
615 *insn_cnt_ptr = insn_cnt;
616
617 if (!one_map)
618 goto out_no_cache;
619
620 /*
621 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
622 * entries.
623 */
624 if (to_ip) {
625 struct intel_pt_cache_entry *e;
626
627 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
628 if (e)
629 return 0;
630 }
631
632 /* Ignore cache errors */
633 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
634 *ip - start_ip, intel_pt_insn);
635
636 return 0;
637
638out_no_cache:
639 *insn_cnt_ptr = insn_cnt;
640 return 0;
641}
642
Adrian Hunter2acee102016-09-23 17:38:48 +0300643static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
644 uint64_t offset, const char *filename)
645{
646 struct addr_filter *filt;
647 bool have_filter = false;
648 bool hit_tracestop = false;
649 bool hit_filter = false;
650
651 list_for_each_entry(filt, &pt->filts.head, list) {
652 if (filt->start)
653 have_filter = true;
654
655 if ((filename && !filt->filename) ||
656 (!filename && filt->filename) ||
657 (filename && strcmp(filename, filt->filename)))
658 continue;
659
660 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
661 continue;
662
663 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
664 ip, offset, filename ? filename : "[kernel]",
665 filt->start ? "filter" : "stop",
666 filt->addr, filt->size);
667
668 if (filt->start)
669 hit_filter = true;
670 else
671 hit_tracestop = true;
672 }
673
674 if (!hit_tracestop && !hit_filter)
675 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
676 ip, offset, filename ? filename : "[kernel]");
677
678 return hit_tracestop || (have_filter && !hit_filter);
679}
680
681static int __intel_pt_pgd_ip(uint64_t ip, void *data)
682{
683 struct intel_pt_queue *ptq = data;
684 struct thread *thread;
685 struct addr_location al;
686 u8 cpumode;
687 u64 offset;
688
689 if (ip >= ptq->pt->kernel_start)
690 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
691
692 cpumode = PERF_RECORD_MISC_USER;
693
694 thread = ptq->thread;
695 if (!thread)
696 return -EINVAL;
697
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300698 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
Adrian Hunter2acee102016-09-23 17:38:48 +0300699 return -EINVAL;
700
701 offset = al.map->map_ip(al.map, ip);
702
703 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
704 al.map->dso->long_name);
705}
706
707static bool intel_pt_pgd_ip(uint64_t ip, void *data)
708{
709 return __intel_pt_pgd_ip(ip, data) > 0;
710}
711
Adrian Hunter90e457f2015-07-17 19:33:41 +0300712static bool intel_pt_get_config(struct intel_pt *pt,
713 struct perf_event_attr *attr, u64 *config)
714{
715 if (attr->type == pt->pmu_type) {
716 if (config)
717 *config = attr->config;
718 return true;
719 }
720
721 return false;
722}
723
724static bool intel_pt_exclude_kernel(struct intel_pt *pt)
725{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200726 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300727
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300728 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200729 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
730 !evsel->core.attr.exclude_kernel)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300731 return false;
732 }
733 return true;
734}
735
736static bool intel_pt_return_compression(struct intel_pt *pt)
737{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200738 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300739 u64 config;
740
741 if (!pt->noretcomp_bit)
742 return true;
743
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300744 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200745 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +0300746 (config & pt->noretcomp_bit))
747 return false;
748 }
749 return true;
750}
751
Adrian Hunter83959812017-05-26 11:17:11 +0300752static bool intel_pt_branch_enable(struct intel_pt *pt)
753{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200754 struct evsel *evsel;
Adrian Hunter83959812017-05-26 11:17:11 +0300755 u64 config;
756
757 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200758 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
Adrian Hunter83959812017-05-26 11:17:11 +0300759 (config & 1) && !(config & 0x2000))
760 return false;
761 }
762 return true;
763}
764
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300765static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
766{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200767 struct evsel *evsel;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300768 unsigned int shift;
769 u64 config;
770
771 if (!pt->mtc_freq_bits)
772 return 0;
773
774 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
775 config >>= 1;
776
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300777 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200778 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300779 return (config & pt->mtc_freq_bits) >> shift;
780 }
781 return 0;
782}
783
Adrian Hunter90e457f2015-07-17 19:33:41 +0300784static bool intel_pt_timeless_decoding(struct intel_pt *pt)
785{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200786 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300787 bool timeless_decoding = true;
788 u64 config;
789
790 if (!pt->tsc_bit || !pt->cap_user_time_zero)
791 return true;
792
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300793 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200794 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
Adrian Hunter90e457f2015-07-17 19:33:41 +0300795 return true;
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200796 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300797 if (config & pt->tsc_bit)
798 timeless_decoding = false;
799 else
800 return true;
801 }
802 }
803 return timeless_decoding;
804}
805
806static bool intel_pt_tracing_kernel(struct intel_pt *pt)
807{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200808 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300809
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300810 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200811 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
812 !evsel->core.attr.exclude_kernel)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300813 return true;
814 }
815 return false;
816}
817
818static bool intel_pt_have_tsc(struct intel_pt *pt)
819{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200820 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300821 bool have_tsc = false;
822 u64 config;
823
824 if (!pt->tsc_bit)
825 return false;
826
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300827 evlist__for_each_entry(pt->session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200828 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300829 if (config & pt->tsc_bit)
830 have_tsc = true;
831 else
832 return false;
833 }
834 }
835 return have_tsc;
836}
837
838static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
839{
840 u64 quot, rem;
841
842 quot = ns / pt->tc.time_mult;
843 rem = ns % pt->tc.time_mult;
844 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
845 pt->tc.time_mult;
846}
847
848static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
849 unsigned int queue_nr)
850{
851 struct intel_pt_params params = { .get_trace = 0, };
Adrian Hunter9fb52332018-05-31 13:23:45 +0300852 struct perf_env *env = pt->machine->env;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300853 struct intel_pt_queue *ptq;
854
855 ptq = zalloc(sizeof(struct intel_pt_queue));
856 if (!ptq)
857 return NULL;
858
859 if (pt->synth_opts.callchain) {
860 size_t sz = sizeof(struct ip_callchain);
861
Adrian Hunter24248302018-10-31 11:10:42 +0200862 /* Add 1 to callchain_sz for callchain context */
863 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300864 ptq->chain = zalloc(sz);
865 if (!ptq->chain)
866 goto out_free;
867 }
868
Adrian Hunterf14445e2015-09-25 16:15:45 +0300869 if (pt->synth_opts.last_branch) {
870 size_t sz = sizeof(struct branch_stack);
871
872 sz += pt->synth_opts.last_branch_sz *
873 sizeof(struct branch_entry);
874 ptq->last_branch = zalloc(sz);
875 if (!ptq->last_branch)
876 goto out_free;
877 ptq->last_branch_rb = zalloc(sz);
878 if (!ptq->last_branch_rb)
879 goto out_free;
880 }
881
Adrian Hunter90e457f2015-07-17 19:33:41 +0300882 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
883 if (!ptq->event_buf)
884 goto out_free;
885
886 ptq->pt = pt;
887 ptq->queue_nr = queue_nr;
888 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
889 ptq->pid = -1;
890 ptq->tid = -1;
891 ptq->cpu = -1;
892 ptq->next_tid = -1;
893
894 params.get_trace = intel_pt_get_trace;
895 params.walk_insn = intel_pt_walk_next_insn;
Adrian Hunterda9000a2019-06-04 16:00:08 +0300896 params.lookahead = intel_pt_lookahead;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300897 params.data = ptq;
898 params.return_compression = intel_pt_return_compression(pt);
Adrian Hunter83959812017-05-26 11:17:11 +0300899 params.branch_enable = intel_pt_branch_enable(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300900 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300901 params.mtc_period = intel_pt_mtc_period(pt);
902 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
903 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300904
Adrian Hunter2acee102016-09-23 17:38:48 +0300905 if (pt->filts.cnt > 0)
906 params.pgd_ip = intel_pt_pgd_ip;
907
Adrian Hunter90e457f2015-07-17 19:33:41 +0300908 if (pt->synth_opts.instructions) {
909 if (pt->synth_opts.period) {
910 switch (pt->synth_opts.period_type) {
911 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
912 params.period_type =
913 INTEL_PT_PERIOD_INSTRUCTIONS;
914 params.period = pt->synth_opts.period;
915 break;
916 case PERF_ITRACE_PERIOD_TICKS:
917 params.period_type = INTEL_PT_PERIOD_TICKS;
918 params.period = pt->synth_opts.period;
919 break;
920 case PERF_ITRACE_PERIOD_NANOSECS:
921 params.period_type = INTEL_PT_PERIOD_TICKS;
922 params.period = intel_pt_ns_to_ticks(pt,
923 pt->synth_opts.period);
924 break;
925 default:
926 break;
927 }
928 }
929
930 if (!params.period) {
931 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
Adrian Huntere1791342015-09-25 16:15:32 +0300932 params.period = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300933 }
934 }
935
Adrian Hunter9fb52332018-05-31 13:23:45 +0300936 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
937 params.flags |= INTEL_PT_FUP_WITH_NLIP;
938
Adrian Hunter90e457f2015-07-17 19:33:41 +0300939 ptq->decoder = intel_pt_decoder_new(&params);
940 if (!ptq->decoder)
941 goto out_free;
942
943 return ptq;
944
945out_free:
946 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300947 zfree(&ptq->last_branch);
948 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300949 zfree(&ptq->chain);
950 free(ptq);
951 return NULL;
952}
953
954static void intel_pt_free_queue(void *priv)
955{
956 struct intel_pt_queue *ptq = priv;
957
958 if (!ptq)
959 return;
960 thread__zput(ptq->thread);
961 intel_pt_decoder_free(ptq->decoder);
962 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300963 zfree(&ptq->last_branch);
964 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300965 zfree(&ptq->chain);
966 free(ptq);
967}
968
969static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
970 struct auxtrace_queue *queue)
971{
972 struct intel_pt_queue *ptq = queue->priv;
973
974 if (queue->tid == -1 || pt->have_sched_switch) {
975 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
976 thread__zput(ptq->thread);
977 }
978
979 if (!ptq->thread && ptq->tid != -1)
980 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
981
982 if (ptq->thread) {
983 ptq->pid = ptq->thread->pid_;
984 if (queue->cpu == -1)
985 ptq->cpu = ptq->thread->cpu;
986 }
987}
988
989static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
990{
991 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
992 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
993 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
994 if (ptq->state->to_ip)
995 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
996 PERF_IP_FLAG_ASYNC |
997 PERF_IP_FLAG_INTERRUPT;
998 else
999 ptq->flags = PERF_IP_FLAG_BRANCH |
1000 PERF_IP_FLAG_TRACE_END;
1001 ptq->insn_len = 0;
1002 } else {
1003 if (ptq->state->from_ip)
1004 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1005 else
1006 ptq->flags = PERF_IP_FLAG_BRANCH |
1007 PERF_IP_FLAG_TRACE_BEGIN;
1008 if (ptq->state->flags & INTEL_PT_IN_TX)
1009 ptq->flags |= PERF_IP_FLAG_IN_TX;
1010 ptq->insn_len = ptq->state->insn_len;
Andi Kleenfaaa8762016-10-07 16:42:26 +03001011 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001012 }
Adrian Hunterc6b5da02018-09-20 16:00:47 +03001013
1014 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1015 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1016 if (ptq->state->type & INTEL_PT_TRACE_END)
1017 ptq->flags |= PERF_IP_FLAG_TRACE_END;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001018}
1019
Adrian Hunter2c47db92019-06-04 16:00:09 +03001020static void intel_pt_setup_time_range(struct intel_pt *pt,
1021 struct intel_pt_queue *ptq)
1022{
1023 if (!pt->range_cnt)
1024 return;
1025
1026 ptq->sel_timestamp = pt->time_ranges[0].start;
1027 ptq->sel_idx = 0;
1028
1029 if (ptq->sel_timestamp) {
1030 ptq->sel_start = true;
1031 } else {
1032 ptq->sel_timestamp = pt->time_ranges[0].end;
1033 ptq->sel_start = false;
1034 }
1035}
1036
Adrian Hunter90e457f2015-07-17 19:33:41 +03001037static int intel_pt_setup_queue(struct intel_pt *pt,
1038 struct auxtrace_queue *queue,
1039 unsigned int queue_nr)
1040{
1041 struct intel_pt_queue *ptq = queue->priv;
1042
1043 if (list_empty(&queue->head))
1044 return 0;
1045
1046 if (!ptq) {
1047 ptq = intel_pt_alloc_queue(pt, queue_nr);
1048 if (!ptq)
1049 return -ENOMEM;
1050 queue->priv = ptq;
1051
1052 if (queue->cpu != -1)
1053 ptq->cpu = queue->cpu;
1054 ptq->tid = queue->tid;
1055
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001056 ptq->cbr_seen = UINT_MAX;
1057
Adrian Hunter1c071c82018-03-07 16:02:26 +02001058 if (pt->sampling_mode && !pt->snapshot_mode &&
1059 pt->timeless_decoding)
1060 ptq->step_through_buffers = true;
Adrian Hunter63d8e382018-03-07 16:02:22 +02001061
1062 ptq->sync_switch = pt->sync_switch;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001063
1064 intel_pt_setup_time_range(pt, ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001065 }
1066
1067 if (!ptq->on_heap &&
Adrian Hunter63d8e382018-03-07 16:02:22 +02001068 (!ptq->sync_switch ||
Adrian Hunter90e457f2015-07-17 19:33:41 +03001069 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1070 const struct intel_pt_state *state;
1071 int ret;
1072
1073 if (pt->timeless_decoding)
1074 return 0;
1075
1076 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1077 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1078 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
Adrian Hunter2c47db92019-06-04 16:00:09 +03001079
1080 if (ptq->sel_start && ptq->sel_timestamp) {
1081 ret = intel_pt_fast_forward(ptq->decoder,
1082 ptq->sel_timestamp);
1083 if (ret)
1084 return ret;
1085 }
1086
Adrian Hunter90e457f2015-07-17 19:33:41 +03001087 while (1) {
1088 state = intel_pt_decode(ptq->decoder);
1089 if (state->err) {
1090 if (state->err == INTEL_PT_ERR_NODATA) {
1091 intel_pt_log("queue %u has no timestamp\n",
1092 queue_nr);
1093 return 0;
1094 }
1095 continue;
1096 }
1097 if (state->timestamp)
1098 break;
1099 }
1100
1101 ptq->timestamp = state->timestamp;
1102 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1103 queue_nr, ptq->timestamp);
1104 ptq->state = state;
1105 ptq->have_sample = true;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001106 if (ptq->sel_start && ptq->sel_timestamp &&
1107 ptq->timestamp < ptq->sel_timestamp)
1108 ptq->have_sample = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001109 intel_pt_sample_flags(ptq);
1110 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1111 if (ret)
1112 return ret;
1113 ptq->on_heap = true;
1114 }
1115
1116 return 0;
1117}
1118
1119static int intel_pt_setup_queues(struct intel_pt *pt)
1120{
1121 unsigned int i;
1122 int ret;
1123
1124 for (i = 0; i < pt->queues.nr_queues; i++) {
1125 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1126 if (ret)
1127 return ret;
1128 }
1129 return 0;
1130}
1131
Adrian Hunterf14445e2015-09-25 16:15:45 +03001132static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
1133{
1134 struct branch_stack *bs_src = ptq->last_branch_rb;
1135 struct branch_stack *bs_dst = ptq->last_branch;
1136 size_t nr = 0;
1137
1138 bs_dst->nr = bs_src->nr;
1139
1140 if (!bs_src->nr)
1141 return;
1142
1143 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1144 memcpy(&bs_dst->entries[0],
1145 &bs_src->entries[ptq->last_branch_pos],
1146 sizeof(struct branch_entry) * nr);
1147
1148 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1149 memcpy(&bs_dst->entries[nr],
1150 &bs_src->entries[0],
1151 sizeof(struct branch_entry) * ptq->last_branch_pos);
1152 }
1153}
1154
1155static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1156{
1157 ptq->last_branch_pos = 0;
1158 ptq->last_branch_rb->nr = 0;
1159}
1160
1161static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1162{
1163 const struct intel_pt_state *state = ptq->state;
1164 struct branch_stack *bs = ptq->last_branch_rb;
1165 struct branch_entry *be;
1166
1167 if (!ptq->last_branch_pos)
1168 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1169
1170 ptq->last_branch_pos -= 1;
1171
1172 be = &bs->entries[ptq->last_branch_pos];
1173 be->from = state->from_ip;
1174 be->to = state->to_ip;
1175 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1176 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1177 /* No support for mispredict */
Adrian Hunterba11ba62015-09-25 16:15:56 +03001178 be->flags.mispred = ptq->pt->mispred_all;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001179
1180 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1181 bs->nr += 1;
1182}
1183
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001184static inline bool intel_pt_skip_event(struct intel_pt *pt)
1185{
1186 return pt->synth_opts.initial_skip &&
1187 pt->num_events++ < pt->synth_opts.initial_skip;
1188}
1189
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001190/*
1191 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
1192 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
1193 * from this decoder state.
1194 */
1195static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
1196{
1197 return pt->synth_opts.initial_skip &&
1198 pt->num_events + 4 < pt->synth_opts.initial_skip;
1199}
1200
Adrian Hunter0dfded32019-06-10 10:27:57 +03001201static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1202 union perf_event *event,
1203 struct perf_sample *sample)
1204{
1205 event->sample.header.type = PERF_RECORD_SAMPLE;
1206 event->sample.header.size = sizeof(struct perf_event_header);
1207
1208 sample->pid = ptq->pid;
1209 sample->tid = ptq->tid;
1210 sample->cpu = ptq->cpu;
1211 sample->insn_len = ptq->insn_len;
1212 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1213}
1214
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001215static void intel_pt_prep_b_sample(struct intel_pt *pt,
1216 struct intel_pt_queue *ptq,
1217 union perf_event *event,
1218 struct perf_sample *sample)
1219{
Adrian Hunter0dfded32019-06-10 10:27:57 +03001220 intel_pt_prep_a_sample(ptq, event, sample);
1221
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001222 if (!pt->timeless_decoding)
1223 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1224
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001225 sample->ip = ptq->state->from_ip;
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001226 sample->cpumode = intel_pt_cpumode(pt, sample->ip);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001227 sample->addr = ptq->state->to_ip;
1228 sample->period = 1;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001229 sample->flags = ptq->flags;
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001230
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001231 event->sample.header.misc = sample->cpumode;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001232}
1233
Adrian Hunter90e457f2015-07-17 19:33:41 +03001234static int intel_pt_inject_event(union perf_event *event,
Adrian Huntera10eb532018-01-16 15:14:50 +02001235 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001236{
1237 event->header.size = perf_event__sample_event_size(sample, type, 0);
Adrian Hunter936f1f32018-01-16 15:14:52 +02001238 return perf_event__synthesize_sample(event, type, 0, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001239}
1240
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001241static inline int intel_pt_opt_inject(struct intel_pt *pt,
1242 union perf_event *event,
1243 struct perf_sample *sample, u64 type)
1244{
1245 if (!pt->synth_opts.inject)
1246 return 0;
1247
Adrian Huntera10eb532018-01-16 15:14:50 +02001248 return intel_pt_inject_event(event, sample, type);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001249}
1250
1251static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1252 union perf_event *event,
1253 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001254{
1255 int ret;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001256
1257 ret = intel_pt_opt_inject(pt, event, sample, type);
1258 if (ret)
1259 return ret;
1260
1261 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1262 if (ret)
1263 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1264
1265 return ret;
1266}
1267
1268static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1269{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001270 struct intel_pt *pt = ptq->pt;
1271 union perf_event *event = ptq->event_buf;
1272 struct perf_sample sample = { .ip = 0, };
Adrian Hunterf14445e2015-09-25 16:15:45 +03001273 struct dummy_branch_stack {
1274 u64 nr;
1275 struct branch_entry entries;
1276 } dummy_bs;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001277
Adrian Hunter385e3302015-09-25 16:15:44 +03001278 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1279 return 0;
1280
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001281 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001282 return 0;
1283
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001284 intel_pt_prep_b_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001285
Adrian Hunter90e457f2015-07-17 19:33:41 +03001286 sample.id = ptq->pt->branches_id;
1287 sample.stream_id = ptq->pt->branches_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001288
Adrian Hunterf14445e2015-09-25 16:15:45 +03001289 /*
1290 * perf report cannot handle events without a branch stack when using
1291 * SORT_MODE__BRANCH so make a dummy one.
1292 */
1293 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1294 dummy_bs = (struct dummy_branch_stack){
1295 .nr = 1,
1296 .entries = {
1297 .from = sample.ip,
1298 .to = sample.addr,
1299 },
1300 };
1301 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1302 }
1303
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001304 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
1305 if (sample.cyc_cnt) {
1306 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1307 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1308 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1309 }
1310
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001311 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1312 pt->branches_sample_type);
1313}
1314
1315static void intel_pt_prep_sample(struct intel_pt *pt,
1316 struct intel_pt_queue *ptq,
1317 union perf_event *event,
1318 struct perf_sample *sample)
1319{
1320 intel_pt_prep_b_sample(pt, ptq, event, sample);
1321
1322 if (pt->synth_opts.callchain) {
Adrian Hunter256d92b2018-12-21 14:06:19 +02001323 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
Adrian Hunter24248302018-10-31 11:10:42 +02001324 pt->synth_opts.callchain_sz + 1,
1325 sample->ip, pt->kernel_start);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001326 sample->callchain = ptq->chain;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001327 }
1328
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001329 if (pt->synth_opts.last_branch) {
1330 intel_pt_copy_last_branch_rb(ptq);
1331 sample->branch_stack = ptq->last_branch;
1332 }
1333}
1334
1335static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1336 struct intel_pt_queue *ptq,
1337 union perf_event *event,
1338 struct perf_sample *sample,
1339 u64 type)
1340{
1341 int ret;
1342
1343 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1344
1345 if (pt->synth_opts.last_branch)
1346 intel_pt_reset_last_branch_rb(ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001347
1348 return ret;
1349}
1350
1351static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1352{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001353 struct intel_pt *pt = ptq->pt;
1354 union perf_event *event = ptq->event_buf;
1355 struct perf_sample sample = { .ip = 0, };
1356
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001357 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001358 return 0;
1359
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001360 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001361
Adrian Hunter90e457f2015-07-17 19:33:41 +03001362 sample.id = ptq->pt->instructions_id;
1363 sample.stream_id = ptq->pt->instructions_id;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001364 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001365
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001366 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
1367 if (sample.cyc_cnt) {
1368 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1369 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1370 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1371 }
1372
Adrian Hunter2a21d032015-07-17 19:33:48 +03001373 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1374
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001375 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1376 pt->instructions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001377}
1378
1379static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1380{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001381 struct intel_pt *pt = ptq->pt;
1382 union perf_event *event = ptq->event_buf;
1383 struct perf_sample sample = { .ip = 0, };
1384
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001385 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001386 return 0;
1387
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001388 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001389
Adrian Hunter90e457f2015-07-17 19:33:41 +03001390 sample.id = ptq->pt->transactions_id;
1391 sample.stream_id = ptq->pt->transactions_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001392
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001393 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1394 pt->transactions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001395}
1396
Adrian Hunter37973072017-06-30 11:36:45 +03001397static void intel_pt_prep_p_sample(struct intel_pt *pt,
1398 struct intel_pt_queue *ptq,
1399 union perf_event *event,
1400 struct perf_sample *sample)
1401{
1402 intel_pt_prep_sample(pt, ptq, event, sample);
1403
1404 /*
1405 * Zero IP is used to mean "trace start" but that is not the case for
1406 * power or PTWRITE events with no IP, so clear the flags.
1407 */
1408 if (!sample->ip)
1409 sample->flags = 0;
1410}
1411
1412static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1413{
1414 struct intel_pt *pt = ptq->pt;
1415 union perf_event *event = ptq->event_buf;
1416 struct perf_sample sample = { .ip = 0, };
1417 struct perf_synth_intel_ptwrite raw;
1418
1419 if (intel_pt_skip_event(pt))
1420 return 0;
1421
1422 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1423
1424 sample.id = ptq->pt->ptwrites_id;
1425 sample.stream_id = ptq->pt->ptwrites_id;
1426
1427 raw.flags = 0;
1428 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1429 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1430
1431 sample.raw_size = perf_synth__raw_size(raw);
1432 sample.raw_data = perf_synth__raw_data(&raw);
1433
1434 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1435 pt->ptwrites_sample_type);
1436}
1437
1438static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1439{
1440 struct intel_pt *pt = ptq->pt;
1441 union perf_event *event = ptq->event_buf;
1442 struct perf_sample sample = { .ip = 0, };
1443 struct perf_synth_intel_cbr raw;
1444 u32 flags;
1445
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001446 if (intel_pt_skip_cbr_event(pt))
Adrian Hunter37973072017-06-30 11:36:45 +03001447 return 0;
1448
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001449 ptq->cbr_seen = ptq->state->cbr;
1450
Adrian Hunter37973072017-06-30 11:36:45 +03001451 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1452
1453 sample.id = ptq->pt->cbr_id;
1454 sample.stream_id = ptq->pt->cbr_id;
1455
1456 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1457 raw.flags = cpu_to_le32(flags);
1458 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1459 raw.reserved3 = 0;
1460
1461 sample.raw_size = perf_synth__raw_size(raw);
1462 sample.raw_data = perf_synth__raw_data(&raw);
1463
1464 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1465 pt->pwr_events_sample_type);
1466}
1467
1468static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1469{
1470 struct intel_pt *pt = ptq->pt;
1471 union perf_event *event = ptq->event_buf;
1472 struct perf_sample sample = { .ip = 0, };
1473 struct perf_synth_intel_mwait raw;
1474
1475 if (intel_pt_skip_event(pt))
1476 return 0;
1477
1478 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1479
1480 sample.id = ptq->pt->mwait_id;
1481 sample.stream_id = ptq->pt->mwait_id;
1482
1483 raw.reserved = 0;
1484 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1485
1486 sample.raw_size = perf_synth__raw_size(raw);
1487 sample.raw_data = perf_synth__raw_data(&raw);
1488
1489 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1490 pt->pwr_events_sample_type);
1491}
1492
1493static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1494{
1495 struct intel_pt *pt = ptq->pt;
1496 union perf_event *event = ptq->event_buf;
1497 struct perf_sample sample = { .ip = 0, };
1498 struct perf_synth_intel_pwre raw;
1499
1500 if (intel_pt_skip_event(pt))
1501 return 0;
1502
1503 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1504
1505 sample.id = ptq->pt->pwre_id;
1506 sample.stream_id = ptq->pt->pwre_id;
1507
1508 raw.reserved = 0;
1509 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1510
1511 sample.raw_size = perf_synth__raw_size(raw);
1512 sample.raw_data = perf_synth__raw_data(&raw);
1513
1514 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1515 pt->pwr_events_sample_type);
1516}
1517
1518static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1519{
1520 struct intel_pt *pt = ptq->pt;
1521 union perf_event *event = ptq->event_buf;
1522 struct perf_sample sample = { .ip = 0, };
1523 struct perf_synth_intel_exstop raw;
1524
1525 if (intel_pt_skip_event(pt))
1526 return 0;
1527
1528 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1529
1530 sample.id = ptq->pt->exstop_id;
1531 sample.stream_id = ptq->pt->exstop_id;
1532
1533 raw.flags = 0;
1534 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1535
1536 sample.raw_size = perf_synth__raw_size(raw);
1537 sample.raw_data = perf_synth__raw_data(&raw);
1538
1539 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1540 pt->pwr_events_sample_type);
1541}
1542
1543static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1544{
1545 struct intel_pt *pt = ptq->pt;
1546 union perf_event *event = ptq->event_buf;
1547 struct perf_sample sample = { .ip = 0, };
1548 struct perf_synth_intel_pwrx raw;
1549
1550 if (intel_pt_skip_event(pt))
1551 return 0;
1552
1553 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1554
1555 sample.id = ptq->pt->pwrx_id;
1556 sample.stream_id = ptq->pt->pwrx_id;
1557
1558 raw.reserved = 0;
1559 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1560
1561 sample.raw_size = perf_synth__raw_size(raw);
1562 sample.raw_data = perf_synth__raw_data(&raw);
1563
1564 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1565 pt->pwr_events_sample_type);
1566}
1567
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001568/*
1569 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
1570 * intel_pt_add_gp_regs().
1571 */
1572static const int pebs_gp_regs[] = {
1573 [PERF_REG_X86_FLAGS] = 1,
1574 [PERF_REG_X86_IP] = 2,
1575 [PERF_REG_X86_AX] = 3,
1576 [PERF_REG_X86_CX] = 4,
1577 [PERF_REG_X86_DX] = 5,
1578 [PERF_REG_X86_BX] = 6,
1579 [PERF_REG_X86_SP] = 7,
1580 [PERF_REG_X86_BP] = 8,
1581 [PERF_REG_X86_SI] = 9,
1582 [PERF_REG_X86_DI] = 10,
1583 [PERF_REG_X86_R8] = 11,
1584 [PERF_REG_X86_R9] = 12,
1585 [PERF_REG_X86_R10] = 13,
1586 [PERF_REG_X86_R11] = 14,
1587 [PERF_REG_X86_R12] = 15,
1588 [PERF_REG_X86_R13] = 16,
1589 [PERF_REG_X86_R14] = 17,
1590 [PERF_REG_X86_R15] = 18,
1591};
1592
1593static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
1594 const struct intel_pt_blk_items *items,
1595 u64 regs_mask)
1596{
1597 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
1598 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
1599 u32 bit;
1600 int i;
1601
1602 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
1603 /* Get the PEBS gp_regs array index */
1604 int n = pebs_gp_regs[i] - 1;
1605
1606 if (n < 0)
1607 continue;
1608 /*
1609 * Add only registers that were requested (i.e. 'regs_mask') and
1610 * that were provided (i.e. 'mask'), and update the resulting
1611 * mask (i.e. 'intr_regs->mask') accordingly.
1612 */
1613 if (mask & 1 << n && regs_mask & bit) {
1614 intr_regs->mask |= bit;
1615 *pos++ = gp_regs[n];
1616 }
1617 }
1618
1619 return pos;
1620}
1621
Adrian Hunter143d34a2019-06-10 10:28:00 +03001622#ifndef PERF_REG_X86_XMM0
1623#define PERF_REG_X86_XMM0 32
1624#endif
1625
1626static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
1627 const struct intel_pt_blk_items *items,
1628 u64 regs_mask)
1629{
1630 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
1631 const u64 *xmm = items->xmm;
1632
1633 /*
1634 * If there are any XMM registers, then there should be all of them.
1635 * Nevertheless, follow the logic to add only registers that were
1636 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
1637 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
1638 */
1639 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
1640
1641 for (; mask; mask >>= 1, xmm++) {
1642 if (mask & 1)
1643 *pos++ = *xmm;
1644 }
1645}
1646
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001647#define LBR_INFO_MISPRED (1ULL << 63)
1648#define LBR_INFO_IN_TX (1ULL << 62)
1649#define LBR_INFO_ABORT (1ULL << 61)
1650#define LBR_INFO_CYCLES 0xffff
1651
1652/* Refer kernel's intel_pmu_store_pebs_lbrs() */
1653static u64 intel_pt_lbr_flags(u64 info)
1654{
1655 union {
1656 struct branch_flags flags;
1657 u64 result;
1658 } u = {
1659 .flags = {
1660 .mispred = !!(info & LBR_INFO_MISPRED),
1661 .predicted = !(info & LBR_INFO_MISPRED),
1662 .in_tx = !!(info & LBR_INFO_IN_TX),
1663 .abort = !!(info & LBR_INFO_ABORT),
1664 .cycles = info & LBR_INFO_CYCLES,
1665 }
1666 };
1667
1668 return u.result;
1669}
1670
1671static void intel_pt_add_lbrs(struct branch_stack *br_stack,
1672 const struct intel_pt_blk_items *items)
1673{
1674 u64 *to;
1675 int i;
1676
1677 br_stack->nr = 0;
1678
1679 to = &br_stack->entries[0].from;
1680
1681 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
1682 u32 mask = items->mask[i];
1683 const u64 *from = items->val[i];
1684
1685 for (; mask; mask >>= 3, from += 3) {
1686 if ((mask & 7) == 7) {
1687 *to++ = from[0];
1688 *to++ = from[1];
1689 *to++ = intel_pt_lbr_flags(from[2]);
1690 br_stack->nr += 1;
1691 }
1692 }
1693 }
1694}
1695
1696/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1697#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3)
1698
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001699static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
Adrian Huntere62ca652019-06-10 10:27:56 +03001700{
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001701 const struct intel_pt_blk_items *items = &ptq->state->items;
1702 struct perf_sample sample = { .ip = 0, };
1703 union perf_event *event = ptq->event_buf;
1704 struct intel_pt *pt = ptq->pt;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001705 struct evsel *evsel = pt->pebs_evsel;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001706 u64 sample_type = evsel->core.attr.sample_type;
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001707 u64 id = evsel->id[0];
1708 u8 cpumode;
1709
1710 if (intel_pt_skip_event(pt))
1711 return 0;
1712
1713 intel_pt_prep_a_sample(ptq, event, &sample);
1714
1715 sample.id = id;
1716 sample.stream_id = id;
1717
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001718 if (!evsel->core.attr.freq)
1719 sample.period = evsel->core.attr.sample_period;
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001720
1721 /* No support for non-zero CS base */
1722 if (items->has_ip)
1723 sample.ip = items->ip;
1724 else if (items->has_rip)
1725 sample.ip = items->rip;
1726 else
1727 sample.ip = ptq->state->from_ip;
1728
1729 /* No support for guest mode at this time */
1730 cpumode = sample.ip < ptq->pt->kernel_start ?
1731 PERF_RECORD_MISC_USER :
1732 PERF_RECORD_MISC_KERNEL;
1733
1734 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
1735
1736 sample.cpumode = cpumode;
1737
1738 if (sample_type & PERF_SAMPLE_TIME) {
1739 u64 timestamp = 0;
1740
1741 if (items->has_timestamp)
1742 timestamp = items->timestamp;
1743 else if (!pt->timeless_decoding)
1744 timestamp = ptq->timestamp;
1745 if (timestamp)
1746 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
1747 }
1748
Adrian Huntere01f0ef2019-06-10 10:28:03 +03001749 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
1750 pt->synth_opts.callchain) {
1751 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1752 pt->synth_opts.callchain_sz, sample.ip,
1753 pt->kernel_start);
1754 sample.callchain = ptq->chain;
1755 }
1756
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001757 if (sample_type & PERF_SAMPLE_REGS_INTR &&
1758 items->mask[INTEL_PT_GP_REGS_POS]) {
1759 u64 regs[sizeof(sample.intr_regs.mask)];
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001760 u64 regs_mask = evsel->core.attr.sample_regs_intr;
Adrian Hunter143d34a2019-06-10 10:28:00 +03001761 u64 *pos;
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001762
1763 sample.intr_regs.abi = items->is_32_bit ?
1764 PERF_SAMPLE_REGS_ABI_32 :
1765 PERF_SAMPLE_REGS_ABI_64;
1766 sample.intr_regs.regs = regs;
1767
Adrian Hunter143d34a2019-06-10 10:28:00 +03001768 pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
1769
1770 intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001771 }
1772
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001773 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1774 struct {
1775 struct branch_stack br_stack;
1776 struct branch_entry entries[LBRS_MAX];
1777 } br;
1778
1779 if (items->mask[INTEL_PT_LBR_0_POS] ||
1780 items->mask[INTEL_PT_LBR_1_POS] ||
1781 items->mask[INTEL_PT_LBR_2_POS]) {
1782 intel_pt_add_lbrs(&br.br_stack, items);
1783 sample.branch_stack = &br.br_stack;
1784 } else if (pt->synth_opts.last_branch) {
1785 intel_pt_copy_last_branch_rb(ptq);
1786 sample.branch_stack = ptq->last_branch;
1787 } else {
1788 br.br_stack.nr = 0;
1789 sample.branch_stack = &br.br_stack;
1790 }
1791 }
1792
Adrian Hunter975846e2019-06-10 10:28:02 +03001793 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
1794 sample.addr = items->mem_access_address;
1795
1796 if (sample_type & PERF_SAMPLE_WEIGHT) {
1797 /*
1798 * Refer kernel's setup_pebs_adaptive_sample_data() and
1799 * intel_hsw_weight().
1800 */
1801 if (items->has_mem_access_latency)
1802 sample.weight = items->mem_access_latency;
1803 if (!sample.weight && items->has_tsx_aux_info) {
1804 /* Cycles last block */
1805 sample.weight = (u32)items->tsx_aux_info;
1806 }
1807 }
1808
1809 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
1810 u64 ax = items->has_rax ? items->rax : 0;
1811 /* Refer kernel's intel_hsw_transaction() */
1812 u64 txn = (u8)(items->tsx_aux_info >> 32);
1813
1814 /* For RTM XABORTs also log the abort code from AX */
1815 if (txn & PERF_TXN_TRANSACTION && ax & 1)
1816 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1817 sample.transaction = txn;
1818 }
1819
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001820 return intel_pt_deliver_synth_event(pt, ptq, event, &sample, sample_type);
Adrian Huntere62ca652019-06-10 10:27:56 +03001821}
1822
Adrian Hunter90e457f2015-07-17 19:33:41 +03001823static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02001824 pid_t pid, pid_t tid, u64 ip, u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001825{
1826 union perf_event event;
1827 char msg[MAX_AUXTRACE_ERROR_MSG];
1828 int err;
1829
1830 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1831
1832 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
Adrian Hunter16bd4322019-02-06 12:39:47 +02001833 code, cpu, pid, tid, ip, msg, timestamp);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001834
1835 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1836 if (err)
1837 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1838 err);
1839
1840 return err;
1841}
1842
Adrian Hunter16bd4322019-02-06 12:39:47 +02001843static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
1844 const struct intel_pt_state *state)
1845{
1846 struct intel_pt *pt = ptq->pt;
1847 u64 tm = ptq->timestamp;
1848
1849 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
1850
1851 return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
1852 ptq->tid, state->from_ip, tm);
1853}
1854
Adrian Hunter90e457f2015-07-17 19:33:41 +03001855static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1856{
1857 struct auxtrace_queue *queue;
1858 pid_t tid = ptq->next_tid;
1859 int err;
1860
1861 if (tid == -1)
1862 return 0;
1863
1864 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1865
1866 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1867
1868 queue = &pt->queues.queue_array[ptq->queue_nr];
1869 intel_pt_set_pid_tid_cpu(pt, queue);
1870
1871 ptq->next_tid = -1;
1872
1873 return err;
1874}
1875
1876static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1877{
1878 struct intel_pt *pt = ptq->pt;
1879
1880 return ip == pt->switch_ip &&
1881 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1882 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1883 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1884}
1885
Adrian Hunter37973072017-06-30 11:36:45 +03001886#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001887 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
Adrian Hunter37973072017-06-30 11:36:45 +03001888
Adrian Hunter90e457f2015-07-17 19:33:41 +03001889static int intel_pt_sample(struct intel_pt_queue *ptq)
1890{
1891 const struct intel_pt_state *state = ptq->state;
1892 struct intel_pt *pt = ptq->pt;
1893 int err;
1894
1895 if (!ptq->have_sample)
1896 return 0;
1897
1898 ptq->have_sample = false;
1899
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001900 if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) {
1901 /*
1902 * Cycle count and instruction count only go together to create
1903 * a valid IPC ratio when the cycle count changes.
1904 */
1905 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
1906 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
1907 }
1908
Adrian Huntere62ca652019-06-10 10:27:56 +03001909 /*
1910 * Do PEBS first to allow for the possibility that the PEBS timestamp
1911 * precedes the current timestamp.
1912 */
1913 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
1914 err = intel_pt_synth_pebs_sample(ptq);
1915 if (err)
1916 return err;
1917 }
1918
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001919 if (pt->sample_pwr_events) {
1920 if (ptq->state->cbr != ptq->cbr_seen) {
Adrian Hunter37973072017-06-30 11:36:45 +03001921 err = intel_pt_synth_cbr_sample(ptq);
1922 if (err)
1923 return err;
1924 }
Adrian Hunter5fe2cf72019-06-22 12:32:45 +03001925 if (state->type & INTEL_PT_PWR_EVT) {
1926 if (state->type & INTEL_PT_MWAIT_OP) {
1927 err = intel_pt_synth_mwait_sample(ptq);
1928 if (err)
1929 return err;
1930 }
1931 if (state->type & INTEL_PT_PWR_ENTRY) {
1932 err = intel_pt_synth_pwre_sample(ptq);
1933 if (err)
1934 return err;
1935 }
1936 if (state->type & INTEL_PT_EX_STOP) {
1937 err = intel_pt_synth_exstop_sample(ptq);
1938 if (err)
1939 return err;
1940 }
1941 if (state->type & INTEL_PT_PWR_EXIT) {
1942 err = intel_pt_synth_pwrx_sample(ptq);
1943 if (err)
1944 return err;
1945 }
Adrian Hunter37973072017-06-30 11:36:45 +03001946 }
1947 }
1948
Adrian Hunter406a1802017-05-26 11:17:29 +03001949 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001950 err = intel_pt_synth_instruction_sample(ptq);
1951 if (err)
1952 return err;
1953 }
1954
Adrian Hunter406a1802017-05-26 11:17:29 +03001955 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001956 err = intel_pt_synth_transaction_sample(ptq);
1957 if (err)
1958 return err;
1959 }
1960
Adrian Hunter37973072017-06-30 11:36:45 +03001961 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
1962 err = intel_pt_synth_ptwrite_sample(ptq);
1963 if (err)
1964 return err;
1965 }
1966
Adrian Hunter90e457f2015-07-17 19:33:41 +03001967 if (!(state->type & INTEL_PT_BRANCH))
1968 return 0;
1969
Adrian Hunter50f736372016-06-23 16:40:57 +03001970 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
Adrian Hunter256d92b2018-12-21 14:06:19 +02001971 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags, state->from_ip,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001972 state->to_ip, ptq->insn_len,
1973 state->trace_nr);
1974 else
Adrian Hunter256d92b2018-12-21 14:06:19 +02001975 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001976
1977 if (pt->sample_branches) {
1978 err = intel_pt_synth_branch_sample(ptq);
1979 if (err)
1980 return err;
1981 }
1982
Adrian Hunterf14445e2015-09-25 16:15:45 +03001983 if (pt->synth_opts.last_branch)
1984 intel_pt_update_last_branch_rb(ptq);
1985
Adrian Hunter63d8e382018-03-07 16:02:22 +02001986 if (!ptq->sync_switch)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001987 return 0;
1988
1989 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1990 switch (ptq->switch_state) {
Adrian Hunterdbcb82b2018-05-31 13:23:42 +03001991 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03001992 case INTEL_PT_SS_UNKNOWN:
1993 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1994 err = intel_pt_next_tid(pt, ptq);
1995 if (err)
1996 return err;
1997 ptq->switch_state = INTEL_PT_SS_TRACING;
1998 break;
1999 default:
2000 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
2001 return 1;
2002 }
2003 } else if (!state->to_ip) {
2004 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2005 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
2006 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2007 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2008 state->to_ip == pt->ptss_ip &&
2009 (ptq->flags & PERF_IP_FLAG_CALL)) {
2010 ptq->switch_state = INTEL_PT_SS_TRACING;
2011 }
2012
2013 return 0;
2014}
2015
Adrian Hunter86c27862015-08-13 12:40:57 +03002016static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002017{
Adrian Hunter86c27862015-08-13 12:40:57 +03002018 struct machine *machine = pt->machine;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002019 struct map *map;
2020 struct symbol *sym, *start;
2021 u64 ip, switch_ip = 0;
Adrian Hunter86c27862015-08-13 12:40:57 +03002022 const char *ptss;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002023
2024 if (ptss_ip)
2025 *ptss_ip = 0;
2026
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002027 map = machine__kernel_map(machine);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002028 if (!map)
2029 return 0;
2030
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002031 if (map__load(map))
Adrian Hunter90e457f2015-07-17 19:33:41 +03002032 return 0;
2033
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -03002034 start = dso__first_symbol(map->dso);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002035
2036 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2037 if (sym->binding == STB_GLOBAL &&
2038 !strcmp(sym->name, "__switch_to")) {
2039 ip = map->unmap_ip(map, sym->start);
2040 if (ip >= map->start && ip < map->end) {
2041 switch_ip = ip;
2042 break;
2043 }
2044 }
2045 }
2046
2047 if (!switch_ip || !ptss_ip)
2048 return 0;
2049
Adrian Hunter86c27862015-08-13 12:40:57 +03002050 if (pt->have_sched_switch == 1)
2051 ptss = "perf_trace_sched_switch";
2052 else
2053 ptss = "__perf_event_task_sched_out";
2054
Adrian Hunter90e457f2015-07-17 19:33:41 +03002055 for (sym = start; sym; sym = dso__next_symbol(sym)) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002056 if (!strcmp(sym->name, ptss)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002057 ip = map->unmap_ip(map, sym->start);
2058 if (ip >= map->start && ip < map->end) {
2059 *ptss_ip = ip;
2060 break;
2061 }
2062 }
2063 }
2064
2065 return switch_ip;
2066}
2067
Adrian Hunter63d8e382018-03-07 16:02:22 +02002068static void intel_pt_enable_sync_switch(struct intel_pt *pt)
2069{
2070 unsigned int i;
2071
2072 pt->sync_switch = true;
2073
2074 for (i = 0; i < pt->queues.nr_queues; i++) {
2075 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2076 struct intel_pt_queue *ptq = queue->priv;
2077
2078 if (ptq)
2079 ptq->sync_switch = true;
2080 }
2081}
2082
Adrian Hunter2c47db92019-06-04 16:00:09 +03002083/*
2084 * To filter against time ranges, it is only necessary to look at the next start
2085 * or end time.
2086 */
2087static bool intel_pt_next_time(struct intel_pt_queue *ptq)
2088{
2089 struct intel_pt *pt = ptq->pt;
2090
2091 if (ptq->sel_start) {
2092 /* Next time is an end time */
2093 ptq->sel_start = false;
2094 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
2095 return true;
2096 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
2097 /* Next time is a start time */
2098 ptq->sel_start = true;
2099 ptq->sel_idx += 1;
2100 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
2101 return true;
2102 }
2103
2104 /* No next time */
2105 return false;
2106}
2107
2108static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
2109{
2110 int err;
2111
2112 while (1) {
2113 if (ptq->sel_start) {
2114 if (ptq->timestamp >= ptq->sel_timestamp) {
2115 /* After start time, so consider next time */
2116 intel_pt_next_time(ptq);
2117 if (!ptq->sel_timestamp) {
2118 /* No end time */
2119 return 0;
2120 }
2121 /* Check against end time */
2122 continue;
2123 }
2124 /* Before start time, so fast forward */
2125 ptq->have_sample = false;
2126 if (ptq->sel_timestamp > *ff_timestamp) {
2127 if (ptq->sync_switch) {
2128 intel_pt_next_tid(ptq->pt, ptq);
2129 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2130 }
2131 *ff_timestamp = ptq->sel_timestamp;
2132 err = intel_pt_fast_forward(ptq->decoder,
2133 ptq->sel_timestamp);
2134 if (err)
2135 return err;
2136 }
2137 return 0;
2138 } else if (ptq->timestamp > ptq->sel_timestamp) {
2139 /* After end time, so consider next time */
2140 if (!intel_pt_next_time(ptq)) {
2141 /* No next time range, so stop decoding */
2142 ptq->have_sample = false;
2143 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2144 return 1;
2145 }
2146 /* Check against next start time */
2147 continue;
2148 } else {
2149 /* Before end time */
2150 return 0;
2151 }
2152 }
2153}
2154
Adrian Hunter90e457f2015-07-17 19:33:41 +03002155static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
2156{
2157 const struct intel_pt_state *state = ptq->state;
2158 struct intel_pt *pt = ptq->pt;
Adrian Hunter2c47db92019-06-04 16:00:09 +03002159 u64 ff_timestamp = 0;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002160 int err;
2161
2162 if (!pt->kernel_start) {
2163 pt->kernel_start = machine__kernel_start(pt->machine);
Adrian Hunter86c27862015-08-13 12:40:57 +03002164 if (pt->per_cpu_mmaps &&
2165 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002166 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
2167 !pt->sampling_mode) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002168 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002169 if (pt->switch_ip) {
2170 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
2171 pt->switch_ip, pt->ptss_ip);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002172 intel_pt_enable_sync_switch(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002173 }
2174 }
2175 }
2176
2177 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
2178 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2179 while (1) {
2180 err = intel_pt_sample(ptq);
2181 if (err)
2182 return err;
2183
2184 state = intel_pt_decode(ptq->decoder);
2185 if (state->err) {
2186 if (state->err == INTEL_PT_ERR_NODATA)
2187 return 1;
Adrian Hunter63d8e382018-03-07 16:02:22 +02002188 if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002189 state->from_ip >= pt->kernel_start) {
Adrian Hunter63d8e382018-03-07 16:02:22 +02002190 ptq->sync_switch = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002191 intel_pt_next_tid(pt, ptq);
2192 }
2193 if (pt->synth_opts.errors) {
Adrian Hunter16bd4322019-02-06 12:39:47 +02002194 err = intel_ptq_synth_error(ptq, state);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002195 if (err)
2196 return err;
2197 }
2198 continue;
2199 }
2200
2201 ptq->state = state;
2202 ptq->have_sample = true;
2203 intel_pt_sample_flags(ptq);
2204
2205 /* Use estimated TSC upon return to user space */
2206 if (pt->est_tsc &&
2207 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
2208 state->to_ip && state->to_ip < pt->kernel_start) {
2209 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2210 state->timestamp, state->est_timestamp);
2211 ptq->timestamp = state->est_timestamp;
2212 /* Use estimated TSC in unknown switch state */
Adrian Hunter63d8e382018-03-07 16:02:22 +02002213 } else if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002214 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2215 intel_pt_is_switch_ip(ptq, state->to_ip) &&
2216 ptq->next_tid == -1) {
2217 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2218 state->timestamp, state->est_timestamp);
2219 ptq->timestamp = state->est_timestamp;
2220 } else if (state->timestamp > ptq->timestamp) {
2221 ptq->timestamp = state->timestamp;
2222 }
2223
Adrian Hunter2c47db92019-06-04 16:00:09 +03002224 if (ptq->sel_timestamp) {
2225 err = intel_pt_time_filter(ptq, &ff_timestamp);
2226 if (err)
2227 return err;
2228 }
2229
Adrian Hunter90e457f2015-07-17 19:33:41 +03002230 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
2231 *timestamp = ptq->timestamp;
2232 return 0;
2233 }
2234 }
2235 return 0;
2236}
2237
2238static inline int intel_pt_update_queues(struct intel_pt *pt)
2239{
2240 if (pt->queues.new_data) {
2241 pt->queues.new_data = false;
2242 return intel_pt_setup_queues(pt);
2243 }
2244 return 0;
2245}
2246
2247static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
2248{
2249 unsigned int queue_nr;
2250 u64 ts;
2251 int ret;
2252
2253 while (1) {
2254 struct auxtrace_queue *queue;
2255 struct intel_pt_queue *ptq;
2256
2257 if (!pt->heap.heap_cnt)
2258 return 0;
2259
2260 if (pt->heap.heap_array[0].ordinal >= timestamp)
2261 return 0;
2262
2263 queue_nr = pt->heap.heap_array[0].queue_nr;
2264 queue = &pt->queues.queue_array[queue_nr];
2265 ptq = queue->priv;
2266
2267 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
2268 queue_nr, pt->heap.heap_array[0].ordinal,
2269 timestamp);
2270
2271 auxtrace_heap__pop(&pt->heap);
2272
2273 if (pt->heap.heap_cnt) {
2274 ts = pt->heap.heap_array[0].ordinal + 1;
2275 if (ts > timestamp)
2276 ts = timestamp;
2277 } else {
2278 ts = timestamp;
2279 }
2280
2281 intel_pt_set_pid_tid_cpu(pt, queue);
2282
2283 ret = intel_pt_run_decoder(ptq, &ts);
2284
2285 if (ret < 0) {
2286 auxtrace_heap__add(&pt->heap, queue_nr, ts);
2287 return ret;
2288 }
2289
2290 if (!ret) {
2291 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
2292 if (ret < 0)
2293 return ret;
2294 } else {
2295 ptq->on_heap = false;
2296 }
2297 }
2298
2299 return 0;
2300}
2301
2302static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
2303 u64 time_)
2304{
2305 struct auxtrace_queues *queues = &pt->queues;
2306 unsigned int i;
2307 u64 ts = 0;
2308
2309 for (i = 0; i < queues->nr_queues; i++) {
2310 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2311 struct intel_pt_queue *ptq = queue->priv;
2312
2313 if (ptq && (tid == -1 || ptq->tid == tid)) {
2314 ptq->time = time_;
2315 intel_pt_set_pid_tid_cpu(pt, queue);
2316 intel_pt_run_decoder(ptq, &ts);
2317 }
2318 }
2319 return 0;
2320}
2321
2322static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
2323{
2324 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02002325 sample->pid, sample->tid, 0, sample->time);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002326}
2327
2328static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
2329{
2330 unsigned i, j;
2331
2332 if (cpu < 0 || !pt->queues.nr_queues)
2333 return NULL;
2334
2335 if ((unsigned)cpu >= pt->queues.nr_queues)
2336 i = pt->queues.nr_queues - 1;
2337 else
2338 i = cpu;
2339
2340 if (pt->queues.queue_array[i].cpu == cpu)
2341 return pt->queues.queue_array[i].priv;
2342
2343 for (j = 0; i > 0; j++) {
2344 if (pt->queues.queue_array[--i].cpu == cpu)
2345 return pt->queues.queue_array[i].priv;
2346 }
2347
2348 for (; j < pt->queues.nr_queues; j++) {
2349 if (pt->queues.queue_array[j].cpu == cpu)
2350 return pt->queues.queue_array[j].priv;
2351 }
2352
2353 return NULL;
2354}
2355
Adrian Hunter86c27862015-08-13 12:40:57 +03002356static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
2357 u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002358{
2359 struct intel_pt_queue *ptq;
Adrian Hunter86c27862015-08-13 12:40:57 +03002360 int err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002361
2362 if (!pt->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002363 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002364
2365 ptq = intel_pt_cpu_to_ptq(pt, cpu);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002366 if (!ptq || !ptq->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002367 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002368
2369 switch (ptq->switch_state) {
2370 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002371 break;
2372 case INTEL_PT_SS_UNKNOWN:
2373 case INTEL_PT_SS_TRACING:
2374 ptq->next_tid = tid;
2375 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
2376 return 0;
2377 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2378 if (!ptq->on_heap) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002379 ptq->timestamp = perf_time_to_tsc(timestamp,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002380 &pt->tc);
2381 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
2382 ptq->timestamp);
2383 if (err)
2384 return err;
2385 ptq->on_heap = true;
2386 }
2387 ptq->switch_state = INTEL_PT_SS_TRACING;
2388 break;
2389 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002390 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
2391 break;
2392 default:
2393 break;
2394 }
Adrian Hunter86c27862015-08-13 12:40:57 +03002395
Adrian Hunter14f1cfd2019-04-12 14:38:30 +03002396 ptq->next_tid = -1;
2397
Adrian Hunter86c27862015-08-13 12:40:57 +03002398 return 1;
2399}
2400
2401static int intel_pt_process_switch(struct intel_pt *pt,
2402 struct perf_sample *sample)
2403{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002404 struct evsel *evsel;
Adrian Hunter86c27862015-08-13 12:40:57 +03002405 pid_t tid;
2406 int cpu, ret;
2407
2408 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
2409 if (evsel != pt->switch_evsel)
2410 return 0;
2411
2412 tid = perf_evsel__intval(evsel, sample, "next_pid");
2413 cpu = sample->cpu;
2414
2415 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2416 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
2417 &pt->tc));
2418
2419 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2420 if (ret <= 0)
2421 return ret;
2422
Adrian Hunter90e457f2015-07-17 19:33:41 +03002423 return machine__set_current_tid(pt->machine, cpu, -1, tid);
2424}
2425
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002426static int intel_pt_context_switch_in(struct intel_pt *pt,
2427 struct perf_sample *sample)
2428{
2429 pid_t pid = sample->pid;
2430 pid_t tid = sample->tid;
2431 int cpu = sample->cpu;
2432
2433 if (pt->sync_switch) {
2434 struct intel_pt_queue *ptq;
2435
2436 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2437 if (ptq && ptq->sync_switch) {
2438 ptq->next_tid = -1;
2439 switch (ptq->switch_state) {
2440 case INTEL_PT_SS_NOT_TRACING:
2441 case INTEL_PT_SS_UNKNOWN:
2442 case INTEL_PT_SS_TRACING:
2443 break;
2444 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2445 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2446 ptq->switch_state = INTEL_PT_SS_TRACING;
2447 break;
2448 default:
2449 break;
2450 }
2451 }
2452 }
2453
2454 /*
2455 * If the current tid has not been updated yet, ensure it is now that
2456 * a "switch in" event has occurred.
2457 */
2458 if (machine__get_current_tid(pt->machine, cpu) == tid)
2459 return 0;
2460
2461 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2462}
2463
Adrian Hunter86c27862015-08-13 12:40:57 +03002464static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
2465 struct perf_sample *sample)
2466{
2467 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2468 pid_t pid, tid;
2469 int cpu, ret;
2470
2471 cpu = sample->cpu;
2472
2473 if (pt->have_sched_switch == 3) {
2474 if (!out)
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002475 return intel_pt_context_switch_in(pt, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002476 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
2477 pr_err("Expecting CPU-wide context switch event\n");
2478 return -EINVAL;
2479 }
2480 pid = event->context_switch.next_prev_pid;
2481 tid = event->context_switch.next_prev_tid;
2482 } else {
2483 if (out)
2484 return 0;
2485 pid = sample->pid;
2486 tid = sample->tid;
2487 }
2488
2489 if (tid == -1) {
2490 pr_err("context_switch event has no tid\n");
2491 return -EINVAL;
2492 }
2493
2494 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2495 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
2496 &pt->tc));
2497
2498 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2499 if (ret <= 0)
2500 return ret;
2501
2502 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2503}
2504
Adrian Hunter90e457f2015-07-17 19:33:41 +03002505static int intel_pt_process_itrace_start(struct intel_pt *pt,
2506 union perf_event *event,
2507 struct perf_sample *sample)
2508{
2509 if (!pt->per_cpu_mmaps)
2510 return 0;
2511
2512 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2513 sample->cpu, event->itrace_start.pid,
2514 event->itrace_start.tid, sample->time,
2515 perf_time_to_tsc(sample->time, &pt->tc));
2516
2517 return machine__set_current_tid(pt->machine, sample->cpu,
2518 event->itrace_start.pid,
2519 event->itrace_start.tid);
2520}
2521
2522static int intel_pt_process_event(struct perf_session *session,
2523 union perf_event *event,
2524 struct perf_sample *sample,
2525 struct perf_tool *tool)
2526{
2527 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2528 auxtrace);
2529 u64 timestamp;
2530 int err = 0;
2531
2532 if (dump_trace)
2533 return 0;
2534
2535 if (!tool->ordered_events) {
2536 pr_err("Intel Processor Trace requires ordered events\n");
2537 return -EINVAL;
2538 }
2539
Adrian Hunter81cd60c2015-08-20 11:51:32 +03002540 if (sample->time && sample->time != (u64)-1)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002541 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
2542 else
2543 timestamp = 0;
2544
2545 if (timestamp || pt->timeless_decoding) {
2546 err = intel_pt_update_queues(pt);
2547 if (err)
2548 return err;
2549 }
2550
2551 if (pt->timeless_decoding) {
2552 if (event->header.type == PERF_RECORD_EXIT) {
2553 err = intel_pt_process_timeless_queues(pt,
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002554 event->fork.tid,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002555 sample->time);
2556 }
2557 } else if (timestamp) {
2558 err = intel_pt_process_queues(pt, timestamp);
2559 }
2560 if (err)
2561 return err;
2562
2563 if (event->header.type == PERF_RECORD_AUX &&
2564 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
2565 pt->synth_opts.errors) {
2566 err = intel_pt_lost(pt, sample);
2567 if (err)
2568 return err;
2569 }
2570
2571 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
2572 err = intel_pt_process_switch(pt, sample);
2573 else if (event->header.type == PERF_RECORD_ITRACE_START)
2574 err = intel_pt_process_itrace_start(pt, event, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002575 else if (event->header.type == PERF_RECORD_SWITCH ||
2576 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2577 err = intel_pt_context_switch(pt, event, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002578
Adrian Hunter93f8be22018-11-05 09:35:04 +02002579 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
2580 event->header.type, sample->cpu, sample->time, timestamp);
2581 intel_pt_log_event(event);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002582
2583 return err;
2584}
2585
2586static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
2587{
2588 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2589 auxtrace);
2590 int ret;
2591
2592 if (dump_trace)
2593 return 0;
2594
2595 if (!tool->ordered_events)
2596 return -EINVAL;
2597
2598 ret = intel_pt_update_queues(pt);
2599 if (ret < 0)
2600 return ret;
2601
2602 if (pt->timeless_decoding)
2603 return intel_pt_process_timeless_queues(pt, -1,
2604 MAX_TIMESTAMP - 1);
2605
2606 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
2607}
2608
2609static void intel_pt_free_events(struct perf_session *session)
2610{
2611 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2612 auxtrace);
2613 struct auxtrace_queues *queues = &pt->queues;
2614 unsigned int i;
2615
2616 for (i = 0; i < queues->nr_queues; i++) {
2617 intel_pt_free_queue(queues->queue_array[i].priv);
2618 queues->queue_array[i].priv = NULL;
2619 }
2620 intel_pt_log_disable();
2621 auxtrace_queues__free(queues);
2622}
2623
2624static void intel_pt_free(struct perf_session *session)
2625{
2626 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2627 auxtrace);
2628
2629 auxtrace_heap__free(&pt->heap);
2630 intel_pt_free_events(session);
2631 session->auxtrace = NULL;
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03002632 thread__put(pt->unknown_thread);
Adrian Hunter2acee102016-09-23 17:38:48 +03002633 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002634 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03002635 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002636 free(pt);
2637}
2638
2639static int intel_pt_process_auxtrace_event(struct perf_session *session,
2640 union perf_event *event,
2641 struct perf_tool *tool __maybe_unused)
2642{
2643 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2644 auxtrace);
2645
Adrian Hunter90e457f2015-07-17 19:33:41 +03002646 if (!pt->data_queued) {
2647 struct auxtrace_buffer *buffer;
2648 off_t data_offset;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002649 int fd = perf_data__fd(session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002650 int err;
2651
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002652 if (perf_data__is_pipe(session->data)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002653 data_offset = 0;
2654 } else {
2655 data_offset = lseek(fd, 0, SEEK_CUR);
2656 if (data_offset == -1)
2657 return -errno;
2658 }
2659
2660 err = auxtrace_queues__add_event(&pt->queues, session, event,
2661 data_offset, &buffer);
2662 if (err)
2663 return err;
2664
2665 /* Dump here now we have copied a piped trace out of the pipe */
2666 if (dump_trace) {
2667 if (auxtrace_buffer__get_data(buffer, fd)) {
2668 intel_pt_dump_event(pt, buffer->data,
2669 buffer->size);
2670 auxtrace_buffer__put_data(buffer);
2671 }
2672 }
2673 }
2674
2675 return 0;
2676}
2677
2678struct intel_pt_synth {
2679 struct perf_tool dummy_tool;
2680 struct perf_session *session;
2681};
2682
2683static int intel_pt_event_synth(struct perf_tool *tool,
2684 union perf_event *event,
2685 struct perf_sample *sample __maybe_unused,
2686 struct machine *machine __maybe_unused)
2687{
2688 struct intel_pt_synth *intel_pt_synth =
2689 container_of(tool, struct intel_pt_synth, dummy_tool);
2690
2691 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
2692 NULL);
2693}
2694
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002695static int intel_pt_synth_event(struct perf_session *session, const char *name,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002696 struct perf_event_attr *attr, u64 id)
2697{
2698 struct intel_pt_synth intel_pt_synth;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002699 int err;
2700
2701 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2702 name, id, (u64)attr->sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002703
2704 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
2705 intel_pt_synth.session = session;
2706
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002707 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
2708 &id, intel_pt_event_synth);
2709 if (err)
2710 pr_err("%s: failed to synthesize '%s' event type\n",
2711 __func__, name);
2712
2713 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002714}
2715
Jiri Olsa63503db2019-07-21 13:23:52 +02002716static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03002717 const char *name)
2718{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002719 struct evsel *evsel;
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03002720
2721 evlist__for_each_entry(evlist, evsel) {
2722 if (evsel->id && evsel->id[0] == id) {
2723 if (evsel->name)
2724 zfree(&evsel->name);
2725 evsel->name = strdup(name);
2726 break;
2727 }
2728 }
2729}
2730
Jiri Olsa32dcd022019-07-21 13:23:51 +02002731static struct evsel *intel_pt_evsel(struct intel_pt *pt,
Jiri Olsa63503db2019-07-21 13:23:52 +02002732 struct evlist *evlist)
Adrian Hunter85a564d2017-05-26 11:17:30 +03002733{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002734 struct evsel *evsel;
Adrian Hunter85a564d2017-05-26 11:17:30 +03002735
2736 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002737 if (evsel->core.attr.type == pt->pmu_type && evsel->ids)
Adrian Hunter85a564d2017-05-26 11:17:30 +03002738 return evsel;
2739 }
2740
2741 return NULL;
2742}
2743
Adrian Hunter90e457f2015-07-17 19:33:41 +03002744static int intel_pt_synth_events(struct intel_pt *pt,
2745 struct perf_session *session)
2746{
Jiri Olsa63503db2019-07-21 13:23:52 +02002747 struct evlist *evlist = session->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002748 struct evsel *evsel = intel_pt_evsel(pt, evlist);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002749 struct perf_event_attr attr;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002750 u64 id;
2751 int err;
2752
Adrian Hunter85a564d2017-05-26 11:17:30 +03002753 if (!evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002754 pr_debug("There are no selected events with Intel Processor Trace data\n");
2755 return 0;
2756 }
2757
2758 memset(&attr, 0, sizeof(struct perf_event_attr));
2759 attr.size = sizeof(struct perf_event_attr);
2760 attr.type = PERF_TYPE_HARDWARE;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002761 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002762 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
2763 PERF_SAMPLE_PERIOD;
2764 if (pt->timeless_decoding)
2765 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
2766 else
2767 attr.sample_type |= PERF_SAMPLE_TIME;
2768 if (!pt->per_cpu_mmaps)
2769 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002770 attr.exclude_user = evsel->core.attr.exclude_user;
2771 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
2772 attr.exclude_hv = evsel->core.attr.exclude_hv;
2773 attr.exclude_host = evsel->core.attr.exclude_host;
2774 attr.exclude_guest = evsel->core.attr.exclude_guest;
2775 attr.sample_id_all = evsel->core.attr.sample_id_all;
2776 attr.read_format = evsel->core.attr.read_format;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002777
2778 id = evsel->id[0] + 1000000000;
2779 if (!id)
2780 id = 1;
2781
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002782 if (pt->synth_opts.branches) {
2783 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2784 attr.sample_period = 1;
2785 attr.sample_type |= PERF_SAMPLE_ADDR;
2786 err = intel_pt_synth_event(session, "branches", &attr, id);
2787 if (err)
2788 return err;
2789 pt->sample_branches = true;
2790 pt->branches_sample_type = attr.sample_type;
2791 pt->branches_id = id;
2792 id += 1;
2793 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
2794 }
2795
2796 if (pt->synth_opts.callchain)
2797 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2798 if (pt->synth_opts.last_branch)
2799 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2800
Adrian Hunter90e457f2015-07-17 19:33:41 +03002801 if (pt->synth_opts.instructions) {
2802 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2803 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2804 attr.sample_period =
2805 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2806 else
2807 attr.sample_period = pt->synth_opts.period;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002808 err = intel_pt_synth_event(session, "instructions", &attr, id);
2809 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002810 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002811 pt->sample_instructions = true;
2812 pt->instructions_sample_type = attr.sample_type;
2813 pt->instructions_id = id;
2814 id += 1;
2815 }
2816
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002817 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
2818 attr.sample_period = 1;
2819
Adrian Hunter90e457f2015-07-17 19:33:41 +03002820 if (pt->synth_opts.transactions) {
2821 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002822 err = intel_pt_synth_event(session, "transactions", &attr, id);
2823 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002824 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002825 pt->sample_transactions = true;
Adrian Hunter21160742017-05-26 11:17:18 +03002826 pt->transactions_sample_type = attr.sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002827 pt->transactions_id = id;
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03002828 intel_pt_set_event_name(evlist, id, "transactions");
Adrian Hunter90e457f2015-07-17 19:33:41 +03002829 id += 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002830 }
2831
Adrian Hunter37973072017-06-30 11:36:45 +03002832 attr.type = PERF_TYPE_SYNTH;
2833 attr.sample_type |= PERF_SAMPLE_RAW;
2834
2835 if (pt->synth_opts.ptwrites) {
2836 attr.config = PERF_SYNTH_INTEL_PTWRITE;
2837 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
2838 if (err)
2839 return err;
2840 pt->sample_ptwrites = true;
2841 pt->ptwrites_sample_type = attr.sample_type;
2842 pt->ptwrites_id = id;
2843 intel_pt_set_event_name(evlist, id, "ptwrite");
2844 id += 1;
2845 }
2846
2847 if (pt->synth_opts.pwr_events) {
2848 pt->sample_pwr_events = true;
2849 pt->pwr_events_sample_type = attr.sample_type;
2850
2851 attr.config = PERF_SYNTH_INTEL_CBR;
2852 err = intel_pt_synth_event(session, "cbr", &attr, id);
2853 if (err)
2854 return err;
2855 pt->cbr_id = id;
2856 intel_pt_set_event_name(evlist, id, "cbr");
2857 id += 1;
2858 }
2859
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002860 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & 0x10)) {
Adrian Hunter37973072017-06-30 11:36:45 +03002861 attr.config = PERF_SYNTH_INTEL_MWAIT;
2862 err = intel_pt_synth_event(session, "mwait", &attr, id);
2863 if (err)
2864 return err;
2865 pt->mwait_id = id;
2866 intel_pt_set_event_name(evlist, id, "mwait");
2867 id += 1;
2868
2869 attr.config = PERF_SYNTH_INTEL_PWRE;
2870 err = intel_pt_synth_event(session, "pwre", &attr, id);
2871 if (err)
2872 return err;
2873 pt->pwre_id = id;
2874 intel_pt_set_event_name(evlist, id, "pwre");
2875 id += 1;
2876
2877 attr.config = PERF_SYNTH_INTEL_EXSTOP;
2878 err = intel_pt_synth_event(session, "exstop", &attr, id);
2879 if (err)
2880 return err;
2881 pt->exstop_id = id;
2882 intel_pt_set_event_name(evlist, id, "exstop");
2883 id += 1;
2884
2885 attr.config = PERF_SYNTH_INTEL_PWRX;
2886 err = intel_pt_synth_event(session, "pwrx", &attr, id);
2887 if (err)
2888 return err;
2889 pt->pwrx_id = id;
2890 intel_pt_set_event_name(evlist, id, "pwrx");
2891 id += 1;
2892 }
2893
Adrian Hunter90e457f2015-07-17 19:33:41 +03002894 return 0;
2895}
2896
Adrian Hunter9e64cef2019-08-06 11:46:04 +03002897static void intel_pt_setup_pebs_events(struct intel_pt *pt)
2898{
2899 struct evsel *evsel;
2900
2901 if (!pt->synth_opts.other_events)
2902 return;
2903
2904 evlist__for_each_entry(pt->session->evlist, evsel) {
2905 if (evsel->core.attr.aux_output && evsel->id) {
2906 pt->sample_pebs = true;
2907 pt->pebs_evsel = evsel;
2908 return;
2909 }
2910 }
2911}
2912
Jiri Olsa63503db2019-07-21 13:23:52 +02002913static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002914{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002915 struct evsel *evsel;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002916
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002917 evlist__for_each_entry_reverse(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002918 const char *name = perf_evsel__name(evsel);
2919
2920 if (!strcmp(name, "sched:sched_switch"))
2921 return evsel;
2922 }
2923
2924 return NULL;
2925}
2926
Jiri Olsa63503db2019-07-21 13:23:52 +02002927static bool intel_pt_find_switch(struct evlist *evlist)
Adrian Hunter86c27862015-08-13 12:40:57 +03002928{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002929 struct evsel *evsel;
Adrian Hunter86c27862015-08-13 12:40:57 +03002930
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002931 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002932 if (evsel->core.attr.context_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002933 return true;
2934 }
2935
2936 return false;
2937}
2938
Adrian Hunterba11ba62015-09-25 16:15:56 +03002939static int intel_pt_perf_config(const char *var, const char *value, void *data)
2940{
2941 struct intel_pt *pt = data;
2942
2943 if (!strcmp(var, "intel-pt.mispred-all"))
2944 pt->mispred_all = perf_config_bool(var, value);
2945
2946 return 0;
2947}
2948
Adrian Hunter2c47db92019-06-04 16:00:09 +03002949/* Find least TSC which converts to ns or later */
2950static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
2951{
2952 u64 tsc, tm;
2953
2954 tsc = perf_time_to_tsc(ns, &pt->tc);
2955
2956 while (1) {
2957 tm = tsc_to_perf_time(tsc, &pt->tc);
2958 if (tm < ns)
2959 break;
2960 tsc -= 1;
2961 }
2962
2963 while (tm < ns)
2964 tm = tsc_to_perf_time(++tsc, &pt->tc);
2965
2966 return tsc;
2967}
2968
2969/* Find greatest TSC which converts to ns or earlier */
2970static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
2971{
2972 u64 tsc, tm;
2973
2974 tsc = perf_time_to_tsc(ns, &pt->tc);
2975
2976 while (1) {
2977 tm = tsc_to_perf_time(tsc, &pt->tc);
2978 if (tm > ns)
2979 break;
2980 tsc += 1;
2981 }
2982
2983 while (tm > ns)
2984 tm = tsc_to_perf_time(--tsc, &pt->tc);
2985
2986 return tsc;
2987}
2988
2989static int intel_pt_setup_time_ranges(struct intel_pt *pt,
2990 struct itrace_synth_opts *opts)
2991{
2992 struct perf_time_interval *p = opts->ptime_range;
2993 int n = opts->range_num;
2994 int i;
2995
2996 if (!n || !p || pt->timeless_decoding)
2997 return 0;
2998
2999 pt->time_ranges = calloc(n, sizeof(struct range));
3000 if (!pt->time_ranges)
3001 return -ENOMEM;
3002
3003 pt->range_cnt = n;
3004
3005 intel_pt_log("%s: %u range(s)\n", __func__, n);
3006
3007 for (i = 0; i < n; i++) {
3008 struct range *r = &pt->time_ranges[i];
3009 u64 ts = p[i].start;
3010 u64 te = p[i].end;
3011
3012 /*
3013 * Take care to ensure the TSC range matches the perf-time range
3014 * when converted back to perf-time.
3015 */
3016 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
3017 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
3018
3019 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
3020 i, ts, te);
3021 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
3022 i, r->start, r->end);
3023 }
3024
3025 return 0;
3026}
3027
Adrian Hunter90e457f2015-07-17 19:33:41 +03003028static const char * const intel_pt_info_fmts[] = {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03003029 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
3030 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
3031 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
3032 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
3033 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
3034 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
3035 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
3036 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
3037 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
3038 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
3039 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
3040 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
3041 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
3042 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003043 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003044 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
Adrian Hunter90e457f2015-07-17 19:33:41 +03003045};
3046
Jiri Olsa9a8dad02019-08-28 15:57:02 +02003047static void intel_pt_print_info(__u64 *arr, int start, int finish)
Adrian Hunter90e457f2015-07-17 19:33:41 +03003048{
3049 int i;
3050
3051 if (!dump_trace)
3052 return;
3053
3054 for (i = start; i <= finish; i++)
3055 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
3056}
3057
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003058static void intel_pt_print_info_str(const char *name, const char *str)
3059{
3060 if (!dump_trace)
3061 return;
3062
3063 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
3064}
3065
Jiri Olsa72932372019-08-28 15:57:16 +02003066static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
Adrian Hunter40b746a2016-09-23 17:38:44 +03003067{
3068 return auxtrace_info->header.size >=
Jiri Olsa72932372019-08-28 15:57:16 +02003069 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
Adrian Hunter40b746a2016-09-23 17:38:44 +03003070}
3071
Adrian Hunter90e457f2015-07-17 19:33:41 +03003072int intel_pt_process_auxtrace_info(union perf_event *event,
3073 struct perf_session *session)
3074{
Jiri Olsa72932372019-08-28 15:57:16 +02003075 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003076 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
3077 struct intel_pt *pt;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003078 void *info_end;
Jiri Olsa9a8dad02019-08-28 15:57:02 +02003079 __u64 *info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003080 int err;
3081
Jiri Olsa72932372019-08-28 15:57:16 +02003082 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
Adrian Hunter90e457f2015-07-17 19:33:41 +03003083 min_sz)
3084 return -EINVAL;
3085
3086 pt = zalloc(sizeof(struct intel_pt));
3087 if (!pt)
3088 return -ENOMEM;
3089
Adrian Hunter2acee102016-09-23 17:38:48 +03003090 addr_filters__init(&pt->filts);
3091
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03003092 err = perf_config(intel_pt_perf_config, pt);
3093 if (err)
3094 goto err_free;
Adrian Hunterba11ba62015-09-25 16:15:56 +03003095
Adrian Hunter90e457f2015-07-17 19:33:41 +03003096 err = auxtrace_queues__init(&pt->queues);
3097 if (err)
3098 goto err_free;
3099
3100 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
3101
3102 pt->session = session;
3103 pt->machine = &session->machines.host; /* No kvm support */
3104 pt->auxtrace_type = auxtrace_info->type;
3105 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
3106 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
3107 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
3108 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
3109 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
3110 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
3111 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
3112 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
3113 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
3114 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
3115 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
3116 INTEL_PT_PER_CPU_MMAPS);
3117
Adrian Hunter40b746a2016-09-23 17:38:44 +03003118 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03003119 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
3120 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
3121 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
3122 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
3123 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
3124 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
3125 INTEL_PT_CYC_BIT);
3126 }
3127
Adrian Hunter40b746a2016-09-23 17:38:44 +03003128 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003129 pt->max_non_turbo_ratio =
3130 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
3131 intel_pt_print_info(&auxtrace_info->priv[0],
3132 INTEL_PT_MAX_NONTURBO_RATIO,
3133 INTEL_PT_MAX_NONTURBO_RATIO);
3134 }
3135
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003136 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
3137 info_end = (void *)info + auxtrace_info->header.size;
3138
3139 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
3140 size_t len;
3141
3142 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
3143 intel_pt_print_info(&auxtrace_info->priv[0],
3144 INTEL_PT_FILTER_STR_LEN,
3145 INTEL_PT_FILTER_STR_LEN);
3146 if (len) {
3147 const char *filter = (const char *)info;
3148
3149 len = roundup(len + 1, 8);
3150 info += len >> 3;
3151 if ((void *)info > info_end) {
3152 pr_err("%s: bad filter string length\n", __func__);
3153 err = -EINVAL;
3154 goto err_free_queues;
3155 }
3156 pt->filter = memdup(filter, len);
3157 if (!pt->filter) {
3158 err = -ENOMEM;
3159 goto err_free_queues;
3160 }
3161 if (session->header.needs_swap)
3162 mem_bswap_64(pt->filter, len);
3163 if (pt->filter[len - 1]) {
3164 pr_err("%s: filter string not null terminated\n", __func__);
3165 err = -EINVAL;
3166 goto err_free_queues;
3167 }
Adrian Hunter2acee102016-09-23 17:38:48 +03003168 err = addr_filters__parse_bare_filter(&pt->filts,
3169 filter);
3170 if (err)
3171 goto err_free_queues;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003172 }
3173 intel_pt_print_info_str("Filter string", pt->filter);
3174 }
3175
Adrian Hunter90e457f2015-07-17 19:33:41 +03003176 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
Adrian Hunter07633382019-03-01 12:35:36 +02003177 if (pt->timeless_decoding && !pt->tc.time_mult)
3178 pt->tc.time_mult = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003179 pt->have_tsc = intel_pt_have_tsc(pt);
3180 pt->sampling_mode = false;
3181 pt->est_tsc = !pt->timeless_decoding;
3182
3183 pt->unknown_thread = thread__new(999999999, 999999999);
3184 if (!pt->unknown_thread) {
3185 err = -ENOMEM;
3186 goto err_free_queues;
3187 }
Adrian Hunter3a4acda2016-02-01 03:21:04 +00003188
3189 /*
3190 * Since this thread will not be kept in any rbtree not in a
3191 * list, initialize its list node so that at thread__put() the
3192 * current thread lifetime assuption is kept and we don't segfault
3193 * at list_del_init().
3194 */
3195 INIT_LIST_HEAD(&pt->unknown_thread->node);
3196
Adrian Hunter90e457f2015-07-17 19:33:41 +03003197 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
3198 if (err)
3199 goto err_delete_thread;
3200 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
3201 err = -ENOMEM;
3202 goto err_delete_thread;
3203 }
3204
3205 pt->auxtrace.process_event = intel_pt_process_event;
3206 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
3207 pt->auxtrace.flush_events = intel_pt_flush;
3208 pt->auxtrace.free_events = intel_pt_free_events;
3209 pt->auxtrace.free = intel_pt_free;
3210 session->auxtrace = &pt->auxtrace;
3211
3212 if (dump_trace)
3213 return 0;
3214
3215 if (pt->have_sched_switch == 1) {
3216 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
3217 if (!pt->switch_evsel) {
3218 pr_err("%s: missing sched_switch event\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003219 err = -EINVAL;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003220 goto err_delete_thread;
3221 }
Adrian Hunter86c27862015-08-13 12:40:57 +03003222 } else if (pt->have_sched_switch == 2 &&
3223 !intel_pt_find_switch(session->evlist)) {
3224 pr_err("%s: missing context_switch attribute flag\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003225 err = -EINVAL;
Adrian Hunter86c27862015-08-13 12:40:57 +03003226 goto err_delete_thread;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003227 }
3228
Leo Yan323fd742019-07-08 22:39:36 +08003229 if (session->itrace_synth_opts->set) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003230 pt->synth_opts = *session->itrace_synth_opts;
3231 } else {
Andi Kleen4eb06812018-09-20 11:05:37 -07003232 itrace_synth_opts__set_default(&pt->synth_opts,
3233 session->itrace_synth_opts->default_no_sample);
Adrian Hunter26f19c22019-05-20 14:37:07 +03003234 if (!session->itrace_synth_opts->default_no_sample &&
3235 !session->itrace_synth_opts->inject) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003236 pt->synth_opts.branches = false;
3237 pt->synth_opts.callchain = true;
3238 }
Leo Yan323fd742019-07-08 22:39:36 +08003239 pt->synth_opts.thread_stack =
Adrian Hunter50f736372016-06-23 16:40:57 +03003240 session->itrace_synth_opts->thread_stack;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003241 }
3242
3243 if (pt->synth_opts.log)
3244 intel_pt_log_enable();
3245
3246 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
3247 if (pt->tc.time_mult) {
3248 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
3249
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003250 if (!pt->max_non_turbo_ratio)
3251 pt->max_non_turbo_ratio =
3252 (tsc_freq + 50000000) / 100000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003253 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
3254 intel_pt_log("Maximum non-turbo ratio %u\n",
3255 pt->max_non_turbo_ratio);
Adrian Hunter37973072017-06-30 11:36:45 +03003256 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003257 }
3258
Leo Yan323fd742019-07-08 22:39:36 +08003259 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
3260 if (err)
3261 goto err_delete_thread;
Adrian Hunter2c47db92019-06-04 16:00:09 +03003262
Adrian Hunter90e457f2015-07-17 19:33:41 +03003263 if (pt->synth_opts.calls)
3264 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
3265 PERF_IP_FLAG_TRACE_END;
3266 if (pt->synth_opts.returns)
3267 pt->branches_filter |= PERF_IP_FLAG_RETURN |
3268 PERF_IP_FLAG_TRACE_BEGIN;
3269
3270 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
3271 symbol_conf.use_callchain = true;
3272 if (callchain_register_param(&callchain_param) < 0) {
3273 symbol_conf.use_callchain = false;
3274 pt->synth_opts.callchain = false;
3275 }
3276 }
3277
3278 err = intel_pt_synth_events(pt, session);
3279 if (err)
3280 goto err_delete_thread;
3281
Adrian Hunter9e64cef2019-08-06 11:46:04 +03003282 intel_pt_setup_pebs_events(pt);
3283
Adrian Hunter90e457f2015-07-17 19:33:41 +03003284 err = auxtrace_queues__process_index(&pt->queues, session);
3285 if (err)
3286 goto err_delete_thread;
3287
3288 if (pt->queues.populated)
3289 pt->data_queued = true;
3290
3291 if (pt->timeless_decoding)
3292 pr_debug2("Intel PT decoding without timestamps\n");
3293
3294 return 0;
3295
3296err_delete_thread:
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03003297 thread__zput(pt->unknown_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003298err_free_queues:
3299 intel_pt_log_disable();
3300 auxtrace_queues__free(&pt->queues);
3301 session->auxtrace = NULL;
3302err_free:
Adrian Hunter2acee102016-09-23 17:38:48 +03003303 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003304 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03003305 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003306 free(pt);
3307 return err;
3308}