1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt_decoder.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
16 #include <linux/compiler.h>
17 #include <linux/string.h>
18 #include <linux/zalloc.h>
20 #include "../auxtrace.h"
22 #include "intel-pt-insn-decoder.h"
23 #include "intel-pt-pkt-decoder.h"
24 #include "intel-pt-decoder.h"
25 #include "intel-pt-log.h"
27 #define INTEL_PT_BLK_SIZE 1024
29 #define BIT63 (((uint64_t)1 << 63))
31 #define INTEL_PT_RETURN 1
33 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
34 #define INTEL_PT_MAX_LOOPS 10000
37 struct intel_pt_blk
*prev
;
38 uint64_t ip
[INTEL_PT_BLK_SIZE
];
41 struct intel_pt_stack
{
42 struct intel_pt_blk
*blk
;
43 struct intel_pt_blk
*spare
;
47 enum intel_pt_pkt_state
{
48 INTEL_PT_STATE_NO_PSB
,
50 INTEL_PT_STATE_ERR_RESYNC
,
51 INTEL_PT_STATE_IN_SYNC
,
52 INTEL_PT_STATE_TNT_CONT
,
55 INTEL_PT_STATE_TIP_PGD
,
57 INTEL_PT_STATE_FUP_NO_TIP
,
58 INTEL_PT_STATE_RESAMPLE
,
61 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state
)
64 case INTEL_PT_STATE_NO_PSB
:
65 case INTEL_PT_STATE_NO_IP
:
66 case INTEL_PT_STATE_ERR_RESYNC
:
67 case INTEL_PT_STATE_IN_SYNC
:
68 case INTEL_PT_STATE_TNT_CONT
:
69 case INTEL_PT_STATE_RESAMPLE
:
71 case INTEL_PT_STATE_TNT
:
72 case INTEL_PT_STATE_TIP
:
73 case INTEL_PT_STATE_TIP_PGD
:
74 case INTEL_PT_STATE_FUP
:
75 case INTEL_PT_STATE_FUP_NO_TIP
:
82 #ifdef INTEL_PT_STRICT
83 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
84 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
85 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
86 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
88 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
89 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
90 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
91 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
94 struct intel_pt_decoder
{
95 int (*get_trace
)(struct intel_pt_buffer
*buffer
, void *data
);
96 int (*walk_insn
)(struct intel_pt_insn
*intel_pt_insn
,
97 uint64_t *insn_cnt_ptr
, uint64_t *ip
, uint64_t to_ip
,
98 uint64_t max_insn_cnt
, void *data
);
99 bool (*pgd_ip
)(uint64_t ip
, void *data
);
100 int (*lookahead
)(void *data
, intel_pt_lookahead_cb_t cb
, void *cb_data
);
102 struct intel_pt_state state
;
103 const unsigned char *buf
;
105 bool return_compression
;
117 enum intel_pt_param_flags flags
;
123 uint64_t tsc_timestamp
;
124 uint64_t ref_timestamp
;
125 uint64_t buf_timestamp
;
126 uint64_t sample_timestamp
;
128 uint64_t ctc_timestamp
;
131 uint64_t cyc_ref_timestamp
;
133 uint32_t tsc_ctc_ratio_n
;
134 uint32_t tsc_ctc_ratio_d
;
135 uint32_t tsc_ctc_mult
;
137 uint32_t ctc_rem_mask
;
139 struct intel_pt_stack stack
;
140 enum intel_pt_pkt_state pkt_state
;
141 enum intel_pt_pkt_ctx pkt_ctx
;
142 enum intel_pt_pkt_ctx prev_pkt_ctx
;
143 enum intel_pt_blk_type blk_type
;
145 struct intel_pt_pkt packet
;
146 struct intel_pt_pkt tnt
;
149 int last_packet_type
;
151 unsigned int cbr_seen
;
152 unsigned int max_non_turbo_ratio
;
153 double max_non_turbo_ratio_fp
;
154 double cbr_cyc_to_tsc
;
155 double calc_cyc_to_tsc
;
156 bool have_calc_cyc_to_tsc
;
158 unsigned int insn_bytes
;
160 enum intel_pt_period_type period_type
;
161 uint64_t tot_insn_cnt
;
162 uint64_t period_insn_cnt
;
163 uint64_t period_mask
;
164 uint64_t period_ticks
;
165 uint64_t last_masked_timestamp
;
166 uint64_t tot_cyc_cnt
;
167 uint64_t sample_tot_cyc_cnt
;
168 uint64_t base_cyc_cnt
;
169 uint64_t cyc_cnt_timestamp
;
171 bool continuous_period
;
173 bool set_fup_tx_flags
;
180 unsigned int fup_tx_flags
;
181 unsigned int tx_flags
;
182 uint64_t fup_ptw_payload
;
183 uint64_t fup_mwait_payload
;
184 uint64_t fup_pwre_payload
;
185 uint64_t cbr_payload
;
186 uint64_t timestamp_insn_cnt
;
187 uint64_t sample_insn_cnt
;
192 const unsigned char *next_buf
;
194 unsigned char temp_buf
[INTEL_PT_PKT_MAX_SZ
];
197 static uint64_t intel_pt_lower_power_of_2(uint64_t x
)
201 for (i
= 0; x
!= 1; i
++)
207 static void intel_pt_setup_period(struct intel_pt_decoder
*decoder
)
209 if (decoder
->period_type
== INTEL_PT_PERIOD_TICKS
) {
212 period
= intel_pt_lower_power_of_2(decoder
->period
);
213 decoder
->period_mask
= ~(period
- 1);
214 decoder
->period_ticks
= period
;
218 static uint64_t multdiv(uint64_t t
, uint32_t n
, uint32_t d
)
222 return (t
/ d
) * n
+ ((t
% d
) * n
) / d
;
225 struct intel_pt_decoder
*intel_pt_decoder_new(struct intel_pt_params
*params
)
227 struct intel_pt_decoder
*decoder
;
229 if (!params
->get_trace
|| !params
->walk_insn
)
232 decoder
= zalloc(sizeof(struct intel_pt_decoder
));
236 decoder
->get_trace
= params
->get_trace
;
237 decoder
->walk_insn
= params
->walk_insn
;
238 decoder
->pgd_ip
= params
->pgd_ip
;
239 decoder
->lookahead
= params
->lookahead
;
240 decoder
->data
= params
->data
;
241 decoder
->return_compression
= params
->return_compression
;
242 decoder
->branch_enable
= params
->branch_enable
;
243 decoder
->hop
= params
->quick
>= 1;
244 decoder
->leap
= params
->quick
>= 2;
246 decoder
->flags
= params
->flags
;
248 decoder
->period
= params
->period
;
249 decoder
->period_type
= params
->period_type
;
251 decoder
->max_non_turbo_ratio
= params
->max_non_turbo_ratio
;
252 decoder
->max_non_turbo_ratio_fp
= params
->max_non_turbo_ratio
;
254 intel_pt_setup_period(decoder
);
256 decoder
->mtc_shift
= params
->mtc_period
;
257 decoder
->ctc_rem_mask
= (1 << decoder
->mtc_shift
) - 1;
259 decoder
->tsc_ctc_ratio_n
= params
->tsc_ctc_ratio_n
;
260 decoder
->tsc_ctc_ratio_d
= params
->tsc_ctc_ratio_d
;
262 if (!decoder
->tsc_ctc_ratio_n
)
263 decoder
->tsc_ctc_ratio_d
= 0;
265 if (decoder
->tsc_ctc_ratio_d
) {
266 if (!(decoder
->tsc_ctc_ratio_n
% decoder
->tsc_ctc_ratio_d
))
267 decoder
->tsc_ctc_mult
= decoder
->tsc_ctc_ratio_n
/
268 decoder
->tsc_ctc_ratio_d
;
272 * A TSC packet can slip past MTC packets so that the timestamp appears
273 * to go backwards. One estimate is that can be up to about 40 CPU
274 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
275 * slippage an order of magnitude more to be on the safe side.
277 decoder
->tsc_slip
= 0x10000;
279 intel_pt_log("timestamp: mtc_shift %u\n", decoder
->mtc_shift
);
280 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder
->tsc_ctc_ratio_n
);
281 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder
->tsc_ctc_ratio_d
);
282 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder
->tsc_ctc_mult
);
283 intel_pt_log("timestamp: tsc_slip %#x\n", decoder
->tsc_slip
);
286 intel_pt_log("Hop mode: decoding FUP and TIPs, but not TNT\n");
291 static void intel_pt_pop_blk(struct intel_pt_stack
*stack
)
293 struct intel_pt_blk
*blk
= stack
->blk
;
295 stack
->blk
= blk
->prev
;
302 static uint64_t intel_pt_pop(struct intel_pt_stack
*stack
)
307 intel_pt_pop_blk(stack
);
310 stack
->pos
= INTEL_PT_BLK_SIZE
;
312 return stack
->blk
->ip
[--stack
->pos
];
315 static int intel_pt_alloc_blk(struct intel_pt_stack
*stack
)
317 struct intel_pt_blk
*blk
;
323 blk
= malloc(sizeof(struct intel_pt_blk
));
328 blk
->prev
= stack
->blk
;
334 static int intel_pt_push(struct intel_pt_stack
*stack
, uint64_t ip
)
338 if (!stack
->blk
|| stack
->pos
== INTEL_PT_BLK_SIZE
) {
339 err
= intel_pt_alloc_blk(stack
);
344 stack
->blk
->ip
[stack
->pos
++] = ip
;
348 static void intel_pt_clear_stack(struct intel_pt_stack
*stack
)
351 intel_pt_pop_blk(stack
);
355 static void intel_pt_free_stack(struct intel_pt_stack
*stack
)
357 intel_pt_clear_stack(stack
);
359 zfree(&stack
->spare
);
362 void intel_pt_decoder_free(struct intel_pt_decoder
*decoder
)
364 intel_pt_free_stack(&decoder
->stack
);
368 static int intel_pt_ext_err(int code
)
372 return INTEL_PT_ERR_NOMEM
;
374 return INTEL_PT_ERR_INTERN
;
376 return INTEL_PT_ERR_BADPKT
;
378 return INTEL_PT_ERR_NODATA
;
380 return INTEL_PT_ERR_NOINSN
;
382 return INTEL_PT_ERR_MISMAT
;
384 return INTEL_PT_ERR_OVR
;
386 return INTEL_PT_ERR_LOST
;
388 return INTEL_PT_ERR_NELOOP
;
390 return INTEL_PT_ERR_UNK
;
394 static const char *intel_pt_err_msgs
[] = {
395 [INTEL_PT_ERR_NOMEM
] = "Memory allocation failed",
396 [INTEL_PT_ERR_INTERN
] = "Internal error",
397 [INTEL_PT_ERR_BADPKT
] = "Bad packet",
398 [INTEL_PT_ERR_NODATA
] = "No more data",
399 [INTEL_PT_ERR_NOINSN
] = "Failed to get instruction",
400 [INTEL_PT_ERR_MISMAT
] = "Trace doesn't match instruction",
401 [INTEL_PT_ERR_OVR
] = "Overflow packet",
402 [INTEL_PT_ERR_LOST
] = "Lost trace data",
403 [INTEL_PT_ERR_UNK
] = "Unknown error!",
404 [INTEL_PT_ERR_NELOOP
] = "Never-ending loop",
407 int intel_pt__strerror(int code
, char *buf
, size_t buflen
)
409 if (code
< 1 || code
>= INTEL_PT_ERR_MAX
)
410 code
= INTEL_PT_ERR_UNK
;
411 strlcpy(buf
, intel_pt_err_msgs
[code
], buflen
);
415 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt
*packet
,
420 switch (packet
->count
) {
422 ip
= (last_ip
& (uint64_t)0xffffffffffff0000ULL
) |
426 ip
= (last_ip
& (uint64_t)0xffffffff00000000ULL
) |
430 ip
= packet
->payload
;
431 /* Sign-extend 6-byte ip */
432 if (ip
& (uint64_t)0x800000000000ULL
)
433 ip
|= (uint64_t)0xffff000000000000ULL
;
436 ip
= (last_ip
& (uint64_t)0xffff000000000000ULL
) |
440 ip
= packet
->payload
;
449 static inline void intel_pt_set_last_ip(struct intel_pt_decoder
*decoder
)
451 decoder
->last_ip
= intel_pt_calc_ip(&decoder
->packet
, decoder
->last_ip
);
452 decoder
->have_last_ip
= true;
455 static inline void intel_pt_set_ip(struct intel_pt_decoder
*decoder
)
457 intel_pt_set_last_ip(decoder
);
458 decoder
->ip
= decoder
->last_ip
;
461 static void intel_pt_decoder_log_packet(struct intel_pt_decoder
*decoder
)
463 intel_pt_log_packet(&decoder
->packet
, decoder
->pkt_len
, decoder
->pos
,
467 static int intel_pt_bug(struct intel_pt_decoder
*decoder
)
469 intel_pt_log("ERROR: Internal error\n");
470 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
474 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder
*decoder
)
476 decoder
->tx_flags
= 0;
479 static inline void intel_pt_update_in_tx(struct intel_pt_decoder
*decoder
)
481 decoder
->tx_flags
= decoder
->packet
.payload
& INTEL_PT_IN_TX
;
484 static int intel_pt_bad_packet(struct intel_pt_decoder
*decoder
)
486 intel_pt_clear_tx_flags(decoder
);
487 decoder
->have_tma
= false;
488 decoder
->pkt_len
= 1;
489 decoder
->pkt_step
= 1;
490 intel_pt_decoder_log_packet(decoder
);
491 if (decoder
->pkt_state
!= INTEL_PT_STATE_NO_PSB
) {
492 intel_pt_log("ERROR: Bad packet\n");
493 decoder
->pkt_state
= INTEL_PT_STATE_ERR1
;
498 static inline void intel_pt_update_sample_time(struct intel_pt_decoder
*decoder
)
500 decoder
->sample_timestamp
= decoder
->timestamp
;
501 decoder
->sample_insn_cnt
= decoder
->timestamp_insn_cnt
;
504 static void intel_pt_reposition(struct intel_pt_decoder
*decoder
)
507 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
508 decoder
->timestamp
= 0;
509 decoder
->have_tma
= false;
512 static int intel_pt_get_data(struct intel_pt_decoder
*decoder
, bool reposition
)
514 struct intel_pt_buffer buffer
= { .buf
= 0, };
517 decoder
->pkt_step
= 0;
519 intel_pt_log("Getting more data\n");
520 ret
= decoder
->get_trace(&buffer
, decoder
->data
);
523 decoder
->buf
= buffer
.buf
;
524 decoder
->len
= buffer
.len
;
526 intel_pt_log("No more data\n");
529 decoder
->buf_timestamp
= buffer
.ref_timestamp
;
530 if (!buffer
.consecutive
|| reposition
) {
531 intel_pt_reposition(decoder
);
532 decoder
->ref_timestamp
= buffer
.ref_timestamp
;
533 decoder
->state
.trace_nr
= buffer
.trace_nr
;
534 intel_pt_log("Reference timestamp 0x%" PRIx64
"\n",
535 decoder
->ref_timestamp
);
542 static int intel_pt_get_next_data(struct intel_pt_decoder
*decoder
,
545 if (!decoder
->next_buf
)
546 return intel_pt_get_data(decoder
, reposition
);
548 decoder
->buf
= decoder
->next_buf
;
549 decoder
->len
= decoder
->next_len
;
550 decoder
->next_buf
= 0;
551 decoder
->next_len
= 0;
555 static int intel_pt_get_split_packet(struct intel_pt_decoder
*decoder
)
557 unsigned char *buf
= decoder
->temp_buf
;
558 size_t old_len
, len
, n
;
561 old_len
= decoder
->len
;
563 memcpy(buf
, decoder
->buf
, len
);
565 ret
= intel_pt_get_data(decoder
, false);
567 decoder
->pos
+= old_len
;
568 return ret
< 0 ? ret
: -EINVAL
;
571 n
= INTEL_PT_PKT_MAX_SZ
- len
;
572 if (n
> decoder
->len
)
574 memcpy(buf
+ len
, decoder
->buf
, n
);
577 decoder
->prev_pkt_ctx
= decoder
->pkt_ctx
;
578 ret
= intel_pt_get_packet(buf
, len
, &decoder
->packet
, &decoder
->pkt_ctx
);
579 if (ret
< (int)old_len
) {
580 decoder
->next_buf
= decoder
->buf
;
581 decoder
->next_len
= decoder
->len
;
583 decoder
->len
= old_len
;
584 return intel_pt_bad_packet(decoder
);
587 decoder
->next_buf
= decoder
->buf
+ (ret
- old_len
);
588 decoder
->next_len
= decoder
->len
- (ret
- old_len
);
596 struct intel_pt_pkt_info
{
597 struct intel_pt_decoder
*decoder
;
598 struct intel_pt_pkt packet
;
601 int last_packet_type
;
605 typedef int (*intel_pt_pkt_cb_t
)(struct intel_pt_pkt_info
*pkt_info
);
607 /* Lookahead packets in current buffer */
608 static int intel_pt_pkt_lookahead(struct intel_pt_decoder
*decoder
,
609 intel_pt_pkt_cb_t cb
, void *data
)
611 struct intel_pt_pkt_info pkt_info
;
612 const unsigned char *buf
= decoder
->buf
;
613 enum intel_pt_pkt_ctx pkt_ctx
= decoder
->pkt_ctx
;
614 size_t len
= decoder
->len
;
617 pkt_info
.decoder
= decoder
;
618 pkt_info
.pos
= decoder
->pos
;
619 pkt_info
.pkt_len
= decoder
->pkt_step
;
620 pkt_info
.last_packet_type
= decoder
->last_packet_type
;
621 pkt_info
.data
= data
;
625 pkt_info
.pos
+= pkt_info
.pkt_len
;
626 buf
+= pkt_info
.pkt_len
;
627 len
-= pkt_info
.pkt_len
;
630 return INTEL_PT_NEED_MORE_BYTES
;
632 ret
= intel_pt_get_packet(buf
, len
, &pkt_info
.packet
,
635 return INTEL_PT_NEED_MORE_BYTES
;
639 pkt_info
.pkt_len
= ret
;
640 } while (pkt_info
.packet
.type
== INTEL_PT_PAD
);
646 pkt_info
.last_packet_type
= pkt_info
.packet
.type
;
650 struct intel_pt_calc_cyc_to_tsc_info
{
654 uint64_t ctc_timestamp
;
656 uint64_t tsc_timestamp
;
661 double cbr_cyc_to_tsc
;
665 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
666 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
667 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
668 * packet by copying the missing bits from the current MTC assuming the least
669 * difference between the two, and that the current MTC comes after last_mtc.
671 static void intel_pt_fixup_last_mtc(uint32_t mtc
, int mtc_shift
,
674 uint32_t first_missing_bit
= 1U << (16 - mtc_shift
);
675 uint32_t mask
= ~(first_missing_bit
- 1);
677 *last_mtc
|= mtc
& mask
;
678 if (*last_mtc
>= mtc
) {
679 *last_mtc
-= first_missing_bit
;
684 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info
*pkt_info
)
686 struct intel_pt_decoder
*decoder
= pkt_info
->decoder
;
687 struct intel_pt_calc_cyc_to_tsc_info
*data
= pkt_info
->data
;
691 uint32_t mtc
, mtc_delta
, ctc
, fc
, ctc_rem
;
693 switch (pkt_info
->packet
.type
) {
695 case INTEL_PT_TIP_PGE
:
700 case INTEL_PT_MODE_EXEC
:
701 case INTEL_PT_MODE_TSX
:
702 case INTEL_PT_PSBEND
:
706 case INTEL_PT_PTWRITE
:
707 case INTEL_PT_PTWRITE_IP
:
711 case INTEL_PT_BEP_IP
:
718 mtc
= pkt_info
->packet
.payload
;
719 if (decoder
->mtc_shift
> 8 && data
->fixup_last_mtc
) {
720 data
->fixup_last_mtc
= false;
721 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
724 if (mtc
> data
->last_mtc
)
725 mtc_delta
= mtc
- data
->last_mtc
;
727 mtc_delta
= mtc
+ 256 - data
->last_mtc
;
728 data
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
729 data
->last_mtc
= mtc
;
731 if (decoder
->tsc_ctc_mult
) {
732 timestamp
= data
->ctc_timestamp
+
733 data
->ctc_delta
* decoder
->tsc_ctc_mult
;
735 timestamp
= data
->ctc_timestamp
+
736 multdiv(data
->ctc_delta
,
737 decoder
->tsc_ctc_ratio_n
,
738 decoder
->tsc_ctc_ratio_d
);
741 if (timestamp
< data
->timestamp
)
744 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
745 data
->timestamp
= timestamp
;
753 * For now, do not support using TSC packets - refer
754 * intel_pt_calc_cyc_to_tsc().
758 timestamp
= pkt_info
->packet
.payload
|
759 (data
->timestamp
& (0xffULL
<< 56));
760 if (data
->from_mtc
&& timestamp
< data
->timestamp
&&
761 data
->timestamp
- timestamp
< decoder
->tsc_slip
)
763 if (timestamp
< data
->timestamp
)
764 timestamp
+= (1ULL << 56);
765 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
768 data
->tsc_timestamp
= timestamp
;
769 data
->timestamp
= timestamp
;
778 if (!decoder
->tsc_ctc_ratio_d
)
781 ctc
= pkt_info
->packet
.payload
;
782 fc
= pkt_info
->packet
.count
;
783 ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
785 data
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
787 data
->ctc_timestamp
= data
->tsc_timestamp
- fc
;
788 if (decoder
->tsc_ctc_mult
) {
789 data
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
791 data
->ctc_timestamp
-=
792 multdiv(ctc_rem
, decoder
->tsc_ctc_ratio_n
,
793 decoder
->tsc_ctc_ratio_d
);
797 data
->have_tma
= true;
798 data
->fixup_last_mtc
= true;
803 data
->cycle_cnt
+= pkt_info
->packet
.payload
;
807 cbr
= pkt_info
->packet
.payload
;
808 if (data
->cbr
&& data
->cbr
!= cbr
)
811 data
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
814 case INTEL_PT_TIP_PGD
:
815 case INTEL_PT_TRACESTOP
:
816 case INTEL_PT_EXSTOP
:
817 case INTEL_PT_EXSTOP_IP
:
822 case INTEL_PT_BAD
: /* Does not happen */
827 if (!data
->cbr
&& decoder
->cbr
) {
828 data
->cbr
= decoder
->cbr
;
829 data
->cbr_cyc_to_tsc
= decoder
->cbr_cyc_to_tsc
;
832 if (!data
->cycle_cnt
)
835 cyc_to_tsc
= (double)(timestamp
- decoder
->timestamp
) / data
->cycle_cnt
;
837 if (data
->cbr
&& cyc_to_tsc
> data
->cbr_cyc_to_tsc
&&
838 cyc_to_tsc
/ data
->cbr_cyc_to_tsc
> 1.25) {
839 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt
"\n",
840 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
844 decoder
->calc_cyc_to_tsc
= cyc_to_tsc
;
845 decoder
->have_calc_cyc_to_tsc
= true;
848 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt
"\n",
849 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
851 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt
"\n",
852 cyc_to_tsc
, pkt_info
->pos
);
858 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder
*decoder
,
861 struct intel_pt_calc_cyc_to_tsc_info data
= {
864 .last_mtc
= decoder
->last_mtc
,
865 .ctc_timestamp
= decoder
->ctc_timestamp
,
866 .ctc_delta
= decoder
->ctc_delta
,
867 .tsc_timestamp
= decoder
->tsc_timestamp
,
868 .timestamp
= decoder
->timestamp
,
869 .have_tma
= decoder
->have_tma
,
870 .fixup_last_mtc
= decoder
->fixup_last_mtc
,
871 .from_mtc
= from_mtc
,
876 * For now, do not support using TSC packets for at least the reasons:
877 * 1) timing might have stopped
878 * 2) TSC packets within PSB+ can slip against CYC packets
883 intel_pt_pkt_lookahead(decoder
, intel_pt_calc_cyc_cb
, &data
);
886 static int intel_pt_get_next_packet(struct intel_pt_decoder
*decoder
)
890 decoder
->last_packet_type
= decoder
->packet
.type
;
893 decoder
->pos
+= decoder
->pkt_step
;
894 decoder
->buf
+= decoder
->pkt_step
;
895 decoder
->len
-= decoder
->pkt_step
;
898 ret
= intel_pt_get_next_data(decoder
, false);
903 decoder
->prev_pkt_ctx
= decoder
->pkt_ctx
;
904 ret
= intel_pt_get_packet(decoder
->buf
, decoder
->len
,
905 &decoder
->packet
, &decoder
->pkt_ctx
);
906 if (ret
== INTEL_PT_NEED_MORE_BYTES
&& BITS_PER_LONG
== 32 &&
907 decoder
->len
< INTEL_PT_PKT_MAX_SZ
&& !decoder
->next_buf
) {
908 ret
= intel_pt_get_split_packet(decoder
);
913 return intel_pt_bad_packet(decoder
);
915 decoder
->pkt_len
= ret
;
916 decoder
->pkt_step
= ret
;
917 intel_pt_decoder_log_packet(decoder
);
918 } while (decoder
->packet
.type
== INTEL_PT_PAD
);
923 static uint64_t intel_pt_next_period(struct intel_pt_decoder
*decoder
)
925 uint64_t timestamp
, masked_timestamp
;
927 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
928 masked_timestamp
= timestamp
& decoder
->period_mask
;
929 if (decoder
->continuous_period
) {
930 if (masked_timestamp
> decoder
->last_masked_timestamp
)
934 masked_timestamp
= timestamp
& decoder
->period_mask
;
935 if (masked_timestamp
> decoder
->last_masked_timestamp
) {
936 decoder
->last_masked_timestamp
= masked_timestamp
;
937 decoder
->continuous_period
= true;
941 if (masked_timestamp
< decoder
->last_masked_timestamp
)
942 return decoder
->period_ticks
;
944 return decoder
->period_ticks
- (timestamp
- masked_timestamp
);
947 static uint64_t intel_pt_next_sample(struct intel_pt_decoder
*decoder
)
949 switch (decoder
->period_type
) {
950 case INTEL_PT_PERIOD_INSTRUCTIONS
:
951 return decoder
->period
- decoder
->period_insn_cnt
;
952 case INTEL_PT_PERIOD_TICKS
:
953 return intel_pt_next_period(decoder
);
954 case INTEL_PT_PERIOD_NONE
:
955 case INTEL_PT_PERIOD_MTC
:
961 static void intel_pt_sample_insn(struct intel_pt_decoder
*decoder
)
963 uint64_t timestamp
, masked_timestamp
;
965 switch (decoder
->period_type
) {
966 case INTEL_PT_PERIOD_INSTRUCTIONS
:
967 decoder
->period_insn_cnt
= 0;
969 case INTEL_PT_PERIOD_TICKS
:
970 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
971 masked_timestamp
= timestamp
& decoder
->period_mask
;
972 if (masked_timestamp
> decoder
->last_masked_timestamp
)
973 decoder
->last_masked_timestamp
= masked_timestamp
;
975 decoder
->last_masked_timestamp
+= decoder
->period_ticks
;
977 case INTEL_PT_PERIOD_NONE
:
978 case INTEL_PT_PERIOD_MTC
:
983 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
986 static int intel_pt_walk_insn(struct intel_pt_decoder
*decoder
,
987 struct intel_pt_insn
*intel_pt_insn
, uint64_t ip
)
989 uint64_t max_insn_cnt
, insn_cnt
= 0;
992 if (!decoder
->mtc_insn
)
993 decoder
->mtc_insn
= true;
995 max_insn_cnt
= intel_pt_next_sample(decoder
);
997 err
= decoder
->walk_insn(intel_pt_insn
, &insn_cnt
, &decoder
->ip
, ip
,
998 max_insn_cnt
, decoder
->data
);
1000 decoder
->tot_insn_cnt
+= insn_cnt
;
1001 decoder
->timestamp_insn_cnt
+= insn_cnt
;
1002 decoder
->sample_insn_cnt
+= insn_cnt
;
1003 decoder
->period_insn_cnt
+= insn_cnt
;
1006 decoder
->no_progress
= 0;
1007 decoder
->pkt_state
= INTEL_PT_STATE_ERR2
;
1008 intel_pt_log_at("ERROR: Failed to get instruction",
1015 if (ip
&& decoder
->ip
== ip
) {
1020 if (max_insn_cnt
&& insn_cnt
>= max_insn_cnt
)
1021 intel_pt_sample_insn(decoder
);
1023 if (intel_pt_insn
->branch
== INTEL_PT_BR_NO_BRANCH
) {
1024 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1025 decoder
->state
.from_ip
= decoder
->ip
;
1026 decoder
->state
.to_ip
= 0;
1027 decoder
->ip
+= intel_pt_insn
->length
;
1028 err
= INTEL_PT_RETURN
;
1032 if (intel_pt_insn
->op
== INTEL_PT_OP_CALL
) {
1033 /* Zero-length calls are excluded */
1034 if (intel_pt_insn
->branch
!= INTEL_PT_BR_UNCONDITIONAL
||
1035 intel_pt_insn
->rel
) {
1036 err
= intel_pt_push(&decoder
->stack
, decoder
->ip
+
1037 intel_pt_insn
->length
);
1041 } else if (intel_pt_insn
->op
== INTEL_PT_OP_RET
) {
1042 decoder
->ret_addr
= intel_pt_pop(&decoder
->stack
);
1045 if (intel_pt_insn
->branch
== INTEL_PT_BR_UNCONDITIONAL
) {
1046 int cnt
= decoder
->no_progress
++;
1048 decoder
->state
.from_ip
= decoder
->ip
;
1049 decoder
->ip
+= intel_pt_insn
->length
+
1051 decoder
->state
.to_ip
= decoder
->ip
;
1052 err
= INTEL_PT_RETURN
;
1055 * Check for being stuck in a loop. This can happen if a
1056 * decoder error results in the decoder erroneously setting the
1057 * ip to an address that is itself in an infinite loop that
1058 * consumes no packets. When that happens, there must be an
1059 * unconditional branch.
1063 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1064 decoder
->stuck_ip_prd
= 1;
1065 decoder
->stuck_ip_cnt
= 1;
1066 } else if (cnt
> INTEL_PT_MAX_LOOPS
||
1067 decoder
->state
.to_ip
== decoder
->stuck_ip
) {
1068 intel_pt_log_at("ERROR: Never-ending loop",
1069 decoder
->state
.to_ip
);
1070 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1073 } else if (!--decoder
->stuck_ip_cnt
) {
1074 decoder
->stuck_ip_prd
+= 1;
1075 decoder
->stuck_ip_cnt
= decoder
->stuck_ip_prd
;
1076 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1079 goto out_no_progress
;
1082 decoder
->no_progress
= 0;
1084 decoder
->state
.insn_op
= intel_pt_insn
->op
;
1085 decoder
->state
.insn_len
= intel_pt_insn
->length
;
1086 memcpy(decoder
->state
.insn
, intel_pt_insn
->buf
,
1087 INTEL_PT_INSN_BUF_SZ
);
1089 if (decoder
->tx_flags
& INTEL_PT_IN_TX
)
1090 decoder
->state
.flags
|= INTEL_PT_IN_TX
;
1095 static bool intel_pt_fup_event(struct intel_pt_decoder
*decoder
)
1099 if (decoder
->set_fup_tx_flags
) {
1100 decoder
->set_fup_tx_flags
= false;
1101 decoder
->tx_flags
= decoder
->fup_tx_flags
;
1102 decoder
->state
.type
= INTEL_PT_TRANSACTION
;
1103 decoder
->state
.from_ip
= decoder
->ip
;
1104 decoder
->state
.to_ip
= 0;
1105 decoder
->state
.flags
= decoder
->fup_tx_flags
;
1108 if (decoder
->set_fup_ptw
) {
1109 decoder
->set_fup_ptw
= false;
1110 decoder
->state
.type
= INTEL_PT_PTW
;
1111 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1112 decoder
->state
.from_ip
= decoder
->ip
;
1113 decoder
->state
.to_ip
= 0;
1114 decoder
->state
.ptw_payload
= decoder
->fup_ptw_payload
;
1117 if (decoder
->set_fup_mwait
) {
1118 decoder
->set_fup_mwait
= false;
1119 decoder
->state
.type
= INTEL_PT_MWAIT_OP
;
1120 decoder
->state
.from_ip
= decoder
->ip
;
1121 decoder
->state
.to_ip
= 0;
1122 decoder
->state
.mwait_payload
= decoder
->fup_mwait_payload
;
1125 if (decoder
->set_fup_pwre
) {
1126 decoder
->set_fup_pwre
= false;
1127 decoder
->state
.type
|= INTEL_PT_PWR_ENTRY
;
1128 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1129 decoder
->state
.from_ip
= decoder
->ip
;
1130 decoder
->state
.to_ip
= 0;
1131 decoder
->state
.pwre_payload
= decoder
->fup_pwre_payload
;
1134 if (decoder
->set_fup_exstop
) {
1135 decoder
->set_fup_exstop
= false;
1136 decoder
->state
.type
|= INTEL_PT_EX_STOP
;
1137 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1138 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1139 decoder
->state
.from_ip
= decoder
->ip
;
1140 decoder
->state
.to_ip
= 0;
1143 if (decoder
->set_fup_bep
) {
1144 decoder
->set_fup_bep
= false;
1145 decoder
->state
.type
|= INTEL_PT_BLK_ITEMS
;
1146 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1147 decoder
->state
.from_ip
= decoder
->ip
;
1148 decoder
->state
.to_ip
= 0;
1154 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder
*decoder
,
1155 struct intel_pt_insn
*intel_pt_insn
,
1156 uint64_t ip
, int err
)
1158 return decoder
->flags
& INTEL_PT_FUP_WITH_NLIP
&& !err
&&
1159 intel_pt_insn
->branch
== INTEL_PT_BR_INDIRECT
&&
1160 ip
== decoder
->ip
+ intel_pt_insn
->length
;
1163 static int intel_pt_walk_fup(struct intel_pt_decoder
*decoder
)
1165 struct intel_pt_insn intel_pt_insn
;
1169 ip
= decoder
->last_ip
;
1172 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, ip
);
1173 if (err
== INTEL_PT_RETURN
)
1175 if (err
== -EAGAIN
||
1176 intel_pt_fup_with_nlip(decoder
, &intel_pt_insn
, ip
, err
)) {
1177 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1178 if (intel_pt_fup_event(decoder
))
1182 decoder
->set_fup_tx_flags
= false;
1186 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1187 intel_pt_log_at("ERROR: Unexpected indirect branch",
1189 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1193 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1194 intel_pt_log_at("ERROR: Unexpected conditional branch",
1196 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1200 intel_pt_bug(decoder
);
1204 static int intel_pt_walk_tip(struct intel_pt_decoder
*decoder
)
1206 struct intel_pt_insn intel_pt_insn
;
1209 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1210 if (err
== INTEL_PT_RETURN
&&
1212 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1213 (decoder
->state
.type
& INTEL_PT_BRANCH
) &&
1214 decoder
->pgd_ip(decoder
->state
.to_ip
, decoder
->data
)) {
1215 /* Unconditional branch leaving filter region */
1216 decoder
->no_progress
= 0;
1217 decoder
->pge
= false;
1218 decoder
->continuous_period
= false;
1219 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1220 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1223 if (err
== INTEL_PT_RETURN
)
1228 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1229 if (decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
) {
1230 decoder
->pge
= false;
1231 decoder
->continuous_period
= false;
1232 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1233 decoder
->state
.from_ip
= decoder
->ip
;
1234 if (decoder
->packet
.count
== 0) {
1235 decoder
->state
.to_ip
= 0;
1237 decoder
->state
.to_ip
= decoder
->last_ip
;
1238 decoder
->ip
= decoder
->last_ip
;
1240 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1242 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1243 decoder
->state
.from_ip
= decoder
->ip
;
1244 if (decoder
->packet
.count
== 0) {
1245 decoder
->state
.to_ip
= 0;
1247 decoder
->state
.to_ip
= decoder
->last_ip
;
1248 decoder
->ip
= decoder
->last_ip
;
1254 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1255 uint64_t to_ip
= decoder
->ip
+ intel_pt_insn
.length
+
1258 if (decoder
->pgd_ip
&&
1259 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1260 decoder
->pgd_ip(to_ip
, decoder
->data
)) {
1261 /* Conditional branch leaving filter region */
1262 decoder
->pge
= false;
1263 decoder
->continuous_period
= false;
1264 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1265 decoder
->ip
= to_ip
;
1266 decoder
->state
.from_ip
= decoder
->ip
;
1267 decoder
->state
.to_ip
= to_ip
;
1268 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1271 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1273 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1277 return intel_pt_bug(decoder
);
1280 static int intel_pt_walk_tnt(struct intel_pt_decoder
*decoder
)
1282 struct intel_pt_insn intel_pt_insn
;
1286 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1287 if (err
== INTEL_PT_RETURN
)
1292 if (intel_pt_insn
.op
== INTEL_PT_OP_RET
) {
1293 if (!decoder
->return_compression
) {
1294 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1296 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1299 if (!decoder
->ret_addr
) {
1300 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1302 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1305 if (!(decoder
->tnt
.payload
& BIT63
)) {
1306 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1308 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1311 decoder
->tnt
.count
-= 1;
1312 if (decoder
->tnt
.count
)
1313 decoder
->pkt_state
= INTEL_PT_STATE_TNT_CONT
;
1315 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1316 decoder
->tnt
.payload
<<= 1;
1317 decoder
->state
.from_ip
= decoder
->ip
;
1318 decoder
->ip
= decoder
->ret_addr
;
1319 decoder
->state
.to_ip
= decoder
->ip
;
1323 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1324 /* Handle deferred TIPs */
1325 err
= intel_pt_get_next_packet(decoder
);
1328 if (decoder
->packet
.type
!= INTEL_PT_TIP
||
1329 decoder
->packet
.count
== 0) {
1330 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1332 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1333 decoder
->pkt_step
= 0;
1336 intel_pt_set_last_ip(decoder
);
1337 decoder
->state
.from_ip
= decoder
->ip
;
1338 decoder
->state
.to_ip
= decoder
->last_ip
;
1339 decoder
->ip
= decoder
->last_ip
;
1343 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1344 decoder
->tnt
.count
-= 1;
1345 if (decoder
->tnt
.count
)
1346 decoder
->pkt_state
= INTEL_PT_STATE_TNT_CONT
;
1348 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1349 if (decoder
->tnt
.payload
& BIT63
) {
1350 decoder
->tnt
.payload
<<= 1;
1351 decoder
->state
.from_ip
= decoder
->ip
;
1352 decoder
->ip
+= intel_pt_insn
.length
+
1354 decoder
->state
.to_ip
= decoder
->ip
;
1357 /* Instruction sample for a non-taken branch */
1358 if (decoder
->state
.type
& INTEL_PT_INSTRUCTION
) {
1359 decoder
->tnt
.payload
<<= 1;
1360 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1361 decoder
->state
.from_ip
= decoder
->ip
;
1362 decoder
->state
.to_ip
= 0;
1363 decoder
->ip
+= intel_pt_insn
.length
;
1366 decoder
->sample_cyc
= false;
1367 decoder
->ip
+= intel_pt_insn
.length
;
1368 if (!decoder
->tnt
.count
) {
1369 intel_pt_update_sample_time(decoder
);
1372 decoder
->tnt
.payload
<<= 1;
1376 return intel_pt_bug(decoder
);
1380 static int intel_pt_mode_tsx(struct intel_pt_decoder
*decoder
, bool *no_tip
)
1382 unsigned int fup_tx_flags
;
1385 fup_tx_flags
= decoder
->packet
.payload
&
1386 (INTEL_PT_IN_TX
| INTEL_PT_ABORT_TX
);
1387 err
= intel_pt_get_next_packet(decoder
);
1390 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1391 decoder
->fup_tx_flags
= fup_tx_flags
;
1392 decoder
->set_fup_tx_flags
= true;
1393 if (!(decoder
->fup_tx_flags
& INTEL_PT_ABORT_TX
))
1396 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1398 intel_pt_update_in_tx(decoder
);
1403 static uint64_t intel_pt_8b_tsc(uint64_t timestamp
, uint64_t ref_timestamp
)
1405 timestamp
|= (ref_timestamp
& (0xffULL
<< 56));
1407 if (timestamp
< ref_timestamp
) {
1408 if (ref_timestamp
- timestamp
> (1ULL << 55))
1409 timestamp
+= (1ULL << 56);
1411 if (timestamp
- ref_timestamp
> (1ULL << 55))
1412 timestamp
-= (1ULL << 56);
1418 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder
*decoder
)
1422 decoder
->have_tma
= false;
1424 if (decoder
->ref_timestamp
) {
1425 timestamp
= intel_pt_8b_tsc(decoder
->packet
.payload
,
1426 decoder
->ref_timestamp
);
1427 decoder
->tsc_timestamp
= timestamp
;
1428 decoder
->timestamp
= timestamp
;
1429 decoder
->ref_timestamp
= 0;
1430 decoder
->timestamp_insn_cnt
= 0;
1431 } else if (decoder
->timestamp
) {
1432 timestamp
= decoder
->packet
.payload
|
1433 (decoder
->timestamp
& (0xffULL
<< 56));
1434 decoder
->tsc_timestamp
= timestamp
;
1435 if (timestamp
< decoder
->timestamp
&&
1436 decoder
->timestamp
- timestamp
< decoder
->tsc_slip
) {
1437 intel_pt_log_to("Suppressing backwards timestamp",
1439 timestamp
= decoder
->timestamp
;
1441 if (timestamp
< decoder
->timestamp
) {
1442 intel_pt_log_to("Wraparound timestamp", timestamp
);
1443 timestamp
+= (1ULL << 56);
1444 decoder
->tsc_timestamp
= timestamp
;
1446 decoder
->timestamp
= timestamp
;
1447 decoder
->timestamp_insn_cnt
= 0;
1450 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1451 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1452 decoder
->cycle_cnt
= 0;
1453 decoder
->have_calc_cyc_to_tsc
= false;
1454 intel_pt_calc_cyc_to_tsc(decoder
, false);
1457 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1460 static int intel_pt_overflow(struct intel_pt_decoder
*decoder
)
1462 intel_pt_log("ERROR: Buffer overflow\n");
1463 intel_pt_clear_tx_flags(decoder
);
1464 decoder
->timestamp_insn_cnt
= 0;
1465 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1466 decoder
->overflow
= true;
1470 static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder
*decoder
)
1472 if (decoder
->have_cyc
)
1475 decoder
->cyc_cnt_timestamp
= decoder
->timestamp
;
1476 decoder
->base_cyc_cnt
= decoder
->tot_cyc_cnt
;
1479 static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder
*decoder
)
1481 decoder
->tsc_to_cyc
= decoder
->cbr
/ decoder
->max_non_turbo_ratio_fp
;
1484 intel_pt_mtc_cyc_cnt_pge(decoder
);
1487 static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder
*decoder
)
1489 uint64_t tot_cyc_cnt
, tsc_delta
;
1491 if (decoder
->have_cyc
)
1494 decoder
->sample_cyc
= true;
1496 if (!decoder
->pge
|| decoder
->timestamp
<= decoder
->cyc_cnt_timestamp
)
1499 tsc_delta
= decoder
->timestamp
- decoder
->cyc_cnt_timestamp
;
1500 tot_cyc_cnt
= tsc_delta
* decoder
->tsc_to_cyc
+ decoder
->base_cyc_cnt
;
1502 if (tot_cyc_cnt
> decoder
->tot_cyc_cnt
)
1503 decoder
->tot_cyc_cnt
= tot_cyc_cnt
;
1506 static void intel_pt_calc_tma(struct intel_pt_decoder
*decoder
)
1508 uint32_t ctc
= decoder
->packet
.payload
;
1509 uint32_t fc
= decoder
->packet
.count
;
1510 uint32_t ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
1512 if (!decoder
->tsc_ctc_ratio_d
)
1515 if (decoder
->pge
&& !decoder
->in_psb
)
1516 intel_pt_mtc_cyc_cnt_pge(decoder
);
1518 intel_pt_mtc_cyc_cnt_upd(decoder
);
1520 decoder
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
1521 decoder
->ctc_timestamp
= decoder
->tsc_timestamp
- fc
;
1522 if (decoder
->tsc_ctc_mult
) {
1523 decoder
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
1525 decoder
->ctc_timestamp
-= multdiv(ctc_rem
,
1526 decoder
->tsc_ctc_ratio_n
,
1527 decoder
->tsc_ctc_ratio_d
);
1529 decoder
->ctc_delta
= 0;
1530 decoder
->have_tma
= true;
1531 decoder
->fixup_last_mtc
= true;
1532 intel_pt_log("CTC timestamp " x64_fmt
" last MTC %#x CTC rem %#x\n",
1533 decoder
->ctc_timestamp
, decoder
->last_mtc
, ctc_rem
);
1536 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder
*decoder
)
1539 uint32_t mtc
, mtc_delta
;
1541 if (!decoder
->have_tma
)
1544 mtc
= decoder
->packet
.payload
;
1546 if (decoder
->mtc_shift
> 8 && decoder
->fixup_last_mtc
) {
1547 decoder
->fixup_last_mtc
= false;
1548 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
1549 &decoder
->last_mtc
);
1552 if (mtc
> decoder
->last_mtc
)
1553 mtc_delta
= mtc
- decoder
->last_mtc
;
1555 mtc_delta
= mtc
+ 256 - decoder
->last_mtc
;
1557 decoder
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
1559 if (decoder
->tsc_ctc_mult
) {
1560 timestamp
= decoder
->ctc_timestamp
+
1561 decoder
->ctc_delta
* decoder
->tsc_ctc_mult
;
1563 timestamp
= decoder
->ctc_timestamp
+
1564 multdiv(decoder
->ctc_delta
,
1565 decoder
->tsc_ctc_ratio_n
,
1566 decoder
->tsc_ctc_ratio_d
);
1569 if (timestamp
< decoder
->timestamp
)
1570 intel_pt_log("Suppressing MTC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1571 timestamp
, decoder
->timestamp
);
1573 decoder
->timestamp
= timestamp
;
1575 intel_pt_mtc_cyc_cnt_upd(decoder
);
1577 decoder
->timestamp_insn_cnt
= 0;
1578 decoder
->last_mtc
= mtc
;
1580 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1581 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1582 decoder
->cycle_cnt
= 0;
1583 decoder
->have_calc_cyc_to_tsc
= false;
1584 intel_pt_calc_cyc_to_tsc(decoder
, true);
1587 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1590 static void intel_pt_calc_cbr(struct intel_pt_decoder
*decoder
)
1592 unsigned int cbr
= decoder
->packet
.payload
& 0xff;
1594 decoder
->cbr_payload
= decoder
->packet
.payload
;
1596 if (decoder
->cbr
== cbr
)
1600 decoder
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
1602 intel_pt_mtc_cyc_cnt_cbr(decoder
);
1605 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder
*decoder
)
1607 uint64_t timestamp
= decoder
->cyc_ref_timestamp
;
1609 decoder
->have_cyc
= true;
1611 decoder
->cycle_cnt
+= decoder
->packet
.payload
;
1613 decoder
->tot_cyc_cnt
+= decoder
->packet
.payload
;
1614 decoder
->sample_cyc
= true;
1616 if (!decoder
->cyc_ref_timestamp
)
1619 if (decoder
->have_calc_cyc_to_tsc
)
1620 timestamp
+= decoder
->cycle_cnt
* decoder
->calc_cyc_to_tsc
;
1621 else if (decoder
->cbr
)
1622 timestamp
+= decoder
->cycle_cnt
* decoder
->cbr_cyc_to_tsc
;
1626 if (timestamp
< decoder
->timestamp
)
1627 intel_pt_log("Suppressing CYC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1628 timestamp
, decoder
->timestamp
);
1630 decoder
->timestamp
= timestamp
;
1632 decoder
->timestamp_insn_cnt
= 0;
1634 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1637 static void intel_pt_bbp(struct intel_pt_decoder
*decoder
)
1639 if (decoder
->prev_pkt_ctx
== INTEL_PT_NO_CTX
) {
1640 memset(decoder
->state
.items
.mask
, 0, sizeof(decoder
->state
.items
.mask
));
1641 decoder
->state
.items
.is_32_bit
= false;
1643 decoder
->blk_type
= decoder
->packet
.payload
;
1644 decoder
->blk_type_pos
= intel_pt_blk_type_pos(decoder
->blk_type
);
1645 if (decoder
->blk_type
== INTEL_PT_GP_REGS
)
1646 decoder
->state
.items
.is_32_bit
= decoder
->packet
.count
;
1647 if (decoder
->blk_type_pos
< 0) {
1648 intel_pt_log("WARNING: Unknown block type %u\n",
1650 } else if (decoder
->state
.items
.mask
[decoder
->blk_type_pos
]) {
1651 intel_pt_log("WARNING: Duplicate block type %u\n",
1656 static void intel_pt_bip(struct intel_pt_decoder
*decoder
)
1658 uint32_t id
= decoder
->packet
.count
;
1659 uint32_t bit
= 1 << id
;
1660 int pos
= decoder
->blk_type_pos
;
1662 if (pos
< 0 || id
>= INTEL_PT_BLK_ITEM_ID_CNT
) {
1663 intel_pt_log("WARNING: Unknown block item %u type %d\n",
1664 id
, decoder
->blk_type
);
1668 if (decoder
->state
.items
.mask
[pos
] & bit
) {
1669 intel_pt_log("WARNING: Duplicate block item %u type %d\n",
1670 id
, decoder
->blk_type
);
1673 decoder
->state
.items
.mask
[pos
] |= bit
;
1674 decoder
->state
.items
.val
[pos
][id
] = decoder
->packet
.payload
;
1677 /* Walk PSB+ packets when already in sync. */
1678 static int intel_pt_walk_psbend(struct intel_pt_decoder
*decoder
)
1682 decoder
->in_psb
= true;
1685 err
= intel_pt_get_next_packet(decoder
);
1689 switch (decoder
->packet
.type
) {
1690 case INTEL_PT_PSBEND
:
1694 case INTEL_PT_TIP_PGD
:
1695 case INTEL_PT_TIP_PGE
:
1698 case INTEL_PT_TRACESTOP
:
1701 case INTEL_PT_PTWRITE
:
1702 case INTEL_PT_PTWRITE_IP
:
1703 case INTEL_PT_EXSTOP
:
1704 case INTEL_PT_EXSTOP_IP
:
1705 case INTEL_PT_MWAIT
:
1711 case INTEL_PT_BEP_IP
:
1712 decoder
->have_tma
= false;
1713 intel_pt_log("ERROR: Unexpected packet\n");
1718 err
= intel_pt_overflow(decoder
);
1722 intel_pt_calc_tsc_timestamp(decoder
);
1726 intel_pt_calc_tma(decoder
);
1730 intel_pt_calc_cbr(decoder
);
1733 case INTEL_PT_MODE_EXEC
:
1734 decoder
->exec_mode
= decoder
->packet
.payload
;
1738 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1742 decoder
->pge
= true;
1743 if (decoder
->packet
.count
) {
1744 intel_pt_set_last_ip(decoder
);
1746 /* Act on FUP at PSBEND */
1747 decoder
->ip
= decoder
->last_ip
;
1748 decoder
->hop_psb_fup
= true;
1753 case INTEL_PT_MODE_TSX
:
1754 intel_pt_update_in_tx(decoder
);
1758 intel_pt_calc_mtc_timestamp(decoder
);
1759 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1760 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1772 decoder
->in_psb
= false;
1777 static int intel_pt_walk_fup_tip(struct intel_pt_decoder
*decoder
)
1781 if (decoder
->tx_flags
& INTEL_PT_ABORT_TX
) {
1782 decoder
->tx_flags
= 0;
1783 decoder
->state
.flags
&= ~INTEL_PT_IN_TX
;
1784 decoder
->state
.flags
|= INTEL_PT_ABORT_TX
;
1786 decoder
->state
.flags
|= INTEL_PT_ASYNC
;
1790 err
= intel_pt_get_next_packet(decoder
);
1794 switch (decoder
->packet
.type
) {
1797 case INTEL_PT_TRACESTOP
:
1801 case INTEL_PT_MODE_TSX
:
1803 case INTEL_PT_PSBEND
:
1804 case INTEL_PT_PTWRITE
:
1805 case INTEL_PT_PTWRITE_IP
:
1806 case INTEL_PT_EXSTOP
:
1807 case INTEL_PT_EXSTOP_IP
:
1808 case INTEL_PT_MWAIT
:
1814 case INTEL_PT_BEP_IP
:
1815 intel_pt_log("ERROR: Missing TIP after FUP\n");
1816 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1817 decoder
->pkt_step
= 0;
1821 intel_pt_calc_cbr(decoder
);
1825 return intel_pt_overflow(decoder
);
1827 case INTEL_PT_TIP_PGD
:
1828 decoder
->state
.from_ip
= decoder
->ip
;
1829 if (decoder
->packet
.count
== 0) {
1830 decoder
->state
.to_ip
= 0;
1832 intel_pt_set_ip(decoder
);
1833 decoder
->state
.to_ip
= decoder
->ip
;
1835 decoder
->pge
= false;
1836 decoder
->continuous_period
= false;
1837 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1840 case INTEL_PT_TIP_PGE
:
1841 decoder
->pge
= true;
1842 intel_pt_log("Omitting PGE ip " x64_fmt
"\n",
1844 decoder
->state
.from_ip
= 0;
1845 if (decoder
->packet
.count
== 0) {
1846 decoder
->state
.to_ip
= 0;
1848 intel_pt_set_ip(decoder
);
1849 decoder
->state
.to_ip
= decoder
->ip
;
1851 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
1852 intel_pt_mtc_cyc_cnt_pge(decoder
);
1856 decoder
->state
.from_ip
= decoder
->ip
;
1857 if (decoder
->packet
.count
== 0) {
1858 decoder
->state
.to_ip
= 0;
1860 intel_pt_set_ip(decoder
);
1861 decoder
->state
.to_ip
= decoder
->ip
;
1866 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1870 intel_pt_calc_mtc_timestamp(decoder
);
1871 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1872 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1876 intel_pt_calc_cyc_timestamp(decoder
);
1879 case INTEL_PT_MODE_EXEC
:
1880 decoder
->exec_mode
= decoder
->packet
.payload
;
1889 return intel_pt_bug(decoder
);
1894 static int intel_pt_resample(struct intel_pt_decoder
*decoder
)
1896 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1897 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1898 decoder
->state
.from_ip
= decoder
->ip
;
1899 decoder
->state
.to_ip
= 0;
1903 #define HOP_PROCESS 0
1904 #define HOP_IGNORE 1
1905 #define HOP_RETURN 2
1908 static int intel_pt_scan_for_psb(struct intel_pt_decoder
*decoder
);
1910 /* Hop mode: Ignore TNT, do not walk code, but get ip from FUPs and TIPs */
1911 static int intel_pt_hop_trace(struct intel_pt_decoder
*decoder
, bool *no_tip
, int *err
)
1913 /* Leap from PSB to PSB, getting ip from FUP within PSB+ */
1914 if (decoder
->leap
&& !decoder
->in_psb
&& decoder
->packet
.type
!= INTEL_PT_PSB
) {
1915 *err
= intel_pt_scan_for_psb(decoder
);
1920 switch (decoder
->packet
.type
) {
1924 case INTEL_PT_TIP_PGD
:
1925 if (!decoder
->packet
.count
)
1927 intel_pt_set_ip(decoder
);
1928 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1929 decoder
->state
.from_ip
= 0;
1930 decoder
->state
.to_ip
= decoder
->ip
;
1934 if (!decoder
->packet
.count
)
1936 intel_pt_set_ip(decoder
);
1937 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1938 decoder
->state
.from_ip
= decoder
->ip
;
1939 decoder
->state
.to_ip
= 0;
1943 if (!decoder
->packet
.count
)
1945 intel_pt_set_ip(decoder
);
1946 if (intel_pt_fup_event(decoder
))
1948 if (!decoder
->branch_enable
)
1951 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1952 decoder
->state
.from_ip
= decoder
->ip
;
1953 decoder
->state
.to_ip
= 0;
1956 *err
= intel_pt_walk_fup_tip(decoder
);
1958 decoder
->pkt_state
= INTEL_PT_STATE_RESAMPLE
;
1962 decoder
->last_ip
= 0;
1963 decoder
->have_last_ip
= true;
1964 decoder
->hop_psb_fup
= false;
1965 *err
= intel_pt_walk_psbend(decoder
);
1966 if (*err
== -EAGAIN
)
1970 if (decoder
->hop_psb_fup
) {
1971 decoder
->hop_psb_fup
= false;
1972 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1973 decoder
->state
.from_ip
= decoder
->ip
;
1974 decoder
->state
.to_ip
= 0;
1977 if (decoder
->cbr
!= decoder
->cbr_seen
) {
1978 decoder
->state
.type
= 0;
1985 case INTEL_PT_TIP_PGE
:
1988 case INTEL_PT_MODE_EXEC
:
1989 case INTEL_PT_MODE_TSX
:
1993 case INTEL_PT_PSBEND
:
1995 case INTEL_PT_TRACESTOP
:
1999 case INTEL_PT_PTWRITE
:
2000 case INTEL_PT_PTWRITE_IP
:
2001 case INTEL_PT_EXSTOP
:
2002 case INTEL_PT_EXSTOP_IP
:
2003 case INTEL_PT_MWAIT
:
2009 case INTEL_PT_BEP_IP
:
2015 static int intel_pt_walk_trace(struct intel_pt_decoder
*decoder
)
2017 bool no_tip
= false;
2021 err
= intel_pt_get_next_packet(decoder
);
2026 switch (intel_pt_hop_trace(decoder
, &no_tip
, &err
)) {
2038 switch (decoder
->packet
.type
) {
2040 if (!decoder
->packet
.count
)
2042 decoder
->tnt
= decoder
->packet
;
2043 decoder
->pkt_state
= INTEL_PT_STATE_TNT
;
2044 err
= intel_pt_walk_tnt(decoder
);
2049 case INTEL_PT_TIP_PGD
:
2050 if (decoder
->packet
.count
!= 0)
2051 intel_pt_set_last_ip(decoder
);
2052 decoder
->pkt_state
= INTEL_PT_STATE_TIP_PGD
;
2053 return intel_pt_walk_tip(decoder
);
2055 case INTEL_PT_TIP_PGE
: {
2056 decoder
->pge
= true;
2057 intel_pt_mtc_cyc_cnt_pge(decoder
);
2058 if (decoder
->packet
.count
== 0) {
2059 intel_pt_log_at("Skipping zero TIP.PGE",
2063 intel_pt_set_ip(decoder
);
2064 decoder
->state
.from_ip
= 0;
2065 decoder
->state
.to_ip
= decoder
->ip
;
2066 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
2068 * In hop mode, resample to get the to_ip as an
2069 * "instruction" sample.
2072 decoder
->pkt_state
= INTEL_PT_STATE_RESAMPLE
;
2077 return intel_pt_overflow(decoder
);
2080 if (decoder
->packet
.count
!= 0)
2081 intel_pt_set_last_ip(decoder
);
2082 decoder
->pkt_state
= INTEL_PT_STATE_TIP
;
2083 return intel_pt_walk_tip(decoder
);
2086 if (decoder
->packet
.count
== 0) {
2087 intel_pt_log_at("Skipping zero FUP",
2092 intel_pt_set_last_ip(decoder
);
2093 if (!decoder
->branch_enable
) {
2094 decoder
->ip
= decoder
->last_ip
;
2095 if (intel_pt_fup_event(decoder
))
2100 if (decoder
->set_fup_mwait
)
2103 decoder
->pkt_state
= INTEL_PT_STATE_FUP_NO_TIP
;
2105 decoder
->pkt_state
= INTEL_PT_STATE_FUP
;
2106 err
= intel_pt_walk_fup(decoder
);
2113 return intel_pt_walk_fup_tip(decoder
);
2115 case INTEL_PT_TRACESTOP
:
2116 decoder
->pge
= false;
2117 decoder
->continuous_period
= false;
2118 intel_pt_clear_tx_flags(decoder
);
2119 decoder
->have_tma
= false;
2123 decoder
->last_ip
= 0;
2124 decoder
->have_last_ip
= true;
2125 intel_pt_clear_stack(&decoder
->stack
);
2126 err
= intel_pt_walk_psbend(decoder
);
2132 * PSB+ CBR will not have changed but cater for the
2133 * possibility of another CBR change that gets caught up
2136 if (decoder
->cbr
!= decoder
->cbr_seen
) {
2137 decoder
->state
.type
= 0;
2143 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2147 intel_pt_calc_mtc_timestamp(decoder
);
2148 if (decoder
->period_type
!= INTEL_PT_PERIOD_MTC
)
2151 * Ensure that there has been an instruction since the
2154 if (!decoder
->mtc_insn
)
2156 decoder
->mtc_insn
= false;
2157 /* Ensure that there is a timestamp */
2158 if (!decoder
->timestamp
)
2160 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
2161 decoder
->state
.from_ip
= decoder
->ip
;
2162 decoder
->state
.to_ip
= 0;
2163 decoder
->mtc_insn
= false;
2167 intel_pt_calc_tsc_timestamp(decoder
);
2171 intel_pt_calc_tma(decoder
);
2175 intel_pt_calc_cyc_timestamp(decoder
);
2179 intel_pt_calc_cbr(decoder
);
2180 if (decoder
->cbr
!= decoder
->cbr_seen
) {
2181 decoder
->state
.type
= 0;
2186 case INTEL_PT_MODE_EXEC
:
2187 decoder
->exec_mode
= decoder
->packet
.payload
;
2190 case INTEL_PT_MODE_TSX
:
2191 /* MODE_TSX need not be followed by FUP */
2192 if (!decoder
->pge
|| decoder
->in_psb
) {
2193 intel_pt_update_in_tx(decoder
);
2196 err
= intel_pt_mode_tsx(decoder
, &no_tip
);
2201 case INTEL_PT_BAD
: /* Does not happen */
2202 return intel_pt_bug(decoder
);
2204 case INTEL_PT_PSBEND
:
2210 case INTEL_PT_PTWRITE_IP
:
2211 decoder
->fup_ptw_payload
= decoder
->packet
.payload
;
2212 err
= intel_pt_get_next_packet(decoder
);
2215 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
2216 decoder
->set_fup_ptw
= true;
2219 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
2224 case INTEL_PT_PTWRITE
:
2225 decoder
->state
.type
= INTEL_PT_PTW
;
2226 decoder
->state
.from_ip
= decoder
->ip
;
2227 decoder
->state
.to_ip
= 0;
2228 decoder
->state
.ptw_payload
= decoder
->packet
.payload
;
2231 case INTEL_PT_MWAIT
:
2232 decoder
->fup_mwait_payload
= decoder
->packet
.payload
;
2233 decoder
->set_fup_mwait
= true;
2237 if (decoder
->set_fup_mwait
) {
2238 decoder
->fup_pwre_payload
=
2239 decoder
->packet
.payload
;
2240 decoder
->set_fup_pwre
= true;
2243 decoder
->state
.type
= INTEL_PT_PWR_ENTRY
;
2244 decoder
->state
.from_ip
= decoder
->ip
;
2245 decoder
->state
.to_ip
= 0;
2246 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
2249 case INTEL_PT_EXSTOP_IP
:
2250 err
= intel_pt_get_next_packet(decoder
);
2253 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
2254 decoder
->set_fup_exstop
= true;
2257 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
2262 case INTEL_PT_EXSTOP
:
2263 decoder
->state
.type
= INTEL_PT_EX_STOP
;
2264 decoder
->state
.from_ip
= decoder
->ip
;
2265 decoder
->state
.to_ip
= 0;
2269 decoder
->state
.type
= INTEL_PT_PWR_EXIT
;
2270 decoder
->state
.from_ip
= decoder
->ip
;
2271 decoder
->state
.to_ip
= 0;
2272 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
2276 intel_pt_bbp(decoder
);
2280 intel_pt_bip(decoder
);
2284 decoder
->state
.type
= INTEL_PT_BLK_ITEMS
;
2285 decoder
->state
.from_ip
= decoder
->ip
;
2286 decoder
->state
.to_ip
= 0;
2289 case INTEL_PT_BEP_IP
:
2290 err
= intel_pt_get_next_packet(decoder
);
2293 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
2294 decoder
->set_fup_bep
= true;
2297 intel_pt_log_at("ERROR: Missing FUP after BEP",
2303 return intel_pt_bug(decoder
);
2308 static inline bool intel_pt_have_ip(struct intel_pt_decoder
*decoder
)
2310 return decoder
->packet
.count
&&
2311 (decoder
->have_last_ip
|| decoder
->packet
.count
== 3 ||
2312 decoder
->packet
.count
== 6);
2315 /* Walk PSB+ packets to get in sync. */
2316 static int intel_pt_walk_psb(struct intel_pt_decoder
*decoder
)
2320 decoder
->in_psb
= true;
2323 err
= intel_pt_get_next_packet(decoder
);
2327 switch (decoder
->packet
.type
) {
2328 case INTEL_PT_TIP_PGD
:
2329 decoder
->continuous_period
= false;
2331 case INTEL_PT_TIP_PGE
:
2333 case INTEL_PT_PTWRITE
:
2334 case INTEL_PT_PTWRITE_IP
:
2335 case INTEL_PT_EXSTOP
:
2336 case INTEL_PT_EXSTOP_IP
:
2337 case INTEL_PT_MWAIT
:
2343 case INTEL_PT_BEP_IP
:
2344 intel_pt_log("ERROR: Unexpected packet\n");
2349 decoder
->pge
= true;
2350 if (intel_pt_have_ip(decoder
)) {
2351 uint64_t current_ip
= decoder
->ip
;
2353 intel_pt_set_ip(decoder
);
2355 intel_pt_log_to("Setting IP",
2361 intel_pt_calc_mtc_timestamp(decoder
);
2365 intel_pt_calc_tsc_timestamp(decoder
);
2369 intel_pt_calc_tma(decoder
);
2373 intel_pt_calc_cyc_timestamp(decoder
);
2377 intel_pt_calc_cbr(decoder
);
2381 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2384 case INTEL_PT_MODE_EXEC
:
2385 decoder
->exec_mode
= decoder
->packet
.payload
;
2388 case INTEL_PT_MODE_TSX
:
2389 intel_pt_update_in_tx(decoder
);
2392 case INTEL_PT_TRACESTOP
:
2393 decoder
->pge
= false;
2394 decoder
->continuous_period
= false;
2395 intel_pt_clear_tx_flags(decoder
);
2399 decoder
->have_tma
= false;
2400 intel_pt_log("ERROR: Unexpected packet\n");
2402 decoder
->pkt_state
= INTEL_PT_STATE_ERR4
;
2404 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
2408 case INTEL_PT_BAD
: /* Does not happen */
2409 err
= intel_pt_bug(decoder
);
2413 err
= intel_pt_overflow(decoder
);
2416 case INTEL_PT_PSBEND
:
2429 decoder
->in_psb
= false;
2434 static int intel_pt_walk_to_ip(struct intel_pt_decoder
*decoder
)
2439 err
= intel_pt_get_next_packet(decoder
);
2443 switch (decoder
->packet
.type
) {
2444 case INTEL_PT_TIP_PGD
:
2445 decoder
->continuous_period
= false;
2446 decoder
->pge
= false;
2447 if (intel_pt_have_ip(decoder
))
2448 intel_pt_set_ip(decoder
);
2451 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
2454 case INTEL_PT_TIP_PGE
:
2455 decoder
->pge
= true;
2456 intel_pt_mtc_cyc_cnt_pge(decoder
);
2457 if (intel_pt_have_ip(decoder
))
2458 intel_pt_set_ip(decoder
);
2461 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
2465 decoder
->pge
= true;
2466 if (intel_pt_have_ip(decoder
))
2467 intel_pt_set_ip(decoder
);
2473 if (intel_pt_have_ip(decoder
))
2474 intel_pt_set_ip(decoder
);
2480 intel_pt_calc_mtc_timestamp(decoder
);
2484 intel_pt_calc_tsc_timestamp(decoder
);
2488 intel_pt_calc_tma(decoder
);
2492 intel_pt_calc_cyc_timestamp(decoder
);
2496 intel_pt_calc_cbr(decoder
);
2500 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2503 case INTEL_PT_MODE_EXEC
:
2504 decoder
->exec_mode
= decoder
->packet
.payload
;
2507 case INTEL_PT_MODE_TSX
:
2508 intel_pt_update_in_tx(decoder
);
2512 return intel_pt_overflow(decoder
);
2514 case INTEL_PT_BAD
: /* Does not happen */
2515 return intel_pt_bug(decoder
);
2517 case INTEL_PT_TRACESTOP
:
2518 decoder
->pge
= false;
2519 decoder
->continuous_period
= false;
2520 intel_pt_clear_tx_flags(decoder
);
2521 decoder
->have_tma
= false;
2525 decoder
->last_ip
= 0;
2526 decoder
->have_last_ip
= true;
2527 intel_pt_clear_stack(&decoder
->stack
);
2528 err
= intel_pt_walk_psb(decoder
);
2532 /* Do not have a sample */
2533 decoder
->state
.type
= 0;
2539 case INTEL_PT_PSBEND
:
2543 case INTEL_PT_PTWRITE
:
2544 case INTEL_PT_PTWRITE_IP
:
2545 case INTEL_PT_EXSTOP
:
2546 case INTEL_PT_EXSTOP_IP
:
2547 case INTEL_PT_MWAIT
:
2553 case INTEL_PT_BEP_IP
:
2560 static int intel_pt_sync_ip(struct intel_pt_decoder
*decoder
)
2564 decoder
->set_fup_tx_flags
= false;
2565 decoder
->set_fup_ptw
= false;
2566 decoder
->set_fup_mwait
= false;
2567 decoder
->set_fup_pwre
= false;
2568 decoder
->set_fup_exstop
= false;
2569 decoder
->set_fup_bep
= false;
2571 if (!decoder
->branch_enable
) {
2572 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2573 decoder
->overflow
= false;
2574 decoder
->state
.type
= 0; /* Do not have a sample */
2578 intel_pt_log("Scanning for full IP\n");
2579 err
= intel_pt_walk_to_ip(decoder
);
2583 /* In hop mode, resample to get the to_ip as an "instruction" sample */
2585 decoder
->pkt_state
= INTEL_PT_STATE_RESAMPLE
;
2587 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2588 decoder
->overflow
= false;
2590 decoder
->state
.from_ip
= 0;
2591 decoder
->state
.to_ip
= decoder
->ip
;
2592 intel_pt_log_to("Setting IP", decoder
->ip
);
2597 static int intel_pt_part_psb(struct intel_pt_decoder
*decoder
)
2599 const unsigned char *end
= decoder
->buf
+ decoder
->len
;
2602 for (i
= INTEL_PT_PSB_LEN
- 1; i
; i
--) {
2603 if (i
> decoder
->len
)
2605 if (!memcmp(end
- i
, INTEL_PT_PSB_STR
, i
))
2611 static int intel_pt_rest_psb(struct intel_pt_decoder
*decoder
, int part_psb
)
2613 size_t rest_psb
= INTEL_PT_PSB_LEN
- part_psb
;
2614 const char *psb
= INTEL_PT_PSB_STR
;
2616 if (rest_psb
> decoder
->len
||
2617 memcmp(decoder
->buf
, psb
+ part_psb
, rest_psb
))
2623 static int intel_pt_get_split_psb(struct intel_pt_decoder
*decoder
,
2628 decoder
->pos
+= decoder
->len
;
2631 ret
= intel_pt_get_next_data(decoder
, false);
2635 rest_psb
= intel_pt_rest_psb(decoder
, part_psb
);
2639 decoder
->pos
-= part_psb
;
2640 decoder
->next_buf
= decoder
->buf
+ rest_psb
;
2641 decoder
->next_len
= decoder
->len
- rest_psb
;
2642 memcpy(decoder
->temp_buf
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2643 decoder
->buf
= decoder
->temp_buf
;
2644 decoder
->len
= INTEL_PT_PSB_LEN
;
2649 static int intel_pt_scan_for_psb(struct intel_pt_decoder
*decoder
)
2651 unsigned char *next
;
2654 intel_pt_log("Scanning for PSB\n");
2656 if (!decoder
->len
) {
2657 ret
= intel_pt_get_next_data(decoder
, false);
2662 next
= memmem(decoder
->buf
, decoder
->len
, INTEL_PT_PSB_STR
,
2667 part_psb
= intel_pt_part_psb(decoder
);
2669 ret
= intel_pt_get_split_psb(decoder
, part_psb
);
2673 decoder
->pos
+= decoder
->len
;
2679 decoder
->pkt_step
= next
- decoder
->buf
;
2680 return intel_pt_get_next_packet(decoder
);
2684 static int intel_pt_sync(struct intel_pt_decoder
*decoder
)
2688 decoder
->pge
= false;
2689 decoder
->continuous_period
= false;
2690 decoder
->have_last_ip
= false;
2691 decoder
->last_ip
= 0;
2693 intel_pt_clear_stack(&decoder
->stack
);
2696 err
= intel_pt_scan_for_psb(decoder
);
2700 decoder
->have_last_ip
= true;
2701 decoder
->pkt_state
= INTEL_PT_STATE_NO_IP
;
2703 err
= intel_pt_walk_psb(decoder
);
2708 decoder
->state
.type
= 0; /* Do not have a sample */
2710 * In hop mode, resample to get the PSB FUP ip as an
2711 * "instruction" sample.
2714 decoder
->pkt_state
= INTEL_PT_STATE_RESAMPLE
;
2716 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2717 } else if (decoder
->leap
) {
2719 * In leap mode, only PSB+ is decoded, so keeping leaping to the
2720 * next PSB until there is an ip.
2724 return intel_pt_sync_ip(decoder
);
2730 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder
*decoder
)
2732 uint64_t est
= decoder
->sample_insn_cnt
<< 1;
2734 if (!decoder
->cbr
|| !decoder
->max_non_turbo_ratio
)
2737 est
*= decoder
->max_non_turbo_ratio
;
2738 est
/= decoder
->cbr
;
2740 return decoder
->sample_timestamp
+ est
;
2743 const struct intel_pt_state
*intel_pt_decode(struct intel_pt_decoder
*decoder
)
2748 decoder
->state
.type
= INTEL_PT_BRANCH
;
2749 decoder
->state
.flags
= 0;
2751 switch (decoder
->pkt_state
) {
2752 case INTEL_PT_STATE_NO_PSB
:
2753 err
= intel_pt_sync(decoder
);
2755 case INTEL_PT_STATE_NO_IP
:
2756 decoder
->have_last_ip
= false;
2757 decoder
->last_ip
= 0;
2760 case INTEL_PT_STATE_ERR_RESYNC
:
2761 err
= intel_pt_sync_ip(decoder
);
2763 case INTEL_PT_STATE_IN_SYNC
:
2764 err
= intel_pt_walk_trace(decoder
);
2766 case INTEL_PT_STATE_TNT
:
2767 case INTEL_PT_STATE_TNT_CONT
:
2768 err
= intel_pt_walk_tnt(decoder
);
2770 err
= intel_pt_walk_trace(decoder
);
2772 case INTEL_PT_STATE_TIP
:
2773 case INTEL_PT_STATE_TIP_PGD
:
2774 err
= intel_pt_walk_tip(decoder
);
2776 case INTEL_PT_STATE_FUP
:
2777 err
= intel_pt_walk_fup(decoder
);
2779 err
= intel_pt_walk_fup_tip(decoder
);
2781 case INTEL_PT_STATE_FUP_NO_TIP
:
2782 err
= intel_pt_walk_fup(decoder
);
2784 err
= intel_pt_walk_trace(decoder
);
2786 case INTEL_PT_STATE_RESAMPLE
:
2787 err
= intel_pt_resample(decoder
);
2790 err
= intel_pt_bug(decoder
);
2793 } while (err
== -ENOLINK
);
2796 decoder
->state
.err
= intel_pt_ext_err(err
);
2797 decoder
->state
.from_ip
= decoder
->ip
;
2798 intel_pt_update_sample_time(decoder
);
2799 decoder
->sample_tot_cyc_cnt
= decoder
->tot_cyc_cnt
;
2801 decoder
->state
.err
= 0;
2802 if (decoder
->cbr
!= decoder
->cbr_seen
) {
2803 decoder
->cbr_seen
= decoder
->cbr
;
2804 if (!decoder
->state
.type
) {
2805 decoder
->state
.from_ip
= decoder
->ip
;
2806 decoder
->state
.to_ip
= 0;
2808 decoder
->state
.type
|= INTEL_PT_CBR_CHG
;
2809 decoder
->state
.cbr_payload
= decoder
->cbr_payload
;
2810 decoder
->state
.cbr
= decoder
->cbr
;
2812 if (intel_pt_sample_time(decoder
->pkt_state
)) {
2813 intel_pt_update_sample_time(decoder
);
2814 if (decoder
->sample_cyc
)
2815 decoder
->sample_tot_cyc_cnt
= decoder
->tot_cyc_cnt
;
2819 decoder
->state
.timestamp
= decoder
->sample_timestamp
;
2820 decoder
->state
.est_timestamp
= intel_pt_est_timestamp(decoder
);
2821 decoder
->state
.cr3
= decoder
->cr3
;
2822 decoder
->state
.tot_insn_cnt
= decoder
->tot_insn_cnt
;
2823 decoder
->state
.tot_cyc_cnt
= decoder
->sample_tot_cyc_cnt
;
2825 return &decoder
->state
;
2829 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2830 * @buf: pointer to buffer pointer
2831 * @len: size of buffer
2833 * Updates the buffer pointer to point to the start of the next PSB packet if
2834 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2835 * @len is adjusted accordingly.
2837 * Return: %true if a PSB packet is found, %false otherwise.
2839 static bool intel_pt_next_psb(unsigned char **buf
, size_t *len
)
2841 unsigned char *next
;
2843 next
= memmem(*buf
, *len
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2845 *len
-= next
- *buf
;
2853 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2855 * @buf: pointer to buffer pointer
2856 * @len: size of buffer
2858 * Updates the buffer pointer to point to the start of the following PSB packet
2859 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2860 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2862 * Return: %true if a PSB packet is found, %false otherwise.
2864 static bool intel_pt_step_psb(unsigned char **buf
, size_t *len
)
2866 unsigned char *next
;
2871 next
= memmem(*buf
+ 1, *len
- 1, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2873 *len
-= next
- *buf
;
2881 * intel_pt_last_psb - find the last PSB packet in a buffer.
2883 * @len: size of buffer
2885 * This function finds the last PSB in a buffer.
2887 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2889 static unsigned char *intel_pt_last_psb(unsigned char *buf
, size_t len
)
2891 const char *n
= INTEL_PT_PSB_STR
;
2895 if (len
< INTEL_PT_PSB_LEN
)
2898 k
= len
- INTEL_PT_PSB_LEN
+ 1;
2900 p
= memrchr(buf
, n
[0], k
);
2903 if (!memcmp(p
+ 1, n
+ 1, INTEL_PT_PSB_LEN
- 1))
2912 * intel_pt_next_tsc - find and return next TSC.
2914 * @len: size of buffer
2915 * @tsc: TSC value returned
2916 * @rem: returns remaining size when TSC is found
2918 * Find a TSC packet in @buf and return the TSC value. This function assumes
2919 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2920 * PSBEND packet is found.
2922 * Return: %true if TSC is found, false otherwise.
2924 static bool intel_pt_next_tsc(unsigned char *buf
, size_t len
, uint64_t *tsc
,
2927 enum intel_pt_pkt_ctx ctx
= INTEL_PT_NO_CTX
;
2928 struct intel_pt_pkt packet
;
2932 ret
= intel_pt_get_packet(buf
, len
, &packet
, &ctx
);
2935 if (packet
.type
== INTEL_PT_TSC
) {
2936 *tsc
= packet
.payload
;
2940 if (packet
.type
== INTEL_PT_PSBEND
)
2949 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2950 * @tsc1: first TSC to compare
2951 * @tsc2: second TSC to compare
2953 * This function compares 7-byte TSC values allowing for the possibility that
2954 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2955 * around so for that purpose this function assumes the absolute difference is
2956 * less than half the maximum difference.
2958 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2961 static int intel_pt_tsc_cmp(uint64_t tsc1
, uint64_t tsc2
)
2963 const uint64_t halfway
= (1ULL << 55);
2969 if (tsc2
- tsc1
< halfway
)
2974 if (tsc1
- tsc2
< halfway
)
2981 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2984 * adj_for_padding - adjust overlap to account for padding.
2985 * @buf_b: second buffer
2986 * @buf_a: first buffer
2987 * @len_a: size of first buffer
2989 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2992 * Return: A pointer into @buf_b from where non-overlapped data starts
2994 static unsigned char *adj_for_padding(unsigned char *buf_b
,
2995 unsigned char *buf_a
, size_t len_a
)
2997 unsigned char *p
= buf_b
- MAX_PADDING
;
2998 unsigned char *q
= buf_a
+ len_a
- MAX_PADDING
;
3001 for (i
= MAX_PADDING
; i
; i
--, p
++, q
++) {
3010 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
3012 * @buf_a: first buffer
3013 * @len_a: size of first buffer
3014 * @buf_b: second buffer
3015 * @len_b: size of second buffer
3016 * @consecutive: returns true if there is data in buf_b that is consecutive
3019 * If the trace contains TSC we can look at the last TSC of @buf_a and the
3020 * first TSC of @buf_b in order to determine if the buffers overlap, and then
3021 * walk forward in @buf_b until a later TSC is found. A precondition is that
3022 * @buf_a and @buf_b are positioned at a PSB.
3024 * Return: A pointer into @buf_b from where non-overlapped data starts, or
3025 * @buf_b + @len_b if there is no non-overlapped data.
3027 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a
,
3029 unsigned char *buf_b
,
3030 size_t len_b
, bool *consecutive
)
3032 uint64_t tsc_a
, tsc_b
;
3034 size_t len
, rem_a
, rem_b
;
3036 p
= intel_pt_last_psb(buf_a
, len_a
);
3038 return buf_b
; /* No PSB in buf_a => no overlap */
3040 len
= len_a
- (p
- buf_a
);
3041 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
)) {
3042 /* The last PSB+ in buf_a is incomplete, so go back one more */
3044 p
= intel_pt_last_psb(buf_a
, len_a
);
3046 return buf_b
; /* No full PSB+ => assume no overlap */
3047 len
= len_a
- (p
- buf_a
);
3048 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
))
3049 return buf_b
; /* No TSC in buf_a => assume no overlap */
3053 /* Ignore PSB+ with no TSC */
3054 if (intel_pt_next_tsc(buf_b
, len_b
, &tsc_b
, &rem_b
)) {
3055 int cmp
= intel_pt_tsc_cmp(tsc_a
, tsc_b
);
3057 /* Same TSC, so buffers are consecutive */
3058 if (!cmp
&& rem_b
>= rem_a
) {
3059 unsigned char *start
;
3061 *consecutive
= true;
3062 start
= buf_b
+ len_b
- (rem_b
- rem_a
);
3063 return adj_for_padding(start
, buf_a
, len_a
);
3066 return buf_b
; /* tsc_a < tsc_b => no overlap */
3069 if (!intel_pt_step_psb(&buf_b
, &len_b
))
3070 return buf_b
+ len_b
; /* No PSB in buf_b => no data */
3075 * intel_pt_find_overlap - determine start of non-overlapped trace data.
3076 * @buf_a: first buffer
3077 * @len_a: size of first buffer
3078 * @buf_b: second buffer
3079 * @len_b: size of second buffer
3080 * @have_tsc: can use TSC packets to detect overlap
3081 * @consecutive: returns true if there is data in buf_b that is consecutive
3084 * When trace samples or snapshots are recorded there is the possibility that
3085 * the data overlaps. Note that, for the purposes of decoding, data is only
3086 * useful if it begins with a PSB packet.
3088 * Return: A pointer into @buf_b from where non-overlapped data starts, or
3089 * @buf_b + @len_b if there is no non-overlapped data.
3091 unsigned char *intel_pt_find_overlap(unsigned char *buf_a
, size_t len_a
,
3092 unsigned char *buf_b
, size_t len_b
,
3093 bool have_tsc
, bool *consecutive
)
3095 unsigned char *found
;
3097 /* Buffer 'b' must start at PSB so throw away everything before that */
3098 if (!intel_pt_next_psb(&buf_b
, &len_b
))
3099 return buf_b
+ len_b
; /* No PSB */
3101 if (!intel_pt_next_psb(&buf_a
, &len_a
))
3102 return buf_b
; /* No overlap */
3105 found
= intel_pt_find_overlap_tsc(buf_a
, len_a
, buf_b
, len_b
,
3112 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
3113 * we can ignore the first part of buffer 'a'.
3115 while (len_b
< len_a
) {
3116 if (!intel_pt_step_psb(&buf_a
, &len_a
))
3117 return buf_b
; /* No overlap */
3120 /* Now len_b >= len_a */
3122 /* Potential overlap so check the bytes */
3123 found
= memmem(buf_a
, len_a
, buf_b
, len_a
);
3125 *consecutive
= true;
3126 return adj_for_padding(buf_b
+ len_a
, buf_a
, len_a
);
3129 /* Try again at next PSB in buffer 'a' */
3130 if (!intel_pt_step_psb(&buf_a
, &len_a
))
3131 return buf_b
; /* No overlap */
3136 * struct fast_forward_data - data used by intel_pt_ff_cb().
3137 * @timestamp: timestamp to fast forward towards
3138 * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
3139 * the fast forward timestamp.
3141 struct fast_forward_data
{
3143 uint64_t buf_timestamp
;
3147 * intel_pt_ff_cb - fast forward lookahead callback.
3148 * @buffer: Intel PT trace buffer
3149 * @data: opaque pointer to fast forward data (struct fast_forward_data)
3151 * Determine if @buffer trace is past the fast forward timestamp.
3153 * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
3154 * timestamp, and 0 otherwise.
3156 static int intel_pt_ff_cb(struct intel_pt_buffer
*buffer
, void *data
)
3158 struct fast_forward_data
*d
= data
;
3164 buf
= (unsigned char *)buffer
->buf
;
3167 if (!intel_pt_next_psb(&buf
, &len
) ||
3168 !intel_pt_next_tsc(buf
, len
, &tsc
, &rem
))
3171 tsc
= intel_pt_8b_tsc(tsc
, buffer
->ref_timestamp
);
3173 intel_pt_log("Buffer 1st timestamp " x64_fmt
" ref timestamp " x64_fmt
"\n",
3174 tsc
, buffer
->ref_timestamp
);
3177 * If the buffer contains a timestamp earlier that the fast forward
3178 * timestamp, then record it, else stop.
3180 if (tsc
< d
->timestamp
)
3181 d
->buf_timestamp
= buffer
->ref_timestamp
;
3189 * intel_pt_fast_forward - reposition decoder forwards.
3190 * @decoder: Intel PT decoder
3191 * @timestamp: timestamp to fast forward towards
3193 * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
3195 * Return: 0 on success or negative error code on failure.
3197 int intel_pt_fast_forward(struct intel_pt_decoder
*decoder
, uint64_t timestamp
)
3199 struct fast_forward_data d
= { .timestamp
= timestamp
};
3204 intel_pt_log("Fast forward towards timestamp " x64_fmt
"\n", timestamp
);
3206 /* Find buffer timestamp of buffer to fast forward to */
3207 err
= decoder
->lookahead(decoder
->data
, intel_pt_ff_cb
, &d
);
3211 /* Walk to buffer with same buffer timestamp */
3212 if (d
.buf_timestamp
) {
3214 decoder
->pos
+= decoder
->len
;
3216 err
= intel_pt_get_next_data(decoder
, true);
3217 /* -ENOLINK means non-consecutive trace */
3218 if (err
&& err
!= -ENOLINK
)
3220 } while (decoder
->buf_timestamp
!= d
.buf_timestamp
);
3226 buf
= (unsigned char *)decoder
->buf
;
3229 if (!intel_pt_next_psb(&buf
, &len
))
3233 * Walk PSBs while the PSB timestamp is less than the fast forward
3240 if (!intel_pt_next_tsc(buf
, len
, &tsc
, &rem
))
3242 tsc
= intel_pt_8b_tsc(tsc
, decoder
->buf_timestamp
);
3244 * A TSC packet can slip past MTC packets but, after fast
3245 * forward, decoding starts at the TSC timestamp. That means
3246 * the timestamps may not be exactly the same as the timestamps
3247 * that would have been decoded without fast forward.
3249 if (tsc
< timestamp
) {
3250 intel_pt_log("Fast forward to next PSB timestamp " x64_fmt
"\n", tsc
);
3251 decoder
->pos
+= decoder
->len
- len
;
3254 intel_pt_reposition(decoder
);
3258 } while (intel_pt_step_psb(&buf
, &len
));