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 == __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",
69 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
71 return packet_name[type];
74 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
75 struct intel_pt_pkt *packet)
81 return INTEL_PT_NEED_MORE_BYTES;
83 payload = le64_to_cpu(*(uint64_t *)buf);
85 for (count = 47; count; count--) {
91 packet->type = INTEL_PT_TNT;
92 packet->count = count;
93 packet->payload = payload << 1;
97 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
98 struct intel_pt_pkt *packet)
100 uint64_t payload = 0;
103 return INTEL_PT_NEED_MORE_BYTES;
105 packet->type = INTEL_PT_PIP;
106 memcpy_le64(&payload, buf + 2, 6);
107 packet->payload = payload;
112 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
114 packet->type = INTEL_PT_TRACESTOP;
118 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
119 struct intel_pt_pkt *packet)
122 return INTEL_PT_NEED_MORE_BYTES;
123 packet->type = INTEL_PT_CBR;
124 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
128 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
129 struct intel_pt_pkt *packet)
131 unsigned int count = (52 - 5) >> 3;
133 if (count < 1 || count > 7)
134 return INTEL_PT_BAD_PACKET;
137 return INTEL_PT_NEED_MORE_BYTES;
139 packet->type = INTEL_PT_VMCS;
140 packet->count = count;
141 memcpy_le64(&packet->payload, buf + 2, count);
146 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
148 packet->type = INTEL_PT_OVF;
152 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
153 struct intel_pt_pkt *packet)
158 return INTEL_PT_NEED_MORE_BYTES;
160 for (i = 2; i < 16; i += 2) {
161 if (buf[i] != 2 || buf[i + 1] != 0x82)
162 return INTEL_PT_BAD_PACKET;
165 packet->type = INTEL_PT_PSB;
169 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
171 packet->type = INTEL_PT_PSBEND;
175 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
176 struct intel_pt_pkt *packet)
179 return INTEL_PT_NEED_MORE_BYTES;
181 packet->type = INTEL_PT_TMA;
182 packet->payload = buf[2] | (buf[3] << 8);
183 packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
187 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
189 packet->type = INTEL_PT_PAD;
193 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
194 struct intel_pt_pkt *packet)
197 return INTEL_PT_NEED_MORE_BYTES;
198 packet->type = INTEL_PT_MNT;
199 memcpy_le64(&packet->payload, buf + 3, 8);
204 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
205 struct intel_pt_pkt *packet)
208 return INTEL_PT_NEED_MORE_BYTES;
212 return intel_pt_get_mnt(buf, len, packet);
214 return INTEL_PT_BAD_PACKET;
218 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
219 struct intel_pt_pkt *packet)
221 packet->count = (buf[1] >> 5) & 0x3;
222 packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
225 switch (packet->count) {
228 return INTEL_PT_NEED_MORE_BYTES;
229 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
233 return INTEL_PT_NEED_MORE_BYTES;
234 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
237 return INTEL_PT_BAD_PACKET;
241 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
243 packet->type = INTEL_PT_EXSTOP;
247 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
249 packet->type = INTEL_PT_EXSTOP_IP;
253 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
254 struct intel_pt_pkt *packet)
257 return INTEL_PT_NEED_MORE_BYTES;
258 packet->type = INTEL_PT_MWAIT;
259 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
263 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
264 struct intel_pt_pkt *packet)
267 return INTEL_PT_NEED_MORE_BYTES;
268 packet->type = INTEL_PT_PWRE;
269 memcpy_le64(&packet->payload, buf + 2, 2);
273 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
274 struct intel_pt_pkt *packet)
277 return INTEL_PT_NEED_MORE_BYTES;
278 packet->type = INTEL_PT_PWRX;
279 memcpy_le64(&packet->payload, buf + 2, 5);
283 static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
284 struct intel_pt_pkt *packet)
287 return INTEL_PT_NEED_MORE_BYTES;
288 packet->type = INTEL_PT_BBP;
289 packet->count = buf[2] >> 7;
290 packet->payload = buf[2] & 0x1f;
294 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
295 struct intel_pt_pkt *packet)
298 return INTEL_PT_NEED_MORE_BYTES;
299 packet->type = INTEL_PT_BIP;
300 packet->count = buf[0] >> 3;
301 memcpy_le64(&packet->payload, buf + 1, 4);
305 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
306 struct intel_pt_pkt *packet)
309 return INTEL_PT_NEED_MORE_BYTES;
310 packet->type = INTEL_PT_BIP;
311 packet->count = buf[0] >> 3;
312 memcpy_le64(&packet->payload, buf + 1, 8);
316 static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
319 return INTEL_PT_NEED_MORE_BYTES;
320 packet->type = INTEL_PT_BEP;
324 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
327 return INTEL_PT_NEED_MORE_BYTES;
328 packet->type = INTEL_PT_BEP_IP;
332 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
333 struct intel_pt_pkt *packet)
336 return INTEL_PT_NEED_MORE_BYTES;
338 if ((buf[1] & 0x1f) == 0x12)
339 return intel_pt_get_ptwrite(buf, len, packet);
342 case 0xa3: /* Long TNT */
343 return intel_pt_get_long_tnt(buf, len, packet);
345 return intel_pt_get_pip(buf, len, packet);
346 case 0x83: /* TraceStop */
347 return intel_pt_get_tracestop(packet);
349 return intel_pt_get_cbr(buf, len, packet);
350 case 0xc8: /* VMCS */
351 return intel_pt_get_vmcs(buf, len, packet);
353 return intel_pt_get_ovf(packet);
355 return intel_pt_get_psb(buf, len, packet);
356 case 0x23: /* PSBEND */
357 return intel_pt_get_psbend(packet);
359 return intel_pt_get_tma(buf, len, packet);
360 case 0xC3: /* 3-byte header */
361 return intel_pt_get_3byte(buf, len, packet);
362 case 0x62: /* EXSTOP no IP */
363 return intel_pt_get_exstop(packet);
364 case 0xE2: /* EXSTOP with IP */
365 return intel_pt_get_exstop_ip(packet);
366 case 0xC2: /* MWAIT */
367 return intel_pt_get_mwait(buf, len, packet);
368 case 0x22: /* PWRE */
369 return intel_pt_get_pwre(buf, len, packet);
370 case 0xA2: /* PWRX */
371 return intel_pt_get_pwrx(buf, len, packet);
373 return intel_pt_get_bbp(buf, len, packet);
374 case 0x33: /* BEP no IP */
375 return intel_pt_get_bep(len, packet);
376 case 0xb3: /* BEP with IP */
377 return intel_pt_get_bep_ip(len, packet);
379 return INTEL_PT_BAD_PACKET;
383 static int intel_pt_get_short_tnt(unsigned int byte,
384 struct intel_pt_pkt *packet)
388 for (count = 6; count; count--) {
394 packet->type = INTEL_PT_TNT;
395 packet->count = count;
396 packet->payload = (uint64_t)byte << 57;
401 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
402 size_t len, struct intel_pt_pkt *packet)
404 unsigned int offs = 1, shift;
405 uint64_t payload = byte >> 3;
409 for (shift = 5; byte & 1; shift += 7) {
411 return INTEL_PT_BAD_PACKET;
413 return INTEL_PT_NEED_MORE_BYTES;
415 payload |= ((uint64_t)byte >> 1) << shift;
418 packet->type = INTEL_PT_CYC;
419 packet->payload = payload;
423 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
424 const unsigned char *buf, size_t len,
425 struct intel_pt_pkt *packet)
429 packet->count = byte >> 5;
431 switch (packet->count) {
437 return INTEL_PT_NEED_MORE_BYTES;
439 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
443 return INTEL_PT_NEED_MORE_BYTES;
445 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
450 return INTEL_PT_NEED_MORE_BYTES;
452 memcpy_le64(&packet->payload, buf + 1, 6);
456 return INTEL_PT_NEED_MORE_BYTES;
458 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
461 return INTEL_PT_BAD_PACKET;
469 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
470 struct intel_pt_pkt *packet)
473 return INTEL_PT_NEED_MORE_BYTES;
475 switch (buf[1] >> 5) {
477 packet->type = INTEL_PT_MODE_EXEC;
478 switch (buf[1] & 3) {
480 packet->payload = 16;
483 packet->payload = 64;
486 packet->payload = 32;
489 return INTEL_PT_BAD_PACKET;
493 packet->type = INTEL_PT_MODE_TSX;
494 if ((buf[1] & 3) == 3)
495 return INTEL_PT_BAD_PACKET;
496 packet->payload = buf[1] & 3;
499 return INTEL_PT_BAD_PACKET;
505 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
506 struct intel_pt_pkt *packet)
509 return INTEL_PT_NEED_MORE_BYTES;
510 packet->type = INTEL_PT_TSC;
511 memcpy_le64(&packet->payload, buf + 1, 7);
515 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
516 struct intel_pt_pkt *packet)
519 return INTEL_PT_NEED_MORE_BYTES;
520 packet->type = INTEL_PT_MTC;
521 packet->payload = buf[1];
525 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
526 struct intel_pt_pkt *packet,
527 enum intel_pt_pkt_ctx ctx)
531 memset(packet, 0, sizeof(struct intel_pt_pkt));
534 return INTEL_PT_NEED_MORE_BYTES;
539 case INTEL_PT_NO_CTX:
541 case INTEL_PT_BLK_4_CTX:
542 if ((byte & 0x7) == 4)
543 return intel_pt_get_bip_4(buf, len, packet);
545 case INTEL_PT_BLK_8_CTX:
546 if ((byte & 0x7) == 4)
547 return intel_pt_get_bip_8(buf, len, packet);
553 if (!(byte & BIT(0))) {
555 return intel_pt_get_pad(packet);
557 return intel_pt_get_ext(buf, len, packet);
558 return intel_pt_get_short_tnt(byte, packet);
562 return intel_pt_get_cyc(byte, buf, len, packet);
564 switch (byte & 0x1f) {
566 return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
568 return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
571 return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
574 return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
578 return intel_pt_get_mode(buf, len, packet);
580 return intel_pt_get_tsc(buf, len, packet);
582 return intel_pt_get_mtc(buf, len, packet);
584 return INTEL_PT_BAD_PACKET;
587 return INTEL_PT_BAD_PACKET;
591 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
592 enum intel_pt_pkt_ctx *ctx)
594 switch (packet->type) {
604 case INTEL_PT_EXSTOP:
605 case INTEL_PT_EXSTOP_IP:
612 case INTEL_PT_TIP_PGD:
613 case INTEL_PT_TIP_PGE:
614 case INTEL_PT_MODE_EXEC:
615 case INTEL_PT_MODE_TSX:
619 case INTEL_PT_TRACESTOP:
621 case INTEL_PT_PSBEND:
622 case INTEL_PT_PTWRITE:
623 case INTEL_PT_PTWRITE_IP:
626 case INTEL_PT_BEP_IP:
627 *ctx = INTEL_PT_NO_CTX;
631 *ctx = INTEL_PT_BLK_4_CTX;
633 *ctx = INTEL_PT_BLK_8_CTX;
640 int intel_pt_get_packet(const unsigned char *buf, size_t len,
641 struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
645 ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
647 while (ret < 8 && len > (size_t)ret && !buf[ret])
649 intel_pt_upd_pkt_ctx(packet, ctx);
654 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
658 unsigned long long payload = packet->payload;
659 const char *name = intel_pt_pkt_name(packet->type);
661 switch (packet->type) {
665 case INTEL_PT_PSBEND:
666 case INTEL_PT_TRACESTOP:
668 return snprintf(buf, buf_len, "%s", name);
670 size_t blen = buf_len;
672 ret = snprintf(buf, blen, "%s ", name);
677 for (i = 0; i < packet->count; i++) {
679 ret = snprintf(buf, blen, "T");
681 ret = snprintf(buf, blen, "N");
688 ret = snprintf(buf, blen, " (%d)", packet->count);
692 return buf_len - blen;
694 case INTEL_PT_TIP_PGD:
695 case INTEL_PT_TIP_PGE:
698 if (!(packet->count))
699 return snprintf(buf, buf_len, "%s no ip", name);
707 return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
709 return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
710 (unsigned)payload, packet->count);
711 case INTEL_PT_MODE_EXEC:
712 return snprintf(buf, buf_len, "%s %lld", name, payload);
713 case INTEL_PT_MODE_TSX:
714 return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
715 name, (unsigned)(payload >> 1) & 1,
716 (unsigned)payload & 1);
718 nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
719 payload &= ~INTEL_PT_VMX_NR_FLAG;
720 ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
721 name, payload >> 1, nr);
723 case INTEL_PT_PTWRITE:
724 return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
725 case INTEL_PT_PTWRITE_IP:
726 return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
728 case INTEL_PT_EXSTOP:
729 return snprintf(buf, buf_len, "%s IP:0", name);
730 case INTEL_PT_BEP_IP:
731 case INTEL_PT_EXSTOP_IP:
732 return snprintf(buf, buf_len, "%s IP:1", name);
734 return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
735 name, payload, (unsigned int)(payload & 0xff),
736 (unsigned int)((payload >> 32) & 0x3));
738 return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
739 name, payload, !!(payload & 0x80),
740 (unsigned int)((payload >> 12) & 0xf),
741 (unsigned int)((payload >> 8) & 0xf));
743 return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
745 (unsigned int)((payload >> 4) & 0xf),
746 (unsigned int)(payload & 0xf),
747 (unsigned int)((payload >> 8) & 0xf));
749 return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
750 name, packet->count ? "4" : "8", payload);
752 return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
753 name, packet->count, payload);
757 return snprintf(buf, buf_len, "%s 0x%llx (%d)",
758 name, payload, packet->count);