2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
29 #include "intel-pt-insn-decoder.h"
30 #include "intel-pt-pkt-decoder.h"
31 #include "intel-pt-decoder.h"
32 #include "intel-pt-log.h"
34 #define INTEL_PT_BLK_SIZE 1024
36 #define BIT63 (((uint64_t)1 << 63))
38 #define INTEL_PT_RETURN 1
40 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
41 #define INTEL_PT_MAX_LOOPS 10000
44 struct intel_pt_blk *prev;
45 uint64_t ip[INTEL_PT_BLK_SIZE];
48 struct intel_pt_stack {
49 struct intel_pt_blk *blk;
50 struct intel_pt_blk *spare;
54 enum intel_pt_pkt_state {
55 INTEL_PT_STATE_NO_PSB,
57 INTEL_PT_STATE_ERR_RESYNC,
58 INTEL_PT_STATE_IN_SYNC,
61 INTEL_PT_STATE_TIP_PGD,
63 INTEL_PT_STATE_FUP_NO_TIP,
66 #ifdef INTEL_PT_STRICT
67 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
68 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
69 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
70 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
72 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
73 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
74 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
75 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
78 struct intel_pt_decoder {
79 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
80 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
81 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
82 uint64_t max_insn_cnt, void *data);
83 bool (*pgd_ip)(uint64_t ip, void *data);
85 struct intel_pt_state state;
86 const unsigned char *buf;
88 bool return_compression;
98 uint64_t tsc_timestamp;
99 uint64_t ref_timestamp;
101 uint64_t ctc_timestamp;
104 uint64_t cyc_ref_timestamp;
106 uint32_t tsc_ctc_ratio_n;
107 uint32_t tsc_ctc_ratio_d;
108 uint32_t tsc_ctc_mult;
110 uint32_t ctc_rem_mask;
112 struct intel_pt_stack stack;
113 enum intel_pt_pkt_state pkt_state;
114 struct intel_pt_pkt packet;
115 struct intel_pt_pkt tnt;
118 int last_packet_type;
120 unsigned int max_non_turbo_ratio;
121 double max_non_turbo_ratio_fp;
122 double cbr_cyc_to_tsc;
123 double calc_cyc_to_tsc;
124 bool have_calc_cyc_to_tsc;
126 unsigned int insn_bytes;
128 enum intel_pt_period_type period_type;
129 uint64_t tot_insn_cnt;
130 uint64_t period_insn_cnt;
131 uint64_t period_mask;
132 uint64_t period_ticks;
133 uint64_t last_masked_timestamp;
134 bool continuous_period;
136 bool set_fup_tx_flags;
137 unsigned int fup_tx_flags;
138 unsigned int tx_flags;
139 uint64_t timestamp_insn_cnt;
144 const unsigned char *next_buf;
146 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
149 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
153 for (i = 0; x != 1; i++)
159 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
161 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
164 period = intel_pt_lower_power_of_2(decoder->period);
165 decoder->period_mask = ~(period - 1);
166 decoder->period_ticks = period;
170 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
174 return (t / d) * n + ((t % d) * n) / d;
177 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
179 struct intel_pt_decoder *decoder;
181 if (!params->get_trace || !params->walk_insn)
184 decoder = zalloc(sizeof(struct intel_pt_decoder));
188 decoder->get_trace = params->get_trace;
189 decoder->walk_insn = params->walk_insn;
190 decoder->pgd_ip = params->pgd_ip;
191 decoder->data = params->data;
192 decoder->return_compression = params->return_compression;
194 decoder->period = params->period;
195 decoder->period_type = params->period_type;
197 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
198 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
200 intel_pt_setup_period(decoder);
202 decoder->mtc_shift = params->mtc_period;
203 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
205 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
206 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
208 if (!decoder->tsc_ctc_ratio_n)
209 decoder->tsc_ctc_ratio_d = 0;
211 if (decoder->tsc_ctc_ratio_d) {
212 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
213 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
214 decoder->tsc_ctc_ratio_d;
217 * Allow for timestamps appearing to backwards because a TSC
218 * packet has slipped past a MTC packet, so allow 2 MTC ticks
221 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
222 decoder->tsc_ctc_ratio_n,
223 decoder->tsc_ctc_ratio_d);
225 /* ... or 0x100 paranoia */
226 if (decoder->tsc_slip < 0x100)
227 decoder->tsc_slip = 0x100;
229 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
230 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
231 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
232 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
233 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
238 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
240 struct intel_pt_blk *blk = stack->blk;
242 stack->blk = blk->prev;
249 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
254 intel_pt_pop_blk(stack);
257 stack->pos = INTEL_PT_BLK_SIZE;
259 return stack->blk->ip[--stack->pos];
262 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
264 struct intel_pt_blk *blk;
270 blk = malloc(sizeof(struct intel_pt_blk));
275 blk->prev = stack->blk;
281 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
285 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
286 err = intel_pt_alloc_blk(stack);
291 stack->blk->ip[stack->pos++] = ip;
295 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
298 intel_pt_pop_blk(stack);
302 static void intel_pt_free_stack(struct intel_pt_stack *stack)
304 intel_pt_clear_stack(stack);
306 zfree(&stack->spare);
309 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
311 intel_pt_free_stack(&decoder->stack);
315 static int intel_pt_ext_err(int code)
319 return INTEL_PT_ERR_NOMEM;
321 return INTEL_PT_ERR_INTERN;
323 return INTEL_PT_ERR_BADPKT;
325 return INTEL_PT_ERR_NODATA;
327 return INTEL_PT_ERR_NOINSN;
329 return INTEL_PT_ERR_MISMAT;
331 return INTEL_PT_ERR_OVR;
333 return INTEL_PT_ERR_LOST;
335 return INTEL_PT_ERR_NELOOP;
337 return INTEL_PT_ERR_UNK;
341 static const char *intel_pt_err_msgs[] = {
342 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
343 [INTEL_PT_ERR_INTERN] = "Internal error",
344 [INTEL_PT_ERR_BADPKT] = "Bad packet",
345 [INTEL_PT_ERR_NODATA] = "No more data",
346 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
347 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
348 [INTEL_PT_ERR_OVR] = "Overflow packet",
349 [INTEL_PT_ERR_LOST] = "Lost trace data",
350 [INTEL_PT_ERR_UNK] = "Unknown error!",
351 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
354 int intel_pt__strerror(int code, char *buf, size_t buflen)
356 if (code < 1 || code >= INTEL_PT_ERR_MAX)
357 code = INTEL_PT_ERR_UNK;
358 strlcpy(buf, intel_pt_err_msgs[code], buflen);
362 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
367 switch (packet->count) {
369 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
373 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
377 ip = packet->payload;
378 /* Sign-extend 6-byte ip */
379 if (ip & (uint64_t)0x800000000000ULL)
380 ip |= (uint64_t)0xffff000000000000ULL;
383 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
387 ip = packet->payload;
396 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
398 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
401 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
403 intel_pt_set_last_ip(decoder);
404 decoder->ip = decoder->last_ip;
407 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
409 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
413 static int intel_pt_bug(struct intel_pt_decoder *decoder)
415 intel_pt_log("ERROR: Internal error\n");
416 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
420 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
422 decoder->tx_flags = 0;
425 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
427 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
430 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
432 intel_pt_clear_tx_flags(decoder);
433 decoder->have_tma = false;
434 decoder->pkt_len = 1;
435 decoder->pkt_step = 1;
436 intel_pt_decoder_log_packet(decoder);
437 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
438 intel_pt_log("ERROR: Bad packet\n");
439 decoder->pkt_state = INTEL_PT_STATE_ERR1;
444 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
446 struct intel_pt_buffer buffer = { .buf = 0, };
449 decoder->pkt_step = 0;
451 intel_pt_log("Getting more data\n");
452 ret = decoder->get_trace(&buffer, decoder->data);
455 decoder->buf = buffer.buf;
456 decoder->len = buffer.len;
458 intel_pt_log("No more data\n");
461 if (!buffer.consecutive) {
463 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
464 decoder->ref_timestamp = buffer.ref_timestamp;
465 decoder->timestamp = 0;
466 decoder->have_tma = false;
467 decoder->state.trace_nr = buffer.trace_nr;
468 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
469 decoder->ref_timestamp);
476 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
478 if (!decoder->next_buf)
479 return intel_pt_get_data(decoder);
481 decoder->buf = decoder->next_buf;
482 decoder->len = decoder->next_len;
483 decoder->next_buf = 0;
484 decoder->next_len = 0;
488 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
490 unsigned char *buf = decoder->temp_buf;
491 size_t old_len, len, n;
494 old_len = decoder->len;
496 memcpy(buf, decoder->buf, len);
498 ret = intel_pt_get_data(decoder);
500 decoder->pos += old_len;
501 return ret < 0 ? ret : -EINVAL;
504 n = INTEL_PT_PKT_MAX_SZ - len;
505 if (n > decoder->len)
507 memcpy(buf + len, decoder->buf, n);
510 ret = intel_pt_get_packet(buf, len, &decoder->packet);
511 if (ret < (int)old_len) {
512 decoder->next_buf = decoder->buf;
513 decoder->next_len = decoder->len;
515 decoder->len = old_len;
516 return intel_pt_bad_packet(decoder);
519 decoder->next_buf = decoder->buf + (ret - old_len);
520 decoder->next_len = decoder->len - (ret - old_len);
528 struct intel_pt_pkt_info {
529 struct intel_pt_decoder *decoder;
530 struct intel_pt_pkt packet;
533 int last_packet_type;
537 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
539 /* Lookahead packets in current buffer */
540 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
541 intel_pt_pkt_cb_t cb, void *data)
543 struct intel_pt_pkt_info pkt_info;
544 const unsigned char *buf = decoder->buf;
545 size_t len = decoder->len;
548 pkt_info.decoder = decoder;
549 pkt_info.pos = decoder->pos;
550 pkt_info.pkt_len = decoder->pkt_step;
551 pkt_info.last_packet_type = decoder->last_packet_type;
552 pkt_info.data = data;
556 pkt_info.pos += pkt_info.pkt_len;
557 buf += pkt_info.pkt_len;
558 len -= pkt_info.pkt_len;
561 return INTEL_PT_NEED_MORE_BYTES;
563 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
565 return INTEL_PT_NEED_MORE_BYTES;
569 pkt_info.pkt_len = ret;
570 } while (pkt_info.packet.type == INTEL_PT_PAD);
576 pkt_info.last_packet_type = pkt_info.packet.type;
580 struct intel_pt_calc_cyc_to_tsc_info {
584 uint64_t ctc_timestamp;
586 uint64_t tsc_timestamp;
590 double cbr_cyc_to_tsc;
593 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
595 struct intel_pt_decoder *decoder = pkt_info->decoder;
596 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
600 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
602 switch (pkt_info->packet.type) {
604 case INTEL_PT_TIP_PGE:
609 case INTEL_PT_MODE_EXEC:
610 case INTEL_PT_MODE_TSX:
611 case INTEL_PT_PSBEND:
621 mtc = pkt_info->packet.payload;
622 if (mtc > data->last_mtc)
623 mtc_delta = mtc - data->last_mtc;
625 mtc_delta = mtc + 256 - data->last_mtc;
626 data->ctc_delta += mtc_delta << decoder->mtc_shift;
627 data->last_mtc = mtc;
629 if (decoder->tsc_ctc_mult) {
630 timestamp = data->ctc_timestamp +
631 data->ctc_delta * decoder->tsc_ctc_mult;
633 timestamp = data->ctc_timestamp +
634 multdiv(data->ctc_delta,
635 decoder->tsc_ctc_ratio_n,
636 decoder->tsc_ctc_ratio_d);
639 if (timestamp < data->timestamp)
642 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
643 data->timestamp = timestamp;
650 timestamp = pkt_info->packet.payload |
651 (data->timestamp & (0xffULL << 56));
652 if (data->from_mtc && timestamp < data->timestamp &&
653 data->timestamp - timestamp < decoder->tsc_slip)
655 if (timestamp < data->timestamp)
656 timestamp += (1ULL << 56);
657 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
660 data->tsc_timestamp = timestamp;
661 data->timestamp = timestamp;
670 if (!decoder->tsc_ctc_ratio_d)
673 ctc = pkt_info->packet.payload;
674 fc = pkt_info->packet.count;
675 ctc_rem = ctc & decoder->ctc_rem_mask;
677 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
679 data->ctc_timestamp = data->tsc_timestamp - fc;
680 if (decoder->tsc_ctc_mult) {
681 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
683 data->ctc_timestamp -=
684 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
685 decoder->tsc_ctc_ratio_d);
689 data->have_tma = true;
694 data->cycle_cnt += pkt_info->packet.payload;
698 cbr = pkt_info->packet.payload;
699 if (data->cbr && data->cbr != cbr)
702 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
705 case INTEL_PT_TIP_PGD:
706 case INTEL_PT_TRACESTOP:
708 case INTEL_PT_BAD: /* Does not happen */
713 if (!data->cbr && decoder->cbr) {
714 data->cbr = decoder->cbr;
715 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
718 if (!data->cycle_cnt)
721 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
723 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
724 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
725 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
726 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
730 decoder->calc_cyc_to_tsc = cyc_to_tsc;
731 decoder->have_calc_cyc_to_tsc = true;
734 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
735 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
737 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
738 cyc_to_tsc, pkt_info->pos);
744 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
747 struct intel_pt_calc_cyc_to_tsc_info data = {
750 .last_mtc = decoder->last_mtc,
751 .ctc_timestamp = decoder->ctc_timestamp,
752 .ctc_delta = decoder->ctc_delta,
753 .tsc_timestamp = decoder->tsc_timestamp,
754 .timestamp = decoder->timestamp,
755 .have_tma = decoder->have_tma,
756 .from_mtc = from_mtc,
760 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
763 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
767 decoder->last_packet_type = decoder->packet.type;
770 decoder->pos += decoder->pkt_step;
771 decoder->buf += decoder->pkt_step;
772 decoder->len -= decoder->pkt_step;
775 ret = intel_pt_get_next_data(decoder);
780 ret = intel_pt_get_packet(decoder->buf, decoder->len,
782 if (ret == INTEL_PT_NEED_MORE_BYTES &&
783 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
784 ret = intel_pt_get_split_packet(decoder);
789 return intel_pt_bad_packet(decoder);
791 decoder->pkt_len = ret;
792 decoder->pkt_step = ret;
793 intel_pt_decoder_log_packet(decoder);
794 } while (decoder->packet.type == INTEL_PT_PAD);
799 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
801 uint64_t timestamp, masked_timestamp;
803 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
804 masked_timestamp = timestamp & decoder->period_mask;
805 if (decoder->continuous_period) {
806 if (masked_timestamp != decoder->last_masked_timestamp)
810 masked_timestamp = timestamp & decoder->period_mask;
811 if (masked_timestamp != decoder->last_masked_timestamp) {
812 decoder->last_masked_timestamp = masked_timestamp;
813 decoder->continuous_period = true;
816 return decoder->period_ticks - (timestamp - masked_timestamp);
819 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
821 switch (decoder->period_type) {
822 case INTEL_PT_PERIOD_INSTRUCTIONS:
823 return decoder->period - decoder->period_insn_cnt;
824 case INTEL_PT_PERIOD_TICKS:
825 return intel_pt_next_period(decoder);
826 case INTEL_PT_PERIOD_NONE:
827 case INTEL_PT_PERIOD_MTC:
833 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
835 uint64_t timestamp, masked_timestamp;
837 switch (decoder->period_type) {
838 case INTEL_PT_PERIOD_INSTRUCTIONS:
839 decoder->period_insn_cnt = 0;
841 case INTEL_PT_PERIOD_TICKS:
842 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
843 masked_timestamp = timestamp & decoder->period_mask;
844 decoder->last_masked_timestamp = masked_timestamp;
846 case INTEL_PT_PERIOD_NONE:
847 case INTEL_PT_PERIOD_MTC:
852 decoder->state.type |= INTEL_PT_INSTRUCTION;
855 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
856 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
858 uint64_t max_insn_cnt, insn_cnt = 0;
861 if (!decoder->mtc_insn)
862 decoder->mtc_insn = true;
864 max_insn_cnt = intel_pt_next_sample(decoder);
866 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
867 max_insn_cnt, decoder->data);
869 decoder->tot_insn_cnt += insn_cnt;
870 decoder->timestamp_insn_cnt += insn_cnt;
871 decoder->period_insn_cnt += insn_cnt;
874 decoder->no_progress = 0;
875 decoder->pkt_state = INTEL_PT_STATE_ERR2;
876 intel_pt_log_at("ERROR: Failed to get instruction",
883 if (ip && decoder->ip == ip) {
888 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
889 intel_pt_sample_insn(decoder);
891 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
892 decoder->state.type = INTEL_PT_INSTRUCTION;
893 decoder->state.from_ip = decoder->ip;
894 decoder->state.to_ip = 0;
895 decoder->ip += intel_pt_insn->length;
896 err = INTEL_PT_RETURN;
900 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
901 /* Zero-length calls are excluded */
902 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
903 intel_pt_insn->rel) {
904 err = intel_pt_push(&decoder->stack, decoder->ip +
905 intel_pt_insn->length);
909 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
910 decoder->ret_addr = intel_pt_pop(&decoder->stack);
913 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
914 int cnt = decoder->no_progress++;
916 decoder->state.from_ip = decoder->ip;
917 decoder->ip += intel_pt_insn->length +
919 decoder->state.to_ip = decoder->ip;
920 err = INTEL_PT_RETURN;
923 * Check for being stuck in a loop. This can happen if a
924 * decoder error results in the decoder erroneously setting the
925 * ip to an address that is itself in an infinite loop that
926 * consumes no packets. When that happens, there must be an
927 * unconditional branch.
931 decoder->stuck_ip = decoder->state.to_ip;
932 decoder->stuck_ip_prd = 1;
933 decoder->stuck_ip_cnt = 1;
934 } else if (cnt > INTEL_PT_MAX_LOOPS ||
935 decoder->state.to_ip == decoder->stuck_ip) {
936 intel_pt_log_at("ERROR: Never-ending loop",
937 decoder->state.to_ip);
938 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
941 } else if (!--decoder->stuck_ip_cnt) {
942 decoder->stuck_ip_prd += 1;
943 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
944 decoder->stuck_ip = decoder->state.to_ip;
947 goto out_no_progress;
950 decoder->no_progress = 0;
952 decoder->state.insn_op = intel_pt_insn->op;
953 decoder->state.insn_len = intel_pt_insn->length;
955 if (decoder->tx_flags & INTEL_PT_IN_TX)
956 decoder->state.flags |= INTEL_PT_IN_TX;
961 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
963 struct intel_pt_insn intel_pt_insn;
967 ip = decoder->last_ip;
970 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
971 if (err == INTEL_PT_RETURN)
973 if (err == -EAGAIN) {
974 if (decoder->set_fup_tx_flags) {
975 decoder->set_fup_tx_flags = false;
976 decoder->tx_flags = decoder->fup_tx_flags;
977 decoder->state.type = INTEL_PT_TRANSACTION;
978 decoder->state.from_ip = decoder->ip;
979 decoder->state.to_ip = 0;
980 decoder->state.flags = decoder->fup_tx_flags;
985 decoder->set_fup_tx_flags = false;
989 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
990 intel_pt_log_at("ERROR: Unexpected indirect branch",
992 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
996 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
997 intel_pt_log_at("ERROR: Unexpected conditional branch",
999 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1003 intel_pt_bug(decoder);
1007 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1009 struct intel_pt_insn intel_pt_insn;
1012 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1013 if (err == INTEL_PT_RETURN &&
1015 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1016 (decoder->state.type & INTEL_PT_BRANCH) &&
1017 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1018 /* Unconditional branch leaving filter region */
1019 decoder->no_progress = 0;
1020 decoder->pge = false;
1021 decoder->continuous_period = false;
1022 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1023 decoder->state.to_ip = 0;
1026 if (err == INTEL_PT_RETURN)
1031 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1032 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1033 decoder->pge = false;
1034 decoder->continuous_period = false;
1035 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1036 decoder->state.from_ip = decoder->ip;
1037 decoder->state.to_ip = 0;
1038 if (decoder->packet.count != 0)
1039 decoder->ip = decoder->last_ip;
1041 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1042 decoder->state.from_ip = decoder->ip;
1043 if (decoder->packet.count == 0) {
1044 decoder->state.to_ip = 0;
1046 decoder->state.to_ip = decoder->last_ip;
1047 decoder->ip = decoder->last_ip;
1053 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1054 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1057 if (decoder->pgd_ip &&
1058 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1059 decoder->pgd_ip(to_ip, decoder->data)) {
1060 /* Conditional branch leaving filter region */
1061 decoder->pge = false;
1062 decoder->continuous_period = false;
1063 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1064 decoder->ip = to_ip;
1065 decoder->state.from_ip = decoder->ip;
1066 decoder->state.to_ip = 0;
1069 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1071 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1075 return intel_pt_bug(decoder);
1078 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1080 struct intel_pt_insn intel_pt_insn;
1084 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1085 if (err == INTEL_PT_RETURN)
1090 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1091 if (!decoder->return_compression) {
1092 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1094 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1097 if (!decoder->ret_addr) {
1098 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1100 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1103 if (!(decoder->tnt.payload & BIT63)) {
1104 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1106 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1109 decoder->tnt.count -= 1;
1110 if (!decoder->tnt.count)
1111 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1112 decoder->tnt.payload <<= 1;
1113 decoder->state.from_ip = decoder->ip;
1114 decoder->ip = decoder->ret_addr;
1115 decoder->state.to_ip = decoder->ip;
1119 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1120 /* Handle deferred TIPs */
1121 err = intel_pt_get_next_packet(decoder);
1124 if (decoder->packet.type != INTEL_PT_TIP ||
1125 decoder->packet.count == 0) {
1126 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1128 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1129 decoder->pkt_step = 0;
1132 intel_pt_set_last_ip(decoder);
1133 decoder->state.from_ip = decoder->ip;
1134 decoder->state.to_ip = decoder->last_ip;
1135 decoder->ip = decoder->last_ip;
1139 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1140 decoder->tnt.count -= 1;
1141 if (!decoder->tnt.count)
1142 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1143 if (decoder->tnt.payload & BIT63) {
1144 decoder->tnt.payload <<= 1;
1145 decoder->state.from_ip = decoder->ip;
1146 decoder->ip += intel_pt_insn.length +
1148 decoder->state.to_ip = decoder->ip;
1151 /* Instruction sample for a non-taken branch */
1152 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1153 decoder->tnt.payload <<= 1;
1154 decoder->state.type = INTEL_PT_INSTRUCTION;
1155 decoder->state.from_ip = decoder->ip;
1156 decoder->state.to_ip = 0;
1157 decoder->ip += intel_pt_insn.length;
1160 decoder->ip += intel_pt_insn.length;
1161 if (!decoder->tnt.count)
1163 decoder->tnt.payload <<= 1;
1167 return intel_pt_bug(decoder);
1171 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1173 unsigned int fup_tx_flags;
1176 fup_tx_flags = decoder->packet.payload &
1177 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1178 err = intel_pt_get_next_packet(decoder);
1181 if (decoder->packet.type == INTEL_PT_FUP) {
1182 decoder->fup_tx_flags = fup_tx_flags;
1183 decoder->set_fup_tx_flags = true;
1184 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1187 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1189 intel_pt_update_in_tx(decoder);
1194 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1198 decoder->have_tma = false;
1200 if (decoder->ref_timestamp) {
1201 timestamp = decoder->packet.payload |
1202 (decoder->ref_timestamp & (0xffULL << 56));
1203 if (timestamp < decoder->ref_timestamp) {
1204 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1205 timestamp += (1ULL << 56);
1207 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1208 timestamp -= (1ULL << 56);
1210 decoder->tsc_timestamp = timestamp;
1211 decoder->timestamp = timestamp;
1212 decoder->ref_timestamp = 0;
1213 decoder->timestamp_insn_cnt = 0;
1214 } else if (decoder->timestamp) {
1215 timestamp = decoder->packet.payload |
1216 (decoder->timestamp & (0xffULL << 56));
1217 decoder->tsc_timestamp = timestamp;
1218 if (timestamp < decoder->timestamp &&
1219 decoder->timestamp - timestamp < decoder->tsc_slip) {
1220 intel_pt_log_to("Suppressing backwards timestamp",
1222 timestamp = decoder->timestamp;
1224 if (timestamp < decoder->timestamp) {
1225 intel_pt_log_to("Wraparound timestamp", timestamp);
1226 timestamp += (1ULL << 56);
1227 decoder->tsc_timestamp = timestamp;
1229 decoder->timestamp = timestamp;
1230 decoder->timestamp_insn_cnt = 0;
1233 if (decoder->last_packet_type == INTEL_PT_CYC) {
1234 decoder->cyc_ref_timestamp = decoder->timestamp;
1235 decoder->cycle_cnt = 0;
1236 decoder->have_calc_cyc_to_tsc = false;
1237 intel_pt_calc_cyc_to_tsc(decoder, false);
1240 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1243 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1245 intel_pt_log("ERROR: Buffer overflow\n");
1246 intel_pt_clear_tx_flags(decoder);
1247 decoder->have_tma = false;
1249 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1250 decoder->overflow = true;
1254 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1256 uint32_t ctc = decoder->packet.payload;
1257 uint32_t fc = decoder->packet.count;
1258 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1260 if (!decoder->tsc_ctc_ratio_d)
1263 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1264 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1265 if (decoder->tsc_ctc_mult) {
1266 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1268 decoder->ctc_timestamp -= multdiv(ctc_rem,
1269 decoder->tsc_ctc_ratio_n,
1270 decoder->tsc_ctc_ratio_d);
1272 decoder->ctc_delta = 0;
1273 decoder->have_tma = true;
1274 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1275 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1278 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1281 uint32_t mtc, mtc_delta;
1283 if (!decoder->have_tma)
1286 mtc = decoder->packet.payload;
1288 if (mtc > decoder->last_mtc)
1289 mtc_delta = mtc - decoder->last_mtc;
1291 mtc_delta = mtc + 256 - decoder->last_mtc;
1293 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1295 if (decoder->tsc_ctc_mult) {
1296 timestamp = decoder->ctc_timestamp +
1297 decoder->ctc_delta * decoder->tsc_ctc_mult;
1299 timestamp = decoder->ctc_timestamp +
1300 multdiv(decoder->ctc_delta,
1301 decoder->tsc_ctc_ratio_n,
1302 decoder->tsc_ctc_ratio_d);
1305 if (timestamp < decoder->timestamp)
1306 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1307 timestamp, decoder->timestamp);
1309 decoder->timestamp = timestamp;
1311 decoder->timestamp_insn_cnt = 0;
1312 decoder->last_mtc = mtc;
1314 if (decoder->last_packet_type == INTEL_PT_CYC) {
1315 decoder->cyc_ref_timestamp = decoder->timestamp;
1316 decoder->cycle_cnt = 0;
1317 decoder->have_calc_cyc_to_tsc = false;
1318 intel_pt_calc_cyc_to_tsc(decoder, true);
1322 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1324 unsigned int cbr = decoder->packet.payload;
1326 if (decoder->cbr == cbr)
1330 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1333 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1335 uint64_t timestamp = decoder->cyc_ref_timestamp;
1337 decoder->have_cyc = true;
1339 decoder->cycle_cnt += decoder->packet.payload;
1341 if (!decoder->cyc_ref_timestamp)
1344 if (decoder->have_calc_cyc_to_tsc)
1345 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1346 else if (decoder->cbr)
1347 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1351 if (timestamp < decoder->timestamp)
1352 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1353 timestamp, decoder->timestamp);
1355 decoder->timestamp = timestamp;
1358 /* Walk PSB+ packets when already in sync. */
1359 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1364 err = intel_pt_get_next_packet(decoder);
1368 switch (decoder->packet.type) {
1369 case INTEL_PT_PSBEND:
1372 case INTEL_PT_TIP_PGD:
1373 case INTEL_PT_TIP_PGE:
1376 case INTEL_PT_TRACESTOP:
1379 decoder->have_tma = false;
1380 intel_pt_log("ERROR: Unexpected packet\n");
1384 return intel_pt_overflow(decoder);
1387 intel_pt_calc_tsc_timestamp(decoder);
1391 intel_pt_calc_tma(decoder);
1395 intel_pt_calc_cbr(decoder);
1398 case INTEL_PT_MODE_EXEC:
1399 decoder->exec_mode = decoder->packet.payload;
1403 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1407 decoder->pge = true;
1408 intel_pt_set_last_ip(decoder);
1411 case INTEL_PT_MODE_TSX:
1412 intel_pt_update_in_tx(decoder);
1416 intel_pt_calc_mtc_timestamp(decoder);
1417 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1418 decoder->state.type |= INTEL_PT_INSTRUCTION;
1431 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1435 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1436 decoder->tx_flags = 0;
1437 decoder->state.flags &= ~INTEL_PT_IN_TX;
1438 decoder->state.flags |= INTEL_PT_ABORT_TX;
1440 decoder->state.flags |= INTEL_PT_ASYNC;
1444 err = intel_pt_get_next_packet(decoder);
1448 switch (decoder->packet.type) {
1451 case INTEL_PT_TRACESTOP:
1456 case INTEL_PT_MODE_TSX:
1458 case INTEL_PT_PSBEND:
1459 intel_pt_log("ERROR: Missing TIP after FUP\n");
1460 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1464 return intel_pt_overflow(decoder);
1466 case INTEL_PT_TIP_PGD:
1467 decoder->state.from_ip = decoder->ip;
1468 decoder->state.to_ip = 0;
1469 if (decoder->packet.count != 0) {
1470 intel_pt_set_ip(decoder);
1471 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1474 decoder->pge = false;
1475 decoder->continuous_period = false;
1478 case INTEL_PT_TIP_PGE:
1479 decoder->pge = true;
1480 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1482 decoder->state.from_ip = 0;
1483 if (decoder->packet.count == 0) {
1484 decoder->state.to_ip = 0;
1486 intel_pt_set_ip(decoder);
1487 decoder->state.to_ip = decoder->ip;
1492 decoder->state.from_ip = decoder->ip;
1493 if (decoder->packet.count == 0) {
1494 decoder->state.to_ip = 0;
1496 intel_pt_set_ip(decoder);
1497 decoder->state.to_ip = decoder->ip;
1502 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1506 intel_pt_calc_mtc_timestamp(decoder);
1507 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1508 decoder->state.type |= INTEL_PT_INSTRUCTION;
1512 intel_pt_calc_cyc_timestamp(decoder);
1515 case INTEL_PT_MODE_EXEC:
1516 decoder->exec_mode = decoder->packet.payload;
1525 return intel_pt_bug(decoder);
1530 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1532 bool no_tip = false;
1536 err = intel_pt_get_next_packet(decoder);
1540 switch (decoder->packet.type) {
1542 if (!decoder->packet.count)
1544 decoder->tnt = decoder->packet;
1545 decoder->pkt_state = INTEL_PT_STATE_TNT;
1546 err = intel_pt_walk_tnt(decoder);
1551 case INTEL_PT_TIP_PGD:
1552 if (decoder->packet.count != 0)
1553 intel_pt_set_last_ip(decoder);
1554 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1555 return intel_pt_walk_tip(decoder);
1557 case INTEL_PT_TIP_PGE: {
1558 decoder->pge = true;
1559 if (decoder->packet.count == 0) {
1560 intel_pt_log_at("Skipping zero TIP.PGE",
1564 intel_pt_set_ip(decoder);
1565 decoder->state.from_ip = 0;
1566 decoder->state.to_ip = decoder->ip;
1571 return intel_pt_overflow(decoder);
1574 if (decoder->packet.count != 0)
1575 intel_pt_set_last_ip(decoder);
1576 decoder->pkt_state = INTEL_PT_STATE_TIP;
1577 return intel_pt_walk_tip(decoder);
1580 if (decoder->packet.count == 0) {
1581 intel_pt_log_at("Skipping zero FUP",
1586 intel_pt_set_last_ip(decoder);
1587 err = intel_pt_walk_fup(decoder);
1588 if (err != -EAGAIN) {
1592 decoder->pkt_state =
1593 INTEL_PT_STATE_FUP_NO_TIP;
1595 decoder->pkt_state = INTEL_PT_STATE_FUP;
1602 return intel_pt_walk_fup_tip(decoder);
1604 case INTEL_PT_TRACESTOP:
1605 decoder->pge = false;
1606 decoder->continuous_period = false;
1607 intel_pt_clear_tx_flags(decoder);
1608 decoder->have_tma = false;
1612 intel_pt_clear_stack(&decoder->stack);
1613 err = intel_pt_walk_psbend(decoder);
1621 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1625 intel_pt_calc_mtc_timestamp(decoder);
1626 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1629 * Ensure that there has been an instruction since the
1632 if (!decoder->mtc_insn)
1634 decoder->mtc_insn = false;
1635 /* Ensure that there is a timestamp */
1636 if (!decoder->timestamp)
1638 decoder->state.type = INTEL_PT_INSTRUCTION;
1639 decoder->state.from_ip = decoder->ip;
1640 decoder->state.to_ip = 0;
1641 decoder->mtc_insn = false;
1645 intel_pt_calc_tsc_timestamp(decoder);
1649 intel_pt_calc_tma(decoder);
1653 intel_pt_calc_cyc_timestamp(decoder);
1657 intel_pt_calc_cbr(decoder);
1660 case INTEL_PT_MODE_EXEC:
1661 decoder->exec_mode = decoder->packet.payload;
1664 case INTEL_PT_MODE_TSX:
1665 /* MODE_TSX need not be followed by FUP */
1666 if (!decoder->pge) {
1667 intel_pt_update_in_tx(decoder);
1670 err = intel_pt_mode_tsx(decoder, &no_tip);
1675 case INTEL_PT_BAD: /* Does not happen */
1676 return intel_pt_bug(decoder);
1678 case INTEL_PT_PSBEND:
1685 return intel_pt_bug(decoder);
1690 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1692 return decoder->last_ip || decoder->packet.count == 0 ||
1693 decoder->packet.count == 3 || decoder->packet.count == 6;
1696 /* Walk PSB+ packets to get in sync. */
1697 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1702 err = intel_pt_get_next_packet(decoder);
1706 switch (decoder->packet.type) {
1707 case INTEL_PT_TIP_PGD:
1708 decoder->continuous_period = false;
1709 case INTEL_PT_TIP_PGE:
1711 intel_pt_log("ERROR: Unexpected packet\n");
1715 decoder->pge = true;
1716 if (intel_pt_have_ip(decoder)) {
1717 uint64_t current_ip = decoder->ip;
1719 intel_pt_set_ip(decoder);
1721 intel_pt_log_to("Setting IP",
1727 intel_pt_calc_mtc_timestamp(decoder);
1731 intel_pt_calc_tsc_timestamp(decoder);
1735 intel_pt_calc_tma(decoder);
1739 intel_pt_calc_cyc_timestamp(decoder);
1743 intel_pt_calc_cbr(decoder);
1747 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1750 case INTEL_PT_MODE_EXEC:
1751 decoder->exec_mode = decoder->packet.payload;
1754 case INTEL_PT_MODE_TSX:
1755 intel_pt_update_in_tx(decoder);
1758 case INTEL_PT_TRACESTOP:
1759 decoder->pge = false;
1760 decoder->continuous_period = false;
1761 intel_pt_clear_tx_flags(decoder);
1763 decoder->have_tma = false;
1764 intel_pt_log("ERROR: Unexpected packet\n");
1766 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1768 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1771 case INTEL_PT_BAD: /* Does not happen */
1772 return intel_pt_bug(decoder);
1775 return intel_pt_overflow(decoder);
1777 case INTEL_PT_PSBEND:
1790 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1795 err = intel_pt_get_next_packet(decoder);
1799 switch (decoder->packet.type) {
1800 case INTEL_PT_TIP_PGD:
1801 decoder->continuous_period = false;
1802 case INTEL_PT_TIP_PGE:
1804 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1805 if (intel_pt_have_ip(decoder))
1806 intel_pt_set_ip(decoder);
1812 if (decoder->overflow) {
1813 if (intel_pt_have_ip(decoder))
1814 intel_pt_set_ip(decoder);
1818 if (decoder->packet.count)
1819 intel_pt_set_last_ip(decoder);
1823 intel_pt_calc_mtc_timestamp(decoder);
1827 intel_pt_calc_tsc_timestamp(decoder);
1831 intel_pt_calc_tma(decoder);
1835 intel_pt_calc_cyc_timestamp(decoder);
1839 intel_pt_calc_cbr(decoder);
1843 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1846 case INTEL_PT_MODE_EXEC:
1847 decoder->exec_mode = decoder->packet.payload;
1850 case INTEL_PT_MODE_TSX:
1851 intel_pt_update_in_tx(decoder);
1855 return intel_pt_overflow(decoder);
1857 case INTEL_PT_BAD: /* Does not happen */
1858 return intel_pt_bug(decoder);
1860 case INTEL_PT_TRACESTOP:
1861 decoder->pge = false;
1862 decoder->continuous_period = false;
1863 intel_pt_clear_tx_flags(decoder);
1864 decoder->have_tma = false;
1868 err = intel_pt_walk_psb(decoder);
1872 /* Do not have a sample */
1873 decoder->state.type = 0;
1879 case INTEL_PT_PSBEND:
1889 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1893 intel_pt_log("Scanning for full IP\n");
1894 err = intel_pt_walk_to_ip(decoder);
1898 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1899 decoder->overflow = false;
1901 decoder->state.from_ip = 0;
1902 decoder->state.to_ip = decoder->ip;
1903 intel_pt_log_to("Setting IP", decoder->ip);
1908 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1910 const unsigned char *end = decoder->buf + decoder->len;
1913 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1914 if (i > decoder->len)
1916 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1922 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1924 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1925 const char *psb = INTEL_PT_PSB_STR;
1927 if (rest_psb > decoder->len ||
1928 memcmp(decoder->buf, psb + part_psb, rest_psb))
1934 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
1939 decoder->pos += decoder->len;
1942 ret = intel_pt_get_next_data(decoder);
1946 rest_psb = intel_pt_rest_psb(decoder, part_psb);
1950 decoder->pos -= part_psb;
1951 decoder->next_buf = decoder->buf + rest_psb;
1952 decoder->next_len = decoder->len - rest_psb;
1953 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1954 decoder->buf = decoder->temp_buf;
1955 decoder->len = INTEL_PT_PSB_LEN;
1960 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
1962 unsigned char *next;
1965 intel_pt_log("Scanning for PSB\n");
1967 if (!decoder->len) {
1968 ret = intel_pt_get_next_data(decoder);
1973 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
1978 part_psb = intel_pt_part_psb(decoder);
1980 ret = intel_pt_get_split_psb(decoder, part_psb);
1984 decoder->pos += decoder->len;
1990 decoder->pkt_step = next - decoder->buf;
1991 return intel_pt_get_next_packet(decoder);
1995 static int intel_pt_sync(struct intel_pt_decoder *decoder)
1999 decoder->pge = false;
2000 decoder->continuous_period = false;
2001 decoder->last_ip = 0;
2003 intel_pt_clear_stack(&decoder->stack);
2005 err = intel_pt_scan_for_psb(decoder);
2009 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2011 err = intel_pt_walk_psb(decoder);
2016 decoder->state.type = 0; /* Do not have a sample */
2017 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2019 return intel_pt_sync_ip(decoder);
2025 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2027 uint64_t est = decoder->timestamp_insn_cnt << 1;
2029 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2032 est *= decoder->max_non_turbo_ratio;
2033 est /= decoder->cbr;
2035 return decoder->timestamp + est;
2038 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2043 decoder->state.type = INTEL_PT_BRANCH;
2044 decoder->state.flags = 0;
2046 switch (decoder->pkt_state) {
2047 case INTEL_PT_STATE_NO_PSB:
2048 err = intel_pt_sync(decoder);
2050 case INTEL_PT_STATE_NO_IP:
2051 decoder->last_ip = 0;
2053 case INTEL_PT_STATE_ERR_RESYNC:
2054 err = intel_pt_sync_ip(decoder);
2056 case INTEL_PT_STATE_IN_SYNC:
2057 err = intel_pt_walk_trace(decoder);
2059 case INTEL_PT_STATE_TNT:
2060 err = intel_pt_walk_tnt(decoder);
2062 err = intel_pt_walk_trace(decoder);
2064 case INTEL_PT_STATE_TIP:
2065 case INTEL_PT_STATE_TIP_PGD:
2066 err = intel_pt_walk_tip(decoder);
2068 case INTEL_PT_STATE_FUP:
2069 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2070 err = intel_pt_walk_fup(decoder);
2072 err = intel_pt_walk_fup_tip(decoder);
2074 decoder->pkt_state = INTEL_PT_STATE_FUP;
2076 case INTEL_PT_STATE_FUP_NO_TIP:
2077 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2078 err = intel_pt_walk_fup(decoder);
2080 err = intel_pt_walk_trace(decoder);
2083 err = intel_pt_bug(decoder);
2086 } while (err == -ENOLINK);
2088 decoder->state.err = err ? intel_pt_ext_err(err) : 0;
2089 decoder->state.timestamp = decoder->timestamp;
2090 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2091 decoder->state.cr3 = decoder->cr3;
2092 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2095 decoder->state.from_ip = decoder->ip;
2097 return &decoder->state;
2100 static bool intel_pt_at_psb(unsigned char *buf, size_t len)
2102 if (len < INTEL_PT_PSB_LEN)
2104 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
2109 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2110 * @buf: pointer to buffer pointer
2111 * @len: size of buffer
2113 * Updates the buffer pointer to point to the start of the next PSB packet if
2114 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2115 * @len is adjusted accordingly.
2117 * Return: %true if a PSB packet is found, %false otherwise.
2119 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2121 unsigned char *next;
2123 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2125 *len -= next - *buf;
2133 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2135 * @buf: pointer to buffer pointer
2136 * @len: size of buffer
2138 * Updates the buffer pointer to point to the start of the following PSB packet
2139 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2140 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2142 * Return: %true if a PSB packet is found, %false otherwise.
2144 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2146 unsigned char *next;
2151 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2153 *len -= next - *buf;
2161 * intel_pt_last_psb - find the last PSB packet in a buffer.
2163 * @len: size of buffer
2165 * This function finds the last PSB in a buffer.
2167 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2169 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2171 const char *n = INTEL_PT_PSB_STR;
2175 if (len < INTEL_PT_PSB_LEN)
2178 k = len - INTEL_PT_PSB_LEN + 1;
2180 p = memrchr(buf, n[0], k);
2183 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2192 * intel_pt_next_tsc - find and return next TSC.
2194 * @len: size of buffer
2195 * @tsc: TSC value returned
2197 * Find a TSC packet in @buf and return the TSC value. This function assumes
2198 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2199 * PSBEND packet is found.
2201 * Return: %true if TSC is found, false otherwise.
2203 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
2205 struct intel_pt_pkt packet;
2209 ret = intel_pt_get_packet(buf, len, &packet);
2212 if (packet.type == INTEL_PT_TSC) {
2213 *tsc = packet.payload;
2216 if (packet.type == INTEL_PT_PSBEND)
2225 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2226 * @tsc1: first TSC to compare
2227 * @tsc2: second TSC to compare
2229 * This function compares 7-byte TSC values allowing for the possibility that
2230 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2231 * around so for that purpose this function assumes the absolute difference is
2232 * less than half the maximum difference.
2234 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2237 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2239 const uint64_t halfway = (1ULL << 55);
2245 if (tsc2 - tsc1 < halfway)
2250 if (tsc1 - tsc2 < halfway)
2258 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2260 * @buf_a: first buffer
2261 * @len_a: size of first buffer
2262 * @buf_b: second buffer
2263 * @len_b: size of second buffer
2265 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2266 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2267 * walk forward in @buf_b until a later TSC is found. A precondition is that
2268 * @buf_a and @buf_b are positioned at a PSB.
2270 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2271 * @buf_b + @len_b if there is no non-overlapped data.
2273 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2275 unsigned char *buf_b,
2278 uint64_t tsc_a, tsc_b;
2282 p = intel_pt_last_psb(buf_a, len_a);
2284 return buf_b; /* No PSB in buf_a => no overlap */
2286 len = len_a - (p - buf_a);
2287 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
2288 /* The last PSB+ in buf_a is incomplete, so go back one more */
2290 p = intel_pt_last_psb(buf_a, len_a);
2292 return buf_b; /* No full PSB+ => assume no overlap */
2293 len = len_a - (p - buf_a);
2294 if (!intel_pt_next_tsc(p, len, &tsc_a))
2295 return buf_b; /* No TSC in buf_a => assume no overlap */
2299 /* Ignore PSB+ with no TSC */
2300 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
2301 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
2302 return buf_b; /* tsc_a < tsc_b => no overlap */
2304 if (!intel_pt_step_psb(&buf_b, &len_b))
2305 return buf_b + len_b; /* No PSB in buf_b => no data */
2310 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2311 * @buf_a: first buffer
2312 * @len_a: size of first buffer
2313 * @buf_b: second buffer
2314 * @len_b: size of second buffer
2315 * @have_tsc: can use TSC packets to detect overlap
2317 * When trace samples or snapshots are recorded there is the possibility that
2318 * the data overlaps. Note that, for the purposes of decoding, data is only
2319 * useful if it begins with a PSB packet.
2321 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2322 * @buf_b + @len_b if there is no non-overlapped data.
2324 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2325 unsigned char *buf_b, size_t len_b,
2328 unsigned char *found;
2330 /* Buffer 'b' must start at PSB so throw away everything before that */
2331 if (!intel_pt_next_psb(&buf_b, &len_b))
2332 return buf_b + len_b; /* No PSB */
2334 if (!intel_pt_next_psb(&buf_a, &len_a))
2335 return buf_b; /* No overlap */
2338 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
2344 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2345 * we can ignore the first part of buffer 'a'.
2347 while (len_b < len_a) {
2348 if (!intel_pt_step_psb(&buf_a, &len_a))
2349 return buf_b; /* No overlap */
2352 /* Now len_b >= len_a */
2353 if (len_b > len_a) {
2354 /* The leftover buffer 'b' must start at a PSB */
2355 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2356 if (!intel_pt_step_psb(&buf_a, &len_a))
2357 return buf_b; /* No overlap */
2362 /* Potential overlap so check the bytes */
2363 found = memmem(buf_a, len_a, buf_b, len_a);
2365 return buf_b + len_a;
2367 /* Try again at next PSB in buffer 'a' */
2368 if (!intel_pt_step_psb(&buf_a, &len_a))
2369 return buf_b; /* No overlap */
2371 /* The leftover buffer 'b' must start at a PSB */
2372 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2373 if (!intel_pt_step_psb(&buf_a, &len_a))
2374 return buf_b; /* No overlap */