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",
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 >> 1;
109 packet->payload |= NR_FLAG;
114 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
116 packet->type = INTEL_PT_TRACESTOP;
120 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
121 struct intel_pt_pkt *packet)
124 return INTEL_PT_NEED_MORE_BYTES;
125 packet->type = INTEL_PT_CBR;
126 packet->payload = buf[2];
130 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
131 struct intel_pt_pkt *packet)
133 unsigned int count = (52 - 5) >> 3;
135 if (count < 1 || count > 7)
136 return INTEL_PT_BAD_PACKET;
139 return INTEL_PT_NEED_MORE_BYTES;
141 packet->type = INTEL_PT_VMCS;
142 packet->count = count;
143 memcpy_le64(&packet->payload, buf + 2, count);
148 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
150 packet->type = INTEL_PT_OVF;
154 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
155 struct intel_pt_pkt *packet)
160 return INTEL_PT_NEED_MORE_BYTES;
162 for (i = 2; i < 16; i += 2) {
163 if (buf[i] != 2 || buf[i + 1] != 0x82)
164 return INTEL_PT_BAD_PACKET;
167 packet->type = INTEL_PT_PSB;
171 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
173 packet->type = INTEL_PT_PSBEND;
177 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
178 struct intel_pt_pkt *packet)
181 return INTEL_PT_NEED_MORE_BYTES;
183 packet->type = INTEL_PT_TMA;
184 packet->payload = buf[2] | (buf[3] << 8);
185 packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
189 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
191 packet->type = INTEL_PT_PAD;
195 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
196 struct intel_pt_pkt *packet)
199 return INTEL_PT_NEED_MORE_BYTES;
200 packet->type = INTEL_PT_MNT;
201 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_ext(const unsigned char *buf, size_t len,
221 struct intel_pt_pkt *packet)
224 return INTEL_PT_NEED_MORE_BYTES;
227 case 0xa3: /* Long TNT */
228 return intel_pt_get_long_tnt(buf, len, packet);
230 return intel_pt_get_pip(buf, len, packet);
231 case 0x83: /* TraceStop */
232 return intel_pt_get_tracestop(packet);
234 return intel_pt_get_cbr(buf, len, packet);
235 case 0xc8: /* VMCS */
236 return intel_pt_get_vmcs(buf, len, packet);
238 return intel_pt_get_ovf(packet);
240 return intel_pt_get_psb(buf, len, packet);
241 case 0x23: /* PSBEND */
242 return intel_pt_get_psbend(packet);
244 return intel_pt_get_tma(buf, len, packet);
245 case 0xC3: /* 3-byte header */
246 return intel_pt_get_3byte(buf, len, packet);
248 return INTEL_PT_BAD_PACKET;
252 static int intel_pt_get_short_tnt(unsigned int byte,
253 struct intel_pt_pkt *packet)
257 for (count = 6; count; count--) {
263 packet->type = INTEL_PT_TNT;
264 packet->count = count;
265 packet->payload = (uint64_t)byte << 57;
270 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
271 size_t len, struct intel_pt_pkt *packet)
273 unsigned int offs = 1, shift;
274 uint64_t payload = byte >> 3;
278 for (shift = 5; byte & 1; shift += 7) {
280 return INTEL_PT_BAD_PACKET;
282 return INTEL_PT_NEED_MORE_BYTES;
284 payload |= (byte >> 1) << shift;
287 packet->type = INTEL_PT_CYC;
288 packet->payload = payload;
292 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
293 const unsigned char *buf, size_t len,
294 struct intel_pt_pkt *packet)
298 packet->count = byte >> 5;
300 switch (packet->count) {
306 return INTEL_PT_NEED_MORE_BYTES;
308 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
312 return INTEL_PT_NEED_MORE_BYTES;
314 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
319 return INTEL_PT_NEED_MORE_BYTES;
321 memcpy_le64(&packet->payload, buf + 1, 6);
325 return INTEL_PT_NEED_MORE_BYTES;
327 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
330 return INTEL_PT_BAD_PACKET;
338 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
339 struct intel_pt_pkt *packet)
342 return INTEL_PT_NEED_MORE_BYTES;
344 switch (buf[1] >> 5) {
346 packet->type = INTEL_PT_MODE_EXEC;
347 switch (buf[1] & 3) {
349 packet->payload = 16;
352 packet->payload = 64;
355 packet->payload = 32;
358 return INTEL_PT_BAD_PACKET;
362 packet->type = INTEL_PT_MODE_TSX;
363 if ((buf[1] & 3) == 3)
364 return INTEL_PT_BAD_PACKET;
365 packet->payload = buf[1] & 3;
368 return INTEL_PT_BAD_PACKET;
374 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
375 struct intel_pt_pkt *packet)
378 return INTEL_PT_NEED_MORE_BYTES;
379 packet->type = INTEL_PT_TSC;
380 memcpy_le64(&packet->payload, buf + 1, 7);
384 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
385 struct intel_pt_pkt *packet)
388 return INTEL_PT_NEED_MORE_BYTES;
389 packet->type = INTEL_PT_MTC;
390 packet->payload = buf[1];
394 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
395 struct intel_pt_pkt *packet)
399 memset(packet, 0, sizeof(struct intel_pt_pkt));
402 return INTEL_PT_NEED_MORE_BYTES;
405 if (!(byte & BIT(0))) {
407 return intel_pt_get_pad(packet);
409 return intel_pt_get_ext(buf, len, packet);
410 return intel_pt_get_short_tnt(byte, packet);
414 return intel_pt_get_cyc(byte, buf, len, packet);
416 switch (byte & 0x1f) {
418 return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
420 return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
423 return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
426 return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
430 return intel_pt_get_mode(buf, len, packet);
432 return intel_pt_get_tsc(buf, len, packet);
434 return intel_pt_get_mtc(buf, len, packet);
436 return INTEL_PT_BAD_PACKET;
439 return INTEL_PT_BAD_PACKET;
443 int intel_pt_get_packet(const unsigned char *buf, size_t len,
444 struct intel_pt_pkt *packet)
448 ret = intel_pt_do_get_packet(buf, len, packet);
450 while (ret < 8 && len > (size_t)ret && !buf[ret])
456 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
460 unsigned long long payload = packet->payload;
461 const char *name = intel_pt_pkt_name(packet->type);
463 switch (packet->type) {
467 case INTEL_PT_PSBEND:
468 case INTEL_PT_TRACESTOP:
470 return snprintf(buf, buf_len, "%s", name);
472 size_t blen = buf_len;
474 ret = snprintf(buf, blen, "%s ", name);
479 for (i = 0; i < packet->count; i++) {
481 ret = snprintf(buf, blen, "T");
483 ret = snprintf(buf, blen, "N");
490 ret = snprintf(buf, blen, " (%d)", packet->count);
494 return buf_len - blen;
496 case INTEL_PT_TIP_PGD:
497 case INTEL_PT_TIP_PGE:
500 if (!(packet->count))
501 return snprintf(buf, buf_len, "%s no ip", name);
509 return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
511 return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
512 (unsigned)payload, packet->count);
513 case INTEL_PT_MODE_EXEC:
514 return snprintf(buf, buf_len, "%s %lld", name, payload);
515 case INTEL_PT_MODE_TSX:
516 return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
517 name, (unsigned)(payload >> 1) & 1,
518 (unsigned)payload & 1);
520 nr = packet->payload & NR_FLAG ? 1 : 0;
522 ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
528 return snprintf(buf, buf_len, "%s 0x%llx (%d)",
529 name, payload, packet->count);