OSDN Git Service

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