perf intel-pt: Add gp registers to synthesized PEBS sample
[linux-2.6-block.git] / tools / perf / util / intel-pt.c
index 7a70693c1b918d151735b4fe8ffe2b631402d1d7..00c2c96bb8052580546419461c2e8ba596a70e90 100644 (file)
@@ -1,16 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0-only
 /*
  * intel_pt.c: Intel Processor Trace support
  * Copyright (c) 2013-2015, Intel Corporation.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
  */
 
 #include <inttypes.h>
@@ -42,6 +33,9 @@
 #include "tsc.h"
 #include "intel-pt.h"
 #include "config.h"
+#include "time-utils.h"
+
+#include "../arch/x86/include/uapi/asm/perf_regs.h"
 
 #include "intel-pt-decoder/intel-pt-log.h"
 #include "intel-pt-decoder/intel-pt-decoder.h"
 
 #define MAX_TIMESTAMP (~0ULL)
 
+struct range {
+       u64 start;
+       u64 end;
+};
+
 struct intel_pt {
        struct auxtrace auxtrace;
        struct auxtrace_queues queues;
@@ -104,6 +103,9 @@ struct intel_pt {
        u64 pwrx_id;
        u64 cbr_id;
 
+       bool sample_pebs;
+       struct perf_evsel *pebs_evsel;
+
        u64 tsc_bit;
        u64 mtc_bit;
        u64 mtc_freq_bits;
@@ -118,6 +120,9 @@ struct intel_pt {
 
        char *filter;
        struct addr_filters filts;
+
+       struct range *time_ranges;
+       unsigned int range_cnt;
 };
 
 enum switch_state {
@@ -154,9 +159,18 @@ struct intel_pt_queue {
        bool have_sample;
        u64 time;
        u64 timestamp;
+       u64 sel_timestamp;
+       bool sel_start;
+       unsigned int sel_idx;
        u32 flags;
        u16 insn_len;
        u64 last_insn_cnt;
+       u64 ipc_insn_cnt;
+       u64 ipc_cyc_cnt;
+       u64 last_in_insn_cnt;
+       u64 last_in_cyc_cnt;
+       u64 last_br_insn_cnt;
+       u64 last_br_cyc_cnt;
        char insn[INTEL_PT_INSN_BUF_SZ];
 };
 
@@ -168,13 +182,14 @@ static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
        int ret, pkt_len, i;
        char desc[INTEL_PT_PKT_DESC_MAX];
        const char *color = PERF_COLOR_BLUE;
+       enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
 
        color_fprintf(stdout, color,
                      ". ... Intel Processor Trace data: size %zu bytes\n",
                      len);
 
        while (len) {
-               ret = intel_pt_get_packet(buf, len, &packet);
+               ret = intel_pt_get_packet(buf, len, &packet, &ctx);
                if (ret > 0)
                        pkt_len = ret;
                else
@@ -233,32 +248,13 @@ static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *
        return 0;
 }
 
-/* This function assumes data is processed sequentially only */
-static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
+static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
+                              struct auxtrace_buffer *buffer,
+                              struct auxtrace_buffer *old_buffer,
+                              struct intel_pt_buffer *b)
 {
-       struct intel_pt_queue *ptq = data;
-       struct auxtrace_buffer *buffer = ptq->buffer;
-       struct auxtrace_buffer *old_buffer = ptq->old_buffer;
-       struct auxtrace_queue *queue;
        bool might_overlap;
 
-       if (ptq->stop) {
-               b->len = 0;
-               return 0;
-       }
-
-       queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
-
-       buffer = auxtrace_buffer__next(queue, buffer);
-       if (!buffer) {
-               if (old_buffer)
-                       auxtrace_buffer__drop_data(old_buffer);
-               b->len = 0;
-               return 0;
-       }
-
-       ptq->buffer = buffer;
-
        if (!buffer->data) {
                int fd = perf_data__fd(ptq->pt->session->data);
 
@@ -288,6 +284,95 @@ static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
                b->consecutive = true;
        }
 
+       return 0;
+}
+
+/* Do not drop buffers with references - refer intel_pt_get_trace() */
+static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
+                                          struct auxtrace_buffer *buffer)
+{
+       if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
+               return;
+
+       auxtrace_buffer__drop_data(buffer);
+}
+
+/* Must be serialized with respect to intel_pt_get_trace() */
+static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
+                             void *cb_data)
+{
+       struct intel_pt_queue *ptq = data;
+       struct auxtrace_buffer *buffer = ptq->buffer;
+       struct auxtrace_buffer *old_buffer = ptq->old_buffer;
+       struct auxtrace_queue *queue;
+       int err = 0;
+
+       queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
+
+       while (1) {
+               struct intel_pt_buffer b = { .len = 0 };
+
+               buffer = auxtrace_buffer__next(queue, buffer);
+               if (!buffer)
+                       break;
+
+               err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
+               if (err)
+                       break;
+
+               if (b.len) {
+                       intel_pt_lookahead_drop_buffer(ptq, old_buffer);
+                       old_buffer = buffer;
+               } else {
+                       intel_pt_lookahead_drop_buffer(ptq, buffer);
+                       continue;
+               }
+
+               err = cb(&b, cb_data);
+               if (err)
+                       break;
+       }
+
+       if (buffer != old_buffer)
+               intel_pt_lookahead_drop_buffer(ptq, buffer);
+       intel_pt_lookahead_drop_buffer(ptq, old_buffer);
+
+       return err;
+}
+
+/*
+ * This function assumes data is processed sequentially only.
+ * Must be serialized with respect to intel_pt_lookahead()
+ */
+static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
+{
+       struct intel_pt_queue *ptq = data;
+       struct auxtrace_buffer *buffer = ptq->buffer;
+       struct auxtrace_buffer *old_buffer = ptq->old_buffer;
+       struct auxtrace_queue *queue;
+       int err;
+
+       if (ptq->stop) {
+               b->len = 0;
+               return 0;
+       }
+
+       queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
+
+       buffer = auxtrace_buffer__next(queue, buffer);
+       if (!buffer) {
+               if (old_buffer)
+                       auxtrace_buffer__drop_data(old_buffer);
+               b->len = 0;
+               return 0;
+       }
+
+       ptq->buffer = buffer;
+
+       err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
+       if (err)
+               return err;
+
        if (ptq->step_through_buffers)
                ptq->stop = true;
 
@@ -807,6 +892,7 @@ static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
 
        params.get_trace = intel_pt_get_trace;
        params.walk_insn = intel_pt_walk_next_insn;
+       params.lookahead = intel_pt_lookahead;
        params.data = ptq;
        params.return_compression = intel_pt_return_compression(pt);
        params.branch_enable = intel_pt_branch_enable(pt);
@@ -930,6 +1016,23 @@ static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
                ptq->flags |= PERF_IP_FLAG_TRACE_END;
 }
 
