xref: /openbmc/linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c (revision 943126417891372d56aa3fe46295cbf53db31370)
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 
1479 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1480 {
1481 	unsigned int cbr = decoder->packet.payload & 0xff;
1482 
1483 	decoder->cbr_payload = decoder->packet.payload;
1484 
1485 	if (decoder->cbr == cbr)
1486 		return;
1487 
1488 	decoder->cbr = cbr;
1489 	decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1490 }
1491 
1492 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1493 {
1494 	uint64_t timestamp = decoder->cyc_ref_timestamp;
1495 
1496 	decoder->have_cyc = true;
1497 
1498 	decoder->cycle_cnt += decoder->packet.payload;
1499 
1500 	if (!decoder->cyc_ref_timestamp)
1501 		return;
1502 
1503 	if (decoder->have_calc_cyc_to_tsc)
1504 		timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1505 	else if (decoder->cbr)
1506 		timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1507 	else
1508 		return;
1509 
1510 	if (timestamp < decoder->timestamp)
1511 		intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1512 			     timestamp, decoder->timestamp);
1513 	else
1514 		decoder->timestamp = timestamp;
1515 
1516 	decoder->timestamp_insn_cnt = 0;
1517 }
1518 
1519 /* Walk PSB+ packets when already in sync. */
1520 static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1521 {
1522 	int err;
1523 
1524 	while (1) {
1525 		err = intel_pt_get_next_packet(decoder);
1526 		if (err)
1527 			return err;
1528 
1529 		switch (decoder->packet.type) {
1530 		case INTEL_PT_PSBEND:
1531 			return 0;
1532 
1533 		case INTEL_PT_TIP_PGD:
1534 		case INTEL_PT_TIP_PGE:
1535 		case INTEL_PT_TIP:
1536 		case INTEL_PT_TNT:
1537 		case INTEL_PT_TRACESTOP:
1538 		case INTEL_PT_BAD:
1539 		case INTEL_PT_PSB:
1540 		case INTEL_PT_PTWRITE:
1541 		case INTEL_PT_PTWRITE_IP:
1542 		case INTEL_PT_EXSTOP:
1543 		case INTEL_PT_EXSTOP_IP:
1544 		case INTEL_PT_MWAIT:
1545 		case INTEL_PT_PWRE:
1546 		case INTEL_PT_PWRX:
1547 			decoder->have_tma = false;
1548 			intel_pt_log("ERROR: Unexpected packet\n");
1549 			return -EAGAIN;
1550 
1551 		case INTEL_PT_OVF:
1552 			return intel_pt_overflow(decoder);
1553 
1554 		case INTEL_PT_TSC:
1555 			intel_pt_calc_tsc_timestamp(decoder);
1556 			break;
1557 
1558 		case INTEL_PT_TMA:
1559 			intel_pt_calc_tma(decoder);
1560 			break;
1561 
1562 		case INTEL_PT_CBR:
1563 			intel_pt_calc_cbr(decoder);
1564 			break;
1565 
1566 		case INTEL_PT_MODE_EXEC:
1567 			decoder->exec_mode = decoder->packet.payload;
1568 			break;
1569 
1570 		case INTEL_PT_PIP:
1571 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1572 			break;
1573 
1574 		case INTEL_PT_FUP:
1575 			decoder->pge = true;
1576 			if (decoder->packet.count)
1577 				intel_pt_set_last_ip(decoder);
1578 			break;
1579 
1580 		case INTEL_PT_MODE_TSX:
1581 			intel_pt_update_in_tx(decoder);
1582 			break;
1583 
1584 		case INTEL_PT_MTC:
1585 			intel_pt_calc_mtc_timestamp(decoder);
1586 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1587 				decoder->state.type |= INTEL_PT_INSTRUCTION;
1588 			break;
1589 
1590 		case INTEL_PT_CYC:
1591 		case INTEL_PT_VMCS:
1592 		case INTEL_PT_MNT:
1593 		case INTEL_PT_PAD:
1594 		default:
1595 			break;
1596 		}
1597 	}
1598 }
1599 
1600 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1601 {
1602 	int err;
1603 
1604 	if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1605 		decoder->tx_flags = 0;
1606 		decoder->state.flags &= ~INTEL_PT_IN_TX;
1607 		decoder->state.flags |= INTEL_PT_ABORT_TX;
1608 	} else {
1609 		decoder->state.flags |= INTEL_PT_ASYNC;
1610 	}
1611 
1612 	while (1) {
1613 		err = intel_pt_get_next_packet(decoder);
1614 		if (err)
1615 			return err;
1616 
1617 		switch (decoder->packet.type) {
1618 		case INTEL_PT_TNT:
1619 		case INTEL_PT_FUP:
1620 		case INTEL_PT_TRACESTOP:
1621 		case INTEL_PT_PSB:
1622 		case INTEL_PT_TSC:
1623 		case INTEL_PT_TMA:
1624 		case INTEL_PT_MODE_TSX:
1625 		case INTEL_PT_BAD:
1626 		case INTEL_PT_PSBEND:
1627 		case INTEL_PT_PTWRITE:
1628 		case INTEL_PT_PTWRITE_IP:
1629 		case INTEL_PT_EXSTOP:
1630 		case INTEL_PT_EXSTOP_IP:
1631 		case INTEL_PT_MWAIT:
1632 		case INTEL_PT_PWRE:
1633 		case INTEL_PT_PWRX:
1634 			intel_pt_log("ERROR: Missing TIP after FUP\n");
1635 			decoder->pkt_state = INTEL_PT_STATE_ERR3;
1636 			decoder->pkt_step = 0;
1637 			return -ENOENT;
1638 
1639 		case INTEL_PT_CBR:
1640 			intel_pt_calc_cbr(decoder);
1641 			break;
1642 
1643 		case INTEL_PT_OVF:
1644 			return intel_pt_overflow(decoder);
1645 
1646 		case INTEL_PT_TIP_PGD:
1647 			decoder->state.from_ip = decoder->ip;
1648 			if (decoder->packet.count == 0) {
1649 				decoder->state.to_ip = 0;
1650 			} else {
1651 				intel_pt_set_ip(decoder);
1652 				decoder->state.to_ip = decoder->ip;
1653 			}
1654 			decoder->pge = false;
1655 			decoder->continuous_period = false;
1656 			decoder->state.type |= INTEL_PT_TRACE_END;
1657 			return 0;
1658 
1659 		case INTEL_PT_TIP_PGE:
1660 			decoder->pge = true;
1661 			intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1662 				     decoder->ip);
1663 			decoder->state.from_ip = 0;
1664 			if (decoder->packet.count == 0) {
1665 				decoder->state.to_ip = 0;
1666 			} else {
1667 				intel_pt_set_ip(decoder);
1668 				decoder->state.to_ip = decoder->ip;
1669 			}
1670 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1671 			return 0;
1672 
1673 		case INTEL_PT_TIP:
1674 			decoder->state.from_ip = decoder->ip;
1675 			if (decoder->packet.count == 0) {
1676 				decoder->state.to_ip = 0;
1677 			} else {
1678 				intel_pt_set_ip(decoder);
1679 				decoder->state.to_ip = decoder->ip;
1680 			}
1681 			return 0;
1682 
1683 		case INTEL_PT_PIP:
1684 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1685 			break;
1686 
1687 		case INTEL_PT_MTC:
1688 			intel_pt_calc_mtc_timestamp(decoder);
1689 			if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1690 				decoder->state.type |= INTEL_PT_INSTRUCTION;
1691 			break;
1692 
1693 		case INTEL_PT_CYC:
1694 			intel_pt_calc_cyc_timestamp(decoder);
1695 			break;
1696 
1697 		case INTEL_PT_MODE_EXEC:
1698 			decoder->exec_mode = decoder->packet.payload;
1699 			break;
1700 
1701 		case INTEL_PT_VMCS:
1702 		case INTEL_PT_MNT:
1703 		case INTEL_PT_PAD:
1704 			break;
1705 
1706 		default:
1707 			return intel_pt_bug(decoder);
1708 		}
1709 	}
1710 }
1711 
1712 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1713 {
1714 	bool no_tip = false;
1715 	int err;
1716 
1717 	while (1) {
1718 		err = intel_pt_get_next_packet(decoder);
1719 		if (err)
1720 			return err;
1721 next:
1722 		switch (decoder->packet.type) {
1723 		case INTEL_PT_TNT:
1724 			if (!decoder->packet.count)
1725 				break;
1726 			decoder->tnt = decoder->packet;
1727 			decoder->pkt_state = INTEL_PT_STATE_TNT;
1728 			err = intel_pt_walk_tnt(decoder);
1729 			if (err == -EAGAIN)
1730 				break;
1731 			return err;
1732 
1733 		case INTEL_PT_TIP_PGD:
1734 			if (decoder->packet.count != 0)
1735 				intel_pt_set_last_ip(decoder);
1736 			decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1737 			return intel_pt_walk_tip(decoder);
1738 
1739 		case INTEL_PT_TIP_PGE: {
1740 			decoder->pge = true;
1741 			if (decoder->packet.count == 0) {
1742 				intel_pt_log_at("Skipping zero TIP.PGE",
1743 						decoder->pos);
1744 				break;
1745 			}
1746 			intel_pt_set_ip(decoder);
1747 			decoder->state.from_ip = 0;
1748 			decoder->state.to_ip = decoder->ip;
1749 			decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1750 			return 0;
1751 		}
1752 
1753 		case INTEL_PT_OVF:
1754 			return intel_pt_overflow(decoder);
1755 
1756 		case INTEL_PT_TIP:
1757 			if (decoder->packet.count != 0)
1758 				intel_pt_set_last_ip(decoder);
1759 			decoder->pkt_state = INTEL_PT_STATE_TIP;
1760 			return intel_pt_walk_tip(decoder);
1761 
1762 		case INTEL_PT_FUP:
1763 			if (decoder->packet.count == 0) {
1764 				intel_pt_log_at("Skipping zero FUP",
1765 						decoder->pos);
1766 				no_tip = false;
1767 				break;
1768 			}
1769 			intel_pt_set_last_ip(decoder);
1770 			if (!decoder->branch_enable) {
1771 				decoder->ip = decoder->last_ip;
1772 				if (intel_pt_fup_event(decoder))
1773 					return 0;
1774 				no_tip = false;
1775 				break;
1776 			}
1777 			if (decoder->set_fup_mwait)
1778 				no_tip = true;
1779 			err = intel_pt_walk_fup(decoder);
1780 			if (err != -EAGAIN) {
1781 				if (err)
1782 					return err;
1783 				if (no_tip)
1784 					decoder->pkt_state =
1785 						INTEL_PT_STATE_FUP_NO_TIP;
1786 				else
1787 					decoder->pkt_state = INTEL_PT_STATE_FUP;
1788 				return 0;
1789 			}
1790 			if (no_tip) {
1791 				no_tip = false;
1792 				break;
1793 			}
1794 			return intel_pt_walk_fup_tip(decoder);
1795 
1796 		case INTEL_PT_TRACESTOP:
1797 			decoder->pge = false;
1798 			decoder->continuous_period = false;
1799 			intel_pt_clear_tx_flags(decoder);
1800 			decoder->have_tma = false;
1801 			break;
1802 
1803 		case INTEL_PT_PSB:
1804 			decoder->last_ip = 0;
1805 			decoder->have_last_ip = true;
1806 			intel_pt_clear_stack(&decoder->stack);
1807 			err = intel_pt_walk_psbend(decoder);
1808 			if (err == -EAGAIN)
1809 				goto next;
1810 			if (err)
1811 				return err;
1812 			break;
1813 
1814 		case INTEL_PT_PIP:
1815 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1816 			break;
1817 
1818 		case INTEL_PT_MTC:
1819 			intel_pt_calc_mtc_timestamp(decoder);
1820 			if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1821 				break;
1822 			/*
1823 			 * Ensure that there has been an instruction since the
1824 			 * last MTC.
1825 			 */
1826 			if (!decoder->mtc_insn)
1827 				break;
1828 			decoder->mtc_insn = false;
1829 			/* Ensure that there is a timestamp */
1830 			if (!decoder->timestamp)
1831 				break;
1832 			decoder->state.type = INTEL_PT_INSTRUCTION;
1833 			decoder->state.from_ip = decoder->ip;
1834 			decoder->state.to_ip = 0;
1835 			decoder->mtc_insn = false;
1836 			return 0;
1837 
1838 		case INTEL_PT_TSC:
1839 			intel_pt_calc_tsc_timestamp(decoder);
1840 			break;
1841 
1842 		case INTEL_PT_TMA:
1843 			intel_pt_calc_tma(decoder);
1844 			break;
1845 
1846 		case INTEL_PT_CYC:
1847 			intel_pt_calc_cyc_timestamp(decoder);
1848 			break;
1849 
1850 		case INTEL_PT_CBR:
1851 			intel_pt_calc_cbr(decoder);
1852 			if (!decoder->branch_enable &&
1853 			    decoder->cbr != decoder->cbr_seen) {
1854 				decoder->cbr_seen = decoder->cbr;
1855 				decoder->state.type = INTEL_PT_CBR_CHG;
1856 				decoder->state.from_ip = decoder->ip;
1857 				decoder->state.to_ip = 0;
1858 				decoder->state.cbr_payload =
1859 							decoder->packet.payload;
1860 				return 0;
1861 			}
1862 			break;
1863 
1864 		case INTEL_PT_MODE_EXEC:
1865 			decoder->exec_mode = decoder->packet.payload;
1866 			break;
1867 
1868 		case INTEL_PT_MODE_TSX:
1869 			/* MODE_TSX need not be followed by FUP */
1870 			if (!decoder->pge) {
1871 				intel_pt_update_in_tx(decoder);
1872 				break;
1873 			}
1874 			err = intel_pt_mode_tsx(decoder, &no_tip);
1875 			if (err)
1876 				return err;
1877 			goto next;
1878 
1879 		case INTEL_PT_BAD: /* Does not happen */
1880 			return intel_pt_bug(decoder);
1881 
1882 		case INTEL_PT_PSBEND:
1883 		case INTEL_PT_VMCS:
1884 		case INTEL_PT_MNT:
1885 		case INTEL_PT_PAD:
1886 			break;
1887 
1888 		case INTEL_PT_PTWRITE_IP:
1889 			decoder->fup_ptw_payload = decoder->packet.payload;
1890 			err = intel_pt_get_next_packet(decoder);
1891 			if (err)
1892 				return err;
1893 			if (decoder->packet.type == INTEL_PT_FUP) {
1894 				decoder->set_fup_ptw = true;
1895 				no_tip = true;
1896 			} else {
1897 				intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
1898 						decoder->pos);
1899 			}
1900 			goto next;
1901 
1902 		case INTEL_PT_PTWRITE:
1903 			decoder->state.type = INTEL_PT_PTW;
1904 			decoder->state.from_ip = decoder->ip;
1905 			decoder->state.to_ip = 0;
1906 			decoder->state.ptw_payload = decoder->packet.payload;
1907 			return 0;
1908 
1909 		case INTEL_PT_MWAIT:
1910 			decoder->fup_mwait_payload = decoder->packet.payload;
1911 			decoder->set_fup_mwait = true;
1912 			break;
1913 
1914 		case INTEL_PT_PWRE:
1915 			if (decoder->set_fup_mwait) {
1916 				decoder->fup_pwre_payload =
1917 							decoder->packet.payload;
1918 				decoder->set_fup_pwre = true;
1919 				break;
1920 			}
1921 			decoder->state.type = INTEL_PT_PWR_ENTRY;
1922 			decoder->state.from_ip = decoder->ip;
1923 			decoder->state.to_ip = 0;
1924 			decoder->state.pwrx_payload = decoder->packet.payload;
1925 			return 0;
1926 
1927 		case INTEL_PT_EXSTOP_IP:
1928 			err = intel_pt_get_next_packet(decoder);
1929 			if (err)
1930 				return err;
1931 			if (decoder->packet.type == INTEL_PT_FUP) {
1932 				decoder->set_fup_exstop = true;
1933 				no_tip = true;
1934 			} else {
1935 				intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
1936 						decoder->pos);
1937 			}
1938 			goto next;
1939 
1940 		case INTEL_PT_EXSTOP:
1941 			decoder->state.type = INTEL_PT_EX_STOP;
1942 			decoder->state.from_ip = decoder->ip;
1943 			decoder->state.to_ip = 0;
1944 			return 0;
1945 
1946 		case INTEL_PT_PWRX:
1947 			decoder->state.type = INTEL_PT_PWR_EXIT;
1948 			decoder->state.from_ip = decoder->ip;
1949 			decoder->state.to_ip = 0;
1950 			decoder->state.pwrx_payload = decoder->packet.payload;
1951 			return 0;
1952 
1953 		default:
1954 			return intel_pt_bug(decoder);
1955 		}
1956 	}
1957 }
1958 
1959 static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1960 {
1961 	return decoder->packet.count &&
1962 	       (decoder->have_last_ip || decoder->packet.count == 3 ||
1963 		decoder->packet.count == 6);
1964 }
1965 
1966 /* Walk PSB+ packets to get in sync. */
1967 static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1968 {
1969 	int err;
1970 
1971 	while (1) {
1972 		err = intel_pt_get_next_packet(decoder);
1973 		if (err)
1974 			return err;
1975 
1976 		switch (decoder->packet.type) {
1977 		case INTEL_PT_TIP_PGD:
1978 			decoder->continuous_period = false;
1979 			__fallthrough;
1980 		case INTEL_PT_TIP_PGE:
1981 		case INTEL_PT_TIP:
1982 		case INTEL_PT_PTWRITE:
1983 		case INTEL_PT_PTWRITE_IP:
1984 		case INTEL_PT_EXSTOP:
1985 		case INTEL_PT_EXSTOP_IP:
1986 		case INTEL_PT_MWAIT:
1987 		case INTEL_PT_PWRE:
1988 		case INTEL_PT_PWRX:
1989 			intel_pt_log("ERROR: Unexpected packet\n");
1990 			return -ENOENT;
1991 
1992 		case INTEL_PT_FUP:
1993 			decoder->pge = true;
1994 			if (intel_pt_have_ip(decoder)) {
1995 				uint64_t current_ip = decoder->ip;
1996 
1997 				intel_pt_set_ip(decoder);
1998 				if (current_ip)
1999 					intel_pt_log_to("Setting IP",
2000 							decoder->ip);
2001 			}
2002 			break;
2003 
2004 		case INTEL_PT_MTC:
2005 			intel_pt_calc_mtc_timestamp(decoder);
2006 			break;
2007 
2008 		case INTEL_PT_TSC:
2009 			intel_pt_calc_tsc_timestamp(decoder);
2010 			break;
2011 
2012 		case INTEL_PT_TMA:
2013 			intel_pt_calc_tma(decoder);
2014 			break;
2015 
2016 		case INTEL_PT_CYC:
2017 			intel_pt_calc_cyc_timestamp(decoder);
2018 			break;
2019 
2020 		case INTEL_PT_CBR:
2021 			intel_pt_calc_cbr(decoder);
2022 			break;
2023 
2024 		case INTEL_PT_PIP:
2025 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2026 			break;
2027 
2028 		case INTEL_PT_MODE_EXEC:
2029 			decoder->exec_mode = decoder->packet.payload;
2030 			break;
2031 
2032 		case INTEL_PT_MODE_TSX:
2033 			intel_pt_update_in_tx(decoder);
2034 			break;
2035 
2036 		case INTEL_PT_TRACESTOP:
2037 			decoder->pge = false;
2038 			decoder->continuous_period = false;
2039 			intel_pt_clear_tx_flags(decoder);
2040 			__fallthrough;
2041 
2042 		case INTEL_PT_TNT:
2043 			decoder->have_tma = false;
2044 			intel_pt_log("ERROR: Unexpected packet\n");
2045 			if (decoder->ip)
2046 				decoder->pkt_state = INTEL_PT_STATE_ERR4;
2047 			else
2048 				decoder->pkt_state = INTEL_PT_STATE_ERR3;
2049 			return -ENOENT;
2050 
2051 		case INTEL_PT_BAD: /* Does not happen */
2052 			return intel_pt_bug(decoder);
2053 
2054 		case INTEL_PT_OVF:
2055 			return intel_pt_overflow(decoder);
2056 
2057 		case INTEL_PT_PSBEND:
2058 			return 0;
2059 
2060 		case INTEL_PT_PSB:
2061 		case INTEL_PT_VMCS:
2062 		case INTEL_PT_MNT:
2063 		case INTEL_PT_PAD:
2064 		default:
2065 			break;
2066 		}
2067 	}
2068 }
2069 
2070 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2071 {
2072 	int err;
2073 
2074 	while (1) {
2075 		err = intel_pt_get_next_packet(decoder);
2076 		if (err)
2077 			return err;
2078 
2079 		switch (decoder->packet.type) {
2080 		case INTEL_PT_TIP_PGD:
2081 			decoder->continuous_period = false;
2082 			__fallthrough;
2083 		case INTEL_PT_TIP_PGE:
2084 		case INTEL_PT_TIP:
2085 			decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
2086 			if (intel_pt_have_ip(decoder))
2087 				intel_pt_set_ip(decoder);
2088 			if (!decoder->ip)
2089 				break;
2090 			if (decoder->packet.type == INTEL_PT_TIP_PGE)
2091 				decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2092 			if (decoder->packet.type == INTEL_PT_TIP_PGD)
2093 				decoder->state.type |= INTEL_PT_TRACE_END;
2094 			return 0;
2095 
2096 		case INTEL_PT_FUP:
2097 			if (intel_pt_have_ip(decoder))
2098 				intel_pt_set_ip(decoder);
2099 			if (decoder->ip)
2100 				return 0;
2101 			break;
2102 
2103 		case INTEL_PT_MTC:
2104 			intel_pt_calc_mtc_timestamp(decoder);
2105 			break;
2106 
2107 		case INTEL_PT_TSC:
2108 			intel_pt_calc_tsc_timestamp(decoder);
2109 			break;
2110 
2111 		case INTEL_PT_TMA:
2112 			intel_pt_calc_tma(decoder);
2113 			break;
2114 
2115 		case INTEL_PT_CYC:
2116 			intel_pt_calc_cyc_timestamp(decoder);
2117 			break;
2118 
2119 		case INTEL_PT_CBR:
2120 			intel_pt_calc_cbr(decoder);
2121 			break;
2122 
2123 		case INTEL_PT_PIP:
2124 			decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
2125 			break;
2126 
2127 		case INTEL_PT_MODE_EXEC:
2128 			decoder->exec_mode = decoder->packet.payload;
2129 			break;
2130 
2131 		case INTEL_PT_MODE_TSX:
2132 			intel_pt_update_in_tx(decoder);
2133 			break;
2134 
2135 		case INTEL_PT_OVF:
2136 			return intel_pt_overflow(decoder);
2137 
2138 		case INTEL_PT_BAD: /* Does not happen */
2139 			return intel_pt_bug(decoder);
2140 
2141 		case INTEL_PT_TRACESTOP:
2142 			decoder->pge = false;
2143 			decoder->continuous_period = false;
2144 			intel_pt_clear_tx_flags(decoder);
2145 			decoder->have_tma = false;
2146 			break;
2147 
2148 		case INTEL_PT_PSB:
2149 			decoder->last_ip = 0;
2150 			decoder->have_last_ip = true;
2151 			intel_pt_clear_stack(&decoder->stack);
2152 			err = intel_pt_walk_psb(decoder);
2153 			if (err)
2154 				return err;
2155 			if (decoder->ip) {
2156 				/* Do not have a sample */
2157 				decoder->state.type = 0;
2158 				return 0;
2159 			}
2160 			break;
2161 
2162 		case INTEL_PT_TNT:
2163 		case INTEL_PT_PSBEND:
2164 		case INTEL_PT_VMCS:
2165 		case INTEL_PT_MNT:
2166 		case INTEL_PT_PAD:
2167 		case INTEL_PT_PTWRITE:
2168 		case INTEL_PT_PTWRITE_IP:
2169 		case INTEL_PT_EXSTOP:
2170 		case INTEL_PT_EXSTOP_IP:
2171 		case INTEL_PT_MWAIT:
2172 		case INTEL_PT_PWRE:
2173 		case INTEL_PT_PWRX:
2174 		default:
2175 			break;
2176 		}
2177 	}
2178 }
2179 
2180 static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2181 {
2182 	int err;
2183 
2184 	decoder->set_fup_tx_flags = false;
2185 	decoder->set_fup_ptw = false;
2186 	decoder->set_fup_mwait = false;
2187 	decoder->set_fup_pwre = false;
2188 	decoder->set_fup_exstop = false;
2189 
2190 	if (!decoder->branch_enable) {
2191 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2192 		decoder->overflow = false;
2193 		decoder->state.type = 0; /* Do not have a sample */
2194 		return 0;
2195 	}
2196 
2197 	intel_pt_log("Scanning for full IP\n");
2198 	err = intel_pt_walk_to_ip(decoder);
2199 	if (err)
2200 		return err;
2201 
2202 	decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2203 	decoder->overflow = false;
2204 
2205 	decoder->state.from_ip = 0;
2206 	decoder->state.to_ip = decoder->ip;
2207 	intel_pt_log_to("Setting IP", decoder->ip);
2208 
2209 	return 0;
2210 }
2211 
2212 static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2213 {
2214 	const unsigned char *end = decoder->buf + decoder->len;
2215 	size_t i;
2216 
2217 	for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2218 		if (i > decoder->len)
2219 			continue;
2220 		if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2221 			return i;
2222 	}
2223 	return 0;
2224 }
2225 
2226 static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2227 {
2228 	size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2229 	const char *psb = INTEL_PT_PSB_STR;
2230 
2231 	if (rest_psb > decoder->len ||
2232 	    memcmp(decoder->buf, psb + part_psb, rest_psb))
2233 		return 0;
2234 
2235 	return rest_psb;
2236 }
2237 
2238 static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2239 				  int part_psb)
2240 {
2241 	int rest_psb, ret;
2242 
2243 	decoder->pos += decoder->len;
2244 	decoder->len = 0;
2245 
2246 	ret = intel_pt_get_next_data(decoder);
2247 	if (ret)
2248 		return ret;
2249 
2250 	rest_psb = intel_pt_rest_psb(decoder, part_psb);
2251 	if (!rest_psb)
2252 		return 0;
2253 
2254 	decoder->pos -= part_psb;
2255 	decoder->next_buf = decoder->buf + rest_psb;
2256 	decoder->next_len = decoder->len - rest_psb;
2257 	memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2258 	decoder->buf = decoder->temp_buf;
2259 	decoder->len = INTEL_PT_PSB_LEN;
2260 
2261 	return 0;
2262 }
2263 
2264 static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2265 {
2266 	unsigned char *next;
2267 	int ret;
2268 
2269 	intel_pt_log("Scanning for PSB\n");
2270 	while (1) {
2271 		if (!decoder->len) {
2272 			ret = intel_pt_get_next_data(decoder);
2273 			if (ret)
2274 				return ret;
2275 		}
2276 
2277 		next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2278 			      INTEL_PT_PSB_LEN);
2279 		if (!next) {
2280 			int part_psb;
2281 
2282 			part_psb = intel_pt_part_psb(decoder);
2283 			if (part_psb) {
2284 				ret = intel_pt_get_split_psb(decoder, part_psb);
2285 				if (ret)
2286 					return ret;
2287 			} else {
2288 				decoder->pos += decoder->len;
2289 				decoder->len = 0;
2290 			}
2291 			continue;
2292 		}
2293 
2294 		decoder->pkt_step = next - decoder->buf;
2295 		return intel_pt_get_next_packet(decoder);
2296 	}
2297 }
2298 
2299 static int intel_pt_sync(struct intel_pt_decoder *decoder)
2300 {
2301 	int err;
2302 
2303 	decoder->pge = false;
2304 	decoder->continuous_period = false;
2305 	decoder->have_last_ip = false;
2306 	decoder->last_ip = 0;
2307 	decoder->ip = 0;
2308 	intel_pt_clear_stack(&decoder->stack);
2309 
2310 	err = intel_pt_scan_for_psb(decoder);
2311 	if (err)
2312 		return err;
2313 
2314 	decoder->have_last_ip = true;
2315 	decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2316 
2317 	err = intel_pt_walk_psb(decoder);
2318 	if (err)
2319 		return err;
2320 
2321 	if (decoder->ip) {
2322 		decoder->state.type = 0; /* Do not have a sample */
2323 		decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2324 	} else {
2325 		return intel_pt_sync_ip(decoder);
2326 	}
2327 
2328 	return 0;
2329 }
2330 
2331 static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2332 {
2333 	uint64_t est = decoder->sample_insn_cnt << 1;
2334 
2335 	if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2336 		goto out;
2337 
2338 	est *= decoder->max_non_turbo_ratio;
2339 	est /= decoder->cbr;
2340 out:
2341 	return decoder->sample_timestamp + est;
2342 }
2343 
2344 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2345 {
2346 	int err;
2347 
2348 	do {
2349 		decoder->state.type = INTEL_PT_BRANCH;
2350 		decoder->state.flags = 0;
2351 
2352 		switch (decoder->pkt_state) {
2353 		case INTEL_PT_STATE_NO_PSB:
2354 			err = intel_pt_sync(decoder);
2355 			break;
2356 		case INTEL_PT_STATE_NO_IP:
2357 			decoder->have_last_ip = false;
2358 			decoder->last_ip = 0;
2359 			decoder->ip = 0;
2360 			__fallthrough;
2361 		case INTEL_PT_STATE_ERR_RESYNC:
2362 			err = intel_pt_sync_ip(decoder);
2363 			break;
2364 		case INTEL_PT_STATE_IN_SYNC:
2365 			err = intel_pt_walk_trace(decoder);
2366 			break;
2367 		case INTEL_PT_STATE_TNT:
2368 			err = intel_pt_walk_tnt(decoder);
2369 			if (err == -EAGAIN)
2370 				err = intel_pt_walk_trace(decoder);
2371 			break;
2372 		case INTEL_PT_STATE_TIP:
2373 		case INTEL_PT_STATE_TIP_PGD:
2374 			err = intel_pt_walk_tip(decoder);
2375 			break;
2376 		case INTEL_PT_STATE_FUP:
2377 			decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2378 			err = intel_pt_walk_fup(decoder);
2379 			if (err == -EAGAIN)
2380 				err = intel_pt_walk_fup_tip(decoder);
2381 			else if (!err)
2382 				decoder->pkt_state = INTEL_PT_STATE_FUP;
2383 			break;
2384 		case INTEL_PT_STATE_FUP_NO_TIP:
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_trace(decoder);
2389 			break;
2390 		default:
2391 			err = intel_pt_bug(decoder);
2392 			break;
2393 		}
2394 	} while (err == -ENOLINK);
2395 
2396 	if (err) {
2397 		decoder->state.err = intel_pt_ext_err(err);
2398 		decoder->state.from_ip = decoder->ip;
2399 		decoder->sample_timestamp = decoder->timestamp;
2400 		decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2401 	} else {
2402 		decoder->state.err = 0;
2403 		if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2404 			decoder->cbr_seen = decoder->cbr;
2405 			decoder->state.type |= INTEL_PT_CBR_CHG;
2406 			decoder->state.cbr_payload = decoder->cbr_payload;
2407 		}
2408 		if (intel_pt_sample_time(decoder->pkt_state)) {
2409 			decoder->sample_timestamp = decoder->timestamp;
2410 			decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2411 		}
2412 	}
2413 
2414 	decoder->state.timestamp = decoder->sample_timestamp;
2415 	decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2416 	decoder->state.cr3 = decoder->cr3;
2417 	decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2418 
2419 	return &decoder->state;
2420 }
2421 
2422 /**
2423  * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2424  * @buf: pointer to buffer pointer
2425  * @len: size of buffer
2426  *
2427  * Updates the buffer pointer to point to the start of the next PSB packet if
2428  * there is one, otherwise the buffer pointer is unchanged.  If @buf is updated,
2429  * @len is adjusted accordingly.
2430  *
2431  * Return: %true if a PSB packet is found, %false otherwise.
2432  */
2433 static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2434 {
2435 	unsigned char *next;
2436 
2437 	next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2438 	if (next) {
2439 		*len -= next - *buf;
2440 		*buf = next;
2441 		return true;
2442 	}
2443 	return false;
2444 }
2445 
2446 /**
2447  * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2448  *                     packet.
2449  * @buf: pointer to buffer pointer
2450  * @len: size of buffer
2451  *
2452  * Updates the buffer pointer to point to the start of the following PSB packet
2453  * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2454  * pointer is unchanged.  If @buf is updated, @len is adjusted accordingly.
2455  *
2456  * Return: %true if a PSB packet is found, %false otherwise.
2457  */
2458 static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2459 {
2460 	unsigned char *next;
2461 
2462 	if (!*len)
2463 		return false;
2464 
2465 	next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2466 	if (next) {
2467 		*len -= next - *buf;
2468 		*buf = next;
2469 		return true;
2470 	}
2471 	return false;
2472 }
2473 
2474 /**
2475  * intel_pt_last_psb - find the last PSB packet in a buffer.
2476  * @buf: buffer
2477  * @len: size of buffer
2478  *
2479  * This function finds the last PSB in a buffer.
2480  *
2481  * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2482  */
2483 static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2484 {
2485 	const char *n = INTEL_PT_PSB_STR;
2486 	unsigned char *p;
2487 	size_t k;
2488 
2489 	if (len < INTEL_PT_PSB_LEN)
2490 		return NULL;
2491 
2492 	k = len - INTEL_PT_PSB_LEN + 1;
2493 	while (1) {
2494 		p = memrchr(buf, n[0], k);
2495 		if (!p)
2496 			return NULL;
2497 		if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2498 			return p;
2499 		k = p - buf;
2500 		if (!k)
2501 			return NULL;
2502 	}
2503 }
2504 
2505 /**
2506  * intel_pt_next_tsc - find and return next TSC.
2507  * @buf: buffer
2508  * @len: size of buffer
2509  * @tsc: TSC value returned
2510  * @rem: returns remaining size when TSC is found
2511  *
2512  * Find a TSC packet in @buf and return the TSC value.  This function assumes
2513  * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2514  * PSBEND packet is found.
2515  *
2516  * Return: %true if TSC is found, false otherwise.
2517  */
2518 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2519 			      size_t *rem)
2520 {
2521 	struct intel_pt_pkt packet;
2522 	int ret;
2523 
2524 	while (len) {
2525 		ret = intel_pt_get_packet(buf, len, &packet);
2526 		if (ret <= 0)
2527 			return false;
2528 		if (packet.type == INTEL_PT_TSC) {
2529 			*tsc = packet.payload;
2530 			*rem = len;
2531 			return true;
2532 		}
2533 		if (packet.type == INTEL_PT_PSBEND)
2534 			return false;
2535 		buf += ret;
2536 		len -= ret;
2537 	}
2538 	return false;
2539 }
2540 
2541 /**
2542  * intel_pt_tsc_cmp - compare 7-byte TSCs.
2543  * @tsc1: first TSC to compare
2544  * @tsc2: second TSC to compare
2545  *
2546  * This function compares 7-byte TSC values allowing for the possibility that
2547  * TSC wrapped around.  Generally it is not possible to know if TSC has wrapped
2548  * around so for that purpose this function assumes the absolute difference is
2549  * less than half the maximum difference.
2550  *
2551  * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2552  * after @tsc2.
2553  */
2554 static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2555 {
2556 	const uint64_t halfway = (1ULL << 55);
2557 
2558 	if (tsc1 == tsc2)
2559 		return 0;
2560 
2561 	if (tsc1 < tsc2) {
2562 		if (tsc2 - tsc1 < halfway)
2563 			return -1;
2564 		else
2565 			return 1;
2566 	} else {
2567 		if (tsc1 - tsc2 < halfway)
2568 			return 1;
2569 		else
2570 			return -1;
2571 	}
2572 }
2573 
2574 /**
2575  * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2576  *                             using TSC.
2577  * @buf_a: first buffer
2578  * @len_a: size of first buffer
2579  * @buf_b: second buffer
2580  * @len_b: size of second buffer
2581  * @consecutive: returns true if there is data in buf_b that is consecutive
2582  *               to buf_a
2583  *
2584  * If the trace contains TSC we can look at the last TSC of @buf_a and the
2585  * first TSC of @buf_b in order to determine if the buffers overlap, and then
2586  * walk forward in @buf_b until a later TSC is found.  A precondition is that
2587  * @buf_a and @buf_b are positioned at a PSB.
2588  *
2589  * Return: A pointer into @buf_b from where non-overlapped data starts, or
2590  * @buf_b + @len_b if there is no non-overlapped data.
2591  */
2592 static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2593 						size_t len_a,
2594 						unsigned char *buf_b,
2595 						size_t len_b, bool *consecutive)
2596 {
2597 	uint64_t tsc_a, tsc_b;
2598 	unsigned char *p;
2599 	size_t len, rem_a, rem_b;
2600 
2601 	p = intel_pt_last_psb(buf_a, len_a);
2602 	if (!p)
2603 		return buf_b; /* No PSB in buf_a => no overlap */
2604 
2605 	len = len_a - (p - buf_a);
2606 	if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
2607 		/* The last PSB+ in buf_a is incomplete, so go back one more */
2608 		len_a -= len;
2609 		p = intel_pt_last_psb(buf_a, len_a);
2610 		if (!p)
2611 			return buf_b; /* No full PSB+ => assume no overlap */
2612 		len = len_a - (p - buf_a);
2613 		if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
2614 			return buf_b; /* No TSC in buf_a => assume no overlap */
2615 	}
2616 
2617 	while (1) {
2618 		/* Ignore PSB+ with no TSC */
2619 		if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2620 			int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2621 
2622 			/* Same TSC, so buffers are consecutive */
2623 			if (!cmp && rem_b >= rem_a) {
2624 				*consecutive = true;
2625 				return buf_b + len_b - (rem_b - rem_a);
2626 			}
2627 			if (cmp < 0)
2628 				return buf_b; /* tsc_a < tsc_b => no overlap */
2629 		}
2630 
2631 		if (!intel_pt_step_psb(&buf_b, &len_b))
2632 			return buf_b + len_b; /* No PSB in buf_b => no data */
2633 	}
2634 }
2635 
2636 /**
2637  * intel_pt_find_overlap - determine start of non-overlapped trace data.
2638  * @buf_a: first buffer
2639  * @len_a: size of first buffer
2640  * @buf_b: second buffer
2641  * @len_b: size of second buffer
2642  * @have_tsc: can use TSC packets to detect overlap
2643  * @consecutive: returns true if there is data in buf_b that is consecutive
2644  *               to buf_a
2645  *
2646  * When trace samples or snapshots are recorded there is the possibility that
2647  * the data overlaps.  Note that, for the purposes of decoding, data is only
2648  * useful if it begins with a PSB packet.
2649  *
2650  * Return: A pointer into @buf_b from where non-overlapped data starts, or
2651  * @buf_b + @len_b if there is no non-overlapped data.
2652  */
2653 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2654 				     unsigned char *buf_b, size_t len_b,
2655 				     bool have_tsc, bool *consecutive)
2656 {
2657 	unsigned char *found;
2658 
2659 	/* Buffer 'b' must start at PSB so throw away everything before that */
2660 	if (!intel_pt_next_psb(&buf_b, &len_b))
2661 		return buf_b + len_b; /* No PSB */
2662 
2663 	if (!intel_pt_next_psb(&buf_a, &len_a))
2664 		return buf_b; /* No overlap */
2665 
2666 	if (have_tsc) {
2667 		found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2668 						  consecutive);
2669 		if (found)
2670 			return found;
2671 	}
2672 
2673 	/*
2674 	 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2675 	 * we can ignore the first part of buffer 'a'.
2676 	 */
2677 	while (len_b < len_a) {
2678 		if (!intel_pt_step_psb(&buf_a, &len_a))
2679 			return buf_b; /* No overlap */
2680 	}
2681 
2682 	/* Now len_b >= len_a */
2683 	while (1) {
2684 		/* Potential overlap so check the bytes */
2685 		found = memmem(buf_a, len_a, buf_b, len_a);
2686 		if (found) {
2687 			*consecutive = true;
2688 			return buf_b + len_a;
2689 		}
2690 
2691 		/* Try again at next PSB in buffer 'a' */
2692 		if (!intel_pt_step_psb(&buf_a, &len_a))
2693 			return buf_b; /* No overlap */
2694 	}
2695 }
2696