intel-pt-pkt-decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. switch (byte >> 5) {
  250. case 0:
  251. packet->count = 0;
  252. break;
  253. case 1:
  254. if (len < 3)
  255. return INTEL_PT_NEED_MORE_BYTES;
  256. packet->count = 2;
  257. packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
  258. break;
  259. case 2:
  260. if (len < 5)
  261. return INTEL_PT_NEED_MORE_BYTES;
  262. packet->count = 4;
  263. packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
  264. break;
  265. case 3:
  266. case 6:
  267. if (len < 7)
  268. return INTEL_PT_NEED_MORE_BYTES;
  269. packet->count = 6;
  270. memcpy_le64(&packet->payload, buf + 1, 6);
  271. break;
  272. default:
  273. return INTEL_PT_BAD_PACKET;
  274. }
  275. packet->type = type;
  276. return packet->count + 1;
  277. }
  278. static int intel_pt_get_mode(const unsigned char *buf, size_t len,
  279. struct intel_pt_pkt *packet)
  280. {
  281. if (len < 2)
  282. return INTEL_PT_NEED_MORE_BYTES;
  283. switch (buf[1] >> 5) {
  284. case 0:
  285. packet->type = INTEL_PT_MODE_EXEC;
  286. switch (buf[1] & 3) {
  287. case 0:
  288. packet->payload = 16;
  289. break;
  290. case 1:
  291. packet->payload = 64;
  292. break;
  293. case 2:
  294. packet->payload = 32;
  295. break;
  296. default:
  297. return INTEL_PT_BAD_PACKET;
  298. }
  299. break;
  300. case 1:
  301. packet->type = INTEL_PT_MODE_TSX;
  302. if ((buf[1] & 3) == 3)
  303. return INTEL_PT_BAD_PACKET;
  304. packet->payload = buf[1] & 3;
  305. break;
  306. default:
  307. return INTEL_PT_BAD_PACKET;
  308. }
  309. return 2;
  310. }
  311. static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
  312. struct intel_pt_pkt *packet)
  313. {
  314. if (len < 8)
  315. return INTEL_PT_NEED_MORE_BYTES;
  316. packet->type = INTEL_PT_TSC;
  317. memcpy_le64(&packet->payload, buf + 1, 7);
  318. return 8;
  319. }
  320. static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
  321. struct intel_pt_pkt *packet)
  322. {
  323. if (len < 2)
  324. return INTEL_PT_NEED_MORE_BYTES;
  325. packet->type = INTEL_PT_MTC;
  326. packet->payload = buf[1];
  327. return 2;
  328. }
  329. static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
  330. struct intel_pt_pkt *packet)
  331. {
  332. unsigned int byte;
  333. memset(packet, 0, sizeof(struct intel_pt_pkt));
  334. if (!len)
  335. return INTEL_PT_NEED_MORE_BYTES;
  336. byte = buf[0];
  337. if (!(byte & BIT(0))) {
  338. if (byte == 0)
  339. return intel_pt_get_pad(packet);
  340. if (byte == 2)
  341. return intel_pt_get_ext(buf, len, packet);
  342. return intel_pt_get_short_tnt(byte, packet);
  343. }
  344. if ((byte & 2))
  345. return intel_pt_get_cyc(byte, buf, len, packet);
  346. switch (byte & 0x1f) {
  347. case 0x0D:
  348. return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
  349. case 0x11:
  350. return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
  351. packet);
  352. case 0x01:
  353. return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
  354. packet);
  355. case 0x1D:
  356. return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
  357. case 0x19:
  358. switch (byte) {
  359. case 0x99:
  360. return intel_pt_get_mode(buf, len, packet);
  361. case 0x19:
  362. return intel_pt_get_tsc(buf, len, packet);
  363. case 0x59:
  364. return intel_pt_get_mtc(buf, len, packet);
  365. default:
  366. return INTEL_PT_BAD_PACKET;
  367. }
  368. default:
  369. return INTEL_PT_BAD_PACKET;
  370. }
  371. }
  372. int intel_pt_get_packet(const unsigned char *buf, size_t len,
  373. struct intel_pt_pkt *packet)
  374. {
  375. int ret;
  376. ret = intel_pt_do_get_packet(buf, len, packet);
  377. if (ret > 0) {
  378. while (ret < 8 && len > (size_t)ret && !buf[ret])
  379. ret += 1;
  380. }
  381. return ret;
  382. }
  383. int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
  384. size_t buf_len)
  385. {
  386. int ret, i, nr;
  387. unsigned long long payload = packet->payload;
  388. const char *name = intel_pt_pkt_name(packet->type);
  389. switch (packet->type) {
  390. case INTEL_PT_BAD:
  391. case INTEL_PT_PAD:
  392. case INTEL_PT_PSB:
  393. case INTEL_PT_PSBEND:
  394. case INTEL_PT_TRACESTOP:
  395. case INTEL_PT_OVF:
  396. return snprintf(buf, buf_len, "%s", name);
  397. case INTEL_PT_TNT: {
  398. size_t blen = buf_len;
  399. ret = snprintf(buf, blen, "%s ", name);
  400. if (ret < 0)
  401. return ret;
  402. buf += ret;
  403. blen -= ret;
  404. for (i = 0; i < packet->count; i++) {
  405. if (payload & BIT63)
  406. ret = snprintf(buf, blen, "T");
  407. else
  408. ret = snprintf(buf, blen, "N");
  409. if (ret < 0)
  410. return ret;
  411. buf += ret;
  412. blen -= ret;
  413. payload <<= 1;
  414. }
  415. ret = snprintf(buf, blen, " (%d)", packet->count);
  416. if (ret < 0)
  417. return ret;
  418. blen -= ret;
  419. return buf_len - blen;
  420. }
  421. case INTEL_PT_TIP_PGD:
  422. case INTEL_PT_TIP_PGE:
  423. case INTEL_PT_TIP:
  424. case INTEL_PT_FUP:
  425. if (!(packet->count))
  426. return snprintf(buf, buf_len, "%s no ip", name);
  427. case INTEL_PT_CYC:
  428. case INTEL_PT_VMCS:
  429. case INTEL_PT_MTC:
  430. case INTEL_PT_MNT:
  431. case INTEL_PT_CBR:
  432. case INTEL_PT_TSC:
  433. return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
  434. case INTEL_PT_TMA:
  435. return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
  436. (unsigned)payload, packet->count);
  437. case INTEL_PT_MODE_EXEC:
  438. return snprintf(buf, buf_len, "%s %lld", name, payload);
  439. case INTEL_PT_MODE_TSX:
  440. return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
  441. name, (unsigned)(payload >> 1) & 1,
  442. (unsigned)payload & 1);
  443. case INTEL_PT_PIP:
  444. nr = packet->payload & NR_FLAG ? 1 : 0;
  445. payload &= ~NR_FLAG;
  446. ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
  447. name, payload, nr);
  448. return ret;
  449. default:
  450. break;
  451. }
  452. return snprintf(buf, buf_len, "%s 0x%llx (%d)",
  453. name, payload, packet->count);
  454. }