xref: /linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
17 #include <linux/string.h>
18 #include <linux/zalloc.h>
19 
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 BITULL(x) (1ULL << (x))
28 
29 /* IA32_RTIT_CTL MSR bits */
30 #define INTEL_PT_CYC_ENABLE		BITULL(1)
31 #define INTEL_PT_CYC_THRESHOLD		(BITULL(22) | BITULL(21) | BITULL(20) | BITULL(19))
32 #define INTEL_PT_CYC_THRESHOLD_SHIFT	19
33 
34 #define INTEL_PT_BLK_SIZE 1024
35 
36 #define BIT63 (((uint64_t)1 << 63))
37 
38 #define SEVEN_BYTES 0xffffffffffffffULL
39 
40 #define NO_VMCS 0xffffffffffULL
41 
42 #define INTEL_PT_RETURN 1
43 
44 /*
45  * Default maximum number of loops with no packets consumed i.e. stuck in a
46  * loop.
47  */
48 #define INTEL_PT_MAX_LOOPS 100000
49 
50 struct intel_pt_blk {
51 	struct intel_pt_blk *prev;
52 	uint64_t ip[INTEL_PT_BLK_SIZE];
53 };
54 
55 struct intel_pt_stack {
56 	struct intel_pt_blk *blk;
57 	struct intel_pt_blk *spare;
58 	int pos;
59 };
60 
61 enum intel_pt_p_once {
62 	INTEL_PT_PRT_ONCE_UNK_VMCS,
63 	INTEL_PT_PRT_ONCE_ERANGE,
64 };
65 
66 enum intel_pt_pkt_state {
67 	INTEL_PT_STATE_NO_PSB,
68 	INTEL_PT_STATE_NO_IP,
69 	INTEL_PT_STATE_ERR_RESYNC,
70 	INTEL_PT_STATE_IN_SYNC,
71 	INTEL_PT_STATE_TNT_CONT,
72 	INTEL_PT_STATE_TNT,
73 	INTEL_PT_STATE_TIP,
74 	INTEL_PT_STATE_TIP_PGD,
75 	INTEL_PT_STATE_FUP,
76 	INTEL_PT_STATE_FUP_NO_TIP,
77 	INTEL_PT_STATE_FUP_IN_PSB,
78 	INTEL_PT_STATE_RESAMPLE,
79 	INTEL_PT_STATE_VM_TIME_CORRELATION,
80 };
81 
82 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
83 {
84 	switch (pkt_state) {
85 	case INTEL_PT_STATE_NO_PSB:
86 	case INTEL_PT_STATE_NO_IP:
87 	case INTEL_PT_STATE_ERR_RESYNC:
88 	case INTEL_PT_STATE_IN_SYNC:
89 	case INTEL_PT_STATE_TNT_CONT:
90 	case INTEL_PT_STATE_RESAMPLE:
91 	case INTEL_PT_STATE_VM_TIME_CORRELATION:
92 		return true;
93 	case INTEL_PT_STATE_TNT:
94 	case INTEL_PT_STATE_TIP:
95 	case INTEL_PT_STATE_TIP_PGD:
96 	case INTEL_PT_STATE_FUP:
97 	case INTEL_PT_STATE_FUP_NO_TIP:
98 	case INTEL_PT_STATE_FUP_IN_PSB:
99 		return false;
100 	default:
101 		return true;
102 	};
103 }
104 
105 #ifdef INTEL_PT_STRICT
106 #define INTEL_PT_STATE_ERR1	INTEL_PT_STATE_NO_PSB
107 #define INTEL_PT_STATE_ERR2	INTEL_PT_STATE_NO_PSB
108 #define INTEL_PT_STATE_ERR3	INTEL_PT_STATE_NO_PSB
109 #define INTEL_PT_STATE_ERR4	INTEL_PT_STATE_NO_PSB
110 #else
111 #define INTEL_PT_STATE_ERR1	(decoder->pkt_state)
112 #define INTEL_PT_STATE_ERR2	INTEL_PT_STATE_NO_IP
113 #define INTEL_PT_STATE_ERR3	INTEL_PT_STATE_ERR_RESYNC
114 #define INTEL_PT_STATE_ERR4	INTEL_PT_STATE_IN_SYNC
115 #endif
116 
117 struct intel_pt_decoder {
118 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
119 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
120 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
121 			 uint64_t max_insn_cnt, void *data);
122 	bool (*pgd_ip)(uint64_t ip, void *data);
123 	int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
124 	struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
125 	void *data;
126 	struct intel_pt_state state;
127 	const unsigned char *buf;
128 	size_t len;
129 	bool return_compression;
130 	bool branch_enable;
131 	bool mtc_insn;
132 	bool pge;
133 	bool have_tma;
134 	bool have_cyc;
135 	bool fixup_last_mtc;
136 	bool have_last_ip;
137 	bool in_psb;
138 	bool hop;
139 	bool leap;
140 	bool vm_time_correlation;
141 	bool vm_tm_corr_dry_run;
142 	bool vm_tm_corr_reliable;
143 	bool vm_tm_corr_same_buf;
144 	bool vm_tm_corr_continuous;
145 	bool nr;
146 	bool next_nr;
147 	bool iflag;
148 	bool next_iflag;
149 	enum intel_pt_param_flags flags;
150 	uint64_t pos;
151 	uint64_t last_ip;
152 	uint64_t ip;
153 	uint64_t pip_payload;
154 	uint64_t timestamp;
155 	uint64_t tsc_timestamp;
156 	uint64_t ref_timestamp;
157 	uint64_t buf_timestamp;
158 	uint64_t sample_timestamp;
159 	uint64_t ret_addr;
160 	uint64_t ctc_timestamp;
161 	uint64_t ctc_delta;
162 	uint64_t cycle_cnt;
163 	uint64_t cyc_ref_timestamp;
164 	uint64_t first_timestamp;
165 	uint64_t last_reliable_timestamp;
166 	uint64_t vmcs;
167 	uint64_t print_once;
168 	uint64_t last_ctc;
169 	uint32_t last_mtc;
170 	uint32_t tsc_ctc_ratio_n;
171 	uint32_t tsc_ctc_ratio_d;
172 	uint32_t tsc_ctc_mult;
173 	uint32_t tsc_slip;
174 	uint32_t ctc_rem_mask;
175 	int mtc_shift;
176 	struct intel_pt_stack stack;
177 	enum intel_pt_pkt_state pkt_state;
178 	enum intel_pt_pkt_ctx pkt_ctx;
179 	enum intel_pt_pkt_ctx prev_pkt_ctx;
180 	enum intel_pt_blk_type blk_type;
181 	int blk_type_pos;
182 	struct intel_pt_pkt packet;
183 	struct intel_pt_pkt tnt;
184 	int pkt_step;
185 	int pkt_len;
186 	int last_packet_type;
187 	unsigned int cbr;
188 	unsigned int cbr_seen;
189 	unsigned int max_non_turbo_ratio;
190 	double max_non_turbo_ratio_fp;
191 	double cbr_cyc_to_tsc;
192 	double calc_cyc_to_tsc;
193 	bool have_calc_cyc_to_tsc;
194 	int exec_mode;
195 	unsigned int insn_bytes;
196 	uint64_t period;
197 	enum intel_pt_period_type period_type;
198 	uint64_t tot_insn_cnt;
199 	uint64_t period_insn_cnt;
200 	uint64_t period_mask;
201 	uint64_t period_ticks;
202 	uint64_t last_masked_timestamp;
203 	uint64_t tot_cyc_cnt;
204 	uint64_t sample_tot_cyc_cnt;
205 	uint64_t base_cyc_cnt;
206 	uint64_t cyc_cnt_timestamp;
207 	uint64_t ctl;
208 	uint64_t cyc_threshold;
209 	double tsc_to_cyc;
210 	bool continuous_period;
211 	bool overflow;
212 	bool set_fup_tx_flags;
213 	bool set_fup_ptw;
214 	bool set_fup_mwait;
215 	bool set_fup_pwre;
216 	bool set_fup_exstop;
217 	bool set_fup_bep;
218 	bool set_fup_cfe_ip;
219 	bool set_fup_cfe;
220 	bool set_fup_mode_exec;
221 	bool sample_cyc;
222 	unsigned int fup_tx_flags;
223 	unsigned int tx_flags;
224 	uint64_t fup_ptw_payload;
225 	uint64_t fup_mwait_payload;
226 	uint64_t fup_pwre_payload;
227 	uint64_t cbr_payload;
228 	uint64_t timestamp_insn_cnt;
229 	uint64_t sample_insn_cnt;
230 	uint64_t stuck_ip;
231 	struct intel_pt_pkt fup_cfe_pkt;
232 	int max_loops;
233 	int no_progress;
234 	int stuck_ip_prd;
235 	int stuck_ip_cnt;
236 	uint64_t psb_ip;
237 	const unsigned char *next_buf;
238 	size_t next_len;
239 	unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
240 	int evd_cnt;
241 	struct intel_pt_evd evd[INTEL_PT_MAX_EVDS];
242 };
243 
244 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
245 {
246 	int i;
247 
248 	for (i = 0; x != 1; i++)
249 		x >>= 1;
250 
251 	return x << i;
252 }
253 
254 __printf(1, 2)
255 static void p_log(const char *fmt, ...)
256 {
257 	char buf[512];
258 	va_list args;
259 
260 	va_start(args, fmt);
261 	vsnprintf(buf, sizeof(buf), fmt, args);
262 	va_end(args);
263 
264 	fprintf(stderr, "%s\n", buf);
265 	intel_pt_log("%s\n", buf);
266 }
267 
268 static bool intel_pt_print_once(struct intel_pt_decoder *decoder,
269 				enum intel_pt_p_once id)
270 {
271 	uint64_t bit = 1ULL << id;
272 
273 	if (decoder->print_once & bit)
274 		return false;
275 	decoder->print_once |= bit;
276 	return true;
277 }
278 
279 static uint64_t intel_pt_cyc_threshold(uint64_t ctl)
280 {
281 	if (!(ctl & INTEL_PT_CYC_ENABLE))
282 		return 0;
283 
284 	return (ctl & INTEL_PT_CYC_THRESHOLD) >> INTEL_PT_CYC_THRESHOLD_SHIFT;
285 }
286 
287 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
288 {
289 	if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
290 		uint64_t period;
291 
292 		period = intel_pt_lower_power_of_2(decoder->period);
293 		decoder->period_mask  = ~(period - 1);
294 		decoder->period_ticks = period;
295 	}
296 }
297 
298 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
299 {
300 	if (!d)
301 		return 0;
302 	return (t / d) * n + ((t % d) * n) / d;
303 }
304 
305 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
306 {
307 	struct intel_pt_decoder *decoder;
308 
309 	if (!params->get_trace || !params->walk_insn)
310 		return NULL;
311 
312 	decoder = zalloc(sizeof(struct intel_pt_decoder));
313 	if (!decoder)
314 		return NULL;
315 
316 	decoder->get_trace          = params->get_trace;
317 	decoder->walk_insn          = params->walk_insn;
318 	decoder->pgd_ip             = params->pgd_ip;
319 	decoder->lookahead          = params->lookahead;
320 	decoder->findnew_vmcs_info  = params->findnew_vmcs_info;
321 	decoder->data               = params->data;
322 	decoder->return_compression = params->return_compression;
323 	decoder->branch_enable      = params->branch_enable;
324 	decoder->hop                = params->quick >= 1;
325 	decoder->leap               = params->quick >= 2;
326 	decoder->vm_time_correlation = params->vm_time_correlation;
327 	decoder->vm_tm_corr_dry_run = params->vm_tm_corr_dry_run;
328 	decoder->first_timestamp    = params->first_timestamp;
329 	decoder->last_reliable_timestamp = params->first_timestamp;
330 	decoder->max_loops          = params->max_loops ? params->max_loops : INTEL_PT_MAX_LOOPS;
331 
332 	decoder->flags              = params->flags;
333 
334 	decoder->ctl                = params->ctl;
335 	decoder->period             = params->period;
336 	decoder->period_type        = params->period_type;
337 
338 	decoder->max_non_turbo_ratio    = params->max_non_turbo_ratio;
339 	decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
340 
341 	decoder->cyc_threshold = intel_pt_cyc_threshold(decoder->ctl);
342 
343 	intel_pt_setup_period(decoder);
344 
345 	decoder->mtc_shift = params->mtc_period;
346 	decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
347 
348 	decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
349 	decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
350 
351 	if (!decoder->tsc_ctc_ratio_n)
352 		decoder->tsc_ctc_ratio_d = 0;
353 
354 	if (decoder->tsc_ctc_ratio_d) {
355 		if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
356 			decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
357 						decoder->tsc_ctc_ratio_d;
358 	}
359 
360 	/*
361 	 * A TSC packet can slip past MTC packets so that the timestamp appears
362 	 * to go backwards. One estimate is that can be up to about 40 CPU
363 	 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
364 	 * slippage an order of magnitude more to be on the safe side.
365 	 */
366 	decoder->tsc_slip = 0x10000;
367 
368 	intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
369 	intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
370 	intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
371 	intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
372 	intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
373 
374 	if (decoder->hop)
375 		intel_pt_log("Hop mode: decoding FUP and TIPs, but not TNT\n");
376 
377 	return decoder;
378 }
379 
380 void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
381 				  uint64_t first_timestamp)
382 {
383 	decoder->first_timestamp = first_timestamp;
384 }
385 
386 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
387 {
388 	struct intel_pt_blk *blk = stack->blk;
389 
390 	stack->blk = blk->prev;
391 	if (!stack->spare)
392 		stack->spare = blk;
393 	else
394 		free(blk);
395 }
396 
397 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
398 {
399 	if (!stack->pos) {
400 		if (!stack->blk)
401 			return 0;
402 		intel_pt_pop_blk(stack);
403 		if (!stack->blk)
404 			return 0;
405 		stack->pos = INTEL_PT_BLK_SIZE;
406 	}
407 	return stack->blk->ip[--stack->pos];
408 }
409 
410 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
411 {
412 	struct intel_pt_blk *blk;
413 
414 	if (stack->spare) {
415 		blk = stack->spare;
416 		stack->spare = NULL;
417 	} else {
418 		blk = malloc(sizeof(struct intel_pt_blk));
419 		if (!blk)
420 			return -ENOMEM;
421 	}
422 
423 	blk->prev = stack->blk;
424 	stack->blk = blk;
425 	stack->pos = 0;
426 	return 0;
427 }
428 
429 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
430 {
431 	int err;
432 
433 	if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
434 		err = intel_pt_alloc_blk(stack);
435 		if (err)
436 			return err;
437 	}
438 
439 	stack->blk->ip[stack->pos++] = ip;
440 	return 0;
441 }
442 
443 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
444 {
445 	while (stack->blk)
446 		intel_pt_pop_blk(stack);
447 	stack->pos = 0;
448 }
449 
450 static void intel_pt_free_stack(struct intel_pt_stack *stack)
451 {
452 	intel_pt_clear_stack(stack);
453 	zfree(&stack->blk);
454 	zfree(&stack->spare);
455 }
456 
457 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
458 {
459 	intel_pt_free_stack(&decoder->stack);
460 	free(decoder);
461 }
462 
463 static int intel_pt_ext_err(int code)
464 {
465 	switch (code) {
466 	case -ENOMEM:
467 		return INTEL_PT_ERR_NOMEM;
468 	case -ENOSYS:
469 		return INTEL_PT_ERR_INTERN;
470 	case -EBADMSG:
471 		return INTEL_PT_ERR_BADPKT;
472 	case -ENODATA:
473 		return INTEL_PT_ERR_NODATA;
474 	case -EILSEQ:
475 		return INTEL_PT_ERR_NOINSN;
476 	case -ENOENT:
477 		return INTEL_PT_ERR_MISMAT;
478 	case -EOVERFLOW:
479 		return INTEL_PT_ERR_OVR;
480 	case -ENOSPC:
481 		return INTEL_PT_ERR_LOST;
482 	case -ELOOP:
483 		return INTEL_PT_ERR_NELOOP;
484 	default:
485 		return INTEL_PT_ERR_UNK;
486 	}
487 }
488 
489 static const char *intel_pt_err_msgs[] = {
490 	[INTEL_PT_ERR_NOMEM]  = "Memory allocation failed",
491 	[INTEL_PT_ERR_INTERN] = "Internal error",
492 	[INTEL_PT_ERR_BADPKT] = "Bad packet",
493 	[INTEL_PT_ERR_NODATA] = "No more data",
494 	[INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
495 	[INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
496 	[INTEL_PT_ERR_OVR]    = "Overflow packet",
497 	[INTEL_PT_ERR_LOST]   = "Lost trace data",
498 	[INTEL_PT_ERR_UNK]    = "Unknown error!",
499 	[INTEL_PT_ERR_NELOOP] = "Never-ending loop (refer perf config intel-pt.max-loops)",
500 };
501 
502 int intel_pt__strerror(int code, char *buf, size_t buflen)
503 {
504 	if (code < 1 || code >= INTEL_PT_ERR_MAX)
505 		code = INTEL_PT_ERR_UNK;
506 	strlcpy(buf, intel_pt_err_msgs[code], buflen);
507 	return 0;
508 }
509 
510 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
511 				 uint64_t last_ip)
512 {
513 	uint64_t ip;
514 
515 	switch (packet->count) {
516 	case 1:
517 		ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
518 		     packet->payload;
519 		break;
520 	case 2:
521 		ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
522 		     packet->payload;
523 		break;
524 	case 3:
525 		ip = packet->payload;
526 		/* Sign-extend 6-byte ip */
527 		if (ip & (uint64_t)0x800000000000ULL)
528 			ip |= (uint64_t)0xffff000000000000ULL;
529 		break;
530 	case 4:
531 		ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
532 		     packet->payload;
533 		break;
534 	case 6:
535 		ip = packet->payload;
536 		break;
537 	default:
538 		return 0;
539 	}
540 
541 	return ip;
542 }
543 
544 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
545 {
546 	decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
547 	decoder->have_last_ip = true;
548 }
549 
550 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
551 {
552 	intel_pt_set_last_ip(decoder);
553 	decoder->ip = decoder->last_ip;
554 }
555 
556 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
557 {
558 	intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
559 			    decoder->buf);
560 }
561 
562 static int intel_pt_bug(struct intel_pt_decoder *decoder)
563 {
564 	intel_pt_log("ERROR: Internal error\n");
565 	decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
566 	return -ENOSYS;
567 }
568 
569 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
570 {
571 	decoder->tx_flags = 0;
572 }
573 
574 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
575 {
576 	decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
577 }
578 
579 static inline void intel_pt_update_pip(struct intel_pt_decoder *decoder)
580 {
581 	decoder->pip_payload = decoder->packet.payload;
582 }
583 
584 static inline void intel_pt_update_nr(struct intel_pt_decoder *decoder)
585 {
586 	decoder->next_nr = decoder->pip_payload & 1;
587 }
588 
589 static inline void intel_pt_set_nr(struct intel_pt_decoder *decoder)
590 {
591 	decoder->nr = decoder->pip_payload & 1;
592 	decoder->next_nr = decoder->nr;
593 }
594 
595 static inline void intel_pt_set_pip(struct intel_pt_decoder *decoder)
596 {
597 	intel_pt_update_pip(decoder);
598 	intel_pt_set_nr(decoder);
599 }
600 
601 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
602 {
603 	intel_pt_clear_tx_flags(decoder);
604 	decoder->have_tma = false;
605 	decoder->pkt_len = 1;
606 	decoder->pkt_step = 1;
607 	intel_pt_decoder_log_packet(decoder);
608 	if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
609 		intel_pt_log("ERROR: Bad packet\n");
610 		decoder->pkt_state = INTEL_PT_STATE_ERR1;
611 	}
612 	return -EBADMSG;
613 }
614 
615 static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
616 {
617 	decoder->sample_timestamp = decoder->timestamp;
618 	decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
619 	decoder->state.cycles = decoder->tot_cyc_cnt;
620 }
621 
622 static void intel_pt_reposition(struct intel_pt_decoder *decoder)
623 {
624 	decoder->ip = 0;
625 	decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
626 	decoder->timestamp = 0;
627 	decoder->have_tma = false;
628 }
629 
630 static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
631 {
632 	struct intel_pt_buffer buffer = { .buf = 0, };
633 	int ret;
634 
635 	decoder->pkt_step = 0;
636 
637 	intel_pt_log("Getting more data\n");
638 	ret = decoder->get_trace(&buffer, decoder->data);
639 	if (ret)
640 		return ret;
641 	decoder->buf = buffer.buf;
642 	decoder->len = buffer.len;
643 	if (!decoder->len) {
644 		intel_pt_log("No more data\n");
645 		return -ENODATA;
646 	}
647 	decoder->buf_timestamp = buffer.ref_timestamp;
648 	if (!buffer.consecutive || reposition) {
649 		intel_pt_reposition(decoder);
650 		decoder->ref_timestamp = buffer.ref_timestamp;
651 		decoder->state.trace_nr = buffer.trace_nr;
652 		decoder->vm_tm_corr_same_buf = false;
653 		intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
654 			     decoder->ref_timestamp);
655 		return -ENOLINK;
656 	}
657 
658 	return 0;
659 }
660 
661 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
662 				  bool reposition)
663 {
664 	if (!decoder->next_buf)
665 		return intel_pt_get_data(decoder, reposition);
666 
667 	decoder->buf = decoder->next_buf;
668 	decoder->len = decoder->next_len;
669 	decoder->next_buf = 0;
670 	decoder->next_len = 0;
671 	return 0;
672 }
673 
674 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
675 {
676 	unsigned char *buf = decoder->temp_buf;
677 	size_t old_len, len, n;
678 	int ret;
679 
680 	old_len = decoder->len;
681 	len = decoder->len;
682 	memcpy(buf, decoder->buf, len);
683 
684 	ret = intel_pt_get_data(decoder, false);
685 	if (ret) {
686 		decoder->pos += old_len;
687 		return ret < 0 ? ret : -EINVAL;
688 	}
689 
690 	n = INTEL_PT_PKT_MAX_SZ - len;
691 	if (n > decoder->len)
692 		n = decoder->len;
693 	memcpy(buf + len, decoder->buf, n);
694 	len += n;
695 
696 	decoder->prev_pkt_ctx = decoder->pkt_ctx;
697 	ret = intel_pt_get_packet(buf, len, &decoder->packet, &decoder->pkt_ctx);
698 	if (ret < (int)old_len) {
699 		decoder->next_buf = decoder->buf;
700 		decoder->next_len = decoder->len;
701 		decoder->buf = buf;
702 		decoder->len = old_len;
703 		return intel_pt_bad_packet(decoder);
704 	}
705 
706 	decoder->next_buf = decoder->buf + (ret - old_len);
707 	decoder->next_len = decoder->len - (ret - old_len);
708 
709 	decoder->buf = buf;
710 	decoder->len = ret;
711 
712 	return ret;
713 }
714 
715 struct intel_pt_pkt_info {
716 	struct intel_pt_decoder	  *decoder;
717 	struct intel_pt_pkt       packet;
718 	uint64_t                  pos;
719 	int                       pkt_len;
720 	int                       last_packet_type;
721 	void                      *data;
722 };
723 
724 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
725 
726 /* Lookahead packets in current buffer */
727 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
728 				  intel_pt_pkt_cb_t cb, void *data)
729 {
730 	struct intel_pt_pkt_info pkt_info;
731 	const unsigned char *buf = decoder->buf;
732 	enum intel_pt_pkt_ctx pkt_ctx = decoder->pkt_ctx;
733 	size_t len = decoder->len;
734 	int ret;
735 
736 	pkt_info.decoder          = decoder;
737 	pkt_info.pos              = decoder->pos;
738 	pkt_info.pkt_len          = decoder->pkt_step;
739 	pkt_info.last_packet_type = decoder->last_packet_type;
740 	pkt_info.data             = data;
741 
742 	while (1) {
743 		do {
744 			pkt_info.pos += pkt_info.pkt_len;
745 			buf          += pkt_info.pkt_len;
746 			len          -= pkt_info.pkt_len;
747 
748 			if (!len)
749 				return INTEL_PT_NEED_MORE_BYTES;
750 
751 			ret = intel_pt_get_packet(buf, len, &pkt_info.packet,
752 						  &pkt_ctx);
753 			if (!ret)
754 				return INTEL_PT_NEED_MORE_BYTES;
755 			if (ret < 0)
756 				return ret;
757 
758 			pkt_info.pkt_len = ret;
759 		} while (pkt_info.packet.type == INTEL_PT_PAD);
760 
761 		ret = cb(&pkt_info);
762 		if (ret)
763 			return 0;
764 
765 		pkt_info.last_packet_type = pkt_info.packet.type;
766 	}
767 }
768 
769 struct intel_pt_calc_cyc_to_tsc_info {
770 	uint64_t        cycle_cnt;
771 	unsigned int    cbr;
772 	uint32_t        last_mtc;
773 	uint64_t        ctc_timestamp;
774 	uint64_t        ctc_delta;
775 	uint64_t        tsc_timestamp;
776 	uint64_t        timestamp;
777 	bool            have_tma;
778 	bool            fixup_last_mtc;
779 	bool            from_mtc;
780 	double          cbr_cyc_to_tsc;
781 };
782 
783 /*
784  * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
785  * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
786  * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
787  * packet by copying the missing bits from the current MTC assuming the least
788  * difference between the two, and that the current MTC comes after last_mtc.
789  */
790 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
791 				    uint32_t *last_mtc)
792 {
793 	uint32_t first_missing_bit = 1U << (16 - mtc_shift);
794 	uint32_t mask = ~(first_missing_bit - 1);
795 
796 	*last_mtc |= mtc & mask;
797 	if (*last_mtc >= mtc) {
798 		*last_mtc -= first_missing_bit;
799 		*last_mtc &= 0xff;
800 	}
801 }
802 
803 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
804 {
805 	struct intel_pt_decoder *decoder = pkt_info->decoder;
806 	struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
807 	uint64_t timestamp;
808 	double cyc_to_tsc;
809 	unsigned int cbr;
810 	uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
811 
812 	switch (pkt_info->packet.type) {
813 	case INTEL_PT_TNT:
814 	case INTEL_PT_TIP_PGE:
815 	case INTEL_PT_TIP:
816 	case INTEL_PT_FUP:
817 	case INTEL_PT_PSB:
818 	case INTEL_PT_PIP:
819 	case INTEL_PT_MODE_EXEC:
820 	case INTEL_PT_MODE_TSX:
821 	case INTEL_PT_PSBEND:
822 	case INTEL_PT_PAD:
823 	case INTEL_PT_VMCS:
824 	case INTEL_PT_MNT:
825 	case INTEL_PT_PTWRITE:
826 	case INTEL_PT_PTWRITE_IP:
827 	case INTEL_PT_BBP:
828 	case INTEL_PT_BIP:
829 	case INTEL_PT_BEP:
830 	case INTEL_PT_BEP_IP:
831 	case INTEL_PT_CFE:
832 	case INTEL_PT_CFE_IP:
833 	case INTEL_PT_EVD:
834 		return 0;
835 
836 	case INTEL_PT_MTC:
837 		if (!data->have_tma)
838 			return 0;
839 
840 		mtc = pkt_info->packet.payload;
841 		if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
842 			data->fixup_last_mtc = false;
843 			intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
844 						&data->last_mtc);
845 		}
846 		if (mtc > data->last_mtc)
847 			mtc_delta = mtc - data->last_mtc;
848 		else
849 			mtc_delta = mtc + 256 - data->last_mtc;
850 		data->ctc_delta += mtc_delta << decoder->mtc_shift;
851 		data->last_mtc = mtc;
852 
853 		if (decoder->tsc_ctc_mult) {
854 			timestamp = data->ctc_timestamp +
855 				data->ctc_delta * decoder->tsc_ctc_mult;
856 		} else {
857 			timestamp = data->ctc_timestamp +
858 				multdiv(data->ctc_delta,
859 					decoder->tsc_ctc_ratio_n,
860 					decoder->tsc_ctc_ratio_d);
861 		}
862 
863 		if (timestamp < data->timestamp)
864 			return 1;
865 
866 		if (pkt_info->last_packet_type != INTEL_PT_CYC) {
867 			data->timestamp = timestamp;
868 			return 0;
869 		}
870 
871 		break;
872 
873 	case INTEL_PT_TSC:
874 		/*
875 		 * For now, do not support using TSC packets - refer
876 		 * intel_pt_calc_cyc_to_tsc().
877 		 */
878 		if (data->from_mtc)
879 			return 1;
880 		timestamp = pkt_info->packet.payload |
881 			    (data->timestamp & (0xffULL << 56));
882 		if (data->from_mtc && timestamp < data->timestamp &&
883 		    data->timestamp - timestamp < decoder->tsc_slip)
884 			return 1;
885 		if (timestamp < data->timestamp)
886 			timestamp += (1ULL << 56);
887 		if (pkt_info->last_packet_type != INTEL_PT_CYC) {
888 			if (data->from_mtc)
889 				return 1;
890 			data->tsc_timestamp = timestamp;
891 			data->timestamp = timestamp;
892 			return 0;
893 		}
894 		break;
895 
896 	case INTEL_PT_TMA:
897 		if (data->from_mtc)
898 			return 1;
899 
900 		if (!decoder->tsc_ctc_ratio_d)
901 			return 0;
902 
903 		ctc = pkt_info->packet.payload;
904 		fc = pkt_info->packet.count;
905 		ctc_rem = ctc & decoder->ctc_rem_mask;
906 
907 		data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
908 
909 		data->ctc_timestamp = data->tsc_timestamp - fc;
910 		if (decoder->tsc_ctc_mult) {
911 			data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
912 		} else {
913 			data->ctc_timestamp -=
914 				multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
915 					decoder->tsc_ctc_ratio_d);
916 		}
917 
918 		data->ctc_delta = 0;
919 		data->have_tma = true;
920 		data->fixup_last_mtc = true;
921 
922 		return 0;
923 
924 	case INTEL_PT_CYC:
925 		data->cycle_cnt += pkt_info->packet.payload;
926 		return 0;
927 
928 	case INTEL_PT_CBR:
929 		cbr = pkt_info->packet.payload;
930 		if (data->cbr && data->cbr != cbr)
931 			return 1;
932 		data->cbr = cbr;
933 		data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
934 		return 0;
935 
936 	case INTEL_PT_TIP_PGD:
937 	case INTEL_PT_TRACESTOP:
938 	case INTEL_PT_EXSTOP:
939 	case INTEL_PT_EXSTOP_IP:
940 	case INTEL_PT_MWAIT:
941 	case INTEL_PT_PWRE:
942 	case INTEL_PT_PWRX:
943 	case INTEL_PT_OVF:
944 	case INTEL_PT_BAD: /* Does not happen */
945 	default:
946 		return 1;
947 	}
948 
949 	if (!data->cbr && decoder->cbr) {
950 		data->cbr = decoder->cbr;
951 		data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
952 	}
953 
954 	if (!data->cycle_cnt)
955 		return 1;
956 
957 	cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
958 
959 	if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
960 	    cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
961 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
962 			     cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
963 		return 1;
964 	}
965 
966 	decoder->calc_cyc_to_tsc = cyc_to_tsc;
967 	decoder->have_calc_cyc_to_tsc = true;
968 
969 	if (data->cbr) {
970 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
971 			     cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
972 	} else {
973 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
974 			     cyc_to_tsc, pkt_info->pos);
975 	}
976 
977 	return 1;
978 }
979 
980 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
981 				     bool from_mtc)
982 {
983 	struct intel_pt_calc_cyc_to_tsc_info data = {
984 		.cycle_cnt      = 0,
985 		.cbr            = 0,
986 		.last_mtc       = decoder->last_mtc,
987 		.ctc_timestamp  = decoder->ctc_timestamp,
988 		.ctc_delta      = decoder->ctc_delta,
989 		.tsc_timestamp  = decoder->tsc_timestamp,
990 		.timestamp      = decoder->timestamp,
991 		.have_tma       = decoder->have_tma,
992 		.fixup_last_mtc = decoder->fixup_last_mtc,
993 		.from_mtc       = from_mtc,
994 		.cbr_cyc_to_tsc = 0,
995 	};
996 
997 	/*
998 	 * For now, do not support using TSC packets for at least the reasons:
999 	 * 1) timing might have stopped
1000 	 * 2) TSC packets within PSB+ can slip against CYC packets
1001 	 */
1002 	if (!from_mtc)
1003 		return;
1004 
1005 	intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
1006 }
1007 
1008 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
1009 {
1010 	int ret;
1011 
1012 	decoder->last_packet_type = decoder->packet.type;
1013 
1014 	do {
1015 		decoder->pos += decoder->pkt_step;
1016 		decoder->buf += decoder->pkt_step;
1017 		decoder->len -= decoder->pkt_step;
1018 
1019 		if (!decoder->len) {
1020 			ret = intel_pt_get_next_data(decoder, false);
1021 			if (ret)
1022 				return ret;
1023 		}
1024 
1025 		decoder->prev_pkt_ctx = decoder->pkt_ctx;
1026 		ret = intel_pt_get_packet(decoder->buf, decoder->len,
1027 					  &decoder->packet, &decoder->pkt_ctx);
1028 		if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
1029 		    decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
1030 			ret = intel_pt_get_split_packet(decoder);
1031 			if (ret < 0)
1032 				return ret;
1033 		}
1034 		if (ret <= 0)
1035 			return intel_pt_bad_packet(decoder);
1036 
1037 		decoder->pkt_len = ret;
1038 		decoder->pkt_step = ret;
1039 		intel_pt_decoder_log_packet(decoder);
1040 	} while (decoder->packet.type == INTEL_PT_PAD);
1041 
1042 	return 0;
1043 }
1044 
1045 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
1046 {
1047 	uint64_t timestamp, masked_timestamp;
1048 
1049 	timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
1050 	masked_timestamp = timestamp & decoder->period_mask;
1051 	if (decoder->continuous_period) {
1052 		if (masked_timestamp > decoder->last_masked_timestamp)
1053 			return 1;
1054 	} else {
1055 		timestamp += 1;
1056 		masked_timestamp = timestamp & decoder->period_mask;
1057 		if (masked_timestamp > decoder->last_masked_timestamp) {
1058 			decoder->last_masked_timestamp = masked_timestamp;
1059 			decoder->continuous_period = true;
1060 		}
1061 	}
1062 
1063 	if (masked_timestamp < decoder->last_masked_timestamp)
1064 		return decoder->period_ticks;
1065 
1066 	return decoder->period_ticks - (timestamp - masked_timestamp);
1067 }
1068 
1069 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
1070 {
1071 	switch (decoder->period_type) {
1072 	case INTEL_PT_PERIOD_INSTRUCTIONS:
1073 		return decoder->period - decoder->period_insn_cnt;
1074 	case INTEL_PT_PERIOD_TICKS:
1075 		return intel_pt_next_period(decoder);
1076 	case INTEL_PT_PERIOD_NONE:
1077 	case INTEL_PT_PERIOD_MTC:
1078 	default:
1079 		return 0;
1080 	}
1081 }
1082 
1083 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
1084 {
1085 	uint64_t timestamp, masked_timestamp;
1086 
1087 	switch (decoder->period_type) {
1088 	case INTEL_PT_PERIOD_INSTRUCTIONS:
1089 		decoder->period_insn_cnt = 0;
1090 		break;
1091 	case INTEL_PT_PERIOD_TICKS:
1092 		timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
1093 		masked_timestamp = timestamp & decoder->period_mask;
1094 		if (masked_timestamp > decoder->last_masked_timestamp)
1095 			decoder->last_masked_timestamp = masked_timestamp;
1096 		else
1097 			decoder->last_masked_timestamp += decoder->period_ticks;
1098 		break;
1099 	case INTEL_PT_PERIOD_NONE:
1100 	case INTEL_PT_PERIOD_MTC:
1101 	default:
1102 		break;
1103 	}
1104 
1105 	decoder->state.type |= INTEL_PT_INSTRUCTION;
1106 }
1107 
1108 /*
1109  * Sample FUP instruction at the same time as reporting the FUP event, so the
1110  * instruction sample gets the same flags as the FUP event.
1111  */
1112 static void intel_pt_sample_fup_insn(struct intel_pt_decoder *decoder)
1113 {
1114 	struct intel_pt_insn intel_pt_insn;
1115 	uint64_t max_insn_cnt, insn_cnt = 0;
1116 	int err;
1117 
1118 	decoder->state.insn_op = INTEL_PT_OP_OTHER;
1119 	decoder->state.insn_len = 0;
1120 
1121 	if (!decoder->branch_enable || !decoder->pge || decoder->hop ||
1122 	    decoder->ip != decoder->last_ip)
1123 		return;
1124 
1125 	if (!decoder->mtc_insn)
1126 		decoder->mtc_insn = true;
1127 
1128 	max_insn_cnt = intel_pt_next_sample(decoder);
1129 	if (max_insn_cnt != 1)
1130 		return;
1131 
1132 	err = decoder->walk_insn(&intel_pt_insn, &insn_cnt, &decoder->ip,
1133 				 0, max_insn_cnt, decoder->data);
1134 	/* Ignore error, it will be reported next walk anyway */
1135 	if (err)
1136 		return;
1137 
1138 	if (intel_pt_insn.branch != INTEL_PT_BR_NO_BRANCH) {
1139 		intel_pt_log_at("ERROR: Unexpected branch at FUP instruction", decoder->ip);
1140 		return;
1141 	}
1142 
1143 	decoder->tot_insn_cnt += insn_cnt;
1144 	decoder->timestamp_insn_cnt += insn_cnt;
1145 	decoder->sample_insn_cnt += insn_cnt;
1146 	decoder->period_insn_cnt += insn_cnt;
1147 
1148 	intel_pt_sample_insn(decoder);
1149 
1150 	decoder->state.type |= INTEL_PT_INSTRUCTION;
1151 	decoder->ip += intel_pt_insn.length;
1152 }
1153 
1154 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
1155 			      struct intel_pt_insn *intel_pt_insn, uint64_t ip)
1156 {
1157 	uint64_t max_insn_cnt, insn_cnt = 0;
1158 	int err;
1159 
1160 	if (!decoder->mtc_insn)
1161 		decoder->mtc_insn = true;
1162 
1163 	max_insn_cnt = intel_pt_next_sample(decoder);
1164 
1165 	err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
1166 				 max_insn_cnt, decoder->data);
1167 
1168 	decoder->tot_insn_cnt += insn_cnt;
1169 	decoder->timestamp_insn_cnt += insn_cnt;
1170 	decoder->sample_insn_cnt += insn_cnt;
1171 	decoder->period_insn_cnt += insn_cnt;
1172 
1173 	if (err) {
1174 		decoder->no_progress = 0;
1175 		decoder->pkt_state = INTEL_PT_STATE_ERR2;
1176 		intel_pt_log_at("ERROR: Failed to get instruction",
1177 				decoder->ip);
1178 		if (err == -ENOENT)
1179 			return -ENOLINK;
1180 		return -EILSEQ;
1181 	}
1182 
1183 	if (ip && decoder->ip == ip) {
1184 		err = -EAGAIN;
1185 		goto out;
1186 	}
1187 
1188 	if (max_insn_cnt && insn_cnt >= max_insn_cnt)
1189 		intel_pt_sample_insn(decoder);
1190 
1191 	if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
1192 		decoder->state.type = INTEL_PT_INSTRUCTION;
1193 		decoder->state.from_ip = decoder->ip;
1194 		decoder->state.to_ip = 0;
1195 		decoder->ip += intel_pt_insn->length;
1196 		err = INTEL_PT_RETURN;
1197 		goto out;
1198 	}
1199 
1200 	if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
1201 		/* Zero-length calls are excluded */
1202 		if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
1203 		    intel_pt_insn->rel) {
1204 			err = intel_pt_push(&decoder->stack, decoder->ip +
1205 					    intel_pt_insn->length);
1206 			if (err)
1207 				goto out;
1208 		}
1209 	} else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
1210 		decoder->ret_addr = intel_pt_pop(&decoder->stack);
1211 	}
1212 
1213 	if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1214 		int cnt = decoder->no_progress++;
1215 
1216 		decoder->state.from_ip = decoder->ip;
1217 		decoder->ip += intel_pt_insn->length +
1218 				intel_pt_insn->rel;
1219 		decoder->state.to_ip = decoder->ip;
1220 		err = INTEL_PT_RETURN;
1221 
1222 		/*
1223 		 * Check for being stuck in a loop.  This can happen if a
1224 		 * decoder error results in the decoder erroneously setting the
1225 		 * ip to an address that is itself in an infinite loop that
1226 		 * consumes no packets.  When that happens, there must be an
1227 		 * unconditional branch.
1228 		 */
1229 		if (cnt) {
1230 			if (cnt == 1) {
1231 				decoder->stuck_ip = decoder->state.to_ip;
1232 				decoder->stuck_ip_prd = 1;
1233 				decoder->stuck_ip_cnt = 1;
1234 			} else if (cnt > decoder->max_loops ||
1235 				   decoder->state.to_ip == decoder->stuck_ip) {
1236 				intel_pt_log_at("ERROR: Never-ending loop",
1237 						decoder->state.to_ip);
1238 				decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1239 				err = -ELOOP;
1240 				goto out;
1241 			} else if (!--decoder->stuck_ip_cnt) {
1242 				decoder->stuck_ip_prd += 1;
1243 				decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1244 				decoder->stuck_ip = decoder->state.to_ip;
1245 			}
1246 		}
1247 		goto out_no_progress;
1248 	}
1249 out:
1250 	decoder->no_progress = 0;
1251 out_no_progress:
1252 	decoder->state.insn_op = intel_pt_insn->op;
1253 	decoder->state.insn_len = intel_pt_insn->length;
1254 	memcpy(decoder->state.insn, intel_pt_insn->buf,
1255 	       INTEL_PT_INSN_BUF_SZ);
1256 
1257 	if (decoder->tx_flags & INTEL_PT_IN_TX)
1258 		decoder->state.flags |= INTEL_PT_IN_TX;
1259 
1260 	return err;
1261 }
1262 
1263 static void intel_pt_mode_exec_status(struct intel_pt_decoder *decoder)
1264 {
1265 	bool iflag = decoder->packet.count & INTEL_PT_IFLAG;
1266 
1267 	decoder->exec_mode = decoder->packet.payload;
1268 	decoder->iflag = iflag;
1269 	decoder->next_iflag = iflag;
1270 	decoder->state.from_iflag = iflag;
1271 	decoder->state.to_iflag = iflag;
1272 }
1273 
1274 static void intel_pt_mode_exec(struct intel_pt_decoder *decoder)
1275 {
1276 	bool iflag = decoder->packet.count & INTEL_PT_IFLAG;
1277 
1278 	decoder->exec_mode = decoder->packet.payload;
1279 	decoder->next_iflag = iflag;
1280 }
1281 
1282 static void intel_pt_sample_iflag(struct intel_pt_decoder *decoder)
1283 {
1284 	decoder->state.type |= INTEL_PT_IFLAG_CHG;
1285 	decoder->state.from_iflag = decoder->iflag;
1286 	decoder->state.to_iflag = decoder->next_iflag;
1287 	decoder->iflag = decoder->next_iflag;
1288 }
1289 
1290 static void intel_pt_sample_iflag_chg(struct intel_pt_decoder *decoder)
1291 {
1292 	if (decoder->iflag != decoder->next_iflag)
1293 		intel_pt_sample_iflag(decoder);
1294 }
1295 
1296 static void intel_pt_clear_fup_event(struct intel_pt_decoder *decoder)
1297 {
1298 	decoder->set_fup_tx_flags = false;
1299 	decoder->set_fup_ptw = false;
1300 	decoder->set_fup_mwait = false;
1301 	decoder->set_fup_pwre = false;
1302 	decoder->set_fup_exstop = false;
1303 	decoder->set_fup_bep = false;
1304 	decoder->set_fup_cfe_ip = false;
1305 	decoder->set_fup_cfe = false;
1306 	decoder->evd_cnt = 0;
1307 	decoder->set_fup_mode_exec = false;
1308 	decoder->iflag = decoder->next_iflag;
1309 }
1310 
1311 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder, bool no_tip)
1312 {
1313 	enum intel_pt_sample_type type = decoder->state.type;
1314 	bool sample_fup_insn = false;
1315 	bool ret = false;
1316 
1317 	decoder->state.type &= ~INTEL_PT_BRANCH;
1318 
1319 	if (decoder->set_fup_cfe_ip || decoder->set_fup_cfe) {
1320 		bool ip = decoder->set_fup_cfe_ip;
1321 
1322 		decoder->set_fup_cfe_ip = false;
1323 		decoder->set_fup_cfe = false;
1324 		decoder->state.type |= INTEL_PT_EVT;
1325 		if (!ip && decoder->pge)
1326 			decoder->state.type |= INTEL_PT_BRANCH;
1327 		decoder->state.cfe_type = decoder->fup_cfe_pkt.count;
1328 		decoder->state.cfe_vector = decoder->fup_cfe_pkt.payload;
1329 		decoder->state.evd_cnt = decoder->evd_cnt;
1330 		decoder->state.evd = decoder->evd;
1331 		decoder->evd_cnt = 0;
1332 		if (ip || decoder->pge)
1333 			decoder->state.flags |= INTEL_PT_FUP_IP;
1334 		ret = true;
1335 	}
1336 	if (decoder->set_fup_mode_exec) {
1337 		decoder->set_fup_mode_exec = false;
1338 		intel_pt_sample_iflag(decoder);
1339 		sample_fup_insn = no_tip;
1340 		ret = true;
1341 	}
1342 	if (decoder->set_fup_tx_flags) {
1343 		decoder->set_fup_tx_flags = false;
1344 		decoder->tx_flags = decoder->fup_tx_flags;
1345 		decoder->state.type |= INTEL_PT_TRANSACTION;
1346 		if (decoder->fup_tx_flags & INTEL_PT_ABORT_TX)
1347 			decoder->state.type |= INTEL_PT_BRANCH;
1348 		decoder->state.flags = decoder->fup_tx_flags;
1349 		ret = true;
1350 	}
1351 	if (decoder->set_fup_ptw) {
1352 		decoder->set_fup_ptw = false;
1353 		decoder->state.type |= INTEL_PT_PTW;
1354 		decoder->state.flags |= INTEL_PT_FUP_IP;
1355 		decoder->state.ptw_payload = decoder->fup_ptw_payload;
1356 		ret = true;
1357 	}
1358 	if (decoder->set_fup_mwait) {
1359 		decoder->set_fup_mwait = false;
1360 		decoder->state.type |= INTEL_PT_MWAIT_OP;
1361 		decoder->state.mwait_payload = decoder->fup_mwait_payload;
1362 		ret = true;
1363 	}
1364 	if (decoder->set_fup_pwre) {
1365 		decoder->set_fup_pwre = false;
1366 		decoder->state.type |= INTEL_PT_PWR_ENTRY;
1367 		decoder->state.pwre_payload = decoder->fup_pwre_payload;
1368 		ret = true;
1369 	}
1370 	if (decoder->set_fup_exstop) {
1371 		decoder->set_fup_exstop = false;
1372 		decoder->state.type |= INTEL_PT_EX_STOP;
1373 		decoder->state.flags |= INTEL_PT_FUP_IP;
1374 		ret = true;
1375 	}
1376 	if (decoder->set_fup_bep) {
1377 		decoder->set_fup_bep = false;
1378 		decoder->state.type |= INTEL_PT_BLK_ITEMS;
1379 		ret = true;
1380 	}
1381 	if (decoder->overflow) {
1382 		decoder->overflow = false;
1383 		if (!ret && !decoder->pge) {
1384 			if (decoder->hop) {
1385 				decoder->state.type = 0;
1386 				decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
1387 			}
1388 			decoder->pge = true;
1389 			decoder->state.type |= INTEL_PT_BRANCH | INTEL_PT_TRACE_BEGIN;
1390 			decoder->state.from_ip = 0;
1391 			decoder->state.to_ip = decoder->ip;
1392 			return true;
1393 		}
1394 	}
1395 	if (ret) {
1396 		decoder->state.from_ip = decoder->ip;
1397 		decoder->state.to_ip = 0;
1398 		if (sample_fup_insn)
1399 			intel_pt_sample_fup_insn(decoder);
1400 	} else {
1401 		decoder->state.type = type;
1402 	}
1403 	return ret;
1404 }
1405 
1406 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1407 					  struct intel_pt_insn *intel_pt_insn,
1408 					  uint64_t ip, int err)
1409 {
1410 	return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1411 	       intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1412 	       ip == decoder->ip + intel_pt_insn->length;
1413 }
1414 
1415 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1416 {
1417 	struct intel_pt_insn intel_pt_insn;
1418 	uint64_t ip;
1419 	int err;
1420 
1421 	ip = decoder->last_ip;
1422 
1423 	while (1) {
1424 		err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1425 		if (err == INTEL_PT_RETURN)
1426 			return 0;
1427 		if (err == -EAGAIN ||
1428 		    intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1429 			bool no_tip = decoder->pkt_state != INTEL_PT_STATE_FUP;
1430 
1431 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1432 			if (intel_pt_fup_event(decoder, no_tip) && no_tip)
1433 				return 0;
1434 			return -EAGAIN;
1435 		}
1436 		decoder->set_fup_tx_flags = false;
1437 		if (err)
1438 			return err;
1439 
1440 		if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1441 			intel_pt_log_at("ERROR: Unexpected indirect branch",
1442 					decoder->ip);
1443 			decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1444 			return -ENOENT;
1445 		}
1446 
1447 		if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1448 			intel_pt_log_at("ERROR: Unexpected conditional branch",
1449 					decoder->ip);
1450 			decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1451 			return -ENOENT;
1452 		}
1453 
1454 		intel_pt_bug(decoder);
1455 	}
1456 }
1457 
1458 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1459 {
1460 	struct intel_pt_insn intel_pt_insn;
1461 	int err;
1462 
1463 	err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1464 	if (err == INTEL_PT_RETURN &&
1465 	    decoder->pgd_ip &&
1466 	    decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1467 	    (decoder->state.type & INTEL_PT_BRANCH) &&
1468 	    decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1469 		/* Unconditional branch leaving filter region */
1470 		decoder->no_progress = 0;
1471 		decoder->pge = false;
1472 		decoder->continuous_period = false;
1473 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1474 		decoder->state.type |= INTEL_PT_TRACE_END;
1475 		intel_pt_update_nr(decoder);
1476 		return 0;
1477 	}
1478 	if (err == INTEL_PT_RETURN)
1479 		return 0;
1480 	if (err)
1481 		return err;
1482 
1483 	intel_pt_update_nr(decoder);
1484 	intel_pt_sample_iflag_chg(decoder);
1485 
1486 	if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1487 		if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1488 			decoder->pge = false;
1489 			decoder->continuous_period = false;
1490 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1491 			decoder->state.from_ip = decoder->ip;
1492 			if (decoder->packet.count == 0) {
1493 				decoder->state.to_ip = 0;
1494 			} else {
1495 				decoder->state.to_ip = decoder->last_ip;
1496 				decoder->ip = decoder->last_ip;
1497 			}
1498 			decoder->state.type |= INTEL_PT_TRACE_END;
1499 		} else {
1500 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1501 			decoder->state.from_ip = decoder->ip;
1502 			if (decoder->packet.count == 0) {
1503 				decoder->state.to_ip = 0;
1504 			} else {
1505 				decoder->state.to_ip = decoder->last_ip;
1506 				decoder->ip = decoder->last_ip;
1507 			}
1508 		}
1509 		return 0;
1510 	}
1511 
1512 	if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1513 		uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1514 				 intel_pt_insn.rel;
1515 
1516 		if (decoder->pgd_ip &&
1517 		    decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1518 		    decoder->pgd_ip(to_ip, decoder->data)) {
1519 			/* Conditional branch leaving filter region */
1520 			decoder->pge = false;
1521 			decoder->continuous_period = false;
1522 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1523 			decoder->ip = to_ip;
1524 			decoder->state.from_ip = decoder->ip;
1525 			decoder->state.to_ip = to_ip;
1526 			decoder->state.type |= INTEL_PT_TRACE_END;
1527 			return 0;
1528 		}
1529 		intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1530 				decoder->ip);
1531 		decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1532 		return -ENOENT;
1533 	}
1534 
1535 	return intel_pt_bug(decoder);
1536 }
1537 
1538 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1539 {
1540 	struct intel_pt_insn intel_pt_insn;
1541 	int err;
1542 
1543 	while (1) {
1544 		err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1545 		if (err == INTEL_PT_RETURN)
1546 			return 0;
1547 		if (err)
1548 			return err;
1549 
1550 		if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1551 			if (!decoder->return_compression) {
1552 				intel_pt_log_at("ERROR: RET when expecting conditional branch",
1553 						decoder->ip);
1554 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1555 				return -ENOENT;
1556 			}
1557 			if (!decoder->ret_addr) {
1558 				intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1559 						decoder->ip);
1560 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1561 				return -ENOENT;
1562 			}
1563 			if (!(decoder->tnt.payload & BIT63)) {
1564 				intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1565 						decoder->ip);
1566 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1567 				return -ENOENT;
1568 			}
1569 			decoder->tnt.count -= 1;
1570 			if (decoder->tnt.count)
1571 				decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1572 			else
1573 				decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1574 			decoder->tnt.payload <<= 1;
1575 			decoder->state.from_ip = decoder->ip;
1576 			decoder->ip = decoder->ret_addr;
1577 			decoder->state.to_ip = decoder->ip;
1578 			return 0;
1579 		}
1580 
1581 		if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1582 			/* Handle deferred TIPs */
1583 			err = intel_pt_get_next_packet(decoder);
1584 			if (err)
1585 				return err;
1586 			if (decoder->packet.type != INTEL_PT_TIP ||
1587 			    decoder->packet.count == 0) {
1588 				intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1589 						decoder->ip);
1590 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1591 				decoder->pkt_step = 0;
1592 				return -ENOENT;
1593 			}
1594 			intel_pt_set_last_ip(decoder);
1595 			decoder->state.from_ip = decoder->ip;
1596 			decoder->state.to_ip = decoder->last_ip;
1597 			decoder->ip = decoder->last_ip;
1598 			intel_pt_update_nr(decoder);
1599 			intel_pt_sample_iflag_chg(decoder);
1600 			return 0;
1601 		}
1602 
1603 		if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1604 			decoder->tnt.count -= 1;
1605 			if (decoder->tnt.count)
1606 				decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1607 			else
1608 				decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1609 			if (decoder->tnt.payload & BIT63) {
1610 				decoder->tnt.payload <<= 1;
1611 				decoder->state.from_ip = decoder->ip;
1612 				decoder->ip += intel_pt_insn.length +
1613 					       intel_pt_insn.rel;
1614 				decoder->state.to_ip = decoder->ip;
1615 				return 0;
1616 			}
1617 			/* Instruction sample for a non-taken branch */
1618 			if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1619 				decoder->tnt.payload <<= 1;
1620 				decoder->state.type = INTEL_PT_INSTRUCTION;
1621 				decoder->state.from_ip = decoder->ip;
1622 				decoder->state.to_ip = 0;
1623 				decoder->ip += intel_pt_insn.length;
1624 				return 0;
1625 			}
1626 			decoder->sample_cyc = false;
1627 			decoder->ip += intel_pt_insn.length;
1628 			if (!decoder->tnt.count) {
1629 				intel_pt_update_sample_time(decoder);
1630 				return -EAGAIN;
1631 			}
1632 			decoder->tnt.payload <<= 1;
1633 			continue;
1634 		}
1635 
1636 		return intel_pt_bug(decoder);
1637 	}
1638 }
1639 
1640 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1641 {
1642 	unsigned int fup_tx_flags;
1643 	int err;
1644 
1645 	fup_tx_flags = decoder->packet.payload &
1646 		       (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1647 	err = intel_pt_get_next_packet(decoder);
1648 	if (err)
1649 		return err;
1650 	if (decoder->packet.type == INTEL_PT_FUP) {
1651 		decoder->fup_tx_flags = fup_tx_flags;
1652 		decoder->set_fup_tx_flags = true;
1653 		if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1654 			*no_tip = true;
1655 	} else {
1656 		intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1657 				decoder->pos);
1658 		intel_pt_update_in_tx(decoder);
1659 	}
1660 	return 0;
1661 }
1662 
1663 static int intel_pt_evd(struct intel_pt_decoder *decoder)
1664 {
1665 	if (decoder->evd_cnt >= INTEL_PT_MAX_EVDS) {
1666 		intel_pt_log_at("ERROR: Too many EVD packets", decoder->pos);
1667 		return -ENOSYS;
1668 	}
1669 	decoder->evd[decoder->evd_cnt++] = (struct intel_pt_evd){
1670 		.type = decoder->packet.count,
1671 		.payload = decoder->packet.payload,
1672 	};
1673 	return 0;
1674 }
1675 
1676 static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
1677 {
1678 	timestamp |= (ref_timestamp & (0xffULL << 56));
1679 
1680 	if (timestamp < ref_timestamp) {
1681 		if (ref_timestamp - timestamp > (1ULL << 55))
1682 			timestamp += (1ULL << 56);
1683 	} else {
1684 		if (timestamp - ref_timestamp > (1ULL << 55))
1685 			timestamp -= (1ULL << 56);
1686 	}
1687 
1688 	return timestamp;
1689 }
1690 
1691 /* For use only when decoder->vm_time_correlation is true */
1692 static bool intel_pt_time_in_range(struct intel_pt_decoder *decoder,
1693 				   uint64_t timestamp)
1694 {
1695 	uint64_t max_timestamp = decoder->buf_timestamp;
1696 
1697 	if (!max_timestamp) {
1698 		max_timestamp = decoder->last_reliable_timestamp +
1699 				0x400000000ULL;
1700 	}
1701 	return timestamp >= decoder->last_reliable_timestamp &&
1702 	       timestamp < decoder->buf_timestamp;
1703 }
1704 
1705 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1706 {
1707 	uint64_t timestamp;
1708 	bool bad = false;
1709 
1710 	decoder->have_tma = false;
1711 
1712 	if (decoder->ref_timestamp) {
1713 		timestamp = intel_pt_8b_tsc(decoder->packet.payload,
1714 					    decoder->ref_timestamp);
1715 		decoder->tsc_timestamp = timestamp;
1716 		decoder->timestamp = timestamp;
1717 		decoder->ref_timestamp = 0;
1718 		decoder->timestamp_insn_cnt = 0;
1719 	} else if (decoder->timestamp) {
1720 		timestamp = decoder->packet.payload |
1721 			    (decoder->timestamp & (0xffULL << 56));
1722 		decoder->tsc_timestamp = timestamp;
1723 		if (timestamp < decoder->timestamp &&
1724 		    decoder->timestamp - timestamp < decoder->tsc_slip) {
1725 			intel_pt_log_to("Suppressing backwards timestamp",
1726 					timestamp);
1727 			timestamp = decoder->timestamp;
1728 		}
1729 		if (timestamp < decoder->timestamp) {
1730 			if (!decoder->buf_timestamp ||
1731 			    (timestamp + (1ULL << 56) < decoder->buf_timestamp)) {
1732 				intel_pt_log_to("Wraparound timestamp", timestamp);
1733 				timestamp += (1ULL << 56);
1734 				decoder->tsc_timestamp = timestamp;
1735 			} else {
1736 				intel_pt_log_to("Suppressing bad timestamp", timestamp);
1737 				timestamp = decoder->timestamp;
1738 				bad = true;
1739 			}
1740 		}
1741 		if (decoder->vm_time_correlation &&
1742 		    (bad || !intel_pt_time_in_range(decoder, timestamp)) &&
1743 		    intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
1744 			p_log("Timestamp out of range");
1745 		decoder->timestamp = timestamp;
1746 		decoder->timestamp_insn_cnt = 0;
1747 	}
1748 
1749 	if (decoder->last_packet_type == INTEL_PT_CYC) {
1750 		decoder->cyc_ref_timestamp = decoder->timestamp;
1751 		decoder->cycle_cnt = 0;
1752 		decoder->have_calc_cyc_to_tsc = false;
1753 		intel_pt_calc_cyc_to_tsc(decoder, false);
1754 	}
1755 
1756 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1757 }
1758 
1759 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1760 {
1761 	intel_pt_log("ERROR: Buffer overflow\n");
1762 	intel_pt_clear_tx_flags(decoder);
1763 	intel_pt_set_nr(decoder);
1764 	decoder->timestamp_insn_cnt = 0;
1765 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1766 	decoder->state.from_ip = decoder->ip;
1767 	decoder->ip = 0;
1768 	decoder->pge = false;
1769 	intel_pt_clear_fup_event(decoder);
1770 	decoder->overflow = true;
1771 	return -EOVERFLOW;
1772 }
1773 
1774 static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
1775 {
1776 	if (decoder->have_cyc)
1777 		return;
1778 
1779 	decoder->cyc_cnt_timestamp = decoder->timestamp;
1780 	decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
1781 }
1782 
1783 static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
1784 {
1785 	decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
1786 
1787 	if (decoder->pge)
1788 		intel_pt_mtc_cyc_cnt_pge(decoder);
1789 }
1790 
1791 static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
1792 {
1793 	uint64_t tot_cyc_cnt, tsc_delta;
1794 
1795 	if (decoder->have_cyc)
1796 		return;
1797 
1798 	decoder->sample_cyc = true;
1799 
1800 	if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
1801 		return;
1802 
1803 	tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
1804 	tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
1805 
1806 	if (tot_cyc_cnt > decoder->tot_cyc_cnt)
1807 		decoder->tot_cyc_cnt = tot_cyc_cnt;
1808 }
1809 
1810 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1811 {
1812 	uint32_t ctc = decoder->packet.payload;
1813 	uint32_t fc = decoder->packet.count;
1814 	uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1815 
1816 	if (!decoder->tsc_ctc_ratio_d)
1817 		return;
1818 
1819 	if (decoder->pge && !decoder->in_psb)
1820 		intel_pt_mtc_cyc_cnt_pge(decoder);
1821 	else
1822 		intel_pt_mtc_cyc_cnt_upd(decoder);
1823 
1824 	decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1825 	decoder->last_ctc = ctc - ctc_rem;
1826 	decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1827 	if (decoder->tsc_ctc_mult) {
1828 		decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1829 	} else {
1830 		decoder->ctc_timestamp -= multdiv(ctc_rem,
1831 						  decoder->tsc_ctc_ratio_n,
1832 						  decoder->tsc_ctc_ratio_d);
1833 	}
1834 	decoder->ctc_delta = 0;
1835 	decoder->have_tma = true;
1836 	decoder->fixup_last_mtc = true;
1837 	intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x  CTC rem %#x\n",
1838 		     decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1839 }
1840 
1841 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1842 {
1843 	uint64_t timestamp;
1844 	uint32_t mtc, mtc_delta;
1845 
1846 	if (!decoder->have_tma)
1847 		return;
1848 
1849 	mtc = decoder->packet.payload;
1850 
1851 	if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1852 		decoder->fixup_last_mtc = false;
1853 		intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1854 					&decoder->last_mtc);
1855 	}
1856 
1857 	if (mtc > decoder->last_mtc)
1858 		mtc_delta = mtc - decoder->last_mtc;
1859 	else
1860 		mtc_delta = mtc + 256 - decoder->last_mtc;
1861 
1862 	decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1863 
1864 	if (decoder->tsc_ctc_mult) {
1865 		timestamp = decoder->ctc_timestamp +
1866 			    decoder->ctc_delta * decoder->tsc_ctc_mult;
1867 	} else {
1868 		timestamp = decoder->ctc_timestamp +
1869 			    multdiv(decoder->ctc_delta,
1870 				    decoder->tsc_ctc_ratio_n,
1871 				    decoder->tsc_ctc_ratio_d);
1872 	}
1873 
1874 	if (timestamp < decoder->timestamp)
1875 		intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1876 			     timestamp, decoder->timestamp);
1877 	else
1878 		decoder->timestamp = timestamp;
1879 
1880 	intel_pt_mtc_cyc_cnt_upd(decoder);
1881 
1882 	decoder->timestamp_insn_cnt = 0;
1883 	decoder->last_mtc = mtc;
1884 
1885 	if (decoder->last_packet_type == INTEL_PT_CYC) {
1886 		decoder->cyc_ref_timestamp = decoder->timestamp;
1887 		decoder->cycle_cnt = 0;
1888 		decoder->have_calc_cyc_to_tsc = false;
1889 		intel_pt_calc_cyc_to_tsc(decoder, true);
1890 	}
1891 
1892 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1893 }
1894 
1895 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1896 {
1897 	unsigned int cbr = decoder->packet.payload & 0xff;
1898 
1899 	decoder->cbr_payload = decoder->packet.payload;
1900 
1901 	if (decoder->cbr == cbr)
1902 		return;
1903 
1904 	decoder->cbr = cbr;
1905 	decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1906 
1907 	intel_pt_mtc_cyc_cnt_cbr(decoder);
1908 }
1909 
1910 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1911 {
1912 	uint64_t timestamp = decoder->cyc_ref_timestamp;
1913 
1914 	decoder->have_cyc = true;
1915 
1916 	decoder->cycle_cnt += decoder->packet.payload;
1917 	if (decoder->pge)
1918 		decoder->tot_cyc_cnt += decoder->packet.payload;
1919 	decoder->sample_cyc = true;
1920 
1921 	if (!decoder->cyc_ref_timestamp)
1922 		return;
1923 
1924 	if (decoder->have_calc_cyc_to_tsc)
1925 		timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1926 	else if (decoder->cbr)
1927 		timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1928 	else
1929 		return;
1930 
1931 	if (timestamp < decoder->timestamp)
1932 		intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1933 			     timestamp, decoder->timestamp);
1934 	else
1935 		decoder->timestamp = timestamp;
1936 
1937 	decoder->timestamp_insn_cnt = 0;
1938 
1939 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1940 }
1941 
1942 static void intel_pt_bbp(struct intel_pt_decoder *decoder)
1943 {
1944 	if (decoder->prev_pkt_ctx == INTEL_PT_NO_CTX) {
1945 		memset(decoder->state.items.mask, 0, sizeof(decoder->state.items.mask));
1946 		decoder->state.items.is_32_bit = false;
1947 	}
1948 	decoder->blk_type = decoder->packet.payload;
1949 	decoder->blk_type_pos = intel_pt_blk_type_pos(decoder->blk_type);
1950 	if (decoder->blk_type == INTEL_PT_GP_REGS)
1951 		decoder->state.items.is_32_bit = decoder->packet.count;
1952 	if (decoder->blk_type_pos < 0) {
1953 		intel_pt_log("WARNING: Unknown block type %u\n",
1954 			     decoder->blk_type);
1955 	} else if (decoder->state.items.mask[decoder->blk_type_pos]) {
1956 		intel_pt_log("WARNING: Duplicate block type %u\n",
1957 			     decoder->blk_type);
1958 	}
1959 }
1960 
1961 static void intel_pt_bip(struct intel_pt_decoder *decoder)
1962 {
1963 	uint32_t id = decoder->packet.count;
1964 	uint32_t bit = 1 << id;
1965 	int pos = decoder->blk_type_pos;
1966 
1967 	if (pos < 0 || id >= INTEL_PT_BLK_ITEM_ID_CNT) {
1968 		intel_pt_log("WARNING: Unknown block item %u type %d\n",
1969 			     id, decoder->blk_type);
1970 		return;
1971 	}
1972 
1973 	if (decoder->state.items.mask[pos] & bit) {
1974 		intel_pt_log("WARNING: Duplicate block item %u type %d\n",
1975 			     id, decoder->blk_type);
1976 	}
1977 
1978 	decoder->state.items.mask[pos] |= bit;
1979 	decoder->state.items.val[pos][id] = decoder->packet.payload;
1980 }
1981 
1982 /* Walk PSB+ packets when already in sync. */
1983 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1984 {
1985 	int err;
1986 
1987 	decoder->in_psb = true;
1988 
1989 	while (1) {
1990 		err = intel_pt_get_next_packet(decoder);
1991 		if (err)
1992 			goto out;
1993 
1994 		switch (decoder->packet.type) {
1995 		case INTEL_PT_PSBEND:
1996 			err = 0;
1997 			goto out;
1998 
1999 		case INTEL_PT_TIP_PGD:
2000 		case INTEL_PT_TIP_PGE:
2001 		case INTEL_PT_TIP:
2002 		case INTEL_PT_TNT:
2003 		case INTEL_PT_TRACESTOP:
2004 		case INTEL_PT_BAD:
2005 		case INTEL_PT_PSB:
2006 		case INTEL_PT_PTWRITE:
2007 		case INTEL_PT_PTWRITE_IP:
2008 		case INTEL_PT_EXSTOP:
2009 		case INTEL_PT_EXSTOP_IP:
2010 		case INTEL_PT_MWAIT:
2011 		case INTEL_PT_PWRE:
2012 		case INTEL_PT_PWRX:
2013 		case INTEL_PT_BBP:
2014 		case INTEL_PT_BIP:
2015 		case INTEL_PT_BEP:
2016 		case INTEL_PT_BEP_IP:
2017 		case INTEL_PT_CFE:
2018 		case INTEL_PT_CFE_IP:
2019 		case INTEL_PT_EVD:
2020 			decoder->have_tma = false;
2021 			intel_pt_log("ERROR: Unexpected packet\n");
2022 			err = -EAGAIN;
2023 			goto out;
2024 
2025 		case INTEL_PT_OVF:
2026 			err = intel_pt_overflow(decoder);
2027 			goto out;
2028 
2029 		case INTEL_PT_TSC:
2030 			intel_pt_calc_tsc_timestamp(decoder);
2031 			break;
2032 
2033 		case INTEL_PT_TMA:
2034 			intel_pt_calc_tma(decoder);
2035 			break;
2036 
2037 		case INTEL_PT_CBR:
2038 			intel_pt_calc_cbr(decoder);
2039 			break;
2040 
2041 		case INTEL_PT_MODE_EXEC:
2042 			intel_pt_mode_exec_status(decoder);
2043 			break;
2044 
2045 		case INTEL_PT_PIP:
2046 			intel_pt_set_pip(decoder);
2047 			break;
2048 
2049 		case INTEL_PT_FUP:
2050 			decoder->pge = true;
2051 			if (decoder->packet.count) {
2052 				intel_pt_set_last_ip(decoder);
2053 				decoder->psb_ip = decoder->last_ip;
2054 			}
2055 			break;
2056 
2057 		case INTEL_PT_MODE_TSX:
2058 			intel_pt_update_in_tx(decoder);
2059 			break;
2060 
2061 		case INTEL_PT_MTC:
2062 			intel_pt_calc_mtc_timestamp(decoder);
2063 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
2064 				decoder->state.type |= INTEL_PT_INSTRUCTION;
2065 			break;
2066 
2067 		case INTEL_PT_CYC:
2068 			intel_pt_calc_cyc_timestamp(decoder);
2069 			break;
2070 
2071 		case INTEL_PT_VMCS:
2072 		case INTEL_PT_MNT:
2073 		case INTEL_PT_PAD:
2074 		default:
2075 			break;
2076 		}
2077 	}
2078 out:
2079 	decoder->in_psb = false;
2080 
2081 	return err;
2082 }
2083 
2084 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
2085 {
2086 	int err;
2087 
2088 	if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
2089 		decoder->tx_flags = 0;
2090 		decoder->state.flags &= ~INTEL_PT_IN_TX;
2091 		decoder->state.flags |= INTEL_PT_ABORT_TX;
2092 	} else {
2093 		decoder->state.flags |= INTEL_PT_ASYNC;
2094 	}
2095 
2096 	while (1) {
2097 		err = intel_pt_get_next_packet(decoder);
2098 		if (err)
2099 			return err;
2100 
2101 		switch (decoder->packet.type) {
2102 		case INTEL_PT_TNT:
2103 		case INTEL_PT_FUP:
2104 		case INTEL_PT_TRACESTOP:
2105 		case INTEL_PT_PSB:
2106 		case INTEL_PT_TSC:
2107 		case INTEL_PT_TMA:
2108 		case INTEL_PT_MODE_TSX:
2109 		case INTEL_PT_BAD:
2110 		case INTEL_PT_PSBEND:
2111 		case INTEL_PT_PTWRITE:
2112 		case INTEL_PT_PTWRITE_IP:
2113 		case INTEL_PT_EXSTOP:
2114 		case INTEL_PT_EXSTOP_IP:
2115 		case INTEL_PT_MWAIT:
2116 		case INTEL_PT_PWRE:
2117 		case INTEL_PT_PWRX:
2118 		case INTEL_PT_BBP:
2119 		case INTEL_PT_BIP:
2120 		case INTEL_PT_BEP:
2121 		case INTEL_PT_BEP_IP:
2122 		case INTEL_PT_CFE:
2123 		case INTEL_PT_CFE_IP:
2124 		case INTEL_PT_EVD:
2125 			intel_pt_log("ERROR: Missing TIP after FUP\n");
2126 			decoder->pkt_state = INTEL_PT_STATE_ERR3;
2127 			decoder->pkt_step = 0;
2128 			return -ENOENT;
2129 
2130 		case INTEL_PT_CBR:
2131 			intel_pt_calc_cbr(decoder);
2132 			break;
2133 
2134 		case INTEL_PT_OVF:
2135 			return intel_pt_overflow(decoder);
2136 
2137 		case INTEL_PT_TIP_PGD:
2138 			decoder->state.from_ip = decoder->ip;
2139 			if (decoder->packet.count == 0) {
2140 				decoder->state.to_ip = 0;
2141 			} else {
2142 				intel_pt_set_ip(decoder);
2143 				decoder->state.to_ip = decoder->ip;
2144 			}
2145 			decoder->pge = false;
2146 			decoder->continuous_period = false;
2147 			decoder->state.type |= INTEL_PT_TRACE_END;
2148 			intel_pt_update_nr(decoder);
2149 			return 0;
2150 
2151 		case INTEL_PT_TIP_PGE:
2152 			decoder->pge = true;
2153 			intel_pt_log("Omitting PGE ip " x64_fmt "\n",
2154 				     decoder->ip);
2155 			decoder->state.from_ip = 0;
2156 			if (decoder->packet.count == 0) {
2157 				decoder->state.to_ip = 0;
2158 			} else {
2159 				intel_pt_set_ip(decoder);
2160 				decoder->state.to_ip = decoder->ip;
2161 			}
2162 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2163 			intel_pt_mtc_cyc_cnt_pge(decoder);
2164 			intel_pt_set_nr(decoder);
2165 			return 0;
2166 
2167 		case INTEL_PT_TIP:
2168 			decoder->state.from_ip = decoder->ip;
2169 			if (decoder->packet.count == 0) {
2170 				decoder->state.to_ip = 0;
2171 			} else {
2172 				intel_pt_set_ip(decoder);
2173 				decoder->state.to_ip = decoder->ip;
2174 			}
2175 			intel_pt_update_nr(decoder);
2176 			intel_pt_sample_iflag_chg(decoder);
2177 			return 0;
2178 
2179 		case INTEL_PT_PIP:
2180 			intel_pt_update_pip(decoder);
2181 			break;
2182 
2183 		case INTEL_PT_MTC:
2184 			intel_pt_calc_mtc_timestamp(decoder);
2185 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
2186 				decoder->state.type |= INTEL_PT_INSTRUCTION;
2187 			break;
2188 
2189 		case INTEL_PT_CYC:
2190 			intel_pt_calc_cyc_timestamp(decoder);
2191 			break;
2192 
2193 		case INTEL_PT_MODE_EXEC:
2194 			intel_pt_mode_exec(decoder);
2195 			break;
2196 
2197 		case INTEL_PT_VMCS:
2198 		case INTEL_PT_MNT:
2199 		case INTEL_PT_PAD:
2200 			break;
2201 
2202 		default:
2203 			return intel_pt_bug(decoder);
2204 		}
2205 	}
2206 }
2207 
2208 static int intel_pt_resample(struct intel_pt_decoder *decoder)
2209 {
2210 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2211 	decoder->state.type = INTEL_PT_INSTRUCTION;
2212 	decoder->state.from_ip = decoder->ip;
2213 	decoder->state.to_ip = 0;
2214 	return 0;
2215 }
2216 
2217 struct intel_pt_vm_tsc_info {
2218 	struct intel_pt_pkt pip_packet;
2219 	struct intel_pt_pkt vmcs_packet;
2220 	struct intel_pt_pkt tma_packet;
2221 	bool tsc, pip, vmcs, tma, psbend;
2222 	uint64_t ctc_delta;
2223 	uint64_t last_ctc;
2224 	int max_lookahead;
2225 };
2226 
2227 /* Lookahead and get the PIP, VMCS and TMA packets from PSB+ */
2228 static int intel_pt_vm_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2229 {
2230 	struct intel_pt_vm_tsc_info *data = pkt_info->data;
2231 
2232 	switch (pkt_info->packet.type) {
2233 	case INTEL_PT_PAD:
2234 	case INTEL_PT_MNT:
2235 	case INTEL_PT_MODE_EXEC:
2236 	case INTEL_PT_MODE_TSX:
2237 	case INTEL_PT_MTC:
2238 	case INTEL_PT_FUP:
2239 	case INTEL_PT_CYC:
2240 	case INTEL_PT_CBR:
2241 		break;
2242 
2243 	case INTEL_PT_TSC:
2244 		data->tsc = true;
2245 		break;
2246 
2247 	case INTEL_PT_TMA:
2248 		data->tma_packet = pkt_info->packet;
2249 		data->tma = true;
2250 		break;
2251 
2252 	case INTEL_PT_PIP:
2253 		data->pip_packet = pkt_info->packet;
2254 		data->pip = true;
2255 		break;
2256 
2257 	case INTEL_PT_VMCS:
2258 		data->vmcs_packet = pkt_info->packet;
2259 		data->vmcs = true;
2260 		break;
2261 
2262 	case INTEL_PT_PSBEND:
2263 		data->psbend = true;
2264 		return 1;
2265 
2266 	case INTEL_PT_TIP_PGE:
2267 	case INTEL_PT_PTWRITE:
2268 	case INTEL_PT_PTWRITE_IP:
2269 	case INTEL_PT_EXSTOP:
2270 	case INTEL_PT_EXSTOP_IP:
2271 	case INTEL_PT_MWAIT:
2272 	case INTEL_PT_PWRE:
2273 	case INTEL_PT_PWRX:
2274 	case INTEL_PT_BBP:
2275 	case INTEL_PT_BIP:
2276 	case INTEL_PT_BEP:
2277 	case INTEL_PT_BEP_IP:
2278 	case INTEL_PT_OVF:
2279 	case INTEL_PT_BAD:
2280 	case INTEL_PT_TNT:
2281 	case INTEL_PT_TIP_PGD:
2282 	case INTEL_PT_TIP:
2283 	case INTEL_PT_PSB:
2284 	case INTEL_PT_TRACESTOP:
2285 	case INTEL_PT_CFE:
2286 	case INTEL_PT_CFE_IP:
2287 	case INTEL_PT_EVD:
2288 	default:
2289 		return 1;
2290 	}
2291 
2292 	return 0;
2293 }
2294 
2295 struct intel_pt_ovf_fup_info {
2296 	int max_lookahead;
2297 	bool found;
2298 };
2299 
2300 /* Lookahead to detect a FUP packet after OVF */
2301 static int intel_pt_ovf_fup_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2302 {
2303 	struct intel_pt_ovf_fup_info *data = pkt_info->data;
2304 
2305 	if (pkt_info->packet.type == INTEL_PT_CYC ||
2306 	    pkt_info->packet.type == INTEL_PT_MTC ||
2307 	    pkt_info->packet.type == INTEL_PT_TSC)
2308 		return !--(data->max_lookahead);
2309 	data->found = pkt_info->packet.type == INTEL_PT_FUP;
2310 	return 1;
2311 }
2312 
2313 static bool intel_pt_ovf_fup_lookahead(struct intel_pt_decoder *decoder)
2314 {
2315 	struct intel_pt_ovf_fup_info data = {
2316 		.max_lookahead = 16,
2317 		.found = false,
2318 	};
2319 
2320 	intel_pt_pkt_lookahead(decoder, intel_pt_ovf_fup_lookahead_cb, &data);
2321 	return data.found;
2322 }
2323 
2324 /* Lookahead and get the TMA packet after TSC */
2325 static int intel_pt_tma_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2326 {
2327 	struct intel_pt_vm_tsc_info *data = pkt_info->data;
2328 
2329 	if (pkt_info->packet.type == INTEL_PT_CYC ||
2330 	    pkt_info->packet.type == INTEL_PT_MTC)
2331 		return !--(data->max_lookahead);
2332 
2333 	if (pkt_info->packet.type == INTEL_PT_TMA) {
2334 		data->tma_packet = pkt_info->packet;
2335 		data->tma = true;
2336 	}
2337 	return 1;
2338 }
2339 
2340 static uint64_t intel_pt_ctc_to_tsc(struct intel_pt_decoder *decoder, uint64_t ctc)
2341 {
2342 	if (decoder->tsc_ctc_mult)
2343 		return ctc * decoder->tsc_ctc_mult;
2344 	else
2345 		return multdiv(ctc, decoder->tsc_ctc_ratio_n, decoder->tsc_ctc_ratio_d);
2346 }
2347 
2348 static uint64_t intel_pt_calc_expected_tsc(struct intel_pt_decoder *decoder,
2349 					   uint32_t ctc,
2350 					   uint32_t fc,
2351 					   uint64_t last_ctc_timestamp,
2352 					   uint64_t ctc_delta,
2353 					   uint32_t last_ctc)
2354 {
2355 	/* Number of CTC ticks from last_ctc_timestamp to last_mtc */
2356 	uint64_t last_mtc_ctc = last_ctc + ctc_delta;
2357 	/*
2358 	 * Number of CTC ticks from there until current TMA packet. We would
2359 	 * expect last_mtc_ctc to be before ctc, but the TSC packet can slip
2360 	 * past an MTC, so a sign-extended value is used.
2361 	 */
2362 	uint64_t delta = (int16_t)((uint16_t)ctc - (uint16_t)last_mtc_ctc);
2363 	/* Total CTC ticks from last_ctc_timestamp to current TMA packet */
2364 	uint64_t new_ctc_delta = ctc_delta + delta;
2365 	uint64_t expected_tsc;
2366 
2367 	/*
2368 	 * Convert CTC ticks to TSC ticks, add the starting point
2369 	 * (last_ctc_timestamp) and the fast counter from the TMA packet.
2370 	 */
2371 	expected_tsc = last_ctc_timestamp + intel_pt_ctc_to_tsc(decoder, new_ctc_delta) + fc;
2372 
2373 	if (intel_pt_enable_logging) {
2374 		intel_pt_log_x64(last_mtc_ctc);
2375 		intel_pt_log_x32(last_ctc);
2376 		intel_pt_log_x64(ctc_delta);
2377 		intel_pt_log_x64(delta);
2378 		intel_pt_log_x32(ctc);
2379 		intel_pt_log_x64(new_ctc_delta);
2380 		intel_pt_log_x64(last_ctc_timestamp);
2381 		intel_pt_log_x32(fc);
2382 		intel_pt_log_x64(intel_pt_ctc_to_tsc(decoder, new_ctc_delta));
2383 		intel_pt_log_x64(expected_tsc);
2384 	}
2385 
2386 	return expected_tsc;
2387 }
2388 
2389 static uint64_t intel_pt_expected_tsc(struct intel_pt_decoder *decoder,
2390 				      struct intel_pt_vm_tsc_info *data)
2391 {
2392 	uint32_t ctc = data->tma_packet.payload;
2393 	uint32_t fc = data->tma_packet.count;
2394 
2395 	return intel_pt_calc_expected_tsc(decoder, ctc, fc,
2396 					  decoder->ctc_timestamp,
2397 					  data->ctc_delta, data->last_ctc);
2398 }
2399 
2400 static void intel_pt_translate_vm_tsc(struct intel_pt_decoder *decoder,
2401 				      struct intel_pt_vmcs_info *vmcs_info)
2402 {
2403 	uint64_t payload = decoder->packet.payload;
2404 
2405 	/* VMX adds the TSC Offset, so subtract to get host TSC */
2406 	decoder->packet.payload -= vmcs_info->tsc_offset;
2407 	/* TSC packet has only 7 bytes */
2408 	decoder->packet.payload &= SEVEN_BYTES;
2409 
2410 	/*
2411 	 * The buffer is mmapped from the data file, so this also updates the
2412 	 * data file.
2413 	 */
2414 	if (!decoder->vm_tm_corr_dry_run)
2415 		memcpy((void *)decoder->buf + 1, &decoder->packet.payload, 7);
2416 
2417 	intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
2418 		     "    VMCS %#" PRIx64 "    TSC Offset %#" PRIx64 "\n",
2419 		     payload, decoder->packet.payload, vmcs_info->vmcs,
2420 		     vmcs_info->tsc_offset);
2421 }
2422 
2423 static void intel_pt_translate_vm_tsc_offset(struct intel_pt_decoder *decoder,
2424 					     uint64_t tsc_offset)
2425 {
2426 	struct intel_pt_vmcs_info vmcs_info = {
2427 		.vmcs = NO_VMCS,
2428 		.tsc_offset = tsc_offset
2429 	};
2430 
2431 	intel_pt_translate_vm_tsc(decoder, &vmcs_info);
2432 }
2433 
2434 static inline bool in_vm(uint64_t pip_payload)
2435 {
2436 	return pip_payload & 1;
2437 }
2438 
2439 static inline bool pip_in_vm(struct intel_pt_pkt *pip_packet)
2440 {
2441 	return pip_packet->payload & 1;
2442 }
2443 
2444 static void intel_pt_print_vmcs_info(struct intel_pt_vmcs_info *vmcs_info)
2445 {
2446 	p_log("VMCS: %#" PRIx64 "  TSC Offset %#" PRIx64,
2447 	      vmcs_info->vmcs, vmcs_info->tsc_offset);
2448 }
2449 
2450 static void intel_pt_vm_tm_corr_psb(struct intel_pt_decoder *decoder,
2451 				    struct intel_pt_vm_tsc_info *data)
2452 {
2453 	memset(data, 0, sizeof(*data));
2454 	data->ctc_delta = decoder->ctc_delta;
2455 	data->last_ctc = decoder->last_ctc;
2456 	intel_pt_pkt_lookahead(decoder, intel_pt_vm_psb_lookahead_cb, data);
2457 	if (data->tsc && !data->psbend)
2458 		p_log("ERROR: PSB without PSBEND");
2459 	decoder->in_psb = data->psbend;
2460 }
2461 
2462 static void intel_pt_vm_tm_corr_first_tsc(struct intel_pt_decoder *decoder,
2463 					  struct intel_pt_vm_tsc_info *data,
2464 					  struct intel_pt_vmcs_info *vmcs_info,
2465 					  uint64_t host_tsc)
2466 {
2467 	if (!decoder->in_psb) {
2468 		/* Can't happen */
2469 		p_log("ERROR: First TSC is not in PSB+");
2470 	}
2471 
2472 	if (data->pip) {
2473 		if (pip_in_vm(&data->pip_packet)) { /* Guest */
2474 			if (vmcs_info && vmcs_info->tsc_offset) {
2475 				intel_pt_translate_vm_tsc(decoder, vmcs_info);
2476 				decoder->vm_tm_corr_reliable = true;
2477 			} else {
2478 				p_log("ERROR: First TSC, unknown TSC Offset");
2479 			}
2480 		} else { /* Host */
2481 			decoder->vm_tm_corr_reliable = true;
2482 		}
2483 	} else { /* Host or Guest */
2484 		decoder->vm_tm_corr_reliable = false;
2485 		if (intel_pt_time_in_range(decoder, host_tsc)) {
2486 			/* Assume Host */
2487 		} else {
2488 			/* Assume Guest */
2489 			if (vmcs_info && vmcs_info->tsc_offset)
2490 				intel_pt_translate_vm_tsc(decoder, vmcs_info);
2491 			else
2492 				p_log("ERROR: First TSC, no PIP, unknown TSC Offset");
2493 		}
2494 	}
2495 }
2496 
2497 static void intel_pt_vm_tm_corr_tsc(struct intel_pt_decoder *decoder,
2498 				    struct intel_pt_vm_tsc_info *data)
2499 {
2500 	struct intel_pt_vmcs_info *vmcs_info;
2501 	uint64_t tsc_offset = 0;
2502 	uint64_t vmcs;
2503 	bool reliable = true;
2504 	uint64_t expected_tsc;
2505 	uint64_t host_tsc;
2506 	uint64_t ref_timestamp;
2507 
2508 	bool assign = false;
2509 	bool assign_reliable = false;
2510 
2511 	/* Already have 'data' for the in_psb case */
2512 	if (!decoder->in_psb) {
2513 		memset(data, 0, sizeof(*data));
2514 		data->ctc_delta = decoder->ctc_delta;
2515 		data->last_ctc = decoder->last_ctc;
2516 		data->max_lookahead = 16;
2517 		intel_pt_pkt_lookahead(decoder, intel_pt_tma_lookahead_cb, data);
2518 		if (decoder->pge) {
2519 			data->pip = true;
2520 			data->pip_packet.payload = decoder->pip_payload;
2521 		}
2522 	}
2523 
2524 	/* Calculations depend on having TMA packets */
2525 	if (!data->tma) {
2526 		p_log("ERROR: TSC without TMA");
2527 		return;
2528 	}
2529 
2530 	vmcs = data->vmcs ? data->vmcs_packet.payload : decoder->vmcs;
2531 	if (vmcs == NO_VMCS)
2532 		vmcs = 0;
2533 
2534 	vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
2535 
2536 	ref_timestamp = decoder->timestamp ? decoder->timestamp : decoder->buf_timestamp;
2537 	host_tsc = intel_pt_8b_tsc(decoder->packet.payload, ref_timestamp);
2538 
2539 	if (!decoder->ctc_timestamp) {
2540 		intel_pt_vm_tm_corr_first_tsc(decoder, data, vmcs_info, host_tsc);
2541 		return;
2542 	}
2543 
2544 	expected_tsc = intel_pt_expected_tsc(decoder, data);
2545 
2546 	tsc_offset = host_tsc - expected_tsc;
2547 
2548 	/* Determine if TSC is from Host or Guest */
2549 	if (data->pip) {
2550 		if (pip_in_vm(&data->pip_packet)) { /* Guest */
2551 			if (!vmcs_info) {
2552 				/* PIP NR=1 without VMCS cannot happen */
2553 				p_log("ERROR: Missing VMCS");
2554 				intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
2555 				decoder->vm_tm_corr_reliable = false;
2556 				return;
2557 			}
2558 		} else { /* Host */
2559 			decoder->last_reliable_timestamp = host_tsc;
2560 			decoder->vm_tm_corr_reliable = true;
2561 			return;
2562 		}
2563 	} else { /* Host or Guest */
2564 		reliable = false; /* Host/Guest is a guess, so not reliable */
2565 		if (decoder->in_psb) {
2566 			if (!tsc_offset)
2567 				return; /* Zero TSC Offset, assume Host */
2568 			/*
2569 			 * TSC packet has only 7 bytes of TSC. We have no
2570 			 * information about the Guest's 8th byte, but it
2571 			 * doesn't matter because we only need 7 bytes.
2572 			 * Here, since the 8th byte is unreliable and
2573 			 * irrelevant, compare only 7 byes.
2574 			 */
2575 			if (vmcs_info &&
2576 			    (tsc_offset & SEVEN_BYTES) ==
2577 			    (vmcs_info->tsc_offset & SEVEN_BYTES)) {
2578 				/* Same TSC Offset as last VMCS, assume Guest */
2579 				goto guest;
2580 			}
2581 		}
2582 		/*
2583 		 * Check if the host_tsc is within the expected range.
2584 		 * Note, we could narrow the range more by looking ahead for
2585 		 * the next host TSC in the same buffer, but we don't bother to
2586 		 * do that because this is probably good enough.
2587 		 */
2588 		if (host_tsc >= expected_tsc && intel_pt_time_in_range(decoder, host_tsc)) {
2589 			/* Within expected range for Host TSC, assume Host */
2590 			decoder->vm_tm_corr_reliable = false;
2591 			return;
2592 		}
2593 	}
2594 
2595 guest: /* Assuming Guest */
2596 
2597 	/* Determine whether to assign TSC Offset */
2598 	if (vmcs_info && vmcs_info->vmcs) {
2599 		if (vmcs_info->tsc_offset && vmcs_info->reliable) {
2600 			assign = false;
2601 		} else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_reliable &&
2602 			   decoder->vm_tm_corr_continuous && decoder->vm_tm_corr_same_buf) {
2603 			/* Continuous tracing, TSC in a PSB is not a time loss */
2604 			assign = true;
2605 			assign_reliable = true;
2606 		} else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_same_buf) {
2607 			/*
2608 			 * Unlikely to be a time loss TSC in a PSB which is not
2609 			 * at the start of a buffer.
2610 			 */
2611 			assign = true;
2612 			assign_reliable = false;
2613 		}
2614 	}
2615 
2616 	/* Record VMCS TSC Offset */
2617 	if (assign && (vmcs_info->tsc_offset != tsc_offset ||
2618 		       vmcs_info->reliable != assign_reliable)) {
2619 		bool print = vmcs_info->tsc_offset != tsc_offset;
2620 
2621 		vmcs_info->tsc_offset = tsc_offset;
2622 		vmcs_info->reliable = assign_reliable;
2623 		if (print)
2624 			intel_pt_print_vmcs_info(vmcs_info);
2625 	}
2626 
2627 	/* Determine what TSC Offset to use */
2628 	if (vmcs_info && vmcs_info->tsc_offset) {
2629 		if (!vmcs_info->reliable)
2630 			reliable = false;
2631 		intel_pt_translate_vm_tsc(decoder, vmcs_info);
2632 	} else {
2633 		reliable = false;
2634 		if (vmcs_info) {
2635 			if (!vmcs_info->error_printed) {
2636 				p_log("ERROR: Unknown TSC Offset for VMCS %#" PRIx64,
2637 				      vmcs_info->vmcs);
2638 				vmcs_info->error_printed = true;
2639 			}
2640 		} else {
2641 			if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
2642 				p_log("ERROR: Unknown VMCS");
2643 		}
2644 		intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
2645 	}
2646 
2647 	decoder->vm_tm_corr_reliable = reliable;
2648 }
2649 
2650 static void intel_pt_vm_tm_corr_pebs_tsc(struct intel_pt_decoder *decoder)
2651 {
2652 	uint64_t host_tsc = decoder->packet.payload;
2653 	uint64_t guest_tsc = decoder->packet.payload;
2654 	struct intel_pt_vmcs_info *vmcs_info;
2655 	uint64_t vmcs;
2656 
2657 	vmcs = decoder->vmcs;
2658 	if (vmcs == NO_VMCS)
2659 		vmcs = 0;
2660 
2661 	vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
2662 
2663 	if (decoder->pge) {
2664 		if (in_vm(decoder->pip_payload)) { /* Guest */
2665 			if (!vmcs_info) {
2666 				/* PIP NR=1 without VMCS cannot happen */
2667 				p_log("ERROR: Missing VMCS");
2668 			}
2669 		} else { /* Host */
2670 			return;
2671 		}
2672 	} else { /* Host or Guest */
2673 		if (intel_pt_time_in_range(decoder, host_tsc)) {
2674 			/* Within expected range for Host TSC, assume Host */
2675 			return;
2676 		}
2677 	}
2678 
2679 	if (vmcs_info) {
2680 		/* Translate Guest TSC to Host TSC */
2681 		host_tsc = ((guest_tsc & SEVEN_BYTES) - vmcs_info->tsc_offset) & SEVEN_BYTES;
2682 		host_tsc = intel_pt_8b_tsc(host_tsc, decoder->timestamp);
2683 		intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
2684 			     "    VMCS %#" PRIx64 "    TSC Offset %#" PRIx64 "\n",
2685 			     guest_tsc, host_tsc, vmcs_info->vmcs,
2686 			     vmcs_info->tsc_offset);
2687 		if (!intel_pt_time_in_range(decoder, host_tsc) &&
2688 		    intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
2689 			p_log("Timestamp out of range");
2690 	} else {
2691 		if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
2692 			p_log("ERROR: Unknown VMCS");
2693 		host_tsc = decoder->timestamp;
2694 	}
2695 
2696 	decoder->packet.payload = host_tsc;
2697 
2698 	if (!decoder->vm_tm_corr_dry_run)
2699 		memcpy((void *)decoder->buf + 1, &host_tsc, 8);
2700 }
2701 
2702 static int intel_pt_vm_time_correlation(struct intel_pt_decoder *decoder)
2703 {
2704 	struct intel_pt_vm_tsc_info data = { .psbend = false };
2705 	bool pge;
2706 	int err;
2707 
2708 	if (decoder->in_psb)
2709 		intel_pt_vm_tm_corr_psb(decoder, &data);
2710 
2711 	while (1) {
2712 		err = intel_pt_get_next_packet(decoder);
2713 		if (err == -ENOLINK)
2714 			continue;
2715 		if (err)
2716 			break;
2717 
2718 		switch (decoder->packet.type) {
2719 		case INTEL_PT_TIP_PGD:
2720 			decoder->pge = false;
2721 			decoder->vm_tm_corr_continuous = false;
2722 			break;
2723 
2724 		case INTEL_PT_TNT:
2725 		case INTEL_PT_TIP:
2726 		case INTEL_PT_TIP_PGE:
2727 			decoder->pge = true;
2728 			break;
2729 
2730 		case INTEL_PT_OVF:
2731 			decoder->in_psb = false;
2732 			pge = decoder->pge;
2733 			decoder->pge = intel_pt_ovf_fup_lookahead(decoder);
2734 			if (pge != decoder->pge)
2735 				intel_pt_log("Surprising PGE change in OVF!");
2736 			if (!decoder->pge)
2737 				decoder->vm_tm_corr_continuous = false;
2738 			break;
2739 
2740 		case INTEL_PT_FUP:
2741 			if (decoder->in_psb)
2742 				decoder->pge = true;
2743 			break;
2744 
2745 		case INTEL_PT_TRACESTOP:
2746 			decoder->pge = false;
2747 			decoder->vm_tm_corr_continuous = false;
2748 			decoder->have_tma = false;
2749 			break;
2750 
2751 		case INTEL_PT_PSB:
2752 			intel_pt_vm_tm_corr_psb(decoder, &data);
2753 			break;
2754 
2755 		case INTEL_PT_PIP:
2756 			decoder->pip_payload = decoder->packet.payload;
2757 			break;
2758 
2759 		case INTEL_PT_MTC:
2760 			intel_pt_calc_mtc_timestamp(decoder);
2761 			break;
2762 
2763 		case INTEL_PT_TSC:
2764 			intel_pt_vm_tm_corr_tsc(decoder, &data);
2765 			intel_pt_calc_tsc_timestamp(decoder);
2766 			decoder->vm_tm_corr_same_buf = true;
2767 			decoder->vm_tm_corr_continuous = decoder->pge;
2768 			break;
2769 
2770 		case INTEL_PT_TMA:
2771 			intel_pt_calc_tma(decoder);
2772 			break;
2773 
2774 		case INTEL_PT_CYC:
2775 			intel_pt_calc_cyc_timestamp(decoder);
2776 			break;
2777 
2778 		case INTEL_PT_CBR:
2779 			intel_pt_calc_cbr(decoder);
2780 			break;
2781 
2782 		case INTEL_PT_PSBEND:
2783 			decoder->in_psb = false;
2784 			data.psbend = false;
2785 			break;
2786 
2787 		case INTEL_PT_VMCS:
2788 			if (decoder->packet.payload != NO_VMCS)
2789 				decoder->vmcs = decoder->packet.payload;
2790 			break;
2791 
2792 		case INTEL_PT_BBP:
2793 			decoder->blk_type = decoder->packet.payload;
2794 			break;
2795 
2796 		case INTEL_PT_BIP:
2797 			if (decoder->blk_type == INTEL_PT_PEBS_BASIC &&
2798 			    decoder->packet.count == 2)
2799 				intel_pt_vm_tm_corr_pebs_tsc(decoder);
2800 			break;
2801 
2802 		case INTEL_PT_BEP:
2803 		case INTEL_PT_BEP_IP:
2804 			decoder->blk_type = 0;
2805 			break;
2806 
2807 		case INTEL_PT_CFE:
2808 		case INTEL_PT_CFE_IP:
2809 		case INTEL_PT_EVD:
2810 		case INTEL_PT_MODE_EXEC:
2811 		case INTEL_PT_MODE_TSX:
2812 		case INTEL_PT_MNT:
2813 		case INTEL_PT_PAD:
2814 		case INTEL_PT_PTWRITE_IP:
2815 		case INTEL_PT_PTWRITE:
2816 		case INTEL_PT_MWAIT:
2817 		case INTEL_PT_PWRE:
2818 		case INTEL_PT_EXSTOP_IP:
2819 		case INTEL_PT_EXSTOP:
2820 		case INTEL_PT_PWRX:
2821 		case INTEL_PT_BAD: /* Does not happen */
2822 		default:
2823 			break;
2824 		}
2825 	}
2826 
2827 	return err;
2828 }
2829 
2830 #define HOP_PROCESS	0
2831 #define HOP_IGNORE	1
2832 #define HOP_RETURN	2
2833 #define HOP_AGAIN	3
2834 
2835 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder);
2836 
2837 /* Hop mode: Ignore TNT, do not walk code, but get ip from FUPs and TIPs */
2838 static int intel_pt_hop_trace(struct intel_pt_decoder *decoder, bool *no_tip, int *err)
2839 {
2840 	*err = 0;
2841 
2842 	/* Leap from PSB to PSB, getting ip from FUP within PSB+ */
2843 	if (decoder->leap && !decoder->in_psb && decoder->packet.type != INTEL_PT_PSB) {
2844 		*err = intel_pt_scan_for_psb(decoder);
2845 		if (*err)
2846 			return HOP_RETURN;
2847 	}
2848 
2849 	switch (decoder->packet.type) {
2850 	case INTEL_PT_TNT:
2851 		return HOP_IGNORE;
2852 
2853 	case INTEL_PT_TIP_PGD:
2854 		decoder->pge = false;
2855 		if (!decoder->packet.count) {
2856 			intel_pt_set_nr(decoder);
2857 			return HOP_IGNORE;
2858 		}
2859 		intel_pt_set_ip(decoder);
2860 		decoder->state.type |= INTEL_PT_TRACE_END;
2861 		decoder->state.from_ip = 0;
2862 		decoder->state.to_ip = decoder->ip;
2863 		intel_pt_update_nr(decoder);
2864 		return HOP_RETURN;
2865 
2866 	case INTEL_PT_TIP:
2867 		if (!decoder->packet.count) {
2868 			intel_pt_set_nr(decoder);
2869 			return HOP_IGNORE;
2870 		}
2871 		intel_pt_set_ip(decoder);
2872 		decoder->state.type = INTEL_PT_INSTRUCTION;
2873 		decoder->state.from_ip = decoder->ip;
2874 		decoder->state.to_ip = 0;
2875 		intel_pt_update_nr(decoder);
2876 		intel_pt_sample_iflag_chg(decoder);
2877 		return HOP_RETURN;
2878 
2879 	case INTEL_PT_FUP:
2880 		if (!decoder->packet.count)
2881 			return HOP_IGNORE;
2882 		intel_pt_set_ip(decoder);
2883 		if (decoder->set_fup_mwait || decoder->set_fup_pwre)
2884 			*no_tip = true;
2885 		if (!decoder->branch_enable || !decoder->pge)
2886 			*no_tip = true;
2887 		if (*no_tip) {
2888 			decoder->state.type = INTEL_PT_INSTRUCTION;
2889 			decoder->state.from_ip = decoder->ip;
2890 			decoder->state.to_ip = 0;
2891 			intel_pt_fup_event(decoder, *no_tip);
2892 			return HOP_RETURN;
2893 		}
2894 		intel_pt_fup_event(decoder, *no_tip);
2895 		decoder->state.type |= INTEL_PT_INSTRUCTION | INTEL_PT_BRANCH;
2896 		*err = intel_pt_walk_fup_tip(decoder);
2897 		if (!*err && decoder->state.to_ip)
2898 			decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2899 		return HOP_RETURN;
2900 
2901 	case INTEL_PT_PSB:
2902 		decoder->state.psb_offset = decoder->pos;
2903 		decoder->psb_ip = 0;
2904 		decoder->last_ip = 0;
2905 		decoder->have_last_ip = true;
2906 		*err = intel_pt_walk_psbend(decoder);
2907 		if (*err == -EAGAIN)
2908 			return HOP_AGAIN;
2909 		if (*err)
2910 			return HOP_RETURN;
2911 		decoder->state.type = INTEL_PT_PSB_EVT;
2912 		if (decoder->psb_ip) {
2913 			decoder->state.type |= INTEL_PT_INSTRUCTION;
2914 			decoder->ip = decoder->psb_ip;
2915 		}
2916 		decoder->state.from_ip = decoder->psb_ip;
2917 		decoder->state.to_ip = 0;
2918 		return HOP_RETURN;
2919 
2920 	case INTEL_PT_BAD:
2921 	case INTEL_PT_PAD:
2922 	case INTEL_PT_TIP_PGE:
2923 	case INTEL_PT_TSC:
2924 	case INTEL_PT_TMA:
2925 	case INTEL_PT_MODE_EXEC:
2926 	case INTEL_PT_MODE_TSX:
2927 	case INTEL_PT_MTC:
2928 	case INTEL_PT_CYC:
2929 	case INTEL_PT_VMCS:
2930 	case INTEL_PT_PSBEND:
2931 	case INTEL_PT_CBR:
2932 	case INTEL_PT_TRACESTOP:
2933 	case INTEL_PT_PIP:
2934 	case INTEL_PT_OVF:
2935 	case INTEL_PT_MNT:
2936 	case INTEL_PT_PTWRITE:
2937 	case INTEL_PT_PTWRITE_IP:
2938 	case INTEL_PT_EXSTOP:
2939 	case INTEL_PT_EXSTOP_IP:
2940 	case INTEL_PT_MWAIT:
2941 	case INTEL_PT_PWRE:
2942 	case INTEL_PT_PWRX:
2943 	case INTEL_PT_BBP:
2944 	case INTEL_PT_BIP:
2945 	case INTEL_PT_BEP:
2946 	case INTEL_PT_BEP_IP:
2947 	case INTEL_PT_CFE:
2948 	case INTEL_PT_CFE_IP:
2949 	case INTEL_PT_EVD:
2950 	default:
2951 		return HOP_PROCESS;
2952 	}
2953 }
2954 
2955 struct intel_pt_psb_info {
2956 	struct intel_pt_pkt fup_packet;
2957 	bool fup;
2958 	int after_psbend;
2959 };
2960 
2961 /* Lookahead and get the FUP packet from PSB+ */
2962 static int intel_pt_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
2963 {
2964 	struct intel_pt_psb_info *data = pkt_info->data;
2965 
2966 	switch (pkt_info->packet.type) {
2967 	case INTEL_PT_PAD:
2968 	case INTEL_PT_MNT:
2969 	case INTEL_PT_TSC:
2970 	case INTEL_PT_TMA:
2971 	case INTEL_PT_MODE_EXEC:
2972 	case INTEL_PT_MODE_TSX:
2973 	case INTEL_PT_MTC:
2974 	case INTEL_PT_CYC:
2975 	case INTEL_PT_VMCS:
2976 	case INTEL_PT_CBR:
2977 	case INTEL_PT_PIP:
2978 		if (data->after_psbend) {
2979 			data->after_psbend -= 1;
2980 			if (!data->after_psbend)
2981 				return 1;
2982 		}
2983 		break;
2984 
2985 	case INTEL_PT_FUP:
2986 		if (data->after_psbend)
2987 			return 1;
2988 		if (data->fup || pkt_info->packet.count == 0)
2989 			return 1;
2990 		data->fup_packet = pkt_info->packet;
2991 		data->fup = true;
2992 		break;
2993 
2994 	case INTEL_PT_PSBEND:
2995 		if (!data->fup)
2996 			return 1;
2997 		/* Keep going to check for a TIP.PGE */
2998 		data->after_psbend = 6;
2999 		break;
3000 
3001 	case INTEL_PT_TIP_PGE:
3002 		/* Ignore FUP in PSB+ if followed by TIP.PGE */
3003 		if (data->after_psbend)
3004 			data->fup = false;
3005 		return 1;
3006 
3007 	case INTEL_PT_PTWRITE:
3008 	case INTEL_PT_PTWRITE_IP:
3009 	case INTEL_PT_EXSTOP:
3010 	case INTEL_PT_EXSTOP_IP:
3011 	case INTEL_PT_MWAIT:
3012 	case INTEL_PT_PWRE:
3013 	case INTEL_PT_PWRX:
3014 	case INTEL_PT_BBP:
3015 	case INTEL_PT_BIP:
3016 	case INTEL_PT_BEP:
3017 	case INTEL_PT_BEP_IP:
3018 	case INTEL_PT_CFE:
3019 	case INTEL_PT_CFE_IP:
3020 	case INTEL_PT_EVD:
3021 		if (data->after_psbend) {
3022 			data->after_psbend -= 1;
3023 			if (!data->after_psbend)
3024 				return 1;
3025 			break;
3026 		}
3027 		return 1;
3028 
3029 	case INTEL_PT_OVF:
3030 	case INTEL_PT_BAD:
3031 	case INTEL_PT_TNT:
3032 	case INTEL_PT_TIP_PGD:
3033 	case INTEL_PT_TIP:
3034 	case INTEL_PT_PSB:
3035 	case INTEL_PT_TRACESTOP:
3036 	default:
3037 		return 1;
3038 	}
3039 
3040 	return 0;
3041 }
3042 
3043 static int intel_pt_psb(struct intel_pt_decoder *decoder)
3044 {
3045 	int err;
3046 
3047 	decoder->last_ip = 0;
3048 	decoder->psb_ip = 0;
3049 	decoder->have_last_ip = true;
3050 	intel_pt_clear_stack(&decoder->stack);
3051 	err = intel_pt_walk_psbend(decoder);
3052 	if (err)
3053 		return err;
3054 	decoder->state.type = INTEL_PT_PSB_EVT;
3055 	decoder->state.from_ip = decoder->psb_ip;
3056 	decoder->state.to_ip = 0;
3057 	return 0;
3058 }
3059 
3060 static int intel_pt_fup_in_psb(struct intel_pt_decoder *decoder)
3061 {
3062 	int err;
3063 
3064 	if (decoder->ip != decoder->last_ip) {
3065 		err = intel_pt_walk_fup(decoder);
3066 		if (!err || err != -EAGAIN)
3067 			return err;
3068 	}
3069 
3070 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3071 	err = intel_pt_psb(decoder);
3072 	if (err) {
3073 		decoder->pkt_state = INTEL_PT_STATE_ERR3;
3074 		return -ENOENT;
3075 	}
3076 
3077 	return 0;
3078 }
3079 
3080 static bool intel_pt_psb_with_fup(struct intel_pt_decoder *decoder, int *err)
3081 {
3082 	struct intel_pt_psb_info data = { .fup = false };
3083 
3084 	if (!decoder->branch_enable)
3085 		return false;
3086 
3087 	intel_pt_pkt_lookahead(decoder, intel_pt_psb_lookahead_cb, &data);
3088 	if (!data.fup)
3089 		return false;
3090 
3091 	decoder->packet = data.fup_packet;
3092 	intel_pt_set_last_ip(decoder);
3093 	decoder->pkt_state = INTEL_PT_STATE_FUP_IN_PSB;
3094 
3095 	*err = intel_pt_fup_in_psb(decoder);
3096 
3097 	return true;
3098 }
3099 
3100 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
3101 {
3102 	int last_packet_type = INTEL_PT_PAD;
3103 	bool no_tip = false;
3104 	int err;
3105 
3106 	while (1) {
3107 		err = intel_pt_get_next_packet(decoder);
3108 		if (err)
3109 			return err;
3110 next:
3111 		err = 0;
3112 		if (decoder->cyc_threshold) {
3113 			if (decoder->sample_cyc && last_packet_type != INTEL_PT_CYC)
3114 				decoder->sample_cyc = false;
3115 			last_packet_type = decoder->packet.type;
3116 		}
3117 
3118 		if (decoder->hop) {
3119 			switch (intel_pt_hop_trace(decoder, &no_tip, &err)) {
3120 			case HOP_IGNORE:
3121 				continue;
3122 			case HOP_RETURN:
3123 				return err;
3124 			case HOP_AGAIN:
3125 				goto next;
3126 			default:
3127 				break;
3128 			}
3129 		}
3130 
3131 		switch (decoder->packet.type) {
3132 		case INTEL_PT_TNT:
3133 			if (!decoder->packet.count)
3134 				break;
3135 			decoder->tnt = decoder->packet;
3136 			decoder->pkt_state = INTEL_PT_STATE_TNT;
3137 			err = intel_pt_walk_tnt(decoder);
3138 			if (err == -EAGAIN)
3139 				break;
3140 			return err;
3141 
3142 		case INTEL_PT_TIP_PGD:
3143 			if (decoder->packet.count != 0)
3144 				intel_pt_set_last_ip(decoder);
3145 			decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
3146 			return intel_pt_walk_tip(decoder);
3147 
3148 		case INTEL_PT_TIP_PGE: {
3149 			decoder->pge = true;
3150 			decoder->overflow = false;
3151 			intel_pt_mtc_cyc_cnt_pge(decoder);
3152 			intel_pt_set_nr(decoder);
3153 			if (decoder->packet.count == 0) {
3154 				intel_pt_log_at("Skipping zero TIP.PGE",
3155 						decoder->pos);
3156 				break;
3157 			}
3158 			intel_pt_sample_iflag_chg(decoder);
3159 			intel_pt_set_ip(decoder);
3160 			decoder->state.from_ip = 0;
3161 			decoder->state.to_ip = decoder->ip;
3162 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
3163 			/*
3164 			 * In hop mode, resample to get the to_ip as an
3165 			 * "instruction" sample.
3166 			 */
3167 			if (decoder->hop)
3168 				decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
3169 			return 0;
3170 		}
3171 
3172 		case INTEL_PT_OVF:
3173 			return intel_pt_overflow(decoder);
3174 
3175 		case INTEL_PT_TIP:
3176 			if (decoder->packet.count != 0)
3177 				intel_pt_set_last_ip(decoder);
3178 			decoder->pkt_state = INTEL_PT_STATE_TIP;
3179 			return intel_pt_walk_tip(decoder);
3180 
3181 		case INTEL_PT_FUP:
3182 			if (decoder->packet.count == 0) {
3183 				intel_pt_log_at("Skipping zero FUP",
3184 						decoder->pos);
3185 				no_tip = false;
3186 				break;
3187 			}
3188 			intel_pt_set_last_ip(decoder);
3189 			if (!decoder->branch_enable || !decoder->pge) {
3190 				decoder->ip = decoder->last_ip;
3191 				if (intel_pt_fup_event(decoder, no_tip))
3192 					return 0;
3193 				no_tip = false;
3194 				break;
3195 			}
3196 			if (decoder->set_fup_mwait)
3197 				no_tip = true;
3198 			if (no_tip)
3199 				decoder->pkt_state = INTEL_PT_STATE_FUP_NO_TIP;
3200 			else
3201 				decoder->pkt_state = INTEL_PT_STATE_FUP;
3202 			err = intel_pt_walk_fup(decoder);
3203 			if (err != -EAGAIN)
3204 				return err;
3205 			if (no_tip) {
3206 				no_tip = false;
3207 				break;
3208 			}
3209 			return intel_pt_walk_fup_tip(decoder);
3210 
3211 		case INTEL_PT_TRACESTOP:
3212 			decoder->pge = false;
3213 			decoder->continuous_period = false;
3214 			intel_pt_clear_tx_flags(decoder);
3215 			decoder->have_tma = false;
3216 			break;
3217 
3218 		case INTEL_PT_PSB:
3219 			decoder->state.psb_offset = decoder->pos;
3220 			decoder->psb_ip = 0;
3221 			if (intel_pt_psb_with_fup(decoder, &err))
3222 				return err;
3223 			err = intel_pt_psb(decoder);
3224 			if (err == -EAGAIN)
3225 				goto next;
3226 			return err;
3227 
3228 		case INTEL_PT_PIP:
3229 			intel_pt_update_pip(decoder);
3230 			break;
3231 
3232 		case INTEL_PT_MTC:
3233 			intel_pt_calc_mtc_timestamp(decoder);
3234 			if (decoder->period_type != INTEL_PT_PERIOD_MTC)
3235 				break;
3236 			/*
3237 			 * Ensure that there has been an instruction since the
3238 			 * last MTC.
3239 			 */
3240 			if (!decoder->mtc_insn)
3241 				break;
3242 			decoder->mtc_insn = false;
3243 			/* Ensure that there is a timestamp */
3244 			if (!decoder->timestamp)
3245 				break;
3246 			decoder->state.type = INTEL_PT_INSTRUCTION;
3247 			decoder->state.from_ip = decoder->ip;
3248 			decoder->state.to_ip = 0;
3249 			decoder->mtc_insn = false;
3250 			return 0;
3251 
3252 		case INTEL_PT_TSC:
3253 			intel_pt_calc_tsc_timestamp(decoder);
3254 			break;
3255 
3256 		case INTEL_PT_TMA:
3257 			intel_pt_calc_tma(decoder);
3258 			break;
3259 
3260 		case INTEL_PT_CYC:
3261 			intel_pt_calc_cyc_timestamp(decoder);
3262 			break;
3263 
3264 		case INTEL_PT_CBR:
3265 			intel_pt_calc_cbr(decoder);
3266 			if (decoder->cbr != decoder->cbr_seen) {
3267 				decoder->state.type = 0;
3268 				return 0;
3269 			}
3270 			break;
3271 
3272 		case INTEL_PT_MODE_EXEC:
3273 			intel_pt_mode_exec(decoder);
3274 			err = intel_pt_get_next_packet(decoder);
3275 			if (err)
3276 				return err;
3277 			if (decoder->packet.type == INTEL_PT_FUP) {
3278 				decoder->set_fup_mode_exec = true;
3279 				no_tip = true;
3280 			}
3281 			goto next;
3282 
3283 		case INTEL_PT_MODE_TSX:
3284 			/* MODE_TSX need not be followed by FUP */
3285 			if (!decoder->pge || decoder->in_psb) {
3286 				intel_pt_update_in_tx(decoder);
3287 				break;
3288 			}
3289 			err = intel_pt_mode_tsx(decoder, &no_tip);
3290 			if (err)
3291 				return err;
3292 			goto next;
3293 
3294 		case INTEL_PT_BAD: /* Does not happen */
3295 			return intel_pt_bug(decoder);
3296 
3297 		case INTEL_PT_PSBEND:
3298 		case INTEL_PT_VMCS:
3299 		case INTEL_PT_MNT:
3300 		case INTEL_PT_PAD:
3301 			break;
3302 
3303 		case INTEL_PT_PTWRITE_IP:
3304 			decoder->fup_ptw_payload = decoder->packet.payload;
3305 			err = intel_pt_get_next_packet(decoder);
3306 			if (err)
3307 				return err;
3308 			if (decoder->packet.type == INTEL_PT_FUP) {
3309 				decoder->set_fup_ptw = true;
3310 				no_tip = true;
3311 			} else {
3312 				intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
3313 						decoder->pos);
3314 			}
3315 			goto next;
3316 
3317 		case INTEL_PT_PTWRITE:
3318 			decoder->state.type = INTEL_PT_PTW;
3319 			decoder->state.from_ip = decoder->ip;
3320 			decoder->state.to_ip = 0;
3321 			decoder->state.ptw_payload = decoder->packet.payload;
3322 			return 0;
3323 
3324 		case INTEL_PT_MWAIT:
3325 			decoder->fup_mwait_payload = decoder->packet.payload;
3326 			decoder->set_fup_mwait = true;
3327 			break;
3328 
3329 		case INTEL_PT_PWRE:
3330 			if (decoder->set_fup_mwait) {
3331 				decoder->fup_pwre_payload =
3332 							decoder->packet.payload;
3333 				decoder->set_fup_pwre = true;
3334 				break;
3335 			}
3336 			decoder->state.type = INTEL_PT_PWR_ENTRY;
3337 			decoder->state.from_ip = decoder->ip;
3338 			decoder->state.to_ip = 0;
3339 			decoder->state.pwrx_payload = decoder->packet.payload;
3340 			return 0;
3341 
3342 		case INTEL_PT_EXSTOP_IP:
3343 			err = intel_pt_get_next_packet(decoder);
3344 			if (err)
3345 				return err;
3346 			if (decoder->packet.type == INTEL_PT_FUP) {
3347 				decoder->set_fup_exstop = true;
3348 				no_tip = true;
3349 			} else {
3350 				intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
3351 						decoder->pos);
3352 			}
3353 			goto next;
3354 
3355 		case INTEL_PT_EXSTOP:
3356 			decoder->state.type = INTEL_PT_EX_STOP;
3357 			decoder->state.from_ip = decoder->ip;
3358 			decoder->state.to_ip = 0;
3359 			return 0;
3360 
3361 		case INTEL_PT_PWRX:
3362 			decoder->state.type = INTEL_PT_PWR_EXIT;
3363 			decoder->state.from_ip = decoder->ip;
3364 			decoder->state.to_ip = 0;
3365 			decoder->state.pwrx_payload = decoder->packet.payload;
3366 			return 0;
3367 
3368 		case INTEL_PT_BBP:
3369 			intel_pt_bbp(decoder);
3370 			break;
3371 
3372 		case INTEL_PT_BIP:
3373 			intel_pt_bip(decoder);
3374 			break;
3375 
3376 		case INTEL_PT_BEP:
3377 			decoder->state.type = INTEL_PT_BLK_ITEMS;
3378 			decoder->state.from_ip = decoder->ip;
3379 			decoder->state.to_ip = 0;
3380 			return 0;
3381 
3382 		case INTEL_PT_BEP_IP:
3383 			err = intel_pt_get_next_packet(decoder);
3384 			if (err)
3385 				return err;
3386 			if (decoder->packet.type == INTEL_PT_FUP) {
3387 				decoder->set_fup_bep = true;
3388 				no_tip = true;
3389 			} else {
3390 				intel_pt_log_at("ERROR: Missing FUP after BEP",
3391 						decoder->pos);
3392 			}
3393 			goto next;
3394 
3395 		case INTEL_PT_CFE:
3396 			decoder->fup_cfe_pkt = decoder->packet;
3397 			decoder->set_fup_cfe = true;
3398 			if (!decoder->pge) {
3399 				intel_pt_fup_event(decoder, true);
3400 				return 0;
3401 			}
3402 			break;
3403 
3404 		case INTEL_PT_CFE_IP:
3405 			decoder->fup_cfe_pkt = decoder->packet;
3406 			err = intel_pt_get_next_packet(decoder);
3407 			if (err)
3408 				return err;
3409 			if (decoder->packet.type == INTEL_PT_FUP) {
3410 				decoder->set_fup_cfe_ip = true;
3411 				no_tip = true;
3412 			} else {
3413 				intel_pt_log_at("ERROR: Missing FUP after CFE",
3414 						decoder->pos);
3415 			}
3416 			goto next;
3417 
3418 		case INTEL_PT_EVD:
3419 			err = intel_pt_evd(decoder);
3420 			if (err)
3421 				return err;
3422 			break;
3423 
3424 		default:
3425 			return intel_pt_bug(decoder);
3426 		}
3427 	}
3428 }
3429 
3430 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
3431 {
3432 	return decoder->packet.count &&
3433 	       (decoder->have_last_ip || decoder->packet.count == 3 ||
3434 		decoder->packet.count == 6);
3435 }
3436 
3437 /* Walk PSB+ packets to get in sync. */
3438 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
3439 {
3440 	int err;
3441 
3442 	decoder->in_psb = true;
3443 
3444 	while (1) {
3445 		err = intel_pt_get_next_packet(decoder);
3446 		if (err)
3447 			goto out;
3448 
3449 		switch (decoder->packet.type) {
3450 		case INTEL_PT_TIP_PGD:
3451 			decoder->continuous_period = false;
3452 			__fallthrough;
3453 		case INTEL_PT_TIP_PGE:
3454 		case INTEL_PT_TIP:
3455 		case INTEL_PT_PTWRITE:
3456 		case INTEL_PT_PTWRITE_IP:
3457 		case INTEL_PT_EXSTOP:
3458 		case INTEL_PT_EXSTOP_IP:
3459 		case INTEL_PT_MWAIT:
3460 		case INTEL_PT_PWRE:
3461 		case INTEL_PT_PWRX:
3462 		case INTEL_PT_BBP:
3463 		case INTEL_PT_BIP:
3464 		case INTEL_PT_BEP:
3465 		case INTEL_PT_BEP_IP:
3466 		case INTEL_PT_CFE:
3467 		case INTEL_PT_CFE_IP:
3468 		case INTEL_PT_EVD:
3469 			intel_pt_log("ERROR: Unexpected packet\n");
3470 			err = -ENOENT;
3471 			goto out;
3472 
3473 		case INTEL_PT_FUP:
3474 			decoder->pge = true;
3475 			if (intel_pt_have_ip(decoder)) {
3476 				uint64_t current_ip = decoder->ip;
3477 
3478 				intel_pt_set_ip(decoder);
3479 				decoder->psb_ip = decoder->ip;
3480 				if (current_ip)
3481 					intel_pt_log_to("Setting IP",
3482 							decoder->ip);
3483 			}
3484 			break;
3485 
3486 		case INTEL_PT_MTC:
3487 			intel_pt_calc_mtc_timestamp(decoder);
3488 			break;
3489 
3490 		case INTEL_PT_TSC:
3491 			intel_pt_calc_tsc_timestamp(decoder);
3492 			break;
3493 
3494 		case INTEL_PT_TMA:
3495 			intel_pt_calc_tma(decoder);
3496 			break;
3497 
3498 		case INTEL_PT_CYC:
3499 			intel_pt_calc_cyc_timestamp(decoder);
3500 			break;
3501 
3502 		case INTEL_PT_CBR:
3503 			intel_pt_calc_cbr(decoder);
3504 			break;
3505 
3506 		case INTEL_PT_PIP:
3507 			intel_pt_set_pip(decoder);
3508 			break;
3509 
3510 		case INTEL_PT_MODE_EXEC:
3511 			intel_pt_mode_exec_status(decoder);
3512 			break;
3513 
3514 		case INTEL_PT_MODE_TSX:
3515 			intel_pt_update_in_tx(decoder);
3516 			break;
3517 
3518 		case INTEL_PT_TRACESTOP:
3519 			decoder->pge = false;
3520 			decoder->continuous_period = false;
3521 			intel_pt_clear_tx_flags(decoder);
3522 			__fallthrough;
3523 
3524 		case INTEL_PT_TNT:
3525 			decoder->have_tma = false;
3526 			intel_pt_log("ERROR: Unexpected packet\n");
3527 			if (decoder->ip)
3528 				decoder->pkt_state = INTEL_PT_STATE_ERR4;
3529 			else
3530 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
3531 			err = -ENOENT;
3532 			goto out;
3533 
3534 		case INTEL_PT_BAD: /* Does not happen */
3535 			err = intel_pt_bug(decoder);
3536 			goto out;
3537 
3538 		case INTEL_PT_OVF:
3539 			err = intel_pt_overflow(decoder);
3540 			goto out;
3541 
3542 		case INTEL_PT_PSBEND:
3543 			err = 0;
3544 			goto out;
3545 
3546 		case INTEL_PT_PSB:
3547 		case INTEL_PT_VMCS:
3548 		case INTEL_PT_MNT:
3549 		case INTEL_PT_PAD:
3550 		default:
3551 			break;
3552 		}
3553 	}
3554 out:
3555 	decoder->in_psb = false;
3556 
3557 	return err;
3558 }
3559 
3560 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
3561 {
3562 	int err;
3563 
3564 	while (1) {
3565 		err = intel_pt_get_next_packet(decoder);
3566 		if (err)
3567 			return err;
3568 
3569 		switch (decoder->packet.type) {
3570 		case INTEL_PT_TIP_PGD:
3571 			decoder->continuous_period = false;
3572 			decoder->pge = false;
3573 			if (intel_pt_have_ip(decoder))
3574 				intel_pt_set_ip(decoder);
3575 			if (!decoder->ip)
3576 				break;
3577 			decoder->state.type |= INTEL_PT_TRACE_END;
3578 			return 0;
3579 
3580 		case INTEL_PT_TIP_PGE:
3581 			decoder->pge = true;
3582 			intel_pt_mtc_cyc_cnt_pge(decoder);
3583 			if (intel_pt_have_ip(decoder))
3584 				intel_pt_set_ip(decoder);
3585 			if (!decoder->ip)
3586 				break;
3587 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
3588 			return 0;
3589 
3590 		case INTEL_PT_TIP:
3591 			decoder->pge = true;
3592 			if (intel_pt_have_ip(decoder))
3593 				intel_pt_set_ip(decoder);
3594 			if (!decoder->ip)
3595 				break;
3596 			return 0;
3597 
3598 		case INTEL_PT_FUP:
3599 			if (intel_pt_have_ip(decoder))
3600 				intel_pt_set_ip(decoder);
3601 			if (decoder->ip)
3602 				return 0;
3603 			break;
3604 
3605 		case INTEL_PT_MTC:
3606 			intel_pt_calc_mtc_timestamp(decoder);
3607 			break;
3608 
3609 		case INTEL_PT_TSC:
3610 			intel_pt_calc_tsc_timestamp(decoder);
3611 			break;
3612 
3613 		case INTEL_PT_TMA:
3614 			intel_pt_calc_tma(decoder);
3615 			break;
3616 
3617 		case INTEL_PT_CYC:
3618 			intel_pt_calc_cyc_timestamp(decoder);
3619 			break;
3620 
3621 		case INTEL_PT_CBR:
3622 			intel_pt_calc_cbr(decoder);
3623 			break;
3624 
3625 		case INTEL_PT_PIP:
3626 			intel_pt_set_pip(decoder);
3627 			break;
3628 
3629 		case INTEL_PT_MODE_EXEC:
3630 			intel_pt_mode_exec_status(decoder);
3631 			break;
3632 
3633 		case INTEL_PT_MODE_TSX:
3634 			intel_pt_update_in_tx(decoder);
3635 			break;
3636 
3637 		case INTEL_PT_OVF:
3638 			return intel_pt_overflow(decoder);
3639 
3640 		case INTEL_PT_BAD: /* Does not happen */
3641 			return intel_pt_bug(decoder);
3642 
3643 		case INTEL_PT_TRACESTOP:
3644 			decoder->pge = false;
3645 			decoder->continuous_period = false;
3646 			intel_pt_clear_tx_flags(decoder);
3647 			decoder->have_tma = false;
3648 			break;
3649 
3650 		case INTEL_PT_PSB:
3651 			decoder->state.psb_offset = decoder->pos;
3652 			decoder->psb_ip = 0;
3653 			decoder->last_ip = 0;
3654 			decoder->have_last_ip = true;
3655 			intel_pt_clear_stack(&decoder->stack);
3656 			err = intel_pt_walk_psb(decoder);
3657 			if (err)
3658 				return err;
3659 			decoder->state.type = INTEL_PT_PSB_EVT;
3660 			decoder->state.from_ip = decoder->psb_ip;
3661 			decoder->state.to_ip = 0;
3662 			return 0;
3663 
3664 		case INTEL_PT_TNT:
3665 		case INTEL_PT_PSBEND:
3666 		case INTEL_PT_VMCS:
3667 		case INTEL_PT_MNT:
3668 		case INTEL_PT_PAD:
3669 		case INTEL_PT_PTWRITE:
3670 		case INTEL_PT_PTWRITE_IP:
3671 		case INTEL_PT_EXSTOP:
3672 		case INTEL_PT_EXSTOP_IP:
3673 		case INTEL_PT_MWAIT:
3674 		case INTEL_PT_PWRE:
3675 		case INTEL_PT_PWRX:
3676 		case INTEL_PT_BBP:
3677 		case INTEL_PT_BIP:
3678 		case INTEL_PT_BEP:
3679 		case INTEL_PT_BEP_IP:
3680 		case INTEL_PT_CFE:
3681 		case INTEL_PT_CFE_IP:
3682 		case INTEL_PT_EVD:
3683 		default:
3684 			break;
3685 		}
3686 	}
3687 }
3688 
3689 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
3690 {
3691 	int err;
3692 
3693 	intel_pt_clear_fup_event(decoder);
3694 	decoder->overflow = false;
3695 
3696 	if (!decoder->branch_enable) {
3697 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3698 		decoder->state.type = 0; /* Do not have a sample */
3699 		return 0;
3700 	}
3701 
3702 	intel_pt_log("Scanning for full IP\n");
3703 	err = intel_pt_walk_to_ip(decoder);
3704 	if (err || ((decoder->state.type & INTEL_PT_PSB_EVT) && !decoder->ip))
3705 		return err;
3706 
3707 	/* In hop mode, resample to get the to_ip as an "instruction" sample */
3708 	if (decoder->hop)
3709 		decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
3710 	else
3711 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3712 
3713 	decoder->state.from_ip = 0;
3714 	decoder->state.to_ip = decoder->ip;
3715 	intel_pt_log_to("Setting IP", decoder->ip);
3716 
3717 	return 0;
3718 }
3719 
3720 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
3721 {
3722 	const unsigned char *end = decoder->buf + decoder->len;
3723 	size_t i;
3724 
3725 	for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
3726 		if (i > decoder->len)
3727 			continue;
3728 		if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
3729 			return i;
3730 	}
3731 	return 0;
3732 }
3733 
3734 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
3735 {
3736 	size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
3737 	const char *psb = INTEL_PT_PSB_STR;
3738 
3739 	if (rest_psb > decoder->len ||
3740 	    memcmp(decoder->buf, psb + part_psb, rest_psb))
3741 		return 0;
3742 
3743 	return rest_psb;
3744 }
3745 
3746 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
3747 				  int part_psb)
3748 {
3749 	int rest_psb, ret;
3750 
3751 	decoder->pos += decoder->len;
3752 	decoder->len = 0;
3753 
3754 	ret = intel_pt_get_next_data(decoder, false);
3755 	if (ret)
3756 		return ret;
3757 
3758 	rest_psb = intel_pt_rest_psb(decoder, part_psb);
3759 	if (!rest_psb)
3760 		return 0;
3761 
3762 	decoder->pos -= part_psb;
3763 	decoder->next_buf = decoder->buf + rest_psb;
3764 	decoder->next_len = decoder->len - rest_psb;
3765 	memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
3766 	decoder->buf = decoder->temp_buf;
3767 	decoder->len = INTEL_PT_PSB_LEN;
3768 
3769 	return 0;
3770 }
3771 
3772 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
3773 {
3774 	unsigned char *next;
3775 	int ret;
3776 
3777 	intel_pt_log("Scanning for PSB\n");
3778 	while (1) {
3779 		if (!decoder->len) {
3780 			ret = intel_pt_get_next_data(decoder, false);
3781 			if (ret)
3782 				return ret;
3783 		}
3784 
3785 		next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
3786 			      INTEL_PT_PSB_LEN);
3787 		if (!next) {
3788 			int part_psb;
3789 
3790 			part_psb = intel_pt_part_psb(decoder);
3791 			if (part_psb) {
3792 				ret = intel_pt_get_split_psb(decoder, part_psb);
3793 				if (ret)
3794 					return ret;
3795 			} else {
3796 				decoder->pos += decoder->len;
3797 				decoder->len = 0;
3798 			}
3799 			continue;
3800 		}
3801 
3802 		decoder->pkt_step = next - decoder->buf;
3803 		return intel_pt_get_next_packet(decoder);
3804 	}
3805 }
3806 
3807 static int intel_pt_sync(struct intel_pt_decoder *decoder)
3808 {
3809 	int err;
3810 
3811 	decoder->pge = false;
3812 	decoder->continuous_period = false;
3813 	decoder->have_last_ip = false;
3814 	decoder->last_ip = 0;
3815 	decoder->psb_ip = 0;
3816 	decoder->ip = 0;
3817 	intel_pt_clear_stack(&decoder->stack);
3818 
3819 	err = intel_pt_scan_for_psb(decoder);
3820 	if (err)
3821 		return err;
3822 
3823 	if (decoder->vm_time_correlation) {
3824 		decoder->in_psb = true;
3825 		if (!decoder->timestamp)
3826 			decoder->timestamp = 1;
3827 		decoder->state.type = 0;
3828 		decoder->pkt_state = INTEL_PT_STATE_VM_TIME_CORRELATION;
3829 		return 0;
3830 	}
3831 
3832 	decoder->have_last_ip = true;
3833 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3834 
3835 	err = intel_pt_walk_psb(decoder);
3836 	if (err)
3837 		return err;
3838 
3839 	decoder->state.type = INTEL_PT_PSB_EVT; /* Only PSB sample */
3840 	decoder->state.from_ip = decoder->psb_ip;
3841 	decoder->state.to_ip = 0;
3842 
3843 	if (decoder->ip) {
3844 		/*
3845 		 * In hop mode, resample to get the PSB FUP ip as an
3846 		 * "instruction" sample.
3847 		 */
3848 		if (decoder->hop)
3849 			decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
3850 		else
3851 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
3852 	}
3853 
3854 	return 0;
3855 }
3856 
3857 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
3858 {
3859 	uint64_t est = decoder->sample_insn_cnt << 1;
3860 
3861 	if (!decoder->cbr || !decoder->max_non_turbo_ratio)
3862 		goto out;
3863 
3864 	est *= decoder->max_non_turbo_ratio;
3865 	est /= decoder->cbr;
3866 out:
3867 	return decoder->sample_timestamp + est;
3868 }
3869 
3870 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
3871 {
3872 	int err;
3873 
3874 	do {
3875 		decoder->state.type = INTEL_PT_BRANCH;
3876 		decoder->state.flags = 0;
3877 
3878 		switch (decoder->pkt_state) {
3879 		case INTEL_PT_STATE_NO_PSB:
3880 			err = intel_pt_sync(decoder);
3881 			break;
3882 		case INTEL_PT_STATE_NO_IP:
3883 			decoder->have_last_ip = false;
3884 			decoder->last_ip = 0;
3885 			decoder->ip = 0;
3886 			__fallthrough;
3887 		case INTEL_PT_STATE_ERR_RESYNC:
3888 			err = intel_pt_sync_ip(decoder);
3889 			break;
3890 		case INTEL_PT_STATE_IN_SYNC:
3891 			err = intel_pt_walk_trace(decoder);
3892 			break;
3893 		case INTEL_PT_STATE_TNT:
3894 		case INTEL_PT_STATE_TNT_CONT:
3895 			err = intel_pt_walk_tnt(decoder);
3896 			if (err == -EAGAIN)
3897 				err = intel_pt_walk_trace(decoder);
3898 			break;
3899 		case INTEL_PT_STATE_TIP:
3900 		case INTEL_PT_STATE_TIP_PGD:
3901 			err = intel_pt_walk_tip(decoder);
3902 			break;
3903 		case INTEL_PT_STATE_FUP:
3904 			err = intel_pt_walk_fup(decoder);
3905 			if (err == -EAGAIN)
3906 				err = intel_pt_walk_fup_tip(decoder);
3907 			break;
3908 		case INTEL_PT_STATE_FUP_NO_TIP:
3909 			err = intel_pt_walk_fup(decoder);
3910 			if (err == -EAGAIN)
3911 				err = intel_pt_walk_trace(decoder);
3912 			break;
3913 		case INTEL_PT_STATE_FUP_IN_PSB:
3914 			err = intel_pt_fup_in_psb(decoder);
3915 			break;
3916 		case INTEL_PT_STATE_RESAMPLE:
3917 			err = intel_pt_resample(decoder);
3918 			break;
3919 		case INTEL_PT_STATE_VM_TIME_CORRELATION:
3920 			err = intel_pt_vm_time_correlation(decoder);
3921 			break;
3922 		default:
3923 			err = intel_pt_bug(decoder);
3924 			break;
3925 		}
3926 	} while (err == -ENOLINK);
3927 
3928 	if (err) {
3929 		decoder->state.err = intel_pt_ext_err(err);
3930 		if (err != -EOVERFLOW)
3931 			decoder->state.from_ip = decoder->ip;
3932 		intel_pt_update_sample_time(decoder);
3933 		decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
3934 		intel_pt_set_nr(decoder);
3935 	} else {
3936 		decoder->state.err = 0;
3937 		if (decoder->cbr != decoder->cbr_seen) {
3938 			decoder->cbr_seen = decoder->cbr;
3939 			if (!decoder->state.type) {
3940 				decoder->state.from_ip = decoder->ip;
3941 				decoder->state.to_ip = 0;
3942 			}
3943 			decoder->state.type |= INTEL_PT_CBR_CHG;
3944 			decoder->state.cbr_payload = decoder->cbr_payload;
3945 			decoder->state.cbr = decoder->cbr;
3946 		}
3947 		if (intel_pt_sample_time(decoder->pkt_state)) {
3948 			intel_pt_update_sample_time(decoder);
3949 			if (decoder->sample_cyc) {
3950 				decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
3951 				decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
3952 				decoder->sample_cyc = false;
3953 			}
3954 		}
3955 		/*
3956 		 * When using only TSC/MTC to compute cycles, IPC can be
3957 		 * sampled as soon as the cycle count changes.
3958 		 */
3959 		if (!decoder->have_cyc)
3960 			decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
3961 	}
3962 
3963 	 /* Let PSB event always have TSC timestamp */
3964 	if ((decoder->state.type & INTEL_PT_PSB_EVT) && decoder->tsc_timestamp)
3965 		decoder->sample_timestamp = decoder->tsc_timestamp;
3966 
3967 	decoder->state.from_nr = decoder->nr;
3968 	decoder->state.to_nr = decoder->next_nr;
3969 	decoder->nr = decoder->next_nr;
3970 
3971 	decoder->state.timestamp = decoder->sample_timestamp;
3972 	decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
3973 	decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
3974 	decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
3975 
3976 	return &decoder->state;
3977 }
3978 
3979 /**
3980  * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
3981  * @buf: pointer to buffer pointer
3982  * @len: size of buffer
3983  *
3984  * Updates the buffer pointer to point to the start of the next PSB packet if
3985  * there is one, otherwise the buffer pointer is unchanged.  If @buf is updated,
3986  * @len is adjusted accordingly.
3987  *
3988  * Return: %true if a PSB packet is found, %false otherwise.
3989  */
3990 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
3991 {
3992 	unsigned char *next;
3993 
3994 	next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
3995 	if (next) {
3996 		*len -= next - *buf;
3997 		*buf = next;
3998 		return true;
3999 	}
4000 	return false;
4001 }
4002 
4003 /**
4004  * intel_pt_step_psb - move buffer pointer to the start of the following PSB
4005  *                     packet.
4006  * @buf: pointer to buffer pointer
4007  * @len: size of buffer
4008  *
4009  * Updates the buffer pointer to point to the start of the following PSB packet
4010  * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
4011  * pointer is unchanged.  If @buf is updated, @len is adjusted accordingly.
4012  *
4013  * Return: %true if a PSB packet is found, %false otherwise.
4014  */
4015 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
4016 {
4017 	unsigned char *next;
4018 
4019 	if (!*len)
4020 		return false;
4021 
4022 	next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
4023 	if (next) {
4024 		*len -= next - *buf;
4025 		*buf = next;
4026 		return true;
4027 	}
4028 	return false;
4029 }
4030 
4031 /**
4032  * intel_pt_last_psb - find the last PSB packet in a buffer.
4033  * @buf: buffer
4034  * @len: size of buffer
4035  *
4036  * This function finds the last PSB in a buffer.
4037  *
4038  * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
4039  */
4040 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
4041 {
4042 	const char *n = INTEL_PT_PSB_STR;
4043 	unsigned char *p;
4044 	size_t k;
4045 
4046 	if (len < INTEL_PT_PSB_LEN)
4047 		return NULL;
4048 
4049 	k = len - INTEL_PT_PSB_LEN + 1;
4050 	while (1) {
4051 		p = memrchr(buf, n[0], k);
4052 		if (!p)
4053 			return NULL;
4054 		if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
4055 			return p;
4056 		k = p - buf;
4057 		if (!k)
4058 			return NULL;
4059 	}
4060 }
4061 
4062 /**
4063  * intel_pt_next_tsc - find and return next TSC.
4064  * @buf: buffer
4065  * @len: size of buffer
4066  * @tsc: TSC value returned
4067  * @rem: returns remaining size when TSC is found
4068  *
4069  * Find a TSC packet in @buf and return the TSC value.  This function assumes
4070  * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
4071  * PSBEND packet is found.
4072  *
4073  * Return: %true if TSC is found, false otherwise.
4074  */
4075 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
4076 			      size_t *rem)
4077 {
4078 	enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
4079 	struct intel_pt_pkt packet;
4080 	int ret;
4081 
4082 	while (len) {
4083 		ret = intel_pt_get_packet(buf, len, &packet, &ctx);
4084 		if (ret <= 0)
4085 			return false;
4086 		if (packet.type == INTEL_PT_TSC) {
4087 			*tsc = packet.payload;
4088 			*rem = len;
4089 			return true;
4090 		}
4091 		if (packet.type == INTEL_PT_PSBEND)
4092 			return false;
4093 		buf += ret;
4094 		len -= ret;
4095 	}
4096 	return false;
4097 }
4098 
4099 /**
4100  * intel_pt_tsc_cmp - compare 7-byte TSCs.
4101  * @tsc1: first TSC to compare
4102  * @tsc2: second TSC to compare
4103  *
4104  * This function compares 7-byte TSC values allowing for the possibility that
4105  * TSC wrapped around.  Generally it is not possible to know if TSC has wrapped
4106  * around so for that purpose this function assumes the absolute difference is
4107  * less than half the maximum difference.
4108  *
4109  * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
4110  * after @tsc2.
4111  */
4112 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
4113 {
4114 	const uint64_t halfway = (1ULL << 55);
4115 
4116 	if (tsc1 == tsc2)
4117 		return 0;
4118 
4119 	if (tsc1 < tsc2) {
4120 		if (tsc2 - tsc1 < halfway)
4121 			return -1;
4122 		else
4123 			return 1;
4124 	} else {
4125 		if (tsc1 - tsc2 < halfway)
4126 			return 1;
4127 		else
4128 			return -1;
4129 	}
4130 }
4131 
4132 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
4133 
4134 /**
4135  * adj_for_padding - adjust overlap to account for padding.
4136  * @buf_b: second buffer
4137  * @buf_a: first buffer
4138  * @len_a: size of first buffer
4139  *
4140  * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
4141  * accordingly.
4142  *
4143  * Return: A pointer into @buf_b from where non-overlapped data starts
4144  */
4145 static unsigned char *adj_for_padding(unsigned char *buf_b,
4146 				      unsigned char *buf_a, size_t len_a)
4147 {
4148 	unsigned char *p = buf_b - MAX_PADDING;
4149 	unsigned char *q = buf_a + len_a - MAX_PADDING;
4150 	int i;
4151 
4152 	for (i = MAX_PADDING; i; i--, p++, q++) {
4153 		if (*p != *q)
4154 			break;
4155 	}
4156 
4157 	return p;
4158 }
4159 
4160 /**
4161  * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
4162  *                             using TSC.
4163  * @buf_a: first buffer
4164  * @len_a: size of first buffer
4165  * @buf_b: second buffer
4166  * @len_b: size of second buffer
4167  * @consecutive: returns true if there is data in buf_b that is consecutive
4168  *               to buf_a
4169  * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
4170  *
4171  * If the trace contains TSC we can look at the last TSC of @buf_a and the
4172  * first TSC of @buf_b in order to determine if the buffers overlap, and then
4173  * walk forward in @buf_b until a later TSC is found.  A precondition is that
4174  * @buf_a and @buf_b are positioned at a PSB.
4175  *
4176  * Return: A pointer into @buf_b from where non-overlapped data starts, or
4177  * @buf_b + @len_b if there is no non-overlapped data.
4178  */
4179 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
4180 						size_t len_a,
4181 						unsigned char *buf_b,
4182 						size_t len_b, bool *consecutive,
4183 						bool ooo_tsc)
4184 {
4185 	uint64_t tsc_a, tsc_b;
4186 	unsigned char *p;
4187 	size_t len, rem_a, rem_b;
4188 
4189 	p = intel_pt_last_psb(buf_a, len_a);
4190 	if (!p)
4191 		return buf_b; /* No PSB in buf_a => no overlap */
4192 
4193 	len = len_a - (p - buf_a);
4194 	if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
4195 		/* The last PSB+ in buf_a is incomplete, so go back one more */
4196 		len_a -= len;
4197 		p = intel_pt_last_psb(buf_a, len_a);
4198 		if (!p)
4199 			return buf_b; /* No full PSB+ => assume no overlap */
4200 		len = len_a - (p - buf_a);
4201 		if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
4202 			return buf_b; /* No TSC in buf_a => assume no overlap */
4203 	}
4204 
4205 	while (1) {
4206 		/* Ignore PSB+ with no TSC */
4207 		if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
4208 			int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
4209 
4210 			/* Same TSC, so buffers are consecutive */
4211 			if (!cmp && rem_b >= rem_a) {
4212 				unsigned char *start;
4213 
4214 				*consecutive = true;
4215 				start = buf_b + len_b - (rem_b - rem_a);
4216 				return adj_for_padding(start, buf_a, len_a);
4217 			}
4218 			if (cmp < 0 && !ooo_tsc)
4219 				return buf_b; /* tsc_a < tsc_b => no overlap */
4220 		}
4221 
4222 		if (!intel_pt_step_psb(&buf_b, &len_b))
4223 			return buf_b + len_b; /* No PSB in buf_b => no data */
4224 	}
4225 }
4226 
4227 /**
4228  * intel_pt_find_overlap - determine start of non-overlapped trace data.
4229  * @buf_a: first buffer
4230  * @len_a: size of first buffer
4231  * @buf_b: second buffer
4232  * @len_b: size of second buffer
4233  * @have_tsc: can use TSC packets to detect overlap
4234  * @consecutive: returns true if there is data in buf_b that is consecutive
4235  *               to buf_a
4236  * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
4237  *
4238  * When trace samples or snapshots are recorded there is the possibility that
4239  * the data overlaps.  Note that, for the purposes of decoding, data is only
4240  * useful if it begins with a PSB packet.
4241  *
4242  * Return: A pointer into @buf_b from where non-overlapped data starts, or
4243  * @buf_b + @len_b if there is no non-overlapped data.
4244  */
4245 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
4246 				     unsigned char *buf_b, size_t len_b,
4247 				     bool have_tsc, bool *consecutive,
4248 				     bool ooo_tsc)
4249 {
4250 	unsigned char *found;
4251 
4252 	/* Buffer 'b' must start at PSB so throw away everything before that */
4253 	if (!intel_pt_next_psb(&buf_b, &len_b))
4254 		return buf_b + len_b; /* No PSB */
4255 
4256 	if (!intel_pt_next_psb(&buf_a, &len_a))
4257 		return buf_b; /* No overlap */
4258 
4259 	if (have_tsc) {
4260 		found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
4261 						  consecutive, ooo_tsc);
4262 		if (found)
4263 			return found;
4264 	}
4265 
4266 	/*
4267 	 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
4268 	 * we can ignore the first part of buffer 'a'.
4269 	 */
4270 	while (len_b < len_a) {
4271 		if (!intel_pt_step_psb(&buf_a, &len_a))
4272 			return buf_b; /* No overlap */
4273 	}
4274 
4275 	/* Now len_b >= len_a */
4276 	while (1) {
4277 		/* Potential overlap so check the bytes */
4278 		found = memmem(buf_a, len_a, buf_b, len_a);
4279 		if (found) {
4280 			*consecutive = true;
4281 			return adj_for_padding(buf_b + len_a, buf_a, len_a);
4282 		}
4283 
4284 		/* Try again at next PSB in buffer 'a' */
4285 		if (!intel_pt_step_psb(&buf_a, &len_a))
4286 			return buf_b; /* No overlap */
4287 	}
4288 }
4289 
4290 /**
4291  * struct fast_forward_data - data used by intel_pt_ff_cb().
4292  * @timestamp: timestamp to fast forward towards
4293  * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
4294  *                 the fast forward timestamp.
4295  */
4296 struct fast_forward_data {
4297 	uint64_t timestamp;
4298 	uint64_t buf_timestamp;
4299 };
4300 
4301 /**
4302  * intel_pt_ff_cb - fast forward lookahead callback.
4303  * @buffer: Intel PT trace buffer
4304  * @data: opaque pointer to fast forward data (struct fast_forward_data)
4305  *
4306  * Determine if @buffer trace is past the fast forward timestamp.
4307  *
4308  * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
4309  *         timestamp, and 0 otherwise.
4310  */
4311 static int intel_pt_ff_cb(struct intel_pt_buffer *buffer, void *data)
4312 {
4313 	struct fast_forward_data *d = data;
4314 	unsigned char *buf;
4315 	uint64_t tsc;
4316 	size_t rem;
4317 	size_t len;
4318 
4319 	buf = (unsigned char *)buffer->buf;
4320 	len = buffer->len;
4321 
4322 	if (!intel_pt_next_psb(&buf, &len) ||
4323 	    !intel_pt_next_tsc(buf, len, &tsc, &rem))
4324 		return 0;
4325 
4326 	tsc = intel_pt_8b_tsc(tsc, buffer->ref_timestamp);
4327 
4328 	intel_pt_log("Buffer 1st timestamp " x64_fmt " ref timestamp " x64_fmt "\n",
4329 		     tsc, buffer->ref_timestamp);
4330 
4331 	/*
4332 	 * If the buffer contains a timestamp earlier that the fast forward
4333 	 * timestamp, then record it, else stop.
4334 	 */
4335 	if (tsc < d->timestamp)
4336 		d->buf_timestamp = buffer->ref_timestamp;
4337 	else
4338 		return 1;
4339 
4340 	return 0;
4341 }
4342 
4343 /**
4344  * intel_pt_fast_forward - reposition decoder forwards.
4345  * @decoder: Intel PT decoder
4346  * @timestamp: timestamp to fast forward towards
4347  *
4348  * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
4349  *
4350  * Return: 0 on success or negative error code on failure.
4351  */
4352 int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp)
4353 {
4354 	struct fast_forward_data d = { .timestamp = timestamp };
4355 	unsigned char *buf;
4356 	size_t len;
4357 	int err;
4358 
4359 	intel_pt_log("Fast forward towards timestamp " x64_fmt "\n", timestamp);
4360 
4361 	/* Find buffer timestamp of buffer to fast forward to */
4362 	err = decoder->lookahead(decoder->data, intel_pt_ff_cb, &d);
4363 	if (err < 0)
4364 		return err;
4365 
4366 	/* Walk to buffer with same buffer timestamp */
4367 	if (d.buf_timestamp) {
4368 		do {
4369 			decoder->pos += decoder->len;
4370 			decoder->len = 0;
4371 			err = intel_pt_get_next_data(decoder, true);
4372 			/* -ENOLINK means non-consecutive trace */
4373 			if (err && err != -ENOLINK)
4374 				return err;
4375 		} while (decoder->buf_timestamp != d.buf_timestamp);
4376 	}
4377 
4378 	if (!decoder->buf)
4379 		return 0;
4380 
4381 	buf = (unsigned char *)decoder->buf;
4382 	len = decoder->len;
4383 
4384 	if (!intel_pt_next_psb(&buf, &len))
4385 		return 0;
4386 
4387 	/*
4388 	 * Walk PSBs while the PSB timestamp is less than the fast forward
4389 	 * timestamp.
4390 	 */
4391 	do {
4392 		uint64_t tsc;
4393 		size_t rem;
4394 
4395 		if (!intel_pt_next_tsc(buf, len, &tsc, &rem))
4396 			break;
4397 		tsc = intel_pt_8b_tsc(tsc, decoder->buf_timestamp);
4398 		/*
4399 		 * A TSC packet can slip past MTC packets but, after fast
4400 		 * forward, decoding starts at the TSC timestamp. That means
4401 		 * the timestamps may not be exactly the same as the timestamps
4402 		 * that would have been decoded without fast forward.
4403 		 */
4404 		if (tsc < timestamp) {
4405 			intel_pt_log("Fast forward to next PSB timestamp " x64_fmt "\n", tsc);
4406 			decoder->pos += decoder->len - len;
4407 			decoder->buf = buf;
4408 			decoder->len = len;
4409 			intel_pt_reposition(decoder);
4410 		} else {
4411 			break;
4412 		}
4413 	} while (intel_pt_step_psb(&buf, &len));
4414 
4415 	return 0;
4416 }
4417