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