intel-pt-insn-decoder.c 6.0 KB

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