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