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>
29 #include "../auxtrace.h"
31 #include "intel-pt-insn-decoder.h"
32 #include "intel-pt-pkt-decoder.h"
33 #include "intel-pt-decoder.h"
34 #include "intel-pt-log.h"
36 #define INTEL_PT_BLK_SIZE 1024
38 #define BIT63 (((uint64_t)1 << 63))
40 #define INTEL_PT_RETURN 1
42 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
43 #define INTEL_PT_MAX_LOOPS 10000
46 struct intel_pt_blk
*prev
;
47 uint64_t ip
[INTEL_PT_BLK_SIZE
];
50 struct intel_pt_stack
{
51 struct intel_pt_blk
*blk
;
52 struct intel_pt_blk
*spare
;
56 enum intel_pt_pkt_state
{
57 INTEL_PT_STATE_NO_PSB
,
59 INTEL_PT_STATE_ERR_RESYNC
,
60 INTEL_PT_STATE_IN_SYNC
,
61 INTEL_PT_STATE_TNT_CONT
,
64 INTEL_PT_STATE_TIP_PGD
,
66 INTEL_PT_STATE_FUP_NO_TIP
,
69 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state
)
72 case INTEL_PT_STATE_NO_PSB
:
73 case INTEL_PT_STATE_NO_IP
:
74 case INTEL_PT_STATE_ERR_RESYNC
:
75 case INTEL_PT_STATE_IN_SYNC
:
76 case INTEL_PT_STATE_TNT_CONT
:
78 case INTEL_PT_STATE_TNT
:
79 case INTEL_PT_STATE_TIP
:
80 case INTEL_PT_STATE_TIP_PGD
:
81 case INTEL_PT_STATE_FUP
:
82 case INTEL_PT_STATE_FUP_NO_TIP
:
89 #ifdef INTEL_PT_STRICT
90 #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
91 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
92 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
93 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
95 #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
96 #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
97 #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
98 #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
101 struct intel_pt_decoder
{
102 int (*get_trace
)(struct intel_pt_buffer
*buffer
, void *data
);
103 int (*walk_insn
)(struct intel_pt_insn
*intel_pt_insn
,
104 uint64_t *insn_cnt_ptr
, uint64_t *ip
, uint64_t to_ip
,
105 uint64_t max_insn_cnt
, void *data
);
106 bool (*pgd_ip
)(uint64_t ip
, void *data
);
107 int (*lookahead
)(void *data
, intel_pt_lookahead_cb_t cb
, void *cb_data
);
109 struct intel_pt_state state
;
110 const unsigned char *buf
;
112 bool return_compression
;
121 enum intel_pt_param_flags flags
;
127 uint64_t tsc_timestamp
;
128 uint64_t ref_timestamp
;
129 uint64_t sample_timestamp
;
131 uint64_t ctc_timestamp
;
134 uint64_t cyc_ref_timestamp
;
136 uint32_t tsc_ctc_ratio_n
;
137 uint32_t tsc_ctc_ratio_d
;
138 uint32_t tsc_ctc_mult
;
140 uint32_t ctc_rem_mask
;
142 struct intel_pt_stack stack
;
143 enum intel_pt_pkt_state pkt_state
;
144 struct intel_pt_pkt packet
;
145 struct intel_pt_pkt tnt
;
148 int last_packet_type
;
150 unsigned int cbr_seen
;
151 unsigned int max_non_turbo_ratio
;
152 double max_non_turbo_ratio_fp
;
153 double cbr_cyc_to_tsc
;
154 double calc_cyc_to_tsc
;
155 bool have_calc_cyc_to_tsc
;
157 unsigned int insn_bytes
;
159 enum intel_pt_period_type period_type
;
160 uint64_t tot_insn_cnt
;
161 uint64_t period_insn_cnt
;
162 uint64_t period_mask
;
163 uint64_t period_ticks
;
164 uint64_t last_masked_timestamp
;
165 uint64_t tot_cyc_cnt
;
166 uint64_t sample_tot_cyc_cnt
;
167 uint64_t base_cyc_cnt
;
168 uint64_t cyc_cnt_timestamp
;
170 bool continuous_period
;
172 bool set_fup_tx_flags
;
178 unsigned int fup_tx_flags
;
179 unsigned int tx_flags
;
180 uint64_t fup_ptw_payload
;
181 uint64_t fup_mwait_payload
;
182 uint64_t fup_pwre_payload
;
183 uint64_t cbr_payload
;
184 uint64_t timestamp_insn_cnt
;
185 uint64_t sample_insn_cnt
;
190 const unsigned char *next_buf
;
192 unsigned char temp_buf
[INTEL_PT_PKT_MAX_SZ
];
195 static uint64_t intel_pt_lower_power_of_2(uint64_t x
)
199 for (i
= 0; x
!= 1; i
++)
205 static void intel_pt_setup_period(struct intel_pt_decoder
*decoder
)
207 if (decoder
->period_type
== INTEL_PT_PERIOD_TICKS
) {
210 period
= intel_pt_lower_power_of_2(decoder
->period
);
211 decoder
->period_mask
= ~(period
- 1);
212 decoder
->period_ticks
= period
;
216 static uint64_t multdiv(uint64_t t
, uint32_t n
, uint32_t d
)
220 return (t
/ d
) * n
+ ((t
% d
) * n
) / d
;
223 struct intel_pt_decoder
*intel_pt_decoder_new(struct intel_pt_params
*params
)
225 struct intel_pt_decoder
*decoder
;
227 if (!params
->get_trace
|| !params
->walk_insn
)
230 decoder
= zalloc(sizeof(struct intel_pt_decoder
));
234 decoder
->get_trace
= params
->get_trace
;
235 decoder
->walk_insn
= params
->walk_insn
;
236 decoder
->pgd_ip
= params
->pgd_ip
;
237 decoder
->lookahead
= params
->lookahead
;
238 decoder
->data
= params
->data
;
239 decoder
->return_compression
= params
->return_compression
;
240 decoder
->branch_enable
= params
->branch_enable
;
242 decoder
->flags
= params
->flags
;
244 decoder
->period
= params
->period
;
245 decoder
->period_type
= params
->period_type
;
247 decoder
->max_non_turbo_ratio
= params
->max_non_turbo_ratio
;
248 decoder
->max_non_turbo_ratio_fp
= params
->max_non_turbo_ratio
;
250 intel_pt_setup_period(decoder
);
252 decoder
->mtc_shift
= params
->mtc_period
;
253 decoder
->ctc_rem_mask
= (1 << decoder
->mtc_shift
) - 1;
255 decoder
->tsc_ctc_ratio_n
= params
->tsc_ctc_ratio_n
;
256 decoder
->tsc_ctc_ratio_d
= params
->tsc_ctc_ratio_d
;
258 if (!decoder
->tsc_ctc_ratio_n
)
259 decoder
->tsc_ctc_ratio_d
= 0;
261 if (decoder
->tsc_ctc_ratio_d
) {
262 if (!(decoder
->tsc_ctc_ratio_n
% decoder
->tsc_ctc_ratio_d
))
263 decoder
->tsc_ctc_mult
= decoder
->tsc_ctc_ratio_n
/
264 decoder
->tsc_ctc_ratio_d
;
268 * A TSC packet can slip past MTC packets so that the timestamp appears
269 * to go backwards. One estimate is that can be up to about 40 CPU
270 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
271 * slippage an order of magnitude more to be on the safe side.
273 decoder
->tsc_slip
= 0x10000;
275 intel_pt_log("timestamp: mtc_shift %u\n", decoder
->mtc_shift
);
276 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder
->tsc_ctc_ratio_n
);
277 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder
->tsc_ctc_ratio_d
);
278 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder
->tsc_ctc_mult
);
279 intel_pt_log("timestamp: tsc_slip %#x\n", decoder
->tsc_slip
);
284 static void intel_pt_pop_blk(struct intel_pt_stack
*stack
)
286 struct intel_pt_blk
*blk
= stack
->blk
;
288 stack
->blk
= blk
->prev
;
295 static uint64_t intel_pt_pop(struct intel_pt_stack
*stack
)
300 intel_pt_pop_blk(stack
);
303 stack
->pos
= INTEL_PT_BLK_SIZE
;
305 return stack
->blk
->ip
[--stack
->pos
];
308 static int intel_pt_alloc_blk(struct intel_pt_stack
*stack
)
310 struct intel_pt_blk
*blk
;
316 blk
= malloc(sizeof(struct intel_pt_blk
));
321 blk
->prev
= stack
->blk
;
327 static int intel_pt_push(struct intel_pt_stack
*stack
, uint64_t ip
)
331 if (!stack
->blk
|| stack
->pos
== INTEL_PT_BLK_SIZE
) {
332 err
= intel_pt_alloc_blk(stack
);
337 stack
->blk
->ip
[stack
->pos
++] = ip
;
341 static void intel_pt_clear_stack(struct intel_pt_stack
*stack
)
344 intel_pt_pop_blk(stack
);
348 static void intel_pt_free_stack(struct intel_pt_stack
*stack
)
350 intel_pt_clear_stack(stack
);
352 zfree(&stack
->spare
);
355 void intel_pt_decoder_free(struct intel_pt_decoder
*decoder
)
357 intel_pt_free_stack(&decoder
->stack
);
361 static int intel_pt_ext_err(int code
)
365 return INTEL_PT_ERR_NOMEM
;
367 return INTEL_PT_ERR_INTERN
;
369 return INTEL_PT_ERR_BADPKT
;
371 return INTEL_PT_ERR_NODATA
;
373 return INTEL_PT_ERR_NOINSN
;
375 return INTEL_PT_ERR_MISMAT
;
377 return INTEL_PT_ERR_OVR
;
379 return INTEL_PT_ERR_LOST
;
381 return INTEL_PT_ERR_NELOOP
;
383 return INTEL_PT_ERR_UNK
;
387 static const char *intel_pt_err_msgs
[] = {
388 [INTEL_PT_ERR_NOMEM
] = "Memory allocation failed",
389 [INTEL_PT_ERR_INTERN
] = "Internal error",
390 [INTEL_PT_ERR_BADPKT
] = "Bad packet",
391 [INTEL_PT_ERR_NODATA
] = "No more data",
392 [INTEL_PT_ERR_NOINSN
] = "Failed to get instruction",
393 [INTEL_PT_ERR_MISMAT
] = "Trace doesn't match instruction",
394 [INTEL_PT_ERR_OVR
] = "Overflow packet",
395 [INTEL_PT_ERR_LOST
] = "Lost trace data",
396 [INTEL_PT_ERR_UNK
] = "Unknown error!",
397 [INTEL_PT_ERR_NELOOP
] = "Never-ending loop",
400 int intel_pt__strerror(int code
, char *buf
, size_t buflen
)
402 if (code
< 1 || code
>= INTEL_PT_ERR_MAX
)
403 code
= INTEL_PT_ERR_UNK
;
404 strlcpy(buf
, intel_pt_err_msgs
[code
], buflen
);
408 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt
*packet
,
413 switch (packet
->count
) {
415 ip
= (last_ip
& (uint64_t)0xffffffffffff0000ULL
) |
419 ip
= (last_ip
& (uint64_t)0xffffffff00000000ULL
) |
423 ip
= packet
->payload
;
424 /* Sign-extend 6-byte ip */
425 if (ip
& (uint64_t)0x800000000000ULL
)
426 ip
|= (uint64_t)0xffff000000000000ULL
;
429 ip
= (last_ip
& (uint64_t)0xffff000000000000ULL
) |
433 ip
= packet
->payload
;
442 static inline void intel_pt_set_last_ip(struct intel_pt_decoder
*decoder
)
444 decoder
->last_ip
= intel_pt_calc_ip(&decoder
->packet
, decoder
->last_ip
);
445 decoder
->have_last_ip
= true;
448 static inline void intel_pt_set_ip(struct intel_pt_decoder
*decoder
)
450 intel_pt_set_last_ip(decoder
);
451 decoder
->ip
= decoder
->last_ip
;
454 static void intel_pt_decoder_log_packet(struct intel_pt_decoder
*decoder
)
456 intel_pt_log_packet(&decoder
->packet
, decoder
->pkt_len
, decoder
->pos
,
460 static int intel_pt_bug(struct intel_pt_decoder
*decoder
)
462 intel_pt_log("ERROR: Internal error\n");
463 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
467 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder
*decoder
)
469 decoder
->tx_flags
= 0;
472 static inline void intel_pt_update_in_tx(struct intel_pt_decoder
*decoder
)
474 decoder
->tx_flags
= decoder
->packet
.payload
& INTEL_PT_IN_TX
;
477 static int intel_pt_bad_packet(struct intel_pt_decoder
*decoder
)
479 intel_pt_clear_tx_flags(decoder
);
480 decoder
->have_tma
= false;
481 decoder
->pkt_len
= 1;
482 decoder
->pkt_step
= 1;
483 intel_pt_decoder_log_packet(decoder
);
484 if (decoder
->pkt_state
!= INTEL_PT_STATE_NO_PSB
) {
485 intel_pt_log("ERROR: Bad packet\n");
486 decoder
->pkt_state
= INTEL_PT_STATE_ERR1
;
491 static inline void intel_pt_update_sample_time(struct intel_pt_decoder
*decoder
)
493 decoder
->sample_timestamp
= decoder
->timestamp
;
494 decoder
->sample_insn_cnt
= decoder
->timestamp_insn_cnt
;
497 static int intel_pt_get_data(struct intel_pt_decoder
*decoder
)
499 struct intel_pt_buffer buffer
= { .buf
= 0, };
502 decoder
->pkt_step
= 0;
504 intel_pt_log("Getting more data\n");
505 ret
= decoder
->get_trace(&buffer
, decoder
->data
);
508 decoder
->buf
= buffer
.buf
;
509 decoder
->len
= buffer
.len
;
511 intel_pt_log("No more data\n");
514 if (!buffer
.consecutive
) {
516 decoder
->pkt_state
= INTEL_PT_STATE_NO_PSB
;
517 decoder
->ref_timestamp
= buffer
.ref_timestamp
;
518 decoder
->timestamp
= 0;
519 decoder
->have_tma
= false;
520 decoder
->state
.trace_nr
= buffer
.trace_nr
;
521 intel_pt_log("Reference timestamp 0x%" PRIx64
"\n",
522 decoder
->ref_timestamp
);
529 static int intel_pt_get_next_data(struct intel_pt_decoder
*decoder
)
531 if (!decoder
->next_buf
)
532 return intel_pt_get_data(decoder
);
534 decoder
->buf
= decoder
->next_buf
;
535 decoder
->len
= decoder
->next_len
;
536 decoder
->next_buf
= 0;
537 decoder
->next_len
= 0;
541 static int intel_pt_get_split_packet(struct intel_pt_decoder
*decoder
)
543 unsigned char *buf
= decoder
->temp_buf
;
544 size_t old_len
, len
, n
;
547 old_len
= decoder
->len
;
549 memcpy(buf
, decoder
->buf
, len
);
551 ret
= intel_pt_get_data(decoder
);
553 decoder
->pos
+= old_len
;
554 return ret
< 0 ? ret
: -EINVAL
;
557 n
= INTEL_PT_PKT_MAX_SZ
- len
;
558 if (n
> decoder
->len
)
560 memcpy(buf
+ len
, decoder
->buf
, n
);
563 ret
= intel_pt_get_packet(buf
, len
, &decoder
->packet
);
564 if (ret
< (int)old_len
) {
565 decoder
->next_buf
= decoder
->buf
;
566 decoder
->next_len
= decoder
->len
;
568 decoder
->len
= old_len
;
569 return intel_pt_bad_packet(decoder
);
572 decoder
->next_buf
= decoder
->buf
+ (ret
- old_len
);
573 decoder
->next_len
= decoder
->len
- (ret
- old_len
);
581 struct intel_pt_pkt_info
{
582 struct intel_pt_decoder
*decoder
;
583 struct intel_pt_pkt packet
;
586 int last_packet_type
;
590 typedef int (*intel_pt_pkt_cb_t
)(struct intel_pt_pkt_info
*pkt_info
);
592 /* Lookahead packets in current buffer */
593 static int intel_pt_pkt_lookahead(struct intel_pt_decoder
*decoder
,
594 intel_pt_pkt_cb_t cb
, void *data
)
596 struct intel_pt_pkt_info pkt_info
;
597 const unsigned char *buf
= decoder
->buf
;
598 size_t len
= decoder
->len
;
601 pkt_info
.decoder
= decoder
;
602 pkt_info
.pos
= decoder
->pos
;
603 pkt_info
.pkt_len
= decoder
->pkt_step
;
604 pkt_info
.last_packet_type
= decoder
->last_packet_type
;
605 pkt_info
.data
= data
;
609 pkt_info
.pos
+= pkt_info
.pkt_len
;
610 buf
+= pkt_info
.pkt_len
;
611 len
-= pkt_info
.pkt_len
;
614 return INTEL_PT_NEED_MORE_BYTES
;
616 ret
= intel_pt_get_packet(buf
, len
, &pkt_info
.packet
);
618 return INTEL_PT_NEED_MORE_BYTES
;
622 pkt_info
.pkt_len
= ret
;
623 } while (pkt_info
.packet
.type
== INTEL_PT_PAD
);
629 pkt_info
.last_packet_type
= pkt_info
.packet
.type
;
633 struct intel_pt_calc_cyc_to_tsc_info
{
637 uint64_t ctc_timestamp
;
639 uint64_t tsc_timestamp
;
644 double cbr_cyc_to_tsc
;
648 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
649 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
650 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
651 * packet by copying the missing bits from the current MTC assuming the least
652 * difference between the two, and that the current MTC comes after last_mtc.
654 static void intel_pt_fixup_last_mtc(uint32_t mtc
, int mtc_shift
,
657 uint32_t first_missing_bit
= 1U << (16 - mtc_shift
);
658 uint32_t mask
= ~(first_missing_bit
- 1);
660 *last_mtc
|= mtc
& mask
;
661 if (*last_mtc
>= mtc
) {
662 *last_mtc
-= first_missing_bit
;
667 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info
*pkt_info
)
669 struct intel_pt_decoder
*decoder
= pkt_info
->decoder
;
670 struct intel_pt_calc_cyc_to_tsc_info
*data
= pkt_info
->data
;
674 uint32_t mtc
, mtc_delta
, ctc
, fc
, ctc_rem
;
676 switch (pkt_info
->packet
.type
) {
678 case INTEL_PT_TIP_PGE
:
683 case INTEL_PT_MODE_EXEC
:
684 case INTEL_PT_MODE_TSX
:
685 case INTEL_PT_PSBEND
:
689 case INTEL_PT_PTWRITE
:
690 case INTEL_PT_PTWRITE_IP
:
697 mtc
= pkt_info
->packet
.payload
;
698 if (decoder
->mtc_shift
> 8 && data
->fixup_last_mtc
) {
699 data
->fixup_last_mtc
= false;
700 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
703 if (mtc
> data
->last_mtc
)
704 mtc_delta
= mtc
- data
->last_mtc
;
706 mtc_delta
= mtc
+ 256 - data
->last_mtc
;
707 data
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
708 data
->last_mtc
= mtc
;
710 if (decoder
->tsc_ctc_mult
) {
711 timestamp
= data
->ctc_timestamp
+
712 data
->ctc_delta
* decoder
->tsc_ctc_mult
;
714 timestamp
= data
->ctc_timestamp
+
715 multdiv(data
->ctc_delta
,
716 decoder
->tsc_ctc_ratio_n
,
717 decoder
->tsc_ctc_ratio_d
);
720 if (timestamp
< data
->timestamp
)
723 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
724 data
->timestamp
= timestamp
;
732 * For now, do not support using TSC packets - refer
733 * intel_pt_calc_cyc_to_tsc().
737 timestamp
= pkt_info
->packet
.payload
|
738 (data
->timestamp
& (0xffULL
<< 56));
739 if (data
->from_mtc
&& timestamp
< data
->timestamp
&&
740 data
->timestamp
- timestamp
< decoder
->tsc_slip
)
742 if (timestamp
< data
->timestamp
)
743 timestamp
+= (1ULL << 56);
744 if (pkt_info
->last_packet_type
!= INTEL_PT_CYC
) {
747 data
->tsc_timestamp
= timestamp
;
748 data
->timestamp
= timestamp
;
757 if (!decoder
->tsc_ctc_ratio_d
)
760 ctc
= pkt_info
->packet
.payload
;
761 fc
= pkt_info
->packet
.count
;
762 ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
764 data
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
766 data
->ctc_timestamp
= data
->tsc_timestamp
- fc
;
767 if (decoder
->tsc_ctc_mult
) {
768 data
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
770 data
->ctc_timestamp
-=
771 multdiv(ctc_rem
, decoder
->tsc_ctc_ratio_n
,
772 decoder
->tsc_ctc_ratio_d
);
776 data
->have_tma
= true;
777 data
->fixup_last_mtc
= true;
782 data
->cycle_cnt
+= pkt_info
->packet
.payload
;
786 cbr
= pkt_info
->packet
.payload
;
787 if (data
->cbr
&& data
->cbr
!= cbr
)
790 data
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
793 case INTEL_PT_TIP_PGD
:
794 case INTEL_PT_TRACESTOP
:
795 case INTEL_PT_EXSTOP
:
796 case INTEL_PT_EXSTOP_IP
:
801 case INTEL_PT_BAD
: /* Does not happen */
806 if (!data
->cbr
&& decoder
->cbr
) {
807 data
->cbr
= decoder
->cbr
;
808 data
->cbr_cyc_to_tsc
= decoder
->cbr_cyc_to_tsc
;
811 if (!data
->cycle_cnt
)
814 cyc_to_tsc
= (double)(timestamp
- decoder
->timestamp
) / data
->cycle_cnt
;
816 if (data
->cbr
&& cyc_to_tsc
> data
->cbr_cyc_to_tsc
&&
817 cyc_to_tsc
/ data
->cbr_cyc_to_tsc
> 1.25) {
818 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt
"\n",
819 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
823 decoder
->calc_cyc_to_tsc
= cyc_to_tsc
;
824 decoder
->have_calc_cyc_to_tsc
= true;
827 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt
"\n",
828 cyc_to_tsc
, data
->cbr_cyc_to_tsc
, pkt_info
->pos
);
830 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt
"\n",
831 cyc_to_tsc
, pkt_info
->pos
);
837 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder
*decoder
,
840 struct intel_pt_calc_cyc_to_tsc_info data
= {
843 .last_mtc
= decoder
->last_mtc
,
844 .ctc_timestamp
= decoder
->ctc_timestamp
,
845 .ctc_delta
= decoder
->ctc_delta
,
846 .tsc_timestamp
= decoder
->tsc_timestamp
,
847 .timestamp
= decoder
->timestamp
,
848 .have_tma
= decoder
->have_tma
,
849 .fixup_last_mtc
= decoder
->fixup_last_mtc
,
850 .from_mtc
= from_mtc
,
855 * For now, do not support using TSC packets for at least the reasons:
856 * 1) timing might have stopped
857 * 2) TSC packets within PSB+ can slip against CYC packets
862 intel_pt_pkt_lookahead(decoder
, intel_pt_calc_cyc_cb
, &data
);
865 static int intel_pt_get_next_packet(struct intel_pt_decoder
*decoder
)
869 decoder
->last_packet_type
= decoder
->packet
.type
;
872 decoder
->pos
+= decoder
->pkt_step
;
873 decoder
->buf
+= decoder
->pkt_step
;
874 decoder
->len
-= decoder
->pkt_step
;
877 ret
= intel_pt_get_next_data(decoder
);
882 ret
= intel_pt_get_packet(decoder
->buf
, decoder
->len
,
884 if (ret
== INTEL_PT_NEED_MORE_BYTES
&& BITS_PER_LONG
== 32 &&
885 decoder
->len
< INTEL_PT_PKT_MAX_SZ
&& !decoder
->next_buf
) {
886 ret
= intel_pt_get_split_packet(decoder
);
891 return intel_pt_bad_packet(decoder
);
893 decoder
->pkt_len
= ret
;
894 decoder
->pkt_step
= ret
;
895 intel_pt_decoder_log_packet(decoder
);
896 } while (decoder
->packet
.type
== INTEL_PT_PAD
);
901 static uint64_t intel_pt_next_period(struct intel_pt_decoder
*decoder
)
903 uint64_t timestamp
, masked_timestamp
;
905 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
906 masked_timestamp
= timestamp
& decoder
->period_mask
;
907 if (decoder
->continuous_period
) {
908 if (masked_timestamp
> decoder
->last_masked_timestamp
)
912 masked_timestamp
= timestamp
& decoder
->period_mask
;
913 if (masked_timestamp
> decoder
->last_masked_timestamp
) {
914 decoder
->last_masked_timestamp
= masked_timestamp
;
915 decoder
->continuous_period
= true;
919 if (masked_timestamp
< decoder
->last_masked_timestamp
)
920 return decoder
->period_ticks
;
922 return decoder
->period_ticks
- (timestamp
- masked_timestamp
);
925 static uint64_t intel_pt_next_sample(struct intel_pt_decoder
*decoder
)
927 switch (decoder
->period_type
) {
928 case INTEL_PT_PERIOD_INSTRUCTIONS
:
929 return decoder
->period
- decoder
->period_insn_cnt
;
930 case INTEL_PT_PERIOD_TICKS
:
931 return intel_pt_next_period(decoder
);
932 case INTEL_PT_PERIOD_NONE
:
933 case INTEL_PT_PERIOD_MTC
:
939 static void intel_pt_sample_insn(struct intel_pt_decoder
*decoder
)
941 uint64_t timestamp
, masked_timestamp
;
943 switch (decoder
->period_type
) {
944 case INTEL_PT_PERIOD_INSTRUCTIONS
:
945 decoder
->period_insn_cnt
= 0;
947 case INTEL_PT_PERIOD_TICKS
:
948 timestamp
= decoder
->timestamp
+ decoder
->timestamp_insn_cnt
;
949 masked_timestamp
= timestamp
& decoder
->period_mask
;
950 if (masked_timestamp
> decoder
->last_masked_timestamp
)
951 decoder
->last_masked_timestamp
= masked_timestamp
;
953 decoder
->last_masked_timestamp
+= decoder
->period_ticks
;
955 case INTEL_PT_PERIOD_NONE
:
956 case INTEL_PT_PERIOD_MTC
:
961 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
964 static int intel_pt_walk_insn(struct intel_pt_decoder
*decoder
,
965 struct intel_pt_insn
*intel_pt_insn
, uint64_t ip
)
967 uint64_t max_insn_cnt
, insn_cnt
= 0;
970 if (!decoder
->mtc_insn
)
971 decoder
->mtc_insn
= true;
973 max_insn_cnt
= intel_pt_next_sample(decoder
);
975 err
= decoder
->walk_insn(intel_pt_insn
, &insn_cnt
, &decoder
->ip
, ip
,
976 max_insn_cnt
, decoder
->data
);
978 decoder
->tot_insn_cnt
+= insn_cnt
;
979 decoder
->timestamp_insn_cnt
+= insn_cnt
;
980 decoder
->sample_insn_cnt
+= insn_cnt
;
981 decoder
->period_insn_cnt
+= insn_cnt
;
984 decoder
->no_progress
= 0;
985 decoder
->pkt_state
= INTEL_PT_STATE_ERR2
;
986 intel_pt_log_at("ERROR: Failed to get instruction",
993 if (ip
&& decoder
->ip
== ip
) {
998 if (max_insn_cnt
&& insn_cnt
>= max_insn_cnt
)
999 intel_pt_sample_insn(decoder
);
1001 if (intel_pt_insn
->branch
== INTEL_PT_BR_NO_BRANCH
) {
1002 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1003 decoder
->state
.from_ip
= decoder
->ip
;
1004 decoder
->state
.to_ip
= 0;
1005 decoder
->ip
+= intel_pt_insn
->length
;
1006 err
= INTEL_PT_RETURN
;
1010 if (intel_pt_insn
->op
== INTEL_PT_OP_CALL
) {
1011 /* Zero-length calls are excluded */
1012 if (intel_pt_insn
->branch
!= INTEL_PT_BR_UNCONDITIONAL
||
1013 intel_pt_insn
->rel
) {
1014 err
= intel_pt_push(&decoder
->stack
, decoder
->ip
+
1015 intel_pt_insn
->length
);
1019 } else if (intel_pt_insn
->op
== INTEL_PT_OP_RET
) {
1020 decoder
->ret_addr
= intel_pt_pop(&decoder
->stack
);
1023 if (intel_pt_insn
->branch
== INTEL_PT_BR_UNCONDITIONAL
) {
1024 int cnt
= decoder
->no_progress
++;
1026 decoder
->state
.from_ip
= decoder
->ip
;
1027 decoder
->ip
+= intel_pt_insn
->length
+
1029 decoder
->state
.to_ip
= decoder
->ip
;
1030 err
= INTEL_PT_RETURN
;
1033 * Check for being stuck in a loop. This can happen if a
1034 * decoder error results in the decoder erroneously setting the
1035 * ip to an address that is itself in an infinite loop that
1036 * consumes no packets. When that happens, there must be an
1037 * unconditional branch.
1041 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1042 decoder
->stuck_ip_prd
= 1;
1043 decoder
->stuck_ip_cnt
= 1;
1044 } else if (cnt
> INTEL_PT_MAX_LOOPS
||
1045 decoder
->state
.to_ip
== decoder
->stuck_ip
) {
1046 intel_pt_log_at("ERROR: Never-ending loop",
1047 decoder
->state
.to_ip
);
1048 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1051 } else if (!--decoder
->stuck_ip_cnt
) {
1052 decoder
->stuck_ip_prd
+= 1;
1053 decoder
->stuck_ip_cnt
= decoder
->stuck_ip_prd
;
1054 decoder
->stuck_ip
= decoder
->state
.to_ip
;
1057 goto out_no_progress
;
1060 decoder
->no_progress
= 0;
1062 decoder
->state
.insn_op
= intel_pt_insn
->op
;
1063 decoder
->state
.insn_len
= intel_pt_insn
->length
;
1064 memcpy(decoder
->state
.insn
, intel_pt_insn
->buf
,
1065 INTEL_PT_INSN_BUF_SZ
);
1067 if (decoder
->tx_flags
& INTEL_PT_IN_TX
)
1068 decoder
->state
.flags
|= INTEL_PT_IN_TX
;
1073 static bool intel_pt_fup_event(struct intel_pt_decoder
*decoder
)
1077 if (decoder
->set_fup_tx_flags
) {
1078 decoder
->set_fup_tx_flags
= false;
1079 decoder
->tx_flags
= decoder
->fup_tx_flags
;
1080 decoder
->state
.type
= INTEL_PT_TRANSACTION
;
1081 decoder
->state
.from_ip
= decoder
->ip
;
1082 decoder
->state
.to_ip
= 0;
1083 decoder
->state
.flags
= decoder
->fup_tx_flags
;
1086 if (decoder
->set_fup_ptw
) {
1087 decoder
->set_fup_ptw
= false;
1088 decoder
->state
.type
= INTEL_PT_PTW
;
1089 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1090 decoder
->state
.from_ip
= decoder
->ip
;
1091 decoder
->state
.to_ip
= 0;
1092 decoder
->state
.ptw_payload
= decoder
->fup_ptw_payload
;
1095 if (decoder
->set_fup_mwait
) {
1096 decoder
->set_fup_mwait
= false;
1097 decoder
->state
.type
= INTEL_PT_MWAIT_OP
;
1098 decoder
->state
.from_ip
= decoder
->ip
;
1099 decoder
->state
.to_ip
= 0;
1100 decoder
->state
.mwait_payload
= decoder
->fup_mwait_payload
;
1103 if (decoder
->set_fup_pwre
) {
1104 decoder
->set_fup_pwre
= false;
1105 decoder
->state
.type
|= INTEL_PT_PWR_ENTRY
;
1106 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1107 decoder
->state
.from_ip
= decoder
->ip
;
1108 decoder
->state
.to_ip
= 0;
1109 decoder
->state
.pwre_payload
= decoder
->fup_pwre_payload
;
1112 if (decoder
->set_fup_exstop
) {
1113 decoder
->set_fup_exstop
= false;
1114 decoder
->state
.type
|= INTEL_PT_EX_STOP
;
1115 decoder
->state
.type
&= ~INTEL_PT_BRANCH
;
1116 decoder
->state
.flags
|= INTEL_PT_FUP_IP
;
1117 decoder
->state
.from_ip
= decoder
->ip
;
1118 decoder
->state
.to_ip
= 0;
1124 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder
*decoder
,
1125 struct intel_pt_insn
*intel_pt_insn
,
1126 uint64_t ip
, int err
)
1128 return decoder
->flags
& INTEL_PT_FUP_WITH_NLIP
&& !err
&&
1129 intel_pt_insn
->branch
== INTEL_PT_BR_INDIRECT
&&
1130 ip
== decoder
->ip
+ intel_pt_insn
->length
;
1133 static int intel_pt_walk_fup(struct intel_pt_decoder
*decoder
)
1135 struct intel_pt_insn intel_pt_insn
;
1139 ip
= decoder
->last_ip
;
1142 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, ip
);
1143 if (err
== INTEL_PT_RETURN
)
1145 if (err
== -EAGAIN
||
1146 intel_pt_fup_with_nlip(decoder
, &intel_pt_insn
, ip
, err
)) {
1147 if (intel_pt_fup_event(decoder
))
1151 decoder
->set_fup_tx_flags
= false;
1155 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1156 intel_pt_log_at("ERROR: Unexpected indirect branch",
1158 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1162 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1163 intel_pt_log_at("ERROR: Unexpected conditional branch",
1165 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1169 intel_pt_bug(decoder
);
1173 static int intel_pt_walk_tip(struct intel_pt_decoder
*decoder
)
1175 struct intel_pt_insn intel_pt_insn
;
1178 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1179 if (err
== INTEL_PT_RETURN
&&
1181 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1182 (decoder
->state
.type
& INTEL_PT_BRANCH
) &&
1183 decoder
->pgd_ip(decoder
->state
.to_ip
, decoder
->data
)) {
1184 /* Unconditional branch leaving filter region */
1185 decoder
->no_progress
= 0;
1186 decoder
->pge
= false;
1187 decoder
->continuous_period
= false;
1188 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1189 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1192 if (err
== INTEL_PT_RETURN
)
1197 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1198 if (decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
) {
1199 decoder
->pge
= false;
1200 decoder
->continuous_period
= false;
1201 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1202 decoder
->state
.from_ip
= decoder
->ip
;
1203 if (decoder
->packet
.count
== 0) {
1204 decoder
->state
.to_ip
= 0;
1206 decoder
->state
.to_ip
= decoder
->last_ip
;
1207 decoder
->ip
= decoder
->last_ip
;
1209 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1211 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1212 decoder
->state
.from_ip
= decoder
->ip
;
1213 if (decoder
->packet
.count
== 0) {
1214 decoder
->state
.to_ip
= 0;
1216 decoder
->state
.to_ip
= decoder
->last_ip
;
1217 decoder
->ip
= decoder
->last_ip
;
1223 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1224 uint64_t to_ip
= decoder
->ip
+ intel_pt_insn
.length
+
1227 if (decoder
->pgd_ip
&&
1228 decoder
->pkt_state
== INTEL_PT_STATE_TIP_PGD
&&
1229 decoder
->pgd_ip(to_ip
, decoder
->data
)) {
1230 /* Conditional branch leaving filter region */
1231 decoder
->pge
= false;
1232 decoder
->continuous_period
= false;
1233 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1234 decoder
->ip
= to_ip
;
1235 decoder
->state
.from_ip
= decoder
->ip
;
1236 decoder
->state
.to_ip
= to_ip
;
1237 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1240 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1242 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1246 return intel_pt_bug(decoder
);
1249 static int intel_pt_walk_tnt(struct intel_pt_decoder
*decoder
)
1251 struct intel_pt_insn intel_pt_insn
;
1255 err
= intel_pt_walk_insn(decoder
, &intel_pt_insn
, 0);
1256 if (err
== INTEL_PT_RETURN
)
1261 if (intel_pt_insn
.op
== INTEL_PT_OP_RET
) {
1262 if (!decoder
->return_compression
) {
1263 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1265 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1268 if (!decoder
->ret_addr
) {
1269 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1271 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1274 if (!(decoder
->tnt
.payload
& BIT63
)) {
1275 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1277 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1280 decoder
->tnt
.count
-= 1;
1281 if (decoder
->tnt
.count
)
1282 decoder
->pkt_state
= INTEL_PT_STATE_TNT_CONT
;
1284 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1285 decoder
->tnt
.payload
<<= 1;
1286 decoder
->state
.from_ip
= decoder
->ip
;
1287 decoder
->ip
= decoder
->ret_addr
;
1288 decoder
->state
.to_ip
= decoder
->ip
;
1292 if (intel_pt_insn
.branch
== INTEL_PT_BR_INDIRECT
) {
1293 /* Handle deferred TIPs */
1294 err
= intel_pt_get_next_packet(decoder
);
1297 if (decoder
->packet
.type
!= INTEL_PT_TIP
||
1298 decoder
->packet
.count
== 0) {
1299 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1301 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1302 decoder
->pkt_step
= 0;
1305 intel_pt_set_last_ip(decoder
);
1306 decoder
->state
.from_ip
= decoder
->ip
;
1307 decoder
->state
.to_ip
= decoder
->last_ip
;
1308 decoder
->ip
= decoder
->last_ip
;
1312 if (intel_pt_insn
.branch
== INTEL_PT_BR_CONDITIONAL
) {
1313 decoder
->tnt
.count
-= 1;
1314 if (decoder
->tnt
.count
)
1315 decoder
->pkt_state
= INTEL_PT_STATE_TNT_CONT
;
1317 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
1318 if (decoder
->tnt
.payload
& BIT63
) {
1319 decoder
->tnt
.payload
<<= 1;
1320 decoder
->state
.from_ip
= decoder
->ip
;
1321 decoder
->ip
+= intel_pt_insn
.length
+
1323 decoder
->state
.to_ip
= decoder
->ip
;
1326 /* Instruction sample for a non-taken branch */
1327 if (decoder
->state
.type
& INTEL_PT_INSTRUCTION
) {
1328 decoder
->tnt
.payload
<<= 1;
1329 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1330 decoder
->state
.from_ip
= decoder
->ip
;
1331 decoder
->state
.to_ip
= 0;
1332 decoder
->ip
+= intel_pt_insn
.length
;
1335 decoder
->sample_cyc
= false;
1336 decoder
->ip
+= intel_pt_insn
.length
;
1337 if (!decoder
->tnt
.count
) {
1338 intel_pt_update_sample_time(decoder
);
1341 decoder
->tnt
.payload
<<= 1;
1345 return intel_pt_bug(decoder
);
1349 static int intel_pt_mode_tsx(struct intel_pt_decoder
*decoder
, bool *no_tip
)
1351 unsigned int fup_tx_flags
;
1354 fup_tx_flags
= decoder
->packet
.payload
&
1355 (INTEL_PT_IN_TX
| INTEL_PT_ABORT_TX
);
1356 err
= intel_pt_get_next_packet(decoder
);
1359 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1360 decoder
->fup_tx_flags
= fup_tx_flags
;
1361 decoder
->set_fup_tx_flags
= true;
1362 if (!(decoder
->fup_tx_flags
& INTEL_PT_ABORT_TX
))
1365 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1367 intel_pt_update_in_tx(decoder
);
1372 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder
*decoder
)
1376 decoder
->have_tma
= false;
1378 if (decoder
->ref_timestamp
) {
1379 timestamp
= decoder
->packet
.payload
|
1380 (decoder
->ref_timestamp
& (0xffULL
<< 56));
1381 if (timestamp
< decoder
->ref_timestamp
) {
1382 if (decoder
->ref_timestamp
- timestamp
> (1ULL << 55))
1383 timestamp
+= (1ULL << 56);
1385 if (timestamp
- decoder
->ref_timestamp
> (1ULL << 55))
1386 timestamp
-= (1ULL << 56);
1388 decoder
->tsc_timestamp
= timestamp
;
1389 decoder
->timestamp
= timestamp
;
1390 decoder
->ref_timestamp
= 0;
1391 decoder
->timestamp_insn_cnt
= 0;
1392 } else if (decoder
->timestamp
) {
1393 timestamp
= decoder
->packet
.payload
|
1394 (decoder
->timestamp
& (0xffULL
<< 56));
1395 decoder
->tsc_timestamp
= timestamp
;
1396 if (timestamp
< decoder
->timestamp
&&
1397 decoder
->timestamp
- timestamp
< decoder
->tsc_slip
) {
1398 intel_pt_log_to("Suppressing backwards timestamp",
1400 timestamp
= decoder
->timestamp
;
1402 if (timestamp
< decoder
->timestamp
) {
1403 intel_pt_log_to("Wraparound timestamp", timestamp
);
1404 timestamp
+= (1ULL << 56);
1405 decoder
->tsc_timestamp
= timestamp
;
1407 decoder
->timestamp
= timestamp
;
1408 decoder
->timestamp_insn_cnt
= 0;
1411 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1412 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1413 decoder
->cycle_cnt
= 0;
1414 decoder
->have_calc_cyc_to_tsc
= false;
1415 intel_pt_calc_cyc_to_tsc(decoder
, false);
1418 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1421 static int intel_pt_overflow(struct intel_pt_decoder
*decoder
)
1423 intel_pt_log("ERROR: Buffer overflow\n");
1424 intel_pt_clear_tx_flags(decoder
);
1425 decoder
->timestamp_insn_cnt
= 0;
1426 decoder
->pkt_state
= INTEL_PT_STATE_ERR_RESYNC
;
1427 decoder
->overflow
= true;
1431 static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder
*decoder
)
1433 if (decoder
->have_cyc
)
1436 decoder
->cyc_cnt_timestamp
= decoder
->timestamp
;
1437 decoder
->base_cyc_cnt
= decoder
->tot_cyc_cnt
;
1440 static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder
*decoder
)
1442 decoder
->tsc_to_cyc
= decoder
->cbr
/ decoder
->max_non_turbo_ratio_fp
;
1445 intel_pt_mtc_cyc_cnt_pge(decoder
);
1448 static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder
*decoder
)
1450 uint64_t tot_cyc_cnt
, tsc_delta
;
1452 if (decoder
->have_cyc
)
1455 decoder
->sample_cyc
= true;
1457 if (!decoder
->pge
|| decoder
->timestamp
<= decoder
->cyc_cnt_timestamp
)
1460 tsc_delta
= decoder
->timestamp
- decoder
->cyc_cnt_timestamp
;
1461 tot_cyc_cnt
= tsc_delta
* decoder
->tsc_to_cyc
+ decoder
->base_cyc_cnt
;
1463 if (tot_cyc_cnt
> decoder
->tot_cyc_cnt
)
1464 decoder
->tot_cyc_cnt
= tot_cyc_cnt
;
1467 static void intel_pt_calc_tma(struct intel_pt_decoder
*decoder
)
1469 uint32_t ctc
= decoder
->packet
.payload
;
1470 uint32_t fc
= decoder
->packet
.count
;
1471 uint32_t ctc_rem
= ctc
& decoder
->ctc_rem_mask
;
1473 if (!decoder
->tsc_ctc_ratio_d
)
1476 if (decoder
->pge
&& !decoder
->in_psb
)
1477 intel_pt_mtc_cyc_cnt_pge(decoder
);
1479 intel_pt_mtc_cyc_cnt_upd(decoder
);
1481 decoder
->last_mtc
= (ctc
>> decoder
->mtc_shift
) & 0xff;
1482 decoder
->ctc_timestamp
= decoder
->tsc_timestamp
- fc
;
1483 if (decoder
->tsc_ctc_mult
) {
1484 decoder
->ctc_timestamp
-= ctc_rem
* decoder
->tsc_ctc_mult
;
1486 decoder
->ctc_timestamp
-= multdiv(ctc_rem
,
1487 decoder
->tsc_ctc_ratio_n
,
1488 decoder
->tsc_ctc_ratio_d
);
1490 decoder
->ctc_delta
= 0;
1491 decoder
->have_tma
= true;
1492 decoder
->fixup_last_mtc
= true;
1493 intel_pt_log("CTC timestamp " x64_fmt
" last MTC %#x CTC rem %#x\n",
1494 decoder
->ctc_timestamp
, decoder
->last_mtc
, ctc_rem
);
1497 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder
*decoder
)
1500 uint32_t mtc
, mtc_delta
;
1502 if (!decoder
->have_tma
)
1505 mtc
= decoder
->packet
.payload
;
1507 if (decoder
->mtc_shift
> 8 && decoder
->fixup_last_mtc
) {
1508 decoder
->fixup_last_mtc
= false;
1509 intel_pt_fixup_last_mtc(mtc
, decoder
->mtc_shift
,
1510 &decoder
->last_mtc
);
1513 if (mtc
> decoder
->last_mtc
)
1514 mtc_delta
= mtc
- decoder
->last_mtc
;
1516 mtc_delta
= mtc
+ 256 - decoder
->last_mtc
;
1518 decoder
->ctc_delta
+= mtc_delta
<< decoder
->mtc_shift
;
1520 if (decoder
->tsc_ctc_mult
) {
1521 timestamp
= decoder
->ctc_timestamp
+
1522 decoder
->ctc_delta
* decoder
->tsc_ctc_mult
;
1524 timestamp
= decoder
->ctc_timestamp
+
1525 multdiv(decoder
->ctc_delta
,
1526 decoder
->tsc_ctc_ratio_n
,
1527 decoder
->tsc_ctc_ratio_d
);
1530 if (timestamp
< decoder
->timestamp
)
1531 intel_pt_log("Suppressing MTC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1532 timestamp
, decoder
->timestamp
);
1534 decoder
->timestamp
= timestamp
;
1536 intel_pt_mtc_cyc_cnt_upd(decoder
);
1538 decoder
->timestamp_insn_cnt
= 0;
1539 decoder
->last_mtc
= mtc
;
1541 if (decoder
->last_packet_type
== INTEL_PT_CYC
) {
1542 decoder
->cyc_ref_timestamp
= decoder
->timestamp
;
1543 decoder
->cycle_cnt
= 0;
1544 decoder
->have_calc_cyc_to_tsc
= false;
1545 intel_pt_calc_cyc_to_tsc(decoder
, true);
1548 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1551 static void intel_pt_calc_cbr(struct intel_pt_decoder
*decoder
)
1553 unsigned int cbr
= decoder
->packet
.payload
& 0xff;
1555 decoder
->cbr_payload
= decoder
->packet
.payload
;
1557 if (decoder
->cbr
== cbr
)
1561 decoder
->cbr_cyc_to_tsc
= decoder
->max_non_turbo_ratio_fp
/ cbr
;
1563 intel_pt_mtc_cyc_cnt_cbr(decoder
);
1566 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder
*decoder
)
1568 uint64_t timestamp
= decoder
->cyc_ref_timestamp
;
1570 decoder
->have_cyc
= true;
1572 decoder
->cycle_cnt
+= decoder
->packet
.payload
;
1574 decoder
->tot_cyc_cnt
+= decoder
->packet
.payload
;
1575 decoder
->sample_cyc
= true;
1577 if (!decoder
->cyc_ref_timestamp
)
1580 if (decoder
->have_calc_cyc_to_tsc
)
1581 timestamp
+= decoder
->cycle_cnt
* decoder
->calc_cyc_to_tsc
;
1582 else if (decoder
->cbr
)
1583 timestamp
+= decoder
->cycle_cnt
* decoder
->cbr_cyc_to_tsc
;
1587 if (timestamp
< decoder
->timestamp
)
1588 intel_pt_log("Suppressing CYC timestamp " x64_fmt
" less than current timestamp " x64_fmt
"\n",
1589 timestamp
, decoder
->timestamp
);
1591 decoder
->timestamp
= timestamp
;
1593 decoder
->timestamp_insn_cnt
= 0;
1595 intel_pt_log_to("Setting timestamp", decoder
->timestamp
);
1598 /* Walk PSB+ packets when already in sync. */
1599 static int intel_pt_walk_psbend(struct intel_pt_decoder
*decoder
)
1603 decoder
->in_psb
= true;
1606 err
= intel_pt_get_next_packet(decoder
);
1610 switch (decoder
->packet
.type
) {
1611 case INTEL_PT_PSBEND
:
1615 case INTEL_PT_TIP_PGD
:
1616 case INTEL_PT_TIP_PGE
:
1619 case INTEL_PT_TRACESTOP
:
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 decoder
->have_tma
= false;
1630 intel_pt_log("ERROR: Unexpected packet\n");
1635 err
= intel_pt_overflow(decoder
);
1639 intel_pt_calc_tsc_timestamp(decoder
);
1643 intel_pt_calc_tma(decoder
);
1647 intel_pt_calc_cbr(decoder
);
1650 case INTEL_PT_MODE_EXEC
:
1651 decoder
->exec_mode
= decoder
->packet
.payload
;
1655 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1659 decoder
->pge
= true;
1660 if (decoder
->packet
.count
)
1661 intel_pt_set_last_ip(decoder
);
1664 case INTEL_PT_MODE_TSX
:
1665 intel_pt_update_in_tx(decoder
);
1669 intel_pt_calc_mtc_timestamp(decoder
);
1670 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1671 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1683 decoder
->in_psb
= false;
1688 static int intel_pt_walk_fup_tip(struct intel_pt_decoder
*decoder
)
1692 if (decoder
->tx_flags
& INTEL_PT_ABORT_TX
) {
1693 decoder
->tx_flags
= 0;
1694 decoder
->state
.flags
&= ~INTEL_PT_IN_TX
;
1695 decoder
->state
.flags
|= INTEL_PT_ABORT_TX
;
1697 decoder
->state
.flags
|= INTEL_PT_ASYNC
;
1701 err
= intel_pt_get_next_packet(decoder
);
1705 switch (decoder
->packet
.type
) {
1708 case INTEL_PT_TRACESTOP
:
1712 case INTEL_PT_MODE_TSX
:
1714 case INTEL_PT_PSBEND
:
1715 case INTEL_PT_PTWRITE
:
1716 case INTEL_PT_PTWRITE_IP
:
1717 case INTEL_PT_EXSTOP
:
1718 case INTEL_PT_EXSTOP_IP
:
1719 case INTEL_PT_MWAIT
:
1722 intel_pt_log("ERROR: Missing TIP after FUP\n");
1723 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
1724 decoder
->pkt_step
= 0;
1728 intel_pt_calc_cbr(decoder
);
1732 return intel_pt_overflow(decoder
);
1734 case INTEL_PT_TIP_PGD
:
1735 decoder
->state
.from_ip
= decoder
->ip
;
1736 if (decoder
->packet
.count
== 0) {
1737 decoder
->state
.to_ip
= 0;
1739 intel_pt_set_ip(decoder
);
1740 decoder
->state
.to_ip
= decoder
->ip
;
1742 decoder
->pge
= false;
1743 decoder
->continuous_period
= false;
1744 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
1747 case INTEL_PT_TIP_PGE
:
1748 decoder
->pge
= true;
1749 intel_pt_log("Omitting PGE ip " x64_fmt
"\n",
1751 decoder
->state
.from_ip
= 0;
1752 if (decoder
->packet
.count
== 0) {
1753 decoder
->state
.to_ip
= 0;
1755 intel_pt_set_ip(decoder
);
1756 decoder
->state
.to_ip
= decoder
->ip
;
1758 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
1759 intel_pt_mtc_cyc_cnt_pge(decoder
);
1763 decoder
->state
.from_ip
= decoder
->ip
;
1764 if (decoder
->packet
.count
== 0) {
1765 decoder
->state
.to_ip
= 0;
1767 intel_pt_set_ip(decoder
);
1768 decoder
->state
.to_ip
= decoder
->ip
;
1773 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1777 intel_pt_calc_mtc_timestamp(decoder
);
1778 if (decoder
->period_type
== INTEL_PT_PERIOD_MTC
)
1779 decoder
->state
.type
|= INTEL_PT_INSTRUCTION
;
1783 intel_pt_calc_cyc_timestamp(decoder
);
1786 case INTEL_PT_MODE_EXEC
:
1787 decoder
->exec_mode
= decoder
->packet
.payload
;
1796 return intel_pt_bug(decoder
);
1801 static int intel_pt_walk_trace(struct intel_pt_decoder
*decoder
)
1803 bool no_tip
= false;
1807 err
= intel_pt_get_next_packet(decoder
);
1811 switch (decoder
->packet
.type
) {
1813 if (!decoder
->packet
.count
)
1815 decoder
->tnt
= decoder
->packet
;
1816 decoder
->pkt_state
= INTEL_PT_STATE_TNT
;
1817 err
= intel_pt_walk_tnt(decoder
);
1822 case INTEL_PT_TIP_PGD
:
1823 if (decoder
->packet
.count
!= 0)
1824 intel_pt_set_last_ip(decoder
);
1825 decoder
->pkt_state
= INTEL_PT_STATE_TIP_PGD
;
1826 return intel_pt_walk_tip(decoder
);
1828 case INTEL_PT_TIP_PGE
: {
1829 decoder
->pge
= true;
1830 intel_pt_mtc_cyc_cnt_pge(decoder
);
1831 if (decoder
->packet
.count
== 0) {
1832 intel_pt_log_at("Skipping zero TIP.PGE",
1836 intel_pt_set_ip(decoder
);
1837 decoder
->state
.from_ip
= 0;
1838 decoder
->state
.to_ip
= decoder
->ip
;
1839 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
1844 return intel_pt_overflow(decoder
);
1847 if (decoder
->packet
.count
!= 0)
1848 intel_pt_set_last_ip(decoder
);
1849 decoder
->pkt_state
= INTEL_PT_STATE_TIP
;
1850 return intel_pt_walk_tip(decoder
);
1853 if (decoder
->packet
.count
== 0) {
1854 intel_pt_log_at("Skipping zero FUP",
1859 intel_pt_set_last_ip(decoder
);
1860 if (!decoder
->branch_enable
) {
1861 decoder
->ip
= decoder
->last_ip
;
1862 if (intel_pt_fup_event(decoder
))
1867 if (decoder
->set_fup_mwait
)
1869 err
= intel_pt_walk_fup(decoder
);
1870 if (err
!= -EAGAIN
) {
1874 decoder
->pkt_state
=
1875 INTEL_PT_STATE_FUP_NO_TIP
;
1877 decoder
->pkt_state
= INTEL_PT_STATE_FUP
;
1884 return intel_pt_walk_fup_tip(decoder
);
1886 case INTEL_PT_TRACESTOP
:
1887 decoder
->pge
= false;
1888 decoder
->continuous_period
= false;
1889 intel_pt_clear_tx_flags(decoder
);
1890 decoder
->have_tma
= false;
1894 decoder
->last_ip
= 0;
1895 decoder
->have_last_ip
= true;
1896 intel_pt_clear_stack(&decoder
->stack
);
1897 err
= intel_pt_walk_psbend(decoder
);
1905 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
1909 intel_pt_calc_mtc_timestamp(decoder
);
1910 if (decoder
->period_type
!= INTEL_PT_PERIOD_MTC
)
1913 * Ensure that there has been an instruction since the
1916 if (!decoder
->mtc_insn
)
1918 decoder
->mtc_insn
= false;
1919 /* Ensure that there is a timestamp */
1920 if (!decoder
->timestamp
)
1922 decoder
->state
.type
= INTEL_PT_INSTRUCTION
;
1923 decoder
->state
.from_ip
= decoder
->ip
;
1924 decoder
->state
.to_ip
= 0;
1925 decoder
->mtc_insn
= false;
1929 intel_pt_calc_tsc_timestamp(decoder
);
1933 intel_pt_calc_tma(decoder
);
1937 intel_pt_calc_cyc_timestamp(decoder
);
1941 intel_pt_calc_cbr(decoder
);
1942 if (!decoder
->branch_enable
&&
1943 decoder
->cbr
!= decoder
->cbr_seen
) {
1944 decoder
->cbr_seen
= decoder
->cbr
;
1945 decoder
->state
.type
= INTEL_PT_CBR_CHG
;
1946 decoder
->state
.from_ip
= decoder
->ip
;
1947 decoder
->state
.to_ip
= 0;
1948 decoder
->state
.cbr_payload
=
1949 decoder
->packet
.payload
;
1954 case INTEL_PT_MODE_EXEC
:
1955 decoder
->exec_mode
= decoder
->packet
.payload
;
1958 case INTEL_PT_MODE_TSX
:
1959 /* MODE_TSX need not be followed by FUP */
1960 if (!decoder
->pge
) {
1961 intel_pt_update_in_tx(decoder
);
1964 err
= intel_pt_mode_tsx(decoder
, &no_tip
);
1969 case INTEL_PT_BAD
: /* Does not happen */
1970 return intel_pt_bug(decoder
);
1972 case INTEL_PT_PSBEND
:
1978 case INTEL_PT_PTWRITE_IP
:
1979 decoder
->fup_ptw_payload
= decoder
->packet
.payload
;
1980 err
= intel_pt_get_next_packet(decoder
);
1983 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
1984 decoder
->set_fup_ptw
= true;
1987 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1992 case INTEL_PT_PTWRITE
:
1993 decoder
->state
.type
= INTEL_PT_PTW
;
1994 decoder
->state
.from_ip
= decoder
->ip
;
1995 decoder
->state
.to_ip
= 0;
1996 decoder
->state
.ptw_payload
= decoder
->packet
.payload
;
1999 case INTEL_PT_MWAIT
:
2000 decoder
->fup_mwait_payload
= decoder
->packet
.payload
;
2001 decoder
->set_fup_mwait
= true;
2005 if (decoder
->set_fup_mwait
) {
2006 decoder
->fup_pwre_payload
=
2007 decoder
->packet
.payload
;
2008 decoder
->set_fup_pwre
= true;
2011 decoder
->state
.type
= INTEL_PT_PWR_ENTRY
;
2012 decoder
->state
.from_ip
= decoder
->ip
;
2013 decoder
->state
.to_ip
= 0;
2014 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
2017 case INTEL_PT_EXSTOP_IP
:
2018 err
= intel_pt_get_next_packet(decoder
);
2021 if (decoder
->packet
.type
== INTEL_PT_FUP
) {
2022 decoder
->set_fup_exstop
= true;
2025 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
2030 case INTEL_PT_EXSTOP
:
2031 decoder
->state
.type
= INTEL_PT_EX_STOP
;
2032 decoder
->state
.from_ip
= decoder
->ip
;
2033 decoder
->state
.to_ip
= 0;
2037 decoder
->state
.type
= INTEL_PT_PWR_EXIT
;
2038 decoder
->state
.from_ip
= decoder
->ip
;
2039 decoder
->state
.to_ip
= 0;
2040 decoder
->state
.pwrx_payload
= decoder
->packet
.payload
;
2044 return intel_pt_bug(decoder
);
2049 static inline bool intel_pt_have_ip(struct intel_pt_decoder
*decoder
)
2051 return decoder
->packet
.count
&&
2052 (decoder
->have_last_ip
|| decoder
->packet
.count
== 3 ||
2053 decoder
->packet
.count
== 6);
2056 /* Walk PSB+ packets to get in sync. */
2057 static int intel_pt_walk_psb(struct intel_pt_decoder
*decoder
)
2061 decoder
->in_psb
= true;
2064 err
= intel_pt_get_next_packet(decoder
);
2068 switch (decoder
->packet
.type
) {
2069 case INTEL_PT_TIP_PGD
:
2070 decoder
->continuous_period
= false;
2072 case INTEL_PT_TIP_PGE
:
2074 case INTEL_PT_PTWRITE
:
2075 case INTEL_PT_PTWRITE_IP
:
2076 case INTEL_PT_EXSTOP
:
2077 case INTEL_PT_EXSTOP_IP
:
2078 case INTEL_PT_MWAIT
:
2081 intel_pt_log("ERROR: Unexpected packet\n");
2086 decoder
->pge
= true;
2087 if (intel_pt_have_ip(decoder
)) {
2088 uint64_t current_ip
= decoder
->ip
;
2090 intel_pt_set_ip(decoder
);
2092 intel_pt_log_to("Setting IP",
2098 intel_pt_calc_mtc_timestamp(decoder
);
2102 intel_pt_calc_tsc_timestamp(decoder
);
2106 intel_pt_calc_tma(decoder
);
2110 intel_pt_calc_cyc_timestamp(decoder
);
2114 intel_pt_calc_cbr(decoder
);
2118 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2121 case INTEL_PT_MODE_EXEC
:
2122 decoder
->exec_mode
= decoder
->packet
.payload
;
2125 case INTEL_PT_MODE_TSX
:
2126 intel_pt_update_in_tx(decoder
);
2129 case INTEL_PT_TRACESTOP
:
2130 decoder
->pge
= false;
2131 decoder
->continuous_period
= false;
2132 intel_pt_clear_tx_flags(decoder
);
2136 decoder
->have_tma
= false;
2137 intel_pt_log("ERROR: Unexpected packet\n");
2139 decoder
->pkt_state
= INTEL_PT_STATE_ERR4
;
2141 decoder
->pkt_state
= INTEL_PT_STATE_ERR3
;
2145 case INTEL_PT_BAD
: /* Does not happen */
2146 err
= intel_pt_bug(decoder
);
2150 err
= intel_pt_overflow(decoder
);
2153 case INTEL_PT_PSBEND
:
2166 decoder
->in_psb
= false;
2171 static int intel_pt_walk_to_ip(struct intel_pt_decoder
*decoder
)
2176 err
= intel_pt_get_next_packet(decoder
);
2180 switch (decoder
->packet
.type
) {
2181 case INTEL_PT_TIP_PGD
:
2182 decoder
->continuous_period
= false;
2183 decoder
->pge
= false;
2184 if (intel_pt_have_ip(decoder
))
2185 intel_pt_set_ip(decoder
);
2188 decoder
->state
.type
|= INTEL_PT_TRACE_END
;
2191 case INTEL_PT_TIP_PGE
:
2192 decoder
->pge
= true;
2193 intel_pt_mtc_cyc_cnt_pge(decoder
);
2194 if (intel_pt_have_ip(decoder
))
2195 intel_pt_set_ip(decoder
);
2198 decoder
->state
.type
|= INTEL_PT_TRACE_BEGIN
;
2202 decoder
->pge
= true;
2203 if (intel_pt_have_ip(decoder
))
2204 intel_pt_set_ip(decoder
);
2210 if (intel_pt_have_ip(decoder
))
2211 intel_pt_set_ip(decoder
);
2217 intel_pt_calc_mtc_timestamp(decoder
);
2221 intel_pt_calc_tsc_timestamp(decoder
);
2225 intel_pt_calc_tma(decoder
);
2229 intel_pt_calc_cyc_timestamp(decoder
);
2233 intel_pt_calc_cbr(decoder
);
2237 decoder
->cr3
= decoder
->packet
.payload
& (BIT63
- 1);
2240 case INTEL_PT_MODE_EXEC
:
2241 decoder
->exec_mode
= decoder
->packet
.payload
;
2244 case INTEL_PT_MODE_TSX
:
2245 intel_pt_update_in_tx(decoder
);
2249 return intel_pt_overflow(decoder
);
2251 case INTEL_PT_BAD
: /* Does not happen */
2252 return intel_pt_bug(decoder
);
2254 case INTEL_PT_TRACESTOP
:
2255 decoder
->pge
= false;
2256 decoder
->continuous_period
= false;
2257 intel_pt_clear_tx_flags(decoder
);
2258 decoder
->have_tma
= false;
2262 decoder
->last_ip
= 0;
2263 decoder
->have_last_ip
= true;
2264 intel_pt_clear_stack(&decoder
->stack
);
2265 err
= intel_pt_walk_psb(decoder
);
2269 /* Do not have a sample */
2270 decoder
->state
.type
= 0;
2276 case INTEL_PT_PSBEND
:
2280 case INTEL_PT_PTWRITE
:
2281 case INTEL_PT_PTWRITE_IP
:
2282 case INTEL_PT_EXSTOP
:
2283 case INTEL_PT_EXSTOP_IP
:
2284 case INTEL_PT_MWAIT
:
2293 static int intel_pt_sync_ip(struct intel_pt_decoder
*decoder
)
2297 decoder
->set_fup_tx_flags
= false;
2298 decoder
->set_fup_ptw
= false;
2299 decoder
->set_fup_mwait
= false;
2300 decoder
->set_fup_pwre
= false;
2301 decoder
->set_fup_exstop
= false;
2303 if (!decoder
->branch_enable
) {
2304 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2305 decoder
->overflow
= false;
2306 decoder
->state
.type
= 0; /* Do not have a sample */
2310 intel_pt_log("Scanning for full IP\n");
2311 err
= intel_pt_walk_to_ip(decoder
);
2315 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2316 decoder
->overflow
= false;
2318 decoder
->state
.from_ip
= 0;
2319 decoder
->state
.to_ip
= decoder
->ip
;
2320 intel_pt_log_to("Setting IP", decoder
->ip
);
2325 static int intel_pt_part_psb(struct intel_pt_decoder
*decoder
)
2327 const unsigned char *end
= decoder
->buf
+ decoder
->len
;
2330 for (i
= INTEL_PT_PSB_LEN
- 1; i
; i
--) {
2331 if (i
> decoder
->len
)
2333 if (!memcmp(end
- i
, INTEL_PT_PSB_STR
, i
))
2339 static int intel_pt_rest_psb(struct intel_pt_decoder
*decoder
, int part_psb
)
2341 size_t rest_psb
= INTEL_PT_PSB_LEN
- part_psb
;
2342 const char *psb
= INTEL_PT_PSB_STR
;
2344 if (rest_psb
> decoder
->len
||
2345 memcmp(decoder
->buf
, psb
+ part_psb
, rest_psb
))
2351 static int intel_pt_get_split_psb(struct intel_pt_decoder
*decoder
,
2356 decoder
->pos
+= decoder
->len
;
2359 ret
= intel_pt_get_next_data(decoder
);
2363 rest_psb
= intel_pt_rest_psb(decoder
, part_psb
);
2367 decoder
->pos
-= part_psb
;
2368 decoder
->next_buf
= decoder
->buf
+ rest_psb
;
2369 decoder
->next_len
= decoder
->len
- rest_psb
;
2370 memcpy(decoder
->temp_buf
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2371 decoder
->buf
= decoder
->temp_buf
;
2372 decoder
->len
= INTEL_PT_PSB_LEN
;
2377 static int intel_pt_scan_for_psb(struct intel_pt_decoder
*decoder
)
2379 unsigned char *next
;
2382 intel_pt_log("Scanning for PSB\n");
2384 if (!decoder
->len
) {
2385 ret
= intel_pt_get_next_data(decoder
);
2390 next
= memmem(decoder
->buf
, decoder
->len
, INTEL_PT_PSB_STR
,
2395 part_psb
= intel_pt_part_psb(decoder
);
2397 ret
= intel_pt_get_split_psb(decoder
, part_psb
);
2401 decoder
->pos
+= decoder
->len
;
2407 decoder
->pkt_step
= next
- decoder
->buf
;
2408 return intel_pt_get_next_packet(decoder
);
2412 static int intel_pt_sync(struct intel_pt_decoder
*decoder
)
2416 decoder
->pge
= false;
2417 decoder
->continuous_period
= false;
2418 decoder
->have_last_ip
= false;
2419 decoder
->last_ip
= 0;
2421 intel_pt_clear_stack(&decoder
->stack
);
2423 err
= intel_pt_scan_for_psb(decoder
);
2427 decoder
->have_last_ip
= true;
2428 decoder
->pkt_state
= INTEL_PT_STATE_NO_IP
;
2430 err
= intel_pt_walk_psb(decoder
);
2435 decoder
->state
.type
= 0; /* Do not have a sample */
2436 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2438 return intel_pt_sync_ip(decoder
);
2444 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder
*decoder
)
2446 uint64_t est
= decoder
->sample_insn_cnt
<< 1;
2448 if (!decoder
->cbr
|| !decoder
->max_non_turbo_ratio
)
2451 est
*= decoder
->max_non_turbo_ratio
;
2452 est
/= decoder
->cbr
;
2454 return decoder
->sample_timestamp
+ est
;
2457 const struct intel_pt_state
*intel_pt_decode(struct intel_pt_decoder
*decoder
)
2462 decoder
->state
.type
= INTEL_PT_BRANCH
;
2463 decoder
->state
.flags
= 0;
2465 switch (decoder
->pkt_state
) {
2466 case INTEL_PT_STATE_NO_PSB
:
2467 err
= intel_pt_sync(decoder
);
2469 case INTEL_PT_STATE_NO_IP
:
2470 decoder
->have_last_ip
= false;
2471 decoder
->last_ip
= 0;
2474 case INTEL_PT_STATE_ERR_RESYNC
:
2475 err
= intel_pt_sync_ip(decoder
);
2477 case INTEL_PT_STATE_IN_SYNC
:
2478 err
= intel_pt_walk_trace(decoder
);
2480 case INTEL_PT_STATE_TNT
:
2481 case INTEL_PT_STATE_TNT_CONT
:
2482 err
= intel_pt_walk_tnt(decoder
);
2484 err
= intel_pt_walk_trace(decoder
);
2486 case INTEL_PT_STATE_TIP
:
2487 case INTEL_PT_STATE_TIP_PGD
:
2488 err
= intel_pt_walk_tip(decoder
);
2490 case INTEL_PT_STATE_FUP
:
2491 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2492 err
= intel_pt_walk_fup(decoder
);
2494 err
= intel_pt_walk_fup_tip(decoder
);
2496 decoder
->pkt_state
= INTEL_PT_STATE_FUP
;
2498 case INTEL_PT_STATE_FUP_NO_TIP
:
2499 decoder
->pkt_state
= INTEL_PT_STATE_IN_SYNC
;
2500 err
= intel_pt_walk_fup(decoder
);
2502 err
= intel_pt_walk_trace(decoder
);
2505 err
= intel_pt_bug(decoder
);
2508 } while (err
== -ENOLINK
);
2511 decoder
->state
.err
= intel_pt_ext_err(err
);
2512 decoder
->state
.from_ip
= decoder
->ip
;
2513 intel_pt_update_sample_time(decoder
);
2514 decoder
->sample_tot_cyc_cnt
= decoder
->tot_cyc_cnt
;
2516 decoder
->state
.err
= 0;
2517 if (decoder
->cbr
!= decoder
->cbr_seen
&& decoder
->state
.type
) {
2518 decoder
->cbr_seen
= decoder
->cbr
;
2519 decoder
->state
.type
|= INTEL_PT_CBR_CHG
;
2520 decoder
->state
.cbr_payload
= decoder
->cbr_payload
;
2522 if (intel_pt_sample_time(decoder
->pkt_state
)) {
2523 intel_pt_update_sample_time(decoder
);
2524 if (decoder
->sample_cyc
)
2525 decoder
->sample_tot_cyc_cnt
= decoder
->tot_cyc_cnt
;
2529 decoder
->state
.timestamp
= decoder
->sample_timestamp
;
2530 decoder
->state
.est_timestamp
= intel_pt_est_timestamp(decoder
);
2531 decoder
->state
.cr3
= decoder
->cr3
;
2532 decoder
->state
.tot_insn_cnt
= decoder
->tot_insn_cnt
;
2533 decoder
->state
.tot_cyc_cnt
= decoder
->sample_tot_cyc_cnt
;
2535 return &decoder
->state
;
2539 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2540 * @buf: pointer to buffer pointer
2541 * @len: size of buffer
2543 * Updates the buffer pointer to point to the start of the next PSB packet if
2544 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2545 * @len is adjusted accordingly.
2547 * Return: %true if a PSB packet is found, %false otherwise.
2549 static bool intel_pt_next_psb(unsigned char **buf
, size_t *len
)
2551 unsigned char *next
;
2553 next
= memmem(*buf
, *len
, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2555 *len
-= next
- *buf
;
2563 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2565 * @buf: pointer to buffer pointer
2566 * @len: size of buffer
2568 * Updates the buffer pointer to point to the start of the following PSB packet
2569 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2570 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2572 * Return: %true if a PSB packet is found, %false otherwise.
2574 static bool intel_pt_step_psb(unsigned char **buf
, size_t *len
)
2576 unsigned char *next
;
2581 next
= memmem(*buf
+ 1, *len
- 1, INTEL_PT_PSB_STR
, INTEL_PT_PSB_LEN
);
2583 *len
-= next
- *buf
;
2591 * intel_pt_last_psb - find the last PSB packet in a buffer.
2593 * @len: size of buffer
2595 * This function finds the last PSB in a buffer.
2597 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2599 static unsigned char *intel_pt_last_psb(unsigned char *buf
, size_t len
)
2601 const char *n
= INTEL_PT_PSB_STR
;
2605 if (len
< INTEL_PT_PSB_LEN
)
2608 k
= len
- INTEL_PT_PSB_LEN
+ 1;
2610 p
= memrchr(buf
, n
[0], k
);
2613 if (!memcmp(p
+ 1, n
+ 1, INTEL_PT_PSB_LEN
- 1))
2622 * intel_pt_next_tsc - find and return next TSC.
2624 * @len: size of buffer
2625 * @tsc: TSC value returned
2626 * @rem: returns remaining size when TSC is found
2628 * Find a TSC packet in @buf and return the TSC value. This function assumes
2629 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2630 * PSBEND packet is found.
2632 * Return: %true if TSC is found, false otherwise.
2634 static bool intel_pt_next_tsc(unsigned char *buf
, size_t len
, uint64_t *tsc
,
2637 struct intel_pt_pkt packet
;
2641 ret
= intel_pt_get_packet(buf
, len
, &packet
);
2644 if (packet
.type
== INTEL_PT_TSC
) {
2645 *tsc
= packet
.payload
;
2649 if (packet
.type
== INTEL_PT_PSBEND
)
2658 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2659 * @tsc1: first TSC to compare
2660 * @tsc2: second TSC to compare
2662 * This function compares 7-byte TSC values allowing for the possibility that
2663 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2664 * around so for that purpose this function assumes the absolute difference is
2665 * less than half the maximum difference.
2667 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2670 static int intel_pt_tsc_cmp(uint64_t tsc1
, uint64_t tsc2
)
2672 const uint64_t halfway
= (1ULL << 55);
2678 if (tsc2
- tsc1
< halfway
)
2683 if (tsc1
- tsc2
< halfway
)
2690 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2693 * adj_for_padding - adjust overlap to account for padding.
2694 * @buf_b: second buffer
2695 * @buf_a: first buffer
2696 * @len_a: size of first buffer
2698 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2701 * Return: A pointer into @buf_b from where non-overlapped data starts
2703 static unsigned char *adj_for_padding(unsigned char *buf_b
,
2704 unsigned char *buf_a
, size_t len_a
)
2706 unsigned char *p
= buf_b
- MAX_PADDING
;
2707 unsigned char *q
= buf_a
+ len_a
- MAX_PADDING
;
2710 for (i
= MAX_PADDING
; i
; i
--, p
++, q
++) {
2719 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2721 * @buf_a: first buffer
2722 * @len_a: size of first buffer
2723 * @buf_b: second buffer
2724 * @len_b: size of second buffer
2725 * @consecutive: returns true if there is data in buf_b that is consecutive
2728 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2729 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2730 * walk forward in @buf_b until a later TSC is found. A precondition is that
2731 * @buf_a and @buf_b are positioned at a PSB.
2733 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2734 * @buf_b + @len_b if there is no non-overlapped data.
2736 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a
,
2738 unsigned char *buf_b
,
2739 size_t len_b
, bool *consecutive
)
2741 uint64_t tsc_a
, tsc_b
;
2743 size_t len
, rem_a
, rem_b
;
2745 p
= intel_pt_last_psb(buf_a
, len_a
);
2747 return buf_b
; /* No PSB in buf_a => no overlap */
2749 len
= len_a
- (p
- buf_a
);
2750 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
)) {
2751 /* The last PSB+ in buf_a is incomplete, so go back one more */
2753 p
= intel_pt_last_psb(buf_a
, len_a
);
2755 return buf_b
; /* No full PSB+ => assume no overlap */
2756 len
= len_a
- (p
- buf_a
);
2757 if (!intel_pt_next_tsc(p
, len
, &tsc_a
, &rem_a
))
2758 return buf_b
; /* No TSC in buf_a => assume no overlap */
2762 /* Ignore PSB+ with no TSC */
2763 if (intel_pt_next_tsc(buf_b
, len_b
, &tsc_b
, &rem_b
)) {
2764 int cmp
= intel_pt_tsc_cmp(tsc_a
, tsc_b
);
2766 /* Same TSC, so buffers are consecutive */
2767 if (!cmp
&& rem_b
>= rem_a
) {
2768 unsigned char *start
;
2770 *consecutive
= true;
2771 start
= buf_b
+ len_b
- (rem_b
- rem_a
);
2772 return adj_for_padding(start
, buf_a
, len_a
);
2775 return buf_b
; /* tsc_a < tsc_b => no overlap */
2778 if (!intel_pt_step_psb(&buf_b
, &len_b
))
2779 return buf_b
+ len_b
; /* No PSB in buf_b => no data */
2784 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2785 * @buf_a: first buffer
2786 * @len_a: size of first buffer
2787 * @buf_b: second buffer
2788 * @len_b: size of second buffer
2789 * @have_tsc: can use TSC packets to detect overlap
2790 * @consecutive: returns true if there is data in buf_b that is consecutive
2793 * When trace samples or snapshots are recorded there is the possibility that
2794 * the data overlaps. Note that, for the purposes of decoding, data is only
2795 * useful if it begins with a PSB packet.
2797 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2798 * @buf_b + @len_b if there is no non-overlapped data.
2800 unsigned char *intel_pt_find_overlap(unsigned char *buf_a
, size_t len_a
,
2801 unsigned char *buf_b
, size_t len_b
,
2802 bool have_tsc
, bool *consecutive
)
2804 unsigned char *found
;
2806 /* Buffer 'b' must start at PSB so throw away everything before that */
2807 if (!intel_pt_next_psb(&buf_b
, &len_b
))
2808 return buf_b
+ len_b
; /* No PSB */
2810 if (!intel_pt_next_psb(&buf_a
, &len_a
))
2811 return buf_b
; /* No overlap */
2814 found
= intel_pt_find_overlap_tsc(buf_a
, len_a
, buf_b
, len_b
,
2821 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2822 * we can ignore the first part of buffer 'a'.
2824 while (len_b
< len_a
) {
2825 if (!intel_pt_step_psb(&buf_a
, &len_a
))
2826 return buf_b
; /* No overlap */
2829 /* Now len_b >= len_a */
2831 /* Potential overlap so check the bytes */
2832 found
= memmem(buf_a
, len_a
, buf_b
, len_a
);
2834 *consecutive
= true;
2835 return adj_for_padding(buf_b
+ len_a
, buf_a
, len_a
);
2838 /* Try again at next PSB in buffer 'a' */
2839 if (!intel_pt_step_psb(&buf_a
, &len_a
))
2840 return buf_b
; /* No overlap */