intel-pt-pkt-decoder.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * intel_pt_pkt_decoder.c: Intel Processor Trace support
  3. * Copyright (c) 2013-2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <endian.h>
  18. #include <byteswap.h>
  19. #include "intel-pt-pkt-decoder.h"
  20. #define BIT(n) (1 << (n))
  21. #define BIT63 ((uint64_t)1 << 63)
  22. #define NR_FLAG BIT63
  23. #if __BYTE_ORDER == __BIG_ENDIAN
  24. #define le16_to_cpu bswap_16
  25. #define le32_to_cpu bswap_32
  26. #define le64_to_cpu bswap_64
  27. #define memcpy_le64(d, s, n) do { \
  28. memcpy((d), (s), (n)); \
  29. *(d) = le64_to_cpu(*(d)); \
  30. } while (0)
  31. #else
  32. #define le16_to_cpu
  33. #define le32_to_cpu
  34. #define le64_to_cpu
  35. #define memcpy_le64 memcpy
  36. #endif
  37. static const char * const packet_name[] = {
  38. [INTEL_PT_BAD] = "Bad Packet!",
  39. [INTEL_PT_PAD] = "PAD",
  40. [INTEL_PT_TNT] = "TNT",
  41. [INTEL_PT_TIP_PGD] = "TIP.PGD",
  42. [INTEL_PT_TIP_PGE] = "TIP.PGE",
  43. [INTEL_PT_TSC] = "TSC",
  44. [INTEL_PT_TMA] = "TMA",
  45. [INTEL_PT_MODE_EXEC] = "MODE.Exec",
  46. [INTEL_PT_MODE_TSX] = "MODE.TSX",
  47. [INTEL_PT_MTC] = "MTC",
  48. [INTEL_PT_TIP] = "TIP",
  49. [INTEL_PT_FUP] = "FUP",
  50. [INTEL_PT_CYC] = "CYC",
  51. [INTEL_PT_VMCS] = "VMCS",
  52. [INTEL_PT_PSB] = "PSB",
  53. [INTEL_PT_PSBEND] = "PSBEND",
  54. [INTEL_PT_CBR] = "CBR",
  55. [INTEL_PT_TRACESTOP] = "TraceSTOP",
  56. [INTEL_PT_PIP] = "PIP",
  57. [INTEL_PT_OVF] = "OVF",
  58. [INTEL_PT_MNT] = "MNT",
  59. };
  60. const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
  61. {
  62. return packet_name[type];
  63. }
  64. static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
  65. struct intel_pt_pkt *packet)
  66. {
  67. uint64_t payload;
  68. int count;
  69. if (len < 8)
  70. return INTEL_PT_NEED_MORE_BYTES;
  71. payload = le64_to_cpu(*(uint64_t *)buf);
  72. for (count = 47; count; count--) {
  73. if (payload & BIT63)
  74. break;
  75. payload <<= 1;
  76. }
  77. packet->type = INTEL_PT_TNT;
  78. packet->count = count;
  79. packet->payload = payload << 1;
  80. return 8;
  81. }
  82. static int intel_pt_get_pip(const unsigned char *buf, size_t len,
  83. struct intel_pt_pkt *packet)
  84. {
  85. uint64_t payload = 0;
  86. if (len < 8)
  87. return INTEL_PT_NEED_MORE_BYTES;
  88. packet->type = INTEL_PT_PIP;
  89. memcpy_le64(&payload, buf + 2, 6);
  90. packet->payload = payload >> 1;
  91. if (payload & 1)
  92. packet->payload |= NR_FLAG;
  93. return 8;
  94. }
  95. static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
  96. {
  97. packet->type = INTEL_PT_TRACESTOP;
  98. return 2;
  99. }
  100. static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
  101. struct intel_pt_pkt *packet)
  102. {
  103. if (len < 4)
  104. return INTEL_PT_NEED_MORE_BYTES;
  105. packet->type = INTEL_PT_CBR;
  106. packet->payload = buf[2];
  107. return 4;
  108. }
  109. static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
  110. struct intel_pt_pkt *packet)
  111. {
  112. unsigned int count = (52 - 5) >> 3;
  113. if (count < 1 || count > 7)
  114. return INTEL_PT_BAD_PACKET;
  115. if (len < count + 2)
  116. return INTEL_PT_NEED_MORE_BYTES;
  117. packet->type = INTEL_PT_VMCS;
  118. packet->count = count;
  119. memcpy_le64(&packet->payload, buf + 2, count);
  120. return count + 2;
  121. }
  122. static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
  123. {
  124. packet->type = INTEL_PT_OVF;
  125. return 2;
  126. }
  127. static int intel_pt_get_psb(const unsigned char *buf, size_t len,
  128. struct intel_pt_pkt *packet)
  129. {
  130. int i;
  131. if (len < 16)
  132. return INTEL_PT_NEED_MORE_BYTES;
  133. for (i = 2; i < 16; i += 2) {
  134. if (buf[i] != 2 || buf[i + 1] != 0x82)
  135. return INTEL_PT_BAD_PACKET;
  136. }
  137. packet->type = INTEL_PT_PSB;
  138. return 16;
  139. }
  140. static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
  141. {
  142. packet->type = INTEL_PT_PSBEND;
  143. return 2;
  144. }
  145. static int intel_pt_get_tma(const unsigned char *buf, size_t len,
  146. struct intel_pt_pkt *packet)
  147. {
  148. if (len < 7)
  149. return INTEL_PT_NEED_MORE_BYTES;
  150. packet->type = INTEL_PT_TMA;
  151. packet->payload = buf[2] | (buf[3] << 8);
  152. packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
  153. return 7;
  154. }
  155. static int intel_pt_get_pad(struct intel_pt_pkt *packet)
  156. {
  157. packet->type = INTEL_PT_PAD;
  158. return 1;
  159. }
  160. static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
  161. struct intel_pt_pkt *packet)
  162. {
  163. if (len < 11)
  164. return INTEL_PT_NEED_MORE_BYTES;
  165. packet->type = INTEL_PT_MNT;
  166. memcpy_le64(&packet->payload, buf + 3, 8);
  167. return 11
  168. ;
  169. }
  170. static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
  171. struct intel_pt_pkt *packet)
  172. {
  173. if (len < 3)
  174. return INTEL_PT_NEED_MORE_BYTES;
  175. switch (buf[2]) {
  176. case 0x88: /* MNT */
  177. return intel_pt_get_mnt(buf, len, packet);
  178. default:
  179. return INTEL_PT_BAD_PACKET;
  180. }
  181. }
  182. static int intel_pt_get_ext(const unsigned char *buf, size_t len,
  183. struct intel_pt_pkt *packet)
  184. {
  185. if (len < 2)
  186. return INTEL_PT_NEED_MORE_BYTES;
  187. switch (buf[1]) {
  188. case 0xa3: /* Long TNT */
  189. return intel_pt_get_long_tnt(buf, len, packet);
  190. case 0x43: /* PIP */
  191. return intel_pt_get_pip(buf, len, packet);
  192. case 0x83: /* TraceStop */
  193. return intel_pt_get_tracestop(packet);
  194. case 0x03: /* CBR */
  195. return intel_pt_get_cbr(buf, len, packet);
  196. case 0xc8: /* VMCS */
  197. return intel_pt_get_vmcs(buf, len, packet);
  198. case 0xf3: /* OVF */
  199. return intel_pt_get_ovf(packet);
  200. case 0x82: /* PSB */
  201. return intel_pt_get_psb(buf, len, packet);
  202. case 0x23: /* PSBEND */
  203. return intel_pt_get_psbend(packet);
  204. case 0x73: /* TMA */
  205. return intel_pt_get_tma(buf, len, packet);
  206. case 0xC3: /* 3-byte header */
  207. return intel_pt_get_3byte(buf, len, packet);
  208. default:
  209. return INTEL_PT_BAD_PACKET;
  210. }
  211. }
  212. static int intel_pt_get_short_tnt(unsigned int byte,
  213. struct intel_pt_pkt *packet)
  214. {
  215. int count;
  216. for (count = 6; count; count--) {
  217. if (byte & BIT(7))
  218. break;
  219. byte <<= 1;
  220. }
  221. packet->type = INTEL_PT_TNT;
  222. packet->count = count;
  223. packet->payload = (uint64_t)byte << 57;
  224. return 1;
  225. }
  226. static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
  227. size_t len, struct intel_pt_pkt *packet)
  228. {
  229. unsigned int offs = 1, shift;
  230. uint64_t payload = byte >> 3;
  231. byte >>= 2;
  232. len -= 1;
  233. for (shift = 5; byte & 1; shift += 7) {
  234. if (offs > 9)
  235. return INTEL_PT_BAD_PACKET;
  236. if (len < offs)
  237. return INTEL_PT_NEED_MORE_BYTES;
  238. byte = buf[offs++];
  239. payload |= (byte >> 1) << shift;
  240. }
  241. packet->type = INTEL_PT_CYC;
  242. packet->payload = payload;
  243. return offs;
  244. }
  245. static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
  246. const unsigned char *buf, size_t len,
  247. struct intel_pt_pkt *packet)
  248. {
  249. int ip_len;
  250. packet->count = byte >> 5;
  251. switch (packet->count) {
  252. case 0:
  253. ip_len = 0;
  254. break;
  255. case 1:
  256. if (len < 3)
  257. return INTEL_PT_NEED_MORE_BYTES;
  258. ip_len = 2;
  259. packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
  260. break;
  261. case 2:
  262. if (len < 5)
  263. return INTEL_PT_NEED_MORE_BYTES;
  264. ip_len = 4;
  265. packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
  266. break;
  267. case 3:
  268. case 4:
  269. if (len < 7)
  270. return INTEL_PT_NEED_MORE_BYTES;
  271. ip_len = 6;
  272. memcpy_le64(&packet->payload, buf + 1, 6);
  273. break;
  274. case 6:
  275. if (len < 9)
  276. return INTEL_PT_NEED_MORE_BYTES;
  277. ip_len = 8;
  278. packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
  279. break;
  280. default:
  281. return INTEL_PT_BAD_PACKET;
  282. }
  283. packet->type = type;
  284. return ip_len + 1;
  285. }
  286. static int intel_pt_get_mode(const unsigned char *buf, size_t len,
  287. struct intel_pt_pkt *packet)
  288. {
  289. if (len < 2)
  290. return INTEL_PT_NEED_MORE_BYTES;
  291. switch (buf[1] >> 5) {
  292. case 0:
  293. packet->type = INTEL_PT_MODE_EXEC;
  294. switch (buf[1] & 3) {
  295. case 0:
  296. packet->payload = 16;
  297. break;
  298. case 1:
  299. packet->payload = 64;
  300. break;
  301. case 2:
  302. packet->payload = 32;
  303. break;
  304. default:
  305. return INTEL_PT_BAD_PACKET;
  306. }
  307. break;
  308. case 1:
  309. packet->type = INTEL_PT_MODE_TSX;
  310. if ((buf[1] & 3) == 3)
  311. return INTEL_PT_BAD_PACKET;
  312. packet->payload = buf[1] & 3;
  313. break;
  314. default:
  315. return INTEL_PT_BAD_PACKET;
  316. }
  317. return 2;
  318. }
  319. static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
  320. struct intel_pt_pkt *packet)
  321. {
  322. if (len < 8)
  323. return INTEL_PT_NEED_MORE_BYTES;
  324. packet->type = INTEL_PT_TSC;
  325. memcpy_le64(&packet->payload, buf + 1, 7);
  326. return 8;
  327. }
  328. static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
  329. struct intel_pt_pkt *packet)
  330. {
  331. if (len < 2)
  332. return INTEL_PT_NEED_MORE_BYTES;
  333. packet->type = INTEL_PT_MTC;
  334. packet->payload = buf[1];
  335. return 2;
  336. }
  337. static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
  338. struct intel_pt_pkt *packet)
  339. {
  340. unsigned int byte;
  341. memset(packet, 0, sizeof(struct intel_pt_pkt));
  342. if (!len)
  343. return INTEL_PT_NEED_MORE_BYTES;
  344. byte = buf[0];
  345. if (!(byte & BIT(0))) {
  346. if (byte == 0)
  347. return intel_pt_get_pad(packet);
  348. if (byte == 2)
  349. return intel_pt_get_ext(buf, len, packet);
  350. return intel_pt_get_short_tnt(byte, packet);
  351. }
  352. if ((byte & 2))
  353. return intel_pt_get_cyc(byte, buf, len, packet);
  354. switch (byte & 0x1f) {
  355. case 0x0D:
  356. return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
  357. case 0x11:
  358. return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
  359. packet);
  360. case 0x01:
  361. return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
  362. packet);
  363. case 0x1D:
  364. return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
  365. case 0x19:
  366. switch (byte) {
  367. case 0x99:
  368. return intel_pt_get_mode(buf, len, packet);
  369. case 0x19:
  370. return intel_pt_get_tsc(buf, len, packet);
  371. case 0x59:
  372. return intel_pt_get_mtc(buf, len, packet);
  373. default:
  374. return INTEL_PT_BAD_PACKET;
  375. }
  376. default:
  377. return INTEL_PT_BAD_PACKET;
  378. }
  379. }
  380. int intel_pt_get_packet(const unsigned char *buf, size_t len,
  381. struct intel_pt_pkt *packet)
  382. {
  383. int ret;
  384. ret = intel_pt_do_get_packet(buf, len, packet);
  385. if (ret > 0) {
  386. while (ret < 8 && len > (size_t)ret && !buf[ret])
  387. ret += 1;
  388. }
  389. return ret;
  390. }
  391. int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
  392. size_t buf_len)
  393. {
  394. int ret, i, nr;
  395. unsigned long long payload = packet->payload;
  396. const char *name = intel_pt_pkt_name(packet->type);
  397. switch (packet->type) {
  398. case INTEL_PT_BAD:
  399. case INTEL_PT_PAD:
  400. case INTEL_PT_PSB:
  401. case INTEL_PT_PSBEND:
  402. case INTEL_PT_TRACESTOP:
  403. case INTEL_PT_OVF:
  404. return snprintf(buf, buf_len, "%s", name);
  405. case INTEL_PT_TNT: {
  406. size_t blen = buf_len;
  407. ret = snprintf(buf, blen, "%s ", name);
  408. if (ret < 0)
  409. return ret;
  410. buf += ret;
  411. blen -= ret;
  412. for (i = 0; i < packet->count; i++) {
  413. if (payload & BIT63)
  414. ret = snprintf(buf, blen, "T");
  415. else
  416. ret = snprintf(buf, blen, "N");
  417. if (ret < 0)
  418. return ret;
  419. buf += ret;
  420. blen -= ret;
  421. payload <<= 1;
  422. }
  423. ret = snprintf(buf, blen, " (%d)", packet->count);
  424. if (ret < 0)
  425. return ret;
  426. blen -= ret;
  427. return buf_len - blen;
  428. }
  429. case INTEL_PT_TIP_PGD:
  430. case INTEL_PT_TIP_PGE:
  431. case INTEL_PT_TIP:
  432. case INTEL_PT_FUP:
  433. if (!(packet->count))
  434. return snprintf(buf, buf_len, "%s no ip", name);
  435. case INTEL_PT_CYC:
  436. case INTEL_PT_VMCS:
  437. case INTEL_PT_MTC:
  438. case INTEL_PT_MNT:
  439. case INTEL_PT_CBR:
  440. case INTEL_PT_TSC:
  441. return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
  442. case INTEL_PT_TMA:
  443. return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
  444. (unsigned)payload, packet->count);
  445. case INTEL_PT_MODE_EXEC:
  446. return snprintf(buf, buf_len, "%s %lld", name, payload);
  447. case INTEL_PT_MODE_TSX:
  448. return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
  449. name, (unsigned)(payload >> 1) & 1,
  450. (unsigned)payload & 1);
  451. case INTEL_PT_PIP:
  452. nr = packet->payload & NR_FLAG ? 1 : 0;
  453. payload &= ~NR_FLAG;
  454. ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
  455. name, payload, nr);
  456. return ret;
  457. default:
  458. break;
  459. }
  460. return snprintf(buf, buf_len, "%s 0x%llx (%d)",
  461. name, payload, packet->count);
  462. }