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> |
Arnaldo Carvalho de Melo | 7ea6856 | 2017-02-09 15:22:22 -0300 | [diff] [blame] | 25 | #include <linux/compiler.h> |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 26 | |
| 27 | #include "../cache.h" |
| 28 | #include "../util.h" |
Adrian Hunter | 5a99d99 | 2019-02-06 12:39:44 +0200 | [diff] [blame] | 29 | #include "../auxtrace.h" |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 30 | |
| 31 | #include "intel-pt-insn-decoder.h" |
| 32 | #include "intel-pt-pkt-decoder.h" |
| 33 | #include "intel-pt-decoder.h" |
| 34 | #include "intel-pt-log.h" |
| 35 | |
| 36 | #define INTEL_PT_BLK_SIZE 1024 |
| 37 | |
| 38 | #define BIT63 (((uint64_t)1 << 63)) |
| 39 | |
| 40 | #define INTEL_PT_RETURN 1 |
| 41 | |
| 42 | /* Maximum number of loops with no packets consumed i.e. stuck in a loop */ |
| 43 | #define INTEL_PT_MAX_LOOPS 10000 |
| 44 | |
| 45 | struct intel_pt_blk { |
| 46 | struct intel_pt_blk *prev; |
| 47 | uint64_t ip[INTEL_PT_BLK_SIZE]; |
| 48 | }; |
| 49 | |
| 50 | struct intel_pt_stack { |
| 51 | struct intel_pt_blk *blk; |
| 52 | struct intel_pt_blk *spare; |
| 53 | int pos; |
| 54 | }; |
| 55 | |
| 56 | enum intel_pt_pkt_state { |
| 57 | INTEL_PT_STATE_NO_PSB, |
| 58 | INTEL_PT_STATE_NO_IP, |
| 59 | INTEL_PT_STATE_ERR_RESYNC, |
| 60 | INTEL_PT_STATE_IN_SYNC, |
Adrian Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 61 | INTEL_PT_STATE_TNT_CONT, |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 62 | 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 Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 69 | static 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 Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 76 | case INTEL_PT_STATE_TNT_CONT: |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 77 | return true; |
Adrian Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 78 | case INTEL_PT_STATE_TNT: |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 79 | case INTEL_PT_STATE_TIP: |
| 80 | case INTEL_PT_STATE_TIP_PGD: |
| 81 | case INTEL_PT_STATE_FUP: |
| 82 | case INTEL_PT_STATE_FUP_NO_TIP: |
| 83 | return false; |
| 84 | default: |
| 85 | return true; |
| 86 | }; |
| 87 | } |
| 88 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 89 | #ifdef INTEL_PT_STRICT |
| 90 | #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB |
| 91 | #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB |
| 92 | #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB |
| 93 | #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB |
| 94 | #else |
| 95 | #define INTEL_PT_STATE_ERR1 (decoder->pkt_state) |
| 96 | #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP |
| 97 | #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC |
| 98 | #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC |
| 99 | #endif |
| 100 | |
| 101 | struct intel_pt_decoder { |
| 102 | int (*get_trace)(struct intel_pt_buffer *buffer, void *data); |
| 103 | int (*walk_insn)(struct intel_pt_insn *intel_pt_insn, |
| 104 | uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip, |
| 105 | uint64_t max_insn_cnt, void *data); |
Adrian Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 106 | bool (*pgd_ip)(uint64_t ip, void *data); |
Adrian Hunter | 4d678e9 | 2019-06-04 16:00:02 +0300 | [diff] [blame] | 107 | int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 108 | void *data; |
| 109 | struct intel_pt_state state; |
| 110 | const unsigned char *buf; |
| 111 | size_t len; |
| 112 | bool return_compression; |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 113 | bool branch_enable; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 114 | bool mtc_insn; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 115 | bool pge; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 116 | bool have_tma; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 117 | bool have_cyc; |
Adrian Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 118 | bool fixup_last_mtc; |
Adrian Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 119 | bool have_last_ip; |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 120 | bool in_psb; |
Adrian Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 121 | enum intel_pt_param_flags flags; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 122 | 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 Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 129 | uint64_t sample_timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 130 | uint64_t ret_addr; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 131 | uint64_t ctc_timestamp; |
| 132 | uint64_t ctc_delta; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 133 | uint64_t cycle_cnt; |
| 134 | uint64_t cyc_ref_timestamp; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 135 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 142 | 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 148 | int last_packet_type; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 149 | unsigned int cbr; |
Adrian Hunter | 0a7c700d | 2017-05-26 11:17:16 +0300 | [diff] [blame] | 150 | unsigned int cbr_seen; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 151 | unsigned int max_non_turbo_ratio; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 152 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 156 | int exec_mode; |
| 157 | unsigned int insn_bytes; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 158 | uint64_t period; |
| 159 | enum intel_pt_period_type period_type; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 160 | uint64_t tot_insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 161 | uint64_t period_insn_cnt; |
| 162 | uint64_t period_mask; |
| 163 | uint64_t period_ticks; |
| 164 | uint64_t last_masked_timestamp; |
Adrian Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 165 | uint64_t tot_cyc_cnt; |
| 166 | uint64_t sample_tot_cyc_cnt; |
Adrian Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 167 | uint64_t base_cyc_cnt; |
| 168 | uint64_t cyc_cnt_timestamp; |
| 169 | double tsc_to_cyc; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 170 | bool continuous_period; |
| 171 | bool overflow; |
| 172 | bool set_fup_tx_flags; |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 173 | bool set_fup_ptw; |
| 174 | bool set_fup_mwait; |
| 175 | bool set_fup_pwre; |
| 176 | bool set_fup_exstop; |
Adrian Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 177 | bool sample_cyc; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 178 | unsigned int fup_tx_flags; |
| 179 | unsigned int tx_flags; |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 180 | uint64_t fup_ptw_payload; |
| 181 | uint64_t fup_mwait_payload; |
| 182 | uint64_t fup_pwre_payload; |
Adrian Hunter | 0a7c700d | 2017-05-26 11:17:16 +0300 | [diff] [blame] | 183 | uint64_t cbr_payload; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 184 | uint64_t timestamp_insn_cnt; |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 185 | uint64_t sample_insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 186 | 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 | |
| 195 | static 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 | |
| 205 | static 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 216 | static 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 223 | struct 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 Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 236 | decoder->pgd_ip = params->pgd_ip; |
Adrian Hunter | 4d678e9 | 2019-06-04 16:00:02 +0300 | [diff] [blame] | 237 | decoder->lookahead = params->lookahead; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 238 | decoder->data = params->data; |
| 239 | decoder->return_compression = params->return_compression; |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 240 | decoder->branch_enable = params->branch_enable; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 241 | |
Adrian Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 242 | decoder->flags = params->flags; |
| 243 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 244 | decoder->period = params->period; |
| 245 | decoder->period_type = params->period_type; |
| 246 | |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 247 | decoder->max_non_turbo_ratio = params->max_non_turbo_ratio; |
| 248 | decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 249 | |
| 250 | intel_pt_setup_period(decoder); |
| 251 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 252 | 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 265 | } |
Adrian Hunter | f3b4e06 | 2019-03-25 15:51:35 +0200 | [diff] [blame] | 266 | |
| 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 274 | |
| 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 281 | return decoder; |
| 282 | } |
| 283 | |
| 284 | static 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 | |
| 295 | static 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 | |
| 308 | static 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 | |
| 327 | static 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 | |
| 341 | static 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 | |
| 348 | static 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 | |
| 355 | void intel_pt_decoder_free(struct intel_pt_decoder *decoder) |
| 356 | { |
| 357 | intel_pt_free_stack(&decoder->stack); |
| 358 | free(decoder); |
| 359 | } |
| 360 | |
| 361 | static 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 | |
| 387 | static 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 | |
| 400 | int intel_pt__strerror(int code, char *buf, size_t buflen) |
| 401 | { |
Colin Ian King | c066489 | 2016-04-24 19:56:43 +0100 | [diff] [blame] | 402 | if (code < 1 || code >= INTEL_PT_ERR_MAX) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 403 | code = INTEL_PT_ERR_UNK; |
| 404 | strlcpy(buf, intel_pt_err_msgs[code], buflen); |
| 405 | return 0; |
| 406 | } |
| 407 | |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 408 | static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet, |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 409 | uint64_t last_ip) |
| 410 | { |
| 411 | uint64_t ip; |
| 412 | |
| 413 | switch (packet->count) { |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 414 | case 1: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 415 | ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) | |
| 416 | packet->payload; |
| 417 | break; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 418 | case 2: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 419 | ip = (last_ip & (uint64_t)0xffffffff00000000ULL) | |
| 420 | packet->payload; |
| 421 | break; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 422 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 432 | case 6: |
| 433 | ip = packet->payload; |
| 434 | break; |
| 435 | default: |
| 436 | return 0; |
| 437 | } |
| 438 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 439 | return ip; |
| 440 | } |
| 441 | |
| 442 | static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder) |
| 443 | { |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 444 | decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip); |
Adrian Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 445 | decoder->have_last_ip = true; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static 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 | |
| 454 | static 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 | |
| 460 | static 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 | |
| 467 | static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder) |
| 468 | { |
| 469 | decoder->tx_flags = 0; |
| 470 | } |
| 471 | |
| 472 | static 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 | |
| 477 | static int intel_pt_bad_packet(struct intel_pt_decoder *decoder) |
| 478 | { |
| 479 | intel_pt_clear_tx_flags(decoder); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 480 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 481 | 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 Hunter | 948e9dc | 2019-05-20 14:37:10 +0300 | [diff] [blame] | 491 | static 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 Hunter | 6492e5f | 2019-06-04 16:00:04 +0300 | [diff] [blame] | 497 | static 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 505 | static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 506 | { |
| 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 522 | if (!buffer.consecutive || reposition) { |
Adrian Hunter | 6492e5f | 2019-06-04 16:00:04 +0300 | [diff] [blame] | 523 | intel_pt_reposition(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 524 | decoder->ref_timestamp = buffer.ref_timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 525 | 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 534 | static int intel_pt_get_next_data(struct intel_pt_decoder *decoder, |
| 535 | bool reposition) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 536 | { |
| 537 | if (!decoder->next_buf) |
Adrian Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 538 | return intel_pt_get_data(decoder, reposition); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 539 | |
| 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 | |
| 547 | static 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 557 | ret = intel_pt_get_data(decoder, false); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 558 | 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 587 | struct 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 | |
| 596 | typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info); |
| 597 | |
| 598 | /* Lookahead packets in current buffer */ |
| 599 | static 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 | |
| 639 | struct 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 Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 648 | bool fixup_last_mtc; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 649 | bool from_mtc; |
| 650 | double cbr_cyc_to_tsc; |
| 651 | }; |
| 652 | |
Adrian Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 653 | /* |
| 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 | */ |
| 660 | static 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 673 | static 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 Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 695 | case INTEL_PT_PTWRITE: |
| 696 | case INTEL_PT_PTWRITE_IP: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 697 | return 0; |
| 698 | |
| 699 | case INTEL_PT_MTC: |
| 700 | if (!data->have_tma) |
| 701 | return 0; |
| 702 | |
| 703 | mtc = pkt_info->packet.payload; |
Adrian Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 704 | 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 709 | 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 Hunter | 38b65b0 | 2017-05-26 11:17:37 +0300 | [diff] [blame] | 737 | /* |
| 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 743 | 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 Hunter | 9992c2d | 2015-09-25 16:15:34 +0300 | [diff] [blame] | 748 | if (timestamp < data->timestamp) |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 749 | 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 Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 783 | data->fixup_last_mtc = true; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 784 | |
| 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 Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 801 | 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 806 | 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 | |
| 843 | static 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 Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 855 | .fixup_last_mtc = decoder->fixup_last_mtc, |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 856 | .from_mtc = from_mtc, |
| 857 | .cbr_cyc_to_tsc = 0, |
| 858 | }; |
| 859 | |
Adrian Hunter | 38b65b0 | 2017-05-26 11:17:37 +0300 | [diff] [blame] | 860 | /* |
| 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 Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 868 | intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data); |
| 869 | } |
| 870 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 871 | static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder) |
| 872 | { |
| 873 | int ret; |
| 874 | |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 875 | decoder->last_packet_type = decoder->packet.type; |
| 876 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 877 | 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 883 | ret = intel_pt_get_next_data(decoder, false); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 884 | if (ret) |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | ret = intel_pt_get_packet(decoder->buf, decoder->len, |
| 889 | &decoder->packet); |
Adrian Hunter | 26ee2bc | 2019-02-06 12:39:46 +0200 | [diff] [blame] | 890 | if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 && |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 891 | 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 | |
| 907 | static 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 Hunter | 7ba8fa2 | 2019-05-10 15:41:41 +0300 | [diff] [blame] | 914 | if (masked_timestamp > decoder->last_masked_timestamp) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 915 | return 1; |
| 916 | } else { |
| 917 | timestamp += 1; |
| 918 | masked_timestamp = timestamp & decoder->period_mask; |
Adrian Hunter | 7ba8fa2 | 2019-05-10 15:41:41 +0300 | [diff] [blame] | 919 | if (masked_timestamp > decoder->last_masked_timestamp) { |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 920 | decoder->last_masked_timestamp = masked_timestamp; |
| 921 | decoder->continuous_period = true; |
| 922 | } |
| 923 | } |
Adrian Hunter | 7ba8fa2 | 2019-05-10 15:41:41 +0300 | [diff] [blame] | 924 | |
| 925 | if (masked_timestamp < decoder->last_masked_timestamp) |
| 926 | return decoder->period_ticks; |
| 927 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 928 | return decoder->period_ticks - (timestamp - masked_timestamp); |
| 929 | } |
| 930 | |
| 931 | static 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 939 | case INTEL_PT_PERIOD_MTC: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 940 | default: |
| 941 | return 0; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | static 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 Hunter | 7ba8fa2 | 2019-05-10 15:41:41 +0300 | [diff] [blame] | 956 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 960 | break; |
| 961 | case INTEL_PT_PERIOD_NONE: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 962 | case INTEL_PT_PERIOD_MTC: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 963 | default: |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
| 968 | } |
| 969 | |
| 970 | static 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 976 | if (!decoder->mtc_insn) |
| 977 | decoder->mtc_insn = true; |
| 978 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 979 | 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 Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 984 | decoder->tot_insn_cnt += insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 985 | decoder->timestamp_insn_cnt += insn_cnt; |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 986 | decoder->sample_insn_cnt += insn_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 987 | 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 | } |
| 1065 | out: |
| 1066 | decoder->no_progress = 0; |
| 1067 | out_no_progress: |
| 1068 | decoder->state.insn_op = intel_pt_insn->op; |
| 1069 | decoder->state.insn_len = intel_pt_insn->length; |
Andi Kleen | faaa876 | 2016-10-07 16:42:26 +0300 | [diff] [blame] | 1070 | memcpy(decoder->state.insn, intel_pt_insn->buf, |
| 1071 | INTEL_PT_INSN_BUF_SZ); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1072 | |
| 1073 | if (decoder->tx_flags & INTEL_PT_IN_TX) |
| 1074 | decoder->state.flags |= INTEL_PT_IN_TX; |
| 1075 | |
| 1076 | return err; |
| 1077 | } |
| 1078 | |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1079 | static 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 Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 1130 | static 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1139 | static 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 Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 1151 | if (err == -EAGAIN || |
| 1152 | intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) { |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1153 | if (intel_pt_fup_event(decoder)) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1154 | return 0; |
Adrian Hunter | 9fb5233 | 2018-05-31 13:23:45 +0300 | [diff] [blame] | 1155 | return -EAGAIN; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1156 | } |
| 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 | |
| 1179 | static 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 Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 1185 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1195 | decoder->state.type |= INTEL_PT_TRACE_END; |
Adrian Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1198 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1209 | if (decoder->packet.count == 0) { |
| 1210 | decoder->state.to_ip = 0; |
| 1211 | } else { |
| 1212 | decoder->state.to_ip = decoder->last_ip; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1213 | decoder->ip = decoder->last_ip; |
Adrian Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1214 | } |
| 1215 | decoder->state.type |= INTEL_PT_TRACE_END; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1216 | } 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 Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 1230 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1242 | decoder->state.to_ip = to_ip; |
| 1243 | decoder->state.type |= INTEL_PT_TRACE_END; |
Adrian Hunter | 9f1d122 | 2016-09-23 17:38:47 +0300 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1246 | 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 | |
| 1255 | static 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 Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 1287 | if (decoder->tnt.count) |
| 1288 | decoder->pkt_state = INTEL_PT_STATE_TNT_CONT; |
| 1289 | else |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1290 | 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 Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 1320 | if (decoder->tnt.count) |
| 1321 | decoder->pkt_state = INTEL_PT_STATE_TNT_CONT; |
| 1322 | else |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1323 | 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 Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 1341 | decoder->sample_cyc = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1342 | decoder->ip += intel_pt_insn.length; |
Adrian Hunter | 1b6599a | 2019-05-10 15:41:43 +0300 | [diff] [blame] | 1343 | if (!decoder->tnt.count) { |
Adrian Hunter | 948e9dc | 2019-05-20 14:37:10 +0300 | [diff] [blame] | 1344 | intel_pt_update_sample_time(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1345 | return -EAGAIN; |
Adrian Hunter | 1b6599a | 2019-05-10 15:41:43 +0300 | [diff] [blame] | 1346 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1347 | decoder->tnt.payload <<= 1; |
| 1348 | continue; |
| 1349 | } |
| 1350 | |
| 1351 | return intel_pt_bug(decoder); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | static 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 Hunter | e72b52a | 2019-06-04 16:00:03 +0300 | [diff] [blame] | 1378 | static 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1393 | static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder) |
| 1394 | { |
| 1395 | uint64_t timestamp; |
| 1396 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1397 | decoder->have_tma = false; |
| 1398 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1399 | if (decoder->ref_timestamp) { |
Adrian Hunter | e72b52a | 2019-06-04 16:00:03 +0300 | [diff] [blame] | 1400 | timestamp = intel_pt_8b_tsc(decoder->packet.payload, |
| 1401 | decoder->ref_timestamp); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1402 | 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1409 | decoder->tsc_timestamp = timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1410 | if (timestamp < decoder->timestamp && |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1411 | decoder->timestamp - timestamp < decoder->tsc_slip) { |
| 1412 | intel_pt_log_to("Suppressing backwards timestamp", |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1413 | timestamp); |
| 1414 | timestamp = decoder->timestamp; |
| 1415 | } |
Adrian Hunter | 9992c2d | 2015-09-25 16:15:34 +0300 | [diff] [blame] | 1416 | if (timestamp < decoder->timestamp) { |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1417 | intel_pt_log_to("Wraparound timestamp", timestamp); |
| 1418 | timestamp += (1ULL << 56); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1419 | decoder->tsc_timestamp = timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1420 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1421 | decoder->timestamp = timestamp; |
| 1422 | decoder->timestamp_insn_cnt = 0; |
| 1423 | } |
| 1424 | |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1425 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1432 | intel_pt_log_to("Setting timestamp", decoder->timestamp); |
| 1433 | } |
| 1434 | |
| 1435 | static 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 Hunter | 91d29b2 | 2018-03-07 16:02:24 +0200 | [diff] [blame] | 1439 | decoder->timestamp_insn_cnt = 0; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1440 | decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC; |
| 1441 | decoder->overflow = true; |
| 1442 | return -EOVERFLOW; |
| 1443 | } |
| 1444 | |
Adrian Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1445 | static 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 | |
| 1454 | static 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 | |
| 1462 | static 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1481 | static 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 Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1490 | 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1495 | 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 Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 1506 | decoder->fixup_last_mtc = true; |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1507 | 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 | |
| 1511 | static 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 Hunter | 3bccbe2 | 2016-09-28 14:41:36 +0300 | [diff] [blame] | 1521 | 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1527 | 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 Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1550 | intel_pt_mtc_cyc_cnt_upd(decoder); |
| 1551 | |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1552 | decoder->timestamp_insn_cnt = 0; |
| 1553 | decoder->last_mtc = mtc; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1554 | |
| 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 Hunter | f6c23e3 | 2018-11-05 09:35:05 +0200 | [diff] [blame] | 1561 | |
| 1562 | intel_pt_log_to("Setting timestamp", decoder->timestamp); |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder) |
| 1566 | { |
Adrian Hunter | 26fb2fb | 2017-05-26 11:17:15 +0300 | [diff] [blame] | 1567 | unsigned int cbr = decoder->packet.payload & 0xff; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1568 | |
Adrian Hunter | 0a7c700d | 2017-05-26 11:17:16 +0300 | [diff] [blame] | 1569 | decoder->cbr_payload = decoder->packet.payload; |
| 1570 | |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1571 | 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 Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1576 | |
| 1577 | intel_pt_mtc_cyc_cnt_cbr(decoder); |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static 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 Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 1587 | if (decoder->pge) |
| 1588 | decoder->tot_cyc_cnt += decoder->packet.payload; |
| 1589 | decoder->sample_cyc = true; |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1590 | |
| 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 Hunter | 51ee648 | 2016-09-28 14:41:35 +0300 | [diff] [blame] | 1606 | |
| 1607 | decoder->timestamp_insn_cnt = 0; |
Adrian Hunter | f6c23e3 | 2018-11-05 09:35:05 +0200 | [diff] [blame] | 1608 | |
| 1609 | intel_pt_log_to("Setting timestamp", decoder->timestamp); |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1610 | } |
| 1611 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1612 | /* Walk PSB+ packets when already in sync. */ |
| 1613 | static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder) |
| 1614 | { |
| 1615 | int err; |
| 1616 | |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1617 | decoder->in_psb = true; |
| 1618 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1619 | while (1) { |
| 1620 | err = intel_pt_get_next_packet(decoder); |
| 1621 | if (err) |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1622 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1623 | |
| 1624 | switch (decoder->packet.type) { |
| 1625 | case INTEL_PT_PSBEND: |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1626 | err = 0; |
| 1627 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1628 | |
| 1629 | case INTEL_PT_TIP_PGD: |
| 1630 | case INTEL_PT_TIP_PGE: |
| 1631 | case INTEL_PT_TIP: |
| 1632 | case INTEL_PT_TNT: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1633 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1634 | case INTEL_PT_BAD: |
| 1635 | case INTEL_PT_PSB: |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1636 | 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 Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1643 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1644 | intel_pt_log("ERROR: Unexpected packet\n"); |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1645 | err = -EAGAIN; |
| 1646 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1647 | |
| 1648 | case INTEL_PT_OVF: |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1649 | err = intel_pt_overflow(decoder); |
| 1650 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1651 | |
| 1652 | case INTEL_PT_TSC: |
| 1653 | intel_pt_calc_tsc_timestamp(decoder); |
| 1654 | break; |
| 1655 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1656 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1657 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1658 | break; |
| 1659 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1660 | case INTEL_PT_CBR: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1661 | intel_pt_calc_cbr(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1662 | break; |
| 1663 | |
| 1664 | case INTEL_PT_MODE_EXEC: |
| 1665 | decoder->exec_mode = decoder->packet.payload; |
| 1666 | break; |
| 1667 | |
| 1668 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1669 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1670 | break; |
| 1671 | |
| 1672 | case INTEL_PT_FUP: |
| 1673 | decoder->pge = true; |
Adrian Hunter | f952eac | 2017-05-26 11:17:07 +0300 | [diff] [blame] | 1674 | if (decoder->packet.count) |
| 1675 | intel_pt_set_last_ip(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1676 | break; |
| 1677 | |
| 1678 | case INTEL_PT_MODE_TSX: |
| 1679 | intel_pt_update_in_tx(decoder); |
| 1680 | break; |
| 1681 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1682 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1683 | intel_pt_calc_mtc_timestamp(decoder); |
| 1684 | if (decoder->period_type == INTEL_PT_PERIOD_MTC) |
| 1685 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1686 | break; |
| 1687 | |
| 1688 | case INTEL_PT_CYC: |
| 1689 | case INTEL_PT_VMCS: |
| 1690 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1691 | case INTEL_PT_PAD: |
| 1692 | default: |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 1696 | out: |
| 1697 | decoder->in_psb = false; |
| 1698 | |
| 1699 | return err; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1722 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1723 | case INTEL_PT_PSB: |
| 1724 | case INTEL_PT_TSC: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1725 | case INTEL_PT_TMA: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1726 | case INTEL_PT_MODE_TSX: |
| 1727 | case INTEL_PT_BAD: |
| 1728 | case INTEL_PT_PSBEND: |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1729 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1736 | intel_pt_log("ERROR: Missing TIP after FUP\n"); |
| 1737 | decoder->pkt_state = INTEL_PT_STATE_ERR3; |
Adrian Hunter | 1c196a6 | 2018-03-07 16:02:23 +0200 | [diff] [blame] | 1738 | decoder->pkt_step = 0; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1739 | return -ENOENT; |
| 1740 | |
Adrian Hunter | bd2e49e | 2018-05-31 13:23:43 +0300 | [diff] [blame] | 1741 | case INTEL_PT_CBR: |
| 1742 | intel_pt_calc_cbr(decoder); |
| 1743 | break; |
| 1744 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1745 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1750 | if (decoder->packet.count == 0) { |
| 1751 | decoder->state.to_ip = 0; |
| 1752 | } else { |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1753 | intel_pt_set_ip(decoder); |
Adrian Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1754 | decoder->state.to_ip = decoder->ip; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1755 | } |
| 1756 | decoder->pge = false; |
| 1757 | decoder->continuous_period = false; |
Adrian Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1758 | decoder->state.type |= INTEL_PT_TRACE_END; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1759 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1772 | decoder->state.type |= INTEL_PT_TRACE_BEGIN; |
Adrian Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1773 | intel_pt_mtc_cyc_cnt_pge(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1774 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1787 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
| 1788 | break; |
| 1789 | |
| 1790 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1791 | intel_pt_calc_mtc_timestamp(decoder); |
| 1792 | if (decoder->period_type == INTEL_PT_PERIOD_MTC) |
| 1793 | decoder->state.type |= INTEL_PT_INSTRUCTION; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1794 | break; |
| 1795 | |
| 1796 | case INTEL_PT_CYC: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1797 | intel_pt_calc_cyc_timestamp(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1798 | break; |
| 1799 | |
| 1800 | case INTEL_PT_MODE_EXEC: |
| 1801 | decoder->exec_mode = decoder->packet.payload; |
| 1802 | break; |
| 1803 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1804 | case INTEL_PT_VMCS: |
| 1805 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1806 | case INTEL_PT_PAD: |
| 1807 | break; |
| 1808 | |
| 1809 | default: |
| 1810 | return intel_pt_bug(decoder); |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | static 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; |
| 1824 | next: |
| 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 Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 1844 | intel_pt_mtc_cyc_cnt_pge(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1845 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 1853 | decoder->state.type |= INTEL_PT_TRACE_BEGIN; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1854 | 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 Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 1874 | if (!decoder->branch_enable) { |
| 1875 | decoder->ip = decoder->last_ip; |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1876 | if (intel_pt_fup_event(decoder)) |
| 1877 | return 0; |
| 1878 | no_tip = false; |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 1879 | break; |
| 1880 | } |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1881 | if (decoder->set_fup_mwait) |
| 1882 | no_tip = true; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1883 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1900 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | 7eacca3 | 2015-07-17 19:33:59 +0300 | [diff] [blame] | 1901 | decoder->pge = false; |
| 1902 | decoder->continuous_period = false; |
| 1903 | intel_pt_clear_tx_flags(decoder); |
| 1904 | decoder->have_tma = false; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1905 | break; |
| 1906 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1907 | case INTEL_PT_PSB: |
Adrian Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 1908 | decoder->last_ip = 0; |
| 1909 | decoder->have_last_ip = true; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1910 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1919 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
| 1920 | break; |
| 1921 | |
| 1922 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1923 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1941 | |
| 1942 | case INTEL_PT_TSC: |
| 1943 | intel_pt_calc_tsc_timestamp(decoder); |
| 1944 | break; |
| 1945 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1946 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 1947 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1948 | break; |
| 1949 | |
| 1950 | case INTEL_PT_CYC: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1951 | intel_pt_calc_cyc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1952 | break; |
| 1953 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1954 | case INTEL_PT_CBR: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 1955 | intel_pt_calc_cbr(decoder); |
Adrian Hunter | 0a7c700d | 2017-05-26 11:17:16 +0300 | [diff] [blame] | 1956 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1966 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 1987 | case INTEL_PT_VMCS: |
| 1988 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 1989 | case INTEL_PT_PAD: |
| 1990 | break; |
| 1991 | |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 1992 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2057 | default: |
| 2058 | return intel_pt_bug(decoder); |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 2063 | static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder) |
| 2064 | { |
Adrian Hunter | f952eac | 2017-05-26 11:17:07 +0300 | [diff] [blame] | 2065 | return decoder->packet.count && |
| 2066 | (decoder->have_last_ip || decoder->packet.count == 3 || |
| 2067 | decoder->packet.count == 6); |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 2068 | } |
| 2069 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2070 | /* Walk PSB+ packets to get in sync. */ |
| 2071 | static int intel_pt_walk_psb(struct intel_pt_decoder *decoder) |
| 2072 | { |
| 2073 | int err; |
| 2074 | |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2075 | decoder->in_psb = true; |
| 2076 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2077 | while (1) { |
| 2078 | err = intel_pt_get_next_packet(decoder); |
| 2079 | if (err) |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2080 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2081 | |
| 2082 | switch (decoder->packet.type) { |
| 2083 | case INTEL_PT_TIP_PGD: |
| 2084 | decoder->continuous_period = false; |
Arnaldo Carvalho de Melo | 7ea6856 | 2017-02-09 15:22:22 -0300 | [diff] [blame] | 2085 | __fallthrough; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2086 | case INTEL_PT_TIP_PGE: |
| 2087 | case INTEL_PT_TIP: |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 2088 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2095 | intel_pt_log("ERROR: Unexpected packet\n"); |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2096 | err = -ENOENT; |
| 2097 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2098 | |
| 2099 | case INTEL_PT_FUP: |
| 2100 | decoder->pge = true; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 2101 | if (intel_pt_have_ip(decoder)) { |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2102 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2111 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 2112 | intel_pt_calc_mtc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2113 | break; |
| 2114 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2115 | case INTEL_PT_TSC: |
| 2116 | intel_pt_calc_tsc_timestamp(decoder); |
| 2117 | break; |
| 2118 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2119 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 2120 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2121 | break; |
| 2122 | |
| 2123 | case INTEL_PT_CYC: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 2124 | intel_pt_calc_cyc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2125 | break; |
| 2126 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2127 | case INTEL_PT_CBR: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 2128 | intel_pt_calc_cbr(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2129 | break; |
| 2130 | |
| 2131 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2132 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2133 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2143 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | 7eacca3 | 2015-07-17 19:33:59 +0300 | [diff] [blame] | 2144 | decoder->pge = false; |
| 2145 | decoder->continuous_period = false; |
| 2146 | intel_pt_clear_tx_flags(decoder); |
Arnaldo Carvalho de Melo | 7ea6856 | 2017-02-09 15:22:22 -0300 | [diff] [blame] | 2147 | __fallthrough; |
| 2148 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2149 | case INTEL_PT_TNT: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 2150 | decoder->have_tma = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2151 | 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 Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2156 | err = -ENOENT; |
| 2157 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2158 | |
| 2159 | case INTEL_PT_BAD: /* Does not happen */ |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2160 | err = intel_pt_bug(decoder); |
| 2161 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2162 | |
| 2163 | case INTEL_PT_OVF: |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2164 | err = intel_pt_overflow(decoder); |
| 2165 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2166 | |
| 2167 | case INTEL_PT_PSBEND: |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2168 | err = 0; |
| 2169 | goto out; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2170 | |
| 2171 | case INTEL_PT_PSB: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2172 | case INTEL_PT_VMCS: |
| 2173 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2174 | case INTEL_PT_PAD: |
| 2175 | default: |
| 2176 | break; |
| 2177 | } |
| 2178 | } |
Adrian Hunter | 9bc668e | 2019-05-20 14:37:15 +0300 | [diff] [blame] | 2179 | out: |
| 2180 | decoder->in_psb = false; |
| 2181 | |
| 2182 | return err; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | static 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 Hunter | f3c98c4 | 2019-05-20 14:37:16 +0300 | [diff] [blame] | 2197 | decoder->pge = false; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 2198 | if (intel_pt_have_ip(decoder)) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2199 | intel_pt_set_ip(decoder); |
Adrian Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 2200 | if (!decoder->ip) |
| 2201 | break; |
Adrian Hunter | f3c98c4 | 2019-05-20 14:37:16 +0300 | [diff] [blame] | 2202 | decoder->state.type |= INTEL_PT_TRACE_END; |
| 2203 | return 0; |
| 2204 | |
| 2205 | case INTEL_PT_TIP_PGE: |
| 2206 | decoder->pge = true; |
Adrian Hunter | 3f05516 | 2019-05-20 14:37:17 +0300 | [diff] [blame] | 2207 | intel_pt_mtc_cyc_cnt_pge(decoder); |
Adrian Hunter | f3c98c4 | 2019-05-20 14:37:16 +0300 | [diff] [blame] | 2208 | 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 Hunter | bea6385 | 2018-09-20 16:00:48 +0300 | [diff] [blame] | 2221 | return 0; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2222 | |
| 2223 | case INTEL_PT_FUP: |
Adrian Hunter | 622b7a4 | 2017-05-26 11:17:08 +0300 | [diff] [blame] | 2224 | if (intel_pt_have_ip(decoder)) |
| 2225 | intel_pt_set_ip(decoder); |
| 2226 | if (decoder->ip) |
| 2227 | return 0; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2228 | break; |
| 2229 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2230 | case INTEL_PT_MTC: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 2231 | intel_pt_calc_mtc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2232 | break; |
| 2233 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2234 | case INTEL_PT_TSC: |
| 2235 | intel_pt_calc_tsc_timestamp(decoder); |
| 2236 | break; |
| 2237 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2238 | case INTEL_PT_TMA: |
Adrian Hunter | 79b5842 | 2015-07-17 19:33:55 +0300 | [diff] [blame] | 2239 | intel_pt_calc_tma(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2240 | break; |
| 2241 | |
| 2242 | case INTEL_PT_CYC: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 2243 | intel_pt_calc_cyc_timestamp(decoder); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2244 | break; |
| 2245 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2246 | case INTEL_PT_CBR: |
Adrian Hunter | cc33618 | 2015-07-17 19:33:57 +0300 | [diff] [blame] | 2247 | intel_pt_calc_cbr(decoder); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2248 | break; |
| 2249 | |
| 2250 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2251 | decoder->cr3 = decoder->packet.payload & (BIT63 - 1); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2252 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2268 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | 7eacca3 | 2015-07-17 19:33:59 +0300 | [diff] [blame] | 2269 | decoder->pge = false; |
| 2270 | decoder->continuous_period = false; |
| 2271 | intel_pt_clear_tx_flags(decoder); |
| 2272 | decoder->have_tma = false; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2273 | break; |
| 2274 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2275 | case INTEL_PT_PSB: |
Adrian Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 2276 | decoder->last_ip = 0; |
| 2277 | decoder->have_last_ip = true; |
Adrian Hunter | 12b7080 | 2017-05-26 11:17:04 +0300 | [diff] [blame] | 2278 | intel_pt_clear_stack(&decoder->stack); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2279 | 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 Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 2291 | case INTEL_PT_VMCS: |
| 2292 | case INTEL_PT_MNT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2293 | case INTEL_PT_PAD: |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 2294 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2301 | default: |
| 2302 | break; |
| 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | static int intel_pt_sync_ip(struct intel_pt_decoder *decoder) |
| 2308 | { |
| 2309 | int err; |
| 2310 | |
Adrian Hunter | 6a558f1 | 2017-05-26 11:17:09 +0300 | [diff] [blame] | 2311 | decoder->set_fup_tx_flags = false; |
Adrian Hunter | a472e65 | 2017-05-26 11:17:14 +0300 | [diff] [blame] | 2312 | decoder->set_fup_ptw = false; |
| 2313 | decoder->set_fup_mwait = false; |
| 2314 | decoder->set_fup_pwre = false; |
| 2315 | decoder->set_fup_exstop = false; |
Adrian Hunter | 6a558f1 | 2017-05-26 11:17:09 +0300 | [diff] [blame] | 2316 | |
Adrian Hunter | 8395981 | 2017-05-26 11:17:11 +0300 | [diff] [blame] | 2317 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2324 | 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 | |
| 2339 | static 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 | |
| 2353 | static 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 | |
| 2365 | static 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 2373 | ret = intel_pt_get_next_data(decoder, false); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2374 | 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 | |
| 2391 | static 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 Hunter | 6c1f0b1 | 2019-06-04 16:00:05 +0300 | [diff] [blame^] | 2399 | ret = intel_pt_get_next_data(decoder, false); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2400 | 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 | |
| 2426 | static int intel_pt_sync(struct intel_pt_decoder *decoder) |
| 2427 | { |
| 2428 | int err; |
| 2429 | |
| 2430 | decoder->pge = false; |
| 2431 | decoder->continuous_period = false; |
Adrian Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 2432 | decoder->have_last_ip = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2433 | 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 Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 2441 | decoder->have_last_ip = true; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2442 | 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 | |
| 2458 | static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder) |
| 2459 | { |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 2460 | uint64_t est = decoder->sample_insn_cnt << 1; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2461 | |
| 2462 | if (!decoder->cbr || !decoder->max_non_turbo_ratio) |
| 2463 | goto out; |
| 2464 | |
| 2465 | est *= decoder->max_non_turbo_ratio; |
| 2466 | est /= decoder->cbr; |
| 2467 | out: |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 2468 | return decoder->sample_timestamp + est; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | const 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 Hunter | ee14ac0 | 2017-05-26 11:17:06 +0300 | [diff] [blame] | 2484 | decoder->have_last_ip = false; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2485 | decoder->last_ip = 0; |
Adrian Hunter | ad7167a | 2017-05-26 11:17:05 +0300 | [diff] [blame] | 2486 | decoder->ip = 0; |
Adrian Hunter | 0419420 | 2017-05-26 11:17:10 +0300 | [diff] [blame] | 2487 | __fallthrough; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2488 | 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 Hunter | 61b6e08 | 2019-05-10 15:41:42 +0300 | [diff] [blame] | 2495 | case INTEL_PT_STATE_TNT_CONT: |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2496 | 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 Hunter | 22c0689 | 2017-05-26 11:17:02 +0300 | [diff] [blame] | 2524 | if (err) { |
| 2525 | decoder->state.err = intel_pt_ext_err(err); |
| 2526 | decoder->state.from_ip = decoder->ip; |
Adrian Hunter | 948e9dc | 2019-05-20 14:37:10 +0300 | [diff] [blame] | 2527 | intel_pt_update_sample_time(decoder); |
Adrian Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 2528 | decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt; |
Adrian Hunter | 22c0689 | 2017-05-26 11:17:02 +0300 | [diff] [blame] | 2529 | } else { |
| 2530 | decoder->state.err = 0; |
Adrian Hunter | 0a7c700d | 2017-05-26 11:17:16 +0300 | [diff] [blame] | 2531 | 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 Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 2536 | if (intel_pt_sample_time(decoder->pkt_state)) { |
Adrian Hunter | 948e9dc | 2019-05-20 14:37:10 +0300 | [diff] [blame] | 2537 | intel_pt_update_sample_time(decoder); |
Adrian Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 2538 | if (decoder->sample_cyc) |
| 2539 | decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt; |
| 2540 | } |
Adrian Hunter | 22c0689 | 2017-05-26 11:17:02 +0300 | [diff] [blame] | 2541 | } |
| 2542 | |
Adrian Hunter | 3f04d98 | 2017-05-26 11:17:03 +0300 | [diff] [blame] | 2543 | decoder->state.timestamp = decoder->sample_timestamp; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2544 | decoder->state.est_timestamp = intel_pt_est_timestamp(decoder); |
| 2545 | decoder->state.cr3 = decoder->cr3; |
Adrian Hunter | 2a21d03 | 2015-07-17 19:33:48 +0300 | [diff] [blame] | 2546 | decoder->state.tot_insn_cnt = decoder->tot_insn_cnt; |
Adrian Hunter | 7b4b4f8 | 2019-05-20 14:37:11 +0300 | [diff] [blame] | 2547 | decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2548 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2549 | return &decoder->state; |
| 2550 | } |
| 2551 | |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2552 | /** |
| 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 | */ |
| 2563 | static 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 | */ |
| 2588 | static 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 | */ |
| 2613 | static 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2640 | * @rem: returns remaining size when TSC is found |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2641 | * |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2648 | static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc, |
| 2649 | size_t *rem) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2650 | { |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2660 | *rem = len; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2661 | 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 | */ |
| 2684 | static 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 Hunter | 5a99d99 | 2019-02-06 12:39:44 +0200 | [diff] [blame] | 2704 | #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 | */ |
| 2717 | static 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2732 | /** |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2739 | * @consecutive: returns true if there is data in buf_b that is consecutive |
| 2740 | * to buf_a |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2741 | * |
| 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 | */ |
| 2750 | static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a, |
| 2751 | size_t len_a, |
| 2752 | unsigned char *buf_b, |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2753 | size_t len_b, bool *consecutive) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2754 | { |
| 2755 | uint64_t tsc_a, tsc_b; |
| 2756 | unsigned char *p; |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2757 | size_t len, rem_a, rem_b; |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2758 | |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2764 | if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) { |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2765 | /* 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2771 | if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2772 | return buf_b; /* No TSC in buf_a => assume no overlap */ |
| 2773 | } |
| 2774 | |
| 2775 | while (1) { |
| 2776 | /* Ignore PSB+ with no TSC */ |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2777 | 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 Hunter | 5a99d99 | 2019-02-06 12:39:44 +0200 | [diff] [blame] | 2782 | unsigned char *start; |
| 2783 | |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2784 | *consecutive = true; |
Adrian Hunter | 5a99d99 | 2019-02-06 12:39:44 +0200 | [diff] [blame] | 2785 | start = buf_b + len_b - (rem_b - rem_a); |
| 2786 | return adj_for_padding(start, buf_a, len_a); |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2787 | } |
| 2788 | if (cmp < 0) |
| 2789 | return buf_b; /* tsc_a < tsc_b => no overlap */ |
| 2790 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2791 | |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2804 | * @consecutive: returns true if there is data in buf_b that is consecutive |
| 2805 | * to buf_a |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2806 | * |
| 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 | */ |
| 2814 | unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a, |
| 2815 | unsigned char *buf_b, size_t len_b, |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2816 | bool have_tsc, bool *consecutive) |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2817 | { |
| 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 Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2828 | found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b, |
| 2829 | consecutive); |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2830 | 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2844 | while (1) { |
| 2845 | /* Potential overlap so check the bytes */ |
| 2846 | found = memmem(buf_a, len_a, buf_b, len_a); |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2847 | if (found) { |
| 2848 | *consecutive = true; |
Adrian Hunter | 5a99d99 | 2019-02-06 12:39:44 +0200 | [diff] [blame] | 2849 | return adj_for_padding(buf_b + len_a, buf_a, len_a); |
Adrian Hunter | 117db4b | 2018-03-07 16:02:21 +0200 | [diff] [blame] | 2850 | } |
Adrian Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2851 | |
| 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 Hunter | f4aa081 | 2015-07-17 19:33:40 +0300 | [diff] [blame] | 2855 | } |
| 2856 | } |