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
29 #include "intel-pt-insn-decoder.h"
30 #include "intel-pt-pkt-decoder.h"
31 #include "intel-pt-decoder.h"
32 #include "intel-pt-log.h"
34 #define INTEL_PT_BLK_SIZE 1024
36 #define BIT63 (((uint64_t)1 << 63))
38 #define INTEL_PT_RETURN 1
40 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
41 #define INTEL_PT_MAX_LOOPS 10000
44 struct intel_pt_blk *prev;
45 uint64_t ip[INTEL_PT_BLK_SIZE];
48 struct intel_pt_stack {
49 struct intel_pt_blk *blk;
50 struct intel_pt_blk *spare;
54 enum intel_pt_pkt_state {
55 INTEL_PT_STATE_NO_PSB,
57 INTEL_PT_STATE_ERR_RESYNC,
58 INTEL_PT_STATE_IN_SYNC,
61 INTEL_PT_STATE_TIP_PGD,
63 INTEL_PT_STATE_FUP_NO_TIP,
66 #ifdef INTEL_PT_STRICT
67 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
68 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
69 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
70 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
72 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
73 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
74 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
75 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
78 struct intel_pt_decoder {
79 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
80 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
81 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
82 uint64_t max_insn_cnt, void *data);
83 bool (*pgd_ip)(uint64_t ip, void *data);
85 struct intel_pt_state state;
86 const unsigned char *buf;
88 bool return_compression;
99 uint64_t tsc_timestamp;
100 uint64_t ref_timestamp;
102 uint64_t ctc_timestamp;
105 uint64_t cyc_ref_timestamp;
107 uint32_t tsc_ctc_ratio_n;
108 uint32_t tsc_ctc_ratio_d;
109 uint32_t tsc_ctc_mult;
111 uint32_t ctc_rem_mask;
113 struct intel_pt_stack stack;
114 enum intel_pt_pkt_state pkt_state;
115 struct intel_pt_pkt packet;
116 struct intel_pt_pkt tnt;
119 int last_packet_type;
121 unsigned int max_non_turbo_ratio;
122 double max_non_turbo_ratio_fp;
123 double cbr_cyc_to_tsc;
124 double calc_cyc_to_tsc;
125 bool have_calc_cyc_to_tsc;
127 unsigned int insn_bytes;
129 enum intel_pt_period_type period_type;
130 uint64_t tot_insn_cnt;
131 uint64_t period_insn_cnt;
132 uint64_t period_mask;
133 uint64_t period_ticks;
134 uint64_t last_masked_timestamp;
135 bool continuous_period;
137 bool set_fup_tx_flags;
138 unsigned int fup_tx_flags;
139 unsigned int tx_flags;
140 uint64_t timestamp_insn_cnt;
145 const unsigned char *next_buf;
147 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
150 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
154 for (i = 0; x != 1; i++)
160 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
162 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
165 period = intel_pt_lower_power_of_2(decoder->period);
166 decoder->period_mask = ~(period - 1);
167 decoder->period_ticks = period;
171 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
175 return (t / d) * n + ((t % d) * n) / d;
178 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
180 struct intel_pt_decoder *decoder;
182 if (!params->get_trace || !params->walk_insn)
185 decoder = zalloc(sizeof(struct intel_pt_decoder));
189 decoder->get_trace = params->get_trace;
190 decoder->walk_insn = params->walk_insn;
191 decoder->pgd_ip = params->pgd_ip;
192 decoder->data = params->data;
193 decoder->return_compression = params->return_compression;
195 decoder->period = params->period;
196 decoder->period_type = params->period_type;
198 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
199 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
201 intel_pt_setup_period(decoder);
203 decoder->mtc_shift = params->mtc_period;
204 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
206 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
207 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
209 if (!decoder->tsc_ctc_ratio_n)
210 decoder->tsc_ctc_ratio_d = 0;
212 if (decoder->tsc_ctc_ratio_d) {
213 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
214 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
215 decoder->tsc_ctc_ratio_d;
218 * Allow for timestamps appearing to backwards because a TSC
219 * packet has slipped past a MTC packet, so allow 2 MTC ticks
222 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
223 decoder->tsc_ctc_ratio_n,
224 decoder->tsc_ctc_ratio_d);
226 /* ... or 0x100 paranoia */
227 if (decoder->tsc_slip < 0x100)
228 decoder->tsc_slip = 0x100;
230 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
231 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
232 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
233 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
234 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
239 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
241 struct intel_pt_blk *blk = stack->blk;
243 stack->blk = blk->prev;
250 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
255 intel_pt_pop_blk(stack);
258 stack->pos = INTEL_PT_BLK_SIZE;
260 return stack->blk->ip[--stack->pos];
263 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
265 struct intel_pt_blk *blk;
271 blk = malloc(sizeof(struct intel_pt_blk));
276 blk->prev = stack->blk;
282 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
286 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
287 err = intel_pt_alloc_blk(stack);
292 stack->blk->ip[stack->pos++] = ip;
296 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
299 intel_pt_pop_blk(stack);
303 static void intel_pt_free_stack(struct intel_pt_stack *stack)
305 intel_pt_clear_stack(stack);
307 zfree(&stack->spare);
310 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
312 intel_pt_free_stack(&decoder->stack);
316 static int intel_pt_ext_err(int code)
320 return INTEL_PT_ERR_NOMEM;
322 return INTEL_PT_ERR_INTERN;
324 return INTEL_PT_ERR_BADPKT;
326 return INTEL_PT_ERR_NODATA;
328 return INTEL_PT_ERR_NOINSN;
330 return INTEL_PT_ERR_MISMAT;
332 return INTEL_PT_ERR_OVR;
334 return INTEL_PT_ERR_LOST;
336 return INTEL_PT_ERR_NELOOP;
338 return INTEL_PT_ERR_UNK;
342 static const char *intel_pt_err_msgs[] = {
343 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
344 [INTEL_PT_ERR_INTERN] = "Internal error",
345 [INTEL_PT_ERR_BADPKT] = "Bad packet",
346 [INTEL_PT_ERR_NODATA] = "No more data",
347 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
348 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
349 [INTEL_PT_ERR_OVR] = "Overflow packet",
350 [INTEL_PT_ERR_LOST] = "Lost trace data",
351 [INTEL_PT_ERR_UNK] = "Unknown error!",
352 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
355 int intel_pt__strerror(int code, char *buf, size_t buflen)
357 if (code < 1 || code >= INTEL_PT_ERR_MAX)
358 code = INTEL_PT_ERR_UNK;
359 strlcpy(buf, intel_pt_err_msgs[code], buflen);
363 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
368 switch (packet->count) {
370 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
374 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
378 ip = packet->payload;
379 /* Sign-extend 6-byte ip */
380 if (ip & (uint64_t)0x800000000000ULL)
381 ip |= (uint64_t)0xffff000000000000ULL;
384 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
388 ip = packet->payload;
397 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
399 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
402 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
404 intel_pt_set_last_ip(decoder);
405 decoder->ip = decoder->last_ip;
408 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
410 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
414 static int intel_pt_bug(struct intel_pt_decoder *decoder)
416 intel_pt_log("ERROR: Internal error\n");
417 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
421 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
423 decoder->tx_flags = 0;
426 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
428 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
431 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
433 intel_pt_clear_tx_flags(decoder);
434 decoder->have_tma = false;
435 decoder->pkt_len = 1;
436 decoder->pkt_step = 1;
437 intel_pt_decoder_log_packet(decoder);
438 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
439 intel_pt_log("ERROR: Bad packet\n");
440 decoder->pkt_state = INTEL_PT_STATE_ERR1;
445 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
447 struct intel_pt_buffer buffer = { .buf = 0, };
450 decoder->pkt_step = 0;
452 intel_pt_log("Getting more data\n");
453 ret = decoder->get_trace(&buffer, decoder->data);
456 decoder->buf = buffer.buf;
457 decoder->len = buffer.len;
459 intel_pt_log("No more data\n");
462 if (!buffer.consecutive) {
464 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
465 decoder->ref_timestamp = buffer.ref_timestamp;
466 decoder->timestamp = 0;
467 decoder->have_tma = false;
468 decoder->state.trace_nr = buffer.trace_nr;
469 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
470 decoder->ref_timestamp);
477 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
479 if (!decoder->next_buf)
480 return intel_pt_get_data(decoder);
482 decoder->buf = decoder->next_buf;
483 decoder->len = decoder->next_len;
484 decoder->next_buf = 0;
485 decoder->next_len = 0;
489 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
491 unsigned char *buf = decoder->temp_buf;
492 size_t old_len, len, n;
495 old_len = decoder->len;
497 memcpy(buf, decoder->buf, len);
499 ret = intel_pt_get_data(decoder);
501 decoder->pos += old_len;
502 return ret < 0 ? ret : -EINVAL;
505 n = INTEL_PT_PKT_MAX_SZ - len;
506 if (n > decoder->len)
508 memcpy(buf + len, decoder->buf, n);
511 ret = intel_pt_get_packet(buf, len, &decoder->packet);
512 if (ret < (int)old_len) {
513 decoder->next_buf = decoder->buf;
514 decoder->next_len = decoder->len;
516 decoder->len = old_len;
517 return intel_pt_bad_packet(decoder);
520 decoder->next_buf = decoder->buf + (ret - old_len);
521 decoder->next_len = decoder->len - (ret - old_len);
529 struct intel_pt_pkt_info {
530 struct intel_pt_decoder *decoder;
531 struct intel_pt_pkt packet;
534 int last_packet_type;
538 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
540 /* Lookahead packets in current buffer */
541 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
542 intel_pt_pkt_cb_t cb, void *data)
544 struct intel_pt_pkt_info pkt_info;
545 const unsigned char *buf = decoder->buf;
546 size_t len = decoder->len;
549 pkt_info.decoder = decoder;
550 pkt_info.pos = decoder->pos;
551 pkt_info.pkt_len = decoder->pkt_step;
552 pkt_info.last_packet_type = decoder->last_packet_type;
553 pkt_info.data = data;
557 pkt_info.pos += pkt_info.pkt_len;
558 buf += pkt_info.pkt_len;
559 len -= pkt_info.pkt_len;
562 return INTEL_PT_NEED_MORE_BYTES;
564 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
566 return INTEL_PT_NEED_MORE_BYTES;
570 pkt_info.pkt_len = ret;
571 } while (pkt_info.packet.type == INTEL_PT_PAD);
577 pkt_info.last_packet_type = pkt_info.packet.type;
581 struct intel_pt_calc_cyc_to_tsc_info {
585 uint64_t ctc_timestamp;
587 uint64_t tsc_timestamp;
592 double cbr_cyc_to_tsc;
596 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
597 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
598 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
599 * packet by copying the missing bits from the current MTC assuming the least
600 * difference between the two, and that the current MTC comes after last_mtc.
602 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
605 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
606 uint32_t mask = ~(first_missing_bit - 1);
608 *last_mtc |= mtc & mask;
609 if (*last_mtc >= mtc) {
610 *last_mtc -= first_missing_bit;
615 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
617 struct intel_pt_decoder *decoder = pkt_info->decoder;
618 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
622 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
624 switch (pkt_info->packet.type) {
626 case INTEL_PT_TIP_PGE:
631 case INTEL_PT_MODE_EXEC:
632 case INTEL_PT_MODE_TSX:
633 case INTEL_PT_PSBEND:
643 mtc = pkt_info->packet.payload;
644 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
645 data->fixup_last_mtc = false;
646 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
649 if (mtc > data->last_mtc)
650 mtc_delta = mtc - data->last_mtc;
652 mtc_delta = mtc + 256 - data->last_mtc;
653 data->ctc_delta += mtc_delta << decoder->mtc_shift;
654 data->last_mtc = mtc;
656 if (decoder->tsc_ctc_mult) {
657 timestamp = data->ctc_timestamp +
658 data->ctc_delta * decoder->tsc_ctc_mult;
660 timestamp = data->ctc_timestamp +
661 multdiv(data->ctc_delta,
662 decoder->tsc_ctc_ratio_n,
663 decoder->tsc_ctc_ratio_d);
666 if (timestamp < data->timestamp)
669 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
670 data->timestamp = timestamp;
677 timestamp = pkt_info->packet.payload |
678 (data->timestamp & (0xffULL << 56));
679 if (data->from_mtc && timestamp < data->timestamp &&
680 data->timestamp - timestamp < decoder->tsc_slip)
682 if (timestamp < data->timestamp)
683 timestamp += (1ULL << 56);
684 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
687 data->tsc_timestamp = timestamp;
688 data->timestamp = timestamp;
697 if (!decoder->tsc_ctc_ratio_d)
700 ctc = pkt_info->packet.payload;
701 fc = pkt_info->packet.count;
702 ctc_rem = ctc & decoder->ctc_rem_mask;
704 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
706 data->ctc_timestamp = data->tsc_timestamp - fc;
707 if (decoder->tsc_ctc_mult) {
708 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
710 data->ctc_timestamp -=
711 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
712 decoder->tsc_ctc_ratio_d);
716 data->have_tma = true;
717 data->fixup_last_mtc = true;
722 data->cycle_cnt += pkt_info->packet.payload;
726 cbr = pkt_info->packet.payload;
727 if (data->cbr && data->cbr != cbr)
730 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
733 case INTEL_PT_TIP_PGD:
734 case INTEL_PT_TRACESTOP:
736 case INTEL_PT_BAD: /* Does not happen */
741 if (!data->cbr && decoder->cbr) {
742 data->cbr = decoder->cbr;
743 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
746 if (!data->cycle_cnt)
749 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
751 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
752 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
753 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
754 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
758 decoder->calc_cyc_to_tsc = cyc_to_tsc;
759 decoder->have_calc_cyc_to_tsc = true;
762 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
763 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
765 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
766 cyc_to_tsc, pkt_info->pos);
772 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
775 struct intel_pt_calc_cyc_to_tsc_info data = {
778 .last_mtc = decoder->last_mtc,
779 .ctc_timestamp = decoder->ctc_timestamp,
780 .ctc_delta = decoder->ctc_delta,
781 .tsc_timestamp = decoder->tsc_timestamp,
782 .timestamp = decoder->timestamp,
783 .have_tma = decoder->have_tma,
784 .fixup_last_mtc = decoder->fixup_last_mtc,
785 .from_mtc = from_mtc,
789 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
792 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
796 decoder->last_packet_type = decoder->packet.type;
799 decoder->pos += decoder->pkt_step;
800 decoder->buf += decoder->pkt_step;
801 decoder->len -= decoder->pkt_step;
804 ret = intel_pt_get_next_data(decoder);
809 ret = intel_pt_get_packet(decoder->buf, decoder->len,
811 if (ret == INTEL_PT_NEED_MORE_BYTES &&
812 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
813 ret = intel_pt_get_split_packet(decoder);
818 return intel_pt_bad_packet(decoder);
820 decoder->pkt_len = ret;
821 decoder->pkt_step = ret;
822 intel_pt_decoder_log_packet(decoder);
823 } while (decoder->packet.type == INTEL_PT_PAD);
828 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
830 uint64_t timestamp, masked_timestamp;
832 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
833 masked_timestamp = timestamp & decoder->period_mask;
834 if (decoder->continuous_period) {
835 if (masked_timestamp != decoder->last_masked_timestamp)
839 masked_timestamp = timestamp & decoder->period_mask;
840 if (masked_timestamp != decoder->last_masked_timestamp) {
841 decoder->last_masked_timestamp = masked_timestamp;
842 decoder->continuous_period = true;
845 return decoder->period_ticks - (timestamp - masked_timestamp);
848 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
850 switch (decoder->period_type) {
851 case INTEL_PT_PERIOD_INSTRUCTIONS:
852 return decoder->period - decoder->period_insn_cnt;
853 case INTEL_PT_PERIOD_TICKS:
854 return intel_pt_next_period(decoder);
855 case INTEL_PT_PERIOD_NONE:
856 case INTEL_PT_PERIOD_MTC:
862 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
864 uint64_t timestamp, masked_timestamp;
866 switch (decoder->period_type) {
867 case INTEL_PT_PERIOD_INSTRUCTIONS:
868 decoder->period_insn_cnt = 0;
870 case INTEL_PT_PERIOD_TICKS:
871 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
872 masked_timestamp = timestamp & decoder->period_mask;
873 decoder->last_masked_timestamp = masked_timestamp;
875 case INTEL_PT_PERIOD_NONE:
876 case INTEL_PT_PERIOD_MTC:
881 decoder->state.type |= INTEL_PT_INSTRUCTION;
884 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
885 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
887 uint64_t max_insn_cnt, insn_cnt = 0;
890 if (!decoder->mtc_insn)
891 decoder->mtc_insn = true;
893 max_insn_cnt = intel_pt_next_sample(decoder);
895 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
896 max_insn_cnt, decoder->data);
898 decoder->tot_insn_cnt += insn_cnt;
899 decoder->timestamp_insn_cnt += insn_cnt;
900 decoder->period_insn_cnt += insn_cnt;
903 decoder->no_progress = 0;
904 decoder->pkt_state = INTEL_PT_STATE_ERR2;
905 intel_pt_log_at("ERROR: Failed to get instruction",
912 if (ip && decoder->ip == ip) {
917 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
918 intel_pt_sample_insn(decoder);
920 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
921 decoder->state.type = INTEL_PT_INSTRUCTION;
922 decoder->state.from_ip = decoder->ip;
923 decoder->state.to_ip = 0;
924 decoder->ip += intel_pt_insn->length;
925 err = INTEL_PT_RETURN;
929 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
930 /* Zero-length calls are excluded */
931 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
932 intel_pt_insn->rel) {
933 err = intel_pt_push(&decoder->stack, decoder->ip +
934 intel_pt_insn->length);
938 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
939 decoder->ret_addr = intel_pt_pop(&decoder->stack);
942 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
943 int cnt = decoder->no_progress++;
945 decoder->state.from_ip = decoder->ip;
946 decoder->ip += intel_pt_insn->length +
948 decoder->state.to_ip = decoder->ip;
949 err = INTEL_PT_RETURN;
952 * Check for being stuck in a loop. This can happen if a
953 * decoder error results in the decoder erroneously setting the
954 * ip to an address that is itself in an infinite loop that
955 * consumes no packets. When that happens, there must be an
956 * unconditional branch.
960 decoder->stuck_ip = decoder->state.to_ip;
961 decoder->stuck_ip_prd = 1;
962 decoder->stuck_ip_cnt = 1;
963 } else if (cnt > INTEL_PT_MAX_LOOPS ||
964 decoder->state.to_ip == decoder->stuck_ip) {
965 intel_pt_log_at("ERROR: Never-ending loop",
966 decoder->state.to_ip);
967 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
970 } else if (!--decoder->stuck_ip_cnt) {
971 decoder->stuck_ip_prd += 1;
972 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
973 decoder->stuck_ip = decoder->state.to_ip;
976 goto out_no_progress;
979 decoder->no_progress = 0;
981 decoder->state.insn_op = intel_pt_insn->op;
982 decoder->state.insn_len = intel_pt_insn->length;
984 if (decoder->tx_flags & INTEL_PT_IN_TX)
985 decoder->state.flags |= INTEL_PT_IN_TX;
990 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
992 struct intel_pt_insn intel_pt_insn;
996 ip = decoder->last_ip;
999 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1000 if (err == INTEL_PT_RETURN)
1002 if (err == -EAGAIN) {
1003 if (decoder->set_fup_tx_flags) {
1004 decoder->set_fup_tx_flags = false;
1005 decoder->tx_flags = decoder->fup_tx_flags;
1006 decoder->state.type = INTEL_PT_TRANSACTION;
1007 decoder->state.from_ip = decoder->ip;
1008 decoder->state.to_ip = 0;
1009 decoder->state.flags = decoder->fup_tx_flags;
1014 decoder->set_fup_tx_flags = false;
1018 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1019 intel_pt_log_at("ERROR: Unexpected indirect branch",
1021 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1025 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1026 intel_pt_log_at("ERROR: Unexpected conditional branch",
1028 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1032 intel_pt_bug(decoder);
1036 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1038 struct intel_pt_insn intel_pt_insn;
1041 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1042 if (err == INTEL_PT_RETURN &&
1044 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1045 (decoder->state.type & INTEL_PT_BRANCH) &&
1046 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1047 /* Unconditional branch leaving filter region */
1048 decoder->no_progress = 0;
1049 decoder->pge = false;
1050 decoder->continuous_period = false;
1051 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1052 decoder->state.to_ip = 0;
1055 if (err == INTEL_PT_RETURN)
1060 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1061 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1062 decoder->pge = false;
1063 decoder->continuous_period = false;
1064 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1065 decoder->state.from_ip = decoder->ip;
1066 decoder->state.to_ip = 0;
1067 if (decoder->packet.count != 0)
1068 decoder->ip = decoder->last_ip;
1070 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1071 decoder->state.from_ip = decoder->ip;
1072 if (decoder->packet.count == 0) {
1073 decoder->state.to_ip = 0;
1075 decoder->state.to_ip = decoder->last_ip;
1076 decoder->ip = decoder->last_ip;
1082 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1083 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1086 if (decoder->pgd_ip &&
1087 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1088 decoder->pgd_ip(to_ip, decoder->data)) {
1089 /* Conditional branch leaving filter region */
1090 decoder->pge = false;
1091 decoder->continuous_period = false;
1092 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1093 decoder->ip = to_ip;
1094 decoder->state.from_ip = decoder->ip;
1095 decoder->state.to_ip = 0;
1098 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1100 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1104 return intel_pt_bug(decoder);
1107 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1109 struct intel_pt_insn intel_pt_insn;
1113 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1114 if (err == INTEL_PT_RETURN)
1119 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1120 if (!decoder->return_compression) {
1121 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1123 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1126 if (!decoder->ret_addr) {
1127 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1129 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1132 if (!(decoder->tnt.payload & BIT63)) {
1133 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1135 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1138 decoder->tnt.count -= 1;
1139 if (!decoder->tnt.count)
1140 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1141 decoder->tnt.payload <<= 1;
1142 decoder->state.from_ip = decoder->ip;
1143 decoder->ip = decoder->ret_addr;
1144 decoder->state.to_ip = decoder->ip;
1148 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1149 /* Handle deferred TIPs */
1150 err = intel_pt_get_next_packet(decoder);
1153 if (decoder->packet.type != INTEL_PT_TIP ||
1154 decoder->packet.count == 0) {
1155 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1157 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1158 decoder->pkt_step = 0;
1161 intel_pt_set_last_ip(decoder);
1162 decoder->state.from_ip = decoder->ip;
1163 decoder->state.to_ip = decoder->last_ip;
1164 decoder->ip = decoder->last_ip;
1168 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1169 decoder->tnt.count -= 1;
1170 if (!decoder->tnt.count)
1171 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1172 if (decoder->tnt.payload & BIT63) {
1173 decoder->tnt.payload <<= 1;
1174 decoder->state.from_ip = decoder->ip;
1175 decoder->ip += intel_pt_insn.length +
1177 decoder->state.to_ip = decoder->ip;
1180 /* Instruction sample for a non-taken branch */
1181 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1182 decoder->tnt.payload <<= 1;
1183 decoder->state.type = INTEL_PT_INSTRUCTION;
1184 decoder->state.from_ip = decoder->ip;
1185 decoder->state.to_ip = 0;
1186 decoder->ip += intel_pt_insn.length;
1189 decoder->ip += intel_pt_insn.length;
1190 if (!decoder->tnt.count)
1192 decoder->tnt.payload <<= 1;
1196 return intel_pt_bug(decoder);
1200 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1202 unsigned int fup_tx_flags;
1205 fup_tx_flags = decoder->packet.payload &
1206 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1207 err = intel_pt_get_next_packet(decoder);
1210 if (decoder->packet.type == INTEL_PT_FUP) {
1211 decoder->fup_tx_flags = fup_tx_flags;
1212 decoder->set_fup_tx_flags = true;
1213 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1216 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1218 intel_pt_update_in_tx(decoder);
1223 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1227 decoder->have_tma = false;
1229 if (decoder->ref_timestamp) {
1230 timestamp = decoder->packet.payload |
1231 (decoder->ref_timestamp & (0xffULL << 56));
1232 if (timestamp < decoder->ref_timestamp) {
1233 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1234 timestamp += (1ULL << 56);
1236 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1237 timestamp -= (1ULL << 56);
1239 decoder->tsc_timestamp = timestamp;
1240 decoder->timestamp = timestamp;
1241 decoder->ref_timestamp = 0;
1242 decoder->timestamp_insn_cnt = 0;
1243 } else if (decoder->timestamp) {
1244 timestamp = decoder->packet.payload |
1245 (decoder->timestamp & (0xffULL << 56));
1246 decoder->tsc_timestamp = timestamp;
1247 if (timestamp < decoder->timestamp &&
1248 decoder->timestamp - timestamp < decoder->tsc_slip) {
1249 intel_pt_log_to("Suppressing backwards timestamp",
1251 timestamp = decoder->timestamp;
1253 if (timestamp < decoder->timestamp) {
1254 intel_pt_log_to("Wraparound timestamp", timestamp);
1255 timestamp += (1ULL << 56);
1256 decoder->tsc_timestamp = timestamp;
1258 decoder->timestamp = timestamp;
1259 decoder->timestamp_insn_cnt = 0;
1262 if (decoder->last_packet_type == INTEL_PT_CYC) {
1263 decoder->cyc_ref_timestamp = decoder->timestamp;
1264 decoder->cycle_cnt = 0;
1265 decoder->have_calc_cyc_to_tsc = false;
1266 intel_pt_calc_cyc_to_tsc(decoder, false);
1269 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1272 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1274 intel_pt_log("ERROR: Buffer overflow\n");
1275 intel_pt_clear_tx_flags(decoder);
1276 decoder->have_tma = false;
1278 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1279 decoder->overflow = true;
1283 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1285 uint32_t ctc = decoder->packet.payload;
1286 uint32_t fc = decoder->packet.count;
1287 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1289 if (!decoder->tsc_ctc_ratio_d)
1292 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1293 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1294 if (decoder->tsc_ctc_mult) {
1295 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1297 decoder->ctc_timestamp -= multdiv(ctc_rem,
1298 decoder->tsc_ctc_ratio_n,
1299 decoder->tsc_ctc_ratio_d);
1301 decoder->ctc_delta = 0;
1302 decoder->have_tma = true;
1303 decoder->fixup_last_mtc = true;
1304 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1305 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1308 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1311 uint32_t mtc, mtc_delta;
1313 if (!decoder->have_tma)
1316 mtc = decoder->packet.payload;
1318 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1319 decoder->fixup_last_mtc = false;
1320 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1321 &decoder->last_mtc);
1324 if (mtc > decoder->last_mtc)
1325 mtc_delta = mtc - decoder->last_mtc;
1327 mtc_delta = mtc + 256 - decoder->last_mtc;
1329 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1331 if (decoder->tsc_ctc_mult) {
1332 timestamp = decoder->ctc_timestamp +
1333 decoder->ctc_delta * decoder->tsc_ctc_mult;
1335 timestamp = decoder->ctc_timestamp +
1336 multdiv(decoder->ctc_delta,
1337 decoder->tsc_ctc_ratio_n,
1338 decoder->tsc_ctc_ratio_d);
1341 if (timestamp < decoder->timestamp)
1342 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1343 timestamp, decoder->timestamp);
1345 decoder->timestamp = timestamp;
1347 decoder->timestamp_insn_cnt = 0;
1348 decoder->last_mtc = mtc;
1350 if (decoder->last_packet_type == INTEL_PT_CYC) {
1351 decoder->cyc_ref_timestamp = decoder->timestamp;
1352 decoder->cycle_cnt = 0;
1353 decoder->have_calc_cyc_to_tsc = false;
1354 intel_pt_calc_cyc_to_tsc(decoder, true);
1358 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1360 unsigned int cbr = decoder->packet.payload;
1362 if (decoder->cbr == cbr)
1366 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1369 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1371 uint64_t timestamp = decoder->cyc_ref_timestamp;
1373 decoder->have_cyc = true;
1375 decoder->cycle_cnt += decoder->packet.payload;
1377 if (!decoder->cyc_ref_timestamp)
1380 if (decoder->have_calc_cyc_to_tsc)
1381 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1382 else if (decoder->cbr)
1383 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1387 if (timestamp < decoder->timestamp)
1388 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1389 timestamp, decoder->timestamp);
1391 decoder->timestamp = timestamp;
1393 decoder->timestamp_insn_cnt = 0;
1396 /* Walk PSB+ packets when already in sync. */
1397 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1402 err = intel_pt_get_next_packet(decoder);
1406 switch (decoder->packet.type) {
1407 case INTEL_PT_PSBEND:
1410 case INTEL_PT_TIP_PGD:
1411 case INTEL_PT_TIP_PGE:
1414 case INTEL_PT_TRACESTOP:
1417 decoder->have_tma = false;
1418 intel_pt_log("ERROR: Unexpected packet\n");
1422 return intel_pt_overflow(decoder);
1425 intel_pt_calc_tsc_timestamp(decoder);
1429 intel_pt_calc_tma(decoder);
1433 intel_pt_calc_cbr(decoder);
1436 case INTEL_PT_MODE_EXEC:
1437 decoder->exec_mode = decoder->packet.payload;
1441 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1445 decoder->pge = true;
1446 intel_pt_set_last_ip(decoder);
1449 case INTEL_PT_MODE_TSX:
1450 intel_pt_update_in_tx(decoder);
1454 intel_pt_calc_mtc_timestamp(decoder);
1455 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1456 decoder->state.type |= INTEL_PT_INSTRUCTION;
1469 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1473 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1474 decoder->tx_flags = 0;
1475 decoder->state.flags &= ~INTEL_PT_IN_TX;
1476 decoder->state.flags |= INTEL_PT_ABORT_TX;
1478 decoder->state.flags |= INTEL_PT_ASYNC;
1482 err = intel_pt_get_next_packet(decoder);
1486 switch (decoder->packet.type) {
1489 case INTEL_PT_TRACESTOP:
1494 case INTEL_PT_MODE_TSX:
1496 case INTEL_PT_PSBEND:
1497 intel_pt_log("ERROR: Missing TIP after FUP\n");
1498 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1502 return intel_pt_overflow(decoder);
1504 case INTEL_PT_TIP_PGD:
1505 decoder->state.from_ip = decoder->ip;
1506 decoder->state.to_ip = 0;
1507 if (decoder->packet.count != 0) {
1508 intel_pt_set_ip(decoder);
1509 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1512 decoder->pge = false;
1513 decoder->continuous_period = false;
1516 case INTEL_PT_TIP_PGE:
1517 decoder->pge = true;
1518 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1520 decoder->state.from_ip = 0;
1521 if (decoder->packet.count == 0) {
1522 decoder->state.to_ip = 0;
1524 intel_pt_set_ip(decoder);
1525 decoder->state.to_ip = decoder->ip;
1530 decoder->state.from_ip = decoder->ip;
1531 if (decoder->packet.count == 0) {
1532 decoder->state.to_ip = 0;
1534 intel_pt_set_ip(decoder);
1535 decoder->state.to_ip = decoder->ip;
1540 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1544 intel_pt_calc_mtc_timestamp(decoder);
1545 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1546 decoder->state.type |= INTEL_PT_INSTRUCTION;
1550 intel_pt_calc_cyc_timestamp(decoder);
1553 case INTEL_PT_MODE_EXEC:
1554 decoder->exec_mode = decoder->packet.payload;
1563 return intel_pt_bug(decoder);
1568 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1570 bool no_tip = false;
1574 err = intel_pt_get_next_packet(decoder);
1578 switch (decoder->packet.type) {
1580 if (!decoder->packet.count)
1582 decoder->tnt = decoder->packet;
1583 decoder->pkt_state = INTEL_PT_STATE_TNT;
1584 err = intel_pt_walk_tnt(decoder);
1589 case INTEL_PT_TIP_PGD:
1590 if (decoder->packet.count != 0)
1591 intel_pt_set_last_ip(decoder);
1592 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1593 return intel_pt_walk_tip(decoder);
1595 case INTEL_PT_TIP_PGE: {
1596 decoder->pge = true;
1597 if (decoder->packet.count == 0) {
1598 intel_pt_log_at("Skipping zero TIP.PGE",
1602 intel_pt_set_ip(decoder);
1603 decoder->state.from_ip = 0;
1604 decoder->state.to_ip = decoder->ip;
1609 return intel_pt_overflow(decoder);
1612 if (decoder->packet.count != 0)
1613 intel_pt_set_last_ip(decoder);
1614 decoder->pkt_state = INTEL_PT_STATE_TIP;
1615 return intel_pt_walk_tip(decoder);
1618 if (decoder->packet.count == 0) {
1619 intel_pt_log_at("Skipping zero FUP",
1624 intel_pt_set_last_ip(decoder);
1625 err = intel_pt_walk_fup(decoder);
1626 if (err != -EAGAIN) {
1630 decoder->pkt_state =
1631 INTEL_PT_STATE_FUP_NO_TIP;
1633 decoder->pkt_state = INTEL_PT_STATE_FUP;
1640 return intel_pt_walk_fup_tip(decoder);
1642 case INTEL_PT_TRACESTOP:
1643 decoder->pge = false;
1644 decoder->continuous_period = false;
1645 intel_pt_clear_tx_flags(decoder);
1646 decoder->have_tma = false;
1650 intel_pt_clear_stack(&decoder->stack);
1651 err = intel_pt_walk_psbend(decoder);
1659 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1663 intel_pt_calc_mtc_timestamp(decoder);
1664 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1667 * Ensure that there has been an instruction since the
1670 if (!decoder->mtc_insn)
1672 decoder->mtc_insn = false;
1673 /* Ensure that there is a timestamp */
1674 if (!decoder->timestamp)
1676 decoder->state.type = INTEL_PT_INSTRUCTION;
1677 decoder->state.from_ip = decoder->ip;
1678 decoder->state.to_ip = 0;
1679 decoder->mtc_insn = false;
1683 intel_pt_calc_tsc_timestamp(decoder);
1687 intel_pt_calc_tma(decoder);
1691 intel_pt_calc_cyc_timestamp(decoder);
1695 intel_pt_calc_cbr(decoder);
1698 case INTEL_PT_MODE_EXEC:
1699 decoder->exec_mode = decoder->packet.payload;
1702 case INTEL_PT_MODE_TSX:
1703 /* MODE_TSX need not be followed by FUP */
1704 if (!decoder->pge) {
1705 intel_pt_update_in_tx(decoder);
1708 err = intel_pt_mode_tsx(decoder, &no_tip);
1713 case INTEL_PT_BAD: /* Does not happen */
1714 return intel_pt_bug(decoder);
1716 case INTEL_PT_PSBEND:
1723 return intel_pt_bug(decoder);
1728 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1730 return decoder->last_ip || decoder->packet.count == 0 ||
1731 decoder->packet.count == 3 || decoder->packet.count == 6;
1734 /* Walk PSB+ packets to get in sync. */
1735 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1740 err = intel_pt_get_next_packet(decoder);
1744 switch (decoder->packet.type) {
1745 case INTEL_PT_TIP_PGD:
1746 decoder->continuous_period = false;
1747 case INTEL_PT_TIP_PGE:
1749 intel_pt_log("ERROR: Unexpected packet\n");
1753 decoder->pge = true;
1754 if (intel_pt_have_ip(decoder)) {
1755 uint64_t current_ip = decoder->ip;
1757 intel_pt_set_ip(decoder);
1759 intel_pt_log_to("Setting IP",
1765 intel_pt_calc_mtc_timestamp(decoder);
1769 intel_pt_calc_tsc_timestamp(decoder);
1773 intel_pt_calc_tma(decoder);
1777 intel_pt_calc_cyc_timestamp(decoder);
1781 intel_pt_calc_cbr(decoder);
1785 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1788 case INTEL_PT_MODE_EXEC:
1789 decoder->exec_mode = decoder->packet.payload;
1792 case INTEL_PT_MODE_TSX:
1793 intel_pt_update_in_tx(decoder);
1796 case INTEL_PT_TRACESTOP:
1797 decoder->pge = false;
1798 decoder->continuous_period = false;
1799 intel_pt_clear_tx_flags(decoder);
1801 decoder->have_tma = false;
1802 intel_pt_log("ERROR: Unexpected packet\n");
1804 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1806 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1809 case INTEL_PT_BAD: /* Does not happen */
1810 return intel_pt_bug(decoder);
1813 return intel_pt_overflow(decoder);
1815 case INTEL_PT_PSBEND:
1828 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1833 err = intel_pt_get_next_packet(decoder);
1837 switch (decoder->packet.type) {
1838 case INTEL_PT_TIP_PGD:
1839 decoder->continuous_period = false;
1840 case INTEL_PT_TIP_PGE:
1842 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1843 if (intel_pt_have_ip(decoder))
1844 intel_pt_set_ip(decoder);
1850 if (decoder->overflow) {
1851 if (intel_pt_have_ip(decoder))
1852 intel_pt_set_ip(decoder);
1856 if (decoder->packet.count)
1857 intel_pt_set_last_ip(decoder);
1861 intel_pt_calc_mtc_timestamp(decoder);
1865 intel_pt_calc_tsc_timestamp(decoder);
1869 intel_pt_calc_tma(decoder);
1873 intel_pt_calc_cyc_timestamp(decoder);
1877 intel_pt_calc_cbr(decoder);
1881 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1884 case INTEL_PT_MODE_EXEC:
1885 decoder->exec_mode = decoder->packet.payload;
1888 case INTEL_PT_MODE_TSX:
1889 intel_pt_update_in_tx(decoder);
1893 return intel_pt_overflow(decoder);
1895 case INTEL_PT_BAD: /* Does not happen */
1896 return intel_pt_bug(decoder);
1898 case INTEL_PT_TRACESTOP:
1899 decoder->pge = false;
1900 decoder->continuous_period = false;
1901 intel_pt_clear_tx_flags(decoder);
1902 decoder->have_tma = false;
1906 err = intel_pt_walk_psb(decoder);
1910 /* Do not have a sample */
1911 decoder->state.type = 0;
1917 case INTEL_PT_PSBEND:
1927 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1931 intel_pt_log("Scanning for full IP\n");
1932 err = intel_pt_walk_to_ip(decoder);
1936 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1937 decoder->overflow = false;
1939 decoder->state.from_ip = 0;
1940 decoder->state.to_ip = decoder->ip;
1941 intel_pt_log_to("Setting IP", decoder->ip);
1946 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1948 const unsigned char *end = decoder->buf + decoder->len;
1951 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1952 if (i > decoder->len)
1954 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1960 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1962 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1963 const char *psb = INTEL_PT_PSB_STR;
1965 if (rest_psb > decoder->len ||
1966 memcmp(decoder->buf, psb + part_psb, rest_psb))
1972 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
1977 decoder->pos += decoder->len;
1980 ret = intel_pt_get_next_data(decoder);
1984 rest_psb = intel_pt_rest_psb(decoder, part_psb);
1988 decoder->pos -= part_psb;
1989 decoder->next_buf = decoder->buf + rest_psb;
1990 decoder->next_len = decoder->len - rest_psb;
1991 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1992 decoder->buf = decoder->temp_buf;
1993 decoder->len = INTEL_PT_PSB_LEN;
1998 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2000 unsigned char *next;
2003 intel_pt_log("Scanning for PSB\n");
2005 if (!decoder->len) {
2006 ret = intel_pt_get_next_data(decoder);
2011 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2016 part_psb = intel_pt_part_psb(decoder);
2018 ret = intel_pt_get_split_psb(decoder, part_psb);
2022 decoder->pos += decoder->len;
2028 decoder->pkt_step = next - decoder->buf;
2029 return intel_pt_get_next_packet(decoder);
2033 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2037 decoder->pge = false;
2038 decoder->continuous_period = false;
2039 decoder->last_ip = 0;
2041 intel_pt_clear_stack(&decoder->stack);
2043 err = intel_pt_scan_for_psb(decoder);
2047 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2049 err = intel_pt_walk_psb(decoder);
2054 decoder->state.type = 0; /* Do not have a sample */
2055 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2057 return intel_pt_sync_ip(decoder);
2063 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2065 uint64_t est = decoder->timestamp_insn_cnt << 1;
2067 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2070 est *= decoder->max_non_turbo_ratio;
2071 est /= decoder->cbr;
2073 return decoder->timestamp + est;
2076 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2081 decoder->state.type = INTEL_PT_BRANCH;
2082 decoder->state.flags = 0;
2084 switch (decoder->pkt_state) {
2085 case INTEL_PT_STATE_NO_PSB:
2086 err = intel_pt_sync(decoder);
2088 case INTEL_PT_STATE_NO_IP:
2089 decoder->last_ip = 0;
2091 case INTEL_PT_STATE_ERR_RESYNC:
2092 err = intel_pt_sync_ip(decoder);
2094 case INTEL_PT_STATE_IN_SYNC:
2095 err = intel_pt_walk_trace(decoder);
2097 case INTEL_PT_STATE_TNT:
2098 err = intel_pt_walk_tnt(decoder);
2100 err = intel_pt_walk_trace(decoder);
2102 case INTEL_PT_STATE_TIP:
2103 case INTEL_PT_STATE_TIP_PGD:
2104 err = intel_pt_walk_tip(decoder);
2106 case INTEL_PT_STATE_FUP:
2107 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2108 err = intel_pt_walk_fup(decoder);
2110 err = intel_pt_walk_fup_tip(decoder);
2112 decoder->pkt_state = INTEL_PT_STATE_FUP;
2114 case INTEL_PT_STATE_FUP_NO_TIP:
2115 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2116 err = intel_pt_walk_fup(decoder);
2118 err = intel_pt_walk_trace(decoder);
2121 err = intel_pt_bug(decoder);
2124 } while (err == -ENOLINK);
2126 decoder->state.err = err ? intel_pt_ext_err(err) : 0;
2127 decoder->state.timestamp = decoder->timestamp;
2128 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2129 decoder->state.cr3 = decoder->cr3;
2130 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2133 decoder->state.from_ip = decoder->ip;
2135 return &decoder->state;
2138 static bool intel_pt_at_psb(unsigned char *buf, size_t len)
2140 if (len < INTEL_PT_PSB_LEN)
2142 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
2147 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2148 * @buf: pointer to buffer pointer
2149 * @len: size of buffer
2151 * Updates the buffer pointer to point to the start of the next PSB packet if
2152 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2153 * @len is adjusted accordingly.
2155 * Return: %true if a PSB packet is found, %false otherwise.
2157 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2159 unsigned char *next;
2161 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2163 *len -= next - *buf;
2171 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2173 * @buf: pointer to buffer pointer
2174 * @len: size of buffer
2176 * Updates the buffer pointer to point to the start of the following PSB packet
2177 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2178 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2180 * Return: %true if a PSB packet is found, %false otherwise.
2182 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2184 unsigned char *next;
2189 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2191 *len -= next - *buf;
2199 * intel_pt_last_psb - find the last PSB packet in a buffer.
2201 * @len: size of buffer
2203 * This function finds the last PSB in a buffer.
2205 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2207 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2209 const char *n = INTEL_PT_PSB_STR;
2213 if (len < INTEL_PT_PSB_LEN)
2216 k = len - INTEL_PT_PSB_LEN + 1;
2218 p = memrchr(buf, n[0], k);
2221 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2230 * intel_pt_next_tsc - find and return next TSC.
2232 * @len: size of buffer
2233 * @tsc: TSC value returned
2235 * Find a TSC packet in @buf and return the TSC value. This function assumes
2236 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2237 * PSBEND packet is found.
2239 * Return: %true if TSC is found, false otherwise.
2241 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
2243 struct intel_pt_pkt packet;
2247 ret = intel_pt_get_packet(buf, len, &packet);
2250 if (packet.type == INTEL_PT_TSC) {
2251 *tsc = packet.payload;
2254 if (packet.type == INTEL_PT_PSBEND)
2263 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2264 * @tsc1: first TSC to compare
2265 * @tsc2: second TSC to compare
2267 * This function compares 7-byte TSC values allowing for the possibility that
2268 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2269 * around so for that purpose this function assumes the absolute difference is
2270 * less than half the maximum difference.
2272 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2275 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2277 const uint64_t halfway = (1ULL << 55);
2283 if (tsc2 - tsc1 < halfway)
2288 if (tsc1 - tsc2 < halfway)
2296 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2298 * @buf_a: first buffer
2299 * @len_a: size of first buffer
2300 * @buf_b: second buffer
2301 * @len_b: size of second buffer
2303 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2304 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2305 * walk forward in @buf_b until a later TSC is found. A precondition is that
2306 * @buf_a and @buf_b are positioned at a PSB.
2308 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2309 * @buf_b + @len_b if there is no non-overlapped data.
2311 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2313 unsigned char *buf_b,
2316 uint64_t tsc_a, tsc_b;
2320 p = intel_pt_last_psb(buf_a, len_a);
2322 return buf_b; /* No PSB in buf_a => no overlap */
2324 len = len_a - (p - buf_a);
2325 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
2326 /* The last PSB+ in buf_a is incomplete, so go back one more */
2328 p = intel_pt_last_psb(buf_a, len_a);
2330 return buf_b; /* No full PSB+ => assume no overlap */
2331 len = len_a - (p - buf_a);
2332 if (!intel_pt_next_tsc(p, len, &tsc_a))
2333 return buf_b; /* No TSC in buf_a => assume no overlap */
2337 /* Ignore PSB+ with no TSC */
2338 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
2339 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
2340 return buf_b; /* tsc_a < tsc_b => no overlap */
2342 if (!intel_pt_step_psb(&buf_b, &len_b))
2343 return buf_b + len_b; /* No PSB in buf_b => no data */
2348 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2349 * @buf_a: first buffer
2350 * @len_a: size of first buffer
2351 * @buf_b: second buffer
2352 * @len_b: size of second buffer
2353 * @have_tsc: can use TSC packets to detect overlap
2355 * When trace samples or snapshots are recorded there is the possibility that
2356 * the data overlaps. Note that, for the purposes of decoding, data is only
2357 * useful if it begins with a PSB packet.
2359 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2360 * @buf_b + @len_b if there is no non-overlapped data.
2362 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2363 unsigned char *buf_b, size_t len_b,
2366 unsigned char *found;
2368 /* Buffer 'b' must start at PSB so throw away everything before that */
2369 if (!intel_pt_next_psb(&buf_b, &len_b))
2370 return buf_b + len_b; /* No PSB */
2372 if (!intel_pt_next_psb(&buf_a, &len_a))
2373 return buf_b; /* No overlap */
2376 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
2382 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2383 * we can ignore the first part of buffer 'a'.
2385 while (len_b < len_a) {
2386 if (!intel_pt_step_psb(&buf_a, &len_a))
2387 return buf_b; /* No overlap */
2390 /* Now len_b >= len_a */
2391 if (len_b > len_a) {
2392 /* The leftover buffer 'b' must start at a PSB */
2393 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2394 if (!intel_pt_step_psb(&buf_a, &len_a))
2395 return buf_b; /* No overlap */
2400 /* Potential overlap so check the bytes */
2401 found = memmem(buf_a, len_a, buf_b, len_a);
2403 return buf_b + len_a;
2405 /* Try again at next PSB in buffer 'a' */
2406 if (!intel_pt_step_psb(&buf_a, &len_a))
2407 return buf_b; /* No overlap */
2409 /* The leftover buffer 'b' must start at a PSB */
2410 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2411 if (!intel_pt_step_psb(&buf_a, &len_a))
2412 return buf_b; /* No overlap */