Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * intel_pt_pkt_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 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <endian.h> |
| 19 | #include <byteswap.h> |
Arnaldo Carvalho de Melo | 7ea6856 | 2017-02-09 15:22:22 -0300 | [diff] [blame^] | 20 | #include <linux/compiler.h> |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 21 | |
| 22 | #include "intel-pt-pkt-decoder.h" |
| 23 | |
| 24 | #define BIT(n) (1 << (n)) |
| 25 | |
| 26 | #define BIT63 ((uint64_t)1 << 63) |
| 27 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 28 | #define NR_FLAG BIT63 |
| 29 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 30 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 31 | #define le16_to_cpu bswap_16 |
| 32 | #define le32_to_cpu bswap_32 |
| 33 | #define le64_to_cpu bswap_64 |
| 34 | #define memcpy_le64(d, s, n) do { \ |
| 35 | memcpy((d), (s), (n)); \ |
| 36 | *(d) = le64_to_cpu(*(d)); \ |
| 37 | } while (0) |
| 38 | #else |
| 39 | #define le16_to_cpu |
| 40 | #define le32_to_cpu |
| 41 | #define le64_to_cpu |
| 42 | #define memcpy_le64 memcpy |
| 43 | #endif |
| 44 | |
| 45 | static const char * const packet_name[] = { |
| 46 | [INTEL_PT_BAD] = "Bad Packet!", |
| 47 | [INTEL_PT_PAD] = "PAD", |
| 48 | [INTEL_PT_TNT] = "TNT", |
| 49 | [INTEL_PT_TIP_PGD] = "TIP.PGD", |
| 50 | [INTEL_PT_TIP_PGE] = "TIP.PGE", |
| 51 | [INTEL_PT_TSC] = "TSC", |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 52 | [INTEL_PT_TMA] = "TMA", |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 53 | [INTEL_PT_MODE_EXEC] = "MODE.Exec", |
| 54 | [INTEL_PT_MODE_TSX] = "MODE.TSX", |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 55 | [INTEL_PT_MTC] = "MTC", |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 56 | [INTEL_PT_TIP] = "TIP", |
| 57 | [INTEL_PT_FUP] = "FUP", |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 58 | [INTEL_PT_CYC] = "CYC", |
| 59 | [INTEL_PT_VMCS] = "VMCS", |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 60 | [INTEL_PT_PSB] = "PSB", |
| 61 | [INTEL_PT_PSBEND] = "PSBEND", |
| 62 | [INTEL_PT_CBR] = "CBR", |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 63 | [INTEL_PT_TRACESTOP] = "TraceSTOP", |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 64 | [INTEL_PT_PIP] = "PIP", |
| 65 | [INTEL_PT_OVF] = "OVF", |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 66 | [INTEL_PT_MNT] = "MNT", |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | const char *intel_pt_pkt_name(enum intel_pt_pkt_type type) |
| 70 | { |
| 71 | return packet_name[type]; |
| 72 | } |
| 73 | |
| 74 | static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len, |
| 75 | struct intel_pt_pkt *packet) |
| 76 | { |
| 77 | uint64_t payload; |
| 78 | int count; |
| 79 | |
| 80 | if (len < 8) |
| 81 | return INTEL_PT_NEED_MORE_BYTES; |
| 82 | |
| 83 | payload = le64_to_cpu(*(uint64_t *)buf); |
| 84 | |
| 85 | for (count = 47; count; count--) { |
| 86 | if (payload & BIT63) |
| 87 | break; |
| 88 | payload <<= 1; |
| 89 | } |
| 90 | |
| 91 | packet->type = INTEL_PT_TNT; |
| 92 | packet->count = count; |
| 93 | packet->payload = payload << 1; |
| 94 | return 8; |
| 95 | } |
| 96 | |
| 97 | static int intel_pt_get_pip(const unsigned char *buf, size_t len, |
| 98 | struct intel_pt_pkt *packet) |
| 99 | { |
| 100 | uint64_t payload = 0; |
| 101 | |
| 102 | if (len < 8) |
| 103 | return INTEL_PT_NEED_MORE_BYTES; |
| 104 | |
| 105 | packet->type = INTEL_PT_PIP; |
| 106 | memcpy_le64(&payload, buf + 2, 6); |
| 107 | packet->payload = payload >> 1; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 108 | if (payload & 1) |
| 109 | packet->payload |= NR_FLAG; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 110 | |
| 111 | return 8; |
| 112 | } |
| 113 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 114 | static int intel_pt_get_tracestop(struct intel_pt_pkt *packet) |
| 115 | { |
| 116 | packet->type = INTEL_PT_TRACESTOP; |
| 117 | return 2; |
| 118 | } |
| 119 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 120 | static int intel_pt_get_cbr(const unsigned char *buf, size_t len, |
| 121 | struct intel_pt_pkt *packet) |
| 122 | { |
| 123 | if (len < 4) |
| 124 | return INTEL_PT_NEED_MORE_BYTES; |
| 125 | packet->type = INTEL_PT_CBR; |
| 126 | packet->payload = buf[2]; |
| 127 | return 4; |
| 128 | } |
| 129 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 130 | static int intel_pt_get_vmcs(const unsigned char *buf, size_t len, |
| 131 | struct intel_pt_pkt *packet) |
| 132 | { |
| 133 | unsigned int count = (52 - 5) >> 3; |
| 134 | |
| 135 | if (count < 1 || count > 7) |
| 136 | return INTEL_PT_BAD_PACKET; |
| 137 | |
| 138 | if (len < count + 2) |
| 139 | return INTEL_PT_NEED_MORE_BYTES; |
| 140 | |
| 141 | packet->type = INTEL_PT_VMCS; |
| 142 | packet->count = count; |
| 143 | memcpy_le64(&packet->payload, buf + 2, count); |
| 144 | |
| 145 | return count + 2; |
| 146 | } |
| 147 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 148 | static int intel_pt_get_ovf(struct intel_pt_pkt *packet) |
| 149 | { |
| 150 | packet->type = INTEL_PT_OVF; |
| 151 | return 2; |
| 152 | } |
| 153 | |
| 154 | static int intel_pt_get_psb(const unsigned char *buf, size_t len, |
| 155 | struct intel_pt_pkt *packet) |
| 156 | { |
| 157 | int i; |
| 158 | |
| 159 | if (len < 16) |
| 160 | return INTEL_PT_NEED_MORE_BYTES; |
| 161 | |
| 162 | for (i = 2; i < 16; i += 2) { |
| 163 | if (buf[i] != 2 || buf[i + 1] != 0x82) |
| 164 | return INTEL_PT_BAD_PACKET; |
| 165 | } |
| 166 | |
| 167 | packet->type = INTEL_PT_PSB; |
| 168 | return 16; |
| 169 | } |
| 170 | |
| 171 | static int intel_pt_get_psbend(struct intel_pt_pkt *packet) |
| 172 | { |
| 173 | packet->type = INTEL_PT_PSBEND; |
| 174 | return 2; |
| 175 | } |
| 176 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 177 | static int intel_pt_get_tma(const unsigned char *buf, size_t len, |
| 178 | struct intel_pt_pkt *packet) |
| 179 | { |
| 180 | if (len < 7) |
| 181 | return INTEL_PT_NEED_MORE_BYTES; |
| 182 | |
| 183 | packet->type = INTEL_PT_TMA; |
| 184 | packet->payload = buf[2] | (buf[3] << 8); |
| 185 | packet->count = buf[5] | ((buf[6] & BIT(0)) << 8); |
| 186 | return 7; |
| 187 | } |
| 188 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 189 | static int intel_pt_get_pad(struct intel_pt_pkt *packet) |
| 190 | { |
| 191 | packet->type = INTEL_PT_PAD; |
| 192 | return 1; |
| 193 | } |
| 194 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 195 | static int intel_pt_get_mnt(const unsigned char *buf, size_t len, |
| 196 | struct intel_pt_pkt *packet) |
| 197 | { |
| 198 | if (len < 11) |
| 199 | return INTEL_PT_NEED_MORE_BYTES; |
| 200 | packet->type = INTEL_PT_MNT; |
| 201 | memcpy_le64(&packet->payload, buf + 3, 8); |
| 202 | return 11 |
| 203 | ; |
| 204 | } |
| 205 | |
| 206 | static int intel_pt_get_3byte(const unsigned char *buf, size_t len, |
| 207 | struct intel_pt_pkt *packet) |
| 208 | { |
| 209 | if (len < 3) |
| 210 | return INTEL_PT_NEED_MORE_BYTES; |
| 211 | |
| 212 | switch (buf[2]) { |
| 213 | case 0x88: /* MNT */ |
| 214 | return intel_pt_get_mnt(buf, len, packet); |
| 215 | default: |
| 216 | return INTEL_PT_BAD_PACKET; |
| 217 | } |
| 218 | } |
| 219 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 220 | static int intel_pt_get_ext(const unsigned char *buf, size_t len, |
| 221 | struct intel_pt_pkt *packet) |
| 222 | { |
| 223 | if (len < 2) |
| 224 | return INTEL_PT_NEED_MORE_BYTES; |
| 225 | |
| 226 | switch (buf[1]) { |
| 227 | case 0xa3: /* Long TNT */ |
| 228 | return intel_pt_get_long_tnt(buf, len, packet); |
| 229 | case 0x43: /* PIP */ |
| 230 | return intel_pt_get_pip(buf, len, packet); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 231 | case 0x83: /* TraceStop */ |
| 232 | return intel_pt_get_tracestop(packet); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 233 | case 0x03: /* CBR */ |
| 234 | return intel_pt_get_cbr(buf, len, packet); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 235 | case 0xc8: /* VMCS */ |
| 236 | return intel_pt_get_vmcs(buf, len, packet); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 237 | case 0xf3: /* OVF */ |
| 238 | return intel_pt_get_ovf(packet); |
| 239 | case 0x82: /* PSB */ |
| 240 | return intel_pt_get_psb(buf, len, packet); |
| 241 | case 0x23: /* PSBEND */ |
| 242 | return intel_pt_get_psbend(packet); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 243 | case 0x73: /* TMA */ |
| 244 | return intel_pt_get_tma(buf, len, packet); |
| 245 | case 0xC3: /* 3-byte header */ |
| 246 | return intel_pt_get_3byte(buf, len, packet); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 247 | default: |
| 248 | return INTEL_PT_BAD_PACKET; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static int intel_pt_get_short_tnt(unsigned int byte, |
| 253 | struct intel_pt_pkt *packet) |
| 254 | { |
| 255 | int count; |
| 256 | |
| 257 | for (count = 6; count; count--) { |
| 258 | if (byte & BIT(7)) |
| 259 | break; |
| 260 | byte <<= 1; |
| 261 | } |
| 262 | |
| 263 | packet->type = INTEL_PT_TNT; |
| 264 | packet->count = count; |
| 265 | packet->payload = (uint64_t)byte << 57; |
| 266 | |
| 267 | return 1; |
| 268 | } |
| 269 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 270 | static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf, |
| 271 | size_t len, struct intel_pt_pkt *packet) |
| 272 | { |
| 273 | unsigned int offs = 1, shift; |
| 274 | uint64_t payload = byte >> 3; |
| 275 | |
| 276 | byte >>= 2; |
| 277 | len -= 1; |
| 278 | for (shift = 5; byte & 1; shift += 7) { |
| 279 | if (offs > 9) |
| 280 | return INTEL_PT_BAD_PACKET; |
| 281 | if (len < offs) |
| 282 | return INTEL_PT_NEED_MORE_BYTES; |
| 283 | byte = buf[offs++]; |
| 284 | payload |= (byte >> 1) << shift; |
| 285 | } |
| 286 | |
| 287 | packet->type = INTEL_PT_CYC; |
| 288 | packet->payload = payload; |
| 289 | return offs; |
| 290 | } |
| 291 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 292 | static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte, |
| 293 | const unsigned char *buf, size_t len, |
| 294 | struct intel_pt_pkt *packet) |
| 295 | { |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 296 | int ip_len; |
| 297 | |
| 298 | packet->count = byte >> 5; |
| 299 | |
| 300 | switch (packet->count) { |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 301 | case 0: |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 302 | ip_len = 0; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 303 | break; |
| 304 | case 1: |
| 305 | if (len < 3) |
| 306 | return INTEL_PT_NEED_MORE_BYTES; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 307 | ip_len = 2; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 308 | packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1)); |
| 309 | break; |
| 310 | case 2: |
| 311 | if (len < 5) |
| 312 | return INTEL_PT_NEED_MORE_BYTES; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 313 | ip_len = 4; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 314 | packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1)); |
| 315 | break; |
| 316 | case 3: |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 317 | case 4: |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 318 | if (len < 7) |
| 319 | return INTEL_PT_NEED_MORE_BYTES; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 320 | ip_len = 6; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 321 | memcpy_le64(&packet->payload, buf + 1, 6); |
| 322 | break; |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 323 | case 6: |
| 324 | if (len < 9) |
| 325 | return INTEL_PT_NEED_MORE_BYTES; |
| 326 | ip_len = 8; |
| 327 | packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1)); |
| 328 | break; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 329 | default: |
| 330 | return INTEL_PT_BAD_PACKET; |
| 331 | } |
| 332 | |
| 333 | packet->type = type; |
| 334 | |
Adrian Hunter | e1717e0 | 2016-07-20 12:00:06 +0300 | [diff] [blame] | 335 | return ip_len + 1; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static int intel_pt_get_mode(const unsigned char *buf, size_t len, |
| 339 | struct intel_pt_pkt *packet) |
| 340 | { |
| 341 | if (len < 2) |
| 342 | return INTEL_PT_NEED_MORE_BYTES; |
| 343 | |
| 344 | switch (buf[1] >> 5) { |
| 345 | case 0: |
| 346 | packet->type = INTEL_PT_MODE_EXEC; |
| 347 | switch (buf[1] & 3) { |
| 348 | case 0: |
| 349 | packet->payload = 16; |
| 350 | break; |
| 351 | case 1: |
| 352 | packet->payload = 64; |
| 353 | break; |
| 354 | case 2: |
| 355 | packet->payload = 32; |
| 356 | break; |
| 357 | default: |
| 358 | return INTEL_PT_BAD_PACKET; |
| 359 | } |
| 360 | break; |
| 361 | case 1: |
| 362 | packet->type = INTEL_PT_MODE_TSX; |
| 363 | if ((buf[1] & 3) == 3) |
| 364 | return INTEL_PT_BAD_PACKET; |
| 365 | packet->payload = buf[1] & 3; |
| 366 | break; |
| 367 | default: |
| 368 | return INTEL_PT_BAD_PACKET; |
| 369 | } |
| 370 | |
| 371 | return 2; |
| 372 | } |
| 373 | |
| 374 | static int intel_pt_get_tsc(const unsigned char *buf, size_t len, |
| 375 | struct intel_pt_pkt *packet) |
| 376 | { |
| 377 | if (len < 8) |
| 378 | return INTEL_PT_NEED_MORE_BYTES; |
| 379 | packet->type = INTEL_PT_TSC; |
| 380 | memcpy_le64(&packet->payload, buf + 1, 7); |
| 381 | return 8; |
| 382 | } |
| 383 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 384 | static int intel_pt_get_mtc(const unsigned char *buf, size_t len, |
| 385 | struct intel_pt_pkt *packet) |
| 386 | { |
| 387 | if (len < 2) |
| 388 | return INTEL_PT_NEED_MORE_BYTES; |
| 389 | packet->type = INTEL_PT_MTC; |
| 390 | packet->payload = buf[1]; |
| 391 | return 2; |
| 392 | } |
| 393 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 394 | static int intel_pt_do_get_packet(const unsigned char *buf, size_t len, |
| 395 | struct intel_pt_pkt *packet) |
| 396 | { |
| 397 | unsigned int byte; |
| 398 | |
| 399 | memset(packet, 0, sizeof(struct intel_pt_pkt)); |
| 400 | |
| 401 | if (!len) |
| 402 | return INTEL_PT_NEED_MORE_BYTES; |
| 403 | |
| 404 | byte = buf[0]; |
| 405 | if (!(byte & BIT(0))) { |
| 406 | if (byte == 0) |
| 407 | return intel_pt_get_pad(packet); |
| 408 | if (byte == 2) |
| 409 | return intel_pt_get_ext(buf, len, packet); |
| 410 | return intel_pt_get_short_tnt(byte, packet); |
| 411 | } |
| 412 | |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 413 | if ((byte & 2)) |
| 414 | return intel_pt_get_cyc(byte, buf, len, packet); |
| 415 | |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 416 | switch (byte & 0x1f) { |
| 417 | case 0x0D: |
| 418 | return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet); |
| 419 | case 0x11: |
| 420 | return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len, |
| 421 | packet); |
| 422 | case 0x01: |
| 423 | return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len, |
| 424 | packet); |
| 425 | case 0x1D: |
| 426 | return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet); |
| 427 | case 0x19: |
| 428 | switch (byte) { |
| 429 | case 0x99: |
| 430 | return intel_pt_get_mode(buf, len, packet); |
| 431 | case 0x19: |
| 432 | return intel_pt_get_tsc(buf, len, packet); |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 433 | case 0x59: |
| 434 | return intel_pt_get_mtc(buf, len, packet); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 435 | default: |
| 436 | return INTEL_PT_BAD_PACKET; |
| 437 | } |
| 438 | default: |
| 439 | return INTEL_PT_BAD_PACKET; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | int intel_pt_get_packet(const unsigned char *buf, size_t len, |
| 444 | struct intel_pt_pkt *packet) |
| 445 | { |
| 446 | int ret; |
| 447 | |
| 448 | ret = intel_pt_do_get_packet(buf, len, packet); |
| 449 | if (ret > 0) { |
| 450 | while (ret < 8 && len > (size_t)ret && !buf[ret]) |
| 451 | ret += 1; |
| 452 | } |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf, |
| 457 | size_t buf_len) |
| 458 | { |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 459 | int ret, i, nr; |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 460 | unsigned long long payload = packet->payload; |
| 461 | const char *name = intel_pt_pkt_name(packet->type); |
| 462 | |
| 463 | switch (packet->type) { |
| 464 | case INTEL_PT_BAD: |
| 465 | case INTEL_PT_PAD: |
| 466 | case INTEL_PT_PSB: |
| 467 | case INTEL_PT_PSBEND: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 468 | case INTEL_PT_TRACESTOP: |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 469 | case INTEL_PT_OVF: |
| 470 | return snprintf(buf, buf_len, "%s", name); |
| 471 | case INTEL_PT_TNT: { |
| 472 | size_t blen = buf_len; |
| 473 | |
| 474 | ret = snprintf(buf, blen, "%s ", name); |
| 475 | if (ret < 0) |
| 476 | return ret; |
| 477 | buf += ret; |
| 478 | blen -= ret; |
| 479 | for (i = 0; i < packet->count; i++) { |
| 480 | if (payload & BIT63) |
| 481 | ret = snprintf(buf, blen, "T"); |
| 482 | else |
| 483 | ret = snprintf(buf, blen, "N"); |
| 484 | if (ret < 0) |
| 485 | return ret; |
| 486 | buf += ret; |
| 487 | blen -= ret; |
| 488 | payload <<= 1; |
| 489 | } |
| 490 | ret = snprintf(buf, blen, " (%d)", packet->count); |
| 491 | if (ret < 0) |
| 492 | return ret; |
| 493 | blen -= ret; |
| 494 | return buf_len - blen; |
| 495 | } |
| 496 | case INTEL_PT_TIP_PGD: |
| 497 | case INTEL_PT_TIP_PGE: |
| 498 | case INTEL_PT_TIP: |
| 499 | case INTEL_PT_FUP: |
| 500 | if (!(packet->count)) |
| 501 | return snprintf(buf, buf_len, "%s no ip", name); |
Arnaldo Carvalho de Melo | 7ea6856 | 2017-02-09 15:22:22 -0300 | [diff] [blame^] | 502 | __fallthrough; |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 503 | case INTEL_PT_CYC: |
| 504 | case INTEL_PT_VMCS: |
| 505 | case INTEL_PT_MTC: |
| 506 | case INTEL_PT_MNT: |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 507 | case INTEL_PT_CBR: |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 508 | case INTEL_PT_TSC: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 509 | return snprintf(buf, buf_len, "%s 0x%llx", name, payload); |
| 510 | case INTEL_PT_TMA: |
| 511 | return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name, |
| 512 | (unsigned)payload, packet->count); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 513 | case INTEL_PT_MODE_EXEC: |
| 514 | return snprintf(buf, buf_len, "%s %lld", name, payload); |
| 515 | case INTEL_PT_MODE_TSX: |
| 516 | return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u", |
| 517 | name, (unsigned)(payload >> 1) & 1, |
| 518 | (unsigned)payload & 1); |
| 519 | case INTEL_PT_PIP: |
Adrian Hunter | 3d49807 | 2015-07-17 19:33:53 +0300 | [diff] [blame] | 520 | nr = packet->payload & NR_FLAG ? 1 : 0; |
| 521 | payload &= ~NR_FLAG; |
| 522 | ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)", |
| 523 | name, payload, nr); |
Adrian Hunter | a4e9259 | 2015-07-17 19:33:37 +0300 | [diff] [blame] | 524 | return ret; |
| 525 | default: |
| 526 | break; |
| 527 | } |
| 528 | return snprintf(buf, buf_len, "%s 0x%llx (%d)", |
| 529 | name, payload, packet->count); |
| 530 | } |