+static void intel_pt_setup_time_range(struct intel_pt *pt,
+                                     struct intel_pt_queue *ptq)
+{
+       if (!pt->range_cnt)
+               return;
+
+       ptq->sel_timestamp = pt->time_ranges[0].start;
+       ptq->sel_idx = 0;
+
+       if (ptq->sel_timestamp) {
+               ptq->sel_start = true;
+       } else {
+               ptq->sel_timestamp = pt->time_ranges[0].end;
+               ptq->sel_start = false;
+       }
+}
+
 static int intel_pt_setup_queue(struct intel_pt *pt,
                                struct auxtrace_queue *queue,
                                unsigned int queue_nr)
@@ -954,6 +1057,8 @@ static int intel_pt_setup_queue(struct intel_pt *pt,
                        ptq->step_through_buffers = true;
 
                ptq->sync_switch = pt->sync_switch;
+
+               intel_pt_setup_time_range(pt, ptq);
        }
 
        if (!ptq->on_heap &&
@@ -968,6 +1073,14 @@ static int intel_pt_setup_queue(struct intel_pt *pt,
                intel_pt_log("queue %u getting timestamp\n", queue_nr);
                intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
                             queue_nr, ptq->cpu, ptq->pid, ptq->tid);
+
+               if (ptq->sel_start && ptq->sel_timestamp) {
+                       ret = intel_pt_fast_forward(ptq->decoder,
+                                                   ptq->sel_timestamp);
+                       if (ret)
+                               return ret;
+               }
+
                while (1) {
                        state = intel_pt_decode(ptq->decoder);
                        if (state->err) {
@@ -987,6 +1100,9 @@ static int intel_pt_setup_queue(struct intel_pt *pt,
                             queue_nr, ptq->timestamp);
                ptq->state = state;
                ptq->have_sample = true;
+               if (ptq->sel_start && ptq->sel_timestamp &&
+                   ptq->timestamp < ptq->sel_timestamp)
+                       ptq->have_sample = false;
                intel_pt_sample_flags(ptq);
                ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
                if (ret)
@@ -1068,28 +1184,37 @@ static inline bool intel_pt_skip_event(struct intel_pt *pt)
               pt->num_events++ < pt->synth_opts.initial_skip;
 }
 
+static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
+                                  union perf_event *event,
+                                  struct perf_sample *sample)
+{
+       event->sample.header.type = PERF_RECORD_SAMPLE;
+       event->sample.header.size = sizeof(struct perf_event_header);
+
+       sample->pid = ptq->pid;
+       sample->tid = ptq->tid;
+       sample->cpu = ptq->cpu;
+       sample->insn_len = ptq->insn_len;
+       memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
+}
+
 static void intel_pt_prep_b_sample(struct intel_pt *pt,
                                   struct intel_pt_queue *ptq,
                                   union perf_event *event,
                                   struct perf_sample *sample)
 {
+       intel_pt_prep_a_sample(ptq, event, sample);
+
        if (!pt->timeless_decoding)
                sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
 
        sample->ip = ptq->state->from_ip;
        sample->cpumode = intel_pt_cpumode(pt, sample->ip);
-       sample->pid = ptq->pid;
-       sample->tid = ptq->tid;
        sample->addr = ptq->state->to_ip;
        sample->period = 1;
-       sample->cpu = ptq->cpu;
        sample->flags = ptq->flags;
-       sample->insn_len = ptq->insn_len;
-       memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
 
-       event->sample.header.type = PERF_RECORD_SAMPLE;
        event->sample.header.misc = sample->cpumode;
-       event->sample.header.size = sizeof(struct perf_event_header);
 }
 
 static int intel_pt_inject_event(union perf_event *event,
@@ -1162,6 +1287,13 @@ static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
                sample.branch_stack = (struct branch_stack *)&dummy_bs;
        }
 
+       sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
+       if (sample.cyc_cnt) {
+               sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
+               ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
+               ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
+       }
+
        return intel_pt_deliver_synth_b_event(pt, event, &sample,
                                              pt->branches_sample_type);
 }
