2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 #include <linux/compiler.h>
30 #include "intel-pt-insn-decoder.h"
31 #include "intel-pt-pkt-decoder.h"
32 #include "intel-pt-decoder.h"
33 #include "intel-pt-log.h"
35 #define INTEL_PT_BLK_SIZE 1024
37 #define BIT63 (((uint64_t)1 << 63))
39 #define INTEL_PT_RETURN 1
41 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
42 #define INTEL_PT_MAX_LOOPS 10000
45 struct intel_pt_blk *prev;
46 uint64_t ip[INTEL_PT_BLK_SIZE];
49 struct intel_pt_stack {
50 struct intel_pt_blk *blk;
51 struct intel_pt_blk *spare;
55 enum intel_pt_pkt_state {
56 INTEL_PT_STATE_NO_PSB,
58 INTEL_PT_STATE_ERR_RESYNC,
59 INTEL_PT_STATE_IN_SYNC,
62 INTEL_PT_STATE_TIP_PGD,
64 INTEL_PT_STATE_FUP_NO_TIP,
67 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
70 case INTEL_PT_STATE_NO_PSB:
71 case INTEL_PT_STATE_NO_IP:
72 case INTEL_PT_STATE_ERR_RESYNC:
73 case INTEL_PT_STATE_IN_SYNC:
74 case INTEL_PT_STATE_TNT:
76 case INTEL_PT_STATE_TIP:
77 case INTEL_PT_STATE_TIP_PGD:
78 case INTEL_PT_STATE_FUP:
79 case INTEL_PT_STATE_FUP_NO_TIP:
86 #ifdef INTEL_PT_STRICT
87 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
88 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
89 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
90 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
92 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
93 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
94 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
95 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
98 struct intel_pt_decoder {
99 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
100 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
101 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
102 uint64_t max_insn_cnt, void *data);
103 bool (*pgd_ip)(uint64_t ip, void *data);
105 struct intel_pt_state state;
106 const unsigned char *buf;
108 bool return_compression;
119 uint64_t tsc_timestamp;
120 uint64_t ref_timestamp;
121 uint64_t sample_timestamp;
123 uint64_t ctc_timestamp;
126 uint64_t cyc_ref_timestamp;
128 uint32_t tsc_ctc_ratio_n;
129 uint32_t tsc_ctc_ratio_d;
130 uint32_t tsc_ctc_mult;
132 uint32_t ctc_rem_mask;
134 struct intel_pt_stack stack;
135 enum intel_pt_pkt_state pkt_state;
136 struct intel_pt_pkt packet;
137 struct intel_pt_pkt tnt;
140 int last_packet_type;
142 unsigned int max_non_turbo_ratio;
143 double max_non_turbo_ratio_fp;
144 double cbr_cyc_to_tsc;
145 double calc_cyc_to_tsc;
146 bool have_calc_cyc_to_tsc;
148 unsigned int insn_bytes;
150 enum intel_pt_period_type period_type;
151 uint64_t tot_insn_cnt;
152 uint64_t period_insn_cnt;
153 uint64_t period_mask;
154 uint64_t period_ticks;
155 uint64_t last_masked_timestamp;
156 bool continuous_period;
158 bool set_fup_tx_flags;
159 unsigned int fup_tx_flags;
160 unsigned int tx_flags;
161 uint64_t timestamp_insn_cnt;
162 uint64_t sample_insn_cnt;
167 const unsigned char *next_buf;
169 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
172 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
176 for (i = 0; x != 1; i++)
182 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
184 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
187 period = intel_pt_lower_power_of_2(decoder->period);
188 decoder->period_mask = ~(period - 1);
189 decoder->period_ticks = period;
193 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
197 return (t / d) * n + ((t % d) * n) / d;
200 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
202 struct intel_pt_decoder *decoder;
204 if (!params->get_trace || !params->walk_insn)
207 decoder = zalloc(sizeof(struct intel_pt_decoder));
211 decoder->get_trace = params->get_trace;
212 decoder->walk_insn = params->walk_insn;
213 decoder->pgd_ip = params->pgd_ip;
214 decoder->data = params->data;
215 decoder->return_compression = params->return_compression;
217 decoder->period = params->period;
218 decoder->period_type = params->period_type;
220 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
221 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
223 intel_pt_setup_period(decoder);
225 decoder->mtc_shift = params->mtc_period;
226 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
228 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
229 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
231 if (!decoder->tsc_ctc_ratio_n)
232 decoder->tsc_ctc_ratio_d = 0;
234 if (decoder->tsc_ctc_ratio_d) {
235 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
236 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
237 decoder->tsc_ctc_ratio_d;
240 * Allow for timestamps appearing to backwards because a TSC
241 * packet has slipped past a MTC packet, so allow 2 MTC ticks
244 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
245 decoder->tsc_ctc_ratio_n,
246 decoder->tsc_ctc_ratio_d);
248 /* ... or 0x100 paranoia */
249 if (decoder->tsc_slip < 0x100)
250 decoder->tsc_slip = 0x100;
252 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
253 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
254 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
255 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
256 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
261 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
263 struct intel_pt_blk *blk = stack->blk;
265 stack->blk = blk->prev;
272 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
277 intel_pt_pop_blk(stack);
280 stack->pos = INTEL_PT_BLK_SIZE;
282 return stack->blk->ip[--stack->pos];
285 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
287 struct intel_pt_blk *blk;
293 blk = malloc(sizeof(struct intel_pt_blk));
298 blk->prev = stack->blk;
304 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
308 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
309 err = intel_pt_alloc_blk(stack);
314 stack->blk->ip[stack->pos++] = ip;
318 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
321 intel_pt_pop_blk(stack);
325 static void intel_pt_free_stack(struct intel_pt_stack *stack)
327 intel_pt_clear_stack(stack);
329 zfree(&stack->spare);
332 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
334 intel_pt_free_stack(&decoder->stack);
338 static int intel_pt_ext_err(int code)
342 return INTEL_PT_ERR_NOMEM;
344 return INTEL_PT_ERR_INTERN;
346 return INTEL_PT_ERR_BADPKT;
348 return INTEL_PT_ERR_NODATA;
350 return INTEL_PT_ERR_NOINSN;
352 return INTEL_PT_ERR_MISMAT;
354 return INTEL_PT_ERR_OVR;
356 return INTEL_PT_ERR_LOST;
358 return INTEL_PT_ERR_NELOOP;
360 return INTEL_PT_ERR_UNK;
364 static const char *intel_pt_err_msgs[] = {
365 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
366 [INTEL_PT_ERR_INTERN] = "Internal error",
367 [INTEL_PT_ERR_BADPKT] = "Bad packet",
368 [INTEL_PT_ERR_NODATA] = "No more data",
369 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
370 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
371 [INTEL_PT_ERR_OVR] = "Overflow packet",
372 [INTEL_PT_ERR_LOST] = "Lost trace data",
373 [INTEL_PT_ERR_UNK] = "Unknown error!",
374 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
377 int intel_pt__strerror(int code, char *buf, size_t buflen)
379 if (code < 1 || code >= INTEL_PT_ERR_MAX)
380 code = INTEL_PT_ERR_UNK;
381 strlcpy(buf, intel_pt_err_msgs[code], buflen);
385 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
390 switch (packet->count) {
392 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
396 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
400 ip = packet->payload;
401 /* Sign-extend 6-byte ip */
402 if (ip & (uint64_t)0x800000000000ULL)
403 ip |= (uint64_t)0xffff000000000000ULL;
406 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
410 ip = packet->payload;
419 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
421 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
424 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
426 intel_pt_set_last_ip(decoder);
427 decoder->ip = decoder->last_ip;
430 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
432 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
436 static int intel_pt_bug(struct intel_pt_decoder *decoder)
438 intel_pt_log("ERROR: Internal error\n");
439 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
443 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
445 decoder->tx_flags = 0;
448 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
450 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
453 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
455 intel_pt_clear_tx_flags(decoder);
456 decoder->have_tma = false;
457 decoder->pkt_len = 1;
458 decoder->pkt_step = 1;
459 intel_pt_decoder_log_packet(decoder);
460 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
461 intel_pt_log("ERROR: Bad packet\n");
462 decoder->pkt_state = INTEL_PT_STATE_ERR1;
467 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
469 struct intel_pt_buffer buffer = { .buf = 0, };
472 decoder->pkt_step = 0;
474 intel_pt_log("Getting more data\n");
475 ret = decoder->get_trace(&buffer, decoder->data);
478 decoder->buf = buffer.buf;
479 decoder->len = buffer.len;
481 intel_pt_log("No more data\n");
484 if (!buffer.consecutive) {
486 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
487 decoder->ref_timestamp = buffer.ref_timestamp;
488 decoder->timestamp = 0;
489 decoder->have_tma = false;
490 decoder->state.trace_nr = buffer.trace_nr;
491 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
492 decoder->ref_timestamp);
499 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
501 if (!decoder->next_buf)
502 return intel_pt_get_data(decoder);
504 decoder->buf = decoder->next_buf;
505 decoder->len = decoder->next_len;
506 decoder->next_buf = 0;
507 decoder->next_len = 0;
511 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
513 unsigned char *buf = decoder->temp_buf;
514 size_t old_len, len, n;
517 old_len = decoder->len;
519 memcpy(buf, decoder->buf, len);
521 ret = intel_pt_get_data(decoder);
523 decoder->pos += old_len;
524 return ret < 0 ? ret : -EINVAL;
527 n = INTEL_PT_PKT_MAX_SZ - len;
528 if (n > decoder->len)
530 memcpy(buf + len, decoder->buf, n);
533 ret = intel_pt_get_packet(buf, len, &decoder->packet);
534 if (ret < (int)old_len) {
535 decoder->next_buf = decoder->buf;
536 decoder->next_len = decoder->len;
538 decoder->len = old_len;
539 return intel_pt_bad_packet(decoder);
542 decoder->next_buf = decoder->buf + (ret - old_len);
543 decoder->next_len = decoder->len - (ret - old_len);
551 struct intel_pt_pkt_info {
552 struct intel_pt_decoder *decoder;
553 struct intel_pt_pkt packet;
556 int last_packet_type;
560 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
562 /* Lookahead packets in current buffer */
563 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
564 intel_pt_pkt_cb_t cb, void *data)
566 struct intel_pt_pkt_info pkt_info;
567 const unsigned char *buf = decoder->buf;
568 size_t len = decoder->len;
571 pkt_info.decoder = decoder;
572 pkt_info.pos = decoder->pos;
573 pkt_info.pkt_len = decoder->pkt_step;
574 pkt_info.last_packet_type = decoder->last_packet_type;
575 pkt_info.data = data;
579 pkt_info.pos += pkt_info.pkt_len;
580 buf += pkt_info.pkt_len;
581 len -= pkt_info.pkt_len;
584 return INTEL_PT_NEED_MORE_BYTES;
586 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
588 return INTEL_PT_NEED_MORE_BYTES;
592 pkt_info.pkt_len = ret;
593 } while (pkt_info.packet.type == INTEL_PT_PAD);
599 pkt_info.last_packet_type = pkt_info.packet.type;
603 struct intel_pt_calc_cyc_to_tsc_info {
607 uint64_t ctc_timestamp;
609 uint64_t tsc_timestamp;
614 double cbr_cyc_to_tsc;
618 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
619 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
620 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
621 * packet by copying the missing bits from the current MTC assuming the least
622 * difference between the two, and that the current MTC comes after last_mtc.
624 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
627 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
628 uint32_t mask = ~(first_missing_bit - 1);
630 *last_mtc |= mtc & mask;
631 if (*last_mtc >= mtc) {
632 *last_mtc -= first_missing_bit;
637 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
639 struct intel_pt_decoder *decoder = pkt_info->decoder;
640 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
644 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
646 switch (pkt_info->packet.type) {
648 case INTEL_PT_TIP_PGE:
653 case INTEL_PT_MODE_EXEC:
654 case INTEL_PT_MODE_TSX:
655 case INTEL_PT_PSBEND:
665 mtc = pkt_info->packet.payload;
666 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
667 data->fixup_last_mtc = false;
668 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
671 if (mtc > data->last_mtc)
672 mtc_delta = mtc - data->last_mtc;
674 mtc_delta = mtc + 256 - data->last_mtc;
675 data->ctc_delta += mtc_delta << decoder->mtc_shift;
676 data->last_mtc = mtc;
678 if (decoder->tsc_ctc_mult) {
679 timestamp = data->ctc_timestamp +
680 data->ctc_delta * decoder->tsc_ctc_mult;
682 timestamp = data->ctc_timestamp +
683 multdiv(data->ctc_delta,
684 decoder->tsc_ctc_ratio_n,
685 decoder->tsc_ctc_ratio_d);
688 if (timestamp < data->timestamp)
691 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
692 data->timestamp = timestamp;
699 timestamp = pkt_info->packet.payload |
700 (data->timestamp & (0xffULL << 56));
701 if (data->from_mtc && timestamp < data->timestamp &&
702 data->timestamp - timestamp < decoder->tsc_slip)
704 if (timestamp < data->timestamp)
705 timestamp += (1ULL << 56);
706 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
709 data->tsc_timestamp = timestamp;
710 data->timestamp = timestamp;
719 if (!decoder->tsc_ctc_ratio_d)
722 ctc = pkt_info->packet.payload;
723 fc = pkt_info->packet.count;
724 ctc_rem = ctc & decoder->ctc_rem_mask;
726 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
728 data->ctc_timestamp = data->tsc_timestamp - fc;
729 if (decoder->tsc_ctc_mult) {
730 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
732 data->ctc_timestamp -=
733 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
734 decoder->tsc_ctc_ratio_d);
738 data->have_tma = true;
739 data->fixup_last_mtc = true;
744 data->cycle_cnt += pkt_info->packet.payload;
748 cbr = pkt_info->packet.payload;
749 if (data->cbr && data->cbr != cbr)
752 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
755 case INTEL_PT_TIP_PGD:
756 case INTEL_PT_TRACESTOP:
758 case INTEL_PT_BAD: /* Does not happen */
763 if (!data->cbr && decoder->cbr) {
764 data->cbr = decoder->cbr;
765 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
768 if (!data->cycle_cnt)
771 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
773 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
774 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
775 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
776 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
780 decoder->calc_cyc_to_tsc = cyc_to_tsc;
781 decoder->have_calc_cyc_to_tsc = true;
784 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
785 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
787 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
788 cyc_to_tsc, pkt_info->pos);
794 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
797 struct intel_pt_calc_cyc_to_tsc_info data = {
800 .last_mtc = decoder->last_mtc,
801 .ctc_timestamp = decoder->ctc_timestamp,
802 .ctc_delta = decoder->ctc_delta,
803 .tsc_timestamp = decoder->tsc_timestamp,
804 .timestamp = decoder->timestamp,
805 .have_tma = decoder->have_tma,
806 .fixup_last_mtc = decoder->fixup_last_mtc,
807 .from_mtc = from_mtc,
811 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
814 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
818 decoder->last_packet_type = decoder->packet.type;
821 decoder->pos += decoder->pkt_step;
822 decoder->buf += decoder->pkt_step;
823 decoder->len -= decoder->pkt_step;
826 ret = intel_pt_get_next_data(decoder);
831 ret = intel_pt_get_packet(decoder->buf, decoder->len,
833 if (ret == INTEL_PT_NEED_MORE_BYTES &&
834 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
835 ret = intel_pt_get_split_packet(decoder);
840 return intel_pt_bad_packet(decoder);
842 decoder->pkt_len = ret;
843 decoder->pkt_step = ret;
844 intel_pt_decoder_log_packet(decoder);
845 } while (decoder->packet.type == INTEL_PT_PAD);
850 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
852 uint64_t timestamp, masked_timestamp;
854 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
855 masked_timestamp = timestamp & decoder->period_mask;
856 if (decoder->continuous_period) {
857 if (masked_timestamp != decoder->last_masked_timestamp)
861 masked_timestamp = timestamp & decoder->period_mask;
862 if (masked_timestamp != decoder->last_masked_timestamp) {
863 decoder->last_masked_timestamp = masked_timestamp;
864 decoder->continuous_period = true;
867 return decoder->period_ticks - (timestamp - masked_timestamp);
870 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
872 switch (decoder->period_type) {
873 case INTEL_PT_PERIOD_INSTRUCTIONS:
874 return decoder->period - decoder->period_insn_cnt;
875 case INTEL_PT_PERIOD_TICKS:
876 return intel_pt_next_period(decoder);
877 case INTEL_PT_PERIOD_NONE:
878 case INTEL_PT_PERIOD_MTC:
884 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
886 uint64_t timestamp, masked_timestamp;
888 switch (decoder->period_type) {
889 case INTEL_PT_PERIOD_INSTRUCTIONS:
890 decoder->period_insn_cnt = 0;
892 case INTEL_PT_PERIOD_TICKS:
893 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
894 masked_timestamp = timestamp & decoder->period_mask;
895 decoder->last_masked_timestamp = masked_timestamp;
897 case INTEL_PT_PERIOD_NONE:
898 case INTEL_PT_PERIOD_MTC:
903 decoder->state.type |= INTEL_PT_INSTRUCTION;
906 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
907 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
909 uint64_t max_insn_cnt, insn_cnt = 0;
912 if (!decoder->mtc_insn)
913 decoder->mtc_insn = true;
915 max_insn_cnt = intel_pt_next_sample(decoder);
917 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
918 max_insn_cnt, decoder->data);
920 decoder->tot_insn_cnt += insn_cnt;
921 decoder->timestamp_insn_cnt += insn_cnt;
922 decoder->sample_insn_cnt += insn_cnt;
923 decoder->period_insn_cnt += insn_cnt;
926 decoder->no_progress = 0;
927 decoder->pkt_state = INTEL_PT_STATE_ERR2;
928 intel_pt_log_at("ERROR: Failed to get instruction",
935 if (ip && decoder->ip == ip) {
940 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
941 intel_pt_sample_insn(decoder);
943 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
944 decoder->state.type = INTEL_PT_INSTRUCTION;
945 decoder->state.from_ip = decoder->ip;
946 decoder->state.to_ip = 0;
947 decoder->ip += intel_pt_insn->length;
948 err = INTEL_PT_RETURN;
952 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
953 /* Zero-length calls are excluded */
954 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
955 intel_pt_insn->rel) {
956 err = intel_pt_push(&decoder->stack, decoder->ip +
957 intel_pt_insn->length);
961 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
962 decoder->ret_addr = intel_pt_pop(&decoder->stack);
965 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
966 int cnt = decoder->no_progress++;
968 decoder->state.from_ip = decoder->ip;
969 decoder->ip += intel_pt_insn->length +
971 decoder->state.to_ip = decoder->ip;
972 err = INTEL_PT_RETURN;
975 * Check for being stuck in a loop. This can happen if a
976 * decoder error results in the decoder erroneously setting the
977 * ip to an address that is itself in an infinite loop that
978 * consumes no packets. When that happens, there must be an
979 * unconditional branch.
983 decoder->stuck_ip = decoder->state.to_ip;
984 decoder->stuck_ip_prd = 1;
985 decoder->stuck_ip_cnt = 1;
986 } else if (cnt > INTEL_PT_MAX_LOOPS ||
987 decoder->state.to_ip == decoder->stuck_ip) {
988 intel_pt_log_at("ERROR: Never-ending loop",
989 decoder->state.to_ip);
990 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
993 } else if (!--decoder->stuck_ip_cnt) {
994 decoder->stuck_ip_prd += 1;
995 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
996 decoder->stuck_ip = decoder->state.to_ip;
999 goto out_no_progress;
1002 decoder->no_progress = 0;
1004 decoder->state.insn_op = intel_pt_insn->op;
1005 decoder->state.insn_len = intel_pt_insn->length;
1007 if (decoder->tx_flags & INTEL_PT_IN_TX)
1008 decoder->state.flags |= INTEL_PT_IN_TX;
1013 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1015 struct intel_pt_insn intel_pt_insn;
1019 ip = decoder->last_ip;
1022 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1023 if (err == INTEL_PT_RETURN)
1025 if (err == -EAGAIN) {
1026 if (decoder->set_fup_tx_flags) {
1027 decoder->set_fup_tx_flags = false;
1028 decoder->tx_flags = decoder->fup_tx_flags;
1029 decoder->state.type = INTEL_PT_TRANSACTION;
1030 decoder->state.from_ip = decoder->ip;
1031 decoder->state.to_ip = 0;
1032 decoder->state.flags = decoder->fup_tx_flags;
1037 decoder->set_fup_tx_flags = false;
1041 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1042 intel_pt_log_at("ERROR: Unexpected indirect branch",
1044 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1048 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1049 intel_pt_log_at("ERROR: Unexpected conditional branch",
1051 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1055 intel_pt_bug(decoder);
1059 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1061 struct intel_pt_insn intel_pt_insn;
1064 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1065 if (err == INTEL_PT_RETURN &&
1067 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1068 (decoder->state.type & INTEL_PT_BRANCH) &&
1069 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1070 /* Unconditional branch leaving filter region */
1071 decoder->no_progress = 0;
1072 decoder->pge = false;
1073 decoder->continuous_period = false;
1074 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1075 decoder->state.to_ip = 0;
1078 if (err == INTEL_PT_RETURN)
1083 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1084 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1085 decoder->pge = false;
1086 decoder->continuous_period = false;
1087 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1088 decoder->state.from_ip = decoder->ip;
1089 decoder->state.to_ip = 0;
1090 if (decoder->packet.count != 0)
1091 decoder->ip = decoder->last_ip;
1093 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1094 decoder->state.from_ip = decoder->ip;
1095 if (decoder->packet.count == 0) {
1096 decoder->state.to_ip = 0;
1098 decoder->state.to_ip = decoder->last_ip;
1099 decoder->ip = decoder->last_ip;
1105 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1106 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1109 if (decoder->pgd_ip &&
1110 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1111 decoder->pgd_ip(to_ip, decoder->data)) {
1112 /* Conditional branch leaving filter region */
1113 decoder->pge = false;
1114 decoder->continuous_period = false;
1115 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1116 decoder->ip = to_ip;
1117 decoder->state.from_ip = decoder->ip;
1118 decoder->state.to_ip = 0;
1121 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1123 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1127 return intel_pt_bug(decoder);
1130 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1132 struct intel_pt_insn intel_pt_insn;
1136 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1137 if (err == INTEL_PT_RETURN)
1142 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1143 if (!decoder->return_compression) {
1144 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1146 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1149 if (!decoder->ret_addr) {
1150 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1152 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1155 if (!(decoder->tnt.payload & BIT63)) {
1156 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1158 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1161 decoder->tnt.count -= 1;
1162 if (!decoder->tnt.count)
1163 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1164 decoder->tnt.payload <<= 1;
1165 decoder->state.from_ip = decoder->ip;
1166 decoder->ip = decoder->ret_addr;
1167 decoder->state.to_ip = decoder->ip;
1171 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1172 /* Handle deferred TIPs */
1173 err = intel_pt_get_next_packet(decoder);
1176 if (decoder->packet.type != INTEL_PT_TIP ||
1177 decoder->packet.count == 0) {
1178 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1180 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1181 decoder->pkt_step = 0;
1184 intel_pt_set_last_ip(decoder);
1185 decoder->state.from_ip = decoder->ip;
1186 decoder->state.to_ip = decoder->last_ip;
1187 decoder->ip = decoder->last_ip;
1191 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1192 decoder->tnt.count -= 1;
1193 if (!decoder->tnt.count)
1194 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1195 if (decoder->tnt.payload & BIT63) {
1196 decoder->tnt.payload <<= 1;
1197 decoder->state.from_ip = decoder->ip;
1198 decoder->ip += intel_pt_insn.length +
1200 decoder->state.to_ip = decoder->ip;
1203 /* Instruction sample for a non-taken branch */
1204 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1205 decoder->tnt.payload <<= 1;
1206 decoder->state.type = INTEL_PT_INSTRUCTION;
1207 decoder->state.from_ip = decoder->ip;
1208 decoder->state.to_ip = 0;
1209 decoder->ip += intel_pt_insn.length;
1212 decoder->ip += intel_pt_insn.length;
1213 if (!decoder->tnt.count)
1215 decoder->tnt.payload <<= 1;
1219 return intel_pt_bug(decoder);
1223 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1225 unsigned int fup_tx_flags;
1228 fup_tx_flags = decoder->packet.payload &
1229 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1230 err = intel_pt_get_next_packet(decoder);
1233 if (decoder->packet.type == INTEL_PT_FUP) {
1234 decoder->fup_tx_flags = fup_tx_flags;
1235 decoder->set_fup_tx_flags = true;
1236 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1239 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1241 intel_pt_update_in_tx(decoder);
1246 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1250 decoder->have_tma = false;
1252 if (decoder->ref_timestamp) {
1253 timestamp = decoder->packet.payload |
1254 (decoder->ref_timestamp & (0xffULL << 56));
1255 if (timestamp < decoder->ref_timestamp) {
1256 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1257 timestamp += (1ULL << 56);
1259 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1260 timestamp -= (1ULL << 56);
1262 decoder->tsc_timestamp = timestamp;
1263 decoder->timestamp = timestamp;
1264 decoder->ref_timestamp = 0;
1265 decoder->timestamp_insn_cnt = 0;
1266 } else if (decoder->timestamp) {
1267 timestamp = decoder->packet.payload |
1268 (decoder->timestamp & (0xffULL << 56));
1269 decoder->tsc_timestamp = timestamp;
1270 if (timestamp < decoder->timestamp &&
1271 decoder->timestamp - timestamp < decoder->tsc_slip) {
1272 intel_pt_log_to("Suppressing backwards timestamp",
1274 timestamp = decoder->timestamp;
1276 if (timestamp < decoder->timestamp) {
1277 intel_pt_log_to("Wraparound timestamp", timestamp);
1278 timestamp += (1ULL << 56);
1279 decoder->tsc_timestamp = timestamp;
1281 decoder->timestamp = timestamp;
1282 decoder->timestamp_insn_cnt = 0;
1285 if (decoder->last_packet_type == INTEL_PT_CYC) {
1286 decoder->cyc_ref_timestamp = decoder->timestamp;
1287 decoder->cycle_cnt = 0;
1288 decoder->have_calc_cyc_to_tsc = false;
1289 intel_pt_calc_cyc_to_tsc(decoder, false);
1292 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1295 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1297 intel_pt_log("ERROR: Buffer overflow\n");
1298 intel_pt_clear_tx_flags(decoder);
1299 decoder->have_tma = false;
1301 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1302 decoder->overflow = true;
1306 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1308 uint32_t ctc = decoder->packet.payload;
1309 uint32_t fc = decoder->packet.count;
1310 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1312 if (!decoder->tsc_ctc_ratio_d)
1315 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1316 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1317 if (decoder->tsc_ctc_mult) {
1318 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1320 decoder->ctc_timestamp -= multdiv(ctc_rem,
1321 decoder->tsc_ctc_ratio_n,
1322 decoder->tsc_ctc_ratio_d);
1324 decoder->ctc_delta = 0;
1325 decoder->have_tma = true;
1326 decoder->fixup_last_mtc = true;
1327 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1328 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1331 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1334 uint32_t mtc, mtc_delta;
1336 if (!decoder->have_tma)
1339 mtc = decoder->packet.payload;
1341 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1342 decoder->fixup_last_mtc = false;
1343 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1344 &decoder->last_mtc);
1347 if (mtc > decoder->last_mtc)
1348 mtc_delta = mtc - decoder->last_mtc;
1350 mtc_delta = mtc + 256 - decoder->last_mtc;
1352 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1354 if (decoder->tsc_ctc_mult) {
1355 timestamp = decoder->ctc_timestamp +
1356 decoder->ctc_delta * decoder->tsc_ctc_mult;
1358 timestamp = decoder->ctc_timestamp +
1359 multdiv(decoder->ctc_delta,
1360 decoder->tsc_ctc_ratio_n,
1361 decoder->tsc_ctc_ratio_d);
1364 if (timestamp < decoder->timestamp)
1365 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1366 timestamp, decoder->timestamp);
1368 decoder->timestamp = timestamp;
1370 decoder->timestamp_insn_cnt = 0;
1371 decoder->last_mtc = mtc;
1373 if (decoder->last_packet_type == INTEL_PT_CYC) {
1374 decoder->cyc_ref_timestamp = decoder->timestamp;
1375 decoder->cycle_cnt = 0;
1376 decoder->have_calc_cyc_to_tsc = false;
1377 intel_pt_calc_cyc_to_tsc(decoder, true);
1381 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1383 unsigned int cbr = decoder->packet.payload;
1385 if (decoder->cbr == cbr)
1389 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1392 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1394 uint64_t timestamp = decoder->cyc_ref_timestamp;
1396 decoder->have_cyc = true;
1398 decoder->cycle_cnt += decoder->packet.payload;
1400 if (!decoder->cyc_ref_timestamp)
1403 if (decoder->have_calc_cyc_to_tsc)
1404 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1405 else if (decoder->cbr)
1406 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1410 if (timestamp < decoder->timestamp)
1411 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1412 timestamp, decoder->timestamp);
1414 decoder->timestamp = timestamp;
1416 decoder->timestamp_insn_cnt = 0;
1419 /* Walk PSB+ packets when already in sync. */
1420 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1425 err = intel_pt_get_next_packet(decoder);
1429 switch (decoder->packet.type) {
1430 case INTEL_PT_PSBEND:
1433 case INTEL_PT_TIP_PGD:
1434 case INTEL_PT_TIP_PGE:
1437 case INTEL_PT_TRACESTOP:
1440 decoder->have_tma = false;
1441 intel_pt_log("ERROR: Unexpected packet\n");
1445 return intel_pt_overflow(decoder);
1448 intel_pt_calc_tsc_timestamp(decoder);
1452 intel_pt_calc_tma(decoder);
1456 intel_pt_calc_cbr(decoder);
1459 case INTEL_PT_MODE_EXEC:
1460 decoder->exec_mode = decoder->packet.payload;
1464 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1468 decoder->pge = true;
1469 intel_pt_set_last_ip(decoder);
1472 case INTEL_PT_MODE_TSX:
1473 intel_pt_update_in_tx(decoder);
1477 intel_pt_calc_mtc_timestamp(decoder);
1478 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1479 decoder->state.type |= INTEL_PT_INSTRUCTION;
1492 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1496 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1497 decoder->tx_flags = 0;
1498 decoder->state.flags &= ~INTEL_PT_IN_TX;
1499 decoder->state.flags |= INTEL_PT_ABORT_TX;
1501 decoder->state.flags |= INTEL_PT_ASYNC;
1505 err = intel_pt_get_next_packet(decoder);
1509 switch (decoder->packet.type) {
1512 case INTEL_PT_TRACESTOP:
1517 case INTEL_PT_MODE_TSX:
1519 case INTEL_PT_PSBEND:
1520 intel_pt_log("ERROR: Missing TIP after FUP\n");
1521 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1525 return intel_pt_overflow(decoder);
1527 case INTEL_PT_TIP_PGD:
1528 decoder->state.from_ip = decoder->ip;
1529 decoder->state.to_ip = 0;
1530 if (decoder->packet.count != 0) {
1531 intel_pt_set_ip(decoder);
1532 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1535 decoder->pge = false;
1536 decoder->continuous_period = false;
1539 case INTEL_PT_TIP_PGE:
1540 decoder->pge = true;
1541 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1543 decoder->state.from_ip = 0;
1544 if (decoder->packet.count == 0) {
1545 decoder->state.to_ip = 0;
1547 intel_pt_set_ip(decoder);
1548 decoder->state.to_ip = decoder->ip;
1553 decoder->state.from_ip = decoder->ip;
1554 if (decoder->packet.count == 0) {
1555 decoder->state.to_ip = 0;
1557 intel_pt_set_ip(decoder);
1558 decoder->state.to_ip = decoder->ip;
1563 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1567 intel_pt_calc_mtc_timestamp(decoder);
1568 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1569 decoder->state.type |= INTEL_PT_INSTRUCTION;
1573 intel_pt_calc_cyc_timestamp(decoder);
1576 case INTEL_PT_MODE_EXEC:
1577 decoder->exec_mode = decoder->packet.payload;
1586 return intel_pt_bug(decoder);
1591 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1593 bool no_tip = false;
1597 err = intel_pt_get_next_packet(decoder);
1601 switch (decoder->packet.type) {
1603 if (!decoder->packet.count)
1605 decoder->tnt = decoder->packet;
1606 decoder->pkt_state = INTEL_PT_STATE_TNT;
1607 err = intel_pt_walk_tnt(decoder);
1612 case INTEL_PT_TIP_PGD:
1613 if (decoder->packet.count != 0)
1614 intel_pt_set_last_ip(decoder);
1615 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1616 return intel_pt_walk_tip(decoder);
1618 case INTEL_PT_TIP_PGE: {
1619 decoder->pge = true;
1620 if (decoder->packet.count == 0) {
1621 intel_pt_log_at("Skipping zero TIP.PGE",
1625 intel_pt_set_ip(decoder);
1626 decoder->state.from_ip = 0;
1627 decoder->state.to_ip = decoder->ip;
1632 return intel_pt_overflow(decoder);
1635 if (decoder->packet.count != 0)
1636 intel_pt_set_last_ip(decoder);
1637 decoder->pkt_state = INTEL_PT_STATE_TIP;
1638 return intel_pt_walk_tip(decoder);
1641 if (decoder->packet.count == 0) {
1642 intel_pt_log_at("Skipping zero FUP",
1647 intel_pt_set_last_ip(decoder);
1648 err = intel_pt_walk_fup(decoder);
1649 if (err != -EAGAIN) {
1653 decoder->pkt_state =
1654 INTEL_PT_STATE_FUP_NO_TIP;
1656 decoder->pkt_state = INTEL_PT_STATE_FUP;
1663 return intel_pt_walk_fup_tip(decoder);
1665 case INTEL_PT_TRACESTOP:
1666 decoder->pge = false;
1667 decoder->continuous_period = false;
1668 intel_pt_clear_tx_flags(decoder);
1669 decoder->have_tma = false;
1673 intel_pt_clear_stack(&decoder->stack);
1674 err = intel_pt_walk_psbend(decoder);
1682 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1686 intel_pt_calc_mtc_timestamp(decoder);
1687 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1690 * Ensure that there has been an instruction since the
1693 if (!decoder->mtc_insn)
1695 decoder->mtc_insn = false;
1696 /* Ensure that there is a timestamp */
1697 if (!decoder->timestamp)
1699 decoder->state.type = INTEL_PT_INSTRUCTION;
1700 decoder->state.from_ip = decoder->ip;
1701 decoder->state.to_ip = 0;
1702 decoder->mtc_insn = false;
1706 intel_pt_calc_tsc_timestamp(decoder);
1710 intel_pt_calc_tma(decoder);
1714 intel_pt_calc_cyc_timestamp(decoder);
1718 intel_pt_calc_cbr(decoder);
1721 case INTEL_PT_MODE_EXEC:
1722 decoder->exec_mode = decoder->packet.payload;
1725 case INTEL_PT_MODE_TSX:
1726 /* MODE_TSX need not be followed by FUP */
1727 if (!decoder->pge) {
1728 intel_pt_update_in_tx(decoder);
1731 err = intel_pt_mode_tsx(decoder, &no_tip);
1736 case INTEL_PT_BAD: /* Does not happen */
1737 return intel_pt_bug(decoder);
1739 case INTEL_PT_PSBEND:
1746 return intel_pt_bug(decoder);
1751 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1753 return decoder->last_ip || decoder->packet.count == 0 ||
1754 decoder->packet.count == 3 || decoder->packet.count == 6;
1757 /* Walk PSB+ packets to get in sync. */
1758 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1763 err = intel_pt_get_next_packet(decoder);
1767 switch (decoder->packet.type) {
1768 case INTEL_PT_TIP_PGD:
1769 decoder->continuous_period = false;
1771 case INTEL_PT_TIP_PGE:
1773 intel_pt_log("ERROR: Unexpected packet\n");
1777 decoder->pge = true;
1778 if (intel_pt_have_ip(decoder)) {
1779 uint64_t current_ip = decoder->ip;
1781 intel_pt_set_ip(decoder);
1783 intel_pt_log_to("Setting IP",
1789 intel_pt_calc_mtc_timestamp(decoder);
1793 intel_pt_calc_tsc_timestamp(decoder);
1797 intel_pt_calc_tma(decoder);
1801 intel_pt_calc_cyc_timestamp(decoder);
1805 intel_pt_calc_cbr(decoder);
1809 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1812 case INTEL_PT_MODE_EXEC:
1813 decoder->exec_mode = decoder->packet.payload;
1816 case INTEL_PT_MODE_TSX:
1817 intel_pt_update_in_tx(decoder);
1820 case INTEL_PT_TRACESTOP:
1821 decoder->pge = false;
1822 decoder->continuous_period = false;
1823 intel_pt_clear_tx_flags(decoder);
1827 decoder->have_tma = false;
1828 intel_pt_log("ERROR: Unexpected packet\n");
1830 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1832 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1835 case INTEL_PT_BAD: /* Does not happen */
1836 return intel_pt_bug(decoder);
1839 return intel_pt_overflow(decoder);
1841 case INTEL_PT_PSBEND:
1854 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1859 err = intel_pt_get_next_packet(decoder);
1863 switch (decoder->packet.type) {
1864 case INTEL_PT_TIP_PGD:
1865 decoder->continuous_period = false;
1867 case INTEL_PT_TIP_PGE:
1869 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1870 if (intel_pt_have_ip(decoder))
1871 intel_pt_set_ip(decoder);
1877 if (decoder->overflow) {
1878 if (intel_pt_have_ip(decoder))
1879 intel_pt_set_ip(decoder);
1883 if (decoder->packet.count)
1884 intel_pt_set_last_ip(decoder);
1888 intel_pt_calc_mtc_timestamp(decoder);
1892 intel_pt_calc_tsc_timestamp(decoder);
1896 intel_pt_calc_tma(decoder);
1900 intel_pt_calc_cyc_timestamp(decoder);
1904 intel_pt_calc_cbr(decoder);
1908 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1911 case INTEL_PT_MODE_EXEC:
1912 decoder->exec_mode = decoder->packet.payload;
1915 case INTEL_PT_MODE_TSX:
1916 intel_pt_update_in_tx(decoder);
1920 return intel_pt_overflow(decoder);
1922 case INTEL_PT_BAD: /* Does not happen */
1923 return intel_pt_bug(decoder);
1925 case INTEL_PT_TRACESTOP:
1926 decoder->pge = false;
1927 decoder->continuous_period = false;
1928 intel_pt_clear_tx_flags(decoder);
1929 decoder->have_tma = false;
1933 intel_pt_clear_stack(&decoder->stack);
1934 err = intel_pt_walk_psb(decoder);
1938 /* Do not have a sample */
1939 decoder->state.type = 0;
1945 case INTEL_PT_PSBEND:
1955 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1959 intel_pt_log("Scanning for full IP\n");
1960 err = intel_pt_walk_to_ip(decoder);
1964 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1965 decoder->overflow = false;
1967 decoder->state.from_ip = 0;
1968 decoder->state.to_ip = decoder->ip;
1969 intel_pt_log_to("Setting IP", decoder->ip);
1974 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1976 const unsigned char *end = decoder->buf + decoder->len;
1979 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1980 if (i > decoder->len)
1982 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1988 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1990 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1991 const char *psb = INTEL_PT_PSB_STR;
1993 if (rest_psb > decoder->len ||
1994 memcmp(decoder->buf, psb + part_psb, rest_psb))
2000 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2005 decoder->pos += decoder->len;
2008 ret = intel_pt_get_next_data(decoder);
2012 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2016 decoder->pos -= part_psb;
2017 decoder->next_buf = decoder->buf + rest_psb;
2018 decoder->next_len = decoder->len - rest_psb;
2019 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2020 decoder->buf = decoder->temp_buf;
2021 decoder->len = INTEL_PT_PSB_LEN;
2026 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2028 unsigned char *next;
2031 intel_pt_log("Scanning for PSB\n");
2033 if (!decoder->len) {
2034 ret = intel_pt_get_next_data(decoder);
2039 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2044 part_psb = intel_pt_part_psb(decoder);
2046 ret = intel_pt_get_split_psb(decoder, part_psb);
2050 decoder->pos += decoder->len;
2056 decoder->pkt_step = next - decoder->buf;
2057 return intel_pt_get_next_packet(decoder);
2061 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2065 decoder->pge = false;
2066 decoder->continuous_period = false;
2067 decoder->last_ip = 0;
2069 intel_pt_clear_stack(&decoder->stack);
2071 err = intel_pt_scan_for_psb(decoder);
2075 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2077 err = intel_pt_walk_psb(decoder);
2082 decoder->state.type = 0; /* Do not have a sample */
2083 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2085 return intel_pt_sync_ip(decoder);
2091 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2093 uint64_t est = decoder->sample_insn_cnt << 1;
2095 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2098 est *= decoder->max_non_turbo_ratio;
2099 est /= decoder->cbr;
2101 return decoder->sample_timestamp + est;
2104 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2109 decoder->state.type = INTEL_PT_BRANCH;
2110 decoder->state.flags = 0;
2112 switch (decoder->pkt_state) {
2113 case INTEL_PT_STATE_NO_PSB:
2114 err = intel_pt_sync(decoder);
2116 case INTEL_PT_STATE_NO_IP:
2117 decoder->last_ip = 0;
2119 case INTEL_PT_STATE_ERR_RESYNC:
2120 err = intel_pt_sync_ip(decoder);
2122 case INTEL_PT_STATE_IN_SYNC:
2123 err = intel_pt_walk_trace(decoder);
2125 case INTEL_PT_STATE_TNT:
2126 err = intel_pt_walk_tnt(decoder);
2128 err = intel_pt_walk_trace(decoder);
2130 case INTEL_PT_STATE_TIP:
2131 case INTEL_PT_STATE_TIP_PGD:
2132 err = intel_pt_walk_tip(decoder);
2134 case INTEL_PT_STATE_FUP:
2135 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2136 err = intel_pt_walk_fup(decoder);
2138 err = intel_pt_walk_fup_tip(decoder);
2140 decoder->pkt_state = INTEL_PT_STATE_FUP;
2142 case INTEL_PT_STATE_FUP_NO_TIP:
2143 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2144 err = intel_pt_walk_fup(decoder);
2146 err = intel_pt_walk_trace(decoder);
2149 err = intel_pt_bug(decoder);
2152 } while (err == -ENOLINK);
2155 decoder->state.err = intel_pt_ext_err(err);
2156 decoder->state.from_ip = decoder->ip;
2157 decoder->sample_timestamp = decoder->timestamp;
2158 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2160 decoder->state.err = 0;
2161 if (intel_pt_sample_time(decoder->pkt_state)) {
2162 decoder->sample_timestamp = decoder->timestamp;
2163 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2167 decoder->state.timestamp = decoder->sample_timestamp;
2168 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2169 decoder->state.cr3 = decoder->cr3;
2170 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2172 return &decoder->state;
2175 static bool intel_pt_at_psb(unsigned char *buf, size_t len)
2177 if (len < INTEL_PT_PSB_LEN)
2179 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
2184 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2185 * @buf: pointer to buffer pointer
2186 * @len: size of buffer
2188 * Updates the buffer pointer to point to the start of the next PSB packet if
2189 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2190 * @len is adjusted accordingly.
2192 * Return: %true if a PSB packet is found, %false otherwise.
2194 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2196 unsigned char *next;
2198 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2200 *len -= next - *buf;
2208 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2210 * @buf: pointer to buffer pointer
2211 * @len: size of buffer
2213 * Updates the buffer pointer to point to the start of the following PSB packet
2214 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2215 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2217 * Return: %true if a PSB packet is found, %false otherwise.
2219 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2221 unsigned char *next;
2226 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2228 *len -= next - *buf;
2236 * intel_pt_last_psb - find the last PSB packet in a buffer.
2238 * @len: size of buffer
2240 * This function finds the last PSB in a buffer.
2242 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2244 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2246 const char *n = INTEL_PT_PSB_STR;
2250 if (len < INTEL_PT_PSB_LEN)
2253 k = len - INTEL_PT_PSB_LEN + 1;
2255 p = memrchr(buf, n[0], k);
2258 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2267 * intel_pt_next_tsc - find and return next TSC.
2269 * @len: size of buffer
2270 * @tsc: TSC value returned
2272 * Find a TSC packet in @buf and return the TSC value. This function assumes
2273 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2274 * PSBEND packet is found.
2276 * Return: %true if TSC is found, false otherwise.
2278 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
2280 struct intel_pt_pkt packet;
2284 ret = intel_pt_get_packet(buf, len, &packet);
2287 if (packet.type == INTEL_PT_TSC) {
2288 *tsc = packet.payload;
2291 if (packet.type == INTEL_PT_PSBEND)
2300 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2301 * @tsc1: first TSC to compare
2302 * @tsc2: second TSC to compare
2304 * This function compares 7-byte TSC values allowing for the possibility that
2305 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2306 * around so for that purpose this function assumes the absolute difference is
2307 * less than half the maximum difference.
2309 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2312 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2314 const uint64_t halfway = (1ULL << 55);
2320 if (tsc2 - tsc1 < halfway)
2325 if (tsc1 - tsc2 < halfway)
2333 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2335 * @buf_a: first buffer
2336 * @len_a: size of first buffer
2337 * @buf_b: second buffer
2338 * @len_b: size of second buffer
2340 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2341 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2342 * walk forward in @buf_b until a later TSC is found. A precondition is that
2343 * @buf_a and @buf_b are positioned at a PSB.
2345 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2346 * @buf_b + @len_b if there is no non-overlapped data.
2348 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2350 unsigned char *buf_b,
2353 uint64_t tsc_a, tsc_b;
2357 p = intel_pt_last_psb(buf_a, len_a);
2359 return buf_b; /* No PSB in buf_a => no overlap */
2361 len = len_a - (p - buf_a);
2362 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
2363 /* The last PSB+ in buf_a is incomplete, so go back one more */
2365 p = intel_pt_last_psb(buf_a, len_a);
2367 return buf_b; /* No full PSB+ => assume no overlap */
2368 len = len_a - (p - buf_a);
2369 if (!intel_pt_next_tsc(p, len, &tsc_a))
2370 return buf_b; /* No TSC in buf_a => assume no overlap */
2374 /* Ignore PSB+ with no TSC */
2375 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
2376 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
2377 return buf_b; /* tsc_a < tsc_b => no overlap */
2379 if (!intel_pt_step_psb(&buf_b, &len_b))
2380 return buf_b + len_b; /* No PSB in buf_b => no data */
2385 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2386 * @buf_a: first buffer
2387 * @len_a: size of first buffer
2388 * @buf_b: second buffer
2389 * @len_b: size of second buffer
2390 * @have_tsc: can use TSC packets to detect overlap
2392 * When trace samples or snapshots are recorded there is the possibility that
2393 * the data overlaps. Note that, for the purposes of decoding, data is only
2394 * useful if it begins with a PSB packet.
2396 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2397 * @buf_b + @len_b if there is no non-overlapped data.
2399 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2400 unsigned char *buf_b, size_t len_b,
2403 unsigned char *found;
2405 /* Buffer 'b' must start at PSB so throw away everything before that */
2406 if (!intel_pt_next_psb(&buf_b, &len_b))
2407 return buf_b + len_b; /* No PSB */
2409 if (!intel_pt_next_psb(&buf_a, &len_a))
2410 return buf_b; /* No overlap */
2413 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
2419 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2420 * we can ignore the first part of buffer 'a'.
2422 while (len_b < len_a) {
2423 if (!intel_pt_step_psb(&buf_a, &len_a))
2424 return buf_b; /* No overlap */
2427 /* Now len_b >= len_a */
2428 if (len_b > len_a) {
2429 /* The leftover buffer 'b' must start at a PSB */
2430 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2431 if (!intel_pt_step_psb(&buf_a, &len_a))
2432 return buf_b; /* No overlap */
2437 /* Potential overlap so check the bytes */
2438 found = memmem(buf_a, len_a, buf_b, len_a);
2440 return buf_b + len_a;
2442 /* Try again at next PSB in buffer 'a' */
2443 if (!intel_pt_step_psb(&buf_a, &len_a))
2444 return buf_b; /* No overlap */
2446 /* The leftover buffer 'b' must start at a PSB */
2447 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2448 if (!intel_pt_step_psb(&buf_a, &len_a))
2449 return buf_b; /* No overlap */