insn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. #ifdef __KERNEL__
  21. #include <linux/string.h>
  22. #else
  23. #include <string.h>
  24. #endif
  25. #include <asm/inat.h>
  26. #include <asm/insn.h>
  27. /* Verify next sizeof(t) bytes can be on the same instruction */
  28. #define validate_next(t, insn, n) \
  29. ((insn)->next_byte + sizeof(t) + n <= (insn)->end_kaddr)
  30. #define __get_next(t, insn) \
  31. ({ t r = *(t*)insn->next_byte; insn->next_byte += sizeof(t); r; })
  32. #define __peek_nbyte_next(t, insn, n) \
  33. ({ t r = *(t*)((insn)->next_byte + n); r; })
  34. #define get_next(t, insn) \
  35. ({ if (unlikely(!validate_next(t, insn, 0))) goto err_out; __get_next(t, insn); })
  36. #define peek_nbyte_next(t, insn, n) \
  37. ({ if (unlikely(!validate_next(t, insn, n))) goto err_out; __peek_nbyte_next(t, insn, n); })
  38. #define peek_next(t, insn) peek_nbyte_next(t, insn, 0)
  39. /**
  40. * insn_init() - initialize struct insn
  41. * @insn: &struct insn to be initialized
  42. * @kaddr: address (in kernel memory) of instruction (or copy thereof)
  43. * @x86_64: !0 for 64-bit kernel or 64-bit app
  44. */
  45. void insn_init(struct insn *insn, const void *kaddr, int buf_len, int x86_64)
  46. {
  47. /*
  48. * Instructions longer than MAX_INSN_SIZE (15 bytes) are invalid
  49. * even if the input buffer is long enough to hold them.
  50. */
  51. if (buf_len > MAX_INSN_SIZE)
  52. buf_len = MAX_INSN_SIZE;
  53. memset(insn, 0, sizeof(*insn));
  54. insn->kaddr = kaddr;
  55. insn->end_kaddr = kaddr + buf_len;
  56. insn->next_byte = kaddr;
  57. insn->x86_64 = x86_64 ? 1 : 0;
  58. insn->opnd_bytes = 4;
  59. if (x86_64)
  60. insn->addr_bytes = 8;
  61. else
  62. insn->addr_bytes = 4;
  63. }
  64. /**
  65. * insn_get_prefixes - scan x86 instruction prefix bytes
  66. * @insn: &struct insn containing instruction
  67. *
  68. * Populates the @insn->prefixes bitmap, and updates @insn->next_byte
  69. * to point to the (first) opcode. No effect if @insn->prefixes.got
  70. * is already set.
  71. */
  72. void insn_get_prefixes(struct insn *insn)
  73. {
  74. struct insn_field *prefixes = &insn->prefixes;
  75. insn_attr_t attr;
  76. insn_byte_t b, lb;
  77. int i, nb;
  78. if (prefixes->got)
  79. return;
  80. nb = 0;
  81. lb = 0;
  82. b = peek_next(insn_byte_t, insn);
  83. attr = inat_get_opcode_attribute(b);
  84. while (inat_is_legacy_prefix(attr)) {
  85. /* Skip if same prefix */
  86. for (i = 0; i < nb; i++)
  87. if (prefixes->bytes[i] == b)
  88. goto found;
  89. if (nb == 4)
  90. /* Invalid instruction */
  91. break;
  92. prefixes->bytes[nb++] = b;
  93. if (inat_is_address_size_prefix(attr)) {
  94. /* address size switches 2/4 or 4/8 */
  95. if (insn->x86_64)
  96. insn->addr_bytes ^= 12;
  97. else
  98. insn->addr_bytes ^= 6;
  99. } else if (inat_is_operand_size_prefix(attr)) {
  100. /* oprand size switches 2/4 */
  101. insn->opnd_bytes ^= 6;
  102. }
  103. found:
  104. prefixes->nbytes++;
  105. insn->next_byte++;
  106. lb = b;
  107. b = peek_next(insn_byte_t, insn);
  108. attr = inat_get_opcode_attribute(b);
  109. }
  110. /* Set the last prefix */
  111. if (lb && lb != insn->prefixes.bytes[3]) {
  112. if (unlikely(insn->prefixes.bytes[3])) {
  113. /* Swap the last prefix */
  114. b = insn->prefixes.bytes[3];
  115. for (i = 0; i < nb; i++)
  116. if (prefixes->bytes[i] == lb)
  117. prefixes->bytes[i] = b;
  118. }
  119. insn->prefixes.bytes[3] = lb;
  120. }
  121. /* Decode REX prefix */
  122. if (insn->x86_64) {
  123. b = peek_next(insn_byte_t, insn);
  124. attr = inat_get_opcode_attribute(b);
  125. if (inat_is_rex_prefix(attr)) {
  126. insn->rex_prefix.value = b;
  127. insn->rex_prefix.nbytes = 1;
  128. insn->next_byte++;
  129. if (X86_REX_W(b))
  130. /* REX.W overrides opnd_size */
  131. insn->opnd_bytes = 8;
  132. }
  133. }
  134. insn->rex_prefix.got = 1;
  135. /* Decode VEX prefix */
  136. b = peek_next(insn_byte_t, insn);
  137. attr = inat_get_opcode_attribute(b);
  138. if (inat_is_vex_prefix(attr)) {
  139. insn_byte_t b2 = peek_nbyte_next(insn_byte_t, insn, 1);
  140. if (!insn->x86_64) {
  141. /*
  142. * In 32-bits mode, if the [7:6] bits (mod bits of
  143. * ModRM) on the second byte are not 11b, it is
  144. * LDS or LES or BOUND.
  145. */
  146. if (X86_MODRM_MOD(b2) != 3)
  147. goto vex_end;
  148. }
  149. insn->vex_prefix.bytes[0] = b;
  150. insn->vex_prefix.bytes[1] = b2;
  151. if (inat_is_evex_prefix(attr)) {
  152. b2 = peek_nbyte_next(insn_byte_t, insn, 2);
  153. insn->vex_prefix.bytes[2] = b2;
  154. b2 = peek_nbyte_next(insn_byte_t, insn, 3);
  155. insn->vex_prefix.bytes[3] = b2;
  156. insn->vex_prefix.nbytes = 4;
  157. insn->next_byte += 4;
  158. if (insn->x86_64 && X86_VEX_W(b2))
  159. /* VEX.W overrides opnd_size */
  160. insn->opnd_bytes = 8;
  161. } else if (inat_is_vex3_prefix(attr)) {
  162. b2 = peek_nbyte_next(insn_byte_t, insn, 2);
  163. insn->vex_prefix.bytes[2] = b2;
  164. insn->vex_prefix.nbytes = 3;
  165. insn->next_byte += 3;
  166. if (insn->x86_64 && X86_VEX_W(b2))
  167. /* VEX.W overrides opnd_size */
  168. insn->opnd_bytes = 8;
  169. } else {
  170. /*
  171. * For VEX2, fake VEX3-like byte#2.
  172. * Makes it easier to decode vex.W, vex.vvvv,
  173. * vex.L and vex.pp. Masking with 0x7f sets vex.W == 0.
  174. */
  175. insn->vex_prefix.bytes[2] = b2 & 0x7f;
  176. insn->vex_prefix.nbytes = 2;
  177. insn->next_byte += 2;
  178. }
  179. }
  180. vex_end:
  181. insn->vex_prefix.got = 1;
  182. prefixes->got = 1;
  183. err_out:
  184. return;
  185. }
  186. /**
  187. * insn_get_opcode - collect opcode(s)
  188. * @insn: &struct insn containing instruction
  189. *
  190. * Populates @insn->opcode, updates @insn->next_byte to point past the
  191. * opcode byte(s), and set @insn->attr (except for groups).
  192. * If necessary, first collects any preceding (prefix) bytes.
  193. * Sets @insn->opcode.value = opcode1. No effect if @insn->opcode.got
  194. * is already 1.
  195. */
  196. void insn_get_opcode(struct insn *insn)
  197. {
  198. struct insn_field *opcode = &insn->opcode;
  199. insn_byte_t op;
  200. int pfx_id;
  201. if (opcode->got)
  202. return;
  203. if (!insn->prefixes.got)
  204. insn_get_prefixes(insn);
  205. /* Get first opcode */
  206. op = get_next(insn_byte_t, insn);
  207. opcode->bytes[0] = op;
  208. opcode->nbytes = 1;
  209. /* Check if there is VEX prefix or not */
  210. if (insn_is_avx(insn)) {
  211. insn_byte_t m, p;
  212. m = insn_vex_m_bits(insn);
  213. p = insn_vex_p_bits(insn);
  214. insn->attr = inat_get_avx_attribute(op, m, p);
  215. if ((inat_must_evex(insn->attr) && !insn_is_evex(insn)) ||
  216. (!inat_accept_vex(insn->attr) &&
  217. !inat_is_group(insn->attr)))
  218. insn->attr = 0; /* This instruction is bad */
  219. goto end; /* VEX has only 1 byte for opcode */
  220. }
  221. insn->attr = inat_get_opcode_attribute(op);
  222. while (inat_is_escape(insn->attr)) {
  223. /* Get escaped opcode */
  224. op = get_next(insn_byte_t, insn);
  225. opcode->bytes[opcode->nbytes++] = op;
  226. pfx_id = insn_last_prefix_id(insn);
  227. insn->attr = inat_get_escape_attribute(op, pfx_id, insn->attr);
  228. }
  229. if (inat_must_vex(insn->attr))
  230. insn->attr = 0; /* This instruction is bad */
  231. end:
  232. opcode->got = 1;
  233. err_out:
  234. return;
  235. }
  236. /**
  237. * insn_get_modrm - collect ModRM byte, if any
  238. * @insn: &struct insn containing instruction
  239. *
  240. * Populates @insn->modrm and updates @insn->next_byte to point past the
  241. * ModRM byte, if any. If necessary, first collects the preceding bytes
  242. * (prefixes and opcode(s)). No effect if @insn->modrm.got is already 1.
  243. */
  244. void insn_get_modrm(struct insn *insn)
  245. {
  246. struct insn_field *modrm = &insn->modrm;
  247. insn_byte_t pfx_id, mod;
  248. if (modrm->got)
  249. return;
  250. if (!insn->opcode.got)
  251. insn_get_opcode(insn);
  252. if (inat_has_modrm(insn->attr)) {
  253. mod = get_next(insn_byte_t, insn);
  254. modrm->value = mod;
  255. modrm->nbytes = 1;
  256. if (inat_is_group(insn->attr)) {
  257. pfx_id = insn_last_prefix_id(insn);
  258. insn->attr = inat_get_group_attribute(mod, pfx_id,
  259. insn->attr);
  260. if (insn_is_avx(insn) && !inat_accept_vex(insn->attr))
  261. insn->attr = 0; /* This is bad */
  262. }
  263. }
  264. if (insn->x86_64 && inat_is_force64(insn->attr))
  265. insn->opnd_bytes = 8;
  266. modrm->got = 1;
  267. err_out:
  268. return;
  269. }
  270. /**
  271. * insn_rip_relative() - Does instruction use RIP-relative addressing mode?
  272. * @insn: &struct insn containing instruction
  273. *
  274. * If necessary, first collects the instruction up to and including the
  275. * ModRM byte. No effect if @insn->x86_64 is 0.
  276. */
  277. int insn_rip_relative(struct insn *insn)
  278. {
  279. struct insn_field *modrm = &insn->modrm;
  280. if (!insn->x86_64)
  281. return 0;
  282. if (!modrm->got)
  283. insn_get_modrm(insn);
  284. /*
  285. * For rip-relative instructions, the mod field (top 2 bits)
  286. * is zero and the r/m field (bottom 3 bits) is 0x5.
  287. */
  288. return (modrm->nbytes && (modrm->value & 0xc7) == 0x5);
  289. }
  290. /**
  291. * insn_get_sib() - Get the SIB byte of instruction
  292. * @insn: &struct insn containing instruction
  293. *
  294. * If necessary, first collects the instruction up to and including the
  295. * ModRM byte.
  296. */
  297. void insn_get_sib(struct insn *insn)
  298. {
  299. insn_byte_t modrm;
  300. if (insn->sib.got)
  301. return;
  302. if (!insn->modrm.got)
  303. insn_get_modrm(insn);
  304. if (insn->modrm.nbytes) {
  305. modrm = (insn_byte_t)insn->modrm.value;
  306. if (insn->addr_bytes != 2 &&
  307. X86_MODRM_MOD(modrm) != 3 && X86_MODRM_RM(modrm) == 4) {
  308. insn->sib.value = get_next(insn_byte_t, insn);
  309. insn->sib.nbytes = 1;
  310. }
  311. }
  312. insn->sib.got = 1;
  313. err_out:
  314. return;
  315. }
  316. /**
  317. * insn_get_displacement() - Get the displacement of instruction
  318. * @insn: &struct insn containing instruction
  319. *
  320. * If necessary, first collects the instruction up to and including the
  321. * SIB byte.
  322. * Displacement value is sign-expanded.
  323. */
  324. void insn_get_displacement(struct insn *insn)
  325. {
  326. insn_byte_t mod, rm, base;
  327. if (insn->displacement.got)
  328. return;
  329. if (!insn->sib.got)
  330. insn_get_sib(insn);
  331. if (insn->modrm.nbytes) {
  332. /*
  333. * Interpreting the modrm byte:
  334. * mod = 00 - no displacement fields (exceptions below)
  335. * mod = 01 - 1-byte displacement field
  336. * mod = 10 - displacement field is 4 bytes, or 2 bytes if
  337. * address size = 2 (0x67 prefix in 32-bit mode)
  338. * mod = 11 - no memory operand
  339. *
  340. * If address size = 2...
  341. * mod = 00, r/m = 110 - displacement field is 2 bytes
  342. *
  343. * If address size != 2...
  344. * mod != 11, r/m = 100 - SIB byte exists
  345. * mod = 00, SIB base = 101 - displacement field is 4 bytes
  346. * mod = 00, r/m = 101 - rip-relative addressing, displacement
  347. * field is 4 bytes
  348. */
  349. mod = X86_MODRM_MOD(insn->modrm.value);
  350. rm = X86_MODRM_RM(insn->modrm.value);
  351. base = X86_SIB_BASE(insn->sib.value);
  352. if (mod == 3)
  353. goto out;
  354. if (mod == 1) {
  355. insn->displacement.value = get_next(signed char, insn);
  356. insn->displacement.nbytes = 1;
  357. } else if (insn->addr_bytes == 2) {
  358. if ((mod == 0 && rm == 6) || mod == 2) {
  359. insn->displacement.value =
  360. get_next(short, insn);
  361. insn->displacement.nbytes = 2;
  362. }
  363. } else {
  364. if ((mod == 0 && rm == 5) || mod == 2 ||
  365. (mod == 0 && base == 5)) {
  366. insn->displacement.value = get_next(int, insn);
  367. insn->displacement.nbytes = 4;
  368. }
  369. }
  370. }
  371. out:
  372. insn->displacement.got = 1;
  373. err_out:
  374. return;
  375. }
  376. /* Decode moffset16/32/64. Return 0 if failed */
  377. static int __get_moffset(struct insn *insn)
  378. {
  379. switch (insn->addr_bytes) {
  380. case 2:
  381. insn->moffset1.value = get_next(short, insn);
  382. insn->moffset1.nbytes = 2;
  383. break;
  384. case 4:
  385. insn->moffset1.value = get_next(int, insn);
  386. insn->moffset1.nbytes = 4;
  387. break;
  388. case 8:
  389. insn->moffset1.value = get_next(int, insn);
  390. insn->moffset1.nbytes = 4;
  391. insn->moffset2.value = get_next(int, insn);
  392. insn->moffset2.nbytes = 4;
  393. break;
  394. default: /* opnd_bytes must be modified manually */
  395. goto err_out;
  396. }
  397. insn->moffset1.got = insn->moffset2.got = 1;
  398. return 1;
  399. err_out:
  400. return 0;
  401. }
  402. /* Decode imm v32(Iz). Return 0 if failed */
  403. static int __get_immv32(struct insn *insn)
  404. {
  405. switch (insn->opnd_bytes) {
  406. case 2:
  407. insn->immediate.value = get_next(short, insn);
  408. insn->immediate.nbytes = 2;
  409. break;
  410. case 4:
  411. case 8:
  412. insn->immediate.value = get_next(int, insn);
  413. insn->immediate.nbytes = 4;
  414. break;
  415. default: /* opnd_bytes must be modified manually */
  416. goto err_out;
  417. }
  418. return 1;
  419. err_out:
  420. return 0;
  421. }
  422. /* Decode imm v64(Iv/Ov), Return 0 if failed */
  423. static int __get_immv(struct insn *insn)
  424. {
  425. switch (insn->opnd_bytes) {
  426. case 2:
  427. insn->immediate1.value = get_next(short, insn);
  428. insn->immediate1.nbytes = 2;
  429. break;
  430. case 4:
  431. insn->immediate1.value = get_next(int, insn);
  432. insn->immediate1.nbytes = 4;
  433. break;
  434. case 8:
  435. insn->immediate1.value = get_next(int, insn);
  436. insn->immediate1.nbytes = 4;
  437. insn->immediate2.value = get_next(int, insn);
  438. insn->immediate2.nbytes = 4;
  439. break;
  440. default: /* opnd_bytes must be modified manually */
  441. goto err_out;
  442. }
  443. insn->immediate1.got = insn->immediate2.got = 1;
  444. return 1;
  445. err_out:
  446. return 0;
  447. }
  448. /* Decode ptr16:16/32(Ap) */
  449. static int __get_immptr(struct insn *insn)
  450. {
  451. switch (insn->opnd_bytes) {
  452. case 2:
  453. insn->immediate1.value = get_next(short, insn);
  454. insn->immediate1.nbytes = 2;
  455. break;
  456. case 4:
  457. insn->immediate1.value = get_next(int, insn);
  458. insn->immediate1.nbytes = 4;
  459. break;
  460. case 8:
  461. /* ptr16:64 is not exist (no segment) */
  462. return 0;
  463. default: /* opnd_bytes must be modified manually */
  464. goto err_out;
  465. }
  466. insn->immediate2.value = get_next(unsigned short, insn);
  467. insn->immediate2.nbytes = 2;
  468. insn->immediate1.got = insn->immediate2.got = 1;
  469. return 1;
  470. err_out:
  471. return 0;
  472. }
  473. /**
  474. * insn_get_immediate() - Get the immediates of instruction
  475. * @insn: &struct insn containing instruction
  476. *
  477. * If necessary, first collects the instruction up to and including the
  478. * displacement bytes.
  479. * Basically, most of immediates are sign-expanded. Unsigned-value can be
  480. * get by bit masking with ((1 << (nbytes * 8)) - 1)
  481. */
  482. void insn_get_immediate(struct insn *insn)
  483. {
  484. if (insn->immediate.got)
  485. return;
  486. if (!insn->displacement.got)
  487. insn_get_displacement(insn);
  488. if (inat_has_moffset(insn->attr)) {
  489. if (!__get_moffset(insn))
  490. goto err_out;
  491. goto done;
  492. }
  493. if (!inat_has_immediate(insn->attr))
  494. /* no immediates */
  495. goto done;
  496. switch (inat_immediate_size(insn->attr)) {
  497. case INAT_IMM_BYTE:
  498. insn->immediate.value = get_next(signed char, insn);
  499. insn->immediate.nbytes = 1;
  500. break;
  501. case INAT_IMM_WORD:
  502. insn->immediate.value = get_next(short, insn);
  503. insn->immediate.nbytes = 2;
  504. break;
  505. case INAT_IMM_DWORD:
  506. insn->immediate.value = get_next(int, insn);
  507. insn->immediate.nbytes = 4;
  508. break;
  509. case INAT_IMM_QWORD:
  510. insn->immediate1.value = get_next(int, insn);
  511. insn->immediate1.nbytes = 4;
  512. insn->immediate2.value = get_next(int, insn);
  513. insn->immediate2.nbytes = 4;
  514. break;
  515. case INAT_IMM_PTR:
  516. if (!__get_immptr(insn))
  517. goto err_out;
  518. break;
  519. case INAT_IMM_VWORD32:
  520. if (!__get_immv32(insn))
  521. goto err_out;
  522. break;
  523. case INAT_IMM_VWORD:
  524. if (!__get_immv(insn))
  525. goto err_out;
  526. break;
  527. default:
  528. /* Here, insn must have an immediate, but failed */
  529. goto err_out;
  530. }
  531. if (inat_has_second_immediate(insn->attr)) {
  532. insn->immediate2.value = get_next(signed char, insn);
  533. insn->immediate2.nbytes = 1;
  534. }
  535. done:
  536. insn->immediate.got = 1;
  537. err_out:
  538. return;
  539. }
  540. /**
  541. * insn_get_length() - Get the length of instruction
  542. * @insn: &struct insn containing instruction
  543. *
  544. * If necessary, first collects the instruction up to and including the
  545. * immediates bytes.
  546. */
  547. void insn_get_length(struct insn *insn)
  548. {
  549. if (insn->length)
  550. return;
  551. if (!insn->immediate.got)
  552. insn_get_immediate(insn);
  553. insn->length = (unsigned char)((unsigned long)insn->next_byte
  554. - (unsigned long)insn->kaddr);
  555. }