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;
120 enum intel_pt_param_flags flags;
126 uint64_t tsc_timestamp;
127 uint64_t ref_timestamp;
128 uint64_t sample_timestamp;
130 uint64_t ctc_timestamp;
133 uint64_t cyc_ref_timestamp;
135 uint32_t tsc_ctc_ratio_n;
136 uint32_t tsc_ctc_ratio_d;
137 uint32_t tsc_ctc_mult;
139 uint32_t ctc_rem_mask;
141 struct intel_pt_stack stack;
142 enum intel_pt_pkt_state pkt_state;
143 struct intel_pt_pkt packet;
144 struct intel_pt_pkt tnt;
147 int last_packet_type;
149 unsigned int cbr_seen;
150 unsigned int max_non_turbo_ratio;
151 double max_non_turbo_ratio_fp;
152 double cbr_cyc_to_tsc;
153 double calc_cyc_to_tsc;
154 bool have_calc_cyc_to_tsc;
156 unsigned int insn_bytes;
158 enum intel_pt_period_type period_type;
159 uint64_t tot_insn_cnt;
160 uint64_t period_insn_cnt;
161 uint64_t period_mask;
162 uint64_t period_ticks;
163 uint64_t last_masked_timestamp;
164 uint64_t tot_cyc_cnt;
165 uint64_t sample_tot_cyc_cnt;
166 bool continuous_period;
168 bool set_fup_tx_flags;
174 unsigned int fup_tx_flags;
175 unsigned int tx_flags;
176 uint64_t fup_ptw_payload;
177 uint64_t fup_mwait_payload;
178 uint64_t fup_pwre_payload;
179 uint64_t cbr_payload;
180 uint64_t timestamp_insn_cnt;
181 uint64_t sample_insn_cnt;
186 const unsigned char *next_buf;
188 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
191 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
195 for (i = 0; x != 1; i++)
201 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
203 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
206 period = intel_pt_lower_power_of_2(decoder->period);
207 decoder->period_mask = ~(period - 1);
208 decoder->period_ticks = period;
212 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
216 return (t / d) * n + ((t % d) * n) / d;
219 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
221 struct intel_pt_decoder *decoder;
223 if (!params->get_trace || !params->walk_insn)
226 decoder = zalloc(sizeof(struct intel_pt_decoder));
230 decoder->get_trace = params->get_trace;
231 decoder->walk_insn = params->walk_insn;
232 decoder->pgd_ip = params->pgd_ip;
233 decoder->data = params->data;
234 decoder->return_compression = params->return_compression;
235 decoder->branch_enable = params->branch_enable;
237 decoder->flags = params->flags;
239 decoder->period = params->period;
240 decoder->period_type = params->period_type;
242 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
243 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
245 intel_pt_setup_period(decoder);
247 decoder->mtc_shift = params->mtc_period;
248 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
250 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
251 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
253 if (!decoder->tsc_ctc_ratio_n)
254 decoder->tsc_ctc_ratio_d = 0;
256 if (decoder->tsc_ctc_ratio_d) {
257 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
258 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
259 decoder->tsc_ctc_ratio_d;
263 * A TSC packet can slip past MTC packets so that the timestamp appears
264 * to go backwards. One estimate is that can be up to about 40 CPU
265 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
266 * slippage an order of magnitude more to be on the safe side.
268 decoder->tsc_slip = 0x10000;
270 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
271 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
272 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
273 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
274 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
279 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
281 struct intel_pt_blk *blk = stack->blk;
283 stack->blk = blk->prev;
290 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
295 intel_pt_pop_blk(stack);
298 stack->pos = INTEL_PT_BLK_SIZE;
300 return stack->blk->ip[--stack->pos];
303 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
305 struct intel_pt_blk *blk;
311 blk = malloc(sizeof(struct intel_pt_blk));
316 blk->prev = stack->blk;
322 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
326 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
327 err = intel_pt_alloc_blk(stack);
332 stack->blk->ip[stack->pos++] = ip;
336 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
339 intel_pt_pop_blk(stack);
343 static void intel_pt_free_stack(struct intel_pt_stack *stack)
345 intel_pt_clear_stack(stack);
347 zfree(&stack->spare);
350 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
352 intel_pt_free_stack(&decoder->stack);
356 static int intel_pt_ext_err(int code)
360 return INTEL_PT_ERR_NOMEM;
362 return INTEL_PT_ERR_INTERN;
364 return INTEL_PT_ERR_BADPKT;
366 return INTEL_PT_ERR_NODATA;
368 return INTEL_PT_ERR_NOINSN;
370 return INTEL_PT_ERR_MISMAT;
372 return INTEL_PT_ERR_OVR;
374 return INTEL_PT_ERR_LOST;
376 return INTEL_PT_ERR_NELOOP;
378 return INTEL_PT_ERR_UNK;
382 static const char *intel_pt_err_msgs[] = {
383 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
384 [INTEL_PT_ERR_INTERN] = "Internal error",
385 [INTEL_PT_ERR_BADPKT] = "Bad packet",
386 [INTEL_PT_ERR_NODATA] = "No more data",
387 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
388 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
389 [INTEL_PT_ERR_OVR] = "Overflow packet",
390 [INTEL_PT_ERR_LOST] = "Lost trace data",
391 [INTEL_PT_ERR_UNK] = "Unknown error!",
392 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
395 int intel_pt__strerror(int code, char *buf, size_t buflen)
397 if (code < 1 || code >= INTEL_PT_ERR_MAX)
398 code = INTEL_PT_ERR_UNK;
399 strlcpy(buf, intel_pt_err_msgs[code], buflen);
403 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
408 switch (packet->count) {
410 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
414 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
418 ip = packet->payload;
419 /* Sign-extend 6-byte ip */
420 if (ip & (uint64_t)0x800000000000ULL)
421 ip |= (uint64_t)0xffff000000000000ULL;
424 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
428 ip = packet->payload;
437 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
439 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
440 decoder->have_last_ip = true;
443 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
445 intel_pt_set_last_ip(decoder);
446 decoder->ip = decoder->last_ip;
449 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
451 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
455 static int intel_pt_bug(struct intel_pt_decoder *decoder)
457 intel_pt_log("ERROR: Internal error\n");
458 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
462 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
464 decoder->tx_flags = 0;
467 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
469 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
472 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
474 intel_pt_clear_tx_flags(decoder);
475 decoder->have_tma = false;
476 decoder->pkt_len = 1;
477 decoder->pkt_step = 1;
478 intel_pt_decoder_log_packet(decoder);
479 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
480 intel_pt_log("ERROR: Bad packet\n");
481 decoder->pkt_state = INTEL_PT_STATE_ERR1;
486 static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
488 decoder->sample_timestamp = decoder->timestamp;
489 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
492 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
494 struct intel_pt_buffer buffer = { .buf = 0, };
497 decoder->pkt_step = 0;
499 intel_pt_log("Getting more data\n");
500 ret = decoder->get_trace(&buffer, decoder->data);
503 decoder->buf = buffer.buf;
504 decoder->len = buffer.len;
506 intel_pt_log("No more data\n");
509 if (!buffer.consecutive) {
511 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
512 decoder->ref_timestamp = buffer.ref_timestamp;
513 decoder->timestamp = 0;
514 decoder->have_tma = false;
515 decoder->state.trace_nr = buffer.trace_nr;
516 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
517 decoder->ref_timestamp);
524 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
526 if (!decoder->next_buf)
527 return intel_pt_get_data(decoder);
529 decoder->buf = decoder->next_buf;
530 decoder->len = decoder->next_len;
531 decoder->next_buf = 0;
532 decoder->next_len = 0;
536 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
538 unsigned char *buf = decoder->temp_buf;
539 size_t old_len, len, n;
542 old_len = decoder->len;
544 memcpy(buf, decoder->buf, len);
546 ret = intel_pt_get_data(decoder);
548 decoder->pos += old_len;
549 return ret < 0 ? ret : -EINVAL;
552 n = INTEL_PT_PKT_MAX_SZ - len;
553 if (n > decoder->len)
555 memcpy(buf + len, decoder->buf, n);
558 ret = intel_pt_get_packet(buf, len, &decoder->packet);
559 if (ret < (int)old_len) {
560 decoder->next_buf = decoder->buf;
561 decoder->next_len = decoder->len;
563 decoder->len = old_len;
564 return intel_pt_bad_packet(decoder);
567 decoder->next_buf = decoder->buf + (ret - old_len);
568 decoder->next_len = decoder->len - (ret - old_len);
576 struct intel_pt_pkt_info {
577 struct intel_pt_decoder *decoder;
578 struct intel_pt_pkt packet;
581 int last_packet_type;
585 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
587 /* Lookahead packets in current buffer */
588 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
589 intel_pt_pkt_cb_t cb, void *data)
591 struct intel_pt_pkt_info pkt_info;
592 const unsigned char *buf = decoder->buf;
593 size_t len = decoder->len;
596 pkt_info.decoder = decoder;
597 pkt_info.pos = decoder->pos;
598 pkt_info.pkt_len = decoder->pkt_step;
599 pkt_info.last_packet_type = decoder->last_packet_type;
600 pkt_info.data = data;
604 pkt_info.pos += pkt_info.pkt_len;
605 buf += pkt_info.pkt_len;
606 len -= pkt_info.pkt_len;
609 return INTEL_PT_NEED_MORE_BYTES;
611 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
613 return INTEL_PT_NEED_MORE_BYTES;
617 pkt_info.pkt_len = ret;
618 } while (pkt_info.packet.type == INTEL_PT_PAD);
624 pkt_info.last_packet_type = pkt_info.packet.type;
628 struct intel_pt_calc_cyc_to_tsc_info {
632 uint64_t ctc_timestamp;
634 uint64_t tsc_timestamp;
639 double cbr_cyc_to_tsc;
643 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
644 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
645 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
646 * packet by copying the missing bits from the current MTC assuming the least
647 * difference between the two, and that the current MTC comes after last_mtc.
649 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
652 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
653 uint32_t mask = ~(first_missing_bit - 1);
655 *last_mtc |= mtc & mask;
656 if (*last_mtc >= mtc) {
657 *last_mtc -= first_missing_bit;
662 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
664 struct intel_pt_decoder *decoder = pkt_info->decoder;
665 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
669 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
671 switch (pkt_info->packet.type) {
673 case INTEL_PT_TIP_PGE:
678 case INTEL_PT_MODE_EXEC:
679 case INTEL_PT_MODE_TSX:
680 case INTEL_PT_PSBEND:
684 case INTEL_PT_PTWRITE:
685 case INTEL_PT_PTWRITE_IP:
692 mtc = pkt_info->packet.payload;
693 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
694 data->fixup_last_mtc = false;
695 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
698 if (mtc > data->last_mtc)
699 mtc_delta = mtc - data->last_mtc;
701 mtc_delta = mtc + 256 - data->last_mtc;
702 data->ctc_delta += mtc_delta << decoder->mtc_shift;
703 data->last_mtc = mtc;
705 if (decoder->tsc_ctc_mult) {
706 timestamp = data->ctc_timestamp +
707 data->ctc_delta * decoder->tsc_ctc_mult;
709 timestamp = data->ctc_timestamp +
710 multdiv(data->ctc_delta,
711 decoder->tsc_ctc_ratio_n,
712 decoder->tsc_ctc_ratio_d);
715 if (timestamp < data->timestamp)
718 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
719 data->timestamp = timestamp;
727 * For now, do not support using TSC packets - refer
728 * intel_pt_calc_cyc_to_tsc().
732 timestamp = pkt_info->packet.payload |
733 (data->timestamp & (0xffULL << 56));
734 if (data->from_mtc && timestamp < data->timestamp &&
735 data->timestamp - timestamp < decoder->tsc_slip)
737 if (timestamp < data->timestamp)
738 timestamp += (1ULL << 56);
739 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
742 data->tsc_timestamp = timestamp;
743 data->timestamp = timestamp;
752 if (!decoder->tsc_ctc_ratio_d)
755 ctc = pkt_info->packet.payload;
756 fc = pkt_info->packet.count;
757 ctc_rem = ctc & decoder->ctc_rem_mask;
759 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
761 data->ctc_timestamp = data->tsc_timestamp - fc;
762 if (decoder->tsc_ctc_mult) {
763 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
765 data->ctc_timestamp -=
766 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
767 decoder->tsc_ctc_ratio_d);
771 data->have_tma = true;
772 data->fixup_last_mtc = true;
777 data->cycle_cnt += pkt_info->packet.payload;
781 cbr = pkt_info->packet.payload;
782 if (data->cbr && data->cbr != cbr)
785 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
788 case INTEL_PT_TIP_PGD:
789 case INTEL_PT_TRACESTOP:
790 case INTEL_PT_EXSTOP:
791 case INTEL_PT_EXSTOP_IP:
796 case INTEL_PT_BAD: /* Does not happen */
801 if (!data->cbr && decoder->cbr) {
802 data->cbr = decoder->cbr;
803 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
806 if (!data->cycle_cnt)
809 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
811 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
812 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
813 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
814 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
818 decoder->calc_cyc_to_tsc = cyc_to_tsc;
819 decoder->have_calc_cyc_to_tsc = true;
822 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
823 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
825 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
826 cyc_to_tsc, pkt_info->pos);
832 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
835 struct intel_pt_calc_cyc_to_tsc_info data = {
838 .last_mtc = decoder->last_mtc,
839 .ctc_timestamp = decoder->ctc_timestamp,
840 .ctc_delta = decoder->ctc_delta,
841 .tsc_timestamp = decoder->tsc_timestamp,
842 .timestamp = decoder->timestamp,
843 .have_tma = decoder->have_tma,
844 .fixup_last_mtc = decoder->fixup_last_mtc,
845 .from_mtc = from_mtc,
850 * For now, do not support using TSC packets for at least the reasons:
851 * 1) timing might have stopped
852 * 2) TSC packets within PSB+ can slip against CYC packets
857 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
860 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
864 decoder->last_packet_type = decoder->packet.type;
867 decoder->pos += decoder->pkt_step;
868 decoder->buf += decoder->pkt_step;
869 decoder->len -= decoder->pkt_step;
872 ret = intel_pt_get_next_data(decoder);
877 ret = intel_pt_get_packet(decoder->buf, decoder->len,
879 if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
880 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
881 ret = intel_pt_get_split_packet(decoder);
886 return intel_pt_bad_packet(decoder);
888 decoder->pkt_len = ret;
889 decoder->pkt_step = ret;
890 intel_pt_decoder_log_packet(decoder);
891 } while (decoder->packet.type == INTEL_PT_PAD);
896 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
898 uint64_t timestamp, masked_timestamp;
900 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
901 masked_timestamp = timestamp & decoder->period_mask;
902 if (decoder->continuous_period) {
903 if (masked_timestamp > decoder->last_masked_timestamp)
907 masked_timestamp = timestamp & decoder->period_mask;
908 if (masked_timestamp > decoder->last_masked_timestamp) {
909 decoder->last_masked_timestamp = masked_timestamp;
910 decoder->continuous_period = true;
914 if (masked_timestamp < decoder->last_masked_timestamp)
915 return decoder->period_ticks;
917 return decoder->period_ticks - (timestamp - masked_timestamp);
920 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
922 switch (decoder->period_type) {
923 case INTEL_PT_PERIOD_INSTRUCTIONS:
924 return decoder->period - decoder->period_insn_cnt;
925 case INTEL_PT_PERIOD_TICKS:
926 return intel_pt_next_period(decoder);
927 case INTEL_PT_PERIOD_NONE:
928 case INTEL_PT_PERIOD_MTC:
934 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
936 uint64_t timestamp, masked_timestamp;
938 switch (decoder->period_type) {
939 case INTEL_PT_PERIOD_INSTRUCTIONS:
940 decoder->period_insn_cnt = 0;
942 case INTEL_PT_PERIOD_TICKS:
943 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
944 masked_timestamp = timestamp & decoder->period_mask;
945 if (masked_timestamp > decoder->last_masked_timestamp)
946 decoder->last_masked_timestamp = masked_timestamp;
948 decoder->last_masked_timestamp += decoder->period_ticks;
950 case INTEL_PT_PERIOD_NONE:
951 case INTEL_PT_PERIOD_MTC:
956 decoder->state.type |= INTEL_PT_INSTRUCTION;
959 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
960 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
962 uint64_t max_insn_cnt, insn_cnt = 0;
965 if (!decoder->mtc_insn)
966 decoder->mtc_insn = true;
968 max_insn_cnt = intel_pt_next_sample(decoder);
970 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
971 max_insn_cnt, decoder->data);
973 decoder->tot_insn_cnt += insn_cnt;
974 decoder->timestamp_insn_cnt += insn_cnt;
975 decoder->sample_insn_cnt += insn_cnt;
976 decoder->period_insn_cnt += insn_cnt;
979 decoder->no_progress = 0;
980 decoder->pkt_state = INTEL_PT_STATE_ERR2;
981 intel_pt_log_at("ERROR: Failed to get instruction",
988 if (ip && decoder->ip == ip) {
993 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
994 intel_pt_sample_insn(decoder);
996 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
997 decoder->state.type = INTEL_PT_INSTRUCTION;
998 decoder->state.from_ip = decoder->ip;
999 decoder->state.to_ip = 0;
1000 decoder->ip += intel_pt_insn->length;
1001 err = INTEL_PT_RETURN;
1005 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
1006 /* Zero-length calls are excluded */
1007 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
1008 intel_pt_insn->rel) {
1009 err = intel_pt_push(&decoder->stack, decoder->ip +
1010 intel_pt_insn->length);
1014 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
1015 decoder->ret_addr = intel_pt_pop(&decoder->stack);
1018 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1019 int cnt = decoder->no_progress++;
1021 decoder->state.from_ip = decoder->ip;
1022 decoder->ip += intel_pt_insn->length +
1024 decoder->state.to_ip = decoder->ip;
1025 err = INTEL_PT_RETURN;
1028 * Check for being stuck in a loop. This can happen if a
1029 * decoder error results in the decoder erroneously setting the
1030 * ip to an address that is itself in an infinite loop that
1031 * consumes no packets. When that happens, there must be an
1032 * unconditional branch.
1036 decoder->stuck_ip = decoder->state.to_ip;
1037 decoder->stuck_ip_prd = 1;
1038 decoder->stuck_ip_cnt = 1;
1039 } else if (cnt > INTEL_PT_MAX_LOOPS ||
1040 decoder->state.to_ip == decoder->stuck_ip) {
1041 intel_pt_log_at("ERROR: Never-ending loop",
1042 decoder->state.to_ip);
1043 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1046 } else if (!--decoder->stuck_ip_cnt) {
1047 decoder->stuck_ip_prd += 1;
1048 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1049 decoder->stuck_ip = decoder->state.to_ip;
1052 goto out_no_progress;
1055 decoder->no_progress = 0;
1057 decoder->state.insn_op = intel_pt_insn->op;
1058 decoder->state.insn_len = intel_pt_insn->length;
1059 memcpy(decoder->state.insn, intel_pt_insn->buf,
1060 INTEL_PT_INSN_BUF_SZ);
1062 if (decoder->tx_flags & INTEL_PT_IN_TX)
1063 decoder->state.flags |= INTEL_PT_IN_TX;
1068 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1072 if (decoder->set_fup_tx_flags) {
1073 decoder->set_fup_tx_flags = false;
1074 decoder->tx_flags = decoder->fup_tx_flags;
1075 decoder->state.type = INTEL_PT_TRANSACTION;
1076 decoder->state.from_ip = decoder->ip;
1077 decoder->state.to_ip = 0;
1078 decoder->state.flags = decoder->fup_tx_flags;
1081 if (decoder->set_fup_ptw) {
1082 decoder->set_fup_ptw = false;
1083 decoder->state.type = INTEL_PT_PTW;
1084 decoder->state.flags |= INTEL_PT_FUP_IP;
1085 decoder->state.from_ip = decoder->ip;
1086 decoder->state.to_ip = 0;
1087 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1090 if (decoder->set_fup_mwait) {
1091 decoder->set_fup_mwait = false;
1092 decoder->state.type = INTEL_PT_MWAIT_OP;
1093 decoder->state.from_ip = decoder->ip;
1094 decoder->state.to_ip = 0;
1095 decoder->state.mwait_payload = decoder->fup_mwait_payload;
1098 if (decoder->set_fup_pwre) {
1099 decoder->set_fup_pwre = false;
1100 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1101 decoder->state.type &= ~INTEL_PT_BRANCH;
1102 decoder->state.from_ip = decoder->ip;
1103 decoder->state.to_ip = 0;
1104 decoder->state.pwre_payload = decoder->fup_pwre_payload;
1107 if (decoder->set_fup_exstop) {
1108 decoder->set_fup_exstop = false;
1109 decoder->state.type |= INTEL_PT_EX_STOP;
1110 decoder->state.type &= ~INTEL_PT_BRANCH;
1111 decoder->state.flags |= INTEL_PT_FUP_IP;
1112 decoder->state.from_ip = decoder->ip;
1113 decoder->state.to_ip = 0;
1119 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1120 struct intel_pt_insn *intel_pt_insn,
1121 uint64_t ip, int err)
1123 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1124 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1125 ip == decoder->ip + intel_pt_insn->length;
1128 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1130 struct intel_pt_insn intel_pt_insn;
1134 ip = decoder->last_ip;
1137 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1138 if (err == INTEL_PT_RETURN)
1140 if (err == -EAGAIN ||
1141 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1142 if (intel_pt_fup_event(decoder))
1146 decoder->set_fup_tx_flags = false;
1150 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1151 intel_pt_log_at("ERROR: Unexpected indirect branch",
1153 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1157 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1158 intel_pt_log_at("ERROR: Unexpected conditional branch",
1160 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1164 intel_pt_bug(decoder);
1168 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1170 struct intel_pt_insn intel_pt_insn;
1173 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1174 if (err == INTEL_PT_RETURN &&
1176 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1177 (decoder->state.type & INTEL_PT_BRANCH) &&
1178 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1179 /* Unconditional branch leaving filter region */
1180 decoder->no_progress = 0;
1181 decoder->pge = false;
1182 decoder->continuous_period = false;
1183 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1184 decoder->state.type |= INTEL_PT_TRACE_END;
1187 if (err == INTEL_PT_RETURN)
1192 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1193 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1194 decoder->pge = false;
1195 decoder->continuous_period = false;
1196 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1197 decoder->state.from_ip = decoder->ip;
1198 if (decoder->packet.count == 0) {
1199 decoder->state.to_ip = 0;
1201 decoder->state.to_ip = decoder->last_ip;
1202 decoder->ip = decoder->last_ip;
1204 decoder->state.type |= INTEL_PT_TRACE_END;
1206 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1207 decoder->state.from_ip = decoder->ip;
1208 if (decoder->packet.count == 0) {
1209 decoder->state.to_ip = 0;
1211 decoder->state.to_ip = decoder->last_ip;
1212 decoder->ip = decoder->last_ip;
1218 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1219 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1222 if (decoder->pgd_ip &&
1223 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1224 decoder->pgd_ip(to_ip, decoder->data)) {
1225 /* Conditional branch leaving filter region */
1226 decoder->pge = false;
1227 decoder->continuous_period = false;
1228 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1229 decoder->ip = to_ip;
1230 decoder->state.from_ip = decoder->ip;
1231 decoder->state.to_ip = to_ip;
1232 decoder->state.type |= INTEL_PT_TRACE_END;
1235 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1237 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1241 return intel_pt_bug(decoder);
1244 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1246 struct intel_pt_insn intel_pt_insn;
1250 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1251 if (err == INTEL_PT_RETURN)
1256 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1257 if (!decoder->return_compression) {
1258 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1260 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1263 if (!decoder->ret_addr) {
1264 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1266 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1269 if (!(decoder->tnt.payload & BIT63)) {
1270 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1272 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1275 decoder->tnt.count -= 1;
1276 if (decoder->tnt.count)
1277 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1279 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1280 decoder->tnt.payload <<= 1;
1281 decoder->state.from_ip = decoder->ip;
1282 decoder->ip = decoder->ret_addr;
1283 decoder->state.to_ip = decoder->ip;
1287 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1288 /* Handle deferred TIPs */
1289 err = intel_pt_get_next_packet(decoder);
1292 if (decoder->packet.type != INTEL_PT_TIP ||
1293 decoder->packet.count == 0) {
1294 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1296 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1297 decoder->pkt_step = 0;
1300 intel_pt_set_last_ip(decoder);
1301 decoder->state.from_ip = decoder->ip;
1302 decoder->state.to_ip = decoder->last_ip;
1303 decoder->ip = decoder->last_ip;
1307 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1308 decoder->tnt.count -= 1;
1309 if (decoder->tnt.count)
1310 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1312 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1313 if (decoder->tnt.payload & BIT63) {
1314 decoder->tnt.payload <<= 1;
1315 decoder->state.from_ip = decoder->ip;
1316 decoder->ip += intel_pt_insn.length +
1318 decoder->state.to_ip = decoder->ip;
1321 /* Instruction sample for a non-taken branch */
1322 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1323 decoder->tnt.payload <<= 1;
1324 decoder->state.type = INTEL_PT_INSTRUCTION;
1325 decoder->state.from_ip = decoder->ip;
1326 decoder->state.to_ip = 0;
1327 decoder->ip += intel_pt_insn.length;
1330 decoder->sample_cyc = false;
1331 decoder->ip += intel_pt_insn.length;
1332 if (!decoder->tnt.count) {
1333 intel_pt_update_sample_time(decoder);
1336 decoder->tnt.payload <<= 1;
1340 return intel_pt_bug(decoder);
1344 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1346 unsigned int fup_tx_flags;
1349 fup_tx_flags = decoder->packet.payload &
1350 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1351 err = intel_pt_get_next_packet(decoder);
1354 if (decoder->packet.type == INTEL_PT_FUP) {
1355 decoder->fup_tx_flags = fup_tx_flags;
1356 decoder->set_fup_tx_flags = true;
1357 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1360 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1362 intel_pt_update_in_tx(decoder);
1367 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1371 decoder->have_tma = false;
1373 if (decoder->ref_timestamp) {
1374 timestamp = decoder->packet.payload |
1375 (decoder->ref_timestamp & (0xffULL << 56));
1376 if (timestamp < decoder->ref_timestamp) {
1377 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1378 timestamp += (1ULL << 56);
1380 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1381 timestamp -= (1ULL << 56);
1383 decoder->tsc_timestamp = timestamp;
1384 decoder->timestamp = timestamp;
1385 decoder->ref_timestamp = 0;
1386 decoder->timestamp_insn_cnt = 0;
1387 } else if (decoder->timestamp) {
1388 timestamp = decoder->packet.payload |
1389 (decoder->timestamp & (0xffULL << 56));
1390 decoder->tsc_timestamp = timestamp;
1391 if (timestamp < decoder->timestamp &&
1392 decoder->timestamp - timestamp < decoder->tsc_slip) {
1393 intel_pt_log_to("Suppressing backwards timestamp",
1395 timestamp = decoder->timestamp;
1397 if (timestamp < decoder->timestamp) {
1398 intel_pt_log_to("Wraparound timestamp", timestamp);
1399 timestamp += (1ULL << 56);
1400 decoder->tsc_timestamp = timestamp;
1402 decoder->timestamp = timestamp;
1403 decoder->timestamp_insn_cnt = 0;
1406 if (decoder->last_packet_type == INTEL_PT_CYC) {
1407 decoder->cyc_ref_timestamp = decoder->timestamp;
1408 decoder->cycle_cnt = 0;
1409 decoder->have_calc_cyc_to_tsc = false;
1410 intel_pt_calc_cyc_to_tsc(decoder, false);
1413 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1416 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1418 intel_pt_log("ERROR: Buffer overflow\n");
1419 intel_pt_clear_tx_flags(decoder);
1420 decoder->timestamp_insn_cnt = 0;
1421 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1422 decoder->overflow = true;
1426 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1428 uint32_t ctc = decoder->packet.payload;
1429 uint32_t fc = decoder->packet.count;
1430 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1432 if (!decoder->tsc_ctc_ratio_d)
1435 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1436 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1437 if (decoder->tsc_ctc_mult) {
1438 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1440 decoder->ctc_timestamp -= multdiv(ctc_rem,
1441 decoder->tsc_ctc_ratio_n,
1442 decoder->tsc_ctc_ratio_d);
1444 decoder->ctc_delta = 0;
1445 decoder->have_tma = true;
1446 decoder->fixup_last_mtc = true;
1447 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1448 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1451 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1454 uint32_t mtc, mtc_delta;
1456 if (!decoder->have_tma)
1459 mtc = decoder->packet.payload;
1461 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1462 decoder->fixup_last_mtc = false;
1463 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1464 &decoder->last_mtc);
1467 if (mtc > decoder->last_mtc)
1468 mtc_delta = mtc - decoder->last_mtc;
1470 mtc_delta = mtc + 256 - decoder->last_mtc;
1472 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1474 if (decoder->tsc_ctc_mult) {
1475 timestamp = decoder->ctc_timestamp +
1476 decoder->ctc_delta * decoder->tsc_ctc_mult;
1478 timestamp = decoder->ctc_timestamp +
1479 multdiv(decoder->ctc_delta,
1480 decoder->tsc_ctc_ratio_n,
1481 decoder->tsc_ctc_ratio_d);
1484 if (timestamp < decoder->timestamp)
1485 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1486 timestamp, decoder->timestamp);
1488 decoder->timestamp = timestamp;
1490 decoder->timestamp_insn_cnt = 0;
1491 decoder->last_mtc = mtc;
1493 if (decoder->last_packet_type == INTEL_PT_CYC) {
1494 decoder->cyc_ref_timestamp = decoder->timestamp;
1495 decoder->cycle_cnt = 0;
1496 decoder->have_calc_cyc_to_tsc = false;
1497 intel_pt_calc_cyc_to_tsc(decoder, true);
1500 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1503 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1505 unsigned int cbr = decoder->packet.payload & 0xff;
1507 decoder->cbr_payload = decoder->packet.payload;
1509 if (decoder->cbr == cbr)
1513 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1516 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1518 uint64_t timestamp = decoder->cyc_ref_timestamp;
1520 decoder->have_cyc = true;
1522 decoder->cycle_cnt += decoder->packet.payload;
1524 decoder->tot_cyc_cnt += decoder->packet.payload;
1525 decoder->sample_cyc = true;
1527 if (!decoder->cyc_ref_timestamp)
1530 if (decoder->have_calc_cyc_to_tsc)
1531 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1532 else if (decoder->cbr)
1533 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1537 if (timestamp < decoder->timestamp)
1538 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1539 timestamp, decoder->timestamp);
1541 decoder->timestamp = timestamp;
1543 decoder->timestamp_insn_cnt = 0;
1545 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1548 /* Walk PSB+ packets when already in sync. */
1549 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1553 decoder->in_psb = true;
1556 err = intel_pt_get_next_packet(decoder);
1560 switch (decoder->packet.type) {
1561 case INTEL_PT_PSBEND:
1565 case INTEL_PT_TIP_PGD:
1566 case INTEL_PT_TIP_PGE:
1569 case INTEL_PT_TRACESTOP:
1572 case INTEL_PT_PTWRITE:
1573 case INTEL_PT_PTWRITE_IP:
1574 case INTEL_PT_EXSTOP:
1575 case INTEL_PT_EXSTOP_IP:
1576 case INTEL_PT_MWAIT:
1579 decoder->have_tma = false;
1580 intel_pt_log("ERROR: Unexpected packet\n");
1585 err = intel_pt_overflow(decoder);
1589 intel_pt_calc_tsc_timestamp(decoder);
1593 intel_pt_calc_tma(decoder);
1597 intel_pt_calc_cbr(decoder);
1600 case INTEL_PT_MODE_EXEC:
1601 decoder->exec_mode = decoder->packet.payload;
1605 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1609 decoder->pge = true;
1610 if (decoder->packet.count)
1611 intel_pt_set_last_ip(decoder);
1614 case INTEL_PT_MODE_TSX:
1615 intel_pt_update_in_tx(decoder);
1619 intel_pt_calc_mtc_timestamp(decoder);
1620 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1621 decoder->state.type |= INTEL_PT_INSTRUCTION;
1633 decoder->in_psb = false;
1638 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1642 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1643 decoder->tx_flags = 0;
1644 decoder->state.flags &= ~INTEL_PT_IN_TX;
1645 decoder->state.flags |= INTEL_PT_ABORT_TX;
1647 decoder->state.flags |= INTEL_PT_ASYNC;
1651 err = intel_pt_get_next_packet(decoder);
1655 switch (decoder->packet.type) {
1658 case INTEL_PT_TRACESTOP:
1662 case INTEL_PT_MODE_TSX:
1664 case INTEL_PT_PSBEND:
1665 case INTEL_PT_PTWRITE:
1666 case INTEL_PT_PTWRITE_IP:
1667 case INTEL_PT_EXSTOP:
1668 case INTEL_PT_EXSTOP_IP:
1669 case INTEL_PT_MWAIT:
1672 intel_pt_log("ERROR: Missing TIP after FUP\n");
1673 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1674 decoder->pkt_step = 0;
1678 intel_pt_calc_cbr(decoder);
1682 return intel_pt_overflow(decoder);
1684 case INTEL_PT_TIP_PGD:
1685 decoder->state.from_ip = decoder->ip;
1686 if (decoder->packet.count == 0) {
1687 decoder->state.to_ip = 0;
1689 intel_pt_set_ip(decoder);
1690 decoder->state.to_ip = decoder->ip;
1692 decoder->pge = false;
1693 decoder->continuous_period = false;
1694 decoder->state.type |= INTEL_PT_TRACE_END;
1697 case INTEL_PT_TIP_PGE:
1698 decoder->pge = true;
1699 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1701 decoder->state.from_ip = 0;
1702 if (decoder->packet.count == 0) {
1703 decoder->state.to_ip = 0;
1705 intel_pt_set_ip(decoder);
1706 decoder->state.to_ip = decoder->ip;
1708 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1712 decoder->state.from_ip = decoder->ip;
1713 if (decoder->packet.count == 0) {
1714 decoder->state.to_ip = 0;
1716 intel_pt_set_ip(decoder);
1717 decoder->state.to_ip = decoder->ip;
1722 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1726 intel_pt_calc_mtc_timestamp(decoder);
1727 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1728 decoder->state.type |= INTEL_PT_INSTRUCTION;
1732 intel_pt_calc_cyc_timestamp(decoder);
1735 case INTEL_PT_MODE_EXEC:
1736 decoder->exec_mode = decoder->packet.payload;
1745 return intel_pt_bug(decoder);
1750 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1752 bool no_tip = false;
1756 err = intel_pt_get_next_packet(decoder);
1760 switch (decoder->packet.type) {
1762 if (!decoder->packet.count)
1764 decoder->tnt = decoder->packet;
1765 decoder->pkt_state = INTEL_PT_STATE_TNT;
1766 err = intel_pt_walk_tnt(decoder);
1771 case INTEL_PT_TIP_PGD:
1772 if (decoder->packet.count != 0)
1773 intel_pt_set_last_ip(decoder);
1774 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1775 return intel_pt_walk_tip(decoder);
1777 case INTEL_PT_TIP_PGE: {
1778 decoder->pge = true;
1779 if (decoder->packet.count == 0) {
1780 intel_pt_log_at("Skipping zero TIP.PGE",
1784 intel_pt_set_ip(decoder);
1785 decoder->state.from_ip = 0;
1786 decoder->state.to_ip = decoder->ip;
1787 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1792 return intel_pt_overflow(decoder);
1795 if (decoder->packet.count != 0)
1796 intel_pt_set_last_ip(decoder);
1797 decoder->pkt_state = INTEL_PT_STATE_TIP;
1798 return intel_pt_walk_tip(decoder);
1801 if (decoder->packet.count == 0) {
1802 intel_pt_log_at("Skipping zero FUP",
1807 intel_pt_set_last_ip(decoder);
1808 if (!decoder->branch_enable) {
1809 decoder->ip = decoder->last_ip;
1810 if (intel_pt_fup_event(decoder))
1815 if (decoder->set_fup_mwait)
1817 err = intel_pt_walk_fup(decoder);
1818 if (err != -EAGAIN) {
1822 decoder->pkt_state =
1823 INTEL_PT_STATE_FUP_NO_TIP;
1825 decoder->pkt_state = INTEL_PT_STATE_FUP;
1832 return intel_pt_walk_fup_tip(decoder);
1834 case INTEL_PT_TRACESTOP:
1835 decoder->pge = false;
1836 decoder->continuous_period = false;
1837 intel_pt_clear_tx_flags(decoder);
1838 decoder->have_tma = false;
1842 decoder->last_ip = 0;
1843 decoder->have_last_ip = true;
1844 intel_pt_clear_stack(&decoder->stack);
1845 err = intel_pt_walk_psbend(decoder);
1853 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1857 intel_pt_calc_mtc_timestamp(decoder);
1858 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1861 * Ensure that there has been an instruction since the
1864 if (!decoder->mtc_insn)
1866 decoder->mtc_insn = false;
1867 /* Ensure that there is a timestamp */
1868 if (!decoder->timestamp)
1870 decoder->state.type = INTEL_PT_INSTRUCTION;
1871 decoder->state.from_ip = decoder->ip;
1872 decoder->state.to_ip = 0;
1873 decoder->mtc_insn = false;
1877 intel_pt_calc_tsc_timestamp(decoder);
1881 intel_pt_calc_tma(decoder);
1885 intel_pt_calc_cyc_timestamp(decoder);
1889 intel_pt_calc_cbr(decoder);
1890 if (!decoder->branch_enable &&
1891 decoder->cbr != decoder->cbr_seen) {
1892 decoder->cbr_seen = decoder->cbr;
1893 decoder->state.type = INTEL_PT_CBR_CHG;
1894 decoder->state.from_ip = decoder->ip;
1895 decoder->state.to_ip = 0;
1896 decoder->state.cbr_payload =
1897 decoder->packet.payload;
1902 case INTEL_PT_MODE_EXEC:
1903 decoder->exec_mode = decoder->packet.payload;
1906 case INTEL_PT_MODE_TSX:
1907 /* MODE_TSX need not be followed by FUP */
1908 if (!decoder->pge) {
1909 intel_pt_update_in_tx(decoder);
1912 err = intel_pt_mode_tsx(decoder, &no_tip);
1917 case INTEL_PT_BAD: /* Does not happen */
1918 return intel_pt_bug(decoder);
1920 case INTEL_PT_PSBEND:
1926 case INTEL_PT_PTWRITE_IP:
1927 decoder->fup_ptw_payload = decoder->packet.payload;
1928 err = intel_pt_get_next_packet(decoder);
1931 if (decoder->packet.type == INTEL_PT_FUP) {
1932 decoder->set_fup_ptw = true;
1935 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1940 case INTEL_PT_PTWRITE:
1941 decoder->state.type = INTEL_PT_PTW;
1942 decoder->state.from_ip = decoder->ip;
1943 decoder->state.to_ip = 0;
1944 decoder->state.ptw_payload = decoder->packet.payload;
1947 case INTEL_PT_MWAIT:
1948 decoder->fup_mwait_payload = decoder->packet.payload;
1949 decoder->set_fup_mwait = true;
1953 if (decoder->set_fup_mwait) {
1954 decoder->fup_pwre_payload =
1955 decoder->packet.payload;
1956 decoder->set_fup_pwre = true;
1959 decoder->state.type = INTEL_PT_PWR_ENTRY;
1960 decoder->state.from_ip = decoder->ip;
1961 decoder->state.to_ip = 0;
1962 decoder->state.pwrx_payload = decoder->packet.payload;
1965 case INTEL_PT_EXSTOP_IP:
1966 err = intel_pt_get_next_packet(decoder);
1969 if (decoder->packet.type == INTEL_PT_FUP) {
1970 decoder->set_fup_exstop = true;
1973 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
1978 case INTEL_PT_EXSTOP:
1979 decoder->state.type = INTEL_PT_EX_STOP;
1980 decoder->state.from_ip = decoder->ip;
1981 decoder->state.to_ip = 0;
1985 decoder->state.type = INTEL_PT_PWR_EXIT;
1986 decoder->state.from_ip = decoder->ip;
1987 decoder->state.to_ip = 0;
1988 decoder->state.pwrx_payload = decoder->packet.payload;
1992 return intel_pt_bug(decoder);
1997 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1999 return decoder->packet.count &&
2000 (decoder->have_last_ip || decoder->packet.count == 3 ||
2001 decoder->packet.count == 6);
2004 /* Walk PSB+ packets to get in sync. */
2005 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
2009 decoder->in_psb = true;
2012 err = intel_pt_get_next_packet(decoder);
2016 switch (decoder->packet.type) {
2017 case INTEL_PT_TIP_PGD:
2018 decoder->continuous_period = false;
2020 case INTEL_PT_TIP_PGE:
2022 case INTEL_PT_PTWRITE:
2023 case INTEL_PT_PTWRITE_IP:
2024 case INTEL_PT_EXSTOP:
2025 case INTEL_PT_EXSTOP_IP:
2026 case INTEL_PT_MWAIT:
2029 intel_pt_log("ERROR: Unexpected packet\n");
2034 decoder->pge = true;
2035 if (intel_pt_have_ip(decoder)) {
2036 uint64_t current_ip = decoder->ip;
2038 intel_pt_set_ip(decoder);
2040 intel_pt_log_to("Setting IP",
2046 intel_pt_calc_mtc_timestamp(decoder);
2050 intel_pt_calc_tsc_timestamp(decoder);
2054 intel_pt_calc_tma(decoder);
2058 intel_pt_calc_cyc_timestamp(decoder);
2062 intel_pt_calc_cbr(decoder);
2066 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2069 case INTEL_PT_MODE_EXEC:
2070 decoder->exec_mode = decoder->packet.payload;
2073 case INTEL_PT_MODE_TSX:
2074 intel_pt_update_in_tx(decoder);
2077 case INTEL_PT_TRACESTOP:
2078 decoder->pge = false;
2079 decoder->continuous_period = false;
2080 intel_pt_clear_tx_flags(decoder);
2084 decoder->have_tma = false;
2085 intel_pt_log("ERROR: Unexpected packet\n");
2087 decoder->pkt_state = INTEL_PT_STATE_ERR4;
2089 decoder->pkt_state = INTEL_PT_STATE_ERR3;
2093 case INTEL_PT_BAD: /* Does not happen */
2094 err = intel_pt_bug(decoder);
2098 err = intel_pt_overflow(decoder);
2101 case INTEL_PT_PSBEND:
2114 decoder->in_psb = false;
2119 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2124 err = intel_pt_get_next_packet(decoder);
2128 switch (decoder->packet.type) {
2129 case INTEL_PT_TIP_PGD:
2130 decoder->continuous_period = false;
2132 case INTEL_PT_TIP_PGE:
2134 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
2135 if (intel_pt_have_ip(decoder))
2136 intel_pt_set_ip(decoder);
2139 if (decoder->packet.type == INTEL_PT_TIP_PGE)
2140 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2141 if (decoder->packet.type == INTEL_PT_TIP_PGD)
2142 decoder->state.type |= INTEL_PT_TRACE_END;
2146 if (intel_pt_have_ip(decoder))
2147 intel_pt_set_ip(decoder);
2153 intel_pt_calc_mtc_timestamp(decoder);
2157 intel_pt_calc_tsc_timestamp(decoder);
2161 intel_pt_calc_tma(decoder);
2165 intel_pt_calc_cyc_timestamp(decoder);
2169 intel_pt_calc_cbr(decoder);
2173 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2176 case INTEL_PT_MODE_EXEC:
2177 decoder->exec_mode = decoder->packet.payload;
2180 case INTEL_PT_MODE_TSX:
2181 intel_pt_update_in_tx(decoder);
2185 return intel_pt_overflow(decoder);
2187 case INTEL_PT_BAD: /* Does not happen */
2188 return intel_pt_bug(decoder);
2190 case INTEL_PT_TRACESTOP:
2191 decoder->pge = false;
2192 decoder->continuous_period = false;
2193 intel_pt_clear_tx_flags(decoder);
2194 decoder->have_tma = false;
2198 decoder->last_ip = 0;
2199 decoder->have_last_ip = true;
2200 intel_pt_clear_stack(&decoder->stack);
2201 err = intel_pt_walk_psb(decoder);
2205 /* Do not have a sample */
2206 decoder->state.type = 0;
2212 case INTEL_PT_PSBEND:
2216 case INTEL_PT_PTWRITE:
2217 case INTEL_PT_PTWRITE_IP:
2218 case INTEL_PT_EXSTOP:
2219 case INTEL_PT_EXSTOP_IP:
2220 case INTEL_PT_MWAIT:
2229 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2233 decoder->set_fup_tx_flags = false;
2234 decoder->set_fup_ptw = false;
2235 decoder->set_fup_mwait = false;
2236 decoder->set_fup_pwre = false;
2237 decoder->set_fup_exstop = false;
2239 if (!decoder->branch_enable) {
2240 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2241 decoder->overflow = false;
2242 decoder->state.type = 0; /* Do not have a sample */
2246 intel_pt_log("Scanning for full IP\n");
2247 err = intel_pt_walk_to_ip(decoder);
2251 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2252 decoder->overflow = false;
2254 decoder->state.from_ip = 0;
2255 decoder->state.to_ip = decoder->ip;
2256 intel_pt_log_to("Setting IP", decoder->ip);
2261 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2263 const unsigned char *end = decoder->buf + decoder->len;
2266 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2267 if (i > decoder->len)
2269 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2275 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2277 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2278 const char *psb = INTEL_PT_PSB_STR;
2280 if (rest_psb > decoder->len ||
2281 memcmp(decoder->buf, psb + part_psb, rest_psb))
2287 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2292 decoder->pos += decoder->len;
2295 ret = intel_pt_get_next_data(decoder);
2299 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2303 decoder->pos -= part_psb;
2304 decoder->next_buf = decoder->buf + rest_psb;
2305 decoder->next_len = decoder->len - rest_psb;
2306 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2307 decoder->buf = decoder->temp_buf;
2308 decoder->len = INTEL_PT_PSB_LEN;
2313 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2315 unsigned char *next;
2318 intel_pt_log("Scanning for PSB\n");
2320 if (!decoder->len) {
2321 ret = intel_pt_get_next_data(decoder);
2326 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2331 part_psb = intel_pt_part_psb(decoder);
2333 ret = intel_pt_get_split_psb(decoder, part_psb);
2337 decoder->pos += decoder->len;
2343 decoder->pkt_step = next - decoder->buf;
2344 return intel_pt_get_next_packet(decoder);
2348 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2352 decoder->pge = false;
2353 decoder->continuous_period = false;
2354 decoder->have_last_ip = false;
2355 decoder->last_ip = 0;
2357 intel_pt_clear_stack(&decoder->stack);
2359 err = intel_pt_scan_for_psb(decoder);
2363 decoder->have_last_ip = true;
2364 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2366 err = intel_pt_walk_psb(decoder);
2371 decoder->state.type = 0; /* Do not have a sample */
2372 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2374 return intel_pt_sync_ip(decoder);
2380 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2382 uint64_t est = decoder->sample_insn_cnt << 1;
2384 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2387 est *= decoder->max_non_turbo_ratio;
2388 est /= decoder->cbr;
2390 return decoder->sample_timestamp + est;
2393 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2398 decoder->state.type = INTEL_PT_BRANCH;
2399 decoder->state.flags = 0;
2401 switch (decoder->pkt_state) {
2402 case INTEL_PT_STATE_NO_PSB:
2403 err = intel_pt_sync(decoder);
2405 case INTEL_PT_STATE_NO_IP:
2406 decoder->have_last_ip = false;
2407 decoder->last_ip = 0;
2410 case INTEL_PT_STATE_ERR_RESYNC:
2411 err = intel_pt_sync_ip(decoder);
2413 case INTEL_PT_STATE_IN_SYNC:
2414 err = intel_pt_walk_trace(decoder);
2416 case INTEL_PT_STATE_TNT:
2417 case INTEL_PT_STATE_TNT_CONT:
2418 err = intel_pt_walk_tnt(decoder);
2420 err = intel_pt_walk_trace(decoder);
2422 case INTEL_PT_STATE_TIP:
2423 case INTEL_PT_STATE_TIP_PGD:
2424 err = intel_pt_walk_tip(decoder);
2426 case INTEL_PT_STATE_FUP:
2427 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2428 err = intel_pt_walk_fup(decoder);
2430 err = intel_pt_walk_fup_tip(decoder);
2432 decoder->pkt_state = INTEL_PT_STATE_FUP;
2434 case INTEL_PT_STATE_FUP_NO_TIP:
2435 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2436 err = intel_pt_walk_fup(decoder);
2438 err = intel_pt_walk_trace(decoder);
2441 err = intel_pt_bug(decoder);
2444 } while (err == -ENOLINK);
2447 decoder->state.err = intel_pt_ext_err(err);
2448 decoder->state.from_ip = decoder->ip;
2449 intel_pt_update_sample_time(decoder);
2450 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
2452 decoder->state.err = 0;
2453 if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2454 decoder->cbr_seen = decoder->cbr;
2455 decoder->state.type |= INTEL_PT_CBR_CHG;
2456 decoder->state.cbr_payload = decoder->cbr_payload;
2458 if (intel_pt_sample_time(decoder->pkt_state)) {
2459 intel_pt_update_sample_time(decoder);
2460 if (decoder->sample_cyc)
2461 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
2465 decoder->state.timestamp = decoder->sample_timestamp;
2466 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2467 decoder->state.cr3 = decoder->cr3;
2468 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2469 decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
2471 return &decoder->state;
2475 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2476 * @buf: pointer to buffer pointer
2477 * @len: size of buffer
2479 * Updates the buffer pointer to point to the start of the next PSB packet if
2480 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2481 * @len is adjusted accordingly.
2483 * Return: %true if a PSB packet is found, %false otherwise.
2485 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2487 unsigned char *next;
2489 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2491 *len -= next - *buf;
2499 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2501 * @buf: pointer to buffer pointer
2502 * @len: size of buffer
2504 * Updates the buffer pointer to point to the start of the following PSB packet
2505 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2506 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2508 * Return: %true if a PSB packet is found, %false otherwise.
2510 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2512 unsigned char *next;
2517 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2519 *len -= next - *buf;
2527 * intel_pt_last_psb - find the last PSB packet in a buffer.
2529 * @len: size of buffer
2531 * This function finds the last PSB in a buffer.
2533 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2535 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2537 const char *n = INTEL_PT_PSB_STR;
2541 if (len < INTEL_PT_PSB_LEN)
2544 k = len - INTEL_PT_PSB_LEN + 1;
2546 p = memrchr(buf, n[0], k);
2549 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2558 * intel_pt_next_tsc - find and return next TSC.
2560 * @len: size of buffer
2561 * @tsc: TSC value returned
2562 * @rem: returns remaining size when TSC is found
2564 * Find a TSC packet in @buf and return the TSC value. This function assumes
2565 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2566 * PSBEND packet is found.
2568 * Return: %true if TSC is found, false otherwise.
2570 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2573 struct intel_pt_pkt packet;
2577 ret = intel_pt_get_packet(buf, len, &packet);
2580 if (packet.type == INTEL_PT_TSC) {
2581 *tsc = packet.payload;
2585 if (packet.type == INTEL_PT_PSBEND)
2594 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2595 * @tsc1: first TSC to compare
2596 * @tsc2: second TSC to compare
2598 * This function compares 7-byte TSC values allowing for the possibility that
2599 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2600 * around so for that purpose this function assumes the absolute difference is
2601 * less than half the maximum difference.
2603 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2606 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2608 const uint64_t halfway = (1ULL << 55);
2614 if (tsc2 - tsc1 < halfway)
2619 if (tsc1 - tsc2 < halfway)
2626 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2629 * adj_for_padding - adjust overlap to account for padding.
2630 * @buf_b: second buffer
2631 * @buf_a: first buffer
2632 * @len_a: size of first buffer
2634 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2637 * Return: A pointer into @buf_b from where non-overlapped data starts
2639 static unsigned char *adj_for_padding(unsigned char *buf_b,
2640 unsigned char *buf_a, size_t len_a)
2642 unsigned char *p = buf_b - MAX_PADDING;
2643 unsigned char *q = buf_a + len_a - MAX_PADDING;
2646 for (i = MAX_PADDING; i; i--, p++, q++) {
2655 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2657 * @buf_a: first buffer
2658 * @len_a: size of first buffer
2659 * @buf_b: second buffer
2660 * @len_b: size of second buffer
2661 * @consecutive: returns true if there is data in buf_b that is consecutive
2664 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2665 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2666 * walk forward in @buf_b until a later TSC is found. A precondition is that
2667 * @buf_a and @buf_b are positioned at a PSB.
2669 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2670 * @buf_b + @len_b if there is no non-overlapped data.
2672 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2674 unsigned char *buf_b,
2675 size_t len_b, bool *consecutive)
2677 uint64_t tsc_a, tsc_b;
2679 size_t len, rem_a, rem_b;
2681 p = intel_pt_last_psb(buf_a, len_a);
2683 return buf_b; /* No PSB in buf_a => no overlap */
2685 len = len_a - (p - buf_a);
2686 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2687 /* The last PSB+ in buf_a is incomplete, so go back one more */
2689 p = intel_pt_last_psb(buf_a, len_a);
2691 return buf_b; /* No full PSB+ => assume no overlap */
2692 len = len_a - (p - buf_a);
2693 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2694 return buf_b; /* No TSC in buf_a => assume no overlap */
2698 /* Ignore PSB+ with no TSC */
2699 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2700 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2702 /* Same TSC, so buffers are consecutive */
2703 if (!cmp && rem_b >= rem_a) {
2704 unsigned char *start;
2706 *consecutive = true;
2707 start = buf_b + len_b - (rem_b - rem_a);
2708 return adj_for_padding(start, buf_a, len_a);
2711 return buf_b; /* tsc_a < tsc_b => no overlap */
2714 if (!intel_pt_step_psb(&buf_b, &len_b))
2715 return buf_b + len_b; /* No PSB in buf_b => no data */
2720 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2721 * @buf_a: first buffer
2722 * @len_a: size of first buffer
2723 * @buf_b: second buffer
2724 * @len_b: size of second buffer
2725 * @have_tsc: can use TSC packets to detect overlap
2726 * @consecutive: returns true if there is data in buf_b that is consecutive
2729 * When trace samples or snapshots are recorded there is the possibility that
2730 * the data overlaps. Note that, for the purposes of decoding, data is only
2731 * useful if it begins with a PSB packet.
2733 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2734 * @buf_b + @len_b if there is no non-overlapped data.
2736 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2737 unsigned char *buf_b, size_t len_b,
2738 bool have_tsc, bool *consecutive)
2740 unsigned char *found;
2742 /* Buffer 'b' must start at PSB so throw away everything before that */
2743 if (!intel_pt_next_psb(&buf_b, &len_b))
2744 return buf_b + len_b; /* No PSB */
2746 if (!intel_pt_next_psb(&buf_a, &len_a))
2747 return buf_b; /* No overlap */
2750 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2757 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2758 * we can ignore the first part of buffer 'a'.
2760 while (len_b < len_a) {
2761 if (!intel_pt_step_psb(&buf_a, &len_a))
2762 return buf_b; /* No overlap */
2765 /* Now len_b >= len_a */
2767 /* Potential overlap so check the bytes */
2768 found = memmem(buf_a, len_a, buf_b, len_a);
2770 *consecutive = true;
2771 return adj_for_padding(buf_b + len_a, buf_a, len_a);
2774 /* Try again at next PSB in buffer 'a' */
2775 if (!intel_pt_step_psb(&buf_a, &len_a))
2776 return buf_b; /* No overlap */