xref: /openbmc/linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c (revision 2f0f2441b4a10948e2ec042b48fef13680387f7c)
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 
18 #include "../cache.h"
19 #include "../util.h"
20 #include "../auxtrace.h"
21 
22 #include "intel-pt-insn-decoder.h"
23 #include "intel-pt-pkt-decoder.h"
24 #include "intel-pt-decoder.h"
25 #include "intel-pt-log.h"
26 
27 #define INTEL_PT_BLK_SIZE 1024
28 
29 #define BIT63 (((uint64_t)1 << 63))
30 
31 #define INTEL_PT_RETURN 1
32 
33 /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
34 #define INTEL_PT_MAX_LOOPS 10000
35 
36 struct intel_pt_blk {
37 	struct intel_pt_blk *prev;
38 	uint64_t ip[INTEL_PT_BLK_SIZE];
39 };
40 
41 struct intel_pt_stack {
42 	struct intel_pt_blk *blk;
43 	struct intel_pt_blk *spare;
44 	int pos;
45 };
46 
47 enum intel_pt_pkt_state {
48 	INTEL_PT_STATE_NO_PSB,
49 	INTEL_PT_STATE_NO_IP,
50 	INTEL_PT_STATE_ERR_RESYNC,
51 	INTEL_PT_STATE_IN_SYNC,
52 	INTEL_PT_STATE_TNT_CONT,
53 	INTEL_PT_STATE_TNT,
54 	INTEL_PT_STATE_TIP,
55 	INTEL_PT_STATE_TIP_PGD,
56 	INTEL_PT_STATE_FUP,
57 	INTEL_PT_STATE_FUP_NO_TIP,
58 };
59 
60 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
61 {
62 	switch (pkt_state) {
63 	case INTEL_PT_STATE_NO_PSB:
64 	case INTEL_PT_STATE_NO_IP:
65 	case INTEL_PT_STATE_ERR_RESYNC:
66 	case INTEL_PT_STATE_IN_SYNC:
67 	case INTEL_PT_STATE_TNT_CONT:
68 		return true;
69 	case INTEL_PT_STATE_TNT:
70 	case INTEL_PT_STATE_TIP:
71 	case INTEL_PT_STATE_TIP_PGD:
72 	case INTEL_PT_STATE_FUP:
73 	case INTEL_PT_STATE_FUP_NO_TIP:
74 		return false;
75 	default:
76 		return true;
77 	};
78 }
79 
80 #ifdef INTEL_PT_STRICT
81 #define INTEL_PT_STATE_ERR1	INTEL_PT_STATE_NO_PSB
82 #define INTEL_PT_STATE_ERR2	INTEL_PT_STATE_NO_PSB
83 #define INTEL_PT_STATE_ERR3	INTEL_PT_STATE_NO_PSB
84 #define INTEL_PT_STATE_ERR4	INTEL_PT_STATE_NO_PSB
85 #else
86 #define INTEL_PT_STATE_ERR1	(decoder->pkt_state)
87 #define INTEL_PT_STATE_ERR2	INTEL_PT_STATE_NO_IP
88 #define INTEL_PT_STATE_ERR3	INTEL_PT_STATE_ERR_RESYNC
89 #define INTEL_PT_STATE_ERR4	INTEL_PT_STATE_IN_SYNC
90 #endif
91 
92 struct intel_pt_decoder {
93 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
94 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
95 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
96 			 uint64_t max_insn_cnt, void *data);
97 	bool (*pgd_ip)(uint64_t ip, void *data);
98 	void *data;
99 	struct intel_pt_state state;
100 	const unsigned char *buf;
101 	size_t len;
102 	bool return_compression;
103 	bool branch_enable;
104 	bool mtc_insn;
105 	bool pge;
106 	bool have_tma;
107 	bool have_cyc;
108 	bool fixup_last_mtc;
109 	bool have_last_ip;
110 	enum intel_pt_param_flags flags;
111 	uint64_t pos;
112 	uint64_t last_ip;
113 	uint64_t ip;
114 	uint64_t cr3;
115 	uint64_t timestamp;
116 	uint64_t tsc_timestamp;
117 	uint64_t ref_timestamp;
118 	uint64_t sample_timestamp;
119 	uint64_t ret_addr;
120 	uint64_t ctc_timestamp;
121 	uint64_t ctc_delta;
122 	uint64_t cycle_cnt;
123 	uint64_t cyc_ref_timestamp;
124 	uint32_t last_mtc;
125 	uint32_t tsc_ctc_ratio_n;
126 	uint32_t tsc_ctc_ratio_d;
127 	uint32_t tsc_ctc_mult;
128 	uint32_t tsc_slip;
129 	uint32_t ctc_rem_mask;
130 	int mtc_shift;
131 	struct intel_pt_stack stack;
132 	enum intel_pt_pkt_state pkt_state;
133 	struct intel_pt_pkt packet;
134 	struct intel_pt_pkt tnt;
135 	int pkt_step;
136 	int pkt_len;
137 	int last_packet_type;
138 	unsigned int cbr;
139 	unsigned int cbr_seen;
140 	unsigned int max_non_turbo_ratio;
141 	double max_non_turbo_ratio_fp;
142 	double cbr_cyc_to_tsc;
143 	double calc_cyc_to_tsc;
144 	bool have_calc_cyc_to_tsc;
145 	int exec_mode;
146 	unsigned int insn_bytes;
147 	uint64_t period;
148 	enum intel_pt_period_type period_type;
149 	uint64_t tot_insn_cnt;
150 	uint64_t period_insn_cnt;
151 	uint64_t period_mask;
152 	uint64_t period_ticks;
153 	uint64_t last_masked_timestamp;
154 	bool continuous_period;
155 	bool overflow;
156 	bool set_fup_tx_flags;
157 	bool set_fup_ptw;
158 	bool set_fup_mwait;
159 	bool set_fup_pwre;
160 	bool set_fup_exstop;
161 	unsigned int fup_tx_flags;
162 	unsigned int tx_flags;
163 	uint64_t fup_ptw_payload;
164 	uint64_t fup_mwait_payload;
165 	uint64_t fup_pwre_payload;
166 	uint64_t cbr_payload;
167 	uint64_t timestamp_insn_cnt;
168 	uint64_t sample_insn_cnt;
169 	uint64_t stuck_ip;
170 	int no_progress;
171 	int stuck_ip_prd;
172 	int stuck_ip_cnt;
173 	const unsigned char *next_buf;
174 	size_t next_len;
175 	unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
176 };
177 
178 static uint64_t intel_pt_lower_power_of_2(uint64_t x)
179 {
180 	int i;
181 
182 	for (i = 0; x != 1; i++)
183 		x >>= 1;
184 
185 	return x << i;
186 }
187 
188 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
189 {
190 	if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
191 		uint64_t period;
192 
193 		period = intel_pt_lower_power_of_2(decoder->period);
194 		decoder->period_mask  = ~(period - 1);
195 		decoder->period_ticks = period;
196 	}
197 }
198 
199 static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
200 {
201 	if (!d)
202 		return 0;
203 	return (t / d) * n + ((t % d) * n) / d;
204 }
205 
206 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
207 {
208 	struct intel_pt_decoder *decoder;
209 
210 	if (!params->get_trace || !params->walk_insn)
211 		return NULL;
212 
213 	decoder = zalloc(sizeof(struct intel_pt_decoder));
214 	if (!decoder)
215 		return NULL;
216 
217 	decoder->get_trace          = params->get_trace;
218 	decoder->walk_insn          = params->walk_insn;
219 	decoder->pgd_ip             = params->pgd_ip;
220 	decoder->data               = params->data;
221 	decoder->return_compression = params->return_compression;
222 	decoder->branch_enable      = params->branch_enable;
223 
224 	decoder->flags              = params->flags;
225 
226 	decoder->period             = params->period;
227 	decoder->period_type        = params->period_type;
228 
229 	decoder->max_non_turbo_ratio    = params->max_non_turbo_ratio;
230 	decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
231 
232 	intel_pt_setup_period(decoder);
233 
234 	decoder->mtc_shift = params->mtc_period;
235 	decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
236 
237 	decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
238 	decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
239 
240 	if (!decoder->tsc_ctc_ratio_n)
241 		decoder->tsc_ctc_ratio_d = 0;
242 
243 	if (decoder->tsc_ctc_ratio_d) {
244 		if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
245 			decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
246 						decoder->tsc_ctc_ratio_d;
247 	}
248 
249 	/*
250 	 * A TSC packet can slip past MTC packets so that the timestamp appears
251 	 * to go backwards. One estimate is that can be up to about 40 CPU
252 	 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
253 	 * slippage an order of magnitude more to be on the safe side.
254 	 */
255 	decoder->tsc_slip = 0x10000;
256 
257 	intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
258 	intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
259 	intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
260 	intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
261 	intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
262 
263 	return decoder;
264 }
265 
266 static void intel_pt_pop_blk(struct intel_pt_stack *stack)
267 {
268 	struct intel_pt_blk *blk = stack->blk;
269 
270 	stack->blk = blk->prev;
271 	if (!stack->spare)
272 		stack->spare = blk;
273 	else
274 		free(blk);
275 }
276 
277 static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
278 {
279 	if (!stack->pos) {
280 		if (!stack->blk)
281 			return 0;
282 		intel_pt_pop_blk(stack);
283 		if (!stack->blk)
284 			return 0;
285 		stack->pos = INTEL_PT_BLK_SIZE;
286 	}
287 	return stack->blk->ip[--stack->pos];
288 }
289 
290 static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
291 {
292 	struct intel_pt_blk *blk;
293 
294 	if (stack->spare) {
295 		blk = stack->spare;
296 		stack->spare = NULL;
297 	} else {
298 		blk = malloc(sizeof(struct intel_pt_blk));
299 		if (!blk)
300 			return -ENOMEM;
301 	}
302 
303 	blk->prev = stack->blk;
304 	stack->blk = blk;
305 	stack->pos = 0;
306 	return 0;
307 }
308 
309 static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
310 {
311 	int err;
312 
313 	if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
314 		err = intel_pt_alloc_blk(stack);
315 		if (err)
316 			return err;
317 	}
318 
319 	stack->blk->ip[stack->pos++] = ip;
320 	return 0;
321 }
322 
323 static void intel_pt_clear_stack(struct intel_pt_stack *stack)
324 {
325 	while (stack->blk)
326 		intel_pt_pop_blk(stack);
327 	stack->pos = 0;
328 }
329 
330 static void intel_pt_free_stack(struct intel_pt_stack *stack)
331 {
332 	intel_pt_clear_stack(stack);
333 	zfree(&stack->blk);
334 	zfree(&stack->spare);
335 }
336 
337 void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
338 {
339 	intel_pt_free_stack(&decoder->stack);
340 	free(decoder);
341 }
342 
343 static int intel_pt_ext_err(int code)
344 {
345 	switch (code) {
346 	case -ENOMEM:
347 		return INTEL_PT_ERR_NOMEM;
348 	case -ENOSYS:
349 		return INTEL_PT_ERR_INTERN;
350 	case -EBADMSG:
351 		return INTEL_PT_ERR_BADPKT;
352 	case -ENODATA:
353 		return INTEL_PT_ERR_NODATA;
354 	case -EILSEQ:
355 		return INTEL_PT_ERR_NOINSN;
356 	case -ENOENT:
357 		return INTEL_PT_ERR_MISMAT;
358 	case -EOVERFLOW:
359 		return INTEL_PT_ERR_OVR;
360 	case -ENOSPC:
361 		return INTEL_PT_ERR_LOST;
362 	case -ELOOP:
363 		return INTEL_PT_ERR_NELOOP;
364 	default:
365 		return INTEL_PT_ERR_UNK;
366 	}
367 }
368 
369 static const char *intel_pt_err_msgs[] = {
370 	[INTEL_PT_ERR_NOMEM]  = "Memory allocation failed",
371 	[INTEL_PT_ERR_INTERN] = "Internal error",
372 	[INTEL_PT_ERR_BADPKT] = "Bad packet",
373 	[INTEL_PT_ERR_NODATA] = "No more data",
374 	[INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
375 	[INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
376 	[INTEL_PT_ERR_OVR]    = "Overflow packet",
377 	[INTEL_PT_ERR_LOST]   = "Lost trace data",
378 	[INTEL_PT_ERR_UNK]    = "Unknown error!",
379 	[INTEL_PT_ERR_NELOOP] = "Never-ending loop",
380 };
381 
382 int intel_pt__strerror(int code, char *buf, size_t buflen)
383 {
384 	if (code < 1 || code >= INTEL_PT_ERR_MAX)
385 		code = INTEL_PT_ERR_UNK;
386 	strlcpy(buf, intel_pt_err_msgs[code], buflen);
387 	return 0;
388 }
389 
390 static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
391 				 uint64_t last_ip)
392 {
393 	uint64_t ip;
394 
395 	switch (packet->count) {
396 	case 1:
397 		ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
398 		     packet->payload;
399 		break;
400 	case 2:
401 		ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
402 		     packet->payload;
403 		break;
404 	case 3:
405 		ip = packet->payload;
406 		/* Sign-extend 6-byte ip */
407 		if (ip & (uint64_t)0x800000000000ULL)
408 			ip |= (uint64_t)0xffff000000000000ULL;
409 		break;
410 	case 4:
411 		ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
412 		     packet->payload;
413 		break;
414 	case 6:
415 		ip = packet->payload;
416 		break;
417 	default:
418 		return 0;
419 	}
420 
421 	return ip;
422 }
423 
424 static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
425 {
426 	decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
427 	decoder->have_last_ip = true;
428 }
429 
430 static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
431 {
432 	intel_pt_set_last_ip(decoder);
433 	decoder->ip = decoder->last_ip;
434 }
435 
436 static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
437 {
438 	intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
439 			    decoder->buf);
440 }
441 
442 static int intel_pt_bug(struct intel_pt_decoder *decoder)
443 {
444 	intel_pt_log("ERROR: Internal error\n");
445 	decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
446 	return -ENOSYS;
447 }
448 
449 static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
450 {
451 	decoder->tx_flags = 0;
452 }
453 
454 static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
455 {
456 	decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
457 }
458 
459 static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
460 {
461 	intel_pt_clear_tx_flags(decoder);
462 	decoder->have_tma = false;
463 	decoder->pkt_len = 1;
464 	decoder->pkt_step = 1;
465 	intel_pt_decoder_log_packet(decoder);
466 	if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
467 		intel_pt_log("ERROR: Bad packet\n");
468 		decoder->pkt_state = INTEL_PT_STATE_ERR1;
469 	}
470 	return -EBADMSG;
471 }
472 
473 static int intel_pt_get_data(struct intel_pt_decoder *decoder)
474 {
475 	struct intel_pt_buffer buffer = { .buf = 0, };
476 	int ret;
477 
478 	decoder->pkt_step = 0;
479 
480 	intel_pt_log("Getting more data\n");
481 	ret = decoder->get_trace(&buffer, decoder->data);
482 	if (ret)
483 		return ret;
484 	decoder->buf = buffer.buf;
485 	decoder->len = buffer.len;
486 	if (!decoder->len) {
487 		intel_pt_log("No more data\n");
488 		return -ENODATA;
489 	}
490 	if (!buffer.consecutive) {
491 		decoder->ip = 0;
492 		decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
493 		decoder->ref_timestamp = buffer.ref_timestamp;
494 		decoder->timestamp = 0;
495 		decoder->have_tma = false;
496 		decoder->state.trace_nr = buffer.trace_nr;
497 		intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
498 			     decoder->ref_timestamp);
499 		return -ENOLINK;
500 	}
501 
502 	return 0;
503 }
504 
505 static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
506 {
507 	if (!decoder->next_buf)
508 		return intel_pt_get_data(decoder);
509 
510 	decoder->buf = decoder->next_buf;
511 	decoder->len = decoder->next_len;
512 	decoder->next_buf = 0;
513 	decoder->next_len = 0;
514 	return 0;
515 }
516 
517 static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
518 {
519 	unsigned char *buf = decoder->temp_buf;
520 	size_t old_len, len, n;
521 	int ret;
522 
523 	old_len = decoder->len;
524 	len = decoder->len;
525 	memcpy(buf, decoder->buf, len);
526 
527 	ret = intel_pt_get_data(decoder);
528 	if (ret) {
529 		decoder->pos += old_len;
530 		return ret < 0 ? ret : -EINVAL;
531 	}
532 
533 	n = INTEL_PT_PKT_MAX_SZ - len;
534 	if (n > decoder->len)
535 		n = decoder->len;
536 	memcpy(buf + len, decoder->buf, n);
537 	len += n;
538 
539 	ret = intel_pt_get_packet(buf, len, &decoder->packet);
540 	if (ret < (int)old_len) {
541 		decoder->next_buf = decoder->buf;
542 		decoder->next_len = decoder->len;
543 		decoder->buf = buf;
544 		decoder->len = old_len;
545 		return intel_pt_bad_packet(decoder);
546 	}
547 
548 	decoder->next_buf = decoder->buf + (ret - old_len);
549 	decoder->next_len = decoder->len - (ret - old_len);
550 
551 	decoder->buf = buf;
552 	decoder->len = ret;
553 
554 	return ret;
555 }
556 
557 struct intel_pt_pkt_info {
558 	struct intel_pt_decoder	  *decoder;
559 	struct intel_pt_pkt       packet;
560 	uint64_t                  pos;
561 	int                       pkt_len;
562 	int                       last_packet_type;
563 	void                      *data;
564 };
565 
566 typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
567 
568 /* Lookahead packets in current buffer */
569 static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
570 				  intel_pt_pkt_cb_t cb, void *data)
571 {
572 	struct intel_pt_pkt_info pkt_info;
573 	const unsigned char *buf = decoder->buf;
574 	size_t len = decoder->len;
575 	int ret;
576 
577 	pkt_info.decoder          = decoder;
578 	pkt_info.pos              = decoder->pos;
579 	pkt_info.pkt_len          = decoder->pkt_step;
580 	pkt_info.last_packet_type = decoder->last_packet_type;
581 	pkt_info.data             = data;
582 
583 	while (1) {
584 		do {
585 			pkt_info.pos += pkt_info.pkt_len;
586 			buf          += pkt_info.pkt_len;
587 			len          -= pkt_info.pkt_len;
588 
589 			if (!len)
590 				return INTEL_PT_NEED_MORE_BYTES;
591 
592 			ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
593 			if (!ret)
594 				return INTEL_PT_NEED_MORE_BYTES;
595 			if (ret < 0)
596 				return ret;
597 
598 			pkt_info.pkt_len = ret;
599 		} while (pkt_info.packet.type == INTEL_PT_PAD);
600 
601 		ret = cb(&pkt_info);
602 		if (ret)
603 			return 0;
604 
605 		pkt_info.last_packet_type = pkt_info.packet.type;
606 	}
607 }
608 
609 struct intel_pt_calc_cyc_to_tsc_info {
610 	uint64_t        cycle_cnt;
611 	unsigned int    cbr;
612 	uint32_t        last_mtc;
613 	uint64_t        ctc_timestamp;
614 	uint64_t        ctc_delta;
615 	uint64_t        tsc_timestamp;
616 	uint64_t        timestamp;
617 	bool            have_tma;
618 	bool            fixup_last_mtc;
619 	bool            from_mtc;
620 	double          cbr_cyc_to_tsc;
621 };
622 
623 /*
624  * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
625  * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
626  * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
627  * packet by copying the missing bits from the current MTC assuming the least
628  * difference between the two, and that the current MTC comes after last_mtc.
629  */
630 static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
631 				    uint32_t *last_mtc)
632 {
633 	uint32_t first_missing_bit = 1U << (16 - mtc_shift);
634 	uint32_t mask = ~(first_missing_bit - 1);
635 
636 	*last_mtc |= mtc & mask;
637 	if (*last_mtc >= mtc) {
638 		*last_mtc -= first_missing_bit;
639 		*last_mtc &= 0xff;
640 	}
641 }
642 
643 static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
644 {
645 	struct intel_pt_decoder *decoder = pkt_info->decoder;
646 	struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
647 	uint64_t timestamp;
648 	double cyc_to_tsc;
649 	unsigned int cbr;
650 	uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
651 
652 	switch (pkt_info->packet.type) {
653 	case INTEL_PT_TNT:
654 	case INTEL_PT_TIP_PGE:
655 	case INTEL_PT_TIP:
656 	case INTEL_PT_FUP:
657 	case INTEL_PT_PSB:
658 	case INTEL_PT_PIP:
659 	case INTEL_PT_MODE_EXEC:
660 	case INTEL_PT_MODE_TSX:
661 	case INTEL_PT_PSBEND:
662 	case INTEL_PT_PAD:
663 	case INTEL_PT_VMCS:
664 	case INTEL_PT_MNT:
665 	case INTEL_PT_PTWRITE:
666 	case INTEL_PT_PTWRITE_IP:
667 		return 0;
668 
669 	case INTEL_PT_MTC:
670 		if (!data->have_tma)
671 			return 0;
672 
673 		mtc = pkt_info->packet.payload;
674 		if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
675 			data->fixup_last_mtc = false;
676 			intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
677 						&data->last_mtc);
678 		}
679 		if (mtc > data->last_mtc)
680 			mtc_delta = mtc - data->last_mtc;
681 		else
682 			mtc_delta = mtc + 256 - data->last_mtc;
683 		data->ctc_delta += mtc_delta << decoder->mtc_shift;
684 		data->last_mtc = mtc;
685 
686 		if (decoder->tsc_ctc_mult) {
687 			timestamp = data->ctc_timestamp +
688 				data->ctc_delta * decoder->tsc_ctc_mult;
689 		} else {
690 			timestamp = data->ctc_timestamp +
691 				multdiv(data->ctc_delta,
692 					decoder->tsc_ctc_ratio_n,
693 					decoder->tsc_ctc_ratio_d);
694 		}
695 
696 		if (timestamp < data->timestamp)
697 			return 1;
698 
699 		if (pkt_info->last_packet_type != INTEL_PT_CYC) {
700 			data->timestamp = timestamp;
701 			return 0;
702 		}
703 
704 		break;
705 
706 	case INTEL_PT_TSC:
707 		/*
708 		 * For now, do not support using TSC packets - refer
709 		 * intel_pt_calc_cyc_to_tsc().
710 		 */
711 		if (data->from_mtc)
712 			return 1;
713 		timestamp = pkt_info->packet.payload |
714 			    (data->timestamp & (0xffULL << 56));
715 		if (data->from_mtc && timestamp < data->timestamp &&
716 		    data->timestamp - timestamp < decoder->tsc_slip)
717 			return 1;
718 		if (timestamp < data->timestamp)
719 			timestamp += (1ULL << 56);
720 		if (pkt_info->last_packet_type != INTEL_PT_CYC) {
721 			if (data->from_mtc)
722 				return 1;
723 			data->tsc_timestamp = timestamp;
724 			data->timestamp = timestamp;
725 			return 0;
726 		}
727 		break;
728 
729 	case INTEL_PT_TMA:
730 		if (data->from_mtc)
731 			return 1;
732 
733 		if (!decoder->tsc_ctc_ratio_d)
734 			return 0;
735 
736 		ctc = pkt_info->packet.payload;
737 		fc = pkt_info->packet.count;
738 		ctc_rem = ctc & decoder->ctc_rem_mask;
739 
740 		data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
741 
742 		data->ctc_timestamp = data->tsc_timestamp - fc;
743 		if (decoder->tsc_ctc_mult) {
744 			data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
745 		} else {
746 			data->ctc_timestamp -=
747 				multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
748 					decoder->tsc_ctc_ratio_d);
749 		}
750 
751 		data->ctc_delta = 0;
752 		data->have_tma = true;
753 		data->fixup_last_mtc = true;
754 
755 		return 0;
756 
757 	case INTEL_PT_CYC:
758 		data->cycle_cnt += pkt_info->packet.payload;
759 		return 0;
760 
761 	case INTEL_PT_CBR:
762 		cbr = pkt_info->packet.payload;
763 		if (data->cbr && data->cbr != cbr)
764 			return 1;
765 		data->cbr = cbr;
766 		data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
767 		return 0;
768 
769 	case INTEL_PT_TIP_PGD:
770 	case INTEL_PT_TRACESTOP:
771 	case INTEL_PT_EXSTOP:
772 	case INTEL_PT_EXSTOP_IP:
773 	case INTEL_PT_MWAIT:
774 	case INTEL_PT_PWRE:
775 	case INTEL_PT_PWRX:
776 	case INTEL_PT_OVF:
777 	case INTEL_PT_BAD: /* Does not happen */
778 	default:
779 		return 1;
780 	}
781 
782 	if (!data->cbr && decoder->cbr) {
783 		data->cbr = decoder->cbr;
784 		data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
785 	}
786 
787 	if (!data->cycle_cnt)
788 		return 1;
789 
790 	cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
791 
792 	if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
793 	    cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
794 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
795 			     cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
796 		return 1;
797 	}
798 
799 	decoder->calc_cyc_to_tsc = cyc_to_tsc;
800 	decoder->have_calc_cyc_to_tsc = true;
801 
802 	if (data->cbr) {
803 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
804 			     cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
805 	} else {
806 		intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
807 			     cyc_to_tsc, pkt_info->pos);
808 	}
809 
810 	return 1;
811 }
812 
813 static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
814 				     bool from_mtc)
815 {
816 	struct intel_pt_calc_cyc_to_tsc_info data = {
817 		.cycle_cnt      = 0,
818 		.cbr            = 0,
819 		.last_mtc       = decoder->last_mtc,
820 		.ctc_timestamp  = decoder->ctc_timestamp,
821 		.ctc_delta      = decoder->ctc_delta,
822 		.tsc_timestamp  = decoder->tsc_timestamp,
823 		.timestamp      = decoder->timestamp,
824 		.have_tma       = decoder->have_tma,
825 		.fixup_last_mtc = decoder->fixup_last_mtc,
826 		.from_mtc       = from_mtc,
827 		.cbr_cyc_to_tsc = 0,
828 	};
829 
830 	/*
831 	 * For now, do not support using TSC packets for at least the reasons:
832 	 * 1) timing might have stopped
833 	 * 2) TSC packets within PSB+ can slip against CYC packets
834 	 */
835 	if (!from_mtc)
836 		return;
837 
838 	intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
839 }
840 
841 static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
842 {
843 	int ret;
844 
845 	decoder->last_packet_type = decoder->packet.type;
846 
847 	do {
848 		decoder->pos += decoder->pkt_step;
849 		decoder->buf += decoder->pkt_step;
850 		decoder->len -= decoder->pkt_step;
851 
852 		if (!decoder->len) {
853 			ret = intel_pt_get_next_data(decoder);
854 			if (ret)
855 				return ret;
856 		}
857 
858 		ret = intel_pt_get_packet(decoder->buf, decoder->len,
859 					  &decoder->packet);
860 		if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
861 		    decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
862 			ret = intel_pt_get_split_packet(decoder);
863 			if (ret < 0)
864 				return ret;
865 		}
866 		if (ret <= 0)
867 			return intel_pt_bad_packet(decoder);
868 
869 		decoder->pkt_len = ret;
870 		decoder->pkt_step = ret;
871 		intel_pt_decoder_log_packet(decoder);
872 	} while (decoder->packet.type == INTEL_PT_PAD);
873 
874 	return 0;
875 }
876 
877 static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
878 {
879 	uint64_t timestamp, masked_timestamp;
880 
881 	timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
882 	masked_timestamp = timestamp & decoder->period_mask;
883 	if (decoder->continuous_period) {
884 		if (masked_timestamp > decoder->last_masked_timestamp)
885 			return 1;
886 	} else {
887 		timestamp += 1;
888 		masked_timestamp = timestamp & decoder->period_mask;
889 		if (masked_timestamp > decoder->last_masked_timestamp) {
890 			decoder->last_masked_timestamp = masked_timestamp;
891 			decoder->continuous_period = true;
892 		}
893 	}
894 
895 	if (masked_timestamp < decoder->last_masked_timestamp)
896 		return decoder->period_ticks;
897 
898 	return decoder->period_ticks - (timestamp - masked_timestamp);
899 }
900 
901 static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
902 {
903 	switch (decoder->period_type) {
904 	case INTEL_PT_PERIOD_INSTRUCTIONS:
905 		return decoder->period - decoder->period_insn_cnt;
906 	case INTEL_PT_PERIOD_TICKS:
907 		return intel_pt_next_period(decoder);
908 	case INTEL_PT_PERIOD_NONE:
909 	case INTEL_PT_PERIOD_MTC:
910 	default:
911 		return 0;
912 	}
913 }
914 
915 static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
916 {
917 	uint64_t timestamp, masked_timestamp;
918 
919 	switch (decoder->period_type) {
920 	case INTEL_PT_PERIOD_INSTRUCTIONS:
921 		decoder->period_insn_cnt = 0;
922 		break;
923 	case INTEL_PT_PERIOD_TICKS:
924 		timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
925 		masked_timestamp = timestamp & decoder->period_mask;
926 		if (masked_timestamp > decoder->last_masked_timestamp)
927 			decoder->last_masked_timestamp = masked_timestamp;
928 		else
929 			decoder->last_masked_timestamp += decoder->period_ticks;
930 		break;
931 	case INTEL_PT_PERIOD_NONE:
932 	case INTEL_PT_PERIOD_MTC:
933 	default:
934 		break;
935 	}
936 
937 	decoder->state.type |= INTEL_PT_INSTRUCTION;
938 }
939 
940 static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
941 			      struct intel_pt_insn *intel_pt_insn, uint64_t ip)
942 {
943 	uint64_t max_insn_cnt, insn_cnt = 0;
944 	int err;
945 
946 	if (!decoder->mtc_insn)
947 		decoder->mtc_insn = true;
948 
949 	max_insn_cnt = intel_pt_next_sample(decoder);
950 
951 	err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
952 				 max_insn_cnt, decoder->data);
953 
954 	decoder->tot_insn_cnt += insn_cnt;
955 	decoder->timestamp_insn_cnt += insn_cnt;
956 	decoder->sample_insn_cnt += insn_cnt;
957 	decoder->period_insn_cnt += insn_cnt;
958 
959 	if (err) {
960 		decoder->no_progress = 0;
961 		decoder->pkt_state = INTEL_PT_STATE_ERR2;
962 		intel_pt_log_at("ERROR: Failed to get instruction",
963 				decoder->ip);
964 		if (err == -ENOENT)
965 			return -ENOLINK;
966 		return -EILSEQ;
967 	}
968 
969 	if (ip && decoder->ip == ip) {
970 		err = -EAGAIN;
971 		goto out;
972 	}
973 
974 	if (max_insn_cnt && insn_cnt >= max_insn_cnt)
975 		intel_pt_sample_insn(decoder);
976 
977 	if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
978 		decoder->state.type = INTEL_PT_INSTRUCTION;
979 		decoder->state.from_ip = decoder->ip;
980 		decoder->state.to_ip = 0;
981 		decoder->ip += intel_pt_insn->length;
982 		err = INTEL_PT_RETURN;
983 		goto out;
984 	}
985 
986 	if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
987 		/* Zero-length calls are excluded */
988 		if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
989 		    intel_pt_insn->rel) {
990 			err = intel_pt_push(&decoder->stack, decoder->ip +
991 					    intel_pt_insn->length);
992 			if (err)
993 				goto out;
994 		}
995 	} else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
996 		decoder->ret_addr = intel_pt_pop(&decoder->stack);
997 	}
998 
999 	if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1000 		int cnt = decoder->no_progress++;
1001 
1002 		decoder->state.from_ip = decoder->ip;
1003 		decoder->ip += intel_pt_insn->length +
1004 				intel_pt_insn->rel;
1005 		decoder->state.to_ip = decoder->ip;
1006 		err = INTEL_PT_RETURN;
1007 
1008 		/*
1009 		 * Check for being stuck in a loop.  This can happen if a
1010 		 * decoder error results in the decoder erroneously setting the
1011 		 * ip to an address that is itself in an infinite loop that
1012 		 * consumes no packets.  When that happens, there must be an
1013 		 * unconditional branch.
1014 		 */
1015 		if (cnt) {
1016 			if (cnt == 1) {
1017 				decoder->stuck_ip = decoder->state.to_ip;
1018 				decoder->stuck_ip_prd = 1;
1019 				decoder->stuck_ip_cnt = 1;
1020 			} else if (cnt > INTEL_PT_MAX_LOOPS ||
1021 				   decoder->state.to_ip == decoder->stuck_ip) {
1022 				intel_pt_log_at("ERROR: Never-ending loop",
1023 						decoder->state.to_ip);
1024 				decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1025 				err = -ELOOP;
1026 				goto out;
1027 			} else if (!--decoder->stuck_ip_cnt) {
1028 				decoder->stuck_ip_prd += 1;
1029 				decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1030 				decoder->stuck_ip = decoder->state.to_ip;
1031 			}
1032 		}
1033 		goto out_no_progress;
1034 	}
1035 out:
1036 	decoder->no_progress = 0;
1037 out_no_progress:
1038 	decoder->state.insn_op = intel_pt_insn->op;
1039 	decoder->state.insn_len = intel_pt_insn->length;
1040 	memcpy(decoder->state.insn, intel_pt_insn->buf,
1041 	       INTEL_PT_INSN_BUF_SZ);
1042 
1043 	if (decoder->tx_flags & INTEL_PT_IN_TX)
1044 		decoder->state.flags |= INTEL_PT_IN_TX;
1045 
1046 	return err;
1047 }
1048 
1049 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1050 {
1051 	bool ret = false;
1052 
1053 	if (decoder->set_fup_tx_flags) {
1054 		decoder->set_fup_tx_flags = false;
1055 		decoder->tx_flags = decoder->fup_tx_flags;
1056 		decoder->state.type = INTEL_PT_TRANSACTION;
1057 		decoder->state.from_ip = decoder->ip;
1058 		decoder->state.to_ip = 0;
1059 		decoder->state.flags = decoder->fup_tx_flags;
1060 		return true;
1061 	}
1062 	if (decoder->set_fup_ptw) {
1063 		decoder->set_fup_ptw = false;
1064 		decoder->state.type = INTEL_PT_PTW;
1065 		decoder->state.flags |= INTEL_PT_FUP_IP;
1066 		decoder->state.from_ip = decoder->ip;
1067 		decoder->state.to_ip = 0;
1068 		decoder->state.ptw_payload = decoder->fup_ptw_payload;
1069 		return true;
1070 	}
1071 	if (decoder->set_fup_mwait) {
1072 		decoder->set_fup_mwait = false;
1073 		decoder->state.type = INTEL_PT_MWAIT_OP;
1074 		decoder->state.from_ip = decoder->ip;
1075 		decoder->state.to_ip = 0;
1076 		decoder->state.mwait_payload = decoder->fup_mwait_payload;
1077 		ret = true;
1078 	}
1079 	if (decoder->set_fup_pwre) {
1080 		decoder->set_fup_pwre = false;
1081 		decoder->state.type |= INTEL_PT_PWR_ENTRY;
1082 		decoder->state.type &= ~INTEL_PT_BRANCH;
1083 		decoder->state.from_ip = decoder->ip;
1084 		decoder->state.to_ip = 0;
1085 		decoder->state.pwre_payload = decoder->fup_pwre_payload;
1086 		ret = true;
1087 	}
1088 	if (decoder->set_fup_exstop) {
1089 		decoder->set_fup_exstop = false;
1090 		decoder->state.type |= INTEL_PT_EX_STOP;
1091 		decoder->state.type &= ~INTEL_PT_BRANCH;
1092 		decoder->state.flags |= INTEL_PT_FUP_IP;
1093 		decoder->state.from_ip = decoder->ip;
1094 		decoder->state.to_ip = 0;
1095 		ret = true;
1096 	}
1097 	return ret;
1098 }
1099 
1100 static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1101 					  struct intel_pt_insn *intel_pt_insn,
1102 					  uint64_t ip, int err)
1103 {
1104 	return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1105 	       intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1106 	       ip == decoder->ip + intel_pt_insn->length;
1107 }
1108 
1109 static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1110 {
1111 	struct intel_pt_insn intel_pt_insn;
1112 	uint64_t ip;
1113 	int err;
1114 
1115 	ip = decoder->last_ip;
1116 
1117 	while (1) {
1118 		err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1119 		if (err == INTEL_PT_RETURN)
1120 			return 0;
1121 		if (err == -EAGAIN ||
1122 		    intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
1123 			if (intel_pt_fup_event(decoder))
1124 				return 0;
1125 			return -EAGAIN;
1126 		}
1127 		decoder->set_fup_tx_flags = false;
1128 		if (err)
1129 			return err;
1130 
1131 		if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1132 			intel_pt_log_at("ERROR: Unexpected indirect branch",
1133 					decoder->ip);
1134 			decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1135 			return -ENOENT;
1136 		}
1137 
1138 		if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1139 			intel_pt_log_at("ERROR: Unexpected conditional branch",
1140 					decoder->ip);
1141 			decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1142 			return -ENOENT;
1143 		}
1144 
1145 		intel_pt_bug(decoder);
1146 	}
1147 }
1148 
1149 static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1150 {
1151 	struct intel_pt_insn intel_pt_insn;
1152 	int err;
1153 
1154 	err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1155 	if (err == INTEL_PT_RETURN &&
1156 	    decoder->pgd_ip &&
1157 	    decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1158 	    (decoder->state.type & INTEL_PT_BRANCH) &&
1159 	    decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1160 		/* Unconditional branch leaving filter region */
1161 		decoder->no_progress = 0;
1162 		decoder->pge = false;
1163 		decoder->continuous_period = false;
1164 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1165 		decoder->state.type |= INTEL_PT_TRACE_END;
1166 		return 0;
1167 	}
1168 	if (err == INTEL_PT_RETURN)
1169 		return 0;
1170 	if (err)
1171 		return err;
1172 
1173 	if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1174 		if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1175 			decoder->pge = false;
1176 			decoder->continuous_period = false;
1177 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1178 			decoder->state.from_ip = decoder->ip;
1179 			if (decoder->packet.count == 0) {
1180 				decoder->state.to_ip = 0;
1181 			} else {
1182 				decoder->state.to_ip = decoder->last_ip;
1183 				decoder->ip = decoder->last_ip;
1184 			}
1185 			decoder->state.type |= INTEL_PT_TRACE_END;
1186 		} else {
1187 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1188 			decoder->state.from_ip = decoder->ip;
1189 			if (decoder->packet.count == 0) {
1190 				decoder->state.to_ip = 0;
1191 			} else {
1192 				decoder->state.to_ip = decoder->last_ip;
1193 				decoder->ip = decoder->last_ip;
1194 			}
1195 		}
1196 		return 0;
1197 	}
1198 
1199 	if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1200 		uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1201 				 intel_pt_insn.rel;
1202 
1203 		if (decoder->pgd_ip &&
1204 		    decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1205 		    decoder->pgd_ip(to_ip, decoder->data)) {
1206 			/* Conditional branch leaving filter region */
1207 			decoder->pge = false;
1208 			decoder->continuous_period = false;
1209 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1210 			decoder->ip = to_ip;
1211 			decoder->state.from_ip = decoder->ip;
1212 			decoder->state.to_ip = to_ip;
1213 			decoder->state.type |= INTEL_PT_TRACE_END;
1214 			return 0;
1215 		}
1216 		intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1217 				decoder->ip);
1218 		decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1219 		return -ENOENT;
1220 	}
1221 
1222 	return intel_pt_bug(decoder);
1223 }
1224 
1225 static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1226 {
1227 	struct intel_pt_insn intel_pt_insn;
1228 	int err;
1229 
1230 	while (1) {
1231 		err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1232 		if (err == INTEL_PT_RETURN)
1233 			return 0;
1234 		if (err)
1235 			return err;
1236 
1237 		if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1238 			if (!decoder->return_compression) {
1239 				intel_pt_log_at("ERROR: RET when expecting conditional branch",
1240 						decoder->ip);
1241 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1242 				return -ENOENT;
1243 			}
1244 			if (!decoder->ret_addr) {
1245 				intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1246 						decoder->ip);
1247 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1248 				return -ENOENT;
1249 			}
1250 			if (!(decoder->tnt.payload & BIT63)) {
1251 				intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1252 						decoder->ip);
1253 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1254 				return -ENOENT;
1255 			}
1256 			decoder->tnt.count -= 1;
1257 			if (decoder->tnt.count)
1258 				decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1259 			else
1260 				decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1261 			decoder->tnt.payload <<= 1;
1262 			decoder->state.from_ip = decoder->ip;
1263 			decoder->ip = decoder->ret_addr;
1264 			decoder->state.to_ip = decoder->ip;
1265 			return 0;
1266 		}
1267 
1268 		if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1269 			/* Handle deferred TIPs */
1270 			err = intel_pt_get_next_packet(decoder);
1271 			if (err)
1272 				return err;
1273 			if (decoder->packet.type != INTEL_PT_TIP ||
1274 			    decoder->packet.count == 0) {
1275 				intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1276 						decoder->ip);
1277 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
1278 				decoder->pkt_step = 0;
1279 				return -ENOENT;
1280 			}
1281 			intel_pt_set_last_ip(decoder);
1282 			decoder->state.from_ip = decoder->ip;
1283 			decoder->state.to_ip = decoder->last_ip;
1284 			decoder->ip = decoder->last_ip;
1285 			return 0;
1286 		}
1287 
1288 		if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1289 			decoder->tnt.count -= 1;
1290 			if (decoder->tnt.count)
1291 				decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1292 			else
1293 				decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1294 			if (decoder->tnt.payload & BIT63) {
1295 				decoder->tnt.payload <<= 1;
1296 				decoder->state.from_ip = decoder->ip;
1297 				decoder->ip += intel_pt_insn.length +
1298 					       intel_pt_insn.rel;
1299 				decoder->state.to_ip = decoder->ip;
1300 				return 0;
1301 			}
1302 			/* Instruction sample for a non-taken branch */
1303 			if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1304 				decoder->tnt.payload <<= 1;
1305 				decoder->state.type = INTEL_PT_INSTRUCTION;
1306 				decoder->state.from_ip = decoder->ip;
1307 				decoder->state.to_ip = 0;
1308 				decoder->ip += intel_pt_insn.length;
1309 				return 0;
1310 			}
1311 			decoder->ip += intel_pt_insn.length;
1312 			if (!decoder->tnt.count) {
1313 				decoder->sample_timestamp = decoder->timestamp;
1314 				decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
1315 				return -EAGAIN;
1316 			}
1317 			decoder->tnt.payload <<= 1;
1318 			continue;
1319 		}
1320 
1321 		return intel_pt_bug(decoder);
1322 	}
1323 }
1324 
1325 static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1326 {
1327 	unsigned int fup_tx_flags;
1328 	int err;
1329 
1330 	fup_tx_flags = decoder->packet.payload &
1331 		       (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1332 	err = intel_pt_get_next_packet(decoder);
1333 	if (err)
1334 		return err;
1335 	if (decoder->packet.type == INTEL_PT_FUP) {
1336 		decoder->fup_tx_flags = fup_tx_flags;
1337 		decoder->set_fup_tx_flags = true;
1338 		if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1339 			*no_tip = true;
1340 	} else {
1341 		intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1342 				decoder->pos);
1343 		intel_pt_update_in_tx(decoder);
1344 	}
1345 	return 0;
1346 }
1347 
1348 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1349 {
1350 	uint64_t timestamp;
1351 
1352 	decoder->have_tma = false;
1353 
1354 	if (decoder->ref_timestamp) {
1355 		timestamp = decoder->packet.payload |
1356 			    (decoder->ref_timestamp & (0xffULL << 56));
1357 		if (timestamp < decoder->ref_timestamp) {
1358 			if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1359 				timestamp += (1ULL << 56);
1360 		} else {
1361 			if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1362 				timestamp -= (1ULL << 56);
1363 		}
1364 		decoder->tsc_timestamp = timestamp;
1365 		decoder->timestamp = timestamp;
1366 		decoder->ref_timestamp = 0;
1367 		decoder->timestamp_insn_cnt = 0;
1368 	} else if (decoder->timestamp) {
1369 		timestamp = decoder->packet.payload |
1370 			    (decoder->timestamp & (0xffULL << 56));
1371 		decoder->tsc_timestamp = timestamp;
1372 		if (timestamp < decoder->timestamp &&
1373 		    decoder->timestamp - timestamp < decoder->tsc_slip) {
1374 			intel_pt_log_to("Suppressing backwards timestamp",
1375 					timestamp);
1376 			timestamp = decoder->timestamp;
1377 		}
1378 		if (timestamp < decoder->timestamp) {
1379 			intel_pt_log_to("Wraparound timestamp", timestamp);
1380 			timestamp += (1ULL << 56);
1381 			decoder->tsc_timestamp = timestamp;
1382 		}
1383 		decoder->timestamp = timestamp;
1384 		decoder->timestamp_insn_cnt = 0;
1385 	}
1386 
1387 	if (decoder->last_packet_type == INTEL_PT_CYC) {
1388 		decoder->cyc_ref_timestamp = decoder->timestamp;
1389 		decoder->cycle_cnt = 0;
1390 		decoder->have_calc_cyc_to_tsc = false;
1391 		intel_pt_calc_cyc_to_tsc(decoder, false);
1392 	}
1393 
1394 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1395 }
1396 
1397 static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1398 {
1399 	intel_pt_log("ERROR: Buffer overflow\n");
1400 	intel_pt_clear_tx_flags(decoder);
1401 	decoder->timestamp_insn_cnt = 0;
1402 	decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1403 	decoder->overflow = true;
1404 	return -EOVERFLOW;
1405 }
1406 
1407 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1408 {
1409 	uint32_t ctc = decoder->packet.payload;
1410 	uint32_t fc = decoder->packet.count;
1411 	uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1412 
1413 	if (!decoder->tsc_ctc_ratio_d)
1414 		return;
1415 
1416 	decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1417 	decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1418 	if (decoder->tsc_ctc_mult) {
1419 		decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1420 	} else {
1421 		decoder->ctc_timestamp -= multdiv(ctc_rem,
1422 						  decoder->tsc_ctc_ratio_n,
1423 						  decoder->tsc_ctc_ratio_d);
1424 	}
1425 	decoder->ctc_delta = 0;
1426 	decoder->have_tma = true;
1427 	decoder->fixup_last_mtc = true;
1428 	intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x  CTC rem %#x\n",
1429 		     decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1430 }
1431 
1432 static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1433 {
1434 	uint64_t timestamp;
1435 	uint32_t mtc, mtc_delta;
1436 
1437 	if (!decoder->have_tma)
1438 		return;
1439 
1440 	mtc = decoder->packet.payload;
1441 
1442 	if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1443 		decoder->fixup_last_mtc = false;
1444 		intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1445 					&decoder->last_mtc);
1446 	}
1447 
1448 	if (mtc > decoder->last_mtc)
1449 		mtc_delta = mtc - decoder->last_mtc;
1450 	else
1451 		mtc_delta = mtc + 256 - decoder->last_mtc;
1452 
1453 	decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1454 
1455 	if (decoder->tsc_ctc_mult) {
1456 		timestamp = decoder->ctc_timestamp +
1457 			    decoder->ctc_delta * decoder->tsc_ctc_mult;
1458 	} else {
1459 		timestamp = decoder->ctc_timestamp +
1460 			    multdiv(decoder->ctc_delta,
1461 				    decoder->tsc_ctc_ratio_n,
1462 				    decoder->tsc_ctc_ratio_d);
1463 	}
1464 
1465 	if (timestamp < decoder->timestamp)
1466 		intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1467 			     timestamp, decoder->timestamp);
1468 	else
1469 		decoder->timestamp = timestamp;
1470 
1471 	decoder->timestamp_insn_cnt = 0;
1472 	decoder->last_mtc = mtc;
1473 
1474 	if (decoder->last_packet_type == INTEL_PT_CYC) {
1475 		decoder->cyc_ref_timestamp = decoder->timestamp;
1476 		decoder->cycle_cnt = 0;
1477 		decoder->have_calc_cyc_to_tsc = false;
1478 		intel_pt_calc_cyc_to_tsc(decoder, true);
1479 	}
1480 
1481 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1482 }
1483 
1484 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1485 {
1486 	unsigned int cbr = decoder->packet.payload & 0xff;
1487 
1488 	decoder->cbr_payload = decoder->packet.payload;
1489 
1490 	if (decoder->cbr == cbr)
1491 		return;
1492 
1493 	decoder->cbr = cbr;
1494 	decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1495 }
1496 
1497 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1498 {
1499 	uint64_t timestamp = decoder->cyc_ref_timestamp;
1500 
1501 	decoder->have_cyc = true;
1502 
1503 	decoder->cycle_cnt += decoder->packet.payload;
1504 
1505 	if (!decoder->cyc_ref_timestamp)
1506 		return;
1507 
1508 	if (decoder->have_calc_cyc_to_tsc)
1509 		timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1510 	else if (decoder->cbr)
1511 		timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1512 	else
1513 		return;
1514 
1515 	if (timestamp < decoder->timestamp)
1516 		intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1517 			     timestamp, decoder->timestamp);
1518 	else
1519 		decoder->timestamp = timestamp;
1520 
1521 	decoder->timestamp_insn_cnt = 0;
1522 
1523 	intel_pt_log_to("Setting timestamp", decoder->timestamp);
1524 }
1525 
1526 /* Walk PSB+ packets when already in sync. */
1527 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1528 {
1529 	int err;
1530 
1531 	while (1) {
1532 		err = intel_pt_get_next_packet(decoder);
1533 		if (err)
1534 			return err;
1535 
1536 		switch (decoder->packet.type) {
1537 		case INTEL_PT_PSBEND:
1538 			return 0;
1539 
1540 		case INTEL_PT_TIP_PGD:
1541 		case INTEL_PT_TIP_PGE:
1542 		case INTEL_PT_TIP:
1543 		case INTEL_PT_TNT:
1544 		case INTEL_PT_TRACESTOP:
1545 		case INTEL_PT_BAD:
1546 		case INTEL_PT_PSB:
1547 		case INTEL_PT_PTWRITE:
1548 		case INTEL_PT_PTWRITE_IP:
1549 		case INTEL_PT_EXSTOP:
1550 		case INTEL_PT_EXSTOP_IP:
1551 		case INTEL_PT_MWAIT:
1552 		case INTEL_PT_PWRE:
1553 		case INTEL_PT_PWRX:
1554 			decoder->have_tma = false;
1555 			intel_pt_log("ERROR: Unexpected packet\n");
1556 			return -EAGAIN;
1557 
1558 		case INTEL_PT_OVF:
1559 			return intel_pt_overflow(decoder);
1560 
1561 		case INTEL_PT_TSC:
1562 			intel_pt_calc_tsc_timestamp(decoder);
1563 			break;
1564 
1565 		case INTEL_PT_TMA:
1566 			intel_pt_calc_tma(decoder);
1567 			break;
1568 
1569 		case INTEL_PT_CBR:
1570 			intel_pt_calc_cbr(decoder);
1571 			break;
1572 
1573 		case INTEL_PT_MODE_EXEC:
1574 			decoder->exec_mode = decoder->packet.payload;
1575 			break;
1576 
1577 		case INTEL_PT_PIP:
1578 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1579 			break;
1580 
1581 		case INTEL_PT_FUP:
1582 			decoder->pge = true;
1583 			if (decoder->packet.count)
1584 				intel_pt_set_last_ip(decoder);
1585 			break;
1586 
1587 		case INTEL_PT_MODE_TSX:
1588 			intel_pt_update_in_tx(decoder);
1589 			break;
1590 
1591 		case INTEL_PT_MTC:
1592 			intel_pt_calc_mtc_timestamp(decoder);
1593 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1594 				decoder->state.type |= INTEL_PT_INSTRUCTION;
1595 			break;
1596 
1597 		case INTEL_PT_CYC:
1598 		case INTEL_PT_VMCS:
1599 		case INTEL_PT_MNT:
1600 		case INTEL_PT_PAD:
1601 		default:
1602 			break;
1603 		}
1604 	}
1605 }
1606 
1607 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1608 {
1609 	int err;
1610 
1611 	if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1612 		decoder->tx_flags = 0;
1613 		decoder->state.flags &= ~INTEL_PT_IN_TX;
1614 		decoder->state.flags |= INTEL_PT_ABORT_TX;
1615 	} else {
1616 		decoder->state.flags |= INTEL_PT_ASYNC;
1617 	}
1618 
1619 	while (1) {
1620 		err = intel_pt_get_next_packet(decoder);
1621 		if (err)
1622 			return err;
1623 
1624 		switch (decoder->packet.type) {
1625 		case INTEL_PT_TNT:
1626 		case INTEL_PT_FUP:
1627 		case INTEL_PT_TRACESTOP:
1628 		case INTEL_PT_PSB:
1629 		case INTEL_PT_TSC:
1630 		case INTEL_PT_TMA:
1631 		case INTEL_PT_MODE_TSX:
1632 		case INTEL_PT_BAD:
1633 		case INTEL_PT_PSBEND:
1634 		case INTEL_PT_PTWRITE:
1635 		case INTEL_PT_PTWRITE_IP:
1636 		case INTEL_PT_EXSTOP:
1637 		case INTEL_PT_EXSTOP_IP:
1638 		case INTEL_PT_MWAIT:
1639 		case INTEL_PT_PWRE:
1640 		case INTEL_PT_PWRX:
1641 			intel_pt_log("ERROR: Missing TIP after FUP\n");
1642 			decoder->pkt_state = INTEL_PT_STATE_ERR3;
1643 			decoder->pkt_step = 0;
1644 			return -ENOENT;
1645 
1646 		case INTEL_PT_CBR:
1647 			intel_pt_calc_cbr(decoder);
1648 			break;
1649 
1650 		case INTEL_PT_OVF:
1651 			return intel_pt_overflow(decoder);
1652 
1653 		case INTEL_PT_TIP_PGD:
1654 			decoder->state.from_ip = decoder->ip;
1655 			if (decoder->packet.count == 0) {
1656 				decoder->state.to_ip = 0;
1657 			} else {
1658 				intel_pt_set_ip(decoder);
1659 				decoder->state.to_ip = decoder->ip;
1660 			}
1661 			decoder->pge = false;
1662 			decoder->continuous_period = false;
1663 			decoder->state.type |= INTEL_PT_TRACE_END;
1664 			return 0;
1665 
1666 		case INTEL_PT_TIP_PGE:
1667 			decoder->pge = true;
1668 			intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1669 				     decoder->ip);
1670 			decoder->state.from_ip = 0;
1671 			if (decoder->packet.count == 0) {
1672 				decoder->state.to_ip = 0;
1673 			} else {
1674 				intel_pt_set_ip(decoder);
1675 				decoder->state.to_ip = decoder->ip;
1676 			}
1677 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1678 			return 0;
1679 
1680 		case INTEL_PT_TIP:
1681 			decoder->state.from_ip = decoder->ip;
1682 			if (decoder->packet.count == 0) {
1683 				decoder->state.to_ip = 0;
1684 			} else {
1685 				intel_pt_set_ip(decoder);
1686 				decoder->state.to_ip = decoder->ip;
1687 			}
1688 			return 0;
1689 
1690 		case INTEL_PT_PIP:
1691 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1692 			break;
1693 
1694 		case INTEL_PT_MTC:
1695 			intel_pt_calc_mtc_timestamp(decoder);
1696 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1697 				decoder->state.type |= INTEL_PT_INSTRUCTION;
1698 			break;
1699 
1700 		case INTEL_PT_CYC:
1701 			intel_pt_calc_cyc_timestamp(decoder);
1702 			break;
1703 
1704 		case INTEL_PT_MODE_EXEC:
1705 			decoder->exec_mode = decoder->packet.payload;
1706 			break;
1707 
1708 		case INTEL_PT_VMCS:
1709 		case INTEL_PT_MNT:
1710 		case INTEL_PT_PAD:
1711 			break;
1712 
1713 		default:
1714 			return intel_pt_bug(decoder);
1715 		}
1716 	}
1717 }
1718 
1719 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1720 {
1721 	bool no_tip = false;
1722 	int err;
1723 
1724 	while (1) {
1725 		err = intel_pt_get_next_packet(decoder);
1726 		if (err)
1727 			return err;
1728 next:
1729 		switch (decoder->packet.type) {
1730 		case INTEL_PT_TNT:
1731 			if (!decoder->packet.count)
1732 				break;
1733 			decoder->tnt = decoder->packet;
1734 			decoder->pkt_state = INTEL_PT_STATE_TNT;
1735 			err = intel_pt_walk_tnt(decoder);
1736 			if (err == -EAGAIN)
1737 				break;
1738 			return err;
1739 
1740 		case INTEL_PT_TIP_PGD:
1741 			if (decoder->packet.count != 0)
1742 				intel_pt_set_last_ip(decoder);
1743 			decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1744 			return intel_pt_walk_tip(decoder);
1745 
1746 		case INTEL_PT_TIP_PGE: {
1747 			decoder->pge = true;
1748 			if (decoder->packet.count == 0) {
1749 				intel_pt_log_at("Skipping zero TIP.PGE",
1750 						decoder->pos);
1751 				break;
1752 			}
1753 			intel_pt_set_ip(decoder);
1754 			decoder->state.from_ip = 0;
1755 			decoder->state.to_ip = decoder->ip;
1756 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1757 			return 0;
1758 		}
1759 
1760 		case INTEL_PT_OVF:
1761 			return intel_pt_overflow(decoder);
1762 
1763 		case INTEL_PT_TIP:
1764 			if (decoder->packet.count != 0)
1765 				intel_pt_set_last_ip(decoder);
1766 			decoder->pkt_state = INTEL_PT_STATE_TIP;
1767 			return intel_pt_walk_tip(decoder);
1768 
1769 		case INTEL_PT_FUP:
1770 			if (decoder->packet.count == 0) {
1771 				intel_pt_log_at("Skipping zero FUP",
1772 						decoder->pos);
1773 				no_tip = false;
1774 				break;
1775 			}
1776 			intel_pt_set_last_ip(decoder);
1777 			if (!decoder->branch_enable) {
1778 				decoder->ip = decoder->last_ip;
1779 				if (intel_pt_fup_event(decoder))
1780 					return 0;
1781 				no_tip = false;
1782 				break;
1783 			}
1784 			if (decoder->set_fup_mwait)
1785 				no_tip = true;
1786 			err = intel_pt_walk_fup(decoder);
1787 			if (err != -EAGAIN) {
1788 				if (err)
1789 					return err;
1790 				if (no_tip)
1791 					decoder->pkt_state =
1792 						INTEL_PT_STATE_FUP_NO_TIP;
1793 				else
1794 					decoder->pkt_state = INTEL_PT_STATE_FUP;
1795 				return 0;
1796 			}
1797 			if (no_tip) {
1798 				no_tip = false;
1799 				break;
1800 			}
1801 			return intel_pt_walk_fup_tip(decoder);
1802 
1803 		case INTEL_PT_TRACESTOP:
1804 			decoder->pge = false;
1805 			decoder->continuous_period = false;
1806 			intel_pt_clear_tx_flags(decoder);
1807 			decoder->have_tma = false;
1808 			break;
1809 
1810 		case INTEL_PT_PSB:
1811 			decoder->last_ip = 0;
1812 			decoder->have_last_ip = true;
1813 			intel_pt_clear_stack(&decoder->stack);
1814 			err = intel_pt_walk_psbend(decoder);
1815 			if (err == -EAGAIN)
1816 				goto next;
1817 			if (err)
1818 				return err;
1819 			break;
1820 
1821 		case INTEL_PT_PIP:
1822 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1823 			break;
1824 
1825 		case INTEL_PT_MTC:
1826 			intel_pt_calc_mtc_timestamp(decoder);
1827 			if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1828 				break;
1829 			/*
1830 			 * Ensure that there has been an instruction since the
1831 			 * last MTC.
1832 			 */
1833 			if (!decoder->mtc_insn)
1834 				break;
1835 			decoder->mtc_insn = false;
1836 			/* Ensure that there is a timestamp */
1837 			if (!decoder->timestamp)
1838 				break;
1839 			decoder->state.type = INTEL_PT_INSTRUCTION;
1840 			decoder->state.from_ip = decoder->ip;
1841 			decoder->state.to_ip = 0;
1842 			decoder->mtc_insn = false;
1843 			return 0;
1844 
1845 		case INTEL_PT_TSC:
1846 			intel_pt_calc_tsc_timestamp(decoder);
1847 			break;
1848 
1849 		case INTEL_PT_TMA:
1850 			intel_pt_calc_tma(decoder);
1851 			break;
1852 
1853 		case INTEL_PT_CYC:
1854 			intel_pt_calc_cyc_timestamp(decoder);
1855 			break;
1856 
1857 		case INTEL_PT_CBR:
1858 			intel_pt_calc_cbr(decoder);
1859 			if (!decoder->branch_enable &&
1860 			    decoder->cbr != decoder->cbr_seen) {
1861 				decoder->cbr_seen = decoder->cbr;
1862 				decoder->state.type = INTEL_PT_CBR_CHG;
1863 				decoder->state.from_ip = decoder->ip;
1864 				decoder->state.to_ip = 0;
1865 				decoder->state.cbr_payload =
1866 							decoder->packet.payload;
1867 				return 0;
1868 			}
1869 			break;
1870 
1871 		case INTEL_PT_MODE_EXEC:
1872 			decoder->exec_mode = decoder->packet.payload;
1873 			break;
1874 
1875 		case INTEL_PT_MODE_TSX:
1876 			/* MODE_TSX need not be followed by FUP */
1877 			if (!decoder->pge) {
1878 				intel_pt_update_in_tx(decoder);
1879 				break;
1880 			}
1881 			err = intel_pt_mode_tsx(decoder, &no_tip);
1882 			if (err)
1883 				return err;
1884 			goto next;
1885 
1886 		case INTEL_PT_BAD: /* Does not happen */
1887 			return intel_pt_bug(decoder);
1888 
1889 		case INTEL_PT_PSBEND:
1890 		case INTEL_PT_VMCS:
1891 		case INTEL_PT_MNT:
1892 		case INTEL_PT_PAD:
1893 			break;
1894 
1895 		case INTEL_PT_PTWRITE_IP:
1896 			decoder->fup_ptw_payload = decoder->packet.payload;
1897 			err = intel_pt_get_next_packet(decoder);
1898 			if (err)
1899 				return err;
1900 			if (decoder->packet.type == INTEL_PT_FUP) {
1901 				decoder->set_fup_ptw = true;
1902 				no_tip = true;
1903 			} else {
1904 				intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1905 						decoder->pos);
1906 			}
1907 			goto next;
1908 
1909 		case INTEL_PT_PTWRITE:
1910 			decoder->state.type = INTEL_PT_PTW;
1911 			decoder->state.from_ip = decoder->ip;
1912 			decoder->state.to_ip = 0;
1913 			decoder->state.ptw_payload = decoder->packet.payload;
1914 			return 0;
1915 
1916 		case INTEL_PT_MWAIT:
1917 			decoder->fup_mwait_payload = decoder->packet.payload;
1918 			decoder->set_fup_mwait = true;
1919 			break;
1920 
1921 		case INTEL_PT_PWRE:
1922 			if (decoder->set_fup_mwait) {
1923 				decoder->fup_pwre_payload =
1924 							decoder->packet.payload;
1925 				decoder->set_fup_pwre = true;
1926 				break;
1927 			}
1928 			decoder->state.type = INTEL_PT_PWR_ENTRY;
1929 			decoder->state.from_ip = decoder->ip;
1930 			decoder->state.to_ip = 0;
1931 			decoder->state.pwrx_payload = decoder->packet.payload;
1932 			return 0;
1933 
1934 		case INTEL_PT_EXSTOP_IP:
1935 			err = intel_pt_get_next_packet(decoder);
1936 			if (err)
1937 				return err;
1938 			if (decoder->packet.type == INTEL_PT_FUP) {
1939 				decoder->set_fup_exstop = true;
1940 				no_tip = true;
1941 			} else {
1942 				intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
1943 						decoder->pos);
1944 			}
1945 			goto next;
1946 
1947 		case INTEL_PT_EXSTOP:
1948 			decoder->state.type = INTEL_PT_EX_STOP;
1949 			decoder->state.from_ip = decoder->ip;
1950 			decoder->state.to_ip = 0;
1951 			return 0;
1952 
1953 		case INTEL_PT_PWRX:
1954 			decoder->state.type = INTEL_PT_PWR_EXIT;
1955 			decoder->state.from_ip = decoder->ip;
1956 			decoder->state.to_ip = 0;
1957 			decoder->state.pwrx_payload = decoder->packet.payload;
1958 			return 0;
1959 
1960 		default:
1961 			return intel_pt_bug(decoder);
1962 		}
1963 	}
1964 }
1965 
1966 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1967 {
1968 	return decoder->packet.count &&
1969 	       (decoder->have_last_ip || decoder->packet.count == 3 ||
1970 		decoder->packet.count == 6);
1971 }
1972 
1973 /* Walk PSB+ packets to get in sync. */
1974 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1975 {
1976 	int err;
1977 
1978 	while (1) {
1979 		err = intel_pt_get_next_packet(decoder);
1980 		if (err)
1981 			return err;
1982 
1983 		switch (decoder->packet.type) {
1984 		case INTEL_PT_TIP_PGD:
1985 			decoder->continuous_period = false;
1986 			__fallthrough;
1987 		case INTEL_PT_TIP_PGE:
1988 		case INTEL_PT_TIP:
1989 		case INTEL_PT_PTWRITE:
1990 		case INTEL_PT_PTWRITE_IP:
1991 		case INTEL_PT_EXSTOP:
1992 		case INTEL_PT_EXSTOP_IP:
1993 		case INTEL_PT_MWAIT:
1994 		case INTEL_PT_PWRE:
1995 		case INTEL_PT_PWRX:
1996 			intel_pt_log("ERROR: Unexpected packet\n");
1997 			return -ENOENT;
1998 
1999 		case INTEL_PT_FUP:
2000 			decoder->pge = true;
2001 			if (intel_pt_have_ip(decoder)) {
2002 				uint64_t current_ip = decoder->ip;
2003 
2004 				intel_pt_set_ip(decoder);
2005 				if (current_ip)
2006 					intel_pt_log_to("Setting IP",
2007 							decoder->ip);
2008 			}
2009 			break;
2010 
2011 		case INTEL_PT_MTC:
2012 			intel_pt_calc_mtc_timestamp(decoder);
2013 			break;
2014 
2015 		case INTEL_PT_TSC:
2016 			intel_pt_calc_tsc_timestamp(decoder);
2017 			break;
2018 
2019 		case INTEL_PT_TMA:
2020 			intel_pt_calc_tma(decoder);
2021 			break;
2022 
2023 		case INTEL_PT_CYC:
2024 			intel_pt_calc_cyc_timestamp(decoder);
2025 			break;
2026 
2027 		case INTEL_PT_CBR:
2028 			intel_pt_calc_cbr(decoder);
2029 			break;
2030 
2031 		case INTEL_PT_PIP:
2032 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2033 			break;
2034 
2035 		case INTEL_PT_MODE_EXEC:
2036 			decoder->exec_mode = decoder->packet.payload;
2037 			break;
2038 
2039 		case INTEL_PT_MODE_TSX:
2040 			intel_pt_update_in_tx(decoder);
2041 			break;
2042 
2043 		case INTEL_PT_TRACESTOP:
2044 			decoder->pge = false;
2045 			decoder->continuous_period = false;
2046 			intel_pt_clear_tx_flags(decoder);
2047 			__fallthrough;
2048 
2049 		case INTEL_PT_TNT:
2050 			decoder->have_tma = false;
2051 			intel_pt_log("ERROR: Unexpected packet\n");
2052 			if (decoder->ip)
2053 				decoder->pkt_state = INTEL_PT_STATE_ERR4;
2054 			else
2055 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
2056 			return -ENOENT;
2057 
2058 		case INTEL_PT_BAD: /* Does not happen */
2059 			return intel_pt_bug(decoder);
2060 
2061 		case INTEL_PT_OVF:
2062 			return intel_pt_overflow(decoder);
2063 
2064 		case INTEL_PT_PSBEND:
2065 			return 0;
2066 
2067 		case INTEL_PT_PSB:
2068 		case INTEL_PT_VMCS:
2069 		case INTEL_PT_MNT:
2070 		case INTEL_PT_PAD:
2071 		default:
2072 			break;
2073 		}
2074 	}
2075 }
2076 
2077 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2078 {
2079 	int err;
2080 
2081 	while (1) {
2082 		err = intel_pt_get_next_packet(decoder);
2083 		if (err)
2084 			return err;
2085 
2086 		switch (decoder->packet.type) {
2087 		case INTEL_PT_TIP_PGD:
2088 			decoder->continuous_period = false;
2089 			__fallthrough;
2090 		case INTEL_PT_TIP_PGE:
2091 		case INTEL_PT_TIP:
2092 			decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
2093 			if (intel_pt_have_ip(decoder))
2094 				intel_pt_set_ip(decoder);
2095 			if (!decoder->ip)
2096 				break;
2097 			if (decoder->packet.type == INTEL_PT_TIP_PGE)
2098 				decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2099 			if (decoder->packet.type == INTEL_PT_TIP_PGD)
2100 				decoder->state.type |= INTEL_PT_TRACE_END;
2101 			return 0;
2102 
2103 		case INTEL_PT_FUP:
2104 			if (intel_pt_have_ip(decoder))
2105 				intel_pt_set_ip(decoder);
2106 			if (decoder->ip)
2107 				return 0;
2108 			break;
2109 
2110 		case INTEL_PT_MTC:
2111 			intel_pt_calc_mtc_timestamp(decoder);
2112 			break;
2113 
2114 		case INTEL_PT_TSC:
2115 			intel_pt_calc_tsc_timestamp(decoder);
2116 			break;
2117 
2118 		case INTEL_PT_TMA:
2119 			intel_pt_calc_tma(decoder);
2120 			break;
2121 
2122 		case INTEL_PT_CYC:
2123 			intel_pt_calc_cyc_timestamp(decoder);
2124 			break;
2125 
2126 		case INTEL_PT_CBR:
2127 			intel_pt_calc_cbr(decoder);
2128 			break;
2129 
2130 		case INTEL_PT_PIP:
2131 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2132 			break;
2133 
2134 		case INTEL_PT_MODE_EXEC:
2135 			decoder->exec_mode = decoder->packet.payload;
2136 			break;
2137 
2138 		case INTEL_PT_MODE_TSX:
2139 			intel_pt_update_in_tx(decoder);
2140 			break;
2141 
2142 		case INTEL_PT_OVF:
2143 			return intel_pt_overflow(decoder);
2144 
2145 		case INTEL_PT_BAD: /* Does not happen */
2146 			return intel_pt_bug(decoder);
2147 
2148 		case INTEL_PT_TRACESTOP:
2149 			decoder->pge = false;
2150 			decoder->continuous_period = false;
2151 			intel_pt_clear_tx_flags(decoder);
2152 			decoder->have_tma = false;
2153 			break;
2154 
2155 		case INTEL_PT_PSB:
2156 			decoder->last_ip = 0;
2157 			decoder->have_last_ip = true;
2158 			intel_pt_clear_stack(&decoder->stack);
2159 			err = intel_pt_walk_psb(decoder);
2160 			if (err)
2161 				return err;
2162 			if (decoder->ip) {
2163 				/* Do not have a sample */
2164 				decoder->state.type = 0;
2165 				return 0;
2166 			}
2167 			break;
2168 
2169 		case INTEL_PT_TNT:
2170 		case INTEL_PT_PSBEND:
2171 		case INTEL_PT_VMCS:
2172 		case INTEL_PT_MNT:
2173 		case INTEL_PT_PAD:
2174 		case INTEL_PT_PTWRITE:
2175 		case INTEL_PT_PTWRITE_IP:
2176 		case INTEL_PT_EXSTOP:
2177 		case INTEL_PT_EXSTOP_IP:
2178 		case INTEL_PT_MWAIT:
2179 		case INTEL_PT_PWRE:
2180 		case INTEL_PT_PWRX:
2181 		default:
2182 			break;
2183 		}
2184 	}
2185 }
2186 
2187 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2188 {
2189 	int err;
2190 
2191 	decoder->set_fup_tx_flags = false;
2192 	decoder->set_fup_ptw = false;
2193 	decoder->set_fup_mwait = false;
2194 	decoder->set_fup_pwre = false;
2195 	decoder->set_fup_exstop = false;
2196 
2197 	if (!decoder->branch_enable) {
2198 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2199 		decoder->overflow = false;
2200 		decoder->state.type = 0; /* Do not have a sample */
2201 		return 0;
2202 	}
2203 
2204 	intel_pt_log("Scanning for full IP\n");
2205 	err = intel_pt_walk_to_ip(decoder);
2206 	if (err)
2207 		return err;
2208 
2209 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2210 	decoder->overflow = false;
2211 
2212 	decoder->state.from_ip = 0;
2213 	decoder->state.to_ip = decoder->ip;
2214 	intel_pt_log_to("Setting IP", decoder->ip);
2215 
2216 	return 0;
2217 }
2218 
2219 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2220 {
2221 	const unsigned char *end = decoder->buf + decoder->len;
2222 	size_t i;
2223 
2224 	for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2225 		if (i > decoder->len)
2226 			continue;
2227 		if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2228 			return i;
2229 	}
2230 	return 0;
2231 }
2232 
2233 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2234 {
2235 	size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2236 	const char *psb = INTEL_PT_PSB_STR;
2237 
2238 	if (rest_psb > decoder->len ||
2239 	    memcmp(decoder->buf, psb + part_psb, rest_psb))
2240 		return 0;
2241 
2242 	return rest_psb;
2243 }
2244 
2245 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2246 				  int part_psb)
2247 {
2248 	int rest_psb, ret;
2249 
2250 	decoder->pos += decoder->len;
2251 	decoder->len = 0;
2252 
2253 	ret = intel_pt_get_next_data(decoder);
2254 	if (ret)
2255 		return ret;
2256 
2257 	rest_psb = intel_pt_rest_psb(decoder, part_psb);
2258 	if (!rest_psb)
2259 		return 0;
2260 
2261 	decoder->pos -= part_psb;
2262 	decoder->next_buf = decoder->buf + rest_psb;
2263 	decoder->next_len = decoder->len - rest_psb;
2264 	memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2265 	decoder->buf = decoder->temp_buf;
2266 	decoder->len = INTEL_PT_PSB_LEN;
2267 
2268 	return 0;
2269 }
2270 
2271 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2272 {
2273 	unsigned char *next;
2274 	int ret;
2275 
2276 	intel_pt_log("Scanning for PSB\n");
2277 	while (1) {
2278 		if (!decoder->len) {
2279 			ret = intel_pt_get_next_data(decoder);
2280 			if (ret)
2281 				return ret;
2282 		}
2283 
2284 		next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2285 			      INTEL_PT_PSB_LEN);
2286 		if (!next) {
2287 			int part_psb;
2288 
2289 			part_psb = intel_pt_part_psb(decoder);
2290 			if (part_psb) {
2291 				ret = intel_pt_get_split_psb(decoder, part_psb);
2292 				if (ret)
2293 					return ret;
2294 			} else {
2295 				decoder->pos += decoder->len;
2296 				decoder->len = 0;
2297 			}
2298 			continue;
2299 		}
2300 
2301 		decoder->pkt_step = next - decoder->buf;
2302 		return intel_pt_get_next_packet(decoder);
2303 	}
2304 }
2305 
2306 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2307 {
2308 	int err;
2309 
2310 	decoder->pge = false;
2311 	decoder->continuous_period = false;
2312 	decoder->have_last_ip = false;
2313 	decoder->last_ip = 0;
2314 	decoder->ip = 0;
2315 	intel_pt_clear_stack(&decoder->stack);
2316 
2317 	err = intel_pt_scan_for_psb(decoder);
2318 	if (err)
2319 		return err;
2320 
2321 	decoder->have_last_ip = true;
2322 	decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2323 
2324 	err = intel_pt_walk_psb(decoder);
2325 	if (err)
2326 		return err;
2327 
2328 	if (decoder->ip) {
2329 		decoder->state.type = 0; /* Do not have a sample */
2330 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2331 	} else {
2332 		return intel_pt_sync_ip(decoder);
2333 	}
2334 
2335 	return 0;
2336 }
2337 
2338 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2339 {
2340 	uint64_t est = decoder->sample_insn_cnt << 1;
2341 
2342 	if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2343 		goto out;
2344 
2345 	est *= decoder->max_non_turbo_ratio;
2346 	est /= decoder->cbr;
2347 out:
2348 	return decoder->sample_timestamp + est;
2349 }
2350 
2351 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2352 {
2353 	int err;
2354 
2355 	do {
2356 		decoder->state.type = INTEL_PT_BRANCH;
2357 		decoder->state.flags = 0;
2358 
2359 		switch (decoder->pkt_state) {
2360 		case INTEL_PT_STATE_NO_PSB:
2361 			err = intel_pt_sync(decoder);
2362 			break;
2363 		case INTEL_PT_STATE_NO_IP:
2364 			decoder->have_last_ip = false;
2365 			decoder->last_ip = 0;
2366 			decoder->ip = 0;
2367 			__fallthrough;
2368 		case INTEL_PT_STATE_ERR_RESYNC:
2369 			err = intel_pt_sync_ip(decoder);
2370 			break;
2371 		case INTEL_PT_STATE_IN_SYNC:
2372 			err = intel_pt_walk_trace(decoder);
2373 			break;
2374 		case INTEL_PT_STATE_TNT:
2375 		case INTEL_PT_STATE_TNT_CONT:
2376 			err = intel_pt_walk_tnt(decoder);
2377 			if (err == -EAGAIN)
2378 				err = intel_pt_walk_trace(decoder);
2379 			break;
2380 		case INTEL_PT_STATE_TIP:
2381 		case INTEL_PT_STATE_TIP_PGD:
2382 			err = intel_pt_walk_tip(decoder);
2383 			break;
2384 		case INTEL_PT_STATE_FUP:
2385 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2386 			err = intel_pt_walk_fup(decoder);
2387 			if (err == -EAGAIN)
2388 				err = intel_pt_walk_fup_tip(decoder);
2389 			else if (!err)
2390 				decoder->pkt_state = INTEL_PT_STATE_FUP;
2391 			break;
2392 		case INTEL_PT_STATE_FUP_NO_TIP:
2393 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2394 			err = intel_pt_walk_fup(decoder);
2395 			if (err == -EAGAIN)
2396 				err = intel_pt_walk_trace(decoder);
2397 			break;
2398 		default:
2399 			err = intel_pt_bug(decoder);
2400 			break;
2401 		}
2402 	} while (err == -ENOLINK);
2403 
2404 	if (err) {
2405 		decoder->state.err = intel_pt_ext_err(err);
2406 		decoder->state.from_ip = decoder->ip;
2407 		decoder->sample_timestamp = decoder->timestamp;
2408 		decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2409 	} else {
2410 		decoder->state.err = 0;
2411 		if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2412 			decoder->cbr_seen = decoder->cbr;
2413 			decoder->state.type |= INTEL_PT_CBR_CHG;
2414 			decoder->state.cbr_payload = decoder->cbr_payload;
2415 		}
2416 		if (intel_pt_sample_time(decoder->pkt_state)) {
2417 			decoder->sample_timestamp = decoder->timestamp;
2418 			decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2419 		}
2420 	}
2421 
2422 	decoder->state.timestamp = decoder->sample_timestamp;
2423 	decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2424 	decoder->state.cr3 = decoder->cr3;
2425 	decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2426 
2427 	return &decoder->state;
2428 }
2429 
2430 /**
2431  * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2432  * @buf: pointer to buffer pointer
2433  * @len: size of buffer
2434  *
2435  * Updates the buffer pointer to point to the start of the next PSB packet if
2436  * there is one, otherwise the buffer pointer is unchanged.  If @buf is updated,
2437  * @len is adjusted accordingly.
2438  *
2439  * Return: %true if a PSB packet is found, %false otherwise.
2440  */
2441 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2442 {
2443 	unsigned char *next;
2444 
2445 	next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2446 	if (next) {
2447 		*len -= next - *buf;
2448 		*buf = next;
2449 		return true;
2450 	}
2451 	return false;
2452 }
2453 
2454 /**
2455  * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2456  *                     packet.
2457  * @buf: pointer to buffer pointer
2458  * @len: size of buffer
2459  *
2460  * Updates the buffer pointer to point to the start of the following PSB packet
2461  * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2462  * pointer is unchanged.  If @buf is updated, @len is adjusted accordingly.
2463  *
2464  * Return: %true if a PSB packet is found, %false otherwise.
2465  */
2466 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2467 {
2468 	unsigned char *next;
2469 
2470 	if (!*len)
2471 		return false;
2472 
2473 	next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2474 	if (next) {
2475 		*len -= next - *buf;
2476 		*buf = next;
2477 		return true;
2478 	}
2479 	return false;
2480 }
2481 
2482 /**
2483  * intel_pt_last_psb - find the last PSB packet in a buffer.
2484  * @buf: buffer
2485  * @len: size of buffer
2486  *
2487  * This function finds the last PSB in a buffer.
2488  *
2489  * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2490  */
2491 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2492 {
2493 	const char *n = INTEL_PT_PSB_STR;
2494 	unsigned char *p;
2495 	size_t k;
2496 
2497 	if (len < INTEL_PT_PSB_LEN)
2498 		return NULL;
2499 
2500 	k = len - INTEL_PT_PSB_LEN + 1;
2501 	while (1) {
2502 		p = memrchr(buf, n[0], k);
2503 		if (!p)
2504 			return NULL;
2505 		if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2506 			return p;
2507 		k = p - buf;
2508 		if (!k)
2509 			return NULL;
2510 	}
2511 }
2512 
2513 /**
2514  * intel_pt_next_tsc - find and return next TSC.
2515  * @buf: buffer
2516  * @len: size of buffer
2517  * @tsc: TSC value returned
2518  * @rem: returns remaining size when TSC is found
2519  *
2520  * Find a TSC packet in @buf and return the TSC value.  This function assumes
2521  * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2522  * PSBEND packet is found.
2523  *
2524  * Return: %true if TSC is found, false otherwise.
2525  */
2526 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2527 			      size_t *rem)
2528 {
2529 	struct intel_pt_pkt packet;
2530 	int ret;
2531 
2532 	while (len) {
2533 		ret = intel_pt_get_packet(buf, len, &packet);
2534 		if (ret <= 0)
2535 			return false;
2536 		if (packet.type == INTEL_PT_TSC) {
2537 			*tsc = packet.payload;
2538 			*rem = len;
2539 			return true;
2540 		}
2541 		if (packet.type == INTEL_PT_PSBEND)
2542 			return false;
2543 		buf += ret;
2544 		len -= ret;
2545 	}
2546 	return false;
2547 }
2548 
2549 /**
2550  * intel_pt_tsc_cmp - compare 7-byte TSCs.
2551  * @tsc1: first TSC to compare
2552  * @tsc2: second TSC to compare
2553  *
2554  * This function compares 7-byte TSC values allowing for the possibility that
2555  * TSC wrapped around.  Generally it is not possible to know if TSC has wrapped
2556  * around so for that purpose this function assumes the absolute difference is
2557  * less than half the maximum difference.
2558  *
2559  * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2560  * after @tsc2.
2561  */
2562 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2563 {
2564 	const uint64_t halfway = (1ULL << 55);
2565 
2566 	if (tsc1 == tsc2)
2567 		return 0;
2568 
2569 	if (tsc1 < tsc2) {
2570 		if (tsc2 - tsc1 < halfway)
2571 			return -1;
2572 		else
2573 			return 1;
2574 	} else {
2575 		if (tsc1 - tsc2 < halfway)
2576 			return 1;
2577 		else
2578 			return -1;
2579 	}
2580 }
2581 
2582 #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2583 
2584 /**
2585  * adj_for_padding - adjust overlap to account for padding.
2586  * @buf_b: second buffer
2587  * @buf_a: first buffer
2588  * @len_a: size of first buffer
2589  *
2590  * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2591  * accordingly.
2592  *
2593  * Return: A pointer into @buf_b from where non-overlapped data starts
2594  */
2595 static unsigned char *adj_for_padding(unsigned char *buf_b,
2596 				      unsigned char *buf_a, size_t len_a)
2597 {
2598 	unsigned char *p = buf_b - MAX_PADDING;
2599 	unsigned char *q = buf_a + len_a - MAX_PADDING;
2600 	int i;
2601 
2602 	for (i = MAX_PADDING; i; i--, p++, q++) {
2603 		if (*p != *q)
2604 			break;
2605 	}
2606 
2607 	return p;
2608 }
2609 
2610 /**
2611  * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2612  *                             using TSC.
2613  * @buf_a: first buffer
2614  * @len_a: size of first buffer
2615  * @buf_b: second buffer
2616  * @len_b: size of second buffer
2617  * @consecutive: returns true if there is data in buf_b that is consecutive
2618  *               to buf_a
2619  *
2620  * If the trace contains TSC we can look at the last TSC of @buf_a and the
2621  * first TSC of @buf_b in order to determine if the buffers overlap, and then
2622  * walk forward in @buf_b until a later TSC is found.  A precondition is that
2623  * @buf_a and @buf_b are positioned at a PSB.
2624  *
2625  * Return: A pointer into @buf_b from where non-overlapped data starts, or
2626  * @buf_b + @len_b if there is no non-overlapped data.
2627  */
2628 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2629 						size_t len_a,
2630 						unsigned char *buf_b,
2631 						size_t len_b, bool *consecutive)
2632 {
2633 	uint64_t tsc_a, tsc_b;
2634 	unsigned char *p;
2635 	size_t len, rem_a, rem_b;
2636 
2637 	p = intel_pt_last_psb(buf_a, len_a);
2638 	if (!p)
2639 		return buf_b; /* No PSB in buf_a => no overlap */
2640 
2641 	len = len_a - (p - buf_a);
2642 	if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2643 		/* The last PSB+ in buf_a is incomplete, so go back one more */
2644 		len_a -= len;
2645 		p = intel_pt_last_psb(buf_a, len_a);
2646 		if (!p)
2647 			return buf_b; /* No full PSB+ => assume no overlap */
2648 		len = len_a - (p - buf_a);
2649 		if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2650 			return buf_b; /* No TSC in buf_a => assume no overlap */
2651 	}
2652 
2653 	while (1) {
2654 		/* Ignore PSB+ with no TSC */
2655 		if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2656 			int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2657 
2658 			/* Same TSC, so buffers are consecutive */
2659 			if (!cmp && rem_b >= rem_a) {
2660 				unsigned char *start;
2661 
2662 				*consecutive = true;
2663 				start = buf_b + len_b - (rem_b - rem_a);
2664 				return adj_for_padding(start, buf_a, len_a);
2665 			}
2666 			if (cmp < 0)
2667 				return buf_b; /* tsc_a < tsc_b => no overlap */
2668 		}
2669 
2670 		if (!intel_pt_step_psb(&buf_b, &len_b))
2671 			return buf_b + len_b; /* No PSB in buf_b => no data */
2672 	}
2673 }
2674 
2675 /**
2676  * intel_pt_find_overlap - determine start of non-overlapped trace data.
2677  * @buf_a: first buffer
2678  * @len_a: size of first buffer
2679  * @buf_b: second buffer
2680  * @len_b: size of second buffer
2681  * @have_tsc: can use TSC packets to detect overlap
2682  * @consecutive: returns true if there is data in buf_b that is consecutive
2683  *               to buf_a
2684  *
2685  * When trace samples or snapshots are recorded there is the possibility that
2686  * the data overlaps.  Note that, for the purposes of decoding, data is only
2687  * useful if it begins with a PSB packet.
2688  *
2689  * Return: A pointer into @buf_b from where non-overlapped data starts, or
2690  * @buf_b + @len_b if there is no non-overlapped data.
2691  */
2692 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2693 				     unsigned char *buf_b, size_t len_b,
2694 				     bool have_tsc, bool *consecutive)
2695 {
2696 	unsigned char *found;
2697 
2698 	/* Buffer 'b' must start at PSB so throw away everything before that */
2699 	if (!intel_pt_next_psb(&buf_b, &len_b))
2700 		return buf_b + len_b; /* No PSB */
2701 
2702 	if (!intel_pt_next_psb(&buf_a, &len_a))
2703 		return buf_b; /* No overlap */
2704 
2705 	if (have_tsc) {
2706 		found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2707 						  consecutive);
2708 		if (found)
2709 			return found;
2710 	}
2711 
2712 	/*
2713 	 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2714 	 * we can ignore the first part of buffer 'a'.
2715 	 */
2716 	while (len_b < len_a) {
2717 		if (!intel_pt_step_psb(&buf_a, &len_a))
2718 			return buf_b; /* No overlap */
2719 	}
2720 
2721 	/* Now len_b >= len_a */
2722 	while (1) {
2723 		/* Potential overlap so check the bytes */
2724 		found = memmem(buf_a, len_a, buf_b, len_a);
2725 		if (found) {
2726 			*consecutive = true;
2727 			return adj_for_padding(buf_b + len_a, buf_a, len_a);
2728 		}
2729 
2730 		/* Try again at next PSB in buffer 'a' */
2731 		if (!intel_pt_step_psb(&buf_a, &len_a))
2732 			return buf_b; /* No overlap */
2733 	}
2734 }
2735