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