@@ -1217,6 +1349,13 @@ static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
        sample.stream_id = ptq->pt->instructions_id;
        sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
 
+       sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
+       if (sample.cyc_cnt) {
+               sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
+               ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
+               ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
+       }
+
        ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
 
        return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
@@ -1410,6 +1549,126 @@ static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
                                            pt->pwr_events_sample_type);
 }
 
+/*
+ * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
+ * intel_pt_add_gp_regs().
+ */
+static const int pebs_gp_regs[] = {
+       [PERF_REG_X86_FLAGS]    = 1,
+       [PERF_REG_X86_IP]       = 2,
+       [PERF_REG_X86_AX]       = 3,
+       [PERF_REG_X86_CX]       = 4,
+       [PERF_REG_X86_DX]       = 5,
+       [PERF_REG_X86_BX]       = 6,
+       [PERF_REG_X86_SP]       = 7,
+       [PERF_REG_X86_BP]       = 8,
+       [PERF_REG_X86_SI]       = 9,
+       [PERF_REG_X86_DI]       = 10,
+       [PERF_REG_X86_R8]       = 11,
+       [PERF_REG_X86_R9]       = 12,
+       [PERF_REG_X86_R10]      = 13,
+       [PERF_REG_X86_R11]      = 14,
+       [PERF_REG_X86_R12]      = 15,
+       [PERF_REG_X86_R13]      = 16,
+       [PERF_REG_X86_R14]      = 17,
+       [PERF_REG_X86_R15]      = 18,
+};
+
+static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
+                                const struct intel_pt_blk_items *items,
+                                u64 regs_mask)
+{
+       const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
+       u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
+       u32 bit;
+       int i;
+
+       for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
+               /* Get the PEBS gp_regs array index */
+               int n = pebs_gp_regs[i] - 1;
+
+               if (n < 0)
+                       continue;
+               /*
+                * Add only registers that were requested (i.e. 'regs_mask') and
+                * that were provided (i.e. 'mask'), and update the resulting
+                * mask (i.e. 'intr_regs->mask') accordingly.
+                */
+               if (mask & 1 << n && regs_mask & bit) {
+                       intr_regs->mask |= bit;
+                       *pos++ = gp_regs[n];
+               }
+       }
+
+       return pos;
+}
+
+static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
+{
+       const struct intel_pt_blk_items *items = &ptq->state->items;
+       struct perf_sample sample = { .ip = 0, };
+       union perf_event *event = ptq->event_buf;
+       struct intel_pt *pt = ptq->pt;
+       struct perf_evsel *evsel = pt->pebs_evsel;
+       u64 sample_type = evsel->attr.sample_type;
+       u64 id = evsel->id[0];
+       u8 cpumode;
+
+       if (intel_pt_skip_event(pt))
+               return 0;
+
+       intel_pt_prep_a_sample(ptq, event, &sample);
+
+       sample.id = id;
+       sample.stream_id = id;
+
+       if (!evsel->attr.freq)
+               sample.period = evsel->attr.sample_period;
+
+       /* No support for non-zero CS base */
+       if (items->has_ip)
+               sample.ip = items->ip;
+       else if (items->has_rip)
+               sample.ip = items->rip;
+       else
+               sample.ip = ptq->state->from_ip;
+
+       /* No support for guest mode at this time */
+       cpumode = sample.ip < ptq->pt->kernel_start ?
+                 PERF_RECORD_MISC_USER :
+                 PERF_RECORD_MISC_KERNEL;
+
+       event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
+
+       sample.cpumode = cpumode;
+
+       if (sample_type & PERF_SAMPLE_TIME) {
+               u64 timestamp = 0;
+
+               if (items->has_timestamp)
+                       timestamp = items->timestamp;
+               else if (!pt->timeless_decoding)
+                       timestamp = ptq->timestamp;
+               if (timestamp)
+                       sample.time = tsc_to_perf_time(timestamp, &pt->tc);
+       }
+
+       if (sample_type & PERF_SAMPLE_REGS_INTR &&
+           items->mask[INTEL_PT_GP_REGS_POS]) {
+               u64 regs[sizeof(sample.intr_regs.mask)];
+               u64 regs_mask = evsel->attr.sample_regs_intr;
+
+               sample.intr_regs.abi = items->is_32_bit ?
+                                      PERF_SAMPLE_REGS_ABI_32 :
+                                      PERF_SAMPLE_REGS_ABI_64;
+               sample.intr_regs.regs = regs;
+
+               intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
+       }
+
+       return intel_pt_deliver_synth_event(pt, ptq, event, &sample, sample_type);
+}
+
 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
                                pid_t pid, pid_t tid, u64 ip, u64 timestamp)
 {
@@ -1488,6 +1747,25 @@ static int intel_pt_sample(struct intel_pt_queue *ptq)
 
        ptq->have_sample = false;
 
+       if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) {
+               /*
+                * Cycle count and instruction count only go together to create
+                * a valid IPC ratio when the cycle count changes.
+                */
+               ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
+               ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
+       }
+
+       /*
+        * Do PEBS first to allow for the possibility that the PEBS timestamp
+        * precedes the current timestamp.
+        */
+       if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
+               err = intel_pt_synth_pebs_sample(ptq);
+               if (err)
+                       return err;
+       }
+
        if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) {
                if (state->type & INTEL_PT_CBR_CHG) {
                        err = intel_pt_synth_cbr_sample(ptq);
@@ -1650,10 +1928,83 @@ static void intel_pt_enable_sync_switch(struct intel_pt *pt)
        }
 }
 
