blob: 7ffcbd6fcd1ae8b99cf883c044c1d2ddfb8d11e6 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0-only
Googleraf606d22022-10-26 21:40:12 -07002/*
3 * intel_pt_decoder.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
5 */
6
7#ifndef _GNU_SOURCE
8#define _GNU_SOURCE
9#endif
10#include <stdlib.h>
11#include <stdbool.h>
12#include <string.h>
13#include <errno.h>
14#include <stdint.h>
15#include <inttypes.h>
16#include <linux/compiler.h>
Googler9398cc32022-12-02 17:21:52 +080017#include <linux/string.h>
18#include <linux/zalloc.h>
Googleraf606d22022-10-26 21:40:12 -070019
20#include "../auxtrace.h"
21
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"
26
27#define INTEL_PT_BLK_SIZE 1024
28
29#define BIT63 (((uint64_t)1 << 63))
30
31#define INTEL_PT_RETURN 1
32
33/* Maximum number of loops with no packets consumed i.e. stuck in a loop */
34#define INTEL_PT_MAX_LOOPS 10000
35
36struct intel_pt_blk {
37 struct intel_pt_blk *prev;
38 uint64_t ip[INTEL_PT_BLK_SIZE];
39};
40
41struct intel_pt_stack {
42 struct intel_pt_blk *blk;
43 struct intel_pt_blk *spare;
44 int pos;
45};
46
47enum intel_pt_pkt_state {
48 INTEL_PT_STATE_NO_PSB,
49 INTEL_PT_STATE_NO_IP,
50 INTEL_PT_STATE_ERR_RESYNC,
51 INTEL_PT_STATE_IN_SYNC,
52 INTEL_PT_STATE_TNT_CONT,
53 INTEL_PT_STATE_TNT,
54 INTEL_PT_STATE_TIP,
55 INTEL_PT_STATE_TIP_PGD,
56 INTEL_PT_STATE_FUP,
57 INTEL_PT_STATE_FUP_NO_TIP,
58};
59
60static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
61{
62 switch (pkt_state) {
63 case INTEL_PT_STATE_NO_PSB:
64 case INTEL_PT_STATE_NO_IP:
65 case INTEL_PT_STATE_ERR_RESYNC:
66 case INTEL_PT_STATE_IN_SYNC:
67 case INTEL_PT_STATE_TNT_CONT:
68 return true;
69 case INTEL_PT_STATE_TNT:
70 case INTEL_PT_STATE_TIP:
71 case INTEL_PT_STATE_TIP_PGD:
72 case INTEL_PT_STATE_FUP:
73 case INTEL_PT_STATE_FUP_NO_TIP:
74 return false;
75 default:
76 return true;
77 };
78}
79
80#ifdef INTEL_PT_STRICT
81#define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
82#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
83#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
84#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
85#else
86#define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
87#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
88#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
89#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
90#endif
91
92struct intel_pt_decoder {
93 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
94 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
95 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
96 uint64_t max_insn_cnt, void *data);
97 bool (*pgd_ip)(uint64_t ip, void *data);
Googler9398cc32022-12-02 17:21:52 +080098 int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
Googleraf606d22022-10-26 21:40:12 -070099 void *data;
100 struct intel_pt_state state;
101 const unsigned char *buf;
102 size_t len;
103 bool return_compression;
Googler9398cc32022-12-02 17:21:52 +0800104 bool branch_enable;
Googleraf606d22022-10-26 21:40:12 -0700105 bool mtc_insn;
106 bool pge;
107 bool have_tma;
108 bool have_cyc;
109 bool fixup_last_mtc;
110 bool have_last_ip;
Googler9398cc32022-12-02 17:21:52 +0800111 bool in_psb;
Googleraf606d22022-10-26 21:40:12 -0700112 enum intel_pt_param_flags flags;
113 uint64_t pos;
114 uint64_t last_ip;
115 uint64_t ip;
116 uint64_t cr3;
117 uint64_t timestamp;
118 uint64_t tsc_timestamp;
119 uint64_t ref_timestamp;
Googler9398cc32022-12-02 17:21:52 +0800120 uint64_t buf_timestamp;
Googleraf606d22022-10-26 21:40:12 -0700121 uint64_t sample_timestamp;
122 uint64_t ret_addr;
123 uint64_t ctc_timestamp;
124 uint64_t ctc_delta;
125 uint64_t cycle_cnt;
126 uint64_t cyc_ref_timestamp;
127 uint32_t last_mtc;
128 uint32_t tsc_ctc_ratio_n;
129 uint32_t tsc_ctc_ratio_d;
130 uint32_t tsc_ctc_mult;
131 uint32_t tsc_slip;
132 uint32_t ctc_rem_mask;
133 int mtc_shift;
134 struct intel_pt_stack stack;
135 enum intel_pt_pkt_state pkt_state;
Googler9398cc32022-12-02 17:21:52 +0800136 enum intel_pt_pkt_ctx pkt_ctx;
137 enum intel_pt_pkt_ctx prev_pkt_ctx;
138 enum intel_pt_blk_type blk_type;
139 int blk_type_pos;
Googleraf606d22022-10-26 21:40:12 -0700140 struct intel_pt_pkt packet;
141 struct intel_pt_pkt tnt;
142 int pkt_step;
143 int pkt_len;
144 int last_packet_type;
145 unsigned int cbr;
Googler9398cc32022-12-02 17:21:52 +0800146 unsigned int cbr_seen;
Googleraf606d22022-10-26 21:40:12 -0700147 unsigned int max_non_turbo_ratio;
148 double max_non_turbo_ratio_fp;
149 double cbr_cyc_to_tsc;
150 double calc_cyc_to_tsc;
151 bool have_calc_cyc_to_tsc;
152 int exec_mode;
153 unsigned int insn_bytes;
154 uint64_t period;
155 enum intel_pt_period_type period_type;
156 uint64_t tot_insn_cnt;
157 uint64_t period_insn_cnt;
158 uint64_t period_mask;
159 uint64_t period_ticks;
160 uint64_t last_masked_timestamp;
Googler9398cc32022-12-02 17:21:52 +0800161 uint64_t tot_cyc_cnt;
162 uint64_t sample_tot_cyc_cnt;
163 uint64_t base_cyc_cnt;
164 uint64_t cyc_cnt_timestamp;
165 double tsc_to_cyc;
Googleraf606d22022-10-26 21:40:12 -0700166 bool continuous_period;
167 bool overflow;
168 bool set_fup_tx_flags;
Googler9398cc32022-12-02 17:21:52 +0800169 bool set_fup_ptw;
170 bool set_fup_mwait;
171 bool set_fup_pwre;
172 bool set_fup_exstop;
173 bool set_fup_bep;
174 bool sample_cyc;
Googleraf606d22022-10-26 21:40:12 -0700175 unsigned int fup_tx_flags;
176 unsigned int tx_flags;
Googler9398cc32022-12-02 17:21:52 +0800177 uint64_t fup_ptw_payload;
178 uint64_t fup_mwait_payload;
179 uint64_t fup_pwre_payload;
180 uint64_t cbr_payload;
Googleraf606d22022-10-26 21:40:12 -0700181 uint64_t timestamp_insn_cnt;
182 uint64_t sample_insn_cnt;
183 uint64_t stuck_ip;
184 int no_progress;
185 int stuck_ip_prd;
186 int stuck_ip_cnt;
187 const unsigned char *next_buf;
188 size_t next_len;
189 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
190};
191
192static uint64_t intel_pt_lower_power_of_2(uint64_t x)
193{
194 int i;
195
196 for (i = 0; x != 1; i++)
197 x >>= 1;
198
199 return x << i;
200}
201
202static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
203{
204 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
205 uint64_t period;
206
207 period = intel_pt_lower_power_of_2(decoder->period);
208 decoder->period_mask = ~(period - 1);
209 decoder->period_ticks = period;
210 }
211}
212
213static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
214{
215 if (!d)
216 return 0;
217 return (t / d) * n + ((t % d) * n) / d;
218}
219
220struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
221{
222 struct intel_pt_decoder *decoder;
223
224 if (!params->get_trace || !params->walk_insn)
225 return NULL;
226
227 decoder = zalloc(sizeof(struct intel_pt_decoder));
228 if (!decoder)
229 return NULL;
230
231 decoder->get_trace = params->get_trace;
232 decoder->walk_insn = params->walk_insn;
233 decoder->pgd_ip = params->pgd_ip;
Googler9398cc32022-12-02 17:21:52 +0800234 decoder->lookahead = params->lookahead;
Googleraf606d22022-10-26 21:40:12 -0700235 decoder->data = params->data;
236 decoder->return_compression = params->return_compression;
Googler9398cc32022-12-02 17:21:52 +0800237 decoder->branch_enable = params->branch_enable;
Googleraf606d22022-10-26 21:40:12 -0700238
239 decoder->flags = params->flags;
240
241 decoder->period = params->period;
242 decoder->period_type = params->period_type;
243
244 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
245 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
246
247 intel_pt_setup_period(decoder);
248
249 decoder->mtc_shift = params->mtc_period;
250 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
251
252 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
253 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
254
255 if (!decoder->tsc_ctc_ratio_n)
256 decoder->tsc_ctc_ratio_d = 0;
257
258 if (decoder->tsc_ctc_ratio_d) {
259 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
260 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
261 decoder->tsc_ctc_ratio_d;
262 }
263
264 /*
265 * A TSC packet can slip past MTC packets so that the timestamp appears
266 * to go backwards. One estimate is that can be up to about 40 CPU
267 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
268 * slippage an order of magnitude more to be on the safe side.
269 */
270 decoder->tsc_slip = 0x10000;
271
272 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
273 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
274 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
275 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
276 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
277
278 return decoder;
279}
280
281static void intel_pt_pop_blk(struct intel_pt_stack *stack)
282{
283 struct intel_pt_blk *blk = stack->blk;
284
285 stack->blk = blk->prev;
286 if (!stack->spare)
287 stack->spare = blk;
288 else
289 free(blk);
290}
291
292static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
293{
294 if (!stack->pos) {
295 if (!stack->blk)
296 return 0;
297 intel_pt_pop_blk(stack);
298 if (!stack->blk)
299 return 0;
300 stack->pos = INTEL_PT_BLK_SIZE;
301 }
302 return stack->blk->ip[--stack->pos];
303}
304
305static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
306{
307 struct intel_pt_blk *blk;
308
309 if (stack->spare) {
310 blk = stack->spare;
311 stack->spare = NULL;
312 } else {
313 blk = malloc(sizeof(struct intel_pt_blk));
314 if (!blk)
315 return -ENOMEM;
316 }
317
318 blk->prev = stack->blk;
319 stack->blk = blk;
320 stack->pos = 0;
321 return 0;
322}
323
324static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
325{
326 int err;
327
328 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
329 err = intel_pt_alloc_blk(stack);
330 if (err)
331 return err;
332 }
333
334 stack->blk->ip[stack->pos++] = ip;
335 return 0;
336}
337
338static void intel_pt_clear_stack(struct intel_pt_stack *stack)
339{
340 while (stack->blk)
341 intel_pt_pop_blk(stack);
342 stack->pos = 0;
343}
344
345static void intel_pt_free_stack(struct intel_pt_stack *stack)
346{
347 intel_pt_clear_stack(stack);
348 zfree(&stack->blk);
349 zfree(&stack->spare);
350}
351
352void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
353{
354 intel_pt_free_stack(&decoder->stack);
355 free(decoder);
356}
357
358static int intel_pt_ext_err(int code)
359{
360 switch (code) {
361 case -ENOMEM:
362 return INTEL_PT_ERR_NOMEM;
363 case -ENOSYS:
364 return INTEL_PT_ERR_INTERN;
365 case -EBADMSG:
366 return INTEL_PT_ERR_BADPKT;
367 case -ENODATA:
368 return INTEL_PT_ERR_NODATA;
369 case -EILSEQ:
370 return INTEL_PT_ERR_NOINSN;
371 case -ENOENT:
372 return INTEL_PT_ERR_MISMAT;
373 case -EOVERFLOW:
374 return INTEL_PT_ERR_OVR;
375 case -ENOSPC:
376 return INTEL_PT_ERR_LOST;
377 case -ELOOP:
378 return INTEL_PT_ERR_NELOOP;
379 default:
380 return INTEL_PT_ERR_UNK;
381 }
382}
383
384static const char *intel_pt_err_msgs[] = {
385 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
386 [INTEL_PT_ERR_INTERN] = "Internal error",
387 [INTEL_PT_ERR_BADPKT] = "Bad packet",
388 [INTEL_PT_ERR_NODATA] = "No more data",
389 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
390 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
391 [INTEL_PT_ERR_OVR] = "Overflow packet",
392 [INTEL_PT_ERR_LOST] = "Lost trace data",
393 [INTEL_PT_ERR_UNK] = "Unknown error!",
394 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
395};
396
397int intel_pt__strerror(int code, char *buf, size_t buflen)
398{
399 if (code < 1 || code >= INTEL_PT_ERR_MAX)
400 code = INTEL_PT_ERR_UNK;
401 strlcpy(buf, intel_pt_err_msgs[code], buflen);
402 return 0;
403}
404
405static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
406 uint64_t last_ip)
407{
408 uint64_t ip;
409
410 switch (packet->count) {
411 case 1:
412 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
413 packet->payload;
414 break;
415 case 2:
416 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
417 packet->payload;
418 break;
419 case 3:
420 ip = packet->payload;
421 /* Sign-extend 6-byte ip */
422 if (ip & (uint64_t)0x800000000000ULL)
423 ip |= (uint64_t)0xffff000000000000ULL;
424 break;
425 case 4:
426 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
427 packet->payload;
428 break;
429 case 6:
430 ip = packet->payload;
431 break;
432 default:
433 return 0;
434 }
435
436 return ip;
437}
438
439static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
440{
441 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
442 decoder->have_last_ip = true;
443}
444
445static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
446{
447 intel_pt_set_last_ip(decoder);
448 decoder->ip = decoder->last_ip;
449}
450
451static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
452{
453 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
454 decoder->buf);
455}
456
457static int intel_pt_bug(struct intel_pt_decoder *decoder)
458{
459 intel_pt_log("ERROR: Internal error\n");
460 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
461 return -ENOSYS;
462}
463
464static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
465{
466 decoder->tx_flags = 0;
467}
468
469static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
470{
471 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
472}
473
474static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
475{
476 intel_pt_clear_tx_flags(decoder);
477 decoder->have_tma = false;
478 decoder->pkt_len = 1;
479 decoder->pkt_step = 1;
480 intel_pt_decoder_log_packet(decoder);
481 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
482 intel_pt_log("ERROR: Bad packet\n");
483 decoder->pkt_state = INTEL_PT_STATE_ERR1;
484 }
485 return -EBADMSG;
486}
487
Googler9398cc32022-12-02 17:21:52 +0800488static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
489{
490 decoder->sample_timestamp = decoder->timestamp;
491 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
492}
493
494static void intel_pt_reposition(struct intel_pt_decoder *decoder)
495{
496 decoder->ip = 0;
497 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
498 decoder->timestamp = 0;
499 decoder->have_tma = false;
500}
501
502static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
Googleraf606d22022-10-26 21:40:12 -0700503{
504 struct intel_pt_buffer buffer = { .buf = 0, };
505 int ret;
506
507 decoder->pkt_step = 0;
508
509 intel_pt_log("Getting more data\n");
510 ret = decoder->get_trace(&buffer, decoder->data);
511 if (ret)
512 return ret;
513 decoder->buf = buffer.buf;
514 decoder->len = buffer.len;
515 if (!decoder->len) {
516 intel_pt_log("No more data\n");
517 return -ENODATA;
518 }
Googler9398cc32022-12-02 17:21:52 +0800519 decoder->buf_timestamp = buffer.ref_timestamp;
520 if (!buffer.consecutive || reposition) {
521 intel_pt_reposition(decoder);
Googleraf606d22022-10-26 21:40:12 -0700522 decoder->ref_timestamp = buffer.ref_timestamp;
523 decoder->state.trace_nr = buffer.trace_nr;
524 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
525 decoder->ref_timestamp);
526 return -ENOLINK;
527 }
528
529 return 0;
530}
531
Googler9398cc32022-12-02 17:21:52 +0800532static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
533 bool reposition)
Googleraf606d22022-10-26 21:40:12 -0700534{
535 if (!decoder->next_buf)
Googler9398cc32022-12-02 17:21:52 +0800536 return intel_pt_get_data(decoder, reposition);
Googleraf606d22022-10-26 21:40:12 -0700537
538 decoder->buf = decoder->next_buf;
539 decoder->len = decoder->next_len;
540 decoder->next_buf = 0;
541 decoder->next_len = 0;
542 return 0;
543}
544
545static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
546{
547 unsigned char *buf = decoder->temp_buf;
548 size_t old_len, len, n;
549 int ret;
550
551 old_len = decoder->len;
552 len = decoder->len;
553 memcpy(buf, decoder->buf, len);
554
Googler9398cc32022-12-02 17:21:52 +0800555 ret = intel_pt_get_data(decoder, false);
Googleraf606d22022-10-26 21:40:12 -0700556 if (ret) {
557 decoder->pos += old_len;
558 return ret < 0 ? ret : -EINVAL;
559 }
560
561 n = INTEL_PT_PKT_MAX_SZ - len;
562 if (n > decoder->len)
563 n = decoder->len;
564 memcpy(buf + len, decoder->buf, n);
565 len += n;
566
Googler9398cc32022-12-02 17:21:52 +0800567 decoder->prev_pkt_ctx = decoder->pkt_ctx;
568 ret = intel_pt_get_packet(buf, len, &decoder->packet, &decoder->pkt_ctx);
Googleraf606d22022-10-26 21:40:12 -0700569 if (ret < (int)old_len) {
570 decoder->next_buf = decoder->buf;
571 decoder->next_len = decoder->len;
572 decoder->buf = buf;
573 decoder->len = old_len;
574 return intel_pt_bad_packet(decoder);
575 }
576
577 decoder->next_buf = decoder->buf + (ret - old_len);
578 decoder->next_len = decoder->len - (ret - old_len);
579
580 decoder->buf = buf;
581 decoder->len = ret;
582
583 return ret;
584}
585
586struct intel_pt_pkt_info {
587 struct intel_pt_decoder *decoder;
588 struct intel_pt_pkt packet;
589 uint64_t pos;
590 int pkt_len;
591 int last_packet_type;
592 void *data;
593};
594
595typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
596
597/* Lookahead packets in current buffer */
598static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
599 intel_pt_pkt_cb_t cb, void *data)
600{
601 struct intel_pt_pkt_info pkt_info;
602 const unsigned char *buf = decoder->buf;
Googler9398cc32022-12-02 17:21:52 +0800603 enum intel_pt_pkt_ctx pkt_ctx = decoder->pkt_ctx;
Googleraf606d22022-10-26 21:40:12 -0700604 size_t len = decoder->len;
605 int ret;
606
607 pkt_info.decoder = decoder;
608 pkt_info.pos = decoder->pos;
609 pkt_info.pkt_len = decoder->pkt_step;
610 pkt_info.last_packet_type = decoder->last_packet_type;
611 pkt_info.data = data;
612
613 while (1) {
614 do {
615 pkt_info.pos += pkt_info.pkt_len;
616 buf += pkt_info.pkt_len;
617 len -= pkt_info.pkt_len;
618
619 if (!len)
620 return INTEL_PT_NEED_MORE_BYTES;
621
Googler9398cc32022-12-02 17:21:52 +0800622 ret = intel_pt_get_packet(buf, len, &pkt_info.packet,
623 &pkt_ctx);
Googleraf606d22022-10-26 21:40:12 -0700624 if (!ret)
625 return INTEL_PT_NEED_MORE_BYTES;
626 if (ret < 0)
627 return ret;
628
629 pkt_info.pkt_len = ret;
630 } while (pkt_info.packet.type == INTEL_PT_PAD);
631
632 ret = cb(&pkt_info);
633 if (ret)
634 return 0;
635
636 pkt_info.last_packet_type = pkt_info.packet.type;
637 }
638}
639
640struct intel_pt_calc_cyc_to_tsc_info {
641 uint64_t cycle_cnt;
642 unsigned int cbr;
643 uint32_t last_mtc;
644 uint64_t ctc_timestamp;
645 uint64_t ctc_delta;
646 uint64_t tsc_timestamp;
647 uint64_t timestamp;
648 bool have_tma;
649 bool fixup_last_mtc;
650 bool from_mtc;
651 double cbr_cyc_to_tsc;
652};
653
654/*
655 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
656 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
657 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
658 * packet by copying the missing bits from the current MTC assuming the least
659 * difference between the two, and that the current MTC comes after last_mtc.
660 */
661static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
662 uint32_t *last_mtc)
663{
664 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
665 uint32_t mask = ~(first_missing_bit - 1);
666
667 *last_mtc |= mtc & mask;
668 if (*last_mtc >= mtc) {
669 *last_mtc -= first_missing_bit;
670 *last_mtc &= 0xff;
671 }
672}
673
674static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
675{
676 struct intel_pt_decoder *decoder = pkt_info->decoder;
677 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
678 uint64_t timestamp;
679 double cyc_to_tsc;
680 unsigned int cbr;
681 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
682
683 switch (pkt_info->packet.type) {
684 case INTEL_PT_TNT:
685 case INTEL_PT_TIP_PGE:
686 case INTEL_PT_TIP:
687 case INTEL_PT_FUP:
688 case INTEL_PT_PSB:
689 case INTEL_PT_PIP:
690 case INTEL_PT_MODE_EXEC:
691 case INTEL_PT_MODE_TSX:
692 case INTEL_PT_PSBEND:
693 case INTEL_PT_PAD:
694 case INTEL_PT_VMCS:
695 case INTEL_PT_MNT:
Googler9398cc32022-12-02 17:21:52 +0800696 case INTEL_PT_PTWRITE:
697 case INTEL_PT_PTWRITE_IP:
698 case INTEL_PT_BBP:
699 case INTEL_PT_BIP:
700 case INTEL_PT_BEP:
701 case INTEL_PT_BEP_IP:
Googleraf606d22022-10-26 21:40:12 -0700702 return 0;
703
704 case INTEL_PT_MTC:
705 if (!data->have_tma)
706 return 0;
707
708 mtc = pkt_info->packet.payload;
709 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
710 data->fixup_last_mtc = false;
711 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
712 &data->last_mtc);
713 }
714 if (mtc > data->last_mtc)
715 mtc_delta = mtc - data->last_mtc;
716 else
717 mtc_delta = mtc + 256 - data->last_mtc;
718 data->ctc_delta += mtc_delta << decoder->mtc_shift;
719 data->last_mtc = mtc;
720
721 if (decoder->tsc_ctc_mult) {
722 timestamp = data->ctc_timestamp +
723 data->ctc_delta * decoder->tsc_ctc_mult;
724 } else {
725 timestamp = data->ctc_timestamp +
726 multdiv(data->ctc_delta,
727 decoder->tsc_ctc_ratio_n,
728 decoder->tsc_ctc_ratio_d);
729 }
730
731 if (timestamp < data->timestamp)
732 return 1;
733
734 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
735 data->timestamp = timestamp;
736 return 0;
737 }
738
739 break;
740
741 case INTEL_PT_TSC:
Googler9398cc32022-12-02 17:21:52 +0800742 /*
743 * For now, do not support using TSC packets - refer
744 * intel_pt_calc_cyc_to_tsc().
745 */
746 if (data->from_mtc)
747 return 1;
Googleraf606d22022-10-26 21:40:12 -0700748 timestamp = pkt_info->packet.payload |
749 (data->timestamp & (0xffULL << 56));
750 if (data->from_mtc && timestamp < data->timestamp &&
751 data->timestamp - timestamp < decoder->tsc_slip)
752 return 1;
753 if (timestamp < data->timestamp)
754 timestamp += (1ULL << 56);
755 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
756 if (data->from_mtc)
757 return 1;
758 data->tsc_timestamp = timestamp;
759 data->timestamp = timestamp;
760 return 0;
761 }
762 break;
763
764 case INTEL_PT_TMA:
765 if (data->from_mtc)
766 return 1;
767
768 if (!decoder->tsc_ctc_ratio_d)
769 return 0;
770
771 ctc = pkt_info->packet.payload;
772 fc = pkt_info->packet.count;
773 ctc_rem = ctc & decoder->ctc_rem_mask;
774
775 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
776
777 data->ctc_timestamp = data->tsc_timestamp - fc;
778 if (decoder->tsc_ctc_mult) {
779 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
780 } else {
781 data->ctc_timestamp -=
782 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
783 decoder->tsc_ctc_ratio_d);
784 }
785
786 data->ctc_delta = 0;
787 data->have_tma = true;
788 data->fixup_last_mtc = true;
789
790 return 0;
791
792 case INTEL_PT_CYC:
793 data->cycle_cnt += pkt_info->packet.payload;
794 return 0;
795
796 case INTEL_PT_CBR:
797 cbr = pkt_info->packet.payload;
798 if (data->cbr && data->cbr != cbr)
799 return 1;
800 data->cbr = cbr;
801 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
802 return 0;
803
804 case INTEL_PT_TIP_PGD:
805 case INTEL_PT_TRACESTOP:
Googler9398cc32022-12-02 17:21:52 +0800806 case INTEL_PT_EXSTOP:
807 case INTEL_PT_EXSTOP_IP:
808 case INTEL_PT_MWAIT:
809 case INTEL_PT_PWRE:
810 case INTEL_PT_PWRX:
Googleraf606d22022-10-26 21:40:12 -0700811 case INTEL_PT_OVF:
812 case INTEL_PT_BAD: /* Does not happen */
813 default:
814 return 1;
815 }
816
817 if (!data->cbr && decoder->cbr) {
818 data->cbr = decoder->cbr;
819 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
820 }
821
822 if (!data->cycle_cnt)
823 return 1;
824
825 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
826
827 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
828 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
829 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
830 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
831 return 1;
832 }
833
834 decoder->calc_cyc_to_tsc = cyc_to_tsc;
835 decoder->have_calc_cyc_to_tsc = true;
836
837 if (data->cbr) {
838 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
839 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
840 } else {
841 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
842 cyc_to_tsc, pkt_info->pos);
843 }
844
845 return 1;
846}
847
848static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
849 bool from_mtc)
850{
851 struct intel_pt_calc_cyc_to_tsc_info data = {
852 .cycle_cnt = 0,
853 .cbr = 0,
854 .last_mtc = decoder->last_mtc,
855 .ctc_timestamp = decoder->ctc_timestamp,
856 .ctc_delta = decoder->ctc_delta,
857 .tsc_timestamp = decoder->tsc_timestamp,
858 .timestamp = decoder->timestamp,
859 .have_tma = decoder->have_tma,
860 .fixup_last_mtc = decoder->fixup_last_mtc,
861 .from_mtc = from_mtc,
862 .cbr_cyc_to_tsc = 0,
863 };
864
Googler9398cc32022-12-02 17:21:52 +0800865 /*
866 * For now, do not support using TSC packets for at least the reasons:
867 * 1) timing might have stopped
868 * 2) TSC packets within PSB+ can slip against CYC packets
869 */
870 if (!from_mtc)
871 return;
872
Googleraf606d22022-10-26 21:40:12 -0700873 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
874}
875
876static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
877{
878 int ret;
879
880 decoder->last_packet_type = decoder->packet.type;
881
882 do {
883 decoder->pos += decoder->pkt_step;
884 decoder->buf += decoder->pkt_step;
885 decoder->len -= decoder->pkt_step;
886
887 if (!decoder->len) {
Googler9398cc32022-12-02 17:21:52 +0800888 ret = intel_pt_get_next_data(decoder, false);
Googleraf606d22022-10-26 21:40:12 -0700889 if (ret)
890 return ret;
891 }
892
Googler9398cc32022-12-02 17:21:52 +0800893 decoder->prev_pkt_ctx = decoder->pkt_ctx;
Googleraf606d22022-10-26 21:40:12 -0700894 ret = intel_pt_get_packet(decoder->buf, decoder->len,
Googler9398cc32022-12-02 17:21:52 +0800895 &decoder->packet, &decoder->pkt_ctx);
896 if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
Googleraf606d22022-10-26 21:40:12 -0700897 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
898 ret = intel_pt_get_split_packet(decoder);
899 if (ret < 0)
900 return ret;
901 }
902 if (ret <= 0)
903 return intel_pt_bad_packet(decoder);
904
905 decoder->pkt_len = ret;
906 decoder->pkt_step = ret;
907 intel_pt_decoder_log_packet(decoder);
908 } while (decoder->packet.type == INTEL_PT_PAD);
909
910 return 0;
911}
912
913static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
914{
915 uint64_t timestamp, masked_timestamp;
916
917 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
918 masked_timestamp = timestamp & decoder->period_mask;
919 if (decoder->continuous_period) {
920 if (masked_timestamp > decoder->last_masked_timestamp)
921 return 1;
922 } else {
923 timestamp += 1;
924 masked_timestamp = timestamp & decoder->period_mask;
925 if (masked_timestamp > decoder->last_masked_timestamp) {
926 decoder->last_masked_timestamp = masked_timestamp;
927 decoder->continuous_period = true;
928 }
929 }
930
931 if (masked_timestamp < decoder->last_masked_timestamp)
932 return decoder->period_ticks;
933
934 return decoder->period_ticks - (timestamp - masked_timestamp);
935}
936
937static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
938{
939 switch (decoder->period_type) {
940 case INTEL_PT_PERIOD_INSTRUCTIONS:
941 return decoder->period - decoder->period_insn_cnt;
942 case INTEL_PT_PERIOD_TICKS:
943 return intel_pt_next_period(decoder);
944 case INTEL_PT_PERIOD_NONE:
945 case INTEL_PT_PERIOD_MTC:
946 default:
947 return 0;
948 }
949}
950
951static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
952{
953 uint64_t timestamp, masked_timestamp;
954
955 switch (decoder->period_type) {
956 case INTEL_PT_PERIOD_INSTRUCTIONS:
957 decoder->period_insn_cnt = 0;
958 break;
959 case INTEL_PT_PERIOD_TICKS:
960 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
961 masked_timestamp = timestamp & decoder->period_mask;
962 if (masked_timestamp > decoder->last_masked_timestamp)
963 decoder->last_masked_timestamp = masked_timestamp;
964 else
965 decoder->last_masked_timestamp += decoder->period_ticks;
966 break;
967 case INTEL_PT_PERIOD_NONE:
968 case INTEL_PT_PERIOD_MTC:
969 default:
970 break;
971 }
972
973 decoder->state.type |= INTEL_PT_INSTRUCTION;
974}
975
976static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
977 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
978{
979 uint64_t max_insn_cnt, insn_cnt = 0;
980 int err;
981
982 if (!decoder->mtc_insn)
983 decoder->mtc_insn = true;
984
985 max_insn_cnt = intel_pt_next_sample(decoder);
986
987 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
988 max_insn_cnt, decoder->data);
989
990 decoder->tot_insn_cnt += insn_cnt;
991 decoder->timestamp_insn_cnt += insn_cnt;
992 decoder->sample_insn_cnt += insn_cnt;
993 decoder->period_insn_cnt += insn_cnt;
994
995 if (err) {
996 decoder->no_progress = 0;
997 decoder->pkt_state = INTEL_PT_STATE_ERR2;
998 intel_pt_log_at("ERROR: Failed to get instruction",
999 decoder->ip);
1000 if (err == -ENOENT)
1001 return -ENOLINK;
1002 return -EILSEQ;
1003 }
1004
1005 if (ip && decoder->ip == ip) {
1006 err = -EAGAIN;
1007 goto out;
1008 }
1009
1010 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
1011 intel_pt_sample_insn(decoder);
1012
1013 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
1014 decoder->state.type = INTEL_PT_INSTRUCTION;
1015 decoder->state.from_ip = decoder->ip;
1016 decoder->state.to_ip = 0;
1017 decoder->ip += intel_pt_insn->length;
1018 err = INTEL_PT_RETURN;
1019 goto out;
1020 }
1021
1022 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
1023 /* Zero-length calls are excluded */
1024 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
1025 intel_pt_insn->rel) {
1026 err = intel_pt_push(&decoder->stack, decoder->ip +
1027 intel_pt_insn->length);
1028 if (err)
1029 goto out;
1030 }
1031 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
1032 decoder->ret_addr = intel_pt_pop(&decoder->stack);
1033 }
1034
1035 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1036 int cnt = decoder->no_progress++;
1037
1038 decoder->state.from_ip = decoder->ip;
1039 decoder->ip += intel_pt_insn->length +
1040 intel_pt_insn->rel;
1041 decoder->state.to_ip = decoder->ip;
1042 err = INTEL_PT_RETURN;
1043
1044 /*
1045 * Check for being stuck in a loop. This can happen if a
1046 * decoder error results in the decoder erroneously setting the
1047 * ip to an address that is itself in an infinite loop that
1048 * consumes no packets. When that happens, there must be an
1049 * unconditional branch.
1050 */
1051 if (cnt) {
1052 if (cnt == 1) {
1053 decoder->stuck_ip = decoder->state.to_ip;
1054 decoder->stuck_ip_prd = 1;
1055 decoder->stuck_ip_cnt = 1;
1056 } else if (cnt > INTEL_PT_MAX_LOOPS ||
1057 decoder->state.to_ip == decoder->stuck_ip) {
1058 intel_pt_log_at("ERROR: Never-ending loop",
1059 decoder->state.to_ip);
1060 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1061 err = -ELOOP;
1062 goto out;
1063 } else if (!--decoder->stuck_ip_cnt) {
1064 decoder->stuck_ip_prd += 1;
1065 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1066 decoder->stuck_ip = decoder->state.to_ip;
1067 }
1068 }
1069 goto out_no_progress;
1070 }
1071out:
1072 decoder->no_progress = 0;
1073out_no_progress:
1074 decoder->state.insn_op = intel_pt_insn->op;
1075 decoder->state.insn_len = intel_pt_insn->length;
Googler9398cc32022-12-02 17:21:52 +08001076 memcpy(decoder->state.insn, intel_pt_insn->buf,
1077 INTEL_PT_INSN_BUF_SZ);
Googleraf606d22022-10-26 21:40:12 -07001078
1079 if (decoder->tx_flags & INTEL_PT_IN_TX)
1080 decoder->state.flags |= INTEL_PT_IN_TX;
1081
1082 return err;
1083}
1084
Googler9398cc32022-12-02 17:21:52 +08001085static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1086{
1087 bool ret = false;
1088
1089 if (decoder->set_fup_tx_flags) {
1090 decoder->set_fup_tx_flags = false;
1091 decoder->tx_flags = decoder->fup_tx_flags;
1092 decoder->state.type = INTEL_PT_TRANSACTION;
Googler9398cc32022-12-02 17:21:52 +08001093 decoder->state.from_ip = decoder->ip;
1094 decoder->state.to_ip = 0;
1095 decoder->state.flags = decoder->fup_tx_flags;
1096 return true;
1097 }
1098 if (decoder->set_fup_ptw) {
1099 decoder->set_fup_ptw = false;
1100 decoder->state.type = INTEL_PT_PTW;
1101 decoder->state.flags |= INTEL_PT_FUP_IP;
1102 decoder->state.from_ip = decoder->ip;
1103 decoder->state.to_ip = 0;
1104 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1105 return true;
1106 }
1107 if (decoder->set_fup_mwait) {
1108 decoder->set_fup_mwait = false;
1109 decoder->state.type = INTEL_PT_MWAIT_OP;
1110 decoder->state.from_ip = decoder->ip;
1111 decoder->state.to_ip = 0;
1112 decoder->state.mwait_payload = decoder->fup_mwait_payload;
1113 ret = true;
1114 }
1115 if (decoder->set_fup_pwre) {
1116 decoder->set_fup_pwre = false;
1117 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1118 decoder->state.type &= ~INTEL_PT_BRANCH;
1119 decoder->state.from_ip = decoder->ip;
1120 decoder->state.to_ip = 0;
1121 decoder->state.pwre_payload = decoder->fup_pwre_payload;
1122 ret = true;
1123 }
1124 if (decoder->set_fup_exstop) {
1125 decoder->set_fup_exstop = false;
1126 decoder->state.type |= INTEL_PT_EX_STOP;
1127 decoder->state.type &= ~INTEL_PT_BRANCH;
1128 decoder->state.flags |= INTEL_PT_FUP_IP;
1129 decoder->state.from_ip = decoder->ip;
1130 decoder->state.to_ip = 0;
1131 ret = true;
1132 }
1133 if (decoder->set_fup_bep) {
1134 decoder->set_fup_bep = false;
1135 decoder->state.type |= INTEL_PT_BLK_ITEMS;
1136 decoder->state.type &= ~INTEL_PT_BRANCH;
1137 decoder->state.from_ip = decoder->ip;
1138 decoder->state.to_ip = 0;
1139 ret = true;
1140 }
1141 return ret;
1142}
1143
Googleraf606d22022-10-26 21:40:12 -07001144static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1145 struct intel_pt_insn *intel_pt_insn,
1146 uint64_t ip, int err)
1147{
1148 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1149 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1150 ip == decoder->ip + intel_pt_insn->length;
1151}
1152
1153static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1154{
1155 struct intel_pt_insn intel_pt_insn;
1156 uint64_t ip;
1157 int err;
1158
1159 ip = decoder->last_ip;
1160
1161 while (1) {
1162 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1163 if (err == INTEL_PT_RETURN)
1164 return 0;
1165 if (err == -EAGAIN ||
1166 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
Googler9398cc32022-12-02 17:21:52 +08001167 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
Googlerb48fa912023-03-17 12:40:29 +05301168 if (intel_pt_fup_event(decoder))
Googleraf606d22022-10-26 21:40:12 -07001169 return 0;
1170 return -EAGAIN;
1171 }
1172 decoder->set_fup_tx_flags = false;
1173 if (err)
1174 return err;
1175
1176 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1177 intel_pt_log_at("ERROR: Unexpected indirect branch",
1178 decoder->ip);
1179 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1180 return -ENOENT;
1181 }
1182
1183 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1184 intel_pt_log_at("ERROR: Unexpected conditional branch",
1185 decoder->ip);
1186 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1187 return -ENOENT;
1188 }
1189
1190 intel_pt_bug(decoder);
1191 }
1192}
1193
1194static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1195{
1196 struct intel_pt_insn intel_pt_insn;
1197 int err;
1198
1199 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1200 if (err == INTEL_PT_RETURN &&
1201 decoder->pgd_ip &&
1202 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1203 (decoder->state.type & INTEL_PT_BRANCH) &&
1204 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1205 /* Unconditional branch leaving filter region */
1206 decoder->no_progress = 0;
1207 decoder->pge = false;
1208 decoder->continuous_period = false;
1209 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
Googler9398cc32022-12-02 17:21:52 +08001210 decoder->state.type |= INTEL_PT_TRACE_END;
Googleraf606d22022-10-26 21:40:12 -07001211 return 0;
1212 }
1213 if (err == INTEL_PT_RETURN)
1214 return 0;
1215 if (err)
1216 return err;
1217
1218 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1219 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1220 decoder->pge = false;
1221 decoder->continuous_period = false;
1222 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1223 decoder->state.from_ip = decoder->ip;
Googler9398cc32022-12-02 17:21:52 +08001224 if (decoder->packet.count == 0) {
1225 decoder->state.to_ip = 0;
1226 } else {
1227 decoder->state.to_ip = decoder->last_ip;
Googleraf606d22022-10-26 21:40:12 -07001228 decoder->ip = decoder->last_ip;
Googler9398cc32022-12-02 17:21:52 +08001229 }
1230 decoder->state.type |= INTEL_PT_TRACE_END;
Googleraf606d22022-10-26 21:40:12 -07001231 } else {
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;
1236 } else {
1237 decoder->state.to_ip = decoder->last_ip;
1238 decoder->ip = decoder->last_ip;
1239 }
1240 }
1241 return 0;
1242 }
1243
1244 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1245 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1246 intel_pt_insn.rel;
1247
1248 if (decoder->pgd_ip &&
1249 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1250 decoder->pgd_ip(to_ip, decoder->data)) {
1251 /* Conditional branch leaving filter region */
1252 decoder->pge = false;
1253 decoder->continuous_period = false;
1254 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1255 decoder->ip = to_ip;
1256 decoder->state.from_ip = decoder->ip;
Googler9398cc32022-12-02 17:21:52 +08001257 decoder->state.to_ip = to_ip;
1258 decoder->state.type |= INTEL_PT_TRACE_END;
Googleraf606d22022-10-26 21:40:12 -07001259 return 0;
1260 }
1261 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1262 decoder->ip);
1263 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1264 return -ENOENT;
1265 }
1266
1267 return intel_pt_bug(decoder);
1268}
1269
1270static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1271{
1272 struct intel_pt_insn intel_pt_insn;
1273 int err;
1274
1275 while (1) {
1276 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1277 if (err == INTEL_PT_RETURN)
1278 return 0;
1279 if (err)
1280 return err;
1281
1282 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1283 if (!decoder->return_compression) {
1284 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1285 decoder->ip);
1286 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1287 return -ENOENT;
1288 }
1289 if (!decoder->ret_addr) {
1290 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1291 decoder->ip);
1292 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1293 return -ENOENT;
1294 }
1295 if (!(decoder->tnt.payload & BIT63)) {
1296 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1297 decoder->ip);
1298 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1299 return -ENOENT;
1300 }
1301 decoder->tnt.count -= 1;
1302 if (decoder->tnt.count)
1303 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1304 else
1305 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1306 decoder->tnt.payload <<= 1;
1307 decoder->state.from_ip = decoder->ip;
1308 decoder->ip = decoder->ret_addr;
1309 decoder->state.to_ip = decoder->ip;
1310 return 0;
1311 }
1312
1313 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1314 /* Handle deferred TIPs */
1315 err = intel_pt_get_next_packet(decoder);
1316 if (err)
1317 return err;
1318 if (decoder->packet.type != INTEL_PT_TIP ||
1319 decoder->packet.count == 0) {
1320 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1321 decoder->ip);
1322 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1323 decoder->pkt_step = 0;
1324 return -ENOENT;
1325 }
1326 intel_pt_set_last_ip(decoder);
1327 decoder->state.from_ip = decoder->ip;
1328 decoder->state.to_ip = decoder->last_ip;
1329 decoder->ip = decoder->last_ip;
1330 return 0;
1331 }
1332
1333 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1334 decoder->tnt.count -= 1;
1335 if (decoder->tnt.count)
1336 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1337 else
1338 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1339 if (decoder->tnt.payload & BIT63) {
1340 decoder->tnt.payload <<= 1;
1341 decoder->state.from_ip = decoder->ip;
1342 decoder->ip += intel_pt_insn.length +
1343 intel_pt_insn.rel;
1344 decoder->state.to_ip = decoder->ip;
1345 return 0;
1346 }
1347 /* Instruction sample for a non-taken branch */
1348 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1349 decoder->tnt.payload <<= 1;
1350 decoder->state.type = INTEL_PT_INSTRUCTION;
1351 decoder->state.from_ip = decoder->ip;
1352 decoder->state.to_ip = 0;
1353 decoder->ip += intel_pt_insn.length;
1354 return 0;
1355 }
Googler9398cc32022-12-02 17:21:52 +08001356 decoder->sample_cyc = false;
Googleraf606d22022-10-26 21:40:12 -07001357 decoder->ip += intel_pt_insn.length;
1358 if (!decoder->tnt.count) {
Googler9398cc32022-12-02 17:21:52 +08001359 intel_pt_update_sample_time(decoder);
Googleraf606d22022-10-26 21:40:12 -07001360 return -EAGAIN;
1361 }
1362 decoder->tnt.payload <<= 1;
1363 continue;
1364 }
1365
1366 return intel_pt_bug(decoder);
1367 }
1368}
1369
1370static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1371{
1372 unsigned int fup_tx_flags;
1373 int err;
1374
1375 fup_tx_flags = decoder->packet.payload &
1376 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1377 err = intel_pt_get_next_packet(decoder);
1378 if (err)
1379 return err;
1380 if (decoder->packet.type == INTEL_PT_FUP) {
1381 decoder->fup_tx_flags = fup_tx_flags;
1382 decoder->set_fup_tx_flags = true;
1383 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1384 *no_tip = true;
1385 } else {
1386 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1387 decoder->pos);
1388 intel_pt_update_in_tx(decoder);
1389 }
1390 return 0;
1391}
1392
Googler9398cc32022-12-02 17:21:52 +08001393static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
1394{
1395 timestamp |= (ref_timestamp & (0xffULL << 56));
1396
1397 if (timestamp < ref_timestamp) {
1398 if (ref_timestamp - timestamp > (1ULL << 55))
1399 timestamp += (1ULL << 56);
1400 } else {
1401 if (timestamp - ref_timestamp > (1ULL << 55))
1402 timestamp -= (1ULL << 56);
1403 }
1404
1405 return timestamp;
1406}
1407
Googleraf606d22022-10-26 21:40:12 -07001408static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1409{
1410 uint64_t timestamp;
1411
1412 decoder->have_tma = false;
1413
1414 if (decoder->ref_timestamp) {
Googler9398cc32022-12-02 17:21:52 +08001415 timestamp = intel_pt_8b_tsc(decoder->packet.payload,
1416 decoder->ref_timestamp);
Googleraf606d22022-10-26 21:40:12 -07001417 decoder->tsc_timestamp = timestamp;
1418 decoder->timestamp = timestamp;
1419 decoder->ref_timestamp = 0;
1420 decoder->timestamp_insn_cnt = 0;
1421 } else if (decoder->timestamp) {
1422 timestamp = decoder->packet.payload |
1423 (decoder->timestamp & (0xffULL << 56));
1424 decoder->tsc_timestamp = timestamp;
1425 if (timestamp < decoder->timestamp &&
1426 decoder->timestamp - timestamp < decoder->tsc_slip) {
1427 intel_pt_log_to("Suppressing backwards timestamp",
1428 timestamp);
1429 timestamp = decoder->timestamp;
1430 }
1431 if (timestamp < decoder->timestamp) {
1432 intel_pt_log_to("Wraparound timestamp", timestamp);
1433 timestamp += (1ULL << 56);
1434 decoder->tsc_timestamp = timestamp;
1435 }
1436 decoder->timestamp = timestamp;
1437 decoder->timestamp_insn_cnt = 0;
1438 }
1439
1440 if (decoder->last_packet_type == INTEL_PT_CYC) {
1441 decoder->cyc_ref_timestamp = decoder->timestamp;
1442 decoder->cycle_cnt = 0;
1443 decoder->have_calc_cyc_to_tsc = false;
1444 intel_pt_calc_cyc_to_tsc(decoder, false);
1445 }
1446
1447 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1448}
1449
1450static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1451{
1452 intel_pt_log("ERROR: Buffer overflow\n");
1453 intel_pt_clear_tx_flags(decoder);
1454 decoder->timestamp_insn_cnt = 0;
1455 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1456 decoder->overflow = true;
1457 return -EOVERFLOW;
1458}
1459
Googler9398cc32022-12-02 17:21:52 +08001460static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
1461{
1462 if (decoder->have_cyc)
1463 return;
1464
1465 decoder->cyc_cnt_timestamp = decoder->timestamp;
1466 decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
1467}
1468
1469static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
1470{
1471 decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
1472
1473 if (decoder->pge)
1474 intel_pt_mtc_cyc_cnt_pge(decoder);
1475}
1476
1477static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
1478{
1479 uint64_t tot_cyc_cnt, tsc_delta;
1480
1481 if (decoder->have_cyc)
1482 return;
1483
1484 decoder->sample_cyc = true;
1485
1486 if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
1487 return;
1488
1489 tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
1490 tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
1491
1492 if (tot_cyc_cnt > decoder->tot_cyc_cnt)
1493 decoder->tot_cyc_cnt = tot_cyc_cnt;
1494}
1495
Googleraf606d22022-10-26 21:40:12 -07001496static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1497{
1498 uint32_t ctc = decoder->packet.payload;
1499 uint32_t fc = decoder->packet.count;
1500 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1501
1502 if (!decoder->tsc_ctc_ratio_d)
1503 return;
1504
Googler9398cc32022-12-02 17:21:52 +08001505 if (decoder->pge && !decoder->in_psb)
1506 intel_pt_mtc_cyc_cnt_pge(decoder);
1507 else
1508 intel_pt_mtc_cyc_cnt_upd(decoder);
1509
Googleraf606d22022-10-26 21:40:12 -07001510 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1511 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1512 if (decoder->tsc_ctc_mult) {
1513 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1514 } else {
1515 decoder->ctc_timestamp -= multdiv(ctc_rem,
1516 decoder->tsc_ctc_ratio_n,
1517 decoder->tsc_ctc_ratio_d);
1518 }
1519 decoder->ctc_delta = 0;
1520 decoder->have_tma = true;
1521 decoder->fixup_last_mtc = true;
1522 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1523 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1524}
1525
1526static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1527{
1528 uint64_t timestamp;
1529 uint32_t mtc, mtc_delta;
1530
1531 if (!decoder->have_tma)
1532 return;
1533
1534 mtc = decoder->packet.payload;
1535
1536 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1537 decoder->fixup_last_mtc = false;
1538 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1539 &decoder->last_mtc);
1540 }
1541
1542 if (mtc > decoder->last_mtc)
1543 mtc_delta = mtc - decoder->last_mtc;
1544 else
1545 mtc_delta = mtc + 256 - decoder->last_mtc;
1546
1547 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1548
1549 if (decoder->tsc_ctc_mult) {
1550 timestamp = decoder->ctc_timestamp +
1551 decoder->ctc_delta * decoder->tsc_ctc_mult;
1552 } else {
1553 timestamp = decoder->ctc_timestamp +
1554 multdiv(decoder->ctc_delta,
1555 decoder->tsc_ctc_ratio_n,
1556 decoder->tsc_ctc_ratio_d);
1557 }
1558
1559 if (timestamp < decoder->timestamp)
1560 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1561 timestamp, decoder->timestamp);
1562 else
1563 decoder->timestamp = timestamp;
1564
Googler9398cc32022-12-02 17:21:52 +08001565 intel_pt_mtc_cyc_cnt_upd(decoder);
1566
Googleraf606d22022-10-26 21:40:12 -07001567 decoder->timestamp_insn_cnt = 0;
1568 decoder->last_mtc = mtc;
1569
1570 if (decoder->last_packet_type == INTEL_PT_CYC) {
1571 decoder->cyc_ref_timestamp = decoder->timestamp;
1572 decoder->cycle_cnt = 0;
1573 decoder->have_calc_cyc_to_tsc = false;
1574 intel_pt_calc_cyc_to_tsc(decoder, true);
1575 }
Googler9398cc32022-12-02 17:21:52 +08001576
1577 intel_pt_log_to("Setting timestamp", decoder->timestamp);
Googleraf606d22022-10-26 21:40:12 -07001578}
1579
1580static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1581{
Googler9398cc32022-12-02 17:21:52 +08001582 unsigned int cbr = decoder->packet.payload & 0xff;
1583
1584 decoder->cbr_payload = decoder->packet.payload;
Googleraf606d22022-10-26 21:40:12 -07001585
1586 if (decoder->cbr == cbr)
1587 return;
1588
1589 decoder->cbr = cbr;
1590 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
Googler9398cc32022-12-02 17:21:52 +08001591
1592 intel_pt_mtc_cyc_cnt_cbr(decoder);
Googleraf606d22022-10-26 21:40:12 -07001593}
1594
1595static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1596{
1597 uint64_t timestamp = decoder->cyc_ref_timestamp;
1598
1599 decoder->have_cyc = true;
1600
1601 decoder->cycle_cnt += decoder->packet.payload;
Googler9398cc32022-12-02 17:21:52 +08001602 if (decoder->pge)
1603 decoder->tot_cyc_cnt += decoder->packet.payload;
1604 decoder->sample_cyc = true;
Googleraf606d22022-10-26 21:40:12 -07001605
1606 if (!decoder->cyc_ref_timestamp)
1607 return;
1608
1609 if (decoder->have_calc_cyc_to_tsc)
1610 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1611 else if (decoder->cbr)
1612 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1613 else
1614 return;
1615
1616 if (timestamp < decoder->timestamp)
1617 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1618 timestamp, decoder->timestamp);
1619 else
1620 decoder->timestamp = timestamp;
1621
1622 decoder->timestamp_insn_cnt = 0;
Googler9398cc32022-12-02 17:21:52 +08001623
1624 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1625}
1626
1627static void intel_pt_bbp(struct intel_pt_decoder *decoder)
1628{
1629 if (decoder->prev_pkt_ctx == INTEL_PT_NO_CTX) {
1630 memset(decoder->state.items.mask, 0, sizeof(decoder->state.items.mask));
1631 decoder->state.items.is_32_bit = false;
1632 }
1633 decoder->blk_type = decoder->packet.payload;
1634 decoder->blk_type_pos = intel_pt_blk_type_pos(decoder->blk_type);
1635 if (decoder->blk_type == INTEL_PT_GP_REGS)
1636 decoder->state.items.is_32_bit = decoder->packet.count;
1637 if (decoder->blk_type_pos < 0) {
1638 intel_pt_log("WARNING: Unknown block type %u\n",
1639 decoder->blk_type);
1640 } else if (decoder->state.items.mask[decoder->blk_type_pos]) {
1641 intel_pt_log("WARNING: Duplicate block type %u\n",
1642 decoder->blk_type);
1643 }
1644}
1645
1646static void intel_pt_bip(struct intel_pt_decoder *decoder)
1647{
1648 uint32_t id = decoder->packet.count;
1649 uint32_t bit = 1 << id;
1650 int pos = decoder->blk_type_pos;
1651
1652 if (pos < 0 || id >= INTEL_PT_BLK_ITEM_ID_CNT) {
1653 intel_pt_log("WARNING: Unknown block item %u type %d\n",
1654 id, decoder->blk_type);
1655 return;
1656 }
1657
1658 if (decoder->state.items.mask[pos] & bit) {
1659 intel_pt_log("WARNING: Duplicate block item %u type %d\n",
1660 id, decoder->blk_type);
1661 }
1662
1663 decoder->state.items.mask[pos] |= bit;
1664 decoder->state.items.val[pos][id] = decoder->packet.payload;
Googleraf606d22022-10-26 21:40:12 -07001665}
1666
1667/* Walk PSB+ packets when already in sync. */
1668static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1669{
1670 int err;
1671
Googler9398cc32022-12-02 17:21:52 +08001672 decoder->in_psb = true;
1673
Googleraf606d22022-10-26 21:40:12 -07001674 while (1) {
1675 err = intel_pt_get_next_packet(decoder);
1676 if (err)
Googler9398cc32022-12-02 17:21:52 +08001677 goto out;
Googleraf606d22022-10-26 21:40:12 -07001678
1679 switch (decoder->packet.type) {
1680 case INTEL_PT_PSBEND:
Googler9398cc32022-12-02 17:21:52 +08001681 err = 0;
1682 goto out;
Googleraf606d22022-10-26 21:40:12 -07001683
1684 case INTEL_PT_TIP_PGD:
1685 case INTEL_PT_TIP_PGE:
1686 case INTEL_PT_TIP:
1687 case INTEL_PT_TNT:
1688 case INTEL_PT_TRACESTOP:
1689 case INTEL_PT_BAD:
1690 case INTEL_PT_PSB:
Googler9398cc32022-12-02 17:21:52 +08001691 case INTEL_PT_PTWRITE:
1692 case INTEL_PT_PTWRITE_IP:
1693 case INTEL_PT_EXSTOP:
1694 case INTEL_PT_EXSTOP_IP:
1695 case INTEL_PT_MWAIT:
1696 case INTEL_PT_PWRE:
1697 case INTEL_PT_PWRX:
1698 case INTEL_PT_BBP:
1699 case INTEL_PT_BIP:
1700 case INTEL_PT_BEP:
1701 case INTEL_PT_BEP_IP:
Googleraf606d22022-10-26 21:40:12 -07001702 decoder->have_tma = false;
1703 intel_pt_log("ERROR: Unexpected packet\n");
Googler9398cc32022-12-02 17:21:52 +08001704 err = -EAGAIN;
1705 goto out;
Googleraf606d22022-10-26 21:40:12 -07001706
1707 case INTEL_PT_OVF:
Googler9398cc32022-12-02 17:21:52 +08001708 err = intel_pt_overflow(decoder);
1709 goto out;
Googleraf606d22022-10-26 21:40:12 -07001710
1711 case INTEL_PT_TSC:
1712 intel_pt_calc_tsc_timestamp(decoder);
1713 break;
1714
1715 case INTEL_PT_TMA:
1716 intel_pt_calc_tma(decoder);
1717 break;
1718
1719 case INTEL_PT_CBR:
1720 intel_pt_calc_cbr(decoder);
1721 break;
1722
1723 case INTEL_PT_MODE_EXEC:
1724 decoder->exec_mode = decoder->packet.payload;
1725 break;
1726
1727 case INTEL_PT_PIP:
1728 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1729 break;
1730
1731 case INTEL_PT_FUP:
1732 decoder->pge = true;
1733 if (decoder->packet.count)
1734 intel_pt_set_last_ip(decoder);
1735 break;
1736
1737 case INTEL_PT_MODE_TSX:
1738 intel_pt_update_in_tx(decoder);
1739 break;
1740
1741 case INTEL_PT_MTC:
1742 intel_pt_calc_mtc_timestamp(decoder);
1743 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1744 decoder->state.type |= INTEL_PT_INSTRUCTION;
1745 break;
1746
1747 case INTEL_PT_CYC:
1748 case INTEL_PT_VMCS:
1749 case INTEL_PT_MNT:
1750 case INTEL_PT_PAD:
1751 default:
1752 break;
1753 }
1754 }
Googler9398cc32022-12-02 17:21:52 +08001755out:
1756 decoder->in_psb = false;
1757
1758 return err;
Googleraf606d22022-10-26 21:40:12 -07001759}
1760
1761static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1762{
1763 int err;
1764
1765 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1766 decoder->tx_flags = 0;
1767 decoder->state.flags &= ~INTEL_PT_IN_TX;
1768 decoder->state.flags |= INTEL_PT_ABORT_TX;
1769 } else {
1770 decoder->state.flags |= INTEL_PT_ASYNC;
1771 }
1772
1773 while (1) {
1774 err = intel_pt_get_next_packet(decoder);
1775 if (err)
1776 return err;
1777
1778 switch (decoder->packet.type) {
1779 case INTEL_PT_TNT:
1780 case INTEL_PT_FUP:
1781 case INTEL_PT_TRACESTOP:
1782 case INTEL_PT_PSB:
1783 case INTEL_PT_TSC:
1784 case INTEL_PT_TMA:
1785 case INTEL_PT_MODE_TSX:
1786 case INTEL_PT_BAD:
1787 case INTEL_PT_PSBEND:
Googler9398cc32022-12-02 17:21:52 +08001788 case INTEL_PT_PTWRITE:
1789 case INTEL_PT_PTWRITE_IP:
1790 case INTEL_PT_EXSTOP:
1791 case INTEL_PT_EXSTOP_IP:
1792 case INTEL_PT_MWAIT:
1793 case INTEL_PT_PWRE:
1794 case INTEL_PT_PWRX:
1795 case INTEL_PT_BBP:
1796 case INTEL_PT_BIP:
1797 case INTEL_PT_BEP:
1798 case INTEL_PT_BEP_IP:
Googleraf606d22022-10-26 21:40:12 -07001799 intel_pt_log("ERROR: Missing TIP after FUP\n");
1800 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1801 decoder->pkt_step = 0;
1802 return -ENOENT;
1803
1804 case INTEL_PT_CBR:
1805 intel_pt_calc_cbr(decoder);
1806 break;
1807
1808 case INTEL_PT_OVF:
1809 return intel_pt_overflow(decoder);
1810
1811 case INTEL_PT_TIP_PGD:
1812 decoder->state.from_ip = decoder->ip;
Googler9398cc32022-12-02 17:21:52 +08001813 if (decoder->packet.count == 0) {
1814 decoder->state.to_ip = 0;
1815 } else {
Googleraf606d22022-10-26 21:40:12 -07001816 intel_pt_set_ip(decoder);
Googler9398cc32022-12-02 17:21:52 +08001817 decoder->state.to_ip = decoder->ip;
Googleraf606d22022-10-26 21:40:12 -07001818 }
1819 decoder->pge = false;
1820 decoder->continuous_period = false;
Googler9398cc32022-12-02 17:21:52 +08001821 decoder->state.type |= INTEL_PT_TRACE_END;
Googleraf606d22022-10-26 21:40:12 -07001822 return 0;
1823
1824 case INTEL_PT_TIP_PGE:
1825 decoder->pge = true;
1826 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1827 decoder->ip);
1828 decoder->state.from_ip = 0;
1829 if (decoder->packet.count == 0) {
1830 decoder->state.to_ip = 0;
1831 } else {
1832 intel_pt_set_ip(decoder);
1833 decoder->state.to_ip = decoder->ip;
1834 }
Googler9398cc32022-12-02 17:21:52 +08001835 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1836 intel_pt_mtc_cyc_cnt_pge(decoder);
Googleraf606d22022-10-26 21:40:12 -07001837 return 0;
1838
1839 case INTEL_PT_TIP:
1840 decoder->state.from_ip = decoder->ip;
1841 if (decoder->packet.count == 0) {
1842 decoder->state.to_ip = 0;
1843 } else {
1844 intel_pt_set_ip(decoder);
1845 decoder->state.to_ip = decoder->ip;
1846 }
1847 return 0;
1848
1849 case INTEL_PT_PIP:
1850 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1851 break;
1852
1853 case INTEL_PT_MTC:
1854 intel_pt_calc_mtc_timestamp(decoder);
1855 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1856 decoder->state.type |= INTEL_PT_INSTRUCTION;
1857 break;
1858
1859 case INTEL_PT_CYC:
1860 intel_pt_calc_cyc_timestamp(decoder);
1861 break;
1862
1863 case INTEL_PT_MODE_EXEC:
1864 decoder->exec_mode = decoder->packet.payload;
1865 break;
1866
1867 case INTEL_PT_VMCS:
1868 case INTEL_PT_MNT:
1869 case INTEL_PT_PAD:
1870 break;
1871
1872 default:
1873 return intel_pt_bug(decoder);
1874 }
1875 }
1876}
1877
1878static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1879{
1880 bool no_tip = false;
1881 int err;
1882
1883 while (1) {
1884 err = intel_pt_get_next_packet(decoder);
1885 if (err)
1886 return err;
1887next:
1888 switch (decoder->packet.type) {
1889 case INTEL_PT_TNT:
1890 if (!decoder->packet.count)
1891 break;
1892 decoder->tnt = decoder->packet;
1893 decoder->pkt_state = INTEL_PT_STATE_TNT;
1894 err = intel_pt_walk_tnt(decoder);
1895 if (err == -EAGAIN)
1896 break;
1897 return err;
1898
1899 case INTEL_PT_TIP_PGD:
1900 if (decoder->packet.count != 0)
1901 intel_pt_set_last_ip(decoder);
1902 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1903 return intel_pt_walk_tip(decoder);
1904
1905 case INTEL_PT_TIP_PGE: {
1906 decoder->pge = true;
Googler9398cc32022-12-02 17:21:52 +08001907 intel_pt_mtc_cyc_cnt_pge(decoder);
Googleraf606d22022-10-26 21:40:12 -07001908 if (decoder->packet.count == 0) {
1909 intel_pt_log_at("Skipping zero TIP.PGE",
1910 decoder->pos);
1911 break;
1912 }
1913 intel_pt_set_ip(decoder);
1914 decoder->state.from_ip = 0;
1915 decoder->state.to_ip = decoder->ip;
Googler9398cc32022-12-02 17:21:52 +08001916 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
Googleraf606d22022-10-26 21:40:12 -07001917 return 0;
1918 }
1919
1920 case INTEL_PT_OVF:
1921 return intel_pt_overflow(decoder);
1922
1923 case INTEL_PT_TIP:
1924 if (decoder->packet.count != 0)
1925 intel_pt_set_last_ip(decoder);
1926 decoder->pkt_state = INTEL_PT_STATE_TIP;
1927 return intel_pt_walk_tip(decoder);
1928
1929 case INTEL_PT_FUP:
1930 if (decoder->packet.count == 0) {
1931 intel_pt_log_at("Skipping zero FUP",
1932 decoder->pos);
1933 no_tip = false;
1934 break;
1935 }
1936 intel_pt_set_last_ip(decoder);
Googler9398cc32022-12-02 17:21:52 +08001937 if (!decoder->branch_enable) {
1938 decoder->ip = decoder->last_ip;
1939 if (intel_pt_fup_event(decoder))
1940 return 0;
1941 no_tip = false;
1942 break;
Googler9726be62022-12-14 05:53:31 +00001943 }
Googler9398cc32022-12-02 17:21:52 +08001944 if (decoder->set_fup_mwait)
1945 no_tip = true;
1946 if (no_tip)
1947 decoder->pkt_state = INTEL_PT_STATE_FUP_NO_TIP;
1948 else
1949 decoder->pkt_state = INTEL_PT_STATE_FUP;
1950 err = intel_pt_walk_fup(decoder);
1951 if (err != -EAGAIN)
1952 return err;
Googleraf606d22022-10-26 21:40:12 -07001953 if (no_tip) {
1954 no_tip = false;
1955 break;
1956 }
1957 return intel_pt_walk_fup_tip(decoder);
1958
1959 case INTEL_PT_TRACESTOP:
1960 decoder->pge = false;
1961 decoder->continuous_period = false;
1962 intel_pt_clear_tx_flags(decoder);
1963 decoder->have_tma = false;
1964 break;
1965
1966 case INTEL_PT_PSB:
1967 decoder->last_ip = 0;
1968 decoder->have_last_ip = true;
1969 intel_pt_clear_stack(&decoder->stack);
1970 err = intel_pt_walk_psbend(decoder);
1971 if (err == -EAGAIN)
1972 goto next;
1973 if (err)
1974 return err;
Googler9398cc32022-12-02 17:21:52 +08001975 /*
1976 * PSB+ CBR will not have changed but cater for the
1977 * possibility of another CBR change that gets caught up
1978 * in the PSB+.
1979 */
1980 if (decoder->cbr != decoder->cbr_seen) {
1981 decoder->state.type = 0;
1982 return 0;
1983 }
Googleraf606d22022-10-26 21:40:12 -07001984 break;
1985
1986 case INTEL_PT_PIP:
1987 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1988 break;
1989
1990 case INTEL_PT_MTC:
1991 intel_pt_calc_mtc_timestamp(decoder);
1992 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1993 break;
1994 /*
1995 * Ensure that there has been an instruction since the
1996 * last MTC.
1997 */
1998 if (!decoder->mtc_insn)
1999 break;
2000 decoder->mtc_insn = false;
2001 /* Ensure that there is a timestamp */
2002 if (!decoder->timestamp)
2003 break;
2004 decoder->state.type = INTEL_PT_INSTRUCTION;
2005 decoder->state.from_ip = decoder->ip;
2006 decoder->state.to_ip = 0;
2007 decoder->mtc_insn = false;
2008 return 0;
2009
2010 case INTEL_PT_TSC:
2011 intel_pt_calc_tsc_timestamp(decoder);
2012 break;
2013
2014 case INTEL_PT_TMA:
2015 intel_pt_calc_tma(decoder);
2016 break;
2017
2018 case INTEL_PT_CYC:
2019 intel_pt_calc_cyc_timestamp(decoder);
2020 break;
2021
2022 case INTEL_PT_CBR:
2023 intel_pt_calc_cbr(decoder);
Googler9398cc32022-12-02 17:21:52 +08002024 if (decoder->cbr != decoder->cbr_seen) {
2025 decoder->state.type = 0;
2026 return 0;
2027 }
Googleraf606d22022-10-26 21:40:12 -07002028 break;
2029
2030 case INTEL_PT_MODE_EXEC:
2031 decoder->exec_mode = decoder->packet.payload;
2032 break;
2033
2034 case INTEL_PT_MODE_TSX:
2035 /* MODE_TSX need not be followed by FUP */
2036 if (!decoder->pge) {
2037 intel_pt_update_in_tx(decoder);
2038 break;
2039 }
2040 err = intel_pt_mode_tsx(decoder, &no_tip);
2041 if (err)
2042 return err;
2043 goto next;
2044
2045 case INTEL_PT_BAD: /* Does not happen */
2046 return intel_pt_bug(decoder);
2047
2048 case INTEL_PT_PSBEND:
2049 case INTEL_PT_VMCS:
2050 case INTEL_PT_MNT:
2051 case INTEL_PT_PAD:
2052 break;
2053
Googler9398cc32022-12-02 17:21:52 +08002054 case INTEL_PT_PTWRITE_IP:
2055 decoder->fup_ptw_payload = decoder->packet.payload;
2056 err = intel_pt_get_next_packet(decoder);
2057 if (err)
2058 return err;
2059 if (decoder->packet.type == INTEL_PT_FUP) {
2060 decoder->set_fup_ptw = true;
2061 no_tip = true;
2062 } else {
2063 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
2064 decoder->pos);
2065 }
2066 goto next;
2067
2068 case INTEL_PT_PTWRITE:
2069 decoder->state.type = INTEL_PT_PTW;
2070 decoder->state.from_ip = decoder->ip;
2071 decoder->state.to_ip = 0;
2072 decoder->state.ptw_payload = decoder->packet.payload;
2073 return 0;
2074
2075 case INTEL_PT_MWAIT:
2076 decoder->fup_mwait_payload = decoder->packet.payload;
2077 decoder->set_fup_mwait = true;
2078 break;
2079
2080 case INTEL_PT_PWRE:
2081 if (decoder->set_fup_mwait) {
2082 decoder->fup_pwre_payload =
2083 decoder->packet.payload;
2084 decoder->set_fup_pwre = true;
2085 break;
2086 }
2087 decoder->state.type = INTEL_PT_PWR_ENTRY;
2088 decoder->state.from_ip = decoder->ip;
2089 decoder->state.to_ip = 0;
2090 decoder->state.pwrx_payload = decoder->packet.payload;
2091 return 0;
2092
2093 case INTEL_PT_EXSTOP_IP:
2094 err = intel_pt_get_next_packet(decoder);
2095 if (err)
2096 return err;
2097 if (decoder->packet.type == INTEL_PT_FUP) {
2098 decoder->set_fup_exstop = true;
2099 no_tip = true;
2100 } else {
2101 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
2102 decoder->pos);
2103 }
2104 goto next;
2105
2106 case INTEL_PT_EXSTOP:
2107 decoder->state.type = INTEL_PT_EX_STOP;
2108 decoder->state.from_ip = decoder->ip;
2109 decoder->state.to_ip = 0;
2110 return 0;
2111
2112 case INTEL_PT_PWRX:
2113 decoder->state.type = INTEL_PT_PWR_EXIT;
2114 decoder->state.from_ip = decoder->ip;
2115 decoder->state.to_ip = 0;
2116 decoder->state.pwrx_payload = decoder->packet.payload;
2117 return 0;
2118
2119 case INTEL_PT_BBP:
2120 intel_pt_bbp(decoder);
2121 break;
2122
2123 case INTEL_PT_BIP:
2124 intel_pt_bip(decoder);
2125 break;
2126
2127 case INTEL_PT_BEP:
2128 decoder->state.type = INTEL_PT_BLK_ITEMS;
2129 decoder->state.from_ip = decoder->ip;
2130 decoder->state.to_ip = 0;
2131 return 0;
2132
2133 case INTEL_PT_BEP_IP:
2134 err = intel_pt_get_next_packet(decoder);
2135 if (err)
2136 return err;
2137 if (decoder->packet.type == INTEL_PT_FUP) {
2138 decoder->set_fup_bep = true;
2139 no_tip = true;
2140 } else {
2141 intel_pt_log_at("ERROR: Missing FUP after BEP",
2142 decoder->pos);
2143 }
2144 goto next;
2145
Googleraf606d22022-10-26 21:40:12 -07002146 default:
2147 return intel_pt_bug(decoder);
2148 }
2149 }
2150}
2151
2152static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
2153{
2154 return decoder->packet.count &&
2155 (decoder->have_last_ip || decoder->packet.count == 3 ||
2156 decoder->packet.count == 6);
2157}
2158
2159/* Walk PSB+ packets to get in sync. */
2160static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
2161{
2162 int err;
2163
Googler9398cc32022-12-02 17:21:52 +08002164 decoder->in_psb = true;
2165
Googleraf606d22022-10-26 21:40:12 -07002166 while (1) {
2167 err = intel_pt_get_next_packet(decoder);
2168 if (err)
Googler9398cc32022-12-02 17:21:52 +08002169 goto out;
Googleraf606d22022-10-26 21:40:12 -07002170
2171 switch (decoder->packet.type) {
2172 case INTEL_PT_TIP_PGD:
2173 decoder->continuous_period = false;
2174 __fallthrough;
2175 case INTEL_PT_TIP_PGE:
2176 case INTEL_PT_TIP:
Googler9398cc32022-12-02 17:21:52 +08002177 case INTEL_PT_PTWRITE:
2178 case INTEL_PT_PTWRITE_IP:
2179 case INTEL_PT_EXSTOP:
2180 case INTEL_PT_EXSTOP_IP:
2181 case INTEL_PT_MWAIT:
2182 case INTEL_PT_PWRE:
2183 case INTEL_PT_PWRX:
2184 case INTEL_PT_BBP:
2185 case INTEL_PT_BIP:
2186 case INTEL_PT_BEP:
2187 case INTEL_PT_BEP_IP:
Googleraf606d22022-10-26 21:40:12 -07002188 intel_pt_log("ERROR: Unexpected packet\n");
Googler9398cc32022-12-02 17:21:52 +08002189 err = -ENOENT;
2190 goto out;
Googleraf606d22022-10-26 21:40:12 -07002191
2192 case INTEL_PT_FUP:
2193 decoder->pge = true;
2194 if (intel_pt_have_ip(decoder)) {
2195 uint64_t current_ip = decoder->ip;
2196
2197 intel_pt_set_ip(decoder);
2198 if (current_ip)
2199 intel_pt_log_to("Setting IP",
2200 decoder->ip);
2201 }
2202 break;
2203
2204 case INTEL_PT_MTC:
2205 intel_pt_calc_mtc_timestamp(decoder);
2206 break;
2207
2208 case INTEL_PT_TSC:
2209 intel_pt_calc_tsc_timestamp(decoder);
2210 break;
2211
2212 case INTEL_PT_TMA:
2213 intel_pt_calc_tma(decoder);
2214 break;
2215
2216 case INTEL_PT_CYC:
2217 intel_pt_calc_cyc_timestamp(decoder);
2218 break;
2219
2220 case INTEL_PT_CBR:
2221 intel_pt_calc_cbr(decoder);
2222 break;
2223
2224 case INTEL_PT_PIP:
2225 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2226 break;
2227
2228 case INTEL_PT_MODE_EXEC:
2229 decoder->exec_mode = decoder->packet.payload;
2230 break;
2231
2232 case INTEL_PT_MODE_TSX:
2233 intel_pt_update_in_tx(decoder);
2234 break;
2235
2236 case INTEL_PT_TRACESTOP:
2237 decoder->pge = false;
2238 decoder->continuous_period = false;
2239 intel_pt_clear_tx_flags(decoder);
2240 __fallthrough;
2241
2242 case INTEL_PT_TNT:
2243 decoder->have_tma = false;
2244 intel_pt_log("ERROR: Unexpected packet\n");
2245 if (decoder->ip)
2246 decoder->pkt_state = INTEL_PT_STATE_ERR4;
2247 else
2248 decoder->pkt_state = INTEL_PT_STATE_ERR3;
Googler9398cc32022-12-02 17:21:52 +08002249 err = -ENOENT;
2250 goto out;
Googleraf606d22022-10-26 21:40:12 -07002251
2252 case INTEL_PT_BAD: /* Does not happen */
Googler9398cc32022-12-02 17:21:52 +08002253 err = intel_pt_bug(decoder);
2254 goto out;
Googleraf606d22022-10-26 21:40:12 -07002255
2256 case INTEL_PT_OVF:
Googler9398cc32022-12-02 17:21:52 +08002257 err = intel_pt_overflow(decoder);
2258 goto out;
Googleraf606d22022-10-26 21:40:12 -07002259
2260 case INTEL_PT_PSBEND:
Googler9398cc32022-12-02 17:21:52 +08002261 err = 0;
2262 goto out;
Googleraf606d22022-10-26 21:40:12 -07002263
2264 case INTEL_PT_PSB:
2265 case INTEL_PT_VMCS:
2266 case INTEL_PT_MNT:
2267 case INTEL_PT_PAD:
2268 default:
2269 break;
2270 }
2271 }
Googler9398cc32022-12-02 17:21:52 +08002272out:
2273 decoder->in_psb = false;
2274
2275 return err;
Googleraf606d22022-10-26 21:40:12 -07002276}
2277
2278static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2279{
2280 int err;
2281
2282 while (1) {
2283 err = intel_pt_get_next_packet(decoder);
2284 if (err)
2285 return err;
2286
2287 switch (decoder->packet.type) {
2288 case INTEL_PT_TIP_PGD:
2289 decoder->continuous_period = false;
Googler9398cc32022-12-02 17:21:52 +08002290 decoder->pge = false;
Googler38bda472022-08-19 10:07:08 -07002291 if (intel_pt_have_ip(decoder))
2292 intel_pt_set_ip(decoder);
Googler9398cc32022-12-02 17:21:52 +08002293 if (!decoder->ip)
2294 break;
2295 decoder->state.type |= INTEL_PT_TRACE_END;
2296 return 0;
2297
2298 case INTEL_PT_TIP_PGE:
2299 decoder->pge = true;
2300 intel_pt_mtc_cyc_cnt_pge(decoder);
2301 if (intel_pt_have_ip(decoder))
2302 intel_pt_set_ip(decoder);
2303 if (!decoder->ip)
2304 break;
2305 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2306 return 0;
2307
2308 case INTEL_PT_TIP:
2309 decoder->pge = true;
2310 if (intel_pt_have_ip(decoder))
2311 intel_pt_set_ip(decoder);
2312 if (!decoder->ip)
2313 break;
2314 return 0;
Googleraf606d22022-10-26 21:40:12 -07002315
2316 case INTEL_PT_FUP:
2317 if (intel_pt_have_ip(decoder))
2318 intel_pt_set_ip(decoder);
2319 if (decoder->ip)
2320 return 0;
2321 break;
2322
2323 case INTEL_PT_MTC:
2324 intel_pt_calc_mtc_timestamp(decoder);
2325 break;
2326
2327 case INTEL_PT_TSC:
2328 intel_pt_calc_tsc_timestamp(decoder);
2329 break;
2330
2331 case INTEL_PT_TMA:
2332 intel_pt_calc_tma(decoder);
2333 break;
2334
2335 case INTEL_PT_CYC:
2336 intel_pt_calc_cyc_timestamp(decoder);
2337 break;
2338
2339 case INTEL_PT_CBR:
2340 intel_pt_calc_cbr(decoder);
2341 break;
2342
2343 case INTEL_PT_PIP:
2344 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2345 break;
2346
2347 case INTEL_PT_MODE_EXEC:
2348 decoder->exec_mode = decoder->packet.payload;
2349 break;
2350
2351 case INTEL_PT_MODE_TSX:
2352 intel_pt_update_in_tx(decoder);
2353 break;
2354
2355 case INTEL_PT_OVF:
2356 return intel_pt_overflow(decoder);
2357
2358 case INTEL_PT_BAD: /* Does not happen */
2359 return intel_pt_bug(decoder);
2360
2361 case INTEL_PT_TRACESTOP:
2362 decoder->pge = false;
2363 decoder->continuous_period = false;
2364 intel_pt_clear_tx_flags(decoder);
2365 decoder->have_tma = false;
2366 break;
2367
2368 case INTEL_PT_PSB:
2369 decoder->last_ip = 0;
2370 decoder->have_last_ip = true;
2371 intel_pt_clear_stack(&decoder->stack);
2372 err = intel_pt_walk_psb(decoder);
2373 if (err)
2374 return err;
2375 if (decoder->ip) {
2376 /* Do not have a sample */
2377 decoder->state.type = 0;
2378 return 0;
2379 }
2380 break;
2381
2382 case INTEL_PT_TNT:
2383 case INTEL_PT_PSBEND:
2384 case INTEL_PT_VMCS:
2385 case INTEL_PT_MNT:
2386 case INTEL_PT_PAD:
Googler9398cc32022-12-02 17:21:52 +08002387 case INTEL_PT_PTWRITE:
2388 case INTEL_PT_PTWRITE_IP:
2389 case INTEL_PT_EXSTOP:
2390 case INTEL_PT_EXSTOP_IP:
2391 case INTEL_PT_MWAIT:
2392 case INTEL_PT_PWRE:
2393 case INTEL_PT_PWRX:
2394 case INTEL_PT_BBP:
2395 case INTEL_PT_BIP:
2396 case INTEL_PT_BEP:
2397 case INTEL_PT_BEP_IP:
Googleraf606d22022-10-26 21:40:12 -07002398 default:
2399 break;
2400 }
2401 }
2402}
2403
2404static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2405{
2406 int err;
2407
2408 decoder->set_fup_tx_flags = false;
Googler9398cc32022-12-02 17:21:52 +08002409 decoder->set_fup_ptw = false;
2410 decoder->set_fup_mwait = false;
2411 decoder->set_fup_pwre = false;
2412 decoder->set_fup_exstop = false;
2413 decoder->set_fup_bep = false;
2414
2415 if (!decoder->branch_enable) {
2416 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2417 decoder->overflow = false;
2418 decoder->state.type = 0; /* Do not have a sample */
2419 return 0;
2420 }
Googleraf606d22022-10-26 21:40:12 -07002421
2422 intel_pt_log("Scanning for full IP\n");
2423 err = intel_pt_walk_to_ip(decoder);
2424 if (err)
2425 return err;
2426
2427 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2428 decoder->overflow = false;
2429
2430 decoder->state.from_ip = 0;
2431 decoder->state.to_ip = decoder->ip;
2432 intel_pt_log_to("Setting IP", decoder->ip);
2433
2434 return 0;
2435}
2436
2437static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2438{
2439 const unsigned char *end = decoder->buf + decoder->len;
2440 size_t i;
2441
2442 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2443 if (i > decoder->len)
2444 continue;
2445 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2446 return i;
2447 }
2448 return 0;
2449}
2450
2451static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2452{
2453 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2454 const char *psb = INTEL_PT_PSB_STR;
2455
2456 if (rest_psb > decoder->len ||
2457 memcmp(decoder->buf, psb + part_psb, rest_psb))
2458 return 0;
2459
2460 return rest_psb;
2461}
2462
2463static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2464 int part_psb)
2465{
2466 int rest_psb, ret;
2467
2468 decoder->pos += decoder->len;
2469 decoder->len = 0;
2470
Googler9398cc32022-12-02 17:21:52 +08002471 ret = intel_pt_get_next_data(decoder, false);
Googleraf606d22022-10-26 21:40:12 -07002472 if (ret)
2473 return ret;
2474
2475 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2476 if (!rest_psb)
2477 return 0;
2478
2479 decoder->pos -= part_psb;
2480 decoder->next_buf = decoder->buf + rest_psb;
2481 decoder->next_len = decoder->len - rest_psb;
2482 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2483 decoder->buf = decoder->temp_buf;
2484 decoder->len = INTEL_PT_PSB_LEN;
2485
2486 return 0;
2487}
2488
2489static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2490{
2491 unsigned char *next;
2492 int ret;
2493
2494 intel_pt_log("Scanning for PSB\n");
2495 while (1) {
2496 if (!decoder->len) {
Googler9398cc32022-12-02 17:21:52 +08002497 ret = intel_pt_get_next_data(decoder, false);
Googleraf606d22022-10-26 21:40:12 -07002498 if (ret)
2499 return ret;
2500 }
2501
2502 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2503 INTEL_PT_PSB_LEN);
2504 if (!next) {
2505 int part_psb;
2506
2507 part_psb = intel_pt_part_psb(decoder);
2508 if (part_psb) {
2509 ret = intel_pt_get_split_psb(decoder, part_psb);
2510 if (ret)
2511 return ret;
2512 } else {
2513 decoder->pos += decoder->len;
2514 decoder->len = 0;
2515 }
2516 continue;
2517 }
2518
2519 decoder->pkt_step = next - decoder->buf;
2520 return intel_pt_get_next_packet(decoder);
2521 }
2522}
2523
2524static int intel_pt_sync(struct intel_pt_decoder *decoder)
2525{
2526 int err;
2527
2528 decoder->pge = false;
2529 decoder->continuous_period = false;
2530 decoder->have_last_ip = false;
2531 decoder->last_ip = 0;
2532 decoder->ip = 0;
2533 intel_pt_clear_stack(&decoder->stack);
2534
2535 err = intel_pt_scan_for_psb(decoder);
2536 if (err)
2537 return err;
2538
2539 decoder->have_last_ip = true;
2540 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2541
2542 err = intel_pt_walk_psb(decoder);
2543 if (err)
2544 return err;
2545
2546 if (decoder->ip) {
2547 decoder->state.type = 0; /* Do not have a sample */
2548 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2549 } else {
2550 return intel_pt_sync_ip(decoder);
2551 }
2552
2553 return 0;
2554}
2555
2556static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2557{
2558 uint64_t est = decoder->sample_insn_cnt << 1;
2559
2560 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2561 goto out;
2562
2563 est *= decoder->max_non_turbo_ratio;
2564 est /= decoder->cbr;
2565out:
2566 return decoder->sample_timestamp + est;
2567}
2568
2569const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2570{
2571 int err;
2572
2573 do {
2574 decoder->state.type = INTEL_PT_BRANCH;
2575 decoder->state.flags = 0;
2576
2577 switch (decoder->pkt_state) {
2578 case INTEL_PT_STATE_NO_PSB:
2579 err = intel_pt_sync(decoder);
2580 break;
2581 case INTEL_PT_STATE_NO_IP:
2582 decoder->have_last_ip = false;
2583 decoder->last_ip = 0;
2584 decoder->ip = 0;
Googler9398cc32022-12-02 17:21:52 +08002585 __fallthrough;
Googleraf606d22022-10-26 21:40:12 -07002586 case INTEL_PT_STATE_ERR_RESYNC:
2587 err = intel_pt_sync_ip(decoder);
2588 break;
2589 case INTEL_PT_STATE_IN_SYNC:
2590 err = intel_pt_walk_trace(decoder);
2591 break;
2592 case INTEL_PT_STATE_TNT:
2593 case INTEL_PT_STATE_TNT_CONT:
2594 err = intel_pt_walk_tnt(decoder);
2595 if (err == -EAGAIN)
2596 err = intel_pt_walk_trace(decoder);
2597 break;
2598 case INTEL_PT_STATE_TIP:
2599 case INTEL_PT_STATE_TIP_PGD:
2600 err = intel_pt_walk_tip(decoder);
2601 break;
2602 case INTEL_PT_STATE_FUP:
2603 err = intel_pt_walk_fup(decoder);
2604 if (err == -EAGAIN)
2605 err = intel_pt_walk_fup_tip(decoder);
2606 break;
2607 case INTEL_PT_STATE_FUP_NO_TIP:
2608 err = intel_pt_walk_fup(decoder);
2609 if (err == -EAGAIN)
2610 err = intel_pt_walk_trace(decoder);
2611 break;
2612 default:
2613 err = intel_pt_bug(decoder);
2614 break;
2615 }
2616 } while (err == -ENOLINK);
2617
2618 if (err) {
2619 decoder->state.err = intel_pt_ext_err(err);
2620 decoder->state.from_ip = decoder->ip;
Googler9398cc32022-12-02 17:21:52 +08002621 intel_pt_update_sample_time(decoder);
2622 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
Googleraf606d22022-10-26 21:40:12 -07002623 } else {
2624 decoder->state.err = 0;
Googler9398cc32022-12-02 17:21:52 +08002625 if (decoder->cbr != decoder->cbr_seen) {
2626 decoder->cbr_seen = decoder->cbr;
2627 if (!decoder->state.type) {
2628 decoder->state.from_ip = decoder->ip;
2629 decoder->state.to_ip = 0;
2630 }
2631 decoder->state.type |= INTEL_PT_CBR_CHG;
2632 decoder->state.cbr_payload = decoder->cbr_payload;
2633 decoder->state.cbr = decoder->cbr;
Googler38bda472022-08-19 10:07:08 -07002634 }
Googler9398cc32022-12-02 17:21:52 +08002635 if (intel_pt_sample_time(decoder->pkt_state)) {
2636 intel_pt_update_sample_time(decoder);
Googlerb48fa912023-03-17 12:40:29 +05302637 if (decoder->sample_cyc)
Googler9398cc32022-12-02 17:21:52 +08002638 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
Googler9398cc32022-12-02 17:21:52 +08002639 }
Googleraf606d22022-10-26 21:40:12 -07002640 }
2641
2642 decoder->state.timestamp = decoder->sample_timestamp;
2643 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2644 decoder->state.cr3 = decoder->cr3;
2645 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
Googler9398cc32022-12-02 17:21:52 +08002646 decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
Googleraf606d22022-10-26 21:40:12 -07002647
2648 return &decoder->state;
2649}
2650
2651/**
2652 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2653 * @buf: pointer to buffer pointer
2654 * @len: size of buffer
2655 *
2656 * Updates the buffer pointer to point to the start of the next PSB packet if
2657 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2658 * @len is adjusted accordingly.
2659 *
2660 * Return: %true if a PSB packet is found, %false otherwise.
2661 */
2662static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2663{
2664 unsigned char *next;
2665
2666 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2667 if (next) {
2668 *len -= next - *buf;
2669 *buf = next;
2670 return true;
2671 }
2672 return false;
2673}
2674
2675/**
2676 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2677 * packet.
2678 * @buf: pointer to buffer pointer
2679 * @len: size of buffer
2680 *
2681 * Updates the buffer pointer to point to the start of the following PSB packet
2682 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2683 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2684 *
2685 * Return: %true if a PSB packet is found, %false otherwise.
2686 */
2687static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2688{
2689 unsigned char *next;
2690
2691 if (!*len)
2692 return false;
2693
2694 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2695 if (next) {
2696 *len -= next - *buf;
2697 *buf = next;
2698 return true;
2699 }
2700 return false;
2701}
2702
2703/**
2704 * intel_pt_last_psb - find the last PSB packet in a buffer.
2705 * @buf: buffer
2706 * @len: size of buffer
2707 *
2708 * This function finds the last PSB in a buffer.
2709 *
2710 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2711 */
2712static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2713{
2714 const char *n = INTEL_PT_PSB_STR;
2715 unsigned char *p;
2716 size_t k;
2717
2718 if (len < INTEL_PT_PSB_LEN)
2719 return NULL;
2720
2721 k = len - INTEL_PT_PSB_LEN + 1;
2722 while (1) {
2723 p = memrchr(buf, n[0], k);
2724 if (!p)
2725 return NULL;
2726 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2727 return p;
2728 k = p - buf;
2729 if (!k)
2730 return NULL;
2731 }
2732}
2733
2734/**
2735 * intel_pt_next_tsc - find and return next TSC.
2736 * @buf: buffer
2737 * @len: size of buffer
2738 * @tsc: TSC value returned
2739 * @rem: returns remaining size when TSC is found
2740 *
2741 * Find a TSC packet in @buf and return the TSC value. This function assumes
2742 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2743 * PSBEND packet is found.
2744 *
2745 * Return: %true if TSC is found, false otherwise.
2746 */
2747static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2748 size_t *rem)
2749{
Googler9398cc32022-12-02 17:21:52 +08002750 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
Googleraf606d22022-10-26 21:40:12 -07002751 struct intel_pt_pkt packet;
2752 int ret;
2753
2754 while (len) {
Googler9398cc32022-12-02 17:21:52 +08002755 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
Googleraf606d22022-10-26 21:40:12 -07002756 if (ret <= 0)
2757 return false;
2758 if (packet.type == INTEL_PT_TSC) {
2759 *tsc = packet.payload;
2760 *rem = len;
2761 return true;
2762 }
2763 if (packet.type == INTEL_PT_PSBEND)
2764 return false;
2765 buf += ret;
2766 len -= ret;
2767 }
2768 return false;
2769}
2770
2771/**
2772 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2773 * @tsc1: first TSC to compare
2774 * @tsc2: second TSC to compare
2775 *
2776 * This function compares 7-byte TSC values allowing for the possibility that
2777 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2778 * around so for that purpose this function assumes the absolute difference is
2779 * less than half the maximum difference.
2780 *
2781 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2782 * after @tsc2.
2783 */
2784static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2785{
2786 const uint64_t halfway = (1ULL << 55);
2787
2788 if (tsc1 == tsc2)
2789 return 0;
2790
2791 if (tsc1 < tsc2) {
2792 if (tsc2 - tsc1 < halfway)
2793 return -1;
2794 else
2795 return 1;
2796 } else {
2797 if (tsc1 - tsc2 < halfway)
2798 return 1;
2799 else
2800 return -1;
2801 }
2802}
2803
2804#define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2805
2806/**
2807 * adj_for_padding - adjust overlap to account for padding.
2808 * @buf_b: second buffer
2809 * @buf_a: first buffer
2810 * @len_a: size of first buffer
2811 *
2812 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2813 * accordingly.
2814 *
2815 * Return: A pointer into @buf_b from where non-overlapped data starts
2816 */
2817static unsigned char *adj_for_padding(unsigned char *buf_b,
2818 unsigned char *buf_a, size_t len_a)
2819{
2820 unsigned char *p = buf_b - MAX_PADDING;
2821 unsigned char *q = buf_a + len_a - MAX_PADDING;
2822 int i;
2823
2824 for (i = MAX_PADDING; i; i--, p++, q++) {
2825 if (*p != *q)
2826 break;
2827 }
2828
2829 return p;
2830}
2831
2832/**
2833 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2834 * using TSC.
2835 * @buf_a: first buffer
2836 * @len_a: size of first buffer
2837 * @buf_b: second buffer
2838 * @len_b: size of second buffer
2839 * @consecutive: returns true if there is data in buf_b that is consecutive
2840 * to buf_a
2841 *
2842 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2843 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2844 * walk forward in @buf_b until a later TSC is found. A precondition is that
2845 * @buf_a and @buf_b are positioned at a PSB.
2846 *
2847 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2848 * @buf_b + @len_b if there is no non-overlapped data.
2849 */
2850static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2851 size_t len_a,
2852 unsigned char *buf_b,
2853 size_t len_b, bool *consecutive)
2854{
2855 uint64_t tsc_a, tsc_b;
2856 unsigned char *p;
2857 size_t len, rem_a, rem_b;
2858
2859 p = intel_pt_last_psb(buf_a, len_a);
2860 if (!p)
2861 return buf_b; /* No PSB in buf_a => no overlap */
2862
2863 len = len_a - (p - buf_a);
2864 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2865 /* The last PSB+ in buf_a is incomplete, so go back one more */
2866 len_a -= len;
2867 p = intel_pt_last_psb(buf_a, len_a);
2868 if (!p)
2869 return buf_b; /* No full PSB+ => assume no overlap */
2870 len = len_a - (p - buf_a);
2871 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2872 return buf_b; /* No TSC in buf_a => assume no overlap */
2873 }
2874
2875 while (1) {
2876 /* Ignore PSB+ with no TSC */
2877 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2878 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2879
2880 /* Same TSC, so buffers are consecutive */
2881 if (!cmp && rem_b >= rem_a) {
2882 unsigned char *start;
2883
2884 *consecutive = true;
2885 start = buf_b + len_b - (rem_b - rem_a);
2886 return adj_for_padding(start, buf_a, len_a);
2887 }
2888 if (cmp < 0)
2889 return buf_b; /* tsc_a < tsc_b => no overlap */
2890 }
2891
2892 if (!intel_pt_step_psb(&buf_b, &len_b))
2893 return buf_b + len_b; /* No PSB in buf_b => no data */
2894 }
2895}
2896
2897/**
2898 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2899 * @buf_a: first buffer
2900 * @len_a: size of first buffer
2901 * @buf_b: second buffer
2902 * @len_b: size of second buffer
2903 * @have_tsc: can use TSC packets to detect overlap
2904 * @consecutive: returns true if there is data in buf_b that is consecutive
2905 * to buf_a
2906 *
2907 * When trace samples or snapshots are recorded there is the possibility that
2908 * the data overlaps. Note that, for the purposes of decoding, data is only
2909 * useful if it begins with a PSB packet.
2910 *
2911 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2912 * @buf_b + @len_b if there is no non-overlapped data.
2913 */
2914unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2915 unsigned char *buf_b, size_t len_b,
2916 bool have_tsc, bool *consecutive)
2917{
2918 unsigned char *found;
2919
2920 /* Buffer 'b' must start at PSB so throw away everything before that */
2921 if (!intel_pt_next_psb(&buf_b, &len_b))
2922 return buf_b + len_b; /* No PSB */
2923
2924 if (!intel_pt_next_psb(&buf_a, &len_a))
2925 return buf_b; /* No overlap */
2926
2927 if (have_tsc) {
2928 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2929 consecutive);
2930 if (found)
2931 return found;
2932 }
2933
2934 /*
2935 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2936 * we can ignore the first part of buffer 'a'.
2937 */
2938 while (len_b < len_a) {
2939 if (!intel_pt_step_psb(&buf_a, &len_a))
2940 return buf_b; /* No overlap */
2941 }
2942
2943 /* Now len_b >= len_a */
2944 while (1) {
2945 /* Potential overlap so check the bytes */
2946 found = memmem(buf_a, len_a, buf_b, len_a);
2947 if (found) {
2948 *consecutive = true;
2949 return adj_for_padding(buf_b + len_a, buf_a, len_a);
2950 }
2951
2952 /* Try again at next PSB in buffer 'a' */
2953 if (!intel_pt_step_psb(&buf_a, &len_a))
2954 return buf_b; /* No overlap */
2955 }
2956}
Googler9398cc32022-12-02 17:21:52 +08002957
2958/**
2959 * struct fast_forward_data - data used by intel_pt_ff_cb().
2960 * @timestamp: timestamp to fast forward towards
2961 * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
2962 * the fast forward timestamp.
2963 */
2964struct fast_forward_data {
2965 uint64_t timestamp;
2966 uint64_t buf_timestamp;
2967};
2968
2969/**
2970 * intel_pt_ff_cb - fast forward lookahead callback.
2971 * @buffer: Intel PT trace buffer
2972 * @data: opaque pointer to fast forward data (struct fast_forward_data)
2973 *
2974 * Determine if @buffer trace is past the fast forward timestamp.
2975 *
2976 * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
2977 * timestamp, and 0 otherwise.
2978 */
2979static int intel_pt_ff_cb(struct intel_pt_buffer *buffer, void *data)
2980{
2981 struct fast_forward_data *d = data;
2982 unsigned char *buf;
2983 uint64_t tsc;
2984 size_t rem;
2985 size_t len;
2986
2987 buf = (unsigned char *)buffer->buf;
2988 len = buffer->len;
2989
2990 if (!intel_pt_next_psb(&buf, &len) ||
2991 !intel_pt_next_tsc(buf, len, &tsc, &rem))
2992 return 0;
2993
2994 tsc = intel_pt_8b_tsc(tsc, buffer->ref_timestamp);
2995
2996 intel_pt_log("Buffer 1st timestamp " x64_fmt " ref timestamp " x64_fmt "\n",
2997 tsc, buffer->ref_timestamp);
2998
2999 /*
3000 * If the buffer contains a timestamp earlier that the fast forward
3001 * timestamp, then record it, else stop.
3002 */
3003 if (tsc < d->timestamp)
3004 d->buf_timestamp = buffer->ref_timestamp;
3005 else
3006 return 1;
3007
3008 return 0;
3009}
3010
3011/**
3012 * intel_pt_fast_forward - reposition decoder forwards.
3013 * @decoder: Intel PT decoder
3014 * @timestamp: timestamp to fast forward towards
3015 *
3016 * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
3017 *
3018 * Return: 0 on success or negative error code on failure.
3019 */
3020int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp)
3021{
3022 struct fast_forward_data d = { .timestamp = timestamp };
3023 unsigned char *buf;
3024 size_t len;
3025 int err;
3026
3027 intel_pt_log("Fast forward towards timestamp " x64_fmt "\n", timestamp);
3028
3029 /* Find buffer timestamp of buffer to fast forward to */
3030 err = decoder->lookahead(decoder->data, intel_pt_ff_cb, &d);
3031 if (err < 0)
3032 return err;
3033
3034 /* Walk to buffer with same buffer timestamp */
3035 if (d.buf_timestamp) {
3036 do {
3037 decoder->pos += decoder->len;
3038 decoder->len = 0;
3039 err = intel_pt_get_next_data(decoder, true);
3040 /* -ENOLINK means non-consecutive trace */
3041 if (err && err != -ENOLINK)
3042 return err;
3043 } while (decoder->buf_timestamp != d.buf_timestamp);
3044 }
3045
3046 if (!decoder->buf)
3047 return 0;
3048
3049 buf = (unsigned char *)decoder->buf;
3050 len = decoder->len;
3051
3052 if (!intel_pt_next_psb(&buf, &len))
3053 return 0;
3054
3055 /*
3056 * Walk PSBs while the PSB timestamp is less than the fast forward
3057 * timestamp.
3058 */
3059 do {
3060 uint64_t tsc;
3061 size_t rem;
3062
3063 if (!intel_pt_next_tsc(buf, len, &tsc, &rem))
3064 break;
3065 tsc = intel_pt_8b_tsc(tsc, decoder->buf_timestamp);
3066 /*
3067 * A TSC packet can slip past MTC packets but, after fast
3068 * forward, decoding starts at the TSC timestamp. That means
3069 * the timestamps may not be exactly the same as the timestamps
3070 * that would have been decoded without fast forward.
3071 */
3072 if (tsc < timestamp) {
3073 intel_pt_log("Fast forward to next PSB timestamp " x64_fmt "\n", tsc);
3074 decoder->pos += decoder->len - len;
3075 decoder->buf = buf;
3076 decoder->len = len;
3077 intel_pt_reposition(decoder);
3078 } else {
3079 break;
3080 }
3081 } while (intel_pt_step_psb(&buf, &len));
3082
3083 return 0;
3084}