insn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * x86 instruction analysis
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2002, 2004, 2009
  19. */
  20. #include <linux/string.h>
  21. #include <asm/inat.h>
  22. #include <asm/insn.h>
  23. #define get_next(t, insn) \
  24. ({t r; r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); r; })
  25. #define peek_next(t, insn) \
  26. ({t r; r = *(t*)insn->next_byte; r; })
  27. /**
  28. * insn_init() - initialize struct insn
  29. * @insn: &struct insn to be initialized
  30. * @kaddr: address (in kernel memory) of instruction (or copy thereof)
  31. * @x86_64: !0 for 64-bit kernel or 64-bit app
  32. */
  33. void insn_init(struct insn *insn, const void *kaddr, int x86_64)
  34. {
  35. memset(insn, 0, sizeof(*insn));
  36. insn->kaddr = kaddr;
  37. insn->next_byte = kaddr;
  38. insn->x86_64 = x86_64 ? 1 : 0;
  39. insn->opnd_bytes = 4;
  40. if (x86_64)
  41. insn->addr_bytes = 8;
  42. else
  43. insn->addr_bytes = 4;
  44. }
  45. /**
  46. * insn_get_prefixes - scan x86 instruction prefix bytes
  47. * @insn: &struct insn containing instruction
  48. *
  49. * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
  50. * to point to the (first) opcode. No effect if @insn->prefixes.got
  51. * is already set.
  52. */
  53. void insn_get_prefixes(struct insn *insn)
  54. {
  55. struct insn_field *prefixes = &insn->prefixes;
  56. insn_attr_t attr;
  57. insn_byte_t b, lb;
  58. int i, nb;
  59. if (prefixes->got)
  60. return;
  61. nb = 0;
  62. lb = 0;
  63. b = peek_next(insn_byte_t, insn);
  64. attr = inat_get_opcode_attribute(b);
  65. while (inat_is_prefix(attr)) {
  66. /* Skip if same prefix */
  67. for (i = 0; i < nb; i++)
  68. if (prefixes->bytes[i] == b)
  69. goto found;
  70. if (nb == 4)
  71. /* Invalid instruction */
  72. break;
  73. prefixes->bytes[nb++] = b;
  74. if (inat_is_address_size_prefix(attr)) {
  75. /* address size switches 2/4 or 4/8 */
  76. if (insn->x86_64)
  77. insn->addr_bytes ^= 12;
  78. else
  79. insn->addr_bytes ^= 6;
  80. } else if (inat_is_operand_size_prefix(attr)) {
  81. /* oprand size switches 2/4 */
  82. insn->opnd_bytes ^= 6;
  83. }
  84. found:
  85. prefixes->nbytes++;
  86. insn->next_byte++;
  87. lb = b;
  88. b = peek_next(insn_byte_t, insn);
  89. attr = inat_get_opcode_attribute(b);
  90. }
  91. /* Set the last prefix */
  92. if (lb && lb != insn->prefixes.bytes[3]) {
  93. if (unlikely(insn->prefixes.bytes[3])) {
  94. /* Swap the last prefix */
  95. b = insn->prefixes.bytes[3];
  96. for (i = 0; i < nb; i++)
  97. if (prefixes->bytes[i] == lb)
  98. prefixes->bytes[i] = b;
  99. }
  100. insn->prefixes.bytes[3] = lb;
  101. }
  102. if (insn->x86_64) {
  103. b = peek_next(insn_byte_t, insn);
  104. attr = inat_get_opcode_attribute(b);
  105. if (inat_is_rex_prefix(attr)) {
  106. insn->rex_prefix.value = b;
  107. insn->rex_prefix.nbytes = 1;
  108. insn->next_byte++;
  109. if (X86_REX_W(b))
  110. /* REX.W overrides opnd_size */
  111. insn->opnd_bytes = 8;
  112. }
  113. }
  114. insn->rex_prefix.got = 1;
  115. prefixes->got = 1;
  116. return;
  117. }
  118. /**
  119. * insn_get_opcode - collect opcode(s)
  120. * @insn: &struct insn containing instruction
  121. *
  122. * Populates @insn->opcode, updates @insn->next_byte to point past the
  123. * opcode byte(s), and set @insn->attr (except for groups).
  124. * If necessary, first collects any preceding (prefix) bytes.
  125. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
  126. * is already 1.
  127. */
  128. void insn_get_opcode(struct insn *insn)
  129. {
  130. struct insn_field *opcode = &insn->opcode;
  131. insn_byte_t op, pfx;
  132. if (opcode->got)
  133. return;
  134. if (!insn->prefixes.got)
  135. insn_get_prefixes(insn);
  136. /* Get first opcode */
  137. op = get_next(insn_byte_t, insn);
  138. opcode->bytes[0] = op;
  139. opcode->nbytes = 1;
  140. insn->attr = inat_get_opcode_attribute(op);
  141. while (inat_is_escape(insn->attr)) {
  142. /* Get escaped opcode */
  143. op = get_next(insn_byte_t, insn);
  144. opcode->bytes[opcode->nbytes++] = op;
  145. pfx = insn_last_prefix(insn);
  146. insn->attr = inat_get_escape_attribute(op, pfx, insn->attr);
  147. }
  148. opcode->got = 1;
  149. }
  150. /**
  151. * insn_get_modrm - collect ModRM byte, if any
  152. * @insn: &struct insn containing instruction
  153. *
  154. * Populates @insn->modrm and updates @insn->next_byte to point past the
  155. * ModRM byte, if any. If necessary, first collects the preceding bytes
  156. * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
  157. */
  158. void insn_get_modrm(struct insn *insn)
  159. {
  160. struct insn_field *modrm = &insn->modrm;
  161. insn_byte_t pfx, mod;
  162. if (modrm->got)
  163. return;
  164. if (!insn->opcode.got)
  165. insn_get_opcode(insn);
  166. if (inat_has_modrm(insn->attr)) {
  167. mod = get_next(insn_byte_t, insn);
  168. modrm->value = mod;
  169. modrm->nbytes = 1;
  170. if (inat_is_group(insn->attr)) {
  171. pfx = insn_last_prefix(insn);
  172. insn->attr = inat_get_group_attribute(mod, pfx,
  173. insn->attr);
  174. }
  175. }
  176. if (insn->x86_64 && inat_is_force64(insn->attr))
  177. insn->opnd_bytes = 8;
  178. modrm->got = 1;
  179. }
  180. /**
  181. * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
  182. * @insn: &struct insn containing instruction
  183. *
  184. * If necessary, first collects the instruction up to and including the
  185. * ModRM byte. No effect if @insn->x86_64 is 0.
  186. */
  187. int insn_rip_relative(struct insn *insn)
  188. {
  189. struct insn_field *modrm = &insn->modrm;
  190. if (!insn->x86_64)
  191. return 0;
  192. if (!modrm->got)
  193. insn_get_modrm(insn);
  194. /*
  195. * For rip-relative instructions, the mod field (top 2 bits)
  196. * is zero and the r/m field (bottom 3 bits) is 0x5.
  197. */
  198. return (modrm->nbytes && (modrm->value & 0xc7) == 0x5);
  199. }
  200. /**
  201. * insn_get_sib() - Get the SIB byte of instruction
  202. * @insn: &struct insn containing instruction
  203. *
  204. * If necessary, first collects the instruction up to and including the
  205. * ModRM byte.
  206. */
  207. void insn_get_sib(struct insn *insn)
  208. {
  209. insn_byte_t modrm;
  210. if (insn->sib.got)
  211. return;
  212. if (!insn->modrm.got)
  213. insn_get_modrm(insn);
  214. if (insn->modrm.nbytes) {
  215. modrm = (insn_byte_t)insn->modrm.value;
  216. if (insn->addr_bytes != 2 &&
  217. X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
  218. insn->sib.value = get_next(insn_byte_t, insn);
  219. insn->sib.nbytes = 1;
  220. }
  221. }
  222. insn->sib.got = 1;
  223. }
  224. /**
  225. * insn_get_displacement() - Get the displacement of instruction
  226. * @insn: &struct insn containing instruction
  227. *
  228. * If necessary, first collects the instruction up to and including the
  229. * SIB byte.
  230. * Displacement value is sign-expanded.
  231. */
  232. void insn_get_displacement(struct insn *insn)
  233. {
  234. insn_byte_t mod, rm, base;
  235. if (insn->displacement.got)
  236. return;
  237. if (!insn->sib.got)
  238. insn_get_sib(insn);
  239. if (insn->modrm.nbytes) {
  240. /*
  241. * Interpreting the modrm byte:
  242. * mod = 00 - no displacement fields (exceptions below)
  243. * mod = 01 - 1-byte displacement field
  244. * mod = 10 - displacement field is 4 bytes, or 2 bytes if
  245. * address size = 2 (0x67 prefix in 32-bit mode)
  246. * mod = 11 - no memory operand
  247. *
  248. * If address size = 2...
  249. * mod = 00, r/m = 110 - displacement field is 2 bytes
  250. *
  251. * If address size != 2...
  252. * mod != 11, r/m = 100 - SIB byte exists
  253. * mod = 00, SIB base = 101 - displacement field is 4 bytes
  254. * mod = 00, r/m = 101 - rip-relative addressing, displacement
  255. * field is 4 bytes
  256. */
  257. mod = X86_MODRM_MOD(insn->modrm.value);
  258. rm = X86_MODRM_RM(insn->modrm.value);
  259. base = X86_SIB_BASE(insn->sib.value);
  260. if (mod == 3)
  261. goto out;
  262. if (mod == 1) {
  263. insn->displacement.value = get_next(char, insn);
  264. insn->displacement.nbytes = 1;
  265. } else if (insn->addr_bytes == 2) {
  266. if ((mod == 0 && rm == 6) || mod == 2) {
  267. insn->displacement.value =
  268. get_next(short, insn);
  269. insn->displacement.nbytes = 2;
  270. }
  271. } else {
  272. if ((mod == 0 && rm == 5) || mod == 2 ||
  273. (mod == 0 && base == 5)) {
  274. insn->displacement.value = get_next(int, insn);
  275. insn->displacement.nbytes = 4;
  276. }
  277. }
  278. }
  279. out:
  280. insn->displacement.got = 1;
  281. }
  282. /* Decode moffset16/32/64 */
  283. static void __get_moffset(struct insn *insn)
  284. {
  285. switch (insn->addr_bytes) {
  286. case 2:
  287. insn->moffset1.value = get_next(short, insn);
  288. insn->moffset1.nbytes = 2;
  289. break;
  290. case 4:
  291. insn->moffset1.value = get_next(int, insn);
  292. insn->moffset1.nbytes = 4;
  293. break;
  294. case 8:
  295. insn->moffset1.value = get_next(int, insn);
  296. insn->moffset1.nbytes = 4;
  297. insn->moffset2.value = get_next(int, insn);
  298. insn->moffset2.nbytes = 4;
  299. break;
  300. }
  301. insn->moffset1.got = insn->moffset2.got = 1;
  302. }
  303. /* Decode imm v32(Iz) */
  304. static void __get_immv32(struct insn *insn)
  305. {
  306. switch (insn->opnd_bytes) {
  307. case 2:
  308. insn->immediate.value = get_next(short, insn);
  309. insn->immediate.nbytes = 2;
  310. break;
  311. case 4:
  312. case 8:
  313. insn->immediate.value = get_next(int, insn);
  314. insn->immediate.nbytes = 4;
  315. break;
  316. }
  317. }
  318. /* Decode imm v64(Iv/Ov) */
  319. static void __get_immv(struct insn *insn)
  320. {
  321. switch (insn->opnd_bytes) {
  322. case 2:
  323. insn->immediate1.value = get_next(short, insn);
  324. insn->immediate1.nbytes = 2;
  325. break;
  326. case 4:
  327. insn->immediate1.value = get_next(int, insn);
  328. insn->immediate1.nbytes = 4;
  329. break;
  330. case 8:
  331. insn->immediate1.value = get_next(int, insn);
  332. insn->immediate1.nbytes = 4;
  333. insn->immediate2.value = get_next(int, insn);
  334. insn->immediate2.nbytes = 4;
  335. break;
  336. }
  337. insn->immediate1.got = insn->immediate2.got = 1;
  338. }
  339. /* Decode ptr16:16/32(Ap) */
  340. static void __get_immptr(struct insn *insn)
  341. {
  342. switch (insn->opnd_bytes) {
  343. case 2:
  344. insn->immediate1.value = get_next(short, insn);
  345. insn->immediate1.nbytes = 2;
  346. break;
  347. case 4:
  348. insn->immediate1.value = get_next(int, insn);
  349. insn->immediate1.nbytes = 4;
  350. break;
  351. case 8:
  352. /* ptr16:64 is not exist (no segment) */
  353. return;
  354. }
  355. insn->immediate2.value = get_next(unsigned short, insn);
  356. insn->immediate2.nbytes = 2;
  357. insn->immediate1.got = insn->immediate2.got = 1;
  358. }
  359. /**
  360. * insn_get_immediate() - Get the immediates of instruction
  361. * @insn: &struct insn containing instruction
  362. *
  363. * If necessary, first collects the instruction up to and including the
  364. * displacement bytes.
  365. * Basically, most of immediates are sign-expanded. Unsigned-value can be
  366. * get by bit masking with ((1 << (nbytes * 8)) - 1)
  367. */
  368. void insn_get_immediate(struct insn *insn)
  369. {
  370. if (insn->immediate.got)
  371. return;
  372. if (!insn->displacement.got)
  373. insn_get_displacement(insn);
  374. if (inat_has_moffset(insn->attr)) {
  375. __get_moffset(insn);
  376. goto done;
  377. }
  378. if (!inat_has_immediate(insn->attr))
  379. /* no immediates */
  380. goto done;
  381. switch (inat_immediate_size(insn->attr)) {
  382. case INAT_IMM_BYTE:
  383. insn->immediate.value = get_next(char, insn);
  384. insn->immediate.nbytes = 1;
  385. break;
  386. case INAT_IMM_WORD:
  387. insn->immediate.value = get_next(short, insn);
  388. insn->immediate.nbytes = 2;
  389. break;
  390. case INAT_IMM_DWORD:
  391. insn->immediate.value = get_next(int, insn);
  392. insn->immediate.nbytes = 4;
  393. break;
  394. case INAT_IMM_QWORD:
  395. insn->immediate1.value = get_next(int, insn);
  396. insn->immediate1.nbytes = 4;
  397. insn->immediate2.value = get_next(int, insn);
  398. insn->immediate2.nbytes = 4;
  399. break;
  400. case INAT_IMM_PTR:
  401. __get_immptr(insn);
  402. break;
  403. case INAT_IMM_VWORD32:
  404. __get_immv32(insn);
  405. break;
  406. case INAT_IMM_VWORD:
  407. __get_immv(insn);
  408. break;
  409. default:
  410. break;
  411. }
  412. if (inat_has_second_immediate(insn->attr)) {
  413. insn->immediate2.value = get_next(char, insn);
  414. insn->immediate2.nbytes = 1;
  415. }
  416. done:
  417. insn->immediate.got = 1;
  418. }
  419. /**
  420. * insn_get_length() - Get the length of instruction
  421. * @insn: &struct insn containing instruction
  422. *
  423. * If necessary, first collects the instruction up to and including the
  424. * immediates bytes.
  425. */
  426. void insn_get_length(struct insn *insn)
  427. {
  428. if (insn->length)
  429. return;
  430. if (!insn->immediate.got)
  431. insn_get_immediate(insn);
  432. insn->length = (unsigned char)((unsigned long)insn->next_byte
  433. - (unsigned long)insn->kaddr);
  434. }