blob: 923c60efda264dd804c87f14b45e46a651782fff [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>
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -030025#include <linux/compiler.h>
Adrian Hunterf4aa0812015-07-17 19:33:40 +030026
27#include "../cache.h"
28#include "../util.h"
29
30#include "intel-pt-insn-decoder.h"
31#include "intel-pt-pkt-decoder.h"
32#include "intel-pt-decoder.h"
33#include "intel-pt-log.h"
34
35#define INTEL_PT_BLK_SIZE 1024
36
37#define BIT63 (((uint64_t)1 << 63))
38
39#define INTEL_PT_RETURN 1
40
41/* Maximum number of loops with no packets consumed i.e. stuck in a loop */
42#define INTEL_PT_MAX_LOOPS 10000
43
44struct intel_pt_blk {
45 struct intel_pt_blk *prev;
46 uint64_t ip[INTEL_PT_BLK_SIZE];
47};
48
49struct intel_pt_stack {
50 struct intel_pt_blk *blk;
51 struct intel_pt_blk *spare;
52 int pos;
53};
54
55enum intel_pt_pkt_state {
56 INTEL_PT_STATE_NO_PSB,
57 INTEL_PT_STATE_NO_IP,
58 INTEL_PT_STATE_ERR_RESYNC,
59 INTEL_PT_STATE_IN_SYNC,
60 INTEL_PT_STATE_TNT,
61 INTEL_PT_STATE_TIP,
62 INTEL_PT_STATE_TIP_PGD,
63 INTEL_PT_STATE_FUP,
64 INTEL_PT_STATE_FUP_NO_TIP,
65};
66
Adrian Hunter3f04d982017-05-26 11:17:03 +030067static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
68{
69 switch (pkt_state) {
70 case INTEL_PT_STATE_NO_PSB:
71 case INTEL_PT_STATE_NO_IP:
72 case INTEL_PT_STATE_ERR_RESYNC:
73 case INTEL_PT_STATE_IN_SYNC:
74 case INTEL_PT_STATE_TNT:
75 return true;
76 case INTEL_PT_STATE_TIP:
77 case INTEL_PT_STATE_TIP_PGD:
78 case INTEL_PT_STATE_FUP:
79 case INTEL_PT_STATE_FUP_NO_TIP:
80 return false;
81 default:
82 return true;
83 };
84}
85
Adrian Hunterf4aa0812015-07-17 19:33:40 +030086#ifdef INTEL_PT_STRICT
87#define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
88#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
89#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
90#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
91#else
92#define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
93#define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
94#define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
95#define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
96#endif
97
98struct intel_pt_decoder {
99 int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
100 int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
101 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
102 uint64_t max_insn_cnt, void *data);
Adrian Hunter9f1d1222016-09-23 17:38:47 +0300103 bool (*pgd_ip)(uint64_t ip, void *data);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300104 void *data;
105 struct intel_pt_state state;
106 const unsigned char *buf;
107 size_t len;
108 bool return_compression;
Adrian Hunter79b58422015-07-17 19:33:55 +0300109 bool mtc_insn;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300110 bool pge;
Adrian Hunter79b58422015-07-17 19:33:55 +0300111 bool have_tma;
Adrian Huntercc336182015-07-17 19:33:57 +0300112 bool have_cyc;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300113 bool fixup_last_mtc;
Adrian Hunteree14ac02017-05-26 11:17:06 +0300114 bool have_last_ip;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300115 uint64_t pos;
116 uint64_t last_ip;
117 uint64_t ip;
118 uint64_t cr3;
119 uint64_t timestamp;
120 uint64_t tsc_timestamp;
121 uint64_t ref_timestamp;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300122 uint64_t sample_timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300123 uint64_t ret_addr;
Adrian Hunter79b58422015-07-17 19:33:55 +0300124 uint64_t ctc_timestamp;
125 uint64_t ctc_delta;
Adrian Huntercc336182015-07-17 19:33:57 +0300126 uint64_t cycle_cnt;
127 uint64_t cyc_ref_timestamp;
Adrian Hunter79b58422015-07-17 19:33:55 +0300128 uint32_t last_mtc;
129 uint32_t tsc_ctc_ratio_n;
130 uint32_t tsc_ctc_ratio_d;
131 uint32_t tsc_ctc_mult;
132 uint32_t tsc_slip;
133 uint32_t ctc_rem_mask;
134 int mtc_shift;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300135 struct intel_pt_stack stack;
136 enum intel_pt_pkt_state pkt_state;
137 struct intel_pt_pkt packet;
138 struct intel_pt_pkt tnt;
139 int pkt_step;
140 int pkt_len;
Adrian Huntercc336182015-07-17 19:33:57 +0300141 int last_packet_type;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300142 unsigned int cbr;
143 unsigned int max_non_turbo_ratio;
Adrian Huntercc336182015-07-17 19:33:57 +0300144 double max_non_turbo_ratio_fp;
145 double cbr_cyc_to_tsc;
146 double calc_cyc_to_tsc;
147 bool have_calc_cyc_to_tsc;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300148 int exec_mode;
149 unsigned int insn_bytes;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300150 uint64_t period;
151 enum intel_pt_period_type period_type;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300152 uint64_t tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300153 uint64_t period_insn_cnt;
154 uint64_t period_mask;
155 uint64_t period_ticks;
156 uint64_t last_masked_timestamp;
157 bool continuous_period;
158 bool overflow;
159 bool set_fup_tx_flags;
160 unsigned int fup_tx_flags;
161 unsigned int tx_flags;
162 uint64_t timestamp_insn_cnt;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300163 uint64_t sample_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300164 uint64_t stuck_ip;
165 int no_progress;
166 int stuck_ip_prd;
167 int stuck_ip_cnt;
168 const unsigned char *next_buf;
169 size_t next_len;
170 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
171};
172
173static uint64_t intel_pt_lower_power_of_2(uint64_t x)
174{
175 int i;
176
177 for (i = 0; x != 1; i++)
178 x >>= 1;
179
180 return x << i;
181}
182
183static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
184{
185 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
186 uint64_t period;
187
188 period = intel_pt_lower_power_of_2(decoder->period);
189 decoder->period_mask = ~(period - 1);
190 decoder->period_ticks = period;
191 }
192}
193
Adrian Hunter79b58422015-07-17 19:33:55 +0300194static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
195{
196 if (!d)
197 return 0;
198 return (t / d) * n + ((t % d) * n) / d;
199}
200
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300201struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
202{
203 struct intel_pt_decoder *decoder;
204
205 if (!params->get_trace || !params->walk_insn)
206 return NULL;
207
208 decoder = zalloc(sizeof(struct intel_pt_decoder));
209 if (!decoder)
210 return NULL;
211
212 decoder->get_trace = params->get_trace;
213 decoder->walk_insn = params->walk_insn;
Adrian Hunter9f1d1222016-09-23 17:38:47 +0300214 decoder->pgd_ip = params->pgd_ip;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300215 decoder->data = params->data;
216 decoder->return_compression = params->return_compression;
217
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300218 decoder->period = params->period;
219 decoder->period_type = params->period_type;
220
Adrian Huntercc336182015-07-17 19:33:57 +0300221 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
222 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300223
224 intel_pt_setup_period(decoder);
225
Adrian Hunter79b58422015-07-17 19:33:55 +0300226 decoder->mtc_shift = params->mtc_period;
227 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
228
229 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
230 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
231
232 if (!decoder->tsc_ctc_ratio_n)
233 decoder->tsc_ctc_ratio_d = 0;
234
235 if (decoder->tsc_ctc_ratio_d) {
236 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
237 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
238 decoder->tsc_ctc_ratio_d;
239
240 /*
241 * Allow for timestamps appearing to backwards because a TSC
242 * packet has slipped past a MTC packet, so allow 2 MTC ticks
243 * or ...
244 */
245 decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
246 decoder->tsc_ctc_ratio_n,
247 decoder->tsc_ctc_ratio_d);
248 }
249 /* ... or 0x100 paranoia */
250 if (decoder->tsc_slip < 0x100)
251 decoder->tsc_slip = 0x100;
252
253 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
254 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
255 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
256 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
257 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
258
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300259 return decoder;
260}
261
262static void intel_pt_pop_blk(struct intel_pt_stack *stack)
263{
264 struct intel_pt_blk *blk = stack->blk;
265
266 stack->blk = blk->prev;
267 if (!stack->spare)
268 stack->spare = blk;
269 else
270 free(blk);
271}
272
273static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
274{
275 if (!stack->pos) {
276 if (!stack->blk)
277 return 0;
278 intel_pt_pop_blk(stack);
279 if (!stack->blk)
280 return 0;
281 stack->pos = INTEL_PT_BLK_SIZE;
282 }
283 return stack->blk->ip[--stack->pos];
284}
285
286static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
287{
288 struct intel_pt_blk *blk;
289
290 if (stack->spare) {
291 blk = stack->spare;
292 stack->spare = NULL;
293 } else {
294 blk = malloc(sizeof(struct intel_pt_blk));
295 if (!blk)
296 return -ENOMEM;
297 }
298
299 blk->prev = stack->blk;
300 stack->blk = blk;
301 stack->pos = 0;
302 return 0;
303}
304
305static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
306{
307 int err;
308
309 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
310 err = intel_pt_alloc_blk(stack);
311 if (err)
312 return err;
313 }
314
315 stack->blk->ip[stack->pos++] = ip;
316 return 0;
317}
318
319static void intel_pt_clear_stack(struct intel_pt_stack *stack)
320{
321 while (stack->blk)
322 intel_pt_pop_blk(stack);
323 stack->pos = 0;
324}
325
326static void intel_pt_free_stack(struct intel_pt_stack *stack)
327{
328 intel_pt_clear_stack(stack);
329 zfree(&stack->blk);
330 zfree(&stack->spare);
331}
332
333void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
334{
335 intel_pt_free_stack(&decoder->stack);
336 free(decoder);
337}
338
339static int intel_pt_ext_err(int code)
340{
341 switch (code) {
342 case -ENOMEM:
343 return INTEL_PT_ERR_NOMEM;
344 case -ENOSYS:
345 return INTEL_PT_ERR_INTERN;
346 case -EBADMSG:
347 return INTEL_PT_ERR_BADPKT;
348 case -ENODATA:
349 return INTEL_PT_ERR_NODATA;
350 case -EILSEQ:
351 return INTEL_PT_ERR_NOINSN;
352 case -ENOENT:
353 return INTEL_PT_ERR_MISMAT;
354 case -EOVERFLOW:
355 return INTEL_PT_ERR_OVR;
356 case -ENOSPC:
357 return INTEL_PT_ERR_LOST;
358 case -ELOOP:
359 return INTEL_PT_ERR_NELOOP;
360 default:
361 return INTEL_PT_ERR_UNK;
362 }
363}
364
365static const char *intel_pt_err_msgs[] = {
366 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
367 [INTEL_PT_ERR_INTERN] = "Internal error",
368 [INTEL_PT_ERR_BADPKT] = "Bad packet",
369 [INTEL_PT_ERR_NODATA] = "No more data",
370 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
371 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
372 [INTEL_PT_ERR_OVR] = "Overflow packet",
373 [INTEL_PT_ERR_LOST] = "Lost trace data",
374 [INTEL_PT_ERR_UNK] = "Unknown error!",
375 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
376};
377
378int intel_pt__strerror(int code, char *buf, size_t buflen)
379{
Colin Ian Kingc0664892016-04-24 19:56:43 +0100380 if (code < 1 || code >= INTEL_PT_ERR_MAX)
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300381 code = INTEL_PT_ERR_UNK;
382 strlcpy(buf, intel_pt_err_msgs[code], buflen);
383 return 0;
384}
385
Adrian Huntere1717e02016-07-20 12:00:06 +0300386static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300387 uint64_t last_ip)
388{
389 uint64_t ip;
390
391 switch (packet->count) {
Adrian Huntere1717e02016-07-20 12:00:06 +0300392 case 1:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300393 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
394 packet->payload;
395 break;
Adrian Huntere1717e02016-07-20 12:00:06 +0300396 case 2:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300397 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
398 packet->payload;
399 break;
Adrian Huntere1717e02016-07-20 12:00:06 +0300400 case 3:
401 ip = packet->payload;
402 /* Sign-extend 6-byte ip */
403 if (ip & (uint64_t)0x800000000000ULL)
404 ip |= (uint64_t)0xffff000000000000ULL;
405 break;
406 case 4:
407 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
408 packet->payload;
409 break;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300410 case 6:
411 ip = packet->payload;
412 break;
413 default:
414 return 0;
415 }
416
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300417 return ip;
418}
419
420static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
421{
Adrian Huntere1717e02016-07-20 12:00:06 +0300422 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
Adrian Hunteree14ac02017-05-26 11:17:06 +0300423 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300424}
425
426static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
427{
428 intel_pt_set_last_ip(decoder);
429 decoder->ip = decoder->last_ip;
430}
431
432static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
433{
434 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
435 decoder->buf);
436}
437
438static int intel_pt_bug(struct intel_pt_decoder *decoder)
439{
440 intel_pt_log("ERROR: Internal error\n");
441 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
442 return -ENOSYS;
443}
444
445static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
446{
447 decoder->tx_flags = 0;
448}
449
450static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
451{
452 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
453}
454
455static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
456{
457 intel_pt_clear_tx_flags(decoder);
Adrian Hunter79b58422015-07-17 19:33:55 +0300458 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300459 decoder->pkt_len = 1;
460 decoder->pkt_step = 1;
461 intel_pt_decoder_log_packet(decoder);
462 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
463 intel_pt_log("ERROR: Bad packet\n");
464 decoder->pkt_state = INTEL_PT_STATE_ERR1;
465 }
466 return -EBADMSG;
467}
468
469static int intel_pt_get_data(struct intel_pt_decoder *decoder)
470{
471 struct intel_pt_buffer buffer = { .buf = 0, };
472 int ret;
473
474 decoder->pkt_step = 0;
475
476 intel_pt_log("Getting more data\n");
477 ret = decoder->get_trace(&buffer, decoder->data);
478 if (ret)
479 return ret;
480 decoder->buf = buffer.buf;
481 decoder->len = buffer.len;
482 if (!decoder->len) {
483 intel_pt_log("No more data\n");
484 return -ENODATA;
485 }
486 if (!buffer.consecutive) {
487 decoder->ip = 0;
488 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
489 decoder->ref_timestamp = buffer.ref_timestamp;
490 decoder->timestamp = 0;
Adrian Hunter79b58422015-07-17 19:33:55 +0300491 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300492 decoder->state.trace_nr = buffer.trace_nr;
493 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
494 decoder->ref_timestamp);
495 return -ENOLINK;
496 }
497
498 return 0;
499}
500
501static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
502{
503 if (!decoder->next_buf)
504 return intel_pt_get_data(decoder);
505
506 decoder->buf = decoder->next_buf;
507 decoder->len = decoder->next_len;
508 decoder->next_buf = 0;
509 decoder->next_len = 0;
510 return 0;
511}
512
513static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
514{
515 unsigned char *buf = decoder->temp_buf;
516 size_t old_len, len, n;
517 int ret;
518
519 old_len = decoder->len;
520 len = decoder->len;
521 memcpy(buf, decoder->buf, len);
522
523 ret = intel_pt_get_data(decoder);
524 if (ret) {
525 decoder->pos += old_len;
526 return ret < 0 ? ret : -EINVAL;
527 }
528
529 n = INTEL_PT_PKT_MAX_SZ - len;
530 if (n > decoder->len)
531 n = decoder->len;
532 memcpy(buf + len, decoder->buf, n);
533 len += n;
534
535 ret = intel_pt_get_packet(buf, len, &decoder->packet);
536 if (ret < (int)old_len) {
537 decoder->next_buf = decoder->buf;
538 decoder->next_len = decoder->len;
539 decoder->buf = buf;
540 decoder->len = old_len;
541 return intel_pt_bad_packet(decoder);
542 }
543
544 decoder->next_buf = decoder->buf + (ret - old_len);
545 decoder->next_len = decoder->len - (ret - old_len);
546
547 decoder->buf = buf;
548 decoder->len = ret;
549
550 return ret;
551}
552
Adrian Huntercc336182015-07-17 19:33:57 +0300553struct intel_pt_pkt_info {
554 struct intel_pt_decoder *decoder;
555 struct intel_pt_pkt packet;
556 uint64_t pos;
557 int pkt_len;
558 int last_packet_type;
559 void *data;
560};
561
562typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
563
564/* Lookahead packets in current buffer */
565static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
566 intel_pt_pkt_cb_t cb, void *data)
567{
568 struct intel_pt_pkt_info pkt_info;
569 const unsigned char *buf = decoder->buf;
570 size_t len = decoder->len;
571 int ret;
572
573 pkt_info.decoder = decoder;
574 pkt_info.pos = decoder->pos;
575 pkt_info.pkt_len = decoder->pkt_step;
576 pkt_info.last_packet_type = decoder->last_packet_type;
577 pkt_info.data = data;
578
579 while (1) {
580 do {
581 pkt_info.pos += pkt_info.pkt_len;
582 buf += pkt_info.pkt_len;
583 len -= pkt_info.pkt_len;
584
585 if (!len)
586 return INTEL_PT_NEED_MORE_BYTES;
587
588 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
589 if (!ret)
590 return INTEL_PT_NEED_MORE_BYTES;
591 if (ret < 0)
592 return ret;
593
594 pkt_info.pkt_len = ret;
595 } while (pkt_info.packet.type == INTEL_PT_PAD);
596
597 ret = cb(&pkt_info);
598 if (ret)
599 return 0;
600
601 pkt_info.last_packet_type = pkt_info.packet.type;
602 }
603}
604
605struct intel_pt_calc_cyc_to_tsc_info {
606 uint64_t cycle_cnt;
607 unsigned int cbr;
608 uint32_t last_mtc;
609 uint64_t ctc_timestamp;
610 uint64_t ctc_delta;
611 uint64_t tsc_timestamp;
612 uint64_t timestamp;
613 bool have_tma;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300614 bool fixup_last_mtc;
Adrian Huntercc336182015-07-17 19:33:57 +0300615 bool from_mtc;
616 double cbr_cyc_to_tsc;
617};
618
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300619/*
620 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
621 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
622 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
623 * packet by copying the missing bits from the current MTC assuming the least
624 * difference between the two, and that the current MTC comes after last_mtc.
625 */
626static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
627 uint32_t *last_mtc)
628{
629 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
630 uint32_t mask = ~(first_missing_bit - 1);
631
632 *last_mtc |= mtc & mask;
633 if (*last_mtc >= mtc) {
634 *last_mtc -= first_missing_bit;
635 *last_mtc &= 0xff;
636 }
637}
638
Adrian Huntercc336182015-07-17 19:33:57 +0300639static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
640{
641 struct intel_pt_decoder *decoder = pkt_info->decoder;
642 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
643 uint64_t timestamp;
644 double cyc_to_tsc;
645 unsigned int cbr;
646 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
647
648 switch (pkt_info->packet.type) {
649 case INTEL_PT_TNT:
650 case INTEL_PT_TIP_PGE:
651 case INTEL_PT_TIP:
652 case INTEL_PT_FUP:
653 case INTEL_PT_PSB:
654 case INTEL_PT_PIP:
655 case INTEL_PT_MODE_EXEC:
656 case INTEL_PT_MODE_TSX:
657 case INTEL_PT_PSBEND:
658 case INTEL_PT_PAD:
659 case INTEL_PT_VMCS:
660 case INTEL_PT_MNT:
661 return 0;
662
663 case INTEL_PT_MTC:
664 if (!data->have_tma)
665 return 0;
666
667 mtc = pkt_info->packet.payload;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300668 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
669 data->fixup_last_mtc = false;
670 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
671 &data->last_mtc);
672 }
Adrian Huntercc336182015-07-17 19:33:57 +0300673 if (mtc > data->last_mtc)
674 mtc_delta = mtc - data->last_mtc;
675 else
676 mtc_delta = mtc + 256 - data->last_mtc;
677 data->ctc_delta += mtc_delta << decoder->mtc_shift;
678 data->last_mtc = mtc;
679
680 if (decoder->tsc_ctc_mult) {
681 timestamp = data->ctc_timestamp +
682 data->ctc_delta * decoder->tsc_ctc_mult;
683 } else {
684 timestamp = data->ctc_timestamp +
685 multdiv(data->ctc_delta,
686 decoder->tsc_ctc_ratio_n,
687 decoder->tsc_ctc_ratio_d);
688 }
689
690 if (timestamp < data->timestamp)
691 return 1;
692
693 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
694 data->timestamp = timestamp;
695 return 0;
696 }
697
698 break;
699
700 case INTEL_PT_TSC:
701 timestamp = pkt_info->packet.payload |
702 (data->timestamp & (0xffULL << 56));
703 if (data->from_mtc && timestamp < data->timestamp &&
704 data->timestamp - timestamp < decoder->tsc_slip)
705 return 1;
Adrian Hunter9992c2d2015-09-25 16:15:34 +0300706 if (timestamp < data->timestamp)
Adrian Huntercc336182015-07-17 19:33:57 +0300707 timestamp += (1ULL << 56);
708 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
709 if (data->from_mtc)
710 return 1;
711 data->tsc_timestamp = timestamp;
712 data->timestamp = timestamp;
713 return 0;
714 }
715 break;
716
717 case INTEL_PT_TMA:
718 if (data->from_mtc)
719 return 1;
720
721 if (!decoder->tsc_ctc_ratio_d)
722 return 0;
723
724 ctc = pkt_info->packet.payload;
725 fc = pkt_info->packet.count;
726 ctc_rem = ctc & decoder->ctc_rem_mask;
727
728 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
729
730 data->ctc_timestamp = data->tsc_timestamp - fc;
731 if (decoder->tsc_ctc_mult) {
732 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
733 } else {
734 data->ctc_timestamp -=
735 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
736 decoder->tsc_ctc_ratio_d);
737 }
738
739 data->ctc_delta = 0;
740 data->have_tma = true;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300741 data->fixup_last_mtc = true;
Adrian Huntercc336182015-07-17 19:33:57 +0300742
743 return 0;
744
745 case INTEL_PT_CYC:
746 data->cycle_cnt += pkt_info->packet.payload;
747 return 0;
748
749 case INTEL_PT_CBR:
750 cbr = pkt_info->packet.payload;
751 if (data->cbr && data->cbr != cbr)
752 return 1;
753 data->cbr = cbr;
754 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
755 return 0;
756
757 case INTEL_PT_TIP_PGD:
758 case INTEL_PT_TRACESTOP:
759 case INTEL_PT_OVF:
760 case INTEL_PT_BAD: /* Does not happen */
761 default:
762 return 1;
763 }
764
765 if (!data->cbr && decoder->cbr) {
766 data->cbr = decoder->cbr;
767 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
768 }
769
770 if (!data->cycle_cnt)
771 return 1;
772
773 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
774
775 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
776 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
777 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
778 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
779 return 1;
780 }
781
782 decoder->calc_cyc_to_tsc = cyc_to_tsc;
783 decoder->have_calc_cyc_to_tsc = true;
784
785 if (data->cbr) {
786 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
787 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
788 } else {
789 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
790 cyc_to_tsc, pkt_info->pos);
791 }
792
793 return 1;
794}
795
796static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
797 bool from_mtc)
798{
799 struct intel_pt_calc_cyc_to_tsc_info data = {
800 .cycle_cnt = 0,
801 .cbr = 0,
802 .last_mtc = decoder->last_mtc,
803 .ctc_timestamp = decoder->ctc_timestamp,
804 .ctc_delta = decoder->ctc_delta,
805 .tsc_timestamp = decoder->tsc_timestamp,
806 .timestamp = decoder->timestamp,
807 .have_tma = decoder->have_tma,
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300808 .fixup_last_mtc = decoder->fixup_last_mtc,
Adrian Huntercc336182015-07-17 19:33:57 +0300809 .from_mtc = from_mtc,
810 .cbr_cyc_to_tsc = 0,
811 };
812
813 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
814}
815
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300816static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
817{
818 int ret;
819
Adrian Huntercc336182015-07-17 19:33:57 +0300820 decoder->last_packet_type = decoder->packet.type;
821
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300822 do {
823 decoder->pos += decoder->pkt_step;
824 decoder->buf += decoder->pkt_step;
825 decoder->len -= decoder->pkt_step;
826
827 if (!decoder->len) {
828 ret = intel_pt_get_next_data(decoder);
829 if (ret)
830 return ret;
831 }
832
833 ret = intel_pt_get_packet(decoder->buf, decoder->len,
834 &decoder->packet);
835 if (ret == INTEL_PT_NEED_MORE_BYTES &&
836 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
837 ret = intel_pt_get_split_packet(decoder);
838 if (ret < 0)
839 return ret;
840 }
841 if (ret <= 0)
842 return intel_pt_bad_packet(decoder);
843
844 decoder->pkt_len = ret;
845 decoder->pkt_step = ret;
846 intel_pt_decoder_log_packet(decoder);
847 } while (decoder->packet.type == INTEL_PT_PAD);
848
849 return 0;
850}
851
852static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
853{
854 uint64_t timestamp, masked_timestamp;
855
856 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
857 masked_timestamp = timestamp & decoder->period_mask;
858 if (decoder->continuous_period) {
859 if (masked_timestamp != decoder->last_masked_timestamp)
860 return 1;
861 } else {
862 timestamp += 1;
863 masked_timestamp = timestamp & decoder->period_mask;
864 if (masked_timestamp != decoder->last_masked_timestamp) {
865 decoder->last_masked_timestamp = masked_timestamp;
866 decoder->continuous_period = true;
867 }
868 }
869 return decoder->period_ticks - (timestamp - masked_timestamp);
870}
871
872static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
873{
874 switch (decoder->period_type) {
875 case INTEL_PT_PERIOD_INSTRUCTIONS:
876 return decoder->period - decoder->period_insn_cnt;
877 case INTEL_PT_PERIOD_TICKS:
878 return intel_pt_next_period(decoder);
879 case INTEL_PT_PERIOD_NONE:
Adrian Hunter79b58422015-07-17 19:33:55 +0300880 case INTEL_PT_PERIOD_MTC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300881 default:
882 return 0;
883 }
884}
885
886static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
887{
888 uint64_t timestamp, masked_timestamp;
889
890 switch (decoder->period_type) {
891 case INTEL_PT_PERIOD_INSTRUCTIONS:
892 decoder->period_insn_cnt = 0;
893 break;
894 case INTEL_PT_PERIOD_TICKS:
895 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
896 masked_timestamp = timestamp & decoder->period_mask;
897 decoder->last_masked_timestamp = masked_timestamp;
898 break;
899 case INTEL_PT_PERIOD_NONE:
Adrian Hunter79b58422015-07-17 19:33:55 +0300900 case INTEL_PT_PERIOD_MTC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300901 default:
902 break;
903 }
904
905 decoder->state.type |= INTEL_PT_INSTRUCTION;
906}
907
908static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
909 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
910{
911 uint64_t max_insn_cnt, insn_cnt = 0;
912 int err;
913
Adrian Hunter79b58422015-07-17 19:33:55 +0300914 if (!decoder->mtc_insn)
915 decoder->mtc_insn = true;
916
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300917 max_insn_cnt = intel_pt_next_sample(decoder);
918
919 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
920 max_insn_cnt, decoder->data);
921
Adrian Hunter2a21d032015-07-17 19:33:48 +0300922 decoder->tot_insn_cnt += insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300923 decoder->timestamp_insn_cnt += insn_cnt;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300924 decoder->sample_insn_cnt += insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300925 decoder->period_insn_cnt += insn_cnt;
926
927 if (err) {
928 decoder->no_progress = 0;
929 decoder->pkt_state = INTEL_PT_STATE_ERR2;
930 intel_pt_log_at("ERROR: Failed to get instruction",
931 decoder->ip);
932 if (err == -ENOENT)
933 return -ENOLINK;
934 return -EILSEQ;
935 }
936
937 if (ip && decoder->ip == ip) {
938 err = -EAGAIN;
939 goto out;
940 }
941
942 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
943 intel_pt_sample_insn(decoder);
944
945 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
946 decoder->state.type = INTEL_PT_INSTRUCTION;
947 decoder->state.from_ip = decoder->ip;
948 decoder->state.to_ip = 0;
949 decoder->ip += intel_pt_insn->length;
950 err = INTEL_PT_RETURN;
951 goto out;
952 }
953
954 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
955 /* Zero-length calls are excluded */
956 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
957 intel_pt_insn->rel) {
958 err = intel_pt_push(&decoder->stack, decoder->ip +
959 intel_pt_insn->length);
960 if (err)
961 goto out;
962 }
963 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
964 decoder->ret_addr = intel_pt_pop(&decoder->stack);
965 }
966
967 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
968 int cnt = decoder->no_progress++;
969
970 decoder->state.from_ip = decoder->ip;
971 decoder->ip += intel_pt_insn->length +
972 intel_pt_insn->rel;
973 decoder->state.to_ip = decoder->ip;
974 err = INTEL_PT_RETURN;
975
976 /*
977 * Check for being stuck in a loop. This can happen if a
978 * decoder error results in the decoder erroneously setting the
979 * ip to an address that is itself in an infinite loop that
980 * consumes no packets. When that happens, there must be an
981 * unconditional branch.
982 */
983 if (cnt) {
984 if (cnt == 1) {
985 decoder->stuck_ip = decoder->state.to_ip;
986 decoder->stuck_ip_prd = 1;
987 decoder->stuck_ip_cnt = 1;
988 } else if (cnt > INTEL_PT_MAX_LOOPS ||
989 decoder->state.to_ip == decoder->stuck_ip) {
990 intel_pt_log_at("ERROR: Never-ending loop",
991 decoder->state.to_ip);
992 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
993 err = -ELOOP;
994 goto out;
995 } else if (!--decoder->stuck_ip_cnt) {
996 decoder->stuck_ip_prd += 1;
997 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
998 decoder->stuck_ip = decoder->state.to_ip;
999 }
1000 }
1001 goto out_no_progress;
1002 }
1003out:
1004 decoder->no_progress = 0;
1005out_no_progress:
1006 decoder->state.insn_op = intel_pt_insn->op;
1007 decoder->state.insn_len = intel_pt_insn->length;
Andi Kleenfaaa8762016-10-07 16:42:26 +03001008 memcpy(decoder->state.insn, intel_pt_insn->buf,
1009 INTEL_PT_INSN_BUF_SZ);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001010
1011 if (decoder->tx_flags & INTEL_PT_IN_TX)
1012 decoder->state.flags |= INTEL_PT_IN_TX;
1013
1014 return err;
1015}
1016
1017static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1018{
1019 struct intel_pt_insn intel_pt_insn;
1020 uint64_t ip;
1021 int err;
1022
1023 ip = decoder->last_ip;
1024
1025 while (1) {
1026 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1027 if (err == INTEL_PT_RETURN)
1028 return 0;
1029 if (err == -EAGAIN) {
1030 if (decoder->set_fup_tx_flags) {
1031 decoder->set_fup_tx_flags = false;
1032 decoder->tx_flags = decoder->fup_tx_flags;
1033 decoder->state.type = INTEL_PT_TRANSACTION;
1034 decoder->state.from_ip = decoder->ip;
1035 decoder->state.to_ip = 0;
1036 decoder->state.flags = decoder->fup_tx_flags;
1037 return 0;
1038 }
1039 return err;
1040 }
1041 decoder->set_fup_tx_flags = false;
1042 if (err)
1043 return err;
1044
1045 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1046 intel_pt_log_at("ERROR: Unexpected indirect branch",
1047 decoder->ip);
1048 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1049 return -ENOENT;
1050 }
1051
1052 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1053 intel_pt_log_at("ERROR: Unexpected conditional branch",
1054 decoder->ip);
1055 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1056 return -ENOENT;
1057 }
1058
1059 intel_pt_bug(decoder);
1060 }
1061}
1062
1063static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1064{
1065 struct intel_pt_insn intel_pt_insn;
1066 int err;
1067
1068 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001069 if (err == INTEL_PT_RETURN &&
1070 decoder->pgd_ip &&
1071 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1072 (decoder->state.type & INTEL_PT_BRANCH) &&
1073 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1074 /* Unconditional branch leaving filter region */
1075 decoder->no_progress = 0;
1076 decoder->pge = false;
1077 decoder->continuous_period = false;
1078 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1079 decoder->state.to_ip = 0;
1080 return 0;
1081 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001082 if (err == INTEL_PT_RETURN)
1083 return 0;
1084 if (err)
1085 return err;
1086
1087 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1088 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1089 decoder->pge = false;
1090 decoder->continuous_period = false;
1091 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1092 decoder->state.from_ip = decoder->ip;
1093 decoder->state.to_ip = 0;
1094 if (decoder->packet.count != 0)
1095 decoder->ip = decoder->last_ip;
1096 } else {
1097 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1098 decoder->state.from_ip = decoder->ip;
1099 if (decoder->packet.count == 0) {
1100 decoder->state.to_ip = 0;
1101 } else {
1102 decoder->state.to_ip = decoder->last_ip;
1103 decoder->ip = decoder->last_ip;
1104 }
1105 }
1106 return 0;
1107 }
1108
1109 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001110 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1111 intel_pt_insn.rel;
1112
1113 if (decoder->pgd_ip &&
1114 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1115 decoder->pgd_ip(to_ip, decoder->data)) {
1116 /* Conditional branch leaving filter region */
1117 decoder->pge = false;
1118 decoder->continuous_period = false;
1119 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1120 decoder->ip = to_ip;
1121 decoder->state.from_ip = decoder->ip;
1122 decoder->state.to_ip = 0;
1123 return 0;
1124 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001125 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1126 decoder->ip);
1127 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1128 return -ENOENT;
1129 }
1130
1131 return intel_pt_bug(decoder);
1132}
1133
1134static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1135{
1136 struct intel_pt_insn intel_pt_insn;
1137 int err;
1138
1139 while (1) {
1140 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1141 if (err == INTEL_PT_RETURN)
1142 return 0;
1143 if (err)
1144 return err;
1145
1146 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1147 if (!decoder->return_compression) {
1148 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1149 decoder->ip);
1150 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1151 return -ENOENT;
1152 }
1153 if (!decoder->ret_addr) {
1154 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1155 decoder->ip);
1156 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1157 return -ENOENT;
1158 }
1159 if (!(decoder->tnt.payload & BIT63)) {
1160 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1161 decoder->ip);
1162 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1163 return -ENOENT;
1164 }
1165 decoder->tnt.count -= 1;
1166 if (!decoder->tnt.count)
1167 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1168 decoder->tnt.payload <<= 1;
1169 decoder->state.from_ip = decoder->ip;
1170 decoder->ip = decoder->ret_addr;
1171 decoder->state.to_ip = decoder->ip;
1172 return 0;
1173 }
1174
1175 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1176 /* Handle deferred TIPs */
1177 err = intel_pt_get_next_packet(decoder);
1178 if (err)
1179 return err;
1180 if (decoder->packet.type != INTEL_PT_TIP ||
1181 decoder->packet.count == 0) {
1182 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1183 decoder->ip);
1184 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1185 decoder->pkt_step = 0;
1186 return -ENOENT;
1187 }
1188 intel_pt_set_last_ip(decoder);
1189 decoder->state.from_ip = decoder->ip;
1190 decoder->state.to_ip = decoder->last_ip;
1191 decoder->ip = decoder->last_ip;
1192 return 0;
1193 }
1194
1195 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1196 decoder->tnt.count -= 1;
1197 if (!decoder->tnt.count)
1198 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1199 if (decoder->tnt.payload & BIT63) {
1200 decoder->tnt.payload <<= 1;
1201 decoder->state.from_ip = decoder->ip;
1202 decoder->ip += intel_pt_insn.length +
1203 intel_pt_insn.rel;
1204 decoder->state.to_ip = decoder->ip;
1205 return 0;
1206 }
1207 /* Instruction sample for a non-taken branch */
1208 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1209 decoder->tnt.payload <<= 1;
1210 decoder->state.type = INTEL_PT_INSTRUCTION;
1211 decoder->state.from_ip = decoder->ip;
1212 decoder->state.to_ip = 0;
1213 decoder->ip += intel_pt_insn.length;
1214 return 0;
1215 }
1216 decoder->ip += intel_pt_insn.length;
1217 if (!decoder->tnt.count)
1218 return -EAGAIN;
1219 decoder->tnt.payload <<= 1;
1220 continue;
1221 }
1222
1223 return intel_pt_bug(decoder);
1224 }
1225}
1226
1227static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1228{
1229 unsigned int fup_tx_flags;
1230 int err;
1231
1232 fup_tx_flags = decoder->packet.payload &
1233 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1234 err = intel_pt_get_next_packet(decoder);
1235 if (err)
1236 return err;
1237 if (decoder->packet.type == INTEL_PT_FUP) {
1238 decoder->fup_tx_flags = fup_tx_flags;
1239 decoder->set_fup_tx_flags = true;
1240 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1241 *no_tip = true;
1242 } else {
1243 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1244 decoder->pos);
1245 intel_pt_update_in_tx(decoder);
1246 }
1247 return 0;
1248}
1249
1250static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1251{
1252 uint64_t timestamp;
1253
Adrian Hunter79b58422015-07-17 19:33:55 +03001254 decoder->have_tma = false;
1255
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001256 if (decoder->ref_timestamp) {
1257 timestamp = decoder->packet.payload |
1258 (decoder->ref_timestamp & (0xffULL << 56));
1259 if (timestamp < decoder->ref_timestamp) {
1260 if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1261 timestamp += (1ULL << 56);
1262 } else {
1263 if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1264 timestamp -= (1ULL << 56);
1265 }
1266 decoder->tsc_timestamp = timestamp;
1267 decoder->timestamp = timestamp;
1268 decoder->ref_timestamp = 0;
1269 decoder->timestamp_insn_cnt = 0;
1270 } else if (decoder->timestamp) {
1271 timestamp = decoder->packet.payload |
1272 (decoder->timestamp & (0xffULL << 56));
Adrian Hunter79b58422015-07-17 19:33:55 +03001273 decoder->tsc_timestamp = timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001274 if (timestamp < decoder->timestamp &&
Adrian Hunter79b58422015-07-17 19:33:55 +03001275 decoder->timestamp - timestamp < decoder->tsc_slip) {
1276 intel_pt_log_to("Suppressing backwards timestamp",
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001277 timestamp);
1278 timestamp = decoder->timestamp;
1279 }
Adrian Hunter9992c2d2015-09-25 16:15:34 +03001280 if (timestamp < decoder->timestamp) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001281 intel_pt_log_to("Wraparound timestamp", timestamp);
1282 timestamp += (1ULL << 56);
Adrian Hunter79b58422015-07-17 19:33:55 +03001283 decoder->tsc_timestamp = timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001284 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001285 decoder->timestamp = timestamp;
1286 decoder->timestamp_insn_cnt = 0;
1287 }
1288
Adrian Huntercc336182015-07-17 19:33:57 +03001289 if (decoder->last_packet_type == INTEL_PT_CYC) {
1290 decoder->cyc_ref_timestamp = decoder->timestamp;
1291 decoder->cycle_cnt = 0;
1292 decoder->have_calc_cyc_to_tsc = false;
1293 intel_pt_calc_cyc_to_tsc(decoder, false);
1294 }
1295
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001296 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1297}
1298
1299static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1300{
1301 intel_pt_log("ERROR: Buffer overflow\n");
1302 intel_pt_clear_tx_flags(decoder);
Adrian Hunter79b58422015-07-17 19:33:55 +03001303 decoder->have_tma = false;
Adrian Huntercc336182015-07-17 19:33:57 +03001304 decoder->cbr = 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001305 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1306 decoder->overflow = true;
1307 return -EOVERFLOW;
1308}
1309
Adrian Hunter79b58422015-07-17 19:33:55 +03001310static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1311{
1312 uint32_t ctc = decoder->packet.payload;
1313 uint32_t fc = decoder->packet.count;
1314 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1315
1316 if (!decoder->tsc_ctc_ratio_d)
1317 return;
1318
1319 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1320 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1321 if (decoder->tsc_ctc_mult) {
1322 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1323 } else {
1324 decoder->ctc_timestamp -= multdiv(ctc_rem,
1325 decoder->tsc_ctc_ratio_n,
1326 decoder->tsc_ctc_ratio_d);
1327 }
1328 decoder->ctc_delta = 0;
1329 decoder->have_tma = true;
Adrian Hunter3bccbe22016-09-28 14:41:36 +03001330 decoder->fixup_last_mtc = true;
Adrian Hunter79b58422015-07-17 19:33:55 +03001331 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1332 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1333}
1334
1335static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1336{
1337 uint64_t timestamp;
1338 uint32_t mtc, mtc_delta;
1339
1340 if (!decoder->have_tma)
1341 return;
1342
1343 mtc = decoder->packet.payload;
1344
Adrian Hunter3bccbe22016-09-28 14:41:36 +03001345 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1346 decoder->fixup_last_mtc = false;
1347 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1348 &decoder->last_mtc);
1349 }
1350
Adrian Hunter79b58422015-07-17 19:33:55 +03001351 if (mtc > decoder->last_mtc)
1352 mtc_delta = mtc - decoder->last_mtc;
1353 else
1354 mtc_delta = mtc + 256 - decoder->last_mtc;
1355
1356 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1357
1358 if (decoder->tsc_ctc_mult) {
1359 timestamp = decoder->ctc_timestamp +
1360 decoder->ctc_delta * decoder->tsc_ctc_mult;
1361 } else {
1362 timestamp = decoder->ctc_timestamp +
1363 multdiv(decoder->ctc_delta,
1364 decoder->tsc_ctc_ratio_n,
1365 decoder->tsc_ctc_ratio_d);
1366 }
1367
1368 if (timestamp < decoder->timestamp)
1369 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1370 timestamp, decoder->timestamp);
1371 else
1372 decoder->timestamp = timestamp;
1373
1374 decoder->timestamp_insn_cnt = 0;
1375 decoder->last_mtc = mtc;
Adrian Huntercc336182015-07-17 19:33:57 +03001376
1377 if (decoder->last_packet_type == INTEL_PT_CYC) {
1378 decoder->cyc_ref_timestamp = decoder->timestamp;
1379 decoder->cycle_cnt = 0;
1380 decoder->have_calc_cyc_to_tsc = false;
1381 intel_pt_calc_cyc_to_tsc(decoder, true);
1382 }
1383}
1384
1385static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1386{
1387 unsigned int cbr = decoder->packet.payload;
1388
1389 if (decoder->cbr == cbr)
1390 return;
1391
1392 decoder->cbr = cbr;
1393 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1394}
1395
1396static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1397{
1398 uint64_t timestamp = decoder->cyc_ref_timestamp;
1399
1400 decoder->have_cyc = true;
1401
1402 decoder->cycle_cnt += decoder->packet.payload;
1403
1404 if (!decoder->cyc_ref_timestamp)
1405 return;
1406
1407 if (decoder->have_calc_cyc_to_tsc)
1408 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1409 else if (decoder->cbr)
1410 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1411 else
1412 return;
1413
1414 if (timestamp < decoder->timestamp)
1415 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1416 timestamp, decoder->timestamp);
1417 else
1418 decoder->timestamp = timestamp;
Adrian Hunter51ee6482016-09-28 14:41:35 +03001419
1420 decoder->timestamp_insn_cnt = 0;
Adrian Hunter79b58422015-07-17 19:33:55 +03001421}
1422
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001423/* Walk PSB+ packets when already in sync. */
1424static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1425{
1426 int err;
1427
1428 while (1) {
1429 err = intel_pt_get_next_packet(decoder);
1430 if (err)
1431 return err;
1432
1433 switch (decoder->packet.type) {
1434 case INTEL_PT_PSBEND:
1435 return 0;
1436
1437 case INTEL_PT_TIP_PGD:
1438 case INTEL_PT_TIP_PGE:
1439 case INTEL_PT_TIP:
1440 case INTEL_PT_TNT:
Adrian Hunter3d498072015-07-17 19:33:53 +03001441 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001442 case INTEL_PT_BAD:
1443 case INTEL_PT_PSB:
Adrian Hunter79b58422015-07-17 19:33:55 +03001444 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001445 intel_pt_log("ERROR: Unexpected packet\n");
1446 return -EAGAIN;
1447
1448 case INTEL_PT_OVF:
1449 return intel_pt_overflow(decoder);
1450
1451 case INTEL_PT_TSC:
1452 intel_pt_calc_tsc_timestamp(decoder);
1453 break;
1454
Adrian Hunter3d498072015-07-17 19:33:53 +03001455 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001456 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001457 break;
1458
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001459 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001460 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001461 break;
1462
1463 case INTEL_PT_MODE_EXEC:
1464 decoder->exec_mode = decoder->packet.payload;
1465 break;
1466
1467 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001468 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001469 break;
1470
1471 case INTEL_PT_FUP:
1472 decoder->pge = true;
Adrian Hunterf952eac2017-05-26 11:17:07 +03001473 if (decoder->packet.count)
1474 intel_pt_set_last_ip(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001475 break;
1476
1477 case INTEL_PT_MODE_TSX:
1478 intel_pt_update_in_tx(decoder);
1479 break;
1480
Adrian Hunter3d498072015-07-17 19:33:53 +03001481 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001482 intel_pt_calc_mtc_timestamp(decoder);
1483 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1484 decoder->state.type |= INTEL_PT_INSTRUCTION;
Adrian Hunter3d498072015-07-17 19:33:53 +03001485 break;
1486
1487 case INTEL_PT_CYC:
1488 case INTEL_PT_VMCS:
1489 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001490 case INTEL_PT_PAD:
1491 default:
1492 break;
1493 }
1494 }
1495}
1496
1497static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1498{
1499 int err;
1500
1501 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1502 decoder->tx_flags = 0;
1503 decoder->state.flags &= ~INTEL_PT_IN_TX;
1504 decoder->state.flags |= INTEL_PT_ABORT_TX;
1505 } else {
1506 decoder->state.flags |= INTEL_PT_ASYNC;
1507 }
1508
1509 while (1) {
1510 err = intel_pt_get_next_packet(decoder);
1511 if (err)
1512 return err;
1513
1514 switch (decoder->packet.type) {
1515 case INTEL_PT_TNT:
1516 case INTEL_PT_FUP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001517 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001518 case INTEL_PT_PSB:
1519 case INTEL_PT_TSC:
Adrian Hunter3d498072015-07-17 19:33:53 +03001520 case INTEL_PT_TMA:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001521 case INTEL_PT_CBR:
1522 case INTEL_PT_MODE_TSX:
1523 case INTEL_PT_BAD:
1524 case INTEL_PT_PSBEND:
1525 intel_pt_log("ERROR: Missing TIP after FUP\n");
1526 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1527 return -ENOENT;
1528
1529 case INTEL_PT_OVF:
1530 return intel_pt_overflow(decoder);
1531
1532 case INTEL_PT_TIP_PGD:
1533 decoder->state.from_ip = decoder->ip;
1534 decoder->state.to_ip = 0;
1535 if (decoder->packet.count != 0) {
1536 intel_pt_set_ip(decoder);
1537 intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1538 decoder->ip);
1539 }
1540 decoder->pge = false;
1541 decoder->continuous_period = false;
1542 return 0;
1543
1544 case INTEL_PT_TIP_PGE:
1545 decoder->pge = true;
1546 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1547 decoder->ip);
1548 decoder->state.from_ip = 0;
1549 if (decoder->packet.count == 0) {
1550 decoder->state.to_ip = 0;
1551 } else {
1552 intel_pt_set_ip(decoder);
1553 decoder->state.to_ip = decoder->ip;
1554 }
1555 return 0;
1556
1557 case INTEL_PT_TIP:
1558 decoder->state.from_ip = decoder->ip;
1559 if (decoder->packet.count == 0) {
1560 decoder->state.to_ip = 0;
1561 } else {
1562 intel_pt_set_ip(decoder);
1563 decoder->state.to_ip = decoder->ip;
1564 }
1565 return 0;
1566
1567 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001568 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1569 break;
1570
1571 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001572 intel_pt_calc_mtc_timestamp(decoder);
1573 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1574 decoder->state.type |= INTEL_PT_INSTRUCTION;
Adrian Hunter3d498072015-07-17 19:33:53 +03001575 break;
1576
1577 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001578 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001579 break;
1580
1581 case INTEL_PT_MODE_EXEC:
1582 decoder->exec_mode = decoder->packet.payload;
1583 break;
1584
Adrian Hunter3d498072015-07-17 19:33:53 +03001585 case INTEL_PT_VMCS:
1586 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001587 case INTEL_PT_PAD:
1588 break;
1589
1590 default:
1591 return intel_pt_bug(decoder);
1592 }
1593 }
1594}
1595
1596static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1597{
1598 bool no_tip = false;
1599 int err;
1600
1601 while (1) {
1602 err = intel_pt_get_next_packet(decoder);
1603 if (err)
1604 return err;
1605next:
1606 switch (decoder->packet.type) {
1607 case INTEL_PT_TNT:
1608 if (!decoder->packet.count)
1609 break;
1610 decoder->tnt = decoder->packet;
1611 decoder->pkt_state = INTEL_PT_STATE_TNT;
1612 err = intel_pt_walk_tnt(decoder);
1613 if (err == -EAGAIN)
1614 break;
1615 return err;
1616
1617 case INTEL_PT_TIP_PGD:
1618 if (decoder->packet.count != 0)
1619 intel_pt_set_last_ip(decoder);
1620 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1621 return intel_pt_walk_tip(decoder);
1622
1623 case INTEL_PT_TIP_PGE: {
1624 decoder->pge = true;
1625 if (decoder->packet.count == 0) {
1626 intel_pt_log_at("Skipping zero TIP.PGE",
1627 decoder->pos);
1628 break;
1629 }
1630 intel_pt_set_ip(decoder);
1631 decoder->state.from_ip = 0;
1632 decoder->state.to_ip = decoder->ip;
1633 return 0;
1634 }
1635
1636 case INTEL_PT_OVF:
1637 return intel_pt_overflow(decoder);
1638
1639 case INTEL_PT_TIP:
1640 if (decoder->packet.count != 0)
1641 intel_pt_set_last_ip(decoder);
1642 decoder->pkt_state = INTEL_PT_STATE_TIP;
1643 return intel_pt_walk_tip(decoder);
1644
1645 case INTEL_PT_FUP:
1646 if (decoder->packet.count == 0) {
1647 intel_pt_log_at("Skipping zero FUP",
1648 decoder->pos);
1649 no_tip = false;
1650 break;
1651 }
1652 intel_pt_set_last_ip(decoder);
1653 err = intel_pt_walk_fup(decoder);
1654 if (err != -EAGAIN) {
1655 if (err)
1656 return err;
1657 if (no_tip)
1658 decoder->pkt_state =
1659 INTEL_PT_STATE_FUP_NO_TIP;
1660 else
1661 decoder->pkt_state = INTEL_PT_STATE_FUP;
1662 return 0;
1663 }
1664 if (no_tip) {
1665 no_tip = false;
1666 break;
1667 }
1668 return intel_pt_walk_fup_tip(decoder);
1669
Adrian Hunter3d498072015-07-17 19:33:53 +03001670 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03001671 decoder->pge = false;
1672 decoder->continuous_period = false;
1673 intel_pt_clear_tx_flags(decoder);
1674 decoder->have_tma = false;
Adrian Hunter3d498072015-07-17 19:33:53 +03001675 break;
1676
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001677 case INTEL_PT_PSB:
Adrian Hunteree14ac02017-05-26 11:17:06 +03001678 decoder->last_ip = 0;
1679 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001680 intel_pt_clear_stack(&decoder->stack);
1681 err = intel_pt_walk_psbend(decoder);
1682 if (err == -EAGAIN)
1683 goto next;
1684 if (err)
1685 return err;
1686 break;
1687
1688 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001689 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1690 break;
1691
1692 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001693 intel_pt_calc_mtc_timestamp(decoder);
1694 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1695 break;
1696 /*
1697 * Ensure that there has been an instruction since the
1698 * last MTC.
1699 */
1700 if (!decoder->mtc_insn)
1701 break;
1702 decoder->mtc_insn = false;
1703 /* Ensure that there is a timestamp */
1704 if (!decoder->timestamp)
1705 break;
1706 decoder->state.type = INTEL_PT_INSTRUCTION;
1707 decoder->state.from_ip = decoder->ip;
1708 decoder->state.to_ip = 0;
1709 decoder->mtc_insn = false;
1710 return 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001711
1712 case INTEL_PT_TSC:
1713 intel_pt_calc_tsc_timestamp(decoder);
1714 break;
1715
Adrian Hunter3d498072015-07-17 19:33:53 +03001716 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001717 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001718 break;
1719
1720 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001721 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001722 break;
1723
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001724 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001725 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001726 break;
1727
1728 case INTEL_PT_MODE_EXEC:
1729 decoder->exec_mode = decoder->packet.payload;
1730 break;
1731
1732 case INTEL_PT_MODE_TSX:
1733 /* MODE_TSX need not be followed by FUP */
1734 if (!decoder->pge) {
1735 intel_pt_update_in_tx(decoder);
1736 break;
1737 }
1738 err = intel_pt_mode_tsx(decoder, &no_tip);
1739 if (err)
1740 return err;
1741 goto next;
1742
1743 case INTEL_PT_BAD: /* Does not happen */
1744 return intel_pt_bug(decoder);
1745
1746 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03001747 case INTEL_PT_VMCS:
1748 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001749 case INTEL_PT_PAD:
1750 break;
1751
1752 default:
1753 return intel_pt_bug(decoder);
1754 }
1755 }
1756}
1757
Adrian Huntere1717e02016-07-20 12:00:06 +03001758static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
1759{
Adrian Hunterf952eac2017-05-26 11:17:07 +03001760 return decoder->packet.count &&
1761 (decoder->have_last_ip || decoder->packet.count == 3 ||
1762 decoder->packet.count == 6);
Adrian Huntere1717e02016-07-20 12:00:06 +03001763}
1764
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001765/* Walk PSB+ packets to get in sync. */
1766static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
1767{
1768 int err;
1769
1770 while (1) {
1771 err = intel_pt_get_next_packet(decoder);
1772 if (err)
1773 return err;
1774
1775 switch (decoder->packet.type) {
1776 case INTEL_PT_TIP_PGD:
1777 decoder->continuous_period = false;
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -03001778 __fallthrough;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001779 case INTEL_PT_TIP_PGE:
1780 case INTEL_PT_TIP:
1781 intel_pt_log("ERROR: Unexpected packet\n");
1782 return -ENOENT;
1783
1784 case INTEL_PT_FUP:
1785 decoder->pge = true;
Adrian Huntere1717e02016-07-20 12:00:06 +03001786 if (intel_pt_have_ip(decoder)) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001787 uint64_t current_ip = decoder->ip;
1788
1789 intel_pt_set_ip(decoder);
1790 if (current_ip)
1791 intel_pt_log_to("Setting IP",
1792 decoder->ip);
1793 }
1794 break;
1795
Adrian Hunter3d498072015-07-17 19:33:53 +03001796 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001797 intel_pt_calc_mtc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001798 break;
1799
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001800 case INTEL_PT_TSC:
1801 intel_pt_calc_tsc_timestamp(decoder);
1802 break;
1803
Adrian Hunter3d498072015-07-17 19:33:53 +03001804 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001805 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001806 break;
1807
1808 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001809 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001810 break;
1811
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001812 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001813 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001814 break;
1815
1816 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001817 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001818 break;
1819
1820 case INTEL_PT_MODE_EXEC:
1821 decoder->exec_mode = decoder->packet.payload;
1822 break;
1823
1824 case INTEL_PT_MODE_TSX:
1825 intel_pt_update_in_tx(decoder);
1826 break;
1827
Adrian Hunter3d498072015-07-17 19:33:53 +03001828 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03001829 decoder->pge = false;
1830 decoder->continuous_period = false;
1831 intel_pt_clear_tx_flags(decoder);
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -03001832 __fallthrough;
1833
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001834 case INTEL_PT_TNT:
Adrian Hunter79b58422015-07-17 19:33:55 +03001835 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001836 intel_pt_log("ERROR: Unexpected packet\n");
1837 if (decoder->ip)
1838 decoder->pkt_state = INTEL_PT_STATE_ERR4;
1839 else
1840 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1841 return -ENOENT;
1842
1843 case INTEL_PT_BAD: /* Does not happen */
1844 return intel_pt_bug(decoder);
1845
1846 case INTEL_PT_OVF:
1847 return intel_pt_overflow(decoder);
1848
1849 case INTEL_PT_PSBEND:
1850 return 0;
1851
1852 case INTEL_PT_PSB:
Adrian Hunter3d498072015-07-17 19:33:53 +03001853 case INTEL_PT_VMCS:
1854 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001855 case INTEL_PT_PAD:
1856 default:
1857 break;
1858 }
1859 }
1860}
1861
1862static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
1863{
1864 int err;
1865
1866 while (1) {
1867 err = intel_pt_get_next_packet(decoder);
1868 if (err)
1869 return err;
1870
1871 switch (decoder->packet.type) {
1872 case INTEL_PT_TIP_PGD:
1873 decoder->continuous_period = false;
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -03001874 __fallthrough;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001875 case INTEL_PT_TIP_PGE:
1876 case INTEL_PT_TIP:
1877 decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
Adrian Huntere1717e02016-07-20 12:00:06 +03001878 if (intel_pt_have_ip(decoder))
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001879 intel_pt_set_ip(decoder);
1880 if (decoder->ip)
1881 return 0;
1882 break;
1883
1884 case INTEL_PT_FUP:
Adrian Hunter622b7a42017-05-26 11:17:08 +03001885 if (intel_pt_have_ip(decoder))
1886 intel_pt_set_ip(decoder);
1887 if (decoder->ip)
1888 return 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001889 break;
1890
Adrian Hunter3d498072015-07-17 19:33:53 +03001891 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001892 intel_pt_calc_mtc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001893 break;
1894
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001895 case INTEL_PT_TSC:
1896 intel_pt_calc_tsc_timestamp(decoder);
1897 break;
1898
Adrian Hunter3d498072015-07-17 19:33:53 +03001899 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001900 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001901 break;
1902
1903 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001904 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001905 break;
1906
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001907 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001908 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001909 break;
1910
1911 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001912 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001913 break;
1914
1915 case INTEL_PT_MODE_EXEC:
1916 decoder->exec_mode = decoder->packet.payload;
1917 break;
1918
1919 case INTEL_PT_MODE_TSX:
1920 intel_pt_update_in_tx(decoder);
1921 break;
1922
1923 case INTEL_PT_OVF:
1924 return intel_pt_overflow(decoder);
1925
1926 case INTEL_PT_BAD: /* Does not happen */
1927 return intel_pt_bug(decoder);
1928
Adrian Hunter3d498072015-07-17 19:33:53 +03001929 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03001930 decoder->pge = false;
1931 decoder->continuous_period = false;
1932 intel_pt_clear_tx_flags(decoder);
1933 decoder->have_tma = false;
Adrian Hunter3d498072015-07-17 19:33:53 +03001934 break;
1935
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001936 case INTEL_PT_PSB:
Adrian Hunteree14ac02017-05-26 11:17:06 +03001937 decoder->last_ip = 0;
1938 decoder->have_last_ip = true;
Adrian Hunter12b70802017-05-26 11:17:04 +03001939 intel_pt_clear_stack(&decoder->stack);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001940 err = intel_pt_walk_psb(decoder);
1941 if (err)
1942 return err;
1943 if (decoder->ip) {
1944 /* Do not have a sample */
1945 decoder->state.type = 0;
1946 return 0;
1947 }
1948 break;
1949
1950 case INTEL_PT_TNT:
1951 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03001952 case INTEL_PT_VMCS:
1953 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001954 case INTEL_PT_PAD:
1955 default:
1956 break;
1957 }
1958 }
1959}
1960
1961static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
1962{
1963 int err;
1964
1965 intel_pt_log("Scanning for full IP\n");
1966 err = intel_pt_walk_to_ip(decoder);
1967 if (err)
1968 return err;
1969
1970 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1971 decoder->overflow = false;
1972
1973 decoder->state.from_ip = 0;
1974 decoder->state.to_ip = decoder->ip;
1975 intel_pt_log_to("Setting IP", decoder->ip);
1976
1977 return 0;
1978}
1979
1980static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
1981{
1982 const unsigned char *end = decoder->buf + decoder->len;
1983 size_t i;
1984
1985 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
1986 if (i > decoder->len)
1987 continue;
1988 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
1989 return i;
1990 }
1991 return 0;
1992}
1993
1994static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
1995{
1996 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
1997 const char *psb = INTEL_PT_PSB_STR;
1998
1999 if (rest_psb > decoder->len ||
2000 memcmp(decoder->buf, psb + part_psb, rest_psb))
2001 return 0;
2002
2003 return rest_psb;
2004}
2005
2006static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2007 int part_psb)
2008{
2009 int rest_psb, ret;
2010
2011 decoder->pos += decoder->len;
2012 decoder->len = 0;
2013
2014 ret = intel_pt_get_next_data(decoder);
2015 if (ret)
2016 return ret;
2017
2018 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2019 if (!rest_psb)
2020 return 0;
2021
2022 decoder->pos -= part_psb;
2023 decoder->next_buf = decoder->buf + rest_psb;
2024 decoder->next_len = decoder->len - rest_psb;
2025 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2026 decoder->buf = decoder->temp_buf;
2027 decoder->len = INTEL_PT_PSB_LEN;
2028
2029 return 0;
2030}
2031
2032static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2033{
2034 unsigned char *next;
2035 int ret;
2036
2037 intel_pt_log("Scanning for PSB\n");
2038 while (1) {
2039 if (!decoder->len) {
2040 ret = intel_pt_get_next_data(decoder);
2041 if (ret)
2042 return ret;
2043 }
2044
2045 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2046 INTEL_PT_PSB_LEN);
2047 if (!next) {
2048 int part_psb;
2049
2050 part_psb = intel_pt_part_psb(decoder);
2051 if (part_psb) {
2052 ret = intel_pt_get_split_psb(decoder, part_psb);
2053 if (ret)
2054 return ret;
2055 } else {
2056 decoder->pos += decoder->len;
2057 decoder->len = 0;
2058 }
2059 continue;
2060 }
2061
2062 decoder->pkt_step = next - decoder->buf;
2063 return intel_pt_get_next_packet(decoder);
2064 }
2065}
2066
2067static int intel_pt_sync(struct intel_pt_decoder *decoder)
2068{
2069 int err;
2070
2071 decoder->pge = false;
2072 decoder->continuous_period = false;
Adrian Hunteree14ac02017-05-26 11:17:06 +03002073 decoder->have_last_ip = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002074 decoder->last_ip = 0;
2075 decoder->ip = 0;
2076 intel_pt_clear_stack(&decoder->stack);
2077
2078 err = intel_pt_scan_for_psb(decoder);
2079 if (err)
2080 return err;
2081
Adrian Hunteree14ac02017-05-26 11:17:06 +03002082 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002083 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2084
2085 err = intel_pt_walk_psb(decoder);
2086 if (err)
2087 return err;
2088
2089 if (decoder->ip) {
2090 decoder->state.type = 0; /* Do not have a sample */
2091 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2092 } else {
2093 return intel_pt_sync_ip(decoder);
2094 }
2095
2096 return 0;
2097}
2098
2099static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2100{
Adrian Hunter3f04d982017-05-26 11:17:03 +03002101 uint64_t est = decoder->sample_insn_cnt << 1;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002102
2103 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2104 goto out;
2105
2106 est *= decoder->max_non_turbo_ratio;
2107 est /= decoder->cbr;
2108out:
Adrian Hunter3f04d982017-05-26 11:17:03 +03002109 return decoder->sample_timestamp + est;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002110}
2111
2112const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2113{
2114 int err;
2115
2116 do {
2117 decoder->state.type = INTEL_PT_BRANCH;
2118 decoder->state.flags = 0;
2119
2120 switch (decoder->pkt_state) {
2121 case INTEL_PT_STATE_NO_PSB:
2122 err = intel_pt_sync(decoder);
2123 break;
2124 case INTEL_PT_STATE_NO_IP:
Adrian Hunteree14ac02017-05-26 11:17:06 +03002125 decoder->have_last_ip = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002126 decoder->last_ip = 0;
Adrian Hunterad7167a2017-05-26 11:17:05 +03002127 decoder->ip = 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002128 /* Fall through */
2129 case INTEL_PT_STATE_ERR_RESYNC:
2130 err = intel_pt_sync_ip(decoder);
2131 break;
2132 case INTEL_PT_STATE_IN_SYNC:
2133 err = intel_pt_walk_trace(decoder);
2134 break;
2135 case INTEL_PT_STATE_TNT:
2136 err = intel_pt_walk_tnt(decoder);
2137 if (err == -EAGAIN)
2138 err = intel_pt_walk_trace(decoder);
2139 break;
2140 case INTEL_PT_STATE_TIP:
2141 case INTEL_PT_STATE_TIP_PGD:
2142 err = intel_pt_walk_tip(decoder);
2143 break;
2144 case INTEL_PT_STATE_FUP:
2145 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2146 err = intel_pt_walk_fup(decoder);
2147 if (err == -EAGAIN)
2148 err = intel_pt_walk_fup_tip(decoder);
2149 else if (!err)
2150 decoder->pkt_state = INTEL_PT_STATE_FUP;
2151 break;
2152 case INTEL_PT_STATE_FUP_NO_TIP:
2153 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2154 err = intel_pt_walk_fup(decoder);
2155 if (err == -EAGAIN)
2156 err = intel_pt_walk_trace(decoder);
2157 break;
2158 default:
2159 err = intel_pt_bug(decoder);
2160 break;
2161 }
2162 } while (err == -ENOLINK);
2163
Adrian Hunter22c06892017-05-26 11:17:02 +03002164 if (err) {
2165 decoder->state.err = intel_pt_ext_err(err);
2166 decoder->state.from_ip = decoder->ip;
Adrian Hunter3f04d982017-05-26 11:17:03 +03002167 decoder->sample_timestamp = decoder->timestamp;
2168 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
Adrian Hunter22c06892017-05-26 11:17:02 +03002169 } else {
2170 decoder->state.err = 0;
Adrian Hunter3f04d982017-05-26 11:17:03 +03002171 if (intel_pt_sample_time(decoder->pkt_state)) {
2172 decoder->sample_timestamp = decoder->timestamp;
2173 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2174 }
Adrian Hunter22c06892017-05-26 11:17:02 +03002175 }
2176
Adrian Hunter3f04d982017-05-26 11:17:03 +03002177 decoder->state.timestamp = decoder->sample_timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002178 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2179 decoder->state.cr3 = decoder->cr3;
Adrian Hunter2a21d032015-07-17 19:33:48 +03002180 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002181
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002182 return &decoder->state;
2183}
2184
2185static bool intel_pt_at_psb(unsigned char *buf, size_t len)
2186{
2187 if (len < INTEL_PT_PSB_LEN)
2188 return false;
2189 return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
2190 INTEL_PT_PSB_LEN);
2191}
2192
2193/**
2194 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2195 * @buf: pointer to buffer pointer
2196 * @len: size of buffer
2197 *
2198 * Updates the buffer pointer to point to the start of the next PSB packet if
2199 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2200 * @len is adjusted accordingly.
2201 *
2202 * Return: %true if a PSB packet is found, %false otherwise.
2203 */
2204static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2205{
2206 unsigned char *next;
2207
2208 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2209 if (next) {
2210 *len -= next - *buf;
2211 *buf = next;
2212 return true;
2213 }
2214 return false;
2215}
2216
2217/**
2218 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2219 * packet.
2220 * @buf: pointer to buffer pointer
2221 * @len: size of buffer
2222 *
2223 * Updates the buffer pointer to point to the start of the following PSB packet
2224 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2225 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2226 *
2227 * Return: %true if a PSB packet is found, %false otherwise.
2228 */
2229static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2230{
2231 unsigned char *next;
2232
2233 if (!*len)
2234 return false;
2235
2236 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2237 if (next) {
2238 *len -= next - *buf;
2239 *buf = next;
2240 return true;
2241 }
2242 return false;
2243}
2244
2245/**
2246 * intel_pt_last_psb - find the last PSB packet in a buffer.
2247 * @buf: buffer
2248 * @len: size of buffer
2249 *
2250 * This function finds the last PSB in a buffer.
2251 *
2252 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2253 */
2254static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2255{
2256 const char *n = INTEL_PT_PSB_STR;
2257 unsigned char *p;
2258 size_t k;
2259
2260 if (len < INTEL_PT_PSB_LEN)
2261 return NULL;
2262
2263 k = len - INTEL_PT_PSB_LEN + 1;
2264 while (1) {
2265 p = memrchr(buf, n[0], k);
2266 if (!p)
2267 return NULL;
2268 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2269 return p;
2270 k = p - buf;
2271 if (!k)
2272 return NULL;
2273 }
2274}
2275
2276/**
2277 * intel_pt_next_tsc - find and return next TSC.
2278 * @buf: buffer
2279 * @len: size of buffer
2280 * @tsc: TSC value returned
2281 *
2282 * Find a TSC packet in @buf and return the TSC value. This function assumes
2283 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2284 * PSBEND packet is found.
2285 *
2286 * Return: %true if TSC is found, false otherwise.
2287 */
2288static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
2289{
2290 struct intel_pt_pkt packet;
2291 int ret;
2292
2293 while (len) {
2294 ret = intel_pt_get_packet(buf, len, &packet);
2295 if (ret <= 0)
2296 return false;
2297 if (packet.type == INTEL_PT_TSC) {
2298 *tsc = packet.payload;
2299 return true;
2300 }
2301 if (packet.type == INTEL_PT_PSBEND)
2302 return false;
2303 buf += ret;
2304 len -= ret;
2305 }
2306 return false;
2307}
2308
2309/**
2310 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2311 * @tsc1: first TSC to compare
2312 * @tsc2: second TSC to compare
2313 *
2314 * This function compares 7-byte TSC values allowing for the possibility that
2315 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2316 * around so for that purpose this function assumes the absolute difference is
2317 * less than half the maximum difference.
2318 *
2319 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2320 * after @tsc2.
2321 */
2322static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2323{
2324 const uint64_t halfway = (1ULL << 55);
2325
2326 if (tsc1 == tsc2)
2327 return 0;
2328
2329 if (tsc1 < tsc2) {
2330 if (tsc2 - tsc1 < halfway)
2331 return -1;
2332 else
2333 return 1;
2334 } else {
2335 if (tsc1 - tsc2 < halfway)
2336 return 1;
2337 else
2338 return -1;
2339 }
2340}
2341
2342/**
2343 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2344 * using TSC.
2345 * @buf_a: first buffer
2346 * @len_a: size of first buffer
2347 * @buf_b: second buffer
2348 * @len_b: size of second buffer
2349 *
2350 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2351 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2352 * walk forward in @buf_b until a later TSC is found. A precondition is that
2353 * @buf_a and @buf_b are positioned at a PSB.
2354 *
2355 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2356 * @buf_b + @len_b if there is no non-overlapped data.
2357 */
2358static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2359 size_t len_a,
2360 unsigned char *buf_b,
2361 size_t len_b)
2362{
2363 uint64_t tsc_a, tsc_b;
2364 unsigned char *p;
2365 size_t len;
2366
2367 p = intel_pt_last_psb(buf_a, len_a);
2368 if (!p)
2369 return buf_b; /* No PSB in buf_a => no overlap */
2370
2371 len = len_a - (p - buf_a);
2372 if (!intel_pt_next_tsc(p, len, &tsc_a)) {
2373 /* The last PSB+ in buf_a is incomplete, so go back one more */
2374 len_a -= len;
2375 p = intel_pt_last_psb(buf_a, len_a);
2376 if (!p)
2377 return buf_b; /* No full PSB+ => assume no overlap */
2378 len = len_a - (p - buf_a);
2379 if (!intel_pt_next_tsc(p, len, &tsc_a))
2380 return buf_b; /* No TSC in buf_a => assume no overlap */
2381 }
2382
2383 while (1) {
2384 /* Ignore PSB+ with no TSC */
2385 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
2386 intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
2387 return buf_b; /* tsc_a < tsc_b => no overlap */
2388
2389 if (!intel_pt_step_psb(&buf_b, &len_b))
2390 return buf_b + len_b; /* No PSB in buf_b => no data */
2391 }
2392}
2393
2394/**
2395 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2396 * @buf_a: first buffer
2397 * @len_a: size of first buffer
2398 * @buf_b: second buffer
2399 * @len_b: size of second buffer
2400 * @have_tsc: can use TSC packets to detect overlap
2401 *
2402 * When trace samples or snapshots are recorded there is the possibility that
2403 * the data overlaps. Note that, for the purposes of decoding, data is only
2404 * useful if it begins with a PSB packet.
2405 *
2406 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2407 * @buf_b + @len_b if there is no non-overlapped data.
2408 */
2409unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2410 unsigned char *buf_b, size_t len_b,
2411 bool have_tsc)
2412{
2413 unsigned char *found;
2414
2415 /* Buffer 'b' must start at PSB so throw away everything before that */
2416 if (!intel_pt_next_psb(&buf_b, &len_b))
2417 return buf_b + len_b; /* No PSB */
2418
2419 if (!intel_pt_next_psb(&buf_a, &len_a))
2420 return buf_b; /* No overlap */
2421
2422 if (have_tsc) {
2423 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
2424 if (found)
2425 return found;
2426 }
2427
2428 /*
2429 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2430 * we can ignore the first part of buffer 'a'.
2431 */
2432 while (len_b < len_a) {
2433 if (!intel_pt_step_psb(&buf_a, &len_a))
2434 return buf_b; /* No overlap */
2435 }
2436
2437 /* Now len_b >= len_a */
2438 if (len_b > len_a) {
2439 /* The leftover buffer 'b' must start at a PSB */
2440 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2441 if (!intel_pt_step_psb(&buf_a, &len_a))
2442 return buf_b; /* No overlap */
2443 }
2444 }
2445
2446 while (1) {
2447 /* Potential overlap so check the bytes */
2448 found = memmem(buf_a, len_a, buf_b, len_a);
2449 if (found)
2450 return buf_b + len_a;
2451
2452 /* Try again at next PSB in buffer 'a' */
2453 if (!intel_pt_step_psb(&buf_a, &len_a))
2454 return buf_b; /* No overlap */
2455
2456 /* The leftover buffer 'b' must start at a PSB */
2457 while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
2458 if (!intel_pt_step_psb(&buf_a, &len_a))
2459 return buf_b; /* No overlap */
2460 }
2461 }
2462}