1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt_pkt_decoder.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
11 #include <linux/compiler.h>
13 #include "intel-pt-pkt-decoder.h"
15 #define BIT(n) (1 << (n))
17 #define BIT63 ((uint64_t)1 << 63)
19 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
20 #define le16_to_cpu bswap_16
21 #define le32_to_cpu bswap_32
22 #define le64_to_cpu bswap_64
23 #define memcpy_le64(d, s, n) do { \
24 memcpy((d), (s), (n)); \
25 *(d) = le64_to_cpu(*(d)); \
31 #define memcpy_le64 memcpy
34 static const char * const packet_name[] = {
35 [INTEL_PT_BAD] = "Bad Packet!",
36 [INTEL_PT_PAD] = "PAD",
37 [INTEL_PT_TNT] = "TNT",
38 [INTEL_PT_TIP_PGD] = "TIP.PGD",
39 [INTEL_PT_TIP_PGE] = "TIP.PGE",
40 [INTEL_PT_TSC] = "TSC",
41 [INTEL_PT_TMA] = "TMA",
42 [INTEL_PT_MODE_EXEC] = "MODE.Exec",
43 [INTEL_PT_MODE_TSX] = "MODE.TSX",
44 [INTEL_PT_MTC] = "MTC",
45 [INTEL_PT_TIP] = "TIP",
46 [INTEL_PT_FUP] = "FUP",
47 [INTEL_PT_CYC] = "CYC",
48 [INTEL_PT_VMCS] = "VMCS",
49 [INTEL_PT_PSB] = "PSB",
50 [INTEL_PT_PSBEND] = "PSBEND",
51 [INTEL_PT_CBR] = "CBR",
52 [INTEL_PT_TRACESTOP] = "TraceSTOP",
53 [INTEL_PT_PIP] = "PIP",
54 [INTEL_PT_OVF] = "OVF",
55 [INTEL_PT_MNT] = "MNT",
56 [INTEL_PT_PTWRITE] = "PTWRITE",
57 [INTEL_PT_PTWRITE_IP] = "PTWRITE",
58 [INTEL_PT_EXSTOP] = "EXSTOP",
59 [INTEL_PT_EXSTOP_IP] = "EXSTOP",
60 [INTEL_PT_MWAIT] = "MWAIT",
61 [INTEL_PT_PWRE] = "PWRE",
62 [INTEL_PT_PWRX] = "PWRX",
63 [INTEL_PT_BBP] = "BBP",
64 [INTEL_PT_BIP] = "BIP",
65 [INTEL_PT_BEP] = "BEP",
66 [INTEL_PT_BEP_IP] = "BEP",
67 [INTEL_PT_CFE] = "CFE",
68 [INTEL_PT_CFE_IP] = "CFE",
69 [INTEL_PT_EVD] = "EVD",
72 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
74 return packet_name[type];
77 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
78 struct intel_pt_pkt *packet)
84 return INTEL_PT_NEED_MORE_BYTES;
86 payload = le64_to_cpu(*(uint64_t *)buf);
88 for (count = 47; count; count--) {
94 packet->type = INTEL_PT_TNT;
95 packet->count = count;
96 packet->payload = payload << 1;
100 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
101 struct intel_pt_pkt *packet)
103 uint64_t payload = 0;
106 return INTEL_PT_NEED_MORE_BYTES;
108 packet->type = INTEL_PT_PIP;
109 memcpy_le64(&payload, buf + 2, 6);
110 packet->payload = payload;
115 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
117 packet->type = INTEL_PT_TRACESTOP;
121 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
122 struct intel_pt_pkt *packet)
125 return INTEL_PT_NEED_MORE_BYTES;
126 packet->type = INTEL_PT_CBR;
127 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
131 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
132 struct intel_pt_pkt *packet)
134 unsigned int count = (52 - 5) >> 3;
136 if (count < 1 || count > 7)
137 return INTEL_PT_BAD_PACKET;
140 return INTEL_PT_NEED_MORE_BYTES;
142 packet->type = INTEL_PT_VMCS;
143 packet->count = count;
144 memcpy_le64(&packet->payload, buf + 2, count);
149 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
151 packet->type = INTEL_PT_OVF;
155 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
156 struct intel_pt_pkt *packet)
161 return INTEL_PT_NEED_MORE_BYTES;
163 for (i = 2; i < 16; i += 2) {
164 if (buf[i] != 2 || buf[i + 1] != 0x82)
165 return INTEL_PT_BAD_PACKET;
168 packet->type = INTEL_PT_PSB;
172 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
174 packet->type = INTEL_PT_PSBEND;
178 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
179 struct intel_pt_pkt *packet)
182 return INTEL_PT_NEED_MORE_BYTES;
184 packet->type = INTEL_PT_TMA;
185 packet->payload = buf[2] | (buf[3] << 8);
186 packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
190 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
192 packet->type = INTEL_PT_PAD;
196 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
197 struct intel_pt_pkt *packet)
200 return INTEL_PT_NEED_MORE_BYTES;
201 packet->type = INTEL_PT_MNT;
202 memcpy_le64(&packet->payload, buf + 3, 8);
206 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
207 struct intel_pt_pkt *packet)
210 return INTEL_PT_NEED_MORE_BYTES;
214 return intel_pt_get_mnt(buf, len, packet);
216 return INTEL_PT_BAD_PACKET;
220 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
221 struct intel_pt_pkt *packet)
223 packet->count = (buf[1] >> 5) & 0x3;
224 packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
227 switch (packet->count) {
230 return INTEL_PT_NEED_MORE_BYTES;
231 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
235 return INTEL_PT_NEED_MORE_BYTES;
236 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
239 return INTEL_PT_BAD_PACKET;
243 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
245 packet->type = INTEL_PT_EXSTOP;
249 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
251 packet->type = INTEL_PT_EXSTOP_IP;
255 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
256 struct intel_pt_pkt *packet)
259 return INTEL_PT_NEED_MORE_BYTES;
260 packet->type = INTEL_PT_MWAIT;
261 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
265 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
266 struct intel_pt_pkt *packet)
269 return INTEL_PT_NEED_MORE_BYTES;
270 packet->type = INTEL_PT_PWRE;
271 memcpy_le64(&packet->payload, buf + 2, 2);
275 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
276 struct intel_pt_pkt *packet)
279 return INTEL_PT_NEED_MORE_BYTES;
280 packet->type = INTEL_PT_PWRX;
281 memcpy_le64(&packet->payload, buf + 2, 5);
285 static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
286 struct intel_pt_pkt *packet)
289 return INTEL_PT_NEED_MORE_BYTES;
290 packet->type = INTEL_PT_BBP;
291 packet->count = buf[2] >> 7;
292 packet->payload = buf[2] & 0x1f;
296 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
297 struct intel_pt_pkt *packet)
300 return INTEL_PT_NEED_MORE_BYTES;
301 packet->type = INTEL_PT_BIP;
302 packet->count = buf[0] >> 3;
303 memcpy_le64(&packet->payload, buf + 1, 4);
307 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
308 struct intel_pt_pkt *packet)
311 return INTEL_PT_NEED_MORE_BYTES;
312 packet->type = INTEL_PT_BIP;
313 packet->count = buf[0] >> 3;
314 memcpy_le64(&packet->payload, buf + 1, 8);
318 static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
321 return INTEL_PT_NEED_MORE_BYTES;
322 packet->type = INTEL_PT_BEP;
326 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
329 return INTEL_PT_NEED_MORE_BYTES;
330 packet->type = INTEL_PT_BEP_IP;
334 static int intel_pt_get_cfe(const unsigned char *buf, size_t len,
335 struct intel_pt_pkt *packet)
338 return INTEL_PT_NEED_MORE_BYTES;
339 packet->type = buf[2] & 0x80 ? INTEL_PT_CFE_IP : INTEL_PT_CFE;
340 packet->count = buf[2] & 0x1f;
341 packet->payload = buf[3];
345 static int intel_pt_get_evd(const unsigned char *buf, size_t len,
346 struct intel_pt_pkt *packet)
349 return INTEL_PT_NEED_MORE_BYTES;
350 packet->type = INTEL_PT_EVD;
351 packet->count = buf[2] & 0x3f;
352 packet->payload = buf[3];
353 memcpy_le64(&packet->payload, buf + 3, 8);
357 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
358 struct intel_pt_pkt *packet)
361 return INTEL_PT_NEED_MORE_BYTES;
363 if ((buf[1] & 0x1f) == 0x12)
364 return intel_pt_get_ptwrite(buf, len, packet);
367 case 0xa3: /* Long TNT */
368 return intel_pt_get_long_tnt(buf, len, packet);
370 return intel_pt_get_pip(buf, len, packet);
371 case 0x83: /* TraceStop */
372 return intel_pt_get_tracestop(packet);
374 return intel_pt_get_cbr(buf, len, packet);
375 case 0xc8: /* VMCS */
376 return intel_pt_get_vmcs(buf, len, packet);
378 return intel_pt_get_ovf(packet);
380 return intel_pt_get_psb(buf, len, packet);
381 case 0x23: /* PSBEND */
382 return intel_pt_get_psbend(packet);
384 return intel_pt_get_tma(buf, len, packet);
385 case 0xC3: /* 3-byte header */
386 return intel_pt_get_3byte(buf, len, packet);
387 case 0x62: /* EXSTOP no IP */
388 return intel_pt_get_exstop(packet);
389 case 0xE2: /* EXSTOP with IP */
390 return intel_pt_get_exstop_ip(packet);
391 case 0xC2: /* MWAIT */
392 return intel_pt_get_mwait(buf, len, packet);
393 case 0x22: /* PWRE */
394 return intel_pt_get_pwre(buf, len, packet);
395 case 0xA2: /* PWRX */
396 return intel_pt_get_pwrx(buf, len, packet);
398 return intel_pt_get_bbp(buf, len, packet);
399 case 0x33: /* BEP no IP */
400 return intel_pt_get_bep(len, packet);
401 case 0xb3: /* BEP with IP */
402 return intel_pt_get_bep_ip(len, packet);
404 return intel_pt_get_cfe(buf, len, packet);
406 return intel_pt_get_evd(buf, len, packet);
408 return INTEL_PT_BAD_PACKET;
412 static int intel_pt_get_short_tnt(unsigned int byte,
413 struct intel_pt_pkt *packet)
417 for (count = 6; count; count--) {
423 packet->type = INTEL_PT_TNT;
424 packet->count = count;
425 packet->payload = (uint64_t)byte << 57;
430 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
431 size_t len, struct intel_pt_pkt *packet)
433 unsigned int offs = 1, shift;
434 uint64_t payload = byte >> 3;
438 for (shift = 5; byte & 1; shift += 7) {
440 return INTEL_PT_BAD_PACKET;
442 return INTEL_PT_NEED_MORE_BYTES;
444 payload |= ((uint64_t)byte >> 1) << shift;
447 packet->type = INTEL_PT_CYC;
448 packet->payload = payload;
452 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
453 const unsigned char *buf, size_t len,
454 struct intel_pt_pkt *packet)
458 packet->count = byte >> 5;
460 switch (packet->count) {
466 return INTEL_PT_NEED_MORE_BYTES;
468 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
472 return INTEL_PT_NEED_MORE_BYTES;
474 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
479 return INTEL_PT_NEED_MORE_BYTES;
481 memcpy_le64(&packet->payload, buf + 1, 6);
485 return INTEL_PT_NEED_MORE_BYTES;
487 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
490 return INTEL_PT_BAD_PACKET;
498 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
499 struct intel_pt_pkt *packet)
502 return INTEL_PT_NEED_MORE_BYTES;
504 switch (buf[1] >> 5) {
506 packet->type = INTEL_PT_MODE_EXEC;
507 packet->count = buf[1];
508 switch (buf[1] & 3) {
510 packet->payload = 16;
513 packet->payload = 64;
516 packet->payload = 32;
519 return INTEL_PT_BAD_PACKET;
523 packet->type = INTEL_PT_MODE_TSX;
524 if ((buf[1] & 3) == 3)
525 return INTEL_PT_BAD_PACKET;
526 packet->payload = buf[1] & 3;
529 return INTEL_PT_BAD_PACKET;
535 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
536 struct intel_pt_pkt *packet)
539 return INTEL_PT_NEED_MORE_BYTES;
540 packet->type = INTEL_PT_TSC;
541 memcpy_le64(&packet->payload, buf + 1, 7);
545 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
546 struct intel_pt_pkt *packet)
549 return INTEL_PT_NEED_MORE_BYTES;
550 packet->type = INTEL_PT_MTC;
551 packet->payload = buf[1];
555 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
556 struct intel_pt_pkt *packet,
557 enum intel_pt_pkt_ctx ctx)
561 memset(packet, 0, sizeof(struct intel_pt_pkt));
564 return INTEL_PT_NEED_MORE_BYTES;
569 case INTEL_PT_NO_CTX:
571 case INTEL_PT_BLK_4_CTX:
572 if ((byte & 0x7) == 4)
573 return intel_pt_get_bip_4(buf, len, packet);
575 case INTEL_PT_BLK_8_CTX:
576 if ((byte & 0x7) == 4)
577 return intel_pt_get_bip_8(buf, len, packet);
583 if (!(byte & BIT(0))) {
585 return intel_pt_get_pad(packet);
587 return intel_pt_get_ext(buf, len, packet);
588 return intel_pt_get_short_tnt(byte, packet);
592 return intel_pt_get_cyc(byte, buf, len, packet);
594 switch (byte & 0x1f) {
596 return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
598 return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
601 return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
604 return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
608 return intel_pt_get_mode(buf, len, packet);
610 return intel_pt_get_tsc(buf, len, packet);
612 return intel_pt_get_mtc(buf, len, packet);
614 return INTEL_PT_BAD_PACKET;
617 return INTEL_PT_BAD_PACKET;
621 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
622 enum intel_pt_pkt_ctx *ctx)
624 switch (packet->type) {
634 case INTEL_PT_EXSTOP:
635 case INTEL_PT_EXSTOP_IP:
642 case INTEL_PT_TIP_PGD:
643 case INTEL_PT_TIP_PGE:
644 case INTEL_PT_MODE_EXEC:
645 case INTEL_PT_MODE_TSX:
649 case INTEL_PT_TRACESTOP:
651 case INTEL_PT_PSBEND:
652 case INTEL_PT_PTWRITE:
653 case INTEL_PT_PTWRITE_IP:
656 case INTEL_PT_BEP_IP:
658 case INTEL_PT_CFE_IP:
660 *ctx = INTEL_PT_NO_CTX;
664 *ctx = INTEL_PT_BLK_4_CTX;
666 *ctx = INTEL_PT_BLK_8_CTX;
673 int intel_pt_get_packet(const unsigned char *buf, size_t len,
674 struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
678 ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
680 while (ret < 8 && len > (size_t)ret && !buf[ret])
682 intel_pt_upd_pkt_ctx(packet, ctx);
687 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
691 unsigned long long payload = packet->payload;
692 const char *name = intel_pt_pkt_name(packet->type);
694 switch (packet->type) {
698 case INTEL_PT_PSBEND:
699 case INTEL_PT_TRACESTOP:
701 return snprintf(buf, buf_len, "%s", name);
703 size_t blen = buf_len;
705 ret = snprintf(buf, blen, "%s ", name);
710 for (i = 0; i < packet->count; i++) {
712 ret = snprintf(buf, blen, "T");
714 ret = snprintf(buf, blen, "N");
721 ret = snprintf(buf, blen, " (%d)", packet->count);
725 return buf_len - blen;
727 case INTEL_PT_TIP_PGD:
728 case INTEL_PT_TIP_PGE:
731 if (!(packet->count))
732 return snprintf(buf, buf_len, "%s no ip", name);
740 return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
742 return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
743 (unsigned)payload, packet->count);
744 case INTEL_PT_MODE_EXEC:
745 return snprintf(buf, buf_len, "%s IF:%d %lld",
746 name, !!(packet->count & 4), payload);
747 case INTEL_PT_MODE_TSX:
748 return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
749 name, (unsigned)(payload >> 1) & 1,
750 (unsigned)payload & 1);
752 nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
753 payload &= ~INTEL_PT_VMX_NR_FLAG;
754 ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
755 name, payload >> 1, nr);
757 case INTEL_PT_PTWRITE:
758 return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
759 case INTEL_PT_PTWRITE_IP:
760 return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
762 case INTEL_PT_EXSTOP:
763 return snprintf(buf, buf_len, "%s IP:0", name);
764 case INTEL_PT_BEP_IP:
765 case INTEL_PT_EXSTOP_IP:
766 return snprintf(buf, buf_len, "%s IP:1", name);
768 return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
769 name, payload, (unsigned int)(payload & 0xff),
770 (unsigned int)((payload >> 32) & 0x3));
772 return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
773 name, payload, !!(payload & 0x80),
774 (unsigned int)((payload >> 12) & 0xf),
775 (unsigned int)((payload >> 8) & 0xf));
777 return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
779 (unsigned int)((payload >> 4) & 0xf),
780 (unsigned int)(payload & 0xf),
781 (unsigned int)((payload >> 8) & 0xf));
783 return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
784 name, packet->count ? "4" : "8", payload);
786 return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
787 name, packet->count, payload);
789 case INTEL_PT_CFE_IP:
790 return snprintf(buf, buf_len, "%s IP:%d Type 0x%02x Vector 0x%llx",
791 name, packet->type == INTEL_PT_CFE_IP, packet->count, payload);
793 return snprintf(buf, buf_len, "%s Type 0x%02x Payload 0x%llx",
794 name, packet->count, payload);
798 return snprintf(buf, buf_len, "%s 0x%llx (%d)",
799 name, payload, packet->count);