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 #if __BYTE_ORDER__ == __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)); \
26 } while (0)
27 #else
28 #define le16_to_cpu
29 #define le32_to_cpu
30 #define le64_to_cpu
31 #define memcpy_le64 memcpy
32 #endif
33 
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",
67 	[INTEL_PT_CFE]		= "CFE",
68 	[INTEL_PT_CFE_IP]	= "CFE",
69 	[INTEL_PT_EVD]		= "EVD",
70 };
71 
72 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
73 {
74 	return packet_name[type];
75 }
76 
77 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
78 				 struct intel_pt_pkt *packet)
79 {
80 	uint64_t payload;
81 	int count;
82 
83 	if (len < 8)
84 		return INTEL_PT_NEED_MORE_BYTES;
85 
86 	payload = le64_to_cpu(*(uint64_t *)buf);
87 
88 	for (count = 47; count; count--) {
89 		if (payload & BIT63)
90 			break;
91 		payload <<= 1;
92 	}
93 
94 	packet->type = INTEL_PT_TNT;
95 	packet->count = count;
96 	packet->payload = payload << 1;
97 	return 8;
98 }
99 
100 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
101 			    struct intel_pt_pkt *packet)
102 {
103 	uint64_t payload = 0;
104 
105 	if (len < 8)
106 		return INTEL_PT_NEED_MORE_BYTES;
107 
108 	packet->type = INTEL_PT_PIP;
109 	memcpy_le64(&payload, buf + 2, 6);
110 	packet->payload = payload;
111 
112 	return 8;
113 }
114 
115 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
116 {
117 	packet->type = INTEL_PT_TRACESTOP;
118 	return 2;
119 }
120 
121 static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
122 			    struct intel_pt_pkt *packet)
123 {
124 	if (len < 4)
125 		return INTEL_PT_NEED_MORE_BYTES;
126 	packet->type = INTEL_PT_CBR;
127 	packet->payload = le16_to_cpu(*(uint16_t *)(buf + 2));
128 	return 4;
129 }
130 
131 static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
132 			     struct intel_pt_pkt *packet)
133 {
134 	unsigned int count = (52 - 5) >> 3;
135 
136 	if (count < 1 || count > 7)
137 		return INTEL_PT_BAD_PACKET;
138 
139 	if (len < count + 2)
140 		return INTEL_PT_NEED_MORE_BYTES;
141 
142 	packet->type = INTEL_PT_VMCS;
143 	packet->count = count;
144 	memcpy_le64(&packet->payload, buf + 2, count);
145 
146 	return count + 2;
147 }
148 
149 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
150 {
151 	packet->type = INTEL_PT_OVF;
152 	return 2;
153 }
154 
155 static int intel_pt_get_psb(const unsigned char *buf, size_t len,
156 			    struct intel_pt_pkt *packet)
157 {
158 	int i;
159 
160 	if (len < 16)
161 		return INTEL_PT_NEED_MORE_BYTES;
162 
163 	for (i = 2; i < 16; i += 2) {
164 		if (buf[i] != 2 || buf[i + 1] != 0x82)
165 			return INTEL_PT_BAD_PACKET;
166 	}
167 
168 	packet->type = INTEL_PT_PSB;
169 	return 16;
170 }
171 
172 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
173 {
174 	packet->type = INTEL_PT_PSBEND;
175 	return 2;
176 }
177 
178 static int intel_pt_get_tma(const unsigned char *buf, size_t len,
179 			    struct intel_pt_pkt *packet)
180 {
181 	if (len < 7)
182 		return INTEL_PT_NEED_MORE_BYTES;
183 
184 	packet->type = INTEL_PT_TMA;
185 	packet->payload = buf[2] | (buf[3] << 8);
186 	packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
187 	return 7;
188 }
189 
190 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
191 {
192 	packet->type = INTEL_PT_PAD;
193 	return 1;
194 }
195 
196 static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
197 			    struct intel_pt_pkt *packet)
198 {
199 	if (len < 11)
200 		return INTEL_PT_NEED_MORE_BYTES;
201 	packet->type = INTEL_PT_MNT;
202 	memcpy_le64(&packet->payload, buf + 3, 8);
203 	return 11;
204 }
205 
206 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
207 			      struct intel_pt_pkt *packet)
208 {
209 	if (len < 3)
210 		return INTEL_PT_NEED_MORE_BYTES;
211 
212 	switch (buf[2]) {
213 	case 0x88: /* MNT */
214 		return intel_pt_get_mnt(buf, len, packet);
215 	default:
216 		return INTEL_PT_BAD_PACKET;
217 	}
218 }
219 
220 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
221 				struct intel_pt_pkt *packet)
222 {
223 	packet->count = (buf[1] >> 5) & 0x3;
224 	packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
225 					 INTEL_PT_PTWRITE;
226 
227 	switch (packet->count) {
228 	case 0:
229 		if (len < 6)
230 			return INTEL_PT_NEED_MORE_BYTES;
231 		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
232 		return 6;
233 	case 1:
234 		if (len < 10)
235 			return INTEL_PT_NEED_MORE_BYTES;
236 		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
237 		return 10;
238 	default:
239 		return INTEL_PT_BAD_PACKET;
240 	}
241 }
242 
243 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
244 {
245 	packet->type = INTEL_PT_EXSTOP;
246 	return 2;
247 }
248 
249 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
250 {
251 	packet->type = INTEL_PT_EXSTOP_IP;
252 	return 2;
253 }
254 
255 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
256 			      struct intel_pt_pkt *packet)
257 {
258 	if (len < 10)
259 		return INTEL_PT_NEED_MORE_BYTES;
260 	packet->type = INTEL_PT_MWAIT;
261 	packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
262 	return 10;
263 }
264 
265 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
266 			     struct intel_pt_pkt *packet)
267 {
268 	if (len < 4)
269 		return INTEL_PT_NEED_MORE_BYTES;
270 	packet->type = INTEL_PT_PWRE;
271 	memcpy_le64(&packet->payload, buf + 2, 2);
272 	return 4;
273 }
274 
275 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
276 			     struct intel_pt_pkt *packet)
277 {
278 	if (len < 7)
279 		return INTEL_PT_NEED_MORE_BYTES;
280 	packet->type = INTEL_PT_PWRX;
281 	memcpy_le64(&packet->payload, buf + 2, 5);
282 	return 7;
283 }
284 
285 static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
286 			    struct intel_pt_pkt *packet)
287 {
288 	if (len < 3)
289 		return INTEL_PT_NEED_MORE_BYTES;
290 	packet->type = INTEL_PT_BBP;
291 	packet->count = buf[2] >> 7;
292 	packet->payload = buf[2] & 0x1f;
293 	return 3;
294 }
295 
296 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
297 			      struct intel_pt_pkt *packet)
298 {
299 	if (len < 5)
300 		return INTEL_PT_NEED_MORE_BYTES;
301 	packet->type = INTEL_PT_BIP;
302 	packet->count = buf[0] >> 3;
303 	memcpy_le64(&packet->payload, buf + 1, 4);
304 	return 5;
305 }
306 
307 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
308 			      struct intel_pt_pkt *packet)
309 {
310 	if (len < 9)
311 		return INTEL_PT_NEED_MORE_BYTES;
312 	packet->type = INTEL_PT_BIP;
313 	packet->count = buf[0] >> 3;
314 	memcpy_le64(&packet->payload, buf + 1, 8);
315 	return 9;
316 }
317 
318 static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
319 {
320 	if (len < 2)
321 		return INTEL_PT_NEED_MORE_BYTES;
322 	packet->type = INTEL_PT_BEP;
323 	return 2;
324 }
325 
326 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
327 {
328 	if (len < 2)
329 		return INTEL_PT_NEED_MORE_BYTES;
330 	packet->type = INTEL_PT_BEP_IP;
331 	return 2;
332 }
333 
334 static int intel_pt_get_cfe(const unsigned char *buf, size_t len,
335 			    struct intel_pt_pkt *packet)
336 {
337 	if (len < 4)
338 		return INTEL_PT_NEED_MORE_BYTES;
339 	packet->type = buf[2] & 0x80 ? INTEL_PT_CFE_IP : INTEL_PT_CFE;
340 	packet->count = buf[2] & 0x1f;
341 	packet->payload = buf[3];
342 	return 4;
343 }
344 
345 static int intel_pt_get_evd(const unsigned char *buf, size_t len,
346 			    struct intel_pt_pkt *packet)
347 {
348 	if (len < 11)
349 		return INTEL_PT_NEED_MORE_BYTES;
350 	packet->type = INTEL_PT_EVD;
351 	packet->count = buf[2] & 0x3f;
352 	packet->payload = buf[3];
353 	memcpy_le64(&packet->payload, buf + 3, 8);
354 	return 11;
355 }
356 
357 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
358 			    struct intel_pt_pkt *packet)
359 {
360 	if (len < 2)
361 		return INTEL_PT_NEED_MORE_BYTES;
362 
363 	if ((buf[1] & 0x1f) == 0x12)
364 		return intel_pt_get_ptwrite(buf, len, packet);
365 
366 	switch (buf[1]) {
367 	case 0xa3: /* Long TNT */
368 		return intel_pt_get_long_tnt(buf, len, packet);
369 	case 0x43: /* PIP */
370 		return intel_pt_get_pip(buf, len, packet);
371 	case 0x83: /* TraceStop */
372 		return intel_pt_get_tracestop(packet);
373 	case 0x03: /* CBR */
374 		return intel_pt_get_cbr(buf, len, packet);
375 	case 0xc8: /* VMCS */
376 		return intel_pt_get_vmcs(buf, len, packet);
377 	case 0xf3: /* OVF */
378 		return intel_pt_get_ovf(packet);
379 	case 0x82: /* PSB */
380 		return intel_pt_get_psb(buf, len, packet);
381 	case 0x23: /* PSBEND */
382 		return intel_pt_get_psbend(packet);
383 	case 0x73: /* TMA */
384 		return intel_pt_get_tma(buf, len, packet);
385 	case 0xC3: /* 3-byte header */
386 		return intel_pt_get_3byte(buf, len, packet);
387 	case 0x62: /* EXSTOP no IP */
388 		return intel_pt_get_exstop(packet);
389 	case 0xE2: /* EXSTOP with IP */
390 		return intel_pt_get_exstop_ip(packet);
391 	case 0xC2: /* MWAIT */
392 		return intel_pt_get_mwait(buf, len, packet);
393 	case 0x22: /* PWRE */
394 		return intel_pt_get_pwre(buf, len, packet);
395 	case 0xA2: /* PWRX */
396 		return intel_pt_get_pwrx(buf, len, packet);
397 	case 0x63: /* BBP */
398 		return intel_pt_get_bbp(buf, len, packet);
399 	case 0x33: /* BEP no IP */
400 		return intel_pt_get_bep(len, packet);
401 	case 0xb3: /* BEP with IP */
402 		return intel_pt_get_bep_ip(len, packet);
403 	case 0x13: /* CFE */
404 		return intel_pt_get_cfe(buf, len, packet);
405 	case 0x53: /* EVD */
406 		return intel_pt_get_evd(buf, len, packet);
407 	default:
408 		return INTEL_PT_BAD_PACKET;
409 	}
410 }
411 
412 static int intel_pt_get_short_tnt(unsigned int byte,
413 				  struct intel_pt_pkt *packet)
414 {
415 	int count;
416 
417 	for (count = 6; count; count--) {
418 		if (byte & BIT(7))
419 			break;
420 		byte <<= 1;
421 	}
422 
423 	packet->type = INTEL_PT_TNT;
424 	packet->count = count;
425 	packet->payload = (uint64_t)byte << 57;
426 
427 	return 1;
428 }
429 
430 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
431 			    size_t len, struct intel_pt_pkt *packet)
432 {
433 	unsigned int offs = 1, shift;
434 	uint64_t payload = byte >> 3;
435 
436 	byte >>= 2;
437 	len -= 1;
438 	for (shift = 5; byte & 1; shift += 7) {
439 		if (offs > 9)
440 			return INTEL_PT_BAD_PACKET;
441 		if (len < offs)
442 			return INTEL_PT_NEED_MORE_BYTES;
443 		byte = buf[offs++];
444 		payload |= ((uint64_t)byte >> 1) << shift;
445 	}
446 
447 	packet->type = INTEL_PT_CYC;
448 	packet->payload = payload;
449 	return offs;
450 }
451 
452 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
453 			   const unsigned char *buf, size_t len,
454 			   struct intel_pt_pkt *packet)
455 {
456 	int ip_len;
457 
458 	packet->count = byte >> 5;
459 
460 	switch (packet->count) {
461 	case 0:
462 		ip_len = 0;
463 		break;
464 	case 1:
465 		if (len < 3)
466 			return INTEL_PT_NEED_MORE_BYTES;
467 		ip_len = 2;
468 		packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
469 		break;
470 	case 2:
471 		if (len < 5)
472 			return INTEL_PT_NEED_MORE_BYTES;
473 		ip_len = 4;
474 		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
475 		break;
476 	case 3:
477 	case 4:
478 		if (len < 7)
479 			return INTEL_PT_NEED_MORE_BYTES;
480 		ip_len = 6;
481 		memcpy_le64(&packet->payload, buf + 1, 6);
482 		break;
483 	case 6:
484 		if (len < 9)
485 			return INTEL_PT_NEED_MORE_BYTES;
486 		ip_len = 8;
487 		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
488 		break;
489 	default:
490 		return INTEL_PT_BAD_PACKET;
491 	}
492 
493 	packet->type = type;
494 
495 	return ip_len + 1;
496 }
497 
498 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
499 			     struct intel_pt_pkt *packet)
500 {
501 	if (len < 2)
502 		return INTEL_PT_NEED_MORE_BYTES;
503 
504 	switch (buf[1] >> 5) {
505 	case 0:
506 		packet->type = INTEL_PT_MODE_EXEC;
507 		packet->count = buf[1];
508 		switch (buf[1] & 3) {
509 		case 0:
510 			packet->payload = 16;
511 			break;
512 		case 1:
513 			packet->payload = 64;
514 			break;
515 		case 2:
516 			packet->payload = 32;
517 			break;
518 		default:
519 			return INTEL_PT_BAD_PACKET;
520 		}
521 		break;
522 	case 1:
523 		packet->type = INTEL_PT_MODE_TSX;
524 		if ((buf[1] & 3) == 3)
525 			return INTEL_PT_BAD_PACKET;
526 		packet->payload = buf[1] & 3;
527 		break;
528 	default:
529 		return INTEL_PT_BAD_PACKET;
530 	}
531 
532 	return 2;
533 }
534 
535 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
536 			    struct intel_pt_pkt *packet)
537 {
538 	if (len < 8)
539 		return INTEL_PT_NEED_MORE_BYTES;
540 	packet->type = INTEL_PT_TSC;
541 	memcpy_le64(&packet->payload, buf + 1, 7);
542 	return 8;
543 }
544 
545 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
546 			    struct intel_pt_pkt *packet)
547 {
548 	if (len < 2)
549 		return INTEL_PT_NEED_MORE_BYTES;
550 	packet->type = INTEL_PT_MTC;
551 	packet->payload = buf[1];
552 	return 2;
553 }
554 
555 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
556 				  struct intel_pt_pkt *packet,
557 				  enum intel_pt_pkt_ctx ctx)
558 {
559 	unsigned int byte;
560 
561 	memset(packet, 0, sizeof(struct intel_pt_pkt));
562 
563 	if (!len)
564 		return INTEL_PT_NEED_MORE_BYTES;
565 
566 	byte = buf[0];
567 
568 	switch (ctx) {
569 	case INTEL_PT_NO_CTX:
570 		break;
571 	case INTEL_PT_BLK_4_CTX:
572 		if ((byte & 0x7) == 4)
573 			return intel_pt_get_bip_4(buf, len, packet);
574 		break;
575 	case INTEL_PT_BLK_8_CTX:
576 		if ((byte & 0x7) == 4)
577 			return intel_pt_get_bip_8(buf, len, packet);
578 		break;
579 	default:
580 		break;
581 	}
582 
583 	if (!(byte & BIT(0))) {
584 		if (byte == 0)
585 			return intel_pt_get_pad(packet);
586 		if (byte == 2)
587 			return intel_pt_get_ext(buf, len, packet);
588 		return intel_pt_get_short_tnt(byte, packet);
589 	}
590 
591 	if ((byte & 2))
592 		return intel_pt_get_cyc(byte, buf, len, packet);
593 
594 	switch (byte & 0x1f) {
595 	case 0x0D:
596 		return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
597 	case 0x11:
598 		return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
599 				       packet);
600 	case 0x01:
601 		return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
602 				       packet);
603 	case 0x1D:
604 		return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
605 	case 0x19:
606 		switch (byte) {
607 		case 0x99:
608 			return intel_pt_get_mode(buf, len, packet);
609 		case 0x19:
610 			return intel_pt_get_tsc(buf, len, packet);
611 		case 0x59:
612 			return intel_pt_get_mtc(buf, len, packet);
613 		default:
614 			return INTEL_PT_BAD_PACKET;
615 		}
616 	default:
617 		return INTEL_PT_BAD_PACKET;
618 	}
619 }
620 
621 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
622 			  enum intel_pt_pkt_ctx *ctx)
623 {
624 	switch (packet->type) {
625 	case INTEL_PT_BAD:
626 	case INTEL_PT_PAD:
627 	case INTEL_PT_TSC:
628 	case INTEL_PT_TMA:
629 	case INTEL_PT_MTC:
630 	case INTEL_PT_FUP:
631 	case INTEL_PT_CYC:
632 	case INTEL_PT_CBR:
633 	case INTEL_PT_MNT:
634 	case INTEL_PT_EXSTOP:
635 	case INTEL_PT_EXSTOP_IP:
636 	case INTEL_PT_PWRE:
637 	case INTEL_PT_PWRX:
638 	case INTEL_PT_BIP:
639 		break;
640 	case INTEL_PT_TNT:
641 	case INTEL_PT_TIP:
642 	case INTEL_PT_TIP_PGD:
643 	case INTEL_PT_TIP_PGE:
644 	case INTEL_PT_MODE_EXEC:
645 	case INTEL_PT_MODE_TSX:
646 	case INTEL_PT_PIP:
647 	case INTEL_PT_OVF:
648 	case INTEL_PT_VMCS:
649 	case INTEL_PT_TRACESTOP:
650 	case INTEL_PT_PSB:
651 	case INTEL_PT_PSBEND:
652 	case INTEL_PT_PTWRITE:
653 	case INTEL_PT_PTWRITE_IP:
654 	case INTEL_PT_MWAIT:
655 	case INTEL_PT_BEP:
656 	case INTEL_PT_BEP_IP:
657 	case INTEL_PT_CFE:
658 	case INTEL_PT_CFE_IP:
659 	case INTEL_PT_EVD:
660 		*ctx = INTEL_PT_NO_CTX;
661 		break;
662 	case INTEL_PT_BBP:
663 		if (packet->count)
664 			*ctx = INTEL_PT_BLK_4_CTX;
665 		else
666 			*ctx = INTEL_PT_BLK_8_CTX;
667 		break;
668 	default:
669 		break;
670 	}
671 }
672 
673 int intel_pt_get_packet(const unsigned char *buf, size_t len,
674 			struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
675 {
676 	int ret;
677 
678 	ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
679 	if (ret > 0) {
680 		while (ret < 8 && len > (size_t)ret && !buf[ret])
681 			ret += 1;
682 		intel_pt_upd_pkt_ctx(packet, ctx);
683 	}
684 	return ret;
685 }
686 
687 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
688 		      size_t buf_len)
689 {
690 	int ret, i, nr;
691 	unsigned long long payload = packet->payload;
692 	const char *name = intel_pt_pkt_name(packet->type);
693 
694 	switch (packet->type) {
695 	case INTEL_PT_BAD:
696 	case INTEL_PT_PAD:
697 	case INTEL_PT_PSB:
698 	case INTEL_PT_PSBEND:
699 	case INTEL_PT_TRACESTOP:
700 	case INTEL_PT_OVF:
701 		return snprintf(buf, buf_len, "%s", name);
702 	case INTEL_PT_TNT: {
703 		size_t blen = buf_len;
704 
705 		ret = snprintf(buf, blen, "%s ", name);
706 		if (ret < 0)
707 			return ret;
708 		buf += ret;
709 		blen -= ret;
710 		for (i = 0; i < packet->count; i++) {
711 			if (payload & BIT63)
712 				ret = snprintf(buf, blen, "T");
713 			else
714 				ret = snprintf(buf, blen, "N");
715 			if (ret < 0)
716 				return ret;
717 			buf += ret;
718 			blen -= ret;
719 			payload <<= 1;
720 		}
721 		ret = snprintf(buf, blen, " (%d)", packet->count);
722 		if (ret < 0)
723 			return ret;
724 		blen -= ret;
725 		return buf_len - blen;
726 	}
727 	case INTEL_PT_TIP_PGD:
728 	case INTEL_PT_TIP_PGE:
729 	case INTEL_PT_TIP:
730 	case INTEL_PT_FUP:
731 		if (!(packet->count))
732 			return snprintf(buf, buf_len, "%s no ip", name);
733 		__fallthrough;
734 	case INTEL_PT_CYC:
735 	case INTEL_PT_VMCS:
736 	case INTEL_PT_MTC:
737 	case INTEL_PT_MNT:
738 	case INTEL_PT_CBR:
739 	case INTEL_PT_TSC:
740 		return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
741 	case INTEL_PT_TMA:
742 		return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
743 				(unsigned)payload, packet->count);
744 	case INTEL_PT_MODE_EXEC:
745 		return snprintf(buf, buf_len, "%s IF:%d %lld",
746 				name, !!(packet->count & 4), payload);
747 	case INTEL_PT_MODE_TSX:
748 		return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
749 				name, (unsigned)(payload >> 1) & 1,
750 				(unsigned)payload & 1);
751 	case INTEL_PT_PIP:
752 		nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
753 		payload &= ~INTEL_PT_VMX_NR_FLAG;
754 		ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
755 			       name, payload >> 1, nr);
756 		return ret;
757 	case INTEL_PT_PTWRITE:
758 		return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
759 	case INTEL_PT_PTWRITE_IP:
760 		return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
761 	case INTEL_PT_BEP:
762 	case INTEL_PT_EXSTOP:
763 		return snprintf(buf, buf_len, "%s IP:0", name);
764 	case INTEL_PT_BEP_IP:
765 	case INTEL_PT_EXSTOP_IP:
766 		return snprintf(buf, buf_len, "%s IP:1", name);
767 	case INTEL_PT_MWAIT:
768 		return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
769 				name, payload, (unsigned int)(payload & 0xff),
770 				(unsigned int)((payload >> 32) & 0x3));
771 	case INTEL_PT_PWRE:
772 		return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
773 				name, payload, !!(payload & 0x80),
774 				(unsigned int)((payload >> 12) & 0xf),
775 				(unsigned int)((payload >> 8) & 0xf));
776 	case INTEL_PT_PWRX:
777 		return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
778 				name, payload,
779 				(unsigned int)((payload >> 4) & 0xf),
780 				(unsigned int)(payload & 0xf),
781 				(unsigned int)((payload >> 8) & 0xf));
782 	case INTEL_PT_BBP:
783 		return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
784 				name, packet->count ? "4" : "8", payload);
785 	case INTEL_PT_BIP:
786 		return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
787 				name, packet->count, payload);
788 	case INTEL_PT_CFE:
789 	case INTEL_PT_CFE_IP:
790 		return snprintf(buf, buf_len, "%s IP:%d Type 0x%02x Vector 0x%llx",
791 				name, packet->type == INTEL_PT_CFE_IP, packet->count, payload);
792 	case INTEL_PT_EVD:
793 		return snprintf(buf, buf_len, "%s Type 0x%02x Payload 0x%llx",
794 				name, packet->count, payload);
795 	default:
796 		break;
797 	}
798 	return snprintf(buf, buf_len, "%s 0x%llx (%d)",
799 			name, payload, packet->count);
800 }
801