+/*
+ * To filter against time ranges, it is only necessary to look at the next start
+ * or end time.
+ */
+static bool intel_pt_next_time(struct intel_pt_queue *ptq)
+{
+       struct intel_pt *pt = ptq->pt;
+
+       if (ptq->sel_start) {
+               /* Next time is an end time */
+               ptq->sel_start = false;
+               ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
+               return true;
+       } else if (ptq->sel_idx + 1 < pt->range_cnt) {
+               /* Next time is a start time */
+               ptq->sel_start = true;
+               ptq->sel_idx += 1;
+               ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
+               return true;
+       }
+
+       /* No next time */
+       return false;
+}
+
+static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
+{
+       int err;
+
+       while (1) {
+               if (ptq->sel_start) {
+                       if (ptq->timestamp >= ptq->sel_timestamp) {
+                               /* After start time, so consider next time */
+                               intel_pt_next_time(ptq);
+                               if (!ptq->sel_timestamp) {
+                                       /* No end time */
+                                       return 0;
+                               }
+                               /* Check against end time */
+                               continue;
+                       }
+                       /* Before start time, so fast forward */
+                       ptq->have_sample = false;
+                       if (ptq->sel_timestamp > *ff_timestamp) {
+                               if (ptq->sync_switch) {
+                                       intel_pt_next_tid(ptq->pt, ptq);
+                                       ptq->switch_state = INTEL_PT_SS_UNKNOWN;
+                               }
+                               *ff_timestamp = ptq->sel_timestamp;
+                               err = intel_pt_fast_forward(ptq->decoder,
+                                                           ptq->sel_timestamp);
+                               if (err)
+                                       return err;
+                       }
+                       return 0;
+               } else if (ptq->timestamp > ptq->sel_timestamp) {
+                       /* After end time, so consider next time */
+                       if (!intel_pt_next_time(ptq)) {
+                               /* No next time range, so stop decoding */
+                               ptq->have_sample = false;
+                               ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
+                               return 1;
+                       }
+                       /* Check against next start time */
+                       continue;
+               } else {
+                       /* Before end time */
+                       return 0;
+               }
+       }
+}
+
 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
 {
        const struct intel_pt_state *state = ptq->state;
        struct intel_pt *pt = ptq->pt;
+       u64 ff_timestamp = 0;
        int err;
 
        if (!pt->kernel_start) {
@@ -1718,6 +2069,12 @@ static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
                        ptq->timestamp = state->timestamp;
                }
 
+               if (ptq->sel_timestamp) {
+                       err = intel_pt_time_filter(ptq, &ff_timestamp);
+                       if (err)
+                               return err;
+               }
+
                if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
                        *timestamp = ptq->timestamp;
                        return 0;
@@ -2123,6 +2480,7 @@ static void intel_pt_free(struct perf_session *session)
        thread__put(pt->unknown_thread);
        addr_filters__exit(&pt->filts);
        zfree(&pt->filter);
+       zfree(&pt->time_ranges);
        free(pt);
 }
 
