1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * intel_pt_pkt_decoder.c: Intel Processor Trace support
4  * Copyright (c) 2013-2014, Intel Corporation.
5  */
6 
7 #include <stdio.h>
8 #include <string.h>
9 #include <endian.h>
10 #include <byteswap.h>
11 #include <linux/compiler.h>
12 
13 #include "intel-pt-pkt-decoder.h"
14 
15 #define BIT(n)		(1 << (n))
16 
17 #define BIT63		((uint64_t)1 << 63)
18 
19 #define NR_FLAG		BIT63
20 
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)); \
28 } while (0)
29 #else
30 #define le16_to_cpu
31 #define le32_to_cpu
32 #define le64_to_cpu
33 #define memcpy_le64 memcpy
34 #endif
35 
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 };
66 
67 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
68 {
69 	return packet_name[type];
70 }
71 
72 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
73 				 struct intel_pt_pkt *packet)
74 {
75 	uint64_t payload;
76 	int count;
77 
78 	if (len < 8)
79 		return INTEL_PT_NEED_MORE_BYTES;
80 
81 	payload = le64_to_cpu(*(uint64_t *)buf);
82 
83 	for (count = 47; count; count--) {
84 		if (payload & BIT63)
85 			break;
86 		payload <<= 1;
87 	}
88 
89 	packet->type = INTEL_PT_TNT;
90 	packet->count = count;
91 	packet->payload = payload << 1;
92 	return 8;
93 }
94 
95 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
96 			    struct intel_pt_pkt *packet)
97 {
98 	uint64_t payload = 0;
99 
100 	if (len < 8)
101 		return INTEL_PT_NEED_MORE_BYTES;
102 
103 	packet->type = INTEL_PT_PIP;
104 	memcpy_le64(&payload, buf + 2, 6);
105 	packet->payload = payload >> 1;
106 	if (payload & 1)
107 		packet->payload |= NR_FLAG;
108 
109 	return 8;
110 }
111 
112 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
113 {
114 	packet->type = INTEL_PT_TRACESTOP;
115 	return 2;
116 }
117 
118 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
119 			    struct intel_pt_pkt *packet)
120 {
121 	if (len < 4)
122 		return INTEL_PT_NEED_MORE_BYTES;
123 	packet->type = INTEL_PT_CBR;
124 	packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
125 	return 4;
126 }
127 
128 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
129 			     struct intel_pt_pkt *packet)
130 {
131 	unsigned int count = (52 - 5) >> 3;
132 
133 	if (count < 1 || count > 7)
134 		return INTEL_PT_BAD_PACKET;
135 
136 	if (len < count + 2)
137 		return INTEL_PT_NEED_MORE_BYTES;
138 
139 	packet->type = INTEL_PT_VMCS;
140 	packet->count = count;
141 	memcpy_le64(&packet->payload, buf + 2, count);
142 
143 	return count + 2;
144 }
145 
146 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
147 {
148 	packet->type = INTEL_PT_OVF;
149 	return 2;
150 }
151 
152 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
153 			    struct intel_pt_pkt *packet)
154 {
155 	int i;
156 
157 	if (len < 16)
158 		return INTEL_PT_NEED_MORE_BYTES;
159 
160 	for (i = 2; i < 16; i += 2) {
161 		if (buf[i] != 2 || buf[i + 1] != 0x82)
162 			return INTEL_PT_BAD_PACKET;
163 	}
164 
165 	packet->type = INTEL_PT_PSB;
166 	return 16;
167 }
168 
169 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
170 {
171 	packet->type = INTEL_PT_PSBEND;
172 	return 2;
173 }
174 
175 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
176 			    struct intel_pt_pkt *packet)
177 {
178 	if (len < 7)
179 		return INTEL_PT_NEED_MORE_BYTES;
180 
181 	packet->type = INTEL_PT_TMA;
182 	packet->payload = buf[2] | (buf[3] << 8);
183 	packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
184 	return 7;
185 }
186 
187 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
188 {
189 	packet->type = INTEL_PT_PAD;
190 	return 1;
191 }
192 
193 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
194 			    struct intel_pt_pkt *packet)
195 {
196 	if (len < 11)
197 		return INTEL_PT_NEED_MORE_BYTES;
198 	packet->type = INTEL_PT_MNT;
199 	memcpy_le64(&packet->payload, buf + 3, 8);
200 	return 11
201 ;
202 }
203 
204 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
205 			      struct intel_pt_pkt *packet)
206 {
207 	if (len < 3)
208 		return INTEL_PT_NEED_MORE_BYTES;
209 
210 	switch (buf[2]) {
211 	case 0x88: /* MNT */
212 		return intel_pt_get_mnt(buf, len, packet);
213 	default:
214 		return INTEL_PT_BAD_PACKET;
215 	}
216 }
217 
218 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
219 				struct intel_pt_pkt *packet)
220 {
221 	packet->count = (buf[1] >> 5) & 0x3;
222 	packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
223 					 INTEL_PT_PTWRITE;
224 
225 	switch (packet->count) {
226 	case 0:
227 		if (len < 6)
228 			return INTEL_PT_NEED_MORE_BYTES;
229 		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
230 		return 6;
231 	case 1:
232 		if (len < 10)
233 			return INTEL_PT_NEED_MORE_BYTES;
234 		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
235 		return 10;
236 	default:
237 		return INTEL_PT_BAD_PACKET;
238 	}
239 }
240 
241 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
242 {
243 	packet->type = INTEL_PT_EXSTOP;
244 	return 2;
245 }
246 
247 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
248 {
249 	packet->type = INTEL_PT_EXSTOP_IP;
250 	return 2;
251 }
252 
253 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
254 			      struct intel_pt_pkt *packet)
255 {
256 	if (len < 10)
257 		return INTEL_PT_NEED_MORE_BYTES;
258 	packet->type = INTEL_PT_MWAIT;
259 	packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
260 	return 10;
261 }
262 
263 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
264 			     struct intel_pt_pkt *packet)
265 {
266 	if (len < 4)
267 		return INTEL_PT_NEED_MORE_BYTES;
268 	packet->type = INTEL_PT_PWRE;
269 	memcpy_le64(&packet->payload, buf + 2, 2);
270 	return 4;
271 }
272 
273 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
274 			     struct intel_pt_pkt *packet)
275 {
276 	if (len < 7)
277 		return INTEL_PT_NEED_MORE_BYTES;
278 	packet->type = INTEL_PT_PWRX;
279 	memcpy_le64(&packet->payload, buf + 2, 5);
280 	return 7;
281 }
282 
283 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
284 			    struct intel_pt_pkt *packet)
285 {
286 	if (len < 2)
287 		return INTEL_PT_NEED_MORE_BYTES;
288 
289 	if ((buf[1] & 0x1f) == 0x12)
290 		return intel_pt_get_ptwrite(buf, len, packet);
291 
292 	switch (buf[1]) {
293 	case 0xa3: /* Long TNT */
294 		return intel_pt_get_long_tnt(buf, len, packet);
295 	case 0x43: /* PIP */
296 		return intel_pt_get_pip(buf, len, packet);
297 	case 0x83: /* TraceStop */
298 		return intel_pt_get_tracestop(packet);
299 	case 0x03: /* CBR */
300 		return intel_pt_get_cbr(buf, len, packet);
301 	case 0xc8: /* VMCS */
302 		return intel_pt_get_vmcs(buf, len, packet);
303 	case 0xf3: /* OVF */
304 		return intel_pt_get_ovf(packet);
305 	case 0x82: /* PSB */
306 		return intel_pt_get_psb(buf, len, packet);
307 	case 0x23: /* PSBEND */
308 		return intel_pt_get_psbend(packet);
309 	case 0x73: /* TMA */
310 		return intel_pt_get_tma(buf, len, packet);
311 	case 0xC3: /* 3-byte header */
312 		return intel_pt_get_3byte(buf, len, packet);
313 	case 0x62: /* EXSTOP no IP */
314 		return intel_pt_get_exstop(packet);
315 	case 0xE2: /* EXSTOP with IP */
316 		return intel_pt_get_exstop_ip(packet);
317 	case 0xC2: /* MWAIT */
318 		return intel_pt_get_mwait(buf, len, packet);
319 	case 0x22: /* PWRE */
320 		return intel_pt_get_pwre(buf, len, packet);
321 	case 0xA2: /* PWRX */
322 		return intel_pt_get_pwrx(buf, len, packet);
323 	default:
324 		return INTEL_PT_BAD_PACKET;
325 	}
326 }
327 
328 static int intel_pt_get_short_tnt(unsigned int byte,
329 				  struct intel_pt_pkt *packet)
330 {
331 	int count;
332 
333 	for (count = 6; count; count--) {
334 		if (byte & BIT(7))
335 			break;
336 		byte <<= 1;
337 	}
338 
339 	packet->type = INTEL_PT_TNT;
340 	packet->count = count;
341 	packet->payload = (uint64_t)byte << 57;
342 
343 	return 1;
344 }
345 
346 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
347 			    size_t len, struct intel_pt_pkt *packet)
348 {
349 	unsigned int offs = 1, shift;
350 	uint64_t payload = byte >> 3;
351 
352 	byte >>= 2;
353 	len -= 1;
354 	for (shift = 5; byte & 1; shift += 7) {
355 		if (offs > 9)
356 			return INTEL_PT_BAD_PACKET;
357 		if (len < offs)
358 			return INTEL_PT_NEED_MORE_BYTES;
359 		byte = buf[offs++];
360 		payload |= ((uint64_t)byte >> 1) << shift;
361 	}
362 
363 	packet->type = INTEL_PT_CYC;
364 	packet->payload = payload;
365 	return offs;
366 }
367 
368 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
369 			   const unsigned char *buf, size_t len,
370 			   struct intel_pt_pkt *packet)
371 {
372 	int ip_len;
373 
374 	packet->count = byte >> 5;
375 
376 	switch (packet->count) {
377 	case 0:
378 		ip_len = 0;
379 		break;
380 	case 1:
381 		if (len < 3)
382 			return INTEL_PT_NEED_MORE_BYTES;
383 		ip_len = 2;
384 		packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
385 		break;
386 	case 2:
387 		if (len < 5)
388 			return INTEL_PT_NEED_MORE_BYTES;
389 		ip_len = 4;
390 		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
391 		break;
392 	case 3:
393 	case 4:
394 		if (len < 7)
395 			return INTEL_PT_NEED_MORE_BYTES;
396 		ip_len = 6;
397 		memcpy_le64(&packet->payload, buf + 1, 6);
398 		break;
399 	case 6:
400 		if (len < 9)
401 			return INTEL_PT_NEED_MORE_BYTES;
402 		ip_len = 8;
403 		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
404 		break;
405 	default:
406 		return INTEL_PT_BAD_PACKET;
407 	}
408 
409 	packet->type = type;
410 
411 	return ip_len + 1;
412 }
413 
414 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
415 			     struct intel_pt_pkt *packet)
416 {
417 	if (len < 2)
418 		return INTEL_PT_NEED_MORE_BYTES;
419 
420 	switch (buf[1] >> 5) {
421 	case 0:
422 		packet->type = INTEL_PT_MODE_EXEC;
423 		switch (buf[1] & 3) {
424 		case 0:
425 			packet->payload = 16;
426 			break;
427 		case 1:
428 			packet->payload = 64;
429 			break;
430 		case 2:
431 			packet->payload = 32;
432 			break;
433 		default:
434 			return INTEL_PT_BAD_PACKET;
435 		}
436 		break;
437 	case 1:
438 		packet->type = INTEL_PT_MODE_TSX;
439 		if ((buf[1] & 3) == 3)
440 			return INTEL_PT_BAD_PACKET;
441 		packet->payload = buf[1] & 3;
442 		break;
443 	default:
444 		return INTEL_PT_BAD_PACKET;
445 	}
446 
447 	return 2;
448 }
449 
450 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
451 			    struct intel_pt_pkt *packet)
452 {
453 	if (len < 8)
454 		return INTEL_PT_NEED_MORE_BYTES;
455 	packet->type = INTEL_PT_TSC;
456 	memcpy_le64(&packet->payload, buf + 1, 7);
457 	return 8;
458 }
459 
460 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
461 			    struct intel_pt_pkt *packet)
462 {
463 	if (len < 2)
464 		return INTEL_PT_NEED_MORE_BYTES;
465 	packet->type = INTEL_PT_MTC;
466 	packet->payload = buf[1];
467 	return 2;
468 }
469 
470 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
471 				  struct intel_pt_pkt *packet)
472 {
473 	unsigned int byte;
474 
475 	memset(packet, 0, sizeof(struct intel_pt_pkt));
476 
477 	if (!len)
478 		return INTEL_PT_NEED_MORE_BYTES;
479 
480 	byte = buf[0];
481 	if (!(byte & BIT(0))) {
482 		if (byte == 0)
483 			return intel_pt_get_pad(packet);
484 		if (byte == 2)
485 			return intel_pt_get_ext(buf, len, packet);
486 		return intel_pt_get_short_tnt(byte, packet);
487 	}
488 
489 	if ((byte & 2))
490 		return intel_pt_get_cyc(byte, buf, len, packet);
491 
492 	switch (byte & 0x1f) {
493 	case 0x0D:
494 		return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
495 	case 0x11:
496 		return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
497 				       packet);
498 	case 0x01:
499 		return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
500 				       packet);
501 	case 0x1D:
502 		return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
503 	case 0x19:
504 		switch (byte) {
505 		case 0x99:
506 			return intel_pt_get_mode(buf, len, packet);
507 		case 0x19:
508 			return intel_pt_get_tsc(buf, len, packet);
509 		case 0x59:
510 			return intel_pt_get_mtc(buf, len, packet);
511 		default:
512 			return INTEL_PT_BAD_PACKET;
513 		}
514 	default:
515 		return INTEL_PT_BAD_PACKET;
516 	}
517 }
518 
519 int intel_pt_get_packet(const unsigned char *buf, size_t len,
520 			struct intel_pt_pkt *packet)
521 {
522 	int ret;
523 
524 	ret = intel_pt_do_get_packet(buf, len, packet);
525 	if (ret > 0) {
526 		while (ret < 8 && len > (size_t)ret && !buf[ret])
527 			ret += 1;
528 	}
529 	return ret;
530 }
531 
532 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
533 		      size_t buf_len)
534 {
535 	int ret, i, nr;
536 	unsigned long long payload = packet->payload;
537 	const char *name = intel_pt_pkt_name(packet->type);
538 
539 	switch (packet->type) {
540 	case INTEL_PT_BAD:
541 	case INTEL_PT_PAD:
542 	case INTEL_PT_PSB:
543 	case INTEL_PT_PSBEND:
544 	case INTEL_PT_TRACESTOP:
545 	case INTEL_PT_OVF:
546 		return snprintf(buf, buf_len, "%s", name);
547 	case INTEL_PT_TNT: {
548 		size_t blen = buf_len;
549 
550 		ret = snprintf(buf, blen, "%s ", name);
551 		if (ret < 0)
552 			return ret;
553 		buf += ret;
554 		blen -= ret;
555 		for (i = 0; i < packet->count; i++) {
556 			if (payload & BIT63)
557 				ret = snprintf(buf, blen, "T");
558 			else
559 				ret = snprintf(buf, blen, "N");
560 			if (ret < 0)
561 				return ret;
562 			buf += ret;
563 			blen -= ret;
564 			payload <<= 1;
565 		}
566 		ret = snprintf(buf, blen, " (%d)", packet->count);
567 		if (ret < 0)
568 			return ret;
569 		blen -= ret;
570 		return buf_len - blen;
571 	}
572 	case INTEL_PT_TIP_PGD:
573 	case INTEL_PT_TIP_PGE:
574 	case INTEL_PT_TIP:
575 	case INTEL_PT_FUP:
576 		if (!(packet->count))
577 			return snprintf(buf, buf_len, "%s no ip", name);
578 		__fallthrough;
579 	case INTEL_PT_CYC:
580 	case INTEL_PT_VMCS:
581 	case INTEL_PT_MTC:
582 	case INTEL_PT_MNT:
583 	case INTEL_PT_CBR:
584 	case INTEL_PT_TSC:
585 		return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
586 	case INTEL_PT_TMA:
587 		return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
588 				(unsigned)payload, packet->count);
589 	case INTEL_PT_MODE_EXEC:
590 		return snprintf(buf, buf_len, "%s %lld", name, payload);
591 	case INTEL_PT_MODE_TSX:
592 		return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
593 				name, (unsigned)(payload >> 1) & 1,
594 				(unsigned)payload & 1);
595 	case INTEL_PT_PIP:
596 		nr = packet->payload & NR_FLAG ? 1 : 0;
597 		payload &= ~NR_FLAG;
598 		ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
599 			       name, payload, nr);
600 		return ret;
601 	case INTEL_PT_PTWRITE:
602 		return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
603 	case INTEL_PT_PTWRITE_IP:
604 		return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
605 	case INTEL_PT_EXSTOP:
606 		return snprintf(buf, buf_len, "%s IP:0", name);
607 	case INTEL_PT_EXSTOP_IP:
608 		return snprintf(buf, buf_len, "%s IP:1", name);
609 	case INTEL_PT_MWAIT:
610 		return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
611 				name, payload, (unsigned int)(payload & 0xff),
612 				(unsigned int)((payload >> 32) & 0x3));
613 	case INTEL_PT_PWRE:
614 		return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
615 				name, payload, !!(payload & 0x80),
616 				(unsigned int)((payload >> 12) & 0xf),
617 				(unsigned int)((payload >> 8) & 0xf));
618 	case INTEL_PT_PWRX:
619 		return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
620 				name, payload,
621 				(unsigned int)((payload >> 4) & 0xf),
622 				(unsigned int)(payload & 0xf),
623 				(unsigned int)((payload >> 8) & 0xf));
624 	default:
625 		break;
626 	}
627 	return snprintf(buf, buf_len, "%s 0x%llx (%d)",
628 			name, payload, packet->count);
629 }
630