blob: 4a0e9fb1d17318ddd8cd1843793f17c7ee828e5f [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:
Adrian Hunter3d498072015-07-17 19:33:53 +0300926 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300927 case INTEL_PT_BAD:
928 case INTEL_PT_PSB:
929 intel_pt_log("ERROR: Unexpected packet\n");
930 return -EAGAIN;
931
932 case INTEL_PT_OVF:
933 return intel_pt_overflow(decoder);
934
935 case INTEL_PT_TSC:
936 intel_pt_calc_tsc_timestamp(decoder);
937 break;
938
Adrian Hunter3d498072015-07-17 19:33:53 +0300939 case INTEL_PT_TMA:
940 break;
941
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300942 case INTEL_PT_CBR:
943 decoder->cbr = decoder->packet.payload;
944 break;
945
946 case INTEL_PT_MODE_EXEC:
947 decoder->exec_mode = decoder->packet.payload;
948 break;
949
950 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +0300951 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300952 break;
953
954 case INTEL_PT_FUP:
955 decoder->pge = true;
956 intel_pt_set_last_ip(decoder);
957 break;
958
959 case INTEL_PT_MODE_TSX:
960 intel_pt_update_in_tx(decoder);
961 break;
962
Adrian Hunter3d498072015-07-17 19:33:53 +0300963 case INTEL_PT_MTC:
964 break;
965
966 case INTEL_PT_CYC:
967 case INTEL_PT_VMCS:
968 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300969 case INTEL_PT_PAD:
970 default:
971 break;
972 }
973 }
974}
975
976static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
977{
978 int err;
979
980 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
981 decoder->tx_flags = 0;
982 decoder->state.flags &= ~INTEL_PT_IN_TX;
983 decoder->state.flags |= INTEL_PT_ABORT_TX;
984 } else {
985 decoder->state.flags |= INTEL_PT_ASYNC;
986 }
987
988 while (1) {
989 err = intel_pt_get_next_packet(decoder);
990 if (err)
991 return err;
992
993 switch (decoder->packet.type) {
994 case INTEL_PT_TNT:
995 case INTEL_PT_FUP:
Adrian Hunter3d498072015-07-17 19:33:53 +0300996 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300997 case INTEL_PT_PSB:
998 case INTEL_PT_TSC:
Adrian Hunter3d498072015-07-17 19:33:53 +0300999 case INTEL_PT_TMA:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001000 case INTEL_PT_CBR:
1001 case INTEL_PT_MODE_TSX:
1002 case INTEL_PT_BAD:
1003 case INTEL_PT_PSBEND:
1004 intel_pt_log("ERROR: Missing TIP after FUP\n");
1005 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1006 return -ENOENT;
1007
1008 case INTEL_PT_OVF:
1009 return intel_pt_overflow(decoder);
1010
1011 case INTEL_PT_TIP_PGD:
1012 decoder->state.from_ip = decoder->ip;
1013 decoder->state.to_ip = 0;
1014 if (decoder->packet.count != 0) {
1015 intel_pt_set_ip(decoder);
1016 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1017 decoder->ip);
1018 }
1019 decoder->pge = false;
1020 decoder->continuous_period = false;
1021 return 0;
1022
1023 case INTEL_PT_TIP_PGE:
1024 decoder->pge = true;
1025 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1026 decoder->ip);
1027 decoder->state.from_ip = 0;
1028 if (decoder->packet.count == 0) {
1029 decoder->state.to_ip = 0;
1030 } else {
1031 intel_pt_set_ip(decoder);
1032 decoder->state.to_ip = decoder->ip;
1033 }
1034 return 0;
1035
1036 case INTEL_PT_TIP:
1037 decoder->state.from_ip = decoder->ip;
1038 if (decoder->packet.count == 0) {
1039 decoder->state.to_ip = 0;
1040 } else {
1041 intel_pt_set_ip(decoder);
1042 decoder->state.to_ip = decoder->ip;
1043 }
1044 return 0;
1045
1046 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001047 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1048 break;
1049
1050 case INTEL_PT_MTC:
1051 break;
1052
1053 case INTEL_PT_CYC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001054 break;
1055
1056 case INTEL_PT_MODE_EXEC:
1057 decoder->exec_mode = decoder->packet.payload;
1058 break;
1059
Adrian Hunter3d498072015-07-17 19:33:53 +03001060 case INTEL_PT_VMCS:
1061 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001062 case INTEL_PT_PAD:
1063 break;
1064
1065 default:
1066 return intel_pt_bug(decoder);
1067 }
1068 }
1069}
1070
1071static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1072{
1073 bool no_tip = false;
1074 int err;
1075
1076 while (1) {
1077 err = intel_pt_get_next_packet(decoder);
1078 if (err)
1079 return err;
1080next:
1081 switch (decoder->packet.type) {
1082 case INTEL_PT_TNT:
1083 if (!decoder->packet.count)
1084 break;
1085 decoder->tnt = decoder->packet;
1086 decoder->pkt_state = INTEL_PT_STATE_TNT;
1087 err = intel_pt_walk_tnt(decoder);
1088 if (err == -EAGAIN)
1089 break;
1090 return err;
1091
1092 case INTEL_PT_TIP_PGD:
1093 if (decoder->packet.count != 0)
1094 intel_pt_set_last_ip(decoder);
1095 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1096 return intel_pt_walk_tip(decoder);
1097
1098 case INTEL_PT_TIP_PGE: {
1099 decoder->pge = true;
1100 if (decoder->packet.count == 0) {
1101 intel_pt_log_at("Skipping zero TIP.PGE",
1102 decoder->pos);
1103 break;
1104 }
1105 intel_pt_set_ip(decoder);
1106 decoder->state.from_ip = 0;
1107 decoder->state.to_ip = decoder->ip;
1108 return 0;
1109 }
1110
1111 case INTEL_PT_OVF:
1112 return intel_pt_overflow(decoder);
1113
1114 case INTEL_PT_TIP:
1115 if (decoder->packet.count != 0)
1116 intel_pt_set_last_ip(decoder);
1117 decoder->pkt_state = INTEL_PT_STATE_TIP;
1118 return intel_pt_walk_tip(decoder);
1119
1120 case INTEL_PT_FUP:
1121 if (decoder->packet.count == 0) {
1122 intel_pt_log_at("Skipping zero FUP",
1123 decoder->pos);
1124 no_tip = false;
1125 break;
1126 }
1127 intel_pt_set_last_ip(decoder);
1128 err = intel_pt_walk_fup(decoder);
1129 if (err != -EAGAIN) {
1130 if (err)
1131 return err;
1132 if (no_tip)
1133 decoder->pkt_state =
1134 INTEL_PT_STATE_FUP_NO_TIP;
1135 else
1136 decoder->pkt_state = INTEL_PT_STATE_FUP;
1137 return 0;
1138 }
1139 if (no_tip) {
1140 no_tip = false;
1141 break;
1142 }
1143 return intel_pt_walk_fup_tip(decoder);
1144
Adrian Hunter3d498072015-07-17 19:33:53 +03001145 case INTEL_PT_TRACESTOP:
1146 break;
1147
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001148 case INTEL_PT_PSB:
1149 intel_pt_clear_stack(&decoder->stack);
1150 err = intel_pt_walk_psbend(decoder);
1151 if (err == -EAGAIN)
1152 goto next;
1153 if (err)
1154 return err;
1155 break;
1156
1157 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001158 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1159 break;
1160
1161 case INTEL_PT_MTC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001162 break;
1163
1164 case INTEL_PT_TSC:
1165 intel_pt_calc_tsc_timestamp(decoder);
1166 break;
1167
Adrian Hunter3d498072015-07-17 19:33:53 +03001168 case INTEL_PT_TMA:
1169 break;
1170
1171 case INTEL_PT_CYC:
1172 break;
1173
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001174 case INTEL_PT_CBR:
1175 decoder->cbr = decoder->packet.payload;
1176 break;
1177
1178 case INTEL_PT_MODE_EXEC:
1179 decoder->exec_mode = decoder->packet.payload;
1180 break;
1181
1182 case INTEL_PT_MODE_TSX:
1183 /* MODE_TSX need not be followed by FUP */
1184 if (!decoder->pge) {
1185 intel_pt_update_in_tx(decoder);
1186 break;
1187 }
1188 err = intel_pt_mode_tsx(decoder, &no_tip);
1189 if (err)
1190 return err;
1191 goto next;
1192
1193 case INTEL_PT_BAD: /* Does not happen */
1194 return intel_pt_bug(decoder);
1195
1196 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03001197 case INTEL_PT_VMCS:
1198 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001199 case INTEL_PT_PAD:
1200 break;
1201
1202 default:
1203 return intel_pt_bug(decoder);
1204 }
1205 }
1206}
1207
1208/* Walk PSB+ packets to get in sync. */
1209static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1210{
1211 int err;
1212
1213 while (1) {
1214 err = intel_pt_get_next_packet(decoder);
1215 if (err)
1216 return err;
1217
1218 switch (decoder->packet.type) {
1219 case INTEL_PT_TIP_PGD:
1220 decoder->continuous_period = false;
1221 case INTEL_PT_TIP_PGE:
1222 case INTEL_PT_TIP:
1223 intel_pt_log("ERROR: Unexpected packet\n");
1224 return -ENOENT;
1225
1226 case INTEL_PT_FUP:
1227 decoder->pge = true;
1228 if (decoder->last_ip || decoder->packet.count == 6 ||
1229 decoder->packet.count == 0) {
1230 uint64_t current_ip = decoder->ip;
1231
1232 intel_pt_set_ip(decoder);
1233 if (current_ip)
1234 intel_pt_log_to("Setting IP",
1235 decoder->ip);
1236 }
1237 break;
1238
Adrian Hunter3d498072015-07-17 19:33:53 +03001239 case INTEL_PT_MTC:
1240 break;
1241
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001242 case INTEL_PT_TSC:
1243 intel_pt_calc_tsc_timestamp(decoder);
1244 break;
1245
Adrian Hunter3d498072015-07-17 19:33:53 +03001246 case INTEL_PT_TMA:
1247 break;
1248
1249 case INTEL_PT_CYC:
1250 break;
1251
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001252 case INTEL_PT_CBR:
1253 decoder->cbr = decoder->packet.payload;
1254 break;
1255
1256 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001257 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001258 break;
1259
1260 case INTEL_PT_MODE_EXEC:
1261 decoder->exec_mode = decoder->packet.payload;
1262 break;
1263
1264 case INTEL_PT_MODE_TSX:
1265 intel_pt_update_in_tx(decoder);
1266 break;
1267
Adrian Hunter3d498072015-07-17 19:33:53 +03001268 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001269 case INTEL_PT_TNT:
1270 intel_pt_log("ERROR: Unexpected packet\n");
1271 if (decoder->ip)
1272 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1273 else
1274 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1275 return -ENOENT;
1276
1277 case INTEL_PT_BAD: /* Does not happen */
1278 return intel_pt_bug(decoder);
1279
1280 case INTEL_PT_OVF:
1281 return intel_pt_overflow(decoder);
1282
1283 case INTEL_PT_PSBEND:
1284 return 0;
1285
1286 case INTEL_PT_PSB:
Adrian Hunter3d498072015-07-17 19:33:53 +03001287 case INTEL_PT_VMCS:
1288 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001289 case INTEL_PT_PAD:
1290 default:
1291 break;
1292 }
1293 }
1294}
1295
1296static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1297{
1298 int err;
1299
1300 while (1) {
1301 err = intel_pt_get_next_packet(decoder);
1302 if (err)
1303 return err;
1304
1305 switch (decoder->packet.type) {
1306 case INTEL_PT_TIP_PGD:
1307 decoder->continuous_period = false;
1308 case INTEL_PT_TIP_PGE:
1309 case INTEL_PT_TIP:
1310 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
1311 if (decoder->last_ip || decoder->packet.count == 6 ||
1312 decoder->packet.count == 0)
1313 intel_pt_set_ip(decoder);
1314 if (decoder->ip)
1315 return 0;
1316 break;
1317
1318 case INTEL_PT_FUP:
1319 if (decoder->overflow) {
1320 if (decoder->last_ip ||
1321 decoder->packet.count == 6 ||
1322 decoder->packet.count == 0)
1323 intel_pt_set_ip(decoder);
1324 if (decoder->ip)
1325 return 0;
1326 }
1327 if (decoder->packet.count)
1328 intel_pt_set_last_ip(decoder);
1329 break;
1330
Adrian Hunter3d498072015-07-17 19:33:53 +03001331 case INTEL_PT_MTC:
1332 break;
1333
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001334 case INTEL_PT_TSC:
1335 intel_pt_calc_tsc_timestamp(decoder);
1336 break;
1337
Adrian Hunter3d498072015-07-17 19:33:53 +03001338 case INTEL_PT_TMA:
1339 break;
1340
1341 case INTEL_PT_CYC:
1342 break;
1343
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001344 case INTEL_PT_CBR:
1345 decoder->cbr = decoder->packet.payload;
1346 break;
1347
1348 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001349 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001350 break;
1351
1352 case INTEL_PT_MODE_EXEC:
1353 decoder->exec_mode = decoder->packet.payload;
1354 break;
1355
1356 case INTEL_PT_MODE_TSX:
1357 intel_pt_update_in_tx(decoder);
1358 break;
1359
1360 case INTEL_PT_OVF:
1361 return intel_pt_overflow(decoder);
1362
1363 case INTEL_PT_BAD: /* Does not happen */
1364 return intel_pt_bug(decoder);
1365
Adrian Hunter3d498072015-07-17 19:33:53 +03001366 case INTEL_PT_TRACESTOP:
1367 break;
1368
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001369 case INTEL_PT_PSB:
1370 err = intel_pt_walk_psb(decoder);
1371 if (err)
1372 return err;
1373 if (decoder->ip) {
1374 /* Do not have a sample */
1375 decoder->state.type = 0;
1376 return 0;
1377 }
1378 break;
1379
1380 case INTEL_PT_TNT:
1381 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03001382 case INTEL_PT_VMCS:
1383 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001384 case INTEL_PT_PAD:
1385 default:
1386 break;
1387 }
1388 }
1389}
1390
1391static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1392{
1393 int err;
1394
1395 intel_pt_log("Scanning for full IP\n");
1396 err = intel_pt_walk_to_ip(decoder);
1397 if (err)
1398 return err;
1399
1400 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1401 decoder->overflow = false;
1402
1403 decoder->state.from_ip = 0;
1404 decoder->state.to_ip = decoder->ip;
1405 intel_pt_log_to("Setting IP", decoder->ip);
1406
1407 return 0;
1408}
1409
1410static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1411{
1412 const unsigned char *end = decoder->buf + decoder->len;
1413 size_t i;
1414
1415 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1416 if (i > decoder->len)
1417 continue;
1418 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1419 return i;
1420 }
1421 return 0;
1422}
1423
1424static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1425{
1426 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1427 const char *psb = INTEL_PT_PSB_STR;
1428
1429 if (rest_psb > decoder->len ||
1430 memcmp(decoder->buf, psb + part_psb, rest_psb))
1431 return 0;
1432
1433 return rest_psb;
1434}
1435
1436static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
1437 int part_psb)
1438{
1439 int rest_psb, ret;
1440
1441 decoder->pos += decoder->len;
1442 decoder->len = 0;
1443
1444 ret = intel_pt_get_next_data(decoder);
1445 if (ret)
1446 return ret;
1447
1448 rest_psb = intel_pt_rest_psb(decoder, part_psb);
1449 if (!rest_psb)
1450 return 0;
1451
1452 decoder->pos -= part_psb;
1453 decoder->next_buf = decoder->buf + rest_psb;
1454 decoder->next_len = decoder->len - rest_psb;
1455 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1456 decoder->buf = decoder->temp_buf;
1457 decoder->len = INTEL_PT_PSB_LEN;
1458
1459 return 0;
1460}
1461
1462static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
1463{
1464 unsigned char *next;
1465 int ret;
1466
1467 intel_pt_log("Scanning for PSB\n");
1468 while (1) {
1469 if (!decoder->len) {
1470 ret = intel_pt_get_next_data(decoder);
1471 if (ret)
1472 return ret;
1473 }
1474
1475 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
1476 INTEL_PT_PSB_LEN);
1477 if (!next) {
1478 int part_psb;
1479
1480 part_psb = intel_pt_part_psb(decoder);
1481 if (part_psb) {
1482 ret = intel_pt_get_split_psb(decoder, part_psb);
1483 if (ret)
1484 return ret;
1485 } else {
1486 decoder->pos += decoder->len;
1487 decoder->len = 0;
1488 }
1489 continue;
1490 }
1491
1492 decoder->pkt_step = next - decoder->buf;
1493 return intel_pt_get_next_packet(decoder);
1494 }
1495}
1496
1497static int intel_pt_sync(struct intel_pt_decoder *decoder)
1498{
1499 int err;
1500
1501 decoder->pge = false;
1502 decoder->continuous_period = false;
1503 decoder->last_ip = 0;
1504 decoder->ip = 0;
1505 intel_pt_clear_stack(&decoder->stack);
1506
1507 err = intel_pt_scan_for_psb(decoder);
1508 if (err)
1509 return err;
1510
1511 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
1512
1513 err = intel_pt_walk_psb(decoder);
1514 if (err)
1515 return err;
1516
1517 if (decoder->ip) {
1518 decoder->state.type = 0; /* Do not have a sample */
1519 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1520 } else {
1521 return intel_pt_sync_ip(decoder);
1522 }
1523
1524 return 0;
1525}
1526
1527static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
1528{
1529 uint64_t est = decoder->timestamp_insn_cnt << 1;
1530
1531 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
1532 goto out;
1533
1534 est *= decoder->max_non_turbo_ratio;
1535 est /= decoder->cbr;
1536out:
1537 return decoder->timestamp + est;
1538}
1539
1540const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
1541{
1542 int err;
1543
1544 do {
1545 decoder->state.type = INTEL_PT_BRANCH;
1546 decoder->state.flags = 0;
1547
1548 switch (decoder->pkt_state) {
1549 case INTEL_PT_STATE_NO_PSB:
1550 err = intel_pt_sync(decoder);
1551 break;
1552 case INTEL_PT_STATE_NO_IP:
1553 decoder->last_ip = 0;
1554 /* Fall through */
1555 case INTEL_PT_STATE_ERR_RESYNC:
1556 err = intel_pt_sync_ip(decoder);
1557 break;
1558 case INTEL_PT_STATE_IN_SYNC:
1559 err = intel_pt_walk_trace(decoder);
1560 break;
1561 case INTEL_PT_STATE_TNT:
1562 err = intel_pt_walk_tnt(decoder);
1563 if (err == -EAGAIN)
1564 err = intel_pt_walk_trace(decoder);
1565 break;
1566 case INTEL_PT_STATE_TIP:
1567 case INTEL_PT_STATE_TIP_PGD:
1568 err = intel_pt_walk_tip(decoder);
1569 break;
1570 case INTEL_PT_STATE_FUP:
1571 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1572 err = intel_pt_walk_fup(decoder);
1573 if (err == -EAGAIN)
1574 err = intel_pt_walk_fup_tip(decoder);
1575 else if (!err)
1576 decoder->pkt_state = INTEL_PT_STATE_FUP;
1577 break;
1578 case INTEL_PT_STATE_FUP_NO_TIP:
1579 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1580 err = intel_pt_walk_fup(decoder);
1581 if (err == -EAGAIN)
1582 err = intel_pt_walk_trace(decoder);
1583 break;
1584 default:
1585 err = intel_pt_bug(decoder);
1586 break;
1587 }
1588 } while (err == -ENOLINK);
1589
1590 decoder->state.err = err ? intel_pt_ext_err(err) : 0;
1591 decoder->state.timestamp = decoder->timestamp;
1592 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
1593 decoder->state.cr3 = decoder->cr3;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001594 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001595
1596 if (err)
1597 decoder->state.from_ip = decoder->ip;
1598
1599 return &decoder->state;
1600}
1601
1602static bool intel_pt_at_psb(unsigned char *buf, size_t len)
1603{
1604 if (len < INTEL_PT_PSB_LEN)
1605 return false;
1606 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
1607 INTEL_PT_PSB_LEN);
1608}
1609
1610/**
1611 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
1612 * @buf: pointer to buffer pointer
1613 * @len: size of buffer
1614 *
1615 * Updates the buffer pointer to point to the start of the next PSB packet if
1616 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
1617 * @len is adjusted accordingly.
1618 *
1619 * Return: %true if a PSB packet is found, %false otherwise.
1620 */
1621static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
1622{
1623 unsigned char *next;
1624
1625 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1626 if (next) {
1627 *len -= next - *buf;
1628 *buf = next;
1629 return true;
1630 }
1631 return false;
1632}
1633
1634/**
1635 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
1636 * packet.
1637 * @buf: pointer to buffer pointer
1638 * @len: size of buffer
1639 *
1640 * Updates the buffer pointer to point to the start of the following PSB packet
1641 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
1642 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
1643 *
1644 * Return: %true if a PSB packet is found, %false otherwise.
1645 */
1646static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
1647{
1648 unsigned char *next;
1649
1650 if (!*len)
1651 return false;
1652
1653 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
1654 if (next) {
1655 *len -= next - *buf;
1656 *buf = next;
1657 return true;
1658 }
1659 return false;
1660}
1661
1662/**
1663 * intel_pt_last_psb - find the last PSB packet in a buffer.
1664 * @buf: buffer
1665 * @len: size of buffer
1666 *
1667 * This function finds the last PSB in a buffer.
1668 *
1669 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
1670 */
1671static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
1672{
1673 const char *n = INTEL_PT_PSB_STR;
1674 unsigned char *p;
1675 size_t k;
1676
1677 if (len < INTEL_PT_PSB_LEN)
1678 return NULL;
1679
1680 k = len - INTEL_PT_PSB_LEN + 1;
1681 while (1) {
1682 p = memrchr(buf, n[0], k);
1683 if (!p)
1684 return NULL;
1685 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
1686 return p;
1687 k = p - buf;
1688 if (!k)
1689 return NULL;
1690 }
1691}
1692
1693/**
1694 * intel_pt_next_tsc - find and return next TSC.
1695 * @buf: buffer
1696 * @len: size of buffer
1697 * @tsc: TSC value returned
1698 *
1699 * Find a TSC packet in @buf and return the TSC value. This function assumes
1700 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
1701 * PSBEND packet is found.
1702 *
1703 * Return: %true if TSC is found, false otherwise.
1704 */
1705static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
1706{
1707 struct intel_pt_pkt packet;
1708 int ret;
1709
1710 while (len) {
1711 ret = intel_pt_get_packet(buf, len, &packet);
1712 if (ret <= 0)
1713 return false;
1714 if (packet.type == INTEL_PT_TSC) {
1715 *tsc = packet.payload;
1716 return true;
1717 }
1718 if (packet.type == INTEL_PT_PSBEND)
1719 return false;
1720 buf += ret;
1721 len -= ret;
1722 }
1723 return false;
1724}
1725
1726/**
1727 * intel_pt_tsc_cmp - compare 7-byte TSCs.
1728 * @tsc1: first TSC to compare
1729 * @tsc2: second TSC to compare
1730 *
1731 * This function compares 7-byte TSC values allowing for the possibility that
1732 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
1733 * around so for that purpose this function assumes the absolute difference is
1734 * less than half the maximum difference.
1735 *
1736 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
1737 * after @tsc2.
1738 */
1739static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
1740{
1741 const uint64_t halfway = (1ULL << 55);
1742
1743 if (tsc1 == tsc2)
1744 return 0;
1745
1746 if (tsc1 < tsc2) {
1747 if (tsc2 - tsc1 < halfway)
1748 return -1;
1749 else
1750 return 1;
1751 } else {
1752 if (tsc1 - tsc2 < halfway)
1753 return 1;
1754 else
1755 return -1;
1756 }
1757}
1758
1759/**
1760 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
1761 * using TSC.
1762 * @buf_a: first buffer
1763 * @len_a: size of first buffer
1764 * @buf_b: second buffer
1765 * @len_b: size of second buffer
1766 *
1767 * If the trace contains TSC we can look at the last TSC of @buf_a and the
1768 * first TSC of @buf_b in order to determine if the buffers overlap, and then
1769 * walk forward in @buf_b until a later TSC is found. A precondition is that
1770 * @buf_a and @buf_b are positioned at a PSB.
1771 *
1772 * Return: A pointer into @buf_b from where non-overlapped data starts, or
1773 * @buf_b + @len_b if there is no non-overlapped data.
1774 */
1775static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
1776 size_t len_a,
1777 unsigned char *buf_b,
1778 size_t len_b)
1779{
1780 uint64_t tsc_a, tsc_b;
1781 unsigned char *p;
1782 size_t len;
1783
1784 p = intel_pt_last_psb(buf_a, len_a);
1785 if (!p)
1786 return buf_b; /* No PSB in buf_a => no overlap */
1787
1788 len = len_a - (p - buf_a);
1789 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
1790 /* The last PSB+ in buf_a is incomplete, so go back one more */
1791 len_a -= len;
1792 p = intel_pt_last_psb(buf_a, len_a);
1793 if (!p)
1794 return buf_b; /* No full PSB+ => assume no overlap */
1795 len = len_a - (p - buf_a);
1796 if (!intel_pt_next_tsc(p, len, &tsc_a))
1797 return buf_b; /* No TSC in buf_a => assume no overlap */
1798 }
1799
1800 while (1) {
1801 /* Ignore PSB+ with no TSC */
1802 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
1803 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
1804 return buf_b; /* tsc_a < tsc_b => no overlap */
1805
1806 if (!intel_pt_step_psb(&buf_b, &len_b))
1807 return buf_b + len_b; /* No PSB in buf_b => no data */
1808 }
1809}
1810
1811/**
1812 * intel_pt_find_overlap - determine start of non-overlapped trace data.
1813 * @buf_a: first buffer
1814 * @len_a: size of first buffer
1815 * @buf_b: second buffer
1816 * @len_b: size of second buffer
1817 * @have_tsc: can use TSC packets to detect overlap
1818 *
1819 * When trace samples or snapshots are recorded there is the possibility that
1820 * the data overlaps. Note that, for the purposes of decoding, data is only
1821 * useful if it begins with a PSB packet.
1822 *
1823 * Return: A pointer into @buf_b from where non-overlapped data starts, or
1824 * @buf_b + @len_b if there is no non-overlapped data.
1825 */
1826unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
1827 unsigned char *buf_b, size_t len_b,
1828 bool have_tsc)
1829{
1830 unsigned char *found;
1831
1832 /* Buffer 'b' must start at PSB so throw away everything before that */
1833 if (!intel_pt_next_psb(&buf_b, &len_b))
1834 return buf_b + len_b; /* No PSB */
1835
1836 if (!intel_pt_next_psb(&buf_a, &len_a))
1837 return buf_b; /* No overlap */
1838
1839 if (have_tsc) {
1840 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
1841 if (found)
1842 return found;
1843 }
1844
1845 /*
1846 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
1847 * we can ignore the first part of buffer 'a'.
1848 */
1849 while (len_b < len_a) {
1850 if (!intel_pt_step_psb(&buf_a, &len_a))
1851 return buf_b; /* No overlap */
1852 }
1853
1854 /* Now len_b >= len_a */
1855 if (len_b > len_a) {
1856 /* The leftover buffer 'b' must start at a PSB */
1857 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
1858 if (!intel_pt_step_psb(&buf_a, &len_a))
1859 return buf_b; /* No overlap */
1860 }
1861 }
1862
1863 while (1) {
1864 /* Potential overlap so check the bytes */
1865 found = memmem(buf_a, len_a, buf_b, len_a);
1866 if (found)
1867 return buf_b + len_a;
1868
1869 /* Try again at next PSB in buffer 'a' */
1870 if (!intel_pt_step_psb(&buf_a, &len_a))
1871 return buf_b; /* No overlap */
1872
1873 /* The leftover buffer 'b' must start at a PSB */
1874 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
1875 if (!intel_pt_step_psb(&buf_a, &len_a))
1876 return buf_b; /* No overlap */
1877 }
1878 }
1879}