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