intel-pt-insn-decoder.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * intel_pt_insn_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 "event.h"
  20. #include "insn.h"
  21. #include "inat.c"
  22. #include "insn.c"
  23. #include "intel-pt-insn-decoder.h"
  24. #include "dump-insn.h"
  25. #if INTEL_PT_INSN_BUF_SZ < MAX_INSN_SIZE || INTEL_PT_INSN_BUF_SZ > MAX_INSN
  26. #error Instruction buffer size too small
  27. #endif
  28. /* Based on branch_type() from arch/x86/events/intel/lbr.c */
  29. static void intel_pt_insn_decoder(struct insn *insn,
  30. struct intel_pt_insn *intel_pt_insn)
  31. {
  32. enum intel_pt_insn_op op = INTEL_PT_OP_OTHER;
  33. enum intel_pt_insn_branch branch = INTEL_PT_BR_NO_BRANCH;
  34. int ext;
  35. intel_pt_insn->rel = 0;
  36. if (insn_is_avx(insn)) {
  37. intel_pt_insn->op = INTEL_PT_OP_OTHER;
  38. intel_pt_insn->branch = INTEL_PT_BR_NO_BRANCH;
  39. intel_pt_insn->length = insn->length;
  40. return;
  41. }
  42. switch (insn->opcode.bytes[0]) {
  43. case 0xf:
  44. switch (insn->opcode.bytes[1]) {
  45. case 0x05: /* syscall */
  46. case 0x34: /* sysenter */
  47. op = INTEL_PT_OP_SYSCALL;
  48. branch = INTEL_PT_BR_INDIRECT;
  49. break;
  50. case 0x07: /* sysret */
  51. case 0x35: /* sysexit */
  52. op = INTEL_PT_OP_SYSRET;
  53. branch = INTEL_PT_BR_INDIRECT;
  54. break;
  55. case 0x80 ... 0x8f: /* jcc */
  56. op = INTEL_PT_OP_JCC;
  57. branch = INTEL_PT_BR_CONDITIONAL;
  58. break;
  59. default:
  60. break;
  61. }
  62. break;
  63. case 0x70 ... 0x7f: /* jcc */
  64. op = INTEL_PT_OP_JCC;
  65. branch = INTEL_PT_BR_CONDITIONAL;
  66. break;
  67. case 0xc2: /* near ret */
  68. case 0xc3: /* near ret */
  69. case 0xca: /* far ret */
  70. case 0xcb: /* far ret */
  71. op = INTEL_PT_OP_RET;
  72. branch = INTEL_PT_BR_INDIRECT;
  73. break;
  74. case 0xcf: /* iret */
  75. op = INTEL_PT_OP_IRET;
  76. branch = INTEL_PT_BR_INDIRECT;
  77. break;
  78. case 0xcc ... 0xce: /* int */
  79. op = INTEL_PT_OP_INT;
  80. branch = INTEL_PT_BR_INDIRECT;
  81. break;
  82. case 0xe8: /* call near rel */
  83. op = INTEL_PT_OP_CALL;
  84. branch = INTEL_PT_BR_UNCONDITIONAL;
  85. break;
  86. case 0x9a: /* call far absolute */
  87. op = INTEL_PT_OP_CALL;
  88. branch = INTEL_PT_BR_INDIRECT;
  89. break;
  90. case 0xe0 ... 0xe2: /* loop */
  91. op = INTEL_PT_OP_LOOP;
  92. branch = INTEL_PT_BR_CONDITIONAL;
  93. break;
  94. case 0xe3: /* jcc */
  95. op = INTEL_PT_OP_JCC;
  96. branch = INTEL_PT_BR_CONDITIONAL;
  97. break;
  98. case 0xe9: /* jmp */
  99. case 0xeb: /* jmp */
  100. op = INTEL_PT_OP_JMP;
  101. branch = INTEL_PT_BR_UNCONDITIONAL;
  102. break;
  103. case 0xea: /* far jmp */
  104. op = INTEL_PT_OP_JMP;
  105. branch = INTEL_PT_BR_INDIRECT;
  106. break;
  107. case 0xff: /* call near absolute, call far absolute ind */
  108. ext = (insn->modrm.bytes[0] >> 3) & 0x7;
  109. switch (ext) {
  110. case 2: /* near ind call */
  111. case 3: /* far ind call */
  112. op = INTEL_PT_OP_CALL;
  113. branch = INTEL_PT_BR_INDIRECT;
  114. break;
  115. case 4:
  116. case 5:
  117. op = INTEL_PT_OP_JMP;
  118. branch = INTEL_PT_BR_INDIRECT;
  119. break;
  120. default:
  121. break;
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. intel_pt_insn->op = op;
  128. intel_pt_insn->branch = branch;
  129. intel_pt_insn->length = insn->length;
  130. if (branch == INTEL_PT_BR_CONDITIONAL ||
  131. branch == INTEL_PT_BR_UNCONDITIONAL) {
  132. #if __BYTE_ORDER == __BIG_ENDIAN
  133. switch (insn->immediate.nbytes) {
  134. case 1:
  135. intel_pt_insn->rel = insn->immediate.value;
  136. break;
  137. case 2:
  138. intel_pt_insn->rel =
  139. bswap_16((short)insn->immediate.value);
  140. break;
  141. case 4:
  142. intel_pt_insn->rel = bswap_32(insn->immediate.value);
  143. break;
  144. default:
  145. intel_pt_insn->rel = 0;
  146. break;
  147. }
  148. #else
  149. intel_pt_insn->rel = insn->immediate.value;
  150. #endif
  151. }
  152. }
  153. int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
  154. struct intel_pt_insn *intel_pt_insn)
  155. {
  156. struct insn insn;
  157. insn_init(&insn, buf, len, x86_64);
  158. insn_get_length(&insn);
  159. if (!insn_complete(&insn) || insn.length > len)
  160. return -1;
  161. intel_pt_insn_decoder(&insn, intel_pt_insn);
  162. if (insn.length < INTEL_PT_INSN_BUF_SZ)
  163. memcpy(intel_pt_insn->buf, buf, insn.length);
  164. else
  165. memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
  166. return 0;
  167. }
  168. const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
  169. u8 *inbuf, int inlen, int *lenp)
  170. {
  171. struct insn insn;
  172. int n, i;
  173. int left;
  174. insn_init(&insn, inbuf, inlen, x->is64bit);
  175. insn_get_length(&insn);
  176. if (!insn_complete(&insn) || insn.length > inlen)
  177. return "<bad>";
  178. if (lenp)
  179. *lenp = insn.length;
  180. left = sizeof(x->out);
  181. n = snprintf(x->out, left, "insn: ");
  182. left -= n;
  183. for (i = 0; i < insn.length; i++) {
  184. n += snprintf(x->out + n, left, "%02x ", inbuf[i]);
  185. left -= n;
  186. }
  187. return x->out;
  188. }
  189. const char *branch_name[] = {
  190. [INTEL_PT_OP_OTHER] = "Other",
  191. [INTEL_PT_OP_CALL] = "Call",
  192. [INTEL_PT_OP_RET] = "Ret",
  193. [INTEL_PT_OP_JCC] = "Jcc",
  194. [INTEL_PT_OP_JMP] = "Jmp",
  195. [INTEL_PT_OP_LOOP] = "Loop",
  196. [INTEL_PT_OP_IRET] = "IRet",
  197. [INTEL_PT_OP_INT] = "Int",
  198. [INTEL_PT_OP_SYSCALL] = "Syscall",
  199. [INTEL_PT_OP_SYSRET] = "Sysret",
  200. };
  201. const char *intel_pt_insn_name(enum intel_pt_insn_op op)
  202. {
  203. return branch_name[op];
  204. }
  205. int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
  206. size_t buf_len)
  207. {
  208. switch (intel_pt_insn->branch) {
  209. case INTEL_PT_BR_CONDITIONAL:
  210. case INTEL_PT_BR_UNCONDITIONAL:
  211. return snprintf(buf, buf_len, "%s %s%d",
  212. intel_pt_insn_name(intel_pt_insn->op),
  213. intel_pt_insn->rel > 0 ? "+" : "",
  214. intel_pt_insn->rel);
  215. case INTEL_PT_BR_NO_BRANCH:
  216. case INTEL_PT_BR_INDIRECT:
  217. return snprintf(buf, buf_len, "%s",
  218. intel_pt_insn_name(intel_pt_insn->op));
  219. default:
  220. break;
  221. }
  222. return 0;
  223. }
  224. int intel_pt_insn_type(enum intel_pt_insn_op op)
  225. {
  226. switch (op) {
  227. case INTEL_PT_OP_OTHER:
  228. return 0;
  229. case INTEL_PT_OP_CALL:
  230. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
  231. case INTEL_PT_OP_RET:
  232. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
  233. case INTEL_PT_OP_JCC:
  234. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
  235. case INTEL_PT_OP_JMP:
  236. return PERF_IP_FLAG_BRANCH;
  237. case INTEL_PT_OP_LOOP:
  238. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
  239. case INTEL_PT_OP_IRET:
  240. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
  241. PERF_IP_FLAG_INTERRUPT;
  242. case INTEL_PT_OP_INT:
  243. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
  244. PERF_IP_FLAG_INTERRUPT;
  245. case INTEL_PT_OP_SYSCALL:
  246. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
  247. PERF_IP_FLAG_SYSCALLRET;
  248. case INTEL_PT_OP_SYSRET:
  249. return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
  250. PERF_IP_FLAG_SYSCALLRET;
  251. default:
  252. return 0;
  253. }
  254. }