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);
104 struct intel_pt_state state;
105 const unsigned char *buf;
107 bool return_compression;
114 enum intel_pt_param_flags flags;
120 uint64_t tsc_timestamp;
121 uint64_t ref_timestamp;
122 uint64_t sample_timestamp;
124 uint64_t ctc_timestamp;
127 uint64_t cyc_ref_timestamp;
129 uint32_t tsc_ctc_ratio_n;
130 uint32_t tsc_ctc_ratio_d;
131 uint32_t tsc_ctc_mult;
133 uint32_t ctc_rem_mask;
135 struct intel_pt_stack stack;
136 enum intel_pt_pkt_state pkt_state;
137 struct intel_pt_pkt packet;
138 struct intel_pt_pkt tnt;
141 int last_packet_type;
143 unsigned int max_non_turbo_ratio;
144 double max_non_turbo_ratio_fp;
145 double cbr_cyc_to_tsc;
146 double calc_cyc_to_tsc;
147 bool have_calc_cyc_to_tsc;
149 unsigned int insn_bytes;
151 enum intel_pt_period_type period_type;
152 uint64_t tot_insn_cnt;
153 uint64_t period_insn_cnt;
154 uint64_t period_mask;
155 uint64_t period_ticks;
156 uint64_t last_masked_timestamp;
157 bool continuous_period;
159 bool set_fup_tx_flags;
160 unsigned int fup_tx_flags;
161 unsigned int tx_flags;
162 uint64_t timestamp_insn_cnt;
163 uint64_t sample_insn_cnt;
168 const unsigned char *next_buf;
170 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
173 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
177 for (i = 0; x != 1; i++)
183 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
185 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
188 period = intel_pt_lower_power_of_2(decoder->period);
189 decoder->period_mask = ~(period - 1);
190 decoder->period_ticks = period;
194 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
198 return (t / d) * n + ((t % d) * n) / d;
201 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
203 struct intel_pt_decoder *decoder;
205 if (!params->get_trace || !params->walk_insn)
208 decoder = zalloc(sizeof(struct intel_pt_decoder));
212 decoder->get_trace = params->get_trace;
213 decoder->walk_insn = params->walk_insn;
214 decoder->data = params->data;
215 decoder->return_compression = params->return_compression;
217 decoder->flags = params->flags;
219 decoder->period = params->period;
220 decoder->period_type = params->period_type;
222 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
223 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
225 intel_pt_setup_period(decoder);
227 decoder->mtc_shift = params->mtc_period;
228 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
230 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
231 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
233 if (!decoder->tsc_ctc_ratio_n)
234 decoder->tsc_ctc_ratio_d = 0;
236 if (decoder->tsc_ctc_ratio_d) {
237 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
238 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
239 decoder->tsc_ctc_ratio_d;
242 * Allow for timestamps appearing to backwards because a TSC
243 * packet has slipped past a MTC packet, so allow 2 MTC ticks
246 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
247 decoder->tsc_ctc_ratio_n,
248 decoder->tsc_ctc_ratio_d);
250 /* ... or 0x100 paranoia */
251 if (decoder->tsc_slip < 0x100)
252 decoder->tsc_slip = 0x100;
254 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
255 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
256 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
257 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
258 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
263 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
265 struct intel_pt_blk *blk = stack->blk;
267 stack->blk = blk->prev;
274 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
279 intel_pt_pop_blk(stack);
282 stack->pos = INTEL_PT_BLK_SIZE;
284 return stack->blk->ip[--stack->pos];
287 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
289 struct intel_pt_blk *blk;
295 blk = malloc(sizeof(struct intel_pt_blk));
300 blk->prev = stack->blk;
306 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
310 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
311 err = intel_pt_alloc_blk(stack);
316 stack->blk->ip[stack->pos++] = ip;
320 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
323 intel_pt_pop_blk(stack);
327 static void intel_pt_free_stack(struct intel_pt_stack *stack)
329 intel_pt_clear_stack(stack);
331 zfree(&stack->spare);
334 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
336 intel_pt_free_stack(&decoder->stack);
340 static int intel_pt_ext_err(int code)
344 return INTEL_PT_ERR_NOMEM;
346 return INTEL_PT_ERR_INTERN;
348 return INTEL_PT_ERR_BADPKT;
350 return INTEL_PT_ERR_NODATA;
352 return INTEL_PT_ERR_NOINSN;
354 return INTEL_PT_ERR_MISMAT;
356 return INTEL_PT_ERR_OVR;
358 return INTEL_PT_ERR_LOST;
360 return INTEL_PT_ERR_NELOOP;
362 return INTEL_PT_ERR_UNK;
366 static const char *intel_pt_err_msgs[] = {
367 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
368 [INTEL_PT_ERR_INTERN] = "Internal error",
369 [INTEL_PT_ERR_BADPKT] = "Bad packet",
370 [INTEL_PT_ERR_NODATA] = "No more data",
371 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
372 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
373 [INTEL_PT_ERR_OVR] = "Overflow packet",
374 [INTEL_PT_ERR_LOST] = "Lost trace data",
375 [INTEL_PT_ERR_UNK] = "Unknown error!",
376 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
379 int intel_pt__strerror(int code, char *buf, size_t buflen)
381 if (code < 1 || code > INTEL_PT_ERR_MAX)
382 code = INTEL_PT_ERR_UNK;
383 strlcpy(buf, intel_pt_err_msgs[code], buflen);
387 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
392 switch (packet->count) {
394 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
398 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
402 ip = packet->payload;
403 /* Sign-extend 6-byte ip */
404 if (ip & (uint64_t)0x800000000000ULL)
405 ip |= (uint64_t)0xffff000000000000ULL;
408 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
412 ip = packet->payload;
421 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
423 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
424 decoder->have_last_ip = true;
427 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
429 intel_pt_set_last_ip(decoder);
430 decoder->ip = decoder->last_ip;
433 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
435 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
439 static int intel_pt_bug(struct intel_pt_decoder *decoder)
441 intel_pt_log("ERROR: Internal error\n");
442 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
446 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
448 decoder->tx_flags = 0;
451 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
453 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
456 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
458 intel_pt_clear_tx_flags(decoder);
459 decoder->have_tma = false;
460 decoder->pkt_len = 1;
461 decoder->pkt_step = 1;
462 intel_pt_decoder_log_packet(decoder);
463 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
464 intel_pt_log("ERROR: Bad packet\n");
465 decoder->pkt_state = INTEL_PT_STATE_ERR1;
470 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
472 struct intel_pt_buffer buffer = { .buf = 0, };
475 decoder->pkt_step = 0;
477 intel_pt_log("Getting more data\n");
478 ret = decoder->get_trace(&buffer, decoder->data);
481 decoder->buf = buffer.buf;
482 decoder->len = buffer.len;
484 intel_pt_log("No more data\n");
487 if (!buffer.consecutive) {
489 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
490 decoder->ref_timestamp = buffer.ref_timestamp;
491 decoder->timestamp = 0;
492 decoder->have_tma = false;
493 decoder->state.trace_nr = buffer.trace_nr;
494 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
495 decoder->ref_timestamp);
502 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
504 if (!decoder->next_buf)
505 return intel_pt_get_data(decoder);
507 decoder->buf = decoder->next_buf;
508 decoder->len = decoder->next_len;
509 decoder->next_buf = 0;
510 decoder->next_len = 0;
514 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
516 unsigned char *buf = decoder->temp_buf;
517 size_t old_len, len, n;
520 old_len = decoder->len;
522 memcpy(buf, decoder->buf, len);
524 ret = intel_pt_get_data(decoder);
526 decoder->pos += old_len;
527 return ret < 0 ? ret : -EINVAL;
530 n = INTEL_PT_PKT_MAX_SZ - len;
531 if (n > decoder->len)
533 memcpy(buf + len, decoder->buf, n);
536 ret = intel_pt_get_packet(buf, len, &decoder->packet);
537 if (ret < (int)old_len) {
538 decoder->next_buf = decoder->buf;
539 decoder->next_len = decoder->len;
541 decoder->len = old_len;
542 return intel_pt_bad_packet(decoder);
545 decoder->next_buf = decoder->buf + (ret - old_len);
546 decoder->next_len = decoder->len - (ret - old_len);
554 struct intel_pt_pkt_info {
555 struct intel_pt_decoder *decoder;
556 struct intel_pt_pkt packet;
559 int last_packet_type;
563 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
565 /* Lookahead packets in current buffer */
566 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
567 intel_pt_pkt_cb_t cb, void *data)
569 struct intel_pt_pkt_info pkt_info;
570 const unsigned char *buf = decoder->buf;
571 size_t len = decoder->len;
574 pkt_info.decoder = decoder;
575 pkt_info.pos = decoder->pos;
576 pkt_info.pkt_len = decoder->pkt_step;
577 pkt_info.last_packet_type = decoder->last_packet_type;
578 pkt_info.data = data;
582 pkt_info.pos += pkt_info.pkt_len;
583 buf += pkt_info.pkt_len;
584 len -= pkt_info.pkt_len;
587 return INTEL_PT_NEED_MORE_BYTES;
589 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
591 return INTEL_PT_NEED_MORE_BYTES;
595 pkt_info.pkt_len = ret;
596 } while (pkt_info.packet.type == INTEL_PT_PAD);
602 pkt_info.last_packet_type = pkt_info.packet.type;
606 struct intel_pt_calc_cyc_to_tsc_info {
610 uint64_t ctc_timestamp;
612 uint64_t tsc_timestamp;
617 double cbr_cyc_to_tsc;
621 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
622 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
623 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
624 * packet by copying the missing bits from the current MTC assuming the least
625 * difference between the two, and that the current MTC comes after last_mtc.
627 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
630 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
631 uint32_t mask = ~(first_missing_bit - 1);
633 *last_mtc |= mtc & mask;
634 if (*last_mtc >= mtc) {
635 *last_mtc -= first_missing_bit;
640 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
642 struct intel_pt_decoder *decoder = pkt_info->decoder;
643 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
647 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
649 switch (pkt_info->packet.type) {
651 case INTEL_PT_TIP_PGE:
656 case INTEL_PT_MODE_EXEC:
657 case INTEL_PT_MODE_TSX:
658 case INTEL_PT_PSBEND:
668 mtc = pkt_info->packet.payload;
669 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
670 data->fixup_last_mtc = false;
671 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
674 if (mtc > data->last_mtc)
675 mtc_delta = mtc - data->last_mtc;
677 mtc_delta = mtc + 256 - data->last_mtc;
678 data->ctc_delta += mtc_delta << decoder->mtc_shift;
679 data->last_mtc = mtc;
681 if (decoder->tsc_ctc_mult) {
682 timestamp = data->ctc_timestamp +
683 data->ctc_delta * decoder->tsc_ctc_mult;
685 timestamp = data->ctc_timestamp +
686 multdiv(data->ctc_delta,
687 decoder->tsc_ctc_ratio_n,
688 decoder->tsc_ctc_ratio_d);
691 if (timestamp < data->timestamp)
694 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
695 data->timestamp = timestamp;
702 timestamp = pkt_info->packet.payload |
703 (data->timestamp & (0xffULL << 56));
704 if (data->from_mtc && timestamp < data->timestamp &&
705 data->timestamp - timestamp < decoder->tsc_slip)
707 if (timestamp < data->timestamp)
708 timestamp += (1ULL << 56);
709 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
712 data->tsc_timestamp = timestamp;
713 data->timestamp = timestamp;
722 if (!decoder->tsc_ctc_ratio_d)
725 ctc = pkt_info->packet.payload;
726 fc = pkt_info->packet.count;
727 ctc_rem = ctc & decoder->ctc_rem_mask;
729 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
731 data->ctc_timestamp = data->tsc_timestamp - fc;
732 if (decoder->tsc_ctc_mult) {
733 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
735 data->ctc_timestamp -=
736 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
737 decoder->tsc_ctc_ratio_d);
741 data->have_tma = true;
742 data->fixup_last_mtc = true;
747 data->cycle_cnt += pkt_info->packet.payload;
751 cbr = pkt_info->packet.payload;
752 if (data->cbr && data->cbr != cbr)
755 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
758 case INTEL_PT_TIP_PGD:
759 case INTEL_PT_TRACESTOP:
761 case INTEL_PT_BAD: /* Does not happen */
766 if (!data->cbr && decoder->cbr) {
767 data->cbr = decoder->cbr;
768 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
771 if (!data->cycle_cnt)
774 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
776 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
777 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
778 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
779 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
783 decoder->calc_cyc_to_tsc = cyc_to_tsc;
784 decoder->have_calc_cyc_to_tsc = true;
787 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
788 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
790 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
791 cyc_to_tsc, pkt_info->pos);
797 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
800 struct intel_pt_calc_cyc_to_tsc_info data = {
803 .last_mtc = decoder->last_mtc,
804 .ctc_timestamp = decoder->ctc_timestamp,
805 .ctc_delta = decoder->ctc_delta,
806 .tsc_timestamp = decoder->tsc_timestamp,
807 .timestamp = decoder->timestamp,
808 .have_tma = decoder->have_tma,
809 .fixup_last_mtc = decoder->fixup_last_mtc,
810 .from_mtc = from_mtc,
814 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
817 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
821 decoder->last_packet_type = decoder->packet.type;
824 decoder->pos += decoder->pkt_step;
825 decoder->buf += decoder->pkt_step;
826 decoder->len -= decoder->pkt_step;
829 ret = intel_pt_get_next_data(decoder);
834 ret = intel_pt_get_packet(decoder->buf, decoder->len,
836 if (ret == INTEL_PT_NEED_MORE_BYTES &&
837 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
838 ret = intel_pt_get_split_packet(decoder);
843 return intel_pt_bad_packet(decoder);
845 decoder->pkt_len = ret;
846 decoder->pkt_step = ret;
847 intel_pt_decoder_log_packet(decoder);
848 } while (decoder->packet.type == INTEL_PT_PAD);
853 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
855 uint64_t timestamp, masked_timestamp;
857 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
858 masked_timestamp = timestamp & decoder->period_mask;
859 if (decoder->continuous_period) {
860 if (masked_timestamp != decoder->last_masked_timestamp)
864 masked_timestamp = timestamp & decoder->period_mask;
865 if (masked_timestamp != decoder->last_masked_timestamp) {
866 decoder->last_masked_timestamp = masked_timestamp;
867 decoder->continuous_period = true;
870 return decoder->period_ticks - (timestamp - masked_timestamp);
873 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
875 switch (decoder->period_type) {
876 case INTEL_PT_PERIOD_INSTRUCTIONS:
877 return decoder->period - decoder->period_insn_cnt;
878 case INTEL_PT_PERIOD_TICKS:
879 return intel_pt_next_period(decoder);
880 case INTEL_PT_PERIOD_NONE:
881 case INTEL_PT_PERIOD_MTC:
887 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
889 uint64_t timestamp, masked_timestamp;
891 switch (decoder->period_type) {
892 case INTEL_PT_PERIOD_INSTRUCTIONS:
893 decoder->period_insn_cnt = 0;
895 case INTEL_PT_PERIOD_TICKS:
896 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
897 masked_timestamp = timestamp & decoder->period_mask;
898 decoder->last_masked_timestamp = masked_timestamp;
900 case INTEL_PT_PERIOD_NONE:
901 case INTEL_PT_PERIOD_MTC:
906 decoder->state.type |= INTEL_PT_INSTRUCTION;
909 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
910 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
912 uint64_t max_insn_cnt, insn_cnt = 0;
915 if (!decoder->mtc_insn)
916 decoder->mtc_insn = true;
918 max_insn_cnt = intel_pt_next_sample(decoder);
920 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
921 max_insn_cnt, decoder->data);
923 decoder->tot_insn_cnt += insn_cnt;
924 decoder->timestamp_insn_cnt += insn_cnt;
925 decoder->sample_insn_cnt += insn_cnt;
926 decoder->period_insn_cnt += insn_cnt;
929 decoder->no_progress = 0;
930 decoder->pkt_state = INTEL_PT_STATE_ERR2;
931 intel_pt_log_at("ERROR: Failed to get instruction",
938 if (ip && decoder->ip == ip) {
943 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
944 intel_pt_sample_insn(decoder);
946 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
947 decoder->state.type = INTEL_PT_INSTRUCTION;
948 decoder->state.from_ip = decoder->ip;
949 decoder->state.to_ip = 0;
950 decoder->ip += intel_pt_insn->length;
951 err = INTEL_PT_RETURN;
955 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
956 /* Zero-length calls are excluded */
957 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
958 intel_pt_insn->rel) {
959 err = intel_pt_push(&decoder->stack, decoder->ip +
960 intel_pt_insn->length);
964 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
965 decoder->ret_addr = intel_pt_pop(&decoder->stack);
968 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
969 int cnt = decoder->no_progress++;
971 decoder->state.from_ip = decoder->ip;
972 decoder->ip += intel_pt_insn->length +
974 decoder->state.to_ip = decoder->ip;
975 err = INTEL_PT_RETURN;
978 * Check for being stuck in a loop. This can happen if a
979 * decoder error results in the decoder erroneously setting the
980 * ip to an address that is itself in an infinite loop that
981 * consumes no packets. When that happens, there must be an
982 * unconditional branch.
986 decoder->stuck_ip = decoder->state.to_ip;
987 decoder->stuck_ip_prd = 1;
988 decoder->stuck_ip_cnt = 1;
989 } else if (cnt > INTEL_PT_MAX_LOOPS ||
990 decoder->state.to_ip == decoder->stuck_ip) {
991 intel_pt_log_at("ERROR: Never-ending loop",
992 decoder->state.to_ip);
993 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
996 } else if (!--decoder->stuck_ip_cnt) {
997 decoder->stuck_ip_prd += 1;
998 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
999 decoder->stuck_ip = decoder->state.to_ip;
1002 goto out_no_progress;
1005 decoder->no_progress = 0;
1007 decoder->state.insn_op = intel_pt_insn->op;
1008 decoder->state.insn_len = intel_pt_insn->length;
1010 if (decoder->tx_flags & INTEL_PT_IN_TX)
1011 decoder->state.flags |= INTEL_PT_IN_TX;
1016 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1017 struct intel_pt_insn *intel_pt_insn,
1018 uint64_t ip, int err)
1020 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1021 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1022 ip == decoder->ip + intel_pt_insn->length;
1025 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1027 struct intel_pt_insn intel_pt_insn;
1031 ip = decoder->last_ip;
1034 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1035 if (err == INTEL_PT_RETURN)
1037 if (err == -EAGAIN ||
1038 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1039 if (decoder->set_fup_tx_flags) {
1040 decoder->set_fup_tx_flags = false;
1041 decoder->tx_flags = decoder->fup_tx_flags;
1042 decoder->state.type = INTEL_PT_TRANSACTION;
1043 decoder->state.from_ip = decoder->ip;
1044 decoder->state.to_ip = 0;
1045 decoder->state.flags = decoder->fup_tx_flags;
1050 decoder->set_fup_tx_flags = false;
1054 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1055 intel_pt_log_at("ERROR: Unexpected indirect branch",
1057 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1061 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1062 intel_pt_log_at("ERROR: Unexpected conditional branch",
1064 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1068 intel_pt_bug(decoder);
1072 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1074 struct intel_pt_insn intel_pt_insn;
1077 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1078 if (err == INTEL_PT_RETURN)
1083 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1084 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1085 decoder->pge = false;
1086 decoder->continuous_period = false;
1087 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1088 decoder->state.from_ip = decoder->ip;
1089 decoder->state.to_ip = 0;
1090 if (decoder->packet.count != 0)
1091 decoder->ip = decoder->last_ip;
1093 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1094 decoder->state.from_ip = decoder->ip;
1095 if (decoder->packet.count == 0) {
1096 decoder->state.to_ip = 0;
1098 decoder->state.to_ip = decoder->last_ip;
1099 decoder->ip = decoder->last_ip;
1105 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1106 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1108 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1112 return intel_pt_bug(decoder);
1115 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1117 struct intel_pt_insn intel_pt_insn;
1121 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1122 if (err == INTEL_PT_RETURN)
1127 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1128 if (!decoder->return_compression) {
1129 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1131 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1134 if (!decoder->ret_addr) {
1135 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1137 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1140 if (!(decoder->tnt.payload & BIT63)) {
1141 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1143 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1146 decoder->tnt.count -= 1;
1147 if (!decoder->tnt.count)
1148 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1149 decoder->tnt.payload <<= 1;
1150 decoder->state.from_ip = decoder->ip;
1151 decoder->ip = decoder->ret_addr;
1152 decoder->state.to_ip = decoder->ip;
1156 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1157 /* Handle deferred TIPs */
1158 err = intel_pt_get_next_packet(decoder);
1161 if (decoder->packet.type != INTEL_PT_TIP ||
1162 decoder->packet.count == 0) {
1163 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1165 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1166 decoder->pkt_step = 0;
1169 intel_pt_set_last_ip(decoder);
1170 decoder->state.from_ip = decoder->ip;
1171 decoder->state.to_ip = decoder->last_ip;
1172 decoder->ip = decoder->last_ip;
1176 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1177 decoder->tnt.count -= 1;
1178 if (!decoder->tnt.count)
1179 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1180 if (decoder->tnt.payload & BIT63) {
1181 decoder->tnt.payload <<= 1;
1182 decoder->state.from_ip = decoder->ip;
1183 decoder->ip += intel_pt_insn.length +
1185 decoder->state.to_ip = decoder->ip;
1188 /* Instruction sample for a non-taken branch */
1189 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1190 decoder->tnt.payload <<= 1;
1191 decoder->state.type = INTEL_PT_INSTRUCTION;
1192 decoder->state.from_ip = decoder->ip;
1193 decoder->state.to_ip = 0;
1194 decoder->ip += intel_pt_insn.length;
1197 decoder->ip += intel_pt_insn.length;
1198 if (!decoder->tnt.count)
1200 decoder->tnt.payload <<= 1;
1204 return intel_pt_bug(decoder);
1208 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1210 unsigned int fup_tx_flags;
1213 fup_tx_flags = decoder->packet.payload &
1214 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1215 err = intel_pt_get_next_packet(decoder);
1218 if (decoder->packet.type == INTEL_PT_FUP) {
1219 decoder->fup_tx_flags = fup_tx_flags;
1220 decoder->set_fup_tx_flags = true;
1221 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1224 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1226 intel_pt_update_in_tx(decoder);
1231 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1235 decoder->have_tma = false;
1237 if (decoder->ref_timestamp) {
1238 timestamp = decoder->packet.payload |
1239 (decoder->ref_timestamp & (0xffULL << 56));
1240 if (timestamp < decoder->ref_timestamp) {
1241 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1242 timestamp += (1ULL << 56);
1244 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1245 timestamp -= (1ULL << 56);
1247 decoder->tsc_timestamp = timestamp;
1248 decoder->timestamp = timestamp;
1249 decoder->ref_timestamp = 0;
1250 decoder->timestamp_insn_cnt = 0;
1251 } else if (decoder->timestamp) {
1252 timestamp = decoder->packet.payload |
1253 (decoder->timestamp & (0xffULL << 56));
1254 decoder->tsc_timestamp = timestamp;
1255 if (timestamp < decoder->timestamp &&
1256 decoder->timestamp - timestamp < decoder->tsc_slip) {
1257 intel_pt_log_to("Suppressing backwards timestamp",
1259 timestamp = decoder->timestamp;
1261 if (timestamp < decoder->timestamp) {
1262 intel_pt_log_to("Wraparound timestamp", timestamp);
1263 timestamp += (1ULL << 56);
1264 decoder->tsc_timestamp = timestamp;
1266 decoder->timestamp = timestamp;
1267 decoder->timestamp_insn_cnt = 0;
1270 if (decoder->last_packet_type == INTEL_PT_CYC) {
1271 decoder->cyc_ref_timestamp = decoder->timestamp;
1272 decoder->cycle_cnt = 0;
1273 decoder->have_calc_cyc_to_tsc = false;
1274 intel_pt_calc_cyc_to_tsc(decoder, false);
1277 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1280 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1282 intel_pt_log("ERROR: Buffer overflow\n");
1283 intel_pt_clear_tx_flags(decoder);
1285 decoder->timestamp_insn_cnt = 0;
1286 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1287 decoder->overflow = true;
1291 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1293 uint32_t ctc = decoder->packet.payload;
1294 uint32_t fc = decoder->packet.count;
1295 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1297 if (!decoder->tsc_ctc_ratio_d)
1300 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1301 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1302 if (decoder->tsc_ctc_mult) {
1303 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1305 decoder->ctc_timestamp -= multdiv(ctc_rem,
1306 decoder->tsc_ctc_ratio_n,
1307 decoder->tsc_ctc_ratio_d);
1309 decoder->ctc_delta = 0;
1310 decoder->have_tma = true;
1311 decoder->fixup_last_mtc = true;
1312 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1313 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1316 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1319 uint32_t mtc, mtc_delta;
1321 if (!decoder->have_tma)
1324 mtc = decoder->packet.payload;
1326 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1327 decoder->fixup_last_mtc = false;
1328 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1329 &decoder->last_mtc);
1332 if (mtc > decoder->last_mtc)
1333 mtc_delta = mtc - decoder->last_mtc;
1335 mtc_delta = mtc + 256 - decoder->last_mtc;
1337 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1339 if (decoder->tsc_ctc_mult) {
1340 timestamp = decoder->ctc_timestamp +
1341 decoder->ctc_delta * decoder->tsc_ctc_mult;
1343 timestamp = decoder->ctc_timestamp +
1344 multdiv(decoder->ctc_delta,
1345 decoder->tsc_ctc_ratio_n,
1346 decoder->tsc_ctc_ratio_d);
1349 if (timestamp < decoder->timestamp)
1350 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1351 timestamp, decoder->timestamp);
1353 decoder->timestamp = timestamp;
1355 decoder->timestamp_insn_cnt = 0;
1356 decoder->last_mtc = mtc;
1358 if (decoder->last_packet_type == INTEL_PT_CYC) {
1359 decoder->cyc_ref_timestamp = decoder->timestamp;
1360 decoder->cycle_cnt = 0;
1361 decoder->have_calc_cyc_to_tsc = false;
1362 intel_pt_calc_cyc_to_tsc(decoder, true);
1366 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1368 unsigned int cbr = decoder->packet.payload;
1370 if (decoder->cbr == cbr)
1374 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1377 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1379 uint64_t timestamp = decoder->cyc_ref_timestamp;
1381 decoder->have_cyc = true;
1383 decoder->cycle_cnt += decoder->packet.payload;
1385 if (!decoder->cyc_ref_timestamp)
1388 if (decoder->have_calc_cyc_to_tsc)
1389 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1390 else if (decoder->cbr)
1391 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1395 if (timestamp < decoder->timestamp)
1396 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1397 timestamp, decoder->timestamp);
1399 decoder->timestamp = timestamp;
1401 decoder->timestamp_insn_cnt = 0;
1404 /* Walk PSB+ packets when already in sync. */
1405 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1410 err = intel_pt_get_next_packet(decoder);
1414 switch (decoder->packet.type) {
1415 case INTEL_PT_PSBEND:
1418 case INTEL_PT_TIP_PGD:
1419 case INTEL_PT_TIP_PGE:
1422 case INTEL_PT_TRACESTOP:
1425 decoder->have_tma = false;
1426 intel_pt_log("ERROR: Unexpected packet\n");
1430 return intel_pt_overflow(decoder);
1433 intel_pt_calc_tsc_timestamp(decoder);
1437 intel_pt_calc_tma(decoder);
1441 intel_pt_calc_cbr(decoder);
1444 case INTEL_PT_MODE_EXEC:
1445 decoder->exec_mode = decoder->packet.payload;
1449 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1453 decoder->pge = true;
1454 if (decoder->packet.count)
1455 intel_pt_set_last_ip(decoder);
1458 case INTEL_PT_MODE_TSX:
1459 intel_pt_update_in_tx(decoder);
1463 intel_pt_calc_mtc_timestamp(decoder);
1464 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1465 decoder->state.type |= INTEL_PT_INSTRUCTION;
1478 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1482 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1483 decoder->tx_flags = 0;
1484 decoder->state.flags &= ~INTEL_PT_IN_TX;
1485 decoder->state.flags |= INTEL_PT_ABORT_TX;
1487 decoder->state.flags |= INTEL_PT_ASYNC;
1491 err = intel_pt_get_next_packet(decoder);
1495 switch (decoder->packet.type) {
1498 case INTEL_PT_TRACESTOP:
1502 case INTEL_PT_MODE_TSX:
1504 case INTEL_PT_PSBEND:
1505 intel_pt_log("ERROR: Missing TIP after FUP\n");
1506 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1507 decoder->pkt_step = 0;
1511 intel_pt_calc_cbr(decoder);
1515 return intel_pt_overflow(decoder);
1517 case INTEL_PT_TIP_PGD:
1518 decoder->state.from_ip = decoder->ip;
1519 decoder->state.to_ip = 0;
1520 if (decoder->packet.count != 0) {
1521 intel_pt_set_ip(decoder);
1522 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1525 decoder->pge = false;
1526 decoder->continuous_period = false;
1529 case INTEL_PT_TIP_PGE:
1530 decoder->pge = true;
1531 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1533 decoder->state.from_ip = 0;
1534 if (decoder->packet.count == 0) {
1535 decoder->state.to_ip = 0;
1537 intel_pt_set_ip(decoder);
1538 decoder->state.to_ip = decoder->ip;
1543 decoder->state.from_ip = decoder->ip;
1544 if (decoder->packet.count == 0) {
1545 decoder->state.to_ip = 0;
1547 intel_pt_set_ip(decoder);
1548 decoder->state.to_ip = decoder->ip;
1553 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1557 intel_pt_calc_mtc_timestamp(decoder);
1558 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1559 decoder->state.type |= INTEL_PT_INSTRUCTION;
1563 intel_pt_calc_cyc_timestamp(decoder);
1566 case INTEL_PT_MODE_EXEC:
1567 decoder->exec_mode = decoder->packet.payload;
1576 return intel_pt_bug(decoder);
1581 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1583 bool no_tip = false;
1587 err = intel_pt_get_next_packet(decoder);
1591 switch (decoder->packet.type) {
1593 if (!decoder->packet.count)
1595 decoder->tnt = decoder->packet;
1596 decoder->pkt_state = INTEL_PT_STATE_TNT;
1597 err = intel_pt_walk_tnt(decoder);
1602 case INTEL_PT_TIP_PGD:
1603 if (decoder->packet.count != 0)
1604 intel_pt_set_last_ip(decoder);
1605 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1606 return intel_pt_walk_tip(decoder);
1608 case INTEL_PT_TIP_PGE: {
1609 decoder->pge = true;
1610 if (decoder->packet.count == 0) {
1611 intel_pt_log_at("Skipping zero TIP.PGE",
1615 intel_pt_set_ip(decoder);
1616 decoder->state.from_ip = 0;
1617 decoder->state.to_ip = decoder->ip;
1622 return intel_pt_overflow(decoder);
1625 if (decoder->packet.count != 0)
1626 intel_pt_set_last_ip(decoder);
1627 decoder->pkt_state = INTEL_PT_STATE_TIP;
1628 return intel_pt_walk_tip(decoder);
1631 if (decoder->packet.count == 0) {
1632 intel_pt_log_at("Skipping zero FUP",
1637 intel_pt_set_last_ip(decoder);
1638 err = intel_pt_walk_fup(decoder);
1639 if (err != -EAGAIN) {
1643 decoder->pkt_state =
1644 INTEL_PT_STATE_FUP_NO_TIP;
1646 decoder->pkt_state = INTEL_PT_STATE_FUP;
1653 return intel_pt_walk_fup_tip(decoder);
1655 case INTEL_PT_TRACESTOP:
1656 decoder->pge = false;
1657 decoder->continuous_period = false;
1658 intel_pt_clear_tx_flags(decoder);
1659 decoder->have_tma = false;
1663 decoder->last_ip = 0;
1664 decoder->have_last_ip = true;
1665 intel_pt_clear_stack(&decoder->stack);
1666 err = intel_pt_walk_psbend(decoder);
1674 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1678 intel_pt_calc_mtc_timestamp(decoder);
1679 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1682 * Ensure that there has been an instruction since the
1685 if (!decoder->mtc_insn)
1687 decoder->mtc_insn = false;
1688 /* Ensure that there is a timestamp */
1689 if (!decoder->timestamp)
1691 decoder->state.type = INTEL_PT_INSTRUCTION;
1692 decoder->state.from_ip = decoder->ip;
1693 decoder->state.to_ip = 0;
1694 decoder->mtc_insn = false;
1698 intel_pt_calc_tsc_timestamp(decoder);
1702 intel_pt_calc_tma(decoder);
1706 intel_pt_calc_cyc_timestamp(decoder);
1710 intel_pt_calc_cbr(decoder);
1713 case INTEL_PT_MODE_EXEC:
1714 decoder->exec_mode = decoder->packet.payload;
1717 case INTEL_PT_MODE_TSX:
1718 /* MODE_TSX need not be followed by FUP */
1719 if (!decoder->pge) {
1720 intel_pt_update_in_tx(decoder);
1723 err = intel_pt_mode_tsx(decoder, &no_tip);
1728 case INTEL_PT_BAD: /* Does not happen */
1729 return intel_pt_bug(decoder);
1731 case INTEL_PT_PSBEND:
1738 return intel_pt_bug(decoder);
1743 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1745 return decoder->packet.count &&
1746 (decoder->have_last_ip || decoder->packet.count == 3 ||
1747 decoder->packet.count == 6);
1750 /* Walk PSB+ packets to get in sync. */
1751 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1756 err = intel_pt_get_next_packet(decoder);
1760 switch (decoder->packet.type) {
1761 case INTEL_PT_TIP_PGD:
1762 decoder->continuous_period = false;
1764 case INTEL_PT_TIP_PGE:
1766 intel_pt_log("ERROR: Unexpected packet\n");
1770 decoder->pge = true;
1771 if (intel_pt_have_ip(decoder)) {
1772 uint64_t current_ip = decoder->ip;
1774 intel_pt_set_ip(decoder);
1776 intel_pt_log_to("Setting IP",
1782 intel_pt_calc_mtc_timestamp(decoder);
1786 intel_pt_calc_tsc_timestamp(decoder);
1790 intel_pt_calc_tma(decoder);
1794 intel_pt_calc_cyc_timestamp(decoder);
1798 intel_pt_calc_cbr(decoder);
1802 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1805 case INTEL_PT_MODE_EXEC:
1806 decoder->exec_mode = decoder->packet.payload;
1809 case INTEL_PT_MODE_TSX:
1810 intel_pt_update_in_tx(decoder);
1813 case INTEL_PT_TRACESTOP:
1814 decoder->pge = false;
1815 decoder->continuous_period = false;
1816 intel_pt_clear_tx_flags(decoder);
1820 decoder->have_tma = false;
1821 intel_pt_log("ERROR: Unexpected packet\n");
1823 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1825 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1828 case INTEL_PT_BAD: /* Does not happen */
1829 return intel_pt_bug(decoder);
1832 return intel_pt_overflow(decoder);
1834 case INTEL_PT_PSBEND:
1847 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1852 err = intel_pt_get_next_packet(decoder);
1856 switch (decoder->packet.type) {
1857 case INTEL_PT_TIP_PGD:
1858 decoder->continuous_period = false;
1860 case INTEL_PT_TIP_PGE:
1862 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1863 if (intel_pt_have_ip(decoder))
1864 intel_pt_set_ip(decoder);
1870 if (intel_pt_have_ip(decoder))
1871 intel_pt_set_ip(decoder);
1877 intel_pt_calc_mtc_timestamp(decoder);
1881 intel_pt_calc_tsc_timestamp(decoder);
1885 intel_pt_calc_tma(decoder);
1889 intel_pt_calc_cyc_timestamp(decoder);
1893 intel_pt_calc_cbr(decoder);
1897 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1900 case INTEL_PT_MODE_EXEC:
1901 decoder->exec_mode = decoder->packet.payload;
1904 case INTEL_PT_MODE_TSX:
1905 intel_pt_update_in_tx(decoder);
1909 return intel_pt_overflow(decoder);
1911 case INTEL_PT_BAD: /* Does not happen */
1912 return intel_pt_bug(decoder);
1914 case INTEL_PT_TRACESTOP:
1915 decoder->pge = false;
1916 decoder->continuous_period = false;
1917 intel_pt_clear_tx_flags(decoder);
1918 decoder->have_tma = false;
1922 decoder->last_ip = 0;
1923 decoder->have_last_ip = true;
1924 intel_pt_clear_stack(&decoder->stack);
1925 err = intel_pt_walk_psb(decoder);
1929 /* Do not have a sample */
1930 decoder->state.type = 0;
1936 case INTEL_PT_PSBEND:
1946 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1950 decoder->set_fup_tx_flags = false;
1952 intel_pt_log("Scanning for full IP\n");
1953 err = intel_pt_walk_to_ip(decoder);
1957 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1958 decoder->overflow = false;
1960 decoder->state.from_ip = 0;
1961 decoder->state.to_ip = decoder->ip;
1962 intel_pt_log_to("Setting IP", decoder->ip);
1967 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1969 const unsigned char *end = decoder->buf + decoder->len;
1972 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1973 if (i > decoder->len)
1975 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1981 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1983 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1984 const char *psb = INTEL_PT_PSB_STR;
1986 if (rest_psb > decoder->len ||
1987 memcmp(decoder->buf, psb + part_psb, rest_psb))
1993 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
1998 decoder->pos += decoder->len;
2001 ret = intel_pt_get_next_data(decoder);
2005 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2009 decoder->pos -= part_psb;
2010 decoder->next_buf = decoder->buf + rest_psb;
2011 decoder->next_len = decoder->len - rest_psb;
2012 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2013 decoder->buf = decoder->temp_buf;
2014 decoder->len = INTEL_PT_PSB_LEN;
2019 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2021 unsigned char *next;
2024 intel_pt_log("Scanning for PSB\n");
2026 if (!decoder->len) {
2027 ret = intel_pt_get_next_data(decoder);
2032 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2037 part_psb = intel_pt_part_psb(decoder);
2039 ret = intel_pt_get_split_psb(decoder, part_psb);
2043 decoder->pos += decoder->len;
2049 decoder->pkt_step = next - decoder->buf;
2050 return intel_pt_get_next_packet(decoder);
2054 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2058 decoder->pge = false;
2059 decoder->continuous_period = false;
2060 decoder->have_last_ip = false;
2061 decoder->last_ip = 0;
2063 intel_pt_clear_stack(&decoder->stack);
2065 err = intel_pt_scan_for_psb(decoder);
2069 decoder->have_last_ip = true;
2070 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2072 err = intel_pt_walk_psb(decoder);
2077 decoder->state.type = 0; /* Do not have a sample */
2078 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2080 return intel_pt_sync_ip(decoder);
2086 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2088 uint64_t est = decoder->sample_insn_cnt << 1;
2090 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2093 est *= decoder->max_non_turbo_ratio;
2094 est /= decoder->cbr;
2096 return decoder->sample_timestamp + est;
2099 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2104 decoder->state.type = INTEL_PT_BRANCH;
2105 decoder->state.flags = 0;
2107 switch (decoder->pkt_state) {
2108 case INTEL_PT_STATE_NO_PSB:
2109 err = intel_pt_sync(decoder);
2111 case INTEL_PT_STATE_NO_IP:
2112 decoder->have_last_ip = false;
2113 decoder->last_ip = 0;
2116 case INTEL_PT_STATE_ERR_RESYNC:
2117 err = intel_pt_sync_ip(decoder);
2119 case INTEL_PT_STATE_IN_SYNC:
2120 err = intel_pt_walk_trace(decoder);
2122 case INTEL_PT_STATE_TNT:
2123 err = intel_pt_walk_tnt(decoder);
2125 err = intel_pt_walk_trace(decoder);
2127 case INTEL_PT_STATE_TIP:
2128 case INTEL_PT_STATE_TIP_PGD:
2129 err = intel_pt_walk_tip(decoder);
2131 case INTEL_PT_STATE_FUP:
2132 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2133 err = intel_pt_walk_fup(decoder);
2135 err = intel_pt_walk_fup_tip(decoder);
2137 decoder->pkt_state = INTEL_PT_STATE_FUP;
2139 case INTEL_PT_STATE_FUP_NO_TIP:
2140 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2141 err = intel_pt_walk_fup(decoder);
2143 err = intel_pt_walk_trace(decoder);
2146 err = intel_pt_bug(decoder);
2149 } while (err == -ENOLINK);
2152 decoder->state.err = intel_pt_ext_err(err);
2153 decoder->state.from_ip = decoder->ip;
2154 decoder->sample_timestamp = decoder->timestamp;
2155 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2157 decoder->state.err = 0;
2158 if (intel_pt_sample_time(decoder->pkt_state)) {
2159 decoder->sample_timestamp = decoder->timestamp;
2160 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2164 decoder->state.timestamp = decoder->sample_timestamp;
2165 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2166 decoder->state.cr3 = decoder->cr3;
2167 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2169 return &decoder->state;
2173 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2174 * @buf: pointer to buffer pointer
2175 * @len: size of buffer
2177 * Updates the buffer pointer to point to the start of the next PSB packet if
2178 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2179 * @len is adjusted accordingly.
2181 * Return: %true if a PSB packet is found, %false otherwise.
2183 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2185 unsigned char *next;
2187 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2189 *len -= next - *buf;
2197 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2199 * @buf: pointer to buffer pointer
2200 * @len: size of buffer
2202 * Updates the buffer pointer to point to the start of the following PSB packet
2203 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2204 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2206 * Return: %true if a PSB packet is found, %false otherwise.
2208 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2210 unsigned char *next;
2215 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2217 *len -= next - *buf;
2225 * intel_pt_last_psb - find the last PSB packet in a buffer.
2227 * @len: size of buffer
2229 * This function finds the last PSB in a buffer.
2231 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2233 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2235 const char *n = INTEL_PT_PSB_STR;
2239 if (len < INTEL_PT_PSB_LEN)
2242 k = len - INTEL_PT_PSB_LEN + 1;
2244 p = memrchr(buf, n[0], k);
2247 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2256 * intel_pt_next_tsc - find and return next TSC.
2258 * @len: size of buffer
2259 * @tsc: TSC value returned
2260 * @rem: returns remaining size when TSC is found
2262 * Find a TSC packet in @buf and return the TSC value. This function assumes
2263 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2264 * PSBEND packet is found.
2266 * Return: %true if TSC is found, false otherwise.
2268 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2271 struct intel_pt_pkt packet;
2275 ret = intel_pt_get_packet(buf, len, &packet);
2278 if (packet.type == INTEL_PT_TSC) {
2279 *tsc = packet.payload;
2283 if (packet.type == INTEL_PT_PSBEND)
2292 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2293 * @tsc1: first TSC to compare
2294 * @tsc2: second TSC to compare
2296 * This function compares 7-byte TSC values allowing for the possibility that
2297 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2298 * around so for that purpose this function assumes the absolute difference is
2299 * less than half the maximum difference.
2301 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2304 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2306 const uint64_t halfway = (1ULL << 55);
2312 if (tsc2 - tsc1 < halfway)
2317 if (tsc1 - tsc2 < halfway)
2325 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2327 * @buf_a: first buffer
2328 * @len_a: size of first buffer
2329 * @buf_b: second buffer
2330 * @len_b: size of second buffer
2331 * @consecutive: returns true if there is data in buf_b that is consecutive
2334 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2335 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2336 * walk forward in @buf_b until a later TSC is found. A precondition is that
2337 * @buf_a and @buf_b are positioned at a PSB.
2339 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2340 * @buf_b + @len_b if there is no non-overlapped data.
2342 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2344 unsigned char *buf_b,
2345 size_t len_b, bool *consecutive)
2347 uint64_t tsc_a, tsc_b;
2349 size_t len, rem_a, rem_b;
2351 p = intel_pt_last_psb(buf_a, len_a);
2353 return buf_b; /* No PSB in buf_a => no overlap */
2355 len = len_a - (p - buf_a);
2356 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2357 /* The last PSB+ in buf_a is incomplete, so go back one more */
2359 p = intel_pt_last_psb(buf_a, len_a);
2361 return buf_b; /* No full PSB+ => assume no overlap */
2362 len = len_a - (p - buf_a);
2363 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2364 return buf_b; /* No TSC in buf_a => assume no overlap */
2368 /* Ignore PSB+ with no TSC */
2369 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2370 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2372 /* Same TSC, so buffers are consecutive */
2373 if (!cmp && rem_b >= rem_a) {
2374 *consecutive = true;
2375 return buf_b + len_b - (rem_b - rem_a);
2378 return buf_b; /* tsc_a < tsc_b => no overlap */
2381 if (!intel_pt_step_psb(&buf_b, &len_b))
2382 return buf_b + len_b; /* No PSB in buf_b => no data */
2387 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2388 * @buf_a: first buffer
2389 * @len_a: size of first buffer
2390 * @buf_b: second buffer
2391 * @len_b: size of second buffer
2392 * @have_tsc: can use TSC packets to detect overlap
2393 * @consecutive: returns true if there is data in buf_b that is consecutive
2396 * When trace samples or snapshots are recorded there is the possibility that
2397 * the data overlaps. Note that, for the purposes of decoding, data is only
2398 * useful if it begins with a PSB packet.
2400 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2401 * @buf_b + @len_b if there is no non-overlapped data.
2403 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2404 unsigned char *buf_b, size_t len_b,
2405 bool have_tsc, bool *consecutive)
2407 unsigned char *found;
2409 /* Buffer 'b' must start at PSB so throw away everything before that */
2410 if (!intel_pt_next_psb(&buf_b, &len_b))
2411 return buf_b + len_b; /* No PSB */
2413 if (!intel_pt_next_psb(&buf_a, &len_a))
2414 return buf_b; /* No overlap */
2417 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2424 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2425 * we can ignore the first part of buffer 'a'.
2427 while (len_b < len_a) {
2428 if (!intel_pt_step_psb(&buf_a, &len_a))
2429 return buf_b; /* No overlap */
2432 /* Now len_b >= len_a */
2434 /* Potential overlap so check the bytes */
2435 found = memmem(buf_a, len_a, buf_b, len_a);
2437 *consecutive = true;
2438 return buf_b + len_a;
2441 /* Try again at next PSB in buffer 'a' */
2442 if (!intel_pt_step_psb(&buf_a, &len_a))
2443 return buf_b; /* No overlap */