2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 #include <linux/compiler.h>
30 #include "intel-pt-insn-decoder.h"
31 #include "intel-pt-pkt-decoder.h"
32 #include "intel-pt-decoder.h"
33 #include "intel-pt-log.h"
35 #define INTEL_PT_BLK_SIZE 1024
37 #define BIT63 (((uint64_t)1 << 63))
39 #define INTEL_PT_RETURN 1
41 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
42 #define INTEL_PT_MAX_LOOPS 10000
45 struct intel_pt_blk
*prev
;
46 uint64_t ip
[INTEL_PT_BLK_SIZE
];
49 struct intel_pt_stack
{
50 struct intel_pt_blk
*blk
;
51 struct intel_pt_blk
*spare
;
55 enum intel_pt_pkt_state
{
56 INTEL_PT_STATE_NO_PSB
,
58 INTEL_PT_STATE_ERR_RESYNC
,
59 INTEL_PT_STATE_IN_SYNC
,
62 INTEL_PT_STATE_TIP_PGD
,
64 INTEL_PT_STATE_FUP_NO_TIP
,
67 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state
)
70 case INTEL_PT_STATE_NO_PSB
:
71 case INTEL_PT_STATE_NO_IP
:
72 case INTEL_PT_STATE_ERR_RESYNC
:
73 case INTEL_PT_STATE_IN_SYNC
:
74 case INTEL_PT_STATE_TNT
:
76 case INTEL_PT_STATE_TIP
:
77 case INTEL_PT_STATE_TIP_PGD
:
78 case INTEL_PT_STATE_FUP
:
79 case INTEL_PT_STATE_FUP_NO_TIP
:
86 #ifdef INTEL_PT_STRICT
87 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
88 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
89 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
90 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
92 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
93 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
94 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
95 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
98 struct intel_pt_decoder
{
99 int (*get_trace
)(struct intel_pt_buffer
*buffer
, void *data
);
100 int (*walk_insn
)(struct intel_pt_insn
*intel_pt_insn
,
101 uint64_t *insn_cnt_ptr
, uint64_t *ip
, uint64_t to_ip
,
102 uint64_t max_insn_cnt
, void *data
);
103 bool (*pgd_ip
)(uint64_t ip
, void *data
);
105 struct intel_pt_state state
;
106 const unsigned char *buf
;
108 bool return_compression
;
116 enum intel_pt_param_flags flags
;
122 uint64_t tsc_timestamp
;
123 uint64_t ref_timestamp
;
124 uint64_t sample_timestamp
;
126 uint64_t ctc_timestamp
;
129 uint64_t cyc_ref_timestamp
;
131 uint32_t tsc_ctc_ratio_n
;
132 uint32_t tsc_ctc_ratio_d
;
133 uint32_t tsc_ctc_mult
;
135 uint32_t ctc_rem_mask
;
137 struct intel_pt_stack stack
;
138 enum intel_pt_pkt_state pkt_state
;
139 struct intel_pt_pkt packet
;
140 struct intel_pt_pkt tnt
;
143 int last_packet_type
;
145 unsigned int cbr_seen
;
146 unsigned int max_non_turbo_ratio
;
147 double max_non_turbo_ratio_fp
;
148 double cbr_cyc_to_tsc
;
149 double calc_cyc_to_tsc
;
150 bool have_calc_cyc_to_tsc
;
152 unsigned int insn_bytes
;
154 enum intel_pt_period_type period_type
;
155 uint64_t tot_insn_cnt
;
156 uint64_t period_insn_cnt
;
157 uint64_t period_mask
;
158 uint64_t period_ticks
;
159 uint64_t last_masked_timestamp
;
160 bool continuous_period
;
162 bool set_fup_tx_flags
;
167 unsigned int fup_tx_flags
;
168 unsigned int tx_flags
;
169 uint64_t fup_ptw_payload
;
170 uint64_t fup_mwait_payload
;
171 uint64_t fup_pwre_payload
;
172 uint64_t cbr_payload
;
173 uint64_t timestamp_insn_cnt
;
174 uint64_t sample_insn_cnt
;
179 const unsigned char *next_buf
;
181 unsigned char temp_buf
[INTEL_PT_PKT_MAX_SZ
];
184 static uint64_t intel_pt_lower_power_of_2(uint64_t x
)
188 for (i
= 0; x
!= 1; i
++)
194 static void intel_pt_setup_period(struct intel_pt_decoder
*decoder
)
196 if (decoder
->period_type
== INTEL_PT_PERIOD_TICKS
) {
199 period
= intel_pt_lower_power_of_2(decoder
->period
);
200 decoder
->period_mask
= ~(period
- 1);
201 decoder
->period_ticks
= period
;
205 static uint64_t multdiv(uint64_t t
, uint32_t n
, uint32_t d
)
209 return (t
/ d
) * n
+ ((t
% d
) * n
) / d
;
212 struct intel_pt_decoder
*intel_pt_decoder_new(struct intel_pt_params
*params
)
214 struct intel_pt_decoder
*decoder
;
216 if (!params
->get_trace
|| !params
->walk_insn
)
219 decoder
= zalloc(sizeof(struct intel_pt_decoder
));
223 decoder
->get_trace
= params
->get_trace
;
224 decoder
->walk_insn
= params
->walk_insn
;
225 decoder
->pgd_ip
= params
->pgd_ip
;
226 decoder
->data
= params
->data
;
227 decoder
->return_compression
= params
->return_compression
;
228 decoder
->branch_enable
= params
->branch_enable
;
230 decoder
->flags
= params
->flags
;
232 decoder
->period
= params
->period
;
233 decoder
->period_type
= params
->period_type
;
235 decoder
->max_non_turbo_ratio
= params
->max_non_turbo_ratio
;
236 decoder
->max_non_turbo_ratio_fp
= params
->max_non_turbo_ratio
;
238 intel_pt_setup_period(decoder
);
240 decoder
->mtc_shift
= params
->mtc_period
;
241 decoder
->ctc_rem_mask
= (1 << decoder
->mtc_shift
) - 1;
243 decoder
->tsc_ctc_ratio_n
= params
->tsc_ctc_ratio_n
;
244 decoder
->tsc_ctc_ratio_d
= params
->tsc_ctc_ratio_d
;
246 if (!decoder
->tsc_ctc_ratio_n
)
247 decoder
->tsc_ctc_ratio_d
= 0;
249 if (decoder
->tsc_ctc_ratio_d
) {
250 if (!(decoder
->tsc_ctc_ratio_n
% decoder
->tsc_ctc_ratio_d
))
251 decoder
->tsc_ctc_mult
= decoder
->tsc_ctc_ratio_n
/
252 decoder
->tsc_ctc_ratio_d
;
255 * Allow for timestamps appearing to backwards because a TSC
256 * packet has slipped past a MTC packet, so allow 2 MTC ticks
259 decoder
->tsc_slip
= multdiv(2 << decoder
->mtc_shift
,
260 decoder
->tsc_ctc_ratio_n
,
261 decoder
->tsc_ctc_ratio_d
);
263 /* ... or 0x100 paranoia */
264 if (decoder
->tsc_slip
< 0x100)
265 decoder
->tsc_slip
= 0x100;
267 intel_pt_log("timestamp: mtc_shift %u\n", decoder
->mtc_shift
);
268 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder
->tsc_ctc_ratio_n
);
269 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder
->tsc_ctc_ratio_d
);
270 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder
->tsc_ctc_mult
);
271 intel_pt_log("timestamp: tsc_slip %#x\n", decoder
->tsc_slip
);
276 static void intel_pt_pop_blk(struct intel_pt_stack
*stack
)
278 struct intel_pt_blk
*blk
= stack
->blk
;
280 stack
->blk
= blk
->prev
;
287 static uint64_t intel_pt_pop(struct intel_pt_stack
*stack
)
292 intel_pt_pop_blk(stack
);
295 stack
->pos
= INTEL_PT_BLK_SIZE
;
297 return stack
->blk
->ip
[--stack
->pos
];
300 static int intel_pt_alloc_blk(struct intel_pt_stack
*stack
)
302 struct intel_pt_blk
*blk
;
308 blk
= malloc(sizeof(struct intel_pt_blk
));
313 blk
->prev
= stack
->blk
;
319 static int intel_pt_push(struct intel_pt_stack
*stack
, uint64_t ip
)
323 if (!stack
->blk
|| stack
->pos
== INTEL_PT_BLK_SIZE
) {
324 err
= intel_pt_alloc_blk(stack
);
329 stack
->blk
->ip
[stack
->pos
++] = ip
;
333 static void intel_pt_clear_stack(struct intel_pt_stack
*stack
)
336 intel_pt_pop_blk(stack
);
340 static void intel_pt_free_stack(struct intel_pt_stack
*stack
)
342 intel_pt_clear_stack(stack
);
344 zfree(&stack
->spare
);
347 void intel_pt_decoder_free(struct intel_pt_decoder
*decoder
)
349 intel_pt_free_stack(&decoder
->stack
);
353 static int intel_pt_ext_err(int code
)
357 return INTEL_PT_ERR_NOMEM
;
359 return INTEL_PT_ERR_INTERN
;
361 return INTEL_PT_ERR_BADPKT
;
363 return INTEL_PT_ERR_NODATA
;
365 return INTEL_PT_ERR_NOINSN
;
367 return INTEL_PT_ERR_MISMAT
;
369 return INTEL_PT_ERR_OVR
;
371 return INTEL_PT_ERR_LOST
;
373 return INTEL_PT_ERR_NELOOP
;
375 return INTEL_PT_ERR_UNK
;
379 static const char *intel_pt_err_msgs
[] = {
380 [INTEL_PT_ERR_NOMEM
] = "Memory allocation failed",
381 [INTEL_PT_ERR_INTERN
] = "Internal error",
382 [INTEL_PT_ERR_BADPKT
] = "Bad packet",
383 [INTEL_PT_ERR_NODATA
] = "No more data",
384 [INTEL_PT_ERR_NOINSN
] = "Failed to get instruction",
385 [INTEL_PT_ERR_MISMAT
] = "Trace doesn't match instruction",
386 [INTEL_PT_ERR_OVR
] = "Overflow packet",
387 [INTEL_PT_ERR_LOST
] = "Lost trace data",
388 [INTEL_PT_ERR_UNK
] = "Unknown error!",
389 [INTEL_PT_ERR_NELOOP
] = "Never-ending loop",
392 int intel_pt__strerror(int code
, char *buf
, size_t buflen
)
394 if (code
< 1 || code
>= INTEL_PT_ERR_MAX
)
395 code
= INTEL_PT_ERR_UNK
;
396 strlcpy(buf
, intel_pt_err_msgs
[code
], buflen
);
400 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt
*packet
,
405 switch (packet
->count
) {
407 ip
= (last_ip
& (uint64_t)0xffffffffffff0000ULL
) |
411 ip
= (last_ip
& (uint64_t)0xffffffff00000000ULL
) |
415 ip
= packet
->payload
;
416 /* Sign-extend 6-byte ip */
417 if (ip
& (uint64_t)0x800000000000ULL
)
418 ip
|= (uint64_t)0xffff000000000000ULL
;
421 ip
= (last_ip
& (uint64_t)0xffff000000000000ULL
) |
425 ip
= packet
->payload
;
434 static inline void intel_pt_set_last_ip(struct intel_pt_decoder
*decoder
)
436 decoder
->last_ip
= intel_pt_calc_ip(&decoder
->packet
, decoder
->last_ip
);
437 decoder
->have_last_ip
= true;
440 static inline void intel_pt_set_ip(struct intel_pt_decoder
*decoder
)
442 intel_pt_set_last_ip(decoder
);
443 decoder
->ip
= decoder
->last_ip
;
446 static void intel_pt_decoder_log_packet(struct intel_pt_decoder
*decoder
)
448 intel_pt_log_packet(&decoder
->packet
, decoder
->pkt_len
, decoder
->pos
,
452 static int intel_pt_bug(struct intel_pt_decoder
*decoder
)
454 intel_pt_log("ERROR: Internal error\n");
455 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
459 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder
*decoder
)
461 decoder
->tx_flags
= 0;
464 static inline void intel_pt_update_in_tx(struct intel_pt_decoder
*decoder
)
466 decoder
->tx_flags
= decoder
->packet
.payload
& INTEL_PT_IN_TX
;
469 static int intel_pt_bad_packet(struct intel_pt_decoder
*decoder
)
471 intel_pt_clear_tx_flags(decoder
);
472 decoder
->have_tma
= false;
473 decoder
->pkt_len
= 1;
474 decoder
->pkt_step
= 1;
475 intel_pt_decoder_log_packet(decoder
);
476 if (decoder
->pkt_state
!= INTEL_PT_STATE_NO_PSB
) {
477 intel_pt_log("ERROR: Bad packet\n");
478 decoder
->pkt_state
= INTEL_PT_STATE_ERR1
;
483 static int intel_pt_get_data(struct intel_pt_decoder
*decoder
)
485 struct intel_pt_buffer buffer
= { .buf
= 0, };
488 decoder
->pkt_step
= 0;
490 intel_pt_log("Getting more data\n");
491 ret
= decoder
->get_trace(&buffer
, decoder
->data
);
494 decoder
->buf
= buffer
.buf
;
495 decoder
->len
= buffer
.len
;
497 intel_pt_log("No more data\n");
500 if (!buffer
.consecutive
) {
502 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
503 decoder
->ref_timestamp
= buffer
.ref_timestamp
;
504 decoder
->timestamp
= 0;
505 decoder
->have_tma
= false;
506 decoder
->state
.trace_nr
= buffer
.trace_nr
;
507 intel_pt_log("Reference timestamp 0x%" PRIx64
"\n",
508 decoder
->ref_timestamp
);
515 static int intel_pt_get_next_data(struct intel_pt_decoder
*decoder
)
517 if (!decoder
->next_buf
)
518 return intel_pt_get_data(decoder
);
520 decoder
->buf
= decoder
->next_buf
;
521 decoder
->len
= decoder
->next_len
;
522 decoder
->next_buf
= 0;
523 decoder
->next_len
= 0;
527 static int intel_pt_get_split_packet(struct intel_pt_decoder
*decoder
)
529 unsigned char *buf
= decoder
->temp_buf
;
530 size_t old_len
, len
, n
;
533 old_len
= decoder
->len
;
535 memcpy(buf
, decoder
->buf
, len
);
537 ret
= intel_pt_get_data(decoder
);
539 decoder
->pos
+= old_len
;
540 return ret
< 0 ? ret
: -EINVAL
;
543 n
= INTEL_PT_PKT_MAX_SZ
- len
;
544 if (n
> decoder
->len
)
546 memcpy(buf
+ len
, decoder
->buf
, n
);
549 ret
= intel_pt_get_packet(buf
, len
, &decoder
->packet
);
550 if (ret
< (int)old_len
) {
551 decoder
->next_buf
= decoder
->buf
;
552 decoder
->next_len
= decoder
->len
;
554 decoder
->len
= old_len
;
555 return intel_pt_bad_packet(decoder
);
558 decoder
->next_buf
= decoder
->buf
+ (ret
- old_len
);
559 decoder
->next_len
= decoder
->len
- (ret
- old_len
);
567 struct intel_pt_pkt_info
{
568 struct intel_pt_decoder
*decoder
;
569 struct intel_pt_pkt packet
;
572 int last_packet_type
;
576 typedef int (*intel_pt_pkt_cb_t
)(struct intel_pt_pkt_info
*pkt_info
);
578 /* Lookahead packets in current buffer */
579 static int intel_pt_pkt_lookahead(struct intel_pt_decoder
*decoder
,
580 intel_pt_pkt_cb_t cb
, void *data
)
582 struct intel_pt_pkt_info pkt_info
;
583 const unsigned char *buf
= decoder
->buf
;
584 size_t len
= decoder
->len
;
587 pkt_info
.decoder
= decoder
;
588 pkt_info
.pos
= decoder
->pos
;
589 pkt_info
.pkt_len
= decoder
->pkt_step
;
590 pkt_info
.last_packet_type
= decoder
->last_packet_type
;
591 pkt_info
.data
= data
;
595 pkt_info
.pos
+= pkt_info
.pkt_len
;
596 buf
+= pkt_info
.pkt_len
;
597 len
-= pkt_info
.pkt_len
;
600 return INTEL_PT_NEED_MORE_BYTES
;
602 ret
= intel_pt_get_packet(buf
, len
, &pkt_info
.packet
);
604 return INTEL_PT_NEED_MORE_BYTES
;
608 pkt_info
.pkt_len
= ret
;
609 } while (pkt_info
.packet
.type
== INTEL_PT_PAD
);
615 pkt_info
.last_packet_type
= pkt_info
.packet
.type
;
619 struct intel_pt_calc_cyc_to_tsc_info
{
623 uint64_t ctc_timestamp
;
625 uint64_t tsc_timestamp
;
630 double cbr_cyc_to_tsc
;
634 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
635 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
636 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
637 * packet by copying the missing bits from the current MTC assuming the least
638 * difference between the two, and that the current MTC comes after last_mtc.
640 static void intel_pt_fixup_last_mtc(uint32_t mtc
, int mtc_shift
,
643 uint32_t first_missing_bit
= 1U << (16 - mtc_shift
);
644 uint32_t mask
= ~(first_missing_bit
- 1);
646 *last_mtc
|= mtc
& mask
;
647 if (*last_mtc
>= mtc
) {
648 *last_mtc
-= first_missing_bit
;
653 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info
*pkt_info
)
655 struct intel_pt_decoder
*decoder
= pkt_info
->decoder
;
656 struct intel_pt_calc_cyc_to_tsc_info
*data
= pkt_info
->data
;
660 uint32_t mtc
, mtc_delta
, ctc
, fc
, ctc_rem
;
662 switch (pkt_info
->packet
.type
) {
664 case INTEL_PT_TIP_PGE
:
669 case INTEL_PT_MODE_EXEC
:
670 case INTEL_PT_MODE_TSX
:
671 case INTEL_PT_PSBEND
:
675 case INTEL_PT_PTWRITE
:
676 case INTEL_PT_PTWRITE_IP
:
683 mtc
= pkt_info
->packet
.payload
;
684 if (decoder
->mtc_shift
> 8 && data
->fixup_last_mtc
) {
685 data
->fixup_last_mtc
= false;
686 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
689 if (mtc
> data
->last_mtc
)
690 mtc_delta
= mtc
- data
->last_mtc
;
692 mtc_delta
= mtc
+ 256 - data
->last_mtc
;
693 data
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
694 data
->last_mtc
= mtc
;
696 if (decoder
->tsc_ctc_mult
) {
697 timestamp
= data
->ctc_timestamp
+
698 data
->ctc_delta
* decoder
->tsc_ctc_mult
;
700 timestamp
= data
->ctc_timestamp
+
701 multdiv(data
->ctc_delta
,
702 decoder
->tsc_ctc_ratio_n
,
703 decoder
->tsc_ctc_ratio_d
);
706 if (timestamp
< data
->timestamp
)
709 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
710 data
->timestamp
= timestamp
;
718 * For now, do not support using TSC packets - refer
719 * intel_pt_calc_cyc_to_tsc().
723 timestamp
= pkt_info
->packet
.payload
|
724 (data
->timestamp
& (0xffULL
<< 56));
725 if (data
->from_mtc
&& timestamp
< data
->timestamp
&&
726 data
->timestamp
- timestamp
< decoder
->tsc_slip
)
728 if (timestamp
< data
->timestamp
)
729 timestamp
+= (1ULL << 56);
730 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
733 data
->tsc_timestamp
= timestamp
;
734 data
->timestamp
= timestamp
;
743 if (!decoder
->tsc_ctc_ratio_d
)
746 ctc
= pkt_info
->packet
.payload
;
747 fc
= pkt_info
->packet
.count
;
748 ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
750 data
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
752 data
->ctc_timestamp
= data
->tsc_timestamp
- fc
;
753 if (decoder
->tsc_ctc_mult
) {
754 data
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
756 data
->ctc_timestamp
-=
757 multdiv(ctc_rem
, decoder
->tsc_ctc_ratio_n
,
758 decoder
->tsc_ctc_ratio_d
);
762 data
->have_tma
= true;
763 data
->fixup_last_mtc
= true;
768 data
->cycle_cnt
+= pkt_info
->packet
.payload
;
772 cbr
= pkt_info
->packet
.payload
;
773 if (data
->cbr
&& data
->cbr
!= cbr
)
776 data
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
779 case INTEL_PT_TIP_PGD
:
780 case INTEL_PT_TRACESTOP
:
781 case INTEL_PT_EXSTOP
:
782 case INTEL_PT_EXSTOP_IP
:
787 case INTEL_PT_BAD
: /* Does not happen */
792 if (!data
->cbr
&& decoder
->cbr
) {
793 data
->cbr
= decoder
->cbr
;
794 data
->cbr_cyc_to_tsc
= decoder
->cbr_cyc_to_tsc
;
797 if (!data
->cycle_cnt
)
800 cyc_to_tsc
= (double)(timestamp
- decoder
->timestamp
) / data
->cycle_cnt
;
802 if (data
->cbr
&& cyc_to_tsc
> data
->cbr_cyc_to_tsc
&&
803 cyc_to_tsc
/ data
->cbr_cyc_to_tsc
> 1.25) {
804 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt
"\n",
805 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
809 decoder
->calc_cyc_to_tsc
= cyc_to_tsc
;
810 decoder
->have_calc_cyc_to_tsc
= true;
813 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt
"\n",
814 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
816 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt
"\n",
817 cyc_to_tsc
, pkt_info
->pos
);
823 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder
*decoder
,
826 struct intel_pt_calc_cyc_to_tsc_info data
= {
829 .last_mtc
= decoder
->last_mtc
,
830 .ctc_timestamp
= decoder
->ctc_timestamp
,
831 .ctc_delta
= decoder
->ctc_delta
,
832 .tsc_timestamp
= decoder
->tsc_timestamp
,
833 .timestamp
= decoder
->timestamp
,
834 .have_tma
= decoder
->have_tma
,
835 .fixup_last_mtc
= decoder
->fixup_last_mtc
,
836 .from_mtc
= from_mtc
,
841 * For now, do not support using TSC packets for at least the reasons:
842 * 1) timing might have stopped
843 * 2) TSC packets within PSB+ can slip against CYC packets
848 intel_pt_pkt_lookahead(decoder
, intel_pt_calc_cyc_cb
, &data
);
851 static int intel_pt_get_next_packet(struct intel_pt_decoder
*decoder
)
855 decoder
->last_packet_type
= decoder
->packet
.type
;
858 decoder
->pos
+= decoder
->pkt_step
;
859 decoder
->buf
+= decoder
->pkt_step
;
860 decoder
->len
-= decoder
->pkt_step
;
863 ret
= intel_pt_get_next_data(decoder
);
868 ret
= intel_pt_get_packet(decoder
->buf
, decoder
->len
,
870 if (ret
== INTEL_PT_NEED_MORE_BYTES
&&
871 decoder
->len
< INTEL_PT_PKT_MAX_SZ
&& !decoder
->next_buf
) {
872 ret
= intel_pt_get_split_packet(decoder
);
877 return intel_pt_bad_packet(decoder
);
879 decoder
->pkt_len
= ret
;
880 decoder
->pkt_step
= ret
;
881 intel_pt_decoder_log_packet(decoder
);
882 } while (decoder
->packet
.type
== INTEL_PT_PAD
);
887 static uint64_t intel_pt_next_period(struct intel_pt_decoder
*decoder
)
889 uint64_t timestamp
, masked_timestamp
;
891 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
892 masked_timestamp
= timestamp
& decoder
->period_mask
;
893 if (decoder
->continuous_period
) {
894 if (masked_timestamp
!= decoder
->last_masked_timestamp
)
898 masked_timestamp
= timestamp
& decoder
->period_mask
;
899 if (masked_timestamp
!= decoder
->last_masked_timestamp
) {
900 decoder
->last_masked_timestamp
= masked_timestamp
;
901 decoder
->continuous_period
= true;
904 return decoder
->period_ticks
- (timestamp
- masked_timestamp
);
907 static uint64_t intel_pt_next_sample(struct intel_pt_decoder
*decoder
)
909 switch (decoder
->period_type
) {
910 case INTEL_PT_PERIOD_INSTRUCTIONS
:
911 return decoder
->period
- decoder
->period_insn_cnt
;
912 case INTEL_PT_PERIOD_TICKS
:
913 return intel_pt_next_period(decoder
);
914 case INTEL_PT_PERIOD_NONE
:
915 case INTEL_PT_PERIOD_MTC
:
921 static void intel_pt_sample_insn(struct intel_pt_decoder
*decoder
)
923 uint64_t timestamp
, masked_timestamp
;
925 switch (decoder
->period_type
) {
926 case INTEL_PT_PERIOD_INSTRUCTIONS
:
927 decoder
->period_insn_cnt
= 0;
929 case INTEL_PT_PERIOD_TICKS
:
930 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
931 masked_timestamp
= timestamp
& decoder
->period_mask
;
932 decoder
->last_masked_timestamp
= masked_timestamp
;
934 case INTEL_PT_PERIOD_NONE
:
935 case INTEL_PT_PERIOD_MTC
:
940 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
943 static int intel_pt_walk_insn(struct intel_pt_decoder
*decoder
,
944 struct intel_pt_insn
*intel_pt_insn
, uint64_t ip
)
946 uint64_t max_insn_cnt
, insn_cnt
= 0;
949 if (!decoder
->mtc_insn
)
950 decoder
->mtc_insn
= true;
952 max_insn_cnt
= intel_pt_next_sample(decoder
);
954 err
= decoder
->walk_insn(intel_pt_insn
, &insn_cnt
, &decoder
->ip
, ip
,
955 max_insn_cnt
, decoder
->data
);
957 decoder
->tot_insn_cnt
+= insn_cnt
;
958 decoder
->timestamp_insn_cnt
+= insn_cnt
;
959 decoder
->sample_insn_cnt
+= insn_cnt
;
960 decoder
->period_insn_cnt
+= insn_cnt
;
963 decoder
->no_progress
= 0;
964 decoder
->pkt_state
= INTEL_PT_STATE_ERR2
;
965 intel_pt_log_at("ERROR: Failed to get instruction",
972 if (ip
&& decoder
->ip
== ip
) {
977 if (max_insn_cnt
&& insn_cnt
>= max_insn_cnt
)
978 intel_pt_sample_insn(decoder
);
980 if (intel_pt_insn
->branch
== INTEL_PT_BR_NO_BRANCH
) {
981 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
982 decoder
->state
.from_ip
= decoder
->ip
;
983 decoder
->state
.to_ip
= 0;
984 decoder
->ip
+= intel_pt_insn
->length
;
985 err
= INTEL_PT_RETURN
;
989 if (intel_pt_insn
->op
== INTEL_PT_OP_CALL
) {
990 /* Zero-length calls are excluded */
991 if (intel_pt_insn
->branch
!= INTEL_PT_BR_UNCONDITIONAL
||
992 intel_pt_insn
->rel
) {
993 err
= intel_pt_push(&decoder
->stack
, decoder
->ip
+
994 intel_pt_insn
->length
);
998 } else if (intel_pt_insn
->op
== INTEL_PT_OP_RET
) {
999 decoder
->ret_addr
= intel_pt_pop(&decoder
->stack
);
1002 if (intel_pt_insn
->branch
== INTEL_PT_BR_UNCONDITIONAL
) {
1003 int cnt
= decoder
->no_progress
++;
1005 decoder
->state
.from_ip
= decoder
->ip
;
1006 decoder
->ip
+= intel_pt_insn
->length
+
1008 decoder
->state
.to_ip
= decoder
->ip
;
1009 err
= INTEL_PT_RETURN
;
1012 * Check for being stuck in a loop. This can happen if a
1013 * decoder error results in the decoder erroneously setting the
1014 * ip to an address that is itself in an infinite loop that
1015 * consumes no packets. When that happens, there must be an
1016 * unconditional branch.
1020 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1021 decoder
->stuck_ip_prd
= 1;
1022 decoder
->stuck_ip_cnt
= 1;
1023 } else if (cnt
> INTEL_PT_MAX_LOOPS
||
1024 decoder
->state
.to_ip
== decoder
->stuck_ip
) {
1025 intel_pt_log_at("ERROR: Never-ending loop",
1026 decoder
->state
.to_ip
);
1027 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1030 } else if (!--decoder
->stuck_ip_cnt
) {
1031 decoder
->stuck_ip_prd
+= 1;
1032 decoder
->stuck_ip_cnt
= decoder
->stuck_ip_prd
;
1033 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1036 goto out_no_progress
;
1039 decoder
->no_progress
= 0;
1041 decoder
->state
.insn_op
= intel_pt_insn
->op
;
1042 decoder
->state
.insn_len
= intel_pt_insn
->length
;
1043 memcpy(decoder
->state
.insn
, intel_pt_insn
->buf
,
1044 INTEL_PT_INSN_BUF_SZ
);
1046 if (decoder
->tx_flags
& INTEL_PT_IN_TX
)
1047 decoder
->state
.flags
|= INTEL_PT_IN_TX
;
1052 static bool intel_pt_fup_event(struct intel_pt_decoder
*decoder
)
1056 if (decoder
->set_fup_tx_flags
) {
1057 decoder
->set_fup_tx_flags
= false;
1058 decoder
->tx_flags
= decoder
->fup_tx_flags
;
1059 decoder
->state
.type
= INTEL_PT_TRANSACTION
;
1060 decoder
->state
.from_ip
= decoder
->ip
;
1061 decoder
->state
.to_ip
= 0;
1062 decoder
->state
.flags
= decoder
->fup_tx_flags
;
1065 if (decoder
->set_fup_ptw
) {
1066 decoder
->set_fup_ptw
= false;
1067 decoder
->state
.type
= INTEL_PT_PTW
;
1068 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1069 decoder
->state
.from_ip
= decoder
->ip
;
1070 decoder
->state
.to_ip
= 0;
1071 decoder
->state
.ptw_payload
= decoder
->fup_ptw_payload
;
1074 if (decoder
->set_fup_mwait
) {
1075 decoder
->set_fup_mwait
= false;
1076 decoder
->state
.type
= INTEL_PT_MWAIT_OP
;
1077 decoder
->state
.from_ip
= decoder
->ip
;
1078 decoder
->state
.to_ip
= 0;
1079 decoder
->state
.mwait_payload
= decoder
->fup_mwait_payload
;
1082 if (decoder
->set_fup_pwre
) {
1083 decoder
->set_fup_pwre
= false;
1084 decoder
->state
.type
|= INTEL_PT_PWR_ENTRY
;
1085 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1086 decoder
->state
.from_ip
= decoder
->ip
;
1087 decoder
->state
.to_ip
= 0;
1088 decoder
->state
.pwre_payload
= decoder
->fup_pwre_payload
;
1091 if (decoder
->set_fup_exstop
) {
1092 decoder
->set_fup_exstop
= false;
1093 decoder
->state
.type
|= INTEL_PT_EX_STOP
;
1094 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1095 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1096 decoder
->state
.from_ip
= decoder
->ip
;
1097 decoder
->state
.to_ip
= 0;
1103 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder
*decoder
,
1104 struct intel_pt_insn
*intel_pt_insn
,
1105 uint64_t ip
, int err
)
1107 return decoder
->flags
& INTEL_PT_FUP_WITH_NLIP
&& !err
&&
1108 intel_pt_insn
->branch
== INTEL_PT_BR_INDIRECT
&&
1109 ip
== decoder
->ip
+ intel_pt_insn
->length
;
1112 static int intel_pt_walk_fup(struct intel_pt_decoder
*decoder
)
1114 struct intel_pt_insn intel_pt_insn
;
1118 ip
= decoder
->last_ip
;
1121 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, ip
);
1122 if (err
== INTEL_PT_RETURN
)
1124 if (err
== -EAGAIN
||
1125 intel_pt_fup_with_nlip(decoder
, &intel_pt_insn
, ip
, err
)) {
1126 if (intel_pt_fup_event(decoder
))
1130 decoder
->set_fup_tx_flags
= false;
1134 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1135 intel_pt_log_at("ERROR: Unexpected indirect branch",
1137 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1141 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1142 intel_pt_log_at("ERROR: Unexpected conditional branch",
1144 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1148 intel_pt_bug(decoder
);
1152 static int intel_pt_walk_tip(struct intel_pt_decoder
*decoder
)
1154 struct intel_pt_insn intel_pt_insn
;
1157 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1158 if (err
== INTEL_PT_RETURN
&&
1160 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1161 (decoder
->state
.type
& INTEL_PT_BRANCH
) &&
1162 decoder
->pgd_ip(decoder
->state
.to_ip
, decoder
->data
)) {
1163 /* Unconditional branch leaving filter region */
1164 decoder
->no_progress
= 0;
1165 decoder
->pge
= false;
1166 decoder
->continuous_period
= false;
1167 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1168 decoder
->state
.to_ip
= 0;
1171 if (err
== INTEL_PT_RETURN
)
1176 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1177 if (decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
) {
1178 decoder
->pge
= false;
1179 decoder
->continuous_period
= false;
1180 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1181 decoder
->state
.from_ip
= decoder
->ip
;
1182 decoder
->state
.to_ip
= 0;
1183 if (decoder
->packet
.count
!= 0)
1184 decoder
->ip
= decoder
->last_ip
;
1186 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1187 decoder
->state
.from_ip
= decoder
->ip
;
1188 if (decoder
->packet
.count
== 0) {
1189 decoder
->state
.to_ip
= 0;
1191 decoder
->state
.to_ip
= decoder
->last_ip
;
1192 decoder
->ip
= decoder
->last_ip
;
1198 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1199 uint64_t to_ip
= decoder
->ip
+ intel_pt_insn
.length
+
1202 if (decoder
->pgd_ip
&&
1203 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1204 decoder
->pgd_ip(to_ip
, decoder
->data
)) {
1205 /* Conditional branch leaving filter region */
1206 decoder
->pge
= false;
1207 decoder
->continuous_period
= false;
1208 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1209 decoder
->ip
= to_ip
;
1210 decoder
->state
.from_ip
= decoder
->ip
;
1211 decoder
->state
.to_ip
= 0;
1214 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1216 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1220 return intel_pt_bug(decoder
);
1223 static int intel_pt_walk_tnt(struct intel_pt_decoder
*decoder
)
1225 struct intel_pt_insn intel_pt_insn
;
1229 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1230 if (err
== INTEL_PT_RETURN
)
1235 if (intel_pt_insn
.op
== INTEL_PT_OP_RET
) {
1236 if (!decoder
->return_compression
) {
1237 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1239 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1242 if (!decoder
->ret_addr
) {
1243 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1245 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1248 if (!(decoder
->tnt
.payload
& BIT63
)) {
1249 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1251 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1254 decoder
->tnt
.count
-= 1;
1255 if (!decoder
->tnt
.count
)
1256 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1257 decoder
->tnt
.payload
<<= 1;
1258 decoder
->state
.from_ip
= decoder
->ip
;
1259 decoder
->ip
= decoder
->ret_addr
;
1260 decoder
->state
.to_ip
= decoder
->ip
;
1264 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1265 /* Handle deferred TIPs */
1266 err
= intel_pt_get_next_packet(decoder
);
1269 if (decoder
->packet
.type
!= INTEL_PT_TIP
||
1270 decoder
->packet
.count
== 0) {
1271 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1273 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1274 decoder
->pkt_step
= 0;
1277 intel_pt_set_last_ip(decoder
);
1278 decoder
->state
.from_ip
= decoder
->ip
;
1279 decoder
->state
.to_ip
= decoder
->last_ip
;
1280 decoder
->ip
= decoder
->last_ip
;
1284 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1285 decoder
->tnt
.count
-= 1;
1286 if (!decoder
->tnt
.count
)
1287 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1288 if (decoder
->tnt
.payload
& BIT63
) {
1289 decoder
->tnt
.payload
<<= 1;
1290 decoder
->state
.from_ip
= decoder
->ip
;
1291 decoder
->ip
+= intel_pt_insn
.length
+
1293 decoder
->state
.to_ip
= decoder
->ip
;
1296 /* Instruction sample for a non-taken branch */
1297 if (decoder
->state
.type
& INTEL_PT_INSTRUCTION
) {
1298 decoder
->tnt
.payload
<<= 1;
1299 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1300 decoder
->state
.from_ip
= decoder
->ip
;
1301 decoder
->state
.to_ip
= 0;
1302 decoder
->ip
+= intel_pt_insn
.length
;
1305 decoder
->ip
+= intel_pt_insn
.length
;
1306 if (!decoder
->tnt
.count
)
1308 decoder
->tnt
.payload
<<= 1;
1312 return intel_pt_bug(decoder
);
1316 static int intel_pt_mode_tsx(struct intel_pt_decoder
*decoder
, bool *no_tip
)
1318 unsigned int fup_tx_flags
;
1321 fup_tx_flags
= decoder
->packet
.payload
&
1322 (INTEL_PT_IN_TX
| INTEL_PT_ABORT_TX
);
1323 err
= intel_pt_get_next_packet(decoder
);
1326 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1327 decoder
->fup_tx_flags
= fup_tx_flags
;
1328 decoder
->set_fup_tx_flags
= true;
1329 if (!(decoder
->fup_tx_flags
& INTEL_PT_ABORT_TX
))
1332 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1334 intel_pt_update_in_tx(decoder
);
1339 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder
*decoder
)
1343 decoder
->have_tma
= false;
1345 if (decoder
->ref_timestamp
) {
1346 timestamp
= decoder
->packet
.payload
|
1347 (decoder
->ref_timestamp
& (0xffULL
<< 56));
1348 if (timestamp
< decoder
->ref_timestamp
) {
1349 if (decoder
->ref_timestamp
- timestamp
> (1ULL << 55))
1350 timestamp
+= (1ULL << 56);
1352 if (timestamp
- decoder
->ref_timestamp
> (1ULL << 55))
1353 timestamp
-= (1ULL << 56);
1355 decoder
->tsc_timestamp
= timestamp
;
1356 decoder
->timestamp
= timestamp
;
1357 decoder
->ref_timestamp
= 0;
1358 decoder
->timestamp_insn_cnt
= 0;
1359 } else if (decoder
->timestamp
) {
1360 timestamp
= decoder
->packet
.payload
|
1361 (decoder
->timestamp
& (0xffULL
<< 56));
1362 decoder
->tsc_timestamp
= timestamp
;
1363 if (timestamp
< decoder
->timestamp
&&
1364 decoder
->timestamp
- timestamp
< decoder
->tsc_slip
) {
1365 intel_pt_log_to("Suppressing backwards timestamp",
1367 timestamp
= decoder
->timestamp
;
1369 if (timestamp
< decoder
->timestamp
) {
1370 intel_pt_log_to("Wraparound timestamp", timestamp
);
1371 timestamp
+= (1ULL << 56);
1372 decoder
->tsc_timestamp
= timestamp
;
1374 decoder
->timestamp
= timestamp
;
1375 decoder
->timestamp_insn_cnt
= 0;
1378 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1379 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1380 decoder
->cycle_cnt
= 0;
1381 decoder
->have_calc_cyc_to_tsc
= false;
1382 intel_pt_calc_cyc_to_tsc(decoder
, false);
1385 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1388 static int intel_pt_overflow(struct intel_pt_decoder
*decoder
)
1390 intel_pt_log("ERROR: Buffer overflow\n");
1391 intel_pt_clear_tx_flags(decoder
);
1393 decoder
->timestamp_insn_cnt
= 0;
1394 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1395 decoder
->overflow
= true;
1399 static void intel_pt_calc_tma(struct intel_pt_decoder
*decoder
)
1401 uint32_t ctc
= decoder
->packet
.payload
;
1402 uint32_t fc
= decoder
->packet
.count
;
1403 uint32_t ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
1405 if (!decoder
->tsc_ctc_ratio_d
)
1408 decoder
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
1409 decoder
->ctc_timestamp
= decoder
->tsc_timestamp
- fc
;
1410 if (decoder
->tsc_ctc_mult
) {
1411 decoder
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
1413 decoder
->ctc_timestamp
-= multdiv(ctc_rem
,
1414 decoder
->tsc_ctc_ratio_n
,
1415 decoder
->tsc_ctc_ratio_d
);
1417 decoder
->ctc_delta
= 0;
1418 decoder
->have_tma
= true;
1419 decoder
->fixup_last_mtc
= true;
1420 intel_pt_log("CTC timestamp " x64_fmt
" last MTC %#x CTC rem %#x\n",
1421 decoder
->ctc_timestamp
, decoder
->last_mtc
, ctc_rem
);
1424 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder
*decoder
)
1427 uint32_t mtc
, mtc_delta
;
1429 if (!decoder
->have_tma
)
1432 mtc
= decoder
->packet
.payload
;
1434 if (decoder
->mtc_shift
> 8 && decoder
->fixup_last_mtc
) {
1435 decoder
->fixup_last_mtc
= false;
1436 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
1437 &decoder
->last_mtc
);
1440 if (mtc
> decoder
->last_mtc
)
1441 mtc_delta
= mtc
- decoder
->last_mtc
;
1443 mtc_delta
= mtc
+ 256 - decoder
->last_mtc
;
1445 decoder
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
1447 if (decoder
->tsc_ctc_mult
) {
1448 timestamp
= decoder
->ctc_timestamp
+
1449 decoder
->ctc_delta
* decoder
->tsc_ctc_mult
;
1451 timestamp
= decoder
->ctc_timestamp
+
1452 multdiv(decoder
->ctc_delta
,
1453 decoder
->tsc_ctc_ratio_n
,
1454 decoder
->tsc_ctc_ratio_d
);
1457 if (timestamp
< decoder
->timestamp
)
1458 intel_pt_log("Suppressing MTC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1459 timestamp
, decoder
->timestamp
);
1461 decoder
->timestamp
= timestamp
;
1463 decoder
->timestamp_insn_cnt
= 0;
1464 decoder
->last_mtc
= mtc
;
1466 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1467 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1468 decoder
->cycle_cnt
= 0;
1469 decoder
->have_calc_cyc_to_tsc
= false;
1470 intel_pt_calc_cyc_to_tsc(decoder
, true);
1474 static void intel_pt_calc_cbr(struct intel_pt_decoder
*decoder
)
1476 unsigned int cbr
= decoder
->packet
.payload
& 0xff;
1478 decoder
->cbr_payload
= decoder
->packet
.payload
;
1480 if (decoder
->cbr
== cbr
)
1484 decoder
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
1487 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder
*decoder
)
1489 uint64_t timestamp
= decoder
->cyc_ref_timestamp
;
1491 decoder
->have_cyc
= true;
1493 decoder
->cycle_cnt
+= decoder
->packet
.payload
;
1495 if (!decoder
->cyc_ref_timestamp
)
1498 if (decoder
->have_calc_cyc_to_tsc
)
1499 timestamp
+= decoder
->cycle_cnt
* decoder
->calc_cyc_to_tsc
;
1500 else if (decoder
->cbr
)
1501 timestamp
+= decoder
->cycle_cnt
* decoder
->cbr_cyc_to_tsc
;
1505 if (timestamp
< decoder
->timestamp
)
1506 intel_pt_log("Suppressing CYC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1507 timestamp
, decoder
->timestamp
);
1509 decoder
->timestamp
= timestamp
;
1511 decoder
->timestamp_insn_cnt
= 0;
1514 /* Walk PSB+ packets when already in sync. */
1515 static int intel_pt_walk_psbend(struct intel_pt_decoder
*decoder
)
1520 err
= intel_pt_get_next_packet(decoder
);
1524 switch (decoder
->packet
.type
) {
1525 case INTEL_PT_PSBEND
:
1528 case INTEL_PT_TIP_PGD
:
1529 case INTEL_PT_TIP_PGE
:
1532 case INTEL_PT_TRACESTOP
:
1535 case INTEL_PT_PTWRITE
:
1536 case INTEL_PT_PTWRITE_IP
:
1537 case INTEL_PT_EXSTOP
:
1538 case INTEL_PT_EXSTOP_IP
:
1539 case INTEL_PT_MWAIT
:
1542 decoder
->have_tma
= false;
1543 intel_pt_log("ERROR: Unexpected packet\n");
1547 return intel_pt_overflow(decoder
);
1550 intel_pt_calc_tsc_timestamp(decoder
);
1554 intel_pt_calc_tma(decoder
);
1558 intel_pt_calc_cbr(decoder
);
1561 case INTEL_PT_MODE_EXEC
:
1562 decoder
->exec_mode
= decoder
->packet
.payload
;
1566 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1570 decoder
->pge
= true;
1571 if (decoder
->packet
.count
)
1572 intel_pt_set_last_ip(decoder
);
1575 case INTEL_PT_MODE_TSX
:
1576 intel_pt_update_in_tx(decoder
);
1580 intel_pt_calc_mtc_timestamp(decoder
);
1581 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1582 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1595 static int intel_pt_walk_fup_tip(struct intel_pt_decoder
*decoder
)
1599 if (decoder
->tx_flags
& INTEL_PT_ABORT_TX
) {
1600 decoder
->tx_flags
= 0;
1601 decoder
->state
.flags
&= ~INTEL_PT_IN_TX
;
1602 decoder
->state
.flags
|= INTEL_PT_ABORT_TX
;
1604 decoder
->state
.flags
|= INTEL_PT_ASYNC
;
1608 err
= intel_pt_get_next_packet(decoder
);
1612 switch (decoder
->packet
.type
) {
1615 case INTEL_PT_TRACESTOP
:
1619 case INTEL_PT_MODE_TSX
:
1621 case INTEL_PT_PSBEND
:
1622 case INTEL_PT_PTWRITE
:
1623 case INTEL_PT_PTWRITE_IP
:
1624 case INTEL_PT_EXSTOP
:
1625 case INTEL_PT_EXSTOP_IP
:
1626 case INTEL_PT_MWAIT
:
1629 intel_pt_log("ERROR: Missing TIP after FUP\n");
1630 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1631 decoder
->pkt_step
= 0;
1635 intel_pt_calc_cbr(decoder
);
1639 return intel_pt_overflow(decoder
);
1641 case INTEL_PT_TIP_PGD
:
1642 decoder
->state
.from_ip
= decoder
->ip
;
1643 decoder
->state
.to_ip
= 0;
1644 if (decoder
->packet
.count
!= 0) {
1645 intel_pt_set_ip(decoder
);
1646 intel_pt_log("Omitting PGD ip " x64_fmt
"\n",
1649 decoder
->pge
= false;
1650 decoder
->continuous_period
= false;
1653 case INTEL_PT_TIP_PGE
:
1654 decoder
->pge
= true;
1655 intel_pt_log("Omitting PGE ip " x64_fmt
"\n",
1657 decoder
->state
.from_ip
= 0;
1658 if (decoder
->packet
.count
== 0) {
1659 decoder
->state
.to_ip
= 0;
1661 intel_pt_set_ip(decoder
);
1662 decoder
->state
.to_ip
= decoder
->ip
;
1667 decoder
->state
.from_ip
= decoder
->ip
;
1668 if (decoder
->packet
.count
== 0) {
1669 decoder
->state
.to_ip
= 0;
1671 intel_pt_set_ip(decoder
);
1672 decoder
->state
.to_ip
= decoder
->ip
;
1677 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1681 intel_pt_calc_mtc_timestamp(decoder
);
1682 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1683 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1687 intel_pt_calc_cyc_timestamp(decoder
);
1690 case INTEL_PT_MODE_EXEC
:
1691 decoder
->exec_mode
= decoder
->packet
.payload
;
1700 return intel_pt_bug(decoder
);
1705 static int intel_pt_walk_trace(struct intel_pt_decoder
*decoder
)
1707 bool no_tip
= false;
1711 err
= intel_pt_get_next_packet(decoder
);
1715 switch (decoder
->packet
.type
) {
1717 if (!decoder
->packet
.count
)
1719 decoder
->tnt
= decoder
->packet
;
1720 decoder
->pkt_state
= INTEL_PT_STATE_TNT
;
1721 err
= intel_pt_walk_tnt(decoder
);
1726 case INTEL_PT_TIP_PGD
:
1727 if (decoder
->packet
.count
!= 0)
1728 intel_pt_set_last_ip(decoder
);
1729 decoder
->pkt_state
= INTEL_PT_STATE_TIP_PGD
;
1730 return intel_pt_walk_tip(decoder
);
1732 case INTEL_PT_TIP_PGE
: {
1733 decoder
->pge
= true;
1734 if (decoder
->packet
.count
== 0) {
1735 intel_pt_log_at("Skipping zero TIP.PGE",
1739 intel_pt_set_ip(decoder
);
1740 decoder
->state
.from_ip
= 0;
1741 decoder
->state
.to_ip
= decoder
->ip
;
1746 return intel_pt_overflow(decoder
);
1749 if (decoder
->packet
.count
!= 0)
1750 intel_pt_set_last_ip(decoder
);
1751 decoder
->pkt_state
= INTEL_PT_STATE_TIP
;
1752 return intel_pt_walk_tip(decoder
);
1755 if (decoder
->packet
.count
== 0) {
1756 intel_pt_log_at("Skipping zero FUP",
1761 intel_pt_set_last_ip(decoder
);
1762 if (!decoder
->branch_enable
) {
1763 decoder
->ip
= decoder
->last_ip
;
1764 if (intel_pt_fup_event(decoder
))
1769 if (decoder
->set_fup_mwait
)
1771 err
= intel_pt_walk_fup(decoder
);
1772 if (err
!= -EAGAIN
) {
1776 decoder
->pkt_state
=
1777 INTEL_PT_STATE_FUP_NO_TIP
;
1779 decoder
->pkt_state
= INTEL_PT_STATE_FUP
;
1786 return intel_pt_walk_fup_tip(decoder
);
1788 case INTEL_PT_TRACESTOP
:
1789 decoder
->pge
= false;
1790 decoder
->continuous_period
= false;
1791 intel_pt_clear_tx_flags(decoder
);
1792 decoder
->have_tma
= false;
1796 decoder
->last_ip
= 0;
1797 decoder
->have_last_ip
= true;
1798 intel_pt_clear_stack(&decoder
->stack
);
1799 err
= intel_pt_walk_psbend(decoder
);
1807 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1811 intel_pt_calc_mtc_timestamp(decoder
);
1812 if (decoder
->period_type
!= INTEL_PT_PERIOD_MTC
)
1815 * Ensure that there has been an instruction since the
1818 if (!decoder
->mtc_insn
)
1820 decoder
->mtc_insn
= false;
1821 /* Ensure that there is a timestamp */
1822 if (!decoder
->timestamp
)
1824 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1825 decoder
->state
.from_ip
= decoder
->ip
;
1826 decoder
->state
.to_ip
= 0;
1827 decoder
->mtc_insn
= false;
1831 intel_pt_calc_tsc_timestamp(decoder
);
1835 intel_pt_calc_tma(decoder
);
1839 intel_pt_calc_cyc_timestamp(decoder
);
1843 intel_pt_calc_cbr(decoder
);
1844 if (!decoder
->branch_enable
&&
1845 decoder
->cbr
!= decoder
->cbr_seen
) {
1846 decoder
->cbr_seen
= decoder
->cbr
;
1847 decoder
->state
.type
= INTEL_PT_CBR_CHG
;
1848 decoder
->state
.from_ip
= decoder
->ip
;
1849 decoder
->state
.to_ip
= 0;
1850 decoder
->state
.cbr_payload
=
1851 decoder
->packet
.payload
;
1856 case INTEL_PT_MODE_EXEC
:
1857 decoder
->exec_mode
= decoder
->packet
.payload
;
1860 case INTEL_PT_MODE_TSX
:
1861 /* MODE_TSX need not be followed by FUP */
1862 if (!decoder
->pge
) {
1863 intel_pt_update_in_tx(decoder
);
1866 err
= intel_pt_mode_tsx(decoder
, &no_tip
);
1871 case INTEL_PT_BAD
: /* Does not happen */
1872 return intel_pt_bug(decoder
);
1874 case INTEL_PT_PSBEND
:
1880 case INTEL_PT_PTWRITE_IP
:
1881 decoder
->fup_ptw_payload
= decoder
->packet
.payload
;
1882 err
= intel_pt_get_next_packet(decoder
);
1885 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1886 decoder
->set_fup_ptw
= true;
1889 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1894 case INTEL_PT_PTWRITE
:
1895 decoder
->state
.type
= INTEL_PT_PTW
;
1896 decoder
->state
.from_ip
= decoder
->ip
;
1897 decoder
->state
.to_ip
= 0;
1898 decoder
->state
.ptw_payload
= decoder
->packet
.payload
;
1901 case INTEL_PT_MWAIT
:
1902 decoder
->fup_mwait_payload
= decoder
->packet
.payload
;
1903 decoder
->set_fup_mwait
= true;
1907 if (decoder
->set_fup_mwait
) {
1908 decoder
->fup_pwre_payload
=
1909 decoder
->packet
.payload
;
1910 decoder
->set_fup_pwre
= true;
1913 decoder
->state
.type
= INTEL_PT_PWR_ENTRY
;
1914 decoder
->state
.from_ip
= decoder
->ip
;
1915 decoder
->state
.to_ip
= 0;
1916 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
1919 case INTEL_PT_EXSTOP_IP
:
1920 err
= intel_pt_get_next_packet(decoder
);
1923 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1924 decoder
->set_fup_exstop
= true;
1927 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
1932 case INTEL_PT_EXSTOP
:
1933 decoder
->state
.type
= INTEL_PT_EX_STOP
;
1934 decoder
->state
.from_ip
= decoder
->ip
;
1935 decoder
->state
.to_ip
= 0;
1939 decoder
->state
.type
= INTEL_PT_PWR_EXIT
;
1940 decoder
->state
.from_ip
= decoder
->ip
;
1941 decoder
->state
.to_ip
= 0;
1942 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
1946 return intel_pt_bug(decoder
);
1951 static inline bool intel_pt_have_ip(struct intel_pt_decoder
*decoder
)
1953 return decoder
->packet
.count
&&
1954 (decoder
->have_last_ip
|| decoder
->packet
.count
== 3 ||
1955 decoder
->packet
.count
== 6);
1958 /* Walk PSB+ packets to get in sync. */
1959 static int intel_pt_walk_psb(struct intel_pt_decoder
*decoder
)
1964 err
= intel_pt_get_next_packet(decoder
);
1968 switch (decoder
->packet
.type
) {
1969 case INTEL_PT_TIP_PGD
:
1970 decoder
->continuous_period
= false;
1972 case INTEL_PT_TIP_PGE
:
1974 case INTEL_PT_PTWRITE
:
1975 case INTEL_PT_PTWRITE_IP
:
1976 case INTEL_PT_EXSTOP
:
1977 case INTEL_PT_EXSTOP_IP
:
1978 case INTEL_PT_MWAIT
:
1981 intel_pt_log("ERROR: Unexpected packet\n");
1985 decoder
->pge
= true;
1986 if (intel_pt_have_ip(decoder
)) {
1987 uint64_t current_ip
= decoder
->ip
;
1989 intel_pt_set_ip(decoder
);
1991 intel_pt_log_to("Setting IP",
1997 intel_pt_calc_mtc_timestamp(decoder
);
2001 intel_pt_calc_tsc_timestamp(decoder
);
2005 intel_pt_calc_tma(decoder
);
2009 intel_pt_calc_cyc_timestamp(decoder
);
2013 intel_pt_calc_cbr(decoder
);
2017 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2020 case INTEL_PT_MODE_EXEC
:
2021 decoder
->exec_mode
= decoder
->packet
.payload
;
2024 case INTEL_PT_MODE_TSX
:
2025 intel_pt_update_in_tx(decoder
);
2028 case INTEL_PT_TRACESTOP
:
2029 decoder
->pge
= false;
2030 decoder
->continuous_period
= false;
2031 intel_pt_clear_tx_flags(decoder
);
2035 decoder
->have_tma
= false;
2036 intel_pt_log("ERROR: Unexpected packet\n");
2038 decoder
->pkt_state
= INTEL_PT_STATE_ERR4
;
2040 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
2043 case INTEL_PT_BAD
: /* Does not happen */
2044 return intel_pt_bug(decoder
);
2047 return intel_pt_overflow(decoder
);
2049 case INTEL_PT_PSBEND
:
2062 static int intel_pt_walk_to_ip(struct intel_pt_decoder
*decoder
)
2067 err
= intel_pt_get_next_packet(decoder
);
2071 switch (decoder
->packet
.type
) {
2072 case INTEL_PT_TIP_PGD
:
2073 decoder
->continuous_period
= false;
2075 case INTEL_PT_TIP_PGE
:
2077 decoder
->pge
= decoder
->packet
.type
!= INTEL_PT_TIP_PGD
;
2078 if (intel_pt_have_ip(decoder
))
2079 intel_pt_set_ip(decoder
);
2085 if (intel_pt_have_ip(decoder
))
2086 intel_pt_set_ip(decoder
);
2092 intel_pt_calc_mtc_timestamp(decoder
);
2096 intel_pt_calc_tsc_timestamp(decoder
);
2100 intel_pt_calc_tma(decoder
);
2104 intel_pt_calc_cyc_timestamp(decoder
);
2108 intel_pt_calc_cbr(decoder
);
2112 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2115 case INTEL_PT_MODE_EXEC
:
2116 decoder
->exec_mode
= decoder
->packet
.payload
;
2119 case INTEL_PT_MODE_TSX
:
2120 intel_pt_update_in_tx(decoder
);
2124 return intel_pt_overflow(decoder
);
2126 case INTEL_PT_BAD
: /* Does not happen */
2127 return intel_pt_bug(decoder
);
2129 case INTEL_PT_TRACESTOP
:
2130 decoder
->pge
= false;
2131 decoder
->continuous_period
= false;
2132 intel_pt_clear_tx_flags(decoder
);
2133 decoder
->have_tma
= false;
2137 decoder
->last_ip
= 0;
2138 decoder
->have_last_ip
= true;
2139 intel_pt_clear_stack(&decoder
->stack
);
2140 err
= intel_pt_walk_psb(decoder
);
2144 /* Do not have a sample */
2145 decoder
->state
.type
= 0;
2151 case INTEL_PT_PSBEND
:
2155 case INTEL_PT_PTWRITE
:
2156 case INTEL_PT_PTWRITE_IP
:
2157 case INTEL_PT_EXSTOP
:
2158 case INTEL_PT_EXSTOP_IP
:
2159 case INTEL_PT_MWAIT
:
2168 static int intel_pt_sync_ip(struct intel_pt_decoder
*decoder
)
2172 decoder
->set_fup_tx_flags
= false;
2173 decoder
->set_fup_ptw
= false;
2174 decoder
->set_fup_mwait
= false;
2175 decoder
->set_fup_pwre
= false;
2176 decoder
->set_fup_exstop
= false;
2178 if (!decoder
->branch_enable
) {
2179 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2180 decoder
->overflow
= false;
2181 decoder
->state
.type
= 0; /* Do not have a sample */
2185 intel_pt_log("Scanning for full IP\n");
2186 err
= intel_pt_walk_to_ip(decoder
);
2190 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2191 decoder
->overflow
= false;
2193 decoder
->state
.from_ip
= 0;
2194 decoder
->state
.to_ip
= decoder
->ip
;
2195 intel_pt_log_to("Setting IP", decoder
->ip
);
2200 static int intel_pt_part_psb(struct intel_pt_decoder
*decoder
)
2202 const unsigned char *end
= decoder
->buf
+ decoder
->len
;
2205 for (i
= INTEL_PT_PSB_LEN
- 1; i
; i
--) {
2206 if (i
> decoder
->len
)
2208 if (!memcmp(end
- i
, INTEL_PT_PSB_STR
, i
))
2214 static int intel_pt_rest_psb(struct intel_pt_decoder
*decoder
, int part_psb
)
2216 size_t rest_psb
= INTEL_PT_PSB_LEN
- part_psb
;
2217 const char *psb
= INTEL_PT_PSB_STR
;
2219 if (rest_psb
> decoder
->len
||
2220 memcmp(decoder
->buf
, psb
+ part_psb
, rest_psb
))
2226 static int intel_pt_get_split_psb(struct intel_pt_decoder
*decoder
,
2231 decoder
->pos
+= decoder
->len
;
2234 ret
= intel_pt_get_next_data(decoder
);
2238 rest_psb
= intel_pt_rest_psb(decoder
, part_psb
);
2242 decoder
->pos
-= part_psb
;
2243 decoder
->next_buf
= decoder
->buf
+ rest_psb
;
2244 decoder
->next_len
= decoder
->len
- rest_psb
;
2245 memcpy(decoder
->temp_buf
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2246 decoder
->buf
= decoder
->temp_buf
;
2247 decoder
->len
= INTEL_PT_PSB_LEN
;
2252 static int intel_pt_scan_for_psb(struct intel_pt_decoder
*decoder
)
2254 unsigned char *next
;
2257 intel_pt_log("Scanning for PSB\n");
2259 if (!decoder
->len
) {
2260 ret
= intel_pt_get_next_data(decoder
);
2265 next
= memmem(decoder
->buf
, decoder
->len
, INTEL_PT_PSB_STR
,
2270 part_psb
= intel_pt_part_psb(decoder
);
2272 ret
= intel_pt_get_split_psb(decoder
, part_psb
);
2276 decoder
->pos
+= decoder
->len
;
2282 decoder
->pkt_step
= next
- decoder
->buf
;
2283 return intel_pt_get_next_packet(decoder
);
2287 static int intel_pt_sync(struct intel_pt_decoder
*decoder
)
2291 decoder
->pge
= false;
2292 decoder
->continuous_period
= false;
2293 decoder
->have_last_ip
= false;
2294 decoder
->last_ip
= 0;
2296 intel_pt_clear_stack(&decoder
->stack
);
2298 err
= intel_pt_scan_for_psb(decoder
);
2302 decoder
->have_last_ip
= true;
2303 decoder
->pkt_state
= INTEL_PT_STATE_NO_IP
;
2305 err
= intel_pt_walk_psb(decoder
);
2310 decoder
->state
.type
= 0; /* Do not have a sample */
2311 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2313 return intel_pt_sync_ip(decoder
);
2319 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder
*decoder
)
2321 uint64_t est
= decoder
->sample_insn_cnt
<< 1;
2323 if (!decoder
->cbr
|| !decoder
->max_non_turbo_ratio
)
2326 est
*= decoder
->max_non_turbo_ratio
;
2327 est
/= decoder
->cbr
;
2329 return decoder
->sample_timestamp
+ est
;
2332 const struct intel_pt_state
*intel_pt_decode(struct intel_pt_decoder
*decoder
)
2337 decoder
->state
.type
= INTEL_PT_BRANCH
;
2338 decoder
->state
.flags
= 0;
2340 switch (decoder
->pkt_state
) {
2341 case INTEL_PT_STATE_NO_PSB
:
2342 err
= intel_pt_sync(decoder
);
2344 case INTEL_PT_STATE_NO_IP
:
2345 decoder
->have_last_ip
= false;
2346 decoder
->last_ip
= 0;
2349 case INTEL_PT_STATE_ERR_RESYNC
:
2350 err
= intel_pt_sync_ip(decoder
);
2352 case INTEL_PT_STATE_IN_SYNC
:
2353 err
= intel_pt_walk_trace(decoder
);
2355 case INTEL_PT_STATE_TNT
:
2356 err
= intel_pt_walk_tnt(decoder
);
2358 err
= intel_pt_walk_trace(decoder
);
2360 case INTEL_PT_STATE_TIP
:
2361 case INTEL_PT_STATE_TIP_PGD
:
2362 err
= intel_pt_walk_tip(decoder
);
2364 case INTEL_PT_STATE_FUP
:
2365 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2366 err
= intel_pt_walk_fup(decoder
);
2368 err
= intel_pt_walk_fup_tip(decoder
);
2370 decoder
->pkt_state
= INTEL_PT_STATE_FUP
;
2372 case INTEL_PT_STATE_FUP_NO_TIP
:
2373 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2374 err
= intel_pt_walk_fup(decoder
);
2376 err
= intel_pt_walk_trace(decoder
);
2379 err
= intel_pt_bug(decoder
);
2382 } while (err
== -ENOLINK
);
2385 decoder
->state
.err
= intel_pt_ext_err(err
);
2386 decoder
->state
.from_ip
= decoder
->ip
;
2387 decoder
->sample_timestamp
= decoder
->timestamp
;
2388 decoder
->sample_insn_cnt
= decoder
->timestamp_insn_cnt
;
2390 decoder
->state
.err
= 0;
2391 if (decoder
->cbr
!= decoder
->cbr_seen
&& decoder
->state
.type
) {
2392 decoder
->cbr_seen
= decoder
->cbr
;
2393 decoder
->state
.type
|= INTEL_PT_CBR_CHG
;
2394 decoder
->state
.cbr_payload
= decoder
->cbr_payload
;
2396 if (intel_pt_sample_time(decoder
->pkt_state
)) {
2397 decoder
->sample_timestamp
= decoder
->timestamp
;
2398 decoder
->sample_insn_cnt
= decoder
->timestamp_insn_cnt
;
2402 decoder
->state
.timestamp
= decoder
->sample_timestamp
;
2403 decoder
->state
.est_timestamp
= intel_pt_est_timestamp(decoder
);
2404 decoder
->state
.cr3
= decoder
->cr3
;
2405 decoder
->state
.tot_insn_cnt
= decoder
->tot_insn_cnt
;
2407 return &decoder
->state
;
2411 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2412 * @buf: pointer to buffer pointer
2413 * @len: size of buffer
2415 * Updates the buffer pointer to point to the start of the next PSB packet if
2416 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2417 * @len is adjusted accordingly.
2419 * Return: %true if a PSB packet is found, %false otherwise.
2421 static bool intel_pt_next_psb(unsigned char **buf
, size_t *len
)
2423 unsigned char *next
;
2425 next
= memmem(*buf
, *len
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2427 *len
-= next
- *buf
;
2435 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2437 * @buf: pointer to buffer pointer
2438 * @len: size of buffer
2440 * Updates the buffer pointer to point to the start of the following PSB packet
2441 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2442 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2444 * Return: %true if a PSB packet is found, %false otherwise.
2446 static bool intel_pt_step_psb(unsigned char **buf
, size_t *len
)
2448 unsigned char *next
;
2453 next
= memmem(*buf
+ 1, *len
- 1, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2455 *len
-= next
- *buf
;
2463 * intel_pt_last_psb - find the last PSB packet in a buffer.
2465 * @len: size of buffer
2467 * This function finds the last PSB in a buffer.
2469 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2471 static unsigned char *intel_pt_last_psb(unsigned char *buf
, size_t len
)
2473 const char *n
= INTEL_PT_PSB_STR
;
2477 if (len
< INTEL_PT_PSB_LEN
)
2480 k
= len
- INTEL_PT_PSB_LEN
+ 1;
2482 p
= memrchr(buf
, n
[0], k
);
2485 if (!memcmp(p
+ 1, n
+ 1, INTEL_PT_PSB_LEN
- 1))
2494 * intel_pt_next_tsc - find and return next TSC.
2496 * @len: size of buffer
2497 * @tsc: TSC value returned
2498 * @rem: returns remaining size when TSC is found
2500 * Find a TSC packet in @buf and return the TSC value. This function assumes
2501 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2502 * PSBEND packet is found.
2504 * Return: %true if TSC is found, false otherwise.
2506 static bool intel_pt_next_tsc(unsigned char *buf
, size_t len
, uint64_t *tsc
,
2509 struct intel_pt_pkt packet
;
2513 ret
= intel_pt_get_packet(buf
, len
, &packet
);
2516 if (packet
.type
== INTEL_PT_TSC
) {
2517 *tsc
= packet
.payload
;
2521 if (packet
.type
== INTEL_PT_PSBEND
)
2530 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2531 * @tsc1: first TSC to compare
2532 * @tsc2: second TSC to compare
2534 * This function compares 7-byte TSC values allowing for the possibility that
2535 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2536 * around so for that purpose this function assumes the absolute difference is
2537 * less than half the maximum difference.
2539 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2542 static int intel_pt_tsc_cmp(uint64_t tsc1
, uint64_t tsc2
)
2544 const uint64_t halfway
= (1ULL << 55);
2550 if (tsc2
- tsc1
< halfway
)
2555 if (tsc1
- tsc2
< halfway
)
2563 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2565 * @buf_a: first buffer
2566 * @len_a: size of first buffer
2567 * @buf_b: second buffer
2568 * @len_b: size of second buffer
2569 * @consecutive: returns true if there is data in buf_b that is consecutive
2572 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2573 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2574 * walk forward in @buf_b until a later TSC is found. A precondition is that
2575 * @buf_a and @buf_b are positioned at a PSB.
2577 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2578 * @buf_b + @len_b if there is no non-overlapped data.
2580 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a
,
2582 unsigned char *buf_b
,
2583 size_t len_b
, bool *consecutive
)
2585 uint64_t tsc_a
, tsc_b
;
2587 size_t len
, rem_a
, rem_b
;
2589 p
= intel_pt_last_psb(buf_a
, len_a
);
2591 return buf_b
; /* No PSB in buf_a => no overlap */
2593 len
= len_a
- (p
- buf_a
);
2594 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
)) {
2595 /* The last PSB+ in buf_a is incomplete, so go back one more */
2597 p
= intel_pt_last_psb(buf_a
, len_a
);
2599 return buf_b
; /* No full PSB+ => assume no overlap */
2600 len
= len_a
- (p
- buf_a
);
2601 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
))
2602 return buf_b
; /* No TSC in buf_a => assume no overlap */
2606 /* Ignore PSB+ with no TSC */
2607 if (intel_pt_next_tsc(buf_b
, len_b
, &tsc_b
, &rem_b
)) {
2608 int cmp
= intel_pt_tsc_cmp(tsc_a
, tsc_b
);
2610 /* Same TSC, so buffers are consecutive */
2611 if (!cmp
&& rem_b
>= rem_a
) {
2612 *consecutive
= true;
2613 return buf_b
+ len_b
- (rem_b
- rem_a
);
2616 return buf_b
; /* tsc_a < tsc_b => no overlap */
2619 if (!intel_pt_step_psb(&buf_b
, &len_b
))
2620 return buf_b
+ len_b
; /* No PSB in buf_b => no data */
2625 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2626 * @buf_a: first buffer
2627 * @len_a: size of first buffer
2628 * @buf_b: second buffer
2629 * @len_b: size of second buffer
2630 * @have_tsc: can use TSC packets to detect overlap
2631 * @consecutive: returns true if there is data in buf_b that is consecutive
2634 * When trace samples or snapshots are recorded there is the possibility that
2635 * the data overlaps. Note that, for the purposes of decoding, data is only
2636 * useful if it begins with a PSB packet.
2638 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2639 * @buf_b + @len_b if there is no non-overlapped data.
2641 unsigned char *intel_pt_find_overlap(unsigned char *buf_a
, size_t len_a
,
2642 unsigned char *buf_b
, size_t len_b
,
2643 bool have_tsc
, bool *consecutive
)
2645 unsigned char *found
;
2647 /* Buffer 'b' must start at PSB so throw away everything before that */
2648 if (!intel_pt_next_psb(&buf_b
, &len_b
))
2649 return buf_b
+ len_b
; /* No PSB */
2651 if (!intel_pt_next_psb(&buf_a
, &len_a
))
2652 return buf_b
; /* No overlap */
2655 found
= intel_pt_find_overlap_tsc(buf_a
, len_a
, buf_b
, len_b
,
2662 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2663 * we can ignore the first part of buffer 'a'.
2665 while (len_b
< len_a
) {
2666 if (!intel_pt_step_psb(&buf_a
, &len_a
))
2667 return buf_b
; /* No overlap */
2670 /* Now len_b >= len_a */
2672 /* Potential overlap so check the bytes */
2673 found
= memmem(buf_a
, len_a
, buf_b
, len_a
);
2675 *consecutive
= true;
2676 return buf_b
+ len_a
;
2679 /* Try again at next PSB in buffer 'a' */
2680 if (!intel_pt_step_psb(&buf_a
, &len_a
))
2681 return buf_b
; /* No overlap */