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 [INTEL_PT_CFE] = "CFE",
68 [INTEL_PT_CFE_IP] = "CFE",
69 [INTEL_PT_EVD] = "EVD",
70 };
71
intel_pt_pkt_name(enum intel_pt_pkt_type type)72 const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
73 {
74 return packet_name[type];
75 }
76
intel_pt_get_long_tnt(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_pip(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_tracestop(struct intel_pt_pkt * packet)115 static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
116 {
117 packet->type = INTEL_PT_TRACESTOP;
118 return 2;
119 }
120
intel_pt_get_cbr(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_vmcs(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_ovf(struct intel_pt_pkt * packet)149 static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
150 {
151 packet->type = INTEL_PT_OVF;
152 return 2;
153 }
154
intel_pt_get_psb(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_psbend(struct intel_pt_pkt * packet)172 static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
173 {
174 packet->type = INTEL_PT_PSBEND;
175 return 2;
176 }
177
intel_pt_get_tma(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_pad(struct intel_pt_pkt * packet)190 static int intel_pt_get_pad(struct intel_pt_pkt *packet)
191 {
192 packet->type = INTEL_PT_PAD;
193 return 1;
194 }
195
intel_pt_get_mnt(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_3byte(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)207 static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
208 struct intel_pt_pkt *packet)
209 {
210 if (len < 3)
211 return INTEL_PT_NEED_MORE_BYTES;
212
213 switch (buf[2]) {
214 case 0x88: /* MNT */
215 return intel_pt_get_mnt(buf, len, packet);
216 default:
217 return INTEL_PT_BAD_PACKET;
218 }
219 }
220
intel_pt_get_ptwrite(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)221 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
222 struct intel_pt_pkt *packet)
223 {
224 packet->count = (buf[1] >> 5) & 0x3;
225 packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
226 INTEL_PT_PTWRITE;
227
228 switch (packet->count) {
229 case 0:
230 if (len < 6)
231 return INTEL_PT_NEED_MORE_BYTES;
232 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
233 return 6;
234 case 1:
235 if (len < 10)
236 return INTEL_PT_NEED_MORE_BYTES;
237 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
238 return 10;
239 default:
240 return INTEL_PT_BAD_PACKET;
241 }
242 }
243
intel_pt_get_exstop(struct intel_pt_pkt * packet)244 static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
245 {
246 packet->type = INTEL_PT_EXSTOP;
247 return 2;
248 }
249
intel_pt_get_exstop_ip(struct intel_pt_pkt * packet)250 static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
251 {
252 packet->type = INTEL_PT_EXSTOP_IP;
253 return 2;
254 }
255
intel_pt_get_mwait(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)256 static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
257 struct intel_pt_pkt *packet)
258 {
259 if (len < 10)
260 return INTEL_PT_NEED_MORE_BYTES;
261 packet->type = INTEL_PT_MWAIT;
262 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
263 return 10;
264 }
265
intel_pt_get_pwre(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)266 static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
267 struct intel_pt_pkt *packet)
268 {
269 if (len < 4)
270 return INTEL_PT_NEED_MORE_BYTES;
271 packet->type = INTEL_PT_PWRE;
272 memcpy_le64(&packet->payload, buf + 2, 2);
273 return 4;
274 }
275
intel_pt_get_pwrx(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)276 static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
277 struct intel_pt_pkt *packet)
278 {
279 if (len < 7)
280 return INTEL_PT_NEED_MORE_BYTES;
281 packet->type = INTEL_PT_PWRX;
282 memcpy_le64(&packet->payload, buf + 2, 5);
283 return 7;
284 }
285
intel_pt_get_bbp(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)286 static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
287 struct intel_pt_pkt *packet)
288 {
289 if (len < 3)
290 return INTEL_PT_NEED_MORE_BYTES;
291 packet->type = INTEL_PT_BBP;
292 packet->count = buf[2] >> 7;
293 packet->payload = buf[2] & 0x1f;
294 return 3;
295 }
296
intel_pt_get_bip_4(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)297 static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
298 struct intel_pt_pkt *packet)
299 {
300 if (len < 5)
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, 4);
305 return 5;
306 }
307
intel_pt_get_bip_8(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)308 static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
309 struct intel_pt_pkt *packet)
310 {
311 if (len < 9)
312 return INTEL_PT_NEED_MORE_BYTES;
313 packet->type = INTEL_PT_BIP;
314 packet->count = buf[0] >> 3;
315 memcpy_le64(&packet->payload, buf + 1, 8);
316 return 9;
317 }
318
intel_pt_get_bep(size_t len,struct intel_pt_pkt * packet)319 static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
320 {
321 if (len < 2)
322 return INTEL_PT_NEED_MORE_BYTES;
323 packet->type = INTEL_PT_BEP;
324 return 2;
325 }
326
intel_pt_get_bep_ip(size_t len,struct intel_pt_pkt * packet)327 static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
328 {
329 if (len < 2)
330 return INTEL_PT_NEED_MORE_BYTES;
331 packet->type = INTEL_PT_BEP_IP;
332 return 2;
333 }
334
intel_pt_get_cfe(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)335 static int intel_pt_get_cfe(const unsigned char *buf, size_t len,
336 struct intel_pt_pkt *packet)
337 {
338 if (len < 4)
339 return INTEL_PT_NEED_MORE_BYTES;
340 packet->type = buf[2] & 0x80 ? INTEL_PT_CFE_IP : INTEL_PT_CFE;
341 packet->count = buf[2] & 0x1f;
342 packet->payload = buf[3];
343 return 4;
344 }
345
intel_pt_get_evd(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)346 static int intel_pt_get_evd(const unsigned char *buf, size_t len,
347 struct intel_pt_pkt *packet)
348 {
349 if (len < 11)
350 return INTEL_PT_NEED_MORE_BYTES;
351 packet->type = INTEL_PT_EVD;
352 packet->count = buf[2] & 0x3f;
353 packet->payload = buf[3];
354 memcpy_le64(&packet->payload, buf + 3, 8);
355 return 11;
356 }
357
intel_pt_get_ext(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)358 static int intel_pt_get_ext(const unsigned char *buf, size_t len,
359 struct intel_pt_pkt *packet)
360 {
361 if (len < 2)
362 return INTEL_PT_NEED_MORE_BYTES;
363
364 if ((buf[1] & 0x1f) == 0x12)
365 return intel_pt_get_ptwrite(buf, len, packet);
366
367 switch (buf[1]) {
368 case 0xa3: /* Long TNT */
369 return intel_pt_get_long_tnt(buf, len, packet);
370 case 0x43: /* PIP */
371 return intel_pt_get_pip(buf, len, packet);
372 case 0x83: /* TraceStop */
373 return intel_pt_get_tracestop(packet);
374 case 0x03: /* CBR */
375 return intel_pt_get_cbr(buf, len, packet);
376 case 0xc8: /* VMCS */
377 return intel_pt_get_vmcs(buf, len, packet);
378 case 0xf3: /* OVF */
379 return intel_pt_get_ovf(packet);
380 case 0x82: /* PSB */
381 return intel_pt_get_psb(buf, len, packet);
382 case 0x23: /* PSBEND */
383 return intel_pt_get_psbend(packet);
384 case 0x73: /* TMA */
385 return intel_pt_get_tma(buf, len, packet);
386 case 0xC3: /* 3-byte header */
387 return intel_pt_get_3byte(buf, len, packet);
388 case 0x62: /* EXSTOP no IP */
389 return intel_pt_get_exstop(packet);
390 case 0xE2: /* EXSTOP with IP */
391 return intel_pt_get_exstop_ip(packet);
392 case 0xC2: /* MWAIT */
393 return intel_pt_get_mwait(buf, len, packet);
394 case 0x22: /* PWRE */
395 return intel_pt_get_pwre(buf, len, packet);
396 case 0xA2: /* PWRX */
397 return intel_pt_get_pwrx(buf, len, packet);
398 case 0x63: /* BBP */
399 return intel_pt_get_bbp(buf, len, packet);
400 case 0x33: /* BEP no IP */
401 return intel_pt_get_bep(len, packet);
402 case 0xb3: /* BEP with IP */
403 return intel_pt_get_bep_ip(len, packet);
404 case 0x13: /* CFE */
405 return intel_pt_get_cfe(buf, len, packet);
406 case 0x53: /* EVD */
407 return intel_pt_get_evd(buf, len, packet);
408 default:
409 return INTEL_PT_BAD_PACKET;
410 }
411 }
412
intel_pt_get_short_tnt(unsigned int byte,struct intel_pt_pkt * packet)413 static int intel_pt_get_short_tnt(unsigned int byte,
414 struct intel_pt_pkt *packet)
415 {
416 int count;
417
418 for (count = 6; count; count--) {
419 if (byte & BIT(7))
420 break;
421 byte <<= 1;
422 }
423
424 packet->type = INTEL_PT_TNT;
425 packet->count = count;
426 packet->payload = (uint64_t)byte << 57;
427
428 return 1;
429 }
430
intel_pt_get_cyc(unsigned int byte,const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)431 static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
432 size_t len, struct intel_pt_pkt *packet)
433 {
434 unsigned int offs = 1, shift;
435 uint64_t payload = byte >> 3;
436
437 byte >>= 2;
438 len -= 1;
439 for (shift = 5; byte & 1; shift += 7) {
440 if (offs > 9)
441 return INTEL_PT_BAD_PACKET;
442 if (len < offs)
443 return INTEL_PT_NEED_MORE_BYTES;
444 byte = buf[offs++];
445 payload |= ((uint64_t)byte >> 1) << shift;
446 }
447
448 packet->type = INTEL_PT_CYC;
449 packet->payload = payload;
450 return offs;
451 }
452
intel_pt_get_ip(enum intel_pt_pkt_type type,unsigned int byte,const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)453 static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
454 const unsigned char *buf, size_t len,
455 struct intel_pt_pkt *packet)
456 {
457 int ip_len;
458
459 packet->count = byte >> 5;
460
461 switch (packet->count) {
462 case 0:
463 ip_len = 0;
464 break;
465 case 1:
466 if (len < 3)
467 return INTEL_PT_NEED_MORE_BYTES;
468 ip_len = 2;
469 packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
470 break;
471 case 2:
472 if (len < 5)
473 return INTEL_PT_NEED_MORE_BYTES;
474 ip_len = 4;
475 packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
476 break;
477 case 3:
478 case 4:
479 if (len < 7)
480 return INTEL_PT_NEED_MORE_BYTES;
481 ip_len = 6;
482 memcpy_le64(&packet->payload, buf + 1, 6);
483 break;
484 case 6:
485 if (len < 9)
486 return INTEL_PT_NEED_MORE_BYTES;
487 ip_len = 8;
488 packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
489 break;
490 default:
491 return INTEL_PT_BAD_PACKET;
492 }
493
494 packet->type = type;
495
496 return ip_len + 1;
497 }
498
intel_pt_get_mode(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)499 static int intel_pt_get_mode(const unsigned char *buf, size_t len,
500 struct intel_pt_pkt *packet)
501 {
502 if (len < 2)
503 return INTEL_PT_NEED_MORE_BYTES;
504
505 switch (buf[1] >> 5) {
506 case 0:
507 packet->type = INTEL_PT_MODE_EXEC;
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
intel_pt_get_tsc(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_get_mtc(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet)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
intel_pt_do_get_packet(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet,enum intel_pt_pkt_ctx ctx)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
intel_pt_upd_pkt_ctx(const struct intel_pt_pkt * packet,enum intel_pt_pkt_ctx * ctx)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
intel_pt_get_packet(const unsigned char * buf,size_t len,struct intel_pt_pkt * packet,enum intel_pt_pkt_ctx * ctx)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
intel_pt_pkt_desc(const struct intel_pt_pkt * packet,char * buf,size_t buf_len)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 %lld", name, payload);
746 case INTEL_PT_MODE_TSX:
747 return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
748 name, (unsigned)(payload >> 1) & 1,
749 (unsigned)payload & 1);
750 case INTEL_PT_PIP:
751 nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
752 payload &= ~INTEL_PT_VMX_NR_FLAG;
753 ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
754 name, payload >> 1, nr);
755 return ret;
756 case INTEL_PT_PTWRITE:
757 return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
758 case INTEL_PT_PTWRITE_IP:
759 return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
760 case INTEL_PT_BEP:
761 case INTEL_PT_EXSTOP:
762 return snprintf(buf, buf_len, "%s IP:0", name);
763 case INTEL_PT_BEP_IP:
764 case INTEL_PT_EXSTOP_IP:
765 return snprintf(buf, buf_len, "%s IP:1", name);
766 case INTEL_PT_MWAIT:
767 return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
768 name, payload, (unsigned int)(payload & 0xff),
769 (unsigned int)((payload >> 32) & 0x3));
770 case INTEL_PT_PWRE:
771 return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
772 name, payload, !!(payload & 0x80),
773 (unsigned int)((payload >> 12) & 0xf),
774 (unsigned int)((payload >> 8) & 0xf));
775 case INTEL_PT_PWRX:
776 return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
777 name, payload,
778 (unsigned int)((payload >> 4) & 0xf),
779 (unsigned int)(payload & 0xf),
780 (unsigned int)((payload >> 8) & 0xf));
781 case INTEL_PT_BBP:
782 return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
783 name, packet->count ? "4" : "8", payload);
784 case INTEL_PT_BIP:
785 return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
786 name, packet->count, payload);
787 case INTEL_PT_CFE:
788 case INTEL_PT_CFE_IP:
789 return snprintf(buf, buf_len, "%s IP:%d Type 0x%02x Vector 0x%llx",
790 name, packet->type == INTEL_PT_CFE_IP, packet->count, payload);
791 case INTEL_PT_EVD:
792 return snprintf(buf, buf_len, "%s Type 0x%02x Payload 0x%llx",
793 name, packet->count, payload);
794 default:
795 break;
796 }
797 return snprintf(buf, buf_len, "%s 0x%llx (%d)",
798 name, payload, packet->count);
799 }
800