blob: 56790ea1e88e67421fc9bd7d36be6a19c78892c3 [file] [log] [blame]
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001/*
2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#ifndef _GNU_SOURCE
17#define _GNU_SOURCE
18#endif
19#include <stdlib.h>
20#include <stdbool.h>
21#include <string.h>
22#include <errno.h>
23#include <stdint.h>
24#include <inttypes.h>
25
26#include "../cache.h"
27#include "../util.h"
28
29#include "intel-pt-insn-decoder.h"
30#include "intel-pt-pkt-decoder.h"
31#include "intel-pt-decoder.h"
32#include "intel-pt-log.h"
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
43struct intel_pt_blk {
44 struct intel_pt_blk *prev;
45 uint64_t ip[INTEL_PT_BLK_SIZE];
46};
47
48struct intel_pt_stack {
49 struct intel_pt_blk *blk;
50 struct intel_pt_blk *spare;
51 int pos;
52};
53
54enum 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,
60 INTEL_PT_STATE_TIP,
61 INTEL_PT_STATE_TIP_PGD,
62 INTEL_PT_STATE_FUP,
63 INTEL_PT_STATE_FUP_NO_TIP,
64};
65
66#ifdef INTEL_PT_STRICT
67#define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
68#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
69#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
70#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
71#else
72#define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
73#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
74#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
75#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
76#endif
77
78struct intel_pt_decoder {
79 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
80 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
81 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
82 uint64_t max_insn_cnt, void *data);
83 void *data;
84 struct intel_pt_state state;
85 const unsigned char *buf;
86 size_t len;
87 bool return_compression;
88 bool pge;
89 uint64_t pos;
90 uint64_t last_ip;
91 uint64_t ip;
92 uint64_t cr3;
93 uint64_t timestamp;
94 uint64_t tsc_timestamp;
95 uint64_t ref_timestamp;
96 uint64_t ret_addr;
97 struct intel_pt_stack stack;
98 enum intel_pt_pkt_state pkt_state;
99 struct intel_pt_pkt packet;
100 struct intel_pt_pkt tnt;
101 int pkt_step;
102 int pkt_len;
103 unsigned int cbr;
104 unsigned int max_non_turbo_ratio;
105 int exec_mode;
106 unsigned int insn_bytes;
107 uint64_t sign_bit;
108 uint64_t sign_bits;
109 uint64_t period;
110 enum intel_pt_period_type period_type;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300111 uint64_t tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300112 uint64_t period_insn_cnt;
113 uint64_t period_mask;
114 uint64_t period_ticks;
115 uint64_t last_masked_timestamp;
116 bool continuous_period;
117 bool overflow;
118 bool set_fup_tx_flags;
119 unsigned int fup_tx_flags;
120 unsigned int tx_flags;
121 uint64_t timestamp_insn_cnt;
122 uint64_t stuck_ip;
123 int no_progress;
124 int stuck_ip_prd;
125 int stuck_ip_cnt;
126 const unsigned char *next_buf;
127 size_t next_len;
128 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
129};
130
131static uint64_t intel_pt_lower_power_of_2(uint64_t x)
132{
133 int i;
134
135 for (i = 0; x != 1; i++)
136 x >>= 1;
137
138 return x << i;
139}
140
141static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
142{
143 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
144 uint64_t period;
145
146 period = intel_pt_lower_power_of_2(decoder->period);
147 decoder->period_mask = ~(period - 1);
148 decoder->period_ticks = period;
149 }
150}
151
152struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
153{
154 struct intel_pt_decoder *decoder;
155
156 if (!params->get_trace || !params->walk_insn)
157 return NULL;
158
159 decoder = zalloc(sizeof(struct intel_pt_decoder));
160 if (!decoder)
161 return NULL;
162
163 decoder->get_trace = params->get_trace;
164 decoder->walk_insn = params->walk_insn;
165 decoder->data = params->data;
166 decoder->return_compression = params->return_compression;
167
168 decoder->sign_bit = (uint64_t)1 << 47;
169 decoder->sign_bits = ~(((uint64_t)1 << 48) - 1);
170
171 decoder->period = params->period;
172 decoder->period_type = params->period_type;
173
174 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
175
176 intel_pt_setup_period(decoder);
177
178 return decoder;
179}
180
181static void intel_pt_pop_blk(struct intel_pt_stack *stack)
182{
183 struct intel_pt_blk *blk = stack->blk;
184
185 stack->blk = blk->prev;
186 if (!stack->spare)
187 stack->spare = blk;
188 else
189 free(blk);
190}
191
192static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
193{
194 if (!stack->pos) {
195 if (!stack->blk)
196 return 0;
197 intel_pt_pop_blk(stack);
198 if (!stack->blk)
199 return 0;
200 stack->pos = INTEL_PT_BLK_SIZE;
201 }
202 return stack->blk->ip[--stack->pos];
203}
204
205static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
206{
207 struct intel_pt_blk *blk;
208
209 if (stack->spare) {
210 blk = stack->spare;
211 stack->spare = NULL;
212 } else {
213 blk = malloc(sizeof(struct intel_pt_blk));
214 if (!blk)
215 return -ENOMEM;
216 }
217
218 blk->prev = stack->blk;
219 stack->blk = blk;
220 stack->pos = 0;
221 return 0;
222}
223
224static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
225{
226 int err;
227
228 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
229 err = intel_pt_alloc_blk(stack);
230 if (err)
231 return err;
232 }
233
234 stack->blk->ip[stack->pos++] = ip;
235 return 0;
236}
237
238static void intel_pt_clear_stack(struct intel_pt_stack *stack)
239{
240 while (stack->blk)
241 intel_pt_pop_blk(stack);
242 stack->pos = 0;
243}
244
245static void intel_pt_free_stack(struct intel_pt_stack *stack)
246{
247 intel_pt_clear_stack(stack);
248 zfree(&stack->blk);
249 zfree(&stack->spare);
250}
251
252void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
253{
254 intel_pt_free_stack(&decoder->stack);
255 free(decoder);
256}
257
258static int intel_pt_ext_err(int code)
259{
260 switch (code) {
261 case -ENOMEM:
262 return INTEL_PT_ERR_NOMEM;
263 case -ENOSYS:
264 return INTEL_PT_ERR_INTERN;
265 case -EBADMSG:
266 return INTEL_PT_ERR_BADPKT;
267 case -ENODATA:
268 return INTEL_PT_ERR_NODATA;
269 case -EILSEQ:
270 return INTEL_PT_ERR_NOINSN;
271 case -ENOENT:
272 return INTEL_PT_ERR_MISMAT;
273 case -EOVERFLOW:
274 return INTEL_PT_ERR_OVR;
275 case -ENOSPC:
276 return INTEL_PT_ERR_LOST;
277 case -ELOOP:
278 return INTEL_PT_ERR_NELOOP;
279 default:
280 return INTEL_PT_ERR_UNK;
281 }
282}
283
284static const char *intel_pt_err_msgs[] = {
285 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
286 [INTEL_PT_ERR_INTERN] = "Internal error",
287 [INTEL_PT_ERR_BADPKT] = "Bad packet",
288 [INTEL_PT_ERR_NODATA] = "No more data",
289 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
290 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
291 [INTEL_PT_ERR_OVR] = "Overflow packet",
292 [INTEL_PT_ERR_LOST] = "Lost trace data",
293 [INTEL_PT_ERR_UNK] = "Unknown error!",
294 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
295};
296
297int intel_pt__strerror(int code, char *buf, size_t buflen)
298{
299 if (code < 1 || code > INTEL_PT_ERR_MAX)
300 code = INTEL_PT_ERR_UNK;
301 strlcpy(buf, intel_pt_err_msgs[code], buflen);
302 return 0;
303}
304
305static uint64_t intel_pt_calc_ip(struct intel_pt_decoder *decoder,
306 const struct intel_pt_pkt *packet,
307 uint64_t last_ip)
308{
309 uint64_t ip;
310
311 switch (packet->count) {
312 case 2:
313 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
314 packet->payload;
315 break;
316 case 4:
317 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
318 packet->payload;
319 break;
320 case 6:
321 ip = packet->payload;
322 break;
323 default:
324 return 0;
325 }
326
327 if (ip & decoder->sign_bit)
328 return ip | decoder->sign_bits;
329
330 return ip;
331}
332
333static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
334{
335 decoder->last_ip = intel_pt_calc_ip(decoder, &decoder->packet,
336 decoder->last_ip);
337}
338
339static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
340{
341 intel_pt_set_last_ip(decoder);
342 decoder->ip = decoder->last_ip;
343}
344
345static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
346{
347 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
348 decoder->buf);
349}
350
351static int intel_pt_bug(struct intel_pt_decoder *decoder)
352{
353 intel_pt_log("ERROR: Internal error\n");
354 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
355 return -ENOSYS;
356}
357
358static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
359{
360 decoder->tx_flags = 0;
361}
362
363static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
364{
365 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
366}
367
368static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
369{
370 intel_pt_clear_tx_flags(decoder);
371 decoder->pkt_len = 1;
372 decoder->pkt_step = 1;
373 intel_pt_decoder_log_packet(decoder);
374 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
375 intel_pt_log("ERROR: Bad packet\n");
376 decoder->pkt_state = INTEL_PT_STATE_ERR1;
377 }
378 return -EBADMSG;
379}
380
381static int intel_pt_get_data(struct intel_pt_decoder *decoder)
382{
383 struct intel_pt_buffer buffer = { .buf = 0, };
384 int ret;
385
386 decoder->pkt_step = 0;
387
388 intel_pt_log("Getting more data\n");
389 ret = decoder->get_trace(&buffer, decoder->data);
390 if (ret)
391 return ret;
392 decoder->buf = buffer.buf;
393 decoder->len = buffer.len;
394 if (!decoder->len) {
395 intel_pt_log("No more data\n");
396 return -ENODATA;
397 }
398 if (!buffer.consecutive) {
399 decoder->ip = 0;
400 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
401 decoder->ref_timestamp = buffer.ref_timestamp;
402 decoder->timestamp = 0;
403 decoder->state.trace_nr = buffer.trace_nr;
404 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
405 decoder->ref_timestamp);
406 return -ENOLINK;
407 }
408
409 return 0;
410}
411
412static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
413{
414 if (!decoder->next_buf)
415 return intel_pt_get_data(decoder);
416
417 decoder->buf = decoder->next_buf;
418 decoder->len = decoder->next_len;
419 decoder->next_buf = 0;
420 decoder->next_len = 0;
421 return 0;
422}
423
424static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
425{
426 unsigned char *buf = decoder->temp_buf;
427 size_t old_len, len, n;
428 int ret;
429
430 old_len = decoder->len;
431 len = decoder->len;
432 memcpy(buf, decoder->buf, len);
433
434 ret = intel_pt_get_data(decoder);
435 if (ret) {
436 decoder->pos += old_len;
437 return ret < 0 ? ret : -EINVAL;
438 }
439
440 n = INTEL_PT_PKT_MAX_SZ - len;
441 if (n > decoder->len)
442 n = decoder->len;
443 memcpy(buf + len, decoder->buf, n);
444 len += n;
445
446 ret = intel_pt_get_packet(buf, len, &decoder->packet);
447 if (ret < (int)old_len) {
448 decoder->next_buf = decoder->buf;
449 decoder->next_len = decoder->len;
450 decoder->buf = buf;
451 decoder->len = old_len;
452 return intel_pt_bad_packet(decoder);
453 }
454
455 decoder->next_buf = decoder->buf + (ret - old_len);
456 decoder->next_len = decoder->len - (ret - old_len);
457
458 decoder->buf = buf;
459 decoder->len = ret;
460
461 return ret;
462}
463
464static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
465{
466 int ret;
467
468 do {
469 decoder->pos += decoder->pkt_step;
470 decoder->buf += decoder->pkt_step;
471 decoder->len -= decoder->pkt_step;
472
473 if (!decoder->len) {
474 ret = intel_pt_get_next_data(decoder);
475 if (ret)
476 return ret;
477 }
478
479 ret = intel_pt_get_packet(decoder->buf, decoder->len,
480 &decoder->packet);
481 if (ret == INTEL_PT_NEED_MORE_BYTES &&
482 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
483 ret = intel_pt_get_split_packet(decoder);
484 if (ret < 0)
485 return ret;
486 }
487 if (ret <= 0)
488 return intel_pt_bad_packet(decoder);
489
490 decoder->pkt_len = ret;
491 decoder->pkt_step = ret;
492 intel_pt_decoder_log_packet(decoder);
493 } while (decoder->packet.type == INTEL_PT_PAD);
494
495 return 0;
496}
497
498static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
499{
500 uint64_t timestamp, masked_timestamp;
501
502 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
503 masked_timestamp = timestamp & decoder->period_mask;
504 if (decoder->continuous_period) {
505 if (masked_timestamp != decoder->last_masked_timestamp)
506 return 1;
507 } else {
508 timestamp += 1;
509 masked_timestamp = timestamp & decoder->period_mask;
510 if (masked_timestamp != decoder->last_masked_timestamp) {
511 decoder->last_masked_timestamp = masked_timestamp;
512 decoder->continuous_period = true;
513 }
514 }
515 return decoder->period_ticks - (timestamp - masked_timestamp);
516}
517
518static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
519{
520 switch (decoder->period_type) {
521 case INTEL_PT_PERIOD_INSTRUCTIONS:
522 return decoder->period - decoder->period_insn_cnt;
523 case INTEL_PT_PERIOD_TICKS:
524 return intel_pt_next_period(decoder);
525 case INTEL_PT_PERIOD_NONE:
526 default:
527 return 0;
528 }
529}
530
531static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
532{
533 uint64_t timestamp, masked_timestamp;
534
535 switch (decoder->period_type) {
536 case INTEL_PT_PERIOD_INSTRUCTIONS:
537 decoder->period_insn_cnt = 0;
538 break;
539 case INTEL_PT_PERIOD_TICKS:
540 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
541 masked_timestamp = timestamp & decoder->period_mask;
542 decoder->last_masked_timestamp = masked_timestamp;
543 break;
544 case INTEL_PT_PERIOD_NONE:
545 default:
546 break;
547 }
548
549 decoder->state.type |= INTEL_PT_INSTRUCTION;
550}
551
552static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
553 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
554{
555 uint64_t max_insn_cnt, insn_cnt = 0;
556 int err;
557
558 max_insn_cnt = intel_pt_next_sample(decoder);
559
560 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
561 max_insn_cnt, decoder->data);
562
Adrian Hunter2a21d032015-07-17 19:33:48 +0300563 decoder->tot_insn_cnt += insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300564 decoder->timestamp_insn_cnt += insn_cnt;
565 decoder->period_insn_cnt += insn_cnt;
566
567 if (err) {
568 decoder->no_progress = 0;
569 decoder->pkt_state = INTEL_PT_STATE_ERR2;
570 intel_pt_log_at("ERROR: Failed to get instruction",
571 decoder->ip);
572 if (err == -ENOENT)
573 return -ENOLINK;
574 return -EILSEQ;
575 }
576
577 if (ip && decoder->ip == ip) {
578 err = -EAGAIN;
579 goto out;
580 }
581
582 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
583 intel_pt_sample_insn(decoder);
584
585 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
586 decoder->state.type = INTEL_PT_INSTRUCTION;
587 decoder->state.from_ip = decoder->ip;
588 decoder->state.to_ip = 0;
589 decoder->ip += intel_pt_insn->length;
590 err = INTEL_PT_RETURN;
591 goto out;
592 }
593
594 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
595 /* Zero-length calls are excluded */
596 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
597 intel_pt_insn->rel) {
598 err = intel_pt_push(&decoder->stack, decoder->ip +
599 intel_pt_insn->length);
600 if (err)
601 goto out;
602 }
603 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
604 decoder->ret_addr = intel_pt_pop(&decoder->stack);
605 }
606
607 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
608 int cnt = decoder->no_progress++;
609
610 decoder->state.from_ip = decoder->ip;
611 decoder->ip += intel_pt_insn->length +
612 intel_pt_insn->rel;
613 decoder->state.to_ip = decoder->ip;
614 err = INTEL_PT_RETURN;
615
616 /*
617 * Check for being stuck in a loop. This can happen if a
618 * decoder error results in the decoder erroneously setting the
619 * ip to an address that is itself in an infinite loop that
620 * consumes no packets. When that happens, there must be an
621 * unconditional branch.
622 */
623 if (cnt) {
624 if (cnt == 1) {
625 decoder->stuck_ip = decoder->state.to_ip;
626 decoder->stuck_ip_prd = 1;
627 decoder->stuck_ip_cnt = 1;
628 } else if (cnt > INTEL_PT_MAX_LOOPS ||
629 decoder->state.to_ip == decoder->stuck_ip) {
630 intel_pt_log_at("ERROR: Never-ending loop",
631 decoder->state.to_ip);
632 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
633 err = -ELOOP;
634 goto out;
635 } else if (!--decoder->stuck_ip_cnt) {
636 decoder->stuck_ip_prd += 1;
637 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
638 decoder->stuck_ip = decoder->state.to_ip;
639 }
640 }
641 goto out_no_progress;
642 }
643out:
644 decoder->no_progress = 0;
645out_no_progress:
646 decoder->state.insn_op = intel_pt_insn->op;
647 decoder->state.insn_len = intel_pt_insn->length;
648
649 if (decoder->tx_flags & INTEL_PT_IN_TX)
650 decoder->state.flags |= INTEL_PT_IN_TX;
651
652 return err;
653}
654
655static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
656{
657 struct intel_pt_insn intel_pt_insn;
658 uint64_t ip;
659 int err;
660
661 ip = decoder->last_ip;
662
663 while (1) {
664 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
665 if (err == INTEL_PT_RETURN)
666 return 0;
667 if (err == -EAGAIN) {
668 if (decoder->set_fup_tx_flags) {
669 decoder->set_fup_tx_flags = false;
670 decoder->tx_flags = decoder->fup_tx_flags;
671 decoder->state.type = INTEL_PT_TRANSACTION;
672 decoder->state.from_ip = decoder->ip;
673 decoder->state.to_ip = 0;
674 decoder->state.flags = decoder->fup_tx_flags;
675 return 0;
676 }
677 return err;
678 }
679 decoder->set_fup_tx_flags = false;
680 if (err)
681 return err;
682
683 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
684 intel_pt_log_at("ERROR: Unexpected indirect branch",
685 decoder->ip);
686 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
687 return -ENOENT;
688 }
689
690 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
691 intel_pt_log_at("ERROR: Unexpected conditional branch",
692 decoder->ip);
693 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
694 return -ENOENT;
695 }
696
697 intel_pt_bug(decoder);
698 }
699}
700
701static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
702{
703 struct intel_pt_insn intel_pt_insn;
704 int err;
705
706 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
707 if (err == INTEL_PT_RETURN)
708 return 0;
709 if (err)
710 return err;
711
712 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
713 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
714 decoder->pge = false;
715 decoder->continuous_period = false;
716 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
717 decoder->state.from_ip = decoder->ip;
718 decoder->state.to_ip = 0;
719 if (decoder->packet.count != 0)
720 decoder->ip = decoder->last_ip;
721 } else {
722 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
723 decoder->state.from_ip = decoder->ip;
724 if (decoder->packet.count == 0) {
725 decoder->state.to_ip = 0;
726 } else {
727 decoder->state.to_ip = decoder->last_ip;
728 decoder->ip = decoder->last_ip;
729 }
730 }
731 return 0;
732 }
733
734 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
735 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
736 decoder->ip);
737 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
738 return -ENOENT;
739 }
740
741 return intel_pt_bug(decoder);
742}
743
744static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
745{
746 struct intel_pt_insn intel_pt_insn;
747 int err;
748
749 while (1) {
750 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
751 if (err == INTEL_PT_RETURN)
752 return 0;
753 if (err)
754 return err;
755
756 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
757 if (!decoder->return_compression) {
758 intel_pt_log_at("ERROR: RET when expecting conditional branch",
759 decoder->ip);
760 decoder->pkt_state = INTEL_PT_STATE_ERR3;
761 return -ENOENT;
762 }
763 if (!decoder->ret_addr) {
764 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
765 decoder->ip);
766 decoder->pkt_state = INTEL_PT_STATE_ERR3;
767 return -ENOENT;
768 }
769 if (!(decoder->tnt.payload & BIT63)) {
770 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
771 decoder->ip);
772 decoder->pkt_state = INTEL_PT_STATE_ERR3;
773 return -ENOENT;
774 }
775 decoder->tnt.count -= 1;
776 if (!decoder->tnt.count)
777 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
778 decoder->tnt.payload <<= 1;
779 decoder->state.from_ip = decoder->ip;
780 decoder->ip = decoder->ret_addr;
781 decoder->state.to_ip = decoder->ip;
782 return 0;
783 }
784
785 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
786 /* Handle deferred TIPs */
787 err = intel_pt_get_next_packet(decoder);
788 if (err)
789 return err;
790 if (decoder->packet.type != INTEL_PT_TIP ||
791 decoder->packet.count == 0) {
792 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
793 decoder->ip);
794 decoder->pkt_state = INTEL_PT_STATE_ERR3;
795 decoder->pkt_step = 0;
796 return -ENOENT;
797 }
798 intel_pt_set_last_ip(decoder);
799 decoder->state.from_ip = decoder->ip;
800 decoder->state.to_ip = decoder->last_ip;
801 decoder->ip = decoder->last_ip;
802 return 0;
803 }
804
805 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
806 decoder->tnt.count -= 1;
807 if (!decoder->tnt.count)
808 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
809 if (decoder->tnt.payload & BIT63) {
810 decoder->tnt.payload <<= 1;
811 decoder->state.from_ip = decoder->ip;
812 decoder->ip += intel_pt_insn.length +
813 intel_pt_insn.rel;
814 decoder->state.to_ip = decoder->ip;
815 return 0;
816 }
817 /* Instruction sample for a non-taken branch */
818 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
819 decoder->tnt.payload <<= 1;
820 decoder->state.type = INTEL_PT_INSTRUCTION;
821 decoder->state.from_ip = decoder->ip;
822 decoder->state.to_ip = 0;
823 decoder->ip += intel_pt_insn.length;
824 return 0;
825 }
826 decoder->ip += intel_pt_insn.length;
827 if (!decoder->tnt.count)
828 return -EAGAIN;
829 decoder->tnt.payload <<= 1;
830 continue;
831 }
832
833 return intel_pt_bug(decoder);
834 }
835}
836
837static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
838{
839 unsigned int fup_tx_flags;
840 int err;
841
842 fup_tx_flags = decoder->packet.payload &
843 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
844 err = intel_pt_get_next_packet(decoder);
845 if (err)
846 return err;
847 if (decoder->packet.type == INTEL_PT_FUP) {
848 decoder->fup_tx_flags = fup_tx_flags;
849 decoder->set_fup_tx_flags = true;
850 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
851 *no_tip = true;
852 } else {
853 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
854 decoder->pos);
855 intel_pt_update_in_tx(decoder);
856 }
857 return 0;
858}
859
860static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
861{
862 uint64_t timestamp;
863
864 if (decoder->ref_timestamp) {
865 timestamp = decoder->packet.payload |
866 (decoder->ref_timestamp & (0xffULL << 56));
867 if (timestamp < decoder->ref_timestamp) {
868 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
869 timestamp += (1ULL << 56);
870 } else {
871 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
872 timestamp -= (1ULL << 56);
873 }
874 decoder->tsc_timestamp = timestamp;
875 decoder->timestamp = timestamp;
876 decoder->ref_timestamp = 0;
877 decoder->timestamp_insn_cnt = 0;
878 } else if (decoder->timestamp) {
879 timestamp = decoder->packet.payload |
880 (decoder->timestamp & (0xffULL << 56));
881 if (timestamp < decoder->timestamp &&
882 decoder->timestamp - timestamp < 0x100) {
883 intel_pt_log_to("ERROR: Suppressing backwards timestamp",
884 timestamp);
885 timestamp = decoder->timestamp;
886 }
887 while (timestamp < decoder->timestamp) {
888 intel_pt_log_to("Wraparound timestamp", timestamp);
889 timestamp += (1ULL << 56);
890 }
891 decoder->tsc_timestamp = timestamp;
892 decoder->timestamp = timestamp;
893 decoder->timestamp_insn_cnt = 0;
894 }
895
896 intel_pt_log_to("Setting timestamp", decoder->timestamp);
897}
898
899static int intel_pt_overflow(struct intel_pt_decoder *decoder)
900{
901 intel_pt_log("ERROR: Buffer overflow\n");
902 intel_pt_clear_tx_flags(decoder);
903 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
904 decoder->overflow = true;
905 return -EOVERFLOW;
906}
907
908/* Walk PSB+ packets when already in sync. */
909static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
910{
911 int err;
912
913 while (1) {
914 err = intel_pt_get_next_packet(decoder);
915 if (err)
916 return err;
917
918 switch (decoder->packet.type) {
919 case INTEL_PT_PSBEND:
920 return 0;
921
922 case INTEL_PT_TIP_PGD:
923 case INTEL_PT_TIP_PGE:
924 case INTEL_PT_TIP:
925 case INTEL_PT_TNT:
926 case INTEL_PT_BAD:
927 case INTEL_PT_PSB:
928 intel_pt_log("ERROR: Unexpected packet\n");
929 return -EAGAIN;
930
931 case INTEL_PT_OVF:
932 return intel_pt_overflow(decoder);
933
934 case INTEL_PT_TSC:
935 intel_pt_calc_tsc_timestamp(decoder);
936 break;
937
938 case INTEL_PT_CBR:
939 decoder->cbr = decoder->packet.payload;
940 break;
941
942 case INTEL_PT_MODE_EXEC:
943 decoder->exec_mode = decoder->packet.payload;
944 break;
945
946 case INTEL_PT_PIP:
947 decoder->cr3 = decoder->packet.payload;
948 break;
949
950 case INTEL_PT_FUP:
951 decoder->pge = true;
952 intel_pt_set_last_ip(decoder);
953 break;
954
955 case INTEL_PT_MODE_TSX:
956 intel_pt_update_in_tx(decoder);
957 break;
958
959 case INTEL_PT_PAD:
960 default:
961 break;
962 }
963 }
964}
965
966static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
967{
968 int err;
969
970 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
971 decoder->tx_flags = 0;
972 decoder->state.flags &= ~INTEL_PT_IN_TX;
973 decoder->state.flags |= INTEL_PT_ABORT_TX;
974 } else {
975 decoder->state.flags |= INTEL_PT_ASYNC;
976 }
977
978 while (1) {
979 err = intel_pt_get_next_packet(decoder);
980 if (err)
981 return err;
982
983 switch (decoder->packet.type) {
984 case INTEL_PT_TNT:
985 case INTEL_PT_FUP:
986 case INTEL_PT_PSB:
987 case INTEL_PT_TSC:
988 case INTEL_PT_CBR:
989 case INTEL_PT_MODE_TSX:
990 case INTEL_PT_BAD:
991 case INTEL_PT_PSBEND:
992 intel_pt_log("ERROR: Missing TIP after FUP\n");
993 decoder->pkt_state = INTEL_PT_STATE_ERR3;
994 return -ENOENT;
995
996 case INTEL_PT_OVF:
997 return intel_pt_overflow(decoder);
998
999 case INTEL_PT_TIP_PGD:
1000 decoder->state.from_ip = decoder->ip;
1001 decoder->state.to_ip = 0;
1002 if (decoder->packet.count != 0) {
1003 intel_pt_set_ip(decoder);
1004 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1005 decoder->ip);
1006 }
1007 decoder->pge = false;
1008 decoder->continuous_period = false;
1009 return 0;
1010
1011 case INTEL_PT_TIP_PGE:
1012 decoder->pge = true;
1013 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1014 decoder->ip);
1015 decoder->state.from_ip = 0;
1016 if (decoder->packet.count == 0) {
1017 decoder->state.to_ip = 0;
1018 } else {
1019 intel_pt_set_ip(decoder);
1020 decoder->state.to_ip = decoder->ip;
1021 }
1022 return 0;
1023
1024 case INTEL_PT_TIP:
1025 decoder->state.from_ip = decoder->ip;
1026 if (decoder->packet.count == 0) {
1027 decoder->state.to_ip = 0;
1028 } else {
1029 intel_pt_set_ip(decoder);
1030 decoder->state.to_ip = decoder->ip;
1031 }
1032 return 0;
1033
1034 case INTEL_PT_PIP:
1035 decoder->cr3 = decoder->packet.payload;
1036 break;
1037
1038 case INTEL_PT_MODE_EXEC:
1039 decoder->exec_mode = decoder->packet.payload;
1040 break;
1041
1042 case INTEL_PT_PAD:
1043 break;
1044
1045 default:
1046 return intel_pt_bug(decoder);
1047 }
1048 }
1049}
1050
1051static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1052{
1053 bool no_tip = false;
1054 int err;
1055
1056 while (1) {
1057 err = intel_pt_get_next_packet(decoder);
1058 if (err)
1059 return err;
1060next:
1061 switch (decoder->packet.type) {
1062 case INTEL_PT_TNT:
1063 if (!decoder->packet.count)
1064 break;
1065 decoder->tnt = decoder->packet;
1066 decoder->pkt_state = INTEL_PT_STATE_TNT;
1067 err = intel_pt_walk_tnt(decoder);
1068 if (err == -EAGAIN)
1069 break;
1070 return err;
1071
1072 case INTEL_PT_TIP_PGD:
1073 if (decoder->packet.count != 0)
1074 intel_pt_set_last_ip(decoder);
1075 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1076 return intel_pt_walk_tip(decoder);
1077
1078 case INTEL_PT_TIP_PGE: {
1079 decoder->pge = true;
1080 if (decoder->packet.count == 0) {
1081 intel_pt_log_at("Skipping zero TIP.PGE",
1082 decoder->pos);
1083 break;
1084 }
1085 intel_pt_set_ip(decoder);
1086 decoder->state.from_ip = 0;
1087 decoder->state.to_ip = decoder->ip;
1088 return 0;
1089 }
1090
1091 case INTEL_PT_OVF:
1092 return intel_pt_overflow(decoder);
1093
1094 case INTEL_PT_TIP:
1095 if (decoder->packet.count != 0)
1096 intel_pt_set_last_ip(decoder);
1097 decoder->pkt_state = INTEL_PT_STATE_TIP;
1098 return intel_pt_walk_tip(decoder);
1099
1100 case INTEL_PT_FUP:
1101 if (decoder->packet.count == 0) {
1102 intel_pt_log_at("Skipping zero FUP",
1103 decoder->pos);
1104 no_tip = false;
1105 break;
1106 }
1107 intel_pt_set_last_ip(decoder);
1108 err = intel_pt_walk_fup(decoder);
1109 if (err != -EAGAIN) {
1110 if (err)
1111 return err;
1112 if (no_tip)
1113 decoder->pkt_state =
1114 INTEL_PT_STATE_FUP_NO_TIP;
1115 else
1116 decoder->pkt_state = INTEL_PT_STATE_FUP;
1117 return 0;
1118 }
1119 if (no_tip) {
1120 no_tip = false;
1121 break;
1122 }
1123 return intel_pt_walk_fup_tip(decoder);
1124
1125 case INTEL_PT_PSB:
1126 intel_pt_clear_stack(&decoder->stack);
1127 err = intel_pt_walk_psbend(decoder);
1128 if (err == -EAGAIN)
1129 goto next;
1130 if (err)
1131 return err;
1132 break;
1133
1134 case INTEL_PT_PIP:
1135 decoder->cr3 = decoder->packet.payload;
1136 break;
1137
1138 case INTEL_PT_TSC:
1139 intel_pt_calc_tsc_timestamp(decoder);
1140 break;
1141
1142 case INTEL_PT_CBR:
1143 decoder->cbr = decoder->packet.payload;
1144 break;
1145
1146 case INTEL_PT_MODE_EXEC:
1147 decoder->exec_mode = decoder->packet.payload;
1148 break;
1149
1150 case INTEL_PT_MODE_TSX:
1151 /* MODE_TSX need not be followed by FUP */
1152 if (!decoder->pge) {
1153 intel_pt_update_in_tx(decoder);
1154 break;
1155 }
1156 err = intel_pt_mode_tsx(decoder, &no_tip);
1157 if (err)
1158 return err;
1159 goto next;
1160
1161 case INTEL_PT_BAD: /* Does not happen */
1162 return intel_pt_bug(decoder);
1163
1164 case INTEL_PT_PSBEND:
1165 case INTEL_PT_PAD:
1166 break;
1167
1168 default:
1169 return intel_pt_bug(decoder);
1170 }
1171 }
1172}
1173
1174/* Walk PSB+ packets to get in sync. */
1175static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1176{
1177 int err;
1178
1179 while (1) {
1180 err = intel_pt_get_next_packet(decoder);
1181 if (err)
1182 return err;
1183
1184 switch (decoder->packet.type) {
1185 case INTEL_PT_TIP_PGD:
1186 decoder->continuous_period = false;
1187 case INTEL_PT_TIP_PGE:
1188 case INTEL_PT_TIP:
1189 intel_pt_log("ERROR: Unexpected packet\n");
1190 return -ENOENT;
1191
1192 case INTEL_PT_FUP:
1193 decoder->pge = true;
1194 if (decoder->last_ip || decoder->packet.count == 6 ||
1195 decoder->packet.count == 0) {
1196 uint64_t current_ip = decoder->ip;
1197
1198 intel_pt_set_ip(decoder);
1199 if (current_ip)
1200 intel_pt_log_to("Setting IP",
1201 decoder->ip);
1202 }
1203 break;
1204
1205 case INTEL_PT_TSC:
1206 intel_pt_calc_tsc_timestamp(decoder);
1207 break;
1208
1209 case INTEL_PT_CBR:
1210 decoder->cbr = decoder->packet.payload;
1211 break;
1212
1213 case INTEL_PT_PIP:
1214 decoder->cr3 = decoder->packet.payload;
1215 break;
1216
1217 case INTEL_PT_MODE_EXEC:
1218 decoder->exec_mode = decoder->packet.payload;
1219 break;
1220
1221 case INTEL_PT_MODE_TSX:
1222 intel_pt_update_in_tx(decoder);
1223 break;
1224
1225 case INTEL_PT_TNT:
1226 intel_pt_log("ERROR: Unexpected packet\n");
1227 if (decoder->ip)
1228 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1229 else
1230 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1231 return -ENOENT;
1232
1233 case INTEL_PT_BAD: /* Does not happen */
1234 return intel_pt_bug(decoder);
1235
1236 case INTEL_PT_OVF:
1237 return intel_pt_overflow(decoder);
1238
1239 case INTEL_PT_PSBEND:
1240 return 0;
1241
1242 case INTEL_PT_PSB:
1243 case INTEL_PT_PAD:
1244 default:
1245 break;
1246 }
1247 }
1248}
1249
1250static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1251{
1252 int err;
1253
1254 while (1) {
1255 err = intel_pt_get_next_packet(decoder);
1256 if (err)
1257 return err;
1258
1259 switch (decoder->packet.type) {
1260 case INTEL_PT_TIP_PGD:
1261 decoder->continuous_period = false;
1262 case INTEL_PT_TIP_PGE:
1263 case INTEL_PT_TIP:
1264 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1265 if (decoder->last_ip || decoder->packet.count == 6 ||
1266 decoder->packet.count == 0)
1267 intel_pt_set_ip(decoder);
1268 if (decoder->ip)
1269 return 0;
1270 break;
1271
1272 case INTEL_PT_FUP:
1273 if (decoder->overflow) {
1274 if (decoder->last_ip ||
1275 decoder->packet.count == 6 ||
1276 decoder->packet.count == 0)
1277 intel_pt_set_ip(decoder);
1278 if (decoder->ip)
1279 return 0;
1280 }
1281 if (decoder->packet.count)
1282 intel_pt_set_last_ip(decoder);
1283 break;
1284
1285 case INTEL_PT_TSC:
1286 intel_pt_calc_tsc_timestamp(decoder);
1287 break;
1288
1289 case INTEL_PT_CBR:
1290 decoder->cbr = decoder->packet.payload;
1291 break;
1292
1293 case INTEL_PT_PIP:
1294 decoder->cr3 = decoder->packet.payload;
1295 break;
1296
1297 case INTEL_PT_MODE_EXEC:
1298 decoder->exec_mode = decoder->packet.payload;
1299 break;
1300
1301 case INTEL_PT_MODE_TSX:
1302 intel_pt_update_in_tx(decoder);
1303 break;
1304
1305 case INTEL_PT_OVF:
1306 return intel_pt_overflow(decoder);
1307
1308 case INTEL_PT_BAD: /* Does not happen */
1309 return intel_pt_bug(decoder);
1310
1311 case INTEL_PT_PSB:
1312 err = intel_pt_walk_psb(decoder);
1313 if (err)
1314 return err;
1315 if (decoder->ip) {
1316 /* Do not have a sample */
1317 decoder->state.type = 0;
1318 return 0;
1319 }
1320 break;
1321
1322 case INTEL_PT_TNT:
1323 case INTEL_PT_PSBEND:
1324 case INTEL_PT_PAD:
1325 default:
1326 break;
1327 }
1328 }
1329}
1330
1331static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1332{
1333 int err;
1334
1335 intel_pt_log("Scanning for full IP\n");
1336 err = intel_pt_walk_to_ip(decoder);
1337 if (err)
1338 return err;
1339
1340 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1341 decoder->overflow = false;
1342
1343 decoder->state.from_ip = 0;
1344 decoder->state.to_ip = decoder->ip;
1345 intel_pt_log_to("Setting IP", decoder->ip);
1346
1347 return 0;
1348}
1349
1350static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1351{
1352 const unsigned char *end = decoder->buf + decoder->len;
1353 size_t i;
1354
1355 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1356 if (i > decoder->len)
1357 continue;
1358 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1359 return i;
1360 }
1361 return 0;
1362}
1363
1364static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1365{
1366 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1367 const char *psb = INTEL_PT_PSB_STR;
1368
1369 if (rest_psb > decoder->len ||
1370 memcmp(decoder->buf, psb + part_psb, rest_psb))
1371 return 0;
1372
1373 return rest_psb;
1374}
1375
1376static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
1377 int part_psb)
1378{
1379 int rest_psb, ret;
1380
1381 decoder->pos += decoder->len;
1382 decoder->len = 0;
1383
1384 ret = intel_pt_get_next_data(decoder);
1385 if (ret)
1386 return ret;
1387
1388 rest_psb = intel_pt_rest_psb(decoder, part_psb);
1389 if (!rest_psb)
1390 return 0;
1391
1392 decoder->pos -= part_psb;
1393 decoder->next_buf = decoder->buf + rest_psb;
1394 decoder->next_len = decoder->len - rest_psb;
1395 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1396 decoder->buf = decoder->temp_buf;
1397 decoder->len = INTEL_PT_PSB_LEN;
1398
1399 return 0;
1400}
1401
1402static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
1403{
1404 unsigned char *next;
1405 int ret;
1406
1407 intel_pt_log("Scanning for PSB\n");
1408 while (1) {
1409 if (!decoder->len) {
1410 ret = intel_pt_get_next_data(decoder);
1411 if (ret)
1412 return ret;
1413 }
1414
1415 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
1416 INTEL_PT_PSB_LEN);
1417 if (!next) {
1418 int part_psb;
1419
1420 part_psb = intel_pt_part_psb(decoder);
1421 if (part_psb) {
1422 ret = intel_pt_get_split_psb(decoder, part_psb);
1423 if (ret)
1424 return ret;
1425 } else {
1426 decoder->pos += decoder->len;
1427 decoder->len = 0;
1428 }
1429 continue;
1430 }
1431
1432 decoder->pkt_step = next - decoder->buf;
1433 return intel_pt_get_next_packet(decoder);
1434 }
1435}
1436
1437static int intel_pt_sync(struct intel_pt_decoder *decoder)
1438{
1439 int err;
1440
1441 decoder->pge = false;
1442 decoder->continuous_period = false;
1443 decoder->last_ip = 0;
1444 decoder->ip = 0;
1445 intel_pt_clear_stack(&decoder->stack);
1446
1447 err = intel_pt_scan_for_psb(decoder);
1448 if (err)
1449 return err;
1450
1451 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
1452
1453 err = intel_pt_walk_psb(decoder);
1454 if (err)
1455 return err;
1456
1457 if (decoder->ip) {
1458 decoder->state.type = 0; /* Do not have a sample */
1459 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1460 } else {
1461 return intel_pt_sync_ip(decoder);
1462 }
1463
1464 return 0;
1465}
1466
1467static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
1468{
1469 uint64_t est = decoder->timestamp_insn_cnt << 1;
1470
1471 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
1472 goto out;
1473
1474 est *= decoder->max_non_turbo_ratio;
1475 est /= decoder->cbr;
1476out:
1477 return decoder->timestamp + est;
1478}
1479
1480const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
1481{
1482 int err;
1483
1484 do {
1485 decoder->state.type = INTEL_PT_BRANCH;
1486 decoder->state.flags = 0;
1487
1488 switch (decoder->pkt_state) {
1489 case INTEL_PT_STATE_NO_PSB:
1490 err = intel_pt_sync(decoder);
1491 break;
1492 case INTEL_PT_STATE_NO_IP:
1493 decoder->last_ip = 0;
1494 /* Fall through */
1495 case INTEL_PT_STATE_ERR_RESYNC:
1496 err = intel_pt_sync_ip(decoder);
1497 break;
1498 case INTEL_PT_STATE_IN_SYNC:
1499 err = intel_pt_walk_trace(decoder);
1500 break;
1501 case INTEL_PT_STATE_TNT:
1502 err = intel_pt_walk_tnt(decoder);
1503 if (err == -EAGAIN)
1504 err = intel_pt_walk_trace(decoder);
1505 break;
1506 case INTEL_PT_STATE_TIP:
1507 case INTEL_PT_STATE_TIP_PGD:
1508 err = intel_pt_walk_tip(decoder);
1509 break;
1510 case INTEL_PT_STATE_FUP:
1511 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1512 err = intel_pt_walk_fup(decoder);
1513 if (err == -EAGAIN)
1514 err = intel_pt_walk_fup_tip(decoder);
1515 else if (!err)
1516 decoder->pkt_state = INTEL_PT_STATE_FUP;
1517 break;
1518 case INTEL_PT_STATE_FUP_NO_TIP:
1519 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1520 err = intel_pt_walk_fup(decoder);
1521 if (err == -EAGAIN)
1522 err = intel_pt_walk_trace(decoder);
1523 break;
1524 default:
1525 err = intel_pt_bug(decoder);
1526 break;
1527 }
1528 } while (err == -ENOLINK);
1529
1530 decoder->state.err = err ? intel_pt_ext_err(err) : 0;
1531 decoder->state.timestamp = decoder->timestamp;
1532 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
1533 decoder->state.cr3 = decoder->cr3;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001534 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001535
1536 if (err)
1537 decoder->state.from_ip = decoder->ip;
1538
1539 return &decoder->state;
1540}
1541
1542static bool intel_pt_at_psb(unsigned char *buf, size_t len)
1543{
1544 if (len < INTEL_PT_PSB_LEN)
1545 return false;
1546 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
1547 INTEL_PT_PSB_LEN);
1548}
1549
1550/**
1551 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
1552 * @buf: pointer to buffer pointer
1553 * @len: size of buffer
1554 *
1555 * Updates the buffer pointer to point to the start of the next PSB packet if
1556 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
1557 * @len is adjusted accordingly.
1558 *
1559 * Return: %true if a PSB packet is found, %false otherwise.
1560 */
1561static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
1562{
1563 unsigned char *next;
1564
1565 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1566 if (next) {
1567 *len -= next - *buf;
1568 *buf = next;
1569 return true;
1570 }
1571 return false;
1572}
1573
1574/**
1575 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
1576 * packet.
1577 * @buf: pointer to buffer pointer
1578 * @len: size of buffer
1579 *
1580 * Updates the buffer pointer to point to the start of the following PSB packet
1581 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
1582 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
1583 *
1584 * Return: %true if a PSB packet is found, %false otherwise.
1585 */
1586static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
1587{
1588 unsigned char *next;
1589
1590 if (!*len)
1591 return false;
1592
1593 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1594 if (next) {
1595 *len -= next - *buf;
1596 *buf = next;
1597 return true;
1598 }
1599 return false;
1600}
1601
1602/**
1603 * intel_pt_last_psb - find the last PSB packet in a buffer.
1604 * @buf: buffer
1605 * @len: size of buffer
1606 *
1607 * This function finds the last PSB in a buffer.
1608 *
1609 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
1610 */
1611static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
1612{
1613 const char *n = INTEL_PT_PSB_STR;
1614 unsigned char *p;
1615 size_t k;
1616
1617 if (len < INTEL_PT_PSB_LEN)
1618 return NULL;
1619
1620 k = len - INTEL_PT_PSB_LEN + 1;
1621 while (1) {
1622 p = memrchr(buf, n[0], k);
1623 if (!p)
1624 return NULL;
1625 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
1626 return p;
1627 k = p - buf;
1628 if (!k)
1629 return NULL;
1630 }
1631}
1632
1633/**
1634 * intel_pt_next_tsc - find and return next TSC.
1635 * @buf: buffer
1636 * @len: size of buffer
1637 * @tsc: TSC value returned
1638 *
1639 * Find a TSC packet in @buf and return the TSC value. This function assumes
1640 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
1641 * PSBEND packet is found.
1642 *
1643 * Return: %true if TSC is found, false otherwise.
1644 */
1645static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
1646{
1647 struct intel_pt_pkt packet;
1648 int ret;
1649
1650 while (len) {
1651 ret = intel_pt_get_packet(buf, len, &packet);
1652 if (ret <= 0)
1653 return false;
1654 if (packet.type == INTEL_PT_TSC) {
1655 *tsc = packet.payload;
1656 return true;
1657 }
1658 if (packet.type == INTEL_PT_PSBEND)
1659 return false;
1660 buf += ret;
1661 len -= ret;
1662 }
1663 return false;
1664}
1665
1666/**
1667 * intel_pt_tsc_cmp - compare 7-byte TSCs.
1668 * @tsc1: first TSC to compare
1669 * @tsc2: second TSC to compare
1670 *
1671 * This function compares 7-byte TSC values allowing for the possibility that
1672 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
1673 * around so for that purpose this function assumes the absolute difference is
1674 * less than half the maximum difference.
1675 *
1676 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
1677 * after @tsc2.
1678 */
1679static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
1680{
1681 const uint64_t halfway = (1ULL << 55);
1682
1683 if (tsc1 == tsc2)
1684 return 0;
1685
1686 if (tsc1 < tsc2) {
1687 if (tsc2 - tsc1 < halfway)
1688 return -1;
1689 else
1690 return 1;
1691 } else {
1692 if (tsc1 - tsc2 < halfway)
1693 return 1;
1694 else
1695 return -1;
1696 }
1697}
1698
1699/**
1700 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
1701 * using TSC.
1702 * @buf_a: first buffer
1703 * @len_a: size of first buffer
1704 * @buf_b: second buffer
1705 * @len_b: size of second buffer
1706 *
1707 * If the trace contains TSC we can look at the last TSC of @buf_a and the
1708 * first TSC of @buf_b in order to determine if the buffers overlap, and then
1709 * walk forward in @buf_b until a later TSC is found. A precondition is that
1710 * @buf_a and @buf_b are positioned at a PSB.
1711 *
1712 * Return: A pointer into @buf_b from where non-overlapped data starts, or
1713 * @buf_b + @len_b if there is no non-overlapped data.
1714 */
1715static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
1716 size_t len_a,
1717 unsigned char *buf_b,
1718 size_t len_b)
1719{
1720 uint64_t tsc_a, tsc_b;
1721 unsigned char *p;
1722 size_t len;
1723
1724 p = intel_pt_last_psb(buf_a, len_a);
1725 if (!p)
1726 return buf_b; /* No PSB in buf_a => no overlap */
1727
1728 len = len_a - (p - buf_a);
1729 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
1730 /* The last PSB+ in buf_a is incomplete, so go back one more */
1731 len_a -= len;
1732 p = intel_pt_last_psb(buf_a, len_a);
1733 if (!p)
1734 return buf_b; /* No full PSB+ => assume no overlap */
1735 len = len_a - (p - buf_a);
1736 if (!intel_pt_next_tsc(p, len, &tsc_a))
1737 return buf_b; /* No TSC in buf_a => assume no overlap */
1738 }
1739
1740 while (1) {
1741 /* Ignore PSB+ with no TSC */
1742 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
1743 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
1744 return buf_b; /* tsc_a < tsc_b => no overlap */
1745
1746 if (!intel_pt_step_psb(&buf_b, &len_b))
1747 return buf_b + len_b; /* No PSB in buf_b => no data */
1748 }
1749}
1750
1751/**
1752 * intel_pt_find_overlap - determine start of non-overlapped trace data.
1753 * @buf_a: first buffer
1754 * @len_a: size of first buffer
1755 * @buf_b: second buffer
1756 * @len_b: size of second buffer
1757 * @have_tsc: can use TSC packets to detect overlap
1758 *
1759 * When trace samples or snapshots are recorded there is the possibility that
1760 * the data overlaps. Note that, for the purposes of decoding, data is only
1761 * useful if it begins with a PSB packet.
1762 *
1763 * Return: A pointer into @buf_b from where non-overlapped data starts, or
1764 * @buf_b + @len_b if there is no non-overlapped data.
1765 */
1766unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
1767 unsigned char *buf_b, size_t len_b,
1768 bool have_tsc)
1769{
1770 unsigned char *found;
1771
1772 /* Buffer 'b' must start at PSB so throw away everything before that */
1773 if (!intel_pt_next_psb(&buf_b, &len_b))
1774 return buf_b + len_b; /* No PSB */
1775
1776 if (!intel_pt_next_psb(&buf_a, &len_a))
1777 return buf_b; /* No overlap */
1778
1779 if (have_tsc) {
1780 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
1781 if (found)
1782 return found;
1783 }
1784
1785 /*
1786 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
1787 * we can ignore the first part of buffer 'a'.
1788 */
1789 while (len_b < len_a) {
1790 if (!intel_pt_step_psb(&buf_a, &len_a))
1791 return buf_b; /* No overlap */
1792 }
1793
1794 /* Now len_b >= len_a */
1795 if (len_b > len_a) {
1796 /* The leftover buffer 'b' must start at a PSB */
1797 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
1798 if (!intel_pt_step_psb(&buf_a, &len_a))
1799 return buf_b; /* No overlap */
1800 }
1801 }
1802
1803 while (1) {
1804 /* Potential overlap so check the bytes */
1805 found = memmem(buf_a, len_a, buf_b, len_a);
1806 if (found)
1807 return buf_b + len_a;
1808
1809 /* Try again at next PSB in buffer 'a' */
1810 if (!intel_pt_step_psb(&buf_a, &len_a))
1811 return buf_b; /* No overlap */
1812
1813 /* The leftover buffer 'b' must start at a PSB */
1814 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
1815 if (!intel_pt_step_psb(&buf_a, &len_a))
1816 return buf_b; /* No overlap */
1817 }
1818 }
1819}