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