1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt_decoder.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
16 #include <linux/compiler.h>
17 #include <linux/string.h>
18 #include <linux/zalloc.h>
20 #include "../auxtrace.h"
22 #include "intel-pt-insn-decoder.h"
23 #include "intel-pt-pkt-decoder.h"
24 #include "intel-pt-decoder.h"
25 #include "intel-pt-log.h"
27 #define BITULL(x) (1ULL << (x))
29 /* IA32_RTIT_CTL MSR bits */
30 #define INTEL_PT_CYC_ENABLE BITULL(1)
31 #define INTEL_PT_CYC_THRESHOLD (BITULL(22) | BITULL(21) | BITULL(20) | BITULL(19))
32 #define INTEL_PT_CYC_THRESHOLD_SHIFT 19
34 #define INTEL_PT_BLK_SIZE 1024
36 #define BIT63 (((uint64_t)1 << 63))
38 #define SEVEN_BYTES 0xffffffffffffffULL
40 #define NO_VMCS 0xffffffffffULL
42 #define INTEL_PT_RETURN 1
45 * Default maximum number of loops with no packets consumed i.e. stuck in a
48 #define INTEL_PT_MAX_LOOPS 100000
51 struct intel_pt_blk *prev;
52 uint64_t ip[INTEL_PT_BLK_SIZE];
55 struct intel_pt_stack {
56 struct intel_pt_blk *blk;
57 struct intel_pt_blk *spare;
61 enum intel_pt_p_once {
62 INTEL_PT_PRT_ONCE_UNK_VMCS,
63 INTEL_PT_PRT_ONCE_ERANGE,
66 enum intel_pt_pkt_state {
67 INTEL_PT_STATE_NO_PSB,
69 INTEL_PT_STATE_ERR_RESYNC,
70 INTEL_PT_STATE_IN_SYNC,
71 INTEL_PT_STATE_TNT_CONT,
74 INTEL_PT_STATE_TIP_PGD,
76 INTEL_PT_STATE_FUP_NO_TIP,
77 INTEL_PT_STATE_FUP_IN_PSB,
78 INTEL_PT_STATE_RESAMPLE,
79 INTEL_PT_STATE_VM_TIME_CORRELATION,
82 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
85 case INTEL_PT_STATE_NO_PSB:
86 case INTEL_PT_STATE_NO_IP:
87 case INTEL_PT_STATE_ERR_RESYNC:
88 case INTEL_PT_STATE_IN_SYNC:
89 case INTEL_PT_STATE_TNT_CONT:
90 case INTEL_PT_STATE_RESAMPLE:
91 case INTEL_PT_STATE_VM_TIME_CORRELATION:
93 case INTEL_PT_STATE_TNT:
94 case INTEL_PT_STATE_TIP:
95 case INTEL_PT_STATE_TIP_PGD:
96 case INTEL_PT_STATE_FUP:
97 case INTEL_PT_STATE_FUP_NO_TIP:
98 case INTEL_PT_STATE_FUP_IN_PSB:
105 #ifdef INTEL_PT_STRICT
106 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
107 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
108 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
109 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
111 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
112 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
113 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
114 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
117 struct intel_pt_decoder {
118 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
119 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
120 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
121 uint64_t max_insn_cnt, void *data);
122 bool (*pgd_ip)(uint64_t ip, void *data);
123 int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
124 struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
126 struct intel_pt_state state;
127 const unsigned char *buf;
129 bool return_compression;
140 bool vm_time_correlation;
141 bool vm_tm_corr_dry_run;
142 bool vm_tm_corr_reliable;
143 bool vm_tm_corr_same_buf;
144 bool vm_tm_corr_continuous;
147 enum intel_pt_param_flags flags;
151 uint64_t pip_payload;
153 uint64_t tsc_timestamp;
154 uint64_t ref_timestamp;
155 uint64_t buf_timestamp;
156 uint64_t sample_timestamp;
158 uint64_t ctc_timestamp;
161 uint64_t cyc_ref_timestamp;
162 uint64_t first_timestamp;
163 uint64_t last_reliable_timestamp;
168 uint32_t tsc_ctc_ratio_n;
169 uint32_t tsc_ctc_ratio_d;
170 uint32_t tsc_ctc_mult;
172 uint32_t ctc_rem_mask;
174 struct intel_pt_stack stack;
175 enum intel_pt_pkt_state pkt_state;
176 enum intel_pt_pkt_ctx pkt_ctx;
177 enum intel_pt_pkt_ctx prev_pkt_ctx;
178 enum intel_pt_blk_type blk_type;
180 struct intel_pt_pkt packet;
181 struct intel_pt_pkt tnt;
184 int last_packet_type;
186 unsigned int cbr_seen;
187 unsigned int max_non_turbo_ratio;
188 double max_non_turbo_ratio_fp;
189 double cbr_cyc_to_tsc;
190 double calc_cyc_to_tsc;
191 bool have_calc_cyc_to_tsc;
193 unsigned int insn_bytes;
195 enum intel_pt_period_type period_type;
196 uint64_t tot_insn_cnt;
197 uint64_t period_insn_cnt;
198 uint64_t period_mask;
199 uint64_t period_ticks;
200 uint64_t last_masked_timestamp;
201 uint64_t tot_cyc_cnt;
202 uint64_t sample_tot_cyc_cnt;
203 uint64_t base_cyc_cnt;
204 uint64_t cyc_cnt_timestamp;
206 uint64_t cyc_threshold;
208 bool continuous_period;
210 bool set_fup_tx_flags;
217 unsigned int fup_tx_flags;
218 unsigned int tx_flags;
219 uint64_t fup_ptw_payload;
220 uint64_t fup_mwait_payload;
221 uint64_t fup_pwre_payload;
222 uint64_t cbr_payload;
223 uint64_t timestamp_insn_cnt;
224 uint64_t sample_insn_cnt;
231 const unsigned char *next_buf;
233 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
236 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
240 for (i = 0; x != 1; i++)
247 static void p_log(const char *fmt, ...)
253 vsnprintf(buf, sizeof(buf), fmt, args);
256 fprintf(stderr, "%s\n", buf);
257 intel_pt_log("%s\n", buf);
260 static bool intel_pt_print_once(struct intel_pt_decoder *decoder,
261 enum intel_pt_p_once id)
263 uint64_t bit = 1ULL << id;
265 if (decoder->print_once & bit)
267 decoder->print_once |= bit;
271 static uint64_t intel_pt_cyc_threshold(uint64_t ctl)
273 if (!(ctl & INTEL_PT_CYC_ENABLE))
276 return (ctl & INTEL_PT_CYC_THRESHOLD) >> INTEL_PT_CYC_THRESHOLD_SHIFT;
279 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
281 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
284 period = intel_pt_lower_power_of_2(decoder->period);
285 decoder->period_mask = ~(period - 1);
286 decoder->period_ticks = period;
290 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
294 return (t / d) * n + ((t % d) * n) / d;
297 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
299 struct intel_pt_decoder *decoder;
301 if (!params->get_trace || !params->walk_insn)
304 decoder = zalloc(sizeof(struct intel_pt_decoder));
308 decoder->get_trace = params->get_trace;
309 decoder->walk_insn = params->walk_insn;
310 decoder->pgd_ip = params->pgd_ip;
311 decoder->lookahead = params->lookahead;
312 decoder->findnew_vmcs_info = params->findnew_vmcs_info;
313 decoder->data = params->data;
314 decoder->return_compression = params->return_compression;
315 decoder->branch_enable = params->branch_enable;
316 decoder->hop = params->quick >= 1;
317 decoder->leap = params->quick >= 2;
318 decoder->vm_time_correlation = params->vm_time_correlation;
319 decoder->vm_tm_corr_dry_run = params->vm_tm_corr_dry_run;
320 decoder->first_timestamp = params->first_timestamp;
321 decoder->last_reliable_timestamp = params->first_timestamp;
322 decoder->max_loops = params->max_loops ? params->max_loops : INTEL_PT_MAX_LOOPS;
324 decoder->flags = params->flags;
326 decoder->ctl = params->ctl;
327 decoder->period = params->period;
328 decoder->period_type = params->period_type;
330 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
331 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
333 decoder->cyc_threshold = intel_pt_cyc_threshold(decoder->ctl);
335 intel_pt_setup_period(decoder);
337 decoder->mtc_shift = params->mtc_period;
338 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
340 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
341 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
343 if (!decoder->tsc_ctc_ratio_n)
344 decoder->tsc_ctc_ratio_d = 0;
346 if (decoder->tsc_ctc_ratio_d) {
347 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
348 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
349 decoder->tsc_ctc_ratio_d;
353 * A TSC packet can slip past MTC packets so that the timestamp appears
354 * to go backwards. One estimate is that can be up to about 40 CPU
355 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
356 * slippage an order of magnitude more to be on the safe side.
358 decoder->tsc_slip = 0x10000;
360 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
361 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
362 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
363 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
364 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
367 intel_pt_log("Hop mode: decoding FUP and TIPs, but not TNT\n");
372 void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
373 uint64_t first_timestamp)
375 decoder->first_timestamp = first_timestamp;
378 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
380 struct intel_pt_blk *blk = stack->blk;
382 stack->blk = blk->prev;
389 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
394 intel_pt_pop_blk(stack);
397 stack->pos = INTEL_PT_BLK_SIZE;
399 return stack->blk->ip[--stack->pos];
402 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
404 struct intel_pt_blk *blk;
410 blk = malloc(sizeof(struct intel_pt_blk));
415 blk->prev = stack->blk;
421 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
425 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
426 err = intel_pt_alloc_blk(stack);
431 stack->blk->ip[stack->pos++] = ip;
435 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
438 intel_pt_pop_blk(stack);
442 static void intel_pt_free_stack(struct intel_pt_stack *stack)
444 intel_pt_clear_stack(stack);
446 zfree(&stack->spare);
449 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
451 intel_pt_free_stack(&decoder->stack);
455 static int intel_pt_ext_err(int code)
459 return INTEL_PT_ERR_NOMEM;
461 return INTEL_PT_ERR_INTERN;
463 return INTEL_PT_ERR_BADPKT;
465 return INTEL_PT_ERR_NODATA;
467 return INTEL_PT_ERR_NOINSN;
469 return INTEL_PT_ERR_MISMAT;
471 return INTEL_PT_ERR_OVR;
473 return INTEL_PT_ERR_LOST;
475 return INTEL_PT_ERR_NELOOP;
477 return INTEL_PT_ERR_UNK;
481 static const char *intel_pt_err_msgs[] = {
482 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
483 [INTEL_PT_ERR_INTERN] = "Internal error",
484 [INTEL_PT_ERR_BADPKT] = "Bad packet",
485 [INTEL_PT_ERR_NODATA] = "No more data",
486 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
487 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
488 [INTEL_PT_ERR_OVR] = "Overflow packet",
489 [INTEL_PT_ERR_LOST] = "Lost trace data",
490 [INTEL_PT_ERR_UNK] = "Unknown error!",
491 [INTEL_PT_ERR_NELOOP] = "Never-ending loop (refer perf config intel-pt.max-loops)",
494 int intel_pt__strerror(int code, char *buf, size_t buflen)
496 if (code < 1 || code >= INTEL_PT_ERR_MAX)
497 code = INTEL_PT_ERR_UNK;
498 strlcpy(buf, intel_pt_err_msgs[code], buflen);
502 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
507 switch (packet->count) {
509 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
513 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
517 ip = packet->payload;
518 /* Sign-extend 6-byte ip */
519 if (ip & (uint64_t)0x800000000000ULL)
520 ip |= (uint64_t)0xffff000000000000ULL;
523 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
527 ip = packet->payload;
536 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
538 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
539 decoder->have_last_ip = true;
542 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
544 intel_pt_set_last_ip(decoder);
545 decoder->ip = decoder->last_ip;
548 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
550 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
554 static int intel_pt_bug(struct intel_pt_decoder *decoder)
556 intel_pt_log("ERROR: Internal error\n");
557 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
561 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
563 decoder->tx_flags = 0;
566 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
568 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
571 static inline void intel_pt_update_pip(struct intel_pt_decoder *decoder)
573 decoder->pip_payload = decoder->packet.payload;
576 static inline void intel_pt_update_nr(struct intel_pt_decoder *decoder)
578 decoder->next_nr = decoder->pip_payload & 1;
581 static inline void intel_pt_set_nr(struct intel_pt_decoder *decoder)
583 decoder->nr = decoder->pip_payload & 1;
584 decoder->next_nr = decoder->nr;
587 static inline void intel_pt_set_pip(struct intel_pt_decoder *decoder)
589 intel_pt_update_pip(decoder);
590 intel_pt_set_nr(decoder);
593 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
595 intel_pt_clear_tx_flags(decoder);
596 decoder->have_tma = false;
597 decoder->pkt_len = 1;
598 decoder->pkt_step = 1;
599 intel_pt_decoder_log_packet(decoder);
600 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
601 intel_pt_log("ERROR: Bad packet\n");
602 decoder->pkt_state = INTEL_PT_STATE_ERR1;
607 static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
609 decoder->sample_timestamp = decoder->timestamp;
610 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
613 static void intel_pt_reposition(struct intel_pt_decoder *decoder)
616 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
617 decoder->timestamp = 0;
618 decoder->have_tma = false;
621 static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
623 struct intel_pt_buffer buffer = { .buf = 0, };
626 decoder->pkt_step = 0;
628 intel_pt_log("Getting more data\n");
629 ret = decoder->get_trace(&buffer, decoder->data);
632 decoder->buf = buffer.buf;
633 decoder->len = buffer.len;
635 intel_pt_log("No more data\n");
638 decoder->buf_timestamp = buffer.ref_timestamp;
639 if (!buffer.consecutive || reposition) {
640 intel_pt_reposition(decoder);
641 decoder->ref_timestamp = buffer.ref_timestamp;
642 decoder->state.trace_nr = buffer.trace_nr;
643 decoder->vm_tm_corr_same_buf = false;
644 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
645 decoder->ref_timestamp);
652 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
655 if (!decoder->next_buf)
656 return intel_pt_get_data(decoder, reposition);
658 decoder->buf = decoder->next_buf;
659 decoder->len = decoder->next_len;
660 decoder->next_buf = 0;
661 decoder->next_len = 0;
665 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
667 unsigned char *buf = decoder->temp_buf;
668 size_t old_len, len, n;
671 old_len = decoder->len;
673 memcpy(buf, decoder->buf, len);
675 ret = intel_pt_get_data(decoder, false);
677 decoder->pos += old_len;
678 return ret < 0 ? ret : -EINVAL;
681 n = INTEL_PT_PKT_MAX_SZ - len;
682 if (n > decoder->len)
684 memcpy(buf + len, decoder->buf, n);
687 decoder->prev_pkt_ctx = decoder->pkt_ctx;
688 ret = intel_pt_get_packet(buf, len, &decoder->packet, &decoder->pkt_ctx);
689 if (ret < (int)old_len) {
690 decoder->next_buf = decoder->buf;
691 decoder->next_len = decoder->len;
693 decoder->len = old_len;
694 return intel_pt_bad_packet(decoder);
697 decoder->next_buf = decoder->buf + (ret - old_len);
698 decoder->next_len = decoder->len - (ret - old_len);
706 struct intel_pt_pkt_info {
707 struct intel_pt_decoder *decoder;
708 struct intel_pt_pkt packet;
711 int last_packet_type;
715 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
717 /* Lookahead packets in current buffer */
718 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
719 intel_pt_pkt_cb_t cb, void *data)
721 struct intel_pt_pkt_info pkt_info;
722 const unsigned char *buf = decoder->buf;
723 enum intel_pt_pkt_ctx pkt_ctx = decoder->pkt_ctx;
724 size_t len = decoder->len;
727 pkt_info.decoder = decoder;
728 pkt_info.pos = decoder->pos;
729 pkt_info.pkt_len = decoder->pkt_step;
730 pkt_info.last_packet_type = decoder->last_packet_type;
731 pkt_info.data = data;
735 pkt_info.pos += pkt_info.pkt_len;
736 buf += pkt_info.pkt_len;
737 len -= pkt_info.pkt_len;
740 return INTEL_PT_NEED_MORE_BYTES;
742 ret = intel_pt_get_packet(buf, len, &pkt_info.packet,
745 return INTEL_PT_NEED_MORE_BYTES;
749 pkt_info.pkt_len = ret;
750 } while (pkt_info.packet.type == INTEL_PT_PAD);
756 pkt_info.last_packet_type = pkt_info.packet.type;
760 struct intel_pt_calc_cyc_to_tsc_info {
764 uint64_t ctc_timestamp;
766 uint64_t tsc_timestamp;
771 double cbr_cyc_to_tsc;
775 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
776 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
777 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
778 * packet by copying the missing bits from the current MTC assuming the least
779 * difference between the two, and that the current MTC comes after last_mtc.
781 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
784 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
785 uint32_t mask = ~(first_missing_bit - 1);
787 *last_mtc |= mtc & mask;
788 if (*last_mtc >= mtc) {
789 *last_mtc -= first_missing_bit;
794 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
796 struct intel_pt_decoder *decoder = pkt_info->decoder;
797 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
801 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
803 switch (pkt_info->packet.type) {
805 case INTEL_PT_TIP_PGE:
810 case INTEL_PT_MODE_EXEC:
811 case INTEL_PT_MODE_TSX:
812 case INTEL_PT_PSBEND:
816 case INTEL_PT_PTWRITE:
817 case INTEL_PT_PTWRITE_IP:
821 case INTEL_PT_BEP_IP:
828 mtc = pkt_info->packet.payload;
829 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
830 data->fixup_last_mtc = false;
831 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
834 if (mtc > data->last_mtc)
835 mtc_delta = mtc - data->last_mtc;
837 mtc_delta = mtc + 256 - data->last_mtc;
838 data->ctc_delta += mtc_delta << decoder->mtc_shift;
839 data->last_mtc = mtc;
841 if (decoder->tsc_ctc_mult) {
842 timestamp = data->ctc_timestamp +
843 data->ctc_delta * decoder->tsc_ctc_mult;
845 timestamp = data->ctc_timestamp +
846 multdiv(data->ctc_delta,
847 decoder->tsc_ctc_ratio_n,
848 decoder->tsc_ctc_ratio_d);
851 if (timestamp < data->timestamp)
854 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
855 data->timestamp = timestamp;
863 * For now, do not support using TSC packets - refer
864 * intel_pt_calc_cyc_to_tsc().
868 timestamp = pkt_info->packet.payload |
869 (data->timestamp & (0xffULL << 56));
870 if (data->from_mtc && timestamp < data->timestamp &&
871 data->timestamp - timestamp < decoder->tsc_slip)
873 if (timestamp < data->timestamp)
874 timestamp += (1ULL << 56);
875 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
878 data->tsc_timestamp = timestamp;
879 data->timestamp = timestamp;
888 if (!decoder->tsc_ctc_ratio_d)
891 ctc = pkt_info->packet.payload;
892 fc = pkt_info->packet.count;
893 ctc_rem = ctc & decoder->ctc_rem_mask;
895 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
897 data->ctc_timestamp = data->tsc_timestamp - fc;
898 if (decoder->tsc_ctc_mult) {
899 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
901 data->ctc_timestamp -=
902 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
903 decoder->tsc_ctc_ratio_d);
907 data->have_tma = true;
908 data->fixup_last_mtc = true;
913 data->cycle_cnt += pkt_info->packet.payload;
917 cbr = pkt_info->packet.payload;
918 if (data->cbr && data->cbr != cbr)
921 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
924 case INTEL_PT_TIP_PGD:
925 case INTEL_PT_TRACESTOP:
926 case INTEL_PT_EXSTOP:
927 case INTEL_PT_EXSTOP_IP:
932 case INTEL_PT_BAD: /* Does not happen */
937 if (!data->cbr && decoder->cbr) {
938 data->cbr = decoder->cbr;
939 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
942 if (!data->cycle_cnt)
945 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
947 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
948 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
949 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
950 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
954 decoder->calc_cyc_to_tsc = cyc_to_tsc;
955 decoder->have_calc_cyc_to_tsc = true;
958 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
959 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
961 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
962 cyc_to_tsc, pkt_info->pos);
968 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
971 struct intel_pt_calc_cyc_to_tsc_info data = {
974 .last_mtc = decoder->last_mtc,
975 .ctc_timestamp = decoder->ctc_timestamp,
976 .ctc_delta = decoder->ctc_delta,
977 .tsc_timestamp = decoder->tsc_timestamp,
978 .timestamp = decoder->timestamp,
979 .have_tma = decoder->have_tma,
980 .fixup_last_mtc = decoder->fixup_last_mtc,
981 .from_mtc = from_mtc,
986 * For now, do not support using TSC packets for at least the reasons:
987 * 1) timing might have stopped
988 * 2) TSC packets within PSB+ can slip against CYC packets
993 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
996 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
1000 decoder->last_packet_type = decoder->packet.type;
1003 decoder->pos += decoder->pkt_step;
1004 decoder->buf += decoder->pkt_step;
1005 decoder->len -= decoder->pkt_step;
1007 if (!decoder->len) {
1008 ret = intel_pt_get_next_data(decoder, false);
1013 decoder->prev_pkt_ctx = decoder->pkt_ctx;
1014 ret = intel_pt_get_packet(decoder->buf, decoder->len,
1015 &decoder->packet, &decoder->pkt_ctx);
1016 if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
1017 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
1018 ret = intel_pt_get_split_packet(decoder);
1023 return intel_pt_bad_packet(decoder);
1025 decoder->pkt_len = ret;
1026 decoder->pkt_step = ret;
1027 intel_pt_decoder_log_packet(decoder);
1028 } while (decoder->packet.type == INTEL_PT_PAD);
1033 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
1035 uint64_t timestamp, masked_timestamp;
1037 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
1038 masked_timestamp = timestamp & decoder->period_mask;
1039 if (decoder->continuous_period) {
1040 if (masked_timestamp > decoder->last_masked_timestamp)
1044 masked_timestamp = timestamp & decoder->period_mask;
1045 if (masked_timestamp > decoder->last_masked_timestamp) {
1046 decoder->last_masked_timestamp = masked_timestamp;
1047 decoder->continuous_period = true;
1051 if (masked_timestamp < decoder->last_masked_timestamp)
1052 return decoder->period_ticks;
1054 return decoder->period_ticks - (timestamp - masked_timestamp);
1057 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
1059 switch (decoder->period_type) {
1060 case INTEL_PT_PERIOD_INSTRUCTIONS:
1061 return decoder->period - decoder->period_insn_cnt;
1062 case INTEL_PT_PERIOD_TICKS:
1063 return intel_pt_next_period(decoder);
1064 case INTEL_PT_PERIOD_NONE:
1065 case INTEL_PT_PERIOD_MTC:
1071 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
1073 uint64_t timestamp, masked_timestamp;
1075 switch (decoder->period_type) {
1076 case INTEL_PT_PERIOD_INSTRUCTIONS:
1077 decoder->period_insn_cnt = 0;
1079 case INTEL_PT_PERIOD_TICKS:
1080 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
1081 masked_timestamp = timestamp & decoder->period_mask;
1082 if (masked_timestamp > decoder->last_masked_timestamp)
1083 decoder->last_masked_timestamp = masked_timestamp;
1085 decoder->last_masked_timestamp += decoder->period_ticks;
1087 case INTEL_PT_PERIOD_NONE:
1088 case INTEL_PT_PERIOD_MTC:
1093 decoder->state.type |= INTEL_PT_INSTRUCTION;
1096 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
1097 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
1099 uint64_t max_insn_cnt, insn_cnt = 0;
1102 if (!decoder->mtc_insn)
1103 decoder->mtc_insn = true;
1105 max_insn_cnt = intel_pt_next_sample(decoder);
1107 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
1108 max_insn_cnt, decoder->data);
1110 decoder->tot_insn_cnt += insn_cnt;
1111 decoder->timestamp_insn_cnt += insn_cnt;
1112 decoder->sample_insn_cnt += insn_cnt;
1113 decoder->period_insn_cnt += insn_cnt;
1116 decoder->no_progress = 0;
1117 decoder->pkt_state = INTEL_PT_STATE_ERR2;
1118 intel_pt_log_at("ERROR: Failed to get instruction",
1125 if (ip && decoder->ip == ip) {
1130 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
1131 intel_pt_sample_insn(decoder);
1133 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
1134 decoder->state.type = INTEL_PT_INSTRUCTION;
1135 decoder->state.from_ip = decoder->ip;
1136 decoder->state.to_ip = 0;
1137 decoder->ip += intel_pt_insn->length;
1138 err = INTEL_PT_RETURN;
1142 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
1143 /* Zero-length calls are excluded */
1144 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
1145 intel_pt_insn->rel) {
1146 err = intel_pt_push(&decoder->stack, decoder->ip +
1147 intel_pt_insn->length);
1151 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
1152 decoder->ret_addr = intel_pt_pop(&decoder->stack);
1155 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1156 int cnt = decoder->no_progress++;
1158 decoder->state.from_ip = decoder->ip;
1159 decoder->ip += intel_pt_insn->length +
1161 decoder->state.to_ip = decoder->ip;
1162 err = INTEL_PT_RETURN;
1165 * Check for being stuck in a loop. This can happen if a
1166 * decoder error results in the decoder erroneously setting the
1167 * ip to an address that is itself in an infinite loop that
1168 * consumes no packets. When that happens, there must be an
1169 * unconditional branch.
1173 decoder->stuck_ip = decoder->state.to_ip;
1174 decoder->stuck_ip_prd = 1;
1175 decoder->stuck_ip_cnt = 1;
1176 } else if (cnt > decoder->max_loops ||
1177 decoder->state.to_ip == decoder->stuck_ip) {
1178 intel_pt_log_at("ERROR: Never-ending loop",
1179 decoder->state.to_ip);
1180 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1183 } else if (!--decoder->stuck_ip_cnt) {
1184 decoder->stuck_ip_prd += 1;
1185 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1186 decoder->stuck_ip = decoder->state.to_ip;
1189 goto out_no_progress;
1192 decoder->no_progress = 0;
1194 decoder->state.insn_op = intel_pt_insn->op;
1195 decoder->state.insn_len = intel_pt_insn->length;
1196 memcpy(decoder->state.insn, intel_pt_insn->buf,
1197 INTEL_PT_INSN_BUF_SZ);
1199 if (decoder->tx_flags & INTEL_PT_IN_TX)
1200 decoder->state.flags |= INTEL_PT_IN_TX;
1205 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1209 if (decoder->set_fup_tx_flags) {
1210 decoder->set_fup_tx_flags = false;
1211 decoder->tx_flags = decoder->fup_tx_flags;
1212 decoder->state.type = INTEL_PT_TRANSACTION;
1213 if (decoder->fup_tx_flags & INTEL_PT_ABORT_TX)
1214 decoder->state.type |= INTEL_PT_BRANCH;
1215 decoder->state.from_ip = decoder->ip;
1216 decoder->state.to_ip = 0;
1217 decoder->state.flags = decoder->fup_tx_flags;
1220 if (decoder->set_fup_ptw) {
1221 decoder->set_fup_ptw = false;
1222 decoder->state.type = INTEL_PT_PTW;
1223 decoder->state.flags |= INTEL_PT_FUP_IP;
1224 decoder->state.from_ip = decoder->ip;
1225 decoder->state.to_ip = 0;
1226 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1229 if (decoder->set_fup_mwait) {
1230 decoder->set_fup_mwait = false;
1231 decoder->state.type = INTEL_PT_MWAIT_OP;
1232 decoder->state.from_ip = decoder->ip;
1233 decoder->state.to_ip = 0;
1234 decoder->state.mwait_payload = decoder->fup_mwait_payload;
1237 if (decoder->set_fup_pwre) {
1238 decoder->set_fup_pwre = false;
1239 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1240 decoder->state.type &= ~INTEL_PT_BRANCH;
1241 decoder->state.from_ip = decoder->ip;
1242 decoder->state.to_ip = 0;
1243 decoder->state.pwre_payload = decoder->fup_pwre_payload;
1246 if (decoder->set_fup_exstop) {
1247 decoder->set_fup_exstop = false;
1248 decoder->state.type |= INTEL_PT_EX_STOP;
1249 decoder->state.type &= ~INTEL_PT_BRANCH;
1250 decoder->state.flags |= INTEL_PT_FUP_IP;
1251 decoder->state.from_ip = decoder->ip;
1252 decoder->state.to_ip = 0;
1255 if (decoder->set_fup_bep) {
1256 decoder->set_fup_bep = false;
1257 decoder->state.type |= INTEL_PT_BLK_ITEMS;
1258 decoder->state.type &= ~INTEL_PT_BRANCH;
1259 decoder->state.from_ip = decoder->ip;
1260 decoder->state.to_ip = 0;
1266 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1267 struct intel_pt_insn *intel_pt_insn,
1268 uint64_t ip, int err)
1270 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1271 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1272 ip == decoder->ip + intel_pt_insn->length;
1275 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1277 struct intel_pt_insn intel_pt_insn;
1281 ip = decoder->last_ip;
1284 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1285 if (err == INTEL_PT_RETURN)
1287 if (err == -EAGAIN ||
1288 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1289 bool no_tip = decoder->pkt_state != INTEL_PT_STATE_FUP;
1291 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1292 if (intel_pt_fup_event(decoder) && no_tip)
1296 decoder->set_fup_tx_flags = false;
1300 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1301 intel_pt_log_at("ERROR: Unexpected indirect branch",
1303 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1307 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1308 intel_pt_log_at("ERROR: Unexpected conditional branch",
1310 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1314 intel_pt_bug(decoder);
1318 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1320 struct intel_pt_insn intel_pt_insn;
1323 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1324 if (err == INTEL_PT_RETURN &&
1326 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1327 (decoder->state.type & INTEL_PT_BRANCH) &&
1328 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1329 /* Unconditional branch leaving filter region */
1330 decoder->no_progress = 0;
1331 decoder->pge = false;
1332 decoder->continuous_period = false;
1333 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1334 decoder->state.type |= INTEL_PT_TRACE_END;
1335 intel_pt_update_nr(decoder);
1338 if (err == INTEL_PT_RETURN)
1343 intel_pt_update_nr(decoder);
1345 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1346 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1347 decoder->pge = false;
1348 decoder->continuous_period = false;
1349 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1350 decoder->state.from_ip = decoder->ip;
1351 if (decoder->packet.count == 0) {
1352 decoder->state.to_ip = 0;
1354 decoder->state.to_ip = decoder->last_ip;
1355 decoder->ip = decoder->last_ip;
1357 decoder->state.type |= INTEL_PT_TRACE_END;
1359 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1360 decoder->state.from_ip = decoder->ip;
1361 if (decoder->packet.count == 0) {
1362 decoder->state.to_ip = 0;
1364 decoder->state.to_ip = decoder->last_ip;
1365 decoder->ip = decoder->last_ip;
1371 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1372 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1375 if (decoder->pgd_ip &&
1376 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1377 decoder->pgd_ip(to_ip, decoder->data)) {
1378 /* Conditional branch leaving filter region */
1379 decoder->pge = false;
1380 decoder->continuous_period = false;
1381 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1382 decoder->ip = to_ip;
1383 decoder->state.from_ip = decoder->ip;
1384 decoder->state.to_ip = to_ip;
1385 decoder->state.type |= INTEL_PT_TRACE_END;
1388 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1390 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1394 return intel_pt_bug(decoder);
1397 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1399 struct intel_pt_insn intel_pt_insn;
1403 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1404 if (err == INTEL_PT_RETURN)
1409 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1410 if (!decoder->return_compression) {
1411 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1413 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1416 if (!decoder->ret_addr) {
1417 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1419 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1422 if (!(decoder->tnt.payload & BIT63)) {
1423 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1425 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1428 decoder->tnt.count -= 1;
1429 if (decoder->tnt.count)
1430 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1432 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1433 decoder->tnt.payload <<= 1;
1434 decoder->state.from_ip = decoder->ip;
1435 decoder->ip = decoder->ret_addr;
1436 decoder->state.to_ip = decoder->ip;
1440 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1441 /* Handle deferred TIPs */
1442 err = intel_pt_get_next_packet(decoder);
1445 if (decoder->packet.type != INTEL_PT_TIP ||
1446 decoder->packet.count == 0) {
1447 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1449 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1450 decoder->pkt_step = 0;
1453 intel_pt_set_last_ip(decoder);
1454 decoder->state.from_ip = decoder->ip;
1455 decoder->state.to_ip = decoder->last_ip;
1456 decoder->ip = decoder->last_ip;
1457 intel_pt_update_nr(decoder);
1461 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1462 decoder->tnt.count -= 1;
1463 if (decoder->tnt.count)
1464 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1466 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1467 if (decoder->tnt.payload & BIT63) {
1468 decoder->tnt.payload <<= 1;
1469 decoder->state.from_ip = decoder->ip;
1470 decoder->ip += intel_pt_insn.length +
1472 decoder->state.to_ip = decoder->ip;
1475 /* Instruction sample for a non-taken branch */
1476 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1477 decoder->tnt.payload <<= 1;
1478 decoder->state.type = INTEL_PT_INSTRUCTION;
1479 decoder->state.from_ip = decoder->ip;
1480 decoder->state.to_ip = 0;
1481 decoder->ip += intel_pt_insn.length;
1484 decoder->sample_cyc = false;
1485 decoder->ip += intel_pt_insn.length;
1486 if (!decoder->tnt.count) {
1487 intel_pt_update_sample_time(decoder);
1490 decoder->tnt.payload <<= 1;
1494 return intel_pt_bug(decoder);
1498 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1500 unsigned int fup_tx_flags;
1503 fup_tx_flags = decoder->packet.payload &
1504 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1505 err = intel_pt_get_next_packet(decoder);
1508 if (decoder->packet.type == INTEL_PT_FUP) {
1509 decoder->fup_tx_flags = fup_tx_flags;
1510 decoder->set_fup_tx_flags = true;
1511 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1514 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1516 intel_pt_update_in_tx(decoder);
1521 static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
1523 timestamp |= (ref_timestamp & (0xffULL << 56));
1525 if (timestamp < ref_timestamp) {
1526 if (ref_timestamp - timestamp > (1ULL << 55))
1527 timestamp += (1ULL << 56);
1529 if (timestamp - ref_timestamp > (1ULL << 55))
1530 timestamp -= (1ULL << 56);
1536 /* For use only when decoder->vm_time_correlation is true */
1537 static bool intel_pt_time_in_range(struct intel_pt_decoder *decoder,
1540 uint64_t max_timestamp = decoder->buf_timestamp;
1542 if (!max_timestamp) {
1543 max_timestamp = decoder->last_reliable_timestamp +
1546 return timestamp >= decoder->last_reliable_timestamp &&
1547 timestamp < decoder->buf_timestamp;
1550 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1555 decoder->have_tma = false;
1557 if (decoder->ref_timestamp) {
1558 timestamp = intel_pt_8b_tsc(decoder->packet.payload,
1559 decoder->ref_timestamp);
1560 decoder->tsc_timestamp = timestamp;
1561 decoder->timestamp = timestamp;
1562 decoder->ref_timestamp = 0;
1563 decoder->timestamp_insn_cnt = 0;
1564 } else if (decoder->timestamp) {
1565 timestamp = decoder->packet.payload |
1566 (decoder->timestamp & (0xffULL << 56));
1567 decoder->tsc_timestamp = timestamp;
1568 if (timestamp < decoder->timestamp &&
1569 decoder->timestamp - timestamp < decoder->tsc_slip) {
1570 intel_pt_log_to("Suppressing backwards timestamp",
1572 timestamp = decoder->timestamp;
1574 if (timestamp < decoder->timestamp) {
1575 if (!decoder->buf_timestamp ||
1576 (timestamp + (1ULL << 56) < decoder->buf_timestamp)) {
1577 intel_pt_log_to("Wraparound timestamp", timestamp);
1578 timestamp += (1ULL << 56);
1579 decoder->tsc_timestamp = timestamp;
1581 intel_pt_log_to("Suppressing bad timestamp", timestamp);
1582 timestamp = decoder->timestamp;
1586 if (decoder->vm_time_correlation &&
1587 (bad || !intel_pt_time_in_range(decoder, timestamp)) &&
1588 intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
1589 p_log("Timestamp out of range");
1590 decoder->timestamp = timestamp;
1591 decoder->timestamp_insn_cnt = 0;
1594 if (decoder->last_packet_type == INTEL_PT_CYC) {
1595 decoder->cyc_ref_timestamp = decoder->timestamp;
1596 decoder->cycle_cnt = 0;
1597 decoder->have_calc_cyc_to_tsc = false;
1598 intel_pt_calc_cyc_to_tsc(decoder, false);
1601 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1604 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1606 intel_pt_log("ERROR: Buffer overflow\n");
1607 intel_pt_clear_tx_flags(decoder);
1608 intel_pt_set_nr(decoder);
1609 decoder->timestamp_insn_cnt = 0;
1610 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1611 decoder->overflow = true;
1615 static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
1617 if (decoder->have_cyc)
1620 decoder->cyc_cnt_timestamp = decoder->timestamp;
1621 decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
1624 static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
1626 decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
1629 intel_pt_mtc_cyc_cnt_pge(decoder);
1632 static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
1634 uint64_t tot_cyc_cnt, tsc_delta;
1636 if (decoder->have_cyc)
1639 decoder->sample_cyc = true;
1641 if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
1644 tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
1645 tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
1647 if (tot_cyc_cnt > decoder->tot_cyc_cnt)
1648 decoder->tot_cyc_cnt = tot_cyc_cnt;
1651 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1653 uint32_t ctc = decoder->packet.payload;
1654 uint32_t fc = decoder->packet.count;
1655 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1657 if (!decoder->tsc_ctc_ratio_d)
1660 if (decoder->pge && !decoder->in_psb)
1661 intel_pt_mtc_cyc_cnt_pge(decoder);
1663 intel_pt_mtc_cyc_cnt_upd(decoder);
1665 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1666 decoder->last_ctc = ctc - ctc_rem;
1667 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1668 if (decoder->tsc_ctc_mult) {
1669 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1671 decoder->ctc_timestamp -= multdiv(ctc_rem,
1672 decoder->tsc_ctc_ratio_n,
1673 decoder->tsc_ctc_ratio_d);
1675 decoder->ctc_delta = 0;
1676 decoder->have_tma = true;
1677 decoder->fixup_last_mtc = true;
1678 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1679 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1682 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1685 uint32_t mtc, mtc_delta;
1687 if (!decoder->have_tma)
1690 mtc = decoder->packet.payload;
1692 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1693 decoder->fixup_last_mtc = false;
1694 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1695 &decoder->last_mtc);
1698 if (mtc > decoder->last_mtc)
1699 mtc_delta = mtc - decoder->last_mtc;
1701 mtc_delta = mtc + 256 - decoder->last_mtc;
1703 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1705 if (decoder->tsc_ctc_mult) {
1706 timestamp = decoder->ctc_timestamp +
1707 decoder->ctc_delta * decoder->tsc_ctc_mult;
1709 timestamp = decoder->ctc_timestamp +
1710 multdiv(decoder->ctc_delta,
1711 decoder->tsc_ctc_ratio_n,
1712 decoder->tsc_ctc_ratio_d);
1715 if (timestamp < decoder->timestamp)
1716 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1717 timestamp, decoder->timestamp);
1719 decoder->timestamp = timestamp;
1721 intel_pt_mtc_cyc_cnt_upd(decoder);
1723 decoder->timestamp_insn_cnt = 0;
1724 decoder->last_mtc = mtc;
1726 if (decoder->last_packet_type == INTEL_PT_CYC) {
1727 decoder->cyc_ref_timestamp = decoder->timestamp;
1728 decoder->cycle_cnt = 0;
1729 decoder->have_calc_cyc_to_tsc = false;
1730 intel_pt_calc_cyc_to_tsc(decoder, true);
1733 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1736 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1738 unsigned int cbr = decoder->packet.payload & 0xff;
1740 decoder->cbr_payload = decoder->packet.payload;
1742 if (decoder->cbr == cbr)
1746 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1748 intel_pt_mtc_cyc_cnt_cbr(decoder);
1751 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1753 uint64_t timestamp = decoder->cyc_ref_timestamp;
1755 decoder->have_cyc = true;
1757 decoder->cycle_cnt += decoder->packet.payload;
1759 decoder->tot_cyc_cnt += decoder->packet.payload;
1760 decoder->sample_cyc = true;
1762 if (!decoder->cyc_ref_timestamp)
1765 if (decoder->have_calc_cyc_to_tsc)
1766 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1767 else if (decoder->cbr)
1768 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1772 if (timestamp < decoder->timestamp)
1773 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1774 timestamp, decoder->timestamp);
1776 decoder->timestamp = timestamp;
1778 decoder->timestamp_insn_cnt = 0;
1780 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1783 static void intel_pt_bbp(struct intel_pt_decoder *decoder)
1785 if (decoder->prev_pkt_ctx == INTEL_PT_NO_CTX) {
1786 memset(decoder->state.items.mask, 0, sizeof(decoder->state.items.mask));
1787 decoder->state.items.is_32_bit = false;
1789 decoder->blk_type = decoder->packet.payload;
1790 decoder->blk_type_pos = intel_pt_blk_type_pos(decoder->blk_type);
1791 if (decoder->blk_type == INTEL_PT_GP_REGS)
1792 decoder->state.items.is_32_bit = decoder->packet.count;
1793 if (decoder->blk_type_pos < 0) {
1794 intel_pt_log("WARNING: Unknown block type %u\n",
1796 } else if (decoder->state.items.mask[decoder->blk_type_pos]) {
1797 intel_pt_log("WARNING: Duplicate block type %u\n",
1802 static void intel_pt_bip(struct intel_pt_decoder *decoder)
1804 uint32_t id = decoder->packet.count;
1805 uint32_t bit = 1 << id;
1806 int pos = decoder->blk_type_pos;
1808 if (pos < 0 || id >= INTEL_PT_BLK_ITEM_ID_CNT) {
1809 intel_pt_log("WARNING: Unknown block item %u type %d\n",
1810 id, decoder->blk_type);
1814 if (decoder->state.items.mask[pos] & bit) {
1815 intel_pt_log("WARNING: Duplicate block item %u type %d\n",
1816 id, decoder->blk_type);
1819 decoder->state.items.mask[pos] |= bit;
1820 decoder->state.items.val[pos][id] = decoder->packet.payload;
1823 /* Walk PSB+ packets when already in sync. */
1824 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1828 decoder->in_psb = true;
1831 err = intel_pt_get_next_packet(decoder);
1835 switch (decoder->packet.type) {
1836 case INTEL_PT_PSBEND:
1840 case INTEL_PT_TIP_PGD:
1841 case INTEL_PT_TIP_PGE:
1844 case INTEL_PT_TRACESTOP:
1847 case INTEL_PT_PTWRITE:
1848 case INTEL_PT_PTWRITE_IP:
1849 case INTEL_PT_EXSTOP:
1850 case INTEL_PT_EXSTOP_IP:
1851 case INTEL_PT_MWAIT:
1857 case INTEL_PT_BEP_IP:
1858 decoder->have_tma = false;
1859 intel_pt_log("ERROR: Unexpected packet\n");
1864 err = intel_pt_overflow(decoder);
1868 intel_pt_calc_tsc_timestamp(decoder);
1872 intel_pt_calc_tma(decoder);
1876 intel_pt_calc_cbr(decoder);
1879 case INTEL_PT_MODE_EXEC:
1880 decoder->exec_mode = decoder->packet.payload;
1884 intel_pt_set_pip(decoder);
1888 decoder->pge = true;
1889 if (decoder->packet.count) {
1890 intel_pt_set_last_ip(decoder);
1891 decoder->psb_ip = decoder->last_ip;
1895 case INTEL_PT_MODE_TSX:
1896 intel_pt_update_in_tx(decoder);
1900 intel_pt_calc_mtc_timestamp(decoder);
1901 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1902 decoder->state.type |= INTEL_PT_INSTRUCTION;
1906 intel_pt_calc_cyc_timestamp(decoder);
1917 decoder->in_psb = false;
1922 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1926 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1927 decoder->tx_flags = 0;
1928 decoder->state.flags &= ~INTEL_PT_IN_TX;
1929 decoder->state.flags |= INTEL_PT_ABORT_TX;
1931 decoder->state.flags |= INTEL_PT_ASYNC;
1935 err = intel_pt_get_next_packet(decoder);
1939 switch (decoder->packet.type) {
1942 case INTEL_PT_TRACESTOP:
1946 case INTEL_PT_MODE_TSX:
1948 case INTEL_PT_PSBEND:
1949 case INTEL_PT_PTWRITE:
1950 case INTEL_PT_PTWRITE_IP:
1951 case INTEL_PT_EXSTOP:
1952 case INTEL_PT_EXSTOP_IP:
1953 case INTEL_PT_MWAIT:
1959 case INTEL_PT_BEP_IP:
1960 intel_pt_log("ERROR: Missing TIP after FUP\n");
1961 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1962 decoder->pkt_step = 0;
1966 intel_pt_calc_cbr(decoder);
1970 return intel_pt_overflow(decoder);
1972 case INTEL_PT_TIP_PGD:
1973 decoder->state.from_ip = decoder->ip;
1974 if (decoder->packet.count == 0) {
1975 decoder->state.to_ip = 0;
1977 intel_pt_set_ip(decoder);
1978 decoder->state.to_ip = decoder->ip;
1980 decoder->pge = false;
1981 decoder->continuous_period = false;
1982 decoder->state.type |= INTEL_PT_TRACE_END;
1983 intel_pt_update_nr(decoder);
1986 case INTEL_PT_TIP_PGE:
1987 decoder->pge = true;
1988 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1990 decoder->state.from_ip = 0;
1991 if (decoder->packet.count == 0) {
1992 decoder->state.to_ip = 0;
1994 intel_pt_set_ip(decoder);
1995 decoder->state.to_ip = decoder->ip;
1997 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1998 intel_pt_mtc_cyc_cnt_pge(decoder);
1999 intel_pt_set_nr(decoder);
2003 decoder->state.from_ip = decoder->ip;
2004 if (decoder->packet.count == 0) {
2005 decoder->state.to_ip = 0;
2007 intel_pt_set_ip(decoder);
2008 decoder->state.to_ip = decoder->ip;
2010 intel_pt_update_nr(decoder);
2014 intel_pt_update_pip(decoder);
2018 intel_pt_calc_mtc_timestamp(decoder);
2019 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
2020 decoder->state.type |= INTEL_PT_INSTRUCTION;
2024 intel_pt_calc_cyc_timestamp(decoder);
2027 case INTEL_PT_MODE_EXEC:
2028 decoder->exec_mode = decoder->packet.payload;
2037 return intel_pt_bug(decoder);
2042 static int intel_pt_resample(struct intel_pt_decoder *decoder)
2044 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2045 decoder->state.type = INTEL_PT_INSTRUCTION;
2046 decoder->state.from_ip = decoder->ip;
2047 decoder->state.to_ip = 0;
2051 struct intel_pt_vm_tsc_info {
2052 struct intel_pt_pkt pip_packet;
2053 struct intel_pt_pkt vmcs_packet;
2054 struct intel_pt_pkt tma_packet;
2055 bool tsc, pip, vmcs, tma, psbend;
2061 /* Lookahead and get the PIP, VMCS and TMA packets from PSB+ */
2062 static int intel_pt_vm_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2064 struct intel_pt_vm_tsc_info *data = pkt_info->data;
2066 switch (pkt_info->packet.type) {
2069 case INTEL_PT_MODE_EXEC:
2070 case INTEL_PT_MODE_TSX:
2082 data->tma_packet = pkt_info->packet;
2087 data->pip_packet = pkt_info->packet;
2092 data->vmcs_packet = pkt_info->packet;
2096 case INTEL_PT_PSBEND:
2097 data->psbend = true;
2100 case INTEL_PT_TIP_PGE:
2101 case INTEL_PT_PTWRITE:
2102 case INTEL_PT_PTWRITE_IP:
2103 case INTEL_PT_EXSTOP:
2104 case INTEL_PT_EXSTOP_IP:
2105 case INTEL_PT_MWAIT:
2111 case INTEL_PT_BEP_IP:
2115 case INTEL_PT_TIP_PGD:
2118 case INTEL_PT_TRACESTOP:
2126 struct intel_pt_ovf_fup_info {
2131 /* Lookahead to detect a FUP packet after OVF */
2132 static int intel_pt_ovf_fup_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2134 struct intel_pt_ovf_fup_info *data = pkt_info->data;
2136 if (pkt_info->packet.type == INTEL_PT_CYC ||
2137 pkt_info->packet.type == INTEL_PT_MTC ||
2138 pkt_info->packet.type == INTEL_PT_TSC)
2139 return !--(data->max_lookahead);
2140 data->found = pkt_info->packet.type == INTEL_PT_FUP;
2144 static bool intel_pt_ovf_fup_lookahead(struct intel_pt_decoder *decoder)
2146 struct intel_pt_ovf_fup_info data = {
2147 .max_lookahead = 16,
2151 intel_pt_pkt_lookahead(decoder, intel_pt_ovf_fup_lookahead_cb, &data);
2155 /* Lookahead and get the TMA packet after TSC */
2156 static int intel_pt_tma_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2158 struct intel_pt_vm_tsc_info *data = pkt_info->data;
2160 if (pkt_info->packet.type == INTEL_PT_CYC ||
2161 pkt_info->packet.type == INTEL_PT_MTC)
2162 return !--(data->max_lookahead);
2164 if (pkt_info->packet.type == INTEL_PT_TMA) {
2165 data->tma_packet = pkt_info->packet;
2171 static uint64_t intel_pt_ctc_to_tsc(struct intel_pt_decoder *decoder, uint64_t ctc)
2173 if (decoder->tsc_ctc_mult)
2174 return ctc * decoder->tsc_ctc_mult;
2176 return multdiv(ctc, decoder->tsc_ctc_ratio_n, decoder->tsc_ctc_ratio_d);
2179 static uint64_t intel_pt_calc_expected_tsc(struct intel_pt_decoder *decoder,
2182 uint64_t last_ctc_timestamp,
2186 /* Number of CTC ticks from last_ctc_timestamp to last_mtc */
2187 uint64_t last_mtc_ctc = last_ctc + ctc_delta;
2189 * Number of CTC ticks from there until current TMA packet. We would
2190 * expect last_mtc_ctc to be before ctc, but the TSC packet can slip
2191 * past an MTC, so a sign-extended value is used.
2193 uint64_t delta = (int16_t)((uint16_t)ctc - (uint16_t)last_mtc_ctc);
2194 /* Total CTC ticks from last_ctc_timestamp to current TMA packet */
2195 uint64_t new_ctc_delta = ctc_delta + delta;
2196 uint64_t expected_tsc;
2199 * Convert CTC ticks to TSC ticks, add the starting point
2200 * (last_ctc_timestamp) and the fast counter from the TMA packet.
2202 expected_tsc = last_ctc_timestamp + intel_pt_ctc_to_tsc(decoder, new_ctc_delta) + fc;
2204 if (intel_pt_enable_logging) {
2205 intel_pt_log_x64(last_mtc_ctc);
2206 intel_pt_log_x32(last_ctc);
2207 intel_pt_log_x64(ctc_delta);
2208 intel_pt_log_x64(delta);
2209 intel_pt_log_x32(ctc);
2210 intel_pt_log_x64(new_ctc_delta);
2211 intel_pt_log_x64(last_ctc_timestamp);
2212 intel_pt_log_x32(fc);
2213 intel_pt_log_x64(intel_pt_ctc_to_tsc(decoder, new_ctc_delta));
2214 intel_pt_log_x64(expected_tsc);
2217 return expected_tsc;
2220 static uint64_t intel_pt_expected_tsc(struct intel_pt_decoder *decoder,
2221 struct intel_pt_vm_tsc_info *data)
2223 uint32_t ctc = data->tma_packet.payload;
2224 uint32_t fc = data->tma_packet.count;
2226 return intel_pt_calc_expected_tsc(decoder, ctc, fc,
2227 decoder->ctc_timestamp,
2228 data->ctc_delta, data->last_ctc);
2231 static void intel_pt_translate_vm_tsc(struct intel_pt_decoder *decoder,
2232 struct intel_pt_vmcs_info *vmcs_info)
2234 uint64_t payload = decoder->packet.payload;
2236 /* VMX adds the TSC Offset, so subtract to get host TSC */
2237 decoder->packet.payload -= vmcs_info->tsc_offset;
2238 /* TSC packet has only 7 bytes */
2239 decoder->packet.payload &= SEVEN_BYTES;
2242 * The buffer is mmapped from the data file, so this also updates the
2245 if (!decoder->vm_tm_corr_dry_run)
2246 memcpy((void *)decoder->buf + 1, &decoder->packet.payload, 7);
2248 intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
2249 " VMCS %#" PRIx64 " TSC Offset %#" PRIx64 "\n",
2250 payload, decoder->packet.payload, vmcs_info->vmcs,
2251 vmcs_info->tsc_offset);
2254 static void intel_pt_translate_vm_tsc_offset(struct intel_pt_decoder *decoder,
2255 uint64_t tsc_offset)
2257 struct intel_pt_vmcs_info vmcs_info = {
2259 .tsc_offset = tsc_offset
2262 intel_pt_translate_vm_tsc(decoder, &vmcs_info);
2265 static inline bool in_vm(uint64_t pip_payload)
2267 return pip_payload & 1;
2270 static inline bool pip_in_vm(struct intel_pt_pkt *pip_packet)
2272 return pip_packet->payload & 1;
2275 static void intel_pt_print_vmcs_info(struct intel_pt_vmcs_info *vmcs_info)
2277 p_log("VMCS: %#" PRIx64 " TSC Offset %#" PRIx64,
2278 vmcs_info->vmcs, vmcs_info->tsc_offset);
2281 static void intel_pt_vm_tm_corr_psb(struct intel_pt_decoder *decoder,
2282 struct intel_pt_vm_tsc_info *data)
2284 memset(data, 0, sizeof(*data));
2285 data->ctc_delta = decoder->ctc_delta;
2286 data->last_ctc = decoder->last_ctc;
2287 intel_pt_pkt_lookahead(decoder, intel_pt_vm_psb_lookahead_cb, data);
2288 if (data->tsc && !data->psbend)
2289 p_log("ERROR: PSB without PSBEND");
2290 decoder->in_psb = data->psbend;
2293 static void intel_pt_vm_tm_corr_first_tsc(struct intel_pt_decoder *decoder,
2294 struct intel_pt_vm_tsc_info *data,
2295 struct intel_pt_vmcs_info *vmcs_info,
2298 if (!decoder->in_psb) {
2300 p_log("ERROR: First TSC is not in PSB+");
2304 if (pip_in_vm(&data->pip_packet)) { /* Guest */
2305 if (vmcs_info && vmcs_info->tsc_offset) {
2306 intel_pt_translate_vm_tsc(decoder, vmcs_info);
2307 decoder->vm_tm_corr_reliable = true;
2309 p_log("ERROR: First TSC, unknown TSC Offset");
2312 decoder->vm_tm_corr_reliable = true;
2314 } else { /* Host or Guest */
2315 decoder->vm_tm_corr_reliable = false;
2316 if (intel_pt_time_in_range(decoder, host_tsc)) {
2320 if (vmcs_info && vmcs_info->tsc_offset)
2321 intel_pt_translate_vm_tsc(decoder, vmcs_info);
2323 p_log("ERROR: First TSC, no PIP, unknown TSC Offset");
2328 static void intel_pt_vm_tm_corr_tsc(struct intel_pt_decoder *decoder,
2329 struct intel_pt_vm_tsc_info *data)
2331 struct intel_pt_vmcs_info *vmcs_info;
2332 uint64_t tsc_offset = 0;
2334 bool reliable = true;
2335 uint64_t expected_tsc;
2337 uint64_t ref_timestamp;
2339 bool assign = false;
2340 bool assign_reliable = false;
2342 /* Already have 'data' for the in_psb case */
2343 if (!decoder->in_psb) {
2344 memset(data, 0, sizeof(*data));
2345 data->ctc_delta = decoder->ctc_delta;
2346 data->last_ctc = decoder->last_ctc;
2347 data->max_lookahead = 16;
2348 intel_pt_pkt_lookahead(decoder, intel_pt_tma_lookahead_cb, data);
2351 data->pip_packet.payload = decoder->pip_payload;
2355 /* Calculations depend on having TMA packets */
2357 p_log("ERROR: TSC without TMA");
2361 vmcs = data->vmcs ? data->vmcs_packet.payload : decoder->vmcs;
2362 if (vmcs == NO_VMCS)
2365 vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
2367 ref_timestamp = decoder->timestamp ? decoder->timestamp : decoder->buf_timestamp;
2368 host_tsc = intel_pt_8b_tsc(decoder->packet.payload, ref_timestamp);
2370 if (!decoder->ctc_timestamp) {
2371 intel_pt_vm_tm_corr_first_tsc(decoder, data, vmcs_info, host_tsc);
2375 expected_tsc = intel_pt_expected_tsc(decoder, data);
2377 tsc_offset = host_tsc - expected_tsc;
2379 /* Determine if TSC is from Host or Guest */
2381 if (pip_in_vm(&data->pip_packet)) { /* Guest */
2383 /* PIP NR=1 without VMCS cannot happen */
2384 p_log("ERROR: Missing VMCS");
2385 intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
2386 decoder->vm_tm_corr_reliable = false;
2390 decoder->last_reliable_timestamp = host_tsc;
2391 decoder->vm_tm_corr_reliable = true;
2394 } else { /* Host or Guest */
2395 reliable = false; /* Host/Guest is a guess, so not reliable */
2396 if (decoder->in_psb) {
2398 return; /* Zero TSC Offset, assume Host */
2400 * TSC packet has only 7 bytes of TSC. We have no
2401 * information about the Guest's 8th byte, but it
2402 * doesn't matter because we only need 7 bytes.
2403 * Here, since the 8th byte is unreliable and
2404 * irrelevant, compare only 7 byes.
2407 (tsc_offset & SEVEN_BYTES) ==
2408 (vmcs_info->tsc_offset & SEVEN_BYTES)) {
2409 /* Same TSC Offset as last VMCS, assume Guest */
2414 * Check if the host_tsc is within the expected range.
2415 * Note, we could narrow the range more by looking ahead for
2416 * the next host TSC in the same buffer, but we don't bother to
2417 * do that because this is probably good enough.
2419 if (host_tsc >= expected_tsc && intel_pt_time_in_range(decoder, host_tsc)) {
2420 /* Within expected range for Host TSC, assume Host */
2421 decoder->vm_tm_corr_reliable = false;
2426 guest: /* Assuming Guest */
2428 /* Determine whether to assign TSC Offset */
2429 if (vmcs_info && vmcs_info->vmcs) {
2430 if (vmcs_info->tsc_offset && vmcs_info->reliable) {
2432 } else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_reliable &&
2433 decoder->vm_tm_corr_continuous && decoder->vm_tm_corr_same_buf) {
2434 /* Continuous tracing, TSC in a PSB is not a time loss */
2436 assign_reliable = true;
2437 } else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_same_buf) {
2439 * Unlikely to be a time loss TSC in a PSB which is not
2440 * at the start of a buffer.
2443 assign_reliable = false;
2447 /* Record VMCS TSC Offset */
2448 if (assign && (vmcs_info->tsc_offset != tsc_offset ||
2449 vmcs_info->reliable != assign_reliable)) {
2450 bool print = vmcs_info->tsc_offset != tsc_offset;
2452 vmcs_info->tsc_offset = tsc_offset;
2453 vmcs_info->reliable = assign_reliable;
2455 intel_pt_print_vmcs_info(vmcs_info);
2458 /* Determine what TSC Offset to use */
2459 if (vmcs_info && vmcs_info->tsc_offset) {
2460 if (!vmcs_info->reliable)
2462 intel_pt_translate_vm_tsc(decoder, vmcs_info);
2466 if (!vmcs_info->error_printed) {
2467 p_log("ERROR: Unknown TSC Offset for VMCS %#" PRIx64,
2469 vmcs_info->error_printed = true;
2472 if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
2473 p_log("ERROR: Unknown VMCS");
2475 intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
2478 decoder->vm_tm_corr_reliable = reliable;
2481 static void intel_pt_vm_tm_corr_pebs_tsc(struct intel_pt_decoder *decoder)
2483 uint64_t host_tsc = decoder->packet.payload;
2484 uint64_t guest_tsc = decoder->packet.payload;
2485 struct intel_pt_vmcs_info *vmcs_info;
2488 vmcs = decoder->vmcs;
2489 if (vmcs == NO_VMCS)
2492 vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
2495 if (in_vm(decoder->pip_payload)) { /* Guest */
2497 /* PIP NR=1 without VMCS cannot happen */
2498 p_log("ERROR: Missing VMCS");
2503 } else { /* Host or Guest */
2504 if (intel_pt_time_in_range(decoder, host_tsc)) {
2505 /* Within expected range for Host TSC, assume Host */
2511 /* Translate Guest TSC to Host TSC */
2512 host_tsc = ((guest_tsc & SEVEN_BYTES) - vmcs_info->tsc_offset) & SEVEN_BYTES;
2513 host_tsc = intel_pt_8b_tsc(host_tsc, decoder->timestamp);
2514 intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
2515 " VMCS %#" PRIx64 " TSC Offset %#" PRIx64 "\n",
2516 guest_tsc, host_tsc, vmcs_info->vmcs,
2517 vmcs_info->tsc_offset);
2518 if (!intel_pt_time_in_range(decoder, host_tsc) &&
2519 intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
2520 p_log("Timestamp out of range");
2522 if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
2523 p_log("ERROR: Unknown VMCS");
2524 host_tsc = decoder->timestamp;
2527 decoder->packet.payload = host_tsc;
2529 if (!decoder->vm_tm_corr_dry_run)
2530 memcpy((void *)decoder->buf + 1, &host_tsc, 8);
2533 static int intel_pt_vm_time_correlation(struct intel_pt_decoder *decoder)
2535 struct intel_pt_vm_tsc_info data = { .psbend = false };
2539 if (decoder->in_psb)
2540 intel_pt_vm_tm_corr_psb(decoder, &data);
2543 err = intel_pt_get_next_packet(decoder);
2544 if (err == -ENOLINK)
2549 switch (decoder->packet.type) {
2550 case INTEL_PT_TIP_PGD:
2551 decoder->pge = false;
2552 decoder->vm_tm_corr_continuous = false;
2557 case INTEL_PT_TIP_PGE:
2558 decoder->pge = true;
2562 decoder->in_psb = false;
2564 decoder->pge = intel_pt_ovf_fup_lookahead(decoder);
2565 if (pge != decoder->pge)
2566 intel_pt_log("Surprising PGE change in OVF!");
2568 decoder->vm_tm_corr_continuous = false;
2572 if (decoder->in_psb)
2573 decoder->pge = true;
2576 case INTEL_PT_TRACESTOP:
2577 decoder->pge = false;
2578 decoder->vm_tm_corr_continuous = false;
2579 decoder->have_tma = false;
2583 intel_pt_vm_tm_corr_psb(decoder, &data);
2587 decoder->pip_payload = decoder->packet.payload;
2591 intel_pt_calc_mtc_timestamp(decoder);
2595 intel_pt_vm_tm_corr_tsc(decoder, &data);
2596 intel_pt_calc_tsc_timestamp(decoder);
2597 decoder->vm_tm_corr_same_buf = true;
2598 decoder->vm_tm_corr_continuous = decoder->pge;
2602 intel_pt_calc_tma(decoder);
2606 intel_pt_calc_cyc_timestamp(decoder);
2610 intel_pt_calc_cbr(decoder);
2613 case INTEL_PT_PSBEND:
2614 decoder->in_psb = false;
2615 data.psbend = false;
2619 if (decoder->packet.payload != NO_VMCS)
2620 decoder->vmcs = decoder->packet.payload;
2624 decoder->blk_type = decoder->packet.payload;
2628 if (decoder->blk_type == INTEL_PT_PEBS_BASIC &&
2629 decoder->packet.count == 2)
2630 intel_pt_vm_tm_corr_pebs_tsc(decoder);
2634 case INTEL_PT_BEP_IP:
2635 decoder->blk_type = 0;
2638 case INTEL_PT_MODE_EXEC:
2639 case INTEL_PT_MODE_TSX:
2642 case INTEL_PT_PTWRITE_IP:
2643 case INTEL_PT_PTWRITE:
2644 case INTEL_PT_MWAIT:
2646 case INTEL_PT_EXSTOP_IP:
2647 case INTEL_PT_EXSTOP:
2649 case INTEL_PT_BAD: /* Does not happen */
2658 #define HOP_PROCESS 0
2659 #define HOP_IGNORE 1
2660 #define HOP_RETURN 2
2663 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder);
2665 /* Hop mode: Ignore TNT, do not walk code, but get ip from FUPs and TIPs */
2666 static int intel_pt_hop_trace(struct intel_pt_decoder *decoder, bool *no_tip, int *err)
2668 /* Leap from PSB to PSB, getting ip from FUP within PSB+ */
2669 if (decoder->leap && !decoder->in_psb && decoder->packet.type != INTEL_PT_PSB) {
2670 *err = intel_pt_scan_for_psb(decoder);
2675 switch (decoder->packet.type) {
2679 case INTEL_PT_TIP_PGD:
2680 if (!decoder->packet.count) {
2681 intel_pt_set_nr(decoder);
2684 intel_pt_set_ip(decoder);
2685 decoder->state.type |= INTEL_PT_TRACE_END;
2686 decoder->state.from_ip = 0;
2687 decoder->state.to_ip = decoder->ip;
2688 intel_pt_update_nr(decoder);
2692 if (!decoder->packet.count) {
2693 intel_pt_set_nr(decoder);
2696 intel_pt_set_ip(decoder);
2697 decoder->state.type = INTEL_PT_INSTRUCTION;
2698 decoder->state.from_ip = decoder->ip;
2699 decoder->state.to_ip = 0;
2700 intel_pt_update_nr(decoder);
2704 if (!decoder->packet.count)
2706 intel_pt_set_ip(decoder);
2707 if (intel_pt_fup_event(decoder))
2709 if (!decoder->branch_enable)
2712 decoder->state.type = INTEL_PT_INSTRUCTION;
2713 decoder->state.from_ip = decoder->ip;
2714 decoder->state.to_ip = 0;
2717 *err = intel_pt_walk_fup_tip(decoder);
2719 decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2723 decoder->state.psb_offset = decoder->pos;
2724 decoder->psb_ip = 0;
2725 decoder->last_ip = 0;
2726 decoder->have_last_ip = true;
2727 *err = intel_pt_walk_psbend(decoder);
2728 if (*err == -EAGAIN)
2732 decoder->state.type = INTEL_PT_PSB_EVT;
2733 if (decoder->psb_ip) {
2734 decoder->state.type |= INTEL_PT_INSTRUCTION;
2735 decoder->ip = decoder->psb_ip;
2737 decoder->state.from_ip = decoder->psb_ip;
2738 decoder->state.to_ip = 0;
2743 case INTEL_PT_TIP_PGE:
2746 case INTEL_PT_MODE_EXEC:
2747 case INTEL_PT_MODE_TSX:
2751 case INTEL_PT_PSBEND:
2753 case INTEL_PT_TRACESTOP:
2757 case INTEL_PT_PTWRITE:
2758 case INTEL_PT_PTWRITE_IP:
2759 case INTEL_PT_EXSTOP:
2760 case INTEL_PT_EXSTOP_IP:
2761 case INTEL_PT_MWAIT:
2767 case INTEL_PT_BEP_IP:
2773 struct intel_pt_psb_info {
2774 struct intel_pt_pkt fup_packet;
2779 /* Lookahead and get the FUP packet from PSB+ */
2780 static int intel_pt_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2782 struct intel_pt_psb_info *data = pkt_info->data;
2784 switch (pkt_info->packet.type) {
2789 case INTEL_PT_MODE_EXEC:
2790 case INTEL_PT_MODE_TSX:
2796 if (data->after_psbend) {
2797 data->after_psbend -= 1;
2798 if (!data->after_psbend)
2804 if (data->after_psbend)
2806 if (data->fup || pkt_info->packet.count == 0)
2808 data->fup_packet = pkt_info->packet;
2812 case INTEL_PT_PSBEND:
2815 /* Keep going to check for a TIP.PGE */
2816 data->after_psbend = 6;
2819 case INTEL_PT_TIP_PGE:
2820 /* Ignore FUP in PSB+ if followed by TIP.PGE */
2821 if (data->after_psbend)
2825 case INTEL_PT_PTWRITE:
2826 case INTEL_PT_PTWRITE_IP:
2827 case INTEL_PT_EXSTOP:
2828 case INTEL_PT_EXSTOP_IP:
2829 case INTEL_PT_MWAIT:
2835 case INTEL_PT_BEP_IP:
2836 if (data->after_psbend) {
2837 data->after_psbend -= 1;
2838 if (!data->after_psbend)
2847 case INTEL_PT_TIP_PGD:
2850 case INTEL_PT_TRACESTOP:
2858 static int intel_pt_psb(struct intel_pt_decoder *decoder)
2862 decoder->last_ip = 0;
2863 decoder->psb_ip = 0;
2864 decoder->have_last_ip = true;
2865 intel_pt_clear_stack(&decoder->stack);
2866 err = intel_pt_walk_psbend(decoder);
2869 decoder->state.type = INTEL_PT_PSB_EVT;
2870 decoder->state.from_ip = decoder->psb_ip;
2871 decoder->state.to_ip = 0;
2875 static int intel_pt_fup_in_psb(struct intel_pt_decoder *decoder)
2879 if (decoder->ip != decoder->last_ip) {
2880 err = intel_pt_walk_fup(decoder);
2881 if (!err || err != -EAGAIN)
2885 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2886 err = intel_pt_psb(decoder);
2888 decoder->pkt_state = INTEL_PT_STATE_ERR3;
2895 static bool intel_pt_psb_with_fup(struct intel_pt_decoder *decoder, int *err)
2897 struct intel_pt_psb_info data = { .fup = false };
2899 if (!decoder->branch_enable || !decoder->pge)
2902 intel_pt_pkt_lookahead(decoder, intel_pt_psb_lookahead_cb, &data);
2906 decoder->packet = data.fup_packet;
2907 intel_pt_set_last_ip(decoder);
2908 decoder->pkt_state = INTEL_PT_STATE_FUP_IN_PSB;
2910 *err = intel_pt_fup_in_psb(decoder);
2915 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
2917 int last_packet_type = INTEL_PT_PAD;
2918 bool no_tip = false;
2922 err = intel_pt_get_next_packet(decoder);
2926 if (decoder->cyc_threshold) {
2927 if (decoder->sample_cyc && last_packet_type != INTEL_PT_CYC)
2928 decoder->sample_cyc = false;
2929 last_packet_type = decoder->packet.type;
2933 switch (intel_pt_hop_trace(decoder, &no_tip, &err)) {
2945 switch (decoder->packet.type) {
2947 if (!decoder->packet.count)
2949 decoder->tnt = decoder->packet;
2950 decoder->pkt_state = INTEL_PT_STATE_TNT;
2951 err = intel_pt_walk_tnt(decoder);
2956 case INTEL_PT_TIP_PGD:
2957 if (decoder->packet.count != 0)
2958 intel_pt_set_last_ip(decoder);
2959 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
2960 return intel_pt_walk_tip(decoder);
2962 case INTEL_PT_TIP_PGE: {
2963 decoder->pge = true;
2964 intel_pt_mtc_cyc_cnt_pge(decoder);
2965 intel_pt_set_nr(decoder);
2966 if (decoder->packet.count == 0) {
2967 intel_pt_log_at("Skipping zero TIP.PGE",
2971 intel_pt_set_ip(decoder);
2972 decoder->state.from_ip = 0;
2973 decoder->state.to_ip = decoder->ip;
2974 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2976 * In hop mode, resample to get the to_ip as an
2977 * "instruction" sample.
2980 decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2985 return intel_pt_overflow(decoder);
2988 if (decoder->packet.count != 0)
2989 intel_pt_set_last_ip(decoder);
2990 decoder->pkt_state = INTEL_PT_STATE_TIP;
2991 return intel_pt_walk_tip(decoder);
2994 if (decoder->packet.count == 0) {
2995 intel_pt_log_at("Skipping zero FUP",
3000 intel_pt_set_last_ip(decoder);
3001 if (!decoder->branch_enable) {
3002 decoder->ip = decoder->last_ip;
3003 if (intel_pt_fup_event(decoder))
3008 if (decoder->set_fup_mwait)
3011 decoder->pkt_state = INTEL_PT_STATE_FUP_NO_TIP;
3013 decoder->pkt_state = INTEL_PT_STATE_FUP;
3014 err = intel_pt_walk_fup(decoder);
3021 return intel_pt_walk_fup_tip(decoder);
3023 case INTEL_PT_TRACESTOP:
3024 decoder->pge = false;
3025 decoder->continuous_period = false;
3026 intel_pt_clear_tx_flags(decoder);
3027 decoder->have_tma = false;
3031 decoder->state.psb_offset = decoder->pos;
3032 decoder->psb_ip = 0;
3033 if (intel_pt_psb_with_fup(decoder, &err))
3035 err = intel_pt_psb(decoder);
3041 intel_pt_update_pip(decoder);
3045 intel_pt_calc_mtc_timestamp(decoder);
3046 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
3049 * Ensure that there has been an instruction since the
3052 if (!decoder->mtc_insn)
3054 decoder->mtc_insn = false;
3055 /* Ensure that there is a timestamp */
3056 if (!decoder->timestamp)
3058 decoder->state.type = INTEL_PT_INSTRUCTION;
3059 decoder->state.from_ip = decoder->ip;
3060 decoder->state.to_ip = 0;
3061 decoder->mtc_insn = false;
3065 intel_pt_calc_tsc_timestamp(decoder);
3069 intel_pt_calc_tma(decoder);
3073 intel_pt_calc_cyc_timestamp(decoder);
3077 intel_pt_calc_cbr(decoder);
3078 if (decoder->cbr != decoder->cbr_seen) {
3079 decoder->state.type = 0;
3084 case INTEL_PT_MODE_EXEC:
3085 decoder->exec_mode = decoder->packet.payload;
3088 case INTEL_PT_MODE_TSX:
3089 /* MODE_TSX need not be followed by FUP */
3090 if (!decoder->pge || decoder->in_psb) {
3091 intel_pt_update_in_tx(decoder);
3094 err = intel_pt_mode_tsx(decoder, &no_tip);
3099 case INTEL_PT_BAD: /* Does not happen */
3100 return intel_pt_bug(decoder);
3102 case INTEL_PT_PSBEND:
3108 case INTEL_PT_PTWRITE_IP:
3109 decoder->fup_ptw_payload = decoder->packet.payload;
3110 err = intel_pt_get_next_packet(decoder);
3113 if (decoder->packet.type == INTEL_PT_FUP) {
3114 decoder->set_fup_ptw = true;
3117 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
3122 case INTEL_PT_PTWRITE:
3123 decoder->state.type = INTEL_PT_PTW;
3124 decoder->state.from_ip = decoder->ip;
3125 decoder->state.to_ip = 0;
3126 decoder->state.ptw_payload = decoder->packet.payload;
3129 case INTEL_PT_MWAIT:
3130 decoder->fup_mwait_payload = decoder->packet.payload;
3131 decoder->set_fup_mwait = true;
3135 if (decoder->set_fup_mwait) {
3136 decoder->fup_pwre_payload =
3137 decoder->packet.payload;
3138 decoder->set_fup_pwre = true;
3141 decoder->state.type = INTEL_PT_PWR_ENTRY;
3142 decoder->state.from_ip = decoder->ip;
3143 decoder->state.to_ip = 0;
3144 decoder->state.pwrx_payload = decoder->packet.payload;
3147 case INTEL_PT_EXSTOP_IP:
3148 err = intel_pt_get_next_packet(decoder);
3151 if (decoder->packet.type == INTEL_PT_FUP) {
3152 decoder->set_fup_exstop = true;
3155 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
3160 case INTEL_PT_EXSTOP:
3161 decoder->state.type = INTEL_PT_EX_STOP;
3162 decoder->state.from_ip = decoder->ip;
3163 decoder->state.to_ip = 0;
3167 decoder->state.type = INTEL_PT_PWR_EXIT;
3168 decoder->state.from_ip = decoder->ip;
3169 decoder->state.to_ip = 0;
3170 decoder->state.pwrx_payload = decoder->packet.payload;
3174 intel_pt_bbp(decoder);
3178 intel_pt_bip(decoder);
3182 decoder->state.type = INTEL_PT_BLK_ITEMS;
3183 decoder->state.from_ip = decoder->ip;
3184 decoder->state.to_ip = 0;
3187 case INTEL_PT_BEP_IP:
3188 err = intel_pt_get_next_packet(decoder);
3191 if (decoder->packet.type == INTEL_PT_FUP) {
3192 decoder->set_fup_bep = true;
3195 intel_pt_log_at("ERROR: Missing FUP after BEP",
3201 return intel_pt_bug(decoder);
3206 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
3208 return decoder->packet.count &&
3209 (decoder->have_last_ip || decoder->packet.count == 3 ||
3210 decoder->packet.count == 6);
3213 /* Walk PSB+ packets to get in sync. */
3214 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
3218 decoder->in_psb = true;
3221 err = intel_pt_get_next_packet(decoder);
3225 switch (decoder->packet.type) {
3226 case INTEL_PT_TIP_PGD:
3227 decoder->continuous_period = false;
3229 case INTEL_PT_TIP_PGE:
3231 case INTEL_PT_PTWRITE:
3232 case INTEL_PT_PTWRITE_IP:
3233 case INTEL_PT_EXSTOP:
3234 case INTEL_PT_EXSTOP_IP:
3235 case INTEL_PT_MWAIT:
3241 case INTEL_PT_BEP_IP:
3242 intel_pt_log("ERROR: Unexpected packet\n");
3247 decoder->pge = true;
3248 if (intel_pt_have_ip(decoder)) {
3249 uint64_t current_ip = decoder->ip;
3251 intel_pt_set_ip(decoder);
3252 decoder->psb_ip = decoder->ip;
3254 intel_pt_log_to("Setting IP",
3260 intel_pt_calc_mtc_timestamp(decoder);
3264 intel_pt_calc_tsc_timestamp(decoder);
3268 intel_pt_calc_tma(decoder);
3272 intel_pt_calc_cyc_timestamp(decoder);
3276 intel_pt_calc_cbr(decoder);
3280 intel_pt_set_pip(decoder);
3283 case INTEL_PT_MODE_EXEC:
3284 decoder->exec_mode = decoder->packet.payload;
3287 case INTEL_PT_MODE_TSX:
3288 intel_pt_update_in_tx(decoder);
3291 case INTEL_PT_TRACESTOP:
3292 decoder->pge = false;
3293 decoder->continuous_period = false;
3294 intel_pt_clear_tx_flags(decoder);
3298 decoder->have_tma = false;
3299 intel_pt_log("ERROR: Unexpected packet\n");
3301 decoder->pkt_state = INTEL_PT_STATE_ERR4;
3303 decoder->pkt_state = INTEL_PT_STATE_ERR3;
3307 case INTEL_PT_BAD: /* Does not happen */
3308 err = intel_pt_bug(decoder);
3312 err = intel_pt_overflow(decoder);
3315 case INTEL_PT_PSBEND:
3328 decoder->in_psb = false;
3333 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
3338 err = intel_pt_get_next_packet(decoder);
3342 switch (decoder->packet.type) {
3343 case INTEL_PT_TIP_PGD:
3344 decoder->continuous_period = false;
3345 decoder->pge = false;
3346 if (intel_pt_have_ip(decoder))
3347 intel_pt_set_ip(decoder);
3350 decoder->state.type |= INTEL_PT_TRACE_END;
3353 case INTEL_PT_TIP_PGE:
3354 decoder->pge = true;
3355 intel_pt_mtc_cyc_cnt_pge(decoder);
3356 if (intel_pt_have_ip(decoder))
3357 intel_pt_set_ip(decoder);
3360 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
3364 decoder->pge = true;
3365 if (intel_pt_have_ip(decoder))
3366 intel_pt_set_ip(decoder);
3372 if (intel_pt_have_ip(decoder))
3373 intel_pt_set_ip(decoder);
3379 intel_pt_calc_mtc_timestamp(decoder);
3383 intel_pt_calc_tsc_timestamp(decoder);
3387 intel_pt_calc_tma(decoder);
3391 intel_pt_calc_cyc_timestamp(decoder);
3395 intel_pt_calc_cbr(decoder);
3399 intel_pt_set_pip(decoder);
3402 case INTEL_PT_MODE_EXEC:
3403 decoder->exec_mode = decoder->packet.payload;
3406 case INTEL_PT_MODE_TSX:
3407 intel_pt_update_in_tx(decoder);
3411 return intel_pt_overflow(decoder);
3413 case INTEL_PT_BAD: /* Does not happen */
3414 return intel_pt_bug(decoder);
3416 case INTEL_PT_TRACESTOP:
3417 decoder->pge = false;
3418 decoder->continuous_period = false;
3419 intel_pt_clear_tx_flags(decoder);
3420 decoder->have_tma = false;
3424 decoder->state.psb_offset = decoder->pos;
3425 decoder->psb_ip = 0;
3426 decoder->last_ip = 0;
3427 decoder->have_last_ip = true;
3428 intel_pt_clear_stack(&decoder->stack);
3429 err = intel_pt_walk_psb(decoder);
3432 decoder->state.type = INTEL_PT_PSB_EVT;
3433 decoder->state.from_ip = decoder->psb_ip;
3434 decoder->state.to_ip = 0;
3438 case INTEL_PT_PSBEND:
3442 case INTEL_PT_PTWRITE:
3443 case INTEL_PT_PTWRITE_IP:
3444 case INTEL_PT_EXSTOP:
3445 case INTEL_PT_EXSTOP_IP:
3446 case INTEL_PT_MWAIT:
3452 case INTEL_PT_BEP_IP:
3459 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
3463 decoder->set_fup_tx_flags = false;
3464 decoder->set_fup_ptw = false;
3465 decoder->set_fup_mwait = false;
3466 decoder->set_fup_pwre = false;
3467 decoder->set_fup_exstop = false;
3468 decoder->set_fup_bep = false;
3470 if (!decoder->branch_enable) {
3471 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3472 decoder->overflow = false;
3473 decoder->state.type = 0; /* Do not have a sample */
3477 intel_pt_log("Scanning for full IP\n");
3478 err = intel_pt_walk_to_ip(decoder);
3479 if (err || ((decoder->state.type & INTEL_PT_PSB_EVT) && !decoder->ip))
3482 /* In hop mode, resample to get the to_ip as an "instruction" sample */
3484 decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
3486 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3487 decoder->overflow = false;
3489 decoder->state.from_ip = 0;
3490 decoder->state.to_ip = decoder->ip;
3491 intel_pt_log_to("Setting IP", decoder->ip);
3496 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
3498 const unsigned char *end = decoder->buf + decoder->len;
3501 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
3502 if (i > decoder->len)
3504 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
3510 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
3512 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
3513 const char *psb = INTEL_PT_PSB_STR;
3515 if (rest_psb > decoder->len ||
3516 memcmp(decoder->buf, psb + part_psb, rest_psb))
3522 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
3527 decoder->pos += decoder->len;
3530 ret = intel_pt_get_next_data(decoder, false);
3534 rest_psb = intel_pt_rest_psb(decoder, part_psb);
3538 decoder->pos -= part_psb;
3539 decoder->next_buf = decoder->buf + rest_psb;
3540 decoder->next_len = decoder->len - rest_psb;
3541 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
3542 decoder->buf = decoder->temp_buf;
3543 decoder->len = INTEL_PT_PSB_LEN;
3548 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
3550 unsigned char *next;
3553 intel_pt_log("Scanning for PSB\n");
3555 if (!decoder->len) {
3556 ret = intel_pt_get_next_data(decoder, false);
3561 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
3566 part_psb = intel_pt_part_psb(decoder);
3568 ret = intel_pt_get_split_psb(decoder, part_psb);
3572 decoder->pos += decoder->len;
3578 decoder->pkt_step = next - decoder->buf;
3579 return intel_pt_get_next_packet(decoder);
3583 static int intel_pt_sync(struct intel_pt_decoder *decoder)
3587 decoder->pge = false;
3588 decoder->continuous_period = false;
3589 decoder->have_last_ip = false;
3590 decoder->last_ip = 0;
3591 decoder->psb_ip = 0;
3593 intel_pt_clear_stack(&decoder->stack);
3595 err = intel_pt_scan_for_psb(decoder);
3599 if (decoder->vm_time_correlation) {
3600 decoder->in_psb = true;
3601 if (!decoder->timestamp)
3602 decoder->timestamp = 1;
3603 decoder->state.type = 0;
3604 decoder->pkt_state = INTEL_PT_STATE_VM_TIME_CORRELATION;
3608 decoder->have_last_ip = true;
3609 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
3611 err = intel_pt_walk_psb(decoder);
3615 decoder->state.type = INTEL_PT_PSB_EVT; /* Only PSB sample */
3616 decoder->state.from_ip = decoder->psb_ip;
3617 decoder->state.to_ip = 0;
3621 * In hop mode, resample to get the PSB FUP ip as an
3622 * "instruction" sample.
3625 decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
3627 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3633 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
3635 uint64_t est = decoder->sample_insn_cnt << 1;
3637 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
3640 est *= decoder->max_non_turbo_ratio;
3641 est /= decoder->cbr;
3643 return decoder->sample_timestamp + est;
3646 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
3651 decoder->state.type = INTEL_PT_BRANCH;
3652 decoder->state.flags = 0;
3654 switch (decoder->pkt_state) {
3655 case INTEL_PT_STATE_NO_PSB:
3656 err = intel_pt_sync(decoder);
3658 case INTEL_PT_STATE_NO_IP:
3659 decoder->have_last_ip = false;
3660 decoder->last_ip = 0;
3663 case INTEL_PT_STATE_ERR_RESYNC:
3664 err = intel_pt_sync_ip(decoder);
3666 case INTEL_PT_STATE_IN_SYNC:
3667 err = intel_pt_walk_trace(decoder);
3669 case INTEL_PT_STATE_TNT:
3670 case INTEL_PT_STATE_TNT_CONT:
3671 err = intel_pt_walk_tnt(decoder);
3673 err = intel_pt_walk_trace(decoder);
3675 case INTEL_PT_STATE_TIP:
3676 case INTEL_PT_STATE_TIP_PGD:
3677 err = intel_pt_walk_tip(decoder);
3679 case INTEL_PT_STATE_FUP:
3680 err = intel_pt_walk_fup(decoder);
3682 err = intel_pt_walk_fup_tip(decoder);
3684 case INTEL_PT_STATE_FUP_NO_TIP:
3685 err = intel_pt_walk_fup(decoder);
3687 err = intel_pt_walk_trace(decoder);
3689 case INTEL_PT_STATE_FUP_IN_PSB:
3690 err = intel_pt_fup_in_psb(decoder);
3692 case INTEL_PT_STATE_RESAMPLE:
3693 err = intel_pt_resample(decoder);
3695 case INTEL_PT_STATE_VM_TIME_CORRELATION:
3696 err = intel_pt_vm_time_correlation(decoder);
3699 err = intel_pt_bug(decoder);
3702 } while (err == -ENOLINK);
3705 decoder->state.err = intel_pt_ext_err(err);
3706 decoder->state.from_ip = decoder->ip;
3707 intel_pt_update_sample_time(decoder);
3708 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
3709 intel_pt_set_nr(decoder);
3711 decoder->state.err = 0;
3712 if (decoder->cbr != decoder->cbr_seen) {
3713 decoder->cbr_seen = decoder->cbr;
3714 if (!decoder->state.type) {
3715 decoder->state.from_ip = decoder->ip;
3716 decoder->state.to_ip = 0;
3718 decoder->state.type |= INTEL_PT_CBR_CHG;
3719 decoder->state.cbr_payload = decoder->cbr_payload;
3720 decoder->state.cbr = decoder->cbr;
3722 if (intel_pt_sample_time(decoder->pkt_state)) {
3723 intel_pt_update_sample_time(decoder);
3724 if (decoder->sample_cyc) {
3725 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
3726 decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
3727 decoder->sample_cyc = false;
3731 * When using only TSC/MTC to compute cycles, IPC can be
3732 * sampled as soon as the cycle count changes.
3734 if (!decoder->have_cyc)
3735 decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
3738 /* Let PSB event always have TSC timestamp */
3739 if ((decoder->state.type & INTEL_PT_PSB_EVT) && decoder->tsc_timestamp)
3740 decoder->sample_timestamp = decoder->tsc_timestamp;
3742 decoder->state.from_nr = decoder->nr;
3743 decoder->state.to_nr = decoder->next_nr;
3744 decoder->nr = decoder->next_nr;
3746 decoder->state.timestamp = decoder->sample_timestamp;
3747 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
3748 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
3749 decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
3751 return &decoder->state;
3755 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
3756 * @buf: pointer to buffer pointer
3757 * @len: size of buffer
3759 * Updates the buffer pointer to point to the start of the next PSB packet if
3760 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
3761 * @len is adjusted accordingly.
3763 * Return: %true if a PSB packet is found, %false otherwise.
3765 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
3767 unsigned char *next;
3769 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
3771 *len -= next - *buf;
3779 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
3781 * @buf: pointer to buffer pointer
3782 * @len: size of buffer
3784 * Updates the buffer pointer to point to the start of the following PSB packet
3785 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
3786 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
3788 * Return: %true if a PSB packet is found, %false otherwise.
3790 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
3792 unsigned char *next;
3797 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
3799 *len -= next - *buf;
3807 * intel_pt_last_psb - find the last PSB packet in a buffer.
3809 * @len: size of buffer
3811 * This function finds the last PSB in a buffer.
3813 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
3815 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
3817 const char *n = INTEL_PT_PSB_STR;
3821 if (len < INTEL_PT_PSB_LEN)
3824 k = len - INTEL_PT_PSB_LEN + 1;
3826 p = memrchr(buf, n[0], k);
3829 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
3838 * intel_pt_next_tsc - find and return next TSC.
3840 * @len: size of buffer
3841 * @tsc: TSC value returned
3842 * @rem: returns remaining size when TSC is found
3844 * Find a TSC packet in @buf and return the TSC value. This function assumes
3845 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
3846 * PSBEND packet is found.
3848 * Return: %true if TSC is found, false otherwise.
3850 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
3853 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
3854 struct intel_pt_pkt packet;
3858 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
3861 if (packet.type == INTEL_PT_TSC) {
3862 *tsc = packet.payload;
3866 if (packet.type == INTEL_PT_PSBEND)
3875 * intel_pt_tsc_cmp - compare 7-byte TSCs.
3876 * @tsc1: first TSC to compare
3877 * @tsc2: second TSC to compare
3879 * This function compares 7-byte TSC values allowing for the possibility that
3880 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
3881 * around so for that purpose this function assumes the absolute difference is
3882 * less than half the maximum difference.
3884 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
3887 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
3889 const uint64_t halfway = (1ULL << 55);
3895 if (tsc2 - tsc1 < halfway)
3900 if (tsc1 - tsc2 < halfway)
3907 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
3910 * adj_for_padding - adjust overlap to account for padding.
3911 * @buf_b: second buffer
3912 * @buf_a: first buffer
3913 * @len_a: size of first buffer
3915 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
3918 * Return: A pointer into @buf_b from where non-overlapped data starts
3920 static unsigned char *adj_for_padding(unsigned char *buf_b,
3921 unsigned char *buf_a, size_t len_a)
3923 unsigned char *p = buf_b - MAX_PADDING;
3924 unsigned char *q = buf_a + len_a - MAX_PADDING;
3927 for (i = MAX_PADDING; i; i--, p++, q++) {
3936 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
3938 * @buf_a: first buffer
3939 * @len_a: size of first buffer
3940 * @buf_b: second buffer
3941 * @len_b: size of second buffer
3942 * @consecutive: returns true if there is data in buf_b that is consecutive
3944 * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
3946 * If the trace contains TSC we can look at the last TSC of @buf_a and the
3947 * first TSC of @buf_b in order to determine if the buffers overlap, and then
3948 * walk forward in @buf_b until a later TSC is found. A precondition is that
3949 * @buf_a and @buf_b are positioned at a PSB.
3951 * Return: A pointer into @buf_b from where non-overlapped data starts, or
3952 * @buf_b + @len_b if there is no non-overlapped data.
3954 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
3956 unsigned char *buf_b,
3957 size_t len_b, bool *consecutive,
3960 uint64_t tsc_a, tsc_b;
3962 size_t len, rem_a, rem_b;
3964 p = intel_pt_last_psb(buf_a, len_a);
3966 return buf_b; /* No PSB in buf_a => no overlap */
3968 len = len_a - (p - buf_a);
3969 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
3970 /* The last PSB+ in buf_a is incomplete, so go back one more */
3972 p = intel_pt_last_psb(buf_a, len_a);
3974 return buf_b; /* No full PSB+ => assume no overlap */
3975 len = len_a - (p - buf_a);
3976 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
3977 return buf_b; /* No TSC in buf_a => assume no overlap */
3981 /* Ignore PSB+ with no TSC */
3982 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
3983 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
3985 /* Same TSC, so buffers are consecutive */
3986 if (!cmp && rem_b >= rem_a) {
3987 unsigned char *start;
3989 *consecutive = true;
3990 start = buf_b + len_b - (rem_b - rem_a);
3991 return adj_for_padding(start, buf_a, len_a);
3993 if (cmp < 0 && !ooo_tsc)
3994 return buf_b; /* tsc_a < tsc_b => no overlap */
3997 if (!intel_pt_step_psb(&buf_b, &len_b))
3998 return buf_b + len_b; /* No PSB in buf_b => no data */
4003 * intel_pt_find_overlap - determine start of non-overlapped trace data.
4004 * @buf_a: first buffer
4005 * @len_a: size of first buffer
4006 * @buf_b: second buffer
4007 * @len_b: size of second buffer
4008 * @have_tsc: can use TSC packets to detect overlap
4009 * @consecutive: returns true if there is data in buf_b that is consecutive
4011 * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
4013 * When trace samples or snapshots are recorded there is the possibility that
4014 * the data overlaps. Note that, for the purposes of decoding, data is only
4015 * useful if it begins with a PSB packet.
4017 * Return: A pointer into @buf_b from where non-overlapped data starts, or
4018 * @buf_b + @len_b if there is no non-overlapped data.
4020 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
4021 unsigned char *buf_b, size_t len_b,
4022 bool have_tsc, bool *consecutive,
4025 unsigned char *found;
4027 /* Buffer 'b' must start at PSB so throw away everything before that */
4028 if (!intel_pt_next_psb(&buf_b, &len_b))
4029 return buf_b + len_b; /* No PSB */
4031 if (!intel_pt_next_psb(&buf_a, &len_a))
4032 return buf_b; /* No overlap */
4035 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
4036 consecutive, ooo_tsc);
4042 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
4043 * we can ignore the first part of buffer 'a'.
4045 while (len_b < len_a) {
4046 if (!intel_pt_step_psb(&buf_a, &len_a))
4047 return buf_b; /* No overlap */
4050 /* Now len_b >= len_a */
4052 /* Potential overlap so check the bytes */
4053 found = memmem(buf_a, len_a, buf_b, len_a);
4055 *consecutive = true;
4056 return adj_for_padding(buf_b + len_a, buf_a, len_a);
4059 /* Try again at next PSB in buffer 'a' */
4060 if (!intel_pt_step_psb(&buf_a, &len_a))
4061 return buf_b; /* No overlap */
4066 * struct fast_forward_data - data used by intel_pt_ff_cb().
4067 * @timestamp: timestamp to fast forward towards
4068 * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
4069 * the fast forward timestamp.
4071 struct fast_forward_data {
4073 uint64_t buf_timestamp;
4077 * intel_pt_ff_cb - fast forward lookahead callback.
4078 * @buffer: Intel PT trace buffer
4079 * @data: opaque pointer to fast forward data (struct fast_forward_data)
4081 * Determine if @buffer trace is past the fast forward timestamp.
4083 * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
4084 * timestamp, and 0 otherwise.
4086 static int intel_pt_ff_cb(struct intel_pt_buffer *buffer, void *data)
4088 struct fast_forward_data *d = data;
4094 buf = (unsigned char *)buffer->buf;
4097 if (!intel_pt_next_psb(&buf, &len) ||
4098 !intel_pt_next_tsc(buf, len, &tsc, &rem))
4101 tsc = intel_pt_8b_tsc(tsc, buffer->ref_timestamp);
4103 intel_pt_log("Buffer 1st timestamp " x64_fmt " ref timestamp " x64_fmt "\n",
4104 tsc, buffer->ref_timestamp);
4107 * If the buffer contains a timestamp earlier that the fast forward
4108 * timestamp, then record it, else stop.
4110 if (tsc < d->timestamp)
4111 d->buf_timestamp = buffer->ref_timestamp;
4119 * intel_pt_fast_forward - reposition decoder forwards.
4120 * @decoder: Intel PT decoder
4121 * @timestamp: timestamp to fast forward towards
4123 * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
4125 * Return: 0 on success or negative error code on failure.
4127 int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp)
4129 struct fast_forward_data d = { .timestamp = timestamp };
4134 intel_pt_log("Fast forward towards timestamp " x64_fmt "\n", timestamp);
4136 /* Find buffer timestamp of buffer to fast forward to */
4137 err = decoder->lookahead(decoder->data, intel_pt_ff_cb, &d);
4141 /* Walk to buffer with same buffer timestamp */
4142 if (d.buf_timestamp) {
4144 decoder->pos += decoder->len;
4146 err = intel_pt_get_next_data(decoder, true);
4147 /* -ENOLINK means non-consecutive trace */
4148 if (err && err != -ENOLINK)
4150 } while (decoder->buf_timestamp != d.buf_timestamp);
4156 buf = (unsigned char *)decoder->buf;
4159 if (!intel_pt_next_psb(&buf, &len))
4163 * Walk PSBs while the PSB timestamp is less than the fast forward
4170 if (!intel_pt_next_tsc(buf, len, &tsc, &rem))
4172 tsc = intel_pt_8b_tsc(tsc, decoder->buf_timestamp);
4174 * A TSC packet can slip past MTC packets but, after fast
4175 * forward, decoding starts at the TSC timestamp. That means
4176 * the timestamps may not be exactly the same as the timestamps
4177 * that would have been decoded without fast forward.
4179 if (tsc < timestamp) {
4180 intel_pt_log("Fast forward to next PSB timestamp " x64_fmt "\n", tsc);
4181 decoder->pos += decoder->len - len;
4184 intel_pt_reposition(decoder);
4188 } while (intel_pt_step_psb(&buf, &len));