@@ -2420,6 +2778,85 @@ static int intel_pt_perf_config(const char *var, const char *value, void *data)
        return 0;
 }
 
+/* Find least TSC which converts to ns or later */
+static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
+{
+       u64 tsc, tm;
+
+       tsc = perf_time_to_tsc(ns, &pt->tc);
+
+       while (1) {
+               tm = tsc_to_perf_time(tsc, &pt->tc);
+               if (tm < ns)
+                       break;
+               tsc -= 1;
+       }
+
+       while (tm < ns)
+               tm = tsc_to_perf_time(++tsc, &pt->tc);
+
+       return tsc;
+}
+
+/* Find greatest TSC which converts to ns or earlier */
+static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
+{
+       u64 tsc, tm;
+
+       tsc = perf_time_to_tsc(ns, &pt->tc);
+
+       while (1) {
+               tm = tsc_to_perf_time(tsc, &pt->tc);
+               if (tm > ns)
+                       break;
+               tsc += 1;
+       }
+
+       while (tm > ns)
+               tm = tsc_to_perf_time(--tsc, &pt->tc);
+
+       return tsc;
+}
+
+static int intel_pt_setup_time_ranges(struct intel_pt *pt,
+                                     struct itrace_synth_opts *opts)
+{
+       struct perf_time_interval *p = opts->ptime_range;
+       int n = opts->range_num;
+       int i;
+
+       if (!n || !p || pt->timeless_decoding)
+               return 0;
+
+       pt->time_ranges = calloc(n, sizeof(struct range));
+       if (!pt->time_ranges)
+               return -ENOMEM;
+
+       pt->range_cnt = n;
+
+       intel_pt_log("%s: %u range(s)\n", __func__, n);
+
+       for (i = 0; i < n; i++) {
+               struct range *r = &pt->time_ranges[i];
+               u64 ts = p[i].start;
+               u64 te = p[i].end;
+
+               /*
+                * Take care to ensure the TSC range matches the perf-time range
+                * when converted back to perf-time.
+                */
+               r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
+               r->end   = te ? intel_pt_tsc_end(te, pt) : 0;
+
+               intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
+                            i, ts, te);
+               intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
+                            i, r->start, r->end);
+       }
+
+       return 0;
+}
+
 static const char * const intel_pt_info_fmts[] = {
        [INTEL_PT_PMU_TYPE]             = "  PMU Type            %"PRId64"\n",
        [INTEL_PT_TIME_SHIFT]           = "  Time Shift          %"PRIu64"\n",
@@ -2652,6 +3089,12 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
                pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
        }
 
+       if (session->itrace_synth_opts) {
+               err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
+               if (err)
+                       goto err_delete_thread;
+       }
+
        if (pt->synth_opts.calls)
                pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
                                       PERF_IP_FLAG_TRACE_END;
@@ -2692,6 +3135,7 @@ err_free_queues:
 err_free:
        addr_filters__exit(&pt->filts);
        zfree(&pt->filter);
+       zfree(&pt->time_ranges);
        free(pt);
        return err;
 }