Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * intel_pt_pkt_decoder.c: Intel Processor Trace support
0004  * Copyright (c) 2013-2014, Intel Corporation.
0005  */
0006 
0007 #include <stdio.h>
0008 #include <string.h>
0009 #include <endian.h>
0010 #include <byteswap.h>
0011 #include <linux/compiler.h>
0012 
0013 #include "intel-pt-pkt-decoder.h"
0014 
0015 #define BIT(n)      (1 << (n))
0016 
0017 #define BIT63       ((uint64_t)1 << 63)
0018 
0019 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
0020 #define le16_to_cpu bswap_16
0021 #define le32_to_cpu bswap_32
0022 #define le64_to_cpu bswap_64
0023 #define memcpy_le64(d, s, n) do { \
0024     memcpy((d), (s), (n));    \
0025     *(d) = le64_to_cpu(*(d)); \
0026 } while (0)
0027 #else
0028 #define le16_to_cpu
0029 #define le32_to_cpu
0030 #define le64_to_cpu
0031 #define memcpy_le64 memcpy
0032 #endif
0033 
0034 static const char * const packet_name[] = {
0035     [INTEL_PT_BAD]      = "Bad Packet!",
0036     [INTEL_PT_PAD]      = "PAD",
0037     [INTEL_PT_TNT]      = "TNT",
0038     [INTEL_PT_TIP_PGD]  = "TIP.PGD",
0039     [INTEL_PT_TIP_PGE]  = "TIP.PGE",
0040     [INTEL_PT_TSC]      = "TSC",
0041     [INTEL_PT_TMA]      = "TMA",
0042     [INTEL_PT_MODE_EXEC]    = "MODE.Exec",
0043     [INTEL_PT_MODE_TSX] = "MODE.TSX",
0044     [INTEL_PT_MTC]      = "MTC",
0045     [INTEL_PT_TIP]      = "TIP",
0046     [INTEL_PT_FUP]      = "FUP",
0047     [INTEL_PT_CYC]      = "CYC",
0048     [INTEL_PT_VMCS]     = "VMCS",
0049     [INTEL_PT_PSB]      = "PSB",
0050     [INTEL_PT_PSBEND]   = "PSBEND",
0051     [INTEL_PT_CBR]      = "CBR",
0052     [INTEL_PT_TRACESTOP]    = "TraceSTOP",
0053     [INTEL_PT_PIP]      = "PIP",
0054     [INTEL_PT_OVF]      = "OVF",
0055     [INTEL_PT_MNT]      = "MNT",
0056     [INTEL_PT_PTWRITE]  = "PTWRITE",
0057     [INTEL_PT_PTWRITE_IP]   = "PTWRITE",
0058     [INTEL_PT_EXSTOP]   = "EXSTOP",
0059     [INTEL_PT_EXSTOP_IP]    = "EXSTOP",
0060     [INTEL_PT_MWAIT]    = "MWAIT",
0061     [INTEL_PT_PWRE]     = "PWRE",
0062     [INTEL_PT_PWRX]     = "PWRX",
0063     [INTEL_PT_BBP]      = "BBP",
0064     [INTEL_PT_BIP]      = "BIP",
0065     [INTEL_PT_BEP]      = "BEP",
0066     [INTEL_PT_BEP_IP]   = "BEP",
0067     [INTEL_PT_CFE]      = "CFE",
0068     [INTEL_PT_CFE_IP]   = "CFE",
0069     [INTEL_PT_EVD]      = "EVD",
0070 };
0071 
0072 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
0073 {
0074     return packet_name[type];
0075 }
0076 
0077 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
0078                  struct intel_pt_pkt *packet)
0079 {
0080     uint64_t payload;
0081     int count;
0082 
0083     if (len < 8)
0084         return INTEL_PT_NEED_MORE_BYTES;
0085 
0086     payload = le64_to_cpu(*(uint64_t *)buf);
0087 
0088     for (count = 47; count; count--) {
0089         if (payload & BIT63)
0090             break;
0091         payload <<= 1;
0092     }
0093 
0094     packet->type = INTEL_PT_TNT;
0095     packet->count = count;
0096     packet->payload = payload << 1;
0097     return 8;
0098 }
0099 
0100 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
0101                 struct intel_pt_pkt *packet)
0102 {
0103     uint64_t payload = 0;
0104 
0105     if (len < 8)
0106         return INTEL_PT_NEED_MORE_BYTES;
0107 
0108     packet->type = INTEL_PT_PIP;
0109     memcpy_le64(&payload, buf + 2, 6);
0110     packet->payload = payload;
0111 
0112     return 8;
0113 }
0114 
0115 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
0116 {
0117     packet->type = INTEL_PT_TRACESTOP;
0118     return 2;
0119 }
0120 
0121 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
0122                 struct intel_pt_pkt *packet)
0123 {
0124     if (len < 4)
0125         return INTEL_PT_NEED_MORE_BYTES;
0126     packet->type = INTEL_PT_CBR;
0127     packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
0128     return 4;
0129 }
0130 
0131 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
0132                  struct intel_pt_pkt *packet)
0133 {
0134     unsigned int count = (52 - 5) >> 3;
0135 
0136     if (count < 1 || count > 7)
0137         return INTEL_PT_BAD_PACKET;
0138 
0139     if (len < count + 2)
0140         return INTEL_PT_NEED_MORE_BYTES;
0141 
0142     packet->type = INTEL_PT_VMCS;
0143     packet->count = count;
0144     memcpy_le64(&packet->payload, buf + 2, count);
0145 
0146     return count + 2;
0147 }
0148 
0149 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
0150 {
0151     packet->type = INTEL_PT_OVF;
0152     return 2;
0153 }
0154 
0155 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
0156                 struct intel_pt_pkt *packet)
0157 {
0158     int i;
0159 
0160     if (len < 16)
0161         return INTEL_PT_NEED_MORE_BYTES;
0162 
0163     for (i = 2; i < 16; i += 2) {
0164         if (buf[i] != 2 || buf[i + 1] != 0x82)
0165             return INTEL_PT_BAD_PACKET;
0166     }
0167 
0168     packet->type = INTEL_PT_PSB;
0169     return 16;
0170 }
0171 
0172 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
0173 {
0174     packet->type = INTEL_PT_PSBEND;
0175     return 2;
0176 }
0177 
0178 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
0179                 struct intel_pt_pkt *packet)
0180 {
0181     if (len < 7)
0182         return INTEL_PT_NEED_MORE_BYTES;
0183 
0184     packet->type = INTEL_PT_TMA;
0185     packet->payload = buf[2] | (buf[3] << 8);
0186     packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
0187     return 7;
0188 }
0189 
0190 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
0191 {
0192     packet->type = INTEL_PT_PAD;
0193     return 1;
0194 }
0195 
0196 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
0197                 struct intel_pt_pkt *packet)
0198 {
0199     if (len < 11)
0200         return INTEL_PT_NEED_MORE_BYTES;
0201     packet->type = INTEL_PT_MNT;
0202     memcpy_le64(&packet->payload, buf + 3, 8);
0203     return 11;
0204 }
0205 
0206 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
0207                   struct intel_pt_pkt *packet)
0208 {
0209     if (len < 3)
0210         return INTEL_PT_NEED_MORE_BYTES;
0211 
0212     switch (buf[2]) {
0213     case 0x88: /* MNT */
0214         return intel_pt_get_mnt(buf, len, packet);
0215     default:
0216         return INTEL_PT_BAD_PACKET;
0217     }
0218 }
0219 
0220 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
0221                 struct intel_pt_pkt *packet)
0222 {
0223     packet->count = (buf[1] >> 5) & 0x3;
0224     packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
0225                      INTEL_PT_PTWRITE;
0226 
0227     switch (packet->count) {
0228     case 0:
0229         if (len < 6)
0230             return INTEL_PT_NEED_MORE_BYTES;
0231         packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
0232         return 6;
0233     case 1:
0234         if (len < 10)
0235             return INTEL_PT_NEED_MORE_BYTES;
0236         packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
0237         return 10;
0238     default:
0239         return INTEL_PT_BAD_PACKET;
0240     }
0241 }
0242 
0243 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
0244 {
0245     packet->type = INTEL_PT_EXSTOP;
0246     return 2;
0247 }
0248 
0249 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
0250 {
0251     packet->type = INTEL_PT_EXSTOP_IP;
0252     return 2;
0253 }
0254 
0255 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
0256                   struct intel_pt_pkt *packet)
0257 {
0258     if (len < 10)
0259         return INTEL_PT_NEED_MORE_BYTES;
0260     packet->type = INTEL_PT_MWAIT;
0261     packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
0262     return 10;
0263 }
0264 
0265 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
0266                  struct intel_pt_pkt *packet)
0267 {
0268     if (len < 4)
0269         return INTEL_PT_NEED_MORE_BYTES;
0270     packet->type = INTEL_PT_PWRE;
0271     memcpy_le64(&packet->payload, buf + 2, 2);
0272     return 4;
0273 }
0274 
0275 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
0276                  struct intel_pt_pkt *packet)
0277 {
0278     if (len < 7)
0279         return INTEL_PT_NEED_MORE_BYTES;
0280     packet->type = INTEL_PT_PWRX;
0281     memcpy_le64(&packet->payload, buf + 2, 5);
0282     return 7;
0283 }
0284 
0285 static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
0286                 struct intel_pt_pkt *packet)
0287 {
0288     if (len < 3)
0289         return INTEL_PT_NEED_MORE_BYTES;
0290     packet->type = INTEL_PT_BBP;
0291     packet->count = buf[2] >> 7;
0292     packet->payload = buf[2] & 0x1f;
0293     return 3;
0294 }
0295 
0296 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
0297                   struct intel_pt_pkt *packet)
0298 {
0299     if (len < 5)
0300         return INTEL_PT_NEED_MORE_BYTES;
0301     packet->type = INTEL_PT_BIP;
0302     packet->count = buf[0] >> 3;
0303     memcpy_le64(&packet->payload, buf + 1, 4);
0304     return 5;
0305 }
0306 
0307 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
0308                   struct intel_pt_pkt *packet)
0309 {
0310     if (len < 9)
0311         return INTEL_PT_NEED_MORE_BYTES;
0312     packet->type = INTEL_PT_BIP;
0313     packet->count = buf[0] >> 3;
0314     memcpy_le64(&packet->payload, buf + 1, 8);
0315     return 9;
0316 }
0317 
0318 static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
0319 {
0320     if (len < 2)
0321         return INTEL_PT_NEED_MORE_BYTES;
0322     packet->type = INTEL_PT_BEP;
0323     return 2;
0324 }
0325 
0326 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
0327 {
0328     if (len < 2)
0329         return INTEL_PT_NEED_MORE_BYTES;
0330     packet->type = INTEL_PT_BEP_IP;
0331     return 2;
0332 }
0333 
0334 static int intel_pt_get_cfe(const unsigned char *buf, size_t len,
0335                 struct intel_pt_pkt *packet)
0336 {
0337     if (len < 4)
0338         return INTEL_PT_NEED_MORE_BYTES;
0339     packet->type = buf[2] & 0x80 ? INTEL_PT_CFE_IP : INTEL_PT_CFE;
0340     packet->count = buf[2] & 0x1f;
0341     packet->payload = buf[3];
0342     return 4;
0343 }
0344 
0345 static int intel_pt_get_evd(const unsigned char *buf, size_t len,
0346                 struct intel_pt_pkt *packet)
0347 {
0348     if (len < 11)
0349         return INTEL_PT_NEED_MORE_BYTES;
0350     packet->type = INTEL_PT_EVD;
0351     packet->count = buf[2] & 0x3f;
0352     packet->payload = buf[3];
0353     memcpy_le64(&packet->payload, buf + 3, 8);
0354     return 11;
0355 }
0356 
0357 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
0358                 struct intel_pt_pkt *packet)
0359 {
0360     if (len < 2)
0361         return INTEL_PT_NEED_MORE_BYTES;
0362 
0363     if ((buf[1] & 0x1f) == 0x12)
0364         return intel_pt_get_ptwrite(buf, len, packet);
0365 
0366     switch (buf[1]) {
0367     case 0xa3: /* Long TNT */
0368         return intel_pt_get_long_tnt(buf, len, packet);
0369     case 0x43: /* PIP */
0370         return intel_pt_get_pip(buf, len, packet);
0371     case 0x83: /* TraceStop */
0372         return intel_pt_get_tracestop(packet);
0373     case 0x03: /* CBR */
0374         return intel_pt_get_cbr(buf, len, packet);
0375     case 0xc8: /* VMCS */
0376         return intel_pt_get_vmcs(buf, len, packet);
0377     case 0xf3: /* OVF */
0378         return intel_pt_get_ovf(packet);
0379     case 0x82: /* PSB */
0380         return intel_pt_get_psb(buf, len, packet);
0381     case 0x23: /* PSBEND */
0382         return intel_pt_get_psbend(packet);
0383     case 0x73: /* TMA */
0384         return intel_pt_get_tma(buf, len, packet);
0385     case 0xC3: /* 3-byte header */
0386         return intel_pt_get_3byte(buf, len, packet);
0387     case 0x62: /* EXSTOP no IP */
0388         return intel_pt_get_exstop(packet);
0389     case 0xE2: /* EXSTOP with IP */
0390         return intel_pt_get_exstop_ip(packet);
0391     case 0xC2: /* MWAIT */
0392         return intel_pt_get_mwait(buf, len, packet);
0393     case 0x22: /* PWRE */
0394         return intel_pt_get_pwre(buf, len, packet);
0395     case 0xA2: /* PWRX */
0396         return intel_pt_get_pwrx(buf, len, packet);
0397     case 0x63: /* BBP */
0398         return intel_pt_get_bbp(buf, len, packet);
0399     case 0x33: /* BEP no IP */
0400         return intel_pt_get_bep(len, packet);
0401     case 0xb3: /* BEP with IP */
0402         return intel_pt_get_bep_ip(len, packet);
0403     case 0x13: /* CFE */
0404         return intel_pt_get_cfe(buf, len, packet);
0405     case 0x53: /* EVD */
0406         return intel_pt_get_evd(buf, len, packet);
0407     default:
0408         return INTEL_PT_BAD_PACKET;
0409     }
0410 }
0411 
0412 static int intel_pt_get_short_tnt(unsigned int byte,
0413                   struct intel_pt_pkt *packet)
0414 {
0415     int count;
0416 
0417     for (count = 6; count; count--) {
0418         if (byte & BIT(7))
0419             break;
0420         byte <<= 1;
0421     }
0422 
0423     packet->type = INTEL_PT_TNT;
0424     packet->count = count;
0425     packet->payload = (uint64_t)byte << 57;
0426 
0427     return 1;
0428 }
0429 
0430 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
0431                 size_t len, struct intel_pt_pkt *packet)
0432 {
0433     unsigned int offs = 1, shift;
0434     uint64_t payload = byte >> 3;
0435 
0436     byte >>= 2;
0437     len -= 1;
0438     for (shift = 5; byte & 1; shift += 7) {
0439         if (offs > 9)
0440             return INTEL_PT_BAD_PACKET;
0441         if (len < offs)
0442             return INTEL_PT_NEED_MORE_BYTES;
0443         byte = buf[offs++];
0444         payload |= ((uint64_t)byte >> 1) << shift;
0445     }
0446 
0447     packet->type = INTEL_PT_CYC;
0448     packet->payload = payload;
0449     return offs;
0450 }
0451 
0452 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
0453                const unsigned char *buf, size_t len,
0454                struct intel_pt_pkt *packet)
0455 {
0456     int ip_len;
0457 
0458     packet->count = byte >> 5;
0459 
0460     switch (packet->count) {
0461     case 0:
0462         ip_len = 0;
0463         break;
0464     case 1:
0465         if (len < 3)
0466             return INTEL_PT_NEED_MORE_BYTES;
0467         ip_len = 2;
0468         packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
0469         break;
0470     case 2:
0471         if (len < 5)
0472             return INTEL_PT_NEED_MORE_BYTES;
0473         ip_len = 4;
0474         packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
0475         break;
0476     case 3:
0477     case 4:
0478         if (len < 7)
0479             return INTEL_PT_NEED_MORE_BYTES;
0480         ip_len = 6;
0481         memcpy_le64(&packet->payload, buf + 1, 6);
0482         break;
0483     case 6:
0484         if (len < 9)
0485             return INTEL_PT_NEED_MORE_BYTES;
0486         ip_len = 8;
0487         packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
0488         break;
0489     default:
0490         return INTEL_PT_BAD_PACKET;
0491     }
0492 
0493     packet->type = type;
0494 
0495     return ip_len + 1;
0496 }
0497 
0498 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
0499                  struct intel_pt_pkt *packet)
0500 {
0501     if (len < 2)
0502         return INTEL_PT_NEED_MORE_BYTES;
0503 
0504     switch (buf[1] >> 5) {
0505     case 0:
0506         packet->type = INTEL_PT_MODE_EXEC;
0507         packet->count = buf[1];
0508         switch (buf[1] & 3) {
0509         case 0:
0510             packet->payload = 16;
0511             break;
0512         case 1:
0513             packet->payload = 64;
0514             break;
0515         case 2:
0516             packet->payload = 32;
0517             break;
0518         default:
0519             return INTEL_PT_BAD_PACKET;
0520         }
0521         break;
0522     case 1:
0523         packet->type = INTEL_PT_MODE_TSX;
0524         if ((buf[1] & 3) == 3)
0525             return INTEL_PT_BAD_PACKET;
0526         packet->payload = buf[1] & 3;
0527         break;
0528     default:
0529         return INTEL_PT_BAD_PACKET;
0530     }
0531 
0532     return 2;
0533 }
0534 
0535 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
0536                 struct intel_pt_pkt *packet)
0537 {
0538     if (len < 8)
0539         return INTEL_PT_NEED_MORE_BYTES;
0540     packet->type = INTEL_PT_TSC;
0541     memcpy_le64(&packet->payload, buf + 1, 7);
0542     return 8;
0543 }
0544 
0545 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
0546                 struct intel_pt_pkt *packet)
0547 {
0548     if (len < 2)
0549         return INTEL_PT_NEED_MORE_BYTES;
0550     packet->type = INTEL_PT_MTC;
0551     packet->payload = buf[1];
0552     return 2;
0553 }
0554 
0555 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
0556                   struct intel_pt_pkt *packet,
0557                   enum intel_pt_pkt_ctx ctx)
0558 {
0559     unsigned int byte;
0560 
0561     memset(packet, 0, sizeof(struct intel_pt_pkt));
0562 
0563     if (!len)
0564         return INTEL_PT_NEED_MORE_BYTES;
0565 
0566     byte = buf[0];
0567 
0568     switch (ctx) {
0569     case INTEL_PT_NO_CTX:
0570         break;
0571     case INTEL_PT_BLK_4_CTX:
0572         if ((byte & 0x7) == 4)
0573             return intel_pt_get_bip_4(buf, len, packet);
0574         break;
0575     case INTEL_PT_BLK_8_CTX:
0576         if ((byte & 0x7) == 4)
0577             return intel_pt_get_bip_8(buf, len, packet);
0578         break;
0579     default:
0580         break;
0581     }
0582 
0583     if (!(byte & BIT(0))) {
0584         if (byte == 0)
0585             return intel_pt_get_pad(packet);
0586         if (byte == 2)
0587             return intel_pt_get_ext(buf, len, packet);
0588         return intel_pt_get_short_tnt(byte, packet);
0589     }
0590 
0591     if ((byte & 2))
0592         return intel_pt_get_cyc(byte, buf, len, packet);
0593 
0594     switch (byte & 0x1f) {
0595     case 0x0D:
0596         return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
0597     case 0x11:
0598         return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
0599                        packet);
0600     case 0x01:
0601         return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
0602                        packet);
0603     case 0x1D:
0604         return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
0605     case 0x19:
0606         switch (byte) {
0607         case 0x99:
0608             return intel_pt_get_mode(buf, len, packet);
0609         case 0x19:
0610             return intel_pt_get_tsc(buf, len, packet);
0611         case 0x59:
0612             return intel_pt_get_mtc(buf, len, packet);
0613         default:
0614             return INTEL_PT_BAD_PACKET;
0615         }
0616     default:
0617         return INTEL_PT_BAD_PACKET;
0618     }
0619 }
0620 
0621 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
0622               enum intel_pt_pkt_ctx *ctx)
0623 {
0624     switch (packet->type) {
0625     case INTEL_PT_BAD:
0626     case INTEL_PT_PAD:
0627     case INTEL_PT_TSC:
0628     case INTEL_PT_TMA:
0629     case INTEL_PT_MTC:
0630     case INTEL_PT_FUP:
0631     case INTEL_PT_CYC:
0632     case INTEL_PT_CBR:
0633     case INTEL_PT_MNT:
0634     case INTEL_PT_EXSTOP:
0635     case INTEL_PT_EXSTOP_IP:
0636     case INTEL_PT_PWRE:
0637     case INTEL_PT_PWRX:
0638     case INTEL_PT_BIP:
0639         break;
0640     case INTEL_PT_TNT:
0641     case INTEL_PT_TIP:
0642     case INTEL_PT_TIP_PGD:
0643     case INTEL_PT_TIP_PGE:
0644     case INTEL_PT_MODE_EXEC:
0645     case INTEL_PT_MODE_TSX:
0646     case INTEL_PT_PIP:
0647     case INTEL_PT_OVF:
0648     case INTEL_PT_VMCS:
0649     case INTEL_PT_TRACESTOP:
0650     case INTEL_PT_PSB:
0651     case INTEL_PT_PSBEND:
0652     case INTEL_PT_PTWRITE:
0653     case INTEL_PT_PTWRITE_IP:
0654     case INTEL_PT_MWAIT:
0655     case INTEL_PT_BEP:
0656     case INTEL_PT_BEP_IP:
0657     case INTEL_PT_CFE:
0658     case INTEL_PT_CFE_IP:
0659     case INTEL_PT_EVD:
0660         *ctx = INTEL_PT_NO_CTX;
0661         break;
0662     case INTEL_PT_BBP:
0663         if (packet->count)
0664             *ctx = INTEL_PT_BLK_4_CTX;
0665         else
0666             *ctx = INTEL_PT_BLK_8_CTX;
0667         break;
0668     default:
0669         break;
0670     }
0671 }
0672 
0673 int intel_pt_get_packet(const unsigned char *buf, size_t len,
0674             struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
0675 {
0676     int ret;
0677 
0678     ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
0679     if (ret > 0) {
0680         while (ret < 8 && len > (size_t)ret && !buf[ret])
0681             ret += 1;
0682         intel_pt_upd_pkt_ctx(packet, ctx);
0683     }
0684     return ret;
0685 }
0686 
0687 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
0688               size_t buf_len)
0689 {
0690     int ret, i, nr;
0691     unsigned long long payload = packet->payload;
0692     const char *name = intel_pt_pkt_name(packet->type);
0693 
0694     switch (packet->type) {
0695     case INTEL_PT_BAD:
0696     case INTEL_PT_PAD:
0697     case INTEL_PT_PSB:
0698     case INTEL_PT_PSBEND:
0699     case INTEL_PT_TRACESTOP:
0700     case INTEL_PT_OVF:
0701         return snprintf(buf, buf_len, "%s", name);
0702     case INTEL_PT_TNT: {
0703         size_t blen = buf_len;
0704 
0705         ret = snprintf(buf, blen, "%s ", name);
0706         if (ret < 0)
0707             return ret;
0708         buf += ret;
0709         blen -= ret;
0710         for (i = 0; i < packet->count; i++) {
0711             if (payload & BIT63)
0712                 ret = snprintf(buf, blen, "T");
0713             else
0714                 ret = snprintf(buf, blen, "N");
0715             if (ret < 0)
0716                 return ret;
0717             buf += ret;
0718             blen -= ret;
0719             payload <<= 1;
0720         }
0721         ret = snprintf(buf, blen, " (%d)", packet->count);
0722         if (ret < 0)
0723             return ret;
0724         blen -= ret;
0725         return buf_len - blen;
0726     }
0727     case INTEL_PT_TIP_PGD:
0728     case INTEL_PT_TIP_PGE:
0729     case INTEL_PT_TIP:
0730     case INTEL_PT_FUP:
0731         if (!(packet->count))
0732             return snprintf(buf, buf_len, "%s no ip", name);
0733         __fallthrough;
0734     case INTEL_PT_CYC:
0735     case INTEL_PT_VMCS:
0736     case INTEL_PT_MTC:
0737     case INTEL_PT_MNT:
0738     case INTEL_PT_CBR:
0739     case INTEL_PT_TSC:
0740         return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
0741     case INTEL_PT_TMA:
0742         return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
0743                 (unsigned)payload, packet->count);
0744     case INTEL_PT_MODE_EXEC:
0745         return snprintf(buf, buf_len, "%s IF:%d %lld",
0746                 name, !!(packet->count & 4), payload);
0747     case INTEL_PT_MODE_TSX:
0748         return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
0749                 name, (unsigned)(payload >> 1) & 1,
0750                 (unsigned)payload & 1);
0751     case INTEL_PT_PIP:
0752         nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
0753         payload &= ~INTEL_PT_VMX_NR_FLAG;
0754         ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
0755                    name, payload >> 1, nr);
0756         return ret;
0757     case INTEL_PT_PTWRITE:
0758         return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
0759     case INTEL_PT_PTWRITE_IP:
0760         return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
0761     case INTEL_PT_BEP:
0762     case INTEL_PT_EXSTOP:
0763         return snprintf(buf, buf_len, "%s IP:0", name);
0764     case INTEL_PT_BEP_IP:
0765     case INTEL_PT_EXSTOP_IP:
0766         return snprintf(buf, buf_len, "%s IP:1", name);
0767     case INTEL_PT_MWAIT:
0768         return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
0769                 name, payload, (unsigned int)(payload & 0xff),
0770                 (unsigned int)((payload >> 32) & 0x3));
0771     case INTEL_PT_PWRE:
0772         return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
0773                 name, payload, !!(payload & 0x80),
0774                 (unsigned int)((payload >> 12) & 0xf),
0775                 (unsigned int)((payload >> 8) & 0xf));
0776     case INTEL_PT_PWRX:
0777         return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
0778                 name, payload,
0779                 (unsigned int)((payload >> 4) & 0xf),
0780                 (unsigned int)(payload & 0xf),
0781                 (unsigned int)((payload >> 8) & 0xf));
0782     case INTEL_PT_BBP:
0783         return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
0784                 name, packet->count ? "4" : "8", payload);
0785     case INTEL_PT_BIP:
0786         return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
0787                 name, packet->count, payload);
0788     case INTEL_PT_CFE:
0789     case INTEL_PT_CFE_IP:
0790         return snprintf(buf, buf_len, "%s IP:%d Type 0x%02x Vector 0x%llx",
0791                 name, packet->type == INTEL_PT_CFE_IP, packet->count, payload);
0792     case INTEL_PT_EVD:
0793         return snprintf(buf, buf_len, "%s Type 0x%02x Payload 0x%llx",
0794                 name, packet->count, payload);
0795     default:
0796         break;
0797     }
0798     return snprintf(buf, buf_len, "%s 0x%llx (%d)",
0799             name, payload, packet->count);
0800 }