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