blob: dde6a7a97a7a3f7d9e6a3abd7e5d3ed996be3d19 [file] [log] [blame]
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001/*
2 * intel_pt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#ifndef _GNU_SOURCE
17#define _GNU_SOURCE
18#endif
19#include <stdlib.h>
20#include <stdbool.h>
21#include <string.h>
22#include <errno.h>
23#include <stdint.h>
24#include <inttypes.h>
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -030025#include <linux/compiler.h>
Adrian Hunterf4aa0812015-07-17 19:33:40 +030026
27#include "../cache.h"
28#include "../util.h"
Adrian Hunter5a99d992019-02-06 12:39:44 +020029#include "../auxtrace.h"
Adrian Hunterf4aa0812015-07-17 19:33:40 +030030
31#include "intel-pt-insn-decoder.h"
32#include "intel-pt-pkt-decoder.h"
33#include "intel-pt-decoder.h"
34#include "intel-pt-log.h"
35
36#define INTEL_PT_BLK_SIZE 1024
37
38#define BIT63 (((uint64_t)1 << 63))
39
40#define INTEL_PT_RETURN 1
41
42/* Maximum number of loops with no packets consumed i.e. stuck in a loop */
43#define INTEL_PT_MAX_LOOPS 10000
44
45struct intel_pt_blk {
46 struct intel_pt_blk *prev;
47 uint64_t ip[INTEL_PT_BLK_SIZE];
48};
49
50struct intel_pt_stack {
51 struct intel_pt_blk *blk;
52 struct intel_pt_blk *spare;
53 int pos;
54};
55
56enum intel_pt_pkt_state {
57 INTEL_PT_STATE_NO_PSB,
58 INTEL_PT_STATE_NO_IP,
59 INTEL_PT_STATE_ERR_RESYNC,
60 INTEL_PT_STATE_IN_SYNC,
Adrian Hunter61b6e082019-05-10 15:41:42 +030061 INTEL_PT_STATE_TNT_CONT,
Adrian Hunterf4aa0812015-07-17 19:33:40 +030062 INTEL_PT_STATE_TNT,
63 INTEL_PT_STATE_TIP,
64 INTEL_PT_STATE_TIP_PGD,
65 INTEL_PT_STATE_FUP,
66 INTEL_PT_STATE_FUP_NO_TIP,
67};
68
Adrian Hunter3f04d982017-05-26 11:17:03 +030069static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
70{
71 switch (pkt_state) {
72 case INTEL_PT_STATE_NO_PSB:
73 case INTEL_PT_STATE_NO_IP:
74 case INTEL_PT_STATE_ERR_RESYNC:
75 case INTEL_PT_STATE_IN_SYNC:
Adrian Hunter61b6e082019-05-10 15:41:42 +030076 case INTEL_PT_STATE_TNT_CONT:
Adrian Hunter3f04d982017-05-26 11:17:03 +030077 return true;
Adrian Hunter61b6e082019-05-10 15:41:42 +030078 case INTEL_PT_STATE_TNT:
Adrian Hunter3f04d982017-05-26 11:17:03 +030079 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
Adrian Hunterf4aa0812015-07-17 19:33:40 +030089#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
101struct 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);
Adrian Hunter9f1d1222016-09-23 17:38:47 +0300106 bool (*pgd_ip)(uint64_t ip, void *data);
Adrian Hunter4d678e92019-06-04 16:00:02 +0300107 int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300108 void *data;
109 struct intel_pt_state state;
110 const unsigned char *buf;
111 size_t len;
112 bool return_compression;
Adrian Hunter83959812017-05-26 11:17:11 +0300113 bool branch_enable;
Adrian Hunter79b58422015-07-17 19:33:55 +0300114 bool mtc_insn;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300115 bool pge;
Adrian Hunter79b58422015-07-17 19:33:55 +0300116 bool have_tma;
Adrian Huntercc336182015-07-17 19:33:57 +0300117 bool have_cyc;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300118 bool fixup_last_mtc;
Adrian Hunteree14ac02017-05-26 11:17:06 +0300119 bool have_last_ip;
Adrian Hunter9bc668e2019-05-20 14:37:15 +0300120 bool in_psb;
Adrian Hunter9fb52332018-05-31 13:23:45 +0300121 enum intel_pt_param_flags flags;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300122 uint64_t pos;
123 uint64_t last_ip;
124 uint64_t ip;
125 uint64_t cr3;
126 uint64_t timestamp;
127 uint64_t tsc_timestamp;
128 uint64_t ref_timestamp;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300129 uint64_t sample_timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300130 uint64_t ret_addr;
Adrian Hunter79b58422015-07-17 19:33:55 +0300131 uint64_t ctc_timestamp;
132 uint64_t ctc_delta;
Adrian Huntercc336182015-07-17 19:33:57 +0300133 uint64_t cycle_cnt;
134 uint64_t cyc_ref_timestamp;
Adrian Hunter79b58422015-07-17 19:33:55 +0300135 uint32_t last_mtc;
136 uint32_t tsc_ctc_ratio_n;
137 uint32_t tsc_ctc_ratio_d;
138 uint32_t tsc_ctc_mult;
139 uint32_t tsc_slip;
140 uint32_t ctc_rem_mask;
141 int mtc_shift;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300142 struct intel_pt_stack stack;
143 enum intel_pt_pkt_state pkt_state;
144 struct intel_pt_pkt packet;
145 struct intel_pt_pkt tnt;
146 int pkt_step;
147 int pkt_len;
Adrian Huntercc336182015-07-17 19:33:57 +0300148 int last_packet_type;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300149 unsigned int cbr;
Adrian Hunter0a7c700d2017-05-26 11:17:16 +0300150 unsigned int cbr_seen;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300151 unsigned int max_non_turbo_ratio;
Adrian Huntercc336182015-07-17 19:33:57 +0300152 double max_non_turbo_ratio_fp;
153 double cbr_cyc_to_tsc;
154 double calc_cyc_to_tsc;
155 bool have_calc_cyc_to_tsc;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300156 int exec_mode;
157 unsigned int insn_bytes;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300158 uint64_t period;
159 enum intel_pt_period_type period_type;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300160 uint64_t tot_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300161 uint64_t period_insn_cnt;
162 uint64_t period_mask;
163 uint64_t period_ticks;
164 uint64_t last_masked_timestamp;
Adrian Hunter7b4b4f82019-05-20 14:37:11 +0300165 uint64_t tot_cyc_cnt;
166 uint64_t sample_tot_cyc_cnt;
Adrian Hunter3f055162019-05-20 14:37:17 +0300167 uint64_t base_cyc_cnt;
168 uint64_t cyc_cnt_timestamp;
169 double tsc_to_cyc;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300170 bool continuous_period;
171 bool overflow;
172 bool set_fup_tx_flags;
Adrian Huntera472e652017-05-26 11:17:14 +0300173 bool set_fup_ptw;
174 bool set_fup_mwait;
175 bool set_fup_pwre;
176 bool set_fup_exstop;
Adrian Hunter7b4b4f82019-05-20 14:37:11 +0300177 bool sample_cyc;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300178 unsigned int fup_tx_flags;
179 unsigned int tx_flags;
Adrian Huntera472e652017-05-26 11:17:14 +0300180 uint64_t fup_ptw_payload;
181 uint64_t fup_mwait_payload;
182 uint64_t fup_pwre_payload;
Adrian Hunter0a7c700d2017-05-26 11:17:16 +0300183 uint64_t cbr_payload;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300184 uint64_t timestamp_insn_cnt;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300185 uint64_t sample_insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300186 uint64_t stuck_ip;
187 int no_progress;
188 int stuck_ip_prd;
189 int stuck_ip_cnt;
190 const unsigned char *next_buf;
191 size_t next_len;
192 unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
193};
194
195static uint64_t intel_pt_lower_power_of_2(uint64_t x)
196{
197 int i;
198
199 for (i = 0; x != 1; i++)
200 x >>= 1;
201
202 return x << i;
203}
204
205static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
206{
207 if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
208 uint64_t period;
209
210 period = intel_pt_lower_power_of_2(decoder->period);
211 decoder->period_mask = ~(period - 1);
212 decoder->period_ticks = period;
213 }
214}
215
Adrian Hunter79b58422015-07-17 19:33:55 +0300216static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
217{
218 if (!d)
219 return 0;
220 return (t / d) * n + ((t % d) * n) / d;
221}
222
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300223struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
224{
225 struct intel_pt_decoder *decoder;
226
227 if (!params->get_trace || !params->walk_insn)
228 return NULL;
229
230 decoder = zalloc(sizeof(struct intel_pt_decoder));
231 if (!decoder)
232 return NULL;
233
234 decoder->get_trace = params->get_trace;
235 decoder->walk_insn = params->walk_insn;
Adrian Hunter9f1d1222016-09-23 17:38:47 +0300236 decoder->pgd_ip = params->pgd_ip;
Adrian Hunter4d678e92019-06-04 16:00:02 +0300237 decoder->lookahead = params->lookahead;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300238 decoder->data = params->data;
239 decoder->return_compression = params->return_compression;
Adrian Hunter83959812017-05-26 11:17:11 +0300240 decoder->branch_enable = params->branch_enable;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300241
Adrian Hunter9fb52332018-05-31 13:23:45 +0300242 decoder->flags = params->flags;
243
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300244 decoder->period = params->period;
245 decoder->period_type = params->period_type;
246
Adrian Huntercc336182015-07-17 19:33:57 +0300247 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
248 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300249
250 intel_pt_setup_period(decoder);
251
Adrian Hunter79b58422015-07-17 19:33:55 +0300252 decoder->mtc_shift = params->mtc_period;
253 decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
254
255 decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
256 decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
257
258 if (!decoder->tsc_ctc_ratio_n)
259 decoder->tsc_ctc_ratio_d = 0;
260
261 if (decoder->tsc_ctc_ratio_d) {
262 if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
263 decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
264 decoder->tsc_ctc_ratio_d;
Adrian Hunter79b58422015-07-17 19:33:55 +0300265 }
Adrian Hunterf3b4e062019-03-25 15:51:35 +0200266
267 /*
268 * A TSC packet can slip past MTC packets so that the timestamp appears
269 * to go backwards. One estimate is that can be up to about 40 CPU
270 * cycles, which is certainly less than 0x1000 TSC ticks, but accept
271 * slippage an order of magnitude more to be on the safe side.
272 */
273 decoder->tsc_slip = 0x10000;
Adrian Hunter79b58422015-07-17 19:33:55 +0300274
275 intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
276 intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
277 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
278 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
279 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
280
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300281 return decoder;
282}
283
284static void intel_pt_pop_blk(struct intel_pt_stack *stack)
285{
286 struct intel_pt_blk *blk = stack->blk;
287
288 stack->blk = blk->prev;
289 if (!stack->spare)
290 stack->spare = blk;
291 else
292 free(blk);
293}
294
295static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
296{
297 if (!stack->pos) {
298 if (!stack->blk)
299 return 0;
300 intel_pt_pop_blk(stack);
301 if (!stack->blk)
302 return 0;
303 stack->pos = INTEL_PT_BLK_SIZE;
304 }
305 return stack->blk->ip[--stack->pos];
306}
307
308static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
309{
310 struct intel_pt_blk *blk;
311
312 if (stack->spare) {
313 blk = stack->spare;
314 stack->spare = NULL;
315 } else {
316 blk = malloc(sizeof(struct intel_pt_blk));
317 if (!blk)
318 return -ENOMEM;
319 }
320
321 blk->prev = stack->blk;
322 stack->blk = blk;
323 stack->pos = 0;
324 return 0;
325}
326
327static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
328{
329 int err;
330
331 if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
332 err = intel_pt_alloc_blk(stack);
333 if (err)
334 return err;
335 }
336
337 stack->blk->ip[stack->pos++] = ip;
338 return 0;
339}
340
341static void intel_pt_clear_stack(struct intel_pt_stack *stack)
342{
343 while (stack->blk)
344 intel_pt_pop_blk(stack);
345 stack->pos = 0;
346}
347
348static void intel_pt_free_stack(struct intel_pt_stack *stack)
349{
350 intel_pt_clear_stack(stack);
351 zfree(&stack->blk);
352 zfree(&stack->spare);
353}
354
355void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
356{
357 intel_pt_free_stack(&decoder->stack);
358 free(decoder);
359}
360
361static int intel_pt_ext_err(int code)
362{
363 switch (code) {
364 case -ENOMEM:
365 return INTEL_PT_ERR_NOMEM;
366 case -ENOSYS:
367 return INTEL_PT_ERR_INTERN;
368 case -EBADMSG:
369 return INTEL_PT_ERR_BADPKT;
370 case -ENODATA:
371 return INTEL_PT_ERR_NODATA;
372 case -EILSEQ:
373 return INTEL_PT_ERR_NOINSN;
374 case -ENOENT:
375 return INTEL_PT_ERR_MISMAT;
376 case -EOVERFLOW:
377 return INTEL_PT_ERR_OVR;
378 case -ENOSPC:
379 return INTEL_PT_ERR_LOST;
380 case -ELOOP:
381 return INTEL_PT_ERR_NELOOP;
382 default:
383 return INTEL_PT_ERR_UNK;
384 }
385}
386
387static const char *intel_pt_err_msgs[] = {
388 [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
389 [INTEL_PT_ERR_INTERN] = "Internal error",
390 [INTEL_PT_ERR_BADPKT] = "Bad packet",
391 [INTEL_PT_ERR_NODATA] = "No more data",
392 [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
393 [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
394 [INTEL_PT_ERR_OVR] = "Overflow packet",
395 [INTEL_PT_ERR_LOST] = "Lost trace data",
396 [INTEL_PT_ERR_UNK] = "Unknown error!",
397 [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
398};
399
400int intel_pt__strerror(int code, char *buf, size_t buflen)
401{
Colin Ian Kingc0664892016-04-24 19:56:43 +0100402 if (code < 1 || code >= INTEL_PT_ERR_MAX)
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300403 code = INTEL_PT_ERR_UNK;
404 strlcpy(buf, intel_pt_err_msgs[code], buflen);
405 return 0;
406}
407
Adrian Huntere1717e02016-07-20 12:00:06 +0300408static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300409 uint64_t last_ip)
410{
411 uint64_t ip;
412
413 switch (packet->count) {
Adrian Huntere1717e02016-07-20 12:00:06 +0300414 case 1:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300415 ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
416 packet->payload;
417 break;
Adrian Huntere1717e02016-07-20 12:00:06 +0300418 case 2:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300419 ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
420 packet->payload;
421 break;
Adrian Huntere1717e02016-07-20 12:00:06 +0300422 case 3:
423 ip = packet->payload;
424 /* Sign-extend 6-byte ip */
425 if (ip & (uint64_t)0x800000000000ULL)
426 ip |= (uint64_t)0xffff000000000000ULL;
427 break;
428 case 4:
429 ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
430 packet->payload;
431 break;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300432 case 6:
433 ip = packet->payload;
434 break;
435 default:
436 return 0;
437 }
438
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300439 return ip;
440}
441
442static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
443{
Adrian Huntere1717e02016-07-20 12:00:06 +0300444 decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
Adrian Hunteree14ac02017-05-26 11:17:06 +0300445 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300446}
447
448static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
449{
450 intel_pt_set_last_ip(decoder);
451 decoder->ip = decoder->last_ip;
452}
453
454static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
455{
456 intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
457 decoder->buf);
458}
459
460static int intel_pt_bug(struct intel_pt_decoder *decoder)
461{
462 intel_pt_log("ERROR: Internal error\n");
463 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
464 return -ENOSYS;
465}
466
467static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
468{
469 decoder->tx_flags = 0;
470}
471
472static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
473{
474 decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
475}
476
477static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
478{
479 intel_pt_clear_tx_flags(decoder);
Adrian Hunter79b58422015-07-17 19:33:55 +0300480 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300481 decoder->pkt_len = 1;
482 decoder->pkt_step = 1;
483 intel_pt_decoder_log_packet(decoder);
484 if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
485 intel_pt_log("ERROR: Bad packet\n");
486 decoder->pkt_state = INTEL_PT_STATE_ERR1;
487 }
488 return -EBADMSG;
489}
490
Adrian Hunter948e9dc2019-05-20 14:37:10 +0300491static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
492{
493 decoder->sample_timestamp = decoder->timestamp;
494 decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
495}
496
Adrian Hunter6492e5f2019-06-04 16:00:04 +0300497static void intel_pt_reposition(struct intel_pt_decoder *decoder)
498{
499 decoder->ip = 0;
500 decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
501 decoder->timestamp = 0;
502 decoder->have_tma = false;
503}
504
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300505static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300506{
507 struct intel_pt_buffer buffer = { .buf = 0, };
508 int ret;
509
510 decoder->pkt_step = 0;
511
512 intel_pt_log("Getting more data\n");
513 ret = decoder->get_trace(&buffer, decoder->data);
514 if (ret)
515 return ret;
516 decoder->buf = buffer.buf;
517 decoder->len = buffer.len;
518 if (!decoder->len) {
519 intel_pt_log("No more data\n");
520 return -ENODATA;
521 }
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300522 if (!buffer.consecutive || reposition) {
Adrian Hunter6492e5f2019-06-04 16:00:04 +0300523 intel_pt_reposition(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300524 decoder->ref_timestamp = buffer.ref_timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300525 decoder->state.trace_nr = buffer.trace_nr;
526 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
527 decoder->ref_timestamp);
528 return -ENOLINK;
529 }
530
531 return 0;
532}
533
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300534static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
535 bool reposition)
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300536{
537 if (!decoder->next_buf)
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300538 return intel_pt_get_data(decoder, reposition);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300539
540 decoder->buf = decoder->next_buf;
541 decoder->len = decoder->next_len;
542 decoder->next_buf = 0;
543 decoder->next_len = 0;
544 return 0;
545}
546
547static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
548{
549 unsigned char *buf = decoder->temp_buf;
550 size_t old_len, len, n;
551 int ret;
552
553 old_len = decoder->len;
554 len = decoder->len;
555 memcpy(buf, decoder->buf, len);
556
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300557 ret = intel_pt_get_data(decoder, false);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300558 if (ret) {
559 decoder->pos += old_len;
560 return ret < 0 ? ret : -EINVAL;
561 }
562
563 n = INTEL_PT_PKT_MAX_SZ - len;
564 if (n > decoder->len)
565 n = decoder->len;
566 memcpy(buf + len, decoder->buf, n);
567 len += n;
568
569 ret = intel_pt_get_packet(buf, len, &decoder->packet);
570 if (ret < (int)old_len) {
571 decoder->next_buf = decoder->buf;
572 decoder->next_len = decoder->len;
573 decoder->buf = buf;
574 decoder->len = old_len;
575 return intel_pt_bad_packet(decoder);
576 }
577
578 decoder->next_buf = decoder->buf + (ret - old_len);
579 decoder->next_len = decoder->len - (ret - old_len);
580
581 decoder->buf = buf;
582 decoder->len = ret;
583
584 return ret;
585}
586
Adrian Huntercc336182015-07-17 19:33:57 +0300587struct intel_pt_pkt_info {
588 struct intel_pt_decoder *decoder;
589 struct intel_pt_pkt packet;
590 uint64_t pos;
591 int pkt_len;
592 int last_packet_type;
593 void *data;
594};
595
596typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
597
598/* Lookahead packets in current buffer */
599static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
600 intel_pt_pkt_cb_t cb, void *data)
601{
602 struct intel_pt_pkt_info pkt_info;
603 const unsigned char *buf = decoder->buf;
604 size_t len = decoder->len;
605 int ret;
606
607 pkt_info.decoder = decoder;
608 pkt_info.pos = decoder->pos;
609 pkt_info.pkt_len = decoder->pkt_step;
610 pkt_info.last_packet_type = decoder->last_packet_type;
611 pkt_info.data = data;
612
613 while (1) {
614 do {
615 pkt_info.pos += pkt_info.pkt_len;
616 buf += pkt_info.pkt_len;
617 len -= pkt_info.pkt_len;
618
619 if (!len)
620 return INTEL_PT_NEED_MORE_BYTES;
621
622 ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
623 if (!ret)
624 return INTEL_PT_NEED_MORE_BYTES;
625 if (ret < 0)
626 return ret;
627
628 pkt_info.pkt_len = ret;
629 } while (pkt_info.packet.type == INTEL_PT_PAD);
630
631 ret = cb(&pkt_info);
632 if (ret)
633 return 0;
634
635 pkt_info.last_packet_type = pkt_info.packet.type;
636 }
637}
638
639struct intel_pt_calc_cyc_to_tsc_info {
640 uint64_t cycle_cnt;
641 unsigned int cbr;
642 uint32_t last_mtc;
643 uint64_t ctc_timestamp;
644 uint64_t ctc_delta;
645 uint64_t tsc_timestamp;
646 uint64_t timestamp;
647 bool have_tma;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300648 bool fixup_last_mtc;
Adrian Huntercc336182015-07-17 19:33:57 +0300649 bool from_mtc;
650 double cbr_cyc_to_tsc;
651};
652
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300653/*
654 * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
655 * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
656 * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
657 * packet by copying the missing bits from the current MTC assuming the least
658 * difference between the two, and that the current MTC comes after last_mtc.
659 */
660static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
661 uint32_t *last_mtc)
662{
663 uint32_t first_missing_bit = 1U << (16 - mtc_shift);
664 uint32_t mask = ~(first_missing_bit - 1);
665
666 *last_mtc |= mtc & mask;
667 if (*last_mtc >= mtc) {
668 *last_mtc -= first_missing_bit;
669 *last_mtc &= 0xff;
670 }
671}
672
Adrian Huntercc336182015-07-17 19:33:57 +0300673static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
674{
675 struct intel_pt_decoder *decoder = pkt_info->decoder;
676 struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
677 uint64_t timestamp;
678 double cyc_to_tsc;
679 unsigned int cbr;
680 uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
681
682 switch (pkt_info->packet.type) {
683 case INTEL_PT_TNT:
684 case INTEL_PT_TIP_PGE:
685 case INTEL_PT_TIP:
686 case INTEL_PT_FUP:
687 case INTEL_PT_PSB:
688 case INTEL_PT_PIP:
689 case INTEL_PT_MODE_EXEC:
690 case INTEL_PT_MODE_TSX:
691 case INTEL_PT_PSBEND:
692 case INTEL_PT_PAD:
693 case INTEL_PT_VMCS:
694 case INTEL_PT_MNT:
Adrian Huntera472e652017-05-26 11:17:14 +0300695 case INTEL_PT_PTWRITE:
696 case INTEL_PT_PTWRITE_IP:
Adrian Huntercc336182015-07-17 19:33:57 +0300697 return 0;
698
699 case INTEL_PT_MTC:
700 if (!data->have_tma)
701 return 0;
702
703 mtc = pkt_info->packet.payload;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300704 if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
705 data->fixup_last_mtc = false;
706 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
707 &data->last_mtc);
708 }
Adrian Huntercc336182015-07-17 19:33:57 +0300709 if (mtc > data->last_mtc)
710 mtc_delta = mtc - data->last_mtc;
711 else
712 mtc_delta = mtc + 256 - data->last_mtc;
713 data->ctc_delta += mtc_delta << decoder->mtc_shift;
714 data->last_mtc = mtc;
715
716 if (decoder->tsc_ctc_mult) {
717 timestamp = data->ctc_timestamp +
718 data->ctc_delta * decoder->tsc_ctc_mult;
719 } else {
720 timestamp = data->ctc_timestamp +
721 multdiv(data->ctc_delta,
722 decoder->tsc_ctc_ratio_n,
723 decoder->tsc_ctc_ratio_d);
724 }
725
726 if (timestamp < data->timestamp)
727 return 1;
728
729 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
730 data->timestamp = timestamp;
731 return 0;
732 }
733
734 break;
735
736 case INTEL_PT_TSC:
Adrian Hunter38b65b02017-05-26 11:17:37 +0300737 /*
738 * For now, do not support using TSC packets - refer
739 * intel_pt_calc_cyc_to_tsc().
740 */
741 if (data->from_mtc)
742 return 1;
Adrian Huntercc336182015-07-17 19:33:57 +0300743 timestamp = pkt_info->packet.payload |
744 (data->timestamp & (0xffULL << 56));
745 if (data->from_mtc && timestamp < data->timestamp &&
746 data->timestamp - timestamp < decoder->tsc_slip)
747 return 1;
Adrian Hunter9992c2d2015-09-25 16:15:34 +0300748 if (timestamp < data->timestamp)
Adrian Huntercc336182015-07-17 19:33:57 +0300749 timestamp += (1ULL << 56);
750 if (pkt_info->last_packet_type != INTEL_PT_CYC) {
751 if (data->from_mtc)
752 return 1;
753 data->tsc_timestamp = timestamp;
754 data->timestamp = timestamp;
755 return 0;
756 }
757 break;
758
759 case INTEL_PT_TMA:
760 if (data->from_mtc)
761 return 1;
762
763 if (!decoder->tsc_ctc_ratio_d)
764 return 0;
765
766 ctc = pkt_info->packet.payload;
767 fc = pkt_info->packet.count;
768 ctc_rem = ctc & decoder->ctc_rem_mask;
769
770 data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
771
772 data->ctc_timestamp = data->tsc_timestamp - fc;
773 if (decoder->tsc_ctc_mult) {
774 data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
775 } else {
776 data->ctc_timestamp -=
777 multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
778 decoder->tsc_ctc_ratio_d);
779 }
780
781 data->ctc_delta = 0;
782 data->have_tma = true;
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300783 data->fixup_last_mtc = true;
Adrian Huntercc336182015-07-17 19:33:57 +0300784
785 return 0;
786
787 case INTEL_PT_CYC:
788 data->cycle_cnt += pkt_info->packet.payload;
789 return 0;
790
791 case INTEL_PT_CBR:
792 cbr = pkt_info->packet.payload;
793 if (data->cbr && data->cbr != cbr)
794 return 1;
795 data->cbr = cbr;
796 data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
797 return 0;
798
799 case INTEL_PT_TIP_PGD:
800 case INTEL_PT_TRACESTOP:
Adrian Huntera472e652017-05-26 11:17:14 +0300801 case INTEL_PT_EXSTOP:
802 case INTEL_PT_EXSTOP_IP:
803 case INTEL_PT_MWAIT:
804 case INTEL_PT_PWRE:
805 case INTEL_PT_PWRX:
Adrian Huntercc336182015-07-17 19:33:57 +0300806 case INTEL_PT_OVF:
807 case INTEL_PT_BAD: /* Does not happen */
808 default:
809 return 1;
810 }
811
812 if (!data->cbr && decoder->cbr) {
813 data->cbr = decoder->cbr;
814 data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
815 }
816
817 if (!data->cycle_cnt)
818 return 1;
819
820 cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
821
822 if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
823 cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
824 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
825 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
826 return 1;
827 }
828
829 decoder->calc_cyc_to_tsc = cyc_to_tsc;
830 decoder->have_calc_cyc_to_tsc = true;
831
832 if (data->cbr) {
833 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
834 cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
835 } else {
836 intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
837 cyc_to_tsc, pkt_info->pos);
838 }
839
840 return 1;
841}
842
843static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
844 bool from_mtc)
845{
846 struct intel_pt_calc_cyc_to_tsc_info data = {
847 .cycle_cnt = 0,
848 .cbr = 0,
849 .last_mtc = decoder->last_mtc,
850 .ctc_timestamp = decoder->ctc_timestamp,
851 .ctc_delta = decoder->ctc_delta,
852 .tsc_timestamp = decoder->tsc_timestamp,
853 .timestamp = decoder->timestamp,
854 .have_tma = decoder->have_tma,
Adrian Hunter3bccbe22016-09-28 14:41:36 +0300855 .fixup_last_mtc = decoder->fixup_last_mtc,
Adrian Huntercc336182015-07-17 19:33:57 +0300856 .from_mtc = from_mtc,
857 .cbr_cyc_to_tsc = 0,
858 };
859
Adrian Hunter38b65b02017-05-26 11:17:37 +0300860 /*
861 * For now, do not support using TSC packets for at least the reasons:
862 * 1) timing might have stopped
863 * 2) TSC packets within PSB+ can slip against CYC packets
864 */
865 if (!from_mtc)
866 return;
867
Adrian Huntercc336182015-07-17 19:33:57 +0300868 intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
869}
870
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300871static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
872{
873 int ret;
874
Adrian Huntercc336182015-07-17 19:33:57 +0300875 decoder->last_packet_type = decoder->packet.type;
876
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300877 do {
878 decoder->pos += decoder->pkt_step;
879 decoder->buf += decoder->pkt_step;
880 decoder->len -= decoder->pkt_step;
881
882 if (!decoder->len) {
Adrian Hunter6c1f0b12019-06-04 16:00:05 +0300883 ret = intel_pt_get_next_data(decoder, false);
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300884 if (ret)
885 return ret;
886 }
887
888 ret = intel_pt_get_packet(decoder->buf, decoder->len,
889 &decoder->packet);
Adrian Hunter26ee2bc2019-02-06 12:39:46 +0200890 if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300891 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
892 ret = intel_pt_get_split_packet(decoder);
893 if (ret < 0)
894 return ret;
895 }
896 if (ret <= 0)
897 return intel_pt_bad_packet(decoder);
898
899 decoder->pkt_len = ret;
900 decoder->pkt_step = ret;
901 intel_pt_decoder_log_packet(decoder);
902 } while (decoder->packet.type == INTEL_PT_PAD);
903
904 return 0;
905}
906
907static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
908{
909 uint64_t timestamp, masked_timestamp;
910
911 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
912 masked_timestamp = timestamp & decoder->period_mask;
913 if (decoder->continuous_period) {
Adrian Hunter7ba8fa22019-05-10 15:41:41 +0300914 if (masked_timestamp > decoder->last_masked_timestamp)
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300915 return 1;
916 } else {
917 timestamp += 1;
918 masked_timestamp = timestamp & decoder->period_mask;
Adrian Hunter7ba8fa22019-05-10 15:41:41 +0300919 if (masked_timestamp > decoder->last_masked_timestamp) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300920 decoder->last_masked_timestamp = masked_timestamp;
921 decoder->continuous_period = true;
922 }
923 }
Adrian Hunter7ba8fa22019-05-10 15:41:41 +0300924
925 if (masked_timestamp < decoder->last_masked_timestamp)
926 return decoder->period_ticks;
927
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300928 return decoder->period_ticks - (timestamp - masked_timestamp);
929}
930
931static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
932{
933 switch (decoder->period_type) {
934 case INTEL_PT_PERIOD_INSTRUCTIONS:
935 return decoder->period - decoder->period_insn_cnt;
936 case INTEL_PT_PERIOD_TICKS:
937 return intel_pt_next_period(decoder);
938 case INTEL_PT_PERIOD_NONE:
Adrian Hunter79b58422015-07-17 19:33:55 +0300939 case INTEL_PT_PERIOD_MTC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300940 default:
941 return 0;
942 }
943}
944
945static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
946{
947 uint64_t timestamp, masked_timestamp;
948
949 switch (decoder->period_type) {
950 case INTEL_PT_PERIOD_INSTRUCTIONS:
951 decoder->period_insn_cnt = 0;
952 break;
953 case INTEL_PT_PERIOD_TICKS:
954 timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
955 masked_timestamp = timestamp & decoder->period_mask;
Adrian Hunter7ba8fa22019-05-10 15:41:41 +0300956 if (masked_timestamp > decoder->last_masked_timestamp)
957 decoder->last_masked_timestamp = masked_timestamp;
958 else
959 decoder->last_masked_timestamp += decoder->period_ticks;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300960 break;
961 case INTEL_PT_PERIOD_NONE:
Adrian Hunter79b58422015-07-17 19:33:55 +0300962 case INTEL_PT_PERIOD_MTC:
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300963 default:
964 break;
965 }
966
967 decoder->state.type |= INTEL_PT_INSTRUCTION;
968}
969
970static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
971 struct intel_pt_insn *intel_pt_insn, uint64_t ip)
972{
973 uint64_t max_insn_cnt, insn_cnt = 0;
974 int err;
975
Adrian Hunter79b58422015-07-17 19:33:55 +0300976 if (!decoder->mtc_insn)
977 decoder->mtc_insn = true;
978
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300979 max_insn_cnt = intel_pt_next_sample(decoder);
980
981 err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
982 max_insn_cnt, decoder->data);
983
Adrian Hunter2a21d032015-07-17 19:33:48 +0300984 decoder->tot_insn_cnt += insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300985 decoder->timestamp_insn_cnt += insn_cnt;
Adrian Hunter3f04d982017-05-26 11:17:03 +0300986 decoder->sample_insn_cnt += insn_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +0300987 decoder->period_insn_cnt += insn_cnt;
988
989 if (err) {
990 decoder->no_progress = 0;
991 decoder->pkt_state = INTEL_PT_STATE_ERR2;
992 intel_pt_log_at("ERROR: Failed to get instruction",
993 decoder->ip);
994 if (err == -ENOENT)
995 return -ENOLINK;
996 return -EILSEQ;
997 }
998
999 if (ip && decoder->ip == ip) {
1000 err = -EAGAIN;
1001 goto out;
1002 }
1003
1004 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
1005 intel_pt_sample_insn(decoder);
1006
1007 if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
1008 decoder->state.type = INTEL_PT_INSTRUCTION;
1009 decoder->state.from_ip = decoder->ip;
1010 decoder->state.to_ip = 0;
1011 decoder->ip += intel_pt_insn->length;
1012 err = INTEL_PT_RETURN;
1013 goto out;
1014 }
1015
1016 if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
1017 /* Zero-length calls are excluded */
1018 if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
1019 intel_pt_insn->rel) {
1020 err = intel_pt_push(&decoder->stack, decoder->ip +
1021 intel_pt_insn->length);
1022 if (err)
1023 goto out;
1024 }
1025 } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
1026 decoder->ret_addr = intel_pt_pop(&decoder->stack);
1027 }
1028
1029 if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
1030 int cnt = decoder->no_progress++;
1031
1032 decoder->state.from_ip = decoder->ip;
1033 decoder->ip += intel_pt_insn->length +
1034 intel_pt_insn->rel;
1035 decoder->state.to_ip = decoder->ip;
1036 err = INTEL_PT_RETURN;
1037
1038 /*
1039 * Check for being stuck in a loop. This can happen if a
1040 * decoder error results in the decoder erroneously setting the
1041 * ip to an address that is itself in an infinite loop that
1042 * consumes no packets. When that happens, there must be an
1043 * unconditional branch.
1044 */
1045 if (cnt) {
1046 if (cnt == 1) {
1047 decoder->stuck_ip = decoder->state.to_ip;
1048 decoder->stuck_ip_prd = 1;
1049 decoder->stuck_ip_cnt = 1;
1050 } else if (cnt > INTEL_PT_MAX_LOOPS ||
1051 decoder->state.to_ip == decoder->stuck_ip) {
1052 intel_pt_log_at("ERROR: Never-ending loop",
1053 decoder->state.to_ip);
1054 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1055 err = -ELOOP;
1056 goto out;
1057 } else if (!--decoder->stuck_ip_cnt) {
1058 decoder->stuck_ip_prd += 1;
1059 decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
1060 decoder->stuck_ip = decoder->state.to_ip;
1061 }
1062 }
1063 goto out_no_progress;
1064 }
1065out:
1066 decoder->no_progress = 0;
1067out_no_progress:
1068 decoder->state.insn_op = intel_pt_insn->op;
1069 decoder->state.insn_len = intel_pt_insn->length;
Andi Kleenfaaa8762016-10-07 16:42:26 +03001070 memcpy(decoder->state.insn, intel_pt_insn->buf,
1071 INTEL_PT_INSN_BUF_SZ);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001072
1073 if (decoder->tx_flags & INTEL_PT_IN_TX)
1074 decoder->state.flags |= INTEL_PT_IN_TX;
1075
1076 return err;
1077}
1078
Adrian Huntera472e652017-05-26 11:17:14 +03001079static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
1080{
1081 bool ret = false;
1082
1083 if (decoder->set_fup_tx_flags) {
1084 decoder->set_fup_tx_flags = false;
1085 decoder->tx_flags = decoder->fup_tx_flags;
1086 decoder->state.type = INTEL_PT_TRANSACTION;
1087 decoder->state.from_ip = decoder->ip;
1088 decoder->state.to_ip = 0;
1089 decoder->state.flags = decoder->fup_tx_flags;
1090 return true;
1091 }
1092 if (decoder->set_fup_ptw) {
1093 decoder->set_fup_ptw = false;
1094 decoder->state.type = INTEL_PT_PTW;
1095 decoder->state.flags |= INTEL_PT_FUP_IP;
1096 decoder->state.from_ip = decoder->ip;
1097 decoder->state.to_ip = 0;
1098 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1099 return true;
1100 }
1101 if (decoder->set_fup_mwait) {
1102 decoder->set_fup_mwait = false;
1103 decoder->state.type = INTEL_PT_MWAIT_OP;
1104 decoder->state.from_ip = decoder->ip;
1105 decoder->state.to_ip = 0;
1106 decoder->state.mwait_payload = decoder->fup_mwait_payload;
1107 ret = true;
1108 }
1109 if (decoder->set_fup_pwre) {
1110 decoder->set_fup_pwre = false;
1111 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1112 decoder->state.type &= ~INTEL_PT_BRANCH;
1113 decoder->state.from_ip = decoder->ip;
1114 decoder->state.to_ip = 0;
1115 decoder->state.pwre_payload = decoder->fup_pwre_payload;
1116 ret = true;
1117 }
1118 if (decoder->set_fup_exstop) {
1119 decoder->set_fup_exstop = false;
1120 decoder->state.type |= INTEL_PT_EX_STOP;
1121 decoder->state.type &= ~INTEL_PT_BRANCH;
1122 decoder->state.flags |= INTEL_PT_FUP_IP;
1123 decoder->state.from_ip = decoder->ip;
1124 decoder->state.to_ip = 0;
1125 ret = true;
1126 }
1127 return ret;
1128}
1129
Adrian Hunter9fb52332018-05-31 13:23:45 +03001130static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
1131 struct intel_pt_insn *intel_pt_insn,
1132 uint64_t ip, int err)
1133{
1134 return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
1135 intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
1136 ip == decoder->ip + intel_pt_insn->length;
1137}
1138
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001139static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
1140{
1141 struct intel_pt_insn intel_pt_insn;
1142 uint64_t ip;
1143 int err;
1144
1145 ip = decoder->last_ip;
1146
1147 while (1) {
1148 err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
1149 if (err == INTEL_PT_RETURN)
1150 return 0;
Adrian Hunter9fb52332018-05-31 13:23:45 +03001151 if (err == -EAGAIN ||
1152 intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
Adrian Huntera472e652017-05-26 11:17:14 +03001153 if (intel_pt_fup_event(decoder))
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001154 return 0;
Adrian Hunter9fb52332018-05-31 13:23:45 +03001155 return -EAGAIN;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001156 }
1157 decoder->set_fup_tx_flags = false;
1158 if (err)
1159 return err;
1160
1161 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1162 intel_pt_log_at("ERROR: Unexpected indirect branch",
1163 decoder->ip);
1164 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1165 return -ENOENT;
1166 }
1167
1168 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1169 intel_pt_log_at("ERROR: Unexpected conditional branch",
1170 decoder->ip);
1171 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1172 return -ENOENT;
1173 }
1174
1175 intel_pt_bug(decoder);
1176 }
1177}
1178
1179static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
1180{
1181 struct intel_pt_insn intel_pt_insn;
1182 int err;
1183
1184 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001185 if (err == INTEL_PT_RETURN &&
1186 decoder->pgd_ip &&
1187 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1188 (decoder->state.type & INTEL_PT_BRANCH) &&
1189 decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
1190 /* Unconditional branch leaving filter region */
1191 decoder->no_progress = 0;
1192 decoder->pge = false;
1193 decoder->continuous_period = false;
1194 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
Adrian Hunterbea63852018-09-20 16:00:48 +03001195 decoder->state.type |= INTEL_PT_TRACE_END;
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001196 return 0;
1197 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001198 if (err == INTEL_PT_RETURN)
1199 return 0;
1200 if (err)
1201 return err;
1202
1203 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1204 if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
1205 decoder->pge = false;
1206 decoder->continuous_period = false;
1207 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1208 decoder->state.from_ip = decoder->ip;
Adrian Hunterbea63852018-09-20 16:00:48 +03001209 if (decoder->packet.count == 0) {
1210 decoder->state.to_ip = 0;
1211 } else {
1212 decoder->state.to_ip = decoder->last_ip;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001213 decoder->ip = decoder->last_ip;
Adrian Hunterbea63852018-09-20 16:00:48 +03001214 }
1215 decoder->state.type |= INTEL_PT_TRACE_END;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001216 } else {
1217 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1218 decoder->state.from_ip = decoder->ip;
1219 if (decoder->packet.count == 0) {
1220 decoder->state.to_ip = 0;
1221 } else {
1222 decoder->state.to_ip = decoder->last_ip;
1223 decoder->ip = decoder->last_ip;
1224 }
1225 }
1226 return 0;
1227 }
1228
1229 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001230 uint64_t to_ip = decoder->ip + intel_pt_insn.length +
1231 intel_pt_insn.rel;
1232
1233 if (decoder->pgd_ip &&
1234 decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
1235 decoder->pgd_ip(to_ip, decoder->data)) {
1236 /* Conditional branch leaving filter region */
1237 decoder->pge = false;
1238 decoder->continuous_period = false;
1239 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1240 decoder->ip = to_ip;
1241 decoder->state.from_ip = decoder->ip;
Adrian Hunterbea63852018-09-20 16:00:48 +03001242 decoder->state.to_ip = to_ip;
1243 decoder->state.type |= INTEL_PT_TRACE_END;
Adrian Hunter9f1d1222016-09-23 17:38:47 +03001244 return 0;
1245 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001246 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
1247 decoder->ip);
1248 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1249 return -ENOENT;
1250 }
1251
1252 return intel_pt_bug(decoder);
1253}
1254
1255static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
1256{
1257 struct intel_pt_insn intel_pt_insn;
1258 int err;
1259
1260 while (1) {
1261 err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
1262 if (err == INTEL_PT_RETURN)
1263 return 0;
1264 if (err)
1265 return err;
1266
1267 if (intel_pt_insn.op == INTEL_PT_OP_RET) {
1268 if (!decoder->return_compression) {
1269 intel_pt_log_at("ERROR: RET when expecting conditional branch",
1270 decoder->ip);
1271 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1272 return -ENOENT;
1273 }
1274 if (!decoder->ret_addr) {
1275 intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
1276 decoder->ip);
1277 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1278 return -ENOENT;
1279 }
1280 if (!(decoder->tnt.payload & BIT63)) {
1281 intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
1282 decoder->ip);
1283 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1284 return -ENOENT;
1285 }
1286 decoder->tnt.count -= 1;
Adrian Hunter61b6e082019-05-10 15:41:42 +03001287 if (decoder->tnt.count)
1288 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1289 else
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001290 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1291 decoder->tnt.payload <<= 1;
1292 decoder->state.from_ip = decoder->ip;
1293 decoder->ip = decoder->ret_addr;
1294 decoder->state.to_ip = decoder->ip;
1295 return 0;
1296 }
1297
1298 if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
1299 /* Handle deferred TIPs */
1300 err = intel_pt_get_next_packet(decoder);
1301 if (err)
1302 return err;
1303 if (decoder->packet.type != INTEL_PT_TIP ||
1304 decoder->packet.count == 0) {
1305 intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
1306 decoder->ip);
1307 decoder->pkt_state = INTEL_PT_STATE_ERR3;
1308 decoder->pkt_step = 0;
1309 return -ENOENT;
1310 }
1311 intel_pt_set_last_ip(decoder);
1312 decoder->state.from_ip = decoder->ip;
1313 decoder->state.to_ip = decoder->last_ip;
1314 decoder->ip = decoder->last_ip;
1315 return 0;
1316 }
1317
1318 if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
1319 decoder->tnt.count -= 1;
Adrian Hunter61b6e082019-05-10 15:41:42 +03001320 if (decoder->tnt.count)
1321 decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
1322 else
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001323 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1324 if (decoder->tnt.payload & BIT63) {
1325 decoder->tnt.payload <<= 1;
1326 decoder->state.from_ip = decoder->ip;
1327 decoder->ip += intel_pt_insn.length +
1328 intel_pt_insn.rel;
1329 decoder->state.to_ip = decoder->ip;
1330 return 0;
1331 }
1332 /* Instruction sample for a non-taken branch */
1333 if (decoder->state.type & INTEL_PT_INSTRUCTION) {
1334 decoder->tnt.payload <<= 1;
1335 decoder->state.type = INTEL_PT_INSTRUCTION;
1336 decoder->state.from_ip = decoder->ip;
1337 decoder->state.to_ip = 0;
1338 decoder->ip += intel_pt_insn.length;
1339 return 0;
1340 }
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03001341 decoder->sample_cyc = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001342 decoder->ip += intel_pt_insn.length;
Adrian Hunter1b6599a2019-05-10 15:41:43 +03001343 if (!decoder->tnt.count) {
Adrian Hunter948e9dc2019-05-20 14:37:10 +03001344 intel_pt_update_sample_time(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001345 return -EAGAIN;
Adrian Hunter1b6599a2019-05-10 15:41:43 +03001346 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001347 decoder->tnt.payload <<= 1;
1348 continue;
1349 }
1350
1351 return intel_pt_bug(decoder);
1352 }
1353}
1354
1355static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
1356{
1357 unsigned int fup_tx_flags;
1358 int err;
1359
1360 fup_tx_flags = decoder->packet.payload &
1361 (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
1362 err = intel_pt_get_next_packet(decoder);
1363 if (err)
1364 return err;
1365 if (decoder->packet.type == INTEL_PT_FUP) {
1366 decoder->fup_tx_flags = fup_tx_flags;
1367 decoder->set_fup_tx_flags = true;
1368 if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
1369 *no_tip = true;
1370 } else {
1371 intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
1372 decoder->pos);
1373 intel_pt_update_in_tx(decoder);
1374 }
1375 return 0;
1376}
1377
Adrian Huntere72b52a2019-06-04 16:00:03 +03001378static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
1379{
1380 timestamp |= (ref_timestamp & (0xffULL << 56));
1381
1382 if (timestamp < ref_timestamp) {
1383 if (ref_timestamp - timestamp > (1ULL << 55))
1384 timestamp += (1ULL << 56);
1385 } else {
1386 if (timestamp - ref_timestamp > (1ULL << 55))
1387 timestamp -= (1ULL << 56);
1388 }
1389
1390 return timestamp;
1391}
1392
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001393static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
1394{
1395 uint64_t timestamp;
1396
Adrian Hunter79b58422015-07-17 19:33:55 +03001397 decoder->have_tma = false;
1398
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001399 if (decoder->ref_timestamp) {
Adrian Huntere72b52a2019-06-04 16:00:03 +03001400 timestamp = intel_pt_8b_tsc(decoder->packet.payload,
1401 decoder->ref_timestamp);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001402 decoder->tsc_timestamp = timestamp;
1403 decoder->timestamp = timestamp;
1404 decoder->ref_timestamp = 0;
1405 decoder->timestamp_insn_cnt = 0;
1406 } else if (decoder->timestamp) {
1407 timestamp = decoder->packet.payload |
1408 (decoder->timestamp & (0xffULL << 56));
Adrian Hunter79b58422015-07-17 19:33:55 +03001409 decoder->tsc_timestamp = timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001410 if (timestamp < decoder->timestamp &&
Adrian Hunter79b58422015-07-17 19:33:55 +03001411 decoder->timestamp - timestamp < decoder->tsc_slip) {
1412 intel_pt_log_to("Suppressing backwards timestamp",
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001413 timestamp);
1414 timestamp = decoder->timestamp;
1415 }
Adrian Hunter9992c2d2015-09-25 16:15:34 +03001416 if (timestamp < decoder->timestamp) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001417 intel_pt_log_to("Wraparound timestamp", timestamp);
1418 timestamp += (1ULL << 56);
Adrian Hunter79b58422015-07-17 19:33:55 +03001419 decoder->tsc_timestamp = timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001420 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001421 decoder->timestamp = timestamp;
1422 decoder->timestamp_insn_cnt = 0;
1423 }
1424
Adrian Huntercc336182015-07-17 19:33:57 +03001425 if (decoder->last_packet_type == INTEL_PT_CYC) {
1426 decoder->cyc_ref_timestamp = decoder->timestamp;
1427 decoder->cycle_cnt = 0;
1428 decoder->have_calc_cyc_to_tsc = false;
1429 intel_pt_calc_cyc_to_tsc(decoder, false);
1430 }
1431
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001432 intel_pt_log_to("Setting timestamp", decoder->timestamp);
1433}
1434
1435static int intel_pt_overflow(struct intel_pt_decoder *decoder)
1436{
1437 intel_pt_log("ERROR: Buffer overflow\n");
1438 intel_pt_clear_tx_flags(decoder);
Adrian Hunter91d29b22018-03-07 16:02:24 +02001439 decoder->timestamp_insn_cnt = 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001440 decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1441 decoder->overflow = true;
1442 return -EOVERFLOW;
1443}
1444
Adrian Hunter3f055162019-05-20 14:37:17 +03001445static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
1446{
1447 if (decoder->have_cyc)
1448 return;
1449
1450 decoder->cyc_cnt_timestamp = decoder->timestamp;
1451 decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
1452}
1453
1454static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
1455{
1456 decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
1457
1458 if (decoder->pge)
1459 intel_pt_mtc_cyc_cnt_pge(decoder);
1460}
1461
1462static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
1463{
1464 uint64_t tot_cyc_cnt, tsc_delta;
1465
1466 if (decoder->have_cyc)
1467 return;
1468
1469 decoder->sample_cyc = true;
1470
1471 if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
1472 return;
1473
1474 tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
1475 tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
1476
1477 if (tot_cyc_cnt > decoder->tot_cyc_cnt)
1478 decoder->tot_cyc_cnt = tot_cyc_cnt;
1479}
1480
Adrian Hunter79b58422015-07-17 19:33:55 +03001481static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
1482{
1483 uint32_t ctc = decoder->packet.payload;
1484 uint32_t fc = decoder->packet.count;
1485 uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
1486
1487 if (!decoder->tsc_ctc_ratio_d)
1488 return;
1489
Adrian Hunter3f055162019-05-20 14:37:17 +03001490 if (decoder->pge && !decoder->in_psb)
1491 intel_pt_mtc_cyc_cnt_pge(decoder);
1492 else
1493 intel_pt_mtc_cyc_cnt_upd(decoder);
1494
Adrian Hunter79b58422015-07-17 19:33:55 +03001495 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
1496 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
1497 if (decoder->tsc_ctc_mult) {
1498 decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
1499 } else {
1500 decoder->ctc_timestamp -= multdiv(ctc_rem,
1501 decoder->tsc_ctc_ratio_n,
1502 decoder->tsc_ctc_ratio_d);
1503 }
1504 decoder->ctc_delta = 0;
1505 decoder->have_tma = true;
Adrian Hunter3bccbe22016-09-28 14:41:36 +03001506 decoder->fixup_last_mtc = true;
Adrian Hunter79b58422015-07-17 19:33:55 +03001507 intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
1508 decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
1509}
1510
1511static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
1512{
1513 uint64_t timestamp;
1514 uint32_t mtc, mtc_delta;
1515
1516 if (!decoder->have_tma)
1517 return;
1518
1519 mtc = decoder->packet.payload;
1520
Adrian Hunter3bccbe22016-09-28 14:41:36 +03001521 if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
1522 decoder->fixup_last_mtc = false;
1523 intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
1524 &decoder->last_mtc);
1525 }
1526
Adrian Hunter79b58422015-07-17 19:33:55 +03001527 if (mtc > decoder->last_mtc)
1528 mtc_delta = mtc - decoder->last_mtc;
1529 else
1530 mtc_delta = mtc + 256 - decoder->last_mtc;
1531
1532 decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
1533
1534 if (decoder->tsc_ctc_mult) {
1535 timestamp = decoder->ctc_timestamp +
1536 decoder->ctc_delta * decoder->tsc_ctc_mult;
1537 } else {
1538 timestamp = decoder->ctc_timestamp +
1539 multdiv(decoder->ctc_delta,
1540 decoder->tsc_ctc_ratio_n,
1541 decoder->tsc_ctc_ratio_d);
1542 }
1543
1544 if (timestamp < decoder->timestamp)
1545 intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1546 timestamp, decoder->timestamp);
1547 else
1548 decoder->timestamp = timestamp;
1549
Adrian Hunter3f055162019-05-20 14:37:17 +03001550 intel_pt_mtc_cyc_cnt_upd(decoder);
1551
Adrian Hunter79b58422015-07-17 19:33:55 +03001552 decoder->timestamp_insn_cnt = 0;
1553 decoder->last_mtc = mtc;
Adrian Huntercc336182015-07-17 19:33:57 +03001554
1555 if (decoder->last_packet_type == INTEL_PT_CYC) {
1556 decoder->cyc_ref_timestamp = decoder->timestamp;
1557 decoder->cycle_cnt = 0;
1558 decoder->have_calc_cyc_to_tsc = false;
1559 intel_pt_calc_cyc_to_tsc(decoder, true);
1560 }
Adrian Hunterf6c23e32018-11-05 09:35:05 +02001561
1562 intel_pt_log_to("Setting timestamp", decoder->timestamp);
Adrian Huntercc336182015-07-17 19:33:57 +03001563}
1564
1565static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
1566{
Adrian Hunter26fb2fb2017-05-26 11:17:15 +03001567 unsigned int cbr = decoder->packet.payload & 0xff;
Adrian Huntercc336182015-07-17 19:33:57 +03001568
Adrian Hunter0a7c700d2017-05-26 11:17:16 +03001569 decoder->cbr_payload = decoder->packet.payload;
1570
Adrian Huntercc336182015-07-17 19:33:57 +03001571 if (decoder->cbr == cbr)
1572 return;
1573
1574 decoder->cbr = cbr;
1575 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
Adrian Hunter3f055162019-05-20 14:37:17 +03001576
1577 intel_pt_mtc_cyc_cnt_cbr(decoder);
Adrian Huntercc336182015-07-17 19:33:57 +03001578}
1579
1580static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
1581{
1582 uint64_t timestamp = decoder->cyc_ref_timestamp;
1583
1584 decoder->have_cyc = true;
1585
1586 decoder->cycle_cnt += decoder->packet.payload;
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03001587 if (decoder->pge)
1588 decoder->tot_cyc_cnt += decoder->packet.payload;
1589 decoder->sample_cyc = true;
Adrian Huntercc336182015-07-17 19:33:57 +03001590
1591 if (!decoder->cyc_ref_timestamp)
1592 return;
1593
1594 if (decoder->have_calc_cyc_to_tsc)
1595 timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
1596 else if (decoder->cbr)
1597 timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
1598 else
1599 return;
1600
1601 if (timestamp < decoder->timestamp)
1602 intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
1603 timestamp, decoder->timestamp);
1604 else
1605 decoder->timestamp = timestamp;
Adrian Hunter51ee6482016-09-28 14:41:35 +03001606
1607 decoder->timestamp_insn_cnt = 0;
Adrian Hunterf6c23e32018-11-05 09:35:05 +02001608
1609 intel_pt_log_to("Setting timestamp", decoder->timestamp);
Adrian Hunter79b58422015-07-17 19:33:55 +03001610}
1611
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001612/* Walk PSB+ packets when already in sync. */
1613static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
1614{
1615 int err;
1616
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001617 decoder->in_psb = true;
1618
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001619 while (1) {
1620 err = intel_pt_get_next_packet(decoder);
1621 if (err)
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001622 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001623
1624 switch (decoder->packet.type) {
1625 case INTEL_PT_PSBEND:
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001626 err = 0;
1627 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001628
1629 case INTEL_PT_TIP_PGD:
1630 case INTEL_PT_TIP_PGE:
1631 case INTEL_PT_TIP:
1632 case INTEL_PT_TNT:
Adrian Hunter3d498072015-07-17 19:33:53 +03001633 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001634 case INTEL_PT_BAD:
1635 case INTEL_PT_PSB:
Adrian Huntera472e652017-05-26 11:17:14 +03001636 case INTEL_PT_PTWRITE:
1637 case INTEL_PT_PTWRITE_IP:
1638 case INTEL_PT_EXSTOP:
1639 case INTEL_PT_EXSTOP_IP:
1640 case INTEL_PT_MWAIT:
1641 case INTEL_PT_PWRE:
1642 case INTEL_PT_PWRX:
Adrian Hunter79b58422015-07-17 19:33:55 +03001643 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001644 intel_pt_log("ERROR: Unexpected packet\n");
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001645 err = -EAGAIN;
1646 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001647
1648 case INTEL_PT_OVF:
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001649 err = intel_pt_overflow(decoder);
1650 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001651
1652 case INTEL_PT_TSC:
1653 intel_pt_calc_tsc_timestamp(decoder);
1654 break;
1655
Adrian Hunter3d498072015-07-17 19:33:53 +03001656 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001657 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001658 break;
1659
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001660 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001661 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001662 break;
1663
1664 case INTEL_PT_MODE_EXEC:
1665 decoder->exec_mode = decoder->packet.payload;
1666 break;
1667
1668 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001669 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001670 break;
1671
1672 case INTEL_PT_FUP:
1673 decoder->pge = true;
Adrian Hunterf952eac2017-05-26 11:17:07 +03001674 if (decoder->packet.count)
1675 intel_pt_set_last_ip(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001676 break;
1677
1678 case INTEL_PT_MODE_TSX:
1679 intel_pt_update_in_tx(decoder);
1680 break;
1681
Adrian Hunter3d498072015-07-17 19:33:53 +03001682 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001683 intel_pt_calc_mtc_timestamp(decoder);
1684 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1685 decoder->state.type |= INTEL_PT_INSTRUCTION;
Adrian Hunter3d498072015-07-17 19:33:53 +03001686 break;
1687
1688 case INTEL_PT_CYC:
1689 case INTEL_PT_VMCS:
1690 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001691 case INTEL_PT_PAD:
1692 default:
1693 break;
1694 }
1695 }
Adrian Hunter9bc668e2019-05-20 14:37:15 +03001696out:
1697 decoder->in_psb = false;
1698
1699 return err;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001700}
1701
1702static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
1703{
1704 int err;
1705
1706 if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
1707 decoder->tx_flags = 0;
1708 decoder->state.flags &= ~INTEL_PT_IN_TX;
1709 decoder->state.flags |= INTEL_PT_ABORT_TX;
1710 } else {
1711 decoder->state.flags |= INTEL_PT_ASYNC;
1712 }
1713
1714 while (1) {
1715 err = intel_pt_get_next_packet(decoder);
1716 if (err)
1717 return err;
1718
1719 switch (decoder->packet.type) {
1720 case INTEL_PT_TNT:
1721 case INTEL_PT_FUP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001722 case INTEL_PT_TRACESTOP:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001723 case INTEL_PT_PSB:
1724 case INTEL_PT_TSC:
Adrian Hunter3d498072015-07-17 19:33:53 +03001725 case INTEL_PT_TMA:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001726 case INTEL_PT_MODE_TSX:
1727 case INTEL_PT_BAD:
1728 case INTEL_PT_PSBEND:
Adrian Huntera472e652017-05-26 11:17:14 +03001729 case INTEL_PT_PTWRITE:
1730 case INTEL_PT_PTWRITE_IP:
1731 case INTEL_PT_EXSTOP:
1732 case INTEL_PT_EXSTOP_IP:
1733 case INTEL_PT_MWAIT:
1734 case INTEL_PT_PWRE:
1735 case INTEL_PT_PWRX:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001736 intel_pt_log("ERROR: Missing TIP after FUP\n");
1737 decoder->pkt_state = INTEL_PT_STATE_ERR3;
Adrian Hunter1c196a62018-03-07 16:02:23 +02001738 decoder->pkt_step = 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001739 return -ENOENT;
1740
Adrian Hunterbd2e49e2018-05-31 13:23:43 +03001741 case INTEL_PT_CBR:
1742 intel_pt_calc_cbr(decoder);
1743 break;
1744
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001745 case INTEL_PT_OVF:
1746 return intel_pt_overflow(decoder);
1747
1748 case INTEL_PT_TIP_PGD:
1749 decoder->state.from_ip = decoder->ip;
Adrian Hunterbea63852018-09-20 16:00:48 +03001750 if (decoder->packet.count == 0) {
1751 decoder->state.to_ip = 0;
1752 } else {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001753 intel_pt_set_ip(decoder);
Adrian Hunterbea63852018-09-20 16:00:48 +03001754 decoder->state.to_ip = decoder->ip;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001755 }
1756 decoder->pge = false;
1757 decoder->continuous_period = false;
Adrian Hunterbea63852018-09-20 16:00:48 +03001758 decoder->state.type |= INTEL_PT_TRACE_END;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001759 return 0;
1760
1761 case INTEL_PT_TIP_PGE:
1762 decoder->pge = true;
1763 intel_pt_log("Omitting PGE ip " x64_fmt "\n",
1764 decoder->ip);
1765 decoder->state.from_ip = 0;
1766 if (decoder->packet.count == 0) {
1767 decoder->state.to_ip = 0;
1768 } else {
1769 intel_pt_set_ip(decoder);
1770 decoder->state.to_ip = decoder->ip;
1771 }
Adrian Hunterbea63852018-09-20 16:00:48 +03001772 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
Adrian Hunter3f055162019-05-20 14:37:17 +03001773 intel_pt_mtc_cyc_cnt_pge(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001774 return 0;
1775
1776 case INTEL_PT_TIP:
1777 decoder->state.from_ip = decoder->ip;
1778 if (decoder->packet.count == 0) {
1779 decoder->state.to_ip = 0;
1780 } else {
1781 intel_pt_set_ip(decoder);
1782 decoder->state.to_ip = decoder->ip;
1783 }
1784 return 0;
1785
1786 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001787 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1788 break;
1789
1790 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001791 intel_pt_calc_mtc_timestamp(decoder);
1792 if (decoder->period_type == INTEL_PT_PERIOD_MTC)
1793 decoder->state.type |= INTEL_PT_INSTRUCTION;
Adrian Hunter3d498072015-07-17 19:33:53 +03001794 break;
1795
1796 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001797 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001798 break;
1799
1800 case INTEL_PT_MODE_EXEC:
1801 decoder->exec_mode = decoder->packet.payload;
1802 break;
1803
Adrian Hunter3d498072015-07-17 19:33:53 +03001804 case INTEL_PT_VMCS:
1805 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001806 case INTEL_PT_PAD:
1807 break;
1808
1809 default:
1810 return intel_pt_bug(decoder);
1811 }
1812 }
1813}
1814
1815static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
1816{
1817 bool no_tip = false;
1818 int err;
1819
1820 while (1) {
1821 err = intel_pt_get_next_packet(decoder);
1822 if (err)
1823 return err;
1824next:
1825 switch (decoder->packet.type) {
1826 case INTEL_PT_TNT:
1827 if (!decoder->packet.count)
1828 break;
1829 decoder->tnt = decoder->packet;
1830 decoder->pkt_state = INTEL_PT_STATE_TNT;
1831 err = intel_pt_walk_tnt(decoder);
1832 if (err == -EAGAIN)
1833 break;
1834 return err;
1835
1836 case INTEL_PT_TIP_PGD:
1837 if (decoder->packet.count != 0)
1838 intel_pt_set_last_ip(decoder);
1839 decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
1840 return intel_pt_walk_tip(decoder);
1841
1842 case INTEL_PT_TIP_PGE: {
1843 decoder->pge = true;
Adrian Hunter3f055162019-05-20 14:37:17 +03001844 intel_pt_mtc_cyc_cnt_pge(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001845 if (decoder->packet.count == 0) {
1846 intel_pt_log_at("Skipping zero TIP.PGE",
1847 decoder->pos);
1848 break;
1849 }
1850 intel_pt_set_ip(decoder);
1851 decoder->state.from_ip = 0;
1852 decoder->state.to_ip = decoder->ip;
Adrian Hunterbea63852018-09-20 16:00:48 +03001853 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001854 return 0;
1855 }
1856
1857 case INTEL_PT_OVF:
1858 return intel_pt_overflow(decoder);
1859
1860 case INTEL_PT_TIP:
1861 if (decoder->packet.count != 0)
1862 intel_pt_set_last_ip(decoder);
1863 decoder->pkt_state = INTEL_PT_STATE_TIP;
1864 return intel_pt_walk_tip(decoder);
1865
1866 case INTEL_PT_FUP:
1867 if (decoder->packet.count == 0) {
1868 intel_pt_log_at("Skipping zero FUP",
1869 decoder->pos);
1870 no_tip = false;
1871 break;
1872 }
1873 intel_pt_set_last_ip(decoder);
Adrian Hunter83959812017-05-26 11:17:11 +03001874 if (!decoder->branch_enable) {
1875 decoder->ip = decoder->last_ip;
Adrian Huntera472e652017-05-26 11:17:14 +03001876 if (intel_pt_fup_event(decoder))
1877 return 0;
1878 no_tip = false;
Adrian Hunter83959812017-05-26 11:17:11 +03001879 break;
1880 }
Adrian Huntera472e652017-05-26 11:17:14 +03001881 if (decoder->set_fup_mwait)
1882 no_tip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001883 err = intel_pt_walk_fup(decoder);
1884 if (err != -EAGAIN) {
1885 if (err)
1886 return err;
1887 if (no_tip)
1888 decoder->pkt_state =
1889 INTEL_PT_STATE_FUP_NO_TIP;
1890 else
1891 decoder->pkt_state = INTEL_PT_STATE_FUP;
1892 return 0;
1893 }
1894 if (no_tip) {
1895 no_tip = false;
1896 break;
1897 }
1898 return intel_pt_walk_fup_tip(decoder);
1899
Adrian Hunter3d498072015-07-17 19:33:53 +03001900 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03001901 decoder->pge = false;
1902 decoder->continuous_period = false;
1903 intel_pt_clear_tx_flags(decoder);
1904 decoder->have_tma = false;
Adrian Hunter3d498072015-07-17 19:33:53 +03001905 break;
1906
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001907 case INTEL_PT_PSB:
Adrian Hunteree14ac02017-05-26 11:17:06 +03001908 decoder->last_ip = 0;
1909 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001910 intel_pt_clear_stack(&decoder->stack);
1911 err = intel_pt_walk_psbend(decoder);
1912 if (err == -EAGAIN)
1913 goto next;
1914 if (err)
1915 return err;
1916 break;
1917
1918 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03001919 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
1920 break;
1921
1922 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03001923 intel_pt_calc_mtc_timestamp(decoder);
1924 if (decoder->period_type != INTEL_PT_PERIOD_MTC)
1925 break;
1926 /*
1927 * Ensure that there has been an instruction since the
1928 * last MTC.
1929 */
1930 if (!decoder->mtc_insn)
1931 break;
1932 decoder->mtc_insn = false;
1933 /* Ensure that there is a timestamp */
1934 if (!decoder->timestamp)
1935 break;
1936 decoder->state.type = INTEL_PT_INSTRUCTION;
1937 decoder->state.from_ip = decoder->ip;
1938 decoder->state.to_ip = 0;
1939 decoder->mtc_insn = false;
1940 return 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001941
1942 case INTEL_PT_TSC:
1943 intel_pt_calc_tsc_timestamp(decoder);
1944 break;
1945
Adrian Hunter3d498072015-07-17 19:33:53 +03001946 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03001947 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001948 break;
1949
1950 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03001951 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03001952 break;
1953
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001954 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03001955 intel_pt_calc_cbr(decoder);
Adrian Hunter0a7c700d2017-05-26 11:17:16 +03001956 if (!decoder->branch_enable &&
1957 decoder->cbr != decoder->cbr_seen) {
1958 decoder->cbr_seen = decoder->cbr;
1959 decoder->state.type = INTEL_PT_CBR_CHG;
1960 decoder->state.from_ip = decoder->ip;
1961 decoder->state.to_ip = 0;
1962 decoder->state.cbr_payload =
1963 decoder->packet.payload;
1964 return 0;
1965 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001966 break;
1967
1968 case INTEL_PT_MODE_EXEC:
1969 decoder->exec_mode = decoder->packet.payload;
1970 break;
1971
1972 case INTEL_PT_MODE_TSX:
1973 /* MODE_TSX need not be followed by FUP */
1974 if (!decoder->pge) {
1975 intel_pt_update_in_tx(decoder);
1976 break;
1977 }
1978 err = intel_pt_mode_tsx(decoder, &no_tip);
1979 if (err)
1980 return err;
1981 goto next;
1982
1983 case INTEL_PT_BAD: /* Does not happen */
1984 return intel_pt_bug(decoder);
1985
1986 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03001987 case INTEL_PT_VMCS:
1988 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03001989 case INTEL_PT_PAD:
1990 break;
1991
Adrian Huntera472e652017-05-26 11:17:14 +03001992 case INTEL_PT_PTWRITE_IP:
1993 decoder->fup_ptw_payload = decoder->packet.payload;
1994 err = intel_pt_get_next_packet(decoder);
1995 if (err)
1996 return err;
1997 if (decoder->packet.type == INTEL_PT_FUP) {
1998 decoder->set_fup_ptw = true;
1999 no_tip = true;
2000 } else {
2001 intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
2002 decoder->pos);
2003 }
2004 goto next;
2005
2006 case INTEL_PT_PTWRITE:
2007 decoder->state.type = INTEL_PT_PTW;
2008 decoder->state.from_ip = decoder->ip;
2009 decoder->state.to_ip = 0;
2010 decoder->state.ptw_payload = decoder->packet.payload;
2011 return 0;
2012
2013 case INTEL_PT_MWAIT:
2014 decoder->fup_mwait_payload = decoder->packet.payload;
2015 decoder->set_fup_mwait = true;
2016 break;
2017
2018 case INTEL_PT_PWRE:
2019 if (decoder->set_fup_mwait) {
2020 decoder->fup_pwre_payload =
2021 decoder->packet.payload;
2022 decoder->set_fup_pwre = true;
2023 break;
2024 }
2025 decoder->state.type = INTEL_PT_PWR_ENTRY;
2026 decoder->state.from_ip = decoder->ip;
2027 decoder->state.to_ip = 0;
2028 decoder->state.pwrx_payload = decoder->packet.payload;
2029 return 0;
2030
2031 case INTEL_PT_EXSTOP_IP:
2032 err = intel_pt_get_next_packet(decoder);
2033 if (err)
2034 return err;
2035 if (decoder->packet.type == INTEL_PT_FUP) {
2036 decoder->set_fup_exstop = true;
2037 no_tip = true;
2038 } else {
2039 intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
2040 decoder->pos);
2041 }
2042 goto next;
2043
2044 case INTEL_PT_EXSTOP:
2045 decoder->state.type = INTEL_PT_EX_STOP;
2046 decoder->state.from_ip = decoder->ip;
2047 decoder->state.to_ip = 0;
2048 return 0;
2049
2050 case INTEL_PT_PWRX:
2051 decoder->state.type = INTEL_PT_PWR_EXIT;
2052 decoder->state.from_ip = decoder->ip;
2053 decoder->state.to_ip = 0;
2054 decoder->state.pwrx_payload = decoder->packet.payload;
2055 return 0;
2056
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002057 default:
2058 return intel_pt_bug(decoder);
2059 }
2060 }
2061}
2062
Adrian Huntere1717e02016-07-20 12:00:06 +03002063static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
2064{
Adrian Hunterf952eac2017-05-26 11:17:07 +03002065 return decoder->packet.count &&
2066 (decoder->have_last_ip || decoder->packet.count == 3 ||
2067 decoder->packet.count == 6);
Adrian Huntere1717e02016-07-20 12:00:06 +03002068}
2069
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002070/* Walk PSB+ packets to get in sync. */
2071static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
2072{
2073 int err;
2074
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002075 decoder->in_psb = true;
2076
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002077 while (1) {
2078 err = intel_pt_get_next_packet(decoder);
2079 if (err)
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002080 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002081
2082 switch (decoder->packet.type) {
2083 case INTEL_PT_TIP_PGD:
2084 decoder->continuous_period = false;
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -03002085 __fallthrough;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002086 case INTEL_PT_TIP_PGE:
2087 case INTEL_PT_TIP:
Adrian Huntera472e652017-05-26 11:17:14 +03002088 case INTEL_PT_PTWRITE:
2089 case INTEL_PT_PTWRITE_IP:
2090 case INTEL_PT_EXSTOP:
2091 case INTEL_PT_EXSTOP_IP:
2092 case INTEL_PT_MWAIT:
2093 case INTEL_PT_PWRE:
2094 case INTEL_PT_PWRX:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002095 intel_pt_log("ERROR: Unexpected packet\n");
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002096 err = -ENOENT;
2097 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002098
2099 case INTEL_PT_FUP:
2100 decoder->pge = true;
Adrian Huntere1717e02016-07-20 12:00:06 +03002101 if (intel_pt_have_ip(decoder)) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002102 uint64_t current_ip = decoder->ip;
2103
2104 intel_pt_set_ip(decoder);
2105 if (current_ip)
2106 intel_pt_log_to("Setting IP",
2107 decoder->ip);
2108 }
2109 break;
2110
Adrian Hunter3d498072015-07-17 19:33:53 +03002111 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03002112 intel_pt_calc_mtc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002113 break;
2114
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002115 case INTEL_PT_TSC:
2116 intel_pt_calc_tsc_timestamp(decoder);
2117 break;
2118
Adrian Hunter3d498072015-07-17 19:33:53 +03002119 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03002120 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002121 break;
2122
2123 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03002124 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002125 break;
2126
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002127 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03002128 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002129 break;
2130
2131 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03002132 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002133 break;
2134
2135 case INTEL_PT_MODE_EXEC:
2136 decoder->exec_mode = decoder->packet.payload;
2137 break;
2138
2139 case INTEL_PT_MODE_TSX:
2140 intel_pt_update_in_tx(decoder);
2141 break;
2142
Adrian Hunter3d498072015-07-17 19:33:53 +03002143 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03002144 decoder->pge = false;
2145 decoder->continuous_period = false;
2146 intel_pt_clear_tx_flags(decoder);
Arnaldo Carvalho de Melo7ea68562017-02-09 15:22:22 -03002147 __fallthrough;
2148
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002149 case INTEL_PT_TNT:
Adrian Hunter79b58422015-07-17 19:33:55 +03002150 decoder->have_tma = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002151 intel_pt_log("ERROR: Unexpected packet\n");
2152 if (decoder->ip)
2153 decoder->pkt_state = INTEL_PT_STATE_ERR4;
2154 else
2155 decoder->pkt_state = INTEL_PT_STATE_ERR3;
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002156 err = -ENOENT;
2157 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002158
2159 case INTEL_PT_BAD: /* Does not happen */
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002160 err = intel_pt_bug(decoder);
2161 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002162
2163 case INTEL_PT_OVF:
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002164 err = intel_pt_overflow(decoder);
2165 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002166
2167 case INTEL_PT_PSBEND:
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002168 err = 0;
2169 goto out;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002170
2171 case INTEL_PT_PSB:
Adrian Hunter3d498072015-07-17 19:33:53 +03002172 case INTEL_PT_VMCS:
2173 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002174 case INTEL_PT_PAD:
2175 default:
2176 break;
2177 }
2178 }
Adrian Hunter9bc668e2019-05-20 14:37:15 +03002179out:
2180 decoder->in_psb = false;
2181
2182 return err;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002183}
2184
2185static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
2186{
2187 int err;
2188
2189 while (1) {
2190 err = intel_pt_get_next_packet(decoder);
2191 if (err)
2192 return err;
2193
2194 switch (decoder->packet.type) {
2195 case INTEL_PT_TIP_PGD:
2196 decoder->continuous_period = false;
Adrian Hunterf3c98c42019-05-20 14:37:16 +03002197 decoder->pge = false;
Adrian Huntere1717e02016-07-20 12:00:06 +03002198 if (intel_pt_have_ip(decoder))
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002199 intel_pt_set_ip(decoder);
Adrian Hunterbea63852018-09-20 16:00:48 +03002200 if (!decoder->ip)
2201 break;
Adrian Hunterf3c98c42019-05-20 14:37:16 +03002202 decoder->state.type |= INTEL_PT_TRACE_END;
2203 return 0;
2204
2205 case INTEL_PT_TIP_PGE:
2206 decoder->pge = true;
Adrian Hunter3f055162019-05-20 14:37:17 +03002207 intel_pt_mtc_cyc_cnt_pge(decoder);
Adrian Hunterf3c98c42019-05-20 14:37:16 +03002208 if (intel_pt_have_ip(decoder))
2209 intel_pt_set_ip(decoder);
2210 if (!decoder->ip)
2211 break;
2212 decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2213 return 0;
2214
2215 case INTEL_PT_TIP:
2216 decoder->pge = true;
2217 if (intel_pt_have_ip(decoder))
2218 intel_pt_set_ip(decoder);
2219 if (!decoder->ip)
2220 break;
Adrian Hunterbea63852018-09-20 16:00:48 +03002221 return 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002222
2223 case INTEL_PT_FUP:
Adrian Hunter622b7a42017-05-26 11:17:08 +03002224 if (intel_pt_have_ip(decoder))
2225 intel_pt_set_ip(decoder);
2226 if (decoder->ip)
2227 return 0;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002228 break;
2229
Adrian Hunter3d498072015-07-17 19:33:53 +03002230 case INTEL_PT_MTC:
Adrian Hunter79b58422015-07-17 19:33:55 +03002231 intel_pt_calc_mtc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002232 break;
2233
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002234 case INTEL_PT_TSC:
2235 intel_pt_calc_tsc_timestamp(decoder);
2236 break;
2237
Adrian Hunter3d498072015-07-17 19:33:53 +03002238 case INTEL_PT_TMA:
Adrian Hunter79b58422015-07-17 19:33:55 +03002239 intel_pt_calc_tma(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002240 break;
2241
2242 case INTEL_PT_CYC:
Adrian Huntercc336182015-07-17 19:33:57 +03002243 intel_pt_calc_cyc_timestamp(decoder);
Adrian Hunter3d498072015-07-17 19:33:53 +03002244 break;
2245
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002246 case INTEL_PT_CBR:
Adrian Huntercc336182015-07-17 19:33:57 +03002247 intel_pt_calc_cbr(decoder);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002248 break;
2249
2250 case INTEL_PT_PIP:
Adrian Hunter3d498072015-07-17 19:33:53 +03002251 decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002252 break;
2253
2254 case INTEL_PT_MODE_EXEC:
2255 decoder->exec_mode = decoder->packet.payload;
2256 break;
2257
2258 case INTEL_PT_MODE_TSX:
2259 intel_pt_update_in_tx(decoder);
2260 break;
2261
2262 case INTEL_PT_OVF:
2263 return intel_pt_overflow(decoder);
2264
2265 case INTEL_PT_BAD: /* Does not happen */
2266 return intel_pt_bug(decoder);
2267
Adrian Hunter3d498072015-07-17 19:33:53 +03002268 case INTEL_PT_TRACESTOP:
Adrian Hunter7eacca32015-07-17 19:33:59 +03002269 decoder->pge = false;
2270 decoder->continuous_period = false;
2271 intel_pt_clear_tx_flags(decoder);
2272 decoder->have_tma = false;
Adrian Hunter3d498072015-07-17 19:33:53 +03002273 break;
2274
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002275 case INTEL_PT_PSB:
Adrian Hunteree14ac02017-05-26 11:17:06 +03002276 decoder->last_ip = 0;
2277 decoder->have_last_ip = true;
Adrian Hunter12b70802017-05-26 11:17:04 +03002278 intel_pt_clear_stack(&decoder->stack);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002279 err = intel_pt_walk_psb(decoder);
2280 if (err)
2281 return err;
2282 if (decoder->ip) {
2283 /* Do not have a sample */
2284 decoder->state.type = 0;
2285 return 0;
2286 }
2287 break;
2288
2289 case INTEL_PT_TNT:
2290 case INTEL_PT_PSBEND:
Adrian Hunter3d498072015-07-17 19:33:53 +03002291 case INTEL_PT_VMCS:
2292 case INTEL_PT_MNT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002293 case INTEL_PT_PAD:
Adrian Huntera472e652017-05-26 11:17:14 +03002294 case INTEL_PT_PTWRITE:
2295 case INTEL_PT_PTWRITE_IP:
2296 case INTEL_PT_EXSTOP:
2297 case INTEL_PT_EXSTOP_IP:
2298 case INTEL_PT_MWAIT:
2299 case INTEL_PT_PWRE:
2300 case INTEL_PT_PWRX:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002301 default:
2302 break;
2303 }
2304 }
2305}
2306
2307static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
2308{
2309 int err;
2310
Adrian Hunter6a558f12017-05-26 11:17:09 +03002311 decoder->set_fup_tx_flags = false;
Adrian Huntera472e652017-05-26 11:17:14 +03002312 decoder->set_fup_ptw = false;
2313 decoder->set_fup_mwait = false;
2314 decoder->set_fup_pwre = false;
2315 decoder->set_fup_exstop = false;
Adrian Hunter6a558f12017-05-26 11:17:09 +03002316
Adrian Hunter83959812017-05-26 11:17:11 +03002317 if (!decoder->branch_enable) {
2318 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2319 decoder->overflow = false;
2320 decoder->state.type = 0; /* Do not have a sample */
2321 return 0;
2322 }
2323
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002324 intel_pt_log("Scanning for full IP\n");
2325 err = intel_pt_walk_to_ip(decoder);
2326 if (err)
2327 return err;
2328
2329 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2330 decoder->overflow = false;
2331
2332 decoder->state.from_ip = 0;
2333 decoder->state.to_ip = decoder->ip;
2334 intel_pt_log_to("Setting IP", decoder->ip);
2335
2336 return 0;
2337}
2338
2339static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
2340{
2341 const unsigned char *end = decoder->buf + decoder->len;
2342 size_t i;
2343
2344 for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
2345 if (i > decoder->len)
2346 continue;
2347 if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
2348 return i;
2349 }
2350 return 0;
2351}
2352
2353static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
2354{
2355 size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
2356 const char *psb = INTEL_PT_PSB_STR;
2357
2358 if (rest_psb > decoder->len ||
2359 memcmp(decoder->buf, psb + part_psb, rest_psb))
2360 return 0;
2361
2362 return rest_psb;
2363}
2364
2365static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
2366 int part_psb)
2367{
2368 int rest_psb, ret;
2369
2370 decoder->pos += decoder->len;
2371 decoder->len = 0;
2372
Adrian Hunter6c1f0b12019-06-04 16:00:05 +03002373 ret = intel_pt_get_next_data(decoder, false);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002374 if (ret)
2375 return ret;
2376
2377 rest_psb = intel_pt_rest_psb(decoder, part_psb);
2378 if (!rest_psb)
2379 return 0;
2380
2381 decoder->pos -= part_psb;
2382 decoder->next_buf = decoder->buf + rest_psb;
2383 decoder->next_len = decoder->len - rest_psb;
2384 memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2385 decoder->buf = decoder->temp_buf;
2386 decoder->len = INTEL_PT_PSB_LEN;
2387
2388 return 0;
2389}
2390
2391static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
2392{
2393 unsigned char *next;
2394 int ret;
2395
2396 intel_pt_log("Scanning for PSB\n");
2397 while (1) {
2398 if (!decoder->len) {
Adrian Hunter6c1f0b12019-06-04 16:00:05 +03002399 ret = intel_pt_get_next_data(decoder, false);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002400 if (ret)
2401 return ret;
2402 }
2403
2404 next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
2405 INTEL_PT_PSB_LEN);
2406 if (!next) {
2407 int part_psb;
2408
2409 part_psb = intel_pt_part_psb(decoder);
2410 if (part_psb) {
2411 ret = intel_pt_get_split_psb(decoder, part_psb);
2412 if (ret)
2413 return ret;
2414 } else {
2415 decoder->pos += decoder->len;
2416 decoder->len = 0;
2417 }
2418 continue;
2419 }
2420
2421 decoder->pkt_step = next - decoder->buf;
2422 return intel_pt_get_next_packet(decoder);
2423 }
2424}
2425
2426static int intel_pt_sync(struct intel_pt_decoder *decoder)
2427{
2428 int err;
2429
2430 decoder->pge = false;
2431 decoder->continuous_period = false;
Adrian Hunteree14ac02017-05-26 11:17:06 +03002432 decoder->have_last_ip = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002433 decoder->last_ip = 0;
2434 decoder->ip = 0;
2435 intel_pt_clear_stack(&decoder->stack);
2436
2437 err = intel_pt_scan_for_psb(decoder);
2438 if (err)
2439 return err;
2440
Adrian Hunteree14ac02017-05-26 11:17:06 +03002441 decoder->have_last_ip = true;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002442 decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2443
2444 err = intel_pt_walk_psb(decoder);
2445 if (err)
2446 return err;
2447
2448 if (decoder->ip) {
2449 decoder->state.type = 0; /* Do not have a sample */
2450 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2451 } else {
2452 return intel_pt_sync_ip(decoder);
2453 }
2454
2455 return 0;
2456}
2457
2458static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
2459{
Adrian Hunter3f04d982017-05-26 11:17:03 +03002460 uint64_t est = decoder->sample_insn_cnt << 1;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002461
2462 if (!decoder->cbr || !decoder->max_non_turbo_ratio)
2463 goto out;
2464
2465 est *= decoder->max_non_turbo_ratio;
2466 est /= decoder->cbr;
2467out:
Adrian Hunter3f04d982017-05-26 11:17:03 +03002468 return decoder->sample_timestamp + est;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002469}
2470
2471const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
2472{
2473 int err;
2474
2475 do {
2476 decoder->state.type = INTEL_PT_BRANCH;
2477 decoder->state.flags = 0;
2478
2479 switch (decoder->pkt_state) {
2480 case INTEL_PT_STATE_NO_PSB:
2481 err = intel_pt_sync(decoder);
2482 break;
2483 case INTEL_PT_STATE_NO_IP:
Adrian Hunteree14ac02017-05-26 11:17:06 +03002484 decoder->have_last_ip = false;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002485 decoder->last_ip = 0;
Adrian Hunterad7167a2017-05-26 11:17:05 +03002486 decoder->ip = 0;
Adrian Hunter04194202017-05-26 11:17:10 +03002487 __fallthrough;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002488 case INTEL_PT_STATE_ERR_RESYNC:
2489 err = intel_pt_sync_ip(decoder);
2490 break;
2491 case INTEL_PT_STATE_IN_SYNC:
2492 err = intel_pt_walk_trace(decoder);
2493 break;
2494 case INTEL_PT_STATE_TNT:
Adrian Hunter61b6e082019-05-10 15:41:42 +03002495 case INTEL_PT_STATE_TNT_CONT:
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002496 err = intel_pt_walk_tnt(decoder);
2497 if (err == -EAGAIN)
2498 err = intel_pt_walk_trace(decoder);
2499 break;
2500 case INTEL_PT_STATE_TIP:
2501 case INTEL_PT_STATE_TIP_PGD:
2502 err = intel_pt_walk_tip(decoder);
2503 break;
2504 case INTEL_PT_STATE_FUP:
2505 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2506 err = intel_pt_walk_fup(decoder);
2507 if (err == -EAGAIN)
2508 err = intel_pt_walk_fup_tip(decoder);
2509 else if (!err)
2510 decoder->pkt_state = INTEL_PT_STATE_FUP;
2511 break;
2512 case INTEL_PT_STATE_FUP_NO_TIP:
2513 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2514 err = intel_pt_walk_fup(decoder);
2515 if (err == -EAGAIN)
2516 err = intel_pt_walk_trace(decoder);
2517 break;
2518 default:
2519 err = intel_pt_bug(decoder);
2520 break;
2521 }
2522 } while (err == -ENOLINK);
2523
Adrian Hunter22c06892017-05-26 11:17:02 +03002524 if (err) {
2525 decoder->state.err = intel_pt_ext_err(err);
2526 decoder->state.from_ip = decoder->ip;
Adrian Hunter948e9dc2019-05-20 14:37:10 +03002527 intel_pt_update_sample_time(decoder);
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03002528 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
Adrian Hunter22c06892017-05-26 11:17:02 +03002529 } else {
2530 decoder->state.err = 0;
Adrian Hunter0a7c700d2017-05-26 11:17:16 +03002531 if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2532 decoder->cbr_seen = decoder->cbr;
2533 decoder->state.type |= INTEL_PT_CBR_CHG;
2534 decoder->state.cbr_payload = decoder->cbr_payload;
2535 }
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03002536 if (intel_pt_sample_time(decoder->pkt_state)) {
Adrian Hunter948e9dc2019-05-20 14:37:10 +03002537 intel_pt_update_sample_time(decoder);
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03002538 if (decoder->sample_cyc)
2539 decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
2540 }
Adrian Hunter22c06892017-05-26 11:17:02 +03002541 }
2542
Adrian Hunter3f04d982017-05-26 11:17:03 +03002543 decoder->state.timestamp = decoder->sample_timestamp;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002544 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
2545 decoder->state.cr3 = decoder->cr3;
Adrian Hunter2a21d032015-07-17 19:33:48 +03002546 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
Adrian Hunter7b4b4f82019-05-20 14:37:11 +03002547 decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002548
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002549 return &decoder->state;
2550}
2551
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002552/**
2553 * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
2554 * @buf: pointer to buffer pointer
2555 * @len: size of buffer
2556 *
2557 * Updates the buffer pointer to point to the start of the next PSB packet if
2558 * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
2559 * @len is adjusted accordingly.
2560 *
2561 * Return: %true if a PSB packet is found, %false otherwise.
2562 */
2563static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
2564{
2565 unsigned char *next;
2566
2567 next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2568 if (next) {
2569 *len -= next - *buf;
2570 *buf = next;
2571 return true;
2572 }
2573 return false;
2574}
2575
2576/**
2577 * intel_pt_step_psb - move buffer pointer to the start of the following PSB
2578 * packet.
2579 * @buf: pointer to buffer pointer
2580 * @len: size of buffer
2581 *
2582 * Updates the buffer pointer to point to the start of the following PSB packet
2583 * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
2584 * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
2585 *
2586 * Return: %true if a PSB packet is found, %false otherwise.
2587 */
2588static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
2589{
2590 unsigned char *next;
2591
2592 if (!*len)
2593 return false;
2594
2595 next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
2596 if (next) {
2597 *len -= next - *buf;
2598 *buf = next;
2599 return true;
2600 }
2601 return false;
2602}
2603
2604/**
2605 * intel_pt_last_psb - find the last PSB packet in a buffer.
2606 * @buf: buffer
2607 * @len: size of buffer
2608 *
2609 * This function finds the last PSB in a buffer.
2610 *
2611 * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
2612 */
2613static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
2614{
2615 const char *n = INTEL_PT_PSB_STR;
2616 unsigned char *p;
2617 size_t k;
2618
2619 if (len < INTEL_PT_PSB_LEN)
2620 return NULL;
2621
2622 k = len - INTEL_PT_PSB_LEN + 1;
2623 while (1) {
2624 p = memrchr(buf, n[0], k);
2625 if (!p)
2626 return NULL;
2627 if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
2628 return p;
2629 k = p - buf;
2630 if (!k)
2631 return NULL;
2632 }
2633}
2634
2635/**
2636 * intel_pt_next_tsc - find and return next TSC.
2637 * @buf: buffer
2638 * @len: size of buffer
2639 * @tsc: TSC value returned
Adrian Hunter117db4b2018-03-07 16:02:21 +02002640 * @rem: returns remaining size when TSC is found
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002641 *
2642 * Find a TSC packet in @buf and return the TSC value. This function assumes
2643 * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
2644 * PSBEND packet is found.
2645 *
2646 * Return: %true if TSC is found, false otherwise.
2647 */
Adrian Hunter117db4b2018-03-07 16:02:21 +02002648static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
2649 size_t *rem)
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002650{
2651 struct intel_pt_pkt packet;
2652 int ret;
2653
2654 while (len) {
2655 ret = intel_pt_get_packet(buf, len, &packet);
2656 if (ret <= 0)
2657 return false;
2658 if (packet.type == INTEL_PT_TSC) {
2659 *tsc = packet.payload;
Adrian Hunter117db4b2018-03-07 16:02:21 +02002660 *rem = len;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002661 return true;
2662 }
2663 if (packet.type == INTEL_PT_PSBEND)
2664 return false;
2665 buf += ret;
2666 len -= ret;
2667 }
2668 return false;
2669}
2670
2671/**
2672 * intel_pt_tsc_cmp - compare 7-byte TSCs.
2673 * @tsc1: first TSC to compare
2674 * @tsc2: second TSC to compare
2675 *
2676 * This function compares 7-byte TSC values allowing for the possibility that
2677 * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
2678 * around so for that purpose this function assumes the absolute difference is
2679 * less than half the maximum difference.
2680 *
2681 * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
2682 * after @tsc2.
2683 */
2684static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
2685{
2686 const uint64_t halfway = (1ULL << 55);
2687
2688 if (tsc1 == tsc2)
2689 return 0;
2690
2691 if (tsc1 < tsc2) {
2692 if (tsc2 - tsc1 < halfway)
2693 return -1;
2694 else
2695 return 1;
2696 } else {
2697 if (tsc1 - tsc2 < halfway)
2698 return 1;
2699 else
2700 return -1;
2701 }
2702}
2703
Adrian Hunter5a99d992019-02-06 12:39:44 +02002704#define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
2705
2706/**
2707 * adj_for_padding - adjust overlap to account for padding.
2708 * @buf_b: second buffer
2709 * @buf_a: first buffer
2710 * @len_a: size of first buffer
2711 *
2712 * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
2713 * accordingly.
2714 *
2715 * Return: A pointer into @buf_b from where non-overlapped data starts
2716 */
2717static unsigned char *adj_for_padding(unsigned char *buf_b,
2718 unsigned char *buf_a, size_t len_a)
2719{
2720 unsigned char *p = buf_b - MAX_PADDING;
2721 unsigned char *q = buf_a + len_a - MAX_PADDING;
2722 int i;
2723
2724 for (i = MAX_PADDING; i; i--, p++, q++) {
2725 if (*p != *q)
2726 break;
2727 }
2728
2729 return p;
2730}
2731
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002732/**
2733 * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
2734 * using TSC.
2735 * @buf_a: first buffer
2736 * @len_a: size of first buffer
2737 * @buf_b: second buffer
2738 * @len_b: size of second buffer
Adrian Hunter117db4b2018-03-07 16:02:21 +02002739 * @consecutive: returns true if there is data in buf_b that is consecutive
2740 * to buf_a
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002741 *
2742 * If the trace contains TSC we can look at the last TSC of @buf_a and the
2743 * first TSC of @buf_b in order to determine if the buffers overlap, and then
2744 * walk forward in @buf_b until a later TSC is found. A precondition is that
2745 * @buf_a and @buf_b are positioned at a PSB.
2746 *
2747 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2748 * @buf_b + @len_b if there is no non-overlapped data.
2749 */
2750static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
2751 size_t len_a,
2752 unsigned char *buf_b,
Adrian Hunter117db4b2018-03-07 16:02:21 +02002753 size_t len_b, bool *consecutive)
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002754{
2755 uint64_t tsc_a, tsc_b;
2756 unsigned char *p;
Adrian Hunter117db4b2018-03-07 16:02:21 +02002757 size_t len, rem_a, rem_b;
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002758
2759 p = intel_pt_last_psb(buf_a, len_a);
2760 if (!p)
2761 return buf_b; /* No PSB in buf_a => no overlap */
2762
2763 len = len_a - (p - buf_a);
Adrian Hunter117db4b2018-03-07 16:02:21 +02002764 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002765 /* The last PSB+ in buf_a is incomplete, so go back one more */
2766 len_a -= len;
2767 p = intel_pt_last_psb(buf_a, len_a);
2768 if (!p)
2769 return buf_b; /* No full PSB+ => assume no overlap */
2770 len = len_a - (p - buf_a);
Adrian Hunter117db4b2018-03-07 16:02:21 +02002771 if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002772 return buf_b; /* No TSC in buf_a => assume no overlap */
2773 }
2774
2775 while (1) {
2776 /* Ignore PSB+ with no TSC */
Adrian Hunter117db4b2018-03-07 16:02:21 +02002777 if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
2778 int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
2779
2780 /* Same TSC, so buffers are consecutive */
2781 if (!cmp && rem_b >= rem_a) {
Adrian Hunter5a99d992019-02-06 12:39:44 +02002782 unsigned char *start;
2783
Adrian Hunter117db4b2018-03-07 16:02:21 +02002784 *consecutive = true;
Adrian Hunter5a99d992019-02-06 12:39:44 +02002785 start = buf_b + len_b - (rem_b - rem_a);
2786 return adj_for_padding(start, buf_a, len_a);
Adrian Hunter117db4b2018-03-07 16:02:21 +02002787 }
2788 if (cmp < 0)
2789 return buf_b; /* tsc_a < tsc_b => no overlap */
2790 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002791
2792 if (!intel_pt_step_psb(&buf_b, &len_b))
2793 return buf_b + len_b; /* No PSB in buf_b => no data */
2794 }
2795}
2796
2797/**
2798 * intel_pt_find_overlap - determine start of non-overlapped trace data.
2799 * @buf_a: first buffer
2800 * @len_a: size of first buffer
2801 * @buf_b: second buffer
2802 * @len_b: size of second buffer
2803 * @have_tsc: can use TSC packets to detect overlap
Adrian Hunter117db4b2018-03-07 16:02:21 +02002804 * @consecutive: returns true if there is data in buf_b that is consecutive
2805 * to buf_a
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002806 *
2807 * When trace samples or snapshots are recorded there is the possibility that
2808 * the data overlaps. Note that, for the purposes of decoding, data is only
2809 * useful if it begins with a PSB packet.
2810 *
2811 * Return: A pointer into @buf_b from where non-overlapped data starts, or
2812 * @buf_b + @len_b if there is no non-overlapped data.
2813 */
2814unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
2815 unsigned char *buf_b, size_t len_b,
Adrian Hunter117db4b2018-03-07 16:02:21 +02002816 bool have_tsc, bool *consecutive)
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002817{
2818 unsigned char *found;
2819
2820 /* Buffer 'b' must start at PSB so throw away everything before that */
2821 if (!intel_pt_next_psb(&buf_b, &len_b))
2822 return buf_b + len_b; /* No PSB */
2823
2824 if (!intel_pt_next_psb(&buf_a, &len_a))
2825 return buf_b; /* No overlap */
2826
2827 if (have_tsc) {
Adrian Hunter117db4b2018-03-07 16:02:21 +02002828 found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
2829 consecutive);
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002830 if (found)
2831 return found;
2832 }
2833
2834 /*
2835 * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
2836 * we can ignore the first part of buffer 'a'.
2837 */
2838 while (len_b < len_a) {
2839 if (!intel_pt_step_psb(&buf_a, &len_a))
2840 return buf_b; /* No overlap */
2841 }
2842
2843 /* Now len_b >= len_a */
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002844 while (1) {
2845 /* Potential overlap so check the bytes */
2846 found = memmem(buf_a, len_a, buf_b, len_a);
Adrian Hunter117db4b2018-03-07 16:02:21 +02002847 if (found) {
2848 *consecutive = true;
Adrian Hunter5a99d992019-02-06 12:39:44 +02002849 return adj_for_padding(buf_b + len_a, buf_a, len_a);
Adrian Hunter117db4b2018-03-07 16:02:21 +02002850 }
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002851
2852 /* Try again at next PSB in buffer 'a' */
2853 if (!intel_pt_step_psb(&buf_a, &len_a))
2854 return buf_b; /* No overlap */
Adrian Hunterf4aa0812015-07-17 19:33:40 +03002855 }
2856}