intel-pt-pkt-decoder.c 12 KB

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