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