intel-pt-insn-decoder.c 6.1 KB

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