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)
21 #if __BYTE_ORDER == __BIG_ENDIAN
22 #define le16_to_cpu bswap_16
23 #define le32_to_cpu bswap_32
24 #define le64_to_cpu bswap_64
25 #define memcpy_le64(d, s, n) do { \
26 memcpy((d), (s), (n)); \
27 *(d) = le64_to_cpu(*(d)); \
33 #define memcpy_le64 memcpy
36 static const char * const packet_name
[] = {
37 [INTEL_PT_BAD
] = "Bad Packet!",
38 [INTEL_PT_PAD
] = "PAD",
39 [INTEL_PT_TNT
] = "TNT",
40 [INTEL_PT_TIP_PGD
] = "TIP.PGD",
41 [INTEL_PT_TIP_PGE
] = "TIP.PGE",
42 [INTEL_PT_TSC
] = "TSC",
43 [INTEL_PT_TMA
] = "TMA",
44 [INTEL_PT_MODE_EXEC
] = "MODE.Exec",
45 [INTEL_PT_MODE_TSX
] = "MODE.TSX",
46 [INTEL_PT_MTC
] = "MTC",
47 [INTEL_PT_TIP
] = "TIP",
48 [INTEL_PT_FUP
] = "FUP",
49 [INTEL_PT_CYC
] = "CYC",
50 [INTEL_PT_VMCS
] = "VMCS",
51 [INTEL_PT_PSB
] = "PSB",
52 [INTEL_PT_PSBEND
] = "PSBEND",
53 [INTEL_PT_CBR
] = "CBR",
54 [INTEL_PT_TRACESTOP
] = "TraceSTOP",
55 [INTEL_PT_PIP
] = "PIP",
56 [INTEL_PT_OVF
] = "OVF",
57 [INTEL_PT_MNT
] = "MNT",
58 [INTEL_PT_PTWRITE
] = "PTWRITE",
59 [INTEL_PT_PTWRITE_IP
] = "PTWRITE",
60 [INTEL_PT_EXSTOP
] = "EXSTOP",
61 [INTEL_PT_EXSTOP_IP
] = "EXSTOP",
62 [INTEL_PT_MWAIT
] = "MWAIT",
63 [INTEL_PT_PWRE
] = "PWRE",
64 [INTEL_PT_PWRX
] = "PWRX",
65 [INTEL_PT_BBP
] = "BBP",
66 [INTEL_PT_BIP
] = "BIP",
67 [INTEL_PT_BEP
] = "BEP",
68 [INTEL_PT_BEP_IP
] = "BEP",
71 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type
)
73 return packet_name
[type
];
76 static int intel_pt_get_long_tnt(const unsigned char *buf
, size_t len
,
77 struct intel_pt_pkt
*packet
)
83 return INTEL_PT_NEED_MORE_BYTES
;
85 payload
= le64_to_cpu(*(uint64_t *)buf
);
87 for (count
= 47; count
; count
--) {
93 packet
->type
= INTEL_PT_TNT
;
94 packet
->count
= count
;
95 packet
->payload
= payload
<< 1;
99 static int intel_pt_get_pip(const unsigned char *buf
, size_t len
,
100 struct intel_pt_pkt
*packet
)
102 uint64_t payload
= 0;
105 return INTEL_PT_NEED_MORE_BYTES
;
107 packet
->type
= INTEL_PT_PIP
;
108 memcpy_le64(&payload
, buf
+ 2, 6);
109 packet
->payload
= payload
>> 1;
111 packet
->payload
|= NR_FLAG
;
116 static int intel_pt_get_tracestop(struct intel_pt_pkt
*packet
)
118 packet
->type
= INTEL_PT_TRACESTOP
;
122 static int intel_pt_get_cbr(const unsigned char *buf
, size_t len
,
123 struct intel_pt_pkt
*packet
)
126 return INTEL_PT_NEED_MORE_BYTES
;
127 packet
->type
= INTEL_PT_CBR
;
128 packet
->payload
= le16_to_cpu(*(uint16_t *)(buf
+ 2));
132 static int intel_pt_get_vmcs(const unsigned char *buf
, size_t len
,
133 struct intel_pt_pkt
*packet
)
135 unsigned int count
= (52 - 5) >> 3;
137 if (count
< 1 || count
> 7)
138 return INTEL_PT_BAD_PACKET
;
141 return INTEL_PT_NEED_MORE_BYTES
;
143 packet
->type
= INTEL_PT_VMCS
;
144 packet
->count
= count
;
145 memcpy_le64(&packet
->payload
, buf
+ 2, count
);
150 static int intel_pt_get_ovf(struct intel_pt_pkt
*packet
)
152 packet
->type
= INTEL_PT_OVF
;
156 static int intel_pt_get_psb(const unsigned char *buf
, size_t len
,
157 struct intel_pt_pkt
*packet
)
162 return INTEL_PT_NEED_MORE_BYTES
;
164 for (i
= 2; i
< 16; i
+= 2) {
165 if (buf
[i
] != 2 || buf
[i
+ 1] != 0x82)
166 return INTEL_PT_BAD_PACKET
;
169 packet
->type
= INTEL_PT_PSB
;
173 static int intel_pt_get_psbend(struct intel_pt_pkt
*packet
)
175 packet
->type
= INTEL_PT_PSBEND
;
179 static int intel_pt_get_tma(const unsigned char *buf
, size_t len
,
180 struct intel_pt_pkt
*packet
)
183 return INTEL_PT_NEED_MORE_BYTES
;
185 packet
->type
= INTEL_PT_TMA
;
186 packet
->payload
= buf
[2] | (buf
[3] << 8);
187 packet
->count
= buf
[5] | ((buf
[6] & BIT(0)) << 8);
191 static int intel_pt_get_pad(struct intel_pt_pkt
*packet
)
193 packet
->type
= INTEL_PT_PAD
;
197 static int intel_pt_get_mnt(const unsigned char *buf
, size_t len
,
198 struct intel_pt_pkt
*packet
)
201 return INTEL_PT_NEED_MORE_BYTES
;
202 packet
->type
= INTEL_PT_MNT
;
203 memcpy_le64(&packet
->payload
, buf
+ 3, 8);
208 static int intel_pt_get_3byte(const unsigned char *buf
, size_t len
,
209 struct intel_pt_pkt
*packet
)
212 return INTEL_PT_NEED_MORE_BYTES
;
216 return intel_pt_get_mnt(buf
, len
, packet
);
218 return INTEL_PT_BAD_PACKET
;
222 static int intel_pt_get_ptwrite(const unsigned char *buf
, size_t len
,
223 struct intel_pt_pkt
*packet
)
225 packet
->count
= (buf
[1] >> 5) & 0x3;
226 packet
->type
= buf
[1] & BIT(7) ? INTEL_PT_PTWRITE_IP
:
229 switch (packet
->count
) {
232 return INTEL_PT_NEED_MORE_BYTES
;
233 packet
->payload
= le32_to_cpu(*(uint32_t *)(buf
+ 2));
237 return INTEL_PT_NEED_MORE_BYTES
;
238 packet
->payload
= le64_to_cpu(*(uint64_t *)(buf
+ 2));
241 return INTEL_PT_BAD_PACKET
;
245 static int intel_pt_get_exstop(struct intel_pt_pkt
*packet
)
247 packet
->type
= INTEL_PT_EXSTOP
;
251 static int intel_pt_get_exstop_ip(struct intel_pt_pkt
*packet
)
253 packet
->type
= INTEL_PT_EXSTOP_IP
;
257 static int intel_pt_get_mwait(const unsigned char *buf
, size_t len
,
258 struct intel_pt_pkt
*packet
)
261 return INTEL_PT_NEED_MORE_BYTES
;
262 packet
->type
= INTEL_PT_MWAIT
;
263 packet
->payload
= le64_to_cpu(*(uint64_t *)(buf
+ 2));
267 static int intel_pt_get_pwre(const unsigned char *buf
, size_t len
,
268 struct intel_pt_pkt
*packet
)
271 return INTEL_PT_NEED_MORE_BYTES
;
272 packet
->type
= INTEL_PT_PWRE
;
273 memcpy_le64(&packet
->payload
, buf
+ 2, 2);
277 static int intel_pt_get_pwrx(const unsigned char *buf
, size_t len
,
278 struct intel_pt_pkt
*packet
)
281 return INTEL_PT_NEED_MORE_BYTES
;
282 packet
->type
= INTEL_PT_PWRX
;
283 memcpy_le64(&packet
->payload
, buf
+ 2, 5);
287 static int intel_pt_get_bbp(const unsigned char *buf
, size_t len
,
288 struct intel_pt_pkt
*packet
)
291 return INTEL_PT_NEED_MORE_BYTES
;
292 packet
->type
= INTEL_PT_BBP
;
293 packet
->count
= buf
[2] >> 7;
294 packet
->payload
= buf
[2] & 0x1f;
298 static int intel_pt_get_bip_4(const unsigned char *buf
, size_t len
,
299 struct intel_pt_pkt
*packet
)
302 return INTEL_PT_NEED_MORE_BYTES
;
303 packet
->type
= INTEL_PT_BIP
;
304 packet
->count
= buf
[0] >> 3;
305 memcpy_le64(&packet
->payload
, buf
+ 1, 4);
309 static int intel_pt_get_bip_8(const unsigned char *buf
, size_t len
,
310 struct intel_pt_pkt
*packet
)
313 return INTEL_PT_NEED_MORE_BYTES
;
314 packet
->type
= INTEL_PT_BIP
;
315 packet
->count
= buf
[0] >> 3;
316 memcpy_le64(&packet
->payload
, buf
+ 1, 8);
320 static int intel_pt_get_bep(size_t len
, struct intel_pt_pkt
*packet
)
323 return INTEL_PT_NEED_MORE_BYTES
;
324 packet
->type
= INTEL_PT_BEP
;
328 static int intel_pt_get_bep_ip(size_t len
, struct intel_pt_pkt
*packet
)
331 return INTEL_PT_NEED_MORE_BYTES
;
332 packet
->type
= INTEL_PT_BEP_IP
;
336 static int intel_pt_get_ext(const unsigned char *buf
, size_t len
,
337 struct intel_pt_pkt
*packet
)
340 return INTEL_PT_NEED_MORE_BYTES
;
342 if ((buf
[1] & 0x1f) == 0x12)
343 return intel_pt_get_ptwrite(buf
, len
, packet
);
346 case 0xa3: /* Long TNT */
347 return intel_pt_get_long_tnt(buf
, len
, packet
);
349 return intel_pt_get_pip(buf
, len
, packet
);
350 case 0x83: /* TraceStop */
351 return intel_pt_get_tracestop(packet
);
353 return intel_pt_get_cbr(buf
, len
, packet
);
354 case 0xc8: /* VMCS */
355 return intel_pt_get_vmcs(buf
, len
, packet
);
357 return intel_pt_get_ovf(packet
);
359 return intel_pt_get_psb(buf
, len
, packet
);
360 case 0x23: /* PSBEND */
361 return intel_pt_get_psbend(packet
);
363 return intel_pt_get_tma(buf
, len
, packet
);
364 case 0xC3: /* 3-byte header */
365 return intel_pt_get_3byte(buf
, len
, packet
);
366 case 0x62: /* EXSTOP no IP */
367 return intel_pt_get_exstop(packet
);
368 case 0xE2: /* EXSTOP with IP */
369 return intel_pt_get_exstop_ip(packet
);
370 case 0xC2: /* MWAIT */
371 return intel_pt_get_mwait(buf
, len
, packet
);
372 case 0x22: /* PWRE */
373 return intel_pt_get_pwre(buf
, len
, packet
);
374 case 0xA2: /* PWRX */
375 return intel_pt_get_pwrx(buf
, len
, packet
);
377 return intel_pt_get_bbp(buf
, len
, packet
);
378 case 0x33: /* BEP no IP */
379 return intel_pt_get_bep(len
, packet
);
380 case 0xb3: /* BEP with IP */
381 return intel_pt_get_bep_ip(len
, packet
);
383 return INTEL_PT_BAD_PACKET
;
387 static int intel_pt_get_short_tnt(unsigned int byte
,
388 struct intel_pt_pkt
*packet
)
392 for (count
= 6; count
; count
--) {
398 packet
->type
= INTEL_PT_TNT
;
399 packet
->count
= count
;
400 packet
->payload
= (uint64_t)byte
<< 57;
405 static int intel_pt_get_cyc(unsigned int byte
, const unsigned char *buf
,
406 size_t len
, struct intel_pt_pkt
*packet
)
408 unsigned int offs
= 1, shift
;
409 uint64_t payload
= byte
>> 3;
413 for (shift
= 5; byte
& 1; shift
+= 7) {
415 return INTEL_PT_BAD_PACKET
;
417 return INTEL_PT_NEED_MORE_BYTES
;
419 payload
|= ((uint64_t)byte
>> 1) << shift
;
422 packet
->type
= INTEL_PT_CYC
;
423 packet
->payload
= payload
;
427 static int intel_pt_get_ip(enum intel_pt_pkt_type type
, unsigned int byte
,
428 const unsigned char *buf
, size_t len
,
429 struct intel_pt_pkt
*packet
)
433 packet
->count
= byte
>> 5;
435 switch (packet
->count
) {
441 return INTEL_PT_NEED_MORE_BYTES
;
443 packet
->payload
= le16_to_cpu(*(uint16_t *)(buf
+ 1));
447 return INTEL_PT_NEED_MORE_BYTES
;
449 packet
->payload
= le32_to_cpu(*(uint32_t *)(buf
+ 1));
454 return INTEL_PT_NEED_MORE_BYTES
;
456 memcpy_le64(&packet
->payload
, buf
+ 1, 6);
460 return INTEL_PT_NEED_MORE_BYTES
;
462 packet
->payload
= le64_to_cpu(*(uint64_t *)(buf
+ 1));
465 return INTEL_PT_BAD_PACKET
;
473 static int intel_pt_get_mode(const unsigned char *buf
, size_t len
,
474 struct intel_pt_pkt
*packet
)
477 return INTEL_PT_NEED_MORE_BYTES
;
479 switch (buf
[1] >> 5) {
481 packet
->type
= INTEL_PT_MODE_EXEC
;
482 switch (buf
[1] & 3) {
484 packet
->payload
= 16;
487 packet
->payload
= 64;
490 packet
->payload
= 32;
493 return INTEL_PT_BAD_PACKET
;
497 packet
->type
= INTEL_PT_MODE_TSX
;
498 if ((buf
[1] & 3) == 3)
499 return INTEL_PT_BAD_PACKET
;
500 packet
->payload
= buf
[1] & 3;
503 return INTEL_PT_BAD_PACKET
;
509 static int intel_pt_get_tsc(const unsigned char *buf
, size_t len
,
510 struct intel_pt_pkt
*packet
)
513 return INTEL_PT_NEED_MORE_BYTES
;
514 packet
->type
= INTEL_PT_TSC
;
515 memcpy_le64(&packet
->payload
, buf
+ 1, 7);
519 static int intel_pt_get_mtc(const unsigned char *buf
, size_t len
,
520 struct intel_pt_pkt
*packet
)
523 return INTEL_PT_NEED_MORE_BYTES
;
524 packet
->type
= INTEL_PT_MTC
;
525 packet
->payload
= buf
[1];
529 static int intel_pt_do_get_packet(const unsigned char *buf
, size_t len
,
530 struct intel_pt_pkt
*packet
,
531 enum intel_pt_pkt_ctx ctx
)
535 memset(packet
, 0, sizeof(struct intel_pt_pkt
));
538 return INTEL_PT_NEED_MORE_BYTES
;
543 case INTEL_PT_NO_CTX
:
545 case INTEL_PT_BLK_4_CTX
:
546 if ((byte
& 0x7) == 4)
547 return intel_pt_get_bip_4(buf
, len
, packet
);
549 case INTEL_PT_BLK_8_CTX
:
550 if ((byte
& 0x7) == 4)
551 return intel_pt_get_bip_8(buf
, len
, packet
);
557 if (!(byte
& BIT(0))) {
559 return intel_pt_get_pad(packet
);
561 return intel_pt_get_ext(buf
, len
, packet
);
562 return intel_pt_get_short_tnt(byte
, packet
);
566 return intel_pt_get_cyc(byte
, buf
, len
, packet
);
568 switch (byte
& 0x1f) {
570 return intel_pt_get_ip(INTEL_PT_TIP
, byte
, buf
, len
, packet
);
572 return intel_pt_get_ip(INTEL_PT_TIP_PGE
, byte
, buf
, len
,
575 return intel_pt_get_ip(INTEL_PT_TIP_PGD
, byte
, buf
, len
,
578 return intel_pt_get_ip(INTEL_PT_FUP
, byte
, buf
, len
, packet
);
582 return intel_pt_get_mode(buf
, len
, packet
);
584 return intel_pt_get_tsc(buf
, len
, packet
);
586 return intel_pt_get_mtc(buf
, len
, packet
);
588 return INTEL_PT_BAD_PACKET
;
591 return INTEL_PT_BAD_PACKET
;
595 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt
*packet
,
596 enum intel_pt_pkt_ctx
*ctx
)
598 switch (packet
->type
) {
608 case INTEL_PT_EXSTOP
:
609 case INTEL_PT_EXSTOP_IP
:
616 case INTEL_PT_TIP_PGD
:
617 case INTEL_PT_TIP_PGE
:
618 case INTEL_PT_MODE_EXEC
:
619 case INTEL_PT_MODE_TSX
:
623 case INTEL_PT_TRACESTOP
:
625 case INTEL_PT_PSBEND
:
626 case INTEL_PT_PTWRITE
:
627 case INTEL_PT_PTWRITE_IP
:
630 case INTEL_PT_BEP_IP
:
631 *ctx
= INTEL_PT_NO_CTX
;
635 *ctx
= INTEL_PT_BLK_4_CTX
;
637 *ctx
= INTEL_PT_BLK_8_CTX
;
644 int intel_pt_get_packet(const unsigned char *buf
, size_t len
,
645 struct intel_pt_pkt
*packet
, enum intel_pt_pkt_ctx
*ctx
)
649 ret
= intel_pt_do_get_packet(buf
, len
, packet
, *ctx
);
651 while (ret
< 8 && len
> (size_t)ret
&& !buf
[ret
])
653 intel_pt_upd_pkt_ctx(packet
, ctx
);
658 int intel_pt_pkt_desc(const struct intel_pt_pkt
*packet
, char *buf
,
662 unsigned long long payload
= packet
->payload
;
663 const char *name
= intel_pt_pkt_name(packet
->type
);
665 switch (packet
->type
) {
669 case INTEL_PT_PSBEND
:
670 case INTEL_PT_TRACESTOP
:
672 return snprintf(buf
, buf_len
, "%s", name
);
674 size_t blen
= buf_len
;
676 ret
= snprintf(buf
, blen
, "%s ", name
);
681 for (i
= 0; i
< packet
->count
; i
++) {
683 ret
= snprintf(buf
, blen
, "T");
685 ret
= snprintf(buf
, blen
, "N");
692 ret
= snprintf(buf
, blen
, " (%d)", packet
->count
);
696 return buf_len
- blen
;
698 case INTEL_PT_TIP_PGD
:
699 case INTEL_PT_TIP_PGE
:
702 if (!(packet
->count
))
703 return snprintf(buf
, buf_len
, "%s no ip", name
);
711 return snprintf(buf
, buf_len
, "%s 0x%llx", name
, payload
);
713 return snprintf(buf
, buf_len
, "%s CTC 0x%x FC 0x%x", name
,
714 (unsigned)payload
, packet
->count
);
715 case INTEL_PT_MODE_EXEC
:
716 return snprintf(buf
, buf_len
, "%s %lld", name
, payload
);
717 case INTEL_PT_MODE_TSX
:
718 return snprintf(buf
, buf_len
, "%s TXAbort:%u InTX:%u",
719 name
, (unsigned)(payload
>> 1) & 1,
720 (unsigned)payload
& 1);
722 nr
= packet
->payload
& NR_FLAG
? 1 : 0;
724 ret
= snprintf(buf
, buf_len
, "%s 0x%llx (NR=%d)",
727 case INTEL_PT_PTWRITE
:
728 return snprintf(buf
, buf_len
, "%s 0x%llx IP:0", name
, payload
);
729 case INTEL_PT_PTWRITE_IP
:
730 return snprintf(buf
, buf_len
, "%s 0x%llx IP:1", name
, payload
);
732 case INTEL_PT_EXSTOP
:
733 return snprintf(buf
, buf_len
, "%s IP:0", name
);
734 case INTEL_PT_BEP_IP
:
735 case INTEL_PT_EXSTOP_IP
:
736 return snprintf(buf
, buf_len
, "%s IP:1", name
);
738 return snprintf(buf
, buf_len
, "%s 0x%llx Hints 0x%x Extensions 0x%x",
739 name
, payload
, (unsigned int)(payload
& 0xff),
740 (unsigned int)((payload
>> 32) & 0x3));
742 return snprintf(buf
, buf_len
, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
743 name
, payload
, !!(payload
& 0x80),
744 (unsigned int)((payload
>> 12) & 0xf),
745 (unsigned int)((payload
>> 8) & 0xf));
747 return snprintf(buf
, buf_len
, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
749 (unsigned int)((payload
>> 4) & 0xf),
750 (unsigned int)(payload
& 0xf),
751 (unsigned int)((payload
>> 8) & 0xf));
753 return snprintf(buf
, buf_len
, "%s SZ %s-byte Type 0x%llx",
754 name
, packet
->count
? "4" : "8", payload
);
756 return snprintf(buf
, buf_len
, "%s ID 0x%02x Value 0x%llx",
757 name
, packet
->count
, payload
);
761 return snprintf(buf
, buf_len
, "%s 0x%llx (%d)",
762 name
, payload
, packet
->count
);