2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 #include <linux/compiler.h>
30 #include "intel-pt-insn-decoder.h"
31 #include "intel-pt-pkt-decoder.h"
32 #include "intel-pt-decoder.h"
33 #include "intel-pt-log.h"
35 #define INTEL_PT_BLK_SIZE 1024
37 #define BIT63 (((uint64_t)1 << 63))
39 #define INTEL_PT_RETURN 1
41 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
42 #define INTEL_PT_MAX_LOOPS 10000
45 struct intel_pt_blk *prev;
46 uint64_t ip[INTEL_PT_BLK_SIZE];
49 struct intel_pt_stack {
50 struct intel_pt_blk *blk;
51 struct intel_pt_blk *spare;
55 enum intel_pt_pkt_state {
56 INTEL_PT_STATE_NO_PSB,
58 INTEL_PT_STATE_ERR_RESYNC,
59 INTEL_PT_STATE_IN_SYNC,
62 INTEL_PT_STATE_TIP_PGD,
64 INTEL_PT_STATE_FUP_NO_TIP,
67 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
70 case INTEL_PT_STATE_NO_PSB:
71 case INTEL_PT_STATE_NO_IP:
72 case INTEL_PT_STATE_ERR_RESYNC:
73 case INTEL_PT_STATE_IN_SYNC:
74 case INTEL_PT_STATE_TNT:
76 case INTEL_PT_STATE_TIP:
77 case INTEL_PT_STATE_TIP_PGD:
78 case INTEL_PT_STATE_FUP:
79 case INTEL_PT_STATE_FUP_NO_TIP:
86 #ifdef INTEL_PT_STRICT
87 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
88 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
89 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
90 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
92 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
93 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
94 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
95 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
98 struct intel_pt_decoder {
99 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
100 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
101 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
102 uint64_t max_insn_cnt, void *data);
103 bool (*pgd_ip)(uint64_t ip, void *data);
105 struct intel_pt_state state;
106 const unsigned char *buf;
108 bool return_compression;
116 enum intel_pt_param_flags flags;
122 uint64_t tsc_timestamp;
123 uint64_t ref_timestamp;
124 uint64_t sample_timestamp;
126 uint64_t ctc_timestamp;
129 uint64_t cyc_ref_timestamp;
131 uint32_t tsc_ctc_ratio_n;
132 uint32_t tsc_ctc_ratio_d;
133 uint32_t tsc_ctc_mult;
135 uint32_t ctc_rem_mask;
137 struct intel_pt_stack stack;
138 enum intel_pt_pkt_state pkt_state;
139 struct intel_pt_pkt packet;
140 struct intel_pt_pkt tnt;
143 int last_packet_type;
145 unsigned int cbr_seen;
146 unsigned int max_non_turbo_ratio;
147 double max_non_turbo_ratio_fp;
148 double cbr_cyc_to_tsc;
149 double calc_cyc_to_tsc;
150 bool have_calc_cyc_to_tsc;
152 unsigned int insn_bytes;
154 enum intel_pt_period_type period_type;
155 uint64_t tot_insn_cnt;
156 uint64_t period_insn_cnt;
157 uint64_t period_mask;
158 uint64_t period_ticks;
159 uint64_t last_masked_timestamp;
160 bool continuous_period;
162 bool set_fup_tx_flags;
167 unsigned int fup_tx_flags;
168 unsigned int tx_flags;
169 uint64_t fup_ptw_payload;
170 uint64_t fup_mwait_payload;
171 uint64_t fup_pwre_payload;
172 uint64_t cbr_payload;
173 uint64_t timestamp_insn_cnt;
174 uint64_t sample_insn_cnt;
179 const unsigned char *next_buf;
181 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
184 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
188 for (i = 0; x != 1; i++)
194 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
196 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
199 period = intel_pt_lower_power_of_2(decoder->period);
200 decoder->period_mask = ~(period - 1);
201 decoder->period_ticks = period;
205 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
209 return (t / d) * n + ((t % d) * n) / d;
212 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
214 struct intel_pt_decoder *decoder;
216 if (!params->get_trace || !params->walk_insn)
219 decoder = zalloc(sizeof(struct intel_pt_decoder));
223 decoder->get_trace = params->get_trace;
224 decoder->walk_insn = params->walk_insn;
225 decoder->pgd_ip = params->pgd_ip;
226 decoder->data = params->data;
227 decoder->return_compression = params->return_compression;
228 decoder->branch_enable = params->branch_enable;
230 decoder->flags = params->flags;
232 decoder->period = params->period;
233 decoder->period_type = params->period_type;
235 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
236 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
238 intel_pt_setup_period(decoder);
240 decoder->mtc_shift = params->mtc_period;
241 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
243 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
244 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
246 if (!decoder->tsc_ctc_ratio_n)
247 decoder->tsc_ctc_ratio_d = 0;
249 if (decoder->tsc_ctc_ratio_d) {
250 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
251 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
252 decoder->tsc_ctc_ratio_d;
255 * Allow for timestamps appearing to backwards because a TSC
256 * packet has slipped past a MTC packet, so allow 2 MTC ticks
259 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
260 decoder->tsc_ctc_ratio_n,
261 decoder->tsc_ctc_ratio_d);
263 /* ... or 0x100 paranoia */
264 if (decoder->tsc_slip < 0x100)
265 decoder->tsc_slip = 0x100;
267 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
268 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
269 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
270 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
271 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
276 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
278 struct intel_pt_blk *blk = stack->blk;
280 stack->blk = blk->prev;
287 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
292 intel_pt_pop_blk(stack);
295 stack->pos = INTEL_PT_BLK_SIZE;
297 return stack->blk->ip[--stack->pos];
300 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
302 struct intel_pt_blk *blk;
308 blk = malloc(sizeof(struct intel_pt_blk));
313 blk->prev = stack->blk;
319 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
323 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
324 err = intel_pt_alloc_blk(stack);
329 stack->blk->ip[stack->pos++] = ip;
333 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
336 intel_pt_pop_blk(stack);
340 static void intel_pt_free_stack(struct intel_pt_stack *stack)
342 intel_pt_clear_stack(stack);
344 zfree(&stack->spare);
347 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
349 intel_pt_free_stack(&decoder->stack);
353 static int intel_pt_ext_err(int code)
357 return INTEL_PT_ERR_NOMEM;
359 return INTEL_PT_ERR_INTERN;
361 return INTEL_PT_ERR_BADPKT;
363 return INTEL_PT_ERR_NODATA;
365 return INTEL_PT_ERR_NOINSN;
367 return INTEL_PT_ERR_MISMAT;
369 return INTEL_PT_ERR_OVR;
371 return INTEL_PT_ERR_LOST;
373 return INTEL_PT_ERR_NELOOP;
375 return INTEL_PT_ERR_UNK;
379 static const char *intel_pt_err_msgs[] = {
380 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
381 [INTEL_PT_ERR_INTERN] = "Internal error",
382 [INTEL_PT_ERR_BADPKT] = "Bad packet",
383 [INTEL_PT_ERR_NODATA] = "No more data",
384 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
385 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
386 [INTEL_PT_ERR_OVR] = "Overflow packet",
387 [INTEL_PT_ERR_LOST] = "Lost trace data",
388 [INTEL_PT_ERR_UNK] = "Unknown error!",
389 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
392 int intel_pt__strerror(int code, char *buf, size_t buflen)
394 if (code < 1 || code >= INTEL_PT_ERR_MAX)
395 code = INTEL_PT_ERR_UNK;
396 strlcpy(buf, intel_pt_err_msgs[code], buflen);
400 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
405 switch (packet->count) {
407 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
411 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
415 ip = packet->payload;
416 /* Sign-extend 6-byte ip */
417 if (ip & (uint64_t)0x800000000000ULL)
418 ip |= (uint64_t)0xffff000000000000ULL;
421 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
425 ip = packet->payload;
434 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
436 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
437 decoder->have_last_ip = true;
440 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
442 intel_pt_set_last_ip(decoder);
443 decoder->ip = decoder->last_ip;
446 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
448 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
452 static int intel_pt_bug(struct intel_pt_decoder *decoder)
454 intel_pt_log("ERROR: Internal error\n");
455 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
459 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
461 decoder->tx_flags = 0;
464 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
466 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
469 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
471 intel_pt_clear_tx_flags(decoder);
472 decoder->have_tma = false;
473 decoder->pkt_len = 1;
474 decoder->pkt_step = 1;
475 intel_pt_decoder_log_packet(decoder);
476 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
477 intel_pt_log("ERROR: Bad packet\n");
478 decoder->pkt_state = INTEL_PT_STATE_ERR1;
483 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
485 struct intel_pt_buffer buffer = { .buf = 0, };
488 decoder->pkt_step = 0;
490 intel_pt_log("Getting more data\n");
491 ret = decoder->get_trace(&buffer, decoder->data);
494 decoder->buf = buffer.buf;
495 decoder->len = buffer.len;
497 intel_pt_log("No more data\n");
500 if (!buffer.consecutive) {
502 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
503 decoder->ref_timestamp = buffer.ref_timestamp;
504 decoder->timestamp = 0;
505 decoder->have_tma = false;
506 decoder->state.trace_nr = buffer.trace_nr;
507 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
508 decoder->ref_timestamp);
515 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
517 if (!decoder->next_buf)
518 return intel_pt_get_data(decoder);
520 decoder->buf = decoder->next_buf;
521 decoder->len = decoder->next_len;
522 decoder->next_buf = 0;
523 decoder->next_len = 0;
527 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
529 unsigned char *buf = decoder->temp_buf;
530 size_t old_len, len, n;
533 old_len = decoder->len;
535 memcpy(buf, decoder->buf, len);
537 ret = intel_pt_get_data(decoder);
539 decoder->pos += old_len;
540 return ret < 0 ? ret : -EINVAL;
543 n = INTEL_PT_PKT_MAX_SZ - len;
544 if (n > decoder->len)
546 memcpy(buf + len, decoder->buf, n);
549 ret = intel_pt_get_packet(buf, len, &decoder->packet);
550 if (ret < (int)old_len) {
551 decoder->next_buf = decoder->buf;
552 decoder->next_len = decoder->len;
554 decoder->len = old_len;
555 return intel_pt_bad_packet(decoder);
558 decoder->next_buf = decoder->buf + (ret - old_len);
559 decoder->next_len = decoder->len - (ret - old_len);
567 struct intel_pt_pkt_info {
568 struct intel_pt_decoder *decoder;
569 struct intel_pt_pkt packet;
572 int last_packet_type;
576 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
578 /* Lookahead packets in current buffer */
579 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
580 intel_pt_pkt_cb_t cb, void *data)
582 struct intel_pt_pkt_info pkt_info;
583 const unsigned char *buf = decoder->buf;
584 size_t len = decoder->len;
587 pkt_info.decoder = decoder;
588 pkt_info.pos = decoder->pos;
589 pkt_info.pkt_len = decoder->pkt_step;
590 pkt_info.last_packet_type = decoder->last_packet_type;
591 pkt_info.data = data;
595 pkt_info.pos += pkt_info.pkt_len;
596 buf += pkt_info.pkt_len;
597 len -= pkt_info.pkt_len;
600 return INTEL_PT_NEED_MORE_BYTES;
602 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
604 return INTEL_PT_NEED_MORE_BYTES;
608 pkt_info.pkt_len = ret;
609 } while (pkt_info.packet.type == INTEL_PT_PAD);
615 pkt_info.last_packet_type = pkt_info.packet.type;
619 struct intel_pt_calc_cyc_to_tsc_info {
623 uint64_t ctc_timestamp;
625 uint64_t tsc_timestamp;
630 double cbr_cyc_to_tsc;
634 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
635 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
636 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
637 * packet by copying the missing bits from the current MTC assuming the least
638 * difference between the two, and that the current MTC comes after last_mtc.
640 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
643 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
644 uint32_t mask = ~(first_missing_bit - 1);
646 *last_mtc |= mtc & mask;
647 if (*last_mtc >= mtc) {
648 *last_mtc -= first_missing_bit;
653 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
655 struct intel_pt_decoder *decoder = pkt_info->decoder;
656 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
660 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
662 switch (pkt_info->packet.type) {
664 case INTEL_PT_TIP_PGE:
669 case INTEL_PT_MODE_EXEC:
670 case INTEL_PT_MODE_TSX:
671 case INTEL_PT_PSBEND:
675 case INTEL_PT_PTWRITE:
676 case INTEL_PT_PTWRITE_IP:
683 mtc = pkt_info->packet.payload;
684 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
685 data->fixup_last_mtc = false;
686 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
689 if (mtc > data->last_mtc)
690 mtc_delta = mtc - data->last_mtc;
692 mtc_delta = mtc + 256 - data->last_mtc;
693 data->ctc_delta += mtc_delta << decoder->mtc_shift;
694 data->last_mtc = mtc;
696 if (decoder->tsc_ctc_mult) {
697 timestamp = data->ctc_timestamp +
698 data->ctc_delta * decoder->tsc_ctc_mult;
700 timestamp = data->ctc_timestamp +
701 multdiv(data->ctc_delta,
702 decoder->tsc_ctc_ratio_n,
703 decoder->tsc_ctc_ratio_d);
706 if (timestamp < data->timestamp)
709 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
710 data->timestamp = timestamp;
718 * For now, do not support using TSC packets - refer
719 * intel_pt_calc_cyc_to_tsc().
723 timestamp = pkt_info->packet.payload |
724 (data->timestamp & (0xffULL << 56));
725 if (data->from_mtc && timestamp < data->timestamp &&
726 data->timestamp - timestamp < decoder->tsc_slip)
728 if (timestamp < data->timestamp)
729 timestamp += (1ULL << 56);
730 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
733 data->tsc_timestamp = timestamp;
734 data->timestamp = timestamp;
743 if (!decoder->tsc_ctc_ratio_d)
746 ctc = pkt_info->packet.payload;
747 fc = pkt_info->packet.count;
748 ctc_rem = ctc & decoder->ctc_rem_mask;
750 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
752 data->ctc_timestamp = data->tsc_timestamp - fc;
753 if (decoder->tsc_ctc_mult) {
754 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
756 data->ctc_timestamp -=
757 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
758 decoder->tsc_ctc_ratio_d);
762 data->have_tma = true;
763 data->fixup_last_mtc = true;
768 data->cycle_cnt += pkt_info->packet.payload;
772 cbr = pkt_info->packet.payload;
773 if (data->cbr && data->cbr != cbr)
776 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
779 case INTEL_PT_TIP_PGD:
780 case INTEL_PT_TRACESTOP:
781 case INTEL_PT_EXSTOP:
782 case INTEL_PT_EXSTOP_IP:
787 case INTEL_PT_BAD: /* Does not happen */
792 if (!data->cbr && decoder->cbr) {
793 data->cbr = decoder->cbr;
794 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
797 if (!data->cycle_cnt)
800 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
802 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
803 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
804 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
805 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
809 decoder->calc_cyc_to_tsc = cyc_to_tsc;
810 decoder->have_calc_cyc_to_tsc = true;
813 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
814 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
816 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
817 cyc_to_tsc, pkt_info->pos);
823 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
826 struct intel_pt_calc_cyc_to_tsc_info data = {
829 .last_mtc = decoder->last_mtc,
830 .ctc_timestamp = decoder->ctc_timestamp,
831 .ctc_delta = decoder->ctc_delta,
832 .tsc_timestamp = decoder->tsc_timestamp,
833 .timestamp = decoder->timestamp,
834 .have_tma = decoder->have_tma,
835 .fixup_last_mtc = decoder->fixup_last_mtc,
836 .from_mtc = from_mtc,
841 * For now, do not support using TSC packets for at least the reasons:
842 * 1) timing might have stopped
843 * 2) TSC packets within PSB+ can slip against CYC packets
848 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
851 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
855 decoder->last_packet_type = decoder->packet.type;
858 decoder->pos += decoder->pkt_step;
859 decoder->buf += decoder->pkt_step;
860 decoder->len -= decoder->pkt_step;
863 ret = intel_pt_get_next_data(decoder);
868 ret = intel_pt_get_packet(decoder->buf, decoder->len,
870 if (ret == INTEL_PT_NEED_MORE_BYTES &&
871 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
872 ret = intel_pt_get_split_packet(decoder);
877 return intel_pt_bad_packet(decoder);
879 decoder->pkt_len = ret;
880 decoder->pkt_step = ret;
881 intel_pt_decoder_log_packet(decoder);
882 } while (decoder->packet.type == INTEL_PT_PAD);
887 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
889 uint64_t timestamp, masked_timestamp;
891 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
892 masked_timestamp = timestamp & decoder->period_mask;
893 if (decoder->continuous_period) {
894 if (masked_timestamp != decoder->last_masked_timestamp)
898 masked_timestamp = timestamp & decoder->period_mask;
899 if (masked_timestamp != decoder->last_masked_timestamp) {
900 decoder->last_masked_timestamp = masked_timestamp;
901 decoder->continuous_period = true;
904 return decoder->period_ticks - (timestamp - masked_timestamp);
907 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
909 switch (decoder->period_type) {
910 case INTEL_PT_PERIOD_INSTRUCTIONS:
911 return decoder->period - decoder->period_insn_cnt;
912 case INTEL_PT_PERIOD_TICKS:
913 return intel_pt_next_period(decoder);
914 case INTEL_PT_PERIOD_NONE:
915 case INTEL_PT_PERIOD_MTC:
921 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
923 uint64_t timestamp, masked_timestamp;
925 switch (decoder->period_type) {
926 case INTEL_PT_PERIOD_INSTRUCTIONS:
927 decoder->period_insn_cnt = 0;
929 case INTEL_PT_PERIOD_TICKS:
930 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
931 masked_timestamp = timestamp & decoder->period_mask;
932 decoder->last_masked_timestamp = masked_timestamp;
934 case INTEL_PT_PERIOD_NONE:
935 case INTEL_PT_PERIOD_MTC:
940 decoder->state.type |= INTEL_PT_INSTRUCTION;
943 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
944 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
946 uint64_t max_insn_cnt, insn_cnt = 0;
949 if (!decoder->mtc_insn)
950 decoder->mtc_insn = true;
952 max_insn_cnt = intel_pt_next_sample(decoder);
954 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
955 max_insn_cnt, decoder->data);
957 decoder->tot_insn_cnt += insn_cnt;
958 decoder->timestamp_insn_cnt += insn_cnt;
959 decoder->sample_insn_cnt += insn_cnt;
960 decoder->period_insn_cnt += insn_cnt;
963 decoder->no_progress = 0;
964 decoder->pkt_state = INTEL_PT_STATE_ERR2;
965 intel_pt_log_at("ERROR: Failed to get instruction",
972 if (ip && decoder->ip == ip) {
977 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
978 intel_pt_sample_insn(decoder);
980 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
981 decoder->state.type = INTEL_PT_INSTRUCTION;
982 decoder->state.from_ip = decoder->ip;
983 decoder->state.to_ip = 0;
984 decoder->ip += intel_pt_insn->length;
985 err = INTEL_PT_RETURN;
989 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
990 /* Zero-length calls are excluded */
991 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
992 intel_pt_insn->rel) {
993 err = intel_pt_push(&decoder->stack, decoder->ip +
994 intel_pt_insn->length);
998 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
999 decoder->ret_addr = intel_pt_pop(&decoder->stack);
1002 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1003 int cnt = decoder->no_progress++;
1005 decoder->state.from_ip = decoder->ip;
1006 decoder->ip += intel_pt_insn->length +
1008 decoder->state.to_ip = decoder->ip;
1009 err = INTEL_PT_RETURN;
1012 * Check for being stuck in a loop. This can happen if a
1013 * decoder error results in the decoder erroneously setting the
1014 * ip to an address that is itself in an infinite loop that
1015 * consumes no packets. When that happens, there must be an
1016 * unconditional branch.
1020 decoder->stuck_ip = decoder->state.to_ip;
1021 decoder->stuck_ip_prd = 1;
1022 decoder->stuck_ip_cnt = 1;
1023 } else if (cnt > INTEL_PT_MAX_LOOPS ||
1024 decoder->state.to_ip == decoder->stuck_ip) {
1025 intel_pt_log_at("ERROR: Never-ending loop",
1026 decoder->state.to_ip);
1027 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1030 } else if (!--decoder->stuck_ip_cnt) {
1031 decoder->stuck_ip_prd += 1;
1032 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1033 decoder->stuck_ip = decoder->state.to_ip;
1036 goto out_no_progress;
1039 decoder->no_progress = 0;
1041 decoder->state.insn_op = intel_pt_insn->op;
1042 decoder->state.insn_len = intel_pt_insn->length;
1043 memcpy(decoder->state.insn, intel_pt_insn->buf,
1044 INTEL_PT_INSN_BUF_SZ);
1046 if (decoder->tx_flags & INTEL_PT_IN_TX)
1047 decoder->state.flags |= INTEL_PT_IN_TX;
1052 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1056 if (decoder->set_fup_tx_flags) {
1057 decoder->set_fup_tx_flags = false;
1058 decoder->tx_flags = decoder->fup_tx_flags;
1059 decoder->state.type = INTEL_PT_TRANSACTION;
1060 decoder->state.from_ip = decoder->ip;
1061 decoder->state.to_ip = 0;
1062 decoder->state.flags = decoder->fup_tx_flags;
1065 if (decoder->set_fup_ptw) {
1066 decoder->set_fup_ptw = false;
1067 decoder->state.type = INTEL_PT_PTW;
1068 decoder->state.flags |= INTEL_PT_FUP_IP;
1069 decoder->state.from_ip = decoder->ip;
1070 decoder->state.to_ip = 0;
1071 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1074 if (decoder->set_fup_mwait) {
1075 decoder->set_fup_mwait = false;
1076 decoder->state.type = INTEL_PT_MWAIT_OP;
1077 decoder->state.from_ip = decoder->ip;
1078 decoder->state.to_ip = 0;
1079 decoder->state.mwait_payload = decoder->fup_mwait_payload;
1082 if (decoder->set_fup_pwre) {
1083 decoder->set_fup_pwre = false;
1084 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1085 decoder->state.type &= ~INTEL_PT_BRANCH;
1086 decoder->state.from_ip = decoder->ip;
1087 decoder->state.to_ip = 0;
1088 decoder->state.pwre_payload = decoder->fup_pwre_payload;
1091 if (decoder->set_fup_exstop) {
1092 decoder->set_fup_exstop = false;
1093 decoder->state.type |= INTEL_PT_EX_STOP;
1094 decoder->state.type &= ~INTEL_PT_BRANCH;
1095 decoder->state.flags |= INTEL_PT_FUP_IP;
1096 decoder->state.from_ip = decoder->ip;
1097 decoder->state.to_ip = 0;
1103 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1104 struct intel_pt_insn *intel_pt_insn,
1105 uint64_t ip, int err)
1107 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1108 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1109 ip == decoder->ip + intel_pt_insn->length;
1112 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1114 struct intel_pt_insn intel_pt_insn;
1118 ip = decoder->last_ip;
1121 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1122 if (err == INTEL_PT_RETURN)
1124 if (err == -EAGAIN ||
1125 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1126 if (intel_pt_fup_event(decoder))
1130 decoder->set_fup_tx_flags = false;
1134 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1135 intel_pt_log_at("ERROR: Unexpected indirect branch",
1137 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1141 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1142 intel_pt_log_at("ERROR: Unexpected conditional branch",
1144 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1148 intel_pt_bug(decoder);
1152 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1154 struct intel_pt_insn intel_pt_insn;
1157 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1158 if (err == INTEL_PT_RETURN &&
1160 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1161 (decoder->state.type & INTEL_PT_BRANCH) &&
1162 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1163 /* Unconditional branch leaving filter region */
1164 decoder->no_progress = 0;
1165 decoder->pge = false;
1166 decoder->continuous_period = false;
1167 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1168 decoder->state.type |= INTEL_PT_TRACE_END;
1171 if (err == INTEL_PT_RETURN)
1176 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1177 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1178 decoder->pge = false;
1179 decoder->continuous_period = false;
1180 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1181 decoder->state.from_ip = decoder->ip;
1182 if (decoder->packet.count == 0) {
1183 decoder->state.to_ip = 0;
1185 decoder->state.to_ip = decoder->last_ip;
1186 decoder->ip = decoder->last_ip;
1188 decoder->state.type |= INTEL_PT_TRACE_END;
1190 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1191 decoder->state.from_ip = decoder->ip;
1192 if (decoder->packet.count == 0) {
1193 decoder->state.to_ip = 0;
1195 decoder->state.to_ip = decoder->last_ip;
1196 decoder->ip = decoder->last_ip;
1202 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1203 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1206 if (decoder->pgd_ip &&
1207 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1208 decoder->pgd_ip(to_ip, decoder->data)) {
1209 /* Conditional branch leaving filter region */
1210 decoder->pge = false;
1211 decoder->continuous_period = false;
1212 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1213 decoder->ip = to_ip;
1214 decoder->state.from_ip = decoder->ip;
1215 decoder->state.to_ip = to_ip;
1216 decoder->state.type |= INTEL_PT_TRACE_END;
1219 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1221 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1225 return intel_pt_bug(decoder);
1228 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1230 struct intel_pt_insn intel_pt_insn;
1234 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1235 if (err == INTEL_PT_RETURN)
1240 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1241 if (!decoder->return_compression) {
1242 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1244 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1247 if (!decoder->ret_addr) {
1248 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1250 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1253 if (!(decoder->tnt.payload & BIT63)) {
1254 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1256 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1259 decoder->tnt.count -= 1;
1260 if (!decoder->tnt.count)
1261 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1262 decoder->tnt.payload <<= 1;
1263 decoder->state.from_ip = decoder->ip;
1264 decoder->ip = decoder->ret_addr;
1265 decoder->state.to_ip = decoder->ip;
1269 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1270 /* Handle deferred TIPs */
1271 err = intel_pt_get_next_packet(decoder);
1274 if (decoder->packet.type != INTEL_PT_TIP ||
1275 decoder->packet.count == 0) {
1276 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1278 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1279 decoder->pkt_step = 0;
1282 intel_pt_set_last_ip(decoder);
1283 decoder->state.from_ip = decoder->ip;
1284 decoder->state.to_ip = decoder->last_ip;
1285 decoder->ip = decoder->last_ip;
1289 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1290 decoder->tnt.count -= 1;
1291 if (!decoder->tnt.count)
1292 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1293 if (decoder->tnt.payload & BIT63) {
1294 decoder->tnt.payload <<= 1;
1295 decoder->state.from_ip = decoder->ip;
1296 decoder->ip += intel_pt_insn.length +
1298 decoder->state.to_ip = decoder->ip;
1301 /* Instruction sample for a non-taken branch */
1302 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1303 decoder->tnt.payload <<= 1;
1304 decoder->state.type = INTEL_PT_INSTRUCTION;
1305 decoder->state.from_ip = decoder->ip;
1306 decoder->state.to_ip = 0;
1307 decoder->ip += intel_pt_insn.length;
1310 decoder->ip += intel_pt_insn.length;
1311 if (!decoder->tnt.count)
1313 decoder->tnt.payload <<= 1;
1317 return intel_pt_bug(decoder);
1321 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1323 unsigned int fup_tx_flags;
1326 fup_tx_flags = decoder->packet.payload &
1327 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1328 err = intel_pt_get_next_packet(decoder);
1331 if (decoder->packet.type == INTEL_PT_FUP) {
1332 decoder->fup_tx_flags = fup_tx_flags;
1333 decoder->set_fup_tx_flags = true;
1334 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1337 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1339 intel_pt_update_in_tx(decoder);
1344 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1348 decoder->have_tma = false;
1350 if (decoder->ref_timestamp) {
1351 timestamp = decoder->packet.payload |
1352 (decoder->ref_timestamp & (0xffULL << 56));
1353 if (timestamp < decoder->ref_timestamp) {
1354 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1355 timestamp += (1ULL << 56);
1357 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1358 timestamp -= (1ULL << 56);
1360 decoder->tsc_timestamp = timestamp;
1361 decoder->timestamp = timestamp;
1362 decoder->ref_timestamp = 0;
1363 decoder->timestamp_insn_cnt = 0;
1364 } else if (decoder->timestamp) {
1365 timestamp = decoder->packet.payload |
1366 (decoder->timestamp & (0xffULL << 56));
1367 decoder->tsc_timestamp = timestamp;
1368 if (timestamp < decoder->timestamp &&
1369 decoder->timestamp - timestamp < decoder->tsc_slip) {
1370 intel_pt_log_to("Suppressing backwards timestamp",
1372 timestamp = decoder->timestamp;
1374 if (timestamp < decoder->timestamp) {
1375 intel_pt_log_to("Wraparound timestamp", timestamp);
1376 timestamp += (1ULL << 56);
1377 decoder->tsc_timestamp = timestamp;
1379 decoder->timestamp = timestamp;
1380 decoder->timestamp_insn_cnt = 0;
1383 if (decoder->last_packet_type == INTEL_PT_CYC) {
1384 decoder->cyc_ref_timestamp = decoder->timestamp;
1385 decoder->cycle_cnt = 0;
1386 decoder->have_calc_cyc_to_tsc = false;
1387 intel_pt_calc_cyc_to_tsc(decoder, false);
1390 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1393 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1395 intel_pt_log("ERROR: Buffer overflow\n");
1396 intel_pt_clear_tx_flags(decoder);
1398 decoder->timestamp_insn_cnt = 0;
1399 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1400 decoder->overflow = true;
1404 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1406 uint32_t ctc = decoder->packet.payload;
1407 uint32_t fc = decoder->packet.count;
1408 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1410 if (!decoder->tsc_ctc_ratio_d)
1413 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1414 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1415 if (decoder->tsc_ctc_mult) {
1416 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1418 decoder->ctc_timestamp -= multdiv(ctc_rem,
1419 decoder->tsc_ctc_ratio_n,
1420 decoder->tsc_ctc_ratio_d);
1422 decoder->ctc_delta = 0;
1423 decoder->have_tma = true;
1424 decoder->fixup_last_mtc = true;
1425 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1426 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1429 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1432 uint32_t mtc, mtc_delta;
1434 if (!decoder->have_tma)
1437 mtc = decoder->packet.payload;
1439 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1440 decoder->fixup_last_mtc = false;
1441 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1442 &decoder->last_mtc);
1445 if (mtc > decoder->last_mtc)
1446 mtc_delta = mtc - decoder->last_mtc;
1448 mtc_delta = mtc + 256 - decoder->last_mtc;
1450 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1452 if (decoder->tsc_ctc_mult) {
1453 timestamp = decoder->ctc_timestamp +
1454 decoder->ctc_delta * decoder->tsc_ctc_mult;
1456 timestamp = decoder->ctc_timestamp +
1457 multdiv(decoder->ctc_delta,
1458 decoder->tsc_ctc_ratio_n,
1459 decoder->tsc_ctc_ratio_d);
1462 if (timestamp < decoder->timestamp)
1463 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1464 timestamp, decoder->timestamp);
1466 decoder->timestamp = timestamp;
1468 decoder->timestamp_insn_cnt = 0;
1469 decoder->last_mtc = mtc;
1471 if (decoder->last_packet_type == INTEL_PT_CYC) {
1472 decoder->cyc_ref_timestamp = decoder->timestamp;
1473 decoder->cycle_cnt = 0;
1474 decoder->have_calc_cyc_to_tsc = false;
1475 intel_pt_calc_cyc_to_tsc(decoder, true);
1478 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1481 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1483 unsigned int cbr = decoder->packet.payload & 0xff;
1485 decoder->cbr_payload = decoder->packet.payload;
1487 if (decoder->cbr == cbr)
1491 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1494 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1496 uint64_t timestamp = decoder->cyc_ref_timestamp;
1498 decoder->have_cyc = true;
1500 decoder->cycle_cnt += decoder->packet.payload;
1502 if (!decoder->cyc_ref_timestamp)
1505 if (decoder->have_calc_cyc_to_tsc)
1506 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1507 else if (decoder->cbr)
1508 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1512 if (timestamp < decoder->timestamp)
1513 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1514 timestamp, decoder->timestamp);
1516 decoder->timestamp = timestamp;
1518 decoder->timestamp_insn_cnt = 0;
1520 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1523 /* Walk PSB+ packets when already in sync. */
1524 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1529 err = intel_pt_get_next_packet(decoder);
1533 switch (decoder->packet.type) {
1534 case INTEL_PT_PSBEND:
1537 case INTEL_PT_TIP_PGD:
1538 case INTEL_PT_TIP_PGE:
1541 case INTEL_PT_TRACESTOP:
1544 case INTEL_PT_PTWRITE:
1545 case INTEL_PT_PTWRITE_IP:
1546 case INTEL_PT_EXSTOP:
1547 case INTEL_PT_EXSTOP_IP:
1548 case INTEL_PT_MWAIT:
1551 decoder->have_tma = false;
1552 intel_pt_log("ERROR: Unexpected packet\n");
1556 return intel_pt_overflow(decoder);
1559 intel_pt_calc_tsc_timestamp(decoder);
1563 intel_pt_calc_tma(decoder);
1567 intel_pt_calc_cbr(decoder);
1570 case INTEL_PT_MODE_EXEC:
1571 decoder->exec_mode = decoder->packet.payload;
1575 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1579 decoder->pge = true;
1580 if (decoder->packet.count)
1581 intel_pt_set_last_ip(decoder);
1584 case INTEL_PT_MODE_TSX:
1585 intel_pt_update_in_tx(decoder);
1589 intel_pt_calc_mtc_timestamp(decoder);
1590 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1591 decoder->state.type |= INTEL_PT_INSTRUCTION;
1604 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1608 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1609 decoder->tx_flags = 0;
1610 decoder->state.flags &= ~INTEL_PT_IN_TX;
1611 decoder->state.flags |= INTEL_PT_ABORT_TX;
1613 decoder->state.flags |= INTEL_PT_ASYNC;
1617 err = intel_pt_get_next_packet(decoder);
1621 switch (decoder->packet.type) {
1624 case INTEL_PT_TRACESTOP:
1628 case INTEL_PT_MODE_TSX:
1630 case INTEL_PT_PSBEND:
1631 case INTEL_PT_PTWRITE:
1632 case INTEL_PT_PTWRITE_IP:
1633 case INTEL_PT_EXSTOP:
1634 case INTEL_PT_EXSTOP_IP:
1635 case INTEL_PT_MWAIT:
1638 intel_pt_log("ERROR: Missing TIP after FUP\n");
1639 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1640 decoder->pkt_step = 0;
1644 intel_pt_calc_cbr(decoder);
1648 return intel_pt_overflow(decoder);
1650 case INTEL_PT_TIP_PGD:
1651 decoder->state.from_ip = decoder->ip;
1652 if (decoder->packet.count == 0) {
1653 decoder->state.to_ip = 0;
1655 intel_pt_set_ip(decoder);
1656 decoder->state.to_ip = decoder->ip;
1658 decoder->pge = false;
1659 decoder->continuous_period = false;
1660 decoder->state.type |= INTEL_PT_TRACE_END;
1663 case INTEL_PT_TIP_PGE:
1664 decoder->pge = true;
1665 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1667 decoder->state.from_ip = 0;
1668 if (decoder->packet.count == 0) {
1669 decoder->state.to_ip = 0;
1671 intel_pt_set_ip(decoder);
1672 decoder->state.to_ip = decoder->ip;
1674 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1678 decoder->state.from_ip = decoder->ip;
1679 if (decoder->packet.count == 0) {
1680 decoder->state.to_ip = 0;
1682 intel_pt_set_ip(decoder);
1683 decoder->state.to_ip = decoder->ip;
1688 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1692 intel_pt_calc_mtc_timestamp(decoder);
1693 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1694 decoder->state.type |= INTEL_PT_INSTRUCTION;
1698 intel_pt_calc_cyc_timestamp(decoder);
1701 case INTEL_PT_MODE_EXEC:
1702 decoder->exec_mode = decoder->packet.payload;
1711 return intel_pt_bug(decoder);
1716 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1718 bool no_tip = false;
1722 err = intel_pt_get_next_packet(decoder);
1726 switch (decoder->packet.type) {
1728 if (!decoder->packet.count)
1730 decoder->tnt = decoder->packet;
1731 decoder->pkt_state = INTEL_PT_STATE_TNT;
1732 err = intel_pt_walk_tnt(decoder);
1737 case INTEL_PT_TIP_PGD:
1738 if (decoder->packet.count != 0)
1739 intel_pt_set_last_ip(decoder);
1740 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1741 return intel_pt_walk_tip(decoder);
1743 case INTEL_PT_TIP_PGE: {
1744 decoder->pge = true;
1745 if (decoder->packet.count == 0) {
1746 intel_pt_log_at("Skipping zero TIP.PGE",
1750 intel_pt_set_ip(decoder);
1751 decoder->state.from_ip = 0;
1752 decoder->state.to_ip = decoder->ip;
1753 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1758 return intel_pt_overflow(decoder);
1761 if (decoder->packet.count != 0)
1762 intel_pt_set_last_ip(decoder);
1763 decoder->pkt_state = INTEL_PT_STATE_TIP;
1764 return intel_pt_walk_tip(decoder);
1767 if (decoder->packet.count == 0) {
1768 intel_pt_log_at("Skipping zero FUP",
1773 intel_pt_set_last_ip(decoder);
1774 if (!decoder->branch_enable) {
1775 decoder->ip = decoder->last_ip;
1776 if (intel_pt_fup_event(decoder))
1781 if (decoder->set_fup_mwait)
1783 err = intel_pt_walk_fup(decoder);
1784 if (err != -EAGAIN) {
1788 decoder->pkt_state =
1789 INTEL_PT_STATE_FUP_NO_TIP;
1791 decoder->pkt_state = INTEL_PT_STATE_FUP;
1798 return intel_pt_walk_fup_tip(decoder);
1800 case INTEL_PT_TRACESTOP:
1801 decoder->pge = false;
1802 decoder->continuous_period = false;
1803 intel_pt_clear_tx_flags(decoder);
1804 decoder->have_tma = false;
1808 decoder->last_ip = 0;
1809 decoder->have_last_ip = true;
1810 intel_pt_clear_stack(&decoder->stack);
1811 err = intel_pt_walk_psbend(decoder);
1819 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1823 intel_pt_calc_mtc_timestamp(decoder);
1824 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1827 * Ensure that there has been an instruction since the
1830 if (!decoder->mtc_insn)
1832 decoder->mtc_insn = false;
1833 /* Ensure that there is a timestamp */
1834 if (!decoder->timestamp)
1836 decoder->state.type = INTEL_PT_INSTRUCTION;
1837 decoder->state.from_ip = decoder->ip;
1838 decoder->state.to_ip = 0;
1839 decoder->mtc_insn = false;
1843 intel_pt_calc_tsc_timestamp(decoder);
1847 intel_pt_calc_tma(decoder);
1851 intel_pt_calc_cyc_timestamp(decoder);
1855 intel_pt_calc_cbr(decoder);
1856 if (!decoder->branch_enable &&
1857 decoder->cbr != decoder->cbr_seen) {
1858 decoder->cbr_seen = decoder->cbr;
1859 decoder->state.type = INTEL_PT_CBR_CHG;
1860 decoder->state.from_ip = decoder->ip;
1861 decoder->state.to_ip = 0;
1862 decoder->state.cbr_payload =
1863 decoder->packet.payload;
1868 case INTEL_PT_MODE_EXEC:
1869 decoder->exec_mode = decoder->packet.payload;
1872 case INTEL_PT_MODE_TSX:
1873 /* MODE_TSX need not be followed by FUP */
1874 if (!decoder->pge) {
1875 intel_pt_update_in_tx(decoder);
1878 err = intel_pt_mode_tsx(decoder, &no_tip);
1883 case INTEL_PT_BAD: /* Does not happen */
1884 return intel_pt_bug(decoder);
1886 case INTEL_PT_PSBEND:
1892 case INTEL_PT_PTWRITE_IP:
1893 decoder->fup_ptw_payload = decoder->packet.payload;
1894 err = intel_pt_get_next_packet(decoder);
1897 if (decoder->packet.type == INTEL_PT_FUP) {
1898 decoder->set_fup_ptw = true;
1901 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1906 case INTEL_PT_PTWRITE:
1907 decoder->state.type = INTEL_PT_PTW;
1908 decoder->state.from_ip = decoder->ip;
1909 decoder->state.to_ip = 0;
1910 decoder->state.ptw_payload = decoder->packet.payload;
1913 case INTEL_PT_MWAIT:
1914 decoder->fup_mwait_payload = decoder->packet.payload;
1915 decoder->set_fup_mwait = true;
1919 if (decoder->set_fup_mwait) {
1920 decoder->fup_pwre_payload =
1921 decoder->packet.payload;
1922 decoder->set_fup_pwre = true;
1925 decoder->state.type = INTEL_PT_PWR_ENTRY;
1926 decoder->state.from_ip = decoder->ip;
1927 decoder->state.to_ip = 0;
1928 decoder->state.pwrx_payload = decoder->packet.payload;
1931 case INTEL_PT_EXSTOP_IP:
1932 err = intel_pt_get_next_packet(decoder);
1935 if (decoder->packet.type == INTEL_PT_FUP) {
1936 decoder->set_fup_exstop = true;
1939 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
1944 case INTEL_PT_EXSTOP:
1945 decoder->state.type = INTEL_PT_EX_STOP;
1946 decoder->state.from_ip = decoder->ip;
1947 decoder->state.to_ip = 0;
1951 decoder->state.type = INTEL_PT_PWR_EXIT;
1952 decoder->state.from_ip = decoder->ip;
1953 decoder->state.to_ip = 0;
1954 decoder->state.pwrx_payload = decoder->packet.payload;
1958 return intel_pt_bug(decoder);
1963 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1965 return decoder->packet.count &&
1966 (decoder->have_last_ip || decoder->packet.count == 3 ||
1967 decoder->packet.count == 6);
1970 /* Walk PSB+ packets to get in sync. */
1971 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1976 err = intel_pt_get_next_packet(decoder);
1980 switch (decoder->packet.type) {
1981 case INTEL_PT_TIP_PGD:
1982 decoder->continuous_period = false;
1984 case INTEL_PT_TIP_PGE:
1986 case INTEL_PT_PTWRITE:
1987 case INTEL_PT_PTWRITE_IP:
1988 case INTEL_PT_EXSTOP:
1989 case INTEL_PT_EXSTOP_IP:
1990 case INTEL_PT_MWAIT:
1993 intel_pt_log("ERROR: Unexpected packet\n");
1997 decoder->pge = true;
1998 if (intel_pt_have_ip(decoder)) {
1999 uint64_t current_ip = decoder->ip;
2001 intel_pt_set_ip(decoder);
2003 intel_pt_log_to("Setting IP",
2009 intel_pt_calc_mtc_timestamp(decoder);
2013 intel_pt_calc_tsc_timestamp(decoder);
2017 intel_pt_calc_tma(decoder);
2021 intel_pt_calc_cyc_timestamp(decoder);
2025 intel_pt_calc_cbr(decoder);
2029 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2032 case INTEL_PT_MODE_EXEC:
2033 decoder->exec_mode = decoder->packet.payload;
2036 case INTEL_PT_MODE_TSX:
2037 intel_pt_update_in_tx(decoder);
2040 case INTEL_PT_TRACESTOP:
2041 decoder->pge = false;
2042 decoder->continuous_period = false;
2043 intel_pt_clear_tx_flags(decoder);
2047 decoder->have_tma = false;
2048 intel_pt_log("ERROR: Unexpected packet\n");
2050 decoder->pkt_state = INTEL_PT_STATE_ERR4;
2052 decoder->pkt_state = INTEL_PT_STATE_ERR3;
2055 case INTEL_PT_BAD: /* Does not happen */
2056 return intel_pt_bug(decoder);
2059 return intel_pt_overflow(decoder);
2061 case INTEL_PT_PSBEND:
2074 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2079 err = intel_pt_get_next_packet(decoder);
2083 switch (decoder->packet.type) {
2084 case INTEL_PT_TIP_PGD:
2085 decoder->continuous_period = false;
2087 case INTEL_PT_TIP_PGE:
2089 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
2090 if (intel_pt_have_ip(decoder))
2091 intel_pt_set_ip(decoder);
2094 if (decoder->packet.type == INTEL_PT_TIP_PGE)
2095 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2096 if (decoder->packet.type == INTEL_PT_TIP_PGD)
2097 decoder->state.type |= INTEL_PT_TRACE_END;
2101 if (intel_pt_have_ip(decoder))
2102 intel_pt_set_ip(decoder);
2108 intel_pt_calc_mtc_timestamp(decoder);
2112 intel_pt_calc_tsc_timestamp(decoder);
2116 intel_pt_calc_tma(decoder);
2120 intel_pt_calc_cyc_timestamp(decoder);
2124 intel_pt_calc_cbr(decoder);
2128 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2131 case INTEL_PT_MODE_EXEC:
2132 decoder->exec_mode = decoder->packet.payload;
2135 case INTEL_PT_MODE_TSX:
2136 intel_pt_update_in_tx(decoder);
2140 return intel_pt_overflow(decoder);
2142 case INTEL_PT_BAD: /* Does not happen */
2143 return intel_pt_bug(decoder);
2145 case INTEL_PT_TRACESTOP:
2146 decoder->pge = false;
2147 decoder->continuous_period = false;
2148 intel_pt_clear_tx_flags(decoder);
2149 decoder->have_tma = false;
2153 decoder->last_ip = 0;
2154 decoder->have_last_ip = true;
2155 intel_pt_clear_stack(&decoder->stack);
2156 err = intel_pt_walk_psb(decoder);
2160 /* Do not have a sample */
2161 decoder->state.type = 0;
2167 case INTEL_PT_PSBEND:
2171 case INTEL_PT_PTWRITE:
2172 case INTEL_PT_PTWRITE_IP:
2173 case INTEL_PT_EXSTOP:
2174 case INTEL_PT_EXSTOP_IP:
2175 case INTEL_PT_MWAIT:
2184 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2188 decoder->set_fup_tx_flags = false;
2189 decoder->set_fup_ptw = false;
2190 decoder->set_fup_mwait = false;
2191 decoder->set_fup_pwre = false;
2192 decoder->set_fup_exstop = false;
2194 if (!decoder->branch_enable) {
2195 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2196 decoder->overflow = false;
2197 decoder->state.type = 0; /* Do not have a sample */
2201 intel_pt_log("Scanning for full IP\n");
2202 err = intel_pt_walk_to_ip(decoder);
2206 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2207 decoder->overflow = false;
2209 decoder->state.from_ip = 0;
2210 decoder->state.to_ip = decoder->ip;
2211 intel_pt_log_to("Setting IP", decoder->ip);
2216 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2218 const unsigned char *end = decoder->buf + decoder->len;
2221 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2222 if (i > decoder->len)
2224 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2230 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2232 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2233 const char *psb = INTEL_PT_PSB_STR;
2235 if (rest_psb > decoder->len ||
2236 memcmp(decoder->buf, psb + part_psb, rest_psb))
2242 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2247 decoder->pos += decoder->len;
2250 ret = intel_pt_get_next_data(decoder);
2254 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2258 decoder->pos -= part_psb;
2259 decoder->next_buf = decoder->buf + rest_psb;
2260 decoder->next_len = decoder->len - rest_psb;
2261 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2262 decoder->buf = decoder->temp_buf;
2263 decoder->len = INTEL_PT_PSB_LEN;
2268 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2270 unsigned char *next;
2273 intel_pt_log("Scanning for PSB\n");
2275 if (!decoder->len) {
2276 ret = intel_pt_get_next_data(decoder);
2281 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2286 part_psb = intel_pt_part_psb(decoder);
2288 ret = intel_pt_get_split_psb(decoder, part_psb);
2292 decoder->pos += decoder->len;
2298 decoder->pkt_step = next - decoder->buf;
2299 return intel_pt_get_next_packet(decoder);
2303 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2307 decoder->pge = false;
2308 decoder->continuous_period = false;
2309 decoder->have_last_ip = false;
2310 decoder->last_ip = 0;
2312 intel_pt_clear_stack(&decoder->stack);
2314 err = intel_pt_scan_for_psb(decoder);
2318 decoder->have_last_ip = true;
2319 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2321 err = intel_pt_walk_psb(decoder);
2326 decoder->state.type = 0; /* Do not have a sample */
2327 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2329 return intel_pt_sync_ip(decoder);
2335 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2337 uint64_t est = decoder->sample_insn_cnt << 1;
2339 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2342 est *= decoder->max_non_turbo_ratio;
2343 est /= decoder->cbr;
2345 return decoder->sample_timestamp + est;
2348 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2353 decoder->state.type = INTEL_PT_BRANCH;
2354 decoder->state.flags = 0;
2356 switch (decoder->pkt_state) {
2357 case INTEL_PT_STATE_NO_PSB:
2358 err = intel_pt_sync(decoder);
2360 case INTEL_PT_STATE_NO_IP:
2361 decoder->have_last_ip = false;
2362 decoder->last_ip = 0;
2365 case INTEL_PT_STATE_ERR_RESYNC:
2366 err = intel_pt_sync_ip(decoder);
2368 case INTEL_PT_STATE_IN_SYNC:
2369 err = intel_pt_walk_trace(decoder);
2371 case INTEL_PT_STATE_TNT:
2372 err = intel_pt_walk_tnt(decoder);
2374 err = intel_pt_walk_trace(decoder);
2376 case INTEL_PT_STATE_TIP:
2377 case INTEL_PT_STATE_TIP_PGD:
2378 err = intel_pt_walk_tip(decoder);
2380 case INTEL_PT_STATE_FUP:
2381 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2382 err = intel_pt_walk_fup(decoder);
2384 err = intel_pt_walk_fup_tip(decoder);
2386 decoder->pkt_state = INTEL_PT_STATE_FUP;
2388 case INTEL_PT_STATE_FUP_NO_TIP:
2389 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2390 err = intel_pt_walk_fup(decoder);
2392 err = intel_pt_walk_trace(decoder);
2395 err = intel_pt_bug(decoder);
2398 } while (err == -ENOLINK);
2401 decoder->state.err = intel_pt_ext_err(err);
2402 decoder->state.from_ip = decoder->ip;
2403 decoder->sample_timestamp = decoder->timestamp;
2404 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2406 decoder->state.err = 0;
2407 if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2408 decoder->cbr_seen = decoder->cbr;
2409 decoder->state.type |= INTEL_PT_CBR_CHG;
2410 decoder->state.cbr_payload = decoder->cbr_payload;
2412 if (intel_pt_sample_time(decoder->pkt_state)) {
2413 decoder->sample_timestamp = decoder->timestamp;
2414 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2418 decoder->state.timestamp = decoder->sample_timestamp;
2419 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2420 decoder->state.cr3 = decoder->cr3;
2421 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2423 return &decoder->state;
2427 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2428 * @buf: pointer to buffer pointer
2429 * @len: size of buffer
2431 * Updates the buffer pointer to point to the start of the next PSB packet if
2432 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2433 * @len is adjusted accordingly.
2435 * Return: %true if a PSB packet is found, %false otherwise.
2437 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2439 unsigned char *next;
2441 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2443 *len -= next - *buf;
2451 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2453 * @buf: pointer to buffer pointer
2454 * @len: size of buffer
2456 * Updates the buffer pointer to point to the start of the following PSB packet
2457 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2458 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2460 * Return: %true if a PSB packet is found, %false otherwise.
2462 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2464 unsigned char *next;
2469 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2471 *len -= next - *buf;
2479 * intel_pt_last_psb - find the last PSB packet in a buffer.
2481 * @len: size of buffer
2483 * This function finds the last PSB in a buffer.
2485 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2487 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2489 const char *n = INTEL_PT_PSB_STR;
2493 if (len < INTEL_PT_PSB_LEN)
2496 k = len - INTEL_PT_PSB_LEN + 1;
2498 p = memrchr(buf, n[0], k);
2501 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2510 * intel_pt_next_tsc - find and return next TSC.
2512 * @len: size of buffer
2513 * @tsc: TSC value returned
2514 * @rem: returns remaining size when TSC is found
2516 * Find a TSC packet in @buf and return the TSC value. This function assumes
2517 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2518 * PSBEND packet is found.
2520 * Return: %true if TSC is found, false otherwise.
2522 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2525 struct intel_pt_pkt packet;
2529 ret = intel_pt_get_packet(buf, len, &packet);
2532 if (packet.type == INTEL_PT_TSC) {
2533 *tsc = packet.payload;
2537 if (packet.type == INTEL_PT_PSBEND)
2546 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2547 * @tsc1: first TSC to compare
2548 * @tsc2: second TSC to compare
2550 * This function compares 7-byte TSC values allowing for the possibility that
2551 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2552 * around so for that purpose this function assumes the absolute difference is
2553 * less than half the maximum difference.
2555 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2558 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2560 const uint64_t halfway = (1ULL << 55);
2566 if (tsc2 - tsc1 < halfway)
2571 if (tsc1 - tsc2 < halfway)
2579 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2581 * @buf_a: first buffer
2582 * @len_a: size of first buffer
2583 * @buf_b: second buffer
2584 * @len_b: size of second buffer
2585 * @consecutive: returns true if there is data in buf_b that is consecutive
2588 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2589 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2590 * walk forward in @buf_b until a later TSC is found. A precondition is that
2591 * @buf_a and @buf_b are positioned at a PSB.
2593 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2594 * @buf_b + @len_b if there is no non-overlapped data.
2596 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2598 unsigned char *buf_b,
2599 size_t len_b, bool *consecutive)
2601 uint64_t tsc_a, tsc_b;
2603 size_t len, rem_a, rem_b;
2605 p = intel_pt_last_psb(buf_a, len_a);
2607 return buf_b; /* No PSB in buf_a => no overlap */
2609 len = len_a - (p - buf_a);
2610 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2611 /* The last PSB+ in buf_a is incomplete, so go back one more */
2613 p = intel_pt_last_psb(buf_a, len_a);
2615 return buf_b; /* No full PSB+ => assume no overlap */
2616 len = len_a - (p - buf_a);
2617 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2618 return buf_b; /* No TSC in buf_a => assume no overlap */
2622 /* Ignore PSB+ with no TSC */
2623 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2624 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2626 /* Same TSC, so buffers are consecutive */
2627 if (!cmp && rem_b >= rem_a) {
2628 *consecutive = true;
2629 return buf_b + len_b - (rem_b - rem_a);
2632 return buf_b; /* tsc_a < tsc_b => no overlap */
2635 if (!intel_pt_step_psb(&buf_b, &len_b))
2636 return buf_b + len_b; /* No PSB in buf_b => no data */
2641 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2642 * @buf_a: first buffer
2643 * @len_a: size of first buffer
2644 * @buf_b: second buffer
2645 * @len_b: size of second buffer
2646 * @have_tsc: can use TSC packets to detect overlap
2647 * @consecutive: returns true if there is data in buf_b that is consecutive
2650 * When trace samples or snapshots are recorded there is the possibility that
2651 * the data overlaps. Note that, for the purposes of decoding, data is only
2652 * useful if it begins with a PSB packet.
2654 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2655 * @buf_b + @len_b if there is no non-overlapped data.
2657 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2658 unsigned char *buf_b, size_t len_b,
2659 bool have_tsc, bool *consecutive)
2661 unsigned char *found;
2663 /* Buffer 'b' must start at PSB so throw away everything before that */
2664 if (!intel_pt_next_psb(&buf_b, &len_b))
2665 return buf_b + len_b; /* No PSB */
2667 if (!intel_pt_next_psb(&buf_a, &len_a))
2668 return buf_b; /* No overlap */
2671 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2678 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2679 * we can ignore the first part of buffer 'a'.
2681 while (len_b < len_a) {
2682 if (!intel_pt_step_psb(&buf_a, &len_a))
2683 return buf_b; /* No overlap */
2686 /* Now len_b >= len_a */
2688 /* Potential overlap so check the bytes */
2689 found = memmem(buf_a, len_a, buf_b, len_a);
2691 *consecutive = true;
2692 return buf_b + len_a;
2695 /* Try again at next PSB in buffer 'a' */
2696 if (!intel_pt_step_psb(&buf_a, &len_a))
2697 return buf_b; /* No overlap */