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>
29 #include "../auxtrace.h"
31 #include "intel-pt-insn-decoder.h"
32 #include "intel-pt-pkt-decoder.h"
33 #include "intel-pt-decoder.h"
34 #include "intel-pt-log.h"
36 #define INTEL_PT_BLK_SIZE 1024
38 #define BIT63 (((uint64_t)1 << 63))
40 #define INTEL_PT_RETURN 1
42 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
43 #define INTEL_PT_MAX_LOOPS 10000
46 struct intel_pt_blk *prev;
47 uint64_t ip[INTEL_PT_BLK_SIZE];
50 struct intel_pt_stack {
51 struct intel_pt_blk *blk;
52 struct intel_pt_blk *spare;
56 enum intel_pt_pkt_state {
57 INTEL_PT_STATE_NO_PSB,
59 INTEL_PT_STATE_ERR_RESYNC,
60 INTEL_PT_STATE_IN_SYNC,
61 INTEL_PT_STATE_TNT_CONT,
64 INTEL_PT_STATE_TIP_PGD,
66 INTEL_PT_STATE_FUP_NO_TIP,
69 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
72 case INTEL_PT_STATE_NO_PSB:
73 case INTEL_PT_STATE_NO_IP:
74 case INTEL_PT_STATE_ERR_RESYNC:
75 case INTEL_PT_STATE_IN_SYNC:
76 case INTEL_PT_STATE_TNT_CONT:
78 case INTEL_PT_STATE_TNT:
79 case INTEL_PT_STATE_TIP:
80 case INTEL_PT_STATE_TIP_PGD:
81 case INTEL_PT_STATE_FUP:
82 case INTEL_PT_STATE_FUP_NO_TIP:
89 #ifdef INTEL_PT_STRICT
90 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
91 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
92 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
93 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
95 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
96 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
97 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
98 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
101 struct intel_pt_decoder {
102 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
103 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
104 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
105 uint64_t max_insn_cnt, void *data);
106 bool (*pgd_ip)(uint64_t ip, void *data);
108 struct intel_pt_state state;
109 const unsigned char *buf;
111 bool return_compression;
118 enum intel_pt_param_flags flags;
124 uint64_t tsc_timestamp;
125 uint64_t ref_timestamp;
126 uint64_t sample_timestamp;
128 uint64_t ctc_timestamp;
131 uint64_t cyc_ref_timestamp;
133 uint32_t tsc_ctc_ratio_n;
134 uint32_t tsc_ctc_ratio_d;
135 uint32_t tsc_ctc_mult;
137 uint32_t ctc_rem_mask;
139 struct intel_pt_stack stack;
140 enum intel_pt_pkt_state pkt_state;
141 struct intel_pt_pkt packet;
142 struct intel_pt_pkt tnt;
145 int last_packet_type;
147 unsigned int max_non_turbo_ratio;
148 double max_non_turbo_ratio_fp;
149 double cbr_cyc_to_tsc;
150 double calc_cyc_to_tsc;
151 bool have_calc_cyc_to_tsc;
153 unsigned int insn_bytes;
155 enum intel_pt_period_type period_type;
156 uint64_t tot_insn_cnt;
157 uint64_t period_insn_cnt;
158 uint64_t period_mask;
159 uint64_t period_ticks;
160 uint64_t last_masked_timestamp;
161 bool continuous_period;
163 bool set_fup_tx_flags;
164 unsigned int fup_tx_flags;
165 unsigned int tx_flags;
166 uint64_t timestamp_insn_cnt;
167 uint64_t sample_insn_cnt;
172 const unsigned char *next_buf;
174 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
177 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
181 for (i = 0; x != 1; i++)
187 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
189 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
192 period = intel_pt_lower_power_of_2(decoder->period);
193 decoder->period_mask = ~(period - 1);
194 decoder->period_ticks = period;
198 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
202 return (t / d) * n + ((t % d) * n) / d;
205 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
207 struct intel_pt_decoder *decoder;
209 if (!params->get_trace || !params->walk_insn)
212 decoder = zalloc(sizeof(struct intel_pt_decoder));
216 decoder->get_trace = params->get_trace;
217 decoder->walk_insn = params->walk_insn;
218 decoder->pgd_ip = params->pgd_ip;
219 decoder->data = params->data;
220 decoder->return_compression = params->return_compression;
222 decoder->flags = params->flags;
224 decoder->period = params->period;
225 decoder->period_type = params->period_type;
227 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
228 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
230 intel_pt_setup_period(decoder);
232 decoder->mtc_shift = params->mtc_period;
233 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
235 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
236 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
238 if (!decoder->tsc_ctc_ratio_n)
239 decoder->tsc_ctc_ratio_d = 0;
241 if (decoder->tsc_ctc_ratio_d) {
242 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
243 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
244 decoder->tsc_ctc_ratio_d;
248 * A TSC packet can slip past MTC packets so that the timestamp appears
249 * to go backwards. One estimate is that can be up to about 40 CPU
250 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
251 * slippage an order of magnitude more to be on the safe side.
253 decoder->tsc_slip = 0x10000;
255 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
256 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
257 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
258 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
259 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
264 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
266 struct intel_pt_blk *blk = stack->blk;
268 stack->blk = blk->prev;
275 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
280 intel_pt_pop_blk(stack);
283 stack->pos = INTEL_PT_BLK_SIZE;
285 return stack->blk->ip[--stack->pos];
288 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
290 struct intel_pt_blk *blk;
296 blk = malloc(sizeof(struct intel_pt_blk));
301 blk->prev = stack->blk;
307 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
311 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
312 err = intel_pt_alloc_blk(stack);
317 stack->blk->ip[stack->pos++] = ip;
321 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
324 intel_pt_pop_blk(stack);
328 static void intel_pt_free_stack(struct intel_pt_stack *stack)
330 intel_pt_clear_stack(stack);
332 zfree(&stack->spare);
335 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
337 intel_pt_free_stack(&decoder->stack);
341 static int intel_pt_ext_err(int code)
345 return INTEL_PT_ERR_NOMEM;
347 return INTEL_PT_ERR_INTERN;
349 return INTEL_PT_ERR_BADPKT;
351 return INTEL_PT_ERR_NODATA;
353 return INTEL_PT_ERR_NOINSN;
355 return INTEL_PT_ERR_MISMAT;
357 return INTEL_PT_ERR_OVR;
359 return INTEL_PT_ERR_LOST;
361 return INTEL_PT_ERR_NELOOP;
363 return INTEL_PT_ERR_UNK;
367 static const char *intel_pt_err_msgs[] = {
368 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
369 [INTEL_PT_ERR_INTERN] = "Internal error",
370 [INTEL_PT_ERR_BADPKT] = "Bad packet",
371 [INTEL_PT_ERR_NODATA] = "No more data",
372 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
373 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
374 [INTEL_PT_ERR_OVR] = "Overflow packet",
375 [INTEL_PT_ERR_LOST] = "Lost trace data",
376 [INTEL_PT_ERR_UNK] = "Unknown error!",
377 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
380 int intel_pt__strerror(int code, char *buf, size_t buflen)
382 if (code < 1 || code >= INTEL_PT_ERR_MAX)
383 code = INTEL_PT_ERR_UNK;
384 strlcpy(buf, intel_pt_err_msgs[code], buflen);
388 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
393 switch (packet->count) {
395 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
399 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
403 ip = packet->payload;
404 /* Sign-extend 6-byte ip */
405 if (ip & (uint64_t)0x800000000000ULL)
406 ip |= (uint64_t)0xffff000000000000ULL;
409 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
413 ip = packet->payload;
422 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
424 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
425 decoder->have_last_ip = true;
428 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
430 intel_pt_set_last_ip(decoder);
431 decoder->ip = decoder->last_ip;
434 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
436 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
440 static int intel_pt_bug(struct intel_pt_decoder *decoder)
442 intel_pt_log("ERROR: Internal error\n");
443 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
447 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
449 decoder->tx_flags = 0;
452 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
454 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
457 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
459 intel_pt_clear_tx_flags(decoder);
460 decoder->have_tma = false;
461 decoder->pkt_len = 1;
462 decoder->pkt_step = 1;
463 intel_pt_decoder_log_packet(decoder);
464 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
465 intel_pt_log("ERROR: Bad packet\n");
466 decoder->pkt_state = INTEL_PT_STATE_ERR1;
471 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
473 struct intel_pt_buffer buffer = { .buf = 0, };
476 decoder->pkt_step = 0;
478 intel_pt_log("Getting more data\n");
479 ret = decoder->get_trace(&buffer, decoder->data);
482 decoder->buf = buffer.buf;
483 decoder->len = buffer.len;
485 intel_pt_log("No more data\n");
488 if (!buffer.consecutive) {
490 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
491 decoder->ref_timestamp = buffer.ref_timestamp;
492 decoder->timestamp = 0;
493 decoder->have_tma = false;
494 decoder->state.trace_nr = buffer.trace_nr;
495 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
496 decoder->ref_timestamp);
503 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
505 if (!decoder->next_buf)
506 return intel_pt_get_data(decoder);
508 decoder->buf = decoder->next_buf;
509 decoder->len = decoder->next_len;
510 decoder->next_buf = 0;
511 decoder->next_len = 0;
515 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
517 unsigned char *buf = decoder->temp_buf;
518 size_t old_len, len, n;
521 old_len = decoder->len;
523 memcpy(buf, decoder->buf, len);
525 ret = intel_pt_get_data(decoder);
527 decoder->pos += old_len;
528 return ret < 0 ? ret : -EINVAL;
531 n = INTEL_PT_PKT_MAX_SZ - len;
532 if (n > decoder->len)
534 memcpy(buf + len, decoder->buf, n);
537 ret = intel_pt_get_packet(buf, len, &decoder->packet);
538 if (ret < (int)old_len) {
539 decoder->next_buf = decoder->buf;
540 decoder->next_len = decoder->len;
542 decoder->len = old_len;
543 return intel_pt_bad_packet(decoder);
546 decoder->next_buf = decoder->buf + (ret - old_len);
547 decoder->next_len = decoder->len - (ret - old_len);
555 struct intel_pt_pkt_info {
556 struct intel_pt_decoder *decoder;
557 struct intel_pt_pkt packet;
560 int last_packet_type;
564 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
566 /* Lookahead packets in current buffer */
567 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
568 intel_pt_pkt_cb_t cb, void *data)
570 struct intel_pt_pkt_info pkt_info;
571 const unsigned char *buf = decoder->buf;
572 size_t len = decoder->len;
575 pkt_info.decoder = decoder;
576 pkt_info.pos = decoder->pos;
577 pkt_info.pkt_len = decoder->pkt_step;
578 pkt_info.last_packet_type = decoder->last_packet_type;
579 pkt_info.data = data;
583 pkt_info.pos += pkt_info.pkt_len;
584 buf += pkt_info.pkt_len;
585 len -= pkt_info.pkt_len;
588 return INTEL_PT_NEED_MORE_BYTES;
590 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
592 return INTEL_PT_NEED_MORE_BYTES;
596 pkt_info.pkt_len = ret;
597 } while (pkt_info.packet.type == INTEL_PT_PAD);
603 pkt_info.last_packet_type = pkt_info.packet.type;
607 struct intel_pt_calc_cyc_to_tsc_info {
611 uint64_t ctc_timestamp;
613 uint64_t tsc_timestamp;
618 double cbr_cyc_to_tsc;
622 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
623 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
624 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
625 * packet by copying the missing bits from the current MTC assuming the least
626 * difference between the two, and that the current MTC comes after last_mtc.
628 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
631 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
632 uint32_t mask = ~(first_missing_bit - 1);
634 *last_mtc |= mtc & mask;
635 if (*last_mtc >= mtc) {
636 *last_mtc -= first_missing_bit;
641 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
643 struct intel_pt_decoder *decoder = pkt_info->decoder;
644 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
648 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
650 switch (pkt_info->packet.type) {
652 case INTEL_PT_TIP_PGE:
657 case INTEL_PT_MODE_EXEC:
658 case INTEL_PT_MODE_TSX:
659 case INTEL_PT_PSBEND:
669 mtc = pkt_info->packet.payload;
670 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
671 data->fixup_last_mtc = false;
672 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
675 if (mtc > data->last_mtc)
676 mtc_delta = mtc - data->last_mtc;
678 mtc_delta = mtc + 256 - data->last_mtc;
679 data->ctc_delta += mtc_delta << decoder->mtc_shift;
680 data->last_mtc = mtc;
682 if (decoder->tsc_ctc_mult) {
683 timestamp = data->ctc_timestamp +
684 data->ctc_delta * decoder->tsc_ctc_mult;
686 timestamp = data->ctc_timestamp +
687 multdiv(data->ctc_delta,
688 decoder->tsc_ctc_ratio_n,
689 decoder->tsc_ctc_ratio_d);
692 if (timestamp < data->timestamp)
695 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
696 data->timestamp = timestamp;
703 timestamp = pkt_info->packet.payload |
704 (data->timestamp & (0xffULL << 56));
705 if (data->from_mtc && timestamp < data->timestamp &&
706 data->timestamp - timestamp < decoder->tsc_slip)
708 if (timestamp < data->timestamp)
709 timestamp += (1ULL << 56);
710 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
713 data->tsc_timestamp = timestamp;
714 data->timestamp = timestamp;
723 if (!decoder->tsc_ctc_ratio_d)
726 ctc = pkt_info->packet.payload;
727 fc = pkt_info->packet.count;
728 ctc_rem = ctc & decoder->ctc_rem_mask;
730 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
732 data->ctc_timestamp = data->tsc_timestamp - fc;
733 if (decoder->tsc_ctc_mult) {
734 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
736 data->ctc_timestamp -=
737 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
738 decoder->tsc_ctc_ratio_d);
742 data->have_tma = true;
743 data->fixup_last_mtc = true;
748 data->cycle_cnt += pkt_info->packet.payload;
752 cbr = pkt_info->packet.payload;
753 if (data->cbr && data->cbr != cbr)
756 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
759 case INTEL_PT_TIP_PGD:
760 case INTEL_PT_TRACESTOP:
762 case INTEL_PT_BAD: /* Does not happen */
767 if (!data->cbr && decoder->cbr) {
768 data->cbr = decoder->cbr;
769 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
772 if (!data->cycle_cnt)
775 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
777 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
778 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
779 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
780 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
784 decoder->calc_cyc_to_tsc = cyc_to_tsc;
785 decoder->have_calc_cyc_to_tsc = true;
788 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
789 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
791 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
792 cyc_to_tsc, pkt_info->pos);
798 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
801 struct intel_pt_calc_cyc_to_tsc_info data = {
804 .last_mtc = decoder->last_mtc,
805 .ctc_timestamp = decoder->ctc_timestamp,
806 .ctc_delta = decoder->ctc_delta,
807 .tsc_timestamp = decoder->tsc_timestamp,
808 .timestamp = decoder->timestamp,
809 .have_tma = decoder->have_tma,
810 .fixup_last_mtc = decoder->fixup_last_mtc,
811 .from_mtc = from_mtc,
815 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
818 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
822 decoder->last_packet_type = decoder->packet.type;
825 decoder->pos += decoder->pkt_step;
826 decoder->buf += decoder->pkt_step;
827 decoder->len -= decoder->pkt_step;
830 ret = intel_pt_get_next_data(decoder);
835 ret = intel_pt_get_packet(decoder->buf, decoder->len,
837 if (ret == INTEL_PT_NEED_MORE_BYTES &&
838 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
839 ret = intel_pt_get_split_packet(decoder);
844 return intel_pt_bad_packet(decoder);
846 decoder->pkt_len = ret;
847 decoder->pkt_step = ret;
848 intel_pt_decoder_log_packet(decoder);
849 } while (decoder->packet.type == INTEL_PT_PAD);
854 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
856 uint64_t timestamp, masked_timestamp;
858 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
859 masked_timestamp = timestamp & decoder->period_mask;
860 if (decoder->continuous_period) {
861 if (masked_timestamp > decoder->last_masked_timestamp)
865 masked_timestamp = timestamp & decoder->period_mask;
866 if (masked_timestamp > decoder->last_masked_timestamp) {
867 decoder->last_masked_timestamp = masked_timestamp;
868 decoder->continuous_period = true;
872 if (masked_timestamp < decoder->last_masked_timestamp)
873 return decoder->period_ticks;
875 return decoder->period_ticks - (timestamp - masked_timestamp);
878 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
880 switch (decoder->period_type) {
881 case INTEL_PT_PERIOD_INSTRUCTIONS:
882 return decoder->period - decoder->period_insn_cnt;
883 case INTEL_PT_PERIOD_TICKS:
884 return intel_pt_next_period(decoder);
885 case INTEL_PT_PERIOD_NONE:
886 case INTEL_PT_PERIOD_MTC:
892 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
894 uint64_t timestamp, masked_timestamp;
896 switch (decoder->period_type) {
897 case INTEL_PT_PERIOD_INSTRUCTIONS:
898 decoder->period_insn_cnt = 0;
900 case INTEL_PT_PERIOD_TICKS:
901 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
902 masked_timestamp = timestamp & decoder->period_mask;
903 if (masked_timestamp > decoder->last_masked_timestamp)
904 decoder->last_masked_timestamp = masked_timestamp;
906 decoder->last_masked_timestamp += decoder->period_ticks;
908 case INTEL_PT_PERIOD_NONE:
909 case INTEL_PT_PERIOD_MTC:
914 decoder->state.type |= INTEL_PT_INSTRUCTION;
917 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
918 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
920 uint64_t max_insn_cnt, insn_cnt = 0;
923 if (!decoder->mtc_insn)
924 decoder->mtc_insn = true;
926 max_insn_cnt = intel_pt_next_sample(decoder);
928 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
929 max_insn_cnt, decoder->data);
931 decoder->tot_insn_cnt += insn_cnt;
932 decoder->timestamp_insn_cnt += insn_cnt;
933 decoder->sample_insn_cnt += insn_cnt;
934 decoder->period_insn_cnt += insn_cnt;
937 decoder->no_progress = 0;
938 decoder->pkt_state = INTEL_PT_STATE_ERR2;
939 intel_pt_log_at("ERROR: Failed to get instruction",
946 if (ip && decoder->ip == ip) {
951 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
952 intel_pt_sample_insn(decoder);
954 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
955 decoder->state.type = INTEL_PT_INSTRUCTION;
956 decoder->state.from_ip = decoder->ip;
957 decoder->state.to_ip = 0;
958 decoder->ip += intel_pt_insn->length;
959 err = INTEL_PT_RETURN;
963 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
964 /* Zero-length calls are excluded */
965 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
966 intel_pt_insn->rel) {
967 err = intel_pt_push(&decoder->stack, decoder->ip +
968 intel_pt_insn->length);
972 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
973 decoder->ret_addr = intel_pt_pop(&decoder->stack);
976 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
977 int cnt = decoder->no_progress++;
979 decoder->state.from_ip = decoder->ip;
980 decoder->ip += intel_pt_insn->length +
982 decoder->state.to_ip = decoder->ip;
983 err = INTEL_PT_RETURN;
986 * Check for being stuck in a loop. This can happen if a
987 * decoder error results in the decoder erroneously setting the
988 * ip to an address that is itself in an infinite loop that
989 * consumes no packets. When that happens, there must be an
990 * unconditional branch.
994 decoder->stuck_ip = decoder->state.to_ip;
995 decoder->stuck_ip_prd = 1;
996 decoder->stuck_ip_cnt = 1;
997 } else if (cnt > INTEL_PT_MAX_LOOPS ||
998 decoder->state.to_ip == decoder->stuck_ip) {
999 intel_pt_log_at("ERROR: Never-ending loop",
1000 decoder->state.to_ip);
1001 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1004 } else if (!--decoder->stuck_ip_cnt) {
1005 decoder->stuck_ip_prd += 1;
1006 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1007 decoder->stuck_ip = decoder->state.to_ip;
1010 goto out_no_progress;
1013 decoder->no_progress = 0;
1015 decoder->state.insn_op = intel_pt_insn->op;
1016 decoder->state.insn_len = intel_pt_insn->length;
1018 if (decoder->tx_flags & INTEL_PT_IN_TX)
1019 decoder->state.flags |= INTEL_PT_IN_TX;
1024 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1025 struct intel_pt_insn *intel_pt_insn,
1026 uint64_t ip, int err)
1028 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1029 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1030 ip == decoder->ip + intel_pt_insn->length;
1033 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1035 struct intel_pt_insn intel_pt_insn;
1039 ip = decoder->last_ip;
1042 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1043 if (err == INTEL_PT_RETURN)
1045 if (err == -EAGAIN ||
1046 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1047 if (decoder->set_fup_tx_flags) {
1048 decoder->set_fup_tx_flags = false;
1049 decoder->tx_flags = decoder->fup_tx_flags;
1050 decoder->state.type = INTEL_PT_TRANSACTION;
1051 decoder->state.from_ip = decoder->ip;
1052 decoder->state.to_ip = 0;
1053 decoder->state.flags = decoder->fup_tx_flags;
1058 decoder->set_fup_tx_flags = false;
1062 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1063 intel_pt_log_at("ERROR: Unexpected indirect branch",
1065 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1069 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1070 intel_pt_log_at("ERROR: Unexpected conditional branch",
1072 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1076 intel_pt_bug(decoder);
1080 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1082 struct intel_pt_insn intel_pt_insn;
1085 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1086 if (err == INTEL_PT_RETURN &&
1088 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1089 (decoder->state.type & INTEL_PT_BRANCH) &&
1090 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1091 /* Unconditional branch leaving filter region */
1092 decoder->no_progress = 0;
1093 decoder->pge = false;
1094 decoder->continuous_period = false;
1095 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1096 decoder->state.to_ip = 0;
1099 if (err == INTEL_PT_RETURN)
1104 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1105 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1106 decoder->pge = false;
1107 decoder->continuous_period = false;
1108 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1109 decoder->state.from_ip = decoder->ip;
1110 decoder->state.to_ip = 0;
1111 if (decoder->packet.count != 0)
1112 decoder->ip = decoder->last_ip;
1114 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1115 decoder->state.from_ip = decoder->ip;
1116 if (decoder->packet.count == 0) {
1117 decoder->state.to_ip = 0;
1119 decoder->state.to_ip = decoder->last_ip;
1120 decoder->ip = decoder->last_ip;
1126 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1127 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1130 if (decoder->pgd_ip &&
1131 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1132 decoder->pgd_ip(to_ip, decoder->data)) {
1133 /* Conditional branch leaving filter region */
1134 decoder->pge = false;
1135 decoder->continuous_period = false;
1136 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1137 decoder->ip = to_ip;
1138 decoder->state.from_ip = decoder->ip;
1139 decoder->state.to_ip = 0;
1142 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1144 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1148 return intel_pt_bug(decoder);
1151 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1153 struct intel_pt_insn intel_pt_insn;
1157 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1158 if (err == INTEL_PT_RETURN)
1163 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1164 if (!decoder->return_compression) {
1165 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1167 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1170 if (!decoder->ret_addr) {
1171 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1173 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1176 if (!(decoder->tnt.payload & BIT63)) {
1177 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1179 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1182 decoder->tnt.count -= 1;
1183 if (decoder->tnt.count)
1184 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1186 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1187 decoder->tnt.payload <<= 1;
1188 decoder->state.from_ip = decoder->ip;
1189 decoder->ip = decoder->ret_addr;
1190 decoder->state.to_ip = decoder->ip;
1194 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1195 /* Handle deferred TIPs */
1196 err = intel_pt_get_next_packet(decoder);
1199 if (decoder->packet.type != INTEL_PT_TIP ||
1200 decoder->packet.count == 0) {
1201 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1203 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1204 decoder->pkt_step = 0;
1207 intel_pt_set_last_ip(decoder);
1208 decoder->state.from_ip = decoder->ip;
1209 decoder->state.to_ip = decoder->last_ip;
1210 decoder->ip = decoder->last_ip;
1214 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1215 decoder->tnt.count -= 1;
1216 if (decoder->tnt.count)
1217 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1219 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1220 if (decoder->tnt.payload & BIT63) {
1221 decoder->tnt.payload <<= 1;
1222 decoder->state.from_ip = decoder->ip;
1223 decoder->ip += intel_pt_insn.length +
1225 decoder->state.to_ip = decoder->ip;
1228 /* Instruction sample for a non-taken branch */
1229 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1230 decoder->tnt.payload <<= 1;
1231 decoder->state.type = INTEL_PT_INSTRUCTION;
1232 decoder->state.from_ip = decoder->ip;
1233 decoder->state.to_ip = 0;
1234 decoder->ip += intel_pt_insn.length;
1237 decoder->ip += intel_pt_insn.length;
1238 if (!decoder->tnt.count) {
1239 decoder->sample_timestamp = decoder->timestamp;
1240 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
1243 decoder->tnt.payload <<= 1;
1247 return intel_pt_bug(decoder);
1251 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1253 unsigned int fup_tx_flags;
1256 fup_tx_flags = decoder->packet.payload &
1257 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1258 err = intel_pt_get_next_packet(decoder);
1261 if (decoder->packet.type == INTEL_PT_FUP) {
1262 decoder->fup_tx_flags = fup_tx_flags;
1263 decoder->set_fup_tx_flags = true;
1264 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1267 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1269 intel_pt_update_in_tx(decoder);
1274 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1278 decoder->have_tma = false;
1280 if (decoder->ref_timestamp) {
1281 timestamp = decoder->packet.payload |
1282 (decoder->ref_timestamp & (0xffULL << 56));
1283 if (timestamp < decoder->ref_timestamp) {
1284 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1285 timestamp += (1ULL << 56);
1287 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1288 timestamp -= (1ULL << 56);
1290 decoder->tsc_timestamp = timestamp;
1291 decoder->timestamp = timestamp;
1292 decoder->ref_timestamp = 0;
1293 decoder->timestamp_insn_cnt = 0;
1294 } else if (decoder->timestamp) {
1295 timestamp = decoder->packet.payload |
1296 (decoder->timestamp & (0xffULL << 56));
1297 decoder->tsc_timestamp = timestamp;
1298 if (timestamp < decoder->timestamp &&
1299 decoder->timestamp - timestamp < decoder->tsc_slip) {
1300 intel_pt_log_to("Suppressing backwards timestamp",
1302 timestamp = decoder->timestamp;
1304 if (timestamp < decoder->timestamp) {
1305 intel_pt_log_to("Wraparound timestamp", timestamp);
1306 timestamp += (1ULL << 56);
1307 decoder->tsc_timestamp = timestamp;
1309 decoder->timestamp = timestamp;
1310 decoder->timestamp_insn_cnt = 0;
1313 if (decoder->last_packet_type == INTEL_PT_CYC) {
1314 decoder->cyc_ref_timestamp = decoder->timestamp;
1315 decoder->cycle_cnt = 0;
1316 decoder->have_calc_cyc_to_tsc = false;
1317 intel_pt_calc_cyc_to_tsc(decoder, false);
1320 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1323 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1325 intel_pt_log("ERROR: Buffer overflow\n");
1326 intel_pt_clear_tx_flags(decoder);
1327 decoder->timestamp_insn_cnt = 0;
1328 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1329 decoder->overflow = true;
1333 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1335 uint32_t ctc = decoder->packet.payload;
1336 uint32_t fc = decoder->packet.count;
1337 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1339 if (!decoder->tsc_ctc_ratio_d)
1342 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1343 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1344 if (decoder->tsc_ctc_mult) {
1345 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1347 decoder->ctc_timestamp -= multdiv(ctc_rem,
1348 decoder->tsc_ctc_ratio_n,
1349 decoder->tsc_ctc_ratio_d);
1351 decoder->ctc_delta = 0;
1352 decoder->have_tma = true;
1353 decoder->fixup_last_mtc = true;
1354 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1355 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1358 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1361 uint32_t mtc, mtc_delta;
1363 if (!decoder->have_tma)
1366 mtc = decoder->packet.payload;
1368 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1369 decoder->fixup_last_mtc = false;
1370 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1371 &decoder->last_mtc);
1374 if (mtc > decoder->last_mtc)
1375 mtc_delta = mtc - decoder->last_mtc;
1377 mtc_delta = mtc + 256 - decoder->last_mtc;
1379 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1381 if (decoder->tsc_ctc_mult) {
1382 timestamp = decoder->ctc_timestamp +
1383 decoder->ctc_delta * decoder->tsc_ctc_mult;
1385 timestamp = decoder->ctc_timestamp +
1386 multdiv(decoder->ctc_delta,
1387 decoder->tsc_ctc_ratio_n,
1388 decoder->tsc_ctc_ratio_d);
1391 if (timestamp < decoder->timestamp)
1392 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1393 timestamp, decoder->timestamp);
1395 decoder->timestamp = timestamp;
1397 decoder->timestamp_insn_cnt = 0;
1398 decoder->last_mtc = mtc;
1400 if (decoder->last_packet_type == INTEL_PT_CYC) {
1401 decoder->cyc_ref_timestamp = decoder->timestamp;
1402 decoder->cycle_cnt = 0;
1403 decoder->have_calc_cyc_to_tsc = false;
1404 intel_pt_calc_cyc_to_tsc(decoder, true);
1408 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1410 unsigned int cbr = decoder->packet.payload;
1412 if (decoder->cbr == cbr)
1416 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1419 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1421 uint64_t timestamp = decoder->cyc_ref_timestamp;
1423 decoder->have_cyc = true;
1425 decoder->cycle_cnt += decoder->packet.payload;
1427 if (!decoder->cyc_ref_timestamp)
1430 if (decoder->have_calc_cyc_to_tsc)
1431 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1432 else if (decoder->cbr)
1433 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1437 if (timestamp < decoder->timestamp)
1438 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1439 timestamp, decoder->timestamp);
1441 decoder->timestamp = timestamp;
1443 decoder->timestamp_insn_cnt = 0;
1446 /* Walk PSB+ packets when already in sync. */
1447 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1452 err = intel_pt_get_next_packet(decoder);
1456 switch (decoder->packet.type) {
1457 case INTEL_PT_PSBEND:
1460 case INTEL_PT_TIP_PGD:
1461 case INTEL_PT_TIP_PGE:
1464 case INTEL_PT_TRACESTOP:
1467 decoder->have_tma = false;
1468 intel_pt_log("ERROR: Unexpected packet\n");
1472 return intel_pt_overflow(decoder);
1475 intel_pt_calc_tsc_timestamp(decoder);
1479 intel_pt_calc_tma(decoder);
1483 intel_pt_calc_cbr(decoder);
1486 case INTEL_PT_MODE_EXEC:
1487 decoder->exec_mode = decoder->packet.payload;
1491 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1495 decoder->pge = true;
1496 if (decoder->packet.count)
1497 intel_pt_set_last_ip(decoder);
1500 case INTEL_PT_MODE_TSX:
1501 intel_pt_update_in_tx(decoder);
1505 intel_pt_calc_mtc_timestamp(decoder);
1506 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1507 decoder->state.type |= INTEL_PT_INSTRUCTION;
1511 intel_pt_calc_cyc_timestamp(decoder);
1523 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1527 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1528 decoder->tx_flags = 0;
1529 decoder->state.flags &= ~INTEL_PT_IN_TX;
1530 decoder->state.flags |= INTEL_PT_ABORT_TX;
1532 decoder->state.flags |= INTEL_PT_ASYNC;
1536 err = intel_pt_get_next_packet(decoder);
1540 switch (decoder->packet.type) {
1543 case INTEL_PT_TRACESTOP:
1547 case INTEL_PT_MODE_TSX:
1549 case INTEL_PT_PSBEND:
1550 intel_pt_log("ERROR: Missing TIP after FUP\n");
1551 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1552 decoder->pkt_step = 0;
1556 intel_pt_calc_cbr(decoder);
1560 return intel_pt_overflow(decoder);
1562 case INTEL_PT_TIP_PGD:
1563 decoder->state.from_ip = decoder->ip;
1564 decoder->state.to_ip = 0;
1565 if (decoder->packet.count != 0) {
1566 intel_pt_set_ip(decoder);
1567 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1570 decoder->pge = false;
1571 decoder->continuous_period = false;
1574 case INTEL_PT_TIP_PGE:
1575 decoder->pge = true;
1576 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1578 decoder->state.from_ip = 0;
1579 if (decoder->packet.count == 0) {
1580 decoder->state.to_ip = 0;
1582 intel_pt_set_ip(decoder);
1583 decoder->state.to_ip = decoder->ip;
1588 decoder->state.from_ip = decoder->ip;
1589 if (decoder->packet.count == 0) {
1590 decoder->state.to_ip = 0;
1592 intel_pt_set_ip(decoder);
1593 decoder->state.to_ip = decoder->ip;
1598 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1602 intel_pt_calc_mtc_timestamp(decoder);
1603 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1604 decoder->state.type |= INTEL_PT_INSTRUCTION;
1608 intel_pt_calc_cyc_timestamp(decoder);
1611 case INTEL_PT_MODE_EXEC:
1612 decoder->exec_mode = decoder->packet.payload;
1621 return intel_pt_bug(decoder);
1626 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1628 bool no_tip = false;
1632 err = intel_pt_get_next_packet(decoder);
1636 switch (decoder->packet.type) {
1638 if (!decoder->packet.count)
1640 decoder->tnt = decoder->packet;
1641 decoder->pkt_state = INTEL_PT_STATE_TNT;
1642 err = intel_pt_walk_tnt(decoder);
1647 case INTEL_PT_TIP_PGD:
1648 if (decoder->packet.count != 0)
1649 intel_pt_set_last_ip(decoder);
1650 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1651 return intel_pt_walk_tip(decoder);
1653 case INTEL_PT_TIP_PGE: {
1654 decoder->pge = true;
1655 if (decoder->packet.count == 0) {
1656 intel_pt_log_at("Skipping zero TIP.PGE",
1660 intel_pt_set_ip(decoder);
1661 decoder->state.from_ip = 0;
1662 decoder->state.to_ip = decoder->ip;
1667 return intel_pt_overflow(decoder);
1670 if (decoder->packet.count != 0)
1671 intel_pt_set_last_ip(decoder);
1672 decoder->pkt_state = INTEL_PT_STATE_TIP;
1673 return intel_pt_walk_tip(decoder);
1676 if (decoder->packet.count == 0) {
1677 intel_pt_log_at("Skipping zero FUP",
1682 intel_pt_set_last_ip(decoder);
1683 err = intel_pt_walk_fup(decoder);
1684 if (err != -EAGAIN) {
1688 decoder->pkt_state =
1689 INTEL_PT_STATE_FUP_NO_TIP;
1691 decoder->pkt_state = INTEL_PT_STATE_FUP;
1698 return intel_pt_walk_fup_tip(decoder);
1700 case INTEL_PT_TRACESTOP:
1701 decoder->pge = false;
1702 decoder->continuous_period = false;
1703 intel_pt_clear_tx_flags(decoder);
1704 decoder->have_tma = false;
1708 decoder->last_ip = 0;
1709 decoder->have_last_ip = true;
1710 intel_pt_clear_stack(&decoder->stack);
1711 err = intel_pt_walk_psbend(decoder);
1719 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1723 intel_pt_calc_mtc_timestamp(decoder);
1724 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1727 * Ensure that there has been an instruction since the
1730 if (!decoder->mtc_insn)
1732 decoder->mtc_insn = false;
1733 /* Ensure that there is a timestamp */
1734 if (!decoder->timestamp)
1736 decoder->state.type = INTEL_PT_INSTRUCTION;
1737 decoder->state.from_ip = decoder->ip;
1738 decoder->state.to_ip = 0;
1739 decoder->mtc_insn = false;
1743 intel_pt_calc_tsc_timestamp(decoder);
1747 intel_pt_calc_tma(decoder);
1751 intel_pt_calc_cyc_timestamp(decoder);
1755 intel_pt_calc_cbr(decoder);
1758 case INTEL_PT_MODE_EXEC:
1759 decoder->exec_mode = decoder->packet.payload;
1762 case INTEL_PT_MODE_TSX:
1763 /* MODE_TSX need not be followed by FUP */
1764 if (!decoder->pge) {
1765 intel_pt_update_in_tx(decoder);
1768 err = intel_pt_mode_tsx(decoder, &no_tip);
1773 case INTEL_PT_BAD: /* Does not happen */
1774 return intel_pt_bug(decoder);
1776 case INTEL_PT_PSBEND:
1783 return intel_pt_bug(decoder);
1788 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1790 return decoder->packet.count &&
1791 (decoder->have_last_ip || decoder->packet.count == 3 ||
1792 decoder->packet.count == 6);
1795 /* Walk PSB+ packets to get in sync. */
1796 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1801 err = intel_pt_get_next_packet(decoder);
1805 switch (decoder->packet.type) {
1806 case INTEL_PT_TIP_PGD:
1807 decoder->continuous_period = false;
1809 case INTEL_PT_TIP_PGE:
1811 intel_pt_log("ERROR: Unexpected packet\n");
1815 decoder->pge = true;
1816 if (intel_pt_have_ip(decoder)) {
1817 uint64_t current_ip = decoder->ip;
1819 intel_pt_set_ip(decoder);
1821 intel_pt_log_to("Setting IP",
1827 intel_pt_calc_mtc_timestamp(decoder);
1831 intel_pt_calc_tsc_timestamp(decoder);
1835 intel_pt_calc_tma(decoder);
1839 intel_pt_calc_cyc_timestamp(decoder);
1843 intel_pt_calc_cbr(decoder);
1847 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1850 case INTEL_PT_MODE_EXEC:
1851 decoder->exec_mode = decoder->packet.payload;
1854 case INTEL_PT_MODE_TSX:
1855 intel_pt_update_in_tx(decoder);
1858 case INTEL_PT_TRACESTOP:
1859 decoder->pge = false;
1860 decoder->continuous_period = false;
1861 intel_pt_clear_tx_flags(decoder);
1865 decoder->have_tma = false;
1866 intel_pt_log("ERROR: Unexpected packet\n");
1868 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1870 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1873 case INTEL_PT_BAD: /* Does not happen */
1874 return intel_pt_bug(decoder);
1877 return intel_pt_overflow(decoder);
1879 case INTEL_PT_PSBEND:
1892 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1897 err = intel_pt_get_next_packet(decoder);
1901 switch (decoder->packet.type) {
1902 case INTEL_PT_TIP_PGD:
1903 decoder->continuous_period = false;
1905 case INTEL_PT_TIP_PGE:
1907 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1908 if (intel_pt_have_ip(decoder))
1909 intel_pt_set_ip(decoder);
1915 if (intel_pt_have_ip(decoder))
1916 intel_pt_set_ip(decoder);
1922 intel_pt_calc_mtc_timestamp(decoder);
1926 intel_pt_calc_tsc_timestamp(decoder);
1930 intel_pt_calc_tma(decoder);
1934 intel_pt_calc_cyc_timestamp(decoder);
1938 intel_pt_calc_cbr(decoder);
1942 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1945 case INTEL_PT_MODE_EXEC:
1946 decoder->exec_mode = decoder->packet.payload;
1949 case INTEL_PT_MODE_TSX:
1950 intel_pt_update_in_tx(decoder);
1954 return intel_pt_overflow(decoder);
1956 case INTEL_PT_BAD: /* Does not happen */
1957 return intel_pt_bug(decoder);
1959 case INTEL_PT_TRACESTOP:
1960 decoder->pge = false;
1961 decoder->continuous_period = false;
1962 intel_pt_clear_tx_flags(decoder);
1963 decoder->have_tma = false;
1967 decoder->last_ip = 0;
1968 decoder->have_last_ip = true;
1969 intel_pt_clear_stack(&decoder->stack);
1970 err = intel_pt_walk_psb(decoder);
1974 /* Do not have a sample */
1975 decoder->state.type = 0;
1981 case INTEL_PT_PSBEND:
1991 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1995 decoder->set_fup_tx_flags = false;
1997 intel_pt_log("Scanning for full IP\n");
1998 err = intel_pt_walk_to_ip(decoder);
2002 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2003 decoder->overflow = false;
2005 decoder->state.from_ip = 0;
2006 decoder->state.to_ip = decoder->ip;
2007 intel_pt_log_to("Setting IP", decoder->ip);
2012 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2014 const unsigned char *end = decoder->buf + decoder->len;
2017 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2018 if (i > decoder->len)
2020 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2026 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2028 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2029 const char *psb = INTEL_PT_PSB_STR;
2031 if (rest_psb > decoder->len ||
2032 memcmp(decoder->buf, psb + part_psb, rest_psb))
2038 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2043 decoder->pos += decoder->len;
2046 ret = intel_pt_get_next_data(decoder);
2050 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2054 decoder->pos -= part_psb;
2055 decoder->next_buf = decoder->buf + rest_psb;
2056 decoder->next_len = decoder->len - rest_psb;
2057 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2058 decoder->buf = decoder->temp_buf;
2059 decoder->len = INTEL_PT_PSB_LEN;
2064 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2066 unsigned char *next;
2069 intel_pt_log("Scanning for PSB\n");
2071 if (!decoder->len) {
2072 ret = intel_pt_get_next_data(decoder);
2077 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2082 part_psb = intel_pt_part_psb(decoder);
2084 ret = intel_pt_get_split_psb(decoder, part_psb);
2088 decoder->pos += decoder->len;
2094 decoder->pkt_step = next - decoder->buf;
2095 return intel_pt_get_next_packet(decoder);
2099 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2103 decoder->pge = false;
2104 decoder->continuous_period = false;
2105 decoder->have_last_ip = false;
2106 decoder->last_ip = 0;
2108 intel_pt_clear_stack(&decoder->stack);
2110 err = intel_pt_scan_for_psb(decoder);
2114 decoder->have_last_ip = true;
2115 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2117 err = intel_pt_walk_psb(decoder);
2122 decoder->state.type = 0; /* Do not have a sample */
2123 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2125 return intel_pt_sync_ip(decoder);
2131 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2133 uint64_t est = decoder->sample_insn_cnt << 1;
2135 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2138 est *= decoder->max_non_turbo_ratio;
2139 est /= decoder->cbr;
2141 return decoder->sample_timestamp + est;
2144 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2149 decoder->state.type = INTEL_PT_BRANCH;
2150 decoder->state.flags = 0;
2152 switch (decoder->pkt_state) {
2153 case INTEL_PT_STATE_NO_PSB:
2154 err = intel_pt_sync(decoder);
2156 case INTEL_PT_STATE_NO_IP:
2157 decoder->have_last_ip = false;
2158 decoder->last_ip = 0;
2161 case INTEL_PT_STATE_ERR_RESYNC:
2162 err = intel_pt_sync_ip(decoder);
2164 case INTEL_PT_STATE_IN_SYNC:
2165 err = intel_pt_walk_trace(decoder);
2167 case INTEL_PT_STATE_TNT:
2168 case INTEL_PT_STATE_TNT_CONT:
2169 err = intel_pt_walk_tnt(decoder);
2171 err = intel_pt_walk_trace(decoder);
2173 case INTEL_PT_STATE_TIP:
2174 case INTEL_PT_STATE_TIP_PGD:
2175 err = intel_pt_walk_tip(decoder);
2177 case INTEL_PT_STATE_FUP:
2178 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2179 err = intel_pt_walk_fup(decoder);
2181 err = intel_pt_walk_fup_tip(decoder);
2183 decoder->pkt_state = INTEL_PT_STATE_FUP;
2185 case INTEL_PT_STATE_FUP_NO_TIP:
2186 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2187 err = intel_pt_walk_fup(decoder);
2189 err = intel_pt_walk_trace(decoder);
2192 err = intel_pt_bug(decoder);
2195 } while (err == -ENOLINK);
2198 decoder->state.err = intel_pt_ext_err(err);
2199 decoder->state.from_ip = decoder->ip;
2200 decoder->sample_timestamp = decoder->timestamp;
2201 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2203 decoder->state.err = 0;
2204 if (intel_pt_sample_time(decoder->pkt_state)) {
2205 decoder->sample_timestamp = decoder->timestamp;
2206 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2210 decoder->state.timestamp = decoder->sample_timestamp;
2211 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2212 decoder->state.cr3 = decoder->cr3;
2213 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2215 return &decoder->state;
2219 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2220 * @buf: pointer to buffer pointer
2221 * @len: size of buffer
2223 * Updates the buffer pointer to point to the start of the next PSB packet if
2224 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2225 * @len is adjusted accordingly.
2227 * Return: %true if a PSB packet is found, %false otherwise.
2229 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2231 unsigned char *next;
2233 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2235 *len -= next - *buf;
2243 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2245 * @buf: pointer to buffer pointer
2246 * @len: size of buffer
2248 * Updates the buffer pointer to point to the start of the following PSB packet
2249 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2250 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2252 * Return: %true if a PSB packet is found, %false otherwise.
2254 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2256 unsigned char *next;
2261 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2263 *len -= next - *buf;
2271 * intel_pt_last_psb - find the last PSB packet in a buffer.
2273 * @len: size of buffer
2275 * This function finds the last PSB in a buffer.
2277 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2279 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2281 const char *n = INTEL_PT_PSB_STR;
2285 if (len < INTEL_PT_PSB_LEN)
2288 k = len - INTEL_PT_PSB_LEN + 1;
2290 p = memrchr(buf, n[0], k);
2293 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2302 * intel_pt_next_tsc - find and return next TSC.
2304 * @len: size of buffer
2305 * @tsc: TSC value returned
2306 * @rem: returns remaining size when TSC is found
2308 * Find a TSC packet in @buf and return the TSC value. This function assumes
2309 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2310 * PSBEND packet is found.
2312 * Return: %true if TSC is found, false otherwise.
2314 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2317 struct intel_pt_pkt packet;
2321 ret = intel_pt_get_packet(buf, len, &packet);
2324 if (packet.type == INTEL_PT_TSC) {
2325 *tsc = packet.payload;
2329 if (packet.type == INTEL_PT_PSBEND)
2338 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2339 * @tsc1: first TSC to compare
2340 * @tsc2: second TSC to compare
2342 * This function compares 7-byte TSC values allowing for the possibility that
2343 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2344 * around so for that purpose this function assumes the absolute difference is
2345 * less than half the maximum difference.
2347 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2350 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2352 const uint64_t halfway = (1ULL << 55);
2358 if (tsc2 - tsc1 < halfway)
2363 if (tsc1 - tsc2 < halfway)
2370 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2373 * adj_for_padding - adjust overlap to account for padding.
2374 * @buf_b: second buffer
2375 * @buf_a: first buffer
2376 * @len_a: size of first buffer
2378 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2381 * Return: A pointer into @buf_b from where non-overlapped data starts
2383 static unsigned char *adj_for_padding(unsigned char *buf_b,
2384 unsigned char *buf_a, size_t len_a)
2386 unsigned char *p = buf_b - MAX_PADDING;
2387 unsigned char *q = buf_a + len_a - MAX_PADDING;
2390 for (i = MAX_PADDING; i; i--, p++, q++) {
2399 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2401 * @buf_a: first buffer
2402 * @len_a: size of first buffer
2403 * @buf_b: second buffer
2404 * @len_b: size of second buffer
2405 * @consecutive: returns true if there is data in buf_b that is consecutive
2408 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2409 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2410 * walk forward in @buf_b until a later TSC is found. A precondition is that
2411 * @buf_a and @buf_b are positioned at a PSB.
2413 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2414 * @buf_b + @len_b if there is no non-overlapped data.
2416 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2418 unsigned char *buf_b,
2419 size_t len_b, bool *consecutive)
2421 uint64_t tsc_a, tsc_b;
2423 size_t len, rem_a, rem_b;
2425 p = intel_pt_last_psb(buf_a, len_a);
2427 return buf_b; /* No PSB in buf_a => no overlap */
2429 len = len_a - (p - buf_a);
2430 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2431 /* The last PSB+ in buf_a is incomplete, so go back one more */
2433 p = intel_pt_last_psb(buf_a, len_a);
2435 return buf_b; /* No full PSB+ => assume no overlap */
2436 len = len_a - (p - buf_a);
2437 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2438 return buf_b; /* No TSC in buf_a => assume no overlap */
2442 /* Ignore PSB+ with no TSC */
2443 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2444 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2446 /* Same TSC, so buffers are consecutive */
2447 if (!cmp && rem_b >= rem_a) {
2448 unsigned char *start;
2450 *consecutive = true;
2451 start = buf_b + len_b - (rem_b - rem_a);
2452 return adj_for_padding(start, buf_a, len_a);
2455 return buf_b; /* tsc_a < tsc_b => no overlap */
2458 if (!intel_pt_step_psb(&buf_b, &len_b))
2459 return buf_b + len_b; /* No PSB in buf_b => no data */
2464 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2465 * @buf_a: first buffer
2466 * @len_a: size of first buffer
2467 * @buf_b: second buffer
2468 * @len_b: size of second buffer
2469 * @have_tsc: can use TSC packets to detect overlap
2470 * @consecutive: returns true if there is data in buf_b that is consecutive
2473 * When trace samples or snapshots are recorded there is the possibility that
2474 * the data overlaps. Note that, for the purposes of decoding, data is only
2475 * useful if it begins with a PSB packet.
2477 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2478 * @buf_b + @len_b if there is no non-overlapped data.
2480 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2481 unsigned char *buf_b, size_t len_b,
2482 bool have_tsc, bool *consecutive)
2484 unsigned char *found;
2486 /* Buffer 'b' must start at PSB so throw away everything before that */
2487 if (!intel_pt_next_psb(&buf_b, &len_b))
2488 return buf_b + len_b; /* No PSB */
2490 if (!intel_pt_next_psb(&buf_a, &len_a))
2491 return buf_b; /* No overlap */
2494 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2501 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2502 * we can ignore the first part of buffer 'a'.
2504 while (len_b < len_a) {
2505 if (!intel_pt_step_psb(&buf_a, &len_a))
2506 return buf_b; /* No overlap */
2509 /* Now len_b >= len_a */
2511 /* Potential overlap so check the bytes */
2512 found = memmem(buf_a, len_a, buf_b, len_a);
2514 *consecutive = true;
2515 return adj_for_padding(buf_b + len_a, buf_a, len_a);
2518 /* Try again at next PSB in buffer 'a' */
2519 if (!intel_pt_step_psb(&buf_a, &len_a))
2520 return buf_b; /* No overlap */