Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1 | /* |
| 2 | * intel_pt_decoder.c: Intel Processor Trace support |
| 3 | * Copyright (c) 2013-2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #ifndef _GNU_SOURCE |
| 17 | #define _GNU_SOURCE |
| 18 | #endif |
| 19 | #include <stdlib.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #include <stdint.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include "../cache.h" |
| 27 | #include "../util.h" |
| 28 | |
| 29 | #include "intel-pt-insn-decoder.h" |
| 30 | #include "intel-pt-pkt-decoder.h" |
| 31 | #include "intel-pt-decoder.h" |
| 32 | #include "intel-pt-log.h" |
| 33 | |
| 34 | #define INTEL_PT_BLK_SIZE 1024 |
| 35 | |
| 36 | #define BIT63 (((uint64_t)1 << 63)) |
| 37 | |
| 38 | #define INTEL_PT_RETURN 1 |
| 39 | |
| 40 | /* Maximum number of loops with no packets consumed i.e. stuck in a loop */ |
| 41 | #define INTEL_PT_MAX_LOOPS 10000 |
| 42 | |
| 43 | struct intel_pt_blk { |
| 44 | struct intel_pt_blk *prev; |
| 45 | uint64_t ip[INTEL_PT_BLK_SIZE]; |
| 46 | }; |
| 47 | |
| 48 | struct intel_pt_stack { |
| 49 | struct intel_pt_blk *blk; |
| 50 | struct intel_pt_blk *spare; |
| 51 | int pos; |
| 52 | }; |
| 53 | |
| 54 | enum intel_pt_pkt_state { |
| 55 | INTEL_PT_STATE_NO_PSB, |
| 56 | INTEL_PT_STATE_NO_IP, |
| 57 | INTEL_PT_STATE_ERR_RESYNC, |
| 58 | INTEL_PT_STATE_IN_SYNC, |
| 59 | INTEL_PT_STATE_TNT, |
| 60 | INTEL_PT_STATE_TIP, |
| 61 | INTEL_PT_STATE_TIP_PGD, |
| 62 | INTEL_PT_STATE_FUP, |
| 63 | INTEL_PT_STATE_FUP_NO_TIP, |
| 64 | }; |
| 65 | |
| 66 | #ifdef INTEL_PT_STRICT |
| 67 | #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB |
| 68 | #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB |
| 69 | #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB |
| 70 | #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB |
| 71 | #else |
| 72 | #define INTEL_PT_STATE_ERR1 (decoder->pkt_state) |
| 73 | #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP |
| 74 | #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC |
| 75 | #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC |
| 76 | #endif |
| 77 | |
| 78 | struct intel_pt_decoder { |
| 79 | int (*get_trace)(struct intel_pt_buffer *buffer, void *data); |
| 80 | int (*walk_insn)(struct intel_pt_insn *intel_pt_insn, |
| 81 | uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip, |
| 82 | uint64_t max_insn_cnt, void *data); |
| 83 | void *data; |
| 84 | struct intel_pt_state state; |
| 85 | const unsigned char *buf; |
| 86 | size_t len; |
| 87 | bool return_compression; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 88 | bool mtc_insn; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 89 | bool pge; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 90 | bool have_tma; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 91 | uint64_t pos; |
| 92 | uint64_t last_ip; |
| 93 | uint64_t ip; |
| 94 | uint64_t cr3; |
| 95 | uint64_t timestamp; |
| 96 | uint64_t tsc_timestamp; |
| 97 | uint64_t ref_timestamp; |
| 98 | uint64_t ret_addr; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 99 | uint64_t ctc_timestamp; |
| 100 | uint64_t ctc_delta; |
| 101 | uint32_t last_mtc; |
| 102 | uint32_t tsc_ctc_ratio_n; |
| 103 | uint32_t tsc_ctc_ratio_d; |
| 104 | uint32_t tsc_ctc_mult; |
| 105 | uint32_t tsc_slip; |
| 106 | uint32_t ctc_rem_mask; |
| 107 | int mtc_shift; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 108 | struct intel_pt_stack stack; |
| 109 | enum intel_pt_pkt_state pkt_state; |
| 110 | struct intel_pt_pkt packet; |
| 111 | struct intel_pt_pkt tnt; |
| 112 | int pkt_step; |
| 113 | int pkt_len; |
| 114 | unsigned int cbr; |
| 115 | unsigned int max_non_turbo_ratio; |
| 116 | int exec_mode; |
| 117 | unsigned int insn_bytes; |
| 118 | uint64_t sign_bit; |
| 119 | uint64_t sign_bits; |
| 120 | uint64_t period; |
| 121 | enum intel_pt_period_type period_type; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 122 | uint64_t tot_insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 123 | uint64_t period_insn_cnt; |
| 124 | uint64_t period_mask; |
| 125 | uint64_t period_ticks; |
| 126 | uint64_t last_masked_timestamp; |
| 127 | bool continuous_period; |
| 128 | bool overflow; |
| 129 | bool set_fup_tx_flags; |
| 130 | unsigned int fup_tx_flags; |
| 131 | unsigned int tx_flags; |
| 132 | uint64_t timestamp_insn_cnt; |
| 133 | uint64_t stuck_ip; |
| 134 | int no_progress; |
| 135 | int stuck_ip_prd; |
| 136 | int stuck_ip_cnt; |
| 137 | const unsigned char *next_buf; |
| 138 | size_t next_len; |
| 139 | unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ]; |
| 140 | }; |
| 141 | |
| 142 | static uint64_t intel_pt_lower_power_of_2(uint64_t x) |
| 143 | { |
| 144 | int i; |
| 145 | |
| 146 | for (i = 0; x != 1; i++) |
| 147 | x >>= 1; |
| 148 | |
| 149 | return x << i; |
| 150 | } |
| 151 | |
| 152 | static void intel_pt_setup_period(struct intel_pt_decoder *decoder) |
| 153 | { |
| 154 | if (decoder->period_type == INTEL_PT_PERIOD_TICKS) { |
| 155 | uint64_t period; |
| 156 | |
| 157 | period = intel_pt_lower_power_of_2(decoder->period); |
| 158 | decoder->period_mask = ~(period - 1); |
| 159 | decoder->period_ticks = period; |
| 160 | } |
| 161 | } |
| 162 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 163 | static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d) |
| 164 | { |
| 165 | if (!d) |
| 166 | return 0; |
| 167 | return (t / d) * n + ((t % d) * n) / d; |
| 168 | } |
| 169 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 170 | struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params) |
| 171 | { |
| 172 | struct intel_pt_decoder *decoder; |
| 173 | |
| 174 | if (!params->get_trace || !params->walk_insn) |
| 175 | return NULL; |
| 176 | |
| 177 | decoder = zalloc(sizeof(struct intel_pt_decoder)); |
| 178 | if (!decoder) |
| 179 | return NULL; |
| 180 | |
| 181 | decoder->get_trace = params->get_trace; |
| 182 | decoder->walk_insn = params->walk_insn; |
| 183 | decoder->data = params->data; |
| 184 | decoder->return_compression = params->return_compression; |
| 185 | |
| 186 | decoder->sign_bit = (uint64_t)1 << 47; |
| 187 | decoder->sign_bits = ~(((uint64_t)1 << 48) - 1); |
| 188 | |
| 189 | decoder->period = params->period; |
| 190 | decoder->period_type = params->period_type; |
| 191 | |
| 192 | decoder->max_non_turbo_ratio = params->max_non_turbo_ratio; |
| 193 | |
| 194 | intel_pt_setup_period(decoder); |
| 195 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 196 | decoder->mtc_shift = params->mtc_period; |
| 197 | decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1; |
| 198 | |
| 199 | decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n; |
| 200 | decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d; |
| 201 | |
| 202 | if (!decoder->tsc_ctc_ratio_n) |
| 203 | decoder->tsc_ctc_ratio_d = 0; |
| 204 | |
| 205 | if (decoder->tsc_ctc_ratio_d) { |
| 206 | if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d)) |
| 207 | decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n / |
| 208 | decoder->tsc_ctc_ratio_d; |
| 209 | |
| 210 | /* |
| 211 | * Allow for timestamps appearing to backwards because a TSC |
| 212 | * packet has slipped past a MTC packet, so allow 2 MTC ticks |
| 213 | * or ... |
| 214 | */ |
| 215 | decoder->tsc_slip = multdiv(2 << decoder->mtc_shift, |
| 216 | decoder->tsc_ctc_ratio_n, |
| 217 | decoder->tsc_ctc_ratio_d); |
| 218 | } |
| 219 | /* ... or 0x100 paranoia */ |
| 220 | if (decoder->tsc_slip < 0x100) |
| 221 | decoder->tsc_slip = 0x100; |
| 222 | |
| 223 | intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift); |
| 224 | intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n); |
| 225 | intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d); |
| 226 | intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult); |
| 227 | intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip); |
| 228 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 229 | return decoder; |
| 230 | } |
| 231 | |
| 232 | static void intel_pt_pop_blk(struct intel_pt_stack *stack) |
| 233 | { |
| 234 | struct intel_pt_blk *blk = stack->blk; |
| 235 | |
| 236 | stack->blk = blk->prev; |
| 237 | if (!stack->spare) |
| 238 | stack->spare = blk; |
| 239 | else |
| 240 | free(blk); |
| 241 | } |
| 242 | |
| 243 | static uint64_t intel_pt_pop(struct intel_pt_stack *stack) |
| 244 | { |
| 245 | if (!stack->pos) { |
| 246 | if (!stack->blk) |
| 247 | return 0; |
| 248 | intel_pt_pop_blk(stack); |
| 249 | if (!stack->blk) |
| 250 | return 0; |
| 251 | stack->pos = INTEL_PT_BLK_SIZE; |
| 252 | } |
| 253 | return stack->blk->ip[--stack->pos]; |
| 254 | } |
| 255 | |
| 256 | static int intel_pt_alloc_blk(struct intel_pt_stack *stack) |
| 257 | { |
| 258 | struct intel_pt_blk *blk; |
| 259 | |
| 260 | if (stack->spare) { |
| 261 | blk = stack->spare; |
| 262 | stack->spare = NULL; |
| 263 | } else { |
| 264 | blk = malloc(sizeof(struct intel_pt_blk)); |
| 265 | if (!blk) |
| 266 | return -ENOMEM; |
| 267 | } |
| 268 | |
| 269 | blk->prev = stack->blk; |
| 270 | stack->blk = blk; |
| 271 | stack->pos = 0; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip) |
| 276 | { |
| 277 | int err; |
| 278 | |
| 279 | if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) { |
| 280 | err = intel_pt_alloc_blk(stack); |
| 281 | if (err) |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | stack->blk->ip[stack->pos++] = ip; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static void intel_pt_clear_stack(struct intel_pt_stack *stack) |
| 290 | { |
| 291 | while (stack->blk) |
| 292 | intel_pt_pop_blk(stack); |
| 293 | stack->pos = 0; |
| 294 | } |
| 295 | |
| 296 | static void intel_pt_free_stack(struct intel_pt_stack *stack) |
| 297 | { |
| 298 | intel_pt_clear_stack(stack); |
| 299 | zfree(&stack->blk); |
| 300 | zfree(&stack->spare); |
| 301 | } |
| 302 | |
| 303 | void intel_pt_decoder_free(struct intel_pt_decoder *decoder) |
| 304 | { |
| 305 | intel_pt_free_stack(&decoder->stack); |
| 306 | free(decoder); |
| 307 | } |
| 308 | |
| 309 | static int intel_pt_ext_err(int code) |
| 310 | { |
| 311 | switch (code) { |
| 312 | case -ENOMEM: |
| 313 | return INTEL_PT_ERR_NOMEM; |
| 314 | case -ENOSYS: |
| 315 | return INTEL_PT_ERR_INTERN; |
| 316 | case -EBADMSG: |
| 317 | return INTEL_PT_ERR_BADPKT; |
| 318 | case -ENODATA: |
| 319 | return INTEL_PT_ERR_NODATA; |
| 320 | case -EILSEQ: |
| 321 | return INTEL_PT_ERR_NOINSN; |
| 322 | case -ENOENT: |
| 323 | return INTEL_PT_ERR_MISMAT; |
| 324 | case -EOVERFLOW: |
| 325 | return INTEL_PT_ERR_OVR; |
| 326 | case -ENOSPC: |
| 327 | return INTEL_PT_ERR_LOST; |
| 328 | case -ELOOP: |
| 329 | return INTEL_PT_ERR_NELOOP; |
| 330 | default: |
| 331 | return INTEL_PT_ERR_UNK; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static const char *intel_pt_err_msgs[] = { |
| 336 | [INTEL_PT_ERR_NOMEM] = "Memory allocation failed", |
| 337 | [INTEL_PT_ERR_INTERN] = "Internal error", |
| 338 | [INTEL_PT_ERR_BADPKT] = "Bad packet", |
| 339 | [INTEL_PT_ERR_NODATA] = "No more data", |
| 340 | [INTEL_PT_ERR_NOINSN] = "Failed to get instruction", |
| 341 | [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction", |
| 342 | [INTEL_PT_ERR_OVR] = "Overflow packet", |
| 343 | [INTEL_PT_ERR_LOST] = "Lost trace data", |
| 344 | [INTEL_PT_ERR_UNK] = "Unknown error!", |
| 345 | [INTEL_PT_ERR_NELOOP] = "Never-ending loop", |
| 346 | }; |
| 347 | |
| 348 | int intel_pt__strerror(int code, char *buf, size_t buflen) |
| 349 | { |
| 350 | if (code < 1 || code > INTEL_PT_ERR_MAX) |
| 351 | code = INTEL_PT_ERR_UNK; |
| 352 | strlcpy(buf, intel_pt_err_msgs[code], buflen); |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static uint64_t intel_pt_calc_ip(struct intel_pt_decoder *decoder, |
| 357 | const struct intel_pt_pkt *packet, |
| 358 | uint64_t last_ip) |
| 359 | { |
| 360 | uint64_t ip; |
| 361 | |
| 362 | switch (packet->count) { |
| 363 | case 2: |
| 364 | ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) | |
| 365 | packet->payload; |
| 366 | break; |
| 367 | case 4: |
| 368 | ip = (last_ip & (uint64_t)0xffffffff00000000ULL) | |
| 369 | packet->payload; |
| 370 | break; |
| 371 | case 6: |
| 372 | ip = packet->payload; |
| 373 | break; |
| 374 | default: |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | if (ip & decoder->sign_bit) |
| 379 | return ip | decoder->sign_bits; |
| 380 | |
| 381 | return ip; |
| 382 | } |
| 383 | |
| 384 | static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder) |
| 385 | { |
| 386 | decoder->last_ip = intel_pt_calc_ip(decoder, &decoder->packet, |
| 387 | decoder->last_ip); |
| 388 | } |
| 389 | |
| 390 | static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder) |
| 391 | { |
| 392 | intel_pt_set_last_ip(decoder); |
| 393 | decoder->ip = decoder->last_ip; |
| 394 | } |
| 395 | |
| 396 | static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder) |
| 397 | { |
| 398 | intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos, |
| 399 | decoder->buf); |
| 400 | } |
| 401 | |
| 402 | static int intel_pt_bug(struct intel_pt_decoder *decoder) |
| 403 | { |
| 404 | intel_pt_log("ERROR: Internal error\n"); |
| 405 | decoder->pkt_state = INTEL_PT_STATE_NO_PSB; |
| 406 | return -ENOSYS; |
| 407 | } |
| 408 | |
| 409 | static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder) |
| 410 | { |
| 411 | decoder->tx_flags = 0; |
| 412 | } |
| 413 | |
| 414 | static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder) |
| 415 | { |
| 416 | decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX; |
| 417 | } |
| 418 | |
| 419 | static int intel_pt_bad_packet(struct intel_pt_decoder *decoder) |
| 420 | { |
| 421 | intel_pt_clear_tx_flags(decoder); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 422 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 423 | decoder->pkt_len = 1; |
| 424 | decoder->pkt_step = 1; |
| 425 | intel_pt_decoder_log_packet(decoder); |
| 426 | if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) { |
| 427 | intel_pt_log("ERROR: Bad packet\n"); |
| 428 | decoder->pkt_state = INTEL_PT_STATE_ERR1; |
| 429 | } |
| 430 | return -EBADMSG; |
| 431 | } |
| 432 | |
| 433 | static int intel_pt_get_data(struct intel_pt_decoder *decoder) |
| 434 | { |
| 435 | struct intel_pt_buffer buffer = { .buf = 0, }; |
| 436 | int ret; |
| 437 | |
| 438 | decoder->pkt_step = 0; |
| 439 | |
| 440 | intel_pt_log("Getting more data\n"); |
| 441 | ret = decoder->get_trace(&buffer, decoder->data); |
| 442 | if (ret) |
| 443 | return ret; |
| 444 | decoder->buf = buffer.buf; |
| 445 | decoder->len = buffer.len; |
| 446 | if (!decoder->len) { |
| 447 | intel_pt_log("No more data\n"); |
| 448 | return -ENODATA; |
| 449 | } |
| 450 | if (!buffer.consecutive) { |
| 451 | decoder->ip = 0; |
| 452 | decoder->pkt_state = INTEL_PT_STATE_NO_PSB; |
| 453 | decoder->ref_timestamp = buffer.ref_timestamp; |
| 454 | decoder->timestamp = 0; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 455 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 456 | decoder->state.trace_nr = buffer.trace_nr; |
| 457 | intel_pt_log("Reference timestamp 0x%" PRIx64 "\n", |
| 458 | decoder->ref_timestamp); |
| 459 | return -ENOLINK; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int intel_pt_get_next_data(struct intel_pt_decoder *decoder) |
| 466 | { |
| 467 | if (!decoder->next_buf) |
| 468 | return intel_pt_get_data(decoder); |
| 469 | |
| 470 | decoder->buf = decoder->next_buf; |
| 471 | decoder->len = decoder->next_len; |
| 472 | decoder->next_buf = 0; |
| 473 | decoder->next_len = 0; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder) |
| 478 | { |
| 479 | unsigned char *buf = decoder->temp_buf; |
| 480 | size_t old_len, len, n; |
| 481 | int ret; |
| 482 | |
| 483 | old_len = decoder->len; |
| 484 | len = decoder->len; |
| 485 | memcpy(buf, decoder->buf, len); |
| 486 | |
| 487 | ret = intel_pt_get_data(decoder); |
| 488 | if (ret) { |
| 489 | decoder->pos += old_len; |
| 490 | return ret < 0 ? ret : -EINVAL; |
| 491 | } |
| 492 | |
| 493 | n = INTEL_PT_PKT_MAX_SZ - len; |
| 494 | if (n > decoder->len) |
| 495 | n = decoder->len; |
| 496 | memcpy(buf + len, decoder->buf, n); |
| 497 | len += n; |
| 498 | |
| 499 | ret = intel_pt_get_packet(buf, len, &decoder->packet); |
| 500 | if (ret < (int)old_len) { |
| 501 | decoder->next_buf = decoder->buf; |
| 502 | decoder->next_len = decoder->len; |
| 503 | decoder->buf = buf; |
| 504 | decoder->len = old_len; |
| 505 | return intel_pt_bad_packet(decoder); |
| 506 | } |
| 507 | |
| 508 | decoder->next_buf = decoder->buf + (ret - old_len); |
| 509 | decoder->next_len = decoder->len - (ret - old_len); |
| 510 | |
| 511 | decoder->buf = buf; |
| 512 | decoder->len = ret; |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder) |
| 518 | { |
| 519 | int ret; |
| 520 | |
| 521 | do { |
| 522 | decoder->pos += decoder->pkt_step; |
| 523 | decoder->buf += decoder->pkt_step; |
| 524 | decoder->len -= decoder->pkt_step; |
| 525 | |
| 526 | if (!decoder->len) { |
| 527 | ret = intel_pt_get_next_data(decoder); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | ret = intel_pt_get_packet(decoder->buf, decoder->len, |
| 533 | &decoder->packet); |
| 534 | if (ret == INTEL_PT_NEED_MORE_BYTES && |
| 535 | decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) { |
| 536 | ret = intel_pt_get_split_packet(decoder); |
| 537 | if (ret < 0) |
| 538 | return ret; |
| 539 | } |
| 540 | if (ret <= 0) |
| 541 | return intel_pt_bad_packet(decoder); |
| 542 | |
| 543 | decoder->pkt_len = ret; |
| 544 | decoder->pkt_step = ret; |
| 545 | intel_pt_decoder_log_packet(decoder); |
| 546 | } while (decoder->packet.type == INTEL_PT_PAD); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder) |
| 552 | { |
| 553 | uint64_t timestamp, masked_timestamp; |
| 554 | |
| 555 | timestamp = decoder->timestamp + decoder->timestamp_insn_cnt; |
| 556 | masked_timestamp = timestamp & decoder->period_mask; |
| 557 | if (decoder->continuous_period) { |
| 558 | if (masked_timestamp != decoder->last_masked_timestamp) |
| 559 | return 1; |
| 560 | } else { |
| 561 | timestamp += 1; |
| 562 | masked_timestamp = timestamp & decoder->period_mask; |
| 563 | if (masked_timestamp != decoder->last_masked_timestamp) { |
| 564 | decoder->last_masked_timestamp = masked_timestamp; |
| 565 | decoder->continuous_period = true; |
| 566 | } |
| 567 | } |
| 568 | return decoder->period_ticks - (timestamp - masked_timestamp); |
| 569 | } |
| 570 | |
| 571 | static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder) |
| 572 | { |
| 573 | switch (decoder->period_type) { |
| 574 | case INTEL_PT_PERIOD_INSTRUCTIONS: |
| 575 | return decoder->period - decoder->period_insn_cnt; |
| 576 | case INTEL_PT_PERIOD_TICKS: |
| 577 | return intel_pt_next_period(decoder); |
| 578 | case INTEL_PT_PERIOD_NONE: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 579 | case INTEL_PT_PERIOD_MTC: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 580 | default: |
| 581 | return 0; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void intel_pt_sample_insn(struct intel_pt_decoder *decoder) |
| 586 | { |
| 587 | uint64_t timestamp, masked_timestamp; |
| 588 | |
| 589 | switch (decoder->period_type) { |
| 590 | case INTEL_PT_PERIOD_INSTRUCTIONS: |
| 591 | decoder->period_insn_cnt = 0; |
| 592 | break; |
| 593 | case INTEL_PT_PERIOD_TICKS: |
| 594 | timestamp = decoder->timestamp + decoder->timestamp_insn_cnt; |
| 595 | masked_timestamp = timestamp & decoder->period_mask; |
| 596 | decoder->last_masked_timestamp = masked_timestamp; |
| 597 | break; |
| 598 | case INTEL_PT_PERIOD_NONE: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 599 | case INTEL_PT_PERIOD_MTC: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
| 605 | } |
| 606 | |
| 607 | static int intel_pt_walk_insn(struct intel_pt_decoder *decoder, |
| 608 | struct intel_pt_insn *intel_pt_insn, uint64_t ip) |
| 609 | { |
| 610 | uint64_t max_insn_cnt, insn_cnt = 0; |
| 611 | int err; |
| 612 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 613 | if (!decoder->mtc_insn) |
| 614 | decoder->mtc_insn = true; |
| 615 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 616 | max_insn_cnt = intel_pt_next_sample(decoder); |
| 617 | |
| 618 | err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip, |
| 619 | max_insn_cnt, decoder->data); |
| 620 | |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 621 | decoder->tot_insn_cnt += insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 622 | decoder->timestamp_insn_cnt += insn_cnt; |
| 623 | decoder->period_insn_cnt += insn_cnt; |
| 624 | |
| 625 | if (err) { |
| 626 | decoder->no_progress = 0; |
| 627 | decoder->pkt_state = INTEL_PT_STATE_ERR2; |
| 628 | intel_pt_log_at("ERROR: Failed to get instruction", |
| 629 | decoder->ip); |
| 630 | if (err == -ENOENT) |
| 631 | return -ENOLINK; |
| 632 | return -EILSEQ; |
| 633 | } |
| 634 | |
| 635 | if (ip && decoder->ip == ip) { |
| 636 | err = -EAGAIN; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | if (max_insn_cnt && insn_cnt >= max_insn_cnt) |
| 641 | intel_pt_sample_insn(decoder); |
| 642 | |
| 643 | if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) { |
| 644 | decoder->state.type = INTEL_PT_INSTRUCTION; |
| 645 | decoder->state.from_ip = decoder->ip; |
| 646 | decoder->state.to_ip = 0; |
| 647 | decoder->ip += intel_pt_insn->length; |
| 648 | err = INTEL_PT_RETURN; |
| 649 | goto out; |
| 650 | } |
| 651 | |
| 652 | if (intel_pt_insn->op == INTEL_PT_OP_CALL) { |
| 653 | /* Zero-length calls are excluded */ |
| 654 | if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL || |
| 655 | intel_pt_insn->rel) { |
| 656 | err = intel_pt_push(&decoder->stack, decoder->ip + |
| 657 | intel_pt_insn->length); |
| 658 | if (err) |
| 659 | goto out; |
| 660 | } |
| 661 | } else if (intel_pt_insn->op == INTEL_PT_OP_RET) { |
| 662 | decoder->ret_addr = intel_pt_pop(&decoder->stack); |
| 663 | } |
| 664 | |
| 665 | if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) { |
| 666 | int cnt = decoder->no_progress++; |
| 667 | |
| 668 | decoder->state.from_ip = decoder->ip; |
| 669 | decoder->ip += intel_pt_insn->length + |
| 670 | intel_pt_insn->rel; |
| 671 | decoder->state.to_ip = decoder->ip; |
| 672 | err = INTEL_PT_RETURN; |
| 673 | |
| 674 | /* |
| 675 | * Check for being stuck in a loop. This can happen if a |
| 676 | * decoder error results in the decoder erroneously setting the |
| 677 | * ip to an address that is itself in an infinite loop that |
| 678 | * consumes no packets. When that happens, there must be an |
| 679 | * unconditional branch. |
| 680 | */ |
| 681 | if (cnt) { |
| 682 | if (cnt == 1) { |
| 683 | decoder->stuck_ip = decoder->state.to_ip; |
| 684 | decoder->stuck_ip_prd = 1; |
| 685 | decoder->stuck_ip_cnt = 1; |
| 686 | } else if (cnt > INTEL_PT_MAX_LOOPS || |
| 687 | decoder->state.to_ip == decoder->stuck_ip) { |
| 688 | intel_pt_log_at("ERROR: Never-ending loop", |
| 689 | decoder->state.to_ip); |
| 690 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 691 | err = -ELOOP; |
| 692 | goto out; |
| 693 | } else if (!--decoder->stuck_ip_cnt) { |
| 694 | decoder->stuck_ip_prd += 1; |
| 695 | decoder->stuck_ip_cnt = decoder->stuck_ip_prd; |
| 696 | decoder->stuck_ip = decoder->state.to_ip; |
| 697 | } |
| 698 | } |
| 699 | goto out_no_progress; |
| 700 | } |
| 701 | out: |
| 702 | decoder->no_progress = 0; |
| 703 | out_no_progress: |
| 704 | decoder->state.insn_op = intel_pt_insn->op; |
| 705 | decoder->state.insn_len = intel_pt_insn->length; |
| 706 | |
| 707 | if (decoder->tx_flags & INTEL_PT_IN_TX) |
| 708 | decoder->state.flags |= INTEL_PT_IN_TX; |
| 709 | |
| 710 | return err; |
| 711 | } |
| 712 | |
| 713 | static int intel_pt_walk_fup(struct intel_pt_decoder *decoder) |
| 714 | { |
| 715 | struct intel_pt_insn intel_pt_insn; |
| 716 | uint64_t ip; |
| 717 | int err; |
| 718 | |
| 719 | ip = decoder->last_ip; |
| 720 | |
| 721 | while (1) { |
| 722 | err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip); |
| 723 | if (err == INTEL_PT_RETURN) |
| 724 | return 0; |
| 725 | if (err == -EAGAIN) { |
| 726 | if (decoder->set_fup_tx_flags) { |
| 727 | decoder->set_fup_tx_flags = false; |
| 728 | decoder->tx_flags = decoder->fup_tx_flags; |
| 729 | decoder->state.type = INTEL_PT_TRANSACTION; |
| 730 | decoder->state.from_ip = decoder->ip; |
| 731 | decoder->state.to_ip = 0; |
| 732 | decoder->state.flags = decoder->fup_tx_flags; |
| 733 | return 0; |
| 734 | } |
| 735 | return err; |
| 736 | } |
| 737 | decoder->set_fup_tx_flags = false; |
| 738 | if (err) |
| 739 | return err; |
| 740 | |
| 741 | if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) { |
| 742 | intel_pt_log_at("ERROR: Unexpected indirect branch", |
| 743 | decoder->ip); |
| 744 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 745 | return -ENOENT; |
| 746 | } |
| 747 | |
| 748 | if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) { |
| 749 | intel_pt_log_at("ERROR: Unexpected conditional branch", |
| 750 | decoder->ip); |
| 751 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 752 | return -ENOENT; |
| 753 | } |
| 754 | |
| 755 | intel_pt_bug(decoder); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static int intel_pt_walk_tip(struct intel_pt_decoder *decoder) |
| 760 | { |
| 761 | struct intel_pt_insn intel_pt_insn; |
| 762 | int err; |
| 763 | |
| 764 | err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0); |
| 765 | if (err == INTEL_PT_RETURN) |
| 766 | return 0; |
| 767 | if (err) |
| 768 | return err; |
| 769 | |
| 770 | if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) { |
| 771 | if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) { |
| 772 | decoder->pge = false; |
| 773 | decoder->continuous_period = false; |
| 774 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 775 | decoder->state.from_ip = decoder->ip; |
| 776 | decoder->state.to_ip = 0; |
| 777 | if (decoder->packet.count != 0) |
| 778 | decoder->ip = decoder->last_ip; |
| 779 | } else { |
| 780 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 781 | decoder->state.from_ip = decoder->ip; |
| 782 | if (decoder->packet.count == 0) { |
| 783 | decoder->state.to_ip = 0; |
| 784 | } else { |
| 785 | decoder->state.to_ip = decoder->last_ip; |
| 786 | decoder->ip = decoder->last_ip; |
| 787 | } |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) { |
| 793 | intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch", |
| 794 | decoder->ip); |
| 795 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 796 | return -ENOENT; |
| 797 | } |
| 798 | |
| 799 | return intel_pt_bug(decoder); |
| 800 | } |
| 801 | |
| 802 | static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder) |
| 803 | { |
| 804 | struct intel_pt_insn intel_pt_insn; |
| 805 | int err; |
| 806 | |
| 807 | while (1) { |
| 808 | err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0); |
| 809 | if (err == INTEL_PT_RETURN) |
| 810 | return 0; |
| 811 | if (err) |
| 812 | return err; |
| 813 | |
| 814 | if (intel_pt_insn.op == INTEL_PT_OP_RET) { |
| 815 | if (!decoder->return_compression) { |
| 816 | intel_pt_log_at("ERROR: RET when expecting conditional branch", |
| 817 | decoder->ip); |
| 818 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 819 | return -ENOENT; |
| 820 | } |
| 821 | if (!decoder->ret_addr) { |
| 822 | intel_pt_log_at("ERROR: Bad RET compression (stack empty)", |
| 823 | decoder->ip); |
| 824 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 825 | return -ENOENT; |
| 826 | } |
| 827 | if (!(decoder->tnt.payload & BIT63)) { |
| 828 | intel_pt_log_at("ERROR: Bad RET compression (TNT=N)", |
| 829 | decoder->ip); |
| 830 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 831 | return -ENOENT; |
| 832 | } |
| 833 | decoder->tnt.count -= 1; |
| 834 | if (!decoder->tnt.count) |
| 835 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 836 | decoder->tnt.payload <<= 1; |
| 837 | decoder->state.from_ip = decoder->ip; |
| 838 | decoder->ip = decoder->ret_addr; |
| 839 | decoder->state.to_ip = decoder->ip; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) { |
| 844 | /* Handle deferred TIPs */ |
| 845 | err = intel_pt_get_next_packet(decoder); |
| 846 | if (err) |
| 847 | return err; |
| 848 | if (decoder->packet.type != INTEL_PT_TIP || |
| 849 | decoder->packet.count == 0) { |
| 850 | intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch", |
| 851 | decoder->ip); |
| 852 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 853 | decoder->pkt_step = 0; |
| 854 | return -ENOENT; |
| 855 | } |
| 856 | intel_pt_set_last_ip(decoder); |
| 857 | decoder->state.from_ip = decoder->ip; |
| 858 | decoder->state.to_ip = decoder->last_ip; |
| 859 | decoder->ip = decoder->last_ip; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) { |
| 864 | decoder->tnt.count -= 1; |
| 865 | if (!decoder->tnt.count) |
| 866 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 867 | if (decoder->tnt.payload & BIT63) { |
| 868 | decoder->tnt.payload <<= 1; |
| 869 | decoder->state.from_ip = decoder->ip; |
| 870 | decoder->ip += intel_pt_insn.length + |
| 871 | intel_pt_insn.rel; |
| 872 | decoder->state.to_ip = decoder->ip; |
| 873 | return 0; |
| 874 | } |
| 875 | /* Instruction sample for a non-taken branch */ |
| 876 | if (decoder->state.type & INTEL_PT_INSTRUCTION) { |
| 877 | decoder->tnt.payload <<= 1; |
| 878 | decoder->state.type = INTEL_PT_INSTRUCTION; |
| 879 | decoder->state.from_ip = decoder->ip; |
| 880 | decoder->state.to_ip = 0; |
| 881 | decoder->ip += intel_pt_insn.length; |
| 882 | return 0; |
| 883 | } |
| 884 | decoder->ip += intel_pt_insn.length; |
| 885 | if (!decoder->tnt.count) |
| 886 | return -EAGAIN; |
| 887 | decoder->tnt.payload <<= 1; |
| 888 | continue; |
| 889 | } |
| 890 | |
| 891 | return intel_pt_bug(decoder); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip) |
| 896 | { |
| 897 | unsigned int fup_tx_flags; |
| 898 | int err; |
| 899 | |
| 900 | fup_tx_flags = decoder->packet.payload & |
| 901 | (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX); |
| 902 | err = intel_pt_get_next_packet(decoder); |
| 903 | if (err) |
| 904 | return err; |
| 905 | if (decoder->packet.type == INTEL_PT_FUP) { |
| 906 | decoder->fup_tx_flags = fup_tx_flags; |
| 907 | decoder->set_fup_tx_flags = true; |
| 908 | if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX)) |
| 909 | *no_tip = true; |
| 910 | } else { |
| 911 | intel_pt_log_at("ERROR: Missing FUP after MODE.TSX", |
| 912 | decoder->pos); |
| 913 | intel_pt_update_in_tx(decoder); |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder) |
| 919 | { |
| 920 | uint64_t timestamp; |
| 921 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 922 | decoder->have_tma = false; |
| 923 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 924 | if (decoder->ref_timestamp) { |
| 925 | timestamp = decoder->packet.payload | |
| 926 | (decoder->ref_timestamp & (0xffULL << 56)); |
| 927 | if (timestamp < decoder->ref_timestamp) { |
| 928 | if (decoder->ref_timestamp - timestamp > (1ULL << 55)) |
| 929 | timestamp += (1ULL << 56); |
| 930 | } else { |
| 931 | if (timestamp - decoder->ref_timestamp > (1ULL << 55)) |
| 932 | timestamp -= (1ULL << 56); |
| 933 | } |
| 934 | decoder->tsc_timestamp = timestamp; |
| 935 | decoder->timestamp = timestamp; |
| 936 | decoder->ref_timestamp = 0; |
| 937 | decoder->timestamp_insn_cnt = 0; |
| 938 | } else if (decoder->timestamp) { |
| 939 | timestamp = decoder->packet.payload | |
| 940 | (decoder->timestamp & (0xffULL << 56)); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 941 | decoder->tsc_timestamp = timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 942 | if (timestamp < decoder->timestamp && |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 943 | decoder->timestamp - timestamp < decoder->tsc_slip) { |
| 944 | intel_pt_log_to("Suppressing backwards timestamp", |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 945 | timestamp); |
| 946 | timestamp = decoder->timestamp; |
| 947 | } |
| 948 | while (timestamp < decoder->timestamp) { |
| 949 | intel_pt_log_to("Wraparound timestamp", timestamp); |
| 950 | timestamp += (1ULL << 56); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 951 | decoder->tsc_timestamp = timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 952 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 953 | decoder->timestamp = timestamp; |
| 954 | decoder->timestamp_insn_cnt = 0; |
| 955 | } |
| 956 | |
| 957 | intel_pt_log_to("Setting timestamp", decoder->timestamp); |
| 958 | } |
| 959 | |
| 960 | static int intel_pt_overflow(struct intel_pt_decoder *decoder) |
| 961 | { |
| 962 | intel_pt_log("ERROR: Buffer overflow\n"); |
| 963 | intel_pt_clear_tx_flags(decoder); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 964 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 965 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 966 | decoder->overflow = true; |
| 967 | return -EOVERFLOW; |
| 968 | } |
| 969 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 970 | static void intel_pt_calc_tma(struct intel_pt_decoder *decoder) |
| 971 | { |
| 972 | uint32_t ctc = decoder->packet.payload; |
| 973 | uint32_t fc = decoder->packet.count; |
| 974 | uint32_t ctc_rem = ctc & decoder->ctc_rem_mask; |
| 975 | |
| 976 | if (!decoder->tsc_ctc_ratio_d) |
| 977 | return; |
| 978 | |
| 979 | decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff; |
| 980 | decoder->ctc_timestamp = decoder->tsc_timestamp - fc; |
| 981 | if (decoder->tsc_ctc_mult) { |
| 982 | decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult; |
| 983 | } else { |
| 984 | decoder->ctc_timestamp -= multdiv(ctc_rem, |
| 985 | decoder->tsc_ctc_ratio_n, |
| 986 | decoder->tsc_ctc_ratio_d); |
| 987 | } |
| 988 | decoder->ctc_delta = 0; |
| 989 | decoder->have_tma = true; |
| 990 | intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n", |
| 991 | decoder->ctc_timestamp, decoder->last_mtc, ctc_rem); |
| 992 | } |
| 993 | |
| 994 | static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder) |
| 995 | { |
| 996 | uint64_t timestamp; |
| 997 | uint32_t mtc, mtc_delta; |
| 998 | |
| 999 | if (!decoder->have_tma) |
| 1000 | return; |
| 1001 | |
| 1002 | mtc = decoder->packet.payload; |
| 1003 | |
| 1004 | if (mtc > decoder->last_mtc) |
| 1005 | mtc_delta = mtc - decoder->last_mtc; |
| 1006 | else |
| 1007 | mtc_delta = mtc + 256 - decoder->last_mtc; |
| 1008 | |
| 1009 | decoder->ctc_delta += mtc_delta << decoder->mtc_shift; |
| 1010 | |
| 1011 | if (decoder->tsc_ctc_mult) { |
| 1012 | timestamp = decoder->ctc_timestamp + |
| 1013 | decoder->ctc_delta * decoder->tsc_ctc_mult; |
| 1014 | } else { |
| 1015 | timestamp = decoder->ctc_timestamp + |
| 1016 | multdiv(decoder->ctc_delta, |
| 1017 | decoder->tsc_ctc_ratio_n, |
| 1018 | decoder->tsc_ctc_ratio_d); |
| 1019 | } |
| 1020 | |
| 1021 | if (timestamp < decoder->timestamp) |
| 1022 | intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n", |
| 1023 | timestamp, decoder->timestamp); |
| 1024 | else |
| 1025 | decoder->timestamp = timestamp; |
| 1026 | |
| 1027 | decoder->timestamp_insn_cnt = 0; |
| 1028 | decoder->last_mtc = mtc; |
| 1029 | } |
| 1030 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1031 | /* Walk PSB+ packets when already in sync. */ |
| 1032 | static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder) |
| 1033 | { |
| 1034 | int err; |
| 1035 | |
| 1036 | while (1) { |
| 1037 | err = intel_pt_get_next_packet(decoder); |
| 1038 | if (err) |
| 1039 | return err; |
| 1040 | |
| 1041 | switch (decoder->packet.type) { |
| 1042 | case INTEL_PT_PSBEND: |
| 1043 | return 0; |
| 1044 | |
| 1045 | case INTEL_PT_TIP_PGD: |
| 1046 | case INTEL_PT_TIP_PGE: |
| 1047 | case INTEL_PT_TIP: |
| 1048 | case INTEL_PT_TNT: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1049 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1050 | case INTEL_PT_BAD: |
| 1051 | case INTEL_PT_PSB: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1052 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1053 | intel_pt_log("ERROR: Unexpected packet\n"); |
| 1054 | return -EAGAIN; |
| 1055 | |
| 1056 | case INTEL_PT_OVF: |
| 1057 | return intel_pt_overflow(decoder); |
| 1058 | |
| 1059 | case INTEL_PT_TSC: |
| 1060 | intel_pt_calc_tsc_timestamp(decoder); |
| 1061 | break; |
| 1062 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1063 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1064 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1065 | break; |
| 1066 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1067 | case INTEL_PT_CBR: |
| 1068 | decoder->cbr = decoder->packet.payload; |
| 1069 | break; |
| 1070 | |
| 1071 | case INTEL_PT_MODE_EXEC: |
| 1072 | decoder->exec_mode = decoder->packet.payload; |
| 1073 | break; |
| 1074 | |
| 1075 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1076 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case INTEL_PT_FUP: |
| 1080 | decoder->pge = true; |
| 1081 | intel_pt_set_last_ip(decoder); |
| 1082 | break; |
| 1083 | |
| 1084 | case INTEL_PT_MODE_TSX: |
| 1085 | intel_pt_update_in_tx(decoder); |
| 1086 | break; |
| 1087 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1088 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1089 | intel_pt_calc_mtc_timestamp(decoder); |
| 1090 | if (decoder->period_type == INTEL_PT_PERIOD_MTC) |
| 1091 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1092 | break; |
| 1093 | |
| 1094 | case INTEL_PT_CYC: |
| 1095 | case INTEL_PT_VMCS: |
| 1096 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1097 | case INTEL_PT_PAD: |
| 1098 | default: |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder) |
| 1105 | { |
| 1106 | int err; |
| 1107 | |
| 1108 | if (decoder->tx_flags & INTEL_PT_ABORT_TX) { |
| 1109 | decoder->tx_flags = 0; |
| 1110 | decoder->state.flags &= ~INTEL_PT_IN_TX; |
| 1111 | decoder->state.flags |= INTEL_PT_ABORT_TX; |
| 1112 | } else { |
| 1113 | decoder->state.flags |= INTEL_PT_ASYNC; |
| 1114 | } |
| 1115 | |
| 1116 | while (1) { |
| 1117 | err = intel_pt_get_next_packet(decoder); |
| 1118 | if (err) |
| 1119 | return err; |
| 1120 | |
| 1121 | switch (decoder->packet.type) { |
| 1122 | case INTEL_PT_TNT: |
| 1123 | case INTEL_PT_FUP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1124 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1125 | case INTEL_PT_PSB: |
| 1126 | case INTEL_PT_TSC: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1127 | case INTEL_PT_TMA: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1128 | case INTEL_PT_CBR: |
| 1129 | case INTEL_PT_MODE_TSX: |
| 1130 | case INTEL_PT_BAD: |
| 1131 | case INTEL_PT_PSBEND: |
| 1132 | intel_pt_log("ERROR: Missing TIP after FUP\n"); |
| 1133 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 1134 | return -ENOENT; |
| 1135 | |
| 1136 | case INTEL_PT_OVF: |
| 1137 | return intel_pt_overflow(decoder); |
| 1138 | |
| 1139 | case INTEL_PT_TIP_PGD: |
| 1140 | decoder->state.from_ip = decoder->ip; |
| 1141 | decoder->state.to_ip = 0; |
| 1142 | if (decoder->packet.count != 0) { |
| 1143 | intel_pt_set_ip(decoder); |
| 1144 | intel_pt_log("Omitting PGD ip " x64_fmt "\n", |
| 1145 | decoder->ip); |
| 1146 | } |
| 1147 | decoder->pge = false; |
| 1148 | decoder->continuous_period = false; |
| 1149 | return 0; |
| 1150 | |
| 1151 | case INTEL_PT_TIP_PGE: |
| 1152 | decoder->pge = true; |
| 1153 | intel_pt_log("Omitting PGE ip " x64_fmt "\n", |
| 1154 | decoder->ip); |
| 1155 | decoder->state.from_ip = 0; |
| 1156 | if (decoder->packet.count == 0) { |
| 1157 | decoder->state.to_ip = 0; |
| 1158 | } else { |
| 1159 | intel_pt_set_ip(decoder); |
| 1160 | decoder->state.to_ip = decoder->ip; |
| 1161 | } |
| 1162 | return 0; |
| 1163 | |
| 1164 | case INTEL_PT_TIP: |
| 1165 | decoder->state.from_ip = decoder->ip; |
| 1166 | if (decoder->packet.count == 0) { |
| 1167 | decoder->state.to_ip = 0; |
| 1168 | } else { |
| 1169 | intel_pt_set_ip(decoder); |
| 1170 | decoder->state.to_ip = decoder->ip; |
| 1171 | } |
| 1172 | return 0; |
| 1173 | |
| 1174 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1175 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
| 1176 | break; |
| 1177 | |
| 1178 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1179 | intel_pt_calc_mtc_timestamp(decoder); |
| 1180 | if (decoder->period_type == INTEL_PT_PERIOD_MTC) |
| 1181 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1182 | break; |
| 1183 | |
| 1184 | case INTEL_PT_CYC: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1185 | break; |
| 1186 | |
| 1187 | case INTEL_PT_MODE_EXEC: |
| 1188 | decoder->exec_mode = decoder->packet.payload; |
| 1189 | break; |
| 1190 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1191 | case INTEL_PT_VMCS: |
| 1192 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1193 | case INTEL_PT_PAD: |
| 1194 | break; |
| 1195 | |
| 1196 | default: |
| 1197 | return intel_pt_bug(decoder); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | static int intel_pt_walk_trace(struct intel_pt_decoder *decoder) |
| 1203 | { |
| 1204 | bool no_tip = false; |
| 1205 | int err; |
| 1206 | |
| 1207 | while (1) { |
| 1208 | err = intel_pt_get_next_packet(decoder); |
| 1209 | if (err) |
| 1210 | return err; |
| 1211 | next: |
| 1212 | switch (decoder->packet.type) { |
| 1213 | case INTEL_PT_TNT: |
| 1214 | if (!decoder->packet.count) |
| 1215 | break; |
| 1216 | decoder->tnt = decoder->packet; |
| 1217 | decoder->pkt_state = INTEL_PT_STATE_TNT; |
| 1218 | err = intel_pt_walk_tnt(decoder); |
| 1219 | if (err == -EAGAIN) |
| 1220 | break; |
| 1221 | return err; |
| 1222 | |
| 1223 | case INTEL_PT_TIP_PGD: |
| 1224 | if (decoder->packet.count != 0) |
| 1225 | intel_pt_set_last_ip(decoder); |
| 1226 | decoder->pkt_state = INTEL_PT_STATE_TIP_PGD; |
| 1227 | return intel_pt_walk_tip(decoder); |
| 1228 | |
| 1229 | case INTEL_PT_TIP_PGE: { |
| 1230 | decoder->pge = true; |
| 1231 | if (decoder->packet.count == 0) { |
| 1232 | intel_pt_log_at("Skipping zero TIP.PGE", |
| 1233 | decoder->pos); |
| 1234 | break; |
| 1235 | } |
| 1236 | intel_pt_set_ip(decoder); |
| 1237 | decoder->state.from_ip = 0; |
| 1238 | decoder->state.to_ip = decoder->ip; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | case INTEL_PT_OVF: |
| 1243 | return intel_pt_overflow(decoder); |
| 1244 | |
| 1245 | case INTEL_PT_TIP: |
| 1246 | if (decoder->packet.count != 0) |
| 1247 | intel_pt_set_last_ip(decoder); |
| 1248 | decoder->pkt_state = INTEL_PT_STATE_TIP; |
| 1249 | return intel_pt_walk_tip(decoder); |
| 1250 | |
| 1251 | case INTEL_PT_FUP: |
| 1252 | if (decoder->packet.count == 0) { |
| 1253 | intel_pt_log_at("Skipping zero FUP", |
| 1254 | decoder->pos); |
| 1255 | no_tip = false; |
| 1256 | break; |
| 1257 | } |
| 1258 | intel_pt_set_last_ip(decoder); |
| 1259 | err = intel_pt_walk_fup(decoder); |
| 1260 | if (err != -EAGAIN) { |
| 1261 | if (err) |
| 1262 | return err; |
| 1263 | if (no_tip) |
| 1264 | decoder->pkt_state = |
| 1265 | INTEL_PT_STATE_FUP_NO_TIP; |
| 1266 | else |
| 1267 | decoder->pkt_state = INTEL_PT_STATE_FUP; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | if (no_tip) { |
| 1271 | no_tip = false; |
| 1272 | break; |
| 1273 | } |
| 1274 | return intel_pt_walk_fup_tip(decoder); |
| 1275 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1276 | case INTEL_PT_TRACESTOP: |
| 1277 | break; |
| 1278 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1279 | case INTEL_PT_PSB: |
| 1280 | intel_pt_clear_stack(&decoder->stack); |
| 1281 | err = intel_pt_walk_psbend(decoder); |
| 1282 | if (err == -EAGAIN) |
| 1283 | goto next; |
| 1284 | if (err) |
| 1285 | return err; |
| 1286 | break; |
| 1287 | |
| 1288 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1289 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
| 1290 | break; |
| 1291 | |
| 1292 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1293 | intel_pt_calc_mtc_timestamp(decoder); |
| 1294 | if (decoder->period_type != INTEL_PT_PERIOD_MTC) |
| 1295 | break; |
| 1296 | /* |
| 1297 | * Ensure that there has been an instruction since the |
| 1298 | * last MTC. |
| 1299 | */ |
| 1300 | if (!decoder->mtc_insn) |
| 1301 | break; |
| 1302 | decoder->mtc_insn = false; |
| 1303 | /* Ensure that there is a timestamp */ |
| 1304 | if (!decoder->timestamp) |
| 1305 | break; |
| 1306 | decoder->state.type = INTEL_PT_INSTRUCTION; |
| 1307 | decoder->state.from_ip = decoder->ip; |
| 1308 | decoder->state.to_ip = 0; |
| 1309 | decoder->mtc_insn = false; |
| 1310 | return 0; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1311 | |
| 1312 | case INTEL_PT_TSC: |
| 1313 | intel_pt_calc_tsc_timestamp(decoder); |
| 1314 | break; |
| 1315 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1316 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1317 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1318 | break; |
| 1319 | |
| 1320 | case INTEL_PT_CYC: |
| 1321 | break; |
| 1322 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1323 | case INTEL_PT_CBR: |
| 1324 | decoder->cbr = decoder->packet.payload; |
| 1325 | break; |
| 1326 | |
| 1327 | case INTEL_PT_MODE_EXEC: |
| 1328 | decoder->exec_mode = decoder->packet.payload; |
| 1329 | break; |
| 1330 | |
| 1331 | case INTEL_PT_MODE_TSX: |
| 1332 | /* MODE_TSX need not be followed by FUP */ |
| 1333 | if (!decoder->pge) { |
| 1334 | intel_pt_update_in_tx(decoder); |
| 1335 | break; |
| 1336 | } |
| 1337 | err = intel_pt_mode_tsx(decoder, &no_tip); |
| 1338 | if (err) |
| 1339 | return err; |
| 1340 | goto next; |
| 1341 | |
| 1342 | case INTEL_PT_BAD: /* Does not happen */ |
| 1343 | return intel_pt_bug(decoder); |
| 1344 | |
| 1345 | case INTEL_PT_PSBEND: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1346 | case INTEL_PT_VMCS: |
| 1347 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1348 | case INTEL_PT_PAD: |
| 1349 | break; |
| 1350 | |
| 1351 | default: |
| 1352 | return intel_pt_bug(decoder); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /* Walk PSB+ packets to get in sync. */ |
| 1358 | static int intel_pt_walk_psb(struct intel_pt_decoder *decoder) |
| 1359 | { |
| 1360 | int err; |
| 1361 | |
| 1362 | while (1) { |
| 1363 | err = intel_pt_get_next_packet(decoder); |
| 1364 | if (err) |
| 1365 | return err; |
| 1366 | |
| 1367 | switch (decoder->packet.type) { |
| 1368 | case INTEL_PT_TIP_PGD: |
| 1369 | decoder->continuous_period = false; |
| 1370 | case INTEL_PT_TIP_PGE: |
| 1371 | case INTEL_PT_TIP: |
| 1372 | intel_pt_log("ERROR: Unexpected packet\n"); |
| 1373 | return -ENOENT; |
| 1374 | |
| 1375 | case INTEL_PT_FUP: |
| 1376 | decoder->pge = true; |
| 1377 | if (decoder->last_ip || decoder->packet.count == 6 || |
| 1378 | decoder->packet.count == 0) { |
| 1379 | uint64_t current_ip = decoder->ip; |
| 1380 | |
| 1381 | intel_pt_set_ip(decoder); |
| 1382 | if (current_ip) |
| 1383 | intel_pt_log_to("Setting IP", |
| 1384 | decoder->ip); |
| 1385 | } |
| 1386 | break; |
| 1387 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1388 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1389 | intel_pt_calc_mtc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1390 | break; |
| 1391 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1392 | case INTEL_PT_TSC: |
| 1393 | intel_pt_calc_tsc_timestamp(decoder); |
| 1394 | break; |
| 1395 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1396 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1397 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1398 | break; |
| 1399 | |
| 1400 | case INTEL_PT_CYC: |
| 1401 | break; |
| 1402 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1403 | case INTEL_PT_CBR: |
| 1404 | decoder->cbr = decoder->packet.payload; |
| 1405 | break; |
| 1406 | |
| 1407 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1408 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1409 | break; |
| 1410 | |
| 1411 | case INTEL_PT_MODE_EXEC: |
| 1412 | decoder->exec_mode = decoder->packet.payload; |
| 1413 | break; |
| 1414 | |
| 1415 | case INTEL_PT_MODE_TSX: |
| 1416 | intel_pt_update_in_tx(decoder); |
| 1417 | break; |
| 1418 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1419 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1420 | case INTEL_PT_TNT: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1421 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1422 | intel_pt_log("ERROR: Unexpected packet\n"); |
| 1423 | if (decoder->ip) |
| 1424 | decoder->pkt_state = INTEL_PT_STATE_ERR4; |
| 1425 | else |
| 1426 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
| 1427 | return -ENOENT; |
| 1428 | |
| 1429 | case INTEL_PT_BAD: /* Does not happen */ |
| 1430 | return intel_pt_bug(decoder); |
| 1431 | |
| 1432 | case INTEL_PT_OVF: |
| 1433 | return intel_pt_overflow(decoder); |
| 1434 | |
| 1435 | case INTEL_PT_PSBEND: |
| 1436 | return 0; |
| 1437 | |
| 1438 | case INTEL_PT_PSB: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1439 | case INTEL_PT_VMCS: |
| 1440 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1441 | case INTEL_PT_PAD: |
| 1442 | default: |
| 1443 | break; |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder) |
| 1449 | { |
| 1450 | int err; |
| 1451 | |
| 1452 | while (1) { |
| 1453 | err = intel_pt_get_next_packet(decoder); |
| 1454 | if (err) |
| 1455 | return err; |
| 1456 | |
| 1457 | switch (decoder->packet.type) { |
| 1458 | case INTEL_PT_TIP_PGD: |
| 1459 | decoder->continuous_period = false; |
| 1460 | case INTEL_PT_TIP_PGE: |
| 1461 | case INTEL_PT_TIP: |
| 1462 | decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD; |
| 1463 | if (decoder->last_ip || decoder->packet.count == 6 || |
| 1464 | decoder->packet.count == 0) |
| 1465 | intel_pt_set_ip(decoder); |
| 1466 | if (decoder->ip) |
| 1467 | return 0; |
| 1468 | break; |
| 1469 | |
| 1470 | case INTEL_PT_FUP: |
| 1471 | if (decoder->overflow) { |
| 1472 | if (decoder->last_ip || |
| 1473 | decoder->packet.count == 6 || |
| 1474 | decoder->packet.count == 0) |
| 1475 | intel_pt_set_ip(decoder); |
| 1476 | if (decoder->ip) |
| 1477 | return 0; |
| 1478 | } |
| 1479 | if (decoder->packet.count) |
| 1480 | intel_pt_set_last_ip(decoder); |
| 1481 | break; |
| 1482 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1483 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1484 | intel_pt_calc_mtc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1485 | break; |
| 1486 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1487 | case INTEL_PT_TSC: |
| 1488 | intel_pt_calc_tsc_timestamp(decoder); |
| 1489 | break; |
| 1490 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1491 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1492 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1493 | break; |
| 1494 | |
| 1495 | case INTEL_PT_CYC: |
| 1496 | break; |
| 1497 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1498 | case INTEL_PT_CBR: |
| 1499 | decoder->cbr = decoder->packet.payload; |
| 1500 | break; |
| 1501 | |
| 1502 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1503 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1504 | break; |
| 1505 | |
| 1506 | case INTEL_PT_MODE_EXEC: |
| 1507 | decoder->exec_mode = decoder->packet.payload; |
| 1508 | break; |
| 1509 | |
| 1510 | case INTEL_PT_MODE_TSX: |
| 1511 | intel_pt_update_in_tx(decoder); |
| 1512 | break; |
| 1513 | |
| 1514 | case INTEL_PT_OVF: |
| 1515 | return intel_pt_overflow(decoder); |
| 1516 | |
| 1517 | case INTEL_PT_BAD: /* Does not happen */ |
| 1518 | return intel_pt_bug(decoder); |
| 1519 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1520 | case INTEL_PT_TRACESTOP: |
| 1521 | break; |
| 1522 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1523 | case INTEL_PT_PSB: |
| 1524 | err = intel_pt_walk_psb(decoder); |
| 1525 | if (err) |
| 1526 | return err; |
| 1527 | if (decoder->ip) { |
| 1528 | /* Do not have a sample */ |
| 1529 | decoder->state.type = 0; |
| 1530 | return 0; |
| 1531 | } |
| 1532 | break; |
| 1533 | |
| 1534 | case INTEL_PT_TNT: |
| 1535 | case INTEL_PT_PSBEND: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1536 | case INTEL_PT_VMCS: |
| 1537 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1538 | case INTEL_PT_PAD: |
| 1539 | default: |
| 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | static int intel_pt_sync_ip(struct intel_pt_decoder *decoder) |
| 1546 | { |
| 1547 | int err; |
| 1548 | |
| 1549 | intel_pt_log("Scanning for full IP\n"); |
| 1550 | err = intel_pt_walk_to_ip(decoder); |
| 1551 | if (err) |
| 1552 | return err; |
| 1553 | |
| 1554 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 1555 | decoder->overflow = false; |
| 1556 | |
| 1557 | decoder->state.from_ip = 0; |
| 1558 | decoder->state.to_ip = decoder->ip; |
| 1559 | intel_pt_log_to("Setting IP", decoder->ip); |
| 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static int intel_pt_part_psb(struct intel_pt_decoder *decoder) |
| 1565 | { |
| 1566 | const unsigned char *end = decoder->buf + decoder->len; |
| 1567 | size_t i; |
| 1568 | |
| 1569 | for (i = INTEL_PT_PSB_LEN - 1; i; i--) { |
| 1570 | if (i > decoder->len) |
| 1571 | continue; |
| 1572 | if (!memcmp(end - i, INTEL_PT_PSB_STR, i)) |
| 1573 | return i; |
| 1574 | } |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
| 1578 | static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb) |
| 1579 | { |
| 1580 | size_t rest_psb = INTEL_PT_PSB_LEN - part_psb; |
| 1581 | const char *psb = INTEL_PT_PSB_STR; |
| 1582 | |
| 1583 | if (rest_psb > decoder->len || |
| 1584 | memcmp(decoder->buf, psb + part_psb, rest_psb)) |
| 1585 | return 0; |
| 1586 | |
| 1587 | return rest_psb; |
| 1588 | } |
| 1589 | |
| 1590 | static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder, |
| 1591 | int part_psb) |
| 1592 | { |
| 1593 | int rest_psb, ret; |
| 1594 | |
| 1595 | decoder->pos += decoder->len; |
| 1596 | decoder->len = 0; |
| 1597 | |
| 1598 | ret = intel_pt_get_next_data(decoder); |
| 1599 | if (ret) |
| 1600 | return ret; |
| 1601 | |
| 1602 | rest_psb = intel_pt_rest_psb(decoder, part_psb); |
| 1603 | if (!rest_psb) |
| 1604 | return 0; |
| 1605 | |
| 1606 | decoder->pos -= part_psb; |
| 1607 | decoder->next_buf = decoder->buf + rest_psb; |
| 1608 | decoder->next_len = decoder->len - rest_psb; |
| 1609 | memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN); |
| 1610 | decoder->buf = decoder->temp_buf; |
| 1611 | decoder->len = INTEL_PT_PSB_LEN; |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder) |
| 1617 | { |
| 1618 | unsigned char *next; |
| 1619 | int ret; |
| 1620 | |
| 1621 | intel_pt_log("Scanning for PSB\n"); |
| 1622 | while (1) { |
| 1623 | if (!decoder->len) { |
| 1624 | ret = intel_pt_get_next_data(decoder); |
| 1625 | if (ret) |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
| 1629 | next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR, |
| 1630 | INTEL_PT_PSB_LEN); |
| 1631 | if (!next) { |
| 1632 | int part_psb; |
| 1633 | |
| 1634 | part_psb = intel_pt_part_psb(decoder); |
| 1635 | if (part_psb) { |
| 1636 | ret = intel_pt_get_split_psb(decoder, part_psb); |
| 1637 | if (ret) |
| 1638 | return ret; |
| 1639 | } else { |
| 1640 | decoder->pos += decoder->len; |
| 1641 | decoder->len = 0; |
| 1642 | } |
| 1643 | continue; |
| 1644 | } |
| 1645 | |
| 1646 | decoder->pkt_step = next - decoder->buf; |
| 1647 | return intel_pt_get_next_packet(decoder); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static int intel_pt_sync(struct intel_pt_decoder *decoder) |
| 1652 | { |
| 1653 | int err; |
| 1654 | |
| 1655 | decoder->pge = false; |
| 1656 | decoder->continuous_period = false; |
| 1657 | decoder->last_ip = 0; |
| 1658 | decoder->ip = 0; |
| 1659 | intel_pt_clear_stack(&decoder->stack); |
| 1660 | |
| 1661 | err = intel_pt_scan_for_psb(decoder); |
| 1662 | if (err) |
| 1663 | return err; |
| 1664 | |
| 1665 | decoder->pkt_state = INTEL_PT_STATE_NO_IP; |
| 1666 | |
| 1667 | err = intel_pt_walk_psb(decoder); |
| 1668 | if (err) |
| 1669 | return err; |
| 1670 | |
| 1671 | if (decoder->ip) { |
| 1672 | decoder->state.type = 0; /* Do not have a sample */ |
| 1673 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 1674 | } else { |
| 1675 | return intel_pt_sync_ip(decoder); |
| 1676 | } |
| 1677 | |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder) |
| 1682 | { |
| 1683 | uint64_t est = decoder->timestamp_insn_cnt << 1; |
| 1684 | |
| 1685 | if (!decoder->cbr || !decoder->max_non_turbo_ratio) |
| 1686 | goto out; |
| 1687 | |
| 1688 | est *= decoder->max_non_turbo_ratio; |
| 1689 | est /= decoder->cbr; |
| 1690 | out: |
| 1691 | return decoder->timestamp + est; |
| 1692 | } |
| 1693 | |
| 1694 | const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder) |
| 1695 | { |
| 1696 | int err; |
| 1697 | |
| 1698 | do { |
| 1699 | decoder->state.type = INTEL_PT_BRANCH; |
| 1700 | decoder->state.flags = 0; |
| 1701 | |
| 1702 | switch (decoder->pkt_state) { |
| 1703 | case INTEL_PT_STATE_NO_PSB: |
| 1704 | err = intel_pt_sync(decoder); |
| 1705 | break; |
| 1706 | case INTEL_PT_STATE_NO_IP: |
| 1707 | decoder->last_ip = 0; |
| 1708 | /* Fall through */ |
| 1709 | case INTEL_PT_STATE_ERR_RESYNC: |
| 1710 | err = intel_pt_sync_ip(decoder); |
| 1711 | break; |
| 1712 | case INTEL_PT_STATE_IN_SYNC: |
| 1713 | err = intel_pt_walk_trace(decoder); |
| 1714 | break; |
| 1715 | case INTEL_PT_STATE_TNT: |
| 1716 | err = intel_pt_walk_tnt(decoder); |
| 1717 | if (err == -EAGAIN) |
| 1718 | err = intel_pt_walk_trace(decoder); |
| 1719 | break; |
| 1720 | case INTEL_PT_STATE_TIP: |
| 1721 | case INTEL_PT_STATE_TIP_PGD: |
| 1722 | err = intel_pt_walk_tip(decoder); |
| 1723 | break; |
| 1724 | case INTEL_PT_STATE_FUP: |
| 1725 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 1726 | err = intel_pt_walk_fup(decoder); |
| 1727 | if (err == -EAGAIN) |
| 1728 | err = intel_pt_walk_fup_tip(decoder); |
| 1729 | else if (!err) |
| 1730 | decoder->pkt_state = INTEL_PT_STATE_FUP; |
| 1731 | break; |
| 1732 | case INTEL_PT_STATE_FUP_NO_TIP: |
| 1733 | decoder->pkt_state = INTEL_PT_STATE_IN_SYNC; |
| 1734 | err = intel_pt_walk_fup(decoder); |
| 1735 | if (err == -EAGAIN) |
| 1736 | err = intel_pt_walk_trace(decoder); |
| 1737 | break; |
| 1738 | default: |
| 1739 | err = intel_pt_bug(decoder); |
| 1740 | break; |
| 1741 | } |
| 1742 | } while (err == -ENOLINK); |
| 1743 | |
| 1744 | decoder->state.err = err ? intel_pt_ext_err(err) : 0; |
| 1745 | decoder->state.timestamp = decoder->timestamp; |
| 1746 | decoder->state.est_timestamp = intel_pt_est_timestamp(decoder); |
| 1747 | decoder->state.cr3 = decoder->cr3; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 1748 | decoder->state.tot_insn_cnt = decoder->tot_insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1749 | |
| 1750 | if (err) |
| 1751 | decoder->state.from_ip = decoder->ip; |
| 1752 | |
| 1753 | return &decoder->state; |
| 1754 | } |
| 1755 | |
| 1756 | static bool intel_pt_at_psb(unsigned char *buf, size_t len) |
| 1757 | { |
| 1758 | if (len < INTEL_PT_PSB_LEN) |
| 1759 | return false; |
| 1760 | return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR, |
| 1761 | INTEL_PT_PSB_LEN); |
| 1762 | } |
| 1763 | |
| 1764 | /** |
| 1765 | * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet. |
| 1766 | * @buf: pointer to buffer pointer |
| 1767 | * @len: size of buffer |
| 1768 | * |
| 1769 | * Updates the buffer pointer to point to the start of the next PSB packet if |
| 1770 | * there is one, otherwise the buffer pointer is unchanged. If @buf is updated, |
| 1771 | * @len is adjusted accordingly. |
| 1772 | * |
| 1773 | * Return: %true if a PSB packet is found, %false otherwise. |
| 1774 | */ |
| 1775 | static bool intel_pt_next_psb(unsigned char **buf, size_t *len) |
| 1776 | { |
| 1777 | unsigned char *next; |
| 1778 | |
| 1779 | next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN); |
| 1780 | if (next) { |
| 1781 | *len -= next - *buf; |
| 1782 | *buf = next; |
| 1783 | return true; |
| 1784 | } |
| 1785 | return false; |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * intel_pt_step_psb - move buffer pointer to the start of the following PSB |
| 1790 | * packet. |
| 1791 | * @buf: pointer to buffer pointer |
| 1792 | * @len: size of buffer |
| 1793 | * |
| 1794 | * Updates the buffer pointer to point to the start of the following PSB packet |
| 1795 | * (skipping the PSB at @buf itself) if there is one, otherwise the buffer |
| 1796 | * pointer is unchanged. If @buf is updated, @len is adjusted accordingly. |
| 1797 | * |
| 1798 | * Return: %true if a PSB packet is found, %false otherwise. |
| 1799 | */ |
| 1800 | static bool intel_pt_step_psb(unsigned char **buf, size_t *len) |
| 1801 | { |
| 1802 | unsigned char *next; |
| 1803 | |
| 1804 | if (!*len) |
| 1805 | return false; |
| 1806 | |
| 1807 | next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN); |
| 1808 | if (next) { |
| 1809 | *len -= next - *buf; |
| 1810 | *buf = next; |
| 1811 | return true; |
| 1812 | } |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * intel_pt_last_psb - find the last PSB packet in a buffer. |
| 1818 | * @buf: buffer |
| 1819 | * @len: size of buffer |
| 1820 | * |
| 1821 | * This function finds the last PSB in a buffer. |
| 1822 | * |
| 1823 | * Return: A pointer to the last PSB in @buf if found, %NULL otherwise. |
| 1824 | */ |
| 1825 | static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len) |
| 1826 | { |
| 1827 | const char *n = INTEL_PT_PSB_STR; |
| 1828 | unsigned char *p; |
| 1829 | size_t k; |
| 1830 | |
| 1831 | if (len < INTEL_PT_PSB_LEN) |
| 1832 | return NULL; |
| 1833 | |
| 1834 | k = len - INTEL_PT_PSB_LEN + 1; |
| 1835 | while (1) { |
| 1836 | p = memrchr(buf, n[0], k); |
| 1837 | if (!p) |
| 1838 | return NULL; |
| 1839 | if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1)) |
| 1840 | return p; |
| 1841 | k = p - buf; |
| 1842 | if (!k) |
| 1843 | return NULL; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /** |
| 1848 | * intel_pt_next_tsc - find and return next TSC. |
| 1849 | * @buf: buffer |
| 1850 | * @len: size of buffer |
| 1851 | * @tsc: TSC value returned |
| 1852 | * |
| 1853 | * Find a TSC packet in @buf and return the TSC value. This function assumes |
| 1854 | * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a |
| 1855 | * PSBEND packet is found. |
| 1856 | * |
| 1857 | * Return: %true if TSC is found, false otherwise. |
| 1858 | */ |
| 1859 | static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc) |
| 1860 | { |
| 1861 | struct intel_pt_pkt packet; |
| 1862 | int ret; |
| 1863 | |
| 1864 | while (len) { |
| 1865 | ret = intel_pt_get_packet(buf, len, &packet); |
| 1866 | if (ret <= 0) |
| 1867 | return false; |
| 1868 | if (packet.type == INTEL_PT_TSC) { |
| 1869 | *tsc = packet.payload; |
| 1870 | return true; |
| 1871 | } |
| 1872 | if (packet.type == INTEL_PT_PSBEND) |
| 1873 | return false; |
| 1874 | buf += ret; |
| 1875 | len -= ret; |
| 1876 | } |
| 1877 | return false; |
| 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * intel_pt_tsc_cmp - compare 7-byte TSCs. |
| 1882 | * @tsc1: first TSC to compare |
| 1883 | * @tsc2: second TSC to compare |
| 1884 | * |
| 1885 | * This function compares 7-byte TSC values allowing for the possibility that |
| 1886 | * TSC wrapped around. Generally it is not possible to know if TSC has wrapped |
| 1887 | * around so for that purpose this function assumes the absolute difference is |
| 1888 | * less than half the maximum difference. |
| 1889 | * |
| 1890 | * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is |
| 1891 | * after @tsc2. |
| 1892 | */ |
| 1893 | static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2) |
| 1894 | { |
| 1895 | const uint64_t halfway = (1ULL << 55); |
| 1896 | |
| 1897 | if (tsc1 == tsc2) |
| 1898 | return 0; |
| 1899 | |
| 1900 | if (tsc1 < tsc2) { |
| 1901 | if (tsc2 - tsc1 < halfway) |
| 1902 | return -1; |
| 1903 | else |
| 1904 | return 1; |
| 1905 | } else { |
| 1906 | if (tsc1 - tsc2 < halfway) |
| 1907 | return 1; |
| 1908 | else |
| 1909 | return -1; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data |
| 1915 | * using TSC. |
| 1916 | * @buf_a: first buffer |
| 1917 | * @len_a: size of first buffer |
| 1918 | * @buf_b: second buffer |
| 1919 | * @len_b: size of second buffer |
| 1920 | * |
| 1921 | * If the trace contains TSC we can look at the last TSC of @buf_a and the |
| 1922 | * first TSC of @buf_b in order to determine if the buffers overlap, and then |
| 1923 | * walk forward in @buf_b until a later TSC is found. A precondition is that |
| 1924 | * @buf_a and @buf_b are positioned at a PSB. |
| 1925 | * |
| 1926 | * Return: A pointer into @buf_b from where non-overlapped data starts, or |
| 1927 | * @buf_b + @len_b if there is no non-overlapped data. |
| 1928 | */ |
| 1929 | static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a, |
| 1930 | size_t len_a, |
| 1931 | unsigned char *buf_b, |
| 1932 | size_t len_b) |
| 1933 | { |
| 1934 | uint64_t tsc_a, tsc_b; |
| 1935 | unsigned char *p; |
| 1936 | size_t len; |
| 1937 | |
| 1938 | p = intel_pt_last_psb(buf_a, len_a); |
| 1939 | if (!p) |
| 1940 | return buf_b; /* No PSB in buf_a => no overlap */ |
| 1941 | |
| 1942 | len = len_a - (p - buf_a); |
| 1943 | if (!intel_pt_next_tsc(p, len, &tsc_a)) { |
| 1944 | /* The last PSB+ in buf_a is incomplete, so go back one more */ |
| 1945 | len_a -= len; |
| 1946 | p = intel_pt_last_psb(buf_a, len_a); |
| 1947 | if (!p) |
| 1948 | return buf_b; /* No full PSB+ => assume no overlap */ |
| 1949 | len = len_a - (p - buf_a); |
| 1950 | if (!intel_pt_next_tsc(p, len, &tsc_a)) |
| 1951 | return buf_b; /* No TSC in buf_a => assume no overlap */ |
| 1952 | } |
| 1953 | |
| 1954 | while (1) { |
| 1955 | /* Ignore PSB+ with no TSC */ |
| 1956 | if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) && |
| 1957 | intel_pt_tsc_cmp(tsc_a, tsc_b) < 0) |
| 1958 | return buf_b; /* tsc_a < tsc_b => no overlap */ |
| 1959 | |
| 1960 | if (!intel_pt_step_psb(&buf_b, &len_b)) |
| 1961 | return buf_b + len_b; /* No PSB in buf_b => no data */ |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /** |
| 1966 | * intel_pt_find_overlap - determine start of non-overlapped trace data. |
| 1967 | * @buf_a: first buffer |
| 1968 | * @len_a: size of first buffer |
| 1969 | * @buf_b: second buffer |
| 1970 | * @len_b: size of second buffer |
| 1971 | * @have_tsc: can use TSC packets to detect overlap |
| 1972 | * |
| 1973 | * When trace samples or snapshots are recorded there is the possibility that |
| 1974 | * the data overlaps. Note that, for the purposes of decoding, data is only |
| 1975 | * useful if it begins with a PSB packet. |
| 1976 | * |
| 1977 | * Return: A pointer into @buf_b from where non-overlapped data starts, or |
| 1978 | * @buf_b + @len_b if there is no non-overlapped data. |
| 1979 | */ |
| 1980 | unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a, |
| 1981 | unsigned char *buf_b, size_t len_b, |
| 1982 | bool have_tsc) |
| 1983 | { |
| 1984 | unsigned char *found; |
| 1985 | |
| 1986 | /* Buffer 'b' must start at PSB so throw away everything before that */ |
| 1987 | if (!intel_pt_next_psb(&buf_b, &len_b)) |
| 1988 | return buf_b + len_b; /* No PSB */ |
| 1989 | |
| 1990 | if (!intel_pt_next_psb(&buf_a, &len_a)) |
| 1991 | return buf_b; /* No overlap */ |
| 1992 | |
| 1993 | if (have_tsc) { |
| 1994 | found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b); |
| 1995 | if (found) |
| 1996 | return found; |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes, |
| 2001 | * we can ignore the first part of buffer 'a'. |
| 2002 | */ |
| 2003 | while (len_b < len_a) { |
| 2004 | if (!intel_pt_step_psb(&buf_a, &len_a)) |
| 2005 | return buf_b; /* No overlap */ |
| 2006 | } |
| 2007 | |
| 2008 | /* Now len_b >= len_a */ |
| 2009 | if (len_b > len_a) { |
| 2010 | /* The leftover buffer 'b' must start at a PSB */ |
| 2011 | while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) { |
| 2012 | if (!intel_pt_step_psb(&buf_a, &len_a)) |
| 2013 | return buf_b; /* No overlap */ |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | while (1) { |
| 2018 | /* Potential overlap so check the bytes */ |
| 2019 | found = memmem(buf_a, len_a, buf_b, len_a); |
| 2020 | if (found) |
| 2021 | return buf_b + len_a; |
| 2022 | |
| 2023 | /* Try again at next PSB in buffer 'a' */ |
| 2024 | if (!intel_pt_step_psb(&buf_a, &len_a)) |
| 2025 | return buf_b; /* No overlap */ |
| 2026 | |
| 2027 | /* The leftover buffer 'b' must start at a PSB */ |
| 2028 | while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) { |
| 2029 | if (!intel_pt_step_psb(&buf_a, &len_a)) |
| 2030 | return buf_b; /* No overlap */ |
| 2031 | } |
| 2032 | } |
| 2033 | } |