2 * intel_pt_pkt_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
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.
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
20 #include <linux/compiler.h>
22 #include "intel-pt-pkt-decoder.h"
24 #define BIT(n) (1 << (n))
26 #define BIT63 ((uint64_t)1 << 63)
30 #if __BYTE_ORDER == __BIG_ENDIAN
31 #define le16_to_cpu bswap_16
32 #define le32_to_cpu bswap_32
33 #define le64_to_cpu bswap_64
34 #define memcpy_le64(d, s, n) do { \
35 memcpy((d), (s), (n)); \
36 *(d) = le64_to_cpu(*(d)); \
42 #define memcpy_le64 memcpy
45 static const char * const packet_name[] = {
46 [INTEL_PT_BAD] = "Bad Packet!",
47 [INTEL_PT_PAD] = "PAD",
48 [INTEL_PT_TNT] = "TNT",
49 [INTEL_PT_TIP_PGD] = "TIP.PGD",
50 [INTEL_PT_TIP_PGE] = "TIP.PGE",
51 [INTEL_PT_TSC] = "TSC",
52 [INTEL_PT_TMA] = "TMA",
53 [INTEL_PT_MODE_EXEC] = "MODE.Exec",
54 [INTEL_PT_MODE_TSX] = "MODE.TSX",
55 [INTEL_PT_MTC] = "MTC",
56 [INTEL_PT_TIP] = "TIP",
57 [INTEL_PT_FUP] = "FUP",
58 [INTEL_PT_CYC] = "CYC",
59 [INTEL_PT_VMCS] = "VMCS",
60 [INTEL_PT_PSB] = "PSB",
61 [INTEL_PT_PSBEND] = "PSBEND",
62 [INTEL_PT_CBR] = "CBR",
63 [INTEL_PT_TRACESTOP] = "TraceSTOP",
64 [INTEL_PT_PIP] = "PIP",
65 [INTEL_PT_OVF] = "OVF",
66 [INTEL_PT_MNT] = "MNT",
67 [INTEL_PT_PTWRITE] = "PTWRITE",
68 [INTEL_PT_PTWRITE_IP] = "PTWRITE",
69 [INTEL_PT_EXSTOP] = "EXSTOP",
70 [INTEL_PT_EXSTOP_IP] = "EXSTOP",
71 [INTEL_PT_MWAIT] = "MWAIT",
72 [INTEL_PT_PWRE] = "PWRE",
73 [INTEL_PT_PWRX] = "PWRX",
76 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
78 return packet_name[type];
81 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
82 struct intel_pt_pkt *packet)
88 return INTEL_PT_NEED_MORE_BYTES;
90 payload = le64_to_cpu(*(uint64_t *)buf);
92 for (count = 47; count; count--) {
98 packet->type = INTEL_PT_TNT;
99 packet->count = count;
100 packet->payload = payload << 1;
104 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
105 struct intel_pt_pkt *packet)
107 uint64_t payload = 0;
110 return INTEL_PT_NEED_MORE_BYTES;
112 packet->type = INTEL_PT_PIP;
113 memcpy_le64(&payload, buf + 2, 6);
114 packet->payload = payload >> 1;
116 packet->payload |= NR_FLAG;
121 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
123 packet->type = INTEL_PT_TRACESTOP;
127 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
128 struct intel_pt_pkt *packet)
131 return INTEL_PT_NEED_MORE_BYTES;
132 packet->type = INTEL_PT_CBR;
133 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
137 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
138 struct intel_pt_pkt *packet)
140 unsigned int count = (52 - 5) >> 3;
142 if (count < 1 || count > 7)
143 return INTEL_PT_BAD_PACKET;
146 return INTEL_PT_NEED_MORE_BYTES;
148 packet->type = INTEL_PT_VMCS;
149 packet->count = count;
150 memcpy_le64(&packet->payload, buf + 2, count);
155 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
157 packet->type = INTEL_PT_OVF;
161 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
162 struct intel_pt_pkt *packet)
167 return INTEL_PT_NEED_MORE_BYTES;
169 for (i = 2; i < 16; i += 2) {
170 if (buf[i] != 2 || buf[i + 1] != 0x82)
171 return INTEL_PT_BAD_PACKET;
174 packet->type = INTEL_PT_PSB;
178 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
180 packet->type = INTEL_PT_PSBEND;
184 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
185 struct intel_pt_pkt *packet)
188 return INTEL_PT_NEED_MORE_BYTES;
190 packet->type = INTEL_PT_TMA;
191 packet->payload = buf[2] | (buf[3] << 8);
192 packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
196 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
198 packet->type = INTEL_PT_PAD;
202 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
203 struct intel_pt_pkt *packet)
206 return INTEL_PT_NEED_MORE_BYTES;
207 packet->type = INTEL_PT_MNT;
208 memcpy_le64(&packet->payload, buf + 3, 8);
213 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
214 struct intel_pt_pkt *packet)
217 return INTEL_PT_NEED_MORE_BYTES;
221 return intel_pt_get_mnt(buf, len, packet);
223 return INTEL_PT_BAD_PACKET;
227 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
228 struct intel_pt_pkt *packet)
230 packet->count = (buf[1] >> 5) & 0x3;
231 packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
234 switch (packet->count) {
237 return INTEL_PT_NEED_MORE_BYTES;
238 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
242 return INTEL_PT_NEED_MORE_BYTES;
243 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
246 return INTEL_PT_BAD_PACKET;
250 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
252 packet->type = INTEL_PT_EXSTOP;
256 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
258 packet->type = INTEL_PT_EXSTOP_IP;
262 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
263 struct intel_pt_pkt *packet)
266 return INTEL_PT_NEED_MORE_BYTES;
267 packet->type = INTEL_PT_MWAIT;
268 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
272 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
273 struct intel_pt_pkt *packet)
276 return INTEL_PT_NEED_MORE_BYTES;
277 packet->type = INTEL_PT_PWRE;
278 memcpy_le64(&packet->payload, buf + 2, 2);
282 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
283 struct intel_pt_pkt *packet)
286 return INTEL_PT_NEED_MORE_BYTES;
287 packet->type = INTEL_PT_PWRX;
288 memcpy_le64(&packet->payload, buf + 2, 5);
292 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
293 struct intel_pt_pkt *packet)
296 return INTEL_PT_NEED_MORE_BYTES;
298 if ((buf[1] & 0x1f) == 0x12)
299 return intel_pt_get_ptwrite(buf, len, packet);
302 case 0xa3: /* Long TNT */
303 return intel_pt_get_long_tnt(buf, len, packet);
305 return intel_pt_get_pip(buf, len, packet);
306 case 0x83: /* TraceStop */
307 return intel_pt_get_tracestop(packet);
309 return intel_pt_get_cbr(buf, len, packet);
310 case 0xc8: /* VMCS */
311 return intel_pt_get_vmcs(buf, len, packet);
313 return intel_pt_get_ovf(packet);
315 return intel_pt_get_psb(buf, len, packet);
316 case 0x23: /* PSBEND */
317 return intel_pt_get_psbend(packet);
319 return intel_pt_get_tma(buf, len, packet);
320 case 0xC3: /* 3-byte header */
321 return intel_pt_get_3byte(buf, len, packet);
322 case 0x62: /* EXSTOP no IP */
323 return intel_pt_get_exstop(packet);
324 case 0xE2: /* EXSTOP with IP */
325 return intel_pt_get_exstop_ip(packet);
326 case 0xC2: /* MWAIT */
327 return intel_pt_get_mwait(buf, len, packet);
328 case 0x22: /* PWRE */
329 return intel_pt_get_pwre(buf, len, packet);
330 case 0xA2: /* PWRX */
331 return intel_pt_get_pwrx(buf, len, packet);
333 return INTEL_PT_BAD_PACKET;
337 static int intel_pt_get_short_tnt(unsigned int byte,
338 struct intel_pt_pkt *packet)
342 for (count = 6; count; count--) {
348 packet->type = INTEL_PT_TNT;
349 packet->count = count;
350 packet->payload = (uint64_t)byte << 57;
355 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
356 size_t len, struct intel_pt_pkt *packet)
358 unsigned int offs = 1, shift;
359 uint64_t payload = byte >> 3;
363 for (shift = 5; byte & 1; shift += 7) {
365 return INTEL_PT_BAD_PACKET;
367 return INTEL_PT_NEED_MORE_BYTES;
369 payload |= (byte >> 1) << shift;
372 packet->type = INTEL_PT_CYC;
373 packet->payload = payload;
377 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
378 const unsigned char *buf, size_t len,
379 struct intel_pt_pkt *packet)
383 packet->count = byte >> 5;
385 switch (packet->count) {
391 return INTEL_PT_NEED_MORE_BYTES;
393 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
397 return INTEL_PT_NEED_MORE_BYTES;
399 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
404 return INTEL_PT_NEED_MORE_BYTES;
406 memcpy_le64(&packet->payload, buf + 1, 6);
410 return INTEL_PT_NEED_MORE_BYTES;
412 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
415 return INTEL_PT_BAD_PACKET;
423 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
424 struct intel_pt_pkt *packet)
427 return INTEL_PT_NEED_MORE_BYTES;
429 switch (buf[1] >> 5) {
431 packet->type = INTEL_PT_MODE_EXEC;
432 switch (buf[1] & 3) {
434 packet->payload = 16;
437 packet->payload = 64;
440 packet->payload = 32;
443 return INTEL_PT_BAD_PACKET;
447 packet->type = INTEL_PT_MODE_TSX;
448 if ((buf[1] & 3) == 3)
449 return INTEL_PT_BAD_PACKET;
450 packet->payload = buf[1] & 3;
453 return INTEL_PT_BAD_PACKET;
459 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
460 struct intel_pt_pkt *packet)
463 return INTEL_PT_NEED_MORE_BYTES;
464 packet->type = INTEL_PT_TSC;
465 memcpy_le64(&packet->payload, buf + 1, 7);
469 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
470 struct intel_pt_pkt *packet)
473 return INTEL_PT_NEED_MORE_BYTES;
474 packet->type = INTEL_PT_MTC;
475 packet->payload = buf[1];
479 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
480 struct intel_pt_pkt *packet)
484 memset(packet, 0, sizeof(struct intel_pt_pkt));
487 return INTEL_PT_NEED_MORE_BYTES;
490 if (!(byte & BIT(0))) {
492 return intel_pt_get_pad(packet);
494 return intel_pt_get_ext(buf, len, packet);
495 return intel_pt_get_short_tnt(byte, packet);
499 return intel_pt_get_cyc(byte, buf, len, packet);
501 switch (byte & 0x1f) {
503 return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
505 return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
508 return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
511 return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
515 return intel_pt_get_mode(buf, len, packet);
517 return intel_pt_get_tsc(buf, len, packet);
519 return intel_pt_get_mtc(buf, len, packet);
521 return INTEL_PT_BAD_PACKET;
524 return INTEL_PT_BAD_PACKET;
528 int intel_pt_get_packet(const unsigned char *buf, size_t len,
529 struct intel_pt_pkt *packet)
533 ret = intel_pt_do_get_packet(buf, len, packet);
535 while (ret < 8 && len > (size_t)ret && !buf[ret])
541 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
545 unsigned long long payload = packet->payload;
546 const char *name = intel_pt_pkt_name(packet->type);
548 switch (packet->type) {
552 case INTEL_PT_PSBEND:
553 case INTEL_PT_TRACESTOP:
555 return snprintf(buf, buf_len, "%s", name);
557 size_t blen = buf_len;
559 ret = snprintf(buf, blen, "%s ", name);
564 for (i = 0; i < packet->count; i++) {
566 ret = snprintf(buf, blen, "T");
568 ret = snprintf(buf, blen, "N");
575 ret = snprintf(buf, blen, " (%d)", packet->count);
579 return buf_len - blen;
581 case INTEL_PT_TIP_PGD:
582 case INTEL_PT_TIP_PGE:
585 if (!(packet->count))
586 return snprintf(buf, buf_len, "%s no ip", name);
594 return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
596 return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
597 (unsigned)payload, packet->count);
598 case INTEL_PT_MODE_EXEC:
599 return snprintf(buf, buf_len, "%s %lld", name, payload);
600 case INTEL_PT_MODE_TSX:
601 return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
602 name, (unsigned)(payload >> 1) & 1,
603 (unsigned)payload & 1);
605 nr = packet->payload & NR_FLAG ? 1 : 0;
607 ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
610 case INTEL_PT_PTWRITE:
611 return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
612 case INTEL_PT_PTWRITE_IP:
613 return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
614 case INTEL_PT_EXSTOP:
615 return snprintf(buf, buf_len, "%s IP:0", name);
616 case INTEL_PT_EXSTOP_IP:
617 return snprintf(buf, buf_len, "%s IP:1", name);
619 return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
620 name, payload, (unsigned int)(payload & 0xff),
621 (unsigned int)((payload >> 32) & 0x3));
623 return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
624 name, payload, !!(payload & 0x80),
625 (unsigned int)((payload >> 12) & 0xf),
626 (unsigned int)((payload >> 8) & 0xf));
628 return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
630 (unsigned int)((payload >> 4) & 0xf),
631 (unsigned int)(payload & 0xf),
632 (unsigned int)((payload >> 8) & 0xf));
636 return snprintf(buf, buf_len, "%s 0x%llx (%d)",
637 name, payload, packet->count);