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 == __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 };
68 
69 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
70 {
71 	return packet_name[type];
72 }
73 
74 static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
75 				 struct intel_pt_pkt *packet)
76 {
77 	uint64_t payload;
78 	int count;
79 
80 	if (len < 8)
81 		return INTEL_PT_NEED_MORE_BYTES;
82 
83 	payload = le64_to_cpu(*(uint64_t *)buf);
84 
85 	for (count = 47; count; count--) {
86 		if (payload & BIT63)
87 			break;
88 		payload <<= 1;
89 	}
90 
91 	packet->type = INTEL_PT_TNT;
92 	packet->count = count;
93 	packet->payload = payload << 1;
94 	return 8;
95 }
96 
97 static int intel_pt_get_pip(const unsigned char *buf, size_t len,
98 			    struct intel_pt_pkt *packet)
99 {
100 	uint64_t payload = 0;
101 
102 	if (len < 8)
103 		return INTEL_PT_NEED_MORE_BYTES;
104 
105 	packet->type = INTEL_PT_PIP;
106 	memcpy_le64(&payload, buf + 2, 6);
107 	packet->payload = payload;
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_bbp(const unsigned char *buf, size_t len,
284 			    struct intel_pt_pkt *packet)
285 {
286 	if (len < 3)
287 		return INTEL_PT_NEED_MORE_BYTES;
288 	packet->type = INTEL_PT_BBP;
289 	packet->count = buf[2] >> 7;
290 	packet->payload = buf[2] & 0x1f;
291 	return 3;
292 }
293 
294 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
295 			      struct intel_pt_pkt *packet)
296 {
297 	if (len < 5)
298 		return INTEL_PT_NEED_MORE_BYTES;
299 	packet->type = INTEL_PT_BIP;
300 	packet->count = buf[0] >> 3;
301 	memcpy_le64(&packet->payload, buf + 1, 4);
302 	return 5;
303 }
304 
305 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
306 			      struct intel_pt_pkt *packet)
307 {
308 	if (len < 9)
309 		return INTEL_PT_NEED_MORE_BYTES;
310 	packet->type = INTEL_PT_BIP;
311 	packet->count = buf[0] >> 3;
312 	memcpy_le64(&packet->payload, buf + 1, 8);
313 	return 9;
314 }
315 
316 static int intel_pt_get_bep(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;
321 	return 2;
322 }
323 
324 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
325 {
326 	if (len < 2)
327 		return INTEL_PT_NEED_MORE_BYTES;
328 	packet->type = INTEL_PT_BEP_IP;
329 	return 2;
330 }
331 
332 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
333 			    struct intel_pt_pkt *packet)
334 {
335 	if (len < 2)
336 		return INTEL_PT_NEED_MORE_BYTES;
337 
338 	if ((buf[1] & 0x1f) == 0x12)
339 		return intel_pt_get_ptwrite(buf, len, packet);
340 
341 	switch (buf[1]) {
342 	case 0xa3: /* Long TNT */
343 		return intel_pt_get_long_tnt(buf, len, packet);
344 	case 0x43: /* PIP */
345 		return intel_pt_get_pip(buf, len, packet);
346 	case 0x83: /* TraceStop */
347 		return intel_pt_get_tracestop(packet);
348 	case 0x03: /* CBR */
349 		return intel_pt_get_cbr(buf, len, packet);
350 	case 0xc8: /* VMCS */
351 		return intel_pt_get_vmcs(buf, len, packet);
352 	case 0xf3: /* OVF */
353 		return intel_pt_get_ovf(packet);
354 	case 0x82: /* PSB */
355 		return intel_pt_get_psb(buf, len, packet);
356 	case 0x23: /* PSBEND */
357 		return intel_pt_get_psbend(packet);
358 	case 0x73: /* TMA */
359 		return intel_pt_get_tma(buf, len, packet);
360 	case 0xC3: /* 3-byte header */
361 		return intel_pt_get_3byte(buf, len, packet);
362 	case 0x62: /* EXSTOP no IP */
363 		return intel_pt_get_exstop(packet);
364 	case 0xE2: /* EXSTOP with IP */
365 		return intel_pt_get_exstop_ip(packet);
366 	case 0xC2: /* MWAIT */
367 		return intel_pt_get_mwait(buf, len, packet);
368 	case 0x22: /* PWRE */
369 		return intel_pt_get_pwre(buf, len, packet);
370 	case 0xA2: /* PWRX */
371 		return intel_pt_get_pwrx(buf, len, packet);
372 	case 0x63: /* BBP */
373 		return intel_pt_get_bbp(buf, len, packet);
374 	case 0x33: /* BEP no IP */
375 		return intel_pt_get_bep(len, packet);
376 	case 0xb3: /* BEP with IP */
377 		return intel_pt_get_bep_ip(len, packet);
378 	default:
379 		return INTEL_PT_BAD_PACKET;
380 	}
381 }
382 
383 static int intel_pt_get_short_tnt(unsigned int byte,
384 				  struct intel_pt_pkt *packet)
385 {
386 	int count;
387 
388 	for (count = 6; count; count--) {
389 		if (byte & BIT(7))
390 			break;
391 		byte <<= 1;
392 	}
393 
394 	packet->type = INTEL_PT_TNT;
395 	packet->count = count;
396 	packet->payload = (uint64_t)byte << 57;
397 
398 	return 1;
399 }
400 
401 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
402 			    size_t len, struct intel_pt_pkt *packet)
403 {
404 	unsigned int offs = 1, shift;
405 	uint64_t payload = byte >> 3;
406 
407 	byte >>= 2;
408 	len -= 1;
409 	for (shift = 5; byte & 1; shift += 7) {
410 		if (offs > 9)
411 			return INTEL_PT_BAD_PACKET;
412 		if (len < offs)
413 			return INTEL_PT_NEED_MORE_BYTES;
414 		byte = buf[offs++];
415 		payload |= ((uint64_t)byte >> 1) << shift;
416 	}
417 
418 	packet->type = INTEL_PT_CYC;
419 	packet->payload = payload;
420 	return offs;
421 }
422 
423 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
424 			   const unsigned char *buf, size_t len,
425 			   struct intel_pt_pkt *packet)
426 {
427 	int ip_len;
428 
429 	packet->count = byte >> 5;
430 
431 	switch (packet->count) {
432 	case 0:
433 		ip_len = 0;
434 		break;
435 	case 1:
436 		if (len < 3)
437 			return INTEL_PT_NEED_MORE_BYTES;
438 		ip_len = 2;
439 		packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
440 		break;
441 	case 2:
442 		if (len < 5)
443 			return INTEL_PT_NEED_MORE_BYTES;
444 		ip_len = 4;
445 		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
446 		break;
447 	case 3:
448 	case 4:
449 		if (len < 7)
450 			return INTEL_PT_NEED_MORE_BYTES;
451 		ip_len = 6;
452 		memcpy_le64(&packet->payload, buf + 1, 6);
453 		break;
454 	case 6:
455 		if (len < 9)
456 			return INTEL_PT_NEED_MORE_BYTES;
457 		ip_len = 8;
458 		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
459 		break;
460 	default:
461 		return INTEL_PT_BAD_PACKET;
462 	}
463 
464 	packet->type = type;
465 
466 	return ip_len + 1;
467 }
468 
469 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
470 			     struct intel_pt_pkt *packet)
471 {
472 	if (len < 2)
473 		return INTEL_PT_NEED_MORE_BYTES;
474 
475 	switch (buf[1] >> 5) {
476 	case 0:
477 		packet->type = INTEL_PT_MODE_EXEC;
478 		switch (buf[1] & 3) {
479 		case 0:
480 			packet->payload = 16;
481 			break;
482 		case 1:
483 			packet->payload = 64;
484 			break;
485 		case 2:
486 			packet->payload = 32;
487 			break;
488 		default:
489 			return INTEL_PT_BAD_PACKET;
490 		}
491 		break;
492 	case 1:
493 		packet->type = INTEL_PT_MODE_TSX;
494 		if ((buf[1] & 3) == 3)
495 			return INTEL_PT_BAD_PACKET;
496 		packet->payload = buf[1] & 3;
497 		break;
498 	default:
499 		return INTEL_PT_BAD_PACKET;
500 	}
501 
502 	return 2;
503 }
504 
505 static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
506 			    struct intel_pt_pkt *packet)
507 {
508 	if (len < 8)
509 		return INTEL_PT_NEED_MORE_BYTES;
510 	packet->type = INTEL_PT_TSC;
511 	memcpy_le64(&packet->payload, buf + 1, 7);
512 	return 8;
513 }
514 
515 static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
516 			    struct intel_pt_pkt *packet)
517 {
518 	if (len < 2)
519 		return INTEL_PT_NEED_MORE_BYTES;
520 	packet->type = INTEL_PT_MTC;
521 	packet->payload = buf[1];
522 	return 2;
523 }
524 
525 static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
526 				  struct intel_pt_pkt *packet,
527 				  enum intel_pt_pkt_ctx ctx)
528 {
529 	unsigned int byte;
530 
531 	memset(packet, 0, sizeof(struct intel_pt_pkt));
532 
533 	if (!len)
534 		return INTEL_PT_NEED_MORE_BYTES;
535 
536 	byte = buf[0];
537 
538 	switch (ctx) {
539 	case INTEL_PT_NO_CTX:
540 		break;
541 	case INTEL_PT_BLK_4_CTX:
542 		if ((byte & 0x7) == 4)
543 			return intel_pt_get_bip_4(buf, len, packet);
544 		break;
545 	case INTEL_PT_BLK_8_CTX:
546 		if ((byte & 0x7) == 4)
547 			return intel_pt_get_bip_8(buf, len, packet);
548 		break;
549 	default:
550 		break;
551 	}
552 
553 	if (!(byte & BIT(0))) {
554 		if (byte == 0)
555 			return intel_pt_get_pad(packet);
556 		if (byte == 2)
557 			return intel_pt_get_ext(buf, len, packet);
558 		return intel_pt_get_short_tnt(byte, packet);
559 	}
560 
561 	if ((byte & 2))
562 		return intel_pt_get_cyc(byte, buf, len, packet);
563 
564 	switch (byte & 0x1f) {
565 	case 0x0D:
566 		return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
567 	case 0x11:
568 		return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
569 				       packet);
570 	case 0x01:
571 		return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
572 				       packet);
573 	case 0x1D:
574 		return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
575 	case 0x19:
576 		switch (byte) {
577 		case 0x99:
578 			return intel_pt_get_mode(buf, len, packet);
579 		case 0x19:
580 			return intel_pt_get_tsc(buf, len, packet);
581 		case 0x59:
582 			return intel_pt_get_mtc(buf, len, packet);
583 		default:
584 			return INTEL_PT_BAD_PACKET;
585 		}
586 	default:
587 		return INTEL_PT_BAD_PACKET;
588 	}
589 }
590 
591 void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
592 			  enum intel_pt_pkt_ctx *ctx)
593 {
594 	switch (packet->type) {
595 	case INTEL_PT_BAD:
596 	case INTEL_PT_PAD:
597 	case INTEL_PT_TSC:
598 	case INTEL_PT_TMA:
599 	case INTEL_PT_MTC:
600 	case INTEL_PT_FUP:
601 	case INTEL_PT_CYC:
602 	case INTEL_PT_CBR:
603 	case INTEL_PT_MNT:
604 	case INTEL_PT_EXSTOP:
605 	case INTEL_PT_EXSTOP_IP:
606 	case INTEL_PT_PWRE:
607 	case INTEL_PT_PWRX:
608 	case INTEL_PT_BIP:
609 		break;
610 	case INTEL_PT_TNT:
611 	case INTEL_PT_TIP:
612 	case INTEL_PT_TIP_PGD:
613 	case INTEL_PT_TIP_PGE:
614 	case INTEL_PT_MODE_EXEC:
615 	case INTEL_PT_MODE_TSX:
616 	case INTEL_PT_PIP:
617 	case INTEL_PT_OVF:
618 	case INTEL_PT_VMCS:
619 	case INTEL_PT_TRACESTOP:
620 	case INTEL_PT_PSB:
621 	case INTEL_PT_PSBEND:
622 	case INTEL_PT_PTWRITE:
623 	case INTEL_PT_PTWRITE_IP:
624 	case INTEL_PT_MWAIT:
625 	case INTEL_PT_BEP:
626 	case INTEL_PT_BEP_IP:
627 		*ctx = INTEL_PT_NO_CTX;
628 		break;
629 	case INTEL_PT_BBP:
630 		if (packet->count)
631 			*ctx = INTEL_PT_BLK_4_CTX;
632 		else
633 			*ctx = INTEL_PT_BLK_8_CTX;
634 		break;
635 	default:
636 		break;
637 	}
638 }
639 
640 int intel_pt_get_packet(const unsigned char *buf, size_t len,
641 			struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
642 {
643 	int ret;
644 
645 	ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
646 	if (ret > 0) {
647 		while (ret < 8 && len > (size_t)ret && !buf[ret])
648 			ret += 1;
649 		intel_pt_upd_pkt_ctx(packet, ctx);
650 	}
651 	return ret;
652 }
653 
654 int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
655 		      size_t buf_len)
656 {
657 	int ret, i, nr;
658 	unsigned long long payload = packet->payload;
659 	const char *name = intel_pt_pkt_name(packet->type);
660 
661 	switch (packet->type) {
662 	case INTEL_PT_BAD:
663 	case INTEL_PT_PAD:
664 	case INTEL_PT_PSB:
665 	case INTEL_PT_PSBEND:
666 	case INTEL_PT_TRACESTOP:
667 	case INTEL_PT_OVF:
668 		return snprintf(buf, buf_len, "%s", name);
669 	case INTEL_PT_TNT: {
670 		size_t blen = buf_len;
671 
672 		ret = snprintf(buf, blen, "%s ", name);
673 		if (ret < 0)
674 			return ret;
675 		buf += ret;
676 		blen -= ret;
677 		for (i = 0; i < packet->count; i++) {
678 			if (payload & BIT63)
679 				ret = snprintf(buf, blen, "T");
680 			else
681 				ret = snprintf(buf, blen, "N");
682 			if (ret < 0)
683 				return ret;
684 			buf += ret;
685 			blen -= ret;
686 			payload <<= 1;
687 		}
688 		ret = snprintf(buf, blen, " (%d)", packet->count);
689 		if (ret < 0)
690 			return ret;
691 		blen -= ret;
692 		return buf_len - blen;
693 	}
694 	case INTEL_PT_TIP_PGD:
695 	case INTEL_PT_TIP_PGE:
696 	case INTEL_PT_TIP:
697 	case INTEL_PT_FUP:
698 		if (!(packet->count))
699 			return snprintf(buf, buf_len, "%s no ip", name);
700 		__fallthrough;
701 	case INTEL_PT_CYC:
702 	case INTEL_PT_VMCS:
703 	case INTEL_PT_MTC:
704 	case INTEL_PT_MNT:
705 	case INTEL_PT_CBR:
706 	case INTEL_PT_TSC:
707 		return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
708 	case INTEL_PT_TMA:
709 		return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
710 				(unsigned)payload, packet->count);
711 	case INTEL_PT_MODE_EXEC:
712 		return snprintf(buf, buf_len, "%s %lld", name, payload);
713 	case INTEL_PT_MODE_TSX:
714 		return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
715 				name, (unsigned)(payload >> 1) & 1,
716 				(unsigned)payload & 1);
717 	case INTEL_PT_PIP:
718 		nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
719 		payload &= ~INTEL_PT_VMX_NR_FLAG;
720 		ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
721 			       name, payload >> 1, nr);
722 		return ret;
723 	case INTEL_PT_PTWRITE:
724 		return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
725 	case INTEL_PT_PTWRITE_IP:
726 		return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
727 	case INTEL_PT_BEP:
728 	case INTEL_PT_EXSTOP:
729 		return snprintf(buf, buf_len, "%s IP:0", name);
730 	case INTEL_PT_BEP_IP:
731 	case INTEL_PT_EXSTOP_IP:
732 		return snprintf(buf, buf_len, "%s IP:1", name);
733 	case INTEL_PT_MWAIT:
734 		return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
735 				name, payload, (unsigned int)(payload & 0xff),
736 				(unsigned int)((payload >> 32) & 0x3));
737 	case INTEL_PT_PWRE:
738 		return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
739 				name, payload, !!(payload & 0x80),
740 				(unsigned int)((payload >> 12) & 0xf),
741 				(unsigned int)((payload >> 8) & 0xf));
742 	case INTEL_PT_PWRX:
743 		return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
744 				name, payload,
745 				(unsigned int)((payload >> 4) & 0xf),
746 				(unsigned int)(payload & 0xf),
747 				(unsigned int)((payload >> 8) & 0xf));
748 	case INTEL_PT_BBP:
749 		return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
750 				name, packet->count ? "4" : "8", payload);
751 	case INTEL_PT_BIP:
752 		return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
753 				name, packet->count, payload);
754 	default:
755 		break;
756 	}
757 	return snprintf(buf, buf_len, "%s 0x%llx (%d)",
758 			name, payload, packet->count);
759 }
760