linux/tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
<<
>>
Prefs
   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>
  20#include <linux/compiler.h>
  21
  22#include "intel-pt-pkt-decoder.h"
  23
  24#define BIT(n)          (1 << (n))
  25
  26#define BIT63           ((uint64_t)1 << 63)
  27
  28#if __BYTE_ORDER == __BIG_ENDIAN
  29#define le16_to_cpu bswap_16
  30#define le32_to_cpu bswap_32
  31#define le64_to_cpu bswap_64
  32#define memcpy_le64(d, s, n) do { \
  33        memcpy((d), (s), (n));    \
  34        *(d) = le64_to_cpu(*(d)); \
  35} while (0)
  36#else
  37#define le16_to_cpu
  38#define le32_to_cpu
  39#define le64_to_cpu
  40#define memcpy_le64 memcpy
  41#endif
  42
  43static const char * const packet_name[] = {
  44        [INTEL_PT_BAD]          = "Bad Packet!",
  45        [INTEL_PT_PAD]          = "PAD",
  46        [INTEL_PT_TNT]          = "TNT",
  47        [INTEL_PT_TIP_PGD]      = "TIP.PGD",
  48        [INTEL_PT_TIP_PGE]      = "TIP.PGE",
  49        [INTEL_PT_TSC]          = "TSC",
  50        [INTEL_PT_TMA]          = "TMA",
  51        [INTEL_PT_MODE_EXEC]    = "MODE.Exec",
  52        [INTEL_PT_MODE_TSX]     = "MODE.TSX",
  53        [INTEL_PT_MTC]          = "MTC",
  54        [INTEL_PT_TIP]          = "TIP",
  55        [INTEL_PT_FUP]          = "FUP",
  56        [INTEL_PT_CYC]          = "CYC",
  57        [INTEL_PT_VMCS]         = "VMCS",
  58        [INTEL_PT_PSB]          = "PSB",
  59        [INTEL_PT_PSBEND]       = "PSBEND",
  60        [INTEL_PT_CBR]          = "CBR",
  61        [INTEL_PT_TRACESTOP]    = "TraceSTOP",
  62        [INTEL_PT_PIP]          = "PIP",
  63        [INTEL_PT_OVF]          = "OVF",
  64        [INTEL_PT_MNT]          = "MNT",
  65        [INTEL_PT_PTWRITE]      = "PTWRITE",
  66        [INTEL_PT_PTWRITE_IP]   = "PTWRITE",
  67        [INTEL_PT_EXSTOP]       = "EXSTOP",
  68        [INTEL_PT_EXSTOP_IP]    = "EXSTOP",
  69        [INTEL_PT_MWAIT]        = "MWAIT",
  70        [INTEL_PT_PWRE]         = "PWRE",
  71        [INTEL_PT_PWRX]         = "PWRX",
  72        [INTEL_PT_BBP]          = "BBP",
  73        [INTEL_PT_BIP]          = "BIP",
  74        [INTEL_PT_BEP]          = "BEP",
  75        [INTEL_PT_BEP_IP]       = "BEP",
  76};
  77
  78const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
  79{
  80        return packet_name[type];
  81}
  82
  83static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
  84                                 struct intel_pt_pkt *packet)
  85{
  86        uint64_t payload;
  87        int count;
  88
  89        if (len < 8)
  90                return INTEL_PT_NEED_MORE_BYTES;
  91
  92        payload = le64_to_cpu(*(uint64_t *)buf);
  93
  94        for (count = 47; count; count--) {
  95                if (payload & BIT63)
  96                        break;
  97                payload <<= 1;
  98        }
  99
 100        packet->type = INTEL_PT_TNT;
 101        packet->count = count;
 102        packet->payload = payload << 1;
 103        return 8;
 104}
 105
 106static int intel_pt_get_pip(const unsigned char *buf, size_t len,
 107                            struct intel_pt_pkt *packet)
 108{
 109        uint64_t payload = 0;
 110
 111        if (len < 8)
 112                return INTEL_PT_NEED_MORE_BYTES;
 113
 114        packet->type = INTEL_PT_PIP;
 115        memcpy_le64(&payload, buf + 2, 6);
 116        packet->payload = payload;
 117
 118        return 8;
 119}
 120
 121static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
 122{
 123        packet->type = INTEL_PT_TRACESTOP;
 124        return 2;
 125}
 126
 127static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
 128                            struct intel_pt_pkt *packet)
 129{
 130        if (len < 4)
 131                return INTEL_PT_NEED_MORE_BYTES;
 132        packet->type = INTEL_PT_CBR;
 133        packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
 134        return 4;
 135}
 136
 137static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
 138                             struct intel_pt_pkt *packet)
 139{
 140        unsigned int count = (52 - 5) >> 3;
 141
 142        if (count < 1 || count > 7)
 143                return INTEL_PT_BAD_PACKET;
 144
 145        if (len < count + 2)
 146                return INTEL_PT_NEED_MORE_BYTES;
 147
 148        packet->type = INTEL_PT_VMCS;
 149        packet->count = count;
 150        memcpy_le64(&packet->payload, buf + 2, count);
 151
 152        return count + 2;
 153}
 154
 155static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
 156{
 157        packet->type = INTEL_PT_OVF;
 158        return 2;
 159}
 160
 161static int intel_pt_get_psb(const unsigned char *buf, size_t len,
 162                            struct intel_pt_pkt *packet)
 163{
 164        int i;
 165
 166        if (len < 16)
 167                return INTEL_PT_NEED_MORE_BYTES;
 168
 169        for (i = 2; i < 16; i += 2) {
 170                if (buf[i] != 2 || buf[i + 1] != 0x82)
 171                        return INTEL_PT_BAD_PACKET;
 172        }
 173
 174        packet->type = INTEL_PT_PSB;
 175        return 16;
 176}
 177
 178static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
 179{
 180        packet->type = INTEL_PT_PSBEND;
 181        return 2;
 182}
 183
 184static int intel_pt_get_tma(const unsigned char *buf, size_t len,
 185                            struct intel_pt_pkt *packet)
 186{
 187        if (len < 7)
 188                return INTEL_PT_NEED_MORE_BYTES;
 189
 190        packet->type = INTEL_PT_TMA;
 191        packet->payload = buf[2] | (buf[3] << 8);
 192        packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
 193        return 7;
 194}
 195
 196static int intel_pt_get_pad(struct intel_pt_pkt *packet)
 197{
 198        packet->type = INTEL_PT_PAD;
 199        return 1;
 200}
 201
 202static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
 203                            struct intel_pt_pkt *packet)
 204{
 205        if (len < 11)
 206                return INTEL_PT_NEED_MORE_BYTES;
 207        packet->type = INTEL_PT_MNT;
 208        memcpy_le64(&packet->payload, buf + 3, 8);
 209        return 11
 210;
 211}
 212
 213static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
 214                              struct intel_pt_pkt *packet)
 215{
 216        if (len < 3)
 217                return INTEL_PT_NEED_MORE_BYTES;
 218
 219        switch (buf[2]) {
 220        case 0x88: /* MNT */
 221                return intel_pt_get_mnt(buf, len, packet);
 222        default:
 223                return INTEL_PT_BAD_PACKET;
 224        }
 225}
 226
 227static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
 228                                struct intel_pt_pkt *packet)
 229{
 230        packet->count = (buf[1] >> 5) & 0x3;
 231        packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
 232                                         INTEL_PT_PTWRITE;
 233
 234        switch (packet->count) {
 235        case 0:
 236                if (len < 6)
 237                        return INTEL_PT_NEED_MORE_BYTES;
 238                packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
 239                return 6;
 240        case 1:
 241                if (len < 10)
 242                        return INTEL_PT_NEED_MORE_BYTES;
 243                packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
 244                return 10;
 245        default:
 246                return INTEL_PT_BAD_PACKET;
 247        }
 248}
 249
 250static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
 251{
 252        packet->type = INTEL_PT_EXSTOP;
 253        return 2;
 254}
 255
 256static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
 257{
 258        packet->type = INTEL_PT_EXSTOP_IP;
 259        return 2;
 260}
 261
 262static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
 263                              struct intel_pt_pkt *packet)
 264{
 265        if (len < 10)
 266                return INTEL_PT_NEED_MORE_BYTES;
 267        packet->type = INTEL_PT_MWAIT;
 268        packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
 269        return 10;
 270}
 271
 272static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
 273                             struct intel_pt_pkt *packet)
 274{
 275        if (len < 4)
 276                return INTEL_PT_NEED_MORE_BYTES;
 277        packet->type = INTEL_PT_PWRE;
 278        memcpy_le64(&packet->payload, buf + 2, 2);
 279        return 4;
 280}
 281
 282static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
 283                             struct intel_pt_pkt *packet)
 284{
 285        if (len < 7)
 286                return INTEL_PT_NEED_MORE_BYTES;
 287        packet->type = INTEL_PT_PWRX;
 288        memcpy_le64(&packet->payload, buf + 2, 5);
 289        return 7;
 290}
 291
 292static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
 293                            struct intel_pt_pkt *packet)
 294{
 295        if (len < 3)
 296                return INTEL_PT_NEED_MORE_BYTES;
 297        packet->type = INTEL_PT_BBP;
 298        packet->count = buf[2] >> 7;
 299        packet->payload = buf[2] & 0x1f;
 300        return 3;
 301}
 302
 303static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
 304                              struct intel_pt_pkt *packet)
 305{
 306        if (len < 5)
 307                return INTEL_PT_NEED_MORE_BYTES;
 308        packet->type = INTEL_PT_BIP;
 309        packet->count = buf[0] >> 3;
 310        memcpy_le64(&packet->payload, buf + 1, 4);
 311        return 5;
 312}
 313
 314static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
 315                              struct intel_pt_pkt *packet)
 316{
 317        if (len < 9)
 318                return INTEL_PT_NEED_MORE_BYTES;
 319        packet->type = INTEL_PT_BIP;
 320        packet->count = buf[0] >> 3;
 321        memcpy_le64(&packet->payload, buf + 1, 8);
 322        return 9;
 323}
 324
 325static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
 326{
 327        if (len < 2)
 328                return INTEL_PT_NEED_MORE_BYTES;
 329        packet->type = INTEL_PT_BEP;
 330        return 2;
 331}
 332
 333static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
 334{
 335        if (len < 2)
 336                return INTEL_PT_NEED_MORE_BYTES;
 337        packet->type = INTEL_PT_BEP_IP;
 338        return 2;
 339}
 340
 341static int intel_pt_get_ext(const unsigned char *buf, size_t len,
 342                            struct intel_pt_pkt *packet)
 343{
 344        if (len < 2)
 345                return INTEL_PT_NEED_MORE_BYTES;
 346
 347        if ((buf[1] & 0x1f) == 0x12)
 348                return intel_pt_get_ptwrite(buf, len, packet);
 349
 350        switch (buf[1]) {
 351        case 0xa3: /* Long TNT */
 352                return intel_pt_get_long_tnt(buf, len, packet);
 353        case 0x43: /* PIP */
 354                return intel_pt_get_pip(buf, len, packet);
 355        case 0x83: /* TraceStop */
 356                return intel_pt_get_tracestop(packet);
 357        case 0x03: /* CBR */
 358                return intel_pt_get_cbr(buf, len, packet);
 359        case 0xc8: /* VMCS */
 360                return intel_pt_get_vmcs(buf, len, packet);
 361        case 0xf3: /* OVF */
 362                return intel_pt_get_ovf(packet);
 363        case 0x82: /* PSB */
 364                return intel_pt_get_psb(buf, len, packet);
 365        case 0x23: /* PSBEND */
 366                return intel_pt_get_psbend(packet);
 367        case 0x73: /* TMA */
 368                return intel_pt_get_tma(buf, len, packet);
 369        case 0xC3: /* 3-byte header */
 370                return intel_pt_get_3byte(buf, len, packet);
 371        case 0x62: /* EXSTOP no IP */
 372                return intel_pt_get_exstop(packet);
 373        case 0xE2: /* EXSTOP with IP */
 374                return intel_pt_get_exstop_ip(packet);
 375        case 0xC2: /* MWAIT */
 376                return intel_pt_get_mwait(buf, len, packet);
 377        case 0x22: /* PWRE */
 378                return intel_pt_get_pwre(buf, len, packet);
 379        case 0xA2: /* PWRX */
 380                return intel_pt_get_pwrx(buf, len, packet);
 381        case 0x63: /* BBP */
 382                return intel_pt_get_bbp(buf, len, packet);
 383        case 0x33: /* BEP no IP */
 384                return intel_pt_get_bep(len, packet);
 385        case 0xb3: /* BEP with IP */
 386                return intel_pt_get_bep_ip(len, packet);
 387        default:
 388                return INTEL_PT_BAD_PACKET;
 389        }
 390}
 391
 392static int intel_pt_get_short_tnt(unsigned int byte,
 393                                  struct intel_pt_pkt *packet)
 394{
 395        int count;
 396
 397        for (count = 6; count; count--) {
 398                if (byte & BIT(7))
 399                        break;
 400                byte <<= 1;
 401        }
 402
 403        packet->type = INTEL_PT_TNT;
 404        packet->count = count;
 405        packet->payload = (uint64_t)byte << 57;
 406
 407        return 1;
 408}
 409
 410static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
 411                            size_t len, struct intel_pt_pkt *packet)
 412{
 413        unsigned int offs = 1, shift;
 414        uint64_t payload = byte >> 3;
 415
 416        byte >>= 2;
 417        len -= 1;
 418        for (shift = 5; byte & 1; shift += 7) {
 419                if (offs > 9)
 420                        return INTEL_PT_BAD_PACKET;
 421                if (len < offs)
 422                        return INTEL_PT_NEED_MORE_BYTES;
 423                byte = buf[offs++];
 424                payload |= ((uint64_t)byte >> 1) << shift;
 425        }
 426
 427        packet->type = INTEL_PT_CYC;
 428        packet->payload = payload;
 429        return offs;
 430}
 431
 432static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
 433                           const unsigned char *buf, size_t len,
 434                           struct intel_pt_pkt *packet)
 435{
 436        int ip_len;
 437
 438        packet->count = byte >> 5;
 439
 440        switch (packet->count) {
 441        case 0:
 442                ip_len = 0;
 443                break;
 444        case 1:
 445                if (len < 3)
 446                        return INTEL_PT_NEED_MORE_BYTES;
 447                ip_len = 2;
 448                packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
 449                break;
 450        case 2:
 451                if (len < 5)
 452                        return INTEL_PT_NEED_MORE_BYTES;
 453                ip_len = 4;
 454                packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
 455                break;
 456        case 3:
 457        case 4:
 458                if (len < 7)
 459                        return INTEL_PT_NEED_MORE_BYTES;
 460                ip_len = 6;
 461                memcpy_le64(&packet->payload, buf + 1, 6);
 462                break;
 463        case 6:
 464                if (len < 9)
 465                        return INTEL_PT_NEED_MORE_BYTES;
 466                ip_len = 8;
 467                packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
 468                break;
 469        default:
 470                return INTEL_PT_BAD_PACKET;
 471        }
 472
 473        packet->type = type;
 474
 475        return ip_len + 1;
 476}
 477
 478static int intel_pt_get_mode(const unsigned char *buf, size_t len,
 479                             struct intel_pt_pkt *packet)
 480{
 481        if (len < 2)
 482                return INTEL_PT_NEED_MORE_BYTES;
 483
 484        switch (buf[1] >> 5) {
 485        case 0:
 486                packet->type = INTEL_PT_MODE_EXEC;
 487                switch (buf[1] & 3) {
 488                case 0:
 489                        packet->payload = 16;
 490                        break;
 491                case 1:
 492                        packet->payload = 64;
 493                        break;
 494                case 2:
 495                        packet->payload = 32;
 496                        break;
 497                default:
 498                        return INTEL_PT_BAD_PACKET;
 499                }
 500                break;
 501        case 1:
 502                packet->type = INTEL_PT_MODE_TSX;
 503                if ((buf[1] & 3) == 3)
 504                        return INTEL_PT_BAD_PACKET;
 505                packet->payload = buf[1] & 3;
 506                break;
 507        default:
 508                return INTEL_PT_BAD_PACKET;
 509        }
 510
 511        return 2;
 512}
 513
 514static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
 515                            struct intel_pt_pkt *packet)
 516{
 517        if (len < 8)
 518                return INTEL_PT_NEED_MORE_BYTES;
 519        packet->type = INTEL_PT_TSC;
 520        memcpy_le64(&packet->payload, buf + 1, 7);
 521        return 8;
 522}
 523
 524static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
 525                            struct intel_pt_pkt *packet)
 526{
 527        if (len < 2)
 528                return INTEL_PT_NEED_MORE_BYTES;
 529        packet->type = INTEL_PT_MTC;
 530        packet->payload = buf[1];
 531        return 2;
 532}
 533
 534static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
 535                                  struct intel_pt_pkt *packet,
 536                                  enum intel_pt_pkt_ctx ctx)
 537{
 538        unsigned int byte;
 539
 540        memset(packet, 0, sizeof(struct intel_pt_pkt));
 541
 542        if (!len)
 543                return INTEL_PT_NEED_MORE_BYTES;
 544
 545        byte = buf[0];
 546
 547        switch (ctx) {
 548        case INTEL_PT_NO_CTX:
 549                break;
 550        case INTEL_PT_BLK_4_CTX:
 551                if ((byte & 0x7) == 4)
 552                        return intel_pt_get_bip_4(buf, len, packet);
 553                break;
 554        case INTEL_PT_BLK_8_CTX:
 555                if ((byte & 0x7) == 4)
 556                        return intel_pt_get_bip_8(buf, len, packet);
 557                break;
 558        default:
 559                break;
 560        }
 561
 562        if (!(byte & BIT(0))) {
 563                if (byte == 0)
 564                        return intel_pt_get_pad(packet);
 565                if (byte == 2)
 566                        return intel_pt_get_ext(buf, len, packet);
 567                return intel_pt_get_short_tnt(byte, packet);
 568        }
 569
 570        if ((byte & 2))
 571                return intel_pt_get_cyc(byte, buf, len, packet);
 572
 573        switch (byte & 0x1f) {
 574        case 0x0D:
 575                return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
 576        case 0x11:
 577                return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
 578                                       packet);
 579        case 0x01:
 580                return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
 581                                       packet);
 582        case 0x1D:
 583                return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
 584        case 0x19:
 585                switch (byte) {
 586                case 0x99:
 587                        return intel_pt_get_mode(buf, len, packet);
 588                case 0x19:
 589                        return intel_pt_get_tsc(buf, len, packet);
 590                case 0x59:
 591                        return intel_pt_get_mtc(buf, len, packet);
 592                default:
 593                        return INTEL_PT_BAD_PACKET;
 594                }
 595        default:
 596                return INTEL_PT_BAD_PACKET;
 597        }
 598}
 599
 600void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
 601                          enum intel_pt_pkt_ctx *ctx)
 602{
 603        switch (packet->type) {
 604        case INTEL_PT_BAD:
 605        case INTEL_PT_PAD:
 606        case INTEL_PT_TSC:
 607        case INTEL_PT_TMA:
 608        case INTEL_PT_MTC:
 609        case INTEL_PT_FUP:
 610        case INTEL_PT_CYC:
 611        case INTEL_PT_CBR:
 612        case INTEL_PT_MNT:
 613        case INTEL_PT_EXSTOP:
 614        case INTEL_PT_EXSTOP_IP:
 615        case INTEL_PT_PWRE:
 616        case INTEL_PT_PWRX:
 617        case INTEL_PT_BIP:
 618                break;
 619        case INTEL_PT_TNT:
 620        case INTEL_PT_TIP:
 621        case INTEL_PT_TIP_PGD:
 622        case INTEL_PT_TIP_PGE:
 623        case INTEL_PT_MODE_EXEC:
 624        case INTEL_PT_MODE_TSX:
 625        case INTEL_PT_PIP:
 626        case INTEL_PT_OVF:
 627        case INTEL_PT_VMCS:
 628        case INTEL_PT_TRACESTOP:
 629        case INTEL_PT_PSB:
 630        case INTEL_PT_PSBEND:
 631        case INTEL_PT_PTWRITE:
 632        case INTEL_PT_PTWRITE_IP:
 633        case INTEL_PT_MWAIT:
 634        case INTEL_PT_BEP:
 635        case INTEL_PT_BEP_IP:
 636                *ctx = INTEL_PT_NO_CTX;
 637                break;
 638        case INTEL_PT_BBP:
 639                if (packet->count)
 640                        *ctx = INTEL_PT_BLK_4_CTX;
 641                else
 642                        *ctx = INTEL_PT_BLK_8_CTX;
 643                break;
 644        default:
 645                break;
 646        }
 647}
 648
 649int intel_pt_get_packet(const unsigned char *buf, size_t len,
 650                        struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
 651{
 652        int ret;
 653
 654        ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
 655        if (ret > 0) {
 656                while (ret < 8 && len > (size_t)ret && !buf[ret])
 657                        ret += 1;
 658                intel_pt_upd_pkt_ctx(packet, ctx);
 659        }
 660        return ret;
 661}
 662
 663int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
 664                      size_t buf_len)
 665{
 666        int ret, i, nr;
 667        unsigned long long payload = packet->payload;
 668        const char *name = intel_pt_pkt_name(packet->type);
 669
 670        switch (packet->type) {
 671        case INTEL_PT_BAD:
 672        case INTEL_PT_PAD:
 673        case INTEL_PT_PSB:
 674        case INTEL_PT_PSBEND:
 675        case INTEL_PT_TRACESTOP:
 676        case INTEL_PT_OVF:
 677                return snprintf(buf, buf_len, "%s", name);
 678        case INTEL_PT_TNT: {
 679                size_t blen = buf_len;
 680
 681                ret = snprintf(buf, blen, "%s ", name);
 682                if (ret < 0)
 683                        return ret;
 684                buf += ret;
 685                blen -= ret;
 686                for (i = 0; i < packet->count; i++) {
 687                        if (payload & BIT63)
 688                                ret = snprintf(buf, blen, "T");
 689                        else
 690                                ret = snprintf(buf, blen, "N");
 691                        if (ret < 0)
 692                                return ret;
 693                        buf += ret;
 694                        blen -= ret;
 695                        payload <<= 1;
 696                }
 697                ret = snprintf(buf, blen, " (%d)", packet->count);
 698                if (ret < 0)
 699                        return ret;
 700                blen -= ret;
 701                return buf_len - blen;
 702        }
 703        case INTEL_PT_TIP_PGD:
 704        case INTEL_PT_TIP_PGE:
 705        case INTEL_PT_TIP:
 706        case INTEL_PT_FUP:
 707                if (!(packet->count))
 708                        return snprintf(buf, buf_len, "%s no ip", name);
 709                __fallthrough;
 710        case INTEL_PT_CYC:
 711        case INTEL_PT_VMCS:
 712        case INTEL_PT_MTC:
 713        case INTEL_PT_MNT:
 714        case INTEL_PT_CBR:
 715        case INTEL_PT_TSC:
 716                return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
 717        case INTEL_PT_TMA:
 718                return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
 719                                (unsigned)payload, packet->count);
 720        case INTEL_PT_MODE_EXEC:
 721                return snprintf(buf, buf_len, "%s %lld", name, payload);
 722        case INTEL_PT_MODE_TSX:
 723                return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
 724                                name, (unsigned)(payload >> 1) & 1,
 725                                (unsigned)payload & 1);
 726        case INTEL_PT_PIP:
 727                nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
 728                payload &= ~INTEL_PT_VMX_NR_FLAG;
 729                ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
 730                               name, payload >> 1, nr);
 731                return ret;
 732        case INTEL_PT_PTWRITE:
 733                return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
 734        case INTEL_PT_PTWRITE_IP:
 735                return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
 736        case INTEL_PT_BEP:
 737        case INTEL_PT_EXSTOP:
 738                return snprintf(buf, buf_len, "%s IP:0", name);
 739        case INTEL_PT_BEP_IP:
 740        case INTEL_PT_EXSTOP_IP:
 741                return snprintf(buf, buf_len, "%s IP:1", name);
 742        case INTEL_PT_MWAIT:
 743                return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
 744                                name, payload, (unsigned int)(payload & 0xff),
 745                                (unsigned int)((payload >> 32) & 0x3));
 746        case INTEL_PT_PWRE:
 747                return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
 748                                name, payload, !!(payload & 0x80),
 749                                (unsigned int)((payload >> 12) & 0xf),
 750                                (unsigned int)((payload >> 8) & 0xf));
 751        case INTEL_PT_PWRX:
 752                return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
 753                                name, payload,
 754                                (unsigned int)((payload >> 4) & 0xf),
 755                                (unsigned int)(payload & 0xf),
 756                                (unsigned int)((payload >> 8) & 0xf));
 757        case INTEL_PT_BBP:
 758                return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
 759                                name, packet->count ? "4" : "8", payload);
 760        case INTEL_PT_BIP:
 761                return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
 762                                name, packet->count, payload);
 763        default:
 764                break;
 765        }
 766        return snprintf(buf, buf_len, "%s 0x%llx (%d)",
 767                        name, payload, packet->count);
 768}
 769